//! Layer I/II against ffmpeg's float decoders. //! //! Two kinds of stream: real ones from ffmpeg's `mp2` / `mp2fixed` encoders //! (synthetic sources only: sine, noise, chirp), and random-payload frames with //! legal headers, which reach every allocation code, every scalefactor-select //! mode, the joint-stereo bound, CRC-protected frames and Layer I -- things no //! encoder here produces. The oracle is `ffmpeg -c:a mp2 -i x.mp2 -f f32le`. //! //! cargo test -p ec-mp3 --release --test mp2_matrix -- --nocapture use std::path::PathBuf; use ec_mp3::header::Layer; use ec_mp3::{FrameHeader, Mp3Reader}; fn work_dir() -> PathBuf { let d = std::env::temp_dir().join("ec-mp2-matrix"); std::fs::create_dir_all(&d).unwrap(); d } fn ffmpeg_decode(path: &std::path::Path, channels: usize, layer: Layer) -> Vec { let decoder = if layer == Layer::I { "mp1float" } else { "mp2float" }; let out = ec_testkit::ffmpeg() .args(["-v", "error", "-c:a", decoder, "-i"]) .arg(path) .args(["-ac", &channels.to_string(), "-f", "f32le", "-"]) .output() .expect("ffmpeg"); assert!( out.status.success(), "ffmpeg: {}", String::from_utf8_lossy(&out.stderr) ); out.stdout .as_chunks::<4>() .0 .iter() .map(|b| f32::from_le_bytes(*b)) .collect() } /// ffmpeg's fixed-point decoder (the default `mp2`/`mp1`): s16 out. fn ffmpeg_decode_s16(path: &std::path::Path, channels: usize, layer: Layer) -> Vec { let decoder = if layer == Layer::I { "mp1" } else { "mp2" }; let out = ec_testkit::ffmpeg() .args(["-v", "error", "-c:a", decoder, "-i"]) .arg(path) .args(["-ac", &channels.to_string(), "-f", "s16le", "-"]) .output() .expect("ffmpeg"); assert!(out.status.success()); out.stdout .as_chunks::<2>() .0 .iter() .map(|b| i16::from_le_bytes(*b)) .collect() } /// `(max LSB difference, fraction of samples equal)` of our float output /// rounded to s16 against ffmpeg's fixed-point decoder. fn s16_agreement(got: &[f32], want: &[i16]) -> (i32, f64) { let n = got.len().min(want.len()); let (mut worst, mut equal) = (0i32, 0usize); for i in 0..n { let g = (got[i] * 32768.0).round().clamp(-32768.0, 32767.0) as i32; let d = (g - i32::from(want[i])).abs(); worst = worst.max(d); equal += usize::from(d == 0); } (worst, equal as f64 / n.max(1) as f64) } fn our_decode(bytes: &[u8]) -> (Vec, u32, usize) { let mut reader = Mp3Reader::new(); reader.push(bytes); let mut out = Vec::new(); let (mut rate, mut ch) = (0, 0); while let Ok(frame) = reader.next_frame() { rate = frame.sample_rate; ch = frame.channels; out.extend_from_slice(&frame.samples); } (out, rate, ch) } fn max_err(a: &[f32], b: &[f32]) -> (f32, usize) { let n = a.len().min(b.len()); let mut worst = (0.0f32, 0usize); for i in 0..n { let e = (a[i] - b[i]).abs(); if e > worst.0 { worst = (e, i); } } worst } struct Rng(u64); impl Rng { fn next(&mut self) -> u64 { self.0 ^= self.0 << 13; self.0 ^= self.0 >> 7; self.0 ^= self.0 << 17; self.0 } fn byte(&mut self) -> u8 { (self.next() >> 24) as u8 } } /// MSB-first bit writer for building frames. struct BitW { bytes: Vec, bits: usize, } impl BitW { fn new() -> BitW { BitW { bytes: Vec::new(), bits: 0, } } fn put(&mut self, v: u32, n: u32) { for i in (0..n).rev() { if self.bits.is_multiple_of(8) { self.bytes.push(0); } if (v >> i) & 1 == 1 { *self.bytes.last_mut().unwrap() |= 0x80 >> (self.bits % 8); } self.bits += 1; } } } /// Quantiser sizes by allocation code for the five Layer II tables, written /// out again here so that a transcription slip in the crate shows up. fn l2_rows(table: usize) -> Vec> { let a = vec![ 0, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, ]; let b = vec![ 0, 3, 5, 7, 9, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 65535, ]; let c = vec![0, 3, 5, 7, 9, 15, 31, 65535]; let d = vec![0, 3, 5, 65535]; let e = vec![ 0, 3, 5, 9, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, ]; let f = vec![0, 3, 5, 9, 15, 31, 63, 127]; let g = vec![ 0, 3, 5, 7, 9, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, ]; let h = vec![0, 3, 5, 9]; let runs: Vec<(&Vec, usize)> = match table { 0 => vec![(&a, 3), (&b, 8), (&c, 12), (&d, 4)], 1 => vec![(&a, 3), (&b, 8), (&c, 12), (&d, 7)], 2 => vec![(&e, 2), (&f, 6)], 3 => vec![(&e, 2), (&f, 10)], _ => vec![(&g, 4), (&f, 7), (&h, 19)], }; runs.into_iter() .flat_map(|(r, n)| std::iter::repeat_n(r.clone(), n)) .collect() } fn l2_table(h: &FrameHeader) -> usize { if h.version != ec_mp3::Version::Mpeg1 { return 4; } let ch = h.bitrate_kbps / h.channels() as u32; let r = h.sample_rate; if (r == 48000 && ch >= 56) || (56..=80).contains(&ch) { 0 } else if r != 48000 && ch >= 96 { 1 } else if r != 32000 && ch <= 48 { 2 } else { 3 } } /// A legal Layer I/II frame body with random allocations, scalefactor /// selectors, scalefactors and samples (never the forbidden codes), as many /// bands filled as fit. `crc` fills in a correct CRC. fn structured_frame(rng: &mut Rng, h: &FrameHeader, density: u32) -> Option> { let channels = h.channels(); let joint = h.mode == ec_mp3::ChannelMode::JointStereo; let mut w = BitW::new(); let rnd = |rng: &mut Rng, n: u32| (rng.next() >> 20) as u32 % n.max(1); let (nb_sb, bound); let rows: Vec>; if h.layer == Layer::I { nb_sb = 32; rows = Vec::new(); } else { rows = l2_rows(l2_table(h)); nb_sb = rows.len(); } bound = if joint { (4 + 4 * h.mode_ext as usize).min(nb_sb) } else { nb_sb }; // allocation codes let mut alloc = vec![vec![0u32; nb_sb]; 2]; let width = |sb: usize| -> u32 { if h.layer == Layer::I { 4 } else { rows[sb].len().trailing_zeros() } }; for sb in 0..nb_sb { let maxcode = if h.layer == Layer::I { 15 } else { rows[sb].len() as u32 }; let pick = |rng: &mut Rng| { if rnd(rng, 100) < density { 1 + rnd(rng, maxcode - 1) } else { 0 } }; for ch in 0..channels { alloc[ch][sb] = if sb < bound || ch == 0 { pick(rng) } else { alloc[0][sb] }; } if sb >= bound && channels == 2 { alloc[1][sb] = alloc[0][sb]; } } for sb in 0..nb_sb { if sb < bound { for a in alloc.iter().take(channels) { w.put(a[sb], width(sb)); } } else { w.put(alloc[0][sb], width(sb)); } } let mut scfsi = vec![vec![0u32; nb_sb]; 2]; if h.layer == Layer::II { for sb in 0..nb_sb { for ch in 0..channels { if alloc[ch][sb] != 0 { scfsi[ch][sb] = rnd(rng, 4); w.put(scfsi[ch][sb], 2); } } } } let protected_bits = w.bits; let protected: Vec = w.bytes.clone(); for sb in 0..nb_sb { for ch in 0..channels { if alloc[ch][sb] == 0 { continue; } let n = if h.layer == Layer::I { 1 } else { match scfsi[ch][sb] { 0 => 3, 2 => 1, _ => 2, } }; for _ in 0..n { w.put(rnd(rng, 63), 6); } } } // Layer I: 12 slots of one sample; Layer II: 12 granules of three (grouped). for _ in 0..12 { for sb in 0..nb_sb { let chs = if sb < bound { channels } else { 1 }; for ch_alloc in alloc.iter().take(chs) { let a = ch_alloc[sb]; if a == 0 { continue; } if h.layer == Layer::I { let nb = a + 1; w.put(rnd(rng, (1 << nb) - 1), nb); } else { let l = rows[sb][a as usize]; match l { 3 | 5 | 9 => { let bits = [0, 0, 0, 5, 0, 7, 0, 0, 0, 10][l as usize]; w.put(rnd(rng, l * l * l), bits); } _ => { let nb = 32 - l.leading_zeros(); for _ in 0..3 { w.put(rnd(rng, l), nb); } } } } } } } let len = h.frame_len().unwrap(); let head = if h.crc { 6 } else { 4 }; if head + w.bytes.len() > len { return None; } let mut frame = h.to_bytes().to_vec(); if h.crc { let c = ec_mp3::header::crc16_bits(&h.to_bytes(), &protected, protected_bits); frame.extend_from_slice(&c.to_be_bytes()); } frame.extend_from_slice(&w.bytes); frame.resize(len, 0); Some(frame) } fn random_header( rng: &mut Rng, layer: u8, version: u8, mode: u8, ext: u8, crc: bool, ) -> FrameHeader { let br = 1 + (rng.byte() % 14); let sr = rng.byte() % 3; let b1 = 0xE0 | (version << 3) | ((4 - layer) << 1) | u8::from(!crc); FrameHeader::parse(&[0xFF, b1, (br << 4) | (sr << 2), (mode << 6) | (ext << 4)]).unwrap() } fn compare_stream(name: &str, frames: &[Vec], tolerance: f32) -> f32 { let dir = work_dir(); let mut bytes = Vec::new(); for f in frames { bytes.extend_from_slice(f); } let path = dir.join(format!("{name}.mp2")); std::fs::write(&path, &bytes).unwrap(); let h0 = FrameHeader::parse(&frames[0]).unwrap(); let ch = h0.channels(); let want = ffmpeg_decode(&path, ch, h0.layer); let (got, _, _) = our_decode(&bytes); assert_eq!(got.len(), want.len(), "{name}: length"); let (e, at) = max_err(&got, &want); let (lsb, same) = s16_agreement(&got, &ffmpeg_decode_s16(&path, ch, h0.layer)); eprintln!( "{name}: {} frames, max abs error {e:.3e}; vs fixed-point s16: max {lsb} LSB, {:.2}% identical", frames.len(), same * 100.0 ); if e > tolerance { let per = h0.samples_per_frame() * ch; for (i, (g, w)) in got.chunks(per).zip(want.chunks(per)).enumerate() { let (fe, fat) = max_err(g, w); if fe > tolerance { eprintln!( " frame {i}: error {fe:.3e} at {fat} (ours {}, ffmpeg {}); header {h0:?}", g[fat], w[fat] ); break; } } } assert!(e <= tolerance, "{name}: max error {e} at {at}"); e } #[test] fn random_frames_match_ffmpeg() { let mut rng = Rng(0x9E37_79B9_7F4A_7C15); let mut worst = 0.0f32; let mut streams = 0; for layer in [2u8, 1] { for version in [3u8, 2, 0] { for (mode, ext) in [(3u8, 0u8), (0, 0), (2, 0), (1, 0), (1, 1), (1, 2), (1, 3)] { for round in 0..8 { let crc = round % 2 == 1; // A header whose frames can at least hold their own allocation fields. let h = loop { let h = random_header(&mut rng, layer, version, mode, ext, crc); if structured_frame(&mut rng, &h, 0).is_some() { break h; } }; let mut frames = Vec::new(); while frames.len() < 24 { let density = 5 + (rng.byte() as u32 % 90); let mut d = density; loop { if let Some(f) = structured_frame(&mut rng, &h, d) { frames.push(f); break; } d = (d * 2 / 3).max(1); } } let name = format!("rand-l{layer}-v{version}-m{mode}-e{ext}-r{round}"); worst = worst.max(compare_stream(&name, &frames, 4e-5)); streams += 1; } } } } eprintln!("{streams} random streams, worst error {worst:.3e}"); }