forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB8c7bf80Tahinli3 months ago114 commits

hevc.rs182 lines, 7.6 KiBHistoryRawDownload
1//! HEVC import: the second source codec that is not H.264, and the first whose
2//! parameter sets have to be dug out of the container by hand (`mp4 0.14` reads
3//! one byte of `hvcC` and skips the rest).
4//!
5//! The container and refusal checks need nothing installed. The decode and
6//! export twins need a built `libengine_hw.so` and a VA-API driver with an HEVC
7//! decode entrypoint (`vainfo | grep HEVC`), so they are `#[ignore]`d:
8//!
9//! ```text
10//! cargo build -p engine -p engine-hw --release
11//! LD_LIBRARY_PATH=target/release \
12//! cargo test -p engine --release --test hevc -- --include-ignored --nocapture --test-threads=1
13//! ```
14//!
15//! `VE_SW` is process-wide, hence `--test-threads=1`: the refusal test sets it
16//! and puts it back so the hardware twins below really are hardware.
17
18use std::path::PathBuf;
19use std::time::{Duration, Instant};
20
21use engine::demux::{Codec, Demuxer};
22use engine::export::ExportSettings;
23use engine::{AudioSession, DecodeSession, PlaybackSession, Project};
24
25/// 1280x720@30, 2 s -- see `scripts/gen_fixtures.sh`.
26const FRAMES: u32 = 60;
27
28fn asset(name: &str) -> PathBuf {
29 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
30 .join("../../assets")
31 .join(name)
32}
33
34/// The container half: a hev1 track is found, described and named as HEVC, and
35/// its AAC track is read exactly as any other file's.
36#[test]
37fn the_demuxer_reports_an_hevc_track() {
38 let (meta, _) = Demuxer::open(&asset("test_hevc.mp4")).expect("open test_hevc.mp4");
39 assert_eq!(meta.codec, Codec::Hevc);
40 assert_eq!((meta.width, meta.height), (1280, 720));
41 assert!((meta.frame_rate - 30.0).abs() < 0.01, "{}", meta.frame_rate);
42 assert_eq!(meta.frame_count, FRAMES);
43
44 let probe = AudioSession::probe(asset("test_hevc.mp4"), 0)
45 .expect("probe")
46 .expect("the fixture has an AAC track");
47 assert_eq!(
48 (probe.sample_rate, probe.channels),
49 (44_100, 2),
50 "audio is read the same whatever the picture is coded with"
51 );
52
53 // The invariant, stated as an assert: the older codecs still say themselves.
54 let (h264, _) = Demuxer::open(&asset("test_baseline.mp4")).expect("open test_baseline.mp4");
55 assert_eq!(h264.codec, Codec::H264);
56 let (vp9, _) = Demuxer::open(&asset("test_vp9.mp4")).expect("open test_vp9.mp4");
57 assert_eq!(vp9.codec, Codec::Vp9);
58}
59
60/// The part `mp4 0.14` cannot do: every access unit is Annex-B framed, and the
61/// sync ones carry the VPS, SPS and PPS read out of `hvcC` by hand. Without
62/// those three a hardware decoder has nothing to configure itself from, so this
63/// is the check that says the box was really parsed and not merely found.
64#[test]
65fn every_sync_sample_carries_annex_b_vps_sps_and_pps() {
66 let (_, mut demuxer) = Demuxer::open(&asset("test_hevc.mp4")).expect("open test_hevc.mp4");
67 let first = demuxer
68 .next_access_unit()
69 .expect("read")
70 .expect("a first access unit");
71
72 // NAL types out of the two-byte HEVC NAL header: bits 6..1 of the first byte.
73 let types: Vec<u8> = first
74 .windows(4)
75 .enumerate()
76 .filter(|(_, w)| *w == [0, 0, 0, 1])
77 .map(|(i, _)| (first[i + 4] >> 1) & 0x3f)
78 .collect();
79 assert!(types.starts_with(&[32, 33, 34]), "{types:?}");
80 assert!(
81 types.iter().any(|&t| (16..=21).contains(&t)),
82 "the first sample must also carry an IRAP slice: {types:?}"
83 );
84
85 let mut count = 1;
86 while let Some(au) = demuxer.next_access_unit().expect("read") {
87 assert_eq!(&au[..4], [0, 0, 0, 1], "sample {count} is not Annex-B");
88 count += 1;
89 }
90 assert_eq!(count, FRAMES, "every sample came back out");
91}
92
93/// There is no software HEVC decoder, so the software path must refuse by name
94/// rather than feed HEVC bytes to `rusty_h264` -- and it must refuse where a
95/// caller can still show it, i.e. out of `open`, not from inside the worker.
96#[test]
97fn the_software_path_refuses_hevc_by_name() {
98 // SAFETY: the suite is documented to run with --test-threads=1.
99 unsafe { std::env::set_var("VE_SW", "1") };
100 let refused =
101 DecodeSession::open(asset("test_hevc.mp4")).expect_err("software must not accept HEVC");
102 let refused = refused.to_string();
103 // Restored immediately: the hardware tests in this binary share the process.
104 unsafe { std::env::remove_var("VE_SW") };
105
106 assert!(refused.contains("HEVC"), "{refused}");
107 assert!(refused.contains("plugin"), "{refused}");
108 // H.264 is untouched by all of this.
109 unsafe { std::env::set_var("VE_SW", "1") };
110 let h264 = DecodeSession::open(asset("test_baseline.mp4")).is_ok();
111 unsafe { std::env::remove_var("VE_SW") };
112 assert!(h264, "software H.264 decode still opens");
113}
114
115/// A timeline is refused a source coded differently, in the same words a
116/// resolution mismatch is refused in: a front-end shows the string as it is.
117#[test]
118fn a_timeline_refuses_the_other_codec() {
119 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open test_av.mp4");
120 let refused = session
121 .import(&asset("test_hevc.mp4"))
122 .expect_err("HEVC must not join an H.264 timeline")
123 .to_string();
124 assert!(refused.contains("HEVC"), "{refused}");
125 assert!(refused.contains("H.264"), "{refused}");
126}
127
128/// The end-to-end user path: opening the file yields pictures, all of them,
129/// through the plugin.
130#[test]
131#[ignore = "needs libengine_hw.so and a VA-API driver with HEVC decode"]
132fn the_plugin_decodes_every_hevc_frame() {
133 let start = Instant::now();
134 let (meta, frames) = DecodeSession::open(asset("test_hevc.mp4")).expect("open test_hevc.mp4");
135 assert_eq!(meta.codec, Codec::Hevc);
136 let frames: Vec<_> = frames.into_iter().collect();
137 eprintln!(
138 "test_hevc.mp4: {} frames in {:?}",
139 frames.len(),
140 start.elapsed()
141 );
142
143 assert_eq!(frames.len() as u32, FRAMES, "every sample decoded");
144 for (i, frame) in frames.iter().enumerate() {
145 assert_eq!((frame.width, frame.height), (1280, 720), "frame {i} dims");
146 assert_eq!(frame.index, i as u32, "frames arrive in display order");
147 assert_eq!(frame.bgra.len(), 1280 * 720 * 4, "frame {i} size");
148 }
149 // A picture, not a flat surface: a driver handing back an untouched buffer
150 // would satisfy every count above.
151 let first = &frames[0].bgra;
152 assert!(
153 first.chunks_exact(4).any(|px| px != &first[..4]),
154 "frame 0 is a single colour -- no picture was decoded"
155 );
156}
157
158/// Export re-encodes to H.264 whatever the source was coded with: the packet
159/// copy path is audio-only, so no HEVC byte can reach an `avc1` track.
160#[test]
161#[ignore = "needs libengine_hw.so and a VA-API driver with HEVC decode"]
162fn an_hevc_source_exports_as_h264() {
163 let session = PlaybackSession::open(asset("test_hevc.mp4")).expect("open test_hevc.mp4");
164 let meta = *session.meta();
165 let project = Project::single(asset("test_hevc.mp4"), meta.frame_count);
166 let out = std::env::temp_dir().join(format!("ve_export_hevc_{}.mp4", std::process::id()));
167 let _ = std::fs::remove_file(&out);
168
169 let handle = engine::export::start(project, meta, &out, &ExportSettings::default());
170 let started = Instant::now();
171 while !handle.is_finished() {
172 assert!(started.elapsed() < Duration::from_secs(120), "export hung");
173 std::thread::sleep(Duration::from_millis(20));
174 }
175 handle.result().expect("outcome").expect("export");
176
177 let (written, _) = Demuxer::open(&out).expect("reopen the export");
178 assert_eq!(written.codec, Codec::H264, "exports are always H.264");
179 assert_eq!(written.frame_count, FRAMES, "every timeline frame written");
180 assert_eq!((written.width, written.height), (1280, 720));
181 let _ = std::fs::remove_file(&out);
182}