Skip to content

Effort free video editing!

Move frames into graphs that are done with them

WyattBlue152eec32837c
diff --git a/src/ffmpeg.nim b/src/ffmpeg.nim
index 248dac96..8934f46d 100644
--- a/src/ffmpeg.nim
+++ b/src/ffmpeg.nim
@@ -892,6 +892,8 @@ proc avfilter_inout_free*(inout: ptr ptr AVFilterInOut) {.importc,
# Buffer source/sink operations
proc av_buffersrc_write_frame*(ctx: ptr AVFilterContext,
frame: ptr AVFrame): cint {.importc, header: "<libavfilter/buffersrc.h>".}
+proc av_buffersrc_add_frame*(ctx: ptr AVFilterContext,
+ frame: ptr AVFrame): cint {.importc, header: "<libavfilter/buffersrc.h>".}
proc av_buffersink_get_frame*(ctx: ptr AVFilterContext,
frame: ptr AVFrame): cint {.importc, header: "<libavfilter/buffersink.h>".}
proc av_buffersink_get_time_base*(ctx: ptr AVFilterContext): AVRational {.
diff --git a/src/graph.nim b/src/graph.nim
index 31ea42f6..d225f244 100644
--- a/src/graph.nim
+++ b/src/graph.nim
@@ -90,6 +90,19 @@ proc command*(graph: Graph, target, cmd, arg: string) =
if ret < 0:
error &"Could not send '{cmd}' to filter '{target}': {ret}"
+proc move*(graph: Graph; frame: ptr AVFrame; idx = 0) =
+ ## Hand `frame`'s picture to the buffer source at node index `idx`, leaving
+ ## `frame` empty for the caller to free. `push` gives the graph a second
+ ## reference, which makes a filter that writes in place copy the picture
+ ## first; this gives it the only one.
+ if not graph.configured:
+ error "Graph must be configured before pushing frames"
+ if frame == nil:
+ error "Frame shouldn't be nil here"
+ let ret = av_buffersrc_add_frame(graph.nodes[idx], frame)
+ if ret < 0:
+ error &"Error moving frame to graph source {idx}: {ret}"
+
proc configure*(graph: Graph) =
if graph.configured:
return
diff --git a/src/render/video.nim b/src/render/video.nim
index acae6b67..5dfa4f57 100644
--- a/src/render/video.nim
+++ b/src/render/video.nim
@@ -1391,8 +1391,8 @@ proc makeNewVideoFrames*(output: var OutputContainer, tl: v3, args: mainArgs,
inc syncPts
bgFrame.pts = syncPts
frame.pts = syncPts
- fxGraph.pushIdx(0, bgFrame)
- fxGraph.pushIdx(1, frame)
+ fxGraph.move(bgFrame, 0)
+ fxGraph.move(frame, 1)
result = fxGraph.pull()
result.pts = 0
av_frame_free(addr bgFrame)
@@ -1436,8 +1436,8 @@ proc makeNewVideoFrames*(output: var OutputContainer, tl: v3, args: mainArgs,
inc syncPts
frame.pts = syncPts
m.pts = syncPts
- fxGraph.pushIdx(0, frame)
- fxGraph.pushIdx(1, m)
+ fxGraph.move(frame, 0)
+ fxGraph.move(m, 1)
result = fxGraph.pull()
result.pts = 0
av_frame_free(addr m)
@@ -1458,8 +1458,8 @@ proc makeNewVideoFrames*(output: var OutputContainer, tl: v3, args: mainArgs,
inc syncPts
bgFrame.pts = syncPts
top.pts = syncPts
- fxGraph.pushIdx(0, bgFrame)
- fxGraph.pushIdx(1, top)
+ fxGraph.move(bgFrame, 0)
+ fxGraph.move(top, 1)
result = fxGraph.pull()
result.pts = 0
av_frame_free(addr bgFrame)
@@ -1539,7 +1539,7 @@ proc makeNewVideoFrames*(output: var OutputContainer, tl: v3, args: mainArgs,
body
template throughFxGraph() =
- fxGraph.push(frame)
+ fxGraph.move(frame)
av_frame_free(addr frame)
frame = fxGraph.pull()
@@ -2167,7 +2167,7 @@ proc makeNewVideoFrames*(output: var OutputContainer, tl: v3, args: mainArgs,
let filt = fxGraph.add("rotate", &"a={rad}:ow=rotw({rad}):oh=roth({rad}):c={bg}")
let bsink = fxGraph.add("buffersink")
fxGraph.linkNodes(bsrc, filt, bsink).configure()
- fxGraph.push(frame)
+ fxGraph.move(frame)
av_frame_free(addr frame)
result[0] = fxGraph.pull()
@@ -2213,8 +2213,10 @@ proc makeNewVideoFrames*(output: var OutputContainer, tl: v3, args: mainArgs,
return dst
proc overlayFrame(base, top: ptr AVFrame; x, y: float32;
- scale: float32): ptr AVFrame =
+ scale: float32; takeBase: bool): ptr AVFrame =
## Composite `top` over `base` at (x, y), preserving the overlay's alpha.
+ ## With `takeBase` the base's picture goes to the graph, which can then
+ ## draw on it where it is, and `base` is left empty.
## (x, y) may be fractional: `overlay` places the integer part (it only does
## whole pixels) and a bilinear shift places the sub-pixel remainder, so a slow
## animated position slides smoothly.
@@ -2261,7 +2263,8 @@ proc makeNewVideoFrames*(output: var OutputContainer, tl: v3, args: mainArgs,
inc syncPts
base.pts = syncPts
upper.pts = syncPts
- fxGraph.pushIdx(0, base)
+ if takeBase: fxGraph.move(base, 0)
+ else: fxGraph.pushIdx(0, base)
fxGraph.pushIdx(1, upper)
result = fxGraph.pull()
result.pts = 0
@@ -2334,10 +2337,12 @@ proc makeNewVideoFrames*(output: var OutputContainer, tl: v3, args: mainArgs,
srcSecs = o.srcSecs)
acc
- proc overlayObj(acc: ptr AVFrame, o: VideoFrame): ptr AVFrame =
+ proc overlayObj(acc: ptr AVFrame, o: VideoFrame, takeAcc = false): ptr AVFrame =
## `o` composited over `acc` as a new frame, or nil when `o` has no frame
## for this position (its source ended, or hasn't started), so the base
- ## shows through rather than an opaque bg-filled fallback.
+ ## shows through rather than an opaque bg-filled fallback. `takeAcc` is
+ ## for a caller done with `acc` once there is a new frame, as
+ ## `overlayFrame` has it.
var (top, topDidDecode) = decodeClipFrame(o)
if not topDidDecode or top == nil or top.width <= 0 or top.height <= 0:
if top != nil: av_frame_free(addr top)
@@ -2368,7 +2373,7 @@ proc makeNewVideoFrames*(output: var OutputContainer, tl: v3, args: mainArgs,
oscale = 1
ox = cx - top.width.float32 / 2
oy = cy - top.height.float32 / 2
- result = overlayFrame(acc, top, ox, oy, oscale)
+ result = overlayFrame(acc, top, ox, oy, oscale, takeAcc)
av_frame_free(addr top)
func placed(o: VideoFrame): bool =
@@ -2523,7 +2528,7 @@ proc makeNewVideoFrames*(output: var OutputContainer, tl: v3, args: mainArgs,
else:
if acc == nil:
acc = makeSolid(tl.res[0], tl.res[1], tl.bg, solidTag)
- let newAcc = overlayObj(acc, o)
+ let newAcc = overlayObj(acc, o, takeAcc = true)
if newAcc != nil:
av_frame_free(addr acc)
acc = newAcc