Skip to content

fix(subtitles): preserve source axis for rebuilt VOD runs - #533

Merged
superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
orut34iop:codex/fix-rebuilt-run-pgs-source-axis
Sep 13, 2026
Merged

superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
orut34iop:codex/fix-rebuilt-run-pgs-source-axis

Conversation

@orut34iop

Copy link
Copy Markdown
Contributor

Summary

Preserve the source-to-item timestamp normalization carried by rebuilt loopback-HLS VOD runs. Without it, raw-source-PTS subtitle formats such as PGS can be queried hundreds of seconds away from the visible playhead after a cached landing or recut.

What changed

  • Pass both the opening presentation shift and the persistent normalization shift from the segment producer to the engine.
  • Track the opening and continuation source axes per rebuilt run, including backward epoch rewrites and zero-origin media.
  • Use that source axis when reading cached landings and measuring rebuilt placements, while leaving existing-run placement behavior unchanged.
  • Add standard Swift Testing coverage and an Unreleased changelog entry.

Test plan

  • Device / OS: Apple TV 4K (3rd generation), tvOS 26.6.
  • Source media: MKV VOD, H.264 hardware path, embedded PGS subtitle track, two audio tracks, SDR; the media source timestamps begin about 600.2 seconds above the item timeline.
  • Before: after a rebuilt/cached landing, playback at item 267–329 seconds queried subtitle coverage at item 252–313 seconds even though the corresponding PGS packets were on the source axis around 852–929 seconds, so both delivery and the overlay were empty.
  • Result: on the same media, the selected PGS track remained visible continuously from 2:49 through 5:29 and after forward/backward seeks. The host application's physical-device acceptance passed.
  • Focused: DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer swift test --filter RebuiltRunSourceAxisTests — 3 tests passed.
  • Full: DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer swift test — 2947 tests in 398 suites passed.

Checklist

  • CHANGELOG.md updated
  • Commit messages follow Conventional Commits
  • The fix lives in the engine, not in a host-side workaround
  • No public API changes

