forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB00c1b3fTahinli2 months ago1999 commits

library.rs140 lines, 4.6 KiBHistoryRawDownload
1//! Walks a real Blu-ray remux's TrueHD track and parses every access unit --
2//! a live discovery over `~/Downloads`, absent files skip loudly, nothing is
3//! generated or bundled.
4
5use std::fs::File;
6use std::io::BufReader;
7use std::path::PathBuf;
8use std::process::Command;
9
10use ec_core::{CodecId, Demuxer};
11use ec_matroska::MatroskaDemuxer;
12use ec_truehd::sync::AccessUnitHeader;
13
14/// The user's own library, not a fixture: a glob over `~/Downloads` for a
15/// known 7.1 TrueHD Blu-ray remux.
16fn find_file() -> Option<PathBuf> {
17 let home = std::env::var("HOME").ok()?;
18 let downloads = PathBuf::from(home).join("Downloads");
19 for entry in glob_dir(&downloads) {
20 let name = entry.file_name()?.to_str()?;
21 if name.contains("ReelR") && name.ends_with(".mkv") {
22 return Some(entry);
23 }
24 }
25 None
26}
27
28/// A one-level-deep, non-recursive-but-good-enough directory walk (the file
29/// this test wants sits in a subdirectory of `~/Downloads`) -- no `glob`
30/// crate in this family, so a plain `read_dir` walk stands in for one.
31fn glob_dir(dir: &std::path::Path) -> Vec<PathBuf> {
32 let mut out = Vec::new();
33 let Ok(entries) = std::fs::read_dir(dir) else {
34 return out;
35 };
36 for entry in entries.flatten() {
37 let path = entry.path();
38 if path.is_dir() {
39 out.extend(glob_dir(&path));
40 } else {
41 out.push(path);
42 }
43 }
44 out
45}
46
47fn have_ffprobe() -> bool {
48 Command::new("ffprobe")
49 .arg("-version")
50 .output()
51 .map(|o| o.status.success())
52 .unwrap_or(false)
53}
54
55/// ffprobe's own packet count for the file's TrueHD stream, by absolute
56/// stream index.
57fn ffprobe_packet_count(path: &std::path::Path, stream: usize) -> Option<u64> {
58 let out = Command::new("ffprobe")
59 .args(["-v", "error", "-select_streams"])
60 .arg(stream.to_string())
61 .args([
62 "-count_packets",
63 "-show_entries",
64 "stream=nb_read_packets",
65 "-of",
66 "csv=p=0",
67 ])
68 .arg(path)
69 .output()
70 .ok()?;
71 if !out.status.success() {
72 return None;
73 }
74 String::from_utf8_lossy(&out.stdout).trim().parse().ok()
75}
76
77#[test]
78fn walks_every_truehd_access_unit_of_a_real_7_1_remux() {
79 let Some(path) = find_file() else {
80 eprintln!("skip: no 'ReelR ... TrueHD ...mkv' under ~/Downloads");
81 return;
82 };
83
84 let file = File::open(&path).expect("open the file");
85 let mut demux = MatroskaDemuxer::new(BufReader::new(file)).expect("open the container");
86
87 let track = demux
88 .streams()
89 .iter()
90 .find(|s| s.params.codec == CodecId::TrueHd)
91 .expect("a TrueHD track");
92 let stream_index = track.index;
93 let audio = track.params.audio().expect("audio parameters");
94 assert_eq!(audio.sample_rate, 48_000, "rate");
95 assert_eq!(audio.layout.channel_count(), 8, "channels");
96 // Matroska's `BitDepth` element is optional and this remux's TrueHD
97 // track does not carry one; report what the container states rather
98 // than asserting a value it never claimed.
99 eprintln!("container-reported bit depth: {:?}", audio.bits_per_sample);
100
101 let mut au_count = 0u64;
102 let mut major_syncs = 0u64;
103 loop {
104 let packet = match demux.next_packet() {
105 Ok(p) => p,
106 Err(e) if e.is_eof() => break,
107 Err(e) => panic!("demux error: {e}"),
108 };
109 if packet.stream != stream_index {
110 continue;
111 }
112 let header = AccessUnitHeader::parse(&packet.data)
113 .unwrap_or_else(|e| panic!("access unit {au_count}: {e}"));
114 assert_eq!(
115 header.length,
116 packet.data.len(),
117 "access unit {au_count} claims a length that does not match its packet"
118 );
119 if header.major_sync.is_some() {
120 major_syncs += 1;
121 }
122 au_count += 1;
123 }
124
125 eprintln!("{au_count} access units, {major_syncs} carrying a major sync");
126 assert!(au_count > 0, "found no TrueHD access units at all");
127
128 if have_ffprobe() {
129 // Absolute demuxer stream index in the file, for ffprobe's own
130 // `-select_streams`: the track this family's stream list found.
131 let absolute = stream_index as usize;
132 if let Some(expected) = ffprobe_packet_count(&path, absolute) {
133 assert_eq!(au_count, expected, "AU count vs ffprobe's packet count");
134 } else {
135 eprintln!("skip: ffprobe packet count unavailable for stream {absolute}");
136 }
137 } else {
138 eprintln!("skip: ffprobe not on PATH, no oracle comparison");
139 }
140}