/src/ffmpeg/libavcodec/g723_1enc.c
Line | Count | Source |
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 "codec_internal.h" |
38 | | #include "encode.h" |
39 | | #include "g723_1.h" |
40 | | |
41 | | #define BITSTREAM_WRITER_LE |
42 | | #include "put_bits.h" |
43 | | |
44 | | /** |
45 | | * Hamming window coefficients scaled by 2^15 |
46 | | */ |
47 | | static const int16_t hamming_window[LPC_FRAME] = { |
48 | | 2621, 2631, 2659, 2705, 2770, 2853, 2955, 3074, 3212, 3367, |
49 | | 3541, 3731, 3939, 4164, 4405, 4663, 4937, 5226, 5531, 5851, |
50 | | 6186, 6534, 6897, 7273, 7661, 8062, 8475, 8899, 9334, 9780, |
51 | | 10235, 10699, 11172, 11653, 12141, 12636, 13138, 13645, 14157, 14673, |
52 | | 15193, 15716, 16242, 16769, 17298, 17827, 18356, 18884, 19411, 19935, |
53 | | 20457, 20975, 21489, 21999, 22503, 23002, 23494, 23978, 24455, 24924, |
54 | | 25384, 25834, 26274, 26704, 27122, 27529, 27924, 28306, 28675, 29031, |
55 | | 29373, 29700, 30012, 30310, 30592, 30857, 31107, 31340, 31557, 31756, |
56 | | 31938, 32102, 32249, 32377, 32488, 32580, 32654, 32710, 32747, 32766, |
57 | | 32766, 32747, 32710, 32654, 32580, 32488, 32377, 32249, 32102, 31938, |
58 | | 31756, 31557, 31340, 31107, 30857, 30592, 30310, 30012, 29700, 29373, |
59 | | 29031, 28675, 28306, 27924, 27529, 27122, 26704, 26274, 25834, 25384, |
60 | | 24924, 24455, 23978, 23494, 23002, 22503, 21999, 21489, 20975, 20457, |
61 | | 19935, 19411, 18884, 18356, 17827, 17298, 16769, 16242, 15716, 15193, |
62 | | 14673, 14157, 13645, 13138, 12636, 12141, 11653, 11172, 10699, 10235, |
63 | | 9780, 9334, 8899, 8475, 8062, 7661, 7273, 6897, 6534, 6186, |
64 | | 5851, 5531, 5226, 4937, 4663, 4405, 4164, 3939, 3731, 3541, |
65 | | 3367, 3212, 3074, 2955, 2853, 2770, 2705, 2659, 2631, 2621 |
66 | | }; |
67 | | |
68 | | /** |
69 | | * Binomial window coefficients scaled by 2^15 |
70 | | */ |
71 | | static const int16_t binomial_window[LPC_ORDER] = { |
72 | | 32749, 32695, 32604, 32477, 32315, 32118, 31887, 31622, 31324, 30995 |
73 | | }; |
74 | | |
75 | | /** |
76 | | * 0.994^i scaled by 2^15 |
77 | | */ |
78 | | static const int16_t bandwidth_expand[LPC_ORDER] = { |
79 | | 32571, 32376, 32182, 31989, 31797, 31606, 31416, 31228, 31040, 30854 |
80 | | }; |
81 | | |
82 | | /** |
83 | | * 0.5^i scaled by 2^15 |
84 | | */ |
85 | | static const int16_t percept_flt_tbl[2][LPC_ORDER] = { |
86 | | /* Zero part */ |
87 | | {29491, 26542, 23888, 21499, 19349, 17414, 15673, 14106, 12695, 11425}, |
88 | | /* Pole part */ |
89 | | {16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32} |
90 | | }; |
91 | | |
92 | | static av_cold int g723_1_encode_init(AVCodecContext *avctx) |
93 | 0 | { |
94 | 0 | G723_1_Context *s = avctx->priv_data; |
95 | 0 | G723_1_ChannelContext *p = &s->ch[0]; |
96 | |
|
97 | 0 | if (avctx->sample_rate != 8000) { |
98 | 0 | av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n"); |
99 | 0 | return AVERROR(EINVAL); |
100 | 0 | } |
101 | | |
102 | 0 | if (avctx->bit_rate == 6300) { |
103 | 0 | p->cur_rate = RATE_6300; |
104 | 0 | } else if (avctx->bit_rate == 5300) { |
105 | 0 | av_log(avctx, AV_LOG_ERROR, "Use bitrate 6300 instead of 5300.\n"); |
106 | 0 | avpriv_report_missing_feature(avctx, "Bitrate 5300"); |
107 | 0 | return AVERROR_PATCHWELCOME; |
108 | 0 | } else { |
109 | 0 | av_log(avctx, AV_LOG_ERROR, "Bitrate not supported, use 6300\n"); |
110 | 0 | return AVERROR(EINVAL); |
111 | 0 | } |
112 | 0 | avctx->frame_size = 240; |
113 | 0 | memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(int16_t)); |
114 | |
|
115 | 0 | return 0; |
116 | 0 | } |
117 | | |
118 | | /** |
119 | | * Remove DC component from the input signal. |
120 | | * |
121 | | * @param buf input signal |
122 | | * @param fir zero memory |
123 | | * @param iir pole memory |
124 | | */ |
125 | | static void highpass_filter(int16_t *buf, int16_t *fir, int *iir) |
126 | 0 | { |
127 | 0 | int i; |
128 | 0 | for (i = 0; i < FRAME_LEN; i++) { |
129 | 0 | *iir = (buf[i] - *fir) * (1 << 15) + MULL2(*iir, 0x7f00); |
130 | 0 | *fir = buf[i]; |
131 | 0 | buf[i] = av_clipl_int32((int64_t)*iir + (1 << 15)) >> 16; |
132 | 0 | } |
133 | 0 | } |
134 | | |
135 | | /** |
136 | | * Estimate autocorrelation of the input vector. |
137 | | * |
138 | | * @param buf input buffer |
139 | | * @param autocorr autocorrelation coefficients vector |
140 | | */ |
141 | | static void comp_autocorr(int16_t *buf, int16_t *autocorr) |
142 | 0 | { |
143 | 0 | int i, scale, temp; |
144 | 0 | int16_t vector[LPC_FRAME]; |
145 | |
|
146 | 0 | ff_g723_1_scale_vector(vector, buf, LPC_FRAME); |
147 | | |
148 | | /* Apply the Hamming window */ |
149 | 0 | for (i = 0; i < LPC_FRAME; i++) |
150 | 0 | vector[i] = (vector[i] * hamming_window[i] + (1 << 14)) >> 15; |
151 | | |
152 | | /* Compute the first autocorrelation coefficient */ |
153 | 0 | temp = ff_dot_product(vector, vector, LPC_FRAME); |
154 | | |
155 | | /* Apply a white noise correlation factor of (1025/1024) */ |
156 | 0 | temp += temp >> 10; |
157 | | |
158 | | /* Normalize */ |
159 | 0 | scale = ff_g723_1_normalize_bits(temp, 31); |
160 | 0 | autocorr[0] = av_clipl_int32((int64_t) (temp << scale) + |
161 | 0 | (1 << 15)) >> 16; |
162 | | |
163 | | /* Compute the remaining coefficients */ |
164 | 0 | if (!autocorr[0]) { |
165 | 0 | memset(autocorr + 1, 0, LPC_ORDER * sizeof(int16_t)); |
166 | 0 | } else { |
167 | 0 | for (i = 1; i <= LPC_ORDER; i++) { |
168 | 0 | temp = ff_dot_product(vector, vector + i, LPC_FRAME - i); |
169 | 0 | temp = MULL2(temp * (1 << scale), binomial_window[i - 1]); |
170 | 0 | autocorr[i] = av_clipl_int32((int64_t) temp + (1 << 15)) >> 16; |
171 | 0 | } |
172 | 0 | } |
173 | 0 | } |
174 | | |
175 | | /** |
176 | | * Use Levinson-Durbin recursion to compute LPC coefficients from |
177 | | * autocorrelation values. |
178 | | * |
179 | | * @param lpc LPC coefficients vector |
180 | | * @param autocorr autocorrelation coefficients vector |
181 | | * @param error prediction error |
182 | | */ |
183 | | static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error) |
184 | 0 | { |
185 | 0 | int16_t vector[LPC_ORDER]; |
186 | 0 | int16_t partial_corr; |
187 | 0 | int i, j, temp; |
188 | |
|
189 | 0 | memset(lpc, 0, LPC_ORDER * sizeof(int16_t)); |
190 | |
|
191 | 0 | for (i = 0; i < LPC_ORDER; i++) { |
192 | | /* Compute the partial correlation coefficient */ |
193 | 0 | temp = 0; |
194 | 0 | for (j = 0; j < i; j++) |
195 | 0 | temp -= lpc[j] * autocorr[i - j - 1]; |
196 | 0 | temp = (autocorr[i] * (1 << 13) + temp) * (1 << 3); |
197 | |
|
198 | 0 | if (FFABS(temp) >= (error << 16)) |
199 | 0 | break; |
200 | | |
201 | 0 | partial_corr = temp / (error << 1); |
202 | |
|
203 | 0 | lpc[i] = (partial_corr + (1 << 1)) >> 2; |
204 | | |
205 | | /* Update the prediction error */ |
206 | 0 | temp = MULL2(temp, partial_corr); |
207 | 0 | error = av_clipl_int32((int64_t) (error << 16) - temp + |
208 | 0 | (1 << 15)) >> 16; |
209 | |
|
210 | 0 | memcpy(vector, lpc, i * sizeof(int16_t)); |
211 | 0 | for (j = 0; j < i; j++) { |
212 | 0 | temp = partial_corr * vector[i - j - 1] * 2; |
213 | 0 | lpc[j] = av_clipl_int32((int64_t) (lpc[j] * (1 << 16)) - temp + |
214 | 0 | (1 << 15)) >> 16; |
215 | 0 | } |
216 | 0 | } |
217 | 0 | } |
218 | | |
219 | | /** |
220 | | * Calculate LPC coefficients for the current frame. |
221 | | * |
222 | | * @param buf current frame |
223 | | * @param prev_data 2 trailing subframes of the previous frame |
224 | | * @param lpc LPC coefficients vector |
225 | | */ |
226 | | static void comp_lpc_coeff(int16_t *buf, int16_t *lpc) |
227 | 0 | { |
228 | 0 | int16_t autocorr[(LPC_ORDER + 1) * SUBFRAMES]; |
229 | 0 | int16_t *autocorr_ptr = autocorr; |
230 | 0 | int16_t *lpc_ptr = lpc; |
231 | 0 | int i, j; |
232 | |
|
233 | 0 | for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) { |
234 | 0 | comp_autocorr(buf + i, autocorr_ptr); |
235 | 0 | levinson_durbin(lpc_ptr, autocorr_ptr + 1, autocorr_ptr[0]); |
236 | |
|
237 | 0 | lpc_ptr += LPC_ORDER; |
238 | 0 | autocorr_ptr += LPC_ORDER + 1; |
239 | 0 | } |
240 | 0 | } |
241 | | |
242 | | static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp) |
243 | 0 | { |
244 | 0 | int f[LPC_ORDER + 2]; ///< coefficients of the sum and difference |
245 | | ///< polynomials (F1, F2) ordered as |
246 | | ///< f1[0], f2[0], ...., f1[5], f2[5] |
247 | |
|
248 | 0 | int max, shift, cur_val, prev_val, count, p; |
249 | 0 | int i, j; |
250 | 0 | int64_t temp; |
251 | | |
252 | | /* Initialize f1[0] and f2[0] to 1 in Q25 */ |
253 | 0 | for (i = 0; i < LPC_ORDER; i++) |
254 | 0 | lsp[i] = (lpc[i] * bandwidth_expand[i] + (1 << 14)) >> 15; |
255 | | |
256 | | /* Apply bandwidth expansion on the LPC coefficients */ |
257 | 0 | f[0] = f[1] = 1 << 25; |
258 | | |
259 | | /* Compute the remaining coefficients */ |
260 | 0 | for (i = 0; i < LPC_ORDER / 2; i++) { |
261 | | /* f1 */ |
262 | 0 | f[2 * i + 2] = -f[2 * i] - (lsp[i] + lsp[LPC_ORDER - 1 - i]) * (1 << 12); |
263 | | /* f2 */ |
264 | 0 | f[2 * i + 3] = f[2 * i + 1] - (lsp[i] - lsp[LPC_ORDER - 1 - i]) * (1 << 12); |
265 | 0 | } |
266 | | |
267 | | /* Divide f1[5] and f2[5] by 2 for use in polynomial evaluation */ |
268 | 0 | f[LPC_ORDER] >>= 1; |
269 | 0 | f[LPC_ORDER + 1] >>= 1; |
270 | | |
271 | | /* Normalize and shorten */ |
272 | 0 | max = FFABS(f[0]); |
273 | 0 | for (i = 1; i < LPC_ORDER + 2; i++) |
274 | 0 | max = FFMAX(max, FFABS(f[i])); |
275 | |
|
276 | 0 | shift = ff_g723_1_normalize_bits(max, 31); |
277 | |
|
278 | 0 | for (i = 0; i < LPC_ORDER + 2; i++) |
279 | 0 | f[i] = av_clipl_int32((int64_t) (f[i] * (1 << shift)) + (1 << 15)) >> 16; |
280 | | |
281 | | /** |
282 | | * Evaluate F1 and F2 at uniform intervals of pi/256 along the |
283 | | * unit circle and check for zero crossings. |
284 | | */ |
285 | 0 | p = 0; |
286 | 0 | temp = 0; |
287 | 0 | for (i = 0; i <= LPC_ORDER / 2; i++) |
288 | 0 | temp += f[2 * i] * G723_1_COS_TAB_FIRST_ELEMENT; |
289 | 0 | prev_val = av_clipl_int32(temp << 1); |
290 | 0 | count = 0; |
291 | 0 | for (i = 1; i < COS_TBL_SIZE / 2; i++) { |
292 | | /* Evaluate */ |
293 | 0 | temp = 0; |
294 | 0 | for (j = 0; j <= LPC_ORDER / 2; j++) |
295 | 0 | temp += f[LPC_ORDER - 2 * j + p] * ff_g723_1_cos_tab[i * j % COS_TBL_SIZE]; |
296 | 0 | cur_val = av_clipl_int32(temp * 2); |
297 | | |
298 | | /* Check for sign change, indicating a zero crossing */ |
299 | 0 | if ((cur_val ^ prev_val) < 0) { |
300 | 0 | int abs_cur = FFABS(cur_val); |
301 | 0 | int abs_prev = FFABS(prev_val); |
302 | 0 | int sum = abs_cur + abs_prev; |
303 | |
|
304 | 0 | shift = ff_g723_1_normalize_bits(sum, 31); |
305 | 0 | sum <<= shift; |
306 | 0 | abs_prev = abs_prev << shift >> 8; |
307 | 0 | lsp[count++] = ((i - 1) << 7) + (abs_prev >> 1) / (sum >> 16); |
308 | |
|
309 | 0 | if (count == LPC_ORDER) |
310 | 0 | break; |
311 | | |
312 | | /* Switch between sum and difference polynomials */ |
313 | 0 | p ^= 1; |
314 | | |
315 | | /* Evaluate */ |
316 | 0 | temp = 0; |
317 | 0 | for (j = 0; j <= LPC_ORDER / 2; j++) |
318 | 0 | temp += f[LPC_ORDER - 2 * j + p] * |
319 | 0 | ff_g723_1_cos_tab[i * j % COS_TBL_SIZE]; |
320 | 0 | cur_val = av_clipl_int32(temp * 2); |
321 | 0 | } |
322 | 0 | prev_val = cur_val; |
323 | 0 | } |
324 | |
|
325 | 0 | if (count != LPC_ORDER) |
326 | 0 | memcpy(lsp, prev_lsp, LPC_ORDER * sizeof(int16_t)); |
327 | 0 | } |
328 | | |
329 | | /** |
330 | | * Quantize the current LSP subvector. |
331 | | * |
332 | | * @param num band number |
333 | | * @param offset offset of the current subvector in an LPC_ORDER vector |
334 | | * @param size size of the current subvector |
335 | | */ |
336 | 0 | #define get_index(num, offset, size) \ |
337 | 0 | { \ |
338 | 0 | int error, max = -1; \ |
339 | 0 | int16_t temp[4]; \ |
340 | 0 | int i, j; \ |
341 | 0 | \ |
342 | 0 | for (i = 0; i < LSP_CB_SIZE; i++) { \ |
343 | 0 | for (j = 0; j < size; j++){ \ |
344 | 0 | temp[j] = (weight[j + (offset)] * ff_g723_1_lsp_band##num[i][j] + \ |
345 | 0 | (1 << 14)) >> 15; \ |
346 | 0 | } \ |
347 | 0 | error = ff_g723_1_dot_product(lsp + (offset), temp, size) * 2; \ |
348 | 0 | error -= ff_g723_1_dot_product(ff_g723_1_lsp_band##num[i], temp, size); \ |
349 | 0 | if (error > max) { \ |
350 | 0 | max = error; \ |
351 | 0 | lsp_index[num] = i; \ |
352 | 0 | } \ |
353 | 0 | } \ |
354 | 0 | } |
355 | | |
356 | | /** |
357 | | * Vector quantize the LSP frequencies. |
358 | | * |
359 | | * @param lsp the current lsp vector |
360 | | * @param prev_lsp the previous lsp vector |
361 | | */ |
362 | | static void lsp_quantize(uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp) |
363 | 0 | { |
364 | 0 | int16_t weight[LPC_ORDER]; |
365 | 0 | int16_t min, max; |
366 | 0 | int shift, i; |
367 | | |
368 | | /* Calculate the VQ weighting vector */ |
369 | 0 | weight[0] = (1 << 20) / (lsp[1] - lsp[0]); |
370 | 0 | weight[LPC_ORDER - 1] = (1 << 20) / |
371 | 0 | (lsp[LPC_ORDER - 1] - lsp[LPC_ORDER - 2]); |
372 | |
|
373 | 0 | for (i = 1; i < LPC_ORDER - 1; i++) { |
374 | 0 | min = FFMIN(lsp[i] - lsp[i - 1], lsp[i + 1] - lsp[i]); |
375 | 0 | if (min > 0x20) |
376 | 0 | weight[i] = (1 << 20) / min; |
377 | 0 | else |
378 | 0 | weight[i] = INT16_MAX; |
379 | 0 | } |
380 | | |
381 | | /* Normalize */ |
382 | 0 | max = 0; |
383 | 0 | for (i = 0; i < LPC_ORDER; i++) |
384 | 0 | max = FFMAX(weight[i], max); |
385 | |
|
386 | 0 | shift = ff_g723_1_normalize_bits(max, 15); |
387 | 0 | for (i = 0; i < LPC_ORDER; i++) { |
388 | 0 | weight[i] <<= shift; |
389 | 0 | } |
390 | | |
391 | | /* Compute the VQ target vector */ |
392 | 0 | for (i = 0; i < LPC_ORDER; i++) { |
393 | 0 | lsp[i] -= dc_lsp[i] + |
394 | 0 | (((prev_lsp[i] - dc_lsp[i]) * 12288 + (1 << 14)) >> 15); |
395 | 0 | } |
396 | |
|
397 | 0 | get_index(0, 0, 3); |
398 | 0 | get_index(1, 3, 3); |
399 | 0 | get_index(2, 6, 4); |
400 | 0 | } |
401 | | |
402 | | /** |
403 | | * Perform IIR filtering. |
404 | | * |
405 | | * @param fir_coef FIR coefficients |
406 | | * @param iir_coef IIR coefficients |
407 | | * @param src source vector |
408 | | * @param dest destination vector |
409 | | */ |
410 | | static void iir_filter(int16_t *fir_coef, int16_t *iir_coef, |
411 | | int16_t *src, int16_t *dest) |
412 | 0 | { |
413 | 0 | int m, n; |
414 | |
|
415 | 0 | for (m = 0; m < SUBFRAME_LEN; m++) { |
416 | 0 | int64_t filter = 0; |
417 | 0 | for (n = 1; n <= LPC_ORDER; n++) { |
418 | 0 | filter -= fir_coef[n - 1] * src[m - n] - |
419 | 0 | iir_coef[n - 1] * dest[m - n]; |
420 | 0 | } |
421 | |
|
422 | 0 | dest[m] = av_clipl_int32(src[m] * (1 << 16) + filter * (1 << 3) + |
423 | 0 | (1 << 15)) >> 16; |
424 | 0 | } |
425 | 0 | } |
426 | | |
427 | | /** |
428 | | * Apply the formant perceptual weighting filter. |
429 | | * |
430 | | * @param flt_coef filter coefficients |
431 | | * @param unq_lpc unquantized lpc vector |
432 | | */ |
433 | | static void perceptual_filter(G723_1_ChannelContext *p, int16_t *flt_coef, |
434 | | int16_t *unq_lpc, int16_t *buf) |
435 | 0 | { |
436 | 0 | int16_t vector[FRAME_LEN + LPC_ORDER]; |
437 | 0 | int i, j, k, l = 0; |
438 | |
|
439 | 0 | memcpy(buf, p->iir_mem, sizeof(int16_t) * LPC_ORDER); |
440 | 0 | memcpy(vector, p->fir_mem, sizeof(int16_t) * LPC_ORDER); |
441 | 0 | memcpy(vector + LPC_ORDER, buf + LPC_ORDER, sizeof(int16_t) * FRAME_LEN); |
442 | |
|
443 | 0 | for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) { |
444 | 0 | for (k = 0; k < LPC_ORDER; k++) { |
445 | 0 | flt_coef[k + 2 * l] = (unq_lpc[k + l] * percept_flt_tbl[0][k] + |
446 | 0 | (1 << 14)) >> 15; |
447 | 0 | flt_coef[k + 2 * l + LPC_ORDER] = (unq_lpc[k + l] * |
448 | 0 | percept_flt_tbl[1][k] + |
449 | 0 | (1 << 14)) >> 15; |
450 | 0 | } |
451 | 0 | iir_filter(flt_coef + 2 * l, flt_coef + 2 * l + LPC_ORDER, |
452 | 0 | vector + i, buf + i); |
453 | 0 | l += LPC_ORDER; |
454 | 0 | } |
455 | 0 | memcpy(p->iir_mem, buf + FRAME_LEN, sizeof(int16_t) * LPC_ORDER); |
456 | 0 | memcpy(p->fir_mem, vector + FRAME_LEN, sizeof(int16_t) * LPC_ORDER); |
457 | 0 | } |
458 | | |
459 | | /** |
460 | | * Estimate the open loop pitch period. |
461 | | * |
462 | | * @param buf perceptually weighted speech |
463 | | * @param start estimation is carried out from this position |
464 | | */ |
465 | | static int estimate_pitch(int16_t *buf, int start) |
466 | 0 | { |
467 | 0 | int max_exp = 32; |
468 | 0 | int max_ccr = 0x4000; |
469 | 0 | int max_eng = 0x7fff; |
470 | 0 | int index = PITCH_MIN; |
471 | 0 | int offset = start - PITCH_MIN + 1; |
472 | |
|
473 | 0 | int ccr, eng, orig_eng, ccr_eng, exp; |
474 | 0 | int diff, temp; |
475 | |
|
476 | 0 | int i; |
477 | |
|
478 | 0 | orig_eng = ff_dot_product(buf + offset, buf + offset, HALF_FRAME_LEN); |
479 | |
|
480 | 0 | for (i = PITCH_MIN; i <= PITCH_MAX - 3; i++) { |
481 | 0 | offset--; |
482 | | |
483 | | /* Update energy and compute correlation */ |
484 | 0 | orig_eng += buf[offset] * buf[offset] - |
485 | 0 | buf[offset + HALF_FRAME_LEN] * buf[offset + HALF_FRAME_LEN]; |
486 | 0 | ccr = ff_dot_product(buf + start, buf + offset, HALF_FRAME_LEN); |
487 | 0 | if (ccr <= 0) |
488 | 0 | continue; |
489 | | |
490 | | /* Split into mantissa and exponent to maintain precision */ |
491 | 0 | exp = ff_g723_1_normalize_bits(ccr, 31); |
492 | 0 | ccr = av_clipl_int32((int64_t) (ccr << exp) + (1 << 15)) >> 16; |
493 | 0 | exp <<= 1; |
494 | 0 | ccr *= ccr; |
495 | 0 | temp = ff_g723_1_normalize_bits(ccr, 31); |
496 | 0 | ccr = ccr << temp >> 16; |
497 | 0 | exp += temp; |
498 | |
|
499 | 0 | temp = ff_g723_1_normalize_bits(orig_eng, 31); |
500 | 0 | eng = av_clipl_int32((int64_t) (orig_eng << temp) + (1 << 15)) >> 16; |
501 | 0 | exp -= temp; |
502 | |
|
503 | 0 | if (ccr >= eng) { |
504 | 0 | exp--; |
505 | 0 | ccr >>= 1; |
506 | 0 | } |
507 | 0 | if (exp > max_exp) |
508 | 0 | continue; |
509 | | |
510 | 0 | if (exp + 1 < max_exp) |
511 | 0 | goto update; |
512 | | |
513 | | /* Equalize exponents before comparison */ |
514 | 0 | if (exp + 1 == max_exp) |
515 | 0 | temp = max_ccr >> 1; |
516 | 0 | else |
517 | 0 | temp = max_ccr; |
518 | 0 | ccr_eng = ccr * max_eng; |
519 | 0 | diff = ccr_eng - eng * temp; |
520 | 0 | if (diff > 0 && (i - index < PITCH_MIN || diff > ccr_eng >> 2)) { |
521 | 0 | update: |
522 | 0 | index = i; |
523 | 0 | max_exp = exp; |
524 | 0 | max_ccr = ccr; |
525 | 0 | max_eng = eng; |
526 | 0 | } |
527 | 0 | } |
528 | 0 | return index; |
529 | 0 | } |
530 | | |
531 | | /** |
532 | | * Compute harmonic noise filter parameters. |
533 | | * |
534 | | * @param buf perceptually weighted speech |
535 | | * @param pitch_lag open loop pitch period |
536 | | * @param hf harmonic filter parameters |
537 | | */ |
538 | | static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag, HFParam *hf) |
539 | 0 | { |
540 | 0 | int ccr, eng, max_ccr, max_eng; |
541 | 0 | int exp, max, diff; |
542 | 0 | int energy[15]; |
543 | 0 | int i, j; |
544 | |
|
545 | 0 | for (i = 0, j = pitch_lag - 3; j <= pitch_lag + 3; i++, j++) { |
546 | | /* Compute residual energy */ |
547 | 0 | energy[i << 1] = ff_dot_product(buf - j, buf - j, SUBFRAME_LEN); |
548 | | /* Compute correlation */ |
549 | 0 | energy[(i << 1) + 1] = ff_dot_product(buf, buf - j, SUBFRAME_LEN); |
550 | 0 | } |
551 | | |
552 | | /* Compute target energy */ |
553 | 0 | energy[14] = ff_dot_product(buf, buf, SUBFRAME_LEN); |
554 | | |
555 | | /* Normalize */ |
556 | 0 | max = 0; |
557 | 0 | for (i = 0; i < 15; i++) |
558 | 0 | max = FFMAX(max, FFABS(energy[i])); |
559 | |
|
560 | 0 | exp = ff_g723_1_normalize_bits(max, 31); |
561 | 0 | for (i = 0; i < 15; i++) { |
562 | 0 | energy[i] = av_clipl_int32((int64_t)(energy[i] * (1 << exp)) + |
563 | 0 | (1 << 15)) >> 16; |
564 | 0 | } |
565 | |
|
566 | 0 | hf->index = -1; |
567 | 0 | hf->gain = 0; |
568 | 0 | max_ccr = 1; |
569 | 0 | max_eng = 0x7fff; |
570 | |
|
571 | 0 | for (i = 0; i <= 6; i++) { |
572 | 0 | eng = energy[i << 1]; |
573 | 0 | ccr = energy[(i << 1) + 1]; |
574 | |
|
575 | 0 | if (ccr <= 0) |
576 | 0 | continue; |
577 | | |
578 | 0 | ccr = (ccr * ccr + (1 << 14)) >> 15; |
579 | 0 | diff = ccr * max_eng - eng * max_ccr; |
580 | 0 | if (diff > 0) { |
581 | 0 | max_ccr = ccr; |
582 | 0 | max_eng = eng; |
583 | 0 | hf->index = i; |
584 | 0 | } |
585 | 0 | } |
586 | |
|
587 | 0 | if (hf->index == -1) { |
588 | 0 | hf->index = pitch_lag; |
589 | 0 | return; |
590 | 0 | } |
591 | | |
592 | 0 | eng = energy[14] * max_eng; |
593 | 0 | eng = (eng >> 2) + (eng >> 3); |
594 | 0 | ccr = energy[(hf->index << 1) + 1] * energy[(hf->index << 1) + 1]; |
595 | 0 | if (eng < ccr) { |
596 | 0 | eng = energy[(hf->index << 1) + 1]; |
597 | |
|
598 | 0 | if (eng >= max_eng) |
599 | 0 | hf->gain = 0x2800; |
600 | 0 | else |
601 | 0 | hf->gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15; |
602 | 0 | } |
603 | 0 | hf->index += pitch_lag - 3; |
604 | 0 | } |
605 | | |
606 | | /** |
607 | | * Apply the harmonic noise shaping filter. |
608 | | * |
609 | | * @param hf filter parameters |
610 | | */ |
611 | | static void harmonic_filter(HFParam *hf, const int16_t *src, int16_t *dest) |
612 | 0 | { |
613 | 0 | int i; |
614 | |
|
615 | 0 | for (i = 0; i < SUBFRAME_LEN; i++) { |
616 | 0 | int64_t temp = hf->gain * src[i - hf->index] * 2; |
617 | 0 | dest[i] = av_clipl_int32(src[i] * (1 << 16) - temp + (1 << 15)) >> 16; |
618 | 0 | } |
619 | 0 | } |
620 | | |
621 | | static void harmonic_noise_sub(HFParam *hf, const int16_t *src, int16_t *dest) |
622 | 0 | { |
623 | 0 | int i; |
624 | 0 | for (i = 0; i < SUBFRAME_LEN; i++) { |
625 | 0 | int64_t temp = hf->gain * src[i - hf->index] * 2; |
626 | 0 | dest[i] = av_clipl_int32((dest[i] - src[i]) * (1 << 16) + temp + |
627 | 0 | (1 << 15)) >> 16; |
628 | 0 | } |
629 | 0 | } |
630 | | |
631 | | /** |
632 | | * Combined synthesis and formant perceptual weighting filer. |
633 | | * |
634 | | * @param qnt_lpc quantized lpc coefficients |
635 | | * @param perf_lpc perceptual filter coefficients |
636 | | * @param perf_fir perceptual filter fir memory |
637 | | * @param perf_iir perceptual filter iir memory |
638 | | * @param scale the filter output will be scaled by 2^scale |
639 | | */ |
640 | | static void synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc, |
641 | | int16_t *perf_fir, int16_t *perf_iir, |
642 | | const int16_t *src, int16_t *dest, int scale) |
643 | 0 | { |
644 | 0 | int i, j; |
645 | 0 | int16_t buf_16[SUBFRAME_LEN + LPC_ORDER]; |
646 | 0 | int64_t buf[SUBFRAME_LEN]; |
647 | |
|
648 | 0 | int16_t *bptr_16 = buf_16 + LPC_ORDER; |
649 | |
|
650 | 0 | memcpy(buf_16, perf_fir, sizeof(int16_t) * LPC_ORDER); |
651 | 0 | memcpy(dest - LPC_ORDER, perf_iir, sizeof(int16_t) * LPC_ORDER); |
652 | |
|
653 | 0 | for (i = 0; i < SUBFRAME_LEN; i++) { |
654 | 0 | int64_t temp = 0; |
655 | 0 | for (j = 1; j <= LPC_ORDER; j++) |
656 | 0 | temp -= qnt_lpc[j - 1] * bptr_16[i - j]; |
657 | |
|
658 | 0 | buf[i] = src[i] * (1 << 15) + temp * (1 << 3); |
659 | 0 | bptr_16[i] = av_clipl_int32(buf[i] + (1 << 15)) >> 16; |
660 | 0 | } |
661 | |
|
662 | 0 | for (i = 0; i < SUBFRAME_LEN; i++) { |
663 | 0 | int64_t fir = 0, iir = 0; |
664 | 0 | for (j = 1; j <= LPC_ORDER; j++) { |
665 | 0 | fir -= perf_lpc[j - 1] * bptr_16[i - j]; |
666 | 0 | iir += perf_lpc[j + LPC_ORDER - 1] * dest[i - j]; |
667 | 0 | } |
668 | 0 | dest[i] = av_clipl_int32((buf[i] + fir * (1 << 3)) * (1 << scale) + iir * (1 << 3) + |
669 | 0 | (1 << 15)) >> 16; |
670 | 0 | } |
671 | 0 | memcpy(perf_fir, buf_16 + SUBFRAME_LEN, sizeof(int16_t) * LPC_ORDER); |
672 | 0 | memcpy(perf_iir, dest + SUBFRAME_LEN - LPC_ORDER, |
673 | 0 | sizeof(int16_t) * LPC_ORDER); |
674 | 0 | } |
675 | | |
676 | | /** |
677 | | * Compute the adaptive codebook contribution. |
678 | | * |
679 | | * @param buf input signal |
680 | | * @param index the current subframe index |
681 | | */ |
682 | | static void acb_search(G723_1_ChannelContext *p, int16_t *residual, |
683 | | int16_t *impulse_resp, const int16_t *buf, |
684 | | int index) |
685 | 0 | { |
686 | 0 | int16_t flt_buf[PITCH_ORDER][SUBFRAME_LEN]; |
687 | |
|
688 | 0 | const int16_t *cb_tbl = ff_g723_1_adaptive_cb_gain85; |
689 | |
|
690 | 0 | int ccr_buf[PITCH_ORDER * SUBFRAMES << 2]; |
691 | |
|
692 | 0 | int pitch_lag = p->pitch_lag[index >> 1]; |
693 | 0 | int acb_lag = 1; |
694 | 0 | int acb_gain = 0; |
695 | 0 | int odd_frame = index & 1; |
696 | 0 | int iter = 3 + odd_frame; |
697 | 0 | int count = 0; |
698 | 0 | int tbl_size = 85; |
699 | |
|
700 | 0 | int i, j, k, l, max; |
701 | 0 | int64_t temp; |
702 | |
|
703 | 0 | if (!odd_frame) { |
704 | 0 | if (pitch_lag == PITCH_MIN) |
705 | 0 | pitch_lag++; |
706 | 0 | else |
707 | 0 | pitch_lag = FFMIN(pitch_lag, PITCH_MAX - 5); |
708 | 0 | } |
709 | |
|
710 | 0 | for (i = 0; i < iter; i++) { |
711 | 0 | ff_g723_1_get_residual(residual, p->prev_excitation, pitch_lag + i - 1); |
712 | |
|
713 | 0 | for (j = 0; j < SUBFRAME_LEN; j++) { |
714 | 0 | temp = 0; |
715 | 0 | for (k = 0; k <= j; k++) |
716 | 0 | temp += residual[PITCH_ORDER - 1 + k] * impulse_resp[j - k]; |
717 | 0 | flt_buf[PITCH_ORDER - 1][j] = av_clipl_int32(temp * 2 + (1 << 15)) >> 16; |
718 | 0 | } |
719 | |
|
720 | 0 | for (j = PITCH_ORDER - 2; j >= 0; j--) { |
721 | 0 | flt_buf[j][0] = (residual[j] + (1 << 1)) >> 2; |
722 | 0 | for (k = 1; k < SUBFRAME_LEN; k++) { |
723 | 0 | temp = flt_buf[j + 1][k - 1] * (1 << 15) + |
724 | 0 | residual[j] * impulse_resp[k]; |
725 | 0 | flt_buf[j][k] = av_clipl_int32(temp * 2 + (1 << 15)) >> 16; |
726 | 0 | } |
727 | 0 | } |
728 | | |
729 | | /* Compute crosscorrelation with the signal */ |
730 | 0 | for (j = 0; j < PITCH_ORDER; j++) { |
731 | 0 | temp = ff_dot_product(buf, flt_buf[j], SUBFRAME_LEN); |
732 | 0 | ccr_buf[count++] = av_clipl_int32(temp * 2); |
733 | 0 | } |
734 | | |
735 | | /* Compute energies */ |
736 | 0 | for (j = 0; j < PITCH_ORDER; j++) { |
737 | 0 | ccr_buf[count++] = ff_g723_1_dot_product(flt_buf[j], flt_buf[j], |
738 | 0 | SUBFRAME_LEN); |
739 | 0 | } |
740 | |
|
741 | 0 | for (j = 1; j < PITCH_ORDER; j++) { |
742 | 0 | for (k = 0; k < j; k++) { |
743 | 0 | temp = ff_dot_product(flt_buf[j], flt_buf[k], SUBFRAME_LEN); |
744 | 0 | ccr_buf[count++] = av_clipl_int32(temp * (1 << 2)); |
745 | 0 | } |
746 | 0 | } |
747 | 0 | } |
748 | | |
749 | | /* Normalize and shorten */ |
750 | 0 | max = 0; |
751 | 0 | for (i = 0; i < 20 * iter; i++) |
752 | 0 | max = FFMAX(max, FFABS(ccr_buf[i])); |
753 | |
|
754 | 0 | temp = ff_g723_1_normalize_bits(max, 31); |
755 | |
|
756 | 0 | for (i = 0; i < 20 * iter; i++) |
757 | 0 | ccr_buf[i] = av_clipl_int32((int64_t) (ccr_buf[i] * (1 << temp)) + |
758 | 0 | (1 << 15)) >> 16; |
759 | |
|
760 | 0 | max = 0; |
761 | 0 | for (i = 0; i < iter; i++) { |
762 | | /* Select quantization table */ |
763 | 0 | if (!odd_frame && pitch_lag + i - 1 >= SUBFRAME_LEN - 2 || |
764 | 0 | odd_frame && pitch_lag >= SUBFRAME_LEN - 2) { |
765 | 0 | cb_tbl = ff_g723_1_adaptive_cb_gain170; |
766 | 0 | tbl_size = 170; |
767 | 0 | } |
768 | |
|
769 | 0 | for (j = 0, k = 0; j < tbl_size; j++, k += 20) { |
770 | 0 | temp = 0; |
771 | 0 | for (l = 0; l < 20; l++) |
772 | 0 | temp += ccr_buf[20 * i + l] * cb_tbl[k + l]; |
773 | 0 | temp = av_clipl_int32(temp); |
774 | |
|
775 | 0 | if (temp > max) { |
776 | 0 | max = temp; |
777 | 0 | acb_gain = j; |
778 | 0 | acb_lag = i; |
779 | 0 | } |
780 | 0 | } |
781 | 0 | } |
782 | |
|
783 | 0 | if (!odd_frame) { |
784 | 0 | pitch_lag += acb_lag - 1; |
785 | 0 | acb_lag = 1; |
786 | 0 | } |
787 | |
|
788 | 0 | p->pitch_lag[index >> 1] = pitch_lag; |
789 | 0 | p->subframe[index].ad_cb_lag = acb_lag; |
790 | 0 | p->subframe[index].ad_cb_gain = acb_gain; |
791 | 0 | } |
792 | | |
793 | | /** |
794 | | * Subtract the adaptive codebook contribution from the input |
795 | | * to obtain the residual. |
796 | | * |
797 | | * @param buf target vector |
798 | | */ |
799 | | static void sub_acb_contrib(const int16_t *residual, const int16_t *impulse_resp, |
800 | | int16_t *buf) |
801 | 0 | { |
802 | 0 | int i, j; |
803 | | /* Subtract adaptive CB contribution to obtain the residual */ |
804 | 0 | for (i = 0; i < SUBFRAME_LEN; i++) { |
805 | 0 | int64_t temp = buf[i] * (1 << 14); |
806 | 0 | for (j = 0; j <= i; j++) |
807 | 0 | temp -= residual[j] * impulse_resp[i - j]; |
808 | |
|
809 | 0 | buf[i] = av_clipl_int32(temp * (1 << 2) + (1 << 15)) >> 16; |
810 | 0 | } |
811 | 0 | } |
812 | | |
813 | | /** |
814 | | * Quantize the residual signal using the fixed codebook (MP-MLQ). |
815 | | * |
816 | | * @param optim optimized fixed codebook parameters |
817 | | * @param buf excitation vector |
818 | | */ |
819 | | static void get_fcb_param(FCBParam *optim, int16_t *impulse_resp, |
820 | | int16_t *buf, int pulse_cnt, int pitch_lag) |
821 | 0 | { |
822 | 0 | FCBParam param; |
823 | 0 | int16_t impulse_r[SUBFRAME_LEN]; |
824 | 0 | int16_t temp_corr[SUBFRAME_LEN]; |
825 | 0 | int16_t impulse_corr[SUBFRAME_LEN]; |
826 | |
|
827 | 0 | int ccr1[SUBFRAME_LEN]; |
828 | 0 | int ccr2[SUBFRAME_LEN]; |
829 | 0 | int amp, err, max, max_amp_index, min, scale, i, j, k, l; |
830 | |
|
831 | 0 | int64_t temp; |
832 | | |
833 | | /* Update impulse response */ |
834 | 0 | memcpy(impulse_r, impulse_resp, sizeof(int16_t) * SUBFRAME_LEN); |
835 | 0 | param.dirac_train = 0; |
836 | 0 | if (pitch_lag < SUBFRAME_LEN - 2) { |
837 | 0 | param.dirac_train = 1; |
838 | 0 | ff_g723_1_gen_dirac_train(impulse_r, pitch_lag); |
839 | 0 | } |
840 | |
|
841 | 0 | for (i = 0; i < SUBFRAME_LEN; i++) |
842 | 0 | temp_corr[i] = impulse_r[i] >> 1; |
843 | | |
844 | | /* Compute impulse response autocorrelation */ |
845 | 0 | temp = ff_g723_1_dot_product(temp_corr, temp_corr, SUBFRAME_LEN); |
846 | |
|
847 | 0 | scale = ff_g723_1_normalize_bits(temp, 31); |
848 | 0 | impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16; |
849 | |
|
850 | 0 | for (i = 1; i < SUBFRAME_LEN; i++) { |
851 | 0 | temp = ff_g723_1_dot_product(temp_corr + i, temp_corr, |
852 | 0 | SUBFRAME_LEN - i); |
853 | 0 | impulse_corr[i] = av_clipl_int32(temp * (1 << scale) + (1 << 15)) >> 16; |
854 | 0 | } |
855 | | |
856 | | /* Compute crosscorrelation of impulse response with residual signal */ |
857 | 0 | scale -= 4; |
858 | 0 | for (i = 0; i < SUBFRAME_LEN; i++) { |
859 | 0 | temp = ff_g723_1_dot_product(buf + i, impulse_r, SUBFRAME_LEN - i); |
860 | 0 | if (scale < 0) |
861 | 0 | ccr1[i] = temp >> -scale; |
862 | 0 | else |
863 | 0 | ccr1[i] = av_clipl_int32(temp * (1 << scale)); |
864 | 0 | } |
865 | | |
866 | | /* Search loop */ |
867 | 0 | for (i = 0; i < GRID_SIZE; i++) { |
868 | | /* Maximize the crosscorrelation */ |
869 | 0 | max = 0; |
870 | 0 | for (j = i; j < SUBFRAME_LEN; j += GRID_SIZE) { |
871 | 0 | temp = FFABS(ccr1[j]); |
872 | 0 | if (temp >= max) { |
873 | 0 | max = temp; |
874 | 0 | param.pulse_pos[0] = j; |
875 | 0 | } |
876 | 0 | } |
877 | | |
878 | | /* Quantize the gain (max crosscorrelation/impulse_corr[0]) */ |
879 | 0 | amp = max; |
880 | 0 | min = 1 << 30; |
881 | 0 | max_amp_index = GAIN_LEVELS - 2; |
882 | 0 | for (j = max_amp_index; j >= 2; j--) { |
883 | 0 | temp = av_clipl_int32((int64_t) ff_g723_1_fixed_cb_gain[j] * |
884 | 0 | impulse_corr[0] << 1); |
885 | 0 | temp = FFABS(temp - amp); |
886 | 0 | if (temp < min) { |
887 | 0 | min = temp; |
888 | 0 | max_amp_index = j; |
889 | 0 | } |
890 | 0 | } |
891 | |
|
892 | 0 | max_amp_index--; |
893 | | /* Select additional gain values */ |
894 | 0 | for (j = 1; j < 5; j++) { |
895 | 0 | for (k = i; k < SUBFRAME_LEN; k += GRID_SIZE) { |
896 | 0 | temp_corr[k] = 0; |
897 | 0 | ccr2[k] = ccr1[k]; |
898 | 0 | } |
899 | 0 | param.amp_index = max_amp_index + j - 2; |
900 | 0 | amp = ff_g723_1_fixed_cb_gain[param.amp_index]; |
901 | |
|
902 | 0 | param.pulse_sign[0] = (ccr2[param.pulse_pos[0]] < 0) ? -amp : amp; |
903 | 0 | temp_corr[param.pulse_pos[0]] = 1; |
904 | |
|
905 | 0 | for (k = 1; k < pulse_cnt; k++) { |
906 | 0 | max = INT_MIN; |
907 | 0 | for (l = i; l < SUBFRAME_LEN; l += GRID_SIZE) { |
908 | 0 | if (temp_corr[l]) |
909 | 0 | continue; |
910 | 0 | temp = impulse_corr[FFABS(l - param.pulse_pos[k - 1])]; |
911 | 0 | temp = av_clipl_int32((int64_t) temp * |
912 | 0 | param.pulse_sign[k - 1] * 2); |
913 | 0 | ccr2[l] -= temp; |
914 | 0 | temp = FFABS(ccr2[l]); |
915 | 0 | if (temp > max) { |
916 | 0 | max = temp; |
917 | 0 | param.pulse_pos[k] = l; |
918 | 0 | } |
919 | 0 | } |
920 | |
|
921 | 0 | param.pulse_sign[k] = (ccr2[param.pulse_pos[k]] < 0) ? |
922 | 0 | -amp : amp; |
923 | 0 | temp_corr[param.pulse_pos[k]] = 1; |
924 | 0 | } |
925 | | |
926 | | /* Create the error vector */ |
927 | 0 | memset(temp_corr, 0, sizeof(int16_t) * SUBFRAME_LEN); |
928 | |
|
929 | 0 | for (k = 0; k < pulse_cnt; k++) |
930 | 0 | temp_corr[param.pulse_pos[k]] = param.pulse_sign[k]; |
931 | |
|
932 | 0 | for (k = SUBFRAME_LEN - 1; k >= 0; k--) { |
933 | 0 | temp = 0; |
934 | 0 | for (l = 0; l <= k; l++) { |
935 | 0 | int prod = av_clipl_int32((int64_t) temp_corr[l] * |
936 | 0 | impulse_r[k - l] * 2); |
937 | 0 | temp = av_clipl_int32(temp + prod); |
938 | 0 | } |
939 | 0 | temp_corr[k] = temp >> 14; |
940 | 0 | } |
941 | | |
942 | | /* Compute square of error */ |
943 | 0 | err = 0; |
944 | 0 | for (k = 0; k < SUBFRAME_LEN; k++) { |
945 | 0 | int64_t prod; |
946 | 0 | prod = av_clipl_int32((int64_t) buf[k] * temp_corr[k] * 2); |
947 | 0 | err = av_clipl_int32(err - prod); |
948 | 0 | prod = av_clipl_int32((int64_t) temp_corr[k] * temp_corr[k]); |
949 | 0 | err = av_clipl_int32(err + prod); |
950 | 0 | } |
951 | | |
952 | | /* Minimize */ |
953 | 0 | if (err < optim->min_err) { |
954 | 0 | optim->min_err = err; |
955 | 0 | optim->grid_index = i; |
956 | 0 | optim->amp_index = param.amp_index; |
957 | 0 | optim->dirac_train = param.dirac_train; |
958 | |
|
959 | 0 | for (k = 0; k < pulse_cnt; k++) { |
960 | 0 | optim->pulse_sign[k] = param.pulse_sign[k]; |
961 | 0 | optim->pulse_pos[k] = param.pulse_pos[k]; |
962 | 0 | } |
963 | 0 | } |
964 | 0 | } |
965 | 0 | } |
966 | 0 | } |
967 | | |
968 | | /** |
969 | | * Encode the pulse position and gain of the current subframe. |
970 | | * |
971 | | * @param optim optimized fixed CB parameters |
972 | | * @param buf excitation vector |
973 | | */ |
974 | | static void pack_fcb_param(G723_1_Subframe *subfrm, FCBParam *optim, |
975 | | int16_t *buf, int pulse_cnt) |
976 | 0 | { |
977 | 0 | int i, j; |
978 | |
|
979 | 0 | j = PULSE_MAX - pulse_cnt; |
980 | |
|
981 | 0 | subfrm->pulse_sign = 0; |
982 | 0 | subfrm->pulse_pos = 0; |
983 | |
|
984 | 0 | for (i = 0; i < SUBFRAME_LEN >> 1; i++) { |
985 | 0 | int val = buf[optim->grid_index + (i << 1)]; |
986 | 0 | if (!val) { |
987 | 0 | subfrm->pulse_pos += ff_g723_1_combinatorial_table[j][i]; |
988 | 0 | } else { |
989 | 0 | subfrm->pulse_sign <<= 1; |
990 | 0 | if (val < 0) |
991 | 0 | subfrm->pulse_sign++; |
992 | 0 | j++; |
993 | |
|
994 | 0 | if (j == PULSE_MAX) |
995 | 0 | break; |
996 | 0 | } |
997 | 0 | } |
998 | 0 | subfrm->amp_index = optim->amp_index; |
999 | 0 | subfrm->grid_index = optim->grid_index; |
1000 | 0 | subfrm->dirac_train = optim->dirac_train; |
1001 | 0 | } |
1002 | | |
1003 | | /** |
1004 | | * Compute the fixed codebook excitation. |
1005 | | * |
1006 | | * @param buf target vector |
1007 | | * @param impulse_resp impulse response of the combined filter |
1008 | | */ |
1009 | | static void fcb_search(G723_1_ChannelContext *p, int16_t *impulse_resp, |
1010 | | int16_t *buf, int index) |
1011 | 0 | { |
1012 | 0 | FCBParam optim; |
1013 | 0 | int pulse_cnt = pulses[index]; |
1014 | 0 | int i; |
1015 | |
|
1016 | 0 | optim.min_err = 1LL << 31; |
1017 | 0 | get_fcb_param(&optim, impulse_resp, buf, pulse_cnt, SUBFRAME_LEN); |
1018 | |
|
1019 | 0 | if (p->pitch_lag[index >> 1] < SUBFRAME_LEN - 2) { |
1020 | 0 | get_fcb_param(&optim, impulse_resp, buf, pulse_cnt, |
1021 | 0 | p->pitch_lag[index >> 1]); |
1022 | 0 | } |
1023 | | |
1024 | | /* Reconstruct the excitation */ |
1025 | 0 | memset(buf, 0, sizeof(int16_t) * SUBFRAME_LEN); |
1026 | 0 | for (i = 0; i < pulse_cnt; i++) |
1027 | 0 | buf[optim.pulse_pos[i]] = optim.pulse_sign[i]; |
1028 | |
|
1029 | 0 | pack_fcb_param(&p->subframe[index], &optim, buf, pulse_cnt); |
1030 | |
|
1031 | 0 | if (optim.dirac_train) |
1032 | 0 | ff_g723_1_gen_dirac_train(buf, p->pitch_lag[index >> 1]); |
1033 | 0 | } |
1034 | | |
1035 | | /** |
1036 | | * Pack the frame parameters into output bitstream. |
1037 | | * |
1038 | | * @param frame output buffer |
1039 | | * @param size size of the buffer |
1040 | | */ |
1041 | | static void pack_bitstream(G723_1_ChannelContext *p, AVPacket *avpkt, int info_bits) |
1042 | 0 | { |
1043 | 0 | PutBitContext pb; |
1044 | 0 | int i, temp; |
1045 | |
|
1046 | 0 | init_put_bits(&pb, avpkt->data, avpkt->size); |
1047 | |
|
1048 | 0 | put_bits(&pb, 2, info_bits); |
1049 | |
|
1050 | 0 | put_bits(&pb, 8, p->lsp_index[2]); |
1051 | 0 | put_bits(&pb, 8, p->lsp_index[1]); |
1052 | 0 | put_bits(&pb, 8, p->lsp_index[0]); |
1053 | |
|
1054 | 0 | put_bits(&pb, 7, p->pitch_lag[0] - PITCH_MIN); |
1055 | 0 | put_bits(&pb, 2, p->subframe[1].ad_cb_lag); |
1056 | 0 | put_bits(&pb, 7, p->pitch_lag[1] - PITCH_MIN); |
1057 | 0 | put_bits(&pb, 2, p->subframe[3].ad_cb_lag); |
1058 | | |
1059 | | /* Write 12 bit combined gain */ |
1060 | 0 | for (i = 0; i < SUBFRAMES; i++) { |
1061 | 0 | temp = p->subframe[i].ad_cb_gain * GAIN_LEVELS + |
1062 | 0 | p->subframe[i].amp_index; |
1063 | 0 | if (p->cur_rate == RATE_6300) |
1064 | 0 | temp += p->subframe[i].dirac_train << 11; |
1065 | 0 | put_bits(&pb, 12, temp); |
1066 | 0 | } |
1067 | |
|
1068 | 0 | put_bits(&pb, 1, p->subframe[0].grid_index); |
1069 | 0 | put_bits(&pb, 1, p->subframe[1].grid_index); |
1070 | 0 | put_bits(&pb, 1, p->subframe[2].grid_index); |
1071 | 0 | put_bits(&pb, 1, p->subframe[3].grid_index); |
1072 | |
|
1073 | 0 | if (p->cur_rate == RATE_6300) { |
1074 | 0 | put_bits(&pb, 1, 0); /* reserved bit */ |
1075 | | |
1076 | | /* Write 13 bit combined position index */ |
1077 | 0 | temp = (p->subframe[0].pulse_pos >> 16) * 810 + |
1078 | 0 | (p->subframe[1].pulse_pos >> 14) * 90 + |
1079 | 0 | (p->subframe[2].pulse_pos >> 16) * 9 + |
1080 | 0 | (p->subframe[3].pulse_pos >> 14); |
1081 | 0 | put_bits(&pb, 13, temp); |
1082 | |
|
1083 | 0 | put_bits(&pb, 16, p->subframe[0].pulse_pos & 0xffff); |
1084 | 0 | put_bits(&pb, 14, p->subframe[1].pulse_pos & 0x3fff); |
1085 | 0 | put_bits(&pb, 16, p->subframe[2].pulse_pos & 0xffff); |
1086 | 0 | put_bits(&pb, 14, p->subframe[3].pulse_pos & 0x3fff); |
1087 | |
|
1088 | 0 | put_bits(&pb, 6, p->subframe[0].pulse_sign); |
1089 | 0 | put_bits(&pb, 5, p->subframe[1].pulse_sign); |
1090 | 0 | put_bits(&pb, 6, p->subframe[2].pulse_sign); |
1091 | 0 | put_bits(&pb, 5, p->subframe[3].pulse_sign); |
1092 | 0 | } |
1093 | |
|
1094 | 0 | flush_put_bits(&pb); |
1095 | 0 | } |
1096 | | |
1097 | | static int g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, |
1098 | | const AVFrame *frame, int *got_packet_ptr) |
1099 | 0 | { |
1100 | 0 | G723_1_Context *s = avctx->priv_data; |
1101 | 0 | G723_1_ChannelContext *p = &s->ch[0]; |
1102 | 0 | int16_t unq_lpc[LPC_ORDER * SUBFRAMES]; |
1103 | 0 | int16_t qnt_lpc[LPC_ORDER * SUBFRAMES]; |
1104 | 0 | int16_t cur_lsp[LPC_ORDER]; |
1105 | 0 | int16_t weighted_lpc[LPC_ORDER * SUBFRAMES << 1]; |
1106 | 0 | int16_t vector[FRAME_LEN + PITCH_MAX]; |
1107 | 0 | int offset, ret, i, j, info_bits = 0; |
1108 | 0 | int16_t *in, *start; |
1109 | 0 | HFParam hf[4]; |
1110 | | |
1111 | | /* duplicate input */ |
1112 | 0 | start = in = av_memdup(frame->data[0], frame->nb_samples * sizeof(int16_t)); |
1113 | 0 | if (!in) |
1114 | 0 | return AVERROR(ENOMEM); |
1115 | | |
1116 | 0 | highpass_filter(in, &p->hpf_fir_mem, &p->hpf_iir_mem); |
1117 | |
|
1118 | 0 | memcpy(vector, p->prev_data, HALF_FRAME_LEN * sizeof(int16_t)); |
1119 | 0 | memcpy(vector + HALF_FRAME_LEN, in, FRAME_LEN * sizeof(int16_t)); |
1120 | |
|
1121 | 0 | comp_lpc_coeff(vector, unq_lpc); |
1122 | 0 | lpc2lsp(&unq_lpc[LPC_ORDER * 3], p->prev_lsp, cur_lsp); |
1123 | 0 | lsp_quantize(p->lsp_index, cur_lsp, p->prev_lsp); |
1124 | | |
1125 | | /* Update memory */ |
1126 | 0 | memcpy(vector + LPC_ORDER, p->prev_data + SUBFRAME_LEN, |
1127 | 0 | sizeof(int16_t) * SUBFRAME_LEN); |
1128 | 0 | memcpy(vector + LPC_ORDER + SUBFRAME_LEN, in, |
1129 | 0 | sizeof(int16_t) * (HALF_FRAME_LEN + SUBFRAME_LEN)); |
1130 | 0 | memcpy(p->prev_data, in + HALF_FRAME_LEN, |
1131 | 0 | sizeof(int16_t) * HALF_FRAME_LEN); |
1132 | 0 | memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN); |
1133 | |
|
1134 | 0 | perceptual_filter(p, weighted_lpc, unq_lpc, vector); |
1135 | |
|
1136 | 0 | memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN); |
1137 | 0 | memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX); |
1138 | 0 | memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN); |
1139 | |
|
1140 | 0 | ff_g723_1_scale_vector(vector, vector, FRAME_LEN + PITCH_MAX); |
1141 | |
|
1142 | 0 | p->pitch_lag[0] = estimate_pitch(vector, PITCH_MAX); |
1143 | 0 | p->pitch_lag[1] = estimate_pitch(vector, PITCH_MAX + HALF_FRAME_LEN); |
1144 | |
|
1145 | 0 | for (i = PITCH_MAX, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) |
1146 | 0 | comp_harmonic_coeff(vector + i, p->pitch_lag[j >> 1], hf + j); |
1147 | |
|
1148 | 0 | memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX); |
1149 | 0 | memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN); |
1150 | 0 | memcpy(p->prev_weight_sig, vector + FRAME_LEN, sizeof(int16_t) * PITCH_MAX); |
1151 | |
|
1152 | 0 | for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) |
1153 | 0 | harmonic_filter(hf + j, vector + PITCH_MAX + i, in + i); |
1154 | |
|
1155 | 0 | ff_g723_1_inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, 0); |
1156 | 0 | ff_g723_1_lsp_interpolate(qnt_lpc, cur_lsp, p->prev_lsp); |
1157 | |
|
1158 | 0 | memcpy(p->prev_lsp, cur_lsp, sizeof(int16_t) * LPC_ORDER); |
1159 | |
|
1160 | 0 | offset = 0; |
1161 | 0 | for (i = 0; i < SUBFRAMES; i++) { |
1162 | 0 | int16_t impulse_resp[SUBFRAME_LEN]; |
1163 | 0 | int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1]; |
1164 | 0 | int16_t flt_in[SUBFRAME_LEN]; |
1165 | 0 | int16_t zero[LPC_ORDER], fir[LPC_ORDER], iir[LPC_ORDER]; |
1166 | | |
1167 | | /** |
1168 | | * Compute the combined impulse response of the synthesis filter, |
1169 | | * formant perceptual weighting filter and harmonic noise shaping filter |
1170 | | */ |
1171 | 0 | memset(zero, 0, sizeof(int16_t) * LPC_ORDER); |
1172 | 0 | memset(vector, 0, sizeof(int16_t) * PITCH_MAX); |
1173 | 0 | memset(flt_in, 0, sizeof(int16_t) * SUBFRAME_LEN); |
1174 | |
|
1175 | 0 | flt_in[0] = 1 << 13; /* Unit impulse */ |
1176 | 0 | synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1), |
1177 | 0 | zero, zero, flt_in, vector + PITCH_MAX, 1); |
1178 | 0 | harmonic_filter(hf + i, vector + PITCH_MAX, impulse_resp); |
1179 | | |
1180 | | /* Compute the combined zero input response */ |
1181 | 0 | flt_in[0] = 0; |
1182 | 0 | memcpy(fir, p->perf_fir_mem, sizeof(int16_t) * LPC_ORDER); |
1183 | 0 | memcpy(iir, p->perf_iir_mem, sizeof(int16_t) * LPC_ORDER); |
1184 | |
|
1185 | 0 | synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1), |
1186 | 0 | fir, iir, flt_in, vector + PITCH_MAX, 0); |
1187 | 0 | memcpy(vector, p->harmonic_mem, sizeof(int16_t) * PITCH_MAX); |
1188 | 0 | harmonic_noise_sub(hf + i, vector + PITCH_MAX, in); |
1189 | |
|
1190 | 0 | acb_search(p, residual, impulse_resp, in, i); |
1191 | 0 | ff_g723_1_gen_acb_excitation(residual, p->prev_excitation, |
1192 | 0 | p->pitch_lag[i >> 1], &p->subframe[i], |
1193 | 0 | p->cur_rate); |
1194 | 0 | sub_acb_contrib(residual, impulse_resp, in); |
1195 | |
|
1196 | 0 | fcb_search(p, impulse_resp, in, i); |
1197 | | |
1198 | | /* Reconstruct the excitation */ |
1199 | 0 | ff_g723_1_gen_acb_excitation(impulse_resp, p->prev_excitation, |
1200 | 0 | p->pitch_lag[i >> 1], &p->subframe[i], |
1201 | 0 | RATE_6300); |
1202 | |
|
1203 | 0 | memmove(p->prev_excitation, p->prev_excitation + SUBFRAME_LEN, |
1204 | 0 | sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN)); |
1205 | 0 | for (j = 0; j < SUBFRAME_LEN; j++) |
1206 | 0 | in[j] = av_clip_int16(in[j] * 2 + impulse_resp[j]); |
1207 | 0 | memcpy(p->prev_excitation + PITCH_MAX - SUBFRAME_LEN, in, |
1208 | 0 | sizeof(int16_t) * SUBFRAME_LEN); |
1209 | | |
1210 | | /* Update filter memories */ |
1211 | 0 | synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1), |
1212 | 0 | p->perf_fir_mem, p->perf_iir_mem, |
1213 | 0 | in, vector + PITCH_MAX, 0); |
1214 | 0 | memmove(p->harmonic_mem, p->harmonic_mem + SUBFRAME_LEN, |
1215 | 0 | sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN)); |
1216 | 0 | memcpy(p->harmonic_mem + PITCH_MAX - SUBFRAME_LEN, vector + PITCH_MAX, |
1217 | 0 | sizeof(int16_t) * SUBFRAME_LEN); |
1218 | |
|
1219 | 0 | in += SUBFRAME_LEN; |
1220 | 0 | offset += LPC_ORDER; |
1221 | 0 | } |
1222 | |
|
1223 | 0 | av_free(start); |
1224 | |
|
1225 | 0 | ret = ff_get_encode_buffer(avctx, avpkt, frame_size[info_bits], 0); |
1226 | 0 | if (ret < 0) |
1227 | 0 | return ret; |
1228 | | |
1229 | 0 | *got_packet_ptr = 1; |
1230 | 0 | pack_bitstream(p, avpkt, info_bits); |
1231 | 0 | return 0; |
1232 | 0 | } |
1233 | | |
1234 | | static const FFCodecDefault defaults[] = { |
1235 | | { "b", "6300" }, |
1236 | | { NULL }, |
1237 | | }; |
1238 | | |
1239 | | const FFCodec ff_g723_1_encoder = { |
1240 | | .p.name = "g723_1", |
1241 | | CODEC_LONG_NAME("G.723.1"), |
1242 | | .p.type = AVMEDIA_TYPE_AUDIO, |
1243 | | .p.id = AV_CODEC_ID_G723_1, |
1244 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, |
1245 | | .priv_data_size = sizeof(G723_1_Context), |
1246 | | .init = g723_1_encode_init, |
1247 | | FF_CODEC_ENCODE_CB(g723_1_encode_frame), |
1248 | | .defaults = defaults, |
1249 | | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16), |
1250 | | CODEC_CH_LAYOUTS(AV_CHANNEL_LAYOUT_MONO), |
1251 | | }; |