forge
Explore
Log in

Don't you dare go Hollow

Stride-1 lag search

Browse files at this commit

4aa4b5f6f18c1f33048212e8b7c82d40875146fbAhmet Kaan GümüşParent: e9d01ed
  • test
    • stride-1 lag search
    • the 0.40 core-only row was the instrument
Show full message
test(aac/sbr): stride-1 lag search; the 0.40 core-only row was the instrument

`core_only_matches_reference` printed `ch0: lag 1015, corr 0.402349` for
reeln in an otherwise green suite. The core is fine: the asserted
reference gate scores that file's below-5kHz band 0.999988, and
`full_chain_low_band_matches_own_core` scores this very core-only PCM 0.998
against our own full chain. A stride-1 sweep of the lag range shows the pair
correlating 0.999845 at lag 481 -- ffmpeg's resampler delay, the same lag FMJ
reads.

The search was missing it. Both `best_lag_correlation_ex` and
`robust_lag_topk` stepped their coarse pass by `SEARCH_STRIDE` (11) and then
refined around the best (or top-K) coarse scorer. A correlation peak between
two decodes of the same audio is often one sample wide -- here 0.9998 at 481,
0.94 at 479 -- so no grid point (the grid is 4 mod 11; 481 is 8 mod 11) saw
anything but the noise floor, and the refinement was pointed by a ranking of
noise. FMJ, same instrument and same true lag, happened to land a noise-ranked
candidate inside its refine window and read correctly, so the failure was
luck-dependent rather than file-dependent.

Both coarse passes now step by 1 (~33M multiply-adds at the narrow bound,
cheaper than the top-K refinement it replaces), and the diagnostic
low-passes both sides at 0.9 * crossover first, since only the reference
side -- ffmpeg's full SBR reconstruction resampled down to the core rate --
can hold anything above the crossover at all.

Class sweep: the sibling copies in ec-aac/obs_real_content.rs,
ec-matroska/matroska.rs and ec-ac3/encode_matrix.rs were already stride-1;
sbr_real_library.rs held the only strided searches in the tree.

