forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBef5832fTahinli3 months ago597 commits

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