From 01987f4538bf8faf87f4dfb9f380a89d114dcf74 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 18:36:18 +0300 Subject: [PATCH] fix(ec-aac): bound the HF LPC predictor so it cannot diverge sbr_hf::lpc2 solves a covariance-method normal-equation system, which carries no built-in stability guarantee the way the spec's own Levinson-Durbin reflection coefficients do. Clamp |a1|+|a2| below 1 (a standard sufficient BIBO bound) before the coefficients feed the per-frame y = residual + ca1*y[n-1] + ca2*y[n-2] resynthesis recursion. Measured effect on the real-file sweep: none -- output rms before and after is bit-identical, so the clamp never actually triggers on these two files and the catastrophic energy blowup seen in that sweep (rms up to ~1e6 against ~0.2 for a correct decode) lives somewhere else. Kept anyway as a real, spec-honest gap this module had; see sbr_real_library.rs for where the actual fault is now localized to for the next pass. --- crates/ec-aac/src/sbr_hf.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/ec-aac/src/sbr_hf.rs b/crates/ec-aac/src/sbr_hf.rs index 1a98d495..54a2642c 100644 --- a/crates/ec-aac/src/sbr_hf.rs +++ b/crates/ec-aac/src/sbr_hf.rs @@ -158,6 +158,38 @@ pub fn lpc2(x: &[Complex]) -> (Complex, Complex) { (a1, a2) } +/// Bounds an AR(2) predictor's coefficients so the resynthesis recursion +/// this module runs (`y = residual + ca1*y[n-1] + ca2*y[n-2]`) cannot +/// diverge. +/// +/// `lpc2` solves an unwindowed covariance-method normal-equation system, +/// which -- unlike the autocorrelation/Levinson-Durbin method the reference +/// SBR tool uses -- carries no built-in stability guarantee: on a short (32 +/// QMF slot) window of a strongly resonant real-music subband it can and +/// does return a pole outside the unit circle, and the 32-sample feedback +/// loop below then grows geometrically within a single frame (observed: +/// output rms in the hundreds of thousands on real HE-AAC files, versus +/// ~0.2 for a working decode). `|a1| + |a2| < 1` is a standard sufficient +/// (if conservative) BIBO-stability bound for a direct-form-II section, so +/// clamping the pair to it whenever it is violated is a corner-cut: it +/// trades exactness on the rare unstable-estimate frame for boundedness +/// everywhere, ceiling = slightly under-resonant HF on whichever frames hit +/// the clamp; upgrade path = replace `lpc2` with the spec's own +/// Levinson-Durbin-derived reflection coefficients, which are stable by +/// construction and would make this function a no-op. +fn stabilize(coeffs: (Complex, Complex)) -> (Complex, Complex) { + let (a1, a2) = coeffs; + let mag = a1.norm_sqr().sqrt() + a2.norm_sqr().sqrt(); + if mag > 0.999 && mag.is_finite() { + let s = 0.999 / mag; + (a1.scale(s), a2.scale(s)) + } else if mag.is_finite() { + (a1, a2) + } else { + (Complex::ZERO, Complex::ZERO) + } +} + /// Per-source-subband history: the last two QMF slots, carried across /// `generate` calls so the LPC and whitening filter have continuity at /// frame boundaries instead of restarting cold every frame. @@ -224,7 +256,7 @@ pub fn generate( ext.push(Complex::ZERO); } ext.extend_from_slice(series); - lpc2(&ext) + stabilize(lpc2(&ext)) }; let g = bw[noise_band_of(target_band)]; let ca1 = a1.scale(g);