Browse code

Add the G723.1 demuxer and decoder

Mohamed Naufal Basheer authored on 2011/03/18 07:56:50
Showing 9 changed files
... ...
@@ -159,6 +159,8 @@ OBJS-$(CONFIG_FLIC_DECODER)            += flicvideo.o
159 159
 OBJS-$(CONFIG_FOURXM_DECODER)          += 4xm.o
160 160
 OBJS-$(CONFIG_FRAPS_DECODER)           += fraps.o
161 161
 OBJS-$(CONFIG_FRWU_DECODER)            += frwu.o
162
+OBJS-$(CONFIG_G723_1_DECODER)          += g723_1.o acelp_vectors.o \
163
+                                          celp_filters.o celp_math.o
162 164
 OBJS-$(CONFIG_G729_DECODER)            += g729dec.o lsp.o celp_math.o acelp_filters.o acelp_pitch_delay.o acelp_vectors.o g729postfilter.o
163 165
 OBJS-$(CONFIG_GIF_DECODER)             += gifdec.o lzw.o
164 166
 OBJS-$(CONFIG_GIF_ENCODER)             += gif.o lzwenc.o
... ...
@@ -258,6 +258,7 @@ void avcodec_register_all(void)
258 258
     REGISTER_DECODER (DSICINAUDIO, dsicinaudio);
259 259
     REGISTER_ENCDEC  (EAC3, eac3);
260 260
     REGISTER_ENCDEC  (FLAC, flac);
261
+    REGISTER_DECODER (G723_1, g723_1);
261 262
     REGISTER_DECODER (G729, g729);
262 263
     REGISTER_DECODER (GSM, gsm);
263 264
     REGISTER_DECODER (GSM_MS, gsm_ms);
