forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBfbc29d1Tahinli2 months ago972 commits

capability_matrix.rs198 lines, 8.4 KiBHistoryRawDownload
1//! The capability matrix against real files: what the unit test in
2//! `demux::tests` asserts about the dispatch tables, this asserts about the
3//! containers themselves — every codec the decoder layer can be handed opens
4//! from *both* an mp4 and a Matroska file, and says its own name.
5//!
6//! The defect class it exists for: VP9 shipped decodable out of an mp4 and
7//! refused out of a `.webm` with the VA-API VP9 decoder compiled in, because one
8//! dispatch arm was missing while the refusal string claimed the capability was
9//! absent. A gap must be a written-down row (`demux::UNSUPPORTED`), never a
10//! silent fall-through.
11//!
12//! Nothing installed is needed: these are container reads, not decodes.
13
14use std::path::PathBuf;
15
16use engine::AudioSession;
17use engine::demux::{Codec, Demuxer};
18
19fn asset(name: &str) -> PathBuf {
20 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
21 .join("../../assets")
22 .join(name)
23}
24
25/// One row per cell of (codec × container). A cell with no fixture is a hole in
26/// the evidence, so every one of them is filled here.
27const MATRIX: &[(Codec, &str, &str)] = &[
28 (Codec::H264, "test_baseline.mp4", "test_h264.mkv"),
29 (Codec::Hevc, "test_hevc.mp4", "test_hevc.mkv"),
30 (Codec::Vp9, "test_vp9.mp4", "test_vp9.webm"),
31 (Codec::Vp8, "test_vp8.mp4", "test_vp8.webm"),
32 // `mp4 0.14` writes no `av01` sample entry, so there is no AV1 mp4 fixture
33 // to open; the mp4 half of that row is `av01`'s presence in the dispatch
34 // table, which `demux::tests` holds.
35 (Codec::Av1, "", "test_av1.mkv"),
36];
37
38#[test]
39fn every_codec_opens_from_every_container_it_arrives_in() {
40 for (codec, mp4, mkv) in MATRIX {
41 for file in [mp4, mkv].into_iter().filter(|f| !f.is_empty()) {
42 let (meta, _) = Demuxer::open(&asset(file))
43 .unwrap_or_else(|e| panic!("{file} carries {} and was refused: {e}", codec.name()));
44 assert_eq!(meta.codec, *codec, "{file}");
45 assert!(meta.frame_count > 0, "{file} came back with no frames");
46 assert_eq!((meta.width, meta.height), (1280, 720), "{file}");
47 }
48 }
49}
50
51/// The depth cell of the matrix: 10-bit is a property of the stream, and VP9
52/// states it in the one place neither container repeats — the keyframe's
53/// uncompressed header. Assumed 8-bit, a profile 2 stream decodes into an NV12
54/// pool and comes back as garbage.
55#[test]
56fn a_ten_bit_vp9_webm_is_read_as_ten_bit() {
57 let (meta, demuxer) = Demuxer::open(&asset("test_vp9_10.webm")).expect("open a profile 2 webm");
58 assert_eq!(meta.codec, Codec::Vp9);
59 assert_eq!(demuxer.bit_depth(), 10, "profile 2 is 10-bit");
60
61 let (_, eight) = Demuxer::open(&asset("test_vp9.webm")).expect("open a profile 0 webm");
62 assert_eq!(eight.bit_depth(), 8, "profile 0 is 8-bit by definition");
63
64 // ...and the probe put the read cursor back where it opened: frame 0 is the
65 // first picture out, not frame 1.
66 let (meta, mut demuxer) = Demuxer::open(&asset("test_vp9.webm")).expect("open");
67 let mut count = 0;
68 while demuxer.next_access_unit().expect("read").is_some() {
69 count += 1;
70 }
71 assert_eq!(
72 count, meta.frame_count,
73 "the depth probe ate an access unit"
74 );
75}
76
77/// The audio half, and the claim a refusal makes: every codec the "cannot be
78/// decoded" notice names as readable really opens, per container. It read "AAC
79/// and AC-3 only" while FLAC, MP3, Vorbis, ALAC and PCM all decoded out of a
80/// Matroska file — a refusal that undersells sends a user re-encoding a film
81/// whose sound already plays.
82#[test]
83fn every_audio_codec_the_refusal_names_really_decodes() {
84 // The five in one fixture, in the order `gen_fixtures.sh` muxes them, plus
85 // the AAC and AC-3 the older fixtures carry.
86 for (stream, codec) in [
87 (0, "flac"),
88 (1, "mp3"),
89 (2, "vorbis"),
90 (3, "alac"),
91 (4, "pcm"),
92 ] {
93 let probe = AudioSession::probe(asset("test_mkv_audio.mkv"), stream)
94 .unwrap_or_else(|e| panic!("{codec} in a Matroska file: {e}"))
95 .unwrap_or_else(|| panic!("{codec} in a Matroska file came back silent"));
96 assert_eq!(probe.sample_rate, 44_100, "{codec}");
97 }
98 // Opus, every container it arrives in: a `.webm` off the web, the `.mka` a
99 // 5.1 film soundtrack is remuxed into, and the standalone `.opus`. It was
100 // the last codec the notice named as absent while both readers here already
101 // *parsed* it -- `A_OPUS` and `OpusHead` have always mapped to symphonia's
102 // `CODEC_ID_OPUS`; only the decoder was missing, and it is `ec-opus` now.
103 for (file, what, channels) in [
104 ("test_vp9.webm", "opus in a webm", 1),
105 // 5.1 arrives as the stereo fold, exactly as a 5.1 AC-3 track does.
106 ("test_opus_51.mka", "5.1 opus in an mka", 2),
107 // ...and 7.1, the widest layout the fold has a table for and the one
108 // a real remux carries.
109 ("test_opus_71.mka", "7.1 opus in an mka", 2),
110 ("test_tone.opus", "a standalone opus", 2),
111 ] {
112 let probe = AudioSession::probe(asset(file), 0)
113 .unwrap_or_else(|e| panic!("{what}: {e}"))
114 .unwrap_or_else(|| panic!("{what} came back silent"));
115 assert_eq!(probe.sample_rate, 48_000, "{what}");
116 assert_eq!(probe.channels, channels, "{what}");
117 }
118 for (file, codec) in [("test_av1.mkv", "aac"), ("test_ac3.mkv", "ac-3")] {
119 assert!(
120 AudioSession::probe(asset(file), 0).expect(codec).is_some(),
121 "{codec} in a Matroska file came back silent"
122 );
123 }
124 for (file, codec) in [("test_av.mp4", "aac-lc"), ("test_ac3.mp4", "ac-3")] {
125 assert!(
126 AudioSession::probe(asset(file), 0).expect(codec).is_some(),
127 "{codec} in an mp4 came back silent"
128 );
129 }
130
131 // ...and the gap the refusal is really *for*, which is DTS and no longer
132 // Opus: no decoder in this tree has it, so the notice names the track and
133 // then names what would have worked -- Opus among them, or the refusal is
134 // back to underselling the engine.
135 let refused = AudioSession::unsupported(asset("test_dts.mkv"))
136 .expect("read the header")
137 .expect("a DTS track is a reason, not silence");
138 assert!(refused.contains("A_DTS"), "{refused}");
139 for named in ["FLAC", "MP3", "Opus", "Vorbis"] {
140 assert!(
141 refused.contains(named),
142 "the refusal hides a codec that decodes ({named}): {refused}"
143 );
144 }
145 // The other half of that claim, and the one a stale refusal string cannot
146 // fake: a file whose track really does decode has nothing to excuse.
147 assert_eq!(
148 AudioSession::unsupported(asset("test_vp9.webm")).expect("read the header"),
149 None,
150 "an Opus track that decodes is not a refusal"
151 );
152}
153
154/// Which of two video tracks an mp4 opens on may not depend on hash order: it is
155/// the first in `moov.traks`, file order, the same box the audio side numbers its
156/// streams out of. A `HashMap` walk here made a saved `.edith` reopen on the
157/// other picture from one launch to the next.
158#[test]
159fn the_video_track_of_an_mp4_is_the_first_one_in_file_order() {
160 let path = asset("test_two_video.mp4");
161 let file = std::fs::File::open(&path).expect("open the fixture");
162 let size = file.metadata().expect("stat").len();
163 let reader = mp4::Mp4Reader::read_header(std::io::BufReader::new(file), size).expect("header");
164
165 // What file order says, read straight off the box rather than through the
166 // map: the test may not share the code it is checking.
167 let first = reader
168 .moov
169 .traks
170 .iter()
171 .find(|trak| {
172 matches!(
173 mp4::TrackType::try_from(&trak.mdia.hdlr.handler_type),
174 Ok(mp4::TrackType::Video)
175 )
176 })
177 .expect("the fixture has two video tracks");
178 let expected = (
179 u32::from(first.tkhd.width.value()),
180 u32::from(first.tkhd.height.value()),
181 );
182 assert_eq!(
183 expected,
184 (1280, 720),
185 "the fixture is the one it says it is"
186 );
187
188 // Repeated, because the failure this guards against is a *random* one: the
189 // hash order that picked the 320x240 track is a different order each run.
190 for _ in 0..8 {
191 let (meta, _) = Demuxer::open(&path).expect("open the two-track fixture");
192 assert_eq!(
193 (meta.width, meta.height),
194 expected,
195 "the demuxer opened the second video track"
196 );
197 }
198}