Skip to content

perf(seek): keyframe-aware forward decoding and seek stats - #68

Merged
blocksmith314 merged 1 commit into
mainfrom
perf/seek-index
Nov 17, 2026
Merged

blocksmith314 merged 1 commit into
mainfrom
perf/seek-index

Conversation

@blocksmith314

Copy link
Copy Markdown
Owner

Summary

Fixes #66: frame selection no longer re-decodes from the previous keyframe on every forward jump.

What changed

  • FrameIndex: container packets are scanned once (when a seek is already likely) for (pts, keyframe) and sorted into presentation order; a keyframe index list is kept.
  • Cost-based choice:
    cost_forward = target - current
    cost_seek    = target - nearest_keyframe + 2
    
    The cheaper path wins. Forward decoding keeps the decoder state (no av_seek_frame, no flush); only ShowFrameAt converts the requested frame.
  • Lazy index build: while the index is missing, forward jumps of at most 30 frames decode directly, so a first small step does not pay for a full demux pass. The index is built when a large forward or backward jump would seek anyway.
  • SeekStats: used_container, frames_decoded, seek_io_ms, decode_ms; the performance log gains seek_frames= and seek_io=.

Verification

  • ctest: 72/72 pass.
  • New tests:
    • ForwardSmallSeekDecodesOnlyTheGap (no container seek, frames_decoded == 4, PTS 0.5)
    • BackwardSeekUsesContainerSeek (container seek, PTS 0.2)
  • tidy.sh on the changed files: 0 issues.

Expected on a long-GOP file: a +12 frame forward click decodes ~12 frames instead of a full GOP; the first large/backward jump pays a one-time index build.

Not included

Build a presentation-order frame index with keyframe flags from container
packets. A seek now compares decoding forward from the current position
with seeking to the nearest keyframe and picks the cheaper path, instead
of seeking on every jump larger than one frame.

The index is built lazily only when a seek is already likely, and forward
decoding keeps the decoder state (no av_seek_frame, no flush); the target
frame is still decoded by ShowFrameAt. Expose SeekStats (used_container,
frames_decoded, seek_io_ms, decode_ms) and add seek_frames/seek_io to the
performance log.

Fixes #66
@blocksmith314
blocksmith314 merged commit 36eaebe into main Nov 17, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Frame selection is slow for long-GOP H.264 (seek re-decodes from the keyframe)

1 participant