forge
Explore
Log in

Don't you dare go Hollow

Widen synthetic_heaac_matrix's lag search past its own LAG_MAX edge

Browse files at this commit

93d654a49fa38e6adca7f68dcee1b47f750580a0Ahmet Kaan GümüşParent: a2fc9d8
  • test
    • widen synthetic_heaac_matrix's lag search past its own LAG_MAX edge
Show full message
test(aac-sbr): widen synthetic_heaac_matrix's lag search past its own LAG_MAX edge

best_lag_correlation's LAG_MAX=4000 bound was landing every HE-AAC row's
winning lag right at its own edge (-3634..-3882); adds
best_lag_correlation_wide (±20000, odd-stride coarse pass + full-WINDOW
refine, mirrors wide_lag_search_full_chain's shape) and uses it for the
full/below/ours_v_core columns plus a new ovc_lag column. Also fixes the
`above` band, which was comparing o[..n]/t[..n] with no lag offset at all
(only valid by accident at lag 0) -- now re-slices at the winning lag first,
same convention sbr_real_library_matches_reference uses. Adds a delay-probe
printout on two named rows testing candidate total HE-vs-reference delays.
crates/ec-aac/tests/sbr_real_library.rsmodified+119 -12
@@ -2107,6 +2107,60 @@ fn full_chain_low_band_matches_own_core() {
21072107 }
21082108 }
21092109
2110+/// Follow-up to round-52: `best_lag_correlation`'s `LAG_MAX=4000` bound was
2111+/// landing HE-AAC winning lags right at its own edge (-3634..-3882) on the
2112+/// synthetic matrix -- this is the same shape `wide_lag_search_full_chain`
2113+/// diagnosed on FMJ. Mirrors that diagnostic's odd-stride coarse pass, but
2114+/// over a caller-supplied `lag_max` and followed by the same full-`WINDOW`
2115+/// refine `best_lag_correlation` does, so its result is directly comparable
2116+/// (not just a coarse `|corr|` estimate).
2117+fn best_lag_correlation_wide(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64, f64) {
2118+ const COARSE: usize = 4096;
2119+ let start = ours
2120+ .len()
2121+ .min(theirs.len())
2122+ .saturating_sub(WINDOW)
2123+ .min(ours.len() / 4);
2124+ let slice_at = |lag: i64, len: usize| -> Option<(usize, usize)> {
2125+ let (oa, ob) = if lag >= 0 {
2126+ (start, start + lag as usize)
2127+ } else {
2128+ (start + (-lag) as usize, start)
2129+ };
2130+ if oa + len > ours.len() || ob + len > theirs.len() {
2131+ None
2132+ } else {
2133+ Some((oa, ob))
2134+ }
2135+ };
2136+ let mut coarse_best = (0i64, -1.0f64);
2137+ let mut lag = -lag_max;
2138+ while lag <= lag_max {
2139+ if let Some((oa, ob)) = slice_at(lag, COARSE) {
2140+ let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]).abs();
2141+ if c > coarse_best.1 {
2142+ coarse_best = (lag, c);
2143+ }
2144+ }
2145+ lag += 11; // odd stride so it doesn't alias against frame-length periodicity
2146+ }
2147+ let mut best = (coarse_best.0, -1.0f64, 0.0f64);
2148+ for lag in (coarse_best.0 - 16)..=(coarse_best.0 + 16) {
2149+ let Some((oa, ob)) = slice_at(lag, WINDOW) else {
2150+ continue;
2151+ };
2152+ let n = WINDOW.min(ours.len() - oa).min(theirs.len() - ob);
2153+ if n < 1024 {
2154+ continue;
2155+ }
2156+ let c = correlation(&ours[oa..oa + n], &theirs[ob..ob + n]);
2157+ if c.abs() > best.1 {
2158+ best = (lag, c.abs(), c);
2159+ }
2160+ }
2161+ (best.0, best.2)
2162+}
2163+
21102164 /// Round-52: a controlled fixture matrix -- unlike reeln/FMJ (one real
21112165 /// file each, whose sample rate, bitrate and content all differ at once),
21122166 /// every row here decodes the SAME synthesized source (three summed sine
@@ -2145,12 +2199,21 @@ fn synthetic_heaac_matrix() {
21452199 std::env::set_var("EC_AAC_SBR_SIDEINFO_DEBUG", "1");
21462200 }
21472201 println!(
2148- "{:>5} {:>6} {:>4} {:>4} {:>4} {:>8} {:>8} {:>8} {:>6} {:>11}",
2149- "rate", "kbps", "prof", "kx", "k2", "full", "below", "above", "lag", "ours_v_core"
2202+ "{:>5} {:>6} {:>4} {:>4} {:>4} {:>8} {:>8} {:>8} {:>6} {:>11} {:>8}",
2203+ "rate", "kbps", "prof", "kx", "k2", "full", "below", "above", "lag", "ours_v_core", "ovc_lag"
21502204 );
2205+ // Wide enough that the WIDE_LAG_MAX==LC's own decoder priming delay
2206+ // (2048) plus a full SBR-chain worth of extra delay (a few thousand
2207+ // samples at the doubled/full output rate) both land well inside it,
2208+ // not right at the edge the way the plain (LAG_MAX=4000) search did.
2209+ const WIDE_LAG_MAX: i64 = 20_000;
21512210 for rate in [48_000u32, 44_100] {
21522211 for br in ["32k", "48k", "64k", "96k"] {
2153- for (profile, prefix) in [("HE", "heaac"), ("LC", "lc")] {
2212+ // LC first: its winning lag is the pure container/decoder
2213+ // priming delay with no SBR chain involved, and is reused below
2214+ // as the anchor for the HE row's own delay-candidate probe.
2215+ let mut lc_lag: Option<i64> = None;
2216+ for (profile, prefix) in [("LC", "lc"), ("HE", "heaac")] {
21542217 let path = dir.join(format!("{prefix}_{rate}_{br}.m4a"));
21552218 if !path.exists() {
21562219 eprintln!("SKIP {}: missing", path.display());
@@ -2170,7 +2233,10 @@ fn synthetic_heaac_matrix() {
21702233 let theirs = ffmpeg_decode(&path, 0, ours.len());
21712234 let o = &ours[0];
21722235 let t = &theirs[0];
2173- let (lag, full) = best_lag_correlation(o, t);
2236+ let (lag, full) = best_lag_correlation_wide(o, t, WIDE_LAG_MAX);
2237+ if profile == "LC" {
2238+ lc_lag = Some(lag);
2239+ }
21742240 // Crossover in Hz from the actual parsed kx (band width
21752241 // rate/128, same domain `sbr_chain.rs`'s own doc comment
21762242 // gives `kx`/`k2` in) when SBR engaged; a fixed quarter-Nyquist
@@ -2180,13 +2246,27 @@ fn synthetic_heaac_matrix() {
21802246 } else {
21812247 f64::from(our_rate) * 0.25
21822248 };
2183- let (_, below) =
2184- best_lag_correlation(&lowpass(o, our_rate, crossover_hz), &lowpass(t, our_rate, crossover_hz));
2185- let n = WINDOW.min(o.len()).min(t.len());
2249+ let (_, below) = best_lag_correlation_wide(
2250+ &lowpass(o, our_rate, crossover_hz),
2251+ &lowpass(t, our_rate, crossover_hz),
2252+ WIDE_LAG_MAX,
2253+ );
2254+ // `above` re-slices at the FULL-band winning `lag` before
2255+ // splitting, same convention `sbr_real_library_matches_reference`
2256+ // uses -- round-52's first cut of this row measured `o[..n]`
2257+ // against `t[..n]` with no lag offset at all, which is only
2258+ // valid by accident at lag 0 and was silently wrong for
2259+ // every other winning lag in that table.
2260+ let (oa, ob) = if lag >= 0 {
2261+ (0usize, lag as usize)
2262+ } else {
2263+ ((-lag) as usize, 0usize)
2264+ };
2265+ let n = WINDOW.min(o.len().saturating_sub(oa)).min(t.len().saturating_sub(ob));
21862266 let above = if n >= 1024 {
21872267 correlation(
2188- &highpass(&o[..n], our_rate, crossover_hz),
2189- &highpass(&t[..n], our_rate, crossover_hz),
2268+ &highpass(&o[oa..oa + n], our_rate, crossover_hz),
2269+ &highpass(&t[ob..ob + n], our_rate, crossover_hz),
21902270 )
21912271 } else {
21922272 0.0
@@ -2201,17 +2281,44 @@ fn synthetic_heaac_matrix() {
22012281 let cutoff = f64::from(core_rate) * 0.4;
22022282 let ol = lowpass(o, our_rate, cutoff);
22032283 let ul = lowpass(&up, our_rate, cutoff);
2204- best_lag_correlation(&ol, &ul).1
2284+ best_lag_correlation_wide(&ol, &ul, WIDE_LAG_MAX)
22052285 })
22062286 } else {
22072287 None
22082288 };
22092289 println!(
2210- "{rate:>5} {br:>6} {profile:>4} {kx:>4} {k2:>4} {full:>8.4} {below:>8.4} {above:>8.4} {lag:>6} {:>11}",
2290+ "{rate:>5} {br:>6} {profile:>4} {kx:>4} {k2:>4} {full:>8.4} {below:>8.4} {above:>8.4} {lag:>6} {:>11} {:>8}",
2291+ ours_v_core
2292+ .map(|(_, c)| format!("{c:.4}"))
2293+ .unwrap_or_else(|| "n/a".into()),
22112294 ours_v_core
2212- .map(|c| format!("{c:.4}"))
2295+ .map(|(l, _)| l.to_string())
22132296 .unwrap_or_else(|| "n/a".into())
22142297 );
2298+ // Coordinator follow-up (4): on the two named rows, probe
2299+ // candidate total HE-vs-reference delays built from LC's own
2300+ // priming lag plus a guessed SBR-chain contribution, to see
2301+ // which (if any) actually lands near 1.0 -- and whether that
2302+ // offset is constant across the two rows.
2303+ if profile == "HE"
2304+ && ((rate == 44_100 && br == "48k") || (rate == 48_000 && br == "64k"))
2305+ {
2306+ let lc = lc_lag.unwrap_or(-2048);
2307+ println!(" ch0 delay probe (LC lag {lc}):");
2308+ for (label, extra) in [
2309+ ("2990", 2990i64),
2310+ ("3010", 3010),
2311+ ("3020", 3020),
2312+ ("1505x2", 1505 * 2),
2313+ ("962x2", 962 * 2),
2314+ ] {
2315+ let candidate = (lc - 2048) + extra;
2316+ println!(
2317+ " offset={label:>6} -> lag={candidate:>6} corr={:.6}",
2318+ correlation_at_lag(o, t, candidate)
2319+ );
2320+ }
2321+ }
22152322 if profile == "LC" {
22162323 assert!(
22172324 full >= 0.999,