2 * G.723.1 compatible decoder
3 * Copyright (c) 2006 Benjamin Larsson
4 * Copyright (c) 2010 Mohamed Naufal Basheer
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/common.h"
27 #include "acelp_vectors.h"
29 #include "celp_math.h"
32 int ff_g723_1_scale_vector(int16_t *dst, const int16_t *vector, int length)
37 for (i = 0; i < length; i++)
38 max |= FFABS(vector[i]);
40 bits= 14 - av_log2_16bit(max);
43 for (i = 0; i < length; i++)
44 dst[i] = (vector[i] * (1 << bits)) >> 3;
49 int ff_g723_1_normalize_bits(int num, int width)
51 return width - av_log2(num) - 1;
54 int ff_g723_1_dot_product(const int16_t *a, const int16_t *b, int length)
56 int sum = ff_dot_product(a, b, length);
57 return av_sat_add32(sum, sum);
60 void ff_g723_1_get_residual(int16_t *residual, int16_t *prev_excitation,
63 int offset = PITCH_MAX - PITCH_ORDER / 2 - lag;
66 residual[0] = prev_excitation[offset];
67 residual[1] = prev_excitation[offset + 1];
70 for (i = 2; i < SUBFRAME_LEN + PITCH_ORDER - 1; i++)
71 residual[i] = prev_excitation[offset + (i - 2) % lag];
74 void ff_g723_1_gen_dirac_train(int16_t *buf, int pitch_lag)
76 int16_t vector[SUBFRAME_LEN];
79 memcpy(vector, buf, SUBFRAME_LEN * sizeof(*vector));
80 for (i = pitch_lag; i < SUBFRAME_LEN; i += pitch_lag) {
81 for (j = 0; j < SUBFRAME_LEN - i; j++)
82 buf[i + j] += vector[j];
86 void ff_g723_1_gen_acb_excitation(int16_t *vector, int16_t *prev_excitation,
87 int pitch_lag, G723_1_Subframe *subfrm,
90 int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
91 const int16_t *cb_ptr;
92 int lag = pitch_lag + subfrm->ad_cb_lag - 1;
97 ff_g723_1_get_residual(residual, prev_excitation, lag);
99 /* Select quantization table */
100 if (cur_rate == RATE_6300 && pitch_lag < SUBFRAME_LEN - 2) {
101 cb_ptr = adaptive_cb_gain85;
103 cb_ptr = adaptive_cb_gain170;
105 /* Calculate adaptive vector */
106 cb_ptr += subfrm->ad_cb_gain * 20;
107 for (i = 0; i < SUBFRAME_LEN; i++) {
108 sum = ff_dot_product(residual + i, cb_ptr, PITCH_ORDER);
109 vector[i] = av_sat_dadd32(1 << 15, av_sat_add32(sum, sum)) >> 16;
114 * Convert LSP frequencies to LPC coefficients.
116 * @param lpc buffer for LPC coefficients
118 static void lsp2lpc(int16_t *lpc)
120 int f1[LPC_ORDER / 2 + 1];
121 int f2[LPC_ORDER / 2 + 1];
124 /* Calculate negative cosine */
125 for (j = 0; j < LPC_ORDER; j++) {
126 int index = (lpc[j] >> 7) & 0x1FF;
127 int offset = lpc[j] & 0x7f;
128 int temp1 = cos_tab[index] * (1 << 16);
129 int temp2 = (cos_tab[index + 1] - cos_tab[index]) *
130 (((offset << 8) + 0x80) << 1);
132 lpc[j] = -(av_sat_dadd32(1 << 15, temp1 + temp2) >> 16);
136 * Compute sum and difference polynomial coefficients
137 * (bitexact alternative to lsp2poly() in lsp.c)
139 /* Initialize with values in Q28 */
141 f1[1] = (lpc[0] + lpc[2]) * (1 << 14);
142 f1[2] = lpc[0] * lpc[2] + (2 << 28);
145 f2[1] = (lpc[1] + lpc[3]) * (1 << 14);
146 f2[2] = lpc[1] * lpc[3] + (2 << 28);
149 * Calculate and scale the coefficients by 1/2 in
150 * each iteration for a final scaling factor of Q25
152 for (i = 2; i < LPC_ORDER / 2; i++) {
153 f1[i + 1] = av_clipl_int32(f1[i - 1] + (int64_t)MULL2(f1[i], lpc[2 * i]));
154 f2[i + 1] = av_clipl_int32(f2[i - 1] + (int64_t)MULL2(f2[i], lpc[2 * i + 1]));
156 for (j = i; j >= 2; j--) {
157 f1[j] = MULL2(f1[j - 1], lpc[2 * i]) +
158 (f1[j] >> 1) + (f1[j - 2] >> 1);
159 f2[j] = MULL2(f2[j - 1], lpc[2 * i + 1]) +
160 (f2[j] >> 1) + (f2[j - 2] >> 1);
165 f1[1] = ((lpc[2 * i] * 65536 >> i) + f1[1]) >> 1;
166 f2[1] = ((lpc[2 * i + 1] * 65536 >> i) + f2[1]) >> 1;
169 /* Convert polynomial coefficients to LPC coefficients */
170 for (i = 0; i < LPC_ORDER / 2; i++) {
171 int64_t ff1 = f1[i + 1] + f1[i];
172 int64_t ff2 = f2[i + 1] - f2[i];
174 lpc[i] = av_clipl_int32(((ff1 + ff2) * 8) + (1 << 15)) >> 16;
175 lpc[LPC_ORDER - i - 1] = av_clipl_int32(((ff1 - ff2) * 8) +
180 void ff_g723_1_lsp_interpolate(int16_t *lpc, int16_t *cur_lsp,
184 int16_t *lpc_ptr = lpc;
186 /* cur_lsp * 0.25 + prev_lsp * 0.75 */
187 ff_acelp_weighted_vector_sum(lpc, cur_lsp, prev_lsp,
188 4096, 12288, 1 << 13, 14, LPC_ORDER);
189 ff_acelp_weighted_vector_sum(lpc + LPC_ORDER, cur_lsp, prev_lsp,
190 8192, 8192, 1 << 13, 14, LPC_ORDER);
191 ff_acelp_weighted_vector_sum(lpc + 2 * LPC_ORDER, cur_lsp, prev_lsp,
192 12288, 4096, 1 << 13, 14, LPC_ORDER);
193 memcpy(lpc + 3 * LPC_ORDER, cur_lsp, LPC_ORDER * sizeof(*lpc));
195 for (i = 0; i < SUBFRAMES; i++) {
197 lpc_ptr += LPC_ORDER;
201 void ff_g723_1_inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp,
202 uint8_t *lsp_index, int bad_frame)
205 int i, j, temp, stable;
207 /* Check for frame erasure */
214 lsp_index[0] = lsp_index[1] = lsp_index[2] = 0;
217 /* Get the VQ table entry corresponding to the transmitted index */
218 cur_lsp[0] = lsp_band0[lsp_index[0]][0];
219 cur_lsp[1] = lsp_band0[lsp_index[0]][1];
220 cur_lsp[2] = lsp_band0[lsp_index[0]][2];
221 cur_lsp[3] = lsp_band1[lsp_index[1]][0];
222 cur_lsp[4] = lsp_band1[lsp_index[1]][1];
223 cur_lsp[5] = lsp_band1[lsp_index[1]][2];
224 cur_lsp[6] = lsp_band2[lsp_index[2]][0];
225 cur_lsp[7] = lsp_band2[lsp_index[2]][1];
226 cur_lsp[8] = lsp_band2[lsp_index[2]][2];
227 cur_lsp[9] = lsp_band2[lsp_index[2]][3];
229 /* Add predicted vector & DC component to the previously quantized vector */
230 for (i = 0; i < LPC_ORDER; i++) {
231 temp = ((prev_lsp[i] - dc_lsp[i]) * pred + (1 << 14)) >> 15;
232 cur_lsp[i] += dc_lsp[i] + temp;
235 for (i = 0; i < LPC_ORDER; i++) {
236 cur_lsp[0] = FFMAX(cur_lsp[0], 0x180);
237 cur_lsp[LPC_ORDER - 1] = FFMIN(cur_lsp[LPC_ORDER - 1], 0x7e00);
239 /* Stability check */
240 for (j = 1; j < LPC_ORDER; j++) {
241 temp = min_dist + cur_lsp[j - 1] - cur_lsp[j];
244 cur_lsp[j - 1] -= temp;
249 for (j = 1; j < LPC_ORDER; j++) {
250 temp = cur_lsp[j - 1] + min_dist - cur_lsp[j] - 4;
260 memcpy(cur_lsp, prev_lsp, LPC_ORDER * sizeof(*cur_lsp));