//! Hardware path checks. These need a built `libengine_hw.so` plus a working //! VA-API driver, so they are `#[ignore]`d by default. Run them with: //! //! ```text //! cargo build --workspace --release //! LD_LIBRARY_PATH=target/release \ //! cargo test -p engine --release --test hw_decode -- --ignored --nocapture --test-threads=1 //! ``` //! //! The comparison test pins [`DecodeSession`] to the software decoder itself //! (it sets `VE_SW=1` in-process, hence the required `--test-threads=1`); the //! hardware side goes through [`HwSession`] directly and can never silently be //! a fallback. use std::path::{Path, PathBuf}; use std::time::Instant; use engine::DecodeSession; use engine::colorspace::{ColorDescription, Matrix}; use engine::convert::i420_to_bgra; use engine::demux::{Codec, Demuxer}; use engine::mux::{self, Mp4Muxer, VideoParams}; use engine::scratch::Scratch; use engine::hw::HwSession; use engine::tonemap::{ToneMapper, Transfer}; fn asset(name: &str) -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")) .join("../../assets") .join(name) } /// What the decode funnel converts this file's planes in, so a conversion done /// by hand here is the one `DecodeSession` would have done. fn color_of(path: &Path) -> ColorDescription { Demuxer::open(path).expect("open for colour").0.color } fn open_hw(path: &Path) -> HwSession { HwSession::open(path).expect("no hardware decode plugin/driver available") } /// Decodes the whole file through the plugin, returning (frame count, frame 30 as BGRA). fn decode_all_hw(name: &str) -> (usize, Vec) { let path = asset(name); let color = color_of(&path); let mut hw = open_hw(&path); let meta = hw.meta().expect("meta"); assert_eq!( (meta.width, meta.height), (1280, 720), "{name} container dims" ); let mut count = 0usize; let mut frame_30 = Vec::new(); let start = Instant::now(); while let Some((y, u, v, w, h)) = hw.next_frame().expect("hardware decode") { assert_eq!((w, h), (1280, 720), "{name} frame {count} dims"); if count == 30 { frame_30 = i420_to_bgra(&color, y, u, v, w as usize, h as usize); } count += 1; } eprintln!( "{name}: {count} hardware frames in {:?} ({:?}/frame)", start.elapsed(), start.elapsed() / count.max(1) as u32 ); (count, frame_30) } /// The plugin must refuse junk by returning null, never by unwinding into us. #[test] #[ignore = "needs libengine_hw.so and a VA-API driver"] fn rejects_unopenable_files() { assert!(HwSession::open(Path::new("/definitely/not/here.mp4")).is_none()); assert!( HwSession::open(Path::new("/etc/hostname")).is_none(), "not an mp4" ); } /// 24 frames cut from a 2160p HDR remux that used to give a black picture and /// an instant `eof`: at 3840x2160 with CTB 64 the picture is 34 CTB rows, so /// wavefront slices carry `num_entry_point_offsets == 33` (§7.4.7.1 allows up /// to PicHeightInCtbsY - 1) and the vendored parser indexed a `[u32; 32]` with /// 32. Conformant stream, panicking parser -- caught at the plugin edge, which /// is exactly why it reached the user as a silent eof. #[test] #[ignore = "needs libengine_hw.so and a VA-API driver"] fn hardware_decodes_4k_wavefront_slices() { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/data/test_hevc_4k_wpp.mkv"); let mut hw = open_hw(&path); let mut count = 0usize; while let Some((_, _, _, w, h)) = hw.next_frame().expect("hardware decode") { assert_eq!((w, h), (3840, 2160), "frame {count} dims"); count += 1; } assert!(count > 0, "4K wavefront stream decoded no pictures"); } /// ...and the same file with its slice bytes scrambled -- a stream the plugin /// opens and then cannot decode a picture out of -- is refused at the door, in /// words that do not send the user installing a plugin they already have. The /// case this whole bug arrived as: never a black picture and a silent `eof`. #[test] #[ignore = "needs libengine_hw.so and a VA-API driver"] fn an_undecodable_hevc_stream_is_refused_by_name() { let good = std::fs::read( PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/data/test_hevc_4k_wpp.mkv"), ) .expect("read the 4K fixture"); // Every SPS in it (`nal_unit_type` 33, so the two header bytes are 42 01) // gets `sps_max_sub_layers_minus1 = 7`, which §7.4.3.2.1 does not allow and // the parser now refuses instead of indexing a 7-long array with 7. let mut broken = good.clone(); let mut patched = 0; for i in 0..broken.len() - 2 { if broken[i] == 0x42 && broken[i + 1] == 0x01 { broken[i + 2] |= 0x0e; patched += 1; } } assert!(patched > 0, "no SPS found in the fixture"); let path = Scratch::file("undecodable_hevc", "mkv"); std::fs::write(&path, &broken).expect("write the broken copy"); let refused = DecodeSession::open(&path) .expect_err("a stream with no decodable picture must not open") .to_string(); assert_eq!(refused, Codec::Hevc.undecodable()); } #[test] #[ignore = "needs libengine_hw.so and a VA-API driver"] fn hardware_decodes_baseline() { let (count, _) = decode_all_hw("test_baseline.mp4"); assert_eq!(count, 150); } #[test] #[ignore = "needs libengine_hw.so and a VA-API driver"] fn hardware_decodes_high_profile() { // B-frames: the plugin emits display order (DPB bumping happens inside // cros-codecs), so a clean run to EOF is the check we can make headlessly. let (count, _) = decode_all_hw("test_high.mp4"); assert_eq!(count, 150); } /// Seeking may only change *when* a picture arrives, never the picture: both /// backends must land on bytes identical to a linear decode of the same index. #[test] #[ignore = "needs libengine_hw.so and a VA-API driver"] fn seek_matches_linear() { const TARGET: u32 = 60; let path = asset("test_baseline.mp4"); // Software side first, pinned like the test above. SAFETY: --test-threads=1. unsafe { std::env::set_var("VE_SW", "1") }; let (_, rx) = DecodeSession::open(&path).expect("software open"); let linear = rx .into_iter() .find(|f| f.index == TARGET) .expect("software frame 60") .bgra; let start = Instant::now(); let (_, rx, _cancel) = DecodeSession::open_at(&path, TARGET).expect("software open_at"); let seeked = rx.into_iter().next().expect("no frame after software seek"); eprintln!("software seek to {TARGET} took {:?}", start.elapsed()); assert_eq!(seeked.index, TARGET, "first frame after seek is the target"); assert_eq!(seeked.bgra, linear, "software seek != software linear"); let start = Instant::now(); let mut hw = HwSession::open_at(&path, TARGET).expect("no plugin/driver available"); let (y, u, v, w, h) = hw .next_frame() .expect("hardware decode") .expect("no frame after hardware seek"); eprintln!("hardware seek to {TARGET} took {:?}", start.elapsed()); assert_eq!( i420_to_bgra(&color_of(&path), y, u, v, w as usize, h as usize), linear, "hardware seek != software linear" ); } /// The same promise as [`seek_matches_linear`], made of every container and /// codec pair there is a fixture for and at targets on, either side of and away /// from a keyframe -- because which of them holds is a property of the *stream's /// structure*, not of the seek code alone. /// /// An open-GOP HEVC stream (x265's default, so `test_hevc.mkv` and its 10-bit /// twin, and every film off a disc or a web rip) stores the CRA that opens a GOP /// *ahead of* the RASL leading pictures that are shown *before* it: keyframe 30 /// on screen is the 28th block in the file. A seek that counts stored positions /// rather than displayed pictures therefore hands back a picture two frames /// later than the index it labels, and by a different amount in a GOP with /// another number of leading pictures. `test_baseline.mp4` alone never showed /// it: it has exactly one keyframe, at frame 0, where the two orders agree. #[test] #[ignore = "needs libengine_hw.so and a VA-API driver"] fn seek_matches_linear_every_container() { // Sanitize at entry: sibling tests leak `VE_SW=1` by design and run before // this one alphabetically, and a leaked pin makes the plugin-only HEVC // fixtures refuse at open. The hardware path for these, including AV1's // fixture: the point is the seek against the same seat a linear decode ran // on, which the tests below pin for the software seat. SAFETY: // --test-threads=1, which the documented invocation keeps. unsafe { std::env::remove_var("VE_SW") }; for name in [ "test_baseline.mp4", "test_high.mp4", "test_hevc.mp4", "test_hevc.mkv", "test_hevc10.mkv", "test_h264.mkv", "test_av1.mkv", ] { let path = asset(name); let (meta, rx) = DecodeSession::open(&path).unwrap_or_else(|e| panic!("{name}: open: {e}")); // On a keyframe, either side of one, well inside a GOP, the first // picture and the last -- deduplicated for a fixture short enough that // some of those coincide. let mut targets = vec![0, 1, 29, 30, 31, 45, meta.frame_count - 1]; targets.retain(|&t| t < meta.frame_count); targets.sort_unstable(); targets.dedup(); let linear: Vec<(u32, Vec)> = rx .into_iter() .filter(|f| targets.contains(&f.index)) .map(|f| (f.index, f.bgra)) .collect(); assert_eq!( linear.iter().map(|(i, _)| *i).collect::>(), targets, "{name}: linear decode never delivered every target" ); for (target, want) in linear { let (_, rx, _cancel) = DecodeSession::open_at(&path, target) .unwrap_or_else(|e| panic!("{name}: open_at({target}): {e}")); let seeked = rx .into_iter() .next() .unwrap_or_else(|| panic!("{name}: no frame after seek to {target}")); assert_eq!( seeked.index, target, "{name}: first frame after seek is not the target" ); assert!( seeked.bgra == want, "{name}: seek to {target} handed back a different picture than a linear decode of {target}" ); } } } /// Drains a bounded session, returning the indices it delivered. The loop ends /// only on `RecvError`, i.e. the worker closed the channel by itself. fn range_indices(path: &Path, start: u32, end: u32) -> Vec { let (_, rx, _cancel) = DecodeSession::open_range(path, start, end).expect("open_range"); let mut got = Vec::new(); while let Ok(frame) = rx.recv() { got.push(frame.index); } got } /// A bounded range must stop on its own — both backends — with `Frame::index` /// still the absolute source index, and then disconnect rather than hang. #[test] #[ignore = "needs libengine_hw.so and a VA-API driver"] fn range_stops_at_end() { const START: u32 = 30; const END: u32 = 60; let path = asset("test_baseline.mp4"); let want: Vec = (START..END).collect(); // Another test in this binary may already have pinned VE_SW; clear it so // the hardware half really is hardware. SAFETY: --test-threads=1. unsafe { std::env::remove_var("VE_SW") }; assert_eq!(range_indices(&path, START, END), want, "hardware range"); unsafe { std::env::set_var("VE_SW", "1") }; assert_eq!(range_indices(&path, START, END), want, "software range"); // Degenerate ranges close cleanly instead of decoding to EOF. assert!(range_indices(&path, 30, 30).is_empty(), "empty range"); assert!(range_indices(&path, 60, 30).is_empty(), "inverted range"); // Past the end is clamped to the frame count. assert_eq!(range_indices(&path, 148, 999).len(), 2, "clamped range"); } /// The real thing: a 4K HDR10 film through the funnel, against the picture the /// same planes gave before the tone map was in it (BT.2020 matrix, HDR codes /// shown as if they were SDR -- flat and grey-washed, which is the complaint /// this whole path answers). Colour has to come *up*, not down, and the picture /// has to land somewhere a display can show rather than at either rail. /// /// Gated on the film being here: it is a 20 GB download, not a fixture. HEVC /// Main 10, so this is a hardware-only test in every sense. #[test] #[ignore = "needs libengine_hw.so, a VA-API driver and the film"] fn the_hdr_film_renders_tone_mapped() { // Two minutes in: past the studio logos, inside lit footage. const TARGET: u32 = 3000; let Some(path) = engine::real_library::film("hevc_4k_hdr") else { return; }; let path = path.as_path(); // SAFETY: --test-threads=1. There is no software HEVC decoder, so a pinned // VE_SW from another test in this binary would refuse the file outright. unsafe { std::env::remove_var("VE_SW") }; // The old picture, by hand off the plugin's own planes: the funnel did // exactly this before the tone map, and it is what the comparison needs. let mut hw = HwSession::open_at(path, TARGET).expect("no plugin/driver available"); let (y, u, v, w, h) = hw .next_frame() .expect("hardware decode") .expect("no frame at the seek"); let (w, h) = (w as usize, h as usize); let (y, u, v) = (y.to_vec(), u.to_vec(), v.to_vec()); let untouched = i420_to_bgra(&color_of(path), &y, &u, &v, w, h); // ...and the picture the engine shows now, through the funnel itself. let (_, rx, _cancel) = DecodeSession::open_at(path, TARGET).expect("open the film"); let shown = rx.recv().expect("a frame off the funnel").bgra; assert_eq!(shown.len(), untouched.len(), "frame size"); // Saturation, as the mean distance from grey per pixel... let spread = |bgra: &[u8]| -> f64 { let total: u64 = bgra .chunks_exact(4) .map(|px| u64::from(px[..3].iter().max().unwrap() - px[..3].iter().min().unwrap())) .sum(); total as f64 / (bgra.len() / 4) as f64 }; // ...and mean luma, in the limited-range code the tone map's anchors are in. let luma = |bgra: &[u8]| -> f64 { let total: f64 = bgra .chunks_exact(4) .map(|px| { 0.2126 * f64::from(px[2]) + 0.7152 * f64::from(px[1]) + 0.0722 * f64::from(px[0]) }) .sum(); 16.0 + 219.0 * (total / (bgra.len() / 4) as f64) / 255.0 }; let (was, now) = (spread(&untouched), spread(&shown)); eprintln!( "frame {TARGET}: saturation {was:.1} -> {now:.1}, mean luma {:.1} -> {:.1}", luma(&untouched), luma(&shown) ); assert!( now > was, "the tone-mapped frame is no more colourful: {now}" ); let mean = luma(&shown); assert!( (24.0..=200.0).contains(&mean), "the tone-mapped frame sits at one end of the scale: mean luma {mean}" ); // The funnel's own budget at this size: the map plus the conversion, which // is what an HDR frame costs on top of the decode. Fastest of five, for the // reason `tonemap::perf_4k_and_1080p` documents. let mapper = ToneMapper::new(Transfer::Pq, engine::tonemap::Preset::default(), None); let sdr = ColorDescription { matrix: Matrix::Bt709, transfer: engine::colorspace::Transfer::Sdr, full_range: false, }; let mut best = f64::MAX; for _ in 0..5 { let (mut my, mut mu, mut mv) = (y.clone(), u.clone(), v.clone()); let t = Instant::now(); mapper.map(&mut my, &mut mu, &mut mv, w, h); let _ = i420_to_bgra(&sdr, &my, &mu, &mv, w, h); best = best.min(t.elapsed().as_secs_f64() * 1000.0); } eprintln!("{w}x{h} tone map + convert: {best:.1} ms/frame"); assert!(best <= 39.0, "{best:.1} ms/frame over the 39 ms budget"); } /// H.264 8-bit decode is bit-exact by conformance, so the two backends must /// agree. A mismatch means a driver deviation and is reported, not swallowed. #[test] #[ignore = "needs libengine_hw.so and a VA-API driver"] fn hardware_matches_software_on_frame_30() { let (_, hw) = decode_all_hw("test_baseline.mp4"); // Pin the comparison side to software ourselves: without this, running the // suite without VE_SW=1 would compare hardware against hardware and pass // vacuously. SAFETY: the suite is documented to run with --test-threads=1. unsafe { std::env::set_var("VE_SW", "1") }; let start = Instant::now(); let (_, rx) = DecodeSession::open(asset("test_baseline.mp4")).expect("software open"); let sw = rx .into_iter() .find(|f| f.index == 30) .expect("software frame 30") .bgra; eprintln!("software reached frame 30 in {:?}", start.elapsed()); assert_eq!(hw.len(), sw.len(), "frame size"); let diff = hw.iter().zip(&sw).filter(|(a, b)| a != b).count(); eprintln!("frame 30 differing bytes: {diff} / {}", hw.len()); assert_eq!(diff, 0, "hardware and software decode disagree"); } /// The AV1 twin of [`hardware_matches_software_on_frame_30`]: the plugin's /// VA-API decoder against the project's own `ec-av1`, whole file, byte for /// byte -- 8-bit AV1 decode is bit-exact by the same conformance argument the /// H.264 twin makes, and a mismatch means a deviation in one of the two /// decoders, reported rather than swallowed. #[test] #[ignore = "needs libengine_hw.so and a VA-API driver with AV1 decode"] fn av1_hardware_matches_software_on_every_frame() { const NAME: &str = "test_av1.mkv"; let (hw_count, hw_frame_30) = decode_all_hw(NAME); // Pin the comparison side to software ourselves: without this, running the // suite without VE_SW=1 would compare hardware against hardware and pass // vacuously. SAFETY: the suite is documented to run with --test-threads=1. unsafe { std::env::set_var("VE_SW", "1") }; let path = asset(NAME); let start = Instant::now(); let (meta, rx) = DecodeSession::open(&path).expect("software open"); let mut sw_count = 0usize; let mut sw_frame_30 = Vec::new(); for frame in rx { if frame.index == 30 { sw_frame_30 = frame.bgra; } sw_count += 1; } let sw_elapsed = start.elapsed(); unsafe { std::env::remove_var("VE_SW") }; eprintln!("{NAME}: hardware {hw_count} frames, software {sw_count} frames in {sw_elapsed:?}"); assert_eq!(sw_count, hw_count, "frame count"); assert_eq!(sw_count, usize::try_from(meta.frame_count).expect("count")); assert_eq!(hw_frame_30.len(), sw_frame_30.len(), "frame size"); let diff = hw_frame_30.iter().zip(&sw_frame_30).filter(|(a, b)| a != b).count(); eprintln!("frame 30 differing bytes: {diff} / {}", sw_frame_30.len()); assert_eq!(diff, 0, "hardware and software AV1 decode disagree"); } /// The 10-bit twin: the plugin reads a P010 surface back as the high byte of /// each pair; `ec-av1` hands `u16` planes the seat downshifts by two. Same /// truncation, same picture -- or one of the two seats is wrong. #[test] #[ignore = "needs libengine_hw.so and a VA-API driver with 10-bit AV1 decode"] fn av1_ten_bit_hardware_matches_software_on_every_frame() { const NAME: &str = "test_av1_10.mkv"; let (hw_count, hw_frame_30) = decode_all_hw(NAME); // SAFETY: the suite is documented to run with --test-threads=1. unsafe { std::env::set_var("VE_SW", "1") }; let path = asset(NAME); let start = Instant::now(); let (_, rx) = DecodeSession::open(&path).expect("software open"); let mut sw_count = 0usize; let mut sw_frame_30 = Vec::new(); for frame in rx { if frame.index == 30 { sw_frame_30 = frame.bgra; } sw_count += 1; } let sw_elapsed = start.elapsed(); unsafe { std::env::remove_var("VE_SW") }; eprintln!("{NAME}: hardware {hw_count} frames, software {sw_count} frames in {sw_elapsed:?}"); assert_eq!(sw_count, hw_count, "frame count"); assert_eq!(hw_frame_30.len(), sw_frame_30.len(), "frame size"); let diff = hw_frame_30.iter().zip(&sw_frame_30).filter(|(a, b)| a != b).count(); eprintln!("frame 30 differing bytes: {diff} / {}", sw_frame_30.len()); assert_eq!(diff, 0, "hardware and software 10-bit AV1 decode disagree"); } /// Annex-B NAL payloads (start codes stripped, trailing zeros trimmed). fn nals(annex_b: &[u8]) -> Vec<&[u8]> { let mut out = Vec::new(); let mut start = None; let mut i = 0; while i + 2 < annex_b.len() { if annex_b[i] == 0 && annex_b[i + 1] == 0 && annex_b[i + 2] == 1 { if let Some(s) = start { out.push(&annex_b[s..i]); } i += 3; start = Some(i); } else { i += 1; } } if let Some(s) = start { out.push(&annex_b[s..]); } out } /// One access unit as a length-prefixed AVCC sample with `cabac_zero_word` -- /// `zeros` zero bytes -- inside the *last* coded NAL's declared length. That is /// byte-for-byte what a phone-camera encoder writes, and what a decoder that /// stops consuming at the last NAL leaves behind as "not a NAL". fn sample_with_trailing_zeros(annex_b: &[u8], zeros: usize) -> (Vec, bool) { let mut coded: Vec<&[u8]> = Vec::new(); let mut key = false; for nal in nals(annex_b) { let end = nal.iter().rposition(|&b| b != 0).map_or(0, |p| p + 1); let nal = &nal[..end]; if nal.is_empty() { continue; } match nal[0] & 0x1f { 7 | 8 | 6 | 9 => continue, // parameter sets/SEI live in avcC or are dropped 5 => key = true, _ => {} } coded.push(nal); } let mut sample = Vec::new(); let last = coded.len().saturating_sub(1); for (i, nal) in coded.iter().enumerate() { let len = nal.len() + if i == last { zeros } else { 0 }; sample.extend_from_slice(&(len as u32).to_be_bytes()); sample.extend_from_slice(nal); if i == last { sample.extend(std::iter::repeat_n(0u8, zeros)); } } (sample, key) } /// An mp4 cut from `test_baseline.mp4` whose every H.264 sample ends in four /// zero bytes. fn write_trailing_zero_fixture(src: &Path, out: &Path, frames: usize) { let (meta, mut demuxer) = Demuxer::open(src).expect("open fixture source"); let first = demuxer.next_access_unit().expect("read").expect("au"); let (sps, pps) = mux::parameter_sets(&first).expect("parameter sets in the first au"); let mut muxer = Mp4Muxer::create( out, &VideoParams { width: meta.width, height: meta.height, frame_rate: meta.frame_rate, sps, pps, }, None, ) .expect("create fixture"); let (sample, key) = sample_with_trailing_zeros(&first, 4); muxer.write_coded_sample(&sample, key).expect("first sample"); for _ in 1..frames { let Some(au) = demuxer.next_access_unit().expect("read") else { break; }; let (sample, key) = sample_with_trailing_zeros(&au, 4); muxer.write_coded_sample(&sample, key).expect("sample"); } muxer.finish().expect("finish fixture"); } /// A phone-camera mp4 ends its every coded sample in `cabac_zero_word`. The /// plugin consumed each access unit only up to the last NAL cros-codecs /// reported and fed the leftover zero bytes back as if they were the next unit; /// `Nalu::next` refuses a buffer with no start code ("No NAL found"), so the /// whole session failed with code -2 before the first picture -- the export's /// `hardware decode failed (code -2)` on a file VA-API otherwise decodes. #[test] #[ignore = "needs libengine_hw.so and a VA-API driver"] fn hardware_decodes_samples_with_trailing_zero_words() { let out = Scratch::file("ve_hw_trailing_zeros", "mp4"); write_trailing_zero_fixture(&asset("test_baseline.mp4"), &out, 30); let mut hw = open_hw(&out); let mut count = 0usize; while let Some((_, _, _, w, h)) = hw .next_frame() .expect("hardware decode of trailing-zero samples") { assert_eq!((w, h), (1280, 720), "frame {count} dims"); count += 1; } assert_eq!(count, 30, "every picture decoded"); }