//! Parametric Stereo (HE-AACv2) against FFmpeg's native AAC decoder. //! //! Each fixture is a 2-second HE-AACv2 stereo ADTS stream (generated by //! `scripts/aac-tables/make-ps-fixtures.sh`). The reference is FFmpeg's own //! decode of the same file. The acceptance bar is a per-channel correlation of //! at least 0.999 after a bounded lag search, which absorbs decoder priming //! delay, as `sbr_real_library.rs` does for HE-AAC v1. use crate::shared::have_ffmpeg; use crate::common::fixture; use crate::common::deinterleave; use crate::common::correlation; use ec_aac::AacDecoder; use std::path::Path; use std::process::Command; /// Per-channel correlation the reconstruction must reach against FFmpeg. const MIN_CORR: f64 = 0.999; /// Fixtures whose PS reconstruction is known to differ from FFmpeg, with the /// correlation floor the current code is held to. Empty: every PS fixture /// matches FFmpeg at `MIN_CORR` or better. An entry here needs a one-line reason /// and a measured floor; remove it once the fixture passes at `MIN_CORR`. const KNOWN_INEXACT: &[&str] = &[]; const KNOWN_INEXACT_FLOOR: f64 = 0.69; /// Lag search bound, in samples. A winning lag at this bound is not a measurement. const LAG_MAX: i64 = 4096; /// Window (in samples) the coarse lag search correlates over. const WINDOW: usize = 16_384; /// Where the coarse window starts, past the decoder's start-up transient. const WINDOW_START: usize = 32_768; /// (fixture, sample rate it decodes to) const FIXTURES: &[(&str, u32)] = &[ ("ps_44100_sine.aac", 44_100), ("ps_44100_music.aac", 44_100), ("ps_48000_sine.aac", 48_000), ("ps_48000_music.aac", 48_000), ]; /// FFmpeg's decode of `path` as planar stereo `f32`. fn ffmpeg_reference(path: &Path) -> Vec> { let out = Command::new("ffmpeg") .args(["-v", "error", "-i"]) .arg(path) .args(["-ac", "2", "-f", "f32le", "-acodec", "pcm_f32le", "-"]) .output() .expect("ffmpeg runs"); assert!( out.status.success(), "ffmpeg decode failed: {}", String::from_utf8_lossy(&out.stderr) ); deinterleave( &out.stdout .chunks(4) .filter(|c| c.len() == 4) .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])) .collect::>(), 2, ) } /// The overlap of `ours` and `theirs` at `lag`: `ours[i]` pairs with `theirs[i + lag]`. fn aligned<'a>(ours: &'a [f32], theirs: &'a [f32], lag: i64) -> (&'a [f32], &'a [f32]) { let (a0, b0) = if lag >= 0 { (0usize, lag as usize) } else { ((-lag) as usize, 0usize) }; let n = ours.len().saturating_sub(a0).min(theirs.len().saturating_sub(b0)); (&ours[a0..a0 + n], &theirs[b0..b0 + n]) } /// The lag that best aligns `ours` with `theirs`, and the full-overlap /// correlation at that lag. A winning lag on the search bound is refused: the /// true alignment may lie outside the search, so the number would be noise. fn best_lag(ours: &[f32], theirs: &[f32]) -> (i64, f64) { let mut best = (0i64, 0.0f64); for lag in -LAG_MAX..=LAG_MAX { let (a, b) = aligned(ours, theirs, lag); if a.len() < WINDOW_START + WINDOW { continue; } let c = correlation(&a[WINDOW_START..WINDOW_START + WINDOW], &b[WINDOW_START..WINDOW_START + WINDOW]); if c.abs() > best.1.abs() { best = (lag, c); } } assert!( best.0.abs() < LAG_MAX, "winning lag {} sits at the search bound -- the alignment is outside the search", best.0 ); let (a, b) = aligned(ours, theirs, best.0); (best.0, correlation(a, b)) } #[test] fn ps_fixtures_decode_to_stereo_matching_ffmpeg() { let require = std::env::var_os("EC_REQUIRE_FIXTURES").is_some(); if !have_ffmpeg() { assert!(!require, "EC_REQUIRE_FIXTURES=1 but ffmpeg is not installed"); eprintln!("SKIP: ffmpeg not installed"); return; } let mut failures = Vec::new(); for &(name, rate) in FIXTURES { let path = fixture(name); assert!( path.exists() || !require, "EC_REQUIRE_FIXTURES=1 but fixture {} is absent", path.display() ); if !path.exists() { eprintln!("SKIP: fixture {} absent", path.display()); continue; } let bytes = std::fs::read(&path).expect("fixture reads"); let audio = AacDecoder::new() .decode(&bytes, None) .unwrap_or_else(|e| panic!("{name}: decode failed: {e:?}")); assert_eq!(audio.channels, 2, "{name}: PS must decode to two channels"); assert_eq!(audio.sample_rate, rate, "{name}: PS must decode at the extension rate"); let ours = deinterleave(&audio.samples, 2); let theirs = ffmpeg_reference(&path); for ch in 0..2 { // The primary check is the full overlap at zero lag: both decoders // emit the same frame count and the same start, so no delay is // expected. The bounded search is reported as a diagnostic only, since // on a pure tone it returns aliases of the period. let (a, b) = aligned(&ours[ch], &theirs[ch], 0); let corr = correlation(a, b); let (lag, search_corr) = best_lag(&ours[ch], &theirs[ch]); println!( "{name} ch{ch}: ours {} frames, ffmpeg {} frames, corr@0 {corr:.6}, search lag {lag} corr {search_corr:.6}", ours[ch].len(), theirs[ch].len() ); let floor = if KNOWN_INEXACT.contains(&name) { KNOWN_INEXACT_FLOOR } else { MIN_CORR }; if corr < floor { failures.push(format!("{name} ch{ch}: corr@0 {corr:.6} < {floor}")); } } } assert!(failures.is_empty(), "PS does not match ffmpeg:\n{}", failures.join("\n")); }