Skip to content

feat(renderer): bounded video look-ahead gate and display-order PTS for reordered streams - #766

Merged
superuser404notfound merged 2 commits into
superuser404notfound:mainfrom
pkajaba:fel2/2-renderer-reorder
Oct 15, 2026
Merged

superuser404notfound merged 2 commits into
superuser404notfound:mainfrom
pkajaba:fel2/2-renderer-reorder

Conversation

@pkajaba

@pkajaba pkajaba commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Part 2/10 of #702, stacked on #765. It targets main and carries #765's commit, so review only the top commit; the diff shrinks to it as the earlier PRs merge.

Summary

Two inert additions to SampleBufferRenderer that the dual-layer path needs for pacing. Nothing calls them yet and the gate is off unless setFELLookahead(enabled:) is called, so pacing is unchanged for every source.

What's in it

  • felVideoLookaheadFrames (9), mayEnqueueVideoFrame and setFELLookahead: a gate that runs before decode and refuses a compressed frame more than nine frame intervals ahead of the playback clock, so composed 4K frames (about 25 MB each) cannot pile up behind Apple's readiness gate alone.
  • displayOrderPTS: estimates a packet's display-order PTS in decode order from the largest amount a later packet fell below the highest PTS seen, so B-frame streams do not swing the cap with the GOP position. A gap beyond HEVC's 16-picture reorder ceiling is a timestamp discontinuity and does not poison the learned lead.
  • The learned lead is capped at maxLearnedReorderFrames = 8. Without a ceiling, one backward timestamp glitch inside the 16-picture window (a 12-frame one, say) raised the lead, and with it the admitted look-ahead, for the whole session. 8 is above the 7 frames of decode-order lag that a hierarchical-B GOP of 8 gives (measured on a UHD Blu-ray FEL remux; other clips measured 2 to 4); a cap of 4, the renderer's post-decode reorder depth, would have cut the look-ahead from 9 frames to about 6 on that remux. The cap is unrelated to hardwareDecoderReorderDepth.
  • The gate state (highest packet PTS and learned lead) is reset by setFELLookahead (enable and disable) as well as by a seek flush, since what was learned belonged to the previous stream or setting.
  • Public API: none.

How it was tested

  • swift test filtered to every test file the stack adds or changes up to this PR (cumulative: git diff --name-only --diff-filter=AM origin/main HEAD -- Tests/AetherEngineTests, one suite per file): 86 tests in 4 suites pass on this commit alone. Real-clip tests skip without DV_FEL_TESTS_DIR.
  • New tests: a glitch inside the reorder window cannot grow the lead past the cap (pure function, and through the gate after a 12-frame backward glitch); a hierarchical-B GOP of 8 learns a lead of exactly 7 frames and a glitch is bounded at 8; disabling and re-enabling the look-ahead, and a seek flush, each forget the learned lead and the highest PTS (both tests fail if either is left standing, checked by removing each reset in turn).
  • swift build --build-tests and aetherctl build at this commit.

What it does not do


FEL stack for #702, merge in order: #765 → #766 → #767 → #768 → #769 → #770 → #771 → #772 → #773 → #774. Standalone read-ahead fixes: #775, #776.

libdovi is built panic=abort, so a single panic ends the host process. A
CRC-valid but hostile Dolby Vision RPU could reach one on the native
Profile 7 -> 8.1 conversion.

Adds DoviRpuGuard: it checks the RPU CRC and pre-scans the payload for the
inputs that make libdovi 3.4 panic or allocate without bound (linear
interpolation pieces, polynomial/MMR mixes that break the writer, pivot and
block counts the payload cannot hold, undefined display-management block
lengths). The existing paths now check each RPU with it before parsing: the Profile 8.1
conversion in DoviRpuConverter (convertPacketToProfile81), the
enhancement-layer-type probe, and the DolbyVisionRecordAudit RPU read. The
mode 2 convert -> write pair on a parsed RPU is factored into
DoviRpuConverter.convertedUnspec62 so the tests drive production's own entry
point. A rejected RPU degrades the frame to the HDR10 base exactly as a
libdovi failure already did (superuser404notfound#135).

This hardens the existing native 8.1 conversion; it changes no routing and
adds no Dolby Vision FEL code. The guard knows the paths listed above; it
does not prove there are no others.

Tests: DoviRpuGuardTests (crafted RPUs, verdicts, conversion round trip) and
a DolbyVisionRecordAudit case. The CRC-valid mutation fuzz and the
real-file guard test arrive with the composer, whose parameter walk they
also exercise.
…or reordered streams

Adds the SampleBufferRenderer pieces the dual-layer Dolby Vision path needs
for pacing, as inert, opt-in additions:

- felVideoLookaheadFrames (9) and mayEnqueueVideoFrame / setFELLookahead:
  a gate that runs BEFORE decode and refuses a compressed frame more than
  nine frame intervals ahead of the playback clock, so composed 4K frames
  (about 25 MB each) cannot pile up behind Apple's readiness gate alone.
- displayOrderPTS: estimates a packet's display-order PTS in decode order
  from the largest amount a later packet fell below the highest PTS seen,
  so B-frame streams do not swing the cap with the position in the GOP. A
  gap larger than HEVC's 16-picture reorder ceiling is treated as a
  timestamp discontinuity and does not poison the learned lead.

Nothing calls the gate yet and it is off unless setFELLookahead(enabled:)
is called, so non-FEL pacing is unchanged. The decoder and the host wiring
come in later PRs; the composer-side pool sizing that follows this cap
(DoviFelComposer.outputPoolAllocationThreshold) is pinned in the composer PR.

Tests: SampleBufferRendererLookaheadTests, SampleBufferRendererReorderDepthTests.
@superuser404notfound

Copy link
Copy Markdown
Owner

Merged as 96fed93, thanks @pkajaba.

Checked: with felFrameRate nil by default, mayEnqueueAnotherFrame reduces to isReadyForMoreMediaData, and the two new resets in flush() sit under reorderLock and only touch the new gate state, so this is behaviour-neutral as described. Full swift test on main with this merged: 4486 tests in 615 suites pass, the five look-ahead tests included.

One thing to keep in mind for the wiring in #769 / #770, not for this PR: the gate compares against the playback clock, so a forward timestamp jump of more than nine frames would hold video back until the clock reaches it. On a P7 VOD remux that should not happen, but the wiring PR should either keep the gate to VOD or let a forward discontinuity reset gateMaxPacketPTS and pass, the way the backward case already does.

Next is #767.

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