forge
Explore
Log in

Don't you dare go Hollow

ec-ac3: coupling in ffmpeg's integer path (coord = (m+16)<<21>>shift, out = cpl*coord>>23 floor, phase negated after); float coupling left ~1 LSB of 2^-22 error per coupled bin, the 1e-6 floor on quiet channels (unrun: orchestrator verifies monsters ch0-2,4)

Browse files at this commit

325b19265ea8b7e7f49bc4e4d0031abeff057fa2Ahmet Kaan GümüşParent: 2aae8d4
crates/ec-ac3/src/decode.rsmodified+17 -19
@@ -64,7 +64,7 @@ pub(crate) struct Core {
6464 pub(crate) ncplbnd: usize,
6565 /// Coupling band each sub-band belongs to, after `cplbndstrc` merging.
6666 pub(crate) cplband: [usize; 18],
67- pub(crate) cplco: [[f32; 18]; MAX_FBW],
67+ pub(crate) cplco: [[i32; 18]; MAX_FBW],
6868 pub(crate) phsflg: [bool; 18],
6969 pub(crate) cplleak: (i32, i32),
7070 /// Annex E sends a channel's coupling coordinates in the first block of
@@ -152,7 +152,7 @@ impl Core {
152152 ncplsubnd: 0,
153153 ncplbnd: 0,
154154 cplband: [0; 18],
155- cplco: [[0.0; 18]; MAX_FBW],
155+ cplco: [[0; 18]; MAX_FBW],
156156 phsflg: [false; 18],
157157 cplleak: (0, 0),
158158 first_cplco: [true; MAX_FBW],
@@ -330,13 +330,8 @@ impl Core {
330330 let mstrcplco = r.read_bits(2)? as i32;
331331 for bnd in 0..self.ncplbnd {
332332 let cplcoexp = r.read_bits(4)? as i32;
333- let cplcomant = r.read_bits(4)? as f32;
334- let mant = if cplcoexp == 15 {
335- cplcomant / 16.0
336- } else {
337- (cplcomant + 16.0) / 32.0
338- };
339- self.cplco[ch][bnd] = mant * scale((cplcoexp + 3 * mstrcplco).min(24) as u8);
333+ let cplcomant = r.read_bits(4)? as i32;
334+ self.cplco[ch][bnd] = coupling_coord(cplcoexp, cplcomant, mstrcplco);
340335 }
341336 }
342337 if self.acmod == Acmod::Stereo && self.phsflginu && (cplcoe[0] || cplcoe[1]) {
@@ -659,22 +654,20 @@ impl Core {
659654 }
660655 for sbnd in 0..self.ncplsubnd {
661656 let bnd = self.cplband[sbnd];
662- let mut co = self.cplco[ch][bnd] * 8.0;
663- // §5.4.3.18: in 2/0 with phase flags, the right channel's
664- // contribution is inverted for the flagged bands.
665- if ch == 1 && self.phsflginu && self.phsflg[bnd] {
666- co = -co;
667- }
657+ let co = i64::from(self.cplco[ch][bnd]);
658+ let invert = ch == 1 && self.phsflginu && self.phsflg[bnd];
668659 let from = start + sbnd * 12;
669660 for bin in from..(from + 12).min(end) {
670661 // §7.3.4: a coupled channel without dithflag gets silence where
671662 // the coupling channel's mantissa was zero-bit.
672- let cpl = if self.bap[CPL][bin] == 0 && !self.dithflag[ch] {
673- 0.0
663+ let out = if self.bap[CPL][bin] == 0 && !self.dithflag[ch] {
664+ 0
674665 } else {
675- self.coeffs[CPL][bin]
666+ let cpl = (self.coeffs[CPL][bin] * 8_388_608.0) as i64;
667+ let v = ((cpl * co) >> 23) as i32;
668+ if invert { -v } else { v }
676669 };
677- self.coeffs[ch][bin] = cpl * co;
670+ self.coeffs[ch][bin] = out as f32 / 8_388_608.0;
678671 }
679672 }
680673 self.coeffs[ch][end..].fill(0.0);
@@ -773,6 +766,11 @@ impl Core {
773766 }
774767 }
775768
769+pub(crate) fn coupling_coord(exp: i32, mant: i32, master: i32) -> i32 {
770+ let base = if exp == 15 { mant << 22 } else { (mant + 16) << 21 };
771+ base >> (exp + 3 * master).min(24)
772+}
773+
776774 /// §7.7.1.2: the 8-bit `dynrng` word as a linear gain.
777775 ///
778776 /// The three most significant bits are a signed power of two from -4 to 3 with
crates/ec-ac3/src/eac3.rsmodified+2 -7
@@ -885,14 +885,9 @@ fn parse_block(
885885 let mstrcplco = r.read_bits(2)? as i32;
886886 for bnd in 0..core.ncplbnd {
887887 let cplcoexp = r.read_bits(4)? as i32;
888- let cplcomant = r.read_bits(4)? as f32;
889- let mant = if cplcoexp == 15 {
890- cplcomant / 16.0
891- } else {
892- (cplcomant + 16.0) / 32.0
893- };
888+ let cplcomant = r.read_bits(4)? as i32;
894889 core.cplco[ch][bnd] =
895- mant * crate::mantissa::scale((cplcoexp + 3 * mstrcplco).min(24) as u8);
890+ crate::decode::coupling_coord(cplcoexp, cplcomant, mstrcplco);
896891 }
897892 }
898893 }