/src/ffmpeg/libavcodec/ilbcdec.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2013, The WebRTC project authors. All rights reserved. |
3 | | * |
4 | | * Redistribution and use in source and binary forms, with or without |
5 | | * modification, are permitted provided that the following conditions are |
6 | | * met: |
7 | | * |
8 | | * * Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * |
11 | | * * Redistributions in binary form must reproduce the above copyright |
12 | | * notice, this list of conditions and the following disclaimer in |
13 | | * the documentation and/or other materials provided with the |
14 | | * distribution. |
15 | | * |
16 | | * * Neither the name of Google nor the names of its contributors may |
17 | | * be used to endorse or promote products derived from this software |
18 | | * without specific prior written permission. |
19 | | * |
20 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
22 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
23 | | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
24 | | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
25 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
26 | | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
27 | | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
28 | | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
29 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
30 | | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | | */ |
32 | | |
33 | | #include "libavutil/channel_layout.h" |
34 | | #include "avcodec.h" |
35 | | #include "codec_internal.h" |
36 | | #include "decode.h" |
37 | | #include "get_bits.h" |
38 | | #include "ilbcdata.h" |
39 | | |
40 | 1.72k | #define LPC_N_20MS 1 |
41 | 626 | #define LPC_N_30MS 2 |
42 | | #define LPC_N_MAX 2 |
43 | 162k | #define LSF_NSPLIT 3 |
44 | | #define NASUB_MAX 4 |
45 | 3.72M | #define LPC_FILTERORDER 10 |
46 | | #define NSUB_MAX 6 |
47 | 3.15M | #define SUBL 40 |
48 | | |
49 | 37.9k | #define ST_MEM_L_TBL 85 |
50 | 50.2k | #define MEM_LF_TBL 147 |
51 | | #define STATE_SHORT_LEN_20MS 57 |
52 | | #define STATE_SHORT_LEN_30MS 58 |
53 | | |
54 | | #define BLOCKL_MAX 240 |
55 | 226k | #define CB_MEML 147 |
56 | 100k | #define CB_NSTAGES 3 |
57 | 79.4k | #define CB_HALFFILTERLEN 4 |
58 | 31.6k | #define CB_FILTERLEN 8 |
59 | | |
60 | | #define ENH_NBLOCKS_TOT 8 |
61 | 70.0k | #define ENH_BLOCKL 80 |
62 | | #define ENH_BUFL (ENH_NBLOCKS_TOT)*ENH_BLOCKL |
63 | | #define ENH_BUFL_FILTEROVERHEAD 3 |
64 | | #define BLOCKL_MAX 240 |
65 | 1.72k | #define NSUB_20MS 4 |
66 | 626 | #define NSUB_30MS 6 |
67 | | #define NSUB_MAX 6 |
68 | 1.72k | #define NASUB_20MS 2 |
69 | 626 | #define NASUB_30MS 4 |
70 | | #define NASUB_MAX 4 |
71 | 60.2k | #define STATE_LEN 80 |
72 | 626 | #define STATE_SHORT_LEN_30MS 58 |
73 | 1.72k | #define STATE_SHORT_LEN_20MS 57 |
74 | | |
75 | 247M | #define SPL_MUL_16_16(a, b) ((int32_t) (((int16_t)(a)) * ((int16_t)(b)))) |
76 | 5.76M | #define SPL_MUL_16_16_RSFT(a, b, c) (SPL_MUL_16_16(a, b) >> (c)) |
77 | | |
78 | | typedef struct ILBCFrame { |
79 | | int16_t lsf[LSF_NSPLIT*LPC_N_MAX]; |
80 | | int16_t cb_index[CB_NSTAGES*(NASUB_MAX + 1)]; |
81 | | int16_t gain_index[CB_NSTAGES*(NASUB_MAX + 1)]; |
82 | | int16_t ifm; |
83 | | int16_t state_first; |
84 | | int16_t idx[STATE_SHORT_LEN_30MS]; |
85 | | int16_t firstbits; |
86 | | int16_t start; |
87 | | } ILBCFrame; |
88 | | |
89 | | typedef struct ILBCContext { |
90 | | AVClass *class; |
91 | | int enhancer; |
92 | | |
93 | | int mode; |
94 | | ILBCFrame frame; |
95 | | |
96 | | int prev_enh_pl; |
97 | | int consPLICount; |
98 | | int last_lag; |
99 | | int state_short_len; |
100 | | int lpc_n; |
101 | | int16_t nasub; |
102 | | int16_t nsub; |
103 | | int block_samples; |
104 | | int16_t no_of_words; |
105 | | int16_t no_of_bytes; |
106 | | int16_t lsfdeq[LPC_FILTERORDER*LPC_N_MAX]; |
107 | | int16_t lsfold[LPC_FILTERORDER]; |
108 | | int16_t syntMem[LPC_FILTERORDER]; |
109 | | int16_t lsfdeqold[LPC_FILTERORDER]; |
110 | | int16_t weightdenum[(LPC_FILTERORDER + 1) * NSUB_MAX]; |
111 | | int16_t syntdenum[NSUB_MAX * (LPC_FILTERORDER + 1)]; |
112 | | int16_t old_syntdenum[NSUB_MAX * (LPC_FILTERORDER + 1)]; |
113 | | int16_t enh_buf[ENH_BUFL+ENH_BUFL_FILTEROVERHEAD]; |
114 | | int16_t enh_period[ENH_NBLOCKS_TOT]; |
115 | | int16_t prevResidual[NSUB_MAX*SUBL]; |
116 | | int16_t decresidual[BLOCKL_MAX]; |
117 | | int16_t plc_residual[BLOCKL_MAX + LPC_FILTERORDER]; |
118 | | int16_t seed; |
119 | | int16_t prevPLI; |
120 | | int16_t prevScale; |
121 | | int16_t prevLag; |
122 | | int16_t per_square; |
123 | | int16_t prev_lpc[LPC_FILTERORDER + 1]; |
124 | | int16_t plc_lpc[LPC_FILTERORDER + 1]; |
125 | | int16_t hpimemx[2]; |
126 | | int16_t hpimemy[4]; |
127 | | } ILBCContext; |
128 | | |
129 | | static int unpack_frame(ILBCContext *s, const uint8_t *buf, int size) |
130 | 196k | { |
131 | 196k | ILBCFrame *frame = &s->frame; |
132 | 196k | GetBitContext gb0, *const gb = &gb0; |
133 | 196k | int j, ret; |
134 | | |
135 | 196k | if ((ret = init_get_bits8(gb, buf, size)) < 0) |
136 | 0 | return ret; |
137 | | |
138 | 196k | frame->lsf[0] = get_bits(gb, 6); |
139 | 196k | frame->lsf[1] = get_bits(gb, 7); |
140 | 196k | frame->lsf[2] = get_bits(gb, 7); |
141 | | |
142 | 196k | if (s->mode == 20) { |
143 | 173k | frame->start = get_bits(gb, 2); |
144 | 173k | frame->state_first = get_bits1(gb); |
145 | 173k | frame->ifm = get_bits(gb, 6); |
146 | 173k | frame->cb_index[0] = get_bits(gb, 6) << 1; |
147 | 173k | frame->gain_index[0] = get_bits(gb, 2) << 3; |
148 | 173k | frame->gain_index[1] = get_bits1(gb) << 3; |
149 | 173k | frame->cb_index[3] = get_bits(gb, 7) << 1; |
150 | 173k | frame->gain_index[3] = get_bits1(gb) << 4; |
151 | 173k | frame->gain_index[4] = get_bits1(gb) << 3; |
152 | 173k | frame->gain_index[6] = get_bits1(gb) << 4; |
153 | 173k | } else { |
154 | 23.3k | frame->lsf[3] = get_bits(gb, 6); |
155 | 23.3k | frame->lsf[4] = get_bits(gb, 7); |
156 | 23.3k | frame->lsf[5] = get_bits(gb, 7); |
157 | 23.3k | frame->start = get_bits(gb, 3); |
158 | 23.3k | frame->state_first = get_bits1(gb); |
159 | 23.3k | frame->ifm = get_bits(gb, 6); |
160 | 23.3k | frame->cb_index[0] = get_bits(gb, 4) << 3; |
161 | 23.3k | frame->gain_index[0] = get_bits1(gb) << 4; |
162 | 23.3k | frame->gain_index[1] = get_bits1(gb) << 3; |
163 | 23.3k | frame->cb_index[3] = get_bits(gb, 6) << 2; |
164 | 23.3k | frame->gain_index[3] = get_bits1(gb) << 4; |
165 | 23.3k | frame->gain_index[4] = get_bits1(gb) << 3; |
166 | 23.3k | } |
167 | | |
168 | 9.64M | for (j = 0; j < 48; j++) |
169 | 9.45M | frame->idx[j] = get_bits1(gb) << 2; |
170 | | |
171 | 196k | if (s->mode == 20) { |
172 | 1.73M | for (; j < 57; j++) |
173 | 1.56M | frame->idx[j] = get_bits1(gb) << 2; |
174 | | |
175 | 173k | frame->gain_index[1] |= get_bits1(gb) << 2; |
176 | 173k | frame->gain_index[3] |= get_bits(gb, 2) << 2; |
177 | 173k | frame->gain_index[4] |= get_bits1(gb) << 2; |
178 | 173k | frame->gain_index[6] |= get_bits1(gb) << 3; |
179 | 173k | frame->gain_index[7] = get_bits(gb, 2) << 2; |
180 | 173k | } else { |
181 | 256k | for (; j < 58; j++) |
182 | 233k | frame->idx[j] = get_bits1(gb) << 2; |
183 | | |
184 | 23.3k | frame->cb_index[0] |= get_bits(gb, 2) << 1; |
185 | 23.3k | frame->gain_index[0] |= get_bits1(gb) << 3; |
186 | 23.3k | frame->gain_index[1] |= get_bits1(gb) << 2; |
187 | 23.3k | frame->cb_index[3] |= get_bits1(gb) << 1; |
188 | 23.3k | frame->cb_index[6] = get_bits1(gb) << 7; |
189 | 23.3k | frame->cb_index[6] |= get_bits(gb, 6) << 1; |
190 | 23.3k | frame->cb_index[9] = get_bits(gb, 7) << 1; |
191 | 23.3k | frame->cb_index[12] = get_bits(gb, 3) << 5; |
192 | 23.3k | frame->cb_index[12] |= get_bits(gb, 4) << 1; |
193 | 23.3k | frame->gain_index[3] |= get_bits(gb, 2) << 2; |
194 | 23.3k | frame->gain_index[4] |= get_bits(gb, 2) << 1; |
195 | 23.3k | frame->gain_index[6] = get_bits(gb, 2) << 3; |
196 | 23.3k | frame->gain_index[7] = get_bits(gb, 2) << 2; |
197 | 23.3k | frame->gain_index[9] = get_bits1(gb) << 4; |
198 | 23.3k | frame->gain_index[10] = get_bits1(gb) << 3; |
199 | 23.3k | frame->gain_index[12] = get_bits1(gb) << 4; |
200 | 23.3k | frame->gain_index[13] = get_bits1(gb) << 3; |
201 | 23.3k | } |
202 | | |
203 | 11.2M | for (j = 0; j < 56; j++) |
204 | 11.0M | frame->idx[j] |= get_bits(gb, 2); |
205 | | |
206 | 196k | if (s->mode == 20) { |
207 | 173k | frame->idx[56] |= get_bits(gb, 2); |
208 | 173k | frame->cb_index[0] |= get_bits1(gb); |
209 | 173k | frame->cb_index[1] = get_bits(gb, 7); |
210 | 173k | frame->cb_index[2] = get_bits(gb, 6) << 1; |
211 | 173k | frame->cb_index[2] |= get_bits1(gb); |
212 | 173k | frame->gain_index[0] |= get_bits(gb, 3); |
213 | 173k | frame->gain_index[1] |= get_bits(gb, 2); |
214 | 173k | frame->gain_index[2] = get_bits(gb, 3); |
215 | 173k | frame->cb_index[3] |= get_bits1(gb); |
216 | 173k | frame->cb_index[4] = get_bits(gb, 6) << 1; |
217 | 173k | frame->cb_index[4] |= get_bits1(gb); |
218 | 173k | frame->cb_index[5] = get_bits(gb, 7); |
219 | 173k | frame->cb_index[6] = get_bits(gb, 8); |
220 | 173k | frame->cb_index[7] = get_bits(gb, 8); |
221 | 173k | frame->cb_index[8] = get_bits(gb, 8); |
222 | 173k | frame->gain_index[3] |= get_bits(gb, 2); |
223 | 173k | frame->gain_index[4] |= get_bits(gb, 2); |
224 | 173k | frame->gain_index[5] = get_bits(gb, 3); |
225 | 173k | frame->gain_index[6] |= get_bits(gb, 3); |
226 | 173k | frame->gain_index[7] |= get_bits(gb, 2); |
227 | 173k | frame->gain_index[8] = get_bits(gb, 3); |
228 | 173k | } else { |
229 | 23.3k | frame->idx[56] |= get_bits(gb, 2); |
230 | 23.3k | frame->idx[57] |= get_bits(gb, 2); |
231 | 23.3k | frame->cb_index[0] |= get_bits1(gb); |
232 | 23.3k | frame->cb_index[1] = get_bits(gb, 7); |
233 | 23.3k | frame->cb_index[2] = get_bits(gb, 4) << 3; |
234 | 23.3k | frame->cb_index[2] |= get_bits(gb, 3); |
235 | 23.3k | frame->gain_index[0] |= get_bits(gb, 3); |
236 | 23.3k | frame->gain_index[1] |= get_bits(gb, 2); |
237 | 23.3k | frame->gain_index[2] = get_bits(gb, 3); |
238 | 23.3k | frame->cb_index[3] |= get_bits1(gb); |
239 | 23.3k | frame->cb_index[4] = get_bits(gb, 4) << 3; |
240 | 23.3k | frame->cb_index[4] |= get_bits(gb, 3); |
241 | 23.3k | frame->cb_index[5] = get_bits(gb, 7); |
242 | 23.3k | frame->cb_index[6] |= get_bits1(gb); |
243 | 23.3k | frame->cb_index[7] = get_bits(gb, 5) << 3; |
244 | 23.3k | frame->cb_index[7] |= get_bits(gb, 3); |
245 | 23.3k | frame->cb_index[8] = get_bits(gb, 8); |
246 | 23.3k | frame->cb_index[9] |= get_bits1(gb); |
247 | 23.3k | frame->cb_index[10] = get_bits(gb, 4) << 4; |
248 | 23.3k | frame->cb_index[10] |= get_bits(gb, 4); |
249 | 23.3k | frame->cb_index[11] = get_bits(gb, 8); |
250 | 23.3k | frame->cb_index[12] |= get_bits1(gb); |
251 | 23.3k | frame->cb_index[13] = get_bits(gb, 3) << 5; |
252 | 23.3k | frame->cb_index[13] |= get_bits(gb, 5); |
253 | 23.3k | frame->cb_index[14] = get_bits(gb, 8); |
254 | 23.3k | frame->gain_index[3] |= get_bits(gb, 2); |
255 | 23.3k | frame->gain_index[4] |= get_bits1(gb); |
256 | 23.3k | frame->gain_index[5] = get_bits(gb, 3); |
257 | 23.3k | frame->gain_index[6] |= get_bits(gb, 3); |
258 | 23.3k | frame->gain_index[7] |= get_bits(gb, 2); |
259 | 23.3k | frame->gain_index[8] = get_bits(gb, 3); |
260 | 23.3k | frame->gain_index[9] |= get_bits(gb, 4); |
261 | 23.3k | frame->gain_index[10] |= get_bits1(gb) << 2; |
262 | 23.3k | frame->gain_index[10] |= get_bits(gb, 2); |
263 | 23.3k | frame->gain_index[11] = get_bits(gb, 3); |
264 | 23.3k | frame->gain_index[12] |= get_bits(gb, 4); |
265 | 23.3k | frame->gain_index[13] |= get_bits(gb, 3); |
266 | 23.3k | frame->gain_index[14] = get_bits(gb, 3); |
267 | 23.3k | } |
268 | | |
269 | 196k | return get_bits1(gb); |
270 | 196k | } |
271 | | |
272 | | static void index_conv(int16_t *index) |
273 | 18.9k | { |
274 | 18.9k | int k; |
275 | | |
276 | 56.8k | for (k = 4; k < 6; k++) { |
277 | 37.9k | if (index[k] >= 44 && index[k] < 108) { |
278 | 2.85k | index[k] += 64; |
279 | 35.0k | } else if (index[k] >= 108 && index[k] < 128) { |
280 | 1.76k | index[k] += 128; |
281 | 1.76k | } |
282 | 37.9k | } |
283 | 18.9k | } |
284 | | |
285 | | static void lsf_dequantization(int16_t *lsfdeq, int16_t *index, int16_t lpc_n) |
286 | 18.9k | { |
287 | 18.9k | int i, j, pos = 0, cb_pos = 0; |
288 | | |
289 | 75.8k | for (i = 0; i < LSF_NSPLIT; i++) { |
290 | 246k | for (j = 0; j < lsf_dim_codebook[i]; j++) { |
291 | 189k | lsfdeq[pos + j] = lsf_codebook[cb_pos + index[i] * lsf_dim_codebook[i] + j]; |
292 | 189k | } |
293 | | |
294 | 56.8k | pos += lsf_dim_codebook[i]; |
295 | 56.8k | cb_pos += lsf_size_codebook[i] * lsf_dim_codebook[i]; |
296 | 56.8k | } |
297 | | |
298 | 18.9k | if (lpc_n > 1) { |
299 | 6.19k | pos = 0; |
300 | 6.19k | cb_pos = 0; |
301 | 24.7k | for (i = 0; i < LSF_NSPLIT; i++) { |
302 | 80.5k | for (j = 0; j < lsf_dim_codebook[i]; j++) { |
303 | 61.9k | lsfdeq[LPC_FILTERORDER + pos + j] = lsf_codebook[cb_pos + |
304 | 61.9k | index[LSF_NSPLIT + i] * lsf_dim_codebook[i] + j]; |
305 | 61.9k | } |
306 | | |
307 | 18.5k | pos += lsf_dim_codebook[i]; |
308 | 18.5k | cb_pos += lsf_size_codebook[i] * lsf_dim_codebook[i]; |
309 | 18.5k | } |
310 | 6.19k | } |
311 | 18.9k | } |
312 | | |
313 | | static void lsf_check_stability(int16_t *lsf, int dim, int nb_vectors) |
314 | 18.9k | { |
315 | 56.8k | for (int n = 0; n < 2; n++) { |
316 | 88.1k | for (int m = 0; m < nb_vectors; m++) { |
317 | 502k | for (int k = 0; k < dim - 1; k++) { |
318 | 452k | int i = m * dim + k; |
319 | | |
320 | 452k | if ((lsf[i + 1] - lsf[i]) < 319) { |
321 | 20.7k | if (lsf[i + 1] < lsf[i]) { |
322 | 9.72k | lsf[i + 1] = lsf[i] + 160; |
323 | 9.72k | lsf[i] = lsf[i + 1] - 160; |
324 | 11.0k | } else { |
325 | 11.0k | lsf[i] -= 160; |
326 | 11.0k | lsf[i + 1] += 160; |
327 | 11.0k | } |
328 | 20.7k | } |
329 | | |
330 | 452k | lsf[i] = av_clip(lsf[i], 82, 25723); |
331 | 452k | } |
332 | 50.2k | } |
333 | 37.9k | } |
334 | 18.9k | } |
335 | | |
336 | | static void lsf_interpolate(int16_t *out, const int16_t *in1, |
337 | | const int16_t *in2, int16_t coef, |
338 | | int size) |
339 | 88.1k | { |
340 | 88.1k | int invcoef = 16384 - coef, i; |
341 | | |
342 | 970k | for (i = 0; i < size; i++) |
343 | 881k | out[i] = (coef * in1[i] + invcoef * in2[i] + 8192) >> 14; |
344 | 88.1k | } |
345 | | |
346 | | static void lsf2lsp(const int16_t *lsf, int16_t *lsp, int order) |
347 | 88.1k | { |
348 | 88.1k | int16_t diff, freq; |
349 | 88.1k | int32_t tmp; |
350 | 88.1k | int i, k; |
351 | | |
352 | 970k | for (i = 0; i < order; i++) { |
353 | 881k | freq = (lsf[i] * 20861) >> 15; |
354 | | /* 20861: 1.0/(2.0*PI) in Q17 */ |
355 | | /* |
356 | | Upper 8 bits give the index k and |
357 | | Lower 8 bits give the difference, which needs |
358 | | to be approximated linearly |
359 | | */ |
360 | 881k | k = FFMIN(freq >> 8, 63); |
361 | 881k | diff = freq & 0xFF; |
362 | | |
363 | | /* Calculate linear approximation */ |
364 | 881k | tmp = cos_derivative_tbl[k] * diff; |
365 | 881k | lsp[i] = cos_tbl[k] + (tmp >> 12); |
366 | 881k | } |
367 | 88.1k | } |
368 | | |
369 | | static void get_lsp_poly(const int16_t *lsp, int32_t *f) |
370 | 176k | { |
371 | 176k | int16_t high, low; |
372 | 176k | int i, j, k, l; |
373 | 176k | int32_t tmp; |
374 | | |
375 | 176k | f[0] = 16777216; |
376 | 176k | f[1] = lsp[0] * -1024; |
377 | | |
378 | 881k | for (i = 2, k = 2, l = 2; i <= 5; i++, k += 2) { |
379 | 705k | f[l] = f[l - 2]; |
380 | | |
381 | 2.46M | for (j = i; j > 1; j--, l--) { |
382 | 1.76M | high = f[l - 1] >> 16; |
383 | 1.76M | low = (f[l - 1] - (high * (1 << 16))) >> 1; |
384 | | |
385 | 1.76M | tmp = ((high * lsp[k]) * 4) + (((low * lsp[k]) >> 15) * 4); |
386 | | |
387 | 1.76M | f[l] += f[l - 2]; |
388 | 1.76M | f[l] -= (unsigned)tmp; |
389 | 1.76M | } |
390 | | |
391 | 705k | f[l] -= lsp[k] * (1 << 10); |
392 | 705k | l += i; |
393 | 705k | } |
394 | 176k | } |
395 | | |
396 | | static void lsf2poly(int16_t *a, const int16_t *lsf) |
397 | 88.1k | { |
398 | 88.1k | int32_t f[2][6]; |
399 | 88.1k | int16_t lsp[10]; |
400 | 88.1k | int32_t tmp; |
401 | 88.1k | int i; |
402 | | |
403 | 88.1k | lsf2lsp(lsf, lsp, LPC_FILTERORDER); |
404 | | |
405 | 88.1k | get_lsp_poly(&lsp[0], f[0]); |
406 | 88.1k | get_lsp_poly(&lsp[1], f[1]); |
407 | | |
408 | 529k | for (i = 5; i > 0; i--) { |
409 | 440k | f[0][i] += (unsigned)f[0][i - 1]; |
410 | 440k | f[1][i] -= (unsigned)f[1][i - 1]; |
411 | 440k | } |
412 | | |
413 | 88.1k | a[0] = 4096; |
414 | 529k | for (i = 5; i > 0; i--) { |
415 | 440k | tmp = f[0][6 - i] + (unsigned)f[1][6 - i] + 4096; |
416 | 440k | a[6 - i] = tmp >> 13; |
417 | | |
418 | 440k | tmp = f[0][6 - i] - (unsigned)f[1][6 - i] + 4096; |
419 | 440k | a[5 + i] = tmp >> 13; |
420 | 440k | } |
421 | 88.1k | } |
422 | | |
423 | | static void lsp_interpolate2polydec(int16_t *a, const int16_t *lsf1, |
424 | | const int16_t *lsf2, int coef, int length) |
425 | 88.1k | { |
426 | 88.1k | int16_t lsftmp[LPC_FILTERORDER]; |
427 | | |
428 | 88.1k | lsf_interpolate(lsftmp, lsf1, lsf2, coef, length); |
429 | 88.1k | lsf2poly(a, lsftmp); |
430 | 88.1k | } |
431 | | |
432 | | static void bw_expand(int16_t *out, const int16_t *in, const int16_t *coef, int length) |
433 | 88.1k | { |
434 | 88.1k | int i; |
435 | | |
436 | 88.1k | out[0] = in[0]; |
437 | 970k | for (i = 1; i < length; i++) |
438 | 881k | out[i] = (coef[i] * in[i] + 16384) >> 15; |
439 | 88.1k | } |
440 | | |
441 | | static void lsp_interpolate(int16_t *syntdenum, int16_t *weightdenum, |
442 | | const int16_t *lsfdeq, int16_t length, |
443 | | ILBCContext *s) |
444 | 18.9k | { |
445 | 18.9k | int16_t lp[LPC_FILTERORDER + 1]; |
446 | 18.9k | const int16_t *const lsfdeq2 = lsfdeq + length; |
447 | 18.9k | int i, pos, lp_length; |
448 | | |
449 | 18.9k | lp_length = length + 1; |
450 | | |
451 | 18.9k | if (s->mode == 30) { |
452 | 6.19k | lsp_interpolate2polydec(lp, (*s).lsfdeqold, lsfdeq, lsf_weight_30ms[0], length); |
453 | 6.19k | memcpy(syntdenum, lp, lp_length * 2); |
454 | 6.19k | bw_expand(weightdenum, lp, kLpcChirpSyntDenum, lp_length); |
455 | | |
456 | 6.19k | pos = lp_length; |
457 | 37.1k | for (i = 1; i < 6; i++) { |
458 | 30.9k | lsp_interpolate2polydec(lp, lsfdeq, lsfdeq2, |
459 | 30.9k | lsf_weight_30ms[i], |
460 | 30.9k | length); |
461 | 30.9k | memcpy(syntdenum + pos, lp, lp_length * 2); |
462 | 30.9k | bw_expand(weightdenum + pos, lp, kLpcChirpSyntDenum, lp_length); |
463 | 30.9k | pos += lp_length; |
464 | 30.9k | } |
465 | 12.7k | } else { |
466 | 12.7k | pos = 0; |
467 | 63.7k | for (i = 0; i < s->nsub; i++) { |
468 | 51.0k | lsp_interpolate2polydec(lp, s->lsfdeqold, lsfdeq, |
469 | 51.0k | lsf_weight_20ms[i], length); |
470 | 51.0k | memcpy(syntdenum + pos, lp, lp_length * 2); |
471 | 51.0k | bw_expand(weightdenum + pos, lp, kLpcChirpSyntDenum, lp_length); |
472 | 51.0k | pos += lp_length; |
473 | 51.0k | } |
474 | 12.7k | } |
475 | | |
476 | 18.9k | if (s->mode == 30) { |
477 | 6.19k | memcpy(s->lsfdeqold, lsfdeq2, length * 2); |
478 | 12.7k | } else { |
479 | 12.7k | memcpy(s->lsfdeqold, lsfdeq, length * 2); |
480 | 12.7k | } |
481 | 18.9k | } |
482 | | |
483 | | static void filter_mafq12(const int16_t *in_ptr, int16_t *out_ptr, |
484 | | const int16_t *B, int16_t B_length, |
485 | | int16_t length) |
486 | 43.7k | { |
487 | 43.7k | int o, i, j; |
488 | | |
489 | 2.16M | for (i = 0; i < length; i++) { |
490 | 2.12M | const int16_t *b_ptr = &B[0]; |
491 | 2.12M | const int16_t *x_ptr = &in_ptr[i]; |
492 | | |
493 | 2.12M | o = 0; |
494 | 22.9M | for (j = 0; j < B_length; j++) |
495 | 20.8M | o += b_ptr[j] * *x_ptr--; |
496 | | |
497 | 2.12M | o = av_clip(o, -134217728, 134215679); |
498 | | |
499 | 2.12M | out_ptr[i] = ((o + 2048) >> 12); |
500 | 2.12M | } |
501 | 43.7k | } |
502 | | |
503 | | static void filter_arfq12(const int16_t *data_in, |
504 | | int16_t *data_out, |
505 | | const int16_t *coefficients, |
506 | | int coefficients_length, |
507 | | int data_length) |
508 | 853k | { |
509 | 853k | int i, j; |
510 | | |
511 | 36.3M | for (i = 0; i < data_length; i++) { |
512 | 35.5M | int output = 0, sum = 0; |
513 | | |
514 | 390M | for (j = coefficients_length - 1; j > 0; j--) { |
515 | 355M | sum += (unsigned)(coefficients[j] * data_out[i - j]); |
516 | 355M | } |
517 | | |
518 | 35.5M | output = coefficients[0] * data_in[i] - (unsigned)sum; |
519 | 35.5M | output = av_clip(output, -134217728, 134215679); |
520 | | |
521 | 35.5M | data_out[i] = (output + 2048) >> 12; |
522 | 35.5M | } |
523 | 853k | } |
524 | | |
525 | | static void state_construct(int16_t ifm, const int16_t *idx, |
526 | | const int16_t *synt_denum, int16_t *Out_fix, |
527 | | int16_t len) |
528 | 18.9k | { |
529 | 18.9k | int k; |
530 | 18.9k | int16_t maxVal; |
531 | 18.9k | int16_t *tmp1, *tmp3; |
532 | 18.9k | const int16_t *tmp2; |
533 | | /* Stack based */ |
534 | 18.9k | int16_t numerator[1 + LPC_FILTERORDER]; |
535 | 18.9k | int16_t sampleValVec[2 * STATE_SHORT_LEN_30MS + LPC_FILTERORDER]; |
536 | 18.9k | int16_t sampleMaVec[2 * STATE_SHORT_LEN_30MS + LPC_FILTERORDER]; |
537 | 18.9k | int16_t *sampleVal = &sampleValVec[LPC_FILTERORDER]; |
538 | 18.9k | int16_t *sampleMa = &sampleMaVec[LPC_FILTERORDER]; |
539 | 18.9k | int16_t *sampleAr = &sampleValVec[LPC_FILTERORDER]; |
540 | | |
541 | | /* initialization of coefficients */ |
542 | | |
543 | 227k | for (k = 0; k < LPC_FILTERORDER + 1; k++) { |
544 | 208k | numerator[k] = synt_denum[LPC_FILTERORDER - k]; |
545 | 208k | } |
546 | | |
547 | | /* decoding of the maximum value */ |
548 | | |
549 | 18.9k | maxVal = frg_quant_mod[ifm]; |
550 | | |
551 | | /* decoding of the sample values */ |
552 | 18.9k | tmp1 = sampleVal; |
553 | 18.9k | tmp2 = &idx[len - 1]; |
554 | | |
555 | 18.9k | if (ifm < 37) { |
556 | 530k | for (k = 0; k < len; k++) { |
557 | | /*the shifting is due to the Q13 in sq4_fixQ13[i], also the adding of 2097152 (= 0.5 << 22) |
558 | | maxVal is in Q8 and result is in Q(-1) */ |
559 | 521k | (*tmp1) = (int16_t) ((SPL_MUL_16_16(maxVal, ilbc_state[(*tmp2)]) + 2097152) >> 22); |
560 | 521k | tmp1++; |
561 | 521k | tmp2--; |
562 | 521k | } |
563 | 9.84k | } else if (ifm < 59) { |
564 | 447k | for (k = 0; k < len; k++) { |
565 | | /*the shifting is due to the Q13 in sq4_fixQ13[i], also the adding of 262144 (= 0.5 << 19) |
566 | | maxVal is in Q5 and result is in Q(-1) */ |
567 | 439k | (*tmp1) = (int16_t) ((SPL_MUL_16_16(maxVal, ilbc_state[(*tmp2)]) + 262144) >> 19); |
568 | 439k | tmp1++; |
569 | 439k | tmp2--; |
570 | 439k | } |
571 | 7.65k | } else { |
572 | 127k | for (k = 0; k < len; k++) { |
573 | | /*the shifting is due to the Q13 in sq4_fixQ13[i], also the adding of 65536 (= 0.5 << 17) |
574 | | maxVal is in Q3 and result is in Q(-1) */ |
575 | 125k | (*tmp1) = (int16_t) ((SPL_MUL_16_16(maxVal, ilbc_state[(*tmp2)]) + 65536) >> 17); |
576 | 125k | tmp1++; |
577 | 125k | tmp2--; |
578 | 125k | } |
579 | 2.19k | } |
580 | | |
581 | | /* Set the rest of the data to zero */ |
582 | 18.9k | memset(&sampleVal[len], 0, len * 2); |
583 | | |
584 | | /* circular convolution with all-pass filter */ |
585 | | |
586 | | /* Set the state to zero */ |
587 | 18.9k | memset(sampleValVec, 0, LPC_FILTERORDER * 2); |
588 | | |
589 | | /* Run MA filter + AR filter */ |
590 | 18.9k | filter_mafq12(sampleVal, sampleMa, numerator, LPC_FILTERORDER + 1, len + LPC_FILTERORDER); |
591 | 18.9k | memset(&sampleMa[len + LPC_FILTERORDER], 0, (len - LPC_FILTERORDER) * 2); |
592 | 18.9k | filter_arfq12(sampleMa, sampleAr, synt_denum, LPC_FILTERORDER + 1, 2 * len); |
593 | | |
594 | 18.9k | tmp1 = &sampleAr[len - 1]; |
595 | 18.9k | tmp2 = &sampleAr[2 * len - 1]; |
596 | 18.9k | tmp3 = Out_fix; |
597 | 1.10M | for (k = 0; k < len; k++) { |
598 | 1.08M | (*tmp3) = (*tmp1) + (*tmp2); |
599 | 1.08M | tmp1--; |
600 | 1.08M | tmp2--; |
601 | 1.08M | tmp3++; |
602 | 1.08M | } |
603 | 18.9k | } |
604 | | |
605 | | static int16_t gain_dequantization(int index, int max_in, int stage) |
606 | 207k | { |
607 | 207k | int16_t scale = FFMAX(1638, FFABS(max_in)); |
608 | | |
609 | 207k | return ((scale * ilbc_gain[stage][index]) + 8192) >> 14; |
610 | 207k | } |
611 | | |
612 | | static void vector_rmultiplication(int16_t *out, const int16_t *in, |
613 | | const int16_t *win, |
614 | | int length, int shift) |
615 | 10.2k | { |
616 | 50.4k | for (int i = 0; i < length; i++) |
617 | 40.2k | out[i] = (in[i] * win[-i]) >> shift; |
618 | 10.2k | } |
619 | | |
620 | | static void vector_multiplication(int16_t *out, const int16_t *in, |
621 | | const int16_t *win, int length, |
622 | | int shift) |
623 | 10.2k | { |
624 | 50.4k | for (int i = 0; i < length; i++) |
625 | 40.2k | out[i] = (in[i] * win[i]) >> shift; |
626 | 10.2k | } |
627 | | |
628 | | static void add_vector_and_shift(int16_t *out, const int16_t *in1, |
629 | | const int16_t *in2, int length, |
630 | | int shift) |
631 | 10.2k | { |
632 | 50.4k | for (int i = 0; i < length; i++) |
633 | 40.2k | out[i] = (in1[i] + in2[i]) >> shift; |
634 | 10.2k | } |
635 | | |
636 | | static void create_augmented_vector(int index, const int16_t *buffer, int16_t *cbVec) |
637 | 10.2k | { |
638 | 10.2k | int16_t cbVecTmp[4]; |
639 | 10.2k | int interpolation_length = FFMIN(4, index); |
640 | 10.2k | int16_t ilow = index - interpolation_length; |
641 | | |
642 | 10.2k | memcpy(cbVec, buffer - index, index * 2); |
643 | | |
644 | 10.2k | vector_multiplication(&cbVec[ilow], buffer - index - interpolation_length, alpha, interpolation_length, 15); |
645 | 10.2k | vector_rmultiplication(cbVecTmp, buffer - interpolation_length, &alpha[interpolation_length - 1], interpolation_length, 15); |
646 | 10.2k | add_vector_and_shift(&cbVec[ilow], &cbVec[ilow], cbVecTmp, interpolation_length, 0); |
647 | | |
648 | 10.2k | memcpy(cbVec + index, buffer - index, FFMIN(SUBL - index, index) * sizeof(*cbVec)); |
649 | 10.2k | } |
650 | | |
651 | | static void get_codebook(int16_t * cbvec, /* (o) Constructed codebook vector */ |
652 | | int16_t * mem, /* (i) Codebook buffer */ |
653 | | int16_t index, /* (i) Codebook index */ |
654 | | int16_t lMem, /* (i) Length of codebook buffer */ |
655 | | int16_t cbveclen /* (i) Codebook vector length */ |
656 | | ) |
657 | 207k | { |
658 | 207k | int16_t k, base_size; |
659 | 207k | int16_t lag; |
660 | | /* Stack based */ |
661 | 207k | int16_t tempbuff2[SUBL + 5] = {0}; |
662 | | |
663 | | /* Determine size of codebook sections */ |
664 | 207k | base_size = lMem - cbveclen + 1; |
665 | | |
666 | 207k | if (cbveclen == SUBL) { |
667 | 150k | base_size += cbveclen / 2; |
668 | 150k | } |
669 | | |
670 | | /* No filter -> First codebook section */ |
671 | 207k | if (index < lMem - cbveclen + 1) { |
672 | | /* first non-interpolated vectors */ |
673 | | |
674 | 179k | k = index + cbveclen; |
675 | | /* get vector */ |
676 | 179k | memcpy(cbvec, mem + lMem - k, cbveclen * 2); |
677 | 179k | } else if (index < base_size) { |
678 | 3.35k | memset(cbvec, 0, cbveclen * 2); |
679 | | |
680 | | /* Calculate lag */ |
681 | | |
682 | 3.35k | k = (int16_t) SPL_MUL_16_16(2, (index - (lMem - cbveclen + 1))) + cbveclen; |
683 | | |
684 | 3.35k | lag = k / 2; |
685 | | |
686 | 3.35k | create_augmented_vector(lag, mem + lMem, cbvec); |
687 | 24.7k | } else { |
688 | 24.7k | int16_t memIndTest; |
689 | | |
690 | | /* first non-interpolated vectors */ |
691 | | |
692 | 24.7k | if (index - base_size < lMem - cbveclen + 1) { |
693 | | |
694 | | /* Set up filter memory, stuff zeros outside memory buffer */ |
695 | | |
696 | 17.8k | memIndTest = lMem - (index - base_size + cbveclen); |
697 | | |
698 | 17.8k | memset(mem - CB_HALFFILTERLEN, 0, CB_HALFFILTERLEN * 2); |
699 | 17.8k | memset(mem + lMem, 0, CB_HALFFILTERLEN * 2); |
700 | | |
701 | | /* do filtering to get the codebook vector */ |
702 | | |
703 | 17.8k | filter_mafq12(&mem[memIndTest + 4], cbvec, kCbFiltersRev, CB_FILTERLEN, cbveclen); |
704 | 17.8k | } else { |
705 | 6.91k | memset(cbvec, 0, cbveclen * 2); |
706 | | /* interpolated vectors */ |
707 | | /* Stuff zeros outside memory buffer */ |
708 | 6.91k | memIndTest = lMem - cbveclen - CB_FILTERLEN; |
709 | 6.91k | memset(mem + lMem, 0, CB_HALFFILTERLEN * 2); |
710 | | |
711 | | /* do filtering */ |
712 | 6.91k | filter_mafq12(&mem[memIndTest + 7], tempbuff2, kCbFiltersRev, CB_FILTERLEN, (int16_t) (cbveclen + 5)); |
713 | | |
714 | | /* Calculate lag index */ |
715 | 6.91k | lag = (cbveclen << 1) - 20 + index - base_size - lMem - 1; |
716 | | |
717 | 6.91k | create_augmented_vector(lag, tempbuff2 + SUBL + 5, cbvec); |
718 | 6.91k | } |
719 | 24.7k | } |
720 | 207k | } |
721 | | |
722 | | static void construct_vector ( |
723 | | int16_t *decvector, /* (o) Decoded vector */ |
724 | | const int16_t *index, /* (i) Codebook indices */ |
725 | | const int16_t *gain_index, /* (i) Gain quantization indices */ |
726 | | int16_t *mem, /* (i) Buffer for codevector construction */ |
727 | | int16_t lMem, /* (i) Length of buffer */ |
728 | | int16_t veclen) |
729 | 69.2k | { |
730 | 69.2k | int16_t gain[CB_NSTAGES]; |
731 | 69.2k | int16_t cbvec0[SUBL]; |
732 | 69.2k | int16_t cbvec1[SUBL]; |
733 | 69.2k | int16_t cbvec2[SUBL]; |
734 | 69.2k | unsigned a32; |
735 | 69.2k | int16_t *gainPtr; |
736 | 69.2k | int j; |
737 | | |
738 | | /* gain de-quantization */ |
739 | | |
740 | 69.2k | gain[0] = gain_dequantization(gain_index[0], 16384, 0); |
741 | 69.2k | gain[1] = gain_dequantization(gain_index[1], gain[0], 1); |
742 | 69.2k | gain[2] = gain_dequantization(gain_index[2], gain[1], 2); |
743 | | |
744 | | /* codebook vector construction and construction of total vector */ |
745 | | |
746 | | /* Stack based */ |
747 | 69.2k | get_codebook(cbvec0, mem, index[0], lMem, veclen); |
748 | 69.2k | get_codebook(cbvec1, mem, index[1], lMem, veclen); |
749 | 69.2k | get_codebook(cbvec2, mem, index[2], lMem, veclen); |
750 | | |
751 | 69.2k | gainPtr = &gain[0]; |
752 | 2.51M | for (j = 0; j < veclen; j++) { |
753 | 2.44M | a32 = SPL_MUL_16_16(*gainPtr++, cbvec0[j]); |
754 | 2.44M | a32 += SPL_MUL_16_16(*gainPtr++, cbvec1[j]); |
755 | 2.44M | a32 += SPL_MUL_16_16(*gainPtr, cbvec2[j]); |
756 | 2.44M | gainPtr -= 2; |
757 | 2.44M | decvector[j] = (int)(a32 + 8192) >> 14; |
758 | 2.44M | } |
759 | 69.2k | } |
760 | | |
761 | | static void reverse_memcpy(int16_t *dest, const int16_t *source, int length) |
762 | 41.8k | { |
763 | 41.8k | int16_t* destPtr = dest; |
764 | 41.8k | const int16_t *sourcePtr = source; |
765 | 41.8k | int j; |
766 | | |
767 | 2.88M | for (j = 0; j < length; j++) |
768 | 2.84M | *destPtr-- = *sourcePtr++; |
769 | 41.8k | } |
770 | | |
771 | | static void decode_residual(ILBCContext *s, |
772 | | ILBCFrame *encbits, |
773 | | int16_t *decresidual, |
774 | | const int16_t *syntdenum) |
775 | 18.9k | { |
776 | 18.9k | int16_t meml_gotten, Nfor, Nback, diff, start_pos; |
777 | 18.9k | int16_t subcount, subframe; |
778 | 18.9k | int16_t *reverseDecresidual = s->enh_buf; /* Reversed decoded data, used for decoding backwards in time (reuse memory in state) */ |
779 | 18.9k | int16_t *memVec = s->prevResidual; |
780 | 18.9k | int16_t *mem = &memVec[CB_HALFFILTERLEN]; /* Memory for codebook */ |
781 | | |
782 | 18.9k | diff = STATE_LEN - s->state_short_len; |
783 | | |
784 | 18.9k | if (encbits->state_first == 1) { |
785 | 10.2k | start_pos = (encbits->start - 1) * SUBL; |
786 | 10.2k | } else { |
787 | 8.73k | start_pos = (encbits->start - 1) * SUBL + diff; |
788 | 8.73k | } |
789 | | |
790 | | /* decode scalar part of start state */ |
791 | | |
792 | 18.9k | state_construct(encbits->ifm, encbits->idx, &syntdenum[(encbits->start - 1) * (LPC_FILTERORDER + 1)], &decresidual[start_pos], s->state_short_len); |
793 | | |
794 | 18.9k | if (encbits->state_first) { /* put adaptive part in the end */ |
795 | | /* setup memory */ |
796 | 10.2k | memset(mem, 0, (int16_t) (CB_MEML - s->state_short_len) * 2); |
797 | 10.2k | memcpy(mem + CB_MEML - s->state_short_len, decresidual + start_pos, s->state_short_len * 2); |
798 | | |
799 | | /* construct decoded vector */ |
800 | | |
801 | 10.2k | construct_vector(&decresidual[start_pos + s->state_short_len], encbits->cb_index, encbits->gain_index, mem + CB_MEML - ST_MEM_L_TBL, ST_MEM_L_TBL, (int16_t) diff); |
802 | | |
803 | 10.2k | } else { /* put adaptive part in the beginning */ |
804 | | /* setup memory */ |
805 | 8.73k | meml_gotten = s->state_short_len; |
806 | 8.73k | reverse_memcpy(mem + CB_MEML - 1, decresidual + start_pos, meml_gotten); |
807 | 8.73k | memset(mem, 0, (int16_t) (CB_MEML - meml_gotten) * 2); |
808 | | |
809 | | /* construct decoded vector */ |
810 | 8.73k | construct_vector(reverseDecresidual, encbits->cb_index, encbits->gain_index, mem + CB_MEML - ST_MEM_L_TBL, ST_MEM_L_TBL, diff); |
811 | | |
812 | | /* get decoded residual from reversed vector */ |
813 | 8.73k | reverse_memcpy(&decresidual[start_pos - 1], reverseDecresidual, diff); |
814 | 8.73k | } |
815 | | |
816 | | /* counter for predicted subframes */ |
817 | 18.9k | subcount = 1; |
818 | | |
819 | | /* forward prediction of subframes */ |
820 | 18.9k | Nfor = s->nsub - encbits->start - 1; |
821 | | |
822 | 18.9k | if (Nfor > 0) { |
823 | | /* setup memory */ |
824 | 13.7k | memset(mem, 0, (CB_MEML - STATE_LEN) * 2); |
825 | 13.7k | memcpy(mem + CB_MEML - STATE_LEN, decresidual + (encbits->start - 1) * SUBL, STATE_LEN * 2); |
826 | | |
827 | | /* loop over subframes to encode */ |
828 | 44.7k | for (subframe = 0; subframe < Nfor; subframe++) { |
829 | | /* construct decoded vector */ |
830 | 31.0k | construct_vector(&decresidual[(encbits->start + 1 + subframe) * SUBL], encbits->cb_index + subcount * CB_NSTAGES, encbits->gain_index + subcount * CB_NSTAGES, mem, MEM_LF_TBL, SUBL); |
831 | | |
832 | | /* update memory */ |
833 | 31.0k | memmove(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(*mem)); |
834 | 31.0k | memcpy(mem + CB_MEML - SUBL, &decresidual[(encbits->start + 1 + subframe) * SUBL], SUBL * 2); |
835 | | |
836 | 31.0k | subcount++; |
837 | 31.0k | } |
838 | | |
839 | 13.7k | } |
840 | | |
841 | | /* backward prediction of subframes */ |
842 | 18.9k | Nback = encbits->start - 1; |
843 | | |
844 | 18.9k | if (Nback > 0) { |
845 | | /* setup memory */ |
846 | 12.1k | meml_gotten = SUBL * (s->nsub + 1 - encbits->start); |
847 | 12.1k | if (meml_gotten > CB_MEML) { |
848 | 4.67k | meml_gotten = CB_MEML; |
849 | 4.67k | } |
850 | | |
851 | 12.1k | reverse_memcpy(mem + CB_MEML - 1, decresidual + (encbits->start - 1) * SUBL, meml_gotten); |
852 | 12.1k | memset(mem, 0, (int16_t) (CB_MEML - meml_gotten) * 2); |
853 | | |
854 | | /* loop over subframes to decode */ |
855 | 31.4k | for (subframe = 0; subframe < Nback; subframe++) { |
856 | | /* construct decoded vector */ |
857 | 19.2k | construct_vector(&reverseDecresidual[subframe * SUBL], encbits->cb_index + subcount * CB_NSTAGES, |
858 | 19.2k | encbits->gain_index + subcount * CB_NSTAGES, mem, MEM_LF_TBL, SUBL); |
859 | | |
860 | | /* update memory */ |
861 | 19.2k | memmove(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(*mem)); |
862 | 19.2k | memcpy(mem + CB_MEML - SUBL, &reverseDecresidual[subframe * SUBL], SUBL * 2); |
863 | | |
864 | 19.2k | subcount++; |
865 | 19.2k | } |
866 | | |
867 | | /* get decoded residual from reversed vector */ |
868 | 12.1k | reverse_memcpy(decresidual + SUBL * Nback - 1, reverseDecresidual, SUBL * Nback); |
869 | 12.1k | } |
870 | 18.9k | } |
871 | | |
872 | | static int16_t max_abs_value_w16(const int16_t* vector, int length) |
873 | 196k | { |
874 | 196k | int i = 0, absolute = 0, maximum = 0; |
875 | | |
876 | 196k | if (vector == NULL || length <= 0) { |
877 | 0 | return -1; |
878 | 0 | } |
879 | | |
880 | 28.4M | for (i = 0; i < length; i++) { |
881 | 28.3M | absolute = FFABS(vector[i]); |
882 | 28.3M | if (absolute > maximum) |
883 | 1.27M | maximum = absolute; |
884 | 28.3M | } |
885 | | |
886 | | // Guard the case for abs(-32768). |
887 | 196k | return FFMIN(maximum, INT16_MAX); |
888 | 196k | } |
889 | | |
890 | | static int16_t get_size_in_bits(uint32_t n) |
891 | 0 | { |
892 | 0 | int16_t bits; |
893 | |
|
894 | 0 | if (0xFFFF0000 & n) { |
895 | 0 | bits = 16; |
896 | 0 | } else { |
897 | 0 | bits = 0; |
898 | 0 | } |
899 | |
|
900 | 0 | if (0x0000FF00 & (n >> bits)) bits += 8; |
901 | 0 | if (0x000000F0 & (n >> bits)) bits += 4; |
902 | 0 | if (0x0000000C & (n >> bits)) bits += 2; |
903 | 0 | if (0x00000002 & (n >> bits)) bits += 1; |
904 | 0 | if (0x00000001 & (n >> bits)) bits += 1; |
905 | |
|
906 | 0 | return bits; |
907 | 0 | } |
908 | | |
909 | | static int32_t scale_dot_product(const int16_t *v1, const int16_t *v2, int length, int scaling) |
910 | 16.4M | { |
911 | 16.4M | int64_t sum = 0; |
912 | | |
913 | 1.04G | for (int i = 0; i < length; i++) |
914 | 1.03G | sum += (v1[i] * v2[i]) >> scaling; |
915 | | |
916 | 16.4M | return av_clipl_int32(sum); |
917 | 16.4M | } |
918 | | |
919 | | static void correlation(int32_t *corr, int32_t *ener, const int16_t *buffer, |
920 | | int16_t lag, int16_t blen, int16_t srange, int16_t scale) |
921 | 0 | { |
922 | 0 | const int16_t *w16ptr = &buffer[blen - srange - lag]; |
923 | |
|
924 | 0 | *corr = scale_dot_product(&buffer[blen - srange], w16ptr, srange, scale); |
925 | 0 | *ener = scale_dot_product(w16ptr, w16ptr, srange, scale); |
926 | |
|
927 | 0 | if (*ener == 0) { |
928 | 0 | *corr = 0; |
929 | 0 | *ener = 1; |
930 | 0 | } |
931 | 0 | } |
932 | | |
933 | 11.5M | #define SPL_SHIFT_W32(x, c) (((c) >= 0) ? ((x) << (c)) : ((x) >> (-(c)))) |
934 | | |
935 | | static int16_t norm_w32(int32_t a) |
936 | 11.5M | { |
937 | 11.5M | if (a == 0) { |
938 | 0 | return 0; |
939 | 11.5M | } else if (a < 0) { |
940 | 0 | a = ~a; |
941 | 0 | } |
942 | | |
943 | 11.5M | return ff_clz(a); |
944 | 11.5M | } |
945 | | |
946 | | static int32_t div_w32_w16(int32_t num, int16_t den) |
947 | 0 | { |
948 | 0 | if (den != 0) |
949 | 0 | return num / den; |
950 | 0 | else |
951 | 0 | return 0x7FFFFFFF; |
952 | 0 | } |
953 | | |
954 | | static void do_plc(int16_t *plc_residual, /* (o) concealed residual */ |
955 | | int16_t *plc_lpc, /* (o) concealed LP parameters */ |
956 | | int16_t PLI, /* (i) packet loss indicator |
957 | | 0 - no PL, 1 = PL */ |
958 | | const int16_t *decresidual, /* (i) decoded residual */ |
959 | | const int16_t *lpc, /* (i) decoded LPC (only used for no PL) */ |
960 | | int16_t inlag, /* (i) pitch lag */ |
961 | | ILBCContext *s) /* (i/o) decoder instance */ |
962 | 18.9k | { |
963 | 18.9k | int16_t i, pick; |
964 | 18.9k | int32_t cross, ener, cross_comp, ener_comp = 0; |
965 | 18.9k | int32_t measure, max_measure, energy; |
966 | 18.9k | int16_t max, cross_square_max, cross_square; |
967 | 18.9k | int16_t j, lag, tmp1, tmp2, randlag; |
968 | 18.9k | int16_t shift1, shift2, shift3, shift_max; |
969 | 18.9k | int16_t scale3; |
970 | 18.9k | int16_t corrLen; |
971 | 18.9k | int32_t tmpW32, tmp2W32; |
972 | 18.9k | int16_t use_gain; |
973 | 18.9k | int16_t tot_gain; |
974 | 18.9k | int16_t max_perSquare; |
975 | 18.9k | int16_t scale1, scale2; |
976 | 18.9k | int16_t totscale; |
977 | 18.9k | int32_t nom; |
978 | 18.9k | int16_t denom; |
979 | 18.9k | int16_t pitchfact; |
980 | 18.9k | int16_t use_lag; |
981 | 18.9k | int ind; |
982 | 18.9k | int16_t randvec[BLOCKL_MAX]; |
983 | | |
984 | | /* Packet Loss */ |
985 | 18.9k | if (PLI == 1) { |
986 | |
|
987 | 0 | s->consPLICount += 1; |
988 | | |
989 | | /* if previous frame not lost, |
990 | | determine pitch pred. gain */ |
991 | |
|
992 | 0 | if (s->prevPLI != 1) { |
993 | | |
994 | | /* Maximum 60 samples are correlated, preserve as high accuracy |
995 | | as possible without getting overflow */ |
996 | 0 | max = max_abs_value_w16(s->prevResidual, s->block_samples); |
997 | 0 | scale3 = (get_size_in_bits(max) << 1) - 25; |
998 | 0 | if (scale3 < 0) { |
999 | 0 | scale3 = 0; |
1000 | 0 | } |
1001 | | |
1002 | | /* Store scale for use when interpolating between the |
1003 | | * concealment and the received packet */ |
1004 | 0 | s->prevScale = scale3; |
1005 | | |
1006 | | /* Search around the previous lag +/-3 to find the |
1007 | | best pitch period */ |
1008 | 0 | lag = inlag - 3; |
1009 | | |
1010 | | /* Guard against getting outside the frame */ |
1011 | 0 | corrLen = FFMIN(60, s->block_samples - (inlag + 3)); |
1012 | |
|
1013 | 0 | correlation(&cross, &ener, s->prevResidual, lag, s->block_samples, corrLen, scale3); |
1014 | | |
1015 | | /* Normalize and store cross^2 and the number of shifts */ |
1016 | 0 | shift_max = get_size_in_bits(FFABS(cross)) - 15; |
1017 | 0 | cross_square_max = (int16_t) SPL_MUL_16_16_RSFT(SPL_SHIFT_W32(cross, -shift_max), SPL_SHIFT_W32(cross, -shift_max), 15); |
1018 | |
|
1019 | 0 | for (j = inlag - 2; j <= inlag + 3; j++) { |
1020 | 0 | correlation(&cross_comp, &ener_comp, s->prevResidual, j, s->block_samples, corrLen, scale3); |
1021 | | |
1022 | | /* Use the criteria (corr*corr)/energy to compare if |
1023 | | this lag is better or not. To avoid the division, |
1024 | | do a cross multiplication */ |
1025 | 0 | shift1 = get_size_in_bits(FFABS(cross_comp)) - 15; |
1026 | 0 | cross_square = (int16_t) SPL_MUL_16_16_RSFT(SPL_SHIFT_W32(cross_comp, -shift1), SPL_SHIFT_W32(cross_comp, -shift1), 15); |
1027 | |
|
1028 | 0 | shift2 = get_size_in_bits(ener) - 15; |
1029 | 0 | measure = SPL_MUL_16_16(SPL_SHIFT_W32(ener, -shift2), cross_square); |
1030 | |
|
1031 | 0 | shift3 = get_size_in_bits(ener_comp) - 15; |
1032 | 0 | max_measure = SPL_MUL_16_16(SPL_SHIFT_W32(ener_comp, -shift3), cross_square_max); |
1033 | | |
1034 | | /* Calculate shift value, so that the two measures can |
1035 | | be put in the same Q domain */ |
1036 | 0 | if (((shift_max << 1) + shift3) > ((shift1 << 1) + shift2)) { |
1037 | 0 | tmp1 = FFMIN(31, (shift_max << 1) + shift3 - (shift1 << 1) - shift2); |
1038 | 0 | tmp2 = 0; |
1039 | 0 | } else { |
1040 | 0 | tmp1 = 0; |
1041 | 0 | tmp2 = FFMIN(31, (shift1 << 1) + shift2 - (shift_max << 1) - shift3); |
1042 | 0 | } |
1043 | |
|
1044 | 0 | if ((measure >> tmp1) > (max_measure >> tmp2)) { |
1045 | | /* New lag is better => record lag, measure and domain */ |
1046 | 0 | lag = j; |
1047 | 0 | cross_square_max = cross_square; |
1048 | 0 | cross = cross_comp; |
1049 | 0 | shift_max = shift1; |
1050 | 0 | ener = ener_comp; |
1051 | 0 | } |
1052 | 0 | } |
1053 | | |
1054 | | /* Calculate the periodicity for the lag with the maximum correlation. |
1055 | | |
1056 | | Definition of the periodicity: |
1057 | | abs(corr(vec1, vec2))/(sqrt(energy(vec1))*sqrt(energy(vec2))) |
1058 | | |
1059 | | Work in the Square domain to simplify the calculations |
1060 | | max_perSquare is less than 1 (in Q15) |
1061 | | */ |
1062 | 0 | tmp2W32 = scale_dot_product(&s->prevResidual[s->block_samples - corrLen], &s->prevResidual[s->block_samples - corrLen], corrLen, scale3); |
1063 | |
|
1064 | 0 | if ((tmp2W32 > 0) && (ener_comp > 0)) { |
1065 | | /* norm energies to int16_t, compute the product of the energies and |
1066 | | use the upper int16_t as the denominator */ |
1067 | |
|
1068 | 0 | scale1 = norm_w32(tmp2W32) - 16; |
1069 | 0 | tmp1 = SPL_SHIFT_W32(tmp2W32, scale1); |
1070 | |
|
1071 | 0 | scale2 = norm_w32(ener) - 16; |
1072 | 0 | tmp2 = SPL_SHIFT_W32(ener, scale2); |
1073 | 0 | denom = SPL_MUL_16_16_RSFT(tmp1, tmp2, 16); /* denom in Q(scale1+scale2-16) */ |
1074 | | |
1075 | | /* Square the cross correlation and norm it such that max_perSquare |
1076 | | will be in Q15 after the division */ |
1077 | |
|
1078 | 0 | totscale = scale1 + scale2 - 1; |
1079 | 0 | tmp1 = SPL_SHIFT_W32(cross, (totscale >> 1)); |
1080 | 0 | tmp2 = SPL_SHIFT_W32(cross, totscale - (totscale >> 1)); |
1081 | |
|
1082 | 0 | nom = SPL_MUL_16_16(tmp1, tmp2); |
1083 | 0 | max_perSquare = div_w32_w16(nom, denom); |
1084 | 0 | } else { |
1085 | 0 | max_perSquare = 0; |
1086 | 0 | } |
1087 | 0 | } else { |
1088 | | /* previous frame lost, use recorded lag and gain */ |
1089 | 0 | lag = s->prevLag; |
1090 | 0 | max_perSquare = s->per_square; |
1091 | 0 | } |
1092 | | |
1093 | | /* Attenuate signal and scale down pitch pred gain if |
1094 | | several frames lost consecutively */ |
1095 | |
|
1096 | 0 | use_gain = 32767; /* 1.0 in Q15 */ |
1097 | |
|
1098 | 0 | if (s->consPLICount * s->block_samples > 320) { |
1099 | 0 | use_gain = 29491; /* 0.9 in Q15 */ |
1100 | 0 | } |
1101 | | |
1102 | | /* Compute mixing factor of picth repetition and noise: |
1103 | | for max_per>0.7 set periodicity to 1.0 |
1104 | | 0.4<max_per<0.7 set periodicity to (maxper-0.4)/0.7-0.4) |
1105 | | max_per<0.4 set periodicity to 0.0 |
1106 | | */ |
1107 | |
|
1108 | 0 | if (max_perSquare > 7868) { /* periodicity > 0.7 (0.7^4=0.2401 in Q15) */ |
1109 | 0 | pitchfact = 32767; |
1110 | 0 | } else if (max_perSquare > 839) { /* 0.4 < periodicity < 0.7 (0.4^4=0.0256 in Q15) */ |
1111 | | /* find best index and interpolate from that */ |
1112 | 0 | ind = 5; |
1113 | 0 | while ((max_perSquare < kPlcPerSqr[ind]) && (ind > 0)) { |
1114 | 0 | ind--; |
1115 | 0 | } |
1116 | | /* pitch fact is approximated by first order */ |
1117 | 0 | tmpW32 = kPlcPitchFact[ind] + SPL_MUL_16_16_RSFT(kPlcPfSlope[ind], (max_perSquare - kPlcPerSqr[ind]), 11); |
1118 | |
|
1119 | 0 | pitchfact = FFMIN(tmpW32, 32767); /* guard against overflow */ |
1120 | |
|
1121 | 0 | } else { /* periodicity < 0.4 */ |
1122 | 0 | pitchfact = 0; |
1123 | 0 | } |
1124 | | |
1125 | | /* avoid repetition of same pitch cycle (buzzyness) */ |
1126 | 0 | use_lag = lag; |
1127 | 0 | if (lag < 80) { |
1128 | 0 | use_lag = 2 * lag; |
1129 | 0 | } |
1130 | | |
1131 | | /* compute concealed residual */ |
1132 | 0 | energy = 0; |
1133 | |
|
1134 | 0 | for (i = 0; i < s->block_samples; i++) { |
1135 | | /* noise component - 52 < randlagFIX < 117 */ |
1136 | 0 | s->seed = SPL_MUL_16_16(s->seed, 31821) + 13849; |
1137 | 0 | randlag = 53 + (s->seed & 63); |
1138 | |
|
1139 | 0 | pick = i - randlag; |
1140 | |
|
1141 | 0 | if (pick < 0) { |
1142 | 0 | randvec[i] = s->prevResidual[s->block_samples + pick]; |
1143 | 0 | } else { |
1144 | 0 | randvec[i] = s->prevResidual[pick]; |
1145 | 0 | } |
1146 | | |
1147 | | /* pitch repetition component */ |
1148 | 0 | pick = i - use_lag; |
1149 | |
|
1150 | 0 | if (pick < 0) { |
1151 | 0 | plc_residual[i] = s->prevResidual[s->block_samples + pick]; |
1152 | 0 | } else { |
1153 | 0 | plc_residual[i] = plc_residual[pick]; |
1154 | 0 | } |
1155 | | |
1156 | | /* Attinuate total gain for each 10 ms */ |
1157 | 0 | if (i < 80) { |
1158 | 0 | tot_gain = use_gain; |
1159 | 0 | } else if (i < 160) { |
1160 | 0 | tot_gain = SPL_MUL_16_16_RSFT(31130, use_gain, 15); /* 0.95*use_gain */ |
1161 | 0 | } else { |
1162 | 0 | tot_gain = SPL_MUL_16_16_RSFT(29491, use_gain, 15); /* 0.9*use_gain */ |
1163 | 0 | } |
1164 | | |
1165 | | /* mix noise and pitch repetition */ |
1166 | 0 | plc_residual[i] = SPL_MUL_16_16_RSFT(tot_gain, (pitchfact * plc_residual[i] + (32767 - pitchfact) * randvec[i] + 16384) >> 15, 15); |
1167 | | |
1168 | | /* Shifting down the result one step extra to ensure that no overflow |
1169 | | will occur */ |
1170 | 0 | energy += SPL_MUL_16_16_RSFT(plc_residual[i], plc_residual[i], (s->prevScale + 1)); |
1171 | |
|
1172 | 0 | } |
1173 | | |
1174 | | /* less than 30 dB, use only noise */ |
1175 | 0 | if (energy < SPL_SHIFT_W32(s->block_samples * 900, -s->prevScale - 1)) { |
1176 | 0 | energy = 0; |
1177 | 0 | for (i = 0; i < s->block_samples; i++) { |
1178 | 0 | plc_residual[i] = randvec[i]; |
1179 | 0 | } |
1180 | 0 | } |
1181 | | |
1182 | | /* use the old LPC */ |
1183 | 0 | memcpy(plc_lpc, (*s).prev_lpc, (LPC_FILTERORDER + 1) * 2); |
1184 | | |
1185 | | /* Update state in case there are multiple frame losses */ |
1186 | 0 | s->prevLag = lag; |
1187 | 0 | s->per_square = max_perSquare; |
1188 | 18.9k | } else { /* no packet loss, copy input */ |
1189 | 18.9k | memcpy(plc_residual, decresidual, s->block_samples * 2); |
1190 | 18.9k | memcpy(plc_lpc, lpc, (LPC_FILTERORDER + 1) * 2); |
1191 | 18.9k | s->consPLICount = 0; |
1192 | 18.9k | } |
1193 | | |
1194 | | /* update state */ |
1195 | 18.9k | s->prevPLI = PLI; |
1196 | 18.9k | memcpy(s->prev_lpc, plc_lpc, (LPC_FILTERORDER + 1) * 2); |
1197 | 18.9k | memcpy(s->prevResidual, plc_residual, s->block_samples * 2); |
1198 | | |
1199 | 18.9k | return; |
1200 | 18.9k | } |
1201 | | |
1202 | | static int xcorr_coeff(const int16_t *target, const int16_t *regressor, |
1203 | | int16_t subl, int16_t searchLen, |
1204 | | int16_t offset, int16_t step) |
1205 | 196k | { |
1206 | 196k | int16_t maxlag; |
1207 | 196k | int16_t pos; |
1208 | 196k | int16_t max; |
1209 | 196k | int16_t cross_corr_scale, energy_scale; |
1210 | 196k | int16_t cross_corr_sg_mod, cross_corr_sg_mod_max; |
1211 | 196k | int32_t cross_corr, energy; |
1212 | 196k | int16_t cross_corr_mod, energy_mod, enery_mod_max; |
1213 | 196k | const int16_t *rp; |
1214 | 196k | const int16_t *rp_beg, *rp_end; |
1215 | 196k | int16_t totscale, totscale_max; |
1216 | 196k | int16_t scalediff; |
1217 | 196k | int32_t new_crit, max_crit; |
1218 | 196k | int shifts; |
1219 | 196k | int k; |
1220 | | |
1221 | | /* Initializations, to make sure that the first one is selected */ |
1222 | 196k | cross_corr_sg_mod_max = 0; |
1223 | 196k | enery_mod_max = INT16_MAX; |
1224 | 196k | totscale_max = -500; |
1225 | 196k | maxlag = 0; |
1226 | 196k | pos = 0; |
1227 | | |
1228 | | /* Find scale value and start position */ |
1229 | 196k | if (step == 1) { |
1230 | 0 | max = max_abs_value_w16(regressor, (int16_t) (subl + searchLen - 1)); |
1231 | 0 | rp_beg = regressor; |
1232 | 0 | rp_end = ®ressor[subl]; |
1233 | 196k | } else { /* step== -1 */ |
1234 | 196k | max = max_abs_value_w16(®ressor[-searchLen], (int16_t) (subl + searchLen - 1)); |
1235 | 196k | rp_beg = ®ressor[-1]; |
1236 | 196k | rp_end = ®ressor[subl - 1]; |
1237 | 196k | } |
1238 | | |
1239 | | /* Introduce a scale factor on the energy in int32_t in |
1240 | | order to make sure that the calculation does not |
1241 | | overflow */ |
1242 | | |
1243 | 196k | if (max > 5000) { |
1244 | 12.7k | shifts = 2; |
1245 | 184k | } else { |
1246 | 184k | shifts = 0; |
1247 | 184k | } |
1248 | | |
1249 | | /* Calculate the first energy, then do a +/- to get the other energies */ |
1250 | 196k | energy = scale_dot_product(regressor, regressor, subl, shifts); |
1251 | | |
1252 | 16.4M | for (k = 0; k < searchLen; k++) { |
1253 | 16.2M | rp = ®ressor[pos]; |
1254 | | |
1255 | 16.2M | cross_corr = scale_dot_product(target, rp, subl, shifts); |
1256 | | |
1257 | 16.2M | if ((energy > 0) && (cross_corr > 0)) { |
1258 | | /* Put cross correlation and energy on 16 bit word */ |
1259 | 5.76M | cross_corr_scale = norm_w32(cross_corr) - 16; |
1260 | 5.76M | cross_corr_mod = (int16_t) SPL_SHIFT_W32(cross_corr, cross_corr_scale); |
1261 | 5.76M | energy_scale = norm_w32(energy) - 16; |
1262 | 5.76M | energy_mod = (int16_t) SPL_SHIFT_W32(energy, energy_scale); |
1263 | | |
1264 | | /* Square cross correlation and store upper int16_t */ |
1265 | 5.76M | cross_corr_sg_mod = (int16_t) SPL_MUL_16_16_RSFT(cross_corr_mod, cross_corr_mod, 16); |
1266 | | |
1267 | | /* Calculate the total number of (dynamic) right shifts that have |
1268 | | been performed on (cross_corr*cross_corr)/energy |
1269 | | */ |
1270 | 5.76M | totscale = energy_scale - (cross_corr_scale * 2); |
1271 | | |
1272 | | /* Calculate the shift difference in order to be able to compare the two |
1273 | | (cross_corr*cross_corr)/energy in the same domain |
1274 | | */ |
1275 | 5.76M | scalediff = totscale - totscale_max; |
1276 | 5.76M | scalediff = FFMIN(scalediff, 31); |
1277 | 5.76M | scalediff = FFMAX(scalediff, -31); |
1278 | | |
1279 | | /* Compute the cross multiplication between the old best criteria |
1280 | | and the new one to be able to compare them without using a |
1281 | | division */ |
1282 | | |
1283 | 5.76M | if (scalediff < 0) { |
1284 | 888k | new_crit = ((int32_t) cross_corr_sg_mod * enery_mod_max) >> (-scalediff); |
1285 | 888k | max_crit = ((int32_t) cross_corr_sg_mod_max * energy_mod); |
1286 | 4.88M | } else { |
1287 | 4.88M | new_crit = ((int32_t) cross_corr_sg_mod * enery_mod_max); |
1288 | 4.88M | max_crit = ((int32_t) cross_corr_sg_mod_max * energy_mod) >> scalediff; |
1289 | 4.88M | } |
1290 | | |
1291 | | /* Store the new lag value if the new criteria is larger |
1292 | | than previous largest criteria */ |
1293 | | |
1294 | 5.76M | if (new_crit > max_crit) { |
1295 | 677k | cross_corr_sg_mod_max = cross_corr_sg_mod; |
1296 | 677k | enery_mod_max = energy_mod; |
1297 | 677k | totscale_max = totscale; |
1298 | 677k | maxlag = k; |
1299 | 677k | } |
1300 | 5.76M | } |
1301 | 16.2M | pos += step; |
1302 | | |
1303 | | /* Do a +/- to get the next energy */ |
1304 | 16.2M | energy += (unsigned)step * ((*rp_end * *rp_end - *rp_beg * *rp_beg) >> shifts); |
1305 | | |
1306 | 16.2M | rp_beg += step; |
1307 | 16.2M | rp_end += step; |
1308 | 16.2M | } |
1309 | | |
1310 | 196k | return maxlag + offset; |
1311 | 196k | } |
1312 | | |
1313 | | static void hp_output(int16_t *signal, const int16_t *ba, int16_t *y, |
1314 | | int16_t *x, int16_t len) |
1315 | 196k | { |
1316 | 196k | int32_t tmp; |
1317 | | |
1318 | 33.5M | for (int i = 0; i < len; i++) { |
1319 | 33.3M | tmp = SPL_MUL_16_16(y[1], ba[3]); /* (-a[1])*y[i-1] (low part) */ |
1320 | 33.3M | tmp += SPL_MUL_16_16(y[3], ba[4]); /* (-a[2])*y[i-2] (low part) */ |
1321 | 33.3M | tmp = (tmp >> 15); |
1322 | 33.3M | tmp += SPL_MUL_16_16(y[0], ba[3]); /* (-a[1])*y[i-1] (high part) */ |
1323 | 33.3M | tmp += SPL_MUL_16_16(y[2], ba[4]); /* (-a[2])*y[i-2] (high part) */ |
1324 | 33.3M | tmp = (tmp * 2); |
1325 | | |
1326 | 33.3M | tmp += SPL_MUL_16_16(signal[i], ba[0]); /* b[0]*x[0] */ |
1327 | 33.3M | tmp += SPL_MUL_16_16(x[0], ba[1]); /* b[1]*x[i-1] */ |
1328 | 33.3M | tmp += SPL_MUL_16_16(x[1], ba[2]); /* b[2]*x[i-2] */ |
1329 | | |
1330 | | /* Update state (input part) */ |
1331 | 33.3M | x[1] = x[0]; |
1332 | 33.3M | x[0] = signal[i]; |
1333 | | |
1334 | | /* Convert back to Q0 and multiply with 2 */ |
1335 | 33.3M | signal[i] = av_clip_intp2(tmp + 1024, 26) >> 11; |
1336 | | |
1337 | | /* Update state (filtered part) */ |
1338 | 33.3M | y[2] = y[0]; |
1339 | 33.3M | y[3] = y[1]; |
1340 | | |
1341 | | /* upshift tmp by 3 with saturation */ |
1342 | 33.3M | if (tmp > 268435455) { |
1343 | 803 | tmp = INT32_MAX; |
1344 | 33.3M | } else if (tmp < -268435456) { |
1345 | 1.22k | tmp = INT32_MIN; |
1346 | 33.3M | } else { |
1347 | 33.3M | tmp = tmp * 8; |
1348 | 33.3M | } |
1349 | | |
1350 | 33.3M | y[0] = tmp >> 16; |
1351 | 33.3M | y[1] = (tmp - (y[0] * (1 << 16))) >> 1; |
1352 | 33.3M | } |
1353 | 196k | } |
1354 | | |
1355 | | static int ilbc_decode_frame(AVCodecContext *avctx, AVFrame *frame, |
1356 | | int *got_frame_ptr, AVPacket *avpkt) |
1357 | 196k | { |
1358 | 196k | ILBCContext *s = avctx->priv_data; |
1359 | 196k | int mode = s->mode, ret; |
1360 | 196k | int16_t *plc_data = &s->plc_residual[LPC_FILTERORDER]; |
1361 | | |
1362 | 196k | memset(&s->frame, 0, sizeof(ILBCFrame)); |
1363 | 196k | ret = unpack_frame(s, avpkt->data, avpkt->size); |
1364 | 196k | if (ret < 0) |
1365 | 0 | return ret; |
1366 | 196k | if (ret) |
1367 | 1.44k | mode = 0; |
1368 | | |
1369 | 196k | frame->nb_samples = s->block_samples; |
1370 | 196k | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
1371 | 0 | return ret; |
1372 | | |
1373 | 196k | if (s->frame.start < 1 || s->frame.start > 5) |
1374 | 176k | mode = 0; |
1375 | | |
1376 | 196k | if (mode) { |
1377 | 18.9k | index_conv(s->frame.cb_index); |
1378 | | |
1379 | 18.9k | lsf_dequantization(s->lsfdeq, s->frame.lsf, s->lpc_n); |
1380 | 18.9k | lsf_check_stability(s->lsfdeq, LPC_FILTERORDER, s->lpc_n); |
1381 | 18.9k | lsp_interpolate(s->syntdenum, s->weightdenum, |
1382 | 18.9k | s->lsfdeq, LPC_FILTERORDER, s); |
1383 | 18.9k | decode_residual(s, &s->frame, s->decresidual, s->syntdenum); |
1384 | | |
1385 | 18.9k | do_plc(s->plc_residual, s->plc_lpc, 0, |
1386 | 18.9k | s->decresidual, s->syntdenum + (LPC_FILTERORDER + 1) * (s->nsub - 1), |
1387 | 18.9k | s->last_lag, s); |
1388 | | |
1389 | 18.9k | memcpy(s->decresidual, s->plc_residual, s->block_samples * 2); |
1390 | 18.9k | } |
1391 | | |
1392 | 196k | if (s->enhancer) { |
1393 | | /* TODO */ |
1394 | 196k | } else { |
1395 | 196k | int16_t lag, i; |
1396 | | |
1397 | | /* Find last lag (since the enhancer is not called to give this info) */ |
1398 | 196k | if (s->mode == 20) { |
1399 | 173k | lag = xcorr_coeff(&s->decresidual[s->block_samples-60], &s->decresidual[s->block_samples-80], |
1400 | 173k | 60, 80, 20, -1); |
1401 | 173k | } else { |
1402 | 23.3k | lag = xcorr_coeff(&s->decresidual[s->block_samples-ENH_BLOCKL], |
1403 | 23.3k | &s->decresidual[s->block_samples-ENH_BLOCKL-20], |
1404 | 23.3k | ENH_BLOCKL, 100, 20, -1); |
1405 | 23.3k | } |
1406 | | |
1407 | | /* Store lag (it is needed if next packet is lost) */ |
1408 | 196k | s->last_lag = lag; |
1409 | | |
1410 | | /* copy data and run synthesis filter */ |
1411 | 196k | memcpy(plc_data, s->decresidual, s->block_samples * 2); |
1412 | | |
1413 | | /* Set up the filter state */ |
1414 | 196k | memcpy(&plc_data[-LPC_FILTERORDER], s->syntMem, LPC_FILTERORDER * 2); |
1415 | | |
1416 | 1.03M | for (i = 0; i < s->nsub; i++) { |
1417 | 834k | filter_arfq12(plc_data+i*SUBL, plc_data+i*SUBL, |
1418 | 834k | s->syntdenum + i*(LPC_FILTERORDER + 1), |
1419 | 834k | LPC_FILTERORDER + 1, SUBL); |
1420 | 834k | } |
1421 | | |
1422 | | /* Save the filter state */ |
1423 | 196k | memcpy(s->syntMem, &plc_data[s->block_samples-LPC_FILTERORDER], LPC_FILTERORDER * 2); |
1424 | 196k | } |
1425 | | |
1426 | 196k | memcpy(frame->data[0], plc_data, s->block_samples * 2); |
1427 | | |
1428 | 196k | hp_output((int16_t *)frame->data[0], hp_out_coeffs, |
1429 | 196k | s->hpimemy, s->hpimemx, s->block_samples); |
1430 | | |
1431 | 196k | memcpy(s->old_syntdenum, s->syntdenum, s->nsub*(LPC_FILTERORDER + 1) * 2); |
1432 | | |
1433 | 196k | s->prev_enh_pl = 0; |
1434 | 196k | if (mode == 0) |
1435 | 177k | s->prev_enh_pl = 1; |
1436 | | |
1437 | 196k | *got_frame_ptr = 1; |
1438 | | |
1439 | 196k | return avpkt->size; |
1440 | 196k | } |
1441 | | |
1442 | | static av_cold int ilbc_decode_init(AVCodecContext *avctx) |
1443 | 2.42k | { |
1444 | 2.42k | ILBCContext *s = avctx->priv_data; |
1445 | | |
1446 | 2.42k | if (avctx->block_align == 38) |
1447 | 6 | s->mode = 20; |
1448 | 2.42k | else if (avctx->block_align == 50) |
1449 | 65 | s->mode = 30; |
1450 | 2.35k | else if (avctx->bit_rate > 0) |
1451 | 2.27k | s->mode = avctx->bit_rate <= 14000 ? 30 : 20; |
1452 | 78 | else |
1453 | 78 | return AVERROR_INVALIDDATA; |
1454 | | |
1455 | 2.34k | av_channel_layout_uninit(&avctx->ch_layout); |
1456 | 2.34k | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; |
1457 | 2.34k | avctx->sample_rate = 8000; |
1458 | 2.34k | avctx->sample_fmt = AV_SAMPLE_FMT_S16; |
1459 | | |
1460 | 2.34k | if (s->mode == 30) { |
1461 | 626 | s->block_samples = 240; |
1462 | 626 | s->nsub = NSUB_30MS; |
1463 | 626 | s->nasub = NASUB_30MS; |
1464 | 626 | s->lpc_n = LPC_N_30MS; |
1465 | 626 | s->state_short_len = STATE_SHORT_LEN_30MS; |
1466 | 1.72k | } else { |
1467 | 1.72k | s->block_samples = 160; |
1468 | 1.72k | s->nsub = NSUB_20MS; |
1469 | 1.72k | s->nasub = NASUB_20MS; |
1470 | 1.72k | s->lpc_n = LPC_N_20MS; |
1471 | 1.72k | s->state_short_len = STATE_SHORT_LEN_20MS; |
1472 | 1.72k | } |
1473 | | |
1474 | 2.34k | return 0; |
1475 | 2.42k | } |
1476 | | |
1477 | | const FFCodec ff_ilbc_decoder = { |
1478 | | .p.name = "ilbc", |
1479 | | CODEC_LONG_NAME("iLBC (Internet Low Bitrate Codec)"), |
1480 | | .p.type = AVMEDIA_TYPE_AUDIO, |
1481 | | .p.id = AV_CODEC_ID_ILBC, |
1482 | | .init = ilbc_decode_init, |
1483 | | FF_CODEC_DECODE_CB(ilbc_decode_frame), |
1484 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF, |
1485 | | .priv_data_size = sizeof(ILBCContext), |
1486 | | }; |