Skip to main content

opus_rs/
modes.rs

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