Skip to content

Commit

Permalink
Added hardware filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Dec 3, 2024
1 parent 7900de7 commit 09052ff
Show file tree
Hide file tree
Showing 11 changed files with 586 additions and 373 deletions.
1 change: 1 addition & 0 deletions BREAKING_CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# v0.25.0

- `CodecParameters`.`CodecType` and `CodecParameters`.`SetCodecType` have been removed, use `CodecParameters`.`MediaType` and `CodecParameters`.`SetMediaType` instead
- `HardwareFrameContext`.`SetPixelFormat` has been replaced with `HardwareFrameContext`.`SetHardwarePixelFormat`

# v0.24.0

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Examples are located in the [examples](examples) directory and mirror as much as
|Demuxing/Decoding|[see](examples/demuxing_decoding/main.go)|[see](https://github.com/FFmpeg/FFmpeg/blob/n7.0/doc/examples/demuxing_decoding.c)
|Filtering|[see](examples/filtering/main.go)|[see](https://github.com/FFmpeg/FFmpeg/blob/n7.0/doc/examples/filtering_video.c)
|Frame data manipulation|[see](examples/frame_data_manipulation/main.go)|X
|Hardware Decoding|[see](examples/hardware_decoding/main.go)|[see](https://github.com/FFmpeg/FFmpeg/blob/n7.0/doc/examples/hw_decode.c)
|Hardware Decoding/Filtering|[see](examples/hardware_decoding_filtering/main.go)|[see](https://github.com/FFmpeg/FFmpeg/blob/n7.0/doc/examples/hw_decode.c)
|Hardware Encoding|[see](examples/hardware_encoding/main.go)|[see](https://github.com/FFmpeg/FFmpeg/blob/n7.0/doc/examples/vaapi_encode.c)
|Remuxing|[see](examples/remuxing/main.go)|[see](https://github.com/FFmpeg/FFmpeg/blob/n7.0/doc/examples/remuxing.c)
|Resampling audio|[see](examples/resampling_audio/main.go)|[see](https://github.com/FFmpeg/FFmpeg/blob/n7.0/doc/examples/resample_audio.c)
Expand Down
87 changes: 87 additions & 0 deletions buffersink_filter_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package astiav

//#include <libavfilter/buffersink.h>
import "C"

type BuffersinkFilterContext struct {
fc *FilterContext
}

func newBuffersinkFilterContext(fc *FilterContext) *BuffersinkFilterContext {
return &BuffersinkFilterContext{fc: fc}
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersink__accessors.html#gaad918036937648701c09f9612f42706e
func (bfc *BuffersinkFilterContext) ChannelLayout() ChannelLayout {
var cl C.AVChannelLayout
C.av_buffersink_get_ch_layout(bfc.fc.c, &cl)
return newChannelLayoutFromC(&cl)
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersink__accessors.html#gab80976e506ab88d23d94bb6d7a4051bd
func (bfc *BuffersinkFilterContext) ColorRange() ColorRange {
return ColorRange(C.av_buffersink_get_color_range(bfc.fc.c))
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersink__accessors.html#gaad817cdcf5493c385126e8e17c5717f2
func (bfc *BuffersinkFilterContext) ColorSpace() ColorSpace {
return ColorSpace(C.av_buffersink_get_colorspace(bfc.fc.c))
}

func (bfc *BuffersinkFilterContext) FilterContext() *FilterContext {
return bfc.fc
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersink__accessors.html#ga55614fd28de2fa05b04f427390061d5b
func (bfc *BuffersinkFilterContext) FrameRate() Rational {
return newRationalFromC(C.av_buffersink_get_frame_rate(bfc.fc.c))
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersink.html#ga71ae9c529c8da51681e12faa37d1a395
func (bfc *BuffersinkFilterContext) GetFrame(f *Frame, fs BuffersinkFlags) error {
var cf *C.AVFrame
if f != nil {
cf = f.c
}
return newError(C.av_buffersink_get_frame_flags(bfc.fc.c, cf, C.int(fs)))
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersink__accessors.html#ga955ecf3680e71e10429d7500343be25c
func (bfc *BuffersinkFilterContext) Height() int {
return int(C.av_buffersink_get_h(bfc.fc.c))
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersink__accessors.html#ga1eb8bbf583ffb7cc29aaa1944b1e699c
func (bfc *BuffersinkFilterContext) MediaType() MediaType {
return MediaType(C.av_buffersink_get_type(bfc.fc.c))
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersink__accessors.html#ga402ddbef6f7347869725696846ac81eb
func (bfc *BuffersinkFilterContext) PixelFormat() PixelFormat {
return PixelFormat(C.av_buffersink_get_format(bfc.fc.c))
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersink__accessors.html#gaa38ee33e1c7f6f7cb190bd2330e5f848
func (bfc *BuffersinkFilterContext) SampleAspectRatio() Rational {
return newRationalFromC(C.av_buffersink_get_sample_aspect_ratio(bfc.fc.c))
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersink__accessors.html#ga402ddbef6f7347869725696846ac81eb
func (bfc *BuffersinkFilterContext) SampleFormat() SampleFormat {
return SampleFormat(C.av_buffersink_get_format(bfc.fc.c))
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersink__accessors.html#ga2af714e82f48759551acdbc4488ded4a
func (bfc *BuffersinkFilterContext) SampleRate() int {
return int(C.av_buffersink_get_sample_rate(bfc.fc.c))
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersink__accessors.html#gabc82f65ec7f4fa47c5216260639258a1
func (bfc *BuffersinkFilterContext) TimeBase() Rational {
return newRationalFromC(C.av_buffersink_get_time_base(bfc.fc.c))
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersink__accessors.html#gac8c86515d2ef56090395dfd74854c835
func (bfc *BuffersinkFilterContext) Width() int {
return int(C.av_buffersink_get_w(bfc.fc.c))
}
30 changes: 30 additions & 0 deletions buffersrc_filter_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package astiav

//#include <libavfilter/buffersrc.h>
import "C"

type BuffersrcFilterContext struct {
fc *FilterContext
}

func newBuffersrcFilterContext(fc *FilterContext) *BuffersrcFilterContext {
return &BuffersrcFilterContext{fc: fc}
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersrc.html#ga73ed90c3c3407f36e54d65f91faaaed9
func (bfc *BuffersrcFilterContext) AddFrame(f *Frame, fs BuffersrcFlags) error {
var cf *C.AVFrame
if f != nil {
cf = f.c
}
return newError(C.av_buffersrc_add_frame_flags(bfc.fc.c, cf, C.int(fs)))
}

func (bfc *BuffersrcFilterContext) FilterContext() *FilterContext {
return bfc.fc
}

// 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))
}
43 changes: 43 additions & 0 deletions buffersrc_filter_context_parameters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package astiav

//#include <libavfilter/buffersrc.h>
import "C"
import "unsafe"

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html
type BuffersrcFilterContextParameters struct {
c *C.AVBufferSrcParameters
}

func newBuffersrcFilterContextParametersFromC(c *C.AVBufferSrcParameters) *BuffersrcFilterContextParameters {
if c == nil {
return nil
}
return &BuffersrcFilterContextParameters{c: c}
}

// https://ffmpeg.org/doxygen/7.0/group__lavfi__buffersrc.html#gaae82d4f8a69757ce01421dd3167861a5
func AllocBuffersrcFilterContextParameters() *BuffersrcFilterContextParameters {
return newBuffersrcFilterContextParametersFromC(C.av_buffersrc_parameters_alloc())
}

func (bfcp *BuffersrcFilterContextParameters) Free() {
if bfcp.c != nil {
if bfcp.c.hw_frames_ctx != nil {
C.av_buffer_unref(&bfcp.c.hw_frames_ctx)
}
C.av_freep(unsafe.Pointer(&bfcp.c))
}
}

// https://ffmpeg.org/doxygen/7.0/structAVBufferSrcParameters.html#a86c49b4202433037c9e2b0b6ae541534
func (bfcp *BuffersrcFilterContextParameters) SetHardwareFrameContext(hfc *HardwareFrameContext) {
if bfcp.c.hw_frames_ctx != nil {
C.av_buffer_unref(&bfcp.c.hw_frames_ctx)
}
if hfc != nil {
bfcp.c.hw_frames_ctx = C.av_buffer_ref(hfc.c)
} else {
bfcp.c.hw_frames_ctx = nil
}
}
39 changes: 18 additions & 21 deletions codec_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import (
// https://ffmpeg.org/doxygen/7.0/structAVCodecContext.html
type CodecContext struct {
c *C.AVCodecContext
// We need to store this to unref it properly
hdc *HardwareDeviceContext
hfc *HardwareFrameContext
}

func newCodecContextFromC(c *C.AVCodecContext) *CodecContext {
Expand All @@ -37,15 +34,13 @@ func AllocCodecContext(c *Codec) *CodecContext {

// https://ffmpeg.org/doxygen/7.0/group__lavc__core.html#gaf869d0829ed607cec3a4a02a1c7026b3
func (cc *CodecContext) Free() {
if cc.hdc != nil {
C.av_buffer_unref(&cc.hdc.c)
cc.hdc = nil
}
if cc.hfc != nil {
C.av_buffer_unref(&cc.hfc.c)
cc.hfc = nil
}
if cc.c != nil {
if cc.c.hw_device_ctx != nil {
C.av_buffer_unref(&cc.c.hw_device_ctx)
}
if cc.c.hw_frames_ctx != nil {
C.av_buffer_unref(&cc.c.hw_frames_ctx)
}
// Make sure to clone the classer before freeing the object since
// the C free method may reset the pointer
c := newClonedClasser(cc)
Expand Down Expand Up @@ -370,12 +365,13 @@ func (cc *CodecContext) FromCodecParameters(cp *CodecParameters) error {

// https://ffmpeg.org/doxygen/7.0/structAVCodecContext.html#acf8113e490f9e7b57465e65af9c0c75c
func (cc *CodecContext) SetHardwareDeviceContext(hdc *HardwareDeviceContext) {
if cc.hdc != nil {
C.av_buffer_unref(&cc.hdc.c)
if cc.c.hw_device_ctx != nil {
C.av_buffer_unref(&cc.c.hw_device_ctx)
}
cc.hdc = hdc
if cc.hdc != nil {
cc.c.hw_device_ctx = C.av_buffer_ref(cc.hdc.c)
if hdc != nil {
cc.c.hw_device_ctx = C.av_buffer_ref(hdc.c)
} else {
cc.c.hw_device_ctx = nil
}
}

Expand All @@ -386,12 +382,13 @@ func (cc *CodecContext) HardwareFrameContext() *HardwareFrameContext {

// https://ffmpeg.org/doxygen/7.0/structAVCodecContext.html#a3bac44bb0b016ab838780cc19ac277d6
func (cc *CodecContext) SetHardwareFrameContext(hfc *HardwareFrameContext) {
if cc.hfc != nil {
C.av_buffer_unref(&cc.hfc.c)
if cc.c.hw_frames_ctx != nil {
C.av_buffer_unref(&cc.c.hw_frames_ctx)
}
cc.hfc = hfc
if cc.hfc != nil {
cc.c.hw_frames_ctx = C.av_buffer_ref(cc.hfc.c)
if hfc != nil {
cc.c.hw_frames_ctx = C.av_buffer_ref(hfc.c)
} else {
cc.c.hw_frames_ctx = nil
}
}

Expand Down
Loading

0 comments on commit 09052ff

Please sign in to comment.