FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/g723_1enc.c
Date: 2021-09-26 18:22:30
Exec Total Coverage
Lines: 565 581 97.2%
Branches: 245 260 94.2%

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