debug(aac): QMF-domain per-stage SBR HF energy dump, gated by EC_AAC_SBR_QMFDUMP
Browse files at this commitea6cc30c0f70b1f86bceedcc0376ae5a0923934cParent: 0a60b222 changed files +66 -13
crates/ec-aac/src/sbr_chain.rsmodified+38 -0
| @@ -101,6 +101,34 @@ fn sideinfo_enabled() -> bool { | ||
| 101 | 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 | 132 | /// Drains nothing -- returns a clone of every row logged so far this |
| 105 | 133 | /// process, in call order. Empty unless `EC_AAC_SBR_SIDEINFO_DEBUG` was set |
| 106 | 134 | /// before decoding. |
| @@ -303,6 +331,10 @@ impl SbrChain { | ||
| 303 | 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 | 338 | let mut hf = sbr_hf::generate( |
| 307 | 339 | &low_cur, |
| 308 | 340 | &tables, |
| @@ -310,6 +342,9 @@ impl SbrChain { | ||
| 310 | 342 | &mut state.chirp, |
| 311 | 343 | &mut state.hf_hist, |
| 312 | 344 | ); |
| 345 | + if qmfdump_on { | |
| 346 | + qmfdump_energies("post_patch", tables.kx as usize, &hf); | |
| 347 | + } | |
| 313 | 348 | let (env_energy, noise_energy) = |
| 314 | 349 | sbr_env::dequantize_frame(&header, &data.channels, ch, coupling); |
| 315 | 350 | // sbr_env::adjust works in QMF-slot units; the parsed borders |
| @@ -338,6 +373,9 @@ impl SbrChain { | ||
| 338 | 373 | &mut state.noise, |
| 339 | 374 | &limiter_table, |
| 340 | 375 | ); |
| 376 | + if qmfdump_on { | |
| 377 | + qmfdump_energies("post_adjust", tables.kx as usize, &hf); | |
| 378 | + } | |
| 341 | 379 | |
| 342 | 380 | let kx = (tables.kx as usize).min(ANALYSIS_BANDS); |
| 343 | 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 | 134 | os.remove(mp4) |
| 135 | 135 | return np.frombuffer(dec.stdout, dtype="<f4").astype(np.float64), dec.stderr.decode(errors="replace") |
| 136 | 136 | 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-bin | |
| 138 | -# core MDCT / 32 core bands), not 16 -- every prior round's k = p*16 + offset | |
| 139 | -# was reading the wrong bin for band p entirely, not just the wrong in-band | |
| 140 | -# offset. Placed at the BAND CENTER (bin 16 of the 32), which lands exactly | |
| 141 | -# on a 4-bin group boundary (16 % 4 == 0) so tone_core's grouping needs no | |
| 142 | -# fractional-offset trick. A center tone's fractional position within its | |
| 143 | -# 32-bin source band is 0.5; patches translate by whole band widths, so the | |
| 144 | -# translated line keeps frac == 0.5 in its target band too (or 1-0.5 == 0.5 | |
| 145 | -# under mirroring -- indistinguishable at the center, which is fine, this | |
| 146 | -# probe only needs the TARGET BAND INDEX, not direct/mirror discrimination). | |
| 137 | +# Round-34 fix: round-30's "32 core-MDCT bins per QMF band" was itself wrong | |
| 138 | +# by 2x. Empirically re-derived by dumping our own decoder's QMF analysis | |
| 139 | +# stage (EC_AAC_SBR_QMFDUMP=1, crates/ec-aac/src/sbr_chain.rs) for k = | |
| 140 | +# active_p*32+16 across several p: the dominant analysis band always read | |
| 141 | +# back as 2p or 2p+1, never p -- a source (low) QMF band is 16 core-MDCT | |
| 142 | +# bins wide (1024-bin core MDCT / 64, not / 32), confirmed by re-running the | |
| 143 | +# same dump with k = active_p*16+8 and seeing the dominant band land exactly | |
| 144 | +# on p for every probed value (0, 5, 13). Placed at the BAND CENTER (bin 8 | |
| 145 | +# of the 16), which lands on a 4-bin group boundary (8 % 4 == 0) so | |
| 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 | 152 | D = 0.5 # fractional offset (of one band width) the probe tone is placed at |
| 148 | 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 | 209 | healthy mid value, noise0 at the quiet end so noise floor cannot bury the |
| 205 | 210 | line), core = one isolated tone at low QMF band `active_p`'s SECOND 4-bin |
| 206 | 211 | 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 | 223 | swb = P.SWB_LONG[SF_INDEX] |
| 209 | - k = active_p * 32 + 16 | |
| 224 | + k = active_p * 16 + 8 | |
| 210 | 225 | found = bin_to_sfb_group(k, swb) |
| 211 | 226 | assert found is not None, f"band {active_p} bin {k} not in SWB_LONG[{SF_INDEX}]" |
| 212 | 227 | sfb_idx, group_active = found |
| @@ -227,7 +242,7 @@ def stream_one_band(cfg, tables, active_p, n_frames=24, invf_mode=0): | ||
| 227 | 242 | n_slots = 2 * (tables["n_low"] - 1) + 2 * (tables["n_q"] - 1) |
| 228 | 243 | for _ in range(n_frames): |
| 229 | 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 | 246 | w = P.BitW() |
| 232 | 247 | C.tone_core(w, sfb_idx, group_active) |
| 233 | 248 | body = F.sbr_bits_full(c, tables, grid, [zero_delta] * n_slots) |