forge
Explore
Log in

Don't you dare go Hollow

diag(aac-sbr): per-AU divergence starts at AU 0, not a per-frame trigger

Browse files at this commit

4f80aee12c8eeb302771c90fb4b580cb91fdb8eaAhmet Kaan GümüşParent: fb8eddb
New sbr_real_library.rs test per_au_low_band_divergence: decodes the same
AU list through the core-only decoder and the full SBR chain, tracks each
AU's own output sample range on both sides (so a per-AU decode failure
does not desync the mapping), aligns the two streams ONCE using the same
upsample+lowpass convention full_chain_low_band_matches_own_core already
trusts (validated: reproduces reeln's known -576 lag / 0.996713 corr
exactly), then reuses that single lag per AU.

Findings:
- reeln (PASSING baseline, 22050->44100 core/full): global align
  lag=-576 corr=0.996713 (matches ledger exactly -- harness calibrated).
  First AU below the per-AU 0.9 bar is AU 3, not AU 0 -- short (2048-sample)
  windows are just noisier than the whole-file 200k-sample window the
  passing gate uses, not a real per-frame break.
- FMJ (FAILING, 24000->48000 core/full, implicit AOT2+syncext ASC
  [13 10 56 e5 98]): divergence starts at the very first AU with any
  output (AU 0) -- confirms this is a per-STREAM setup mismatch between
  the two decode paths, not a per-frame trigger. Global align lag lands
  at 20005, one past WIDE_LAG_MAX=20000's own coarse-search edge -- FMJ's
  true full-chain delay is apparently larger than every existing wide
  search in this file allows, unlike reeln's -576.
- Synthetic heaac_44100_48k.m4a (FAILING, same 22050->44100 core/full
  pair as the PASSING reeln): also diverges from its first real AU.
  This refutes the "44.1k core/full rate pair itself is broken" reading
  of the synthetic matrix -- reeln uses the identical rate pair and
  passes, so the break tracks the STREAM, not the rate family.

Config-diff hand-trace (task 2): parsed cfg (object_type/sample_rate/
sf_index/sbr_present/extension_sample_rate) is bit-identical in content
between the explicit-SBR-object ASC form (AOT 5, e.g. the PASSING
48000 kbps=64k/96k synthetic rows, asc=[2b,11,88,00]) and the
backward-compatible sync-extension form (AOT 2, e.g. FMJ and the
44100 fixtures, asc=[13,10,...]/[12,10]) for matching core/ext rates --
config.rs's two branches converge on the same AudioSpecificConfig
struct. Both AacDecoder::with_config paths set sf_index from the same
field either way, so table selection is not observably different at
that layer. The earlier "explicit vs implicit signaling" and "core
table fed the wrong rate" hypotheses this round's charter proposed are
NOT supported by this trace; root cause remains open.

