diag(aac-sbr): quantify heaac_48000_64k HF undershoot at the sweep-crossover cliff
Browse files at this commit217ee4420604ecbd0de1964b5e02da8412a46146Parent: e5e62472 changed files +124 -0
crates/ec-aac/src/sbr_hf.rsmodified+12 -0
| @@ -361,6 +361,18 @@ pub fn lpc2(x: &[Complex<f64>]) -> (Complex<f64>, Complex<f64>) { | ||
| 361 | 361 | fn stabilize(coeffs: (Complex<f64>, Complex<f64>)) -> (Complex<f64>, Complex<f64>) { |
| 362 | 362 | let (a1, a2) = coeffs; |
| 363 | 363 | let mag = a1.norm_sqr().sqrt() + a2.norm_sqr().sqrt(); |
| 364 | + // (Round-56) Tried tightening this bound to 0.9 on the theory that a | |
| 365 | + // pole at 0.999 gives ~1e6x power resonant gain (`~1/(1-r)^2`), which | |
| 366 | + // `EC_AAC_SBR_CELLDUMP` traces do show happening (`cur`, the raw | |
| 367 | + // pre-gain HF estimate, reaching 1e7-1e11 against a ~1e4 transmitted | |
| 368 | + // target on heaac_48000_64k.m4a's crossover-straddling sweep window, | |
| 369 | + // in the SAME limiter band as starved neighbor cells) -- measured | |
| 370 | + // ZERO effect on the t=18s/24s corr cliff (0.054601->0.054536, | |
| 371 | + // 0.058942->0.058797) or the `EC_AAC_SBR_HF_BYPASS` A/B, because | |
| 372 | + // `raw_gain = sqrt(target/cur)` cancels `cur` exactly for any | |
| 373 | + // non-capped cell regardless of this bound's value -- reverted, no | |
| 374 | + // measured benefit to justify the regression risk on frames that | |
| 375 | + // legitimately need resonance near this bound. Left at `0.999`. | |
| 364 | 376 | if mag > 0.999 && mag.is_finite() { |
| 365 | 377 | let s = 0.999 / mag; |
| 366 | 378 | (a1.scale(s), a2.scale(s)) |
crates/ec-aac/tests/sbr_real_library.rsmodified+112 -0
| @@ -4486,3 +4486,115 @@ fn sbr441_family_sample_drift_probe() { | ||
| 4486 | 4486 | println!("-- apply_last (hold) calls during this file's decode: {holds} of {au_count} AUs"); |
| 4487 | 4487 | } |
| 4488 | 4488 | } |
| 4489 | + | |
| 4490 | +/// Round-56 charter probe: at the corr cliff `heaac_48000_64k.m4a` shows | |
| 4491 | +/// once the synthetic sweep content reaches the SBR HF register (good | |
| 4492 | +/// t=14s, bad t=18s/24s per the ledger lever), print per-band |X| (dB) and | |
| 4493 | +/// ours-minus-reference phase for 8 consecutive QMF slots at each of the | |
| 4494 | +/// three timestamps, reading both sides through | |
| 4495 | +/// [`ec_aac::sbr_qmf::HfAnalysis`] via the existing `qmf_domain_spec` third | |
| 4496 | +/// witness (ISO/IEC 14496-3 4.6.18.4 64-band analysis of each side's own | |
| 4497 | +/// final PCM, band unit = `rate/128` Hz -- exactly the patch table's own | |
| 4498 | +/// unit). Also prints the file's own patch table (ISO/IEC 14496-3 | |
| 4499 | +/// 4.6.18.6.3 `build_patches`) and an `EC_AAC_SBR_HF_BYPASS` A/B so a | |
| 4500 | +/// "our HF actively hurts" vs "our HF is merely incomplete" verdict is | |
| 4501 | +/// readable straight from the printed corr numbers. | |
| 4502 | +#[test] | |
| 4503 | +fn sbr_hf_window_band_probe() { | |
| 4504 | + if !have_ffmpeg() { | |
| 4505 | + eprintln!("SKIP: ffmpeg not on PATH"); | |
| 4506 | + return; | |
| 4507 | + } | |
| 4508 | + let dir = std::env::var("EC_AAC_HEAAC_FIXTURES").unwrap_or_else(|_| { | |
| 4509 | + format!("{}/../../.cache/heaac-fixtures", env!("CARGO_MANIFEST_DIR")) | |
| 4510 | + }); | |
| 4511 | + let path = PathBuf::from(&dir).join("heaac_48000_64k.m4a"); | |
| 4512 | + if !path.exists() { | |
| 4513 | + eprintln!("SKIP: {} absent", path.display()); | |
| 4514 | + return; | |
| 4515 | + } | |
| 4516 | + const LAG: i64 = -4673; | |
| 4517 | + | |
| 4518 | + let Some((ours, out_rate, _au_count)) = our_decode_uncapped(&path, 0) else { | |
| 4519 | + eprintln!("SKIP: our_decode_uncapped failed"); | |
| 4520 | + return; | |
| 4521 | + }; | |
| 4522 | + let ref_l = ffmpeg_decode(&path, 0, 2).swap_remove(0); | |
| 4523 | + let ch0 = &ours[0]; | |
| 4524 | + | |
| 4525 | + for t in [14usize, 18, 24] { | |
| 4526 | + let oa0 = t * out_rate as usize; | |
| 4527 | + if oa0 + out_rate as usize > ch0.len() { | |
| 4528 | + continue; | |
| 4529 | + } | |
| 4530 | + let (lag, corr) = narrow_lag_at(ch0, &ref_l, oa0, out_rate as usize, LAG, 32); | |
| 4531 | + let (wlag, wcorr) = narrow_lag_at(ch0, &ref_l, oa0, out_rate as usize, LAG, 8192); | |
| 4532 | + println!( | |
| 4533 | + "\n=== t={t}s narrow-lag corr={corr:.6} lag={lag} (wide +/-8192 search: lag={wlag} corr={wcorr:.6}) ===" | |
| 4534 | + ); | |
| 4535 | + let ob0 = (oa0 as i64 + lag) as usize; | |
| 4536 | + // 8 QMF slots (512 samples) starting at this second's aligned position. | |
| 4537 | + const WIN: usize = 8 * 64; | |
| 4538 | + if oa0 + WIN > ch0.len() || ob0 + WIN > ref_l.len() { | |
| 4539 | + println!(" SKIP: window out of range"); | |
| 4540 | + continue; | |
| 4541 | + } | |
| 4542 | + let (o_spec, t_spec, _sim) = qmf_domain_spec(&ch0[oa0..oa0 + WIN], &ref_l[ob0..ob0 + WIN]); | |
| 4543 | + for band in 14..48usize { | |
| 4544 | + let mut mags_o = Vec::new(); | |
| 4545 | + let mut mags_t = Vec::new(); | |
| 4546 | + let mut dphase_deg = Vec::new(); | |
| 4547 | + for slot in 0..8 { | |
| 4548 | + let o = o_spec[band][slot]; | |
| 4549 | + let r = t_spec[band][slot]; | |
| 4550 | + let mo = (o.re * o.re + o.im * o.im).sqrt(); | |
| 4551 | + let mr = (r.re * r.re + r.im * r.im).sqrt(); | |
| 4552 | + mags_o.push(20.0 * (mo.max(1e-12)).log10()); | |
| 4553 | + mags_t.push(20.0 * (mr.max(1e-12)).log10()); | |
| 4554 | + if mo > 1e-6 && mr > 1e-6 { | |
| 4555 | + let d = (o.im.atan2(o.re) - r.im.atan2(r.re)).to_degrees(); | |
| 4556 | + let d = ((d + 180.0).rem_euclid(360.0)) - 180.0; | |
| 4557 | + dphase_deg.push(d); | |
| 4558 | + } else { | |
| 4559 | + dphase_deg.push(f64::NAN); | |
| 4560 | + } | |
| 4561 | + } | |
| 4562 | + let mean_o: f64 = mags_o.iter().sum::<f64>() / 8.0; | |
| 4563 | + let mean_t: f64 = mags_t.iter().sum::<f64>() / 8.0; | |
| 4564 | + println!( | |
| 4565 | + " band{band:>3}: |ours|dB mean={mean_o:>7.2} |ref|dB mean={mean_t:>7.2} dphase(deg)={:?}", | |
| 4566 | + dphase_deg.iter().map(|d| format!("{d:.0}")).collect::<Vec<_>>() | |
| 4567 | + ); | |
| 4568 | + } | |
| 4569 | + } | |
| 4570 | + | |
| 4571 | + // HF-bypass A/B on the bad windows: does zeroing our HF raise or lower | |
| 4572 | + // corr against the reference in [18s,19s) and [24s,25s)? | |
| 4573 | + unsafe { std::env::set_var("EC_AAC_SBR_HF_BYPASS", "1") }; | |
| 4574 | + let bypass = our_decode_uncapped(&path, 0); | |
| 4575 | + unsafe { std::env::remove_var("EC_AAC_SBR_HF_BYPASS") }; | |
| 4576 | + if let Some((bp, bp_rate, _)) = bypass { | |
| 4577 | + let bch0 = &bp[0]; | |
| 4578 | + for t in [14usize, 18, 24] { | |
| 4579 | + let oa0 = t * bp_rate as usize; | |
| 4580 | + if oa0 + bp_rate as usize > bch0.len() { | |
| 4581 | + continue; | |
| 4582 | + } | |
| 4583 | + let (lag, corr) = narrow_lag_at(bch0, &ref_l, oa0, bp_rate as usize, LAG, 32); | |
| 4584 | + println!(" t={t}s HF_BYPASS corr={corr:.6} lag={lag}"); | |
| 4585 | + } | |
| 4586 | + } else { | |
| 4587 | + eprintln!("HF_BYPASS decode failed"); | |
| 4588 | + } | |
| 4589 | + | |
| 4590 | + // Patch table (ISO/IEC 14496-3 4.6.18.6.3), sideinfo-derived. | |
| 4591 | + unsafe { std::env::set_var("EC_AAC_SBR_SIDEINFO_DEBUG", "1") }; | |
| 4592 | + let _ = our_decode_uncapped(&path, 0); | |
| 4593 | + unsafe { std::env::remove_var("EC_AAC_SBR_SIDEINFO_DEBUG") }; | |
| 4594 | + if let Some(row) = ec_aac::sbr_sideinfo_log().first() { | |
| 4595 | + println!( | |
| 4596 | + "\n-- patch table (first frame): kx={} k2={} patch widths={:?}", | |
| 4597 | + row.kx, row.k2, row.patch_lengths | |
| 4598 | + ); | |
| 4599 | + } | |
| 4600 | +} |