... ...
@@ -345,6 +345,7 @@ enum CodecID {
345 345
     CODEC_ID_QDMC,
346 346
     CODEC_ID_CELT,
347 347
     CODEC_ID_G729 = 0x15800,
348
+    CODEC_ID_G723_1= 0x15801,
348 349
 
349 350
     /* subtitle codecs */
350 351
     CODEC_ID_FIRST_SUBTITLE = 0x17000,          ///< A dummy ID pointing at the start of subtitle codecs.
351 352
new file mode 100644
... ...
@@ -0,0 +1,1081 @@
0
+/*
1
+ * G.723.1 compatible decoder
2
+ * Copyright (c) 2006 Benjamin Larsson
3
+ * Copyright (c) 2010 Mohamed Naufal Basheer
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
+ * G.723.1 compatible decoder
25
+ */
26
+
27
+#include "avcodec.h"
28
+#define ALT_BITSTREAM_READER_LE
29
+#include "get_bits.h"
30
+#include "acelp_vectors.h"
31
+#include "celp_filters.h"
32
+#include "celp_math.h"
33
+#include "lsp.h"
34
+#include "libavutil/lzo.h"
35
+#include "g723_1_data.h"
36
+
37
+typedef struct g723_1_context {
38
+    G723_1_Subframe subframe[4];
39
+    FrameType cur_frame_type;
40
+    FrameType past_frame_type;
41
+    Rate cur_rate;
42
+    uint8_t lsp_index[LSP_BANDS];
43
+    int pitch_lag[2];
44
+    int erased_frames;
45
+
46
+    int16_t prev_lsp[LPC_ORDER];
47
+    int16_t prev_excitation[PITCH_MAX];
48
+    int16_t excitation[PITCH_MAX + FRAME_LEN];
49
+    int16_t synth_mem[LPC_ORDER];
50
+    int16_t fir_mem[LPC_ORDER];
51
+    int     iir_mem[LPC_ORDER];
52
+
53
+    int random_seed;
54
+    int interp_index;
55
+    int interp_gain;
56
+    int sid_gain;
57
+    int cur_gain;
58
+    int reflection_coef;
59
+    int pf_gain;                 ///< formant postfilter
60
+                                 ///< gain scaling unit memory
61
+} G723_1_Context;
62
+
63
+static av_cold int g723_1_decode_init(AVCodecContext *avctx)
64
+{
65
+    G723_1_Context *p  = avctx->priv_data;
66
+
67
+    avctx->sample_fmt  = SAMPLE_FMT_S16;
68
+    p->pf_gain         = 1 << 12;
69
+    memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(int16_t));
70
+
71
+    return 0;
72
+}
73
+
74
+/**
75
+ * Unpack the frame into parameters.
76
+ *
77
+ * @param p           the context
78
+ * @param buf         pointer to the input buffer
79
+ * @param buf_size    size of the input buffer
80
+ */
81
+static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf,
82
+                            int buf_size)
83
+{
84
+    GetBitContext gb;
85
+    int ad_cb_len;
86
+    int temp, info_bits, i;
87
+
88
+    init_get_bits(&gb, buf, buf_size * 8);
89
+
90
+    /* Extract frame type and rate info */
91
+    info_bits = get_bits(&gb, 2);
92
+
93
+    if (info_bits == 3) {
94
+        p->cur_frame_type = UntransmittedFrame;
95
+        return 0;
96
+    }
97
+
98
+    /* Extract 24 bit lsp indices, 8 bit for each band */
99
+    p->lsp_index[2] = get_bits(&gb, 8);
100
+    p->lsp_index[1] = get_bits(&gb, 8);
101
+    p->lsp_index[0] = get_bits(&gb, 8);
102
+
103
+    if (info_bits == 2) {
104
+        p->cur_frame_type = SIDFrame;
105
+        p->subframe[0].amp_index = get_bits(&gb, 6);
106
+        return 0;
107
+    }
108
+
109
+    /* Extract the info common to both rates */
110
+    p->cur_rate       = info_bits ? Rate5k3 : Rate6k3;
111
+    p->cur_frame_type = ActiveFrame;
112
+
113
+    p->pitch_lag[0] = get_bits(&gb, 7);
114
+    if (p->pitch_lag[0] > 123)       /* test if forbidden code */
115
+        return -1;
116
+    p->pitch_lag[0] += PITCH_MIN;
117
+    p->subframe[1].ad_cb_lag = get_bits(&gb, 2);
118
+
119
+    p->pitch_lag[1] = get_bits(&gb, 7);
120
+    if (p->pitch_lag[1] > 123)
121
+        return -1;
122
+    p->pitch_lag[1] += PITCH_MIN;
123
+    p->subframe[3].ad_cb_lag = get_bits(&gb, 2);
124
+    p->subframe[0].ad_cb_lag = 1;
125
+    p->subframe[2].ad_cb_lag = 1;
126
+
127
+    for (i = 0; i < SUBFRAMES; i++) {
128
+        /* Extract combined gain */
129
+        temp = get_bits(&gb, 12);
130
+        ad_cb_len = 170;
131
+        p->subframe[i].dirac_train = 0;
132
+        if (p->cur_rate == Rate6k3 && p->pitch_lag[i >> 1] < SUBFRAME_LEN - 2) {
133
+            p->subframe[i].dirac_train = temp >> 11;
134
+            temp &= 0x7ff;
135
+            ad_cb_len = 85;
136
+        }
137
+        p->subframe[i].ad_cb_gain = FASTDIV(temp, GAIN_LEVELS);
138
+        if (p->subframe[i].ad_cb_gain < ad_cb_len) {
139
+            p->subframe[i].amp_index = temp - p->subframe[i].ad_cb_gain *
140
+                                       GAIN_LEVELS;
141
+        } else {
142
+            return -1;
143
+        }
144
+    }
145
+
146
+    p->subframe[0].grid_index = get_bits(&gb, 1);
147
+    p->subframe[1].grid_index = get_bits(&gb, 1);
148
+    p->subframe[2].grid_index = get_bits(&gb, 1);
149
+    p->subframe[3].grid_index = get_bits(&gb, 1);
150
+
151
+    if (p->cur_rate == Rate6k3) {
152
+        skip_bits(&gb, 1);  /* skip reserved bit */
153
+
154
+        /* Compute pulse_pos index using the 13-bit combined position index */
155
+        temp = get_bits(&gb, 13);
156
+        p->subframe[0].pulse_pos = temp / 810;
157
+
158
+        temp -= p->subframe[0].pulse_pos * 810;
159
+        p->subframe[1].pulse_pos = FASTDIV(temp, 90);
160
+
161
+        temp -= p->subframe[1].pulse_pos * 90;
162
+        p->subframe[2].pulse_pos = FASTDIV(temp, 9);
163
+        p->subframe[3].pulse_pos = temp - p->subframe[2].pulse_pos * 9;
164
+
165
+        p->subframe[0].pulse_pos = (p->subframe[0].pulse_pos << 16) +
166
+                                   get_bits(&gb, 16);
167
+        p->subframe[1].pulse_pos = (p->subframe[1].pulse_pos << 14) +
168
+                                   get_bits(&gb, 14);
169
+        p->subframe[2].pulse_pos = (p->subframe[2].pulse_pos << 16) +
170
+                                   get_bits(&gb, 16);
171
+        p->subframe[3].pulse_pos = (p->subframe[3].pulse_pos << 14) +
172
+                                   get_bits(&gb, 14);
173
+
174
+        p->subframe[0].pulse_sign = get_bits(&gb, 6);
175
+        p->subframe[1].pulse_sign = get_bits(&gb, 5);
176
+        p->subframe[2].pulse_sign = get_bits(&gb, 6);
177
+        p->subframe[3].pulse_sign = get_bits(&gb, 5);
178
+    } else { /* Rate5k3 */
179
+        p->subframe[0].pulse_pos  = get_bits(&gb, 12);
180
+        p->subframe[1].pulse_pos  = get_bits(&gb, 12);
181
+        p->subframe[2].pulse_pos  = get_bits(&gb, 12);
182
+        p->subframe[3].pulse_pos  = get_bits(&gb, 12);
183
+
184
+        p->subframe[0].pulse_sign = get_bits(&gb, 4);
185
+        p->subframe[1].pulse_sign = get_bits(&gb, 4);
186
+        p->subframe[2].pulse_sign = get_bits(&gb, 4);
187
+        p->subframe[3].pulse_sign = get_bits(&gb, 4);
188
+    }
189
+
190
+    return 0;
191
+}
192
+
193
+/**
194
+ * Bitexact implementation of sqrt(val/2).
195
+ */
196
+static int16_t square_root(int val)
197
+{
198
+    int16_t res = 0;
199
+    int16_t exp = 0x4000;
200
+    int i;
201
+
202
+    for (i = 0; i < 14; i ++) {
203
+        int res_exp = res + exp;
204
+        if (val >= res_exp * res_exp << 1)
205
+            res += exp;
206
+        exp >>= 1;
207
+    }
208
+    return res;
209
+}
210
+
211
+/**
212
+ * Calculate the number of left-shifts required for normalizing the input.
213
+ *
214
+ * @param num   input number
215
+ * @param width width of the input, 16 bits(0) / 32 bits(1)
216
+ */
217
+static int normalize_bits(int num, int width)
218
+{
219
+    int i = 0;
220
+    int bits = (width) ? 31 : 15;
221
+    int limit = 1 << (bits - 1);
222
+
223
+    if (num) {
224
+        if (num == -1)
225
+            return bits;
226
+        if (num < 0)
227
+            num = ~num;
228
+        for (i = 0; num < limit; i++)
229
+            num <<= 1;
230
+    }
231
+    return i;
232
+}
233
+
234
+/**
235
+ * Scale vector contents based on the largest of their absolutes.
236
+ */
237
+static int scale_vector(int16_t *vector, int length)
238
+{
239
+    int bits, scale, max = 0;
240
+    int i;
241
+
242
+    const int16_t shift_table[16] = {
243
+        0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
244
+        0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000, 0x7fff
245
+    };
246
+
247
+    for (i = 0; i < length; i++)
248
+        max = FFMAX(max, FFABS(vector[i]));
249
+
250
+    bits  = normalize_bits(max, 0);
251
+    scale = shift_table[bits];
252
+
253
+    for (i = 0; i < length; i++)
254
+        vector[i] = (int16_t)(av_clipl_int32(vector[i] * scale << 1) >> 4);
255
+
256
+    return bits - 3;
257
+}
258
+
259
+/**
260
+ * Perform inverse quantization of LSP frequencies.
261
+ *
262
+ * @param cur_lsp    the current LSP vector
263
+ * @param prev_lsp   the previous LSP vector
264
+ * @param lsp_index  VQ indices
265
+ * @param bad_frame  bad frame flag
266
+ */
267
+static void inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp,
268
+                          uint8_t *lsp_index, int bad_frame)
269
+{
270
+    int min_dist, pred;
271
+    int i, j, temp, stable;
272
+
273
+    /* Check for frame erasure */
274
+    if (!bad_frame) {
275
+        min_dist     = 0x100;
276
+        pred         = 12288;
277
+    } else {
278
+        min_dist     = 0x200;
279
+        pred         = 23552;
280
+        lsp_index[0] = lsp_index[1] = lsp_index[2] = 0;
281
+    }
282
+
283
+    /* Get the VQ table entry corresponding to the transmitted index */
284
+    cur_lsp[0] = lsp_band0[lsp_index[0]][0];
285
+    cur_lsp[1] = lsp_band0[lsp_index[0]][1];
286
+    cur_lsp[2] = lsp_band0[lsp_index[0]][2];
287
+    cur_lsp[3] = lsp_band1[lsp_index[1]][0];
288
+    cur_lsp[4] = lsp_band1[lsp_index[1]][1];
289
+    cur_lsp[5] = lsp_band1[lsp_index[1]][2];
290
+    cur_lsp[6] = lsp_band2[lsp_index[2]][0];
291
+    cur_lsp[7] = lsp_band2[lsp_index[2]][1];
292
+    cur_lsp[8] = lsp_band2[lsp_index[2]][2];
293
+    cur_lsp[9] = lsp_band2[lsp_index[2]][3];
294
+
295
+    /* Add predicted vector & DC component to the previously quantized vector */
296
+    for (i = 0; i < LPC_ORDER; i++) {
297
+        temp        = ((prev_lsp[i] - dc_lsp[i]) * pred + (1 << 14)) >> 15;
298
+        cur_lsp[i] += dc_lsp[i] + temp;
299
+    }
300
+
301
+    for (i = 0; i < LPC_ORDER; i++) {
302
+        cur_lsp[0]             = FFMAX(cur_lsp[0],  0x180);
303
+        cur_lsp[LPC_ORDER - 1] = FFMIN(cur_lsp[LPC_ORDER - 1], 0x7e00);
304
+
305
+        /* Stability check */
306
+        for (j = 1; j < LPC_ORDER; j++) {
307
+            temp = min_dist + cur_lsp[j - 1] - cur_lsp[j];
308
+            if (temp > 0) {
309
+                temp >>= 1;
310
+                cur_lsp[j - 1] -= temp;
311
+                cur_lsp[j]     += temp;
312
+            }
313
+        }
314
+        stable = 1;
315
+        for (j = 1; j < LPC_ORDER; j++) {
316
+            temp = cur_lsp[j - 1] + min_dist - cur_lsp[j] - 4;
317
+            if (temp > 0) {
318
+                stable = 0;
319
+                break;
320
+            }
321
+        }
322
+        if (stable)
323
+            break;
324
+    }
325
+    if (!stable)
326
+        memcpy(cur_lsp, prev_lsp, LPC_ORDER * sizeof(int16_t));
327
+}
328
+
329
+/**
330
+ * Bitexact implementation of 2ab scaled by 1/2^16.
331
+ *
332
+ * @param a 32 bit multiplicand
333
+ * @param b 16 bit multiplier
334
+ */
335
+#define MULL2(a, b) \
336
+        ((((a) >> 16) * (b) << 1) + (((a) & 0xffff) * (b) >> 15))
337
+
338
+/**
339
+ * Convert LSP frequencies to LPC coefficients.
340
+ *
341
+ * @param lpc buffer for LPC coefficients
342
+ */
343
+static void lsp2lpc(int16_t *lpc)
344
+{
345
+    int f1[LPC_ORDER / 2 + 1];
346
+    int f2[LPC_ORDER / 2 + 1];
347
+    int i, j;
348
+
349
+    /* Calculate negative cosine */
350
+    for (j = 0; j < LPC_ORDER; j++) {
351
+        int index     = lpc[j] >> 7;
352
+        int offset    = lpc[j] & 0x7f;
353
+        int64_t temp1 = cos_tab[index] << 16;
354
+        int temp2     = (cos_tab[index + 1] - cos_tab[index]) *
355
+                          ((offset << 8) + 0x80) << 1;
356
+
357
+        lpc[j] = -(av_clipl_int32(((temp1 + temp2) << 1) + (1 << 15)) >> 16);
358
+    }
359
+
360
+    /*
361
+     * Compute sum and difference polynomial coefficients
362
+     * (bitexact alternative to lsp2poly() in lsp.c)
363
+     */
364
+    /* Initialize with values in Q28 */
365
+    f1[0] = 1 << 28;
366
+    f1[1] = (lpc[0] << 14) + (lpc[2] << 14);
367
+    f1[2] = lpc[0] * lpc[2] + (2 << 28);
368
+
369
+    f2[0] = 1 << 28;
370
+    f2[1] = (lpc[1] << 14) + (lpc[3] << 14);
371
+    f2[2] = lpc[1] * lpc[3] + (2 << 28);
372
+
373
+    /*
374
+     * Calculate and scale the coefficients by 1/2 in
375
+     * each iteration for a final scaling factor of Q25
376
+     */
377
+    for (i = 2; i < LPC_ORDER / 2; i++) {
378
+        f1[i + 1] = f1[i - 1] + MULL2(f1[i], lpc[2 * i]);
379
+        f2[i + 1] = f2[i - 1] + MULL2(f2[i], lpc[2 * i + 1]);
380
+
381
+        for (j = i; j >= 2; j--) {
382
+            f1[j] = MULL2(f1[j - 1], lpc[2 * i]) +
383
+                    (f1[j] >> 1) + (f1[j - 2] >> 1);
384
+            f2[j] = MULL2(f2[j - 1], lpc[2 * i + 1]) +
385
+                    (f2[j] >> 1) + (f2[j - 2] >> 1);
386
+        }
387
+
388
+        f1[0] >>= 1;
389
+        f2[0] >>= 1;
390
+        f1[1] = ((lpc[2 * i]     << 16 >> i) + f1[1]) >> 1;
391
+        f2[1] = ((lpc[2 * i + 1] << 16 >> i) + f2[1]) >> 1;
392
+    }
393
+
394
+    /* Convert polynomial coefficients to LPC coefficients */
395
+    for (i = 0; i < LPC_ORDER / 2; i++) {
396
+        int64_t ff1 = f1[i + 1] + f1[i];
397
+        int64_t ff2 = f2[i + 1] - f2[i];
398
+
399
+        lpc[i] = av_clipl_int32(((ff1 + ff2) << 3) + (1 << 15)) >> 16;
400
+        lpc[LPC_ORDER - i - 1] = av_clipl_int32(((ff1 - ff2) << 3) +
401
+                                                (1 << 15)) >> 16;
402
+    }
403
+}
404
+
405
+/**
406
+ * Quantize LSP frequencies by interpolation and convert them to
407
+ * the corresponding LPC coefficients.
408
+ *
409
+ * @param lpc      buffer for LPC coefficients
410
+ * @param cur_lsp  the current LSP vector
411
+ * @param prev_lsp the previous LSP vector
412
+ */
413
+static void lsp_interpolate(int16_t *lpc, int16_t *cur_lsp, int16_t *prev_lsp)
414
+{
415
+    int i;
416
+    int16_t *lpc_ptr = lpc;
417
+
418
+    /* cur_lsp * 0.25 + prev_lsp * 0.75 */
419
+    ff_acelp_weighted_vector_sum(lpc, cur_lsp, prev_lsp,
420
+                                 4096, 12288, 1 << 13, 14, LPC_ORDER);
421
+    ff_acelp_weighted_vector_sum(lpc + LPC_ORDER, cur_lsp, prev_lsp,
422
+                                 8192, 8192, 1 << 13, 14, LPC_ORDER);
423
+    ff_acelp_weighted_vector_sum(lpc + 2 * LPC_ORDER, cur_lsp, prev_lsp,
424
+                                 12288, 4096, 1 << 13, 14, LPC_ORDER);
425
+    memcpy(lpc + 3 * LPC_ORDER, cur_lsp, LPC_ORDER * sizeof(int16_t));
426
+
427
+    for (i = 0; i < SUBFRAMES; i++) {
428
+        lsp2lpc(lpc_ptr);
429
+        lpc_ptr += LPC_ORDER;
430
+    }
431
+}
432
+
433
+/**
434
+ * Generate a train of dirac functions with period as pitch lag.
435
+ */
436
+static void gen_dirac_train(int16_t *buf, int pitch_lag)
437
+{
438
+    int16_t vector[SUBFRAME_LEN];
439
+    int i, j;
440
+
441
+    memcpy(vector, buf, SUBFRAME_LEN * sizeof(int16_t));
442
+    for (i = pitch_lag; i < SUBFRAME_LEN; i += pitch_lag) {
443
+        for (j = 0; j < SUBFRAME_LEN - i; j++)
444
+            buf[i + j] += vector[j];
445
+    }
446
+}
447
+
448
+/**
449
+ * Generate fixed codebook excitation vector.
450
+ *
451
+ * @param vector    decoded excitation vector
452
+ * @param subfrm    current subframe
453
+ * @param cur_rate  current bitrate
454
+ * @param pitch_lag closed loop pitch lag
455
+ * @param index     current subframe index
456
+ */
457
+static void gen_fcb_excitation(int16_t *vector, G723_1_Subframe subfrm,
458
+                               Rate cur_rate, int pitch_lag, int index)
459
+{
460
+    int temp, i, j;
461
+
462
+    memset(vector, 0, SUBFRAME_LEN * sizeof(int16_t));
463
+
464
+    if (cur_rate == Rate6k3) {
465
+        if (subfrm.pulse_pos >= max_pos[index])
466
+            return;
467
+
468
+        /* Decode amplitudes and positions */
469
+        j = PULSE_MAX - pulses[index];
470
+        temp = subfrm.pulse_pos;
471
+        for (i = 0; i < SUBFRAME_LEN / GRID_SIZE; i++) {
472
+            temp -= combinatorial_table[j][i];
473
+            if (temp >= 0)
474
+                continue;
475
+            temp += combinatorial_table[j++][i];
476
+            if (subfrm.pulse_sign & (1 << (PULSE_MAX - j))) {
477
+                vector[subfrm.grid_index + GRID_SIZE * i] =
478
+                                        -fixed_cb_gain[subfrm.amp_index];
479
+            } else {
480
+                vector[subfrm.grid_index + GRID_SIZE * i] =
481
+                                         fixed_cb_gain[subfrm.amp_index];
482
+            }
483
+            if (j == PULSE_MAX)
484
+                break;
485
+        }
486
+        if (subfrm.dirac_train == 1)
487
+            gen_dirac_train(vector, pitch_lag);
488
+    } else { /* Rate5k3 */
489
+        int cb_gain  = fixed_cb_gain[subfrm.amp_index];
490
+        int cb_shift = subfrm.grid_index;
491
+        int cb_sign  = subfrm.pulse_sign;
492
+        int cb_pos   = subfrm.pulse_pos;
493
+        int offset, beta, lag;
494
+
495
+        for (i = 0; i < 8; i += 2) {
496
+            offset         = ((cb_pos & 7) << 3) + cb_shift + i;
497
+            vector[offset] = (cb_sign & 1) ? cb_gain : -cb_gain;
498
+            cb_pos  >>= 3;
499
+            cb_sign >>= 1;
500
+        }
501
+
502
+        /* Enhance harmonic components */
503
+        lag  = pitch_contrib[subfrm.ad_cb_gain << 1] + pitch_lag +
504
+               subfrm.ad_cb_lag - 1;
505
+        beta = pitch_contrib[(subfrm.ad_cb_gain << 1) + 1];
506
+
507
+        if (lag < SUBFRAME_LEN - 2) {
508
+            for (i = lag; i < SUBFRAME_LEN; i++)
509
+                vector[i] += beta * vector[i - lag] >> 15;
510
+        }
511
+    }
512
+}
513
+
514
+/**
515
+ * Get delayed contribution from the previous excitation vector.
516
+ */
517
+static void get_residual(int16_t *residual, int16_t *prev_excitation, int lag)
518
+{
519
+    int offset = PITCH_MAX - PITCH_ORDER / 2 - lag;
520
+    int i;
521
+
522
+    residual[0] = prev_excitation[offset];
523
+    residual[1] = prev_excitation[offset + 1];
524
+
525
+    offset += 2;
526
+    for (i = 2; i < SUBFRAME_LEN + PITCH_ORDER - 1; i++)
527
+        residual[i] = prev_excitation[offset + (i - 2) % lag];
528
+}
529
+
530
+/**
531
+ * Generate adaptive codebook excitation.
532
+ */
533
+static void gen_acb_excitation(int16_t *vector, int16_t *prev_excitation,
534
+                               int pitch_lag, G723_1_Subframe subfrm,
535
+                               Rate cur_rate)
536
+{
537
+    int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
538
+    const int16_t *cb_ptr;
539
+    int lag = pitch_lag + subfrm.ad_cb_lag - 1;
540
+
541
+    int i;
542
+    int64_t sum;
543
+
544
+    get_residual(residual, prev_excitation, lag);
545
+
546
+    /* Select quantization table */
547
+    if (cur_rate == Rate6k3 && pitch_lag < SUBFRAME_LEN - 2) {
548
+        cb_ptr = adaptive_cb_gain85;
549
+    } else
550
+        cb_ptr = adaptive_cb_gain170;
551
+
552
+    /* Calculate adaptive vector */
553
+    cb_ptr += subfrm.ad_cb_gain * 20;
554
+    for (i = 0; i < SUBFRAME_LEN; i++) {
555
+        sum = ff_dot_product(residual + i, cb_ptr, PITCH_ORDER, 1);
556
+        vector[i] = av_clipl_int32((sum << 1) + (1 << 15)) >> 16;
557
+    }
558
+}
559
+
560
+/**
561
+ * Estimate maximum auto-correlation around pitch lag.
562
+ *
563
+ * @param p         the context
564
+ * @param offset    offset of the excitation vector
565
+ * @param ccr_max   pointer to the maximum auto-correlation
566
+ * @param pitch_lag decoded pitch lag
567
+ * @param length    length of autocorrelation
568
+ * @param dir       forward lag(1) / backward lag(-1)
569
+ */
570
+static int autocorr_max(G723_1_Context *p, int offset, int *ccr_max,
571
+                        int pitch_lag, int length, int dir)
572
+{
573
+    int limit, ccr, lag = 0;
574
+    int16_t *buf = p->excitation + offset;
575
+    int i;
576
+
577
+    pitch_lag = FFMIN(PITCH_MAX - 3, pitch_lag);
578
+    limit     = FFMIN(FRAME_LEN + PITCH_MAX - offset - length, pitch_lag + 3);
579
+
580
+    for (i = pitch_lag - 3; i <= limit; i++) {
581
+        ccr = ff_dot_product(buf, buf + dir * i, length, 1);
582
+
583
+        if (ccr > *ccr_max) {
584
+            *ccr_max = ccr;
585
+            lag = i;
586
+        }
587
+    }
588
+    return lag;
589
+}
590
+
591
+/**
592
+ * Calculate pitch postfilter optimal and scaling gains.
593
+ *
594
+ * @param lag      pitch postfilter forward/backward lag
595
+ * @param ppf      pitch postfilter parameters
596
+ * @param cur_rate current bitrate
597
+ * @param tgt_eng  target energy
598
+ * @param ccr      cross-correlation
599
+ * @param res_eng  residual energy
600
+ */
601
+static void comp_ppf_gains(int lag, PPFParam *ppf, Rate cur_rate,
602
+                           int tgt_eng, int ccr, int res_eng)
603
+{
604
+    int pf_residual;     /* square of postfiltered residual */
605
+    int64_t temp1, temp2;
606
+
607
+    ppf->index = lag;
608
+
609
+    temp1 = tgt_eng * res_eng >> 1;
610
+    temp2 = ccr * ccr << 1;
611
+
612
+    if (temp2 > temp1) {
613
+        if (ccr >= res_eng) {
614
+            ppf->opt_gain = ppf_gain_weight[cur_rate];
615
+        } else {
616
+            ppf->opt_gain = (ccr << 15) / res_eng *
617
+                            ppf_gain_weight[cur_rate] >> 15;
618
+        }
619
+        /* pf_res^2 = tgt_eng + 2*ccr*gain + res_eng*gain^2 */
620
+        temp1       = (tgt_eng << 15) + (ccr * ppf->opt_gain << 1);
621
+        temp2       = (ppf->opt_gain * ppf->opt_gain >> 15) * res_eng;
622
+        pf_residual = av_clipl_int32(temp1 + temp2 + (1 << 15)) >> 16;
623
+
624
+        if (tgt_eng >= pf_residual << 1) {
625
+            temp1 = 0x7fff;
626
+        } else {
627
+            temp1 = (tgt_eng << 14) / pf_residual;
628
+        }
629
+
630
+        /* scaling_gain = sqrt(tgt_eng/pf_res^2) */
631
+        ppf->sc_gain = square_root(temp1 << 16);
632
+    } else {
633
+        ppf->opt_gain = 0;
634
+        ppf->sc_gain  = 0x7fff;
635
+    }
636
+
637
+    ppf->opt_gain = av_clip_int16(ppf->opt_gain * ppf->sc_gain >> 15);
638
+}
639
+
640
+/**
641
+ * Calculate pitch postfilter parameters.
642
+ *
643
+ * @param p         the context
644
+ * @param offset    offset of the excitation vector
645
+ * @param pitch_lag decoded pitch lag
646
+ * @param ppf       pitch postfilter parameters
647
+ * @param cur_rate  current bitrate
648
+ */
649
+static void comp_ppf_coeff(G723_1_Context *p, int offset, int pitch_lag,
650
+                           PPFParam *ppf, Rate cur_rate)
651
+{
652
+
653
+    int16_t scale;
654
+    int i;
655
+    int64_t temp1, temp2;
656
+
657
+    /*
658
+     * 0 - target energy
659
+     * 1 - forward cross-correlation
660
+     * 2 - forward residual energy
661
+     * 3 - backward cross-correlation
662
+     * 4 - backward residual energy
663
+     */
664
+    int energy[5] = {0, 0, 0, 0, 0};
665
+    int16_t *buf  = p->excitation + offset;
666
+    int fwd_lag   = autocorr_max(p, offset, &energy[1], pitch_lag,
667
+                                 SUBFRAME_LEN, 1);
668
+    int back_lag  = autocorr_max(p, offset, &energy[3], pitch_lag,
669
+                                 SUBFRAME_LEN, -1);
670
+
671
+    ppf->index    = 0;
672
+    ppf->opt_gain = 0;
673
+    ppf->sc_gain  = 0x7fff;
674
+
675
+    /* Case 0, Section 3.6 */
676
+    if (!back_lag && !fwd_lag)
677
+        return;
678
+
679
+    /* Compute target energy */
680
+    energy[0] = ff_dot_product(buf, buf, SUBFRAME_LEN, 1);
681
+
682
+    /* Compute forward residual energy */
683
+    if (fwd_lag)
684
+        energy[2] = ff_dot_product(buf + fwd_lag, buf + fwd_lag,
685
+                                   SUBFRAME_LEN, 1);
686
+
687
+    /* Compute backward residual energy */
688
+    if (back_lag)
689
+        energy[4] = ff_dot_product(buf - back_lag, buf - back_lag,
690
+                                   SUBFRAME_LEN, 1);
691
+
692
+    /* Normalize and shorten */
693
+    temp1 = 0;
694
+    for (i = 0; i < 5; i++)
695
+        temp1 = FFMAX(energy[i], temp1);
696
+
697
+    scale = normalize_bits(temp1, 1);
698
+    for (i = 0; i < 5; i++)
699
+        energy[i] = av_clipl_int32(energy[i] << scale) >> 16;
700
+
701
+    if (fwd_lag && !back_lag) {  /* Case 1 */
702
+        comp_ppf_gains(fwd_lag,  ppf, cur_rate, energy[0], energy[1],
703
+                       energy[2]);
704
+    } else if (!fwd_lag) {       /* Case 2 */
705
+        comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
706
+                       energy[4]);
707
+    } else {                     /* Case 3 */
708
+
709
+        /*
710
+         * Select the largest of energy[1]^2/energy[2]
711
+         * and energy[3]^2/energy[4]
712
+         */
713
+        temp1 = energy[4] * ((energy[1] * energy[1] + (1 << 14)) >> 15);
714
+        temp2 = energy[2] * ((energy[3] * energy[3] + (1 << 14)) >> 15);
715
+        if (temp1 >= temp2) {
716
+            comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
717
+                           energy[2]);
718
+        } else {
719
+            comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
720
+                           energy[4]);
721
+        }
722
+    }
723
+}
724
+
725
+/**
726
+ * Classify frames as voiced/unvoiced.
727
+ *
728
+ * @param p         the context
729
+ * @param pitch_lag decoded pitch_lag
730
+ * @param exc_eng   excitation energy estimation
731
+ * @param scale     scaling factor of exc_eng
732
+ *
733
+ * @return residual interpolation index if voiced, 0 otherwise
734
+ */
735
+static int comp_interp_index(G723_1_Context *p, int pitch_lag,
736
+                             int *exc_eng, int *scale)
737
+{
738
+    int offset = PITCH_MAX + 2 * SUBFRAME_LEN;
739
+    int16_t *buf = p->excitation + offset;
740
+
741
+    int index, ccr, tgt_eng, best_eng, temp;
742
+
743
+    *scale = scale_vector(p->excitation, FRAME_LEN + PITCH_MAX);
744
+
745
+    /* Compute maximum backward cross-correlation */
746
+    ccr   = 0;
747
+    index = autocorr_max(p, offset, &ccr, pitch_lag, SUBFRAME_LEN * 2, -1);
748
+    ccr   = av_clipl_int32((int64_t)ccr + (1 << 15)) >> 16;
749
+
750
+    /* Compute target energy */
751
+    tgt_eng  = ff_dot_product(buf, buf, SUBFRAME_LEN * 2, 1);
752
+    *exc_eng = av_clipl_int32(tgt_eng + (1 << 15)) >> 16;
753
+
754
+    if (ccr <= 0)
755
+        return 0;
756
+
757
+    /* Compute best energy */
758
+    best_eng = ff_dot_product(buf - index, buf - index,
759
+                              SUBFRAME_LEN * 2, 1);
760
+    best_eng = av_clipl_int32((int64_t)best_eng + (1 << 15)) >> 16;
761
+
762
+    temp = best_eng * *exc_eng >> 3;
763
+
764
+    if (temp < ccr * ccr) {
765
+        return index;
766
+    } else
767
+        return 0;
768
+}
769
+
770
+/**
771
+ * Peform residual interpolation based on frame classification.
772
+ *
773
+ * @param buf   decoded excitation vector
774
+ * @param out   output vector
775
+ * @param lag   decoded pitch lag
776
+ * @param gain  interpolated gain
777
+ * @param rseed seed for random number generator
778
+ */
779
+static void residual_interp(int16_t *buf, int16_t *out, int lag,
780
+                            int gain, int *rseed)
781
+{
782
+    int i;
783
+    if (lag) { /* Voiced */
784
+        int16_t *vector_ptr = buf + PITCH_MAX;
785
+        /* Attenuate */
786
+        for (i = 0; i < lag; i++)
787
+            vector_ptr[i - lag] = vector_ptr[i - lag] * 3 >> 2;
788
+        av_memcpy_backptr((uint8_t*)vector_ptr, lag * sizeof(int16_t),
789
+                          FRAME_LEN * sizeof(int16_t));
790
+        memcpy(out, vector_ptr, FRAME_LEN * sizeof(int16_t));
791
+    } else {  /* Unvoiced */
792
+        for (i = 0; i < FRAME_LEN; i++) {
793
+            *rseed = *rseed * 521 + 259;
794
+            out[i] = gain * *rseed >> 15;
795
+        }
796
+        memset(buf, 0, (FRAME_LEN + PITCH_MAX) * sizeof(int16_t));
797
+    }
798
+}
799
+
800
+/**
801
+ * Perform IIR filtering.
802
+ *
803
+ * @param fir_coef FIR coefficients
804
+ * @param iir_coef IIR coefficients
805
+ * @param src      source vector
806
+ * @param dest     destination vector
807
+ * @param width    width of the output, 16 bits(0) / 32 bits(1)
808
+ */
809
+#define iir_filter(fir_coef, iir_coef, src, dest, width)\
810
+{\
811
+    int m, n;\
812
+    int res_shift = 16 & ~-(width);\
813
+    int in_shift  = 16 - res_shift;\
814
+\
815
+    for (m = 0; m < SUBFRAME_LEN; m++) {\
816
+        int64_t filter = 0;\
817
+        for (n = 1; n <= LPC_ORDER; n++) {\
818
+            filter -= (fir_coef)[n - 1] * (src)[m - n] -\
819
+                      (iir_coef)[n - 1] * ((dest)[m - n] >> in_shift);\
820
+        }\
821
+\
822
+        (dest)[m] = av_clipl_int32(((src)[m] << 16) + (filter << 3) +\
823
+                                   (1 << 15)) >> res_shift;\
824
+    }\
825
+}
826
+
827
+/**
828
+ * Adjust gain of postfiltered signal.
829
+ *
830
+ * @param p      the context
831
+ * @param buf    postfiltered output vector
832
+ * @param energy input energy coefficient
833
+ */
834
+static void gain_scale(G723_1_Context *p, int16_t * buf, int energy)
835
+{
836
+    int num, denom, gain, bits1, bits2;
837
+    int i;
838
+
839
+    num   = energy;
840
+    denom = 0;
841
+    for (i = 0; i < SUBFRAME_LEN; i++) {
842
+        int64_t temp = buf[i] >> 2;
843
+        temp  = av_clipl_int32(MUL64(temp, temp) << 1);
844
+        denom = av_clipl_int32(denom + temp);
845
+    }
846
+
847
+    if (num && denom) {
848
+        bits1   = normalize_bits(num, 1);
849
+        bits2   = normalize_bits(denom, 1);
850
+        num     = num << bits1 >> 1;
851
+        denom <<= bits2;
852
+
853
+        bits2 = 5 + bits1 - bits2;
854
+        bits2 = FFMAX(0, bits2);
855
+
856
+        gain = (num >> 1) / (denom >> 16);
857
+        gain = square_root(gain << 16 >> bits2);
858
+    } else {
859
+        gain = 1 << 12;
860
+    }
861
+
862
+    for (i = 0; i < SUBFRAME_LEN; i++) {
863
+        p->pf_gain = ((p->pf_gain << 4) - p->pf_gain + gain + (1 << 3)) >> 4;
864
+        buf[i]     = av_clip_int16((buf[i] * (p->pf_gain + (p->pf_gain >> 4)) +
865
+                                   (1 << 10)) >> 11);
866
+    }
867
+}
868
+
869
+/**
870
+ * Perform formant filtering.
871
+ *
872
+ * @param p   the context
873
+ * @param lpc quantized lpc coefficients
874
+ * @param buf output buffer
875
+ */
876
+static void formant_postfilter(G723_1_Context *p, int16_t *lpc, int16_t *buf)
877
+{
878
+    int16_t filter_coef[2][LPC_ORDER], *buf_ptr;
879
+    int filter_signal[LPC_ORDER + FRAME_LEN], *signal_ptr;
880
+    int i, j, k;
881
+
882
+    memcpy(buf, p->fir_mem, LPC_ORDER * sizeof(int16_t));
883
+    memcpy(filter_signal, p->iir_mem, LPC_ORDER * sizeof(int));
884
+
885
+    for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
886
+        for (k = 0; k < LPC_ORDER; k++) {
887
+            filter_coef[0][k] = (-lpc[k] * postfilter_tbl[0][k] +
888
+                                 (1 << 14)) >> 15;
889
+            filter_coef[1][k] = (-lpc[k] * postfilter_tbl[1][k] +
890
+                                 (1 << 14)) >> 15;
891
+        }
892
+        iir_filter(filter_coef[0], filter_coef[1], buf + i,
893
+                   filter_signal + i, 1);
894
+    }
895
+
896
+    memcpy(p->fir_mem, buf + FRAME_LEN, LPC_ORDER * sizeof(int16_t));
897
+    memcpy(p->iir_mem, filter_signal + FRAME_LEN, LPC_ORDER * sizeof(int));
898
+
899
+    buf_ptr    = buf + LPC_ORDER;
900
+    signal_ptr = filter_signal + LPC_ORDER;
901
+    for (i = 0; i < SUBFRAMES; i++) {
902
+        int16_t temp_vector[SUBFRAME_LEN];
903
+        int16_t temp;
904
+        int auto_corr[2];
905
+        int scale, energy;
906
+
907
+        /* Normalize */
908
+        memcpy(temp_vector, buf_ptr, SUBFRAME_LEN * sizeof(int16_t));
909
+        scale = scale_vector(temp_vector, SUBFRAME_LEN);
910
+
911
+        /* Compute auto correlation coefficients */
912
+        auto_corr[0] = ff_dot_product(temp_vector, temp_vector + 1,
913
+                                      SUBFRAME_LEN - 1, 1);
914
+        auto_corr[1] = ff_dot_product(temp_vector, temp_vector,
915
+                                      SUBFRAME_LEN, 1);
916
+
917
+        /* Compute reflection coefficient */
918
+        temp = auto_corr[1] >> 16;
919
+        if (temp) {
920
+            temp = (auto_corr[0] >> 2) / temp;
921
+        }
922
+        p->reflection_coef = ((p->reflection_coef << 2) - p->reflection_coef +
923
+                              temp + 2) >> 2;
924
+        temp = (p->reflection_coef * 0xffffc >> 3) & 0xfffc;
925
+
926
+        /* Compensation filter */
927
+        for (j = 0; j < SUBFRAME_LEN; j++) {
928
+            buf_ptr[j] = av_clipl_int32(signal_ptr[j] +
929
+                                        ((signal_ptr[j - 1] >> 16) *
930
+                                         temp << 1)) >> 16;
931
+        }
932
+
933
+        /* Compute normalized signal energy */
934
+        temp = 2 * scale + 4;
935
+        if (temp < 0) {
936
+            energy = av_clipl_int32((int64_t)auto_corr[1] << -temp);
937
+        } else
938
+            energy = auto_corr[1] >> temp;
939
+
940
+        gain_scale(p, buf_ptr, energy);
941
+
942
+        buf_ptr    += SUBFRAME_LEN;
943
+        signal_ptr += SUBFRAME_LEN;
944
+    }
945
+}
946
+
947
+static int g723_1_decode_frame(AVCodecContext *avctx, void *data,
948
+                               int *data_size, AVPacket *avpkt)
949
+{
950
+    G723_1_Context *p  = avctx->priv_data;
951
+    const uint8_t *buf = avpkt->data;
952
+    int buf_size       = avpkt->size;
953
+    int16_t *out       = data;
954
+    int dec_mode       = buf[0] & 3;
955
+
956
+    PPFParam ppf[SUBFRAMES];
957
+    int16_t cur_lsp[LPC_ORDER];
958
+    int16_t lpc[SUBFRAMES * LPC_ORDER];
959
+    int16_t acb_vector[SUBFRAME_LEN];
960
+    int16_t *vector_ptr;
961
+    int bad_frame = 0, i, j;
962
+
963
+    if (!buf_size || buf_size < frame_size[dec_mode]) {
964
+        *data_size = 0;
965
+        return buf_size;
966
+    }
967
+
968
+    if (unpack_bitstream(p, buf, buf_size) < 0) {
969
+        bad_frame         = 1;
970
+        p->cur_frame_type = p->past_frame_type == ActiveFrame ?
971
+                            ActiveFrame : UntransmittedFrame;
972
+    }
973
+
974
+    *data_size = FRAME_LEN * sizeof(int16_t);
975
+    if(p->cur_frame_type == ActiveFrame) {
976
+        if (!bad_frame) {
977
+            p->erased_frames = 0;
978
+        } else if(p->erased_frames != 3)
979
+            p->erased_frames++;
980
+
981
+        inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, bad_frame);
982
+        lsp_interpolate(lpc, cur_lsp, p->prev_lsp);
983
+
984
+        /* Save the lsp_vector for the next frame */
985
+        memcpy(p->prev_lsp, cur_lsp, LPC_ORDER * sizeof(int16_t));
986
+
987
+        /* Generate the excitation for the frame */
988
+        memcpy(p->excitation, p->prev_excitation, PITCH_MAX * sizeof(int16_t));
989
+        vector_ptr = p->excitation + PITCH_MAX;
990
+        if (!p->erased_frames) {
991
+            /* Update interpolation gain memory */
992
+            p->interp_gain = fixed_cb_gain[(p->subframe[2].amp_index +
993
+                                            p->subframe[3].amp_index) >> 1];
994
+            for (i = 0; i < SUBFRAMES; i++) {
995
+                gen_fcb_excitation(vector_ptr, p->subframe[i], p->cur_rate,
996
+                                   p->pitch_lag[i >> 1], i);
997
+                gen_acb_excitation(acb_vector, &p->excitation[SUBFRAME_LEN * i],
998
+                                   p->pitch_lag[i >> 1], p->subframe[i],
999
+                                   p->cur_rate);
1000
+                /* Get the total excitation */
1001
+                for (j = 0; j < SUBFRAME_LEN; j++) {
1002
+                    vector_ptr[j] = av_clip_int16(vector_ptr[j] << 1);
1003
+                    vector_ptr[j] = av_clip_int16(vector_ptr[j] +
1004
+                                                  acb_vector[j]);
1005
+                }
1006
+                vector_ptr += SUBFRAME_LEN;
1007
+            }
1008
+
1009
+            vector_ptr = p->excitation + PITCH_MAX;
1010
+
1011
+            /* Save the excitation */
1012
+            memcpy(out, vector_ptr, FRAME_LEN * sizeof(int16_t));
1013
+
1014
+            p->interp_index = comp_interp_index(p, p->pitch_lag[1],
1015
+                                                &p->sid_gain, &p->cur_gain);
1016
+
1017
+            for (i = PITCH_MAX, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1018
+                comp_ppf_coeff(p, i, p->pitch_lag[j >> 1],
1019
+                               ppf + j, p->cur_rate);
1020
+
1021
+            /* Restore the original excitation */
1022
+            memcpy(p->excitation, p->prev_excitation,
1023
+                   PITCH_MAX * sizeof(int16_t));
1024
+            memcpy(vector_ptr, out, FRAME_LEN * sizeof(int16_t));
1025
+
1026
+            /* Peform pitch postfiltering */
1027
+            for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1028
+                ff_acelp_weighted_vector_sum(out + LPC_ORDER + i, vector_ptr + i,
1029
+                                             vector_ptr + i + ppf[j].index,
1030
+                                             ppf[j].sc_gain, ppf[j].opt_gain,
1031
+                                             1 << 14, 15, SUBFRAME_LEN);
1032
+        } else {
1033
+            p->interp_gain = (p->interp_gain * 3 + 2) >> 2;
1034
+            if (p->erased_frames == 3) {
1035
+                /* Mute output */
1036
+                memset(p->excitation, 0,
1037
+                       (FRAME_LEN + PITCH_MAX) * sizeof(int16_t));
1038
+                memset(out, 0, (FRAME_LEN + LPC_ORDER) * sizeof(int16_t));
1039
+            } else {
1040
+                /* Regenerate frame */
1041
+                residual_interp(p->excitation, out + LPC_ORDER, p->interp_index,
1042
+                                p->interp_gain, &p->random_seed);
1043
+            }
1044
+        }
1045
+        /* Save the excitation for the next frame */
1046
+        memcpy(p->prev_excitation, p->excitation + FRAME_LEN,
1047
+               PITCH_MAX * sizeof(int16_t));
1048
+    } else {
1049
+        memset(out, 0, *data_size);
1050
+        av_log(avctx, AV_LOG_WARNING,
1051
+               "G.723.1: Comfort noise generation not supported yet\n");
1052
+        return frame_size[dec_mode];
1053
+    }
1054
+
1055
+    p->past_frame_type = p->cur_frame_type;
1056
+
1057
+    memcpy(out, p->synth_mem, LPC_ORDER * sizeof(int16_t));
1058
+    for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1059
+        ff_celp_lp_synthesis_filter(out + i, &lpc[j * LPC_ORDER],
1060
+                                    out + i, SUBFRAME_LEN, LPC_ORDER,
1061
+                                    0, 1, 1 << 12);
1062
+    memcpy(p->synth_mem, out + FRAME_LEN, LPC_ORDER * sizeof(int16_t));
1063
+
1064
+    formant_postfilter(p, lpc, out);
1065
+
1066
+    memmove(out, out + LPC_ORDER, *data_size);
1067
+
1068
+    return frame_size[dec_mode];
1069
+}
1070
+
1071
+AVCodec ff_g723_1_decoder = {
1072
+    .name           = "g723_1",
1073
+    .type           = AVMEDIA_TYPE_AUDIO,
1074
+    .id             = CODEC_ID_G723_1,
1075
+    .priv_data_size = sizeof(G723_1_Context),
1076
+    .init           = g723_1_decode_init,
1077
+    .decode         = g723_1_decode_frame,
1078
+    .long_name      = NULL_IF_CONFIG_SMALL("G.723.1"),
1079
+    .capabilities   = CODEC_CAP_SUBFRAMES,
1080
+};
... ...
@@ -21,7 +21,7 @@
21 21
 #define AVCODEC_VERSION_H
