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
61
62 #define SPEEX_NB_MODES 3
63 #define SPEEX_INBAND_STEREO 9
64
67 #define NB_FRAME_SIZE 160
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
92 } LtpParam;
93
98
106
115
116 /** Quantizes LSPs */
118
119 /** Decodes quantized LSPs */
121
122 /** Long-term predictor quantization */
124 float *, float *, float *,
128
129 /** Long-term un-quantize */
131 float,
const void *,
int,
int *,
134
135 /** Innovation quantization function */
137 float *, float *, const void *,
138 int,
int,
float *,
float *,
140
141 /** Innovation unquantization function */
144
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)*/
150 sub-frames */
152 gain */
154 quality (and higher bit-rate)*/
156
158 const void *
LtpParam;
/**< Pitch parameters (options) */
159
162
165
176
179 int modeID;
/**< ID of the decoder mode */
187 int lpc_size;
/**< Order of high-band LPC analysis */
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 */
194 uint32_t
seed;
/**< Seed used for random number generation */
195
200
201 /* Vocoder data */
206
209
210 float *
exc;
/**< Start of excitation frame */
211 float mem_hp[2];
/**< High-pass filter memory */
221
222 /* Default handler for user callbacks: skip it */
224 {
225 const int req_size =
get_bits(gb, 4);
227 return 0;
228 }
229
231 float balance;
/**< Left/right balance info */
232 float e_ratio;
/**< Ratio of energies: E(left+right)/[E(left)+E(right)] */
236
240
243 int32_t mode;
/**< Mode used (0 for narrowband, 1 for wideband) */
251
253
256
259
261 {
263
264 for (
int i = 0;
i < order;
i++)
266
268 for (
int i = 0;
i < 10;
i++)
270
272 for (
int i = 0;
i < 5;
i++)
274
276 for (
int i = 0;
i < 5;
i++)
278 }
279
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 {
286 pitch_coef =
fminf(pitch_coef, .99
f);
287 for (
int i = 0;
i < nsf;
i++) {
288 exc_out[
i] = exc[
i - start] * pitch_coef;
290 }
291 pitch_val[0] = start;
292 gain_val[0] = gain_val[2] = 0.f;
293 gain_val[1] = pitch_coef;
294 }
295
297 {
298 const uint32_t jflone = 0x3f800000;
299 const uint32_t jflmsk = 0x007fffff;
300 float fran;
302 seed[0] = 1664525 *
seed[0] + 1013904223;
303 ran = jflone | (jflmsk &
seed[0]);
305 fran -= 1.5f;
306 fran *= std;
307 return fran;
308 }
309
312 {
313 for (
int i = 0;
i < nsf;
i++)
315 }
316
319 {
320 int subvect_size, nb_subvect, have_sign, shape_bits;
322 const signed char *shape_cb;
323 int signs[10], ind[10];
324
325 params = par;
328
332
333 /* Decode codewords and gains */
334 for (
int i = 0;
i < nb_subvect;
i++) {
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.03125
f * 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
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
378
379 if (gain_sum >
tmp && gain_sum > 0.
f) {
381 for (
int i = 0;
i < 3;
i++)
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];
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
409 {
411
412 for (
int i = 0;
i < order;
i++)
414
416 for (
int i = 0;
i < 10;
i++)
418
420 for (
int i = 0;
i < 5;
i++)
422
424 for (
int i = 0;
i < 5;
i++)
426
428 for (
int i = 0;
i < 5;
i++)
430
432 for (
int i = 0;
i < 5;
i++)
434 }
435
437 {
439
440 for (
int i = 0;
i < order;
i++)
442
444 for (
int i = 0;
i < order;
i++)
446
448 for (
int i = 0;
i < order;
i++)
450 }
451
452 /* 2150 bps "vocoder-like" mode for comfort noise */
456 };
457
458 /* 5.95 kbps very low bit-rate mode */
462 };
463
464 /* 8 kbps low bit-rate mode */
468 };
469
470 /* 11 kbps medium bit-rate mode */
474 };
475
476 /* 15 kbps high bit-rate mode */
480 };
481
482 /* 18.2 high bit-rate mode */
486 };
487
488 /* 24.6 kbps high bit-rate mode */
492 };
493
494 /* 3.95 kbps very low bit-rate mode */
498 };
499
503 };
504
508 };
509
513 };
514
518 };
519
522
524 {
530 .submodes = {
533 },
534 .default_submode = 5,
535 },
536 {
537 .modeID = 1,
541 .lpc_size = 8,
542 .folding_gain = 0.9f,
543 .submodes = {
545 },
546 .default_submode = 3,
547 },
548 {
549 .modeID = 2,
551 .frame_size = 320,
552 .subframe_size = 80,
553 .lpc_size = 8,
554 .folding_gain = 0.7f,
555 .submodes = {
557 },
558 .default_submode = 1,
559 },
560 };
561
563 {
564 float sum = 0.f;
565
566 for (
int i = 0;
i <
len;
i++)
568
570 return sqrtf(.1
f + sum /
len);
571 }
572
573 static void bw_lpc(
float gamma,
const float *lpc_in,
574 float *lpc_out, int order)
575 {
577
578 for (
int i = 0;
i < order;
i++) {
579 lpc_out[
i] =
tmp * lpc_in[
i];
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;
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;
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
618 {
621
624
625 return 0;
626 }
627
629 {
631
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;
650 }
651 return 0;
652 }
653
655 {
656 for (
int i = 0;
i <
len;
i++) {
657 if (!isnormal(vec[
i]) ||
fabsf(vec[
i]) < 1e-8
f)
659 else
661 }
662 }
663
665 {
666 for (
int i = 0;
i <
len;
i++)
668 }
669
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
691 {
692 float corr[4][7], maxcorr;
693 int maxi, maxj;
694
695 for (
int i = 0;
i < 7;
i++)
697 for (
int i = 0;
i < 3;
i++) {
698 for (int j = 0; j < 7; j++) {
699 int i1, i2;
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++)
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];
720 maxj = j;
721 }
722 }
723 }
724 for (
int i = 0;
i <
len;
i++) {
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 }
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;
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
750 if (corr_pitch > max_pitch)
752 else
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));
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 {
775 }
776 g1 = 1.f -
c2 * pgain1 * pgain1;
777 g2 = 1.f -
c2 * pgain2 * pgain2;
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]);
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++)
802 }
803
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];
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;
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) {
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.5
f;
857 n0[4] = xin1;
858 n0[5] = xin2;
859
860 xin1 = 0.f;
861 xin2 = 0.f;
862 }
863 }
864
867 {
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;
877 float pitch_gain[3] = { 0 };
878
880
882 do { /* Search for next narrowband block (handle requests, skip wideband blocks) */
886 if (wideband) /* Skip wideband block (for compatibility) */ {
887 int submode, advance;
888
892 if (advance < 0)
895
899 if (wideband) {
903 if (advance < 0)
907 if (wideband) {
910 }
911 }
912 }
916 if (m == 15) /* We found a terminator */ {
918 } else if (m == 14) /* Speex in-band request */ {
922 } else if (m == 13) /* User in-band request */ {
926 } else if (m > 8) /* Invalid mode */ {
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 */
936
937 /* If null mode (no transmission), just set a couple things to zero */
940 float innov_gain = 0.f;
941
946
947 /* Final signal synthesis from excitation */
950
951 return 0;
952 }
953
954 /* Unquantize LSPs */
956
957 /* Damp memory if a frame was lost and the LSP changed too much */
959 float fact, lsp_dist = 0;
960
966 }
967
968 /* Handle first frame and lost-packet case */
971
972 /* Get open-loop pitch estimation for low bit-rate pitch coding */
975
976 if (
SUBMODE(forced_pitch_gain))
977 ol_pitch_coef = 0.066667f *
get_bits(gb, 4);
978
979 /* Get global excitation gain */
981
984
987
989 float *exc, *innov_save =
NULL,
tmp, ener;
990 int pit_min, pit_max,
offset, q_energy;
991
996
998
999 /* Adaptive codebook contribution */
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;
1008 pit_max = ol_pitch + margin;
1010 } else {
1011 pit_min = pit_max = ol_pitch;
1012 }
1013 } else {
1016 }
1017
1018 SUBMODE(ltp_unquant)(exc, exc32, pit_min, pit_max, ol_pitch_coef,
SUBMODE(LtpParam),
1021
1023
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 > .6
f * 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 ((.67
f *
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) {
1050 }
else if (
SUBMODE(have_subframe_gain) == 1) {
1053 } else {
1054 ener = ol_gain;
1055 }
1056
1058 /* Fixed codebook contribution */
1060 /* De-normalize innovation and update excitation */
1061
1063
1064 /* Decode second codebook (only for some modes) */
1065 if (
SUBMODE(double_codebook)) {
1067
1071 innov[
i] += innov2[
i];
1072 }
1074 exc[
i] = exc32[
i] + innov[
i];
1075 if (innov_save)
1076 memcpy(innov_save, innov, sizeof(innov));
1077
1078 /* Vocoder mode */
1080 float g = ol_pitch_coef;
1081
1083
1087 exc[st->
voc_offset] = sqrtf(2.
f * ol_pitch) * (
g * ol_gain);
1089 }
1091
1093 float exci = exc[
i];
1094 exc[
i] = (.7f * exc[
i] + .3f * st->
voc_m1) + ((1.
f - .85
f *
g) * innov[
i]) - .15
f *
g * st->
voc_m2;
1099 }
1100 }
1101 }
1102
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 */
1116 float exc_ener, gain;
1117
1120 gain =
fminf(ol_gain / (exc_ener + 1.
f), 2.
f);
1124 }
1125 }
1126
1129 float pi_g = 1.f, *
sp =
out +
offset;
/* Original signal */
1130
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];
1138
1140
1142 }
1143
1146
1147 /* Store the LSPs for interpolation in the next frame */
1149
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;
1180
1181 for (int j = 0; j < M2; j += 2) {
1182 float x11, x21;
1184
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);
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
1218 {
1225 float *low_innov_alias;
1229
1231
1234 s->st[st->
modeID - 1].innov_save = low_innov_alias;
1238 }
1239
1243 else
1244 wideband = 0;
1245 if (wideband) { /* Regular wideband frame, read the submode */
1248 } else { /* Was a narrowband frame, set "null submode" */
1250 }
1253 }
1254
1255 /* If null mode (no transmission), just set a couple things to zero */
1259
1261
1262 /* Final signal synthesis from excitation */
1264
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
1274
1277
1279 float filter_ratio, el, rl, rh;
1280 float *innov_save =
NULL, *
sp;
1281 float exc[80];
1283
1286 /* Pointer for saving innovation */
1290 }
1291
1295
1296 /* Calculate reponse ratio between the low and high filter in the middle
1297 of the band (4000 Hz) */
1299 rh = 1.f;
1301 rh += ak[
i + 1] - ak[
i];
1303 }
1304
1305 rl = low_pi_gain[
sub];
1306 filter_ratio = (rl + .01f) / (rh + .01
f);
1307
1309 if (!
SUBMODE(innovation_unquant)) {
1311 const float g =
expf(.125
f * (x - 10)) / filter_ratio;
1312
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 {
1319
1320 el = low_exc_rms[
sub];
1322
1325
1326 scale = (gc * el) / filter_ratio;
1330
1332 if (
SUBMODE(double_codebook)) {
1333 float innov2[80];
1334
1339 exc[
i] += innov2[
i];
1340 }
1341 }
1342
1345 innov_save[2 *
i] = exc[
i];
1346 }
1347
1349 memcpy(st->
exc_buf, exc,
sizeof(exc));
1352 }
1353
1356
1358
1359 return 0;
1360 }
1361
1363 {
1366
1371
1379
1381
1386
1391
1392 return 0;
1393 }
1394
1396 const uint8_t *extradata, int extradata_size)
1397 {
1399 const uint8_t *buf = extradata;
1400
1401 if (memcmp(buf, "Speex ", 8))
1403
1404 buf += 28;
1405
1406 s->version_id = bytestream_get_le32(&buf);
1407 buf += 4;
1408 s->rate = bytestream_get_le32(&buf);
1411 s->mode = bytestream_get_le32(&buf);
1414 s->bitstream_version = bytestream_get_le32(&buf);
1415 if (
s->bitstream_version != 4)
1417 s->nb_channels = bytestream_get_le32(&buf);
1418 if (
s->nb_channels <= 0 ||
s->nb_channels > 2)
1420 s->bitrate = bytestream_get_le32(&buf);
1421 s->frame_size = bytestream_get_le32(&buf);
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)
1430 s->extra_headers = bytestream_get_le32(&buf);
1431
1432 return 0;
1433 }
1434
1436 {
1439
1443
1448 } else {
1452
1454 if (
s->nb_channels <= 0)
1456
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;
1466 }
1467
1470
1474 }
1475
1480 }
1481
1482 s->pkt_size = ((
const uint8_t[]){ 5, 10, 15, 20, 20, 28, 28, 38, 38, 46, 62 })[
quality];
1483
1489 s->frames_per_packet = 1;
1491 }
1492
1498
1499 for (
int m = 0; m <=
s->mode; m++) {
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
1514 {
1515 float balance, e_left, e_right, e_ratio;
1516
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
1530 }
1531 }
1532
1534 int *got_frame_ptr,
AVPacket *avpkt)
1535 {
1538 const float scale = 1.f / 32768.f;
1539 int buf_size = avpkt->
size;
1540 float *dst;
1542
1543 if (
s->pkt_size && avpkt->
size == 62)
1544 buf_size =
s->pkt_size;
1547
1548 frame->nb_samples =
FFALIGN(
s->frame_size *
s->frames_per_packet, 4);
1551
1552 dst = (
float *)
frame->extended_data[0];
1553 for (
int i = 0;
i <
s->frames_per_packet;
i++) {
1559 }
1560
1561 dst = (
float *)
frame->extended_data[0];
1562 s->fdsp->vector_fmul_scalar(dst, dst,
scale,
frame->nb_samples *
frame->channels);
1563 frame->nb_samples =
s->frame_size *
s->frames_per_packet;
1564
1565 *got_frame_ptr = 1;
1566
1567 return buf_size;
1568 }
1569
1571 {
1574 return 0;
1575 }
1576
1588 };