forge
Explore
Log in

Don't you dare go Hollow

U12: ec-ac3 one read_optional (bsi, reused by eac3) and one block-count const (AC3_BLOCKS_PER_FRAME)

Browse files at this commit

8122b343fd79cb1998f3d23f1afa3292489ae5eeAhmet Kaan GümüşParent: 9d1d075
crates/ec-ac3/src/bsi.rsmodified+1 -1
@@ -249,7 +249,7 @@ pub fn parse_from(r: &mut BitReader<'_>) -> Result<Bsi> {
249249 }
250250
251251 /// A presence bit followed by an `n`-bit value.
252-fn read_optional(r: &mut BitReader<'_>, n: u32) -> Result<Option<u8>> {
252+pub(crate) fn read_optional(r: &mut BitReader<'_>, n: u32) -> Result<Option<u8>> {
253253 if r.read_bit()? {
254254 Ok(Some(r.read_bits(n)? as u8))
255255 } else {
crates/ec-ac3/src/decoder.rsmodified+2 -5
@@ -12,9 +12,6 @@ use crate::eac3;
1212 use crate::syncinfo;
1313 use crate::transform::BLOCK_SAMPLES;
1414
15-/// Blocks per AC-3 syncframe (A/52 §5.3.3); E-AC-3 states its own count.
16-const AC3_BLOCKS: usize = 6;
17-
1815 /// How the decoder should fold the coded channels down, if at all.
1916 #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
2017 pub enum Downmix {
@@ -285,11 +282,11 @@ impl Ac3Decoder {
285282 self.core.dither_on = self.options.dither;
286283 self.core
287284 .start_frame(Syntax::Ac3, sync.fscod as usize, &bsi);
288- for blk in 0..AC3_BLOCKS {
285+ for blk in 0..crate::AC3_BLOCKS_PER_FRAME {
289286 self.core.block(&mut r, blk, out)?;
290287 }
291288 Ok((
292- self.info_from(Syntax::Ac3, sync.sample_rate, &bsi, AC3_BLOCKS),
289+ self.info_from(Syntax::Ac3, sync.sample_rate, &bsi, crate::AC3_BLOCKS_PER_FRAME),
293290 sync.frame_size,
294291 ))
295292 } else {
crates/ec-ac3/src/eac3.rsmodified+1 -9
@@ -11,7 +11,7 @@
1111 use ec_core::{BitReader, Error, Result};
1212
1313 use crate::bitalloc::{BitAllocParams, DeltaBa};
14-use crate::bsi::{Acmod, Bsi};
14+use crate::bsi::{Acmod, Bsi, read_optional};
1515 use crate::decode::{CHANNELS, COEFFS, CPL, Core, LFE, Syntax};
1616 use crate::exps::{self, Strategy};
1717
@@ -424,14 +424,6 @@ fn read_mixdata_option4(r: &mut BitReader<'_>, _nblocks: usize) -> Result<()> {
424424 Ok(())
425425 }
426426
427-fn read_optional(r: &mut BitReader<'_>, n: u32) -> Result<Option<u8>> {
428- if r.read_bit()? {
429- Ok(Some(r.read_bits(n)? as u8))
430- } else {
431- Ok(None)
432- }
433-}
434-
435427 /// The frame-level strategy fields of `audfrm()` (Table E1.3).
436428 #[derive(Debug, Clone)]
437429 struct AudFrm {
crates/ec-ac3/src/encode.rsmodified+13 -15
@@ -28,8 +28,6 @@ use crate::tables::{BIT_RATE_KBPS, FRAME_SIZE_WORDS, QNTZTAB, QUANT_LEVELS, SAMP
2828
2929 /// Samples per channel one syncframe codes.
3030 const FRAME_SAMPLES: usize = 1536;
31-/// Blocks per syncframe.
32-const BLOCKS: usize = 6;
3331 /// Coefficients per channel per block ([`crate::decode::COEFFS`], not public).
3432 const COEFFS: usize = 256;
3533 /// Lowest bit rate per coded channel this encoder accepts: below it the
@@ -392,15 +390,15 @@ impl Ac3Encoder {
392390 fn encode_frame(&mut self, interleaved: &[f32]) -> Vec<u8> {
393391 // Deinterleave into coded-channel order, pick long or short transform
394392 // per block from the transient detector, and run the MDCT.
395- let mut coeffs = vec![vec![[0.0f32; COEFFS]; self.coded]; BLOCKS];
396- let mut blksw = vec![vec![false; self.coded]; BLOCKS];
393+ let mut coeffs = vec![vec![[0.0f32; COEFFS]; self.coded]; crate::AC3_BLOCKS_PER_FRAME];
394+ let mut blksw = vec![vec![false; self.coded]; crate::AC3_BLOCKS_PER_FRAME];
397395 for ch in 0..self.coded {
398396 let slot = if ch < self.nfchans {
399397 self.order.iter().position(|&s| s == Some(ch)).unwrap()
400398 } else {
401399 self.order.iter().position(|&s| s.is_none()).unwrap()
402400 };
403- for blk in 0..BLOCKS {
401+ for blk in 0..crate::AC3_BLOCKS_PER_FRAME {
404402 let mut window_in = [0.0f32; 512];
405403 window_in[..256].copy_from_slice(&self.history[ch]);
406404 for n in 0..256 {
@@ -477,7 +475,7 @@ impl Ac3Encoder {
477475 self.stats.csnroffst_sum += u64::from(csnroffst);
478476 self.stats.bits_used += w.bit_len();
479477 self.stats.bits_budget += budget_bits;
480- for blk in 0..BLOCKS {
478+ for blk in 0..crate::AC3_BLOCKS_PER_FRAME {
481479 for ch in 0..self.nfchans {
482480 self.stats.blksw_blocks += u64::from(blksw[blk][ch]);
483481 match plan.strategy[blk][ch] {
@@ -520,14 +518,14 @@ impl Ac3Encoder {
520518 .collect();
521519 let mut plan = Plan {
522520 blksw: blksw.to_vec(),
523- strategy: vec![vec![Strategy::Reuse; self.coded]; BLOCKS],
524- sent: vec![vec![Vec::new(); self.coded]; BLOCKS],
525- exps: vec![vec![[24u8; COEFFS]; self.coded]; BLOCKS],
521+ strategy: vec![vec![Strategy::Reuse; self.coded]; crate::AC3_BLOCKS_PER_FRAME],
522+ sent: vec![vec![Vec::new(); self.coded]; crate::AC3_BLOCKS_PER_FRAME],
523+ exps: vec![vec![[24u8; COEFFS]; self.coded]; crate::AC3_BLOCKS_PER_FRAME],
526524 };
527525 for ch in 0..self.coded {
528526 let mut starts = vec![0usize];
529527 if !conservative {
530- for blk in 1..BLOCKS {
528+ for blk in 1..crate::AC3_BLOCKS_PER_FRAME {
531529 let last = *starts.last().unwrap();
532530 if blksw[blk][ch] != blksw[blk - 1][ch]
533531 || envelope_moved(&ideal[blk][ch], &ideal[last][ch], endmant[ch])
@@ -537,7 +535,7 @@ impl Ac3Encoder {
537535 }
538536 }
539537 for (i, &start) in starts.iter().enumerate() {
540- let end = starts.get(i + 1).copied().unwrap_or(BLOCKS);
538+ let end = starts.get(i + 1).copied().unwrap_or(crate::AC3_BLOCKS_PER_FRAME);
541539 let mut env = ideal[start][ch];
542540 for blk in start + 1..end {
543541 for bin in 0..endmant[ch] {
@@ -575,10 +573,10 @@ impl Ac3Encoder {
575573 // §7.2.2.1.1: all-zero SNR offsets mean no mantissa bits at all —
576574 // the decoder short-circuits there instead of running the model.
577575 if csnroffst == 0 && fsnroffst == 0 {
578- return vec![vec![[0u8; COEFFS]; self.coded]; BLOCKS];
576+ return vec![vec![[0u8; COEFFS]; self.coded]; crate::AC3_BLOCKS_PER_FRAME];
579577 }
580- let mut out: Vec<Vec<[u8; COEFFS]>> = Vec::with_capacity(BLOCKS);
581- for blk in 0..BLOCKS {
578+ let mut out: Vec<Vec<[u8; COEFFS]>> = Vec::with_capacity(crate::AC3_BLOCKS_PER_FRAME);
579+ for blk in 0..crate::AC3_BLOCKS_PER_FRAME {
582580 let baps = (0..self.coded)
583581 .map(|ch| {
584582 if blk > 0 && plan.strategy[blk][ch] == Strategy::Reuse {
@@ -652,7 +650,7 @@ impl Ac3Encoder {
652650 w.write_bit(false); // timecod2
653651 w.write_bit(false); // addbsi
654652
655- for blk in 0..BLOCKS {
653+ for blk in 0..crate::AC3_BLOCKS_PER_FRAME {
656654 self.write_block(&mut w, blk, coeffs, plan, endmant, bap, csnroffst, fsnroffst);
657655 }
658656 w