Skip to content

perf(seek): raw Annex B IDR byte seeking - #73

Merged
blocksmith314 merged 1 commit into
mainfrom
perf/raw-idr-seek
Nov 19, 2026
Merged

blocksmith314 merged 1 commit into
mainfrom
perf/raw-idr-seek

Conversation

@blocksmith314

Copy link
Copy Markdown
Owner

Summary

Fixes #6: raw Annex B streams no longer decode from frame 0 on every backward jump.

Changes

  • MainWindow builds a raw keyframe index from the parser NAL list: offsets of SPS/PPS and each IDR frame, passed to the player decoder via SetRawKeyframeIndex().
  • FFmpegDecoder::SeekToFrame() uses av_seek_frame(..., AVSEEK_FLAG_BYTE) to jump to the nearest IDR before the target, flushes, sets current_frame_ to that IDR frame and decodes forward.
  • Raw streams now use the same cost comparison as containers: decode forward from the current frame vs seek to the nearest IDR (+2 frame overhead).
  • If the byte seek fails, the previous reset-and-decode-from-zero path is kept as a fallback.
  • SeekStats gained used_raw_keyframe.

Verification

  • Byte seeking on the raw H.264 demuxer was verified independently (av_seek_frame returns 0 and the following packet starts at the requested offset).
  • New DecoderTest.RawSeekJumpsToPreviousIdr: seeks from frame 50 back to 22, checks used_raw_keyframe and compares the image pixel-for-pixel with a sequentially decoded frame 22.
  • ctest: 73/73 pass.
  • tidy.sh on the changed files: 0 issues.

Notes

  • Assumes parameter sets are present before the IDR (typical x264 output) or available as codec extradata; otherwise the fallback still works but is slower.
  • Large-file performance should be checked on a multi-GB raw stream: seek_frames should be close to target - IDR frame.

Fixes #6

Build a byte-offset index for raw streams from the parser's NAL list (SPS/
PPS plus each IDR frame) and pass it to the decoder. SeekToFrame now
compares decoding forward from the current position with a byte seek to
the nearest IDR and picks the cheaper path, so backward/random jumps no
longer decode from frame 0. Falls back to the old reset path when the
byte seek fails.

Adds DecoderTest.RawSeekJumpsToPreviousIdr, which seeks back to frame 22
and compares the image with a sequentially decoded reference.

Fixes #6
@blocksmith314
blocksmith314 merged commit 2b0ea95 into main Nov 19, 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.

Raw Annex B seek is O(n): every jump decodes from frame 0

1 participant