forge
Explore
Log in

Don't you dare go Hollow

The real-library correlation search was a false witness

Browse files at this commit

25a65cb176e18a2fa2424559b133a1bff08268bdAhmet Kaan GümüşParent: 81915ce
  • fix
    • the real-library correlation search was a false witness
Show full message
fix(aac-sbr-test): the real-library correlation search was a false witness

best_lag_correlation's signed-max search, combined with a `best.1 < 0.0`
fallback meant to catch "no in-bounds lag found", was silently discarding
every genuinely-negative full-WINDOW refined correlation and substituting
the COARSE (4096-sample) pass's result instead -- a window 20x shorter and
prone to spuriously reading ~0.87 on real audio by chance. That fallback is
where every "reeln ch0 ~0.87, healthy" measurement this week came from;
ch0's actual full-window correlation at its own best-aligned lag is -0.034.
Search by |correlation| and gate the fallback on "no candidate lag existed"
rather than on sign, so a real negative (inverted) result is reported as
what it is instead of getting silently replaced.

Corrected picture: reeln ch0 -0.034 / ch1 0.014, both near-zero -- this
is not a channel-1-specific defect, both channels of the SBR-armed decode
are badly decorrelated from the reference. FMJ's ch0 0.005 / ch1 0.024 are
unchanged by this fix (their true refined correlation was never negative,
so the buggy fallback never masked them). Root cause of the SBR-armed
decode's own corruption is still open -- this only fixes the instrument.
crates/ec-aac/tests/sbr_real_library.rsmodified+71 -8
@@ -219,17 +219,34 @@ fn best_lag_correlation(ours: &[f32], theirs: &[f32]) -> (i64, f64) {
219 Some((oa, ob))219 Some((oa, ob))
220 }220 }
221 };221 };
222- let mut coarse_best = (0i64, -1.0f64);222+ // The winning lag is the one that maximizes |correlation|, not signed
223+ // correlation: a channel that is a pure phase/sign inversion of the
224+ // reference reads strongly NEGATIVE at its true alignment lag, and a
225+ // signed-max search rejects that lag for whatever unrelated lag gives a
226+ // merely positive value -- reporting a real inversion as near-zero
227+ // noise. `found` (not "best abs value >= 0", which every real
228+ // correlation already satisfies) is what actually distinguishes "no
229+ // in-bounds lag existed" from "the best in-bounds lag was negative" --
230+ // an earlier version used `best.1 < 0.0` for that check, which treats
231+ // every genuinely negative (inverted) result as "not found" and falls
232+ // back to the COARSE, `COARSE`-sample-window-only candidate instead;
233+ // that coarse window is short enough to spuriously correlate at 0.87 on
234+ // real audio, so the fallback was silently replacing an honest,
235+ // correctly-refined negative full-`WINDOW` result with a noise-driven
236+ // positive one from a 20x-shorter slice. Carry the signed value through
237+ // so callers can see the sign, not just |corr|.
238+ let mut coarse_best = (0i64, -1.0f64, 0.0f64);
223 for lag in -(LAG_MAX as i64)..=(LAG_MAX as i64) {239 for lag in -(LAG_MAX as i64)..=(LAG_MAX as i64) {
224 let Some((oa, ob)) = slice_at(lag, COARSE) else {240 let Some((oa, ob)) = slice_at(lag, COARSE) else {
225 continue;241 continue;
226 };242 };
227 let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]);243 let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]);
228- if c > coarse_best.1 {244+ if c.abs() > coarse_best.1 {
229- coarse_best = (lag, c);245+ coarse_best = (lag, c.abs(), c);
230 }246 }
231 }247 }
232- let mut best = (coarse_best.0, -1.0f64);248+ let mut best = (coarse_best.0, -1.0f64, 0.0f64);
249+ let mut found = false;
233 for lag in (coarse_best.0 - 8)..=(coarse_best.0 + 8) {250 for lag in (coarse_best.0 - 8)..=(coarse_best.0 + 8) {
234 let Some((oa, ob)) = slice_at(lag, WINDOW) else {251 let Some((oa, ob)) = slice_at(lag, WINDOW) else {
235 continue;252 continue;
@@ -239,14 +256,43 @@ fn best_lag_correlation(ours: &[f32], theirs: &[f32]) -> (i64, f64) {
239 continue;256 continue;
240 }257 }
241 let c = correlation(&ours[oa..oa + n], &theirs[ob..ob + n]);258 let c = correlation(&ours[oa..oa + n], &theirs[ob..ob + n]);
242- if c > best.1 {259+ if c.abs() > best.1 {
243- best = (lag, c);260+ best = (lag, c.abs(), c);
244 }261 }
262+ found = true;
245 }263 }
246- if best.1 < 0.0 {264+ if !found {
247 best = coarse_best;265 best = coarse_best;
248 }266 }
249- best267+ (best.0, best.2)
268+}
269+
270+/// Signed correlation of `ours` against `theirs` at one fixed `lag`, full
271+/// `WINDOW`-sized, anchored at the SAME `start` offset `best_lag_correlation`
272+/// uses internally -- used to test a hypothesized sign inversion by reusing
273+/// a KNOWN-good lag (and window) from a healthy channel instead of trusting
274+/// the (signed, positive-biased) search above to relocate both for a
275+/// channel that may be exactly inverted at that same lag. Anchoring must
276+/// match `best_lag_correlation`'s `start`, not sample 0 -- the two windows
277+/// otherwise cover different seconds of audio and are not comparable.
278+fn correlation_at_lag(ours: &[f32], theirs: &[f32], lag: i64) -> f64 {
279+ let start = ours
280+ .len()
281+ .min(theirs.len())
282+ .saturating_sub(WINDOW)
283+ .min(ours.len() / 4);
284+ let (oa, ob) = if lag >= 0 {
285+ (start, start + lag as usize)
286+ } else {
287+ (start + (-lag) as usize, start)
288+ };
289+ let n = WINDOW
290+ .min(ours.len().saturating_sub(oa))
291+ .min(theirs.len().saturating_sub(ob));
292+ if n < 1024 {
293+ return 0.0;
294+ }
295+ correlation(&ours[oa..oa + n], &theirs[ob..ob + n])
250 }296 }
251 297
252 /// A first-order RC low-pass, run forward then backward to cancel its own298 /// A first-order RC low-pass, run forward then backward to cancel its own
@@ -352,6 +398,23 @@ fn sbr_real_library_matches_reference() {
352 }398 }
353 let theirs = ffmpeg_decode(&c.path, c.ffmpeg_stream, ours.len());399 let theirs = ffmpeg_decode(&c.path, c.ffmpeg_stream, ours.len());
354 println!("{} ({} ch, {} Hz):", c.path.display(), ours.len(), rate);400 println!("{} ({} ch, {} Hz):", c.path.display(), ours.len(), rate);
401+ // Full cross-channel triangle at one fixed lag/window: both channels
402+ // of one CPE are decoded frame-locked and so share the same
403+ // start-of-stream offset, so ch0's own alignment lag is a valid
404+ // anchor for every pair here, letting all five numbers be compared
405+ // directly instead of each being independently (and, before the
406+ // |corr|-max fix above, unreliably) re-searched.
407+ if ours.len() >= 2 && theirs.len() >= 2 {
408+ let (lag0, _) = best_lag_correlation(&ours[0], &theirs[0]);
409+ println!(
410+ " TRIANGLE @ ch0's lag {lag0}: ours0*ref0={:.6} ours0*ref1={:.6} ours1*ref0={:.6} ours1*ref1={:.6} ours0*ours1={:.6}",
411+ correlation_at_lag(&ours[0], &theirs[0], lag0),
412+ correlation_at_lag(&ours[0], &theirs[1], lag0),
413+ correlation_at_lag(&ours[1], &theirs[0], lag0),
414+ correlation_at_lag(&ours[1], &theirs[1], lag0),
415+ correlation_at_lag(&ours[0], &ours[1], 0),
416+ );
417+ }
355 for (ch, (o, t)) in ours.iter().zip(&theirs).enumerate() {418 for (ch, (o, t)) in ours.iter().zip(&theirs).enumerate() {
356 let rms = |s: &[f32]| {419 let rms = |s: &[f32]| {
357 (s.iter().map(|v| f64::from(*v) * f64::from(*v)).sum::<f64>()420 (s.iter().map(|v| f64::from(*v) * f64::from(*v)).sum::<f64>()