| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | use ec_core::{BitReader, Error, Result}; |
| 10 | |
| 11 | |
| 12 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 13 | pub enum Strategy { |
| 14 | |
| 15 | #[default] |
| 16 | Reuse, |
| 17 | |
| 18 | D15, |
| 19 | |
| 20 | D25, |
| 21 | |
| 22 | D45, |
| 23 | } |
| 24 | |
| 25 | impl Strategy { |
| 26 | |
| 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 | |
| 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 | |
| 47 | |
| 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 | |
| 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 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | pub 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 | |
| 105 | |
| 106 | break; |
| 107 | } |
| 108 | exps[bin] = prev as u8; |
| 109 | bin += 1; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | Ok(()) |
| 114 | } |
| 115 | |
| 116 | #[cfg(test)] |
| 117 | mod tests { |
| 118 | use super::*; |
| 119 | use ec_core::BitWriter; |
| 120 | |
| 121 | #[test] |
| 122 | fn d15_expands_one_exponent_per_mantissa() { |
| 123 | |
| 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); |
| 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); |
| 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 | |
| 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 | |
| 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 | } |