From 9631d420d976c9551bb927762a7f30d52f12a826 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 18:39:35 +0300 Subject: [PATCH] diag(aac-sbr): isolate the coupled-CPE L/R swap to the bare Analysis/Synthesis QMF pair, not plane/state threading Adds env-gated (EC_AAC_SBR_PREQMF_DUMP) pre-QMF/post-QMF plane dumps in sbr_chain::apply_data and matching core-only dumps in the test harness, plus an isolated round-trip probe in coupled_cpe_channel_swap_probe that runs the already-correctly-directed core-only decode through a bare, fresh Analysis(32)->zero-stuff-HF->Synthesis(64) pair per channel, with no sbr_chain, HashMap element lookup, envelope/HF/noise code, or per-AU state at all. Findings (see ledger DELTA for the full table): the plane content entering apply_data is byte-identical to the core-only decode's own channel content (no pre-QMF swap); the isolated round trip alone reproduces the swap numbers to ~5 significant figures (kx=20: ch0 vs ref L corr=-0.089, vs ref R corr=0.980) matching the full-chain and EC_AAC_SBR_HF_BYPASS=1 numbers almost exactly -- narrowing the bug to crates/ec-aac/src/sbr_qmf.rs's Analysis/Synthesis pair itself, not sbr_chain's plane/channel-index threading or sbr_env/sbr_hf. The real reference L/R content does not invert at any tested lowpass cutoff (100Hz-12kHz via ffmpeg), ruling out "the source material really does swap identity in that band". Root cause inside sbr_qmf.rs not found this round; NOT fixed, gates NOT re-run to passing. --- crates/ec-aac/src/sbr_chain.rs | 28 +++++++++ crates/ec-aac/tests/sbr_real_library.rs | 83 ++++++++++++++++++++++++- 2 files changed, 109 insertions(+), 2 deletions(-) diff --git a/crates/ec-aac/src/sbr_chain.rs b/crates/ec-aac/src/sbr_chain.rs index 1ec40ce0..5678e1da 100644 --- a/crates/ec-aac/src/sbr_chain.rs +++ b/crates/ec-aac/src/sbr_chain.rs @@ -295,6 +295,20 @@ impl SbrChain { if num_slots == 0 { continue; } + if std::env::var("EC_AAC_SBR_PREQMF_DUMP").is_ok() { + static N: AtomicUsize = AtomicUsize::new(0); + let n = N.fetch_add(1, Ordering::Relaxed); + if n < 40 { + let rms = (plane.iter().map(|v| f64::from(*v) * f64::from(*v)).sum::() + / plane.len().max(1) as f64) + .sqrt(); + eprintln!( + "PREQMF n={n} tag={tag} ch={ch} len={} rms={rms:.6} first8={:?}", + plane.len(), + &plane[..8.min(plane.len())] + ); + } + } let state = &mut elem.channels[ch]; let mut low_cur = vec![vec![Complex::ZERO; num_slots]; ANALYSIS_BANDS]; let mut raw = Vec::with_capacity(num_slots); @@ -441,6 +455,20 @@ impl SbrChain { } out.extend_from_slice(&pcm); } + if std::env::var("EC_AAC_SBR_PREQMF_DUMP").is_ok() { + static N: AtomicUsize = AtomicUsize::new(0); + let n = N.fetch_add(1, Ordering::Relaxed); + if n < 40 { + let rms = (out.iter().map(|v| f64::from(*v) * f64::from(*v)).sum::() + / out.len().max(1) as f64) + .sqrt(); + eprintln!( + "POSTQMF n={n} tag={tag} ch={ch} len={} rms={rms:.6} first8={:?}", + out.len(), + &out[..8.min(out.len())] + ); + } + } *plane = out; } } diff --git a/crates/ec-aac/tests/sbr_real_library.rs b/crates/ec-aac/tests/sbr_real_library.rs index 16ff960e..bc0d79f8 100644 --- a/crates/ec-aac/tests/sbr_real_library.rs +++ b/crates/ec-aac/tests/sbr_real_library.rs @@ -1930,7 +1930,8 @@ fn our_decode_core_only(path: &Path, stream_index: usize) -> Option<(Vec> = Vec::new(); - for au in &aus { + let dump = std::env::var("EC_AAC_SBR_PREQMF_DUMP").is_ok(); + for (au_idx, au) in aus.iter().enumerate() { let Ok(frame) = decoder.decode(au, None) else { continue; }; @@ -1938,6 +1939,19 @@ fn our_decode_core_only(path: &Path, stream_index: usize) -> Option<(Vec = frame.samples.iter().skip(c).step_by(ch).copied().collect(); + let rms = (plane.iter().map(|v| f64::from(*v) * f64::from(*v)).sum::() + / plane.len().max(1) as f64) + .sqrt(); + eprintln!( + "COREAU n={au_idx} ch={c} len={} rms={rms:.6} first8={:?}", + plane.len(), + &plane[..8.min(plane.len())] + ); + } + } if planes.is_empty() { planes = vec![Vec::new(); ch]; } @@ -3902,6 +3916,12 @@ fn coupled_cpe_channel_swap_probe() { assert_eq!(ours.len(), 2, "expect stereo"); let ref_l = ffmpeg_decode_pan_channel(&path, 0, 0); let ref_r = ffmpeg_decode_pan_channel(&path, 0, 1); + { + let n = ref_l.len().min(ref_r.len()).min(WINDOW); + println!("ref L vs ref R (no lag): corr={:.6}", correlation(&ref_l[..n], &ref_r[..n])); + let (lag, corr) = best_lag_correlation_wide(&ref_l, &ref_r, SEARCH_LAG_MAX); + println!("ref L vs ref R (best lag search): lag={lag} corr={corr:.6}"); + } for (och, ochan) in ours.iter().enumerate() { let (lag_l, corr_l) = best_lag_correlation_wide(ochan, &ref_l, SEARCH_LAG_MAX); let (lag_r, corr_r) = best_lag_correlation_wide(ochan, &ref_r, SEARCH_LAG_MAX); @@ -3920,10 +3940,28 @@ fn coupled_cpe_channel_swap_probe() { core_cfg.extension_sample_rate = None; let mut core_dec = AacDecoder::with_config(core_cfg); let mut core_planes: Vec> = vec![Vec::new(), Vec::new()]; - for au in &aus { + let dump = std::env::var("EC_AAC_SBR_PREQMF_DUMP").is_ok(); + for (au_idx, au) in aus.iter().enumerate() { if let Ok(f) = core_dec.decode(au, None) { let ch = usize::from(f.channels); if ch == 2 { + if dump && au_idx < 20 { + for c in 0..2 { + let plane: Vec = + f.samples.iter().skip(c).step_by(2).copied().collect(); + let rms = (plane + .iter() + .map(|v| f64::from(*v) * f64::from(*v)) + .sum::() + / plane.len().max(1) as f64) + .sqrt(); + eprintln!( + "COREAU n={au_idx} ch={c} len={} rms={rms:.6} first8={:?}", + plane.len(), + &plane[..8.min(plane.len())] + ); + } + } for (i, v) in f.samples.iter().enumerate() { core_planes[i % 2].push(*v); } @@ -3942,4 +3980,45 @@ fn coupled_cpe_channel_swap_probe() { println!("core ch{och} vs ref {name}: lag={lag} corr={corr:.6}"); } } + + // Isolated round trip: run `core_planes` (the CORE-ONLY decode, already + // shown correctly-directed above) through a bare, fresh + // Analysis(32)->zero-stuff-HF->Synthesis(64) pair -- no sbr_chain, no + // per-AU/per-tag HashMap element lookup, no envelope/HF/noise code at + // all -- to see whether the swap reproduces on the QMF math alone in a + // single continuous stream, isolating it from any per-AU state + // threading in `sbr_chain::apply_data`. + use ec_aac::sbr_qmf::{ANALYSIS_BANDS as AB, Analysis, SYNTHESIS_BANDS as SB, Synthesis}; + const OUTPUT_SCALE: f32 = 1.0 / 65536.0; + for kx in [6usize, 12, 20] { + let mut iso_out: [Vec; 2] = [Vec::new(), Vec::new()]; + for (c, chan) in core_planes.iter().enumerate() { + let mut analysis = Analysis::new(); + let mut synthesis = Synthesis::new(); + let n_slots = chan.len() / AB; + for slot in 0..n_slots { + let mut chunk = [0f32; 32]; + chunk.copy_from_slice(&chan[slot * AB..(slot + 1) * AB]); + for s in &mut chunk { + *s /= OUTPUT_SCALE; + } + let sub = analysis.process_slot(&chunk); + let mut v = [ec_dsp::Complex::new(0.0, 0.0); 64]; + v[0..kx.min(AB)].copy_from_slice(&sub[0..kx.min(AB)]); + let mut pcm = synthesis.process_slot(&v); + for s in &mut pcm { + *s *= OUTPUT_SCALE; + } + iso_out[c].extend_from_slice(&pcm); + } + let _ = SB; + } + for (och, ochan) in iso_out.iter().enumerate() { + for (rch, rplane) in [&ref_l, &ref_r].iter().enumerate() { + let (lag, corr) = best_lag_correlation_wide(ochan, rplane, SEARCH_LAG_MAX); + let name = if rch == 0 { "L" } else { "R" }; + println!("iso kx={kx} ch{och} vs ref {name}: lag={lag} corr={corr:.6}"); + } + } + } }