Bound the HF LPC predictor so it cannot diverge
Browse files at this commit01987f4538bf8faf87f4dfb9f380a89d114dcf74Parent: 5bbfe62- fix
- bound the HF LPC predictor so it cannot diverge
1 changed file +33 -1
- crates/ec-aac/src/sbr_hf.rs +33 -1
crates/ec-aac/src/sbr_hf.rsmodified+33 -1
| @@ -158,6 +158,38 @@ pub fn lpc2(x: &[Complex<f64>]) -> (Complex<f64>, Complex<f64>) { | ||
| 158 | 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 | 193 | /// Per-source-subband history: the last two QMF slots, carried across |
| 162 | 194 | /// `generate` calls so the LPC and whitening filter have continuity at |
| 163 | 195 | /// frame boundaries instead of restarting cold every frame. |
| @@ -224,7 +256,7 @@ pub fn generate( | ||
| 224 | 256 | ext.push(Complex::ZERO); |
| 225 | 257 | } |
| 226 | 258 | ext.extend_from_slice(series); |
| 227 | - lpc2(&ext) | |
| 259 | + stabilize(lpc2(&ext)) | |
| 228 | 260 | }; |
| 229 | 261 | let g = bw[noise_band_of(target_band)]; |
| 230 | 262 | let ca1 = a1.scale(g); |