22 22
 
23 23
 #define LIBAVCODEC_VERSION_MAJOR 53
24
-#define LIBAVCODEC_VERSION_MINOR 18
24
+#define LIBAVCODEC_VERSION_MINOR 19
25 25
 #define LIBAVCODEC_VERSION_MICRO  0
26 26
 
27 27
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -98,6 +98,7 @@ OBJS-$(CONFIG_GXF_DEMUXER)               += gxf.o
98 98
 OBJS-$(CONFIG_GXF_MUXER)                 += gxfenc.o audiointerleave.o
99 99
 OBJS-$(CONFIG_G722_DEMUXER)              += rawdec.o
100 100
 OBJS-$(CONFIG_G722_MUXER)                += rawenc.o
101
+OBJS-$(CONFIG_G723_1_DEMUXER)            += g723_1.o
101 102
 OBJS-$(CONFIG_H261_DEMUXER)              += h261dec.o rawdec.o
102 103
 OBJS-$(CONFIG_H261_MUXER)                += rawenc.o
103 104
 OBJS-$(CONFIG_H263_DEMUXER)              += h263dec.o rawdec.o
... ...
@@ -100,6 +100,7 @@ void av_register_all(void)
100 100
     REGISTER_MUXER    (FRAMECRC, framecrc);
