2 * G.723.1 compatible encoder
3 * Copyright (c) Mohamed Naufal <naufal22@gmail.com>
5 * This file is part of FFmpeg.
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.
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.
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
24 * G.723.1 compatible encoder
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/common.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/opt.h"
36 #include "celp_math.h"
40 #define BITSTREAM_WRITER_LE
43 static av_cold int g723_1_encode_init(AVCodecContext *avctx)
45 G723_1_Context *p = avctx->priv_data;
47 if (avctx->sample_rate != 8000) {
48 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
49 return AVERROR(EINVAL);
52 if (avctx->channels != 1) {
53 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
54 return AVERROR(EINVAL);
57 if (avctx->bit_rate == 6300) {
58 p->cur_rate = RATE_6300;
59 } else if (avctx->bit_rate == 5300) {
60 av_log(avctx, AV_LOG_ERROR, "Use bitrate 6300 instead of 5300.\n");
61 avpriv_report_missing_feature(avctx, "Bitrate 5300");
62 return AVERROR_PATCHWELCOME;
64 av_log(avctx, AV_LOG_ERROR, "Bitrate not supported, use 6300\n");
65 return AVERROR(EINVAL);
67 avctx->frame_size = 240;
68 memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(int16_t));
74 * Remove DC component from the input signal.
76 * @param buf input signal
77 * @param fir zero memory
78 * @param iir pole memory
80 static void highpass_filter(int16_t *buf, int16_t *fir, int *iir)
83 for (i = 0; i < FRAME_LEN; i++) {
84 *iir = (buf[i] << 15) + ((-*fir) << 15) + MULL2(*iir, 0x7f00);
86 buf[i] = av_clipl_int32((int64_t)*iir + (1 << 15)) >> 16;
91 * Estimate autocorrelation of the input vector.
93 * @param buf input buffer
94 * @param autocorr autocorrelation coefficients vector
96 static void comp_autocorr(int16_t *buf, int16_t *autocorr)
99 int16_t vector[LPC_FRAME];
101 ff_g723_1_scale_vector(vector, buf, LPC_FRAME);
103 /* Apply the Hamming window */
104 for (i = 0; i < LPC_FRAME; i++)
105 vector[i] = (vector[i] * hamming_window[i] + (1 << 14)) >> 15;
107 /* Compute the first autocorrelation coefficient */
108 temp = ff_dot_product(vector, vector, LPC_FRAME);
110 /* Apply a white noise correlation factor of (1025/1024) */
114 scale = ff_g723_1_normalize_bits(temp, 31);
115 autocorr[0] = av_clipl_int32((int64_t) (temp << scale) +
118 /* Compute the remaining coefficients */
120 memset(autocorr + 1, 0, LPC_ORDER * sizeof(int16_t));
122 for (i = 1; i <= LPC_ORDER; i++) {
123 temp = ff_dot_product(vector, vector + i, LPC_FRAME - i);
124 temp = MULL2((temp << scale), binomial_window[i - 1]);
125 autocorr[i] = av_clipl_int32((int64_t) temp + (1 << 15)) >> 16;
131 * Use Levinson-Durbin recursion to compute LPC coefficients from
132 * autocorrelation values.
134 * @param lpc LPC coefficients vector
135 * @param autocorr autocorrelation coefficients vector
136 * @param error prediction error
138 static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)
140 int16_t vector[LPC_ORDER];
141 int16_t partial_corr;
144 memset(lpc, 0, LPC_ORDER * sizeof(int16_t));
146 for (i = 0; i < LPC_ORDER; i++) {
147 /* Compute the partial correlation coefficient */
149 for (j = 0; j < i; j++)
150 temp -= lpc[j] * autocorr[i - j - 1];
151 temp = ((autocorr[i] << 13) + temp) << 3;
153 if (FFABS(temp) >= (error << 16))
156 partial_corr = temp / (error << 1);
158 lpc[i] = av_clipl_int32((int64_t) (partial_corr << 14) +
161 /* Update the prediction error */
162 temp = MULL2(temp, partial_corr);
163 error = av_clipl_int32((int64_t) (error << 16) - temp +
166 memcpy(vector, lpc, i * sizeof(int16_t));
167 for (j = 0; j < i; j++) {
168 temp = partial_corr * vector[i - j - 1] << 1;
169 lpc[j] = av_clipl_int32((int64_t) (lpc[j] << 16) - temp +
176 * Calculate LPC coefficients for the current frame.
178 * @param buf current frame
179 * @param prev_data 2 trailing subframes of the previous frame
180 * @param lpc LPC coefficients vector
182 static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
184 int16_t autocorr[(LPC_ORDER + 1) * SUBFRAMES];
185 int16_t *autocorr_ptr = autocorr;
186 int16_t *lpc_ptr = lpc;
189 for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
190 comp_autocorr(buf + i, autocorr_ptr);
191 levinson_durbin(lpc_ptr, autocorr_ptr + 1, autocorr_ptr[0]);
193 lpc_ptr += LPC_ORDER;
194 autocorr_ptr += LPC_ORDER + 1;
198 static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
200 int f[LPC_ORDER + 2]; ///< coefficients of the sum and difference
201 ///< polynomials (F1, F2) ordered as
202 ///< f1[0], f2[0], ...., f1[5], f2[5]
204 int max, shift, cur_val, prev_val, count, p;
208 /* Initialize f1[0] and f2[0] to 1 in Q25 */
209 for (i = 0; i < LPC_ORDER; i++)
210 lsp[i] = (lpc[i] * bandwidth_expand[i] + (1 << 14)) >> 15;
212 /* Apply bandwidth expansion on the LPC coefficients */
213 f[0] = f[1] = 1 << 25;
215 /* Compute the remaining coefficients */
216 for (i = 0; i < LPC_ORDER / 2; i++) {
218 f[2 * i + 2] = -f[2 * i] - ((lsp[i] + lsp[LPC_ORDER - 1 - i]) << 12);
220 f[2 * i + 3] = f[2 * i + 1] - ((lsp[i] - lsp[LPC_ORDER - 1 - i]) << 12);
223 /* Divide f1[5] and f2[5] by 2 for use in polynomial evaluation */
225 f[LPC_ORDER + 1] >>= 1;
227 /* Normalize and shorten */
229 for (i = 1; i < LPC_ORDER + 2; i++)
230 max = FFMAX(max, FFABS(f[i]));
232 shift = ff_g723_1_normalize_bits(max, 31);
234 for (i = 0; i < LPC_ORDER + 2; i++)
235 f[i] = av_clipl_int32((int64_t) (f[i] << shift) + (1 << 15)) >> 16;
238 * Evaluate F1 and F2 at uniform intervals of pi/256 along the
239 * unit circle and check for zero crossings.
243 for (i = 0; i <= LPC_ORDER / 2; i++)
244 temp += f[2 * i] * cos_tab[0];
245 prev_val = av_clipl_int32(temp << 1);
247 for (i = 1; i < COS_TBL_SIZE / 2; i++) {
250 for (j = 0; j <= LPC_ORDER / 2; j++)
251 temp += f[LPC_ORDER - 2 * j + p] * cos_tab[i * j % COS_TBL_SIZE];
252 cur_val = av_clipl_int32(temp << 1);
254 /* Check for sign change, indicating a zero crossing */
255 if ((cur_val ^ prev_val) < 0) {
256 int abs_cur = FFABS(cur_val);
257 int abs_prev = FFABS(prev_val);
258 int sum = abs_cur + abs_prev;
260 shift = ff_g723_1_normalize_bits(sum, 31);
262 abs_prev = abs_prev << shift >> 8;
263 lsp[count++] = ((i - 1) << 7) + (abs_prev >> 1) / (sum >> 16);
265 if (count == LPC_ORDER)
268 /* Switch between sum and difference polynomials */
273 for (j = 0; j <= LPC_ORDER / 2; j++)
274 temp += f[LPC_ORDER - 2 * j + p] *
275 cos_tab[i * j % COS_TBL_SIZE];
276 cur_val = av_clipl_int32(temp << 1);
281 if (count != LPC_ORDER)
282 memcpy(lsp, prev_lsp, LPC_ORDER * sizeof(int16_t));
286 * Quantize the current LSP subvector.
288 * @param num band number
289 * @param offset offset of the current subvector in an LPC_ORDER vector
290 * @param size size of the current subvector
292 #define get_index(num, offset, size) \
294 int error, max = -1; \
298 for (i = 0; i < LSP_CB_SIZE; i++) { \
299 for (j = 0; j < size; j++){ \
300 temp[j] = (weight[j + (offset)] * lsp_band##num[i][j] + \
303 error = ff_g723_1_dot_product(lsp + (offset), temp, size) << 1; \
304 error -= ff_g723_1_dot_product(lsp_band##num[i], temp, size); \
307 lsp_index[num] = i; \
313 * Vector quantize the LSP frequencies.
315 * @param lsp the current lsp vector
316 * @param prev_lsp the previous lsp vector
318 static void lsp_quantize(uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
320 int16_t weight[LPC_ORDER];
324 /* Calculate the VQ weighting vector */
325 weight[0] = (1 << 20) / (lsp[1] - lsp[0]);
326 weight[LPC_ORDER - 1] = (1 << 20) /
327 (lsp[LPC_ORDER - 1] - lsp[LPC_ORDER - 2]);
329 for (i = 1; i < LPC_ORDER - 1; i++) {
330 min = FFMIN(lsp[i] - lsp[i - 1], lsp[i + 1] - lsp[i]);
332 weight[i] = (1 << 20) / min;
334 weight[i] = INT16_MAX;
339 for (i = 0; i < LPC_ORDER; i++)
340 max = FFMAX(weight[i], max);
342 shift = ff_g723_1_normalize_bits(max, 15);
343 for (i = 0; i < LPC_ORDER; i++) {
347 /* Compute the VQ target vector */
348 for (i = 0; i < LPC_ORDER; i++) {
349 lsp[i] -= dc_lsp[i] +
350 (((prev_lsp[i] - dc_lsp[i]) * 12288 + (1 << 14)) >> 15);
359 * Perform IIR filtering.
361 * @param fir_coef FIR coefficients
362 * @param iir_coef IIR coefficients
363 * @param src source vector
364 * @param dest destination vector
366 static void iir_filter(int16_t *fir_coef, int16_t *iir_coef,
367 int16_t *src, int16_t *dest)
371 for (m = 0; m < SUBFRAME_LEN; m++) {
373 for (n = 1; n <= LPC_ORDER; n++) {
374 filter -= fir_coef[n - 1] * src[m - n] -
375 iir_coef[n - 1] * dest[m - n];
378 dest[m] = av_clipl_int32((src[m] << 16) + (filter << 3) +
384 * Apply the formant perceptual weighting filter.
386 * @param flt_coef filter coefficients
387 * @param unq_lpc unquantized lpc vector
389 static void perceptual_filter(G723_1_Context *p, int16_t *flt_coef,
390 int16_t *unq_lpc, int16_t *buf)
392 int16_t vector[FRAME_LEN + LPC_ORDER];
395 memcpy(buf, p->iir_mem, sizeof(int16_t) * LPC_ORDER);
396 memcpy(vector, p->fir_mem, sizeof(int16_t) * LPC_ORDER);
397 memcpy(vector + LPC_ORDER, buf + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
399 for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
400 for (k = 0; k < LPC_ORDER; k++) {
401 flt_coef[k + 2 * l] = (unq_lpc[k + l] * percept_flt_tbl[0][k] +
403 flt_coef[k + 2 * l + LPC_ORDER] = (unq_lpc[k + l] *
404 percept_flt_tbl[1][k] +
407 iir_filter(flt_coef + 2 * l, flt_coef + 2 * l + LPC_ORDER,
408 vector + i, buf + i);
411 memcpy(p->iir_mem, buf + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
412 memcpy(p->fir_mem, vector + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
416 * Estimate the open loop pitch period.
418 * @param buf perceptually weighted speech
419 * @param start estimation is carried out from this position
421 static int estimate_pitch(int16_t *buf, int start)
424 int max_ccr = 0x4000;
425 int max_eng = 0x7fff;
426 int index = PITCH_MIN;
427 int offset = start - PITCH_MIN + 1;
429 int ccr, eng, orig_eng, ccr_eng, exp;
434 orig_eng = ff_dot_product(buf + offset, buf + offset, HALF_FRAME_LEN);
436 for (i = PITCH_MIN; i <= PITCH_MAX - 3; i++) {
439 /* Update energy and compute correlation */
440 orig_eng += buf[offset] * buf[offset] -
441 buf[offset + HALF_FRAME_LEN] * buf[offset + HALF_FRAME_LEN];
442 ccr = ff_dot_product(buf + start, buf + offset, HALF_FRAME_LEN);
446 /* Split into mantissa and exponent to maintain precision */
447 exp = ff_g723_1_normalize_bits(ccr, 31);
448 ccr = av_clipl_int32((int64_t) (ccr << exp) + (1 << 15)) >> 16;
451 temp = ff_g723_1_normalize_bits(ccr, 31);
452 ccr = ccr << temp >> 16;
455 temp = ff_g723_1_normalize_bits(orig_eng, 31);
456 eng = av_clipl_int32((int64_t) (orig_eng << temp) + (1 << 15)) >> 16;
466 if (exp + 1 < max_exp)
469 /* Equalize exponents before comparison */
470 if (exp + 1 == max_exp)
474 ccr_eng = ccr * max_eng;
475 diff = ccr_eng - eng * temp;
476 if (diff > 0 && (i - index < PITCH_MIN || diff > ccr_eng >> 2)) {
488 * Compute harmonic noise filter parameters.
490 * @param buf perceptually weighted speech
491 * @param pitch_lag open loop pitch period
492 * @param hf harmonic filter parameters
494 static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag, HFParam *hf)
496 int ccr, eng, max_ccr, max_eng;
501 for (i = 0, j = pitch_lag - 3; j <= pitch_lag + 3; i++, j++) {
502 /* Compute residual energy */
503 energy[i << 1] = ff_dot_product(buf - j, buf - j, SUBFRAME_LEN);
504 /* Compute correlation */
505 energy[(i << 1) + 1] = ff_dot_product(buf, buf - j, SUBFRAME_LEN);
508 /* Compute target energy */
509 energy[14] = ff_dot_product(buf, buf, SUBFRAME_LEN);
513 for (i = 0; i < 15; i++)
514 max = FFMAX(max, FFABS(energy[i]));
516 exp = ff_g723_1_normalize_bits(max, 31);
517 for (i = 0; i < 15; i++) {
518 energy[i] = av_clipl_int32((int64_t)(energy[i] << exp) +
527 for (i = 0; i <= 6; i++) {
528 eng = energy[i << 1];
529 ccr = energy[(i << 1) + 1];
534 ccr = (ccr * ccr + (1 << 14)) >> 15;
535 diff = ccr * max_eng - eng * max_ccr;
543 if (hf->index == -1) {
544 hf->index = pitch_lag;
548 eng = energy[14] * max_eng;
549 eng = (eng >> 2) + (eng >> 3);
550 ccr = energy[(hf->index << 1) + 1] * energy[(hf->index << 1) + 1];
552 eng = energy[(hf->index << 1) + 1];
557 hf->gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15;
559 hf->index += pitch_lag - 3;
563 * Apply the harmonic noise shaping filter.
565 * @param hf filter parameters
567 static void harmonic_filter(HFParam *hf, const int16_t *src, int16_t *dest)
571 for (i = 0; i < SUBFRAME_LEN; i++) {
572 int64_t temp = hf->gain * src[i - hf->index] << 1;
573 dest[i] = av_clipl_int32((src[i] << 16) - temp + (1 << 15)) >> 16;
577 static void harmonic_noise_sub(HFParam *hf, const int16_t *src, int16_t *dest)
580 for (i = 0; i < SUBFRAME_LEN; i++) {
581 int64_t temp = hf->gain * src[i - hf->index] << 1;
582 dest[i] = av_clipl_int32(((dest[i] - src[i]) << 16) + temp +
588 * Combined synthesis and formant perceptual weighting filer.
590 * @param qnt_lpc quantized lpc coefficients
591 * @param perf_lpc perceptual filter coefficients
592 * @param perf_fir perceptual filter fir memory
593 * @param perf_iir perceptual filter iir memory
594 * @param scale the filter output will be scaled by 2^scale
596 static void synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc,
597 int16_t *perf_fir, int16_t *perf_iir,
598 const int16_t *src, int16_t *dest, int scale)
601 int16_t buf_16[SUBFRAME_LEN + LPC_ORDER];
602 int64_t buf[SUBFRAME_LEN];
604 int16_t *bptr_16 = buf_16 + LPC_ORDER;
606 memcpy(buf_16, perf_fir, sizeof(int16_t) * LPC_ORDER);
607 memcpy(dest - LPC_ORDER, perf_iir, sizeof(int16_t) * LPC_ORDER);
609 for (i = 0; i < SUBFRAME_LEN; i++) {
611 for (j = 1; j <= LPC_ORDER; j++)
612 temp -= qnt_lpc[j - 1] * bptr_16[i - j];
614 buf[i] = (src[i] << 15) + (temp << 3);
615 bptr_16[i] = av_clipl_int32(buf[i] + (1 << 15)) >> 16;
618 for (i = 0; i < SUBFRAME_LEN; i++) {
619 int64_t fir = 0, iir = 0;
620 for (j = 1; j <= LPC_ORDER; j++) {
621 fir -= perf_lpc[j - 1] * bptr_16[i - j];
622 iir += perf_lpc[j + LPC_ORDER - 1] * dest[i - j];
624 dest[i] = av_clipl_int32(((buf[i] + (fir << 3)) << scale) + (iir << 3) +
627 memcpy(perf_fir, buf_16 + SUBFRAME_LEN, sizeof(int16_t) * LPC_ORDER);
628 memcpy(perf_iir, dest + SUBFRAME_LEN - LPC_ORDER,
629 sizeof(int16_t) * LPC_ORDER);
633 * Compute the adaptive codebook contribution.
635 * @param buf input signal
636 * @param index the current subframe index
638 static void acb_search(G723_1_Context *p, int16_t *residual,
639 int16_t *impulse_resp, const int16_t *buf,
642 int16_t flt_buf[PITCH_ORDER][SUBFRAME_LEN];
644 const int16_t *cb_tbl = adaptive_cb_gain85;
646 int ccr_buf[PITCH_ORDER * SUBFRAMES << 2];
648 int pitch_lag = p->pitch_lag[index >> 1];
651 int odd_frame = index & 1;
652 int iter = 3 + odd_frame;
660 if (pitch_lag == PITCH_MIN)
663 pitch_lag = FFMIN(pitch_lag, PITCH_MAX - 5);
666 for (i = 0; i < iter; i++) {
667 ff_g723_1_get_residual(residual, p->prev_excitation, pitch_lag + i - 1);
669 for (j = 0; j < SUBFRAME_LEN; j++) {
671 for (k = 0; k <= j; k++)
672 temp += residual[PITCH_ORDER - 1 + k] * impulse_resp[j - k];
673 flt_buf[PITCH_ORDER - 1][j] = av_clipl_int32((temp << 1) +
677 for (j = PITCH_ORDER - 2; j >= 0; j--) {
678 flt_buf[j][0] = ((residual[j] << 13) + (1 << 14)) >> 15;
679 for (k = 1; k < SUBFRAME_LEN; k++) {
680 temp = (flt_buf[j + 1][k - 1] << 15) +
681 residual[j] * impulse_resp[k];
682 flt_buf[j][k] = av_clipl_int32((temp << 1) + (1 << 15)) >> 16;
686 /* Compute crosscorrelation with the signal */
687 for (j = 0; j < PITCH_ORDER; j++) {
688 temp = ff_dot_product(buf, flt_buf[j], SUBFRAME_LEN);
689 ccr_buf[count++] = av_clipl_int32(temp << 1);
692 /* Compute energies */
693 for (j = 0; j < PITCH_ORDER; j++) {
694 ccr_buf[count++] = ff_g723_1_dot_product(flt_buf[j], flt_buf[j],
698 for (j = 1; j < PITCH_ORDER; j++) {
699 for (k = 0; k < j; k++) {
700 temp = ff_dot_product(flt_buf[j], flt_buf[k], SUBFRAME_LEN);
701 ccr_buf[count++] = av_clipl_int32(temp << 2);
706 /* Normalize and shorten */
708 for (i = 0; i < 20 * iter; i++)
709 max = FFMAX(max, FFABS(ccr_buf[i]));
711 temp = ff_g723_1_normalize_bits(max, 31);
713 for (i = 0; i < 20 * iter; i++)
714 ccr_buf[i] = av_clipl_int32((int64_t) (ccr_buf[i] << temp) +
718 for (i = 0; i < iter; i++) {
719 /* Select quantization table */
720 if (!odd_frame && pitch_lag + i - 1 >= SUBFRAME_LEN - 2 ||
721 odd_frame && pitch_lag >= SUBFRAME_LEN - 2) {
722 cb_tbl = adaptive_cb_gain170;
726 for (j = 0, k = 0; j < tbl_size; j++, k += 20) {
728 for (l = 0; l < 20; l++)
729 temp += ccr_buf[20 * i + l] * cb_tbl[k + l];
730 temp = av_clipl_int32(temp);
741 pitch_lag += acb_lag - 1;
745 p->pitch_lag[index >> 1] = pitch_lag;
746 p->subframe[index].ad_cb_lag = acb_lag;
747 p->subframe[index].ad_cb_gain = acb_gain;
751 * Subtract the adaptive codebook contribution from the input
752 * to obtain the residual.
754 * @param buf target vector
756 static void sub_acb_contrib(const int16_t *residual, const int16_t *impulse_resp,
760 /* Subtract adaptive CB contribution to obtain the residual */
761 for (i = 0; i < SUBFRAME_LEN; i++) {
762 int64_t temp = buf[i] << 14;
763 for (j = 0; j <= i; j++)
764 temp -= residual[j] * impulse_resp[i - j];
766 buf[i] = av_clipl_int32((temp << 2) + (1 << 15)) >> 16;
771 * Quantize the residual signal using the fixed codebook (MP-MLQ).
773 * @param optim optimized fixed codebook parameters
774 * @param buf excitation vector
776 static void get_fcb_param(FCBParam *optim, int16_t *impulse_resp,
777 int16_t *buf, int pulse_cnt, int pitch_lag)
780 int16_t impulse_r[SUBFRAME_LEN];
781 int16_t temp_corr[SUBFRAME_LEN];
782 int16_t impulse_corr[SUBFRAME_LEN];
784 int ccr1[SUBFRAME_LEN];
785 int ccr2[SUBFRAME_LEN];
786 int amp, err, max, max_amp_index, min, scale, i, j, k, l;
790 /* Update impulse response */
791 memcpy(impulse_r, impulse_resp, sizeof(int16_t) * SUBFRAME_LEN);
792 param.dirac_train = 0;
793 if (pitch_lag < SUBFRAME_LEN - 2) {
794 param.dirac_train = 1;
795 ff_g723_1_gen_dirac_train(impulse_r, pitch_lag);
798 for (i = 0; i < SUBFRAME_LEN; i++)
799 temp_corr[i] = impulse_r[i] >> 1;
801 /* Compute impulse response autocorrelation */
802 temp = ff_g723_1_dot_product(temp_corr, temp_corr, SUBFRAME_LEN);
804 scale = ff_g723_1_normalize_bits(temp, 31);
805 impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
807 for (i = 1; i < SUBFRAME_LEN; i++) {
808 temp = ff_g723_1_dot_product(temp_corr + i, temp_corr,
810 impulse_corr[i] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
813 /* Compute crosscorrelation of impulse response with residual signal */
815 for (i = 0; i < SUBFRAME_LEN; i++) {
816 temp = ff_g723_1_dot_product(buf + i, impulse_r, SUBFRAME_LEN - i);
818 ccr1[i] = temp >> -scale;
820 ccr1[i] = av_clipl_int32(temp << scale);
824 for (i = 0; i < GRID_SIZE; i++) {
825 /* Maximize the crosscorrelation */
827 for (j = i; j < SUBFRAME_LEN; j += GRID_SIZE) {
828 temp = FFABS(ccr1[j]);
831 param.pulse_pos[0] = j;
835 /* Quantize the gain (max crosscorrelation/impulse_corr[0]) */
838 max_amp_index = GAIN_LEVELS - 2;
839 for (j = max_amp_index; j >= 2; j--) {
840 temp = av_clipl_int32((int64_t) fixed_cb_gain[j] *
841 impulse_corr[0] << 1);
842 temp = FFABS(temp - amp);
850 /* Select additional gain values */
851 for (j = 1; j < 5; j++) {
852 for (k = i; k < SUBFRAME_LEN; k += GRID_SIZE) {
856 param.amp_index = max_amp_index + j - 2;
857 amp = fixed_cb_gain[param.amp_index];
859 param.pulse_sign[0] = (ccr2[param.pulse_pos[0]] < 0) ? -amp : amp;
860 temp_corr[param.pulse_pos[0]] = 1;
862 for (k = 1; k < pulse_cnt; k++) {
864 for (l = i; l < SUBFRAME_LEN; l += GRID_SIZE) {
867 temp = impulse_corr[FFABS(l - param.pulse_pos[k - 1])];
868 temp = av_clipl_int32((int64_t) temp *
869 param.pulse_sign[k - 1] << 1);
871 temp = FFABS(ccr2[l]);
874 param.pulse_pos[k] = l;
878 param.pulse_sign[k] = (ccr2[param.pulse_pos[k]] < 0) ?
880 temp_corr[param.pulse_pos[k]] = 1;
883 /* Create the error vector */
884 memset(temp_corr, 0, sizeof(int16_t) * SUBFRAME_LEN);
886 for (k = 0; k < pulse_cnt; k++)
887 temp_corr[param.pulse_pos[k]] = param.pulse_sign[k];
889 for (k = SUBFRAME_LEN - 1; k >= 0; k--) {
891 for (l = 0; l <= k; l++) {
892 int prod = av_clipl_int32((int64_t) temp_corr[l] *
893 impulse_r[k - l] << 1);
894 temp = av_clipl_int32(temp + prod);
896 temp_corr[k] = temp << 2 >> 16;
899 /* Compute square of error */
901 for (k = 0; k < SUBFRAME_LEN; k++) {
903 prod = av_clipl_int32((int64_t) buf[k] * temp_corr[k] << 1);
904 err = av_clipl_int32(err - prod);
905 prod = av_clipl_int32((int64_t) temp_corr[k] * temp_corr[k]);
906 err = av_clipl_int32(err + prod);
910 if (err < optim->min_err) {
911 optim->min_err = err;
912 optim->grid_index = i;
913 optim->amp_index = param.amp_index;
914 optim->dirac_train = param.dirac_train;
916 for (k = 0; k < pulse_cnt; k++) {
917 optim->pulse_sign[k] = param.pulse_sign[k];
918 optim->pulse_pos[k] = param.pulse_pos[k];
926 * Encode the pulse position and gain of the current subframe.
928 * @param optim optimized fixed CB parameters
929 * @param buf excitation vector
931 static void pack_fcb_param(G723_1_Subframe *subfrm, FCBParam *optim,
932 int16_t *buf, int pulse_cnt)
936 j = PULSE_MAX - pulse_cnt;
938 subfrm->pulse_sign = 0;
939 subfrm->pulse_pos = 0;
941 for (i = 0; i < SUBFRAME_LEN >> 1; i++) {
942 int val = buf[optim->grid_index + (i << 1)];
944 subfrm->pulse_pos += combinatorial_table[j][i];
946 subfrm->pulse_sign <<= 1;
948 subfrm->pulse_sign++;
955 subfrm->amp_index = optim->amp_index;
956 subfrm->grid_index = optim->grid_index;
957 subfrm->dirac_train = optim->dirac_train;
961 * Compute the fixed codebook excitation.
963 * @param buf target vector
964 * @param impulse_resp impulse response of the combined filter
966 static void fcb_search(G723_1_Context *p, int16_t *impulse_resp,
967 int16_t *buf, int index)
970 int pulse_cnt = pulses[index];
973 optim.min_err = 1 << 30;
974 get_fcb_param(&optim, impulse_resp, buf, pulse_cnt, SUBFRAME_LEN);
976 if (p->pitch_lag[index >> 1] < SUBFRAME_LEN - 2) {
977 get_fcb_param(&optim, impulse_resp, buf, pulse_cnt,
978 p->pitch_lag[index >> 1]);
981 /* Reconstruct the excitation */
982 memset(buf, 0, sizeof(int16_t) * SUBFRAME_LEN);
983 for (i = 0; i < pulse_cnt; i++)
984 buf[optim.pulse_pos[i]] = optim.pulse_sign[i];
986 pack_fcb_param(&p->subframe[index], &optim, buf, pulse_cnt);
988 if (optim.dirac_train)
989 ff_g723_1_gen_dirac_train(buf, p->pitch_lag[index >> 1]);
993 * Pack the frame parameters into output bitstream.
995 * @param frame output buffer
996 * @param size size of the buffer
998 static int pack_bitstream(G723_1_Context *p, AVPacket *avpkt)
1004 init_put_bits(&pb, avpkt->data, avpkt->size);
1006 put_bits(&pb, 2, info_bits);
1008 put_bits(&pb, 8, p->lsp_index[2]);
1009 put_bits(&pb, 8, p->lsp_index[1]);
1010 put_bits(&pb, 8, p->lsp_index[0]);
1012 put_bits(&pb, 7, p->pitch_lag[0] - PITCH_MIN);
1013 put_bits(&pb, 2, p->subframe[1].ad_cb_lag);
1014 put_bits(&pb, 7, p->pitch_lag[1] - PITCH_MIN);
1015 put_bits(&pb, 2, p->subframe[3].ad_cb_lag);
1017 /* Write 12 bit combined gain */
1018 for (i = 0; i < SUBFRAMES; i++) {
1019 temp = p->subframe[i].ad_cb_gain * GAIN_LEVELS +
1020 p->subframe[i].amp_index;
1021 if (p->cur_rate == RATE_6300)
1022 temp += p->subframe[i].dirac_train << 11;
1023 put_bits(&pb, 12, temp);
1026 put_bits(&pb, 1, p->subframe[0].grid_index);
1027 put_bits(&pb, 1, p->subframe[1].grid_index);
1028 put_bits(&pb, 1, p->subframe[2].grid_index);
1029 put_bits(&pb, 1, p->subframe[3].grid_index);
1031 if (p->cur_rate == RATE_6300) {
1032 skip_put_bits(&pb, 1); /* reserved bit */
1034 /* Write 13 bit combined position index */
1035 temp = (p->subframe[0].pulse_pos >> 16) * 810 +
1036 (p->subframe[1].pulse_pos >> 14) * 90 +
1037 (p->subframe[2].pulse_pos >> 16) * 9 +
1038 (p->subframe[3].pulse_pos >> 14);
1039 put_bits(&pb, 13, temp);
1041 put_bits(&pb, 16, p->subframe[0].pulse_pos & 0xffff);
1042 put_bits(&pb, 14, p->subframe[1].pulse_pos & 0x3fff);
1043 put_bits(&pb, 16, p->subframe[2].pulse_pos & 0xffff);
1044 put_bits(&pb, 14, p->subframe[3].pulse_pos & 0x3fff);
1046 put_bits(&pb, 6, p->subframe[0].pulse_sign);
1047 put_bits(&pb, 5, p->subframe[1].pulse_sign);
1048 put_bits(&pb, 6, p->subframe[2].pulse_sign);
1049 put_bits(&pb, 5, p->subframe[3].pulse_sign);
1052 flush_put_bits(&pb);
1053 return frame_size[info_bits];
1056 static int g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1057 const AVFrame *frame, int *got_packet_ptr)
1059 G723_1_Context *p = avctx->priv_data;
1060 int16_t unq_lpc[LPC_ORDER * SUBFRAMES];
1061 int16_t qnt_lpc[LPC_ORDER * SUBFRAMES];
1062 int16_t cur_lsp[LPC_ORDER];
1063 int16_t weighted_lpc[LPC_ORDER * SUBFRAMES << 1];
1064 int16_t vector[FRAME_LEN + PITCH_MAX];
1065 int offset, ret, i, j;
1066 int16_t *in, *start;
1069 /* duplicate input */
1070 start = in = av_malloc(frame->nb_samples * sizeof(int16_t));
1072 return AVERROR(ENOMEM);
1073 memcpy(in, frame->data[0], frame->nb_samples * sizeof(int16_t));
1075 highpass_filter(in, &p->hpf_fir_mem, &p->hpf_iir_mem);
1077 memcpy(vector, p->prev_data, HALF_FRAME_LEN * sizeof(int16_t));
1078 memcpy(vector + HALF_FRAME_LEN, in, FRAME_LEN * sizeof(int16_t));
1080 comp_lpc_coeff(vector, unq_lpc);
1081 lpc2lsp(&unq_lpc[LPC_ORDER * 3], p->prev_lsp, cur_lsp);
1082 lsp_quantize(p->lsp_index, cur_lsp, p->prev_lsp);
1085 memcpy(vector + LPC_ORDER, p->prev_data + SUBFRAME_LEN,
1086 sizeof(int16_t) * SUBFRAME_LEN);
1087 memcpy(vector + LPC_ORDER + SUBFRAME_LEN, in,
1088 sizeof(int16_t) * (HALF_FRAME_LEN + SUBFRAME_LEN));
1089 memcpy(p->prev_data, in + HALF_FRAME_LEN,
1090 sizeof(int16_t) * HALF_FRAME_LEN);
1091 memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
1093 perceptual_filter(p, weighted_lpc, unq_lpc, vector);
1095 memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
1096 memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
1097 memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
1099 ff_g723_1_scale_vector(vector, vector, FRAME_LEN + PITCH_MAX);
1101 p->pitch_lag[0] = estimate_pitch(vector, PITCH_MAX);
1102 p->pitch_lag[1] = estimate_pitch(vector, PITCH_MAX + HALF_FRAME_LEN);
1104 for (i = PITCH_MAX, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1105 comp_harmonic_coeff(vector + i, p->pitch_lag[j >> 1], hf + j);
1107 memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
1108 memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
1109 memcpy(p->prev_weight_sig, vector + FRAME_LEN, sizeof(int16_t) * PITCH_MAX);
1111 for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
1112 harmonic_filter(hf + j, vector + PITCH_MAX + i, in + i);
1114 ff_g723_1_inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, 0);
1115 ff_g723_1_lsp_interpolate(qnt_lpc, cur_lsp, p->prev_lsp);
1117 memcpy(p->prev_lsp, cur_lsp, sizeof(int16_t) * LPC_ORDER);
1120 for (i = 0; i < SUBFRAMES; i++) {
1121 int16_t impulse_resp[SUBFRAME_LEN];
1122 int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
1123 int16_t flt_in[SUBFRAME_LEN];
1124 int16_t zero[LPC_ORDER], fir[LPC_ORDER], iir[LPC_ORDER];
1127 * Compute the combined impulse response of the synthesis filter,
1128 * formant perceptual weighting filter and harmonic noise shaping filter
1130 memset(zero, 0, sizeof(int16_t) * LPC_ORDER);
1131 memset(vector, 0, sizeof(int16_t) * PITCH_MAX);
1132 memset(flt_in, 0, sizeof(int16_t) * SUBFRAME_LEN);
1134 flt_in[0] = 1 << 13; /* Unit impulse */
1135 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
1136 zero, zero, flt_in, vector + PITCH_MAX, 1);
1137 harmonic_filter(hf + i, vector + PITCH_MAX, impulse_resp);
1139 /* Compute the combined zero input response */
1141 memcpy(fir, p->perf_fir_mem, sizeof(int16_t) * LPC_ORDER);
1142 memcpy(iir, p->perf_iir_mem, sizeof(int16_t) * LPC_ORDER);
1144 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
1145 fir, iir, flt_in, vector + PITCH_MAX, 0);
1146 memcpy(vector, p->harmonic_mem, sizeof(int16_t) * PITCH_MAX);
1147 harmonic_noise_sub(hf + i, vector + PITCH_MAX, in);
1149 acb_search(p, residual, impulse_resp, in, i);
1150 ff_g723_1_gen_acb_excitation(residual, p->prev_excitation,
1151 p->pitch_lag[i >> 1], &p->subframe[i],
1153 sub_acb_contrib(residual, impulse_resp, in);
1155 fcb_search(p, impulse_resp, in, i);
1157 /* Reconstruct the excitation */
1158 ff_g723_1_gen_acb_excitation(impulse_resp, p->prev_excitation,
1159 p->pitch_lag[i >> 1], &p->subframe[i],
1162 memmove(p->prev_excitation, p->prev_excitation + SUBFRAME_LEN,
1163 sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
1164 for (j = 0; j < SUBFRAME_LEN; j++)
1165 in[j] = av_clip_int16((in[j] << 1) + impulse_resp[j]);
1166 memcpy(p->prev_excitation + PITCH_MAX - SUBFRAME_LEN, in,
1167 sizeof(int16_t) * SUBFRAME_LEN);
1169 /* Update filter memories */
1170 synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
1171 p->perf_fir_mem, p->perf_iir_mem,
1172 in, vector + PITCH_MAX, 0);
1173 memmove(p->harmonic_mem, p->harmonic_mem + SUBFRAME_LEN,
1174 sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
1175 memcpy(p->harmonic_mem + PITCH_MAX - SUBFRAME_LEN, vector + PITCH_MAX,
1176 sizeof(int16_t) * SUBFRAME_LEN);
1179 offset += LPC_ORDER;
1184 if ((ret = ff_alloc_packet2(avctx, avpkt, 24, 0)) < 0)
1187 *got_packet_ptr = 1;
1188 avpkt->size = pack_bitstream(p, avpkt);
1192 AVCodec ff_g723_1_encoder = {
1194 .long_name = NULL_IF_CONFIG_SMALL("G.723.1"),
1195 .type = AVMEDIA_TYPE_AUDIO,
1196 .id = AV_CODEC_ID_G723_1,
1197 .priv_data_size = sizeof(G723_1_Context),
1198 .init = g723_1_encode_init,
1199 .encode2 = g723_1_encode_frame,
1200 .sample_fmts = (const enum AVSampleFormat[]) {
1201 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE