forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'simplify/c-ac3'

Browse files at this commit

5fa31c7eae5f8aca4c0a6f52154417e08b166e9eAhmet Kaan GümüşParent: 35cac3aacbfd4c
crates/ec-ac3/src/aht.rsmodified+3 -10
@@ -107,10 +107,10 @@ pub(crate) fn read_channel(
107 let mut next_gain = 0usize;107 let mut next_gain = 0usize;
108 for bin in start..end {108 for bin in start..end {
109 let h = hebap[bin];109 let h = hebap[bin];
110- let mut zero_bits = false;
111 if h == 0 {110 if h == 0 {
112- pre = [0.0; AHT_BLOCKS];111+ for slot in &mut pre {
113- zero_bits = true;112+ *slot = if dither_on { dither.value() } else { 0.0 };
113+ }
114 } else if h <= 7 {114 } else if h <= 7 {
115 let index = r.read_bits(VQ_BITS[h as usize])? as usize;115 let index = r.read_bits(VQ_BITS[h as usize])? as usize;
116 let table = vq_table(h);116 let table = vq_table(h);
@@ -131,13 +131,6 @@ pub(crate) fn read_channel(
131 }131 }
132 }132 }
133 133
134- if zero_bits {
135- // A zero-bit AHT bin is the same silence AC-3 fills with dither.
136- for blk in 0..AHT_BLOCKS {
137- out[blk * COEFFS + bin] = if dither_on { dither.value() } else { 0.0 };
138- }
139- continue;
140- }
141 // §3.4.5: invert the six-point DCT across blocks.134 // §3.4.5: invert the six-point DCT across blocks.
142 for (blk, slot) in idct6(&pre).into_iter().enumerate() {135 for (blk, slot) in idct6(&pre).into_iter().enumerate() {
143 out[blk * COEFFS + bin] = slot;136 out[blk * COEFFS + bin] = slot;
crates/ec-ac3/src/decode.rsmodified+27 -34
@@ -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).
@@ -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 of70 /// Annex E sends a channel's coupling coordinates in the first block of
@@ -118,9 +118,6 @@ pub(crate) struct Core {
118 pub(crate) first_spxco: [bool; MAX_FBW],118 pub(crate) first_spxco: [bool; MAX_FBW],
119 pub(crate) chinspxatten: [bool; MAX_FBW],119 pub(crate) chinspxatten: [bool; MAX_FBW],
120 pub(crate) spxattencod: [u8; MAX_FBW],120 pub(crate) spxattencod: [u8; MAX_FBW],
121- /// The noise source of §3.6.4.2.4, kept apart from the dither source so
122- /// neither one's sequence depends on the other's use.
123- pub(crate) spx_noise: Dither,
124 // Adaptive hybrid transform (Annex E §3.4). All false under AC-3.121 // Adaptive hybrid transform (Annex E §3.4). All false under AC-3.
125 /// Which channels are AHT-coded this frame.122 /// Which channels are AHT-coded this frame.
126 pub(crate) aht: [bool; CHANNELS],123 pub(crate) aht: [bool; CHANNELS],
@@ -152,7 +149,7 @@ impl Core {
152 ncplsubnd: 0,149 ncplsubnd: 0,
153 ncplbnd: 0,150 ncplbnd: 0,
154 cplband: [0; 18],151 cplband: [0; 18],
155- cplco: [[0.0; 18]; MAX_FBW],152+ cplco: [[0; 18]; MAX_FBW],
156 phsflg: [false; 18],153 phsflg: [false; 18],
157 cplleak: (0, 0),154 cplleak: (0, 0),
158 first_cplco: [true; MAX_FBW],155 first_cplco: [true; MAX_FBW],
@@ -190,7 +187,6 @@ impl Core {
190 first_spxco: [true; MAX_FBW],187 first_spxco: [true; MAX_FBW],
191 chinspxatten: [false; MAX_FBW],188 chinspxatten: [false; MAX_FBW],
192 spxattencod: [0; MAX_FBW],189 spxattencod: [0; MAX_FBW],
193- spx_noise: Dither::default(),
194 aht: [false; CHANNELS],190 aht: [false; CHANNELS],
195 aht_read: [false; CHANNELS],191 aht_read: [false; CHANNELS],
196 aht_mant: vec![0.0; CHANNELS * crate::aht::AHT_BLOCKS * COEFFS],192 aht_mant: vec![0.0; CHANNELS * crate::aht::AHT_BLOCKS * COEFFS],
@@ -330,13 +326,8 @@ impl Core {
330 let mstrcplco = r.read_bits(2)? as i32;326 let mstrcplco = r.read_bits(2)? as i32;
331 for bnd in 0..self.ncplbnd {327 for bnd in 0..self.ncplbnd {
332 let cplcoexp = r.read_bits(4)? as i32;328 let cplcoexp = r.read_bits(4)? as i32;
333- let cplcomant = r.read_bits(4)? as f32;329+ let cplcomant = r.read_bits(4)? as i32;
334- let mant = if cplcoexp == 15 {330+ 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 }331 }
341 }332 }
342 if self.acmod == Acmod::Stereo && self.phsflginu && (cplcoe[0] || cplcoe[1]) {333 if self.acmod == Acmod::Stereo && self.phsflginu && (cplcoe[0] || cplcoe[1]) {
@@ -573,11 +564,12 @@ impl Core {
573 for bin in 0..end {564 for bin in 0..end {
574 let bap = self.bap[ch][bin];565 let bap = self.bap[ch][bin];
575 let value = m.read(r, bap)?;566 let value = m.read(r, bap)?;
576- self.coeffs[ch][bin] = if bap == 0 && self.dithflag[ch] && self.dither_on {567+ let mant = if bap == 0 && self.dithflag[ch] && self.dither_on {
577- self.dither.value() * scale(self.exps[ch][bin])568+ self.dither.mantissa()
578 } else {569 } else {
579- value * scale(self.exps[ch][bin])570+ value
580 };571 };
572+ self.coeffs[ch][bin] = shift(mant, self.exps[ch][bin]);
581 }573 }
582 }574 }
583 self.coeffs[ch][end..].fill(0.0);575 self.coeffs[ch][end..].fill(0.0);
@@ -589,14 +581,12 @@ impl Core {
589 for bin in start..end {581 for bin in start..end {
590 let bap = self.bap[CPL][bin];582 let bap = self.bap[CPL][bin];
591 let value = m.read(r, bap)?;583 let value = m.read(r, bap)?;
592- // The coupling channel always draws dither for its zero-bit584+ let mant = if bap == 0 && self.dither_on {
593- // mantissas, whatever the coupled channels' dithflag says:585+ 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 {586 } else {
598- value * scale(self.exps[CPL][bin])587+ value
599 };588 };
589+ self.coeffs[CPL][bin] = shift(mant, self.exps[CPL][bin]);
600 }590 }
601 }591 }
602 got_cplchan = true;592 got_cplchan = true;
@@ -608,7 +598,7 @@ impl Core {
608 } else {598 } else {
609 for bin in 0..7 {599 for bin in 0..7 {
610 let value = m.read(r, self.bap[LFE][bin])?;600 let value = m.read(r, self.bap[LFE][bin])?;
611- self.coeffs[LFE][bin] = value * scale(self.exps[LFE][bin]);601+ self.coeffs[LFE][bin] = shift(value, self.exps[LFE][bin]);
612 }602 }
613 }603 }
614 self.coeffs[LFE][7..].fill(0.0);604 self.coeffs[LFE][7..].fill(0.0);
@@ -660,22 +650,20 @@ impl Core {
660 }650 }
661 for sbnd in 0..self.ncplsubnd {651 for sbnd in 0..self.ncplsubnd {
662 let bnd = self.cplband[sbnd];652 let bnd = self.cplband[sbnd];
663- let mut co = self.cplco[ch][bnd] * 8.0;653+ let co = i64::from(self.cplco[ch][bnd]);
664- // §5.4.3.18: in 2/0 with phase flags, the right channel's654+ let invert = ch == 1 && self.phsflginu && self.phsflg[bnd];
665- // contribution is inverted for the flagged bands.
666- if ch == 1 && self.phsflginu && self.phsflg[bnd] {
667- co = -co;
668- }
669 let from = start + sbnd * 12;655 let from = start + sbnd * 12;
670 for bin in from..(from + 12).min(end) {656 for bin in from..(from + 12).min(end) {
671 // §7.3.4: a coupled channel without dithflag gets silence where657 // §7.3.4: a coupled channel without dithflag gets silence where
672 // the coupling channel's mantissa was zero-bit.658 // the coupling channel's mantissa was zero-bit.
673- let cpl = if self.bap[CPL][bin] == 0 && !self.dithflag[ch] {659+ let out = if self.bap[CPL][bin] == 0 && !self.dithflag[ch] {
674- 0.0660+ 0
675 } else {661 } else {
676- self.coeffs[CPL][bin]662+ let cpl = (self.coeffs[CPL][bin] * 8_388_608.0) as i64;
663+ let v = ((cpl * co) >> 23) as i32;
664+ if invert { -v } else { v }
677 };665 };
678- self.coeffs[ch][bin] = cpl * co;666+ self.coeffs[ch][bin] = out as f32 / 8_388_608.0;
679 }667 }
680 }668 }
681 self.coeffs[ch][end..].fill(0.0);669 self.coeffs[ch][end..].fill(0.0);
@@ -774,6 +762,11 @@ impl Core {
774 }762 }
775 }763 }
776 764
765+pub(crate) fn coupling_coord(exp: i32, mant: i32, master: i32) -> i32 {
766+ let base = if exp == 15 { mant << 22 } else { (mant + 16) << 21 };
767+ base >> (exp + 3 * master).min(24)
768+}
769+
777 /// §7.7.1.2: the 8-bit `dynrng` word as a linear gain.770 /// §7.7.1.2: the 8-bit `dynrng` word as a linear gain.
778 ///771 ///
779 /// The three most significant bits are a signed power of two from -4 to 3 with772 /// The three most significant bits are a signed power of two from -4 to 3 with
crates/ec-ac3/src/decoder.rsmodified+1 -1
@@ -342,7 +342,7 @@ impl Ac3Decoder {
342 )));342 )));
343 }343 }
344 let channels = hdr.nfchans + usize::from(hdr.bsi.lfeon);344 let channels = hdr.nfchans + usize::from(hdr.bsi.lfeon);
345- let positions = dependent::positions(hdr.chanmap, channels)?;345+ let positions = dependent::positions(hdr.chanmap, channels, hdr.bsi.acmod, hdr.bsi.lfeon)?;
346 if self.dependents.len() <= slot {346 if self.dependents.len() <= slot {
347 self.dependents.push(Core::new());347 self.dependents.push(Core::new());
348 }348 }
crates/ec-ac3/src/dependent.rsmodified+36 -11
@@ -28,6 +28,7 @@
28 28
29 use ec_core::{BitReader, ChannelPosition, Error, Result};29 use ec_core::{BitReader, ChannelPosition, Error, Result};
30 30
31+use crate::bsi::Acmod;
31 use crate::eac3::{self, Eac3Bsi, StreamType};32 use crate::eac3::{self, Eac3Bsi, StreamType};
32 use crate::syncinfo::SYNCWORD;33 use crate::syncinfo::SYNCWORD;
33 34
@@ -54,7 +55,7 @@ pub(crate) fn peek(data: &[u8]) -> Option<Eac3Bsi> {
54 55
55 /// The loudspeaker positions a dependent substream's `channels` coded channels56 /// The loudspeaker positions a dependent substream's `channels` coded channels
56 /// fill, in coded order, from its custom channel map (Table E2.5).57 /// fill, in coded order, from its custom channel map (Table E2.5).
57-pub(crate) fn positions(map: Option<u16>, channels: usize) -> Result<Vec<ChannelPosition>> {58+pub(crate) fn positions(map: Option<u16>, channels: usize, acmod: Acmod, lfeon: bool) -> Result<Vec<ChannelPosition>> {
58 use ChannelPosition::*;59 use ChannelPosition::*;
59 let Some(map) = map else {60 let Some(map) = map else {
60 return Err(Error::unsupported(61 return Err(Error::unsupported(
@@ -99,7 +100,31 @@ pub(crate) fn positions(map: Option<u16>, channels: usize) -> Result<Vec<Channel
99 out.len()100 out.len()
100 )));101 )));
101 }102 }
102- Ok(out)103+ let slots = slot_order(acmod, lfeon);
104+ let mut placed = out.clone();
105+ for (k, position) in out.into_iter().enumerate() {
106+ placed[usize::from(slots[k])] = position;
107+ }
108+ Ok(placed)
109+}
110+
111+fn slot_order(acmod: Acmod, lfeon: bool) -> &'static [u8] {
112+ match (acmod, lfeon) {
113+ (Acmod::DualMono | Acmod::Stereo, false) => &[0, 1],
114+ (Acmod::DualMono | Acmod::Stereo, true) => &[0, 1, 2],
115+ (Acmod::Mono, false) => &[0],
116+ (Acmod::Mono, true) => &[0, 1],
117+ (Acmod::Surround3_0, false) => &[0, 2, 1],
118+ (Acmod::Surround3_0, true) => &[0, 2, 1, 3],
119+ (Acmod::Surround2_1, false) => &[0, 1, 2],
120+ (Acmod::Surround2_1, true) => &[0, 1, 3, 2],
121+ (Acmod::Surround3_1, false) => &[0, 2, 1, 3],
122+ (Acmod::Surround3_1, true) => &[0, 2, 1, 4, 3],
123+ (Acmod::Surround2_2, false) => &[0, 1, 2, 3],
124+ (Acmod::Surround2_2, true) => &[0, 1, 4, 2, 3],
125+ (Acmod::Surround3_2, false) => &[0, 2, 1, 3, 4],
126+ (Acmod::Surround3_2, true) => &[0, 2, 1, 5, 3, 4],
127+ }
103 }128 }
104 129
105 /// The Table E2.5 name of the map entry `i` that this build cannot place.130 /// The Table E2.5 name of the map entry `i` that this build cannot place.
@@ -188,26 +213,26 @@ mod tests {
188 213
189 #[test]214 #[test]
190 fn map_bits_name_positions_as_ffmpeg_does() {215 fn map_bits_name_positions_as_ffmpeg_does() {
191- assert_eq!(positions(Some(0x8000), 1).unwrap(), vec![FrontLeft]);216+ assert_eq!(positions(Some(0x8000), 1, Acmod::Mono, false).unwrap(), vec![FrontLeft]);
192- assert_eq!(positions(Some(0x0400), 2).unwrap(), vec![FrontLeftOfCenter, FrontRightOfCenter]);217+ assert_eq!(positions(Some(0x0400), 2, Acmod::Stereo, false).unwrap(), vec![FrontLeftOfCenter, FrontRightOfCenter]);
193- assert_eq!(positions(Some(0x0200), 2).unwrap(), vec![BackLeft, BackRight]);218+ assert_eq!(positions(Some(0x0200), 2, Acmod::Stereo, false).unwrap(), vec![BackLeft, BackRight]);
194- assert_eq!(positions(Some(0x1800), 2).unwrap(), vec![SideLeft, SideRight]);219+ assert_eq!(positions(Some(0x1800), 2, Acmod::Stereo, false).unwrap(), vec![SideLeft, SideRight]);
195- assert_eq!(positions(Some(0x0001), 1).unwrap(), vec![Lfe]);220+ assert_eq!(positions(Some(0x0001), 1, Acmod::Mono, false).unwrap(), vec![Lfe]);
196- assert_eq!(positions(Some(0x8400), 3).unwrap(), vec![FrontLeft, FrontLeftOfCenter, FrontRightOfCenter]);221+ assert_eq!(positions(Some(0x8400), 3, Acmod::Surround2_1, false).unwrap(), vec![FrontLeft, FrontLeftOfCenter, FrontRightOfCenter]);
197 }222 }
198 223
199 #[test]224 #[test]
200 fn unnamed_and_missing_maps_are_refused_by_name() {225 fn unnamed_and_missing_maps_are_refused_by_name() {
201 for map in [None, Some(0x0080), Some(0x0040), Some(0x0010), Some(0x0004), Some(0x0002)] {226 for map in [None, Some(0x0080), Some(0x0040), Some(0x0010), Some(0x0004), Some(0x0002)] {
202- let err = positions(map, 2).expect_err("refused");227+ let err = positions(map, 2, Acmod::Stereo, false).expect_err("refused");
203 assert!(matches!(err, Error::Unsupported { .. }), "{map:?}: {err}");228 assert!(matches!(err, Error::Unsupported { .. }), "{map:?}: {err}");
204 }229 }
205 }230 }
206 231
207 #[test]232 #[test]
208 fn map_that_disagrees_with_the_channel_count_is_corrupt() {233 fn map_that_disagrees_with_the_channel_count_is_corrupt() {
209- assert!(matches!(positions(Some(0x1800), 1), Err(Error::Corrupt { .. })));234+ assert!(matches!(positions(Some(0x1800), 1, Acmod::Mono, false), Err(Error::Corrupt { .. })));
210- assert!(matches!(positions(Some(0x0400), 3), Err(Error::Corrupt { .. })));235+ assert!(matches!(positions(Some(0x0400), 3, Acmod::Surround2_1, false), Err(Error::Corrupt { .. })));
211 }236 }
212 237
213 #[test]238 #[test]
crates/ec-ac3/src/eac3.rsmodified+4 -12
@@ -781,7 +781,7 @@ fn parse_block(
781 // §3.6.4.2.1: the blend follows the band's frequency midpoint.781 // §3.6.4.2.1: the blend follows the band's frequency midpoint.
782 let bandsize = core.spxbndsz[bnd] as f32;782 let bandsize = core.spxbndsz[bnd] as f32;
783 let nratio = ((spxmant + 0.5 * bandsize) / end - spxblnd).clamp(0.0, 1.0);783 let nratio = ((spxmant + 0.5 * bandsize) / end - spxblnd).clamp(0.0, 1.0);
784- core.nblend[ch][bnd] = nratio.sqrt();784+ core.nblend[ch][bnd] = (3.0 * nratio).sqrt();
785 core.sblend[ch][bnd] = (1.0 - nratio).sqrt();785 core.sblend[ch][bnd] = (1.0 - nratio).sqrt();
786 spxmant += bandsize;786 spxmant += bandsize;
787 }787 }
@@ -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 }
@@ -1196,11 +1191,8 @@ fn spectral_extension(core: &mut Core, ch: usize) {
1196 if spxmant >= COEFFS {1191 if spxmant >= COEFFS {
1197 break;1192 break;
1198 }1193 }
1199- // §E3.6.4.2.4's noise(): a zero-mean unit-variance source the
1200- // standard leaves to the implementation, so it is switched by the
1201- // same option as §7.3.4's dither.
1202 let noise = if core.dither_on {1194 let noise = if core.dither_on {
1203- core.spx_noise.unit_variance()1195+ (core.dither.next_word() as i32) as f32 / -2_147_483_648.0
1204 } else {1196 } else {
1205 0.01197 0.0
1206 };1198 };
crates/ec-ac3/src/mantissa.rsmodified+22 -30
@@ -50,27 +50,20 @@ impl Default for Dither {
50 }50 }
51 51
52 impl Dither {52 impl Dither {
53- /// Next dither value, uniform in ±0.707 (ffmpeg's mantissa scaled by 2^-23).53+ pub(crate) fn value(&mut self) -> f32 {
54- pub fn value(&mut self) -> f32 {54+ let x = self.next_word() & 0x7f_ffff;
55- self.mantissa() as f32 / 8_388_608.055+ (x as i32 - 0x40_0000) as f32 / 8_388_608.0
56- }
57-
58- /// Next value from a zero-mean, unit-variance source — what Annex E
59- /// §3.6.4.2.4 calls `noise()`. A uniform distribution on `[-1, 1]` has
60- /// variance 1/3, so it is scaled by `sqrt(3)`.
61- pub fn unit_variance(&mut self) -> f32 {
62- self.mantissa() as f32 / 5_931_008.0 * 1.732_050_8
63 }56 }
64 57
65 /// ffmpeg's integer dither mantissa: `((x >> 8) * 181 >> 8) - 5931008`.58 /// ffmpeg's integer dither mantissa: `((x >> 8) * 181 >> 8) - 5931008`.
66- fn mantissa(&mut self) -> i32 {59+ pub fn mantissa(&mut self) -> i32 {
67 let x = self.next_word();60 let x = self.next_word();
68 let u = ((x >> 8) * 181) >> 8;61 let u = ((x >> 8) * 181) >> 8;
69 u as i32 - 5_931_00862 u as i32 - 5_931_008
70 }63 }
71 64
72 /// `av_lfg_get`: `state[i] = state[i-24] + state[i-55]`, returning the new word.65 /// `av_lfg_get`: `state[i] = state[i-24] + state[i-55]`, returning the new word.
73- fn next_word(&mut self) -> u32 {66+ pub fn next_word(&mut self) -> u32 {
74 let i = self.index & 63;67 let i = self.index & 63;
75 let v = self.state[(i + 40) & 63].wrapping_add(self.state[(i + 9) & 63]);68 let v = self.state[(i + 40) & 63].wrapping_add(self.state[(i + 9) & 63]);
76 self.state[i] = v;69 self.state[i] = v;
@@ -87,11 +80,11 @@ impl Dither {
87 /// the next channel begins.80 /// the next channel begins.
88 #[derive(Debug, Clone, Copy, Default)]81 #[derive(Debug, Clone, Copy, Default)]
89 pub struct Mantissas {82 pub struct Mantissas {
90- group3: [f32; 3],83+ group3: [i32; 3],
91 left3: usize,84 left3: usize,
92- group5: [f32; 3],85+ group5: [i32; 3],
93 left5: usize,86 left5: usize,
94- group11: [f32; 2],87+ group11: [i32; 2],
95 left11: usize,88 left11: usize,
96 }89 }
97 90
@@ -106,9 +99,9 @@ impl Mantissas {
106 /// `bap == 0` returns 0.0; the caller decides whether that bin gets dither,99 /// `bap == 0` returns 0.0; the caller decides whether that bin gets dither,
107 /// because in the coupled range the decision belongs to the channel rather100 /// because in the coupled range the decision belongs to the channel rather
108 /// than to the coupling channel the mantissa came from.101 /// than to the coupling channel the mantissa came from.
109- pub fn read(&mut self, r: &mut BitReader<'_>, bap: u8) -> Result<f32> {102+ pub fn read(&mut self, r: &mut BitReader<'_>, bap: u8) -> Result<i32> {
110 Ok(match bap {103 Ok(match bap {
111- 0 => 0.0,104+ 0 => 0,
112 1 => {105 1 => {
113 if self.left3 == 0 {106 if self.left3 == 0 {
114 let code = r.read_bits(5)?.min(26);107 let code = r.read_bits(5)?.min(26);
@@ -159,13 +152,16 @@ impl Mantissas {
159 // 6..=15: asymmetric two's complement, decimal point left of152 // 6..=15: asymmetric two's complement, decimal point left of
160 // the MSB (§7.3.2).153 // the MSB (§7.3.2).
161 let bits = QNTZTAB[(bap as usize).min(15)];154 let bits = QNTZTAB[(bap as usize).min(15)];
162- let value = r.read_signed(bits)?;155+ r.read_signed(bits)? << (24 - bits)
163- value as f32 / (1u32 << (bits - 1)) as f32
164 }156 }
165 })157 })
166 }158 }
167 }159 }
168 160
161+pub(crate) fn shift(mantissa: i32, exponent: u8) -> f32 {
162+ (mantissa >> exponent) as f32 / 8_388_608.0
163+}
164+
169 /// `2^-exponent`, the right shift §7.3 applies to every mantissa.165 /// `2^-exponent`, the right shift §7.3 applies to every mantissa.
170 pub fn scale(exponent: u8) -> f32 {166 pub fn scale(exponent: u8) -> f32 {
171 // Exponents are 0..=24, so a table lookup would buy nothing over the167 // Exponents are 0..=24, so a table lookup would buy nothing over the
@@ -188,14 +184,10 @@ mod tests {
188 let bytes = w.into_bytes();184 let bytes = w.into_bytes();
189 let mut r = BitReader::new(&bytes);185 let mut r = BitReader::new(&bytes);
190 let mut m = Mantissas::new();186 let mut m = Mantissas::new();
191- let got: Vec<f32> = (0..3).map(|_| m.read(&mut r, 1).unwrap()).collect();187+ 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);188+ assert_eq!(got, [5_592_405, 0, -5_592_405]);
193- assert!(got[1].abs() < 1e-6);189+ assert_eq!(m.read(&mut r, 4).unwrap(), 7_626_007);
194- assert!((got[2] + 2.0 / 3.0).abs() < 1e-6);190+ 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 }191 }
200 192
201 #[test]193 #[test]
@@ -206,9 +198,9 @@ mod tests {
206 let bytes = w.into_bytes();198 let bytes = w.into_bytes();
207 let mut r = BitReader::new(&bytes);199 let mut r = BitReader::new(&bytes);
208 let mut m = Mantissas::new();200 let mut m = Mantissas::new();
209- assert!((m.read(&mut r, 7).unwrap() + 1.0).abs() < 1e-6);201+ 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);202+ assert_eq!(m.read(&mut r, 7).unwrap(), 31 << 18);
211- assert_eq!(m.read(&mut r, 0).unwrap(), 0.0);203+ assert_eq!(m.read(&mut r, 0).unwrap(), 0);
212 }204 }
213 205
214 #[test]206 #[test]
crates/ec-ac3/src/tables.rsmodified+9 -8
@@ -155,8 +155,9 @@ 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): the uniform midtread set `(2k - (levels-1)) / levels`.
158-pub fn symmetric_level(levels: u32, k: u32) -> f32 {158+pub fn symmetric_level(levels: u32, k: u32) -> i32 {
159- (2.0 * k as f32 - (levels as f32 - 1.0)) / levels as f32159+ let levels = levels as i32;
160+ (2 * k as i32 - (levels - 1)) * (1 << 23) / levels
160 }161 }
161 162
162 /// Transform window `w[]`: 256 values used back to back to form the 512-point163 /// Transform window `w[]`: 256 values used back to back to form the 512-point
@@ -216,12 +217,12 @@ mod tests {
216 #[test]217 #[test]
217 fn symmetric_quantizer_matches_printed_tables() {218 fn symmetric_quantizer_matches_printed_tables() {
218 // Tables 7.19-7.23, as printed.219 // Tables 7.19-7.23, as printed.
219- assert!((symmetric_level(3, 0) + 2.0 / 3.0).abs() < 1e-6);220+ assert_eq!(symmetric_level(3, 0), -5_592_405);
220- assert!(symmetric_level(3, 1).abs() < 1e-6);221+ assert_eq!(symmetric_level(3, 1), 0);
221- assert!((symmetric_level(5, 3) - 2.0 / 5.0).abs() < 1e-6);222+ assert_eq!(symmetric_level(5, 3), 3_355_443);
222- assert!((symmetric_level(7, 6) - 6.0 / 7.0).abs() < 1e-6);223+ assert_eq!(symmetric_level(7, 6), 7_190_235);
223- assert!((symmetric_level(11, 0) + 10.0 / 11.0).abs() < 1e-6);224+ assert_eq!(symmetric_level(11, 0), -7_626_007);
224- assert!((symmetric_level(15, 14) - 14.0 / 15.0).abs() < 1e-6);225+ assert_eq!(symmetric_level(15, 14), 7_829_367);
225 }226 }
226 227
227 #[test]228 #[test]
crates/ec-ac3/tests/it/real_library.rsmodified+11 -0
@@ -43,6 +43,17 @@ fn decode(path: &Path) -> (Vec<f32>, usize) {
43 if pos + size > data.len() {43 if pos + size > data.len() {
44 break;44 break;
45 }45 }
46+ let mut end = pos + size;
47+ while end + 6 <= data.len() && data[end] == 0x0B && data[end + 1] == 0x77 && data[end + 5] >> 3 > 10 && data[end + 2] >> 6 == 1 {
48+ let Ok(more) = ec_ac3::frame_size(&data[end..]) else {
49+ break;
50+ };
51+ if end + more > data.len() {
52+ break;
53+ }
54+ end += more;
55+ }
56+ let size = end - pos;
46 let frame = decoder57 let frame = decoder
47 .decode_frame(&data[pos..pos + size])58 .decode_frame(&data[pos..pos + size])
48 .unwrap_or_else(|e| panic!("{}: {e}", path.display()));59 .unwrap_or_else(|e| panic!("{}: {e}", path.display()));