forge
Explore
Log in

Don't you dare go Hollow

Bound the HF LPC predictor so it cannot diverge

Browse files at this commit

01987f4538bf8faf87f4dfb9f380a89d114dcf74Ahmet Kaan GümüşParent: 5bbfe62
  • fix
    • bound the HF LPC predictor so it cannot diverge
Show full message
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.rsmodified+33 -1
@@ -158,6 +158,38 @@ pub fn lpc2(x: &[Complex<f64>]) -> (Complex<f64>, Complex<f64>) {
158 (a1, a2)158 (a1, a2)
159 }159 }
160 160
161+/// Bounds an AR(2) predictor's coefficients so the resynthesis recursion
162+/// this module runs (`y = residual + ca1*y[n-1] + ca2*y[n-2]`) cannot
163+/// diverge.
164+///
165+/// `lpc2` solves an unwindowed covariance-method normal-equation system,
166+/// which -- unlike the autocorrelation/Levinson-Durbin method the reference
167+/// SBR tool uses -- carries no built-in stability guarantee: on a short (32
168+/// QMF slot) window of a strongly resonant real-music subband it can and
169+/// does return a pole outside the unit circle, and the 32-sample feedback
170+/// loop below then grows geometrically within a single frame (observed:
171+/// output rms in the hundreds of thousands on real HE-AAC files, versus
172+/// ~0.2 for a working decode). `|a1| + |a2| < 1` is a standard sufficient
173+/// (if conservative) BIBO-stability bound for a direct-form-II section, so
174+/// clamping the pair to it whenever it is violated is a corner-cut: it
175+/// trades exactness on the rare unstable-estimate frame for boundedness
176+/// everywhere, ceiling = slightly under-resonant HF on whichever frames hit
177+/// the clamp; upgrade path = replace `lpc2` with the spec's own
178+/// Levinson-Durbin-derived reflection coefficients, which are stable by
179+/// construction and would make this function a no-op.
180+fn stabilize(coeffs: (Complex<f64>, Complex<f64>)) -> (Complex<f64>, Complex<f64>) {
181+ let (a1, a2) = coeffs;
182+ let mag = a1.norm_sqr().sqrt() + a2.norm_sqr().sqrt();
183+ if mag > 0.999 && mag.is_finite() {
184+ let s = 0.999 / mag;
185+ (a1.scale(s), a2.scale(s))
186+ } else if mag.is_finite() {
187+ (a1, a2)
188+ } else {
189+ (Complex::ZERO, Complex::ZERO)
190+ }
191+}
192+
161 /// Per-source-subband history: the last two QMF slots, carried across193 /// Per-source-subband history: the last two QMF slots, carried across
162 /// `generate` calls so the LPC and whitening filter have continuity at194 /// `generate` calls so the LPC and whitening filter have continuity at
163 /// frame boundaries instead of restarting cold every frame.195 /// frame boundaries instead of restarting cold every frame.
@@ -224,7 +256,7 @@ pub fn generate(
224 ext.push(Complex::ZERO);256 ext.push(Complex::ZERO);
225 }257 }
226 ext.extend_from_slice(series);258 ext.extend_from_slice(series);
227- lpc2(&ext)259+ stabilize(lpc2(&ext))
228 };260 };
229 let g = bw[noise_band_of(target_band)];261 let g = bw[noise_band_of(target_band)];
230 let ca1 = a1.scale(g);262 let ca1 = a1.scale(g);