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