From 90930ddb97b839891b5b2ed518f69ac7f656d05e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 19 Aug 2026 19:27:28 +0300 Subject: [PATCH] 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.lock | 2 ++ crates/ec-aac/src/sbr_env.rs | 54 ++++++++++++++++++-------------- crates/ec-aac/src/sbr_payload.rs | 5 +++ 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1b07078a..b11de139 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -228,6 +228,8 @@ version = "0.1.0" dependencies = [ "ec-core", "ec-dsp", + "ec-matroska", + "ec-mp4", ] [[package]] diff --git a/crates/ec-aac/src/sbr_env.rs b/crates/ec-aac/src/sbr_env.rs index 81d3272e..c7f58140 100644 --- a/crates/ec-aac/src/sbr_env.rs +++ b/crates/ec-aac/src/sbr_env.rs @@ -152,30 +152,6 @@ pub fn adjust( let kx = tables.kx as usize; let limiter_max = LIMITER_FACTOR[usize::from(header.limiter_gains).min(3)]; - // Noise floor first: every target bin gets the transmitted floor for - // its noise-time-segment and noise-frequency-band, independent of the - // envelope grid (they are different, coarser grids over the same axes). - for (ni, (t0, t1)) in ch.t_noise.windows(2).map(|w| (w[0], w[1])).enumerate() { - if ni >= noise_energy.len() { - break; - } - for q in 0..tables.n_q { - let lo = tables.f_noise[q] as usize; - let hi = tables.f_noise[q + 1] as usize; - let energy = noise_energy[ni].get(q).copied().unwrap_or(0.0); - let amp = (energy / (hi - lo).max(1) as f64).sqrt(); - for band in lo..hi { - if band < kx || band - kx >= hf.len() { - continue; - } - let row = &mut hf[band - kx]; - for slot in t0.max(0) as usize..(t1.max(0) as usize).min(row.len()) { - row[slot] = row[slot] + rng.complex_unit().scale(amp); - } - } - } - } - // Gain toward the transmitted envelope, per (envelope, band) cell, // limited relative to that cell's own noise floor. for (ei, (t0, t1)) in ch.t_env.windows(2).map(|w| (w[0], w[1])).enumerate() { @@ -270,6 +246,36 @@ pub fn adjust( } } } + + // Noise floor last: every target bin gets the transmitted floor for its + // noise-time-segment and noise-frequency-band, independent of the + // envelope grid (they are different, coarser grids over the same axes). + // This must run after the envelope-gain loop above -- gain is computed + // from the HF-signal-only estimate and must not also rescale the noise + // that lands on top of it (injecting noise first made the gain step + // measure signal+noise together and rescale both, which distorts the + // noise/signal split by however much noise dominates a given cell -- a + // content- and channel-dependent error, not a uniform scale bug). + for (ni, (t0, t1)) in ch.t_noise.windows(2).map(|w| (w[0], w[1])).enumerate() { + if ni >= noise_energy.len() { + break; + } + for q in 0..tables.n_q { + let lo = tables.f_noise[q] as usize; + let hi = tables.f_noise[q + 1] as usize; + let energy = noise_energy[ni].get(q).copied().unwrap_or(0.0); + let amp = (energy / (hi - lo).max(1) as f64).sqrt(); + for band in lo..hi { + if band < kx || band - kx >= hf.len() { + continue; + } + let row = &mut hf[band - kx]; + for slot in t0.max(0) as usize..(t1.max(0) as usize).min(row.len()) { + row[slot] = row[slot] + rng.complex_unit().scale(amp); + } + } + } + } } fn noise_band_for(tables: &BandTables, low_band: usize) -> usize { diff --git a/crates/ec-aac/src/sbr_payload.rs b/crates/ec-aac/src/sbr_payload.rs index 8a321481..58fcd060 100644 --- a/crates/ec-aac/src/sbr_payload.rs +++ b/crates/ec-aac/src/sbr_payload.rs @@ -320,6 +320,11 @@ impl SbrParser { row[b] = row[b - 1] + delta; } } + if std::env::var("EC_AAC_SBR_ENV_DEBUG").is_ok() { + eprintln!( + "ENVDBG dt={dt} balance={balance} amp_res={amp_res} bands={bands} prev={prev:?} row={row:?}" + ); + } Ok(row) }