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