diff --git a/.gitignore b/.gitignore index 9d7816c2..eba9f4b8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ target/ fixtures/ __pycache__/ scripts/aac-tables/sbr-cache.json +.cache/ diff --git a/crates/ec-aac/tests/sbr_real_library.rs b/crates/ec-aac/tests/sbr_real_library.rs index 6dc528fd..486a9c9c 100644 --- a/crates/ec-aac/tests/sbr_real_library.rs +++ b/crates/ec-aac/tests/sbr_real_library.rs @@ -2107,6 +2107,125 @@ fn full_chain_low_band_matches_own_core() { } } +/// Round-52: a controlled fixture matrix -- unlike reeln/FMJ (one real +/// file each, whose sample rate, bitrate and content all differ at once), +/// every row here decodes the SAME synthesized source (three summed sine +/// sweeps 100 Hz-18 kHz + seeded pink noise, per-channel phase offset), +/// varying only sample rate (44100/48000) and bitrate (32k/48k/64k/96k, +/// which sweeps the SBR crossover `kx`/`k2`), so a break tied to a rate +/// FAMILY or to `kx` GEOMETRY shows as a pattern across the table instead +/// of being confounded with one file's own content. `aac_low` control rows +/// (no SBR at all) prove the harness itself -- container extraction, +/// decode, correlation -- is sound before the HE-AAC rows next to them are +/// trusted; those are the only asserted numbers here, everything else is +/// instrumentation for the ledger, not a regression gate yet. +/// +/// Env-gated on `EC_AAC_HEAAC_FIXTURES` (defaults to the same +/// `/.cache/heaac-fixtures` `scripts/aac-tables/make-heaac-fixtures.sh` +/// writes to) -- skips loudly, generates nothing itself, when that +/// directory is absent. +#[test] +fn synthetic_heaac_matrix() { + if !have_ffmpeg() { + eprintln!("SKIP: ffmpeg not on PATH"); + return; + } + let dir = std::env::var("EC_AAC_HEAAC_FIXTURES").unwrap_or_else(|_| { + format!("{}/../../.cache/heaac-fixtures", env!("CARGO_MANIFEST_DIR")) + }); + let dir = PathBuf::from(dir); + if !dir.is_dir() { + eprintln!( + "SKIP: {} absent -- run scripts/aac-tables/make-heaac-fixtures.sh first", + dir.display() + ); + return; + } + unsafe { + std::env::set_var("EC_AAC_SBR_SIDEINFO_DEBUG", "1"); + } + println!( + "{:>5} {:>6} {:>4} {:>4} {:>4} {:>8} {:>8} {:>8} {:>6} {:>11}", + "rate", "kbps", "prof", "kx", "k2", "full", "below", "above", "lag", "ours_v_core" + ); + for rate in [48_000u32, 44_100] { + for br in ["32k", "48k", "64k", "96k"] { + for (profile, prefix) in [("HE", "heaac"), ("LC", "lc")] { + let path = dir.join(format!("{prefix}_{rate}_{br}.m4a")); + if !path.exists() { + eprintln!("SKIP {}: missing", path.display()); + continue; + } + let before = ec_aac::sbr_sideinfo_log().len(); + let Some((ours, _sbr, our_rate)) = our_decode(&path, 0) else { + eprintln!("SKIP {}: could not decode", path.display()); + continue; + }; + let rows = ec_aac::sbr_sideinfo_log(); + let (kx, k2) = rows[before..] + .iter() + .find(|r| r.ch == 0) + .map(|r| (r.kx, r.k2)) + .unwrap_or((-1, -1)); + let theirs = ffmpeg_decode(&path, 0, ours.len()); + let o = &ours[0]; + let t = &theirs[0]; + let (lag, full) = best_lag_correlation(o, t); + // Crossover in Hz from the actual parsed kx (band width + // rate/128, same domain `sbr_chain.rs`'s own doc comment + // gives `kx`/`k2` in) when SBR engaged; a fixed quarter-Nyquist + // split for the LC controls, which carry no kx at all. + let crossover_hz = if kx > 0 { + f64::from(our_rate) * (kx as f64) / 128.0 + } else { + f64::from(our_rate) * 0.25 + }; + let (_, below) = + best_lag_correlation(&lowpass(o, our_rate, crossover_hz), &lowpass(t, our_rate, crossover_hz)); + let n = WINDOW.min(o.len()).min(t.len()); + let above = if n >= 1024 { + correlation( + &highpass(&o[..n], our_rate, crossover_hz), + &highpass(&t[..n], our_rate, crossover_hz), + ) + } else { + 0.0 + }; + // Round-51's self-consistency probe (our full chain's low + // band vs our own core-only decode, 2x upsampled), run + // per-row so a rate/kx-specific self-inconsistency shows in + // the same table instead of needing a second pass. + let ours_v_core = if kx > 0 { + our_decode_core_only(&path, 0).map(|(core, core_rate)| { + let up: Vec = core[0].iter().flat_map(|&s| [s, s]).collect(); + let cutoff = f64::from(core_rate) * 0.4; + let ol = lowpass(o, our_rate, cutoff); + let ul = lowpass(&up, our_rate, cutoff); + best_lag_correlation(&ol, &ul).1 + }) + } else { + None + }; + println!( + "{rate:>5} {br:>6} {profile:>4} {kx:>4} {k2:>4} {full:>8.4} {below:>8.4} {above:>8.4} {lag:>6} {:>11}", + ours_v_core + .map(|c| format!("{c:.4}")) + .unwrap_or_else(|| "n/a".into()) + ); + if profile == "LC" { + assert!( + full >= 0.999, + "{}: LC control full-band corr {full:.6} < 0.999 -- the \ + harness itself (extraction/decode/correlation) is broken, \ + not the SBR chain this fixture matrix is meant to isolate", + path.display() + ); + } + } + } + } +} + fn candidates() -> Vec { let home = std::env::var("HOME").unwrap_or_default(); [ diff --git a/scripts/aac-tables/make-heaac-fixtures.sh b/scripts/aac-tables/make-heaac-fixtures.sh new file mode 100755 index 00000000..279aebee --- /dev/null +++ b/scripts/aac-tables/make-heaac-fixtures.sh @@ -0,0 +1,67 @@ +#!/usr/bin/env bash +# Deterministic HE-AAC v1 fixture matrix for tests/sbr_real_library.rs's +# `synthetic_heaac_matrix`: two sample-rate families (48000, 44100) x four +# bitrates (32k/48k/64k/96k) x two profiles (aac_he, aac_low control) = 16 +# .m4a files, sweeping the SBR crossover (kx/k2) that bitrate controls +# without depending on any one real file's content. +# +# `ffmpeg`'s system libfdk_aac (the `fdk-aac-free` package) has SBR/PS +# encoding compiled out (patent-restricted tools) -- `-profile:a aac_he` +# fails with "Unable to set the AOT 5: Invalid config" against it. RPM +# Fusion's full `fdk-aac` package carries the SBR-capable encoder but +# installs to its OWN /usr/lib64/fdk-aac dir specifically so it doesn't +# clobber the system one; this script fetches (no root) and extracts just +# that .so into a local cache and points ffmpeg at it via +# LD_LIBRARY_PATH for the encode step only, never touching the system +# library. +set -euo pipefail + +SCRATCH="${EC_AAC_HEAAC_FIXTURES:-$(cd "$(dirname "$0")/../.." && pwd)/.cache/heaac-fixtures}" +mkdir -p "$SCRATCH" +CACHE="$SCRATCH/.fdk-aac-full" +mkdir -p "$CACHE" + +fdk_libdir="$CACHE/usr/lib64/fdk-aac" +if [ ! -f "$fdk_libdir/libfdk-aac.so.2" ]; then + echo "fetching SBR-capable libfdk-aac (no root, local cache only)..." + ( cd "$CACHE" && dnf download fdk-aac.x86_64 ) + rpm_file=$(find "$CACHE" -maxdepth 1 -name 'fdk-aac-*.rpm' | head -1) + ( cd "$CACHE" && rpm2cpio "$rpm_file" | cpio -idm --quiet ) +fi +export LD_LIBRARY_PATH="$fdk_libdir${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" + +# Three summed sweeps 100 Hz -> 18 kHz over the full 30 s (phase(t) = +# 2*pi*(f0*t + (f1-f0)/(2T)*t^2), f0=100 f1=18000 T=30 -> +# (f1-f0)/(2T)=298.333), offset in phase per partial and per channel so +# left/right are correlated but not identical -- plus low-level pink noise +# (seeded, so every run of this script reproduces bit-identical fixtures). +sweep() { echo "0.15*sin(2*PI*(100*t+298.333*t*t)+$1)+0.15*sin(2*PI*(100*t+298.333*t*t)+$2)+0.15*sin(2*PI*(100*t+298.333*t*t)+$3)"; } +L=$(sweep 0 1.047 2.094) +R=$(sweep 0.5 1.571 2.618) + +synth_source() { + local rate=$1 out=$2 + ffmpeg -y -hide_banner -loglevel error -filter_complex " + aevalsrc=exprs='${L}|${R}':channel_layout=stereo:sample_rate=${rate}:duration=30 [tone]; + anoisesrc=color=pink:amplitude=0.05:seed=42:sample_rate=${rate}:duration=30 [n0]; + anoisesrc=color=pink:amplitude=0.05:seed=43:sample_rate=${rate}:duration=30 [n1]; + [n0][n1] amerge=inputs=2 [noise]; + [tone][noise] amix=inputs=2:normalize=0 [out] + " -map "[out]" -ar "$rate" -sample_fmt s16 -f wav "$out" +} + +for rate in 48000 44100; do + src="$SCRATCH/src_${rate}.wav" + [ -f "$src" ] || synth_source "$rate" "$src" + for br in 32k 48k 64k 96k; do + he="$SCRATCH/heaac_${rate}_${br}.m4a" + lc="$SCRATCH/lc_${rate}_${br}.m4a" + [ -f "$he" ] || ffmpeg -y -hide_banner -loglevel error -i "$src" \ + -c:a libfdk_aac -profile:a aac_he -b:a "$br" "$he" + [ -f "$lc" ] || ffmpeg -y -hide_banner -loglevel error -i "$src" \ + -c:a libfdk_aac -profile:a aac_low -b:a "$br" "$lc" + done +done + +echo "fixtures in $SCRATCH:" +ls "$SCRATCH"/*.m4a