101 101
     REGISTER_MUXER    (FRAMEMD5, framemd5);
102 102
     REGISTER_MUXDEMUX (G722, g722);
103
+    REGISTER_DEMUXER  (G723_1, g723_1);
103 104
     REGISTER_MUXER    (GIF, gif);
104 105
     REGISTER_DEMUXER  (GSM, gsm);
105 106
     REGISTER_MUXDEMUX (GXF, gxf);
106 107
new file mode 100644
... ...
@@ -0,0 +1,83 @@
0
+/*
1
+ * G.723.1 demuxer
2
+ * Copyright (c) 2010 Mohamed Naufal Basheer
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * G.723.1 demuxer
24
+ */
25
+
26
+#include "avformat.h"
27
+
28
+static const uint8_t frame_size[4] = {24, 20, 4, 1};
29
+
30
+static int g723_1_init(AVFormatContext *s, AVFormatParameters *ap)
31
+{
32
+    AVStream *st;
33
+
34
+    st = av_new_stream(s, 0);
35
+    if (!st)
36
+        return AVERROR(ENOMEM);
37
+
38
+    st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
39
+    st->codec->codec_id    = CODEC_ID_G723_1;
40
+    st->codec->channels    = 1;
41
+    st->codec->sample_rate = 8000;
42
+
43
+    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
44
+
45
+    return 0;
46
+}
47
+
48
+static int g723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
49
+{
50
+    int size, byte, ret;
51
+
52
+    pkt->pos = url_ftell(s->pb);
53
+    byte     = get_byte(s->pb);
54
+    size     = frame_size[byte & 3];
55
+
56
+    ret = av_new_packet(pkt, size);
57
+    if (ret < 0)
58
+        return ret;
59
+
60
+    pkt->data[0]      = byte;
61
+    pkt->duration     = 240;
62
+    pkt->stream_index = 0;
63
+
64
+    ret = get_buffer(s->pb, pkt->data + 1, size - 1);
65
+    if (ret < size - 1) {
66
+        av_free_packet(pkt);
67
+        return ret < 0 ? ret : AVERROR_EOF;
68
+    }
69
+
70
+    return pkt->size;
71
+}
72
+
73
+AVInputFormat ff_g723_1_demuxer = {
74
+    "g723_1",
75
+    NULL_IF_CONFIG_SMALL("G.723.1 format"),
76
+    0,
77
+    NULL,
78
+    g723_1_init,
79
+    g723_1_read_packet,
80
+    .extensions = "tco",
81
+    .flags = AVFMT_GENERIC_INDEX
82
+};
... ...
@@ -44,7 +44,7 @@ static const struct
44 44
 {
45 45
   {0, "PCMU",        AVMEDIA_TYPE_AUDIO,   CODEC_ID_PCM_MULAW, 8000, 1},
46 46
   {3, "GSM",         AVMEDIA_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
47
-  {4, "G723",        AVMEDIA_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
47
+  {4, "G723",        AVMEDIA_TYPE_AUDIO,   CODEC_ID_G723_1, 8000, 1},
48 48
   {5, "DVI4",        AVMEDIA_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
49 49
   {6, "DVI4",        AVMEDIA_TYPE_AUDIO,   CODEC_ID_NONE, 16000, 1},
50 50
   {7, "LPC",         AVMEDIA_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},