diff --git a/crates/ec-aac/src/decode.rs b/crates/ec-aac/src/decode.rs index 89681d85..e782f647 100644 --- a/crates/ec-aac/src/decode.rs +++ b/crates/ec-aac/src/decode.rs @@ -986,7 +986,7 @@ impl BlockDecoder { } } } - if !applied { + if !applied && !arming { // No fresh SBR payload this frame (no SBR fill // element, or one whose extension_type wasn't // SBR, or a parse failure): ยง4.6.18 still diff --git a/crates/ec-aac/src/sbr_chain.rs b/crates/ec-aac/src/sbr_chain.rs index a6032a87..0984ea77 100644 --- a/crates/ec-aac/src/sbr_chain.rs +++ b/crates/ec-aac/src/sbr_chain.rs @@ -236,6 +236,15 @@ struct Element { ps_engine: PsEngine, } +impl Element { + fn ensure_channels(&mut self, need: usize) { + while self.channels.len() < need { + let seed = 0x1234_5678u32.wrapping_add(self.channels.len() as u32 * 0x9e37_79b9); + self.channels.push(ChannelState::new(1, seed)); + } + } +} + /// QMF slots per SBR frame: the PS path runs only for the 32-slot frames /// that AAC-LC's 1024-sample frames produce. const PS_SLOTS: usize = 32; @@ -280,8 +289,22 @@ impl SbrChain { /// Parses one FIL element's `sbr_extension_data` (the reader /// positioned right past `bs_extension_type`) for element `tag`. pub fn parse(&mut self, r: &mut BitReader<'_>, tag: u8, is_cpe: bool) -> Option { + let elem = self.element(tag, is_cpe); + let data = elem.parser.parse(r, is_cpe).ok()?; + let n_q = elem.parser.tables().map(|t| t.n_q).unwrap_or(1); + let need = data.channels.len().max(if data.ps.is_some() { 2 } else { 1 }); + elem.ensure_channels(need); + for ch in &mut elem.channels { + if ch.chirp.bands() != n_q { + ch.chirp = ChirpState::new(n_q); + } + } + Some(data) + } + + fn element(&mut self, tag: u8, is_cpe: bool) -> &mut Element { let (rate, ps_config) = (self.rate, self.ps_config); - let elem = self.elements.entry((tag, is_cpe)).or_insert_with(|| { + self.elements.entry((tag, is_cpe)).or_insert_with(|| { let mut parser = SbrParser::new(rate); parser.set_ps_config(ps_config); Element { @@ -291,16 +314,37 @@ impl SbrChain { last_header: None, ps_engine: PsEngine::new(), } - }); - let data = elem.parser.parse(r, is_cpe).ok()?; - let n_q = elem.parser.tables().map(|t| t.n_q).unwrap_or(1); - // PS needs a second synthesis state: the right output channel. - let need = data.channels.len().max(if data.ps.is_some() { 2 } else { 1 }); - while elem.channels.len() < need { - let seed = 0x1234_5678u32.wrapping_add(elem.channels.len() as u32 * 0x9e37_79b9); - elem.channels.push(ChannelState::new(n_q, seed)); + }) + } + + fn apply_low_only(&mut self, tag: u8, is_cpe: bool, planes: &mut [Vec]) { + let elem = self.element(tag, is_cpe); + elem.ensure_channels(planes.len()); + for (plane, state) in planes.iter_mut().zip(elem.channels.iter_mut()) { + let num_slots = plane.len() / 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]); + for s in &mut chunk { + *s /= crate::decode::OUTPUT_SCALE; + } + raw.push(state.analysis.process_slot(&chunk)); + } + let mut delayed = state.low_tail.clone(); + delayed.extend_from_slice(&raw); + state.low_tail = delayed[delayed.len() - HF_ADJ..].to_vec(); + delayed.truncate(num_slots); + let vs: Vec<[Complex; SYNTHESIS_BANDS]> = delayed + .iter() + .map(|sub| { + let mut v = [Complex::ZERO; SYNTHESIS_BANDS]; + v[..ANALYSIS_BANDS].copy_from_slice(sub); + v + }) + .collect(); + *plane = synth_slots(&mut state.synthesis, &vs); } - Some(data) } /// Doubles the rate of an LFE plane. The standard gives the LFE no SBR @@ -405,6 +449,7 @@ impl SbrChain { .get(&(tag, is_cpe)) .and_then(|e| e.last_data.clone()) else { + self.apply_low_only(tag, is_cpe, planes); return Ok(()); }; self.apply_data(tag, is_cpe, &data, planes, "hold") diff --git a/crates/ec-aac/src/sbr_hf.rs b/crates/ec-aac/src/sbr_hf.rs index 7d605cf4..7420658f 100644 --- a/crates/ec-aac/src/sbr_hf.rs +++ b/crates/ec-aac/src/sbr_hf.rs @@ -277,6 +277,10 @@ impl ChirpState { } } + pub fn bands(&self) -> usize { + self.bw.len() + } + /// Advances the state one frame given this frame's `bs_invf_mode` per /// noise band, returning the smoothed bw to use for HF generation. /// diff --git a/crates/ec-aac/src/sbr_payload.rs b/crates/ec-aac/src/sbr_payload.rs index fa58f27f..18134d42 100644 --- a/crates/ec-aac/src/sbr_payload.rs +++ b/crates/ec-aac/src/sbr_payload.rs @@ -319,7 +319,7 @@ impl SbrParser { let var_bord = i64::from(r.read_bits(2)?); let num_rel = r.read_bits(2)? as usize; let num_env = num_rel + 1; - if !(2..=5).contains(&num_env) { + if !(1..=5).contains(&num_env) { return Err(Error::corrupt( "sbr: variable grid envelope count out of range", )); @@ -369,7 +369,7 @@ impl SbrParser { let num_rel0 = r.read_bits(2)? as usize; let num_rel1 = r.read_bits(2)? as usize; let num_env = num_rel0 + num_rel1 + 1; - if !(2..=5).contains(&num_env) { + if !(1..=5).contains(&num_env) { return Err(Error::corrupt("sbr VARVAR: envelope count out of range")); } let mut rel0 = Vec::with_capacity(num_rel0); @@ -536,7 +536,6 @@ impl SbrParser { smoothing_mode, }); self.tables = Some(tables); - self.state = Default::default(); } else if self.header.is_none() { return Err(Error::corrupt("sbr: headerless frame with no prior header")); }