Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

Refactor ac3 for strict Clippy checks

Dylan M. Taylor08fcd65e6219
diff --git a/crates/ac3/src/eac3.rs b/crates/ac3/src/eac3.rs
index 075f3dddc..a2dad0be6 100644
--- a/crates/ac3/src/eac3.rs
+++ b/crates/ac3/src/eac3.rs
@@ -133,7 +133,6 @@ pub(crate) fn read_bsi(r: &mut Bits) -> Result<Bsi> {
} else {
(fscod, r.read(2) as usize)
};
- let nblocks = BLOCKS[numblkscod & 3] as usize;
let acmod = r.read(3) as usize;
let lfeon = r.bit();
r.skip(5 + 5); // bsid, dialnorm
@@ -149,92 +148,105 @@ pub(crate) fn read_bsi(r: &mut Bits) -> Result<Bsi> {
if strmtyp == 1 && r.bit() {
r.skip(16); // chanmap
}
+ skip_mixing_metadata(r, strmtyp, acmod, lfeon, numblkscod);
+ skip_informational_metadata(r, acmod, fscod);
+ if strmtyp == 0 && numblkscod != 3 {
+ r.skip(1); // convsync
+ }
+ if strmtyp == 2 && (numblkscod == 3 || r.bit()) {
+ r.skip(6); // blkid → frmsizecod
+ }
if r.bit() {
- // mixing metadata
- if acmod > 2 {
- r.skip(2); // dmixmod
- }
- if acmod & 1 != 0 && acmod > 2 {
- r.skip(6); // ltrtcmixlev, lorocmixlev
+ let l = r.read(6) as usize;
+ r.skip((l + 1) * 8); // addbsi
+ }
+ Ok(Bsi { strmtyp, fscod: rate_code, numblkscod })
+}
+
+/// Mixing metadata (mixmdate), when present.
+fn skip_mixing_metadata(r: &mut Bits, strmtyp: u8, acmod: usize, lfeon: bool, numblkscod: usize) {
+ if !r.bit() {
+ return;
+ }
+ let nblocks = BLOCKS[numblkscod & 3] as usize;
+ // mixing metadata
+ if acmod > 2 {
+ r.skip(2); // dmixmod
+ }
+ if acmod & 1 != 0 && acmod > 2 {
+ r.skip(6); // ltrtcmixlev, lorocmixlev
+ }
+ if acmod & 4 != 0 {
+ r.skip(6); // ltrtsurmixlev, lorosurmixlev
+ }
+ if lfeon && r.bit() {
+ r.skip(5); // lfemixlevcod
+ }
+ if strmtyp == 0 {
+ if r.bit() {
+ r.skip(6); // pgmscl
}
- if acmod & 4 != 0 {
- r.skip(6); // ltrtsurmixlev, lorosurmixlev
+ if acmod == 0 && r.bit() {
+ r.skip(6); // pgmscl2
}
- if lfeon && r.bit() {
- r.skip(5); // lfemixlevcod
+ if r.bit() {
+ r.skip(6); // extpgmscl
+ }
+ match r.read(2) {
+ 1 => r.skip(5), // premixcmpsel, drcsrc, premixcmpscl
+ 2 => r.skip(12),
+ // mixdeflen: the mixdata field (through mixdatafill) is mixdeflen + 2 bytes
+ 3 => {
+ let len = r.read(5) as usize;
+ r.skip(8 * (len + 2));
+ }
+ _ => {}
}
- if strmtyp == 0 {
+ if acmod < 2 {
if r.bit() {
- r.skip(6); // pgmscl
+ r.skip(14); // panmean, paninfo
}
if acmod == 0 && r.bit() {
- r.skip(6); // pgmscl2
- }
- if r.bit() {
- r.skip(6); // extpgmscl
- }
- match r.read(2) {
- 1 => r.skip(5), // premixcmpsel, drcsrc, premixcmpscl
- 2 => r.skip(12),
- // mixdeflen: the mixdata field (through mixdatafill) is mixdeflen + 2 bytes
- 3 => {
- let len = r.read(5) as usize;
- r.skip(8 * (len + 2));
- }
- _ => {}
+ r.skip(14); // panmean2, paninfo2
}
- if acmod < 2 {
- if r.bit() {
- r.skip(14); // panmean, paninfo
- }
- if acmod == 0 && r.bit() {
- r.skip(14); // panmean2, paninfo2
- }
- }
- if r.bit() {
- // frame mixing configuration information
- if numblkscod == 0 {
- r.skip(5);
- } else {
- for _ in 0..nblocks {
- if r.bit() {
- r.skip(5);
- }
+ }
+ if r.bit() {
+ // frame mixing configuration information
+ if numblkscod == 0 {
+ r.skip(5);
+ } else {
+ for _ in 0..nblocks {
+ if r.bit() {
+ r.skip(5);
}
}
}
}
}
- if r.bit() {
- // informational metadata
- r.skip(5); // bsmod, copyrightb, origbs
- if acmod == 2 {
- r.skip(4); // dsurmod, dheadphonmod
- }
- if acmod >= 6 {
- r.skip(2); // dsurexmod
- }
- if r.bit() {
- r.skip(8); // mixlevel, roomtyp, adconvtyp
- }
- if acmod == 0 && r.bit() {
- r.skip(8); // mixlevel2, roomtyp2, adconvtyp2
- }
- if fscod < 3 {
- r.skip(1); // sourcefscod
- }
+}
+
+/// Informational metadata (infomdate), when present.
+fn skip_informational_metadata(r: &mut Bits, acmod: usize, fscod: usize) {
+ if !r.bit() {
+ return;
}
- if strmtyp == 0 && numblkscod != 3 {
- r.skip(1); // convsync
+ // informational metadata
+ r.skip(5); // bsmod, copyrightb, origbs
+ if acmod == 2 {
+ r.skip(4); // dsurmod, dheadphonmod
}
- if strmtyp == 2 && (numblkscod == 3 || r.bit()) {
- r.skip(6); // blkid → frmsizecod
+ if acmod >= 6 {
+ r.skip(2); // dsurexmod
}
if r.bit() {
- let l = r.read(6) as usize;
- r.skip((l + 1) * 8); // addbsi
+ r.skip(8); // mixlevel, roomtyp, adconvtyp
+ }
+ if acmod == 0 && r.bit() {
+ r.skip(8); // mixlevel2, roomtyp2, adconvtyp2
+ }
+ if fscod < 3 {
+ r.skip(1); // sourcefscod
}
- Ok(Bsi { strmtyp, fscod: rate_code, numblkscod })
}
/// The audio frame (§E2.2.3) and the state that runs from block to block within a syncframe.
diff --git a/crates/ac3/src/lib.rs b/crates/ac3/src/lib.rs
index acc2dbe48..fb91dad8c 100644
--- a/crates/ac3/src/lib.rs
+++ b/crates/ac3/src/lib.rs
@@ -791,7 +791,7 @@ impl Decoder {
let mut pcm: Vec<Vec<f32>> = vec![Vec::with_capacity(256 * nblocks); nout];
for blk in 0..nblocks {
let mut coefs = [[0f32; 256]; 6];
- self.audio_block(r, f, acmod, lfeon, blk, &mut coefs, x.as_deref_mut())?;
+ self.audio_block(r, f, BlockPos { acmod, lfeon, blk }, &mut coefs, x.as_deref_mut())?;
if r.overrun() {
return Err(Error::Invalid("audio block runs past the frame"));
}
@@ -811,461 +811,573 @@ impl Decoder {
Ok(pcm)
}
- #[allow(clippy::too_many_arguments)]
- fn audio_block(
- &mut self,
- r: &mut Bits,
- f: &mut Frame,
- acmod: usize,
- lfeon: bool,
- blk: usize,
- coefs: &mut [[f32; 256]; 6],
- mut x: Option<&mut eac3::FrameInfo>,
- ) -> Result<()> {
+ /// Decode one audio block's side information, exponents and mantissas into `coefs`.
+ fn audio_block(&mut self, r: &mut Bits, f: &mut Frame, pos: BlockPos, coefs: &mut [[f32; 256]; 6], mut x: Option<&mut eac3::FrameInfo>) -> Result<()> {
+ let BlockPos { acmod, lfeon, blk } = pos;
let nf = f.nfchans;
- let eac3 = x.is_some();
- match x.as_deref() {
- Some(e) if !e.blkswe => f.blksw = [false; 5],
- _ => (0..nf).for_each(|ch| f.blksw[ch] = r.bit()),
+ read_block_flags(r, f, x.as_deref());
+ read_dynrng(r, f, pos);
+ // E-AC-3 spectral extension strategy and coordinates
+ if let Some(e) = x.as_deref_mut() {
+ e.spx.read(r, blk, acmod, nf)?;
}
+ let spx = SpxBands::of(x.as_deref());
+ read_coupling_strategy(r, f, x.as_deref_mut(), pos, spx.begf)?;
+ read_coupling_coords(r, f, x.as_deref_mut(), acmod);
+ read_rematrix_flags(r, f, x.is_some(), pos, spx.begf);
+ read_exp_strategies(r, f, x.as_deref(), pos);
+ read_channel_bandwidths(r, f, &spx)?;
+ read_exponents(r, f, lfeon)?;
+ read_bit_alloc_params(r, f, x.as_deref(), pos)?;
match x.as_deref() {
- // dither on when the flags are not sent (§E2.2.4)
- Some(e) if !e.dithflage => f.dithflag = [true; 5],
- _ => (0..nf).for_each(|ch| f.dithflag[ch] = r.bit()),
+ Some(e) => read_eac3_snr_offsets(r, f, e, pos),
+ None => read_ac3_snr_offsets(r, f, pos)?,
}
- for k in 0..if acmod == 0 { 2 } else { 1 } {
- if r.bit() {
- f.dynrng[k] = dynrng_gain(r.read(8));
- } else if blk == 0 {
- f.dynrng[k] = 1.0;
- }
+ read_coupling_leak(r, f, x.as_deref_mut());
+ read_delta_bit_alloc(r, f, x.as_deref(), blk)?;
+ // skip field
+ if x.as_deref().is_none_or(|e| e.skipflde) && r.bit() {
+ let l = r.read(9) as usize;
+ r.skip(l * 8);
}
- // E-AC-3 spectral extension strategy and coordinates
- if let Some(e) = x.as_deref_mut() {
- e.spx.read(r, blk, acmod, nf)?;
+ allocate_bits(f, lfeon);
+ let cpl = self.read_mantissas(r, f, lfeon, coefs);
+ self.decouple(f, acmod, &cpl, coefs);
+ if acmod == 2 {
+ rematrix(f, coefs);
}
- let spx = x.as_deref().map(|e| &e.spx).filter(|s| s.inu);
- let spxbegf = spx.map(|s| s.begf);
- let spx_begin = spx.map(|s| s.begin_bin());
- let spx_chans = spx.map(|s| s.chinspx).unwrap_or([false; 5]);
- // coupling strategy
- let strategy = match x.as_deref() {
- Some(e) => e.cplstre[blk].then_some(e.cplinu[blk]),
- None => r.bit().then(|| r.bit()),
- };
- if let Some(inu) = strategy {
- f.cplinu = inu;
- if f.cplinu {
- if eac3 && r.bit() {
- return Err(Error::Unsupported("E-AC-3 enhanced coupling".into()));
- }
- if eac3 && acmod == 2 {
- f.chincpl = [true, true, false, false, false];
- } else {
- for ch in 0..nf {
- f.chincpl[ch] = r.bit();
- }
- }
- if acmod == 2 {
- f.phsflginu = r.bit();
- }
- f.cplbegf = r.read(4) as usize;
- f.cplend = match spxbegf {
- // the coupling region ends where spectral extension begins (§E2.2.4)
- Some(b) if b < 6 => b + 1,
- Some(b) => 2 * b - 4,
- None => r.read(4) as usize + 3,
- };
- if f.cplend <= f.cplbegf {
- return Err(Error::Invalid("coupling end below start"));
- }
- // coupling band structure, by absolute sub-band
- let mut strc = [false; 18];
- match x.as_deref_mut() {
- Some(e) => {
- if r.bit() {
- for s in f.cplbegf + 1..f.cplend {
- e.cplbndstrc[s] = r.bit();
- }
- }
- strc = e.cplbndstrc;
- }
- None => (f.cplbegf + 1..f.cplend).for_each(|s| strc[s] = r.bit()),
- }
- let mut band = 0;
- f.sub_to_band[0] = 0;
- for s in 1..f.cplend - f.cplbegf {
- if !strc[f.cplbegf + s] {
- band += 1;
- }
- f.sub_to_band[s] = band;
- }
- f.ncplbnd = band + 1;
- } else {
- f.chincpl = [false; 5];
- if let Some(e) = x.as_deref_mut() {
- e.firstcplcos = [true; 5];
- e.firstcplleak = true;
- f.phsflginu = false;
+ // spectral extension: synthesize the high band of each channel from its low band
+ if let Some(e) = x.as_deref()
+ && e.spx.inu
+ {
+ for ch in 0..nf {
+ if e.spx.chinspx[ch] {
+ e.spx.synthesize(ch, e.spxatten[ch], &mut coefs[ch], || self.noise());
}
}
- } else if blk == 0 && !eac3 {
- return Err(Error::Invalid("no coupling strategy in block 0"));
}
- // coupling coordinates
- if f.cplinu {
- let mut any = false;
- for ch in 0..nf {
- if !f.chincpl[ch] {
- if let Some(e) = x.as_deref_mut() {
- e.firstcplcos[ch] = true;
- }
- continue;
- }
- let coe = match x.as_deref_mut() {
- Some(e) if e.firstcplcos[ch] => {
- e.firstcplcos[ch] = false;
- true
- }
- _ => r.bit(),
- };
- if coe {
- any = true;
- let mstr = r.read(2) as i32;
- for bnd in 0..f.ncplbnd {
- let e = r.read(4) as i32;
- let m = r.read(4) as f32;
- let v = if e == 15 { m / 16.0 } else { (m + 16.0) / 32.0 };
- let co = v * 2f32.powi(-(e + 3 * mstr));
- for s in 0..(f.cplend - f.cplbegf) {
- if f.sub_to_band[s] == bnd {
- f.cplco[ch][s] = co;
- }
- }
- }
- }
+ apply_dynrng(f, pos, coefs);
+ Ok(())
+ }
+
+ /// Read the mantissas of every channel (and the shared coupling channel) into `coefs`, and
+ /// return the coupling channel's mantissas (NaN where a zero-bit mantissa is dithered per
+ /// channel when decoupling).
+ fn read_mantissas(&mut self, r: &mut Bits, f: &Frame, lfeon: bool, coefs: &mut [[f32; 256]; 6]) -> [f32; 256] {
+ let mut gr = Groups::default();
+ let mut cpl = [0f32; 256];
+ let mut got_cpl = false;
+ for ch in 0..f.nfchans {
+ for bin in 0..f.endmant[ch] {
+ let b = f.bap[ch][bin];
+ let m = if b == 0 { if f.dithflag[ch] { self.dither() } else { 0.0 } } else { mantissa(r, b, &mut gr) };
+ coefs[ch][bin] = m * 2f32.powi(-(f.exps[ch][bin] as i32));
}
- if acmod == 2 && f.phsflginu && any {
- for bnd in 0..f.ncplbnd {
- let flag = r.bit();
- for s in 0..(f.cplend - f.cplbegf) {
- if f.sub_to_band[s] == bnd {
- f.phsflg[s] = flag;
- }
- }
+ if f.cplinu && f.chincpl[ch] && !got_cpl {
+ got_cpl = true;
+ for bin in f.cplstrtmant()..f.cplendmant() {
+ let b = f.bap[CPL][bin];
+ cpl[bin] = if b == 0 { f32::NAN } else { mantissa(r, b, &mut gr) };
}
}
}
- // rematrixing (E-AC-3 always sends the flags in block 0)
- if acmod == 2 && ((eac3 && blk == 0) || r.bit()) {
- let n = if f.cplinu {
- match f.cplbegf {
- 0 => 2,
- 1 | 2 => 3,
- _ => 4,
- }
- } else {
- match spxbegf {
- Some(b) if b < 2 => 3,
- _ => 4,
- }
- };
- f.rematflg = [false; 4];
- for k in 0..n {
- f.rematflg[k] = r.bit();
+ if lfeon {
+ for bin in 0..7 {
+ let b = f.bap[LFE][bin];
+ let m = if b == 0 { 0.0 } else { mantissa(r, b, &mut gr) };
+ coefs[LFE][bin] = m * 2f32.powi(-(f.exps[LFE][bin] as i32));
}
}
- // exponent strategies (E-AC-3: from the audio frame)
- match x.as_deref() {
- Some(e) => {
- if f.cplinu {
- f.expstr[CPL] = e.cplexpstr[blk];
- }
- f.expstr[..nf].copy_from_slice(&e.chexpstr[blk][..nf]);
- if lfeon {
- f.expstr[LFE] = e.lfeexpstr[blk];
- }
+ cpl
+ }
+
+ /// Scale the shared coupling channel's mantissas into each coupled channel.
+ fn decouple(&mut self, f: &Frame, acmod: usize, cpl: &[f32; 256], coefs: &mut [[f32; 256]; 6]) {
+ if !f.cplinu {
+ return;
+ }
+ for ch in 0..f.nfchans {
+ if !f.chincpl[ch] {
+ continue;
}
- None => {
- if f.cplinu {
- f.expstr[CPL] = r.read(2) as u8;
- }
- for ch in 0..nf {
- f.expstr[ch] = r.read(2) as u8;
- }
- if lfeon {
- f.expstr[LFE] = r.read(1) as u8;
+ for bin in f.cplstrtmant()..f.cplendmant() {
+ let s = (bin - 37) / 12 - f.cplbegf;
+ let m = if cpl[bin].is_nan() { if f.dithflag[ch] { self.dither() } else { 0.0 } } else { cpl[bin] };
+ let mut v = m * 2f32.powi(-(f.exps[CPL][bin] as i32)) * f.cplco[ch][s] * 8.0;
+ if acmod == 2 && ch == 1 && f.phsflginu && f.phsflg[s] {
+ v = -v;
}
+ coefs[ch][bin] = v;
}
}
- for ch in 0..nf {
- if f.expstr[ch] != 0 {
- if f.chincpl[ch] && f.cplinu {
- f.endmant[ch] = f.cplstrtmant();
- } else if let (true, Some(begin)) = (spx_chans[ch], spx_begin) {
- // §E3.3.3: the channel ends where spectral extension begins
- f.endmant[ch] = begin;
- } else {
- let bw = r.read(6) as usize;
- if bw > 60 {
- return Err(Error::Invalid("chbwcod > 60"));
- }
- f.endmant[ch] = (bw + 12) * 3 + 37;
- }
- }
+ }
+}
+
+/// Where an audio block sits in its frame: channel mode, LFE presence and block index.
+#[derive(Clone, Copy)]
+struct BlockPos {
+ acmod: usize,
+ lfeon: bool,
+ blk: usize,
+}
+
+/// Spectral extension as the later block fields see it.
+struct SpxBands {
+ /// Sub-band where extension begins, when it is in use.
+ begf: Option<usize>,
+ /// First extended bin.
+ begin: Option<usize>,
+ chans: [bool; 5],
+}
+
+impl SpxBands {
+ fn of(x: Option<&eac3::FrameInfo>) -> SpxBands {
+ let spx = x.map(|e| &e.spx).filter(|s| s.inu);
+ SpxBands { begf: spx.map(|s| s.begf), begin: spx.map(|s| s.begin_bin()), chans: spx.map(|s| s.chinspx).unwrap_or([false; 5]) }
+ }
+}
+
+/// Block switch and dither flags (E-AC-3 may omit them, §E2.2.4).
+fn read_block_flags(r: &mut Bits, f: &mut Frame, x: Option<&eac3::FrameInfo>) {
+ let nf = f.nfchans;
+ match x {
+ Some(e) if !e.blkswe => f.blksw = [false; 5],
+ _ => (0..nf).for_each(|ch| f.blksw[ch] = r.bit()),
+ }
+ match x {
+ // dither on when the flags are not sent (§E2.2.4)
+ Some(e) if !e.dithflage => f.dithflag = [true; 5],
+ _ => (0..nf).for_each(|ch| f.dithflag[ch] = r.bit()),
+ }
+}
+
+fn read_dynrng(r: &mut Bits, f: &mut Frame, pos: BlockPos) {
+ for k in 0..if pos.acmod == 0 { 2 } else { 1 } {
+ if r.bit() {
+ f.dynrng[k] = dynrng_gain(r.read(8));
+ } else if pos.blk == 0 {
+ f.dynrng[k] = 1.0;
}
- // exponents
- if f.cplinu && f.expstr[CPL] != 0 {
- let abs = (r.read(4) as i32) << 1;
- let grpsize = 1 << (f.expstr[CPL] - 1);
- let ngrps = (f.cplendmant() - f.cplstrtmant()) / (3 * grpsize);
- let first = f.cplstrtmant();
- decode_exponents(r, abs, ngrps, grpsize, &mut f.exps[CPL], first, true)?;
+ }
+}
+
+/// Coupling strategy, channels in coupling, bounds and band structure.
+fn read_coupling_strategy(r: &mut Bits, f: &mut Frame, x: Option<&mut eac3::FrameInfo>, pos: BlockPos, spxbegf: Option<usize>) -> Result<()> {
+ let BlockPos { acmod, blk, .. } = pos;
+ let eac3 = x.is_some();
+ let strategy = match x.as_deref() {
+ Some(e) => e.cplstre[blk].then_some(e.cplinu[blk]),
+ None => r.bit().then(|| r.bit()),
+ };
+ let Some(inu) = strategy else {
+ if blk == 0 && !eac3 {
+ return Err(Error::Invalid("no coupling strategy in block 0"));
}
- for ch in 0..nf {
- if f.expstr[ch] != 0 {
- let abs = r.read(4) as i32;
- let grpsize = 1usize << (f.expstr[ch] - 1);
- let ngrps = (f.endmant[ch] - 1 + 3 * grpsize - 3) / (3 * grpsize);
- decode_exponents(r, abs, ngrps, grpsize, &mut f.exps[ch], 0, false)?;
- r.skip(2); // gainrng
+ return Ok(());
+ };
+ f.cplinu = inu;
+ if !f.cplinu {
+ f.chincpl = [false; 5];
+ if let Some(e) = x {
+ e.firstcplcos = [true; 5];
+ e.firstcplleak = true;
+ f.phsflginu = false;
+ }
+ return Ok(());
+ }
+ if eac3 && r.bit() {
+ return Err(Error::Unsupported("E-AC-3 enhanced coupling".into()));
+ }
+ if eac3 && acmod == 2 {
+ f.chincpl = [true, true, false, false, false];
+ } else {
+ for ch in 0..f.nfchans {
+ f.chincpl[ch] = r.bit();
+ }
+ }
+ if acmod == 2 {
+ f.phsflginu = r.bit();
+ }
+ f.cplbegf = r.read(4) as usize;
+ f.cplend = match spxbegf {
+ // the coupling region ends where spectral extension begins (§E2.2.4)
+ Some(b) if b < 6 => b + 1,
+ Some(b) => 2 * b - 4,
+ None => r.read(4) as usize + 3,
+ };
+ if f.cplend <= f.cplbegf {
+ return Err(Error::Invalid("coupling end below start"));
+ }
+ read_coupling_bands(r, f, x);
+ Ok(())
+}
+
+/// Coupling band structure, by absolute sub-band; fills `sub_to_band` and `ncplbnd`.
+fn read_coupling_bands(r: &mut Bits, f: &mut Frame, x: Option<&mut eac3::FrameInfo>) {
+ let mut strc = [false; 18];
+ match x {
+ Some(e) => {
+ if r.bit() {
+ for s in f.cplbegf + 1..f.cplend {
+ e.cplbndstrc[s] = r.bit();
+ }
}
+ strc = e.cplbndstrc;
}
- if lfeon && f.expstr[LFE] != 0 {
- let abs = r.read(4) as i32;
- decode_exponents(r, abs, 2, 1, &mut f.exps[LFE], 0, false)?;
+ None => (f.cplbegf + 1..f.cplend).for_each(|s| strc[s] = r.bit()),
+ }
+ let mut band = 0;
+ f.sub_to_band[0] = 0;
+ for s in 1..f.cplend - f.cplbegf {
+ if !strc[f.cplbegf + s] {
+ band += 1;
}
- // bit allocation parametric information
- match x.as_deref() {
- Some(e) if !e.bamode => {
- // §E2.2.4 defaults
- (f.g.sdcycod, f.g.fdcycod, f.g.sgaincod, f.g.dbpbcod, f.g.floorcod) = (2, 1, 1, 2, 7);
+ f.sub_to_band[s] = band;
+ }
+ f.ncplbnd = band + 1;
+}
+
+/// Coupling coordinates and phase flags.
+fn read_coupling_coords(r: &mut Bits, f: &mut Frame, mut x: Option<&mut eac3::FrameInfo>, acmod: usize) {
+ if !f.cplinu {
+ return;
+ }
+ let mut any = false;
+ for ch in 0..f.nfchans {
+ if !f.chincpl[ch] {
+ if let Some(e) = x.as_deref_mut() {
+ e.firstcplcos[ch] = true;
}
- _ => {
- if r.bit() {
- f.g.sdcycod = r.read(2) as usize;
- f.g.fdcycod = r.read(2) as usize;
- f.g.sgaincod = r.read(2) as usize;
- f.g.dbpbcod = r.read(2) as usize;
- f.g.floorcod = r.read(3) as usize;
- } else if blk == 0 && !eac3 {
- return Err(Error::Invalid("no bit allocation information in block 0"));
- }
+ continue;
+ }
+ let coe = match x.as_deref_mut() {
+ Some(e) if e.firstcplcos[ch] => {
+ e.firstcplcos[ch] = false;
+ true
}
+ _ => r.bit(),
+ };
+ if coe {
+ any = true;
+ read_channel_coupling_coords(r, f, ch);
}
- match x.as_deref() {
- Some(e) => {
- // SNR offset strategies (Table E2.9), fast gain codes
- let sets = [CPL, 0, 1, 2, 3, 4, LFE];
- match e.snroffststr {
- 0 => {
- f.csnroffst = e.frmcsnroffst;
- sets.iter().for_each(|&c| f.fsnroffst[c] = e.frmfsnroffst);
- }
- s if blk == 0 || r.bit() => {
- f.csnroffst = r.read(6) as i32;
- if s == 1 {
- let v = r.read(4) as i32;
- sets.iter().for_each(|&c| f.fsnroffst[c] = v);
- } else {
- if f.cplinu {
- f.fsnroffst[CPL] = r.read(4) as i32;
- }
- for ch in 0..nf {
- f.fsnroffst[ch] = r.read(4) as i32;
- }
- if lfeon {
- f.fsnroffst[LFE] = r.read(4) as i32;
- }
- }
- }
- _ => {}
- }
- if e.frmfgaincode && r.bit() {
- if f.cplinu {
- f.fgaincod[CPL] = r.read(3) as usize;
- }
- for ch in 0..nf {
- f.fgaincod[ch] = r.read(3) as usize;
- }
- if lfeon {
- f.fgaincod[LFE] = r.read(3) as usize;
- }
- } else {
- f.fgaincod = [4; 7];
- }
- if e.strmtyp == 0 && r.bit() {
- r.skip(10); // convsnroffst
+ }
+ if acmod == 2 && f.phsflginu && any {
+ for bnd in 0..f.ncplbnd {
+ let flag = r.bit();
+ for s in 0..(f.cplend - f.cplbegf) {
+ if f.sub_to_band[s] == bnd {
+ f.phsflg[s] = flag;
}
}
- None => {
- if r.bit() {
- f.csnroffst = r.read(6) as i32;
- if f.cplinu {
- f.fsnroffst[CPL] = r.read(4) as i32;
- f.fgaincod[CPL] = r.read(3) as usize;
- }
- for ch in 0..nf {
- f.fsnroffst[ch] = r.read(4) as i32;
- f.fgaincod[ch] = r.read(3) as usize;
- }
- if lfeon {
- f.fsnroffst[LFE] = r.read(4) as i32;
- f.fgaincod[LFE] = r.read(3) as usize;
- }
- } else if blk == 0 {
- return Err(Error::Invalid("no SNR offsets in block 0"));
- }
+ }
+ }
+}
+
+fn read_channel_coupling_coords(r: &mut Bits, f: &mut Frame, ch: usize) {
+ let mstr = r.read(2) as i32;
+ for bnd in 0..f.ncplbnd {
+ let e = r.read(4) as i32;
+ let m = r.read(4) as f32;
+ let v = if e == 15 { m / 16.0 } else { (m + 16.0) / 32.0 };
+ let co = v * 2f32.powi(-(e + 3 * mstr));
+ for s in 0..(f.cplend - f.cplbegf) {
+ if f.sub_to_band[s] == bnd {
+ f.cplco[ch][s] = co;
}
}
- if f.cplinu {
- let leak = match x.as_deref_mut() {
- Some(e) if e.firstcplleak => {
- e.firstcplleak = false;
- true
- }
- _ => r.bit(),
- };
- if leak {
- f.cplleak = (((r.read(3) as i32) << 8) + 768, ((r.read(3) as i32) << 8) + 768);
+ }
+}
+
+/// Rematrixing flags (E-AC-3 always sends them in block 0).
+fn read_rematrix_flags(r: &mut Bits, f: &mut Frame, eac3: bool, pos: BlockPos, spxbegf: Option<usize>) {
+ if pos.acmod == 2 && ((eac3 && pos.blk == 0) || r.bit()) {
+ let n = if f.cplinu {
+ match f.cplbegf {
+ 0 => 2,
+ 1 | 2 => 3,
+ _ => 4,
}
+ } else {
+ match spxbegf {
+ Some(b) if b < 2 => 3,
+ _ => 4,
+ }
+ };
+ f.rematflg = [false; 4];
+ for k in 0..n {
+ f.rematflg[k] = r.bit();
}
- // delta bit allocation
- if blk == 0 {
- f.deltbae = [2; 7];
+ }
+}
+
+/// Exponent strategies (E-AC-3: from the audio frame).
+fn read_exp_strategies(r: &mut Bits, f: &mut Frame, x: Option<&eac3::FrameInfo>, pos: BlockPos) {
+ let nf = f.nfchans;
+ match x {
+ Some(e) => {
+ if f.cplinu {
+ f.expstr[CPL] = e.cplexpstr[pos.blk];
+ }
+ f.expstr[..nf].copy_from_slice(&e.chexpstr[pos.blk][..nf]);
+ if pos.lfeon {
+ f.expstr[LFE] = e.lfeexpstr[pos.blk];
+ }
}
- if x.as_deref().is_none_or(|e| e.dbaflde) && r.bit() {
+ None => {
if f.cplinu {
- f.deltbae[CPL] = r.read(2) as u8;
+ f.expstr[CPL] = r.read(2) as u8;
}
for ch in 0..nf {
- f.deltbae[ch] = r.read(2) as u8;
+ f.expstr[ch] = r.read(2) as u8;
}
- let order: Vec<usize> = f.cplinu.then_some(CPL).into_iter().chain(0..nf).collect();
- for ch in order {
- if f.deltbae[ch] == 1 {
- let n = r.read(3) as usize + 1;
- f.deltsegs[ch] = (0..n).map(|_| (r.read(5) as u8, r.read(4) as u8, r.read(3) as u8)).collect();
- } else if f.deltbae[ch] == 3 {
- return Err(Error::Invalid("reserved delta bit allocation state"));
- }
+ if pos.lfeon {
+ f.expstr[LFE] = r.read(1) as u8;
}
}
- // skip field
- if x.as_deref().is_none_or(|e| e.skipflde) && r.bit() {
- let l = r.read(9) as usize;
- r.skip(l * 8);
- }
- // bit allocation for every exponent set
- let all_zero = f.csnroffst == 0 && (0..7).all(|c| f.fsnroffst[c] == 0);
- let snr = |fine: i32| (((f.csnroffst - 15) << 4) + fine) << 2;
- let mut sets: Vec<(usize, AllocParams)> = Vec::new();
- for ch in 0..nf {
- let dba = (f.deltbae[ch] <= 1).then(|| f.deltsegs[ch].clone());
- sets.push((ch, AllocParams { start: 0, end: f.endmant[ch], fgain: FASTGAIN[f.fgaincod[ch]], snroffset: snr(f.fsnroffst[ch]), leak: None, dba }));
+ }
+}
+
+/// End bin of each channel whose exponents are sent.
+fn read_channel_bandwidths(r: &mut Bits, f: &mut Frame, spx: &SpxBands) -> Result<()> {
+ for ch in 0..f.nfchans {
+ if f.expstr[ch] == 0 {
+ continue;
+ }
+ if f.chincpl[ch] && f.cplinu {
+ f.endmant[ch] = f.cplstrtmant();
+ } else if let (true, Some(begin)) = (spx.chans[ch], spx.begin) {
+ // §E3.3.3: the channel ends where spectral extension begins
+ f.endmant[ch] = begin;
+ } else {
+ let bw = r.read(6) as usize;
+ if bw > 60 {
+ return Err(Error::Invalid("chbwcod > 60"));
+ }
+ f.endmant[ch] = (bw + 12) * 3 + 37;
}
- if f.cplinu {
- let dba = (f.deltbae[CPL] <= 1).then(|| f.deltsegs[CPL].clone());
- sets.push((
- CPL,
- AllocParams {
- start: f.cplstrtmant(),
- end: f.cplendmant(),
- fgain: FASTGAIN[f.fgaincod[CPL]],
- snroffset: snr(f.fsnroffst[CPL]),
- leak: Some(f.cplleak),
- dba,
- },
- ));
+ }
+ Ok(())
+}
+
+/// Coupling, full-bandwidth and LFE exponents.
+fn read_exponents(r: &mut Bits, f: &mut Frame, lfeon: bool) -> Result<()> {
+ if f.cplinu && f.expstr[CPL] != 0 {
+ let abs = (r.read(4) as i32) << 1;
+ let grpsize = 1 << (f.expstr[CPL] - 1);
+ let ngrps = (f.cplendmant() - f.cplstrtmant()) / (3 * grpsize);
+ let first = f.cplstrtmant();
+ decode_exponents(r, abs, ngrps, grpsize, &mut f.exps[CPL], first, true)?;
+ }
+ for ch in 0..f.nfchans {
+ if f.expstr[ch] != 0 {
+ let abs = r.read(4) as i32;
+ let grpsize = 1usize << (f.expstr[ch] - 1);
+ let ngrps = (f.endmant[ch] - 1 + 3 * grpsize - 3) / (3 * grpsize);
+ decode_exponents(r, abs, ngrps, grpsize, &mut f.exps[ch], 0, false)?;
+ r.skip(2); // gainrng
}
- if lfeon {
- sets.push((LFE, AllocParams { start: 0, end: 7, fgain: FASTGAIN[f.fgaincod[LFE]], snroffset: snr(f.fsnroffst[LFE]), leak: None, dba: None }));
+ }
+ if lfeon && f.expstr[LFE] != 0 {
+ let abs = r.read(4) as i32;
+ decode_exponents(r, abs, 2, 1, &mut f.exps[LFE], 0, false)?;
+ }
+ Ok(())
+}
+
+/// Bit allocation parametric information.
+fn read_bit_alloc_params(r: &mut Bits, f: &mut Frame, x: Option<&eac3::FrameInfo>, pos: BlockPos) -> Result<()> {
+ match x {
+ Some(e) if !e.bamode => {
+ // §E2.2.4 defaults
+ (f.g.sdcycod, f.g.fdcycod, f.g.sgaincod, f.g.dbpbcod, f.g.floorcod) = (2, 1, 1, 2, 7);
}
- for (ch, p) in &sets {
- f.bap[*ch] = [0; 256];
- if !all_zero {
- let exps = f.exps[*ch];
- bit_allocation(&exps, p, &f.g, &mut f.bap[*ch]);
+ _ => {
+ if r.bit() {
+ f.g.sdcycod = r.read(2) as usize;
+ f.g.fdcycod = r.read(2) as usize;
+ f.g.sgaincod = r.read(2) as usize;
+ f.g.dbpbcod = r.read(2) as usize;
+ f.g.floorcod = r.read(3) as usize;
+ } else if pos.blk == 0 && x.is_none() {
+ return Err(Error::Invalid("no bit allocation information in block 0"));
}
}
- // mantissas
- let mut gr = Groups::default();
- let mut cpl = [0f32; 256];
- let mut got_cpl = false;
- for ch in 0..nf {
- for bin in 0..f.endmant[ch] {
- let b = f.bap[ch][bin];
- let m = if b == 0 { if f.dithflag[ch] { self.dither() } else { 0.0 } } else { mantissa(r, b, &mut gr) };
- coefs[ch][bin] = m * 2f32.powi(-(f.exps[ch][bin] as i32));
- }
- if f.cplinu && f.chincpl[ch] && !got_cpl {
- got_cpl = true;
- for bin in f.cplstrtmant()..f.cplendmant() {
- let b = f.bap[CPL][bin];
- // zero-bit coupling mantissas are dithered per channel when decoupling
- cpl[bin] = if b == 0 { f32::NAN } else { mantissa(r, b, &mut gr) };
+ }
+ Ok(())
+}
+
+/// E-AC-3 SNR offset strategies (Table E2.9) and fast gain codes.
+fn read_eac3_snr_offsets(r: &mut Bits, f: &mut Frame, e: &eac3::FrameInfo, pos: BlockPos) {
+ let nf = f.nfchans;
+ let sets = [CPL, 0, 1, 2, 3, 4, LFE];
+ match e.snroffststr {
+ 0 => {
+ f.csnroffst = e.frmcsnroffst;
+ sets.iter().for_each(|&c| f.fsnroffst[c] = e.frmfsnroffst);
+ }
+ s if pos.blk == 0 || r.bit() => {
+ f.csnroffst = r.read(6) as i32;
+ if s == 1 {
+ let v = r.read(4) as i32;
+ sets.iter().for_each(|&c| f.fsnroffst[c] = v);
+ } else {
+ if f.cplinu {
+ f.fsnroffst[CPL] = r.read(4) as i32;
+ }
+ for ch in 0..nf {
+ f.fsnroffst[ch] = r.read(4) as i32;
+ }
+ if pos.lfeon {
+ f.fsnroffst[LFE] = r.read(4) as i32;
}
}
}
- if lfeon {
- for bin in 0..7 {
- let b = f.bap[LFE][bin];
- let m = if b == 0 { 0.0 } else { mantissa(r, b, &mut gr) };
- coefs[LFE][bin] = m * 2f32.powi(-(f.exps[LFE][bin] as i32));
- }
+ _ => {}
+ }
+ if e.frmfgaincode && r.bit() {
+ if f.cplinu {
+ f.fgaincod[CPL] = r.read(3) as usize;
+ }
+ for ch in 0..nf {
+ f.fgaincod[ch] = r.read(3) as usize;
+ }
+ if pos.lfeon {
+ f.fgaincod[LFE] = r.read(3) as usize;
}
- // decoupling
+ } else {
+ f.fgaincod = [4; 7];
+ }
+ if e.strmtyp == 0 && r.bit() {
+ r.skip(10); // convsnroffst
+ }
+}
+
+/// AC-3 SNR offsets and fast gain codes.
+fn read_ac3_snr_offsets(r: &mut Bits, f: &mut Frame, pos: BlockPos) -> Result<()> {
+ if r.bit() {
+ f.csnroffst = r.read(6) as i32;
if f.cplinu {
- for ch in 0..nf {
- if !f.chincpl[ch] {
- continue;
- }
- for bin in f.cplstrtmant()..f.cplendmant() {
- let s = (bin - 37) / 12 - f.cplbegf;
- let m = if cpl[bin].is_nan() { if f.dithflag[ch] { self.dither() } else { 0.0 } } else { cpl[bin] };
- let mut v = m * 2f32.powi(-(f.exps[CPL][bin] as i32)) * f.cplco[ch][s] * 8.0;
- if acmod == 2 && ch == 1 && f.phsflginu && f.phsflg[s] {
- v = -v;
- }
- coefs[ch][bin] = v;
- }
- }
+ f.fsnroffst[CPL] = r.read(4) as i32;
+ f.fgaincod[CPL] = r.read(3) as usize;
}
- // rematrixing
- if acmod == 2 {
- let end = if f.cplinu { f.cplstrtmant() } else { f.endmant[0].min(f.endmant[1]) };
- const B: [usize; 5] = [13, 25, 37, 61, 253];
- for k in 0..4 {
- if f.rematflg[k] {
- for bin in B[k]..B[k + 1].min(end) {
- let (l, rr) = (coefs[0][bin], coefs[1][bin]);
- coefs[0][bin] = l + rr;
- coefs[1][bin] = l - rr;
- }
- }
- }
+ for ch in 0..f.nfchans {
+ f.fsnroffst[ch] = r.read(4) as i32;
+ f.fgaincod[ch] = r.read(3) as usize;
}
- // spectral extension: synthesize the high band of each channel from its low band
- if let Some(e) = x.as_deref()
- && e.spx.inu
- {
- for ch in 0..nf {
- if e.spx.chinspx[ch] {
- e.spx.synthesize(ch, e.spxatten[ch], &mut coefs[ch], || self.noise());
- }
- }
+ if pos.lfeon {
+ f.fsnroffst[LFE] = r.read(4) as i32;
+ f.fgaincod[LFE] = r.read(3) as usize;
+ }
+ } else if pos.blk == 0 {
+ return Err(Error::Invalid("no SNR offsets in block 0"));
+ }
+ Ok(())
+}
+
+/// Coupling leak initialisation (E-AC-3 sends it only on the first use).
+fn read_coupling_leak(r: &mut Bits, f: &mut Frame, x: Option<&mut eac3::FrameInfo>) {
+ if !f.cplinu {
+ return;
+ }
+ let leak = match x {
+ Some(e) if e.firstcplleak => {
+ e.firstcplleak = false;
+ true
+ }
+ _ => r.bit(),
+ };
+ if leak {
+ f.cplleak = (((r.read(3) as i32) << 8) + 768, ((r.read(3) as i32) << 8) + 768);
+ }
+}
+
+/// Delta bit allocation.
+fn read_delta_bit_alloc(r: &mut Bits, f: &mut Frame, x: Option<&eac3::FrameInfo>, blk: usize) -> Result<()> {
+ let nf = f.nfchans;
+ if blk == 0 {
+ f.deltbae = [2; 7];
+ }
+ if x.is_none_or(|e| e.dbaflde) && r.bit() {
+ if f.cplinu {
+ f.deltbae[CPL] = r.read(2) as u8;
}
- // dynamic range
for ch in 0..nf {
- let g = if acmod == 0 && ch == 1 { f.dynrng[1] } else { f.dynrng[0] };
- if g != 1.0 {
- coefs[ch].iter_mut().for_each(|c| *c *= g);
+ f.deltbae[ch] = r.read(2) as u8;
+ }
+ let order: Vec<usize> = f.cplinu.then_some(CPL).into_iter().chain(0..nf).collect();
+ for ch in order {
+ if f.deltbae[ch] == 1 {
+ let n = r.read(3) as usize + 1;
+ f.deltsegs[ch] = (0..n).map(|_| (r.read(5) as u8, r.read(4) as u8, r.read(3) as u8)).collect();
+ } else if f.deltbae[ch] == 3 {
+ return Err(Error::Invalid("reserved delta bit allocation state"));
+ }
+ }
+ }
+ Ok(())
+}
+
+/// Bit allocation (bap) for every exponent set.
+fn allocate_bits(f: &mut Frame, lfeon: bool) {
+ let all_zero = f.csnroffst == 0 && (0..7).all(|c| f.fsnroffst[c] == 0);
+ let snr = |fine: i32| (((f.csnroffst - 15) << 4) + fine) << 2;
+ let mut sets: Vec<(usize, AllocParams)> = Vec::new();
+ for ch in 0..f.nfchans {
+ let dba = (f.deltbae[ch] <= 1).then(|| f.deltsegs[ch].clone());
+ sets.push((ch, AllocParams { start: 0, end: f.endmant[ch], fgain: FASTGAIN[f.fgaincod[ch]], snroffset: snr(f.fsnroffst[ch]), leak: None, dba }));
+ }
+ if f.cplinu {
+ let dba = (f.deltbae[CPL] <= 1).then(|| f.deltsegs[CPL].clone());
+ sets.push((
+ CPL,
+ AllocParams {
+ start: f.cplstrtmant(),
+ end: f.cplendmant(),
+ fgain: FASTGAIN[f.fgaincod[CPL]],
+ snroffset: snr(f.fsnroffst[CPL]),
+ leak: Some(f.cplleak),
+ dba,
+ },
+ ));
+ }
+ if lfeon {
+ sets.push((LFE, AllocParams { start: 0, end: 7, fgain: FASTGAIN[f.fgaincod[LFE]], snroffset: snr(f.fsnroffst[LFE]), leak: None, dba: None }));
+ }
+ for (ch, p) in &sets {
+ f.bap[*ch] = [0; 256];
+ if !all_zero {
+ let exps = f.exps[*ch];
+ bit_allocation(&exps, p, &f.g, &mut f.bap[*ch]);
+ }
+ }
+}
+
+/// Undo the stereo rematrixing of the flagged bands.
+fn rematrix(f: &Frame, coefs: &mut [[f32; 256]; 6]) {
+ let end = if f.cplinu { f.cplstrtmant() } else { f.endmant[0].min(f.endmant[1]) };
+ const B: [usize; 5] = [13, 25, 37, 61, 253];
+ for k in 0..4 {
+ if f.rematflg[k] {
+ for bin in B[k]..B[k + 1].min(end) {
+ let (l, rr) = (coefs[0][bin], coefs[1][bin]);
+ coefs[0][bin] = l + rr;
+ coefs[1][bin] = l - rr;
}
}
- if lfeon && f.dynrng[0] != 1.0 {
- coefs[LFE].iter_mut().for_each(|c| *c *= f.dynrng[0]);
+ }
+}
+
+/// Apply the dynamic range gain.
+fn apply_dynrng(f: &Frame, pos: BlockPos, coefs: &mut [[f32; 256]; 6]) {
+ for ch in 0..f.nfchans {
+ let g = if pos.acmod == 0 && ch == 1 { f.dynrng[1] } else { f.dynrng[0] };
+ if g != 1.0 {
+ coefs[ch].iter_mut().for_each(|c| *c *= g);
}
- Ok(())
+ }
+ if pos.lfeon && f.dynrng[0] != 1.0 {
+ coefs[LFE].iter_mut().for_each(|c| *c *= f.dynrng[0]);
}
}
diff --git a/crates/ac3/src/tables.rs b/crates/ac3/src/tables.rs
index 29ec3cfb4..c233715f6 100644
--- a/crates/ac3/src/tables.rs
+++ b/crates/ac3/src/tables.rs
@@ -47,23 +47,19 @@ pub(crate) const HTH: [[u16; 50]; 3] = [
848, 832, 816, 800, 784, 768, 752, 752, 752, 768, 784, 816, 848, 960, 1040, 1136, 1184, 1120, 1088, 1104, 1248,
],
];
-/// Table 7.33 as printed (5 decimals), for checking the computed window.
+/// Table 7.33 as printed, in units of 1e-5, for checking the computed window.
#[cfg(test)]
-#[allow(clippy::approx_constant)]
-pub(crate) const WINDOW_TABLE: [f32; 256] = [
- 0.00014, 0.00024, 0.00037, 0.00051, 0.00067, 0.00086, 0.00107, 0.0013, 0.00157, 0.00187, 0.0022, 0.00256, 0.00297, 0.00341, 0.0039, 0.00443, 0.00501,
- 0.00564, 0.00632, 0.00706, 0.00785, 0.00871, 0.00962, 0.01061, 0.01166, 0.01279, 0.01399, 0.01526, 0.01662, 0.01806, 0.01959, 0.02121, 0.02292, 0.02472,
- 0.02662, 0.02863, 0.03073, 0.03294, 0.03527, 0.0377, 0.04025, 0.04292, 0.04571, 0.04862, 0.05165, 0.05481, 0.0581, 0.06153, 0.06508, 0.06878, 0.07261,
- 0.07658, 0.08069, 0.08495, 0.08935, 0.09389, 0.09859, 0.10343, 0.10842, 0.11356, 0.11885, 0.12429, 0.12988, 0.13563, 0.14152, 0.14757, 0.15376, 0.16011,
- 0.16661, 0.17325, 0.18005, 0.18699, 0.19407, 0.2013, 0.20867, 0.21618, 0.22382, 0.23161, 0.23952, 0.24757, 0.25574, 0.26404, 0.27246, 0.281, 0.28965,
- 0.29841, 0.30729, 0.31626, 0.32533, 0.3345, 0.34376, 0.35311, 0.36253, 0.37204, 0.38161, 0.39126, 0.40096, 0.41072, 0.42054, 0.4304, 0.4403, 0.45023,
- 0.4602, 0.47019, 0.4802, 0.49022, 0.50025, 0.51028, 0.52031, 0.53033, 0.54033, 0.55031, 0.56026, 0.57019, 0.58007, 0.58991, 0.5997, 0.60944, 0.61912,
- 0.62873, 0.63827, 0.64774, 0.65713, 0.66643, 0.67564, 0.68476, 0.69377, 0.70269, 0.7115, 0.72019, 0.72877, 0.73723, 0.74557, 0.75378, 0.76186, 0.76981,
- 0.77762, 0.7853, 0.79283, 0.80022, 0.80747, 0.81457, 0.82151, 0.82831, 0.83496, 0.84145, 0.84779, 0.85398, 0.86001, 0.86588, 0.8716, 0.87716, 0.88257,
- 0.88782, 0.89291, 0.89785, 0.90264, 0.90728, 0.91176, 0.9161, 0.92028, 0.92432, 0.92822, 0.93197, 0.93558, 0.93906, 0.9424, 0.9456, 0.94867, 0.95162,
- 0.95444, 0.95713, 0.95971, 0.96217, 0.96451, 0.96674, 0.96887, 0.97089, 0.97281, 0.97463, 0.97635, 0.97799, 0.97953, 0.98099, 0.98236, 0.98366, 0.98488,
- 0.98602, 0.9871, 0.98811, 0.98905, 0.98994, 0.99076, 0.99153, 0.99225, 0.99291, 0.99353, 0.99411, 0.99464, 0.99513, 0.99558, 0.996, 0.99639, 0.99674,
- 0.99706, 0.99736, 0.99763, 0.99788, 0.99811, 0.99831, 0.9985, 0.99867, 0.99882, 0.99895, 0.99908, 0.99919, 0.99929, 0.99938, 0.99946, 0.99953, 0.99959,
- 0.99965, 0.99969, 0.99974, 0.99978, 0.99981, 0.99984, 0.99986, 0.99988, 0.9999, 0.99992, 0.99993, 0.99994, 0.99995, 0.99996, 0.99997, 0.99998, 0.99998,
- 0.99998, 0.99999, 0.99999, 0.99999, 0.99999, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
+pub(crate) const WINDOW_TABLE: [u32; 256] = [
+ 14, 24, 37, 51, 67, 86, 107, 130, 157, 187, 220, 256, 297, 341, 390, 443, 501, 564, 632, 706, 785, 871, 962, 1061, 1166, 1279, 1399, 1526, 1662, 1806,
+ 1959, 2121, 2292, 2472, 2662, 2863, 3073, 3294, 3527, 3770, 4025, 4292, 4571, 4862, 5165, 5481, 5810, 6153, 6508, 6878, 7261, 7658, 8069, 8495, 8935, 9389,
+ 9859, 10343, 10842, 11356, 11885, 12429, 12988, 13563, 14152, 14757, 15376, 16011, 16661, 17325, 18005, 18699, 19407, 20130, 20867, 21618, 22382, 23161,
+ 23952, 24757, 25574, 26404, 27246, 28100, 28965, 29841, 30729, 31626, 32533, 33450, 34376, 35311, 36253, 37204, 38161, 39126, 40096, 41072, 42054, 43040,
+ 44030, 45023, 46020, 47019, 48020, 49022, 50025, 51028, 52031, 53033, 54033, 55031, 56026, 57019, 58007, 58991, 59970, 60944, 61912, 62873, 63827, 64774,
+ 65713, 66643, 67564, 68476, 69377, 70269, 71150, 72019, 72877, 73723, 74557, 75378, 76186, 76981, 77762, 78530, 79283, 80022, 80747, 81457, 82151, 82831,
+ 83496, 84145, 84779, 85398, 86001, 86588, 87160, 87716, 88257, 88782, 89291, 89785, 90264, 90728, 91176, 91610, 92028, 92432, 92822, 93197, 93558, 93906,
+ 94240, 94560, 94867, 95162, 95444, 95713, 95971, 96217, 96451, 96674, 96887, 97089, 97281, 97463, 97635, 97799, 97953, 98099, 98236, 98366, 98488, 98602,
+ 98710, 98811, 98905, 98994, 99076, 99153, 99225, 99291, 99353, 99411, 99464, 99513, 99558, 99600, 99639, 99674, 99706, 99736, 99763, 99788, 99811, 99831,
+ 99850, 99867, 99882, 99895, 99908, 99919, 99929, 99938, 99946, 99953, 99959, 99965, 99969, 99974, 99978, 99981, 99984, 99986, 99988, 99990, 99992, 99993,
+ 99994, 99995, 99996, 99997, 99998, 99998, 99998, 99999, 99999, 99999, 99999, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
+ 100000, 100000, 100000, 100000,
];
diff --git a/crates/ac3/src/tests.rs b/crates/ac3/src/tests.rs
index c77d97b7a..3388e4184 100644
--- a/crates/ac3/src/tests.rs
+++ b/crates/ac3/src/tests.rs
@@ -4,6 +4,7 @@ use super::*;
fn computed_window_matches_table_7_33() {
let w = window();
for (n, (&a, &b)) in w.iter().zip(tables::WINDOW_TABLE.iter()).enumerate() {
+ let b = b as f32 * 1e-5;
assert!((a - b).abs() <= 6e-6, "w[{n}] = {a}, table {b}");
}
// princen-bradley: w[n]² + w[255-n]²... (the second half mirrors): w[n]² + w[511-n]² = 1