ec-aac SBR: explicit-PS mono elements stayed 1 plane until a PS payload arrived, so pre-header AUs (and AUs without PS data) output mono while later AUs output stereo: interleave/channel count flipped, ps_06 lag -19877/-100021 at search bounds, ps_04 corr 0.96. ffmpeg (ff_aac_sbr_apply: m4ac.ps==1 -> nch=2, X[1]=X[0] when ps.start==0) always outputs 2 planes. Fix: expand_for_ps when ps_config; apply_low_only and apply_data synthesize the same slots through both channel states (dup). Delay path itself already matches (6-slot low delay = ENVELOPE_ADJUSTMENT_OFFSET gap, kx=32). Evidence: source reading + test log (cm_48_2 lag -961 is the normal QMF delay vs core, not a regression); cargo check -p ec-aac --all-targets clean; tests not run
Browse files at this commit90f24267e2d8cce4442441c0c85edab778ec0e7bParent: 386140a1 changed file +36 -4
crates/ec-aac/src/sbr_chain.rsmodified+36 -4
| @@ -289,10 +289,12 @@ impl SbrChain { | |||
| 289 | /// Parses one FIL element's `sbr_extension_data` (the reader | 289 | /// Parses one FIL element's `sbr_extension_data` (the reader |
| 290 | /// positioned right past `bs_extension_type`) for element `tag`. | 290 | /// 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> { | 291 | pub fn parse(&mut self, r: &mut BitReader<'_>, tag: u8, is_cpe: bool) -> Option<SbrData> { |
| 292 | + let ps_config = self.ps_config; | ||
| 292 | let elem = self.element(tag, is_cpe); | 293 | let elem = self.element(tag, is_cpe); |
| 293 | let data = elem.parser.parse(r, is_cpe).ok()?; | 294 | 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 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 | + let ps_out = data.ps.is_some() || (ps_config && !is_cpe); |
| 297 | + let need = data.channels.len().max(if ps_out { 2 } else { 1 }); | ||
| 296 | elem.ensure_channels(need); | 298 | elem.ensure_channels(need); |
| 297 | for ch in &mut elem.channels { | 299 | for ch in &mut elem.channels { |
| 298 | if ch.chirp.bands() != n_q { | 300 | if ch.chirp.bands() != n_q { |
| @@ -318,9 +320,19 @@ impl SbrChain { | |||
| 318 | } | 320 | } |
| 319 | 321 | ||
| 320 | fn apply_low_only(&mut self, tag: u8, is_cpe: bool, planes: &mut [Vec<f32>]) { | 322 | fn apply_low_only(&mut self, tag: u8, is_cpe: bool, planes: &mut [Vec<f32>]) { |
| 323 | + let dup = !is_cpe && planes.len() == 2; | ||
| 321 | let elem = self.element(tag, is_cpe); | 324 | let elem = self.element(tag, is_cpe); |
| 322 | elem.ensure_channels(planes.len()); | 325 | elem.ensure_channels(planes.len()); |
| 326 | + let mut right_synth = if dup { | ||
| 327 | + Some(std::mem::take(&mut elem.channels[1].synthesis)) | ||
| 328 | + } else { | ||
| 329 | + None | ||
| 330 | + }; | ||
| 331 | + let mut right_plane = None; | ||
| 323 | for (plane, state) in planes.iter_mut().zip(elem.channels.iter_mut()) { | 332 | for (plane, state) in planes.iter_mut().zip(elem.channels.iter_mut()) { |
| 333 | + if right_plane.is_some() { | ||
| 334 | + break; | ||
| 335 | + } | ||
| 324 | let num_slots = plane.len() / ANALYSIS_BANDS; | 336 | let num_slots = plane.len() / ANALYSIS_BANDS; |
| 325 | let mut raw = Vec::with_capacity(num_slots); | 337 | let mut raw = Vec::with_capacity(num_slots); |
| 326 | for slot in 0..num_slots { | 338 | for slot in 0..num_slots { |
| @@ -344,6 +356,15 @@ impl SbrChain { | |||
| 344 | }) | 356 | }) |
| 345 | .collect(); | 357 | .collect(); |
| 346 | *plane = synth_slots(&mut state.synthesis, &vs); | 358 | *plane = synth_slots(&mut state.synthesis, &vs); |
| 359 | + if let Some(rs) = right_synth.as_mut() { | ||
| 360 | + right_plane = Some(synth_slots(rs, &vs)); | ||
| 361 | + } | ||
| 362 | + } | ||
| 363 | + if let Some(rs) = right_synth { | ||
| 364 | + elem.channels[1].synthesis = rs; | ||
| 365 | + } | ||
| 366 | + if let Some(right) = right_plane { | ||
| 367 | + planes[1] = right; | ||
| 347 | } | 368 | } |
| 348 | } | 369 | } |
| 349 | 370 | ||
| @@ -394,7 +415,7 @@ impl SbrChain { | |||
| 394 | start: usize, | 415 | start: usize, |
| 395 | n: usize, | 416 | n: usize, |
| 396 | ) -> crate::Result<()> { | 417 | ) -> crate::Result<()> { |
| 397 | - let n = expand_for_ps(out, start, n, is_cpe, data.ps.is_some()); | 418 | + let n = expand_for_ps(out, start, n, is_cpe, data.ps.is_some() || self.ps_config); |
| 398 | self.apply(tag, is_cpe, data, &mut out[start..start + n]) | 419 | self.apply(tag, is_cpe, data, &mut out[start..start + n]) |
| 399 | } | 420 | } |
| 400 | 421 | ||
| @@ -411,7 +432,8 @@ impl SbrChain { | |||
| 411 | .elements | 432 | .elements |
| 412 | .get(&(tag, is_cpe)) | 433 | .get(&(tag, is_cpe)) |
| 413 | .and_then(|e| e.last_data.as_ref()) | 434 | .and_then(|e| e.last_data.as_ref()) |
| 414 | - .is_some_and(|d| d.ps.is_some()); | 435 | + .is_some_and(|d| d.ps.is_some()) |
| 436 | + || self.ps_config; | ||
| 415 | let n = expand_for_ps(out, start, n, is_cpe, has_ps); | 437 | let n = expand_for_ps(out, start, n, is_cpe, has_ps); |
| 416 | self.apply_last(tag, is_cpe, &mut out[start..start + n]) | 438 | self.apply_last(tag, is_cpe, &mut out[start..start + n]) |
| 417 | } | 439 | } |
| @@ -488,7 +510,8 @@ impl SbrChain { | |||
| 488 | // through channel 1's (the right output), so both stay in step. | 510 | // through channel 1's (the right output), so both stay in step. |
| 489 | let ps_mode = !is_cpe && planes.len() >= 2 && data.ps.is_some(); | 511 | let ps_mode = !is_cpe && planes.len() >= 2 && data.ps.is_some(); |
| 490 | let mut ps_planes: Option<(Vec<f32>, Vec<f32>)> = None; | 512 | let mut ps_planes: Option<(Vec<f32>, Vec<f32>)> = None; |
| 491 | - let mut r_synth = if ps_mode && elem.channels.len() >= 2 { | 513 | + let dup_mode = !is_cpe && planes.len() >= 2 && data.ps.is_none(); |
| 514 | + let mut r_synth = if (ps_mode || dup_mode) && elem.channels.len() >= 2 { | ||
| 492 | Some(std::mem::take(&mut elem.channels[1].synthesis)) | 515 | Some(std::mem::take(&mut elem.channels[1].synthesis)) |
| 493 | } else { | 516 | } else { |
| 494 | None | 517 | None |
| @@ -708,6 +731,15 @@ impl SbrChain { | |||
| 708 | ps_planes = Some((left_pcm, right_pcm)); | 731 | ps_planes = Some((left_pcm, right_pcm)); |
| 709 | Vec::new() | 732 | Vec::new() |
| 710 | } | 733 | } |
| 734 | + _ if dup_mode => { | ||
| 735 | + let left_pcm = synth_slots(&mut state.synthesis, &vs); | ||
| 736 | + let right_pcm = match r_synth.as_mut() { | ||
| 737 | + Some(rs) => synth_slots(rs, &vs), | ||
| 738 | + None => left_pcm.clone(), | ||
| 739 | + }; | ||
| 740 | + ps_planes = Some((left_pcm, right_pcm)); | ||
| 741 | + Vec::new() | ||
| 742 | + } | ||
| 711 | _ => synth_slots(&mut state.synthesis, &vs), | 743 | _ => synth_slots(&mut state.synthesis, &vs), |
| 712 | }; | 744 | }; |
| 713 | if std::env::var("EC_AAC_SBR_PREQMF_DUMP").is_ok() { | 745 | if std::env::var("EC_AAC_SBR_PREQMF_DUMP").is_ok() { |