Browse code

g723.1 encoder

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Mohamed Naufal authored on 2011/10/10 09:48:34
Showing 4 changed files
... ...
@@ -161,6 +161,7 @@ OBJS-$(CONFIG_FRAPS_DECODER)           += fraps.o
161 161
 OBJS-$(CONFIG_FRWU_DECODER)            += frwu.o
162 162
 OBJS-$(CONFIG_G723_1_DECODER)          += g723_1.o acelp_vectors.o \
163 163
                                           celp_filters.o celp_math.o
164
+OBJS-$(CONFIG_G723_1_ENCODER)          += g723_1.o
164 165
 OBJS-$(CONFIG_G729_DECODER)            += g729dec.o lsp.o celp_math.o acelp_filters.o acelp_pitch_delay.o acelp_vectors.o g729postfilter.o
165 166
 OBJS-$(CONFIG_GIF_DECODER)             += gifdec.o lzw.o
166 167
 OBJS-$(CONFIG_GIF_ENCODER)             += gif.o lzwenc.o
... ...
@@ -258,7 +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
+    REGISTER_ENCDEC  (G723_1, g723_1);
262 262
     REGISTER_DECODER (G729, g729);
263 263
     REGISTER_DECODER (GSM, gsm);
264 264
     REGISTER_DECODER (GSM_MS, gsm_ms);
... ...
@@ -59,6 +59,17 @@ typedef struct g723_1_context {
59 59
     int reflection_coef;
60 60
     int pf_gain;                 ///< formant postfilter
61 61
                                  ///< gain scaling unit memory
62
+
63
+    int16_t prev_data[HALF_FRAME_LEN];
64
+    int16_t prev_weight_sig[PITCH_MAX];
65
+
66
+
67
+    int16_t hpf_fir_mem;                   ///< highpass filter fir
68
+    int     hpf_iir_mem;                   ///< and iir memories
69
+    int16_t perf_fir_mem[LPC_ORDER];       ///< perceptual filter fir
70
+    int16_t perf_iir_mem[LPC_ORDER];       ///< and iir memories
71
+
72
+    int16_t harmonic_mem[PITCH_MAX];
62 73
 } G723_1_Context;
63 74
 
64 75
 static av_cold int g723_1_decode_init(AVCodecContext *avctx)
... ...
@@ -221,6 +232,10 @@ static int normalize_bits(int num, int width)
221 221
     return i;
222 222
 }
223 223
 
224
+#define normalize_bits_int16(num) normalize_bits(num, 0)
225
+#define normalize_bits_int32(num) normalize_bits(num, 1)
226
+#define dot_product(a,b,c,d) (ff_dot_product(a,b,c)<<(d))
227
+
224 228
 /**
225 229
  * Scale vector contents based on the largest of their absolutes.
226 230
  */
... ...
@@ -1068,3 +1083,1134 @@ AVCodec ff_g723_1_decoder = {
1068 1068
     .long_name      = NULL_IF_CONFIG_SMALL("G.723.1"),
1069 1069
     .capabilities   = CODEC_CAP_SUBFRAMES,
1070 1070
 };
