Skip to content

Image subtitles turned on mid-play show the read-ahead window's lines instead of a ~10 s gap - #1393

Merged
GLinnik21 merged 5 commits into
mainfrom
fix/image-subs-on-enable
Nov 14, 2026
Merged

GLinnik21 merged 5 commits into
mainfrom
fix/image-subs-on-enable

Conversation

@GLinnik21

Copy link
Copy Markdown
Collaborator

Problem

Turn on an image subtitle track (PGS, VobSub, DVB) during playback while subtitles are Off, and nothing appears for about as long as the player reads ahead. On a 4K direct play that was ~10 s. Text tracks (SRT and others) show up straight away.

Cause: the main demuxer (play-feed/src/ff.rs) reads ahead of the playhead. Text-track cues are kept for every track. Image-track packets were decoded only while some subtitle was on, and thrown away otherwise. Switching from one image track to another was fine; only Off → image had the gap.

Fix

While subtitles are Off, SubTracks (play-feed/src/ff_subs.rs) keeps an owned copy of each image-track packet in an ImageBacklog instead of dropping it. On the first packet read after a subtitle is selected, the backlog is decoded through the existing image decoders, in demux order across tracks, before the live packet. It is then emitted under the current generation, exactly like live cues.

  • Retention, no cap. For each track, packets are kept from the last one at or before the anchor onward. The anchor is the new CaptionRetention::anchor_ns(): the earlier of the playhead and the seek target, now also used by subtitle_floor_ns. Memory is bounded by the read-ahead window. For PGS (hdmv_pgs_subtitle), the cut moves back to the newest display set's PCS (0x16). In M2TS/TS each segment is its own packet: the bundled FFmpeg 9.0.2 has no PGS parser and mpegts emits one packet per PES.
  • Seek and close discard the backlog, so a held line can never land after a seek.
  • One code path. The per-packet step (release-if-selected, the decode gate, decode-or-hold, seek discard) lives in SubTracks methods. demux() and the tests both call them. The gate also requires an empty backlog, so a selection that lands mid-packet cannot put a live packet ahead of the held ones.
  • No new FFmpeg symbol or struct offset. Held bytes (plus 64 zeroed padding bytes) are fed through an av_packet_alloc scratch packet with buf == NULL. INPUT_PADDING is pinned against AV_INPUT_BUFFER_PADDING_SIZE in ci/ffabi-assert.c. If the allocation fails, the backlog stays held for the next packet and one log line is written.
  • The live subtitle path now reads (*fmt).streams instead of the stream table captured at open, which goes stale when a transport stream adds streams.
  • Unchanged: the HTTP side reader. It starts fresh for each selection at the selected track, so it has no gap.

Verification

Regression tests, red before the fix, green after:

  • backlog_tests in ff_subs.rs (retention, PGS segment framing, demux order).
  • End-to-end tests through the bundled FFmpeg in media/src/ff_image_subtitle_tests.rs, run with the host build and --ignored: flip after the read, flip mid-read, a selection racing the decode, and a seek discard.

Gates on the committed tree:

  • make test-crate C=plx_play_feed: 322 passed
  • make test-crate C=plx_media: 2134 passed
  • make test-crate C=plx_playback_contract: 42 passed
  • make check-nodef: clean
  • ci/check-deps.sh: green
  • ci/ffabi-assert.c compiled for the host only, plus a negative control.
  • Full make check and the ARM build are left to CI.

On the dev TV (webOS 4.5): debug build, guest on tests/mock_pms.py, a 3-minute 1080p H.264 clip with one PGS track (a cue every 10 s, held 8 s), subtitles Off at start.

  • The track was turned on at ~5 s, with ~10 s already read ahead.
  • image cue [0ms] and image cue [10000ms] were both emitted at the flip, then 20, 30, 40 and 50 s on schedule.
  • Before the fix the first cue would have been the 20 s one. The old build was not run on the TV for comparison.

Not covered by a host test: the three call sites in demux(). The only PGS test file has no video, so demux() cannot open it. The TV run above covers them.

Reviews: pr-reviewer (one round; all findings addressed), fw-compat-reviewer (source-only: no new imports or DT_NEEDED; the AVPacket use is valid for the bundled FFmpeg), doc-claim-auditor (two stale comments fixed).

