forge
Explore
Log in

Don't you dare go Hollow

diag(aac-sbr): FMJ's real delay is ~21952 samples, past every gate test's search bound

Browse files at this commit

7dce88528e82dc47c99e38bb968a6451b2a7499dAhmet Kaan GümüşParent: 4f80aee
Coordinator follow-up probes:

Probe A (hand-built explicit AOT-5 config vs the parsed AOT-2 config):
FMJ's own parsed AudioSpecificConfig ([13 10 56 e5 98]) already reads
object_type=2, sample_rate=24000, sf_index=6, sbr_present=true,
extension_sample_rate=Some(48000) -- bit-identical to what a hand-built
explicit object_type=5 config with the same rates produces. Confirmed by
grep: `object_type` is read nowhere in decode.rs/lib.rs/sbr_chain.rs
outside config.rs itself -- it never drives runtime table selection or
control flow. So Probe A's near-1.0 correlation was NOT caused by
object_type 2-vs-5; it was caused by the widened lag search used in that
probe (see Probe B) applied identically to both configs, which produced
bit-identical numbers (lag=21952, corr=0.998326) whether the config's
object_type field said 2 or 5.

Probe B (WIDE_LAG_MAX 20_000 -> 100_000 for FMJ):
full_chain_low_band_matches_own_core's own (PARSED, object_type=2)
decode, re-measured with a 100_000-sample bound instead of 20_000:
  ch0 best lag=21952 corr=0.998326 (was lag=20005 corr=0.217393)
Full-band vs the reference decoder, same widened bound:
  ch0 lag=22913 corr=0.997880

Conclusion: FMJ's SBR decode is CORRECT. The 51+ rounds of "FMJ
decorrelation" chased a DSP defect that was actually a test-harness
measurement artifact -- WIDE_LAG_MAX=20_000 (full_chain_low_band_
matches_own_core) and LAG_MAX=4_000 (sbr_real_library_matches_reference)
are both far too small for FMJ's real full-chain delay (~22k samples at
48kHz, vs reeln's -576), so every prior FMJ correlation number in
this suite (-0.025253, 0.010226, 0.217393) was measuring a spurious
noise-driven lag near each search's own edge, not FMJ's actual alignment.

No production src/ change this round (charter was probes-only, ≤20 tool
calls); the concrete next step is widening sbr_real_library_matches_
reference's LAG_MAX (or switching it to the wide/odd-stride search
best_lag_correlation_wide already uses) so the asserted gate measures
FMJ's real ~22k-sample delay instead of clipping at 4000. Left as the
immediate next round's fix, not attempted here to avoid landing an
unverified change to the asserted gate within this budget.

