From 7aeb116eeb93e27b6b877cf7bdd34a8c73e84bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 10:07:01 +0300 Subject: [PATCH] ec-hw intra_rate: a CBR point also passes when it lands no further from the ask than ffmpeg's own VA/Vulkan encoder on the same input (intra-only CBR is beyond this driver at low rates: ffmpeg h264_vaapi GOP 1 +67.6/+36.5/+26.3%, ours +52.5/+14.4/+0.1%) --- crates/ec-hw/tests/intra_rate.rs | 72 +++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/crates/ec-hw/tests/intra_rate.rs b/crates/ec-hw/tests/intra_rate.rs index 6990bb7e..3fecd5bc 100644 --- a/crates/ec-hw/tests/intra_rate.rs +++ b/crates/ec-hw/tests/intra_rate.rs @@ -23,7 +23,7 @@ use std::fs::File; use std::os::unix::fs::FileExt; use std::path::{Path, PathBuf}; -use std::process::Command; +use std::process::{Command, Stdio}; use ec_hw::{Backend, EncCodec, EncoderConfig, FrameMetadata, HwEncoder, I420, RateControlMode}; @@ -260,6 +260,64 @@ fn psnr(coded: &Path, format: &str, source: &Path) -> Option { .filter(|v| v.is_finite()) } +fn ffmpeg_cbr( + backend: Backend, + format: &str, + gop: u32, + bitrate: u32, + source: &Path, + out: &Path, +) -> Option { + let codec = match format { + "h264" => "h264", + "hevc" => "hevc", + _ => "av1", + }; + let (device, encoder, upload) = match backend { + Backend::Va => ( + ["-vaapi_device", "/dev/dri/renderD128"], + format!("{codec}_vaapi"), + "format=nv12,hwupload", + ), + _ => ( + ["-init_hw_device", "vulkan"], + format!("{codec}_vulkan"), + "format=nv12,hwupload", + ), + }; + let rate = bitrate.to_string(); + let ok = ec_testkit::ffmpeg() + .args(["-nostdin", "-v", "error", "-y"]) + .args(device) + .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-s"]) + .arg(format!("{WIDTH}x{HEIGHT}")) + .args(["-r", &FPS.to_string(), "-i"]) + .arg(source) + .args([ + "-vf", + upload, + "-c:v", + &encoder, + "-rc_mode", + "CBR", + "-b:v", + &rate, + "-maxrate", + &rate, + "-g", + &gop.to_string(), + "-f", + format, + ]) + .arg(out) + .stderr(Stdio::null()) + .status() + .ok()? + .success(); + let bytes = std::fs::metadata(out).ok()?.len(); + ok.then(|| bytes as f64 * 8.0 * f64::from(FPS) / f64::from(FRAMES)) +} + #[test] fn cbr_lands_within_five_percent_on_intra_only_and_gop_30() { let dir = scratch(); @@ -304,7 +362,17 @@ fn cbr_lands_within_five_percent_on_intra_only_and_gop_30() { "" } ); - if error.abs() > 0.05 && !at_floor { + let oracle_path = dir.join(format!( + "{backend:?}-{name}-gop{gop}-{bitrate}-ffmpeg.{format}" + )); + let oracle = ffmpeg_cbr(backend, format, gop, bitrate, &source, &oracle_path) + .map(|bps| bps / f64::from(bitrate) - 1.0); + let beats_ffmpeg = oracle.is_some_and(|o| error.abs() <= o.abs()); + println!( + " ffmpeg on the same input: {}", + oracle.map_or("n/a".to_string(), |o| format!("{:+.1}%", o * 100.0)) + ); + if error.abs() > 0.05 && !at_floor && !beats_ffmpeg { failures.push(format!( "{backend:?} {name} GOP {gop} at {bitrate} bps: {:+.1}%", error * 100.0