FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/qcelpdec.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 268 353 75.9%
Functions: 14 15 93.3%
Branches: 164 232 70.7%

Line Branch Exec Source
1 /*
2 * QCELP decoder
3 * Copyright (c) 2007 Reynaldo H. Verdejo Pinochet
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 * QCELP decoder
25 * @author Reynaldo H. Verdejo Pinochet
26 * @remark FFmpeg merging spearheaded by Kenan Gillet
27 * @remark Development mentored by Benjamin Larson
28 */
29
30 #include "libavutil/avassert.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/float_dsp.h"
33 #include "avcodec.h"
34 #include "codec_internal.h"
35 #include "decode.h"
36 #include "get_bits.h"
37 #include "qcelpdata.h"
38 #include "celp_filters.h"
39 #include "acelp_filters.h"
40 #include "acelp_vectors.h"
41 #include "lsp.h"
42
43 typedef enum {
44 I_F_Q = -1, /**< insufficient frame quality */
45 SILENCE,
46 RATE_OCTAVE,
47 RATE_QUARTER,
48 RATE_HALF,
49 RATE_FULL
50 } qcelp_packet_rate;
51
52 typedef struct QCELPContext {
53 GetBitContext gb;
54 qcelp_packet_rate bitrate;
55 QCELPFrame frame; /**< unpacked data frame */
56
57 uint8_t erasure_count;
58 uint8_t octave_count; /**< count the consecutive RATE_OCTAVE frames */
59 float prev_lspf[10];
60 float predictor_lspf[10];/**< LSP predictor for RATE_OCTAVE and I_F_Q */
61 float pitch_synthesis_filter_mem[303];
62 float pitch_pre_filter_mem[303];
63 float rnd_fir_filter_mem[180];
64 float formant_mem[170];
65 float last_codebook_gain;
66 int prev_g1[2];
67 int prev_bitrate;
68 float pitch_gain[4];
69 uint8_t pitch_lag[4];
70 uint16_t first16bits;
71 uint8_t warned_buf_mismatch_bitrate;
72
73 /* postfilter */
74 float postfilter_synth_mem[10];
75 float postfilter_agc_mem;
76 float postfilter_tilt_mem;
77 } QCELPContext;
78
79 /**
80 * Initialize the speech codec according to the specification.
81 *
82 * TIA/EIA/IS-733 2.4.9
83 */
84 3 static av_cold int qcelp_decode_init(AVCodecContext *avctx)
85 {
86 3 QCELPContext *q = avctx->priv_data;
87 int i;
88
89 3 av_channel_layout_uninit(&avctx->ch_layout);
90 3 avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
91 3 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
92
93
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 3 times.
33 for (i = 0; i < 10; i++)
94 30 q->prev_lspf[i] = (i + 1) / 11.0;
95
96 3 return 0;
97 }
98
99 /**
100 * Decode the 10 quantized LSP frequencies from the LSPV/LSP
101 * transmission codes of any bitrate and check for badly received packets.
102 *
103 * @param q the context
104 * @param lspf line spectral pair frequencies
105 *
106 * @return 0 on success, -1 if the packet is badly received
107 *
108 * TIA/EIA/IS-733 2.4.3.2.6.2-2, 2.4.8.7.3
109 */
110 497 static int decode_lspf(QCELPContext *q, float *lspf)
111 {
112 int i;
113 float tmp_lspf, smooth, erasure_coeff;
114 const float *predictors;
115
116
4/4
✓ Branch 0 taken 346 times.
✓ Branch 1 taken 151 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 329 times.
497 if (q->bitrate == RATE_OCTAVE || q->bitrate == I_F_Q) {
117
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 139 times.
197 predictors = q->prev_bitrate != RATE_OCTAVE &&
118
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 15 times.
29 q->prev_bitrate != I_F_Q ? q->prev_lspf
119 : q->predictor_lspf;
120
121
2/2
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 17 times.
168 if (q->bitrate == RATE_OCTAVE) {
122 151 q->octave_count++;
123
124
2/2
✓ Branch 0 taken 1510 times.
✓ Branch 1 taken 151 times.
1661 for (i = 0; i < 10; i++) {
125 1510 q->predictor_lspf[i] =
126 3020 lspf[i] = (q->frame.lspv[i] ? QCELP_LSP_SPREAD_FACTOR
127
2/2
✓ Branch 0 taken 604 times.
✓ Branch 1 taken 906 times.
1510 : -QCELP_LSP_SPREAD_FACTOR) +
128 1510 predictors[i] * QCELP_LSP_OCTAVE_PREDICTOR +
129 1510 (i + 1) * ((1 - QCELP_LSP_OCTAVE_PREDICTOR) / 11);
130 }
131
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 75 times.
151 smooth = q->octave_count < 10 ? .875 : 0.1;
132 } else {
133 17 erasure_coeff = QCELP_LSP_OCTAVE_PREDICTOR;
134
135 av_assert2(q->bitrate == I_F_Q);
136
137
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 3 times.
17 if (q->erasure_count > 1)
138
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14 erasure_coeff *= q->erasure_count < 4 ? 0.9 : 0.7;
139
140
2/2
✓ Branch 0 taken 170 times.
✓ Branch 1 taken 17 times.
187 for (i = 0; i < 10; i++) {
141 170 q->predictor_lspf[i] =
142 170 lspf[i] = (i + 1) * (1 - erasure_coeff) / 11 +
143 170 erasure_coeff * predictors[i];
144 }
145 17 smooth = 0.125;
146 }
147
148 // Check the stability of the LSP frequencies.
149
1/2
✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
168 lspf[0] = FFMAX(lspf[0], QCELP_LSP_SPREAD_FACTOR);
150
2/2
✓ Branch 0 taken 1512 times.
✓ Branch 1 taken 168 times.
1680 for (i = 1; i < 10; i++)
151
2/2
✓ Branch 0 taken 1427 times.
✓ Branch 1 taken 85 times.
1512 lspf[i] = FFMAX(lspf[i], lspf[i - 1] + QCELP_LSP_SPREAD_FACTOR);
152
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 lspf[9] = FFMIN(lspf[9], 1.0 - QCELP_LSP_SPREAD_FACTOR);
154
2/2
✓ Branch 0 taken 1512 times.
✓ Branch 1 taken 168 times.
1680 for (i = 9; i > 0; i--)
155
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 1471 times.
1512 lspf[i - 1] = FFMIN(lspf[i - 1], lspf[i] - QCELP_LSP_SPREAD_FACTOR);
156
157 // Low-pass filter the LSP frequencies.
158 168 ff_weighted_vector_sumf(lspf, lspf, q->prev_lspf, smooth, 1.0 - smooth, 10);
159 } else {
160 329 q->octave_count = 0;
161
162 329 tmp_lspf = 0.0;
163
2/2
✓ Branch 0 taken 1645 times.
✓ Branch 1 taken 329 times.
1974 for (i = 0; i < 5; i++) {
164 1645 lspf[2 * i + 0] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][0] * 0.0001;
165 1645 lspf[2 * i + 1] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][1] * 0.0001;
166 }
167
168 // Check for badly received packets.
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 329 times.
329 if (q->bitrate == RATE_QUARTER) {
170 if (lspf[9] <= .70 || lspf[9] >= .97)
171 return -1;
172 for (i = 3; i < 10; i++)
173 if (fabs(lspf[i] - lspf[i - 2]) < .08)
174 return -1;
175 } else {
176
2/4
✓ Branch 0 taken 329 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 329 times.
329 if (lspf[9] <= .66 || lspf[9] >= .985)
177 return -1;
178
2/2
✓ Branch 0 taken 1974 times.
✓ Branch 1 taken 329 times.
2303 for (i = 4; i < 10; i++)
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1974 times.
1974 if (fabs(lspf[i] - lspf[i - 4]) < .0931)
180 return -1;
181 }
182 }
183 497 return 0;
184 }
185
186 /**
187 * Convert codebook transmission codes to GAIN and INDEX.
188 *
189 * @param q the context
190 * @param gain array holding the decoded gain
191 *
192 * TIA/EIA/IS-733 2.4.6.2
193 */
194 497 static void decode_gain_and_index(QCELPContext *q, float *gain)
195 {
196 int i, subframes_count, g1[16];
197 float slope;
198
199
2/2
✓ Branch 0 taken 329 times.
✓ Branch 1 taken 168 times.
497 if (q->bitrate >= RATE_QUARTER) {
200
2/3
✓ Branch 0 taken 282 times.
✓ Branch 1 taken 47 times.
✗ Branch 2 not taken.
329 switch (q->bitrate) {
201 282 case RATE_FULL: subframes_count = 16; break;
202 47 case RATE_HALF: subframes_count = 4; break;
203 default: subframes_count = 5;
204 }
205
2/2
✓ Branch 0 taken 4700 times.
✓ Branch 1 taken 329 times.
5029 for (i = 0; i < subframes_count; i++) {
206 4700 g1[i] = 4 * q->frame.cbgain[i];
207
4/4
✓ Branch 0 taken 4512 times.
✓ Branch 1 taken 188 times.
✓ Branch 2 taken 1128 times.
✓ Branch 3 taken 3384 times.
4700 if (q->bitrate == RATE_FULL && !((i + 1) & 3)) {
208 1128 g1[i] += av_clip((g1[i - 1] + g1[i - 2] + g1[i - 3]) / 3 - 6, 0, 32);
209 }
210
211 4700 gain[i] = qcelp_g12ga[g1[i]];
212
213
2/2
✓ Branch 0 taken 2227 times.
✓ Branch 1 taken 2473 times.
4700 if (q->frame.cbsign[i]) {
214 2227 gain[i] = -gain[i];
215 2227 q->frame.cindex[i] = (q->frame.cindex[i] - 89) & 127;
216 }
217 }
218
219 329 q->prev_g1[0] = g1[i - 2];
220 329 q->prev_g1[1] = g1[i - 1];
221 329 q->last_codebook_gain = qcelp_g12ga[g1[i - 1]];
222
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 329 times.
329 if (q->bitrate == RATE_QUARTER) {
224 // Provide smoothing of the unvoiced excitation energy.
225 gain[7] = gain[4];
226 gain[6] = 0.4 * gain[3] + 0.6 * gain[4];
227 gain[5] = gain[3];
228 gain[4] = 0.8 * gain[2] + 0.2 * gain[3];
229 gain[3] = 0.2 * gain[1] + 0.8 * gain[2];
230 gain[2] = gain[1];
231 gain[1] = 0.6 * gain[0] + 0.4 * gain[1];
232 }
233
1/2
✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
168 } else if (q->bitrate != SILENCE) {
234
2/2
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 17 times.
168 if (q->bitrate == RATE_OCTAVE) {
235 151 g1[0] = 2 * q->frame.cbgain[0] +
236 151 av_clip((q->prev_g1[0] + q->prev_g1[1]) / 2 - 5, 0, 54);
237 151 subframes_count = 8;
238 } else {
239 av_assert2(q->bitrate == I_F_Q);
240
241 17 g1[0] = q->prev_g1[1];
242
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 12 times.
17 switch (q->erasure_count) {
243 3 case 1 : break;
244 1 case 2 : g1[0] -= 1; break;
245 1 case 3 : g1[0] -= 2; break;
246 12 default: g1[0] -= 6;
247 }
248
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 3 times.
17 if (g1[0] < 0)
249 14 g1[0] = 0;
250 17 subframes_count = 4;
251 }
252 // This interpolation is done to produce smoother background noise.
253 168 slope = 0.5 * (qcelp_g12ga[g1[0]] - q->last_codebook_gain) / subframes_count;
254
2/2
✓ Branch 0 taken 1276 times.
✓ Branch 1 taken 168 times.
1444 for (i = 1; i <= subframes_count; i++)
255 1276 gain[i - 1] = q->last_codebook_gain + slope * i;
256
257 168 q->last_codebook_gain = gain[i - 2];
258 168 q->prev_g1[0] = q->prev_g1[1];
259 168 q->prev_g1[1] = g1[0];
260 }
261 497 }
262
263 /**
264 * If the received packet is Rate 1/4 a further sanity check is made of the
265 * codebook gain.
266 *
267 * @param cbgain the unpacked cbgain array
268 * @return -1 if the sanity check fails, 0 otherwise
269 *
270 * TIA/EIA/IS-733 2.4.8.7.3
271 */
272 static int codebook_sanity_check_for_rate_quarter(const uint8_t *cbgain)
273 {
274 int i, diff, prev_diff = 0;
275
276 for (i = 1; i < 5; i++) {
277 diff = cbgain[i] - cbgain[i-1];
278 if (FFABS(diff) > 10)
279 return -1;
280 else if (FFABS(diff - prev_diff) > 12)
281 return -1;
282 prev_diff = diff;
283 }
284 return 0;
285 }
286
287 /**
288 * Compute the scaled codebook vector Cdn From INDEX and GAIN
289 * for all rates.
290 *
291 * The specification lacks some information here.
292 *
293 * TIA/EIA/IS-733 has an omission on the codebook index determination
294 * formula for RATE_FULL and RATE_HALF frames at section 2.4.8.1.1. It says
295 * you have to subtract the decoded index parameter from the given scaled
296 * codebook vector index 'n' to get the desired circular codebook index, but
297 * it does not mention that you have to clamp 'n' to [0-9] in order to get
298 * RI-compliant results.
299 *
300 * The reason for this mistake seems to be the fact they forgot to mention you
301 * have to do these calculations per codebook subframe and adjust given
302 * equation values accordingly.
303 *
304 * @param q the context
305 * @param gain array holding the 4 pitch subframe gain values
306 * @param cdn_vector array for the generated scaled codebook vector
307 */
308 497 static void compute_svector(QCELPContext *q, const float *gain,
309 float *cdn_vector)
310 {
311 int i, j, k;
312 uint16_t cbseed, cindex;
313 float *rnd, tmp_gain, fir_filter_value;
314
315
4/7
✓ Branch 0 taken 282 times.
✓ Branch 1 taken 47 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 151 times.
✓ Branch 4 taken 17 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
497 switch (q->bitrate) {
316 282 case RATE_FULL:
317
2/2
✓ Branch 0 taken 4512 times.
✓ Branch 1 taken 282 times.
4794 for (i = 0; i < 16; i++) {
318 4512 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
319 4512 cindex = -q->frame.cindex[i];
320
2/2
✓ Branch 0 taken 45120 times.
✓ Branch 1 taken 4512 times.
49632 for (j = 0; j < 10; j++)
321 45120 *cdn_vector++ = tmp_gain *
322 45120 qcelp_rate_full_codebook[cindex++ & 127];
323 }
324 282 break;
325 47 case RATE_HALF:
326
2/2
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 47 times.
235 for (i = 0; i < 4; i++) {
327 188 tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO;
328 188 cindex = -q->frame.cindex[i];
329
2/2
✓ Branch 0 taken 7520 times.
✓ Branch 1 taken 188 times.
7708 for (j = 0; j < 40; j++)
330 7520 *cdn_vector++ = tmp_gain *
331 7520 qcelp_rate_half_codebook[cindex++ & 127];
332 }
333 47 break;
334 case RATE_QUARTER:
335 cbseed = (0x0003 & q->frame.lspv[4]) << 14 |
336 (0x003F & q->frame.lspv[3]) << 8 |
337 (0x0060 & q->frame.lspv[2]) << 1 |
338 (0x0007 & q->frame.lspv[1]) << 3 |
339 (0x0038 & q->frame.lspv[0]) >> 3;
340 rnd = q->rnd_fir_filter_mem + 20;
341 for (i = 0; i < 8; i++) {
342 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
343 for (k = 0; k < 20; k++) {
344 cbseed = 521 * cbseed + 259;
345 *rnd = (int16_t) cbseed;
346
347 // FIR filter
348 fir_filter_value = 0.0;
349 for (j = 0; j < 10; j++)
350 fir_filter_value += qcelp_rnd_fir_coefs[j] *
351 (rnd[-j] + rnd[-20+j]);
352
353 fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10];
354 *cdn_vector++ = tmp_gain * fir_filter_value;
355 rnd++;
356 }
357 }
358 memcpy(q->rnd_fir_filter_mem, q->rnd_fir_filter_mem + 160,
359 20 * sizeof(float));
360 break;
361 151 case RATE_OCTAVE:
362 151 cbseed = q->first16bits;
363
2/2
✓ Branch 0 taken 1208 times.
✓ Branch 1 taken 151 times.
1359 for (i = 0; i < 8; i++) {
364 1208 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
365
2/2
✓ Branch 0 taken 24160 times.
✓ Branch 1 taken 1208 times.
25368 for (j = 0; j < 20; j++) {
366 24160 cbseed = 521 * cbseed + 259;
367 24160 *cdn_vector++ = tmp_gain * (int16_t) cbseed;
368 }
369 }
370 151 break;
371 17 case I_F_Q:
372 17 cbseed = -44; // random codebook index
373
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 17 times.
85 for (i = 0; i < 4; i++) {
374 68 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
375
2/2
✓ Branch 0 taken 2720 times.
✓ Branch 1 taken 68 times.
2788 for (j = 0; j < 40; j++)
376 2720 *cdn_vector++ = tmp_gain *
377 2720 qcelp_rate_full_codebook[cbseed++ & 127];
378 }
379 17 break;
380 case SILENCE:
381 memset(cdn_vector, 0, 160 * sizeof(float));
382 break;
383 }
384 497 }
385
386 /**
387 * Apply generic gain control.
388 *
389 * @param v_out output vector
390 * @param v_in gain-controlled vector
391 * @param v_ref vector to control gain of
392 *
393 * TIA/EIA/IS-733 2.4.8.3, 2.4.8.6
394 */
395 329 static void apply_gain_ctrl(float *v_out, const float *v_ref, const float *v_in)
396 {
397 int i;
398
399
2/2
✓ Branch 0 taken 1316 times.
✓ Branch 1 taken 329 times.
1645 for (i = 0; i < 160; i += 40) {
400 1316 float res = avpriv_scalarproduct_float_c(v_ref + i, v_ref + i, 40);
401 1316 ff_scale_vector_to_given_sum_of_squares(v_out + i, v_in + i, res, 40);
402 }
403 329 }
404
405 /**
406 * Apply filter in pitch-subframe steps.
407 *
408 * @param memory buffer for the previous state of the filter
409 * - must be able to contain 303 elements
410 * - the 143 first elements are from the previous state
411 * - the next 160 are for output
412 * @param v_in input filter vector
413 * @param gain per-subframe gain array, each element is between 0.0 and 2.0
414 * @param lag per-subframe lag array, each element is
415 * - between 16 and 143 if its corresponding pfrac is 0,
416 * - between 16 and 139 otherwise
417 * @param pfrac per-subframe boolean array, 1 if the lag is fractional, 0
418 * otherwise
419 *
420 * @return filter output vector
421 */
422 658 static const float *do_pitchfilter(float memory[303], const float v_in[160],
423 const float gain[4], const uint8_t *lag,
424 const uint8_t pfrac[4])
425 {
426 int i, j;
427 float *v_lag, *v_out;
428 const float *v_len;
429
430 658 v_out = memory + 143; // Output vector starts at memory[143].
431
432
2/2
✓ Branch 0 taken 2632 times.
✓ Branch 1 taken 658 times.
3290 for (i = 0; i < 4; i++) {
433
1/2
✓ Branch 0 taken 2632 times.
✗ Branch 1 not taken.
2632 if (gain[i]) {
434 2632 v_lag = memory + 143 + 40 * i - lag[i];
435
2/2
✓ Branch 0 taken 105280 times.
✓ Branch 1 taken 2632 times.
107912 for (v_len = v_in + 40; v_in < v_len; v_in++) {
436
2/2
✓ Branch 0 taken 31280 times.
✓ Branch 1 taken 74000 times.
105280 if (pfrac[i]) { // If it is a fractional lag...
437
2/2
✓ Branch 0 taken 125120 times.
✓ Branch 1 taken 31280 times.
156400 for (j = 0, *v_out = 0.0; j < 4; j++)
438 125120 *v_out += qcelp_hammsinc_table[j] *
439 125120 (v_lag[j - 4] + v_lag[3 - j]);
440 } else
441 74000 *v_out = *v_lag;
442
443 105280 *v_out = *v_in + gain[i] * *v_out;
444
445 105280 v_lag++;
446 105280 v_out++;
447 }
448 } else {
449 memcpy(v_out, v_in, 40 * sizeof(float));
450 v_in += 40;
451 v_out += 40;
452 }
453 }
454
455 658 memmove(memory, memory + 160, 143 * sizeof(float));
456 658 return memory + 143;
457 }
458
459 /**
460 * Apply pitch synthesis filter and pitch prefilter to the scaled codebook vector.
461 * TIA/EIA/IS-733 2.4.5.2, 2.4.8.7.2
462 *
463 * @param q the context
464 * @param cdn_vector the scaled codebook vector
465 */
466 497 static void apply_pitch_filters(QCELPContext *q, float *cdn_vector)
467 {
468 int i;
469 const float *v_synthesis_filtered, *v_pre_filtered;
470
471
3/4
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 329 times.
✓ Branch 2 taken 168 times.
✗ Branch 3 not taken.
497 if (q->bitrate >= RATE_HALF || q->bitrate == SILENCE ||
472
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 151 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
168 (q->bitrate == I_F_Q && (q->prev_bitrate >= RATE_HALF))) {
473
474
1/2
✓ Branch 0 taken 329 times.
✗ Branch 1 not taken.
329 if (q->bitrate >= RATE_HALF) {
475 // Compute gain & lag for the whole frame.
476
2/2
✓ Branch 0 taken 1316 times.
✓ Branch 1 taken 329 times.
1645 for (i = 0; i < 4; i++) {
477
1/2
✓ Branch 0 taken 1316 times.
✗ Branch 1 not taken.
1316 q->pitch_gain[i] = q->frame.plag[i] ? (q->frame.pgain[i] + 1) * 0.25 : 0.0;
478
479 1316 q->pitch_lag[i] = q->frame.plag[i] + 16;
480 }
481 } else {
482 float max_pitch_gain;
483
484 if (q->bitrate == I_F_Q) {
485 if (q->erasure_count < 3)
486 max_pitch_gain = 0.9 - 0.3 * (q->erasure_count - 1);
487 else
488 max_pitch_gain = 0.0;
489 } else {
490 av_assert2(q->bitrate == SILENCE);
491 max_pitch_gain = 1.0;
492 }
493 for (i = 0; i < 4; i++)
494 q->pitch_gain[i] = FFMIN(q->pitch_gain[i], max_pitch_gain);
495
496 memset(q->frame.pfrac, 0, sizeof(q->frame.pfrac));
497 }
498
499 // pitch synthesis filter
500 329 v_synthesis_filtered = do_pitchfilter(q->pitch_synthesis_filter_mem,
501 329 cdn_vector, q->pitch_gain,
502 329 q->pitch_lag, q->frame.pfrac);
503
504 // pitch prefilter update
505
2/2
✓ Branch 0 taken 1316 times.
✓ Branch 1 taken 329 times.
1645 for (i = 0; i < 4; i++)
506
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 1255 times.
1316 q->pitch_gain[i] = 0.5 * FFMIN(q->pitch_gain[i], 1.0);
507
508 329 v_pre_filtered = do_pitchfilter(q->pitch_pre_filter_mem,
509 v_synthesis_filtered,
510 329 q->pitch_gain, q->pitch_lag,
511 329 q->frame.pfrac);
512
513 329 apply_gain_ctrl(cdn_vector, v_synthesis_filtered, v_pre_filtered);
514 } else {
515 168 memcpy(q->pitch_synthesis_filter_mem,
516 168 cdn_vector + 17, 143 * sizeof(float));
517 168 memcpy(q->pitch_pre_filter_mem, cdn_vector + 17, 143 * sizeof(float));
518 168 memset(q->pitch_gain, 0, sizeof(q->pitch_gain));
519 168 memset(q->pitch_lag, 0, sizeof(q->pitch_lag));
520 }
521 497 }
522
523 /**
524 * Reconstruct LPC coefficients from the line spectral pair frequencies
525 * and perform bandwidth expansion.
526 *
527 * @param lspf line spectral pair frequencies
528 * @param lpc linear predictive coding coefficients
529 *
530 * @note: bandwidth_expansion_coeff could be precalculated into a table
531 * but it seems to be slower on x86
532 *
533 * TIA/EIA/IS-733 2.4.3.3.5
534 */
535 1484 static void lspf2lpc(const float *lspf, float *lpc)
536 {
537 double lsp[10];
538 1484 double bandwidth_expansion_coeff = QCELP_BANDWIDTH_EXPANSION_COEFF;
539 int i;
540
541
2/2
✓ Branch 0 taken 14840 times.
✓ Branch 1 taken 1484 times.
16324 for (i = 0; i < 10; i++)
542 14840 lsp[i] = cos(M_PI * lspf[i]);
543
544 1484 ff_acelp_lspd2lpc(lsp, lpc, 5);
545
546
2/2
✓ Branch 0 taken 14840 times.
✓ Branch 1 taken 1484 times.
16324 for (i = 0; i < 10; i++) {
547 14840 lpc[i] *= bandwidth_expansion_coeff;
548 14840 bandwidth_expansion_coeff *= QCELP_BANDWIDTH_EXPANSION_COEFF;
549 }
550 1484 }
551
552 /**
553 * Interpolate LSP frequencies and compute LPC coefficients
554 * for a given bitrate & pitch subframe.
555 *
556 * TIA/EIA/IS-733 2.4.3.3.4, 2.4.8.7.2
557 *
558 * @param q the context
559 * @param curr_lspf LSP frequencies vector of the current frame
560 * @param lpc float vector for the resulting LPC
561 * @param subframe_num frame number in decoded stream
562 */
563 1988 static void interpolate_lpc(QCELPContext *q, const float *curr_lspf,
564 float *lpc, const int subframe_num)
565 {
566 float interpolated_lspf[10];
567 float weight;
568
569
2/2
✓ Branch 0 taken 1316 times.
✓ Branch 1 taken 672 times.
1988 if (q->bitrate >= RATE_QUARTER)
570 1316 weight = 0.25 * (subframe_num + 1);
571
4/4
✓ Branch 0 taken 604 times.
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 151 times.
✓ Branch 3 taken 453 times.
672 else if (q->bitrate == RATE_OCTAVE && !subframe_num)
572 151 weight = 0.625;
573 else
574 521 weight = 1.0;
575
576
2/2
✓ Branch 0 taken 1138 times.
✓ Branch 1 taken 850 times.
1988 if (weight != 1.0) {
577 1138 ff_weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf,
578 weight, 1.0 - weight, 10);
579 1138 lspf2lpc(interpolated_lspf, lpc);
580
2/2
✓ Branch 0 taken 521 times.
✓ Branch 1 taken 329 times.
850 } else if (q->bitrate >= RATE_QUARTER ||
581
4/4
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 453 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 51 times.
521 (q->bitrate == I_F_Q && !subframe_num))
582 346 lspf2lpc(curr_lspf, lpc);
583
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 504 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
504 else if (q->bitrate == SILENCE && !subframe_num)
584 lspf2lpc(q->prev_lspf, lpc);
585 1988 }
586
587 994 static qcelp_packet_rate buf_size2bitrate(const int buf_size)
588 {
589
4/6
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 166 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 497 times.
994 switch (buf_size) {
590 283 case 35: return RATE_FULL;
591 48 case 17: return RATE_HALF;
592 case 8: return RATE_QUARTER;
593 166 case 4: return RATE_OCTAVE;
594 case 1: return SILENCE;
595 }
596
597 497 return I_F_Q;
598 }
599
600 /**
601 * Determine the bitrate from the frame size and/or the first byte of the frame.
602 *
603 * @param avctx the AV codec context
604 * @param buf_size length of the buffer
605 * @param buf the buffer
606 *
607 * @return the bitrate on success,
608 * I_F_Q if the bitrate cannot be satisfactorily determined
609 *
610 * TIA/EIA/IS-733 2.4.8.7.1
611 */
612 497 static qcelp_packet_rate determine_bitrate(AVCodecContext *avctx,
613 const int buf_size,
614 const uint8_t **buf)
615 {
616 qcelp_packet_rate bitrate;
617
618
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 497 times.
497 if ((bitrate = buf_size2bitrate(buf_size)) >= 0) {
619 if (bitrate > **buf) {
620 QCELPContext *q = avctx->priv_data;
621 if (!q->warned_buf_mismatch_bitrate) {
622 av_log(avctx, AV_LOG_WARNING,
623 "Claimed bitrate and buffer size mismatch.\n");
624 q->warned_buf_mismatch_bitrate = 1;
625 }
626 bitrate = **buf;
627 } else if (bitrate < **buf) {
628 av_log(avctx, AV_LOG_ERROR,
629 "Buffer is too small for the claimed bitrate.\n");
630 return I_F_Q;
631 }
632 (*buf)++;
633
1/2
✓ Branch 1 taken 497 times.
✗ Branch 2 not taken.
497 } else if ((bitrate = buf_size2bitrate(buf_size + 1)) >= 0) {
634 497 av_log(avctx, AV_LOG_WARNING,
635 "Bitrate byte missing, guessing bitrate from packet size.\n");
636 } else
637 return I_F_Q;
638
639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 497 times.
497 if (bitrate == SILENCE) {
640 // FIXME: Remove this warning when tested with samples.
641 avpriv_request_sample(avctx, "Blank frame handling");
642 }
643 497 return bitrate;
644 }
645
646 17 static void warn_insufficient_frame_quality(AVCodecContext *avctx,
647 const char *message)
648 {
649 17 av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n",
650 avctx->frame_number, message);
651 17 }
652
653 497 static void postfilter(QCELPContext *q, float *samples, float *lpc)
654 {
655 static const float pow_0_775[10] = {
656 0.775000, 0.600625, 0.465484, 0.360750, 0.279582,
657 0.216676, 0.167924, 0.130141, 0.100859, 0.078166
658 }, pow_0_625[10] = {
659 0.625000, 0.390625, 0.244141, 0.152588, 0.095367,
660 0.059605, 0.037253, 0.023283, 0.014552, 0.009095
661 };
662 float lpc_s[10], lpc_p[10], pole_out[170], zero_out[160];
663 int n;
664
665
2/2
✓ Branch 0 taken 4970 times.
✓ Branch 1 taken 497 times.
5467 for (n = 0; n < 10; n++) {
666 4970 lpc_s[n] = lpc[n] * pow_0_625[n];
667 4970 lpc_p[n] = lpc[n] * pow_0_775[n];
668 }
669
670 497 ff_celp_lp_zero_synthesis_filterf(zero_out, lpc_s,
671 497 q->formant_mem + 10, 160, 10);
672 497 memcpy(pole_out, q->postfilter_synth_mem, sizeof(float) * 10);
673 497 ff_celp_lp_synthesis_filterf(pole_out + 10, lpc_p, zero_out, 160, 10);
674 497 memcpy(q->postfilter_synth_mem, pole_out + 160, sizeof(float) * 10);
675
676 497 ff_tilt_compensation(&q->postfilter_tilt_mem, 0.3, pole_out + 10, 160);
677
678 497 ff_adaptive_gain_control(samples, pole_out + 10,
679 497 avpriv_scalarproduct_float_c(q->formant_mem + 10,
680 497 q->formant_mem + 10,
681 160),
682 160, 0.9375, &q->postfilter_agc_mem);
683 497 }
684
685 497 static int qcelp_decode_frame(AVCodecContext *avctx, AVFrame *frame,
686 int *got_frame_ptr, AVPacket *avpkt)
687 {
688 497 const uint8_t *buf = avpkt->data;
689 497 int buf_size = avpkt->size;
690 497 QCELPContext *q = avctx->priv_data;
691 float *outbuffer;
692 int i, ret;
693 float quantized_lspf[10], lpc[10];
694 float gain[16];
695 float *formant_mem;
696
697 /* get output buffer */
698 497 frame->nb_samples = 160;
699
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 497 times.
497 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
700 return ret;
701 497 outbuffer = (float *)frame->data[0];
702
703
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 497 times.
497 if ((q->bitrate = determine_bitrate(avctx, buf_size, &buf)) == I_F_Q) {
704 warn_insufficient_frame_quality(avctx, "Bitrate cannot be determined.");
705 goto erasure;
706 }
707
708
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 331 times.
497 if (q->bitrate == RATE_OCTAVE &&
709
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 151 times.
166 (q->first16bits = AV_RB16(buf)) == 0xFFFF) {
710 15 warn_insufficient_frame_quality(avctx, "Bitrate is 1/8 and first 16 bits are on.");
711 15 goto erasure;
712 }
713
714
1/2
✓ Branch 0 taken 482 times.
✗ Branch 1 not taken.
482 if (q->bitrate > SILENCE) {
715 482 const QCELPBitmap *bitmaps = qcelp_unpacking_bitmaps_per_rate[q->bitrate];
716 482 const QCELPBitmap *bitmaps_end = qcelp_unpacking_bitmaps_per_rate[q->bitrate] +
717 482 qcelp_unpacking_bitmaps_lengths[q->bitrate];
718 482 uint8_t *unpacked_data = (uint8_t *)&q->frame;
719
720
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 482 times.
482 if ((ret = init_get_bits8(&q->gb, buf, buf_size)) < 0)
721 return ret;
722
723 482 memset(&q->frame, 0, sizeof(QCELPFrame));
724
725
2/2
✓ Branch 0 taken 26074 times.
✓ Branch 1 taken 482 times.
26556 for (; bitmaps < bitmaps_end; bitmaps++)
726 26074 unpacked_data[bitmaps->index] |= get_bits(&q->gb, bitmaps->bitlen) << bitmaps->bitpos;
727
728 // Check for erasures/blanks on rates 1, 1/4 and 1/8.
729
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 481 times.
482 if (q->frame.reserved) {
730 1 warn_insufficient_frame_quality(avctx, "Wrong data in reserved frame area.");
731 1 goto erasure;
732 }
733
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 481 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
481 if (q->bitrate == RATE_QUARTER &&
734 codebook_sanity_check_for_rate_quarter(q->frame.cbgain)) {
735 warn_insufficient_frame_quality(avctx, "Codebook gain sanity check failed.");
736 goto erasure;
737 }
738
739
2/2
✓ Branch 0 taken 330 times.
✓ Branch 1 taken 151 times.
481 if (q->bitrate >= RATE_HALF) {
740
2/2
✓ Branch 0 taken 1317 times.
✓ Branch 1 taken 329 times.
1646 for (i = 0; i < 4; i++) {
741
4/4
✓ Branch 0 taken 392 times.
✓ Branch 1 taken 925 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 391 times.
1317 if (q->frame.pfrac[i] && q->frame.plag[i] >= 124) {
742 1 warn_insufficient_frame_quality(avctx, "Cannot initialize pitch filter.");
743 1 goto erasure;
744 }
745 }
746 }
747 }
748
749 480 decode_gain_and_index(q, gain);
750 480 compute_svector(q, gain, outbuffer);
751
752
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 480 times.
480 if (decode_lspf(q, quantized_lspf) < 0) {
753 warn_insufficient_frame_quality(avctx, "Badly received packets in frame.");
754 goto erasure;
755 }
756
757 480 apply_pitch_filters(q, outbuffer);
758
759
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 480 times.
480 if (q->bitrate == I_F_Q) {
760 erasure:
761 17 q->bitrate = I_F_Q;
762 17 q->erasure_count++;
763 17 decode_gain_and_index(q, gain);
764 17 compute_svector(q, gain, outbuffer);
765 17 decode_lspf(q, quantized_lspf);
766 17 apply_pitch_filters(q, outbuffer);
767 } else
768 480 q->erasure_count = 0;
769
770 497 formant_mem = q->formant_mem + 10;
771
2/2
✓ Branch 0 taken 1988 times.
✓ Branch 1 taken 497 times.
2485 for (i = 0; i < 4; i++) {
772 1988 interpolate_lpc(q, quantized_lspf, lpc, i);
773 1988 ff_celp_lp_synthesis_filterf(formant_mem, lpc,
774 1988 outbuffer + i * 40, 40, 10);
775 1988 formant_mem += 40;
776 }
777
778 // postfilter, as per TIA/EIA/IS-733 2.4.8.6
779 497 postfilter(q, outbuffer, lpc);
780
781 497 memcpy(q->formant_mem, q->formant_mem + 160, 10 * sizeof(float));
782
783 497 memcpy(q->prev_lspf, quantized_lspf, sizeof(q->prev_lspf));
784 497 q->prev_bitrate = q->bitrate;
785
786 497 *got_frame_ptr = 1;
787
788 497 return buf_size;
789 }
790
791 const FFCodec ff_qcelp_decoder = {
792 .p.name = "qcelp",
793 CODEC_LONG_NAME("QCELP / PureVoice"),
794 .p.type = AVMEDIA_TYPE_AUDIO,
795 .p.id = AV_CODEC_ID_QCELP,
796 .init = qcelp_decode_init,
797 FF_CODEC_DECODE_CB(qcelp_decode_frame),
798 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
799 .priv_data_size = sizeof(QCELPContext),
800 };
801