Skip to content

#151 subtitle prefetcher: the forward park is edge-triggered on subtitle packets only #230

Closed

Description

@superuser404notfound

Found by @rrgomes while reading the software path for #220.

SubtitleForwardPrefetcher.run evaluates its forward park only after a routed subtitle packet. Every other packet takes the discard branch and continues before the park guard is reached:

guard streamIndices.contains(streamIdx) else {
    var p: UnsafeMutablePointer<AVPacket>? = pkt
    trackedPacketFree(&p)
    continue
}

So a stretch of the file with no subtitle packets is read at full speed regardless of how far past playhead + leadSeconds the demuxer already is. The reader only reconsiders when the next subtitle packet arrives, and it is the PTS of that packet, not the read position, that decides whether to park.

On a dense PGS track this is bounded: the reporter measured packets every 1 to 5 seconds across the region of interest, so the park had constant opportunities. It is not bounded in general. A sparse track, a long dialogue-free stretch, or a forced-subtitle track with a handful of cues per hour all let the side reader run ahead by however many bytes lie between two cues, on a second connection that shares the origin with the pump.

It also couples badly with a park that is disarmed for another reason: with nothing else limiting the loop, "read until the next subtitle packet" becomes "read to EOF".

The park position should follow the read position, not only the subtitle PTS axis. One option is to evaluate it on a discarded packet too when that packet carries a usable PTS, which costs a time-base lookup per packet and needs the per-packet cost measured first. Another is a byte or wall-clock bound on how far the loop may run without reaching a park decision.

Not the cause of #220 on its own; filed so it does not stay implicit in that thread.

Activity

  1. added
    bug
    on Jul 27, 2026
  2. superuser404notfound commented on Jul 27, 2026

    @superuser404notfound
    OwnerAuthor

    Fixed on main in 5dd0b15, not yet in a release.

    One correction to the issue text first, because it changes the fix. The premise that non-subtitle packets reach the loop and take the discard branch does not hold: AVDISCARD_ALL is applied inside av_read_frame, so those packets never arrive and the discard branch is dead code in the steady state. Evaluating the park there would never have run. The defect itself is real and worse than described: between two cues the loop has no control point at all, so a single readPacket call walks whatever lies between them, and the park is not merely late, it is unreachable.

    The fix leaves one non-subtitle stream at AVDISCARD_NONKEY instead. Video is preferred, since that setting genuinely thins it to one packet per IRAP, which is the right granularity for a 60 s window; audio is the fallback and its packets are small; cover art is excluded by disposition. Those packets are freed unharvested and exist only to place the read on the timeline. They are placed by DTS rather than PTS, since a video PTS runs ahead of the bytes when the stream carries B-frames, and it is the bytes the park exists to bound.

    runNativeSubtitleReaders had the same defect. It already evaluates its park on every delivered packet, so it was only ever starved of deliveries for the same reason, and it now takes a pacing stream too. The whole-program reader (readToEOF) wants no park, so it gets none.

    Worth recording for #220: the comment that made this look sound claimed av_read_frame fast-walks the discarded index with no I/O. That holds for mov, which skips the avio_seek and the read entirely at AVDISCARD_ALL. It does not hold for Matroska, where ebml_parse reads each cluster's blocks off the wire and matroska_parse_block only then checks discard. So on MKV every video and audio byte was pulled through the side connection and thrown away, with no park to stop it. That is the shape of the read volume measured there (2.6x media rate at a lead of +333 s), and it was never explained by the subtitle packets alone.

    Regression coverage runs through the real mov demuxer on a fixture with the topology that matters (dense video, cues at 0/1/3/8/10/13 s): with a 4 s lead the read now stops on the 6 s IRAP inside the 3 s to 8 s gap instead of crossing it to reach the next cue.

    Thanks for reading the path closely enough to find this one.

  3. superuser404notfound commented on Jul 27, 2026

    @superuser404notfound
    OwnerAuthor

    Released in 5.23.11.

    Closing this one directly rather than waiting on a retest: it was found by reading the path, not from a symptom, so there is nothing to reproduce on a device. The behaviour is pinned by Issue230PrefetchReadPositionParkTests, which runs the real mov demuxer over a fixture with the shape that matters (dense video, cues at 0/1/3/8/10/13 s) and asserts the read now stops on the 6 s IRAP inside the 3 s to 8 s gap instead of crossing it.

    Reopen if the pacing stream misbehaves on a container this was not measured against.

  4. added a commit that references this issue on Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions