diag(aac-sbr): per-AU divergence starts at AU 0, not a per-frame trigger
Browse files at this commit4f80aee12c8eeb302771c90fb4b580cb91fdb8eaParent: fb8eddb1 changed file +152 -0
crates/ec-aac/tests/sbr_real_library.rsmodified+152 -0
| @@ -3431,3 +3431,155 @@ fn sbr_header_feature_table() { | ||
| 3431 | 3431 | ); |
| 3432 | 3432 | } |
| 3433 | 3433 | } |
| 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 | +} |