forge
Explore
Log in

Don't you dare go Hollow

ec-aac SBR: downsampled SBR (ext rate < 2*core, al_sbr_sr_48_2_fsaac48) ran the 64-band chain with tables at the 48 kHz ext rate (2048/AU). ffmpeg aacsbr_template.c: tables at 2*core, 32-band synthesis with decimated window c[2n] when ext < 2*core. Fix: parser rate always 2*core, SbrChain::set_downsampled, Synthesis::downsampled (32 bands, 64-band path numerically unchanged), k2 clamped to 32, LFE bypass same bank. QMF_WINDOW untouched (d8db6262 sign flips dropped). The 'missing' 170 AUs are the test MAX_SAMPLES=1_000_000 cap (977*1024=1000448), not decoder loss. cargo check -p ec-aac --all-targets clean; tests not run

Browse files at this commit

780c4a9016142df90e3f21daa86bd8a4f093e596Ahmet Kaan GümüşParent: b3f487f
crates/ec-aac/src/decode.rsmodified+6 -0
@@ -582,6 +582,12 @@ impl BlockDecoder {
582 self.sbr = rate.map(crate::sbr_chain::SbrChain::new);582 self.sbr = rate.map(crate::sbr_chain::SbrChain::new);
583 }583 }
584 584
585+ pub fn set_sbr_downsampled(&mut self, on: bool) {
586+ if let Some(sbr) = self.sbr.as_mut() {
587+ sbr.set_downsampled(on);
588+ }
589+ }
590+
585 /// Marks Parametric Stereo as signalled by the config, for the SBR chain591 /// Marks Parametric Stereo as signalled by the config, for the SBR chain
586 /// (if one is armed): its elements then decode to two channels.592 /// (if one is armed): its elements then decode to two channels.
587 pub fn set_sbr_ps(&mut self, on: bool) {593 pub fn set_sbr_ps(&mut self, on: bool) {
crates/ec-aac/src/lib.rsmodified+4 -2
@@ -144,8 +144,10 @@ impl AacDecoder {
144 // SBR (and Parametric Stereo, for HE-AACv2) is reconstructed: the144 // SBR (and Parametric Stereo, for HE-AACv2) is reconstructed: the
145 // core is decoded first, then the extension-rate chain runs on it.145 // core is decoded first, then the extension-rate chain runs on it.
146 if cfg.sbr_present {146 if cfg.sbr_present {
147- let rate = cfg.extension_sample_rate.unwrap_or(cfg.sample_rate * 2);147+ let sbr_rate = cfg.sample_rate * 2;
148- d.block.set_sbr_rate(Some(rate));148+ let ext_rate = cfg.extension_sample_rate.unwrap_or(sbr_rate);
149+ d.block.set_sbr_rate(Some(sbr_rate));
150+ d.block.set_sbr_downsampled(ext_rate < sbr_rate);
149 d.block.set_sbr_ps(cfg.ps_present);151 d.block.set_sbr_ps(cfg.ps_present);
150 } else if !cfg.ps_present {152 } else if !cfg.ps_present {
151 // Plain AAC-LC config: an SBR payload may still follow (implicit signalling).153 // Plain AAC-LC config: an SBR payload may still follow (implicit signalling).
crates/ec-aac/src/sbr_chain.rsmodified+33 -14
@@ -237,10 +237,14 @@ struct Element {
237 }237 }
238 238
239 impl Element {239 impl Element {
240- fn ensure_channels(&mut self, need: usize) {240+ fn ensure_channels(&mut self, need: usize, downsampled: bool) {
241 while self.channels.len() < need {241 while self.channels.len() < need {
242 let seed = 0x1234_5678u32.wrapping_add(self.channels.len() as u32 * 0x9e37_79b9);242 let seed = 0x1234_5678u32.wrapping_add(self.channels.len() as u32 * 0x9e37_79b9);
243- self.channels.push(ChannelState::new(1, seed));243+ let mut channel = ChannelState::new(1, seed);
244+ if downsampled {
245+ channel.synthesis = Synthesis::downsampled();
246+ }
247+ self.channels.push(channel);
244 }248 }
245 }249 }
246 }250 }
@@ -254,6 +258,7 @@ const PS_SLOTS: usize = 32;
254 /// encoder in practice emits it).258 /// encoder in practice emits it).
255 pub struct SbrChain {259 pub struct SbrChain {
256 rate: u32,260 rate: u32,
261+ downsampled: bool,
257 /// PS is signalled by the config (explicit HE-AACv2).262 /// PS is signalled by the config (explicit HE-AACv2).
258 ps_config: bool,263 ps_config: bool,
259 // Keyed by (element_instance_tag, is_cpe): SCE/LFE and CPE instance264 // Keyed by (element_instance_tag, is_cpe): SCE/LFE and CPE instance
@@ -272,12 +277,17 @@ impl SbrChain {
272 pub fn new(rate: u32) -> SbrChain {277 pub fn new(rate: u32) -> SbrChain {
273 SbrChain {278 SbrChain {
274 rate,279 rate,
280+ downsampled: false,
275 ps_config: false,281 ps_config: false,
276 elements: HashMap::new(),282 elements: HashMap::new(),
277 bypass: HashMap::new(),283 bypass: HashMap::new(),
278 }284 }
279 }285 }
280 286
287+ pub fn set_downsampled(&mut self, on: bool) {
288+ self.downsampled = on;
289+ }
290+
281 /// Marks PS as signalled by the config: the element's output is stereo.291 /// Marks PS as signalled by the config: the element's output is stereo.
282 pub fn set_ps_config(&mut self, on: bool) {292 pub fn set_ps_config(&mut self, on: bool) {
283 self.ps_config = on;293 self.ps_config = on;
@@ -289,13 +299,13 @@ impl SbrChain {
289 /// Parses one FIL element's `sbr_extension_data` (the reader299 /// Parses one FIL element's `sbr_extension_data` (the reader
290 /// positioned right past `bs_extension_type`) for element `tag`.300 /// positioned right past `bs_extension_type`) for element `tag`.
291 pub fn parse(&mut self, r: &mut BitReader<'_>, tag: u8, is_cpe: bool) -> Option<SbrData> {301 pub fn parse(&mut self, r: &mut BitReader<'_>, tag: u8, is_cpe: bool) -> Option<SbrData> {
292- let ps_config = self.ps_config;302+ let (ps_config, downsampled) = (self.ps_config, self.downsampled);
293 let elem = self.element(tag, is_cpe);303 let elem = self.element(tag, is_cpe);
294 let data = elem.parser.parse(r, is_cpe).ok()?;304 let data = elem.parser.parse(r, is_cpe).ok()?;
295 let n_q = elem.parser.tables().map(|t| t.n_q).unwrap_or(1);305 let n_q = elem.parser.tables().map(|t| t.n_q).unwrap_or(1);
296 let ps_out = data.ps.is_some() || (ps_config && !is_cpe);306 let ps_out = data.ps.is_some() || (ps_config && !is_cpe);
297 let need = data.channels.len().max(if ps_out { 2 } else { 1 });307 let need = data.channels.len().max(if ps_out { 2 } else { 1 });
298- elem.ensure_channels(need);308+ elem.ensure_channels(need, downsampled);
299 for ch in &mut elem.channels {309 for ch in &mut elem.channels {
300 if ch.chirp.bands() != n_q {310 if ch.chirp.bands() != n_q {
301 ch.chirp = ChirpState::new(n_q);311 ch.chirp = ChirpState::new(n_q);
@@ -321,8 +331,9 @@ impl SbrChain {
321 331
322 fn apply_low_only(&mut self, tag: u8, is_cpe: bool, planes: &mut [Vec<f32>]) {332 fn apply_low_only(&mut self, tag: u8, is_cpe: bool, planes: &mut [Vec<f32>]) {
323 let dup = !is_cpe && planes.len() == 2;333 let dup = !is_cpe && planes.len() == 2;
334+ let downsampled = self.downsampled;
324 let elem = self.element(tag, is_cpe);335 let elem = self.element(tag, is_cpe);
325- elem.ensure_channels(planes.len());336+ elem.ensure_channels(planes.len(), downsampled);
326 let mut right_synth = if dup {337 let mut right_synth = if dup {
327 Some(std::mem::take(&mut elem.channels[1].synthesis))338 Some(std::mem::take(&mut elem.channels[1].synthesis))
328 } else {339 } else {
@@ -375,13 +386,19 @@ impl SbrChain {
375 /// planes, with the high bands left empty, which is what ffmpeg's output386 /// planes, with the high bands left empty, which is what ffmpeg's output
376 /// does for the LFE channel.387 /// does for the LFE channel.
377 pub fn bypass_plane(&mut self, tag: u8, plane: &mut Vec<f32>) {388 pub fn bypass_plane(&mut self, tag: u8, plane: &mut Vec<f32>) {
378- let (state, delay) = self389+ let downsampled = self.downsampled;
379- .bypass390+ let (state, delay) = self.bypass.entry(tag).or_insert_with(|| {
380- .entry(tag)391+ let mut state = ChannelState::new(1, 0);
381- .or_insert_with(|| (ChannelState::new(1, 0), vec![0.0; BYPASS_DELAY]));392+ let mut delay = BYPASS_DELAY;
393+ if downsampled {
394+ state.synthesis = Synthesis::downsampled();
395+ delay /= 2;
396+ }
397+ (state, vec![0.0; delay])
398+ });
382 let num_slots = plane.len() / ANALYSIS_BANDS;399 let num_slots = plane.len() / ANALYSIS_BANDS;
383 let mut out = std::mem::take(delay);400 let mut out = std::mem::take(delay);
384- out.reserve(num_slots * SYNTHESIS_BANDS);401+ out.reserve(num_slots * state.synthesis.bands());
385 for slot in 0..num_slots {402 for slot in 0..num_slots {
386 let mut chunk = [0f32; ANALYSIS_BANDS];403 let mut chunk = [0f32; ANALYSIS_BANDS];
387 chunk.copy_from_slice(&plane[slot * ANALYSIS_BANDS..(slot + 1) * ANALYSIS_BANDS]);404 chunk.copy_from_slice(&plane[slot * ANALYSIS_BANDS..(slot + 1) * ANALYSIS_BANDS]);
@@ -397,7 +414,7 @@ impl SbrChain {
397 }414 }
398 // The reconstructed planes lag their raw analysis by `HF_ADJ` slots;415 // The reconstructed planes lag their raw analysis by `HF_ADJ` slots;
399 // hold the bypass output back by the same amount so they line up.416 // hold the bypass output back by the same amount so they line up.
400- let produced = num_slots * SYNTHESIS_BANDS;417+ let produced = num_slots * state.synthesis.bands();
401 *plane = out[..produced].to_vec();418 *plane = out[..produced].to_vec();
402 *delay = out[produced..].to_vec();419 *delay = out[produced..].to_vec();
403 }420 }
@@ -488,6 +505,7 @@ impl SbrChain {
488 if source == "hold" {505 if source == "hold" {
489 HOLD_CALLS.fetch_add(1, Ordering::Relaxed);506 HOLD_CALLS.fetch_add(1, Ordering::Relaxed);
490 }507 }
508+ let band_limit = if self.downsampled { ANALYSIS_BANDS } else { SYNTHESIS_BANDS };
491 let Some(elem) = self.elements.get_mut(&(tag, is_cpe)) else {509 let Some(elem) = self.elements.get_mut(&(tag, is_cpe)) else {
492 return Ok(());510 return Ok(());
493 };511 };
@@ -705,7 +723,7 @@ impl SbrChain {
705 }723 }
706 724
707 let kx = (tables.kx as usize).min(ANALYSIS_BANDS);725 let kx = (tables.kx as usize).min(ANALYSIS_BANDS);
708- let k2 = (tables.k2 as usize).min(SYNTHESIS_BANDS);726+ let k2 = (tables.k2 as usize).min(band_limit);
709 let mut vs: Vec<[Complex<f64>; SYNTHESIS_BANDS]> = Vec::with_capacity(num_slots);727 let mut vs: Vec<[Complex<f64>; SYNTHESIS_BANDS]> = Vec::with_capacity(num_slots);
710 for slot in 0..num_slots {728 for slot in 0..num_slots {
711 let mut v = [Complex::ZERO; SYNTHESIS_BANDS];729 let mut v = [Complex::ZERO; SYNTHESIS_BANDS];
@@ -843,7 +861,8 @@ fn render_ps(
843 861
844 /// Runs `slots` through a synthesis bank, at the SBR output scale.862 /// Runs `slots` through a synthesis bank, at the SBR output scale.
845 fn synth_slots(synth: &mut Synthesis, slots: &[[Complex<f64>; SYNTHESIS_BANDS]]) -> Vec<f32> {863 fn synth_slots(synth: &mut Synthesis, slots: &[[Complex<f64>; SYNTHESIS_BANDS]]) -> Vec<f32> {
846- let mut out = Vec::with_capacity(slots.len() * SYNTHESIS_BANDS);864+ let bands = synth.bands();
865+ let mut out = Vec::with_capacity(slots.len() * bands);
847 for v in slots {866 for v in slots {
848 let mut pcm = synth.process_slot(v);867 let mut pcm = synth.process_slot(v);
849 for s in &mut pcm {868 for s in &mut pcm {
@@ -852,7 +871,7 @@ fn synth_slots(synth: &mut Synthesis, slots: &[[Complex<f64>; SYNTHESIS_BANDS]])
852 // core's amplitude; the 2x is the upsampler's gain.871 // core's amplitude; the 2x is the upsampler's gain.
853 *s *= 2.0 * crate::decode::OUTPUT_SCALE;872 *s *= 2.0 * crate::decode::OUTPUT_SCALE;
854 }873 }
855- out.extend_from_slice(&pcm);874+ out.extend_from_slice(&pcm[..bands]);
856 }875 }
857 out876 out
858 }877 }
crates/ec-aac/src/sbr_qmf.rsmodified+39 -41
@@ -218,70 +218,68 @@ impl Default for HfAnalysis {
218 }218 }
219 }219 }
220 220
221-/// The ISO/IEC 14496-3 4.6.18.8.2 64-band complex synthesis QMF bank:
222-/// complex subband slots in, doubled-rate PCM out, [`SYNTHESIS_BANDS`]
223-/// output samples per slot. Verified against the literal spec equations at
224-/// `corr=0.999999` self-consistency (see the module doc).
225 pub struct Synthesis {221 pub struct Synthesis {
226- /// History buffer `v`, `10 * PROTO_LEN / SYNTHESIS_BANDS * SYNTHESIS_BANDS`222+ bands: usize,
227- /// = `1280` samples (`4.6.18.8.2`'s own length, not `PROTO_LEN`).223+ v: Vec<f64>,
228- v: [f64; 1280],
229 cos_tab: Vec<f64>,224 cos_tab: Vec<f64>,
230 sin_tab: Vec<f64>,225 sin_tab: Vec<f64>,
231 }226 }
232 227
233 impl Synthesis {228 impl Synthesis {
234- /// A fresh bank with a silent history buffer.
235 pub fn new() -> Synthesis {229 pub fn new() -> Synthesis {
236- let mut cos_tab = vec![0.0f64; SYNTHESIS_BANDS * 128];230+ Synthesis::with_bands(SYNTHESIS_BANDS, 0.0)
237- let mut sin_tab = vec![0.0f64; SYNTHESIS_BANDS * 128];231+ }
238- for k in 0..SYNTHESIS_BANDS {232+
239- for n in 0..128 {233+ pub fn downsampled() -> Synthesis {
240- // phi = pi/128*(k+0.5)*(2n-255)234+ Synthesis::with_bands(SYNTHESIS_BANDS / 2, -0.5)
241- let phi = std::f64::consts::PI / 128.0235+ }
236+
237+ fn with_bands(bands: usize, shift: f64) -> Synthesis {
238+ let len = 2 * bands;
239+ let mut cos_tab = vec![0.0f64; bands * len];
240+ let mut sin_tab = vec![0.0f64; bands * len];
241+ for k in 0..bands {
242+ for n in 0..len {
243+ let phi = std::f64::consts::PI / len as f64
242 * (k as f64 + 0.5)244 * (k as f64 + 0.5)
243- * (2.0 * n as f64 - 255.0);245+ * (2.0 * n as f64 - (2 * len - 1) as f64 + shift);
244- cos_tab[k * 128 + n] = phi.cos();246+ cos_tab[k * len + n] = phi.cos();
245- sin_tab[k * 128 + n] = phi.sin();247+ sin_tab[k * len + n] = phi.sin();
246 }248 }
247 }249 }
248 Synthesis {250 Synthesis {
249- v: [0.0; 1280],251+ bands,
252+ v: vec![0.0; 10 * len],
250 cos_tab,253 cos_tab,
251 sin_tab,254 sin_tab,
252 }255 }
253 }256 }
254 257
255- /// Feeds one slot of [`SYNTHESIS_BANDS`] complex subband samples and258+ pub fn bands(&self) -> usize {
256- /// returns [`SYNTHESIS_BANDS`] output PCM samples.259+ self.bands
260+ }
261+
257 pub fn process_slot(&mut self, x: &[Complex<f64>; SYNTHESIS_BANDS]) -> [f32; SYNTHESIS_BANDS] {262 pub fn process_slot(&mut self, x: &[Complex<f64>; SYNTHESIS_BANDS]) -> [f32; SYNTHESIS_BANDS] {
258- // v[n] = v[n-128] for n=1279..128 (shift), then v[0..128] filled with263+ let bands = self.bands;
259- // v[n] = Re{ (1/64) sum_k X[k]*exp(j*phi) }, n=0..127.264+ let len = 2 * bands;
260- self.v.copy_within(0..1152, 128);265+ self.v.copy_within(0..9 * len, len);
261- for n in 0..128 {266+ for n in 0..len {
262 let mut re = 0.0f64;267 let mut re = 0.0f64;
263- for (k, xk) in x.iter().enumerate() {268+ for (k, xk) in x.iter().take(bands).enumerate() {
264- let row = k * 128;269+ let at = k * len + n;
265- // Re{X[k]*exp(j*phi)} = Xre*cos(phi) - Xim*sin(phi)270+ re += xk.re * self.cos_tab[at] - xk.im * self.sin_tab[at];
266- re += xk.re * self.cos_tab[row + n] - xk.im * self.sin_tab[row + n];
267 }271 }
268 self.v[n] = re / SYNTHESIS_BANDS as f64;272 self.v[n] = re / SYNTHESIS_BANDS as f64;
269 }273 }
270- // g[128i+n] = v[256i+n], g[128i+64+n] = v[256i+192+n], i=0..4, n=0..63274+ let step = SYNTHESIS_BANDS / bands;
271- let mut g = [0.0f64; PROTO_LEN];
272- for i in 0..5 {
273- for n in 0..64 {
274- g[128 * i + n] = self.v[256 * i + n];
275- g[128 * i + 64 + n] = self.v[256 * i + 192 + n];
276- }
277- }
278- // w[n] = g[n]*c[n], n=0..639; y[n] = sum_{k=0}^{9} w[64k+n], n=0..63
279 let mut out = [0.0f32; SYNTHESIS_BANDS];275 let mut out = [0.0f32; SYNTHESIS_BANDS];
280- for (n, o) in out.iter_mut().enumerate() {276+ for (n, o) in out.iter_mut().take(bands).enumerate() {
281 let mut acc = 0.0f64;277 let mut acc = 0.0f64;
282- for k in 0..10 {278+ for i in 0..5 {
283- let idx = 64 * k + n;279+ let near = self.v[2 * len * i + n];
284- acc += g[idx] * QMF_WINDOW[idx];280+ let far = self.v[2 * len * i + 3 * bands + n];
281+ acc += near * QMF_WINDOW[step * (len * i + n)];
282+ acc += far * QMF_WINDOW[step * (len * i + bands + n)];
285 }283 }
286 *o = acc as f32;284 *o = acc as f32;
287 }285 }