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