FFmpeg  2.8.15
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
opus_silk.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Andrew D'Addesio
3  * Copyright (c) 2013-2014 Mozilla Corporation
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Opus SILK decoder
25  */
26 
27 #include <stdint.h>
28 
29 #include "opus.h"
30 
31 typedef struct SilkFrame {
32  int coded;
33  int log_gain;
34  int16_t nlsf[16];
35  float lpc[16];
36 
37  float output [2 * SILK_HISTORY];
40 
42 } SilkFrame;
43 
44 struct SilkContext {
47 
48  int midonly;
49  int subframes;
50  int sflength;
51  int flength;
53 
55  int wb;
56 
59  float stereo_weights[2];
60 
62 };
63 
64 static const uint16_t silk_model_stereo_s1[] = {
65  256, 7, 9, 10, 11, 12, 22, 46, 54, 55, 56, 59, 82, 174, 197, 200,
66  201, 202, 210, 234, 244, 245, 246, 247, 249, 256
67 };
68 
69 static const uint16_t silk_model_stereo_s2[] = {256, 85, 171, 256};
70 
71 static const uint16_t silk_model_stereo_s3[] = {256, 51, 102, 154, 205, 256};
72 
73 static const uint16_t silk_model_mid_only[] = {256, 192, 256};
74 
75 static const uint16_t silk_model_frame_type_inactive[] = {256, 26, 256};
76 
77 static const uint16_t silk_model_frame_type_active[] = {256, 24, 98, 246, 256};
78 
79 static const uint16_t silk_model_gain_highbits[3][9] = {
80  {256, 32, 144, 212, 241, 253, 254, 255, 256},
81  {256, 2, 19, 64, 124, 186, 233, 252, 256},
82  {256, 1, 4, 30, 101, 195, 245, 254, 256}
83 };
84 
85 static const uint16_t silk_model_gain_lowbits[] = {256, 32, 64, 96, 128, 160, 192, 224, 256};
86 
87 static const uint16_t silk_model_gain_delta[] = {
88  256, 6, 11, 22, 53, 185, 206, 214, 218, 221, 223, 225, 227, 228, 229, 230,
89  231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246,
90  247, 248, 249, 250, 251, 252, 253, 254, 255, 256
91 };
92 static const uint16_t silk_model_lsf_s1[2][2][33] = {
93  {
94  { // NB or MB, unvoiced
95  256, 44, 78, 108, 127, 148, 160, 171, 174, 177, 179, 195, 197, 199, 200, 205,
96  207, 208, 211, 214, 215, 216, 218, 220, 222, 225, 226, 235, 244, 246, 253, 255, 256
97  }, { // NB or MB, voiced
98  256, 1, 11, 12, 20, 23, 31, 39, 53, 66, 80, 81, 95, 107, 120, 131,
99  142, 154, 165, 175, 185, 196, 204, 213, 221, 228, 236, 237, 238, 244, 245, 251, 256
100  }
101  }, {
102  { // WB, unvoiced
103  256, 31, 52, 55, 72, 73, 81, 98, 102, 103, 121, 137, 141, 143, 146, 147,
104  157, 158, 161, 177, 188, 204, 206, 208, 211, 213, 224, 225, 229, 238, 246, 253, 256
105  }, { // WB, voiced
106  256, 1, 5, 21, 26, 44, 55, 60, 74, 89, 90, 93, 105, 118, 132, 146,
107  152, 166, 178, 180, 186, 187, 199, 211, 222, 232, 235, 245, 250, 251, 252, 253, 256
108  }
109  }
110 };
111 
112 static const uint16_t silk_model_lsf_s2[32][10] = {
113  // NB, MB
114  { 256, 1, 2, 3, 18, 242, 253, 254, 255, 256 },
115  { 256, 1, 2, 4, 38, 221, 253, 254, 255, 256 },
116  { 256, 1, 2, 6, 48, 197, 252, 254, 255, 256 },
117  { 256, 1, 2, 10, 62, 185, 246, 254, 255, 256 },
118  { 256, 1, 4, 20, 73, 174, 248, 254, 255, 256 },
119  { 256, 1, 4, 21, 76, 166, 239, 254, 255, 256 },
120  { 256, 1, 8, 32, 85, 159, 226, 252, 255, 256 },
121  { 256, 1, 2, 20, 83, 161, 219, 249, 255, 256 },
122 
123  // WB
124  { 256, 1, 2, 3, 12, 244, 253, 254, 255, 256 },
125  { 256, 1, 2, 4, 32, 218, 253, 254, 255, 256 },
126  { 256, 1, 2, 5, 47, 199, 252, 254, 255, 256 },
127  { 256, 1, 2, 12, 61, 187, 252, 254, 255, 256 },
128  { 256, 1, 5, 24, 72, 172, 249, 254, 255, 256 },
129  { 256, 1, 2, 16, 70, 170, 242, 254, 255, 256 },
130  { 256, 1, 2, 17, 78, 165, 226, 251, 255, 256 },
131  { 256, 1, 8, 29, 79, 156, 237, 254, 255, 256 }
132 };
133 
134 static const uint16_t silk_model_lsf_s2_ext[] = { 256, 156, 216, 240, 249, 253, 255, 256 };
135 
136 static const uint16_t silk_model_lsf_interpolation_offset[] = { 256, 13, 35, 64, 75, 256 };
137 
138 static const uint16_t silk_model_pitch_highbits[] = {
139  256, 3, 6, 12, 23, 44, 74, 106, 125, 136, 146, 158, 171, 184, 196, 207,
140  216, 224, 231, 237, 241, 243, 245, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256
141 };
142 
143 static const uint16_t silk_model_pitch_lowbits_nb[]= { 256, 64, 128, 192, 256 };
144 
145 static const uint16_t silk_model_pitch_lowbits_mb[]= { 256, 43, 85, 128, 171, 213, 256 };
146 
147 static const uint16_t silk_model_pitch_lowbits_wb[]= { 256, 32, 64, 96, 128, 160, 192, 224, 256 };
148 
149 static const uint16_t silk_model_pitch_delta[] = {
150  256, 46, 48, 50, 53, 57, 63, 73, 88, 114, 152, 182, 204, 219, 229, 236,
151  242, 246, 250, 252, 254, 256
152 };
153 
154 static const uint16_t silk_model_pitch_contour_nb10ms[] = { 256, 143, 193, 256 };
155 
156 static const uint16_t silk_model_pitch_contour_nb20ms[] = {
157  256, 68, 80, 101, 118, 137, 159, 189, 213, 230, 246, 256
158 };
159 
160 static const uint16_t silk_model_pitch_contour_mbwb10ms[] = {
161  256, 91, 137, 176, 195, 209, 221, 229, 236, 242, 247, 252, 256
162 };
163 
164 static const uint16_t silk_model_pitch_contour_mbwb20ms[] = {
165  256, 33, 55, 73, 89, 104, 118, 132, 145, 158, 168, 177, 186, 194, 200, 206,
166  212, 217, 221, 225, 229, 232, 235, 238, 240, 242, 244, 246, 248, 250, 252, 253,
167  254, 255, 256
168 };
169 
170 static const uint16_t silk_model_ltp_filter[] = { 256, 77, 157, 256 };
171 
172 static const uint16_t silk_model_ltp_filter0_sel[] = {
173  256, 185, 200, 213, 226, 235, 244, 250, 256
174 };
175 
176 static const uint16_t silk_model_ltp_filter1_sel[] = {
177  256, 57, 91, 112, 132, 147, 160, 172, 185, 195, 205, 214, 224, 233, 241, 248, 256
178 };
179 
180 static const uint16_t silk_model_ltp_filter2_sel[] = {
181  256, 15, 31, 45, 57, 69, 81, 92, 103, 114, 124, 133, 142, 151, 160, 168,
182  176, 184, 192, 199, 206, 212, 218, 223, 227, 232, 236, 240, 244, 247, 251, 254, 256
183 };
184 
185 static const uint16_t silk_model_ltp_scale_index[] = { 256, 128, 192, 256 };
186 
187 static const uint16_t silk_model_lcg_seed[] = { 256, 64, 128, 192, 256 };
188 
189 static const uint16_t silk_model_exc_rate[2][10] = {
190  { 256, 15, 66, 78, 124, 169, 182, 215, 242, 256 }, // unvoiced
191  { 256, 33, 63, 99, 116, 150, 199, 217, 238, 256 } // voiced
192 };
193 
194 static const uint16_t silk_model_pulse_count[11][19] = {
195  { 256, 131, 205, 230, 238, 241, 244, 245, 246,
196  247, 248, 249, 250, 251, 252, 253, 254, 255, 256 },
197  { 256, 58, 151, 211, 234, 241, 244, 245, 246,
198  247, 248, 249, 250, 251, 252, 253, 254, 255, 256 },
199  { 256, 43, 94, 140, 173, 197, 213, 224, 232,
200  238, 241, 244, 247, 249, 250, 251, 253, 254, 256 },
201  { 256, 17, 69, 140, 197, 228, 240, 245, 246,
202  247, 248, 249, 250, 251, 252, 253, 254, 255, 256 },
203  { 256, 6, 27, 68, 121, 170, 205, 226, 237,
204  243, 246, 248, 250, 251, 252, 253, 254, 255, 256 },
205  { 256, 7, 21, 43, 71, 100, 128, 153, 173,
206  190, 203, 214, 223, 230, 235, 239, 243, 246, 256 },
207  { 256, 2, 7, 21, 50, 92, 138, 179, 210,
208  229, 240, 246, 249, 251, 252, 253, 254, 255, 256 },
209  { 256, 1, 3, 7, 17, 36, 65, 100, 137,
210  171, 199, 219, 233, 241, 246, 250, 252, 254, 256 },
211  { 256, 1, 3, 5, 10, 19, 33, 53, 77,
212  104, 132, 158, 181, 201, 216, 227, 235, 241, 256 },
213  { 256, 1, 2, 3, 9, 36, 94, 150, 189,
214  214, 228, 238, 244, 247, 250, 252, 253, 254, 256 },
215  { 256, 2, 3, 9, 36, 94, 150, 189, 214,
216  228, 238, 244, 247, 250, 252, 253, 254, 256, 256 }
217 };
218 
219 static const uint16_t silk_model_pulse_location[4][168] = {
220  {
221  256, 126, 256,
222  256, 56, 198, 256,
223  256, 25, 126, 230, 256,
224  256, 12, 72, 180, 244, 256,
225  256, 7, 42, 126, 213, 250, 256,
226  256, 4, 24, 83, 169, 232, 253, 256,
227  256, 3, 15, 53, 125, 200, 242, 254, 256,
228  256, 2, 10, 35, 89, 162, 221, 248, 255, 256,
229  256, 2, 7, 24, 63, 126, 191, 233, 251, 255, 256,
230  256, 1, 5, 17, 45, 94, 157, 211, 241, 252, 255, 256,
231  256, 1, 5, 13, 33, 70, 125, 182, 223, 245, 253, 255, 256,
232  256, 1, 4, 11, 26, 54, 98, 151, 199, 232, 248, 254, 255, 256,
233  256, 1, 3, 9, 21, 42, 77, 124, 172, 212, 237, 249, 254, 255, 256,
234  256, 1, 2, 6, 16, 33, 60, 97, 144, 187, 220, 241, 250, 254, 255, 256,
235  256, 1, 2, 3, 11, 25, 47, 80, 120, 163, 201, 229, 245, 253, 254, 255, 256,
236  256, 1, 2, 3, 4, 17, 35, 62, 98, 139, 180, 214, 238, 252, 253, 254, 255, 256
237  },{
238  256, 127, 256,
239  256, 53, 202, 256,
240  256, 22, 127, 233, 256,
241  256, 11, 72, 183, 246, 256,
242  256, 6, 41, 127, 215, 251, 256,
243  256, 4, 24, 83, 170, 232, 253, 256,
244  256, 3, 16, 56, 127, 200, 241, 254, 256,
245  256, 3, 12, 39, 92, 162, 218, 246, 255, 256,
246  256, 3, 11, 30, 67, 124, 185, 229, 249, 255, 256,
247  256, 3, 10, 25, 53, 97, 151, 200, 233, 250, 255, 256,
248  256, 1, 8, 21, 43, 77, 123, 171, 209, 237, 251, 255, 256,
249  256, 1, 2, 13, 35, 62, 97, 139, 186, 219, 244, 254, 255, 256,
250  256, 1, 2, 8, 22, 48, 85, 128, 171, 208, 234, 248, 254, 255, 256,
251  256, 1, 2, 6, 16, 36, 67, 107, 149, 189, 220, 240, 250, 254, 255, 256,
252  256, 1, 2, 5, 13, 29, 55, 90, 128, 166, 201, 227, 243, 251, 254, 255, 256,
253  256, 1, 2, 4, 10, 22, 43, 73, 109, 147, 183, 213, 234, 246, 252, 254, 255, 256
254  },{
255  256, 127, 256,
256  256, 49, 206, 256,
257  256, 20, 127, 236, 256,
258  256, 11, 71, 184, 246, 256,
259  256, 7, 43, 127, 214, 250, 256,
260  256, 6, 30, 87, 169, 229, 252, 256,
261  256, 5, 23, 62, 126, 194, 236, 252, 256,
262  256, 6, 20, 49, 96, 157, 209, 239, 253, 256,
263  256, 1, 16, 39, 74, 125, 175, 215, 245, 255, 256,
264  256, 1, 2, 23, 55, 97, 149, 195, 236, 254, 255, 256,
265  256, 1, 7, 23, 50, 86, 128, 170, 206, 233, 249, 255, 256,
266  256, 1, 6, 18, 39, 70, 108, 148, 186, 217, 238, 250, 255, 256,
267  256, 1, 4, 13, 30, 56, 90, 128, 166, 200, 226, 243, 252, 255, 256,
268  256, 1, 4, 11, 25, 47, 76, 110, 146, 180, 209, 231, 245, 252, 255, 256,
269  256, 1, 3, 8, 19, 37, 62, 93, 128, 163, 194, 219, 237, 248, 253, 255, 256,
270  256, 1, 2, 6, 15, 30, 51, 79, 111, 145, 177, 205, 226, 241, 250, 254, 255, 256
271  },{
272  256, 128, 256,
273  256, 42, 214, 256,
274  256, 21, 128, 235, 256,
275  256, 12, 72, 184, 245, 256,
276  256, 8, 42, 128, 214, 249, 256,
277  256, 8, 31, 86, 176, 231, 251, 256,
278  256, 5, 20, 58, 130, 202, 238, 253, 256,
279  256, 6, 18, 45, 97, 174, 221, 241, 251, 256,
280  256, 6, 25, 53, 88, 128, 168, 203, 231, 250, 256,
281  256, 4, 18, 40, 71, 108, 148, 185, 216, 238, 252, 256,
282  256, 3, 13, 31, 57, 90, 128, 166, 199, 225, 243, 253, 256,
283  256, 2, 10, 23, 44, 73, 109, 147, 183, 212, 233, 246, 254, 256,
284  256, 1, 6, 16, 33, 58, 90, 128, 166, 198, 223, 240, 250, 255, 256,
285  256, 1, 5, 12, 25, 46, 75, 110, 146, 181, 210, 231, 244, 251, 255, 256,
286  256, 1, 3, 8, 18, 35, 60, 92, 128, 164, 196, 221, 238, 248, 253, 255, 256,
287  256, 1, 3, 7, 14, 27, 48, 76, 110, 146, 180, 208, 229, 242, 249, 253, 255, 256
288  }
289 };
290 
291 static const uint16_t silk_model_excitation_lsb[] = {256, 136, 256};
292 
293 static const uint16_t silk_model_excitation_sign[3][2][7][3] = {
294  { // Inactive
295  { // Low offset
296  {256, 2, 256},
297  {256, 207, 256},
298  {256, 189, 256},
299  {256, 179, 256},
300  {256, 174, 256},
301  {256, 163, 256},
302  {256, 157, 256}
303  }, { // High offset
304  {256, 58, 256},
305  {256, 245, 256},
306  {256, 238, 256},
307  {256, 232, 256},
308  {256, 225, 256},
309  {256, 220, 256},
310  {256, 211, 256}
311  }
312  }, { // Unvoiced
313  { // Low offset
314  {256, 1, 256},
315  {256, 210, 256},
316  {256, 190, 256},
317  {256, 178, 256},
318  {256, 169, 256},
319  {256, 162, 256},
320  {256, 152, 256}
321  }, { // High offset
322  {256, 48, 256},
323  {256, 242, 256},
324  {256, 235, 256},
325  {256, 224, 256},
326  {256, 214, 256},
327  {256, 205, 256},
328  {256, 190, 256}
329  }
330  }, { // Voiced
331  { // Low offset
332  {256, 1, 256},
333  {256, 162, 256},
334  {256, 152, 256},
335  {256, 147, 256},
336  {256, 144, 256},
337  {256, 141, 256},
338  {256, 138, 256}
339  }, { // High offset
340  {256, 8, 256},
341  {256, 203, 256},
342  {256, 187, 256},
343  {256, 176, 256},
344  {256, 168, 256},
345  {256, 161, 256},
346  {256, 154, 256}
347  }
348  }
349 };
350 
351 static const int16_t silk_stereo_weights[] = {
352  -13732, -10050, -8266, -7526, -6500, -5000, -2950, -820,
353  820, 2950, 5000, 6500, 7526, 8266, 10050, 13732
354 };
355 
356 static const uint8_t silk_lsf_s2_model_sel_nbmb[32][10] = {
357  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
358  { 1, 3, 1, 2, 2, 1, 2, 1, 1, 1 },
359  { 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
360  { 1, 2, 2, 2, 2, 1, 2, 1, 1, 1 },
361  { 2, 3, 3, 3, 3, 2, 2, 2, 2, 2 },
362  { 0, 5, 3, 3, 2, 2, 2, 2, 1, 1 },
363  { 0, 2, 2, 2, 2, 2, 2, 2, 2, 1 },
364  { 2, 3, 6, 4, 4, 4, 5, 4, 5, 5 },
365  { 2, 4, 5, 5, 4, 5, 4, 6, 4, 4 },
366  { 2, 4, 4, 7, 4, 5, 4, 5, 5, 4 },
367  { 4, 3, 3, 3, 2, 3, 2, 2, 2, 2 },
368  { 1, 5, 5, 6, 4, 5, 4, 5, 5, 5 },
369  { 2, 7, 4, 6, 5, 5, 5, 5, 5, 5 },
370  { 2, 7, 5, 5, 5, 5, 5, 6, 5, 4 },
371  { 3, 3, 5, 4, 4, 5, 4, 5, 4, 4 },
372  { 2, 3, 3, 5, 5, 4, 4, 4, 4, 4 },
373  { 2, 4, 4, 6, 4, 5, 4, 5, 5, 5 },
374  { 2, 5, 4, 6, 5, 5, 5, 4, 5, 4 },
375  { 2, 7, 4, 5, 4, 5, 4, 5, 5, 5 },
376  { 2, 5, 4, 6, 7, 6, 5, 6, 5, 4 },
377  { 3, 6, 7, 4, 6, 5, 5, 6, 4, 5 },
378  { 2, 7, 6, 4, 4, 4, 5, 4, 5, 5 },
379  { 4, 5, 5, 4, 6, 6, 5, 6, 5, 4 },
380  { 2, 5, 5, 6, 5, 6, 4, 6, 4, 4 },
381  { 4, 5, 5, 5, 3, 7, 4, 5, 5, 4 },
382  { 2, 3, 4, 5, 5, 6, 4, 5, 5, 4 },
383  { 2, 3, 2, 3, 3, 4, 2, 3, 3, 3 },
384  { 1, 1, 2, 2, 2, 2, 2, 3, 2, 2 },
385  { 4, 5, 5, 6, 6, 6, 5, 6, 4, 5 },
386  { 3, 5, 5, 4, 4, 4, 4, 3, 3, 2 },
387  { 2, 5, 3, 7, 5, 5, 4, 4, 5, 4 },
388  { 4, 4, 5, 4, 5, 6, 5, 6, 5, 4 }
389 };
390 
391 static const uint8_t silk_lsf_s2_model_sel_wb[32][16] = {
392  { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 },
393  { 10, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 9, 9, 9, 8, 11 },
394  { 10, 13, 13, 11, 15, 12, 12, 13, 10, 13, 12, 13, 13, 12, 11, 11 },
395  { 8, 10, 9, 10, 10, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 9 },
396  { 8, 14, 13, 12, 14, 12, 15, 13, 12, 12, 12, 13, 13, 12, 12, 11 },
397  { 8, 11, 13, 13, 12, 11, 11, 13, 11, 11, 11, 11, 11, 11, 10, 12 },
398  { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 },
399  { 8, 10, 14, 11, 15, 10, 13, 11, 12, 13, 13, 12, 11, 11, 10, 11 },
400  { 8, 14, 10, 14, 14, 12, 13, 12, 14, 13, 12, 12, 13, 11, 11, 11 },
401  { 10, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 },
402  { 8, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9 },
403  { 10, 10, 11, 12, 13, 11, 11, 11, 11, 11, 11, 11, 10, 10, 9, 11 },
404  { 10, 10, 11, 11, 12, 11, 11, 11, 11, 11, 11, 11, 11, 10, 9, 11 },
405  { 11, 12, 12, 12, 14, 12, 12, 13, 11, 13, 12, 12, 13, 12, 11, 12 },
406  { 8, 14, 12, 13, 12, 15, 13, 10, 14, 13, 15, 12, 12, 11, 13, 11 },
407  { 8, 9, 8, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 9, 8 },
408  { 9, 14, 13, 15, 13, 12, 13, 11, 12, 13, 12, 12, 12, 11, 11, 12 },
409  { 9, 11, 11, 12, 12, 11, 11, 13, 10, 11, 11, 13, 13, 13, 11, 12 },
410  { 10, 11, 11, 10, 10, 10, 11, 10, 9, 10, 9, 10, 9, 9, 9, 12 },
411  { 8, 10, 11, 13, 11, 11, 10, 10, 10, 9, 9, 8, 8, 8, 8, 8 },
412  { 11, 12, 11, 13, 11, 11, 10, 10, 9, 9, 9, 9, 9, 10, 10, 12 },
413  { 10, 14, 11, 15, 15, 12, 13, 12, 13, 11, 13, 11, 11, 10, 11, 11 },
414  { 10, 11, 13, 14, 14, 11, 13, 11, 12, 12, 11, 11, 11, 11, 10, 12 },
415  { 9, 11, 11, 12, 12, 12, 12, 11, 13, 13, 13, 11, 9, 9, 9, 9 },
416  { 10, 13, 11, 14, 14, 12, 15, 12, 12, 13, 11, 12, 12, 11, 11, 11 },
417  { 8, 14, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 },
418  { 8, 14, 14, 11, 13, 10, 13, 13, 11, 12, 12, 15, 15, 12, 12, 12 },
419  { 11, 11, 15, 11, 13, 12, 11, 11, 11, 10, 10, 11, 11, 11, 10, 11 },
420  { 8, 8, 9, 8, 8, 8, 10, 9, 10, 9, 9, 10, 10, 10, 9, 9 },
421  { 8, 11, 10, 13, 11, 11, 10, 11, 10, 9, 8, 8, 9, 8, 8, 9 },
422  { 11, 13, 13, 12, 15, 13, 11, 11, 10, 11, 10, 10, 9, 8, 9, 8 },
423  { 10, 11, 13, 11, 12, 11, 11, 11, 10, 9, 10, 14, 12, 8, 8, 8 }
424 };
425 
426 static const uint8_t silk_lsf_pred_weights_nbmb[2][9] = {
427  {179, 138, 140, 148, 151, 149, 153, 151, 163},
428  {116, 67, 82, 59, 92, 72, 100, 89, 92}
429 };
430 
431 static const uint8_t silk_lsf_pred_weights_wb[2][15] = {
432  {175, 148, 160, 176, 178, 173, 174, 164, 177, 174, 196, 182, 198, 192, 182},
433  { 68, 62, 66, 60, 72, 117, 85, 90, 118, 136, 151, 142, 160, 142, 155}
434 };
435 
436 static const uint8_t silk_lsf_weight_sel_nbmb[32][9] = {
437  { 0, 1, 0, 0, 0, 0, 0, 0, 0 },
438  { 1, 0, 0, 0, 0, 0, 0, 0, 0 },
439  { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
440  { 1, 1, 1, 0, 0, 0, 0, 1, 0 },
441  { 0, 1, 0, 0, 0, 0, 0, 0, 0 },
442  { 0, 1, 0, 0, 0, 0, 0, 0, 0 },
443  { 1, 0, 1, 1, 0, 0, 0, 1, 0 },
444  { 0, 1, 1, 0, 0, 1, 1, 0, 0 },
445  { 0, 0, 1, 1, 0, 1, 0, 1, 1 },
446  { 0, 0, 1, 1, 0, 0, 1, 1, 1 },
447  { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
448  { 0, 1, 0, 1, 1, 1, 1, 1, 0 },
449  { 0, 1, 0, 1, 1, 1, 1, 1, 0 },
450  { 0, 1, 1, 1, 1, 1, 1, 1, 0 },
451  { 1, 0, 1, 1, 0, 1, 1, 1, 1 },
452  { 0, 1, 1, 1, 1, 1, 0, 1, 0 },
453  { 0, 0, 1, 1, 0, 1, 0, 1, 0 },
454  { 0, 0, 1, 1, 1, 0, 1, 1, 1 },
455  { 0, 1, 1, 0, 0, 1, 1, 1, 0 },
456  { 0, 0, 0, 1, 1, 1, 0, 1, 0 },
457  { 0, 1, 1, 0, 0, 1, 0, 1, 0 },
458  { 0, 1, 1, 0, 0, 0, 1, 1, 0 },
459  { 0, 0, 0, 0, 0, 1, 1, 1, 1 },
460  { 0, 0, 1, 1, 0, 0, 0, 1, 1 },
461  { 0, 0, 0, 1, 0, 1, 1, 1, 1 },
462  { 0, 1, 1, 1, 1, 1, 1, 1, 0 },
463  { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
464  { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
465  { 0, 0, 1, 0, 1, 1, 0, 1, 0 },
466  { 1, 0, 0, 1, 0, 0, 0, 0, 0 },
467  { 0, 0, 0, 1, 1, 0, 1, 0, 1 },
468  { 1, 0, 1, 1, 0, 1, 1, 1, 1 }
469 };
470 
471 static const uint8_t silk_lsf_weight_sel_wb[32][15] = {
472  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
473  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
474  { 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0 },
475  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
476  { 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0 },
477  { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
478  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0 },
479  { 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1 },
480  { 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1 },
481  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
482  { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
483  { 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0 },
484  { 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 },
485  { 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0 },
486  { 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1 },
487  { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 },
488  { 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0 },
489  { 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0 },
490  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
491  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
492  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
493  { 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0 },
494  { 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0 },
495  { 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0 },
496  { 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1 },
497  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
498  { 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1 },
499  { 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
500  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
501  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
502  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
503  { 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0 }
504 };
505 
506 static const uint8_t silk_lsf_codebook_nbmb[32][10] = {
507  { 12, 35, 60, 83, 108, 132, 157, 180, 206, 228 },
508  { 15, 32, 55, 77, 101, 125, 151, 175, 201, 225 },
509  { 19, 42, 66, 89, 114, 137, 162, 184, 209, 230 },
510  { 12, 25, 50, 72, 97, 120, 147, 172, 200, 223 },
511  { 26, 44, 69, 90, 114, 135, 159, 180, 205, 225 },
512  { 13, 22, 53, 80, 106, 130, 156, 180, 205, 228 },
513  { 15, 25, 44, 64, 90, 115, 142, 168, 196, 222 },
514  { 19, 24, 62, 82, 100, 120, 145, 168, 190, 214 },
515  { 22, 31, 50, 79, 103, 120, 151, 170, 203, 227 },
516  { 21, 29, 45, 65, 106, 124, 150, 171, 196, 224 },
517  { 30, 49, 75, 97, 121, 142, 165, 186, 209, 229 },
518  { 19, 25, 52, 70, 93, 116, 143, 166, 192, 219 },
519  { 26, 34, 62, 75, 97, 118, 145, 167, 194, 217 },
520  { 25, 33, 56, 70, 91, 113, 143, 165, 196, 223 },
521  { 21, 34, 51, 72, 97, 117, 145, 171, 196, 222 },
522  { 20, 29, 50, 67, 90, 117, 144, 168, 197, 221 },
523  { 22, 31, 48, 66, 95, 117, 146, 168, 196, 222 },
524  { 24, 33, 51, 77, 116, 134, 158, 180, 200, 224 },
525  { 21, 28, 70, 87, 106, 124, 149, 170, 194, 217 },
526  { 26, 33, 53, 64, 83, 117, 152, 173, 204, 225 },
527  { 27, 34, 65, 95, 108, 129, 155, 174, 210, 225 },
528  { 20, 26, 72, 99, 113, 131, 154, 176, 200, 219 },
529  { 34, 43, 61, 78, 93, 114, 155, 177, 205, 229 },
530  { 23, 29, 54, 97, 124, 138, 163, 179, 209, 229 },
531  { 30, 38, 56, 89, 118, 129, 158, 178, 200, 231 },
532  { 21, 29, 49, 63, 85, 111, 142, 163, 193, 222 },
533  { 27, 48, 77, 103, 133, 158, 179, 196, 215, 232 },
534  { 29, 47, 74, 99, 124, 151, 176, 198, 220, 237 },
535  { 33, 42, 61, 76, 93, 121, 155, 174, 207, 225 },
536  { 29, 53, 87, 112, 136, 154, 170, 188, 208, 227 },
537  { 24, 30, 52, 84, 131, 150, 166, 186, 203, 229 },
538  { 37, 48, 64, 84, 104, 118, 156, 177, 201, 230 }
539 };
540 
541 static const uint8_t silk_lsf_codebook_wb[32][16] = {
542  { 7, 23, 38, 54, 69, 85, 100, 116, 131, 147, 162, 178, 193, 208, 223, 239 },
543  { 13, 25, 41, 55, 69, 83, 98, 112, 127, 142, 157, 171, 187, 203, 220, 236 },
544  { 15, 21, 34, 51, 61, 78, 92, 106, 126, 136, 152, 167, 185, 205, 225, 240 },
545  { 10, 21, 36, 50, 63, 79, 95, 110, 126, 141, 157, 173, 189, 205, 221, 237 },
546  { 17, 20, 37, 51, 59, 78, 89, 107, 123, 134, 150, 164, 184, 205, 224, 240 },
547  { 10, 15, 32, 51, 67, 81, 96, 112, 129, 142, 158, 173, 189, 204, 220, 236 },
548  { 8, 21, 37, 51, 65, 79, 98, 113, 126, 138, 155, 168, 179, 192, 209, 218 },
549  { 12, 15, 34, 55, 63, 78, 87, 108, 118, 131, 148, 167, 185, 203, 219, 236 },
550  { 16, 19, 32, 36, 56, 79, 91, 108, 118, 136, 154, 171, 186, 204, 220, 237 },
551  { 11, 28, 43, 58, 74, 89, 105, 120, 135, 150, 165, 180, 196, 211, 226, 241 },
552  { 6, 16, 33, 46, 60, 75, 92, 107, 123, 137, 156, 169, 185, 199, 214, 225 },
553  { 11, 19, 30, 44, 57, 74, 89, 105, 121, 135, 152, 169, 186, 202, 218, 234 },
554  { 12, 19, 29, 46, 57, 71, 88, 100, 120, 132, 148, 165, 182, 199, 216, 233 },
555  { 17, 23, 35, 46, 56, 77, 92, 106, 123, 134, 152, 167, 185, 204, 222, 237 },
556  { 14, 17, 45, 53, 63, 75, 89, 107, 115, 132, 151, 171, 188, 206, 221, 240 },
557  { 9, 16, 29, 40, 56, 71, 88, 103, 119, 137, 154, 171, 189, 205, 222, 237 },
558  { 16, 19, 36, 48, 57, 76, 87, 105, 118, 132, 150, 167, 185, 202, 218, 236 },
559  { 12, 17, 29, 54, 71, 81, 94, 104, 126, 136, 149, 164, 182, 201, 221, 237 },
560  { 15, 28, 47, 62, 79, 97, 115, 129, 142, 155, 168, 180, 194, 208, 223, 238 },
561  { 8, 14, 30, 45, 62, 78, 94, 111, 127, 143, 159, 175, 192, 207, 223, 239 },
562  { 17, 30, 49, 62, 79, 92, 107, 119, 132, 145, 160, 174, 190, 204, 220, 235 },
563  { 14, 19, 36, 45, 61, 76, 91, 108, 121, 138, 154, 172, 189, 205, 222, 238 },
564  { 12, 18, 31, 45, 60, 76, 91, 107, 123, 138, 154, 171, 187, 204, 221, 236 },
565  { 13, 17, 31, 43, 53, 70, 83, 103, 114, 131, 149, 167, 185, 203, 220, 237 },
566  { 17, 22, 35, 42, 58, 78, 93, 110, 125, 139, 155, 170, 188, 206, 224, 240 },
567  { 8, 15, 34, 50, 67, 83, 99, 115, 131, 146, 162, 178, 193, 209, 224, 239 },
568  { 13, 16, 41, 66, 73, 86, 95, 111, 128, 137, 150, 163, 183, 206, 225, 241 },
569  { 17, 25, 37, 52, 63, 75, 92, 102, 119, 132, 144, 160, 175, 191, 212, 231 },
570  { 19, 31, 49, 65, 83, 100, 117, 133, 147, 161, 174, 187, 200, 213, 227, 242 },
571  { 18, 31, 52, 68, 88, 103, 117, 126, 138, 149, 163, 177, 192, 207, 223, 239 },
572  { 16, 29, 47, 61, 76, 90, 106, 119, 133, 147, 161, 176, 193, 209, 224, 240 },
573  { 15, 21, 35, 50, 61, 73, 86, 97, 110, 119, 129, 141, 175, 198, 218, 237 }
574 };
575 
576 static const uint16_t silk_lsf_min_spacing_nbmb[] = {
577  250, 3, 6, 3, 3, 3, 4, 3, 3, 3, 461
578 };
579 
580 static const uint16_t silk_lsf_min_spacing_wb[] = {
581  100, 3, 40, 3, 3, 3, 5, 14, 14, 10, 11, 3, 8, 9, 7, 3, 347
582 };
583 
584 static const uint8_t silk_lsf_ordering_nbmb[] = {
585  0, 9, 6, 3, 4, 5, 8, 1, 2, 7
586 };
587 
588 static const uint8_t silk_lsf_ordering_wb[] = {
589  0, 15, 8, 7, 4, 11, 12, 3, 2, 13, 10, 5, 6, 9, 14, 1
590 };
591 
592 static const int16_t silk_cosine[] = { /* (0.12) */
593  4096, 4095, 4091, 4085,
594  4076, 4065, 4052, 4036,
595  4017, 3997, 3973, 3948,
596  3920, 3889, 3857, 3822,
597  3784, 3745, 3703, 3659,
598  3613, 3564, 3513, 3461,
599  3406, 3349, 3290, 3229,
600  3166, 3102, 3035, 2967,
601  2896, 2824, 2751, 2676,
602  2599, 2520, 2440, 2359,
603  2276, 2191, 2106, 2019,
604  1931, 1842, 1751, 1660,
605  1568, 1474, 1380, 1285,
606  1189, 1093, 995, 897,
607  799, 700, 601, 501,
608  401, 301, 201, 101,
609  0, -101, -201, -301,
610  -401, -501, -601, -700,
611  -799, -897, -995, -1093,
612  -1189, -1285, -1380, -1474,
613  -1568, -1660, -1751, -1842,
614  -1931, -2019, -2106, -2191,
615  -2276, -2359, -2440, -2520,
616  -2599, -2676, -2751, -2824,
617  -2896, -2967, -3035, -3102,
618  -3166, -3229, -3290, -3349,
619  -3406, -3461, -3513, -3564,
620  -3613, -3659, -3703, -3745,
621  -3784, -3822, -3857, -3889,
622  -3920, -3948, -3973, -3997,
623  -4017, -4036, -4052, -4065,
624  -4076, -4085, -4091, -4095,
625  -4096
626 };
627 
628 static const uint16_t silk_pitch_scale[] = { 4, 6, 8};
629 
630 static const uint16_t silk_pitch_min_lag[] = { 16, 24, 32};
631 
632 static const uint16_t silk_pitch_max_lag[] = {144, 216, 288};
633 
634 static const int8_t silk_pitch_offset_nb10ms[3][2] = {
635  { 0, 0},
636  { 1, 0},
637  { 0, 1}
638 };
639 
640 static const int8_t silk_pitch_offset_nb20ms[11][4] = {
641  { 0, 0, 0, 0},
642  { 2, 1, 0, -1},
643  {-1, 0, 1, 2},
644  {-1, 0, 0, 1},
645  {-1, 0, 0, 0},
646  { 0, 0, 0, 1},
647  { 0, 0, 1, 1},
648  { 1, 1, 0, 0},
649  { 1, 0, 0, 0},
650  { 0, 0, 0, -1},
651  { 1, 0, 0, -1}
652 };
653 
654 static const int8_t silk_pitch_offset_mbwb10ms[12][2] = {
655  { 0, 0},
656  { 0, 1},
657  { 1, 0},
658  {-1, 1},
659  { 1, -1},
660  {-1, 2},
661  { 2, -1},
662  {-2, 2},
663  { 2, -2},
664  {-2, 3},
665  { 3, -2},
666  {-3, 3}
667 };
668 
669 static const int8_t silk_pitch_offset_mbwb20ms[34][4] = {
670  { 0, 0, 0, 0},
671  { 0, 0, 1, 1},
672  { 1, 1, 0, 0},
673  {-1, 0, 0, 0},
674  { 0, 0, 0, 1},
675  { 1, 0, 0, 0},
676  {-1, 0, 0, 1},
677  { 0, 0, 0, -1},
678  {-1, 0, 1, 2},
679  { 1, 0, 0, -1},
680  {-2, -1, 1, 2},
681  { 2, 1, 0, -1},
682  {-2, 0, 0, 2},
683  {-2, 0, 1, 3},
684  { 2, 1, -1, -2},
685  {-3, -1, 1, 3},
686  { 2, 0, 0, -2},
687  { 3, 1, 0, -2},
688  {-3, -1, 2, 4},
689  {-4, -1, 1, 4},
690  { 3, 1, -1, -3},
691  {-4, -1, 2, 5},
692  { 4, 2, -1, -3},
693  { 4, 1, -1, -4},
694  {-5, -1, 2, 6},
695  { 5, 2, -1, -4},
696  {-6, -2, 2, 6},
697  {-5, -2, 2, 5},
698  { 6, 2, -1, -5},
699  {-7, -2, 3, 8},
700  { 6, 2, -2, -6},
701  { 5, 2, -2, -5},
702  { 8, 3, -2, -7},
703  {-9, -3, 3, 9}
704 };
705 
706 static const int8_t silk_ltp_filter0_taps[8][5] = {
707  { 4, 6, 24, 7, 5},
708  { 0, 0, 2, 0, 0},
709  { 12, 28, 41, 13, -4},
710  { -9, 15, 42, 25, 14},
711  { 1, -2, 62, 41, -9},
712  {-10, 37, 65, -4, 3},
713  { -6, 4, 66, 7, -8},
714  { 16, 14, 38, -3, 33}
715 };
716 
717 static const int8_t silk_ltp_filter1_taps[16][5] = {
718  { 13, 22, 39, 23, 12},
719  { -1, 36, 64, 27, -6},
720  { -7, 10, 55, 43, 17},
721  { 1, 1, 8, 1, 1},
722  { 6, -11, 74, 53, -9},
723  {-12, 55, 76, -12, 8},
724  { -3, 3, 93, 27, -4},
725  { 26, 39, 59, 3, -8},
726  { 2, 0, 77, 11, 9},
727  { -8, 22, 44, -6, 7},
728  { 40, 9, 26, 3, 9},
729  { -7, 20, 101, -7, 4},
730  { 3, -8, 42, 26, 0},
731  {-15, 33, 68, 2, 23},
732  { -2, 55, 46, -2, 15},
733  { 3, -1, 21, 16, 41}
734 };
735 
736 static const int8_t silk_ltp_filter2_taps[32][5] = {
737  { -6, 27, 61, 39, 5},
738  {-11, 42, 88, 4, 1},
739  { -2, 60, 65, 6, -4},
740  { -1, -5, 73, 56, 1},
741  { -9, 19, 94, 29, -9},
742  { 0, 12, 99, 6, 4},
743  { 8, -19, 102, 46, -13},
744  { 3, 2, 13, 3, 2},
745  { 9, -21, 84, 72, -18},
746  {-11, 46, 104, -22, 8},
747  { 18, 38, 48, 23, 0},
748  {-16, 70, 83, -21, 11},
749  { 5, -11, 117, 22, -8},
750  { -6, 23, 117, -12, 3},
751  { 3, -8, 95, 28, 4},
752  {-10, 15, 77, 60, -15},
753  { -1, 4, 124, 2, -4},
754  { 3, 38, 84, 24, -25},
755  { 2, 13, 42, 13, 31},
756  { 21, -4, 56, 46, -1},
757  { -1, 35, 79, -13, 19},
758  { -7, 65, 88, -9, -14},
759  { 20, 4, 81, 49, -29},
760  { 20, 0, 75, 3, -17},
761  { 5, -9, 44, 92, -8},
762  { 1, -3, 22, 69, 31},
763  { -6, 95, 41, -12, 5},
764  { 39, 67, 16, -4, 1},
765  { 0, -6, 120, 55, -36},
766  {-13, 44, 122, 4, -24},
767  { 81, 5, 11, 3, 7},
768  { 2, 0, 9, 10, 88}
769 };
770 
771 static const uint16_t silk_ltp_scale_factor[] = {15565, 12288, 8192};
772 
773 static const uint8_t silk_shell_blocks[3][2] = {
774  { 5, 10}, // NB
775  { 8, 15}, // MB
776  {10, 20} // WB
777 };
778 
779 static const uint8_t silk_quant_offset[2][2] = { /* (0.23) */
780  {25, 60}, // Inactive or Unvoiced
781  { 8, 25} // Voiced
782 };
783 
784 static const int silk_stereo_interp_len[3] = {
785  64, 96, 128
786 };
787 
788 static inline void silk_stabilize_lsf(int16_t nlsf[16], int order, const uint16_t min_delta[17])
789 {
790  int pass, i;
791  for (pass = 0; pass < 20; pass++) {
792  int k, min_diff = 0;
793  for (i = 0; i < order+1; i++) {
794  int low = i != 0 ? nlsf[i-1] : 0;
795  int high = i != order ? nlsf[i] : 32768;
796  int diff = (high - low) - (min_delta[i]);
797 
798  if (diff < min_diff) {
799  min_diff = diff;
800  k = i;
801 
802  if (pass == 20)
803  break;
804  }
805  }
806  if (min_diff == 0) /* no issues; stabilized */
807  return;
808 
809  /* wiggle one or two LSFs */
810  if (k == 0) {
811  /* repel away from lower bound */
812  nlsf[0] = min_delta[0];
813  } else if (k == order) {
814  /* repel away from higher bound */
815  nlsf[order-1] = 32768 - min_delta[order];
816  } else {
817  /* repel away from current position */
818  int min_center = 0, max_center = 32768, center_val;
819 
820  /* lower extent */
821  for (i = 0; i < k; i++)
822  min_center += min_delta[i];
823  min_center += min_delta[k] >> 1;
824 
825  /* upper extent */
826  for (i = order; i > k; i--)
827  max_center -= min_delta[i];
828  max_center -= min_delta[k] >> 1;
829 
830  /* move apart */
831  center_val = nlsf[k - 1] + nlsf[k];
832  center_val = (center_val >> 1) + (center_val & 1); // rounded divide by 2
833  center_val = FFMIN(max_center, FFMAX(min_center, center_val));
834 
835  nlsf[k - 1] = center_val - (min_delta[k] >> 1);
836  nlsf[k] = nlsf[k - 1] + min_delta[k];
837  }
838  }
839 
840  /* resort to the fall-back method, the standard method for LSF stabilization */
841 
842  /* sort; as the LSFs should be nearly sorted, use insertion sort */
843  for (i = 1; i < order; i++) {
844  int j, value = nlsf[i];
845  for (j = i - 1; j >= 0 && nlsf[j] > value; j--)
846  nlsf[j + 1] = nlsf[j];
847  nlsf[j + 1] = value;
848  }
849 
850  /* push forwards to increase distance */
851  if (nlsf[0] < min_delta[0])
852  nlsf[0] = min_delta[0];
853  for (i = 1; i < order; i++)
854  nlsf[i] = FFMAX(nlsf[i], FFMIN(nlsf[i - 1] + min_delta[i], 32767));
855 
856  /* push backwards to increase distance */
857  if (nlsf[order-1] > 32768 - min_delta[order])
858  nlsf[order-1] = 32768 - min_delta[order];
859  for (i = order-2; i >= 0; i--)
860  if (nlsf[i] > nlsf[i + 1] - min_delta[i+1])
861  nlsf[i] = nlsf[i + 1] - min_delta[i+1];
862 
863  return;
864 }
865 
866 static inline int silk_is_lpc_stable(const int16_t lpc[16], int order)
867 {
868  int k, j, DC_resp = 0;
869  int32_t lpc32[2][16]; // Q24
870  int totalinvgain = 1 << 30; // 1.0 in Q30
871  int32_t *row = lpc32[0], *prevrow;
872 
873  /* initialize the first row for the Levinson recursion */
874  for (k = 0; k < order; k++) {
875  DC_resp += lpc[k];
876  row[k] = lpc[k] * 4096;
877  }
878 
879  if (DC_resp >= 4096)
880  return 0;
881 
882  /* check if prediction gain pushes any coefficients too far */
883  for (k = order - 1; 1; k--) {
884  int rc; // Q31; reflection coefficient
885  int gaindiv; // Q30; inverse of the gain (the divisor)
886  int gain; // gain for this reflection coefficient
887  int fbits; // fractional bits used for the gain
888  int error; // Q29; estimate of the error of our partial estimate of 1/gaindiv
889 
890  if (FFABS(row[k]) > 16773022)
891  return 0;
892 
893  rc = -(row[k] * 128);
894  gaindiv = (1 << 30) - MULH(rc, rc);
895 
896  totalinvgain = MULH(totalinvgain, gaindiv) << 2;
897  if (k == 0)
898  return (totalinvgain >= 107374);
899 
900  /* approximate 1.0/gaindiv */
901  fbits = opus_ilog(gaindiv);
902  gain = ((1 << 29) - 1) / (gaindiv >> (fbits + 1 - 16)); // Q<fbits-16>
903  error = (1 << 29) - MULL(gaindiv << (15 + 16 - fbits), gain, 16);
904  gain = ((gain << 16) + (error * gain >> 13));
905 
906  /* switch to the next row of the LPC coefficients */
907  prevrow = row;
908  row = lpc32[k & 1];
909 
910  for (j = 0; j < k; j++) {
911  int x = prevrow[j] - ROUND_MULL(prevrow[k - j - 1], rc, 31);
912  row[j] = ROUND_MULL(x, gain, fbits);
913  }
914  }
915 }
916 
917 static void silk_lsp2poly(const int32_t lsp[16], int32_t pol[16], int half_order)
918 {
919  int i, j;
920 
921  pol[0] = 65536; // 1.0 in Q16
922  pol[1] = -lsp[0];
923 
924  for (i = 1; i < half_order; i++) {
925  pol[i + 1] = pol[i - 1] * 2 - ROUND_MULL(lsp[2 * i], pol[i], 16);
926  for (j = i; j > 1; j--)
927  pol[j] += pol[j - 2] - ROUND_MULL(lsp[2 * i], pol[j - 1], 16);
928 
929  pol[1] -= lsp[2 * i];
930  }
931 }
932 
933 static void silk_lsf2lpc(const int16_t nlsf[16], float lpcf[16], int order)
934 {
935  int i, k;
936  int32_t lsp[16]; // Q17; 2*cos(LSF)
937  int32_t p[9], q[9]; // Q16
938  int32_t lpc32[16]; // Q17
939  int16_t lpc[16]; // Q12
940 
941  /* convert the LSFs to LSPs, i.e. 2*cos(LSF) */
942  for (k = 0; k < order; k++) {
943  int index = nlsf[k] >> 8;
944  int offset = nlsf[k] & 255;
945  int k2 = (order == 10) ? silk_lsf_ordering_nbmb[k] : silk_lsf_ordering_wb[k];
946 
947  /* interpolate and round */
948  lsp[k2] = silk_cosine[index] * 256;
949  lsp[k2] += (silk_cosine[index + 1] - silk_cosine[index]) * offset;
950  lsp[k2] = (lsp[k2] + 4) >> 3;
951  }
952 
953  silk_lsp2poly(lsp , p, order >> 1);
954  silk_lsp2poly(lsp + 1, q, order >> 1);
955 
956  /* reconstruct A(z) */
957  for (k = 0; k < order>>1; k++) {
958  int32_t p_tmp = p[k + 1] + p[k];
959  int32_t q_tmp = q[k + 1] - q[k];
960  lpc32[k] = -q_tmp - p_tmp;
961  lpc32[order-k-1] = q_tmp - p_tmp;
962  }
963 
964  /* limit the range of the LPC coefficients to each fit within an int16_t */
965  for (i = 0; i < 10; i++) {
966  int j;
967  unsigned int maxabs = 0;
968  for (j = 0, k = 0; j < order; j++) {
969  unsigned int x = FFABS(lpc32[k]);
970  if (x > maxabs) {
971  maxabs = x; // Q17
972  k = j;
973  }
974  }
975 
976  maxabs = (maxabs + 16) >> 5; // convert to Q12
977 
978  if (maxabs > 32767) {
979  /* perform bandwidth expansion */
980  unsigned int chirp, chirp_base; // Q16
981  maxabs = FFMIN(maxabs, 163838); // anything above this overflows chirp's numerator
982  chirp_base = chirp = 65470 - ((maxabs - 32767) << 14) / ((maxabs * (k+1)) >> 2);
983 
984  for (k = 0; k < order; k++) {
985  lpc32[k] = ROUND_MULL(lpc32[k], chirp, 16);
986  chirp = (chirp_base * chirp + 32768) >> 16;
987  }
988  } else break;
989  }
990 
991  if (i == 10) {
992  /* time's up: just clamp */
993  for (k = 0; k < order; k++) {
994  int x = (lpc32[k] + 16) >> 5;
995  lpc[k] = av_clip_int16(x);
996  lpc32[k] = lpc[k] << 5; // shortcut mandated by the spec; drops lower 5 bits
997  }
998  } else {
999  for (k = 0; k < order; k++)
1000  lpc[k] = (lpc32[k] + 16) >> 5;
1001  }
1002 
1003  /* if the prediction gain causes the LPC filter to become unstable,
1004  apply further bandwidth expansion on the Q17 coefficients */
1005  for (i = 1; i <= 16 && !silk_is_lpc_stable(lpc, order); i++) {
1006  unsigned int chirp, chirp_base;
1007  chirp_base = chirp = 65536 - (1 << i);
1008 
1009  for (k = 0; k < order; k++) {
1010  lpc32[k] = ROUND_MULL(lpc32[k], chirp, 16);
1011  lpc[k] = (lpc32[k] + 16) >> 5;
1012  chirp = (chirp_base * chirp + 32768) >> 16;
1013  }
1014  }
1015 
1016  for (i = 0; i < order; i++)
1017  lpcf[i] = lpc[i] / 4096.0f;
1018 }
1019 
1021  OpusRangeCoder *rc,
1022  float lpc_leadin[16], float lpc[16],
1023  int *lpc_order, int *has_lpc_leadin, int voiced)
1024 {
1025  int i;
1026  int order; // order of the LP polynomial; 10 for NB/MB and 16 for WB
1027  int8_t lsf_i1, lsf_i2[16]; // stage-1 and stage-2 codebook indices
1028  int16_t lsf_res[16]; // residual as a Q10 value
1029  int16_t nlsf[16]; // Q15
1030 
1031  *lpc_order = order = s->wb ? 16 : 10;
1032 
1033  /* obtain LSF stage-1 and stage-2 indices */
1034  lsf_i1 = opus_rc_getsymbol(rc, silk_model_lsf_s1[s->wb][voiced]);
1035  for (i = 0; i < order; i++) {
1036  int index = s->wb ? silk_lsf_s2_model_sel_wb [lsf_i1][i] :
1037  silk_lsf_s2_model_sel_nbmb[lsf_i1][i];
1038  lsf_i2[i] = opus_rc_getsymbol(rc, silk_model_lsf_s2[index]) - 4;
1039  if (lsf_i2[i] == -4)
1040  lsf_i2[i] -= opus_rc_getsymbol(rc, silk_model_lsf_s2_ext);
1041  else if (lsf_i2[i] == 4)
1042  lsf_i2[i] += opus_rc_getsymbol(rc, silk_model_lsf_s2_ext);
1043  }
1044 
1045  /* reverse the backwards-prediction step */
1046  for (i = order - 1; i >= 0; i--) {
1047  int qstep = s->wb ? 9830 : 11796;
1048 
1049  lsf_res[i] = lsf_i2[i] * 1024;
1050  if (lsf_i2[i] < 0) lsf_res[i] += 102;
1051  else if (lsf_i2[i] > 0) lsf_res[i] -= 102;
1052  lsf_res[i] = (lsf_res[i] * qstep) >> 16;
1053 
1054  if (i + 1 < order) {
1055  int weight = s->wb ? silk_lsf_pred_weights_wb [silk_lsf_weight_sel_wb [lsf_i1][i]][i] :
1057  lsf_res[i] += (lsf_res[i+1] * weight) >> 8;
1058  }
1059  }
1060 
1061  /* reconstruct the NLSF coefficients from the supplied indices */
1062  for (i = 0; i < order; i++) {
1063  const uint8_t * codebook = s->wb ? silk_lsf_codebook_wb [lsf_i1] :
1064  silk_lsf_codebook_nbmb[lsf_i1];
1065  int cur, prev, next, weight_sq, weight, ipart, fpart, y, value;
1066 
1067  /* find the weight of the residual */
1068  /* TODO: precompute */
1069  cur = codebook[i];
1070  prev = i ? codebook[i - 1] : 0;
1071  next = i + 1 < order ? codebook[i + 1] : 256;
1072  weight_sq = (1024 / (cur - prev) + 1024 / (next - cur)) << 16;
1073 
1074  /* approximate square-root with mandated fixed-point arithmetic */
1075  ipart = opus_ilog(weight_sq);
1076  fpart = (weight_sq >> (ipart-8)) & 127;
1077  y = ((ipart & 1) ? 32768 : 46214) >> ((32 - ipart)>>1);
1078  weight = y + ((213 * fpart * y) >> 16);
1079 
1080  value = cur * 128 + (lsf_res[i] * 16384) / weight;
1081  nlsf[i] = av_clip_uintp2(value, 15);
1082  }
1083 
1084  /* stabilize the NLSF coefficients */
1085  silk_stabilize_lsf(nlsf, order, s->wb ? silk_lsf_min_spacing_wb :
1087 
1088  /* produce an interpolation for the first 2 subframes, */
1089  /* and then convert both sets of NLSFs to LPC coefficients */
1090  *has_lpc_leadin = 0;
1091  if (s->subframes == 4) {
1093  if (offset != 4 && frame->coded) {
1094  *has_lpc_leadin = 1;
1095  if (offset != 0) {
1096  int16_t nlsf_leadin[16];
1097  for (i = 0; i < order; i++)
1098  nlsf_leadin[i] = frame->nlsf[i] +
1099  ((nlsf[i] - frame->nlsf[i]) * offset >> 2);
1100  silk_lsf2lpc(nlsf_leadin, lpc_leadin, order);
1101  } else /* avoid re-computation for a (roughly) 1-in-4 occurrence */
1102  memcpy(lpc_leadin, frame->lpc, 16 * sizeof(float));
1103  } else
1104  offset = 4;
1106 
1107  silk_lsf2lpc(nlsf, lpc, order);
1108  } else {
1109  s->nlsf_interp_factor = 4;
1110  silk_lsf2lpc(nlsf, lpc, order);
1111  }
1112 
1113  memcpy(frame->nlsf, nlsf, order * sizeof(nlsf[0]));
1114  memcpy(frame->lpc, lpc, order * sizeof(lpc[0]));
1115 }
1116 
1117 static inline void silk_count_children(OpusRangeCoder *rc, int model, int32_t total,
1118  int32_t child[2])
1119 {
1120  if (total != 0) {
1121  child[0] = opus_rc_getsymbol(rc,
1122  silk_model_pulse_location[model] + (((total - 1 + 5) * (total - 1)) >> 1));
1123  child[1] = total - child[0];
1124  } else {
1125  child[0] = 0;
1126  child[1] = 0;
1127  }
1128 }
1129 
1131  float* excitationf,
1132  int qoffset_high, int active, int voiced)
1133 {
1134  int i;
1135  uint32_t seed;
1136  int shellblocks;
1137  int ratelevel;
1138  uint8_t pulsecount[20]; // total pulses in each shell block
1139  uint8_t lsbcount[20] = {0}; // raw lsbits defined for each pulse in each shell block
1140  int32_t excitation[320]; // Q23
1141 
1142  /* excitation parameters */
1144  shellblocks = silk_shell_blocks[s->bandwidth][s->subframes >> 2];
1145  ratelevel = opus_rc_getsymbol(rc, silk_model_exc_rate[voiced]);
1146 
1147  for (i = 0; i < shellblocks; i++) {
1148  pulsecount[i] = opus_rc_getsymbol(rc, silk_model_pulse_count[ratelevel]);
1149  if (pulsecount[i] == 17) {
1150  while (pulsecount[i] == 17 && ++lsbcount[i] != 10)
1151  pulsecount[i] = opus_rc_getsymbol(rc, silk_model_pulse_count[9]);
1152  if (lsbcount[i] == 10)
1153  pulsecount[i] = opus_rc_getsymbol(rc, silk_model_pulse_count[10]);
1154  }
1155  }
1156 
1157  /* decode pulse locations using PVQ */
1158  for (i = 0; i < shellblocks; i++) {
1159  if (pulsecount[i] != 0) {
1160  int a, b, c, d;
1161  int32_t * location = excitation + 16*i;
1162  int32_t branch[4][2];
1163  branch[0][0] = pulsecount[i];
1164 
1165  /* unrolled tail recursion */
1166  for (a = 0; a < 1; a++) {
1167  silk_count_children(rc, 0, branch[0][a], branch[1]);
1168  for (b = 0; b < 2; b++) {
1169  silk_count_children(rc, 1, branch[1][b], branch[2]);
1170  for (c = 0; c < 2; c++) {
1171  silk_count_children(rc, 2, branch[2][c], branch[3]);
1172  for (d = 0; d < 2; d++) {
1173  silk_count_children(rc, 3, branch[3][d], location);
1174  location += 2;
1175  }
1176  }
1177  }
1178  }
1179  } else
1180  memset(excitation + 16*i, 0, 16*sizeof(int32_t));
1181  }
1182 
1183  /* decode least significant bits */
1184  for (i = 0; i < shellblocks << 4; i++) {
1185  int bit;
1186  for (bit = 0; bit < lsbcount[i >> 4]; bit++)
1187  excitation[i] = (excitation[i] << 1) |
1189  }
1190 
1191  /* decode signs */
1192  for (i = 0; i < shellblocks << 4; i++) {
1193  if (excitation[i] != 0) {
1194  int sign = opus_rc_getsymbol(rc, silk_model_excitation_sign[active +
1195  voiced][qoffset_high][FFMIN(pulsecount[i >> 4], 6)]);
1196  if (sign == 0)
1197  excitation[i] *= -1;
1198  }
1199  }
1200 
1201  /* assemble the excitation */
1202  for (i = 0; i < shellblocks << 4; i++) {
1203  int value = excitation[i];
1204  excitation[i] = value * 256 | silk_quant_offset[voiced][qoffset_high];
1205  if (value < 0) excitation[i] += 20;
1206  else if (value > 0) excitation[i] -= 20;
1207 
1208  /* invert samples pseudorandomly */
1209  seed = 196314165 * seed + 907633515;
1210  if (seed & 0x80000000)
1211  excitation[i] *= -1;
1212  seed += value;
1213 
1214  excitationf[i] = excitation[i] / 8388608.0f;
1215  }
1216 }
1217 
1218 /** Maximum residual history according to 4.2.7.6.1 */
1219 #define SILK_MAX_LAG (288 + LTP_ORDER / 2)
1220 
1221 /** Order of the LTP filter */
1222 #define LTP_ORDER 5
1223 
1225  int frame_num, int channel, int coded_channels, int active, int active1)
1226 {
1227  /* per frame */
1228  int voiced; // combines with active to indicate inactive, active, or active+voiced
1229  int qoffset_high;
1230  int order; // order of the LPC coefficients
1231  float lpc_leadin[16], lpc_body[16], residual[SILK_MAX_LAG + SILK_HISTORY];
1232  int has_lpc_leadin;
1233  float ltpscale;
1234 
1235  /* per subframe */
1236  struct {
1237  float gain;
1238  int pitchlag;
1239  float ltptaps[5];
1240  } sf[4];
1241 
1242  SilkFrame * const frame = s->frame + channel;
1243 
1244  int i;
1245 
1246  /* obtain stereo weights */
1247  if (coded_channels == 2 && channel == 0) {
1248  int n, wi[2], ws[2], w[2];
1250  wi[0] = opus_rc_getsymbol(rc, silk_model_stereo_s2) + 3 * (n / 5);
1252  wi[1] = opus_rc_getsymbol(rc, silk_model_stereo_s2) + 3 * (n % 5);
1254 
1255  for (i = 0; i < 2; i++)
1256  w[i] = silk_stereo_weights[wi[i]] +
1257  (((silk_stereo_weights[wi[i] + 1] - silk_stereo_weights[wi[i]]) * 6554) >> 16)
1258  * (ws[i]*2 + 1);
1259 
1260  s->stereo_weights[0] = (w[0] - w[1]) / 8192.0;
1261  s->stereo_weights[1] = w[1] / 8192.0;
1262 
1263  /* and read the mid-only flag */
1264  s->midonly = active1 ? 0 : opus_rc_getsymbol(rc, silk_model_mid_only);
1265  }
1266 
1267  /* obtain frame type */
1268  if (!active) {
1270  voiced = 0;
1271  } else {
1273  qoffset_high = type & 1;
1274  voiced = type >> 1;
1275  }
1276 
1277  /* obtain subframe quantization gains */
1278  for (i = 0; i < s->subframes; i++) {
1279  int log_gain; //Q7
1280  int ipart, fpart, lingain;
1281 
1282  if (i == 0 && (frame_num == 0 || !frame->coded)) {
1283  /* gain is coded absolute */
1284  int x = opus_rc_getsymbol(rc, silk_model_gain_highbits[active + voiced]);
1285  log_gain = (x<<3) | opus_rc_getsymbol(rc, silk_model_gain_lowbits);
1286 
1287  if (frame->coded)
1288  log_gain = FFMAX(log_gain, frame->log_gain - 16);
1289  } else {
1290  /* gain is coded relative */
1291  int delta_gain = opus_rc_getsymbol(rc, silk_model_gain_delta);
1292  log_gain = av_clip_uintp2(FFMAX((delta_gain<<1) - 16,
1293  frame->log_gain + delta_gain - 4), 6);
1294  }
1295 
1296  frame->log_gain = log_gain;
1297 
1298  /* approximate 2**(x/128) with a Q7 (i.e. non-integer) input */
1299  log_gain = (log_gain * 0x1D1C71 >> 16) + 2090;
1300  ipart = log_gain >> 7;
1301  fpart = log_gain & 127;
1302  lingain = (1 << ipart) + ((-174 * fpart * (128-fpart) >>16) + fpart) * ((1<<ipart) >> 7);
1303  sf[i].gain = lingain / 65536.0f;
1304  }
1305 
1306  /* obtain LPC filter coefficients */
1307  silk_decode_lpc(s, frame, rc, lpc_leadin, lpc_body, &order, &has_lpc_leadin, voiced);
1308 
1309  /* obtain pitch lags, if this is a voiced frame */
1310  if (voiced) {
1311  int lag_absolute = (!frame_num || !frame->prev_voiced);
1312  int primarylag; // primary pitch lag for the entire SILK frame
1313  int ltpfilter;
1314  const int8_t * offsets;
1315 
1316  if (!lag_absolute) {
1318  if (delta)
1319  primarylag = frame->primarylag + delta - 9;
1320  else
1321  lag_absolute = 1;
1322  }
1323 
1324  if (lag_absolute) {
1325  /* primary lag is coded absolute */
1326  int highbits, lowbits;
1327  const uint16_t *model[] = {
1330  };
1332  lowbits = opus_rc_getsymbol(rc, model[s->bandwidth]);
1333 
1334  primarylag = silk_pitch_min_lag[s->bandwidth] +
1335  highbits*silk_pitch_scale[s->bandwidth] + lowbits;
1336  }
1337  frame->primarylag = primarylag;
1338 
1339  if (s->subframes == 2)
1340  offsets = (s->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
1345  else
1346  offsets = (s->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
1351 
1352  for (i = 0; i < s->subframes; i++)
1353  sf[i].pitchlag = av_clip(primarylag + offsets[i],
1356 
1357  /* obtain LTP filter coefficients */
1358  ltpfilter = opus_rc_getsymbol(rc, silk_model_ltp_filter);
1359  for (i = 0; i < s->subframes; i++) {
1360  int index, j;
1361  const uint16_t *filter_sel[] = {
1364  };
1365  const int8_t (*filter_taps[])[5] = {
1367  };
1368  index = opus_rc_getsymbol(rc, filter_sel[ltpfilter]);
1369  for (j = 0; j < 5; j++)
1370  sf[i].ltptaps[j] = filter_taps[ltpfilter][index][j] / 128.0f;
1371  }
1372  }
1373 
1374  /* obtain LTP scale factor */
1375  if (voiced && frame_num == 0)
1377  silk_model_ltp_scale_index)] / 16384.0f;
1378  else ltpscale = 15565.0f/16384.0f;
1379 
1380  /* generate the excitation signal for the entire frame */
1381  silk_decode_excitation(s, rc, residual + SILK_MAX_LAG, qoffset_high,
1382  active, voiced);
1383 
1384  /* skip synthesising the side channel if we want mono-only */
1385  if (s->output_channels == channel)
1386  return;
1387 
1388  /* generate the output signal */
1389  for (i = 0; i < s->subframes; i++) {
1390  const float * lpc_coeff = (i < 2 && has_lpc_leadin) ? lpc_leadin : lpc_body;
1391  float *dst = frame->output + SILK_HISTORY + i * s->sflength;
1392  float *resptr = residual + SILK_MAX_LAG + i * s->sflength;
1393  float *lpc = frame->lpc_history + SILK_HISTORY + i * s->sflength;
1394  float sum;
1395  int j, k;
1396 
1397  if (voiced) {
1398  int out_end;
1399  float scale;
1400 
1401  if (i < 2 || s->nlsf_interp_factor == 4) {
1402  out_end = -i * s->sflength;
1403  scale = ltpscale;
1404  } else {
1405  out_end = -(i - 2) * s->sflength;
1406  scale = 1.0f;
1407  }
1408 
1409  /* when the LPC coefficients change, a re-whitening filter is used */
1410  /* to produce a residual that accounts for the change */
1411  for (j = - sf[i].pitchlag - LTP_ORDER/2; j < out_end; j++) {
1412  sum = dst[j];
1413  for (k = 0; k < order; k++)
1414  sum -= lpc_coeff[k] * dst[j - k - 1];
1415  resptr[j] = av_clipf(sum, -1.0f, 1.0f) * scale / sf[i].gain;
1416  }
1417 
1418  if (out_end) {
1419  float rescale = sf[i-1].gain / sf[i].gain;
1420  for (j = out_end; j < 0; j++)
1421  resptr[j] *= rescale;
1422  }
1423 
1424  /* LTP synthesis */
1425  for (j = 0; j < s->sflength; j++) {
1426  sum = resptr[j];
1427  for (k = 0; k < LTP_ORDER; k++)
1428  sum += sf[i].ltptaps[k] * resptr[j - sf[i].pitchlag + LTP_ORDER/2 - k];
1429  resptr[j] = sum;
1430  }
1431  }
1432 
1433  /* LPC synthesis */
1434  for (j = 0; j < s->sflength; j++) {
1435  sum = resptr[j] * sf[i].gain;
1436  for (k = 1; k <= order; k++)
1437  sum += lpc_coeff[k - 1] * lpc[j - k];
1438 
1439  lpc[j] = sum;
1440  dst[j] = av_clipf(sum, -1.0f, 1.0f);
1441  }
1442  }
1443 
1444  frame->prev_voiced = voiced;
1445  memmove(frame->lpc_history, frame->lpc_history + s->flength, SILK_HISTORY * sizeof(float));
1446  memmove(frame->output, frame->output + s->flength, SILK_HISTORY * sizeof(float));
1447 
1448  frame->coded = 1;
1449 }
1450 
1451 static void silk_unmix_ms(SilkContext *s, float *l, float *r)
1452 {
1453  float *mid = s->frame[0].output + SILK_HISTORY - s->flength;
1454  float *side = s->frame[1].output + SILK_HISTORY - s->flength;
1455  float w0_prev = s->prev_stereo_weights[0];
1456  float w1_prev = s->prev_stereo_weights[1];
1457  float w0 = s->stereo_weights[0];
1458  float w1 = s->stereo_weights[1];
1459  int n1 = silk_stereo_interp_len[s->bandwidth];
1460  int i;
1461 
1462  for (i = 0; i < n1; i++) {
1463  float interp0 = w0_prev + i * (w0 - w0_prev) / n1;
1464  float interp1 = w1_prev + i * (w1 - w1_prev) / n1;
1465  float p0 = 0.25 * (mid[i - 2] + 2 * mid[i - 1] + mid[i]);
1466 
1467  l[i] = av_clipf((1 + interp1) * mid[i - 1] + side[i - 1] + interp0 * p0, -1.0, 1.0);
1468  r[i] = av_clipf((1 - interp1) * mid[i - 1] - side[i - 1] - interp0 * p0, -1.0, 1.0);
1469  }
1470 
1471  for (; i < s->flength; i++) {
1472  float p0 = 0.25 * (mid[i - 2] + 2 * mid[i - 1] + mid[i]);
1473 
1474  l[i] = av_clipf((1 + w1) * mid[i - 1] + side[i - 1] + w0 * p0, -1.0, 1.0);
1475  r[i] = av_clipf((1 - w1) * mid[i - 1] - side[i - 1] - w0 * p0, -1.0, 1.0);
1476  }
1477 
1478  memcpy(s->prev_stereo_weights, s->stereo_weights, sizeof(s->stereo_weights));
1479 }
1480 
1482 {
1483  if (!frame->coded)
1484  return;
1485 
1486  memset(frame->output, 0, sizeof(frame->output));
1487  memset(frame->lpc_history, 0, sizeof(frame->lpc_history));
1488 
1489  memset(frame->lpc, 0, sizeof(frame->lpc));
1490  memset(frame->nlsf, 0, sizeof(frame->nlsf));
1491 
1492  frame->log_gain = 0;
1493 
1494  frame->primarylag = 0;
1495  frame->prev_voiced = 0;
1496  frame->coded = 0;
1497 }
1498 
1500  float *output[2],
1501  enum OpusBandwidth bandwidth,
1502  int coded_channels,
1503  int duration_ms)
1504 {
1505  int active[2][6], redundancy[2];
1506  int nb_frames, i, j;
1507 
1508  if (bandwidth > OPUS_BANDWIDTH_WIDEBAND ||
1509  coded_channels > 2 || duration_ms > 60) {
1510  av_log(s->avctx, AV_LOG_ERROR, "Invalid parameters passed "
1511  "to the SILK decoder.\n");
1512  return AVERROR(EINVAL);
1513  }
1514 
1515  nb_frames = 1 + (duration_ms > 20) + (duration_ms > 40);
1516  s->subframes = duration_ms / nb_frames / 5; // 5ms subframes
1517  s->sflength = 20 * (bandwidth + 2);
1518  s->flength = s->sflength * s->subframes;
1519  s->bandwidth = bandwidth;
1520  s->wb = bandwidth == OPUS_BANDWIDTH_WIDEBAND;
1521 
1522  /* make sure to flush the side channel when switching from mono to stereo */
1523  if (coded_channels > s->prev_coded_channels)
1524  silk_flush_frame(&s->frame[1]);
1525  s->prev_coded_channels = coded_channels;
1526 
1527  /* read the LP-layer header bits */
1528  for (i = 0; i < coded_channels; i++) {
1529  for (j = 0; j < nb_frames; j++)
1530  active[i][j] = opus_rc_p2model(rc, 1);
1531 
1532  redundancy[i] = opus_rc_p2model(rc, 1);
1533  if (redundancy[i]) {
1534  av_log(s->avctx, AV_LOG_ERROR, "LBRR frames present; this is unsupported\n");
1535  return AVERROR_PATCHWELCOME;
1536  }
1537  }
1538 
1539  for (i = 0; i < nb_frames; i++) {
1540  for (j = 0; j < coded_channels && !s->midonly; j++)
1541  silk_decode_frame(s, rc, i, j, coded_channels, active[j][i], active[1][i]);
1542 
1543  /* reset the side channel if it is not coded */
1544  if (s->midonly && s->frame[1].coded)
1545  silk_flush_frame(&s->frame[1]);
1546 
1547  if (coded_channels == 1 || s->output_channels == 1) {
1548  for (j = 0; j < s->output_channels; j++) {
1549  memcpy(output[j] + i * s->flength,
1550  s->frame[0].output + SILK_HISTORY - s->flength - 2,
1551  s->flength * sizeof(float));
1552  }
1553  } else {
1554  silk_unmix_ms(s, output[0] + i * s->flength, output[1] + i * s->flength);
1555  }
1556 
1557  s->midonly = 0;
1558  }
1559 
1560  return nb_frames * s->flength;
1561 }
1562 
1564 {
1565  av_freep(ps);
1566 }
1567 
1569 {
1570  silk_flush_frame(&s->frame[0]);
1571  silk_flush_frame(&s->frame[1]);
1572 
1573  memset(s->prev_stereo_weights, 0, sizeof(s->prev_stereo_weights));
1574 }
1575 
1576 int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels)
1577 {
1578  SilkContext *s;
1579 
1580  if (output_channels != 1 && output_channels != 2) {
1581  av_log(avctx, AV_LOG_ERROR, "Invalid number of output channels: %d\n",
1582  output_channels);
1583  return AVERROR(EINVAL);
1584  }
1585 
1586  s = av_mallocz(sizeof(*s));
1587  if (!s)
1588  return AVERROR(ENOMEM);
1589 
1590  s->avctx = avctx;
1591  s->output_channels = output_channels;
1592 
1593  ff_silk_flush(s);
1594 
1595  *ps = s;
1596 
1597  return 0;
1598 }
static void silk_lsp2poly(const int32_t lsp[16], int32_t pol[16], int half_order)
Definition: opus_silk.c:917
int output_channels
Definition: opus_silk.c:46
const char * s
Definition: avisynth_c.h:631
static void silk_decode_frame(SilkContext *s, OpusRangeCoder *rc, int frame_num, int channel, int coded_channels, int active, int active1)
Definition: opus_silk.c:1224
int nlsf_interp_factor
Definition: opus_silk.c:52
static av_always_inline unsigned int opus_rc_getsymbol(OpusRangeCoder *rc, const uint16_t *cdf)
Definition: opus.h:215
int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels)
Definition: opus_silk.c:1576
static const uint16_t silk_model_pitch_delta[]
Definition: opus_silk.c:149
const char * b
Definition: vf_curves.c:109
static const uint16_t silk_model_pitch_lowbits_mb[]
Definition: opus_silk.c:145
int sflength
Definition: opus_silk.c:50
static const uint16_t silk_model_excitation_lsb[]
Definition: opus_silk.c:291
static av_always_inline unsigned int opus_rc_p2model(OpusRangeCoder *rc, unsigned int bits)
Definition: opus.h:234
static const uint16_t silk_model_frame_type_inactive[]
Definition: opus_silk.c:75
static const uint16_t silk_model_gain_highbits[3][9]
Definition: opus_silk.c:79
static const uint8_t silk_lsf_pred_weights_nbmb[2][9]
Definition: opus_silk.c:426
#define SILK_HISTORY
Definition: opus.h:57
int subframes
Definition: opus_silk.c:49
static void silk_count_children(OpusRangeCoder *rc, int model, int32_t total, int32_t child[2])
Definition: opus_silk.c:1117
static const uint16_t silk_model_gain_lowbits[]
Definition: opus_silk.c:85
float output[2 *SILK_HISTORY]
Definition: opus_silk.c:37
static const int16_t silk_cosine[]
Definition: opus_silk.c:592
static const int8_t silk_pitch_offset_mbwb20ms[34][4]
Definition: opus_silk.c:669
static const uint8_t silk_lsf_codebook_nbmb[32][10]
Definition: opus_silk.c:506
uint8_t
static const uint16_t silk_model_pitch_lowbits_wb[]
Definition: opus_silk.c:147
float delta
AVS_FilterInfo AVS_Value child
Definition: avisynth_c.h:594
static const uint16_t silk_lsf_min_spacing_wb[]
Definition: opus_silk.c:580
#define opus_ilog(i)
Definition: opus.h:62
static const uint16_t silk_model_pitch_contour_nb20ms[]
Definition: opus_silk.c:156
int log_gain
Definition: opus_silk.c:33
static const uint8_t silk_lsf_s2_model_sel_wb[32][16]
Definition: opus_silk.c:391
static AVFrame * frame
static const int8_t silk_pitch_offset_nb20ms[11][4]
Definition: opus_silk.c:640
int16_t nlsf[16]
Definition: opus_silk.c:34
static const int silk_stereo_interp_len[3]
Definition: opus_silk.c:784
#define av_log(a,...)
static const uint16_t silk_model_exc_rate[2][10]
Definition: opus_silk.c:189
static void silk_stabilize_lsf(int16_t nlsf[16], int order, const uint16_t min_delta[17])
Definition: opus_silk.c:788
static const int8_t silk_pitch_offset_nb10ms[3][2]
Definition: opus_silk.c:634
static const uint16_t silk_model_gain_delta[]
Definition: opus_silk.c:87
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static const uint16_t silk_model_pitch_contour_mbwb20ms[]
Definition: opus_silk.c:164
#define AVERROR(e)
Definition: error.h:43
static const uint16_t silk_ltp_scale_factor[]
Definition: opus_silk.c:771
static const int8_t silk_pitch_offset_mbwb10ms[12][2]
Definition: opus_silk.c:654
const char * r
Definition: vf_curves.c:107
static const uint8_t silk_lsf_ordering_wb[]
Definition: opus_silk.c:588
static const uint16_t silk_lsf_min_spacing_nbmb[]
Definition: opus_silk.c:576
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define FFMAX(a, b)
Definition: common.h:90
static void silk_unmix_ms(SilkContext *s, float *l, float *r)
Definition: opus_silk.c:1451
static const uint16_t silk_pitch_min_lag[]
Definition: opus_silk.c:630
static const uint16_t silk_model_ltp_filter1_sel[]
Definition: opus_silk.c:176
#define pass
Definition: fft_template.c:509
static void silk_decode_excitation(SilkContext *s, OpusRangeCoder *rc, float *excitationf, int qoffset_high, int active, int voiced)
Definition: opus_silk.c:1130
static const uint8_t silk_quant_offset[2][2]
Definition: opus_silk.c:779
static const uint16_t silk_model_frame_type_active[]
Definition: opus_silk.c:77
float lpc[16]
Definition: opus_silk.c:35
#define FFMIN(a, b)
Definition: common.h:92
float y
enum OpusBandwidth bandwidth
Definition: opus_silk.c:54
int midonly
Definition: opus_silk.c:48
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
static const uint16_t silk_model_mid_only[]
Definition: opus_silk.c:73
int32_t
static const uint16_t silk_pitch_max_lag[]
Definition: opus_silk.c:632
static const uint16_t silk_model_lsf_s2_ext[]
Definition: opus_silk.c:134
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:68
int coded
Definition: opus_silk.c:32
static const uint16_t silk_model_pulse_count[11][19]
Definition: opus_silk.c:194
static const int8_t silk_ltp_filter0_taps[8][5]
Definition: opus_silk.c:706
int n
Definition: avisynth_c.h:547
static const uint16_t silk_model_pitch_highbits[]
Definition: opus_silk.c:138
static const uint8_t silk_shell_blocks[3][2]
Definition: opus_silk.c:773
static const uint8_t silk_lsf_ordering_nbmb[]
Definition: opus_silk.c:584
static const uint16_t silk_model_pitch_contour_nb10ms[]
Definition: opus_silk.c:154
static const uint16_t silk_model_stereo_s1[]
Definition: opus_silk.c:64
int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc, float *output[2], enum OpusBandwidth bandwidth, int coded_channels, int duration_ms)
Decode the LP layer of one Opus frame (which may correspond to several SILK frames).
Definition: opus_silk.c:1499
static const uint8_t silk_lsf_weight_sel_wb[32][15]
Definition: opus_silk.c:471
static const uint16_t silk_model_lcg_seed[]
Definition: opus_silk.c:187
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define SILK_MAX_LAG
Maximum residual history according to 4.2.7.6.1.
Definition: opus_silk.c:1219
static const uint16_t silk_model_lsf_s1[2][2][33]
Definition: opus_silk.c:92
static const uint16_t silk_model_stereo_s3[]
Definition: opus_silk.c:71
int flength
Definition: opus_silk.c:51
static const uint16_t silk_model_pulse_location[4][168]
Definition: opus_silk.c:219
static const uint8_t silk_lsf_s2_model_sel_nbmb[32][10]
Definition: opus_silk.c:356
main external API structure.
Definition: avcodec.h:1512
static unsigned int seed
Definition: videogen.c:78
static const uint8_t silk_lsf_pred_weights_wb[2][15]
Definition: opus_silk.c:431
int primarylag
Definition: opus_silk.c:39
GLint GLenum type
Definition: opengl_enc.c:105
float stereo_weights[2]
Definition: opus_silk.c:59
#define LTP_ORDER
Order of the LTP filter.
Definition: opus_silk.c:1222
static void silk_flush_frame(SilkFrame *frame)
Definition: opus_silk.c:1481
int index
Definition: gxfenc.c:89
static const uint16_t silk_model_stereo_s2[]
Definition: opus_silk.c:69
int prev_coded_channels
Definition: opus_silk.c:61
static const uint16_t silk_model_pitch_lowbits_nb[]
Definition: opus_silk.c:143
static const uint16_t silk_model_lsf_interpolation_offset[]
Definition: opus_silk.c:136
static void silk_decode_lpc(SilkContext *s, SilkFrame *frame, OpusRangeCoder *rc, float lpc_leadin[16], float lpc[16], int *lpc_order, int *has_lpc_leadin, int voiced)
Definition: opus_silk.c:1020
AVCodecContext * avctx
Definition: opus_silk.c:45
#define ROUND_MULL(a, b, s)
Definition: opus.h:60
static int weight(int i, int blen, int offset)
Definition: diracdec.c:1310
static const uint16_t silk_pitch_scale[]
Definition: opus_silk.c:628
static const int16_t silk_stereo_weights[]
Definition: opus_silk.c:351
static const uint8_t silk_lsf_codebook_wb[32][16]
Definition: opus_silk.c:541
static const uint16_t silk_model_ltp_filter[]
Definition: opus_silk.c:170
static const int8_t silk_ltp_filter1_taps[16][5]
Definition: opus_silk.c:717
static const uint16_t silk_model_pitch_contour_mbwb10ms[]
Definition: opus_silk.c:160
OpusBandwidth
Definition: opus.h:79
static int silk_is_lpc_stable(const int16_t lpc[16], int order)
Definition: opus_silk.c:866
static double c[64]
float prev_stereo_weights[2]
Definition: opus_silk.c:58
SilkFrame frame[2]
Definition: opus_silk.c:57
void ff_silk_free(SilkContext **ps)
Definition: opus_silk.c:1563
static const uint8_t silk_lsf_weight_sel_nbmb[32][9]
Definition: opus_silk.c:436
static const int8_t silk_ltp_filter2_taps[32][5]
Definition: opus_silk.c:736
static const uint16_t silk_model_ltp_filter2_sel[]
Definition: opus_silk.c:180
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static const uint16_t silk_model_ltp_scale_index[]
Definition: opus_silk.c:185
void ff_silk_flush(SilkContext *s)
Definition: opus_silk.c:1568
int prev_voiced
Definition: opus_silk.c:41
#define MULL(a, b, s)
Definition: mathops.h:56
#define av_freep(p)
static void silk_lsf2lpc(const int16_t nlsf[16], float lpcf[16], int order)
Definition: opus_silk.c:933
static const uint16_t silk_model_excitation_sign[3][2][7][3]
Definition: opus_silk.c:293
static const uint16_t silk_model_lsf_s2[32][10]
Definition: opus_silk.c:112
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
float lpc_history[2 *SILK_HISTORY]
Definition: opus_silk.c:38
static const uint16_t silk_model_ltp_filter0_sel[]
Definition: opus_silk.c:172
#define MULH
Definition: mathops.h:42