10-bit BD row, 10-bit fps, ec-bench 10-bit row, docs
Browse files at this commitebea85eca0c7e63ced998b046f5a2b70d6d7dcd5Parent: 5fcd26f- feat
- 10-bit BD row, 10-bit fps, ec-bench 10-bit row, docs
4 changed files +235 -0
- crates/ec-hw/src/enc/mod.rs +14 -0
- crates/ec-hw/src/lib.rs +6 -0
- crates/ec-hw/tests/gpu.rs +124 -0
- tools/ec-bench/src/main.rs +91 -0
crates/ec-hw/src/enc/mod.rsmodified+14 -0
| @@ -16,6 +16,20 @@ | |||
| 16 | //! driver composes no headers of its own from thin air: it parses 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`) | 17 | //! sequence and frame header OBUs this crate packs (see `enc::headers::av1`) |
| 18 | //! and writes the stream's headers from them. | 18 | //! and writes the stream's headers from them. |
| 19 | +//! | ||
| 20 | +//! AV1 encodes at 8 or 10 bits ([`EncoderConfig::bit_depth`]); 10-bit is the | ||
| 21 | +//! depth his own library is stored at, and `gpu.rs`'s | ||
| 22 | +//! `av1_10bit_encode_agrees_between_two_decoders` proves a 10-bit stream out | ||
| 23 | +//! of this path decodes identically through `ec-av1` and libdav1d with | ||
| 24 | +//! ffprobe reading `yuv420p10le` out of the sequence header. | ||
| 25 | +//! | ||
| 26 | +//! # Rate control | ||
| 27 | +//! | ||
| 28 | +//! [`RateControlMode`] is not advisory: it is set as | ||
| 29 | +//! `VAConfigAttribRateControl` when the VA config is created, so an encoder | ||
| 30 | +//! built for CQP ignores a bitrate handed to it later. CQP, CBR, VBR and QVBR | ||
| 31 | +//! are all measured by `gpu.rs`'s `av1_encode_rate_control_lands`, which | ||
| 32 | +//! prints what each mode delivered against what it was asked for. | ||
| 19 | 33 | ||
| 20 | use std::sync::Arc; | 34 | use std::sync::Arc; |
| 21 | 35 | ||
crates/ec-hw/src/lib.rsmodified+6 -0
| @@ -25,6 +25,12 @@ | |||
| 25 | //! call [`Frame::to_i420_16`] instead — that is the lossless path that keeps | 25 | //! call [`Frame::to_i420_16`] instead — that is the lossless path that keeps |
| 26 | //! all ten bits. | 26 | //! all ten bits. |
| 27 | //! | 27 | //! |
| 28 | +//! Encoding is 10-bit too, for AV1: `EncoderConfig::ten_bit()` allocates P010 | ||
| 29 | +//! surfaces and takes its pixels through [`Encoder::encode_16`] (or straight | ||
| 30 | +//! from a 10-bit [`Frame`], with no conversion at all). H.264 and H.265 stay | ||
| 31 | +//! 8-bit here, because High and Main — the profiles this crate configures — | ||
| 32 | +//! are 8-bit profiles. | ||
| 33 | +//! | ||
| 28 | //! # Shape | 34 | //! # Shape |
| 29 | //! | 35 | //! |
| 30 | //! * [`params`] — the `#[repr(C)]` codec parameter buffers, each checked | 36 | //! * [`params`] — the `#[repr(C)]` codec parameter buffers, each checked |
crates/ec-hw/tests/gpu.rsmodified+124 -0
| @@ -1324,6 +1324,114 @@ fn ffmpeg_psnr_at(coded: &Path, source: &Path, fps: u32) -> Option<f64> { | |||
| 1324 | field.split_whitespace().next()?.parse::<f64>().ok().filter(|v| v.is_finite()) | 1324 | field.split_whitespace().next()?.parse::<f64>().ok().filter(|v| v.is_finite()) |
| 1325 | } | 1325 | } |
| 1326 | 1326 | ||
| 1327 | +/// What the GPU's 10-bit AV1 costs against 10-bit software, on his own film. | ||
| 1328 | +/// | ||
| 1329 | +/// The same shape as the 8-bit row, at film B's native 1920x1024 crop and | ||
| 1330 | +/// entirely in the 10-bit domain: no arm ever sees an 8-bit sample, so the | ||
| 1331 | +/// comparison is encoder against encoder rather than depth against depth. | ||
| 1332 | +/// Skips when the manifest has no 10-bit film -- a synthetic gradient would | ||
| 1333 | +/// be a BD-rate number about a gradient. | ||
| 1334 | +#[test] | ||
| 1335 | +fn av1_10bit_bd_row_vs_software() { | ||
| 1336 | + let Some(display) = display() else { return }; | ||
| 1337 | + let dir = std::env::temp_dir().join("ec-hw-av1-10bit-bd"); | ||
| 1338 | + let _ = std::fs::create_dir_all(&dir); | ||
| 1339 | + let frames = 12u32; | ||
| 1340 | + let Some((label, y4m, planes)) = ten_bit_source(&dir, frames) else { | ||
| 1341 | + eprintln!("skipped: no 10-bit source could be built"); | ||
| 1342 | + return; | ||
| 1343 | + }; | ||
| 1344 | + if !label.starts_with("film") { | ||
| 1345 | + eprintln!("skipped: no 10-bit film in the real-library manifest ({label})"); | ||
| 1346 | + return; | ||
| 1347 | + } | ||
| 1348 | + let (width, height) = (planes[0].width, planes[0].height); | ||
| 1349 | + let coded = planes.len() as u32; | ||
| 1350 | + | ||
| 1351 | + let mut ours = Vec::new(); | ||
| 1352 | + for qp in [180u32, 140, 100, 60] { | ||
| 1353 | + let data = match encode_stream_16( | ||
| 1354 | + &display, | ||
| 1355 | + &planes, | ||
| 1356 | + qp, | ||
| 1357 | + RateControlMode::ConstantQp { qp }, | ||
| 1358 | + ) { | ||
| 1359 | + Ok(data) => data, | ||
| 1360 | + Err(e) => { | ||
| 1361 | + eprintln!("skipped: 10-bit AV1 encode unavailable ({e})"); | ||
| 1362 | + return; | ||
| 1363 | + } | ||
| 1364 | + }; | ||
| 1365 | + let path = dir.join(format!("hw10-{qp}.obu")); | ||
| 1366 | + std::fs::write(&path, &data).expect("write"); | ||
| 1367 | + if let Some(db) = ffmpeg_psnr(&path, &y4m, true) { | ||
| 1368 | + ours.push((data.len() as f64 * 8.0 / f64::from(coded), db)); | ||
| 1369 | + } | ||
| 1370 | + } | ||
| 1371 | + let rav1e: Vec<(f64, f64)> = [180u32, 140, 100, 60] | ||
| 1372 | + .iter() | ||
| 1373 | + .filter_map(|q| { | ||
| 1374 | + software_point( | ||
| 1375 | + &dir, | ||
| 1376 | + &y4m, | ||
| 1377 | + "film-B-10bit", | ||
| 1378 | + "librav1e", | ||
| 1379 | + &[ | ||
| 1380 | + "-pix_fmt", "yuv420p10le", | ||
| 1381 | + "-rav1e-params", &format!("speed=6:quantizer={q}"), | ||
| 1382 | + "-g", "60", | ||
| 1383 | + ], | ||
| 1384 | + *q, | ||
| 1385 | + coded, | ||
| 1386 | + ) | ||
| 1387 | + }) | ||
| 1388 | + .collect(); | ||
| 1389 | + let libaom: Vec<(f64, f64)> = [56u32, 44, 32, 20] | ||
| 1390 | + .iter() | ||
| 1391 | + .filter_map(|crf| { | ||
| 1392 | + software_point( | ||
| 1393 | + &dir, | ||
| 1394 | + &y4m, | ||
| 1395 | + "film-B-10bit", | ||
| 1396 | + "libaom-av1", | ||
| 1397 | + &[ | ||
| 1398 | + "-pix_fmt", "yuv420p10le", | ||
| 1399 | + "-cpu-used", "6", "-crf", &crf.to_string(), "-b:v", "0", "-g", "60", | ||
| 1400 | + ], | ||
| 1401 | + *crf, | ||
| 1402 | + coded, | ||
| 1403 | + ) | ||
| 1404 | + }) | ||
| 1405 | + .collect(); | ||
| 1406 | + | ||
| 1407 | + println!("10-bit BD row: {label}, {width}x{height} yuv420p10le, {coded} frames"); | ||
| 1408 | + for (name, ladder) in [ | ||
| 1409 | + ("hw AV1 10-bit", &ours), | ||
| 1410 | + ("librav1e 10-bit", &rav1e), | ||
| 1411 | + ("libaom 10-bit", &libaom), | ||
| 1412 | + ] { | ||
| 1413 | + println!(" {name}: {ladder:?}"); | ||
| 1414 | + for pair in ladder.windows(2) { | ||
| 1415 | + assert!( | ||
| 1416 | + pair[1].0 > pair[0].0 && pair[1].1 > pair[0].1, | ||
| 1417 | + "{name}: ladder is not monotone at {pair:?}" | ||
| 1418 | + ); | ||
| 1419 | + } | ||
| 1420 | + } | ||
| 1421 | + if ours.len() >= 3 && rav1e.len() >= 3 { | ||
| 1422 | + println!( | ||
| 1423 | + " BD-rate: hw AV1 10-bit vs librav1e speed 6 10-bit: {:+.1}%", | ||
| 1424 | + bd_rate(&rav1e, &ours) * 100.0 | ||
| 1425 | + ); | ||
| 1426 | + } | ||
| 1427 | + if ours.len() >= 3 && libaom.len() >= 3 { | ||
| 1428 | + println!( | ||
| 1429 | + " BD-rate: hw AV1 10-bit vs libaom cpu-used 6 10-bit: {:+.1}%", | ||
| 1430 | + bd_rate(&libaom, &ours) * 100.0 | ||
| 1431 | + ); | ||
| 1432 | + } | ||
| 1433 | +} | ||
| 1434 | + | ||
| 1327 | /// What the GPU's AV1 costs against software, on his own content. | 1435 | /// What the GPU's AV1 costs against software, on his own content. |
| 1328 | /// | 1436 | /// |
| 1329 | /// Four quantisers per encoder on a 1920x1080 crop of a film and of a screen | 1437 | /// Four quantisers per encoder on a 1920x1080 crop of a film and of a screen |
| @@ -1653,6 +1761,22 @@ fn av1_encode_speed() { | |||
| 1653 | } | 1761 | } |
| 1654 | } | 1762 | } |
| 1655 | } | 1763 | } |
| 1764 | + // 10-bit at the size his own films are in: the depth his library is | ||
| 1765 | + // actually stored at, so the fps that matters for a real export. | ||
| 1766 | + let planes: Vec<I420_16> = (0..30).map(|t| source_frame_16(3840, 1608, t)).collect(); | ||
| 1767 | + let start = Instant::now(); | ||
| 1768 | + match encode_stream_16(&display, &planes, 90, RateControlMode::ConstantQp { qp: 90 }) { | ||
| 1769 | + Ok(data) => { | ||
| 1770 | + let secs = start.elapsed().as_secs_f64(); | ||
| 1771 | + println!( | ||
| 1772 | + "AV1 encode 3840x1608 10-bit: {:.1} fps ({} frames in {secs:.2} s), {} kbit/frame", | ||
| 1773 | + planes.len() as f64 / secs, | ||
| 1774 | + planes.len(), | ||
| 1775 | + data.len() * 8 / planes.len() / 1000 | ||
| 1776 | + ); | ||
| 1777 | + } | ||
| 1778 | + Err(e) => eprintln!("skipped: 10-bit AV1 encode unavailable ({e})"), | ||
| 1779 | + } | ||
| 1656 | } | 1780 | } |
| 1657 | 1781 | ||
| 1658 | // --------------------------------------------------------------------------- | 1782 | // --------------------------------------------------------------------------- |
tools/ec-bench/src/main.rsmodified+91 -0
| @@ -43,6 +43,7 @@ fn main() { | |||
| 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_hw_av1_encode(&mut rows); |
| 46 | + bench_hw_av1_encode_10bit(&mut rows); | ||
| 46 | bench_audio_decode(&mut rows); | 47 | bench_audio_decode(&mut rows); |
| 47 | bench_audio_encode(&mut rows); | 48 | bench_audio_encode(&mut rows); |
| 48 | bench_truehd(&mut rows); | 49 | bench_truehd(&mut rows); |
| @@ -400,6 +401,96 @@ fn bench_hw_av1_encode(rows: &mut Vec<Row>) { | |||
| 400 | }); | 401 | }); |
| 401 | } | 402 | } |
| 402 | 403 | ||
| 404 | +/// The same row at 10 bits, which is the depth his own AV1 library is stored | ||
| 405 | +/// at (`yuv420p10le`, both films). Skips as a row when there is no VA device, | ||
| 406 | +/// no AV1 encode entrypoint, or no source to read. | ||
| 407 | +fn bench_hw_av1_encode_10bit(rows: &mut Vec<Row>) { | ||
| 408 | + let Some(src) = real_video("av1").or_else(|| real_video("hevc")).or_else(|| real_video("h264")) | ||
| 409 | + else { | ||
| 410 | + rows.push(missing("ec-hw av1 10-bit", "encode")); | ||
| 411 | + return; | ||
| 412 | + }; | ||
| 413 | + let display = match ec_va::Display::open() { | ||
| 414 | + Ok(display) => display, | ||
| 415 | + Err(e) => { | ||
| 416 | + rows.push(missing_direction("ec-hw av1 10-bit", "encode", &format!("no VA display ({e})"))); | ||
| 417 | + return; | ||
| 418 | + } | ||
| 419 | + }; | ||
| 420 | + let (w, h) = (1920u32, 1080u32); | ||
| 421 | + let raw = extract_yuv420p10(&src, w, h, 30); | ||
| 422 | + let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); | ||
| 423 | + let frame_len = ((w * h + 2 * cw * ch) * 2) as usize; | ||
| 424 | + let n = (raw.len() / frame_len) as u32; | ||
| 425 | + if n == 0 { | ||
| 426 | + rows.push(missing("ec-hw av1 10-bit", "encode")); | ||
| 427 | + return; | ||
| 428 | + } | ||
| 429 | + let mut cfg = ec_hw::EncoderConfig::new(ec_hw::EncCodec::Av1, w, h).ten_bit(); | ||
| 430 | + cfg.rate_control = ec_hw::RateControlMode::ConstantQp { qp: 120 }; | ||
| 431 | + let mut enc = match ec_hw::Encoder::new(&display, cfg) { | ||
| 432 | + Ok(enc) => enc, | ||
| 433 | + Err(e) => { | ||
| 434 | + rows.push(missing_direction("ec-hw av1 10-bit", "encode", &format!("driver refused ({e})"))); | ||
| 435 | + return; | ||
| 436 | + } | ||
| 437 | + }; | ||
| 438 | + let read = |s: &[u8]| -> Vec<u16> { | ||
| 439 | + s.chunks_exact(2).map(|p| u16::from_le_bytes([p[0], p[1]])).collect() | ||
| 440 | + }; | ||
| 441 | + let start = Instant::now(); | ||
| 442 | + let mut coded = 0usize; | ||
| 443 | + for (i, frame) in raw.chunks_exact(frame_len).enumerate() { | ||
| 444 | + let (y, rest) = frame.split_at((w * h * 2) as usize); | ||
| 445 | + let (u, v) = rest.split_at(rest.len() / 2); | ||
| 446 | + let picture = ec_hw::I420_16 { | ||
| 447 | + y: read(y), | ||
| 448 | + u: read(u), | ||
| 449 | + v: read(v), | ||
| 450 | + width: w, | ||
| 451 | + height: h, | ||
| 452 | + bit_depth: 10, | ||
| 453 | + }; | ||
| 454 | + let meta = ec_hw::FrameMetadata { timestamp: i as i64, force_keyframe: false }; | ||
| 455 | + match enc.encode_16(&picture, meta) { | ||
| 456 | + Ok(out) => coded += out.data.len(), | ||
| 457 | + Err(e) => { | ||
| 458 | + rows.push(missing_direction("ec-hw av1 10-bit", "encode", &format!("encode failed ({e})"))); | ||
| 459 | + return; | ||
| 460 | + } | ||
| 461 | + } | ||
| 462 | + } | ||
| 463 | + let wall = start.elapsed().as_secs_f64(); | ||
| 464 | + let media_s = f64::from(n) / 30.0; | ||
| 465 | + rows.push(Row { | ||
| 466 | + component: "ec-hw av1 10-bit", | ||
| 467 | + direction: "encode", | ||
| 468 | + content: format!("{w}x{h} yuv420p10le, {n} frames, CQP 120, {} kB", coded / 1000), | ||
| 469 | + media: format!("{media_s:.1}s"), | ||
| 470 | + wall_ms: wall * 1000.0, | ||
| 471 | + rtf: (wall > 0.0).then_some(media_s / wall), | ||
| 472 | + }); | ||
| 473 | +} | ||
| 474 | + | ||
| 475 | +/// `extract_yuv420p` at ten bits: P010-ordered planar 16-bit little endian. | ||
| 476 | +fn extract_yuv420p10(src: &Path, w: u32, h: u32, n: u32) -> Vec<u8> { | ||
| 477 | + let out = Command::new("ffmpeg") | ||
| 478 | + .args(["-v", "error", "-i"]) | ||
| 479 | + .arg(src) | ||
| 480 | + .args([ | ||
| 481 | + "-vf", &format!("scale={w}:{h}"), | ||
| 482 | + "-frames:v", &n.to_string(), | ||
| 483 | + "-pix_fmt", "yuv420p10le", | ||
| 484 | + "-f", "rawvideo", "-", | ||
| 485 | + ]) | ||
| 486 | + .output() | ||
| 487 | + .expect("ffmpeg extract yuv420p10le"); | ||
| 488 | + if !out.status.success() { | ||
| 489 | + return Vec::new(); | ||
| 490 | + } | ||
| 491 | + out.stdout | ||
| 492 | +} | ||
| 493 | + | ||
| 403 | // --------------------------------------------------------------------------- | 494 | // --------------------------------------------------------------------------- |
| 404 | // av1: encode only -- same shape as h265. | 495 | // av1: encode only -- same shape as h265. |
| 405 | // --------------------------------------------------------------------------- | 496 | // --------------------------------------------------------------------------- |