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 @@
1 use crate::{error, guard, hash, name};1 use crate::{error, guard, hash, name};
2 use ec_ac3::{Ac3Decoder, Downmix, Options};2 use ec_ac3::{Ac3Decoder, Downmix, Options};
3 use ec_core::{AudioFrame, MediaType};3 use ec_core::{AudioFrame, MediaType};
4+use ec_opus::Decoder as OpusDecoder;
4 use ec_probe::Reader;5 use ec_probe::Reader;
5 use std::path::{Path, PathBuf};6 use std::path::{Path, PathBuf};
6 7
@@ -13,6 +14,7 @@ const MODES: [(&str, Downmix); 3] = [
13 ];14 ];
14 15
15 type Keep = fn(&str) -> bool;16 type Keep = fn(&str) -> bool;
17+type Units = fn(&[u8]) -> Vec<(usize, usize)>;
16 18
17 pub fn run() {19 pub fn run() {
18 probe("aac", aac_files());20 probe("aac", aac_files());
@@ -29,8 +31,8 @@ pub fn run() {
29 n.ends_with(".dts") || n.ends_with(".dtshd")31 n.ends_with(".dts") || n.ends_with(".dtshd")
30 }));32 }));
31 for (mode, downmix) in MODES {33 for (mode, downmix) in MODES {
32- ac3("ac3", mode, downmix, ac3_files());34+ ac3("ac3", mode, downmix, ac3_files(), ac3_units);
33- ac3("eac3", mode, downmix, eac3_files());35+ ac3("eac3", mode, downmix, eac3_files(), eac3_units);
34 }36 }
35 }37 }
36 38
@@ -55,6 +57,10 @@ fn vector(n: &str) -> bool {
55 !n.ends_with(".md5") && !n.ends_with(".txt")57 !n.ends_with(".md5") && !n.ends_with(".txt")
56 }58 }
57 59
60+fn bit(n: &str) -> bool {
61+ n.ends_with(".bit")
62+}
63+
58 fn aac_files() -> Vec<PathBuf> {64 fn aac_files() -> Vec<PathBuf> {
59 let mut found = listed("audio", |n| n.starts_with("aac"));65 let mut found = listed("audio", |n| n.starts_with("aac"));
60 found.extend(listed("open/fate/aac", |_| true));66 found.extend(listed("open/fate/aac", |_| true));
@@ -69,8 +75,8 @@ fn mp3_files() -> Vec<PathBuf> {
69 75
70 fn opus_files() -> Vec<PathBuf> {76 fn opus_files() -> Vec<PathBuf> {
71 let mut found = listed("audio", |n| n.starts_with("opus"));77 let mut found = listed("audio", |n| n.starts_with("opus"));
72- found.extend(listed("vectors/opus-rfc6716/opus_testvectors", vector));78+ found.extend(listed("vectors/opus-rfc6716/opus_testvectors", bit));
73- found.extend(listed("vectors/opus-rfc8251/opus_newvectors", vector));79+ found.extend(listed("vectors/opus-rfc8251/opus_newvectors", bit));
74 found80 found
75 }81 }
76 82
@@ -121,6 +127,9 @@ fn probe(codec: &str, files: Vec<PathBuf>) {
121 }127 }
122 128
123 fn probe_file(base: &str, path: &Path) {129 fn probe_file(base: &str, path: &Path) {
130+ if path.extension().is_some_and(|e| e == "bit") {
131+ return opus_vector(base, path);
132+ }
124 let mut reader = match Reader::open(path) {133 let mut reader = match Reader::open(path) {
125 Ok(reader) => reader,134 Ok(reader) => reader,
126 Err(err) => return error(base, err),135 Err(err) => return error(base, err),
@@ -160,14 +169,67 @@ fn probe_file(base: &str, path: &Path) {
160 }169 }
161 }170 }
162 171
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) {
164 for path in files.iter().take(FILES) {209 for path in files.iter().take(FILES) {
165 let base = format!("audio/{codec}-{mode}/{}", name(path));210 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));
212+ }
213+}
214+
215+fn ac3_units(data: &[u8]) -> Vec<(usize, usize)> {
216+ spans(data, |d| ec_ac3::frame_size(d).ok())
167 }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
168 }230 }
169 231
170-fn ac3_file(base: &str, path: &Path, downmix: Downmix) {232+fn ac3_file(base: &str, path: &Path, downmix: Downmix, units: Units) {
171 let data = match std::fs::read(path) {233 let data = match std::fs::read(path) {
172 Ok(data) => data,234 Ok(data) => data,
173 Err(err) => return error(base, err),235 Err(err) => return error(base, err),
@@ -176,10 +238,7 @@ fn ac3_file(base: &str, path: &Path, downmix: Downmix) {
176 downmix,238 downmix,
177 ..Options::default()239 ..Options::default()
178 });240 });
179- for (index, (start, len)) in spans(&data, |d| ec_ac3::frame_size(d).ok())241+ for (index, (start, len)) in units(&data).into_iter().enumerate() {
180- .into_iter()
181- .enumerate()
182- {
183 let case = format!("{base}/{index}");242 let case = format!("{base}/{index}");
184 match decoder.decode_packet(&data[start..start + len]) {243 match decoder.decode_packet(&data[start..start + len]) {
185 Ok(frame) => hash(&case, &frame_bytes(&frame)),244 Ok(frame) => hash(&case, &frame_bytes(&frame)),
@@ -257,9 +316,15 @@ fn strmdata(buf: &[u8]) -> &[u8] {
257 }316 }
258 317
259 fn spans(data: &[u8], size: fn(&[u8]) -> Option<usize>) -> Vec<(usize, usize)> {318 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)> {
260 let mut found = Vec::new();325 let mut found = Vec::new();
261 let mut pos = 0;326 let mut pos = 0;
262- while pos < data.len() && found.len() < PACKETS {327+ while pos < data.len() {
263 match size(&data[pos..]) {328 match size(&data[pos..]) {
264 Some(len) if len > 0 && pos + len <= data.len() => {329 Some(len) if len > 0 && pos + len <= data.len() => {
265 found.push((pos, len));330 found.push((pos, len));