GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/g723_1enc.c Lines: 564 580 97.2 %
Date: 2019-11-22 03:34:36 Branches: 245 260 94.2 %

Line Branch Exec Source
1
/*
2
 * G.723.1 compatible encoder
3
 * Copyright (c) Mohamed Naufal <naufal22@gmail.com>
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 encoder
25
 */
26
27
#include <stdint.h>
28
#include <string.h>
29
30
#include "libavutil/channel_layout.h"
31
#include "libavutil/common.h"
32
#include "libavutil/mem.h"
33
#include "libavutil/opt.h"
34
35
#include "avcodec.h"
36
#include "celp_math.h"
37
#include "g723_1.h"
38
#include "internal.h"
39
40
#define BITSTREAM_WRITER_LE
41
#include "put_bits.h"
42
43
1
static av_cold int g723_1_encode_init(AVCodecContext *avctx)
44
{
45
1
    G723_1_Context *s = avctx->priv_data;
46
1
    G723_1_ChannelContext *p = &s->ch[0];
47
48
1
    if (avctx->sample_rate != 8000) {
49
        av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
50
        return AVERROR(EINVAL);
51
    }
52
53
1
    if (avctx->channels != 1) {
54
        av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
55
        return AVERROR(EINVAL);
56
    }
57
58
1
    if (avctx->bit_rate == 6300) {
59
1
        p->cur_rate = RATE_6300;
60
    } else if (avctx->bit_rate == 5300) {
61
        av_log(avctx, AV_LOG_ERROR, "Use bitrate 6300 instead of 5300.\n");
62
        avpriv_report_missing_feature(avctx, "Bitrate 5300");
63
        return AVERROR_PATCHWELCOME;
64
    } else {
65
        av_log(avctx, AV_LOG_ERROR, "Bitrate not supported, use 6300\n");
66
        return AVERROR(EINVAL);
67
    }
68
1
    avctx->frame_size = 240;
69
1
    memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(int16_t));
70
71
1
    return 0;
72
}
73
74
/**
75
 * Remove DC component from the input signal.
76
 *
77
 * @param buf input signal
78
 * @param fir zero memory
79
 * @param iir pole memory
80
 */
81
200
static void highpass_filter(int16_t *buf, int16_t *fir, int *iir)
82
{
83
    int i;
84
48200
    for (i = 0; i < FRAME_LEN; i++) {
85
48000
        *iir   = (buf[i] << 15) + ((-*fir) << 15) + MULL2(*iir, 0x7f00);
86
48000
        *fir   = buf[i];
87
48000
        buf[i] = av_clipl_int32((int64_t)*iir + (1 << 15)) >> 16;
88
    }
89
200
}
90
91
/**
92
 * Estimate autocorrelation of the input vector.
93
 *
94
 * @param buf      input buffer
95
 * @param autocorr autocorrelation coefficients vector
96
 */
97
800
static void comp_autocorr(int16_t *buf, int16_t *autocorr)
98
{
99
    int i, scale, temp;
100
    int16_t vector[LPC_FRAME];
101
102
800
    ff_g723_1_scale_vector(vector, buf, LPC_FRAME);
103
104
    /* Apply the Hamming window */
105
144800
    for (i = 0; i < LPC_FRAME; i++)
106
144000
        vector[i] = (vector[i] * hamming_window[i] + (1 << 14)) >> 15;
107
108
    /* Compute the first autocorrelation coefficient */
109
800
    temp = ff_dot_product(vector, vector, LPC_FRAME);
110
111
    /* Apply a white noise correlation factor of (1025/1024) */
112
800
    temp += temp >> 10;
113
114
    /* Normalize */
115
800
    scale       = ff_g723_1_normalize_bits(temp, 31);
116
800
    autocorr[0] = av_clipl_int32((int64_t) (temp << scale) +
117
800
                                 (1 << 15)) >> 16;
118
119
    /* Compute the remaining coefficients */
120
800
    if (!autocorr[0]) {
121
        memset(autocorr + 1, 0, LPC_ORDER * sizeof(int16_t));
122
    } else {
123
8800
        for (i = 1; i <= LPC_ORDER; i++) {
124
8000
            temp        = ff_dot_product(vector, vector + i, LPC_FRAME - i);
125
8000
            temp        = MULL2((temp << scale), binomial_window[i - 1]);
126
8000
            autocorr[i] = av_clipl_int32((int64_t) temp + (1 << 15)) >> 16;
127
        }
128
    }
129
800
}
130
131
/**
132
 * Use Levinson-Durbin recursion to compute LPC coefficients from
133
 * autocorrelation values.
134
 *
135
 * @param lpc      LPC coefficients vector
136
 * @param autocorr autocorrelation coefficients vector
137
 * @param error    prediction error
138
 */
139
800
static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)
140
{
141
    int16_t vector[LPC_ORDER];
142
    int16_t partial_corr;
143
    int i, j, temp;
144
145
800
    memset(lpc, 0, LPC_ORDER * sizeof(int16_t));
146
147
8800
    for (i = 0; i < LPC_ORDER; i++) {
148
        /* Compute the partial correlation coefficient */
149
8000
        temp = 0;
150
44000
        for (j = 0; j < i; j++)
151
36000
            temp -= lpc[j] * autocorr[i - j - 1];
152
8000
        temp = ((autocorr[i] << 13) + temp) << 3;
153
154
8000
        if (FFABS(temp) >= (error << 16))
155
            break;
156
157
8000
        partial_corr = temp / (error << 1);
158
159
8000
        lpc[i] = av_clipl_int32((int64_t) (partial_corr << 14) +
160
8000
                                (1 << 15)) >> 16;
161
162
        /* Update the prediction error */
163
8000
        temp  = MULL2(temp, partial_corr);
164
8000
        error = av_clipl_int32((int64_t) (error << 16) - temp +
165
8000
                               (1 << 15)) >> 16;
166
167
8000
        memcpy(vector, lpc, i * sizeof(int16_t));
168
44000
        for (j = 0; j < i; j++) {
169
36000
            temp   = partial_corr * vector[i - j - 1] << 1;
170
36000
            lpc[j] = av_clipl_int32((int64_t) (lpc[j] << 16) - temp +
171
36000
                                    (1 << 15)) >> 16;
172
        }
173
    }
174
800
}
175
176
/**
177
 * Calculate LPC coefficients for the current frame.
178
 *
179
 * @param buf       current frame
180
 * @param prev_data 2 trailing subframes of the previous frame
181
 * @param lpc       LPC coefficients vector
182
 */
