forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

h264_mkv.rs149 lines, 5.9 KiBHistoryRawDownload
1//! H.264 in Matroska: the codec this project has always decoded, in the
2//! container that used to refuse it by name. Nothing new decodes here -- the
3//! demuxer reframes the `avcC`-prefixed blocks to Annex-B and hands them to the
4//! same dispatch an mp4's samples go to -- so the whole file runs with nothing
5//! installed, on the software seat (`ec-h264`) if the plugin is absent.
6//!
7//! ```text
8//! cargo test -p engine --test h264_mkv
9//! ```
10
11use std::path::PathBuf;
12use std::time::{Duration, Instant};
13
14use engine::demux::{Codec, Demuxer};
15use engine::{AudioSession, PlaybackSession};
16
17/// 1280x720@30, 2 s, keyframes 30 apart -- see `scripts/gen_fixtures.sh`.
18const FRAMES: u32 = 60;
19const KEYFRAME: u32 = 30;
20
21fn asset(name: &str) -> PathBuf {
22 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
23 .join("../../assets")
24 .join(name)
25}
26
27/// The Annex-B NAL types of an access unit, in order: `nal_unit_type` is the low
28/// 5 bits of the byte after each start code.
29fn nal_types(au: &[u8]) -> Vec<u8> {
30 au.windows(4)
31 .enumerate()
32 .filter(|(_, w)| *w == [0, 0, 0, 1])
33 .filter_map(|(i, _)| au.get(i + 4).map(|b| b & 0x1f))
34 .collect()
35}
36
37/// The container half, and the refusal this slice exists to delete: the file
38/// opens, and it says H.264 rather than "not supported".
39#[test]
40fn the_demuxer_reports_an_h264_track_in_matroska() {
41 let (meta, _) = Demuxer::open(&asset("test_h264.mkv")).expect("open test_h264.mkv");
42 assert_eq!(meta.codec, Codec::H264);
43 assert_eq!((meta.width, meta.height), (1280, 720));
44 assert!(
45 (meta.frame_rate - 30.0).abs() < 1e-6,
46 "DefaultDuration must not truncate: {}",
47 meta.frame_rate
48 );
49 assert_eq!(meta.frame_count, FRAMES);
50
51 // The invariant, stated as an assert: the other containers and codecs read
52 // back exactly as they did before this arm existed.
53 let (av1, _) = Demuxer::open(&asset("test_av1.mkv")).expect("open test_av1.mkv");
54 assert_eq!(av1.codec, Codec::Av1);
55 assert_eq!(av1.frame_count, FRAMES);
56 let (h264, _) = Demuxer::open(&asset("test_baseline.mp4")).expect("open test_baseline.mp4");
57 assert_eq!(h264.codec, Codec::H264);
58}
59
60/// Matroska stores H.264 in `avcC` framing -- length prefixes, and the SPS/PPS
61/// out of band in `CodecPrivate` -- so an access unit is only decodable if both
62/// were handled: reframed to Annex-B, parameter sets ahead of every keyframe.
63#[test]
64fn keyframes_carry_the_parameter_sets_and_every_block_comes_back() {
65 let (_, mut demuxer) = Demuxer::open(&asset("test_h264.mkv")).expect("open test_h264.mkv");
66 let first = demuxer
67 .next_access_unit()
68 .expect("read")
69 .expect("a first access unit");
70 assert_eq!(first[..4], [0, 0, 0, 1], "Annex-B, not a length prefix");
71 let types = nal_types(&first);
72 assert_eq!(
73 &types[..2],
74 &[7, 8],
75 "the keyframe leads with SPS and PPS: {types:?}"
76 );
77 assert!(types.contains(&5), "and then the IDR picture: {types:?}");
78
79 let second = demuxer.next_access_unit().expect("read").expect("a unit");
80 let types = nal_types(&second);
81 assert!(
82 !types.contains(&7) && types.contains(&1),
83 "a non-keyframe is a plain slice: {types:?}"
84 );
85
86 let mut count = 2;
87 while demuxer.next_access_unit().expect("read").is_some() {
88 count += 1;
89 }
90 assert_eq!(count, FRAMES, "every block came back out");
91
92 // Keyed every 30 frames, and a seek may only land on one.
93 assert_eq!(
94 demuxer.seek_to_sync_at_or_before(KEYFRAME + 15),
95 i64::from(KEYFRAME)
96 );
97 assert_eq!(demuxer.seek_to_sync_at_or_before(KEYFRAME - 1), 0);
98 // The unit after a seek carries the parameter sets too: a decoder restarted
99 // mid-file needs them exactly as much as one started at frame 0.
100 let after = demuxer.next_access_unit().expect("read").expect("a unit");
101 assert_eq!(&nal_types(&after)[..2], &[7, 8]);
102}
103
104/// The user-facing end of it, through the very call the window's open door makes
105/// (`app/src/main.rs:2242`): the file becomes a timeline and that timeline shows
106/// a picture. No plugin is required -- H.264 is the one codec with a software
107/// decoder here -- so this is what says the reframed access units are really
108/// decodable and not just well-shaped.
109#[test]
110fn opening_an_h264_matroska_file_shows_frames() {
111 let mut session = PlaybackSession::open(asset("test_h264.mkv")).expect("open test_h264.mkv");
112 assert_eq!(session.meta().codec, Codec::H264);
113 assert_eq!(session.meta().frame_count, FRAMES);
114
115 // The decoder is a thread behind the door, so a frame is waited for exactly
116 // as the window's own pump waits for one.
117 let deadline = Instant::now() + Duration::from_secs(60);
118 let frame = loop {
119 if let Some(frame) = session.try_frame() {
120 break frame;
121 }
122 assert!(Instant::now() < deadline, "no frame in 60 s");
123 std::thread::sleep(Duration::from_millis(10));
124 };
125 assert_eq!((frame.width, frame.height), (1280, 720));
126 // A picture, not a flat surface: a decoder handed garbage would satisfy
127 // every count above.
128 assert!(
129 frame.bgra.as_chunks::<4>().0.iter().any(|px| px != &frame.bgra[..4]),
130 "the first frame is a single colour -- no picture was decoded"
131 );
132}
133
134/// The sound comes with it: a Matroska file's audio is read whatever its picture
135/// is coded with, so nothing is left for the "cannot be decoded" notice to name.
136#[test]
137fn the_matroska_audio_path_takes_this_file_too() {
138 let stereo = AudioSession::probe(asset("test_h264.mkv"), 0)
139 .expect("probe")
140 .expect("the fixture has an AAC track");
141 assert_eq!((stereo.sample_rate, stereo.channels), (44_100, 2));
142 // ...and it really opens, which is what says the sound plays rather than
143 // merely being described.
144 assert!(
145 AudioSession::open(asset("test_h264.mkv"))
146 .expect("open")
147 .is_some()
148 );
149}