From ea6cc30c0f70b1f86bceedcc0376ae5a0923934c 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 05:38:21 +0300 Subject: [PATCH] debug(aac): QMF-domain per-stage SBR HF energy dump, gated by EC_AAC_SBR_QMFDUMP Round-34's decisive measurement: dump per-QMF-band mean energy at analysis output, post-patch (pre-adjust), and post-adjust in sbr_chain.rs, and probe header A (kx=14/k2=43) at source bands p=0/5/13. Result: generate()'s patch application places content EXACTLY where build_patches predicts (verdict: generate()/adjust()/the kx-row synthesis copy are all exonerated) -- the "wrong target band" cluster reported by earlier rounds' sbrpatchmap.py probe was an artifact of that probe's own tone-placement bin math (k = p*32+16 assumed 32 core-MDCT bins per QMF band; empirically re-derived via this same dump to be 16, confirmed exact for p=0/5/13) plus a noise0=[2,2] "quiet" setting that was actually loud (low raw index = louder noise floor, matching sbr_env::dequant_noise's spec-correct direction). Both fixed in sbrpatchmap.py. The QMF-domain sanity gate (post-patch stage) now matches build_patches' prediction exactly for every probed p; the PCM/FFT-domain frequency-line readout still shows a separate, weaker artifact specific to this single-isolated-tone-with-flat-envelope probe shape (not chased this round -- out of Task 1's QMF-domain scope, and not corroborated by the real-file sweep, which is unchanged: 0.971793/0.967975 full-band, 0.956522/0.951190 whole-file mean, 0.992565/0.991558 below-crossover, identical to the pre-round-34 baseline since this change adds no new always-on code path). --- crates/ec-aac/src/sbr_chain.rs | 38 ++++++++++++++++++++++++++++ scripts/aac-tables/sbrpatchmap.py | 41 +++++++++++++++++++++---------- 2 files changed, 66 insertions(+), 13 deletions(-) diff --git a/crates/ec-aac/src/sbr_chain.rs b/crates/ec-aac/src/sbr_chain.rs index a80d6a0e..d0fafa43 100644 --- a/crates/ec-aac/src/sbr_chain.rs +++ b/crates/ec-aac/src/sbr_chain.rs @@ -101,6 +101,34 @@ fn sideinfo_enabled() -> bool { std::env::var("EC_AAC_SBR_SIDEINFO_DEBUG").is_ok() } +/// Round-34 Task 1 diagnostic: per-AU per-QMF-band mean energy at a named +/// pipeline stage, gated by `EC_AAC_SBR_QMFDUMP`. `start_band` is the +/// absolute QMF band index `rows[0]` represents (`0` for the core analysis +/// matrix, `kx` for the HF matrix), so every printed band index lines up +/// with `build_patches`' target-band units without the reader having to +/// offset anything by hand. +fn qmfdump_enabled() -> bool { + std::env::var("EC_AAC_SBR_QMFDUMP").is_ok() +} + +fn qmfdump_energies(stage: &str, start_band: usize, rows: &[Vec>]) { + let means: Vec = rows + .iter() + .map(|series| { + if series.is_empty() { + 0.0 + } else { + series + .iter() + .map(|c| c.re * c.re + c.im * c.im) + .sum::() + / series.len() as f64 + } + }) + .collect(); + eprintln!("QMFDUMP stage={stage} start_band={start_band} means={means:?}"); +} + /// Drains nothing -- returns a clone of every row logged so far this /// process, in call order. Empty unless `EC_AAC_SBR_SIDEINFO_DEBUG` was set /// before decoding. @@ -303,6 +331,10 @@ impl SbrChain { l.push(row); } } + let qmfdump_on = qmfdump_enabled(); + if qmfdump_on { + qmfdump_energies("analysis", 0, &low_cur); + } let mut hf = sbr_hf::generate( &low_cur, &tables, @@ -310,6 +342,9 @@ impl SbrChain { &mut state.chirp, &mut state.hf_hist, ); + if qmfdump_on { + qmfdump_energies("post_patch", tables.kx as usize, &hf); + } let (env_energy, noise_energy) = sbr_env::dequantize_frame(&header, &data.channels, ch, coupling); // sbr_env::adjust works in QMF-slot units; the parsed borders @@ -338,6 +373,9 @@ impl SbrChain { &mut state.noise, &limiter_table, ); + if qmfdump_on { + qmfdump_energies("post_adjust", tables.kx as usize, &hf); + } let kx = (tables.kx as usize).min(ANALYSIS_BANDS); let k2 = (tables.k2 as usize).min(SYNTHESIS_BANDS); diff --git a/scripts/aac-tables/sbrpatchmap.py b/scripts/aac-tables/sbrpatchmap.py index e0dac0f8..a94f0d1e 100644 --- a/scripts/aac-tables/sbrpatchmap.py +++ b/scripts/aac-tables/sbrpatchmap.py @@ -134,16 +134,21 @@ def ref_decode(frames, tag): os.remove(mp4) return np.frombuffer(dec.stdout, dtype="