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