From cc3e7cc7ecbd59384fa628b6216e1249d8969de3 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 15:16:22 +0300 Subject: [PATCH] S10: aac raw_data_block FIL arm moves to fill_element (same reads, same SBR calls, same order); the match keeps every arm --- crates/ec-aac/src/decode.rs | 152 +++++++++++++++++++----------------- 1 file changed, 81 insertions(+), 71 deletions(-) diff --git a/crates/ec-aac/src/decode.rs b/crates/ec-aac/src/decode.rs index a74f4ac3..25e061fa 100644 --- a/crates/ec-aac/src/decode.rs +++ b/crates/ec-aac/src/decode.rs @@ -788,6 +788,86 @@ impl BlockDecoder { } } + fn fill_element( + &mut self, + r: &mut BitReader<'_>, + out: &mut Vec>, + pending: &mut Option<(u8, usize, usize, bool)>, + ) -> Result<()> { + let mut count = r.read_bits(4)? as u64; + if count == 15 { + // 15 + esc - 1, written so an escape of 0 cannot underflow. + count = 14 + r.read_bits(8)? as u64; + } + let end_bit = r.bit_position() + count * 8; + // Implicit SBR: no SBR in the config, but this FIL is the first + // to carry an SBR extension right after an element. Arm the + // chain for it; if its payload does not parse, disarm again. + let arming = match self.implicit_core { + Some(core) + if self.sbr.is_none() + && pending.is_some() + && r.bits_remaining() >= 4 + && matches!(r.peek_bits(4)?, 13 | 14) => + { + self.sbr = Some(crate::sbr_chain::SbrChain::new(core * 2)); + true + } + _ => false, + }; + let mut applied = false; + if let (Some(sbr), Some((tag, start, n, is_cpe))) = + (self.sbr.as_mut(), pending.take()) + { + // extension_payload(): bs_extension_type(4), then + // (for SBR) an optional 10-bit CRC, then + // sbr_extension_data() itself. + if r.bits_remaining() >= 4 { + let ext_type = r.peek_bits(4)?; + if ext_type == 13 || ext_type == 14 { + r.skip_bits(4)?; + if ext_type == 14 && r.bits_remaining() >= 10 { + r.skip_bits(10)?; // bs_sbr_crc_bits + } + if let Some(data) = sbr.parse(r, tag, is_cpe) { + if data.ps.is_some() && (arming || self.implicit_on) { + self.implicit_ps = true; + } + sbr.apply_planes(tag, is_cpe, &data, out, start, n)?; + applied = true; + } + } + } + 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 + // requires doubled-rate reconstruction, using + // the last successfully parsed frame's data. + sbr.apply_last_planes(tag, is_cpe, out, start, n)?; + } + } + if arming { + if applied { + self.implicit_on = true; + self.implicit_core = None; + } else { + // The payload did not parse: this was not SBR after all. + self.sbr = None; + } + } + // Resync to the FIL's declared byte boundary regardless + // of how many bits SBR parsing above actually consumed: + // a parse error or trailing padding must never desync + // the elements that follow. + let now = r.bit_position(); + if now < end_bit { + r.skip_bits(end_bit - now)?; + } + *pending = None; + Ok(()) + } + /// Decodes one `raw_data_block`, returning one PCM plane per channel in /// bitstream element order. pub fn raw_data_block(&mut self, r: &mut BitReader<'_>) -> Result>> { @@ -887,77 +967,7 @@ impl BlockDecoder { pending = None; } 6 => { - let mut count = r.read_bits(4)? as u64; - if count == 15 { - // 15 + esc - 1, written so an escape of 0 cannot underflow. - count = 14 + r.read_bits(8)? as u64; - } - let end_bit = r.bit_position() + count * 8; - // Implicit SBR: no SBR in the config, but this FIL is the first - // to carry an SBR extension right after an element. Arm the - // chain for it; if its payload does not parse, disarm again. - let arming = match self.implicit_core { - Some(core) - if self.sbr.is_none() - && pending.is_some() - && r.bits_remaining() >= 4 - && matches!(r.peek_bits(4)?, 13 | 14) => - { - self.sbr = Some(crate::sbr_chain::SbrChain::new(core * 2)); - true - } - _ => false, - }; - let mut applied = false; - if let (Some(sbr), Some((tag, start, n, is_cpe))) = - (self.sbr.as_mut(), pending.take()) - { - // extension_payload(): bs_extension_type(4), then - // (for SBR) an optional 10-bit CRC, then - // sbr_extension_data() itself. - if r.bits_remaining() >= 4 { - let ext_type = r.peek_bits(4)?; - if ext_type == 13 || ext_type == 14 { - r.skip_bits(4)?; - if ext_type == 14 && r.bits_remaining() >= 10 { - r.skip_bits(10)?; // bs_sbr_crc_bits - } - if let Some(data) = sbr.parse(r, tag, is_cpe) { - if data.ps.is_some() && (arming || self.implicit_on) { - self.implicit_ps = true; - } - sbr.apply_planes(tag, is_cpe, &data, &mut out, start, n)?; - applied = true; - } - } - } - 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 - // requires doubled-rate reconstruction, using - // the last successfully parsed frame's data. - sbr.apply_last_planes(tag, is_cpe, &mut out, start, n)?; - } - } - if arming { - if applied { - self.implicit_on = true; - self.implicit_core = None; - } else { - // The payload did not parse: this was not SBR after all. - self.sbr = None; - } - } - // Resync to the FIL's declared byte boundary regardless - // of how many bits SBR parsing above actually consumed: - // a parse error or trailing padding must never desync - // the elements that follow. - let now = r.bit_position(); - if now < end_bit { - r.skip_bits(end_bit - now)?; - } - pending = None; + self.fill_element(r, &mut out, &mut pending)?; } _ => { // The block ended with no FIL element ever following the