Skip to content

Commit

Permalink
Added hardware device/frame context Free() method
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Nov 22, 2024
1 parent 3f2badc commit 4f133cd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/hardware_decoding/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions examples/hardware_encoding/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions hardware_device_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
6 changes: 6 additions & 0 deletions hardware_frame_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
Expand Down

0 comments on commit 4f133cd

Please sign in to comment.