tools/golden audio: probe decoders (aac mp3 opus vorbis flac alac amr pcm-ext), raw ac3/eac3 x3 downmix, dts, truehd
Browse files at this commitfd19d1c478a8ca7aaa34bef11bebab315fce6cd9Parent: 5b64ff01 changed file +285 -1
- tools/golden/src/audio.rs +285 -1
tools/golden/src/audio.rsmodified+285 -1
| @@ -1 +1,285 @@ | |||
| 1 | -pub fn run() {} | 1 | +use crate::{error, guard, hash, name}; |
| 2 | +use ec_ac3::{Ac3Decoder, Downmix, Options}; | ||
| 3 | +use ec_core::{AudioFrame, MediaType}; | ||
| 4 | +use ec_probe::Reader; | ||
| 5 | +use std::path::{Path, PathBuf}; | ||
| 6 | + | ||
| 7 | +const PACKETS: usize = 200; | ||
| 8 | +const FILES: usize = 40; | ||
| 9 | +const MODES: [(&str, Downmix); 3] = [ | ||
| 10 | + ("native", Downmix::Native), | ||
| 11 | + ("stereo", Downmix::Stereo), | ||
| 12 | + ("mono", Downmix::Mono), | ||
| 13 | +]; | ||
| 14 | + | ||
| 15 | +type Keep = fn(&str) -> bool; | ||
| 16 | + | ||
| 17 | +pub fn run() { | ||
| 18 | + probe("aac", aac_files()); | ||
| 19 | + probe("mp3", mp3_files()); | ||
| 20 | + probe("opus", opus_files()); | ||
| 21 | + probe("vorbis", vorbis_files()); | ||
| 22 | + probe("flac", flac_files()); | ||
| 23 | + probe("alac", listed("audio", |n| n.starts_with("alac"))); | ||
| 24 | + probe("amr-nb", listed("codecs", |n| n.starts_with("amr-nb"))); | ||
| 25 | + probe("amr-wb", listed("codecs", |n| n.starts_with("amr-wb"))); | ||
| 26 | + probe("pcm-ext", listed("audio", |n| n.ends_with(".wav"))); | ||
| 27 | + truehd(listed("open/fate/truehd", |n| n.ends_with(".thd"))); | ||
| 28 | + dts(listed("open/fate/dts", |n| { | ||
| 29 | + n.ends_with(".dts") || n.ends_with(".dtshd") | ||
| 30 | + })); | ||
| 31 | + for (mode, downmix) in MODES { | ||
| 32 | + ac3("ac3", mode, downmix, ac3_files()); | ||
| 33 | + ac3("eac3", mode, downmix, eac3_files()); | ||
| 34 | + } | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +fn listed(dir: &str, keep: Keep) -> Vec<PathBuf> { | ||
| 38 | + let root = ec_testkit::root().join(dir); | ||
| 39 | + if !root.is_dir() { | ||
| 40 | + eprintln!("golden audio: skipping absent {dir}"); | ||
| 41 | + return Vec::new(); | ||
| 42 | + } | ||
| 43 | + let mut found: Vec<PathBuf> = std::fs::read_dir(&root) | ||
| 44 | + .into_iter() | ||
| 45 | + .flatten() | ||
| 46 | + .flatten() | ||
| 47 | + .map(|entry| entry.path()) | ||
| 48 | + .filter(|path| path.is_file()) | ||
| 49 | + .filter(|path| keep(path.file_name().and_then(|n| n.to_str()).unwrap_or(""))) | ||
| 50 | + .collect(); | ||
| 51 | + found.sort(); | ||
| 52 | + found | ||
| 53 | +} | ||
| 54 | + | ||
| 55 | +fn vector(n: &str) -> bool { | ||
| 56 | + !n.ends_with(".md5") && !n.ends_with(".txt") | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +fn aac_files() -> Vec<PathBuf> { | ||
| 60 | + let mut found = listed("audio", |n| n.starts_with("aac")); | ||
| 61 | + found.extend(listed("open/fate/aac", |_| true)); | ||
| 62 | + found | ||
| 63 | +} | ||
| 64 | + | ||
| 65 | +fn mp3_files() -> Vec<PathBuf> { | ||
| 66 | + listed("audio", |n| { | ||
| 67 | + n.ends_with(".mp1") || n.ends_with(".mp2") || n.ends_with(".mp3") | ||
| 68 | + }) | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +fn opus_files() -> Vec<PathBuf> { | ||
| 72 | + let mut found = listed("audio", |n| n.starts_with("opus")); | ||
| 73 | + found.extend(listed("vectors/opus-rfc6716/opus_testvectors", vector)); | ||
| 74 | + found.extend(listed("vectors/opus-rfc8251/opus_newvectors", vector)); | ||
| 75 | + found | ||
| 76 | +} | ||
| 77 | + | ||
| 78 | +fn vorbis_files() -> Vec<PathBuf> { | ||
| 79 | + let mut found = listed("audio", |n| n.starts_with("vorbis")); | ||
| 80 | + found.extend(listed("vectors/vorbis-xiph", vector)); | ||
| 81 | + found | ||
| 82 | +} | ||
| 83 | + | ||
| 84 | +fn flac_files() -> Vec<PathBuf> { | ||
| 85 | + let mut found = listed("audio", |n| n.starts_with("flac")); | ||
| 86 | + found.extend(listed( | ||
| 87 | + "vectors/flac-xiph/flac-test-files-main/subset", | ||
| 88 | + vector, | ||
| 89 | + )); | ||
| 90 | + found.extend(listed( | ||
| 91 | + "vectors/flac-xiph/flac-test-files-main/uncommon", | ||
| 92 | + vector, | ||
| 93 | + )); | ||
| 94 | + found.extend(listed( | ||
| 95 | + "vectors/flac-xiph/flac-test-files-main/faulty", | ||
| 96 | + vector, | ||
| 97 | + )); | ||
| 98 | + found | ||
| 99 | +} | ||
| 100 | + | ||
| 101 | +fn ac3_files() -> Vec<PathBuf> { | ||
| 102 | + let mut found = listed("audio", |n| n.starts_with("ac3-") && n.ends_with(".ac3")); | ||
| 103 | + found.extend(listed("open/fate/ac3", |n| n.ends_with(".ac3"))); | ||
| 104 | + found | ||
| 105 | +} | ||
| 106 | + | ||
| 107 | +fn eac3_files() -> Vec<PathBuf> { | ||
| 108 | + let mut found = listed("audio", |n| { | ||
| 109 | + (n.starts_with("eac3") || n.starts_with("ac3dep")) && n.ends_with(".eac3") | ||
| 110 | + }); | ||
| 111 | + found.extend(listed("open/fate/eac3", |n| { | ||
| 112 | + n.ends_with(".eac3") || n.ends_with(".ec3") | ||
| 113 | + })); | ||
| 114 | + found | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +fn probe(codec: &str, files: Vec<PathBuf>) { | ||
| 118 | + for path in files.iter().take(FILES) { | ||
| 119 | + let base = format!("audio/{codec}/{}", name(path)); | ||
| 120 | + guard(&base, || probe_file(&base, path)); | ||
| 121 | + } | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +fn probe_file(base: &str, path: &Path) { | ||
| 125 | + let mut reader = match Reader::open(path) { | ||
| 126 | + Ok(reader) => reader, | ||
| 127 | + Err(err) => return error(base, err), | ||
| 128 | + }; | ||
| 129 | + let Some(stream) = reader.default_stream(MediaType::Audio).map(|s| s.index) else { | ||
| 130 | + return error(base, "no audio stream"); | ||
| 131 | + }; | ||
| 132 | + let mut decoder = match reader.make_decoder(stream) { | ||
| 133 | + Ok(decoder) => decoder, | ||
| 134 | + Err(err) => return error(base, err), | ||
| 135 | + }; | ||
| 136 | + let mut pcm = Vec::new(); | ||
| 137 | + let mut index = 0; | ||
| 138 | + while index < PACKETS { | ||
| 139 | + let packet = match reader.next_packet() { | ||
| 140 | + Ok(packet) => packet, | ||
| 141 | + Err(err) if err.is_eof() => break, | ||
| 142 | + Err(err) => { | ||
| 143 | + error(base, err); | ||
| 144 | + break; | ||
| 145 | + } | ||
| 146 | + }; | ||
| 147 | + if packet.stream != stream { | ||
| 148 | + continue; | ||
| 149 | + } | ||
| 150 | + let case = format!("{base}/{index}"); | ||
| 151 | + match decoder.decode(&packet, &mut pcm) { | ||
| 152 | + Ok(()) => hash(&case, &f32_bytes(&pcm)), | ||
| 153 | + Err(err) => error(&case, err), | ||
| 154 | + } | ||
| 155 | + index += 1; | ||
| 156 | + } | ||
| 157 | + let case = format!("{base}/{index}"); | ||
| 158 | + match decoder.flush(&mut pcm) { | ||
| 159 | + Ok(()) => hash(&case, &f32_bytes(&pcm)), | ||
| 160 | + Err(err) => error(&case, err), | ||
| 161 | + } | ||
| 162 | +} | ||
| 163 | + | ||
| 164 | +fn ac3(codec: &str, mode: &str, downmix: Downmix, files: Vec<PathBuf>) { | ||
| 165 | + for path in files.iter().take(FILES) { | ||
| 166 | + let base = format!("audio/{codec}-{mode}/{}", name(path)); | ||
| 167 | + guard(&base, || ac3_file(&base, path, downmix)); | ||
| 168 | + } | ||
| 169 | +} | ||
| 170 | + | ||
| 171 | +fn ac3_file(base: &str, path: &Path, downmix: Downmix) { | ||
| 172 | + let data = match std::fs::read(path) { | ||
| 173 | + Ok(data) => data, | ||
| 174 | + Err(err) => return error(base, err), | ||
| 175 | + }; | ||
| 176 | + let mut decoder = Ac3Decoder::with_options(Options { | ||
| 177 | + downmix, | ||
| 178 | + ..Options::default() | ||
| 179 | + }); | ||
| 180 | + for (index, (start, len)) in spans(&data, |d| ec_ac3::frame_size(d).ok()) | ||
| 181 | + .into_iter() | ||
| 182 | + .enumerate() | ||
| 183 | + { | ||
| 184 | + let case = format!("{base}/{index}"); | ||
| 185 | + match decoder.decode_packet(&data[start..start + len]) { | ||
| 186 | + Ok(frame) => hash(&case, &frame_bytes(&frame)), | ||
| 187 | + Err(err) => error(&case, err), | ||
| 188 | + } | ||
| 189 | + } | ||
| 190 | +} | ||
| 191 | + | ||
| 192 | +fn truehd(files: Vec<PathBuf>) { | ||
| 193 | + for path in files.iter().take(FILES) { | ||
| 194 | + let base = format!("audio/truehd/{}", name(path)); | ||
| 195 | + guard(&base, || truehd_file(&base, path)); | ||
| 196 | + } | ||
| 197 | +} | ||
| 198 | + | ||
| 199 | +fn truehd_file(base: &str, path: &Path) { | ||
| 200 | + let data = match std::fs::read(path) { | ||
| 201 | + Ok(data) => data, | ||
| 202 | + Err(err) => return error(base, err), | ||
| 203 | + }; | ||
| 204 | + let mut decoder = ec_truehd::TrueHdDecoder::new(); | ||
| 205 | + for (index, (start, len)) in spans(&data, |d| ec_truehd::frame_length(d).ok()) | ||
| 206 | + .into_iter() | ||
| 207 | + .enumerate() | ||
| 208 | + { | ||
| 209 | + let case = format!("{base}/{index}"); | ||
| 210 | + match decoder.decode_access_unit(&data[start..start + len]) { | ||
| 211 | + Ok(frame) => hash(&case, &frame.as_ref().map(frame_bytes).unwrap_or_default()), | ||
| 212 | + Err(err) => error(&case, err), | ||
| 213 | + } | ||
| 214 | + } | ||
| 215 | +} | ||
| 216 | + | ||
| 217 | +fn dts(files: Vec<PathBuf>) { | ||
| 218 | + for path in files.iter().take(FILES) { | ||
| 219 | + let base = format!("audio/dts/{}", name(path)); | ||
| 220 | + guard(&base, || dts_file(&base, path)); | ||
| 221 | + } | ||
| 222 | +} | ||
| 223 | + | ||
| 224 | +fn dts_file(base: &str, path: &Path) { | ||
| 225 | + let file = match std::fs::read(path) { | ||
| 226 | + Ok(file) => file, | ||
| 227 | + Err(err) => return error(base, err), | ||
| 228 | + }; | ||
| 229 | + let data = strmdata(&file); | ||
| 230 | + let mut decoder = ec_dts::DtsDecoder::new(); | ||
| 231 | + for (index, (start, len)) in spans(data, |d| ec_dts::frame_size(d).ok()) | ||
| 232 | + .into_iter() | ||
| 233 | + .enumerate() | ||
| 234 | + { | ||
| 235 | + let case = format!("{base}/{index}"); | ||
| 236 | + match decoder.decode_frame(&data[start..start + len]) { | ||
| 237 | + Ok(frame) => hash(&case, &frame_bytes(&frame)), | ||
| 238 | + Err(err) => error(&case, err), | ||
| 239 | + } | ||
| 240 | + } | ||
| 241 | +} | ||
| 242 | + | ||
| 243 | +fn strmdata(buf: &[u8]) -> &[u8] { | ||
| 244 | + if !buf.starts_with(b"DTSHDHDR") { | ||
| 245 | + return buf; | ||
| 246 | + } | ||
| 247 | + let mut p = 0; | ||
| 248 | + while p + 16 <= buf.len() { | ||
| 249 | + let tag = &buf[p..p + 8]; | ||
| 250 | + let size = u64::from_be_bytes(buf[p + 8..p + 16].try_into().unwrap()) as usize; | ||
| 251 | + let end = p.saturating_add(16).saturating_add(size).min(buf.len()); | ||
| 252 | + if tag == b"STRMDATA" { | ||
| 253 | + return &buf[p + 16..end]; | ||
| 254 | + } | ||
| 255 | + p = end; | ||
| 256 | + } | ||
| 257 | + buf | ||
| 258 | +} | ||
| 259 | + | ||
| 260 | +fn spans(data: &[u8], size: fn(&[u8]) -> Option<usize>) -> Vec<(usize, usize)> { | ||
| 261 | + let mut found = Vec::new(); | ||
| 262 | + let mut pos = 0; | ||
| 263 | + while pos < data.len() && found.len() < PACKETS { | ||
| 264 | + match size(&data[pos..]) { | ||
| 265 | + Some(len) if len > 0 && pos + len <= data.len() => { | ||
| 266 | + found.push((pos, len)); | ||
| 267 | + pos += len; | ||
| 268 | + } | ||
| 269 | + _ => pos += 1, | ||
| 270 | + } | ||
| 271 | + } | ||
| 272 | + found | ||
| 273 | +} | ||
| 274 | + | ||
| 275 | +fn f32_bytes(samples: &[f32]) -> Vec<u8> { | ||
| 276 | + samples.iter().flat_map(|s| s.to_le_bytes()).collect() | ||
| 277 | +} | ||
| 278 | + | ||
| 279 | +fn frame_bytes(frame: &AudioFrame) -> Vec<u8> { | ||
| 280 | + frame | ||
| 281 | + .data | ||
| 282 | + .iter() | ||
| 283 | + .flat_map(|plane| plane.iter().copied()) | ||
| 284 | + .collect() | ||
| 285 | +} | ||