FFmpeg coverage


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