Skip to content

Fix Vorbis pts misalignment on the first frame after a seek - #1

Merged
jpc merged 1 commit into
masterfrom
jpc/vorbis-seek-warmup-fix
Jul 10, 2026
Merged

Fix Vorbis pts misalignment on the first frame after a seek#1
jpc merged 1 commit into
masterfrom
jpc/vorbis-seek-warmup-fix

Conversation

@jpc

@jpcjpc commented Jul 10, 2026

Copy link
Copy Markdown
Owner

Problem

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 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) land early by that amount. A long→short transition (2048→256) is off by (2048-256)/4 = 448 samples = 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, shift frame->pts earlier 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

  • Raw timestamp-seek accuracy over 92 Vorbis files: 64/1472 failing → 0/1472.
  • Codec-zoo regression: Vorbis 0/96 failing across six sample rates; aac/adpcm_ms/etc. unchanged.

Predictor

delta = frame0.samples - packet0.duration is 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

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>
@jpcjpc closed this Jul 10, 2026
@jpcjpc reopened this Jul 10, 2026
@jpc
jpc merged commit 9aca294 into masterJul 10, 2026
6 checks passed
Sign up for freeto 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.

1 participant

@jpc