Widen synthetic_heaac_matrix's lag search past its own LAG_MAX edge
Browse files at this commit93d654a49fa38e6adca7f68dcee1b47f750580a0Parent: a2fc9d8- test
- widen synthetic_heaac_matrix's lag search past its own LAG_MAX edge
1 changed file +119 -12
crates/ec-aac/tests/sbr_real_library.rsmodified+119 -12
| @@ -2107,6 +2107,60 @@ fn full_chain_low_band_matches_own_core() { | |||
| 2107 | } | 2107 | } |
| 2108 | } | 2108 | } |
| 2109 | 2109 | ||
| 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 | + | ||
| 2110 | /// Round-52: a controlled fixture matrix -- unlike reeln/FMJ (one real | 2164 | /// Round-52: a controlled fixture matrix -- unlike reeln/FMJ (one real |
| 2111 | /// file each, whose sample rate, bitrate and content all differ at once), | 2165 | /// file each, whose sample rate, bitrate and content all differ at once), |
| 2112 | /// every row here decodes the SAME synthesized source (three summed sine | 2166 | /// every row here decodes the SAME synthesized source (three summed sine |
| @@ -2145,12 +2199,21 @@ fn synthetic_heaac_matrix() { | |||
| 2145 | std::env::set_var("EC_AAC_SBR_SIDEINFO_DEBUG", "1"); | 2199 | std::env::set_var("EC_AAC_SBR_SIDEINFO_DEBUG", "1"); |
| 2146 | } | 2200 | } |
| 2147 | println!( | 2201 | println!( |
| 2148 | - "{:>5} {:>6} {:>4} {:>4} {:>4} {:>8} {:>8} {:>8} {:>6} {:>11}", | 2202 | + "{:>5} {:>6} {:>4} {:>4} {:>4} {:>8} {:>8} {:>8} {:>6} {:>11} {:>8}", |
| 2149 | - "rate", "kbps", "prof", "kx", "k2", "full", "below", "above", "lag", "ours_v_core" | 2203 | + "rate", "kbps", "prof", "kx", "k2", "full", "below", "above", "lag", "ours_v_core", "ovc_lag" |
| 2150 | ); | 2204 | ); |
| 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; | ||
| 2151 | for rate in [48_000u32, 44_100] { | 2210 | for rate in [48_000u32, 44_100] { |
| 2152 | for br in ["32k", "48k", "64k", "96k"] { | 2211 | 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")] { | ||
| 2154 | let path = dir.join(format!("{prefix}_{rate}_{br}.m4a")); | 2217 | let path = dir.join(format!("{prefix}_{rate}_{br}.m4a")); |
| 2155 | if !path.exists() { | 2218 | if !path.exists() { |
| 2156 | eprintln!("SKIP {}: missing", path.display()); | 2219 | eprintln!("SKIP {}: missing", path.display()); |
| @@ -2170,7 +2233,10 @@ fn synthetic_heaac_matrix() { | |||
| 2170 | let theirs = ffmpeg_decode(&path, 0, ours.len()); | 2233 | let theirs = ffmpeg_decode(&path, 0, ours.len()); |
| 2171 | let o = &ours[0]; | 2234 | let o = &ours[0]; |
| 2172 | let t = &theirs[0]; | 2235 | 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 | + } | ||
| 2174 | // Crossover in Hz from the actual parsed kx (band width | 2240 | // Crossover in Hz from the actual parsed kx (band width |
| 2175 | // rate/128, same domain `sbr_chain.rs`'s own doc comment | 2241 | // rate/128, same domain `sbr_chain.rs`'s own doc comment |
| 2176 | // gives `kx`/`k2` in) when SBR engaged; a fixed quarter-Nyquist | 2242 | // gives `kx`/`k2` in) when SBR engaged; a fixed quarter-Nyquist |
| @@ -2180,13 +2246,27 @@ fn synthetic_heaac_matrix() { | |||
| 2180 | } else { | 2246 | } else { |
| 2181 | f64::from(our_rate) * 0.25 | 2247 | f64::from(our_rate) * 0.25 |
| 2182 | }; | 2248 | }; |
| 2183 | - let (_, below) = | 2249 | + let (_, below) = best_lag_correlation_wide( |
| 2184 | - best_lag_correlation(&lowpass(o, our_rate, crossover_hz), &lowpass(t, our_rate, crossover_hz)); | 2250 | + &lowpass(o, our_rate, crossover_hz), |
| 2185 | - let n = WINDOW.min(o.len()).min(t.len()); | 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)); | ||
| 2186 | let above = if n >= 1024 { | 2266 | let above = if n >= 1024 { |
| 2187 | correlation( | 2267 | correlation( |
| 2188 | - &highpass(&o[..n], our_rate, crossover_hz), | 2268 | + &highpass(&o[oa..oa + n], our_rate, crossover_hz), |
| 2189 | - &highpass(&t[..n], our_rate, crossover_hz), | 2269 | + &highpass(&t[ob..ob + n], our_rate, crossover_hz), |
| 2190 | ) | 2270 | ) |
| 2191 | } else { | 2271 | } else { |
| 2192 | 0.0 | 2272 | 0.0 |
| @@ -2201,17 +2281,44 @@ fn synthetic_heaac_matrix() { | |||
| 2201 | let cutoff = f64::from(core_rate) * 0.4; | 2281 | let cutoff = f64::from(core_rate) * 0.4; |
| 2202 | let ol = lowpass(o, our_rate, cutoff); | 2282 | let ol = lowpass(o, our_rate, cutoff); |
| 2203 | let ul = lowpass(&up, our_rate, cutoff); | 2283 | let ul = lowpass(&up, our_rate, cutoff); |
| 2204 | - best_lag_correlation(&ol, &ul).1 | 2284 | + best_lag_correlation_wide(&ol, &ul, WIDE_LAG_MAX) |
| 2205 | }) | 2285 | }) |
| 2206 | } else { | 2286 | } else { |
| 2207 | None | 2287 | None |
| 2208 | }; | 2288 | }; |
| 2209 | println!( | 2289 | 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}", |
| 2211 | ours_v_core | 2291 | ours_v_core |
| 2212 | - .map(|c| format!("{c:.4}")) | 2292 | + .map(|(_, c)| format!("{c:.4}")) |
| 2293 | + .unwrap_or_else(|| "n/a".into()), | ||
| 2294 | + ours_v_core | ||
| 2295 | + .map(|(l, _)| l.to_string()) | ||
| 2213 | .unwrap_or_else(|| "n/a".into()) | 2296 | .unwrap_or_else(|| "n/a".into()) |
| 2214 | ); | 2297 | ); |
| 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 | + } | ||
| 2215 | if profile == "LC" { | 2322 | if profile == "LC" { |
| 2216 | assert!( | 2323 | assert!( |
| 2217 | full >= 0.999, | 2324 | full >= 0.999, |