Known limits

  • An image track turned on while paused draws once the demux loop moves again.
  • A pick made inside the last read-ahead window before EOF does not drain.
  • With a positive subtitle delay, lines from that far behind the playhead are not held.
  • A PGS "normal case" set that depends on an earlier epoch start can still decode as nothing until the next epoch. This matches the existing post-seek behaviour.

Observed during the TV run and not caused by this change: the track menu open over 1080p video ran at ~40 fps, and 60 fps came back when it closed, with subtitles still drawing. The cause was not determined.

🤖 Generated with Claude Code

GLinnik21 and others added 4 commits November 14, 2026 01:56
…them when one is turned on

The main demuxer reads about ten seconds ahead of the playhead. While no subtitle was selected,
SubTracks::decode dropped every image-track packet, so an image track (PGS) turned on mid-play
showed nothing until the read-ahead had been used up, while a text track answered at once
because text cues are observed whatever the selection.

SubTracks now keeps an owned copy of each image packet while the gate is closed (ImageBacklog:
per track, from the last packet at or before the playhead onward, with no cap of its own; the
playhead is CaptionRetention::anchor_ns, the earlier of playhead and seek target, and an unknown
playhead retires nothing). The demux loop checks on every packet it reads whether a subtitle is
on and, if so, decodes the backlog in file order per track through the same decoder a live packet
takes, ahead of the packet just read, emitting under the current generation. A seek and a close
discard it. The decode feeds a scratch packet pointing at the owned bytes: no new FFmpeg symbol
or struct offset.

CaptionRetention::anchor_ns is the one definition of the measuring point; subtitle_floor_ns now
uses it instead of its own copy of the seek-target rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…g selection, drop test-only members, test a mid-read flip

A selection that lands between the demux loop's release check and the subtitle branch's own read
of it no longer decodes the current image packet ahead of the held ones: the packet is held and
released with the next read. The backlog's test helpers move into its test module, and the
end-to-end test gains the case that matters on the television, a track turned on between the 0 s
display set and its 8 s clear.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ests both call, PGS sets kept whole from their PCS, live stream table

The demux loop's subtitle step lived in ff.rs and the end-to-end tests drove a hand-written copy
of it, so reverting every ff.rs change left them green. The release-if-selected check, the decode
gate, decode-or-hold and the seek discard are now SubTracks methods (release_if_selected,
on_packet, discard_held) that demux() and the tests both call; hold and release_held are private,
so a test cannot reach around the gate. A new test lands the selection between a packet's release
check and its decode, the race the gate closes.

A PGS display set is one packet in Matroska but one packet per segment (PCS, WDS, PDS, ODS, END)
from libavformat's MPEG-TS demuxer, so keeping only "from the last packet at or before the
playhead" could drop the PCS and ODS of the set on screen and leave a lone END. For PGS tracks the
backlog now retires only up to the PCS of the newest set at or before the playhead; the other image
codecs keep the packet rule.

The held packets and the live packet are read through (*fmt).streams with its bound, not the table
captured at open, which a transport stream reallocates when it grows streams (AVFMTCTX_NOHEADER).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e backlog on an allocation failure, pin the padding

ImageBacklog::take returned the held packets one whole track after another, so the bitmap store
(push_subtitle_bitmap_locked) got a track-grouped batch where it documents and relies on demux
(increasing-pts) order for its budget eviction. Every held packet now takes a read sequence number at
hold time and take merges the tracks by it: exactly the order the demuxer read them, with no
dependence on a timestamp a packet may lack.

release_held took the backlog before checking the scratch packet allocation, so a failed
av_packet_alloc silently dropped every held packet. It allocates first now; on failure it logs one
line and leaves the backlog held for the next packet's release check.

INPUT_PADDING is pinned against the bundled headers in ci/ffabi-assert.c instead of claiming to match
every FFmpeg the app links, and request_subtitle's comment names the mechanism that replaced the
desired_sub_idx gate it used to cite.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
present_zlib_fixture indexed the stream table with the packet's stream_index unchecked; it now
resolves it through the demux loop's bounded live_stream (exported via ff_test_support) and skips
a packet the table does not describe.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@GLinnik21
GLinnik21 merged commit 5570be8 into main Nov 14, 2026
19 checks passed
@GLinnik21
GLinnik21 deleted the fix/image-subs-on-enable branch November 14, 2026 14:11
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