From ade9b5453e2d48576fe156dbeee2a0f4b25b23db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 13:34:58 +0300 Subject: [PATCH] U10: ec-aac drop env experiments (EC_AAC_SBR_BW_SCALE, CHIRP_SMOOTH, BW_DUMP, ENV_DEBUG; defaults kept) and dead band_shift_correction --- crates/ec-aac/src/sbr_hf.rs | 135 +++---------------------------- crates/ec-aac/src/sbr_payload.rs | 5 -- 2 files changed, 9 insertions(+), 131 deletions(-) diff --git a/crates/ec-aac/src/sbr_hf.rs b/crates/ec-aac/src/sbr_hf.rs index 7420658f..eed959ac 100644 --- a/crates/ec-aac/src/sbr_hf.rs +++ b/crates/ec-aac/src/sbr_hf.rs @@ -57,64 +57,6 @@ use crate::sbr_bands::BandTables; use ec_dsp::Complex; -/// Characterizes (but, as of round-37, is deliberately NOT applied to) a QMF -/// bank convention gap `sbr_qmf`'s round-trip tuning never exercised: -/// replaying a source band's raw complex series into a *different* synthesis -/// band (exactly what patch copying does) lands the content at the "wrong" -/// absolute frequency by exactly one synthesis-band width whenever -/// `target_band - source_band` is odd, with the sign flipping every other -/// odd step -- i.e. a period-4 pattern in `target - source` (even -/// difference: no error; `+1 mod 4`: content lands one band flat; `+3 mod -/// 4`: one band sharp). `sbr_qmf::Synthesis`'s modulation kernel centres -/// band `k` at `(k+0.5) * pi/(2*SYNTHESIS_BANDS)` via a phase intercept that -/// is itself `(k+0.5) * pi/4` -- exactly the odd-stacked cosine-modulated- -/// bank convention the round-trip test tuned for same-band replay, where -/// this intercept cancels against the receiving band's own reconstruction. -/// -/// Round-35 had `generate` counter-rotate the patched series by this amount -/// before it reaches `Synthesis`, and proved (in isolation, via a -/// stationary-tone FFT probe through the actual production call site -- -/// `sbr_qmf::tests::patch_replay_through_generate_preserves_absolute_frequency_mapping`) -/// that doing so restores textbook-coherent absolute-frequency placement. -/// Round-36/37's real-file mechanical A/B (`EC_AAC_SBR_PARITY_SPLIT`) -/// showed applying that correction REGRESSES odd-gap coherence against the -/// reference decoder (ch0 0.1907->0.0714, ch1 0.2461->0.0741; a -/// sign-flipped variant and a half-rate variant both regress it too, to -/// 0.0757 and 0.1449 respectively on ch0) -- the reference apparently -/// reproduces this same "wrong" convention itself, so leaving the raw copy -/// unrotated is what actually matches it. This function is kept (and -/// pinned by its own unit tests) purely to document that convention as -/// intentional, not to be called from `generate`'s patch-copy site. -/// -/// Round-46 raised a re-trial candidate: `EC_AAC_SBR_PARITY_SPLIT` (an 8-bin -/// STFT metric) had been suspected untrustworthy at this precision, and a -/// QMF-exact fit of the reference decoder's own HF content on patch -/// `(3,28,11)` (gap 25, `mod 4 == 1`) found a transfer with this function's -/// quarter-turn phase signature. Round-47 re-applied the rotation at -/// `generate`'s patch-copy site (env-gated, both signs) and re-judged with -/// `EC_AAC_SBR_QMF_WITNESS`'s own QMF-domain coherence table instead of the -/// STFT one -- the trustworthy instrument the re-trial was chartered to use. -/// Both signs made sim-vs-ours coherence on patch `(3,28,11)` WORSE, not -/// better (band28 0.2628 unrotated -> 0.0161 rotated -> 0.0282 flipped; -/// band38 0.0669 -> 0.0694 -> 0.1068), moving further from the reference's -/// 0.42-0.79 rather than toward it, refuting round-46's fit-based -/// prediction under the very instrument that was supposed to vindicate it. -/// (One sign did nudge whole-file full-band correlation up rather than -/// round-36's dip, but that's moot once the witness itself disagrees.) The -/// verdict stands as round-37 left it: `generate`'s patch-copy site keeps -/// the plain, unrotated copy. -/// Returns the signed number of `pi/2` quarter-turns per QMF slot that -/// *would* need to be applied to restore frequency-pure placement. -#[allow(dead_code)] // documentation of the characterized convention; tests call it -pub(crate) fn band_shift_correction(_target_band: usize, _source_band: usize) -> i64 { - // The fitted Kaiser bank's odd-stacked convention needed a per-slot - // quarter-turn correction here (see the doc above); the normative - // ISO/IEC 14496-3 4.6.18.4/4.6.18.8.2 QMF pair in `sbr_qmf` carries its - // own correct phase from the spec equations directly, so patching needs - // no extra rotation at this site. - 0 -} - /// One copy-up patch: `width` consecutive QMF bands starting at /// `source_start` are written to `target_start..target_start+width`. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -283,40 +225,18 @@ impl ChirpState { /// Advances the state one frame given this frame's `bs_invf_mode` per /// noise band, returning the smoothed bw to use for HF generation. - /// - /// (Round-22 sweep instrumentation, zero cost unset) `EC_AAC_SBR_BW_SCALE` - /// scales the chirp target globally (clamped below 1.0) before - /// smoothing, to probe whether the chirp/inverse-filter strength is - /// miscalibrated on real content. `EC_AAC_SBR_CHIRP_SMOOTH=none` skips - /// the attack/release smoothing entirely (target used immediately); - /// `=swap` swaps which curve (0.75/0.25 vs 0.90625/0.09375) applies to - /// rising vs falling bw, to probe whether the asymmetry is backwards. pub fn update(&mut self, invf_mode: &[u8]) -> &[f64] { - let scale = bw_scale_override(); - let smooth = chirp_smooth_mode(); for ((slot, prev), &mode) in self.bw.iter_mut().zip(&mut self.prev_mode).zip(invf_mode) { - let target = (bw_target(mode, *prev) * scale).min(0.999_999); + let target = bw_target(mode, *prev).min(0.999_999); *prev = mode; - *slot = match smooth { - ChirpSmooth::None => target, - ChirpSmooth::Current => { - // 4.6.18.6.3 bwArray smoothing: both branches weight the - // NEW value (0.75 falling, 0.90625 rising). The rising - // branch previously had the operands swapped (0.09375 - // of the new value), making every rise ~10x too slow. - if target < *slot { - 0.75 * target + 0.25 * *slot - } else { - 0.90625 * target + 0.09375 * *slot - } - } - ChirpSmooth::Swap => { - if target < *slot { - 0.90625 * target + 0.09375 * *slot - } else { - 0.75 * *slot + 0.25 * target - } - } + // 4.6.18.6.3 bwArray smoothing: both branches weight the + // NEW value (0.75 falling, 0.90625 rising). The rising + // branch previously had the operands swapped (0.09375 + // of the new value), making every rise ~10x too slow. + *slot = if target < *slot { + 0.75 * target + 0.25 * *slot + } else { + 0.90625 * target + 0.09375 * *slot }; // 4.6.18.6.3: a smoothed bw under 0.015 is forced to zero. if *slot < 0.015 { @@ -327,32 +247,6 @@ impl ChirpState { } } -/// `EC_AAC_SBR_BW_SCALE` global multiplier on the chirp target, clamped below 1.0 -/// (a chirp target at or past 1.0 is an undamped all-pole resonance, not a -/// meaningful "stronger" whitening). `1.0` (no-op) when unset or unparsable. -fn bw_scale_override() -> f64 { - std::env::var("EC_AAC_SBR_BW_SCALE") - .ok() - .and_then(|s| s.parse::().ok()) - .map(|s| s.max(0.0)) - .unwrap_or(1.0) -} - -#[derive(Clone, Copy, PartialEq, Eq)] -enum ChirpSmooth { - None, - Current, - Swap, -} - -fn chirp_smooth_mode() -> ChirpSmooth { - match std::env::var("EC_AAC_SBR_CHIRP_SMOOTH").as_deref() { - Ok("none") => ChirpSmooth::None, - Ok("swap") => ChirpSmooth::Swap, - _ => ChirpSmooth::Current, - } -} - /// Order-2 linear-prediction coefficients per ISO 14496-3 4.6.18.6.2 /// (covariance method, `phi(i,j) = sum_n x[n-i]*conj(x[n-j])` over /// `n = 2..x.len()`, index 0 of `x` is the oldest slot). Returns the spec's @@ -464,13 +358,6 @@ pub fn generate( lpc2(&ext) }; let g = bw[noise_band_of(target_band)]; - // (Round-46, Task 2) `EC_AAC_SBR_BW_DUMP` -- one line per patched - // band per frame with the smoothed chirp bw actually applied, - // to compare against the QMF-domain fitted reference transfer - // (`hf_patch_transfer_fit` in the real-file test). - if std::env::var("EC_AAC_SBR_BW_DUMP").is_ok() { - eprintln!("BW_DUMP target_band={target_band} source_band={source_band} bw={g:.6}"); - } // 4.6.18.6.3 feed-forward: X_high = X_low + bw*alpha0*X_low[l-1] // + bw^2*alpha1*X_low[l-2]. bw=0 is a plain copy, bw->1 is the // full inverse (whitening) filter. The all-pole recursion this @@ -493,10 +380,6 @@ pub fn generate( }; let dst = &mut out[target_band - kx]; for (n, &x) in series.iter().enumerate() { - // Round-37 (Task 2): `band_shift_correction` is proven exact - // in isolation but a real-file A/B (`EC_AAC_SBR_PARITY_SPLIT`) - // shows applying it REGRESSES odd-gap coherence against the - // reference; `dst` stays the uncorrected band placement. dst[n] = x + c1 * x_prev1 + c2 * x_prev2; x_prev2 = x_prev1; x_prev1 = x; diff --git a/crates/ec-aac/src/sbr_payload.rs b/crates/ec-aac/src/sbr_payload.rs index fa58f27f..f94180e5 100644 --- a/crates/ec-aac/src/sbr_payload.rs +++ b/crates/ec-aac/src/sbr_payload.rs @@ -443,11 +443,6 @@ impl SbrParser { row[b] = (row[b - 1] + delta).clamp(0, cap); } } - 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) }