Repository navigation
feat(dovi): compose Profile 7 FEL on AVPlayer's own frames behind a load option - #967
Conversation
…oad option LoadOptions.dualLayerRoute (default .softwareHost, unchanged) takes .avPlayerOutput with the DualLayerDolbyVision trait and preferDualLayerDolbyVision on. AVPlayer then plays the base layer and the audio from the engine's loopback stream and owns the clock; the engine takes its decoded frames from an AVPlayerItemVideoOutput, pairs each with its enhancement layer, composes them with the composer the software host uses and presents the result in a display layer on the item's timebase. The enhancement layer comes from an in-process tap on the segment producer's single read, with a bounded on-disk ring (1 GiB, unlinked at once) that keeps the packets for seeks back. The forward-buffer window is capped at 10 segments on this route so the producer's lead stays inside the ring. DualLayerVideoDecoder gets an external-base-layer mode; pairing, RPU, display metadata and kernels are the existing code. The route composes into the output pool, not into AVPlayer's buffers, unless AETHER_FEL_OUTPUT_INPLACE=1 asks for it. A position with no enhancement layer shows AVPlayer's own base-layer frame, and every seek of any length resets the feed from the clock. When an AirPlay video receiver or an external display holds the picture, the route stops and AVPlayer shows the Profile 8.1 base layer. Picture in Picture is not offered on this route. A composition runs on the thread that hands the decoder the frame: the pull thread, or VideoToolbox's callback thread when the enhancement layer arrives last. stop() waits at most 2 s for a pull or feed tick. Closing the decoder itself still waits for the enhancement-layer session, as on the software host, so a GPU that never completes can still hold it. The two routes share the compose maths and the pairing but are not bit-identical: the dither parity follows the frame's position in the stream here, and the software host's count of compositions there. Opt-in; every other session loads as before. Built on macOS, tvOS and iOS; run on the Mac, on a tvOS Simulator and once on an Apple TV. CHANGELOG, docs/api.md, docs/cli.md and docs/dolby-vision-fel.md updated.
|
Thanks for this, Pavel, and for the unusually honest test write-up (the open 13.5% and the I read it against main and ran it locally merged onto current
Minor, no action needed: the four new suites carry Otherwise I'll wait for your Apple TV list (drops, seeks, memory on the TV decoder, AirPlay, flash behaviour of the 1 GiB ring). Mark it ready when you are happy with it and I'll merge. |
CHANGELOG.md: both sides' Added entries kept, the route's last.
There was a problem hiding this comment.
Automated review
Status: 🟠 Needs changes. Real issues to fix before merge.
This adds an opt-in second Profile 7 FEL route (LoadOptions.dualLayerRoute = .avPlayerOutput). AVPlayer plays the 8.1 base layer and owns the clock, and the engine composes the enhancement layer onto AVPlayer's frames, using the existing pairing and composer. The direction is right. The fix lives in the engine, it reuses DualLayerVideoDecoder instead of copying it, it stays behind the DualLayerDolbyVision trait, and the default route is unchanged. The PR is now marked ready, but neither of the two points the maintainer asked to see before that is addressed at this head.
Findings
- Should fix:
outputRouteFrameRateis still not reset with the other per-source state inloadSessionand in the stop path (maintainer's point 1). (Sources/AetherEngine/AetherEngine.swift:2530) - Should fix:
DualLayerOutputRoute.stop()still blocks the main actor. It waits without a bound indecoder.close(), then up to 2 s inwaitForTicksToEnd(), on every teardown and on the AirPlay hand-back. There is no move off main and no TV log of how long it takes (maintainer's point 2, #735). (Sources/AetherEngine/Native/DualLayerOutputRoute.swift:282) - Nit: a
loadNativethat fails or is superseded afterprepareDualLayerOutputRouteleaves the prepared route indualLayerOutputRouteuntil the next teardown. (Sources/AetherEngine/AetherEngine+Loading.swift:953) - Nit: the new doc section quotes 10 segments, 0.15 s, 1 GiB and 2 s, and
DocumentedConstantsTestsdoes not pin any of them. (docs/dolby-vision-fel.md:380)
Checklist: CHANGELOG yes, docs yes, tests yes, traits both ways yes (every new site is behind #if DualLayerDolbyVision or reached only through it)
Needs a device run: On the TV, these are still open:
- how long
stop()takes there - the 13.5% display-layer drops seen once, and whether they relate to the master playlist or the
-11868reload - seeks on a device
- memory of the TV decoder service
- the 1 GiB ring written with
pwriteon the segment producer's pump thread, against TV flash and a purgeable temporary directory - an AirPlay hand-back run
- recovery after a return from background
- iOS, which has never been run
Thanks for the careful measurements and for writing down plainly what is still unexplained.
Automated review by Quint (Claude) of 8f281dd. It reads the code, it does not build or run it; a maintainer reviews and merges.
| /// Set by the load's route decision when a Profile 7 FEL source takes `DualLayerRoute.avPlayerOutput`: | ||
| /// the stream's frame rate (known and positive; the decision refuses the route otherwise). `loadNative` | ||
| /// starts the route for the session it builds. | ||
| var outputRouteFrameRate: Double? |
There was a problem hiding this comment.
Still open from the maintainer's review: outputRouteFrameRate is set only by the route decision (line 5670). Neither loadSession (next to sourceElType = nil, line 4691) nor the stop path (line 7245) clears it. prepareDualLayerOutputRoute and forwardBufferSegmentsForSession both read it, so a loadNative reached without the decision (a future rebuild path, or one that skips the probe) would build the route for an unrelated source. That route would suppress AVPlayer's picture and cap the forward window. Fix: set outputRouteFrameRate = nil next to sourceElType = nil in both places.
| // wait that ends when the GPU completes one and that closing the decoder does not cut short. Only | ||
| // the wait for the ticks below is bounded; closing the decoder itself still waits for the | ||
| // enhancement-layer session, as on the software host, so a GPU that never completes can still hold it. | ||
| decoder.close() |
There was a problem hiding this comment.
Still open from the maintainer's review. stop() is @MainActor. decoder.close() waits for the enhancement-layer session without a bound, and waitForTicksToEnd() then parks main for up to stopWaitSeconds (2 s) on a pull tick waiting for a GPU slot. This runs on every stopInternal teardown, on every prepareDualLayerOutputRoute, and in handleExternalPlaybackChange during the AirPlay hand-back. The engine had a main-thread hang on teardown before (#735). Suggested fix: keep the timer cancel, item.remove(output), the overlay detach and the controlTimebase reset on main, and do decoder.close(), the tick drain and the final flush on a background queue. The stopped flag and the decoder's epoch already stop a late tick from handing anything on. If you keep it on main, a device log of how long stop() takes on the TV is the minimum the maintainer asked for.
| } | ||
| // #260: an observer installed before load has to reach this session's producers too. | ||
| session.setNativeVideoFrameTimeObserver(nativeVideoFrameTimeObserver) | ||
| prepareDualLayerOutputRoute(session: session) |
There was a problem hiding this comment.
Nit: the route is prepared here, but self.nativeVideoSession = session happens only at line 1342. Between them are the session.start() / prepare() / serve() awaits and the supersede check, and each of those paths stops only the session. A failed or superseded load leaves the route in dualLayerOutputRoute, holding its 1 GiB ring fd and its decoder. In that state nativePlayerLayer returns nil until the next teardown or prepare clears it. Stopping the route (stopDualLayerOutputRoute()) on those failure exits would close the gap.
|
|
||
| | Part | What it does | Code it reuses | | ||
| |---|---|---| | ||
| | Base layer and audio | `AVPlayer` plays the engine's loopback HLS stream: the base layer with the RPU converted to Profile 8.1 and the enhancement layer dropped, as on the native route, plus the audio. The item owns the clock. The forward-buffer window of the session is capped at 10 segments (the default) on this route, see the ring below. | the native route, unchanged | |
There was a problem hiding this comment.
Nit: this section quotes several constants: the forward-buffer cap of 10 segments (DualLayerRouteDecision.maxForwardBufferSegments), the 0.15 s pull lead (DualLayerOutputRoute.pullAheadSeconds), the 1 GiB ring (VideoPacketHistory.defaultCapacityBytes) and the 2 s stop bound (stopWaitSeconds). The house rule is that numbers quoted in docs are pinned by DocumentedConstantsTests, and none of these is. Since the route is trait-gated, the pin needs the same #if DualLayerDolbyVision guard.
There was a problem hiding this comment.
Automated review
Status: 🟠 Needs changes. Real issues to fix before merge.
I checked new head 8e2407a and nothing my last review covered has changed. It looks like a rebase onto current main. The two should-fix findings are still open: outputRouteFrameRate is not reset in loadSession and the stop path, and DualLayerOutputRoute.stop() still blocks main. The two nits are open too, and the inline comments from the last review still apply.
Automated review by Quint (Claude) of 8e2407a. It reads the code, it does not build or run it; a maintainer reviews and merges.
DualLayerVideoDecoder.swift: the in-place test keeps composesIntoBase and the measure skip keeps the stall check. Both sides declared streamFrameRate; main's stays, and the route's frame index still only reads it on the external base layer.
There was a problem hiding this comment.
Automated review
Status: 🟠 Needs changes. Real issues to fix before merge.
I checked new head 27f0cd2 and nothing my last review covered has changed. Two should-fix findings are still open. outputRouteFrameRate is not reset in loadSession or in the stop path, and DualLayerOutputRoute.stop() still blocks main. The two nits (the prepared route left in place after a failed or superseded load, and the doc constants not pinned) are also open. The earlier inline comments still apply.
Automated review by Quint (Claude) of 27f0cd2. It reads the code, it does not build or run it; a maintainer reviews and merges.
AetherEngine.swift: the video gravity setter keeps the route's layer update and main's PiP frame refresh. AetherPlayerView.swift: the hosted layer takes main's PiP frame and the overlay stays at the view's bounds; detach clears both the overlay and the PiP layout.
There was a problem hiding this comment.
Automated review
Status: 🟠 Needs changes. Real issues to fix before merge.
I checked new head 80de16b. Nothing my last review covered has changed; it looks like another rebase onto main. Two should-fix findings are still open: outputRouteFrameRate is not reset in loadSession or in the stop path, and DualLayerOutputRoute.stop() still blocks main. The two nits are also still open: a prepared route is left in place after a failed or superseded load, and the doc constants are not pinned. The earlier inline comments still apply.
Automated review by Quint (Claude) of 80de16b. It reads the code, it does not build or run it; a maintainer reviews and merges.
…ate, stop it on failed loads Follow-up to #967, the open review points: - DualLayerOutputRoute.stop() detaches on main and returns; closing the decoder and the bounded wait for the feed and pull ticks run on a teardown queue, and the display-layer flush hops back to main after it. A pull tick waiting for a GPU slot can no longer park main for up to 2 s on every teardown or AirPlay hand-back (#735 was a teardown hang). - outputRouteFrameRate is cleared with sourceElType in loadSession and stop(), so a load path that skips the route decision cannot build the route for an unrelated source. - A load that fails or is superseded between preparing the route and handing the session over stops that route, and only that one, so its 1 GiB ring and decoder do not linger. - DocumentedConstantsTests pins the route's documented numbers (10 segments, 0.15 s, 1 GiB, 2 s). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SeT6NgupsgTSme7g6RFnE5
566ba87
into
superuser404notfound:main
|
Merged into main as I pushed one follow-up on top,
Local run of the merge plus the follow-up: all traits (XCTest 833, Swift Testing 6136), the one-lane strict pool and trait off (828 / 5661) are all green, and so are the standalone scripts. The trickle-watchdog test failed once under load in the all-traits run and passed three times on its own. Still open on the Apple TV, from your list: the 13.5% drops on the master run, |
|
One more follow-up on main, Measured on an M2 Pro: 4K 23.976 fps P7 FEL, release
Most of what remains is AVPlayer's own. On the Mac it decodes into the lossless-compressed A correction to my earlier comment: the route's summary line is logged in If you do another TV round, the two things that matter most now are picture identity with in place on against |
|
Released in 7.56.0 ( |
Base
origin/mainc991c8b, tip 8f281dd.What this does
LoadOptions.dualLayerRoute(default.softwareHost, unchanged) takes.avPlayerOutputwith theDualLayerDolbyVisiontrait andpreferDualLayerDolbyVisionon. AVPlayer then plays the base layer and the audiofrom the engine's loopback stream and owns the clock; the engine takes its decoded frames from an
AVPlayerItemVideoOutput, pairs each with its enhancement layer, composes them with the composer the softwarehost uses and presents the result in a display layer on the item's timebase. It is opt-in; every other session loads
as before.
connection, no full-file scan); a bounded on-disk ring (1 GiB, unlinked at once) keeps the packets for seeks back.
The session's forward-buffer window is capped at 10 segments on this route, so the producer's lead stays inside the ring.
DualLayerVideoDecodergets an external-base-layer mode; pairing, RPU, display metadata and kernels are the existingcode. The compose target is the output pool. Composing in place on the player's buffers is an environment opt-in
(
AETHER_FEL_OUTPUT_INPLACE=1), not shown safe off the Mac.length and either direction, resets the feed from the clock.
base layer (logged); Picture in Picture is not offered on this route (
nativePlayerLayeris nil), never a blank window.on a device.
LoadOptions.dualLayerRoute,DualLayerRoute. Nothing private. Documented indocs/api.md,docs/dolby-vision-fel.md,docs/cli.md, CHANGELOG.Threads and stop: there is no compose queue. A composition runs on the thread that hands the decoder the frame: the route's pull thread, or VideoToolbox's callback thread when the enhancement layer arrives last. The in-flight limit (3) and the pool reserve (3) are upstream's, and there is no stall report. A pull tick can wait for a free GPU slot, a wait that ends when the GPU completes one.
stop()waits at most 2 s for a pull or feed tick. Closing the decoder itself still waits for the enhancement-layer session, as on the software host, so a GPU that never completes can still hold it.Dither differs from the software host (by design)
The compose kernels dither on the parity of a frame index. The software host counts compositions; this route uses the
frame's position in the stream, because with the player delivering frames a count would depend on arrival timing. On a
91 s 4K P7 FEL clip: with the software host given the same index rule, 0 of 2181 compared frames differ (every plane and
every RPU attachment). Against the software host as committed, 1265 of 2181 frames differ, and in 23 sampled raw frames the
differing samples (75.8 million of 286 million) all differ by exactly one 10-bit code; the picture and the RPU differ in nothing else (the route's frames also carry a few extra player attachments). The two routes
share the maths and the pairing; they are not bit-identical.
Tested on Mac
At 8f281dd, with measurement hooks kept out of the commit, Mac only. The plain route run, frame comparisons and tests are on this tree; the seek, ring and
--surfaceruns and the memory figures are from before the last merge oforigin/mainand the decoder lock change:origin/mainc991c8b: delivered software-host frames on a 91 s 4K P7 FEL clip, 0 of 2182 differ (all planes, colour tags, RPU digest), and 0 samples differ in 23 raw frames.aetherctl play --fel --fel-output, whole clip: fed 2182, pulled and composed 2181, presented 2180 at the last report, 0 base-layer-only, 0 resyncs, 0 dropped, 0 corrupted. The run compared above did not present frame 3 (onlyA [3]).--surface: with the ring held at 1536 frames by a local hook, seeks past it landed in 81, 161 and 219 ms, 415 base-layer-only frames until the playhead reached held packets, 2254 presented.--surfacecompleted with the overlay shown.swift testpasses without the trait (5577 Swift Testing tests in 754 suites, XCTest 0 failures). The strict pool run with all traits passes (5993 tests in 794 suites). tvOS and iOSbuild-for-testingsucceed with and without the trait.swift test --enable-all-traitsreported 36 issues in 11 local-origin network suites at load 16 to 45; each of the 11 passes alone. I did not run that family onorigin/mainin this round, and they are not on a known-flake list. 12 runs of the dual-layer suites had 0 hangs.DualLayerVideoDecoderTests, also onorigin/main(2 of 120 runs of the suite): a frame loses its ticket toOrderedDelivery's stall guard and the test waits on it with no limit, so the run stops at the 300 s test limit. This branch takes no decoder lock between pairing a frame and delivering it, because an earlier revision that did so made the race common (14 of 23 runs at the same load). The upstream race is not fixed here.Measured on an earlier revision, not repeated on this branch:
--surfacecapture shows the overlay filling the window with the right aspect and colour (a Mac compositor capture, not a Dolby Vision signal).Frame 3 (125 ms in): earlier runs never presented it. The plain run on this branch pulled 2181 frames and the route run above did not present frame 3; another run pulled all 2182. The frame itself was not checked.
Memory
Measured on a Mac with a 91 s 4K Profile 7 FEL clip, one run at a time, 100 s each, median of samples from 40 to 95 s, nothing else running. "Player" is the app process, "decoder service" the system video decoder service its playback uses. Totals count each shared picture buffer once (one
footprintcall over both processes); the plain sum of the two processes is 250 to 290 MB higher for the two AVPlayer rows because shared buffers show in both.(The software host's player and service figures trade places between runs, because a shared buffer is attributed to one process or the other; its total stays at about 710 MB.)
What the route adds to plain AVPlayer, in the decoder service (+406 MB of picture buffers): +262 MB from attaching the
AVPlayerItemVideoOutputwith the route's pixel buffer attributes (measured with the output attached and no frame pulled, no enhancement layer, nothing shown), +95 MB from pulling frames 0.15 s ahead of the clock (four 4K pictures, 24 MB each; 0.10 s gives one picture less), +49 MB for the enhancement-layer decoder session. In the player: a 196 MB output pool for the composed pictures (opt-in in-place composition avoids most of it but is not shown safe). The CoreMedia pool is AVPlayer's own: 330 MB with or without the output, the enhancement layer or the route; onlypreferredForwardBufferDurationmoved it (1 s instead of 4 s: -30 MB, stall risk not measured).Measured and not a cause or not a lever:
suppressesPlayerRendering(changes the service by 2 MB), the HLS segment window (no change), the output's IOSurface and Metal keys (no change), an 8-bit output (-36 MB, loses the 10-bit picture), the pull lead beyond the 95 MB (at zero lead the service still holds 286 MB more than plain AVPlayer). Requesting no pixel format at all removes the 262 MB, but AVPlayer then delivers a packed lossless 10-bit format the composer does not read correctly (every frame differs), so the route requestsx420; a conversion step would be needed to use it. Inferred, not measured: that the 262 MB is look-ahead storage AVFoundation keeps for a requested format (the attach-only run proves the cost is the attach, not why it is that size).The route therefore costs about 540 MB more than the software host and about 550 MB more than plain AVPlayer on this clip. Other clips and bit rates were not measured.
Tested on Apple TV
One run of 11 minutes on an Apple TV 4K, through
AetherPlayerView, no seeks, a 4K Profile 7 FEL title with TrueHD Atmos, on the commit before the merge of currentorigin/main, with failure logging added locally:total=15739 dropped=0 corrupted=0, and 0 in all 154 samples. Composed 15,510, no bypasses, no failed writes, peak footprint 743 MB, available memory never below 1358 MB, mean compose GPU time about 18 to 24 ms per frame (highest 74 ms). So the 13.5% did not recur, and when those drops happen is still unknown. This run had one item failure,-11868at a playhead of 21.42 s (underlying-17223). The reload on the master was refused, which set the media-direct latch, and the media playlist played the rest, about 10.5 of the 11 minutes, after about 3.5 s of frozen picture, resuming at the position it failed at. The first run stayed on the master, so the two runs are not like for like, and with one run each a link between the drops and the master is only a guess. Nobody watched this run either.AVPlayerViewControllerwith the display layer in its overlay, had AVPlayer's item fail once with-11868 'Cannot open'at a playhead of 22.74 s. Recovery on the same master was refused, and the media playlist then played to the end. That refusal latches the process to media-direct HDR loads. It did not recur in the 11-minute run or in 3 minutes of a later launch of that host. It is intermittent and its cause is not known, so the route is not ruled out.-11868is not specific to this route. Ten fresh 60-second starts of the same title on this PR's tree with logging added, alternating: plain AVPlayer with the enhancement layer discarded failed in 5 of 5, this route in 5 of 5, every time at a playhead of 21.4 to 21.8 s, 1.4 to 1.8 s afterseg6.mp4was served in full. All ten then played on from the media playlist. The cause is not known, and the logs show no failed request. The panel had accepted the same master 22 s earlier, so the engine's reading of the refusal as a panel HDR refusal looks wrong here, and the first route run above, on the earlier commit, did not fail, though the two commits serve identical bytes for the master, playlists, init and the first eight segments, so the commit does not explain that. The Mac does not fail. A plain start about 10 minutes after restarting the TV failed the same way, and so did three route starts on the earlier commit that had passed that morning, so the commit is ruled out: 15 of 15 starts on the master have failed since then, against 2 earlier passes. Something that changed on the TV, the title or the origin during that day explains the passes. The only logged difference is free space on the TV (about 6.9 GiB against 3.9 GiB), with no mechanism shown; a start with the session cache budget set to the morning value failed too. On this title the master therefore cannot be held past that point, and the 13.5% has only been seen on that first run.total=7098 dropped=0 corrupted=0. Each of the five 60-second route starts reported 1345 to 1355 frames and 0 dropped. So every drop figure above 0 so far comes from the one run that stayed on the master, and all the 0 figures are nearly all media playlist. An attempt to measure the TV decoder's memory with Instruments in that 5-minute run failed, because the capture disconnected about 1 s after attaching during playback, so that number is still missing.Still needs Apple TV
saved about 90 MB on the Mac; 0 bypassed frames at 0.15 s and 0.10 s and 38 at 0.05 s); memory on a TV decoder; seeks on a device.
AVPlayerItemVideoOutputbuffers can be composed in place on tvOS hardware decoders (opt-in only).wants it, subtitles on this route, audio-to-video offset on a real display chain.
AVPlayerViewControllergets no picture from the route (the player's rendering is suppressed); theoption is for hosts using the engine's
AetherPlayerView.