forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

hevc_sw.rs230 lines, 9.0 KiBHistoryRawDownload
1//! HEVC on the software seat: `ec-h265` through the engine's own span, with the
2//! VA-API plugin forced off (`VE_SW=1`), the way VP9 and VP8 already run there.
3//!
4//! The fixtures are encoded by ffmpeg's libx265 (`scripts/gen_fixtures.sh`); the
5//! plane-for-plane check against ffmpeg's own decode lives beside the seat, in
6//! `decode/hevc.rs`. Here the seat as a whole: what a file opens as, what a span
7//! delivers, and whether a seek lands on the very bytes a linear decode gave.
8//!
9//! `VE_SW` is process-wide, so every test that pins it holds [`SW`] for as long
10//! as it is set: the span reads the pin when it opens on its worker.
11
12use std::path::{Path, PathBuf};
13use std::sync::Mutex;
14use std::time::{Duration, Instant};
15
16use ec_h265::Decoder;
17use engine::decode::{Backend, DecodeSession, probe};
18use engine::demux::{Codec, Demuxer, VideoMeta};
19
20/// 352x288@30, 1 s -- see `scripts/gen_fixtures.sh`.
21const FRAMES: u32 = 30;
22/// 1920x1080@30, 2 s.
23const FRAMES_1080: u32 = 60;
24
25static SW: Mutex<()> = Mutex::new(());
26
27fn asset(name: &str) -> PathBuf {
28 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
29 .join("../../assets")
30 .join(name)
31}
32
33/// Runs `f` with the plugin forced off, holding [`SW`] so no other test here sees
34/// the pin under it.
35fn software<T>(f: impl FnOnce() -> T) -> T {
36 let _guard = SW.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
37 engine::envcfg::set("VE_SW", "1");
38 let out = f();
39 engine::envcfg::unset("VE_SW");
40 out
41}
42
43/// Opens a clip on the software seat and drains it.
44fn software_frames(path: &Path) -> (VideoMeta, Vec<engine::decode::Frame>) {
45 software(|| {
46 let (meta, rx) = DecodeSession::open(path).expect("software HEVC open");
47 (meta, rx.into_iter().collect())
48 })
49}
50
51/// The container half: an HEVC track is found in both containers, at its size,
52/// its length and its bit depth, which is what picks the 10-bit path.
53#[test]
54fn the_demuxer_reports_hevc_in_mp4_and_matroska() {
55 for (name, depth) in [
56 ("test_hevcsw_8.mp4", 8),
57 ("test_hevcsw_8.mkv", 8),
58 ("test_hevcsw_10.mp4", 10),
59 ("test_hevcsw_10.mkv", 10),
60 ] {
61 let (meta, demuxer) = Demuxer::open(&asset(name)).expect(name);
62 assert_eq!(meta.codec, Codec::Hevc, "{name}");
63 assert_eq!((meta.width, meta.height), (352, 288), "{name}");
64 assert_eq!(meta.frame_count, FRAMES, "{name}: container count");
65 assert_eq!(demuxer.bit_depth(), depth, "{name}: bit depth off the hvcC");
66 }
67}
68
69/// With the plugin off, an HEVC file probes as the software seat, not as a
70/// refusal naming the plugin. This is the answer a box without VA-API gets.
71#[test]
72fn the_software_seat_takes_hevc_with_the_plugin_off() {
73 let (codec, backend) = software(|| probe(&asset("test_hevcsw_8.mp4")).expect("probe"));
74 assert_eq!(codec, Some(Codec::Hevc));
75 assert_eq!(backend, Backend::Software);
76}
77
78/// The seat end to end, for 8-bit and Main 10, both containers, an odd size and a
79/// GOP that starts on an open CRA: every frame comes out in display order at the
80/// clip's size with a picture in it, and a seek lands on the bytes a linear decode
81/// delivered.
82#[test]
83fn the_software_seat_decodes_and_seeks_hevc() {
84 let cases = [
85 ("test_hevcsw_8.mp4", 352, 288),
86 ("test_hevcsw_8.mkv", 352, 288),
87 ("test_hevcsw_10.mp4", 352, 288),
88 ("test_hevcsw_10.mkv", 352, 288),
89 ("test_hevcsw_odd.mp4", 350, 286),
90 ("test_hevcsw_cra.mp4", 352, 288),
91 ];
92 for (name, w, h) in cases {
93 let clip = asset(name);
94 let (meta, frames) = software_frames(&clip);
95 assert_eq!(meta.codec, Codec::Hevc, "{name}");
96 assert_eq!(
97 frames.iter().map(|f| f.index).collect::<Vec<_>>(),
98 (0..FRAMES).collect::<Vec<_>>(),
99 "{name}: frames must arrive 0..n in display order"
100 );
101 assert!(
102 frames.iter().all(|f| (f.width, f.height) == (w, h)),
103 "{name}: every frame at the clip's size"
104 );
105 let first = &frames[0].bgra;
106 assert!(
107 first
108 .as_chunks::<4>()
109 .0
110 .iter()
111 .any(|px| px[..] != first[..4]),
112 "{name}: frame 0 is a single colour -- no picture was decoded"
113 );
114
115 // The sync points (the IDR at 0, and the IRAP at 15 that the open-GOP
116 // fixture starts its leading pictures on), the frames either side, and the last.
117 for target in [0, 1, 14, 15, 16, FRAMES - 1] {
118 let want = &frames[target as usize].bgra;
119 let seeked = software(|| {
120 let (_, rx, _cancel) = DecodeSession::open_at(&clip, target).expect("open_at");
121 rx.into_iter()
122 .next()
123 .unwrap_or_else(|| panic!("{name}: no frame after a seek to {target}"))
124 });
125 assert_eq!(
126 seeked.index, target,
127 "{name}: first frame after seek to {target}"
128 );
129 assert!(
130 seeked.bgra == *want,
131 "{name}: seek to {target} differs from a linear decode"
132 );
133 }
134 }
135}
136
137/// A clip of `pix_fmt` encoded by ffmpeg's libx265 into the temp dir: 30 frames
138/// of 352x288. `assets/` is gitignored, so the clips these tests need are made here.
139fn gen_clip(name: &str, pix_fmt: &str) -> PathBuf {
140 let path = std::env::temp_dir().join(format!("{name}-{}.mp4", std::process::id()));
141 let out = std::process::Command::new("ffmpeg")
142 .args(["-v", "error", "-y", "-f", "lavfi", "-i"])
143 .arg("testsrc2=size=352x288:rate=30:duration=1")
144 .args(["-frames:v", "30", "-c:v", "libx265", "-pix_fmt", pix_fmt])
145 .args(["-x265-params", "log-level=error"])
146 .arg(&path)
147 .output()
148 .expect("run ffmpeg: the clips are encoded with it");
149 assert!(
150 out.status.success(),
151 "ffmpeg: {}",
152 String::from_utf8_lossy(&out.stderr)
153 );
154 path
155}
156
157/// A 4:2:2 HEVC file opens on the software seat at 8 and 10 bits and every frame
158/// comes out: the chroma is averaged to 4:2:0 on the way in, and the plane check
159/// against ffmpeg lives beside the seat, in `decode/hevc.rs`.
160#[test]
161fn a_4_2_2_hevc_file_opens_and_decodes_every_frame_on_the_software_seat() {
162 for (name, pix_fmt) in [
163 ("hevc422-it-8", "yuv422p"),
164 ("hevc422-it-10", "yuv422p10le"),
165 ] {
166 let clip = gen_clip(name, pix_fmt);
167 let (meta, frames) = software(|| {
168 let (meta, rx) = DecodeSession::open(&clip)
169 .unwrap_or_else(|e| panic!("{name}: the software seat must open 4:2:2: {e}"));
170 (meta, rx.into_iter().collect::<Vec<_>>())
171 });
172 let _ = std::fs::remove_file(&clip);
173 assert_eq!(meta.codec, Codec::Hevc, "{name}");
174 assert_eq!(frames.len(), 30, "{name}: every frame comes out");
175 }
176}
177
178/// 4:4:4 opens on the software seat at 8 and 10 bits, and every frame comes out.
179#[test]
180fn a_4_4_4_hevc_file_opens_and_decodes_every_frame_on_the_software_seat() {
181 for (name, pix_fmt) in [("hevc444-it-8", "yuv444p"), ("hevc444-it-10", "yuv444p10le")] {
182 let clip = gen_clip(name, pix_fmt);
183 Demuxer::open(&clip).unwrap_or_else(|e| panic!("{name}: {e}"));
184 let (_, frames) = software_frames(&clip);
185 let _ = std::fs::remove_file(&clip);
186 assert_eq!(frames.len(), 30, "{name}: every frame comes out");
187 }
188}
189
190/// The per-frame cost at 1080p, 8-bit: `ec-h265` alone (decode, no conversion)
191/// and the whole software seat (decode, crop, narrow, BGRA). Printed for the
192/// lane report; every frame must come out.
193#[test]
194fn the_software_seat_times_1080p_hevc() {
195 let clip = asset("test_hevcsw_1080.mp4");
196
197 let (_, mut demuxer) = Demuxer::open(&clip).expect("open the 1080p clip");
198 let mut decoder = Decoder::new();
199 let (mut decoded, mut spent) = (0u32, Duration::ZERO);
200 while let Some(au) = demuxer.next_access_unit().expect("read") {
201 let start = Instant::now();
202 decoder.decode_annex_b(&au).expect("decode");
203 while decoder.next_frame().is_some() {
204 decoded += 1;
205 }
206 spent += start.elapsed();
207 }
208 let start = Instant::now();
209 decoder.flush().expect("flush");
210 while decoder.next_frame().is_some() {
211 decoded += 1;
212 }
213 spent += start.elapsed();
214 assert_eq!(decoded, FRAMES_1080, "ec-h265 alone releases every frame");
215 let per_frame = spent.as_secs_f64() * 1e3 / f64::from(decoded);
216 println!("ec-h265 alone, 1080p 8-bit: {per_frame:.2} ms/frame over {decoded} frames");
217
218 let (seat_frames, seat_spent) = software(|| {
219 let start = Instant::now();
220 let (_, rx) = DecodeSession::open(&clip).expect("software 1080p open");
221 let n = rx.into_iter().count();
222 (n, start.elapsed())
223 });
224 assert_eq!(
225 seat_frames, FRAMES_1080 as usize,
226 "the seat delivers every frame"
227 );
228 let per_frame = seat_spent.as_secs_f64() * 1e3 / seat_frames as f64;
229 println!("software seat, 1080p 8-bit: {per_frame:.2} ms/frame over {seat_frames} frames");
230}