No production fix landed this round -- 51+ prior rounds' worth of
hypotheses (QMF convention, HF generation, zero-stuffing gain, kx
geometry) are already dead-ended in the ledger, and this round's new
evidence (divergence from AU 0, FMJ's true lag likely >20000) narrows
but does not by itself localize the remaining bug within budget.

GATE (unchanged from before this diff, confirming no regression from
adding a diagnostic-only test):
- sbr_real_library_matches_reference: FAILS as before, FMJ ch0 full
  -0.025253, ch1 full 0.010226 (baseline -0.025/0.010, unchanged)
- full_chain_low_band_matches_own_core: FAILS as before, FMJ ch0
  corr 0.217393 at lag 20005 (baseline 0.217393, unchanged)
- synthetic_heaac_matrix: passes (LC control rows >=0.999)
- ec-aac --lib: 45 passed, 0 failed
crates/ec-aac/tests/sbr_real_library.rsmodified+152 -0
@@ -3431,3 +3431,155 @@ fn sbr_header_feature_table() {
34313431 );
34323432 }
34333433 }
3434+
3435+/// Round (sbr-per-au): per-AU localization. Decodes the same AU list through
3436+/// (a) the core-only decoder and (b) the full SBR chain, tracks each AU's
3437+/// sample range in both output streams as it goes (so a per-AU decode
3438+/// failure on either side does not desync the mapping), aligns the two
3439+/// streams ONCE in the core's own native rate domain (same decimate
3440+/// convention `synthetic_heaac_matrix`'s `ours_v_core` row already trusts),
3441+/// then re-uses that single lag for every AU's own low-band correlation.
3442+/// Prints the first 5 AUs below 0.9 with that AU's SBR side-info (source/
3443+/// envelope count/freq_res/kx/k2) so a per-frame trigger (e.g. `hold`
3444+/// frames, a specific `freq_res`) shows up directly instead of needing a
3445+/// second pass. If the first divergent AU is 0, that is reported explicitly
3446+/// -- divergence from the very first AU means a per-stream setup mismatch,
3447+/// not a per-frame trigger.
3448+fn per_au_low_band_probe(label: &str, path: &Path, aac_stream: usize) {
3449+ let Some((asc, aus)) = extract_aac_track(path, aac_stream) else {
3450+ eprintln!("SKIP {label}: extract_aac_track failed");
3451+ return;
3452+ };
3453+ let Ok(full_cfg) = parse_audio_specific_config(&asc) else {
3454+ eprintln!("SKIP {label}: asc parse failed");
3455+ return;
3456+ };
3457+ if !full_cfg.sbr_present {
3458+ eprintln!("SKIP {label}: no SBR");
3459+ return;
3460+ }
3461+ let mut core_cfg = full_cfg.clone();
3462+ core_cfg.sbr_present = false;
3463+ core_cfg.ps_present = false;
3464+ core_cfg.extension_sample_rate = None;
3465+ let core_rate = core_cfg.sample_rate;
3466+ let mut core_dec = AacDecoder::with_config(core_cfg);
3467+ let mut full_dec = AacDecoder::with_config(full_cfg);
3468+ let full_rate = full_dec.output_sample_rate().unwrap_or(core_rate * 2);
3469+
3470+ struct AuInfo {
3471+ core_range: (usize, usize),
3472+ side: Option<ec_aac::SbrSideInfoRow>,
3473+ }
3474+ let mut core_ch0: Vec<f32> = Vec::new();
3475+ let mut full_ch0: Vec<f32> = Vec::new();
3476+ let mut aus_info: Vec<AuInfo> = Vec::new();
3477+ for au in aus.iter().take(1_500) {
3478+ let side_before = ec_aac::sbr_sideinfo_log().len();
3479+ let cs = core_ch0.len();
3480+ if let Ok(f) = core_dec.decode(au, None) {
3481+ let ch = usize::from(f.channels).max(1);
3482+ core_ch0.extend(f.samples.iter().step_by(ch).copied());
3483+ }
3484+ let ce = core_ch0.len();
3485+ if let Ok(f) = full_dec.decode(au, None) {
3486+ let ch = usize::from(f.channels).max(1);
3487+ full_ch0.extend(f.samples.iter().step_by(ch).copied());
3488+ }
3489+ let side = ec_aac::sbr_sideinfo_log()[side_before..]
3490+ .iter()
3491+ .find(|r| r.ch == 0)
3492+ .cloned();
3493+ aus_info.push(AuInfo {
3494+ core_range: (cs, ce),
3495+ side,
3496+ });
3497+ if full_ch0.len() > MAX_SAMPLES {
3498+ break;
3499+ }
3500+ }
3501+ if core_ch0.is_empty() || full_ch0.is_empty() {
3502+ eprintln!("SKIP {label}: empty decode (core {} full {})", core_ch0.len(), full_ch0.len());
3503+ return;
3504+ }
3505+
3506+ // Same convention `full_chain_low_band_matches_own_core` already
3507+ // validated (0.9967 on reeln): naive nearest-neighbor 2x-upsample
3508+ // the core into the full-rate domain, low-pass both well under the core
3509+ // Nyquist. The decimate-into-core-rate convention `synthetic_heaac_matrix`
3510+ // uses for its own `ours_v_core` diagnostic column was tried first here
3511+ // and reads badly (~-0.19) even on reeln, which is known-good --
3512+ // that column is evidently not a reliable self-consistency probe on its
3513+ // own, so this per-AU tool anchors on the convention independently
3514+ // proven correct instead.
3515+ let up: Vec<f32> = core_ch0.iter().flat_map(|&s| [s, s]).collect();
3516+ let cutoff = f64::from(core_rate) * 0.4;
3517+ let ol = lowpass(&full_ch0, full_rate, cutoff);
3518+ let ul = lowpass(&up, full_rate, cutoff);
3519+ const WIDE_LAG_MAX: i64 = 20_000;
3520+ let (lag, align_corr) = best_lag_correlation_wide(&ol, &ul, WIDE_LAG_MAX);
3521+ println!(
3522+ "{label}: core={core_rate}Hz full={full_rate}Hz global align (upsampled full-rate domain) lag={lag} corr={align_corr:.6}"
3523+ );
3524+
3525+ let mut shown = 0usize;
3526+ let mut first_divergent: Option<usize> = None;
3527+ for (i, info) in aus_info.iter().enumerate() {
3528+ let (cs, ce) = info.core_range;
3529+ if ce <= cs {
3530+ continue;
3531+ }
3532+ // core_range is in core-domain sample counts; the upsampled/lowpassed
3533+ // arrays are at full rate, so the AU's window there is 2x as wide.
3534+ let fs = cs * 2;
3535+ let fe = ce * 2;
3536+ let ds = match usize::try_from(fs as i64 + lag) {
3537+ Ok(v) => v,
3538+ Err(_) => continue,
3539+ };
3540+ let de = ds + (fe - fs);
3541+ if de > ul.len() || fe > ol.len() {
3542+ break;
3543+ }
3544+ let c = correlation(&ol[fs..fe], &ul[ds..de]);
3545+ if c < 0.9 {
3546+ if first_divergent.is_none() {
3547+ first_divergent = Some(i);
3548+ }
3549+ if shown < 5 {
3550+ println!(" AU {i}: low-band corr={c:.6} side={:?}", info.side);
3551+ shown += 1;
3552+ }
3553+ }
3554+ }
3555+ match first_divergent {
3556+ Some(0) => println!(
3557+ "{label}: divergence starts at AU 0 -- per-STREAM setup mismatch, not a per-frame trigger"
3558+ ),
3559+ Some(i) => println!("{label}: first divergent AU = {i}"),
3560+ None => println!("{label}: no AU below 0.9 corr in the probed window"),
3561+ }
3562+}
3563+
3564+#[test]
3565+fn per_au_low_band_divergence() {
3566+ if !have_ffmpeg() {
3567+ eprintln!("SKIP: ffmpeg not on PATH");
3568+ return;
3569+ }
3570+ unsafe {
3571+ std::env::set_var("EC_AAC_SBR_SIDEINFO_DEBUG", "1");
3572+ }
3573+ for c in &candidates() {
3574+ per_au_low_band_probe(&c.path.display().to_string(), &c.path, c.aac_stream);
3575+ }
3576+ let dir = std::env::var("EC_AAC_HEAAC_FIXTURES").unwrap_or_else(|_| {
3577+ format!("{}/../../.cache/heaac-fixtures", env!("CARGO_MANIFEST_DIR"))
3578+ });
3579+ let failing = PathBuf::from(&dir).join("heaac_44100_48k.m4a");
3580+ if failing.exists() {
3581+ per_au_low_band_probe("synthetic 44100 48k (failing)", &failing, 0);
3582+ } else {
3583+ eprintln!("SKIP synthetic 44100 48k: {} absent", failing.display());
3584+ }
3585+}