Repository navigation
media: apple: ave: constant quality and speed-over-quality (macOS parity) - #1228
Merged
Merged
Conversation
Offer V4L2_MPEG_VIDEO_BITRATE_MODE_CQ with V4L2_CID_MPEG_VIDEO_CONSTANT_QUALITY (0-100, 100 best). The session and frame writers already carry the constant-quality layout (rate-control mode 3); what was missing was the quality-to-QP mapping. Measured on macOS 27.0 on the M4 Pro from the slice QPs of its own VideoToolbox output at every quality step 0.00-1.00 (H.264 and HEVC agree, and so do the J700 captures at 0.30, 0.60 and 0.90): one QP for every frame, capped at 48. Use the same table, no data-rate limit, and twice the coded-data capacity, as macOS allows. Signed-off-by: Ryan Murray <[email protected]> Assisted-by: LLM
Add a driver-private boolean control, "Prioritize Speed Over Quality" (V4L2_CID_APPLE_AVE_PRIORITIZE_SPEED), off by default. It is VideoToolbox's PrioritizeEncodingSpeedOverQuality: macOS changes one Start word from 3 to 5 for it, the word the driver called the size class (3 above 720p). With 5 there, HEVC encodes 1.7-1.8 times faster for about 12% more bits at the same PSNR; H.264 does not get faster (on macOS either), so the control only applies to 8-bit HEVC above 1280x720 (Main 10 is not measured yet). macOS 27.0 also changes a per-frame Process word from 3 to 5. Its counterpart in this firmware's layout, the low-delay word at Process +0xcf8, is not the same field: writing 5 there corrupts the coded pictures (PSNR-Y 16 dB), so it is left alone. With only the Start word the bitstreams match what macOS writes in speed mode to the byte average. The control is read when streaming starts. With the control off, every command the driver builds is byte-identical to before. Also expose the constant-quality table lookup for a KUnit test, and document constant quality and the new control. Signed-off-by: Ryan Murray <[email protected]> Assisted-by: LLM
Contributor
Author
|
Correction to the CQ note in the body, from our own decode of macOS 27.0's HEVC constant-quality output on the J616s (mandel, quality 0.30, all 150 frames, ffmpeg trace_headers). macOS does set weighted_pred_flag=1 and lists_modification_present_flag=1 in the PPS. However, every one of the 145 P slices has luma_weight_l0_flag=0, chroma_weight_l0_flag=0 and ref_pic_list_modification_flag_l0=0. So neither tool changes prediction, and the 1.0-1.2 KB/frame and +0.7% BD-rate difference does not come from them. It comes from encoder decisions inside macOS 27's newer AVE firmware; Linux runs the 25G83 build. macOS 26 (25G83) CQ output on the Neo sets neither flag. Follow-up tracked internally. |
This was referenced Nov 20, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Brings the AVE encoder to macOS parity for the two rate/speed modes VideoToolbox offers and Linux lacked. Replaces #1128 and #1227 (same constant-quality commit, now on the current
integration/aurora).a3044a66, unchanged from media: apple: ave: constant-quality rate control #1128):V4L2_MPEG_VIDEO_BITRATE_MODE_CQwithV4L2_CID_MPEG_VIDEO_CONSTANT_QUALITY(0-100, 100 best, default 50). Each quality step maps to one QP for every frame, from a table measured on macOS 27.0 on the M4 Pro (slice QPs of VideoToolbox's own output at every step from 0.00 to 1.00; H.264 and HEVC agree; capped at 48).556fa115): a new driver-private boolean control,Prioritize Speed Over Quality(V4L2_CID_APPLE_AVE_PRIORITIZE_SPEED, codec class base0x1200, off by default). It is VideoToolbox'sPrioritizeEncodingSpeedOverQuality. macOS changes one Start word from 3 to 5 for it (the word the driver called the size class). The control applies to 8-bit HEVC above 1280x720 (Main 10 is not measured yet). H.264 does not get faster from it, on macOS either. It is read at STREAMON. With the control off, every command is byte-identical to before.macOS 27.0 also changes a per-frame Process word from 3 to 5 in speed mode. I tested whether the low-delay word at Process
+0xcf8is the same field in this firmware's layout, and it is not. Writing 5 there corrupts the coded pictures (PSNR-Y 16 dB at every rate), so the driver does not write it. With the Start word alone, the bitstreams match macOS speed mode to the byte average (1080p 16712 vs 16713 bytes per frame, 4K 17430 vs 17430). Both Macs produce bit-identical speed-mode output.Method
ENCODER_CMD_STOPdrain, fps = frames / wall time). Synthetic input: 1080p 600 frames and 4K 120 frames, VBR 4 Mb/s, GOP 30, no B-frames unless noted, 3 runs per row. Quality runs use a 150-frame 1080p Mandelbrot zoom with temporal grain, decoded in software with ffmpeg; PSNR-Y is measured against the source and BD-rate is computed from the PSNR-Y curves.insmodright after boot. Stock = the fleet kernel's ownapple-aveafter a reboot to the default. Both Macs were left on their defaults afterwards.7.3.0-aurora-fleet33. The test module also carried a test-only parameter to compare speed mode with and without the Process word in the same boot. That parameter is not in this PR. Its "off" setting runs the code that is in this PR.7.3.0-aurora-fleet34, with this PR's code. The one change made after the hardware runs keeps P010/Main 10 sessions out of speed mode, and those sessions take the unchanged quality path.AllowFrameReordering=false), 2026-11-07.Results: fps, mean of 3 runs (individual runs in brackets)
Speed mode makes HEVC 1.70x (1080p) and 1.82x (4K) faster on both Macs. It matches macOS at 4K (106.0 vs 106.2) and comes within 1.8% at 1080p. macOS runs its own 27.0 encoder firmware; Linux runs the 25G83 build, which may account for that 1.8%. With the control off, the commands are byte-identical to before (host tests below), so the 1-2% spreads between stock and this PR are run-to-run variance. The J616s H.264 4K test runs 2 and 3 were 59.6/59.9, while run 1 and all three stock runs were 61.6-61.7 with the same commands.
Results: quality
Results: constant quality (mandel 1080p, average bytes per frame, fps)
H.264 pictures are identical to macOS: same QP and same PSNR-Y, and the 1-byte difference is in the headers. HEVC uses the same QP as macOS, but its frames are smaller than macOS's (7092 vs 8301 bytes at quality 30) because macOS HEVC P-frames use weighted prediction and reference-list modification. The RD cost of that difference is +0.7% BD-rate, measured on the same chip (rows in #1128). CQ combined with speed mode (Neo, this PR's code, HEVC quality 50): 280.6 fps, 23435 bytes per frame, PSNR-Y 33.31 dB, against 165.9 fps, 31723 bytes and 34.76 dB without speed mode.
Checks
v4l2-compliance: 48/48 on both Macs, with the new control.apple-ave-abi): newave_9003_speed, which checks that speed mode changes only Start+0x320(3 to 5) and no Process word, with and without B-frames. Newave_cq_tablechecks the table's anchors and that it never increases.tests/run.pyandrun-recovery.pypass.--compare-baselineagainst bothintegration/auroraand the CQ commit: 288 J700 sessions / 1440 commands byte-identical.tools/asahi/run-host-tests.py --only ave5/5.W=1module build: no warnings.checkpatch.pl --strict: 0/0/0 on both patches.🤖 Generated with Claude Code