Skip to content

Commit

Permalink
add SetExtraData
Browse files Browse the repository at this point in the history
  • Loading branch information
Cacsjep committed Mar 29, 2024
1 parent 443a5f5 commit 4e05643
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions codec_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ 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 @@ -141,6 +145,27 @@ func (cp *CodecParameters) SetSampleAspectRatio(r Rational) {
cp.c.sample_aspect_ratio = r.c
}

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)
cp.c.extradata = (*C.uint8_t)(C.av_mallocz(C.size_t(extradataSize + C.AV_INPUT_BUFFER_PADDING_SIZE)))
if cp.c.extradata == nil {
return fmt.Errorf("failed to allocate extradata")
}

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

0 comments on commit 4e05643

Please sign in to comment.