forge
Explore
Log in

Don't you dare go Hollow

diag(aac-sbr): isolate the coupled-CPE L/R swap to the bare Analysis/Synthesis QMF pair, not plane/state threading

Browse files at this commit

9631d420d976c9551bb927762a7f30d52f12a826Ahmet Kaan GümüşParent: c1d01e5
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.rsmodified+28 -0
@@ -295,6 +295,20 @@ impl SbrChain {
295 if num_slots == 0 {295 if num_slots == 0 {
296 continue;296 continue;
297 }297 }
298+ if std::env::var("EC_AAC_SBR_PREQMF_DUMP").is_ok() {
299+ static N: AtomicUsize = AtomicUsize::new(0);
300+ let n = N.fetch_add(1, Ordering::Relaxed);
301+ if n < 40 {
302+ let rms = (plane.iter().map(|v| f64::from(*v) * f64::from(*v)).sum::<f64>()
303+ / plane.len().max(1) as f64)
304+ .sqrt();
305+ eprintln!(
306+ "PREQMF n={n} tag={tag} ch={ch} len={} rms={rms:.6} first8={:?}",
307+ plane.len(),
308+ &plane[..8.min(plane.len())]
309+ );
310+ }
311+ }
298 let state = &mut elem.channels[ch];312 let state = &mut elem.channels[ch];
299 let mut low_cur = vec![vec![Complex::ZERO; num_slots]; ANALYSIS_BANDS];313 let mut low_cur = vec![vec![Complex::ZERO; num_slots]; ANALYSIS_BANDS];
300 let mut raw = Vec::with_capacity(num_slots);314 let mut raw = Vec::with_capacity(num_slots);
@@ -441,6 +455,20 @@ impl SbrChain {
441 }455 }
442 out.extend_from_slice(&pcm);456 out.extend_from_slice(&pcm);
443 }457 }
458+ if std::env::var("EC_AAC_SBR_PREQMF_DUMP").is_ok() {
459+ static N: AtomicUsize = AtomicUsize::new(0);
460+ let n = N.fetch_add(1, Ordering::Relaxed);
461+ if n < 40 {
462+ let rms = (out.iter().map(|v| f64::from(*v) * f64::from(*v)).sum::<f64>()
463+ / out.len().max(1) as f64)
464+ .sqrt();
465+ eprintln!(
466+ "POSTQMF n={n} tag={tag} ch={ch} len={} rms={rms:.6} first8={:?}",
467+ out.len(),
468+ &out[..8.min(out.len())]
469+ );
470+ }
471+ }
444 *plane = out;472 *plane = out;
445 }473 }
446 }474 }
crates/ec-aac/tests/sbr_real_library.rsmodified+81 -2
@@ -1930,7 +1930,8 @@ fn our_decode_core_only(path: &Path, stream_index: usize) -> Option<(Vec<Vec<f32
1930 let core_rate = cfg.sample_rate;1930 let core_rate = cfg.sample_rate;
1931 let mut decoder = AacDecoder::with_config(cfg);1931 let mut decoder = AacDecoder::with_config(cfg);
1932 let mut planes: Vec<Vec<f32>> = Vec::new();1932 let mut planes: Vec<Vec<f32>> = Vec::new();
1933- for au in &aus {1933+ let dump = std::env::var("EC_AAC_SBR_PREQMF_DUMP").is_ok();
1934+ for (au_idx, au) in aus.iter().enumerate() {
1934 let Ok(frame) = decoder.decode(au, None) else {1935 let Ok(frame) = decoder.decode(au, None) else {
1935 continue;1936 continue;
1936 };1937 };
@@ -1938,6 +1939,19 @@ fn our_decode_core_only(path: &Path, stream_index: usize) -> Option<(Vec<Vec<f32
1938 if ch == 0 {1939 if ch == 0 {
1939 continue;1940 continue;
1940 }1941 }
1942+ if dump && au_idx < 40 {
1943+ for c in 0..ch {
1944+ let plane: Vec<f32> = frame.samples.iter().skip(c).step_by(ch).copied().collect();
1945+ let rms = (plane.iter().map(|v| f64::from(*v) * f64::from(*v)).sum::<f64>()
1946+ / plane.len().max(1) as f64)
1947+ .sqrt();
1948+ eprintln!(
1949+ "COREAU n={au_idx} ch={c} len={} rms={rms:.6} first8={:?}",
1950+ plane.len(),
1951+ &plane[..8.min(plane.len())]
1952+ );
1953+ }
1954+ }
1941 if planes.is_empty() {1955 if planes.is_empty() {
1942 planes = vec![Vec::new(); ch];1956 planes = vec![Vec::new(); ch];
1943 }1957 }
@@ -3902,6 +3916,12 @@ fn coupled_cpe_channel_swap_probe() {
3902 assert_eq!(ours.len(), 2, "expect stereo");3916 assert_eq!(ours.len(), 2, "expect stereo");
3903 let ref_l = ffmpeg_decode_pan_channel(&path, 0, 0);3917 let ref_l = ffmpeg_decode_pan_channel(&path, 0, 0);
3904 let ref_r = ffmpeg_decode_pan_channel(&path, 0, 1);3918 let ref_r = ffmpeg_decode_pan_channel(&path, 0, 1);
3919+ {
3920+ let n = ref_l.len().min(ref_r.len()).min(WINDOW);
3921+ println!("ref L vs ref R (no lag): corr={:.6}", correlation(&ref_l[..n], &ref_r[..n]));
3922+ let (lag, corr) = best_lag_correlation_wide(&ref_l, &ref_r, SEARCH_LAG_MAX);
3923+ println!("ref L vs ref R (best lag search): lag={lag} corr={corr:.6}");
3924+ }
3905 for (och, ochan) in ours.iter().enumerate() {3925 for (och, ochan) in ours.iter().enumerate() {
3906 let (lag_l, corr_l) = best_lag_correlation_wide(ochan, &ref_l, SEARCH_LAG_MAX);3926 let (lag_l, corr_l) = best_lag_correlation_wide(ochan, &ref_l, SEARCH_LAG_MAX);
3907 let (lag_r, corr_r) = best_lag_correlation_wide(ochan, &ref_r, SEARCH_LAG_MAX);3927 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() {
3920 core_cfg.extension_sample_rate = None;3940 core_cfg.extension_sample_rate = None;
3921 let mut core_dec = AacDecoder::with_config(core_cfg);3941 let mut core_dec = AacDecoder::with_config(core_cfg);
3922 let mut core_planes: Vec<Vec<f32>> = vec![Vec::new(), Vec::new()];3942 let mut core_planes: Vec<Vec<f32>> = vec![Vec::new(), Vec::new()];
3923- for au in &aus {3943+ let dump = std::env::var("EC_AAC_SBR_PREQMF_DUMP").is_ok();
3944+ for (au_idx, au) in aus.iter().enumerate() {
3924 if let Ok(f) = core_dec.decode(au, None) {3945 if let Ok(f) = core_dec.decode(au, None) {
3925 let ch = usize::from(f.channels);3946 let ch = usize::from(f.channels);
3926 if ch == 2 {3947 if ch == 2 {
3948+ if dump && au_idx < 20 {
3949+ for c in 0..2 {
3950+ let plane: Vec<f32> =
3951+ f.samples.iter().skip(c).step_by(2).copied().collect();
3952+ let rms = (plane
3953+ .iter()
3954+ .map(|v| f64::from(*v) * f64::from(*v))
3955+ .sum::<f64>()
3956+ / plane.len().max(1) as f64)
3957+ .sqrt();
3958+ eprintln!(
3959+ "COREAU n={au_idx} ch={c} len={} rms={rms:.6} first8={:?}",
3960+ plane.len(),
3961+ &plane[..8.min(plane.len())]
3962+ );
3963+ }
3964+ }
3927 for (i, v) in f.samples.iter().enumerate() {3965 for (i, v) in f.samples.iter().enumerate() {
3928 core_planes[i % 2].push(*v);3966 core_planes[i % 2].push(*v);
3929 }3967 }
@@ -3942,4 +3980,45 @@ fn coupled_cpe_channel_swap_probe() {
3942 println!("core ch{och} vs ref {name}: lag={lag} corr={corr:.6}");3980 println!("core ch{och} vs ref {name}: lag={lag} corr={corr:.6}");
3943 }3981 }
3944 }3982 }
3983+
3984+ // Isolated round trip: run `core_planes` (the CORE-ONLY decode, already
3985+ // shown correctly-directed above) through a bare, fresh
3986+ // Analysis(32)->zero-stuff-HF->Synthesis(64) pair -- no sbr_chain, no
3987+ // per-AU/per-tag HashMap element lookup, no envelope/HF/noise code at
3988+ // all -- to see whether the swap reproduces on the QMF math alone in a
3989+ // single continuous stream, isolating it from any per-AU state
3990+ // threading in `sbr_chain::apply_data`.
3991+ use ec_aac::sbr_qmf::{ANALYSIS_BANDS as AB, Analysis, SYNTHESIS_BANDS as SB, Synthesis};
3992+ const OUTPUT_SCALE: f32 = 1.0 / 65536.0;
3993+ for kx in [6usize, 12, 20] {
3994+ let mut iso_out: [Vec<f32>; 2] = [Vec::new(), Vec::new()];
3995+ for (c, chan) in core_planes.iter().enumerate() {
3996+ let mut analysis = Analysis::new();
3997+ let mut synthesis = Synthesis::new();
3998+ let n_slots = chan.len() / AB;
3999+ for slot in 0..n_slots {
4000+ let mut chunk = [0f32; 32];
4001+ chunk.copy_from_slice(&chan[slot * AB..(slot + 1) * AB]);
4002+ for s in &mut chunk {
4003+ *s /= OUTPUT_SCALE;
4004+ }
4005+ let sub = analysis.process_slot(&chunk);
4006+ let mut v = [ec_dsp::Complex::new(0.0, 0.0); 64];
4007+ v[0..kx.min(AB)].copy_from_slice(&sub[0..kx.min(AB)]);
4008+ let mut pcm = synthesis.process_slot(&v);
4009+ for s in &mut pcm {
4010+ *s *= OUTPUT_SCALE;
4011+ }
4012+ iso_out[c].extend_from_slice(&pcm);
4013+ }
4014+ let _ = SB;
4015+ }
4016+ for (och, ochan) in iso_out.iter().enumerate() {
4017+ for (rch, rplane) in [&ref_l, &ref_r].iter().enumerate() {
4018+ let (lag, corr) = best_lag_correlation_wide(ochan, rplane, SEARCH_LAG_MAX);
4019+ let name = if rch == 0 { "L" } else { "R" };
4020+ println!("iso kx={kx} ch{och} vs ref {name}: lag={lag} corr={corr:.6}");
4021+ }
4022+ }
4023+ }
3945 }4024 }