]> git.sesse.net Git - ffmpeg/blob - libavcodec/evrcdec.c
avcodec/vp8: Cosmetics, maintain alphabetical order in threading headers
[ffmpeg] / libavcodec / evrcdec.c
1 /*
2  * Enhanced Variable Rate Codec, Service Option 3 decoder
3  * Copyright (c) 2013 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Enhanced Variable Rate Codec, Service Option 3 decoder
25  * @author Paul B Mahol
26  */
27
28 #include "libavutil/mathematics.h"
29 #include "avcodec.h"
30 #include "internal.h"
31 #include "get_bits.h"
32 #include "evrcdata.h"
33 #include "acelp_vectors.h"
34 #include "lsp.h"
35
36 #define MIN_LSP_SEP (0.05 / (2.0 * M_PI))
37 #define MIN_DELAY      20
38 #define MAX_DELAY     120
39 #define NB_SUBFRAMES    3
40 #define SUBFRAME_SIZE  54
41 #define FILTER_ORDER   10
42 #define ACB_SIZE      128
43
44 typedef enum {
45     RATE_ERRS = -1,
46     SILENCE,
47     RATE_QUANT,
48     RATE_QUARTER,
49     RATE_HALF,
50     RATE_FULL,
51 } evrc_packet_rate;
52
53 /**
54  * EVRC-A unpacked data frame
55  */
56 typedef struct EVRCAFrame {
57     uint8_t  lpc_flag;        ///< spectral change indicator
58     uint16_t lsp[4];          ///< index into LSP codebook
59     uint8_t  pitch_delay;     ///< pitch delay for entire frame
60     uint8_t  delay_diff;      ///< delay difference for entire frame
61     uint8_t  acb_gain[3];     ///< adaptive codebook gain
62     uint16_t fcb_shape[3][4]; ///< fixed codebook shape
63     uint8_t  fcb_gain[3];     ///< fixed codebook gain index
64     uint8_t  energy_gain;     ///< frame energy gain index
65     uint8_t  tty;             ///< tty baud rate bit
66 } EVRCAFrame;
67
68 typedef struct EVRCContext {
69     GetBitContext    gb;
70     evrc_packet_rate bitrate;
71     evrc_packet_rate last_valid_bitrate;
72     EVRCAFrame       frame;
73
74     float            lspf[FILTER_ORDER];
75     float            prev_lspf[FILTER_ORDER];
76     float            synthesis[FILTER_ORDER];
77     float            postfilter_fir[FILTER_ORDER];
78     float            postfilter_iir[FILTER_ORDER];
79     float            postfilter_residual[ACB_SIZE + SUBFRAME_SIZE];
80     float            pitch_delay;
81     float            prev_pitch_delay;
82     float            avg_acb_gain;  ///< average adaptive codebook gain
83     float            avg_fcb_gain;  ///< average fixed codebook gain
84     float            pitch[ACB_SIZE + FILTER_ORDER + SUBFRAME_SIZE];
85     float            pitch_back[ACB_SIZE];
86     float            interpolation_coeffs[136];
87     float            energy_vector[NB_SUBFRAMES];
88     float            fade_scale;
89     float            last;
90
91     uint8_t          prev_energy_gain;
92     uint8_t          prev_error_flag;
93     uint8_t          warned_buf_mismatch_bitrate;
94 } EVRCContext;
95
96 /**
97  * Frame unpacking for RATE_FULL, RATE_HALF and RATE_QUANT
98  *
99  * @param e the context
100  *
101  * TIA/IS-127 Table 4.21-1
102  */
103 static void unpack_frame(EVRCContext *e)
104 {
105     EVRCAFrame *frame = &e->frame;
106     GetBitContext *gb = &e->gb;
107
108     switch (e->bitrate) {
109     case RATE_FULL:
110         frame->lpc_flag        = get_bits1(gb);
111         frame->lsp[0]          = get_bits(gb,  6);
112         frame->lsp[1]          = get_bits(gb,  6);
113         frame->lsp[2]          = get_bits(gb,  9);
114         frame->lsp[3]          = get_bits(gb,  7);
115         frame->pitch_delay     = get_bits(gb,  7);
116         frame->delay_diff      = get_bits(gb,  5);
117         frame->acb_gain[0]     = get_bits(gb,  3);
118         frame->fcb_shape[0][0] = get_bits(gb,  8);
119         frame->fcb_shape[0][1] = get_bits(gb,  8);
120         frame->fcb_shape[0][2] = get_bits(gb,  8);
121         frame->fcb_shape[0][3] = get_bits(gb, 11);
122         frame->fcb_gain[0]     = get_bits(gb,  5);
123         frame->acb_gain[1]     = get_bits(gb,  3);
124         frame->fcb_shape[1][0] = get_bits(gb,  8);
125         frame->fcb_shape[1][1] = get_bits(gb,  8);
126         frame->fcb_shape[1][2] = get_bits(gb,  8);
127         frame->fcb_shape[1][3] = get_bits(gb, 11);
128         frame->fcb_gain    [1] = get_bits(gb,  5);
129         frame->acb_gain    [2] = get_bits(gb,  3);
130         frame->fcb_shape[2][0] = get_bits(gb,  8);
131         frame->fcb_shape[2][1] = get_bits(gb,  8);
132         frame->fcb_shape[2][2] = get_bits(gb,  8);
133         frame->fcb_shape[2][3] = get_bits(gb, 11);
134         frame->fcb_gain    [2] = get_bits(gb,  5);
135         frame->tty             = get_bits1(gb);
136         break;
137     case RATE_HALF:
138         frame->lsp         [0] = get_bits(gb,  7);
139         frame->lsp         [1] = get_bits(gb,  7);
140         frame->lsp         [2] = get_bits(gb,  8);
141         frame->pitch_delay     = get_bits(gb,  7);
142         frame->acb_gain    [0] = get_bits(gb,  3);
143         frame->fcb_shape[0][0] = get_bits(gb, 10);
144         frame->fcb_gain    [0] = get_bits(gb,  4);
145         frame->acb_gain    [1] = get_bits(gb,  3);
146         frame->fcb_shape[1][0] = get_bits(gb, 10);
147         frame->fcb_gain    [1] = get_bits(gb,  4);
148         frame->acb_gain    [2] = get_bits(gb,  3);
149         frame->fcb_shape[2][0] = get_bits(gb, 10);
150         frame->fcb_gain    [2] = get_bits(gb,  4);
151         break;
152     case RATE_QUANT:
153         frame->lsp         [0] = get_bits(gb, 4);
154         frame->lsp         [1] = get_bits(gb, 4);
155         frame->energy_gain     = get_bits(gb, 8);
156         break;
157     }
158 }
159
160 static evrc_packet_rate buf_size2bitrate(const int buf_size)
161 {
162     switch (buf_size) {
163     case 23: return RATE_FULL;
164     case 11: return RATE_HALF;
165     case  6: return RATE_QUARTER;
166     case  3: return RATE_QUANT;
167     case  1: return SILENCE;
168     }
169
170     return RATE_ERRS;
171 }
172
173 /**
174  * Determine the bitrate from the frame size and/or the first byte of the frame.
175  *
176  * @param avctx the AV codec context
177  * @param buf_size length of the buffer
178  * @param buf the bufffer
179  *
180  * @return the bitrate on success,
181  *         RATE_ERRS  if the bitrate cannot be satisfactorily determined
182  */
183 static evrc_packet_rate determine_bitrate(AVCodecContext *avctx,
184                                           int *buf_size,
185                                           const uint8_t **buf)
186 {
187     evrc_packet_rate bitrate;
188
189     if ((bitrate = buf_size2bitrate(*buf_size)) >= 0) {
190         if (bitrate > **buf) {
191             EVRCContext *e = avctx->priv_data;
192             if (!e->warned_buf_mismatch_bitrate) {
193                 av_log(avctx, AV_LOG_WARNING,
194                        "Claimed bitrate and buffer size mismatch.\n");
195                 e->warned_buf_mismatch_bitrate = 1;
196             }
197             bitrate = **buf;
198         } else if (bitrate < **buf) {
199             av_log(avctx, AV_LOG_ERROR,
200                    "Buffer is too small for the claimed bitrate.\n");
201             return RATE_ERRS;
202         }
203         (*buf)++;
204         *buf_size -= 1;
205     } else if ((bitrate = buf_size2bitrate(*buf_size + 1)) >= 0) {
206         av_log(avctx, AV_LOG_DEBUG,
207                "Bitrate byte is missing, guessing the bitrate from packet size.\n");
208     } else
209         return RATE_ERRS;
210
211     return bitrate;
212 }
213
214 static void warn_insufficient_frame_quality(AVCodecContext *avctx,
215                                             const char *message)
216 {
217     av_log(avctx, AV_LOG_WARNING, "Frame #%d, %s\n",
218            avctx->frame_number, message);
219 }
220
221 /**
222  * Initialize the speech codec according to the specification.
223  *
224  * TIA/IS-127 5.2
225  */
226 static av_cold int evrc_decode_init(AVCodecContext *avctx)
227 {
228     EVRCContext *e = avctx->priv_data;
229     int i, n, idx = 0;
230     float denom = 2.0 / (2.0 * 8.0 + 1.0);
231
232     avctx->channels       = 1;
233     avctx->channel_layout = AV_CH_LAYOUT_MONO;
234     avctx->sample_fmt     = AV_SAMPLE_FMT_FLT;
235
236     for (i = 0; i < FILTER_ORDER; i++) {
237         e->prev_lspf[i] = (i + 1) * 0.048;
238         e->synthesis[i] = 0.0;
239     }
240
241     for (i = 0; i < ACB_SIZE; i++)
242         e->pitch[i] = e->pitch_back[i] = 0.0;
243
244     e->last_valid_bitrate = RATE_QUANT;
245     e->prev_pitch_delay   = 40.0;
246     e->fade_scale         = 1.0;
247     e->prev_error_flag    = 0;
248     e->avg_acb_gain = e->avg_fcb_gain = 0.0;
249
250     for (i = 0; i < 8; i++) {
251         float tt = ((float)i - 8.0 / 2.0) / 8.0;
252
253         for (n = -8; n <= 8; n++, idx++) {
254             float arg1 = M_PI * 0.9 * (tt - n);
255             float arg2 = M_PI * (tt - n);
256
257             e->interpolation_coeffs[idx] = 0.9;
258             if (arg1)
259                 e->interpolation_coeffs[idx] *= (0.54 + 0.46 * cos(arg2 * denom)) *
260                                                  sin(arg1) / arg1;
261         }
262     }
263
264     return 0;
265 }
266
267 /**
268  * Decode the 10 vector quantized line spectral pair frequencies from the LSP
269  * transmission codes of any bitrate and check for badly received packets.
270  *
271  * @param e the context
272  *
273  * @return 0 on success, -1 if the packet is badly received
274  *
275  * TIA/IS-127 5.2.1, 5.7.1
276  */
277 static int decode_lspf(EVRCContext *e)
278 {
279     const float **codebooks = evrc_lspq_codebooks[e->bitrate];
280     int i, j, k = 0;
281
282     for (i = 0; i < evrc_lspq_nb_codebooks[e->bitrate]; i++) {
283         int row_size = evrc_lspq_codebooks_row_sizes[e->bitrate][i];
284         const float *codebook = codebooks[i];
285
286         for (j = 0; j < row_size; j++)
287             e->lspf[k++] = codebook[e->frame.lsp[i] * row_size + j];
288     }
289
290     // check for monotonic LSPs
291     for (i = 1; i < FILTER_ORDER; i++)
292         if (e->lspf[i] <= e->lspf[i - 1])
293             return -1;
294
295     // check for minimum separation of LSPs at the splits
296     for (i = 0, k = 0; i < evrc_lspq_nb_codebooks[e->bitrate] - 1; i++) {
297         k += evrc_lspq_codebooks_row_sizes[e->bitrate][i];
298         if (e->lspf[k] - e->lspf[k - 1] <= MIN_LSP_SEP)
299             return -1;
300     }
301
302     return 0;
303 }
304
305 /*
306  * Interpolation of LSP parameters.
307  *
308  * TIA/IS-127 5.2.3.1, 5.7.3.2
309  */
310 static void interpolate_lsp(float *ilsp, const float *lsp,
311                             const float *prev, int index)
312 {
313     static const float lsp_interpolation_factors[] = { 0.1667, 0.5, 0.8333 };
314     ff_weighted_vector_sumf(ilsp, prev, lsp,
315                             1.0 - lsp_interpolation_factors[index],
316                             lsp_interpolation_factors[index], FILTER_ORDER);
317 }
318
319 /*
320  * Reconstruction of the delay contour.
321  *
322  * TIA/IS-127 5.2.2.3.2
323  */
324 static void interpolate_delay(float *dst, float current, float prev, int index)
325 {
326     static const float d_interpolation_factors[] = { 0, 0.3313, 0.6625, 1, 1 };
327     dst[0] = (1.0 - d_interpolation_factors[index    ]) * prev
328                   + d_interpolation_factors[index    ]  * current;
329     dst[1] = (1.0 - d_interpolation_factors[index + 1]) * prev
330                   + d_interpolation_factors[index + 1]  * current;
331     dst[2] = (1.0 - d_interpolation_factors[index + 2]) * prev
332                   + d_interpolation_factors[index + 2]  * current;
333 }
334
335 /*
336  * Convert the quantized, interpolated line spectral frequencies,
337  * to prediction coefficients.
338  *
339  * TIA/IS-127 5.2.3.2, 4.7.2.2
340  */
341 static void decode_predictor_coeffs(const float *ilspf, float *ilpc)
342 {
343     double lsp[FILTER_ORDER];
344     float a[FILTER_ORDER / 2 + 1], b[FILTER_ORDER / 2 + 1];
345     float a1[FILTER_ORDER / 2] = { 0 };
346     float a2[FILTER_ORDER / 2] = { 0 };
347     float b1[FILTER_ORDER / 2] = { 0 };
348     float b2[FILTER_ORDER / 2] = { 0 };
349     int i, k;
350
351     ff_acelp_lsf2lspd(lsp, ilspf, FILTER_ORDER);
352
353     for (k = 0; k <= FILTER_ORDER; k++) {
354         a[0] = k < 2 ? 0.25 : 0;
355         b[0] = k < 2 ? k < 1 ? 0.25 : -0.25 : 0;
356
357         for (i = 0; i < FILTER_ORDER / 2; i++) {
358             a[i + 1] = a[i] - 2 * lsp[i * 2    ] * a1[i] + a2[i];
359             b[i + 1] = b[i] - 2 * lsp[i * 2 + 1] * b1[i] + b2[i];
360             a2[i] = a1[i];
361             a1[i] = a[i];
362             b2[i] = b1[i];
363             b1[i] = b[i];
364         }
365
366         if (k)
367             ilpc[k - 1] = 2.0 * (a[FILTER_ORDER / 2] + b[FILTER_ORDER / 2]);
368     }
369 }
370
371 static void bl_intrp(EVRCContext *e, float *ex, float delay)
372 {
373     float *f;
374     int offset, i, coef_idx;
375     int16_t t;
376
377     offset = lrintf(delay);
378
379     t = (offset - delay + 0.5) * 8.0 + 0.5;
380     if (t == 8) {
381         t = 0;
382         offset--;
383     }
384
385     f = ex - offset - 8;
386
387     coef_idx = t * (2 * 8 + 1);
388
389     ex[0] = 0.0;
390     for (i = 0; i < 2 * 8 + 1; i++)
391         ex[0] += e->interpolation_coeffs[coef_idx + i] * f[i];
392 }
393
394 /*
395  * Adaptive codebook excitation.
396  *
397  * TIA/IS-127 5.2.2.3.3, 4.12.5.2
398  */
399 static void acb_excitation(EVRCContext *e, float *excitation, float gain,
400                            const float delay[3], int length)
401 {
402     float denom, locdelay, dpr, invl;
403     int i;
404
405     invl = 1.0 / ((float) length);
406     dpr = length;
407
408     /* first at-most extra samples */
409     denom = (delay[1] - delay[0]) * invl;
410     for (i = 0; i < dpr; i++) {
411         locdelay = delay[0] + i * denom;
412         bl_intrp(e, excitation + i, locdelay);
413     }
414
415     denom = (delay[2] - delay[1]) * invl;
416     /* interpolation */
417     for (i = dpr; i < dpr + 10; i++) {
418         locdelay = delay[1] + (i - dpr) * denom;
419         bl_intrp(e, excitation + i, locdelay);
420     }
421
422     for (i = 0; i < length; i++)
423         excitation[i] *= gain;
424 }
425
426 static void decode_8_pulses_35bits(const uint16_t *fixed_index, float *cod)
427 {
428     int i, pos1, pos2, offset;
429
430     offset = (fixed_index[3] >> 9) & 3;
431
432     for (i = 0; i < 3; i++) {
433         pos1 = ((fixed_index[i] & 0x7f) / 11) * 5 + ((i + offset) % 5);
434         pos2 = ((fixed_index[i] & 0x7f) % 11) * 5 + ((i + offset) % 5);
435
436         cod[pos1] = (fixed_index[i] & 0x80) ? -1.0 : 1.0;
437
438         if (pos2 < pos1)
439             cod[pos2]  = -cod[pos1];
440         else
441             cod[pos2] +=  cod[pos1];
442     }
443
444     pos1 = ((fixed_index[3] & 0x7f) / 11) * 5 + ((3 + offset) % 5);
445     pos2 = ((fixed_index[3] & 0x7f) % 11) * 5 + ((4 + offset) % 5);
446
447     cod[pos1] = (fixed_index[3] & 0x100) ? -1.0 : 1.0;
448     cod[pos2] = (fixed_index[3] & 0x80 ) ? -1.0 : 1.0;
449 }
450
451 static void decode_3_pulses_10bits(uint16_t fixed_index, float *cod)
452 {
453     float sign;
454     int pos;
455
456     sign = (fixed_index & 0x200) ? -1.0 : 1.0;
457
458     pos = ((fixed_index        & 0x7) * 7) + 4;
459     cod[pos] += sign;
460     pos = (((fixed_index >> 3) & 0x7) * 7) + 2;
461     cod[pos] -= sign;
462     pos = (((fixed_index >> 6) & 0x7) * 7);
463     cod[pos] += sign;
464 }
465
466 /*
467  * Reconstruction of ACELP fixed codebook excitation for full and half rate.
468  *
469  * TIA/IS-127 5.2.3.7
470  */
471 static void fcb_excitation(EVRCContext *e, const uint16_t *codebook,
472                            float *excitation, float pitch_gain,
473                            int pitch_lag, int subframe_size)
474 {
475     int i;
476
477     if (e->bitrate == RATE_FULL)
478         decode_8_pulses_35bits(codebook, excitation);
479     else
480         decode_3_pulses_10bits(*codebook, excitation);
481
482     pitch_gain = av_clipf(pitch_gain, 0.2, 0.9);
483
484     for (i = pitch_lag; i < subframe_size; i++)
485         excitation[i] += pitch_gain * excitation[i - pitch_lag];
486 }
487
488 /**
489  * Synthesis of the decoder output signal.
490  *
491  * param[in]     in              input signal
492  * param[in]     filter_coeffs   LPC coefficients
493  * param[in/out] memory          synthesis filter memory
494  * param         buffer_length   amount of data to process
495  * param[out]    samples         output samples
496  *
497  * TIA/IS-127 5.2.3.15, 5.7.3.4
498  */
499 static void synthesis_filter(const float *in, const float *filter_coeffs,
500                              float *memory, int buffer_length, float *samples)
501 {
502     int i, j;
503
504     for (i = 0; i < buffer_length; i++) {
505         samples[i] = in[i];
506         for (j = FILTER_ORDER - 1; j > 0; j--) {
507             samples[i] -= filter_coeffs[j] * memory[j];
508             memory[j]   = memory[j - 1];
509         }
510         samples[i] -= filter_coeffs[0] * memory[0];
511         memory[0]   = samples[i];
512     }
513 }
514
515 static void bandwidth_expansion(float *coeff, const float *inbuf, float gamma)
516 {
517     double fac = gamma;
518     int i;
519
520     for (i = 0; i < FILTER_ORDER; i++) {
521         coeff[i] = inbuf[i] * fac;
522         fac *= gamma;
523     }
524 }
525
526 static void residual_filter(float *output, const float *input,
527                             const float *coef, float *memory, int length)
528 {
529     float sum;
530     int i, j;
531
532     for (i = 0; i < length; i++) {
533         sum = input[i];
534
535         for (j = FILTER_ORDER - 1; j > 0; j--) {
536             sum      += coef[j] * memory[j];
537             memory[j] = memory[j - 1];
538         }
539         sum += coef[0] * memory[0];
540         memory[0] = input[i];
541         output[i] = sum;
542     }
543 }
544
545 /*
546  * TIA/IS-127 Table 5.9.1-1.
547  */
548 static const struct PfCoeff {
549     float tilt;
550     float ltgain;
551     float p1;
552     float p2;
553 } postfilter_coeffs[5] = {
554     { 0.0 , 0.0 , 0.0 , 0.0  },
555     { 0.0 , 0.0 , 0.57, 0.57 },
556     { 0.0 , 0.0 , 0.0 , 0.0  },
557     { 0.35, 0.50, 0.50, 0.75 },
558     { 0.20, 0.50, 0.57, 0.75 },
559 };
560
561 /*
562  * Adaptive postfilter.
563  *
564  * TIA/IS-127 5.9
565  */
566 static void postfilter(EVRCContext *e, float *in, const float *coeff,
567                        float *out, int idx, const struct PfCoeff *pfc,
568                        int length)
569 {
570     float wcoef1[FILTER_ORDER], wcoef2[FILTER_ORDER],
571           scratch[SUBFRAME_SIZE], temp[SUBFRAME_SIZE],
572           mem[SUBFRAME_SIZE];
573     float sum1 = 0.0, sum2 = 0.0, gamma, gain;
574     float tilt = pfc->tilt;
575     int i, n, best;
576
577     bandwidth_expansion(wcoef1, coeff, pfc->p1);
578     bandwidth_expansion(wcoef2, coeff, pfc->p2);
579
580     /* Tilt compensation filter, TIA/IS-127 5.9.1 */
581     for (i = 0; i < length - 1; i++)
582         sum2 += in[i] * in[i + 1];
583     if (sum2 < 0.0)
584         tilt = 0.0;
585
586     for (i = 0; i < length; i++) {
587         scratch[i] = in[i] - tilt * e->last;
588         e->last = in[i];
589     }
590
591     /* Short term residual filter, TIA/IS-127 5.9.2 */
592     residual_filter(&e->postfilter_residual[ACB_SIZE], scratch, wcoef1, e->postfilter_fir, length);
593
594     /* Long term postfilter */
595     best = idx;
596     for (i = FFMIN(MIN_DELAY, idx - 3); i <= FFMAX(MAX_DELAY, idx + 3); i++) {
597         for (n = ACB_SIZE, sum2 = 0; n < ACB_SIZE + length; n++)
598             sum2 += e->postfilter_residual[n] * e->postfilter_residual[n - i];
599         if (sum2 > sum1) {
600             sum1 = sum2;
601             best = i;
602         }
603     }
604
605     for (i = ACB_SIZE, sum1 = 0; i < ACB_SIZE + length; i++)
606         sum1 += e->postfilter_residual[i - best] * e->postfilter_residual[i - best];
607     for (i = ACB_SIZE, sum2 = 0; i < ACB_SIZE + length; i++)
608         sum2 += e->postfilter_residual[i] * e->postfilter_residual[i - best];
609
610     if (sum2 * sum1 == 0 || e->bitrate == RATE_QUANT) {
611         memcpy(temp, e->postfilter_residual + ACB_SIZE, length * sizeof(float));
612     } else {
613         gamma = sum2 / sum1;
614         if (gamma < 0.5)
615             memcpy(temp, e->postfilter_residual + ACB_SIZE, length * sizeof(float));
616         else {
617             gamma = FFMIN(gamma, 1.0);
618
619             for (i = 0; i < length; i++) {
620                 temp[i] = e->postfilter_residual[ACB_SIZE + i] + gamma *
621                     pfc->ltgain * e->postfilter_residual[ACB_SIZE + i - best];
622             }
623         }
624     }
625
626     memcpy(scratch, temp, length * sizeof(float));
627     memcpy(mem, e->postfilter_iir, FILTER_ORDER * sizeof(float));
628     synthesis_filter(scratch, wcoef2, mem, length, scratch);
629
630     /* Gain computation, TIA/IS-127 5.9.4-2 */
631     for (i = 0, sum1 = 0, sum2 = 0; i < length; i++) {
632         sum1 += in[i] * in[i];
633         sum2 += scratch[i] * scratch[i];
634     }
635     gain = sum2 ? sqrt(sum1 / sum2) : 1.0;
636
637     for (i = 0; i < length; i++)
638         temp[i] *= gain;
639
640     /* Short term postfilter */
641     synthesis_filter(temp, wcoef2, e->postfilter_iir, length, out);
642
643     memmove(e->postfilter_residual,
644            e->postfilter_residual + length, ACB_SIZE * sizeof(float));
645 }
646
647 static void frame_erasure(EVRCContext *e, float *samples)
648 {
649     float ilspf[FILTER_ORDER], ilpc[FILTER_ORDER], idelay[NB_SUBFRAMES],
650           tmp[SUBFRAME_SIZE + 6], f;
651     int i, j;
652
653     for (i = 0; i < FILTER_ORDER; i++) {
654         if (e->bitrate != RATE_QUANT)
655             e->lspf[i] = e->prev_lspf[i] * 0.875 + 0.125 * (i + 1) * 0.048;
656         else
657             e->lspf[i] = e->prev_lspf[i];
658     }
659
660     if (e->prev_error_flag)
661         e->avg_acb_gain *= 0.75;
662     if (e->bitrate == RATE_FULL)
663         memcpy(e->pitch_back, e->pitch, ACB_SIZE * sizeof(float));
664     if (e->last_valid_bitrate == RATE_QUANT)
665         e->bitrate = RATE_QUANT;
666     else
667         e->bitrate = RATE_FULL;
668
669     if (e->bitrate == RATE_FULL || e->bitrate == RATE_HALF) {
670         e->pitch_delay = e->prev_pitch_delay;
671     } else {
672         float sum = 0;
673
674         idelay[0] = idelay[1] = idelay[2] = MIN_DELAY;
675
676         for (i = 0; i < NB_SUBFRAMES; i++)
677             sum += evrc_energy_quant[e->prev_energy_gain][i];
678         sum /= (float) NB_SUBFRAMES;
679         sum  = pow(10, sum);
680         for (i = 0; i < NB_SUBFRAMES; i++)
681             e->energy_vector[i] = sum;
682     }
683
684     if (fabs(e->pitch_delay - e->prev_pitch_delay) > 15)
685         e->prev_pitch_delay = e->pitch_delay;
686
687     for (i = 0; i < NB_SUBFRAMES; i++) {
688         int subframe_size = subframe_sizes[i];
689         int pitch_lag;
690
691         interpolate_lsp(ilspf, e->lspf, e->prev_lspf, i);
692
693         if (e->bitrate != RATE_QUANT) {
694             if (e->avg_acb_gain < 0.3) {
695                 idelay[0] = estimation_delay[i];
696                 idelay[1] = estimation_delay[i + 1];
697                 idelay[2] = estimation_delay[i + 2];
698             } else {
699                 interpolate_delay(idelay, e->pitch_delay, e->prev_pitch_delay, i);
700             }
701         }
702
703         pitch_lag = lrintf((idelay[1] + idelay[0]) / 2.0);
704         decode_predictor_coeffs(ilspf, ilpc);
705
706         if (e->bitrate != RATE_QUANT) {
707             acb_excitation(e, e->pitch + ACB_SIZE,
708                            e->avg_acb_gain, idelay, subframe_size);
709             for (j = 0; j < subframe_size; j++)
710                 e->pitch[ACB_SIZE + j] *= e->fade_scale;
711             e->fade_scale = FFMAX(e->fade_scale - 0.05, 0.0);
712         } else {
713             for (j = 0; j < subframe_size; j++)
714                 e->pitch[ACB_SIZE + j] = e->energy_vector[i];
715         }
716
717         memmove(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
718
719         if (e->bitrate != RATE_QUANT && e->avg_acb_gain < 0.4) {
720             f = 0.1 * e->avg_fcb_gain;
721             for (j = 0; j < subframe_size; j++)
722                 e->pitch[ACB_SIZE + j] += f;
723         } else if (e->bitrate == RATE_QUANT) {
724             for (j = 0; j < subframe_size; j++)
725                 e->pitch[ACB_SIZE + j] = e->energy_vector[i];
726         }
727
728         synthesis_filter(e->pitch + ACB_SIZE, ilpc,
729                          e->synthesis, subframe_size, tmp);
730         postfilter(e, tmp, ilpc, samples, pitch_lag,
731                    &postfilter_coeffs[e->bitrate], subframe_size);
732
733         samples += subframe_size;
734     }
735 }
736
737 static int evrc_decode_frame(AVCodecContext *avctx, void *data,
738                              int *got_frame_ptr, AVPacket *avpkt)
739 {
740     const uint8_t *buf = avpkt->data;
741     AVFrame *frame     = data;
742     EVRCContext *e     = avctx->priv_data;
743     int buf_size       = avpkt->size;
744     float ilspf[FILTER_ORDER], ilpc[FILTER_ORDER], idelay[NB_SUBFRAMES];
745     float *samples;
746     int   i, j, ret, error_flag = 0;
747
748     frame->nb_samples = 160;
749     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
750         return ret;
751     samples = (float *)frame->data[0];
752
753     if ((e->bitrate = determine_bitrate(avctx, &buf_size, &buf)) == RATE_ERRS) {
754         warn_insufficient_frame_quality(avctx, "bitrate cannot be determined.");
755         goto erasure;
756     }
757     if (e->bitrate <= SILENCE || e->bitrate == RATE_QUARTER)
758         goto erasure;
759     if (e->bitrate == RATE_QUANT && e->last_valid_bitrate == RATE_FULL
760                                  && !e->prev_error_flag)
761         goto erasure;
762
763     init_get_bits(&e->gb, buf, 8 * buf_size);
764     memset(&e->frame, 0, sizeof(EVRCAFrame));
765
766     unpack_frame(e);
767
768     if (e->bitrate != RATE_QUANT) {
769         uint8_t *p = (uint8_t *) &e->frame;
770         for (i = 0; i < sizeof(EVRCAFrame); i++) {
771             if (p[i])
772                 break;
773         }
774         if (i == sizeof(EVRCAFrame))
775             goto erasure;
776     } else if (e->frame.lsp[0] == 0xf &&
777                e->frame.lsp[1] == 0xf &&
778                e->frame.energy_gain == 0xff) {
779         goto erasure;
780     }
781
782     if (decode_lspf(e) < 0)
783         goto erasure;
784
785     if (e->bitrate == RATE_FULL || e->bitrate == RATE_HALF) {
786         /* Pitch delay parameter checking as per TIA/IS-127 5.1.5.1 */
787         if (e->frame.pitch_delay > MAX_DELAY - MIN_DELAY)
788             goto erasure;
789
790         e->pitch_delay = e->frame.pitch_delay + MIN_DELAY;
791
792         /* Delay diff parameter checking as per TIA/IS-127 5.1.5.2 */
793         if (e->frame.delay_diff) {
794             int p = e->pitch_delay - e->frame.delay_diff + 16;
795             if (p < MIN_DELAY || p > MAX_DELAY)
796                 goto erasure;
797         }
798
799         /* Delay contour reconstruction as per TIA/IS-127 5.2.2.2 */
800         if (e->frame.delay_diff &&
801             e->bitrate == RATE_FULL && e->prev_error_flag) {
802             float delay;
803
804             memcpy(e->pitch, e->pitch_back, ACB_SIZE * sizeof(float));
805
806             delay = e->prev_pitch_delay;
807             e->prev_pitch_delay = delay - e->frame.delay_diff + 16.0;
808
809             if (fabs(e->pitch_delay - delay) > 15)
810                 delay = e->pitch_delay;
811
812             for (i = 0; i < NB_SUBFRAMES; i++) {
813                 int subframe_size = subframe_sizes[i];
814
815                 interpolate_delay(idelay, delay, e->prev_pitch_delay, i);
816                 acb_excitation(e, e->pitch + ACB_SIZE, e->avg_acb_gain, idelay, subframe_size);
817                 memmove(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
818             }
819         }
820
821         /* Smoothing of the decoded delay as per TIA/IS-127 5.2.2.5 */
822         if (fabs(e->pitch_delay - e->prev_pitch_delay) > 15)
823             e->prev_pitch_delay = e->pitch_delay;
824
825         e->avg_acb_gain = e->avg_fcb_gain = 0.0;
826     } else {
827         idelay[0] = idelay[1] = idelay[2] = MIN_DELAY;
828
829         /* Decode frame energy vectors as per TIA/IS-127 5.7.2 */
830         for (i = 0; i < NB_SUBFRAMES; i++)
831             e->energy_vector[i] = pow(10, evrc_energy_quant[e->frame.energy_gain][i]);
832         e->prev_energy_gain = e->frame.energy_gain;
833     }
834
835     for (i = 0; i < NB_SUBFRAMES; i++) {
836         float tmp[SUBFRAME_SIZE + 6] = { 0 };
837         int subframe_size = subframe_sizes[i];
838         int pitch_lag;
839
840         interpolate_lsp(ilspf, e->lspf, e->prev_lspf, i);
841
842         if (e->bitrate != RATE_QUANT)
843             interpolate_delay(idelay, e->pitch_delay, e->prev_pitch_delay, i);
844
845         pitch_lag = lrintf((idelay[1] + idelay[0]) / 2.0);
846         decode_predictor_coeffs(ilspf, ilpc);
847
848         /* Bandwidth expansion as per TIA/IS-127 5.2.3.3 */
849         if (e->frame.lpc_flag && e->prev_error_flag)
850             bandwidth_expansion(ilpc, ilpc, 0.75);
851
852         if (e->bitrate != RATE_QUANT) {
853             float acb_sum, f;
854
855             f = exp((e->bitrate == RATE_HALF ? 0.5 : 0.25)
856                          * (e->frame.fcb_gain[i] + 1));
857             acb_sum = pitch_gain_vq[e->frame.acb_gain[i]];
858             e->avg_acb_gain += acb_sum / NB_SUBFRAMES;
859             e->avg_fcb_gain += f / NB_SUBFRAMES;
860
861             acb_excitation(e, e->pitch + ACB_SIZE,
862                            acb_sum, idelay, subframe_size);
863             fcb_excitation(e, e->frame.fcb_shape[i], tmp,
864                            acb_sum, pitch_lag, subframe_size);
865
866             /* Total excitation generation as per TIA/IS-127 5.2.3.9 */
867             for (j = 0; j < subframe_size; j++)
868                 e->pitch[ACB_SIZE + j] += f * tmp[j];
869             e->fade_scale = FFMIN(e->fade_scale + 0.2, 1.0);
870         } else {
871             for (j = 0; j < subframe_size; j++)
872                 e->pitch[ACB_SIZE + j] = e->energy_vector[i];
873         }
874
875         memmove(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
876
877         synthesis_filter(e->pitch + ACB_SIZE, ilpc,
878                          e->synthesis, subframe_size, tmp);
879         postfilter(e, tmp, ilpc, samples, pitch_lag,
880                    &postfilter_coeffs[e->bitrate], subframe_size);
881
882         samples += subframe_size;
883     }
884
885     if (error_flag) {
886 erasure:
887         error_flag = 1;
888         av_log(avctx, AV_LOG_WARNING, "frame erasure\n");
889         frame_erasure(e, samples);
890     }
891
892     memcpy(e->prev_lspf, e->lspf, sizeof(e->prev_lspf));
893     e->prev_error_flag    = error_flag;
894     e->last_valid_bitrate = e->bitrate;
895
896     if (e->bitrate != RATE_QUANT)
897         e->prev_pitch_delay = e->pitch_delay;
898
899     samples = (float *)frame->data[0];
900     for (i = 0; i < 160; i++)
901         samples[i] /= 32768;
902
903     *got_frame_ptr   = 1;
904
905     return avpkt->size;
906 }
907
908 AVCodec ff_evrc_decoder = {
909     .name           = "evrc",
910     .type           = AVMEDIA_TYPE_AUDIO,
911     .id             = AV_CODEC_ID_EVRC,
912     .init           = evrc_decode_init,
913     .decode         = evrc_decode_frame,
914     .capabilities   = CODEC_CAP_DR1,
915     .priv_data_size = sizeof(EVRCContext),
916     .long_name      = NULL_IF_CONFIG_SMALL("EVRC (Enhanced Variable Rate Codec)"),
917 };