forge
Explore
Log in

Don't you dare go Hollow

Synthetic HE-AAC matrix fixture to separate rate family from kx geometry

Browse files at this commit

002ea6d78cda3e774671f2b900b3ce8383992311Ahmet Kaan GümüşParent: dbe43b0
  • test
    • synthetic HE-AAC matrix fixture to separate rate family from kx geometry
Show full message
test(aac-sbr): synthetic HE-AAC matrix fixture to separate rate family from kx geometry

Generates a controlled 16-file HE-AAC/LC fixture matrix (2 sample rates x 4
bitrates x 2 profiles) from one shared synthesized source via the system
encoder's HE-AAC profile, and adds synthetic_heaac_matrix to instrument
full/below/above-crossover correlation and kx/k2 per row against the
reference decoder, plus our own full-chain-vs-core-only self-consistency --
without depending on any single real file's content. LC control rows gate
the test (>=0.999), proving the harness before the HE-AAC numbers are
trusted.
.gitignoremodified+1 -0
@@ -2,3 +2,4 @@ target/
22 fixtures/
33 __pycache__/
44 scripts/aac-tables/sbr-cache.json
5+.cache/
crates/ec-aac/tests/sbr_real_library.rsmodified+119 -0
@@ -2107,6 +2107,125 @@ fn full_chain_low_band_matches_own_core() {
21072107 }
21082108 }
21092109
2110+/// Round-52: a controlled fixture matrix -- unlike reeln/FMJ (one real
2111+/// file each, whose sample rate, bitrate and content all differ at once),
2112+/// every row here decodes the SAME synthesized source (three summed sine
2113+/// sweeps 100 Hz-18 kHz + seeded pink noise, per-channel phase offset),
2114+/// varying only sample rate (44100/48000) and bitrate (32k/48k/64k/96k,
2115+/// which sweeps the SBR crossover `kx`/`k2`), so a break tied to a rate
2116+/// FAMILY or to `kx` GEOMETRY shows as a pattern across the table instead
2117+/// of being confounded with one file's own content. `aac_low` control rows
2118+/// (no SBR at all) prove the harness itself -- container extraction,
2119+/// decode, correlation -- is sound before the HE-AAC rows next to them are
2120+/// trusted; those are the only asserted numbers here, everything else is
2121+/// instrumentation for the ledger, not a regression gate yet.
2122+///
2123+/// Env-gated on `EC_AAC_HEAAC_FIXTURES` (defaults to the same
2124+/// `<repo>/.cache/heaac-fixtures` `scripts/aac-tables/make-heaac-fixtures.sh`
2125+/// writes to) -- skips loudly, generates nothing itself, when that
2126+/// directory is absent.
2127+#[test]
2128+fn synthetic_heaac_matrix() {
2129+ if !have_ffmpeg() {
2130+ eprintln!("SKIP: ffmpeg not on PATH");
2131+ return;
2132+ }
2133+ let dir = std::env::var("EC_AAC_HEAAC_FIXTURES").unwrap_or_else(|_| {
2134+ format!("{}/../../.cache/heaac-fixtures", env!("CARGO_MANIFEST_DIR"))
2135+ });
2136+ let dir = PathBuf::from(dir);
2137+ if !dir.is_dir() {
2138+ eprintln!(
2139+ "SKIP: {} absent -- run scripts/aac-tables/make-heaac-fixtures.sh first",
2140+ dir.display()
2141+ );
2142+ return;
2143+ }
2144+ unsafe {
2145+ std::env::set_var("EC_AAC_SBR_SIDEINFO_DEBUG", "1");
2146+ }
2147+ println!(
2148+ "{:>5} {:>6} {:>4} {:>4} {:>4} {:>8} {:>8} {:>8} {:>6} {:>11}",
2149+ "rate", "kbps", "prof", "kx", "k2", "full", "below", "above", "lag", "ours_v_core"
2150+ );
2151+ for rate in [48_000u32, 44_100] {
2152+ for br in ["32k", "48k", "64k", "96k"] {
2153+ for (profile, prefix) in [("HE", "heaac"), ("LC", "lc")] {
2154+ let path = dir.join(format!("{prefix}_{rate}_{br}.m4a"));
2155+ if !path.exists() {
2156+ eprintln!("SKIP {}: missing", path.display());
2157+ continue;
2158+ }
2159+ let before = ec_aac::sbr_sideinfo_log().len();
2160+ let Some((ours, _sbr, our_rate)) = our_decode(&path, 0) else {
2161+ eprintln!("SKIP {}: could not decode", path.display());
2162+ continue;
2163+ };
2164+ let rows = ec_aac::sbr_sideinfo_log();
2165+ let (kx, k2) = rows[before..]
2166+ .iter()
2167+ .find(|r| r.ch == 0)
2168+ .map(|r| (r.kx, r.k2))
2169+ .unwrap_or((-1, -1));
2170+ let theirs = ffmpeg_decode(&path, 0, ours.len());
2171+ let o = &ours[0];
2172+ let t = &theirs[0];
2173+ let (lag, full) = best_lag_correlation(o, t);
2174+ // Crossover in Hz from the actual parsed kx (band width
2175+ // rate/128, same domain `sbr_chain.rs`'s own doc comment
2176+ // gives `kx`/`k2` in) when SBR engaged; a fixed quarter-Nyquist
2177+ // split for the LC controls, which carry no kx at all.
2178+ let crossover_hz = if kx > 0 {
2179+ f64::from(our_rate) * (kx as f64) / 128.0
2180+ } else {
2181+ f64::from(our_rate) * 0.25
2182+ };
2183+ let (_, below) =
2184+ best_lag_correlation(&lowpass(o, our_rate, crossover_hz), &lowpass(t, our_rate, crossover_hz));
2185+ let n = WINDOW.min(o.len()).min(t.len());
2186+ let above = if n >= 1024 {
2187+ correlation(
2188+ &highpass(&o[..n], our_rate, crossover_hz),
2189+ &highpass(&t[..n], our_rate, crossover_hz),
2190+ )
2191+ } else {
2192+ 0.0
2193+ };
2194+ // Round-51's self-consistency probe (our full chain's low
2195+ // band vs our own core-only decode, 2x upsampled), run
2196+ // per-row so a rate/kx-specific self-inconsistency shows in
2197+ // the same table instead of needing a second pass.
2198+ let ours_v_core = if kx > 0 {
2199+ our_decode_core_only(&path, 0).map(|(core, core_rate)| {
2200+ let up: Vec<f32> = core[0].iter().flat_map(|&s| [s, s]).collect();
2201+ let cutoff = f64::from(core_rate) * 0.4;
2202+ let ol = lowpass(o, our_rate, cutoff);
2203+ let ul = lowpass(&up, our_rate, cutoff);
2204+ best_lag_correlation(&ol, &ul).1
2205+ })
2206+ } else {
2207+ None
2208+ };
2209+ println!(
2210+ "{rate:>5} {br:>6} {profile:>4} {kx:>4} {k2:>4} {full:>8.4} {below:>8.4} {above:>8.4} {lag:>6} {:>11}",
2211+ ours_v_core
2212+ .map(|c| format!("{c:.4}"))
2213+ .unwrap_or_else(|| "n/a".into())
2214+ );
2215+ if profile == "LC" {
2216+ assert!(
2217+ full >= 0.999,
2218+ "{}: LC control full-band corr {full:.6} < 0.999 -- the \
2219+ harness itself (extraction/decode/correlation) is broken, \
2220+ not the SBR chain this fixture matrix is meant to isolate",
2221+ path.display()
2222+ );
2223+ }
2224+ }
2225+ }
2226+ }
2227+}
2228+
21102229 fn candidates() -> Vec<Candidate> {
21112230 let home = std::env::var("HOME").unwrap_or_default();
21122231 [
scripts/aac-tables/make-heaac-fixtures.shadded+67 -0
@@ -0,0 +1,67 @@
1+#!/usr/bin/env bash
2+# Deterministic HE-AAC v1 fixture matrix for tests/sbr_real_library.rs's
3+# `synthetic_heaac_matrix`: two sample-rate families (48000, 44100) x four
4+# bitrates (32k/48k/64k/96k) x two profiles (aac_he, aac_low control) = 16
5+# .m4a files, sweeping the SBR crossover (kx/k2) that bitrate controls
6+# without depending on any one real file's content.
7+#
8+# `ffmpeg`'s system libfdk_aac (the `fdk-aac-free` package) has SBR/PS
9+# encoding compiled out (patent-restricted tools) -- `-profile:a aac_he`
10+# fails with "Unable to set the AOT 5: Invalid config" against it. RPM
11+# Fusion's full `fdk-aac` package carries the SBR-capable encoder but
12+# installs to its OWN /usr/lib64/fdk-aac dir specifically so it doesn't
13+# clobber the system one; this script fetches (no root) and extracts just
14+# that .so into a local cache and points ffmpeg at it via
15+# LD_LIBRARY_PATH for the encode step only, never touching the system
16+# library.
17+set -euo pipefail
18+
19+SCRATCH="${EC_AAC_HEAAC_FIXTURES:-$(cd "$(dirname "$0")/../.." && pwd)/.cache/heaac-fixtures}"
20+mkdir -p "$SCRATCH"
21+CACHE="$SCRATCH/.fdk-aac-full"
22+mkdir -p "$CACHE"
23+
24+fdk_libdir="$CACHE/usr/lib64/fdk-aac"
25+if [ ! -f "$fdk_libdir/libfdk-aac.so.2" ]; then
26+ echo "fetching SBR-capable libfdk-aac (no root, local cache only)..."
27+ ( cd "$CACHE" && dnf download fdk-aac.x86_64 )
28+ rpm_file=$(find "$CACHE" -maxdepth 1 -name 'fdk-aac-*.rpm' | head -1)
29+ ( cd "$CACHE" && rpm2cpio "$rpm_file" | cpio -idm --quiet )
30+fi
31+export LD_LIBRARY_PATH="$fdk_libdir${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
32+
33+# Three summed sweeps 100 Hz -> 18 kHz over the full 30 s (phase(t) =
34+# 2*pi*(f0*t + (f1-f0)/(2T)*t^2), f0=100 f1=18000 T=30 ->
35+# (f1-f0)/(2T)=298.333), offset in phase per partial and per channel so
36+# left/right are correlated but not identical -- plus low-level pink noise
37+# (seeded, so every run of this script reproduces bit-identical fixtures).
38+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)"; }
39+L=$(sweep 0 1.047 2.094)
40+R=$(sweep 0.5 1.571 2.618)
41+
42+synth_source() {
43+ local rate=$1 out=$2
44+ ffmpeg -y -hide_banner -loglevel error -filter_complex "
45+ aevalsrc=exprs='${L}|${R}':channel_layout=stereo:sample_rate=${rate}:duration=30 [tone];
46+ anoisesrc=color=pink:amplitude=0.05:seed=42:sample_rate=${rate}:duration=30 [n0];
47+ anoisesrc=color=pink:amplitude=0.05:seed=43:sample_rate=${rate}:duration=30 [n1];
48+ [n0][n1] amerge=inputs=2 [noise];
49+ [tone][noise] amix=inputs=2:normalize=0 [out]
50+ " -map "[out]" -ar "$rate" -sample_fmt s16 -f wav "$out"
51+}
52+
53+for rate in 48000 44100; do
54+ src="$SCRATCH/src_${rate}.wav"
55+ [ -f "$src" ] || synth_source "$rate" "$src"
56+ for br in 32k 48k 64k 96k; do
57+ he="$SCRATCH/heaac_${rate}_${br}.m4a"
58+ lc="$SCRATCH/lc_${rate}_${br}.m4a"
59+ [ -f "$he" ] || ffmpeg -y -hide_banner -loglevel error -i "$src" \
60+ -c:a libfdk_aac -profile:a aac_he -b:a "$br" "$he"
61+ [ -f "$lc" ] || ffmpeg -y -hide_banner -loglevel error -i "$src" \
62+ -c:a libfdk_aac -profile:a aac_low -b:a "$br" "$lc"
63+ done
64+done
65+
66+echo "fixtures in $SCRATCH:"
67+ls "$SCRATCH"/*.m4a