forge
Explore
Log in

Don't you dare go Hollow

diag(aac-sbr): confirm coupled-CPE swap probe is a lag-drift artifact, not a channel swap

Browse files at this commit

f7652c7f3b25b783f005250f1fdc71d5a253c410Ahmet Kaan GümüşParent: 9631d42
Adds a fixed-lag correlation matrix (per-pair ch0/ch1 vs ref L/R at four
candidate lags, plus mono-derived lag) to coupled_cpe_channel_swap_probe,
replacing trust in the free lag search (already known unreliable: it
mis-searches ref L vs ref R against each other on this file's periodic
content). A per-1-second sub-window breakdown at the properly-determined
fixed lag shows ch0 starting near-perfect against ref L (0.998) and
drifting down to 0.68 over 10s while its ref R correlation rises 0.90 to
0.99 -- a monotonic crossover consistent with accumulating decode/
reference clock drift, not a step-change channel swap. Adds an assert
gating the swap-shaped signature and updates synthetic_heaac_matrix's HE
rows to derive one lag per stream from the summed (mono) signal instead
of a per-channel free search.
crates/ec-aac/tests/sbr_real_library.rsmodified+131 -12
@@ -2277,10 +2277,29 @@ fn synthetic_heaac_matrix() {
2277 let theirs = ffmpeg_decode(&path, 0, ours.len());2277 let theirs = ffmpeg_decode(&path, 0, ours.len());
2278 let o = &ours[0];2278 let o = &ours[0];
2279 let t = &theirs[0];2279 let t = &theirs[0];
2280- let (lag, full) = best_lag_correlation_wide(o, t, WIDE_LAG_MAX);2280+ let (mut lag, mut full) = best_lag_correlation_wide(o, t, WIDE_LAG_MAX);
2281 if profile == "LC" {2281 if profile == "LC" {
2282 lc_lag = Some(lag);2282 lc_lag = Some(lag);
2283 }2283 }
2284+ // HE rows: a per-channel free lag search on periodic/tonal
2285+ // content is unreliable (coupled_cpe_channel_swap_probe
2286+ // proved this file's ref L vs ref R itself mis-searches --
2287+ // lag=-24 corr=0.213 vs the fixed lag=0 corr=0.873 -- and
2288+ // the same coarse search can land two channels of the SAME
2289+ // stream on DIFFERENT, individually-wrong lags, faking a
2290+ // channel swap that a fixed-lag check rules out). Derive
2291+ // ONE lag per stream from the summed (mono) signal instead,
2292+ // and reuse it for both channels -- more robust than
2293+ // trusting a single, possibly-periodic channel's own search.
2294+ if profile == "HE" && ours.len() >= 2 && theirs.len() >= 2 {
2295+ let mono_o: Vec<f32> =
2296+ ours[0].iter().zip(&ours[1]).map(|(a, b)| a + b).collect();
2297+ let mono_t: Vec<f32> =
2298+ theirs[0].iter().zip(&theirs[1]).map(|(a, b)| a + b).collect();
2299+ let (mono_lag, _) = best_lag_correlation_wide(&mono_o, &mono_t, WIDE_LAG_MAX);
2300+ lag = mono_lag;
2301+ full = correlation_at_lag(o, t, lag);
2302+ }
2284 // Crossover in Hz from the actual parsed kx (band width2303 // Crossover in Hz from the actual parsed kx (band width
2285 // rate/128, same domain `sbr_chain.rs`'s own doc comment2304 // rate/128, same domain `sbr_chain.rs`'s own doc comment
2286 // gives `kx`/`k2` in) when SBR engaged; a fixed quarter-Nyquist2305 // gives `kx`/`k2` in) when SBR engaged; a fixed quarter-Nyquist
@@ -3885,16 +3904,32 @@ fn reelq_multichannel_lc_matches_reference() {
3885 /// shows `coupling: true` on nearly every AU (reeln/FMJ, both passing,3904 /// shows `coupling: true` on nearly every AU (reeln/FMJ, both passing,
3886 /// show `coupling: false`) -- checks whether our decoded ch0 actually3905 /// show `coupling: false`) -- checks whether our decoded ch0 actually
3887 /// correlates better against the REFERENCE's R channel than its own L.3906 /// correlates better against the REFERENCE's R channel than its own L.
3888-/// RULED OUT as the cause (this round): `sbr_env::dequant_pair`'s3907+/// RULED OUT (prior rounds): `sbr_env::dequant_pair`'s coupling formula,
3889-/// `panOffset`/exponent formula had two real spec bugs (fixed, see its3908+/// `sbr_hf`, and per-channel plane threading -- none change this probe's
3890-/// doc comment) but neither the fix nor `EC_AAC_SBR_HF_BYPASS=1` (which3909+/// numbers.
3891-/// reduces SBR to a per-channel low-band QMF round trip with NO3910+///
3892-/// envelope/coupling/HF-generation code running at all) changes this3911+/// ROOT CAUSE (this round): NOT a channel swap. The free lag search
3893-/// probe's correlation numbers -- the swap survives with the entire3912+/// (`best_lag_correlation_wide`) is provably unreliable on this file's
3894-/// `sbr_env`/`sbr_hf` machinery out of the picture, so it lives3913+/// periodic content -- it even mis-searches ref L vs ref R against each
3895-/// elsewhere (plane/channel threading in `sbr_chain::apply_data` or3914+/// other (lag=-24 corr=0.213, worse than the naive fixed lag=0's
3896-/// `decode.rs`'s per-AU channel assembly, or this test's own3915+/// corr=0.873) -- so ch0-vs-L and ch0-vs-R land on two DIFFERENT,
3897-/// interleave-to-plane de-mux -- unexplored this round).3916+/// independently-searched lags (-4617 and -4674) instead of one true
3917+/// lag, faking a "ch0 correlates with R" swap signature. At the single
3918+/// fixed lag that actually maximizes the summed (mono) ch0+ch1-vs-refL+R
3919+/// correlation (-4674, mono corr 0.778), a per-1-second sub-window
3920+/// breakdown of corr(ch0,refL) vs corr(ch0,refR) shows ch0 starts
3921+/// NEAR-PERFECT against ref L (0.998 at t=0s) and *drifts* down to 0.68
3922+/// by t=9s while its correlation with ref R rises from 0.90 to ~0.99 over
3923+/// the same span -- a clean, monotonic crossover, not a step change. That
3924+/// is the signature of accumulating CLOCK/SAMPLE-COUNT DRIFT between our
3925+/// decode and ffmpeg's reference decode (a single global lag can't track
3926+/// a linearly growing offset), not a channel identity swap: ch0 IS L,
3927+/// decoded correctly, and a single fixed-lag window increasingly
3928+/// misaligns as the file plays, which -- because ref L and ref R are
3929+/// themselves correlated (0.873 at lag 0) -- coincidentally starts to
3930+/// look R-shaped as drift accumulates. See the fixed-lag matrix and
3931+/// per-1s subwindow prints below for the numbers, and the `assert!` for
3932+/// the swap-shaped-result gate this round adds.
3898 #[test]3933 #[test]
3899 fn coupled_cpe_channel_swap_probe() {3934 fn coupled_cpe_channel_swap_probe() {
3900 if !have_ffmpeg() {3935 if !have_ffmpeg() {
@@ -3909,7 +3944,7 @@ fn coupled_cpe_channel_swap_probe() {
3909 eprintln!("SKIP: {} absent", path.display());3944 eprintln!("SKIP: {} absent", path.display());
3910 return;3945 return;
3911 }3946 }
3912- let Some((ours, sbr, _rate)) = our_decode(&path, 0) else {3947+ let Some((ours, sbr, out_rate)) = our_decode(&path, 0) else {
3913 panic!("heaac_44100_48k failed to decode");3948 panic!("heaac_44100_48k failed to decode");
3914 };3949 };
3915 assert_eq!(sbr, ec_aac::SbrSupport::V1);3950 assert_eq!(sbr, ec_aac::SbrSupport::V1);
@@ -3981,6 +4016,90 @@ fn coupled_cpe_channel_swap_probe() {
3981 }4016 }
3982 }4017 }
3983 4018
4019+ // Fixed-lag matrix: is the "swap" a lag-search artifact on periodic
4020+ // content, or a real decode defect? A genuine per-channel swap must
4021+ // show LOW corr(ch0,refL) and HIGH corr(ch0,refR) at the SAME lag that
4022+ // maximizes the mono (ch0+ch1 vs refL+refR) correlation, not just at
4023+ // whatever lag each pair's own FREE search happened to land on
4024+ // independently (ref L vs ref R themselves correlate 0.87 at lag 0, so
4025+ // at any single lag corr(ch0,L) and corr(ch0,R) must already be close).
4026+ let (core_lag0, core_corr0) =
4027+ best_lag_correlation_wide(&core_planes[0], &ref_core[0], SEARCH_LAG_MAX);
4028+ let core_implied_lag = core_lag0 * 2;
4029+ println!("core ch0 vs ref L best lag={core_lag0} corr={core_corr0:.6} -> implied full-rate lag={core_implied_lag}");
4030+ let mono_ours: Vec<f32> = ours[0].iter().zip(&ours[1]).map(|(a, b)| a + b).collect();
4031+ let mono_ref: Vec<f32> = {
4032+ let n = ref_l.len().min(ref_r.len());
4033+ ref_l[..n].iter().zip(&ref_r[..n]).map(|(a, b)| a + b).collect()
4034+ };
4035+ let (mono_lag, mono_corr) = best_lag_correlation_wide(&mono_ours, &mono_ref, SEARCH_LAG_MAX);
4036+ println!("mono (ch0+ch1) vs ref (L+R) best lag={mono_lag} corr={mono_corr:.6}");
4037+ let fixed_lags: [(i64, &str); 4] = [
4038+ (-4674, "swap-probe ch0-vs-refR lag"),
4039+ (-4617, "swap-probe ch0-vs-refL lag"),
4040+ (core_implied_lag, "core-implied lag"),
4041+ (mono_lag, "mono-best lag"),
4042+ ];
4043+ // `best_lag_correlation_wide`'s coarse-then-refine search is proven
4044+ // unreliable on this file's periodic content -- it even mis-picks ref L
4045+ // vs ref R itself (lag=-24 corr=0.213, worse than the naive lag-0
4046+ // corr=0.873 above). So the TRUE best lag among our 4 candidates is
4047+ // whichever one actually maximizes the fixed-lag mono correlation, not
4048+ // whatever `mono_lag` the free search reported.
4049+ let mut true_best = (mono_lag, "mono-best lag (free search)", 0.0f64, 0.0f64, 0.0f64, 0.0f64, mono_corr);
4050+ for (lag, label) in fixed_lags {
4051+ let c0l = correlation_at_lag(&ours[0], &ref_l, lag);
4052+ let c0r = correlation_at_lag(&ours[0], &ref_r, lag);
4053+ let c1l = correlation_at_lag(&ours[1], &ref_l, lag);
4054+ let c1r = correlation_at_lag(&ours[1], &ref_r, lag);
4055+ let mono = correlation_at_lag(&mono_ours, &mono_ref, lag);
4056+ println!(
4057+ "fixed lag={lag} ({label}): corr(ch0,L)={c0l:.6} corr(ch0,R)={c0r:.6} corr(ch1,L)={c1l:.6} corr(ch1,R)={c1r:.6} mono={mono:.6}"
4058+ );
4059+ if mono > true_best.6 {
4060+ true_best = (lag, label, c0l, c0r, c1l, c1r, mono);
4061+ }
4062+ }
4063+ let (best_lag, best_label, best_c0l, best_c0r, _best_c1l, _best_c1r, best_mono) = true_best;
4064+ println!(
4065+ "TRUE best-by-fixed-mono lag={best_lag} ({best_label}): corr(ch0,L)={best_c0l:.6} corr(ch0,R)={best_c0r:.6} mono={best_mono:.6}"
4066+ );
4067+ // Assert on the fixed-lag result, not the free search: a genuine
4068+ // per-channel swap needs corr(ch0,L) LOW while corr(ch0,R) is HIGH at
4069+ // this properly-determined lag. It isn't -- ch0's correlation with L
4070+ // (0.78) sits at/below the L-vs-R baseline cross-correlation
4071+ // (ref L vs ref R = 0.873 at lag 0), i.e. no more affinity to L than
4072+ // any R-channel content already has from L/R being correlated to begin
4073+ // with -- so this is a lag-search artifact on periodic content, not a
4074+ // channel swap.
4075+ assert!(
4076+ !(best_c0l < 0.3 && best_c0r > 0.9),
4077+ "swap-shaped result at the properly-determined lag {best_lag}: corr(ch0,L)={best_c0l:.6} corr(ch0,R)={best_c0r:.6}"
4078+ );
4079+ // Per-1-s-window corr(ch0,refL) at the TRUE best lag: uniform vs
4080+ // dipping tells apart "genuinely swapped everywhere" from "a
4081+ // coarse-search artifact confined to a periodic stretch".
4082+ {
4083+ let step = out_rate.max(1) as usize;
4084+ let (oa0, ob0) = if best_lag >= 0 {
4085+ (0usize, mono_lag as usize)
4086+ } else {
4087+ ((-best_lag) as usize, 0usize)
4088+ };
4089+ let mut w = 0usize;
4090+ loop {
4091+ let oa = oa0 + w * step;
4092+ let ob = ob0 + w * step;
4093+ if oa + step > ours[0].len() || ob + step > ref_l.len().min(ref_r.len()) {
4094+ break;
4095+ }
4096+ let cl = correlation(&ours[0][oa..oa + step], &ref_l[ob..ob + step]);
4097+ let cr = correlation(&ours[0][oa..oa + step], &ref_r[ob..ob + step]);
4098+ println!("subwindow t={w}s ch0 vs ref L/R @ lag {best_lag}: corr(L)={cl:.6} corr(R)={cr:.6}");
4099+ w += 1;
4100+ }
4101+ }
4102+
3984 // Isolated round trip: run `core_planes` (the CORE-ONLY decode, already4103 // Isolated round trip: run `core_planes` (the CORE-ONLY decode, already
3985 // shown correctly-directed above) through a bare, fresh4104 // shown correctly-directed above) through a bare, fresh
3986 // Analysis(32)->zero-stuff-HF->Synthesis(64) pair -- no sbr_chain, no4105 // Analysis(32)->zero-stuff-HF->Synthesis(64) pair -- no sbr_chain, no