From 4f133cd508a2e021dd65123adaf196358fcbc65d Mon Sep 17 00:00:00 2001 From: Quentin Renard Date: Fri, 22 Nov 2024 13:52:42 +0100 Subject: [PATCH] Added hardware device/frame context Free() method --- examples/hardware_decoding/main.go | 1 + examples/hardware_encoding/main.go | 2 ++ hardware_device_context.go | 6 ++++++ hardware_frame_context.go | 6 ++++++ 4 files changed, 15 insertions(+) diff --git a/examples/hardware_decoding/main.go b/examples/hardware_decoding/main.go index fcaa76d..a1847f5 100644 --- a/examples/hardware_decoding/main.go +++ b/examples/hardware_decoding/main.go @@ -136,6 +136,7 @@ func main() { if s.hardwareDeviceContext, err = astiav.CreateHardwareDeviceContext(hardwareDeviceType, *hardwareDeviceName, nil, 0); err != nil { log.Fatal(fmt.Errorf("main: creating hardware device context failed: %w", err)) } + defer s.hardwareDeviceContext.Free() // Update decoder context s.decCodecContext.SetHardwareDeviceContext(s.hardwareDeviceContext) diff --git a/examples/hardware_encoding/main.go b/examples/hardware_encoding/main.go index 2f12291..7cd6ba9 100644 --- a/examples/hardware_encoding/main.go +++ b/examples/hardware_encoding/main.go @@ -53,6 +53,7 @@ func main() { if err != nil { log.Fatal(fmt.Errorf("main: creating hardware device context failed: %w", err)) } + defer hardwareDeviceContext.Free() // Find encoder codec encCodec := astiav.FindEncoderByName(*encoderCodecName) @@ -85,6 +86,7 @@ func main() { if hardwareFrameContext == nil { log.Fatal("main: hardware frame context is nil") } + defer hardwareFrameContext.Free() // Get software pixel format softwarePixelFormat := astiav.FindPixelFormatByName(*softwarePixelFormatName) diff --git a/hardware_device_context.go b/hardware_device_context.go index d246378..b8bcb01 100644 --- a/hardware_device_context.go +++ b/hardware_device_context.go @@ -29,3 +29,9 @@ func CreateHardwareDeviceContext(t HardwareDeviceType, device string, options *D } return &hdc, nil } + +func (hdc *HardwareDeviceContext) Free() { + if hdc.c != nil { + C.av_buffer_unref(&hdc.c) + } +} diff --git a/hardware_frame_context.go b/hardware_frame_context.go index 8ea4941..7bfdc7c 100644 --- a/hardware_frame_context.go +++ b/hardware_frame_context.go @@ -23,6 +23,12 @@ func AllocHardwareFrameContext(hdc *HardwareDeviceContext) *HardwareFrameContext return newHardwareFrameContextFromC(C.av_hwframe_ctx_alloc(hdc.c)) } +func (hfc *HardwareFrameContext) Free() { + if hfc.c != nil { + C.av_buffer_unref(&hfc.c) + } +} + func (hfc *HardwareFrameContext) data() *C.AVHWFramesContext { return (*C.AVHWFramesContext)(unsafe.Pointer((hfc.c.data))) }