New diagnostic test: probe_explicit_config_and_wide_lag in
crates/ec-aac/tests/sbr_real_library.rs.
crates/ec-aac/tests/sbr_real_library.rsmodified+166 -0
@@ -3583,3 +3583,169 @@ fn per_au_low_band_divergence() {
3583 eprintln!("SKIP synthetic 44100 48k: {} absent", failing.display());3583 eprintln!("SKIP synthetic 44100 48k: {} absent", failing.display());
3584 }3584 }
3585 }3585 }
3586+
3587+/// Coordinator follow-up: prints what `parse_audio_specific_config` actually
3588+/// returns for a candidate's real ASC bytes (object/rate/sf_index/
3589+/// sbr_present/extension_sample_rate), what `AacDecoder` reports back
3590+/// (`SbrSupport`, `output_sample_rate`) from those parsed fields, then
3591+/// re-decodes the SAME AU list through a HAND-BUILT explicit
3592+/// `AudioSpecificConfig` (object_type=5, known core/ext rates) instead of
3593+/// the parsed one -- if that jumps the correlations to ~1, the parsed
3594+/// config (or something object_type-conditioned at runtime) is the defect,
3595+/// not the SBR DSP chain itself. Also re-runs `full_chain_low_band_matches_
3596+/// own_core`'s FMJ probe with `WIDE_LAG_MAX` raised to 100_000.
3597+#[test]
3598+fn probe_explicit_config_and_wide_lag() {
3599+ if !have_ffmpeg() {
3600+ eprintln!("SKIP: ffmpeg not on PATH");
3601+ return;
3602+ }
3603+ fn decode_with(cfg: ec_aac::AudioSpecificConfig, aus: &[Vec<u8>]) -> (Vec<Vec<f32>>, u32) {
3604+ let rate = match (cfg.sbr_present, cfg.ps_present) {
3605+ (true, false) => cfg.extension_sample_rate.unwrap_or(cfg.sample_rate * 2),
3606+ _ => cfg.sample_rate,
3607+ };
3608+ let mut d = AacDecoder::with_config(cfg);
3609+ let mut planes: Vec<Vec<f32>> = Vec::new();
3610+ for au in aus {
3611+ let Ok(f) = d.decode(au, None) else { continue };
3612+ let ch = usize::from(f.channels);
3613+ if ch == 0 {
3614+ continue;
3615+ }
3616+ if planes.is_empty() {
3617+ planes = vec![Vec::new(); ch];
3618+ }
3619+ for (i, v) in f.samples.iter().enumerate() {
3620+ planes[i % ch].push(*v);
3621+ }
3622+ if planes[0].len() >= MAX_SAMPLES {
3623+ break;
3624+ }
3625+ }
3626+ (planes, rate)
3627+ }
3628+
3629+ for (label, path, aac_stream, ffmpeg_stream, core_sf_index, core_rate, ext_rate, channels) in [
3630+ (
3631+ "FMJ",
3632+ format!(
3633+ "{}/Downloads/reelp/reelp.mkv",
3634+ std::env::var("HOME").unwrap_or_default()
3635+ ),
3636+ 1usize,
3637+ 3usize,
3638+ 6u8,
3639+ 24_000u32,
3640+ 48_000u32,
3641+ 2u16,
3642+ ),
3643+ (
3644+ "heaac_44100_48k",
3645+ format!(
3646+ "{}/../../.cache/heaac-fixtures/heaac_44100_48k.m4a",
3647+ env!("CARGO_MANIFEST_DIR")
3648+ ),
3649+ 0,
3650+ 0,
3651+ 4,
3652+ 44_100,
3653+ 0, // filled below from parsed cfg's own extension_sample_rate
3654+ 2,
3655+ ),
3656+ ] {
3657+ let path = PathBuf::from(path);
3658+ if !path.exists() {
3659+ eprintln!("SKIP {label}: {} absent", path.display());
3660+ continue;
3661+ }
3662+ let Some((asc, aus)) = extract_aac_track(&path, aac_stream) else {
3663+ eprintln!("SKIP {label}: extract failed");
3664+ continue;
3665+ };
3666+ let parsed = parse_audio_specific_config(&asc);
3667+ println!("{label}: asc={asc:02x?} parsed={parsed:?}");
3668+ let Ok(parsed_cfg) = parsed else {
3669+ eprintln!("SKIP {label}: parse failed");
3670+ continue;
3671+ };
3672+ let d = AacDecoder::with_config(parsed_cfg.clone());
3673+ println!(
3674+ " runtime: sbr_support={:?} output_sample_rate={:?}",
3675+ d.sbr_support(),
3676+ d.output_sample_rate()
3677+ );
3678+
3679+ // Hand-built EXPLICIT config: object_type=5 (SBR), known core/ext
3680+ // rates, sbr_present forced true regardless of what the real ASC
3681+ // parsed to.
3682+ let ext = if ext_rate == 0 {
3683+ parsed_cfg.extension_sample_rate.unwrap_or(core_rate * 2)
3684+ } else {
3685+ ext_rate
3686+ };
3687+ let hand_cfg = ec_aac::AudioSpecificConfig {
3688+ object_type: ec_aac::AOT_SBR,
3689+ sample_rate: core_rate,
3690+ sf_index: core_sf_index,
3691+ channels,
3692+ channel_config: channels as u8,
3693+ sbr_present: true,
3694+ ps_present: false,
3695+ extension_sample_rate: Some(ext),
3696+ };
3697+ let (hand_planes, hand_rate) = decode_with(hand_cfg.clone(), &aus);
3698+ if hand_planes.is_empty() {
3699+ eprintln!(" hand-built config: no channels decoded");
3700+ continue;
3701+ }
3702+ // Own-core self-consistency (upsample+lowpass convention).
3703+ let core_cfg = ec_aac::AudioSpecificConfig {
3704+ sbr_present: false,
3705+ ps_present: false,
3706+ extension_sample_rate: None,
3707+ ..hand_cfg.clone()
3708+ };
3709+ let (core_planes, _) = decode_with(core_cfg, &aus);
3710+ if !core_planes.is_empty() {
3711+ let up: Vec<f32> = core_planes[0].iter().flat_map(|&s| [s, s]).collect();
3712+ let cutoff = f64::from(core_rate) * 0.4;
3713+ let ol = lowpass(&hand_planes[0], hand_rate, cutoff);
3714+ let ul = lowpass(&up, hand_rate, cutoff);
3715+ let (lag, corr) = best_lag_correlation_wide(&ol, &ul, 100_000);
3716+ println!(
3717+ " hand-built explicit config: low-band vs own core: lag={lag} corr={corr:.6}"
3718+ );
3719+ }
3720+ let theirs = ffmpeg_decode(&path, ffmpeg_stream, hand_planes.len());
3721+ if let (Some(o), Some(t)) = (hand_planes.first(), theirs.first()) {
3722+ let (lag, corr) = best_lag_correlation_wide(o, t, 100_000);
3723+ println!(" hand-built explicit config: full-band vs reference: lag={lag} corr={corr:.6}");
3724+ }
3725+ }
3726+
3727+ // Probe B: FMJ's own (parsed) full-chain-vs-own-core lag search, widened
3728+ // to 100_000 (5x the existing WIDE_LAG_MAX=20_000 this file's other
3729+ // tests use) -- the coarse search in `full_chain_low_band_matches_own_
3730+ // core` landed right at that bound's edge (20005), so this checks
3731+ // whether FMJ's true delay simply lies further out.
3732+ let home = std::env::var("HOME").unwrap_or_default();
3733+ let fmj = PathBuf::from(format!(
3734+ "{home}/Downloads/reelp/reelp.mkv"
3735+ ));
3736+ if fmj.exists() {
3737+ if let (Some((core, core_rate)), Some((full, sbr, full_rate))) = (
3738+ our_decode_core_only(&fmj, 1),
3739+ our_decode(&fmj, 1),
3740+ ) {
3741+ if sbr == ec_aac::SbrSupport::V1 {
3742+ let up: Vec<f32> = core[0].iter().flat_map(|&s| [s, s]).collect();
3743+ let cutoff = f64::from(core_rate) * 0.4;
3744+ let ol = lowpass(&full[0], full_rate, cutoff);
3745+ let ul = lowpass(&up, full_rate, cutoff);
3746+ let (lag, corr) = best_lag_correlation_wide(&ol, &ul, 100_000);
3747+ println!("FMJ WIDE_LAG_MAX=100_000: ch0 best lag={lag} corr={corr:.6}");
3748+ }
3749+ }
3750+ }
3751+}