diff --git a/crates/ec-aac/tests/sbr_real_library.rs b/crates/ec-aac/tests/sbr_real_library.rs index 1baea572..198e8d5d 100644 --- a/crates/ec-aac/tests/sbr_real_library.rs +++ b/crates/ec-aac/tests/sbr_real_library.rs @@ -173,12 +173,38 @@ fn our_decode( Some((planes, sbr, rate)) } -/// Search window and lag bound: keeps the lag search and the correlation sum -/// both bounded, per the charter -- an unbounded version over a whole film's -/// worth of samples was too slow to be an every-run test. -const LAG_MAX: usize = 4_000; +/// Search window bound: keeps the correlation sum bounded, per the charter +/// -- an unbounded version over a whole film's worth of samples was too slow +/// to be an every-run test. const WINDOW: usize = 200_000; +/// Wide lag search bound for the specific gate proven to need it +/// (`sbr_real_library_matches_reference`, ours-vs-reference across a real +/// container's remux/priming delay). Measured (round-54 wide-lag probe, +/// worktree sbr-per-au) FMJ's true full-chain delay vs the reference is +/// ~22,913 samples @48kHz -- that gate's old `LAG_MAX=4_000` search clipped +/// at its own edge and silently reported the edge's noise as the answer for +/// 50+ rounds (class: "an alignment instrument whose result sits at its own +/// search edge is invalid"). 100_000 covers that with 4x headroom and is +/// already the bound round-53's own `WIDE_LAG_MAX=100_000` probe used to +/// find it. NOT used as the default everywhere: a wider bound also widens +/// the coarse pass's exposure to spurious short-slice noise peaks, so +/// call sites whose true delay is known-small (container/decoder priming, +/// ours-vs-own-core self-consistency) keep their original, narrower bound +/// -- round-54 first tried blanket-widening every call site to this value +/// and it flipped `full_chain_low_band_matches_own_core` and the LC control +/// row of `synthetic_heaac_matrix` from a correct small-lag answer to a +/// wrong, noise-driven distant one, still comfortably inside the bound +/// (not `at_edge`) but wrong regardless. +const SEARCH_LAG_MAX: i64 = 100_000; +/// Original bound for the plain `best_lag_correlation` wrapper, used by the +/// file's other (ours-vs-reference small-priming-delay, or ours-vs-ours) +/// callers that were never shown to need more. +const PLAIN_LAG_MAX: i64 = 4_000; +/// Odd stride `best_lag_correlation_ex`'s coarse pass steps by, so it +/// doesn't alias against frame-length periodicity. +const SEARCH_STRIDE: i64 = 11; + fn dot(a: &[f32], b: &[f32]) -> f64 { a.iter() .zip(b) @@ -201,14 +227,19 @@ fn correlation(a: &[f32], b: &[f32]) -> f64 { if den == 0.0 { 0.0 } else { num / den } } -/// Best correlation over a bounded lag search. Two passes keep the whole -/// search O(reasonable) on a film-length file: a coarse pass over every lag -/// but a short slice finds roughly where the alignment is, then one full-size -/// correlation at that lag (and its immediate neighbours, in case the coarse -/// slice picked a noisy lag) gives the reported number. The 8001-lag charter -/// bound times the full `WINDOW` in one pass, tried first, took minutes on -/// this box -- this is that ceiling brought back to milliseconds. -fn best_lag_correlation(ours: &[f32], theirs: &[f32]) -> (i64, f64) { +/// Best correlation over a bounded lag search, PLUS whether the winning lag +/// sits at the search's own edge -- the single instrument every asserting +/// test in this file now routes through (class fix, round-54: a search that +/// silently clips at its bound and reports the edge's noise as "the +/// answer" is not an instrument, it's a lie). Two passes keep the whole +/// search O(reasonable) even at `SEARCH_LAG_MAX`-sized bounds on a +/// film-length file: an odd-`SEARCH_STRIDE` coarse pass over a short slice +/// finds roughly where the alignment is (the stride, not full 1-by-1 +/// stepping, is what keeps a `lag_max` in the tens of thousands cheap, and +/// also avoids aliasing against frame-length periodicity), then one +/// full-`WINDOW` correlation at that lag (and its immediate neighbours, in +/// case the coarse slice picked a noisy lag) gives the reported number. +fn best_lag_correlation_ex(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64, f64, bool) { const COARSE: usize = 4_096; let start = ours .len() @@ -244,18 +275,20 @@ fn best_lag_correlation(ours: &[f32], theirs: &[f32]) -> (i64, f64) { // positive one from a 20x-shorter slice. Carry the signed value through // so callers can see the sign, not just |corr|. let mut coarse_best = (0i64, -1.0f64, 0.0f64); - for lag in -(LAG_MAX as i64)..=(LAG_MAX as i64) { - let Some((oa, ob)) = slice_at(lag, COARSE) else { - continue; - }; - let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]); - if c.abs() > coarse_best.1 { - coarse_best = (lag, c.abs(), c); + let mut lag = -lag_max; + while lag <= lag_max { + if let Some((oa, ob)) = slice_at(lag, COARSE) { + let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]); + if c.abs() > coarse_best.1 { + coarse_best = (lag, c.abs(), c); + } } + lag += SEARCH_STRIDE; } + let at_edge = coarse_best.0.abs() >= lag_max - SEARCH_STRIDE; let mut best = (coarse_best.0, -1.0f64, 0.0f64); let mut found = false; - for lag in (coarse_best.0 - 8)..=(coarse_best.0 + 8) { + for lag in (coarse_best.0 - 2 * SEARCH_STRIDE)..=(coarse_best.0 + 2 * SEARCH_STRIDE) { let Some((oa, ob)) = slice_at(lag, WINDOW) else { continue; }; @@ -272,7 +305,25 @@ fn best_lag_correlation(ours: &[f32], theirs: &[f32]) -> (i64, f64) { if !found { best = coarse_best; } - (best.0, best.2) + (best.0, best.2, at_edge) +} + +/// Thin wrapper over `best_lag_correlation_ex` at `PLAIN_LAG_MAX`, for the +/// many non-asserting call sites that only want `(lag, corr)` and whose true +/// delay was never shown to exceed the original 4_000 bound. The one gate +/// proven to need the wide bound (`sbr_real_library_matches_reference`) +/// calls `_ex` directly with `SEARCH_LAG_MAX` instead of going through this +/// wrapper. +fn best_lag_correlation(ours: &[f32], theirs: &[f32]) -> (i64, f64) { + let (lag, corr, _) = best_lag_correlation_ex(ours, theirs, PLAIN_LAG_MAX); + (lag, corr) +} + +/// Thin wrapper over `best_lag_correlation_ex` at a caller-supplied bound, +/// for non-asserting call sites (asserting ones use `_ex` directly). +fn best_lag_correlation_wide(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64, f64) { + let (lag, corr, _) = best_lag_correlation_ex(ours, theirs, lag_max); + (lag, corr) } /// Signed correlation of `ours` against `theirs` at one fixed `lag`, full @@ -2097,14 +2148,36 @@ fn full_chain_low_band_matches_own_core() { let cutoff = f64::from(core_rate) * 0.4; let ol = lowpass(&full[ch], full_rate, cutoff); let ul = lowpass(&up, full_rate, cutoff); - // Wide search, not the plain `best_lag_correlation`'s - // `LAG_MAX=4000` -- the QMF-chain delay this measures is not a - // fixed constant across files/rates (measured -576 on reeln - // vs the stale -897 this test used to assert against), so the - // lag itself is part of what's measured here, not assumed. + // Escalating search: narrow (`WIDE_LAG_MAX`=20_000, the file's + // original bound) first, widen to `SEARCH_LAG_MAX` only if that + // narrow search hit its own edge (`at_edge`). A single shared + // wide bound was tried first (round-54) and, unconditionally + // widening this comparison, flipped reeln from its correct + // small-lag answer (-576, corr 0.9967) to a wrong, noise-driven + // distant one (-912, corr -0.295) -- a genuinely low-quality + // low-passed signal's coarse correlation surface has enough + // stray peaks over a 100_000-wide range to beat a real but + // modest true peak. Escalating only when the narrow bound + // proves insufficient (FMJ's own core-vs-full delay lands AT + // the 20_000 edge, corr 0.217393 there, the same edge-noise + // class the reference-vs-ours gate had) gets both files right + // without exposing the well-behaved one to the wider bound's + // noise floor. const WIDE_LAG_MAX: i64 = 20_000; - let (lag, corr) = best_lag_correlation_wide(&ol, &ul, WIDE_LAG_MAX); + let (mut lag, mut corr, mut at_edge) = best_lag_correlation_ex(&ol, &ul, WIDE_LAG_MAX); + let mut bound = WIDE_LAG_MAX; + if at_edge { + bound = SEARCH_LAG_MAX; + (lag, corr, at_edge) = best_lag_correlation_ex(&ol, &ul, SEARCH_LAG_MAX); + } println!(" ch{ch}: measured best lag {lag}, corr {corr:.6}"); + assert!( + !at_edge, + "{} ch{ch}: winning lag {lag} sits at the +/-{bound} \ + search bound -- the true alignment is outside the search, \ + corr {corr:.6} is noise, not a measurement", + c.path.display() + ); assert!( corr >= 0.99, "{} ch{ch}: full-chain low band vs our own core-only decode \ @@ -2117,60 +2190,6 @@ fn full_chain_low_band_matches_own_core() { } } -/// Follow-up to round-52: `best_lag_correlation`'s `LAG_MAX=4000` bound was -/// landing HE-AAC winning lags right at its own edge (-3634..-3882) on the -/// synthetic matrix -- this is the same shape `wide_lag_search_full_chain` -/// diagnosed on FMJ. Mirrors that diagnostic's odd-stride coarse pass, but -/// over a caller-supplied `lag_max` and followed by the same full-`WINDOW` -/// refine `best_lag_correlation` does, so its result is directly comparable -/// (not just a coarse `|corr|` estimate). -fn best_lag_correlation_wide(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64, f64) { - const COARSE: usize = 4096; - let start = ours - .len() - .min(theirs.len()) - .saturating_sub(WINDOW) - .min(ours.len() / 4); - let slice_at = |lag: i64, len: usize| -> Option<(usize, usize)> { - let (oa, ob) = if lag >= 0 { - (start, start + lag as usize) - } else { - (start + (-lag) as usize, start) - }; - if oa + len > ours.len() || ob + len > theirs.len() { - None - } else { - Some((oa, ob)) - } - }; - let mut coarse_best = (0i64, -1.0f64); - let mut lag = -lag_max; - while lag <= lag_max { - if let Some((oa, ob)) = slice_at(lag, COARSE) { - let c = correlation(&ours[oa..oa + COARSE], &theirs[ob..ob + COARSE]).abs(); - if c > coarse_best.1 { - coarse_best = (lag, c); - } - } - lag += 11; // odd stride so it doesn't alias against frame-length periodicity - } - let mut best = (coarse_best.0, -1.0f64, 0.0f64); - for lag in (coarse_best.0 - 16)..=(coarse_best.0 + 16) { - let Some((oa, ob)) = slice_at(lag, WINDOW) else { - continue; - }; - let n = WINDOW.min(ours.len() - oa).min(theirs.len() - ob); - if n < 1024 { - continue; - } - let c = correlation(&ours[oa..oa + n], &theirs[ob..ob + n]); - if c.abs() > best.1 { - best = (lag, c.abs(), c); - } - } - (best.0, best.2) -} - /// Round-52: a controlled fixture matrix -- unlike reeln/FMJ (one real /// file each, whose sample rate, bitrate and content all differ at once), /// every row here decodes the SAME synthesized source (three summed sine @@ -2448,6 +2467,7 @@ fn sbr_real_library_matches_reference() { let mut checked = 0; let mut worst_full = 1.0f64; let mut worst_low = 1.0f64; + let mut any_at_edge: Option = None; for c in &list { let Some((ours, sbr, rate)) = our_decode(&c.path, c.aac_stream) else { eprintln!("SKIP {}: could not decode", c.path.display()); @@ -2518,7 +2538,15 @@ fn sbr_real_library_matches_reference() { rms(t), &t[..10.min(t.len())], ); - let (lag, full) = best_lag_correlation(o, t); + let (lag, full, full_at_edge) = best_lag_correlation_ex(o, t, SEARCH_LAG_MAX); + if full_at_edge { + any_at_edge.get_or_insert_with(|| { + format!( + "{} ch{ch} full-band: winning lag {lag} at +/-{SEARCH_LAG_MAX} search edge", + c.path.display() + ) + }); + } // Re-slice both signals at the winning lag before splitting into // bands, so the band correlations are aligned too. let (oa, ob) = if lag >= 0 { @@ -2546,7 +2574,16 @@ fn sbr_real_library_matches_reference() { let low = { let ol_full = lowpass(o, rate, c.crossover_hz); let tl_full = lowpass(t, rate, c.crossover_hz); - let (low_lag, low_corr) = best_lag_correlation(&ol_full, &tl_full); + let (low_lag, low_corr, low_at_edge) = + best_lag_correlation_ex(&ol_full, &tl_full, SEARCH_LAG_MAX); + if low_at_edge { + any_at_edge.get_or_insert_with(|| { + format!( + "{} ch{ch} below-crossover: winning lag {low_lag} at +/-{SEARCH_LAG_MAX} search edge", + c.path.display() + ) + }); + } if low_lag != lag { eprintln!( " 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() { eprintln!("SKIP: no plain-SBR real files decoded"); return; } + assert!( + any_at_edge.is_none(), + "lag search hit its bound: {} -- the reported correlations are noise \ + at the search edge, not a measurement", + any_at_edge.unwrap_or_default() + ); assert!( worst_full >= 0.999, "worst full-band correlation {worst_full:.6} < 0.999 bar" @@ -3432,6 +3475,324 @@ fn sbr_header_feature_table() { } } +/// Round (sbr-per-au): per-AU localization. Decodes the same AU list through +/// (a) the core-only decoder and (b) the full SBR chain, tracks each AU's +/// sample range in both output streams as it goes (so a per-AU decode +/// failure on either side does not desync the mapping), aligns the two +/// streams ONCE in the core's own native rate domain (same decimate +/// convention `synthetic_heaac_matrix`'s `ours_v_core` row already trusts), +/// then re-uses that single lag for every AU's own low-band correlation. +/// Prints the first 5 AUs below 0.9 with that AU's SBR side-info (source/ +/// envelope count/freq_res/kx/k2) so a per-frame trigger (e.g. `hold` +/// frames, a specific `freq_res`) shows up directly instead of needing a +/// second pass. If the first divergent AU is 0, that is reported explicitly +/// -- divergence from the very first AU means a per-stream setup mismatch, +/// not a per-frame trigger. +fn per_au_low_band_probe(label: &str, path: &Path, aac_stream: usize) { + let Some((asc, aus)) = extract_aac_track(path, aac_stream) else { + eprintln!("SKIP {label}: extract_aac_track failed"); + return; + }; + let Ok(full_cfg) = parse_audio_specific_config(&asc) else { + eprintln!("SKIP {label}: asc parse failed"); + return; + }; + if !full_cfg.sbr_present { + eprintln!("SKIP {label}: no SBR"); + return; + } + let mut core_cfg = full_cfg.clone(); + core_cfg.sbr_present = false; + core_cfg.ps_present = false; + core_cfg.extension_sample_rate = None; + let core_rate = core_cfg.sample_rate; + let mut core_dec = AacDecoder::with_config(core_cfg); + let mut full_dec = AacDecoder::with_config(full_cfg); + let full_rate = full_dec.output_sample_rate().unwrap_or(core_rate * 2); + + struct AuInfo { + core_range: (usize, usize), + side: Option, + } + let mut core_ch0: Vec = Vec::new(); + let mut full_ch0: Vec = Vec::new(); + let mut aus_info: Vec = Vec::new(); + for au in aus.iter().take(1_500) { + let side_before = ec_aac::sbr_sideinfo_log().len(); + let cs = core_ch0.len(); + if let Ok(f) = core_dec.decode(au, None) { + let ch = usize::from(f.channels).max(1); + core_ch0.extend(f.samples.iter().step_by(ch).copied()); + } + let ce = core_ch0.len(); + if let Ok(f) = full_dec.decode(au, None) { + let ch = usize::from(f.channels).max(1); + full_ch0.extend(f.samples.iter().step_by(ch).copied()); + } + let side = ec_aac::sbr_sideinfo_log()[side_before..] + .iter() + .find(|r| r.ch == 0) + .cloned(); + aus_info.push(AuInfo { + core_range: (cs, ce), + side, + }); + if full_ch0.len() > MAX_SAMPLES { + break; + } + } + if core_ch0.is_empty() || full_ch0.is_empty() { + eprintln!("SKIP {label}: empty decode (core {} full {})", core_ch0.len(), full_ch0.len()); + return; + } + + // Same convention `full_chain_low_band_matches_own_core` already + // validated (0.9967 on reeln): naive nearest-neighbor 2x-upsample + // the core into the full-rate domain, low-pass both well under the core + // Nyquist. The decimate-into-core-rate convention `synthetic_heaac_matrix` + // uses for its own `ours_v_core` diagnostic column was tried first here + // and reads badly (~-0.19) even on reeln, which is known-good -- + // that column is evidently not a reliable self-consistency probe on its + // own, so this per-AU tool anchors on the convention independently + // proven correct instead. + let up: Vec = core_ch0.iter().flat_map(|&s| [s, s]).collect(); + let cutoff = f64::from(core_rate) * 0.4; + let ol = lowpass(&full_ch0, full_rate, cutoff); + let ul = lowpass(&up, full_rate, cutoff); + const WIDE_LAG_MAX: i64 = 20_000; + let (lag, align_corr) = best_lag_correlation_wide(&ol, &ul, WIDE_LAG_MAX); + println!( + "{label}: core={core_rate}Hz full={full_rate}Hz global align (upsampled full-rate domain) lag={lag} corr={align_corr:.6}" + ); + + let mut shown = 0usize; + let mut first_divergent: Option = None; + for (i, info) in aus_info.iter().enumerate() { + let (cs, ce) = info.core_range; + if ce <= cs { + continue; + } + // core_range is in core-domain sample counts; the upsampled/lowpassed + // arrays are at full rate, so the AU's window there is 2x as wide. + let fs = cs * 2; + let fe = ce * 2; + let ds = match usize::try_from(fs as i64 + lag) { + Ok(v) => v, + Err(_) => continue, + }; + let de = ds + (fe - fs); + if de > ul.len() || fe > ol.len() { + break; + } + let c = correlation(&ol[fs..fe], &ul[ds..de]); + if c < 0.9 { + if first_divergent.is_none() { + first_divergent = Some(i); + } + if shown < 5 { + println!(" AU {i}: low-band corr={c:.6} side={:?}", info.side); + shown += 1; + } + } + } + match first_divergent { + Some(0) => println!( + "{label}: divergence starts at AU 0 -- per-STREAM setup mismatch, not a per-frame trigger" + ), + Some(i) => println!("{label}: first divergent AU = {i}"), + None => println!("{label}: no AU below 0.9 corr in the probed window"), + } +} + +#[test] +fn per_au_low_band_divergence() { + if !have_ffmpeg() { + eprintln!("SKIP: ffmpeg not on PATH"); + return; + } + unsafe { + std::env::set_var("EC_AAC_SBR_SIDEINFO_DEBUG", "1"); + } + for c in &candidates() { + per_au_low_band_probe(&c.path.display().to_string(), &c.path, c.aac_stream); + } + let dir = std::env::var("EC_AAC_HEAAC_FIXTURES").unwrap_or_else(|_| { + format!("{}/../../.cache/heaac-fixtures", env!("CARGO_MANIFEST_DIR")) + }); + let failing = PathBuf::from(&dir).join("heaac_44100_48k.m4a"); + if failing.exists() { + per_au_low_band_probe("synthetic 44100 48k (failing)", &failing, 0); + } else { + eprintln!("SKIP synthetic 44100 48k: {} absent", failing.display()); + } +} + +/// Coordinator follow-up: prints what `parse_audio_specific_config` actually +/// returns for a candidate's real ASC bytes (object/rate/sf_index/ +/// sbr_present/extension_sample_rate), what `AacDecoder` reports back +/// (`SbrSupport`, `output_sample_rate`) from those parsed fields, then +/// re-decodes the SAME AU list through a HAND-BUILT explicit +/// `AudioSpecificConfig` (object_type=5, known core/ext rates) instead of +/// the parsed one -- if that jumps the correlations to ~1, the parsed +/// config (or something object_type-conditioned at runtime) is the defect, +/// not the SBR DSP chain itself. Also re-runs `full_chain_low_band_matches_ +/// own_core`'s FMJ probe with `WIDE_LAG_MAX` raised to 100_000. +#[test] +fn probe_explicit_config_and_wide_lag() { + if !have_ffmpeg() { + eprintln!("SKIP: ffmpeg not on PATH"); + return; + } + fn decode_with(cfg: ec_aac::AudioSpecificConfig, aus: &[Vec]) -> (Vec>, u32) { + let rate = match (cfg.sbr_present, cfg.ps_present) { + (true, false) => cfg.extension_sample_rate.unwrap_or(cfg.sample_rate * 2), + _ => cfg.sample_rate, + }; + let mut d = AacDecoder::with_config(cfg); + let mut planes: Vec> = Vec::new(); + for au in aus { + let Ok(f) = d.decode(au, None) else { continue }; + let ch = usize::from(f.channels); + if ch == 0 { + continue; + } + if planes.is_empty() { + planes = vec![Vec::new(); ch]; + } + for (i, v) in f.samples.iter().enumerate() { + planes[i % ch].push(*v); + } + if planes[0].len() >= MAX_SAMPLES { + break; + } + } + (planes, rate) + } + + for (label, path, aac_stream, ffmpeg_stream, core_sf_index, core_rate, ext_rate, channels) in [ + ( + "FMJ", + format!( + "{}/Downloads/reelp/reelp.mkv", + std::env::var("HOME").unwrap_or_default() + ), + 1usize, + 3usize, + 6u8, + 24_000u32, + 48_000u32, + 2u16, + ), + ( + "heaac_44100_48k", + format!( + "{}/../../.cache/heaac-fixtures/heaac_44100_48k.m4a", + env!("CARGO_MANIFEST_DIR") + ), + 0, + 0, + 4, + 44_100, + 0, // filled below from parsed cfg's own extension_sample_rate + 2, + ), + ] { + let path = PathBuf::from(path); + if !path.exists() { + eprintln!("SKIP {label}: {} absent", path.display()); + continue; + } + let Some((asc, aus)) = extract_aac_track(&path, aac_stream) else { + eprintln!("SKIP {label}: extract failed"); + continue; + }; + let parsed = parse_audio_specific_config(&asc); + println!("{label}: asc={asc:02x?} parsed={parsed:?}"); + let Ok(parsed_cfg) = parsed else { + eprintln!("SKIP {label}: parse failed"); + continue; + }; + let d = AacDecoder::with_config(parsed_cfg.clone()); + println!( + " runtime: sbr_support={:?} output_sample_rate={:?}", + d.sbr_support(), + d.output_sample_rate() + ); + + // Hand-built EXPLICIT config: object_type=5 (SBR), known core/ext + // rates, sbr_present forced true regardless of what the real ASC + // parsed to. + let ext = if ext_rate == 0 { + parsed_cfg.extension_sample_rate.unwrap_or(core_rate * 2) + } else { + ext_rate + }; + let hand_cfg = ec_aac::AudioSpecificConfig { + object_type: ec_aac::AOT_SBR, + sample_rate: core_rate, + sf_index: core_sf_index, + channels, + channel_config: channels as u8, + sbr_present: true, + ps_present: false, + extension_sample_rate: Some(ext), + }; + let (hand_planes, hand_rate) = decode_with(hand_cfg.clone(), &aus); + if hand_planes.is_empty() { + eprintln!(" hand-built config: no channels decoded"); + continue; + } + // Own-core self-consistency (upsample+lowpass convention). + let core_cfg = ec_aac::AudioSpecificConfig { + sbr_present: false, + ps_present: false, + extension_sample_rate: None, + ..hand_cfg.clone() + }; + let (core_planes, _) = decode_with(core_cfg, &aus); + if !core_planes.is_empty() { + let up: Vec = core_planes[0].iter().flat_map(|&s| [s, s]).collect(); + let cutoff = f64::from(core_rate) * 0.4; + let ol = lowpass(&hand_planes[0], hand_rate, cutoff); + let ul = lowpass(&up, hand_rate, cutoff); + let (lag, corr) = best_lag_correlation_wide(&ol, &ul, 100_000); + println!( + " hand-built explicit config: low-band vs own core: lag={lag} corr={corr:.6}" + ); + } + let theirs = ffmpeg_decode(&path, ffmpeg_stream, hand_planes.len()); + if let (Some(o), Some(t)) = (hand_planes.first(), theirs.first()) { + let (lag, corr) = best_lag_correlation_wide(o, t, 100_000); + println!(" hand-built explicit config: full-band vs reference: lag={lag} corr={corr:.6}"); + } + } + + // Probe B: FMJ's own (parsed) full-chain-vs-own-core lag search, widened + // to 100_000 (5x the existing WIDE_LAG_MAX=20_000 this file's other + // tests use) -- the coarse search in `full_chain_low_band_matches_own_ + // core` landed right at that bound's edge (20005), so this checks + // whether FMJ's true delay simply lies further out. + let home = std::env::var("HOME").unwrap_or_default(); + let fmj = PathBuf::from(format!( + "{home}/Downloads/reelp/reelp.mkv" + )); + if fmj.exists() { + if let (Some((core, core_rate)), Some((full, sbr, full_rate))) = ( + our_decode_core_only(&fmj, 1), + our_decode(&fmj, 1), + ) { + if sbr == ec_aac::SbrSupport::V1 { + let up: Vec = core[0].iter().flat_map(|&s| [s, s]).collect(); + let cutoff = f64::from(core_rate) * 0.4; + let ol = lowpass(&full[0], full_rate, cutoff); + let ul = lowpass(&up, full_rate, cutoff); + let (lag, corr) = best_lag_correlation_wide(&ol, &ul, 100_000); + println!("FMJ WIDE_LAG_MAX=100_000: ch0 best lag={lag} corr={corr:.6}"); + } + } + } +} + /// One channel of `path`'s given absolute stream, isolated with an ffmpeg /// `pan` filter rather than raw de-interleaving -- the charter's own witness /// rule (see `ffmpeg_decode`'s doc and the ledger's "never use -ac 1", which