| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | use std::path::{Path, PathBuf}; |
| 16 | use std::time::Instant; |
| 17 | |
| 18 | use engine::DecodeSession; |
| 19 | use engine::convert::i420_to_bgra; |
| 20 | use engine::hw::HwSession; |
| 21 | |
| 22 | fn asset(name: &str) -> PathBuf { |
| 23 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 24 | .join("../../assets") |
| 25 | .join(name) |
| 26 | } |
| 27 | |
| 28 | fn open_hw(path: &Path) -> HwSession { |
| 29 | HwSession::open(path).expect("no hardware decode plugin/driver available") |
| 30 | } |
| 31 | |
| 32 | |
| 33 | fn 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 | |
| 62 | #[test] |
| 63 | #[ignore = "needs libengine_hw.so and a VA-API driver"] |
| 64 | fn 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"] |
| 74 | fn 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"] |
| 81 | fn hardware_decodes_high_profile() { |
| 82 | |
| 83 | |
| 84 | let (count, _) = decode_all_hw("test_high.mp4"); |
| 85 | assert_eq!(count, 150); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | #[test] |
| 91 | #[ignore = "needs libengine_hw.so and a VA-API driver"] |
| 92 | fn seek_matches_linear() { |
| 93 | const TARGET: u32 = 60; |
| 94 | let path = asset("test_baseline.mp4"); |
| 95 | |
| 96 | |
| 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 | |
| 127 | |
| 128 | fn 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 | |
| 138 | |
| 139 | #[test] |
| 140 | #[ignore = "needs libengine_hw.so and a VA-API driver"] |
| 141 | fn 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 | |
| 148 | |
| 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 | |
| 156 | assert!(range_indices(&path, 30, 30).is_empty(), "empty range"); |
| 157 | assert!(range_indices(&path, 60, 30).is_empty(), "inverted range"); |
| 158 | |
| 159 | assert_eq!(range_indices(&path, 148, 999).len(), 2, "clamped range"); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | |
| 164 | #[test] |
| 165 | #[ignore = "needs libengine_hw.so and a VA-API driver"] |
| 166 | fn hardware_matches_software_on_frame_30() { |
| 167 | let (_, hw) = decode_all_hw("test_baseline.mp4"); |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 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 | } |