forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'sbr-matrix-lag'

Browse files at this commit

e5e6247ec2b54cf9a40e0b86a31fea0b2b210a7fAhmet Kaan GümüşParent: f40e85cfd37ec1
crates/ec-aac/tests/sbr_real_library.rsmodified+125 -5
@@ -326,6 +326,77 @@ fn best_lag_correlation_wide(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64
326 (lag, corr)326 (lag, corr)
327 }327 }
328 328
329+/// The robust two-stage lag search `sbr441_family_sample_drift_probe` proved
330+/// out (round-55/56): a plain coarse-stride search picks the SINGLE best
331+/// `COARSE`-sample-window candidate, which on periodic/tonal real music
332+/// content routinely aliases onto a wrong lag (this file family's ref-L-vs-
333+/// ref-R self-check scored a coarse-search lag=-24 corr=0.213, worse than
334+/// the true fixed-lag comparison's 0.873). Instead of trusting the single
335+/// coarse winner, keep the top `K` coarse candidates and refine EACH with a
336+/// full-resolution, narrow (+/-128) search restricted to the file's first
337+/// 10 seconds (`sample_rate`-derived) -- periodic aliasing needs many
338+/// seconds of a repeating pattern to fool a full-resolution window, so the
339+/// candidate that is genuinely aligned wins there even when it wasn't the
340+/// coarse pass's top pick. Returns `(lag, corr, at_edge)` for the winning
341+/// refined candidate, `at_edge` inherited from that candidate's own coarse
342+/// bucket (true if ITS coarse lag sat at the +/-`lag_max` search bound).
343+fn robust_lag_topk(ours: &[f32], theirs: &[f32], lag_max: i64, rate: u32, k: usize) -> (i64, f64, bool) {
344+ const COARSE: usize = 4_096;
345+ let start = ours
346+ .len()
347+ .min(theirs.len())
348+ .saturating_sub(WINDOW)
349+ .min(ours.len() / 4);
350+ let slice_at = |lag: i64, len: usize| -> Option<(usize, usize)> {
351+ let (oa, ob) = if lag >= 0 {
352+ (start, start + lag as usize)
353+ } else {
354+ (start + (-lag) as usize, start)
355+ };
356+ if oa + len > ours.len() || ob + len > theirs.len() {
357+ None
358+ } else {
359+ Some((oa, ob))
360+ }
361+ };
362+ let mut coarse: Vec<(i64, f64)> = Vec::new();
363+ let mut lag = -lag_max;
364+ while lag <= lag_max {
365+ if let Some((oa, ob)) = slice_at(lag, COARSE) {
366+ let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]);
367+ coarse.push((lag, c.abs()));
368+ }
369+ lag += SEARCH_STRIDE;
370+ }
371+ coarse.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
372+ let ten_s = 10 * rate.max(1) as usize;
373+ let mut best: (i64, f64, bool) = (0, -2.0, false);
374+ for &(cand_lag, _) in coarse.iter().take(k) {
375+ let at_edge = cand_lag.abs() >= lag_max - SEARCH_STRIDE;
376+ // `narrow_lag_at` computes `ob_i = oa0 + lag` directly (no sign
377+ // split the way `slice_at` above does), so a fixed `oa0 = 0` goes
378+ // negative -- and every lag in the whole +/-128 window skips --
379+ // whenever `cand_lag` is negative enough (this file family's true
380+ // lag is -4674). Shift the window's start so it stays anchored to
381+ // "the first 10s of real content" while guaranteeing `ob_i >= 0`
382+ // even at the most-negative lag in the +/-128 refine range.
383+ let oa0 = (128 - cand_lag).max(0) as usize;
384+ let refine_len = ten_s.min(ours.len().saturating_sub(oa0)).min(
385+ theirs
386+ .len()
387+ .saturating_sub((oa0 as i64 + cand_lag).max(0) as usize),
388+ );
389+ if refine_len < 1024 {
390+ continue;
391+ }
392+ let (rlag, rcorr) = narrow_lag_at(ours, theirs, oa0, refine_len, cand_lag, 128);
393+ if rcorr > best.1 {
394+ best = (rlag, rcorr, at_edge);
395+ }
396+ }
397+ best
398+}
399+
329 /// Signed correlation of `ours` against `theirs` at one fixed `lag`, full400 /// Signed correlation of `ours` against `theirs` at one fixed `lag`, full
330 /// `WINDOW`-sized, anchored at the SAME `start` offset `best_lag_correlation`401 /// `WINDOW`-sized, anchored at the SAME `start` offset `best_lag_correlation`
331 /// uses internally -- used to test a hypothesized sign inversion by reusing402 /// uses internally -- used to test a hypothesized sign inversion by reusing
@@ -2277,7 +2348,15 @@ fn synthetic_heaac_matrix() {
2277 let theirs = ffmpeg_decode(&path, 0, ours.len());2348 let theirs = ffmpeg_decode(&path, 0, ours.len());
2278 let o = &ours[0];2349 let o = &ours[0];
2279 let t = &theirs[0];2350 let t = &theirs[0];
2280- let (mut lag, mut full) = best_lag_correlation_wide(o, t, WIDE_LAG_MAX);2351+ const TOPK: usize = 5;
2352+ let (mut lag, mut full, edge) =
2353+ robust_lag_topk(o, t, WIDE_LAG_MAX, our_rate, TOPK);
2354+ assert!(
2355+ !edge,
2356+ "{}: {profile} row's refined winning lag {lag} sits at the \
2357+ +/-{WIDE_LAG_MAX} search's own edge",
2358+ path.display()
2359+ );
2281 if profile == "LC" {2360 if profile == "LC" {
2282 lc_lag = Some(lag);2361 lc_lag = Some(lag);
2283 }2362 }
@@ -2290,13 +2369,21 @@ fn synthetic_heaac_matrix() {
2290 // channel swap that a fixed-lag check rules out). Derive2369 // channel swap that a fixed-lag check rules out). Derive
2291 // ONE lag per stream from the summed (mono) signal instead,2370 // ONE lag per stream from the summed (mono) signal instead,
2292 // and reuse it for both channels -- more robust than2371 // and reuse it for both channels -- more robust than
2293- // trusting a single, possibly-periodic channel's own search.2372+ // trusting a single, possibly-periodic channel's own search
2373+ // -- via the same top-K/refine-on-first-10s robust method.
2294 if profile == "HE" && ours.len() >= 2 && theirs.len() >= 2 {2374 if profile == "HE" && ours.len() >= 2 && theirs.len() >= 2 {
2295 let mono_o: Vec<f32> =2375 let mono_o: Vec<f32> =
2296 ours[0].iter().zip(&ours[1]).map(|(a, b)| a + b).collect();2376 ours[0].iter().zip(&ours[1]).map(|(a, b)| a + b).collect();
2297 let mono_t: Vec<f32> =2377 let mono_t: Vec<f32> =
2298 theirs[0].iter().zip(&theirs[1]).map(|(a, b)| a + b).collect();2378 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);2379+ let (mono_lag, _, mono_edge) =
2380+ robust_lag_topk(&mono_o, &mono_t, WIDE_LAG_MAX, our_rate, TOPK);
2381+ assert!(
2382+ !mono_edge,
2383+ "{}: HE row's mono-derived refined lag {mono_lag} sits at the \
2384+ +/-{WIDE_LAG_MAX} search's own edge",
2385+ path.display()
2386+ );
2300 lag = mono_lag;2387 lag = mono_lag;
2301 full = correlation_at_lag(o, t, lag);2388 full = correlation_at_lag(o, t, lag);
2302 }2389 }
@@ -2309,10 +2396,16 @@ fn synthetic_heaac_matrix() {
2309 } else {2396 } else {
2310 f64::from(our_rate) * 0.252397 f64::from(our_rate) * 0.25
2311 };2398 };
2312- let (_, below) = best_lag_correlation_wide(2399+ // Same single robust `lag` the full/above bands use, not an
2400+ // independent search: a narrowband low-passed signal's
2401+ // correlation surface is broad and flat across nearby lags,
2402+ // which used to let an independent search here silently
2403+ // report a different (and unvalidated) alignment than the
2404+ // one everything else in the row is anchored at.
2405+ let below = correlation_at_lag(
2313 &lowpass(o, our_rate, crossover_hz),2406 &lowpass(o, our_rate, crossover_hz),
2314 &lowpass(t, our_rate, crossover_hz),2407 &lowpass(t, our_rate, crossover_hz),
2315- WIDE_LAG_MAX,2408+ lag,
2316 );2409 );
2317 // `above` re-slices at the FULL-band winning `lag` before2410 // `above` re-slices at the FULL-band winning `lag` before
2318 // splitting, same convention `sbr_real_library_matches_reference`2411 // splitting, same convention `sbr_real_library_matches_reference`
@@ -2418,6 +2511,33 @@ fn synthetic_heaac_matrix() {
2418 core_rate_str,2511 core_rate_str,
2419 our_rate,2512 our_rate,
2420 );2513 );
2514+ // 44.1k-family HE rows: print per-second full-band corr at
2515+ // the row's own robust `lag` over 30s, so a content-
2516+ // dependent HF-reconstruction dip (real, not an alignment
2517+ // artifact -- `sbr441_family_sample_drift_probe` isolated
2518+ // one at t=13-27s on heaac_44100_48k.m4a) is visible per
2519+ // second instead of averaged away by the single `full`
2520+ // column above.
2521+ if profile == "HE" && rate == 44_100 {
2522+ println!(" per-second full-band corr @ lag {lag} (t, corr):");
2523+ let step = our_rate.max(1) as usize;
2524+ for sec in 0..30usize {
2525+ let oa0 = sec * step;
2526+ if oa0 + step > o.len() {
2527+ break;
2528+ }
2529+ let ob0 = oa0 as i64 + lag;
2530+ if ob0 < 0 {
2531+ continue;
2532+ }
2533+ let ob0 = ob0 as usize;
2534+ if ob0 + step > t.len() {
2535+ continue;
2536+ }
2537+ let c = correlation(&o[oa0..oa0 + step], &t[ob0..ob0 + step]);
2538+ println!(" t={sec:>2}s corr={c:.6}");
2539+ }
2540+ }
2421 // Coordinator follow-up (4): on the two named rows, probe2541 // Coordinator follow-up (4): on the two named rows, probe
2422 // candidate total HE-vs-reference delays built from LC's own2542 // candidate total HE-vs-reference delays built from LC's own
2423 // priming lag plus a guessed SBR-chain contribution, to see2543 // priming lag plus a guessed SBR-chain contribution, to see