Effort free video editing!
Skip non-reference frames while decoding up to a far target
WyattBluec0e76e6b729c
diff --git a/src/av.nim b/src/av.nimindex f046ef8f..784f7c7f 100644--- a/src/av.nim+++ b/src/av.nim@@ -522,10 +522,14 @@ iterator decode*(container: InputContainer, index: cint, codecCtx: ptr AVCodecCoyield frameiterator flushDecode*(container: InputContainer, index: cint,- codecCtx: ptr AVCodecContext, frame: ptr AVFrame): ptr AVFrame =+ codecCtx: ptr AVCodecContext, frame: ptr AVFrame,+ exactFrom: int64 = AV_NOPTS_VALUE): ptr AVFrame =## Drain-first decode loop. Always pulls all available frames out of the decoder## before sending more packets, so we never hit AVERROR(EAGAIN) on send, which would## silently drop the packet.+ ## Frames timed before `exactFrom` that no other frame references are never+ ## decoded, so they aren't yielded. `codecCtx.skip_frame` is left as the last+ ## packet set it.var ret: cintlet packet = container.packetvar flushed = false@@ -547,6 +551,12 @@ iterator flushDecode*(container: InputContainer, index: cint,flushed = trueelse:if packet.stream_index == index:+ # Go by the packet's time, not the last frame out: decode order+ # runs ahead of display order by the reorder delay.+ codecCtx.skip_frame =+ if packet.pts != AV_NOPTS_VALUE and packet.pts < exactFrom:+ AVDISCARD_NONREF+ else: AVDISCARD_DEFAULTdiscard avcodec_send_packet(codecCtx, packet)av_packet_unref(packet)diff --git a/src/ffmpeg.nim b/src/ffmpeg.nimindex 5a2c0da6..62d5e06c 100644--- a/src/ffmpeg.nim+++ b/src/ffmpeg.nim@@ -322,6 +322,7 @@ constAVMEDIA_TYPE_SUBTITLE* = AVMediaType(3)AVMEDIA_TYPE_ATTACHMENT* = AVMediaType(4)AVDISCARD_DEFAULT* = AVDiscard(0)+ AVDISCARD_NONREF* = AVDiscard(8) # Discard frames no other frame referencesAVDISCARD_ALL* = AVDiscard(48) # Discard all packets for the streamAVFMT_GLOBALHEADER* = 0x0040AV_CODEC_FLAG_GLOBAL_HEADER* = 4194304 # 1 << 22diff --git a/src/render/video.nim b/src/render/video.nimindex aea04bb9..5907f449 100644--- a/src/render/video.nim+++ b/src/render/video.nim@@ -226,6 +226,7 @@ typelastReqIndex: int # last obj.index requested (held-frame reuse)loopBase: int # source frames consumed by completed loopsusedAt: int64 # timeline frame it last decoded for; -1 = never+ exactFrom: int64 # stream pts; before it only reference frames decodedeintW, deintH, deintFmt: cint # what deint was built forownsCn: bool # opened for this decoder, not the media cache'sdeintDone: bool # deint has been sent end-of-stream@@ -306,7 +307,8 @@ proc nextFrame(st: DecState, stream: ptr AVStream, frame: ptr AVFrame): bool =return falseelse:var got = false- for _ in st.cn.flushDecode(stream.index.cint, st.decoder, frame):+ for _ in st.cn.flushDecode(stream.index.cint, st.decoder, frame,+ st.exactFrom):got = truebreakif not got and st.deint != nil:@@ -349,7 +351,8 @@ proc nextFrame(st: DecState, stream: ptr AVStream, frame: ptr AVFrame): bool =proc newDecState(cn: InputContainer, decoder: ptr AVCodecContext,ownsCn: bool): DecState =DecState(cn: cn, ownsCn: ownsCn, decoder: decoder, frameIndex: -1,- seekThreshold: 10, lastSeekTarget: -1, lastReqIndex: -1, usedAt: -1)+ seekThreshold: 10, lastSeekTarget: -1, lastReqIndex: -1, usedAt: -1,+ exactFrom: AV_NOPTS_VALUE)proc buildKeyframeIndex(stream: ptr AVStream, fps: float,defaultInterval: int): KeyframeIndex =@@ -1950,6 +1953,18 @@ proc makeNewVideoFrames*(output: var OutputContainer, tl: v3, args: mainArgs,let holdPastEnd = obj.win >= 0 and not obj.incomingvar lastGood: ptr AVFrame = nilvar fromHeld = false+ # Catching up to a far target decodes frames only to drop them. Those no+ # other frame references needn't be decoded at all, until close enough to+ # the target that it and the frame before it, which bwdif needs, come+ # whole. The lead also covers frames a frame-threaded decoder already has.+ var exactAll = ss.frameRate.num <= 0 or ss.frameRate.den <= 0+ template aimSkip() =+ let lead = if exactAll: 0 else: max(2, int(ceil(0.3 * ss.frameRate.float)))+ st.exactFrom =+ if exactAll or target <= lead: AV_NOPTS_VALUE+ else: videoFrameToTimestamp(target - lead, ss.frameRate,+ myStream.time_base) + st.cn.startPts(myStream)+ aimSkip()while frameIndex < target:if target - frameIndex > ss.kfInterval and frameIndex > seekThreshold:if st.lastSeekTarget != target:@@ -2007,9 +2022,28 @@ proc makeNewVideoFrames*(output: var OutputContainer, tl: v3, args: mainArgs,debug &"Seek landed at frame {frameIndex}, avoided decoding {framesAvoided} frames"framesSaved += framesAvoidedhasSeekFrame = false+ elif not exactAll and st.decoder.skip_frame != AVDISCARD_DEFAULT:+ # The stream ended while still skipping, so its last frames may be+ # among the skipped. Which frame is last decides where a loop wraps,+ # what is held past the end, and whether a later request is behind+ # the decoder, so decode the tail again in full.+ exactAll = true+ aimSkip()+ st.decoder.skip_frame = AVDISCARD_DEFAULT+ let want = max(frameIndex, 0)+ let idx = upperBound(ss.observedKeyframes, want) - 1+ let seekTarget = if idx >= 0: ss.observedKeyframes[idx] else: 0+ st.cn.seek(+ videoFrameToTimestamp(seekTarget, ss.frameRate, myStream.time_base) ++ st.cn.startPts(myStream), stream = myStream)+ avcodec_flush_buffers(st.decoder)+ st.resetDeint()+ seekWant = want+ seekMargin = 0elif looping and frameIndex >= 0:loopBase += frameIndex + 1target = obj.index - loopBase+ aimSkip()st.cn.seek(0, stream = myStream)avcodec_flush_buffers(st.decoder)st.resetDeint()