Skip to content

Commit

Permalink
Removed args from NewXXXFilterContext()
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Dec 19, 2024
1 parent ab81ca0 commit 4b35c9a
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 89 deletions.
5 changes: 5 additions & 0 deletions BREAKING_CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v0.29.0

- `NewFilterContext` has been removed, use `NewBuffersinkFilterContext` or `NewBuffersrcFilterContext` instead
- `args` has been removed from `NewBuffersinkFilterContext` and `NewBuffersrcFilterContext`. Instead, after calling `NewBuffersrcFilterContext`, you need to use `BuffersrcFilterContext`.`SetParameters` then `BuffersrcFilterContext`.`Initialize`. You don't need to use anything else after calling `NewBuffersinkFilterContext`

# v0.27.0

- make sure to call the `IOInterrupter`.`Free` method after using `NewIOInterrupter`
Expand Down
5 changes: 5 additions & 0 deletions buffersrc_filter_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func (bfc *BuffersrcFilterContext) FilterContext() *FilterContext {
return bfc.fc
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi.html#ga8c15af28902395399fe455f6f8236848
func (bfc *BuffersrcFilterContext) Initialize() error {
return newError(C.avfilter_init_dict(bfc.fc.c, nil))
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersrc.html#ga398cd2a84f8b4a588197ab9d90135048
func (bfc *BuffersrcFilterContext) SetParameters(bfcp *BuffersrcFilterContextParameters) error {
return newError(C.av_buffersrc_parameters_set(bfc.fc.c, bfcp.c))
Expand Down
111 changes: 111 additions & 0 deletions buffersrc_filter_context_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,47 @@ func (bfcp *BuffersrcFilterContextParameters) Free() {
}
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a5267368bf88b4f2a65a5e06ac3f9ecd4
func (bfcp *BuffersrcFilterContextParameters) ChannelLayout() ChannelLayout {
l, _ := newChannelLayoutFromC(&bfcp.c.ch_layout).clone()
return l
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a5267368bf88b4f2a65a5e06ac3f9ecd4
func (bfcp *BuffersrcFilterContextParameters) SetChannelLayout(l ChannelLayout) {
l.copy(&bfcp.c.ch_layout) //nolint: errcheck
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a442add2b039f416dd7c92ccf1ccd0d3b
func (bfcp *BuffersrcFilterContextParameters) ColorRange() ColorRange {
return ColorRange(bfcp.c.color_range)
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a442add2b039f416dd7c92ccf1ccd0d3b
func (bfcp *BuffersrcFilterContextParameters) SetColorRange(r ColorRange) {
bfcp.c.color_range = C.enum_AVColorRange(r)
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a700226626af70f787c930d7506554757
func (bfcp *BuffersrcFilterContextParameters) ColorSpace() ColorSpace {
return ColorSpace(bfcp.c.color_space)
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a700226626af70f787c930d7506554757
func (bfcp *BuffersrcFilterContextParameters) SetColorSpace(s ColorSpace) {
bfcp.c.color_space = C.enum_AVColorSpace(s)
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a032a202496206e18449c66233058647a
func (bfcp *BuffersrcFilterContextParameters) Framerate() Rational {
return newRationalFromC(bfcp.c.frame_rate)
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a032a202496206e18449c66233058647a
func (bfcp *BuffersrcFilterContextParameters) SetFramerate(f Rational) {
bfcp.c.frame_rate = f.c
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a86c49b4202433037c9e2b0b6ae541534
func (bfcp *BuffersrcFilterContextParameters) SetHardwareFrameContext(hfc *HardwareFrameContext) {
if bfcp.c.hw_frames_ctx != nil {
Expand All @@ -41,3 +82,73 @@ func (bfcp *BuffersrcFilterContextParameters) SetHardwareFrameContext(hfc *Hardw
bfcp.c.hw_frames_ctx = nil
}
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a89783d603b84908fb1998bbbea981b70
func (bfcp *BuffersrcFilterContextParameters) Height() int {
return int(bfcp.c.height)
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a89783d603b84908fb1998bbbea981b70
func (bfcp *BuffersrcFilterContextParameters) SetHeight(height int) {
bfcp.c.height = C.int(height)
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a56f28f81f1a86cecc39a8d61674912b8
func (bfcp *BuffersrcFilterContextParameters) PixelFormat() PixelFormat {
return PixelFormat(bfcp.c.format)
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a56f28f81f1a86cecc39a8d61674912b8
func (bfcp *BuffersrcFilterContextParameters) SetPixelFormat(f PixelFormat) {
bfcp.c.format = C.int(f)
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#ae47c141ea7a7770351613242229f951a
func (bfcp *BuffersrcFilterContextParameters) SampleAspectRatio() Rational {
return newRationalFromC(bfcp.c.sample_aspect_ratio)
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#ae47c141ea7a7770351613242229f951a
func (bfcp *BuffersrcFilterContextParameters) SetSampleAspectRatio(r Rational) {
bfcp.c.sample_aspect_ratio = r.c
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a56f28f81f1a86cecc39a8d61674912b8
func (bfcp *BuffersrcFilterContextParameters) SampleFormat() SampleFormat {
return SampleFormat(bfcp.c.format)
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a56f28f81f1a86cecc39a8d61674912b8
func (bfcp *BuffersrcFilterContextParameters) SetSampleFormat(f SampleFormat) {
bfcp.c.format = C.int(f)
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a34a1613f1e80f8520c159fac59e29834
func (bfcp *BuffersrcFilterContextParameters) SampleRate() int {
return int(bfcp.c.sample_rate)
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a34a1613f1e80f8520c159fac59e29834
func (bfcp *BuffersrcFilterContextParameters) SetSampleRate(sampleRate int) {
bfcp.c.sample_rate = C.int(sampleRate)
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a7767325c1259942a33586f05c90e38b0
func (bfcp *BuffersrcFilterContextParameters) TimeBase() Rational {
return newRationalFromC(bfcp.c.time_base)
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a7767325c1259942a33586f05c90e38b0
func (bfcp *BuffersrcFilterContextParameters) SetTimeBase(r Rational) {
bfcp.c.time_base = r.c
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a6c6f3d9ed8b427070e9055e7ac61263f
func (bfcp *BuffersrcFilterContextParameters) Width() int {
return int(bfcp.c.width)
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a6c6f3d9ed8b427070e9055e7ac61263f
func (bfcp *BuffersrcFilterContextParameters) SetWidth(width int) {
bfcp.c.width = C.int(width)
}
34 changes: 34 additions & 0 deletions buffersrc_filter_context_parameters_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package astiav

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestBuffersrcFilterContextParameters(t *testing.T) {
p := AllocBuffersrcFilterContextParameters()
defer p.Free()
p.SetChannelLayout(ChannelLayoutStereo)
require.Equal(t, ChannelLayoutStereo, p.ChannelLayout())
p.SetColorRange(ColorRangeMpeg)
require.Equal(t, ColorRangeMpeg, p.ColorRange())
p.SetColorSpace(ColorSpaceBt470Bg)
require.Equal(t, ColorSpaceBt470Bg, p.ColorSpace())
p.SetFramerate(NewRational(1, 2))
require.Equal(t, NewRational(1, 2), p.Framerate())
p.SetHeight(1)
require.Equal(t, 1, p.Height())
p.SetPixelFormat(PixelFormatRgba)
require.Equal(t, PixelFormatRgba, p.PixelFormat())
p.SetSampleAspectRatio(NewRational(3, 4))
require.Equal(t, NewRational(3, 4), p.SampleAspectRatio())
p.SetSampleFormat(SampleFormatDblp)
require.Equal(t, SampleFormatDblp, p.SampleFormat())
p.SetSampleRate(2)
require.Equal(t, 2, p.SampleRate())
p.SetTimeBase(NewRational(5, 6))
require.Equal(t, NewRational(5, 6), p.TimeBase())
p.SetWidth(3)
require.Equal(t, 3, p.Width())
}
6 changes: 3 additions & 3 deletions class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func TestClassers(t *testing.T) {
require.NotNil(t, cc)
bufferSink := FindFilterByName("buffersink")
require.NotNil(t, bufferSink)
fc1, err := f1.NewFilterContext(bufferSink, "filter_out", nil)
bfc1, err := f1.NewBuffersinkFilterContext(bufferSink, "filter_out")
require.NoError(t, err)
_, err = f2.NewFilterContext(bufferSink, "filter_out", nil)
_, err = f2.NewBuffersinkFilterContext(bufferSink, "filter_out")
require.NoError(t, err)
fmc1 := AllocFormatContext()
fmc2 := AllocFormatContext()
Expand All @@ -64,7 +64,7 @@ func TestClassers(t *testing.T) {

bfc.Free()
cc.Free()
fc1.Free()
bfc1.FilterContext().Free()
f1.Free()
f2.Free()
fmc1.Free()
Expand Down
31 changes: 23 additions & 8 deletions examples/filtering/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"flag"
"fmt"
"log"
"strconv"
"strings"

"github.com/asticode/go-astiav"
Expand Down Expand Up @@ -250,20 +249,36 @@ func initFilter() (err error) {
}

// Create filter contexts
if s.buffersrcContext, err = s.filterGraph.NewBuffersrcFilterContext(buffersrc, "in", astiav.FilterArgs{
"pix_fmt": strconv.Itoa(int(s.decCodecContext.PixelFormat())),
"pixel_aspect": s.decCodecContext.SampleAspectRatio().String(),
"time_base": s.inputStream.TimeBase().String(),
"video_size": strconv.Itoa(s.decCodecContext.Width()) + "x" + strconv.Itoa(s.decCodecContext.Height()),
}); err != nil {
if s.buffersrcContext, err = s.filterGraph.NewBuffersrcFilterContext(buffersrc, "in"); err != nil {
err = fmt.Errorf("main: creating buffersrc context failed: %w", err)
return
}
if s.buffersinkContext, err = s.filterGraph.NewBuffersinkFilterContext(buffersink, "in", nil); err != nil {
if s.buffersinkContext, err = s.filterGraph.NewBuffersinkFilterContext(buffersink, "in"); err != nil {
err = fmt.Errorf("main: creating buffersink context failed: %w", err)
return
}

// Create buffersrc context parameters
buffersrcContextParameters := astiav.AllocBuffersrcFilterContextParameters()
defer buffersrcContextParameters.Free()
buffersrcContextParameters.SetHeight(s.decCodecContext.Height())
buffersrcContextParameters.SetPixelFormat(s.decCodecContext.PixelFormat())
buffersrcContextParameters.SetSampleAspectRatio(s.decCodecContext.SampleAspectRatio())
buffersrcContextParameters.SetTimeBase(s.inputStream.TimeBase())
buffersrcContextParameters.SetWidth(s.decCodecContext.Width())

// Set buffersrc context parameters
if err = s.buffersrcContext.SetParameters(buffersrcContextParameters); err != nil {
err = fmt.Errorf("main: setting buffersrc context parameters failed: %w", err)
return
}

// Initialize buffersrc context
if err = s.buffersrcContext.Initialize(); err != nil {
err = fmt.Errorf("main: initializing buffersrc context failed: %w", err)
return
}

// Update outputs
outputs.SetName("in")
outputs.SetFilterContext(s.buffersrcContext.FilterContext())
Expand Down
35 changes: 20 additions & 15 deletions examples/hardware_decoding_filtering/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"flag"
"fmt"
"log"
"strconv"
"strings"

"github.com/asticode/go-astiav"
Expand Down Expand Up @@ -294,28 +293,34 @@ func initFilter() (err error) {
}

// Create filter contexts
if buffersrcContext, err = filterGraph.NewBuffersrcFilterContext(buffersrc, "in", astiav.FilterArgs{
"pix_fmt": strconv.Itoa(int(decCodecContext.PixelFormat())),
"pixel_aspect": decCodecContext.SampleAspectRatio().String(),
"time_base": inputStream.TimeBase().String(),
"video_size": strconv.Itoa(decCodecContext.Width()) + "x" + strconv.Itoa(decCodecContext.Height()),
}); err != nil {
if buffersrcContext, err = filterGraph.NewBuffersrcFilterContext(buffersrc, "in"); err != nil {
err = fmt.Errorf("main: creating buffersrc context failed: %w", err)
return
}
if buffersinkContext, err = filterGraph.NewBuffersinkFilterContext(buffersink, "in", nil); err != nil {
if buffersinkContext, err = filterGraph.NewBuffersinkFilterContext(buffersink, "in"); err != nil {
err = fmt.Errorf("main: creating buffersink context failed: %w", err)
return
}

// Create buffersrc parameters
bfcp := astiav.AllocBuffersrcFilterContextParameters()
defer bfcp.Free()
bfcp.SetHardwareFrameContext(decCodecContext.HardwareFrameContext())
// Create buffersrc context parameters
buffersrcContextParameters := astiav.AllocBuffersrcFilterContextParameters()
defer buffersrcContextParameters.Free()
buffersrcContextParameters.SetHardwareFrameContext(decCodecContext.HardwareFrameContext())
buffersrcContextParameters.SetHeight(decCodecContext.Height())
buffersrcContextParameters.SetPixelFormat(decCodecContext.PixelFormat())
buffersrcContextParameters.SetSampleAspectRatio(decCodecContext.SampleAspectRatio())
buffersrcContextParameters.SetTimeBase(inputStream.TimeBase())
buffersrcContextParameters.SetWidth(decCodecContext.Width())

// Set buffersrc context parameters
if err = buffersrcContext.SetParameters(buffersrcContextParameters); err != nil {
err = fmt.Errorf("main: setting buffersrc context parameters failed: %w", err)
return
}

// Set buffersrc parameters
if err = buffersrcContext.SetParameters(bfcp); err != nil {
err = fmt.Errorf("main: setting buffersrc parameters failed: %w", err)
// Initialize buffersrc context
if err = buffersrcContext.Initialize(); err != nil {
err = fmt.Errorf("main: initializing buffersrc context failed: %w", err)
return
}

Expand Down
43 changes: 27 additions & 16 deletions examples/transcoding/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"flag"
"fmt"
"log"
"strconv"
"strings"

"github.com/asticode/go-astiav"
Expand Down Expand Up @@ -378,29 +377,29 @@ func initFilters() (err error) {
}
c.Add(inputs.Free)

// Create buffersrc context parameters
buffersrcContextParameters := astiav.AllocBuffersrcFilterContextParameters()
defer buffersrcContextParameters.Free()

// Switch on media type
var args astiav.FilterArgs
var buffersrc, buffersink *astiav.Filter
var content string
switch s.decCodecContext.MediaType() {
case astiav.MediaTypeAudio:
args = astiav.FilterArgs{
"channel_layout": s.decCodecContext.ChannelLayout().String(),
"sample_fmt": s.decCodecContext.SampleFormat().Name(),
"sample_rate": strconv.Itoa(s.decCodecContext.SampleRate()),
"time_base": s.decCodecContext.TimeBase().String(),
}
buffersrc = astiav.FindFilterByName("abuffer")
buffersrcContextParameters.SetChannelLayout(s.decCodecContext.ChannelLayout())
buffersrcContextParameters.SetSampleFormat(s.decCodecContext.SampleFormat())
buffersrcContextParameters.SetSampleRate(s.decCodecContext.SampleRate())
buffersrcContextParameters.SetTimeBase(s.decCodecContext.TimeBase())
buffersink = astiav.FindFilterByName("abuffersink")
content = fmt.Sprintf("aformat=sample_fmts=%s:channel_layouts=%s", s.encCodecContext.SampleFormat().Name(), s.encCodecContext.ChannelLayout().String())
default:
args = astiav.FilterArgs{
"pix_fmt": strconv.Itoa(int(s.decCodecContext.PixelFormat())),
"pixel_aspect": s.decCodecContext.SampleAspectRatio().String(),
"time_base": s.inputStream.TimeBase().String(),
"video_size": strconv.Itoa(s.decCodecContext.Width()) + "x" + strconv.Itoa(s.decCodecContext.Height()),
}
buffersrc = astiav.FindFilterByName("buffer")
buffersrcContextParameters.SetHeight(s.decCodecContext.Height())
buffersrcContextParameters.SetPixelFormat(s.decCodecContext.PixelFormat())
buffersrcContextParameters.SetSampleAspectRatio(s.decCodecContext.SampleAspectRatio())
buffersrcContextParameters.SetTimeBase(s.inputStream.TimeBase())
buffersrcContextParameters.SetWidth(s.decCodecContext.Width())
buffersink = astiav.FindFilterByName("buffersink")
content = fmt.Sprintf("format=pix_fmts=%s", s.encCodecContext.PixelFormat().Name())
}
Expand All @@ -416,15 +415,27 @@ func initFilters() (err error) {
}

// Create filter contexts
if s.buffersrcContext, err = s.filterGraph.NewBuffersrcFilterContext(buffersrc, "in", args); err != nil {
if s.buffersrcContext, err = s.filterGraph.NewBuffersrcFilterContext(buffersrc, "in"); err != nil {
err = fmt.Errorf("main: creating buffersrc context failed: %w", err)
return
}
if s.buffersinkContext, err = s.filterGraph.NewBuffersinkFilterContext(buffersink, "out", nil); err != nil {
if s.buffersinkContext, err = s.filterGraph.NewBuffersinkFilterContext(buffersink, "out"); err != nil {
err = fmt.Errorf("main: creating buffersink context failed: %w", err)
return
}

// Set buffersrc context parameters
if err = s.buffersrcContext.SetParameters(buffersrcContextParameters); err != nil {
err = fmt.Errorf("main: setting buffersrc context parameters failed: %w", err)
return
}

// Initialize buffersrc context
if err = s.buffersrcContext.Initialize(); err != nil {
err = fmt.Errorf("main: initializing buffersrc context failed: %w", err)
return
}

// Update outputs
outputs.SetName("in")
outputs.SetFilterContext(s.buffersrcContext.FilterContext())
Expand Down
Loading

0 comments on commit 4b35c9a

Please sign in to comment.