Suite: 16 passed, 0 failed (53.4s). Every printed row is unchanged or
better; the four core-only rows now read 0.999779-0.999999.
crates/ec-aac/tests/sbr_real_library.rsmodified+100 -8
@@ -274,16 +274,22 @@ fn best_lag_correlation_ex(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64,
274274 // correctly-refined negative full-`WINDOW` result with a noise-driven
275275 // positive one from a 20x-shorter slice. Carry the signed value through
276276 // so callers can see the sign, not just |corr|.
277+ // Stride 1, not `SEARCH_STRIDE` (round-61): a real correlation peak
278+ // between two decodes of the same audio is often ONE sample wide (on
279+ // reeln's core-only PCM: 0.9998 at lag 481, 0.94 at 479), so a
280+ // strided grid samples only the noise floor around it and the "best
281+ // coarse lag" it hands the refine pass is arbitrary. That is how a
282+ // 0.9998 match printed as corr 0.408 at lag -52. Stride 1 over the
283+ // narrow bound is ~33M multiply-adds; the wide bound is 25x that and
284+ // only runs on escalation, so this is not worth a resolution trade.
277285 let mut coarse_best = (0i64, -1.0f64, 0.0f64);
278- let mut lag = -lag_max;
279- while lag <= lag_max {
286+ for lag in -lag_max..=lag_max {
280287 if let Some((oa, ob)) = slice_at(lag, COARSE) {
281288 let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]);
282289 if c.abs() > coarse_best.1 {
283290 coarse_best = (lag, c.abs(), c);
284291 }
285292 }
286- lag += SEARCH_STRIDE;
287293 }
288294 let at_edge = coarse_best.0.abs() >= lag_max - SEARCH_STRIDE;
289295 let mut best = (coarse_best.0, -1.0f64, 0.0f64);
@@ -352,6 +358,70 @@ fn best_lag_correlation_wide(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64
352358 /// candidate that is genuinely aligned wins there even when it wasn't the
353359 /// coarse pass's top pick. Returns `(lag, corr, at_edge)` for the winning
354360 /// refined candidate, `at_edge` inherited from that candidate's own coarse
361+/// Stride-1 lag search: correlates a `COARSE`-sized window at EVERY lag in
362+/// `+/-lag_max`, then refines the winner with a full `WINDOW`.
363+///
364+/// `robust_lag_topk`'s coarse pass steps by `SEARCH_STRIDE` (11) and then
365+/// refines only its top-K coarse scorers. That ranking is meaningless when
366+/// the true peak is narrower than the stride: on reeln's core-only PCM
367+/// the correlation is 0.9998 at lag 481 and already 0.94 at 479, so no
368+/// stride-11 grid point (the grid is 4 mod 11; 481 is 8 mod 11) scores
369+/// anything but noise, the top-K is drawn from noise, and the search
370+/// returned lag -52 corr 0.408 for a pair that is actually a 0.9998 match.
371+/// FMJ, same instrument, same true lag 481, happened to land a noise-ranked
372+/// candidate within the +/-128 refine window and read correctly -- the
373+/// failure is luck-dependent, not file-dependent. Stride 1 over a 4_096
374+/// window is ~33M multiply-adds at `PLAIN_LAG_MAX`, cheaper than the top-K
375+/// refinement it replaces, so the resolution is simply not worth trading.
376+fn exhaustive_lag_correlation(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64, f64, bool) {
377+ const COARSE: usize = 4_096;
378+ let start = ours
379+ .len()
380+ .min(theirs.len())
381+ .saturating_sub(WINDOW)
382+ .min(ours.len() / 4);
383+ let slice_at = |lag: i64, len: usize| -> Option<(usize, usize)> {
384+ let (oa, ob) = if lag >= 0 {
385+ (start, start + lag as usize)
386+ } else {
387+ (start + (-lag) as usize, start)
388+ };
389+ if oa + len > ours.len() || ob + len > theirs.len() {
390+ None
391+ } else {
392+ Some((oa, ob))
393+ }
394+ };
395+ let mut best = (0i64, -1.0f64);
396+ for lag in -lag_max..=lag_max {
397+ if let Some((oa, ob)) = slice_at(lag, COARSE) {
398+ let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]).abs();
399+ if c > best.1 {
400+ best = (lag, c);
401+ }
402+ }
403+ }
404+ let at_edge = best.0.abs() >= lag_max - 1;
405+ let mut refined = (best.0, -1.0f64);
406+ for lag in (best.0 - 2)..=(best.0 + 2) {
407+ let Some((oa, ob)) = slice_at(lag, WINDOW) else {
408+ continue;
409+ };
410+ let n = WINDOW.min(ours.len() - oa).min(theirs.len() - ob);
411+ if n < 1024 {
412+ continue;
413+ }
414+ let c = correlation(&ours[oa..oa + n], &theirs[ob..ob + n]).abs();
415+ if c > refined.1 {
416+ refined = (lag, c);
417+ }
418+ }
419+ if refined.1 < 0.0 {
420+ refined = best;
421+ }
422+ (refined.0, refined.1, at_edge)
423+}
424+
355425 /// bucket (true if ITS coarse lag sat at the +/-`lag_max` search bound).
356426 fn robust_lag_topk(ours: &[f32], theirs: &[f32], lag_max: i64, rate: u32, k: usize) -> (i64, f64, bool) {
357427 const COARSE: usize = 4_096;
@@ -372,14 +442,15 @@ fn robust_lag_topk(ours: &[f32], theirs: &[f32], lag_max: i64, rate: u32, k: usi
372442 Some((oa, ob))
373443 }
374444 };
445+ // Stride 1 (round-61), for the reason spelled out on
446+ // `exhaustive_lag_correlation`: a strided grid can miss a 1-sample-wide
447+ // peak entirely, and then the top-K is a ranking of noise.
375448 let mut coarse: Vec<(i64, f64)> = Vec::new();
376- let mut lag = -lag_max;
377- while lag <= lag_max {
449+ for lag in -lag_max..=lag_max {
378450 if let Some((oa, ob)) = slice_at(lag, COARSE) {
379451 let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]);
380452 coarse.push((lag, c.abs()));
381453 }
382- lag += SEARCH_STRIDE;
383454 }
384455 coarse.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
385456 let ten_s = 10 * rate.max(1) as usize;
@@ -2103,6 +2174,19 @@ fn ffmpeg_decode_at_rate(
21032174 /// not a regression gate (the asserted gate is
21042175 /// `sbr_real_library_matches_reference`, which already covers the full-band
21052176 /// SBR-reconstructed output).
2177+///
2178+/// Round-61: this printed corr 0.402349 for reeln ch0 while that file's
2179+/// core is provably fine (the asserted reference gate scores its below-5kHz
2180+/// band 0.999988, and `full_chain_low_band_matches_own_core` scores this very
2181+/// core-only PCM 0.998 against our own full chain). The number was the
2182+/// instrument, not the codec: `robust_lag_topk`'s stride-11 coarse grid
2183+/// cannot see a 1-sample-wide correlation peak, so its top-K was ranked from
2184+/// noise. At the true lag -- 481, ffmpeg's resampler delay, the same lag FMJ
2185+/// reads -- these two signals correlate 0.999845. The search here is now
2186+/// stride-1 (`exhaustive_lag_correlation`), and both sides are low-passed at
2187+/// `0.9 * crossover` first, since only the reference side (ffmpeg's full SBR
2188+/// reconstruction, resampled down to the core rate) can hold anything above
2189+/// the crossover at all.
21062190 #[test]
21072191 fn core_only_matches_reference() {
21082192 if !have_ffmpeg() {
@@ -2121,9 +2205,17 @@ fn core_only_matches_reference() {
21212205 ours.len(),
21222206 core_rate
21232207 );
2208+ let cutoff = f64::from(c.crossover_hz) * 0.9;
21242209 for (ch, (o, t)) in ours.iter().zip(&theirs).enumerate() {
2125- let (lag, corr) = best_lag_correlation(o, t);
2126- println!(" ch{ch}: lag {lag}, corr {corr:.6}");
2210+ let ol = lowpass(o, core_rate, cutoff);
2211+ let tl = lowpass(t, core_rate, cutoff);
2212+ let (mut lag, mut corr, mut at_edge) =
2213+ exhaustive_lag_correlation(&ol, &tl, PLAIN_LAG_MAX);
2214+ if at_edge {
2215+ (lag, corr, at_edge) = exhaustive_lag_correlation(&ol, &tl, SEARCH_LAG_MAX);
2216+ }
2217+ let edge = if at_edge { " (AT SEARCH BOUND -- noise, not a measurement)" } else { "" };
2218+ println!(" ch{ch}: below {cutoff:.0}Hz lag {lag}, corr {corr:.6}{edge}");
21272219 }
21282220 }
21292221 }