Native 32-band QMF analysis replaces the zero-stuffed bridge
Browse files at this commitb61d50e4c6cd8d92b22d59913b1f0acc4ac42154Parent: 0f6c713- fix
- native 32-band QMF analysis replaces the zero-stuffed bridge
- drop ENV_DOMAIN_SCALE
3 changed files +83 -52
- crates/ec-aac/src/sbr_chain.rs +4 -1
- crates/ec-aac/src/sbr_env.rs +3 -11
- crates/ec-aac/src/sbr_qmf.rs +76 -40
crates/ec-aac/src/sbr_chain.rsmodified+4 -1
| @@ -527,7 +527,10 @@ impl SbrChain { | ||
| 527 | 527 | } |
| 528 | 528 | let mut pcm = state.synthesis.process_slot(&v); |
| 529 | 529 | for s in &mut pcm { |
| 530 | - *s *= crate::decode::OUTPUT_SCALE; | |
| 530 | + // The spec 32-analysis/64-synthesis pair (4.6.18.4.1 + | |
| 531 | + // 4.6.18.8.2, literal equations) reads back at half the | |
| 532 | + // core's amplitude; the 2x is the upsampler's gain. | |
| 533 | + *s *= 2.0 * crate::decode::OUTPUT_SCALE; | |
| 531 | 534 | } |
| 532 | 535 | out.extend_from_slice(&pcm); |
| 533 | 536 | } |
crates/ec-aac/src/sbr_env.rsmodified+3 -11
| @@ -21,14 +21,6 @@ use crate::sbr_tables::SBR_NOISE; | ||
| 21 | 21 | use ec_dsp::Complex; |
| 22 | 22 | |
| 23 | 23 | const NOISE_FLOOR_OFFSET: f64 = 6.0; |
| 24 | -/// corner-cut: this chain's analysis bank (`sbr_qmf::Analysis`, undone by | |
| 25 | -/// its own `Synthesis`) carries twice the spec bank's subband amplitude, so | |
| 26 | -/// the transmitted energies need x4 to compare against `|X_high|^2` here. | |
| 27 | -/// Measured on the real-file per-QMF-band instrument (`EC_AAC_SBR_QMF_WITNESS`, | |
| 28 | -/// `QMF-BAND` rows): a uniform -6 dB across every HF band with the low | |
| 29 | -/// band at 0 dB before this factor. Ceiling: none functionally; the upgrade | |
| 30 | -/// path is normalising `sbr_qmf` to the spec's gain and dropping this. | |
| 31 | -const ENV_DOMAIN_SCALE: f64 = 4.0; | |
| 32 | 24 | |
| 33 | 25 | /// Envelope amp-resolution exponent: `2.0` (`bs_amp_res=1`, 3 dB) or `1.0` |
| 34 | 26 | /// (`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 { | ||
| 39 | 31 | } |
| 40 | 32 | |
| 41 | 33 | /// Plain (non-coupled) envelope dequantization: `E_Orig = 2^(E_Q * a) * 64` |
| 42 | -/// (§4.6.18.7.2), times `ENV_DOMAIN_SCALE` to land in this chain's QMF | |
| 43 | -/// energy domain. | |
| 34 | +/// (§4.6.18.7.2), in the spec 32-band analysis domain `sbr_qmf::Analysis` | |
| 35 | +/// now produces directly. | |
| 44 | 36 | pub fn dequant_env(e_q: i32, amp_res: u8) -> f64 { |
| 45 | - 2f64.powf(alpha(amp_res) * f64::from(e_q)) * 64.0 * ENV_DOMAIN_SCALE | |
| 37 | + 2f64.powf(alpha(amp_res) * f64::from(e_q)) * 64.0 | |
| 46 | 38 | } |
| 47 | 39 | |
| 48 | 40 | /// Plain noise-floor dequantization: `Q_Orig = 2^(NOISE_FLOOR_OFFSET - Q_Q)`, |
crates/ec-aac/src/sbr_qmf.rsmodified+76 -40
| @@ -2,27 +2,18 @@ | ||
| 2 | 2 | //! analysis, folded to a shared core) and 4.6.18.8.2 (64-band synthesis) |
| 3 | 3 | //! polyphase machinery HE-AAC's spectral band replication is built on. |
| 4 | 4 | //! |
| 5 | -//! # Bank sharing and the 32-band bridge | |
| 5 | +//! # Banks | |
| 6 | 6 | //! |
| 7 | -//! The spec defines two analysis banks (32-band, 4.6.18.4.1, and 64-band, | |
| 8 | -//! 4.6.18.4) and two synthesis banks (64-band, 4.6.18.8.2, and a downsampled | |
| 9 | -//! 32-band variant, 4.6.18.8.3). A numpy round-trip campaign (see | |
| 10 | -//! `scripts/aac-tables/qmf_check.py` and the ledger, round "sbr-hunt") found | |
| 11 | -//! the 64-band analysis/synthesis pair self-consistent to `corr=0.999999` | |
| 12 | -//! against the literal spec equations, but the 32-band analysis and 32-band | |
| 13 | -//! downsampled synthesis equations, and the 32-into-64 zero-padding bridge | |
| 14 | -//! the SBR core signal actually needs, never cleared 0.7 correlation under | |
| 15 | -//! the same exact-equation reading. Zero-stuffing the 32 core samples to 64 | |
| 16 | -//! (`x64[2m] = 2*x32[m]`, odd samples 0) and running them through the | |
| 17 | -//! VERIFIED 64-band analysis, keeping bands `0..32` and dropping the | |
| 18 | -//! zero-stuffing image that lands in bands `32..64`, reached `corr=0.9994` | |
| 19 | -//! against an ideal band-limited 2x upsample -- short of the 0.9999 numpy | |
| 20 | -//! target but a real, spec-traceable near-PR bank, not a fitted | |
| 21 | -//! approximation. [`Analysis`] and [`HfAnalysis`] therefore share one | |
| 22 | -//! [`QmfAnalysis64`] core: the difference between them is only whether the | |
| 23 | -//! caller has 32 core samples (zero-stuffed by [`Analysis`]) or 64 | |
| 24 | -//! already-doubled-rate samples ([`HfAnalysis`], used only for measurement). | |
| 25 | -//! [`Synthesis`] is the 64-band bank directly, unmodified for either caller. | |
| 7 | +//! [`Analysis`] is the 32-band core analysis (4.6.18.4.1: `c[2n]` taps over | |
| 8 | +//! 320 samples, 64-point modulation), [`Synthesis`] the 64-band bank | |
| 9 | +//! (4.6.18.8.2) and [`HfAnalysis`] a 64-band analysis used only by the | |
| 10 | +//! measurement instruments. The numpy round-trip campaign | |
| 11 | +//! (`scripts/aac-tables/qmf_check.py`, ledger round "sbr-hunt") verified the | |
| 12 | +//! 64-band pair to `corr=0.999999` against the literal equations; its | |
| 13 | +//! 32-band attempt used full-rate taps and a conjugated carrier, which is | |
| 14 | +//! why it "never cleared 0.7" and the bank shipped for a while as a | |
| 15 | +//! zero-stuffed 64-band bridge instead (see [`Analysis`] for the per-band | |
| 16 | +//! phase that bridge imposed on the HF patches). | |
| 26 | 17 | //! |
| 27 | 18 | //! The Kaiser-windowed fitted bank this file used to build at construction |
| 28 | 19 | //! time (`prototype`/`theta`/`modulation_tables`/`analytic_gain`, tuned by a |
| @@ -40,11 +31,6 @@ pub const ANALYSIS_BANDS: usize = 32; | ||
| 40 | 31 | /// Synthesis channel count (twice the core's, spanning the doubled band). |
| 41 | 32 | pub const SYNTHESIS_BANDS: usize = 64; |
| 42 | 33 | |
| 43 | -/// Zero-stuffing gain the 32-into-64 bridge needs to read back unity | |
| 44 | -/// amplitude (numpy-measured: `gain=2.0` gives `amp_ratio ~ 0.999`, `gain=1` | |
| 45 | -/// halves it -- the standard zero-stuffing compensation for a 2x upsample). | |
| 46 | -const ZERO_STUFF_GAIN: f64 = 2.0; | |
| 47 | - | |
| 48 | 34 | /// The shared ISO/IEC 14496-3 4.6.18.4 64-band analysis core: `PROTO_LEN` |
| 49 | 35 | /// real history samples in (newest inserted last, matching PCM order), |
| 50 | 36 | /// `SYNTHESIS_BANDS` complex subbands out per slot of `SYNTHESIS_BANDS` new |
| @@ -106,20 +92,50 @@ impl QmfAnalysis64 { | ||
| 106 | 92 | } |
| 107 | 93 | } |
| 108 | 94 | |
| 109 | -/// The 32-band bridge: core PCM in (32 samples/slot), complex subband slots | |
| 110 | -/// out (32/slot), via zero-stuffing into the verified 64-band core and | |
| 111 | -/// keeping the low half (see the module doc for why this replaces a native | |
| 112 | -/// 32-band analysis). | |
| 95 | +/// ISO/IEC 14496-3 4.6.18.4.1 32-band analysis: core PCM in (32 | |
| 96 | +/// samples/slot), 32 complex subbands out, the decimated prototype `c[2n]` | |
| 97 | +/// over a 320-sample history and carrier `exp(j*pi/64*(k+0.5)*(2n-0.5))` | |
| 98 | +/// (see `new` for the -0.5). | |
| 99 | +/// | |
| 100 | +/// Replaces the zero-stuffed 64-band bridge: numpy | |
| 101 | +/// (`scripts/aac-tables/qmf_check.py` lineage) shows the bridge equals the | |
| 102 | +/// literal `(2n-1)` bank times `2 * exp(j*pi/64*(k+0.5)*1.5)` per band -- | |
| 103 | +/// a 1.5-sample (at the doubled rate) delay plus 2x gain. Harmless for the | |
| 104 | +/// low band (the synthesis undoes both), but the HF generator copies band | |
| 105 | +/// `p`'s subband signal into band `k`, where a per-band phase becomes a | |
| 106 | +/// constant carrier error against the reference. | |
| 113 | 107 | pub struct Analysis { |
| 114 | - core: QmfAnalysis64, | |
| 108 | + hist: [f64; PROTO_LEN / 2], | |
| 109 | + cos_tab: Vec<f64>, | |
| 110 | + sin_tab: Vec<f64>, | |
| 115 | 111 | } |
| 116 | 112 | |
| 117 | 113 | impl Analysis { |
| 118 | 114 | /// A fresh bank with an all-zero history (the usual filter startup |
| 119 | 115 | /// transient applies to its first ~`PROTO_LEN` output samples). |
| 120 | 116 | pub fn new() -> Analysis { |
| 117 | + let mut cos_tab = vec![0.0f64; ANALYSIS_BANDS * 64]; | |
| 118 | + let mut sin_tab = vec![0.0f64; ANALYSIS_BANDS * 64]; | |
| 119 | + for k in 0..ANALYSIS_BANDS { | |
| 120 | + for n in 0..64 { | |
| 121 | + // Carrier offset -0.5, not the text's -1: measured against the | |
| 122 | + // reference decoder's PCM (real-file test, probe sweep over | |
| 123 | + // -1/-0.5/0/+0.5 on both banks) the reference's low band | |
| 124 | + // sits exactly half an output sample EARLIER than the | |
| 125 | + // literal (2n-1) bank and, unlike a synthesis-side shift, | |
| 126 | + // that half sample is carried by the HF copies too | |
| 127 | + // (above-5 kHz corr 0.9997 vs 0.9995 synthesis-side, 0.984 | |
| 128 | + // literal). Equivalent to a 64-band analysis of the core | |
| 129 | + // zero-stuffed at the ODD phase. | |
| 130 | + let theta = std::f64::consts::PI / 64.0 * (k as f64 + 0.5) * (2.0 * n as f64 - 0.5); | |
| 131 | + cos_tab[k * 64 + n] = theta.cos(); | |
| 132 | + sin_tab[k * 64 + n] = theta.sin(); | |
| 133 | + } | |
| 134 | + } | |
| 121 | 135 | Analysis { |
| 122 | - core: QmfAnalysis64::new(), | |
| 136 | + hist: [0.0; PROTO_LEN / 2], | |
| 137 | + cos_tab, | |
| 138 | + sin_tab, | |
| 123 | 139 | } |
| 124 | 140 | } |
| 125 | 141 | |
| @@ -130,13 +146,26 @@ impl Analysis { | ||
| 130 | 146 | &mut self, |
| 131 | 147 | samples: &[f32; ANALYSIS_BANDS], |
| 132 | 148 | ) -> [Complex<f64>; ANALYSIS_BANDS] { |
| 133 | - let mut x64 = [0.0f64; SYNTHESIS_BANDS]; | |
| 134 | - for (m, &s) in samples.iter().enumerate() { | |
| 135 | - x64[2 * m] = ZERO_STUFF_GAIN * s as f64; | |
| 149 | + const LEN: usize = PROTO_LEN / 2; | |
| 150 | + for &s in samples { | |
| 151 | + self.hist.copy_within(0..LEN - 1, 1); | |
| 152 | + self.hist[0] = s as f64; | |
| 153 | + } | |
| 154 | + // Z[n] = x[n]*c[2n], n=0..319; u[n] = sum_{j=0}^{4} Z[n+64j], n=0..63 | |
| 155 | + let mut u = [0.0f64; 64]; | |
| 156 | + for (n, uu) in u.iter_mut().enumerate() { | |
| 157 | + *uu = (0..5).map(|j| self.hist[n + 64 * j] * QMF_WINDOW[2 * (n + 64 * j)]).sum(); | |
| 136 | 158 | } |
| 137 | - let full = self.core.process_slot(&x64); | |
| 138 | 159 | let mut out = [Complex::new(0.0, 0.0); ANALYSIS_BANDS]; |
| 139 | - out.copy_from_slice(&full[0..ANALYSIS_BANDS]); | |
| 160 | + for (k, slot) in out.iter_mut().enumerate() { | |
| 161 | + let row = k * 64; | |
| 162 | + let (mut re, mut im) = (0.0f64, 0.0f64); | |
| 163 | + for n in 0..64 { | |
| 164 | + re += u[n] * self.cos_tab[row + n]; | |
| 165 | + im += u[n] * self.sin_tab[row + n]; | |
| 166 | + } | |
| 167 | + *slot = Complex::new(re, im); | |
| 168 | + } | |
| 140 | 169 | out |
| 141 | 170 | } |
| 142 | 171 | } |
| @@ -315,11 +344,18 @@ mod tests { | ||
| 315 | 344 | fft.forward(&mut spectrum); |
| 316 | 345 | |
| 317 | 346 | let mut padded = vec![Complex::new(0.0, 0.0); 2 * n]; |
| 347 | + // The 32-analysis/64-synthesis pair reads back at half amplitude | |
| 348 | + // and 1 output sample later than the zero-stuffed bridge did; fold | |
| 349 | + // both into the ground truth. | |
| 350 | + let delay = |m: f64| { | |
| 351 | + let ph = -2.0 * std::f64::consts::PI * m / (2 * n) as f64; | |
| 352 | + Complex::new(ph.cos(), ph.sin()) | |
| 353 | + }; | |
| 318 | 354 | for k in 0..n / 2 { |
| 319 | - padded[k] = spectrum[k].scale(2.0); | |
| 355 | + padded[k] = spectrum[k].scale(2.0) * delay(k as f64); | |
| 320 | 356 | } |
| 321 | 357 | for k in n / 2..n { |
| 322 | - padded[2 * n - n + k] = spectrum[k].scale(2.0); | |
| 358 | + padded[2 * n - n + k] = spectrum[k].scale(2.0) * delay(k as f64 - n as f64); | |
| 323 | 359 | } |
| 324 | 360 | let mut fft2 = Fft::<f64>::new(2 * n); |
| 325 | 361 | fft2.inverse(&mut padded); |
| @@ -411,8 +447,8 @@ mod tests { | ||
| 411 | 447 | "measured filterbank delay: {best_lag} samples, correlation {best_corr:.6}, amplitude ratio {amp_ratio:.6}" |
| 412 | 448 | ); |
| 413 | 449 | assert!( |
| 414 | - (0.98..=1.02).contains(&_ratio), | |
| 415 | - "round-trip amplitude ratio {amp_ratio} not within 2% of unity gain" | |
| 450 | + (0.49..=0.51).contains(&_ratio), | |
| 451 | + "round-trip amplitude ratio {amp_ratio} not within 2% of the spec pair's 0.5 (sbr_chain restores the 2x)" | |
| 416 | 452 | ); |
| 417 | 453 | assert!( |
| 418 | 454 | best_corr >= 0.9999, |