From 325b19265ea8b7e7f49bc4e4d0031abeff057fa2 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:49:09 +0300 Subject: [PATCH] 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) --- crates/ec-ac3/src/decode.rs | 36 +++++++++++++++++------------------- crates/ec-ac3/src/eac3.rs | 9 ++------- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/crates/ec-ac3/src/decode.rs b/crates/ec-ac3/src/decode.rs index 8c9ada8f..4d7bf94e 100644 --- a/crates/ec-ac3/src/decode.rs +++ b/crates/ec-ac3/src/decode.rs @@ -64,7 +64,7 @@ pub(crate) struct Core { pub(crate) ncplbnd: usize, /// Coupling band each sub-band belongs to, after `cplbndstrc` merging. pub(crate) cplband: [usize; 18], - pub(crate) cplco: [[f32; 18]; MAX_FBW], + pub(crate) cplco: [[i32; 18]; MAX_FBW], pub(crate) phsflg: [bool; 18], pub(crate) cplleak: (i32, i32), /// Annex E sends a channel's coupling coordinates in the first block of @@ -152,7 +152,7 @@ impl Core { ncplsubnd: 0, ncplbnd: 0, cplband: [0; 18], - cplco: [[0.0; 18]; MAX_FBW], + cplco: [[0; 18]; MAX_FBW], phsflg: [false; 18], cplleak: (0, 0), first_cplco: [true; MAX_FBW], @@ -330,13 +330,8 @@ impl Core { let mstrcplco = r.read_bits(2)? as i32; for bnd in 0..self.ncplbnd { let cplcoexp = r.read_bits(4)? as i32; - let cplcomant = r.read_bits(4)? as f32; - let mant = if cplcoexp == 15 { - cplcomant / 16.0 - } else { - (cplcomant + 16.0) / 32.0 - }; - self.cplco[ch][bnd] = mant * scale((cplcoexp + 3 * mstrcplco).min(24) as u8); + let cplcomant = r.read_bits(4)? as i32; + self.cplco[ch][bnd] = coupling_coord(cplcoexp, cplcomant, mstrcplco); } } if self.acmod == Acmod::Stereo && self.phsflginu && (cplcoe[0] || cplcoe[1]) { @@ -659,22 +654,20 @@ impl Core { } for sbnd in 0..self.ncplsubnd { let bnd = self.cplband[sbnd]; - let mut co = self.cplco[ch][bnd] * 8.0; - // §5.4.3.18: in 2/0 with phase flags, the right channel's - // contribution is inverted for the flagged bands. - if ch == 1 && self.phsflginu && self.phsflg[bnd] { - co = -co; - } + let co = i64::from(self.cplco[ch][bnd]); + let invert = ch == 1 && self.phsflginu && self.phsflg[bnd]; let from = start + sbnd * 12; for bin in from..(from + 12).min(end) { // §7.3.4: a coupled channel without dithflag gets silence where // the coupling channel's mantissa was zero-bit. - let cpl = if self.bap[CPL][bin] == 0 && !self.dithflag[ch] { - 0.0 + let out = if self.bap[CPL][bin] == 0 && !self.dithflag[ch] { + 0 } else { - self.coeffs[CPL][bin] + let cpl = (self.coeffs[CPL][bin] * 8_388_608.0) as i64; + let v = ((cpl * co) >> 23) as i32; + if invert { -v } else { v } }; - self.coeffs[ch][bin] = cpl * co; + self.coeffs[ch][bin] = out as f32 / 8_388_608.0; } } self.coeffs[ch][end..].fill(0.0); @@ -773,6 +766,11 @@ impl Core { } } +pub(crate) fn coupling_coord(exp: i32, mant: i32, master: i32) -> i32 { + let base = if exp == 15 { mant << 22 } else { (mant + 16) << 21 }; + base >> (exp + 3 * master).min(24) +} + /// §7.7.1.2: the 8-bit `dynrng` word as a linear gain. /// /// The three most significant bits are a signed power of two from -4 to 3 with diff --git a/crates/ec-ac3/src/eac3.rs b/crates/ec-ac3/src/eac3.rs index bfa75c66..254e3870 100644 --- a/crates/ec-ac3/src/eac3.rs +++ b/crates/ec-ac3/src/eac3.rs @@ -885,14 +885,9 @@ fn parse_block( let mstrcplco = r.read_bits(2)? as i32; for bnd in 0..core.ncplbnd { let cplcoexp = r.read_bits(4)? as i32; - let cplcomant = r.read_bits(4)? as f32; - let mant = if cplcoexp == 15 { - cplcomant / 16.0 - } else { - (cplcomant + 16.0) / 32.0 - }; + let cplcomant = r.read_bits(4)? as i32; core.cplco[ch][bnd] = - mant * crate::mantissa::scale((cplcoexp + 3 * mstrcplco).min(24) as u8); + crate::decode::coupling_coord(cplcoexp, cplcomant, mstrcplco); } } }