forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB552edc4Tahinli2 months ago2048 commits

exps.rs178 lines, 5.8 KiBHistoryRawDownload
1//! Exponent decoding (A/52 §7.1.3).
2//!
3//! Exponents travel as 7-bit groups of three differential values, each of which
4//! covers one, two or four mantissas depending on the strategy. A group whose
5//! mapped value exceeds 124, or a running exponent outside 0..=24, is a corrupt
6//! stream rather than a clamp: the bit allocation that follows would otherwise
7//! read the rest of the block at the wrong offsets and call it audio.
8
9use ec_core::{BitReader, Error, Result};
10
11/// Exponent strategy (Tables 7.4, 7.5).
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
13pub enum Strategy {
14 /// Keep the previous block's exponents for this channel.
15 #[default]
16 Reuse,
17 /// One exponent per mantissa.
18 D15,
19 /// One exponent per pair.
20 D25,
21 /// One exponent per quad.
22 D45,
23}
24
25impl Strategy {
26 /// From the 2-bit `chexpstr`/`cplexpstr` code.
27 pub fn from_code(code: u32) -> Strategy {
28 match code & 3 {
29 0 => Strategy::Reuse,
30 1 => Strategy::D15,
31 2 => Strategy::D25,
32 _ => Strategy::D45,
33 }
34 }
35
36 /// Mantissas covered by one differential exponent: 1, 2 or 4.
37 pub fn group_size(self) -> usize {
38 match self {
39 Strategy::Reuse => 0,
40 Strategy::D15 => 1,
41 Strategy::D25 => 2,
42 Strategy::D45 => 4,
43 }
44 }
45
46 /// Number of 7-bit groups for a full-bandwidth or coupled channel whose
47 /// mantissas end at `endmant` (§7.1.3), not counting the absolute exponent.
48 pub fn fbw_groups(self, endmant: usize) -> usize {
49 match self {
50 Strategy::Reuse => 0,
51 Strategy::D15 => endmant.saturating_sub(1) / 3,
52 Strategy::D25 => (endmant + 2) / 6,
53 Strategy::D45 => (endmant + 8) / 12,
54 }
55 }
56
57 /// Number of 7-bit groups for the coupling channel over `[start, end)`.
58 pub fn coupling_groups(self, start: usize, end: usize) -> usize {
59 let span = end.saturating_sub(start);
60 match self {
61 Strategy::Reuse => 0,
62 Strategy::D15 => span / 3,
63 Strategy::D25 => span / 6,
64 Strategy::D45 => span / 12,
65 }
66 }
67}
68
69/// Read `ngrps` groups and expand them into `exps`, starting at `first_bin`.
70///
71/// `previous` is the absolute exponent the first differential is applied to:
72/// `exps[ch][0]` for a full-bandwidth or LFE channel (which the caller has
73/// already stored), or `cplabsexp << 1` for the coupling channel.
74pub fn decode(
75 r: &mut BitReader<'_>,
76 strategy: Strategy,
77 ngrps: usize,
78 previous: u8,
79 first_bin: usize,
80 exps: &mut [u8],
81) -> Result<()> {
82 let group_size = strategy.group_size();
83 if group_size == 0 {
84 return Ok(());
85 }
86 let mut prev = i32::from(previous);
87 let mut bin = first_bin;
88 for _ in 0..ngrps {
89 let gexp = r.read_bits(7)?;
90 if gexp > 124 {
91 return Err(Error::corrupt(format!(
92 "AC-3 exponents: grouped value {gexp} > 124"
93 )));
94 }
95 for mapped in [gexp / 25, (gexp % 25) / 5, gexp % 5] {
96 prev += mapped as i32 - 2;
97 if !(0..=24).contains(&prev) {
98 return Err(Error::corrupt(format!(
99 "AC-3 exponents: absolute exponent {prev} outside 0..=24"
100 )));
101 }
102 for _ in 0..group_size {
103 if bin >= exps.len() {
104 // The last group of a D25/D45 set can overrun the channel's
105 // own bandwidth by design; those values are not used.
106 break;
107 }
108 exps[bin] = prev as u8;
109 bin += 1;
110 }
111 }
112 }
113 Ok(())
114}
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119 use ec_core::BitWriter;
120
121 #[test]
122 fn d15_expands_one_exponent_per_mantissa() {
123 // Two groups: deltas (+2, 0, -1) and (0, +1, +1) from an absolute 5.
124 let mut w = BitWriter::new();
125 w.write_bits(4 * 25 + 2 * 5 + 1, 7);
126 w.write_bits(2 * 25 + 3 * 5 + 3, 7);
127 let bytes = w.into_bytes();
128 let mut r = BitReader::new(&bytes);
129 let mut exps = [0u8; 16];
130 exps[0] = 5;
131 decode(&mut r, Strategy::D15, 2, 5, 1, &mut exps).unwrap();
132 assert_eq!(&exps[..7], &[5, 7, 7, 6, 6, 7, 8]);
133 }
134
135 #[test]
136 fn d45_repeats_each_exponent_four_times() {
137 let mut w = BitWriter::new();
138 w.write_bits(3 * 25 + 2 * 5 + 2, 7); // +1, 0, 0
139 let bytes = w.into_bytes();
140 let mut r = BitReader::new(&bytes);
141 let mut exps = [0u8; 16];
142 exps[0] = 10;
143 decode(&mut r, Strategy::D45, 1, 10, 1, &mut exps).unwrap();
144 assert_eq!(
145 &exps[..13],
146 &[10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11]
147 );
148 }
149
150 #[test]
151 fn out_of_range_exponents_are_corrupt_not_clamped() {
152 let mut w = BitWriter::new();
153 w.write_bits(4 * 25 + 4 * 5 + 4, 7); // +2, +2, +2 from 23 => 29
154 let bytes = w.into_bytes();
155 let mut r = BitReader::new(&bytes);
156 let mut exps = [0u8; 16];
157 assert!(matches!(
158 decode(&mut r, Strategy::D15, 1, 23, 1, &mut exps),
159 Err(Error::Corrupt { .. })
160 ));
161 // And a truncated group is NeedMore, never a panic.
162 let mut r = BitReader::new(&[0u8; 0]);
163 assert!(matches!(
164 decode(&mut r, Strategy::D15, 1, 5, 1, &mut exps),
165 Err(Error::NeedMore)
166 ));
167 }
168
169 #[test]
170 fn group_counts_match_the_standards_formulas() {
171 // §7.1.3 worked cases: a full-bandwidth channel ending at 253.
172 assert_eq!(Strategy::D15.fbw_groups(253), 84);
173 assert_eq!(Strategy::D25.fbw_groups(253), 42);
174 assert_eq!(Strategy::D45.fbw_groups(253), 21);
175 assert_eq!(Strategy::D15.fbw_groups(7), 2);
176 assert_eq!(Strategy::D25.coupling_groups(37, 253), 36);
177 }
178}