af4777cc355129e0422cb0c0275d36df322c73e9
[libav.git] / libavcodec / g723_1.c
1 /*
2 * G.723.1 compatible decoder
3 * Copyright (c) 2006 Benjamin Larsson
4 * Copyright (c) 2010 Mohamed Naufal Basheer
5 *
6 * This file is part of Libav.
7 *
8 * Libav 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.
12 *
13 * Libav 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.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <stdint.h>
24
25 #include "libavutil/common.h"
26
27 #include "acelp_vectors.h"
28 #include "avcodec.h"
29 #include "celp_math.h"
30 #include "g723_1.h"
31
32 int ff_g723_1_scale_vector(int16_t *dst, const int16_t *vector, int length)
33 {
34 int bits, max = 0;
35 int i;
36
37 for (i = 0; i < length; i++)
38 max |= FFABS(vector[i]);
39
40 max = FFMIN(max, 0x7FFF);
41 bits = ff_g723_1_normalize_bits(max, 15);
42
43 for (i = 0; i < length; i++)
44 dst[i] = vector[i] << bits >> 3;
45
46 return bits - 3;
47 }
48
49 int ff_g723_1_normalize_bits(int num, int width)
50 {
51 return width - av_log2(num) - 1;
52 }
53
54 int ff_g723_1_dot_product(const int16_t *a, const int16_t *b, int length)
55 {
56 int i, sum = 0;
57
58 for (i = 0; i < length; i++) {
59 int prod = a[i] * b[i];
60 sum = av_sat_dadd32(sum, prod);
61 }
62 return sum;
63 }
64
65 void ff_g723_1_get_residual(int16_t *residual, int16_t *prev_excitation,
66 int lag)
67 {
68 int offset = PITCH_MAX - PITCH_ORDER / 2 - lag;
69 int i;
70
71 residual[0] = prev_excitation[offset];
72 residual[1] = prev_excitation[offset + 1];
73
74 offset += 2;
75 for (i = 2; i < SUBFRAME_LEN + PITCH_ORDER - 1; i++)
76 residual[i] = prev_excitation[offset + (i - 2) % lag];
77 }
78
79 void ff_g723_1_gen_dirac_train(int16_t *buf, int pitch_lag)
80 {
81 int16_t vector[SUBFRAME_LEN];
82 int i, j;
83
84 memcpy(vector, buf, SUBFRAME_LEN * sizeof(*vector));
85 for (i = pitch_lag; i < SUBFRAME_LEN; i += pitch_lag) {
86 for (j = 0; j < SUBFRAME_LEN - i; j++)
87 buf[i + j] += vector[j];
88 }
89 }
90
91 void ff_g723_1_gen_acb_excitation(int16_t *vector, int16_t *prev_excitation,
92 int pitch_lag, G723_1_Subframe *subfrm,
93 enum Rate cur_rate)
94 {
95 int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
96 const int16_t *cb_ptr;
97 int lag = pitch_lag + subfrm->ad_cb_lag - 1;
98
99 int i;
100 int sum;
101
102 ff_g723_1_get_residual(residual, prev_excitation, lag);
103
104 /* Select quantization table */
105 if (cur_rate == RATE_6300 && pitch_lag < SUBFRAME_LEN - 2)
106 cb_ptr = adaptive_cb_gain85;
107 else
108 cb_ptr = adaptive_cb_gain170;
109
110 /* Calculate adaptive vector */
111 cb_ptr += subfrm->ad_cb_gain * 20;
112 for (i = 0; i < SUBFRAME_LEN; i++) {
113 sum = ff_g723_1_dot_product(residual + i, cb_ptr, PITCH_ORDER);
114 vector[i] = av_sat_dadd32(1 << 15, sum) >> 16;
115 }
116 }
117
118 /**
119 * Convert LSP frequencies to LPC coefficients.
120 *
121 * @param lpc buffer for LPC coefficients
122 */
123 static void lsp2lpc(int16_t *lpc)
124 {
125 int f1[LPC_ORDER / 2 + 1];
126 int f2[LPC_ORDER / 2 + 1];
127 int i, j;
128
129 /* Calculate negative cosine */
130 for (j = 0; j < LPC_ORDER; j++) {
131 int index = (lpc[j] >> 7) & 0x1FF;
132 int offset = lpc[j] & 0x7f;
133 int temp1 = cos_tab[index] << 16;
134 int temp2 = (cos_tab[index + 1] - cos_tab[index]) *
135 ((offset << 8) + 0x80) << 1;
136
137 lpc[j] = -(av_sat_dadd32(1 << 15, temp1 + temp2) >> 16);
138 }
139
140 /*
141 * Compute sum and difference polynomial coefficients
142 * (bitexact alternative to lsp2poly() in lsp.c)
143 */
144 /* Initialize with values in Q28 */
145 f1[0] = 1 << 28;
146 f1[1] = (lpc[0] << 14) + (lpc[2] << 14);
147 f1[2] = lpc[0] * lpc[2] + (2 << 28);
148
149 f2[0] = 1 << 28;
150 f2[1] = (lpc[1] << 14) + (lpc[3] << 14);
151 f2[2] = lpc[1] * lpc[3] + (2 << 28);
152
153 /*
154 * Calculate and scale the coefficients by 1/2 in
155 * each iteration for a final scaling factor of Q25
156 */
157 for (i = 2; i < LPC_ORDER / 2; i++) {
158 f1[i + 1] = f1[i - 1] + MULL2(f1[i], lpc[2 * i]);
159 f2[i + 1] = f2[i - 1] + MULL2(f2[i], lpc[2 * i + 1]);
160
161 for (j = i; j >= 2; j--) {
162 f1[j] = MULL2(f1[j - 1], lpc[2 * i]) +
163 (f1[j] >> 1) + (f1[j - 2] >> 1);
164 f2[j] = MULL2(f2[j - 1], lpc[2 * i + 1]) +
165 (f2[j] >> 1) + (f2[j - 2] >> 1);
166 }
167
168 f1[0] >>= 1;
169 f2[0] >>= 1;
170 f1[1] = ((lpc[2 * i] << 16 >> i) + f1[1]) >> 1;
171 f2[1] = ((lpc[2 * i + 1] << 16 >> i) + f2[1]) >> 1;
172 }
173
174 /* Convert polynomial coefficients to LPC coefficients */
175 for (i = 0; i < LPC_ORDER / 2; i++) {
176 int64_t ff1 = f1[i + 1] + f1[i];
177 int64_t ff2 = f2[i + 1] - f2[i];
178
179 lpc[i] = av_clipl_int32(((ff1 + ff2) << 3) +
180 (1 << 15)) >> 16;
181 lpc[LPC_ORDER - i - 1] = av_clipl_int32(((ff1 - ff2) << 3) +
182 (1 << 15)) >> 16;
183 }
184 }
185
186 void ff_g723_1_lsp_interpolate(int16_t *lpc, int16_t *cur_lsp,
187 int16_t *prev_lsp)
188 {
189 int i;
190 int16_t *lpc_ptr = lpc;
191
192 /* cur_lsp * 0.25 + prev_lsp * 0.75 */
193 ff_acelp_weighted_vector_sum(lpc, cur_lsp, prev_lsp,
194 4096, 12288, 1 << 13, 14, LPC_ORDER);
195 ff_acelp_weighted_vector_sum(lpc + LPC_ORDER, cur_lsp, prev_lsp,
196 8192, 8192, 1 << 13, 14, LPC_ORDER);
197 ff_acelp_weighted_vector_sum(lpc + 2 * LPC_ORDER, cur_lsp, prev_lsp,
198 12288, 4096, 1 << 13, 14, LPC_ORDER);
199 memcpy(lpc + 3 * LPC_ORDER, cur_lsp, LPC_ORDER * sizeof(*lpc));
200
201 for (i = 0; i < SUBFRAMES; i++) {
202 lsp2lpc(lpc_ptr);
203 lpc_ptr += LPC_ORDER;
204 }
205 }
206
207 void ff_g723_1_inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp,
208 uint8_t *lsp_index, int bad_frame)
209 {
210 int min_dist, pred;
211 int i, j, temp, stable;
212
213 /* Check for frame erasure */
214 if (!bad_frame) {
215 min_dist = 0x100;
216 pred = 12288;
217 } else {
218 min_dist = 0x200;
219 pred = 23552;
220 lsp_index[0] = lsp_index[1] = lsp_index[2] = 0;
221 }
222
223 /* Get the VQ table entry corresponding to the transmitted index */
224 cur_lsp[0] = lsp_band0[lsp_index[0]][0];
225 cur_lsp[1] = lsp_band0[lsp_index[0]][1];
226 cur_lsp[2] = lsp_band0[lsp_index[0]][2];
227 cur_lsp[3] = lsp_band1[lsp_index[1]][0];
228 cur_lsp[4] = lsp_band1[lsp_index[1]][1];
229 cur_lsp[5] = lsp_band1[lsp_index[1]][2];
230 cur_lsp[6] = lsp_band2[lsp_index[2]][0];
231 cur_lsp[7] = lsp_band2[lsp_index[2]][1];
232 cur_lsp[8] = lsp_band2[lsp_index[2]][2];
233 cur_lsp[9] = lsp_band2[lsp_index[2]][3];
234
235 /* Add predicted vector & DC component to the previously quantized vector */
236 for (i = 0; i < LPC_ORDER; i++) {
237 temp = ((prev_lsp[i] - dc_lsp[i]) * pred + (1 << 14)) >> 15;
238 cur_lsp[i] += dc_lsp[i] + temp;
239 }
240
241 for (i = 0; i < LPC_ORDER; i++) {
242 cur_lsp[0] = FFMAX(cur_lsp[0], 0x180);
243 cur_lsp[LPC_ORDER - 1] = FFMIN(cur_lsp[LPC_ORDER - 1], 0x7e00);
244
245 /* Stability check */
246 for (j = 1; j < LPC_ORDER; j++) {
247 temp = min_dist + cur_lsp[j - 1] - cur_lsp[j];
248 if (temp > 0) {
249 temp >>= 1;
250 cur_lsp[j - 1] -= temp;
251 cur_lsp[j] += temp;
252 }
253 }
254 stable = 1;
255 for (j = 1; j < LPC_ORDER; j++) {
256 temp = cur_lsp[j - 1] + min_dist - cur_lsp[j] - 4;
257 if (temp > 0) {
258 stable = 0;
259 break;
260 }
261 }
262 if (stable)
263 break;
264 }
265 if (!stable)
266 memcpy(cur_lsp, prev_lsp, LPC_ORDER * sizeof(*cur_lsp));
267 }