From b61d50e4c6cd8d92b22d59913b1f0acc4ac42154 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 21 Aug 2026 01:22:33 +0300 Subject: [PATCH] fix(aac-sbr): native 32-band QMF analysis replaces the zero-stuffed bridge; drop ENV_DOMAIN_SCALE The bridge (64-band bank on the core zero-stuffed at the even phase) equals the 32-band bank times 2*exp(j*pi/64*(k+0.5)*1.5) per band: 2x gain and a 1.5-output-sample delay. The low band never saw it (synthesis undoes both) but the HF generator copies band p into band k, so the per-band phase became a constant carrier error on every patched band against the reference. Carrier offset is -0.5 (reference-measured; the literal -1 lands the reference half a sample off, 0.984 above 5 kHz vs 0.9997). Real-library: reeln full 0.9964/0.9956 -> 0.9987/0.9966, above-5k 0.9990/0.9987 -> 0.9997/0.9993; second file full 0.9979/0.9935 -> 0.9999/0.9993. synthetic_heaac_matrix: every HE row up (44.1k/32k full 0.9376 -> 0.9823, 48k 64k/96k above 0.9857 -> 0.9999), LC rows unchanged. Envelope energies now dequantize in the spec domain directly; the 2x the spec 32/64 pair loses is restored once at the chain output. --- crates/ec-aac/src/sbr_chain.rs | 5 +- crates/ec-aac/src/sbr_env.rs | 14 +--- crates/ec-aac/src/sbr_qmf.rs | 116 +++++++++++++++++++++------------ 3 files changed, 83 insertions(+), 52 deletions(-) diff --git a/crates/ec-aac/src/sbr_chain.rs b/crates/ec-aac/src/sbr_chain.rs index f53e0333..b340e57f 100644 --- a/crates/ec-aac/src/sbr_chain.rs +++ b/crates/ec-aac/src/sbr_chain.rs @@ -527,7 +527,10 @@ impl SbrChain { } let mut pcm = state.synthesis.process_slot(&v); for s in &mut pcm { - *s *= crate::decode::OUTPUT_SCALE; + // The spec 32-analysis/64-synthesis pair (4.6.18.4.1 + + // 4.6.18.8.2, literal equations) reads back at half the + // core's amplitude; the 2x is the upsampler's gain. + *s *= 2.0 * crate::decode::OUTPUT_SCALE; } out.extend_from_slice(&pcm); } diff --git a/crates/ec-aac/src/sbr_env.rs b/crates/ec-aac/src/sbr_env.rs index 9ad3541c..db7198a4 100644 --- a/crates/ec-aac/src/sbr_env.rs +++ b/crates/ec-aac/src/sbr_env.rs @@ -21,14 +21,6 @@ use crate::sbr_tables::SBR_NOISE; use ec_dsp::Complex; const NOISE_FLOOR_OFFSET: f64 = 6.0; -/// corner-cut: this chain's analysis bank (`sbr_qmf::Analysis`, undone by -/// its own `Synthesis`) carries twice the spec bank's subband amplitude, so -/// the transmitted energies need x4 to compare against `|X_high|^2` here. -/// Measured on the real-file per-QMF-band instrument (`EC_AAC_SBR_QMF_WITNESS`, -/// `QMF-BAND` rows): a uniform -6 dB across every HF band with the low -/// band at 0 dB before this factor. Ceiling: none functionally; the upgrade -/// path is normalising `sbr_qmf` to the spec's gain and dropping this. -const ENV_DOMAIN_SCALE: f64 = 4.0; /// Envelope amp-resolution exponent: `2.0` (`bs_amp_res=1`, 3 dB) or `1.0` /// (`bs_amp_res=0`, 1.5 dB) -- expressed as the multiplier on `e_q/2` so @@ -39,10 +31,10 @@ fn alpha(amp_res: u8) -> f64 { } /// Plain (non-coupled) envelope dequantization: `E_Orig = 2^(E_Q * a) * 64` -/// (§4.6.18.7.2), times `ENV_DOMAIN_SCALE` to land in this chain's QMF -/// energy domain. +/// (§4.6.18.7.2), in the spec 32-band analysis domain `sbr_qmf::Analysis` +/// now produces directly. pub fn dequant_env(e_q: i32, amp_res: u8) -> f64 { - 2f64.powf(alpha(amp_res) * f64::from(e_q)) * 64.0 * ENV_DOMAIN_SCALE + 2f64.powf(alpha(amp_res) * f64::from(e_q)) * 64.0 } /// Plain noise-floor dequantization: `Q_Orig = 2^(NOISE_FLOOR_OFFSET - Q_Q)`, diff --git a/crates/ec-aac/src/sbr_qmf.rs b/crates/ec-aac/src/sbr_qmf.rs index 96524792..5c363ab6 100644 --- a/crates/ec-aac/src/sbr_qmf.rs +++ b/crates/ec-aac/src/sbr_qmf.rs @@ -2,27 +2,18 @@ //! analysis, folded to a shared core) and 4.6.18.8.2 (64-band synthesis) //! polyphase machinery HE-AAC's spectral band replication is built on. //! -//! # Bank sharing and the 32-band bridge +//! # Banks //! -//! The spec defines two analysis banks (32-band, 4.6.18.4.1, and 64-band, -//! 4.6.18.4) and two synthesis banks (64-band, 4.6.18.8.2, and a downsampled -//! 32-band variant, 4.6.18.8.3). A numpy round-trip campaign (see -//! `scripts/aac-tables/qmf_check.py` and the ledger, round "sbr-hunt") found -//! the 64-band analysis/synthesis pair self-consistent to `corr=0.999999` -//! against the literal spec equations, but the 32-band analysis and 32-band -//! downsampled synthesis equations, and the 32-into-64 zero-padding bridge -//! the SBR core signal actually needs, never cleared 0.7 correlation under -//! the same exact-equation reading. Zero-stuffing the 32 core samples to 64 -//! (`x64[2m] = 2*x32[m]`, odd samples 0) and running them through the -//! VERIFIED 64-band analysis, keeping bands `0..32` and dropping the -//! zero-stuffing image that lands in bands `32..64`, reached `corr=0.9994` -//! against an ideal band-limited 2x upsample -- short of the 0.9999 numpy -//! target but a real, spec-traceable near-PR bank, not a fitted -//! approximation. [`Analysis`] and [`HfAnalysis`] therefore share one -//! [`QmfAnalysis64`] core: the difference between them is only whether the -//! caller has 32 core samples (zero-stuffed by [`Analysis`]) or 64 -//! already-doubled-rate samples ([`HfAnalysis`], used only for measurement). -//! [`Synthesis`] is the 64-band bank directly, unmodified for either caller. +//! [`Analysis`] is the 32-band core analysis (4.6.18.4.1: `c[2n]` taps over +//! 320 samples, 64-point modulation), [`Synthesis`] the 64-band bank +//! (4.6.18.8.2) and [`HfAnalysis`] a 64-band analysis used only by the +//! measurement instruments. The numpy round-trip campaign +//! (`scripts/aac-tables/qmf_check.py`, ledger round "sbr-hunt") verified the +//! 64-band pair to `corr=0.999999` against the literal equations; its +//! 32-band attempt used full-rate taps and a conjugated carrier, which is +//! why it "never cleared 0.7" and the bank shipped for a while as a +//! zero-stuffed 64-band bridge instead (see [`Analysis`] for the per-band +//! phase that bridge imposed on the HF patches). //! //! The Kaiser-windowed fitted bank this file used to build at construction //! time (`prototype`/`theta`/`modulation_tables`/`analytic_gain`, tuned by a @@ -40,11 +31,6 @@ pub const ANALYSIS_BANDS: usize = 32; /// Synthesis channel count (twice the core's, spanning the doubled band). pub const SYNTHESIS_BANDS: usize = 64; -/// Zero-stuffing gain the 32-into-64 bridge needs to read back unity -/// amplitude (numpy-measured: `gain=2.0` gives `amp_ratio ~ 0.999`, `gain=1` -/// halves it -- the standard zero-stuffing compensation for a 2x upsample). -const ZERO_STUFF_GAIN: f64 = 2.0; - /// The shared ISO/IEC 14496-3 4.6.18.4 64-band analysis core: `PROTO_LEN` /// real history samples in (newest inserted last, matching PCM order), /// `SYNTHESIS_BANDS` complex subbands out per slot of `SYNTHESIS_BANDS` new @@ -106,20 +92,50 @@ impl QmfAnalysis64 { } } -/// The 32-band bridge: core PCM in (32 samples/slot), complex subband slots -/// out (32/slot), via zero-stuffing into the verified 64-band core and -/// keeping the low half (see the module doc for why this replaces a native -/// 32-band analysis). +/// ISO/IEC 14496-3 4.6.18.4.1 32-band analysis: core PCM in (32 +/// samples/slot), 32 complex subbands out, the decimated prototype `c[2n]` +/// over a 320-sample history and carrier `exp(j*pi/64*(k+0.5)*(2n-0.5))` +/// (see `new` for the -0.5). +/// +/// Replaces the zero-stuffed 64-band bridge: numpy +/// (`scripts/aac-tables/qmf_check.py` lineage) shows the bridge equals the +/// literal `(2n-1)` bank times `2 * exp(j*pi/64*(k+0.5)*1.5)` per band -- +/// a 1.5-sample (at the doubled rate) delay plus 2x gain. Harmless for the +/// low band (the synthesis undoes both), but the HF generator copies band +/// `p`'s subband signal into band `k`, where a per-band phase becomes a +/// constant carrier error against the reference. pub struct Analysis { - core: QmfAnalysis64, + hist: [f64; PROTO_LEN / 2], + cos_tab: Vec, + sin_tab: Vec, } impl Analysis { /// A fresh bank with an all-zero history (the usual filter startup /// transient applies to its first ~`PROTO_LEN` output samples). pub fn new() -> Analysis { + let mut cos_tab = vec![0.0f64; ANALYSIS_BANDS * 64]; + let mut sin_tab = vec![0.0f64; ANALYSIS_BANDS * 64]; + for k in 0..ANALYSIS_BANDS { + for n in 0..64 { + // Carrier offset -0.5, not the text's -1: measured against the + // reference decoder's PCM (real-file test, probe sweep over + // -1/-0.5/0/+0.5 on both banks) the reference's low band + // sits exactly half an output sample EARLIER than the + // literal (2n-1) bank and, unlike a synthesis-side shift, + // that half sample is carried by the HF copies too + // (above-5 kHz corr 0.9997 vs 0.9995 synthesis-side, 0.984 + // literal). Equivalent to a 64-band analysis of the core + // zero-stuffed at the ODD phase. + let theta = std::f64::consts::PI / 64.0 * (k as f64 + 0.5) * (2.0 * n as f64 - 0.5); + cos_tab[k * 64 + n] = theta.cos(); + sin_tab[k * 64 + n] = theta.sin(); + } + } Analysis { - core: QmfAnalysis64::new(), + hist: [0.0; PROTO_LEN / 2], + cos_tab, + sin_tab, } } @@ -130,13 +146,26 @@ impl Analysis { &mut self, samples: &[f32; ANALYSIS_BANDS], ) -> [Complex; ANALYSIS_BANDS] { - let mut x64 = [0.0f64; SYNTHESIS_BANDS]; - for (m, &s) in samples.iter().enumerate() { - x64[2 * m] = ZERO_STUFF_GAIN * s as f64; + const LEN: usize = PROTO_LEN / 2; + for &s in samples { + self.hist.copy_within(0..LEN - 1, 1); + self.hist[0] = s as f64; + } + // Z[n] = x[n]*c[2n], n=0..319; u[n] = sum_{j=0}^{4} Z[n+64j], n=0..63 + let mut u = [0.0f64; 64]; + for (n, uu) in u.iter_mut().enumerate() { + *uu = (0..5).map(|j| self.hist[n + 64 * j] * QMF_WINDOW[2 * (n + 64 * j)]).sum(); } - let full = self.core.process_slot(&x64); let mut out = [Complex::new(0.0, 0.0); ANALYSIS_BANDS]; - out.copy_from_slice(&full[0..ANALYSIS_BANDS]); + for (k, slot) in out.iter_mut().enumerate() { + let row = k * 64; + let (mut re, mut im) = (0.0f64, 0.0f64); + for n in 0..64 { + re += u[n] * self.cos_tab[row + n]; + im += u[n] * self.sin_tab[row + n]; + } + *slot = Complex::new(re, im); + } out } } @@ -315,11 +344,18 @@ mod tests { fft.forward(&mut spectrum); let mut padded = vec![Complex::new(0.0, 0.0); 2 * n]; + // The 32-analysis/64-synthesis pair reads back at half amplitude + // and 1 output sample later than the zero-stuffed bridge did; fold + // both into the ground truth. + let delay = |m: f64| { + let ph = -2.0 * std::f64::consts::PI * m / (2 * n) as f64; + Complex::new(ph.cos(), ph.sin()) + }; for k in 0..n / 2 { - padded[k] = spectrum[k].scale(2.0); + padded[k] = spectrum[k].scale(2.0) * delay(k as f64); } for k in n / 2..n { - padded[2 * n - n + k] = spectrum[k].scale(2.0); + padded[2 * n - n + k] = spectrum[k].scale(2.0) * delay(k as f64 - n as f64); } let mut fft2 = Fft::::new(2 * n); fft2.inverse(&mut padded); @@ -411,8 +447,8 @@ mod tests { "measured filterbank delay: {best_lag} samples, correlation {best_corr:.6}, amplitude ratio {amp_ratio:.6}" ); assert!( - (0.98..=1.02).contains(&_ratio), - "round-trip amplitude ratio {amp_ratio} not within 2% of unity gain" + (0.49..=0.51).contains(&_ratio), + "round-trip amplitude ratio {amp_ratio} not within 2% of the spec pair's 0.5 (sbr_chain restores the 2x)" ); assert!( best_corr >= 0.9999,