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 "codec_internal.h"
38 #include "encode.h"
39 #include "g723_1.h"
40
41 #define BITSTREAM_WRITER_LE
42 #include "put_bits.h"
43
44 /**
45 * Hamming window coefficients scaled by 2^15
46 */
47 static const int16_t hamming_window[LPC_FRAME] = {
48 2621, 2631, 2659, 2705, 2770, 2853, 2955, 3074, 3212, 3367,
49 3541, 3731, 3939, 4164, 4405, 4663, 4937, 5226, 5531, 5851,
50 6186, 6534, 6897, 7273, 7661, 8062, 8475, 8899, 9334, 9780,
51 10235, 10699, 11172, 11653, 12141, 12636, 13138, 13645, 14157, 14673,
52 15193, 15716, 16242, 16769, 17298, 17827, 18356, 18884, 19411, 19935,
53 20457, 20975, 21489, 21999, 22503, 23002, 23494, 23978, 24455, 24924,
54 25384, 25834, 26274, 26704, 27122, 27529, 27924, 28306, 28675, 29031,
55 29373, 29700, 30012, 30310, 30592, 30857, 31107, 31340, 31557, 31756,
56 31938, 32102, 32249, 32377, 32488, 32580, 32654, 32710, 32747, 32766,
57 32766, 32747, 32710, 32654, 32580, 32488, 32377, 32249, 32102, 31938,
58 31756, 31557, 31340, 31107, 30857, 30592, 30310, 30012, 29700, 29373,
59 29031, 28675, 28306, 27924, 27529, 27122, 26704, 26274, 25834, 25384,
60 24924, 24455, 23978, 23494, 23002, 22503, 21999, 21489, 20975, 20457,
61 19935, 19411, 18884, 18356, 17827, 17298, 16769, 16242, 15716, 15193,
62 14673, 14157, 13645, 13138, 12636, 12141, 11653, 11172, 10699, 10235,
63 9780, 9334, 8899, 8475, 8062, 7661, 7273, 6897, 6534, 6186,
64 5851, 5531, 5226, 4937, 4663, 4405, 4164, 3939, 3731, 3541,
65 3367, 3212, 3074, 2955, 2853, 2770, 2705, 2659, 2631, 2621
66 };
67
68 /**
69 * Binomial window coefficients scaled by 2^15
70 */
71 static const int16_t binomial_window[LPC_ORDER] = {
72 32749, 32695, 32604, 32477, 32315, 32118, 31887, 31622, 31324, 30995
73 };
74
75 /**
76 * 0.994^i scaled by 2^15
77 */
78 static const int16_t bandwidth_expand[LPC_ORDER] = {
79 32571, 32376, 32182, 31989, 31797, 31606, 31416, 31228, 31040, 30854
80 };
81
82 /**
83 * 0.5^i scaled by 2^15
84 */
85 static const int16_t percept_flt_tbl[2][LPC_ORDER] = {
86 /* Zero part */
87 {29491, 26542, 23888, 21499, 19349, 17414, 15673, 14106, 12695, 11425},
88 /* Pole part */
89 {16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32}
90 };
91
g723_1_encode_init(AVCodecContext *avctx)92 static av_cold int g723_1_encode_init(AVCodecContext *avctx)
93 {
94 G723_1_Context *s = avctx->priv_data;
95 G723_1_ChannelContext *p = &s->ch[0];
96
97 if (avctx->sample_rate != 8000) {
98 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
99 return AVERROR(EINVAL);
100 }
101
102 if (avctx->bit_rate == 6300) {
103 p->cur_rate = RATE_6300;
104 } else if (avctx->bit_rate == 5300) {
105 av_log(avctx, AV_LOG_ERROR, "Use bitrate 6300 instead of 5300.\n");
106 avpriv_report_missing_feature(avctx, "Bitrate 5300");
107 return AVERROR_PATCHWELCOME;
108 } else {
109 av_log(avctx, AV_LOG_ERROR, "Bitrate not supported, use 6300\n");
110 return AVERROR(EINVAL);
111 }
112 avctx->frame_size = 240;
113 memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(int16_t));
114
115 return 0;
116 }
117
118 /**
119 * Remove DC component from the input signal.
120 *
121 * @param buf input signal
122 * @param fir zero memory
123 * @param iir pole memory
124 */
highpass_filter(int16_t *buf, int16_t *fir, int *iir)125 static void highpass_filter(int16_t *buf, int16_t *fir, int *iir)
126 {
127 int i;
128 for (i = 0; i < FRAME_LEN; i++) {
129 *iir = (buf[i] << 15) + ((-*fir) << 15) + MULL2(*iir, 0x7f00);
130 *fir = buf[i];
131 buf[i] = av_clipl_int32((int64_t)*iir + (1 << 15)) >> 16;
132 }
133 }
134
135 /**
136 * Estimate autocorrelation of the input vector.
137 *
138 * @param buf input buffer
139 * @param autocorr autocorrelation coefficients vector
140 */
comp_autocorr(int16_t *buf, int16_t *autocorr)141 static void comp_autocorr(int16_t *buf, int16_t *autocorr)
142 {
143 int i, scale, temp;
144 int16_t vector[LPC_FRAME];
145
146 ff_g723_1_scale_vector(vector, buf, LPC_FRAME);
147
148 /* Apply the Hamming window */
149 for (i = 0; i < LPC_FRAME; i++)
150 vector[i] = (vector[i] * hamming_window[i] + (1 << 14)) >> 15;
151
152 /* Compute the first autocorrelation coefficient */
153 temp = ff_dot_product(vector, vector, LPC_FRAME);
154
155 /* Apply a white noise correlation factor of (1025/1024) */
156 temp += temp >> 10;
157
158 /* Normalize */
159 scale = ff_g723_1_normalize_bits(temp, 31);
160 autocorr[0] = av_clipl_int32((int64_t) (temp << scale) +
161 (1 << 15)) >> 16;
162
163 /* Compute the remaining coefficients */
164 if (!autocorr[0]) {
165 memset(autocorr + 1, 0, LPC_ORDER * sizeof(int16_t));
166 } else {
167 for (i = 1; i <= LPC_ORDER; i++) {
168 temp = ff_dot_product(vector, vector + i, LPC_FRAME - i);
169 temp = MULL2((temp << scale), binomial_window[i - 1]);
170 autocorr[i] = av_clipl_int32((int64_t) temp + (1 << 15)) >> 16;
171 }
172 }
173 }
174
175 /**
176 * Use Levinson-Durbin recursion to compute LPC coefficients from
177 * autocorrelation values.
178 *
179 * @param lpc LPC coefficients vector
180 * @param autocorr autocorrelation coefficients vector
181 * @param error prediction error
182 */
levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)183 static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)
184 {
185 int16_t vector[LPC_ORDER];
186 int16_t partial_corr;
187 int i, j, temp;
188
189 memset(lpc, 0, LPC_ORDER * sizeof(int16_t));
190
191 for (i = 0; i < LPC_ORDER; i++) {
192 /* Compute the partial correlation coefficient */
193 temp = 0;
194 for (j = 0; j < i; j++)
195 temp -= lpc[j] * autocorr[i - j - 1];
196 temp = ((autocorr[i] << 13) + temp) << 3;
197
198 if (FFABS(temp) >= (error << 16))
199 break;
200
201 partial_corr = temp / (error << 1);
202
203 lpc[i] = av_clipl_int32((int64_t) (partial_corr << 14) +
204 (1 << 15)) >> 16;
205
206 /* Update the prediction error */
207 temp = MULL2(temp, partial_corr);
208 error = av_clipl_int32((int64_t) (error << 16) - temp +
209 (1 << 15)) >> 16;
210
211 memcpy(vector, lpc, i * sizeof(int16_t));
212 for (j = 0; j < i; j++) {
213 temp = partial_corr * vector[i - j - 1] << 1;
214 lpc[j] = av_clipl_int32((int64_t) (lpc[j] << 16) - temp +
215 (1 << 15)) >> 16;
216 }
217 }
218 }
219
220 /**
221 * Calculate LPC coefficients for the current frame.
222 *
223 * @param buf current frame
224 * @param prev_data 2 trailing subframes of the previous frame
225 * @param lpc LPC coefficients vector
226 */
comp_lpc_coeff(int16_t *buf, int16_t *lpc)227 static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
228 {
229 int16_t autocorr[(LPC_ORDER + 1) * SUBFRAMES];
230 int16_t *autocorr_ptr = autocorr;
231 int16_t *lpc_ptr = lpc;
232 int i, j;
233
234 for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
235 comp_autocorr(buf + i, autocorr_ptr);
236 levinson_durbin(lpc_ptr, autocorr_ptr + 1, autocorr_ptr[0]);
237
238 lpc_ptr += LPC_ORDER;
239 autocorr_ptr += LPC_ORDER + 1;
240 }
241 }
242
lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)243 static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
244 {
245 int f[LPC_ORDER + 2]; ///< coefficients of the sum and difference
246 ///< polynomials (F1, F2) ordered as
247 ///< f1[0], f2[0], ...., f1[5], f2[5]
248
249 int max, shift, cur_val, prev_val, count, p;
250 int i, j;
251 int64_t temp;
252
253 /* Initialize f1[0] and f2[0] to 1 in Q25 */
254 for (i = 0; i < LPC_ORDER; i++)
255 lsp[i] = (lpc[i] * bandwidth_expand[i] + (1 << 14)) >> 15;
256
257 /* Apply bandwidth expansion on the LPC coefficients */
258 f[0] = f[1] = 1 << 25;
259
260 /* Compute the remaining coefficients */
261 for (i = 0; i < LPC_ORDER / 2; i++) {
262 /* f1 */
263 f[2 * i + 2] = -f[2 * i] - ((lsp[i] + lsp[LPC_ORDER - 1 - i]) << 12);
264 /* f2 */
265 f[2 * i + 3] = f[2 * i + 1] - ((lsp[i] - lsp[LPC_ORDER - 1 - i]) << 12);
266 }
267
268 /* Divide f1[5] and f2[5] by 2 for use in polynomial evaluation */
269 f[LPC_ORDER] >>= 1;
270 f[LPC_ORDER + 1] >>= 1;
271
272 /* Normalize and shorten */
273 max = FFABS(f[0]);
274 for (i = 1; i < LPC_ORDER + 2; i++)
275 max = FFMAX(max, FFABS(f[i]));
276
277 shift = ff_g723_1_normalize_bits(max, 31);
278
279 for (i = 0; i < LPC_ORDER + 2; i++)
280 f[i] = av_clipl_int32((int64_t) (f[i] << shift) + (1 << 15)) >> 16;
281
282 /**
283 * Evaluate F1 and F2 at uniform intervals of pi/256 along the
284 * unit circle and check for zero crossings.
285 */
286 p = 0;
287 temp = 0;
288 for (i = 0; i <= LPC_ORDER / 2; i++)
289 temp += f[2 * i] * G723_1_COS_TAB_FIRST_ELEMENT;
290 prev_val = av_clipl_int32(temp << 1);
291 count = 0;
292 for (i = 1; i < COS_TBL_SIZE / 2; i++) {
293 /* Evaluate */
294 temp = 0;
295 for (j = 0; j <= LPC_ORDER / 2; j++)
296 temp += f[LPC_ORDER - 2 * j + p] * ff_g723_1_cos_tab[i * j % COS_TBL_SIZE];
297 cur_val = av_clipl_int32(temp << 1);
298
299 /* Check for sign change, indicating a zero crossing */
300 if ((cur_val ^ prev_val) < 0) {
301 int abs_cur = FFABS(cur_val);
302 int abs_prev = FFABS(prev_val);
303 int sum = abs_cur + abs_prev;
304
305 shift = ff_g723_1_normalize_bits(sum, 31);
306 sum <<= shift;
307 abs_prev = abs_prev << shift >> 8;
308 lsp[count++] = ((i - 1) << 7) + (abs_prev >> 1) / (sum >> 16);
309
310 if (count == LPC_ORDER)
311 break;
312
313 /* Switch between sum and difference polynomials */
314 p ^= 1;
315
316 /* Evaluate */
317 temp = 0;
318 for (j = 0; j <= LPC_ORDER / 2; j++)
319 temp += f[LPC_ORDER - 2 * j + p] *
320 ff_g723_1_cos_tab[i * j % COS_TBL_SIZE];
321 cur_val = av_clipl_int32(temp << 1);
322 }
323 prev_val = cur_val;
324 }
325
326 if (count != LPC_ORDER)
327 memcpy(lsp, prev_lsp, LPC_ORDER * sizeof(int16_t));
328 }
329
330 /**
331 * Quantize the current LSP subvector.
332 *
333 * @param num band number
334 * @param offset offset of the current subvector in an LPC_ORDER vector
335 * @param size size of the current subvector
336 */
337 #define get_index(num, offset, size) \
338 { \
339 int error, max = -1; \
340 int16_t temp[4]; \
341 int i, j; \
342 \
343 for (i = 0; i < LSP_CB_SIZE; i++) { \
344 for (j = 0; j < size; j++){ \
345 temp[j] = (weight[j + (offset)] * ff_g723_1_lsp_band##num[i][j] + \
346 (1 << 14)) >> 15; \
347 } \
348 error = ff_g723_1_dot_product(lsp + (offset), temp, size) << 1; \
349 error -= ff_g723_1_dot_product(ff_g723_1_lsp_band##num[i], temp, size); \
350 if (error > max) { \
351 max = error; \
352 lsp_index[num] = i; \
353 } \
354 } \
355 }
356
357 /**
358 * Vector quantize the LSP frequencies.
359 *
360 * @param lsp the current lsp vector
361 * @param prev_lsp the previous lsp vector
362 */
lsp_quantize(uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)363 static void lsp_quantize(uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
364 {
365 int16_t weight[LPC_ORDER];
366 int16_t min, max;
367 int shift, i;
368
369 /* Calculate the VQ weighting vector */
370 weight[0] = (1 << 20) / (lsp[1] - lsp[0]);
371 weight[LPC_ORDER - 1] = (1 << 20) /
372 (lsp[LPC_ORDER - 1] - lsp[LPC_ORDER - 2]);
373
374 for (i = 1; i < LPC_ORDER - 1; i++) {
375 min = FFMIN(lsp[i] - lsp[i - 1], lsp[i + 1] - lsp[i]);
376 if (min > 0x20)
377 weight[i] = (1 << 20) / min;
378 else
379 weight[i] = INT16_MAX;
380 }
381
382 /* Normalize */
383 max = 0;
384 for (i = 0; i < LPC_ORDER; i++)
385 max = FFMAX(weight[i], max);
386
387 shift = ff_g723_1_normalize_bits(max, 15);
388 for (i = 0; i < LPC_ORDER; i++) {
389 weight[i] <<= shift;
390 }
391
392 /* Compute the VQ target vector */
393 for (i = 0; i < LPC_ORDER; i++) {
394 lsp[i] -= dc_lsp[i] +
395 (((prev_lsp[i] - dc_lsp[i]) * 12288 + (1 << 14)) >> 15);
396 }
397
398 get_index(0, 0, 3);
399 get_index(1, 3, 3);
400 get_index(2, 6, 4);
401 }
402
403 /**
404 * Perform IIR filtering.
405 *
406 * @param fir_coef FIR coefficients
407 * @param iir_coef IIR coefficients
408 * @param src source vector
409 * @param dest destination vector
410 */
iir_filter(int16_t *fir_coef, int16_t *iir_coef, int16_t *src, int16_t *dest)411 static void iir_filter(int16_t *fir_coef, int16_t *iir_coef,
412 int16_t *src, int16_t *dest)
413 {
414 int m, n;
415
416 for (m = 0; m < SUBFRAME_LEN; m++) {
417 int64_t filter = 0;
418 for (n = 1; n <= LPC_ORDER; n++) {
419 filter -= fir_coef[n - 1] * src[m - n] -
420 iir_coef[n - 1] * dest[m - n];
421 }
422
423 dest[m] = av_clipl_int32((src[m] << 16) + (filter << 3) +
424 (1 << 15)) >> 16;
425 }
426 }
427
428 /**
429 * Apply the formant perceptual weighting filter.
430 *
431 * @param flt_coef filter coefficients
432 * @param unq_lpc unquantized lpc vector
433 */
perceptual_filter(G723_1_ChannelContext *p, int16_t *flt_coef, int16_t *unq_lpc, int16_t *buf)434 static void perceptual_filter(G723_1_ChannelContext *p, int16_t *flt_coef,
435 int16_t *unq_lpc, int16_t *buf)
436 {
437 int16_t vector[FRAME_LEN + LPC_ORDER];
438 int i, j, k, l = 0;
439
440 memcpy(buf, p->iir_mem, sizeof(int16_t) * LPC_ORDER);
441 memcpy(vector, p->fir_mem, sizeof(int16_t) * LPC_ORDER);
442 memcpy(vector + LPC_ORDER, buf + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
443
444 for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
445 for (k = 0; k < LPC_ORDER; k++) {
446 flt_coef[k + 2 * l] = (unq_lpc[k + l] * percept_flt_tbl[0][k] +
447 (1 << 14)) >> 15;
448 flt_coef[k + 2 * l + LPC_ORDER] = (unq_lpc[k + l] *
449 percept_flt_tbl[1][k] +
450 (1 << 14)) >> 15;
451 }
452 iir_filter(flt_coef + 2 * l, flt_coef + 2 * l + LPC_ORDER,
453 vector + i, buf + i);
454 l += LPC_ORDER;
455 }
456 memcpy(p->iir_mem, buf + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
457 memcpy(p->fir_mem, vector + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
458 }
459
460 /**
461 * Estimate the open loop pitch period.
462 *
463 * @param buf perceptually weighted speech
464 * @param start estimation is carried out from this position
465 */
estimate_pitch(int16_t *buf, int start)466 static int estimate_pitch(int16_t *buf, int start)
467 {
468 int max_exp = 32;
469 int max_ccr = 0x4000;
470 int max_eng = 0x7fff;
471 int index = PITCH_MIN;
472 int offset = start - PITCH_MIN + 1;
473
474 int ccr, eng, orig_eng, ccr_eng, exp;
475 int diff, temp;
476
477 int i;
478
479 orig_eng = ff_dot_product(buf + offset, buf + offset, HALF_FRAME_LEN);
480
481 for (i = PITCH_MIN; i <= PITCH_MAX - 3; i++) {
482 offset--;
483
484 /* Update energy and compute correlation */
485 orig_eng += buf[offset] * buf[offset] -
486 buf[offset + HALF_FRAME_LEN] * buf[offset + HALF_FRAME_LEN];
487 ccr = ff_dot_product(buf + start, buf + offset, HALF_FRAME_LEN);
488 if (ccr <= 0)
489 continue;
490
491 /* Split into mantissa and exponent to maintain precision */
492 exp = ff_g723_1_normalize_bits(ccr, 31);
493 ccr = av_clipl_int32((int64_t) (ccr << exp) + (1 << 15)) >> 16;
494 exp <<= 1;
495 ccr *= ccr;
496 temp = ff_g723_1_normalize_bits(ccr, 31);
497 ccr = ccr << temp >> 16;
498 exp += temp;
499
500 temp = ff_g723_1_normalize_bits(orig_eng, 31);
501 eng = av_clipl_int32((int64_t) (orig_eng << temp) + (1 << 15)) >> 16;
502 exp -= temp;
503
504 if (ccr >= eng) {
505 exp--;
506 ccr >>= 1;
507 }
508 if (exp > max_exp)
509 continue;
510
511 if (exp + 1 < max_exp)
512 goto update;
513
514 /* Equalize exponents before comparison */
515 if (exp + 1 == max_exp)
516 temp = max_ccr >> 1;
517 else
518 temp = max_ccr;
519 ccr_eng = ccr * max_eng;
520 diff = ccr_eng - eng * temp;
521 if (diff > 0 && (i - index < PITCH_MIN || diff > ccr_eng >> 2)) {
522 update:
523 index = i;
524 max_exp = exp;
525 max_ccr = ccr;
526 max_eng = eng;
527 }
528 }
529 return index;
530 }
531
532 /**
533 * Compute harmonic noise filter parameters.
534 *
535 * @param buf perceptually weighted speech
536 * @param pitch_lag open loop pitch period
537 * @param hf harmonic filter parameters
538 */
comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag, HFParam *hf)539 static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag, HFParam *hf)
540 {
541 int ccr, eng, max_ccr, max_eng;
542 int exp, max, diff;
543 int energy[15];
544 int i, j;
545
546 for (i = 0, j = pitch_lag - 3; j <= pitch_lag + 3; i++, j++) {
547 /* Compute residual energy */
548 energy[i << 1] = ff_dot_product(buf - j, buf - j, SUBFRAME_LEN);
549 /* Compute correlation */
550 energy[(i << 1) + 1] = ff_dot_product(buf, buf - j, SUBFRAME_LEN);
551 }
552
553 /* Compute target energy */
554 energy[14] = ff_dot_product(buf, buf, SUBFRAME_LEN);
555
556 /* Normalize */
557 max = 0;
558 for (i = 0; i < 15; i++)
559 max = FFMAX(max, FFABS(energy[i]));
560
561 exp = ff_g723_1_normalize_bits(max, 31);
562 for (i = 0; i < 15; i++) {
563 energy[i] = av_clipl_int32((int64_t)(energy[i] << exp) +
564 (1 << 15)) >> 16;
565 }
566
567 hf->index = -1;
568 hf->gain = 0;
569 max_ccr = 1;
570 max_eng = 0x7fff;
571
572 for (i = 0; i <= 6; i++) {
573 eng = energy[i << 1];
574 ccr = energy[(i << 1) + 1];
575
576 if (ccr <= 0)
577 continue;
578
579 ccr = (ccr * ccr + (1 << 14)) >> 15;
580 diff = ccr * max_eng - eng * max_ccr;
581 if (diff > 0) {
582 max_ccr = ccr;
583 max_eng = eng;
584 hf->index = i;
585 }
586 }
587
588 if (hf->index == -1) {
589 hf->index = pitch_lag;
590 return;
591 }
592
593 eng = energy[14] * max_eng;
594 eng = (eng >> 2) + (eng >> 3);
595 ccr = energy[(hf->index << 1) + 1] * energy[(hf->index << 1) + 1];
596 if (eng < ccr) {
597 eng = energy[(hf->index << 1) + 1];
598
599 if (eng >= max_eng)
600 hf->gain = 0x2800;
601 else
602 hf->gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15;
603 }
604 hf->index += pitch_lag - 3;
605 }
606
607 /**
608 * Apply the harmonic noise shaping filter.
609 *
610 * @param hf filter parameters
611 */
harmonic_filter(HFParam *hf, const int16_t *src, int16_t *dest)612 static void harmonic_filter(HFParam *hf, const int16_t *src, int16_t *dest)
613 {
614 int i;
615
616 for (i = 0; i < SUBFRAME_LEN; i++) {
617 int64_t temp = hf->gain * src[i - hf->index] << 1;
618 dest[i] = av_clipl_int32((src[i] << 16) - temp + (1 << 15)) >> 16;
619 }
620 }
621
harmonic_noise_sub(HFParam *hf, const int16_t *src, int16_t *dest)622 static void harmonic_noise_sub(HFParam *hf, const int16_t *src, int16_t *dest)
623 {
624 int i;
625 for (i = 0; i < SUBFRAME_LEN; i++) {
626 int64_t temp = hf->gain * src[i - hf->index] << 1;
627 dest[i] = av_clipl_int32(((dest[i] - src[i]) << 16) + temp +
628 (1 << 15)) >> 16;
629 }
630 }
631
632 /**
633 * Combined synthesis and formant perceptual weighting filer.
634 *
635 * @param qnt_lpc quantized lpc coefficients
636 * @param perf_lpc perceptual filter coefficients
637 * @param perf_fir perceptual filter fir memory
638 * @param perf_iir perceptual filter iir memory
639 * @param scale the filter output will be scaled by 2^scale
640 */
synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc, int16_t *perf_fir, int16_t *perf_iir, const int16_t *src, int16_t *dest, int scale)641 static void synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc,
642 int16_t *perf_fir, int16_t *perf_iir,
643 const int16_t *src, int16_t *dest, int scale)
644 {
645 int i, j;
646 int16_t buf_16[SUBFRAME_LEN + LPC_ORDER];
647 int64_t buf[SUBFRAME_LEN];
648
649 int16_t *bptr_16 = buf_16 + LPC_ORDER;
650
651 memcpy(buf_16, perf_fir, sizeof(int16_t) * LPC_ORDER);
652 memcpy(dest - LPC_ORDER, perf_iir, sizeof(int16_t) * LPC_ORDER);
653
654 for (i = 0; i < SUBFRAME_LEN; i++) {
655 int64_t temp = 0;
656 for (j = 1; j <= LPC_ORDER; j++)
657 temp -= qnt_lpc[j - 1] * bptr_16[i - j];
658
659 buf[i] = (src[i] << 15) + (temp << 3);
660 bptr_16[i] = av_clipl_int32(buf[i] + (1 << 15)) >> 16;
661 }
662
663 for (i = 0; i < SUBFRAME_LEN; i++) {
664 int64_t fir = 0, iir = 0;
665 for (j = 1; j <= LPC_ORDER; j++) {
666 fir -= perf_lpc[j - 1] * bptr_16[i - j];
667 iir += perf_lpc[j + LPC_ORDER - 1] * dest[i - j];
668 }
669 dest[i] = av_clipl_int32(((buf[i] + (fir << 3)) << scale) + (iir << 3) +
670 (1 << 15)) >> 16;
671 }
672 memcpy(perf_fir, buf_16 + SUBFRAME_LEN, sizeof(int16_t) * LPC_ORDER);
673 memcpy(perf_iir, dest + SUBFRAME_LEN - LPC_ORDER,
674 sizeof(int16_t) * LPC_ORDER);
675 }
676
677 /**
678 * Compute the adaptive codebook contribution.
679 *
680 * @param buf input signal
681 * @param index the current subframe index
682 */
acb_search(G723_1_ChannelContext *p, int16_t *residual, int16_t *impulse_resp, const int16_t *buf, int index)683 static void acb_search(G723_1_ChannelContext *p, int16_t *residual,
684 int16_t *impulse_resp, const int16_t *buf,
685 int index)
686 {
687 int16_t flt_buf[PITCH_ORDER][SUBFRAME_LEN];
688
689 const int16_t *cb_tbl = ff_g723_1_adaptive_cb_gain85;
690
691 int ccr_buf[PITCH_ORDER * SUBFRAMES << 2];
692
693 int pitch_lag = p->pitch_lag[index >> 1];
694 int acb_lag = 1;
695 int acb_gain = 0;
696 int odd_frame = index & 1;
697 int iter = 3 + odd_frame;
698 int count = 0;
699 int tbl_size = 85;
700
701 int i, j, k, l, max;
702 int64_t temp;
703
704 if (!odd_frame) {
705 if (pitch_lag == PITCH_MIN)
706 pitch_lag++;
707 else
708 pitch_lag = FFMIN(pitch_lag, PITCH_MAX - 5);
709 }
710
711 for (i = 0; i < iter; i++) {
712 ff_g723_1_get_residual(residual, p->prev_excitation, pitch_lag + i - 1);
713
714 for (j = 0; j < SUBFRAME_LEN; j++) {
715 temp = 0;
716 for (k = 0; k <= j; k++)
717 temp += residual[PITCH_ORDER - 1 + k] * impulse_resp[j - k];
718 flt_buf[PITCH_ORDER - 1][j] = av_clipl_int32((temp << 1) +
719 (1 << 15)) >> 16;
720 }
721
722 for (j = PITCH_ORDER - 2; j >= 0; j--) {
723 flt_buf[j][0] = ((residual[j] << 13) + (1 << 14)) >> 15;
724 for (k = 1; k < SUBFRAME_LEN; k++) {
725 temp = (flt_buf[j + 1][k - 1] << 15) +
726 residual[j] * impulse_resp[k];
727 flt_buf[j][k] = av_clipl_int32((temp << 1) + (1 << 15)) >> 16;
728 }
729 }
730
731 /* Compute crosscorrelation with the signal */
732 for (j = 0; j < PITCH_ORDER; j++) {
733 temp = ff_dot_product(buf, flt_buf[j], SUBFRAME_LEN);
734 ccr_buf[count++] = av_clipl_int32(temp << 1);
735 }
736
737 /* Compute energies */
738 for (j = 0; j < PITCH_ORDER; j++) {
739 ccr_buf[count++] = ff_g723_1_dot_product(flt_buf[j], flt_buf[j],
740 SUBFRAME_LEN);
741 }
742
743 for (j = 1; j < PITCH_ORDER; j++) {
744 for (k = 0; k < j; k++) {
745 temp = ff_dot_product(flt_buf[j], flt_buf[k], SUBFRAME_LEN);
746 ccr_buf[count++] = av_clipl_int32(temp << 2);
747 }
748 }
749 }
750
751 /* Normalize and shorten */
752 max = 0;
753 for (i = 0; i < 20 * iter; i++)
754 max = FFMAX(max, FFABS(ccr_buf[i]));
755
756 temp = ff_g723_1_normalize_bits(max, 31);
757
758 for (i = 0; i < 20 * iter; i++)
759 ccr_buf[i] = av_clipl_int32((int64_t) (ccr_buf[i] << temp) +
760 (1 << 15)) >> 16;
761
762 max = 0;
763 for (i = 0; i < iter; i++) {
764 /* Select quantization table */
765 if (!odd_frame && pitch_lag + i - 1 >= SUBFRAME_LEN - 2 ||
766 odd_frame && pitch_lag >= SUBFRAME_LEN - 2) {
767 cb_tbl = ff_g723_1_adaptive_cb_gain170;
768 tbl_size = 170;
769 }
770
771 for (j = 0, k = 0; j < tbl_size; j++, k += 20) {
772 temp = 0;
773 for (l = 0; l < 20; l++)
774 temp += ccr_buf[20 * i + l] * cb_tbl[k + l];
775 temp = av_clipl_int32(temp);
776
777 if (temp > max) {
778 max = temp;
779 acb_gain = j;
780 acb_lag = i;
781 }
782 }
783 }
784
785 if (!odd_frame) {
786 pitch_lag += acb_lag - 1;
787 acb_lag = 1;
788 }
789
790 p->pitch_lag[index >> 1] = pitch_lag;
791 p->subframe[index].ad_cb_lag = acb_lag;
792 p->subframe[index].ad_cb_gain = acb_gain;
793 }
794
795 /**
796 * Subtract the adaptive codebook contribution from the input
797 * to obtain the residual.
798 *
799 * @param buf target vector
800 */
sub_acb_contrib(const int16_t *residual, const int16_t *impulse_resp, int16_t *buf)801 static void sub_acb_contrib(const int16_t *residual, const int16_t *impulse_resp,
802 int16_t *buf)
803 {
804 int i, j;
805 /* Subtract adaptive CB contribution to obtain the residual */
806 for (i = 0; i < SUBFRAME_LEN; i++) {
807 int64_t temp = buf[i] << 14;
808 for (j = 0; j <= i; j++)
809 temp -= residual[j] * impulse_resp[i - j];
810
811 buf[i] = av_clipl_int32((temp << 2) + (1 << 15)) >> 16;
812 }
813 }
814
815 /**
816 * Quantize the residual signal using the fixed codebook (MP-MLQ).
817 *
818 * @param optim optimized fixed codebook parameters
819 * @param buf excitation vector
820 */
get_fcb_param(FCBParam *optim, int16_t *impulse_resp, int16_t *buf, int pulse_cnt, int pitch_lag)821 static void get_fcb_param(FCBParam *optim, int16_t *impulse_resp,
822 int16_t *buf, int pulse_cnt, int pitch_lag)
823 {
824 FCBParam param;
825 int16_t impulse_r[SUBFRAME_LEN];
826 int16_t temp_corr[SUBFRAME_LEN];
827 int16_t impulse_corr[SUBFRAME_LEN];
828
829 int ccr1[SUBFRAME_LEN];
830 int ccr2[SUBFRAME_LEN];
831 int amp, err, max, max_amp_index, min, scale, i, j, k, l;
832
833 int64_t temp;
834
835 /* Update impulse response */
836 memcpy(impulse_r, impulse_resp, sizeof(int16_t) * SUBFRAME_LEN);
837 param.dirac_train = 0;
838 if (pitch_lag < SUBFRAME_LEN - 2) {
839 param.dirac_train = 1;
840 ff_g723_1_gen_dirac_train(impulse_r, pitch_lag);
841 }
842
843 for (i = 0; i < SUBFRAME_LEN; i++)
844 temp_corr[i] = impulse_r[i] >> 1;
845
846 /* Compute impulse response autocorrelation */
847 temp = ff_g723_1_dot_product(temp_corr, temp_corr, SUBFRAME_LEN);
848
849 scale = ff_g723_1_normalize_bits(temp, 31);
850 impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
851
852 for (i = 1; i < SUBFRAME_LEN; i++) {
853 temp = ff_g723_1_dot_product(temp_corr + i, temp_corr,
854 SUBFRAME_LEN - i);
855 impulse_corr[i] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
856 }
857
858 /* Compute crosscorrelation of impulse response with residual signal */
859 scale -= 4;
860 for (i = 0; i < SUBFRAME_LEN; i++) {
861 temp = ff_g723_1_dot_product(buf + i, impulse_r, SUBFRAME_LEN - i);
862 if (scale < 0)
863 ccr1[i] = temp >> -scale;
864 else
865 ccr1[i] = av_clipl_int32(temp << scale);
866 }
867
868 /* Search loop */
869 for (i = 0; i < GRID_SIZE; i++) {
870 /* Maximize the crosscorrelation */
871 max = 0;
872 for (j = i; j < SUBFRAME_LEN; j += GRID_SIZE) {
873 temp = FFABS(ccr1[j]);
874 if (temp >= max) {
875 max = temp;
876 param.pulse_pos[0] = j;
877 }
878 }
879
880 /* Quantize the gain (max crosscorrelation/impulse_corr[0]) */
881 amp = max;
882 min = 1 << 30;
883 max_amp_index = GAIN_LEVELS - 2;
884 for (j = max_amp_index; j >= 2; j--) {
885 temp = av_clipl_int32((int64_t) ff_g723_1_fixed_cb_gain[j] *
886 impulse_corr[0] << 1);
887 temp = FFABS(temp - amp);
888 if (temp < min) {
889 min = temp;
890 max_amp_index = j;
891 }
892 }
893
894 max_amp_index--;
895 /* Select additional gain values */
896 for (j = 1; j < 5; j++) {
897 for (k = i; k < SUBFRAME_LEN; k += GRID_SIZE) {
898 temp_corr[k] = 0;
899 ccr2[k] = ccr1[k];
900 }
901 param.amp_index = max_amp_index + j - 2;
902 amp = ff_g723_1_fixed_cb_gain[param.amp_index];
903
904 param.pulse_sign[0] = (ccr2[param.pulse_pos[0]] < 0) ? -amp : amp;
905 temp_corr[param.pulse_pos[0]] = 1;
906
907 for (k = 1; k < pulse_cnt; k++) {
908 max = INT_MIN;
909 for (l = i; l < SUBFRAME_LEN; l += GRID_SIZE) {
910 if (temp_corr[l])
911 continue;
912 temp = impulse_corr[FFABS(l - param.pulse_pos[k - 1])];
913 temp = av_clipl_int32((int64_t) temp *
914 param.pulse_sign[k - 1] << 1);
915 ccr2[l] -= temp;
916 temp = FFABS(ccr2[l]);
917 if (temp > max) {
918 max = temp;
919 param.pulse_pos[k] = l;
920 }
921 }
922
923 param.pulse_sign[k] = (ccr2[param.pulse_pos[k]] < 0) ?
924 -amp : amp;
925 temp_corr[param.pulse_pos[k]] = 1;
926 }
927
928 /* Create the error vector */
929 memset(temp_corr, 0, sizeof(int16_t) * SUBFRAME_LEN);
930
931 for (k = 0; k < pulse_cnt; k++)
932 temp_corr[param.pulse_pos[k]] = param.pulse_sign[k];
933
934 for (k = SUBFRAME_LEN - 1; k >= 0; k--) {
935 temp = 0;
936 for (l = 0; l <= k; l++) {
937 int prod = av_clipl_int32((int64_t) temp_corr[l] *
938 impulse_r[k - l] << 1);
939 temp = av_clipl_int32(temp + prod);
940 }
941 temp_corr[k] = temp << 2 >> 16;
942 }
943
944 /* Compute square of error */
945 err = 0;
946 for (k = 0; k < SUBFRAME_LEN; k++) {
947 int64_t prod;
948 prod = av_clipl_int32((int64_t) buf[k] * temp_corr[k] << 1);
949 err = av_clipl_int32(err - prod);
950 prod = av_clipl_int32((int64_t) temp_corr[k] * temp_corr[k]);
951 err = av_clipl_int32(err + prod);
952 }
953
954 /* Minimize */
955 if (err < optim->min_err) {
956 optim->min_err = err;
957 optim->grid_index = i;
958 optim->amp_index = param.amp_index;
959 optim->dirac_train = param.dirac_train;
960
961 for (k = 0; k < pulse_cnt; k++) {
962 optim->pulse_sign[k] = param.pulse_sign[k];
963 optim->pulse_pos[k] = param.pulse_pos[k];
964 }
965 }
966 }
967 }
968 }
969
970 /**
971 * Encode the pulse position and gain of the current subframe.
972 *
973 * @param optim optimized fixed CB parameters
974 * @param buf excitation vector
975 */
pack_fcb_param(G723_1_Subframe *subfrm, FCBParam *optim, int16_t *buf, int pulse_cnt)976 static void pack_fcb_param(G723_1_Subframe *subfrm, FCBParam *optim,
977 int16_t *buf, int pulse_cnt)
978 {
979 int i, j;
980
981 j = PULSE_MAX - pulse_cnt;
982
983 subfrm->pulse_sign = 0;
984 subfrm->pulse_pos = 0;
985
986 for (i = 0; i < SUBFRAME_LEN >> 1; i++) {
987 int val = buf[optim->grid_index + (i << 1)];
988 if (!val) {
989 subfrm->pulse_pos += ff_g723_1_combinatorial_table[j][i];
990 } else {
991 subfrm->pulse_sign <<= 1;
992 if (val < 0)
993 subfrm->pulse_sign++;
994 j++;
995
996 if (j == PULSE_MAX)
997 break;
998 }
999 }
1000 subfrm->amp_index = optim->amp_index;
1001 subfrm->grid_index = optim->grid_index;
1002 subfrm->dirac_train = optim->dirac_train;
1003 }
1004
1005 /**
1006 * Compute the fixed codebook excitation.
1007 *
1008 * @param buf target vector
1009 * @param impulse_resp impulse response of the combined filter
1010 */
fcb_search(G723_1_ChannelContext *p, int16_t *impulse_resp, int16_t *buf, int index)1011 static void fcb_search(G723_1_ChannelContext *p, int16_t *impulse_resp,
1012 int16_t *buf, int index)
1013 {
1014 FCBParam optim;
1015 int pulse_cnt = pulses[index];
1016 int i;
1017
1018 optim.min_err = 1 << 30;
1019 get_fcb_param(&optim, impulse_resp, buf, pulse_cnt, SUBFRAME_LEN);
1020
1021 if (p->pitch_lag[index >> 1] < SUBFRAME_LEN - 2) {
1022 get_fcb_param(&optim, impulse_resp, buf, pulse_cnt,
1023 p->pitch_lag[index >> 1]);
1024 }
1025
1026 /* Reconstruct the excitation */
1027 memset(buf, 0, sizeof(int16_t) * SUBFRAME_LEN);
1028 for (i = 0; i < pulse_cnt; i++)
1029 buf[optim.pulse_pos[i]] = optim.pulse_sign[i];
1030
1031 pack_fcb_param(&p->subframe[index], &optim, buf, pulse_cnt);
1032
1033 if (optim.dirac_train)
1034 ff_g723_1_gen_dirac_train(buf, p->pitch_lag[index >> 1]);
1035 }
1036
1037 /**
1038 * Pack the frame parameters into output bitstream.
1039 *
1040 * @param frame output buffer
1041 * @param size size of the buffer
1042 */
pack_bitstream(G723_1_ChannelContext *p, AVPacket *avpkt, int info_bits)1043 static void pack_bitstream(G723_1_ChannelContext *p, AVPacket *avpkt, int info_bits)
1044 {
1045 PutBitContext pb;
1046 int i, temp;
1047
1048 init_put_bits(&pb, avpkt->data, avpkt->size);
1049
1050 put_bits(&pb, 2, info_bits);
1051
1052 put_bits(&pb, 8, p->lsp_index[2]);
1053 put_bits(&pb, 8, p->lsp_index[1]);
1054 put_bits(&pb, 8, p->lsp_index[0]);
1055
1056 put_bits(&pb, 7, p->pitch_lag[0] - PITCH_MIN);
1057 put_bits(&pb, 2, p->subframe[1].ad_cb_lag);
1058 put_bits(&pb, 7, p->pitch_lag[1] - PITCH_MIN);
1059 put_bits(&pb, 2, p->subframe[3].ad_cb_lag);
1060
1061 /* Write 12 bit combined gain */
1062 for (i = 0; i < SUBFRAMES; i++) {
1063 temp = p->subframe[i].ad_cb_gain * GAIN_LEVELS +
1064 p->subframe[i].amp_index;
1065 if (p->cur_rate == RATE_6300)
1066 temp += p->subframe[i].dirac_train << 11;
1067 put_bits(&pb, 12, temp);
1068 }
1069
1070 put_bits(&pb, 1, p->subframe[0].grid_index);
1071 put_bits(&pb, 1, p->subframe[1].grid_index);
1072 put_bits(&pb, 1, p->subframe[2].grid_index);
1073 put_bits(&pb, 1, p->subframe[3].grid_index);
1074
1075 if (p->cur_rate == RATE_6300) {
1076 put_bits(&pb, 1, 0); /* reserved bit */
1077
1078 /* Write 13 bit combined position index */
1079 temp = (p->subframe[0].pulse_pos >> 16) * 810 +
1080 (p->subframe[1].pulse_pos >> 14) * 90 +
1081 (p->subframe[2].pulse_pos >> 16) * 9 +
1082 (p->subframe[3].pulse_pos >> 14);
1083 put_bits(&pb, 13, temp);
1084
1085 put_bits(&pb, 16, p->subframe[0].pulse_pos & 0xffff);
1086 put_bits(&pb, 14, p->subframe[1].pulse_pos & 0x3fff);
1087 put_bits(&pb, 16, p->subframe[2].pulse_pos & 0xffff);
1088 put_bits(&pb, 14, p->subframe[3].pulse_pos & 0x3fff);
1089
1090 put_bits(&pb, 6, p->subframe[0].pulse_sign);
1091 put_bits(&pb, 5, p->subframe[1].pulse_sign);
1092 put_bits(&pb, 6, p->subframe[2].pulse_sign);
1093 put_bits(&pb, 5, p->subframe[3].pulse_sign);
1094 }
1095
1096 flush_put_bits(&pb);
1097 }
1098
g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)1099 static int g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1100 const AVFrame *frame, int *got_packet_ptr)
1101 {
1102 G723_1_Context *s = avctx->priv_data;
1103 G723_1_ChannelContext *p = &s->ch[0];
1104 int16_t unq_lpc[LPC_ORDER * SUBFRAMES];
1105 int16_t qnt_lpc[LPC_ORDER * SUBFRAMES];
1106 int16_t cur_lsp[LPC_ORDER];
1107 int16_t weighted_lpc[LPC_ORDER * SUBFRAMES << 1];
1108 int16_t vector[FRAME_LEN + PITCH_MAX];
1109 int offset, ret, i, j, info_bits = 0;
1110 int16_t *in, *start;
1111 HFParam hf[4];
1112
1113 /* duplicate input */
1114 start = in = av_memdup(frame->data[0], frame->nb_samples * sizeof(int16_t));
1115 if (!in)
1116 return AVERROR(ENOMEM);
1117
1118 highpass_filter(in, &p->hpf_fir_mem, &p->hpf_iir_mem);
1119
1120 memcpy(vector, p->prev_data, HALF_FRAME_LEN * sizeof(int16_t));
1121 memcpy(vector + HALF_FRAME_LEN, in, FRAME_LEN * sizeof(int16_t));
1122
1123 comp_lpc_coeff(vector, unq_lpc);
1124 lpc2lsp(&unq_lpc[LPC_ORDER * 3], p->prev_lsp, cur_lsp);
1125 lsp_quantize(p->lsp_index, cur_lsp, p->prev_lsp);
1126
1127 /* Update memory */
1128 memcpy(vector + LPC_ORDER, p->prev_data + SUBFRAME_LEN,
1129 sizeof(int16_t) * SUBFRAME_LEN);
1130 memcpy(vector + LPC_ORDER + SUBFRAME_LEN, in,
1131 sizeof(int16_t) * (HALF_FRAME_LEN + SUBFRAME_LEN));
1132 memcpy(p->prev_data, in + HALF_FRAME_LEN,
1133 sizeof(int16_t) * HALF_FRAME_LEN);
1134 memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
1135
1136 perceptual_filter(p, weighted_lpc, unq_lpc, vector);
1137
1138 memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
1139 memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
1140 memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
1141
1142 ff_g723_1_scale_vector(vector, vector, FRAME_LEN + PITCH_MAX);
1143
1144 p->pitch_lag[0] = estimate_pitch(vector, PITCH_MAX);
1145 p->pitch_lag[1] = estimate_pitch(vector, PITCH_MAX + HALF_FRAME_LEN);
1146
1147 for (i = PITCH_MAX, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1148 comp_harmonic_coeff(vector + i, p->pitch_lag[j >> 1], hf + j);
1149
1150 memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
1151 memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
1152 memcpy(p->prev_weight_sig, vector + FRAME_LEN, sizeof(int16_t) * PITCH_MAX);
1153
1154 for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1155 harmonic_filter(hf + j, vector + PITCH_MAX + i, in + i);
1156
1157 ff_g723_1_inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, 0);
1158 ff_g723_1_lsp_interpolate(qnt_lpc, cur_lsp, p->prev_lsp);
1159
1160 memcpy(p->prev_lsp, cur_lsp, sizeof(int16_t) * LPC_ORDER);
1161
1162 offset = 0;
1163 for (i = 0; i < SUBFRAMES; i++) {
1164 int16_t impulse_resp[SUBFRAME_LEN];
1165 int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
1166 int16_t flt_in[SUBFRAME_LEN];
1167 int16_t zero[LPC_ORDER], fir[LPC_ORDER], iir[LPC_ORDER];
1168
1169 /**
1170 * Compute the combined impulse response of the synthesis filter,
1171 * formant perceptual weighting filter and harmonic noise shaping filter
1172 */
1173 memset(zero, 0, sizeof(int16_t) * LPC_ORDER);
1174 memset(vector, 0, sizeof(int16_t) * PITCH_MAX);
1175 memset(flt_in, 0, sizeof(int16_t) * SUBFRAME_LEN);
1176
1177 flt_in[0] = 1 << 13; /* Unit impulse */
1178 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
1179 zero, zero, flt_in, vector + PITCH_MAX, 1);
1180 harmonic_filter(hf + i, vector + PITCH_MAX, impulse_resp);
1181
1182 /* Compute the combined zero input response */
1183 flt_in[0] = 0;
1184 memcpy(fir, p->perf_fir_mem, sizeof(int16_t) * LPC_ORDER);
1185 memcpy(iir, p->perf_iir_mem, sizeof(int16_t) * LPC_ORDER);
1186
1187 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
1188 fir, iir, flt_in, vector + PITCH_MAX, 0);
1189 memcpy(vector, p->harmonic_mem, sizeof(int16_t) * PITCH_MAX);
1190 harmonic_noise_sub(hf + i, vector + PITCH_MAX, in);
1191
1192 acb_search(p, residual, impulse_resp, in, i);
1193 ff_g723_1_gen_acb_excitation(residual, p->prev_excitation,
1194 p->pitch_lag[i >> 1], &p->subframe[i],
1195 p->cur_rate);
1196 sub_acb_contrib(residual, impulse_resp, in);
1197
1198 fcb_search(p, impulse_resp, in, i);
1199
1200 /* Reconstruct the excitation */
1201 ff_g723_1_gen_acb_excitation(impulse_resp, p->prev_excitation,
1202 p->pitch_lag[i >> 1], &p->subframe[i],
1203 RATE_6300);
1204
1205 memmove(p->prev_excitation, p->prev_excitation + SUBFRAME_LEN,
1206 sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
1207 for (j = 0; j < SUBFRAME_LEN; j++)
1208 in[j] = av_clip_int16((in[j] << 1) + impulse_resp[j]);
1209 memcpy(p->prev_excitation + PITCH_MAX - SUBFRAME_LEN, in,
1210 sizeof(int16_t) * SUBFRAME_LEN);
1211
1212 /* Update filter memories */
1213 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
1214 p->perf_fir_mem, p->perf_iir_mem,
1215 in, vector + PITCH_MAX, 0);
1216 memmove(p->harmonic_mem, p->harmonic_mem + SUBFRAME_LEN,
1217 sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
1218 memcpy(p->harmonic_mem + PITCH_MAX - SUBFRAME_LEN, vector + PITCH_MAX,
1219 sizeof(int16_t) * SUBFRAME_LEN);
1220
1221 in += SUBFRAME_LEN;
1222 offset += LPC_ORDER;
1223 }
1224
1225 av_free(start);
1226
1227 ret = ff_get_encode_buffer(avctx, avpkt, frame_size[info_bits], 0);
1228 if (ret < 0)
1229 return ret;
1230
1231 *got_packet_ptr = 1;
1232 pack_bitstream(p, avpkt, info_bits);
1233 return 0;
1234 }
1235
1236 static const FFCodecDefault defaults[] = {
1237 { "b", "6300" },
1238 { NULL },
1239 };
1240
1241 const FFCodec ff_g723_1_encoder = {
1242 .p.name = "g723_1",
1243 .p.long_name = NULL_IF_CONFIG_SMALL("G.723.1"),
1244 .p.type = AVMEDIA_TYPE_AUDIO,
1245 .p.id = AV_CODEC_ID_G723_1,
1246 .p.capabilities = AV_CODEC_CAP_DR1,
1247 .priv_data_size = sizeof(G723_1_Context),
1248 .init = g723_1_encode_init,
1249 FF_CODEC_ENCODE_CB(g723_1_encode_frame),
1250 .defaults = defaults,
1251 .p.sample_fmts = (const enum AVSampleFormat[]) {
1252 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
1253 },
1254 .p.ch_layouts = (const AVChannelLayout[]){
1255 AV_CHANNEL_LAYOUT_MONO, { 0 }
1256 },
1257 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
1258 };
1259