From 90f24267e2d8cce4442441c0c85edab778ec0e7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 11:27:15 +0300 Subject: [PATCH] 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 --- crates/ec-aac/src/sbr_chain.rs | 40 ++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/crates/ec-aac/src/sbr_chain.rs b/crates/ec-aac/src/sbr_chain.rs index 0984ea77..e2378ebb 100644 --- a/crates/ec-aac/src/sbr_chain.rs +++ b/crates/ec-aac/src/sbr_chain.rs @@ -289,10 +289,12 @@ 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 ps_config = self.ps_config; 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 }); + let ps_out = data.ps.is_some() || (ps_config && !is_cpe); + let need = data.channels.len().max(if ps_out { 2 } else { 1 }); elem.ensure_channels(need); for ch in &mut elem.channels { if ch.chirp.bands() != n_q { @@ -318,9 +320,19 @@ impl SbrChain { } fn apply_low_only(&mut self, tag: u8, is_cpe: bool, planes: &mut [Vec]) { + let dup = !is_cpe && planes.len() == 2; let elem = self.element(tag, is_cpe); elem.ensure_channels(planes.len()); + let mut right_synth = if dup { + Some(std::mem::take(&mut elem.channels[1].synthesis)) + } else { + None + }; + let mut right_plane = None; for (plane, state) in planes.iter_mut().zip(elem.channels.iter_mut()) { + if right_plane.is_some() { + break; + } let num_slots = plane.len() / ANALYSIS_BANDS; let mut raw = Vec::with_capacity(num_slots); for slot in 0..num_slots { @@ -344,6 +356,15 @@ impl SbrChain { }) .collect(); *plane = synth_slots(&mut state.synthesis, &vs); + if let Some(rs) = right_synth.as_mut() { + right_plane = Some(synth_slots(rs, &vs)); + } + } + if let Some(rs) = right_synth { + elem.channels[1].synthesis = rs; + } + if let Some(right) = right_plane { + planes[1] = right; } } @@ -394,7 +415,7 @@ impl SbrChain { start: usize, n: usize, ) -> crate::Result<()> { - let n = expand_for_ps(out, start, n, is_cpe, data.ps.is_some()); + let n = expand_for_ps(out, start, n, is_cpe, data.ps.is_some() || self.ps_config); self.apply(tag, is_cpe, data, &mut out[start..start + n]) } @@ -411,7 +432,8 @@ impl SbrChain { .elements .get(&(tag, is_cpe)) .and_then(|e| e.last_data.as_ref()) - .is_some_and(|d| d.ps.is_some()); + .is_some_and(|d| d.ps.is_some()) + || self.ps_config; let n = expand_for_ps(out, start, n, is_cpe, has_ps); self.apply_last(tag, is_cpe, &mut out[start..start + n]) } @@ -488,7 +510,8 @@ impl SbrChain { // through channel 1's (the right output), so both stay in step. let ps_mode = !is_cpe && planes.len() >= 2 && data.ps.is_some(); let mut ps_planes: Option<(Vec, Vec)> = None; - let mut r_synth = if ps_mode && elem.channels.len() >= 2 { + let dup_mode = !is_cpe && planes.len() >= 2 && data.ps.is_none(); + let mut r_synth = if (ps_mode || dup_mode) && elem.channels.len() >= 2 { Some(std::mem::take(&mut elem.channels[1].synthesis)) } else { None @@ -708,6 +731,15 @@ impl SbrChain { ps_planes = Some((left_pcm, right_pcm)); Vec::new() } + _ if dup_mode => { + let left_pcm = synth_slots(&mut state.synthesis, &vs); + let right_pcm = match r_synth.as_mut() { + Some(rs) => synth_slots(rs, &vs), + None => left_pcm.clone(), + }; + ps_planes = Some((left_pcm, right_pcm)); + Vec::new() + } _ => synth_slots(&mut state.synthesis, &vs), }; if std::env::var("EC_AAC_SBR_PREQMF_DUMP").is_ok() {