forge
Explore
Log in

Don't you dare go Hollow

AV1 encode on by default, and an ec-bench hardware row

Browse files at this commit

24f4e5c7860d97712f5699d4360d4ed94d919144Ahmet Kaan GümüşParent: a518304
  • feat
    • AV1 encode on by default, and an ec-bench hardware row
Show full message
feat(ec-hw): AV1 encode on by default, and an ec-bench hardware row

The opt-in existed because a 2026-08-14 submission crashed the GPU; on
mesa 26.1.8 the path encodes streams two decoders agree on, so the
refusal goes rather than outliving its evidence. ec-bench gains an
'ec-hw av1 encode' row that skips as a row when there is no VA device.
Cargo.lockmodified+2 -0
@@ -285,12 +285,14 @@ dependencies = [
285 "ec-h264",285 "ec-h264",
286 "ec-h264-syntax",286 "ec-h264-syntax",
287 "ec-h265",287 "ec-h265",
288+ "ec-hw",
288 "ec-image",289 "ec-image",
289 "ec-inflate",290 "ec-inflate",
290 "ec-mp3",291 "ec-mp3",
291 "ec-opus",292 "ec-opus",
292 "ec-probe",293 "ec-probe",
293 "ec-truehd",294 "ec-truehd",
295+ "ec-va",
294 "ec-vorbis",296 "ec-vorbis",
295 ]297 ]
296 298
crates/ec-hw/src/enc/mod.rsmodified+11 -16
@@ -1,4 +1,4 @@
1-//! Hardware encoding: H.264, HEVC, and AV1 behind an explicit opt-in.1+//! Hardware encoding: H.264, HEVC and AV1.
2 //!2 //!
3 //! One frame in, one coded frame out. The GOP is IDR-then-P with a single3 //! One frame in, one coded frame out. The GOP is IDR-then-P with a single
4 //! reference, which is what this GPU advertises (`VAConfigAttribEncMaxRefFrames`4 //! reference, which is what this GPU advertises (`VAConfigAttribEncMaxRefFrames`
@@ -7,11 +7,15 @@
7 //!7 //!
8 //! # AV18 //! # AV1
9 //!9 //!
10-//! AV1 encoding is off unless [`EncoderConfig::allow_av1`] is set, and never10+//! AV1 encode was opt-in until 2026-09-07, after a submission on an older
11-//! reachable from an "auto" codec choice. The reason is on the record rather11+//! driver took the GPU down. It is on by default now because the refusal was
12-//! than theoretical: an AV1 encode submission on this driver generation took12+//! retired by measurement, not by opinion: on mesa 26.1.8 (gfx1200) ffmpeg's
13-//! the GPU down hard enough to need a reset. The path is built, typed and13+//! own `av1_vaapi` encodes, this path encodes, and `gpu.rs`'s
14-//! probed; turning it on is a caller's explicit decision.14+//! `av1_encode_agrees_between_two_decoders` proves every coded frame decodes
15+//! identically through `ec-av1` and libdav1d. Unlike H.264 and HEVC, the
16+//! driver composes no headers of its own from thin air: it parses the
17+//! sequence and frame header OBUs this crate packs (see `enc::headers::av1`)
18+//! and writes the stream's headers from them.
15 19
16 use std::sync::Arc;20 use std::sync::Arc;
17 21
@@ -42,7 +46,7 @@ pub enum EncCodec {
42 H264,46 H264,
43 /// H.265 / HEVC, Main profile.47 /// H.265 / HEVC, Main profile.
44 H265,48 H265,
45- /// AV1 Profile 0 — opt-in only, see the module docs.49+ /// AV1 Profile 0.
46 Av1,50 Av1,
47 }51 }
48 52
@@ -95,8 +99,6 @@ pub struct EncoderConfig {
95 pub rate_control: RateControlMode,99 pub rate_control: RateControlMode,
96 /// Driver quality/speed level, 1 (best quality) upwards; 0 = driver default.100 /// Driver quality/speed level, 1 (best quality) upwards; 0 = driver default.
97 pub quality: u32,101 pub quality: u32,
98- /// Permit AV1 encoding. Without it, [`EncCodec::Av1`] is refused.
99- pub allow_av1: bool,
100 /// The colour description to write into the VUI, or `None` to leave it102 /// The colour description to write into the VUI, or `None` to leave it
101 /// unsignalled (`video_signal_type_present_flag = 0`) as before.103 /// unsignalled (`video_signal_type_present_flag = 0`) as before.
102 pub colour: Option<Colour>,104 pub colour: Option<Colour>,
@@ -132,7 +134,6 @@ impl EncoderConfig {
132 gop_size: 60,134 gop_size: 60,
133 rate_control: RateControlMode::ConstantBitrate,135 rate_control: RateControlMode::ConstantBitrate,
134 quality: 0,136 quality: 0,
135- allow_av1: false,
136 colour: None,137 colour: None,
137 }138 }
138 }139 }
@@ -199,12 +200,6 @@ pub struct Encoder {
199 impl Encoder {200 impl Encoder {
200 /// Build an encoder for `config` on `display`.201 /// Build an encoder for `config` on `display`.
201 pub fn new(display: &Arc<Display>, config: EncoderConfig) -> Result<Encoder> {202 pub fn new(display: &Arc<Display>, config: EncoderConfig) -> Result<Encoder> {
202- if config.codec == EncCodec::Av1 && !config.allow_av1 {
203- return Err(Error::unsupported(
204- "AV1 hardware encoding",
205- "it is opt-in (EncoderConfig::allow_av1) after a GPU recovery incident",
206- ));
207- }
208 let profile = match config.codec {203 let profile = match config.codec {
209 // High is the profile every H.264 encoder on this driver reports and204 // High is the profile every H.264 encoder on this driver reports and
210 // the one a High-profile decoder expects; Main is a strict subset.205 // the one a High-profile decoder expects; Main is a strict subset.
crates/ec-hw/src/lib.rsmodified+1 -1
@@ -30,7 +30,7 @@
30 //! * [`params`] — the `#[repr(C)]` codec parameter buffers, each checked30 //! * [`params`] — the `#[repr(C)]` codec parameter buffers, each checked
31 //! against the system headers by a `const` assertion.31 //! against the system headers by a `const` assertion.
32 //! * [`dec`] — one stateless decoder per codec behind [`Decoder`].32 //! * [`dec`] — one stateless decoder per codec behind [`Decoder`].
33-//! * [`enc`] — H.264 and HEVC encoders, and an opt-in AV1 one.33+//! * [`enc`] — H.264, HEVC and AV1 encoders.
34 //! * [`SurfacePool`] / [`Frame`] — surface recycling and pixel access.34 //! * [`SurfacePool`] / [`Frame`] — surface recycling and pixel access.
35 //!35 //!
36 //! # Unsafe36 //! # Unsafe
crates/ec-hw/tests/gpu.rsmodified+4 -6
@@ -532,13 +532,11 @@ fn encode_stream(
532 width: u32,532 width: u32,
533 height: u32,533 height: u32,
534 frames: u32,534 frames: u32,
535- allow_av1: bool,
536 ) -> Result<Vec<u8>, ec_hw::Error> {535 ) -> Result<Vec<u8>, ec_hw::Error> {
537 let mut config = EncoderConfig::new(codec, width, height);536 let mut config = EncoderConfig::new(codec, width, height);
538 config.gop_size = 30;537 config.gop_size = 30;
539 config.framerate = (30, 1);538 config.framerate = (30, 1);
540 config.rate_control = RateControlMode::ConstantQp { qp: 26 };539 config.rate_control = RateControlMode::ConstantQp { qp: 26 };
541- config.allow_av1 = allow_av1;
542 let mut encoder = Encoder::new(display, config)?;540 let mut encoder = Encoder::new(display, config)?;
543 let mut out = Vec::new();541 let mut out = Vec::new();
544 for t in 0..frames {542 for t in 0..frames {
@@ -559,7 +557,7 @@ fn encode_stream(
559 fn round_trip(codec: EncCodec, width: u32, height: u32, min_db: f64) {557 fn round_trip(codec: EncCodec, width: u32, height: u32, min_db: f64) {
560 let Some(display) = display() else { return };558 let Some(display) = display() else { return };
561 let frames = 12u32;559 let frames = 12u32;
562- let stream = match encode_stream(&display, codec, width, height, frames, false) {560+ let stream = match encode_stream(&display, codec, width, height, frames) {
563 Ok(stream) => stream,561 Ok(stream) => stream,
564 Err(e) => panic!("{codec:?} encode failed: {e}"),562 Err(e) => panic!("{codec:?} encode failed: {e}"),
565 };563 };
@@ -649,7 +647,7 @@ fn hevc_encode_honours_the_conformance_window() {
649 fn our_encoder_and_our_decoder_agree() {647 fn our_encoder_and_our_decoder_agree() {
650 let Some(display) = display() else { return };648 let Some(display) = display() else { return };
651 for (enc, dec) in [(EncCodec::H264, Codec::H264), (EncCodec::H265, Codec::H265)] {649 for (enc, dec) in [(EncCodec::H264, Codec::H264), (EncCodec::H265, Codec::H265)] {
652- let stream = encode_stream(&display, enc, 640, 480, 12, false).expect("encode");650+ let stream = encode_stream(&display, enc, 640, 480, 12).expect("encode");
653 let mut decoder = Decoder::new(&display, dec).expect("decoder opens");651 let mut decoder = Decoder::new(&display, dec).expect("decoder opens");
654 let mut worst = f64::INFINITY;652 let mut worst = f64::INFINITY;
655 let mut count = 0u32;653 let mut count = 0u32;
@@ -863,7 +861,7 @@ fn av1_encode_agrees_between_two_decoders() {
863 let dir = std::env::temp_dir().join("ec-hw-av1-encode");861 let dir = std::env::temp_dir().join("ec-hw-av1-encode");
864 let _ = std::fs::create_dir_all(&dir);862 let _ = std::fs::create_dir_all(&dir);
865 for (width, height, frames) in [(640u32, 384u32, 10u32), (1920, 1080, 10)] {863 for (width, height, frames) in [(640u32, 384u32, 10u32), (1920, 1080, 10)] {
866- let data = match encode_stream(&display, EncCodec::Av1, width, height, frames, true) {864+ let data = match encode_stream(&display, EncCodec::Av1, width, height, frames) {
867 Ok(data) => data,865 Ok(data) => data,
868 Err(e) => {866 Err(e) => {
869 eprintln!("skipped: AV1 encode unavailable ({e})");867 eprintln!("skipped: AV1 encode unavailable ({e})");
@@ -929,7 +927,7 @@ fn av1_encode_speed() {
929 for (width, height) in [(1920u32, 1080u32), (3840, 1608)] {927 for (width, height) in [(1920u32, 1080u32), (3840, 1608)] {
930 let frames = 30u32;928 let frames = 30u32;
931 let start = Instant::now();929 let start = Instant::now();
932- match encode_stream(&display, EncCodec::Av1, width, height, frames, true) {930+ match encode_stream(&display, EncCodec::Av1, width, height, frames) {
933 Ok(data) => {931 Ok(data) => {
934 let secs = start.elapsed().as_secs_f64();932 let secs = start.elapsed().as_secs_f64();
935 println!(933 println!(
tools/ec-bench/Cargo.tomlmodified+2 -0
@@ -18,6 +18,8 @@ ec-core = { workspace = true }
18 ec-h264 = { workspace = true }18 ec-h264 = { workspace = true }
19 ec-h265 = { workspace = true }19 ec-h265 = { workspace = true }
20 ec-av1 = { workspace = true }20 ec-av1 = { workspace = true }
21+ec-hw = { workspace = true }
22+ec-va = { workspace = true }
21 ec-probe = { workspace = true }23 ec-probe = { workspace = true }
22 ec-h264-syntax = { workspace = true }24 ec-h264-syntax = { workspace = true }
23 ec-image = { workspace = true }25 ec-image = { workspace = true }
tools/ec-bench/src/main.rsmodified+72 -0
@@ -42,6 +42,7 @@ fn main() {
42 bench_h264(&mut rows);42 bench_h264(&mut rows);
43 bench_h265_encode(&mut rows);43 bench_h265_encode(&mut rows);
44 bench_av1_encode(&mut rows);44 bench_av1_encode(&mut rows);
45+ bench_hw_av1_encode(&mut rows);
45 bench_audio_decode(&mut rows);46 bench_audio_decode(&mut rows);
46 bench_audio_encode(&mut rows);47 bench_audio_encode(&mut rows);
47 bench_truehd(&mut rows);48 bench_truehd(&mut rows);
@@ -328,6 +329,77 @@ fn bench_h265_encode(rows: &mut Vec<Row>) {
328 });329 });
329 }330 }
330 331
332+// ---------------------------------------------------------------------------
333+// hardware av1: the GPU encoder, at the size his library is in.
334+// ---------------------------------------------------------------------------
335+
336+/// AV1 encode on the GPU (`ec-hw`, VA-API). Skips as a row rather than a
337+/// failure when there is no VA device or the driver has no AV1 encode
338+/// entrypoint -- a machine without one is not a slow machine.
339+fn bench_hw_av1_encode(rows: &mut Vec<Row>) {
340+ let Some(src) = real_video("h264").or_else(|| real_video("hevc")) else {
341+ rows.push(missing("ec-hw av1", "encode"));
342+ return;
343+ };
344+ let display = match ec_va::Display::open() {
345+ Ok(display) => display,
346+ Err(e) => {
347+ rows.push(missing_direction("ec-hw av1", "encode", &format!("no VA display ({e})")));
348+ return;
349+ }
350+ };
351+ let (planes, w, h, n) = extract_yuv420p(&src, 1920, 1080, 30);
352+ if n == 0 {
353+ rows.push(missing("ec-hw av1", "encode"));
354+ return;
355+ }
356+ let mut cfg = ec_hw::EncoderConfig::new(ec_hw::EncCodec::Av1, w, h);
357+ cfg.rate_control = ec_hw::RateControlMode::ConstantQp { qp: 120 };
358+ let mut enc = match ec_hw::Encoder::new(&display, cfg) {
359+ Ok(enc) => enc,
360+ Err(e) => {
361+ rows.push(missing_direction("ec-hw av1", "encode", &format!("driver refused ({e})")));
362+ return;
363+ }
364+ };
365+ let (cw, ch) = (w.div_ceil(2), h.div_ceil(2));
366+ let frame_len = (w * h + 2 * cw * ch) as usize;
367+ let start = Instant::now();
368+ let mut coded = 0usize;
369+ for (i, frame) in planes.chunks_exact(frame_len).enumerate() {
370+ let (y, rest) = frame.split_at((w * h) as usize);
371+ let (u, v) = rest.split_at(rest.len() / 2);
372+ let picture = ec_hw::I420 {
373+ y: y.to_vec(),
374+ u: u.to_vec(),
375+ v: v.to_vec(),
376+ width: w,
377+ height: h,
378+ };
379+ let meta = ec_hw::FrameMetadata {
380+ timestamp: i as i64,
381+ force_keyframe: false,
382+ };
383+ match enc.encode(&picture, meta) {
384+ Ok(out) => coded += out.data.len(),
385+ Err(e) => {
386+ rows.push(missing_direction("ec-hw av1", "encode", &format!("encode failed ({e})")));
387+ return;
388+ }
389+ }
390+ }
391+ let wall = start.elapsed().as_secs_f64();
392+ let media_s = n as f64 / 30.0;
393+ rows.push(Row {
394+ component: "ec-hw av1",
395+ direction: "encode",
396+ content: format!("{w}x{h}, {n} frames, CQP 120, {} kB", coded / 1000),
397+ media: format!("{media_s:.1}s"),
398+ wall_ms: wall * 1000.0,
399+ rtf: (wall > 0.0).then_some(media_s / wall),
400+ });
401+}
402+
331 // ---------------------------------------------------------------------------403 // ---------------------------------------------------------------------------
332 // av1: encode only -- same shape as h265.404 // av1: encode only -- same shape as h265.
333 // ---------------------------------------------------------------------------405 // ---------------------------------------------------------------------------