Skip to content

Anchor the subtitle prefetch seek on the subtitle axis (#234) - #236

Merged
superuser404notfound merged 1 commit into
mainfrom
fix/issue-234-side-reader-seek-anchor
Jul 28, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
fix/issue-234-side-reader-seek-anchor

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

What broke

5.23.11 regressed the #143 / #204 shape: a seek landing whose only later display set sits beyond the lead window is never shown. Reported by @cmcpherson274 with a bisection across four releases on identical fixture bytes (5.23.10 good, 5.23.11 dark, deterministic 3/3).

Root cause

Positioning, not the park.

The prefetcher seeks with avformat_seek_file(ctx, -1, ...). A -1 stream index leaves the reference stream to av_find_default_stream_index, whose score awards +200 to any stream with discard != AVDISCARD_ALL (libavformat/avformat.c:369).

On Matroska matroska_read_seek jumps to the cluster byte position the reference stream's index entry names, so everything in earlier clusters is simply not read: a landing cue that starts further back than that cluster is behind the read head before the first packet arrives. It is never harvested, so neither the #143 candidate seed nor the #204 finalize ever sees it. That matches the reporter's harvested dropping 3 to 1 with no applySubtitleEvent at the destination.

The reader never asked for the subtitle axis, it inherited it from a discard flag.

Fix

seekBounded(to:anchorStreamIndex:timeout:) takes the anchor explicitly and converts the target into that stream's own time base. The prefetcher passes its lowest subtitle stream index, which is exactly what the score used to elect. The #230 park is untouched.

Why the #230 tests did not catch it

They run on MP4. mov_read_seek re-seeks every stream individually and backwards, so a sparse subtitle track keeps its landing sample whatever the reference is. The container difference is now a test of its own.

Verification

  • New Issue234SideReaderSeekAnchorTests, 8 cases on a purpose-built 3.4 KB Matroska fixture (keyframe every second, two cues: landing at 3 s running to 11 s, successor at 12 s, clusters every 5 s, seek to 7.5 s). Covers the libavformat mechanism, the regression at demuxer level, the fix, the mov contrast, the time-base conversion and the anchor edge cases.
  • pacingStreamSkipsTheLandingCue reproduces the reported symptom directly: unpaced the first delivered cue is the 3 s landing, paced it is the 12 s successor.
  • prefetchLoopHarvestsTheLandingCue runs the real prefetch loop: without the anchor the landing never reaches the store, with it both cues do.
  • swift test: 1161 tests / 185 suites green.
  • swift build -Xswiftc -strict-concurrency=complete clean, tvOS Simulator xcodebuild BUILD SUCCEEDED.

Reporter retest still open, so no auto-close keyword.

The forward prefetcher positions with avformat_seek_file(ctx, -1, ...), which
leaves the reference stream to av_find_default_stream_index. That score awards
+200 to any stream with discard != AVDISCARD_ALL, so until 5.23.10 the subtitle
stream won it simply by being the only stream not fully discarded, and the seek
was measured on the subtitle axis by accident rather than by intent.

#230's pacing stream sits at AVDISCARD_NONKEY, which is not AVDISCARD_ALL, so
from 5.23.11 it collected the same +200 and video outranked the subtitle stream
275 to 200. On Matroska the seek then jumps to the cluster holding the last
video keyframe, and a landing cue that starts further back than that cluster is
behind the read head before the first packet is delivered: never harvested,
never decoded, so neither the #143 candidate seed nor the #204 finalize can
recover it. MP4 absorbs the same anchor change because mov_read_seek re-seeks
every stream individually and backwards, which is why the #230 tests missed it.

Pass the anchor explicitly instead, converting the target into that stream's own
time base. The #230 park is unchanged.

Reported by cmcpherson274, bisected across four releases on identical fixture
bytes.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01At1agC2qU2Y2MX2BKLdT4N
@superuser404notfound
superuser404notfound merged commit f1c09cc into main Jul 28, 2026
3 checks passed
@superuser404notfound
superuser404notfound deleted the fix/issue-234-side-reader-seek-anchor branch July 28, 2026 09:29
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.

1 participant