forge
Explore
Log in

Don't you dare go Hollow

tools/golden audio: opus_demo vectors, E-AC-3 dependent substreams grouped per packet

Browse files at this commit

acead95d8e467000b4f0cebe6a9083fe3e627dc2Ahmet Kaan GümüşParent: 787f323
tools/golden/src/audio.rsmodified+77 -12
@@ -1,6 +1,7 @@
11 use crate::{error, guard, hash, name};
22 use ec_ac3::{Ac3Decoder, Downmix, Options};
33 use ec_core::{AudioFrame, MediaType};
4+use ec_opus::Decoder as OpusDecoder;
45 use ec_probe::Reader;
56 use std::path::{Path, PathBuf};
67
@@ -13,6 +14,7 @@ const MODES: [(&str, Downmix); 3] = [
1314 ];
1415
1516 type Keep = fn(&str) -> bool;
17+type Units = fn(&[u8]) -> Vec<(usize, usize)>;
1618
1719 pub fn run() {
1820 probe("aac", aac_files());
@@ -29,8 +31,8 @@ pub fn run() {
2931 n.ends_with(".dts") || n.ends_with(".dtshd")
3032 }));
3133 for (mode, downmix) in MODES {
32- ac3("ac3", mode, downmix, ac3_files());
33- ac3("eac3", mode, downmix, eac3_files());
34+ ac3("ac3", mode, downmix, ac3_files(), ac3_units);
35+ ac3("eac3", mode, downmix, eac3_files(), eac3_units);
3436 }
3537 }
3638
@@ -55,6 +57,10 @@ fn vector(n: &str) -> bool {
5557 !n.ends_with(".md5") && !n.ends_with(".txt")
5658 }
5759
60+fn bit(n: &str) -> bool {
61+ n.ends_with(".bit")
62+}
63+
5864 fn aac_files() -> Vec<PathBuf> {
5965 let mut found = listed("audio", |n| n.starts_with("aac"));
6066 found.extend(listed("open/fate/aac", |_| true));
@@ -69,8 +75,8 @@ fn mp3_files() -> Vec<PathBuf> {
6975
7076 fn opus_files() -> Vec<PathBuf> {
7177 let mut found = listed("audio", |n| n.starts_with("opus"));
72- found.extend(listed("vectors/opus-rfc6716/opus_testvectors", vector));
73- found.extend(listed("vectors/opus-rfc8251/opus_newvectors", vector));
78+ found.extend(listed("vectors/opus-rfc6716/opus_testvectors", bit));
79+ found.extend(listed("vectors/opus-rfc8251/opus_newvectors", bit));
7480 found
7581 }
7682
@@ -121,6 +127,9 @@ fn probe(codec: &str, files: Vec<PathBuf>) {
121127 }
122128
123129 fn probe_file(base: &str, path: &Path) {
130+ if path.extension().is_some_and(|e| e == "bit") {
131+ return opus_vector(base, path);
132+ }
124133 let mut reader = match Reader::open(path) {
125134 Ok(reader) => reader,
126135 Err(err) => return error(base, err),
@@ -160,14 +169,67 @@ fn probe_file(base: &str, path: &Path) {
160169 }
161170 }
162171
163-fn ac3(codec: &str, mode: &str, downmix: Downmix, files: Vec<PathBuf>) {
172+fn opus_vector(base: &str, path: &Path) {
173+ let data = match std::fs::read(path) {
174+ Ok(data) => data,
175+ Err(err) => return error(base, err),
176+ };
177+ let legacy = !path.starts_with(ec_testkit::root().join("vectors/opus-rfc8251"));
178+ let mut decoder = match OpusDecoder::new(48000, 2) {
179+ Ok(decoder) => decoder,
180+ Err(err) => return error(base, err),
181+ };
182+ decoder.set_legacy_rfc6716(legacy);
183+ let mut buf = vec![0.0f32; 5760 * 2];
184+ for (index, packet) in opus_packets(&data).into_iter().take(PACKETS).enumerate() {
185+ let case = format!("{base}/{index}");
186+ match decoder.decode_float(packet, &mut buf) {
187+ Ok(n) => hash(&case, &f32_bytes(&buf[..n * 2])),
188+ Err(err) => error(&case, err),
189+ }
190+ }
191+}
192+
193+fn opus_packets(data: &[u8]) -> Vec<&[u8]> {
194+ let mut found = Vec::new();
195+ let mut pos = 0;
196+ while pos + 8 <= data.len() {
197+ let len = u32::from_be_bytes(data[pos..pos + 4].try_into().unwrap()) as usize;
198+ pos += 8;
199+ if pos + len > data.len() {
200+ break;
201+ }
202+ found.push(&data[pos..pos + len]);
203+ pos += len;
204+ }
205+ found
206+}
207+
208+fn ac3(codec: &str, mode: &str, downmix: Downmix, files: Vec<PathBuf>, units: Units) {
164209 for path in files.iter().take(FILES) {
165210 let base = format!("audio/{codec}-{mode}/{}", name(path));
166- guard(&base, || ac3_file(&base, path, downmix));
211+ guard(&base, || ac3_file(&base, path, downmix, units));
167212 }
168213 }
169214
170-fn ac3_file(base: &str, path: &Path, downmix: Downmix) {
215+fn ac3_units(data: &[u8]) -> Vec<(usize, usize)> {
216+ spans(data, |d| ec_ac3::frame_size(d).ok())
217+}
218+
219+fn eac3_units(data: &[u8]) -> Vec<(usize, usize)> {
220+ let mut packets: Vec<(usize, usize)> = Vec::new();
221+ for (start, len) in frames(data, |d| ec_ac3::frame_size(d).ok()) {
222+ let dependent = data.get(start + 2).is_some_and(|b| b >> 6 == 1);
223+ match packets.last_mut() {
224+ Some(last) if dependent => last.1 = start + len - last.0,
225+ _ => packets.push((start, len)),
226+ }
227+ }
228+ packets.truncate(PACKETS);
229+ packets
230+}
231+
232+fn ac3_file(base: &str, path: &Path, downmix: Downmix, units: Units) {
171233 let data = match std::fs::read(path) {
172234 Ok(data) => data,
173235 Err(err) => return error(base, err),
@@ -176,10 +238,7 @@ fn ac3_file(base: &str, path: &Path, downmix: Downmix) {
176238 downmix,
177239 ..Options::default()
178240 });
179- for (index, (start, len)) in spans(&data, |d| ec_ac3::frame_size(d).ok())
180- .into_iter()
181- .enumerate()
182- {
241+ for (index, (start, len)) in units(&data).into_iter().enumerate() {
183242 let case = format!("{base}/{index}");
184243 match decoder.decode_packet(&data[start..start + len]) {
185244 Ok(frame) => hash(&case, &frame_bytes(&frame)),
@@ -257,9 +316,15 @@ fn strmdata(buf: &[u8]) -> &[u8] {
257316 }
258317
259318 fn spans(data: &[u8], size: fn(&[u8]) -> Option<usize>) -> Vec<(usize, usize)> {
319+ let mut found = frames(data, size);
320+ found.truncate(PACKETS);
321+ found
322+}
323+
324+fn frames(data: &[u8], size: fn(&[u8]) -> Option<usize>) -> Vec<(usize, usize)> {
260325 let mut found = Vec::new();
261326 let mut pos = 0;
262- while pos < data.len() && found.len() < PACKETS {
327+ while pos < data.len() {
263328 match size(&data[pos..]) {
264329 Some(len) if len > 0 && pos + len <= data.len() => {
265330 found.push((pos, len));