From a62782ec30b80caed4890f70f8f4b4ae3da0a78a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 11:43:41 +0300 Subject: [PATCH] ec-aac test: lag-search window skipped digital silence; own-core reference interpolates linearly. Root cause (measured with pinned ffmpeg, f32le): decoder timing is right, instruments were not. (1) ps_06 is 4x53 AUs; AUs 53-55 of ffmpeg output are digital silence (rms 0 over [108544,112640)). With the now-correct 2048/AU length (434176/ch, = ffmpeg) every lag search anchored at len/4 = 108544 lands exactly there, so corr is noise and the winner sits at the +/-100000 / +/-20000 bound (sbr_real_library_matches_reference, per_au_low_band_divergence, sbr_header_feature_table, synthetic_heaac_matrix ps_06). On main the shorter output moved the window to loud audio, hence the earlier pass. window_start() (single source for 4 copies) advances to the first 4x1024 block louder than 1e-4 of mean power in both signals, unchanged when the base block is loud. (2) full_chain_low_band ps_04 corr 0.964 at lag -961: nearest-neighbor 2x of the core carries a half-sample error; hold-resampling ffmpeg's own ps_04 output gives at best 0.9578 for any decoder, linear interpolation 0.9936 (cm_48_2: 0.9985 -> 0.9999, matches observed 0.9987). upsample_2x is linear; threshold stays 0.99. Also ch0 of a PS stream is not the mono core (ffmpeg ps_06 corr(L,L+R) 0.937 over 10 s), so the comparison stops where the channels first differ (mono_prefix). Decoder evidence: ours vs ffmpeg ps_04 lag 0 corr 1.000000, 52/53 windows exactly 1.0 (1 silent), len 217088 = 106*2048, pre-header AUs included; ps_06 headers only from AU 106. cargo check -p ec-aac --all-targets clean; tests not run --- .../ec-aac/tests/sbr_real_library/chunk_a.rs | 9 ++- .../ec-aac/tests/sbr_real_library/helpers.rs | 58 ++++++++++++------- 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/crates/ec-aac/tests/sbr_real_library/chunk_a.rs b/crates/ec-aac/tests/sbr_real_library/chunk_a.rs index 48bad545..8f8978af 100644 --- a/crates/ec-aac/tests/sbr_real_library/chunk_a.rs +++ b/crates/ec-aac/tests/sbr_real_library/chunk_a.rs @@ -164,9 +164,14 @@ pub(super) fn full_chain_low_band_matches_own_core() { // sample-for-sample with the full-chain output's rate -- // crude, but a low-pass well below the core Nyquist erases the // stairstep artifacts this introduces. - let up: Vec = core[ch].iter().flat_map(|&s| [s, s]).collect(); + let up = upsample_2x(&core[ch]); let cutoff = f64::from(core_rate) * 0.4; - let ol = lowpass(&full[ch], full_rate, cutoff); + let keep = if full.len() > core.len() { + mono_prefix(&full) + } else { + full[ch].len() + }; + let ol = lowpass(&full[ch][..keep], full_rate, cutoff); let ul = lowpass(&up, full_rate, cutoff); // Escalating search: narrow (`WIDE_LAG_MAX`=20_000, the file's // original bound) first, widen to `SEARCH_LAG_MAX` only if that diff --git a/crates/ec-aac/tests/sbr_real_library/helpers.rs b/crates/ec-aac/tests/sbr_real_library/helpers.rs index e8b0f15d..4c50a910 100644 --- a/crates/ec-aac/tests/sbr_real_library/helpers.rs +++ b/crates/ec-aac/tests/sbr_real_library/helpers.rs @@ -231,11 +231,7 @@ pub(super) fn correlation(a: &[f32], b: &[f32]) -> f64 { /// case the coarse slice picked a noisy lag) gives the reported number. pub(super) fn best_lag_correlation_ex(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64, f64, bool) { const COARSE: usize = 4_096; - let start = ours - .len() - .min(theirs.len()) - .saturating_sub(WINDOW) - .min(ours.len() / 4); + let start = window_start(ours, theirs); let slice_at = |lag: i64, len: usize| -> Option<(usize, usize)> { let (oa, ob) = if lag >= 0 { (start, start + lag as usize) @@ -365,11 +361,7 @@ pub(super) fn best_lag_correlation_wide(ours: &[f32], theirs: &[f32], lag_max: i /// refinement it replaces, so the resolution is simply not worth trading. pub(super) fn exhaustive_lag_correlation(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64, f64, bool) { const COARSE: usize = 4_096; - let start = ours - .len() - .min(theirs.len()) - .saturating_sub(WINDOW) - .min(ours.len() / 4); + let start = window_start(ours, theirs); let slice_at = |lag: i64, len: usize| -> Option<(usize, usize)> { let (oa, ob) = if lag >= 0 { (start, start + lag as usize) @@ -421,11 +413,7 @@ pub(super) fn robust_lag_topk( k: usize, ) -> (i64, f64, bool) { const COARSE: usize = 4_096; - let start = ours - .len() - .min(theirs.len()) - .saturating_sub(WINDOW) - .min(ours.len() / 4); + let start = window_start(ours, theirs); let slice_at = |lag: i64, len: usize| -> Option<(usize, usize)> { let (oa, ob) = if lag >= 0 { (start, start + lag as usize) @@ -477,6 +465,40 @@ pub(super) fn robust_lag_topk( best } +pub(super) fn window_start(ours: &[f32], theirs: &[f32]) -> usize { + const CHUNK: usize = 1_024; + const CHUNKS: usize = 4; + let end = ours.len().min(theirs.len()); + let base = end.saturating_sub(WINDOW).min(ours.len() / 4); + let power = |s: &[f32]| { + s.iter().map(|v| f64::from(*v) * f64::from(*v)).sum::() / s.len().max(1) as f64 + }; + let floor = 1e-4 * power(&ours[base..end]).min(power(&theirs[base..end])); + let loud = |p: usize| { + p + CHUNK * CHUNKS <= end + && (p..p + CHUNK * CHUNKS) + .step_by(CHUNK) + .all(|a| power(&ours[a..a + CHUNK]) > floor && power(&theirs[a..a + CHUNK]) > floor) + }; + (base..end).step_by(CHUNK).find(|&p| loud(p)).unwrap_or(base) +} + +pub(super) fn upsample_2x(core: &[f32]) -> Vec { + let mut out = Vec::with_capacity(core.len() * 2); + for (i, &s) in core.iter().enumerate() { + out.push(s); + out.push((s + core.get(i + 1).copied().unwrap_or(s)) * 0.5); + } + out +} + +pub(super) fn mono_prefix(planes: &[Vec]) -> usize { + let first = &planes[0]; + planes.get(1).map_or(first.len(), |second| { + first.iter().zip(second).position(|(a, b)| a != b).unwrap_or(first.len()) + }) +} + /// Signed correlation of `ours` against `theirs` at one fixed `lag`, full /// `WINDOW`-sized, anchored at the SAME `start` offset `best_lag_correlation` /// uses internally -- used to test a hypothesized sign inversion by reusing @@ -486,11 +508,7 @@ pub(super) fn robust_lag_topk( /// match `best_lag_correlation`'s `start`, not sample 0 -- the two windows /// otherwise cover different seconds of audio and are not comparable. pub(super) fn correlation_at_lag(ours: &[f32], theirs: &[f32], lag: i64) -> f64 { - let start = ours - .len() - .min(theirs.len()) - .saturating_sub(WINDOW) - .min(ours.len() / 4); + let start = window_start(ours, theirs); let (oa, ob) = if lag >= 0 { (start, start + lag as usize) } else {