Skip to content

Commit

Permalink
Added extra data to codec context
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Apr 13, 2024
1 parent c490911 commit 7854574
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
23 changes: 23 additions & 0 deletions bytes.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package astiav

//#cgo pkg-config: libavcodec
//#include <libavcodec/avcodec.h>
//#include <stdlib.h>
//#include <stdint.h>
import "C"
import (
"errors"
"fmt"
"unsafe"
)

Expand Down Expand Up @@ -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
}
11 changes: 11 additions & 0 deletions codec_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions codec_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 deletions codec_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ package astiav
//#cgo pkg-config: libavcodec
//#include <libavcodec/avcodec.h>
import "C"
import (
"fmt"
"unsafe"
)

// https://github.com/FFmpeg/FFmpeg/blob/n5.0/libavcodec/codec_par.h#L52
type CodecParameters struct {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions codec_parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,18 @@ 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())
b := []byte("test")
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
}

0 comments on commit 7854574

Please sign in to comment.