forge
Explore
Log in

Don't you dare go Hollow

debug(aac): QMF-domain per-stage SBR HF energy dump, gated by EC_AAC_SBR_QMFDUMP

Browse files at this commit

ea6cc30c0f70b1f86bceedcc0376ae5a0923934cAhmet Kaan GümüşParent: 0a60b22
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.rsmodified+38 -0
@@ -101,6 +101,34 @@ fn sideinfo_enabled() -> bool {
101 std::env::var("EC_AAC_SBR_SIDEINFO_DEBUG").is_ok()101 std::env::var("EC_AAC_SBR_SIDEINFO_DEBUG").is_ok()
102 }102 }
103 103
104+/// Round-34 Task 1 diagnostic: per-AU per-QMF-band mean energy at a named
105+/// pipeline stage, gated by `EC_AAC_SBR_QMFDUMP`. `start_band` is the
106+/// absolute QMF band index `rows[0]` represents (`0` for the core analysis
107+/// matrix, `kx` for the HF matrix), so every printed band index lines up
108+/// with `build_patches`' target-band units without the reader having to
109+/// offset anything by hand.
110+fn qmfdump_enabled() -> bool {
111+ std::env::var("EC_AAC_SBR_QMFDUMP").is_ok()
112+}
113+
114+fn qmfdump_energies(stage: &str, start_band: usize, rows: &[Vec<Complex<f64>>]) {
115+ let means: Vec<f64> = rows
116+ .iter()
117+ .map(|series| {
118+ if series.is_empty() {
119+ 0.0
120+ } else {
121+ series
122+ .iter()
123+ .map(|c| c.re * c.re + c.im * c.im)
124+ .sum::<f64>()
125+ / series.len() as f64
126+ }
127+ })
128+ .collect();
129+ eprintln!("QMFDUMP stage={stage} start_band={start_band} means={means:?}");
130+}
131+
104 /// Drains nothing -- returns a clone of every row logged so far this132 /// Drains nothing -- returns a clone of every row logged so far this
105 /// process, in call order. Empty unless `EC_AAC_SBR_SIDEINFO_DEBUG` was set133 /// process, in call order. Empty unless `EC_AAC_SBR_SIDEINFO_DEBUG` was set
106 /// before decoding.134 /// before decoding.
@@ -303,6 +331,10 @@ impl SbrChain {
303 l.push(row);331 l.push(row);
304 }332 }
305 }333 }
334+ let qmfdump_on = qmfdump_enabled();
335+ if qmfdump_on {
336+ qmfdump_energies("analysis", 0, &low_cur);
337+ }
306 let mut hf = sbr_hf::generate(338 let mut hf = sbr_hf::generate(
307 &low_cur,339 &low_cur,
308 &tables,340 &tables,
@@ -310,6 +342,9 @@ impl SbrChain {
310 &mut state.chirp,342 &mut state.chirp,
311 &mut state.hf_hist,343 &mut state.hf_hist,
312 );344 );
345+ if qmfdump_on {
346+ qmfdump_energies("post_patch", tables.kx as usize, &hf);
347+ }
313 let (env_energy, noise_energy) =348 let (env_energy, noise_energy) =
314 sbr_env::dequantize_frame(&header, &data.channels, ch, coupling);349 sbr_env::dequantize_frame(&header, &data.channels, ch, coupling);
315 // sbr_env::adjust works in QMF-slot units; the parsed borders350 // sbr_env::adjust works in QMF-slot units; the parsed borders
@@ -338,6 +373,9 @@ impl SbrChain {
338 &mut state.noise,373 &mut state.noise,
339 &limiter_table,374 &limiter_table,
340 );375 );
376+ if qmfdump_on {
377+ qmfdump_energies("post_adjust", tables.kx as usize, &hf);
378+ }
341 379
342 let kx = (tables.kx as usize).min(ANALYSIS_BANDS);380 let kx = (tables.kx as usize).min(ANALYSIS_BANDS);
343 let k2 = (tables.k2 as usize).min(SYNTHESIS_BANDS);381 let k2 = (tables.k2 as usize).min(SYNTHESIS_BANDS);
scripts/aac-tables/sbrpatchmap.pymodified+28 -13
@@ -134,16 +134,21 @@ def ref_decode(frames, tag):
134 os.remove(mp4)134 os.remove(mp4)
135 return np.frombuffer(dec.stdout, dtype="<f4").astype(np.float64), dec.stderr.decode(errors="replace")135 return np.frombuffer(dec.stdout, dtype="<f4").astype(np.float64), dec.stderr.decode(errors="replace")
136 BAND_HZ = RATE / 128 # QMF band width, both source and target index units136 BAND_HZ = RATE / 128 # QMF band width, both source and target index units
137-# Round-30 fix: a source (low) QMF band is 32 core-MDCT bins wide (1024-bin137+# Round-34 fix: round-30's "32 core-MDCT bins per QMF band" was itself wrong
138-# core MDCT / 32 core bands), not 16 -- every prior round's k = p*16 + offset138+# by 2x. Empirically re-derived by dumping our own decoder's QMF analysis
139-# was reading the wrong bin for band p entirely, not just the wrong in-band139+# stage (EC_AAC_SBR_QMFDUMP=1, crates/ec-aac/src/sbr_chain.rs) for k =
140-# offset. Placed at the BAND CENTER (bin 16 of the 32), which lands exactly140+# active_p*32+16 across several p: the dominant analysis band always read
141-# on a 4-bin group boundary (16 % 4 == 0) so tone_core's grouping needs no141+# back as 2p or 2p+1, never p -- a source (low) QMF band is 16 core-MDCT
142-# fractional-offset trick. A center tone's fractional position within its142+# bins wide (1024-bin core MDCT / 64, not / 32), confirmed by re-running the
143-# 32-bin source band is 0.5; patches translate by whole band widths, so the143+# same dump with k = active_p*16+8 and seeing the dominant band land exactly
144-# translated line keeps frac == 0.5 in its target band too (or 1-0.5 == 0.5144+# on p for every probed value (0, 5, 13). Placed at the BAND CENTER (bin 8
145-# under mirroring -- indistinguishable at the center, which is fine, this145+# of the 16), which lands on a 4-bin group boundary (8 % 4 == 0) so
146-# probe only needs the TARGET BAND INDEX, not direct/mirror discrimination).146+# tone_core's grouping needs no fractional-offset trick. A center tone's
147+# fractional position within its 16-bin source band is 0.5; patches
148+# translate by whole band widths, so the translated line keeps frac == 0.5
149+# in its target band too (or 1-0.5 == 0.5 under mirroring -- indistinguishable
150+# at the center, which is fine, this probe only needs the TARGET BAND INDEX,
151+# not direct/mirror discrimination).
147 D = 0.5 # fractional offset (of one band width) the probe tone is placed at152 D = 0.5 # fractional offset (of one band width) the probe tone is placed at
148 NO_QUANT_WARNING = "No quantized data read for sbr_dequant."153 NO_QUANT_WARNING = "No quantized data read for sbr_dequant."
149 154
@@ -204,9 +209,19 @@ def stream_one_band(cfg, tables, active_p, n_frames=24, invf_mode=0):
204 healthy mid value, noise0 at the quiet end so noise floor cannot bury the209 healthy mid value, noise0 at the quiet end so noise floor cannot bury the
205 line), core = one isolated tone at low QMF band `active_p`'s SECOND 4-bin210 line), core = one isolated tone at low QMF band `active_p`'s SECOND 4-bin
206 group (offset `D` of a band width into the band, not the ambiguous211 group (offset `D` of a band width into the band, not the ambiguous
207- boundary bin), held fixed; invf NONE, no harmonics."""212+ boundary bin), held fixed; invf NONE, no harmonics.
213+
214+ Round-34: noise0's raw index runs the OPPOSITE direction from what this
215+ docstring's "quiet end" phrase assumed -- `dequant_noise`
216+ (`crates/ec-aac/src/sbr_env.rs`) is `2^(NOISE_FLOOR_OFFSET - q_q)`, so a
217+ LOW raw index is the LOUD end (matches the spec's own noise-floor
218+ dequantization direction) and `noise0=[2, 2]` was injecting a broadband
219+ noise floor loud enough to swamp the QMF-domain energy of the probe's own
220+ tone (confirmed via EC_AAC_SBR_QMFDUMP=1: post-adjust energy was flat
221+ across every target band, not peaked at the patched ones). `[30, 30]` is
222+ the genuinely quiet end."""
208 swb = P.SWB_LONG[SF_INDEX]223 swb = P.SWB_LONG[SF_INDEX]
209- k = active_p * 32 + 16224+ k = active_p * 16 + 8
210 found = bin_to_sfb_group(k, swb)225 found = bin_to_sfb_group(k, swb)
211 assert found is not None, f"band {active_p} bin {k} not in SWB_LONG[{SF_INDEX}]"226 assert found is not None, f"band {active_p} bin {k} not in SWB_LONG[{SF_INDEX}]"
212 sfb_idx, group_active = found227 sfb_idx, group_active = found
@@ -227,7 +242,7 @@ def stream_one_band(cfg, tables, active_p, n_frames=24, invf_mode=0):
227 n_slots = 2 * (tables["n_low"] - 1) + 2 * (tables["n_q"] - 1)242 n_slots = 2 * (tables["n_low"] - 1) + 2 * (tables["n_q"] - 1)
228 for _ in range(n_frames):243 for _ in range(n_frames):
229 c = dict(cfg, invf=invf_mode, header=1, amp_res=1, element="sce", coupling=0,244 c = dict(cfg, invf=invf_mode, header=1, amp_res=1, element="sce", coupling=0,
230- df_env=[0] * 8, df_noise=[0, 0], env0=[40] * 8, noise0=[2, 2])245+ df_env=[0] * 8, df_noise=[0, 0], env0=[40] * 8, noise0=[30, 30])
231 w = P.BitW()246 w = P.BitW()
232 C.tone_core(w, sfb_idx, group_active)247 C.tone_core(w, sfb_idx, group_active)
233 body = F.sbr_bits_full(c, tables, grid, [zero_delta] * n_slots)248 body = F.sbr_bits_full(c, tables, grid, [zero_delta] * n_slots)