Merge branch 'simplify/g1-audio' into simplify/g1
Browse files at this commit1 changed file +77 -12
- tools/golden/src/audio.rs +77 -12
tools/golden/src/audio.rsmodified+77 -12
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | use crate::{error, guard, hash, name}; |
| 2 | 2 | use ec_ac3::{Ac3Decoder, Downmix, Options}; |
| 3 | 3 | use ec_core::{AudioFrame, MediaType}; |
| 4 | +use ec_opus::Decoder as OpusDecoder; | |
| 4 | 5 | use ec_probe::Reader; |
| 5 | 6 | use std::path::{Path, PathBuf}; |
| 6 | 7 | |
| @@ -13,6 +14,7 @@ const MODES: [(&str, Downmix); 3] = [ | ||
| 13 | 14 | ]; |
| 14 | 15 | |
| 15 | 16 | type Keep = fn(&str) -> bool; |
| 17 | +type Units = fn(&[u8]) -> Vec<(usize, usize)>; | |
| 16 | 18 | |
| 17 | 19 | pub fn run() { |
| 18 | 20 | probe("aac", aac_files()); |
| @@ -29,8 +31,8 @@ pub fn run() { | ||
| 29 | 31 | n.ends_with(".dts") || n.ends_with(".dtshd") |
| 30 | 32 | })); |
| 31 | 33 | 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); | |
| 34 | 36 | } |
| 35 | 37 | } |
| 36 | 38 | |
| @@ -55,6 +57,10 @@ fn vector(n: &str) -> bool { | ||
| 55 | 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 | 64 | fn aac_files() -> Vec<PathBuf> { |
| 59 | 65 | let mut found = listed("audio", |n| n.starts_with("aac")); |
| 60 | 66 | found.extend(listed("open/fate/aac", |_| true)); |
| @@ -69,8 +75,8 @@ fn mp3_files() -> Vec<PathBuf> { | ||
| 69 | 75 | |
| 70 | 76 | fn opus_files() -> Vec<PathBuf> { |
| 71 | 77 | 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)); | |
| 74 | 80 | found |
| 75 | 81 | } |
| 76 | 82 | |
| @@ -121,6 +127,9 @@ fn probe(codec: &str, files: Vec<PathBuf>) { | ||
| 121 | 127 | } |
| 122 | 128 | |
| 123 | 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 | 133 | let mut reader = match Reader::open(path) { |
| 125 | 134 | Ok(reader) => reader, |
| 126 | 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 | 209 | for path in files.iter().take(FILES) { |
| 165 | 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)); | |
| 167 | 212 | } |
| 168 | 213 | } |
| 169 | 214 | |
| 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) { | |
| 171 | 233 | let data = match std::fs::read(path) { |
| 172 | 234 | Ok(data) => data, |
| 173 | 235 | Err(err) => return error(base, err), |
| @@ -176,10 +238,7 @@ fn ac3_file(base: &str, path: &Path, downmix: Downmix) { | ||
| 176 | 238 | downmix, |
| 177 | 239 | ..Options::default() |
| 178 | 240 | }); |
| 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() { | |
| 183 | 242 | let case = format!("{base}/{index}"); |
| 184 | 243 | match decoder.decode_packet(&data[start..start + len]) { |
| 185 | 244 | Ok(frame) => hash(&case, &frame_bytes(&frame)), |
| @@ -257,9 +316,15 @@ fn strmdata(buf: &[u8]) -> &[u8] { | ||
| 257 | 316 | } |
| 258 | 317 | |
| 259 | 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 | 325 | let mut found = Vec::new(); |
| 261 | 326 | let mut pos = 0; |
| 262 | - while pos < data.len() && found.len() < PACKETS { | |
| 327 | + while pos < data.len() { | |
| 263 | 328 | match size(&data[pos..]) { |
| 264 | 329 | Some(len) if len > 0 && pos + len <= data.len() => { |
| 265 | 330 | found.push((pos, len)); |