From ebea85eca0c7e63ced998b046f5a2b70d6d7dcd5 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:56:54 +0300 Subject: [PATCH] feat(ec-hw): 10-bit BD row, 10-bit fps, ec-bench 10-bit row, docs The 10-bit BD row is film B's own 1920x1024 native crop, every arm in the 10-bit domain: hw AV1 10-bit is +36.7% BD-rate against librav1e speed 6 10-bit and +52.9% against libaom cpu-used 6 10-bit, with all three ladders asserted monotone. av1_encode_speed gains a 3840x1608 10-bit row, and ec-bench an 'ec-hw av1 10-bit' encode row that skips as a row without a VA device. --- crates/ec-hw/src/enc/mod.rs | 14 ++++ crates/ec-hw/src/lib.rs | 6 ++ crates/ec-hw/tests/gpu.rs | 124 ++++++++++++++++++++++++++++++++++++ tools/ec-bench/src/main.rs | 91 ++++++++++++++++++++++++++ 4 files changed, 235 insertions(+) diff --git a/crates/ec-hw/src/enc/mod.rs b/crates/ec-hw/src/enc/mod.rs index 6971971d..94bd1fa6 100644 --- a/crates/ec-hw/src/enc/mod.rs +++ b/crates/ec-hw/src/enc/mod.rs @@ -16,6 +16,20 @@ //! driver composes no headers of its own from thin air: it parses the //! sequence and frame header OBUs this crate packs (see `enc::headers::av1`) //! and writes the stream's headers from them. +//! +//! AV1 encodes at 8 or 10 bits ([`EncoderConfig::bit_depth`]); 10-bit is the +//! depth his own library is stored at, and `gpu.rs`'s +//! `av1_10bit_encode_agrees_between_two_decoders` proves a 10-bit stream out +//! of this path decodes identically through `ec-av1` and libdav1d with +//! ffprobe reading `yuv420p10le` out of the sequence header. +//! +//! # Rate control +//! +//! [`RateControlMode`] is not advisory: it is set as +//! `VAConfigAttribRateControl` when the VA config is created, so an encoder +//! built for CQP ignores a bitrate handed to it later. CQP, CBR, VBR and QVBR +//! are all measured by `gpu.rs`'s `av1_encode_rate_control_lands`, which +//! prints what each mode delivered against what it was asked for. use std::sync::Arc; diff --git a/crates/ec-hw/src/lib.rs b/crates/ec-hw/src/lib.rs index 5beceec5..0d57ce4c 100644 --- a/crates/ec-hw/src/lib.rs +++ b/crates/ec-hw/src/lib.rs @@ -25,6 +25,12 @@ //! call [`Frame::to_i420_16`] instead — that is the lossless path that keeps //! all ten bits. //! +//! Encoding is 10-bit too, for AV1: `EncoderConfig::ten_bit()` allocates P010 +//! surfaces and takes its pixels through [`Encoder::encode_16`] (or straight +//! from a 10-bit [`Frame`], with no conversion at all). H.264 and H.265 stay +//! 8-bit here, because High and Main — the profiles this crate configures — +//! are 8-bit profiles. +//! //! # Shape //! //! * [`params`] — the `#[repr(C)]` codec parameter buffers, each checked diff --git a/crates/ec-hw/tests/gpu.rs b/crates/ec-hw/tests/gpu.rs index 7b34bb43..43483bfb 100644 --- a/crates/ec-hw/tests/gpu.rs +++ b/crates/ec-hw/tests/gpu.rs @@ -1324,6 +1324,114 @@ fn ffmpeg_psnr_at(coded: &Path, source: &Path, fps: u32) -> Option { field.split_whitespace().next()?.parse::().ok().filter(|v| v.is_finite()) } +/// What the GPU's 10-bit AV1 costs against 10-bit software, on his own film. +/// +/// The same shape as the 8-bit row, at film B's native 1920x1024 crop and +/// entirely in the 10-bit domain: no arm ever sees an 8-bit sample, so the +/// comparison is encoder against encoder rather than depth against depth. +/// Skips when the manifest has no 10-bit film -- a synthetic gradient would +/// be a BD-rate number about a gradient. +#[test] +fn av1_10bit_bd_row_vs_software() { + let Some(display) = display() else { return }; + let dir = std::env::temp_dir().join("ec-hw-av1-10bit-bd"); + let _ = std::fs::create_dir_all(&dir); + let frames = 12u32; + let Some((label, y4m, planes)) = ten_bit_source(&dir, frames) else { + eprintln!("skipped: no 10-bit source could be built"); + return; + }; + if !label.starts_with("film") { + eprintln!("skipped: no 10-bit film in the real-library manifest ({label})"); + return; + } + let (width, height) = (planes[0].width, planes[0].height); + let coded = planes.len() as u32; + + let mut ours = Vec::new(); + for qp in [180u32, 140, 100, 60] { + let data = match encode_stream_16( + &display, + &planes, + qp, + RateControlMode::ConstantQp { qp }, + ) { + Ok(data) => data, + Err(e) => { + eprintln!("skipped: 10-bit AV1 encode unavailable ({e})"); + return; + } + }; + let path = dir.join(format!("hw10-{qp}.obu")); + std::fs::write(&path, &data).expect("write"); + if let Some(db) = ffmpeg_psnr(&path, &y4m, true) { + ours.push((data.len() as f64 * 8.0 / f64::from(coded), db)); + } + } + let rav1e: Vec<(f64, f64)> = [180u32, 140, 100, 60] + .iter() + .filter_map(|q| { + software_point( + &dir, + &y4m, + "film-B-10bit", + "librav1e", + &[ + "-pix_fmt", "yuv420p10le", + "-rav1e-params", &format!("speed=6:quantizer={q}"), + "-g", "60", + ], + *q, + coded, + ) + }) + .collect(); + let libaom: Vec<(f64, f64)> = [56u32, 44, 32, 20] + .iter() + .filter_map(|crf| { + software_point( + &dir, + &y4m, + "film-B-10bit", + "libaom-av1", + &[ + "-pix_fmt", "yuv420p10le", + "-cpu-used", "6", "-crf", &crf.to_string(), "-b:v", "0", "-g", "60", + ], + *crf, + coded, + ) + }) + .collect(); + + println!("10-bit BD row: {label}, {width}x{height} yuv420p10le, {coded} frames"); + for (name, ladder) in [ + ("hw AV1 10-bit", &ours), + ("librav1e 10-bit", &rav1e), + ("libaom 10-bit", &libaom), + ] { + println!(" {name}: {ladder:?}"); + for pair in ladder.windows(2) { + assert!( + pair[1].0 > pair[0].0 && pair[1].1 > pair[0].1, + "{name}: ladder is not monotone at {pair:?}" + ); + } + } + if ours.len() >= 3 && rav1e.len() >= 3 { + println!( + " BD-rate: hw AV1 10-bit vs librav1e speed 6 10-bit: {:+.1}%", + bd_rate(&rav1e, &ours) * 100.0 + ); + } + if ours.len() >= 3 && libaom.len() >= 3 { + println!( + " BD-rate: hw AV1 10-bit vs libaom cpu-used 6 10-bit: {:+.1}%", + bd_rate(&libaom, &ours) * 100.0 + ); + } +} + /// 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 @@ -1653,6 +1761,22 @@ fn av1_encode_speed() { } } } + // 10-bit at the size his own films are in: the depth his library is + // actually stored at, so the fps that matters for a real export. + let planes: Vec = (0..30).map(|t| source_frame_16(3840, 1608, t)).collect(); + let start = Instant::now(); + match encode_stream_16(&display, &planes, 90, RateControlMode::ConstantQp { qp: 90 }) { + Ok(data) => { + let secs = start.elapsed().as_secs_f64(); + println!( + "AV1 encode 3840x1608 10-bit: {:.1} fps ({} frames in {secs:.2} s), {} kbit/frame", + planes.len() as f64 / secs, + planes.len(), + data.len() * 8 / planes.len() / 1000 + ); + } + Err(e) => eprintln!("skipped: 10-bit AV1 encode unavailable ({e})"), + } } // --------------------------------------------------------------------------- diff --git a/tools/ec-bench/src/main.rs b/tools/ec-bench/src/main.rs index e2d2d862..7c44680d 100644 --- a/tools/ec-bench/src/main.rs +++ b/tools/ec-bench/src/main.rs @@ -43,6 +43,7 @@ fn main() { bench_h265_encode(&mut rows); bench_av1_encode(&mut rows); bench_hw_av1_encode(&mut rows); + bench_hw_av1_encode_10bit(&mut rows); bench_audio_decode(&mut rows); bench_audio_encode(&mut rows); bench_truehd(&mut rows); @@ -400,6 +401,96 @@ fn bench_hw_av1_encode(rows: &mut Vec) { }); } +/// The same row at 10 bits, which is the depth his own AV1 library is stored +/// at (`yuv420p10le`, both films). Skips as a row when there is no VA device, +/// no AV1 encode entrypoint, or no source to read. +fn bench_hw_av1_encode_10bit(rows: &mut Vec) { + let Some(src) = real_video("av1").or_else(|| real_video("hevc")).or_else(|| real_video("h264")) + else { + rows.push(missing("ec-hw av1 10-bit", "encode")); + return; + }; + let display = match ec_va::Display::open() { + Ok(display) => display, + Err(e) => { + rows.push(missing_direction("ec-hw av1 10-bit", "encode", &format!("no VA display ({e})"))); + return; + } + }; + let (w, h) = (1920u32, 1080u32); + let raw = extract_yuv420p10(&src, w, h, 30); + let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); + let frame_len = ((w * h + 2 * cw * ch) * 2) as usize; + let n = (raw.len() / frame_len) as u32; + if n == 0 { + rows.push(missing("ec-hw av1 10-bit", "encode")); + return; + } + let mut cfg = ec_hw::EncoderConfig::new(ec_hw::EncCodec::Av1, w, h).ten_bit(); + cfg.rate_control = ec_hw::RateControlMode::ConstantQp { qp: 120 }; + let mut enc = match ec_hw::Encoder::new(&display, cfg) { + Ok(enc) => enc, + Err(e) => { + rows.push(missing_direction("ec-hw av1 10-bit", "encode", &format!("driver refused ({e})"))); + return; + } + }; + let read = |s: &[u8]| -> Vec { + s.chunks_exact(2).map(|p| u16::from_le_bytes([p[0], p[1]])).collect() + }; + let start = Instant::now(); + let mut coded = 0usize; + for (i, frame) in raw.chunks_exact(frame_len).enumerate() { + let (y, rest) = frame.split_at((w * h * 2) as usize); + let (u, v) = rest.split_at(rest.len() / 2); + let picture = ec_hw::I420_16 { + y: read(y), + u: read(u), + v: read(v), + width: w, + height: h, + bit_depth: 10, + }; + let meta = ec_hw::FrameMetadata { timestamp: i as i64, force_keyframe: false }; + match enc.encode_16(&picture, meta) { + Ok(out) => coded += out.data.len(), + Err(e) => { + rows.push(missing_direction("ec-hw av1 10-bit", "encode", &format!("encode failed ({e})"))); + return; + } + } + } + let wall = start.elapsed().as_secs_f64(); + let media_s = f64::from(n) / 30.0; + rows.push(Row { + component: "ec-hw av1 10-bit", + direction: "encode", + content: format!("{w}x{h} yuv420p10le, {n} frames, CQP 120, {} kB", coded / 1000), + media: format!("{media_s:.1}s"), + wall_ms: wall * 1000.0, + rtf: (wall > 0.0).then_some(media_s / wall), + }); +} + +/// `extract_yuv420p` at ten bits: P010-ordered planar 16-bit little endian. +fn extract_yuv420p10(src: &Path, w: u32, h: u32, n: u32) -> Vec { + let out = Command::new("ffmpeg") + .args(["-v", "error", "-i"]) + .arg(src) + .args([ + "-vf", &format!("scale={w}:{h}"), + "-frames:v", &n.to_string(), + "-pix_fmt", "yuv420p10le", + "-f", "rawvideo", "-", + ]) + .output() + .expect("ffmpeg extract yuv420p10le"); + if !out.status.success() { + return Vec::new(); + } + out.stdout +} + // --------------------------------------------------------------------------- // av1: encode only -- same shape as h265. // ---------------------------------------------------------------------------