Skip to content

Effort free video editing!

Give AVChannelLayout and AudioResampler move-only hooks

WyattBlue4c427505b015
diff --git a/src/ffmpeg.nim b/src/ffmpeg.nim
index 99c9fa71..5a2c0da6 100644
--- a/src/ffmpeg.nim
+++ b/src/ffmpeg.nim
@@ -439,6 +439,19 @@ proc av_channel_layout_compare*(chl, chl1: ptr AVChannelLayout): cint {.importc,
proc av_channel_layout_uninit(channel_layout: ptr AVChannelLayout) {.importc,
header: "<libavutil/channel_layout.h>".}
+# A custom-order layout owns its channel map, so a layout is moved, never
+# copied: copy one with av_channel_layout_copy, which can fail.
+proc `=destroy`*(layout: AVChannelLayout) =
+ # uninit frees the map and zeroes what it's given, here the dead layout.
+ av_channel_layout_uninit(addr layout)
+
+proc `=wasMoved`*(layout: var AVChannelLayout) =
+ # All zero is AV_CHANNEL_ORDER_UNSPEC with no map, which uninit leaves be.
+ zeroMem(addr layout, sizeof(AVChannelLayout))
+
+proc `=copy`*(dest: var AVChannelLayout, src: AVChannelLayout) {.error.}
+proc `=dup`*(src: AVChannelLayout): AVChannelLayout {.error.}
+
func `$`*(layout: ptr AVChannelLayout): string =
const bufSize: csize_t = 256
var buffer = newString(bufSize)
@@ -455,18 +468,6 @@ func `$`*(layout: ptr AVChannelLayout): string =
template `$`*(layout: AVChannelLayout): string = $(addr layout)
func `$`*(layout: ref AVChannelLayout): string = $(addr layout[])
-proc `=copy`*(dest: var AVChannelLayout, src: AVChannelLayout) {.error:
- "Direct copy of AVChannelLayout is forbidden. Use av_channel_layout_copy() instead.".}
-
-proc `=destroy`*(layout: var AVChannelLayout) =
- av_channel_layout_uninit(addr layout)
-
-proc `=sink`*(dest: var AVChannelLayout, src: AVChannelLayout) =
- # Take ownership of src's bits: Nim won't destroy a moved-from src, so a
- # deep copy here would leak a custom-order layout's heap map (and its
- # ENOMEM result would go unchecked). uninit frees dest's old map first.
- av_channel_layout_uninit(addr dest)
- copyMem(addr dest, addr src, sizeof(AVChannelLayout))
type
# https://ffmpeg.org/doxygen/8.0/structAVFrame.html
diff --git a/src/resampler.nim b/src/resampler.nim
index 77aaa8d2..9b9ec1a8 100644
--- a/src/resampler.nim
+++ b/src/resampler.nim
@@ -15,6 +15,20 @@ type AudioResampler* = object
rate: cint
isPassthrough: bool
+proc `=destroy`*(resampler: AudioResampler) =
+ # A custom destroy hook skips field destruction, so release fields here.
+ if resampler.graph != nil:
+ var graph = resampler.graph
+ avfilter_graph_free(addr graph)
+ `=destroy`(resampler.layout)
+ `=destroy`(resampler.templateLayout)
+
+proc `=wasMoved`*(resampler: var AudioResampler) =
+ # A nil graph and zeroed layouts own nothing.
+ zeroMem(addr resampler, sizeof(AudioResampler))
+
+proc `=copy`*(dest: var AudioResampler, src: AudioResampler) {.error.}
+proc `=dup`*(src: AudioResampler): AudioResampler {.error.}
func getFormatName(format: cint): string =
let name = av_get_sample_fmt_name(format)
@@ -31,15 +45,6 @@ proc newAudioResampler*(format: AVSampleFormat, layout: ref AVChannelLayout, rat
result.rate = rate
result.isPassthrough = false
-proc `=destroy`*(resampler: var AudioResampler) =
- # A custom destroy hook skips field destruction, so release fields here.
- if resampler.graph != nil:
- avfilter_graph_free(addr resampler.graph)
- `=destroy`(resampler.layout)
- `=destroy`(resampler.templateLayout)
-
-proc `=copy`*(dest: var AudioResampler, src: AudioResampler) {.error.}
-
proc collect(resampler: var AudioResampler, into: var seq[ptr AVFrame]) =
while true:
var outFrame = av_frame_alloc()