From 8122b343fd79cb1998f3d23f1afa3292489ae5ee 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 13:34:32 +0300 Subject: [PATCH] U12: ec-ac3 one read_optional (bsi, reused by eac3) and one block-count const (AC3_BLOCKS_PER_FRAME) --- crates/ec-ac3/src/bsi.rs | 2 +- crates/ec-ac3/src/decoder.rs | 7 ++----- crates/ec-ac3/src/eac3.rs | 10 +--------- crates/ec-ac3/src/encode.rs | 28 +++++++++++++--------------- 4 files changed, 17 insertions(+), 30 deletions(-) diff --git a/crates/ec-ac3/src/bsi.rs b/crates/ec-ac3/src/bsi.rs index c3793a46..cb402f4a 100644 --- a/crates/ec-ac3/src/bsi.rs +++ b/crates/ec-ac3/src/bsi.rs @@ -249,7 +249,7 @@ pub fn parse_from(r: &mut BitReader<'_>) -> Result { } /// A presence bit followed by an `n`-bit value. -fn read_optional(r: &mut BitReader<'_>, n: u32) -> Result> { +pub(crate) fn read_optional(r: &mut BitReader<'_>, n: u32) -> Result> { if r.read_bit()? { Ok(Some(r.read_bits(n)? as u8)) } else { diff --git a/crates/ec-ac3/src/decoder.rs b/crates/ec-ac3/src/decoder.rs index fc07fef3..fa56bfdc 100644 --- a/crates/ec-ac3/src/decoder.rs +++ b/crates/ec-ac3/src/decoder.rs @@ -12,9 +12,6 @@ use crate::eac3; use crate::syncinfo; use crate::transform::BLOCK_SAMPLES; -/// Blocks per AC-3 syncframe (A/52 §5.3.3); E-AC-3 states its own count. -const AC3_BLOCKS: usize = 6; - /// How the decoder should fold the coded channels down, if at all. #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub enum Downmix { @@ -285,11 +282,11 @@ impl Ac3Decoder { self.core.dither_on = self.options.dither; self.core .start_frame(Syntax::Ac3, sync.fscod as usize, &bsi); - for blk in 0..AC3_BLOCKS { + for blk in 0..crate::AC3_BLOCKS_PER_FRAME { self.core.block(&mut r, blk, out)?; } Ok(( - self.info_from(Syntax::Ac3, sync.sample_rate, &bsi, AC3_BLOCKS), + self.info_from(Syntax::Ac3, sync.sample_rate, &bsi, crate::AC3_BLOCKS_PER_FRAME), sync.frame_size, )) } else { diff --git a/crates/ec-ac3/src/eac3.rs b/crates/ec-ac3/src/eac3.rs index d66712ed..230627b2 100644 --- a/crates/ec-ac3/src/eac3.rs +++ b/crates/ec-ac3/src/eac3.rs @@ -11,7 +11,7 @@ use ec_core::{BitReader, Error, Result}; use crate::bitalloc::{BitAllocParams, DeltaBa}; -use crate::bsi::{Acmod, Bsi}; +use crate::bsi::{Acmod, Bsi, read_optional}; use crate::decode::{CHANNELS, COEFFS, CPL, Core, LFE, Syntax}; use crate::exps::{self, Strategy}; @@ -424,14 +424,6 @@ fn read_mixdata_option4(r: &mut BitReader<'_>, _nblocks: usize) -> Result<()> { Ok(()) } -fn read_optional(r: &mut BitReader<'_>, n: u32) -> Result> { - if r.read_bit()? { - Ok(Some(r.read_bits(n)? as u8)) - } else { - Ok(None) - } -} - /// The frame-level strategy fields of `audfrm()` (Table E1.3). #[derive(Debug, Clone)] struct AudFrm { diff --git a/crates/ec-ac3/src/encode.rs b/crates/ec-ac3/src/encode.rs index d48ed5a7..be1193b9 100644 --- a/crates/ec-ac3/src/encode.rs +++ b/crates/ec-ac3/src/encode.rs @@ -28,8 +28,6 @@ use crate::tables::{BIT_RATE_KBPS, FRAME_SIZE_WORDS, QNTZTAB, QUANT_LEVELS, SAMP /// Samples per channel one syncframe codes. const FRAME_SAMPLES: usize = 1536; -/// Blocks per syncframe. -const BLOCKS: usize = 6; /// Coefficients per channel per block ([`crate::decode::COEFFS`], not public). const COEFFS: usize = 256; /// Lowest bit rate per coded channel this encoder accepts: below it the @@ -392,15 +390,15 @@ impl Ac3Encoder { fn encode_frame(&mut self, interleaved: &[f32]) -> Vec { // Deinterleave into coded-channel order, pick long or short transform // per block from the transient detector, and run the MDCT. - let mut coeffs = vec![vec![[0.0f32; COEFFS]; self.coded]; BLOCKS]; - let mut blksw = vec![vec![false; self.coded]; BLOCKS]; + let mut coeffs = vec![vec![[0.0f32; COEFFS]; self.coded]; crate::AC3_BLOCKS_PER_FRAME]; + let mut blksw = vec![vec![false; self.coded]; crate::AC3_BLOCKS_PER_FRAME]; for ch in 0..self.coded { let slot = if ch < self.nfchans { self.order.iter().position(|&s| s == Some(ch)).unwrap() } else { self.order.iter().position(|&s| s.is_none()).unwrap() }; - for blk in 0..BLOCKS { + for blk in 0..crate::AC3_BLOCKS_PER_FRAME { let mut window_in = [0.0f32; 512]; window_in[..256].copy_from_slice(&self.history[ch]); for n in 0..256 { @@ -477,7 +475,7 @@ impl Ac3Encoder { self.stats.csnroffst_sum += u64::from(csnroffst); self.stats.bits_used += w.bit_len(); self.stats.bits_budget += budget_bits; - for blk in 0..BLOCKS { + for blk in 0..crate::AC3_BLOCKS_PER_FRAME { for ch in 0..self.nfchans { self.stats.blksw_blocks += u64::from(blksw[blk][ch]); match plan.strategy[blk][ch] { @@ -520,14 +518,14 @@ impl Ac3Encoder { .collect(); let mut plan = Plan { blksw: blksw.to_vec(), - strategy: vec![vec![Strategy::Reuse; self.coded]; BLOCKS], - sent: vec![vec![Vec::new(); self.coded]; BLOCKS], - exps: vec![vec![[24u8; COEFFS]; self.coded]; BLOCKS], + strategy: vec![vec![Strategy::Reuse; self.coded]; crate::AC3_BLOCKS_PER_FRAME], + sent: vec![vec![Vec::new(); self.coded]; crate::AC3_BLOCKS_PER_FRAME], + exps: vec![vec![[24u8; COEFFS]; self.coded]; crate::AC3_BLOCKS_PER_FRAME], }; for ch in 0..self.coded { let mut starts = vec![0usize]; if !conservative { - for blk in 1..BLOCKS { + for blk in 1..crate::AC3_BLOCKS_PER_FRAME { let last = *starts.last().unwrap(); if blksw[blk][ch] != blksw[blk - 1][ch] || envelope_moved(&ideal[blk][ch], &ideal[last][ch], endmant[ch]) @@ -537,7 +535,7 @@ impl Ac3Encoder { } } for (i, &start) in starts.iter().enumerate() { - let end = starts.get(i + 1).copied().unwrap_or(BLOCKS); + let end = starts.get(i + 1).copied().unwrap_or(crate::AC3_BLOCKS_PER_FRAME); let mut env = ideal[start][ch]; for blk in start + 1..end { for bin in 0..endmant[ch] { @@ -575,10 +573,10 @@ impl Ac3Encoder { // §7.2.2.1.1: all-zero SNR offsets mean no mantissa bits at all — // the decoder short-circuits there instead of running the model. if csnroffst == 0 && fsnroffst == 0 { - return vec![vec![[0u8; COEFFS]; self.coded]; BLOCKS]; + return vec![vec![[0u8; COEFFS]; self.coded]; crate::AC3_BLOCKS_PER_FRAME]; } - let mut out: Vec> = Vec::with_capacity(BLOCKS); - for blk in 0..BLOCKS { + let mut out: Vec> = Vec::with_capacity(crate::AC3_BLOCKS_PER_FRAME); + for blk in 0..crate::AC3_BLOCKS_PER_FRAME { let baps = (0..self.coded) .map(|ch| { if blk > 0 && plan.strategy[blk][ch] == Strategy::Reuse { @@ -652,7 +650,7 @@ impl Ac3Encoder { w.write_bit(false); // timecod2 w.write_bit(false); // addbsi - for blk in 0..BLOCKS { + for blk in 0..crate::AC3_BLOCKS_PER_FRAME { self.write_block(&mut w, blk, coeffs, plan, endmant, bap, csnroffst, fsnroffst); } w