diff --git a/crates/ec-aac/src/sbr_chain.rs b/crates/ec-aac/src/sbr_chain.rs index 4fcf994f..11189e1c 100644 --- a/crates/ec-aac/src/sbr_chain.rs +++ b/crates/ec-aac/src/sbr_chain.rs @@ -377,31 +377,7 @@ impl SbrChain { continue; } let state = &mut elem.channels[ch]; - let mut low_cur = vec![vec![Complex::ZERO; num_slots]; ANALYSIS_BANDS]; - let mut raw = Vec::with_capacity(num_slots); - for slot in 0..num_slots { - let mut chunk = [0f32; ANALYSIS_BANDS]; - chunk.copy_from_slice(&plane[slot * ANALYSIS_BANDS..(slot + 1) * ANALYSIS_BANDS]); - // `plane` is already at the decoder's final output scale - // (`decode::OUTPUT_SCALE`, applied by the core filterbank), - // but the transmitted envelope/noise data is calibrated - // against the core's un-normalized internal PCM domain -- - // undo that scale before analysis so the SBR gain match - // (raw QMF energy vs. the transmitted target) compares - // like against like, and reapply it once below after - // synthesis. Without this the gain step's `target/current` - // ratio is inflated by `1/OUTPUT_SCALE^2` in energy, - // exactly the multi-order-of-magnitude blowup this chain - // used to produce on every real HE-AAC file. - for s in &mut chunk { - *s /= crate::decode::OUTPUT_SCALE; - } - let sub = state.analysis.process_slot(&chunk); - for b in 0..ANALYSIS_BANDS { - low_cur[b][slot] = sub[b]; - } - raw.push(sub); - } + let (low_cur, raw) = analyze_plane(state, plane, num_slots); let sbr_ch = &data.channels[ch]; let hf = sbr_hf::generate( @@ -481,17 +457,7 @@ impl SbrChain { let kx = (tables.kx as usize).min(ANALYSIS_BANDS); let k2 = (tables.k2 as usize).min(band_limit); - let mut vs: Vec<[Complex; SYNTHESIS_BANDS]> = Vec::with_capacity(num_slots); - for slot in 0..num_slots { - let mut v = [Complex::ZERO; SYNTHESIS_BANDS]; - v[0..kx].copy_from_slice(&raw[slot][0..kx]); - for b in kx..k2 { - if b - kx < hf.len() { - v[b] = hf[b - kx][slot]; - } - } - vs.push(v); - } + let vs = merge_bands(&raw, &hf, kx, k2, num_slots); let out = match (ps_mode && num_slots == PS_SLOTS, data.ps.as_ref()) { (true, Some(ps)) => { let (left, right) = @@ -530,6 +496,60 @@ impl SbrChain { } } +fn analyze_plane( + state: &mut ChannelState, + plane: &[f32], + num_slots: usize, +) -> (Vec>>, Vec<[Complex; ANALYSIS_BANDS]>) { + let mut low_cur = vec![vec![Complex::ZERO; num_slots]; ANALYSIS_BANDS]; + let mut raw = Vec::with_capacity(num_slots); + for slot in 0..num_slots { + let mut chunk = [0f32; ANALYSIS_BANDS]; + chunk.copy_from_slice(&plane[slot * ANALYSIS_BANDS..(slot + 1) * ANALYSIS_BANDS]); + // `plane` is already at the decoder's final output scale + // (`decode::OUTPUT_SCALE`, applied by the core filterbank), + // but the transmitted envelope/noise data is calibrated + // against the core's un-normalized internal PCM domain -- + // undo that scale before analysis so the SBR gain match + // (raw QMF energy vs. the transmitted target) compares + // like against like, and reapply it once below after + // synthesis. Without this the gain step's `target/current` + // ratio is inflated by `1/OUTPUT_SCALE^2` in energy, + // exactly the multi-order-of-magnitude blowup this chain + // used to produce on every real HE-AAC file. + for s in &mut chunk { + *s /= crate::decode::OUTPUT_SCALE; + } + let sub = state.analysis.process_slot(&chunk); + for b in 0..ANALYSIS_BANDS { + low_cur[b][slot] = sub[b]; + } + raw.push(sub); + } + (low_cur, raw) +} + +fn merge_bands( + raw: &[[Complex; ANALYSIS_BANDS]], + hf: &[Vec>], + kx: usize, + k2: usize, + num_slots: usize, +) -> Vec<[Complex; SYNTHESIS_BANDS]> { + let mut vs: Vec<[Complex; SYNTHESIS_BANDS]> = Vec::with_capacity(num_slots); + for slot in 0..num_slots { + let mut v = [Complex::ZERO; SYNTHESIS_BANDS]; + v[0..kx].copy_from_slice(&raw[slot][0..kx]); + for b in kx..k2 { + if b - kx < hf.len() { + v[b] = hf[b - kx][slot]; + } + } + vs.push(v); + } + vs +} + /// Inserts the second output plane a PS element needs, right after its first /// (a copy, which the PS path then overwrites). Returns the element's new /// plane count: 2 when a plane was inserted.