From 26ff0f1b02c532a9b2a53ea0c39b189554a0e0f0 Mon Sep 17 00:00:00 2001 From: Quentin Renard Date: Sat, 13 Apr 2024 10:30:20 +0200 Subject: [PATCH] Added extra data to codec context --- bytes.go | 23 ++++++++++++++++++++ codec_context.go | 11 ++++++++++ codec_context_test.go | 6 ++++++ codec_parameters.go | 46 ++++++++++------------------------------ codec_parameters_test.go | 7 +++--- 5 files changed, 54 insertions(+), 39 deletions(-) diff --git a/bytes.go b/bytes.go index f776b43..83a6cbe 100644 --- a/bytes.go +++ b/bytes.go @@ -1,10 +1,13 @@ package astiav +//#cgo pkg-config: libavcodec +//#include //#include //#include import "C" import ( "errors" + "fmt" "unsafe" ) @@ -36,3 +39,23 @@ func bytesToC(b []byte, fn func(b *C.uint8_t, size C.size_t) error) error { } return fn(ptr, C.size_t(len(b))) } + +// TODO Rename? +func setBytesWithIntSizeInC(b []byte, d **C.uint8_t, size *C.int) error { + if len(b) == 0 { + return nil + } + + if *d != nil { + C.av_freep(unsafe.Pointer(d)) + *size = 0 + } + + if *d = (*C.uint8_t)(C.av_mallocz(C.size_t(len(b) + C.AV_INPUT_BUFFER_PADDING_SIZE))); *d == nil { + return fmt.Errorf("astiav: allocation is nil") + } + + C.memcpy(unsafe.Pointer(*d), unsafe.Pointer(&b[0]), C.size_t(len(b))) + *size = C.int(len(b)) + return nil +} diff --git a/codec_context.go b/codec_context.go index 36af8c0..5ea3044 100644 --- a/codec_context.go +++ b/codec_context.go @@ -128,6 +128,17 @@ func (cc *CodecContext) ColorTransferCharacteristic() ColorTransferCharacteristi return ColorTransferCharacteristic(cc.c.color_trc) } +func (cc *CodecContext) ExtraData() []byte { + return bytesFromC(func(size *C.size_t) *C.uint8_t { + *size = C.size_t(cc.c.extradata_size) + return cc.c.extradata + }) +} + +func (cc *CodecContext) SetExtraData(b []byte) error { + return setBytesWithIntSizeInC(b, &cc.c.extradata, &cc.c.extradata_size) +} + func (cc *CodecContext) Flags() CodecContextFlags { return CodecContextFlags(cc.c.flags) } diff --git a/codec_context_test.go b/codec_context_test.go index 177e083..8d3b2b1 100644 --- a/codec_context_test.go +++ b/codec_context_test.go @@ -136,6 +136,12 @@ func TestCodecContext(t *testing.T) { require.NoError(t, err) require.Equal(t, 2, cp1.Channels()) + cc6 := AllocCodecContext(nil) + require.NotNil(t, cc6) + b := []byte("test") + require.NoError(t, cc6.SetExtraData(b)) + require.Equal(t, b, cc6.ExtraData()) + // TODO Test ReceivePacket // TODO Test SendPacket // TODO Test ReceiveFrame diff --git a/codec_parameters.go b/codec_parameters.go index 42689d0..30e1390 100644 --- a/codec_parameters.go +++ b/codec_parameters.go @@ -3,10 +3,6 @@ package astiav //#cgo pkg-config: libavcodec //#include import "C" -import ( - "fmt" - "unsafe" -) // https://github.com/FFmpeg/FFmpeg/blob/n5.0/libavcodec/codec_par.h#L52 type CodecParameters struct { @@ -97,6 +93,17 @@ func (cp *CodecParameters) ColorTransferCharacteristic() ColorTransferCharacteri return ColorTransferCharacteristic(cp.c.color_trc) } +func (cp *CodecParameters) ExtraData() []byte { + return bytesFromC(func(size *C.size_t) *C.uint8_t { + *size = C.size_t(cp.c.extradata_size) + return cp.c.extradata + }) +} + +func (cp *CodecParameters) SetExtraData(b []byte) error { + return setBytesWithIntSizeInC(b, &cp.c.extradata, &cp.c.extradata_size) +} + func (cp *CodecParameters) FrameSize() int { return int(cp.c.frame_size) } @@ -153,37 +160,6 @@ func (cp *CodecParameters) SetSampleAspectRatio(r Rational) { cp.c.sample_aspect_ratio = r.c } -func (cp *CodecParameters) ExtraData() []byte { - return bytesFromC(func(size *C.size_t) *C.uint8_t { - if cp.c.extradata == nil { - *size = C.size_t(0) - return nil - } - *size = C.size_t(cp.c.extradata_size) - return cp.c.extradata - }) -} - -func (cp *CodecParameters) SetExtraData(extraData []byte) error { - if len(extraData) == 0 { - return nil - } - - if cp.c.extradata != nil { - C.av_freep(unsafe.Pointer(&cp.c.extradata)) - cp.c.extradata_size = 0 - } - - extradataSize := len(extraData) - if cp.c.extradata = (*C.uint8_t)(C.av_mallocz(C.size_t(extradataSize + C.AV_INPUT_BUFFER_PADDING_SIZE))); cp.c.extradata == nil { - return fmt.Errorf("astiav: allocation is nil") - } - - C.memcpy(unsafe.Pointer(cp.c.extradata), unsafe.Pointer(&extraData[0]), C.size_t(extradataSize)) - cp.c.extradata_size = C.int(extradataSize) - return nil -} - func (cp *CodecParameters) SampleFormat() SampleFormat { return SampleFormat(cp.c.format) } diff --git a/codec_parameters_test.go b/codec_parameters_test.go index dbb8d5b..8fca69f 100644 --- a/codec_parameters_test.go +++ b/codec_parameters_test.go @@ -99,8 +99,7 @@ func TestCodecParameters(t *testing.T) { require.Equal(t, 4, cp6.SampleRate()) cp6.SetWidth(2) require.Equal(t, 2, cp6.Width()) - - extraBytes := []byte{0, 0, 0, 1} - require.NoError(t, cp6.SetExtraData(extraBytes)) - require.Equal(t, extraBytes, cp6.ExtraData()) + b := []byte("test") + require.NoError(t, cp6.SetExtraData(b)) + require.Equal(t, b, cp6.ExtraData()) }