- int16_t vector[SUBFRAME_LEN];
- int i, j;
-
- memcpy(vector, buf, SUBFRAME_LEN * sizeof(*vector));
- for (i = pitch_lag; i < SUBFRAME_LEN; i += pitch_lag) {
- for (j = 0; j < SUBFRAME_LEN - i; j++)
- buf[i + j] += vector[j];
- }
-}
-
-/**
- * Generate fixed codebook excitation vector.
- *
- * @param vector decoded excitation vector
- * @param subfrm current subframe
- * @param cur_rate current bitrate
- * @param pitch_lag closed loop pitch lag
- * @param index current subframe index
- */
-static void gen_fcb_excitation(int16_t *vector, G723_1_Subframe *subfrm,
- enum Rate cur_rate, int pitch_lag, int index)
-{
- int temp, i, j;
-
- memset(vector, 0, SUBFRAME_LEN * sizeof(*vector));
-
- if (cur_rate == RATE_6300) {
- if (subfrm->pulse_pos >= max_pos[index])
- return;
-
- /* Decode amplitudes and positions */
- j = PULSE_MAX - pulses[index];
- temp = subfrm->pulse_pos;
- for (i = 0; i < SUBFRAME_LEN / GRID_SIZE; i++) {
- temp -= combinatorial_table[j][i];
- if (temp >= 0)
- continue;
- temp += combinatorial_table[j++][i];
- if (subfrm->pulse_sign & (1 << (PULSE_MAX - j))) {
- vector[subfrm->grid_index + GRID_SIZE * i] =
- -fixed_cb_gain[subfrm->amp_index];
- } else {
- vector[subfrm->grid_index + GRID_SIZE * i] =
- fixed_cb_gain[subfrm->amp_index];
- }
- if (j == PULSE_MAX)
- break;
- }
- if (subfrm->dirac_train == 1)
- gen_dirac_train(vector, pitch_lag);
- } else { /* 5300 bps */
- int cb_gain = fixed_cb_gain[subfrm->amp_index];
- int cb_shift = subfrm->grid_index;
- int cb_sign = subfrm->pulse_sign;
- int cb_pos = subfrm->pulse_pos;
- int offset, beta, lag;
-
- for (i = 0; i < 8; i += 2) {
- offset = ((cb_pos & 7) << 3) + cb_shift + i;
- vector[offset] = (cb_sign & 1) ? cb_gain : -cb_gain;
- cb_pos >>= 3;
- cb_sign >>= 1;
- }
-
- /* Enhance harmonic components */
- lag = pitch_contrib[subfrm->ad_cb_gain << 1] + pitch_lag +
- subfrm->ad_cb_lag - 1;
- beta = pitch_contrib[(subfrm->ad_cb_gain << 1) + 1];
-
- if (lag < SUBFRAME_LEN - 2) {
- for (i = lag; i < SUBFRAME_LEN; i++)
- vector[i] += beta * vector[i - lag] >> 15;
- }
- }
-}
-
-/**
- * Get delayed contribution from the previous excitation vector.
- */
-static void get_residual(int16_t *residual, int16_t *prev_excitation, int lag)
-{
- int offset = PITCH_MAX - PITCH_ORDER / 2 - lag;
- int i;
-
- residual[0] = prev_excitation[offset];
- residual[1] = prev_excitation[offset + 1];
-
- offset += 2;
- for (i = 2; i < SUBFRAME_LEN + PITCH_ORDER - 1; i++)
- residual[i] = prev_excitation[offset + (i - 2) % lag];
-}
-
-static int dot_product(const int16_t *a, const int16_t *b, int length)
-{
- int i, sum = 0;
-
- for (i = 0; i < length; i++) {
- int prod = a[i] * b[i];
- sum = av_sat_dadd32(sum, prod);
- }
- return sum;
-}
-
-/**
- * Generate adaptive codebook excitation.
- */
-static void gen_acb_excitation(int16_t *vector, int16_t *prev_excitation,
- int pitch_lag, G723_1_Subframe *subfrm,
- enum Rate cur_rate)
-{
- int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
- const int16_t *cb_ptr;
- int lag = pitch_lag + subfrm->ad_cb_lag - 1;
-
- int i;
- int sum;
-
- get_residual(residual, prev_excitation, lag);
-
- /* Select quantization table */
- if (cur_rate == RATE_6300 && pitch_lag < SUBFRAME_LEN - 2)
- cb_ptr = adaptive_cb_gain85;
- else
- cb_ptr = adaptive_cb_gain170;
-
- /* Calculate adaptive vector */
- cb_ptr += subfrm->ad_cb_gain * 20;
- for (i = 0; i < SUBFRAME_LEN; i++) {
- sum = dot_product(residual + i, cb_ptr, PITCH_ORDER);
- vector[i] = av_sat_dadd32(1 << 15, sum) >> 16;
- }
-}
-
-/**
- * Estimate maximum auto-correlation around pitch lag.
- *
- * @param buf buffer with offset applied
- * @param offset offset of the excitation vector
- * @param ccr_max pointer to the maximum auto-correlation
- * @param pitch_lag decoded pitch lag
- * @param length length of autocorrelation
- * @param dir forward lag(1) / backward lag(-1)
- */
-static int autocorr_max(const int16_t *buf, int offset, int *ccr_max,
- int pitch_lag, int length, int dir)
-{
- int limit, ccr, lag = 0;
- int i;
-
- pitch_lag = FFMIN(PITCH_MAX - 3, pitch_lag);
- if (dir > 0)
- limit = FFMIN(FRAME_LEN + PITCH_MAX - offset - length, pitch_lag + 3);
- else
- limit = pitch_lag + 3;
-
- for (i = pitch_lag - 3; i <= limit; i++) {
- ccr = dot_product(buf, buf + dir * i, length);
-
- if (ccr > *ccr_max) {
- *ccr_max = ccr;
- lag = i;
- }
- }
- return lag;
-}
-
-/**
- * Calculate pitch postfilter optimal and scaling gains.
- *
- * @param lag pitch postfilter forward/backward lag
- * @param ppf pitch postfilter parameters
- * @param cur_rate current bitrate
- * @param tgt_eng target energy
- * @param ccr cross-correlation
- * @param res_eng residual energy
- */
-static void comp_ppf_gains(int lag, PPFParam *ppf, enum Rate cur_rate,
- int tgt_eng, int ccr, int res_eng)
-{
- int pf_residual; /* square of postfiltered residual */
- int temp1, temp2;
-
- ppf->index = lag;
-
- temp1 = tgt_eng * res_eng >> 1;
- temp2 = ccr * ccr << 1;
-
- if (temp2 > temp1) {
- if (ccr >= res_eng) {
- ppf->opt_gain = ppf_gain_weight[cur_rate];
- } else {
- ppf->opt_gain = (ccr << 15) / res_eng *
- ppf_gain_weight[cur_rate] >> 15;
- }
- /* pf_res^2 = tgt_eng + 2*ccr*gain + res_eng*gain^2 */
- temp1 = (tgt_eng << 15) + (ccr * ppf->opt_gain << 1);
- temp2 = (ppf->opt_gain * ppf->opt_gain >> 15) * res_eng;
- pf_residual = av_sat_add32(temp1, temp2 + (1 << 15)) >> 16;
-
- if (tgt_eng >= pf_residual << 1) {
- temp1 = 0x7fff;
- } else {
- temp1 = (tgt_eng << 14) / pf_residual;
- }
-
- /* scaling_gain = sqrt(tgt_eng/pf_res^2) */
- ppf->sc_gain = square_root(temp1 << 16);
- } else {
- ppf->opt_gain = 0;
- ppf->sc_gain = 0x7fff;
- }
-
- ppf->opt_gain = av_clip_int16(ppf->opt_gain * ppf->sc_gain >> 15);
-}
-
-/**
- * Calculate pitch postfilter parameters.
- *
- * @param p the context
- * @param offset offset of the excitation vector
- * @param pitch_lag decoded pitch lag
- * @param ppf pitch postfilter parameters
- * @param cur_rate current bitrate
- */
-static void comp_ppf_coeff(G723_1_Context *p, int offset, int pitch_lag,
- PPFParam *ppf, enum Rate cur_rate)
-{
-
- int16_t scale;
- int i;
- int temp1, temp2;
-
- /*
- * 0 - target energy
- * 1 - forward cross-correlation
- * 2 - forward residual energy
- * 3 - backward cross-correlation
- * 4 - backward residual energy
- */
- int energy[5] = {0, 0, 0, 0, 0};
- int16_t *buf = p->audio + LPC_ORDER + offset;
- int fwd_lag = autocorr_max(buf, offset, &energy[1], pitch_lag,
- SUBFRAME_LEN, 1);
- int back_lag = autocorr_max(buf, offset, &energy[3], pitch_lag,
- SUBFRAME_LEN, -1);
-
- ppf->index = 0;
- ppf->opt_gain = 0;
- ppf->sc_gain = 0x7fff;
-
- /* Case 0, Section 3.6 */
- if (!back_lag && !fwd_lag)
- return;
-
- /* Compute target energy */
- energy[0] = dot_product(buf, buf, SUBFRAME_LEN);
-
- /* Compute forward residual energy */
- if (fwd_lag)
- energy[2] = dot_product(buf + fwd_lag, buf + fwd_lag, SUBFRAME_LEN);
-
- /* Compute backward residual energy */
- if (back_lag)
- energy[4] = dot_product(buf - back_lag, buf - back_lag, SUBFRAME_LEN);
-
- /* Normalize and shorten */
- temp1 = 0;
- for (i = 0; i < 5; i++)
- temp1 = FFMAX(energy[i], temp1);
-
- scale = normalize_bits(temp1, 31);
- for (i = 0; i < 5; i++)
- energy[i] = (energy[i] << scale) >> 16;
-
- if (fwd_lag && !back_lag) { /* Case 1 */
- comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
- energy[2]);
- } else if (!fwd_lag) { /* Case 2 */
- comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
- energy[4]);
- } else { /* Case 3 */
-
- /*
- * Select the largest of energy[1]^2/energy[2]
- * and energy[3]^2/energy[4]
- */
- temp1 = energy[4] * ((energy[1] * energy[1] + (1 << 14)) >> 15);
- temp2 = energy[2] * ((energy[3] * energy[3] + (1 << 14)) >> 15);
- if (temp1 >= temp2) {
- comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
- energy[2]);
- } else {
- comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
- energy[4]);
- }
- }
-}
-
-/**
- * Classify frames as voiced/unvoiced.
- *
- * @param p the context
- * @param pitch_lag decoded pitch_lag
- * @param exc_eng excitation energy estimation
- * @param scale scaling factor of exc_eng
- *
- * @return residual interpolation index if voiced, 0 otherwise
- */
-static int comp_interp_index(G723_1_Context *p, int pitch_lag,
- int *exc_eng, int *scale)
-{
- int offset = PITCH_MAX + 2 * SUBFRAME_LEN;
- int16_t *buf = p->audio + LPC_ORDER;
-
- int index, ccr, tgt_eng, best_eng, temp;
-
- *scale = scale_vector(buf, p->excitation, FRAME_LEN + PITCH_MAX);
- buf += offset;
-
- /* Compute maximum backward cross-correlation */
- ccr = 0;
- index = autocorr_max(buf, offset, &ccr, pitch_lag, SUBFRAME_LEN * 2, -1);
- ccr = av_sat_add32(ccr, 1 << 15) >> 16;
-
- /* Compute target energy */
- tgt_eng = dot_product(buf, buf, SUBFRAME_LEN * 2);
- *exc_eng = av_sat_add32(tgt_eng, 1 << 15) >> 16;
-
- if (ccr <= 0)
- return 0;
-
- /* Compute best energy */
- best_eng = dot_product(buf - index, buf - index, SUBFRAME_LEN * 2);
- best_eng = av_sat_add32(best_eng, 1 << 15) >> 16;
-
- temp = best_eng * *exc_eng >> 3;
-
- if (temp < ccr * ccr)
- return index;
- else
- return 0;
-}
-
-/**
- * Peform residual interpolation based on frame classification.
- *
- * @param buf decoded excitation vector
- * @param out output vector
- * @param lag decoded pitch lag
- * @param gain interpolated gain
- * @param rseed seed for random number generator
- */
-static void residual_interp(int16_t *buf, int16_t *out, int lag,
- int gain, int *rseed)
-{
- int i;
- if (lag) { /* Voiced */
- int16_t *vector_ptr = buf + PITCH_MAX;
- /* Attenuate */
- for (i = 0; i < lag; i++)
- out[i] = vector_ptr[i - lag] * 3 >> 2;
- av_memcpy_backptr((uint8_t*)(out + lag), lag * sizeof(*out),
- (FRAME_LEN - lag) * sizeof(*out));
- } else { /* Unvoiced */
- for (i = 0; i < FRAME_LEN; i++) {
- *rseed = *rseed * 521 + 259;
- out[i] = gain * *rseed >> 15;
- }
- memset(buf, 0, (FRAME_LEN + PITCH_MAX) * sizeof(*buf));
- }
-}
-
-/**
- * Perform IIR filtering.
- *
- * @param fir_coef FIR coefficients
- * @param iir_coef IIR coefficients
- * @param src source vector
- * @param dest destination vector
- */
-static inline void iir_filter(int16_t *fir_coef, int16_t *iir_coef,
- int16_t *src, int *dest)
-{
- int m, n;
-
- for (m = 0; m < SUBFRAME_LEN; m++) {
- int64_t filter = 0;
- for (n = 1; n <= LPC_ORDER; n++) {
- filter -= fir_coef[n - 1] * src[m - n] -
- iir_coef[n - 1] * (dest[m - n] >> 16);
- }
-
- dest[m] = av_clipl_int32((src[m] << 16) + (filter << 3) + (1 << 15));
- }
-}
-
-/**
- * Adjust gain of postfiltered signal.
- *
- * @param p the context
- * @param buf postfiltered output vector
- * @param energy input energy coefficient
- */
-static void gain_scale(G723_1_Context *p, int16_t * buf, int energy)
-{
- int num, denom, gain, bits1, bits2;
- int i;
-
- num = energy;
- denom = 0;
- for (i = 0; i < SUBFRAME_LEN; i++) {
- int temp = buf[i] >> 2;
- temp *= temp;
- denom = av_sat_dadd32(denom, temp);
- }
-
- if (num && denom) {
- bits1 = normalize_bits(num, 31);
- bits2 = normalize_bits(denom, 31);
- num = num << bits1 >> 1;
- denom <<= bits2;
-
- bits2 = 5 + bits1 - bits2;
- bits2 = FFMAX(0, bits2);
-
- gain = (num >> 1) / (denom >> 16);
- gain = square_root(gain << 16 >> bits2);
- } else {
- gain = 1 << 12;
- }
-
- for (i = 0; i < SUBFRAME_LEN; i++) {
- p->pf_gain = (15 * p->pf_gain + gain + (1 << 3)) >> 4;
- buf[i] = av_clip_int16((buf[i] * (p->pf_gain + (p->pf_gain >> 4)) +
- (1 << 10)) >> 11);
- }
-}
-
-/**
- * Perform formant filtering.
- *
- * @param p the context
- * @param lpc quantized lpc coefficients
- * @param buf input buffer
- * @param dst output buffer
- */
-static void formant_postfilter(G723_1_Context *p, int16_t *lpc,
- int16_t *buf, int16_t *dst)
-{
- int16_t filter_coef[2][LPC_ORDER];
- int filter_signal[LPC_ORDER + FRAME_LEN], *signal_ptr;
- int i, j, k;
-
- memcpy(buf, p->fir_mem, LPC_ORDER * sizeof(*buf));
- memcpy(filter_signal, p->iir_mem, LPC_ORDER * sizeof(*filter_signal));
-
- for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
- for (k = 0; k < LPC_ORDER; k++) {
- filter_coef[0][k] = (-lpc[k] * postfilter_tbl[0][k] +
- (1 << 14)) >> 15;
- filter_coef[1][k] = (-lpc[k] * postfilter_tbl[1][k] +
- (1 << 14)) >> 15;
- }
- iir_filter(filter_coef[0], filter_coef[1], buf + i,
- filter_signal + i);
- lpc += LPC_ORDER;
- }
-
- memcpy(p->fir_mem, buf + FRAME_LEN, LPC_ORDER * sizeof(*p->fir_mem));
- memcpy(p->iir_mem, filter_signal + FRAME_LEN,
- LPC_ORDER * sizeof(*p->iir_mem));
-
- buf += LPC_ORDER;
- signal_ptr = filter_signal + LPC_ORDER;
- for (i = 0; i < SUBFRAMES; i++) {
- int temp;
- int auto_corr[2];
- int scale, energy;
-
- /* Normalize */
- scale = scale_vector(dst, buf, SUBFRAME_LEN);
-
- /* Compute auto correlation coefficients */
- auto_corr[0] = dot_product(dst, dst + 1, SUBFRAME_LEN - 1);
- auto_corr[1] = dot_product(dst, dst, SUBFRAME_LEN);
-
- /* Compute reflection coefficient */
- temp = auto_corr[1] >> 16;
- if (temp) {
- temp = (auto_corr[0] >> 2) / temp;
- }
- p->reflection_coef = (3 * p->reflection_coef + temp + 2) >> 2;
- temp = -p->reflection_coef >> 1 & ~3;
-
- /* Compensation filter */
- for (j = 0; j < SUBFRAME_LEN; j++) {
- dst[j] = av_sat_dadd32(signal_ptr[j],
- (signal_ptr[j - 1] >> 16) * temp) >> 16;
- }
-
- /* Compute normalized signal energy */
- temp = 2 * scale + 4;
- if (temp < 0) {
- energy = av_clipl_int32((int64_t)auto_corr[1] << -temp);
- } else
- energy = auto_corr[1] >> temp;
-
- gain_scale(p, dst, energy);
-
- buf += SUBFRAME_LEN;
- signal_ptr += SUBFRAME_LEN;
- dst += SUBFRAME_LEN;
- }
-}
-
-static int sid_gain_to_lsp_index(int gain)
-{
- if (gain < 0x10)
- return gain << 6;
- else if (gain < 0x20)
- return gain - 8 << 7;
- else
- return gain - 20 << 8;
-}
-
-static inline int cng_rand(int *state, int base)
-{
- *state = (*state * 521 + 259) & 0xFFFF;
- return (*state & 0x7FFF) * base >> 15;
-}
-
-static int estimate_sid_gain(G723_1_Context *p)
-{
- int i, shift, seg, seg2, t, val, val_add, x, y;
-
- shift = 16 - p->cur_gain * 2;
- if (shift > 0)
- t = p->sid_gain << shift;
- else
- t = p->sid_gain >> -shift;
- x = t * cng_filt[0] >> 16;
-
- if (x >= cng_bseg[2])
- return 0x3F;
-
- if (x >= cng_bseg[1]) {
- shift = 4;
- seg = 3;
- } else {
- shift = 3;
- seg = (x >= cng_bseg[0]);
- }
- seg2 = FFMIN(seg, 3);
-
- val = 1 << shift;
- val_add = val >> 1;
- for (i = 0; i < shift; i++) {
- t = seg * 32 + (val << seg2);
- t *= t;
- if (x >= t)
- val += val_add;
- else
- val -= val_add;
- val_add >>= 1;
- }