From db69235e305c513928a46d301b5ced1a426bca2c Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Tue, 1 Feb 2022 17:29:45 +0100 Subject: [PATCH] improve h264 decode examples --- .../h264decoder.go | 14 +++++++------- .../client-read-h264-convert-to-jpeg/main.go | 19 +++++++++++++------ .../client-read-h264-decode/h264decoder.go | 14 +++++++------- examples/client-read-h264-decode/main.go | 19 +++++++++++++------ 4 files changed, 40 insertions(+), 26 deletions(-) diff --git a/examples/client-read-h264-convert-to-jpeg/h264decoder.go b/examples/client-read-h264-convert-to-jpeg/h264decoder.go index f55bc7ee..7e89cc7e 100644 --- a/examples/client-read-h264-convert-to-jpeg/h264decoder.go +++ b/examples/client-read-h264-convert-to-jpeg/h264decoder.go @@ -106,12 +106,6 @@ func (d *h264Decoder) decode(nalu []byte) (image.Image, error) { C.sws_freeContext(d.swsCtx) } - d.swsCtx = C.sws_getContext(d.srcFrame.width, d.srcFrame.height, C.AV_PIX_FMT_YUV420P, // d.codecCtx.pix_fmt, - d.srcFrame.width, d.srcFrame.height, C.AV_PIX_FMT_RGBA, C.SWS_BILINEAR, nil, nil, nil) - if d.swsCtx == nil { - return nil, fmt.Errorf("sws_getContext() err") - } - d.dstFrame = C.av_frame_alloc() d.dstFrame.format = C.AV_PIX_FMT_RGBA d.dstFrame.width = d.srcFrame.width @@ -122,13 +116,19 @@ func (d *h264Decoder) decode(nalu []byte) (image.Image, error) { return nil, fmt.Errorf("av_frame_get_buffer() err") } + d.swsCtx = C.sws_getContext(d.srcFrame.width, d.srcFrame.height, C.AV_PIX_FMT_YUV420P, + d.dstFrame.width, d.dstFrame.height, (int32)(d.dstFrame.format), C.SWS_BILINEAR, nil, nil, nil) + if d.swsCtx == nil { + return nil, fmt.Errorf("sws_getContext() err") + } + dstFrameSize := C.av_image_get_buffer_size((int32)(d.dstFrame.format), d.dstFrame.width, d.dstFrame.height, 1) d.dstFramePtr = (*[1 << 30]uint8)(unsafe.Pointer(d.dstFrame.data[0]))[:dstFrameSize:dstFrameSize] } // convert frame from YUV420 to RGB res = C.sws_scale(d.swsCtx, frameData(d.srcFrame), frameLineSize(d.srcFrame), - 0, d.codecCtx.height, frameData(d.dstFrame), frameLineSize(d.dstFrame)) + 0, d.srcFrame.height, frameData(d.dstFrame), frameLineSize(d.dstFrame)) if res < 0 { return nil, fmt.Errorf("sws_scale() err") } diff --git a/examples/client-read-h264-convert-to-jpeg/main.go b/examples/client-read-h264-convert-to-jpeg/main.go index 77b43a73..3036c747 100644 --- a/examples/client-read-h264-convert-to-jpeg/main.go +++ b/examples/client-read-h264-convert-to-jpeg/main.go @@ -68,15 +68,15 @@ func main() { } // find the H264 track - h264Track := func() int { + h264tr, h264trID := func() (*gortsplib.TrackH264, int) { for i, track := range tracks { - if _, ok := track.(*gortsplib.TrackH264); ok { - return i + if h264tr, ok := track.(*gortsplib.TrackH264); ok { + return h264tr, i } } - return -1 + return nil, -1 }() - if h264Track < 0 { + if h264trID < 0 { panic("H264 track not found") } @@ -90,10 +90,17 @@ func main() { } defer h264dec.close() + // send SPS and PPS to the decoder + if h264tr.SPS() == nil || h264tr.PPS() == nil { + panic("SPS or PPS not provided by the SDP") + } + h264dec.decode(h264tr.SPS()) + h264dec.decode(h264tr.PPS()) + // called when a RTP packet arrives saveCount := 0 c.OnPacketRTP = func(trackID int, payload []byte) { - if trackID != h264Track { + if trackID != h264trID { return } diff --git a/examples/client-read-h264-decode/h264decoder.go b/examples/client-read-h264-decode/h264decoder.go index f55bc7ee..7e89cc7e 100644 --- a/examples/client-read-h264-decode/h264decoder.go +++ b/examples/client-read-h264-decode/h264decoder.go @@ -106,12 +106,6 @@ func (d *h264Decoder) decode(nalu []byte) (image.Image, error) { C.sws_freeContext(d.swsCtx) } - d.swsCtx = C.sws_getContext(d.srcFrame.width, d.srcFrame.height, C.AV_PIX_FMT_YUV420P, // d.codecCtx.pix_fmt, - d.srcFrame.width, d.srcFrame.height, C.AV_PIX_FMT_RGBA, C.SWS_BILINEAR, nil, nil, nil) - if d.swsCtx == nil { - return nil, fmt.Errorf("sws_getContext() err") - } - d.dstFrame = C.av_frame_alloc() d.dstFrame.format = C.AV_PIX_FMT_RGBA d.dstFrame.width = d.srcFrame.width @@ -122,13 +116,19 @@ func (d *h264Decoder) decode(nalu []byte) (image.Image, error) { return nil, fmt.Errorf("av_frame_get_buffer() err") } + d.swsCtx = C.sws_getContext(d.srcFrame.width, d.srcFrame.height, C.AV_PIX_FMT_YUV420P, + d.dstFrame.width, d.dstFrame.height, (int32)(d.dstFrame.format), C.SWS_BILINEAR, nil, nil, nil) + if d.swsCtx == nil { + return nil, fmt.Errorf("sws_getContext() err") + } + dstFrameSize := C.av_image_get_buffer_size((int32)(d.dstFrame.format), d.dstFrame.width, d.dstFrame.height, 1) d.dstFramePtr = (*[1 << 30]uint8)(unsafe.Pointer(d.dstFrame.data[0]))[:dstFrameSize:dstFrameSize] } // convert frame from YUV420 to RGB res = C.sws_scale(d.swsCtx, frameData(d.srcFrame), frameLineSize(d.srcFrame), - 0, d.codecCtx.height, frameData(d.dstFrame), frameLineSize(d.dstFrame)) + 0, d.srcFrame.height, frameData(d.dstFrame), frameLineSize(d.dstFrame)) if res < 0 { return nil, fmt.Errorf("sws_scale() err") } diff --git a/examples/client-read-h264-decode/main.go b/examples/client-read-h264-decode/main.go index 546eb722..5288d5a3 100644 --- a/examples/client-read-h264-decode/main.go +++ b/examples/client-read-h264-decode/main.go @@ -45,15 +45,15 @@ func main() { } // find the H264 track - h264Track := func() int { + h264tr, h264trID := func() (*gortsplib.TrackH264, int) { for i, track := range tracks { - if _, ok := track.(*gortsplib.TrackH264); ok { - return i + if h264tr, ok := track.(*gortsplib.TrackH264); ok { + return h264tr, i } } - return -1 + return nil, -1 }() - if h264Track < 0 { + if h264trID < 0 { panic("H264 track not found") } @@ -67,9 +67,16 @@ func main() { } defer h264dec.close() + // send SPS and PPS to the decoder + if h264tr.SPS() == nil || h264tr.PPS() == nil { + panic("SPS or PPS not provided by the SDP") + } + h264dec.decode(h264tr.SPS()) + h264dec.decode(h264tr.PPS()) + // called when a RTP packet arrives c.OnPacketRTP = func(trackID int, payload []byte) { - if trackID != h264Track { + if trackID != h264trID { return }