Skip to content

Effort free video editing!

Take the timeline to borrow as a pointer

WyattBlue3d2b1ab1231d
diff --git a/src/render/audio.nim b/src/render/audio.nim
index a54ec4b1..06cd10c9 100644
--- a/src/render/audio.nim
+++ b/src/render/audio.nim
@@ -941,18 +941,18 @@ proc loudnormOutToChunk(outF: ptr AVFrame, channels: int): seq[int16] =
if not outF.toInt16(0, outF.nb_samples.int, result, 0, channels):
error &"Unexpected output format from loudnorm: {outF.format}"
-proc makeAudioFrames*(fmt: AVSampleFormat, timeline: v3, frameSize: int, layerIndices: seq[
+proc makeAudioFrames*(fmt: AVSampleFormat, timeline: ptr v3, frameSize: int, layerIndices: seq[
int], norm: Norm,
cache: MediaCache = nil, fromSample = 0, stop: ptr Atomic[bool] = nil):
iterator(): (ptr AVFrame, int64) =
## Samples from `fromSample` on (normalizing measures the whole timeline,
## so it needs 0). A set `stop` ends the iterator at its next call, freeing
## what it holds.
- # What this returns outlives the call, and capturing the timeline would
- # copy it whole. It is borrowed instead: the caller keeps it alive and
- # unmoved until done with the result.
- let tlPtr = addr timeline
- template tl: v3 = tlPtr[]
+ # What this returns outlives the call, and capturing a timeline would copy
+ # it whole. So it is borrowed, by a pointer the caller takes: the address of
+ # a `v3` parameter can be that of a temporary made for the call. The caller
+ # keeps the timeline alive and unmoved until done with the result.
+ template tl: v3 = timeline[]
# Paths are interned, so a path's pointer names its getter without a copy.
var samples: Table[(ptr string, int16), Getter]
diff --git a/src/render/format.nim b/src/render/format.nim
index 57b89306..20b10e9d 100644
--- a/src/render/format.nim
+++ b/src/render/format.nim
@@ -88,14 +88,14 @@ proc checkAudioCtx(ctx: ptr AVCodecContext, rate: cint) =
error &"samplerate '{rate}' not allowed for {ctx.codec.name}."
-proc makePartialLosslessVideo(output: var OutputContainer, tl: v3, args: mainArgs,
+proc makePartialLosslessVideo(output: var OutputContainer, tl: ptr v3, args: mainArgs,
video: VideoSettings):
tuple[stream: ptr AVStream, packets: iterator(): (ptr AVPacket, int64)] =
let encoder = initCodec(video.codec)
if encoder == nil:
return
- let (spans, keyframes) = output.partialLosslessPlan(tl, args, video, encoder.id)
+ let (spans, keyframes) = output.partialLosslessPlan(tl[], args, video, encoder.id)
if spans.len == 0:
return
@@ -208,11 +208,11 @@ proc makeMedia*(args: mainArgs, inputVideo: VideoSettings, audio: AudioSettings,
if renderVideo:
(vOutStream, videoPacketIter) =
- output.makePartialLosslessVideo(renderTl, args, video)
+ output.makePartialLosslessVideo(addr renderTl, args, video)
partialLosslessVideo = vOutStream != nil
if not partialLosslessVideo:
(vEncCtx, vOutStream, videoFrameIter) =
- makeNewVideoFrames(output, renderTl, args, video, cache)
+ makeNewVideoFrames(output, addr renderTl, args, video, cache)
var audioStreams: seq[ptr AVStream] = @[]
var audioEncoders: seq[ptr AVCodecContext] = @[]
@@ -248,7 +248,7 @@ proc makeMedia*(args: mainArgs, inputVideo: VideoSettings, audio: AudioSettings,
audioEncoders.add(aEncCtx)
let frameSize = if aEncCtx.frame_size > 0: aEncCtx.frame_size else: 1024
- let audioFrameIter = makeAudioFrames(encoder.supportedSampleFmts[0], renderTl,
+ let audioFrameIter = makeAudioFrames(encoder.supportedSampleFmts[0], addr renderTl,
frameSize, toSeq(0 ..< tl.a.len), audio.normalize, cache)
audioFrameIters.add(audioFrameIter)
elif includeAudio:
@@ -281,7 +281,7 @@ proc makeMedia*(args: mainArgs, inputVideo: VideoSettings, audio: AudioSettings,
audioEncoders.add(aEncCtx)
let frameSize = if aEncCtx.frame_size > 0: aEncCtx.frame_size else: 1024
- let audioFrameIter = makeAudioFrames(encoder.supportedSampleFmts[0], renderTl,
+ let audioFrameIter = makeAudioFrames(encoder.supportedSampleFmts[0], addr renderTl,
frameSize, @[i], audio.normalize, cache)
audioFrameIters.add(audioFrameIter)
diff --git a/src/render/partialplan.nim b/src/render/partialplan.nim
index 07609bfd..09330429 100644
--- a/src/render/partialplan.nim
+++ b/src/render/partialplan.nim
@@ -310,15 +310,15 @@ func holdsFrames(tb, frameTb: AVRational): bool =
## don't for 30 fps: a frame is 33 or 34 of them.
(frameTb.num.int64 * tb.den.int64) mod (frameTb.den.int64 * tb.num.int64) == 0
-proc makePartialLossless*(output: var OutputContainer, timeline: v3,
+proc makePartialLossless*(output: var OutputContainer, timeline: ptr v3,
video: VideoSettings,
spans: seq[SmartSpan], keyframes: seq[int64], codecId: AVCodecID):
(ptr AVStream, iterator(): (ptr AVPacket, int64)) =
- # What this returns outlives the call, and capturing the timeline would
- # copy it whole. It is borrowed instead: the caller keeps it alive and
- # unmoved until done with the result.
- let tlPtr = addr timeline
- template tl: v3 = tlPtr[]
+ # What this returns outlives the call, and capturing a timeline would copy
+ # it whole. So it is borrowed, by a pointer the caller takes: the address of
+ # a `v3` parameter can be that of a temporary made for the call. The caller
+ # keeps the timeline alive and unmoved until done with the result.
+ template tl: v3 = timeline[]
let label = codecId.codecLabel
let isNal = codecId.isNalCodec
let sourcePath = tl.v[0][0].src[]
diff --git a/src/render/preview.nim b/src/render/preview.nim
index 1d4a30b2..ac6c364d 100644
--- a/src/render/preview.nim
+++ b/src/render/preview.nim
@@ -138,14 +138,14 @@ proc openPreview*(args: sink mainArgs, startSeconds, scale: float64,
if vids.len > 0: r.quarterTurns = quarterTurns(vids[0].codecpar)
r.output = openWrite("preview.mkv")
var stream: ptr AVStream
- (r.encoder, stream, r.video) = makeNewVideoFrames(r.output, r.renderTl,
+ (r.encoder, stream, r.video) = makeNewVideoFrames(r.output, addr r.renderTl,
r.args, video, r.cache, start, addr r.stop,
if session != nil: session.sources else: nil)
r.sar = r.encoder.sample_aspect_ratio
r.hasVideo = true
if withAudio and r.renderTl.a.anyIt(it.len > 0):
let fromSample = audioSampleSpan(start, 0, r.renderTl.sr, tb).start
- r.audio = makeAudioFrames(AV_SAMPLE_FMT_FLT, r.renderTl, 1024,
+ r.audio = makeAudioFrames(AV_SAMPLE_FMT_FLT, addr r.renderTl, 1024,
toSeq(0 ..< r.renderTl.a.len), Norm(kind: nkNull), r.cache,
fromSample, addr r.stop)
r.audioAt = fromSample
diff --git a/src/render/video.nim b/src/render/video.nim
index c6148f5e..8a74a6a7 100644
--- a/src/render/video.nim
+++ b/src/render/video.nim
@@ -1009,7 +1009,8 @@ func scaledVideoResolution*(resolution: (int32, int32),
max(int32(round(resolution[1].float64 * scale)) and not 1'i32, 2),
)
-proc makeNewVideoFrames*(output: var OutputContainer, timeline: v3, args: mainArgs,
+proc makeNewVideoFrames*(output: var OutputContainer, timeline: ptr v3,
+ args: mainArgs,
video: VideoSettings, myCache: MediaCache, startIndex = 0'i64,
stop: ptr Atomic[bool] = nil, keep: VideoSources = nil):
(ptr AVCodecContext, ptr AVStream, iterator(): (ptr AVFrame, int64)) =
@@ -1017,11 +1018,11 @@ proc makeNewVideoFrames*(output: var OutputContainer, timeline: v3, args: mainAr
## call, freeing what it holds (a preview abandons a render this way).
## With `keep`, sources' decoders come from it and stay in it at the end,
## for the next render to pick up where this one left off.
- # What this returns outlives the call, and capturing the timeline would
- # copy it whole. It is borrowed instead: the caller keeps it alive and
- # unmoved until done with the result.
- let tlPtr = addr timeline
- template tl: v3 = tlPtr[]
+ # What this returns outlives the call, and capturing a timeline would copy
+ # it whole. So it is borrowed, by a pointer the caller takes: the address of
+ # a `v3` parameter can be that of a temporary made for the call. The caller
+ # keeps the timeline alive and unmoved until done with the result.
+ template tl: v3 = timeline[]
# One state object per source (decoders, seek bookkeeping, still/held frame
# caches, loop accounting). Still-image sources (overlay logos/watermarks)
diff --git a/src/timeline.nim b/src/timeline.nim
index a5503018..cb8002eb 100644
--- a/src/timeline.nim
+++ b/src/timeline.nim
@@ -60,7 +60,7 @@ type
kind*: TransitionKind
alignment*: TransitionAlignment
-type v3* {.byref.} = object
+type v3* = object
layout*: ref AVChannelLayout
res*: (int32, int32)
tb*: AVRational