Skip to content

feat(decoder): seek by resetting the decoder instead of rebuilding it - #61

Merged
hgaiser merged 2 commits into
hgaiser:mainfrom
trmosala:feat/decoder-reset
Nov 18, 2026
Merged

hgaiser merged 2 commits into
hgaiser:mainfrom
trmosala:feat/decoder-reset

Conversation

@trmosala

Copy link
Copy Markdown
Contributor

Seeking in a file currently means dropping the Decoder and building a new one, which recreates the video session, its images and the completion thread on every jump. I'm using the decoder in a video editor, where scrubbing and frame stepping make that the common case.

This adds two calls on DecodeSink, forwarded by Decoder:

  • reset() declares a discontinuity. It drops buffered input and the pictures held for reordering, clears reference state, and keeps the session, its images and the parameter sets. Feeding resumes at a keyframe.
  • flush() delivers the pictures still held for reordering and keeps the source open, so a decoder that reached the end of a file can still seek afterwards. It is a separate commit and easy to drop.

Seek to first frame on an RTX 4080 SUPER, median of 30 seeks per method (H.264 without B-frames, same VideoContext for both):

reset() drop + Decoder::new
1080p 3.5 ms 10.5 ms
4K 13.3 ms 22.0 ms

Does this fit?

The library is built around live streams, so I'd rather ask than assume: is file seeking something you want in pixelforge? If it is, I'm happy to rework this to your taste and keep contributing along these lines. If not, no problem, I'll carry it in a fork.

Things worth knowing

  • reset() waits for the work already submitted, so when it returns every frame from before the seek is on the source. Callers tell old frames from new ones by the pts they supplied. There are no new public types.
  • The first decode after a reset records the same RESET control operation as the first decode of a new session. For that, dpb_slot_active is split into "written" (which governs the image layout) and needs_reset.
  • Byte-stream framing stamped the first complete picture of a call with the previous carry's pts when nothing was carried over (0 on a new decoder). That is fixed here because telling frames apart after a reset depends on it.
  • Both calls return InvalidInput after finish().

Testing

  • Reset followed by replay is compared byte for byte with a fresh decoder: H.264 with B-frames, H.265 closed GOP, CRA start (RASL skipped again), Main10, and AV1 with alt-refs.
  • 600 resets with pictures still in flight, on the zero-copy and the copying path, checking frame counts and that the copy pool stays bounded.
  • flush() is compared with what finish() delivers, then the decoder is reused.
  • Unit tests for the H.264, H.265 and AV1 reference state after a reset.
  • cargo test -- --ignored passes (79 tests) on NVIDIA 620.70.06, the only device I have. The dmabuf tests were not run.
  • With the validation layer's synchronization checks on, the new tests report WRITE_AFTER_WRITE hazards between the decode and the copy queue's layout transition. main reports the same on its existing decode tests, so I left that alone. There are no core or thread-safety findings.

🤖 Created with GPT-6.2 Sol in Codex and reviewed with Claude Fable 5.2 in Claude Code.

@hgaiser

hgaiser commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Thanks!

As for if it fits: primarily this crate was created for low latency encoding in moonshine, but I made it a standalone crate so that it could also be used for other purposes. Nice to hear it's being used in other projects 👍

Decoding itself is not something I currently use, but I would like pixelforge to support it properly. Getting feedback from people using it in those scenarios is helpful, so once more: thanks!

@hgaiser
hgaiser merged commit 3c49a65 into hgaiser:main Nov 18, 2026
7 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.

2 participants