diff --git a/.gitignore b/.gitignore index ee79665..f5a73b8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_STORE -coverage.out \ No newline at end of file +coverage.out +/.idea \ No newline at end of file diff --git a/filter_context.go b/filter_context.go index 7a1eb68..065b0d0 100644 --- a/filter_context.go +++ b/filter_context.go @@ -128,6 +128,7 @@ func (bfc *BuffersinkFilterContext) Width() int { type BuffersrcFilterContext struct { fc *FilterContext + bp *BuffersrcParameters } func newBuffersrcFilterContext(fc *FilterContext) *BuffersrcFilterContext { @@ -146,3 +147,44 @@ func (bfc *BuffersrcFilterContext) AddFrame(f *Frame, fs BuffersrcFlags) error { func (bfc *BuffersrcFilterContext) FilterContext() *FilterContext { return bfc.fc } + +func (bfc *BuffersrcFilterContext) BuffersrcParameters() *BuffersrcParameters { + return newBuffersrcParametersFromC(bfc.bp.p) +} + +// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersrc.html#ga398cd2a84f8b4a588197ab9d90135048 +func (bfc *BuffersrcFilterContext) SetBuffersrcParameters(bp *BuffersrcParameters) error { + if bp != nil { + bfc.bp = bp + } + return newError(C.av_buffersrc_parameters_set(bfc.fc.c, bfc.bp.p)) +} + +// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html +type BuffersrcParameters struct { + p *C.AVBufferSrcParameters + hfc *HardwareFrameContext +} + +// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersrc.html#gaae82d4f8a69757ce01421dd3167861a5 +func AllocBuffersrcParameters() *BuffersrcParameters { + return newBuffersrcParametersFromC(C.av_buffersrc_parameters_alloc()) +} + +func newBuffersrcParametersFromC(p *C.AVBufferSrcParameters) *BuffersrcParameters { + if p == nil { + return nil + } + return &BuffersrcParameters{p: p} +} + +func (bp *BuffersrcParameters) HardwareFrameContext() *HardwareFrameContext { + return newHardwareFrameContextFromC(bp.p.hw_frames_ctx) +} + +func (bp *BuffersrcParameters) SetHardwareFrameContext(hfc *HardwareFrameContext) { + bp.hfc = hfc + if bp.hfc != nil { + bp.p.hw_frames_ctx = C.av_buffer_ref(bp.hfc.c) + } +} diff --git a/filter_context_test.go b/filter_context_test.go new file mode 100644 index 0000000..c4cfc88 --- /dev/null +++ b/filter_context_test.go @@ -0,0 +1,34 @@ +package astiav + +import ( + "github.com/stretchr/testify/require" + "testing" +) + +func TestBuffersrcParameters(t *testing.T) { + buffersrcParameters := AllocBuffersrcParameters() + require.NotNil(t, buffersrcParameters) + + hardwareDeviceCtx, err := CreateHardwareDeviceContext(HardwareDeviceTypeCUDA, "0", nil, 0) + if err == nil { + require.NoError(t, err) + hardwareFrameCtx := AllocHardwareFrameContext(hardwareDeviceCtx) + require.NotNil(t, hardwareFrameCtx) + buffersrcParameters.SetHardwareFrameContext(hardwareFrameCtx) + require.Equal(t, hardwareFrameCtx, buffersrcParameters.HardwareFrameContext()) + + args := FilterArgs{ + "pix_fmt": "0", + "pixel_aspect": "1/1", + "time_base": "1/1000", + "video_size": "1920x1080", + } + buffersrc := FindFilterByName("buffer") + fg := AllocFilterGraph() + buffersrcCtx, err := fg.NewBuffersrcFilterContext(buffersrc, "in", args) + require.NoError(t, err) + err = buffersrcCtx.SetBuffersrcParameters(buffersrcParameters) + require.NoError(t, err) + require.Equal(t, buffersrcParameters.HardwareFrameContext(), buffersrcCtx.BuffersrcParameters().HardwareFrameContext()) + } +}