From 3745d4569047978e395d1a83b2153a11b3a70722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 19 Aug 2026 22:55:37 +0300 Subject: [PATCH] fix(aac): uncoupled SBR CPEs read envelope+noise as two channel passes sbr_channel_pair_element()'s envelope/noise interleave depends on bs_coupling: the coupled form reads sbr_envelope(ch)/sbr_noise(ch) interleaved per channel, but the uncoupled form reads two separate passes -- both channels' envelopes, then both channels' noise floors. The parser (and the fixture rig's writer) read both forms the same, per-channel-interleaved way. Total bit count is order-invariant, so byte-accounting fixtures never caught it; content desyncs every field after channel 0's envelopes whenever the channels' envelope (6/7-bit) and noise (5-bit) field counts differ -- exactly the shape round-11 pinned to channel 1's second envelope going to garbage. A synthetic uncoupled CPE (ch0 FIXVAR 4-env loud HF, ch1 FIXFIX 2-env quiet HF -- the real failing shape) decoded to real PCM confirms the fix: writing the two candidate orders and decoding both, the separated (spec) order reproduces the coded amplitude cleanly while the interleaved order desyncs it. Fixed the parser and the rig's CPE writer together, regenerated CPE_UNCOUPLED_BODY (bits unchanged: order is still byte-count-invariant). reeln real-file sweep: ch0 full 0.9726 (unchanged), ch1 full 0.7716 -> 0.9705, ch1 above-crossover 0.0055 -> 0.7227. FMJ's separate pre-SBR bug is unaffected and still queued last. --- crates/ec-aac/src/sbr_payload.rs | 64 +++++++++++++++++++---- scripts/aac-tables/sbrpayload_fixtures.py | 24 ++++++++- 2 files changed, 76 insertions(+), 12 deletions(-) diff --git a/crates/ec-aac/src/sbr_payload.rs b/crates/ec-aac/src/sbr_payload.rs index 9b35161b..2d97900c 100644 --- a/crates/ec-aac/src/sbr_payload.rs +++ b/crates/ec-aac/src/sbr_payload.rs @@ -512,13 +512,25 @@ impl SbrParser { } } - let mut channels = Vec::with_capacity(n_channels); - for ch in 0..n_channels { + // sbr_channel_pair_element's envelope/noise interleave depends on + // bs_coupling: the COUPLED form reads sbr_envelope(ch)/sbr_noise(ch) + // interleaved per channel (env0,noise0,env1,noise1), but the + // UNCOUPLED form reads two separate per-channel passes -- + // sbr_envelope(ch=0), sbr_envelope(ch=1), sbr_noise(ch=0), + // sbr_noise(ch=1). Reading the uncoupled form interleaved (as a + // single per-channel loop) is order-invariant in total bit count (so + // a byte-accounting oracle can't see the mistake) but desyncs every + // field after the first channel's envelopes whenever the two + // channels' envelope (6/7-bit) and noise (5-bit) field counts + // differ, which round-11 pinned to ch1's second envelope. + let separated = is_cpe && !coupling && n_channels == 2; + let mut e_q_all: Vec>> = vec![Vec::new(); n_channels]; + let mut q_q_all: Vec>> = vec![Vec::new(); n_channels]; + + let mut read_env = |this: &mut Self, r: &mut BitReader, ch: usize| -> Result<()> { let balance = is_cpe && coupling && ch == 1; let num_env = grids[ch].num_env; - let num_noise = if num_env == 1 { 1 } else { 2 }; let amp_res = amp_res_of(header.amp_res, num_env); - let mut e_q: Vec> = Vec::with_capacity(num_env); for i in 0..num_env { let bands = if grids[ch].freq_res[i] != 0 { @@ -530,23 +542,55 @@ impl SbrParser { let prev: &[i32] = if i > 0 { &e_q[i - 1] } else { - &self.state[ch].last_env + &this.state[ch].last_env }; - let row = self.decode_envelope(r, bands, dt, balance, amp_res, prev)?; + let row = this.decode_envelope(r, bands, dt, balance, amp_res, prev)?; e_q.push(row); } - + e_q_all[ch] = e_q; + Ok(()) + }; + if separated { + for ch in 0..n_channels { + read_env(self, r, ch)?; + } + } + let mut read_noise = |this: &mut Self, r: &mut BitReader, ch: usize| -> Result<()> { + let balance = is_cpe && coupling && ch == 1; + let num_env = grids[ch].num_env; + let num_noise = if num_env == 1 { 1 } else { 2 }; let mut q_q: Vec> = Vec::with_capacity(num_noise); for i in 0..num_noise { let dt = df_noise[ch][i]; let prev: &[i32] = if i > 0 { &q_q[i - 1] } else { - &self.state[ch].last_noise + &this.state[ch].last_noise }; - let row = self.decode_noise(r, tables.n_q, dt, balance, prev)?; + let row = this.decode_noise(r, tables.n_q, dt, balance, prev)?; q_q.push(row); } + q_q_all[ch] = q_q; + Ok(()) + }; + if separated { + for ch in 0..n_channels { + read_noise(self, r, ch)?; + } + } else { + for ch in 0..n_channels { + read_env(self, r, ch)?; + read_noise(self, r, ch)?; + } + } + + let mut channels = Vec::with_capacity(n_channels); + for ch in 0..n_channels { + let balance = is_cpe && coupling && ch == 1; + let num_env = grids[ch].num_env; + let amp_res = amp_res_of(header.amp_res, num_env); + let e_q = e_q_all[ch].clone(); + let q_q = q_q_all[ch].clone(); // Containment, not the fix: a still-undiscovered desync could // make a DT delta huge, and since a following frame's DT reads @@ -623,7 +667,7 @@ mod tests { const VARVAR_DF_BITS: u64 = 77; const FIXFIX_DT_BODY: &[u8] = &[0x90, 0x31, 0xd8, 0xb0, 0x95, 0x78, 0x00, 0x00]; const FIXFIX_DT_BITS: u64 = 57; - const CPE_UNCOUPLED_BODY: &[u8] = &[0x90, 0x31, 0xd8, 0xb0, 0x00, 0x05, 0x78, 0x07, 0x80, 0x00]; + const CPE_UNCOUPLED_BODY: &[u8] = &[0x90, 0x31, 0xd8, 0xb0, 0x00, 0x05, 0x78, 0xf0, 0x00, 0x00]; const CPE_UNCOUPLED_BITS: u64 = 75; const EXTENSION_BODY: &[u8] = &[0x90, 0x31, 0xd8, 0xb0, 0x05, 0xe0, 0x11, 0x00]; const EXTENSION_BITS: u64 = 64; diff --git a/scripts/aac-tables/sbrpayload_fixtures.py b/scripts/aac-tables/sbrpayload_fixtures.py index 2b2f957c..bba92cbe 100644 --- a/scripts/aac-tables/sbrpayload_fixtures.py +++ b/scripts/aac-tables/sbrpayload_fixtures.py @@ -152,10 +152,14 @@ def sbr_bits_full(cfg, tables, grid_cfg, words=()): for _ in channels: for _ in range(tables["n_q"]): s.w(cfg["invf"], 2) - for idx, ch in enumerate(channels): + # sbr_channel_pair_element's envelope/noise interleave depends on + # bs_coupling: COUPLED reads sbr_envelope(ch)/sbr_noise(ch) interleaved + # per channel; UNCOUPLED reads two separate passes -- both channels' + # sbr_envelope(), then both channels' sbr_noise() (matches the Rust + # parser's `separated` split in sbr_payload.rs). + def write_env(idx, ch): balance = 1 if (cpe and coupling and ch == 1) else 0 num_env = num_env_list[idx] - num_noise = 1 if num_env == 1 else 2 freq_res_list = freq_res_lists[idx] amp_res_eff = 0 if num_env == 1 else cfg["amp_res"] for i in range(num_env): @@ -168,6 +172,11 @@ def sbr_bits_full(cfg, tables, grid_cfg, words=()): else: for _ in range(bands): slot() + + def write_noise(idx, ch): + balance = 1 if (cpe and coupling and ch == 1) else 0 + num_env = num_env_list[idx] + num_noise = 1 if num_env == 1 else 2 for i in range(num_noise): if cfg["df_noise"][i] == 0: key = "noise0b" if balance else "noise0" @@ -177,6 +186,17 @@ def sbr_bits_full(cfg, tables, grid_cfg, words=()): else: for _ in range(tables["n_q"]): slot() + + separated = cpe and not coupling and len(channels) == 2 + if separated: + for idx, ch in enumerate(channels): + write_env(idx, ch) + for idx, ch in enumerate(channels): + write_noise(idx, ch) + else: + for idx, ch in enumerate(channels): + write_env(idx, ch) + write_noise(idx, ch) for ch in channels: on = cfg.get("add_harmonic", [0, 0])[ch] s.w(1 if on else 0, 1)