FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/speexdec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 532 774 68.7%
Functions: 22 31 71.0%
Branches: 236 454 52.0%

Line Branch Exec Source
1 /*
2 * Copyright 2002-2008 Xiph.org Foundation
3 * Copyright 2002-2008 Jean-Marc Valin
4 * Copyright 2005-2007 Analog Devices Inc.
5 * Copyright 2005-2008 Commonwealth Scientific and Industrial Research Organisation (CSIRO)
6 * Copyright 1993, 2002, 2006 David Rowe
7 * Copyright 2003 EpicGames
8 * Copyright 1992-1994 Jutta Degener, Carsten Bormann
9
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16
17 * - Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20
21 * - Neither the name of the Xiph.org Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * This file is part of FFmpeg.
38 *
39 * FFmpeg is free software; you can redistribute it and/or
40 * modify it under the terms of the GNU Lesser General Public
41 * License as published by the Free Software Foundation; either
42 * version 2.1 of the License, or (at your option) any later version.
43 *
44 * FFmpeg is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
47 * Lesser General Public License for more details.
48 *
49 * You should have received a copy of the GNU Lesser General Public
50 * License along with FFmpeg; if not, write to the Free Software
51 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
52 */
53
54 #include "libavutil/avassert.h"
55 #include "libavutil/avstring.h"
56 #include "libavutil/float_dsp.h"
57 #include "libavutil/mem.h"
58 #include "avcodec.h"
59 #include "bytestream.h"
60 #include "codec_internal.h"
61 #include "decode.h"
62 #include "get_bits.h"
63 #include "speexdata.h"
64
65 #define SPEEX_NB_MODES 3
66 #define SPEEX_INBAND_STEREO 9
67
68 #define QMF_ORDER 64
69 #define NB_ORDER 10
70 #define NB_FRAME_SIZE 160
71 #define NB_SUBMODES 9
72 #define NB_SUBMODE_BITS 4
73 #define SB_SUBMODE_BITS 3
74
75 #define NB_SUBFRAME_SIZE 40
76 #define NB_NB_SUBFRAMES 4
77 #define NB_PITCH_START 17
78 #define NB_PITCH_END 144
79
80 #define NB_DEC_BUFFER (NB_FRAME_SIZE + 2 * NB_PITCH_END + NB_SUBFRAME_SIZE + 12)
81
82 #define SPEEX_MEMSET(dst, c, n) (memset((dst), (c), (n) * sizeof(*(dst))))
83 #define SPEEX_COPY(dst, src, n) (memcpy((dst), (src), (n) * sizeof(*(dst))))
84
85 #define LSP_LINEAR(i) (.25f * (i) + .25f)
86 #define LSP_LINEAR_HIGH(i) (.3125f * (i) + .75f)
87 #define LSP_DIV_256(x) (0.00390625f * (x))
88 #define LSP_DIV_512(x) (0.001953125f * (x))
89 #define LSP_DIV_1024(x) (0.0009765625f * (x))
90
91 typedef struct LtpParams {
92 const int8_t *gain_cdbk;
93 int gain_bits;
94 int pitch_bits;
95 } LtpParam;
96
97 static const LtpParam ltp_params_vlbr = { gain_cdbk_lbr, 5, 0 };
98 static const LtpParam ltp_params_lbr = { gain_cdbk_lbr, 5, 7 };
99 static const LtpParam ltp_params_med = { gain_cdbk_lbr, 5, 7 };
100 static const LtpParam ltp_params_nb = { gain_cdbk_nb, 7, 7 };
101
102 typedef struct SplitCodebookParams {
103 int subvect_size;
104 int nb_subvect;
105 const signed char *shape_cb;
106 int shape_bits;
107 int have_sign;
108 } SplitCodebookParams;
109
110 static const SplitCodebookParams split_cb_nb_ulbr = { 20, 2, exc_20_32_table, 5, 0 };
111 static const SplitCodebookParams split_cb_nb_vlbr = { 10, 4, exc_10_16_table, 4, 0 };
112 static const SplitCodebookParams split_cb_nb_lbr = { 10, 4, exc_10_32_table, 5, 0 };
113 static const SplitCodebookParams split_cb_nb_med = { 8, 5, exc_8_128_table, 7, 0 };
114 static const SplitCodebookParams split_cb_nb = { 5, 8, exc_5_64_table, 6, 0 };
115 static const SplitCodebookParams split_cb_sb = { 5, 8, exc_5_256_table, 8, 0 };
116 static const SplitCodebookParams split_cb_high = { 8, 5, hexc_table, 7, 1 };
117 static const SplitCodebookParams split_cb_high_lbr= { 10, 4, hexc_10_32_table,5, 0 };
118
119 /** Quantizes LSPs */
120 typedef void (*lsp_quant_func)(float *, float *, int, GetBitContext *);
121
122 /** Decodes quantized LSPs */
123 typedef void (*lsp_unquant_func)(float *, int, GetBitContext *);
124
125 /** Long-term predictor quantization */
126 typedef int (*ltp_quant_func)(float *, float *, float *,
127 float *, float *, float *,
128 const void *, int, int, float, int, int,
129 GetBitContext *, char *, float *,
130 float *, int, int, int, float *);
131
132 /** Long-term un-quantize */
133 typedef void (*ltp_unquant_func)(float *, float *, int, int,
134 float, const void *, int, int *,
135 float *, GetBitContext *, int, int,
136 float, int);
137
138 /** Innovation quantization function */
139 typedef void (*innovation_quant_func)(float *, float *,
140 float *, float *, const void *,
141 int, int, float *, float *,
142 GetBitContext *, char *, int, int);
143
144 /** Innovation unquantization function */
145 typedef void (*innovation_unquant_func)(float *, const void *, int,
146 GetBitContext *, uint32_t *);
147
148 typedef struct SpeexSubmode {
149 int lbr_pitch; /**< Set to -1 for "normal" modes, otherwise encode pitch using
150 a global pitch and allowing a +- lbr_pitch variation (for
151 low not-rates)*/
152 int forced_pitch_gain; /**< Use the same (forced) pitch gain for all
153 sub-frames */
154 int have_subframe_gain; /**< Number of bits to use as sub-frame innovation
155 gain */
156 int double_codebook; /**< Apply innovation quantization twice for higher
157 quality (and higher bit-rate)*/
158 lsp_unquant_func lsp_unquant; /**< LSP unquantization function */
159
160 ltp_unquant_func ltp_unquant; /**< Long-term predictor (pitch) un-quantizer */
161 const void *LtpParam; /**< Pitch parameters (options) */
162
163 innovation_unquant_func innovation_unquant; /**< Innovation un-quantization */
164 const void *innovation_params; /**< Innovation quantization parameters*/
165
166 float comb_gain; /**< Gain of enhancer comb filter */
167 } SpeexSubmode;
168
169 typedef struct SpeexMode {
170 int modeID; /**< ID of the mode */
171 int (*decode)(AVCodecContext *avctx, void *dec, GetBitContext *gb, float *out);
172 int frame_size; /**< Size of frames used for decoding */
173 int subframe_size; /**< Size of sub-frames used for decoding */
174 int lpc_size; /**< Order of LPC filter */
175 float folding_gain; /**< Folding gain */
176 const SpeexSubmode *submodes[NB_SUBMODES]; /**< Sub-mode data for the mode */
177 int default_submode; /**< Default sub-mode to use when decoding */
178 } SpeexMode;
179
180 typedef struct DecoderState {
181 const SpeexMode *mode;
182 int modeID; /**< ID of the decoder mode */
183 int first; /**< Is first frame */
184 int full_frame_size; /**< Length of full-band frames */
185 int is_wideband; /**< If wideband is present */
186 int count_lost; /**< Was the last frame lost? */
187 int frame_size; /**< Length of high-band frames */
188 int subframe_size; /**< Length of high-band sub-frames */
189 int nb_subframes; /**< Number of high-band sub-frames */
190 int lpc_size; /**< Order of high-band LPC analysis */
191 float last_ol_gain; /**< Open-loop gain for previous frame */
192 float *innov_save; /**< If non-NULL, innovation is copied here */
193
194 /* This is used in packet loss concealment */
195 int last_pitch; /**< Pitch of last correctly decoded frame */
196 float last_pitch_gain; /**< Pitch gain of last correctly decoded frame */
197 uint32_t seed; /**< Seed used for random number generation */
198
199 int encode_submode;
200 const SpeexSubmode *const *submodes; /**< Sub-mode data */
201 int submodeID; /**< Activated sub-mode */
202 int lpc_enh_enabled; /**< 1 when LPC enhancer is on, 0 otherwise */
203
204 /* Vocoder data */
205 float voc_m1;
206 float voc_m2;
207 float voc_mean;
208 int voc_offset;
209
210 int dtx_enabled;
211 int highpass_enabled; /**< Is the input filter enabled */
212
213 float *exc; /**< Start of excitation frame */
214 float mem_hp[2]; /**< High-pass filter memory */
215 float exc_buf[NB_DEC_BUFFER]; /**< Excitation buffer */
216 float old_qlsp[NB_ORDER]; /**< Quantized LSPs for previous frame */
217 float interp_qlpc[NB_ORDER]; /**< Interpolated quantized LPCs */
218 float mem_sp[NB_ORDER]; /**< Filter memory for synthesis signal */
219 float g0_mem[QMF_ORDER];
220 float g1_mem[QMF_ORDER];
221 float pi_gain[NB_NB_SUBFRAMES]; /**< Gain of LPC filter at theta=pi (fe/2) */
222 float exc_rms[NB_NB_SUBFRAMES]; /**< RMS of excitation per subframe */
223 } DecoderState;
224
225 /* Default handler for user callbacks: skip it */
226 static int speex_default_user_handler(GetBitContext *gb, void *state, void *data)
227 {
228 const int req_size = get_bits(gb, 4);
229 skip_bits_long(gb, 5 + 8 * req_size);
230 return 0;
231 }
232
233 typedef struct StereoState {
234 float balance; /**< Left/right balance info */
235 float e_ratio; /**< Ratio of energies: E(left+right)/[E(left)+E(right)] */
236 float smooth_left; /**< Smoothed left channel gain */
237 float smooth_right; /**< Smoothed right channel gain */
238 } StereoState;
239
240 typedef struct SpeexContext {
241 AVClass *class;
242 GetBitContext gb;
243
244 int32_t version_id; /**< Version for Speex (for checking compatibility) */
245 int32_t rate; /**< Sampling rate used */
246 int32_t mode; /**< Mode used (0 for narrowband, 1 for wideband) */
247 int32_t bitstream_version; /**< Version ID of the bit-stream */
248 int32_t nb_channels; /**< Number of channels decoded */
249 int32_t bitrate; /**< Bit-rate used */
250 int32_t frame_size; /**< Size of frames */
251 int32_t vbr; /**< 1 for a VBR decoding, 0 otherwise */
252 int32_t frames_per_packet; /**< Number of frames stored per Ogg packet */
253 int32_t extra_headers; /**< Number of additional headers after the comments */
254
255 int pkt_size;
256
257 StereoState stereo;
258 DecoderState st[SPEEX_NB_MODES];
259
260 AVFloatDSPContext *fdsp;
261 } SpeexContext;
262
263 static void lsp_unquant_lbr(float *lsp, int order, GetBitContext *gb)
264 {
265 int id;
266
267 for (int i = 0; i < order; i++)
268 lsp[i] = LSP_LINEAR(i);
269
270 id = get_bits(gb, 6);
271 for (int i = 0; i < 10; i++)
272 lsp[i] += LSP_DIV_256(cdbk_nb[id * 10 + i]);
273
274 id = get_bits(gb, 6);
275 for (int i = 0; i < 5; i++)
276 lsp[i] += LSP_DIV_512(cdbk_nb_low1[id * 5 + i]);
277
278 id = get_bits(gb, 6);
279 for (int i = 0; i < 5; i++)
280 lsp[i + 5] += LSP_DIV_512(cdbk_nb_high1[id * 5 + i]);
281 }
282
283 static void forced_pitch_unquant(float *exc, float *exc_out, int start, int end,
284 float pitch_coef, const void *par, int nsf,
285 int *pitch_val, float *gain_val, GetBitContext *gb, int count_lost,
286 int subframe_offset, float last_pitch_gain, int cdbk_offset)
287 {
288 av_assert0(!isnan(pitch_coef));
289 pitch_coef = fminf(pitch_coef, .99f);
290 for (int i = 0; i < nsf; i++) {
291 exc_out[i] = exc[i - start] * pitch_coef;
292 exc[i] = exc_out[i];
293 }
294 pitch_val[0] = start;
295 gain_val[0] = gain_val[2] = 0.f;
296 gain_val[1] = pitch_coef;
297 }
298
299 static inline float speex_rand(float std, uint32_t *seed)
300 {
301 const uint32_t jflone = 0x3f800000;
302 const uint32_t jflmsk = 0x007fffff;
303 float fran;
304 uint32_t ran;
305 seed[0] = 1664525 * seed[0] + 1013904223;
306 ran = jflone | (jflmsk & seed[0]);
307 fran = av_int2float(ran);
308 fran -= 1.5f;
309 fran *= std;
310 return fran;
311 }
312
313 static void noise_codebook_unquant(float *exc, const void *par, int nsf,
314 GetBitContext *gb, uint32_t *seed)
315 {
316 for (int i = 0; i < nsf; i++)
317 exc[i] = speex_rand(1.f, seed);
318 }
319
320 24 static void split_cb_shape_sign_unquant(float *exc, const void *par, int nsf,
321 GetBitContext *gb, uint32_t *seed)
322 {
323 int subvect_size, nb_subvect, have_sign, shape_bits;
324 const SplitCodebookParams *params;
325 const signed char *shape_cb;
326 int signs[10], ind[10];
327
328 24 params = par;
329 24 subvect_size = params->subvect_size;
330 24 nb_subvect = params->nb_subvect;
331
332 24 shape_cb = params->shape_cb;
333 24 have_sign = params->have_sign;
334 24 shape_bits = params->shape_bits;
335
336 /* Decode codewords and gains */
337
2/2
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 24 times.
180 for (int i = 0; i < nb_subvect; i++) {
338
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 96 times.
156 signs[i] = have_sign ? get_bits1(gb) : 0;
339 156 ind[i] = get_bitsz(gb, shape_bits);
340 }
341 /* Compute decoded excitation */
342
2/2
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 24 times.
180 for (int i = 0; i < nb_subvect; i++) {
343
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 126 times.
156 const float s = signs[i] ? -1.f : 1.f;
344
345
2/2
✓ Branch 0 taken 960 times.
✓ Branch 1 taken 156 times.
1116 for (int j = 0; j < subvect_size; j++)
346 960 exc[subvect_size * i + j] += s * 0.03125f * shape_cb[ind[i] * subvect_size + j];
347 }
348 24 }
349
350 #define SUBMODE(x) st->submodes[st->submodeID]->x
351
352 #define gain_3tap_to_1tap(g) (FFABS(g[1]) + (g[0] > 0.f ? g[0] : -.5f * g[0]) + (g[2] > 0.f ? g[2] : -.5f * g[2]))
353
354 static void
355 12 pitch_unquant_3tap(float *exc, float *exc_out, int start, int end, float pitch_coef,
356 const void *par, int nsf, int *pitch_val, float *gain_val, GetBitContext *gb,
357 int count_lost, int subframe_offset, float last_pitch_gain, int cdbk_offset)
358 {
359 int pitch, gain_index, gain_cdbk_size;
360 const int8_t *gain_cdbk;
361 const LtpParam *params;
362 float gain[3];
363
364 12 params = (const LtpParam *)par;
365 12 gain_cdbk_size = 1 << params->gain_bits;
366 12 gain_cdbk = params->gain_cdbk + 4 * gain_cdbk_size * cdbk_offset;
367
368 12 pitch = get_bitsz(gb, params->pitch_bits);
369 12 pitch += start;
370 12 gain_index = get_bitsz(gb, params->gain_bits);
371 12 gain[0] = 0.015625f * gain_cdbk[gain_index * 4] + .5f;
372 12 gain[1] = 0.015625f * gain_cdbk[gain_index * 4 + 1] + .5f;
373 12 gain[2] = 0.015625f * gain_cdbk[gain_index * 4 + 2] + .5f;
374
375
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
12 if (count_lost && pitch > subframe_offset) {
376 float tmp = count_lost < 4 ? last_pitch_gain : 0.5f * last_pitch_gain;
377 float gain_sum;
378
379 tmp = fminf(tmp, .95f);
380 gain_sum = gain_3tap_to_1tap(gain);
381
382 if (gain_sum > tmp && gain_sum > 0.f) {
383 float fact = tmp / gain_sum;
384 for (int i = 0; i < 3; i++)
385 gain[i] *= fact;
386 }
387 }
388
389 12 pitch_val[0] = pitch;
390 12 gain_val[0] = gain[0];
391 12 gain_val[1] = gain[1];
392 12 gain_val[2] = gain[2];
393 12 SPEEX_MEMSET(exc_out, 0, nsf);
394
395
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 12 times.
48 for (int i = 0; i < 3; i++) {
396 int tmp1, tmp3;
397 36 int pp = pitch + 1 - i;
398 36 tmp1 = nsf;
399
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 9 times.
36 if (tmp1 > pp)
400 27 tmp1 = pp;
401
2/2
✓ Branch 0 taken 954 times.
✓ Branch 1 taken 36 times.
990 for (int j = 0; j < tmp1; j++)
402 954 exc_out[j] += gain[2 - i] * exc[j - pp];
403 36 tmp3 = nsf;
404
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 27 times.
36 if (tmp3 > pp + pitch)
405 9 tmp3 = pp + pitch;
406
2/2
✓ Branch 0 taken 432 times.
✓ Branch 1 taken 36 times.
468 for (int j = tmp1; j < tmp3; j++)
407 432 exc_out[j] += gain[2 - i] * exc[j - pp - pitch];
408 }
409 12 }
410
411 3 static void lsp_unquant_nb(float *lsp, int order, GetBitContext *gb)
412 {
413 int id;
414
415
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 3 times.
33 for (int i = 0; i < order; i++)
416 30 lsp[i] = LSP_LINEAR(i);
417
418 3 id = get_bits(gb, 6);
419
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 3 times.
33 for (int i = 0; i < 10; i++)
420 30 lsp[i] += LSP_DIV_256(cdbk_nb[id * 10 + i]);
421
422 3 id = get_bits(gb, 6);
423
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3 times.
18 for (int i = 0; i < 5; i++)
424 15 lsp[i] += LSP_DIV_512(cdbk_nb_low1[id * 5 + i]);
425
426 3 id = get_bits(gb, 6);
427
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3 times.
18 for (int i = 0; i < 5; i++)
428 15 lsp[i] += LSP_DIV_1024(cdbk_nb_low2[id * 5 + i]);
429
430 3 id = get_bits(gb, 6);
431
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3 times.
18 for (int i = 0; i < 5; i++)
432 15 lsp[i + 5] += LSP_DIV_512(cdbk_nb_high1[id * 5 + i]);
433
434 3 id = get_bits(gb, 6);
435
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3 times.
18 for (int i = 0; i < 5; i++)
436 15 lsp[i + 5] += LSP_DIV_1024(cdbk_nb_high2[id * 5 + i]);
437 3 }
438
439 6 static void lsp_unquant_high(float *lsp, int order, GetBitContext *gb)
440 {
441 int id;
442
443
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 6 times.
54 for (int i = 0; i < order; i++)
444 48 lsp[i] = LSP_LINEAR_HIGH(i);
445
446 6 id = get_bits(gb, 6);
447
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 6 times.
54 for (int i = 0; i < order; i++)
448 48 lsp[i] += LSP_DIV_256(high_lsp_cdbk[id * order + i]);
449
450 6 id = get_bits(gb, 6);
451
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 6 times.
54 for (int i = 0; i < order; i++)
452 48 lsp[i] += LSP_DIV_512(high_lsp_cdbk2[id * order + i]);
453 6 }
454
455 /* 2150 bps "vocoder-like" mode for comfort noise */
456 static const SpeexSubmode nb_submode1 = {
457 0, 1, 0, 0, lsp_unquant_lbr, forced_pitch_unquant, NULL,
458 noise_codebook_unquant, NULL, -1.f
459 };
460
461 /* 5.95 kbps very low bit-rate mode */
462 static const SpeexSubmode nb_submode2 = {
463 0, 0, 0, 0, lsp_unquant_lbr, pitch_unquant_3tap, &ltp_params_vlbr,
464 split_cb_shape_sign_unquant, &split_cb_nb_vlbr, .6f
465 };
466
467 /* 8 kbps low bit-rate mode */
468 static const SpeexSubmode nb_submode3 = {
469 -1, 0, 1, 0, lsp_unquant_lbr, pitch_unquant_3tap, &ltp_params_lbr,
470 split_cb_shape_sign_unquant, &split_cb_nb_lbr, .55f
471 };
472
473 /* 11 kbps medium bit-rate mode */
474 static const SpeexSubmode nb_submode4 = {
475 -1, 0, 1, 0, lsp_unquant_lbr, pitch_unquant_3tap, &ltp_params_med,
476 split_cb_shape_sign_unquant, &split_cb_nb_med, .45f
477 };
478
479 /* 15 kbps high bit-rate mode */
480 static const SpeexSubmode nb_submode5 = {
481 -1, 0, 3, 0, lsp_unquant_nb, pitch_unquant_3tap, &ltp_params_nb,
482 split_cb_shape_sign_unquant, &split_cb_nb, .25f
483 };
484
485 /* 18.2 high bit-rate mode */
486 static const SpeexSubmode nb_submode6 = {
487 -1, 0, 3, 0, lsp_unquant_nb, pitch_unquant_3tap, &ltp_params_nb,
488 split_cb_shape_sign_unquant, &split_cb_sb, .15f
489 };
490
491 /* 24.6 kbps high bit-rate mode */
492 static const SpeexSubmode nb_submode7 = {
493 -1, 0, 3, 1, lsp_unquant_nb, pitch_unquant_3tap, &ltp_params_nb,
494 split_cb_shape_sign_unquant, &split_cb_nb, 0.05f
495 };
496
497 /* 3.95 kbps very low bit-rate mode */
498 static const SpeexSubmode nb_submode8 = {
499 0, 1, 0, 0, lsp_unquant_lbr, forced_pitch_unquant, NULL,
500 split_cb_shape_sign_unquant, &split_cb_nb_ulbr, .5f
501 };
502
503 static const SpeexSubmode wb_submode1 = {
504 0, 0, 1, 0, lsp_unquant_high, NULL, NULL,
505 NULL, NULL, -1.f
506 };
507
508 static const SpeexSubmode wb_submode2 = {
509 0, 0, 1, 0, lsp_unquant_high, NULL, NULL,
510 split_cb_shape_sign_unquant, &split_cb_high_lbr, -1.f
511 };
512
513 static const SpeexSubmode wb_submode3 = {
514 0, 0, 1, 0, lsp_unquant_high, NULL, NULL,
515 split_cb_shape_sign_unquant, &split_cb_high, -1.f
516 };
517
518 static const SpeexSubmode wb_submode4 = {
519 0, 0, 1, 1, lsp_unquant_high, NULL, NULL,
520 split_cb_shape_sign_unquant, &split_cb_high, -1.f
521 };
522
523 static int nb_decode(AVCodecContext *, void *, GetBitContext *, float *);
524 static int sb_decode(AVCodecContext *, void *, GetBitContext *, float *);
525
526 static const SpeexMode speex_modes[SPEEX_NB_MODES] = {
527 {
528 .modeID = 0,
529 .decode = nb_decode,
530 .frame_size = NB_FRAME_SIZE,
531 .subframe_size = NB_SUBFRAME_SIZE,
532 .lpc_size = NB_ORDER,
533 .submodes = {
534 NULL, &nb_submode1, &nb_submode2, &nb_submode3, &nb_submode4,
535 &nb_submode5, &nb_submode6, &nb_submode7, &nb_submode8
536 },
537 .default_submode = 5,
538 },
539 {
540 .modeID = 1,
541 .decode = sb_decode,
542 .frame_size = NB_FRAME_SIZE,
543 .subframe_size = NB_SUBFRAME_SIZE,
544 .lpc_size = 8,
545 .folding_gain = 0.9f,
546 .submodes = {
547 NULL, &wb_submode1, &wb_submode2, &wb_submode3, &wb_submode4
548 },
549 .default_submode = 3,
550 },
551 {
552 .modeID = 2,
553 .decode = sb_decode,
554 .frame_size = 320,
555 .subframe_size = 80,
556 .lpc_size = 8,
557 .folding_gain = 0.7f,
558 .submodes = {
559 NULL, &wb_submode1
560 },
561 .default_submode = 1,
562 },
563 };
564
565 48 static float compute_rms(const float *x, int len)
566 {
567 48 float sum = 0.f;
568
569
2/2
✓ Branch 0 taken 2880 times.
✓ Branch 1 taken 48 times.
2928 for (int i = 0; i < len; i++)
570 2880 sum += x[i] * x[i];
571
572
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 av_assert0(len > 0);
573 48 return sqrtf(.1f + sum / len);
574 }
575
576 static void bw_lpc(float gamma, const float *lpc_in,
577 float *lpc_out, int order)
578 {
579 float tmp = gamma;
580
581 for (int i = 0; i < order; i++) {
582 lpc_out[i] = tmp * lpc_in[i];
583 tmp *= gamma;
584 }
585 }
586
587 36 static void iir_mem(const float *x, const float *den,
588 float *y, int N, int ord, float *mem)
589 {
590
2/2
✓ Branch 0 taken 1920 times.
✓ Branch 1 taken 36 times.
1956 for (int i = 0; i < N; i++) {
591 1920 float yi = x[i] + mem[0];
592 1920 float nyi = -yi;
593
2/2
✓ Branch 0 taken 14400 times.
✓ Branch 1 taken 1920 times.
16320 for (int j = 0; j < ord - 1; j++)
594 14400 mem[j] = mem[j + 1] + den[j] * nyi;
595 1920 mem[ord - 1] = den[ord - 1] * nyi;
596 1920 y[i] = yi;
597 }
598 36 }
599
600 3 static void highpass(const float *x, float *y, int len, float *mem, int wide)
601 {
602 static const float Pcoef[2][3] = {{ 1.00000f, -1.92683f, 0.93071f }, { 1.00000f, -1.97226f, 0.97332f } };
603 static const float Zcoef[2][3] = {{ 0.96446f, -1.92879f, 0.96446f }, { 0.98645f, -1.97277f, 0.98645f } };
604 const float *den, *num;
605
606 3 den = Pcoef[wide];
607 3 num = Zcoef[wide];
608
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 3 times.
483 for (int i = 0; i < len; i++) {
609 480 float yi = num[0] * x[i] + mem[0];
610 480 mem[0] = mem[1] + num[1] * x[i] + -den[1] * yi;
611 480 mem[1] = num[2] * x[i] + -den[2] * yi;
612 480 y[i] = yi;
613 }
614 3 }
615
616 #define median3(a, b, c) \
617 ((a) < (b) ? ((b) < (c) ? (b) : ((a) < (c) ? (c) : (a))) \
618 : ((c) < (b) ? (b) : ((c) < (a) ? (c) : (a))))
619
620 static int speex_std_stereo(GetBitContext *gb, void *state, void *data)
621 {
622 StereoState *stereo = data;
623 float sign = get_bits1(gb) ? -1.f : 1.f;
624
625 stereo->balance = exp(sign * .25f * get_bits(gb, 5));
626 stereo->e_ratio = e_ratio_quant[get_bits(gb, 2)];
627
628 return 0;
629 }
630
631 static int speex_inband_handler(GetBitContext *gb, void *state, StereoState *stereo)
632 {
633 int id = get_bits(gb, 4);
634
635 if (id == SPEEX_INBAND_STEREO) {
636 return speex_std_stereo(gb, state, stereo);
637 } else {
638 int adv;
639
640 if (id < 2)
641 adv = 1;
642 else if (id < 8)
643 adv = 4;
644 else if (id < 10)
645 adv = 8;
646 else if (id < 12)
647 adv = 16;
648 else if (id < 14)
649 adv = 32;
650 else
651 adv = 64;
652 skip_bits_long(gb, adv);
653 }
654 return 0;
655 }
656
657 12 static void sanitize_values(float *vec, float min_val, float max_val, int len)
658 {
659
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 12 times.
492 for (int i = 0; i < len; i++) {
660
3/4
✓ Branch 0 taken 360 times.
✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 360 times.
480 if (!isnormal(vec[i]) || fabsf(vec[i]) < 1e-8f)
661 120 vec[i] = 0.f;
662 else
663 360 vec[i] = av_clipf(vec[i], min_val, max_val);
664 }
665 12 }
666
667 24 static void signal_mul(const float *x, float *y, float scale, int len)
668 {
669
2/2
✓ Branch 0 taken 960 times.
✓ Branch 1 taken 24 times.
984 for (int i = 0; i < len; i++)
670 960 y[i] = scale * x[i];
671 24 }
672
673 114 static float inner_prod(const float *x, const float *y, int len)
674 {
675 114 float sum = 0.f;
676
677
2/2
✓ Branch 0 taken 1140 times.
✓ Branch 1 taken 114 times.
1254 for (int i = 0; i < len; i += 8) {
678 1140 float part = 0.f;
679 1140 part += x[i + 0] * y[i + 0];
680 1140 part += x[i + 1] * y[i + 1];
681 1140 part += x[i + 2] * y[i + 2];
682 1140 part += x[i + 3] * y[i + 3];
683 1140 part += x[i + 4] * y[i + 4];
684 1140 part += x[i + 5] * y[i + 5];
685 1140 part += x[i + 6] * y[i + 6];
686 1140 part += x[i + 7] * y[i + 7];
687 1140 sum += part;
688 }
689
690 114 return sum;
691 }
692
693 12 static int interp_pitch(const float *exc, float *interp, int pitch, int len)
694 {
695 float corr[4][7], maxcorr;
696 int maxi, maxj;
697
698
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 12 times.
96 for (int i = 0; i < 7; i++)
699 84 corr[0][i] = inner_prod(exc, exc - pitch - 3 + i, len);
700
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 12 times.
48 for (int i = 0; i < 3; i++) {
701
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 36 times.
288 for (int j = 0; j < 7; j++) {
702 int i1, i2;
703 252 float tmp = 0.f;
704
705 252 i1 = 3 - j;
706
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 144 times.
252 if (i1 < 0)
707 108 i1 = 0;
708 252 i2 = 10 - j;
709
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 144 times.
252 if (i2 > 7)
710 108 i2 = 7;
711
2/2
✓ Branch 0 taken 1332 times.
✓ Branch 1 taken 252 times.
1584 for (int k = i1; k < i2; k++)
712 1332 tmp += shift_filt[i][k] * corr[0][j + k - 3];
713 252 corr[i + 1][j] = tmp;
714 }
715 }
716 12 maxi = maxj = 0;
717 12 maxcorr = corr[0][0];
718
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 12 times.
60 for (int i = 0; i < 4; i++) {
719
2/2
✓ Branch 0 taken 336 times.
✓ Branch 1 taken 48 times.
384 for (int j = 0; j < 7; j++) {
720
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 324 times.
336 if (corr[i][j] > maxcorr) {
721 12 maxcorr = corr[i][j];
722 12 maxi = i;
723 12 maxj = j;
724 }
725 }
726 }
727
2/2
✓ Branch 0 taken 960 times.
✓ Branch 1 taken 12 times.
972 for (int i = 0; i < len; i++) {
728 960 float tmp = 0.f;
729
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 480 times.
960 if (maxi > 0.f) {
730
2/2
✓ Branch 0 taken 3360 times.
✓ Branch 1 taken 480 times.
3840 for (int k = 0; k < 7; k++)
731 3360 tmp += exc[i - (pitch - maxj + 3) + k - 3] * shift_filt[maxi - 1][k];
732 } else {
733 480 tmp = exc[i - (pitch - maxj + 3)];
734 }
735 960 interp[i] = tmp;
736 }
737 12 return pitch - maxj + 3;
738 }
739
740 6 static void multicomb(const float *exc, float *new_exc, float *ak, int p, int nsf,
741 int pitch, int max_pitch, float comb_gain)
742 {
743 float old_ener, new_ener;
744 float iexc0_mag, iexc1_mag, exc_mag;
745 float iexc[4 * NB_SUBFRAME_SIZE];
746 float corr0, corr1, gain0, gain1;
747 float pgain1, pgain2;
748 float c1, c2, g1, g2;
749 float ngain, gg1, gg2;
750 6 int corr_pitch = pitch;
751
752 6 interp_pitch(exc, iexc, corr_pitch, 80);
753
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (corr_pitch > max_pitch)
754 6 interp_pitch(exc, iexc + nsf, 2 * corr_pitch, 80);
755 else
756 interp_pitch(exc, iexc + nsf, -corr_pitch, 80);
757
758 6 iexc0_mag = sqrtf(1000.f + inner_prod(iexc, iexc, nsf));
759 6 iexc1_mag = sqrtf(1000.f + inner_prod(iexc + nsf, iexc + nsf, nsf));
760 6 exc_mag = sqrtf(1.f + inner_prod(exc, exc, nsf));
761 6 corr0 = inner_prod(iexc, exc, nsf);
762 6 corr1 = inner_prod(iexc + nsf, exc, nsf);
763
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (corr0 > iexc0_mag * exc_mag)
764 pgain1 = 1.f;
765 else
766 6 pgain1 = (corr0 / exc_mag) / iexc0_mag;
767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (corr1 > iexc1_mag * exc_mag)
768 pgain2 = 1.f;
769 else
770 6 pgain2 = (corr1 / exc_mag) / iexc1_mag;
771 6 gg1 = exc_mag / iexc0_mag;
772 6 gg2 = exc_mag / iexc1_mag;
773
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (comb_gain > 0.f) {
774 6 c1 = .4f * comb_gain + .07f;
775 6 c2 = .5f + 1.72f * (c1 - .07f);
776 } else {
777 c1 = c2 = 0.f;
778 }
779 6 g1 = 1.f - c2 * pgain1 * pgain1;
780 6 g2 = 1.f - c2 * pgain2 * pgain2;
781 6 g1 = fmaxf(g1, c1);
782 6 g2 = fmaxf(g2, c1);
783 6 g1 = c1 / g1;
784 6 g2 = c1 / g2;
785
786
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (corr_pitch > max_pitch) {
787 6 gain0 = .7f * g1 * gg1;
788 6 gain1 = .3f * g2 * gg2;
789 } else {
790 gain0 = .6f * g1 * gg1;
791 gain1 = .6f * g2 * gg2;
792 }
793
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 6 times.
486 for (int i = 0; i < nsf; i++)
794 480 new_exc[i] = exc[i] + (gain0 * iexc[i]) + (gain1 * iexc[i + nsf]);
795 6 new_ener = compute_rms(new_exc, nsf);
796 6 old_ener = compute_rms(exc, nsf);
797
798 6 old_ener = fmaxf(old_ener, 1.f);
799 6 new_ener = fmaxf(new_ener, 1.f);
800 6 old_ener = fminf(old_ener, new_ener);
801 6 ngain = old_ener / new_ener;
802
803
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 6 times.
486 for (int i = 0; i < nsf; i++)
804 480 new_exc[i] *= ngain;
805 6 }
806
807 36 static void lsp_interpolate(const float *old_lsp, const float *new_lsp,
808 float *lsp, int len, int subframe,
809 int nb_subframes, float margin)
810 {
811 36 const float tmp = (1.f + subframe) / nb_subframes;
812
813
2/2
✓ Branch 0 taken 312 times.
✓ Branch 1 taken 36 times.
348 for (int i = 0; i < len; i++) {
814 312 lsp[i] = (1.f - tmp) * old_lsp[i] + tmp * new_lsp[i];
815 312 lsp[i] = av_clipf(lsp[i], margin, M_PI - margin);
816 }
817
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 36 times.
276 for (int i = 1; i < len - 1; i++) {
818 240 lsp[i] = fmaxf(lsp[i], lsp[i - 1] + margin);
819
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 240 times.
240 if (lsp[i] > lsp[i + 1] - margin)
820 lsp[i] = .5f * (lsp[i] + lsp[i + 1] - margin);
821 }
822 36 }
823
824 36 static void lsp_to_lpc(const float *freq, float *ak, int lpcrdr)
825 {
826 float xout1, xout2, xin1, xin2;
827 float *pw, *n0;
828 36 float Wp[4 * NB_ORDER + 2] = { 0 };
829 float x_freq[NB_ORDER];
830 36 const int m = lpcrdr >> 1;
831
832 36 pw = Wp;
833
834 36 xin1 = xin2 = 1.f;
835
836
2/2
✓ Branch 0 taken 312 times.
✓ Branch 1 taken 36 times.
348 for (int i = 0; i < lpcrdr; i++)
837 312 x_freq[i] = -cosf(freq[i]);
838
839 /* reconstruct P(z) and Q(z) by cascading second order
840 * polynomials in form 1 - 2xz(-1) +z(-2), where x is the
841 * LSP coefficient
842 */
843
2/2
✓ Branch 0 taken 348 times.
✓ Branch 1 taken 36 times.
384 for (int j = 0; j <= lpcrdr; j++) {
844 348 int i2 = 0;
845
2/2
✓ Branch 0 taken 1524 times.
✓ Branch 1 taken 348 times.
1872 for (int i = 0; i < m; i++, i2 += 2) {
846 1524 n0 = pw + (i * 4);
847 1524 xout1 = xin1 + 2.f * x_freq[i2 ] * n0[0] + n0[1];
848 1524 xout2 = xin2 + 2.f * x_freq[i2 + 1] * n0[2] + n0[3];
849 1524 n0[1] = n0[0];
850 1524 n0[3] = n0[2];
851 1524 n0[0] = xin1;
852 1524 n0[2] = xin2;
853 1524 xin1 = xout1;
854 1524 xin2 = xout2;
855 }
856 348 xout1 = xin1 + n0[4];
857 348 xout2 = xin2 - n0[5];
858
2/2
✓ Branch 0 taken 312 times.
✓ Branch 1 taken 36 times.
348 if (j > 0)
859 312 ak[j - 1] = (xout1 + xout2) * 0.5f;
860 348 n0[4] = xin1;
861 348 n0[5] = xin2;
862
863 348 xin1 = 0.f;
864 348 xin2 = 0.f;
865 }
866 36 }
867
868 3 static int nb_decode(AVCodecContext *avctx, void *ptr_st,
869 GetBitContext *gb, float *out)
870 {
871 3 DecoderState *st = ptr_st;
872 3 float ol_gain = 0, ol_pitch_coef = 0, best_pitch_gain = 0, pitch_average = 0;
873 3 int m, pitch, wideband, ol_pitch = 0, best_pitch = 40;
874 3 SpeexContext *s = avctx->priv_data;
875 float innov[NB_SUBFRAME_SIZE];
876 float exc32[NB_SUBFRAME_SIZE];
877 float interp_qlsp[NB_ORDER];
878 float qlsp[NB_ORDER];
879 float ak[NB_ORDER];
880 3 float pitch_gain[3] = { 0 };
881
882 3 st->exc = st->exc_buf + 2 * NB_PITCH_END + NB_SUBFRAME_SIZE + 6;
883
884
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (st->encode_submode) {
885 do { /* Search for next narrowband block (handle requests, skip wideband blocks) */
886
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (get_bits_left(gb) < 5)
887 return AVERROR_INVALIDDATA;
888 3 wideband = get_bits1(gb);
889
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (wideband) /* Skip wideband block (for compatibility) */ {
890 int submode, advance;
891
892 submode = get_bits(gb, SB_SUBMODE_BITS);
893 advance = wb_skip_table[submode];
894 advance -= SB_SUBMODE_BITS + 1;
895 if (advance < 0)
896 return AVERROR_INVALIDDATA;
897 skip_bits_long(gb, advance);
898
899 if (get_bits_left(gb) < 5)
900 return AVERROR_INVALIDDATA;
901 wideband = get_bits1(gb);
902 if (wideband) {
903 submode = get_bits(gb, SB_SUBMODE_BITS);
904 advance = wb_skip_table[submode];
905 advance -= SB_SUBMODE_BITS + 1;
906 if (advance < 0)
907 return AVERROR_INVALIDDATA;
908 skip_bits_long(gb, advance);
909 wideband = get_bits1(gb);
910 if (wideband) {
911 av_log(avctx, AV_LOG_ERROR, "more than two wideband layers found\n");
912 return AVERROR_INVALIDDATA;
913 }
914 }
915 }
916
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (get_bits_left(gb) < 4)
917 return AVERROR_INVALIDDATA;
918 3 m = get_bits(gb, 4);
919
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (m == 15) /* We found a terminator */ {
920 return AVERROR_INVALIDDATA;
921
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (m == 14) /* Speex in-band request */ {
922 int ret = speex_inband_handler(gb, st, &s->stereo);
923 if (ret)
924 return ret;
925
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (m == 13) /* User in-band request */ {
926 int ret = speex_default_user_handler(gb, st, NULL);
927 if (ret)
928 return ret;
929
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (m > 8) /* Invalid mode */ {
930 return AVERROR_INVALIDDATA;
931 }
932
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } while (m > 8);
933
934 3 st->submodeID = m; /* Get the sub-mode that was used */
935 }
936
937 /* Shift all buffers by one frame */
938 3 memmove(st->exc_buf, st->exc_buf + NB_FRAME_SIZE, (2 * NB_PITCH_END + NB_SUBFRAME_SIZE + 12) * sizeof(float));
939
940 /* If null mode (no transmission), just set a couple things to zero */
941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (st->submodes[st->submodeID] == NULL) {
942 float lpc[NB_ORDER];
943 float innov_gain = 0.f;
944
945 bw_lpc(0.93f, st->interp_qlpc, lpc, NB_ORDER);
946 innov_gain = compute_rms(st->exc, NB_FRAME_SIZE);
947 for (int i = 0; i < NB_FRAME_SIZE; i++)
948 st->exc[i] = speex_rand(innov_gain, &st->seed);
949
950 /* Final signal synthesis from excitation */
951 iir_mem(st->exc, lpc, out, NB_FRAME_SIZE, NB_ORDER, st->mem_sp);
952 st->count_lost = 0;
953
954 return 0;
955 }
956
957 /* Unquantize LSPs */
958 3 SUBMODE(lsp_unquant)(qlsp, NB_ORDER, gb);
959
960 /* Damp memory if a frame was lost and the LSP changed too much */
961
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (st->count_lost) {
962 float fact, lsp_dist = 0;
963
964 for (int i = 0; i < NB_ORDER; i++)
965 lsp_dist = lsp_dist + FFABS(st->old_qlsp[i] - qlsp[i]);
966 fact = .6f * exp(-.2f * lsp_dist);
967 for (int i = 0; i < NB_ORDER; i++)
968 st->mem_sp[i] = fact * st->mem_sp[i];
969 }
970
971 /* Handle first frame and lost-packet case */
972
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 if (st->first || st->count_lost)
973 3 memcpy(st->old_qlsp, qlsp, sizeof(st->old_qlsp));
974
975 /* Get open-loop pitch estimation for low bit-rate pitch coding */
976
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (SUBMODE(lbr_pitch) != -1)
977 ol_pitch = NB_PITCH_START + get_bits(gb, 7);
978
979
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (SUBMODE(forced_pitch_gain))
980 ol_pitch_coef = 0.066667f * get_bits(gb, 4);
981
982 /* Get global excitation gain */
983 3 ol_gain = expf(get_bits(gb, 5) / 3.5f);
984
985
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (st->submodeID == 1)
986 st->dtx_enabled = get_bits(gb, 4) == 15;
987
988
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (st->submodeID > 1)
989 3 st->dtx_enabled = 0;
990
991
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
15 for (int sub = 0; sub < NB_NB_SUBFRAMES; sub++) { /* Loop on subframes */
992 12 float *exc, *innov_save = NULL, tmp, ener;
993 int pit_min, pit_max, offset, q_energy;
994
995 12 offset = NB_SUBFRAME_SIZE * sub; /* Offset relative to start of frame */
996 12 exc = st->exc + offset; /* Excitation */
997
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (st->innov_save) /* Original signal */
998 12 innov_save = st->innov_save + offset;
999
1000 12 SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE); /* Reset excitation */
1001
1002 /* Adaptive codebook contribution */
1003
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 av_assert0(SUBMODE(ltp_unquant));
1004 /* Handle pitch constraints if any */
1005
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (SUBMODE(lbr_pitch) != -1) {
1006 int margin = SUBMODE(lbr_pitch);
1007
1008 if (margin) {
1009 pit_min = ol_pitch - margin + 1;
1010 pit_min = FFMAX(pit_min, NB_PITCH_START);
1011 pit_max = ol_pitch + margin;
1012 pit_max = FFMIN(pit_max, NB_PITCH_START);
1013 } else {
1014 pit_min = pit_max = ol_pitch;
1015 }
1016 } else {
1017 12 pit_min = NB_PITCH_START;
1018 12 pit_max = NB_PITCH_END;
1019 }
1020
1021 12 SUBMODE(ltp_unquant)(exc, exc32, pit_min, pit_max, ol_pitch_coef, SUBMODE(LtpParam),
1022 NB_SUBFRAME_SIZE, &pitch, pitch_gain, gb, st->count_lost, offset,
1023 st->last_pitch_gain, 0);
1024
1025 12 sanitize_values(exc32, -32000, 32000, NB_SUBFRAME_SIZE);
1026
1027
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 9 times.
12 tmp = gain_3tap_to_1tap(pitch_gain);
1028
1029 12 pitch_average += tmp;
1030
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 if ((tmp > best_pitch_gain &&
1031
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 FFABS(2 * best_pitch - pitch) >= 3 &&
1032
2/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
3 FFABS(3 * best_pitch - pitch) >= 4 &&
1033
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 FFABS(4 * best_pitch - pitch) >= 5) ||
1034
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 (tmp > .6f * best_pitch_gain &&
1035
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 (FFABS(best_pitch - 2 * pitch) < 3 ||
1036
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 FFABS(best_pitch - 3 * pitch) < 4 ||
1037
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 FFABS(best_pitch - 4 * pitch) < 5)) ||
1038
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 ((.67f * tmp) > best_pitch_gain &&
1039
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 (FFABS(2 * best_pitch - pitch) < 3 ||
1040 FFABS(3 * best_pitch - pitch) < 4 ||
1041 FFABS(4 * best_pitch - pitch) < 5))) {
1042 6 best_pitch = pitch;
1043
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (tmp > best_pitch_gain)
1044 6 best_pitch_gain = tmp;
1045 }
1046
1047 12 memset(innov, 0, sizeof(innov));
1048
1049 /* Decode sub-frame gain correction */
1050
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (SUBMODE(have_subframe_gain) == 3) {
1051 12 q_energy = get_bits(gb, 3);
1052 12 ener = exc_gain_quant_scal3[q_energy] * ol_gain;
1053 } else if (SUBMODE(have_subframe_gain) == 1) {
1054 q_energy = get_bits1(gb);
1055 ener = exc_gain_quant_scal1[q_energy] * ol_gain;
1056 } else {
1057 ener = ol_gain;
1058 }
1059
1060
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 av_assert0(SUBMODE(innovation_unquant));
1061 /* Fixed codebook contribution */
1062 12 SUBMODE(innovation_unquant)(innov, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, gb, &st->seed);
1063 /* De-normalize innovation and update excitation */
1064
1065 12 signal_mul(innov, innov, ener, NB_SUBFRAME_SIZE);
1066
1067 /* Decode second codebook (only for some modes) */
1068
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (SUBMODE(double_codebook)) {
1069 float innov2[NB_SUBFRAME_SIZE] = { 0 };
1070
1071 SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, gb, &st->seed);
1072 signal_mul(innov2, innov2, 0.454545f * ener, NB_SUBFRAME_SIZE);
1073 for (int i = 0; i < NB_SUBFRAME_SIZE; i++)
1074 innov[i] += innov2[i];
1075 }
1076
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 12 times.
492 for (int i = 0; i < NB_SUBFRAME_SIZE; i++)
1077 480 exc[i] = exc32[i] + innov[i];
1078
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (innov_save)
1079 12 memcpy(innov_save, innov, sizeof(innov));
1080
1081 /* Vocoder mode */
1082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (st->submodeID == 1) {
1083 float g = ol_pitch_coef;
1084
1085 g = av_clipf(1.5f * (g - .2f), 0.f, 1.f);
1086
1087 SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE);
1088 while (st->voc_offset < NB_SUBFRAME_SIZE) {
1089 if (st->voc_offset >= 0)
1090 exc[st->voc_offset] = sqrtf(2.f * ol_pitch) * (g * ol_gain);
1091 st->voc_offset += ol_pitch;
1092 }
1093 st->voc_offset -= NB_SUBFRAME_SIZE;
1094
1095 for (int i = 0; i < NB_SUBFRAME_SIZE; i++) {
1096 float exci = exc[i];
1097 exc[i] = (.7f * exc[i] + .3f * st->voc_m1) + ((1.f - .85f * g) * innov[i]) - .15f * g * st->voc_m2;
1098 st->voc_m1 = exci;
1099 st->voc_m2 = innov[i];
1100 st->voc_mean = .8f * st->voc_mean + .2f * exc[i];
1101 exc[i] -= st->voc_mean;
1102 }
1103 }
1104 }
1105
1106
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 if (st->lpc_enh_enabled && SUBMODE(comb_gain) > 0 && !st->count_lost) {
1107 3 multicomb(st->exc - NB_SUBFRAME_SIZE, out, st->interp_qlpc, NB_ORDER,
1108 3 2 * NB_SUBFRAME_SIZE, best_pitch, 40, SUBMODE(comb_gain));
1109 3 multicomb(st->exc + NB_SUBFRAME_SIZE, out + 2 * NB_SUBFRAME_SIZE,
1110 3 st->interp_qlpc, NB_ORDER, 2 * NB_SUBFRAME_SIZE, best_pitch, 40,
1111 3 SUBMODE(comb_gain));
1112 } else {
1113 SPEEX_COPY(out, &st->exc[-NB_SUBFRAME_SIZE], NB_FRAME_SIZE);
1114 }
1115
1116 /* If the last packet was lost, re-scale the excitation to obtain the same
1117 * energy as encoded in ol_gain */
1118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (st->count_lost) {
1119 float exc_ener, gain;
1120
1121 exc_ener = compute_rms(st->exc, NB_FRAME_SIZE);
1122 av_assert0(exc_ener + 1.f > 0.f);
1123 gain = fminf(ol_gain / (exc_ener + 1.f), 2.f);
1124 for (int i = 0; i < NB_FRAME_SIZE; i++) {
1125 st->exc[i] *= gain;
1126 out[i] = st->exc[i - NB_SUBFRAME_SIZE];
1127 }
1128 }
1129
1130
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
15 for (int sub = 0; sub < NB_NB_SUBFRAMES; sub++) { /* Loop on subframes */
1131 12 const int offset = NB_SUBFRAME_SIZE * sub; /* Offset relative to start of frame */
1132 12 float pi_g = 1.f, *sp = out + offset; /* Original signal */
1133
1134 12 lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, NB_ORDER, sub, NB_NB_SUBFRAMES, 0.002f);
1135 12 lsp_to_lpc(interp_qlsp, ak, NB_ORDER); /* Compute interpolated LPCs (unquantized) */
1136
1137
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 12 times.
72 for (int i = 0; i < NB_ORDER; i += 2) /* Compute analysis filter at w=pi */
1138 60 pi_g += ak[i + 1] - ak[i];
1139 12 st->pi_gain[sub] = pi_g;
1140 12 st->exc_rms[sub] = compute_rms(st->exc + offset, NB_SUBFRAME_SIZE);
1141
1142 12 iir_mem(sp, st->interp_qlpc, sp, NB_SUBFRAME_SIZE, NB_ORDER, st->mem_sp);
1143
1144 12 memcpy(st->interp_qlpc, ak, sizeof(st->interp_qlpc));
1145 }
1146
1147
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (st->highpass_enabled)
1148 3 highpass(out, out, NB_FRAME_SIZE, st->mem_hp, st->is_wideband);
1149
1150 /* Store the LSPs for interpolation in the next frame */
1151 3 memcpy(st->old_qlsp, qlsp, sizeof(st->old_qlsp));
1152
1153 3 st->count_lost = 0;
1154 3 st->last_pitch = best_pitch;
1155 3 st->last_pitch_gain = .25f * pitch_average;
1156 3 st->last_ol_gain = ol_gain;
1157 3 st->first = 0;
1158
1159 3 return 0;
1160 }
1161
1162 6 static void qmf_synth(const float *x1, const float *x2, const float *a, float *y, int N, int M, float *mem1, float *mem2)
1163 {
1164 6 const int M2 = M >> 1, N2 = N >> 1;
1165 float xx1[352], xx2[352];
1166
1167
2/2
✓ Branch 0 taken 1440 times.
✓ Branch 1 taken 6 times.
1446 for (int i = 0; i < N2; i++)
1168 1440 xx1[i] = x1[N2-1-i];
1169
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 6 times.
198 for (int i = 0; i < M2; i++)
1170 192 xx1[N2+i] = mem1[2*i+1];
1171
2/2
✓ Branch 0 taken 1440 times.
✓ Branch 1 taken 6 times.
1446 for (int i = 0; i < N2; i++)
1172 1440 xx2[i] = x2[N2-1-i];
1173
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 6 times.
198 for (int i = 0; i < M2; i++)
1174 192 xx2[N2+i] = mem2[2*i+1];
1175
1176
2/2
✓ Branch 0 taken 720 times.
✓ Branch 1 taken 6 times.
726 for (int i = 0; i < N2; i += 2) {
1177 float y0, y1, y2, y3;
1178 float x10, x20;
1179
1180 720 y0 = y1 = y2 = y3 = 0.f;
1181 720 x10 = xx1[N2-2-i];
1182 720 x20 = xx2[N2-2-i];
1183
1184
2/2
✓ Branch 0 taken 11520 times.
✓ Branch 1 taken 720 times.
12240 for (int j = 0; j < M2; j += 2) {
1185 float x11, x21;
1186 float a0, a1;
1187
1188 11520 a0 = a[2*j];
1189 11520 a1 = a[2*j+1];
1190 11520 x11 = xx1[N2-1+j-i];
1191 11520 x21 = xx2[N2-1+j-i];
1192
1193 11520 y0 += a0 * (x11-x21);
1194 11520 y1 += a1 * (x11+x21);
1195 11520 y2 += a0 * (x10-x20);
1196 11520 y3 += a1 * (x10+x20);
1197 11520 a0 = a[2*j+2];
1198 11520 a1 = a[2*j+3];
1199 11520 x10 = xx1[N2+j-i];
1200 11520 x20 = xx2[N2+j-i];
1201
1202 11520 y0 += a0 * (x10-x20);
1203 11520 y1 += a1 * (x10+x20);
1204 11520 y2 += a0 * (x11-x21);
1205 11520 y3 += a1 * (x11+x21);
1206 }
1207 720 y[2 * i ] = 2.f * y0;
1208 720 y[2 * i+1] = 2.f * y1;
1209 720 y[2 * i+2] = 2.f * y2;
1210 720 y[2 * i+3] = 2.f * y3;
1211 }
1212
1213
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 6 times.
198 for (int i = 0; i < M2; i++)
1214 192 mem1[2*i+1] = xx1[i];
1215
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 6 times.
198 for (int i = 0; i < M2; i++)
1216 192 mem2[2*i+1] = xx2[i];
1217 6 }
1218
1219 6 static int sb_decode(AVCodecContext *avctx, void *ptr_st,
1220 GetBitContext *gb, float *out)
1221 {
1222 6 SpeexContext *s = avctx->priv_data;
1223 6 DecoderState *st = ptr_st;
1224 float low_pi_gain[NB_NB_SUBFRAMES];
1225 float low_exc_rms[NB_NB_SUBFRAMES];
1226 float interp_qlsp[NB_ORDER];
1227 int ret, wideband;
1228 float *low_innov_alias;
1229 float qlsp[NB_ORDER];
1230 float ak[NB_ORDER];
1231 const SpeexMode *mode;
1232
1233 6 mode = st->mode;
1234
1235
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (st->modeID > 0) {
1236 6 low_innov_alias = out + st->frame_size;
1237 6 s->st[st->modeID - 1].innov_save = low_innov_alias;
1238 6 ret = speex_modes[st->modeID - 1].decode(avctx, &s->st[st->modeID - 1], gb, out);
1239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1240 return ret;
1241 }
1242
1243
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (st->encode_submode) { /* Check "wideband bit" */
1244
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 if (get_bits_left(gb) > 0)
1245 6 wideband = show_bits1(gb);
1246 else
1247 wideband = 0;
1248
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (wideband) { /* Regular wideband frame, read the submode */
1249 6 wideband = get_bits1(gb);
1250 6 st->submodeID = get_bits(gb, SB_SUBMODE_BITS);
1251 } else { /* Was a narrowband frame, set "null submode" */
1252 st->submodeID = 0;
1253 }
1254
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (st->submodeID != 0 && st->submodes[st->submodeID] == NULL)
1255 return AVERROR_INVALIDDATA;
1256 }
1257
1258 /* If null mode (no transmission), just set a couple things to zero */
1259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (st->submodes[st->submodeID] == NULL) {
1260 for (int i = 0; i < st->frame_size; i++)
1261 out[st->frame_size + i] = 1e-15f;
1262
1263 st->first = 1;
1264
1265 /* Final signal synthesis from excitation */
1266 iir_mem(out + st->frame_size, st->interp_qlpc, out + st->frame_size, st->frame_size, st->lpc_size, st->mem_sp);
1267
1268 qmf_synth(out, out + st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem);
1269
1270 return 0;
1271 }
1272
1273 6 memcpy(low_pi_gain, s->st[st->modeID - 1].pi_gain, sizeof(low_pi_gain));
1274 6 memcpy(low_exc_rms, s->st[st->modeID - 1].exc_rms, sizeof(low_exc_rms));
1275
1276 6 SUBMODE(lsp_unquant)(qlsp, st->lpc_size, gb);
1277
1278
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (st->first)
1279 6 memcpy(st->old_qlsp, qlsp, sizeof(st->old_qlsp));
1280
1281
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 6 times.
30 for (int sub = 0; sub < st->nb_subframes; sub++) {
1282 float filter_ratio, el, rl, rh;
1283 24 float *innov_save = NULL, *sp;
1284 float exc[80];
1285 int offset;
1286
1287 24 offset = st->subframe_size * sub;
1288 24 sp = out + st->frame_size + offset;
1289 /* Pointer for saving innovation */
1290
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
24 if (st->innov_save) {
1291 12 innov_save = st->innov_save + 2 * offset;
1292 12 SPEEX_MEMSET(innov_save, 0, 2 * st->subframe_size);
1293 }
1294
1295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 av_assert0(st->nb_subframes > 0);
1296 24 lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpc_size, sub, st->nb_subframes, 0.05f);
1297 24 lsp_to_lpc(interp_qlsp, ak, st->lpc_size);
1298
1299 /* Calculate reponse ratio between the low and high filter in the middle
1300 of the band (4000 Hz) */
1301 24 st->pi_gain[sub] = 1.f;
1302 24 rh = 1.f;
1303
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 24 times.
120 for (int i = 0; i < st->lpc_size; i += 2) {
1304 96 rh += ak[i + 1] - ak[i];
1305 96 st->pi_gain[sub] += ak[i] + ak[i + 1];
1306 }
1307
1308 24 rl = low_pi_gain[sub];
1309 24 filter_ratio = (rl + .01f) / (rh + .01f);
1310
1311 24 SPEEX_MEMSET(exc, 0, st->subframe_size);
1312
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
24 if (!SUBMODE(innovation_unquant)) {
1313 12 const int x = get_bits(gb, 5);
1314 12 const float g = expf(.125f * (x - 10)) / filter_ratio;
1315
1316
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 12 times.
492 for (int i = 0; i < st->subframe_size; i += 2) {
1317 480 exc[i ] = mode->folding_gain * low_innov_alias[offset + i ] * g;
1318 480 exc[i + 1] = -mode->folding_gain * low_innov_alias[offset + i + 1] * g;
1319 }
1320 } else {
1321 float gc, scale;
1322
1323 12 el = low_exc_rms[sub];
1324 12 gc = 0.87360f * gc_quant_bound[get_bits(gb, 4)];
1325
1326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (st->subframe_size == 80)
1327 gc *= M_SQRT2;
1328
1329 12 scale = (gc * el) / filter_ratio;
1330 12 SUBMODE(innovation_unquant)
1331 12 (exc, SUBMODE(innovation_params), st->subframe_size,
1332 gb, &st->seed);
1333
1334 12 signal_mul(exc, exc, scale, st->subframe_size);
1335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (SUBMODE(double_codebook)) {
1336 float innov2[80];
1337
1338 SPEEX_MEMSET(innov2, 0, st->subframe_size);
1339 SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframe_size, gb, &st->seed);
1340 signal_mul(innov2, innov2, 0.4f * scale, st->subframe_size);
1341 for (int i = 0; i < st->subframe_size; i++)
1342 exc[i] += innov2[i];
1343 }
1344 }
1345
1346
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
24 if (st->innov_save) {
1347
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 12 times.
492 for (int i = 0; i < st->subframe_size; i++)
1348 480 innov_save[2 * i] = exc[i];
1349 }
1350
1351 24 iir_mem(st->exc_buf, st->interp_qlpc, sp, st->subframe_size, st->lpc_size, st->mem_sp);
1352 24 memcpy(st->exc_buf, exc, sizeof(exc));
1353 24 memcpy(st->interp_qlpc, ak, sizeof(st->interp_qlpc));
1354 24 st->exc_rms[sub] = compute_rms(st->exc_buf, st->subframe_size);
1355 }
1356
1357 6 qmf_synth(out, out + st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem);
1358 6 memcpy(st->old_qlsp, qlsp, sizeof(st->old_qlsp));
1359
1360 6 st->first = 0;
1361
1362 6 return 0;
1363 }
1364
1365 9 static int decoder_init(SpeexContext *s, DecoderState *st, const SpeexMode *mode)
1366 {
1367 9 st->mode = mode;
1368 9 st->modeID = mode->modeID;
1369
1370 9 st->first = 1;
1371 9 st->encode_submode = 1;
1372 9 st->is_wideband = st->modeID > 0;
1373 9 st->innov_save = NULL;
1374
1375 9 st->submodes = mode->submodes;
1376 9 st->submodeID = mode->default_submode;
1377 9 st->subframe_size = mode->subframe_size;
1378 9 st->lpc_size = mode->lpc_size;
1379
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 st->full_frame_size = (1 + (st->modeID > 0)) * mode->frame_size;
1380 9 st->nb_subframes = mode->frame_size / mode->subframe_size;
1381 9 st->frame_size = mode->frame_size;
1382
1383 9 st->lpc_enh_enabled = 1;
1384
1385 9 st->last_pitch = 40;
1386 9 st->count_lost = 0;
1387 9 st->seed = 1000;
1388 9 st->last_ol_gain = 0;
1389
1390 9 st->voc_m1 = st->voc_m2 = st->voc_mean = 0;
1391 9 st->voc_offset = 0;
1392 9 st->dtx_enabled = 0;
1393 9 st->highpass_enabled = mode->modeID == 0;
1394
1395 9 return 0;
1396 }
1397
1398 3 static int parse_speex_extradata(AVCodecContext *avctx,
1399 const uint8_t *extradata, int extradata_size)
1400 {
1401 3 SpeexContext *s = avctx->priv_data;
1402 3 const uint8_t *buf = av_strnstr(extradata, "Speex ", extradata_size);
1403
1404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!buf)
1405 return AVERROR_INVALIDDATA;
1406
1407 3 buf += 28;
1408
1409 3 s->version_id = bytestream_get_le32(&buf);
1410 3 buf += 4;
1411 3 s->rate = bytestream_get_le32(&buf);
1412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->rate <= 0)
1413 return AVERROR_INVALIDDATA;
1414 3 s->mode = bytestream_get_le32(&buf);
1415
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (s->mode < 0 || s->mode >= SPEEX_NB_MODES)
1416 return AVERROR_INVALIDDATA;
1417 3 s->bitstream_version = bytestream_get_le32(&buf);
1418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->bitstream_version != 4)
1419 return AVERROR_INVALIDDATA;
1420 3 s->nb_channels = bytestream_get_le32(&buf);
1421
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (s->nb_channels <= 0 || s->nb_channels > 2)
1422 return AVERROR_INVALIDDATA;
1423 3 s->bitrate = bytestream_get_le32(&buf);
1424 3 s->frame_size = bytestream_get_le32(&buf);
1425
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
6 if (s->frame_size < NB_FRAME_SIZE << (s->mode > 0) ||
1426
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 s->frame_size > INT32_MAX >> (s->mode > 0))
1427 return AVERROR_INVALIDDATA;
1428 3 s->frame_size <<= (s->mode > 0);
1429 3 s->vbr = bytestream_get_le32(&buf);
1430 3 s->frames_per_packet = bytestream_get_le32(&buf);
1431
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (s->frames_per_packet <= 0 ||
1432
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 s->frames_per_packet > 64 ||
1433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 s->frames_per_packet >= INT32_MAX / s->nb_channels / s->frame_size)
1434 return AVERROR_INVALIDDATA;
1435 3 s->extra_headers = bytestream_get_le32(&buf);
1436
1437 3 return 0;
1438 }
1439
1440 3 static av_cold int speex_decode_init(AVCodecContext *avctx)
1441 {
1442 3 SpeexContext *s = avctx->priv_data;
1443 int ret;
1444
1445 3 s->fdsp = avpriv_float_dsp_alloc(0);
1446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!s->fdsp)
1447 return AVERROR(ENOMEM);
1448
1449
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (avctx->extradata && avctx->extradata_size >= 80) {
1450 3 ret = parse_speex_extradata(avctx, avctx->extradata, avctx->extradata_size);
1451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
1452 return ret;
1453 } else {
1454 s->rate = avctx->sample_rate;
1455 if (s->rate <= 0)
1456 return AVERROR_INVALIDDATA;
1457
1458 s->nb_channels = avctx->ch_layout.nb_channels;
1459 if (s->nb_channels <= 0 || s->nb_channels > 2)
1460 return AVERROR_INVALIDDATA;
1461
1462 switch (s->rate) {
1463 case 8000: s->mode = 0; break;
1464 case 16000: s->mode = 1; break;
1465 case 32000: s->mode = 2; break;
1466 default: s->mode = 2;
1467 }
1468
1469 s->frames_per_packet = 64;
1470 s->frame_size = NB_FRAME_SIZE << s->mode;
1471 }
1472
1473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (avctx->codec_tag == MKTAG('S', 'P', 'X', 'N')) {
1474 int quality;
1475
1476 if (!avctx->extradata || avctx->extradata && avctx->extradata_size < 47) {
1477 av_log(avctx, AV_LOG_ERROR, "Missing or invalid extradata.\n");
1478 return AVERROR_INVALIDDATA;
1479 }
1480
1481 quality = avctx->extradata[37];
1482 if (quality > 10) {
1483 av_log(avctx, AV_LOG_ERROR, "Unsupported quality mode %d.\n", quality);
1484 return AVERROR_PATCHWELCOME;
1485 }
1486
1487 s->pkt_size = ((const uint8_t[]){ 5, 10, 15, 20, 20, 28, 28, 38, 38, 46, 62 })[quality];
1488
1489 s->mode = 0;
1490 s->nb_channels = 1;
1491 s->rate = avctx->sample_rate;
1492 if (s->rate <= 0)
1493 return AVERROR_INVALIDDATA;
1494 s->frames_per_packet = 1;
1495 s->frame_size = NB_FRAME_SIZE;
1496 }
1497
1498
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (s->bitrate > 0)
1499 3 avctx->bit_rate = s->bitrate;
1500 3 av_channel_layout_uninit(&avctx->ch_layout);
1501 3 avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
1502 3 avctx->ch_layout.nb_channels = s->nb_channels;
1503 3 avctx->sample_rate = s->rate;
1504 3 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
1505
1506
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
12 for (int m = 0; m <= s->mode; m++) {
1507 9 ret = decoder_init(s, &s->st[m], &speex_modes[m]);
1508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
1509 return ret;
1510 }
1511
1512 3 s->stereo.balance = 1.f;
1513 3 s->stereo.e_ratio = .5f;
1514 3 s->stereo.smooth_left = 1.f;
1515 3 s->stereo.smooth_right = 1.f;
1516
1517 3 return 0;
1518 }
1519
1520 static void speex_decode_stereo(float *data, int frame_size, StereoState *stereo)
1521 {
1522 float balance, e_left, e_right, e_ratio;
1523
1524 balance = stereo->balance;
1525 e_ratio = stereo->e_ratio;
1526
1527 /* These two are Q14, with max value just below 2. */
1528 e_right = 1.f / sqrtf(e_ratio * (1.f + balance));
1529 e_left = sqrtf(balance) * e_right;
1530
1531 for (int i = frame_size - 1; i >= 0; i--) {
1532 float tmp = data[i];
1533 stereo->smooth_left = stereo->smooth_left * 0.98f + e_left * 0.02f;
1534 stereo->smooth_right = stereo->smooth_right * 0.98f + e_right * 0.02f;
1535 data[2 * i ] = stereo->smooth_left * tmp;
1536 data[2 * i + 1] = stereo->smooth_right * tmp;
1537 }
1538 }
1539
1540 3 static int speex_decode_frame(AVCodecContext *avctx, AVFrame *frame,
1541 int *got_frame_ptr, AVPacket *avpkt)
1542 {
1543 3 SpeexContext *s = avctx->priv_data;
1544 3 int frames_per_packet = s->frames_per_packet;
1545 3 const float scale = 1.f / 32768.f;
1546 3 int buf_size = avpkt->size;
1547 float *dst;
1548 int ret;
1549
1550
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 if (s->pkt_size && avpkt->size == 62)
1551 buf_size = s->pkt_size;
1552
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = init_get_bits8(&s->gb, avpkt->data, buf_size)) < 0)
1553 return ret;
1554
1555 3 frame->nb_samples = FFALIGN(s->frame_size * frames_per_packet, 4);
1556
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1557 return ret;
1558
1559 3 dst = (float *)frame->extended_data[0];
1560
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 for (int i = 0; i < frames_per_packet; i++) {
1561 3 ret = speex_modes[s->mode].decode(avctx, &s->st[s->mode], &s->gb, dst + i * s->frame_size);
1562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
1563 return ret;
1564
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (avctx->ch_layout.nb_channels == 2)
1565 speex_decode_stereo(dst + i * s->frame_size, s->frame_size, &s->stereo);
1566
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
3 if (get_bits_left(&s->gb) < 5 ||
1567 show_bits(&s->gb, 5) == 15) {
1568 3 frames_per_packet = i + 1;
1569 3 break;
1570 }
1571 }
1572
1573 3 dst = (float *)frame->extended_data[0];
1574 3 s->fdsp->vector_fmul_scalar(dst, dst, scale, frame->nb_samples * frame->ch_layout.nb_channels);
1575 3 frame->nb_samples = s->frame_size * frames_per_packet;
1576
1577 3 *got_frame_ptr = 1;
1578
1579 3 return (get_bits_count(&s->gb) + 7) >> 3;
1580 }
1581
1582 3 static av_cold int speex_decode_close(AVCodecContext *avctx)
1583 {
1584 3 SpeexContext *s = avctx->priv_data;
1585 3 av_freep(&s->fdsp);
1586 3 return 0;
1587 }
1588
1589 const FFCodec ff_speex_decoder = {
1590 .p.name = "speex",
1591 CODEC_LONG_NAME("Speex"),
1592 .p.type = AVMEDIA_TYPE_AUDIO,
1593 .p.id = AV_CODEC_ID_SPEEX,
1594 .init = speex_decode_init,
1595 FF_CODEC_DECODE_CB(speex_decode_frame),
1596 .close = speex_decode_close,
1597 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
1598 .priv_data_size = sizeof(SpeexContext),
1599 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1600 };
1601