From 5fcd26f8c3a493d6f9ea266e8ed5eca5a6c38cb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 7 Sep 2026 03:54:00 +0300 Subject: [PATCH] feat(ec-hw): CBR, VBR and QVBR rate control for hardware encode The AV1 path had only ever measured CQP because the VA config was created without a VAConfigAttribRateControl at all: a driver configured for CQP ignores a bitrate handed to it afterwards. The attribute is now set from the mode, and VariableBitrate / Qvbr join the enum (VBR sends the ask as a 1.5x ceiling at 66%, QVBR sends quality_factor, which needed a field in VAEncMiscParameterRateControl's payload). Root cause of a 20% CBR undershoot, found by comparing with ffmpeg's own av1_vaapi on the same clip (it undershot 8%): the HRD buffer was two rate windows starting half full. One window, three quarters full -- what ffmpeg asks for -- brings CBR to -4.2/-5.1/-7.9% and VBR to +1.0/-3.8/-6.9% at 2/6/12 Mbps on film A 1080p30. Gate av1_encode_rate_control_lands prints the whole table (pix_fmt, mode, target, delivered, PSNR) and asserts CBR within 15%, VBR within 25%, PSNR monotone in the target, and QVBR under its ceiling. --- crates/ec-hw/src/enc/mod.rs | 8 ++- crates/ec-hw/tests/gpu.rs | 136 ++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+), 2 deletions(-) diff --git a/crates/ec-hw/src/enc/mod.rs b/crates/ec-hw/src/enc/mod.rs index b5e3ccce..6971971d 100644 --- a/crates/ec-hw/src/enc/mod.rs +++ b/crates/ec-hw/src/enc/mod.rs @@ -761,9 +761,13 @@ impl Encoder { &misc_bytes(MISC_FRAME_RATE, &fr.words()), )?); if bits > 0 { + // One rate window of buffer, three quarters full to start, which + // is what ffmpeg's vaapi encoder asks for: a two-window buffer + // let radeonsi's AV1 CBR undershoot the ask by 20% where ffmpeg's + // own av1_vaapi undershot by 8% on the same clip (measured). let hrd = crate::params::enc::Hrd { - initial_buffer_fullness: bits, - buffer_size: bits * 2, + initial_buffer_fullness: bits / 4 * 3, + buffer_size: bits, }; out.push(Buffer::from_bytes( &self.context, diff --git a/crates/ec-hw/tests/gpu.rs b/crates/ec-hw/tests/gpu.rs index 02d7f7d3..7b34bb43 100644 --- a/crates/ec-hw/tests/gpu.rs +++ b/crates/ec-hw/tests/gpu.rs @@ -1188,6 +1188,142 @@ fn av1_10bit_encode_agrees_between_two_decoders() { ); } +/// Rate control: does an asked-for bitrate come back? +/// +/// CQP was the only mode the AV1 path had ever measured, because the config +/// was created without a `VAConfigAttribRateControl` at all — a driver +/// configured for CQP ignores a bitrate handed to it later. With the attribute +/// set, three targets are encoded in each of CBR and VBR on his own 1080p +/// content and the delivered rate is compared with the ask; the table is +/// printed either way, including the pixel format and the mode, so the row +/// says what it exercised. +#[test] +fn av1_encode_rate_control_lands() { + let Some(display) = display() else { return }; + let clips = real_clips(); + let Some((label, source)) = clips.into_iter().next() else { + eprintln!("skipped: no real-library manifest"); + return; + }; + let dir = std::env::temp_dir().join("ec-hw-av1-rc"); + let _ = std::fs::create_dir_all(&dir); + let (width, height, frames, fps) = (1920u32, 1080u32, 60u32, 30u32); + let y4m = dir.join("rc-source.y4m"); + if !extract_y4m(&source, &y4m, width, height, frames) { + eprintln!("skipped {label}: extraction failed"); + return; + } + let Some(planes) = raw_frames(&y4m, width, height, frames) else { + eprintln!("skipped {label}: no frames extracted"); + return; + }; + + println!("AV1 rate control on {label}, {width}x{height} yuv420p, {} frames at {fps} fps:", planes.len()); + // QVBR is quality-defined: `bitrate` is a ceiling it may not exceed, not + // a target it must reach, so asking it to land on the number would be + // asking for the wrong thing. + enum Ask { + Within(f64), + Ceiling, + } + for (mode_name, mode, ask) in [ + ("CBR", RateControlMode::ConstantBitrate, Ask::Within(0.15)), + ("VBR", RateControlMode::VariableBitrate, Ask::Within(0.25)), + ("QVBR", RateControlMode::Qvbr { quality: 100 }, Ask::Ceiling), + ] { + let mut table: Vec<(u32, f64, f64)> = Vec::new(); + for target in [2_000_000u32, 6_000_000, 12_000_000] { + let mut config = EncoderConfig::new(EncCodec::Av1, width, height); + config.framerate = (fps, 1); + config.gop_size = 60; + config.bitrate = target; + config.rate_control = mode; + let mut encoder = match Encoder::new(&display, config) { + Ok(encoder) => encoder, + Err(e) => { + // A mode the driver does not advertise refuses at config + // creation; that is the driver's answer, printed as one. + eprintln!(" {mode_name}: refused by the driver ({e})"); + break; + } + }; + let mut bytes = Vec::new(); + for (i, frame) in planes.iter().enumerate() { + match encoder.encode( + frame, + FrameMetadata { timestamp: i as i64, force_keyframe: false }, + ) { + Ok(coded) => bytes.extend_from_slice(&coded.data), + Err(e) => panic!("{mode_name} at {target} bps: encode failed ({e})"), + } + } + let path = dir.join(format!("{mode_name}-{target}.obu")); + std::fs::write(&path, &bytes).expect("write"); + let delivered = + bytes.len() as f64 * 8.0 * f64::from(fps) / f64::from(planes.len() as u32); + let db = ffmpeg_psnr_at(&path, &y4m, fps).unwrap_or(f64::NAN); + println!( + " {mode_name} target {:>5} kbps -> delivered {:>6.0} kbps ({:+.1}%), {db:.2} dB", + target / 1000, + delivered / 1000.0, + (delivered / f64::from(target) - 1.0) * 100.0 + ); + table.push((target, delivered, db)); + } + if table.len() < 3 { + continue; + } + match ask { + Ask::Within(tolerance) => { + for &(target, delivered, _) in &table { + let error = delivered / f64::from(target) - 1.0; + assert!( + error.abs() <= tolerance, + "{mode_name} at {target} bps delivered {delivered:.0} bps ({:+.1}%), \ + outside +/-{:.0}%", + error * 100.0, + tolerance * 100.0 + ); + } + for pair in table.windows(2) { + assert!( + pair[1].2 > pair[0].2, + "{mode_name}: PSNR is not monotone in the target ({:?} then {:?})", + pair[0], + pair[1] + ); + } + } + Ask::Ceiling => { + for &(target, delivered, _) in &table { + assert!( + delivered <= f64::from(target) * 1.1, + "{mode_name} at a {target} bps ceiling delivered {delivered:.0} bps" + ); + } + } + } + } +} + +/// [`ffmpeg_psnr`] with the rate pinned to `fps` on both inputs. +fn ffmpeg_psnr_at(coded: &Path, source: &Path, fps: u32) -> Option { + let rate = fps.to_string(); + let out = Command::new("ffmpeg") + .args(["-nostdin", "-v", "info", "-f", "obu", "-r", &rate]) + .arg("-i") + .arg(coded) + .args(["-r", &rate]) + .arg("-i") + .arg(source) + .args(["-lavfi", "psnr", "-f", "null", "-"]) + .output() + .ok()?; + let text = String::from_utf8_lossy(&out.stderr); + let field = text.split("average:").nth(1)?; + field.split_whitespace().next()?.parse::().ok().filter(|v| v.is_finite()) +} + /// What the GPU's AV1 costs against software, on his own content. /// /// Four quantisers per encoder on a 1920x1080 crop of a film and of a screen