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 85058c3..4f741e9 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 5a54f3e..594e6cf 100644 --- a/codec_context_test.go +++ b/codec_context_test.go @@ -132,6 +132,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{0, 0, 0, 1} + 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 fd0155b..5f04078 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) } @@ -145,6 +152,7 @@ func (cp *CodecParameters) SetSampleAspectRatio(r Rational) { cp.c.sample_aspect_ratio = r.c } +<<<<<<< Updated upstream func (cp *CodecParameters) ExtraData() []byte { return bytesFromC(func(size *C.size_t) *C.uint8_t { if cp.c.extradata == nil { @@ -180,6 +188,8 @@ func (cp *CodecParameters) SideData() *PacketSideData { return newPacketSideDataFromC(&cp.c.coded_side_data, &cp.c.nb_coded_side_data) } +======= +>>>>>>> Stashed changes func (cp *CodecParameters) SampleFormat() SampleFormat { return SampleFormat(cp.c.format) } diff --git a/codec_parameters_test.go b/codec_parameters_test.go index dbdec73..09b2e88 100644 --- a/codec_parameters_test.go +++ b/codec_parameters_test.go @@ -95,6 +95,7 @@ func TestCodecParameters(t *testing.T) { require.Equal(t, 4, cp6.SampleRate()) cp6.SetWidth(2) require.Equal(t, 2, cp6.Width()) +<<<<<<< Updated upstream extraBytes := []byte{0, 0, 0, 1} require.NoError(t, cp6.SetExtraData(extraBytes)) require.Equal(t, extraBytes, cp6.ExtraData()) @@ -102,4 +103,10 @@ func TestCodecParameters(t *testing.T) { sd := cp6.SideData() require.NoError(t, sd.Add(PacketSideDataTypeDisplaymatrix, b)) require.Equal(t, b, sd.Get(PacketSideDataTypeDisplaymatrix)) +======= + + b := []byte{0, 0, 0, 1} + require.NoError(t, cp6.SetExtraData(b)) + require.Equal(t, b, cp6.ExtraData()) +>>>>>>> Stashed changes }