Skip to content

media: apple: ave: constant quality and speed-over-quality (macOS parity) - #1227

Closed
RyanTheTide wants to merge 2 commits into
integration/aurorafrom
media/ave-cq-speed
Closed

RyanTheTide wants to merge 2 commits into
integration/aurorafrom
media/ave-cq-speed

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

Brings the AVE encoder to macOS parity for the two rate/speed modes VideoToolbox offers and Linux lacked. Replaces #1128 (same constant-quality commit, now on the current integration/aurora).

  1. Constant quality (a3044a66, unchanged from media: apple: ave: constant-quality rate control #1128): V4L2_MPEG_VIDEO_BITRATE_MODE_CQ with V4L2_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).
  2. Speed over quality (857fc704): a new driver-private boolean control, Prioritize Speed Over Quality (V4L2_CID_APPLE_AVE_PRIORITIZE_SPEED, codec class base 0x1200, 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). The control applies to HEVC above 1280x720. 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 +0xcf8 is 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

  • Tool: a V4L2 encoder bench (16 OUTPUT buffers queued, MMAP, ENCODER_CMD_STOP drain, 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.
  • The AVE firmware cannot be reloaded within one boot, so each variant got a fresh boot. Test = this driver as a module swapped in by insmod right after boot. Stock = the fleet kernel's own apple-ave after a reboot to the default. Both Macs were left on their defaults afterwards.
  • J616s (M4 Pro, T6040), 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.
  • Neo (J700, T8140), 7.3.0-aurora-fleet34, with exactly this PR's code.
  • macOS reference: macOS 27.0 on the same J616s, VideoToolbox with the same synthetic input and settings (AllowFrameReordering=false), 2026-11-07.

Results: fps, mean of 3 runs (individual runs in brackets)

Mac row stock this PR, control off this PR, speed on macOS quality / speed
J616s HEVC 1080p 216.3 (215.9/216.3/216.6) 215.7 (216.7/214.9/215.5) 367.7 (365.0/369.4/368.7) 216.8 / 374.5
J616s HEVC 4K 58.3 (58.3/58.3/58.2) 58.3 (58.3/58.3/58.2) 106.0 (106.2/105.8/105.9) 58.3 / 106.2
J616s HEVC 1080p, B-frames 217.2 216.6 368.2 (369.4/367.5/367.6)
J616s HEVC 720p (control does not apply) 370.9 368.3 367.8 (bitstream identical to off)
J616s H.264 1080p (control does not apply) 230.1 (231.0/231.2/228.0) 229.9 (231.3/230.2/228.1) 228.6 (bitstream identical) 231.1 / 231.0
J616s H.264 4K (control does not apply) 61.7 (61.6/61.7/61.7) 60.4 (61.7/59.6/59.9) 60.6 61.7 / 61.8
Neo HEVC 1080p 168.4 (168.5/168.6/168.0) 168.0 (168.3/167.9/167.9) 282.5 (279.4/284.5/283.7)
Neo HEVC 4K 44.8 (42.8/45.8/45.9) 45.8 (45.8/45.8/45.8) 83.1 (83.4/82.8/83.2)
Neo HEVC 1080p, B-frames 165.2 165.4 285.5
Neo H.264 1080p 175.4 (171.5/177.5/177.2) 177.0 177.1 (bitstream identical)

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

  • Quality mode unchanged: the mandel HEVC 1/2/4/8 Mb/s and H.264 4 Mb/s bitstreams from this PR (control off) are bit-identical to the stock module's on the J616s and on the Neo.
  • Speed mode cost: BD-rate (PSNR-Y) +12.4% on both Macs, which produce bit-identical streams. macOS speed mode costs +10.8% for the same input (measured on the M2).
Mb/s quality: kb/s, PSNR-Y speed: kb/s, PSNR-Y speed fps (J616s / Neo)
1 1037.8, 30.42 dB 1041.0, 30.37 dB 357.5 / 277.2
2 2038.4, 31.45 dB 2049.8, 31.45 dB 356.8 / 275.1
4 4048.5, 32.80 dB 4040.7, 32.46 dB 356.7 / 275.6
8 8106.9, 34.56 dB 8110.1, 33.73 dB 355.9 / 276.6
  • The rejected Process-word variant produced 1037-8102 kb/s at PSNR-Y 16.2-16.4 dB (broken pictures). That is why it is not in the PR.

Results: constant quality (mandel 1080p, average bytes per frame, fps)

quality H.264 J616s = Neo macOS H.264 HEVC J616s = Neo J616s / Neo fps (H.264, HEVC)
30 13311 13312 7092 230.8, 211.8 / 180.3, 166.3
45 27508 27509 23802 230.6, 212.1 / 180.3, 166.1
50 34840 31723 231.4, 212.7 / 180.2, 165.9
60 61771 61772 58462 231.6, 213.1 / 181.1, 166.6
75 233554 233555 156360 190.3, 213.9 / 150.0, 166.6
90 974602 1003362 68.5, 176.5 / 53.6, 140.2

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.
  • 0 DART faults, 0 AVC denials and 0 AVE errors in every boot. SELinux enforcing.
  • KUnit (apple-ave-abi): new ave_9003_speed, which checks that speed mode changes only Start +0x320 (3 to 5) and no Process word, with and without B-frames. New ave_cq_table checks the table's anchors and that it never increases.
  • Host tests: tests/run.py and run-recovery.py pass. --compare-baseline against both integration/aurora and the CQ commit: 288 J700 sessions / 1440 commands byte-identical. tools/asahi/run-host-tests.py --only ave 5/5.
  • W=1 module build: no warnings. checkpatch.pl --strict: 0/0/0 on both patches.

🤖 Generated with Claude Code

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 HEVC above 1280x720.

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
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Superseded by #1228: same two commits, with speed mode limited to 8-bit HEVC (Main 10 is not measured yet).

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.

1 participant