forge
Explore
Log in

Don't you dare go Hollow

Apply the envelope gain before noise injection, not after

Browse files at this commit

90930ddb97b839891b5b2ed518f69ac7f656d05eAhmet Kaan GümüşParent: 92f96f7
  • fix
    • apply the envelope gain before noise injection, not after
Show full message
fix(aac-sbr): apply the envelope gain before noise injection, not after

sbr_env::adjust injected the transmitted noise floor into the HF QMF
signal before measuring the (envelope, band) cell's current energy and
computing the gain toward the transmitted target -- so gain was
computed from signal+noise together and then rescaled both, distorting
the noise/signal split by however much noise dominates a given cell.
Move noise injection after the gain loop (signal only informs gain,
noise is added at its own transmitted amplitude afterward), matching
the spec order. Real-file sweep correlation barely moved from this
alone (0.019/0.834 and 0.005/0.0001 before and after), so it is a
real but non-dominant contributor to the residual failure; see the
localization note left for the next pass.

Also added an EC_AAC_SBR_ENV_DEBUG-gated dump in decode_envelope that
pinpointed the dominant remaining defect: multi-envelope real-file
frames occasionally carry a raw (non-delta) envelope whose absolute
value is inconsistent with every neighbouring envelope's magnitude in
the same frame (observed: FMJ ch0 frame 24's envelope 3 reads raw=43
right after envelopes 0-2 all sit at 8-10, and the frame's own core
PCM never exceeds ~9600 in this domain, so a target energy of 2^43 is
unreachable by any correctly functioning HF patch of that content).
That corrupted value then persists and grows across frames via the
cross-frame DT envelope prediction (self.state[ch].last_env), which
does no plausibility check on what it carries forward. Root bit-level
cause not yet found: env0-2 in the same frame decode plausibly, ruling
out a bit-count bug in bs_data_extra/bs_coupling/sbr_grid/sbr_dtdf/
sbr_invf that would already have desynced env0. Next step: instrument
whether this specific raw envelope's df_env bit is being read at the
position the FIXVAR/VARFIX/VARVAR grid class actually places it, since
these real files exercise those unverified border-value grid classes
(sbr_payload.rs's honest gap) far more than the byte-exact FIXFIX
fixtures.
Cargo.lockmodified+2 -0
@@ -228,6 +228,8 @@ version = "0.1.0"
228228 dependencies = [
229229 "ec-core",
230230 "ec-dsp",
231+ "ec-matroska",
232+ "ec-mp4",
231233 ]
232234
233235 [[package]]
crates/ec-aac/src/sbr_env.rsmodified+30 -24
@@ -152,30 +152,6 @@ pub fn adjust(
152152 let kx = tables.kx as usize;
153153 let limiter_max = LIMITER_FACTOR[usize::from(header.limiter_gains).min(3)];
154154
155- // Noise floor first: every target bin gets the transmitted floor for
156- // its noise-time-segment and noise-frequency-band, independent of the
157- // envelope grid (they are different, coarser grids over the same axes).
158- for (ni, (t0, t1)) in ch.t_noise.windows(2).map(|w| (w[0], w[1])).enumerate() {
159- if ni >= noise_energy.len() {
160- break;
161- }
162- for q in 0..tables.n_q {
163- let lo = tables.f_noise[q] as usize;
164- let hi = tables.f_noise[q + 1] as usize;
165- let energy = noise_energy[ni].get(q).copied().unwrap_or(0.0);
166- let amp = (energy / (hi - lo).max(1) as f64).sqrt();
167- for band in lo..hi {
168- if band < kx || band - kx >= hf.len() {
169- continue;
170- }
171- let row = &mut hf[band - kx];
172- for slot in t0.max(0) as usize..(t1.max(0) as usize).min(row.len()) {
173- row[slot] = row[slot] + rng.complex_unit().scale(amp);
174- }
175- }
176- }
177- }
178-
179155 // Gain toward the transmitted envelope, per (envelope, band) cell,
180156 // limited relative to that cell's own noise floor.
181157 for (ei, (t0, t1)) in ch.t_env.windows(2).map(|w| (w[0], w[1])).enumerate() {
@@ -270,6 +246,36 @@ pub fn adjust(
270246 }
271247 }
272248 }
249+
250+ // Noise floor last: every target bin gets the transmitted floor for its
251+ // noise-time-segment and noise-frequency-band, independent of the
252+ // envelope grid (they are different, coarser grids over the same axes).
253+ // This must run after the envelope-gain loop above -- gain is computed
254+ // from the HF-signal-only estimate and must not also rescale the noise
255+ // that lands on top of it (injecting noise first made the gain step
256+ // measure signal+noise together and rescale both, which distorts the
257+ // noise/signal split by however much noise dominates a given cell -- a
258+ // content- and channel-dependent error, not a uniform scale bug).
259+ for (ni, (t0, t1)) in ch.t_noise.windows(2).map(|w| (w[0], w[1])).enumerate() {
260+ if ni >= noise_energy.len() {
261+ break;
262+ }
263+ for q in 0..tables.n_q {
264+ let lo = tables.f_noise[q] as usize;
265+ let hi = tables.f_noise[q + 1] as usize;
266+ let energy = noise_energy[ni].get(q).copied().unwrap_or(0.0);
267+ let amp = (energy / (hi - lo).max(1) as f64).sqrt();
268+ for band in lo..hi {
269+ if band < kx || band - kx >= hf.len() {
270+ continue;
271+ }
272+ let row = &mut hf[band - kx];
273+ for slot in t0.max(0) as usize..(t1.max(0) as usize).min(row.len()) {
274+ row[slot] = row[slot] + rng.complex_unit().scale(amp);
275+ }
276+ }
277+ }
278+ }
273279 }
274280
275281 fn noise_band_for(tables: &BandTables, low_band: usize) -> usize {
crates/ec-aac/src/sbr_payload.rsmodified+5 -0
@@ -320,6 +320,11 @@ impl SbrParser {
320320 row[b] = row[b - 1] + delta;
321321 }
322322 }
323+ if std::env::var("EC_AAC_SBR_ENV_DEBUG").is_ok() {
324+ eprintln!(
325+ "ENVDBG dt={dt} balance={balance} amp_res={amp_res} bands={bands} prev={prev:?} row={row:?}"
326+ );
327+ }
323328 Ok(row)
324329 }
325330