Merge branch 'sbr-per-au'
Browse files at this commit1 changed file +444 -83
crates/ec-aac/tests/sbr_real_library.rsmodified+444 -83
| @@ -173,12 +173,38 @@ fn our_decode( | ||
| 173 | 173 | Some((planes, sbr, rate)) |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | -/// Search window and lag bound: keeps the lag search and the correlation sum | |
| 177 | -/// both bounded, per the charter -- an unbounded version over a whole film's | |
| 178 | -/// worth of samples was too slow to be an every-run test. | |
| 179 | -const LAG_MAX: usize = 4_000; | |
| 176 | +/// Search window bound: keeps the correlation sum bounded, per the charter | |
| 177 | +/// -- an unbounded version over a whole film's worth of samples was too slow | |
| 178 | +/// to be an every-run test. | |
| 180 | 179 | const WINDOW: usize = 200_000; |
| 181 | 180 | |
| 181 | +/// Wide lag search bound for the specific gate proven to need it | |
| 182 | +/// (`sbr_real_library_matches_reference`, ours-vs-reference across a real | |
| 183 | +/// container's remux/priming delay). Measured (round-54 wide-lag probe, | |
| 184 | +/// worktree sbr-per-au) FMJ's true full-chain delay vs the reference is | |
| 185 | +/// ~22,913 samples @48kHz -- that gate's old `LAG_MAX=4_000` search clipped | |
| 186 | +/// at its own edge and silently reported the edge's noise as the answer for | |
| 187 | +/// 50+ rounds (class: "an alignment instrument whose result sits at its own | |
| 188 | +/// search edge is invalid"). 100_000 covers that with 4x headroom and is | |
| 189 | +/// already the bound round-53's own `WIDE_LAG_MAX=100_000` probe used to | |
| 190 | +/// find it. NOT used as the default everywhere: a wider bound also widens | |
| 191 | +/// the coarse pass's exposure to spurious short-slice noise peaks, so | |
| 192 | +/// call sites whose true delay is known-small (container/decoder priming, | |
| 193 | +/// ours-vs-own-core self-consistency) keep their original, narrower bound | |
| 194 | +/// -- round-54 first tried blanket-widening every call site to this value | |
| 195 | +/// and it flipped `full_chain_low_band_matches_own_core` and the LC control | |
| 196 | +/// row of `synthetic_heaac_matrix` from a correct small-lag answer to a | |
| 197 | +/// wrong, noise-driven distant one, still comfortably inside the bound | |
| 198 | +/// (not `at_edge`) but wrong regardless. | |
| 199 | +const SEARCH_LAG_MAX: i64 = 100_000; | |
| 200 | +/// Original bound for the plain `best_lag_correlation` wrapper, used by the | |
| 201 | +/// file's other (ours-vs-reference small-priming-delay, or ours-vs-ours) | |
| 202 | +/// callers that were never shown to need more. | |
| 203 | +const PLAIN_LAG_MAX: i64 = 4_000; | |
| 204 | +/// Odd stride `best_lag_correlation_ex`'s coarse pass steps by, so it | |
| 205 | +/// doesn't alias against frame-length periodicity. | |
| 206 | +const SEARCH_STRIDE: i64 = 11; | |
| 207 | + | |
| 182 | 208 | fn dot(a: &[f32], b: &[f32]) -> f64 { |
| 183 | 209 | a.iter() |
| 184 | 210 | .zip(b) |
| @@ -201,14 +227,19 @@ fn correlation(a: &[f32], b: &[f32]) -> f64 { | ||
| 201 | 227 | if den == 0.0 { 0.0 } else { num / den } |
| 202 | 228 | } |
| 203 | 229 | |
| 204 | -/// Best correlation over a bounded lag search. Two passes keep the whole | |
| 205 | -/// search O(reasonable) on a film-length file: a coarse pass over every lag | |
| 206 | -/// but a short slice finds roughly where the alignment is, then one full-size | |
| 207 | -/// correlation at that lag (and its immediate neighbours, in case the coarse | |
| 208 | -/// slice picked a noisy lag) gives the reported number. The 8001-lag charter | |
| 209 | -/// bound times the full `WINDOW` in one pass, tried first, took minutes on | |
| 210 | -/// this box -- this is that ceiling brought back to milliseconds. | |
| 211 | -fn best_lag_correlation(ours: &[f32], theirs: &[f32]) -> (i64, f64) { | |
| 230 | +/// Best correlation over a bounded lag search, PLUS whether the winning lag | |
| 231 | +/// sits at the search's own edge -- the single instrument every asserting | |
| 232 | +/// test in this file now routes through (class fix, round-54: a search that | |
| 233 | +/// silently clips at its bound and reports the edge's noise as "the | |
| 234 | +/// answer" is not an instrument, it's a lie). Two passes keep the whole | |
| 235 | +/// search O(reasonable) even at `SEARCH_LAG_MAX`-sized bounds on a | |
| 236 | +/// film-length file: an odd-`SEARCH_STRIDE` coarse pass over a short slice | |
| 237 | +/// finds roughly where the alignment is (the stride, not full 1-by-1 | |
| 238 | +/// stepping, is what keeps a `lag_max` in the tens of thousands cheap, and | |
| 239 | +/// also avoids aliasing against frame-length periodicity), then one | |
| 240 | +/// full-`WINDOW` correlation at that lag (and its immediate neighbours, in | |
| 241 | +/// case the coarse slice picked a noisy lag) gives the reported number. | |
| 242 | +fn best_lag_correlation_ex(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64, f64, bool) { | |
| 212 | 243 | const COARSE: usize = 4_096; |
| 213 | 244 | let start = ours |
| 214 | 245 | .len() |
| @@ -244,18 +275,20 @@ fn best_lag_correlation(ours: &[f32], theirs: &[f32]) -> (i64, f64) { | ||
| 244 | 275 | // positive one from a 20x-shorter slice. Carry the signed value through |
| 245 | 276 | // so callers can see the sign, not just |corr|. |
| 246 | 277 | let mut coarse_best = (0i64, -1.0f64, 0.0f64); |
| 247 | - for lag in -(LAG_MAX as i64)..=(LAG_MAX as i64) { | |
| 248 | - let Some((oa, ob)) = slice_at(lag, COARSE) else { | |
| 249 | - continue; | |
| 250 | - }; | |
| 251 | - let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]); | |
| 252 | - if c.abs() > coarse_best.1 { | |
| 253 | - coarse_best = (lag, c.abs(), c); | |
| 278 | + let mut lag = -lag_max; | |
| 279 | + while lag <= lag_max { | |
| 280 | + if let Some((oa, ob)) = slice_at(lag, COARSE) { | |
| 281 | + let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]); | |
| 282 | + if c.abs() > coarse_best.1 { | |
| 283 | + coarse_best = (lag, c.abs(), c); | |
| 284 | + } | |
| 254 | 285 | } |
| 286 | + lag += SEARCH_STRIDE; | |
| 255 | 287 | } |
| 288 | + let at_edge = coarse_best.0.abs() >= lag_max - SEARCH_STRIDE; | |
| 256 | 289 | let mut best = (coarse_best.0, -1.0f64, 0.0f64); |
| 257 | 290 | let mut found = false; |
| 258 | - for lag in (coarse_best.0 - 8)..=(coarse_best.0 + 8) { | |
| 291 | + for lag in (coarse_best.0 - 2 * SEARCH_STRIDE)..=(coarse_best.0 + 2 * SEARCH_STRIDE) { | |
| 259 | 292 | let Some((oa, ob)) = slice_at(lag, WINDOW) else { |
| 260 | 293 | continue; |
| 261 | 294 | }; |
| @@ -272,7 +305,25 @@ fn best_lag_correlation(ours: &[f32], theirs: &[f32]) -> (i64, f64) { | ||
| 272 | 305 | if !found { |
| 273 | 306 | best = coarse_best; |
| 274 | 307 | } |
| 275 | - (best.0, best.2) | |
| 308 | + (best.0, best.2, at_edge) | |
| 309 | +} | |
| 310 | + | |
| 311 | +/// Thin wrapper over `best_lag_correlation_ex` at `PLAIN_LAG_MAX`, for the | |
| 312 | +/// many non-asserting call sites that only want `(lag, corr)` and whose true | |
| 313 | +/// delay was never shown to exceed the original 4_000 bound. The one gate | |
| 314 | +/// proven to need the wide bound (`sbr_real_library_matches_reference`) | |
| 315 | +/// calls `_ex` directly with `SEARCH_LAG_MAX` instead of going through this | |
| 316 | +/// wrapper. | |
| 317 | +fn best_lag_correlation(ours: &[f32], theirs: &[f32]) -> (i64, f64) { | |
| 318 | + let (lag, corr, _) = best_lag_correlation_ex(ours, theirs, PLAIN_LAG_MAX); | |
| 319 | + (lag, corr) | |
| 320 | +} | |
| 321 | + | |
| 322 | +/// Thin wrapper over `best_lag_correlation_ex` at a caller-supplied bound, | |
| 323 | +/// for non-asserting call sites (asserting ones use `_ex` directly). | |
| 324 | +fn best_lag_correlation_wide(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64, f64) { | |
| 325 | + let (lag, corr, _) = best_lag_correlation_ex(ours, theirs, lag_max); | |
| 326 | + (lag, corr) | |
| 276 | 327 | } |
| 277 | 328 | |
| 278 | 329 | /// Signed correlation of `ours` against `theirs` at one fixed `lag`, full |
| @@ -2097,14 +2148,36 @@ fn full_chain_low_band_matches_own_core() { | ||
| 2097 | 2148 | let cutoff = f64::from(core_rate) * 0.4; |
| 2098 | 2149 | let ol = lowpass(&full[ch], full_rate, cutoff); |
| 2099 | 2150 | let ul = lowpass(&up, full_rate, cutoff); |
| 2100 | - // Wide search, not the plain `best_lag_correlation`'s | |
| 2101 | - // `LAG_MAX=4000` -- the QMF-chain delay this measures is not a | |
| 2102 | - // fixed constant across files/rates (measured -576 on reeln | |
| 2103 | - // vs the stale -897 this test used to assert against), so the | |
| 2104 | - // lag itself is part of what's measured here, not assumed. | |
| 2151 | + // Escalating search: narrow (`WIDE_LAG_MAX`=20_000, the file's | |
| 2152 | + // original bound) first, widen to `SEARCH_LAG_MAX` only if that | |
| 2153 | + // narrow search hit its own edge (`at_edge`). A single shared | |
| 2154 | + // wide bound was tried first (round-54) and, unconditionally | |
| 2155 | + // widening this comparison, flipped reeln from its correct | |
| 2156 | + // small-lag answer (-576, corr 0.9967) to a wrong, noise-driven | |
| 2157 | + // distant one (-912, corr -0.295) -- a genuinely low-quality | |
| 2158 | + // low-passed signal's coarse correlation surface has enough | |
| 2159 | + // stray peaks over a 100_000-wide range to beat a real but | |
| 2160 | + // modest true peak. Escalating only when the narrow bound | |
| 2161 | + // proves insufficient (FMJ's own core-vs-full delay lands AT | |
| 2162 | + // the 20_000 edge, corr 0.217393 there, the same edge-noise | |
| 2163 | + // class the reference-vs-ours gate had) gets both files right | |
| 2164 | + // without exposing the well-behaved one to the wider bound's | |
| 2165 | + // noise floor. | |
| 2105 | 2166 | const WIDE_LAG_MAX: i64 = 20_000; |
| 2106 | - let (lag, corr) = best_lag_correlation_wide(&ol, &ul, WIDE_LAG_MAX); | |
| 2167 | + let (mut lag, mut corr, mut at_edge) = best_lag_correlation_ex(&ol, &ul, WIDE_LAG_MAX); | |
| 2168 | + let mut bound = WIDE_LAG_MAX; | |
| 2169 | + if at_edge { | |
| 2170 | + bound = SEARCH_LAG_MAX; | |
| 2171 | + (lag, corr, at_edge) = best_lag_correlation_ex(&ol, &ul, SEARCH_LAG_MAX); | |
| 2172 | + } | |
| 2107 | 2173 | println!(" ch{ch}: measured best lag {lag}, corr {corr:.6}"); |
| 2174 | + assert!( | |
| 2175 | + !at_edge, | |
| 2176 | + "{} ch{ch}: winning lag {lag} sits at the +/-{bound} \ | |
| 2177 | + search bound -- the true alignment is outside the search, \ | |
| 2178 | + corr {corr:.6} is noise, not a measurement", | |
| 2179 | + c.path.display() | |
| 2180 | + ); | |
| 2108 | 2181 | assert!( |
| 2109 | 2182 | corr >= 0.99, |
| 2110 | 2183 | "{} ch{ch}: full-chain low band vs our own core-only decode \ |
| @@ -2117,60 +2190,6 @@ fn full_chain_low_band_matches_own_core() { | ||
| 2117 | 2190 | } |
| 2118 | 2191 | } |
| 2119 | 2192 | |
| 2120 | -/// Follow-up to round-52: `best_lag_correlation`'s `LAG_MAX=4000` bound was | |
| 2121 | -/// landing HE-AAC winning lags right at its own edge (-3634..-3882) on the | |
| 2122 | -/// synthetic matrix -- this is the same shape `wide_lag_search_full_chain` | |
| 2123 | -/// diagnosed on FMJ. Mirrors that diagnostic's odd-stride coarse pass, but | |
| 2124 | -/// over a caller-supplied `lag_max` and followed by the same full-`WINDOW` | |
| 2125 | -/// refine `best_lag_correlation` does, so its result is directly comparable | |
| 2126 | -/// (not just a coarse `|corr|` estimate). | |
| 2127 | -fn best_lag_correlation_wide(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64, f64) { | |
| 2128 | - const COARSE: usize = 4096; | |
| 2129 | - let start = ours | |
| 2130 | - .len() | |
| 2131 | - .min(theirs.len()) | |
| 2132 | - .saturating_sub(WINDOW) | |
| 2133 | - .min(ours.len() / 4); | |
| 2134 | - let slice_at = |lag: i64, len: usize| -> Option<(usize, usize)> { | |
| 2135 | - let (oa, ob) = if lag >= 0 { | |
| 2136 | - (start, start + lag as usize) | |
| 2137 | - } else { | |
| 2138 | - (start + (-lag) as usize, start) | |
| 2139 | - }; | |
| 2140 | - if oa + len > ours.len() || ob + len > theirs.len() { | |
| 2141 | - None | |
| 2142 | - } else { | |
| 2143 | - Some((oa, ob)) | |
| 2144 | - } | |
| 2145 | - }; | |
| 2146 | - let mut coarse_best = (0i64, -1.0f64); | |
| 2147 | - let mut lag = -lag_max; | |
| 2148 | - while lag <= lag_max { | |
| 2149 | - if let Some((oa, ob)) = slice_at(lag, COARSE) { | |
| 2150 | - let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]).abs(); | |
| 2151 | - if c > coarse_best.1 { | |
| 2152 | - coarse_best = (lag, c); | |
| 2153 | - } | |
| 2154 | - } | |
| 2155 | - lag += 11; // odd stride so it doesn't alias against frame-length periodicity | |
| 2156 | - } | |
| 2157 | - let mut best = (coarse_best.0, -1.0f64, 0.0f64); | |
| 2158 | - for lag in (coarse_best.0 - 16)..=(coarse_best.0 + 16) { | |
| 2159 | - let Some((oa, ob)) = slice_at(lag, WINDOW) else { | |
| 2160 | - continue; | |
| 2161 | - }; | |
| 2162 | - let n = WINDOW.min(ours.len() - oa).min(theirs.len() - ob); | |
| 2163 | - if n < 1024 { | |
| 2164 | - continue; | |
| 2165 | - } | |
| 2166 | - let c = correlation(&ours[oa..oa + n], &theirs[ob..ob + n]); | |
| 2167 | - if c.abs() > best.1 { | |
| 2168 | - best = (lag, c.abs(), c); | |
| 2169 | - } | |
| 2170 | - } | |
| 2171 | - (best.0, best.2) | |
| 2172 | -} | |
| 2173 | - | |
| 2174 | 2193 | /// Round-52: a controlled fixture matrix -- unlike reeln/FMJ (one real |
| 2175 | 2194 | /// file each, whose sample rate, bitrate and content all differ at once), |
| 2176 | 2195 | /// every row here decodes the SAME synthesized source (three summed sine |
| @@ -2448,6 +2467,7 @@ fn sbr_real_library_matches_reference() { | ||
| 2448 | 2467 | let mut checked = 0; |
| 2449 | 2468 | let mut worst_full = 1.0f64; |
| 2450 | 2469 | let mut worst_low = 1.0f64; |
| 2470 | + let mut any_at_edge: Option<String> = None; | |
| 2451 | 2471 | for c in &list { |
| 2452 | 2472 | let Some((ours, sbr, rate)) = our_decode(&c.path, c.aac_stream) else { |
| 2453 | 2473 | eprintln!("SKIP {}: could not decode", c.path.display()); |
| @@ -2518,7 +2538,15 @@ fn sbr_real_library_matches_reference() { | ||
| 2518 | 2538 | rms(t), |
| 2519 | 2539 | &t[..10.min(t.len())], |
| 2520 | 2540 | ); |
| 2521 | - let (lag, full) = best_lag_correlation(o, t); | |
| 2541 | + let (lag, full, full_at_edge) = best_lag_correlation_ex(o, t, SEARCH_LAG_MAX); | |
| 2542 | + if full_at_edge { | |
| 2543 | + any_at_edge.get_or_insert_with(|| { | |
| 2544 | + format!( | |
| 2545 | + "{} ch{ch} full-band: winning lag {lag} at +/-{SEARCH_LAG_MAX} search edge", | |
| 2546 | + c.path.display() | |
| 2547 | + ) | |
| 2548 | + }); | |
| 2549 | + } | |
| 2522 | 2550 | // Re-slice both signals at the winning lag before splitting into |
| 2523 | 2551 | // bands, so the band correlations are aligned too. |
| 2524 | 2552 | let (oa, ob) = if lag >= 0 { |
| @@ -2546,7 +2574,16 @@ fn sbr_real_library_matches_reference() { | ||
| 2546 | 2574 | let low = { |
| 2547 | 2575 | let ol_full = lowpass(o, rate, c.crossover_hz); |
| 2548 | 2576 | let tl_full = lowpass(t, rate, c.crossover_hz); |
| 2549 | - let (low_lag, low_corr) = best_lag_correlation(&ol_full, &tl_full); | |
| 2577 | + let (low_lag, low_corr, low_at_edge) = | |
| 2578 | + best_lag_correlation_ex(&ol_full, &tl_full, SEARCH_LAG_MAX); | |
| 2579 | + if low_at_edge { | |
| 2580 | + any_at_edge.get_or_insert_with(|| { | |
| 2581 | + format!( | |
| 2582 | + "{} ch{ch} below-crossover: winning lag {low_lag} at +/-{SEARCH_LAG_MAX} search edge", | |
| 2583 | + c.path.display() | |
| 2584 | + ) | |
| 2585 | + }); | |
| 2586 | + } | |
| 2550 | 2587 | if low_lag != lag { |
| 2551 | 2588 | eprintln!( |
| 2552 | 2589 | " ch{ch} below-crossover band's own best lag {low_lag} differs from full-band lag {lag}" |
| @@ -2973,6 +3010,12 @@ fn sbr_real_library_matches_reference() { | ||
| 2973 | 3010 | eprintln!("SKIP: no plain-SBR real files decoded"); |
| 2974 | 3011 | return; |
| 2975 | 3012 | } |
| 3013 | + assert!( | |
| 3014 | + any_at_edge.is_none(), | |
| 3015 | + "lag search hit its bound: {} -- the reported correlations are noise \ | |
| 3016 | + at the search edge, not a measurement", | |
| 3017 | + any_at_edge.unwrap_or_default() | |
| 3018 | + ); | |
| 2976 | 3019 | assert!( |
| 2977 | 3020 | worst_full >= 0.999, |
| 2978 | 3021 | "worst full-band correlation {worst_full:.6} < 0.999 bar" |
| @@ -3432,6 +3475,324 @@ fn sbr_header_feature_table() { | ||
| 3432 | 3475 | } |
| 3433 | 3476 | } |
| 3434 | 3477 | |
| 3478 | +/// Round (sbr-per-au): per-AU localization. Decodes the same AU list through | |
| 3479 | +/// (a) the core-only decoder and (b) the full SBR chain, tracks each AU's | |
| 3480 | +/// sample range in both output streams as it goes (so a per-AU decode | |
| 3481 | +/// failure on either side does not desync the mapping), aligns the two | |
| 3482 | +/// streams ONCE in the core's own native rate domain (same decimate | |
| 3483 | +/// convention `synthetic_heaac_matrix`'s `ours_v_core` row already trusts), | |
| 3484 | +/// then re-uses that single lag for every AU's own low-band correlation. | |
| 3485 | +/// Prints the first 5 AUs below 0.9 with that AU's SBR side-info (source/ | |
| 3486 | +/// envelope count/freq_res/kx/k2) so a per-frame trigger (e.g. `hold` | |
| 3487 | +/// frames, a specific `freq_res`) shows up directly instead of needing a | |
| 3488 | +/// second pass. If the first divergent AU is 0, that is reported explicitly | |
| 3489 | +/// -- divergence from the very first AU means a per-stream setup mismatch, | |
| 3490 | +/// not a per-frame trigger. | |
| 3491 | +fn per_au_low_band_probe(label: &str, path: &Path, aac_stream: usize) { | |
| 3492 | + let Some((asc, aus)) = extract_aac_track(path, aac_stream) else { | |
| 3493 | + eprintln!("SKIP {label}: extract_aac_track failed"); | |
| 3494 | + return; | |
| 3495 | + }; | |
| 3496 | + let Ok(full_cfg) = parse_audio_specific_config(&asc) else { | |
| 3497 | + eprintln!("SKIP {label}: asc parse failed"); | |
| 3498 | + return; | |
| 3499 | + }; | |
| 3500 | + if !full_cfg.sbr_present { | |
| 3501 | + eprintln!("SKIP {label}: no SBR"); | |
| 3502 | + return; | |
| 3503 | + } | |
| 3504 | + let mut core_cfg = full_cfg.clone(); | |
| 3505 | + core_cfg.sbr_present = false; | |
| 3506 | + core_cfg.ps_present = false; | |
| 3507 | + core_cfg.extension_sample_rate = None; | |
| 3508 | + let core_rate = core_cfg.sample_rate; | |
| 3509 | + let mut core_dec = AacDecoder::with_config(core_cfg); | |
| 3510 | + let mut full_dec = AacDecoder::with_config(full_cfg); | |
| 3511 | + let full_rate = full_dec.output_sample_rate().unwrap_or(core_rate * 2); | |
| 3512 | + | |
| 3513 | + struct AuInfo { | |
| 3514 | + core_range: (usize, usize), | |
| 3515 | + side: Option<ec_aac::SbrSideInfoRow>, | |
| 3516 | + } | |
| 3517 | + let mut core_ch0: Vec<f32> = Vec::new(); | |
| 3518 | + let mut full_ch0: Vec<f32> = Vec::new(); | |
| 3519 | + let mut aus_info: Vec<AuInfo> = Vec::new(); | |
| 3520 | + for au in aus.iter().take(1_500) { | |
| 3521 | + let side_before = ec_aac::sbr_sideinfo_log().len(); | |
| 3522 | + let cs = core_ch0.len(); | |
| 3523 | + if let Ok(f) = core_dec.decode(au, None) { | |
| 3524 | + let ch = usize::from(f.channels).max(1); | |
| 3525 | + core_ch0.extend(f.samples.iter().step_by(ch).copied()); | |
| 3526 | + } | |
| 3527 | + let ce = core_ch0.len(); | |
| 3528 | + if let Ok(f) = full_dec.decode(au, None) { | |
| 3529 | + let ch = usize::from(f.channels).max(1); | |
| 3530 | + full_ch0.extend(f.samples.iter().step_by(ch).copied()); | |
| 3531 | + } | |
| 3532 | + let side = ec_aac::sbr_sideinfo_log()[side_before..] | |
| 3533 | + .iter() | |
| 3534 | + .find(|r| r.ch == 0) | |
| 3535 | + .cloned(); | |
| 3536 | + aus_info.push(AuInfo { | |
| 3537 | + core_range: (cs, ce), | |
| 3538 | + side, | |
| 3539 | + }); | |
| 3540 | + if full_ch0.len() > MAX_SAMPLES { | |
| 3541 | + break; | |
| 3542 | + } | |
| 3543 | + } | |
| 3544 | + if core_ch0.is_empty() || full_ch0.is_empty() { | |
| 3545 | + eprintln!("SKIP {label}: empty decode (core {} full {})", core_ch0.len(), full_ch0.len()); | |
| 3546 | + return; | |
| 3547 | + } | |
| 3548 | + | |
| 3549 | + // Same convention `full_chain_low_band_matches_own_core` already | |
| 3550 | + // validated (0.9967 on reeln): naive nearest-neighbor 2x-upsample | |
| 3551 | + // the core into the full-rate domain, low-pass both well under the core | |
| 3552 | + // Nyquist. The decimate-into-core-rate convention `synthetic_heaac_matrix` | |
| 3553 | + // uses for its own `ours_v_core` diagnostic column was tried first here | |
| 3554 | + // and reads badly (~-0.19) even on reeln, which is known-good -- | |
| 3555 | + // that column is evidently not a reliable self-consistency probe on its | |
| 3556 | + // own, so this per-AU tool anchors on the convention independently | |
| 3557 | + // proven correct instead. | |
| 3558 | + let up: Vec<f32> = core_ch0.iter().flat_map(|&s| [s, s]).collect(); | |
| 3559 | + let cutoff = f64::from(core_rate) * 0.4; | |
| 3560 | + let ol = lowpass(&full_ch0, full_rate, cutoff); | |
| 3561 | + let ul = lowpass(&up, full_rate, cutoff); | |
| 3562 | + const WIDE_LAG_MAX: i64 = 20_000; | |
| 3563 | + let (lag, align_corr) = best_lag_correlation_wide(&ol, &ul, WIDE_LAG_MAX); | |
| 3564 | + println!( | |
| 3565 | + "{label}: core={core_rate}Hz full={full_rate}Hz global align (upsampled full-rate domain) lag={lag} corr={align_corr:.6}" | |
| 3566 | + ); | |
| 3567 | + | |
| 3568 | + let mut shown = 0usize; | |
| 3569 | + let mut first_divergent: Option<usize> = None; | |
| 3570 | + for (i, info) in aus_info.iter().enumerate() { | |
| 3571 | + let (cs, ce) = info.core_range; | |
| 3572 | + if ce <= cs { | |
| 3573 | + continue; | |
| 3574 | + } | |
| 3575 | + // core_range is in core-domain sample counts; the upsampled/lowpassed | |
| 3576 | + // arrays are at full rate, so the AU's window there is 2x as wide. | |
| 3577 | + let fs = cs * 2; | |
| 3578 | + let fe = ce * 2; | |
| 3579 | + let ds = match usize::try_from(fs as i64 + lag) { | |
| 3580 | + Ok(v) => v, | |
| 3581 | + Err(_) => continue, | |
| 3582 | + }; | |
| 3583 | + let de = ds + (fe - fs); | |
| 3584 | + if de > ul.len() || fe > ol.len() { | |
| 3585 | + break; | |
| 3586 | + } | |
| 3587 | + let c = correlation(&ol[fs..fe], &ul[ds..de]); | |
| 3588 | + if c < 0.9 { | |
| 3589 | + if first_divergent.is_none() { | |
| 3590 | + first_divergent = Some(i); | |
| 3591 | + } | |
| 3592 | + if shown < 5 { | |
| 3593 | + println!(" AU {i}: low-band corr={c:.6} side={:?}", info.side); | |
| 3594 | + shown += 1; | |
| 3595 | + } | |
| 3596 | + } | |
| 3597 | + } | |
| 3598 | + match first_divergent { | |
| 3599 | + Some(0) => println!( | |
| 3600 | + "{label}: divergence starts at AU 0 -- per-STREAM setup mismatch, not a per-frame trigger" | |
| 3601 | + ), | |
| 3602 | + Some(i) => println!("{label}: first divergent AU = {i}"), | |
| 3603 | + None => println!("{label}: no AU below 0.9 corr in the probed window"), | |
| 3604 | + } | |
| 3605 | +} | |
| 3606 | + | |
| 3607 | +#[test] | |
| 3608 | +fn per_au_low_band_divergence() { | |
| 3609 | + if !have_ffmpeg() { | |
| 3610 | + eprintln!("SKIP: ffmpeg not on PATH"); | |
| 3611 | + return; | |
| 3612 | + } | |
| 3613 | + unsafe { | |
| 3614 | + std::env::set_var("EC_AAC_SBR_SIDEINFO_DEBUG", "1"); | |
| 3615 | + } | |
| 3616 | + for c in &candidates() { | |
| 3617 | + per_au_low_band_probe(&c.path.display().to_string(), &c.path, c.aac_stream); | |
| 3618 | + } | |
| 3619 | + let dir = std::env::var("EC_AAC_HEAAC_FIXTURES").unwrap_or_else(|_| { | |
| 3620 | + format!("{}/../../.cache/heaac-fixtures", env!("CARGO_MANIFEST_DIR")) | |
| 3621 | + }); | |
| 3622 | + let failing = PathBuf::from(&dir).join("heaac_44100_48k.m4a"); | |
| 3623 | + if failing.exists() { | |
| 3624 | + per_au_low_band_probe("synthetic 44100 48k (failing)", &failing, 0); | |
| 3625 | + } else { | |
| 3626 | + eprintln!("SKIP synthetic 44100 48k: {} absent", failing.display()); | |
| 3627 | + } | |
| 3628 | +} | |
| 3629 | + | |
| 3630 | +/// Coordinator follow-up: prints what `parse_audio_specific_config` actually | |
| 3631 | +/// returns for a candidate's real ASC bytes (object/rate/sf_index/ | |
| 3632 | +/// sbr_present/extension_sample_rate), what `AacDecoder` reports back | |
| 3633 | +/// (`SbrSupport`, `output_sample_rate`) from those parsed fields, then | |
| 3634 | +/// re-decodes the SAME AU list through a HAND-BUILT explicit | |
| 3635 | +/// `AudioSpecificConfig` (object_type=5, known core/ext rates) instead of | |
| 3636 | +/// the parsed one -- if that jumps the correlations to ~1, the parsed | |
| 3637 | +/// config (or something object_type-conditioned at runtime) is the defect, | |
| 3638 | +/// not the SBR DSP chain itself. Also re-runs `full_chain_low_band_matches_ | |
| 3639 | +/// own_core`'s FMJ probe with `WIDE_LAG_MAX` raised to 100_000. | |
| 3640 | +#[test] | |
| 3641 | +fn probe_explicit_config_and_wide_lag() { | |
| 3642 | + if !have_ffmpeg() { | |
| 3643 | + eprintln!("SKIP: ffmpeg not on PATH"); | |
| 3644 | + return; | |
| 3645 | + } | |
| 3646 | + fn decode_with(cfg: ec_aac::AudioSpecificConfig, aus: &[Vec<u8>]) -> (Vec<Vec<f32>>, u32) { | |
| 3647 | + let rate = match (cfg.sbr_present, cfg.ps_present) { | |
| 3648 | + (true, false) => cfg.extension_sample_rate.unwrap_or(cfg.sample_rate * 2), | |
| 3649 | + _ => cfg.sample_rate, | |
| 3650 | + }; | |
| 3651 | + let mut d = AacDecoder::with_config(cfg); | |
| 3652 | + let mut planes: Vec<Vec<f32>> = Vec::new(); | |
| 3653 | + for au in aus { | |
| 3654 | + let Ok(f) = d.decode(au, None) else { continue }; | |
| 3655 | + let ch = usize::from(f.channels); | |
| 3656 | + if ch == 0 { | |
| 3657 | + continue; | |
| 3658 | + } | |
| 3659 | + if planes.is_empty() { | |
| 3660 | + planes = vec![Vec::new(); ch]; | |
| 3661 | + } | |
| 3662 | + for (i, v) in f.samples.iter().enumerate() { | |
| 3663 | + planes[i % ch].push(*v); | |
| 3664 | + } | |
| 3665 | + if planes[0].len() >= MAX_SAMPLES { | |
| 3666 | + break; | |
| 3667 | + } | |
| 3668 | + } | |
| 3669 | + (planes, rate) | |
| 3670 | + } | |
| 3671 | + | |
| 3672 | + for (label, path, aac_stream, ffmpeg_stream, core_sf_index, core_rate, ext_rate, channels) in [ | |
| 3673 | + ( | |
| 3674 | + "FMJ", | |
| 3675 | + format!( | |
| 3676 | + "{}/Downloads/reelp/reelp.mkv", | |
| 3677 | + std::env::var("HOME").unwrap_or_default() | |
| 3678 | + ), | |
| 3679 | + 1usize, | |
| 3680 | + 3usize, | |
| 3681 | + 6u8, | |
| 3682 | + 24_000u32, | |
| 3683 | + 48_000u32, | |
| 3684 | + 2u16, | |
| 3685 | + ), | |
| 3686 | + ( | |
| 3687 | + "heaac_44100_48k", | |
| 3688 | + format!( | |
| 3689 | + "{}/../../.cache/heaac-fixtures/heaac_44100_48k.m4a", | |
| 3690 | + env!("CARGO_MANIFEST_DIR") | |
| 3691 | + ), | |
| 3692 | + 0, | |
| 3693 | + 0, | |
| 3694 | + 4, | |
| 3695 | + 44_100, | |
| 3696 | + 0, // filled below from parsed cfg's own extension_sample_rate | |
| 3697 | + 2, | |
| 3698 | + ), | |
| 3699 | + ] { | |
| 3700 | + let path = PathBuf::from(path); | |
| 3701 | + if !path.exists() { | |
| 3702 | + eprintln!("SKIP {label}: {} absent", path.display()); | |
| 3703 | + continue; | |
| 3704 | + } | |
| 3705 | + let Some((asc, aus)) = extract_aac_track(&path, aac_stream) else { | |
| 3706 | + eprintln!("SKIP {label}: extract failed"); | |
| 3707 | + continue; | |
| 3708 | + }; | |
| 3709 | + let parsed = parse_audio_specific_config(&asc); | |
| 3710 | + println!("{label}: asc={asc:02x?} parsed={parsed:?}"); | |
| 3711 | + let Ok(parsed_cfg) = parsed else { | |
| 3712 | + eprintln!("SKIP {label}: parse failed"); | |
| 3713 | + continue; | |
| 3714 | + }; | |
| 3715 | + let d = AacDecoder::with_config(parsed_cfg.clone()); | |
| 3716 | + println!( | |
| 3717 | + " runtime: sbr_support={:?} output_sample_rate={:?}", | |
| 3718 | + d.sbr_support(), | |
| 3719 | + d.output_sample_rate() | |
| 3720 | + ); | |
| 3721 | + | |
| 3722 | + // Hand-built EXPLICIT config: object_type=5 (SBR), known core/ext | |
| 3723 | + // rates, sbr_present forced true regardless of what the real ASC | |
| 3724 | + // parsed to. | |
| 3725 | + let ext = if ext_rate == 0 { | |
| 3726 | + parsed_cfg.extension_sample_rate.unwrap_or(core_rate * 2) | |
| 3727 | + } else { | |
| 3728 | + ext_rate | |
| 3729 | + }; | |
| 3730 | + let hand_cfg = ec_aac::AudioSpecificConfig { | |
| 3731 | + object_type: ec_aac::AOT_SBR, | |
| 3732 | + sample_rate: core_rate, | |
| 3733 | + sf_index: core_sf_index, | |
| 3734 | + channels, | |
| 3735 | + channel_config: channels as u8, | |
| 3736 | + sbr_present: true, | |
| 3737 | + ps_present: false, | |
| 3738 | + extension_sample_rate: Some(ext), | |
| 3739 | + }; | |
| 3740 | + let (hand_planes, hand_rate) = decode_with(hand_cfg.clone(), &aus); | |
| 3741 | + if hand_planes.is_empty() { | |
| 3742 | + eprintln!(" hand-built config: no channels decoded"); | |
| 3743 | + continue; | |
| 3744 | + } | |
| 3745 | + // Own-core self-consistency (upsample+lowpass convention). | |
| 3746 | + let core_cfg = ec_aac::AudioSpecificConfig { | |
| 3747 | + sbr_present: false, | |
| 3748 | + ps_present: false, | |
| 3749 | + extension_sample_rate: None, | |
| 3750 | + ..hand_cfg.clone() | |
| 3751 | + }; | |
| 3752 | + let (core_planes, _) = decode_with(core_cfg, &aus); | |
| 3753 | + if !core_planes.is_empty() { | |
| 3754 | + let up: Vec<f32> = core_planes[0].iter().flat_map(|&s| [s, s]).collect(); | |
| 3755 | + let cutoff = f64::from(core_rate) * 0.4; | |
| 3756 | + let ol = lowpass(&hand_planes[0], hand_rate, cutoff); | |
| 3757 | + let ul = lowpass(&up, hand_rate, cutoff); | |
| 3758 | + let (lag, corr) = best_lag_correlation_wide(&ol, &ul, 100_000); | |
| 3759 | + println!( | |
| 3760 | + " hand-built explicit config: low-band vs own core: lag={lag} corr={corr:.6}" | |
| 3761 | + ); | |
| 3762 | + } | |
| 3763 | + let theirs = ffmpeg_decode(&path, ffmpeg_stream, hand_planes.len()); | |
| 3764 | + if let (Some(o), Some(t)) = (hand_planes.first(), theirs.first()) { | |
| 3765 | + let (lag, corr) = best_lag_correlation_wide(o, t, 100_000); | |
| 3766 | + println!(" hand-built explicit config: full-band vs reference: lag={lag} corr={corr:.6}"); | |
| 3767 | + } | |
| 3768 | + } | |
| 3769 | + | |
| 3770 | + // Probe B: FMJ's own (parsed) full-chain-vs-own-core lag search, widened | |
| 3771 | + // to 100_000 (5x the existing WIDE_LAG_MAX=20_000 this file's other | |
| 3772 | + // tests use) -- the coarse search in `full_chain_low_band_matches_own_ | |
| 3773 | + // core` landed right at that bound's edge (20005), so this checks | |
| 3774 | + // whether FMJ's true delay simply lies further out. | |
| 3775 | + let home = std::env::var("HOME").unwrap_or_default(); | |
| 3776 | + let fmj = PathBuf::from(format!( | |
| 3777 | + "{home}/Downloads/reelp/reelp.mkv" | |
| 3778 | + )); | |
| 3779 | + if fmj.exists() { | |
| 3780 | + if let (Some((core, core_rate)), Some((full, sbr, full_rate))) = ( | |
| 3781 | + our_decode_core_only(&fmj, 1), | |
| 3782 | + our_decode(&fmj, 1), | |
| 3783 | + ) { | |
| 3784 | + if sbr == ec_aac::SbrSupport::V1 { | |
| 3785 | + let up: Vec<f32> = core[0].iter().flat_map(|&s| [s, s]).collect(); | |
| 3786 | + let cutoff = f64::from(core_rate) * 0.4; | |
| 3787 | + let ol = lowpass(&full[0], full_rate, cutoff); | |
| 3788 | + let ul = lowpass(&up, full_rate, cutoff); | |
| 3789 | + let (lag, corr) = best_lag_correlation_wide(&ol, &ul, 100_000); | |
| 3790 | + println!("FMJ WIDE_LAG_MAX=100_000: ch0 best lag={lag} corr={corr:.6}"); | |
| 3791 | + } | |
| 3792 | + } | |
| 3793 | + } | |
| 3794 | +} | |
| 3795 | + | |
| 3435 | 3796 | /// One channel of `path`'s given absolute stream, isolated with an ffmpeg |
| 3436 | 3797 | /// `pan` filter rather than raw de-interleaving -- the charter's own witness |
| 3437 | 3798 | /// rule (see `ffmpeg_decode`'s doc and the ledger's "never use -ac 1", which |