LCOV - code coverage report
Current view: top level - libavcodec - g723_1enc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 562 578 97.2 %
Date: 2017-12-16 13:57:32 Functions: 21 21 100.0 %

          Line data    Source code
       1             : /*
       2             :  * G.723.1 compatible encoder
       3             :  * Copyright (c) Mohamed Naufal <naufal22@gmail.com>
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : /**
      23             :  * @file
      24             :  * G.723.1 compatible encoder
      25             :  */
      26             : 
      27             : #include <stdint.h>
      28             : #include <string.h>
      29             : 
      30             : #include "libavutil/channel_layout.h"
      31             : #include "libavutil/common.h"
      32             : #include "libavutil/mem.h"
      33             : #include "libavutil/opt.h"
      34             : 
      35             : #include "avcodec.h"
      36             : #include "celp_math.h"
      37             : #include "g723_1.h"
      38             : #include "internal.h"
      39             : 
      40             : #define BITSTREAM_WRITER_LE
      41             : #include "put_bits.h"
      42             : 
      43           1 : static av_cold int g723_1_encode_init(AVCodecContext *avctx)
      44             : {
      45           1 :     G723_1_Context *p = avctx->priv_data;
      46             : 
      47           1 :     if (avctx->sample_rate != 8000) {
      48           0 :         av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
      49           0 :         return AVERROR(EINVAL);
      50             :     }
      51             : 
      52           1 :     if (avctx->channels != 1) {
      53           0 :         av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
      54           0 :         return AVERROR(EINVAL);
      55             :     }
      56             : 
      57           1 :     if (avctx->bit_rate == 6300) {
      58           1 :         p->cur_rate = RATE_6300;
      59           0 :     } else if (avctx->bit_rate == 5300) {
      60           0 :         av_log(avctx, AV_LOG_ERROR, "Use bitrate 6300 instead of 5300.\n");
      61           0 :         avpriv_report_missing_feature(avctx, "Bitrate 5300");
      62           0 :         return AVERROR_PATCHWELCOME;
      63             :     } else {
      64           0 :         av_log(avctx, AV_LOG_ERROR, "Bitrate not supported, use 6300\n");
      65           0 :         return AVERROR(EINVAL);
      66             :     }
      67           1 :     avctx->frame_size = 240;
      68           1 :     memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(int16_t));
      69             : 
      70           1 :     return 0;
      71             : }
      72             : 
      73             : /**
      74             :  * Remove DC component from the input signal.
      75             :  *
      76             :  * @param buf input signal
      77             :  * @param fir zero memory
      78             :  * @param iir pole memory
      79             :  */
      80         200 : static void highpass_filter(int16_t *buf, int16_t *fir, int *iir)
      81             : {
      82             :     int i;
      83       48200 :     for (i = 0; i < FRAME_LEN; i++) {
      84       48000 :         *iir   = (buf[i] << 15) + ((-*fir) << 15) + MULL2(*iir, 0x7f00);
      85       48000 :         *fir   = buf[i];
      86       48000 :         buf[i] = av_clipl_int32((int64_t)*iir + (1 << 15)) >> 16;
      87             :     }
      88         200 : }
      89             : 
      90             : /**
      91             :  * Estimate autocorrelation of the input vector.
      92             :  *
      93             :  * @param buf      input buffer
      94             :  * @param autocorr autocorrelation coefficients vector
      95             :  */
      96         800 : static void comp_autocorr(int16_t *buf, int16_t *autocorr)
      97             : {
      98             :     int i, scale, temp;
      99             :     int16_t vector[LPC_FRAME];
     100             : 
     101         800 :     ff_g723_1_scale_vector(vector, buf, LPC_FRAME);
     102             : 
     103             :     /* Apply the Hamming window */
     104      144800 :     for (i = 0; i < LPC_FRAME; i++)
     105      144000 :         vector[i] = (vector[i] * hamming_window[i] + (1 << 14)) >> 15;
     106             : 
     107             :     /* Compute the first autocorrelation coefficient */
     108         800 :     temp = ff_dot_product(vector, vector, LPC_FRAME);
     109             : 
     110             :     /* Apply a white noise correlation factor of (1025/1024) */
     111         800 :     temp += temp >> 10;
     112             : 
     113             :     /* Normalize */
     114         800 :     scale       = ff_g723_1_normalize_bits(temp, 31);
     115        1600 :     autocorr[0] = av_clipl_int32((int64_t) (temp << scale) +
     116         800 :                                  (1 << 15)) >> 16;
     117             : 
     118             :     /* Compute the remaining coefficients */
     119         800 :     if (!autocorr[0]) {
     120           0 :         memset(autocorr + 1, 0, LPC_ORDER * sizeof(int16_t));
     121             :     } else {
     122        8800 :         for (i = 1; i <= LPC_ORDER; i++) {
     123        8000 :             temp        = ff_dot_product(vector, vector + i, LPC_FRAME - i);
     124        8000 :             temp        = MULL2((temp << scale), binomial_window[i - 1]);
     125        8000 :             autocorr[i] = av_clipl_int32((int64_t) temp + (1 << 15)) >> 16;
     126             :         }
     127             :     }
     128         800 : }
     129             : 
     130             : /**
     131             :  * Use Levinson-Durbin recursion to compute LPC coefficients from
     132             :  * autocorrelation values.
     133             :  *
     134             :  * @param lpc      LPC coefficients vector
     135             :  * @param autocorr autocorrelation coefficients vector
     136             :  * @param error    prediction error
     137             :  */
     138         800 : static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)
     139             : {
     140             :     int16_t vector[LPC_ORDER];
     141             :     int16_t partial_corr;
     142             :     int i, j, temp;
     143             : 
     144         800 :     memset(lpc, 0, LPC_ORDER * sizeof(int16_t));
     145             : 
     146        8800 :     for (i = 0; i < LPC_ORDER; i++) {
     147             :         /* Compute the partial correlation coefficient */
     148        8000 :         temp = 0;
     149       44000 :         for (j = 0; j < i; j++)
     150       36000 :             temp -= lpc[j] * autocorr[i - j - 1];
     151        8000 :         temp = ((autocorr[i] << 13) + temp) << 3;
     152             : 
     153        8000 :         if (FFABS(temp) >= (error << 16))
     154           0 :             break;
     155             : 
     156        8000 :         partial_corr = temp / (error << 1);
     157             : 
     158       16000 :         lpc[i] = av_clipl_int32((int64_t) (partial_corr << 14) +
     159        8000 :                                 (1 << 15)) >> 16;
     160             : 
     161             :         /* Update the prediction error */
     162        8000 :         temp  = MULL2(temp, partial_corr);
     163       16000 :         error = av_clipl_int32((int64_t) (error << 16) - temp +
     164        8000 :                                (1 << 15)) >> 16;
     165             : 
     166        8000 :         memcpy(vector, lpc, i * sizeof(int16_t));
     167       44000 :         for (j = 0; j < i; j++) {
     168       36000 :             temp   = partial_corr * vector[i - j - 1] << 1;
     169       72000 :             lpc[j] = av_clipl_int32((int64_t) (lpc[j] << 16) - temp +
     170       36000 :                                     (1 << 15)) >> 16;
     171             :         }
     172             :     }
     173         800 : }
     174             : 
     175             : /**
     176             :  * Calculate LPC coefficients for the current frame.
     177             :  *
     178             :  * @param buf       current frame
     179             :  * @param prev_data 2 trailing subframes of the previous frame
     180             :  * @param lpc       LPC coefficients vector
     181             :  */
     182         200 : static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
     183             : {
     184             :     int16_t autocorr[(LPC_ORDER + 1) * SUBFRAMES];
     185         200 :     int16_t *autocorr_ptr = autocorr;
     186         200 :     int16_t *lpc_ptr      = lpc;
     187             :     int i, j;
     188             : 
     189        1000 :     for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
     190         800 :         comp_autocorr(buf + i, autocorr_ptr);
     191         800 :         levinson_durbin(lpc_ptr, autocorr_ptr + 1, autocorr_ptr[0]);
     192             : 
     193         800 :         lpc_ptr      += LPC_ORDER;
     194         800 :         autocorr_ptr += LPC_ORDER + 1;
     195             :     }
     196         200 : }
     197             : 
     198         200 : static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
     199             : {
     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]
     203             : 
     204             :     int max, shift, cur_val, prev_val, count, p;
     205             :     int i, j;
     206             :     int64_t temp;
     207             : 
     208             :     /* Initialize f1[0] and f2[0] to 1 in Q25 */
     209        2200 :     for (i = 0; i < LPC_ORDER; i++)
     210        2000 :         lsp[i] = (lpc[i] * bandwidth_expand[i] + (1 << 14)) >> 15;
     211             : 
     212             :     /* Apply bandwidth expansion on the LPC coefficients */
     213         200 :     f[0] = f[1] = 1 << 25;
     214             : 
     215             :     /* Compute the remaining coefficients */
     216        1200 :     for (i = 0; i < LPC_ORDER / 2; i++) {
     217             :         /* f1 */
     218        1000 :         f[2 * i + 2] = -f[2 * i] - ((lsp[i] + lsp[LPC_ORDER - 1 - i]) << 12);
     219             :         /* f2 */
     220        1000 :         f[2 * i + 3] = f[2 * i + 1] - ((lsp[i] - lsp[LPC_ORDER - 1 - i]) << 12);
     221             :     }
     222             : 
     223             :     /* Divide f1[5] and f2[5] by 2 for use in polynomial evaluation */
     224         200 :     f[LPC_ORDER]     >>= 1;
     225         200 :     f[LPC_ORDER + 1] >>= 1;
     226             : 
     227             :     /* Normalize and shorten */
     228         200 :     max = FFABS(f[0]);
     229        2400 :     for (i = 1; i < LPC_ORDER + 2; i++)
     230        2200 :         max = FFMAX(max, FFABS(f[i]));
     231             : 
     232         200 :     shift = ff_g723_1_normalize_bits(max, 31);
     233             : 
     234        2600 :     for (i = 0; i < LPC_ORDER + 2; i++)
     235        2400 :         f[i] = av_clipl_int32((int64_t) (f[i] << shift) + (1 << 15)) >> 16;
     236             : 
     237             :     /**
     238             :      * Evaluate F1 and F2 at uniform intervals of pi/256 along the
     239             :      * unit circle and check for zero crossings.
     240             :      */
     241         200 :     p    = 0;
     242         200 :     temp = 0;
     243        1400 :     for (i = 0; i <= LPC_ORDER / 2; i++)
     244        1200 :         temp += f[2 * i] * cos_tab[0];
     245         200 :     prev_val = av_clipl_int32(temp << 1);
     246         200 :     count    = 0;
     247       46245 :     for (i = 1; i < COS_TBL_SIZE / 2; i++) {
     248             :         /* Evaluate */
     249       46245 :         temp = 0;
     250      323715 :         for (j = 0; j <= LPC_ORDER / 2; j++)
     251      277470 :             temp += f[LPC_ORDER - 2 * j + p] * cos_tab[i * j % COS_TBL_SIZE];
     252       46245 :         cur_val = av_clipl_int32(temp << 1);
     253             : 
     254             :         /* Check for sign change, indicating a zero crossing */
     255       46245 :         if ((cur_val ^ prev_val) < 0) {
     256        2000 :             int abs_cur  = FFABS(cur_val);
     257        2000 :             int abs_prev = FFABS(prev_val);
     258        2000 :             int sum      = abs_cur + abs_prev;
     259             : 
     260        2000 :             shift        = ff_g723_1_normalize_bits(sum, 31);
     261        2000 :             sum        <<= shift;
     262        2000 :             abs_prev     = abs_prev << shift >> 8;
     263        2000 :             lsp[count++] = ((i - 1) << 7) + (abs_prev >> 1) / (sum >> 16);
     264             : 
     265        2000 :             if (count == LPC_ORDER)
     266         200 :                 break;
     267             : 
     268             :             /* Switch between sum and difference polynomials */
     269        1800 :             p ^= 1;
     270             : 
     271             :             /* Evaluate */
     272        1800 :             temp = 0;
     273       12600 :             for (j = 0; j <= LPC_ORDER / 2; j++)
     274       21600 :                 temp += f[LPC_ORDER - 2 * j + p] *
     275       10800 :                         cos_tab[i * j % COS_TBL_SIZE];
     276        1800 :             cur_val = av_clipl_int32(temp << 1);
     277             :         }
     278       46045 :         prev_val = cur_val;
     279             :     }
     280             : 
     281         200 :     if (count != LPC_ORDER)
     282           0 :         memcpy(lsp, prev_lsp, LPC_ORDER * sizeof(int16_t));
     283         200 : }
     284             : 
     285             : /**
     286             :  * Quantize the current LSP subvector.
     287             :  *
     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
     291             :  */
     292             : #define get_index(num, offset, size)                                          \
     293             : {                                                                             \
     294             :     int error, max = -1;                                                      \
     295             :     int16_t temp[4];                                                          \
     296             :     int i, j;                                                                 \
     297             :                                                                               \
     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] +           \
     301             :                       (1 << 14)) >> 15;                                       \
     302             :         }                                                                     \
     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);         \
     305             :         if (error > max) {                                                    \
     306             :             max = error;                                                      \
     307             :             lsp_index[num] = i;                                               \
     308             :         }                                                                     \
     309             :     }                                                                         \
     310             : }
     311             : 
     312             : /**
     313             :  * Vector quantize the LSP frequencies.
     314             :  *
     315             :  * @param lsp      the current lsp vector
     316             :  * @param prev_lsp the previous lsp vector
     317             :  */
     318         200 : static void lsp_quantize(uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
     319             : {
     320             :     int16_t weight[LPC_ORDER];
     321             :     int16_t min, max;
     322             :     int shift, i;
     323             : 
     324             :     /* Calculate the VQ weighting vector */
     325         200 :     weight[0]             = (1 << 20) / (lsp[1] - lsp[0]);
     326         200 :     weight[LPC_ORDER - 1] = (1 << 20) /
     327         200 :                             (lsp[LPC_ORDER - 1] - lsp[LPC_ORDER - 2]);
     328             : 
     329        1800 :     for (i = 1; i < LPC_ORDER - 1; i++) {
     330        1600 :         min = FFMIN(lsp[i] - lsp[i - 1], lsp[i + 1] - lsp[i]);
     331        1600 :         if (min > 0x20)
     332        1600 :             weight[i] = (1 << 20) / min;
     333             :         else
     334           0 :             weight[i] = INT16_MAX;
     335             :     }
     336             : 
     337             :     /* Normalize */
     338         200 :     max = 0;
     339        2200 :     for (i = 0; i < LPC_ORDER; i++)
     340        2000 :         max = FFMAX(weight[i], max);
     341             : 
     342         200 :     shift = ff_g723_1_normalize_bits(max, 15);
     343        2200 :     for (i = 0; i < LPC_ORDER; i++) {
     344        2000 :         weight[i] <<= shift;
     345             :     }
     346             : 
     347             :     /* Compute the VQ target vector */
     348        2200 :     for (i = 0; i < LPC_ORDER; i++) {
     349        4000 :         lsp[i] -= dc_lsp[i] +
     350        2000 :                   (((prev_lsp[i] - dc_lsp[i]) * 12288 + (1 << 14)) >> 15);
     351             :     }
     352             : 
     353         200 :     get_index(0, 0, 3);
     354         200 :     get_index(1, 3, 3);
     355         200 :     get_index(2, 6, 4);
     356         200 : }
     357             : 
     358             : /**
     359             :  * Perform IIR filtering.
     360             :  *
     361             :  * @param fir_coef FIR coefficients
     362             :  * @param iir_coef IIR coefficients
     363             :  * @param src      source vector
     364             :  * @param dest     destination vector
     365             :  */
     366         800 : static void iir_filter(int16_t *fir_coef, int16_t *iir_coef,
     367             :                        int16_t *src, int16_t *dest)
     368             : {
     369             :     int m, n;
     370             : 
     371       48800 :     for (m = 0; m < SUBFRAME_LEN; m++) {
     372       48000 :         int64_t filter = 0;
     373      528000 :         for (n = 1; n <= LPC_ORDER; n++) {
     374      960000 :             filter -= fir_coef[n - 1] * src[m - n] -
     375      480000 :                       iir_coef[n - 1] * dest[m - n];
     376             :         }
     377             : 
     378       96000 :         dest[m] = av_clipl_int32((src[m] << 16) + (filter << 3) +
     379       48000 :                                  (1 << 15)) >> 16;
     380             :     }
     381         800 : }
     382             : 
     383             : /**
     384             :  * Apply the formant perceptual weighting filter.
     385             :  *
     386             :  * @param flt_coef filter coefficients
     387             :  * @param unq_lpc  unquantized lpc vector
     388             :  */
     389         200 : static void perceptual_filter(G723_1_Context *p, int16_t *flt_coef,
     390             :                               int16_t *unq_lpc, int16_t *buf)
     391             : {
     392             :     int16_t vector[FRAME_LEN + LPC_ORDER];
     393         200 :     int i, j, k, l = 0;
     394             : 
     395         200 :     memcpy(buf, p->iir_mem, sizeof(int16_t) * LPC_ORDER);
     396         200 :     memcpy(vector, p->fir_mem, sizeof(int16_t) * LPC_ORDER);
     397         200 :     memcpy(vector + LPC_ORDER, buf + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
     398             : 
     399        1000 :     for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
     400        8800 :         for (k = 0; k < LPC_ORDER; k++) {
     401       16000 :             flt_coef[k + 2 * l] = (unq_lpc[k + l] * percept_flt_tbl[0][k] +
     402        8000 :                                    (1 << 14)) >> 15;
     403       24000 :             flt_coef[k + 2 * l + LPC_ORDER] = (unq_lpc[k + l] *
     404       16000 :                                                percept_flt_tbl[1][k] +
     405        8000 :                                                (1 << 14)) >> 15;
     406             :         }
     407        1600 :         iir_filter(flt_coef + 2 * l, flt_coef + 2 * l + LPC_ORDER,
     408        1600 :                    vector + i, buf + i);
     409         800 :         l += LPC_ORDER;
     410             :     }
     411         200 :     memcpy(p->iir_mem, buf + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
     412         200 :     memcpy(p->fir_mem, vector + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
     413         200 : }
     414             : 
     415             : /**
     416             :  * Estimate the open loop pitch period.
     417             :  *
     418             :  * @param buf   perceptually weighted speech
     419             :  * @param start estimation is carried out from this position
     420             :  */
     421         400 : static int estimate_pitch(int16_t *buf, int start)
     422             : {
     423         400 :     int max_exp = 32;
     424         400 :     int max_ccr = 0x4000;
     425         400 :     int max_eng = 0x7fff;
     426         400 :     int index   = PITCH_MIN;
     427         400 :     int offset  = start - PITCH_MIN + 1;
     428             : 
     429             :     int ccr, eng, orig_eng, ccr_eng, exp;
     430             :     int diff, temp;
     431             : 
     432             :     int i;
     433             : 
     434         400 :     orig_eng = ff_dot_product(buf + offset, buf + offset, HALF_FRAME_LEN);
     435             : 
     436       50400 :     for (i = PITCH_MIN; i <= PITCH_MAX - 3; i++) {
     437       50000 :         offset--;
     438             : 
     439             :         /* Update energy and compute correlation */
     440      100000 :         orig_eng += buf[offset] * buf[offset] -
     441       50000 :                     buf[offset + HALF_FRAME_LEN] * buf[offset + HALF_FRAME_LEN];
     442       50000 :         ccr = ff_dot_product(buf + start, buf + offset, HALF_FRAME_LEN);
     443       50000 :         if (ccr <= 0)
     444       25406 :             continue;
     445             : 
     446             :         /* Split into mantissa and exponent to maintain precision */
     447       24594 :         exp   = ff_g723_1_normalize_bits(ccr, 31);
     448       24594 :         ccr   = av_clipl_int32((int64_t) (ccr << exp) + (1 << 15)) >> 16;
     449       24594 :         exp <<= 1;
     450       24594 :         ccr  *= ccr;
     451       24594 :         temp  = ff_g723_1_normalize_bits(ccr, 31);
     452       24594 :         ccr   = ccr << temp >> 16;
     453       24594 :         exp  += temp;
     454             : 
     455       24594 :         temp = ff_g723_1_normalize_bits(orig_eng, 31);
     456       24594 :         eng  = av_clipl_int32((int64_t) (orig_eng << temp) + (1 << 15)) >> 16;
     457       24594 :         exp -= temp;
     458             : 
     459       24594 :         if (ccr >= eng) {
     460        9755 :             exp--;
     461        9755 :             ccr >>= 1;
     462             :         }
     463       24594 :         if (exp > max_exp)
     464       19555 :             continue;
     465             : 
     466        5039 :         if (exp + 1 < max_exp)
     467         502 :             goto update;
     468             : 
     469             :         /* Equalize exponents before comparison */
     470        4537 :         if (exp + 1 == max_exp)
     471         364 :             temp = max_ccr >> 1;
     472             :         else
     473        4173 :             temp = max_ccr;
     474        4537 :         ccr_eng = ccr * max_eng;
     475        4537 :         diff    = ccr_eng - eng * temp;
     476        5039 :         if (diff > 0 && (i - index < PITCH_MIN || diff > ccr_eng >> 2)) {
     477         693 : update:
     478        1195 :             index   = i;
     479        1195 :             max_exp = exp;
     480        1195 :             max_ccr = ccr;
     481        1195 :             max_eng = eng;
     482             :         }
     483             :     }
     484         400 :     return index;
     485             : }
     486             : 
     487             : /**
     488             :  * Compute harmonic noise filter parameters.
     489             :  *
     490             :  * @param buf       perceptually weighted speech
     491             :  * @param pitch_lag open loop pitch period
     492             :  * @param hf        harmonic filter parameters
     493             :  */
     494         800 : static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag, HFParam *hf)
     495             : {
     496             :     int ccr, eng, max_ccr, max_eng;
     497             :     int exp, max, diff;
     498             :     int energy[15];
     499             :     int i, j;
     500             : 
     501        6400 :     for (i = 0, j = pitch_lag - 3; j <= pitch_lag + 3; i++, j++) {
     502             :         /* Compute residual energy */
     503        5600 :         energy[i << 1] = ff_dot_product(buf - j, buf - j, SUBFRAME_LEN);
     504             :         /* Compute correlation */
     505        5600 :         energy[(i << 1) + 1] = ff_dot_product(buf, buf - j, SUBFRAME_LEN);
     506             :     }
     507             : 
     508             :     /* Compute target energy */
     509         800 :     energy[14] = ff_dot_product(buf, buf, SUBFRAME_LEN);
     510             : 
     511             :     /* Normalize */
     512         800 :     max = 0;
     513       12800 :     for (i = 0; i < 15; i++)
     514       12000 :         max = FFMAX(max, FFABS(energy[i]));
     515             : 
     516         800 :     exp = ff_g723_1_normalize_bits(max, 31);
     517       12800 :     for (i = 0; i < 15; i++) {
     518       24000 :         energy[i] = av_clipl_int32((int64_t)(energy[i] << exp) +
     519       12000 :                                    (1 << 15)) >> 16;
     520             :     }
     521             : 
     522         800 :     hf->index = -1;
     523         800 :     hf->gain  =  0;
     524         800 :     max_ccr   =  1;
     525         800 :     max_eng   =  0x7fff;
     526             : 
     527        6400 :     for (i = 0; i <= 6; i++) {
     528        5600 :         eng = energy[i << 1];
     529        5600 :         ccr = energy[(i << 1) + 1];
     530             : 
     531        5600 :         if (ccr <= 0)
     532        1732 :             continue;
     533             : 
     534        3868 :         ccr  = (ccr * ccr + (1 << 14)) >> 15;
     535        3868 :         diff = ccr * max_eng - eng * max_ccr;
     536        3868 :         if (diff > 0) {
     537        2218 :             max_ccr   = ccr;
     538        2218 :             max_eng   = eng;
     539        2218 :             hf->index = i;
     540             :         }
     541             :     }
     542             : 
     543         800 :     if (hf->index == -1) {
     544           3 :         hf->index = pitch_lag;
     545           3 :         return;
     546             :     }
     547             : 
     548         797 :     eng = energy[14] * max_eng;
     549         797 :     eng = (eng >> 2) + (eng >> 3);
     550         797 :     ccr = energy[(hf->index << 1) + 1] * energy[(hf->index << 1) + 1];
     551         797 :     if (eng < ccr) {
     552         656 :         eng = energy[(hf->index << 1) + 1];
     553             : 
     554         656 :         if (eng >= max_eng)
     555         202 :             hf->gain = 0x2800;
     556             :         else
     557         454 :             hf->gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15;
     558             :     }
     559         797 :     hf->index += pitch_lag - 3;
     560             : }
     561             : 
     562             : /**
     563             :  * Apply the harmonic noise shaping filter.
     564             :  *
     565             :  * @param hf filter parameters
     566             :  */
     567        1600 : static void harmonic_filter(HFParam *hf, const int16_t *src, int16_t *dest)
     568             : {
     569             :     int i;
     570             : 
     571       97600 :     for (i = 0; i < SUBFRAME_LEN; i++) {
     572       96000 :         int64_t temp = hf->gain * src[i - hf->index] << 1;
     573       96000 :         dest[i] = av_clipl_int32((src[i] << 16) - temp + (1 << 15)) >> 16;
     574             :     }
     575        1600 : }
     576             : 
     577         800 : static void harmonic_noise_sub(HFParam *hf, const int16_t *src, int16_t *dest)
     578             : {
     579             :     int i;
     580       48800 :     for (i = 0; i < SUBFRAME_LEN; i++) {
     581       48000 :         int64_t temp = hf->gain * src[i - hf->index] << 1;
     582       96000 :         dest[i] = av_clipl_int32(((dest[i] - src[i]) << 16) + temp +
     583       48000 :                                  (1 << 15)) >> 16;
     584             :     }
     585         800 : }
     586             : 
     587             : /**
     588             :  * Combined synthesis and formant perceptual weighting filer.
     589             :  *
     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
     595             :  */
     596        2400 : 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)
     599             : {
     600             :     int i, j;
     601             :     int16_t buf_16[SUBFRAME_LEN + LPC_ORDER];
     602             :     int64_t buf[SUBFRAME_LEN];
     603             : 
     604        2400 :     int16_t *bptr_16 = buf_16 + LPC_ORDER;
     605             : 
     606        2400 :     memcpy(buf_16, perf_fir, sizeof(int16_t) * LPC_ORDER);
     607        2400 :     memcpy(dest - LPC_ORDER, perf_iir, sizeof(int16_t) * LPC_ORDER);
     608             : 
     609      146400 :     for (i = 0; i < SUBFRAME_LEN; i++) {
     610      144000 :         int64_t temp = 0;
     611     1584000 :         for (j = 1; j <= LPC_ORDER; j++)
     612     1440000 :             temp -= qnt_lpc[j - 1] * bptr_16[i - j];
     613             : 
     614      144000 :         buf[i]     = (src[i] << 15) + (temp << 3);
     615      144000 :         bptr_16[i] = av_clipl_int32(buf[i] + (1 << 15)) >> 16;
     616             :     }
     617             : 
     618      146400 :     for (i = 0; i < SUBFRAME_LEN; i++) {
     619      144000 :         int64_t fir = 0, iir = 0;
     620     1584000 :         for (j = 1; j <= LPC_ORDER; j++) {
     621     1440000 :             fir -= perf_lpc[j - 1] * bptr_16[i - j];
     622     1440000 :             iir += perf_lpc[j + LPC_ORDER - 1] * dest[i - j];
     623             :         }
     624      288000 :         dest[i] = av_clipl_int32(((buf[i] + (fir << 3)) << scale) + (iir << 3) +
     625      144000 :                                  (1 << 15)) >> 16;
     626             :     }
     627        2400 :     memcpy(perf_fir, buf_16 + SUBFRAME_LEN, sizeof(int16_t) * LPC_ORDER);
     628        2400 :     memcpy(perf_iir, dest + SUBFRAME_LEN - LPC_ORDER,
     629             :            sizeof(int16_t) * LPC_ORDER);
     630        2400 : }
     631             : 
     632             : /**
     633             :  * Compute the adaptive codebook contribution.
     634             :  *
     635             :  * @param buf   input signal
     636             :  * @param index the current subframe index
     637             :  */
     638         800 : static void acb_search(G723_1_Context *p, int16_t *residual,
     639             :                        int16_t *impulse_resp, const int16_t *buf,
     640             :                        int index)
     641             : {
     642             :     int16_t flt_buf[PITCH_ORDER][SUBFRAME_LEN];
     643             : 
     644         800 :     const int16_t *cb_tbl = adaptive_cb_gain85;
     645             : 
     646             :     int ccr_buf[PITCH_ORDER * SUBFRAMES << 2];
     647             : 
     648         800 :     int pitch_lag = p->pitch_lag[index >> 1];
     649         800 :     int acb_lag   = 1;
     650         800 :     int acb_gain  = 0;
     651         800 :     int odd_frame = index & 1;
     652         800 :     int iter      = 3 + odd_frame;
     653         800 :     int count     = 0;
     654         800 :     int tbl_size  = 85;
     655             : 
     656             :     int i, j, k, l, max;
     657             :     int64_t temp;
     658             : 
     659         800 :     if (!odd_frame) {
     660         400 :         if (pitch_lag == PITCH_MIN)
     661          36 :             pitch_lag++;
     662             :         else
     663         364 :             pitch_lag = FFMIN(pitch_lag, PITCH_MAX - 5);
     664             :     }
     665             : 
     666        3600 :     for (i = 0; i < iter; i++) {
     667        2800 :         ff_g723_1_get_residual(residual, p->prev_excitation, pitch_lag + i - 1);
     668             : 
     669      170800 :         for (j = 0; j < SUBFRAME_LEN; j++) {
     670      168000 :             temp = 0;
     671     5292000 :             for (k = 0; k <= j; k++)
     672     5124000 :                 temp += residual[PITCH_ORDER - 1 + k] * impulse_resp[j - k];
     673      336000 :             flt_buf[PITCH_ORDER - 1][j] = av_clipl_int32((temp << 1) +
     674      168000 :                                                          (1 << 15)) >> 16;
     675             :         }
     676             : 
     677       14000 :         for (j = PITCH_ORDER - 2; j >= 0; j--) {
     678       11200 :             flt_buf[j][0] = ((residual[j] << 13) + (1 << 14)) >> 15;
     679      672000 :             for (k = 1; k < SUBFRAME_LEN; k++) {
     680     1321600 :                 temp = (flt_buf[j + 1][k - 1] << 15) +
     681      660800 :                        residual[j] * impulse_resp[k];
     682      660800 :                 flt_buf[j][k] = av_clipl_int32((temp << 1) + (1 << 15)) >> 16;
     683             :             }
     684             :         }
     685             : 
     686             :         /* Compute crosscorrelation with the signal */
     687       16800 :         for (j = 0; j < PITCH_ORDER; j++) {
     688       14000 :             temp             = ff_dot_product(buf, flt_buf[j], SUBFRAME_LEN);
     689       14000 :             ccr_buf[count++] = av_clipl_int32(temp << 1);
     690             :         }
     691             : 
     692             :         /* Compute energies */
     693       16800 :         for (j = 0; j < PITCH_ORDER; j++) {
     694       14000 :             ccr_buf[count++] = ff_g723_1_dot_product(flt_buf[j], flt_buf[j],
     695             :                                                      SUBFRAME_LEN);
     696             :         }
     697             : 
     698       14000 :         for (j = 1; j < PITCH_ORDER; j++) {
     699       39200 :             for (k = 0; k < j; k++) {
     700       28000 :                 temp             = ff_dot_product(flt_buf[j], flt_buf[k], SUBFRAME_LEN);
     701       28000 :                 ccr_buf[count++] = av_clipl_int32(temp << 2);
     702             :             }
     703             :         }
     704             :     }
     705             : 
     706             :     /* Normalize and shorten */
     707         800 :     max = 0;
     708       56800 :     for (i = 0; i < 20 * iter; i++)
     709       56000 :         max = FFMAX(max, FFABS(ccr_buf[i]));
     710             : 
     711         800 :     temp = ff_g723_1_normalize_bits(max, 31);
     712             : 
     713       56800 :     for (i = 0; i < 20 * iter; i++)
     714      112000 :         ccr_buf[i] = av_clipl_int32((int64_t) (ccr_buf[i] << temp) +
     715       56000 :                                     (1 << 15)) >> 16;
     716             : 
     717         800 :     max = 0;
     718        3600 :     for (i = 0; i < iter; i++) {
     719             :         /* Select quantization table */
     720        2800 :         if (!odd_frame && pitch_lag + i - 1 >= SUBFRAME_LEN - 2 ||
     721        1600 :             odd_frame && pitch_lag >= SUBFRAME_LEN - 2) {
     722         232 :             cb_tbl   = adaptive_cb_gain170;
     723         232 :             tbl_size = 170;
     724             :         }
     725             : 
     726      260520 :         for (j = 0, k = 0; j < tbl_size; j++, k += 20) {
     727      257720 :             temp = 0;
     728     5412120 :             for (l = 0; l < 20; l++)
     729     5154400 :                 temp += ccr_buf[20 * i + l] * cb_tbl[k + l];
     730      257720 :             temp = av_clipl_int32(temp);
     731             : 
     732      257720 :             if (temp > max) {
     733        8615 :                 max      = temp;
     734        8615 :                 acb_gain = j;
     735        8615 :                 acb_lag  = i;
     736             :             }
     737             :         }
     738             :     }
     739             : 
     740         800 :     if (!odd_frame) {
     741         400 :         pitch_lag += acb_lag - 1;
     742         400 :         acb_lag    = 1;
     743             :     }
     744             : 
     745         800 :     p->pitch_lag[index >> 1]      = pitch_lag;
     746         800 :     p->subframe[index].ad_cb_lag  = acb_lag;
     747         800 :     p->subframe[index].ad_cb_gain = acb_gain;
     748         800 : }
     749             : 
     750             : /**
     751             :  * Subtract the adaptive codebook contribution from the input
     752             :  * to obtain the residual.
     753             :  *
     754             :  * @param buf target vector
     755             :  */
     756         800 : static void sub_acb_contrib(const int16_t *residual, const int16_t *impulse_resp,
     757             :                             int16_t *buf)
     758             : {
     759             :     int i, j;
     760             :     /* Subtract adaptive CB contribution to obtain the residual */
     761       48800 :     for (i = 0; i < SUBFRAME_LEN; i++) {
     762       48000 :         int64_t temp = buf[i] << 14;
     763     1512000 :         for (j = 0; j <= i; j++)
     764     1464000 :             temp -= residual[j] * impulse_resp[i - j];
     765             : 
     766       48000 :         buf[i] = av_clipl_int32((temp << 2) + (1 << 15)) >> 16;
     767             :     }
     768         800 : }
     769             : 
     770             : /**
     771             :  * Quantize the residual signal using the fixed codebook (MP-MLQ).
     772             :  *
     773             :  * @param optim optimized fixed codebook parameters
     774             :  * @param buf   excitation vector
     775             :  */
     776        1534 : static void get_fcb_param(FCBParam *optim, int16_t *impulse_resp,
     777             :                           int16_t *buf, int pulse_cnt, int pitch_lag)
     778             : {
     779             :     FCBParam param;
     780             :     int16_t impulse_r[SUBFRAME_LEN];
     781             :     int16_t temp_corr[SUBFRAME_LEN];
     782             :     int16_t impulse_corr[SUBFRAME_LEN];
     783             : 
     784             :     int ccr1[SUBFRAME_LEN];
     785             :     int ccr2[SUBFRAME_LEN];
     786             :     int amp, err, max, max_amp_index, min, scale, i, j, k, l;
     787             : 
     788             :     int64_t temp;
     789             : 
     790             :     /* Update impulse response */
     791        1534 :     memcpy(impulse_r, impulse_resp, sizeof(int16_t) * SUBFRAME_LEN);
     792        1534 :     param.dirac_train = 0;
     793        1534 :     if (pitch_lag < SUBFRAME_LEN - 2) {
     794         734 :         param.dirac_train = 1;
     795         734 :         ff_g723_1_gen_dirac_train(impulse_r, pitch_lag);
     796             :     }
     797             : 
     798       93574 :     for (i = 0; i < SUBFRAME_LEN; i++)
     799       92040 :         temp_corr[i] = impulse_r[i] >> 1;
     800             : 
     801             :     /* Compute impulse response autocorrelation */
     802        1534 :     temp = ff_g723_1_dot_product(temp_corr, temp_corr, SUBFRAME_LEN);
     803             : 
     804        1534 :     scale           = ff_g723_1_normalize_bits(temp, 31);
     805        1534 :     impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
     806             : 
     807       92040 :     for (i = 1; i < SUBFRAME_LEN; i++) {
     808       90506 :         temp = ff_g723_1_dot_product(temp_corr + i, temp_corr,
     809             :                                      SUBFRAME_LEN - i);
     810       90506 :         impulse_corr[i] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
     811             :     }
     812             : 
     813             :     /* Compute crosscorrelation of impulse response with residual signal */
     814        1534 :     scale -= 4;
     815       93574 :     for (i = 0; i < SUBFRAME_LEN; i++) {
     816       92040 :         temp = ff_g723_1_dot_product(buf + i, impulse_r, SUBFRAME_LEN - i);
     817       92040 :         if (scale < 0)
     818       55500 :             ccr1[i] = temp >> -scale;
     819             :         else
     820       36540 :             ccr1[i] = av_clipl_int32(temp << scale);
     821             :     }
     822             : 
     823             :     /* Search loop */
     824        4602 :     for (i = 0; i < GRID_SIZE; i++) {
     825             :         /* Maximize the crosscorrelation */
     826        3068 :         max = 0;
     827       95108 :         for (j = i; j < SUBFRAME_LEN; j += GRID_SIZE) {
     828       92040 :             temp = FFABS(ccr1[j]);
     829       92040 :             if (temp >= max) {
     830       12301 :                 max                = temp;
     831       12301 :                 param.pulse_pos[0] = j;
     832             :             }
     833             :         }
     834             : 
     835             :         /* Quantize the gain (max crosscorrelation/impulse_corr[0]) */
     836        3068 :         amp           = max;
     837        3068 :         min           = 1 << 30;
     838        3068 :         max_amp_index = GAIN_LEVELS - 2;
     839       67496 :         for (j = max_amp_index; j >= 2; j--) {
     840      128856 :             temp = av_clipl_int32((int64_t) fixed_cb_gain[j] *
     841       64428 :                                   impulse_corr[0] << 1);
     842       64428 :             temp = FFABS(temp - amp);
     843       64428 :             if (temp < min) {
     844       33898 :                 min           = temp;
     845       33898 :                 max_amp_index = j;
     846             :             }
     847             :         }
     848             : 
     849        3068 :         max_amp_index--;
     850             :         /* Select additional gain values */
     851       15340 :         for (j = 1; j < 5; j++) {
     852      380432 :             for (k = i; k < SUBFRAME_LEN; k += GRID_SIZE) {
     853      368160 :                 temp_corr[k] = 0;
     854      368160 :                 ccr2[k]      = ccr1[k];
     855             :             }
     856       12272 :             param.amp_index = max_amp_index + j - 2;
     857       12272 :             amp             = fixed_cb_gain[param.amp_index];
     858             : 
     859       12272 :             param.pulse_sign[0] = (ccr2[param.pulse_pos[0]] < 0) ? -amp : amp;
     860       12272 :             temp_corr[param.pulse_pos[0]] = 1;
     861             : 
     862       67496 :             for (k = 1; k < pulse_cnt; k++) {
     863       55224 :                 max = INT_MIN;
     864     1711944 :                 for (l = i; l < SUBFRAME_LEN; l += GRID_SIZE) {
     865     1656720 :                     if (temp_corr[l])
     866      153400 :                         continue;
     867     1503320 :                     temp = impulse_corr[FFABS(l - param.pulse_pos[k - 1])];
     868     1503320 :                     temp = av_clipl_int32((int64_t) temp *
     869     1503320 :                                           param.pulse_sign[k - 1] << 1);
     870     1503320 :                     ccr2[l] -= temp;
     871     1503320 :                     temp     = FFABS(ccr2[l]);
     872     1503320 :                     if (temp > max) {
     873      224253 :                         max                = temp;
     874      224253 :                         param.pulse_pos[k] = l;
     875             :                     }
     876             :                 }
     877             : 
     878      110448 :                 param.pulse_sign[k] = (ccr2[param.pulse_pos[k]] < 0) ?
     879       55224 :                                       -amp : amp;
     880       55224 :                 temp_corr[param.pulse_pos[k]] = 1;
     881             :             }
     882             : 
     883             :             /* Create the error vector */
     884       12272 :             memset(temp_corr, 0, sizeof(int16_t) * SUBFRAME_LEN);
     885             : 
     886       79768 :             for (k = 0; k < pulse_cnt; k++)
     887       67496 :                 temp_corr[param.pulse_pos[k]] = param.pulse_sign[k];
     888             : 
     889      748592 :             for (k = SUBFRAME_LEN - 1; k >= 0; k--) {
     890      736320 :                 temp = 0;
     891    23194080 :                 for (l = 0; l <= k; l++) {
     892    44915520 :                     int prod = av_clipl_int32((int64_t) temp_corr[l] *
     893    22457760 :                                               impulse_r[k - l] << 1);
     894    22457760 :                     temp = av_clipl_int32(temp + prod);
     895             :                 }
     896      736320 :                 temp_corr[k] = temp << 2 >> 16;
     897             :             }
     898             : 
     899             :             /* Compute square of error */
     900       12272 :             err = 0;
     901      748592 :             for (k = 0; k < SUBFRAME_LEN; k++) {
     902             :                 int64_t prod;
     903      736320 :                 prod = av_clipl_int32((int64_t) buf[k] * temp_corr[k] << 1);
     904      736320 :                 err  = av_clipl_int32(err - prod);
     905      736320 :                 prod = av_clipl_int32((int64_t) temp_corr[k] * temp_corr[k]);
     906      736320 :                 err  = av_clipl_int32(err + prod);
     907             :             }
     908             : 
     909             :             /* Minimize */
     910       12272 :             if (err < optim->min_err) {
     911        2638 :                 optim->min_err     = err;
     912        2638 :                 optim->grid_index  = i;
     913        2638 :                 optim->amp_index   = param.amp_index;
     914        2638 :                 optim->dirac_train = param.dirac_train;
     915             : 
     916       17164 :                 for (k = 0; k < pulse_cnt; k++) {
     917       14526 :                     optim->pulse_sign[k] = param.pulse_sign[k];
     918       14526 :                     optim->pulse_pos[k]  = param.pulse_pos[k];
     919             :                 }
     920             :             }
     921             :         }
     922             :     }
     923        1534 : }
     924             : 
     925             : /**
     926             :  * Encode the pulse position and gain of the current subframe.
     927             :  *
     928             :  * @param optim optimized fixed CB parameters
     929             :  * @param buf   excitation vector
     930             :  */
     931         800 : static void pack_fcb_param(G723_1_Subframe *subfrm, FCBParam *optim,
     932             :                            int16_t *buf, int pulse_cnt)
     933             : {
     934             :     int i, j;
     935             : 
     936         800 :     j = PULSE_MAX - pulse_cnt;
     937             : 
     938         800 :     subfrm->pulse_sign = 0;
     939         800 :     subfrm->pulse_pos  = 0;
     940             : 
     941       17850 :     for (i = 0; i < SUBFRAME_LEN >> 1; i++) {
     942       17850 :         int val = buf[optim->grid_index + (i << 1)];
     943       17850 :         if (!val) {
     944       13450 :             subfrm->pulse_pos += combinatorial_table[j][i];
     945             :         } else {
     946        4400 :             subfrm->pulse_sign <<= 1;
     947        4400 :             if (val < 0)
     948        2239 :                 subfrm->pulse_sign++;
     949        4400 :             j++;
     950             : 
     951        4400 :             if (j == PULSE_MAX)
     952         800 :                 break;
     953             :         }
     954             :     }
     955         800 :     subfrm->amp_index   = optim->amp_index;
     956         800 :     subfrm->grid_index  = optim->grid_index;
     957         800 :     subfrm->dirac_train = optim->dirac_train;
     958         800 : }
     959             : 
     960             : /**
     961             :  * Compute the fixed codebook excitation.
     962             :  *
     963             :  * @param buf          target vector
     964             :  * @param impulse_resp impulse response of the combined filter
     965             :  */
     966         800 : static void fcb_search(G723_1_Context *p, int16_t *impulse_resp,
     967             :                        int16_t *buf, int index)
     968             : {
     969             :     FCBParam optim;
     970         800 :     int pulse_cnt = pulses[index];
     971             :     int i;
     972             : 
     973         800 :     optim.min_err = 1 << 30;
     974         800 :     get_fcb_param(&optim, impulse_resp, buf, pulse_cnt, SUBFRAME_LEN);
     975             : 
     976         800 :     if (p->pitch_lag[index >> 1] < SUBFRAME_LEN - 2) {
     977         734 :         get_fcb_param(&optim, impulse_resp, buf, pulse_cnt,
     978         734 :                       p->pitch_lag[index >> 1]);
     979             :     }
     980             : 
     981             :     /* Reconstruct the excitation */
     982         800 :     memset(buf, 0, sizeof(int16_t) * SUBFRAME_LEN);
     983        5200 :     for (i = 0; i < pulse_cnt; i++)
     984        4400 :         buf[optim.pulse_pos[i]] = optim.pulse_sign[i];
     985             : 
     986         800 :     pack_fcb_param(&p->subframe[index], &optim, buf, pulse_cnt);
     987             : 
     988         800 :     if (optim.dirac_train)
     989         378 :         ff_g723_1_gen_dirac_train(buf, p->pitch_lag[index >> 1]);
     990         800 : }
     991             : 
     992             : /**
     993             :  * Pack the frame parameters into output bitstream.
     994             :  *
     995             :  * @param frame output buffer
     996             :  * @param size  size of the buffer
     997             :  */
     998         200 : static int pack_bitstream(G723_1_Context *p, AVPacket *avpkt)
     999             : {
    1000             :     PutBitContext pb;
    1001         200 :     int info_bits = 0;
    1002             :     int i, temp;
    1003             : 
    1004         200 :     init_put_bits(&pb, avpkt->data, avpkt->size);
    1005             : 
    1006         200 :     put_bits(&pb, 2, info_bits);
    1007             : 
    1008         200 :     put_bits(&pb, 8, p->lsp_index[2]);
    1009         200 :     put_bits(&pb, 8, p->lsp_index[1]);
    1010         200 :     put_bits(&pb, 8, p->lsp_index[0]);
    1011             : 
    1012         200 :     put_bits(&pb, 7, p->pitch_lag[0] - PITCH_MIN);
    1013         200 :     put_bits(&pb, 2, p->subframe[1].ad_cb_lag);
    1014         200 :     put_bits(&pb, 7, p->pitch_lag[1] - PITCH_MIN);
    1015         200 :     put_bits(&pb, 2, p->subframe[3].ad_cb_lag);
    1016             : 
    1017             :     /* Write 12 bit combined gain */
    1018        1000 :     for (i = 0; i < SUBFRAMES; i++) {
    1019        1600 :         temp = p->subframe[i].ad_cb_gain * GAIN_LEVELS +
    1020         800 :                p->subframe[i].amp_index;
    1021         800 :         if (p->cur_rate == RATE_6300)
    1022         800 :             temp += p->subframe[i].dirac_train << 11;
    1023         800 :         put_bits(&pb, 12, temp);
    1024             :     }
    1025             : 
    1026         200 :     put_bits(&pb, 1, p->subframe[0].grid_index);
    1027         200 :     put_bits(&pb, 1, p->subframe[1].grid_index);
    1028         200 :     put_bits(&pb, 1, p->subframe[2].grid_index);
    1029         200 :     put_bits(&pb, 1, p->subframe[3].grid_index);
    1030             : 
    1031         200 :     if (p->cur_rate == RATE_6300) {
    1032         200 :         skip_put_bits(&pb, 1); /* reserved bit */
    1033             : 
    1034             :         /* Write 13 bit combined position index */
    1035         600 :         temp = (p->subframe[0].pulse_pos >> 16) * 810 +
    1036         400 :                (p->subframe[1].pulse_pos >> 14) *  90 +
    1037         200 :                (p->subframe[2].pulse_pos >> 16) *   9 +
    1038         200 :                (p->subframe[3].pulse_pos >> 14);
    1039         200 :         put_bits(&pb, 13, temp);
    1040             : 
    1041         200 :         put_bits(&pb, 16, p->subframe[0].pulse_pos & 0xffff);
    1042         200 :         put_bits(&pb, 14, p->subframe[1].pulse_pos & 0x3fff);
    1043         200 :         put_bits(&pb, 16, p->subframe[2].pulse_pos & 0xffff);
    1044         200 :         put_bits(&pb, 14, p->subframe[3].pulse_pos & 0x3fff);
    1045             : 
    1046         200 :         put_bits(&pb, 6, p->subframe[0].pulse_sign);
    1047         200 :         put_bits(&pb, 5, p->subframe[1].pulse_sign);
    1048         200 :         put_bits(&pb, 6, p->subframe[2].pulse_sign);
    1049         200 :         put_bits(&pb, 5, p->subframe[3].pulse_sign);
    1050             :     }
    1051             : 
    1052         200 :     flush_put_bits(&pb);
    1053         200 :     return frame_size[info_bits];
    1054             : }
    1055             : 
    1056         200 : static int g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
    1057             :                                const AVFrame *frame, int *got_packet_ptr)
    1058             : {
    1059         200 :     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;
    1067             :     HFParam hf[4];
    1068             : 
    1069             :     /* duplicate input */
    1070         200 :     start = in = av_malloc(frame->nb_samples * sizeof(int16_t));
    1071         200 :     if (!in)
    1072           0 :         return AVERROR(ENOMEM);
    1073         200 :     memcpy(in, frame->data[0], frame->nb_samples * sizeof(int16_t));
    1074             : 
    1075         200 :     highpass_filter(in, &p->hpf_fir_mem, &p->hpf_iir_mem);
    1076             : 
    1077         200 :     memcpy(vector, p->prev_data, HALF_FRAME_LEN * sizeof(int16_t));
    1078         200 :     memcpy(vector + HALF_FRAME_LEN, in, FRAME_LEN * sizeof(int16_t));
    1079             : 
    1080         200 :     comp_lpc_coeff(vector, unq_lpc);
    1081         200 :     lpc2lsp(&unq_lpc[LPC_ORDER * 3], p->prev_lsp, cur_lsp);
    1082         200 :     lsp_quantize(p->lsp_index, cur_lsp, p->prev_lsp);
    1083             : 
    1084             :     /* Update memory */
    1085         200 :     memcpy(vector + LPC_ORDER, p->prev_data + SUBFRAME_LEN,
    1086             :            sizeof(int16_t) * SUBFRAME_LEN);
    1087         200 :     memcpy(vector + LPC_ORDER + SUBFRAME_LEN, in,
    1088             :            sizeof(int16_t) * (HALF_FRAME_LEN + SUBFRAME_LEN));
    1089         200 :     memcpy(p->prev_data, in + HALF_FRAME_LEN,
    1090             :            sizeof(int16_t) * HALF_FRAME_LEN);
    1091         200 :     memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
    1092             : 
    1093         200 :     perceptual_filter(p, weighted_lpc, unq_lpc, vector);
    1094             : 
    1095         200 :     memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
    1096         200 :     memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
    1097         200 :     memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
    1098             : 
    1099         200 :     ff_g723_1_scale_vector(vector, vector, FRAME_LEN + PITCH_MAX);
    1100             : 
    1101         200 :     p->pitch_lag[0] = estimate_pitch(vector, PITCH_MAX);
    1102         200 :     p->pitch_lag[1] = estimate_pitch(vector, PITCH_MAX + HALF_FRAME_LEN);
    1103             : 
    1104        1000 :     for (i = PITCH_MAX, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
    1105         800 :         comp_harmonic_coeff(vector + i, p->pitch_lag[j >> 1], hf + j);
    1106             : 
    1107         200 :     memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
    1108         200 :     memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
    1109         200 :     memcpy(p->prev_weight_sig, vector + FRAME_LEN, sizeof(int16_t) * PITCH_MAX);
    1110             : 
    1111        1000 :     for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
    1112         800 :         harmonic_filter(hf + j, vector + PITCH_MAX + i, in + i);
    1113             : 
    1114         200 :     ff_g723_1_inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, 0);
    1115         200 :     ff_g723_1_lsp_interpolate(qnt_lpc, cur_lsp, p->prev_lsp);
    1116             : 
    1117         200 :     memcpy(p->prev_lsp, cur_lsp, sizeof(int16_t) * LPC_ORDER);
    1118             : 
    1119         200 :     offset = 0;
    1120        1000 :     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];
    1125             : 
    1126             :         /**
    1127             :          * Compute the combined impulse response of the synthesis filter,
    1128             :          * formant perceptual weighting filter and harmonic noise shaping filter
    1129             :          */
    1130         800 :         memset(zero, 0, sizeof(int16_t) * LPC_ORDER);
    1131         800 :         memset(vector, 0, sizeof(int16_t) * PITCH_MAX);
    1132         800 :         memset(flt_in, 0, sizeof(int16_t) * SUBFRAME_LEN);
    1133             : 
    1134         800 :         flt_in[0] = 1 << 13; /* Unit impulse */
    1135         800 :         synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
    1136             :                              zero, zero, flt_in, vector + PITCH_MAX, 1);
    1137         800 :         harmonic_filter(hf + i, vector + PITCH_MAX, impulse_resp);
    1138             : 
    1139             :         /* Compute the combined zero input response */
    1140         800 :         flt_in[0] = 0;
    1141         800 :         memcpy(fir, p->perf_fir_mem, sizeof(int16_t) * LPC_ORDER);
    1142         800 :         memcpy(iir, p->perf_iir_mem, sizeof(int16_t) * LPC_ORDER);
    1143             : 
    1144         800 :         synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
    1145             :                              fir, iir, flt_in, vector + PITCH_MAX, 0);
    1146         800 :         memcpy(vector, p->harmonic_mem, sizeof(int16_t) * PITCH_MAX);
    1147         800 :         harmonic_noise_sub(hf + i, vector + PITCH_MAX, in);
    1148             : 
    1149         800 :         acb_search(p, residual, impulse_resp, in, i);
    1150        1600 :         ff_g723_1_gen_acb_excitation(residual, p->prev_excitation,
    1151         800 :                                      p->pitch_lag[i >> 1], &p->subframe[i],
    1152             :                                      p->cur_rate);
    1153         800 :         sub_acb_contrib(residual, impulse_resp, in);
    1154             : 
    1155         800 :         fcb_search(p, impulse_resp, in, i);
    1156             : 
    1157             :         /* Reconstruct the excitation */
    1158        1600 :         ff_g723_1_gen_acb_excitation(impulse_resp, p->prev_excitation,
    1159         800 :                                      p->pitch_lag[i >> 1], &p->subframe[i],
    1160             :                                      RATE_6300);
    1161             : 
    1162         800 :         memmove(p->prev_excitation, p->prev_excitation + SUBFRAME_LEN,
    1163             :                 sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
    1164       48800 :         for (j = 0; j < SUBFRAME_LEN; j++)
    1165       48000 :             in[j] = av_clip_int16((in[j] << 1) + impulse_resp[j]);
    1166         800 :         memcpy(p->prev_excitation + PITCH_MAX - SUBFRAME_LEN, in,
    1167             :                sizeof(int16_t) * SUBFRAME_LEN);
    1168             : 
    1169             :         /* Update filter memories */
    1170        1600 :         synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
    1171         800 :                              p->perf_fir_mem, p->perf_iir_mem,
    1172             :                              in, vector + PITCH_MAX, 0);
    1173         800 :         memmove(p->harmonic_mem, p->harmonic_mem + SUBFRAME_LEN,
    1174             :                 sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
    1175         800 :         memcpy(p->harmonic_mem + PITCH_MAX - SUBFRAME_LEN, vector + PITCH_MAX,
    1176             :                sizeof(int16_t) * SUBFRAME_LEN);
    1177             : 
    1178         800 :         in     += SUBFRAME_LEN;
    1179         800 :         offset += LPC_ORDER;
    1180             :     }
    1181             : 
    1182         200 :     av_free(start);
    1183             : 
    1184         200 :     if ((ret = ff_alloc_packet2(avctx, avpkt, 24, 0)) < 0)
    1185           0 :         return ret;
    1186             : 
    1187         200 :     *got_packet_ptr = 1;
    1188         200 :     avpkt->size = pack_bitstream(p, avpkt);
    1189         200 :     return 0;
    1190             : }
    1191             : 
    1192             : AVCodec ff_g723_1_encoder = {
    1193             :     .name           = "g723_1",
    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
    1202             :     },
    1203             : };

Generated by: LCOV version 1.13