]> git.sesse.net Git - ffmpeg/blob - libavcodec/g729dec.c
High-pass filter
[ffmpeg] / libavcodec / g729dec.c
1 /*
2  * G.729 decoder
3  * Copyright (c) 2008 Vladimir Voroshilov
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 #include <stdlib.h>
22 #include <inttypes.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <math.h>
27 #include <assert.h>
28
29 #include "avcodec.h"
30 #include "libavutil/avutil.h"
31 #include "get_bits.h"
32 #include "dsputil.h"
33
34 #include "g729.h"
35 #include "lsp.h"
36 #include "celp_math.h"
37 #include "celp_filters.h"
38 #include "acelp_filters.h"
39 #include "acelp_pitch_delay.h"
40 #include "acelp_vectors.h"
41 #include "g729data.h"
42
43 /**
44  * minimum quantized LSF value (3.2.4)
45  * 0.005 in Q13
46  */
47 #define LSFQ_MIN                   40
48
49 /**
50  * maximum quantized LSF value (3.2.4)
51  * 3.135 in Q13
52  */
53 #define LSFQ_MAX                   25681
54
55 /**
56  * minimum LSF distance (3.2.4)
57  * 0.0391 in Q13
58  */
59 #define LSFQ_DIFF_MIN              321
60
61 /// interpolation filter length
62 #define INTERPOL_LEN              11
63
64 /**
65  * minimum gain pitch value (3.8, Equation 47)
66  * 0.2 in (1.14)
67  */
68 #define SHARP_MIN                  3277
69
70 /**
71  * maximum gain pitch value (3.8, Equation 47)
72  * (EE) This does not comply with the specification.
73  * Specification says about 0.8, which should be
74  * 13107 in (1.14), but reference C code uses
75  * 13017 (equals to 0.7945) instead of it.
76  */
77 #define SHARP_MAX                  13017
78
79 /**
80  * MR_ENERGY (mean removed energy) = mean_energy + 10 * log10(2^26  * subframe_size) in (7.13)
81  */
82 #define MR_ENERGY 1018156
83
84 #define DECISION_NOISE        0
85 #define DECISION_INTERMEDIATE 1
86 #define DECISION_VOICE        2
87
88 typedef enum {
89     FORMAT_G729_8K = 0,
90     FORMAT_G729D_6K4,
91     FORMAT_COUNT,
92 } G729Formats;
93
94 typedef struct {
95     uint8_t ac_index_bits[2];   ///< adaptive codebook index for second subframe (size in bits)
96     uint8_t parity_bit;         ///< parity bit for pitch delay
97     uint8_t gc_1st_index_bits;  ///< gain codebook (first stage) index (size in bits)
98     uint8_t gc_2nd_index_bits;  ///< gain codebook (second stage) index (size in bits)
99     uint8_t fc_signs_bits;      ///< number of pulses in fixed-codebook vector
100     uint8_t fc_indexes_bits;    ///< size (in bits) of fixed-codebook index entry
101 } G729FormatDescription;
102
103 typedef struct {
104     DSPContext dsp;
105
106     /// past excitation signal buffer
107     int16_t exc_base[2*SUBFRAME_SIZE+PITCH_DELAY_MAX+INTERPOL_LEN];
108
109     int16_t* exc;               ///< start of past excitation data in buffer
110     int pitch_delay_int_prev;   ///< integer part of previous subframe's pitch delay (4.1.3)
111
112     /// (2.13) LSP quantizer outputs
113     int16_t  past_quantizer_output_buf[MA_NP + 1][10];
114     int16_t* past_quantizer_outputs[MA_NP + 1];
115
116     int16_t lsfq[10];           ///< (2.13) quantized LSF coefficients from previous frame
117     int16_t lsp_buf[2][10];     ///< (0.15) LSP coefficients (previous and current frames) (3.2.5)
118     int16_t *lsp[2];            ///< pointers to lsp_buf
119
120     int16_t quant_energy[4];    ///< (5.10) past quantized energy
121
122     /// previous speech data for LP synthesis filter
123     int16_t syn_filter_data[10];
124
125     /// (1.14) pitch gain of current and five previous subframes
126     int16_t past_gain_pitch[6];
127
128     /// (14.1) gain code from current and previous subframe
129     int16_t past_gain_code[2];
130
131     /// voice decision on previous subframe (0-noise, 1-intermediate, 2-voice), G.729D
132     int16_t voice_decision;
133
134     int16_t onset;              ///< detected onset level (0-2)
135     int16_t was_periodic;       ///< whether previous frame was declared as periodic or not (4.4)
136     uint16_t rand_value;        ///< random number generator value (4.4.4)
137     int ma_predictor_prev;      ///< switched MA predictor of LSP quantizer from last good frame
138
139     /// (14.14) high-pass filter data (past input)
140     int hpf_f[2];
141
142     /// high-pass filter data (past output)
143     int16_t hpf_z[2];
144 }  G729Context;
145
146 static const G729FormatDescription format_g729_8k = {
147     .ac_index_bits     = {8,5},
148     .parity_bit        = 1,
149     .gc_1st_index_bits = GC_1ST_IDX_BITS_8K,
150     .gc_2nd_index_bits = GC_2ND_IDX_BITS_8K,
151     .fc_signs_bits     = 4,
152     .fc_indexes_bits   = 13,
153 };
154
155 static const G729FormatDescription format_g729d_6k4 = {
156     .ac_index_bits     = {8,4},
157     .parity_bit        = 0,
158     .gc_1st_index_bits = GC_1ST_IDX_BITS_6K4,
159     .gc_2nd_index_bits = GC_2ND_IDX_BITS_6K4,
160     .fc_signs_bits     = 2,
161     .fc_indexes_bits   = 9,
162 };
163
164 /**
165  * @brief pseudo random number generator
166  */
167 static inline uint16_t g729_prng(uint16_t value)
168 {
169     return 31821 * value + 13849;
170 }
171
172 /**
173  * Get parity bit of bit 2..7
174  */
175 static inline int get_parity(uint8_t value)
176 {
177    return (0x6996966996696996ULL >> (value >> 2)) & 1;
178 }
179
180 /*
181  * Decodes LSF (Line Spectral Frequencies) from L0-L3 (3.2.4).
182  * @param lsfq [out] (2.13) quantized LSF coefficients
183  * @param past_quantizer_outputs [in/out] (2.13) quantizer outputs from previous frames
184  * @param ma_predictor switched MA predictor of LSP quantizer
185  * @param vq_1st first stage vector of quantizer
186  * @param vq_2nd_low second stage lower vector of LSP quantizer
187  * @param vq_2nd_high second stage higher vector of LSP quantizer
188  */
189 static void lsf_decode(int16_t* lsfq, int16_t* past_quantizer_outputs[MA_NP + 1],
190                        int16_t ma_predictor,
191                        int16_t vq_1st, int16_t vq_2nd_low, int16_t vq_2nd_high)
192 {
193     int i,j;
194     static const uint8_t min_distance[2]={10, 5}; //(2.13)
195     int16_t* quantizer_output = past_quantizer_outputs[MA_NP];
196
197     for (i = 0; i < 5; i++) {
198         quantizer_output[i]     = cb_lsp_1st[vq_1st][i    ] + cb_lsp_2nd[vq_2nd_low ][i    ];
199         quantizer_output[i + 5] = cb_lsp_1st[vq_1st][i + 5] + cb_lsp_2nd[vq_2nd_high][i + 5];
200     }
201
202     for (j = 0; j < 2; j++) {
203         for (i = 1; i < 10; i++) {
204             int diff = (quantizer_output[i - 1] - quantizer_output[i] + min_distance[j]) >> 1;
205             if (diff > 0) {
206                 quantizer_output[i - 1] -= diff;
207                 quantizer_output[i    ] += diff;
208             }
209         }
210     }
211
212     for (i = 0; i < 10; i++) {
213         int sum = quantizer_output[i] * cb_ma_predictor_sum[ma_predictor][i];
214         for (j = 0; j < MA_NP; j++)
215             sum += past_quantizer_outputs[j][i] * cb_ma_predictor[ma_predictor][j][i];
216
217         lsfq[i] = sum >> 15;
218     }
219
220     ff_acelp_reorder_lsf(lsfq, LSFQ_DIFF_MIN, LSFQ_MIN, LSFQ_MAX, 10);
221 }
222
223 /**
224  * Restores past LSP quantizer output using LSF from previous frame
225  * @param lsfq [in/out] (2.13) quantized LSF coefficients
226  * @param past_quantizer_outputs [in/out] (2.13) quantizer outputs from previous frames
227  * @param ma_predictor_prev MA predictor from previous frame
228  * @param lsfq_prev (2.13) quantized LSF coefficients from previous frame
229  */
230 static void lsf_restore_from_previous(int16_t* lsfq,
231                                       int16_t* past_quantizer_outputs[MA_NP + 1],
232                                       int ma_predictor_prev)
233 {
234     int16_t* quantizer_output = past_quantizer_outputs[MA_NP];
235     int i,k;
236
237     for (i = 0; i < 10; i++) {
238         int tmp = lsfq[i] << 15;
239
240         for (k = 0; k < MA_NP; k++)
241             tmp -= past_quantizer_outputs[k][i] * cb_ma_predictor[ma_predictor_prev][k][i];
242
243         quantizer_output[i] = ((tmp >> 15) * cb_ma_predictor_sum_inv[ma_predictor_prev][i]) >> 12;
244     }
245 }
246
247 /**
248  * Constructs new excitation signal and applies phase filter to it
249  * @param out[out] constructed speech signal
250  * @param in original excitation signal
251  * @param fc_cur (2.13) original fixed-codebook vector
252  * @param gain_code (14.1) gain code
253  * @param subframe_size length of the subframe
254  */
255 void g729d_get_new_exc(
256         int16_t* out,
257         const int16_t* in,
258         const int16_t* fc_cur,
259         int dstate,
260         int gain_code,
261         int subframe_size)
262 {
263     int i;
264     int16_t fc_new[SUBFRAME_SIZE];
265
266     ff_celp_convolve_circ(fc_new, fc_cur, phase_filter[dstate], subframe_size);
267
268     for(i=0; i<subframe_size; i++)
269     {
270         out[i]  = in[i];
271         out[i] -= (gain_code * fc_cur[i] + 0x2000) >> 14;
272         out[i] += (gain_code * fc_new[i] + 0x2000) >> 14;
273     }
274 }
275
276 /**
277  * Makes decision about onset in current subframe
278  * @param past_onset decision result of previous subframe
279  * @param past_gain_code gain code of current and previous subframe
280  *
281  * @return onset decision result for current subframe
282  */
283 int g729d_onset_decision(int past_onset, const int16_t* past_gain_code)
284 {
285     if((past_gain_code[0] >> 1) > past_gain_code[1])
286         return 2;
287     else
288         return FFMAX(past_onset-1, 0);
289 }
290
291 /**
292  * Makes decision about voice presence in current subframe
293  * @param onset onset level
294  * @param prev_voice_decision voice decision result from previous subframe
295  * @param past_gain_pitch pitch gain of current and previous subframes
296  *
297  * @return voice decision result for current subframe
298  */
299 static int16_t g729d_voice_decision(int onset, int prev_voice_decision, const int16_t* past_gain_pitch)
300 {
301     int i, low_gain_pitch_cnt, voice_decision;
302
303     if(past_gain_pitch[0] >= 14745)      // 0.9
304         voice_decision = DECISION_VOICE;
305     else if (past_gain_pitch[0] <= 9830) // 0.6
306         voice_decision = DECISION_NOISE;
307     else
308         voice_decision = DECISION_INTERMEDIATE;
309
310     for(i=0, low_gain_pitch_cnt=0; i<6; i++)
311         if(past_gain_pitch[i] < 9830)
312             low_gain_pitch_cnt++;
313
314     if(low_gain_pitch_cnt > 2 && !onset)
315         voice_decision = DECISION_NOISE;
316
317     if(!onset && voice_decision > prev_voice_decision + 1)
318         voice_decision--;
319
320     if(onset && voice_decision < DECISION_VOICE)
321         voice_decision++;
322
323     return voice_decision;
324 }
325
326 static av_cold int decoder_init(AVCodecContext * avctx)
327 {
328     G729Context* ctx = avctx->priv_data;
329     int i,k;
330
331     if (avctx->channels != 1) {
332         av_log(avctx, AV_LOG_ERROR, "Only mono sound is supported (requested channels: %d).\n", avctx->channels);
333         return AVERROR(EINVAL);
334     }
335
336     /* Both 8kbit/s and 6.4kbit/s modes uses two subframes per frame. */
337     avctx->frame_size = SUBFRAME_SIZE << 1;
338
339     for (k = 0; k < MA_NP + 1; k++) {
340         ctx->past_quantizer_outputs[k] = ctx->past_quantizer_output_buf[k];
341         for (i = 1; i < 11; i++)
342             ctx->past_quantizer_outputs[k][i - 1] = (18717 * i) >> 3;
343     }
344
345     ctx->lsp[0] = ctx->lsp_buf[0];
346     ctx->lsp[1] = ctx->lsp_buf[1];
347     memcpy(ctx->lsp[0], lsp_init, 10 * sizeof(int16_t));
348
349     ctx->exc = &ctx->exc_base[PITCH_DELAY_MAX+INTERPOL_LEN];
350
351     /* random seed initialization */
352     ctx->rand_value = 21845;
353
354     /* quantized prediction error */
355     for(i=0; i<4; i++)
356         ctx->quant_energy[i] = -14336; // -14 in (5.10)
357
358     dsputil_init(&ctx->dsp, avctx);
359
360     return 0;
361 }
362
363 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
364                         AVPacket *avpkt)
365 {
366     const uint8_t *buf = avpkt->data;
367     int buf_size       = avpkt->size;
368     int16_t *out_frame = data;
369     GetBitContext gb;
370     G729FormatDescription format;
371     int frame_erasure = 0;    ///< frame erasure detected during decoding
372     int bad_pitch = 0;        ///< parity check failed
373     int i;
374     int16_t *tmp;
375     G729Formats packet_type;
376     G729Context *ctx = avctx->priv_data;
377     int16_t lp[2][11];           // (3.12)
378     uint8_t ma_predictor;     ///< switched MA predictor of LSP quantizer
379     uint8_t quantizer_1st;    ///< first stage vector of quantizer
380     uint8_t quantizer_2nd_lo; ///< second stage lower vector of quantizer (size in bits)
381     uint8_t quantizer_2nd_hi; ///< second stage higher vector of quantizer (size in bits)
382
383     int pitch_delay_int;         // pitch delay, integer part
384     int pitch_delay_3x;          // pitch delay, multiplied by 3
385     int16_t fc[SUBFRAME_SIZE];   // fixed-codebook vector
386     int16_t synth[SUBFRAME_SIZE+10]; // fixed-codebook vector
387     int j;
388     int is_periodic = 0;         // whether one of the subframes is declared as periodic or not
389
390     if (*data_size < SUBFRAME_SIZE << 2) {
391         av_log(avctx, AV_LOG_ERROR, "Error processing packet: output buffer too small\n");
392         return AVERROR(EIO);
393     }
394
395     if (buf_size == 10) {
396         packet_type = FORMAT_G729_8K;
397         format = format_g729_8k;
398         //Reset voice decision
399         ctx->onset = 0;
400         ctx->voice_decision = DECISION_VOICE;
401         av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729 @ 8kbit/s");
402     } else if (buf_size == 8) {
403         packet_type = FORMAT_G729D_6K4;
404         format = format_g729d_6k4;
405         av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729D @ 6.4kbit/s");
406     } else {
407         av_log(avctx, AV_LOG_ERROR, "Packet size %d is unknown.\n", buf_size);
408         return AVERROR_INVALIDDATA;
409     }
410
411     for (i=0; i < buf_size; i++)
412         frame_erasure |= buf[i];
413     frame_erasure = !frame_erasure;
414
415     init_get_bits(&gb, buf, buf_size);
416
417     ma_predictor     = get_bits(&gb, 1);
418     quantizer_1st    = get_bits(&gb, VQ_1ST_BITS);
419     quantizer_2nd_lo = get_bits(&gb, VQ_2ND_BITS);
420     quantizer_2nd_hi = get_bits(&gb, VQ_2ND_BITS);
421
422     if(frame_erasure)
423         lsf_restore_from_previous(ctx->lsfq, ctx->past_quantizer_outputs,
424                                   ctx->ma_predictor_prev);
425     else {
426         lsf_decode(ctx->lsfq, ctx->past_quantizer_outputs,
427                    ma_predictor,
428                    quantizer_1st, quantizer_2nd_lo, quantizer_2nd_hi);
429         ctx->ma_predictor_prev = ma_predictor;
430     }
431
432     tmp = ctx->past_quantizer_outputs[MA_NP];
433     memmove(ctx->past_quantizer_outputs + 1, ctx->past_quantizer_outputs,
434             MA_NP * sizeof(int16_t*));
435     ctx->past_quantizer_outputs[0] = tmp;
436
437     ff_acelp_lsf2lsp(ctx->lsp[1], ctx->lsfq, 10);
438
439     ff_acelp_lp_decode(&lp[0][0], &lp[1][0], ctx->lsp[1], ctx->lsp[0], 10);
440
441     FFSWAP(int16_t*, ctx->lsp[1], ctx->lsp[0]);
442
443     for (i = 0; i < 2; i++) {
444         int gain_corr_factor;
445
446         uint8_t ac_index;      ///< adaptive codebook index
447         uint8_t pulses_signs;  ///< fixed-codebook vector pulse signs
448         int fc_indexes;        ///< fixed-codebook indexes
449         uint8_t gc_1st_index;  ///< gain codebook (first stage) index
450         uint8_t gc_2nd_index;  ///< gain codebook (second stage) index
451
452         ac_index      = get_bits(&gb, format.ac_index_bits[i]);
453         if(!i && format.parity_bit)
454             bad_pitch = get_parity(ac_index) == get_bits1(&gb);
455         fc_indexes    = get_bits(&gb, format.fc_indexes_bits);
456         pulses_signs  = get_bits(&gb, format.fc_signs_bits);
457         gc_1st_index  = get_bits(&gb, format.gc_1st_index_bits);
458         gc_2nd_index  = get_bits(&gb, format.gc_2nd_index_bits);
459
460         if (frame_erasure)
461             pitch_delay_3x   = 3 * ctx->pitch_delay_int_prev;
462         else if(!i) {
463             if (bad_pitch)
464                 pitch_delay_3x   = 3 * ctx->pitch_delay_int_prev;
465             else
466                 pitch_delay_3x = ff_acelp_decode_8bit_to_1st_delay3(ac_index);
467         } else {
468             int pitch_delay_min = av_clip(ctx->pitch_delay_int_prev - 5,
469                                           PITCH_DELAY_MIN, PITCH_DELAY_MAX - 9);
470
471             if(packet_type == FORMAT_G729D_6K4)
472                 pitch_delay_3x = ff_acelp_decode_4bit_to_2nd_delay3(ac_index, pitch_delay_min);
473             else
474                 pitch_delay_3x = ff_acelp_decode_5_6_bit_to_2nd_delay3(ac_index, pitch_delay_min);
475         }
476
477         /* Round pitch delay to nearest (used everywhere except ff_acelp_interpolate). */
478         pitch_delay_int  = (pitch_delay_3x + 1) / 3;
479
480         if (frame_erasure) {
481             ctx->rand_value = g729_prng(ctx->rand_value);
482             fc_indexes   = ctx->rand_value & ((1 << format.fc_indexes_bits) - 1);
483
484             ctx->rand_value = g729_prng(ctx->rand_value);
485             pulses_signs = ctx->rand_value;
486         }
487
488
489         memset(fc, 0, sizeof(int16_t) * SUBFRAME_SIZE);
490         switch (packet_type) {
491             case FORMAT_G729_8K:
492                 ff_acelp_fc_pulse_per_track(fc, ff_fc_4pulses_8bits_tracks_13,
493                                             ff_fc_4pulses_8bits_track_4,
494                                             fc_indexes, pulses_signs, 3, 3);
495                 break;
496             case FORMAT_G729D_6K4:
497                 ff_acelp_fc_pulse_per_track(fc, ff_fc_2pulses_9bits_track1_gray,
498                                             ff_fc_2pulses_9bits_track2_gray,
499                                             fc_indexes, pulses_signs, 1, 4);
500                 break;
501         }
502
503         /*
504           This filter enhances harmonic components of the fixed-codebook vector to
505           improve the quality of the reconstructed speech.
506
507                      / fc_v[i],                                    i < pitch_delay
508           fc_v[i] = <
509                      \ fc_v[i] + gain_pitch * fc_v[i-pitch_delay], i >= pitch_delay
510         */
511         ff_acelp_weighted_vector_sum(fc + pitch_delay_int,
512                                      fc + pitch_delay_int,
513                                      fc, 1 << 14,
514                                      av_clip(ctx->past_gain_pitch[0], SHARP_MIN, SHARP_MAX),
515                                      0, 14,
516                                      SUBFRAME_SIZE - pitch_delay_int);
517
518         memmove(ctx->past_gain_pitch+1, ctx->past_gain_pitch, 5 * sizeof(int16_t));
519         ctx->past_gain_code[1] = ctx->past_gain_code[0];
520
521         if (frame_erasure) {
522             ctx->past_gain_pitch[0] = (29491 * ctx->past_gain_pitch[0]) >> 15; // 0.90 (0.15)
523             ctx->past_gain_code[0]  = ( 2007 * ctx->past_gain_code[0] ) >> 11; // 0.98 (0.11)
524
525             gain_corr_factor = 0;
526         } else {
527             if (packet_type == FORMAT_G729D_6K4) {
528                 ctx->past_gain_pitch[0]  = cb_gain_1st_6k4[gc_1st_index][0] +
529                                            cb_gain_2nd_6k4[gc_2nd_index][0];
530                 gain_corr_factor = cb_gain_1st_6k4[gc_1st_index][1] +
531                                    cb_gain_2nd_6k4[gc_2nd_index][1];
532
533                 /* Without check below overflow can occure in ff_acelp_update_past_gain.
534                    It is not issue for G.729, because gain_corr_factor in it's case is always
535                    greater than 1024, while in G.729D it can be even zero. */
536                 gain_corr_factor = FFMAX(gain_corr_factor, 1024);
537 #ifndef G729_BITEXACT
538                 gain_corr_factor >>= 1;
539 #endif
540             } else {
541                 ctx->past_gain_pitch[0]  = cb_gain_1st_8k[gc_1st_index][0] +
542                                            cb_gain_2nd_8k[gc_2nd_index][0];
543                 gain_corr_factor = cb_gain_1st_8k[gc_1st_index][1] +
544                                    cb_gain_2nd_8k[gc_2nd_index][1];
545             }
546
547             /* Decode the fixed-codebook gain. */
548             ctx->past_gain_code[0] = ff_acelp_decode_gain_code(&ctx->dsp, gain_corr_factor,
549                                                                fc, MR_ENERGY,
550                                                                ctx->quant_energy,
551                                                                ma_prediction_coeff,
552                                                                SUBFRAME_SIZE, 4);
553 #ifdef G729_BITEXACT
554             /*
555               This correction required to get bit-exact result with
556               reference code, because gain_corr_factor in G.729D is
557               two times larger than in original G.729.
558
559               If bit-exact result is not issue then gain_corr_factor
560               can be simpler devided by 2 before call to g729_get_gain_code
561               instead of using correction below.
562             */
563             if (packet_type == FORMAT_G729D_6K4) {
564                 gain_corr_factor >>= 1;
565                 ctx->past_gain_code[0] >>= 1;
566             }
567 #endif
568         }
569         ff_acelp_update_past_gain(ctx->quant_energy, gain_corr_factor, 2, frame_erasure);
570
571         /* Routine requires rounding to lowest. */
572         ff_acelp_interpolate(ctx->exc + i * SUBFRAME_SIZE,
573                              ctx->exc + i * SUBFRAME_SIZE - pitch_delay_3x / 3,
574                              ff_acelp_interp_filter, 6,
575                              (pitch_delay_3x % 3) << 1,
576                              10, SUBFRAME_SIZE);
577
578         ff_acelp_weighted_vector_sum(ctx->exc + i * SUBFRAME_SIZE,
579                                      ctx->exc + i * SUBFRAME_SIZE, fc,
580                                      (!ctx->was_periodic && frame_erasure) ? 0 : ctx->past_gain_pitch[0],
581                                      ( ctx->was_periodic && frame_erasure) ? 0 : ctx->past_gain_code[0],
582                                      1 << 13, 14, SUBFRAME_SIZE);
583
584         memcpy(synth, ctx->syn_filter_data, 10 * sizeof(int16_t));
585
586         if (ff_celp_lp_synthesis_filter(
587             synth+10,
588             &lp[i][1],
589             ctx->exc  + i * SUBFRAME_SIZE,
590             SUBFRAME_SIZE,
591             10,
592             1,
593             0x800))
594             /* Overflow occured, downscale excitation signal... */
595             for (j = 0; j < 2 * SUBFRAME_SIZE + PITCH_DELAY_MAX + INTERPOL_LEN; j++)
596                 ctx->exc_base[j] >>= 2;
597
598         /* ... and make synthesis again. */
599         if (packet_type == FORMAT_G729D_6K4) {
600             int16_t exc_new[SUBFRAME_SIZE];
601
602             ctx->onset = g729d_onset_decision(ctx->onset, ctx->past_gain_code);
603             ctx->voice_decision = g729d_voice_decision(ctx->onset, ctx->voice_decision, ctx->past_gain_pitch);
604
605             g729d_get_new_exc(exc_new, ctx->exc  + i * SUBFRAME_SIZE, fc, ctx->voice_decision, ctx->past_gain_code[0], SUBFRAME_SIZE);
606
607             ff_celp_lp_synthesis_filter(
608                     synth+10,
609                     &lp[i][1],
610                     exc_new,
611                     SUBFRAME_SIZE,
612                     10,
613                     0,
614                     0x800);
615         } else {
616             ff_celp_lp_synthesis_filter(
617                     synth+10,
618                     &lp[i][1],
619                     ctx->exc  + i * SUBFRAME_SIZE,
620                     SUBFRAME_SIZE,
621                     10,
622                     0,
623                     0x800);
624         }
625         /* Save data (without postfilter) for use in next subframe. */
626         memcpy(ctx->syn_filter_data, synth+SUBFRAME_SIZE, 10 * sizeof(int16_t));
627
628         if (frame_erasure)
629             ctx->pitch_delay_int_prev = FFMIN(ctx->pitch_delay_int_prev + 1, PITCH_DELAY_MAX);
630         else
631             ctx->pitch_delay_int_prev = pitch_delay_int;
632
633         memcpy(synth+8, ctx->hpf_z, 2*sizeof(int16_t));
634         ff_acelp_high_pass_filter(
635                 out_frame + i*SUBFRAME_SIZE,
636                 ctx->hpf_f,
637                 synth+10,
638                 SUBFRAME_SIZE);
639         memcpy(ctx->hpf_z, synth+8+SUBFRAME_SIZE, 2*sizeof(int16_t));
640     }
641
642     ctx->was_periodic = is_periodic;
643
644     /* Save signal for use in next frame. */
645     memmove(ctx->exc_base, ctx->exc_base + 2 * SUBFRAME_SIZE, (PITCH_DELAY_MAX+INTERPOL_LEN)*sizeof(int16_t));
646
647     *data_size = SUBFRAME_SIZE << 2;
648     return buf_size;
649 }
650
651 AVCodec ff_g729_decoder =
652 {
653     "g729",
654     AVMEDIA_TYPE_AUDIO,
655     CODEC_ID_G729,
656     sizeof(G729Context),
657     decoder_init,
658     NULL,
659     NULL,
660     decode_frame,
661     .long_name = NULL_IF_CONFIG_SMALL("G.729"),
662 };