From 23f057e2d13593f68f2f4078ea11ce1a022f6113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 20 Aug 2026 03:34:20 +0300 Subject: [PATCH] fix(sbr-probe): tone_core's pulse/tns/gain flags sat after spectral data, not before tone_core() (scripts/aac-tables/sbrchirp.py) is the isolated-single-MDCT-line probe writer used by sbrpatchmap.py's patch-map fingerprinting; it desynced the reference decoder on every real-header probe ("channel element X.Y is not allocated" / "buffer exhausted before END"). Byte-accounted against sce_core's proven field order: the `pulse_data_present/tns_data_present/gain_control_ data_present` 3-bit field must precede spectral_data(), not follow it -- the misplaced write shifted every bit after it. Core-only round-trip is now byte-clean (0 errors) and the real reeln header/grid shape (kx=14/k2=43) decodes to nonzero PCM instead of 0. Also fixes sbr_hf.rs's `real_file_tables()` test fixture, whose stop_freq=3/ xover_band=2 guess was wrong (gave kx=16/k2=29); the real value read off the file's own bitstream is stop_freq=8/xover_band=0 (kx=14/k2=43) -- confirmed build_patches() on the corrected header exactly matches sbrpatchmap.py's independently-ported prediction: [(0,14,14),(3,28,11),(0,39,3),(13,42,1)]. Running the full three-header patch-map sweep against both the reference and our own decoder (adts_to_pcm) surfaced a new blocker, not fixed here: our own decoder's *measured* map disagrees with build_patches' own prediction (empty for source band 0, a constant band range for every other source band) even though build_patches itself is proven correct in isolation -- the runtime HF generation/envelope path or the probe's PCM analysis is unverified, not build_patches. Per the probe's own sanity rule this blocks trusting the reference's measured map until resolved. --- crates/ec-aac/examples/adts_to_pcm.rs | 4 +++- crates/ec-aac/src/sbr_hf.rs | 12 ++++++------ scripts/aac-tables/sbrchirp.py | 5 ++++- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/crates/ec-aac/examples/adts_to_pcm.rs b/crates/ec-aac/examples/adts_to_pcm.rs index acbb8461..b98811ed 100644 --- a/crates/ec-aac/examples/adts_to_pcm.rs +++ b/crates/ec-aac/examples/adts_to_pcm.rs @@ -5,7 +5,9 @@ use std::io::Write; fn main() { - let path = std::env::args().nth(1).expect("usage: adts_to_pcm "); + let path = std::env::args() + .nth(1) + .expect("usage: adts_to_pcm "); let data = std::fs::read(&path).expect("file readable"); // Plain `AacDecoder::new()` never enables SBR (it only turns on from an // explicit AudioSpecificConfig's `sbr_present`, per `with_config`) -- diff --git a/crates/ec-aac/src/sbr_hf.rs b/crates/ec-aac/src/sbr_hf.rs index 12f48f52..804ed2e7 100644 --- a/crates/ec-aac/src/sbr_hf.rs +++ b/crates/ec-aac/src/sbr_hf.rs @@ -354,12 +354,12 @@ mod tests { use crate::sbr_bands::freq_tables; fn real_file_tables() -> BandTables { - // The rig's own header params for ~/Music/reeln.mp4's SBR - // element (44100 Hz extension rate, core-Nyquist crossover ~5 kHz): - // freq_scale=2, alter_scale=1, noise_bands=2, start/stop/xover taken - // from the mid-range defaults `EXTRA1_DEFAULTS` documents when a - // stream omits bs_header_extra_1/2. - freq_tables(44100, 5, 3, 2, 1, 2, 2).expect("valid header for the real file's rate") + // The real file's actual SBR header, read straight off the + // bitstream (round-27/28: start_freq=5 stop_freq=8 xover_band=0 + // freq_scale=2 alter_scale=1 noise_bands=2 -> kx=14 k2=43 n_q=3; + // the previous stop_freq=3/xover_band=2 guess gave a smaller, + // wrong kx=16 k2=29 header). + freq_tables(44100, 5, 8, 2, 1, 0, 2).expect("valid header for the real file's rate") } #[test] diff --git a/scripts/aac-tables/sbrchirp.py b/scripts/aac-tables/sbrchirp.py index e757c361..d6cd1920 100644 --- a/scripts/aac-tables/sbrchirp.py +++ b/scripts/aac-tables/sbrchirp.py @@ -87,6 +87,10 @@ def tone_core(w, sfb_idx, group_active, gain=200): w.w(3, 4) w.w(1, 5) w.w(P.HCB_SF[60][1], P.HCB_SF[60][0]) # scalefactor delta 0 + w.w(0, 3) # pulse/tns/gain-control absent -- MUST precede spectral_data() + # (round-28 fix: this write sat AFTER the spectral loop below, shifting + # every bit that followed and desyncing the reference decoder on every + # probed band -- byte-accounted against sce_core's proven field order.) ngroups = (swb[sfb_idx + 1] - swb[sfb_idx]) // 4 for i in range(ngroups): quad = (2, 0, 0, 0) if i == group_active else (0, 0, 0, 0) @@ -95,7 +99,6 @@ def tone_core(w, sfb_idx, group_active, gain=200): w.w(code, length) for _ in signs: w.w(0, 1) - w.w(0, 3) # pulse/tns/gain-control absent return w