1use crate::compat::OnceCell;
2
3use crate::mdct::MdctLookup;
4
5pub const BAND_ALLOCATION: [u8; 11 * 21] = [
6 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 80, 75, 69, 63, 56, 49, 40,
7 34, 29, 20, 18, 10, 0, 0, 0, 0, 0, 0, 0, 0, 110, 100, 90, 84, 78, 71, 65, 58, 51, 45, 39, 32,
8 26, 20, 12, 0, 0, 0, 0, 0, 0, 118, 110, 103, 93, 86, 80, 75, 70, 65, 59, 53, 47, 40, 31, 23,
9 15, 4, 0, 0, 0, 0, 126, 119, 112, 104, 95, 89, 83, 78, 72, 66, 60, 54, 47, 39, 32, 25, 17, 12,
10 1, 0, 0, 134, 127, 120, 114, 103, 97, 91, 85, 78, 72, 66, 60, 54, 47, 41, 35, 29, 23, 16, 10,
11 1, 144, 137, 130, 124, 113, 107, 101, 95, 88, 82, 76, 70, 64, 57, 51, 45, 39, 33, 26, 15, 1,
12 152, 145, 138, 132, 123, 117, 111, 105, 98, 92, 86, 80, 74, 67, 61, 55, 49, 43, 36, 20, 1, 162,
13 155, 148, 142, 133, 127, 121, 115, 108, 102, 96, 90, 84, 77, 71, 65, 59, 53, 46, 30, 1, 172,
14 165, 158, 152, 143, 137, 131, 125, 118, 112, 106, 100, 94, 87, 81, 75, 69, 63, 56, 45, 20, 200,
15 200, 200, 200, 200, 200, 200, 200, 198, 193, 188, 183, 178, 173, 168, 163, 158, 153, 148, 129, 104,
16];
17
18pub const CACHE_INDEX50: [i16; 105] = [
19 -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 41, 41, 41, 82, 82, 123, 164, 200, 222, 0, 0, 0, 0,
20 0, 0, 0, 0, 41, 41, 41, 41, 123, 123, 123, 164, 164, 240, 266, 283, 295, 41, 41, 41, 41, 41,
21 41, 41, 41, 123, 123, 123, 123, 240, 240, 240, 266, 266, 305, 318, 328, 336, 123, 123, 123,
22 123, 123, 123, 123, 123, 240, 240, 240, 240, 305, 305, 305, 318, 318, 343, 351, 358, 364, 240,
23 240, 240, 240, 240, 240, 240, 240, 305, 305, 305, 305, 343, 343, 343, 351, 351, 370, 376, 382,
24 387,
25];
26
27pub const CACHE_BITS50: [u8; 392] = [
28 40, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
29 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 40, 15, 23, 28, 31, 34, 36, 38, 39, 41, 42, 43, 44, 45, 46, 47,
30 47, 49, 50, 51, 52, 53, 54, 55, 55, 57, 58, 59, 60, 61, 62, 63, 63, 65, 66, 67, 68, 69, 70, 71,
31 71, 40, 20, 33, 41, 48, 53, 57, 61, 64, 66, 69, 71, 73, 75, 76, 78, 80, 82, 85, 87, 89, 91, 92,
32 94, 96, 98, 101, 103, 105, 107, 108, 110, 112, 114, 117, 119, 121, 123, 124, 126, 128, 40, 23,
33 39, 51, 60, 67, 73, 79, 83, 87, 91, 94, 97, 100, 102, 105, 107, 111, 115, 118, 121, 124, 126,
34 129, 131, 135, 139, 142, 145, 148, 150, 153, 155, 159, 163, 166, 169, 172, 174, 177, 179, 35,
35 28, 49, 65, 78, 89, 99, 107, 114, 120, 126, 132, 136, 141, 145, 149, 153, 159, 165, 171, 176,
36 180, 185, 189, 192, 199, 205, 211, 216, 220, 225, 229, 232, 239, 245, 251, 21, 33, 58, 79, 97,
37 112, 125, 137, 148, 157, 166, 174, 182, 189, 195, 201, 207, 217, 227, 235, 243, 251, 17, 35,
38 63, 86, 106, 123, 139, 152, 165, 177, 187, 197, 206, 214, 222, 230, 237, 250, 25, 31, 55, 75,
39 91, 105, 117, 128, 138, 146, 154, 161, 168, 174, 180, 185, 190, 200, 208, 215, 222, 229, 235,
40 240, 245, 255, 16, 36, 65, 89, 110, 128, 144, 159, 173, 185, 196, 207, 217, 226, 234, 242, 250,
41 11, 41, 74, 103, 128, 151, 172, 191, 209, 225, 241, 255, 9, 43, 79, 110, 138, 163, 186, 207,
42 227, 246, 12, 39, 71, 99, 123, 144, 164, 182, 198, 214, 228, 241, 253, 9, 44, 81, 113, 142,
43 168, 192, 214, 235, 255, 7, 49, 90, 127, 160, 191, 220, 247, 6, 51, 95, 134, 170, 203, 234, 7,
44 47, 87, 123, 155, 184, 212, 237, 6, 52, 97, 137, 174, 208, 240, 5, 57, 106, 151, 192, 231, 5,
45 59, 111, 158, 202, 243, 5, 55, 103, 147, 187, 224, 5, 60, 113, 161, 206, 248, 4, 65, 122, 175,
46 224, 4, 67, 127, 182, 234,
47];
48
49pub const CACHE_CAPS50: [u8; 168] = [
50 224, 224, 224, 224, 224, 224, 224, 224, 160, 160, 160, 160, 185, 185, 185, 178, 178, 168, 134,
51 61, 37, 224, 224, 224, 224, 224, 224, 224, 224, 240, 240, 240, 240, 207, 207, 207, 198, 198,
52 183, 144, 66, 40, 160, 160, 160, 160, 160, 160, 160, 160, 185, 185, 185, 185, 193, 193, 193,
53 183, 183, 172, 138, 64, 38, 240, 240, 240, 240, 240, 240, 240, 240, 207, 207, 207, 207, 204,
54 204, 204, 193, 193, 180, 143, 66, 40, 185, 185, 185, 185, 185, 185, 185, 185, 193, 193, 193,
55 193, 193, 193, 193, 183, 183, 172, 138, 65, 39, 207, 207, 207, 207, 207, 207, 207, 207, 204,
56 204, 204, 204, 201, 201, 201, 188, 188, 176, 141, 66, 40, 193, 193, 193, 193, 193, 193, 193,
57 193, 193, 193, 193, 193, 194, 194, 194, 184, 184, 173, 139, 65, 39, 204, 204, 204, 204, 204,
58 204, 204, 204, 201, 201, 201, 201, 198, 198, 198, 187, 187, 175, 140, 66, 40,
59];
60
61pub struct PulseCache {
62 pub size: usize,
63 pub index: &'static [i16],
64 pub bits: &'static [u8],
65 pub caps: &'static [u8],
66}
67
68pub struct CeltMode {
69 pub fs: i32,
70 pub overlap: usize,
71 pub nb_ebands: usize,
72 pub eff_ebands: usize,
73 pub preemph: [f32; 4],
74 pub e_bands: &'static [i16],
75 pub max_lm: usize,
76 pub nb_short_mdcts: usize,
77 pub short_mdct_size: usize,
78 pub nb_alloc_vectors: usize,
79 pub alloc_vectors: &'static [u8],
80 pub alloc_stride: usize,
81 pub log_n: &'static [i16],
82 pub window: &'static [f32],
83 pub mdct: MdctLookup,
84 pub cache: PulseCache,
85 pub e_means: &'static [f32],
86}
87
88pub const EBAND_5MS: [i16; 22] = [
89 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100,
90];
91
92impl CeltMode {
93 pub fn new_48000_960_120() -> Self {
94 let short_mdct_size = 120;
95 let nb_short_mdcts = 8;
96
97 let max_lm = 3usize;
98 let mdct = MdctLookup::new(2 * short_mdct_size * nb_short_mdcts, max_lm);
99 Self {
100 fs: 48000,
101 overlap: 120,
102 nb_ebands: 21,
103 eff_ebands: 21,
104 preemph: [0.850_006_1, 0.100_006_1, 1.512_834_7, 0.661_010_7],
105 e_bands: &EBAND_5MS,
106 max_lm,
107 nb_short_mdcts,
108 short_mdct_size,
109 nb_alloc_vectors: 11,
110 alloc_vectors: &BAND_ALLOCATION,
111 alloc_stride: 21,
112 log_n: &LOG_N_400,
113 window: &WINDOW_120,
114 mdct,
115 cache: PulseCache {
116 size: 392,
117 index: &CACHE_INDEX50,
118 bits: &CACHE_BITS50,
119 caps: &CACHE_CAPS50,
120 },
121 e_means: &E_MEANS,
122 }
123 }
124}
125
126pub const E_MEANS: [f32; 25] = [
127 6.4375, 6.25, 5.75, 5.3125, 5.0625, 4.8125, 4.5, 4.375, 4.875, 4.6875, 4.5625, 4.4375, 4.875,
128 4.625, 4.3125, 4.5, 4.375, 4.625, 4.75, 4.4375, 3.75, 3.75, 3.75, 3.75, 3.75,
129];
130
131pub fn default_mode() -> &'static CeltMode {
132 MODE_48000_960_120.get(CeltMode::new_48000_960_120)
133}
134
135static MODE_48000_960_120: OnceCell<CeltMode> = OnceCell::new();
136
137const LOG_N_400: [i16; 21] = [
138 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 16, 16, 16, 21, 21, 24, 29, 34, 36,
139];
140
141const WINDOW_120: [f32; 120] = [
142 6.728_696_6e-05,
143 0.000_605_513_5,
144 0.001_681_597,
145 0.003_294_796_2,
146 0.005_443_994_3,
147 0.008_127_692,
148 0.011_344_001,
149 0.015_090_633,
150 0.019_364_886,
151 0.024_163_635,
152 0.029_483_315,
153 0.035_319_905,
154 0.041_668_91,
155 0.048_525_35,
156 0.055_883_718,
157 0.063_737_999,
158 0.072_081_62,
159 0.080_907_43,
160 0.090_207_7,
161 0.099_974_11,
162 0.110_197_69,
163 0.120_868_83,
164 0.131_977_29,
165 0.143_512_14,
166 0.155_461_77,
167 0.167_813_9,
168 0.180_555_5,
169 0.193_672_9,
170 0.207_151_71,
171 0.220_976_82,
172 0.235_132_43,
173 0.249_602_08,
174 0.264_368_6,
175 0.279_414_19,
176 0.294_720_4,
177 0.310_268_2,
178 0.326_037_88,
179 0.342_009_3,
180 0.358_161_77,
181 0.374_474_07,
182 0.390_924_62,
183 0.407_491_42,
184 0.424_152_15,
185 0.440_884_23,
186 0.457_664_84,
187 0.474_471_04,
188 0.491_279_78,
189 0.508_067_98,
190 0.524_812_61,
191 0.541_490_8,
192 0.558_079_7,
193 0.574_557,
194 0.590_900_5,
195 0.607_088_4,
196 0.623_099_5,
197 0.638_913_06,
198 0.654_508_96,
199 0.669_867_76,
200 0.684_970_8,
201 0.699_800_1,
202 0.714_338_7,
203 0.728_570_5,
204 0.742_480_43,
205 0.756_054_2,
206 0.769_278_95,
207 0.782_142_6,
208 0.794_634_3,
209 0.806_744_45,
210 0.818_464_6,
211 0.829_787_3,
212 0.840_706_7,
213 0.851_217_8,
214 0.861_317,
215 0.871_001_83,
216 0.880_271_11,
217 0.889_124_8,
218 0.897_564,
219 0.905_590_94,
220 0.913_209,
221 0.920_422_7,
222 0.927_237_4,
223 0.933_659_55,
224 0.939_696_56,
225 0.945_356_7,
226 0.950_649_1,
227 0.955_583_5,
228 0.960_170_7,
229 0.964_421_7,
230 0.968_348_5,
231 0.971_963_34,
232 0.975_279_06,
233 0.978_308_83,
234 0.981_066_16,
235 0.983_564_8,
236 0.985_818_7,
237 0.987_841_9,
238 0.989_648_6,
239 0.991_252_7,
240 0.992_668_5,
241 0.993_909_7,
242 0.994_990_04,
243 0.995_923,
244 0.996_721_6,
245 0.997_398_74,
246 0.997_966_67,
247 0.998_437_3,
248 0.998_822,
249 0.999_131_47,
250 0.999_376_06,
251 0.999_565_27,
252 0.999_708,
253 0.999_812_5,
254 0.999_886_13,
255 0.999_935_6,
256 0.999_967,
257 0.999_985_18,
258 0.999_994_6,
259 0.999_998_59,
260 0.999_999_8,
261 1.0,
262];
263
264pub const SPREAD_ICDF: [u8; 4] = [25, 23, 2, 0];
265pub const TRIM_ICDF: [u8; 11] = [126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0];
266pub const TAPSET_ICDF: [u8; 3] = [2, 1, 0];
267
268pub const TF_SELECT_TABLE: [[i8; 8]; 4] = [
269 [0, -1, 0, -1, 0, -1, 0, -1],
270 [0, -1, 0, -2, 1, 0, 1, -1],
271 [0, -2, 0, -3, 2, 0, 1, -1],
272 [0, -2, 0, -3, 3, 0, 1, -1],
273];