Merge branch 'sbr-limiter'
Browse files at this commit2 changed files +73 -26
- crates/ec-aac/src/sbr_env.rs +31 -0
- crates/ec-aac/src/sbr_hf.rs +42 -26
crates/ec-aac/src/sbr_env.rsmodified+31 -0
| @@ -476,8 +476,14 @@ pub fn adjust( | |||
| 476 | .map(|j| limiter_max * (lim_e_orig[j] / lim_e_curr[j].max(1e-12)).sqrt()) | 476 | .map(|j| limiter_max * (lim_e_orig[j] / lim_e_curr[j].max(1e-12)).sqrt()) |
| 477 | .collect(); | 477 | .collect(); |
| 478 | let mut lim_e_actual = vec![0.0f64; n_lim]; | 478 | 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]; | ||
| 479 | for b in 0..gains.len() { | 481 | for b in 0..gains.len() { |
| 480 | let j = cell_lim[b]; | 482 | 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 | + } | ||
| 481 | gains[b] = gains[b].min(lim_cap[j]); | 487 | gains[b] = gains[b].min(lim_cap[j]); |
| 482 | let lo = table[b] as usize; | 488 | let lo = table[b] as usize; |
| 483 | let hi = table[b + 1] as usize; | 489 | let hi = table[b + 1] as usize; |
| @@ -501,6 +507,31 @@ pub fn adjust( | |||
| 501 | } | 507 | } |
| 502 | }) | 508 | }) |
| 503 | .collect(); | 509 | .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 | + } | ||
| 504 | for b in 0..gains.len() { | 535 | for b in 0..gains.len() { |
| 505 | let j = cell_lim[b]; | 536 | let j = cell_lim[b]; |
| 506 | gains[b] *= lim_boost[j]; | 537 | gains[b] *= lim_boost[j]; |
crates/ec-aac/src/sbr_hf.rsmodified+42 -26
| @@ -306,36 +306,45 @@ fn chirp_smooth_mode() -> ChirpSmooth { | |||
| 306 | /// a2*x[n-2]|^2` over `n = 2..x.len()`. Returns `(a1, a2)`; `(0, 0)` if `x` | 306 | /// a2*x[n-2]|^2` over `n = 2..x.len()`. Returns `(a1, a2)`; `(0, 0)` if `x` |
| 307 | /// is too short or the system is singular (silence or a near-constant | 307 | /// is too short or the system is singular (silence or a near-constant |
| 308 | /// subband, both of which need no prediction). | 308 | /// 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). | ||
| 309 | pub fn lpc2(x: &[Complex<f64>]) -> (Complex<f64>, Complex<f64>) { | 325 | pub fn lpc2(x: &[Complex<f64>]) -> (Complex<f64>, Complex<f64>) { |
| 310 | if x.len() < 3 { | 326 | if x.len() < 3 { |
| 311 | return (Complex::ZERO, Complex::ZERO); | 327 | return (Complex::ZERO, Complex::ZERO); |
| 312 | } | 328 | } |
| 313 | - let mut r00 = Complex::ZERO; // <x1,x1> | 329 | + let mut r0 = 0.0f64; |
| 314 | - let mut r01 = Complex::ZERO; // <x1,x2> | 330 | + let mut r1 = Complex::ZERO; |
| 315 | - let mut r11 = Complex::ZERO; // <x2,x2> | 331 | + let mut r2 = Complex::ZERO; |
| 316 | - let mut r0y = Complex::ZERO; // <x1,y> | 332 | + for n in 0..x.len() { |
| 317 | - let mut r1y = Complex::ZERO; // <x2,y> | 333 | + r0 += x[n].norm_sqr(); |
| 318 | - for n in 2..x.len() { | 334 | + if n >= 1 { |
| 319 | - let y = x[n]; | 335 | + r1 = r1 + x[n] * x[n - 1].conj(); |
| 320 | - let x1 = x[n - 1]; | 336 | + } |
| 321 | - let x2 = x[n - 2]; | 337 | + if n >= 2 { |
| 322 | - r00 = r00 + x1.conj() * x1; | 338 | + r2 = r2 + x[n] * x[n - 2].conj(); |
| 323 | - r01 = r01 + x1.conj() * x2; | 339 | + } |
| 324 | - r11 = r11 + x2.conj() * x2; | 340 | + } |
| 325 | - r0y = r0y + x1.conj() * y; | 341 | + // Yule-Walker: [[r0, conj(r1)], [r1, r0]] * [a1, a2]^T = [r1, r2]^T. |
| 326 | - r1y = r1y + x2.conj() * y; | 342 | + let det = r0 * r0 - r1.norm_sqr(); |
| 327 | - } | 343 | + if det.abs() < 1e-20 { |
| 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 { | ||
| 334 | return (Complex::ZERO, Complex::ZERO); | 344 | return (Complex::ZERO, Complex::ZERO); |
| 335 | } | 345 | } |
| 336 | - let inv_det = det.conj().scale(1.0 / norm); | 346 | + let a1 = (r1.scale(r0) - r1.conj() * r2).scale(1.0 / det); |
| 337 | - let a1 = (r0y * r11 - r01 * r1y) * inv_det; | 347 | + let a2 = (r2.scale(r0) - r1 * r1).scale(1.0 / det); |
| 338 | - let a2 = (r00 * r1y - r10 * r0y) * inv_det; | ||
| 339 | (a1, a2) | 348 | (a1, a2) |
| 340 | } | 349 | } |
| 341 | 350 | ||
| @@ -563,8 +572,15 @@ mod tests { | |||
| 563 | x.push(v); | 572 | x.push(v); |
| 564 | } | 573 | } |
| 565 | let (fa1, fa2) = lpc2(&x); | 574 | let (fa1, fa2) = lpc2(&x); |
| 566 | - assert!((fa1 - a1).norm_sqr().sqrt() < 0.05, "a1 {fa1:?} vs {a1:?}"); | 575 | + // (Round-57) Tolerance widened 0.05->0.2 for the Yule-Walker/ |
| 567 | - assert!((fa2 - a2).norm_sqr().sqrt() < 0.05, "a2 {fa2:?} vs {a2:?}"); | 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:?}"); | ||
| 568 | } | 584 | } |
| 569 | 585 | ||
| 570 | #[test] | 586 | #[test] |