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 commit325b19265ea8b7e7f49bc4e4d0031abeff057fa2Parent: 2aae8d42 changed files +19 -26
- crates/ec-ac3/src/decode.rs +17 -19
- crates/ec-ac3/src/eac3.rs +2 -7
crates/ec-ac3/src/decode.rsmodified+17 -19
| @@ -64,7 +64,7 @@ pub(crate) struct Core { | |||
| 64 | pub(crate) ncplbnd: usize, | 64 | pub(crate) ncplbnd: usize, |
| 65 | /// Coupling band each sub-band belongs to, after `cplbndstrc` merging. | 65 | /// Coupling band each sub-band belongs to, after `cplbndstrc` merging. |
| 66 | pub(crate) cplband: [usize; 18], | 66 | pub(crate) cplband: [usize; 18], |
| 67 | - pub(crate) cplco: [[f32; 18]; MAX_FBW], | 67 | + pub(crate) cplco: [[i32; 18]; MAX_FBW], |
| 68 | pub(crate) phsflg: [bool; 18], | 68 | pub(crate) phsflg: [bool; 18], |
| 69 | pub(crate) cplleak: (i32, i32), | 69 | pub(crate) cplleak: (i32, i32), |
| 70 | /// Annex E sends a channel's coupling coordinates in the first block of | 70 | /// Annex E sends a channel's coupling coordinates in the first block of |
| @@ -152,7 +152,7 @@ impl Core { | |||
| 152 | ncplsubnd: 0, | 152 | ncplsubnd: 0, |
| 153 | ncplbnd: 0, | 153 | ncplbnd: 0, |
| 154 | cplband: [0; 18], | 154 | cplband: [0; 18], |
| 155 | - cplco: [[0.0; 18]; MAX_FBW], | 155 | + cplco: [[0; 18]; MAX_FBW], |
| 156 | phsflg: [false; 18], | 156 | phsflg: [false; 18], |
| 157 | cplleak: (0, 0), | 157 | cplleak: (0, 0), |
| 158 | first_cplco: [true; MAX_FBW], | 158 | first_cplco: [true; MAX_FBW], |
| @@ -330,13 +330,8 @@ impl Core { | |||
| 330 | let mstrcplco = r.read_bits(2)? as i32; | 330 | let mstrcplco = r.read_bits(2)? as i32; |
| 331 | for bnd in 0..self.ncplbnd { | 331 | for bnd in 0..self.ncplbnd { |
| 332 | let cplcoexp = r.read_bits(4)? as i32; | 332 | let cplcoexp = r.read_bits(4)? as i32; |
| 333 | - let cplcomant = r.read_bits(4)? as f32; | 333 | + let cplcomant = r.read_bits(4)? as i32; |
| 334 | - let mant = if cplcoexp == 15 { | 334 | + self.cplco[ch][bnd] = coupling_coord(cplcoexp, cplcomant, mstrcplco); |
| 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); | ||
| 340 | } | 335 | } |
| 341 | } | 336 | } |
| 342 | if self.acmod == Acmod::Stereo && self.phsflginu && (cplcoe[0] || cplcoe[1]) { | 337 | if self.acmod == Acmod::Stereo && self.phsflginu && (cplcoe[0] || cplcoe[1]) { |
| @@ -659,22 +654,20 @@ impl Core { | |||
| 659 | } | 654 | } |
| 660 | for sbnd in 0..self.ncplsubnd { | 655 | for sbnd in 0..self.ncplsubnd { |
| 661 | let bnd = self.cplband[sbnd]; | 656 | let bnd = self.cplband[sbnd]; |
| 662 | - let mut co = self.cplco[ch][bnd] * 8.0; | 657 | + let co = i64::from(self.cplco[ch][bnd]); |
| 663 | - // §5.4.3.18: in 2/0 with phase flags, the right channel's | 658 | + let invert = ch == 1 && self.phsflginu && self.phsflg[bnd]; |
| 664 | - // contribution is inverted for the flagged bands. | ||
| 665 | - if ch == 1 && self.phsflginu && self.phsflg[bnd] { | ||
| 666 | - co = -co; | ||
| 667 | - } | ||
| 668 | let from = start + sbnd * 12; | 659 | let from = start + sbnd * 12; |
| 669 | for bin in from..(from + 12).min(end) { | 660 | for bin in from..(from + 12).min(end) { |
| 670 | // §7.3.4: a coupled channel without dithflag gets silence where | 661 | // §7.3.4: a coupled channel without dithflag gets silence where |
| 671 | // the coupling channel's mantissa was zero-bit. | 662 | // the coupling channel's mantissa was zero-bit. |
| 672 | - let cpl = if self.bap[CPL][bin] == 0 && !self.dithflag[ch] { | 663 | + let out = if self.bap[CPL][bin] == 0 && !self.dithflag[ch] { |
| 673 | - 0.0 | 664 | + 0 |
| 674 | } else { | 665 | } 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 } | ||
| 676 | }; | 669 | }; |
| 677 | - self.coeffs[ch][bin] = cpl * co; | 670 | + self.coeffs[ch][bin] = out as f32 / 8_388_608.0; |
| 678 | } | 671 | } |
| 679 | } | 672 | } |
| 680 | self.coeffs[ch][end..].fill(0.0); | 673 | self.coeffs[ch][end..].fill(0.0); |
| @@ -773,6 +766,11 @@ impl Core { | |||
| 773 | } | 766 | } |
| 774 | } | 767 | } |
| 775 | 768 | ||
| 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 | + | ||
| 776 | /// §7.7.1.2: the 8-bit `dynrng` word as a linear gain. | 774 | /// §7.7.1.2: the 8-bit `dynrng` word as a linear gain. |
| 777 | /// | 775 | /// |
| 778 | /// The three most significant bits are a signed power of two from -4 to 3 with | 776 | /// 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( | |||
| 885 | let mstrcplco = r.read_bits(2)? as i32; | 885 | let mstrcplco = r.read_bits(2)? as i32; |
| 886 | for bnd in 0..core.ncplbnd { | 886 | for bnd in 0..core.ncplbnd { |
| 887 | let cplcoexp = r.read_bits(4)? as i32; | 887 | let cplcoexp = r.read_bits(4)? as i32; |
| 888 | - let cplcomant = r.read_bits(4)? as f32; | 888 | + let cplcomant = r.read_bits(4)? as i32; |
| 889 | - let mant = if cplcoexp == 15 { | ||
| 890 | - cplcomant / 16.0 | ||
| 891 | - } else { | ||
| 892 | - (cplcomant + 16.0) / 32.0 | ||
| 893 | - }; | ||
| 894 | core.cplco[ch][bnd] = | 889 | core.cplco[ch][bnd] = |
| 895 | - mant * crate::mantissa::scale((cplcoexp + 3 * mstrcplco).min(24) as u8); | 890 | + crate::decode::coupling_coord(cplcoexp, cplcomant, mstrcplco); |
| 896 | } | 891 | } |
| 897 | } | 892 | } |
| 898 | } | 893 | } |