diff --git a/codec_context.go b/codec_context.go index e819596..714fbf0 100644 --- a/codec_context.go +++ b/codec_context.go @@ -347,8 +347,8 @@ func (cc *CodecContext) SetHardwareDeviceContext(hdc *HardwareDeviceContext) { } func (cc *CodecContext) SetHardwareFrameContext(hfc *HardwareFrameContext) { - if cc.c.hw_frames_ctx != nil { - C.av_buffer_unref(&cc.c.hw_frames_ctx) + if cc.hfc != nil { + C.av_buffer_unref(&cc.hfc.c) } cc.hfc = hfc if cc.hfc != nil { diff --git a/frame.go b/frame.go index 252a027..b7adbdc 100644 --- a/frame.go +++ b/frame.go @@ -33,8 +33,8 @@ func (f *Frame) AllocBuffer(align int) error { return newError(C.av_frame_get_buffer(f.c, C.int(align))) } -func (f *Frame) AllocHWBuffer(hfc *HardwareFrameContext, align int) error { - return newError(C.av_hwframe_get_buffer(hfc.c, f.c, C.int(align))) +func (f *Frame) AllocHardwareBuffer(hfc *HardwareFrameContext) error { + return newError(C.av_hwframe_get_buffer(hfc.c, f.c, 0)) } func (f *Frame) AllocImage(align int) error { diff --git a/hardware_frame_context.go b/hardware_frame_context.go index 39bbe2c..9aeb2ae 100644 --- a/hardware_frame_context.go +++ b/hardware_frame_context.go @@ -3,46 +3,49 @@ package astiav //#include import "C" import ( - "fmt" "unsafe" ) +// https://github.com/FFmpeg/FFmpeg/blob/n7.0/libavutil/hwcontext.h#L115 type HardwareFrameContext struct { c *C.struct_AVBufferRef } -func AllocHardwareFrameContext(hdc *HardwareDeviceContext) (*HardwareFrameContext, error) { - if hfc := C.av_hwframe_ctx_alloc(hdc.c); hfc != nil { - return &HardwareFrameContext{c: hfc}, nil +func newHardwareFrameContextFromC(c *C.struct_AVBufferRef) *HardwareFrameContext { + if c == nil { + return nil } - return nil, fmt.Errorf("failed to allocate hardware frame context") + return &HardwareFrameContext{c: c} +} + +func AllocHardwareFrameContext(hdc *HardwareDeviceContext) *HardwareFrameContext { + return newHardwareFrameContextFromC(C.av_hwframe_ctx_alloc(hdc.c)) +} + +func (hfc *HardwareFrameContext) data() *C.AVHWFramesContext { + return (*C.AVHWFramesContext)(unsafe.Pointer((hfc.c.data))) } func (hfc *HardwareFrameContext) SetWidth(width int) { - frameCtx := (*C.AVHWFramesContext)(unsafe.Pointer((hfc.c.data))) - frameCtx.width = C.int(width) + hfc.data().width = C.int(width) } func (hfc *HardwareFrameContext) SetHeight(height int) { - frameCtx := (*C.AVHWFramesContext)(unsafe.Pointer((hfc.c.data))) - frameCtx.height = C.int(height) + hfc.data().height = C.int(height) } -func (hfc *HardwareFrameContext) SetFormat(format PixelFormat) { - frameCtx := (*C.AVHWFramesContext)(unsafe.Pointer((hfc.c.data))) - frameCtx.format = C.enum_AVPixelFormat(format) +func (hfc *HardwareFrameContext) SetPixelFormat(format PixelFormat) { + hfc.data().format = C.enum_AVPixelFormat(format) } -func (hfc *HardwareFrameContext) SetSWFormat(swFormat PixelFormat) { - frameCtx := (*C.AVHWFramesContext)(unsafe.Pointer((hfc.c.data))) - frameCtx.sw_format = C.enum_AVPixelFormat(swFormat) +func (hfc *HardwareFrameContext) SetSoftwarePixelFormat(swFormat PixelFormat) { + hfc.data().sw_format = C.enum_AVPixelFormat(swFormat) } func (hfc *HardwareFrameContext) SetInitialPoolSize(initialPoolSize int) { - frameCtx := (*C.AVHWFramesContext)(unsafe.Pointer((hfc.c.data))) - frameCtx.initial_pool_size = C.int(initialPoolSize) + hfc.data().initial_pool_size = C.int(initialPoolSize) } -func (hfc *HardwareFrameContext) Init() error { +func (hfc *HardwareFrameContext) Initialize() error { return newError(C.av_hwframe_ctx_init(hfc.c)) }