forge
Explore
Log in

Don't you dare go Hollow

ec-aac SBR: AUs before the first sbr_header stayed at core rate (1024/ch) instead of QMF pass-through at 2048 like ffmpeg (sbr->start=0, kx=32, m=0), shifting every later sample; al_sbr_ps_06 ours 325632 vs ffmpeg 434176 = exactly 106 AUs*1024 (first header at AU 106), cm_48_2 first header at AU ~28 -> lag >20000, corr 0.037. Fix: SbrChain::apply_low_only (analysis, 6-slot delay, 64-band synthesis, kx=32) when no SBR data was ever parsed; chirp state resized to n_q on parse. Also: sbr_header no longer zeroes DT state (ffmpeg keeps env_facs_q across headers), VARFIX/FIXVAR/VARVAR 1-envelope grids accepted (ffmpeg only rejects >5)

Browse files at this commit

a657a423b9b5ab3c2278d739a4e72ca91c6407fcAhmet Kaan GümüşParent: f958102
crates/ec-aac/src/decode.rsmodified+1 -1
@@ -986,7 +986,7 @@ impl BlockDecoder {
986986 }
987987 }
988988 }
989- if !applied {
989+ if !applied && !arming {
990990 // No fresh SBR payload this frame (no SBR fill
991991 // element, or one whose extension_type wasn't
992992 // SBR, or a parse failure): §4.6.18 still
crates/ec-aac/src/sbr_chain.rsmodified+55 -10
@@ -236,6 +236,15 @@ struct Element {
236236 ps_engine: PsEngine,
237237 }
238238
239+impl Element {
240+ fn ensure_channels(&mut self, need: usize) {
241+ while self.channels.len() < need {
242+ let seed = 0x1234_5678u32.wrapping_add(self.channels.len() as u32 * 0x9e37_79b9);
243+ self.channels.push(ChannelState::new(1, seed));
244+ }
245+ }
246+}
247+
239248 /// QMF slots per SBR frame: the PS path runs only for the 32-slot frames
240249 /// that AAC-LC's 1024-sample frames produce.
241250 const PS_SLOTS: usize = 32;
@@ -280,8 +289,22 @@ impl SbrChain {
280289 /// Parses one FIL element's `sbr_extension_data` (the reader
281290 /// positioned right past `bs_extension_type`) for element `tag`.
282291 pub fn parse(&mut self, r: &mut BitReader<'_>, tag: u8, is_cpe: bool) -> Option<SbrData> {
292+ let elem = self.element(tag, is_cpe);
293+ let data = elem.parser.parse(r, is_cpe).ok()?;
294+ let n_q = elem.parser.tables().map(|t| t.n_q).unwrap_or(1);
295+ let need = data.channels.len().max(if data.ps.is_some() { 2 } else { 1 });
296+ elem.ensure_channels(need);
297+ for ch in &mut elem.channels {
298+ if ch.chirp.bands() != n_q {
299+ ch.chirp = ChirpState::new(n_q);
300+ }
301+ }
302+ Some(data)
303+ }
304+
305+ fn element(&mut self, tag: u8, is_cpe: bool) -> &mut Element {
283306 let (rate, ps_config) = (self.rate, self.ps_config);
284- let elem = self.elements.entry((tag, is_cpe)).or_insert_with(|| {
307+ self.elements.entry((tag, is_cpe)).or_insert_with(|| {
285308 let mut parser = SbrParser::new(rate);
286309 parser.set_ps_config(ps_config);
287310 Element {
@@ -291,16 +314,37 @@ impl SbrChain {
291314 last_header: None,
292315 ps_engine: PsEngine::new(),
293316 }
294- });
295- let data = elem.parser.parse(r, is_cpe).ok()?;
296- let n_q = elem.parser.tables().map(|t| t.n_q).unwrap_or(1);
297- // PS needs a second synthesis state: the right output channel.
298- let need = data.channels.len().max(if data.ps.is_some() { 2 } else { 1 });
299- while elem.channels.len() < need {
300- let seed = 0x1234_5678u32.wrapping_add(elem.channels.len() as u32 * 0x9e37_79b9);
301- elem.channels.push(ChannelState::new(n_q, seed));
317+ })
318+ }
319+
320+ fn apply_low_only(&mut self, tag: u8, is_cpe: bool, planes: &mut [Vec<f32>]) {
321+ let elem = self.element(tag, is_cpe);
322+ elem.ensure_channels(planes.len());
323+ for (plane, state) in planes.iter_mut().zip(elem.channels.iter_mut()) {
324+ let num_slots = plane.len() / ANALYSIS_BANDS;
325+ let mut raw = Vec::with_capacity(num_slots);
326+ for slot in 0..num_slots {
327+ let mut chunk = [0f32; ANALYSIS_BANDS];
328+ chunk.copy_from_slice(&plane[slot * ANALYSIS_BANDS..(slot + 1) * ANALYSIS_BANDS]);
329+ for s in &mut chunk {
330+ *s /= crate::decode::OUTPUT_SCALE;
331+ }
332+ raw.push(state.analysis.process_slot(&chunk));
333+ }
334+ let mut delayed = state.low_tail.clone();
335+ delayed.extend_from_slice(&raw);
336+ state.low_tail = delayed[delayed.len() - HF_ADJ..].to_vec();
337+ delayed.truncate(num_slots);
338+ let vs: Vec<[Complex<f64>; SYNTHESIS_BANDS]> = delayed
339+ .iter()
340+ .map(|sub| {
341+ let mut v = [Complex::ZERO; SYNTHESIS_BANDS];
342+ v[..ANALYSIS_BANDS].copy_from_slice(sub);
343+ v
344+ })
345+ .collect();
346+ *plane = synth_slots(&mut state.synthesis, &vs);
302347 }
303- Some(data)
304348 }
305349
306350 /// Doubles the rate of an LFE plane. The standard gives the LFE no SBR
@@ -405,6 +449,7 @@ impl SbrChain {
405449 .get(&(tag, is_cpe))
406450 .and_then(|e| e.last_data.clone())
407451 else {
452+ self.apply_low_only(tag, is_cpe, planes);
408453 return Ok(());
409454 };
410455 self.apply_data(tag, is_cpe, &data, planes, "hold")
crates/ec-aac/src/sbr_hf.rsmodified+4 -0
@@ -277,6 +277,10 @@ impl ChirpState {
277277 }
278278 }
279279
280+ pub fn bands(&self) -> usize {
281+ self.bw.len()
282+ }
283+
280284 /// Advances the state one frame given this frame's `bs_invf_mode` per
281285 /// noise band, returning the smoothed bw to use for HF generation.
282286 ///
crates/ec-aac/src/sbr_payload.rsmodified+2 -3
@@ -319,7 +319,7 @@ impl SbrParser {
319319 let var_bord = i64::from(r.read_bits(2)?);
320320 let num_rel = r.read_bits(2)? as usize;
321321 let num_env = num_rel + 1;
322- if !(2..=5).contains(&num_env) {
322+ if !(1..=5).contains(&num_env) {
323323 return Err(Error::corrupt(
324324 "sbr: variable grid envelope count out of range",
325325 ));
@@ -369,7 +369,7 @@ impl SbrParser {
369369 let num_rel0 = r.read_bits(2)? as usize;
370370 let num_rel1 = r.read_bits(2)? as usize;
371371 let num_env = num_rel0 + num_rel1 + 1;
372- if !(2..=5).contains(&num_env) {
372+ if !(1..=5).contains(&num_env) {
373373 return Err(Error::corrupt("sbr VARVAR: envelope count out of range"));
374374 }
375375 let mut rel0 = Vec::with_capacity(num_rel0);
@@ -536,7 +536,6 @@ impl SbrParser {
536536 smoothing_mode,
537537 });
538538 self.tables = Some(tables);
539- self.state = Default::default();
540539 } else if self.header.is_none() {
541540 return Err(Error::corrupt("sbr: headerless frame with no prior header"));
542541 }