From 2b4d6911648b2d36c107858e26958b9df3fbf018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 20 Aug 2026 16:53:23 +0300 Subject: [PATCH] diag(aac-sbr): add a header/frame-feature discriminator table; fix two self-check instruments' stale/wrong lag conventions Task A: sbr_header_feature_table (tests/sbr_real_library.rs) prints one row per stream (reeln, FMJ, all 8 synthetic HE fixtures) of sbr_header()/ frame-level features (kx/k2, xover/start/stop freq, freq_scale, alter_scale, noise_bands, limiter_bands/gains, interpol_freq, smoothing_mode, per-frame num_env histogram, invf modes seen, add_harmonic usage, df_env/df_noise delta-coding rate, patch geometry) next to each stream's pass/fail correlation. Needed df_env/df_noise plumbed through SbrChannel and header params + patch lengths added to SbrSideInfoRow (src change, no behaviour change). Key finding: FMJ (fail, corr 0.036) and synth-48000-64k (pass, corr 1.000) share IDENTICAL kx/k2 (27/59), IDENTICAL header config, and IDENTICAL patch geometry [26,1,5] -- the only differences in the table are per-frame entropy (envelope-count histogram, invf-mode diversity), which is exactly what differing real-vs-synthetic CONTENT should produce on its own. This directly refutes "a per-stream SBR header/config feature separates pass from fail" (the standing hypothesis this task was chartered to test): no such feature exists in what sbr_header()/sbr_data() actually carries. Whatever breaks FMJ must be in the payload's numeric content or the container/extraction path, not the SBR sideinfo shape. Task B: full_chain_low_band_matches_own_core no longer hardcodes a stale QMF delay (-897); it now runs the same wide lag search `synthetic_heaac_matrix` uses and asserts corr >= 0.99 at the MEASURED lag. Confirms reeln's true lag is -576 (matches the ledger) at corr 0.997, and turns FMJ's already-known low-band self-inconsistency (round-51) into a real failing assertion instead of a silent print. synthetic_heaac_matrix's `ours_v_core`/`core_v_ref` columns: replaced the naive 2x-nearest-neighbor-upsample-then-lowpass-at-full-rate convention (which read a flat ~-0.12 regardless of file) with the native-core-rate decimate/`ffmpeg_decode_at_rate` convention `core_only_matches_reference` already trusts. This measurably fixed the 44100 family (core_v_ref now 0.44-0.995, proving the core decode itself is correct there even though the full chain fails at 0.07) but the 48000 family's core_v_ref is still flat/ negative despite `full`/`below` proving near-perfect correctness at native rate -- left open, most likely a remaining artifact in this resampling- domain comparison itself (ffmpeg's -ar downsample phase vs the wide-lag search), not a real SBR defect, since `below` already disproves it at the rate that matters. Correcting the charter's premise: core_v_ref did NOT read 0.9999 before this fix (measured 0.22-0.40 on 48000, near 0 on 44100) -- both self-check columns were broken, not one healthy one to copy. --- crates/ec-aac/src/sbr_chain.rs | 36 ++++ crates/ec-aac/src/sbr_env.rs | 6 + crates/ec-aac/src/sbr_payload.rs | 7 + crates/ec-aac/tests/sbr_real_library.rs | 215 +++++++++++++++++++++--- 4 files changed, 241 insertions(+), 23 deletions(-) diff --git a/crates/ec-aac/src/sbr_chain.rs b/crates/ec-aac/src/sbr_chain.rs index 71df3fb9..54fe0cf4 100644 --- a/crates/ec-aac/src/sbr_chain.rs +++ b/crates/ec-aac/src/sbr_chain.rs @@ -91,6 +91,26 @@ pub struct SbrSideInfoRow { pub add_harmonic: Option>, pub e_q_means: Vec, pub q_q_means: Vec, + /// Header fields held for the frame's duration (ยง4.6.18.3.2), added for + /// the FMJ-vs-reeln feature-table hunt: `sbr_header()`'s own coding + /// choices, not per-envelope grid data. + pub freq_scale: u8, + pub alter_scale: u8, + pub noise_bands: u8, + pub limiter_bands: u8, + pub limiter_gains: u8, + pub interpol_freq: u8, + pub smoothing_mode: u8, + pub start_freq: u8, + pub stop_freq: u8, + pub xover_band: u8, + /// `t_noise.len() - 1`, i.e. `numNoiseFloors` this frame. + pub num_noise: usize, + /// One entry per HF patch, each the patch's band count (from + /// `sbr_hf::build_patches`), so patch geometry is visible per frame. + pub patch_lengths: Vec, + pub df_env: Vec, + pub df_noise: Vec, } static SIDEINFO_LOG: std::sync::OnceLock>> = @@ -325,6 +345,20 @@ impl SbrChain { add_harmonic: sbr_ch.add_harmonic.clone(), e_q_means: mean(&sbr_ch.e_q), q_q_means: mean(&sbr_ch.q_q), + freq_scale: header.freq_scale, + alter_scale: header.alter_scale, + noise_bands: header.noise_bands, + limiter_bands: header.limiter_bands, + limiter_gains: header.limiter_gains, + interpol_freq: header.interpol_freq, + smoothing_mode: header.smoothing_mode, + start_freq: header.start_freq, + stop_freq: header.stop_freq, + xover_band: header.xover_band, + num_noise: sbr_ch.t_noise.len().saturating_sub(1), + patch_lengths: sbr_hf::build_patches(&tables).iter().map(|p| p.width).collect(), + df_env: sbr_ch.df_env.clone(), + df_noise: sbr_ch.df_noise.clone(), }; let log = SIDEINFO_LOG.get_or_init(|| std::sync::Mutex::new(Vec::new())); if let Ok(mut l) = log.lock() { @@ -436,6 +470,8 @@ mod tests { q_q: vec![vec![2i32; n_q]], invf_mode: vec![0; n_q], add_harmonic: None, + df_env: vec![], + df_noise: vec![], } } diff --git a/crates/ec-aac/src/sbr_env.rs b/crates/ec-aac/src/sbr_env.rs index 51ce923a..2fb45f93 100644 --- a/crates/ec-aac/src/sbr_env.rs +++ b/crates/ec-aac/src/sbr_env.rs @@ -848,6 +848,8 @@ mod tests { q_q: vec![vec![]], invf_mode: vec![], add_harmonic: None, + df_env: vec![], + df_noise: vec![], }; let target = 4.0f64; let env_energy = vec![vec![target; tables.n_low]]; @@ -940,6 +942,8 @@ mod tests { q_q: vec![vec![]], invf_mode: vec![], add_harmonic: None, + df_env: vec![], + df_noise: vec![], }; let env_energy = vec![vec![1.0e6f64; tables.n_low]]; // (Round-21) noise_energy is now a Q_div ratio against the cell's @@ -1001,6 +1005,8 @@ mod tests { q_q: vec![vec![]], invf_mode: vec![], add_harmonic: None, + df_env: vec![], + df_noise: vec![], }; let target = 4.0f64; let env_energy = vec![vec![target; tables.n_low]]; diff --git a/crates/ec-aac/src/sbr_payload.rs b/crates/ec-aac/src/sbr_payload.rs index 2d97900c..5df71cfd 100644 --- a/crates/ec-aac/src/sbr_payload.rs +++ b/crates/ec-aac/src/sbr_payload.rs @@ -55,6 +55,11 @@ pub struct SbrChannel { pub invf_mode: Vec, /// `bs_add_harmonic` per high-resolution band, when the flag is set. pub add_harmonic: Option>, + /// `bs_df_env` per envelope: `1` selects delta-time coding over + /// delta-frequency (diagnostic only, not consumed by the DSP stage). + pub df_env: Vec, + /// `bs_df_noise` per noise floor, same convention as `df_env`. + pub df_noise: Vec, } /// One `sbr_data()` frame: one channel for an SCE, two for a CPE. @@ -621,6 +626,8 @@ impl SbrParser { q_q, invf_mode: invf[ch].clone(), add_harmonic: None, + df_env: df_env[ch].iter().map(|&b| u8::from(b)).collect(), + df_noise: df_noise[ch].iter().map(|&b| u8::from(b)).collect(), }); } diff --git a/crates/ec-aac/tests/sbr_real_library.rs b/crates/ec-aac/tests/sbr_real_library.rs index e1437444..0a234136 100644 --- a/crates/ec-aac/tests/sbr_real_library.rs +++ b/crates/ec-aac/tests/sbr_real_library.rs @@ -2097,11 +2097,21 @@ fn full_chain_low_band_matches_own_core() { let cutoff = f64::from(core_rate) * 0.4; let ol = lowpass(&full[ch], full_rate, cutoff); let ul = lowpass(&up, full_rate, cutoff); - let (lag, corr) = best_lag_correlation(&ol, &ul); - println!(" ch{ch}: lag {lag}, corr {corr:.6}"); - println!( - " ch{ch} @ rate-independent QMF delay -897: corr {:.6}", - correlation_at_lag(&ol, &ul, -897) + // Wide search, not the plain `best_lag_correlation`'s + // `LAG_MAX=4000` -- the QMF-chain delay this measures is not a + // fixed constant across files/rates (measured -576 on reeln + // vs the stale -897 this test used to assert against), so the + // lag itself is part of what's measured here, not assumed. + const WIDE_LAG_MAX: i64 = 20_000; + let (lag, corr) = best_lag_correlation_wide(&ol, &ul, WIDE_LAG_MAX); + println!(" ch{ch}: measured best lag {lag}, corr {corr:.6}"); + assert!( + corr >= 0.99, + "{} ch{ch}: full-chain low band vs our own core-only decode \ + corr {corr:.6} at measured lag {lag} < 0.99 -- the \ + synthesis/gain-adjust stage is corrupting even the untouched \ + low-band region copied straight from core analysis", + c.path.display() ); } } @@ -2273,35 +2283,66 @@ fn synthetic_heaac_matrix() { 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. + // band vs our own core-only decode), run per-row so a + // rate/kx-specific self-inconsistency shows in the same + // table instead of needing a second pass. + // + // Fixed (this round): the original convention naively + // nearest-neighbor UPSAMPLED the core 2x and compared it + // against the full-rate signal, low-passed at + // `core_rate*0.4` -- that stairstep-and-lowpass round trip + // is lossy enough on its own to sink a genuinely-correct + // pair's correlation (measured flat ~-0.12 here even on + // rows whose `full`/`below` columns read 0.999+ against the + // real reference). `core_only_matches_reference` already + // proved a clean pattern for this exact comparison: stay in + // the CORE's own native rate (DECIMATE the full/reference + // signal down instead of upsampling the core up) and use a + // wide lag search there. `decimate` below is a lowpass + // (well under the core Nyquist) followed by a plain + // every-other-sample drop -- the inverse of the old + // `[s, s]` upsample, at the same information cost but + // without its stairstep artifact. + let decimate = |s: &[f32], from_rate: u32, to_rate: u32| -> Vec { + let cutoff = f64::from(to_rate) * 0.45; + lowpass(s, from_rate, cutoff) + .iter() + .step_by((from_rate / to_rate.max(1)).max(1) as usize) + .copied() + .collect() + }; + let core_lag_max = WIDE_LAG_MAX / 2; 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_wide(&ol, &ul, WIDE_LAG_MAX) + let full_at_core_rate = decimate(o, our_rate, core_rate); + best_lag_correlation_wide(&full_at_core_rate, &core[0], core_lag_max) }) } else { None }; // Coordinator follow-up 2: splits "our core decode of THIS // fixture is wrong" (core_v_ref ~0) from "the SBR chain - // scrambles a good core" (core_v_ref >= ~0.99 while `below` - // above stays ~0/negative) -- OUR core-only decode (2x - // upsampled, same convention `ours_v_core` uses) against the - // REFERENCE decoder's own decode, below the crossover only, - // wide lag search since this is an independent alignment - // from `full`/`below`'s (core-only carries no SBR delay). + // scrambles a good core" (core_v_ref >= ~0.99 while `below`/ + // `above` stays ~0/negative) -- OUR core-only decode, same + // native-core-rate convention `ours_v_core` now uses, against + // the REFERENCE decoder's own decode resampled to that same + // core rate (`ffmpeg_decode_at_rate`, the same helper + // `core_only_matches_reference` already trusts) -- wide lag + // search since this is an independent alignment from + // `full`/`below`'s (core-only carries no SBR delay). let (core_v_ref, core_rate_str) = if kx > 0 { match our_decode_core_only(&path, 0) { Some((core, core_rate)) => { - let up: Vec = core[0].iter().flat_map(|&s| [s, s]).collect(); - let tl = lowpass(t, our_rate, crossover_hz); - let ul = lowpass(&up, our_rate, crossover_hz); - (Some(best_lag_correlation_wide(&ul, &tl, WIDE_LAG_MAX)), core_rate.to_string()) + let ref_at_core_rate = + ffmpeg_decode_at_rate(&path, 0, core.len(), core_rate); + ( + Some(best_lag_correlation_wide( + &core[0], + &ref_at_core_rate[0], + core_lag_max, + )), + core_rate.to_string(), + ) } None => (None, "n/a".into()), } @@ -3262,3 +3303,131 @@ fn sbr_actual_noise_fraction() { 0.1787 pre-fix -- shape does not move this metric, see doc above)" ); } + +/// Discriminator instrument: one row per stream of the SBR header/frame +/// features actually parsed from its first ~20 `sbr_data()` frames, next to +/// that stream's own pass/fail correlation vs the reference decoder -- +/// looking for which feature(s) separate the two known-PASS streams +/// (reeln, synthetic 48k HE) from the two known-FAIL families (FMJ, +/// synthetic 44.1k HE). Gated on the real files / fixture directory being +/// present, same as the tests it borrows machinery from; skips loudly, +/// generates nothing. +#[test] +fn sbr_header_feature_table() { + if !have_ffmpeg() { + eprintln!("SKIP: ffmpeg not on PATH"); + return; + } + struct Row { + label: String, + path: PathBuf, + aac_stream: usize, + ffmpeg_stream: usize, + } + let mut rows: Vec = candidates() + .into_iter() + .map(|c| Row { + label: c + .path + .file_stem() + .map(|s| s.to_string_lossy().into_owned()) + .unwrap_or_default(), + path: c.path, + aac_stream: c.aac_stream, + ffmpeg_stream: c.ffmpeg_stream, + }) + .collect(); + let fixtures = std::env::var("EC_AAC_HEAAC_FIXTURES").unwrap_or_else(|_| { + format!("{}/../../.cache/heaac-fixtures", env!("CARGO_MANIFEST_DIR")) + }); + let fixtures = PathBuf::from(fixtures); + if fixtures.is_dir() { + for rate in [48_000u32, 44_100] { + for br in ["32k", "48k", "64k", "96k"] { + let p = fixtures.join(format!("heaac_{rate}_{br}.m4a")); + if p.exists() { + rows.push(Row { + label: format!("synth-{rate}-{br}"), + path: p, + aac_stream: 0, + ffmpeg_stream: 0, + }); + } + } + } + } else { + eprintln!( + "SKIP synthetic rows: {} absent -- run scripts/aac-tables/make-heaac-fixtures.sh first", + fixtures.display() + ); + } + if rows.is_empty() { + eprintln!("SKIP: no real HE-AAC files and no synthetic fixtures found"); + return; + } + unsafe { + std::env::set_var("EC_AAC_SBR_SIDEINFO_DEBUG", "1"); + } + const WIDE_LAG_MAX: i64 = 20_000; + println!( + "{:<20} {:>6} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>5} {:>10} {:>10} {:>8} {:>8} {:>10} {:>6}", + "stream", "rate", "kx", "k2", "xovr", "sfrq", "efrq", "fscl", "ascl", "nbnd", "lbnd", "lgn", + "ifrq", "smooth", "envs", "invf", "harm", "df%", "corr" + ); + for r in &rows { + let before = ec_aac::sbr_sideinfo_log().len(); + let Some((ours, sbr, our_rate)) = our_decode(&r.path, r.aac_stream) else { + eprintln!("SKIP {}: could not decode", r.label); + continue; + }; + if sbr != ec_aac::SbrSupport::V1 { + eprintln!("SKIP {}: not SBR v1", r.label); + continue; + } + let log = ec_aac::sbr_sideinfo_log(); + let frames: Vec<_> = log[before..].iter().filter(|row| row.ch == 0).take(20).collect(); + if frames.is_empty() { + eprintln!("SKIP {}: no sideinfo rows captured", r.label); + continue; + } + let f0 = &frames[0]; + let mut env_counts: std::collections::BTreeMap = Default::default(); + let mut invf_seen: std::collections::BTreeSet = Default::default(); + let mut any_harmonic = false; + let mut df_total = 0usize; + let mut df_ones = 0usize; + for row in &frames { + *env_counts.entry(row.t_env.len().saturating_sub(1)).or_default() += 1; + invf_seen.extend(&row.invf_mode); + if row.add_harmonic.as_ref().is_some_and(|h| h.iter().any(|&v| v != 0)) { + any_harmonic = true; + } + df_total += row.df_env.len() + row.df_noise.len(); + df_ones += row.df_env.iter().chain(&row.df_noise).filter(|&&b| b != 0).count(); + } + let df_pct = if df_total > 0 { + 100.0 * df_ones as f64 / df_total as f64 + } else { + 0.0 + }; + let envs_hist: String = env_counts + .iter() + .map(|(k, v)| format!("{k}x{v}")) + .collect::>() + .join(","); + let invf_str: String = invf_seen.iter().map(|v| v.to_string()).collect::>().join(""); + let theirs = ffmpeg_decode(&r.path, r.ffmpeg_stream, ours.len()); + let (_, corr) = best_lag_correlation_wide(&ours[0], &theirs[0], WIDE_LAG_MAX); + println!( + "{:<20} {:>6} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>5} {:>10} {:>10} {:>8} {:>8} {:>10.1} {:>6.3}", + r.label, our_rate, f0.kx, f0.k2, f0.xover_band, f0.start_freq, f0.stop_freq, + f0.freq_scale, f0.alter_scale, f0.noise_bands, f0.limiter_bands, f0.limiter_gains, + f0.interpol_freq, f0.smoothing_mode, envs_hist, invf_str, + any_harmonic, df_pct, corr + ); + println!( + " patches (frame0): {:?}", + f0.patch_lengths + ); + } +}