FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/g723_1enc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 561 575 97.6%
Functions: 21 21 100.0%
Branches: 244 258 94.6%

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