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