forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB2e2ed69Tahinli3 months ago69 commits

hw_decode.rs187 lines, 6.9 KiBHistoryRawDownload
1//! Hardware path checks. These need a built `libengine_hw.so` plus a working
2//! VA-API driver, so they are `#[ignore]`d by default. Run them with:
3//!
4//! ```text
5//! cargo build --workspace --release
6//! LD_LIBRARY_PATH=target/release \
7//! cargo test -p engine --release --test hw_decode -- --ignored --nocapture --test-threads=1
8//! ```
9//!
10//! The comparison test pins [`DecodeSession`] to the software decoder itself
11//! (it sets `VE_SW=1` in-process, hence the required `--test-threads=1`); the
12//! hardware side goes through [`HwSession`] directly and can never silently be
13//! a fallback.
14
15use std::path::{Path, PathBuf};
16use std::time::Instant;
17
18use engine::DecodeSession;
19use engine::convert::i420_to_bgra;
20use engine::hw::HwSession;
21
22fn asset(name: &str) -> PathBuf {
23 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
24 .join("../../assets")
25 .join(name)
26}
27
28fn open_hw(path: &Path) -> HwSession {
29 HwSession::open(path).expect("no hardware decode plugin/driver available")
30}
31
32/// Decodes the whole file through the plugin, returning (frame count, frame 30 as BGRA).
33fn decode_all_hw(name: &str) -> (usize, Vec<u8>) {
34 let path = asset(name);
35 let mut hw = open_hw(&path);
36 let meta = hw.meta().expect("meta");
37 assert_eq!(
38 (meta.width, meta.height),
39 (1280, 720),
40 "{name} container dims"
41 );
42
43 let mut count = 0usize;
44 let mut frame_30 = Vec::new();
45 let start = Instant::now();
46 while let Some((y, u, v, w, h)) = hw.next_frame().expect("hardware decode") {
47 assert_eq!((w, h), (1280, 720), "{name} frame {count} dims");
48 if count == 30 {
49 frame_30 = i420_to_bgra(y, u, v, w as usize, h as usize);
50 }
51 count += 1;
52 }
53 eprintln!(
54 "{name}: {count} hardware frames in {:?} ({:?}/frame)",
55 start.elapsed(),
56 start.elapsed() / count.max(1) as u32
57 );
58 (count, frame_30)
59}
60
61/// The plugin must refuse junk by returning null, never by unwinding into us.
62#[test]
63#[ignore = "needs libengine_hw.so and a VA-API driver"]
64fn rejects_unopenable_files() {
65 assert!(HwSession::open(Path::new("/definitely/not/here.mp4")).is_none());
66 assert!(
67 HwSession::open(Path::new("/etc/hostname")).is_none(),
68 "not an mp4"
69 );
70}
71
72#[test]
73#[ignore = "needs libengine_hw.so and a VA-API driver"]
74fn hardware_decodes_baseline() {
75 let (count, _) = decode_all_hw("test_baseline.mp4");
76 assert_eq!(count, 150);
77}
78
79#[test]
80#[ignore = "needs libengine_hw.so and a VA-API driver"]
81fn hardware_decodes_high_profile() {
82 // B-frames: the plugin emits display order (DPB bumping happens inside
83 // cros-codecs), so a clean run to EOF is the check we can make headlessly.
84 let (count, _) = decode_all_hw("test_high.mp4");
85 assert_eq!(count, 150);
86}
87
88/// Seeking may only change *when* a picture arrives, never the picture: both
89/// backends must land on bytes identical to a linear decode of the same index.
90#[test]
91#[ignore = "needs libengine_hw.so and a VA-API driver"]
92fn seek_matches_linear() {
93 const TARGET: u32 = 60;
94 let path = asset("test_baseline.mp4");
95
96 // Software side first, pinned like the test above. SAFETY: --test-threads=1.
97 unsafe { std::env::set_var("VE_SW", "1") };
98 let (_, rx) = DecodeSession::open(&path).expect("software open");
99 let linear = rx
100 .into_iter()
101 .find(|f| f.index == TARGET)
102 .expect("software frame 60")
103 .bgra;
104
105 let start = Instant::now();
106 let (_, rx, _cancel) = DecodeSession::open_at(&path, TARGET).expect("software open_at");
107 let seeked = rx.into_iter().next().expect("no frame after software seek");
108 eprintln!("software seek to {TARGET} took {:?}", start.elapsed());
109 assert_eq!(seeked.index, TARGET, "first frame after seek is the target");
110 assert_eq!(seeked.bgra, linear, "software seek != software linear");
111
112 let start = Instant::now();
113 let mut hw = HwSession::open_at(&path, TARGET).expect("no plugin/driver available");
114 let (y, u, v, w, h) = hw
115 .next_frame()
116 .expect("hardware decode")
117 .expect("no frame after hardware seek");
118 eprintln!("hardware seek to {TARGET} took {:?}", start.elapsed());
119 assert_eq!(
120 i420_to_bgra(y, u, v, w as usize, h as usize),
121 linear,
122 "hardware seek != software linear"
123 );
124}
125
126/// Drains a bounded session, returning the indices it delivered. The loop ends
127/// only on `RecvError`, i.e. the worker closed the channel by itself.
128fn range_indices(path: &Path, start: u32, end: u32) -> Vec<u32> {
129 let (_, rx, _cancel) = DecodeSession::open_range(path, start, end).expect("open_range");
130 let mut got = Vec::new();
131 while let Ok(frame) = rx.recv() {
132 got.push(frame.index);
133 }
134 got
135}
136
137/// A bounded range must stop on its own — both backends — with `Frame::index`
138/// still the absolute source index, and then disconnect rather than hang.
139#[test]
140#[ignore = "needs libengine_hw.so and a VA-API driver"]
141fn range_stops_at_end() {
142 const START: u32 = 30;
143 const END: u32 = 60;
144 let path = asset("test_baseline.mp4");
145 let want: Vec<u32> = (START..END).collect();
146
147 // Another test in this binary may already have pinned VE_SW; clear it so
148 // the hardware half really is hardware. SAFETY: --test-threads=1.
149 unsafe { std::env::remove_var("VE_SW") };
150 assert_eq!(range_indices(&path, START, END), want, "hardware range");
151
152 unsafe { std::env::set_var("VE_SW", "1") };
153 assert_eq!(range_indices(&path, START, END), want, "software range");
154
155 // Degenerate ranges close cleanly instead of decoding to EOF.
156 assert!(range_indices(&path, 30, 30).is_empty(), "empty range");
157 assert!(range_indices(&path, 60, 30).is_empty(), "inverted range");
158 // Past the end is clamped to the frame count.
159 assert_eq!(range_indices(&path, 148, 999).len(), 2, "clamped range");
160}
161
162/// H.264 8-bit decode is bit-exact by conformance, so the two backends must
163/// agree. A mismatch means a driver deviation and is reported, not swallowed.
164#[test]
165#[ignore = "needs libengine_hw.so and a VA-API driver"]
166fn hardware_matches_software_on_frame_30() {
167 let (_, hw) = decode_all_hw("test_baseline.mp4");
168
169 // Pin the comparison side to software ourselves: without this, running the
170 // suite without VE_SW=1 would compare hardware against hardware and pass
171 // vacuously. SAFETY: the suite is documented to run with --test-threads=1.
172 unsafe { std::env::set_var("VE_SW", "1") };
173
174 let start = Instant::now();
175 let (_, rx) = DecodeSession::open(asset("test_baseline.mp4")).expect("software open");
176 let sw = rx
177 .into_iter()
178 .find(|f| f.index == 30)
179 .expect("software frame 30")
180 .bgra;
181 eprintln!("software reached frame 30 in {:?}", start.elapsed());
182
183 assert_eq!(hw.len(), sw.len(), "frame size");
184 let diff = hw.iter().zip(&sw).filter(|(a, b)| a != b).count();
185 eprintln!("frame 30 differing bytes: {diff} / {}", hw.len());
186 assert_eq!(diff, 0, "hardware and software decode disagree");
187}