| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | use std::sync::OnceLock; |
| 11 | |
| 12 | |
| 13 | pub(crate) fn long_widths(sample_rate: u32) -> &'static [u16; 22] { |
| 14 | match sample_rate { |
| 15 | 44100 => &[ |
| 16 | 4, 4, 4, 4, 4, 4, 6, 6, 8, 8, 10, 12, 16, 20, 24, 28, 34, 42, 50, 54, 76, 158, |
| 17 | ], |
| 18 | 48000 => &[ |
| 19 | 4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 10, 12, 16, 18, 22, 28, 34, 40, 46, 54, 54, 192, |
| 20 | ], |
| 21 | 32000 => &[ |
| 22 | 4, 4, 4, 4, 4, 4, 6, 6, 8, 10, 12, 16, 20, 24, 30, 38, 46, 56, 68, 84, 102, 26, |
| 23 | ], |
| 24 | 22050 | 16000 | 11025 | 12000 => &[ |
| 25 | 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, |
| 26 | ], |
| 27 | 24000 => &[ |
| 28 | 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16, 18, 22, 26, 32, 38, 46, 54, 62, 70, 76, 36, |
| 29 | ], |
| 30 | |
| 31 | |
| 32 | _ => &[ |
| 33 | 12, 12, 12, 12, 12, 12, 16, 20, 24, 28, 32, 40, 48, 56, 64, 76, 90, 2, 2, 2, 2, 2, |
| 34 | ], |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | |
| 39 | pub(crate) fn short_widths(sample_rate: u32) -> &'static [u16; 13] { |
| 40 | match sample_rate { |
| 41 | 44100 => &[4, 4, 4, 4, 6, 8, 10, 12, 14, 18, 22, 30, 56], |
| 42 | 48000 => &[4, 4, 4, 4, 6, 6, 10, 12, 14, 16, 20, 26, 66], |
| 43 | 32000 => &[4, 4, 4, 4, 6, 8, 12, 16, 20, 26, 34, 42, 12], |
| 44 | 22050 => &[4, 4, 4, 6, 6, 8, 10, 14, 18, 26, 32, 42, 18], |
| 45 | 24000 => &[4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 32, 44, 12], |
| 46 | |
| 47 | |
| 48 | 16000 | 11025 | 12000 => &[4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18], |
| 49 | _ => &[8, 8, 8, 12, 16, 20, 24, 28, 36, 2, 2, 2, 26], |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
| 54 | pub(crate) fn long_starts(sample_rate: u32) -> [u16; 23] { |
| 55 | let mut out = [0u16; 23]; |
| 56 | let mut acc = 0; |
| 57 | for (i, w) in long_widths(sample_rate).iter().enumerate() { |
| 58 | out[i] = acc; |
| 59 | acc += w; |
| 60 | } |
| 61 | out[22] = acc; |
| 62 | out |
| 63 | } |
| 64 | |
| 65 | |
| 66 | pub(crate) fn short_starts(sample_rate: u32) -> [u16; 14] { |
| 67 | let mut out = [0u16; 14]; |
| 68 | let mut acc = 0; |
| 69 | for (i, w) in short_widths(sample_rate).iter().enumerate() { |
| 70 | out[i] = acc; |
| 71 | acc += w; |
| 72 | } |
| 73 | out[13] = acc; |
| 74 | out |
| 75 | } |
| 76 | |
| 77 | |
| 78 | pub(crate) const PRETAB: [u8; 22] = [ |
| 79 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0, |
| 80 | ]; |
| 81 | |
| 82 | |
| 83 | pub(crate) const SLEN: [(u32, u32); 16] = [ |
| 84 | (0, 0), |
| 85 | (0, 1), |
| 86 | (0, 2), |
| 87 | (0, 3), |
| 88 | (3, 0), |
| 89 | (1, 1), |
| 90 | (1, 2), |
| 91 | (1, 3), |
| 92 | (2, 1), |
| 93 | (2, 2), |
| 94 | (2, 3), |
| 95 | (3, 1), |
| 96 | (3, 2), |
| 97 | (3, 3), |
| 98 | (4, 2), |
| 99 | (4, 3), |
| 100 | ]; |
| 101 | |
| 102 | |
| 103 | |
| 104 | pub(crate) const LSF_PARTITIONS: [[[u8; 4]; 3]; 6] = [ |
| 105 | [[6, 5, 5, 5], [9, 9, 9, 9], [6, 9, 9, 9]], |
| 106 | [[6, 5, 7, 3], [9, 9, 12, 6], [6, 9, 12, 6]], |
| 107 | [[11, 10, 0, 0], [18, 18, 0, 0], [15, 18, 0, 0]], |
| 108 | [[7, 7, 7, 0], [12, 12, 12, 0], [6, 15, 12, 0]], |
| 109 | [[6, 6, 6, 3], [12, 9, 9, 6], [6, 12, 9, 6]], |
| 110 | [[8, 8, 5, 0], [15, 12, 9, 0], [6, 18, 9, 0]], |
| 111 | ]; |
| 112 | |
| 113 | |
| 114 | pub(crate) const MAX_QUANT: usize = 8206; |
| 115 | |
| 116 | |
| 117 | pub(crate) fn power43() -> &'static [f32; MAX_QUANT + 1] { |
| 118 | static TABLE: OnceLock<Box<[f32; MAX_QUANT + 1]>> = OnceLock::new(); |
| 119 | TABLE.get_or_init(|| { |
| 120 | let mut table = Box::new([0.0f32; MAX_QUANT + 1]); |
| 121 | for (i, slot) in table.iter_mut().enumerate() { |
| 122 | *slot = (i as f64).powf(4.0 / 3.0) as f32; |
| 123 | } |
| 124 | table |
| 125 | }) |
| 126 | } |
| 127 | |
| 128 | |
| 129 | pub(crate) fn windows() -> &'static [[f32; 36]; 4] { |
| 130 | static WINDOWS: OnceLock<[[f32; 36]; 4]> = OnceLock::new(); |
| 131 | WINDOWS.get_or_init(|| { |
| 132 | let mut w = [[0.0f32; 36]; 4]; |
| 133 | let sin36 = |i: usize| (std::f64::consts::PI / 36.0 * (i as f64 + 0.5)).sin() as f32; |
| 134 | let sin12 = |i: usize| (std::f64::consts::PI / 12.0 * (i as f64 + 0.5)).sin() as f32; |
| 135 | for (i, slot) in w[0].iter_mut().enumerate() { |
| 136 | *slot = sin36(i); |
| 137 | } |
| 138 | for (i, slot) in w[1].iter_mut().enumerate() { |
| 139 | *slot = match i { |
| 140 | 0..=17 => sin36(i), |
| 141 | 18..=23 => 1.0, |
| 142 | 24..=29 => sin12(i - 18), |
| 143 | _ => 0.0, |
| 144 | }; |
| 145 | } |
| 146 | for (i, slot) in w[2].iter_mut().enumerate().take(12) { |
| 147 | *slot = sin12(i); |
| 148 | } |
| 149 | for (i, slot) in w[3].iter_mut().enumerate() { |
| 150 | *slot = match i { |
| 151 | 0..=5 => 0.0, |
| 152 | 6..=11 => sin12(i - 6), |
| 153 | 12..=17 => 1.0, |
| 154 | _ => sin36(i), |
| 155 | }; |
| 156 | } |
| 157 | w |
| 158 | }) |
| 159 | } |
| 160 | |
| 161 | |
| 162 | |
| 163 | pub(crate) fn alias_coefficients() -> &'static [(f32, f32); 8] { |
| 164 | static COEFFS: OnceLock<[(f32, f32); 8]> = OnceLock::new(); |
| 165 | COEFFS.get_or_init(|| { |
| 166 | let ci = [ |
| 167 | -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037, |
| 168 | ]; |
| 169 | let mut out = [(0.0f32, 0.0f32); 8]; |
| 170 | for (slot, c) in out.iter_mut().zip(ci) { |
| 171 | let norm = (1.0f64 + c * c).sqrt(); |
| 172 | *slot = ((1.0 / norm) as f32, (c / norm) as f32); |
| 173 | } |
| 174 | out |
| 175 | }) |
| 176 | } |
| 177 | |
| 178 | #[cfg(test)] |
| 179 | mod tests { |
| 180 | use super::*; |
| 181 | |
| 182 | #[test] |
| 183 | fn every_band_layout_spans_its_granule() { |
| 184 | for rate in [44100, 48000, 32000, 24000, 22050, 16000, 12000, 11025, 8000] { |
| 185 | let long: u16 = long_widths(rate).iter().sum(); |
| 186 | let short: u16 = short_widths(rate).iter().sum(); |
| 187 | assert_eq!(long, 576, "long bands at {rate} Hz"); |
| 188 | assert_eq!(short * 3, 576, "short bands at {rate} Hz"); |
| 189 | assert_eq!(long_starts(rate)[22], 576); |
| 190 | assert_eq!(short_starts(rate)[13], 192); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | #[test] |
| 195 | fn lsf_partitions_cover_their_band_counts() { |
| 196 | for (block, entry) in LSF_PARTITIONS.iter().enumerate() { |
| 197 | let sums: Vec<u32> = entry |
| 198 | .iter() |
| 199 | .map(|p| p.iter().map(|v| u32::from(*v)).sum()) |
| 200 | .collect(); |
| 201 | assert_eq!(sums, vec![21, 36, 33], "block number {block}"); |
| 202 | } |
| 203 | } |
| 204 | } |