183
200
static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
184
{
185
    int16_t autocorr[(LPC_ORDER + 1) * SUBFRAMES];
186
200
    int16_t *autocorr_ptr = autocorr;
187
200
    int16_t *lpc_ptr      = lpc;
188
    int i, j;
189
190
1000
    for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
191
800
        comp_autocorr(buf + i, autocorr_ptr);
192
800
        levinson_durbin(lpc_ptr, autocorr_ptr + 1, autocorr_ptr[0]);
193
194
800
        lpc_ptr      += LPC_ORDER;
195
800
        autocorr_ptr += LPC_ORDER + 1;
196
    }
197
200
}
198
199
200
static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
200
{
201
    int f[LPC_ORDER + 2]; ///< coefficients of the sum and difference
202
                          ///< polynomials (F1, F2) ordered as
203
                          ///< f1[0], f2[0], ...., f1[5], f2[5]
204
205
    int max, shift, cur_val, prev_val, count, p;
206
    int i, j;
207
    int64_t temp;
208
209
    /* Initialize f1[0] and f2[0] to 1 in Q25 */
210
2200
    for (i = 0; i < LPC_ORDER; i++)
211
2000
        lsp[i] = (lpc[i] * bandwidth_expand[i] + (1 << 14)) >> 15;
212
213
    /* Apply bandwidth expansion on the LPC coefficients */
214
200
    f[0] = f[1] = 1 << 25;
215
216
    /* Compute the remaining coefficients */
217
1200
    for (i = 0; i < LPC_ORDER / 2; i++) {
218
        /* f1 */
219
1000
        f[2 * i + 2] = -f[2 * i] - ((lsp[i] + lsp[LPC_ORDER - 1 - i]) << 12);
220
        /* f2 */
221
1000
        f[2 * i + 3] = f[2 * i + 1] - ((lsp[i] - lsp[LPC_ORDER - 1 - i]) << 12);
222
    }
223
224
    /* Divide f1[5] and f2[5] by 2 for use in polynomial evaluation */
225
200
    f[LPC_ORDER]     >>= 1;
226
200
    f[LPC_ORDER + 1] >>= 1;
227
228
    /* Normalize and shorten */
229
200
    max = FFABS(f[0]);
230
2400
    for (i = 1; i < LPC_ORDER + 2; i++)
231
2200
        max = FFMAX(max, FFABS(f[i]));
232
233
200
    shift = ff_g723_1_normalize_bits(max, 31);
234
235
2600
    for (i = 0; i < LPC_ORDER + 2; i++)
236
2400
        f[i] = av_clipl_int32((int64_t) (f[i] << shift) + (1 << 15)) >> 16;
237
238
    /**
239
     * Evaluate F1 and F2 at uniform intervals of pi/256 along the
240
     * unit circle and check for zero crossings.
241
     */
242
200
    p    = 0;
243
200
    temp = 0;
244
1400
    for (i = 0; i <= LPC_ORDER / 2; i++)
245
1200
        temp += f[2 * i] * cos_tab[0];
246
200
    prev_val = av_clipl_int32(temp << 1);
247
200
    count    = 0;
248
46245
    for (i = 1; i < COS_TBL_SIZE / 2; i++) {
249
        /* Evaluate */
250
46245
        temp = 0;
251
323715
        for (j = 0; j <= LPC_ORDER / 2; j++)
252
277470
            temp += f[LPC_ORDER - 2 * j + p] * cos_tab[i * j % COS_TBL_SIZE];
253
46245
        cur_val = av_clipl_int32(temp << 1);
254
255
        /* Check for sign change, indicating a zero crossing */
256
46245
        if ((cur_val ^ prev_val) < 0) {
257
2000
            int abs_cur  = FFABS(cur_val);
258
2000
            int abs_prev = FFABS(prev_val);
259
2000
            int sum      = abs_cur + abs_prev;
260
261
2000
            shift        = ff_g723_1_normalize_bits(sum, 31);
262
2000
            sum        <<= shift;
263
2000
            abs_prev     = abs_prev << shift >> 8;
264
2000
            lsp[count++] = ((i - 1) << 7) + (abs_prev >> 1) / (sum >> 16);
265
266
2000
            if (count == LPC_ORDER)
267
200
                break;
268
269
            /* Switch between sum and difference polynomials */
270
1800
            p ^= 1;
271
272
            /* Evaluate */
273
1800
            temp = 0;
274
12600
            for (j = 0; j <= LPC_ORDER / 2; j++)
275
10800
                temp += f[LPC_ORDER - 2 * j + p] *
276
10800
                        cos_tab[i * j % COS_TBL_SIZE];
277
1800
            cur_val = av_clipl_int32(temp << 1);
278
        }
279
46045
        prev_val = cur_val;
280
    }
281
282
200
    if (count != LPC_ORDER)
283
        memcpy(lsp, prev_lsp, LPC_ORDER * sizeof(int16_t));
284
200
}
285
286
/**
287
 * Quantize the current LSP subvector.
288
 *
289
 * @param num    band number
290
 * @param offset offset of the current subvector in an LPC_ORDER vector
291
 * @param size   size of the current subvector
292
 */
