Skip to content

media: apple: ave: constant-quality rate control - #1128

Closed
RyanTheTide wants to merge 1 commit into
integration/aurorafrom
avx/ave-cq
Closed

RyanTheTide wants to merge 1 commit into
integration/aurorafrom
avx/ave-cq

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Adds constant-quality rate control to the AVE encoder: V4L2_MPEG_VIDEO_BITRATE_MODE_CQ with V4L2_CID_MPEG_VIDEO_CONSTANT_QUALITY (0-100, 100 best, default 50). This is the one rate-control mode macOS offers (VideoToolbox kVTCompressionPropertyKey_Quality) that Linux refused until now.

The session and frame writers already had the constant-quality layout (rate-control mode 3); the driver refused CQ only because the quality-to-QP mapping was unknown. That mapping is now measured: on macOS 27.0 on the M4 Pro, VideoToolbox encodes every frame of a constant-quality session with one QP, and reading the slice QPs of its own output at every step from 0.00 to 1.00 gives the table in ave_cq_qp[] (H.264 and HEVC give the same values; the earlier J700 captures at 0.30, 0.60 and 0.90 agree: QP 40, 28, 10; the QP never exceeds 48). HEVC constant-quality frame sizes differ from macOS (7092 vs 8301 bytes at quality 30) on the Neo and on the J616s alike, at the same QP, because macOS HEVC P-frames use weighted prediction; H.264 matches to the picture.

Validation

  • Neo (J700), 7.3.0-aurora-fleet23 + this driver as a signed module on a fresh boot, 1080p, 150 frames of identical raw input (Mandelbrot zoom with temporal grain), GOP 30, no B-frames. Average H.264 frame sizes equal to macOS 27.0 on the M4 Pro for the same input and quality: 13311/27508/61771/233554 bytes on Linux vs 13312/27509/61772/233555 on macOS at quality 30/45/60/75 (the averages are rounded differently). The bitstreams carry exactly the table's QP.
  • Same-chip comparison on the J616s (M4 Pro, fleet27 + this driver, 2026-11-08): QP equal to macOS at every quality for both codecs. H.264 pictures are identical (same PSNR-Y; sizes differ by 178 bytes of headers). HEVC sizes match the Neo, so this is not a generation difference: macOS HEVC P-frames use weighted prediction and reference-list modification, which Linux does not. The RD cost is +0.7% BD-rate (PSNR-Y). Rows in the PR comments.
  • VBR is unchanged (H.264 1080p 178.6 fps, 16530-byte average frame, the same as before).
  • checkpatch.pl --strict clean.

Benchmarks (Neo, CQ, 1080p, fps)

quality H.264 HEVC
30 181.3 166.7
50 182.0 165.2
75 150.2 166.8
90 53.5 (974 KB frames) 140.0

Still a draft until the HEVC and J616s rows are in.

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

Copy link
Copy Markdown
Contributor Author

Same-chip comparison on the J616s (M4 Pro). Linux: 7.3.0-aurora-fleet27 with this driver as a signed module (apple-ave-fix-fleet27.ko, d110f2f1) on a fresh boot (18071ced). macOS: the AVX VideoToolbox CQ captures on the same machine. Same raw input both sides (mandel-1080.nv12, 150 frames, GOP 30, no B-frames). PSNR-Y is against that raw input with ffmpeg psnr. The slice QP is read from each bitstream with trace_headers.

codec quality Linux avg bytes macOS avg bytes Linux QP macOS QP Linux PSNR-Y macOS PSNR-Y
H.264 30 13311 13312 40 40 32.302 32.302
H.264 45 27508 27509 35 35 34.714 34.714
H.264 60 61771 61772 28 28 37.293 37.293
H.264 75 233554 233555 21 21 39.689 39.689
HEVC 30 7092 8301 40 40 31.228 31.508
HEVC 45 23802 24721 35 35 34.317 34.480
HEVC 60 58462 59170 28 28 37.202 37.237
HEVC 75 156360 165496 21 21 38.761 38.866
  • The quality-to-QP table is exact on the same chip for both codecs. Every slice of every frame carries the table's QP on both OSes.
  • The H.264 pictures are identical: same PSNR to the last digit, and the file sizes differ by a constant 178 bytes of stream headers.
  • The HEVC size difference is the same on the J616s as on the Neo (7092 bytes at 30 on both), so it is not a generation difference. It comes from P-frame tools. The macOS PPS sets weighted_pred_flag=1 and lists_modification_present_flag=1 and puts the base QP in init_qp_minus26 (9 at quality 45). Linux sets both flags to 0 and carries the same QP as init_qp_minus26=2 plus slice_qp_delta. macOS also adds a user-data SEI per frame. On rate-distortion the gap is small: BD-rate of Linux vs macOS HEVC CQ over the four points is +0.7% (PSNR-Y). That is a separate HEVC weighted-prediction question, not a field missing from the CQ path.
  • Linux CQ throughput on the J616s at 1080p: H.264 228/233/233/233/192/69 fps and HEVC 214/214/214/214/214/179 fps at quality 30/45/50/60/75/90. macOS at 30/45/60/75 runs 237/237/237/207 (H.264) and 222 (HEVC). VBR on the same boot: H.264 231.7 fps, HEVC 216.8 fps, unchanged.

Data: work/avd/AVX/linux-j616s/{cq-j616s,cq-j616s.log,speed-psnr-j6pm.txt} on the S500.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Merged as part of #1228 (merge 27c200d): the same constant-quality commit, with the speed-over-quality control, J616s + Neo rows in its body.

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