diff --git a/Cargo.lock b/Cargo.lock index 769f0cb4..c535ff8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -285,12 +285,14 @@ dependencies = [ "ec-h264", "ec-h264-syntax", "ec-h265", + "ec-hw", "ec-image", "ec-inflate", "ec-mp3", "ec-opus", "ec-probe", "ec-truehd", + "ec-va", "ec-vorbis", ] diff --git a/crates/ec-hw/src/enc/mod.rs b/crates/ec-hw/src/enc/mod.rs index d862dc22..c0bc0fbf 100644 --- a/crates/ec-hw/src/enc/mod.rs +++ b/crates/ec-hw/src/enc/mod.rs @@ -1,4 +1,4 @@ -//! Hardware encoding: H.264, HEVC, and AV1 behind an explicit opt-in. +//! Hardware encoding: H.264, HEVC and AV1. //! //! One frame in, one coded frame out. The GOP is IDR-then-P with a single //! reference, which is what this GPU advertises (`VAConfigAttribEncMaxRefFrames` @@ -7,11 +7,15 @@ //! //! # AV1 //! -//! AV1 encoding is off unless [`EncoderConfig::allow_av1`] is set, and never -//! reachable from an "auto" codec choice. The reason is on the record rather -//! than theoretical: an AV1 encode submission on this driver generation took -//! the GPU down hard enough to need a reset. The path is built, typed and -//! probed; turning it on is a caller's explicit decision. +//! AV1 encode was opt-in until 2026-09-07, after a submission on an older +//! driver took the GPU down. It is on by default now because the refusal was +//! retired by measurement, not by opinion: on mesa 26.1.8 (gfx1200) ffmpeg's +//! own `av1_vaapi` encodes, this path encodes, and `gpu.rs`'s +//! `av1_encode_agrees_between_two_decoders` proves every coded frame decodes +//! identically through `ec-av1` and libdav1d. Unlike H.264 and HEVC, the +//! 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. use std::sync::Arc; @@ -42,7 +46,7 @@ pub enum EncCodec { H264, /// H.265 / HEVC, Main profile. H265, - /// AV1 Profile 0 — opt-in only, see the module docs. + /// AV1 Profile 0. Av1, } @@ -95,8 +99,6 @@ pub struct EncoderConfig { pub rate_control: RateControlMode, /// Driver quality/speed level, 1 (best quality) upwards; 0 = driver default. pub quality: u32, - /// Permit AV1 encoding. Without it, [`EncCodec::Av1`] is refused. - pub allow_av1: bool, /// The colour description to write into the VUI, or `None` to leave it /// unsignalled (`video_signal_type_present_flag = 0`) as before. pub colour: Option, @@ -132,7 +134,6 @@ impl EncoderConfig { gop_size: 60, rate_control: RateControlMode::ConstantBitrate, quality: 0, - allow_av1: false, colour: None, } } @@ -199,12 +200,6 @@ pub struct Encoder { impl Encoder { /// Build an encoder for `config` on `display`. pub fn new(display: &Arc, config: EncoderConfig) -> Result { - if config.codec == EncCodec::Av1 && !config.allow_av1 { - return Err(Error::unsupported( - "AV1 hardware encoding", - "it is opt-in (EncoderConfig::allow_av1) after a GPU recovery incident", - )); - } let profile = match config.codec { // High is the profile every H.264 encoder on this driver reports and // the one a High-profile decoder expects; Main is a strict subset. diff --git a/crates/ec-hw/src/lib.rs b/crates/ec-hw/src/lib.rs index 4087024e..5beceec5 100644 --- a/crates/ec-hw/src/lib.rs +++ b/crates/ec-hw/src/lib.rs @@ -30,7 +30,7 @@ //! * [`params`] — the `#[repr(C)]` codec parameter buffers, each checked //! against the system headers by a `const` assertion. //! * [`dec`] — one stateless decoder per codec behind [`Decoder`]. -//! * [`enc`] — H.264 and HEVC encoders, and an opt-in AV1 one. +//! * [`enc`] — H.264, HEVC and AV1 encoders. //! * [`SurfacePool`] / [`Frame`] — surface recycling and pixel access. //! //! # Unsafe diff --git a/crates/ec-hw/tests/gpu.rs b/crates/ec-hw/tests/gpu.rs index b5987c11..7201917c 100644 --- a/crates/ec-hw/tests/gpu.rs +++ b/crates/ec-hw/tests/gpu.rs @@ -532,13 +532,11 @@ fn encode_stream( width: u32, height: u32, frames: u32, - allow_av1: bool, ) -> Result, ec_hw::Error> { let mut config = EncoderConfig::new(codec, width, height); config.gop_size = 30; config.framerate = (30, 1); config.rate_control = RateControlMode::ConstantQp { qp: 26 }; - config.allow_av1 = allow_av1; let mut encoder = Encoder::new(display, config)?; let mut out = Vec::new(); for t in 0..frames { @@ -559,7 +557,7 @@ fn encode_stream( fn round_trip(codec: EncCodec, width: u32, height: u32, min_db: f64) { let Some(display) = display() else { return }; let frames = 12u32; - let stream = match encode_stream(&display, codec, width, height, frames, false) { + let stream = match encode_stream(&display, codec, width, height, frames) { Ok(stream) => stream, Err(e) => panic!("{codec:?} encode failed: {e}"), }; @@ -649,7 +647,7 @@ fn hevc_encode_honours_the_conformance_window() { fn our_encoder_and_our_decoder_agree() { let Some(display) = display() else { return }; for (enc, dec) in [(EncCodec::H264, Codec::H264), (EncCodec::H265, Codec::H265)] { - let stream = encode_stream(&display, enc, 640, 480, 12, false).expect("encode"); + let stream = encode_stream(&display, enc, 640, 480, 12).expect("encode"); let mut decoder = Decoder::new(&display, dec).expect("decoder opens"); let mut worst = f64::INFINITY; let mut count = 0u32; @@ -863,7 +861,7 @@ fn av1_encode_agrees_between_two_decoders() { let dir = std::env::temp_dir().join("ec-hw-av1-encode"); let _ = std::fs::create_dir_all(&dir); for (width, height, frames) in [(640u32, 384u32, 10u32), (1920, 1080, 10)] { - let data = match encode_stream(&display, EncCodec::Av1, width, height, frames, true) { + let data = match encode_stream(&display, EncCodec::Av1, width, height, frames) { Ok(data) => data, Err(e) => { eprintln!("skipped: AV1 encode unavailable ({e})"); @@ -929,7 +927,7 @@ fn av1_encode_speed() { for (width, height) in [(1920u32, 1080u32), (3840, 1608)] { let frames = 30u32; let start = Instant::now(); - match encode_stream(&display, EncCodec::Av1, width, height, frames, true) { + match encode_stream(&display, EncCodec::Av1, width, height, frames) { Ok(data) => { let secs = start.elapsed().as_secs_f64(); println!( diff --git a/tools/ec-bench/Cargo.toml b/tools/ec-bench/Cargo.toml index b6193d2c..f1cffa3b 100644 --- a/tools/ec-bench/Cargo.toml +++ b/tools/ec-bench/Cargo.toml @@ -18,6 +18,8 @@ ec-core = { workspace = true } ec-h264 = { workspace = true } ec-h265 = { workspace = true } ec-av1 = { workspace = true } +ec-hw = { workspace = true } +ec-va = { workspace = true } ec-probe = { workspace = true } ec-h264-syntax = { workspace = true } ec-image = { workspace = true } diff --git a/tools/ec-bench/src/main.rs b/tools/ec-bench/src/main.rs index 6260b649..e2d2d862 100644 --- a/tools/ec-bench/src/main.rs +++ b/tools/ec-bench/src/main.rs @@ -42,6 +42,7 @@ fn main() { bench_h264(&mut rows); bench_h265_encode(&mut rows); bench_av1_encode(&mut rows); + bench_hw_av1_encode(&mut rows); bench_audio_decode(&mut rows); bench_audio_encode(&mut rows); bench_truehd(&mut rows); @@ -328,6 +329,77 @@ fn bench_h265_encode(rows: &mut Vec) { }); } +// --------------------------------------------------------------------------- +// hardware av1: the GPU encoder, at the size his library is in. +// --------------------------------------------------------------------------- + +/// AV1 encode on the GPU (`ec-hw`, VA-API). Skips as a row rather than a +/// failure when there is no VA device or the driver has no AV1 encode +/// entrypoint -- a machine without one is not a slow machine. +fn bench_hw_av1_encode(rows: &mut Vec) { + let Some(src) = real_video("h264").or_else(|| real_video("hevc")) else { + rows.push(missing("ec-hw av1", "encode")); + return; + }; + let display = match ec_va::Display::open() { + Ok(display) => display, + Err(e) => { + rows.push(missing_direction("ec-hw av1", "encode", &format!("no VA display ({e})"))); + return; + } + }; + let (planes, w, h, n) = extract_yuv420p(&src, 1920, 1080, 30); + if n == 0 { + rows.push(missing("ec-hw av1", "encode")); + return; + } + let mut cfg = ec_hw::EncoderConfig::new(ec_hw::EncCodec::Av1, w, h); + 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", "encode", &format!("driver refused ({e})"))); + return; + } + }; + let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); + let frame_len = (w * h + 2 * cw * ch) as usize; + let start = Instant::now(); + let mut coded = 0usize; + for (i, frame) in planes.chunks_exact(frame_len).enumerate() { + let (y, rest) = frame.split_at((w * h) as usize); + let (u, v) = rest.split_at(rest.len() / 2); + let picture = ec_hw::I420 { + y: y.to_vec(), + u: u.to_vec(), + v: v.to_vec(), + width: w, + height: h, + }; + let meta = ec_hw::FrameMetadata { + timestamp: i as i64, + force_keyframe: false, + }; + match enc.encode(&picture, meta) { + Ok(out) => coded += out.data.len(), + Err(e) => { + rows.push(missing_direction("ec-hw av1", "encode", &format!("encode failed ({e})"))); + return; + } + } + } + let wall = start.elapsed().as_secs_f64(); + let media_s = n as f64 / 30.0; + rows.push(Row { + component: "ec-hw av1", + direction: "encode", + content: format!("{w}x{h}, {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), + }); +} + // --------------------------------------------------------------------------- // av1: encode only -- same shape as h265. // ---------------------------------------------------------------------------