//! HEVC import: the second source codec that is not H.264, and the first whose //! parameter sets have to be dug out of the container by hand (`mp4 0.14` reads //! one byte of `hvcC` and skips the rest). //! //! The container and refusal checks need nothing installed. The decode and //! export twins need a built `libengine_hw.so` and a VA-API driver with an HEVC //! decode entrypoint (`vainfo | grep HEVC`), so they are `#[ignore]`d: //! //! ```text //! cargo build -p engine -p engine-hw --release //! LD_LIBRARY_PATH=target/release \ //! cargo test -p engine --release --test hevc -- --include-ignored --nocapture --test-threads=1 //! ``` //! //! `VE_SW` is process-wide, hence `--test-threads=1`: the refusal test sets it //! and puts it back so the hardware twins below really are hardware. use std::path::PathBuf; use std::time::{Duration, Instant}; use engine::demux::{Codec, Demuxer}; use engine::export::ExportSettings; use engine::{AudioSession, DecodeSession, PlaybackSession, Project}; /// 1280x720@30, 2 s -- see `scripts/gen_fixtures.sh`. const FRAMES: u32 = 60; fn asset(name: &str) -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")) .join("../../assets") .join(name) } /// The container half: a hev1 track is found, described and named as HEVC, and /// its AAC track is read exactly as any other file's. #[test] fn the_demuxer_reports_an_hevc_track() { let (meta, _) = Demuxer::open(&asset("test_hevc.mp4")).expect("open test_hevc.mp4"); assert_eq!(meta.codec, Codec::Hevc); assert_eq!((meta.width, meta.height), (1280, 720)); assert!((meta.frame_rate - 30.0).abs() < 0.01, "{}", meta.frame_rate); assert_eq!(meta.frame_count, FRAMES); let probe = AudioSession::probe(asset("test_hevc.mp4"), 0) .expect("probe") .expect("the fixture has an AAC track"); assert_eq!( (probe.sample_rate, probe.channels), (44_100, 2), "audio is read the same whatever the picture is coded with" ); // The invariant, stated as an assert: the older codecs still say themselves. let (h264, _) = Demuxer::open(&asset("test_baseline.mp4")).expect("open test_baseline.mp4"); assert_eq!(h264.codec, Codec::H264); let (vp9, _) = Demuxer::open(&asset("test_vp9.mp4")).expect("open test_vp9.mp4"); assert_eq!(vp9.codec, Codec::Vp9); } /// The part `mp4 0.14` cannot do: every access unit is Annex-B framed, and the /// sync ones carry the VPS, SPS and PPS read out of `hvcC` by hand. Without /// those three a hardware decoder has nothing to configure itself from, so this /// is the check that says the box was really parsed and not merely found. #[test] fn every_sync_sample_carries_annex_b_vps_sps_and_pps() { let (_, mut demuxer) = Demuxer::open(&asset("test_hevc.mp4")).expect("open test_hevc.mp4"); let first = demuxer .next_access_unit() .expect("read") .expect("a first access unit"); // NAL types out of the two-byte HEVC NAL header: bits 6..1 of the first byte. let types: Vec = first .windows(4) .enumerate() .filter(|(_, w)| *w == [0, 0, 0, 1]) .map(|(i, _)| (first[i + 4] >> 1) & 0x3f) .collect(); assert!(types.starts_with(&[32, 33, 34]), "{types:?}"); assert!( types.iter().any(|&t| (16..=21).contains(&t)), "the first sample must also carry an IRAP slice: {types:?}" ); let mut count = 1; while let Some(au) = demuxer.next_access_unit().expect("read") { assert_eq!(&au[..4], [0, 0, 0, 1], "sample {count} is not Annex-B"); count += 1; } assert_eq!(count, FRAMES, "every sample came back out"); } /// There is no software HEVC decoder, so the software path must refuse by name /// rather than feed HEVC bytes to `rusty_h264` -- and it must refuse where a /// caller can still show it, i.e. out of `open`, not from inside the worker. #[test] fn the_software_path_refuses_hevc_by_name() { // SAFETY: the suite is documented to run with --test-threads=1. unsafe { std::env::set_var("VE_SW", "1") }; let refused = DecodeSession::open(asset("test_hevc.mp4")).expect_err("software must not accept HEVC"); let refused = refused.to_string(); // Restored immediately: the hardware tests in this binary share the process. unsafe { std::env::remove_var("VE_SW") }; assert!(refused.contains("HEVC"), "{refused}"); assert!(refused.contains("plugin"), "{refused}"); // H.264 is untouched by all of this. unsafe { std::env::set_var("VE_SW", "1") }; let h264 = DecodeSession::open(asset("test_baseline.mp4")).is_ok(); unsafe { std::env::remove_var("VE_SW") }; assert!(h264, "software H.264 decode still opens"); } /// A timeline is refused a source coded differently, in the same words a /// resolution mismatch is refused in: a front-end shows the string as it is. #[test] fn a_timeline_refuses_the_other_codec() { let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open test_av.mp4"); let refused = session .import(&asset("test_hevc.mp4")) .expect_err("HEVC must not join an H.264 timeline") .to_string(); assert!(refused.contains("HEVC"), "{refused}"); assert!(refused.contains("H.264"), "{refused}"); } /// The end-to-end user path: opening the file yields pictures, all of them, /// through the plugin. #[test] #[ignore = "needs libengine_hw.so and a VA-API driver with HEVC decode"] fn the_plugin_decodes_every_hevc_frame() { let start = Instant::now(); let (meta, frames) = DecodeSession::open(asset("test_hevc.mp4")).expect("open test_hevc.mp4"); assert_eq!(meta.codec, Codec::Hevc); let frames: Vec<_> = frames.into_iter().collect(); eprintln!( "test_hevc.mp4: {} frames in {:?}", frames.len(), start.elapsed() ); assert_eq!(frames.len() as u32, FRAMES, "every sample decoded"); for (i, frame) in frames.iter().enumerate() { assert_eq!((frame.width, frame.height), (1280, 720), "frame {i} dims"); assert_eq!(frame.index, i as u32, "frames arrive in display order"); assert_eq!(frame.bgra.len(), 1280 * 720 * 4, "frame {i} size"); } // A picture, not a flat surface: a driver handing back an untouched buffer // would satisfy every count above. let first = &frames[0].bgra; assert!( first.chunks_exact(4).any(|px| px != &first[..4]), "frame 0 is a single colour -- no picture was decoded" ); } /// Export re-encodes to H.264 whatever the source was coded with: the packet /// copy path is audio-only, so no HEVC byte can reach an `avc1` track. #[test] #[ignore = "needs libengine_hw.so and a VA-API driver with HEVC decode"] fn an_hevc_source_exports_as_h264() { let session = PlaybackSession::open(asset("test_hevc.mp4")).expect("open test_hevc.mp4"); let meta = *session.meta(); let project = Project::single(asset("test_hevc.mp4"), meta.frame_count); let out = std::env::temp_dir().join(format!("ve_export_hevc_{}.mp4", std::process::id())); let _ = std::fs::remove_file(&out); let handle = engine::export::start(project, meta, &out, &ExportSettings::default()); let started = Instant::now(); while !handle.is_finished() { assert!(started.elapsed() < Duration::from_secs(120), "export hung"); std::thread::sleep(Duration::from_millis(20)); } handle.result().expect("outcome").expect("export"); let (written, _) = Demuxer::open(&out).expect("reopen the export"); assert_eq!(written.codec, Codec::H264, "exports are always H.264"); assert_eq!(written.frame_count, FRAMES, "every timeline frame written"); assert_eq!((written.width, written.height), (1280, 720)); let _ = std::fs::remove_file(&out); }