//! Our encoder measured the only way a lossy one can be: encode, decode the //! result with ffmpeg, correlate against the samples that went in. //! //! The bar is the incumbent this crate replaces, `rusty_mp3` 0.6.1, measured on //! the same fixtures and the same metric (see `BAR`, and //! `scripts/mp3-incumbent-bar.md` for how those numbers were produced). //! //! Run the table: //! cargo test -p ec-mp3 --release --test encode_matrix -- --nocapture use std::path::{Path, PathBuf}; use std::process::Command; use ec_mp3::{Mp3Encoder, Mp3EncoderConfig}; /// Correlation `rusty_mp3` 0.6.1 reaches on this corpus, measured with this /// same alignment and metric (its own encoder, ffmpeg's decoder). The tone /// fixtures reach 1.0 on both encoders and so cannot separate them; the /// `mp3src-*` material — glide, broadband noise and a click every half second — /// is what does. const BAR: [(&str, [f64; 3]); 4] = [ ("mp3src-mono-44100", [0.99920, 0.99983, 0.99999]), ("mp3src-stereo-44100", [0.99612, 0.99847, 0.99972]), ("mp3src-mono-48000", [0.99867, 0.99972, 0.99998]), ("mp3src-stereo-48000", [0.99461, 0.99787, 0.99956]), ]; const BITRATES: [u32; 3] = [128, 192, 320]; /// The incumbent's number for this fixture and bitrate. Fixtures it was not /// measured on (the tones, which both encoders code perfectly) fall back to its /// worst result at that rate. fn bar_for(name: &str, kbps: u32) -> f64 { let index = BITRATES.iter().position(|k| *k == kbps).unwrap_or(0); for (fixture, values) in BAR { if name.starts_with(fixture) { return values[index]; } } BAR.iter() .map(|(_, v)| v[index]) .fold(f64::INFINITY, f64::min) } fn fixtures() -> PathBuf { Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures") } fn workdir(test: &str) -> PathBuf { let dir = std::env::temp_dir().join(format!("ec-mp3-{}-{test}", std::process::id())); std::fs::create_dir_all(&dir).expect("create work dir"); dir } /// Reads a WAV fixture as interleaved f32 plus its rate and channel count. fn read_wav(path: &Path) -> Option<(Vec, u32, usize)> { let out = Command::new("ffmpeg") .args(["-v", "error", "-i"]) .arg(path) .args(["-f", "f32le", "-"]) .output() .ok()?; if !out.status.success() { return None; } let samples = out .stdout .chunks_exact(4) .map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])) .collect(); let probe = Command::new("ffprobe") .args([ "-v", "error", "-select_streams", "a:0", "-show_entries", "stream=channels,sample_rate", "-of", "default=noprint_wrappers=1", ]) .arg(path) .output() .ok()?; let text = String::from_utf8_lossy(&probe.stdout); let field = |key: &str| { text.lines() .find_map(|l| l.strip_prefix(key)?.strip_prefix('=')) .and_then(|v| v.trim().parse::().ok()) }; Some((samples, field("sample_rate")?, field("channels")? as usize)) } fn correlation(a: &[f32], b: &[f32]) -> f64 { let n = a.len().min(b.len()); let (mut ab, mut aa, mut bb) = (0.0f64, 0.0f64, 0.0f64); for i in 0..n { let (x, y) = (a[i] as f64, b[i] as f64); ab += x * y; aa += x * x; bb += y * y; } if aa == 0.0 || bb == 0.0 { return 0.0; } ab / (aa * bb).sqrt() } /// Best correlation over the encoder delay, which no MP3 encoder avoids. fn aligned(got: &[f32], want: &[f32], channels: usize) -> (f64, usize) { let mut best = (f64::MIN, 0usize); let mut offset = 0; while offset + channels < got.len() && offset <= 3000 * channels { let n = (got.len() - offset).min(want.len()); let skip = 1152 * channels; if n <= 2 * skip { break; } let corr = correlation( &got[offset + skip..offset + n - skip], &want[skip..n - skip], ); if corr > best.0 { best = (corr, offset); } offset += channels; } best } fn encode(pcm: &[f32], rate: u32, channels: usize, kbps: u32) -> Vec { let mut encoder = Mp3Encoder::new(Mp3EncoderConfig { bitrate_kbps: kbps, vbr_quality: None, }); encoder .push_pcm_f32(pcm, channels as u16, rate) .expect("encoder accepts the fixture"); encoder.finish(); let mut out = Vec::new(); while let Ok(frame) = encoder.next_packet() { out.extend_from_slice(&frame); } out } /// CBR-192 of the stereo 48 kHz tone fixture decodes at least as well as it /// did before the masking model was put in the quantiser's units (9ac19d6: /// corr 0.999999, RMS error 0.020% of the signal). A byte pin stood here /// while VBR landed; the psychoacoustic change legitimately moves the bytes, /// so the gate is the quality those bytes carried. #[test] fn cbr_quality_holds_its_floor() { let path = fixtures().join("audio/wav16-stereo-48000.wav"); let Some((pcm, rate, channels)) = read_wav(&path) else { eprintln!("no WAV fixtures: run scripts/gen-fixtures.sh"); return; }; let bytes = encode(&pcm, rate, channels, 192); let file = workdir("cbr").join("cbr192.mp3"); std::fs::write(&file, &bytes).unwrap(); let decoded = decode(&file); assert_eq!(decoded.len(), pcm.len()); let corr = correlation(&decoded, &pcm); let (err, sig) = decoded .iter() .zip(&pcm) .fold((0.0f64, 0.0f64), |(e, s), (d, p)| { (e + f64::from(d - p).powi(2), s + f64::from(*p).powi(2)) }); let rms = (err / sig).sqrt(); println!("cbr192 corr={corr:.6} rms={:.4}%", rms * 100.0); assert!(corr >= 0.999_99, "corr {corr:.6}"); assert!(rms <= 0.000_25, "rms {:.4}%", rms * 100.0); } fn decode(file: &Path) -> Vec { let out = Command::new("ffmpeg") .args(["-v", "warning", "-i"]) .arg(file) .args(["-f", "f32le", "-"]) .output() .expect("run ffmpeg"); assert!( out.stderr.is_empty(), "ffmpeg: {}", String::from_utf8_lossy(&out.stderr) ); out.stdout .chunks_exact(4) .map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])) .collect() } fn encode_vbr(pcm: &[f32], rate: u32, channels: usize, quality: f32) -> Vec { let mut encoder = Mp3Encoder::new(Mp3EncoderConfig { bitrate_kbps: 0, vbr_quality: Some(quality), }); encoder .push_pcm_f32(pcm, channels as u16, rate) .expect("encoder accepts the fixture"); encoder.finish(); let mut out = Vec::new(); while let Ok(frame) = encoder.next_packet() { out.extend_from_slice(&frame); } out } /// Quality 0.5 asks for a 192 kbit/s mean; true VBR spends it unevenly across /// frames, decodes at least as well as the incumbent's CBR-192, and announces /// itself through a Xing tag ffprobe reads as VBR. #[test] fn vbr_frames_vary_their_bitrate_index() { let name = "mp3src-stereo-48000"; let path = fixtures().join(format!("audio/{name}.wav")); let Some((pcm, rate, channels)) = read_wav(&path) else { eprintln!("no WAV fixtures: run scripts/gen-fixtures.sh"); return; }; let bytes = encode_vbr(&pcm, rate, channels, 0.5); // Walk the frame headers (MPEG-1, Layer III), skipping the Xing frame. let table = [ 0u32, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, ]; let mut indices = std::collections::BTreeSet::new(); let (mut pos, mut frames) = (0usize, 0usize); while pos + 4 <= bytes.len() { let h = &bytes[pos..pos + 4]; assert!(h[0] == 0xFF && (h[1] & 0xE0) == 0xE0, "sync lost at {pos}"); let index = usize::from(h[2] >> 4); let kbps = table[index.min(14)] as usize; assert!(kbps > 0, "free-format header at {pos}"); if frames > 0 { indices.insert(index); } frames += 1; pos += 144 * kbps * 1000 / rate as usize + usize::from((h[2] >> 1) & 1); } let seconds = pcm.len() as f64 / (rate as f64 * channels as f64); let mean = bytes.len() as f64 * 8.0 / seconds / 1000.0; let target = f64::from(ec_mp3::encode::bitrate_for_quality(0.5)); println!("vbr frames={frames} indices={indices:?} mean={mean:.1} kbit/s"); assert!(indices.len() >= 3, "only {indices:?}"); assert!( (mean - target).abs() <= target * 0.25, "mean {mean:.1} vs {target}" ); let work = workdir("vbr"); let file = work.join("vbr.mp3"); std::fs::write(&file, &bytes).unwrap(); let out = Command::new("ffmpeg") .args(["-v", "warning", "-i"]) .arg(&file) .args(["-f", "f32le", "-"]) .output() .expect("run ffmpeg"); assert!( out.stderr.is_empty(), "ffmpeg: {}", String::from_utf8_lossy(&out.stderr) ); let decoded: Vec = out .stdout .chunks_exact(4) .map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])) .collect(); let corr = correlation(&decoded, &pcm); let bar = bar_for(name, 192); println!("vbr corr={corr:.5} bar={bar:.5}"); assert!(corr >= bar, "corr {corr:.5} < bar {bar:.5}"); let probe = Command::new("ffprobe") .args(["-v", "error", "-show_format"]) .arg(&file) .output() .expect("run ffprobe"); let probe = String::from_utf8_lossy(&probe.stdout); // "Xing" (not "Info") plus the VBR-method byte after the 9-byte version. assert!( &bytes[36..40] == b"Xing" && bytes[36 + 16 + 9] == 0x70, "no VBR-flagged Xing tag" ); assert!(probe.contains("format_name=mp3"), "{probe}"); } #[test] fn vbr_mean_bitrate_tracks_quality_on_music() { let path = fixtures().join("audio/wav16-stereo-48000.wav"); let Some((pcm, rate, channels)) = read_wav(&path) else { eprintln!("no WAV fixtures: run scripts/gen-fixtures.sh"); return; }; let mut previous = 0.0; for q in [0.25f32, 0.5, 0.75] { let bytes = encode_vbr(&pcm, rate, channels, q); let seconds = pcm.len() as f64 / (rate as f64 * channels as f64); let mean = bytes.len() as f64 * 8.0 / seconds / 1000.0; let target = f64::from(ec_mp3::encode::bitrate_for_quality(q)); let tolerance = if q <= 0.5 { 0.70 } else { 0.25 }; println!("q={q:.2} mean={mean:.1} target={target:.1}"); assert!( mean > previous, "q={q:.2} mean {mean:.1} did not rise above {previous:.1}" ); assert!( (mean - target).abs() <= target * tolerance, "fixture is sparse/tonal, so q={q:.2} allows {:.0}%: mean {mean:.1} vs {target:.1}", tolerance * 100.0, ); previous = mean; } } #[test] fn encodes_above_the_incumbent_bar() { let dir = fixtures().join("audio"); let mut sources: Vec = std::fs::read_dir(&dir) .into_iter() .flatten() .flatten() .map(|e| e.path()) .filter(|p| { let name = p.file_name().map(|n| n.to_string_lossy().to_string()); name.is_some_and(|n| n.starts_with("wav16-") || n.starts_with("mp3src-")) && p.extension().is_some_and(|e| e == "wav") }) .collect(); sources.sort(); sources.retain(|p| { let name = p.file_name().unwrap().to_string_lossy().to_string(); name.contains("mono") || name.contains("stereo") }); if sources.is_empty() { eprintln!("no WAV fixtures: run scripts/gen-fixtures.sh"); return; } let work = workdir("encode"); println!( "{:<30} {:>5} {:>6} {:>3} {:>9} {:>8} {:>9}", "fixture", "kbps", "rate", "ch", "corr", "bar", "kbit/s" ); let mut failures = Vec::new(); for source in &sources { let Some((pcm, rate, channels)) = read_wav(source) else { continue; }; for kbps in BITRATES { let bar = bar_for(&source.file_name().unwrap().to_string_lossy(), kbps); let bytes = encode(&pcm, rate, channels, kbps); let path = work.join(format!( "{}-{kbps}.mp3", source.file_stem().unwrap().to_string_lossy() )); std::fs::write(&path, &bytes).expect("write encoded file"); let out = Command::new("ffmpeg") .args(["-v", "warning", "-i"]) .arg(&path) .args(["-f", "f32le", "-"]) .output() .expect("run ffmpeg"); let stderr = String::from_utf8_lossy(&out.stderr).trim().to_string(); if !stderr.is_empty() { failures.push(format!("{}: ffmpeg said {stderr}", path.display())); } let decoded: Vec = out .stdout .chunks_exact(4) .map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])) .collect(); // With the Info tag's delay and padding honoured, ffmpeg hands // back exactly the samples that went in: same count, no shift. That // is the gapless claim, so the correlation is measured at lag zero // rather than at whatever lag looks best. if decoded.len() != pcm.len() { failures.push(format!( "{}: decoded {} samples, pushed {}", source.file_name().unwrap().to_string_lossy(), decoded.len(), pcm.len() )); } let corr = correlation(&decoded, &pcm); // A periodic fixture correlates equally well a whole period out, // so a tie is not a misalignment; only a shift that is genuinely // better than lag zero is. let (searched, offset) = aligned(&decoded, &pcm, channels); if offset != 0 && searched > corr + 1e-4 { failures.push(format!( "{}: lag {offset} correlates {searched:.6}, better than lag zero {corr:.6}", source.file_name().unwrap().to_string_lossy(), )); } let seconds = pcm.len() as f64 / (rate as f64 * channels as f64); let measured = bytes.len() as f64 * 8.0 / seconds / 1000.0; let name = source.file_name().unwrap().to_string_lossy(); println!( "{name:<30} {kbps:>5} {rate:>6} {channels:>3} {corr:>9.5} {bar:>8.5} {measured:>9.1}" ); // Matching counts as passing: at 320 kbit/s both encoders are // within a part in 10^5 of the source and the difference between // them is smaller than the metric resolves. if corr < bar - 1e-5 { failures.push(format!("{name} at {kbps} kbit/s: {corr:.5} < bar {bar:.5}")); } if (measured - kbps as f64).abs() > kbps as f64 * 0.05 { failures.push(format!( "{name} at {kbps} kbit/s: wrote {measured:.1} kbit/s" )); } } } assert!(failures.is_empty(), "{failures:#?}"); }