Stride-1 lag search
Browse files at this commit4aa4b5f6f18c1f33048212e8b7c82d40875146fbParent: e9d01ed- test
- stride-1 lag search
- the 0.40 core-only row was the instrument
1 changed file +100 -8
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, | ||
| 274 | 274 | // correctly-refined negative full-`WINDOW` result with a noise-driven |
| 275 | 275 | // positive one from a 20x-shorter slice. Carry the signed value through |
| 276 | 276 | // 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. | |
| 277 | 285 | 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 { | |
| 280 | 287 | if let Some((oa, ob)) = slice_at(lag, COARSE) { |
| 281 | 288 | let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]); |
| 282 | 289 | if c.abs() > coarse_best.1 { |
| 283 | 290 | coarse_best = (lag, c.abs(), c); |
| 284 | 291 | } |
| 285 | 292 | } |
| 286 | - lag += SEARCH_STRIDE; | |
| 287 | 293 | } |
| 288 | 294 | let at_edge = coarse_best.0.abs() >= lag_max - SEARCH_STRIDE; |
| 289 | 295 | 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 | ||
| 352 | 358 | /// candidate that is genuinely aligned wins there even when it wasn't the |
| 353 | 359 | /// coarse pass's top pick. Returns `(lag, corr, at_edge)` for the winning |
| 354 | 360 | /// 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 | + | |
| 355 | 425 | /// bucket (true if ITS coarse lag sat at the +/-`lag_max` search bound). |
| 356 | 426 | fn robust_lag_topk(ours: &[f32], theirs: &[f32], lag_max: i64, rate: u32, k: usize) -> (i64, f64, bool) { |
| 357 | 427 | const COARSE: usize = 4_096; |
| @@ -372,14 +442,15 @@ fn robust_lag_topk(ours: &[f32], theirs: &[f32], lag_max: i64, rate: u32, k: usi | ||
| 372 | 442 | Some((oa, ob)) |
| 373 | 443 | } |
| 374 | 444 | }; |
| 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. | |
| 375 | 448 | 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 { | |
| 378 | 450 | if let Some((oa, ob)) = slice_at(lag, COARSE) { |
| 379 | 451 | let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]); |
| 380 | 452 | coarse.push((lag, c.abs())); |
| 381 | 453 | } |
| 382 | - lag += SEARCH_STRIDE; | |
| 383 | 454 | } |
| 384 | 455 | coarse.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal)); |
| 385 | 456 | let ten_s = 10 * rate.max(1) as usize; |
| @@ -2103,6 +2174,19 @@ fn ffmpeg_decode_at_rate( | ||
| 2103 | 2174 | /// not a regression gate (the asserted gate is |
| 2104 | 2175 | /// `sbr_real_library_matches_reference`, which already covers the full-band |
| 2105 | 2176 | /// 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. | |
| 2106 | 2190 | #[test] |
| 2107 | 2191 | fn core_only_matches_reference() { |
| 2108 | 2192 | if !have_ffmpeg() { |
| @@ -2121,9 +2205,17 @@ fn core_only_matches_reference() { | ||
| 2121 | 2205 | ours.len(), |
| 2122 | 2206 | core_rate |
| 2123 | 2207 | ); |
| 2208 | + let cutoff = f64::from(c.crossover_hz) * 0.9; | |
| 2124 | 2209 | 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}"); | |
| 2127 | 2219 | } |
| 2128 | 2220 | } |
| 2129 | 2221 | } |