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> {
249 }249 }
250 250
251 /// A presence bit followed by an `n`-bit value.251 /// 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>> {
253 if r.read_bit()? {253 if r.read_bit()? {
254 Ok(Some(r.read_bits(n)? as u8))254 Ok(Some(r.read_bits(n)? as u8))
255 } else {255 } else {
crates/ec-ac3/src/decoder.rsmodified+2 -5
@@ -12,9 +12,6 @@ use crate::eac3;
12 use crate::syncinfo;12 use crate::syncinfo;
13 use crate::transform::BLOCK_SAMPLES;13 use crate::transform::BLOCK_SAMPLES;
14 14
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-
18 /// How the decoder should fold the coded channels down, if at all.15 /// How the decoder should fold the coded channels down, if at all.
19 #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]16 #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
20 pub enum Downmix {17 pub enum Downmix {
@@ -285,11 +282,11 @@ impl Ac3Decoder {
285 self.core.dither_on = self.options.dither;282 self.core.dither_on = self.options.dither;
286 self.core283 self.core
287 .start_frame(Syntax::Ac3, sync.fscod as usize, &bsi);284 .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 {
289 self.core.block(&mut r, blk, out)?;286 self.core.block(&mut r, blk, out)?;
290 }287 }
291 Ok((288 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),
293 sync.frame_size,290 sync.frame_size,
294 ))291 ))
295 } else {292 } else {
crates/ec-ac3/src/eac3.rsmodified+1 -9
@@ -11,7 +11,7 @@
11 use ec_core::{BitReader, Error, Result};11 use ec_core::{BitReader, Error, Result};
12 12
13 use crate::bitalloc::{BitAllocParams, DeltaBa};13 use crate::bitalloc::{BitAllocParams, DeltaBa};
14-use crate::bsi::{Acmod, Bsi};14+use crate::bsi::{Acmod, Bsi, read_optional};
15 use crate::decode::{CHANNELS, COEFFS, CPL, Core, LFE, Syntax};15 use crate::decode::{CHANNELS, COEFFS, CPL, Core, LFE, Syntax};
16 use crate::exps::{self, Strategy};16 use crate::exps::{self, Strategy};
17 17
@@ -424,14 +424,6 @@ fn read_mixdata_option4(r: &mut BitReader<'_>, _nblocks: usize) -> Result<()> {
424 Ok(())424 Ok(())
425 }425 }
426 426
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-
435 /// The frame-level strategy fields of `audfrm()` (Table E1.3).427 /// The frame-level strategy fields of `audfrm()` (Table E1.3).
436 #[derive(Debug, Clone)]428 #[derive(Debug, Clone)]
437 struct AudFrm {429 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
28 28
29 /// Samples per channel one syncframe codes.29 /// Samples per channel one syncframe codes.
30 const FRAME_SAMPLES: usize = 1536;30 const FRAME_SAMPLES: usize = 1536;
31-/// Blocks per syncframe.
32-const BLOCKS: usize = 6;
33 /// Coefficients per channel per block ([`crate::decode::COEFFS`], not public).31 /// Coefficients per channel per block ([`crate::decode::COEFFS`], not public).
34 const COEFFS: usize = 256;32 const COEFFS: usize = 256;
35 /// Lowest bit rate per coded channel this encoder accepts: below it the33 /// Lowest bit rate per coded channel this encoder accepts: below it the
@@ -392,15 +390,15 @@ impl Ac3Encoder {
392 fn encode_frame(&mut self, interleaved: &[f32]) -> Vec<u8> {390 fn encode_frame(&mut self, interleaved: &[f32]) -> Vec<u8> {
393 // Deinterleave into coded-channel order, pick long or short transform391 // Deinterleave into coded-channel order, pick long or short transform
394 // per block from the transient detector, and run the MDCT.392 // per block from the transient detector, and run the MDCT.
395- let mut coeffs = vec![vec![[0.0f32; COEFFS]; self.coded]; BLOCKS];393+ let mut coeffs = vec![vec![[0.0f32; COEFFS]; self.coded]; crate::AC3_BLOCKS_PER_FRAME];
396- let mut blksw = vec![vec![false; self.coded]; BLOCKS];394+ let mut blksw = vec![vec![false; self.coded]; crate::AC3_BLOCKS_PER_FRAME];
397 for ch in 0..self.coded {395 for ch in 0..self.coded {
398 let slot = if ch < self.nfchans {396 let slot = if ch < self.nfchans {
399 self.order.iter().position(|&s| s == Some(ch)).unwrap()397 self.order.iter().position(|&s| s == Some(ch)).unwrap()
400 } else {398 } else {
401 self.order.iter().position(|&s| s.is_none()).unwrap()399 self.order.iter().position(|&s| s.is_none()).unwrap()
402 };400 };
403- for blk in 0..BLOCKS {401+ for blk in 0..crate::AC3_BLOCKS_PER_FRAME {
404 let mut window_in = [0.0f32; 512];402 let mut window_in = [0.0f32; 512];
405 window_in[..256].copy_from_slice(&self.history[ch]);403 window_in[..256].copy_from_slice(&self.history[ch]);
406 for n in 0..256 {404 for n in 0..256 {
@@ -477,7 +475,7 @@ impl Ac3Encoder {
477 self.stats.csnroffst_sum += u64::from(csnroffst);475 self.stats.csnroffst_sum += u64::from(csnroffst);
478 self.stats.bits_used += w.bit_len();476 self.stats.bits_used += w.bit_len();
479 self.stats.bits_budget += budget_bits;477 self.stats.bits_budget += budget_bits;
480- for blk in 0..BLOCKS {478+ for blk in 0..crate::AC3_BLOCKS_PER_FRAME {
481 for ch in 0..self.nfchans {479 for ch in 0..self.nfchans {
482 self.stats.blksw_blocks += u64::from(blksw[blk][ch]);480 self.stats.blksw_blocks += u64::from(blksw[blk][ch]);
483 match plan.strategy[blk][ch] {481 match plan.strategy[blk][ch] {
@@ -520,14 +518,14 @@ impl Ac3Encoder {
520 .collect();518 .collect();
521 let mut plan = Plan {519 let mut plan = Plan {
522 blksw: blksw.to_vec(),520 blksw: blksw.to_vec(),
523- strategy: vec![vec![Strategy::Reuse; self.coded]; BLOCKS],521+ strategy: vec![vec![Strategy::Reuse; self.coded]; crate::AC3_BLOCKS_PER_FRAME],
524- sent: vec![vec![Vec::new(); self.coded]; BLOCKS],522+ sent: vec![vec![Vec::new(); self.coded]; crate::AC3_BLOCKS_PER_FRAME],
525- exps: vec![vec![[24u8; COEFFS]; self.coded]; BLOCKS],523+ exps: vec![vec![[24u8; COEFFS]; self.coded]; crate::AC3_BLOCKS_PER_FRAME],
526 };524 };
527 for ch in 0..self.coded {525 for ch in 0..self.coded {
528 let mut starts = vec![0usize];526 let mut starts = vec![0usize];
529 if !conservative {527 if !conservative {
530- for blk in 1..BLOCKS {528+ for blk in 1..crate::AC3_BLOCKS_PER_FRAME {
531 let last = *starts.last().unwrap();529 let last = *starts.last().unwrap();
532 if blksw[blk][ch] != blksw[blk - 1][ch]530 if blksw[blk][ch] != blksw[blk - 1][ch]
533 || envelope_moved(&ideal[blk][ch], &ideal[last][ch], endmant[ch])531 || envelope_moved(&ideal[blk][ch], &ideal[last][ch], endmant[ch])
@@ -537,7 +535,7 @@ impl Ac3Encoder {
537 }535 }
538 }536 }
539 for (i, &start) in starts.iter().enumerate() {537 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);
541 let mut env = ideal[start][ch];539 let mut env = ideal[start][ch];
542 for blk in start + 1..end {540 for blk in start + 1..end {
543 for bin in 0..endmant[ch] {541 for bin in 0..endmant[ch] {
@@ -575,10 +573,10 @@ impl Ac3Encoder {
575 // §7.2.2.1.1: all-zero SNR offsets mean no mantissa bits at all —573 // §7.2.2.1.1: all-zero SNR offsets mean no mantissa bits at all —
576 // the decoder short-circuits there instead of running the model.574 // the decoder short-circuits there instead of running the model.
577 if csnroffst == 0 && fsnroffst == 0 {575 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];
579 }577 }
580- let mut out: Vec<Vec<[u8; COEFFS]>> = Vec::with_capacity(BLOCKS);578+ let mut out: Vec<Vec<[u8; COEFFS]>> = Vec::with_capacity(crate::AC3_BLOCKS_PER_FRAME);
581- for blk in 0..BLOCKS {579+ for blk in 0..crate::AC3_BLOCKS_PER_FRAME {
582 let baps = (0..self.coded)580 let baps = (0..self.coded)
583 .map(|ch| {581 .map(|ch| {
584 if blk > 0 && plan.strategy[blk][ch] == Strategy::Reuse {582 if blk > 0 && plan.strategy[blk][ch] == Strategy::Reuse {
@@ -652,7 +650,7 @@ impl Ac3Encoder {
652 w.write_bit(false); // timecod2650 w.write_bit(false); // timecod2
653 w.write_bit(false); // addbsi651 w.write_bit(false); // addbsi
654 652
655- for blk in 0..BLOCKS {653+ for blk in 0..crate::AC3_BLOCKS_PER_FRAME {
656 self.write_block(&mut w, blk, coeffs, plan, endmant, bap, csnroffst, fsnroffst);654 self.write_block(&mut w, blk, coeffs, plan, endmant, bap, csnroffst, fsnroffst);
657 }655 }
658 w656 w