1071
+
1072
+#if CONFIG_G723_1_ENCODER
1073
+#define BITSTREAM_WRITER_LE
1074
+#include "put_bits.h"
1075
+
1076
+static av_cold int g723_1_encode_init(AVCodecContext *avctx)
1077
+{
1078
+    G723_1_Context *p = avctx->priv_data;
1079
+
1080
+    if (avctx->sample_rate != 8000) {
1081
+        av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
1082
+        return -1;
1083
+    }
1084
+
1085
+    if (avctx->channels != 1) {
1086
+        av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
1087
+        return AVERROR(EINVAL);
1088
+    }
1089
+
1090
+    if (avctx->bit_rate == 6300) {
1091
+        p->cur_rate = Rate6k3;
1092
+    } else if (avctx->bit_rate == 5300) {
1093
+        av_log(avctx, AV_LOG_ERROR, "Bitrate not supported yet, use 6.3k\n");
1094
+        return AVERROR_PATCHWELCOME;
1095
+    } else {
1096
+        av_log(avctx, AV_LOG_ERROR,
1097
+               "Bitrate not supported, use 6.3k\n");
1098
+        return AVERROR(EINVAL);
1099
+    }
1100
+    avctx->frame_size = 240;
1101
+    memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(int16_t));
1102
+
1103
+    return 0;
1104
+}
1105
+
1106
+/**
1107
+ * Remove DC component from the input signal.
1108
+ *
1109
+ * @param buf input signal
1110
+ * @param fir zero memory
1111
+ * @param iir pole memory
1112
+ */
1113
+static void highpass_filter(int16_t *buf, int16_t *fir, int *iir)
1114
+{
1115
+    int i;
1116
+    for (i = 0; i < FRAME_LEN; i++) {
1117
+        *iir   = (buf[i] << 15) + ((-*fir) << 15) + MULL2(*iir, 0x7f00);
1118
+        *fir   = buf[i];
1119
+        buf[i] = av_clipl_int32((int64_t)*iir + (1 << 15)) >> 16;
1120
+    }
1121
+}
1122
+
1123
+/**
1124
+ * Estimate autocorrelation of the input vector.
1125
+ *
1126
+ * @param buf      input buffer
1127
+ * @param autocorr autocorrelation coefficients vector
1128
+ */
1129
+static void comp_autocorr(int16_t *buf, int16_t *autocorr)
1130
+{
1131
+    int i, scale, temp;
1132
+    int16_t vector[LPC_FRAME];
1133
+
1134
+    memcpy(vector, buf, LPC_FRAME * sizeof(int16_t));
1135
+    scale_vector(vector, LPC_FRAME);
1136
+
1137
+    /* Apply the Hamming window */
1138
+    for (i = 0; i < LPC_FRAME; i++)
1139
+        vector[i] = (vector[i] * hamming_window[i] + (1 << 14)) >> 15;
1140
+
1141
+    /* Compute the first autocorrelation coefficient */
1142
+    temp = dot_product(vector, vector, LPC_FRAME, 0);
1143
+
1144
+    /* Apply a white noise correlation factor of (1025/1024) */
1145
+    temp += temp >> 10;
1146
+
1147
+    /* Normalize */
1148
+    scale = normalize_bits_int32(temp);
1149
+    autocorr[0] = av_clipl_int32((int64_t)(temp << scale) +
1150
+                                 (1 << 15)) >> 16;
1151
+
1152
+    /* Compute the remaining coefficients */
1153
+    if (!autocorr[0]) {
1154
+        memset(autocorr + 1, 0, LPC_ORDER * sizeof(int16_t));
1155
+    } else {
1156
+        for (i = 1; i <= LPC_ORDER; i++) {
1157
+           temp = dot_product(vector, vector + i, LPC_FRAME - i, 0);
1158
+           temp = MULL2((temp << scale), binomial_window[i - 1]);
1159
+           autocorr[i] = av_clipl_int32((int64_t)temp + (1 << 15)) >> 16;
1160
+        }
1161
+    }
1162
+}
1163
+
1164
+/**
1165
+ * Use Levinson-Durbin recursion to compute LPC coefficients from
1166
+ * autocorrelation values.
1167
+ *
1168
+ * @param lpc      LPC coefficients vector
1169
+ * @param autocorr autocorrelation coefficients vector
1170
+ * @param error    prediction error
1171
+ */
1172
+static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)
1173
+{
1174
+    int16_t vector[LPC_ORDER];
1175
+    int16_t partial_corr;
1176
+    int i, j, temp;
1177
+
1178
+    memset(lpc, 0, LPC_ORDER * sizeof(int16_t));
1179
+
1180
+    for (i = 0; i < LPC_ORDER; i++) {
1181
+        /* Compute the partial correlation coefficient */
1182
+        temp = 0;
1183
+        for (j = 0; j < i; j++)
1184
+            temp -= lpc[j] * autocorr[i - j - 1];
1185
+        temp = ((autocorr[i] << 13) + temp) << 3;
1186
+
1187
+        if (FFABS(temp) >= (error << 16))
1188
+            break;
1189
+
1190
+        partial_corr = temp / (error << 1);
1191
+
1192
+        lpc[i] = av_clipl_int32((int64_t)(partial_corr << 14) +
1193
+                                (1 << 15)) >> 16;
1194
+
1195
+        /* Update the prediction error */
1196
+        temp  = MULL2(temp, partial_corr);
1197
+        error = av_clipl_int32((int64_t)(error << 16) - temp +
1198
+                                (1 << 15)) >> 16;
1199
+
1200
+        memcpy(vector, lpc, i * sizeof(int16_t));
1201
+        for (j = 0; j < i; j++) {
1202
+            temp = partial_corr * vector[i - j - 1] << 1;
1203
+            lpc[j] = av_clipl_int32((int64_t)(lpc[j] << 16) - temp +
1204
+                                    (1 << 15)) >> 16;
1205
+        }
1206
+    }
1207
+}
1208
+
1209
+/**
1210
+ * Calculate LPC coefficients for the current frame.
1211
+ *
1212
+ * @param buf       current frame
1213
+ * @param prev_data 2 trailing subframes of the previous frame
1214
+ * @param lpc       LPC coefficients vector
1215
+ */
1216
+static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
1217
+{
1218
+    int16_t autocorr[(LPC_ORDER + 1) * SUBFRAMES];
1219
+    int16_t *autocorr_ptr = autocorr;
1220
+    int16_t *lpc_ptr      = lpc;
1221
+    int i, j;
1222
+
1223
+    for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
1224
+        comp_autocorr(buf + i, autocorr_ptr);
1225
+        levinson_durbin(lpc_ptr, autocorr_ptr + 1, autocorr_ptr[0]);
1226
+
1227
+        lpc_ptr += LPC_ORDER;
1228
+        autocorr_ptr += LPC_ORDER + 1;
1229
+    }
1230
+}
1231
+
1232
+static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
1233
+{
1234
+    int f[LPC_ORDER + 2]; ///< coefficients of the sum and difference
1235
+                          ///< polynomials (F1, F2) ordered as
1236
+                          ///< f1[0], f2[0], ...., f1[5], f2[5]
1237
+
1238
+    int max, shift, cur_val, prev_val, count, p;
1239
+    int i, j;
1240
+    int64_t temp;
1241
+
1242
+    /* Initialize f1[0] and f2[0] to 1 in Q25 */
1243
+    for (i = 0; i < LPC_ORDER; i++)
1244
+        lsp[i] = (lpc[i] * bandwidth_expand[i] + (1 << 14)) >> 15;
1245
+
1246
+    /* Apply bandwidth expansion on the LPC coefficients */
1247
+    f[0] = f[1] = 1 << 25;
1248
+
1249
+    /* Compute the remaining coefficients */
1250
+    for (i = 0; i < LPC_ORDER / 2; i++) {
1251
+        /* f1 */
1252
+        f[2 * i + 2] = -f[2 * i] - ((lsp[i] + lsp[LPC_ORDER - 1 - i]) << 12);
1253
+        /* f2 */
1254
+        f[2 * i + 3] = f[2 * i + 1] - ((lsp[i] - lsp[LPC_ORDER - 1 - i]) << 12);
1255
+    }
1256
+
1257
+    /* Divide f1[5] and f2[5] by 2 for use in polynomial evaluation */
1258
+    f[LPC_ORDER] >>= 1;
1259
+    f[LPC_ORDER + 1] >>= 1;
1260
+
1261
+    /* Normalize and shorten */
1262
+    max = FFABS(f[0]);
1263
+    for (i = 1; i < LPC_ORDER + 2; i++)
1264
+        max = FFMAX(max, FFABS(f[i]));
1265
+
1266
+    shift = normalize_bits_int32(max);
1267
+
1268
+    for (i = 0; i < LPC_ORDER + 2; i++)
1269
+        f[i] = av_clipl_int32((int64_t)(f[i] << shift) + (1 << 15)) >> 16;
1270
+
1271
+    /**
1272
+     * Evaluate F1 and F2 at uniform intervals of pi/256 along the
1273
+     * unit circle and check for zero crossings.
1274
+     */
1275
+    p    = 0;
1276
+    temp = 0;
1277
+    for (i = 0; i <= LPC_ORDER / 2; i++)
1278
+        temp += f[2 * i] * cos_tab[0];
1279
+    prev_val = av_clipl_int32(temp << 1);
1280
+    count    = 0;
1281
+    for ( i = 1; i < COS_TBL_SIZE / 2; i++) {
1282
+        /* Evaluate */
1283
+        temp = 0;
1284
+        for (j = 0; j <= LPC_ORDER / 2; j++)
1285
+            temp += f[LPC_ORDER - 2 * j + p] * cos_tab[i * j % COS_TBL_SIZE];
1286
+        cur_val = av_clipl_int32(temp << 1);
1287
+
1288
+        /* Check for sign change, indicating a zero crossing */
1289
+        if ((cur_val ^ prev_val) < 0) {
1290
+            int abs_cur  = FFABS(cur_val);
1291
+            int abs_prev = FFABS(prev_val);
1292
+            int sum      = abs_cur + abs_prev;
1293
+
1294
+            shift        = normalize_bits_int32(sum);
1295
+            sum          <<= shift;
1296
+            abs_prev     = abs_prev << shift >> 8;
1297
+            lsp[count++] = ((i - 1) << 7) + (abs_prev >> 1) / (sum >> 16);
1298
+
1299
+            if (count == LPC_ORDER)
1300
+                break;
1301
+
1302
+            /* Switch between sum and difference polynomials */
1303
+            p ^= 1;
1304
+
1305
+            /* Evaluate */
1306
+            temp = 0;
1307
+            for (j = 0; j <= LPC_ORDER / 2; j++){
1308
+                temp += f[LPC_ORDER - 2 * j + p] *
1309
+                        cos_tab[i * j % COS_TBL_SIZE];
1310
+            }
1311
+            cur_val = av_clipl_int32(temp<<1);
1312
+        }
1313
+        prev_val = cur_val;
1314
+    }
1315
+
1316
+    if (count != LPC_ORDER)
1317
+        memcpy(lsp, prev_lsp, LPC_ORDER * sizeof(int16_t));
1318
+}
1319
+
1320
+/**
1321
+ * Quantize the current LSP subvector.
1322
+ *
1323
+ * @param num    band number
1324
+ * @param offset offset of the current subvector in an LPC_ORDER vector
1325
+ * @param size   size of the current subvector
1326
+ */
1327
+#define get_index(num, offset, size) \
1328
+{\
1329
+    int error, max = -1;\
1330
+    int16_t temp[4];\
1331
+    int i, j;\
1332
+    for (i = 0; i < LSP_CB_SIZE; i++) {\
1333
+        for (j = 0; j < size; j++){\
1334
+            temp[j] = (weight[j + (offset)] * lsp_band##num[i][j] +\
1335
+                      (1 << 14)) >> 15;\
1336
+        }\
1337
+        error =  dot_product(lsp + (offset), temp, size, 1) << 1;\
1338
+        error -= dot_product(lsp_band##num[i], temp, size, 1);\
1339
+        if (error > max) {\
1340
+            max = error;\
1341
+            lsp_index[num] = i;\
1342
+        }\
1343
+    }\
1344
+}
1345
+
1346
+/**
1347
+ * Vector quantize the LSP frequencies.
1348
+ *
1349
+ * @param lsp      the current lsp vector
1350
+ * @param prev_lsp the previous lsp vector
1351
+ */
1352
+static void lsp_quantize(uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
1353
+{
1354
+    int16_t weight[LPC_ORDER];
1355
+    int16_t min, max;
1356
+    int shift, i;
1357
+
1358
+    /* Calculate the VQ weighting vector */
1359
+    weight[0] = (1 << 20) / (lsp[1] - lsp[0]);
1360
+    weight[LPC_ORDER - 1] = (1 << 20) /
1361
+                            (lsp[LPC_ORDER - 1] - lsp[LPC_ORDER - 2]);
1362
+
1363
+    for (i = 1; i < LPC_ORDER - 1; i++) {
1364
+        min  = FFMIN(lsp[i] - lsp[i - 1], lsp[i + 1] - lsp[i]);
1365
+        if (min > 0x20)
1366
+            weight[i] = (1 << 20) / min;
1367
+        else
1368
+            weight[i] = INT16_MAX;
1369
+    }
1370
+
1371
+    /* Normalize */
1372
+    max = 0;
1373
+    for (i = 0; i < LPC_ORDER; i++)
1374
+        max = FFMAX(weight[i], max);
1375
+
1376
+    shift = normalize_bits_int16(max);
1377
+    for (i = 0; i < LPC_ORDER; i++) {
1378
+        weight[i] <<= shift;
1379
+    }
1380
+
1381
+    /* Compute the VQ target vector */
1382
+    for (i = 0; i < LPC_ORDER; i++) {
1383
+        lsp[i] -= dc_lsp[i] +
1384
+                  (((prev_lsp[i] - dc_lsp[i]) * 12288 + (1 << 14)) >> 15);
1385
+    }
1386
+
1387
+    get_index(0, 0, 3);
1388
+    get_index(1, 3, 3);
1389
+    get_index(2, 6, 4);
1390
+}
1391
+
1392
+/**
1393
+ * Apply the formant perceptual weighting filter.
1394
+ *
1395
+ * @param flt_coef filter coefficients
1396
+ * @param unq_lpc  unquantized lpc vector
1397
+ */
1398
+static void perceptual_filter(G723_1_Context *p, int16_t *flt_coef,
1399
+                              int16_t *unq_lpc, int16_t *buf)
1400
+{
1401
+    int16_t vector[FRAME_LEN + LPC_ORDER];
1402
+    int i, j, k, l = 0;
1403
+
1404
+    memcpy(buf, p->iir_mem, sizeof(int16_t) * LPC_ORDER);
1405
+    memcpy(vector, p->fir_mem, sizeof(int16_t) * LPC_ORDER);
1406
+    memcpy(vector + LPC_ORDER, buf + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
1407
+
1408
+    for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
1409
+        for (k = 0; k < LPC_ORDER; k++) {
1410
+            flt_coef[k + 2 * l] = (unq_lpc[k + l] * percept_flt_tbl[0][k] +
1411
+                                  (1 << 14)) >> 15;
1412
+            flt_coef[k + 2 * l + LPC_ORDER] = (unq_lpc[k + l] *
1413
+                                             percept_flt_tbl[1][k] +
1414
+                                             (1 << 14)) >> 15;
1415
+        }
1416
+        iir_filter(flt_coef + 2 * l, flt_coef + 2 * l + LPC_ORDER, vector + i,
1417
+                   buf + i, 0);
1418
+        l += LPC_ORDER;
1419
+    }
1420
+    memcpy(p->iir_mem, buf + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
1421
+    memcpy(p->fir_mem, vector + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
1422
+}
1423
+
1424
+/**
1425
+ * Estimate the open loop pitch period.
1426
+ *
1427
+ * @param buf   perceptually weighted speech
1428
+ * @param start estimation is carried out from this position
1429
+ */
1430
+static int estimate_pitch(int16_t *buf, int start)
1431
+{
1432
+    int max_exp = 32;
1433
+    int max_ccr = 0x4000;
1434
+    int max_eng = 0x7fff;
1435
+    int index   = PITCH_MIN;
1436
+    int offset  = start - PITCH_MIN + 1;
1437
+
1438
+    int ccr, eng, orig_eng, ccr_eng, exp;
1439
+    int diff, temp;
1440
+
1441
+    int i;
1442
+
1443
+    orig_eng = dot_product(buf + offset, buf + offset, HALF_FRAME_LEN, 0);
1444
+
1445
+    for (i = PITCH_MIN; i <= PITCH_MAX - 3; i++) {
1446
+        offset--;
1447
+
1448
+        /* Update energy and compute correlation */
1449
+        orig_eng += buf[offset] * buf[offset] -
1450
+                    buf[offset + HALF_FRAME_LEN] * buf[offset + HALF_FRAME_LEN];
1451
+        ccr      =  dot_product(buf + start, buf + offset, HALF_FRAME_LEN, 0);
1452
+        if (ccr <= 0)
1453
+            continue;
1454
+
1455
+        /* Split into mantissa and exponent to maintain precision */
1456
+        exp  =   normalize_bits_int32(ccr);
1457
+        ccr  =   av_clipl_int32((int64_t)(ccr << exp) + (1 << 15)) >> 16;
1458
+        exp  <<= 1;
1459
+        ccr  *=  ccr;
1460
+        temp =   normalize_bits_int32(ccr);
1461
+        ccr  =   ccr << temp >> 16;
1462
+        exp  +=  temp;
1463
+
1464
+        temp =   normalize_bits_int32(orig_eng);
1465
+        eng  =   av_clipl_int32((int64_t)(orig_eng << temp) + (1 << 15)) >> 16;
1466
+        exp  -=  temp;
1467
+
1468
+        if (ccr >= eng) {
1469
+            exp--;
1470
+            ccr >>= 1;
1471
+        }
1472
+        if (exp > max_exp)
1473
+            continue;
1474
+
1475
+        if (exp + 1 < max_exp)
1476
+            goto update;
1477
+
1478
+        /* Equalize exponents before comparison */
1479
+        if (exp + 1 == max_exp)
1480
+            temp = max_ccr >> 1;
1481
+        else
1482
+            temp = max_ccr;
1483
+        ccr_eng = ccr * max_eng;
1484
+        diff    = ccr_eng - eng * temp;
1485
+        if (diff > 0 && (i - index < PITCH_MIN || diff > ccr_eng >> 2)) {
1486
+update:
1487
+            index   = i;
1488
+            max_exp = exp;
1489
+            max_ccr = ccr;
1490
+            max_eng = eng;
1491
+        }
1492
+    }
1493
+    return index;
1494
+}
1495
+
1496
+/**
1497
+ * Compute harmonic noise filter parameters.
1498
+ *
1499
+ * @param buf       perceptually weighted speech
1500
+ * @param pitch_lag open loop pitch period
1501
+ * @param hf        harmonic filter parameters
1502
+ */
1503
+static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag, HFParam *hf)
1504
+{
1505
+    int ccr, eng, max_ccr, max_eng;
1506
+    int exp, max, diff;
1507
+    int energy[15];
1508
+    int i, j;
1509
+
1510
+    for (i = 0, j = pitch_lag - 3; j <= pitch_lag + 3; i++, j++) {
1511
+        /* Compute residual energy */
1512
+        energy[i << 1] = dot_product(buf - j, buf - j, SUBFRAME_LEN, 0);
1513
+        /* Compute correlation */
1514
+        energy[(i << 1) + 1] = dot_product(buf, buf - j, SUBFRAME_LEN, 0);
1515
+    }
1516
+
1517
+    /* Compute target energy */
1518
+    energy[14] = dot_product(buf, buf, SUBFRAME_LEN, 0);
1519
+
1520
+    /* Normalize */
1521
+    max = 0;
1522
+    for (i = 0; i < 15; i++)
1523
+        max = FFMAX(max, FFABS(energy[i]));
1524
+
1525
+    exp = normalize_bits_int32(max);
1526
+    for (i = 0; i < 15; i++) {
1527
+        energy[i] = av_clipl_int32((int64_t)(energy[i] << exp) +
1528
+                                   (1 << 15)) >> 16;
1529
+    }
1530
+
1531
+    hf->index = -1;
1532
+    hf->gain  =  0;
1533
+    max_ccr   =  1;
1534
+    max_eng   =  0x7fff;
1535
+
1536
+    for (i = 0; i <= 6; i++) {
1537
+        eng = energy[i << 1];
1538
+        ccr = energy[(i << 1) + 1];
1539
+
1540
+        if (ccr <= 0)
1541
+            continue;
1542
+
1543
+        ccr  = (ccr * ccr + (1 << 14)) >> 15;
1544
+        diff = ccr * max_eng - eng * max_ccr;
1545
+        if (diff > 0) {
1546
+            max_ccr   = ccr;
1547
+            max_eng   = eng;
1548
+            hf->index = i;
1549
+        }
1550
+    }
1551
+
1552
+    if (hf->index == -1) {
1553
+        hf->index = pitch_lag;
1554
+        return;
1555
+    }
1556
+
1557
+    eng = energy[14] * max_eng;
1558
+    eng = (eng >> 2) + (eng >> 3);
1559
+    ccr = energy[(hf->index << 1) + 1] * energy[(hf->index << 1) + 1];
1560
+    if (eng < ccr) {
1561
+        eng = energy[(hf->index << 1) + 1];
1562
+
1563
+        if (eng >= max_eng)
1564
+            hf->gain = 0x2800;
1565
+        else
1566
+            hf->gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15;
1567
+    }
1568
+    hf->index += pitch_lag - 3;
1569
+}
1570
+
1571
+/**
1572
+ * Apply the harmonic noise shaping filter.
1573
+ *
1574
+ * @param hf filter parameters
1575
+ */
1576
+static void harmonic_filter(HFParam *hf, int16_t *src, int16_t *dest)
1577
+{
1578
+    int i;
1579
+
1580
+    for (i = 0; i < SUBFRAME_LEN; i++) {
1581
+        int64_t temp = hf->gain * src[i - hf->index] << 1;
1582
+        dest[i] = av_clipl_int32((src[i] << 16) - temp + (1 << 15)) >> 16;
1583
+    }
1584
+}
1585
+
1586
+static void harmonic_noise_sub(HFParam *hf, int16_t *src, int16_t *dest)
1587
+{
1588
+    int i;
1589
+    for (i = 0; i < SUBFRAME_LEN; i++) {
1590
+        int64_t temp = hf->gain * src[i - hf->index] << 1;
1591
+        dest[i] = av_clipl_int32(((dest[i] - src[i]) << 16) + temp +
1592
+                                 (1 << 15)) >> 16;
1593
+
1594
+    }
1595
+}
1596
+
1597
+/**
1598
+ * Combined synthesis and formant perceptual weighting filer.
1599
+ *
1600
+ * @param qnt_lpc  quantized lpc coefficients
1601
+ * @param perf_lpc perceptual filter coefficients
1602
+ * @param perf_fir perceptual filter fir memory
1603
+ * @param perf_iir perceptual filter iir memory
1604
+ * @param scale    the filter output will be scaled by 2^scale
1605
+ */
1606
+static void synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc,
1607
+                                 int16_t *perf_fir, int16_t *perf_iir,
1608
+                                 int16_t *src, int16_t *dest, int scale)
1609
+{
1610
+    int i, j;
1611
+    int16_t buf_16[SUBFRAME_LEN + LPC_ORDER];
1612
+    int64_t buf[SUBFRAME_LEN];
1613
+
1614
+    int16_t *bptr_16 = buf_16 + LPC_ORDER;
1615
+
1616
+    memcpy(buf_16, perf_fir, sizeof(int16_t) * LPC_ORDER);
1617
+    memcpy(dest - LPC_ORDER, perf_iir, sizeof(int16_t) * LPC_ORDER);
1618
+
1619
+    for (i = 0; i < SUBFRAME_LEN; i++) {
1620
+        int64_t temp = 0;
1621
+        for (j = 1; j <= LPC_ORDER; j++)
1622
+            temp -= qnt_lpc[j - 1] * bptr_16[i - j];
1623
+
1624
+        buf[i]     = (src[i] << 15) + (temp << 3);
1625
+        bptr_16[i] = av_clipl_int32(buf[i] + (1 << 15)) >> 16;
1626
+    }
1627
+
1628
+    for (i = 0; i < SUBFRAME_LEN; i++) {
1629
+        int64_t fir = 0, iir = 0;
1630
+        for (j = 1; j <= LPC_ORDER; j++) {
1631
+            fir -= perf_lpc[j - 1] * bptr_16[i - j];
1632
+            iir += perf_lpc[j + LPC_ORDER - 1] * dest[i - j];
1633
+        }
1634
+        dest[i] = av_clipl_int32(((buf[i] + (fir << 3)) << scale) + (iir << 3) +
1635
+                                 (1 << 15)) >> 16;
1636
+    }
1637
+    memcpy(perf_fir, buf_16 + SUBFRAME_LEN, sizeof(int16_t) * LPC_ORDER);
1638
+    memcpy(perf_iir, dest + SUBFRAME_LEN - LPC_ORDER,
1639
+           sizeof(int16_t) * LPC_ORDER);
1640
+}
1641
+
1642
+/**
1643
+ * Compute the adaptive codebook contribution.
1644
+ *
1645
+ * @param buf   input signal
1646
+ * @param index the current subframe index
1647
+ */
1648
+static void acb_search(G723_1_Context *p, int16_t *residual,
1649
+                       int16_t *impulse_resp, int16_t *buf,
1650
+                       int index)
1651
+{
1652
+
1653
+    int16_t flt_buf[PITCH_ORDER][SUBFRAME_LEN];
1654
+
1655
+    const int16_t *cb_tbl = adaptive_cb_gain85;
1656
+
1657
+    int ccr_buf[PITCH_ORDER * SUBFRAMES << 2];
1658
+
1659
+    int pitch_lag = p->pitch_lag[index >> 1];
1660
+    int acb_lag   = 1;
1661
+    int acb_gain  = 0;
1662
+    int odd_frame = index & 1;
1663
+    int iter      = 3 + odd_frame;
1664
+    int count     = 0;
1665
+    int tbl_size  = 85;
1666
+
1667
+    int i, j, k, l, max;
1668
+    int64_t temp;
1669
+
1670
+    if (!odd_frame) {
1671
+        if (pitch_lag == PITCH_MIN)
1672
+            pitch_lag++;
1673
+        else
1674
+            pitch_lag = FFMIN(pitch_lag, PITCH_MAX - 5);
1675
+    }
1676
+
1677
+    for (i = 0; i < iter; i++) {
1678
+        get_residual(residual, p->prev_excitation, pitch_lag + i - 1);
1679
+
1680
+        for (j = 0; j < SUBFRAME_LEN; j++) {
1681
+            temp = 0;
1682
+            for (k = 0; k <= j; k++)
1683
+                temp += residual[PITCH_ORDER - 1 + k] * impulse_resp[j - k];
1684
+            flt_buf[PITCH_ORDER - 1][j] = av_clipl_int32((temp << 1) +
1685
+                                                         (1 << 15)) >> 16;
1686
+        }
1687
+
1688
+        for (j = PITCH_ORDER - 2; j >= 0; j--) {
1689
+            flt_buf[j][0] = ((residual[j] << 13) + (1 << 14)) >> 15;
1690
+            for (k = 1; k < SUBFRAME_LEN; k++) {
1691
+                temp = (flt_buf[j + 1][k - 1] << 15) +
1692
+                       residual[j] * impulse_resp[k];
1693
+                flt_buf[j][k] = av_clipl_int32((temp << 1) + (1 << 15)) >> 16;
1694
+            }
1695
+        }
1696
+
1697
+        /* Compute crosscorrelation with the signal */
1698
+        for (j = 0; j < PITCH_ORDER; j++) {
1699
+            temp = dot_product(buf, flt_buf[j], SUBFRAME_LEN, 0);
1700
+            ccr_buf[count++] = av_clipl_int32(temp << 1);
1701
+        }
1702
+
1703
+        /* Compute energies */
1704
+        for (j = 0; j < PITCH_ORDER; j++) {
1705
+            ccr_buf[count++] = dot_product(flt_buf[j], flt_buf[j],
1706
+                                           SUBFRAME_LEN, 1);
1707
+        }
1708
+
1709
+        for (j = 1; j < PITCH_ORDER; j++) {
1710
+            for (k = 0; k < j; k++) {
1711
+                temp = dot_product(flt_buf[j], flt_buf[k], SUBFRAME_LEN, 0);
1712
+                ccr_buf[count++] = av_clipl_int32(temp<<2);
1713
+            }
1714
+        }
1715
+    }
1716
+
1717
+    /* Normalize and shorten */
1718
+    max = 0;
1719
+    for (i = 0; i < 20 * iter; i++)
1720
+        max = FFMAX(max, FFABS(ccr_buf[i]));
1721
+
1722
+    temp = normalize_bits_int32(max);
1723
+
1724
+    for (i = 0; i < 20 * iter; i++){
1725
+        ccr_buf[i] = av_clipl_int32((int64_t)(ccr_buf[i] << temp) +
1726
+                                    (1 << 15)) >> 16;
1727
+    }
1728
+
1729
+    max = 0;
1730
+    for (i = 0; i < iter; i++) {
1731
+        /* Select quantization table */
1732
+        if (!odd_frame && pitch_lag + i - 1 >= SUBFRAME_LEN - 2 ||
1733
+            odd_frame && pitch_lag >= SUBFRAME_LEN - 2) {
1734
+            cb_tbl = adaptive_cb_gain170;
1735
+            tbl_size = 170;
1736
+        }
1737
+
1738
+        for (j = 0, k = 0; j < tbl_size; j++, k += 20) {
1739
+            temp = 0;
1740
+            for (l = 0; l < 20; l++)
1741
+                temp += ccr_buf[20 * i + l] * cb_tbl[k + l];
1742
+            temp =  av_clipl_int32(temp);
1743
+
1744
+            if (temp > max) {
1745
+                max      = temp;
1746
+                acb_gain = j;
1747
+                acb_lag  = i;
1748
+            }
1749
+        }
1750
+    }
1751
+
1752
+    if (!odd_frame) {
1753
+        pitch_lag += acb_lag - 1;
1754
+        acb_lag   =  1;
1755
+    }
1756
+
1757
+    p->pitch_lag[index >> 1]      = pitch_lag;
1758
+    p->subframe[index].ad_cb_lag  = acb_lag;
1759
+    p->subframe[index].ad_cb_gain = acb_gain;
1760
+}
1761
+
1762
+/**
1763
+ * Subtract the adaptive codebook contribution from the input
1764
+ * to obtain the residual.
1765
+ *
1766
+ * @param buf target vector
1767
+ */
1768
+static void sub_acb_contrib(int16_t *residual, int16_t *impulse_resp,
1769
+                            int16_t *buf)
1770
+{
1771
+    int i, j;
1772
+    /* Subtract adaptive CB contribution to obtain the residual */
1773
+    for (i = 0; i < SUBFRAME_LEN; i++) {
1774
+        int64_t temp = buf[i] << 14;
1775
+        for (j = 0; j <= i; j++)
1776
+            temp -= residual[j] * impulse_resp[i - j];
1777
+
1778
+        buf[i] = av_clipl_int32((temp << 2) + (1 << 15)) >> 16;
1779
+    }
1780
+}
1781
+
1782
+/**
1783
+ * Quantize the residual signal using the fixed codebook (MP-MLQ).
1784
+ *
1785
+ * @param optim optimized fixed codebook parameters
1786
+ * @param buf   excitation vector
1787
+ */
1788
+static void get_fcb_param(FCBParam *optim, int16_t *impulse_resp,
1789
+                          int16_t *buf, int pulse_cnt, int pitch_lag)
1790
+{
1791
+    FCBParam param;
1792
+    int16_t impulse_r[SUBFRAME_LEN];
1793
+    int16_t temp_corr[SUBFRAME_LEN];
1794
+    int16_t impulse_corr[SUBFRAME_LEN];
1795
+
1796
+    int ccr1[SUBFRAME_LEN];
1797
+    int ccr2[SUBFRAME_LEN];
1798
+    int amp, err, max, max_amp_index, min, scale, i, j, k, l;
1799
+
1800
+    int64_t temp;
1801
+
1802
+    /* Update impulse response */
1803
+    memcpy(impulse_r, impulse_resp, sizeof(int16_t) * SUBFRAME_LEN);
1804
+    param.dirac_train = 0;
1805
+    if (pitch_lag < SUBFRAME_LEN - 2) {
1806
+        param.dirac_train = 1;
1807
+        gen_dirac_train(impulse_r, pitch_lag);
1808
+    }
1809
+
1810
+    for (i = 0; i < SUBFRAME_LEN; i++)
1811
+        temp_corr[i] = impulse_r[i] >> 1;
1812
+
1813
+    /* Compute impulse response autocorrelation */
1814
+    temp = dot_product(temp_corr, temp_corr, SUBFRAME_LEN, 1);
1815
+
1816
+    scale = normalize_bits_int32(temp);
1817
+    impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
1818
+
1819
+    for (i = 1; i < SUBFRAME_LEN; i++) {
1820
+        temp = dot_product(temp_corr + i, temp_corr, SUBFRAME_LEN - i, 1);
1821
+        impulse_corr[i] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
1822
+    }
1823
+
1824
+    /* Compute crosscorrelation of impulse response with residual signal */
1825
+    scale -= 4;
1826
+    for (i = 0; i < SUBFRAME_LEN; i++){
1827
+        temp = dot_product(buf + i, impulse_r, SUBFRAME_LEN - i, 1);
1828
+        if (scale < 0)
1829
+            ccr1[i] = temp >> -scale;
1830
+        else
1831
+            ccr1[i] = av_clipl_int32(temp << scale);
1832
+    }
1833
+
1834
+    /* Search loop */
1835
+    for (i = 0; i < GRID_SIZE; i++) {
1836
+        /* Maximize the crosscorrelation */
1837
+        max = 0;
1838
+        for (j = i; j < SUBFRAME_LEN; j += GRID_SIZE) {
1839
+            temp = FFABS(ccr1[j]);
1840
+            if (temp >= max) {
1841
+                max = temp;
1842
+                param.pulse_pos[0] = j;
1843
+            }
1844
+        }
1845
+
1846
+        /* Quantize the gain (max crosscorrelation/impulse_corr[0]) */
1847
+        amp = max;
1848
+        min = 1 << 30;
1849
+        max_amp_index = GAIN_LEVELS - 2;
1850
+        for (j = max_amp_index; j >= 2; j--) {
1851
+            temp = av_clipl_int32((int64_t)fixed_cb_gain[j] *
1852
+                                  impulse_corr[0] << 1);
1853
+            temp = FFABS(temp - amp);
1854
+            if (temp < min) {
1855
+                min = temp;
1856
+                max_amp_index = j;
1857
+            }
1858
+        }
1859
+
1860
+        max_amp_index--;
1861
+        /* Select additional gain values */
1862
+        for (j = 1; j < 5; j++) {
1863
+            for (k = i; k < SUBFRAME_LEN; k += GRID_SIZE) {
1864
+                temp_corr[k] = 0;
1865
+                ccr2[k]      = ccr1[k];
1866
+            }
1867
+            param.amp_index = max_amp_index + j - 2;
1868
+            amp = fixed_cb_gain[param.amp_index];
1869
+
1870
+            param.pulse_sign[0] = (ccr2[param.pulse_pos[0]] < 0) ? -amp : amp;
1871
+            temp_corr[param.pulse_pos[0]] = 1;
1872
+
1873
+            for (k = 1; k < pulse_cnt; k++) {
1874
+                max = -1 << 30;
1875
+                for (l = i; l < SUBFRAME_LEN; l += GRID_SIZE) {
1876
+                    if (temp_corr[l])
1877
+                        continue;
1878
+                    temp = impulse_corr[FFABS(l - param.pulse_pos[k - 1])];
1879
+                    temp = av_clipl_int32((int64_t)temp *
1880
+                                          param.pulse_sign[k - 1] << 1);
1881
+                    ccr2[l] -= temp;
1882
+                    temp = FFABS(ccr2[l]);
1883
+                    if (temp > max) {
1884
+                        max = temp;
1885
+                        param.pulse_pos[k] = l;
1886
+                    }
1887
+                }
1888
+
1889
+                param.pulse_sign[k] = (ccr2[param.pulse_pos[k]] < 0) ?
1890
+                                      -amp : amp;
1891
+                temp_corr[param.pulse_pos[k]] = 1;
1892
+            }
1893
+
1894
+            /* Create the error vector */
1895
+            memset(temp_corr, 0, sizeof(int16_t) * SUBFRAME_LEN);
1896
+
1897
+            for (k = 0; k < pulse_cnt; k++)
1898
+                temp_corr[param.pulse_pos[k]] = param.pulse_sign[k];
1899
+
1900
+            for (k = SUBFRAME_LEN - 1; k >= 0; k--) {
1901
+                temp = 0;
1902
+                for (l = 0; l <= k; l++) {
1903
+                    int prod = av_clipl_int32((int64_t)temp_corr[l] *
1904
+                                              impulse_r[k - l] << 1);
1905
+                    temp     = av_clipl_int32(temp + prod);
1906
+                }
1907
+                temp_corr[k] = temp << 2 >> 16;
1908
+            }
1909
+
1910
+            /* Compute square of error */
1911
+            err = 0;
1912
+            for (k = 0; k < SUBFRAME_LEN; k++) {
1913
+                int64_t prod;
1914
+                prod = av_clipl_int32((int64_t)buf[k] * temp_corr[k] << 1);
1915
+                err  = av_clipl_int32(err - prod);
1916
+                prod = av_clipl_int32((int64_t)temp_corr[k] * temp_corr[k]);
1917
+                err  = av_clipl_int32(err + prod);
1918
+            }
1919
+
1920
+            /* Minimize */
1921
+            if (err < optim->min_err) {
1922
+                optim->min_err     = err;
1923
+                optim->grid_index  = i;
1924
+                optim->amp_index   = param.amp_index;
1925
+                optim->dirac_train = param.dirac_train;
1926
+
1927
+                for (k = 0; k < pulse_cnt; k++) {
1928
+                    optim->pulse_sign[k] = param.pulse_sign[k];
1929
+                    optim->pulse_pos[k]  = param.pulse_pos[k];
1930
+                }
1931
+            }
1932
+        }
1933
+    }
1934
+}
1935
+
1936
+/**
1937
+ * Encode the pulse position and gain of the current subframe.
1938
+ *
1939
+ * @param optim optimized fixed CB parameters
1940
+ * @param buf   excitation vector
1941
+ */
1942
+static void pack_fcb_param(G723_1_Subframe *subfrm, FCBParam *optim,
1943
+                           int16_t *buf, int pulse_cnt)
1944
+{
1945
+    int i, j;
1946
+
1947
+    j = PULSE_MAX - pulse_cnt;
1948
+
1949
+    subfrm->pulse_sign = 0;
1950
+    subfrm->pulse_pos  = 0;
1951
+
1952
+    for (i = 0; i < SUBFRAME_LEN >> 1; i++) {
1953
+        int val = buf[optim->grid_index + (i << 1)];
1954
+        if (!val) {
1955
+            subfrm->pulse_pos += combinatorial_table[j][i];
1956
+        } else {
1957
+            subfrm->pulse_sign <<= 1;
1958
+            if (val < 0) subfrm->pulse_sign++;
1959
+            j++;
1960
+
1961
+            if (j == PULSE_MAX) break;
1962
+        }
1963
+    }
1964
+    subfrm->amp_index   = optim->amp_index;
1965
+    subfrm->grid_index  = optim->grid_index;
1966
+    subfrm->dirac_train = optim->dirac_train;
1967
+}
1968
+
1969
+/**
1970
+ * Compute the fixed codebook excitation.
1971
+ *
1972
+ * @param buf          target vector
1973
+ * @param impulse_resp impulse response of the combined filter
1974
+ */
1975
+static void fcb_search(G723_1_Context *p, int16_t *impulse_resp,
1976
+                       int16_t *buf, int index)
1977
+{
1978
+    FCBParam optim;
1979
+    int pulse_cnt = pulses[index];
1980
+    int i;
1981
+
1982
+    optim.min_err = 1 << 30;
1983
+    get_fcb_param(&optim, impulse_resp, buf, pulse_cnt, SUBFRAME_LEN);
1984
+
1985
+    if (p->pitch_lag[index >> 1] < SUBFRAME_LEN - 2) {
1986
+        get_fcb_param(&optim, impulse_resp, buf, pulse_cnt,
1987
+                      p->pitch_lag[index >> 1]);
1988
+    }
1989
+
1990
+    /* Reconstruct the excitation */
1991
+    memset(buf, 0, sizeof(int16_t) * SUBFRAME_LEN);
1992
+    for (i = 0; i < pulse_cnt; i++)
1993
+        buf[optim.pulse_pos[i]] = optim.pulse_sign[i];
1994
+
1995
+    pack_fcb_param(&p->subframe[index], &optim, buf, pulse_cnt);
1996
+
1997
+    if (optim.dirac_train)
1998
+        gen_dirac_train(buf, p->pitch_lag[index >> 1]);
1999
+}
2000
+
2001
+/**
2002
+ * Pack the frame parameters into output bitstream.
2003
+ *
2004
+ * @param frame output buffer
2005
+ * @param size  size of the buffer
2006
+ */
2007
+static int pack_bitstream(G723_1_Context *p, unsigned char *frame, int size)
2008
+{
2009
+    PutBitContext pb;
2010
+    int info_bits, i, temp;
2011
+
2012
+    init_put_bits(&pb, frame, size);
2013
+
2014
+    if (p->cur_rate == Rate6k3) {
2015
+        info_bits = 0;
2016
+        put_bits(&pb, 2, info_bits);
2017
+    }
2018
+
2019
+    put_bits(&pb, 8, p->lsp_index[2]);
2020
+    put_bits(&pb, 8, p->lsp_index[1]);
2021
+    put_bits(&pb, 8, p->lsp_index[0]);
2022
+
2023
+    put_bits(&pb, 7, p->pitch_lag[0] - PITCH_MIN);
2024
+    put_bits(&pb, 2, p->subframe[1].ad_cb_lag);
2025
+    put_bits(&pb, 7, p->pitch_lag[1] - PITCH_MIN);
2026
+    put_bits(&pb, 2, p->subframe[3].ad_cb_lag);
2027
+
2028
+    /* Write 12 bit combined gain */
2029
+    for (i = 0; i < SUBFRAMES; i++) {
2030
+        temp = p->subframe[i].ad_cb_gain * GAIN_LEVELS +
2031
+               p->subframe[i].amp_index;
2032
+        if (p->cur_rate ==  Rate6k3)
2033
+            temp += p->subframe[i].dirac_train << 11;
2034
+        put_bits(&pb, 12, temp);
2035
+    }
2036
+
2037
+    put_bits(&pb, 1, p->subframe[0].grid_index);
2038
+    put_bits(&pb, 1, p->subframe[1].grid_index);
2039
+    put_bits(&pb, 1, p->subframe[2].grid_index);
2040
+    put_bits(&pb, 1, p->subframe[3].grid_index);
2041
+
2042
+    if (p->cur_rate == Rate6k3) {
2043
+        skip_put_bits(&pb, 1); /* reserved bit */
2044
+
2045
+        /* Write 13 bit combined position index */
2046
+        temp = (p->subframe[0].pulse_pos >> 16) * 810 +
2047
+               (p->subframe[1].pulse_pos >> 14) *  90 +
2048
+               (p->subframe[2].pulse_pos >> 16) *   9 +
2049
+               (p->subframe[3].pulse_pos >> 14);
2050
+        put_bits(&pb, 13, temp);
2051
+
2052
+        put_bits(&pb, 16, p->subframe[0].pulse_pos & 0xffff);
2053
+        put_bits(&pb, 14, p->subframe[1].pulse_pos & 0x3fff);
2054
+        put_bits(&pb, 16, p->subframe[2].pulse_pos & 0xffff);
2055
+        put_bits(&pb, 14, p->subframe[3].pulse_pos & 0x3fff);
2056
+
2057
+        put_bits(&pb, 6, p->subframe[0].pulse_sign);
2058
+        put_bits(&pb, 5, p->subframe[1].pulse_sign);
2059
+        put_bits(&pb, 6, p->subframe[2].pulse_sign);
2060
+        put_bits(&pb, 5, p->subframe[3].pulse_sign);
2061
+    }
2062
+
2063
+    flush_put_bits(&pb);
2064
+    return frame_size[info_bits];
2065
+}
2066
+
2067
+static int g723_1_encode_frame(AVCodecContext *avctx, unsigned char *buf,
2068
+                               int buf_size, void *data)
2069
+{
2070
+    G723_1_Context *p = avctx->priv_data;
2071
+    int16_t unq_lpc[LPC_ORDER * SUBFRAMES];
2072
+    int16_t qnt_lpc[LPC_ORDER * SUBFRAMES];
2073
+    int16_t cur_lsp[LPC_ORDER];
2074
+    int16_t weighted_lpc[LPC_ORDER * SUBFRAMES << 1];
2075
+    int16_t vector[FRAME_LEN + PITCH_MAX];
2076
+    int offset;
2077
+    int16_t *in = data;
2078
+
2079
+    HFParam hf[4];
2080
+    int i, j;
2081
+
2082
+    highpass_filter(in, &p->hpf_fir_mem, &p->hpf_iir_mem);
2083
+
2084
+    memcpy(vector, p->prev_data, HALF_FRAME_LEN * sizeof(int16_t));
2085
+    memcpy(vector + HALF_FRAME_LEN, in, FRAME_LEN * sizeof(int16_t));
2086
+
2087
+    comp_lpc_coeff(vector, unq_lpc);
2088
+    lpc2lsp(&unq_lpc[LPC_ORDER * 3], p->prev_lsp, cur_lsp);
2089
+    lsp_quantize(p->lsp_index, cur_lsp, p->prev_lsp);
2090
+
2091
+    /* Update memory */
2092
+    memcpy(vector + LPC_ORDER, p->prev_data + SUBFRAME_LEN,
2093
+           sizeof(int16_t) * SUBFRAME_LEN);
2094
+    memcpy(vector + LPC_ORDER + SUBFRAME_LEN, in,
2095
+           sizeof(int16_t) * (HALF_FRAME_LEN + SUBFRAME_LEN));
2096
+    memcpy(p->prev_data, in + HALF_FRAME_LEN,
2097
+           sizeof(int16_t) * HALF_FRAME_LEN);
2098
+    memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
2099
+
2100
+    perceptual_filter(p, weighted_lpc, unq_lpc, vector);
2101
+
2102
+    memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
2103
+    memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
2104
+    memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
2105
+
2106
+    scale_vector(vector, FRAME_LEN + PITCH_MAX);
2107
+
2108
+    p->pitch_lag[0] = estimate_pitch(vector, PITCH_MAX);
2109
+    p->pitch_lag[1] = estimate_pitch(vector, PITCH_MAX + HALF_FRAME_LEN);
2110
+
2111
+    for (i = PITCH_MAX, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
2112
+        comp_harmonic_coeff(vector + i, p->pitch_lag[j >> 1], hf + j);
2113
+
2114
+    memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
2115
+    memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
2116
+    memcpy(p->prev_weight_sig, vector + FRAME_LEN, sizeof(int16_t) * PITCH_MAX);
2117
+
2118
+    for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
2119
+        harmonic_filter(hf + j, vector + PITCH_MAX + i, in + i);
2120
+
2121
+    inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, 0);
2122
+    lsp_interpolate(qnt_lpc, cur_lsp, p->prev_lsp);
2123
+
2124
+    memcpy(p->prev_lsp, cur_lsp, sizeof(int16_t) * LPC_ORDER);
2125
+
2126
+    offset = 0;
2127
+    for (i = 0; i < SUBFRAMES; i++) {
2128
+        int16_t impulse_resp[SUBFRAME_LEN];
2129
+        int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
2130
+        int16_t flt_in[SUBFRAME_LEN];
2131
+        int16_t zero[LPC_ORDER], fir[LPC_ORDER], iir[LPC_ORDER];
2132
+
2133
+        /**
2134
+         * Compute the combined impulse response of the synthesis filter,
2135
+         * formant perceptual weighting filter and harmonic noise shaping filter
2136
+         */
2137
+        memset(zero, 0, sizeof(int16_t) * LPC_ORDER);
2138
+        memset(vector, 0, sizeof(int16_t) * PITCH_MAX);
2139
+        memset(flt_in, 0, sizeof(int16_t) * SUBFRAME_LEN);
2140
+
2141
+        flt_in[0] = 1 << 13; /* Unit impulse */
2142
+        synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
2143
+                             zero, zero, flt_in, vector + PITCH_MAX, 1);
2144
+        harmonic_filter(hf + i, vector + PITCH_MAX, impulse_resp);
2145
+
2146
+         /* Compute the combined zero input response */
2147
+        flt_in[0] = 0;
2148
+        memcpy(fir, p->perf_fir_mem, sizeof(int16_t) * LPC_ORDER);
2149
+        memcpy(iir, p->perf_iir_mem, sizeof(int16_t) * LPC_ORDER);
2150
+
2151
+        synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
2152
+                             fir, iir, flt_in, vector + PITCH_MAX, 0);
2153
+        memcpy(vector, p->harmonic_mem, sizeof(int16_t) * PITCH_MAX);
2154
+        harmonic_noise_sub(hf + i, vector + PITCH_MAX, in);
2155
+
2156
+        acb_search(p, residual, impulse_resp, in, i);
2157
+        gen_acb_excitation(residual, p->prev_excitation,p->pitch_lag[i >> 1],
2158
+                           p->subframe[i], p->cur_rate);
2159
+        sub_acb_contrib(residual, impulse_resp, in);
2160
+
2161
+        fcb_search(p, impulse_resp, in, i);
2162
+
2163
+        /* Reconstruct the excitation */
2164
+        gen_acb_excitation(impulse_resp, p->prev_excitation, p->pitch_lag[i >> 1],
2165
+                           p->subframe[i], Rate6k3);
2166
+
2167
+        memcpy(p->prev_excitation, p->prev_excitation + SUBFRAME_LEN,
2168
+               sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
2169
+        for (j = 0; j < SUBFRAME_LEN; j++)
2170
+            in[j] = av_clip_int16((in[j] << 1) + impulse_resp[j]);
2171
+        memcpy(p->prev_excitation + PITCH_MAX - SUBFRAME_LEN, in,
2172
+               sizeof(int16_t) * SUBFRAME_LEN);
2173
+
2174
+        /* Update filter memories */
2175
+        synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
2176
+                             p->perf_fir_mem, p->perf_iir_mem,
2177
+                             in, vector + PITCH_MAX, 0);
2178
+        memmove(p->harmonic_mem, p->harmonic_mem + SUBFRAME_LEN,
2179
+                sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
2180
+        memcpy(p->harmonic_mem + PITCH_MAX - SUBFRAME_LEN, vector + PITCH_MAX,
2181
+               sizeof(int16_t) * SUBFRAME_LEN);
2182
+
2183
+        in += SUBFRAME_LEN;
2184
+        offset += LPC_ORDER;
2185
+    }
2186
+
2187
+    return pack_bitstream(p, buf, buf_size);
2188
+}
2189
+
2190
+AVCodec ff_g723_1_encoder = {
2191
+    .name           = "g723_1",
2192
+    .type           = AVMEDIA_TYPE_AUDIO,
2193
+    .id             = CODEC_ID_G723_1,
2194
+    .priv_data_size = sizeof(G723_1_Context),
2195
+    .init           = g723_1_encode_init,
2196
+    .encode         = g723_1_encode_frame,
2197
+    .long_name      = NULL_IF_CONFIG_SMALL("G.723.1"),
2198
+    .sample_fmts    = (const enum SampleFormat[]){SAMPLE_FMT_S16,
2199
+                                                  SAMPLE_FMT_NONE},
2200
+};
2201
+#endif
... ...
@@ -28,6 +28,8 @@
28 28
 #define SUBFRAMES       4
29 29
 #define SUBFRAME_LEN    60
30 30
 #define FRAME_LEN       (SUBFRAME_LEN << 2)
31
+#define HALF_FRAME_LEN  (FRAME_LEN / 2)
32
+#define LPC_FRAME       (HALF_FRAME_LEN + SUBFRAME_LEN)
31 33
 #define LPC_ORDER       10
32 34
 #define LSP_BANDS       3
33 35
 #define LSP_CB_SIZE     256
... ...
@@ -78,6 +80,26 @@ typedef struct {
78 78
 } PPFParam;
79 79
 
80 80
 /**
81
+ * Harmonic filter parameters
82
+ */
83
+typedef struct {
84
+    int index;
85
+    int gain;
86
+} HFParam;
87
+
88
+/**
89
+ * Optimized fixed codebook excitation parameters
90
+ */
91
+typedef struct {
92
+    int min_err;
93
+    int amp_index;
94
+    int grid_index;
95
+    int dirac_train;
96
+    int pulse_pos[PULSE_MAX];
97
+    int pulse_sign[PULSE_MAX];
98
+} FCBParam;
99
+
100
+/**
81 101
  * Postfilter gain weighting factors scaled by 2^15
82 102
  */
83 103
 static const int16_t ppf_gain_weight[2] = {0x1800, 0x2000};
... ...
@@ -1243,3 +1265,51 @@ static const int16_t postfilter_tbl[2][LPC_ORDER] = {
1243 1243
     /* Pole */
1244 1244
     {24576, 18432, 13824, 10368, 7776, 5832, 4374, 3281, 2460, 1845}
1245 1245
 };
1246
+
1247
+/**
1248
+ * Hamming window coefficients scaled by 2^15
1249
+ */
1250
+static const int16_t hamming_window[LPC_FRAME] = {
1251
+     2621,  2631,  2659,  2705,  2770,  2853,  2955,  3074,  3212,  3367,
1252
+     3541,  3731,  3939,  4164,  4405,  4663,  4937,  5226,  5531,  5851,
1253
+     6186,  6534,  6897,  7273,  7661,  8062,  8475,  8899,  9334,  9780,
1254
+    10235, 10699, 11172, 11653, 12141, 12636, 13138, 13645, 14157, 14673,
1255
+    15193, 15716, 16242, 16769, 17298, 17827, 18356, 18884, 19411, 19935,
1256
+    20457, 20975, 21489, 21999, 22503, 23002, 23494, 23978, 24455, 24924,
1257
+    25384, 25834, 26274, 26704, 27122, 27529, 27924, 28306, 28675, 29031,
1258
+    29373, 29700, 30012, 30310, 30592, 30857, 31107, 31340, 31557, 31756,
1259
+    31938, 32102, 32249, 32377, 32488, 32580, 32654, 32710, 32747, 32766,
1260
+    32766, 32747, 32710, 32654, 32580, 32488, 32377, 32249, 32102, 31938,
1261
+    31756, 31557, 31340, 31107, 30857, 30592, 30310, 30012, 29700, 29373,
1262
+    29031, 28675, 28306, 27924, 27529, 27122, 26704, 26274, 25834, 25384,
1263
+    24924, 24455, 23978, 23494, 23002, 22503, 21999, 21489, 20975, 20457,
1264
+    19935, 19411, 18884, 18356, 17827, 17298, 16769, 16242, 15716, 15193,
1265
+    14673, 14157, 13645, 13138, 12636, 12141, 11653, 11172, 10699, 10235,
1266
+     9780, 9334,   8899,  8475,  8062,  7661,  7273,  6897,  6534,  6186,
1267
+     5851, 5531,   5226,  4937,  4663,  4405,  4164,  3939,  3731,  3541,
1268
+     3367, 3212,   3074,  2955,  2853,  2770,  2705,  2659,  2631,  2621
1269
+};
1270
+
1271
+/**
1272
+ * Binomial window coefficients scaled by 2^15
1273
+ */
1274
+static const int16_t binomial_window[LPC_ORDER] = {
1275
+    32749, 32695, 32604, 32477, 32315, 32118, 31887, 31622, 31324, 30995
1276
+};
1277
+
1278
+/**
1279
+ * 0.994^i scaled by 2^15
1280
+ */
1281
+static const int16_t bandwidth_expand[LPC_ORDER] = {
1282
+    32571, 32376, 32182, 31989, 31797, 31606, 31416, 31228, 31040, 30854
1283
+};
1284
+
1285
+/**
1286
+ * 0.5^i scaled by 2^15
1287
+ */
1288
+static const int16_t percept_flt_tbl[2][LPC_ORDER] = {
1289
+    /* Zero part */
1290
+    {29491, 26542, 23888, 21499, 19349, 17414, 15673, 14106, 12695, 11425},
1291
+    /* Pole part */
1292
+    {16384,  8192,  4096,  2048,  1024,   512,   256,   128,    64,    32}
1293
+};