| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | use ec_core::{BitReader, Result}; |
| 12 | |
| 13 | use crate::aht_tables::{GAQ_BITS, REMAP, VQ_BITS, vq_table}; |
| 14 | use crate::decode::COEFFS; |
| 15 | use crate::mantissa::Dither; |
| 16 | |
| 17 | |
| 18 | pub(crate) const AHT_BLOCKS: usize = 6; |
| 19 | |
| 20 | |
| 21 | pub(crate) type Frame = [f32]; |
| 22 | |
| 23 | |
| 24 | fn gain_of(gaqmod: u32, mapped: u32) -> u32 { |
| 25 | match (gaqmod, mapped) { |
| 26 | (_, 0) => 1, |
| 27 | (1, _) | (3, 1) => 2, |
| 28 | _ => 4, |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | |
| 33 | fn remap(hebap: u8, gain: u32, x: f32) -> f32 { |
| 34 | let row = (hebap as usize).saturating_sub(8).min(11); |
| 35 | let column = match gain { |
| 36 | 1 => 0, |
| 37 | 2 => 1, |
| 38 | _ => 2, |
| 39 | }; |
| 40 | let (a, b) = REMAP[row][column][usize::from(x < 0.0)]; |
| 41 | x + x * (f32::from(a) / 32768.0) + f32::from(b) / 32768.0 |
| 42 | } |
| 43 | |
| 44 | |
| 45 | fn read_gaq(r: &mut BitReader<'_>, hebap: u8, gain: u32) -> Result<f32> { |
| 46 | let m = GAQ_BITS[(hebap as usize).min(19)]; |
| 47 | if gain == 1 { |
| 48 | let code = r.read_signed(m)?; |
| 49 | return Ok(remap(hebap, 1, code as f32 / (1u32 << (m - 1)) as f32)); |
| 50 | } |
| 51 | let small = if gain == 2 { m - 1 } else { m - 2 }; |
| 52 | let code = r.read_signed(small)?; |
| 53 | |
| 54 | |
| 55 | if code == -(1i32 << (small - 1)) { |
| 56 | let large = if gain == 2 { m - 1 } else { m }; |
| 57 | let value = r.read_signed(large)? as f32 / (1u32 << (large - 1)) as f32; |
| 58 | Ok(remap(hebap, gain, value)) |
| 59 | } else { |
| 60 | Ok(code as f32 / (1u32 << (small - 1)) as f32 / gain as f32) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | #[allow(clippy::too_many_arguments)] |
| 70 | pub(crate) fn read_channel( |
| 71 | r: &mut BitReader<'_>, |
| 72 | hebap: &[u8], |
| 73 | range: (usize, usize), |
| 74 | dither: &mut Dither, |
| 75 | dither_on: bool, |
| 76 | out: &mut Frame, |
| 77 | ) -> Result<()> { |
| 78 | let (start, end) = range; |
| 79 | let gaqmod = r.read_bits(2)?; |
| 80 | let endbap = if gaqmod < 2 { 12 } else { 17 }; |
| 81 | |
| 82 | |
| 83 | |
| 84 | let active = hebap[start..end] |
| 85 | .iter() |
| 86 | .filter(|&&h| h > 7 && u32::from(h) < endbap) |
| 87 | .count(); |
| 88 | let sections = match gaqmod { |
| 89 | 0 => 0, |
| 90 | 1 | 2 => active, |
| 91 | _ => active.div_ceil(3), |
| 92 | }; |
| 93 | let mut gains = Vec::with_capacity(sections * 3); |
| 94 | for _ in 0..sections { |
| 95 | if gaqmod == 3 { |
| 96 | |
| 97 | let word = r.read_bits(5)?.min(26); |
| 98 | gains.push(word / 9); |
| 99 | gains.push((word % 9) / 3); |
| 100 | gains.push((word % 9) % 3); |
| 101 | } else { |
| 102 | gains.push(r.read_bits(1)?); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | let mut pre = [0.0f32; AHT_BLOCKS]; |
| 107 | let mut next_gain = 0usize; |
| 108 | for bin in start..end { |
| 109 | let h = hebap[bin]; |
| 110 | let mut zero_bits = false; |
| 111 | if h == 0 { |
| 112 | pre = [0.0; AHT_BLOCKS]; |
| 113 | zero_bits = true; |
| 114 | } else if h <= 7 { |
| 115 | let index = r.read_bits(VQ_BITS[h as usize])? as usize; |
| 116 | let table = vq_table(h); |
| 117 | let vector = table[index.min(table.len() - 1)]; |
| 118 | for (slot, v) in pre.iter_mut().zip(vector) { |
| 119 | *slot = f32::from(v) / 32768.0; |
| 120 | } |
| 121 | } else { |
| 122 | let gain = if gaqmod > 0 && u32::from(h) < endbap { |
| 123 | let mapped = gains.get(next_gain).copied().unwrap_or(0); |
| 124 | next_gain += 1; |
| 125 | gain_of(gaqmod, mapped) |
| 126 | } else { |
| 127 | 1 |
| 128 | }; |
| 129 | for slot in &mut pre { |
| 130 | *slot = read_gaq(r, h, gain)?; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if zero_bits { |
| 135 | |
| 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 | |
| 142 | for (blk, slot) in idct6(&pre).into_iter().enumerate() { |
| 143 | out[blk * COEFFS + bin] = slot; |
| 144 | } |
| 145 | } |
| 146 | Ok(()) |
| 147 | } |
| 148 | |
| 149 | |
| 150 | fn idct6(x: &[f32; AHT_BLOCKS]) -> [f32; AHT_BLOCKS] { |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | const COS: [[f32; AHT_BLOCKS]; AHT_BLOCKS] = cos_table(); |
| 158 | let mut out = [0.0f32; AHT_BLOCKS]; |
| 159 | for (m, slot) in out.iter_mut().enumerate() { |
| 160 | let mut acc = 0.0; |
| 161 | for (j, &v) in x.iter().enumerate() { |
| 162 | acc += v * COS[j][m]; |
| 163 | } |
| 164 | *slot = std::f32::consts::SQRT_2 * acc; |
| 165 | } |
| 166 | out |
| 167 | } |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | const fn cos_table() -> [[f32; AHT_BLOCKS]; AHT_BLOCKS] { |
| 173 | |
| 174 | use std::f32::consts::FRAC_1_SQRT_2; |
| 175 | const C: [f32; 25] = [ |
| 176 | 1.0, |
| 177 | 0.965_925_8, |
| 178 | 0.866_025_4, |
| 179 | FRAC_1_SQRT_2, |
| 180 | 0.5, |
| 181 | 0.258_819_04, |
| 182 | 0.0, |
| 183 | -0.258_819_04, |
| 184 | -0.5, |
| 185 | -FRAC_1_SQRT_2, |
| 186 | -0.866_025_4, |
| 187 | -0.965_925_8, |
| 188 | -1.0, |
| 189 | -0.965_925_8, |
| 190 | -0.866_025_4, |
| 191 | -FRAC_1_SQRT_2, |
| 192 | -0.5, |
| 193 | -0.258_819_04, |
| 194 | 0.0, |
| 195 | 0.258_819_04, |
| 196 | 0.5, |
| 197 | FRAC_1_SQRT_2, |
| 198 | 0.866_025_4, |
| 199 | 0.965_925_8, |
| 200 | 1.0, |
| 201 | ]; |
| 202 | let mut table = [[0.0f32; AHT_BLOCKS]; AHT_BLOCKS]; |
| 203 | let mut j = 0; |
| 204 | while j < AHT_BLOCKS { |
| 205 | let mut m = 0; |
| 206 | while m < AHT_BLOCKS { |
| 207 | let k = (j * (2 * m + 1)) % 24; |
| 208 | let value = C[k]; |
| 209 | |
| 210 | table[j][m] = if j == 0 { value * FRAC_1_SQRT_2 } else { value }; |
| 211 | m += 1; |
| 212 | } |
| 213 | j += 1; |
| 214 | } |
| 215 | table |
| 216 | } |
| 217 | |
| 218 | #[cfg(test)] |
| 219 | mod tests { |
| 220 | use super::*; |
| 221 | |
| 222 | #[test] |
| 223 | fn idct_of_a_dc_only_vector_is_flat() { |
| 224 | let mut x = [0.0f32; AHT_BLOCKS]; |
| 225 | x[0] = 1.0; |
| 226 | let out = idct6(&x); |
| 227 | for v in out { |
| 228 | assert!((v - out[0]).abs() < 1e-5, "{out:?}"); |
| 229 | } |
| 230 | assert!(out[0] > 0.0); |
| 231 | } |
| 232 | |
| 233 | #[test] |
| 234 | fn idct_matches_the_direct_sum() { |
| 235 | |
| 236 | |
| 237 | let x = [0.3f32, -0.7, 0.1, 0.9, -0.2, 0.5]; |
| 238 | let got = idct6(&x); |
| 239 | for (m, &g) in got.iter().enumerate() { |
| 240 | let mut acc = 0.0f64; |
| 241 | for (j, &v) in x.iter().enumerate() { |
| 242 | let r = if j == 0 { 1.0 / 2f64.sqrt() } else { 1.0 }; |
| 243 | let angle = j as f64 * (2 * m + 1) as f64 * std::f64::consts::PI / 12.0; |
| 244 | acc += r * f64::from(v) * angle.cos(); |
| 245 | } |
| 246 | assert!((f64::from(g) - 2f64.sqrt() * acc).abs() < 1e-5, "m = {m}"); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | #[test] |
| 251 | fn vq4_is_the_printed_table_shifted_by_one() { |
| 252 | |
| 253 | |
| 254 | |
| 255 | use crate::aht_tables::VQ4; |
| 256 | assert_eq!(VQ4[31], [0; 6]); |
| 257 | assert_eq!(VQ4[29], [-83, 278, 323, 55, -154, 232]); |
| 258 | assert_eq!(VQ4[0], [6636, -4593, 14173, -17297, -16523, 864]); |
| 259 | |
| 260 | assert_eq!(vq_table(3).len(), 16); |
| 261 | assert_eq!(vq_table(4).len(), 32); |
| 262 | assert_eq!(vq_table(7).len(), 512); |
| 263 | } |
| 264 | |
| 265 | #[test] |
| 266 | fn gaq_gains_follow_table_e3_3() { |
| 267 | assert_eq!(gain_of(1, 0), 1); |
| 268 | assert_eq!(gain_of(1, 1), 2); |
| 269 | assert_eq!(gain_of(2, 1), 4); |
| 270 | assert_eq!(gain_of(3, 1), 2); |
| 271 | assert_eq!(gain_of(3, 2), 4); |
| 272 | } |
| 273 | |
| 274 | #[test] |
| 275 | fn remap_reproduces_the_step_sizes_of_table_e3_5() { |
| 276 | |
| 277 | let a = remap(8, 1, 1.0 / 4.0); |
| 278 | let b = remap(8, 1, 2.0 / 4.0); |
| 279 | assert!((b - a - 2.0 / 7.0).abs() < 1e-4, "{a} {b}"); |
| 280 | |
| 281 | let a = remap(8, 2, 0.0); |
| 282 | let b = remap(8, 2, 0.5); |
| 283 | assert!((b - a - 1.0 / 3.0).abs() < 1e-4, "{a} {b}"); |
| 284 | } |
| 285 | } |