forge
Explore
Log in

Don't you dare go Hollow

ec-ac3: mantissas as 2^23 integers, floor-shifted by exponent like ffmpeg

Browse files at this commit

275e634a6b002569656ae55807817599dfe60691Ahmet Kaan GümüşParent: 76482df
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.rsmodified+10 -11
@@ -12,7 +12,7 @@ use ec_core::{BitReader, Error, Result};
12 use crate::bitalloc::{self, Allocation, BitAllocParams, Channel, DeltaBa};12 use crate::bitalloc::{self, Allocation, BitAllocParams, Channel, DeltaBa};
13 use crate::bsi::{Acmod, Bsi};13 use crate::bsi::{Acmod, Bsi};
14 use crate::exps::{self, Strategy};14 use crate::exps::{self, Strategy};
15-use crate::mantissa::{Dither, Mantissas, scale};15+use crate::mantissa::{Dither, Mantissas, scale, shift};
16 use crate::transform::{BLOCK_SAMPLES, Imdct};16 use crate::transform::{BLOCK_SAMPLES, Imdct};
17 17
18 /// Full-bandwidth channels at most (3/2 mode).18 /// Full-bandwidth channels at most (3/2 mode).
@@ -573,11 +573,12 @@ impl Core {
573 for bin in 0..end {573 for bin in 0..end {
574 let bap = self.bap[ch][bin];574 let bap = self.bap[ch][bin];
575 let value = m.read(r, bap)?;575 let value = m.read(r, bap)?;
576- self.coeffs[ch][bin] = if bap == 0 && self.dithflag[ch] && self.dither_on {576+ let mant = if bap == 0 && self.dithflag[ch] && self.dither_on {
577- self.dither.value() * scale(self.exps[ch][bin])577+ self.dither.mantissa()
578 } else {578 } else {
579- value * scale(self.exps[ch][bin])579+ value
580 };580 };
581+ self.coeffs[ch][bin] = shift(mant, self.exps[ch][bin]);
581 }582 }
582 }583 }
583 self.coeffs[ch][end..].fill(0.0);584 self.coeffs[ch][end..].fill(0.0);
@@ -589,14 +590,12 @@ impl Core {
589 for bin in start..end {590 for bin in start..end {
590 let bap = self.bap[CPL][bin];591 let bap = self.bap[CPL][bin];
591 let value = m.read(r, bap)?;592 let value = m.read(r, bap)?;
592- // The coupling channel always draws dither for its zero-bit593+ let mant = if bap == 0 && self.dither_on {
593- // mantissas, whatever the coupled channels' dithflag says:594+ self.dither.mantissa()
594- // each coupled channel decides later whether to keep it.
595- self.coeffs[CPL][bin] = if bap == 0 && self.dither_on {
596- self.dither.value() * scale(self.exps[CPL][bin])
597 } else {595 } else {
598- value * scale(self.exps[CPL][bin])596+ value
599 };597 };
598+ self.coeffs[CPL][bin] = shift(mant, self.exps[CPL][bin]);
600 }599 }
601 }600 }
602 got_cplchan = true;601 got_cplchan = true;
@@ -608,7 +607,7 @@ impl Core {
608 } else {607 } else {
609 for bin in 0..7 {608 for bin in 0..7 {
610 let value = m.read(r, self.bap[LFE][bin])?;609 let value = m.read(r, self.bap[LFE][bin])?;
611- self.coeffs[LFE][bin] = value * scale(self.exps[LFE][bin]);610+ self.coeffs[LFE][bin] = shift(value, self.exps[LFE][bin]);
612 }611 }
613 }612 }
614 self.coeffs[LFE][7..].fill(0.0);613 self.coeffs[LFE][7..].fill(0.0);
crates/ec-ac3/src/mantissa.rsmodified+23 -22
@@ -62,8 +62,8 @@ impl Dither {
62 self.mantissa() as f32 / 5_931_008.0 * 1.732_050_862 self.mantissa() as f32 / 5_931_008.0 * 1.732_050_8
63 }63 }
64 64
65- /// ffmpeg's integer dither mantissa: `((x >> 8) * 181 >> 8) - 5931008`.65+ /// ffmpeg's integer dither mantissa at 2^23 full scale: `((x >> 8) * 181 >> 8) - 5931008`.
66- fn mantissa(&mut self) -> i32 {66+ pub fn mantissa(&mut self) -> i32 {
67 let x = self.next_word();67 let x = self.next_word();
68 let u = ((x >> 8) * 181) >> 8;68 let u = ((x >> 8) * 181) >> 8;
69 u as i32 - 5_931_00869 u as i32 - 5_931_008
@@ -87,11 +87,11 @@ impl Dither {
87 /// the next channel begins.87 /// the next channel begins.
88 #[derive(Debug, Clone, Copy, Default)]88 #[derive(Debug, Clone, Copy, Default)]
89 pub struct Mantissas {89 pub struct Mantissas {
90- group3: [f32; 3],90+ group3: [i32; 3],
91 left3: usize,91 left3: usize,
92- group5: [f32; 3],92+ group5: [i32; 3],
93 left5: usize,93 left5: usize,
94- group11: [f32; 2],94+ group11: [i32; 2],
95 left11: usize,95 left11: usize,
96 }96 }
97 97
@@ -101,14 +101,14 @@ impl Mantissas {
101 Mantissas::default()101 Mantissas::default()
102 }102 }
103 103
104- /// One mantissa value in `[-1, 1)`, before the exponent is applied.104+ /// One mantissa in `[-2^23, 2^23)`, before the exponent is applied.
105 ///105 ///
106- /// `bap == 0` returns 0.0; the caller decides whether that bin gets dither,106+ /// `bap == 0` returns 0; the caller decides whether that bin gets dither,
107 /// because in the coupled range the decision belongs to the channel rather107 /// because in the coupled range the decision belongs to the channel rather
108 /// than to the coupling channel the mantissa came from.108 /// than to the coupling channel the mantissa came from.
109- pub fn read(&mut self, r: &mut BitReader<'_>, bap: u8) -> Result<f32> {109+ pub fn read(&mut self, r: &mut BitReader<'_>, bap: u8) -> Result<i32> {
110 Ok(match bap {110 Ok(match bap {
111- 0 => 0.0,111+ 0 => 0,
112 1 => {112 1 => {
113 if self.left3 == 0 {113 if self.left3 == 0 {
114 let code = r.read_bits(5)?.min(26);114 let code = r.read_bits(5)?.min(26);
@@ -159,13 +159,18 @@ impl Mantissas {
159 // 6..=15: asymmetric two's complement, decimal point left of159 // 6..=15: asymmetric two's complement, decimal point left of
160 // the MSB (§7.3.2).160 // the MSB (§7.3.2).
161 let bits = QNTZTAB[(bap as usize).min(15)];161 let bits = QNTZTAB[(bap as usize).min(15)];
162- let value = r.read_signed(bits)?;162+ r.read_signed(bits)? << (24 - bits)
163- value as f32 / (1u32 << (bits - 1)) as f32
164 }163 }
165 })164 })
166 }165 }
167 }166 }
168 167
168+/// A mantissa after the exponent shift. ffmpeg shifts the integer mantissa
169+/// right, flooring, before the float conversion; so does this.
170+pub fn shift(mantissa: i32, exponent: u8) -> f32 {
171+ (mantissa >> exponent) as f32 / 8_388_608.0
172+}
173+
169 /// `2^-exponent`, the right shift §7.3 applies to every mantissa.174 /// `2^-exponent`, the right shift §7.3 applies to every mantissa.
170 pub fn scale(exponent: u8) -> f32 {175 pub fn scale(exponent: u8) -> f32 {
171 // Exponents are 0..=24, so a table lookup would buy nothing over the176 // Exponents are 0..=24, so a table lookup would buy nothing over the
@@ -188,14 +193,10 @@ mod tests {
188 let bytes = w.into_bytes();193 let bytes = w.into_bytes();
189 let mut r = BitReader::new(&bytes);194 let mut r = BitReader::new(&bytes);
190 let mut m = Mantissas::new();195 let mut m = Mantissas::new();
191- let got: Vec<f32> = (0..3).map(|_| m.read(&mut r, 1).unwrap()).collect();196+ let got: Vec<i32> = (0..3).map(|_| m.read(&mut r, 1).unwrap()).collect();
192- assert!((got[0] - 2.0 / 3.0).abs() < 1e-6);197+ assert_eq!(got, [5_592_405, 0, -5_592_405]);
193- assert!(got[1].abs() < 1e-6);198+ assert_eq!(m.read(&mut r, 4).unwrap(), 7_626_007);
194- assert!((got[2] + 2.0 / 3.0).abs() < 1e-6);199+ assert_eq!(m.read(&mut r, 4).unwrap(), -7_626_007);
195- let a = m.read(&mut r, 4).unwrap();
196- let b = m.read(&mut r, 4).unwrap();
197- assert!((a - 10.0 / 11.0).abs() < 1e-6);
198- assert!((b + 10.0 / 11.0).abs() < 1e-6);
199 }200 }
200 201
201 #[test]202 #[test]
@@ -206,9 +207,9 @@ mod tests {
206 let bytes = w.into_bytes();207 let bytes = w.into_bytes();
207 let mut r = BitReader::new(&bytes);208 let mut r = BitReader::new(&bytes);
208 let mut m = Mantissas::new();209 let mut m = Mantissas::new();
209- assert!((m.read(&mut r, 7).unwrap() + 1.0).abs() < 1e-6);210+ assert_eq!(m.read(&mut r, 7).unwrap(), -(1 << 23));
210- assert!((m.read(&mut r, 7).unwrap() - 31.0 / 32.0).abs() < 1e-6);211+ assert_eq!(m.read(&mut r, 7).unwrap(), 31 << 18);
211- assert_eq!(m.read(&mut r, 0).unwrap(), 0.0);212+ assert_eq!(m.read(&mut r, 0).unwrap(), 0);
212 }213 }
213 214
214 #[test]215 #[test]
crates/ec-ac3/src/tables.rsmodified+11 -9
@@ -154,9 +154,11 @@ pub const QNTZTAB: [u32; 16] = [0, 5, 7, 3, 7, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14,
154 pub const QUANT_LEVELS: [u32; 6] = [0, 3, 5, 7, 11, 15];154 pub const QUANT_LEVELS: [u32; 6] = [0, 3, 5, 7, 11, 15];
155 155
156 /// The `k`-th reconstruction level of a `levels`-level symmetric quantizer156 /// The `k`-th reconstruction level of a `levels`-level symmetric quantizer
157-/// (Tables 7.19-7.23): the uniform midtread set `(2k - (levels-1)) / levels`.157+/// (Tables 7.19-7.23), `(2k - (levels-1)) / levels` at 2^23 full scale,
158-pub fn symmetric_level(levels: u32, k: u32) -> f32 {158+/// truncated toward zero as ffmpeg's `SYMMETRIC_DEQUANT` is.
159- (2.0 * k as f32 - (levels as f32 - 1.0)) / levels as f32159+pub fn symmetric_level(levels: u32, k: u32) -> i32 {
160+ let levels = levels as i32;
161+ (2 * k as i32 - (levels - 1)) * (1 << 23) / levels
160 }162 }
161 163
162 /// Transform window `w[]`: 256 values used back to back to form the 512-point164 /// Transform window `w[]`: 256 values used back to back to form the 512-point
@@ -216,12 +218,12 @@ mod tests {
216 #[test]218 #[test]
217 fn symmetric_quantizer_matches_printed_tables() {219 fn symmetric_quantizer_matches_printed_tables() {
218 // Tables 7.19-7.23, as printed.220 // Tables 7.19-7.23, as printed.
219- assert!((symmetric_level(3, 0) + 2.0 / 3.0).abs() < 1e-6);221+ assert_eq!(symmetric_level(3, 0), -5_592_405);
220- assert!(symmetric_level(3, 1).abs() < 1e-6);222+ assert_eq!(symmetric_level(3, 1), 0);
221- assert!((symmetric_level(5, 3) - 2.0 / 5.0).abs() < 1e-6);223+ assert_eq!(symmetric_level(5, 3), 3_355_443);
222- assert!((symmetric_level(7, 6) - 6.0 / 7.0).abs() < 1e-6);224+ assert_eq!(symmetric_level(7, 6), 7_190_235);
223- assert!((symmetric_level(11, 0) + 10.0 / 11.0).abs() < 1e-6);225+ assert_eq!(symmetric_level(11, 0), -7_626_007);
224- assert!((symmetric_level(15, 14) - 14.0 / 15.0).abs() < 1e-6);226+ assert_eq!(symmetric_level(15, 14), 7_829_367);
225 }227 }
226 228
227 #[test]229 #[test]