forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

audio_bit_depth.rs203 lines, 5.3 KiBHistoryRawDownload
1//! The sample depth a probe states (`StreamFacts::bit_depth`), checked against
2//! ffprobe for FLAC and ALAC at 16 and 24 bits in every container that carries
3//! them (bare `.flac` or `.m4a`, mp4 and Matroska), for PCM at 16 and 24 bits in
4//! WAV and Matroska, and against `None` for AAC, which is lossy.
5//!
6//! ffprobe's field is `bits_per_raw_sample` for FLAC and ALAC. For PCM ffprobe
7//! fills `bits_per_sample` and leaves `bits_per_raw_sample` out, so PCM is
8//! compared on `bits_per_sample`. A field that is `N/A` or `0` is `None`.
9//! Fixtures are generated into `CARGO_TARGET_TMPDIR`, each about 0.3 s long.
10
11use std::path::{Path, PathBuf};
12use std::process::Command;
13
14use engine::{StreamKind, probe_media};
15
16/// One fixture: its name, the ffmpeg codec arguments, the ffprobe field that
17/// states its depth, and the depth the fixture is meant to carry.
18struct Case {
19 name: &'static str,
20 codec: &'static [&'static str],
21 field: &'static str,
22 expect: Option<u32>,
23}
24
25const FLAC16: &[&str] = &["-c:a", "flac", "-sample_fmt", "s16"];
26const FLAC24: &[&str] = &[
27 "-c:a",
28 "flac",
29 "-sample_fmt",
30 "s32",
31 "-bits_per_raw_sample",
32 "24",
33];
34const ALAC16: &[&str] = &["-c:a", "alac", "-sample_fmt", "s16p"];
35const ALAC24: &[&str] = &["-c:a", "alac", "-sample_fmt", "s32p"];
36const PCM16: &[&str] = &["-c:a", "pcm_s16le"];
37const PCM24: &[&str] = &["-c:a", "pcm_s24le"];
38const AAC: &[&str] = &["-c:a", "aac"];
39
40const RAW: &str = "bits_per_raw_sample";
41const PCM: &str = "bits_per_sample";
42
43const CASES: &[Case] = &[
44 Case {
45 name: "flac16.flac",
46 codec: FLAC16,
47 field: RAW,
48 expect: Some(16),
49 },
50 Case {
51 name: "flac24.flac",
52 codec: FLAC24,
53 field: RAW,
54 expect: Some(24),
55 },
56 Case {
57 name: "flac16.mp4",
58 codec: FLAC16,
59 field: RAW,
60 expect: Some(16),
61 },
62 Case {
63 name: "flac24.mp4",
64 codec: FLAC24,
65 field: RAW,
66 expect: Some(24),
67 },
68 Case {
69 name: "flac16.mkv",
70 codec: FLAC16,
71 field: RAW,
72 expect: Some(16),
73 },
74 Case {
75 name: "flac24.mkv",
76 codec: FLAC24,
77 field: RAW,
78 expect: Some(24),
79 },
80 Case {
81 name: "alac16.m4a",
82 codec: ALAC16,
83 field: RAW,
84 expect: Some(16),
85 },
86 Case {
87 name: "alac24.m4a",
88 codec: ALAC24,
89 field: RAW,
90 expect: Some(24),
91 },
92 Case {
93 name: "alac16.mkv",
94 codec: ALAC16,
95 field: RAW,
96 expect: Some(16),
97 },
98 Case {
99 name: "alac24.mkv",
100 codec: ALAC24,
101 field: RAW,
102 expect: Some(24),
103 },
104 Case {
105 name: "pcm16.wav",
106 codec: PCM16,
107 field: PCM,
108 expect: Some(16),
109 },
110 Case {
111 name: "pcm24.wav",
112 codec: PCM24,
113 field: PCM,
114 expect: Some(24),
115 },
116 Case {
117 name: "pcm16.mkv",
118 codec: PCM16,
119 field: PCM,
120 expect: Some(16),
121 },
122 Case {
123 name: "pcm24.mkv",
124 codec: PCM24,
125 field: PCM,
126 expect: Some(24),
127 },
128 Case {
129 name: "aac.m4a",
130 codec: AAC,
131 field: RAW,
132 expect: None,
133 },
134];
135
136fn make(path: &Path, codec: &[&str]) {
137 let status = Command::new("ffmpeg")
138 .args(["-y", "-loglevel", "error", "-f", "lavfi", "-i"])
139 .arg("sine=f=440:d=0.3")
140 .args(codec)
141 .arg(path)
142 .status()
143 .expect("run ffmpeg");
144 assert!(status.success(), "ffmpeg failed for {}", path.display());
145}
146
147/// The first audio stream's `field` as ffprobe reports it; `None` for `N/A` or 0.
148fn ffprobe_depth(path: &Path, field: &str) -> Option<u32> {
149 let out = Command::new("ffprobe")
150 .args(["-v", "error", "-select_streams", "a:0", "-show_entries"])
151 .arg(format!("stream={field}"))
152 .args(["-of", "csv=p=0"])
153 .arg(path)
154 .output()
155 .expect("run ffprobe");
156 assert!(
157 out.status.success(),
158 "ffprobe failed for {}",
159 path.display()
160 );
161 String::from_utf8_lossy(&out.stdout)
162 .trim()
163 .parse::<u32>()
164 .ok()
165 .filter(|d| *d > 0)
166}
167
168/// The first audio stream's `bit_depth` as the engine's probe states it.
169fn probed_depth(path: &Path) -> Option<u32> {
170 probe_media(path)
171 .streams
172 .into_iter()
173 .find(|s| s.kind == StreamKind::Audio)
174 .and_then(|s| s.bit_depth)
175}
176
177#[test]
178fn bit_depth_matches_ffprobe_for_lossless_and_is_none_for_aac() {
179 let dir: PathBuf = Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("audio_bit_depth-{}", std::process::id()));
180 std::fs::create_dir_all(&dir).expect("make the test dir");
181 let mut failures = Vec::new();
182 for case in CASES {
183 let path = dir.join(case.name);
184 make(&path, case.codec);
185 let want = ffprobe_depth(&path, case.field);
186 // The fixture must carry the depth it is named for, or the comparison
187 // below proves nothing.
188 assert_eq!(
189 want, case.expect,
190 "ffprobe {} for {}",
191 case.field, case.name
192 );
193 let got = probed_depth(&path);
194 if got != want {
195 failures.push(format!("{}: engine {got:?}, ffprobe {want:?}", case.name));
196 }
197 }
198 assert!(
199 failures.is_empty(),
200 "bit_depth mismatches:\n{}",
201 failures.join("\n")
202 );
203}