forge
Explore
Log in

Don't you dare go Hollow

debug(ec-aac): EC_AAC_SBR_CELLDUMP per-(envelope,band) noise-realization dump

Browse files at this commit

79dc3f66aa982b8135a5c38c0afc67e67bf1ff94Ahmet Kaan GümüşParent: f737a34
Round-43 Task 1: env-gated dump in sbr_env::adjust printing target/
signal_target/noise_here/cur/raw+capped gain/boost/namp and the actually-
injected (and post-addition) sample energy per absolute QMF band, so the
noise-realization gap can be read directly off real content instead of
guessed at.
crates/ec-aac/src/sbr_env.rsmodified+122 -0
@@ -271,6 +271,17 @@ pub fn adjust(
271271 // holding it flat -- probing whether our constant-per-cell gain shape
272272 // (vs. the spec's smoother per-slot shape) is a live suspect.
273273 let gain_lerp = std::env::var("EC_AAC_SBR_GAIN_LERP").is_ok();
274+ // (Round-43, Task 1) `EC_AAC_SBR_CELLDUMP=band[,band...]` -- comma list
275+ // of ABSOLUTE QMF band indices (same units as `kx`/`k2`/`f_high`,
276+ // `band_hz = rate/128`, per round-42's fixed test-side mapping) -- dumps
277+ // every (envelope, band) cell whose `[lo,hi)` range covers a listed
278+ // band: target/signal_target/noise_here/measured cur/raw+capped gain/
279+ // boost/namp/sinusoid amp, and the actually-injected noise energy that
280+ // frame, to stderr.
281+ let celldump: Vec<usize> = std::env::var("EC_AAC_SBR_CELLDUMP")
282+ .ok()
283+ .map(|s| s.split(',').filter_map(|t| t.trim().parse().ok()).collect())
284+ .unwrap_or_default();
274285
275286 // Gain toward the transmitted envelope, per (envelope, band) cell,
276287 // limited relative to that cell's own noise floor.
@@ -389,6 +400,14 @@ pub fn adjust(
389400 }
390401 }
391402 }
403+ // (Round-43, Task 1) `raw_gain` (pre-cap) is about to be overwritten
404+ // in place inside `gains` below; keep an uncapped copy for the
405+ // celldump so its "cap applied?" column can compare against it.
406+ let raw_gains: Vec<f64> = if celldump.is_empty() {
407+ Vec::new()
408+ } else {
409+ gains.clone()
410+ };
392411 // Pass 2: per-limiter-band aggregate cap, then per-band boost
393412 // compensation so the band still meets its transmitted total.
394413 let lim_cap: Vec<f64> = (0..n_lim)
@@ -437,6 +456,64 @@ pub fn adjust(
437456 // the band in `EC_AAC_SBR_NOISE_ANCHOR`.
438457 noise_amps.push(cell_noise_here[b].sqrt() * lim_boost[j]);
439458 }
459+ // (Round-43, Task 1) per-cell diagnostic dump for `EC_AAC_SBR_CELLDUMP`
460+ // -- printed before the application loop below so it reflects the
461+ // gain/noise-amplitude values that loop is about to use; the
462+ // actually-injected sample energy (which depends on the RNG draws
463+ // that loop consumes) is measured separately and printed right
464+ // after it, tagged with the same `ei`/band range so the two lines
465+ // pair up by eye.
466+ if !celldump.is_empty() {
467+ for b in 0..table.len().saturating_sub(1) {
468+ let lo = table[b] as usize;
469+ let hi = table[b + 1] as usize;
470+ if !celldump.iter().any(|&cb| cb >= lo && cb < hi) {
471+ continue;
472+ }
473+ let target = env_energy[ei].get(b).copied().unwrap_or(0.0);
474+ let noise_here = cell_noise_here[b];
475+ let signal_target = target - noise_here;
476+ let cur = cell_current[b];
477+ let raw_gain = raw_gains[b];
478+ let lim_idx = cell_lim[b];
479+ let capped = raw_gain > lim_cap[lim_idx] + 1e-12;
480+ let boost = lim_boost[lim_idx];
481+ let namp = noise_amps[b];
482+ // Harmonic flag/amp for any listed band this cell covers
483+ // (checked against the high-res `f_high` grid the sinusoid
484+ // block below uses, regardless of this cell's own
485+ // resolution).
486+ let mut harmonic = None;
487+ if let Some(flags) = &ch.add_harmonic {
488+ for (hb, &on) in flags.iter().enumerate() {
489+ if on == 0 || hb + 1 >= tables.f_high.len() {
490+ continue;
491+ }
492+ let hlo = tables.f_high[hb] as usize;
493+ let hhi = tables.f_high[hb + 1] as usize;
494+ if celldump
495+ .iter()
496+ .any(|&cb| cb >= hlo && cb < hhi && cb >= lo && cb < hi)
497+ {
498+ let e = if high_res {
499+ env_energy[ei].get(hb).copied().unwrap_or(0.0)
500+ } else {
501+ target
502+ };
503+ let hboost = lim_boost
504+ [limiter_band_index(limiter_table, hlo, hhi).min(n_lim - 1)];
505+ harmonic = Some((2.0 * e).sqrt() * hboost);
506+ }
507+ }
508+ }
509+ eprintln!(
510+ "CELLDUMP ei={ei} band[{lo},{hi}) target={target:.6e} \
511+ signal_target={signal_target:.6e} noise_here={noise_here:.6e} \
512+ cur={cur:.6e} raw_gain={raw_gain:.6} capped={capped} boost={boost:.6} \
513+ namp={namp:.6} harmonic_amp={harmonic:?}"
514+ );
515+ }
516+ }
440517 // (Round-22 sweep instrumentation) the next envelope's gains, only
441518 // when `EC_AAC_SBR_GAIN_LERP` is set and the band layouts match --
442519 // `hf` in the next envelope's time range is still untouched at this
@@ -488,6 +565,14 @@ pub fn adjust(
488565 // decoder's exact per-subband gain-interpolation formula, not a
489566 // plausible reconstruction of it.
490567 let span = (t1 - t0.max(0)).max(1) as f64;
568+ // (Round-43, Task 1) actual injected-noise-sample energy for the
569+ // celldump, summed per listed absolute band (the RNG draws it from
570+ // are only realized inside this loop, unlike everything the print
571+ // block above already knows).
572+ let mut celldump_injected: std::collections::HashMap<usize, (f64, f64, usize)> =
573+ std::collections::HashMap::new();
574+ let mut celldump_post: std::collections::HashMap<usize, (f64, usize)> =
575+ std::collections::HashMap::new();
491576 for b in 0..table.len().saturating_sub(1) {
492577 let lo = table[b] as usize;
493578 let hi = table[b + 1] as usize;
@@ -507,14 +592,51 @@ pub fn adjust(
507592 base_gain
508593 };
509594 row[slot] = row[slot].scale(gain);
595+ if !celldump.is_empty() && celldump.contains(&band) {
596+ let e = celldump_injected.entry(band).or_insert((0.0, 0.0, 0));
597+ e.1 += row[slot].norm_sqr(); // signal alone, pre-noise
598+ e.2 += 1;
599+ }
510600 // Consume the RNG unconditionally (matches the pre-round-21
511601 // stream shape) even under EC_AAC_SBR_NOISE_ZERO, which
512602 // only skips the PCM addition, not the bookkeeping.
513603 let n = rng.complex_unit().scale(namp);
604+ if !celldump.is_empty() && celldump.contains(&band) {
605+ let e = celldump_injected.entry(band).or_insert((0.0, 0.0, 0));
606+ e.0 += n.norm_sqr();
607+ }
514608 row[slot] = row[slot] + if zero_noise { Complex::ZERO } else { n };
609+ if !celldump.is_empty() && celldump.contains(&band) {
610+ celldump_post.entry(band).or_insert((0.0, 0)).0 += row[slot].norm_sqr();
611+ celldump_post.entry(band).or_insert((0.0, 0)).1 += 1;
612+ }
515613 }
516614 }
517615 }
616+ if !celldump.is_empty() {
617+ for (&band, &(noise_sum, signal_sum, count)) in &celldump_injected {
618+ let (post_sum, post_count) = celldump_post.get(&band).copied().unwrap_or((0.0, 0));
619+ eprintln!(
620+ "CELLDUMP ei={ei} band={band} actually_injected_noise_energy={:.6e} \
621+ actual_signal_only_energy={:.6e} post_addition_energy={:.6e} (n={count})",
622+ if count > 0 {
623+ noise_sum / count as f64
624+ } else {
625+ 0.0
626+ },
627+ if count > 0 {
628+ signal_sum / count as f64
629+ } else {
630+ 0.0
631+ },
632+ if post_count > 0 {
633+ post_sum / post_count as f64
634+ } else {
635+ 0.0
636+ },
637+ );
638+ }
639+ }
518640
519641 // Sinusoids: one extra tone per flagged high-resolution band,
520642 // added after gain so its amplitude is exact regardless of the