From 275e634a6b002569656ae55807817599dfe60691 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 10:16:45 +0300 Subject: [PATCH] ec-ac3: mantissas as 2^23 integers, floor-shifted by exponent like ffmpeg ffmpeg n8.0 ac3dec.c ac3_decode_transform_coeffs_ch stores each mantissa (dither included, ac3dec.c:412, 466) as an int at 2^23 full scale and computes coeffs[freq] = mantissa >> exps[freq] before the float conversion (1/4194304 gain, ac3dec.c:1290). We multiplied floats by 2^-exp, so zero-bit dither and small mantissas on quiet bins kept bits ffmpeg drops (up to 2^-23 per bin, ~20% of a quiet bin). The LFG generator was already identical (av_lfg_init(0), never reseeded, ac3dec.c:124/156, default cons_noisegen=false); the mismatch was the rounding, not the noise. Symmetric levels and the dither now use ffmpeg's integer values (SYMMETRIC_DEQUANT, ac3dec_data.c:57). AHT and E-AC-3 spx/cpl paths are unchanged and still float. --- crates/ec-ac3/src/decode.rs | 21 ++++++++-------- crates/ec-ac3/src/mantissa.rs | 45 ++++++++++++++++++----------------- crates/ec-ac3/src/tables.rs | 20 +++++++++------- 3 files changed, 44 insertions(+), 42 deletions(-) diff --git a/crates/ec-ac3/src/decode.rs b/crates/ec-ac3/src/decode.rs index e0b09bca..8c9ada8f 100644 --- a/crates/ec-ac3/src/decode.rs +++ b/crates/ec-ac3/src/decode.rs @@ -12,7 +12,7 @@ use ec_core::{BitReader, Error, Result}; use crate::bitalloc::{self, Allocation, BitAllocParams, Channel, DeltaBa}; use crate::bsi::{Acmod, Bsi}; use crate::exps::{self, Strategy}; -use crate::mantissa::{Dither, Mantissas, scale}; +use crate::mantissa::{Dither, Mantissas, scale, shift}; use crate::transform::{BLOCK_SAMPLES, Imdct}; /// Full-bandwidth channels at most (3/2 mode). @@ -573,11 +573,12 @@ impl Core { for bin in 0..end { let bap = self.bap[ch][bin]; let value = m.read(r, bap)?; - self.coeffs[ch][bin] = if bap == 0 && self.dithflag[ch] && self.dither_on { - self.dither.value() * scale(self.exps[ch][bin]) + let mant = if bap == 0 && self.dithflag[ch] && self.dither_on { + self.dither.mantissa() } else { - value * scale(self.exps[ch][bin]) + value }; + self.coeffs[ch][bin] = shift(mant, self.exps[ch][bin]); } } self.coeffs[ch][end..].fill(0.0); @@ -589,14 +590,12 @@ impl Core { for bin in start..end { let bap = self.bap[CPL][bin]; let value = m.read(r, bap)?; - // The coupling channel always draws dither for its zero-bit - // mantissas, whatever the coupled channels' dithflag says: - // each coupled channel decides later whether to keep it. - self.coeffs[CPL][bin] = if bap == 0 && self.dither_on { - self.dither.value() * scale(self.exps[CPL][bin]) + let mant = if bap == 0 && self.dither_on { + self.dither.mantissa() } else { - value * scale(self.exps[CPL][bin]) + value }; + self.coeffs[CPL][bin] = shift(mant, self.exps[CPL][bin]); } } got_cplchan = true; @@ -608,7 +607,7 @@ impl Core { } else { for bin in 0..7 { let value = m.read(r, self.bap[LFE][bin])?; - self.coeffs[LFE][bin] = value * scale(self.exps[LFE][bin]); + self.coeffs[LFE][bin] = shift(value, self.exps[LFE][bin]); } } self.coeffs[LFE][7..].fill(0.0); diff --git a/crates/ec-ac3/src/mantissa.rs b/crates/ec-ac3/src/mantissa.rs index 97d2e558..6ae2f06a 100644 --- a/crates/ec-ac3/src/mantissa.rs +++ b/crates/ec-ac3/src/mantissa.rs @@ -62,8 +62,8 @@ impl Dither { self.mantissa() as f32 / 5_931_008.0 * 1.732_050_8 } - /// ffmpeg's integer dither mantissa: `((x >> 8) * 181 >> 8) - 5931008`. - fn mantissa(&mut self) -> i32 { + /// ffmpeg's integer dither mantissa at 2^23 full scale: `((x >> 8) * 181 >> 8) - 5931008`. + pub fn mantissa(&mut self) -> i32 { let x = self.next_word(); let u = ((x >> 8) * 181) >> 8; u as i32 - 5_931_008 @@ -87,11 +87,11 @@ impl Dither { /// the next channel begins. #[derive(Debug, Clone, Copy, Default)] pub struct Mantissas { - group3: [f32; 3], + group3: [i32; 3], left3: usize, - group5: [f32; 3], + group5: [i32; 3], left5: usize, - group11: [f32; 2], + group11: [i32; 2], left11: usize, } @@ -101,14 +101,14 @@ impl Mantissas { Mantissas::default() } - /// One mantissa value in `[-1, 1)`, before the exponent is applied. + /// One mantissa in `[-2^23, 2^23)`, before the exponent is applied. /// - /// `bap == 0` returns 0.0; the caller decides whether that bin gets dither, + /// `bap == 0` returns 0; the caller decides whether that bin gets dither, /// because in the coupled range the decision belongs to the channel rather /// than to the coupling channel the mantissa came from. - pub fn read(&mut self, r: &mut BitReader<'_>, bap: u8) -> Result { + pub fn read(&mut self, r: &mut BitReader<'_>, bap: u8) -> Result { Ok(match bap { - 0 => 0.0, + 0 => 0, 1 => { if self.left3 == 0 { let code = r.read_bits(5)?.min(26); @@ -159,13 +159,18 @@ impl Mantissas { // 6..=15: asymmetric two's complement, decimal point left of // the MSB (§7.3.2). let bits = QNTZTAB[(bap as usize).min(15)]; - let value = r.read_signed(bits)?; - value as f32 / (1u32 << (bits - 1)) as f32 + r.read_signed(bits)? << (24 - bits) } }) } } +/// A mantissa after the exponent shift. ffmpeg shifts the integer mantissa +/// right, flooring, before the float conversion; so does this. +pub fn shift(mantissa: i32, exponent: u8) -> f32 { + (mantissa >> exponent) as f32 / 8_388_608.0 +} + /// `2^-exponent`, the right shift §7.3 applies to every mantissa. pub fn scale(exponent: u8) -> f32 { // Exponents are 0..=24, so a table lookup would buy nothing over the @@ -188,14 +193,10 @@ mod tests { let bytes = w.into_bytes(); let mut r = BitReader::new(&bytes); let mut m = Mantissas::new(); - let got: Vec = (0..3).map(|_| m.read(&mut r, 1).unwrap()).collect(); - assert!((got[0] - 2.0 / 3.0).abs() < 1e-6); - assert!(got[1].abs() < 1e-6); - assert!((got[2] + 2.0 / 3.0).abs() < 1e-6); - let a = m.read(&mut r, 4).unwrap(); - let b = m.read(&mut r, 4).unwrap(); - assert!((a - 10.0 / 11.0).abs() < 1e-6); - assert!((b + 10.0 / 11.0).abs() < 1e-6); + let got: Vec = (0..3).map(|_| m.read(&mut r, 1).unwrap()).collect(); + assert_eq!(got, [5_592_405, 0, -5_592_405]); + assert_eq!(m.read(&mut r, 4).unwrap(), 7_626_007); + assert_eq!(m.read(&mut r, 4).unwrap(), -7_626_007); } #[test] @@ -206,9 +207,9 @@ mod tests { let bytes = w.into_bytes(); let mut r = BitReader::new(&bytes); let mut m = Mantissas::new(); - assert!((m.read(&mut r, 7).unwrap() + 1.0).abs() < 1e-6); - assert!((m.read(&mut r, 7).unwrap() - 31.0 / 32.0).abs() < 1e-6); - assert_eq!(m.read(&mut r, 0).unwrap(), 0.0); + assert_eq!(m.read(&mut r, 7).unwrap(), -(1 << 23)); + assert_eq!(m.read(&mut r, 7).unwrap(), 31 << 18); + assert_eq!(m.read(&mut r, 0).unwrap(), 0); } #[test] diff --git a/crates/ec-ac3/src/tables.rs b/crates/ec-ac3/src/tables.rs index 9e88a860..465d626f 100644 --- a/crates/ec-ac3/src/tables.rs +++ b/crates/ec-ac3/src/tables.rs @@ -154,9 +154,11 @@ pub const QNTZTAB: [u32; 16] = [0, 5, 7, 3, 7, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, pub const QUANT_LEVELS: [u32; 6] = [0, 3, 5, 7, 11, 15]; /// The `k`-th reconstruction level of a `levels`-level symmetric quantizer -/// (Tables 7.19-7.23): the uniform midtread set `(2k - (levels-1)) / levels`. -pub fn symmetric_level(levels: u32, k: u32) -> f32 { - (2.0 * k as f32 - (levels as f32 - 1.0)) / levels as f32 +/// (Tables 7.19-7.23), `(2k - (levels-1)) / levels` at 2^23 full scale, +/// truncated toward zero as ffmpeg's `SYMMETRIC_DEQUANT` is. +pub fn symmetric_level(levels: u32, k: u32) -> i32 { + let levels = levels as i32; + (2 * k as i32 - (levels - 1)) * (1 << 23) / levels } /// Transform window `w[]`: 256 values used back to back to form the 512-point @@ -216,12 +218,12 @@ mod tests { #[test] fn symmetric_quantizer_matches_printed_tables() { // Tables 7.19-7.23, as printed. - assert!((symmetric_level(3, 0) + 2.0 / 3.0).abs() < 1e-6); - assert!(symmetric_level(3, 1).abs() < 1e-6); - assert!((symmetric_level(5, 3) - 2.0 / 5.0).abs() < 1e-6); - assert!((symmetric_level(7, 6) - 6.0 / 7.0).abs() < 1e-6); - assert!((symmetric_level(11, 0) + 10.0 / 11.0).abs() < 1e-6); - assert!((symmetric_level(15, 14) - 14.0 / 15.0).abs() < 1e-6); + assert_eq!(symmetric_level(3, 0), -5_592_405); + assert_eq!(symmetric_level(3, 1), 0); + assert_eq!(symmetric_level(5, 3), 3_355_443); + assert_eq!(symmetric_level(7, 6), 7_190_235); + assert_eq!(symmetric_level(11, 0), -7_626_007); + assert_eq!(symmetric_level(15, 14), 7_829_367); } #[test]