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

hardware filter #114

Closed
wants to merge 14 commits into from
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_STORE
coverage.out
coverage.out
/.idea
42 changes: 42 additions & 0 deletions filter_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (bfc *BuffersinkFilterContext) Width() int {

type BuffersrcFilterContext struct {
fc *FilterContext
bp *BuffersrcParameters
}

func newBuffersrcFilterContext(fc *FilterContext) *BuffersrcFilterContext {
Expand All @@ -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)
}
}
34 changes: 34 additions & 0 deletions filter_context_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
Loading