Fix Vorbis pts misalignment on the first frame after a seek - #1
Merged
Conversation
After a mid-stream seek (avcodec_flush_buffers), the first emitted Vorbis frame is often a block-size *transition* frame: it decodes retlen = (bs_prev + bs_cur)/4 samples, but its granule position only advances bs_cur/2. The leading (nb_samples - granule_advance) = (bs_prev - bs_cur)/4 samples are overlap that belongs *before* the packet's pts, yet ffmpeg stamps the whole frame at that pts. Callers that trust the frame pts (rather than discarding down to an exact target sample, as the ffmpeg CLI does) therefore land early by that amount: a long->short transition (2048->256) is off by (2048-256)/4 = 448 samples = 10.16 ms at 44.1 kHz. Measured on 92 real Ogg/Vorbis podcasts, ~7% of seeks past 5 s hit this, unpredictably by position. Fix: on the first frame after a real seek-flush (not a seek back to stream start), for AV_CODEC_ID_VORBIS, shift frame->pts earlier by the warmup (nb_samples - duration) when positive. Gated to Vorbis and fires once per seek, so other codecs and the stream-start path are untouched. Verified: raw timestamp-seek accuracy over 92 files went from 64/1472 failing to 0/1472; codec-zoo regression shows Vorbis 0/96 failing across six sample rates with no change to aac/adpcm_ms/etc. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
After a mid-stream seek (
avcodec_flush_buffers), the first emitted Vorbis frame is often a block-size transition frame: it decodesretlen = (bs_prev + bs_cur)/4samples, but its granule only advancesbs_cur/2. The leading(nb_samples - granule_advance) = (bs_prev - bs_cur)/4samples are overlap that belongs before the packet's pts — yet ffmpeg stamps the whole frame at that pts.Callers that trust the frame pts (rather than discarding down to an exact target sample, as the ffmpeg CLI does) land early by that amount. A long→short transition (2048→256) is off by
(2048-256)/4 = 448samples = 10.16 ms @ 44.1 kHz. On 92 real Ogg/Vorbis podcasts, ~7% of seeks past 5 s hit this, unpredictably by position.The audio samples are correct — only the pts placement is wrong.
Fix
On the first frame after a real seek-flush (not a seek back to stream start), for
AV_CODEC_ID_VORBIS, shiftframe->ptsearlier by the warmup(nb_samples - duration)when positive. Gated to Vorbis, fires once per seek — other codecs and the stream-start path are untouched.Verification
Predictor
delta = frame0.samples - packet0.durationis a perfect indicator of the bug (448 for all failing seeks, 0 for all passing) — it's exactly the correction applied here.🤖 Generated with Claude Code