293
#define get_index(num, offset, size)                                          \
294
{                                                                             \
295
    int error, max = -1;                                                      \
296
    int16_t temp[4];                                                          \
297
    int i, j;                                                                 \
298
                                                                              \
299
    for (i = 0; i < LSP_CB_SIZE; i++) {                                       \
300
        for (j = 0; j < size; j++){                                           \
301
            temp[j] = (weight[j + (offset)] * lsp_band##num[i][j] +           \
302
                      (1 << 14)) >> 15;                                       \
303
        }                                                                     \
304
        error  = ff_g723_1_dot_product(lsp + (offset), temp, size) << 1;      \
305
        error -= ff_g723_1_dot_product(lsp_band##num[i], temp, size);         \
306
        if (error > max) {                                                    \
307
            max = error;                                                      \
308
            lsp_index[num] = i;                                               \
309
        }                                                                     \
310
    }                                                                         \
311
}
312
313
/**
314
 * Vector quantize the LSP frequencies.
315
 *
316
 * @param lsp      the current lsp vector
317
 * @param prev_lsp the previous lsp vector
318
 */
319
200
static void lsp_quantize(uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
320
{
321
    int16_t weight[LPC_ORDER];
322
    int16_t min, max;
323
    int shift, i;
324
325
    /* Calculate the VQ weighting vector */
326
200
    weight[0]             = (1 << 20) / (lsp[1] - lsp[0]);
327
200
    weight[LPC_ORDER - 1] = (1 << 20) /
328
200
                            (lsp[LPC_ORDER - 1] - lsp[LPC_ORDER - 2]);
329
330
1800
    for (i = 1; i < LPC_ORDER - 1; i++) {
331
1600
        min = FFMIN(lsp[i] - lsp[i - 1], lsp[i + 1] - lsp[i]);
332
1600
        if (min > 0x20)
333
1600
            weight[i] = (1 << 20) / min;
334
        else
335
            weight[i] = INT16_MAX;
336
    }
337
338
    /* Normalize */
339
200
    max = 0;
340
2200
    for (i = 0; i < LPC_ORDER; i++)
341
2000
        max = FFMAX(weight[i], max);
342
343
200
    shift = ff_g723_1_normalize_bits(max, 15);
344
2200
    for (i = 0; i < LPC_ORDER; i++) {
345
2000
        weight[i] <<= shift;
346
    }
347
348
    /* Compute the VQ target vector */
349
2200
    for (i = 0; i < LPC_ORDER; i++) {
350
2000
        lsp[i] -= dc_lsp[i] +
351
2000
                  (((prev_lsp[i] - dc_lsp[i]) * 12288 + (1 << 14)) >> 15);
352
    }
353
354

205000
    get_index(0, 0, 3);
355

205000
    get_index(1, 3, 3);
356

256200
    get_index(2, 6, 4);
357
200
}
358
359
/**
360
 * Perform IIR filtering.
361
 *
362
 * @param fir_coef FIR coefficients
363
 * @param iir_coef IIR coefficients
364
 * @param src      source vector
365
 * @param dest     destination vector
366
 */
367
800
static void iir_filter(int16_t *fir_coef, int16_t *iir_coef,
368
                       int16_t *src, int16_t *dest)
369
{
370
    int m, n;
371
372
48800
    for (m = 0; m < SUBFRAME_LEN; m++) {
373
48000
        int64_t filter = 0;
374
528000
        for (n = 1; n <= LPC_ORDER; n++) {
375
480000
            filter -= fir_coef[n - 1] * src[m - n] -
376
480000
                      iir_coef[n - 1] * dest[m - n];
377
        }
378
379
48000
        dest[m] = av_clipl_int32((src[m] << 16) + (filter << 3) +
380
48000
                                 (1 << 15)) >> 16;
381
    }
382
800
}
383
384
/**
385
 * Apply the formant perceptual weighting filter.
386
 *
387
 * @param flt_coef filter coefficients
388
 * @param unq_lpc  unquantized lpc vector
389
 */
390
200
static void perceptual_filter(G723_1_ChannelContext *p, int16_t *flt_coef,
391
                              int16_t *unq_lpc, int16_t *buf)
392
{
393
    int16_t vector[FRAME_LEN + LPC_ORDER];
394
200
    int i, j, k, l = 0;
395
396
200
    memcpy(buf, p->iir_mem, sizeof(int16_t) * LPC_ORDER);
397
200
    memcpy(vector, p->fir_mem, sizeof(int16_t) * LPC_ORDER);
398
200
    memcpy(vector + LPC_ORDER, buf + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
399
400
1000
    for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
401
8800
        for (k = 0; k < LPC_ORDER; k++) {
402
8000
            flt_coef[k + 2 * l] = (unq_lpc[k + l] * percept_flt_tbl[0][k] +
403
8000
                                   (1 << 14)) >> 15;
404
8000
            flt_coef[k + 2 * l + LPC_ORDER] = (unq_lpc[k + l] *
405
8000
                                               percept_flt_tbl[1][k] +
406
8000
                                               (1 << 14)) >> 15;
407
        }
408
800
        iir_filter(flt_coef + 2 * l, flt_coef + 2 * l + LPC_ORDER,
409
800
                   vector + i, buf + i);
410
800
        l += LPC_ORDER;
411
    }
412
200
    memcpy(p->iir_mem, buf + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
413
200
    memcpy(p->fir_mem, vector + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
414
200
}
415
416
/**
417
 * Estimate the open loop pitch period.
418
 *
419
 * @param buf   perceptually weighted speech
420
 * @param start estimation is carried out from this position
421
 */
422
400
static int estimate_pitch(int16_t *buf, int start)
423
{
424
400
    int max_exp = 32;
425
400
    int max_ccr = 0x4000;
426
400
    int max_eng = 0x7fff;
427
400
    int index   = PITCH_MIN;
428
400
    int offset  = start - PITCH_MIN + 1;
429
430
    int ccr, eng, orig_eng, ccr_eng, exp;
431
    int diff, temp;
432
433
    int i;
434
435
400
    orig_eng = ff_dot_product(buf + offset, buf + offset, HALF_FRAME_LEN);
436
437
50400
    for (i = PITCH_MIN; i <= PITCH_MAX - 3; i++) {
438
50000
        offset--;
439
440
        /* Update energy and compute correlation */
441
50000
        orig_eng += buf[offset] * buf[offset] -
442
50000
                    buf[offset + HALF_FRAME_LEN] * buf[offset + HALF_FRAME_LEN];
443
50000
        ccr = ff_dot_product(buf + start, buf + offset, HALF_FRAME_LEN);
444
50000
        if (ccr <= 0)
445
25406
            continue;
446
447
        /* Split into mantissa and exponent to maintain precision */
448
24594
        exp   = ff_g723_1_normalize_bits(ccr, 31);
449
24594
        ccr   = av_clipl_int32((int64_t) (ccr << exp) + (1 << 15)) >> 16;
450
24594
        exp <<= 1;
451
24594
        ccr  *= ccr;
452
24594
        temp  = ff_g723_1_normalize_bits(ccr, 31);
453
24594
        ccr   = ccr << temp >> 16;
454
24594
        exp  += temp;
455
456
24594
        temp = ff_g723_1_normalize_bits(orig_eng, 31);
457
24594
        eng  = av_clipl_int32((int64_t) (orig_eng << temp) + (1 << 15)) >> 16;
458
24594
        exp -= temp;
459
460
24594
        if (ccr >= eng) {
461
9755
            exp--;
462
9755
            ccr >>= 1;
463
        }
464
24594
        if (exp > max_exp)
465
19555
            continue;
466
467
5039
        if (exp + 1 < max_exp)
468
502
            goto update;
469
470
        /* Equalize exponents before comparison */
471
4537
        if (exp + 1 == max_exp)
472
364
            temp = max_ccr >> 1;
473
        else
474
4173
            temp = max_ccr;
475
4537
        ccr_eng = ccr * max_eng;
476
4537
        diff    = ccr_eng - eng * temp;
477

4537
        if (diff > 0 && (i - index < PITCH_MIN || diff > ccr_eng >> 2)) {
478
693
update:
479
1195
            index   = i;
480
1195
            max_exp = exp;
481
1195
            max_ccr = ccr;
482
1195
            max_eng = eng;
483
        }
484
    }
485
400
    return index;
486
}
487
488
/**
489
 * Compute harmonic noise filter parameters.
490
 *
491
 * @param buf       perceptually weighted speech
492
 * @param pitch_lag open loop pitch period
493
 * @param hf        harmonic filter parameters
494
 */
495
800
static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag, HFParam *hf)
496
{
497
    int ccr, eng, max_ccr, max_eng;
498
    int exp, max, diff;
499
    int energy[15];
500
    int i, j;
501
502
6400
    for (i = 0, j = pitch_lag - 3; j <= pitch_lag + 3; i++, j++) {
503
        /* Compute residual energy */
504
5600
        energy[i << 1] = ff_dot_product(buf - j, buf - j, SUBFRAME_LEN);
505
        /* Compute correlation */
506
5600
        energy[(i << 1) + 1] = ff_dot_product(buf, buf - j, SUBFRAME_LEN);
507
    }
508
509
    /* Compute target energy */
510
800
    energy[14] = ff_dot_product(buf, buf, SUBFRAME_LEN);
511
512
    /* Normalize */
513
800
    max = 0;
514
12800
    for (i = 0; i < 15; i++)
515
12000
        max = FFMAX(max, FFABS(energy[i]));
516
517
800
    exp = ff_g723_1_normalize_bits(max, 31);
518
12800
    for (i = 0; i < 15; i++) {
519
12000
        energy[i] = av_clipl_int32((int64_t)(energy[i] << exp) +
520
12000
                                   (1 << 15)) >> 16;
521
    }
522
523
800
    hf->index = -1;
524
800
    hf->gain  =  0;
525
800
    max_ccr   =  1;
526
800
    max_eng   =  0x7fff;
527
528
6400
    for (i = 0; i <= 6; i++) {
529
5600
        eng = energy[i << 1];
530
5600
        ccr = energy[(i << 1) + 1];
531
532
5600
        if (ccr <= 0)
533
1732
            continue;
534
535
3868
        ccr  = (ccr * ccr + (1 << 14)) >> 15;
536
3868
        diff = ccr * max_eng - eng * max_ccr;
537
3868
        if (diff > 0) {
538
2218
            max_ccr   = ccr;
539
2218
            max_eng   = eng;
540
2218
            hf->index = i;
541
        }
542
    }
543
544
800
    if (hf->index == -1) {
545
3
        hf->index = pitch_lag;
546
3
        return;
547
    }
548
549
797
    eng = energy[14] * max_eng;
550
797
    eng = (eng >> 2) + (eng >> 3);
551
797
    ccr = energy[(hf->index << 1) + 1] * energy[(hf->index << 1) + 1];
552
797
    if (eng < ccr) {
553
656
        eng = energy[(hf->index << 1) + 1];
554
555
656
        if (eng >= max_eng)
556
202
            hf->gain = 0x2800;
557
        else
558
454
            hf->gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15;
559
    }
560
797
    hf->index += pitch_lag - 3;
561
}
562
563
/**
564
 * Apply the harmonic noise shaping filter.
565
 *
566
 * @param hf filter parameters
567
 */
568
1600
static void harmonic_filter(HFParam *hf, const int16_t *src, int16_t *dest)
569
{
570
    int i;
571
572
97600
    for (i = 0; i < SUBFRAME_LEN; i++) {
573
96000
        int64_t temp = hf->gain * src[i - hf->index] << 1;
574
96000
        dest[i] = av_clipl_int32((src[i] << 16) - temp + (1 << 15)) >> 16;
575
    }
576
1600
}
577
578
800
static void harmonic_noise_sub(HFParam *hf, const int16_t *src, int16_t *dest)
579
{
580
    int i;
581
48800
    for (i = 0; i < SUBFRAME_LEN; i++) {
582
48000
        int64_t temp = hf->gain * src[i - hf->index] << 1;
583
48000
        dest[i] = av_clipl_int32(((dest[i] - src[i]) << 16) + temp +
584
48000
                                 (1 << 15)) >> 16;
585
    }
586
800
}
587
588
/**
589
 * Combined synthesis and formant perceptual weighting filer.
590
 *
591
 * @param qnt_lpc  quantized lpc coefficients
592
 * @param perf_lpc perceptual filter coefficients
593
 * @param perf_fir perceptual filter fir memory
594
 * @param perf_iir perceptual filter iir memory
595
 * @param scale    the filter output will be scaled by 2^scale
596
 */
597
2400
static void synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc,
598
                                 int16_t *perf_fir, int16_t *perf_iir,
599
                                 const int16_t *src, int16_t *dest, int scale)
600
{
601
    int i, j;
602
    int16_t buf_16[SUBFRAME_LEN + LPC_ORDER];
603
    int64_t buf[SUBFRAME_LEN];
604
605
2400
    int16_t *bptr_16 = buf_16 + LPC_ORDER;
606
607
2400
    memcpy(buf_16, perf_fir, sizeof(int16_t) * LPC_ORDER);
608
2400
    memcpy(dest - LPC_ORDER, perf_iir, sizeof(int16_t) * LPC_ORDER);
609
610
146400
    for (i = 0; i < SUBFRAME_LEN; i++) {
611
144000
        int64_t temp = 0;
612
1584000
        for (j = 1; j <= LPC_ORDER; j++)
613
1440000
            temp -= qnt_lpc[j - 1] * bptr_16[i - j];
614
615
144000
        buf[i]     = (src[i] << 15) + (temp << 3);
616
144000
        bptr_16[i] = av_clipl_int32(buf[i] + (1 << 15)) >> 16;
617
    }
618
619
146400
    for (i = 0; i < SUBFRAME_LEN; i++) {
620
144000
        int64_t fir = 0, iir = 0;
621
1584000
        for (j = 1; j <= LPC_ORDER; j++) {
622
1440000
            fir -= perf_lpc[j - 1] * bptr_16[i - j];
623
1440000
            iir += perf_lpc[j + LPC_ORDER - 1] * dest[i - j];
624
        }
625
144000
        dest[i] = av_clipl_int32(((buf[i] + (fir << 3)) << scale) + (iir << 3) +
626
144000
                                 (1 << 15)) >> 16;
627
    }
628
2400
    memcpy(perf_fir, buf_16 + SUBFRAME_LEN, sizeof(int16_t) * LPC_ORDER);
629
2400
    memcpy(perf_iir, dest + SUBFRAME_LEN - LPC_ORDER,
630
           sizeof(int16_t) * LPC_ORDER);
631
2400
}
632
633
/**
634
 * Compute the adaptive codebook contribution.
635
 *
636
 * @param buf   input signal
637
 * @param index the current subframe index
638
 */
639
800
static void acb_search(G723_1_ChannelContext *p, int16_t *residual,
640
                       int16_t *impulse_resp, const int16_t *buf,
641
                       int index)
642
{
643
    int16_t flt_buf[PITCH_ORDER][SUBFRAME_LEN];
644
645
800
    const int16_t *cb_tbl = adaptive_cb_gain85;
646
647
    int ccr_buf[PITCH_ORDER * SUBFRAMES << 2];
648
649
800
    int pitch_lag = p->pitch_lag[index >> 1];
650
800
    int acb_lag   = 1;
651
800
    int acb_gain  = 0;
652
800
    int odd_frame = index & 1;
653
800
    int iter      = 3 + odd_frame;
654
800
    int count     = 0;
655
800
    int tbl_size  = 85;
656
657
    int i, j, k, l, max;
658
    int64_t temp;
659
660
800
    if (!odd_frame) {
661
400
        if (pitch_lag == PITCH_MIN)
662
36
            pitch_lag++;
663
        else
664
364
            pitch_lag = FFMIN(pitch_lag, PITCH_MAX - 5);
665
    }
666
667
3600
    for (i = 0; i < iter; i++) {
668
2800
        ff_g723_1_get_residual(residual, p->prev_excitation, pitch_lag + i - 1);
669
670
170800
        for (j = 0; j < SUBFRAME_LEN; j++) {
671
168000
            temp = 0;
672
5292000
            for (k = 0; k <= j; k++)
673
5124000
                temp += residual[PITCH_ORDER - 1 + k] * impulse_resp[j - k];
674
168000
            flt_buf[PITCH_ORDER - 1][j] = av_clipl_int32((temp << 1) +
675
168000
                                                         (1 << 15)) >> 16;
676
        }
677
678
14000
        for (j = PITCH_ORDER - 2; j >= 0; j--) {
679
11200
            flt_buf[j][0] = ((residual[j] << 13) + (1 << 14)) >> 15;
680
672000
            for (k = 1; k < SUBFRAME_LEN; k++) {
681
660800
                temp = (flt_buf[j + 1][k - 1] << 15) +
682
660800
                       residual[j] * impulse_resp[k];
683
660800
                flt_buf[j][k] = av_clipl_int32((temp << 1) + (1 << 15)) >> 16;
684
            }
685
        }
686
687
        /* Compute crosscorrelation with the signal */
688
16800
        for (j = 0; j < PITCH_ORDER; j++) {
689
14000
            temp             = ff_dot_product(buf, flt_buf[j], SUBFRAME_LEN);
690
14000
            ccr_buf[count++] = av_clipl_int32(temp << 1);
691
        }
692
693
        /* Compute energies */
694
16800
        for (j = 0; j < PITCH_ORDER; j++) {
695
14000
            ccr_buf[count++] = ff_g723_1_dot_product(flt_buf[j], flt_buf[j],
696
                                                     SUBFRAME_LEN);
697
        }
698
699
14000
        for (j = 1; j < PITCH_ORDER; j++) {
700
39200
            for (k = 0; k < j; k++) {
701
28000
                temp             = ff_dot_product(flt_buf[j], flt_buf[k], SUBFRAME_LEN);
702
28000
                ccr_buf[count++] = av_clipl_int32(temp << 2);
703
            }
704
        }
705
    }
706
707
    /* Normalize and shorten */
708
800
    max = 0;
709
56800
    for (i = 0; i < 20 * iter; i++)
710
56000
        max = FFMAX(max, FFABS(ccr_buf[i]));
711
712
800
    temp = ff_g723_1_normalize_bits(max, 31);
713
714
56800
    for (i = 0; i < 20 * iter; i++)
715
56000
        ccr_buf[i] = av_clipl_int32((int64_t) (ccr_buf[i] << temp) +
716
56000
                                    (1 << 15)) >> 16;
717
718
800
    max = 0;
719
3600
    for (i = 0; i < iter; i++) {
720
        /* Select quantization table */
721

2800
        if (!odd_frame && pitch_lag + i - 1 >= SUBFRAME_LEN - 2 ||
722
1600
            odd_frame && pitch_lag >= SUBFRAME_LEN - 2) {
723
232
            cb_tbl   = adaptive_cb_gain170;
724
232
            tbl_size = 170;
725
        }
726
727
260520
        for (j = 0, k = 0; j < tbl_size; j++, k += 20) {
728
257720
            temp = 0;
729
5412120
            for (l = 0; l < 20; l++)
730
5154400
                temp += ccr_buf[20 * i + l] * cb_tbl[k + l];
731
257720
            temp = av_clipl_int32(temp);
732
733
257720
            if (temp > max) {
734
8615
                max      = temp;
735
8615
                acb_gain = j;
736
8615
                acb_lag  = i;
737
            }
738
        }
739
    }
740
741
800
    if (!odd_frame) {
742
400
        pitch_lag += acb_lag - 1;
743
400
        acb_lag    = 1;
744
    }
745
746
800
    p->pitch_lag[index >> 1]      = pitch_lag;
747
800
    p->subframe[index].ad_cb_lag  = acb_lag;
748
800
    p->subframe[index].ad_cb_gain = acb_gain;
749
800
}
750
751
/**
752
 * Subtract the adaptive codebook contribution from the input
753
 * to obtain the residual.
754
 *
755
 * @param buf target vector
756
 */
757
800
static void sub_acb_contrib(const int16_t *residual, const int16_t *impulse_resp,
758
                            int16_t *buf)
759
{
760
    int i, j;
761
    /* Subtract adaptive CB contribution to obtain the residual */
762
48800
    for (i = 0; i < SUBFRAME_LEN; i++) {
763
48000
        int64_t temp = buf[i] << 14;
764
1512000
        for (j = 0; j <= i; j++)
765
1464000
            temp -= residual[j] * impulse_resp[i - j];
766
767
48000
        buf[i] = av_clipl_int32((temp << 2) + (1 << 15)) >> 16;
768
    }
769
800
}
770
771
/**
772
 * Quantize the residual signal using the fixed codebook (MP-MLQ).
773
 *
774
 * @param optim optimized fixed codebook parameters
775
 * @param buf   excitation vector
776
 */
777
1534
static void get_fcb_param(FCBParam *optim, int16_t *impulse_resp,
778
                          int16_t *buf, int pulse_cnt, int pitch_lag)
779
{
780
    FCBParam param;
781
    int16_t impulse_r[SUBFRAME_LEN];
782
    int16_t temp_corr[SUBFRAME_LEN];
783
    int16_t impulse_corr[SUBFRAME_LEN];
784
785
    int ccr1[SUBFRAME_LEN];
786
    int ccr2[SUBFRAME_LEN];
787
    int amp, err, max, max_amp_index, min, scale, i, j, k, l;
788
789
    int64_t temp;
790
791
    /* Update impulse response */
792
1534
    memcpy(impulse_r, impulse_resp, sizeof(int16_t) * SUBFRAME_LEN);
793
1534
    param.dirac_train = 0;
794
1534
    if (pitch_lag < SUBFRAME_LEN - 2) {
795
734
        param.dirac_train = 1;
796
734
        ff_g723_1_gen_dirac_train(impulse_r, pitch_lag);
797
    }
798
799
93574
    for (i = 0; i < SUBFRAME_LEN; i++)
800
92040
        temp_corr[i] = impulse_r[i] >> 1;
801
802
    /* Compute impulse response autocorrelation */
803
1534
    temp = ff_g723_1_dot_product(temp_corr, temp_corr, SUBFRAME_LEN);
804
805
1534
    scale           = ff_g723_1_normalize_bits(temp, 31);
806
1534
    impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
807
808
92040
    for (i = 1; i < SUBFRAME_LEN; i++) {
809
90506
        temp = ff_g723_1_dot_product(temp_corr + i, temp_corr,
810
                                     SUBFRAME_LEN - i);
811
90506
        impulse_corr[i] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
812
    }
813
814
    /* Compute crosscorrelation of impulse response with residual signal */
815
1534
    scale -= 4;
816
93574
    for (i = 0; i < SUBFRAME_LEN; i++) {
817
92040
        temp = ff_g723_1_dot_product(buf + i, impulse_r, SUBFRAME_LEN - i);
818
92040
        if (scale < 0)
819
55500
            ccr1[i] = temp >> -scale;
820
        else
821
36540
            ccr1[i] = av_clipl_int32(temp << scale);
822
    }
823
824
    /* Search loop */
825
4602
    for (i = 0; i < GRID_SIZE; i++) {
826
        /* Maximize the crosscorrelation */
827
3068
        max = 0;
828
95108
        for (j = i; j < SUBFRAME_LEN; j += GRID_SIZE) {
829
92040
            temp = FFABS(ccr1[j]);
830
92040
            if (temp >= max) {
831
12301
                max                = temp;
832
12301
                param.pulse_pos[0] = j;
833
            }
834
        }
835
836
        /* Quantize the gain (max crosscorrelation/impulse_corr[0]) */
837
3068
        amp           = max;
838
3068
        min           = 1 << 30;
839
3068
        max_amp_index = GAIN_LEVELS - 2;
840
67496
        for (j = max_amp_index; j >= 2; j--) {
841
64428
            temp = av_clipl_int32((int64_t) fixed_cb_gain[j] *
842
64428
                                  impulse_corr[0] << 1);
843
64428
            temp = FFABS(temp - amp);
844
64428
            if (temp < min) {
845
33898
                min           = temp;
846
33898
                max_amp_index = j;
847
            }
848
        }
849
850
3068
        max_amp_index--;
851
        /* Select additional gain values */
852
15340
        for (j = 1; j < 5; j++) {
853
380432
            for (k = i; k < SUBFRAME_LEN; k += GRID_SIZE) {
854
368160
                temp_corr[k] = 0;
855
368160
                ccr2[k]      = ccr1[k];
856
            }
857
12272
            param.amp_index = max_amp_index + j - 2;
858
12272
            amp             = fixed_cb_gain[param.amp_index];
859
860
12272
            param.pulse_sign[0] = (ccr2[param.pulse_pos[0]] < 0) ? -amp : amp;
861
12272
            temp_corr[param.pulse_pos[0]] = 1;
862
863
67496
            for (k = 1; k < pulse_cnt; k++) {
864
55224
                max = INT_MIN;
865
1711944
                for (l = i; l < SUBFRAME_LEN; l += GRID_SIZE) {
866
1656720
                    if (temp_corr[l])
867
153400
                        continue;
868
1503320
                    temp = impulse_corr[FFABS(l - param.pulse_pos[k - 1])];
869
1503320
                    temp = av_clipl_int32((int64_t) temp *
870
1503320
                                          param.pulse_sign[k - 1] << 1);
871
1503320
                    ccr2[l] -= temp;
872
1503320
                    temp     = FFABS(ccr2[l]);
873
1503320
                    if (temp > max) {
874
224253
                        max                = temp;
875
224253
                        param.pulse_pos[k] = l;
876
                    }
877
                }
878
879
110448
                param.pulse_sign[k] = (ccr2[param.pulse_pos[k]] < 0) ?
880
55224
                                      -amp : amp;
881
55224
                temp_corr[param.pulse_pos[k]] = 1;
882
            }
883
884
            /* Create the error vector */
885
12272
            memset(temp_corr, 0, sizeof(int16_t) * SUBFRAME_LEN);
886
887
79768
            for (k = 0; k < pulse_cnt; k++)
888
67496
                temp_corr[param.pulse_pos[k]] = param.pulse_sign[k];
889
890
748592
            for (k = SUBFRAME_LEN - 1; k >= 0; k--) {
891
736320
                temp = 0;
892
23194080
                for (l = 0; l <= k; l++) {
893
22457760
                    int prod = av_clipl_int32((int64_t) temp_corr[l] *
894
22457760
                                              impulse_r[k - l] << 1);
895
22457760
                    temp = av_clipl_int32(temp + prod);
896
                }
897
736320
                temp_corr[k] = temp << 2 >> 16;
898
            }
899
900
            /* Compute square of error */
901
12272
            err = 0;
902
748592
            for (k = 0; k < SUBFRAME_LEN; k++) {
903
                int64_t prod;
904
736320
                prod = av_clipl_int32((int64_t) buf[k] * temp_corr[k] << 1);
905
736320
                err  = av_clipl_int32(err - prod);
906
736320
                prod = av_clipl_int32((int64_t) temp_corr[k] * temp_corr[k]);
907
736320
                err  = av_clipl_int32(err + prod);
908
            }
909
910
            /* Minimize */
911
12272
            if (err < optim->min_err) {
912
2638
                optim->min_err     = err;
913
2638
                optim->grid_index  = i;
914
2638
                optim->amp_index   = param.amp_index;
915
2638
                optim->dirac_train = param.dirac_train;
916
917
17164
                for (k = 0; k < pulse_cnt; k++) {
918
14526
                    optim->pulse_sign[k] = param.pulse_sign[k];
919
14526
                    optim->pulse_pos[k]  = param.pulse_pos[k];
920
                }
921
            }
922
        }
923
    }
924
1534
}
925
926
/**
927
 * Encode the pulse position and gain of the current subframe.
928
 *
929
 * @param optim optimized fixed CB parameters
930
 * @param buf   excitation vector
931
 */
932
800
static void pack_fcb_param(G723_1_Subframe *subfrm, FCBParam *optim,
933
                           int16_t *buf, int pulse_cnt)
934
{
935
    int i, j;
936
937
800
    j = PULSE_MAX - pulse_cnt;
938
939
800
    subfrm->pulse_sign = 0;
940
800
    subfrm->pulse_pos  = 0;
941
942
17850
    for (i = 0; i < SUBFRAME_LEN >> 1; i++) {
943
17850
        int val = buf[optim->grid_index + (i << 1)];
944
17850
        if (!val) {
945
13450
            subfrm->pulse_pos += combinatorial_table[j][i];
946
        } else {
947
4400
            subfrm->pulse_sign <<= 1;
948
4400
            if (val < 0)
949
2239
                subfrm->pulse_sign++;
950
4400
            j++;
951
952
4400
            if (j == PULSE_MAX)
953
800
                break;
954
        }
955
    }
956
800
    subfrm->amp_index   = optim->amp_index;
957
800
    subfrm->grid_index  = optim->grid_index;
958
800
    subfrm->dirac_train = optim->dirac_train;
959
800
}
960
961
/**
962
 * Compute the fixed codebook excitation.
963
 *
964
 * @param buf          target vector
965
 * @param impulse_resp impulse response of the combined filter
966
 */
967
800
static void fcb_search(G723_1_ChannelContext *p, int16_t *impulse_resp,
968
                       int16_t *buf, int index)
969
{
970
    FCBParam optim;
971
800
    int pulse_cnt = pulses[index];
972
    int i;
973
974
800
    optim.min_err = 1 << 30;
975
800
    get_fcb_param(&optim, impulse_resp, buf, pulse_cnt, SUBFRAME_LEN);
976
977
800
    if (p->pitch_lag[index >> 1] < SUBFRAME_LEN - 2) {
978
734
        get_fcb_param(&optim, impulse_resp, buf, pulse_cnt,
979
734
                      p->pitch_lag[index >> 1]);
980
    }
981
982
    /* Reconstruct the excitation */
983
800
    memset(buf, 0, sizeof(int16_t) * SUBFRAME_LEN);
984
5200
    for (i = 0; i < pulse_cnt; i++)
985
4400
        buf[optim.pulse_pos[i]] = optim.pulse_sign[i];
986
987
800
    pack_fcb_param(&p->subframe[index], &optim, buf, pulse_cnt);
988
989
800
    if (optim.dirac_train)
990
378
        ff_g723_1_gen_dirac_train(buf, p->pitch_lag[index >> 1]);
991
800
}
992
993
/**
994
 * Pack the frame parameters into output bitstream.
995
 *
996
 * @param frame output buffer
997
 * @param size  size of the buffer
998
 */
999
200
static int pack_bitstream(G723_1_ChannelContext *p, AVPacket *avpkt)
1000
{
1001
    PutBitContext pb;
1002
200
    int info_bits = 0;
1003
    int i, temp;
1004
1005
200
    init_put_bits(&pb, avpkt->data, avpkt->size);
1006
1007
200
    put_bits(&pb, 2, info_bits);
1008
1009
200
    put_bits(&pb, 8, p->lsp_index[2]);
1010
200
    put_bits(&pb, 8, p->lsp_index[1]);
1011
200
    put_bits(&pb, 8, p->lsp_index[0]);
1012
1013
200
    put_bits(&pb, 7, p->pitch_lag[0] - PITCH_MIN);
1014
200
    put_bits(&pb, 2, p->subframe[1].ad_cb_lag);
1015
200
    put_bits(&pb, 7, p->pitch_lag[1] - PITCH_MIN);
1016
200
    put_bits(&pb, 2, p->subframe[3].ad_cb_lag);
1017
1018
    /* Write 12 bit combined gain */
1019
1000
    for (i = 0; i < SUBFRAMES; i++) {
1020
800
        temp = p->subframe[i].ad_cb_gain * GAIN_LEVELS +
1021
800
               p->subframe[i].amp_index;
1022
800
        if (p->cur_rate == RATE_6300)
1023
800
            temp += p->subframe[i].dirac_train << 11;
1024
800
        put_bits(&pb, 12, temp);
1025
    }
1026
1027
200
    put_bits(&pb, 1, p->subframe[0].grid_index);
1028
200
    put_bits(&pb, 1, p->subframe[1].grid_index);
1029
200
    put_bits(&pb, 1, p->subframe[2].grid_index);
1030
200
    put_bits(&pb, 1, p->subframe[3].grid_index);
1031
1032
200
    if (p->cur_rate == RATE_6300) {
1033
200
        skip_put_bits(&pb, 1); /* reserved bit */
1034
1035
        /* Write 13 bit combined position index */
1036
200
        temp = (p->subframe[0].pulse_pos >> 16) * 810 +
1037
200
               (p->subframe[1].pulse_pos >> 14) *  90 +
1038
200
               (p->subframe[2].pulse_pos >> 16) *   9 +
1039
200
               (p->subframe[3].pulse_pos >> 14);
1040
200
        put_bits(&pb, 13, temp);
1041
1042
200
        put_bits(&pb, 16, p->subframe[0].pulse_pos & 0xffff);
1043
200
        put_bits(&pb, 14, p->subframe[1].pulse_pos & 0x3fff);
1044
200
        put_bits(&pb, 16, p->subframe[2].pulse_pos & 0xffff);
1045
200
        put_bits(&pb, 14, p->subframe[3].pulse_pos & 0x3fff);
1046
1047
200
        put_bits(&pb, 6, p->subframe[0].pulse_sign);
1048
200
        put_bits(&pb, 5, p->subframe[1].pulse_sign);
1049
200
        put_bits(&pb, 6, p->subframe[2].pulse_sign);
1050
200
        put_bits(&pb, 5, p->subframe[3].pulse_sign);
1051
    }
1052
1053
200
    flush_put_bits(&pb);
1054
200
    return frame_size[info_bits];
1055
}
1056
1057
200
static int g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1058
                               const AVFrame *frame, int *got_packet_ptr)
1059
{
1060
200
    G723_1_Context *s = avctx->priv_data;
1061
200
    G723_1_ChannelContext *p = &s->ch[0];
1062
    int16_t unq_lpc[LPC_ORDER * SUBFRAMES];
1063
    int16_t qnt_lpc[LPC_ORDER * SUBFRAMES];
1064
    int16_t cur_lsp[LPC_ORDER];
1065
    int16_t weighted_lpc[LPC_ORDER * SUBFRAMES << 1];
1066
    int16_t vector[FRAME_LEN + PITCH_MAX];
1067
    int offset, ret, i, j;
1068
    int16_t *in, *start;
1069
    HFParam hf[4];
1070
1071
    /* duplicate input */
1072
200
    start = in = av_malloc(frame->nb_samples * sizeof(int16_t));
1073
200
    if (!in)
1074
        return AVERROR(ENOMEM);
1075
200
    memcpy(in, frame->data[0], frame->nb_samples * sizeof(int16_t));
1076
1077
200
    highpass_filter(in, &p->hpf_fir_mem, &p->hpf_iir_mem);
1078
1079
200
    memcpy(vector, p->prev_data, HALF_FRAME_LEN * sizeof(int16_t));
1080
200
    memcpy(vector + HALF_FRAME_LEN, in, FRAME_LEN * sizeof(int16_t));
1081
1082
200
    comp_lpc_coeff(vector, unq_lpc);
1083
200
    lpc2lsp(&unq_lpc[LPC_ORDER * 3], p->prev_lsp, cur_lsp);
1084
200
    lsp_quantize(p->lsp_index, cur_lsp, p->prev_lsp);
1085
1086
    /* Update memory */
1087
200
    memcpy(vector + LPC_ORDER, p->prev_data + SUBFRAME_LEN,
1088
           sizeof(int16_t) * SUBFRAME_LEN);
1089
200
    memcpy(vector + LPC_ORDER + SUBFRAME_LEN, in,
1090
           sizeof(int16_t) * (HALF_FRAME_LEN + SUBFRAME_LEN));
1091
200
    memcpy(p->prev_data, in + HALF_FRAME_LEN,
1092
           sizeof(int16_t) * HALF_FRAME_LEN);
1093
200
    memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
1094
1095
200
    perceptual_filter(p, weighted_lpc, unq_lpc, vector);
1096
1097
200
    memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
1098
200
    memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
1099
200
    memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
1100
1101
200
    ff_g723_1_scale_vector(vector, vector, FRAME_LEN + PITCH_MAX);
1102
1103
200
    p->pitch_lag[0] = estimate_pitch(vector, PITCH_MAX);
1104
200
    p->pitch_lag[1] = estimate_pitch(vector, PITCH_MAX + HALF_FRAME_LEN);
1105
1106
1000
    for (i = PITCH_MAX, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1107
800
        comp_harmonic_coeff(vector + i, p->pitch_lag[j >> 1], hf + j);
1108
1109
200
    memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
1110
200
    memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
1111
200
    memcpy(p->prev_weight_sig, vector + FRAME_LEN, sizeof(int16_t) * PITCH_MAX);
1112
1113
1000
    for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1114
800
        harmonic_filter(hf + j, vector + PITCH_MAX + i, in + i);
1115
1116
200
    ff_g723_1_inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, 0);
1117
200
    ff_g723_1_lsp_interpolate(qnt_lpc, cur_lsp, p->prev_lsp);
1118
1119
200
    memcpy(p->prev_lsp, cur_lsp, sizeof(int16_t) * LPC_ORDER);
1120
1121
200
    offset = 0;
1122
1000
    for (i = 0; i < SUBFRAMES; i++) {
1123
        int16_t impulse_resp[SUBFRAME_LEN];
1124
        int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
1125
        int16_t flt_in[SUBFRAME_LEN];
1126
        int16_t zero[LPC_ORDER], fir[LPC_ORDER], iir[LPC_ORDER];
1127
1128
        /**
1129
         * Compute the combined impulse response of the synthesis filter,
1130
         * formant perceptual weighting filter and harmonic noise shaping filter
1131
         */
1132
800
        memset(zero, 0, sizeof(int16_t) * LPC_ORDER);
1133
800
        memset(vector, 0, sizeof(int16_t) * PITCH_MAX);
1134
800
        memset(flt_in, 0, sizeof(int16_t) * SUBFRAME_LEN);
1135
1136
800
        flt_in[0] = 1 << 13; /* Unit impulse */
1137
800
        synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
1138
                             zero, zero, flt_in, vector + PITCH_MAX, 1);
1139
800
        harmonic_filter(hf + i, vector + PITCH_MAX, impulse_resp);
1140
1141
        /* Compute the combined zero input response */
1142
800
        flt_in[0] = 0;
1143
800
        memcpy(fir, p->perf_fir_mem, sizeof(int16_t) * LPC_ORDER);
1144
800
        memcpy(iir, p->perf_iir_mem, sizeof(int16_t) * LPC_ORDER);
1145
1146
800
        synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
1147
                             fir, iir, flt_in, vector + PITCH_MAX, 0);
1148
800
        memcpy(vector, p->harmonic_mem, sizeof(int16_t) * PITCH_MAX);
1149
800
        harmonic_noise_sub(hf + i, vector + PITCH_MAX, in);
1150
1151
800
        acb_search(p, residual, impulse_resp, in, i);
1152
800
        ff_g723_1_gen_acb_excitation(residual, p->prev_excitation,
1153
800
                                     p->pitch_lag[i >> 1], &p->subframe[i],
1154
                                     p->cur_rate);
1155
800
        sub_acb_contrib(residual, impulse_resp, in);
1156
1157
800
        fcb_search(p, impulse_resp, in, i);
1158
1159
        /* Reconstruct the excitation */
1160
800
        ff_g723_1_gen_acb_excitation(impulse_resp, p->prev_excitation,
1161
800
                                     p->pitch_lag[i >> 1], &p->subframe[i],
1162
                                     RATE_6300);
1163
1164
800
        memmove(p->prev_excitation, p->prev_excitation + SUBFRAME_LEN,
1165
                sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
1166
48800
        for (j = 0; j < SUBFRAME_LEN; j++)
1167
48000
            in[j] = av_clip_int16((in[j] << 1) + impulse_resp[j]);
1168
800
        memcpy(p->prev_excitation + PITCH_MAX - SUBFRAME_LEN, in,
1169
               sizeof(int16_t) * SUBFRAME_LEN);
1170
1171
        /* Update filter memories */
1172
800
        synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
1173
800
                             p->perf_fir_mem, p->perf_iir_mem,
1174
                             in, vector + PITCH_MAX, 0);
1175
800
        memmove(p->harmonic_mem, p->harmonic_mem + SUBFRAME_LEN,
1176
                sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
1177
800
        memcpy(p->harmonic_mem + PITCH_MAX - SUBFRAME_LEN, vector + PITCH_MAX,
1178
               sizeof(int16_t) * SUBFRAME_LEN);
1179
1180
800
        in     += SUBFRAME_LEN;
1181
800
        offset += LPC_ORDER;
1182
    }
1183
1184
200
    av_free(start);
1185
1186
200
    if ((ret = ff_alloc_packet2(avctx, avpkt, 24, 0)) < 0)
1187
        return ret;
1188
1189
200
    *got_packet_ptr = 1;
1190
200
    avpkt->size = pack_bitstream(p, avpkt);
1191
200
    return 0;
1192
}
1193
1194
static const AVCodecDefault defaults[] = {
1195
    { "b", "6300" },
1196
    { NULL },
1197
};
1198
1199
AVCodec ff_g723_1_encoder = {
1200
    .name           = "g723_1",
1201
    .long_name      = NULL_IF_CONFIG_SMALL("G.723.1"),
1202
    .type           = AVMEDIA_TYPE_AUDIO,
1203
    .id             = AV_CODEC_ID_G723_1,
1204
    .priv_data_size = sizeof(G723_1_Context),
1205
    .init           = g723_1_encode_init,
1206
    .encode2        = g723_1_encode_frame,
1207
    .defaults       = defaults,
1208
    .sample_fmts    = (const enum AVSampleFormat[]) {
1209
        AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
1210
    },
1211
};