forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'sbr-limiter'

Browse files at this commit

73af0e7ef6b22fad39975a1b555ade4e86dc89c6Ahmet Kaan GümüşParent: 59ecff3190e8e3
crates/ec-aac/src/sbr_env.rsmodified+31 -0
@@ -476,8 +476,14 @@ pub fn adjust(
476476 .map(|j| limiter_max * (lim_e_orig[j] / lim_e_curr[j].max(1e-12)).sqrt())
477477 .collect();
478478 let mut lim_e_actual = vec![0.0f64; n_lim];
479+ let mut lim_capped_count = vec![0usize; n_lim];
480+ let mut lim_total_count = vec![0usize; n_lim];
479481 for b in 0..gains.len() {
480482 let j = cell_lim[b];
483+ lim_total_count[j] += 1;
484+ if gains[b] > lim_cap[j] + 1e-12 {
485+ lim_capped_count[j] += 1;
486+ }
481487 gains[b] = gains[b].min(lim_cap[j]);
482488 let lo = table[b] as usize;
483489 let hi = table[b + 1] as usize;
@@ -501,6 +507,31 @@ pub fn adjust(
501507 }
502508 })
503509 .collect();
510+ // (Round-57 audit, ISO/IEC 14496-3 4.6.18.7.5/.7.6) Per-limiter-band
511+ // aggregate dump under `EC_AAC_SBR_CELLDUMP`: the exact quantities
512+ // the spec's two equations operate on (E_orig,mapped/E_curr sums,
513+ // G_max=limGain*sqrt(ratio), capped-cell count, G_boost), one line
514+ // per limiter band per envelope, so the aggregate math can be
515+ // checked directly rather than inferred from individual cells.
516+ if !celldump.is_empty() {
517+ for j in 0..n_lim {
518+ eprintln!(
519+ "LIMDUMP ei={ei} band=[{},{}) limiter_bands={} j={j}/{n_lim} \
520+ E_orig_mapped={:.6e} E_curr={:.6e} G_max={:.6} capped_cells={}/{} \
521+ E_actual={:.6e} G_boost={:.6}",
522+ limiter_table.get(j).copied().unwrap_or(-1),
523+ limiter_table.get(j + 1).copied().unwrap_or(-1),
524+ header.limiter_bands,
525+ lim_e_orig[j],
526+ lim_e_curr[j],
527+ lim_cap[j],
528+ lim_capped_count[j],
529+ lim_total_count[j],
530+ lim_e_actual[j],
531+ lim_boost[j],
532+ );
533+ }
534+ }
504535 for b in 0..gains.len() {
505536 let j = cell_lim[b];
506537 gains[b] *= lim_boost[j];
crates/ec-aac/src/sbr_hf.rsmodified+41 -25
@@ -306,36 +306,45 @@ fn chirp_smooth_mode() -> ChirpSmooth {
306306 /// a2*x[n-2]|^2` over `n = 2..x.len()`. Returns `(a1, a2)`; `(0, 0)` if `x`
307307 /// is too short or the system is singular (silence or a near-constant
308308 /// subband, both of which need no prediction).
309+/// (Round-57) AR(2) coefficients via the Yule-Walker/autocorrelation
310+/// method (`r(k) = sum_n x[n]*conj(x[n-k])`, Hermitian-Toeplitz `R`),
311+/// replacing the previous unwindowed covariance-method normal-equation
312+/// solve. The autocorrelation matrix is always positive semi-definite,
313+/// which is what gives Levinson-Durbin-style AR fits their built-in
314+/// BIBO-stability guarantee (poles inside or on the unit circle) -- the
315+/// covariance method has no such guarantee and was measured producing
316+/// resonant poles that blow `cur` (the raw HF estimate energy) up to
317+/// 1e7-1e11 against a ~1e4 transmitted target on real content
318+/// (heaac_48000_64k.m4a's crossover-straddling sweep window), which then
319+/// poisons the WHOLE limiter band's aggregate `E_curr` (4.6.18.7.5) and
320+/// crushes every other cell sharing that band's gain cap, not just the
321+/// outlier cell itself (round-56's stabilize-bound tightening measured
322+/// zero effect because `raw_gain=sqrt(target/cur)` cancels `cur` exactly
323+/// for the outlier cell's OWN gain -- it never reaches the neighbors this
324+/// aggregate-poisoning does).
309325 pub fn lpc2(x: &[Complex<f64>]) -> (Complex<f64>, Complex<f64>) {
310326 if x.len() < 3 {
311327 return (Complex::ZERO, Complex::ZERO);
312328 }
313- let mut r00 = Complex::ZERO; // <x1,x1>
314- let mut r01 = Complex::ZERO; // <x1,x2>
315- let mut r11 = Complex::ZERO; // <x2,x2>
316- let mut r0y = Complex::ZERO; // <x1,y>
317- let mut r1y = Complex::ZERO; // <x2,y>
318- for n in 2..x.len() {
319- let y = x[n];
320- let x1 = x[n - 1];
321- let x2 = x[n - 2];
322- r00 = r00 + x1.conj() * x1;
323- r01 = r01 + x1.conj() * x2;
324- r11 = r11 + x2.conj() * x2;
325- r0y = r0y + x1.conj() * y;
326- r1y = r1y + x2.conj() * y;
329+ let mut r0 = 0.0f64;
330+ let mut r1 = Complex::ZERO;
331+ let mut r2 = Complex::ZERO;
332+ for n in 0..x.len() {
333+ r0 += x[n].norm_sqr();
334+ if n >= 1 {
335+ r1 = r1 + x[n] * x[n - 1].conj();
336+ }
337+ if n >= 2 {
338+ r2 = r2 + x[n] * x[n - 2].conj();
339+ }
327340 }
328- // R is Hermitian (r10 = conj(r01)): solve the 2x2 system directly
329- // rather than reusing r01 for both off-diagonal entries.
330- let r10 = r01.conj();
331- let det = r00 * r11 - r01 * r10;
332- let norm = det.norm_sqr();
333- if norm < 1e-20 {
341+ // Yule-Walker: [[r0, conj(r1)], [r1, r0]] * [a1, a2]^T = [r1, r2]^T.
342+ let det = r0 * r0 - r1.norm_sqr();
343+ if det.abs() < 1e-20 {
334344 return (Complex::ZERO, Complex::ZERO);
335345 }
336- let inv_det = det.conj().scale(1.0 / norm);
337- let a1 = (r0y * r11 - r01 * r1y) * inv_det;
338- let a2 = (r00 * r1y - r10 * r0y) * inv_det;
346+ let a1 = (r1.scale(r0) - r1.conj() * r2).scale(1.0 / det);
347+ let a2 = (r2.scale(r0) - r1 * r1).scale(1.0 / det);
339348 (a1, a2)
340349 }
341350
@@ -563,8 +572,15 @@ mod tests {
563572 x.push(v);
564573 }
565574 let (fa1, fa2) = lpc2(&x);
566- assert!((fa1 - a1).norm_sqr().sqrt() < 0.05, "a1 {fa1:?} vs {a1:?}");
567- assert!((fa2 - a2).norm_sqr().sqrt() < 0.05, "a2 {fa2:?} vs {a2:?}");
575+ // (Round-57) Tolerance widened 0.05->0.2 for the Yule-Walker/
576+ // autocorrelation-method swap: it trades some finite-window
577+ // estimation bias (its Toeplitz-PSD structure is what makes the
578+ // resulting AR(2) filter unconditionally stable, unlike the
579+ // previous covariance-method solve) for a stability guarantee real
580+ // content needs -- still recovers the synthetic poles to well
581+ // within this looser bound.
582+ assert!((fa1 - a1).norm_sqr().sqrt() < 0.2, "a1 {fa1:?} vs {a1:?}");
583+ assert!((fa2 - a2).norm_sqr().sqrt() < 0.2, "a2 {fa2:?} vs {a2:?}");
568584 }
569585
570586 #[test]