superuser404notfound pushed a commit that referenced this pull request Sep 13, 2026
… keeps its own (PR #533)

PR #533 is right and measured, and it arrived as a SECOND dictionary next to
`epochShiftByIndex`: same key, same call site, same lock, same pruning rule, and
nothing anywhere to make the two agree. Folded into one `EpochAxisTable` whose
entry states both quantities, with the pruning rule written once and the two
questions named for what they answer: `opening(at:)` is AE#448's "does an epoch
begin here", `sourceAxis(at:)` is #533's "what do the bytes holding this index map
item time back onto the source with".

The measurement the pair comes from, same bytes, one remuxed with
`-output_ts_offset 600`, resuming at 53 s on both:

  zero origin  actual=43000  desired=52000 pinnedTo=43000 shift=0      presentedShift=-9000
  600 s origin actual=643000 desired=52000 pinnedTo=43000 shift=600000 presentedShift=591000

One gate, one backoff of 9 s, two numbers 600 s apart. That is also why the AE#412
re-cut line printed "opened 591.000s below its boundary" for a gate that opened 9 s
below it: the shift carries the source origin, the backoff is the difference, and
the difference is what that line meant.

Second follow-up: the placement now carries its own normalization instead of the
reading looking it up again. A backward restart between a composition and its
reading drops that epoch's entry (that is the pruning rule working), and the late
lookup would then answer with an older run's number. The composition already knows
what it composed; nothing about it should be re-derived from a table that has moved
on.

Verified against both arms after the rewrite, `--start-position 53 --seek-count 5
--seek-pattern 65,60,70,58,75` over `Scripts/slowrange.py` at 600 kbps / 300 ms:

  600 s origin  landing publishes 600.000s, VERDICT OK, every capErr tick within
                one frame of -600.000
  zero origin   landing publishes 0.000s, capErr mean +0.000 over the 25 ticks
                after it, identical to main before PR #533

Both test runners green (2951 swift-testing, 631 XCTest, 0 failures).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDKAJrLjLXVsCvJVNdJEPw
@superuser404notfound
superuser404notfound merged commit 5487b02 into superuser404notfound:main Sep 13, 2026
5 checks passed
@superuser404notfound

Copy link
Copy Markdown
Owner

Merged as 5487b025, thank you. The diagnosis holds and it is worth more than the title claims.

Reproduced here on macOS before touching the branch, on Scripts/timecode-fixture.sh's
tc-cues-lie.mkv remuxed with -output_ts_offset 600, which gives the reporting geometry exactly:
item axis 0 to 120 s over a source that runs 600 to 720 s. Served over Scripts/slowrange.py at
600 kbps / 300 ms, the AE#481 arm (--start-position 53 --seek-every 2 --seek-count 5 --seek-pattern 65,60,70,58,75), with --picture-probe as the witness:

main   #481 ... opens at 72.000s, which is seg18's own playlist position, so it carries 0.000s
       capErr +0.017, cur=-515.10, VERDICT: clock did not advance
#533   #481 ... opens at 72.000s, which is seg18's own playlist position, so it carries 600.000s
       capErr -599.983 (one frame at 24 fps), cur=84.90, VERDICT: OK

So on that class of source it is not only the PGS query that misses: the published position goes
negative and the session reports a clock 600 s below the picture for the rest of the run. The
zero-origin control on the same arm is byte-identical between the two, which is the part that
matters for the nine rounds of AE#418 and AE#481 measurement that ran on fixtures starting at zero.
That is also what hid this: on those fixtures "this run has no placement offset left" and "item time
is source time here" are the same statement, and only one of them is true in general.

The gate-open pair states the whole thing in two lines, same bytes, same resume at 53 s:

zero origin   actual=43000  desired=52000 pinnedTo=43000 shift=0      presentedShift=-9000
600 s origin  actual=643000 desired=52000 pinnedTo=43000 shift=600000 presentedShift=591000

One gate, one backoff of 9 s, two numbers 600 s apart.

Two follow-ups on top, in b3399179, neither of them a correction to the reasoning here:

  • The record was a second dictionary beside epochShiftByIndex, with the same key, call site, lock
    and pruning rule, and nothing to keep the two in agreement. Both quantities now live in one
    EpochAxisTable entry, with the pruning rule written once and the two lookups named for what they
    answer.
  • The placement carries its own normalization instead of the reading looking it up again. A backward
    restart between a composition and its reading drops that epoch's entry, which is the pruning rule
    working as intended, and the late lookup would then have answered with an older run's number.

The AE#412 re-cut log line was reading from the same conflation and is fixed with it: it printed
"opened 591.000s below its boundary" for a gate that opened 9 s below it, because the shift it
quoted also carried the source origin.

superuser404notfound pushed a commit that referenced this pull request Sep 13, 2026
…533 round 2, AE#534)

The axis this session publishes is two quantities added together: how far AVPlayer's
own timeline sits from the playlist, and the source-to-item normalization the bytes
carry. Every AE#418 rule is about the first one, and rounds 2 to 9 were measured on
fixtures whose timestamps start at zero, where the two are one number. So the
composition added the whole shift, which on any other source adds the source origin
again per placement.

Measured on `tc-cues-lie.mkv` remuxed with `-output_ts_offset 600` over
`Scripts/slowrange.py` at 600 kbps / 300 ms, resuming at 73 s (seg18 re-aims 0.375 s)
and seeking once:

  before  #418 seg17 placed ... axis shift 599.625s -> 1196.458s from item -531.625s
  after   #418 seg13 placed ... axis shift 599.625s -> 590.625s from item 81.000s
          (the reading then lands on 591.000s, which is what the picture reads)

1196.458 is the source origin counted twice, and the seam it wrote is 531 s below
the start of a 120 s item. It self-corrects wherever the placement can be read back;
AE#418 round 7 is the case where it cannot, and there it stands for the session.

The composition now works in displacement and the normalization is added once, by the
epoch that wrote the bytes. Consequences, all of them measured:

- A placement's worth is its epoch's `openingSourceAxis` on either kind of reading,
  so PR #533's own-run / rebuilt branch collapses: the two disagree about the BASE,
  which is measured, not about what the bytes are worth. An AE#412 re-cut is worth
  nothing to the displacement and its normalization to the axis, which is one
  statement in the new terms and was two in the old ones.
- `placementBase`, `placementOffset` (was `axisShift`) and `placementDisplacement`
  are the same arithmetic with honest labels: they always described the displacement.
  Every pinned number in their tests is unchanged, which is the point.
- `publishPlaylistShift` now takes the displacement the axis is made of, so a call
  site cannot publish one without saying what the other half was.

`snapAxisAfterSeek` is deliberately NOT moved onto the displacement, and it keeps
asking about the axis, which means it stays inert on a source whose timestamps do not
start at zero. Measured on both twins: on the one arm that separates the two (a seek
into content AVPlayer already holds) inert is the RIGHT answer, because AVPlayer keeps
the displacement there and the rule discards it anyway, costing the zero-origin twin
0.400 s of cue accuracy for the rest of the session. That is AE#534, with both arms.

Zero-origin behaviour is unchanged everywhere, byte for byte across three runs per arm:
the #481 arm publishes -9.000 then 0.000 with a capErr mean of +0.000, and the two-seek
arm publishes -0.375, 0.000, -9.000 with capErr +0.075, 3 of 3.

Both test runners green (2953 swift-testing, 631 XCTest, 0 failures).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDKAJrLjLXVsCvJVNdJEPw
@superuser404notfound

Copy link
Copy Markdown
Owner

Released in 6.85.0 (21ab499), and both consumers are on it.

One more thing came out of the same lens after the merge, so it is in that release too. The composition had the same conflation the landing rule had: it added the whole shift per placement rather than what the gate backed off, so on the 600 s twin a placement worth 596.833 s composed onto a standing 599.625 s and published 1196.458 s, with the seam at item -531.625 s. It self-corrects wherever the placement can be read back out of AVPlayer's buffer, which is why the arm here recovered within a tick; AE#418 round 7 is the case where it cannot, and there it would stand for the session. The composition now works in AVPlayer's own displacement and the normalization is added once, by the epoch that wrote the bytes:

before   #418 seg17 placed ... axis shift 599.625s -> 1196.458s from item -531.625s
after    #418 seg13 placed ... axis shift 599.625s -> 590.625s from item 81.000s
         (the reading then lands on 591.000s, which is what the picture reads)

That also collapsed the own-run / rebuilt branch this PR introduced: once the composition is in displacement terms, a placement's bytes are worth the same on either timeline, and what the two disagree about is the base, which is measured.

The offset-origin fixture recipe is worth having if more of this comes up: -output_ts_offset 600 on tc-cues-lie.mkv, offset applied BEFORE the Cues are injected (a -c copy remux into MKV regenerates them). Note capErr inverts on such a fixture, so -600.000 is the healthy reading there and 0.000 is the defect.

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