From 7dce88528e82dc47c99e38bb968a6451b2a7499d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 20 Aug 2026 17:20:50 +0300 Subject: [PATCH] diag(aac-sbr): FMJ's real delay is ~21952 samples, past every gate test's search bound MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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.rs | 166 ++++++++++++++++++++++++ 1 file changed, 166 insertions(+) diff --git a/crates/ec-aac/tests/sbr_real_library.rs b/crates/ec-aac/tests/sbr_real_library.rs index f2049cfd..49528685 100644 --- a/crates/ec-aac/tests/sbr_real_library.rs +++ b/crates/ec-aac/tests/sbr_real_library.rs @@ -3583,3 +3583,169 @@ fn per_au_low_band_divergence() { eprintln!("SKIP synthetic 44100 48k: {} absent", failing.display()); } } + +/// Coordinator follow-up: prints what `parse_audio_specific_config` actually +/// returns for a candidate's real ASC bytes (object/rate/sf_index/ +/// sbr_present/extension_sample_rate), what `AacDecoder` reports back +/// (`SbrSupport`, `output_sample_rate`) from those parsed fields, then +/// re-decodes the SAME AU list through a HAND-BUILT explicit +/// `AudioSpecificConfig` (object_type=5, known core/ext rates) instead of +/// the parsed one -- if that jumps the correlations to ~1, the parsed +/// config (or something object_type-conditioned at runtime) is the defect, +/// not the SBR DSP chain itself. Also re-runs `full_chain_low_band_matches_ +/// own_core`'s FMJ probe with `WIDE_LAG_MAX` raised to 100_000. +#[test] +fn probe_explicit_config_and_wide_lag() { + if !have_ffmpeg() { + eprintln!("SKIP: ffmpeg not on PATH"); + return; + } + fn decode_with(cfg: ec_aac::AudioSpecificConfig, aus: &[Vec]) -> (Vec>, u32) { + let rate = match (cfg.sbr_present, cfg.ps_present) { + (true, false) => cfg.extension_sample_rate.unwrap_or(cfg.sample_rate * 2), + _ => cfg.sample_rate, + }; + let mut d = AacDecoder::with_config(cfg); + let mut planes: Vec> = Vec::new(); + for au in aus { + let Ok(f) = d.decode(au, None) else { continue }; + let ch = usize::from(f.channels); + if ch == 0 { + continue; + } + if planes.is_empty() { + planes = vec![Vec::new(); ch]; + } + for (i, v) in f.samples.iter().enumerate() { + planes[i % ch].push(*v); + } + if planes[0].len() >= MAX_SAMPLES { + break; + } + } + (planes, rate) + } + + for (label, path, aac_stream, ffmpeg_stream, core_sf_index, core_rate, ext_rate, channels) in [ + ( + "FMJ", + format!( + "{}/Downloads/reelp/reelp.mkv", + std::env::var("HOME").unwrap_or_default() + ), + 1usize, + 3usize, + 6u8, + 24_000u32, + 48_000u32, + 2u16, + ), + ( + "heaac_44100_48k", + format!( + "{}/../../.cache/heaac-fixtures/heaac_44100_48k.m4a", + env!("CARGO_MANIFEST_DIR") + ), + 0, + 0, + 4, + 44_100, + 0, // filled below from parsed cfg's own extension_sample_rate + 2, + ), + ] { + let path = PathBuf::from(path); + if !path.exists() { + eprintln!("SKIP {label}: {} absent", path.display()); + continue; + } + let Some((asc, aus)) = extract_aac_track(&path, aac_stream) else { + eprintln!("SKIP {label}: extract failed"); + continue; + }; + let parsed = parse_audio_specific_config(&asc); + println!("{label}: asc={asc:02x?} parsed={parsed:?}"); + let Ok(parsed_cfg) = parsed else { + eprintln!("SKIP {label}: parse failed"); + continue; + }; + let d = AacDecoder::with_config(parsed_cfg.clone()); + println!( + " runtime: sbr_support={:?} output_sample_rate={:?}", + d.sbr_support(), + d.output_sample_rate() + ); + + // Hand-built EXPLICIT config: object_type=5 (SBR), known core/ext + // rates, sbr_present forced true regardless of what the real ASC + // parsed to. + let ext = if ext_rate == 0 { + parsed_cfg.extension_sample_rate.unwrap_or(core_rate * 2) + } else { + ext_rate + }; + let hand_cfg = ec_aac::AudioSpecificConfig { + object_type: ec_aac::AOT_SBR, + sample_rate: core_rate, + sf_index: core_sf_index, + channels, + channel_config: channels as u8, + sbr_present: true, + ps_present: false, + extension_sample_rate: Some(ext), + }; + let (hand_planes, hand_rate) = decode_with(hand_cfg.clone(), &aus); + if hand_planes.is_empty() { + eprintln!(" hand-built config: no channels decoded"); + continue; + } + // Own-core self-consistency (upsample+lowpass convention). + let core_cfg = ec_aac::AudioSpecificConfig { + sbr_present: false, + ps_present: false, + extension_sample_rate: None, + ..hand_cfg.clone() + }; + let (core_planes, _) = decode_with(core_cfg, &aus); + if !core_planes.is_empty() { + let up: Vec = core_planes[0].iter().flat_map(|&s| [s, s]).collect(); + let cutoff = f64::from(core_rate) * 0.4; + let ol = lowpass(&hand_planes[0], hand_rate, cutoff); + let ul = lowpass(&up, hand_rate, cutoff); + let (lag, corr) = best_lag_correlation_wide(&ol, &ul, 100_000); + println!( + " hand-built explicit config: low-band vs own core: lag={lag} corr={corr:.6}" + ); + } + let theirs = ffmpeg_decode(&path, ffmpeg_stream, hand_planes.len()); + if let (Some(o), Some(t)) = (hand_planes.first(), theirs.first()) { + let (lag, corr) = best_lag_correlation_wide(o, t, 100_000); + println!(" hand-built explicit config: full-band vs reference: lag={lag} corr={corr:.6}"); + } + } + + // Probe B: FMJ's own (parsed) full-chain-vs-own-core lag search, widened + // to 100_000 (5x the existing WIDE_LAG_MAX=20_000 this file's other + // tests use) -- the coarse search in `full_chain_low_band_matches_own_ + // core` landed right at that bound's edge (20005), so this checks + // whether FMJ's true delay simply lies further out. + let home = std::env::var("HOME").unwrap_or_default(); + let fmj = PathBuf::from(format!( + "{home}/Downloads/reelp/reelp.mkv" + )); + if fmj.exists() { + if let (Some((core, core_rate)), Some((full, sbr, full_rate))) = ( + our_decode_core_only(&fmj, 1), + our_decode(&fmj, 1), + ) { + if sbr == ec_aac::SbrSupport::V1 { + let up: Vec = core[0].iter().flat_map(|&s| [s, s]).collect(); + let cutoff = f64::from(core_rate) * 0.4; + let ol = lowpass(&full[0], full_rate, cutoff); + let ul = lowpass(&up, full_rate, cutoff); + let (lag, corr) = best_lag_correlation_wide(&ol, &ul, 100_000); + println!("FMJ WIDE_LAG_MAX=100_000: ch0 best lag={lag} corr={corr:.6}"); + } + } + } +}