Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SetExtraData to CodecParameters to set extradata #56

Merged
merged 5 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)))
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
if cp.c.extradata == nil {
return fmt.Errorf("failed to allocate extradata")
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
}

C.memcpy(unsafe.Pointer(cp.c.extradata), unsafe.Pointer(&extraData[0]), C.size_t(extradataSize))
cp.c.extradata_size = C.int(extradataSize)
return nil
}

Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
func (cp *CodecParameters) SampleFormat() SampleFormat {
return SampleFormat(cp.c.format)
}
Expand Down
3 changes: 3 additions & 0 deletions codec_parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func TestCodecParameters(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 2, cp5.Channels())

err = cp5.SetExtraData([]byte{0, 0, 0, 1})
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved

cp6 := AllocCodecParameters()
require.NotNil(t, cp6)
defer cp6.Free()
Expand Down
Loading