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 5661da7 commit 26ff0f1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 39 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 @@ -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
Expand Down
46 changes: 11 additions & 35 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 @@ -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)
}
Expand Down
7 changes: 3 additions & 4 deletions codec_parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit 26ff0f1

Please sign in to comment.