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 commita657a423b9b5ab3c2278d739a4e72ca91c6407fcParent: f9581024 changed files +62 -14
- crates/ec-aac/src/decode.rs +1 -1
- crates/ec-aac/src/sbr_chain.rs +55 -10
- crates/ec-aac/src/sbr_hf.rs +4 -0
- crates/ec-aac/src/sbr_payload.rs +2 -3
crates/ec-aac/src/decode.rsmodified+1 -1
| @@ -986,7 +986,7 @@ impl BlockDecoder { | ||
| 986 | 986 | } |
| 987 | 987 | } |
| 988 | 988 | } |
| 989 | - if !applied { | |
| 989 | + if !applied && !arming { | |
| 990 | 990 | // No fresh SBR payload this frame (no SBR fill |
| 991 | 991 | // element, or one whose extension_type wasn't |
| 992 | 992 | // SBR, or a parse failure): §4.6.18 still |
crates/ec-aac/src/sbr_chain.rsmodified+55 -10
| @@ -236,6 +236,15 @@ struct Element { | ||
| 236 | 236 | ps_engine: PsEngine, |
| 237 | 237 | } |
| 238 | 238 | |
| 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 | + | |
| 239 | 248 | /// QMF slots per SBR frame: the PS path runs only for the 32-slot frames |
| 240 | 249 | /// that AAC-LC's 1024-sample frames produce. |
| 241 | 250 | const PS_SLOTS: usize = 32; |
| @@ -280,8 +289,22 @@ impl SbrChain { | ||
| 280 | 289 | /// Parses one FIL element's `sbr_extension_data` (the reader |
| 281 | 290 | /// positioned right past `bs_extension_type`) for element `tag`. |
| 282 | 291 | 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 { | |
| 283 | 306 | 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(|| { | |
| 285 | 308 | let mut parser = SbrParser::new(rate); |
| 286 | 309 | parser.set_ps_config(ps_config); |
| 287 | 310 | Element { |
| @@ -291,16 +314,37 @@ impl SbrChain { | ||
| 291 | 314 | last_header: None, |
| 292 | 315 | ps_engine: PsEngine::new(), |
| 293 | 316 | } |
| 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); | |
| 302 | 347 | } |
| 303 | - Some(data) | |
| 304 | 348 | } |
| 305 | 349 | |
| 306 | 350 | /// Doubles the rate of an LFE plane. The standard gives the LFE no SBR |
| @@ -405,6 +449,7 @@ impl SbrChain { | ||
| 405 | 449 | .get(&(tag, is_cpe)) |
| 406 | 450 | .and_then(|e| e.last_data.clone()) |
| 407 | 451 | else { |
| 452 | + self.apply_low_only(tag, is_cpe, planes); | |
| 408 | 453 | return Ok(()); |
| 409 | 454 | }; |
| 410 | 455 | self.apply_data(tag, is_cpe, &data, planes, "hold") |
crates/ec-aac/src/sbr_hf.rsmodified+4 -0
| @@ -277,6 +277,10 @@ impl ChirpState { | ||
| 277 | 277 | } |
| 278 | 278 | } |
| 279 | 279 | |
| 280 | + pub fn bands(&self) -> usize { | |
| 281 | + self.bw.len() | |
| 282 | + } | |
| 283 | + | |
| 280 | 284 | /// Advances the state one frame given this frame's `bs_invf_mode` per |
| 281 | 285 | /// noise band, returning the smoothed bw to use for HF generation. |
| 282 | 286 | /// |
crates/ec-aac/src/sbr_payload.rsmodified+2 -3
| @@ -319,7 +319,7 @@ impl SbrParser { | ||
| 319 | 319 | let var_bord = i64::from(r.read_bits(2)?); |
| 320 | 320 | let num_rel = r.read_bits(2)? as usize; |
| 321 | 321 | let num_env = num_rel + 1; |
| 322 | - if !(2..=5).contains(&num_env) { | |
| 322 | + if !(1..=5).contains(&num_env) { | |
| 323 | 323 | return Err(Error::corrupt( |
| 324 | 324 | "sbr: variable grid envelope count out of range", |
| 325 | 325 | )); |
| @@ -369,7 +369,7 @@ impl SbrParser { | ||
| 369 | 369 | let num_rel0 = r.read_bits(2)? as usize; |
| 370 | 370 | let num_rel1 = r.read_bits(2)? as usize; |
| 371 | 371 | let num_env = num_rel0 + num_rel1 + 1; |
| 372 | - if !(2..=5).contains(&num_env) { | |
| 372 | + if !(1..=5).contains(&num_env) { | |
| 373 | 373 | return Err(Error::corrupt("sbr VARVAR: envelope count out of range")); |
| 374 | 374 | } |
| 375 | 375 | let mut rel0 = Vec::with_capacity(num_rel0); |
| @@ -536,7 +536,6 @@ impl SbrParser { | ||
| 536 | 536 | smoothing_mode, |
| 537 | 537 | }); |
| 538 | 538 | self.tables = Some(tables); |
| 539 | - self.state = Default::default(); | |
| 540 | 539 | } else if self.header.is_none() { |
| 541 | 540 | return Err(Error::corrupt("sbr: headerless frame with no prior header")); |
| 542 | 541 | } |