forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB8dbd683Tahinli3 months ago856 commits

tables.rs204 lines, 7.0 KiBHistoryRawDownload
1//! The constant tables Layer III needs besides the Huffman codes: scalefactor
2//! band layouts, the pre-emphasis table, and the two lazily built power tables
3//! requantisation runs on.
4//!
5//! The three MPEG-1 long-block layouts here were measured against a reference
6//! decoder the same way the Huffman tables were (`scripts/mp3-tables/`, one
7//! band attenuated at a time), which is also why they are stated as widths: a
8//! width table cannot silently disagree with itself about where 576 ends.
9
10use std::sync::OnceLock;
11
12/// Widths of the 22 long scalefactor bands, by sample rate.
13pub(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 // 8 kHz is the odd one out: five two-wide bands at the top rather than
31 // one wide one.
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/// Widths of the 13 short scalefactor bands (per window), by sample rate.
39pub(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 // Measured: the MPEG-2.5 rates 11.025 and 12 kHz share the 16 kHz
47 // layout rather than the LSF rate they halve.
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/// Start offset of each long band, plus 576 as the last entry.
54pub(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/// Start offset of each short band within one window, plus 192.
66pub(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/// Additional high-frequency scalefactor applied when `preflag` is set.
78pub(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/// `slen1`/`slen2` for the MPEG-1 `scalefac_compress` index.
83pub(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/// Scalefactor band counts per partition for the MPEG-2 LSF scalefactor
103/// scheme, indexed `[block number][0 = long, 1 = short, 2 = mixed]`.
104pub(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/// The largest quantised magnitude Layer III can code (15 plus 13 linbits).
114pub(crate) const MAX_QUANT: usize = 8206;
115
116/// `|is|^(4/3)`, the requantisation curve, tabulated once.
117pub(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/// The four Layer III block windows: normal, start, short, stop.
129pub(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/// Alias-reduction butterfly coefficients, `(cs, ca)` for the eight lines each
162/// side of a subband boundary.
163pub(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)]
179mod 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}