diff options
author | Nils Wallménius <nils@rockbox.org> | 2013-05-20 22:25:57 +0200 |
---|---|---|
committer | Nils Wallménius <nils@rockbox.org> | 2013-08-31 08:30:51 +0200 |
commit | 580b307fd791c0997a8831bc800bba87797bfb7e (patch) | |
tree | 807846056f06fd944a750ce41217a877910ebd59 /lib/rbcodec/codecs/libopus/celt | |
parent | 74761b70acd96cecc0d35450dd56a98ad9ee7d3d (diff) | |
download | rockbox-580b307fd791c0997a8831bc800bba87797bfb7e.tar.gz rockbox-580b307fd791c0997a8831bc800bba87797bfb7e.zip |
Sync opus codec to upstream git
Sync opus codec to upstream commit
02fed471a4568852d6618e041c4f2af0d7730ee2 (August 30 2013)
This brings in a lot of optimizations but also makes the diff
between our codec and the upstream much smaller as most of our
optimizations have been upstreamed or supeceded.
Speedups across the board for CELT mode files:
64kbps 128kbps
H300 9.82MHz 15.48MHz
c200 4.86MHz 9.63MHz
fuze v1 10.32MHz 15.92MHz
For the silk mode test file (16kbps) arm targets get a speedup
of about 2MHz while the H300 is 7.8MHz slower, likely because it's
now using the pseudostack more rather than the real stack which
is in iram. Patches to get around that are upcomming.
Change-Id: Ifecf963e461c51ac42e09dac1e91bc4bc3b12fa3
Diffstat (limited to 'lib/rbcodec/codecs/libopus/celt')
39 files changed, 3681 insertions, 3968 deletions
diff --git a/lib/rbcodec/codecs/libopus/celt/_kiss_fft_guts.h b/lib/rbcodec/codecs/libopus/celt/_kiss_fft_guts.h index 63e2548843..866a6520ca 100644 --- a/lib/rbcodec/codecs/libopus/celt/_kiss_fft_guts.h +++ b/lib/rbcodec/codecs/libopus/celt/_kiss_fft_guts.h | |||
@@ -61,76 +61,9 @@ | |||
61 | do{ (m).r = SUB32(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)); \ | 61 | do{ (m).r = SUB32(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)); \ |
62 | (m).i = ADD32(S_MUL((a).r,(b).i) , S_MUL((a).i,(b).r)); }while(0) | 62 | (m).i = ADD32(S_MUL((a).r,(b).i) , S_MUL((a).i,(b).r)); }while(0) |
63 | 63 | ||
64 | #if defined (CPU_COLDFIRE) | ||
65 | # define C_MULC(m,a,b) \ | ||
66 | { \ | ||
67 | asm volatile("move.l (%[bp]), %%d2;" \ | ||
68 | "clr.l %%d3;" \ | ||
69 | "move.w %%d2, %%d3;" \ | ||
70 | "swap %%d3;" \ | ||
71 | "clr.w %%d2;" \ | ||
72 | "movem.l (%[ap]), %%d0-%%d1;" \ | ||
73 | "mac.l %%d0, %%d2, %%acc0;" \ | ||
74 | "mac.l %%d1, %%d3, %%acc0;" \ | ||
75 | "mac.l %%d1, %%d2, %%acc1;" \ | ||
76 | "msac.l %%d0, %%d3, %%acc1;" \ | ||
77 | "movclr.l %%acc0, %[mr];" \ | ||
78 | "movclr.l %%acc1, %[mi];" \ | ||
79 | : [mr] "=r" ((m).r), [mi] "=r" ((m).i) \ | ||
80 | : [ap] "a" (&(a)), [bp] "a" (&(b)) \ | ||
81 | : "d0", "d1", "d2", "d3", "cc"); \ | ||
82 | } | ||
83 | #elif defined(CPU_ARM) | ||
84 | #if (ARM_ARCH < 5) | ||
85 | |||
86 | |||
87 | # define C_MULC(m,a,b) \ | ||
88 | { \ | ||
89 | asm volatile( \ | ||
90 | "ldm %[ap], {r0,r1} \n\t" \ | ||
91 | "ldrsh r2, [%[bp], #0] \n\t" \ | ||
92 | "ldrsh r3, [%[bp], #2] \n\t" \ | ||
93 | \ | ||
94 | "smull r4, %[mr], r0, r2 \n\t" \ | ||
95 | "smlal r4, %[mr], r1, r3 \n\t" \ | ||
96 | "mov r4, r4, lsr #15 \n\t" \ | ||
97 | "orr %[mr], r4, %[mr], lsl #17 \n\t" \ | ||
98 | \ | ||
99 | "smull r4, %[mi], r1, r2 \n\t" \ | ||
100 | "rsb r3, r3, #0 \n\t" \ | ||
101 | "smlal r4, %[mi], r0, r3 \n\t" \ | ||
102 | "mov r4, r4, lsr #15 \n\t" \ | ||
103 | "orr %[mi], r4, %[mi], lsl #17 \n\t" \ | ||
104 | : [mr] "=r" ((m).r), [mi] "=r" ((m).i) \ | ||
105 | : [ap] "r" (&(a)), [bp] "r" (&(b)) \ | ||
106 | : "r0", "r1", "r2", "r3", "r4"); \ | ||
107 | } | ||
108 | #else | ||
109 | /*same as above but using armv5 packed multiplies*/ | ||
110 | # define C_MULC(m,a,b) \ | ||
111 | { \ | ||
112 | asm volatile( \ | ||
113 | "ldm %[ap], {r0,r1} \n\t" \ | ||
114 | "ldr r2, [%[bp], #0] \n\t" \ | ||
115 | \ | ||
116 | "smulwb r4, r0, r2 \n\t" /*r4=a.r*b.r*/ \ | ||
117 | "smlawt %[mr], r1, r2, r4 \n\t" /*m.r=r4+a.i*b.i*/\ | ||
118 | "mov %[mr], %[mr], lsl #1 \n\t" /*Q15 not Q16*/ \ | ||
119 | \ | ||
120 | "smulwb r1, r1, r2 \n\t" /*r1=a.i*b.r*/ \ | ||
121 | "smulwt r4, r0, r2 \n\t" /*r4=a.r*b.i*/ \ | ||
122 | "sub %[mi], r1, r4 \n\t" \ | ||
123 | "mov %[mi], %[mi], lsl #1 \n\t" \ | ||
124 | : [mr] "=r" ((m).r), [mi] "=r" ((m).i) \ | ||
125 | : [ap] "r" (&(a)), [bp] "r" (&(b)) \ | ||
126 | : "r0", "r1", "r2", "r4"); \ | ||
127 | } | ||
128 | #endif /*ARMv5 code*/ | ||
129 | #else | ||
130 | # define C_MULC(m,a,b) \ | 64 | # define C_MULC(m,a,b) \ |
131 | do{ (m).r = ADD32(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)); \ | 65 | do{ (m).r = ADD32(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)); \ |
132 | (m).i = SUB32(S_MUL((a).i,(b).r) , S_MUL((a).r,(b).i)); }while(0) | 66 | (m).i = SUB32(S_MUL((a).i,(b).r) , S_MUL((a).r,(b).i)); }while(0) |
133 | #endif | ||
134 | 67 | ||
135 | # define C_MUL4(m,a,b) \ | 68 | # define C_MUL4(m,a,b) \ |
136 | do{ (m).r = SHR32(SUB32(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)),2); \ | 69 | do{ (m).r = SHR32(SUB32(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)),2); \ |
@@ -161,6 +94,18 @@ | |||
161 | do {(res).r = ADD32((res).r,(a).r); (res).i = SUB32((res).i,(a).i); \ | 94 | do {(res).r = ADD32((res).r,(a).r); (res).i = SUB32((res).i,(a).i); \ |
162 | }while(0) | 95 | }while(0) |
163 | 96 | ||
97 | #if defined(ARMv4_ASM) | ||
98 | #include "arm/kiss_fft_armv4.h" | ||
99 | #endif | ||
100 | |||
101 | #if defined(ARMv5E_ASM) | ||
102 | #include "arm/kiss_fft_armv5e.h" | ||
103 | #endif | ||
104 | |||
105 | #if defined(CF_ASM) | ||
106 | #include "cf/kiss_fft_cf.h" | ||
107 | #endif | ||
108 | |||
164 | #else /* not FIXED_POINT*/ | 109 | #else /* not FIXED_POINT*/ |
165 | 110 | ||
166 | # define S_MUL(a,b) ( (a)*(b) ) | 111 | # define S_MUL(a,b) ( (a)*(b) ) |
diff --git a/lib/rbcodec/codecs/libopus/celt/arch.h b/lib/rbcodec/codecs/libopus/celt/arch.h index 03cda40f69..c0f9413d00 100644 --- a/lib/rbcodec/codecs/libopus/celt/arch.h +++ b/lib/rbcodec/codecs/libopus/celt/arch.h | |||
@@ -100,6 +100,7 @@ typedef opus_val32 celt_ener; | |||
100 | #define DB_SHIFT 10 | 100 | #define DB_SHIFT 10 |
101 | 101 | ||
102 | #define EPSILON 1 | 102 | #define EPSILON 1 |
103 | #define VERY_SMALL 0 | ||
103 | #define VERY_LARGE16 ((opus_val16)32767) | 104 | #define VERY_LARGE16 ((opus_val16)32767) |
104 | #define Q15_ONE ((opus_val16)32767) | 105 | #define Q15_ONE ((opus_val16)32767) |
105 | 106 | ||
@@ -112,16 +113,18 @@ typedef opus_val32 celt_ener; | |||
112 | 113 | ||
113 | #include "fixed_generic.h" | 114 | #include "fixed_generic.h" |
114 | 115 | ||
115 | #ifdef ARM5E_ASM | 116 | #ifdef ARMv5E_ASM |
116 | #include "fixed_arm5e.h" | 117 | #include "arm/fixed_armv5e.h" |
117 | #elif defined (ARM4_ASM) | 118 | #elif defined (ARMv4_ASM) |
118 | #include "fixed_arm4.h" | 119 | #include "arm/fixed_armv4.h" |
119 | #elif defined (BFIN_ASM) | 120 | #elif defined (BFIN_ASM) |
120 | #include "fixed_bfin.h" | 121 | #include "fixed_bfin.h" |
121 | #elif defined (TI_C5X_ASM) | 122 | #elif defined (TI_C5X_ASM) |
122 | #include "fixed_c5x.h" | 123 | #include "fixed_c5x.h" |
123 | #elif defined (TI_C6X_ASM) | 124 | #elif defined (TI_C6X_ASM) |
124 | #include "fixed_c6x.h" | 125 | #include "fixed_c6x.h" |
126 | #elif defined (CF_ASM) | ||
127 | #include "cf/fixed_cf.h" | ||
125 | #endif | 128 | #endif |
126 | 129 | ||
127 | #endif | 130 | #endif |
@@ -140,6 +143,7 @@ typedef float celt_ener; | |||
140 | #define NORM_SCALING 1.f | 143 | #define NORM_SCALING 1.f |
141 | 144 | ||
142 | #define EPSILON 1e-15f | 145 | #define EPSILON 1e-15f |
146 | #define VERY_SMALL 1e-30f | ||
143 | #define VERY_LARGE16 1e15f | 147 | #define VERY_LARGE16 1e15f |
144 | #define Q15_ONE ((opus_val16)1.f) | 148 | #define Q15_ONE ((opus_val16)1.f) |
145 | 149 | ||
@@ -161,6 +165,7 @@ typedef float celt_ener; | |||
161 | #define SHR(a,shift) (a) | 165 | #define SHR(a,shift) (a) |
162 | #define SHL(a,shift) (a) | 166 | #define SHL(a,shift) (a) |
163 | #define SATURATE(x,a) (x) | 167 | #define SATURATE(x,a) (x) |
168 | #define SATURATE16(x) (x) | ||
164 | 169 | ||
165 | #define ROUND16(a,shift) (a) | 170 | #define ROUND16(a,shift) (a) |
166 | #define HALF16(x) (.5f*(x)) | 171 | #define HALF16(x) (.5f*(x)) |
diff --git a/lib/rbcodec/codecs/libopus/celt/arm/fixed_armv4.h b/lib/rbcodec/codecs/libopus/celt/arm/fixed_armv4.h new file mode 100644 index 0000000000..bcacc343e8 --- /dev/null +++ b/lib/rbcodec/codecs/libopus/celt/arm/fixed_armv4.h | |||
@@ -0,0 +1,76 @@ | |||
1 | /* Copyright (C) 2013 Xiph.Org Foundation and contributors */ | ||
2 | /* | ||
3 | Redistribution and use in source and binary forms, with or without | ||
4 | modification, are permitted provided that the following conditions | ||
5 | are met: | ||
6 | |||
7 | - Redistributions of source code must retain the above copyright | ||
8 | notice, this list of conditions and the following disclaimer. | ||
9 | |||
10 | - Redistributions in binary form must reproduce the above copyright | ||
11 | notice, this list of conditions and the following disclaimer in the | ||
12 | documentation and/or other materials provided with the distribution. | ||
13 | |||
14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
15 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
16 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
17 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER | ||
18 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
19 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
20 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
21 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
22 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
23 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
25 | */ | ||
26 | |||
27 | #ifndef FIXED_ARMv4_H | ||
28 | #define FIXED_ARMv4_H | ||
29 | |||
30 | /** 16x32 multiplication, followed by a 16-bit shift right. Results fits in 32 bits */ | ||
31 | #undef MULT16_32_Q16 | ||
32 | static inline opus_val32 MULT16_32_Q16_armv4(opus_val16 a, opus_val32 b) | ||
33 | { | ||
34 | unsigned rd_lo; | ||
35 | int rd_hi; | ||
36 | __asm__( | ||
37 | "#MULT16_32_Q16\n\t" | ||
38 | "smull %0, %1, %2, %3\n\t" | ||
39 | : "=&r"(rd_lo), "=&r"(rd_hi) | ||
40 | : "%r"(b),"r"(a<<16) | ||
41 | ); | ||
42 | return rd_hi; | ||
43 | } | ||
44 | #define MULT16_32_Q16(a, b) (MULT16_32_Q16_armv4(a, b)) | ||
45 | |||
46 | |||
47 | /** 16x32 multiplication, followed by a 15-bit shift right. Results fits in 32 bits */ | ||
48 | #undef MULT16_32_Q15 | ||
49 | static inline opus_val32 MULT16_32_Q15_armv4(opus_val16 a, opus_val32 b) | ||
50 | { | ||
51 | unsigned rd_lo; | ||
52 | int rd_hi; | ||
53 | __asm__( | ||
54 | "#MULT16_32_Q15\n\t" | ||
55 | "smull %0, %1, %2, %3\n\t" | ||
56 | : "=&r"(rd_lo), "=&r"(rd_hi) | ||
57 | : "%r"(b), "r"(a<<16) | ||
58 | ); | ||
59 | /*We intentionally don't OR in the high bit of rd_lo for speed.*/ | ||
60 | return rd_hi<<1; | ||
61 | } | ||
62 | #define MULT16_32_Q15(a, b) (MULT16_32_Q15_armv4(a, b)) | ||
63 | |||
64 | |||
65 | /** 16x32 multiply, followed by a 15-bit shift right and 32-bit add. | ||
66 | b must fit in 31 bits. | ||
67 | Result fits in 32 bits. */ | ||
68 | #undef MAC16_32_Q15 | ||
69 | #define MAC16_32_Q15(c, a, b) ADD32(c, MULT16_32_Q15(a, b)) | ||
70 | |||
71 | |||
72 | /** 32x32 multiplication, followed by a 31-bit shift right. Results fits in 32 bits */ | ||
73 | #undef MULT32_32_Q31 | ||
74 | #define MULT32_32_Q31(a,b) (opus_val32)((((opus_int64)(a)) * ((opus_int64)(b)))>>31) | ||
75 | |||
76 | #endif | ||
diff --git a/lib/rbcodec/codecs/libopus/celt/arm/fixed_armv5e.h b/lib/rbcodec/codecs/libopus/celt/arm/fixed_armv5e.h new file mode 100644 index 0000000000..80632c4a94 --- /dev/null +++ b/lib/rbcodec/codecs/libopus/celt/arm/fixed_armv5e.h | |||
@@ -0,0 +1,116 @@ | |||
1 | /* Copyright (C) 2007-2009 Xiph.Org Foundation | ||
2 | Copyright (C) 2003-2008 Jean-Marc Valin | ||
3 | Copyright (C) 2007-2008 CSIRO | ||
4 | Copyright (C) 2013 Parrot */ | ||
5 | /* | ||
6 | Redistribution and use in source and binary forms, with or without | ||
7 | modification, are permitted provided that the following conditions | ||
8 | are met: | ||
9 | |||
10 | - Redistributions of source code must retain the above copyright | ||
11 | notice, this list of conditions and the following disclaimer. | ||
12 | |||
13 | - Redistributions in binary form must reproduce the above copyright | ||
14 | notice, this list of conditions and the following disclaimer in the | ||
15 | documentation and/or other materials provided with the distribution. | ||
16 | |||
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
18 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER | ||
21 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
22 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
23 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
24 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
25 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
26 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #ifndef FIXED_ARMv5E_H | ||
31 | #define FIXED_ARMv5E_H | ||
32 | |||
33 | #include "fixed_armv4.h" | ||
34 | |||
35 | /** 16x32 multiplication, followed by a 16-bit shift right. Results fits in 32 bits */ | ||
36 | #undef MULT16_32_Q16 | ||
37 | static inline opus_val32 MULT16_32_Q16_armv5e(opus_val16 a, opus_val32 b) | ||
38 | { | ||
39 | int res; | ||
40 | __asm__( | ||
41 | "#MULT16_32_Q16\n\t" | ||
42 | "smulwb %0, %1, %2\n\t" | ||
43 | : "=r"(res) | ||
44 | : "r"(b),"r"(a) | ||
45 | ); | ||
46 | return res; | ||
47 | } | ||
48 | #define MULT16_32_Q16(a, b) (MULT16_32_Q16_armv5e(a, b)) | ||
49 | |||
50 | |||
51 | /** 16x32 multiplication, followed by a 15-bit shift right. Results fits in 32 bits */ | ||
52 | #undef MULT16_32_Q15 | ||
53 | static inline opus_val32 MULT16_32_Q15_armv5e(opus_val16 a, opus_val32 b) | ||
54 | { | ||
55 | int res; | ||
56 | __asm__( | ||
57 | "#MULT16_32_Q15\n\t" | ||
58 | "smulwb %0, %1, %2\n\t" | ||
59 | : "=r"(res) | ||
60 | : "r"(b), "r"(a) | ||
61 | ); | ||
62 | return res<<1; | ||
63 | } | ||
64 | #define MULT16_32_Q15(a, b) (MULT16_32_Q15_armv5e(a, b)) | ||
65 | |||
66 | |||
67 | /** 16x32 multiply, followed by a 15-bit shift right and 32-bit add. | ||
68 | b must fit in 31 bits. | ||
69 | Result fits in 32 bits. */ | ||
70 | #undef MAC16_32_Q15 | ||
71 | static inline opus_val32 MAC16_32_Q15_armv5e(opus_val32 c, opus_val16 a, | ||
72 | opus_val32 b) | ||
73 | { | ||
74 | int res; | ||
75 | __asm__( | ||
76 | "#MAC16_32_Q15\n\t" | ||
77 | "smlawb %0, %1, %2, %3;\n" | ||
78 | : "=r"(res) | ||
79 | : "r"(b<<1), "r"(a), "r"(c) | ||
80 | ); | ||
81 | return res; | ||
82 | } | ||
83 | #define MAC16_32_Q15(c, a, b) (MAC16_32_Q15_armv5e(c, a, b)) | ||
84 | |||
85 | /** 16x16 multiply-add where the result fits in 32 bits */ | ||
86 | #undef MAC16_16 | ||
87 | static inline opus_val32 MAC16_16_armv5e(opus_val32 c, opus_val16 a, | ||
88 | opus_val16 b) | ||
89 | { | ||
90 | int res; | ||
91 | __asm__( | ||
92 | "#MAC16_16\n\t" | ||
93 | "smlabb %0, %1, %2, %3;\n" | ||
94 | : "=r"(res) | ||
95 | : "r"(a), "r"(b), "r"(c) | ||
96 | ); | ||
97 | return res; | ||
98 | } | ||
99 | #define MAC16_16(c, a, b) (MAC16_16_armv5e(c, a, b)) | ||
100 | |||
101 | /** 16x16 multiplication where the result fits in 32 bits */ | ||
102 | #undef MULT16_16 | ||
103 | static inline opus_val32 MULT16_16_armv5e(opus_val16 a, opus_val16 b) | ||
104 | { | ||
105 | int res; | ||
106 | __asm__( | ||
107 | "#MULT16_16\n\t" | ||
108 | "smulbb %0, %1, %2;\n" | ||
109 | : "=r"(res) | ||
110 | : "r"(a), "r"(b) | ||
111 | ); | ||
112 | return res; | ||
113 | } | ||
114 | #define MULT16_16(a, b) (MULT16_16_armv5e(a, b)) | ||
115 | |||
116 | #endif | ||
diff --git a/lib/rbcodec/codecs/libopus/celt/arm/kiss_fft_armv4.h b/lib/rbcodec/codecs/libopus/celt/arm/kiss_fft_armv4.h new file mode 100644 index 0000000000..e4faad6f2b --- /dev/null +++ b/lib/rbcodec/codecs/libopus/celt/arm/kiss_fft_armv4.h | |||
@@ -0,0 +1,121 @@ | |||
1 | /*Copyright (c) 2013, Xiph.Org Foundation and contributors. | ||
2 | |||
3 | All rights reserved. | ||
4 | |||
5 | Redistribution and use in source and binary forms, with or without | ||
6 | modification, are permitted provided that the following conditions are met: | ||
7 | |||
8 | * Redistributions of source code must retain the above copyright notice, | ||
9 | this list of conditions and the following disclaimer. | ||
10 | * Redistributions in binary form must reproduce the above copyright notice, | ||
11 | this list of conditions and the following disclaimer in the | ||
12 | documentation and/or other materials provided with the distribution. | ||
13 | |||
14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
18 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
19 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
20 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
21 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
22 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
23 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
24 | POSSIBILITY OF SUCH DAMAGE.*/ | ||
25 | |||
26 | #ifndef KISS_FFT_ARMv4_H | ||
27 | #define KISS_FFT_ARMv4_H | ||
28 | |||
29 | #if !defined(KISS_FFT_GUTS_H) | ||
30 | #error "This file should only be included from _kiss_fft_guts.h" | ||
31 | #endif | ||
32 | |||
33 | #ifdef FIXED_POINT | ||
34 | |||
35 | #undef C_MUL | ||
36 | #define C_MUL(m,a,b) \ | ||
37 | do{ \ | ||
38 | int br__; \ | ||
39 | int bi__; \ | ||
40 | int tt__; \ | ||
41 | __asm__ __volatile__( \ | ||
42 | "#C_MUL\n\t" \ | ||
43 | "ldrsh %[br], [%[bp], #0]\n\t" \ | ||
44 | "ldm %[ap], {r0,r1}\n\t" \ | ||
45 | "ldrsh %[bi], [%[bp], #2]\n\t" \ | ||
46 | "smull %[tt], %[mi], r1, %[br]\n\t" \ | ||
47 | "smlal %[tt], %[mi], r0, %[bi]\n\t" \ | ||
48 | "rsb %[bi], %[bi], #0\n\t" \ | ||
49 | "smull %[br], %[mr], r0, %[br]\n\t" \ | ||
50 | "mov %[tt], %[tt], lsr #15\n\t" \ | ||
51 | "smlal %[br], %[mr], r1, %[bi]\n\t" \ | ||
52 | "orr %[mi], %[tt], %[mi], lsl #17\n\t" \ | ||
53 | "mov %[br], %[br], lsr #15\n\t" \ | ||
54 | "orr %[mr], %[br], %[mr], lsl #17\n\t" \ | ||
55 | : [mr]"=r"((m).r), [mi]"=r"((m).i), \ | ||
56 | [br]"=&r"(br__), [bi]"=r"(bi__), [tt]"=r"(tt__) \ | ||
57 | : [ap]"r"(&(a)), [bp]"r"(&(b)) \ | ||
58 | : "r0", "r1" \ | ||
59 | ); \ | ||
60 | } \ | ||
61 | while(0) | ||
62 | |||
63 | #undef C_MUL4 | ||
64 | #define C_MUL4(m,a,b) \ | ||
65 | do{ \ | ||
66 | int br__; \ | ||
67 | int bi__; \ | ||
68 | int tt__; \ | ||
69 | __asm__ __volatile__( \ | ||
70 | "#C_MUL4\n\t" \ | ||
71 | "ldrsh %[br], [%[bp], #0]\n\t" \ | ||
72 | "ldm %[ap], {r0,r1}\n\t" \ | ||
73 | "ldrsh %[bi], [%[bp], #2]\n\t" \ | ||
74 | "smull %[tt], %[mi], r1, %[br]\n\t" \ | ||
75 | "smlal %[tt], %[mi], r0, %[bi]\n\t" \ | ||
76 | "rsb %[bi], %[bi], #0\n\t" \ | ||
77 | "smull %[br], %[mr], r0, %[br]\n\t" \ | ||
78 | "mov %[tt], %[tt], lsr #17\n\t" \ | ||
79 | "smlal %[br], %[mr], r1, %[bi]\n\t" \ | ||
80 | "orr %[mi], %[tt], %[mi], lsl #15\n\t" \ | ||
81 | "mov %[br], %[br], lsr #17\n\t" \ | ||
82 | "orr %[mr], %[br], %[mr], lsl #15\n\t" \ | ||
83 | : [mr]"=r"((m).r), [mi]"=r"((m).i), \ | ||
84 | [br]"=&r"(br__), [bi]"=r"(bi__), [tt]"=r"(tt__) \ | ||
85 | : [ap]"r"(&(a)), [bp]"r"(&(b)) \ | ||
86 | : "r0", "r1" \ | ||
87 | ); \ | ||
88 | } \ | ||
89 | while(0) | ||
90 | |||
91 | #undef C_MULC | ||
92 | #define C_MULC(m,a,b) \ | ||
93 | do{ \ | ||
94 | int br__; \ | ||
95 | int bi__; \ | ||
96 | int tt__; \ | ||
97 | __asm__ __volatile__( \ | ||
98 | "#C_MULC\n\t" \ | ||
99 | "ldrsh %[br], [%[bp], #0]\n\t" \ | ||
100 | "ldm %[ap], {r0,r1}\n\t" \ | ||
101 | "ldrsh %[bi], [%[bp], #2]\n\t" \ | ||
102 | "smull %[tt], %[mr], r0, %[br]\n\t" \ | ||
103 | "smlal %[tt], %[mr], r1, %[bi]\n\t" \ | ||
104 | "rsb %[bi], %[bi], #0\n\t" \ | ||
105 | "smull %[br], %[mi], r1, %[br]\n\t" \ | ||
106 | "mov %[tt], %[tt], lsr #15\n\t" \ | ||
107 | "smlal %[br], %[mi], r0, %[bi]\n\t" \ | ||
108 | "orr %[mr], %[tt], %[mr], lsl #17\n\t" \ | ||
109 | "mov %[br], %[br], lsr #15\n\t" \ | ||
110 | "orr %[mi], %[br], %[mi], lsl #17\n\t" \ | ||
111 | : [mr]"=r"((m).r), [mi]"=r"((m).i), \ | ||
112 | [br]"=&r"(br__), [bi]"=r"(bi__), [tt]"=r"(tt__) \ | ||
113 | : [ap]"r"(&(a)), [bp]"r"(&(b)) \ | ||
114 | : "r0", "r1" \ | ||
115 | ); \ | ||
116 | } \ | ||
117 | while(0) | ||
118 | |||
119 | #endif /* FIXED_POINT */ | ||
120 | |||
121 | #endif /* KISS_FFT_ARMv4_H */ | ||
diff --git a/lib/rbcodec/codecs/libopus/celt/arm/kiss_fft_armv5e.h b/lib/rbcodec/codecs/libopus/celt/arm/kiss_fft_armv5e.h new file mode 100644 index 0000000000..9eca183d77 --- /dev/null +++ b/lib/rbcodec/codecs/libopus/celt/arm/kiss_fft_armv5e.h | |||
@@ -0,0 +1,118 @@ | |||
1 | /*Copyright (c) 2013, Xiph.Org Foundation and contributors. | ||
2 | |||
3 | All rights reserved. | ||
4 | |||
5 | Redistribution and use in source and binary forms, with or without | ||
6 | modification, are permitted provided that the following conditions are met: | ||
7 | |||
8 | * Redistributions of source code must retain the above copyright notice, | ||
9 | this list of conditions and the following disclaimer. | ||
10 | * Redistributions in binary form must reproduce the above copyright notice, | ||
11 | this list of conditions and the following disclaimer in the | ||
12 | documentation and/or other materials provided with the distribution. | ||
13 | |||
14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
18 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
19 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
20 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
21 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
22 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
23 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
24 | POSSIBILITY OF SUCH DAMAGE.*/ | ||
25 | |||
26 | #ifndef KISS_FFT_ARMv5E_H | ||
27 | #define KISS_FFT_ARMv5E_H | ||
28 | |||
29 | #if !defined(KISS_FFT_GUTS_H) | ||
30 | #error "This file should only be included from _kiss_fft_guts.h" | ||
31 | #endif | ||
32 | |||
33 | #ifdef FIXED_POINT | ||
34 | |||
35 | #if defined(__thumb__)||defined(__thumb2__) | ||
36 | #define LDRD_CONS "Q" | ||
37 | #else | ||
38 | #define LDRD_CONS "Uq" | ||
39 | #endif | ||
40 | |||
41 | #undef C_MUL | ||
42 | #define C_MUL(m,a,b) \ | ||
43 | do{ \ | ||
44 | int mr1__; \ | ||
45 | int mr2__; \ | ||
46 | int mi__; \ | ||
47 | long long aval__; \ | ||
48 | int bval__; \ | ||
49 | __asm__( \ | ||
50 | "#C_MUL\n\t" \ | ||
51 | "ldrd %[aval], %H[aval], %[ap]\n\t" \ | ||
52 | "ldr %[bval], %[bp]\n\t" \ | ||
53 | "smulwb %[mi], %H[aval], %[bval]\n\t" \ | ||
54 | "smulwb %[mr1], %[aval], %[bval]\n\t" \ | ||
55 | "smulwt %[mr2], %H[aval], %[bval]\n\t" \ | ||
56 | "smlawt %[mi], %[aval], %[bval], %[mi]\n\t" \ | ||
57 | : [mr1]"=r"(mr1__), [mr2]"=r"(mr2__), [mi]"=r"(mi__), \ | ||
58 | [aval]"=&r"(aval__), [bval]"=r"(bval__) \ | ||
59 | : [ap]LDRD_CONS(a), [bp]"m"(b) \ | ||
60 | ); \ | ||
61 | (m).r = SHL32(SUB32(mr1__, mr2__), 1); \ | ||
62 | (m).i = SHL32(mi__, 1); \ | ||
63 | } \ | ||
64 | while(0) | ||
65 | |||
66 | #undef C_MUL4 | ||
67 | #define C_MUL4(m,a,b) \ | ||
68 | do{ \ | ||
69 | int mr1__; \ | ||
70 | int mr2__; \ | ||
71 | int mi__; \ | ||
72 | long long aval__; \ | ||
73 | int bval__; \ | ||
74 | __asm__( \ | ||
75 | "#C_MUL4\n\t" \ | ||
76 | "ldrd %[aval], %H[aval], %[ap]\n\t" \ | ||
77 | "ldr %[bval], %[bp]\n\t" \ | ||
78 | "smulwb %[mi], %H[aval], %[bval]\n\t" \ | ||
79 | "smulwb %[mr1], %[aval], %[bval]\n\t" \ | ||
80 | "smulwt %[mr2], %H[aval], %[bval]\n\t" \ | ||
81 | "smlawt %[mi], %[aval], %[bval], %[mi]\n\t" \ | ||
82 | : [mr1]"=r"(mr1__), [mr2]"=r"(mr2__), [mi]"=r"(mi__), \ | ||
83 | [aval]"=&r"(aval__), [bval]"=r"(bval__) \ | ||
84 | : [ap]LDRD_CONS(a), [bp]"m"(b) \ | ||
85 | ); \ | ||
86 | (m).r = SHR32(SUB32(mr1__, mr2__), 1); \ | ||
87 | (m).i = SHR32(mi__, 1); \ | ||
88 | } \ | ||
89 | while(0) | ||
90 | |||
91 | #undef C_MULC | ||
92 | #define C_MULC(m,a,b) \ | ||
93 | do{ \ | ||
94 | int mr__; \ | ||
95 | int mi1__; \ | ||
96 | int mi2__; \ | ||
97 | long long aval__; \ | ||
98 | int bval__; \ | ||
99 | __asm__( \ | ||
100 | "#C_MULC\n\t" \ | ||
101 | "ldrd %[aval], %H[aval], %[ap]\n\t" \ | ||
102 | "ldr %[bval], %[bp]\n\t" \ | ||
103 | "smulwb %[mr], %[aval], %[bval]\n\t" \ | ||
104 | "smulwb %[mi1], %H[aval], %[bval]\n\t" \ | ||
105 | "smulwt %[mi2], %[aval], %[bval]\n\t" \ | ||
106 | "smlawt %[mr], %H[aval], %[bval], %[mr]\n\t" \ | ||
107 | : [mr]"=r"(mr__), [mi1]"=r"(mi1__), [mi2]"=r"(mi2__), \ | ||
108 | [aval]"=&r"(aval__), [bval]"=r"(bval__) \ | ||
109 | : [ap]LDRD_CONS(a), [bp]"m"(b) \ | ||
110 | ); \ | ||
111 | (m).r = SHL32(mr__, 1); \ | ||
112 | (m).i = SHL32(SUB32(mi1__, mi2__), 1); \ | ||
113 | } \ | ||
114 | while(0) | ||
115 | |||
116 | #endif /* FIXED_POINT */ | ||
117 | |||
118 | #endif /* KISS_FFT_GUTS_H */ | ||
diff --git a/lib/rbcodec/codecs/libopus/celt/bands.c b/lib/rbcodec/codecs/libopus/celt/bands.c index c7cb0d5500..5c715aff53 100644 --- a/lib/rbcodec/codecs/libopus/celt/bands.c +++ b/lib/rbcodec/codecs/libopus/celt/bands.c | |||
@@ -28,7 +28,7 @@ | |||
28 | */ | 28 | */ |
29 | 29 | ||
30 | #ifdef HAVE_CONFIG_H | 30 | #ifdef HAVE_CONFIG_H |
31 | #include "opus_config.h" | 31 | #include "config.h" |
32 | #endif | 32 | #endif |
33 | 33 | ||
34 | #include <math.h> | 34 | #include <math.h> |
@@ -40,6 +40,23 @@ | |||
40 | #include "os_support.h" | 40 | #include "os_support.h" |
41 | #include "mathops.h" | 41 | #include "mathops.h" |
42 | #include "rate.h" | 42 | #include "rate.h" |
43 | #include "quant_bands.h" | ||
44 | #include "pitch.h" | ||
45 | |||
46 | int hysteresis_decision(opus_val16 val, const opus_val16 *thresholds, const opus_val16 *hysteresis, int N, int prev) | ||
47 | { | ||
48 | int i; | ||
49 | for (i=0;i<N;i++) | ||
50 | { | ||
51 | if (val < thresholds[i]) | ||
52 | break; | ||
53 | } | ||
54 | if (i>prev && val < thresholds[prev]+hysteresis[prev]) | ||
55 | i=prev; | ||
56 | if (i<prev && val > thresholds[prev-1]-hysteresis[prev-1]) | ||
57 | i=prev; | ||
58 | return i; | ||
59 | } | ||
43 | 60 | ||
44 | opus_uint32 celt_lcg_rand(opus_uint32 seed) | 61 | opus_uint32 celt_lcg_rand(opus_uint32 seed) |
45 | { | 62 | { |
@@ -174,7 +191,8 @@ void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, cel | |||
174 | #endif | 191 | #endif |
175 | 192 | ||
176 | /* De-normalise the energy to produce the synthesis from the unit-energy bands */ | 193 | /* De-normalise the energy to produce the synthesis from the unit-energy bands */ |
177 | void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X, celt_sig * OPUS_RESTRICT freq, const celt_ener *bandE, int end, int C, int M) | 194 | void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X, |
195 | celt_sig * OPUS_RESTRICT freq, const opus_val16 *bandLogE, int start, int end, int C, int M) | ||
178 | { | 196 | { |
179 | int i, c, N; | 197 | int i, c, N; |
180 | const opus_int16 *eBands = m->eBands; | 198 | const opus_int16 *eBands = m->eBands; |
@@ -184,18 +202,39 @@ void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X, cel | |||
184 | celt_sig * OPUS_RESTRICT f; | 202 | celt_sig * OPUS_RESTRICT f; |
185 | const celt_norm * OPUS_RESTRICT x; | 203 | const celt_norm * OPUS_RESTRICT x; |
186 | f = freq+c*N; | 204 | f = freq+c*N; |
187 | x = X+c*N; | 205 | x = X+c*N+M*eBands[start]; |
188 | for (i=0;i<end;i++) | 206 | for (i=0;i<M*eBands[start];i++) |
207 | *f++ = 0; | ||
208 | for (i=start;i<end;i++) | ||
189 | { | 209 | { |
190 | int j, band_end; | 210 | int j, band_end; |
191 | opus_val32 g = SHR32(bandE[i+c*m->nbEBands],1); | 211 | opus_val16 g; |
212 | opus_val16 lg; | ||
213 | #ifdef FIXED_POINT | ||
214 | int shift; | ||
215 | #endif | ||
192 | j=M*eBands[i]; | 216 | j=M*eBands[i]; |
193 | band_end = M*eBands[i+1]; | 217 | band_end = M*eBands[i+1]; |
218 | lg = ADD16(bandLogE[i+c*m->nbEBands], SHL16((opus_val16)eMeans[i],6)); | ||
219 | #ifdef FIXED_POINT | ||
220 | /* Handle the integer part of the log energy */ | ||
221 | shift = 16-(lg>>DB_SHIFT); | ||
222 | if (shift>31) | ||
223 | { | ||
224 | shift=0; | ||
225 | g=0; | ||
226 | } else { | ||
227 | /* Handle the fractional part. */ | ||
228 | g = celt_exp2_frac(lg&((1<<DB_SHIFT)-1)); | ||
229 | } | ||
230 | #else | ||
231 | g = celt_exp2(lg); | ||
232 | #endif | ||
194 | do { | 233 | do { |
195 | *f++ = SHL32(MULT16_32_Q15(*x, g),2); | 234 | *f++ = SHR32(MULT16_16(*x++, g), shift); |
196 | x++; | ||
197 | } while (++j<band_end); | 235 | } while (++j<band_end); |
198 | } | 236 | } |
237 | celt_assert(start <= end); | ||
199 | for (i=M*eBands[end];i<N;i++) | 238 | for (i=M*eBands[end];i<N;i++) |
200 | *f++ = 0; | 239 | *f++ = 0; |
201 | } while (++c<C); | 240 | } while (++c<C); |
@@ -347,11 +386,7 @@ static void stereo_merge(celt_norm *X, celt_norm *Y, opus_val16 mid, int N) | |||
347 | opus_val32 t, lgain, rgain; | 386 | opus_val32 t, lgain, rgain; |
348 | 387 | ||
349 | /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */ | 388 | /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */ |
350 | for (j=0;j<N;j++) | 389 | dual_inner_prod(Y, X, Y, N, &xp, &side); |
351 | { | ||
352 | xp = MAC16_16(xp, X[j], Y[j]); | ||
353 | side = MAC16_16(side, Y[j], Y[j]); | ||
354 | } | ||
355 | /* Compensating for the mid normalization */ | 390 | /* Compensating for the mid normalization */ |
356 | xp = MULT16_32_Q15(mid, xp); | 391 | xp = MULT16_32_Q15(mid, xp); |
357 | /* mid and side are in Q15, not Q14 like X and Y */ | 392 | /* mid and side are in Q15, not Q14 like X and Y */ |
@@ -487,50 +522,6 @@ int spreading_decision(const CELTMode *m, celt_norm *X, int *average, | |||
487 | } | 522 | } |
488 | #endif | 523 | #endif |
489 | 524 | ||
490 | #ifdef MEASURE_NORM_MSE | ||
491 | |||
492 | float MSE[30] = {0}; | ||
493 | int nbMSEBands = 0; | ||
494 | int MSECount[30] = {0}; | ||
495 | |||
496 | void dump_norm_mse(void) | ||
497 | { | ||
498 | int i; | ||
499 | for (i=0;i<nbMSEBands;i++) | ||
500 | { | ||
501 | printf ("%g ", MSE[i]/MSECount[i]); | ||
502 | } | ||
503 | printf ("\n"); | ||
504 | } | ||
505 | |||
506 | void measure_norm_mse(const CELTMode *m, float *X, float *X0, float *bandE, float *bandE0, int M, int N, int C) | ||
507 | { | ||
508 | static int init = 0; | ||
509 | int i; | ||
510 | if (!init) | ||
511 | { | ||
512 | atexit(dump_norm_mse); | ||
513 | init = 1; | ||
514 | } | ||
515 | for (i=0;i<m->nbEBands;i++) | ||
516 | { | ||
517 | int j; | ||
518 | int c; | ||
519 | float g; | ||
520 | if (bandE0[i]<10 || (C==2 && bandE0[i+m->nbEBands]<1)) | ||
521 | continue; | ||
522 | c=0; do { | ||
523 | g = bandE[i+c*m->nbEBands]/(1e-15+bandE0[i+c*m->nbEBands]); | ||
524 | for (j=M*m->eBands[i];j<M*m->eBands[i+1];j++) | ||
525 | MSE[i] += (g*X[j+c*N]-X0[j+c*N])*(g*X[j+c*N]-X0[j+c*N]); | ||
526 | } while (++c<C); | ||
527 | MSECount[i]+=C; | ||
528 | } | ||
529 | nbMSEBands = m->nbEBands; | ||
530 | } | ||
531 | |||
532 | #endif | ||
533 | |||
534 | /* Indexing table for converting from natural Hadamard to ordery Hadamard | 525 | /* Indexing table for converting from natural Hadamard to ordery Hadamard |
535 | This is essentially a bit-reversed Gray, on top of which we've added | 526 | This is essentially a bit-reversed Gray, on top of which we've added |
536 | an inversion of the order because we want the DC at the end rather than | 527 | an inversion of the order because we want the DC at the end rather than |
@@ -633,289 +624,304 @@ static int compute_qn(int N, int b, int offset, int pulse_cap, int stereo) | |||
633 | return qn; | 624 | return qn; |
634 | } | 625 | } |
635 | 626 | ||
636 | /* This function is responsible for encoding and decoding a band for both | 627 | struct band_ctx { |
637 | the mono and stereo case. Even in the mono case, it can split the band | 628 | int encode; |
638 | in two and transmit the energy difference with the two half-bands. It | 629 | const CELTMode *m; |
639 | can be called recursively so bands can end up being split in 8 parts. */ | 630 | int i; |
640 | static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, celt_norm *Y, | 631 | int intensity; |
641 | int N, int b, int spread, int B, int intensity, int tf_change, celt_norm *lowband, ec_ctx *ec, | 632 | int spread; |
642 | opus_int32 *remaining_bits, int LM, celt_norm *lowband_out, const celt_ener *bandE, int level, | 633 | int tf_change; |
643 | opus_uint32 *seed, opus_val16 gain, celt_norm *lowband_scratch, int fill) | 634 | ec_ctx *ec; |
635 | opus_int32 remaining_bits; | ||
636 | const celt_ener *bandE; | ||
637 | opus_uint32 seed; | ||
638 | }; | ||
639 | |||
640 | struct split_ctx { | ||
641 | int inv; | ||
642 | int imid; | ||
643 | int iside; | ||
644 | int delta; | ||
645 | int itheta; | ||
646 | int qalloc; | ||
647 | }; | ||
648 | |||
649 | static void compute_theta(struct band_ctx *ctx, struct split_ctx *sctx, | ||
650 | celt_norm *X, celt_norm *Y, int N, int *b, int B, int B0, | ||
651 | int LM, | ||
652 | int stereo, int *fill) | ||
644 | { | 653 | { |
645 | const unsigned char *cache; | 654 | int qn; |
646 | int q; | 655 | int itheta=0; |
647 | int curr_bits; | 656 | int delta; |
648 | int stereo, split; | 657 | int imid, iside; |
649 | int imid=0, iside=0; | 658 | int qalloc; |
650 | int N0=N; | 659 | int pulse_cap; |
651 | int N_B=N; | 660 | int offset; |
652 | int N_B0; | 661 | opus_int32 tell; |
653 | int B0=B; | 662 | int inv=0; |
654 | int time_divide=0; | 663 | int encode; |
655 | int recombine=0; | 664 | const CELTMode *m; |
656 | int inv = 0; | 665 | int i; |
657 | opus_val16 mid=0, side=0; | 666 | int intensity; |
658 | int longBlocks; | 667 | ec_ctx *ec; |
659 | unsigned cm=0; | 668 | const celt_ener *bandE; |
660 | #ifdef RESYNTH | 669 | |
661 | int resynth = 1; | 670 | encode = ctx->encode; |
662 | #else | 671 | m = ctx->m; |
663 | int resynth = !encode; | 672 | i = ctx->i; |
664 | #endif | 673 | intensity = ctx->intensity; |
674 | ec = ctx->ec; | ||
675 | bandE = ctx->bandE; | ||
676 | |||
677 | /* Decide on the resolution to give to the split parameter theta */ | ||
678 | pulse_cap = m->logN[i]+LM*(1<<BITRES); | ||
679 | offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET); | ||
680 | qn = compute_qn(N, *b, offset, pulse_cap, stereo); | ||
681 | if (stereo && i>=intensity) | ||
682 | qn = 1; | ||
683 | if (encode) | ||
684 | { | ||
685 | /* theta is the atan() of the ratio between the (normalized) | ||
686 | side and mid. With just that parameter, we can re-scale both | ||
687 | mid and side because we know that 1) they have unit norm and | ||
688 | 2) they are orthogonal. */ | ||
689 | itheta = stereo_itheta(X, Y, stereo, N); | ||
690 | } | ||
691 | tell = ec_tell_frac(ec); | ||
692 | if (qn!=1) | ||
693 | { | ||
694 | if (encode) | ||
695 | itheta = (itheta*qn+8192)>>14; | ||
665 | 696 | ||
666 | longBlocks = B0==1; | 697 | /* Entropy coding of the angle. We use a uniform pdf for the |
698 | time split, a step for stereo, and a triangular one for the rest. */ | ||
699 | if (stereo && N>2) | ||
700 | { | ||
701 | int p0 = 3; | ||
702 | int x = itheta; | ||
703 | int x0 = qn/2; | ||
704 | int ft = p0*(x0+1) + x0; | ||
705 | /* Use a probability of p0 up to itheta=8192 and then use 1 after */ | ||
706 | if (encode) | ||
707 | { | ||
708 | ec_encode(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft); | ||
709 | } else { | ||
710 | int fs; | ||
711 | fs=ec_decode(ec,ft); | ||
712 | if (fs<(x0+1)*p0) | ||
713 | x=fs/p0; | ||
714 | else | ||
715 | x=x0+1+(fs-(x0+1)*p0); | ||
716 | ec_dec_update(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft); | ||
717 | itheta = x; | ||
718 | } | ||
719 | } else if (B0>1 || stereo) { | ||
720 | /* Uniform pdf */ | ||
721 | if (encode) | ||
722 | ec_enc_uint(ec, itheta, qn+1); | ||
723 | else | ||
724 | itheta = ec_dec_uint(ec, qn+1); | ||
725 | } else { | ||
726 | int fs=1, ft; | ||
727 | ft = ((qn>>1)+1)*((qn>>1)+1); | ||
728 | if (encode) | ||
729 | { | ||
730 | int fl; | ||
667 | 731 | ||
668 | N_B /= B; | 732 | fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta; |
669 | N_B0 = N_B; | 733 | fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 : |
734 | ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1); | ||
670 | 735 | ||
671 | split = stereo = Y != NULL; | 736 | ec_encode(ec, fl, fl+fs, ft); |
737 | } else { | ||
738 | /* Triangular pdf */ | ||
739 | int fl=0; | ||
740 | int fm; | ||
741 | fm = ec_decode(ec, ft); | ||
672 | 742 | ||
673 | /* Special case for one sample */ | 743 | if (fm < ((qn>>1)*((qn>>1) + 1)>>1)) |
674 | if (N==1) | ||
675 | { | ||
676 | int c; | ||
677 | celt_norm *x = X; | ||
678 | c=0; do { | ||
679 | int sign=0; | ||
680 | if (*remaining_bits>=1<<BITRES) | ||
681 | { | ||
682 | if (encode) | ||
683 | { | 744 | { |
684 | sign = x[0]<0; | 745 | itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1; |
685 | ec_enc_bits(ec, sign, 1); | 746 | fs = itheta + 1; |
686 | } else { | 747 | fl = itheta*(itheta + 1)>>1; |
687 | sign = ec_dec_bits(ec, 1); | 748 | } |
749 | else | ||
750 | { | ||
751 | itheta = (2*(qn + 1) | ||
752 | - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1; | ||
753 | fs = qn + 1 - itheta; | ||
754 | fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1); | ||
688 | } | 755 | } |
689 | *remaining_bits -= 1<<BITRES; | ||
690 | b-=1<<BITRES; | ||
691 | } | ||
692 | if (resynth) | ||
693 | x[0] = sign ? -NORM_SCALING : NORM_SCALING; | ||
694 | x = Y; | ||
695 | } while (++c<1+stereo); | ||
696 | if (lowband_out) | ||
697 | lowband_out[0] = SHR16(X[0],4); | ||
698 | return 1; | ||
699 | } | ||
700 | |||
701 | if (!stereo && level == 0) | ||
702 | { | ||
703 | int k; | ||
704 | if (tf_change>0) | ||
705 | recombine = tf_change; | ||
706 | /* Band recombining to increase frequency resolution */ | ||
707 | 756 | ||
708 | if (lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1)) | 757 | ec_dec_update(ec, fl, fl+fs, ft); |
709 | { | 758 | } |
710 | int j; | ||
711 | for (j=0;j<N;j++) | ||
712 | lowband_scratch[j] = lowband[j]; | ||
713 | lowband = lowband_scratch; | ||
714 | } | 759 | } |
715 | 760 | itheta = (opus_int32)itheta*16384/qn; | |
716 | for (k=0;k<recombine;k++) | 761 | if (encode && stereo) |
717 | { | 762 | { |
718 | static const unsigned char bit_interleave_table[16]={ | 763 | if (itheta==0) |
719 | 0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3 | 764 | intensity_stereo(m, X, Y, bandE, i, N); |
720 | }; | 765 | else |
721 | if (encode) | 766 | stereo_split(X, Y, N); |
722 | haar1(X, N>>k, 1<<k); | ||
723 | if (lowband) | ||
724 | haar1(lowband, N>>k, 1<<k); | ||
725 | fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2; | ||
726 | } | 767 | } |
727 | B>>=recombine; | 768 | /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate. |
728 | N_B<<=recombine; | 769 | Let's do that at higher complexity */ |
729 | 770 | } else if (stereo) { | |
730 | /* Increasing the time resolution */ | 771 | if (encode) |
731 | while ((N_B&1) == 0 && tf_change<0) | ||
732 | { | 772 | { |
733 | if (encode) | 773 | inv = itheta > 8192; |
734 | haar1(X, N_B, B); | 774 | if (inv) |
735 | if (lowband) | 775 | { |
736 | haar1(lowband, N_B, B); | 776 | int j; |
737 | fill |= fill<<B; | 777 | for (j=0;j<N;j++) |
738 | B <<= 1; | 778 | Y[j] = -Y[j]; |
739 | N_B >>= 1; | 779 | } |
740 | time_divide++; | 780 | intensity_stereo(m, X, Y, bandE, i, N); |
741 | tf_change++; | ||
742 | } | 781 | } |
743 | B0=B; | 782 | if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES) |
744 | N_B0 = N_B; | ||
745 | |||
746 | /* Reorganize the samples in time order instead of frequency order */ | ||
747 | if (B0>1) | ||
748 | { | 783 | { |
749 | if (encode) | 784 | if (encode) |
750 | deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks); | 785 | ec_enc_bit_logp(ec, inv, 2); |
751 | if (lowband) | 786 | else |
752 | deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks); | 787 | inv = ec_dec_bit_logp(ec, 2); |
753 | } | 788 | } else |
789 | inv = 0; | ||
790 | itheta = 0; | ||
754 | } | 791 | } |
792 | qalloc = ec_tell_frac(ec) - tell; | ||
793 | *b -= qalloc; | ||
755 | 794 | ||
756 | /* If we need 1.5 more bit than we can produce, split the band in two. */ | 795 | if (itheta == 0) |
757 | cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i]; | ||
758 | if (!stereo && LM != -1 && b > cache[cache[0]]+12 && N>2) | ||
759 | { | 796 | { |
760 | N >>= 1; | 797 | imid = 32767; |
761 | Y = X+N; | 798 | iside = 0; |
762 | split = 1; | 799 | *fill &= (1<<B)-1; |
763 | LM -= 1; | 800 | delta = -16384; |
764 | if (B==1) | 801 | } else if (itheta == 16384) |
765 | fill = (fill&1)|(fill<<1); | 802 | { |
766 | B = (B+1)>>1; | 803 | imid = 0; |
804 | iside = 32767; | ||
805 | *fill &= ((1<<B)-1)<<B; | ||
806 | delta = 16384; | ||
807 | } else { | ||
808 | imid = bitexact_cos((opus_int16)itheta); | ||
809 | iside = bitexact_cos((opus_int16)(16384-itheta)); | ||
810 | /* This is the mid vs side allocation that minimizes squared error | ||
811 | in that band. */ | ||
812 | delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid)); | ||
767 | } | 813 | } |
768 | 814 | ||
769 | if (split) | 815 | sctx->inv = inv; |
770 | { | 816 | sctx->imid = imid; |
771 | int qn; | 817 | sctx->iside = iside; |
772 | int itheta=0; | 818 | sctx->delta = delta; |
773 | int mbits, sbits, delta; | 819 | sctx->itheta = itheta; |
774 | int qalloc; | 820 | sctx->qalloc = qalloc; |
775 | int pulse_cap; | 821 | } |
776 | int offset; | 822 | static unsigned quant_band_n1(struct band_ctx *ctx, celt_norm *X, celt_norm *Y, int b, |
777 | int orig_fill; | 823 | celt_norm *lowband_out) |
778 | opus_int32 tell; | 824 | { |
825 | #ifdef RESYNTH | ||
826 | int resynth = 1; | ||
827 | #else | ||
828 | int resynth = !ctx->encode; | ||
829 | #endif | ||
830 | int c; | ||
831 | int stereo; | ||
832 | celt_norm *x = X; | ||
833 | int encode; | ||
834 | ec_ctx *ec; | ||
779 | 835 | ||
780 | /* Decide on the resolution to give to the split parameter theta */ | 836 | encode = ctx->encode; |
781 | pulse_cap = m->logN[i]+LM*(1<<BITRES); | 837 | ec = ctx->ec; |
782 | offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET); | 838 | |
783 | qn = compute_qn(N, b, offset, pulse_cap, stereo); | 839 | stereo = Y != NULL; |
784 | if (stereo && i>=intensity) | 840 | c=0; do { |
785 | qn = 1; | 841 | int sign=0; |
786 | if (encode) | 842 | if (ctx->remaining_bits>=1<<BITRES) |
787 | { | ||
788 | /* theta is the atan() of the ratio between the (normalized) | ||
789 | side and mid. With just that parameter, we can re-scale both | ||
790 | mid and side because we know that 1) they have unit norm and | ||
791 | 2) they are orthogonal. */ | ||
792 | itheta = stereo_itheta(X, Y, stereo, N); | ||
793 | } | ||
794 | tell = ec_tell_frac(ec); | ||
795 | if (qn!=1) | ||
796 | { | 843 | { |
797 | if (encode) | 844 | if (encode) |
798 | itheta = (itheta*qn+8192)>>14; | ||
799 | |||
800 | /* Entropy coding of the angle. We use a uniform pdf for the | ||
801 | time split, a step for stereo, and a triangular one for the rest. */ | ||
802 | if (stereo && N>2) | ||
803 | { | 845 | { |
804 | int p0 = 3; | 846 | sign = x[0]<0; |
805 | int x = itheta; | 847 | ec_enc_bits(ec, sign, 1); |
806 | int x0 = qn/2; | ||
807 | int ft = p0*(x0+1) + x0; | ||
808 | /* Use a probability of p0 up to itheta=8192 and then use 1 after */ | ||
809 | if (encode) | ||
810 | { | ||
811 | ec_encode(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft); | ||
812 | } else { | ||
813 | int fs; | ||
814 | fs=ec_decode(ec,ft); | ||
815 | if (fs<(x0+1)*p0) | ||
816 | x=fs/p0; | ||
817 | else | ||
818 | x=x0+1+(fs-(x0+1)*p0); | ||
819 | ec_dec_update(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft); | ||
820 | itheta = x; | ||
821 | } | ||
822 | } else if (B0>1 || stereo) { | ||
823 | /* Uniform pdf */ | ||
824 | if (encode) | ||
825 | ec_enc_uint(ec, itheta, qn+1); | ||
826 | else | ||
827 | itheta = ec_dec_uint(ec, qn+1); | ||
828 | } else { | 848 | } else { |
829 | int fs=1, ft; | 849 | sign = ec_dec_bits(ec, 1); |
830 | ft = ((qn>>1)+1)*((qn>>1)+1); | 850 | } |
831 | if (encode) | 851 | ctx->remaining_bits -= 1<<BITRES; |
832 | { | 852 | b-=1<<BITRES; |
833 | int fl; | 853 | } |
854 | if (resynth) | ||
855 | x[0] = sign ? -NORM_SCALING : NORM_SCALING; | ||
856 | x = Y; | ||
857 | } while (++c<1+stereo); | ||
858 | if (lowband_out) | ||
859 | lowband_out[0] = SHR16(X[0],4); | ||
860 | return 1; | ||
861 | } | ||
834 | 862 | ||
835 | fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta; | 863 | /* This function is responsible for encoding and decoding a mono partition. |
836 | fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 : | 864 | It can split the band in two and transmit the energy difference with |
837 | ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1); | 865 | the two half-bands. It can be called recursively so bands can end up being |
866 | split in 8 parts. */ | ||
867 | static unsigned quant_partition(struct band_ctx *ctx, celt_norm *X, | ||
868 | int N, int b, int B, celt_norm *lowband, | ||
869 | int LM, | ||
870 | opus_val16 gain, int fill) | ||
871 | { | ||
872 | const unsigned char *cache; | ||
873 | int q; | ||
874 | int curr_bits; | ||
875 | int imid=0, iside=0; | ||
876 | int N_B=N; | ||
877 | int B0=B; | ||
878 | opus_val16 mid=0, side=0; | ||
879 | unsigned cm=0; | ||
880 | #ifdef RESYNTH | ||
881 | int resynth = 1; | ||
882 | #else | ||
883 | int resynth = !ctx->encode; | ||
884 | #endif | ||
885 | celt_norm *Y=NULL; | ||
886 | int encode; | ||
887 | const CELTMode *m; | ||
888 | int i; | ||
889 | int spread; | ||
890 | ec_ctx *ec; | ||
838 | 891 | ||
839 | ec_encode(ec, fl, fl+fs, ft); | 892 | encode = ctx->encode; |
840 | } else { | 893 | m = ctx->m; |
841 | /* Triangular pdf */ | 894 | i = ctx->i; |
842 | int fl=0; | 895 | spread = ctx->spread; |
843 | int fm; | 896 | ec = ctx->ec; |
844 | fm = ec_decode(ec, ft); | ||
845 | 897 | ||
846 | if (fm < ((qn>>1)*((qn>>1) + 1)>>1)) | 898 | N_B /= B; |
847 | { | ||
848 | itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1; | ||
849 | fs = itheta + 1; | ||
850 | fl = itheta*(itheta + 1)>>1; | ||
851 | } | ||
852 | else | ||
853 | { | ||
854 | itheta = (2*(qn + 1) | ||
855 | - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1; | ||
856 | fs = qn + 1 - itheta; | ||
857 | fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1); | ||
858 | } | ||
859 | 899 | ||
860 | ec_dec_update(ec, fl, fl+fs, ft); | 900 | /* If we need 1.5 more bit than we can produce, split the band in two. */ |
861 | } | 901 | cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i]; |
862 | } | 902 | if (LM != -1 && b > cache[cache[0]]+12 && N>2) |
863 | itheta = (opus_int32)itheta*16384/qn; | 903 | { |
864 | if (encode && stereo) | 904 | int mbits, sbits, delta; |
865 | { | 905 | int itheta; |
866 | if (itheta==0) | 906 | int qalloc; |
867 | intensity_stereo(m, X, Y, bandE, i, N); | 907 | struct split_ctx sctx; |
868 | else | 908 | celt_norm *next_lowband2=NULL; |
869 | stereo_split(X, Y, N); | 909 | opus_int32 rebalance; |
870 | } | ||
871 | /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate. | ||
872 | Let's do that at higher complexity */ | ||
873 | } else if (stereo) { | ||
874 | if (encode) | ||
875 | { | ||
876 | inv = itheta > 8192; | ||
877 | if (inv) | ||
878 | { | ||
879 | int j; | ||
880 | for (j=0;j<N;j++) | ||
881 | Y[j] = -Y[j]; | ||
882 | } | ||
883 | intensity_stereo(m, X, Y, bandE, i, N); | ||
884 | } | ||
885 | if (b>2<<BITRES && *remaining_bits > 2<<BITRES) | ||
886 | { | ||
887 | if (encode) | ||
888 | ec_enc_bit_logp(ec, inv, 2); | ||
889 | else | ||
890 | inv = ec_dec_bit_logp(ec, 2); | ||
891 | } else | ||
892 | inv = 0; | ||
893 | itheta = 0; | ||
894 | } | ||
895 | qalloc = ec_tell_frac(ec) - tell; | ||
896 | b -= qalloc; | ||
897 | 910 | ||
898 | orig_fill = fill; | 911 | N >>= 1; |
899 | if (itheta == 0) | 912 | Y = X+N; |
900 | { | 913 | LM -= 1; |
901 | imid = 32767; | 914 | if (B==1) |
902 | iside = 0; | 915 | fill = (fill&1)|(fill<<1); |
903 | fill &= (1<<B)-1; | 916 | B = (B+1)>>1; |
904 | delta = -16384; | ||
905 | } else if (itheta == 16384) | ||
906 | { | ||
907 | imid = 0; | ||
908 | iside = 32767; | ||
909 | fill &= ((1<<B)-1)<<B; | ||
910 | delta = 16384; | ||
911 | } else { | ||
912 | imid = bitexact_cos(itheta); | ||
913 | iside = bitexact_cos(16384-itheta); | ||
914 | /* This is the mid vs side allocation that minimizes squared error | ||
915 | in that band. */ | ||
916 | delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid)); | ||
917 | } | ||
918 | 917 | ||
918 | compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, | ||
919 | LM, 0, &fill); | ||
920 | imid = sctx.imid; | ||
921 | iside = sctx.iside; | ||
922 | delta = sctx.delta; | ||
923 | itheta = sctx.itheta; | ||
924 | qalloc = sctx.qalloc; | ||
919 | #ifdef FIXED_POINT | 925 | #ifdef FIXED_POINT |
920 | mid = imid; | 926 | mid = imid; |
921 | side = iside; | 927 | side = iside; |
@@ -924,136 +930,59 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c | |||
924 | side = (1.f/32768)*iside; | 930 | side = (1.f/32768)*iside; |
925 | #endif | 931 | #endif |
926 | 932 | ||
927 | /* This is a special case for N=2 that only works for stereo and takes | 933 | /* Give more bits to low-energy MDCTs than they would otherwise deserve */ |
928 | advantage of the fact that mid and side are orthogonal to encode | 934 | if (B0>1 && (itheta&0x3fff)) |
929 | the side with just one bit. */ | ||
930 | if (N==2 && stereo) | ||
931 | { | 935 | { |
932 | int c; | 936 | if (itheta > 8192) |
933 | int sign=0; | 937 | /* Rough approximation for pre-echo masking */ |
934 | celt_norm *x2, *y2; | 938 | delta -= delta>>(4-LM); |
935 | mbits = b; | ||
936 | sbits = 0; | ||
937 | /* Only need one bit for the side */ | ||
938 | if (itheta != 0 && itheta != 16384) | ||
939 | sbits = 1<<BITRES; | ||
940 | mbits -= sbits; | ||
941 | c = itheta > 8192; | ||
942 | *remaining_bits -= qalloc+sbits; | ||
943 | |||
944 | x2 = c ? Y : X; | ||
945 | y2 = c ? X : Y; | ||
946 | if (sbits) | ||
947 | { | ||
948 | if (encode) | ||
949 | { | ||
950 | /* Here we only need to encode a sign for the side */ | ||
951 | sign = x2[0]*y2[1] - x2[1]*y2[0] < 0; | ||
952 | ec_enc_bits(ec, sign, 1); | ||
953 | } else { | ||
954 | sign = ec_dec_bits(ec, 1); | ||
955 | } | ||
956 | } | ||
957 | sign = 1-2*sign; | ||
958 | /* We use orig_fill here because we want to fold the side, but if | ||
959 | itheta==16384, we'll have cleared the low bits of fill. */ | ||
960 | cm = quant_band(encode, m, i, x2, NULL, N, mbits, spread, B, intensity, tf_change, lowband, ec, remaining_bits, LM, lowband_out, NULL, level, seed, gain, lowband_scratch, orig_fill); | ||
961 | /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse), | ||
962 | and there's no need to worry about mixing with the other channel. */ | ||
963 | y2[0] = -sign*x2[1]; | ||
964 | y2[1] = sign*x2[0]; | ||
965 | if (resynth) | ||
966 | { | ||
967 | celt_norm tmp; | ||
968 | X[0] = MULT16_16_Q15(mid, X[0]); | ||
969 | X[1] = MULT16_16_Q15(mid, X[1]); | ||
970 | Y[0] = MULT16_16_Q15(side, Y[0]); | ||
971 | Y[1] = MULT16_16_Q15(side, Y[1]); | ||
972 | tmp = X[0]; | ||
973 | X[0] = SUB16(tmp,Y[0]); | ||
974 | Y[0] = ADD16(tmp,Y[0]); | ||
975 | tmp = X[1]; | ||
976 | X[1] = SUB16(tmp,Y[1]); | ||
977 | Y[1] = ADD16(tmp,Y[1]); | ||
978 | } | ||
979 | } else { | ||
980 | /* "Normal" split code */ | ||
981 | celt_norm *next_lowband2=NULL; | ||
982 | celt_norm *next_lowband_out1=NULL; | ||
983 | int next_level=0; | ||
984 | opus_int32 rebalance; | ||
985 | |||
986 | /* Give more bits to low-energy MDCTs than they would otherwise deserve */ | ||
987 | if (B0>1 && !stereo && (itheta&0x3fff)) | ||
988 | { | ||
989 | if (itheta > 8192) | ||
990 | /* Rough approximation for pre-echo masking */ | ||
991 | delta -= delta>>(4-LM); | ||
992 | else | ||
993 | /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */ | ||
994 | delta = IMIN(0, delta + (N<<BITRES>>(5-LM))); | ||
995 | } | ||
996 | mbits = IMAX(0, IMIN(b, (b-delta)/2)); | ||
997 | sbits = b-mbits; | ||
998 | *remaining_bits -= qalloc; | ||
999 | |||
1000 | if (lowband && !stereo) | ||
1001 | next_lowband2 = lowband+N; /* >32-bit split case */ | ||
1002 | |||
1003 | /* Only stereo needs to pass on lowband_out. Otherwise, it's | ||
1004 | handled at the end */ | ||
1005 | if (stereo) | ||
1006 | next_lowband_out1 = lowband_out; | ||
1007 | else | 939 | else |
1008 | next_level = level+1; | 940 | /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */ |
1009 | 941 | delta = IMIN(0, delta + (N<<BITRES>>(5-LM))); | |
1010 | rebalance = *remaining_bits; | ||
1011 | if (mbits >= sbits) | ||
1012 | { | ||
1013 | /* In stereo mode, we do not apply a scaling to the mid because we need the normalized | ||
1014 | mid for folding later */ | ||
1015 | cm = quant_band(encode, m, i, X, NULL, N, mbits, spread, B, intensity, tf_change, | ||
1016 | lowband, ec, remaining_bits, LM, next_lowband_out1, | ||
1017 | NULL, next_level, seed, stereo ? Q15ONE : MULT16_16_P15(gain,mid), lowband_scratch, fill); | ||
1018 | rebalance = mbits - (rebalance-*remaining_bits); | ||
1019 | if (rebalance > 3<<BITRES && itheta!=0) | ||
1020 | sbits += rebalance - (3<<BITRES); | ||
1021 | |||
1022 | /* For a stereo split, the high bits of fill are always zero, so no | ||
1023 | folding will be done to the side. */ | ||
1024 | cm |= quant_band(encode, m, i, Y, NULL, N, sbits, spread, B, intensity, tf_change, | ||
1025 | next_lowband2, ec, remaining_bits, LM, NULL, | ||
1026 | NULL, next_level, seed, MULT16_16_P15(gain,side), NULL, fill>>B)<<((B0>>1)&(stereo-1)); | ||
1027 | } else { | ||
1028 | /* For a stereo split, the high bits of fill are always zero, so no | ||
1029 | folding will be done to the side. */ | ||
1030 | cm = quant_band(encode, m, i, Y, NULL, N, sbits, spread, B, intensity, tf_change, | ||
1031 | next_lowband2, ec, remaining_bits, LM, NULL, | ||
1032 | NULL, next_level, seed, MULT16_16_P15(gain,side), NULL, fill>>B)<<((B0>>1)&(stereo-1)); | ||
1033 | rebalance = sbits - (rebalance-*remaining_bits); | ||
1034 | if (rebalance > 3<<BITRES && itheta!=16384) | ||
1035 | mbits += rebalance - (3<<BITRES); | ||
1036 | /* In stereo mode, we do not apply a scaling to the mid because we need the normalized | ||
1037 | mid for folding later */ | ||
1038 | cm |= quant_band(encode, m, i, X, NULL, N, mbits, spread, B, intensity, tf_change, | ||
1039 | lowband, ec, remaining_bits, LM, next_lowband_out1, | ||
1040 | NULL, next_level, seed, stereo ? Q15ONE : MULT16_16_P15(gain,mid), lowband_scratch, fill); | ||
1041 | } | ||
1042 | } | 942 | } |
943 | mbits = IMAX(0, IMIN(b, (b-delta)/2)); | ||
944 | sbits = b-mbits; | ||
945 | ctx->remaining_bits -= qalloc; | ||
946 | |||
947 | if (lowband) | ||
948 | next_lowband2 = lowband+N; /* >32-bit split case */ | ||
1043 | 949 | ||
950 | rebalance = ctx->remaining_bits; | ||
951 | if (mbits >= sbits) | ||
952 | { | ||
953 | cm = quant_partition(ctx, X, N, mbits, B, | ||
954 | lowband, LM, | ||
955 | MULT16_16_P15(gain,mid), fill); | ||
956 | rebalance = mbits - (rebalance-ctx->remaining_bits); | ||
957 | if (rebalance > 3<<BITRES && itheta!=0) | ||
958 | sbits += rebalance - (3<<BITRES); | ||
959 | cm |= quant_partition(ctx, Y, N, sbits, B, | ||
960 | next_lowband2, LM, | ||
961 | MULT16_16_P15(gain,side), fill>>B)<<(B0>>1); | ||
962 | } else { | ||
963 | cm = quant_partition(ctx, Y, N, sbits, B, | ||
964 | next_lowband2, LM, | ||
965 | MULT16_16_P15(gain,side), fill>>B)<<(B0>>1); | ||
966 | rebalance = sbits - (rebalance-ctx->remaining_bits); | ||
967 | if (rebalance > 3<<BITRES && itheta!=16384) | ||
968 | mbits += rebalance - (3<<BITRES); | ||
969 | cm |= quant_partition(ctx, X, N, mbits, B, | ||
970 | lowband, LM, | ||
971 | MULT16_16_P15(gain,mid), fill); | ||
972 | } | ||
1044 | } else { | 973 | } else { |
1045 | /* This is the basic no-split case */ | 974 | /* This is the basic no-split case */ |
1046 | q = bits2pulses(m, i, LM, b); | 975 | q = bits2pulses(m, i, LM, b); |
1047 | curr_bits = pulses2bits(m, i, LM, q); | 976 | curr_bits = pulses2bits(m, i, LM, q); |
1048 | *remaining_bits -= curr_bits; | 977 | ctx->remaining_bits -= curr_bits; |
1049 | 978 | ||
1050 | /* Ensures we can never bust the budget */ | 979 | /* Ensures we can never bust the budget */ |
1051 | while (*remaining_bits < 0 && q > 0) | 980 | while (ctx->remaining_bits < 0 && q > 0) |
1052 | { | 981 | { |
1053 | *remaining_bits += curr_bits; | 982 | ctx->remaining_bits += curr_bits; |
1054 | q--; | 983 | q--; |
1055 | curr_bits = pulses2bits(m, i, LM, q); | 984 | curr_bits = pulses2bits(m, i, LM, q); |
1056 | *remaining_bits -= curr_bits; | 985 | ctx->remaining_bits -= curr_bits; |
1057 | } | 986 | } |
1058 | 987 | ||
1059 | if (q!=0) | 988 | if (q!=0) |
@@ -1077,7 +1006,7 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c | |||
1077 | if (resynth) | 1006 | if (resynth) |
1078 | { | 1007 | { |
1079 | unsigned cm_mask; | 1008 | unsigned cm_mask; |
1080 | /*B can be as large as 16, so this shift might overflow an int on a | 1009 | /* B can be as large as 16, so this shift might overflow an int on a |
1081 | 16-bit platform; use a long to get defined behavior.*/ | 1010 | 16-bit platform; use a long to get defined behavior.*/ |
1082 | cm_mask = (unsigned)(1UL<<B)-1; | 1011 | cm_mask = (unsigned)(1UL<<B)-1; |
1083 | fill &= cm_mask; | 1012 | fill &= cm_mask; |
@@ -1091,8 +1020,8 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c | |||
1091 | /* Noise */ | 1020 | /* Noise */ |
1092 | for (j=0;j<N;j++) | 1021 | for (j=0;j<N;j++) |
1093 | { | 1022 | { |
1094 | *seed = celt_lcg_rand(*seed); | 1023 | ctx->seed = celt_lcg_rand(ctx->seed); |
1095 | X[j] = (celt_norm)((opus_int32)*seed>>20); | 1024 | X[j] = (celt_norm)((opus_int32)ctx->seed>>20); |
1096 | } | 1025 | } |
1097 | cm = cm_mask; | 1026 | cm = cm_mask; |
1098 | } else { | 1027 | } else { |
@@ -1100,10 +1029,10 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c | |||
1100 | for (j=0;j<N;j++) | 1029 | for (j=0;j<N;j++) |
1101 | { | 1030 | { |
1102 | opus_val16 tmp; | 1031 | opus_val16 tmp; |
1103 | *seed = celt_lcg_rand(*seed); | 1032 | ctx->seed = celt_lcg_rand(ctx->seed); |
1104 | /* About 48 dB below the "normal" folding level */ | 1033 | /* About 48 dB below the "normal" folding level */ |
1105 | tmp = QCONST16(1.0f/256, 10); | 1034 | tmp = QCONST16(1.0f/256, 10); |
1106 | tmp = (*seed)&0x8000 ? tmp : -tmp; | 1035 | tmp = (ctx->seed)&0x8000 ? tmp : -tmp; |
1107 | X[j] = lowband[j]+tmp; | 1036 | X[j] = lowband[j]+tmp; |
1108 | } | 1037 | } |
1109 | cm = fill; | 1038 | cm = fill; |
@@ -1114,64 +1043,307 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c | |||
1114 | } | 1043 | } |
1115 | } | 1044 | } |
1116 | 1045 | ||
1046 | return cm; | ||
1047 | } | ||
1048 | |||
1049 | |||
1050 | /* This function is responsible for encoding and decoding a band for the mono case. */ | ||
1051 | static unsigned quant_band(struct band_ctx *ctx, celt_norm *X, | ||
1052 | int N, int b, int B, celt_norm *lowband, | ||
1053 | int LM, celt_norm *lowband_out, | ||
1054 | opus_val16 gain, celt_norm *lowband_scratch, int fill) | ||
1055 | { | ||
1056 | int N0=N; | ||
1057 | int N_B=N; | ||
1058 | int N_B0; | ||
1059 | int B0=B; | ||
1060 | int time_divide=0; | ||
1061 | int recombine=0; | ||
1062 | int longBlocks; | ||
1063 | unsigned cm=0; | ||
1064 | #ifdef RESYNTH | ||
1065 | int resynth = 1; | ||
1066 | #else | ||
1067 | int resynth = !ctx->encode; | ||
1068 | #endif | ||
1069 | int k; | ||
1070 | int encode; | ||
1071 | int tf_change; | ||
1072 | |||
1073 | encode = ctx->encode; | ||
1074 | tf_change = ctx->tf_change; | ||
1075 | |||
1076 | longBlocks = B0==1; | ||
1077 | |||
1078 | N_B /= B; | ||
1079 | N_B0 = N_B; | ||
1080 | |||
1081 | /* Special case for one sample */ | ||
1082 | if (N==1) | ||
1083 | { | ||
1084 | return quant_band_n1(ctx, X, NULL, b, lowband_out); | ||
1085 | } | ||
1086 | |||
1087 | if (tf_change>0) | ||
1088 | recombine = tf_change; | ||
1089 | /* Band recombining to increase frequency resolution */ | ||
1090 | |||
1091 | if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1)) | ||
1092 | { | ||
1093 | int j; | ||
1094 | for (j=0;j<N;j++) | ||
1095 | lowband_scratch[j] = lowband[j]; | ||
1096 | lowband = lowband_scratch; | ||
1097 | } | ||
1098 | |||
1099 | for (k=0;k<recombine;k++) | ||
1100 | { | ||
1101 | static const unsigned char bit_interleave_table[16]={ | ||
1102 | 0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3 | ||
1103 | }; | ||
1104 | if (encode) | ||
1105 | haar1(X, N>>k, 1<<k); | ||
1106 | if (lowband) | ||
1107 | haar1(lowband, N>>k, 1<<k); | ||
1108 | fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2; | ||
1109 | } | ||
1110 | B>>=recombine; | ||
1111 | N_B<<=recombine; | ||
1112 | |||
1113 | /* Increasing the time resolution */ | ||
1114 | while ((N_B&1) == 0 && tf_change<0) | ||
1115 | { | ||
1116 | if (encode) | ||
1117 | haar1(X, N_B, B); | ||
1118 | if (lowband) | ||
1119 | haar1(lowband, N_B, B); | ||
1120 | fill |= fill<<B; | ||
1121 | B <<= 1; | ||
1122 | N_B >>= 1; | ||
1123 | time_divide++; | ||
1124 | tf_change++; | ||
1125 | } | ||
1126 | B0=B; | ||
1127 | N_B0 = N_B; | ||
1128 | |||
1129 | /* Reorganize the samples in time order instead of frequency order */ | ||
1130 | if (B0>1) | ||
1131 | { | ||
1132 | if (encode) | ||
1133 | deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks); | ||
1134 | if (lowband) | ||
1135 | deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks); | ||
1136 | } | ||
1137 | |||
1138 | cm = quant_partition(ctx, X, N, b, B, lowband, | ||
1139 | LM, gain, fill); | ||
1140 | |||
1117 | /* This code is used by the decoder and by the resynthesis-enabled encoder */ | 1141 | /* This code is used by the decoder and by the resynthesis-enabled encoder */ |
1118 | if (resynth) | 1142 | if (resynth) |
1119 | { | 1143 | { |
1120 | if (stereo) | 1144 | /* Undo the sample reorganization going from time order to frequency order */ |
1145 | if (B0>1) | ||
1146 | interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks); | ||
1147 | |||
1148 | /* Undo time-freq changes that we did earlier */ | ||
1149 | N_B = N_B0; | ||
1150 | B = B0; | ||
1151 | for (k=0;k<time_divide;k++) | ||
1121 | { | 1152 | { |
1122 | if (N!=2) | 1153 | B >>= 1; |
1123 | stereo_merge(X, Y, mid, N); | 1154 | N_B <<= 1; |
1124 | if (inv) | 1155 | cm |= cm>>B; |
1125 | { | 1156 | haar1(X, N_B, B); |
1126 | int j; | 1157 | } |
1127 | for (j=0;j<N;j++) | 1158 | |
1128 | Y[j] = -Y[j]; | 1159 | for (k=0;k<recombine;k++) |
1129 | } | ||
1130 | } else if (level == 0) | ||
1131 | { | 1160 | { |
1132 | int k; | 1161 | static const unsigned char bit_deinterleave_table[16]={ |
1162 | 0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F, | ||
1163 | 0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF | ||
1164 | }; | ||
1165 | cm = bit_deinterleave_table[cm]; | ||
1166 | haar1(X, N0>>k, 1<<k); | ||
1167 | } | ||
1168 | B<<=recombine; | ||
1133 | 1169 | ||
1134 | /* Undo the sample reorganization going from time order to frequency order */ | 1170 | /* Scale output for later folding */ |
1135 | if (B0>1) | 1171 | if (lowband_out) |
1136 | interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks); | 1172 | { |
1173 | int j; | ||
1174 | opus_val16 n; | ||
1175 | n = celt_sqrt(SHL32(EXTEND32(N0),22)); | ||
1176 | for (j=0;j<N0;j++) | ||
1177 | lowband_out[j] = MULT16_16_Q15(n,X[j]); | ||
1178 | } | ||
1179 | cm &= (1<<B)-1; | ||
1180 | } | ||
1181 | return cm; | ||
1182 | } | ||
1137 | 1183 | ||
1138 | /* Undo time-freq changes that we did earlier */ | ||
1139 | N_B = N_B0; | ||
1140 | B = B0; | ||
1141 | for (k=0;k<time_divide;k++) | ||
1142 | { | ||
1143 | B >>= 1; | ||
1144 | N_B <<= 1; | ||
1145 | cm |= cm>>B; | ||
1146 | haar1(X, N_B, B); | ||
1147 | } | ||
1148 | 1184 | ||
1149 | for (k=0;k<recombine;k++) | 1185 | /* This function is responsible for encoding and decoding a band for the stereo case. */ |
1150 | { | 1186 | static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm *Y, |
1151 | static const unsigned char bit_deinterleave_table[16]={ | 1187 | int N, int b, int B, celt_norm *lowband, |
1152 | 0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F, | 1188 | int LM, celt_norm *lowband_out, |
1153 | 0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF | 1189 | celt_norm *lowband_scratch, int fill) |
1154 | }; | 1190 | { |
1155 | cm = bit_deinterleave_table[cm]; | 1191 | int imid=0, iside=0; |
1156 | haar1(X, N0>>k, 1<<k); | 1192 | int inv = 0; |
1157 | } | 1193 | opus_val16 mid=0, side=0; |
1158 | B<<=recombine; | 1194 | unsigned cm=0; |
1195 | #ifdef RESYNTH | ||
1196 | int resynth = 1; | ||
1197 | #else | ||
1198 | int resynth = !ctx->encode; | ||
1199 | #endif | ||
1200 | int mbits, sbits, delta; | ||
1201 | int itheta; | ||
1202 | int qalloc; | ||
1203 | struct split_ctx sctx; | ||
1204 | int orig_fill; | ||
1205 | int encode; | ||
1206 | ec_ctx *ec; | ||
1207 | |||
1208 | encode = ctx->encode; | ||
1209 | ec = ctx->ec; | ||
1210 | |||
1211 | /* Special case for one sample */ | ||
1212 | if (N==1) | ||
1213 | { | ||
1214 | return quant_band_n1(ctx, X, Y, b, lowband_out); | ||
1215 | } | ||
1216 | |||
1217 | orig_fill = fill; | ||
1218 | |||
1219 | compute_theta(ctx, &sctx, X, Y, N, &b, B, B, | ||
1220 | LM, 1, &fill); | ||
1221 | inv = sctx.inv; | ||
1222 | imid = sctx.imid; | ||
1223 | iside = sctx.iside; | ||
1224 | delta = sctx.delta; | ||
1225 | itheta = sctx.itheta; | ||
1226 | qalloc = sctx.qalloc; | ||
1227 | #ifdef FIXED_POINT | ||
1228 | mid = imid; | ||
1229 | side = iside; | ||
1230 | #else | ||
1231 | mid = (1.f/32768)*imid; | ||
1232 | side = (1.f/32768)*iside; | ||
1233 | #endif | ||
1159 | 1234 | ||
1160 | /* Scale output for later folding */ | 1235 | /* This is a special case for N=2 that only works for stereo and takes |
1161 | if (lowband_out) | 1236 | advantage of the fact that mid and side are orthogonal to encode |
1237 | the side with just one bit. */ | ||
1238 | if (N==2) | ||
1239 | { | ||
1240 | int c; | ||
1241 | int sign=0; | ||
1242 | celt_norm *x2, *y2; | ||
1243 | mbits = b; | ||
1244 | sbits = 0; | ||
1245 | /* Only need one bit for the side. */ | ||
1246 | if (itheta != 0 && itheta != 16384) | ||
1247 | sbits = 1<<BITRES; | ||
1248 | mbits -= sbits; | ||
1249 | c = itheta > 8192; | ||
1250 | ctx->remaining_bits -= qalloc+sbits; | ||
1251 | |||
1252 | x2 = c ? Y : X; | ||
1253 | y2 = c ? X : Y; | ||
1254 | if (sbits) | ||
1255 | { | ||
1256 | if (encode) | ||
1162 | { | 1257 | { |
1163 | int j; | 1258 | /* Here we only need to encode a sign for the side. */ |
1164 | opus_val16 n; | 1259 | sign = x2[0]*y2[1] - x2[1]*y2[0] < 0; |
1165 | n = celt_sqrt(SHL32(EXTEND32(N0),22)); | 1260 | ec_enc_bits(ec, sign, 1); |
1166 | for (j=0;j<N0;j++) | 1261 | } else { |
1167 | lowband_out[j] = MULT16_16_Q15(n,X[j]); | 1262 | sign = ec_dec_bits(ec, 1); |
1168 | } | 1263 | } |
1169 | cm &= (1<<B)-1; | 1264 | } |
1265 | sign = 1-2*sign; | ||
1266 | /* We use orig_fill here because we want to fold the side, but if | ||
1267 | itheta==16384, we'll have cleared the low bits of fill. */ | ||
1268 | cm = quant_band(ctx, x2, N, mbits, B, lowband, | ||
1269 | LM, lowband_out, Q15ONE, lowband_scratch, orig_fill); | ||
1270 | /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse), | ||
1271 | and there's no need to worry about mixing with the other channel. */ | ||
1272 | y2[0] = -sign*x2[1]; | ||
1273 | y2[1] = sign*x2[0]; | ||
1274 | if (resynth) | ||
1275 | { | ||
1276 | celt_norm tmp; | ||
1277 | X[0] = MULT16_16_Q15(mid, X[0]); | ||
1278 | X[1] = MULT16_16_Q15(mid, X[1]); | ||
1279 | Y[0] = MULT16_16_Q15(side, Y[0]); | ||
1280 | Y[1] = MULT16_16_Q15(side, Y[1]); | ||
1281 | tmp = X[0]; | ||
1282 | X[0] = SUB16(tmp,Y[0]); | ||
1283 | Y[0] = ADD16(tmp,Y[0]); | ||
1284 | tmp = X[1]; | ||
1285 | X[1] = SUB16(tmp,Y[1]); | ||
1286 | Y[1] = ADD16(tmp,Y[1]); | ||
1287 | } | ||
1288 | } else { | ||
1289 | /* "Normal" split code */ | ||
1290 | opus_int32 rebalance; | ||
1291 | |||
1292 | mbits = IMAX(0, IMIN(b, (b-delta)/2)); | ||
1293 | sbits = b-mbits; | ||
1294 | ctx->remaining_bits -= qalloc; | ||
1295 | |||
1296 | rebalance = ctx->remaining_bits; | ||
1297 | if (mbits >= sbits) | ||
1298 | { | ||
1299 | /* In stereo mode, we do not apply a scaling to the mid because we need the normalized | ||
1300 | mid for folding later. */ | ||
1301 | cm = quant_band(ctx, X, N, mbits, B, | ||
1302 | lowband, LM, lowband_out, | ||
1303 | Q15ONE, lowband_scratch, fill); | ||
1304 | rebalance = mbits - (rebalance-ctx->remaining_bits); | ||
1305 | if (rebalance > 3<<BITRES && itheta!=0) | ||
1306 | sbits += rebalance - (3<<BITRES); | ||
1307 | |||
1308 | /* For a stereo split, the high bits of fill are always zero, so no | ||
1309 | folding will be done to the side. */ | ||
1310 | cm |= quant_band(ctx, Y, N, sbits, B, | ||
1311 | NULL, LM, NULL, | ||
1312 | side, NULL, fill>>B); | ||
1313 | } else { | ||
1314 | /* For a stereo split, the high bits of fill are always zero, so no | ||
1315 | folding will be done to the side. */ | ||
1316 | cm = quant_band(ctx, Y, N, sbits, B, | ||
1317 | NULL, LM, NULL, | ||
1318 | side, NULL, fill>>B); | ||
1319 | rebalance = sbits - (rebalance-ctx->remaining_bits); | ||
1320 | if (rebalance > 3<<BITRES && itheta!=16384) | ||
1321 | mbits += rebalance - (3<<BITRES); | ||
1322 | /* In stereo mode, we do not apply a scaling to the mid because we need the normalized | ||
1323 | mid for folding later. */ | ||
1324 | cm |= quant_band(ctx, X, N, mbits, B, | ||
1325 | lowband, LM, lowband_out, | ||
1326 | Q15ONE, lowband_scratch, fill); | ||
1327 | } | ||
1328 | } | ||
1329 | |||
1330 | |||
1331 | /* This code is used by the decoder and by the resynthesis-enabled encoder */ | ||
1332 | if (resynth) | ||
1333 | { | ||
1334 | if (N!=2) | ||
1335 | stereo_merge(X, Y, mid, N); | ||
1336 | if (inv) | ||
1337 | { | ||
1338 | int j; | ||
1339 | for (j=0;j<N;j++) | ||
1340 | Y[j] = -Y[j]; | ||
1170 | } | 1341 | } |
1171 | } | 1342 | } |
1172 | return cm; | 1343 | return cm; |
1173 | } | 1344 | } |
1174 | 1345 | ||
1346 | |||
1175 | void quant_all_bands(int encode, const CELTMode *m, int start, int end, | 1347 | void quant_all_bands(int encode, const CELTMode *m, int start, int end, |
1176 | celt_norm *X_, celt_norm *Y_, unsigned char *collapse_masks, const celt_ener *bandE, int *pulses, | 1348 | celt_norm *X_, celt_norm *Y_, unsigned char *collapse_masks, const celt_ener *bandE, int *pulses, |
1177 | int shortBlocks, int spread, int dual_stereo, int intensity, int *tf_res, | 1349 | int shortBlocks, int spread, int dual_stereo, int intensity, int *tf_res, |
@@ -1182,27 +1354,41 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1182 | const opus_int16 * OPUS_RESTRICT eBands = m->eBands; | 1354 | const opus_int16 * OPUS_RESTRICT eBands = m->eBands; |
1183 | celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2; | 1355 | celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2; |
1184 | VARDECL(celt_norm, _norm); | 1356 | VARDECL(celt_norm, _norm); |
1185 | VARDECL(celt_norm, lowband_scratch); | 1357 | celt_norm *lowband_scratch; |
1186 | int B; | 1358 | int B; |
1187 | int M; | 1359 | int M; |
1188 | int lowband_offset; | 1360 | int lowband_offset; |
1189 | int update_lowband = 1; | 1361 | int update_lowband = 1; |
1190 | int C = Y_ != NULL ? 2 : 1; | 1362 | int C = Y_ != NULL ? 2 : 1; |
1363 | int norm_offset; | ||
1191 | #ifdef RESYNTH | 1364 | #ifdef RESYNTH |
1192 | int resynth = 1; | 1365 | int resynth = 1; |
1193 | #else | 1366 | #else |
1194 | int resynth = !encode; | 1367 | int resynth = !encode; |
1195 | #endif | 1368 | #endif |
1369 | struct band_ctx ctx; | ||
1196 | SAVE_STACK; | 1370 | SAVE_STACK; |
1197 | 1371 | ||
1198 | M = 1<<LM; | 1372 | M = 1<<LM; |
1199 | B = shortBlocks ? M : 1; | 1373 | B = shortBlocks ? M : 1; |
1200 | ALLOC(_norm, C*M*eBands[m->nbEBands], celt_norm); | 1374 | norm_offset = M*eBands[start]; |
1201 | ALLOC(lowband_scratch, M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]), celt_norm); | 1375 | /* No need to allocate norm for the last band because we don't need an |
1376 | output in that band. */ | ||
1377 | ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm); | ||
1202 | norm = _norm; | 1378 | norm = _norm; |
1203 | norm2 = norm + M*eBands[m->nbEBands]; | 1379 | norm2 = norm + M*eBands[m->nbEBands-1]-norm_offset; |
1380 | /* We can use the last band as scratch space because we don't need that | ||
1381 | scratch space for the last band. */ | ||
1382 | lowband_scratch = X_+M*eBands[m->nbEBands-1]; | ||
1204 | 1383 | ||
1205 | lowband_offset = 0; | 1384 | lowband_offset = 0; |
1385 | ctx.bandE = bandE; | ||
1386 | ctx.ec = ec; | ||
1387 | ctx.encode = encode; | ||
1388 | ctx.intensity = intensity; | ||
1389 | ctx.m = m; | ||
1390 | ctx.seed = *seed; | ||
1391 | ctx.spread = spread; | ||
1206 | for (i=start;i<end;i++) | 1392 | for (i=start;i<end;i++) |
1207 | { | 1393 | { |
1208 | opus_int32 tell; | 1394 | opus_int32 tell; |
@@ -1214,6 +1400,10 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1214 | int tf_change=0; | 1400 | int tf_change=0; |
1215 | unsigned x_cm; | 1401 | unsigned x_cm; |
1216 | unsigned y_cm; | 1402 | unsigned y_cm; |
1403 | int last; | ||
1404 | |||
1405 | ctx.i = i; | ||
1406 | last = (i==end-1); | ||
1217 | 1407 | ||
1218 | X = X_+M*eBands[i]; | 1408 | X = X_+M*eBands[i]; |
1219 | if (Y_!=NULL) | 1409 | if (Y_!=NULL) |
@@ -1227,6 +1417,7 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1227 | if (i != start) | 1417 | if (i != start) |
1228 | balance -= tell; | 1418 | balance -= tell; |
1229 | remaining_bits = total_bits-tell-1; | 1419 | remaining_bits = total_bits-tell-1; |
1420 | ctx.remaining_bits = remaining_bits; | ||
1230 | if (i <= codedBands-1) | 1421 | if (i <= codedBands-1) |
1231 | { | 1422 | { |
1232 | curr_balance = balance / IMIN(3, codedBands-i); | 1423 | curr_balance = balance / IMIN(3, codedBands-i); |
@@ -1239,26 +1430,30 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1239 | lowband_offset = i; | 1430 | lowband_offset = i; |
1240 | 1431 | ||
1241 | tf_change = tf_res[i]; | 1432 | tf_change = tf_res[i]; |
1433 | ctx.tf_change = tf_change; | ||
1242 | if (i>=m->effEBands) | 1434 | if (i>=m->effEBands) |
1243 | { | 1435 | { |
1244 | X=norm; | 1436 | X=norm; |
1245 | if (Y_!=NULL) | 1437 | if (Y_!=NULL) |
1246 | Y = norm; | 1438 | Y = norm; |
1439 | lowband_scratch = NULL; | ||
1247 | } | 1440 | } |
1441 | if (i==end-1) | ||
1442 | lowband_scratch = NULL; | ||
1248 | 1443 | ||
1249 | /* Get a conservative estimate of the collapse_mask's for the bands we're | 1444 | /* Get a conservative estimate of the collapse_mask's for the bands we're |
1250 | going to be folding from. */ | 1445 | going to be folding from. */ |
1251 | if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0)) | 1446 | if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0)) |
1252 | { | 1447 | { |
1253 | int fold_start; | 1448 | int fold_start; |
1254 | int fold_end; | 1449 | int fold_end; |
1255 | int fold_i; | 1450 | int fold_i; |
1256 | /* This ensures we never repeat spectral content within one band */ | 1451 | /* This ensures we never repeat spectral content within one band */ |
1257 | effective_lowband = IMAX(M*eBands[start], M*eBands[lowband_offset]-N); | 1452 | effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N); |
1258 | fold_start = lowband_offset; | 1453 | fold_start = lowband_offset; |
1259 | while(M*eBands[--fold_start] > effective_lowband); | 1454 | while(M*eBands[--fold_start] > effective_lowband+norm_offset); |
1260 | fold_end = lowband_offset-1; | 1455 | fold_end = lowband_offset-1; |
1261 | while(M*eBands[++fold_end] < effective_lowband+N); | 1456 | while(M*eBands[++fold_end] < effective_lowband+norm_offset+N); |
1262 | x_cm = y_cm = 0; | 1457 | x_cm = y_cm = 0; |
1263 | fold_i = fold_start; do { | 1458 | fold_i = fold_start; do { |
1264 | x_cm |= collapse_masks[fold_i*C+0]; | 1459 | x_cm |= collapse_masks[fold_i*C+0]; |
@@ -1266,7 +1461,7 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1266 | } while (++fold_i<fold_end); | 1461 | } while (++fold_i<fold_end); |
1267 | } | 1462 | } |
1268 | /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost | 1463 | /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost |
1269 | always) be non-zero.*/ | 1464 | always) be non-zero. */ |
1270 | else | 1465 | else |
1271 | x_cm = y_cm = (1<<B)-1; | 1466 | x_cm = y_cm = (1<<B)-1; |
1272 | 1467 | ||
@@ -1274,33 +1469,42 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1274 | { | 1469 | { |
1275 | int j; | 1470 | int j; |
1276 | 1471 | ||
1277 | /* Switch off dual stereo to do intensity */ | 1472 | /* Switch off dual stereo to do intensity. */ |
1278 | dual_stereo = 0; | 1473 | dual_stereo = 0; |
1279 | if (resynth) | 1474 | if (resynth) |
1280 | for (j=M*eBands[start];j<M*eBands[i];j++) | 1475 | for (j=0;j<M*eBands[i]-norm_offset;j++) |
1281 | norm[j] = HALF32(norm[j]+norm2[j]); | 1476 | norm[j] = HALF32(norm[j]+norm2[j]); |
1282 | } | 1477 | } |
1283 | if (dual_stereo) | 1478 | if (dual_stereo) |
1284 | { | 1479 | { |
1285 | x_cm = quant_band(encode, m, i, X, NULL, N, b/2, spread, B, intensity, tf_change, | 1480 | x_cm = quant_band(&ctx, X, N, b/2, B, |
1286 | effective_lowband != -1 ? norm+effective_lowband : NULL, ec, &remaining_bits, LM, | 1481 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, |
1287 | norm+M*eBands[i], bandE, 0, seed, Q15ONE, lowband_scratch, x_cm); | 1482 | last?NULL:norm+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, x_cm); |
1288 | y_cm = quant_band(encode, m, i, Y, NULL, N, b/2, spread, B, intensity, tf_change, | 1483 | y_cm = quant_band(&ctx, Y, N, b/2, B, |
1289 | effective_lowband != -1 ? norm2+effective_lowband : NULL, ec, &remaining_bits, LM, | 1484 | effective_lowband != -1 ? norm2+effective_lowband : NULL, LM, |
1290 | norm2+M*eBands[i], bandE, 0, seed, Q15ONE, lowband_scratch, y_cm); | 1485 | last?NULL:norm2+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, y_cm); |
1291 | } else { | 1486 | } else { |
1292 | x_cm = quant_band(encode, m, i, X, Y, N, b, spread, B, intensity, tf_change, | 1487 | if (Y!=NULL) |
1293 | effective_lowband != -1 ? norm+effective_lowband : NULL, ec, &remaining_bits, LM, | 1488 | { |
1294 | norm+M*eBands[i], bandE, 0, seed, Q15ONE, lowband_scratch, x_cm|y_cm); | 1489 | x_cm = quant_band_stereo(&ctx, X, Y, N, b, B, |
1490 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, | ||
1491 | last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm); | ||
1492 | } else { | ||
1493 | x_cm = quant_band(&ctx, X, N, b, B, | ||
1494 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, | ||
1495 | last?NULL:norm+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, x_cm|y_cm); | ||
1496 | } | ||
1295 | y_cm = x_cm; | 1497 | y_cm = x_cm; |
1296 | } | 1498 | } |
1297 | collapse_masks[i*C+0] = (unsigned char)x_cm; | 1499 | collapse_masks[i*C+0] = (unsigned char)x_cm; |
1298 | collapse_masks[i*C+C-1] = (unsigned char)y_cm; | 1500 | collapse_masks[i*C+C-1] = (unsigned char)y_cm; |
1299 | balance += pulses[i] + tell; | 1501 | balance += pulses[i] + tell; |
1300 | 1502 | ||
1301 | /* Update the folding position only as long as we have 1 bit/sample depth */ | 1503 | /* Update the folding position only as long as we have 1 bit/sample depth. */ |
1302 | update_lowband = b>(N<<BITRES); | 1504 | update_lowband = b>(N<<BITRES); |
1303 | } | 1505 | } |
1506 | *seed = ctx.seed; | ||
1507 | |||
1304 | RESTORE_STACK; | 1508 | RESTORE_STACK; |
1305 | } | 1509 | } |
1306 | 1510 | ||
diff --git a/lib/rbcodec/codecs/libopus/celt/bands.h b/lib/rbcodec/codecs/libopus/celt/bands.h index 9ff8ffd7ba..96ba52a649 100644 --- a/lib/rbcodec/codecs/libopus/celt/bands.h +++ b/lib/rbcodec/codecs/libopus/celt/bands.h | |||
@@ -39,7 +39,7 @@ | |||
39 | /** Compute the amplitude (sqrt energy) in each of the bands | 39 | /** Compute the amplitude (sqrt energy) in each of the bands |
40 | * @param m Mode data | 40 | * @param m Mode data |
41 | * @param X Spectrum | 41 | * @param X Spectrum |
42 | * @param bands Square root of the energy for each band (returned) | 42 | * @param bandE Square root of the energy for each band (returned) |
43 | */ | 43 | */ |
44 | void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bandE, int end, int C, int M); | 44 | void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bandE, int end, int C, int M); |
45 | 45 | ||
@@ -49,16 +49,17 @@ void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *band | |||
49 | equal to 1 | 49 | equal to 1 |
50 | * @param m Mode data | 50 | * @param m Mode data |
51 | * @param X Spectrum (returned normalised) | 51 | * @param X Spectrum (returned normalised) |
52 | * @param bands Square root of the energy for each band | 52 | * @param bandE Square root of the energy for each band |
53 | */ | 53 | */ |
54 | void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, celt_norm * OPUS_RESTRICT X, const celt_ener *bandE, int end, int C, int M); | 54 | void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, celt_norm * OPUS_RESTRICT X, const celt_ener *bandE, int end, int C, int M); |
55 | 55 | ||
56 | /** Denormalise each band of X to restore full amplitude | 56 | /** Denormalise each band of X to restore full amplitude |
57 | * @param m Mode data | 57 | * @param m Mode data |
58 | * @param X Spectrum (returned de-normalised) | 58 | * @param X Spectrum (returned de-normalised) |
59 | * @param bands Square root of the energy for each band | 59 | * @param bandE Square root of the energy for each band |
60 | */ | 60 | */ |
61 | void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X, celt_sig * OPUS_RESTRICT freq, const celt_ener *bandE, int end, int C, int M); | 61 | void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X, |
62 | celt_sig * OPUS_RESTRICT freq, const opus_val16 *bandE, int start, int end, int C, int M); | ||
62 | 63 | ||
63 | #define SPREAD_NONE (0) | 64 | #define SPREAD_NONE (0) |
64 | #define SPREAD_LIGHT (1) | 65 | #define SPREAD_LIGHT (1) |
@@ -76,14 +77,30 @@ void measure_norm_mse(const CELTMode *m, float *X, float *X0, float *bandE, floa | |||
76 | void haar1(celt_norm *X, int N0, int stride); | 77 | void haar1(celt_norm *X, int N0, int stride); |
77 | 78 | ||
78 | /** Quantisation/encoding of the residual spectrum | 79 | /** Quantisation/encoding of the residual spectrum |
80 | * @param encode flag that indicates whether we're encoding (1) or decoding (0) | ||
79 | * @param m Mode data | 81 | * @param m Mode data |
82 | * @param start First band to process | ||
83 | * @param end Last band to process + 1 | ||
80 | * @param X Residual (normalised) | 84 | * @param X Residual (normalised) |
85 | * @param Y Residual (normalised) for second channel (or NULL for mono) | ||
86 | * @param collapse_masks Anti-collapse tracking mask | ||
87 | * @param bandE Square root of the energy for each band | ||
88 | * @param pulses Bit allocation (per band) for PVQ | ||
89 | * @param shortBlocks Zero for long blocks, non-zero for short blocks | ||
90 | * @param spread Amount of spreading to use | ||
91 | * @param dual_stereo Zero for MS stereo, non-zero for dual stereo | ||
92 | * @param intensity First band to use intensity stereo | ||
93 | * @param tf_res Time-frequency resolution change | ||
81 | * @param total_bits Total number of bits that can be used for the frame (including the ones already spent) | 94 | * @param total_bits Total number of bits that can be used for the frame (including the ones already spent) |
82 | * @param enc Entropy encoder | 95 | * @param balance Number of unallocated bits |
96 | * @param en Entropy coder state | ||
97 | * @param LM log2() of the number of 2.5 subframes in the frame | ||
98 | * @param codedBands Last band to receive bits + 1 | ||
99 | * @param seed Random generator seed | ||
83 | */ | 100 | */ |
84 | void quant_all_bands(int encode, const CELTMode *m, int start, int end, | 101 | void quant_all_bands(int encode, const CELTMode *m, int start, int end, |
85 | celt_norm * X, celt_norm * Y, unsigned char *collapse_masks, const celt_ener *bandE, int *pulses, | 102 | celt_norm * X, celt_norm * Y, unsigned char *collapse_masks, const celt_ener *bandE, int *pulses, |
86 | int time_domain, int fold, int dual_stereo, int intensity, int *tf_res, | 103 | int shortBlocks, int spread, int dual_stereo, int intensity, int *tf_res, |
87 | opus_int32 total_bits, opus_int32 balance, ec_ctx *ec, int M, int codedBands, opus_uint32 *seed); | 104 | opus_int32 total_bits, opus_int32 balance, ec_ctx *ec, int M, int codedBands, opus_uint32 *seed); |
88 | 105 | ||
89 | void anti_collapse(const CELTMode *m, celt_norm *X_, unsigned char *collapse_masks, int LM, int C, int size, | 106 | void anti_collapse(const CELTMode *m, celt_norm *X_, unsigned char *collapse_masks, int LM, int C, int size, |
@@ -92,4 +109,6 @@ void anti_collapse(const CELTMode *m, celt_norm *X_, unsigned char *collapse_mas | |||
92 | 109 | ||
93 | opus_uint32 celt_lcg_rand(opus_uint32 seed); | 110 | opus_uint32 celt_lcg_rand(opus_uint32 seed); |
94 | 111 | ||
112 | int hysteresis_decision(opus_val16 val, const opus_val16 *thresholds, const opus_val16 *hysteresis, int N, int prev); | ||
113 | |||
95 | #endif /* BANDS_H */ | 114 | #endif /* BANDS_H */ |
diff --git a/lib/rbcodec/codecs/libopus/celt/celt.c b/lib/rbcodec/codecs/libopus/celt/celt.c index 52a66d1b68..3e0ce6e6a5 100644 --- a/lib/rbcodec/codecs/libopus/celt/celt.c +++ b/lib/rbcodec/codecs/libopus/celt/celt.c | |||
@@ -28,7 +28,7 @@ | |||
28 | */ | 28 | */ |
29 | 29 | ||
30 | #ifdef HAVE_CONFIG_H | 30 | #ifdef HAVE_CONFIG_H |
31 | #include "opus_config.h" | 31 | #include "config.h" |
32 | #endif | 32 | #endif |
33 | 33 | ||
34 | #define CELT_C | 34 | #define CELT_C |
@@ -50,62 +50,12 @@ | |||
50 | #include "celt_lpc.h" | 50 | #include "celt_lpc.h" |
51 | #include "vq.h" | 51 | #include "vq.h" |
52 | 52 | ||
53 | #ifndef OPUS_VERSION | 53 | #ifndef PACKAGE_VERSION |
54 | #define OPUS_VERSION "unknown" | 54 | #define PACKAGE_VERSION "unknown" |
55 | #endif | 55 | #endif |
56 | 56 | ||
57 | #ifdef CUSTOM_MODES | ||
58 | #define OPUS_CUSTOM_NOSTATIC | ||
59 | #else | ||
60 | #define OPUS_CUSTOM_NOSTATIC static inline | ||
61 | #endif | ||
62 | |||
63 | static const unsigned char trim_icdf[11] = {126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0}; | ||
64 | /* Probs: NONE: 21.875%, LIGHT: 6.25%, NORMAL: 65.625%, AGGRESSIVE: 6.25% */ | ||
65 | static const unsigned char spread_icdf[4] = {25, 23, 2, 0}; | ||
66 | |||
67 | static const unsigned char tapset_icdf[3]={2,1,0}; | ||
68 | |||
69 | #ifdef CUSTOM_MODES | ||
70 | static const unsigned char toOpusTable[20] = { | ||
71 | 0xE0, 0xE8, 0xF0, 0xF8, | ||
72 | 0xC0, 0xC8, 0xD0, 0xD8, | ||
73 | 0xA0, 0xA8, 0xB0, 0xB8, | ||
74 | 0x00, 0x00, 0x00, 0x00, | ||
75 | 0x80, 0x88, 0x90, 0x98, | ||
76 | }; | ||
77 | |||
78 | static const unsigned char fromOpusTable[16] = { | ||
79 | 0x80, 0x88, 0x90, 0x98, | ||
80 | 0x40, 0x48, 0x50, 0x58, | ||
81 | 0x20, 0x28, 0x30, 0x38, | ||
82 | 0x00, 0x08, 0x10, 0x18 | ||
83 | }; | ||
84 | |||
85 | static inline int toOpus(unsigned char c) | ||
86 | { | ||
87 | int ret=0; | ||
88 | if (c<0xA0) | ||
89 | ret = toOpusTable[c>>3]; | ||
90 | if (ret == 0) | ||
91 | return -1; | ||
92 | else | ||
93 | return ret|(c&0x7); | ||
94 | } | ||
95 | 57 | ||
96 | static inline int fromOpus(unsigned char c) | 58 | int resampling_factor(opus_int32 rate) |
97 | { | ||
98 | if (c<0x80) | ||
99 | return -1; | ||
100 | else | ||
101 | return fromOpusTable[(c>>3)-16] | (c&0x7); | ||
102 | } | ||
103 | #endif /* CUSTOM_MODES */ | ||
104 | |||
105 | #define COMBFILTER_MAXPERIOD 1024 | ||
106 | #define COMBFILTER_MINPERIOD 15 | ||
107 | |||
108 | static int resampling_factor(opus_int32 rate) | ||
109 | { | 59 | { |
110 | int ret; | 60 | int ret; |
111 | switch (rate) | 61 | switch (rate) |
@@ -135,693 +85,101 @@ static int resampling_factor(opus_int32 rate) | |||
135 | return ret; | 85 | return ret; |
136 | } | 86 | } |
137 | 87 | ||
138 | /** Encoder state | 88 | #ifndef OVERRIDE_COMB_FILTER_CONST |
139 | @brief Encoder state | 89 | static void comb_filter_const(opus_val32 *y, opus_val32 *x, int T, int N, |
140 | */ | 90 | opus_val16 g10, opus_val16 g11, opus_val16 g12) |
141 | struct OpusCustomEncoder { | ||
142 | const OpusCustomMode *mode; /**< Mode used by the encoder */ | ||
143 | int overlap; | ||
144 | int channels; | ||
145 | int stream_channels; | ||
146 | |||
147 | int force_intra; | ||
148 | int clip; | ||
149 | int disable_pf; | ||
150 | int complexity; | ||
151 | int upsample; | ||
152 | int start, end; | ||
153 | |||
154 | opus_int32 bitrate; | ||
155 | int vbr; | ||
156 | int signalling; | ||
157 | int constrained_vbr; /* If zero, VBR can do whatever it likes with the rate */ | ||
158 | int loss_rate; | ||
159 | int lsb_depth; | ||
160 | |||
161 | /* Everything beyond this point gets cleared on a reset */ | ||
162 | #define ENCODER_RESET_START rng | ||
163 | |||
164 | opus_uint32 rng; | ||
165 | int spread_decision; | ||
166 | opus_val32 delayedIntra; | ||
167 | int tonal_average; | ||
168 | int lastCodedBands; | ||
169 | int hf_average; | ||
170 | int tapset_decision; | ||
171 | |||
172 | int prefilter_period; | ||
173 | opus_val16 prefilter_gain; | ||
174 | int prefilter_tapset; | ||
175 | #ifdef RESYNTH | ||
176 | int prefilter_period_old; | ||
177 | opus_val16 prefilter_gain_old; | ||
178 | int prefilter_tapset_old; | ||
179 | #endif | ||
180 | int consec_transient; | ||
181 | |||
182 | opus_val32 preemph_memE[2]; | ||
183 | opus_val32 preemph_memD[2]; | ||
184 | |||
185 | /* VBR-related parameters */ | ||
186 | opus_int32 vbr_reservoir; | ||
187 | opus_int32 vbr_drift; | ||
188 | opus_int32 vbr_offset; | ||
189 | opus_int32 vbr_count; | ||
190 | |||
191 | #ifdef RESYNTH | ||
192 | celt_sig syn_mem[2][2*MAX_PERIOD]; | ||
193 | #endif | ||
194 | |||
195 | celt_sig in_mem[1]; /* Size = channels*mode->overlap */ | ||
196 | /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_PERIOD */ | ||
197 | /* celt_sig overlap_mem[], Size = channels*mode->overlap */ | ||
198 | /* opus_val16 oldEBands[], Size = 2*channels*mode->nbEBands */ | ||
199 | }; | ||
200 | |||
201 | #if 0 | ||
202 | int celt_encoder_get_size(int channels) | ||
203 | { | ||
204 | CELTMode *mode = opus_custom_mode_create(48000, 960, NULL); | ||
205 | return opus_custom_encoder_get_size(mode, channels); | ||
206 | } | ||
207 | |||
208 | OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels) | ||
209 | { | ||
210 | int size = sizeof(struct CELTEncoder) | ||
211 | + (2*channels*mode->overlap-1)*sizeof(celt_sig) | ||
212 | + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig) | ||
213 | + 3*channels*mode->nbEBands*sizeof(opus_val16); | ||
214 | return size; | ||
215 | } | ||
216 | |||
217 | #ifdef CUSTOM_MODES | ||
218 | CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error) | ||
219 | { | ||
220 | int ret; | ||
221 | CELTEncoder *st = (CELTEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels)); | ||
222 | /* init will handle the NULL case */ | ||
223 | ret = opus_custom_encoder_init(st, mode, channels); | ||
224 | if (ret != OPUS_OK) | ||
225 | { | ||
226 | opus_custom_encoder_destroy(st); | ||
227 | st = NULL; | ||
228 | } | ||
229 | if (error) | ||
230 | *error = ret; | ||
231 | return st; | ||
232 | } | ||
233 | #endif /* CUSTOM_MODES */ | ||
234 | |||
235 | int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels) | ||
236 | { | ||
237 | int ret; | ||
238 | ret = opus_custom_encoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels); | ||
239 | if (ret != OPUS_OK) | ||
240 | return ret; | ||
241 | st->upsample = resampling_factor(sampling_rate); | ||
242 | return OPUS_OK; | ||
243 | } | ||
244 | |||
245 | OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels) | ||
246 | { | ||
247 | if (channels < 0 || channels > 2) | ||
248 | return OPUS_BAD_ARG; | ||
249 | |||
250 | if (st==NULL || mode==NULL) | ||
251 | return OPUS_ALLOC_FAIL; | ||
252 | |||
253 | OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels)); | ||
254 | |||
255 | st->mode = mode; | ||
256 | st->overlap = mode->overlap; | ||
257 | st->stream_channels = st->channels = channels; | ||
258 | |||
259 | st->upsample = 1; | ||
260 | st->start = 0; | ||
261 | st->end = st->mode->effEBands; | ||
262 | st->signalling = 1; | ||
263 | |||
264 | st->constrained_vbr = 1; | ||
265 | st->clip = 1; | ||
266 | |||
267 | st->bitrate = OPUS_BITRATE_MAX; | ||
268 | st->vbr = 0; | ||
269 | st->force_intra = 0; | ||
270 | st->complexity = 5; | ||
271 | st->lsb_depth=24; | ||
272 | |||
273 | opus_custom_encoder_ctl(st, OPUS_RESET_STATE); | ||
274 | |||
275 | return OPUS_OK; | ||
276 | } | ||
277 | |||
278 | #ifdef CUSTOM_MODES | ||
279 | void opus_custom_encoder_destroy(CELTEncoder *st) | ||
280 | { | ||
281 | opus_free(st); | ||
282 | } | ||
283 | #endif /* CUSTOM_MODES */ | ||
284 | #endif | ||
285 | |||
286 | static inline opus_val16 SIG2WORD16(celt_sig x) | ||
287 | { | ||
288 | #ifdef FIXED_POINT | ||
289 | x = PSHR32(x, SIG_SHIFT); | ||
290 | x = MAX32(x, -32768); | ||
291 | x = MIN32(x, 32767); | ||
292 | return EXTRACT16(x); | ||
293 | #else | ||
294 | return (opus_val16)x; | ||
295 | #endif | ||
296 | } | ||
297 | |||
298 | #if 0 | ||
299 | static int transient_analysis(const opus_val32 * OPUS_RESTRICT in, int len, int C, | ||
300 | int overlap) | ||
301 | { | 91 | { |
92 | opus_val32 x0, x1, x2, x3, x4; | ||
302 | int i; | 93 | int i; |
303 | VARDECL(opus_val16, tmp); | 94 | x4 = x[-T-2]; |
304 | opus_val32 mem0=0,mem1=0; | 95 | x3 = x[-T-1]; |
305 | int is_transient = 0; | 96 | x2 = x[-T]; |
306 | int block; | 97 | x1 = x[-T+1]; |
307 | int N; | ||
308 | VARDECL(opus_val16, bins); | ||
309 | SAVE_STACK; | ||
310 | ALLOC(tmp, len, opus_val16); | ||
311 | |||
312 | block = overlap/2; | ||
313 | N=len/block; | ||
314 | ALLOC(bins, N, opus_val16); | ||
315 | if (C==1) | ||
316 | { | ||
317 | for (i=0;i<len;i++) | ||
318 | tmp[i] = SHR32(in[i],SIG_SHIFT); | ||
319 | } else { | ||
320 | for (i=0;i<len;i++) | ||
321 | tmp[i] = SHR32(ADD32(in[i],in[i+len]), SIG_SHIFT+1); | ||
322 | } | ||
323 | |||
324 | /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */ | ||
325 | for (i=0;i<len;i++) | ||
326 | { | ||
327 | opus_val32 x,y; | ||
328 | x = tmp[i]; | ||
329 | y = ADD32(mem0, x); | ||
330 | #ifdef FIXED_POINT | ||
331 | mem0 = mem1 + y - SHL32(x,1); | ||
332 | mem1 = x - SHR32(y,1); | ||
333 | #else | ||
334 | mem0 = mem1 + y - 2*x; | ||
335 | mem1 = x - .5f*y; | ||
336 | #endif | ||
337 | tmp[i] = EXTRACT16(SHR32(y,2)); | ||
338 | } | ||
339 | /* First few samples are bad because we don't propagate the memory */ | ||
340 | for (i=0;i<12;i++) | ||
341 | tmp[i] = 0; | ||
342 | |||
343 | for (i=0;i<N;i++) | 98 | for (i=0;i<N;i++) |
344 | { | 99 | { |
345 | int j; | 100 | x0=x[i-T+2]; |
346 | opus_val16 max_abs=0; | 101 | y[i] = x[i] |
347 | for (j=0;j<block;j++) | 102 | + MULT16_32_Q15(g10,x2) |
348 | max_abs = MAX16(max_abs, ABS16(tmp[i*block+j])); | 103 | + MULT16_32_Q15(g11,ADD32(x1,x3)) |
349 | bins[i] = max_abs; | 104 | + MULT16_32_Q15(g12,ADD32(x0,x4)); |
350 | } | 105 | x4=x3; |
351 | for (i=0;i<N;i++) | 106 | x3=x2; |
352 | { | 107 | x2=x1; |
353 | int j; | 108 | x1=x0; |
354 | int conseq=0; | ||
355 | opus_val16 t1, t2, t3; | ||
356 | |||
357 | t1 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]); | ||
358 | t2 = MULT16_16_Q15(QCONST16(.4f, 15), bins[i]); | ||
359 | t3 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]); | ||
360 | for (j=0;j<i;j++) | ||
361 | { | ||
362 | if (bins[j] < t1) | ||
363 | conseq++; | ||
364 | if (bins[j] < t2) | ||
365 | conseq++; | ||
366 | else | ||
367 | conseq = 0; | ||
368 | } | ||
369 | if (conseq>=3) | ||
370 | is_transient=1; | ||
371 | conseq = 0; | ||
372 | for (j=i+1;j<N;j++) | ||
373 | { | ||
374 | if (bins[j] < t3) | ||
375 | conseq++; | ||
376 | else | ||
377 | conseq = 0; | ||
378 | } | ||
379 | if (conseq>=7) | ||
380 | is_transient=1; | ||
381 | } | ||
382 | RESTORE_STACK; | ||
383 | #ifdef FUZZING | ||
384 | is_transient = rand()&0x1; | ||
385 | #endif | ||
386 | return is_transient; | ||
387 | } | ||
388 | |||
389 | /** Apply window and compute the MDCT for all sub-frames and | ||
390 | all channels in a frame */ | ||
391 | static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * OPUS_RESTRICT in, celt_sig * OPUS_RESTRICT out, int C, int LM) | ||
392 | { | ||
393 | if (C==1 && !shortBlocks) | ||
394 | { | ||
395 | const int overlap = OVERLAP(mode); | ||
396 | clt_mdct_forward(&mode->mdct, in, out, mode->window, overlap, mode->maxLM-LM, 1); | ||
397 | } else { | ||
398 | const int overlap = OVERLAP(mode); | ||
399 | int N = mode->shortMdctSize<<LM; | ||
400 | int B = 1; | ||
401 | int b, c; | ||
402 | if (shortBlocks) | ||
403 | { | ||
404 | N = mode->shortMdctSize; | ||
405 | B = shortBlocks; | ||
406 | } | ||
407 | c=0; do { | ||
408 | for (b=0;b<B;b++) | ||
409 | { | ||
410 | /* Interleaving the sub-frames while doing the MDCTs */ | ||
411 | clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N, &out[b+c*N*B], mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B); | ||
412 | } | ||
413 | } while (++c<C); | ||
414 | } | 109 | } |
415 | } | ||
416 | #endif | ||
417 | |||
418 | /** Compute the IMDCT and apply window for all sub-frames and | ||
419 | all channels in a frame */ | ||
420 | static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X, | ||
421 | celt_sig * OPUS_RESTRICT out_mem[], | ||
422 | celt_sig * OPUS_RESTRICT overlap_mem[], int C, int LM) | ||
423 | { | ||
424 | int c; | ||
425 | const int N = mode->shortMdctSize<<LM; | ||
426 | const int overlap = OVERLAP(mode); | ||
427 | VARDECL(opus_val32, x); | ||
428 | SAVE_STACK; | ||
429 | 110 | ||
430 | ALLOC(x, N+overlap, opus_val32); | ||
431 | c=0; do { | ||
432 | int j; | ||
433 | int b; | ||
434 | int N2 = N; | ||
435 | int B = 1; | ||
436 | |||
437 | if (shortBlocks) | ||
438 | { | ||
439 | N2 = mode->shortMdctSize; | ||
440 | B = shortBlocks; | ||
441 | } | ||
442 | /* Prevents problems from the imdct doing the overlap-add */ | ||
443 | OPUS_CLEAR(x, overlap); | ||
444 | |||
445 | for (b=0;b<B;b++) | ||
446 | { | ||
447 | /* IMDCT on the interleaved the sub-frames */ | ||
448 | clt_mdct_backward(&mode->mdct, &X[b+c*N2*B], x+N2*b, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B); | ||
449 | } | ||
450 | |||
451 | /* overlap can be divided by 4 */ | ||
452 | for (j=0;j<overlap;j+=4) | ||
453 | { | ||
454 | out_mem[c][j ] = x[j ] + overlap_mem[c][j ]; | ||
455 | out_mem[c][j+1] = x[j+1] + overlap_mem[c][j+1]; | ||
456 | out_mem[c][j+2] = x[j+2] + overlap_mem[c][j+2]; | ||
457 | out_mem[c][j+3] = x[j+3] + overlap_mem[c][j+3]; | ||
458 | } | ||
459 | OPUS_COPY(out_mem[c]+overlap, x+overlap, N-overlap); | ||
460 | OPUS_COPY(overlap_mem[c] , x+N , overlap); | ||
461 | } while (++c<C); | ||
462 | RESTORE_STACK; | ||
463 | } | 111 | } |
464 | |||
465 | static void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, /* int downsample,*/ const opus_val16 *coef, celt_sig *mem) | ||
466 | { | ||
467 | int c; | ||
468 | /* int count=0;*/ | ||
469 | c=0; do { | ||
470 | int j; | ||
471 | celt_sig * OPUS_RESTRICT x; | ||
472 | opus_val16 * OPUS_RESTRICT y; | ||
473 | opus_val16 coef0 = coef[0]; | ||
474 | #ifdef CUSTOM_MODES | ||
475 | opus_val16 coef1 = coef[1]; | ||
476 | opus_val16 coef3 = coef[3]; | ||
477 | #endif | 112 | #endif |
478 | celt_sig m = mem[c]; | ||
479 | x =in[c]; | ||
480 | y = pcm+c; | ||
481 | for (j=0;j<N;j++) | ||
482 | { | ||
483 | celt_sig tmp = *x + m; | ||
484 | m = MULT16_32_Q15(coef0, tmp); | ||
485 | #ifdef CUSTOM_MODES | ||
486 | m -= MULT16_32_Q15(coef1, *x); | ||
487 | tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2); | ||
488 | #endif | ||
489 | x++; | ||
490 | /* Technically the store could be moved outside of the if because | ||
491 | the stores we don't want will just be overwritten */ | ||
492 | /* ROCKBOX: we don't downsample | ||
493 | if (count==0) */ | ||
494 | *y = SCALEOUT(SIG2WORD16(tmp)); | ||
495 | /* if (++count==downsample) */ | ||
496 | { | ||
497 | y+=C; | ||
498 | /* count=0; */ | ||
499 | } | ||
500 | } | ||
501 | mem[c] = m; | ||
502 | } while (++c<C); | ||
503 | } | ||
504 | 113 | ||
505 | static void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N, | 114 | void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N, |
506 | opus_val16 g0, opus_val16 g1, int tapset0, int tapset1, | 115 | opus_val16 g0, opus_val16 g1, int tapset0, int tapset1, |
507 | const opus_val16 *window, int overlap) | 116 | const opus_val16 *window, int overlap) |
508 | { | 117 | { |
509 | /* Multiply-adds are only needed if g0 or g1 are non-zero. In all other cases a simple | 118 | int i; |
510 | * copy of vector x to y is possible. */ | 119 | /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */ |
511 | if (g0!=0 || g1!=0) | 120 | opus_val16 g00, g01, g02, g10, g11, g12; |
512 | { | 121 | opus_val32 x0, x1, x2, x3, x4; |
513 | int i; | 122 | static const opus_val16 gains[3][3] = { |
514 | opus_val16 g00, g01, g02, g10, g11, g12, idx0, idx1; | ||
515 | static const opus_val16 gains[3][3] = { | ||
516 | {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)}, | 123 | {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)}, |
517 | {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)}, | 124 | {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)}, |
518 | {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}}; | 125 | {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}}; |
519 | g00 = MULT16_16_Q15(g0, gains[tapset0][0]); | ||
520 | g01 = MULT16_16_Q15(g0, gains[tapset0][1]); | ||
521 | g02 = MULT16_16_Q15(g0, gains[tapset0][2]); | ||
522 | g10 = MULT16_16_Q15(g1, gains[tapset1][0]); | ||
523 | g11 = MULT16_16_Q15(g1, gains[tapset1][1]); | ||
524 | g12 = MULT16_16_Q15(g1, gains[tapset1][2]); | ||
525 | /* printf("g0 %d g1 %d\n", g0,g1); */ | ||
526 | idx0 = -T0; | ||
527 | idx1 = -T1; | ||
528 | for (i=0;i<overlap;i++,idx0++,idx1++) | ||
529 | { | ||
530 | opus_val16 f0, f1; | ||
531 | f1 = MULT16_16_Q15(window[i],window[i]); | ||
532 | f0 = Q15ONE - f1; | ||
533 | y[i] = x[i] | ||
534 | + MULT16_32_Q15(MULT16_16_Q15(f0,g02), x[idx0-2]) | ||
535 | + MULT16_32_Q15(MULT16_16_Q15(f0,g01), x[idx0-1]) | ||
536 | + MULT16_32_Q15(MULT16_16_Q15(f0,g00), x[idx0 ]) | ||
537 | + MULT16_32_Q15(MULT16_16_Q15(f0,g01), x[idx0+1]) | ||
538 | + MULT16_32_Q15(MULT16_16_Q15(f0,g02), x[idx0+2]) | ||
539 | + MULT16_32_Q15(MULT16_16_Q15(f1,g12), x[idx1-2]) | ||
540 | + MULT16_32_Q15(MULT16_16_Q15(f1,g11), x[idx1-1]) | ||
541 | + MULT16_32_Q15(MULT16_16_Q15(f1,g10), x[idx1 ]) | ||
542 | + MULT16_32_Q15(MULT16_16_Q15(f1,g11), x[idx1+1]) | ||
543 | + MULT16_32_Q15(MULT16_16_Q15(f1,g12), x[idx1+2]); | ||
544 | } | ||
545 | /* No multiply-add required if g1=0 as all multiplicants are =0. */ | ||
546 | if (g1!=0) | ||
547 | { | ||
548 | idx1 = overlap-T1; | ||
549 | for (i=overlap;i<N;i++,idx1++) | ||
550 | { | ||
551 | y[i] = x[i] | ||
552 | + MULT16_32_Q15(g12, x[idx1-2]) | ||
553 | + MULT16_32_Q15(g11, x[idx1-1]) | ||
554 | + MULT16_32_Q15(g10, x[idx1 ]) | ||
555 | + MULT16_32_Q15(g11, x[idx1+1]) | ||
556 | + MULT16_32_Q15(g12, x[idx1+2]); | ||
557 | } | ||
558 | } | ||
559 | /* Only perform vector copy if source and destination are not same. */ | ||
560 | else if (x != y) | ||
561 | { | ||
562 | /* Copy part of vector from x[overlap..N] to y[overlap..N] */ | ||
563 | OPUS_COPY(y+overlap, x+overlap, N-overlap); | ||
564 | } | ||
565 | } | ||
566 | /* Only perform vector copy if source and destination are not same. */ | ||
567 | else if (x != y) | ||
568 | { | ||
569 | /* Copy full vector from x[0..N] to y[0..N] */ | ||
570 | OPUS_COPY(y, x, N); | ||
571 | } | ||
572 | } | ||
573 | 126 | ||
574 | static const signed char tf_select_table[4][8] = { | 127 | if (g0==0 && g1==0) |
128 | { | ||
129 | /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */ | ||
130 | if (x!=y) | ||
131 | OPUS_MOVE(y, x, N); | ||
132 | return; | ||
133 | } | ||
134 | g00 = MULT16_16_Q15(g0, gains[tapset0][0]); | ||
135 | g01 = MULT16_16_Q15(g0, gains[tapset0][1]); | ||
136 | g02 = MULT16_16_Q15(g0, gains[tapset0][2]); | ||
137 | g10 = MULT16_16_Q15(g1, gains[tapset1][0]); | ||
138 | g11 = MULT16_16_Q15(g1, gains[tapset1][1]); | ||
139 | g12 = MULT16_16_Q15(g1, gains[tapset1][2]); | ||
140 | x1 = x[-T1+1]; | ||
141 | x2 = x[-T1 ]; | ||
142 | x3 = x[-T1-1]; | ||
143 | x4 = x[-T1-2]; | ||
144 | for (i=0;i<overlap;i++) | ||
145 | { | ||
146 | opus_val16 f; | ||
147 | x0=x[i-T1+2]; | ||
148 | f = MULT16_16_Q15(window[i],window[i]); | ||
149 | y[i] = x[i] | ||
150 | + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0]) | ||
151 | + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),ADD32(x[i-T0+1],x[i-T0-1])) | ||
152 | + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),ADD32(x[i-T0+2],x[i-T0-2])) | ||
153 | + MULT16_32_Q15(MULT16_16_Q15(f,g10),x2) | ||
154 | + MULT16_32_Q15(MULT16_16_Q15(f,g11),ADD32(x1,x3)) | ||
155 | + MULT16_32_Q15(MULT16_16_Q15(f,g12),ADD32(x0,x4)); | ||
156 | x4=x3; | ||
157 | x3=x2; | ||
158 | x2=x1; | ||
159 | x1=x0; | ||
160 | |||
161 | } | ||
162 | if (g1==0) | ||
163 | { | ||
164 | /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */ | ||
165 | if (x!=y) | ||
166 | OPUS_MOVE(y+overlap, x+overlap, N-overlap); | ||
167 | return; | ||
168 | } | ||
169 | |||
170 | /* Compute the part with the constant filter. */ | ||
171 | comb_filter_const(y+i, x+i, T1, N-i, g10, g11, g12); | ||
172 | } | ||
173 | |||
174 | const signed char tf_select_table[4][8] = { | ||
575 | {0, -1, 0, -1, 0,-1, 0,-1}, | 175 | {0, -1, 0, -1, 0,-1, 0,-1}, |
576 | {0, -1, 0, -2, 1, 0, 1,-1}, | 176 | {0, -1, 0, -2, 1, 0, 1,-1}, |
577 | {0, -2, 0, -3, 2, 0, 1,-1}, | 177 | {0, -2, 0, -3, 2, 0, 1,-1}, |
578 | {0, -2, 0, -3, 3, 0, 1,-1}, | 178 | {0, -2, 0, -3, 3, 0, 1,-1}, |
579 | }; | 179 | }; |
580 | 180 | ||
581 | #if 0 | ||
582 | static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, int width) | ||
583 | { | ||
584 | int i, j; | ||
585 | static const opus_val16 sqrtM_1[4] = {Q15ONE, QCONST16(.70710678f,15), QCONST16(0.5f,15), QCONST16(0.35355339f,15)}; | ||
586 | opus_val32 L1; | ||
587 | opus_val16 bias; | ||
588 | L1=0; | ||
589 | for (i=0;i<1<<LM;i++) | ||
590 | { | ||
591 | opus_val32 L2 = 0; | ||
592 | for (j=0;j<N>>LM;j++) | ||
593 | L2 = MAC16_16(L2, tmp[(j<<LM)+i], tmp[(j<<LM)+i]); | ||
594 | L1 += celt_sqrt(L2); | ||
595 | } | ||
596 | L1 = MULT16_32_Q15(sqrtM_1[LM], L1); | ||
597 | if (width==1) | ||
598 | bias = QCONST16(.12f,15)*LM; | ||
599 | else if (width==2) | ||
600 | bias = QCONST16(.05f,15)*LM; | ||
601 | else | ||
602 | bias = QCONST16(.02f,15)*LM; | ||
603 | L1 = MAC16_32_Q15(L1, bias, L1); | ||
604 | return L1; | ||
605 | } | ||
606 | 181 | ||
607 | static int tf_analysis(const CELTMode *m, int len, int C, int isTransient, | 182 | void init_caps(const CELTMode *m,int *cap,int LM,int C) |
608 | int *tf_res, int nbCompressedBytes, celt_norm *X, int N0, int LM, | ||
609 | int *tf_sum) | ||
610 | { | ||
611 | int i; | ||
612 | VARDECL(int, metric); | ||
613 | int cost0; | ||
614 | int cost1; | ||
615 | VARDECL(int, path0); | ||
616 | VARDECL(int, path1); | ||
617 | VARDECL(celt_norm, tmp); | ||
618 | int lambda; | ||
619 | int tf_select=0; | ||
620 | SAVE_STACK; | ||
621 | |||
622 | if (nbCompressedBytes<15*C) | ||
623 | { | ||
624 | *tf_sum = 0; | ||
625 | for (i=0;i<len;i++) | ||
626 | tf_res[i] = isTransient; | ||
627 | return 0; | ||
628 | } | ||
629 | if (nbCompressedBytes<40) | ||
630 | lambda = 12; | ||
631 | else if (nbCompressedBytes<60) | ||
632 | lambda = 6; | ||
633 | else if (nbCompressedBytes<100) | ||
634 | lambda = 4; | ||
635 | else | ||
636 | lambda = 3; | ||
637 | |||
638 | ALLOC(metric, len, int); | ||
639 | ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm); | ||
640 | ALLOC(path0, len, int); | ||
641 | ALLOC(path1, len, int); | ||
642 | |||
643 | *tf_sum = 0; | ||
644 | for (i=0;i<len;i++) | ||
645 | { | ||
646 | int j, k, N; | ||
647 | opus_val32 L1, best_L1; | ||
648 | int best_level=0; | ||
649 | N = (m->eBands[i+1]-m->eBands[i])<<LM; | ||
650 | for (j=0;j<N;j++) | ||
651 | tmp[j] = X[j+(m->eBands[i]<<LM)]; | ||
652 | /* Just add the right channel if we're in stereo */ | ||
653 | if (C==2) | ||
654 | for (j=0;j<N;j++) | ||
655 | tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1)); | ||
656 | L1 = l1_metric(tmp, N, isTransient ? LM : 0, N>>LM); | ||
657 | best_L1 = L1; | ||
658 | /*printf ("%f ", L1);*/ | ||
659 | for (k=0;k<LM;k++) | ||
660 | { | ||
661 | int B; | ||
662 | |||
663 | if (isTransient) | ||
664 | B = (LM-k-1); | ||
665 | else | ||
666 | B = k+1; | ||
667 | |||
668 | if (isTransient) | ||
669 | haar1(tmp, N>>(LM-k), 1<<(LM-k)); | ||
670 | else | ||
671 | haar1(tmp, N>>k, 1<<k); | ||
672 | |||
673 | L1 = l1_metric(tmp, N, B, N>>LM); | ||
674 | |||
675 | if (L1 < best_L1) | ||
676 | { | ||
677 | best_L1 = L1; | ||
678 | best_level = k+1; | ||
679 | } | ||
680 | } | ||
681 | /*printf ("%d ", isTransient ? LM-best_level : best_level);*/ | ||
682 | if (isTransient) | ||
683 | metric[i] = best_level; | ||
684 | else | ||
685 | metric[i] = -best_level; | ||
686 | *tf_sum += metric[i]; | ||
687 | } | ||
688 | /*printf("\n");*/ | ||
689 | /* NOTE: Future optimized implementations could detect extreme transients and set | ||
690 | tf_select = 1 but so far we have not found a reliable way of making this useful */ | ||
691 | tf_select = 0; | ||
692 | |||
693 | cost0 = 0; | ||
694 | cost1 = isTransient ? 0 : lambda; | ||
695 | /* Viterbi forward pass */ | ||
696 | for (i=1;i<len;i++) | ||
697 | { | ||
698 | int curr0, curr1; | ||
699 | int from0, from1; | ||
700 | |||
701 | from0 = cost0; | ||
702 | from1 = cost1 + lambda; | ||
703 | if (from0 < from1) | ||
704 | { | ||
705 | curr0 = from0; | ||
706 | path0[i]= 0; | ||
707 | } else { | ||
708 | curr0 = from1; | ||
709 | path0[i]= 1; | ||
710 | } | ||
711 | |||
712 | from0 = cost0 + lambda; | ||
713 | from1 = cost1; | ||
714 | if (from0 < from1) | ||
715 | { | ||
716 | curr1 = from0; | ||
717 | path1[i]= 0; | ||
718 | } else { | ||
719 | curr1 = from1; | ||
720 | path1[i]= 1; | ||
721 | } | ||
722 | cost0 = curr0 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+0]); | ||
723 | cost1 = curr1 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+1]); | ||
724 | } | ||
725 | tf_res[len-1] = cost0 < cost1 ? 0 : 1; | ||
726 | /* Viterbi backward pass to check the decisions */ | ||
727 | for (i=len-2;i>=0;i--) | ||
728 | { | ||
729 | if (tf_res[i+1] == 1) | ||
730 | tf_res[i] = path1[i+1]; | ||
731 | else | ||
732 | tf_res[i] = path0[i+1]; | ||
733 | } | ||
734 | RESTORE_STACK; | ||
735 | #ifdef FUZZING | ||
736 | tf_select = rand()&0x1; | ||
737 | tf_res[0] = rand()&0x1; | ||
738 | for (i=1;i<len;i++) | ||
739 | tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0); | ||
740 | #endif | ||
741 | return tf_select; | ||
742 | } | ||
743 | |||
744 | static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc) | ||
745 | { | ||
746 | int curr, i; | ||
747 | int tf_select_rsv; | ||
748 | int tf_changed; | ||
749 | int logp; | ||
750 | opus_uint32 budget; | ||
751 | opus_uint32 tell; | ||
752 | budget = enc->storage*8; | ||
753 | tell = ec_tell(enc); | ||
754 | logp = isTransient ? 2 : 4; | ||
755 | /* Reserve space to code the tf_select decision. */ | ||
756 | tf_select_rsv = LM>0 && tell+logp+1 <= budget; | ||
757 | budget -= tf_select_rsv; | ||
758 | curr = tf_changed = 0; | ||
759 | for (i=start;i<end;i++) | ||
760 | { | ||
761 | if (tell+logp<=budget) | ||
762 | { | ||
763 | ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp); | ||
764 | tell = ec_tell(enc); | ||
765 | curr = tf_res[i]; | ||
766 | tf_changed |= curr; | ||
767 | } | ||
768 | else | ||
769 | tf_res[i] = curr; | ||
770 | logp = isTransient ? 4 : 5; | ||
771 | } | ||
772 | /* Only code tf_select if it would actually make a difference. */ | ||
773 | if (tf_select_rsv && | ||
774 | tf_select_table[LM][4*isTransient+0+tf_changed]!= | ||
775 | tf_select_table[LM][4*isTransient+2+tf_changed]) | ||
776 | ec_enc_bit_logp(enc, tf_select, 1); | ||
777 | else | ||
778 | tf_select = 0; | ||
779 | for (i=start;i<end;i++) | ||
780 | tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]]; | ||
781 | /*printf("%d %d ", isTransient, tf_select); for(i=0;i<end;i++)printf("%d ", tf_res[i]);printf("\n");*/ | ||
782 | } | ||
783 | #endif | ||
784 | |||
785 | static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec) | ||
786 | { | ||
787 | int i, curr, tf_select; | ||
788 | int tf_select_rsv; | ||
789 | int tf_changed; | ||
790 | int logp; | ||
791 | opus_uint32 budget; | ||
792 | opus_uint32 tell; | ||
793 | |||
794 | budget = dec->storage*8; | ||
795 | tell = ec_tell(dec); | ||
796 | logp = isTransient ? 2 : 4; | ||
797 | tf_select_rsv = LM>0 && tell+logp+1<=budget; | ||
798 | budget -= tf_select_rsv; | ||
799 | tf_changed = curr = 0; | ||
800 | for (i=start;i<end;i++) | ||
801 | { | ||
802 | if (tell+logp<=budget) | ||
803 | { | ||
804 | curr ^= ec_dec_bit_logp(dec, logp); | ||
805 | tell = ec_tell(dec); | ||
806 | tf_changed |= curr; | ||
807 | } | ||
808 | tf_res[i] = curr; | ||
809 | logp = isTransient ? 4 : 5; | ||
810 | } | ||
811 | tf_select = 0; | ||
812 | if (tf_select_rsv && | ||
813 | tf_select_table[LM][4*isTransient+0+tf_changed] != | ||
814 | tf_select_table[LM][4*isTransient+2+tf_changed]) | ||
815 | { | ||
816 | tf_select = ec_dec_bit_logp(dec, 1); | ||
817 | } | ||
818 | for (i=start;i<end;i++) | ||
819 | { | ||
820 | tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]]; | ||
821 | } | ||
822 | } | ||
823 | |||
824 | static void init_caps(const CELTMode *m,int *cap,int LM,int C) | ||
825 | { | 183 | { |
826 | int i; | 184 | int i; |
827 | for (i=0;i<m->nbEBands;i++) | 185 | for (i=0;i<m->nbEBands;i++) |
@@ -832,2070 +190,6 @@ static void init_caps(const CELTMode *m,int *cap,int LM,int C) | |||
832 | } | 190 | } |
833 | } | 191 | } |
834 | 192 | ||
835 | #if 0 | ||
836 | static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X, | ||
837 | const opus_val16 *bandLogE, int end, int LM, int C, int N0) | ||
838 | { | ||
839 | int i; | ||
840 | opus_val32 diff=0; | ||
841 | int c; | ||
842 | int trim_index = 5; | ||
843 | if (C==2) | ||
844 | { | ||
845 | opus_val16 sum = 0; /* Q10 */ | ||
846 | /* Compute inter-channel correlation for low frequencies */ | ||
847 | for (i=0;i<8;i++) | ||
848 | { | ||
849 | int j; | ||
850 | opus_val32 partial = 0; | ||
851 | for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++) | ||
852 | partial = MAC16_16(partial, X[j], X[N0+j]); | ||
853 | sum = ADD16(sum, EXTRACT16(SHR32(partial, 18))); | ||
854 | } | ||
855 | sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum); | ||
856 | /*printf ("%f\n", sum);*/ | ||
857 | if (sum > QCONST16(.995f,10)) | ||
858 | trim_index-=4; | ||
859 | else if (sum > QCONST16(.92f,10)) | ||
860 | trim_index-=3; | ||
861 | else if (sum > QCONST16(.85f,10)) | ||
862 | trim_index-=2; | ||
863 | else if (sum > QCONST16(.8f,10)) | ||
864 | trim_index-=1; | ||
865 | } | ||
866 | |||
867 | /* Estimate spectral tilt */ | ||
868 | c=0; do { | ||
869 | for (i=0;i<end-1;i++) | ||
870 | { | ||
871 | diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-m->nbEBands); | ||
872 | } | ||
873 | } while (++c<C); | ||
874 | /* We divide by two here to avoid making the tilt larger for stereo as a | ||
875 | result of a bug in the loop above */ | ||
876 | diff /= 2*C*(end-1); | ||
877 | /*printf("%f\n", diff);*/ | ||
878 | if (diff > QCONST16(2.f, DB_SHIFT)) | ||
879 | trim_index--; | ||
880 | if (diff > QCONST16(8.f, DB_SHIFT)) | ||
881 | trim_index--; | ||
882 | if (diff < -QCONST16(4.f, DB_SHIFT)) | ||
883 | trim_index++; | ||
884 | if (diff < -QCONST16(10.f, DB_SHIFT)) | ||
885 | trim_index++; | ||
886 | |||
887 | if (trim_index<0) | ||
888 | trim_index = 0; | ||
889 | if (trim_index>10) | ||
890 | trim_index = 10; | ||
891 | #ifdef FUZZING | ||
892 | trim_index = rand()%11; | ||
893 | #endif | ||
894 | return trim_index; | ||
895 | } | ||
896 | |||
897 | static int stereo_analysis(const CELTMode *m, const celt_norm *X, | ||
898 | int LM, int N0) | ||
899 | { | ||
900 | int i; | ||
901 | int thetas; | ||
902 | opus_val32 sumLR = EPSILON, sumMS = EPSILON; | ||
903 | |||
904 | /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */ | ||
905 | for (i=0;i<13;i++) | ||
906 | { | ||
907 | int j; | ||
908 | for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++) | ||
909 | { | ||
910 | opus_val32 L, R, M, S; | ||
911 | /* We cast to 32-bit first because of the -32768 case */ | ||
912 | L = EXTEND32(X[j]); | ||
913 | R = EXTEND32(X[N0+j]); | ||
914 | M = ADD32(L, R); | ||
915 | S = SUB32(L, R); | ||
916 | sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R))); | ||
917 | sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S))); | ||
918 | } | ||
919 | } | ||
920 | sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS); | ||
921 | thetas = 13; | ||
922 | /* We don't need thetas for lower bands with LM<=1 */ | ||
923 | if (LM<=1) | ||
924 | thetas -= 8; | ||
925 | return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS) | ||
926 | > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR); | ||
927 | } | ||
928 | |||
929 | int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc) | ||
930 | { | ||
931 | int i, c, N; | ||
932 | opus_int32 bits; | ||
933 | ec_enc _enc; | ||
934 | VARDECL(celt_sig, in); | ||
935 | VARDECL(celt_sig, freq); | ||
936 | VARDECL(celt_norm, X); | ||
937 | VARDECL(celt_ener, bandE); | ||
938 | VARDECL(opus_val16, bandLogE); | ||
939 | VARDECL(int, fine_quant); | ||
940 | VARDECL(opus_val16, error); | ||
941 | VARDECL(int, pulses); | ||
942 | VARDECL(int, cap); | ||
943 | VARDECL(int, offsets); | ||
944 | VARDECL(int, fine_priority); | ||
945 | VARDECL(int, tf_res); | ||
946 | VARDECL(unsigned char, collapse_masks); | ||
947 | celt_sig *prefilter_mem; | ||
948 | opus_val16 *oldBandE, *oldLogE, *oldLogE2; | ||
949 | int shortBlocks=0; | ||
950 | int isTransient=0; | ||
951 | const int CC = st->channels; | ||
952 | const int C = st->stream_channels; | ||
953 | int LM, M; | ||
954 | int tf_select; | ||
955 | int nbFilledBytes, nbAvailableBytes; | ||
956 | int effEnd; | ||
957 | int codedBands; | ||
958 | int tf_sum; | ||
959 | int alloc_trim; | ||
960 | int pitch_index=COMBFILTER_MINPERIOD; | ||
961 | opus_val16 gain1 = 0; | ||
962 | int intensity=0; | ||
963 | int dual_stereo=0; | ||
964 | int effectiveBytes; | ||
965 | opus_val16 pf_threshold; | ||
966 | int dynalloc_logp; | ||
967 | opus_int32 vbr_rate; | ||
968 | opus_int32 total_bits; | ||
969 | opus_int32 total_boost; | ||
970 | opus_int32 balance; | ||
971 | opus_int32 tell; | ||
972 | int prefilter_tapset=0; | ||
973 | int pf_on; | ||
974 | int anti_collapse_rsv; | ||
975 | int anti_collapse_on=0; | ||
976 | int silence=0; | ||
977 | ALLOC_STACK; | ||
978 | |||
979 | if (nbCompressedBytes<2 || pcm==NULL) | ||
980 | return OPUS_BAD_ARG; | ||
981 | |||
982 | frame_size *= st->upsample; | ||
983 | for (LM=0;LM<=st->mode->maxLM;LM++) | ||
984 | if (st->mode->shortMdctSize<<LM==frame_size) | ||
985 | break; | ||
986 | if (LM>st->mode->maxLM) | ||
987 | return OPUS_BAD_ARG; | ||
988 | M=1<<LM; | ||
989 | N = M*st->mode->shortMdctSize; | ||
990 | |||
991 | prefilter_mem = st->in_mem+CC*(st->overlap); | ||
992 | oldBandE = (opus_val16*)(st->in_mem+CC*(2*st->overlap+COMBFILTER_MAXPERIOD)); | ||
993 | oldLogE = oldBandE + CC*st->mode->nbEBands; | ||
994 | oldLogE2 = oldLogE + CC*st->mode->nbEBands; | ||
995 | |||
996 | if (enc==NULL) | ||
997 | { | ||
998 | tell=1; | ||
999 | nbFilledBytes=0; | ||
1000 | } else { | ||
1001 | tell=ec_tell(enc); | ||
1002 | nbFilledBytes=(tell+4)>>3; | ||
1003 | } | ||
1004 | |||
1005 | #ifdef CUSTOM_MODES | ||
1006 | if (st->signalling && enc==NULL) | ||
1007 | { | ||
1008 | int tmp = (st->mode->effEBands-st->end)>>1; | ||
1009 | st->end = IMAX(1, st->mode->effEBands-tmp); | ||
1010 | compressed[0] = tmp<<5; | ||
1011 | compressed[0] |= LM<<3; | ||
1012 | compressed[0] |= (C==2)<<2; | ||
1013 | /* Convert "standard mode" to Opus header */ | ||
1014 | if (st->mode->Fs==48000 && st->mode->shortMdctSize==120) | ||
1015 | { | ||
1016 | int c0 = toOpus(compressed[0]); | ||
1017 | if (c0<0) | ||
1018 | return OPUS_BAD_ARG; | ||
1019 | compressed[0] = c0; | ||
1020 | } | ||
1021 | compressed++; | ||
1022 | nbCompressedBytes--; | ||
1023 | } | ||
1024 | #else | ||
1025 | celt_assert(st->signalling==0); | ||
1026 | #endif | ||
1027 | |||
1028 | /* Can't produce more than 1275 output bytes */ | ||
1029 | nbCompressedBytes = IMIN(nbCompressedBytes,1275); | ||
1030 | nbAvailableBytes = nbCompressedBytes - nbFilledBytes; | ||
1031 | |||
1032 | if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX) | ||
1033 | { | ||
1034 | opus_int32 den=st->mode->Fs>>BITRES; | ||
1035 | vbr_rate=(st->bitrate*frame_size+(den>>1))/den; | ||
1036 | #ifdef CUSTOM_MODES | ||
1037 | if (st->signalling) | ||
1038 | vbr_rate -= 8<<BITRES; | ||
1039 | #endif | ||
1040 | effectiveBytes = vbr_rate>>(3+BITRES); | ||
1041 | } else { | ||
1042 | opus_int32 tmp; | ||
1043 | vbr_rate = 0; | ||
1044 | tmp = st->bitrate*frame_size; | ||
1045 | if (tell>1) | ||
1046 | tmp += tell; | ||
1047 | if (st->bitrate!=OPUS_BITRATE_MAX) | ||
1048 | nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes, | ||
1049 | (tmp+4*st->mode->Fs)/(8*st->mode->Fs)-!!st->signalling)); | ||
1050 | effectiveBytes = nbCompressedBytes; | ||
1051 | } | ||
1052 | |||
1053 | if (enc==NULL) | ||
1054 | { | ||
1055 | ec_enc_init(&_enc, compressed, nbCompressedBytes); | ||
1056 | enc = &_enc; | ||
1057 | } | ||
1058 | |||
1059 | if (vbr_rate>0) | ||
1060 | { | ||
1061 | /* Computes the max bit-rate allowed in VBR mode to avoid violating the | ||
1062 | target rate and buffering. | ||
1063 | We must do this up front so that bust-prevention logic triggers | ||
1064 | correctly if we don't have enough bits. */ | ||
1065 | if (st->constrained_vbr) | ||
1066 | { | ||
1067 | opus_int32 vbr_bound; | ||
1068 | opus_int32 max_allowed; | ||
1069 | /* We could use any multiple of vbr_rate as bound (depending on the | ||
1070 | delay). | ||
1071 | This is clamped to ensure we use at least two bytes if the encoder | ||
1072 | was entirely empty, but to allow 0 in hybrid mode. */ | ||
1073 | vbr_bound = vbr_rate; | ||
1074 | max_allowed = IMIN(IMAX(tell==1?2:0, | ||
1075 | (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)), | ||
1076 | nbAvailableBytes); | ||
1077 | if(max_allowed < nbAvailableBytes) | ||
1078 | { | ||
1079 | nbCompressedBytes = nbFilledBytes+max_allowed; | ||
1080 | nbAvailableBytes = max_allowed; | ||
1081 | ec_enc_shrink(enc, nbCompressedBytes); | ||
1082 | } | ||
1083 | } | ||
1084 | } | ||
1085 | total_bits = nbCompressedBytes*8; | ||
1086 | |||
1087 | effEnd = st->end; | ||
1088 | if (effEnd > st->mode->effEBands) | ||
1089 | effEnd = st->mode->effEBands; | ||
1090 | |||
1091 | ALLOC(in, CC*(N+st->overlap), celt_sig); | ||
1092 | |||
1093 | /* Find pitch period and gain */ | ||
1094 | { | ||
1095 | VARDECL(celt_sig, _pre); | ||
1096 | celt_sig *pre[2]; | ||
1097 | SAVE_STACK; | ||
1098 | ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig); | ||
1099 | |||
1100 | pre[0] = _pre; | ||
1101 | pre[1] = _pre + (N+COMBFILTER_MAXPERIOD); | ||
1102 | |||
1103 | silence = 1; | ||
1104 | c=0; do { | ||
1105 | int count = 0; | ||
1106 | const opus_val16 * OPUS_RESTRICT pcmp = pcm+c; | ||
1107 | celt_sig * OPUS_RESTRICT inp = in+c*(N+st->overlap)+st->overlap; | ||
1108 | |||
1109 | for (i=0;i<N;i++) | ||
1110 | { | ||
1111 | celt_sig x, tmp; | ||
1112 | |||
1113 | x = SCALEIN(*pcmp); | ||
1114 | #ifndef FIXED_POINT | ||
1115 | if (!(x==x)) | ||
1116 | x = 0; | ||
1117 | if (st->clip) | ||
1118 | x = MAX32(-65536.f, MIN32(65536.f,x)); | ||
1119 | #endif | ||
1120 | if (++count==st->upsample) | ||
1121 | { | ||
1122 | count=0; | ||
1123 | pcmp+=CC; | ||
1124 | } else { | ||
1125 | x = 0; | ||
1126 | } | ||
1127 | /* Apply pre-emphasis */ | ||
1128 | tmp = MULT16_16(st->mode->preemph[2], x); | ||
1129 | *inp = tmp + st->preemph_memE[c]; | ||
1130 | st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp) | ||
1131 | - MULT16_32_Q15(st->mode->preemph[0], tmp); | ||
1132 | silence = silence && *inp == 0; | ||
1133 | inp++; | ||
1134 | } | ||
1135 | OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD); | ||
1136 | OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N); | ||
1137 | } while (++c<CC); | ||
1138 | |||
1139 | #ifdef FUZZING | ||
1140 | if ((rand()&0x3F)==0) | ||
1141 | silence = 1; | ||
1142 | #endif | ||
1143 | if (tell==1) | ||
1144 | ec_enc_bit_logp(enc, silence, 15); | ||
1145 | else | ||
1146 | silence=0; | ||
1147 | if (silence) | ||
1148 | { | ||
1149 | /*In VBR mode there is no need to send more than the minimum. */ | ||
1150 | if (vbr_rate>0) | ||
1151 | { | ||
1152 | effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2); | ||
1153 | total_bits=nbCompressedBytes*8; | ||
1154 | nbAvailableBytes=2; | ||
1155 | ec_enc_shrink(enc, nbCompressedBytes); | ||
1156 | } | ||
1157 | /* Pretend we've filled all the remaining bits with zeros | ||
1158 | (that's what the initialiser did anyway) */ | ||
1159 | tell = nbCompressedBytes*8; | ||
1160 | enc->nbits_total+=tell-ec_tell(enc); | ||
1161 | } | ||
1162 | if (nbAvailableBytes>12*C && st->start==0 && !silence && !st->disable_pf && st->complexity >= 5) | ||
1163 | { | ||
1164 | VARDECL(opus_val16, pitch_buf); | ||
1165 | ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16); | ||
1166 | |||
1167 | pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC); | ||
1168 | pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N, | ||
1169 | COMBFILTER_MAXPERIOD-COMBFILTER_MINPERIOD, &pitch_index); | ||
1170 | pitch_index = COMBFILTER_MAXPERIOD-pitch_index; | ||
1171 | |||
1172 | gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD, | ||
1173 | N, &pitch_index, st->prefilter_period, st->prefilter_gain); | ||
1174 | if (pitch_index > COMBFILTER_MAXPERIOD-2) | ||
1175 | pitch_index = COMBFILTER_MAXPERIOD-2; | ||
1176 | gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1); | ||
1177 | if (st->loss_rate>2) | ||
1178 | gain1 = HALF32(gain1); | ||
1179 | if (st->loss_rate>4) | ||
1180 | gain1 = HALF32(gain1); | ||
1181 | if (st->loss_rate>8) | ||
1182 | gain1 = 0; | ||
1183 | prefilter_tapset = st->tapset_decision; | ||
1184 | } else { | ||
1185 | gain1 = 0; | ||
1186 | } | ||
1187 | |||
1188 | /* Gain threshold for enabling the prefilter/postfilter */ | ||
1189 | pf_threshold = QCONST16(.2f,15); | ||
1190 | |||
1191 | /* Adjusting the threshold based on rate and continuity */ | ||
1192 | if (abs(pitch_index-st->prefilter_period)*10>pitch_index) | ||
1193 | pf_threshold += QCONST16(.2f,15); | ||
1194 | if (nbAvailableBytes<25) | ||
1195 | pf_threshold += QCONST16(.1f,15); | ||
1196 | if (nbAvailableBytes<35) | ||
1197 | pf_threshold += QCONST16(.1f,15); | ||
1198 | if (st->prefilter_gain > QCONST16(.4f,15)) | ||
1199 | pf_threshold -= QCONST16(.1f,15); | ||
1200 | if (st->prefilter_gain > QCONST16(.55f,15)) | ||
1201 | pf_threshold -= QCONST16(.1f,15); | ||
1202 | |||
1203 | /* Hard threshold at 0.2 */ | ||
1204 | pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15)); | ||
1205 | if (gain1<pf_threshold) | ||
1206 | { | ||
1207 | if(st->start==0 && tell+16<=total_bits) | ||
1208 | ec_enc_bit_logp(enc, 0, 1); | ||
1209 | gain1 = 0; | ||
1210 | pf_on = 0; | ||
1211 | } else { | ||
1212 | /*This block is not gated by a total bits check only because | ||
1213 | of the nbAvailableBytes check above.*/ | ||
1214 | int qg; | ||
1215 | int octave; | ||
1216 | |||
1217 | if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15)) | ||
1218 | gain1=st->prefilter_gain; | ||
1219 | |||
1220 | #ifdef FIXED_POINT | ||
1221 | qg = ((gain1+1536)>>10)/3-1; | ||
1222 | #else | ||
1223 | qg = (int)floor(.5f+gain1*32/3)-1; | ||
1224 | #endif | ||
1225 | qg = IMAX(0, IMIN(7, qg)); | ||
1226 | ec_enc_bit_logp(enc, 1, 1); | ||
1227 | pitch_index += 1; | ||
1228 | octave = EC_ILOG(pitch_index)-5; | ||
1229 | ec_enc_uint(enc, octave, 6); | ||
1230 | ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave); | ||
1231 | pitch_index -= 1; | ||
1232 | ec_enc_bits(enc, qg, 3); | ||
1233 | if (ec_tell(enc)+2<=total_bits) | ||
1234 | ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2); | ||
1235 | else | ||
1236 | prefilter_tapset = 0; | ||
1237 | gain1 = QCONST16(0.09375f,15)*(qg+1); | ||
1238 | pf_on = 1; | ||
1239 | } | ||
1240 | /*printf("%d %f\n", pitch_index, gain1);*/ | ||
1241 | |||
1242 | c=0; do { | ||
1243 | int offset = st->mode->shortMdctSize-st->mode->overlap; | ||
1244 | st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD); | ||
1245 | OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap); | ||
1246 | if (offset) | ||
1247 | comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD, | ||
1248 | st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain, | ||
1249 | st->prefilter_tapset, st->prefilter_tapset, NULL, 0); | ||
1250 | |||
1251 | comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset, | ||
1252 | st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1, | ||
1253 | st->prefilter_tapset, prefilter_tapset, st->mode->window, st->mode->overlap); | ||
1254 | OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap); | ||
1255 | |||
1256 | if (N>COMBFILTER_MAXPERIOD) | ||
1257 | { | ||
1258 | OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD); | ||
1259 | } else { | ||
1260 | OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N); | ||
1261 | OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N); | ||
1262 | } | ||
1263 | } while (++c<CC); | ||
1264 | |||
1265 | RESTORE_STACK; | ||
1266 | } | ||
1267 | |||
1268 | isTransient = 0; | ||
1269 | shortBlocks = 0; | ||
1270 | if (LM>0 && ec_tell(enc)+3<=total_bits) | ||
1271 | { | ||
1272 | if (st->complexity > 1) | ||
1273 | { | ||
1274 | isTransient = transient_analysis(in, N+st->overlap, CC, | ||
1275 | st->overlap); | ||
1276 | if (isTransient) | ||
1277 | shortBlocks = M; | ||
1278 | } | ||
1279 | ec_enc_bit_logp(enc, isTransient, 3); | ||
1280 | } | ||
1281 | |||
1282 | ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */ | ||
1283 | ALLOC(bandE,st->mode->nbEBands*CC, celt_ener); | ||
1284 | ALLOC(bandLogE,st->mode->nbEBands*CC, opus_val16); | ||
1285 | /* Compute MDCTs */ | ||
1286 | compute_mdcts(st->mode, shortBlocks, in, freq, CC, LM); | ||
1287 | |||
1288 | if (CC==2&&C==1) | ||
1289 | { | ||
1290 | for (i=0;i<N;i++) | ||
1291 | freq[i] = ADD32(HALF32(freq[i]), HALF32(freq[N+i])); | ||
1292 | } | ||
1293 | if (st->upsample != 1) | ||
1294 | { | ||
1295 | c=0; do | ||
1296 | { | ||
1297 | int bound = N/st->upsample; | ||
1298 | for (i=0;i<bound;i++) | ||
1299 | freq[c*N+i] *= st->upsample; | ||
1300 | for (;i<N;i++) | ||
1301 | freq[c*N+i] = 0; | ||
1302 | } while (++c<C); | ||
1303 | } | ||
1304 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ | ||
1305 | |||
1306 | compute_band_energies(st->mode, freq, bandE, effEnd, C, M); | ||
1307 | |||
1308 | amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C); | ||
1309 | |||
1310 | /* Band normalisation */ | ||
1311 | normalise_bands(st->mode, freq, X, bandE, effEnd, C, M); | ||
1312 | |||
1313 | ALLOC(tf_res, st->mode->nbEBands, int); | ||
1314 | tf_select = tf_analysis(st->mode, effEnd, C, isTransient, tf_res, effectiveBytes, X, N, LM, &tf_sum); | ||
1315 | for (i=effEnd;i<st->end;i++) | ||
1316 | tf_res[i] = tf_res[effEnd-1]; | ||
1317 | |||
1318 | ALLOC(error, C*st->mode->nbEBands, opus_val16); | ||
1319 | quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE, | ||
1320 | oldBandE, total_bits, error, enc, | ||
1321 | C, LM, nbAvailableBytes, st->force_intra, | ||
1322 | &st->delayedIntra, st->complexity >= 4, st->loss_rate); | ||
1323 | |||
1324 | tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc); | ||
1325 | |||
1326 | st->spread_decision = SPREAD_NORMAL; | ||
1327 | if (ec_tell(enc)+4<=total_bits) | ||
1328 | { | ||
1329 | if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C) | ||
1330 | { | ||
1331 | if (st->complexity == 0) | ||
1332 | st->spread_decision = SPREAD_NONE; | ||
1333 | } else { | ||
1334 | st->spread_decision = spreading_decision(st->mode, X, | ||
1335 | &st->tonal_average, st->spread_decision, &st->hf_average, | ||
1336 | &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M); | ||
1337 | } | ||
1338 | ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5); | ||
1339 | } | ||
1340 | |||
1341 | ALLOC(cap, st->mode->nbEBands, int); | ||
1342 | ALLOC(offsets, st->mode->nbEBands, int); | ||
1343 | |||
1344 | init_caps(st->mode,cap,LM,C); | ||
1345 | for (i=0;i<st->mode->nbEBands;i++) | ||
1346 | offsets[i] = 0; | ||
1347 | /* Dynamic allocation code */ | ||
1348 | /* Make sure that dynamic allocation can't make us bust the budget */ | ||
1349 | if (effectiveBytes > 50 && LM>=1) | ||
1350 | { | ||
1351 | int t1, t2; | ||
1352 | if (LM <= 1) | ||
1353 | { | ||
1354 | t1 = 3; | ||
1355 | t2 = 5; | ||
1356 | } else { | ||
1357 | t1 = 2; | ||
1358 | t2 = 4; | ||
1359 | } | ||
1360 | for (i=st->start+1;i<st->end-1;i++) | ||
1361 | { | ||
1362 | opus_val32 d2; | ||
1363 | d2 = 2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1]; | ||
1364 | if (C==2) | ||
1365 | d2 = HALF32(d2 + 2*bandLogE[i+st->mode->nbEBands]- | ||
1366 | bandLogE[i-1+st->mode->nbEBands]-bandLogE[i+1+st->mode->nbEBands]); | ||
1367 | #ifdef FUZZING | ||
1368 | if((rand()&0xF)==0) | ||
1369 | { | ||
1370 | offsets[i] += 1; | ||
1371 | if((rand()&0x3)==0) | ||
1372 | offsets[i] += 1+(rand()&0x3); | ||
1373 | } | ||
1374 | #else | ||
1375 | if (d2 > SHL16(t1,DB_SHIFT)) | ||
1376 | offsets[i] += 1; | ||
1377 | if (d2 > SHL16(t2,DB_SHIFT)) | ||
1378 | offsets[i] += 1; | ||
1379 | #endif | ||
1380 | } | ||
1381 | } | ||
1382 | dynalloc_logp = 6; | ||
1383 | total_bits<<=BITRES; | ||
1384 | total_boost = 0; | ||
1385 | tell = ec_tell_frac(enc); | ||
1386 | for (i=st->start;i<st->end;i++) | ||
1387 | { | ||
1388 | int width, quanta; | ||
1389 | int dynalloc_loop_logp; | ||
1390 | int boost; | ||
1391 | int j; | ||
1392 | width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM; | ||
1393 | /* quanta is 6 bits, but no more than 1 bit/sample | ||
1394 | and no less than 1/8 bit/sample */ | ||
1395 | quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); | ||
1396 | dynalloc_loop_logp = dynalloc_logp; | ||
1397 | boost = 0; | ||
1398 | for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost | ||
1399 | && boost < cap[i]; j++) | ||
1400 | { | ||
1401 | int flag; | ||
1402 | flag = j<offsets[i]; | ||
1403 | ec_enc_bit_logp(enc, flag, dynalloc_loop_logp); | ||
1404 | tell = ec_tell_frac(enc); | ||
1405 | if (!flag) | ||
1406 | break; | ||
1407 | boost += quanta; | ||
1408 | total_boost += quanta; | ||
1409 | dynalloc_loop_logp = 1; | ||
1410 | } | ||
1411 | /* Making dynalloc more likely */ | ||
1412 | if (j) | ||
1413 | dynalloc_logp = IMAX(2, dynalloc_logp-1); | ||
1414 | offsets[i] = boost; | ||
1415 | } | ||
1416 | alloc_trim = 5; | ||
1417 | if (tell+(6<<BITRES) <= total_bits - total_boost) | ||
1418 | { | ||
1419 | alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE, | ||
1420 | st->end, LM, C, N); | ||
1421 | ec_enc_icdf(enc, alloc_trim, trim_icdf, 7); | ||
1422 | tell = ec_tell_frac(enc); | ||
1423 | } | ||
1424 | |||
1425 | /* Variable bitrate */ | ||
1426 | if (vbr_rate>0) | ||
1427 | { | ||
1428 | opus_val16 alpha; | ||
1429 | opus_int32 delta; | ||
1430 | /* The target rate in 8th bits per frame */ | ||
1431 | opus_int32 target; | ||
1432 | opus_int32 min_allowed; | ||
1433 | int lm_diff = st->mode->maxLM - LM; | ||
1434 | |||
1435 | /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms. | ||
1436 | The CELT allocator will just not be able to use more than that anyway. */ | ||
1437 | nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM)); | ||
1438 | target = vbr_rate + (st->vbr_offset>>lm_diff) - ((40*C+20)<<BITRES); | ||
1439 | |||
1440 | /* Shortblocks get a large boost in bitrate, but since they | ||
1441 | are uncommon long blocks are not greatly affected */ | ||
1442 | if (shortBlocks || tf_sum < -2*(st->end-st->start)) | ||
1443 | target = 7*target/4; | ||
1444 | else if (tf_sum < -(st->end-st->start)) | ||
1445 | target = 3*target/2; | ||
1446 | else if (M > 1) | ||
1447 | target-=(target+14)/28; | ||
1448 | |||
1449 | /* The current offset is removed from the target and the space used | ||
1450 | so far is added*/ | ||
1451 | target=target+tell; | ||
1452 | |||
1453 | /* In VBR mode the frame size must not be reduced so much that it would | ||
1454 | result in the encoder running out of bits. | ||
1455 | The margin of 2 bytes ensures that none of the bust-prevention logic | ||
1456 | in the decoder will have triggered so far. */ | ||
1457 | min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes; | ||
1458 | |||
1459 | nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3); | ||
1460 | nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes); | ||
1461 | nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes; | ||
1462 | |||
1463 | /* By how much did we "miss" the target on that frame */ | ||
1464 | delta = target - vbr_rate; | ||
1465 | |||
1466 | target=nbAvailableBytes<<(BITRES+3); | ||
1467 | |||
1468 | /*If the frame is silent we don't adjust our drift, otherwise | ||
1469 | the encoder will shoot to very high rates after hitting a | ||
1470 | span of silence, but we do allow the bitres to refill. | ||
1471 | This means that we'll undershoot our target in CVBR/VBR modes | ||
1472 | on files with lots of silence. */ | ||
1473 | if(silence) | ||
1474 | { | ||
1475 | nbAvailableBytes = 2; | ||
1476 | target = 2*8<<BITRES; | ||
1477 | delta = 0; | ||
1478 | } | ||
1479 | |||
1480 | if (st->vbr_count < 970) | ||
1481 | { | ||
1482 | st->vbr_count++; | ||
1483 | alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16)); | ||
1484 | } else | ||
1485 | alpha = QCONST16(.001f,15); | ||
1486 | /* How many bits have we used in excess of what we're allowed */ | ||
1487 | if (st->constrained_vbr) | ||
1488 | st->vbr_reservoir += target - vbr_rate; | ||
1489 | /*printf ("%d\n", st->vbr_reservoir);*/ | ||
1490 | |||
1491 | /* Compute the offset we need to apply in order to reach the target */ | ||
1492 | st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift); | ||
1493 | st->vbr_offset = -st->vbr_drift; | ||
1494 | /*printf ("%d\n", st->vbr_drift);*/ | ||
1495 | |||
1496 | if (st->constrained_vbr && st->vbr_reservoir < 0) | ||
1497 | { | ||
1498 | /* We're under the min value -- increase rate */ | ||
1499 | int adjust = (-st->vbr_reservoir)/(8<<BITRES); | ||
1500 | /* Unless we're just coding silence */ | ||
1501 | nbAvailableBytes += silence?0:adjust; | ||
1502 | st->vbr_reservoir = 0; | ||
1503 | /*printf ("+%d\n", adjust);*/ | ||
1504 | } | ||
1505 | nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes); | ||
1506 | /* This moves the raw bits to take into account the new compressed size */ | ||
1507 | ec_enc_shrink(enc, nbCompressedBytes); | ||
1508 | } | ||
1509 | if (C==2) | ||
1510 | { | ||
1511 | int effectiveRate; | ||
1512 | |||
1513 | /* Always use MS for 2.5 ms frames until we can do a better analysis */ | ||
1514 | if (LM!=0) | ||
1515 | dual_stereo = stereo_analysis(st->mode, X, LM, N); | ||
1516 | |||
1517 | /* Account for coarse energy */ | ||
1518 | effectiveRate = (8*effectiveBytes - 80)>>LM; | ||
1519 | |||
1520 | /* effectiveRate in kb/s */ | ||
1521 | effectiveRate = 2*effectiveRate/5; | ||
1522 | if (effectiveRate<35) | ||
1523 | intensity = 8; | ||
1524 | else if (effectiveRate<50) | ||
1525 | intensity = 12; | ||
1526 | else if (effectiveRate<68) | ||
1527 | intensity = 16; | ||
1528 | else if (effectiveRate<84) | ||
1529 | intensity = 18; | ||
1530 | else if (effectiveRate<102) | ||
1531 | intensity = 19; | ||
1532 | else if (effectiveRate<130) | ||
1533 | intensity = 20; | ||
1534 | else | ||
1535 | intensity = 100; | ||
1536 | intensity = IMIN(st->end,IMAX(st->start, intensity)); | ||
1537 | } | ||
1538 | |||
1539 | /* Bit allocation */ | ||
1540 | ALLOC(fine_quant, st->mode->nbEBands, int); | ||
1541 | ALLOC(pulses, st->mode->nbEBands, int); | ||
1542 | ALLOC(fine_priority, st->mode->nbEBands, int); | ||
1543 | |||
1544 | /* bits = packet size - where we are - safety*/ | ||
1545 | bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1; | ||
1546 | anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0; | ||
1547 | bits -= anti_collapse_rsv; | ||
1548 | codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap, | ||
1549 | alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses, | ||
1550 | fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands); | ||
1551 | st->lastCodedBands = codedBands; | ||
1552 | |||
1553 | quant_fine_energy(st->mode, st->start, st->end, oldBandE, error, fine_quant, enc, C); | ||
1554 | |||
1555 | #ifdef MEASURE_NORM_MSE | ||
1556 | float X0[3000]; | ||
1557 | float bandE0[60]; | ||
1558 | c=0; do | ||
1559 | for (i=0;i<N;i++) | ||
1560 | X0[i+c*N] = X[i+c*N]; | ||
1561 | while (++c<C); | ||
1562 | for (i=0;i<C*st->mode->nbEBands;i++) | ||
1563 | bandE0[i] = bandE[i]; | ||
1564 | #endif | ||
1565 | |||
1566 | /* Residual quantisation */ | ||
1567 | ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char); | ||
1568 | quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks, | ||
1569 | bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, intensity, tf_res, | ||
1570 | nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, codedBands, &st->rng); | ||
1571 | |||
1572 | if (anti_collapse_rsv > 0) | ||
1573 | { | ||
1574 | anti_collapse_on = st->consec_transient<2; | ||
1575 | #ifdef FUZZING | ||
1576 | anti_collapse_on = rand()&0x1; | ||
1577 | #endif | ||
1578 | ec_enc_bits(enc, anti_collapse_on, 1); | ||
1579 | } | ||
1580 | quant_energy_finalise(st->mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C); | ||
1581 | |||
1582 | if (silence) | ||
1583 | { | ||
1584 | for (i=0;i<C*st->mode->nbEBands;i++) | ||
1585 | oldBandE[i] = -QCONST16(28.f,DB_SHIFT); | ||
1586 | } | ||
1587 | |||
1588 | #ifdef RESYNTH | ||
1589 | /* Re-synthesis of the coded audio if required */ | ||
1590 | { | ||
1591 | celt_sig *out_mem[2]; | ||
1592 | celt_sig *overlap_mem[2]; | ||
1593 | |||
1594 | log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C); | ||
1595 | if (silence) | ||
1596 | { | ||
1597 | for (i=0;i<C*st->mode->nbEBands;i++) | ||
1598 | bandE[i] = 0; | ||
1599 | } | ||
1600 | |||
1601 | #ifdef MEASURE_NORM_MSE | ||
1602 | measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C); | ||
1603 | #endif | ||
1604 | if (anti_collapse_on) | ||
1605 | { | ||
1606 | anti_collapse(st->mode, X, collapse_masks, LM, C, N, | ||
1607 | st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng); | ||
1608 | } | ||
1609 | |||
1610 | /* Synthesis */ | ||
1611 | denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M); | ||
1612 | |||
1613 | OPUS_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD); | ||
1614 | if (CC==2) | ||
1615 | OPUS_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD); | ||
1616 | |||
1617 | c=0; do | ||
1618 | for (i=0;i<M*st->mode->eBands[st->start];i++) | ||
1619 | freq[c*N+i] = 0; | ||
1620 | while (++c<C); | ||
1621 | c=0; do | ||
1622 | for (i=M*st->mode->eBands[st->end];i<N;i++) | ||
1623 | freq[c*N+i] = 0; | ||
1624 | while (++c<C); | ||
1625 | |||
1626 | if (CC==2&&C==1) | ||
1627 | { | ||
1628 | for (i=0;i<N;i++) | ||
1629 | freq[N+i] = freq[i]; | ||
1630 | } | ||
1631 | |||
1632 | out_mem[0] = st->syn_mem[0]+MAX_PERIOD; | ||
1633 | if (CC==2) | ||
1634 | out_mem[1] = st->syn_mem[1]+MAX_PERIOD; | ||
1635 | |||
1636 | overlap_mem[0] = prefilter_mem+CC*COMBFILTER_MAXPERIOD; | ||
1637 | if (CC==2) | ||
1638 | overlap_mem[1] = overlap_mem[0] + st->overlap; | ||
1639 | |||
1640 | compute_inv_mdcts(st->mode, shortBlocks, freq, out_mem, overlap_mem, CC, LM); | ||
1641 | |||
1642 | c=0; do { | ||
1643 | st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD); | ||
1644 | st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD); | ||
1645 | comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, st->mode->shortMdctSize, | ||
1646 | st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset, | ||
1647 | st->mode->window, st->overlap); | ||
1648 | if (LM!=0) | ||
1649 | comb_filter(out_mem[c]+st->mode->shortMdctSize, out_mem[c]+st->mode->shortMdctSize, st->prefilter_period, pitch_index, N-st->mode->shortMdctSize, | ||
1650 | st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset, | ||
1651 | st->mode->window, st->mode->overlap); | ||
1652 | } while (++c<CC); | ||
1653 | |||
1654 | deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, st->mode->preemph, st->preemph_memD); | ||
1655 | st->prefilter_period_old = st->prefilter_period; | ||
1656 | st->prefilter_gain_old = st->prefilter_gain; | ||
1657 | st->prefilter_tapset_old = st->prefilter_tapset; | ||
1658 | } | ||
1659 | #endif | ||
1660 | |||
1661 | st->prefilter_period = pitch_index; | ||
1662 | st->prefilter_gain = gain1; | ||
1663 | st->prefilter_tapset = prefilter_tapset; | ||
1664 | #ifdef RESYNTH | ||
1665 | if (LM!=0) | ||
1666 | { | ||
1667 | st->prefilter_period_old = st->prefilter_period; | ||
1668 | st->prefilter_gain_old = st->prefilter_gain; | ||
1669 | st->prefilter_tapset_old = st->prefilter_tapset; | ||
1670 | } | ||
1671 | #endif | ||
1672 | |||
1673 | if (CC==2&&C==1) { | ||
1674 | for (i=0;i<st->mode->nbEBands;i++) | ||
1675 | oldBandE[st->mode->nbEBands+i]=oldBandE[i]; | ||
1676 | } | ||
1677 | |||
1678 | if (!isTransient) | ||
1679 | { | ||
1680 | for (i=0;i<CC*st->mode->nbEBands;i++) | ||
1681 | oldLogE2[i] = oldLogE[i]; | ||
1682 | for (i=0;i<CC*st->mode->nbEBands;i++) | ||
1683 | oldLogE[i] = oldBandE[i]; | ||
1684 | } else { | ||
1685 | for (i=0;i<CC*st->mode->nbEBands;i++) | ||
1686 | oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]); | ||
1687 | } | ||
1688 | /* In case start or end were to change */ | ||
1689 | c=0; do | ||
1690 | { | ||
1691 | for (i=0;i<st->start;i++) | ||
1692 | { | ||
1693 | oldBandE[c*st->mode->nbEBands+i]=0; | ||
1694 | oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT); | ||
1695 | } | ||
1696 | for (i=st->end;i<st->mode->nbEBands;i++) | ||
1697 | { | ||
1698 | oldBandE[c*st->mode->nbEBands+i]=0; | ||
1699 | oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT); | ||
1700 | } | ||
1701 | } while (++c<CC); | ||
1702 | |||
1703 | if (isTransient) | ||
1704 | st->consec_transient++; | ||
1705 | else | ||
1706 | st->consec_transient=0; | ||
1707 | st->rng = enc->rng; | ||
1708 | |||
1709 | /* If there's any room left (can only happen for very high rates), | ||
1710 | it's already filled with zeros */ | ||
1711 | ec_enc_done(enc); | ||
1712 | |||
1713 | #ifdef CUSTOM_MODES | ||
1714 | if (st->signalling) | ||
1715 | nbCompressedBytes++; | ||
1716 | #endif | ||
1717 | |||
1718 | RESTORE_STACK; | ||
1719 | if (ec_get_error(enc)) | ||
1720 | return OPUS_INTERNAL_ERROR; | ||
1721 | else | ||
1722 | return nbCompressedBytes; | ||
1723 | } | ||
1724 | |||
1725 | |||
1726 | #ifdef CUSTOM_MODES | ||
1727 | |||
1728 | #ifdef FIXED_POINT | ||
1729 | int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) | ||
1730 | { | ||
1731 | return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL); | ||
1732 | } | ||
1733 | |||
1734 | #ifndef DISABLE_FLOAT_API | ||
1735 | int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) | ||
1736 | { | ||
1737 | int j, ret, C, N; | ||
1738 | VARDECL(opus_int16, in); | ||
1739 | ALLOC_STACK; | ||
1740 | |||
1741 | if (pcm==NULL) | ||
1742 | return OPUS_BAD_ARG; | ||
1743 | |||
1744 | C = st->channels; | ||
1745 | N = frame_size; | ||
1746 | ALLOC(in, C*N, opus_int16); | ||
1747 | |||
1748 | for (j=0;j<C*N;j++) | ||
1749 | in[j] = FLOAT2INT16(pcm[j]); | ||
1750 | |||
1751 | ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL); | ||
1752 | #ifdef RESYNTH | ||
1753 | for (j=0;j<C*N;j++) | ||
1754 | ((float*)pcm)[j]=in[j]*(1.f/32768.f); | ||
1755 | #endif | ||
1756 | RESTORE_STACK; | ||
1757 | return ret; | ||
1758 | } | ||
1759 | #endif /* DISABLE_FLOAT_API */ | ||
1760 | #else | ||
1761 | |||
1762 | int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) | ||
1763 | { | ||
1764 | int j, ret, C, N; | ||
1765 | VARDECL(celt_sig, in); | ||
1766 | ALLOC_STACK; | ||
1767 | |||
1768 | if (pcm==NULL) | ||
1769 | return OPUS_BAD_ARG; | ||
1770 | |||
1771 | C=st->channels; | ||
1772 | N=frame_size; | ||
1773 | ALLOC(in, C*N, celt_sig); | ||
1774 | for (j=0;j<C*N;j++) { | ||
1775 | in[j] = SCALEOUT(pcm[j]); | ||
1776 | } | ||
1777 | |||
1778 | ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL); | ||
1779 | #ifdef RESYNTH | ||
1780 | for (j=0;j<C*N;j++) | ||
1781 | ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]); | ||
1782 | #endif | ||
1783 | RESTORE_STACK; | ||
1784 | return ret; | ||
1785 | } | ||
1786 | |||
1787 | int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) | ||
1788 | { | ||
1789 | return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL); | ||
1790 | } | ||
1791 | |||
1792 | #endif | ||
1793 | |||
1794 | #endif /* CUSTOM_MODES */ | ||
1795 | |||
1796 | int opus_custom_encoder_ctl(CELTEncoder * OPUS_RESTRICT st, int request, ...) | ||
1797 | { | ||
1798 | va_list ap; | ||
1799 | |||
1800 | va_start(ap, request); | ||
1801 | switch (request) | ||
1802 | { | ||
1803 | case OPUS_SET_COMPLEXITY_REQUEST: | ||
1804 | { | ||
1805 | int value = va_arg(ap, opus_int32); | ||
1806 | if (value<0 || value>10) | ||
1807 | goto bad_arg; | ||
1808 | st->complexity = value; | ||
1809 | } | ||
1810 | break; | ||
1811 | case CELT_SET_START_BAND_REQUEST: | ||
1812 | { | ||
1813 | opus_int32 value = va_arg(ap, opus_int32); | ||
1814 | if (value<0 || value>=st->mode->nbEBands) | ||
1815 | goto bad_arg; | ||
1816 | st->start = value; | ||
1817 | } | ||
1818 | break; | ||
1819 | case CELT_SET_END_BAND_REQUEST: | ||
1820 | { | ||
1821 | opus_int32 value = va_arg(ap, opus_int32); | ||
1822 | if (value<1 || value>st->mode->nbEBands) | ||
1823 | goto bad_arg; | ||
1824 | st->end = value; | ||
1825 | } | ||
1826 | break; | ||
1827 | case CELT_SET_PREDICTION_REQUEST: | ||
1828 | { | ||
1829 | int value = va_arg(ap, opus_int32); | ||
1830 | if (value<0 || value>2) | ||
1831 | goto bad_arg; | ||
1832 | st->disable_pf = value<=1; | ||
1833 | st->force_intra = value==0; | ||
1834 | } | ||
1835 | break; | ||
1836 | case OPUS_SET_PACKET_LOSS_PERC_REQUEST: | ||
1837 | { | ||
1838 | int value = va_arg(ap, opus_int32); | ||
1839 | if (value<0 || value>100) | ||
1840 | goto bad_arg; | ||
1841 | st->loss_rate = value; | ||
1842 | } | ||
1843 | break; | ||
1844 | case OPUS_SET_VBR_CONSTRAINT_REQUEST: | ||
1845 | { | ||
1846 | opus_int32 value = va_arg(ap, opus_int32); | ||
1847 | st->constrained_vbr = value; | ||
1848 | } | ||
1849 | break; | ||
1850 | case OPUS_SET_VBR_REQUEST: | ||
1851 | { | ||
1852 | opus_int32 value = va_arg(ap, opus_int32); | ||
1853 | st->vbr = value; | ||
1854 | } | ||
1855 | break; | ||
1856 | case OPUS_SET_BITRATE_REQUEST: | ||
1857 | { | ||
1858 | opus_int32 value = va_arg(ap, opus_int32); | ||
1859 | if (value<=500 && value!=OPUS_BITRATE_MAX) | ||
1860 | goto bad_arg; | ||
1861 | value = IMIN(value, 260000*st->channels); | ||
1862 | st->bitrate = value; | ||
1863 | } | ||
1864 | break; | ||
1865 | case CELT_SET_CHANNELS_REQUEST: | ||
1866 | { | ||
1867 | opus_int32 value = va_arg(ap, opus_int32); | ||
1868 | if (value<1 || value>2) | ||
1869 | goto bad_arg; | ||
1870 | st->stream_channels = value; | ||
1871 | } | ||
1872 | break; | ||
1873 | case OPUS_SET_LSB_DEPTH_REQUEST: | ||
1874 | { | ||
1875 | opus_int32 value = va_arg(ap, opus_int32); | ||
1876 | if (value<8 || value>24) | ||
1877 | goto bad_arg; | ||
1878 | st->lsb_depth=value; | ||
1879 | } | ||
1880 | break; | ||
1881 | case OPUS_GET_LSB_DEPTH_REQUEST: | ||
1882 | { | ||
1883 | opus_int32 *value = va_arg(ap, opus_int32*); | ||
1884 | *value=st->lsb_depth; | ||
1885 | } | ||
1886 | break; | ||
1887 | case OPUS_RESET_STATE: | ||
1888 | { | ||
1889 | int i; | ||
1890 | opus_val16 *oldBandE, *oldLogE, *oldLogE2; | ||
1891 | oldBandE = (opus_val16*)(st->in_mem+st->channels*(2*st->overlap+COMBFILTER_MAXPERIOD)); | ||
1892 | oldLogE = oldBandE + st->channels*st->mode->nbEBands; | ||
1893 | oldLogE2 = oldLogE + st->channels*st->mode->nbEBands; | ||
1894 | OPUS_CLEAR((char*)&st->ENCODER_RESET_START, | ||
1895 | opus_custom_encoder_get_size(st->mode, st->channels)- | ||
1896 | ((char*)&st->ENCODER_RESET_START - (char*)st)); | ||
1897 | for (i=0;i<st->channels*st->mode->nbEBands;i++) | ||
1898 | oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT); | ||
1899 | st->vbr_offset = 0; | ||
1900 | st->delayedIntra = 1; | ||
1901 | st->spread_decision = SPREAD_NORMAL; | ||
1902 | st->tonal_average = 256; | ||
1903 | st->hf_average = 0; | ||
1904 | st->tapset_decision = 0; | ||
1905 | } | ||
1906 | break; | ||
1907 | #ifdef CUSTOM_MODES | ||
1908 | case CELT_SET_INPUT_CLIPPING_REQUEST: | ||
1909 | { | ||
1910 | opus_int32 value = va_arg(ap, opus_int32); | ||
1911 | st->clip = value; | ||
1912 | } | ||
1913 | break; | ||
1914 | #endif | ||
1915 | case CELT_SET_SIGNALLING_REQUEST: | ||
1916 | { | ||
1917 | opus_int32 value = va_arg(ap, opus_int32); | ||
1918 | st->signalling = value; | ||
1919 | } | ||
1920 | break; | ||
1921 | case CELT_GET_MODE_REQUEST: | ||
1922 | { | ||
1923 | const CELTMode ** value = va_arg(ap, const CELTMode**); | ||
1924 | if (value==0) | ||
1925 | goto bad_arg; | ||
1926 | *value=st->mode; | ||
1927 | } | ||
1928 | break; | ||
1929 | case OPUS_GET_FINAL_RANGE_REQUEST: | ||
1930 | { | ||
1931 | opus_uint32 * value = va_arg(ap, opus_uint32 *); | ||
1932 | if (value==0) | ||
1933 | goto bad_arg; | ||
1934 | *value=st->rng; | ||
1935 | } | ||
1936 | break; | ||
1937 | default: | ||
1938 | goto bad_request; | ||
1939 | } | ||
1940 | va_end(ap); | ||
1941 | return OPUS_OK; | ||
1942 | bad_arg: | ||
1943 | va_end(ap); | ||
1944 | return OPUS_BAD_ARG; | ||
1945 | bad_request: | ||
1946 | va_end(ap); | ||
1947 | return OPUS_UNIMPLEMENTED; | ||
1948 | } | ||
1949 | #endif | ||
1950 | |||
1951 | /**********************************************************************/ | ||
1952 | /* */ | ||
1953 | /* DECODER */ | ||
1954 | /* */ | ||
1955 | /**********************************************************************/ | ||
1956 | #define DECODE_BUFFER_SIZE 2048 | ||
1957 | |||
1958 | /** Decoder state | ||
1959 | @brief Decoder state | ||
1960 | */ | ||
1961 | struct OpusCustomDecoder { | ||
1962 | const OpusCustomMode *mode; | ||
1963 | int overlap; | ||
1964 | int channels; | ||
1965 | int stream_channels; | ||
1966 | |||
1967 | int downsample; | ||
1968 | int start, end; | ||
1969 | int signalling; | ||
1970 | |||
1971 | /* Everything beyond this point gets cleared on a reset */ | ||
1972 | #define DECODER_RESET_START rng | ||
1973 | |||
1974 | opus_uint32 rng; | ||
1975 | int error; | ||
1976 | int last_pitch_index; | ||
1977 | int loss_count; | ||
1978 | int postfilter_period; | ||
1979 | int postfilter_period_old; | ||
1980 | opus_val16 postfilter_gain; | ||
1981 | opus_val16 postfilter_gain_old; | ||
1982 | int postfilter_tapset; | ||
1983 | int postfilter_tapset_old; | ||
1984 | |||
1985 | celt_sig preemph_memD[2]; | ||
1986 | |||
1987 | celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */ | ||
1988 | /* opus_val16 lpc[], Size = channels*LPC_ORDER */ | ||
1989 | /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */ | ||
1990 | /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */ | ||
1991 | /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */ | ||
1992 | /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */ | ||
1993 | }; | ||
1994 | |||
1995 | int celt_decoder_get_size(int channels) | ||
1996 | { | ||
1997 | const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL); | ||
1998 | return opus_custom_decoder_get_size(mode, channels); | ||
1999 | } | ||
2000 | |||
2001 | OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels) | ||
2002 | { | ||
2003 | int size = sizeof(struct CELTDecoder) | ||
2004 | + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig) | ||
2005 | + channels*LPC_ORDER*sizeof(opus_val16) | ||
2006 | + 4*2*mode->nbEBands*sizeof(opus_val16); | ||
2007 | return size; | ||
2008 | } | ||
2009 | |||
2010 | #ifdef CUSTOM_MODES | ||
2011 | CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error) | ||
2012 | { | ||
2013 | int ret; | ||
2014 | CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels)); | ||
2015 | ret = opus_custom_decoder_init(st, mode, channels); | ||
2016 | if (ret != OPUS_OK) | ||
2017 | { | ||
2018 | opus_custom_decoder_destroy(st); | ||
2019 | st = NULL; | ||
2020 | } | ||
2021 | if (error) | ||
2022 | *error = ret; | ||
2023 | return st; | ||
2024 | } | ||
2025 | #endif /* CUSTOM_MODES */ | ||
2026 | |||
2027 | int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels) | ||
2028 | { | ||
2029 | int ret; | ||
2030 | ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels); | ||
2031 | if (ret != OPUS_OK) | ||
2032 | return ret; | ||
2033 | st->downsample = resampling_factor(sampling_rate); | ||
2034 | if (st->downsample==0) | ||
2035 | return OPUS_BAD_ARG; | ||
2036 | else | ||
2037 | return OPUS_OK; | ||
2038 | } | ||
2039 | |||
2040 | OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels) | ||
2041 | { | ||
2042 | if (channels < 0 || channels > 2) | ||
2043 | return OPUS_BAD_ARG; | ||
2044 | |||
2045 | if (st==NULL) | ||
2046 | return OPUS_ALLOC_FAIL; | ||
2047 | |||
2048 | OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels)); | ||
2049 | |||
2050 | st->mode = mode; | ||
2051 | st->overlap = mode->overlap; | ||
2052 | st->stream_channels = st->channels = channels; | ||
2053 | |||
2054 | st->downsample = 1; | ||
2055 | st->start = 0; | ||
2056 | st->end = st->mode->effEBands; | ||
2057 | st->signalling = 1; | ||
2058 | |||
2059 | st->loss_count = 0; | ||
2060 | |||
2061 | opus_custom_decoder_ctl(st, OPUS_RESET_STATE); | ||
2062 | |||
2063 | return OPUS_OK; | ||
2064 | } | ||
2065 | |||
2066 | #ifdef CUSTOM_MODES | ||
2067 | void opus_custom_decoder_destroy(CELTDecoder *st) | ||
2068 | { | ||
2069 | opus_free(st); | ||
2070 | } | ||
2071 | #endif /* CUSTOM_MODES */ | ||
2072 | |||
2073 | static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, opus_val16 * OPUS_RESTRICT pcm, int N, int LM) | ||
2074 | { | ||
2075 | int c; | ||
2076 | int pitch_index; | ||
2077 | int overlap = st->mode->overlap; | ||
2078 | opus_val16 fade = Q15ONE; | ||
2079 | int i, len; | ||
2080 | const int C = st->channels; | ||
2081 | int offset; | ||
2082 | celt_sig *out_mem[2]; | ||
2083 | celt_sig *decode_mem[2]; | ||
2084 | celt_sig *overlap_mem[2]; | ||
2085 | opus_val16 *lpc; | ||
2086 | opus_val32 *out_syn[2]; | ||
2087 | opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; | ||
2088 | SAVE_STACK; | ||
2089 | |||
2090 | c=0; do { | ||
2091 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap); | ||
2092 | out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD; | ||
2093 | overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE; | ||
2094 | } while (++c<C); | ||
2095 | lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C); | ||
2096 | oldBandE = lpc+C*LPC_ORDER; | ||
2097 | oldLogE = oldBandE + 2*st->mode->nbEBands; | ||
2098 | oldLogE2 = oldLogE + 2*st->mode->nbEBands; | ||
2099 | backgroundLogE = oldLogE2 + 2*st->mode->nbEBands; | ||
2100 | |||
2101 | out_syn[0] = out_mem[0]+MAX_PERIOD-N; | ||
2102 | if (C==2) | ||
2103 | out_syn[1] = out_mem[1]+MAX_PERIOD-N; | ||
2104 | |||
2105 | len = N+st->mode->overlap; | ||
2106 | |||
2107 | if (st->loss_count >= 5 || st->start!=0) | ||
2108 | { | ||
2109 | /* Noise-based PLC/CNG */ | ||
2110 | VARDECL(celt_sig, freq); | ||
2111 | VARDECL(celt_norm, X); | ||
2112 | VARDECL(celt_ener, bandE); | ||
2113 | opus_uint32 seed; | ||
2114 | int effEnd; | ||
2115 | |||
2116 | effEnd = st->end; | ||
2117 | if (effEnd > st->mode->effEBands) | ||
2118 | effEnd = st->mode->effEBands; | ||
2119 | |||
2120 | ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */ | ||
2121 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ | ||
2122 | ALLOC(bandE, st->mode->nbEBands*C, celt_ener); | ||
2123 | |||
2124 | if (st->loss_count >= 5) | ||
2125 | log2Amp(st->mode, st->start, st->end, bandE, backgroundLogE, C); | ||
2126 | else { | ||
2127 | /* Energy decay */ | ||
2128 | opus_val16 decay = st->loss_count==0 ? QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT); | ||
2129 | c=0; do | ||
2130 | { | ||
2131 | for (i=st->start;i<st->end;i++) | ||
2132 | oldBandE[c*st->mode->nbEBands+i] -= decay; | ||
2133 | } while (++c<C); | ||
2134 | log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C); | ||
2135 | } | ||
2136 | seed = st->rng; | ||
2137 | for (c=0;c<C;c++) | ||
2138 | { | ||
2139 | for (i=0;i<(st->mode->eBands[st->start]<<LM);i++) | ||
2140 | X[c*N+i] = 0; | ||
2141 | for (i=st->start;i<st->mode->effEBands;i++) | ||
2142 | { | ||
2143 | int j; | ||
2144 | int boffs; | ||
2145 | int blen; | ||
2146 | boffs = N*c+(st->mode->eBands[i]<<LM); | ||
2147 | blen = (st->mode->eBands[i+1]-st->mode->eBands[i])<<LM; | ||
2148 | for (j=0;j<blen;j++) | ||
2149 | { | ||
2150 | seed = celt_lcg_rand(seed); | ||
2151 | X[boffs+j] = (celt_norm)((opus_int32)seed>>20); | ||
2152 | } | ||
2153 | renormalise_vector(X+boffs, blen, Q15ONE); | ||
2154 | } | ||
2155 | for (i=(st->mode->eBands[st->end]<<LM);i<N;i++) | ||
2156 | X[c*N+i] = 0; | ||
2157 | } | ||
2158 | st->rng = seed; | ||
2159 | |||
2160 | denormalise_bands(st->mode, X, freq, bandE, st->mode->effEBands, C, 1<<LM); | ||
2161 | |||
2162 | c=0; do | ||
2163 | for (i=0;i<st->mode->eBands[st->start]<<LM;i++) | ||
2164 | freq[c*N+i] = 0; | ||
2165 | while (++c<C); | ||
2166 | c=0; do { | ||
2167 | int bound = st->mode->eBands[effEnd]<<LM; | ||
2168 | if (st->downsample!=1) | ||
2169 | bound = IMIN(bound, N/st->downsample); | ||
2170 | for (i=bound;i<N;i++) | ||
2171 | freq[c*N+i] = 0; | ||
2172 | } while (++c<C); | ||
2173 | compute_inv_mdcts(st->mode, 0, freq, out_syn, overlap_mem, C, LM); | ||
2174 | } else { | ||
2175 | /* Pitch-based PLC */ | ||
2176 | if (st->loss_count == 0) | ||
2177 | { | ||
2178 | opus_val16 pitch_buf[DECODE_BUFFER_SIZE>>1]; | ||
2179 | /* Corresponds to a min pitch of 67 Hz. It's possible to save CPU in this | ||
2180 | search by using only part of the decode buffer */ | ||
2181 | int poffset = 720; | ||
2182 | pitch_downsample(decode_mem, pitch_buf, DECODE_BUFFER_SIZE, C); | ||
2183 | /* Max pitch is 100 samples (480 Hz) */ | ||
2184 | pitch_search(pitch_buf+((poffset)>>1), pitch_buf, DECODE_BUFFER_SIZE-poffset, | ||
2185 | poffset-100, &pitch_index); | ||
2186 | pitch_index = poffset-pitch_index; | ||
2187 | st->last_pitch_index = pitch_index; | ||
2188 | } else { | ||
2189 | pitch_index = st->last_pitch_index; | ||
2190 | fade = QCONST16(.8f,15); | ||
2191 | } | ||
2192 | |||
2193 | c=0; do { | ||
2194 | VARDECL(opus_val32, e); | ||
2195 | opus_val16 exc[MAX_PERIOD]; | ||
2196 | opus_val32 ac[LPC_ORDER+1]; | ||
2197 | opus_val16 decay = 1; | ||
2198 | opus_val32 S1=0; | ||
2199 | opus_val16 mem[LPC_ORDER]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | ||
2200 | |||
2201 | ALLOC(e, MAX_PERIOD+2*st->mode->overlap, opus_val32); | ||
2202 | |||
2203 | offset = MAX_PERIOD-pitch_index; | ||
2204 | for (i=0;i<MAX_PERIOD;i++) | ||
2205 | exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT); | ||
2206 | |||
2207 | if (st->loss_count == 0) | ||
2208 | { | ||
2209 | _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap, | ||
2210 | LPC_ORDER, MAX_PERIOD); | ||
2211 | |||
2212 | /* Noise floor -40 dB */ | ||
2213 | #ifdef FIXED_POINT | ||
2214 | ac[0] += SHR32(ac[0],13); | ||
2215 | #else | ||
2216 | ac[0] *= 1.0001f; | ||
2217 | #endif | ||
2218 | /* Lag windowing */ | ||
2219 | for (i=1;i<=LPC_ORDER;i++) | ||
2220 | { | ||
2221 | /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/ | ||
2222 | #ifdef FIXED_POINT | ||
2223 | ac[i] -= MULT16_32_Q15(2*i*i, ac[i]); | ||
2224 | #else | ||
2225 | ac[i] -= ac[i]*(.008f*i)*(.008f*i); | ||
2226 | #endif | ||
2227 | } | ||
2228 | |||
2229 | _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER); | ||
2230 | } | ||
2231 | for (i=0;i<LPC_ORDER;i++) | ||
2232 | mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT); | ||
2233 | celt_fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem); | ||
2234 | /*for (i=0;i<MAX_PERIOD;i++)printf("%d ", exc[i]); printf("\n");*/ | ||
2235 | /* Check if the waveform is decaying (and if so how fast) */ | ||
2236 | { | ||
2237 | opus_val32 E1=1, E2=1; | ||
2238 | int period; | ||
2239 | if (pitch_index <= MAX_PERIOD/2) | ||
2240 | period = pitch_index; | ||
2241 | else | ||
2242 | period = MAX_PERIOD/2; | ||
2243 | for (i=0;i<period;i++) | ||
2244 | { | ||
2245 | E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8); | ||
2246 | E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8); | ||
2247 | } | ||
2248 | if (E1 > E2) | ||
2249 | E1 = E2; | ||
2250 | decay = celt_sqrt(frac_div32(SHR32(E1,1),E2)); | ||
2251 | } | ||
2252 | |||
2253 | /* Copy excitation, taking decay into account */ | ||
2254 | for (i=0;i<len+st->mode->overlap;i++) | ||
2255 | { | ||
2256 | opus_val16 tmp; | ||
2257 | if (offset+i >= MAX_PERIOD) | ||
2258 | { | ||
2259 | offset -= pitch_index; | ||
2260 | decay = MULT16_16_Q15(decay, decay); | ||
2261 | } | ||
2262 | e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT); | ||
2263 | tmp = ROUND16(out_mem[c][offset+i],SIG_SHIFT); | ||
2264 | S1 += SHR32(MULT16_16(tmp,tmp),8); | ||
2265 | } | ||
2266 | for (i=0;i<LPC_ORDER;i++) | ||
2267 | mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT); | ||
2268 | for (i=0;i<len+st->mode->overlap;i++) | ||
2269 | e[i] = MULT16_32_Q15(fade, e[i]); | ||
2270 | celt_iir(e, lpc+c*LPC_ORDER, e, len+st->mode->overlap, LPC_ORDER, mem); | ||
2271 | |||
2272 | { | ||
2273 | opus_val32 S2=0; | ||
2274 | for (i=0;i<len+overlap;i++) | ||
2275 | { | ||
2276 | opus_val16 tmp = ROUND16(e[i],SIG_SHIFT); | ||
2277 | S2 += SHR32(MULT16_16(tmp,tmp),8); | ||
2278 | } | ||
2279 | /* This checks for an "explosion" in the synthesis */ | ||
2280 | #ifdef FIXED_POINT | ||
2281 | if (!(S1 > SHR32(S2,2))) | ||
2282 | #else | ||
2283 | /* Float test is written this way to catch NaNs at the same time */ | ||
2284 | if (!(S1 > 0.2f*S2)) | ||
2285 | #endif | ||
2286 | { | ||
2287 | for (i=0;i<len+overlap;i++) | ||
2288 | e[i] = 0; | ||
2289 | } else if (S1 < S2) | ||
2290 | { | ||
2291 | opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1)); | ||
2292 | for (i=0;i<len+overlap;i++) | ||
2293 | e[i] = MULT16_32_Q15(ratio, e[i]); | ||
2294 | } | ||
2295 | } | ||
2296 | |||
2297 | /* Apply post-filter to the MDCT overlap of the previous frame */ | ||
2298 | comb_filter(out_mem[c]+MAX_PERIOD, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap, | ||
2299 | st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset, | ||
2300 | NULL, 0); | ||
2301 | |||
2302 | for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++) | ||
2303 | out_mem[c][i] = out_mem[c][N+i]; | ||
2304 | |||
2305 | /* Apply TDAC to the concealed audio so that it blends with the | ||
2306 | previous and next frames */ | ||
2307 | for (i=0;i<overlap/2;i++) | ||
2308 | { | ||
2309 | opus_val32 tmp; | ||
2310 | tmp = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) + | ||
2311 | MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]); | ||
2312 | out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp); | ||
2313 | out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp); | ||
2314 | } | ||
2315 | for (i=0;i<N;i++) | ||
2316 | out_mem[c][MAX_PERIOD-N+i] = e[i]; | ||
2317 | |||
2318 | /* Apply pre-filter to the MDCT overlap for the next frame (post-filter will be applied then) */ | ||
2319 | comb_filter(e, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap, | ||
2320 | -st->postfilter_gain, -st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset, | ||
2321 | NULL, 0); | ||
2322 | for (i=0;i<overlap;i++) | ||
2323 | out_mem[c][MAX_PERIOD+i] = e[i]; | ||
2324 | } while (++c<C); | ||
2325 | } | ||
2326 | |||
2327 | deemphasis(out_syn, pcm, N, C, /*st->downsample,*/ st->mode->preemph, st->preemph_memD); | ||
2328 | |||
2329 | st->loss_count++; | ||
2330 | |||
2331 | RESTORE_STACK; | ||
2332 | } | ||
2333 | |||
2334 | #define FREQ_X_BUF_SIZE (2*8*120) /* stereo * nbShortMdcts * shortMdctSize */ | ||
2335 | static celt_sig s_freq[FREQ_X_BUF_SIZE] IBSS_ATTR MEM_ALIGN_ATTR; /* 7680 byte */ | ||
2336 | static celt_norm s_X[FREQ_X_BUF_SIZE] IBSS_ATTR MEM_ALIGN_ATTR; /* 3840 byte */ | ||
2337 | int celt_decode_with_ec(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec) | ||
2338 | { | ||
2339 | int c, i, N; | ||
2340 | int spread_decision; | ||
2341 | opus_int32 bits; | ||
2342 | ec_dec _dec; | ||
2343 | VARDECL(celt_sig, freq); | ||
2344 | VARDECL(celt_norm, X); | ||
2345 | VARDECL(celt_ener, bandE); | ||
2346 | VARDECL(int, fine_quant); | ||
2347 | VARDECL(int, pulses); | ||
2348 | VARDECL(int, cap); | ||
2349 | VARDECL(int, offsets); | ||
2350 | VARDECL(int, fine_priority); | ||
2351 | VARDECL(int, tf_res); | ||
2352 | VARDECL(unsigned char, collapse_masks); | ||
2353 | celt_sig *out_mem[2]; | ||
2354 | celt_sig *decode_mem[2]; | ||
2355 | celt_sig *overlap_mem[2]; | ||
2356 | celt_sig *out_syn[2]; | ||
2357 | opus_val16 *lpc; | ||
2358 | opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; | ||
2359 | |||
2360 | int shortBlocks; | ||
2361 | int isTransient; | ||
2362 | int intra_ener; | ||
2363 | const int CC = st->channels; | ||
2364 | int LM, M; | ||
2365 | int effEnd; | ||
2366 | int codedBands; | ||
2367 | int alloc_trim; | ||
2368 | int postfilter_pitch; | ||
2369 | opus_val16 postfilter_gain; | ||
2370 | int intensity=0; | ||
2371 | int dual_stereo=0; | ||
2372 | opus_int32 total_bits; | ||
2373 | opus_int32 balance; | ||
2374 | opus_int32 tell; | ||
2375 | int dynalloc_logp; | ||
2376 | int postfilter_tapset; | ||
2377 | int anti_collapse_rsv; | ||
2378 | int anti_collapse_on=0; | ||
2379 | int silence; | ||
2380 | int C = st->stream_channels; | ||
2381 | ALLOC_STACK; | ||
2382 | |||
2383 | frame_size *= st->downsample; | ||
2384 | |||
2385 | c=0; do { | ||
2386 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap); | ||
2387 | out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD; | ||
2388 | overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE; | ||
2389 | } while (++c<CC); | ||
2390 | lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*CC); | ||
2391 | oldBandE = lpc+CC*LPC_ORDER; | ||
2392 | oldLogE = oldBandE + 2*st->mode->nbEBands; | ||
2393 | oldLogE2 = oldLogE + 2*st->mode->nbEBands; | ||
2394 | backgroundLogE = oldLogE2 + 2*st->mode->nbEBands; | ||
2395 | |||
2396 | #ifdef CUSTOM_MODES | ||
2397 | if (st->signalling && data!=NULL) | ||
2398 | { | ||
2399 | int data0=data[0]; | ||
2400 | /* Convert "standard mode" to Opus header */ | ||
2401 | if (st->mode->Fs==48000 && st->mode->shortMdctSize==120) | ||
2402 | { | ||
2403 | data0 = fromOpus(data0); | ||
2404 | if (data0<0) | ||
2405 | return OPUS_INVALID_PACKET; | ||
2406 | } | ||
2407 | st->end = IMAX(1, st->mode->effEBands-2*(data0>>5)); | ||
2408 | LM = (data0>>3)&0x3; | ||
2409 | C = 1 + ((data0>>2)&0x1); | ||
2410 | data++; | ||
2411 | len--; | ||
2412 | if (LM>st->mode->maxLM) | ||
2413 | return OPUS_INVALID_PACKET; | ||
2414 | if (frame_size < st->mode->shortMdctSize<<LM) | ||
2415 | return OPUS_BUFFER_TOO_SMALL; | ||
2416 | else | ||
2417 | frame_size = st->mode->shortMdctSize<<LM; | ||
2418 | } else { | ||
2419 | #else | ||
2420 | { | ||
2421 | #endif | ||
2422 | for (LM=0;LM<=st->mode->maxLM;LM++) | ||
2423 | if (st->mode->shortMdctSize<<LM==frame_size) | ||
2424 | break; | ||
2425 | if (LM>st->mode->maxLM) | ||
2426 | return OPUS_BAD_ARG; | ||
2427 | } | ||
2428 | M=1<<LM; | ||
2429 | |||
2430 | if (len<0 || len>1275 || pcm==NULL) | ||
2431 | return OPUS_BAD_ARG; | ||
2432 | |||
2433 | N = M*st->mode->shortMdctSize; | ||
2434 | |||
2435 | effEnd = st->end; | ||
2436 | if (effEnd > st->mode->effEBands) | ||
2437 | effEnd = st->mode->effEBands; | ||
2438 | |||
2439 | /**< Interleaved signal MDCTs */ | ||
2440 | if (FREQ_X_BUF_SIZE >= IMAX(CC,C)*N) | ||
2441 | freq = s_freq; | ||
2442 | else | ||
2443 | ALLOC(freq, IMAX(CC,C)*N, celt_sig); | ||
2444 | |||
2445 | /**< Interleaved normalised MDCTs */ | ||
2446 | if (FREQ_X_BUF_SIZE >= C*N) | ||
2447 | X = s_X; | ||
2448 | else | ||
2449 | ALLOC(X, C*N, celt_norm); | ||
2450 | |||
2451 | ALLOC(bandE, st->mode->nbEBands*C, celt_ener); | ||
2452 | c=0; do | ||
2453 | for (i=0;i<M*st->mode->eBands[st->start];i++) | ||
2454 | X[c*N+i] = 0; | ||
2455 | while (++c<C); | ||
2456 | c=0; do | ||
2457 | for (i=M*st->mode->eBands[effEnd];i<N;i++) | ||
2458 | X[c*N+i] = 0; | ||
2459 | while (++c<C); | ||
2460 | |||
2461 | if (data == NULL || len<=1) | ||
2462 | { | ||
2463 | celt_decode_lost(st, pcm, N, LM); | ||
2464 | RESTORE_STACK; | ||
2465 | return frame_size/st->downsample; | ||
2466 | } | ||
2467 | |||
2468 | if (dec == NULL) | ||
2469 | { | ||
2470 | ec_dec_init(&_dec,(unsigned char*)data,len); | ||
2471 | dec = &_dec; | ||
2472 | } | ||
2473 | |||
2474 | if (C==1) | ||
2475 | { | ||
2476 | for (i=0;i<st->mode->nbEBands;i++) | ||
2477 | oldBandE[i]=MAX16(oldBandE[i],oldBandE[st->mode->nbEBands+i]); | ||
2478 | } | ||
2479 | |||
2480 | total_bits = len*8; | ||
2481 | tell = ec_tell(dec); | ||
2482 | |||
2483 | if (tell >= total_bits) | ||
2484 | silence = 1; | ||
2485 | else if (tell==1) | ||
2486 | silence = ec_dec_bit_logp(dec, 15); | ||
2487 | else | ||
2488 | silence = 0; | ||
2489 | if (silence) | ||
2490 | { | ||
2491 | /* Pretend we've read all the remaining bits */ | ||
2492 | tell = len*8; | ||
2493 | dec->nbits_total+=tell-ec_tell(dec); | ||
2494 | } | ||
2495 | |||
2496 | postfilter_gain = 0; | ||
2497 | postfilter_pitch = 0; | ||
2498 | postfilter_tapset = 0; | ||
2499 | if (st->start==0 && tell+16 <= total_bits) | ||
2500 | { | ||
2501 | if(ec_dec_bit_logp(dec, 1)) | ||
2502 | { | ||
2503 | int qg, octave; | ||
2504 | octave = ec_dec_uint(dec, 6); | ||
2505 | postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1; | ||
2506 | qg = ec_dec_bits(dec, 3); | ||
2507 | if (ec_tell(dec)+2<=total_bits) | ||
2508 | postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2); | ||
2509 | postfilter_gain = QCONST16(.09375f,15)*(qg+1); | ||
2510 | } | ||
2511 | tell = ec_tell(dec); | ||
2512 | } | ||
2513 | |||
2514 | if (LM > 0 && tell+3 <= total_bits) | ||
2515 | { | ||
2516 | isTransient = ec_dec_bit_logp(dec, 3); | ||
2517 | tell = ec_tell(dec); | ||
2518 | } | ||
2519 | else | ||
2520 | isTransient = 0; | ||
2521 | |||
2522 | if (isTransient) | ||
2523 | shortBlocks = M; | ||
2524 | else | ||
2525 | shortBlocks = 0; | ||
2526 | |||
2527 | /* Decode the global flags (first symbols in the stream) */ | ||
2528 | intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0; | ||
2529 | /* Get band energies */ | ||
2530 | unquant_coarse_energy(st->mode, st->start, st->end, oldBandE, | ||
2531 | intra_ener, dec, C, LM); | ||
2532 | |||
2533 | ALLOC(tf_res, st->mode->nbEBands, int); | ||
2534 | tf_decode(st->start, st->end, isTransient, tf_res, LM, dec); | ||
2535 | |||
2536 | tell = ec_tell(dec); | ||
2537 | spread_decision = SPREAD_NORMAL; | ||
2538 | if (tell+4 <= total_bits) | ||
2539 | spread_decision = ec_dec_icdf(dec, spread_icdf, 5); | ||
2540 | |||
2541 | ALLOC(pulses, st->mode->nbEBands, int); | ||
2542 | ALLOC(cap, st->mode->nbEBands, int); | ||
2543 | ALLOC(offsets, st->mode->nbEBands, int); | ||
2544 | ALLOC(fine_priority, st->mode->nbEBands, int); | ||
2545 | |||
2546 | init_caps(st->mode,cap,LM,C); | ||
2547 | |||
2548 | dynalloc_logp = 6; | ||
2549 | total_bits<<=BITRES; | ||
2550 | tell = ec_tell_frac(dec); | ||
2551 | for (i=st->start;i<st->end;i++) | ||
2552 | { | ||
2553 | int width, quanta; | ||
2554 | int dynalloc_loop_logp; | ||
2555 | int boost; | ||
2556 | width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM; | ||
2557 | /* quanta is 6 bits, but no more than 1 bit/sample | ||
2558 | and no less than 1/8 bit/sample */ | ||
2559 | quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); | ||
2560 | dynalloc_loop_logp = dynalloc_logp; | ||
2561 | boost = 0; | ||
2562 | while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i]) | ||
2563 | { | ||
2564 | int flag; | ||
2565 | flag = ec_dec_bit_logp(dec, dynalloc_loop_logp); | ||
2566 | tell = ec_tell_frac(dec); | ||
2567 | if (!flag) | ||
2568 | break; | ||
2569 | boost += quanta; | ||
2570 | total_bits -= quanta; | ||
2571 | dynalloc_loop_logp = 1; | ||
2572 | } | ||
2573 | offsets[i] = boost; | ||
2574 | /* Making dynalloc more likely */ | ||
2575 | if (boost>0) | ||
2576 | dynalloc_logp = IMAX(2, dynalloc_logp-1); | ||
2577 | } | ||
2578 | |||
2579 | ALLOC(fine_quant, st->mode->nbEBands, int); | ||
2580 | alloc_trim = tell+(6<<BITRES) <= total_bits ? | ||
2581 | ec_dec_icdf(dec, trim_icdf, 7) : 5; | ||
2582 | |||
2583 | bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1; | ||
2584 | anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0; | ||
2585 | bits -= anti_collapse_rsv; | ||
2586 | codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap, | ||
2587 | alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses, | ||
2588 | fine_quant, fine_priority, C, LM, dec, 0, 0); | ||
2589 | |||
2590 | unquant_fine_energy(st->mode, st->start, st->end, oldBandE, fine_quant, dec, C); | ||
2591 | |||
2592 | /* Decode fixed codebook */ | ||
2593 | ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char); | ||
2594 | quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks, | ||
2595 | NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res, | ||
2596 | len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng); | ||
2597 | |||
2598 | if (anti_collapse_rsv > 0) | ||
2599 | { | ||
2600 | anti_collapse_on = ec_dec_bits(dec, 1); | ||
2601 | } | ||
2602 | |||
2603 | unquant_energy_finalise(st->mode, st->start, st->end, oldBandE, | ||
2604 | fine_quant, fine_priority, len*8-ec_tell(dec), dec, C); | ||
2605 | |||
2606 | if (anti_collapse_on) | ||
2607 | anti_collapse(st->mode, X, collapse_masks, LM, C, N, | ||
2608 | st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng); | ||
2609 | |||
2610 | log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C); | ||
2611 | |||
2612 | if (silence) | ||
2613 | { | ||
2614 | for (i=0;i<C*st->mode->nbEBands;i++) | ||
2615 | { | ||
2616 | bandE[i] = 0; | ||
2617 | oldBandE[i] = -QCONST16(28.f,DB_SHIFT); | ||
2618 | } | ||
2619 | } | ||
2620 | /* Synthesis */ | ||
2621 | denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M); | ||
2622 | |||
2623 | OPUS_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N); | ||
2624 | if (CC==2) | ||
2625 | OPUS_MOVE(decode_mem[1], decode_mem[1]+N, DECODE_BUFFER_SIZE-N); | ||
2626 | |||
2627 | c=0; do | ||
2628 | for (i=0;i<M*st->mode->eBands[st->start];i++) | ||
2629 | freq[c*N+i] = 0; | ||
2630 | while (++c<C); | ||
2631 | c=0; do { | ||
2632 | int bound = M*st->mode->eBands[effEnd]; | ||
2633 | if (st->downsample!=1) | ||
2634 | bound = IMIN(bound, N/st->downsample); | ||
2635 | for (i=bound;i<N;i++) | ||
2636 | freq[c*N+i] = 0; | ||
2637 | } while (++c<C); | ||
2638 | |||
2639 | out_syn[0] = out_mem[0]+MAX_PERIOD-N; | ||
2640 | if (CC==2) | ||
2641 | out_syn[1] = out_mem[1]+MAX_PERIOD-N; | ||
2642 | |||
2643 | if (CC==2&&C==1) | ||
2644 | { | ||
2645 | for (i=0;i<N;i++) | ||
2646 | freq[N+i] = freq[i]; | ||
2647 | } | ||
2648 | if (CC==1&&C==2) | ||
2649 | { | ||
2650 | for (i=0;i<N;i++) | ||
2651 | freq[i] = HALF32(ADD32(freq[i],freq[N+i])); | ||
2652 | } | ||
2653 | |||
2654 | /* Compute inverse MDCTs */ | ||
2655 | compute_inv_mdcts(st->mode, shortBlocks, freq, out_syn, overlap_mem, CC, LM); | ||
2656 | |||
2657 | c=0; do { | ||
2658 | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); | ||
2659 | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); | ||
2660 | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, st->mode->shortMdctSize, | ||
2661 | st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, | ||
2662 | st->mode->window, st->overlap); | ||
2663 | if (LM!=0) | ||
2664 | comb_filter(out_syn[c]+st->mode->shortMdctSize, out_syn[c]+st->mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-st->mode->shortMdctSize, | ||
2665 | st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset, | ||
2666 | st->mode->window, st->mode->overlap); | ||
2667 | |||
2668 | } while (++c<CC); | ||
2669 | st->postfilter_period_old = st->postfilter_period; | ||
2670 | st->postfilter_gain_old = st->postfilter_gain; | ||
2671 | st->postfilter_tapset_old = st->postfilter_tapset; | ||
2672 | st->postfilter_period = postfilter_pitch; | ||
2673 | st->postfilter_gain = postfilter_gain; | ||
2674 | st->postfilter_tapset = postfilter_tapset; | ||
2675 | if (LM!=0) | ||
2676 | { | ||
2677 | st->postfilter_period_old = st->postfilter_period; | ||
2678 | st->postfilter_gain_old = st->postfilter_gain; | ||
2679 | st->postfilter_tapset_old = st->postfilter_tapset; | ||
2680 | } | ||
2681 | |||
2682 | if (C==1) { | ||
2683 | for (i=0;i<st->mode->nbEBands;i++) | ||
2684 | oldBandE[st->mode->nbEBands+i]=oldBandE[i]; | ||
2685 | } | ||
2686 | |||
2687 | /* In case start or end were to change */ | ||
2688 | if (!isTransient) | ||
2689 | { | ||
2690 | for (i=0;i<2*st->mode->nbEBands;i++) | ||
2691 | oldLogE2[i] = oldLogE[i]; | ||
2692 | for (i=0;i<2*st->mode->nbEBands;i++) | ||
2693 | oldLogE[i] = oldBandE[i]; | ||
2694 | for (i=0;i<2*st->mode->nbEBands;i++) | ||
2695 | backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]); | ||
2696 | } else { | ||
2697 | for (i=0;i<2*st->mode->nbEBands;i++) | ||
2698 | oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]); | ||
2699 | } | ||
2700 | c=0; do | ||
2701 | { | ||
2702 | for (i=0;i<st->start;i++) | ||
2703 | { | ||
2704 | oldBandE[c*st->mode->nbEBands+i]=0; | ||
2705 | oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT); | ||
2706 | } | ||
2707 | for (i=st->end;i<st->mode->nbEBands;i++) | ||
2708 | { | ||
2709 | oldBandE[c*st->mode->nbEBands+i]=0; | ||
2710 | oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT); | ||
2711 | } | ||
2712 | } while (++c<2); | ||
2713 | st->rng = dec->rng; | ||
2714 | |||
2715 | deemphasis(out_syn, pcm, N, CC, /*st->downsample,*/ st->mode->preemph, st->preemph_memD); | ||
2716 | st->loss_count = 0; | ||
2717 | RESTORE_STACK; | ||
2718 | if (ec_tell(dec) > 8*len) | ||
2719 | return OPUS_INTERNAL_ERROR; | ||
2720 | if(ec_get_error(dec)) | ||
2721 | st->error = 1; | ||
2722 | return frame_size/st->downsample; | ||
2723 | } | ||
2724 | |||
2725 | |||
2726 | #ifdef CUSTOM_MODES | ||
2727 | |||
2728 | #ifdef FIXED_POINT | ||
2729 | int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size) | ||
2730 | { | ||
2731 | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL); | ||
2732 | } | ||
2733 | |||
2734 | #ifndef DISABLE_FLOAT_API | ||
2735 | int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size) | ||
2736 | { | ||
2737 | int j, ret, C, N; | ||
2738 | VARDECL(opus_int16, out); | ||
2739 | ALLOC_STACK; | ||
2740 | |||
2741 | if (pcm==NULL) | ||
2742 | return OPUS_BAD_ARG; | ||
2743 | |||
2744 | C = st->channels; | ||
2745 | N = frame_size; | ||
2746 | |||
2747 | ALLOC(out, C*N, opus_int16); | ||
2748 | ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL); | ||
2749 | if (ret>0) | ||
2750 | for (j=0;j<C*ret;j++) | ||
2751 | pcm[j]=out[j]*(1.f/32768.f); | ||
2752 | |||
2753 | RESTORE_STACK; | ||
2754 | return ret; | ||
2755 | } | ||
2756 | #endif /* DISABLE_FLOAT_API */ | ||
2757 | |||
2758 | #else | ||
2759 | |||
2760 | int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size) | ||
2761 | { | ||
2762 | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL); | ||
2763 | } | ||
2764 | |||
2765 | int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size) | ||
2766 | { | ||
2767 | int j, ret, C, N; | ||
2768 | VARDECL(celt_sig, out); | ||
2769 | ALLOC_STACK; | ||
2770 | |||
2771 | if (pcm==NULL) | ||
2772 | return OPUS_BAD_ARG; | ||
2773 | |||
2774 | C = st->channels; | ||
2775 | N = frame_size; | ||
2776 | ALLOC(out, C*N, celt_sig); | ||
2777 | |||
2778 | ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL); | ||
2779 | |||
2780 | if (ret>0) | ||
2781 | for (j=0;j<C*ret;j++) | ||
2782 | pcm[j] = FLOAT2INT16 (out[j]); | ||
2783 | |||
2784 | RESTORE_STACK; | ||
2785 | return ret; | ||
2786 | } | ||
2787 | |||
2788 | #endif | ||
2789 | #endif /* CUSTOM_MODES */ | ||
2790 | |||
2791 | int opus_custom_decoder_ctl(CELTDecoder * OPUS_RESTRICT st, int request, ...) | ||
2792 | { | ||
2793 | va_list ap; | ||
2794 | |||
2795 | va_start(ap, request); | ||
2796 | switch (request) | ||
2797 | { | ||
2798 | case CELT_SET_START_BAND_REQUEST: | ||
2799 | { | ||
2800 | opus_int32 value = va_arg(ap, opus_int32); | ||
2801 | if (value<0 || value>=st->mode->nbEBands) | ||
2802 | goto bad_arg; | ||
2803 | st->start = value; | ||
2804 | } | ||
2805 | break; | ||
2806 | case CELT_SET_END_BAND_REQUEST: | ||
2807 | { | ||
2808 | opus_int32 value = va_arg(ap, opus_int32); | ||
2809 | if (value<1 || value>st->mode->nbEBands) | ||
2810 | goto bad_arg; | ||
2811 | st->end = value; | ||
2812 | } | ||
2813 | break; | ||
2814 | case CELT_SET_CHANNELS_REQUEST: | ||
2815 | { | ||
2816 | opus_int32 value = va_arg(ap, opus_int32); | ||
2817 | if (value<1 || value>2) | ||
2818 | goto bad_arg; | ||
2819 | st->stream_channels = value; | ||
2820 | } | ||
2821 | break; | ||
2822 | case CELT_GET_AND_CLEAR_ERROR_REQUEST: | ||
2823 | { | ||
2824 | opus_int32 *value = va_arg(ap, opus_int32*); | ||
2825 | if (value==NULL) | ||
2826 | goto bad_arg; | ||
2827 | *value=st->error; | ||
2828 | st->error = 0; | ||
2829 | } | ||
2830 | break; | ||
2831 | case OPUS_GET_LOOKAHEAD_REQUEST: | ||
2832 | { | ||
2833 | opus_int32 *value = va_arg(ap, opus_int32*); | ||
2834 | if (value==NULL) | ||
2835 | goto bad_arg; | ||
2836 | *value = st->overlap/st->downsample; | ||
2837 | } | ||
2838 | break; | ||
2839 | case OPUS_RESET_STATE: | ||
2840 | { | ||
2841 | int i; | ||
2842 | opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2; | ||
2843 | lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels); | ||
2844 | oldBandE = lpc+st->channels*LPC_ORDER; | ||
2845 | oldLogE = oldBandE + 2*st->mode->nbEBands; | ||
2846 | oldLogE2 = oldLogE + 2*st->mode->nbEBands; | ||
2847 | OPUS_CLEAR((char*)&st->DECODER_RESET_START, | ||
2848 | opus_custom_decoder_get_size(st->mode, st->channels)- | ||
2849 | ((char*)&st->DECODER_RESET_START - (char*)st)); | ||
2850 | for (i=0;i<2*st->mode->nbEBands;i++) | ||
2851 | oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT); | ||
2852 | } | ||
2853 | break; | ||
2854 | case OPUS_GET_PITCH_REQUEST: | ||
2855 | { | ||
2856 | opus_int32 *value = va_arg(ap, opus_int32*); | ||
2857 | if (value==NULL) | ||
2858 | goto bad_arg; | ||
2859 | *value = st->postfilter_period; | ||
2860 | } | ||
2861 | break; | ||
2862 | #ifdef OPUS_BUILD | ||
2863 | case CELT_GET_MODE_REQUEST: | ||
2864 | { | ||
2865 | const CELTMode ** value = va_arg(ap, const CELTMode**); | ||
2866 | if (value==0) | ||
2867 | goto bad_arg; | ||
2868 | *value=st->mode; | ||
2869 | } | ||
2870 | break; | ||
2871 | case CELT_SET_SIGNALLING_REQUEST: | ||
2872 | { | ||
2873 | opus_int32 value = va_arg(ap, opus_int32); | ||
2874 | st->signalling = value; | ||
2875 | } | ||
2876 | break; | ||
2877 | case OPUS_GET_FINAL_RANGE_REQUEST: | ||
2878 | { | ||
2879 | opus_uint32 * value = va_arg(ap, opus_uint32 *); | ||
2880 | if (value==0) | ||
2881 | goto bad_arg; | ||
2882 | *value=st->rng; | ||
2883 | } | ||
2884 | break; | ||
2885 | #endif | ||
2886 | default: | ||
2887 | goto bad_request; | ||
2888 | } | ||
2889 | va_end(ap); | ||
2890 | return OPUS_OK; | ||
2891 | bad_arg: | ||
2892 | va_end(ap); | ||
2893 | return OPUS_BAD_ARG; | ||
2894 | bad_request: | ||
2895 | va_end(ap); | ||
2896 | return OPUS_UNIMPLEMENTED; | ||
2897 | } | ||
2898 | |||
2899 | 193 | ||
2900 | 194 | ||
2901 | const char *opus_strerror(int error) | 195 | const char *opus_strerror(int error) |
@@ -2918,7 +212,7 @@ const char *opus_strerror(int error) | |||
2918 | 212 | ||
2919 | const char *opus_get_version_string(void) | 213 | const char *opus_get_version_string(void) |
2920 | { | 214 | { |
2921 | return "libopus " OPUS_VERSION | 215 | return "libopus " PACKAGE_VERSION |
2922 | #ifdef FIXED_POINT | 216 | #ifdef FIXED_POINT |
2923 | "-fixed" | 217 | "-fixed" |
2924 | #endif | 218 | #endif |
diff --git a/lib/rbcodec/codecs/libopus/celt/celt.h b/lib/rbcodec/codecs/libopus/celt/celt.h index 218cd883df..0911c72f72 100644 --- a/lib/rbcodec/codecs/libopus/celt/celt.h +++ b/lib/rbcodec/codecs/libopus/celt/celt.h | |||
@@ -50,7 +50,19 @@ extern "C" { | |||
50 | #define CELTDecoder OpusCustomDecoder | 50 | #define CELTDecoder OpusCustomDecoder |
51 | #define CELTMode OpusCustomMode | 51 | #define CELTMode OpusCustomMode |
52 | 52 | ||
53 | #define _celt_check_mode_ptr_ptr(ptr) ((ptr) + ((ptr) - (const CELTMode**)(ptr))) | 53 | typedef struct { |
54 | int valid; | ||
55 | opus_val16 tonality; | ||
56 | opus_val16 tonality_slope; | ||
57 | opus_val16 noisiness; | ||
58 | opus_val16 activity; | ||
59 | opus_val16 music_prob; | ||
60 | int bandwidth; | ||
61 | }AnalysisInfo; | ||
62 | |||
63 | #define __celt_check_mode_ptr_ptr(ptr) ((ptr) + ((ptr) - (const CELTMode**)(ptr))) | ||
64 | |||
65 | #define __celt_check_analysis_ptr(ptr) ((ptr) + ((ptr) - (const AnalysisInfo*)(ptr))) | ||
54 | 66 | ||
55 | /* Encoder/decoder Requests */ | 67 | /* Encoder/decoder Requests */ |
56 | 68 | ||
@@ -81,12 +93,27 @@ extern "C" { | |||
81 | 93 | ||
82 | #define CELT_GET_MODE_REQUEST 10015 | 94 | #define CELT_GET_MODE_REQUEST 10015 |
83 | /** Get the CELTMode used by an encoder or decoder */ | 95 | /** Get the CELTMode used by an encoder or decoder */ |
84 | #define CELT_GET_MODE(x) CELT_GET_MODE_REQUEST, _celt_check_mode_ptr_ptr(x) | 96 | #define CELT_GET_MODE(x) CELT_GET_MODE_REQUEST, __celt_check_mode_ptr_ptr(x) |
85 | 97 | ||
86 | #define CELT_SET_SIGNALLING_REQUEST 10016 | 98 | #define CELT_SET_SIGNALLING_REQUEST 10016 |
87 | #define CELT_SET_SIGNALLING(x) CELT_SET_SIGNALLING_REQUEST, __opus_check_int(x) | 99 | #define CELT_SET_SIGNALLING(x) CELT_SET_SIGNALLING_REQUEST, __opus_check_int(x) |
88 | 100 | ||
101 | #define CELT_SET_TONALITY_REQUEST 10018 | ||
102 | #define CELT_SET_TONALITY(x) CELT_SET_TONALITY_REQUEST, __opus_check_int(x) | ||
103 | #define CELT_SET_TONALITY_SLOPE_REQUEST 10020 | ||
104 | #define CELT_SET_TONALITY_SLOPE(x) CELT_SET_TONALITY_SLOPE_REQUEST, __opus_check_int(x) | ||
105 | |||
106 | #define CELT_SET_ANALYSIS_REQUEST 10022 | ||
107 | #define CELT_SET_ANALYSIS(x) CELT_SET_ANALYSIS_REQUEST, __celt_check_analysis_ptr(x) | ||
108 | |||
109 | #define OPUS_SET_LFE_REQUEST 10024 | ||
110 | #define OPUS_SET_LFE(x) OPUS_SET_LFE_REQUEST, __opus_check_int(x) | ||
111 | |||
112 | #define OPUS_SET_ENERGY_SAVE_REQUEST 10026 | ||
113 | #define OPUS_SET_ENERGY_SAVE(x) OPUS_SET_ENERGY_SAVE_REQUEST, __opus_check_val16_ptr(x) | ||
89 | 114 | ||
115 | #define OPUS_SET_ENERGY_MASK_REQUEST 10028 | ||
116 | #define OPUS_SET_ENERGY_MASK(x) OPUS_SET_ENERGY_MASK_REQUEST, __opus_check_val16_ptr(x) | ||
90 | 117 | ||
91 | /* Encoder stuff */ | 118 | /* Encoder stuff */ |
92 | 119 | ||
@@ -110,6 +137,75 @@ int celt_decode_with_ec(OpusCustomDecoder * OPUS_RESTRICT st, const unsigned cha | |||
110 | #define celt_encoder_ctl opus_custom_encoder_ctl | 137 | #define celt_encoder_ctl opus_custom_encoder_ctl |
111 | #define celt_decoder_ctl opus_custom_decoder_ctl | 138 | #define celt_decoder_ctl opus_custom_decoder_ctl |
112 | 139 | ||
140 | |||
141 | #ifdef CUSTOM_MODES | ||
142 | #define OPUS_CUSTOM_NOSTATIC | ||
143 | #else | ||
144 | #define OPUS_CUSTOM_NOSTATIC static inline | ||
145 | #endif | ||
146 | |||
147 | static const unsigned char trim_icdf[11] = {126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0}; | ||
148 | /* Probs: NONE: 21.875%, LIGHT: 6.25%, NORMAL: 65.625%, AGGRESSIVE: 6.25% */ | ||
149 | static const unsigned char spread_icdf[4] = {25, 23, 2, 0}; | ||
150 | |||
151 | static const unsigned char tapset_icdf[3]={2,1,0}; | ||
152 | |||
153 | #ifdef CUSTOM_MODES | ||
154 | static const unsigned char toOpusTable[20] = { | ||
155 | 0xE0, 0xE8, 0xF0, 0xF8, | ||
156 | 0xC0, 0xC8, 0xD0, 0xD8, | ||
157 | 0xA0, 0xA8, 0xB0, 0xB8, | ||
158 | 0x00, 0x00, 0x00, 0x00, | ||
159 | 0x80, 0x88, 0x90, 0x98, | ||
160 | }; | ||
161 | |||
162 | static const unsigned char fromOpusTable[16] = { | ||
163 | 0x80, 0x88, 0x90, 0x98, | ||
164 | 0x40, 0x48, 0x50, 0x58, | ||
165 | 0x20, 0x28, 0x30, 0x38, | ||
166 | 0x00, 0x08, 0x10, 0x18 | ||
167 | }; | ||
168 | |||
169 | static inline int toOpus(unsigned char c) | ||
170 | { | ||
171 | int ret=0; | ||
172 | if (c<0xA0) | ||
173 | ret = toOpusTable[c>>3]; | ||
174 | if (ret == 0) | ||
175 | return -1; | ||
176 | else | ||
177 | return ret|(c&0x7); | ||
178 | } | ||
179 | |||
180 | static inline int fromOpus(unsigned char c) | ||
181 | { | ||
182 | if (c<0x80) | ||
183 | return -1; | ||
184 | else | ||
185 | return fromOpusTable[(c>>3)-16] | (c&0x7); | ||
186 | } | ||
187 | #endif /* CUSTOM_MODES */ | ||
188 | |||
189 | #define COMBFILTER_MAXPERIOD 1024 | ||
190 | #define COMBFILTER_MINPERIOD 15 | ||
191 | |||
192 | extern const signed char tf_select_table[4][8]; | ||
193 | |||
194 | int resampling_factor(opus_int32 rate); | ||
195 | |||
196 | void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N, | ||
197 | opus_val16 g0, opus_val16 g1, int tapset0, int tapset1, | ||
198 | const opus_val16 *window, int overlap); | ||
199 | |||
200 | void init_caps(const CELTMode *m,int *cap,int LM,int C); | ||
201 | |||
202 | #ifdef RESYNTH | ||
203 | void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem, celt_sig * OPUS_RESTRICT scratch); | ||
204 | |||
205 | void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X, | ||
206 | celt_sig * OPUS_RESTRICT out_mem[], int C, int LM); | ||
207 | #endif | ||
208 | |||
113 | #ifdef __cplusplus | 209 | #ifdef __cplusplus |
114 | } | 210 | } |
115 | #endif | 211 | #endif |
diff --git a/lib/rbcodec/codecs/libopus/celt/celt_decoder.c b/lib/rbcodec/codecs/libopus/celt/celt_decoder.c new file mode 100644 index 0000000000..929d1d441b --- /dev/null +++ b/lib/rbcodec/codecs/libopus/celt/celt_decoder.c | |||
@@ -0,0 +1,1207 @@ | |||
1 | /* Copyright (c) 2007-2008 CSIRO | ||
2 | Copyright (c) 2007-2010 Xiph.Org Foundation | ||
3 | Copyright (c) 2008 Gregory Maxwell | ||
4 | Written by Jean-Marc Valin and Gregory Maxwell */ | ||
5 | /* | ||
6 | Redistribution and use in source and binary forms, with or without | ||
7 | modification, are permitted provided that the following conditions | ||
8 | are met: | ||
9 | |||
10 | - Redistributions of source code must retain the above copyright | ||
11 | notice, this list of conditions and the following disclaimer. | ||
12 | |||
13 | - Redistributions in binary form must reproduce the above copyright | ||
14 | notice, this list of conditions and the following disclaimer in the | ||
15 | documentation and/or other materials provided with the distribution. | ||
16 | |||
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
18 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER | ||
21 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
22 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
23 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
24 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
25 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
26 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #ifdef HAVE_CONFIG_H | ||
31 | #include "config.h" | ||
32 | #endif | ||
33 | |||
34 | #define CELT_DECODER_C | ||
35 | |||
36 | #include "cpu_support.h" | ||
37 | #include "os_support.h" | ||
38 | #include "mdct.h" | ||
39 | #include <math.h> | ||
40 | #include "celt.h" | ||
41 | #include "pitch.h" | ||
42 | #include "bands.h" | ||
43 | #include "modes.h" | ||
44 | #include "entcode.h" | ||
45 | #include "quant_bands.h" | ||
46 | #include "rate.h" | ||
47 | #include "stack_alloc.h" | ||
48 | #include "mathops.h" | ||
49 | #include "float_cast.h" | ||
50 | #include <stdarg.h> | ||
51 | #include "celt_lpc.h" | ||
52 | #include "vq.h" | ||
53 | |||
54 | /**********************************************************************/ | ||
55 | /* */ | ||
56 | /* DECODER */ | ||
57 | /* */ | ||
58 | /**********************************************************************/ | ||
59 | #define DECODE_BUFFER_SIZE 2048 | ||
60 | |||
61 | /** Decoder state | ||
62 | @brief Decoder state | ||
63 | */ | ||
64 | struct OpusCustomDecoder { | ||
65 | const OpusCustomMode *mode; | ||
66 | int overlap; | ||
67 | int channels; | ||
68 | int stream_channels; | ||
69 | |||
70 | int downsample; | ||
71 | int start, end; | ||
72 | int signalling; | ||
73 | int arch; | ||
74 | |||
75 | /* Everything beyond this point gets cleared on a reset */ | ||
76 | #define DECODER_RESET_START rng | ||
77 | |||
78 | opus_uint32 rng; | ||
79 | int error; | ||
80 | int last_pitch_index; | ||
81 | int loss_count; | ||
82 | int postfilter_period; | ||
83 | int postfilter_period_old; | ||
84 | opus_val16 postfilter_gain; | ||
85 | opus_val16 postfilter_gain_old; | ||
86 | int postfilter_tapset; | ||
87 | int postfilter_tapset_old; | ||
88 | |||
89 | celt_sig preemph_memD[2]; | ||
90 | |||
91 | celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */ | ||
92 | /* opus_val16 lpc[], Size = channels*LPC_ORDER */ | ||
93 | /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */ | ||
94 | /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */ | ||
95 | /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */ | ||
96 | /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */ | ||
97 | }; | ||
98 | |||
99 | int celt_decoder_get_size(int channels) | ||
100 | { | ||
101 | const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL); | ||
102 | return opus_custom_decoder_get_size(mode, channels); | ||
103 | } | ||
104 | |||
105 | OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels) | ||
106 | { | ||
107 | int size = sizeof(struct CELTDecoder) | ||
108 | + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig) | ||
109 | + channels*LPC_ORDER*sizeof(opus_val16) | ||
110 | + 4*2*mode->nbEBands*sizeof(opus_val16); | ||
111 | return size; | ||
112 | } | ||
113 | |||
114 | #ifdef CUSTOM_MODES | ||
115 | CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error) | ||
116 | { | ||
117 | int ret; | ||
118 | CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels)); | ||
119 | ret = opus_custom_decoder_init(st, mode, channels); | ||
120 | if (ret != OPUS_OK) | ||
121 | { | ||
122 | opus_custom_decoder_destroy(st); | ||
123 | st = NULL; | ||
124 | } | ||
125 | if (error) | ||
126 | *error = ret; | ||
127 | return st; | ||
128 | } | ||
129 | #endif /* CUSTOM_MODES */ | ||
130 | |||
131 | int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels) | ||
132 | { | ||
133 | int ret; | ||
134 | ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels); | ||
135 | if (ret != OPUS_OK) | ||
136 | return ret; | ||
137 | st->downsample = resampling_factor(sampling_rate); | ||
138 | if (st->downsample==0) | ||
139 | return OPUS_BAD_ARG; | ||
140 | else | ||
141 | return OPUS_OK; | ||
142 | } | ||
143 | |||
144 | OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels) | ||
145 | { | ||
146 | if (channels < 0 || channels > 2) | ||
147 | return OPUS_BAD_ARG; | ||
148 | |||
149 | if (st==NULL) | ||
150 | return OPUS_ALLOC_FAIL; | ||
151 | |||
152 | OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels)); | ||
153 | |||
154 | st->mode = mode; | ||
155 | st->overlap = mode->overlap; | ||
156 | st->stream_channels = st->channels = channels; | ||
157 | |||
158 | st->downsample = 1; | ||
159 | st->start = 0; | ||
160 | st->end = st->mode->effEBands; | ||
161 | st->signalling = 1; | ||
162 | st->arch = opus_select_arch(); | ||
163 | |||
164 | st->loss_count = 0; | ||
165 | |||
166 | opus_custom_decoder_ctl(st, OPUS_RESET_STATE); | ||
167 | |||
168 | return OPUS_OK; | ||
169 | } | ||
170 | |||
171 | #ifdef CUSTOM_MODES | ||
172 | void opus_custom_decoder_destroy(CELTDecoder *st) | ||
173 | { | ||
174 | opus_free(st); | ||
175 | } | ||
176 | #endif /* CUSTOM_MODES */ | ||
177 | |||
178 | static inline opus_val16 SIG2WORD16(celt_sig x) | ||
179 | { | ||
180 | #ifdef FIXED_POINT | ||
181 | x = PSHR32(x, SIG_SHIFT); | ||
182 | x = MAX32(x, -32768); | ||
183 | x = MIN32(x, 32767); | ||
184 | return EXTRACT16(x); | ||
185 | #else | ||
186 | return (opus_val16)x; | ||
187 | #endif | ||
188 | } | ||
189 | |||
190 | #ifndef RESYNTH | ||
191 | static | ||
192 | #endif | ||
193 | void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem, celt_sig * OPUS_RESTRICT scratch) | ||
194 | { | ||
195 | int c; | ||
196 | int Nd; | ||
197 | int apply_downsampling=0; | ||
198 | opus_val16 coef0; | ||
199 | |||
200 | coef0 = coef[0]; | ||
201 | Nd = N/downsample; | ||
202 | c=0; do { | ||
203 | int j; | ||
204 | celt_sig * OPUS_RESTRICT x; | ||
205 | opus_val16 * OPUS_RESTRICT y; | ||
206 | celt_sig m = mem[c]; | ||
207 | x =in[c]; | ||
208 | y = pcm+c; | ||
209 | #ifdef CUSTOM_MODES | ||
210 | if (coef[1] != 0) | ||
211 | { | ||
212 | opus_val16 coef1 = coef[1]; | ||
213 | opus_val16 coef3 = coef[3]; | ||
214 | for (j=0;j<N;j++) | ||
215 | { | ||
216 | celt_sig tmp = x[j] + m; | ||
217 | m = MULT16_32_Q15(coef0, tmp) | ||
218 | - MULT16_32_Q15(coef1, x[j]); | ||
219 | tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2); | ||
220 | scratch[j] = tmp; | ||
221 | } | ||
222 | apply_downsampling=1; | ||
223 | } else | ||
224 | #endif | ||
225 | if (downsample>1) | ||
226 | { | ||
227 | /* Shortcut for the standard (non-custom modes) case */ | ||
228 | for (j=0;j<N;j++) | ||
229 | { | ||
230 | celt_sig tmp = x[j] + m; | ||
231 | m = MULT16_32_Q15(coef0, tmp); | ||
232 | scratch[j] = tmp; | ||
233 | } | ||
234 | apply_downsampling=1; | ||
235 | } else { | ||
236 | /* Shortcut for the standard (non-custom modes) case */ | ||
237 | for (j=0;j<N;j++) | ||
238 | { | ||
239 | celt_sig tmp = x[j] + m + VERY_SMALL; | ||
240 | m = MULT16_32_Q15(coef0, tmp); | ||
241 | y[j*C] = SCALEOUT(SIG2WORD16(tmp)); | ||
242 | } | ||
243 | } | ||
244 | mem[c] = m; | ||
245 | |||
246 | if (apply_downsampling) | ||
247 | { | ||
248 | /* Perform down-sampling */ | ||
249 | for (j=0;j<Nd;j++) | ||
250 | y[j*C] = SCALEOUT(SIG2WORD16(scratch[j*downsample])); | ||
251 | } | ||
252 | } while (++c<C); | ||
253 | } | ||
254 | |||
255 | /** Compute the IMDCT and apply window for all sub-frames and | ||
256 | all channels in a frame */ | ||
257 | #ifndef RESYNTH | ||
258 | static | ||
259 | #endif | ||
260 | void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X, | ||
261 | celt_sig * OPUS_RESTRICT out_mem[], int C, int LM) | ||
262 | { | ||
263 | int b, c; | ||
264 | int B; | ||
265 | int N; | ||
266 | int shift; | ||
267 | const int overlap = OVERLAP(mode); | ||
268 | |||
269 | if (shortBlocks) | ||
270 | { | ||
271 | B = shortBlocks; | ||
272 | N = mode->shortMdctSize; | ||
273 | shift = mode->maxLM; | ||
274 | } else { | ||
275 | B = 1; | ||
276 | N = mode->shortMdctSize<<LM; | ||
277 | shift = mode->maxLM-LM; | ||
278 | } | ||
279 | c=0; do { | ||
280 | /* IMDCT on the interleaved the sub-frames, overlap-add is performed by the IMDCT */ | ||
281 | for (b=0;b<B;b++) | ||
282 | clt_mdct_backward(&mode->mdct, &X[b+c*N*B], out_mem[c]+N*b, mode->window, overlap, shift, B); | ||
283 | } while (++c<C); | ||
284 | } | ||
285 | |||
286 | static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec) | ||
287 | { | ||
288 | int i, curr, tf_select; | ||
289 | int tf_select_rsv; | ||
290 | int tf_changed; | ||
291 | int logp; | ||
292 | opus_uint32 budget; | ||
293 | opus_uint32 tell; | ||
294 | |||
295 | budget = dec->storage*8; | ||
296 | tell = ec_tell(dec); | ||
297 | logp = isTransient ? 2 : 4; | ||
298 | tf_select_rsv = LM>0 && tell+logp+1<=budget; | ||
299 | budget -= tf_select_rsv; | ||
300 | tf_changed = curr = 0; | ||
301 | for (i=start;i<end;i++) | ||
302 | { | ||
303 | if (tell+logp<=budget) | ||
304 | { | ||
305 | curr ^= ec_dec_bit_logp(dec, logp); | ||
306 | tell = ec_tell(dec); | ||
307 | tf_changed |= curr; | ||
308 | } | ||
309 | tf_res[i] = curr; | ||
310 | logp = isTransient ? 4 : 5; | ||
311 | } | ||
312 | tf_select = 0; | ||
313 | if (tf_select_rsv && | ||
314 | tf_select_table[LM][4*isTransient+0+tf_changed] != | ||
315 | tf_select_table[LM][4*isTransient+2+tf_changed]) | ||
316 | { | ||
317 | tf_select = ec_dec_bit_logp(dec, 1); | ||
318 | } | ||
319 | for (i=start;i<end;i++) | ||
320 | { | ||
321 | tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]]; | ||
322 | } | ||
323 | } | ||
324 | |||
325 | /* The maximum pitch lag to allow in the pitch-based PLC. It's possible to save | ||
326 | CPU time in the PLC pitch search by making this smaller than MAX_PERIOD. The | ||
327 | current value corresponds to a pitch of 66.67 Hz. */ | ||
328 | #define PLC_PITCH_LAG_MAX (720) | ||
329 | /* The minimum pitch lag to allow in the pitch-based PLC. This corresponds to a | ||
330 | pitch of 480 Hz. */ | ||
331 | #define PLC_PITCH_LAG_MIN (100) | ||
332 | |||
333 | static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, opus_val16 * OPUS_RESTRICT pcm, int N, int LM) | ||
334 | { | ||
335 | int c; | ||
336 | int i; | ||
337 | const int C = st->channels; | ||
338 | celt_sig *decode_mem[2]; | ||
339 | celt_sig *out_syn[2]; | ||
340 | opus_val16 *lpc; | ||
341 | opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; | ||
342 | const OpusCustomMode *mode; | ||
343 | int nbEBands; | ||
344 | int overlap; | ||
345 | int start; | ||
346 | int downsample; | ||
347 | int loss_count; | ||
348 | int noise_based; | ||
349 | const opus_int16 *eBands; | ||
350 | VARDECL(celt_sig, scratch); | ||
351 | SAVE_STACK; | ||
352 | |||
353 | mode = st->mode; | ||
354 | nbEBands = mode->nbEBands; | ||
355 | overlap = mode->overlap; | ||
356 | eBands = mode->eBands; | ||
357 | |||
358 | c=0; do { | ||
359 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+overlap); | ||
360 | out_syn[c] = decode_mem[c]+DECODE_BUFFER_SIZE-N; | ||
361 | } while (++c<C); | ||
362 | lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+overlap)*C); | ||
363 | oldBandE = lpc+C*LPC_ORDER; | ||
364 | oldLogE = oldBandE + 2*nbEBands; | ||
365 | oldLogE2 = oldLogE + 2*nbEBands; | ||
366 | backgroundLogE = oldLogE2 + 2*nbEBands; | ||
367 | |||
368 | loss_count = st->loss_count; | ||
369 | start = st->start; | ||
370 | downsample = st->downsample; | ||
371 | noise_based = loss_count >= 5 || start != 0; | ||
372 | ALLOC(scratch, noise_based?N*C:N, celt_sig); | ||
373 | if (noise_based) | ||
374 | { | ||
375 | /* Noise-based PLC/CNG */ | ||
376 | celt_sig *freq; | ||
377 | VARDECL(celt_norm, X); | ||
378 | opus_uint32 seed; | ||
379 | opus_val16 *plcLogE; | ||
380 | int end; | ||
381 | int effEnd; | ||
382 | |||
383 | end = st->end; | ||
384 | effEnd = IMAX(start, IMIN(end, mode->effEBands)); | ||
385 | |||
386 | /* Share the interleaved signal MDCT coefficient buffer with the | ||
387 | deemphasis scratch buffer. */ | ||
388 | freq = scratch; | ||
389 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ | ||
390 | |||
391 | if (loss_count >= 5) | ||
392 | plcLogE = backgroundLogE; | ||
393 | else { | ||
394 | /* Energy decay */ | ||
395 | opus_val16 decay = loss_count==0 ? | ||
396 | QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT); | ||
397 | c=0; do | ||
398 | { | ||
399 | for (i=start;i<end;i++) | ||
400 | oldBandE[c*nbEBands+i] -= decay; | ||
401 | } while (++c<C); | ||
402 | plcLogE = oldBandE; | ||
403 | } | ||
404 | seed = st->rng; | ||
405 | for (c=0;c<C;c++) | ||
406 | { | ||
407 | for (i=start;i<effEnd;i++) | ||
408 | { | ||
409 | int j; | ||
410 | int boffs; | ||
411 | int blen; | ||
412 | boffs = N*c+(eBands[i]<<LM); | ||
413 | blen = (eBands[i+1]-eBands[i])<<LM; | ||
414 | for (j=0;j<blen;j++) | ||
415 | { | ||
416 | seed = celt_lcg_rand(seed); | ||
417 | X[boffs+j] = (celt_norm)((opus_int32)seed>>20); | ||
418 | } | ||
419 | renormalise_vector(X+boffs, blen, Q15ONE); | ||
420 | } | ||
421 | } | ||
422 | st->rng = seed; | ||
423 | |||
424 | denormalise_bands(mode, X, freq, plcLogE, start, effEnd, C, 1<<LM); | ||
425 | |||
426 | c=0; do { | ||
427 | int bound = eBands[effEnd]<<LM; | ||
428 | if (downsample!=1) | ||
429 | bound = IMIN(bound, N/downsample); | ||
430 | for (i=bound;i<N;i++) | ||
431 | freq[c*N+i] = 0; | ||
432 | } while (++c<C); | ||
433 | c=0; do { | ||
434 | OPUS_MOVE(decode_mem[c], decode_mem[c]+N, | ||
435 | DECODE_BUFFER_SIZE-N+(overlap>>1)); | ||
436 | } while (++c<C); | ||
437 | compute_inv_mdcts(mode, 0, freq, out_syn, C, LM); | ||
438 | } else { | ||
439 | /* Pitch-based PLC */ | ||
440 | const opus_val16 *window; | ||
441 | opus_val16 fade = Q15ONE; | ||
442 | int pitch_index; | ||
443 | VARDECL(opus_val32, etmp); | ||
444 | VARDECL(opus_val16, exc); | ||
445 | |||
446 | if (loss_count == 0) | ||
447 | { | ||
448 | VARDECL( opus_val16, lp_pitch_buf ); | ||
449 | ALLOC( lp_pitch_buf, DECODE_BUFFER_SIZE>>1, opus_val16 ); | ||
450 | pitch_downsample(decode_mem, lp_pitch_buf, DECODE_BUFFER_SIZE, C); | ||
451 | pitch_search(lp_pitch_buf+(PLC_PITCH_LAG_MAX>>1), lp_pitch_buf, | ||
452 | DECODE_BUFFER_SIZE-PLC_PITCH_LAG_MAX, | ||
453 | PLC_PITCH_LAG_MAX-PLC_PITCH_LAG_MIN, &pitch_index); | ||
454 | pitch_index = PLC_PITCH_LAG_MAX-pitch_index; | ||
455 | st->last_pitch_index = pitch_index; | ||
456 | } else { | ||
457 | pitch_index = st->last_pitch_index; | ||
458 | fade = QCONST16(.8f,15); | ||
459 | } | ||
460 | |||
461 | ALLOC(etmp, overlap, opus_val32); | ||
462 | ALLOC(exc, MAX_PERIOD, opus_val16); | ||
463 | window = mode->window; | ||
464 | c=0; do { | ||
465 | opus_val16 decay; | ||
466 | opus_val16 attenuation; | ||
467 | opus_val32 S1=0; | ||
468 | celt_sig *buf; | ||
469 | int extrapolation_offset; | ||
470 | int extrapolation_len; | ||
471 | int exc_length; | ||
472 | int j; | ||
473 | |||
474 | buf = decode_mem[c]; | ||
475 | for (i=0;i<MAX_PERIOD;i++) { | ||
476 | exc[i] = ROUND16(buf[DECODE_BUFFER_SIZE-MAX_PERIOD+i], SIG_SHIFT); | ||
477 | } | ||
478 | |||
479 | if (loss_count == 0) | ||
480 | { | ||
481 | opus_val32 ac[LPC_ORDER+1]; | ||
482 | /* Compute LPC coefficients for the last MAX_PERIOD samples before | ||
483 | the first loss so we can work in the excitation-filter domain. */ | ||
484 | _celt_autocorr(exc, ac, window, overlap, LPC_ORDER, MAX_PERIOD); | ||
485 | /* Add a noise floor of -40 dB. */ | ||
486 | #ifdef FIXED_POINT | ||
487 | ac[0] += SHR32(ac[0],13); | ||
488 | #else | ||
489 | ac[0] *= 1.0001f; | ||
490 | #endif | ||
491 | /* Use lag windowing to stabilize the Levinson-Durbin recursion. */ | ||
492 | for (i=1;i<=LPC_ORDER;i++) | ||
493 | { | ||
494 | /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/ | ||
495 | #ifdef FIXED_POINT | ||
496 | ac[i] -= MULT16_32_Q15(2*i*i, ac[i]); | ||
497 | #else | ||
498 | ac[i] -= ac[i]*(0.008f*0.008f)*i*i; | ||
499 | #endif | ||
500 | } | ||
501 | _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER); | ||
502 | } | ||
503 | /* We want the excitation for 2 pitch periods in order to look for a | ||
504 | decaying signal, but we can't get more than MAX_PERIOD. */ | ||
505 | exc_length = IMIN(2*pitch_index, MAX_PERIOD); | ||
506 | /* Initialize the LPC history with the samples just before the start | ||
507 | of the region for which we're computing the excitation. */ | ||
508 | { | ||
509 | opus_val16 lpc_mem[LPC_ORDER]; | ||
510 | for (i=0;i<LPC_ORDER;i++) | ||
511 | { | ||
512 | lpc_mem[i] = | ||
513 | ROUND16(buf[DECODE_BUFFER_SIZE-exc_length-1-i], SIG_SHIFT); | ||
514 | } | ||
515 | /* Compute the excitation for exc_length samples before the loss. */ | ||
516 | celt_fir(exc+MAX_PERIOD-exc_length, lpc+c*LPC_ORDER, | ||
517 | exc+MAX_PERIOD-exc_length, exc_length, LPC_ORDER, lpc_mem); | ||
518 | } | ||
519 | |||
520 | /* Check if the waveform is decaying, and if so how fast. | ||
521 | We do this to avoid adding energy when concealing in a segment | ||
522 | with decaying energy. */ | ||
523 | { | ||
524 | opus_val32 E1=1, E2=1; | ||
525 | int decay_length; | ||
526 | #ifdef FIXED_POINT | ||
527 | int shift = IMAX(0,2*celt_zlog2(celt_maxabs16(&exc[MAX_PERIOD-exc_length], exc_length))-20); | ||
528 | #endif | ||
529 | decay_length = exc_length>>1; | ||
530 | for (i=0;i<decay_length;i++) | ||
531 | { | ||
532 | opus_val16 e; | ||
533 | e = exc[MAX_PERIOD-decay_length+i]; | ||
534 | E1 += SHR32(MULT16_16(e, e), shift); | ||
535 | e = exc[MAX_PERIOD-2*decay_length+i]; | ||
536 | E2 += SHR32(MULT16_16(e, e), shift); | ||
537 | } | ||
538 | E1 = MIN32(E1, E2); | ||
539 | decay = celt_sqrt(frac_div32(SHR32(E1, 1), E2)); | ||
540 | } | ||
541 | |||
542 | /* Move the decoder memory one frame to the left to give us room to | ||
543 | add the data for the new frame. We ignore the overlap that extends | ||
544 | past the end of the buffer, because we aren't going to use it. */ | ||
545 | OPUS_MOVE(buf, buf+N, DECODE_BUFFER_SIZE-N); | ||
546 | |||
547 | /* Extrapolate from the end of the excitation with a period of | ||
548 | "pitch_index", scaling down each period by an additional factor of | ||
549 | "decay". */ | ||
550 | extrapolation_offset = MAX_PERIOD-pitch_index; | ||
551 | /* We need to extrapolate enough samples to cover a complete MDCT | ||
552 | window (including overlap/2 samples on both sides). */ | ||
553 | extrapolation_len = N+overlap; | ||
554 | /* We also apply fading if this is not the first loss. */ | ||
555 | attenuation = MULT16_16_Q15(fade, decay); | ||
556 | for (i=j=0;i<extrapolation_len;i++,j++) | ||
557 | { | ||
558 | opus_val16 tmp; | ||
559 | if (j >= pitch_index) { | ||
560 | j -= pitch_index; | ||
561 | attenuation = MULT16_16_Q15(attenuation, decay); | ||
562 | } | ||
563 | buf[DECODE_BUFFER_SIZE-N+i] = | ||
564 | SHL32(EXTEND32(MULT16_16_Q15(attenuation, | ||
565 | exc[extrapolation_offset+j])), SIG_SHIFT); | ||
566 | /* Compute the energy of the previously decoded signal whose | ||
567 | excitation we're copying. */ | ||
568 | tmp = ROUND16( | ||
569 | buf[DECODE_BUFFER_SIZE-MAX_PERIOD-N+extrapolation_offset+j], | ||
570 | SIG_SHIFT); | ||
571 | S1 += SHR32(MULT16_16(tmp, tmp), 8); | ||
572 | } | ||
573 | |||
574 | { | ||
575 | opus_val16 lpc_mem[LPC_ORDER]; | ||
576 | /* Copy the last decoded samples (prior to the overlap region) to | ||
577 | synthesis filter memory so we can have a continuous signal. */ | ||
578 | for (i=0;i<LPC_ORDER;i++) | ||
579 | lpc_mem[i] = ROUND16(buf[DECODE_BUFFER_SIZE-N-1-i], SIG_SHIFT); | ||
580 | /* Apply the synthesis filter to convert the excitation back into | ||
581 | the signal domain. */ | ||
582 | celt_iir(buf+DECODE_BUFFER_SIZE-N, lpc+c*LPC_ORDER, | ||
583 | buf+DECODE_BUFFER_SIZE-N, extrapolation_len, LPC_ORDER, | ||
584 | lpc_mem); | ||
585 | } | ||
586 | |||
587 | /* Check if the synthesis energy is higher than expected, which can | ||
588 | happen with the signal changes during our window. If so, | ||
589 | attenuate. */ | ||
590 | { | ||
591 | opus_val32 S2=0; | ||
592 | for (i=0;i<extrapolation_len;i++) | ||
593 | { | ||
594 | opus_val16 tmp = ROUND16(buf[DECODE_BUFFER_SIZE-N+i], SIG_SHIFT); | ||
595 | S2 += SHR32(MULT16_16(tmp, tmp), 8); | ||
596 | } | ||
597 | /* This checks for an "explosion" in the synthesis. */ | ||
598 | #ifdef FIXED_POINT | ||
599 | if (!(S1 > SHR32(S2,2))) | ||
600 | #else | ||
601 | /* The float test is written this way to catch NaNs in the output | ||
602 | of the IIR filter at the same time. */ | ||
603 | if (!(S1 > 0.2f*S2)) | ||
604 | #endif | ||
605 | { | ||
606 | for (i=0;i<extrapolation_len;i++) | ||
607 | buf[DECODE_BUFFER_SIZE-N+i] = 0; | ||
608 | } else if (S1 < S2) | ||
609 | { | ||
610 | opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1)); | ||
611 | for (i=0;i<overlap;i++) | ||
612 | { | ||
613 | opus_val16 tmp_g = Q15ONE | ||
614 | - MULT16_16_Q15(window[i], Q15ONE-ratio); | ||
615 | buf[DECODE_BUFFER_SIZE-N+i] = | ||
616 | MULT16_32_Q15(tmp_g, buf[DECODE_BUFFER_SIZE-N+i]); | ||
617 | } | ||
618 | for (i=overlap;i<extrapolation_len;i++) | ||
619 | { | ||
620 | buf[DECODE_BUFFER_SIZE-N+i] = | ||
621 | MULT16_32_Q15(ratio, buf[DECODE_BUFFER_SIZE-N+i]); | ||
622 | } | ||
623 | } | ||
624 | } | ||
625 | |||
626 | /* Apply the pre-filter to the MDCT overlap for the next frame because | ||
627 | the post-filter will be re-applied in the decoder after the MDCT | ||
628 | overlap. */ | ||
629 | comb_filter(etmp, buf+DECODE_BUFFER_SIZE, | ||
630 | st->postfilter_period, st->postfilter_period, overlap, | ||
631 | -st->postfilter_gain, -st->postfilter_gain, | ||
632 | st->postfilter_tapset, st->postfilter_tapset, NULL, 0); | ||
633 | |||
634 | /* Simulate TDAC on the concealed audio so that it blends with the | ||
635 | MDCT of the next frame. */ | ||
636 | for (i=0;i<overlap/2;i++) | ||
637 | { | ||
638 | buf[DECODE_BUFFER_SIZE+i] = | ||
639 | MULT16_32_Q15(window[i], etmp[overlap-1-i]) | ||
640 | + MULT16_32_Q15(window[overlap-i-1], etmp[i]); | ||
641 | } | ||
642 | } while (++c<C); | ||
643 | } | ||
644 | |||
645 | deemphasis(out_syn, pcm, N, C, downsample, | ||
646 | mode->preemph, st->preemph_memD, scratch); | ||
647 | |||
648 | st->loss_count = loss_count+1; | ||
649 | |||
650 | RESTORE_STACK; | ||
651 | } | ||
652 | |||
653 | #define FREQ_X_BUF_SIZE (2*8*120) /* stereo * nbShortMdcts * shortMdctSize */ | ||
654 | static celt_sig s_freq[FREQ_X_BUF_SIZE] IBSS_ATTR MEM_ALIGN_ATTR; /* 7680 byte */ | ||
655 | static celt_norm s_X[FREQ_X_BUF_SIZE] IBSS_ATTR MEM_ALIGN_ATTR; /* 3840 byte */ | ||
656 | int celt_decode_with_ec(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec) | ||
657 | { | ||
658 | int c, i, N; | ||
659 | int spread_decision; | ||
660 | opus_int32 bits; | ||
661 | ec_dec _dec; | ||
662 | VARDECL(celt_sig, freq); | ||
663 | VARDECL(celt_norm, X); | ||
664 | VARDECL(int, fine_quant); | ||
665 | VARDECL(int, pulses); | ||
666 | VARDECL(int, cap); | ||
667 | VARDECL(int, offsets); | ||
668 | VARDECL(int, fine_priority); | ||
669 | VARDECL(int, tf_res); | ||
670 | VARDECL(unsigned char, collapse_masks); | ||
671 | celt_sig *out_mem[2]; | ||
672 | celt_sig *decode_mem[2]; | ||
673 | celt_sig *out_syn[2]; | ||
674 | opus_val16 *lpc; | ||
675 | opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; | ||
676 | |||
677 | int shortBlocks; | ||
678 | int isTransient; | ||
679 | int intra_ener; | ||
680 | const int CC = st->channels; | ||
681 | int LM, M; | ||
682 | int effEnd; | ||
683 | int codedBands; | ||
684 | int alloc_trim; | ||
685 | int postfilter_pitch; | ||
686 | opus_val16 postfilter_gain; | ||
687 | int intensity=0; | ||
688 | int dual_stereo=0; | ||
689 | opus_int32 total_bits; | ||
690 | opus_int32 balance; | ||
691 | opus_int32 tell; | ||
692 | int dynalloc_logp; | ||
693 | int postfilter_tapset; | ||
694 | int anti_collapse_rsv; | ||
695 | int anti_collapse_on=0; | ||
696 | int silence; | ||
697 | int C = st->stream_channels; | ||
698 | const OpusCustomMode *mode; | ||
699 | int nbEBands; | ||
700 | int overlap; | ||
701 | const opus_int16 *eBands; | ||
702 | ALLOC_STACK; | ||
703 | |||
704 | mode = st->mode; | ||
705 | nbEBands = mode->nbEBands; | ||
706 | overlap = mode->overlap; | ||
707 | eBands = mode->eBands; | ||
708 | frame_size *= st->downsample; | ||
709 | |||
710 | c=0; do { | ||
711 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+overlap); | ||
712 | out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD; | ||
713 | } while (++c<CC); | ||
714 | lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+overlap)*CC); | ||
715 | oldBandE = lpc+CC*LPC_ORDER; | ||
716 | oldLogE = oldBandE + 2*nbEBands; | ||
717 | oldLogE2 = oldLogE + 2*nbEBands; | ||
718 | backgroundLogE = oldLogE2 + 2*nbEBands; | ||
719 | |||
720 | #ifdef CUSTOM_MODES | ||
721 | if (st->signalling && data!=NULL) | ||
722 | { | ||
723 | int data0=data[0]; | ||
724 | /* Convert "standard mode" to Opus header */ | ||
725 | if (mode->Fs==48000 && mode->shortMdctSize==120) | ||
726 | { | ||
727 | data0 = fromOpus(data0); | ||
728 | if (data0<0) | ||
729 | return OPUS_INVALID_PACKET; | ||
730 | } | ||
731 | st->end = IMAX(1, mode->effEBands-2*(data0>>5)); | ||
732 | LM = (data0>>3)&0x3; | ||
733 | C = 1 + ((data0>>2)&0x1); | ||
734 | data++; | ||
735 | len--; | ||
736 | if (LM>mode->maxLM) | ||
737 | return OPUS_INVALID_PACKET; | ||
738 | if (frame_size < mode->shortMdctSize<<LM) | ||
739 | return OPUS_BUFFER_TOO_SMALL; | ||
740 | else | ||
741 | frame_size = mode->shortMdctSize<<LM; | ||
742 | } else { | ||
743 | #else | ||
744 | { | ||
745 | #endif | ||
746 | for (LM=0;LM<=mode->maxLM;LM++) | ||
747 | if (mode->shortMdctSize<<LM==frame_size) | ||
748 | break; | ||
749 | if (LM>mode->maxLM) | ||
750 | return OPUS_BAD_ARG; | ||
751 | } | ||
752 | M=1<<LM; | ||
753 | |||
754 | if (len<0 || len>1275 || pcm==NULL) | ||
755 | return OPUS_BAD_ARG; | ||
756 | |||
757 | N = M*mode->shortMdctSize; | ||
758 | |||
759 | effEnd = st->end; | ||
760 | if (effEnd > mode->effEBands) | ||
761 | effEnd = mode->effEBands; | ||
762 | |||
763 | if (data == NULL || len<=1) | ||
764 | { | ||
765 | celt_decode_lost(st, pcm, N, LM); | ||
766 | RESTORE_STACK; | ||
767 | return frame_size/st->downsample; | ||
768 | } | ||
769 | |||
770 | if (dec == NULL) | ||
771 | { | ||
772 | ec_dec_init(&_dec,(unsigned char*)data,len); | ||
773 | dec = &_dec; | ||
774 | } | ||
775 | |||
776 | if (C==1) | ||
777 | { | ||
778 | for (i=0;i<nbEBands;i++) | ||
779 | oldBandE[i]=MAX16(oldBandE[i],oldBandE[nbEBands+i]); | ||
780 | } | ||
781 | |||
782 | total_bits = len*8; | ||
783 | tell = ec_tell(dec); | ||
784 | |||
785 | if (tell >= total_bits) | ||
786 | silence = 1; | ||
787 | else if (tell==1) | ||
788 | silence = ec_dec_bit_logp(dec, 15); | ||
789 | else | ||
790 | silence = 0; | ||
791 | if (silence) | ||
792 | { | ||
793 | /* Pretend we've read all the remaining bits */ | ||
794 | tell = len*8; | ||
795 | dec->nbits_total+=tell-ec_tell(dec); | ||
796 | } | ||
797 | |||
798 | postfilter_gain = 0; | ||
799 | postfilter_pitch = 0; | ||
800 | postfilter_tapset = 0; | ||
801 | if (st->start==0 && tell+16 <= total_bits) | ||
802 | { | ||
803 | if(ec_dec_bit_logp(dec, 1)) | ||
804 | { | ||
805 | int qg, octave; | ||
806 | octave = ec_dec_uint(dec, 6); | ||
807 | postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1; | ||
808 | qg = ec_dec_bits(dec, 3); | ||
809 | if (ec_tell(dec)+2<=total_bits) | ||
810 | postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2); | ||
811 | postfilter_gain = QCONST16(.09375f,15)*(qg+1); | ||
812 | } | ||
813 | tell = ec_tell(dec); | ||
814 | } | ||
815 | |||
816 | if (LM > 0 && tell+3 <= total_bits) | ||
817 | { | ||
818 | isTransient = ec_dec_bit_logp(dec, 3); | ||
819 | tell = ec_tell(dec); | ||
820 | } | ||
821 | else | ||
822 | isTransient = 0; | ||
823 | |||
824 | if (isTransient) | ||
825 | shortBlocks = M; | ||
826 | else | ||
827 | shortBlocks = 0; | ||
828 | |||
829 | /* Decode the global flags (first symbols in the stream) */ | ||
830 | intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0; | ||
831 | /* Get band energies */ | ||
832 | unquant_coarse_energy(mode, st->start, st->end, oldBandE, | ||
833 | intra_ener, dec, C, LM); | ||
834 | |||
835 | ALLOC(tf_res, nbEBands, int); | ||
836 | tf_decode(st->start, st->end, isTransient, tf_res, LM, dec); | ||
837 | |||
838 | tell = ec_tell(dec); | ||
839 | spread_decision = SPREAD_NORMAL; | ||
840 | if (tell+4 <= total_bits) | ||
841 | spread_decision = ec_dec_icdf(dec, spread_icdf, 5); | ||
842 | |||
843 | ALLOC(cap, nbEBands, int); | ||
844 | |||
845 | init_caps(mode,cap,LM,C); | ||
846 | |||
847 | ALLOC(offsets, nbEBands, int); | ||
848 | |||
849 | dynalloc_logp = 6; | ||
850 | total_bits<<=BITRES; | ||
851 | tell = ec_tell_frac(dec); | ||
852 | for (i=st->start;i<st->end;i++) | ||
853 | { | ||
854 | int width, quanta; | ||
855 | int dynalloc_loop_logp; | ||
856 | int boost; | ||
857 | width = C*(eBands[i+1]-eBands[i])<<LM; | ||
858 | /* quanta is 6 bits, but no more than 1 bit/sample | ||
859 | and no less than 1/8 bit/sample */ | ||
860 | quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); | ||
861 | dynalloc_loop_logp = dynalloc_logp; | ||
862 | boost = 0; | ||
863 | while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i]) | ||
864 | { | ||
865 | int flag; | ||
866 | flag = ec_dec_bit_logp(dec, dynalloc_loop_logp); | ||
867 | tell = ec_tell_frac(dec); | ||
868 | if (!flag) | ||
869 | break; | ||
870 | boost += quanta; | ||
871 | total_bits -= quanta; | ||
872 | dynalloc_loop_logp = 1; | ||
873 | } | ||
874 | offsets[i] = boost; | ||
875 | /* Making dynalloc more likely */ | ||
876 | if (boost>0) | ||
877 | dynalloc_logp = IMAX(2, dynalloc_logp-1); | ||
878 | } | ||
879 | |||
880 | ALLOC(fine_quant, nbEBands, int); | ||
881 | alloc_trim = tell+(6<<BITRES) <= total_bits ? | ||
882 | ec_dec_icdf(dec, trim_icdf, 7) : 5; | ||
883 | |||
884 | bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1; | ||
885 | anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0; | ||
886 | bits -= anti_collapse_rsv; | ||
887 | |||
888 | ALLOC(pulses, nbEBands, int); | ||
889 | ALLOC(fine_priority, nbEBands, int); | ||
890 | |||
891 | codedBands = compute_allocation(mode, st->start, st->end, offsets, cap, | ||
892 | alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses, | ||
893 | fine_quant, fine_priority, C, LM, dec, 0, 0, 0); | ||
894 | |||
895 | unquant_fine_energy(mode, st->start, st->end, oldBandE, fine_quant, dec, C); | ||
896 | |||
897 | /* Decode fixed codebook */ | ||
898 | ALLOC(collapse_masks, C*nbEBands, unsigned char); | ||
899 | /**< Interleaved normalised MDCTs */ | ||
900 | if (FREQ_X_BUF_SIZE >= C*N) | ||
901 | X = s_X; | ||
902 | else | ||
903 | ALLOC(X, C*N, celt_norm); | ||
904 | |||
905 | quant_all_bands(0, mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks, | ||
906 | NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res, | ||
907 | len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng); | ||
908 | |||
909 | if (anti_collapse_rsv > 0) | ||
910 | { | ||
911 | anti_collapse_on = ec_dec_bits(dec, 1); | ||
912 | } | ||
913 | |||
914 | unquant_energy_finalise(mode, st->start, st->end, oldBandE, | ||
915 | fine_quant, fine_priority, len*8-ec_tell(dec), dec, C); | ||
916 | |||
917 | if (anti_collapse_on) | ||
918 | anti_collapse(mode, X, collapse_masks, LM, C, N, | ||
919 | st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng); | ||
920 | |||
921 | /**< Interleaved signal MDCTs */ | ||
922 | if (FREQ_X_BUF_SIZE >= IMAX(CC,C)*N) | ||
923 | freq = s_freq; | ||
924 | else | ||
925 | ALLOC(freq, IMAX(CC,C)*N, celt_sig); | ||
926 | |||
927 | if (silence) | ||
928 | { | ||
929 | for (i=0;i<C*nbEBands;i++) | ||
930 | oldBandE[i] = -QCONST16(28.f,DB_SHIFT); | ||
931 | for (i=0;i<C*N;i++) | ||
932 | freq[i] = 0; | ||
933 | } else { | ||
934 | /* Synthesis */ | ||
935 | denormalise_bands(mode, X, freq, oldBandE, st->start, effEnd, C, M); | ||
936 | } | ||
937 | |||
938 | c=0; do { | ||
939 | OPUS_MOVE(decode_mem[c], decode_mem[c]+N, DECODE_BUFFER_SIZE-N+overlap/2); | ||
940 | } while (++c<CC); | ||
941 | |||
942 | c=0; do { | ||
943 | int bound = M*eBands[effEnd]; | ||
944 | if (st->downsample!=1) | ||
945 | bound = IMIN(bound, N/st->downsample); | ||
946 | for (i=bound;i<N;i++) | ||
947 | freq[c*N+i] = 0; | ||
948 | } while (++c<C); | ||
949 | |||
950 | c=0; do { | ||
951 | out_syn[c] = out_mem[c]+MAX_PERIOD-N; | ||
952 | } while (++c<CC); | ||
953 | |||
954 | if (CC==2&&C==1) | ||
955 | { | ||
956 | for (i=0;i<N;i++) | ||
957 | freq[N+i] = freq[i]; | ||
958 | } | ||
959 | if (CC==1&&C==2) | ||
960 | { | ||
961 | for (i=0;i<N;i++) | ||
962 | freq[i] = HALF32(ADD32(freq[i],freq[N+i])); | ||
963 | } | ||
964 | |||
965 | /* Compute inverse MDCTs */ | ||
966 | compute_inv_mdcts(mode, shortBlocks, freq, out_syn, CC, LM); | ||
967 | |||
968 | c=0; do { | ||
969 | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); | ||
970 | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); | ||
971 | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize, | ||
972 | st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, | ||
973 | mode->window, overlap); | ||
974 | if (LM!=0) | ||
975 | comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize, | ||
976 | st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset, | ||
977 | mode->window, overlap); | ||
978 | |||
979 | } while (++c<CC); | ||
980 | st->postfilter_period_old = st->postfilter_period; | ||
981 | st->postfilter_gain_old = st->postfilter_gain; | ||
982 | st->postfilter_tapset_old = st->postfilter_tapset; | ||
983 | st->postfilter_period = postfilter_pitch; | ||
984 | st->postfilter_gain = postfilter_gain; | ||
985 | st->postfilter_tapset = postfilter_tapset; | ||
986 | if (LM!=0) | ||
987 | { | ||
988 | st->postfilter_period_old = st->postfilter_period; | ||
989 | st->postfilter_gain_old = st->postfilter_gain; | ||
990 | st->postfilter_tapset_old = st->postfilter_tapset; | ||
991 | } | ||
992 | |||
993 | if (C==1) { | ||
994 | for (i=0;i<nbEBands;i++) | ||
995 | oldBandE[nbEBands+i]=oldBandE[i]; | ||
996 | } | ||
997 | |||
998 | /* In case start or end were to change */ | ||
999 | if (!isTransient) | ||
1000 | { | ||
1001 | for (i=0;i<2*nbEBands;i++) | ||
1002 | oldLogE2[i] = oldLogE[i]; | ||
1003 | for (i=0;i<2*nbEBands;i++) | ||
1004 | oldLogE[i] = oldBandE[i]; | ||
1005 | for (i=0;i<2*nbEBands;i++) | ||
1006 | backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]); | ||
1007 | } else { | ||
1008 | for (i=0;i<2*nbEBands;i++) | ||
1009 | oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]); | ||
1010 | } | ||
1011 | c=0; do | ||
1012 | { | ||
1013 | for (i=0;i<st->start;i++) | ||
1014 | { | ||
1015 | oldBandE[c*nbEBands+i]=0; | ||
1016 | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT); | ||
1017 | } | ||
1018 | for (i=st->end;i<nbEBands;i++) | ||
1019 | { | ||
1020 | oldBandE[c*nbEBands+i]=0; | ||
1021 | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT); | ||
1022 | } | ||
1023 | } while (++c<2); | ||
1024 | st->rng = dec->rng; | ||
1025 | |||
1026 | /* We reuse freq[] as scratch space for the de-emphasis */ | ||
1027 | deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, freq); | ||
1028 | st->loss_count = 0; | ||
1029 | RESTORE_STACK; | ||
1030 | if (ec_tell(dec) > 8*len) | ||
1031 | return OPUS_INTERNAL_ERROR; | ||
1032 | if(ec_get_error(dec)) | ||
1033 | st->error = 1; | ||
1034 | return frame_size/st->downsample; | ||
1035 | } | ||
1036 | |||
1037 | |||
1038 | #ifdef CUSTOM_MODES | ||
1039 | |||
1040 | #ifdef FIXED_POINT | ||
1041 | int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size) | ||
1042 | { | ||
1043 | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL); | ||
1044 | } | ||
1045 | |||
1046 | #ifndef DISABLE_FLOAT_API | ||
1047 | int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size) | ||
1048 | { | ||
1049 | int j, ret, C, N; | ||
1050 | VARDECL(opus_int16, out); | ||
1051 | ALLOC_STACK; | ||
1052 | |||
1053 | if (pcm==NULL) | ||
1054 | return OPUS_BAD_ARG; | ||
1055 | |||
1056 | C = st->channels; | ||
1057 | N = frame_size; | ||
1058 | |||
1059 | ALLOC(out, C*N, opus_int16); | ||
1060 | ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL); | ||
1061 | if (ret>0) | ||
1062 | for (j=0;j<C*ret;j++) | ||
1063 | pcm[j]=out[j]*(1.f/32768.f); | ||
1064 | |||
1065 | RESTORE_STACK; | ||
1066 | return ret; | ||
1067 | } | ||
1068 | #endif /* DISABLE_FLOAT_API */ | ||
1069 | |||
1070 | #else | ||
1071 | |||
1072 | int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size) | ||
1073 | { | ||
1074 | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL); | ||
1075 | } | ||
1076 | |||
1077 | int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size) | ||
1078 | { | ||
1079 | int j, ret, C, N; | ||
1080 | VARDECL(celt_sig, out); | ||
1081 | ALLOC_STACK; | ||
1082 | |||
1083 | if (pcm==NULL) | ||
1084 | return OPUS_BAD_ARG; | ||
1085 | |||
1086 | C = st->channels; | ||
1087 | N = frame_size; | ||
1088 | ALLOC(out, C*N, celt_sig); | ||
1089 | |||
1090 | ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL); | ||
1091 | |||
1092 | if (ret>0) | ||
1093 | for (j=0;j<C*ret;j++) | ||
1094 | pcm[j] = FLOAT2INT16 (out[j]); | ||
1095 | |||
1096 | RESTORE_STACK; | ||
1097 | return ret; | ||
1098 | } | ||
1099 | |||
1100 | #endif | ||
1101 | #endif /* CUSTOM_MODES */ | ||
1102 | |||
1103 | int opus_custom_decoder_ctl(CELTDecoder * OPUS_RESTRICT st, int request, ...) | ||
1104 | { | ||
1105 | va_list ap; | ||
1106 | |||
1107 | va_start(ap, request); | ||
1108 | switch (request) | ||
1109 | { | ||
1110 | case CELT_SET_START_BAND_REQUEST: | ||
1111 | { | ||
1112 | opus_int32 value = va_arg(ap, opus_int32); | ||
1113 | if (value<0 || value>=st->mode->nbEBands) | ||
1114 | goto bad_arg; | ||
1115 | st->start = value; | ||
1116 | } | ||
1117 | break; | ||
1118 | case CELT_SET_END_BAND_REQUEST: | ||
1119 | { | ||
1120 | opus_int32 value = va_arg(ap, opus_int32); | ||
1121 | if (value<1 || value>st->mode->nbEBands) | ||
1122 | goto bad_arg; | ||
1123 | st->end = value; | ||
1124 | } | ||
1125 | break; | ||
1126 | case CELT_SET_CHANNELS_REQUEST: | ||
1127 | { | ||
1128 | opus_int32 value = va_arg(ap, opus_int32); | ||
1129 | if (value<1 || value>2) | ||
1130 | goto bad_arg; | ||
1131 | st->stream_channels = value; | ||
1132 | } | ||
1133 | break; | ||
1134 | case CELT_GET_AND_CLEAR_ERROR_REQUEST: | ||
1135 | { | ||
1136 | opus_int32 *value = va_arg(ap, opus_int32*); | ||
1137 | if (value==NULL) | ||
1138 | goto bad_arg; | ||
1139 | *value=st->error; | ||
1140 | st->error = 0; | ||
1141 | } | ||
1142 | break; | ||
1143 | case OPUS_GET_LOOKAHEAD_REQUEST: | ||
1144 | { | ||
1145 | opus_int32 *value = va_arg(ap, opus_int32*); | ||
1146 | if (value==NULL) | ||
1147 | goto bad_arg; | ||
1148 | *value = st->overlap/st->downsample; | ||
1149 | } | ||
1150 | break; | ||
1151 | case OPUS_RESET_STATE: | ||
1152 | { | ||
1153 | int i; | ||
1154 | opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2; | ||
1155 | lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels); | ||
1156 | oldBandE = lpc+st->channels*LPC_ORDER; | ||
1157 | oldLogE = oldBandE + 2*st->mode->nbEBands; | ||
1158 | oldLogE2 = oldLogE + 2*st->mode->nbEBands; | ||
1159 | OPUS_CLEAR((char*)&st->DECODER_RESET_START, | ||
1160 | opus_custom_decoder_get_size(st->mode, st->channels)- | ||
1161 | ((char*)&st->DECODER_RESET_START - (char*)st)); | ||
1162 | for (i=0;i<2*st->mode->nbEBands;i++) | ||
1163 | oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT); | ||
1164 | } | ||
1165 | break; | ||
1166 | case OPUS_GET_PITCH_REQUEST: | ||
1167 | { | ||
1168 | opus_int32 *value = va_arg(ap, opus_int32*); | ||
1169 | if (value==NULL) | ||
1170 | goto bad_arg; | ||
1171 | *value = st->postfilter_period; | ||
1172 | } | ||
1173 | break; | ||
1174 | case CELT_GET_MODE_REQUEST: | ||
1175 | { | ||
1176 | const CELTMode ** value = va_arg(ap, const CELTMode**); | ||
1177 | if (value==0) | ||
1178 | goto bad_arg; | ||
1179 | *value=st->mode; | ||
1180 | } | ||
1181 | break; | ||
1182 | case CELT_SET_SIGNALLING_REQUEST: | ||
1183 | { | ||
1184 | opus_int32 value = va_arg(ap, opus_int32); | ||
1185 | st->signalling = value; | ||
1186 | } | ||
1187 | break; | ||
1188 | case OPUS_GET_FINAL_RANGE_REQUEST: | ||
1189 | { | ||
1190 | opus_uint32 * value = va_arg(ap, opus_uint32 *); | ||
1191 | if (value==0) | ||
1192 | goto bad_arg; | ||
1193 | *value=st->rng; | ||
1194 | } | ||
1195 | break; | ||
1196 | default: | ||
1197 | goto bad_request; | ||
1198 | } | ||
1199 | va_end(ap); | ||
1200 | return OPUS_OK; | ||
1201 | bad_arg: | ||
1202 | va_end(ap); | ||
1203 | return OPUS_BAD_ARG; | ||
1204 | bad_request: | ||
1205 | va_end(ap); | ||
1206 | return OPUS_UNIMPLEMENTED; | ||
1207 | } | ||
diff --git a/lib/rbcodec/codecs/libopus/celt/celt_lpc.c b/lib/rbcodec/codecs/libopus/celt/celt_lpc.c index 66aed1de09..7ffe90a357 100644 --- a/lib/rbcodec/codecs/libopus/celt/celt_lpc.c +++ b/lib/rbcodec/codecs/libopus/celt/celt_lpc.c | |||
@@ -26,12 +26,13 @@ | |||
26 | */ | 26 | */ |
27 | 27 | ||
28 | #ifdef HAVE_CONFIG_H | 28 | #ifdef HAVE_CONFIG_H |
29 | #include "opus_config.h" | 29 | #include "config.h" |
30 | #endif | 30 | #endif |
31 | 31 | ||
32 | #include "celt_lpc.h" | 32 | #include "celt_lpc.h" |
33 | #include "stack_alloc.h" | 33 | #include "stack_alloc.h" |
34 | #include "mathops.h" | 34 | #include "mathops.h" |
35 | #include "pitch.h" | ||
35 | 36 | ||
36 | void _celt_lpc( | 37 | void _celt_lpc( |
37 | opus_val16 *_lpc, /* out: [0...p-1] LPC coefficients */ | 38 | opus_val16 *_lpc, /* out: [0...p-1] LPC coefficients */ |
@@ -87,42 +88,71 @@ int p | |||
87 | #endif | 88 | #endif |
88 | } | 89 | } |
89 | 90 | ||
90 | void celt_fir(const opus_val16 *x, | 91 | void celt_fir(const opus_val16 *_x, |
91 | const opus_val16 *num, | 92 | const opus_val16 *num, |
92 | opus_val16 *y, | 93 | opus_val16 *_y, |
93 | int N, | 94 | int N, |
94 | int ord, | 95 | int ord, |
95 | opus_val16 *mem) | 96 | opus_val16 *mem) |
96 | { | 97 | { |
97 | int i,j; | 98 | int i,j; |
99 | VARDECL(opus_val16, rnum); | ||
100 | VARDECL(opus_val16, x); | ||
101 | SAVE_STACK; | ||
98 | 102 | ||
103 | ALLOC(rnum, ord, opus_val16); | ||
104 | ALLOC(x, N+ord, opus_val16); | ||
105 | for(i=0;i<ord;i++) | ||
106 | rnum[i] = num[ord-i-1]; | ||
107 | for(i=0;i<ord;i++) | ||
108 | x[i] = mem[ord-i-1]; | ||
109 | for (i=0;i<N;i++) | ||
110 | x[i+ord]=_x[i]; | ||
111 | for(i=0;i<ord;i++) | ||
112 | mem[i] = _x[N-i-1]; | ||
113 | #ifdef SMALL_FOOTPRINT | ||
99 | for (i=0;i<N;i++) | 114 | for (i=0;i<N;i++) |
100 | { | 115 | { |
101 | opus_val32 sum = SHL32(EXTEND32(x[i]), SIG_SHIFT); | 116 | opus_val32 sum = SHL32(EXTEND32(_x[i]), SIG_SHIFT); |
102 | for (j=0;j<ord;j++) | 117 | for (j=0;j<ord;j++) |
103 | { | 118 | { |
104 | sum += MULT16_16(num[j],mem[j]); | 119 | sum = MAC16_16(sum,rnum[j],x[i+j]); |
105 | } | ||
106 | for (j=ord-1;j>=1;j--) | ||
107 | { | ||
108 | mem[j]=mem[j-1]; | ||
109 | } | 120 | } |
110 | mem[0] = x[i]; | 121 | _y[i] = SATURATE16(PSHR32(sum, SIG_SHIFT)); |
111 | y[i] = ROUND16(sum, SIG_SHIFT); | ||
112 | } | 122 | } |
123 | #else | ||
124 | for (i=0;i<N-3;i+=4) | ||
125 | { | ||
126 | opus_val32 sum[4]={0,0,0,0}; | ||
127 | xcorr_kernel(rnum, x+i, sum, ord); | ||
128 | _y[i ] = SATURATE16(ADD32(EXTEND32(_x[i ]), PSHR32(sum[0], SIG_SHIFT))); | ||
129 | _y[i+1] = SATURATE16(ADD32(EXTEND32(_x[i+1]), PSHR32(sum[1], SIG_SHIFT))); | ||
130 | _y[i+2] = SATURATE16(ADD32(EXTEND32(_x[i+2]), PSHR32(sum[2], SIG_SHIFT))); | ||
131 | _y[i+3] = SATURATE16(ADD32(EXTEND32(_x[i+3]), PSHR32(sum[3], SIG_SHIFT))); | ||
132 | } | ||
133 | for (;i<N;i++) | ||
134 | { | ||
135 | opus_val32 sum = 0; | ||
136 | for (j=0;j<ord;j++) | ||
137 | sum = MAC16_16(sum,rnum[j],x[i+j]); | ||
138 | _y[i] = SATURATE16(ADD32(EXTEND32(_x[i]), PSHR32(sum, SIG_SHIFT))); | ||
139 | } | ||
140 | #endif | ||
141 | RESTORE_STACK; | ||
113 | } | 142 | } |
114 | 143 | ||
115 | void celt_iir(const opus_val32 *x, | 144 | void celt_iir(const opus_val32 *_x, |
116 | const opus_val16 *den, | 145 | const opus_val16 *den, |
117 | opus_val32 *y, | 146 | opus_val32 *_y, |
118 | int N, | 147 | int N, |
119 | int ord, | 148 | int ord, |
120 | opus_val16 *mem) | 149 | opus_val16 *mem) |
121 | { | 150 | { |
151 | #ifdef SMALL_FOOTPRINT | ||
122 | int i,j; | 152 | int i,j; |
123 | for (i=0;i<N;i++) | 153 | for (i=0;i<N;i++) |
124 | { | 154 | { |
125 | opus_val32 sum = x[i]; | 155 | opus_val32 sum = _x[i]; |
126 | for (j=0;j<ord;j++) | 156 | for (j=0;j<ord;j++) |
127 | { | 157 | { |
128 | sum -= MULT16_16(den[j],mem[j]); | 158 | sum -= MULT16_16(den[j],mem[j]); |
@@ -132,11 +162,65 @@ void celt_iir(const opus_val32 *x, | |||
132 | mem[j]=mem[j-1]; | 162 | mem[j]=mem[j-1]; |
133 | } | 163 | } |
134 | mem[0] = ROUND16(sum,SIG_SHIFT); | 164 | mem[0] = ROUND16(sum,SIG_SHIFT); |
135 | y[i] = sum; | 165 | _y[i] = sum; |
136 | } | 166 | } |
167 | #else | ||
168 | int i,j; | ||
169 | VARDECL(opus_val16, rden); | ||
170 | VARDECL(opus_val16, y); | ||
171 | SAVE_STACK; | ||
172 | |||
173 | celt_assert((ord&3)==0); | ||
174 | ALLOC(rden, ord, opus_val16); | ||
175 | ALLOC(y, N+ord, opus_val16); | ||
176 | for(i=0;i<ord;i++) | ||
177 | rden[i] = den[ord-i-1]; | ||
178 | for(i=0;i<ord;i++) | ||
179 | y[i] = -mem[ord-i-1]; | ||
180 | for(;i<N+ord;i++) | ||
181 | y[i]=0; | ||
182 | for (i=0;i<N-3;i+=4) | ||
183 | { | ||
184 | /* Unroll by 4 as if it were an FIR filter */ | ||
185 | opus_val32 sum[4]; | ||
186 | sum[0]=_x[i]; | ||
187 | sum[1]=_x[i+1]; | ||
188 | sum[2]=_x[i+2]; | ||
189 | sum[3]=_x[i+3]; | ||
190 | xcorr_kernel(rden, y+i, sum, ord); | ||
191 | |||
192 | /* Patch up the result to compensate for the fact that this is an IIR */ | ||
193 | y[i+ord ] = -ROUND16(sum[0],SIG_SHIFT); | ||
194 | _y[i ] = sum[0]; | ||
195 | sum[1] = MAC16_16(sum[1], y[i+ord ], den[0]); | ||
196 | y[i+ord+1] = -ROUND16(sum[1],SIG_SHIFT); | ||
197 | _y[i+1] = sum[1]; | ||
198 | sum[2] = MAC16_16(sum[2], y[i+ord+1], den[0]); | ||
199 | sum[2] = MAC16_16(sum[2], y[i+ord ], den[1]); | ||
200 | y[i+ord+2] = -ROUND16(sum[2],SIG_SHIFT); | ||
201 | _y[i+2] = sum[2]; | ||
202 | |||
203 | sum[3] = MAC16_16(sum[3], y[i+ord+2], den[0]); | ||
204 | sum[3] = MAC16_16(sum[3], y[i+ord+1], den[1]); | ||
205 | sum[3] = MAC16_16(sum[3], y[i+ord ], den[2]); | ||
206 | y[i+ord+3] = -ROUND16(sum[3],SIG_SHIFT); | ||
207 | _y[i+3] = sum[3]; | ||
208 | } | ||
209 | for (;i<N;i++) | ||
210 | { | ||
211 | opus_val32 sum = _x[i]; | ||
212 | for (j=0;j<ord;j++) | ||
213 | sum -= MULT16_16(rden[j],y[i+j]); | ||
214 | y[i+ord] = ROUND16(sum,SIG_SHIFT); | ||
215 | _y[i] = sum; | ||
216 | } | ||
217 | for(i=0;i<ord;i++) | ||
218 | mem[i] = _y[N-i-1]; | ||
219 | RESTORE_STACK; | ||
220 | #endif | ||
137 | } | 221 | } |
138 | 222 | ||
139 | void _celt_autocorr( | 223 | int _celt_autocorr( |
140 | const opus_val16 *x, /* in: [0...n-1] samples x */ | 224 | const opus_val16 *x, /* in: [0...n-1] samples x */ |
141 | opus_val32 *ac, /* out: [0...lag-1] ac values */ | 225 | opus_val32 *ac, /* out: [0...lag-1] ac values */ |
142 | const opus_val16 *window, | 226 | const opus_val16 *window, |
@@ -146,43 +230,79 @@ void _celt_autocorr( | |||
146 | ) | 230 | ) |
147 | { | 231 | { |
148 | opus_val32 d; | 232 | opus_val32 d; |
149 | int i; | 233 | int i, k; |
234 | int fastN=n-lag; | ||
235 | int shift; | ||
236 | const opus_val16 *xptr; | ||
150 | VARDECL(opus_val16, xx); | 237 | VARDECL(opus_val16, xx); |
151 | SAVE_STACK; | 238 | SAVE_STACK; |
152 | ALLOC(xx, n, opus_val16); | 239 | ALLOC(xx, n, opus_val16); |
153 | celt_assert(n>0); | 240 | celt_assert(n>0); |
154 | celt_assert(overlap>=0); | 241 | celt_assert(overlap>=0); |
155 | for (i=0;i<n;i++) | 242 | if (overlap == 0) |
156 | xx[i] = x[i]; | ||
157 | for (i=0;i<overlap;i++) | ||
158 | { | 243 | { |
159 | xx[i] = MULT16_16_Q15(x[i],window[i]); | 244 | xptr = x; |
160 | xx[n-i-1] = MULT16_16_Q15(x[n-i-1],window[i]); | 245 | } else { |
246 | for (i=0;i<n;i++) | ||
247 | xx[i] = x[i]; | ||
248 | for (i=0;i<overlap;i++) | ||
249 | { | ||
250 | xx[i] = MULT16_16_Q15(x[i],window[i]); | ||
251 | xx[n-i-1] = MULT16_16_Q15(x[n-i-1],window[i]); | ||
252 | } | ||
253 | xptr = xx; | ||
161 | } | 254 | } |
255 | shift=0; | ||
162 | #ifdef FIXED_POINT | 256 | #ifdef FIXED_POINT |
163 | { | 257 | { |
164 | opus_val32 ac0=0; | 258 | opus_val32 ac0; |
165 | int shift; | 259 | ac0 = 1+(n<<7); |
166 | for(i=0;i<n;i++) | 260 | if (n&1) ac0 += SHR32(MULT16_16(xptr[0],xptr[0]),9); |
167 | ac0 += SHR32(MULT16_16(xx[i],xx[i]),9); | 261 | for(i=(n&1);i<n;i+=2) |
168 | ac0 += 1+n; | 262 | { |
263 | ac0 += SHR32(MULT16_16(xptr[i],xptr[i]),9); | ||
264 | ac0 += SHR32(MULT16_16(xptr[i+1],xptr[i+1]),9); | ||
265 | } | ||
169 | 266 | ||
170 | shift = celt_ilog2(ac0)-30+10; | 267 | shift = celt_ilog2(ac0)-30+10; |
171 | shift = (shift+1)/2; | 268 | shift = (shift)/2; |
172 | for(i=0;i<n;i++) | 269 | if (shift>0) |
173 | xx[i] = VSHR32(xx[i], shift); | 270 | { |
271 | for(i=0;i<n;i++) | ||
272 | xx[i] = PSHR32(xptr[i], shift); | ||
273 | xptr = xx; | ||
274 | } else | ||
275 | shift = 0; | ||
174 | } | 276 | } |
175 | #endif | 277 | #endif |
176 | while (lag>=0) | 278 | celt_pitch_xcorr(xptr, xptr, ac, fastN, lag+1); |
279 | for (k=0;k<=lag;k++) | ||
177 | { | 280 | { |
178 | for (i = lag, d = 0; i < n; i++) | 281 | for (i = k+fastN, d = 0; i < n; i++) |
179 | d += xx[i] * xx[i-lag]; | 282 | d = MAC16_16(d, xptr[i], xptr[i-k]); |
180 | ac[lag] = d; | 283 | ac[k] += d; |
181 | /*printf ("%f ", ac[lag]);*/ | ||
182 | lag--; | ||
183 | } | 284 | } |
184 | /*printf ("\n");*/ | 285 | #ifdef FIXED_POINT |
185 | ac[0] += 10; | 286 | shift = 2*shift; |
287 | if (shift<=0) | ||
288 | ac[0] += SHL32((opus_int32)1, -shift); | ||
289 | if (ac[0] < 268435456) | ||
290 | { | ||
291 | int shift2 = 29 - EC_ILOG(ac[0]); | ||
292 | for (i=0;i<=lag;i++) | ||
293 | ac[i] = SHL32(ac[i], shift2); | ||
294 | shift -= shift2; | ||
295 | } else if (ac[0] >= 536870912) | ||
296 | { | ||
297 | int shift2=1; | ||
298 | if (ac[0] >= 1073741824) | ||
299 | shift2++; | ||
300 | for (i=0;i<=lag;i++) | ||
301 | ac[i] = SHR32(ac[i], shift2); | ||
302 | shift += shift2; | ||
303 | } | ||
304 | #endif | ||
186 | 305 | ||
187 | RESTORE_STACK; | 306 | RESTORE_STACK; |
307 | return shift; | ||
188 | } | 308 | } |
diff --git a/lib/rbcodec/codecs/libopus/celt/celt_lpc.h b/lib/rbcodec/codecs/libopus/celt/celt_lpc.h index 2baa77edf8..19279a0ed6 100644 --- a/lib/rbcodec/codecs/libopus/celt/celt_lpc.h +++ b/lib/rbcodec/codecs/libopus/celt/celt_lpc.h | |||
@@ -48,6 +48,6 @@ void celt_iir(const opus_val32 *x, | |||
48 | int ord, | 48 | int ord, |
49 | opus_val16 *mem); | 49 | opus_val16 *mem); |
50 | 50 | ||
51 | void _celt_autocorr(const opus_val16 *x, opus_val32 *ac, const opus_val16 *window, int overlap, int lag, int n); | 51 | int _celt_autocorr(const opus_val16 *x, opus_val32 *ac, const opus_val16 *window, int overlap, int lag, int n); |
52 | 52 | ||
53 | #endif /* PLC_H */ | 53 | #endif /* PLC_H */ |
diff --git a/lib/rbcodec/codecs/libopus/celt/cf/fixed_cf.h b/lib/rbcodec/codecs/libopus/celt/cf/fixed_cf.h new file mode 100644 index 0000000000..c442a55663 --- /dev/null +++ b/lib/rbcodec/codecs/libopus/celt/cf/fixed_cf.h | |||
@@ -0,0 +1,56 @@ | |||
1 | /* Copyright (C) 2013 Nils Wallménius */ | ||
2 | /* | ||
3 | Redistribution and use in source and binary forms, with or without | ||
4 | modification, are permitted provided that the following conditions | ||
5 | are met: | ||
6 | |||
7 | - Redistributions of source code must retain the above copyright | ||
8 | notice, this list of conditions and the following disclaimer. | ||
9 | |||
10 | - Redistributions in binary form must reproduce the above copyright | ||
11 | notice, this list of conditions and the following disclaimer in the | ||
12 | documentation and/or other materials provided with the distribution. | ||
13 | |||
14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
15 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
16 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
17 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER | ||
18 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
19 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
20 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
21 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
22 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
23 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
25 | */ | ||
26 | |||
27 | #ifndef FIXED_CF_H | ||
28 | #define FIXED_CF_H | ||
29 | |||
30 | #undef MULT16_32_Q15 | ||
31 | static inline int32_t MULT16_32_Q15_cf(int32_t a, int32_t b) | ||
32 | { | ||
33 | int32_t r; | ||
34 | asm volatile ("mac.l %[a], %[b], %%acc0;" | ||
35 | "movclr.l %%acc0, %[r];" | ||
36 | : [r] "=r" (r) | ||
37 | : [a] "r" (a<<16), [b] "r" (b) | ||
38 | : "cc"); | ||
39 | return r; | ||
40 | } | ||
41 | #define MULT16_32_Q15(a, b) (MULT16_32_Q15_cf(a, b)) | ||
42 | |||
43 | #undef MULT32_32_Q31 | ||
44 | static inline int32_t MULT32_32_Q31_cf(int32_t a, int32_t b) | ||
45 | { | ||
46 | int32_t r; | ||
47 | asm volatile ("mac.l %[a], %[b], %%acc0;" | ||
48 | "movclr.l %%acc0, %[r];" | ||
49 | : [r] "=r" (r) | ||
50 | : [a] "r" (a), [b] "r" (b) | ||
51 | : "cc"); | ||
52 | return r; | ||
53 | } | ||
54 | #define MULT32_32_Q31(a, b) (MULT32_32_Q31_cf(a, b)) | ||
55 | |||
56 | #endif | ||
diff --git a/lib/rbcodec/codecs/libopus/celt/cf/kiss_fft_cf.h b/lib/rbcodec/codecs/libopus/celt/cf/kiss_fft_cf.h new file mode 100644 index 0000000000..fe0fe2f3ca --- /dev/null +++ b/lib/rbcodec/codecs/libopus/celt/cf/kiss_fft_cf.h | |||
@@ -0,0 +1,57 @@ | |||
1 | /*Copyright (c) 2013, Xiph.Org Foundation and contributors. | ||
2 | |||
3 | All rights reserved. | ||
4 | |||
5 | Redistribution and use in source and binary forms, with or without | ||
6 | modification, are permitted provided that the following conditions are met: | ||
7 | |||
8 | * Redistributions of source code must retain the above copyright notice, | ||
9 | this list of conditions and the following disclaimer. | ||
10 | * Redistributions in binary form must reproduce the above copyright notice, | ||
11 | this list of conditions and the following disclaimer in the | ||
12 | documentation and/or other materials provided with the distribution. | ||
13 | |||
14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
18 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
19 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
20 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
21 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
22 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
23 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
24 | POSSIBILITY OF SUCH DAMAGE.*/ | ||
25 | |||
26 | #ifndef KISS_FFT_CF_H | ||
27 | #define KISS_FFT_CF_H | ||
28 | |||
29 | #if !defined(KISS_FFT_GUTS_H) | ||
30 | #error "This file should only be included from _kiss_fft_guts.h" | ||
31 | #endif | ||
32 | |||
33 | #ifdef FIXED_POINT | ||
34 | |||
35 | #undef C_MULC | ||
36 | #define C_MULC(m,a,b) \ | ||
37 | { \ | ||
38 | asm volatile("move.l (%[bp]), %%d2;" \ | ||
39 | "clr.l %%d3;" \ | ||
40 | "move.w %%d2, %%d3;" \ | ||
41 | "swap %%d3;" \ | ||
42 | "clr.w %%d2;" \ | ||
43 | "movem.l (%[ap]), %%d0-%%d1;" \ | ||
44 | "mac.l %%d0, %%d2, %%acc0;" \ | ||
45 | "mac.l %%d1, %%d3, %%acc0;" \ | ||
46 | "mac.l %%d1, %%d2, %%acc1;" \ | ||
47 | "msac.l %%d0, %%d3, %%acc1;" \ | ||
48 | "movclr.l %%acc0, %[mr];" \ | ||
49 | "movclr.l %%acc1, %[mi];" \ | ||
50 | : [mr] "=r" ((m).r), [mi] "=r" ((m).i) \ | ||
51 | : [ap] "a" (&(a)), [bp] "a" (&(b)) \ | ||
52 | : "d0", "d1", "d2", "d3", "cc"); \ | ||
53 | } | ||
54 | |||
55 | #endif /* FIXED_POINT */ | ||
56 | |||
57 | #endif /* KISS_FFT_CF_H */ | ||
diff --git a/lib/rbcodec/codecs/libopus/celt/cpu_support.h b/lib/rbcodec/codecs/libopus/celt/cpu_support.h new file mode 100644 index 0000000000..41481feb9c --- /dev/null +++ b/lib/rbcodec/codecs/libopus/celt/cpu_support.h | |||
@@ -0,0 +1,51 @@ | |||
1 | /* Copyright (c) 2010 Xiph.Org Foundation | ||
2 | * Copyright (c) 2013 Parrot */ | ||
3 | /* | ||
4 | Redistribution and use in source and binary forms, with or without | ||
5 | modification, are permitted provided that the following conditions | ||
6 | are met: | ||
7 | |||
8 | - Redistributions of source code must retain the above copyright | ||
9 | notice, this list of conditions and the following disclaimer. | ||
10 | |||
11 | - Redistributions in binary form must reproduce the above copyright | ||
12 | notice, this list of conditions and the following disclaimer in the | ||
13 | documentation and/or other materials provided with the distribution. | ||
14 | |||
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
16 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
17 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
18 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER | ||
19 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
20 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
21 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
22 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
23 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
24 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | #ifndef CPU_SUPPORT_H | ||
29 | #define CPU_SUPPORT_H | ||
30 | |||
31 | #if defined(OPUS_HAVE_RTCD) && defined(ARMv4_ASM) | ||
32 | #include "arm/armcpu.h" | ||
33 | |||
34 | /* We currently support 4 ARM variants: | ||
35 | * arch[0] -> ARMv4 | ||
36 | * arch[1] -> ARMv5E | ||
37 | * arch[2] -> ARMv6 | ||
38 | * arch[3] -> NEON | ||
39 | */ | ||
40 | #define OPUS_ARCHMASK 3 | ||
41 | |||
42 | #else | ||
43 | #define OPUS_ARCHMASK 0 | ||
44 | |||
45 | static inline int opus_select_arch(void) | ||
46 | { | ||
47 | return 0; | ||
48 | } | ||
49 | #endif | ||
50 | |||
51 | #endif | ||
diff --git a/lib/rbcodec/codecs/libopus/celt/cwrs.c b/lib/rbcodec/codecs/libopus/celt/cwrs.c index b8ade96fce..eb8fa1c807 100644 --- a/lib/rbcodec/codecs/libopus/celt/cwrs.c +++ b/lib/rbcodec/codecs/libopus/celt/cwrs.c | |||
@@ -28,14 +28,13 @@ | |||
28 | */ | 28 | */ |
29 | 29 | ||
30 | #ifdef HAVE_CONFIG_H | 30 | #ifdef HAVE_CONFIG_H |
31 | #include "opus_config.h" | 31 | #include "config.h" |
32 | #endif | 32 | #endif |
33 | 33 | ||
34 | #include "os_support.h" | 34 | #include "os_support.h" |
35 | #include "cwrs.h" | 35 | #include "cwrs.h" |
36 | #include "mathops.h" | 36 | #include "mathops.h" |
37 | #include "arch.h" | 37 | #include "arch.h" |
38 | #include "rate.h" | ||
39 | 38 | ||
40 | #ifdef CUSTOM_MODES | 39 | #ifdef CUSTOM_MODES |
41 | 40 | ||
@@ -72,64 +71,6 @@ int log2_frac(opus_uint32 val, int frac) | |||
72 | } | 71 | } |
73 | #endif | 72 | #endif |
74 | 73 | ||
75 | #ifndef SMALL_FOOTPRINT | ||
76 | |||
77 | #define MASK32 (0xFFFFFFFF) | ||
78 | |||
79 | /*INV_TABLE[i] holds the multiplicative inverse of (2*i+1) mod 2**32.*/ | ||
80 | static const opus_uint32 INV_TABLE[53]={ | ||
81 | 0x00000001,0xAAAAAAAB,0xCCCCCCCD,0xB6DB6DB7, | ||
82 | 0x38E38E39,0xBA2E8BA3,0xC4EC4EC5,0xEEEEEEEF, | ||
83 | 0xF0F0F0F1,0x286BCA1B,0x3CF3CF3D,0xE9BD37A7, | ||
84 | 0xC28F5C29,0x684BDA13,0x4F72C235,0xBDEF7BDF, | ||
85 | 0x3E0F83E1,0x8AF8AF8B,0x914C1BAD,0x96F96F97, | ||
86 | 0xC18F9C19,0x2FA0BE83,0xA4FA4FA5,0x677D46CF, | ||
87 | 0x1A1F58D1,0xFAFAFAFB,0x8C13521D,0x586FB587, | ||
88 | 0xB823EE09,0xA08AD8F3,0xC10C9715,0xBEFBEFBF, | ||
89 | 0xC0FC0FC1,0x07A44C6B,0xA33F128D,0xE327A977, | ||
90 | 0xC7E3F1F9,0x962FC963,0x3F2B3885,0x613716AF, | ||
91 | 0x781948B1,0x2B2E43DB,0xFCFCFCFD,0x6FD0EB67, | ||
92 | 0xFA3F47E9,0xD2FD2FD3,0x3F4FD3F5,0xD4E25B9F, | ||
93 | 0x5F02A3A1,0xBF5A814B,0x7C32B16D,0xD3431B57, | ||
94 | 0xD8FD8FD9, | ||
95 | }; | ||
96 | |||
97 | /*Computes (_a*_b-_c)/(2*_d+1) when the quotient is known to be exact. | ||
98 | _a, _b, _c, and _d may be arbitrary so long as the arbitrary precision result | ||
99 | fits in 32 bits, but currently the table for multiplicative inverses is only | ||
100 | valid for _d<=52.*/ | ||
101 | static inline opus_uint32 imusdiv32odd(opus_uint32 _a,opus_uint32 _b, | ||
102 | opus_uint32 _c,int _d){ | ||
103 | celt_assert(_d<=52); | ||
104 | return (_a*_b-_c)*INV_TABLE[_d]&MASK32; | ||
105 | } | ||
106 | |||
107 | /*Computes (_a*_b-_c)/_d when the quotient is known to be exact. | ||
108 | _d does not actually have to be even, but imusdiv32odd will be faster when | ||
109 | it's odd, so you should use that instead. | ||
110 | _a and _d are assumed to be small (e.g., _a*_d fits in 32 bits; currently the | ||
111 | table for multiplicative inverses is only valid for _d<=54). | ||
112 | _b and _c may be arbitrary so long as the arbitrary precision reuslt fits in | ||
113 | 32 bits.*/ | ||
114 | static inline opus_uint32 imusdiv32even(opus_uint32 _a,opus_uint32 _b, | ||
115 | opus_uint32 _c,int _d){ | ||
116 | opus_uint32 inv; | ||
117 | int mask; | ||
118 | int shift; | ||
119 | int one; | ||
120 | celt_assert(_d>0); | ||
121 | celt_assert(_d<=54); | ||
122 | shift=EC_ILOG(_d^(_d-1)); | ||
123 | inv=INV_TABLE[(_d-1)>>shift]; | ||
124 | shift--; | ||
125 | one=1<<shift; | ||
126 | mask=one-1; | ||
127 | return (_a*(_b>>shift)-(_c>>shift)+ | ||
128 | ((_a*(_b&mask)+one-(_c&mask))>>shift)-1)*inv&MASK32; | ||
129 | } | ||
130 | |||
131 | #endif /* SMALL_FOOTPRINT */ | ||
132 | |||
133 | /*Although derived separately, the pulse vector coding scheme is equivalent to | 74 | /*Although derived separately, the pulse vector coding scheme is equivalent to |
134 | a Pyramid Vector Quantizer \cite{Fis86}. | 75 | a Pyramid Vector Quantizer \cite{Fis86}. |
135 | Some additional notes about an early version appear at | 76 | Some additional notes about an early version appear at |
@@ -249,46 +190,346 @@ static inline opus_uint32 imusdiv32even(opus_uint32 _a,opus_uint32 _b, | |||
249 | year=1986 | 190 | year=1986 |
250 | }*/ | 191 | }*/ |
251 | 192 | ||
252 | #ifndef SMALL_FOOTPRINT | 193 | #if !defined(SMALL_FOOTPRINT) |
253 | /*Compute U(2,_k). | 194 | |
254 | Note that this may be called with _k=32768 (maxK[2]+1).*/ | 195 | /*U(N,K) = U(K,N) := N>0?K>0?U(N-1,K)+U(N,K-1)+U(N-1,K-1):0:K>0?1:0*/ |
255 | static inline unsigned ucwrs2(unsigned _k){ | 196 | # define CELT_PVQ_U(_n,_k) (CELT_PVQ_U_ROW[IMIN(_n,_k)][IMAX(_n,_k)]) |
256 | celt_assert(_k>0); | 197 | /*V(N,K) := U(N,K)+U(N,K+1) = the number of PVQ codewords for a band of size N |
257 | return _k+(_k-1); | 198 | with K pulses allocated to it.*/ |
258 | } | 199 | # define CELT_PVQ_V(_n,_k) (CELT_PVQ_U(_n,_k)+CELT_PVQ_U(_n,(_k)+1)) |
200 | |||
201 | /*For each V(N,K) supported, we will access element U(min(N,K+1),max(N,K+1)). | ||
202 | Thus, the number of entries in row I is the larger of the maximum number of | ||
203 | pulses we will ever allocate for a given N=I (K=128, or however many fit in | ||
204 | 32 bits, whichever is smaller), plus one, and the maximum N for which | ||
205 | K=I-1 pulses fit in 32 bits. | ||
206 | The largest band size in an Opus Custom mode is 208. | ||
207 | Otherwise, we can limit things to the set of N which can be achieved by | ||
208 | splitting a band from a standard Opus mode: 176, 144, 96, 88, 72, 64, 48, | ||
209 | 44, 36, 32, 24, 22, 18, 16, 8, 4, 2).*/ | ||
210 | #if defined(CUSTOM_MODES) | ||
211 | static const opus_uint32 CELT_PVQ_U_DATA[1488]={ | ||
212 | #else | ||
213 | static const opus_uint32 CELT_PVQ_U_DATA[1272] ICONST_ATTR ={ | ||
214 | #endif | ||
215 | /*N=0, K=0...176:*/ | ||
216 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
217 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
218 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
219 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
220 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
221 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
222 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
223 | #if defined(CUSTOM_MODES) | ||
224 | /*...208:*/ | ||
225 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
226 | 0, 0, 0, 0, 0, 0, | ||
227 | #endif | ||
228 | /*N=1, K=1...176:*/ | ||
229 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
230 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
231 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
232 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
233 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
234 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
235 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
236 | #if defined(CUSTOM_MODES) | ||
237 | /*...208:*/ | ||
238 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
239 | 1, 1, 1, 1, 1, 1, | ||
240 | #endif | ||
241 | /*N=2, K=2...176:*/ | ||
242 | 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, | ||
243 | 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, | ||
244 | 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, | ||
245 | 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, | ||
246 | 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, | ||
247 | 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, | ||
248 | 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, | ||
249 | 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255, 257, 259, 261, 263, | ||
250 | 265, 267, 269, 271, 273, 275, 277, 279, 281, 283, 285, 287, 289, 291, 293, | ||
251 | 295, 297, 299, 301, 303, 305, 307, 309, 311, 313, 315, 317, 319, 321, 323, | ||
252 | 325, 327, 329, 331, 333, 335, 337, 339, 341, 343, 345, 347, 349, 351, | ||
253 | #if defined(CUSTOM_MODES) | ||
254 | /*...208:*/ | ||
255 | 353, 355, 357, 359, 361, 363, 365, 367, 369, 371, 373, 375, 377, 379, 381, | ||
256 | 383, 385, 387, 389, 391, 393, 395, 397, 399, 401, 403, 405, 407, 409, 411, | ||
257 | 413, 415, | ||
258 | #endif | ||
259 | /*N=3, K=3...176:*/ | ||
260 | 13, 25, 41, 61, 85, 113, 145, 181, 221, 265, 313, 365, 421, 481, 545, 613, | ||
261 | 685, 761, 841, 925, 1013, 1105, 1201, 1301, 1405, 1513, 1625, 1741, 1861, | ||
262 | 1985, 2113, 2245, 2381, 2521, 2665, 2813, 2965, 3121, 3281, 3445, 3613, 3785, | ||
263 | 3961, 4141, 4325, 4513, 4705, 4901, 5101, 5305, 5513, 5725, 5941, 6161, 6385, | ||
264 | 6613, 6845, 7081, 7321, 7565, 7813, 8065, 8321, 8581, 8845, 9113, 9385, 9661, | ||
265 | 9941, 10225, 10513, 10805, 11101, 11401, 11705, 12013, 12325, 12641, 12961, | ||
266 | 13285, 13613, 13945, 14281, 14621, 14965, 15313, 15665, 16021, 16381, 16745, | ||
267 | 17113, 17485, 17861, 18241, 18625, 19013, 19405, 19801, 20201, 20605, 21013, | ||
268 | 21425, 21841, 22261, 22685, 23113, 23545, 23981, 24421, 24865, 25313, 25765, | ||
269 | 26221, 26681, 27145, 27613, 28085, 28561, 29041, 29525, 30013, 30505, 31001, | ||
270 | 31501, 32005, 32513, 33025, 33541, 34061, 34585, 35113, 35645, 36181, 36721, | ||
271 | 37265, 37813, 38365, 38921, 39481, 40045, 40613, 41185, 41761, 42341, 42925, | ||
272 | 43513, 44105, 44701, 45301, 45905, 46513, 47125, 47741, 48361, 48985, 49613, | ||
273 | 50245, 50881, 51521, 52165, 52813, 53465, 54121, 54781, 55445, 56113, 56785, | ||
274 | 57461, 58141, 58825, 59513, 60205, 60901, 61601, | ||
275 | #if defined(CUSTOM_MODES) | ||
276 | /*...208:*/ | ||
277 | 62305, 63013, 63725, 64441, 65161, 65885, 66613, 67345, 68081, 68821, 69565, | ||
278 | 70313, 71065, 71821, 72581, 73345, 74113, 74885, 75661, 76441, 77225, 78013, | ||
279 | 78805, 79601, 80401, 81205, 82013, 82825, 83641, 84461, 85285, 86113, | ||
280 | #endif | ||
281 | /*N=4, K=4...176:*/ | ||
282 | 63, 129, 231, 377, 575, 833, 1159, 1561, 2047, 2625, 3303, 4089, 4991, 6017, | ||
283 | 7175, 8473, 9919, 11521, 13287, 15225, 17343, 19649, 22151, 24857, 27775, | ||
284 | 30913, 34279, 37881, 41727, 45825, 50183, 54809, 59711, 64897, 70375, 76153, | ||
285 | 82239, 88641, 95367, 102425, 109823, 117569, 125671, 134137, 142975, 152193, | ||
286 | 161799, 171801, 182207, 193025, 204263, 215929, 228031, 240577, 253575, | ||
287 | 267033, 280959, 295361, 310247, 325625, 341503, 357889, 374791, 392217, | ||
288 | 410175, 428673, 447719, 467321, 487487, 508225, 529543, 551449, 573951, | ||
289 | 597057, 620775, 645113, 670079, 695681, 721927, 748825, 776383, 804609, | ||
290 | 833511, 863097, 893375, 924353, 956039, 988441, 1021567, 1055425, 1090023, | ||
291 | 1125369, 1161471, 1198337, 1235975, 1274393, 1313599, 1353601, 1394407, | ||
292 | 1436025, 1478463, 1521729, 1565831, 1610777, 1656575, 1703233, 1750759, | ||
293 | 1799161, 1848447, 1898625, 1949703, 2001689, 2054591, 2108417, 2163175, | ||
294 | 2218873, 2275519, 2333121, 2391687, 2451225, 2511743, 2573249, 2635751, | ||
295 | 2699257, 2763775, 2829313, 2895879, 2963481, 3032127, 3101825, 3172583, | ||
296 | 3244409, 3317311, 3391297, 3466375, 3542553, 3619839, 3698241, 3777767, | ||
297 | 3858425, 3940223, 4023169, 4107271, 4192537, 4278975, 4366593, 4455399, | ||
298 | 4545401, 4636607, 4729025, 4822663, 4917529, 5013631, 5110977, 5209575, | ||
299 | 5309433, 5410559, 5512961, 5616647, 5721625, 5827903, 5935489, 6044391, | ||
300 | 6154617, 6266175, 6379073, 6493319, 6608921, 6725887, 6844225, 6963943, | ||
301 | 7085049, 7207551, | ||
302 | #if defined(CUSTOM_MODES) | ||
303 | /*...208:*/ | ||
304 | 7331457, 7456775, 7583513, 7711679, 7841281, 7972327, 8104825, 8238783, | ||
305 | 8374209, 8511111, 8649497, 8789375, 8930753, 9073639, 9218041, 9363967, | ||
306 | 9511425, 9660423, 9810969, 9963071, 10116737, 10271975, 10428793, 10587199, | ||
307 | 10747201, 10908807, 11072025, 11236863, 11403329, 11571431, 11741177, | ||
308 | 11912575, | ||
309 | #endif | ||
310 | /*N=5, K=5...176:*/ | ||
311 | 321, 681, 1289, 2241, 3649, 5641, 8361, 11969, 16641, 22569, 29961, 39041, | ||
312 | 50049, 63241, 78889, 97281, 118721, 143529, 172041, 204609, 241601, 283401, | ||
313 | 330409, 383041, 441729, 506921, 579081, 658689, 746241, 842249, 947241, | ||
314 | 1061761, 1186369, 1321641, 1468169, 1626561, 1797441, 1981449, 2179241, | ||
315 | 2391489, 2618881, 2862121, 3121929, 3399041, 3694209, 4008201, 4341801, | ||
316 | 4695809, 5071041, 5468329, 5888521, 6332481, 6801089, 7295241, 7815849, | ||
317 | 8363841, 8940161, 9545769, 10181641, 10848769, 11548161, 12280841, 13047849, | ||
318 | 13850241, 14689089, 15565481, 16480521, 17435329, 18431041, 19468809, | ||
319 | 20549801, 21675201, 22846209, 24064041, 25329929, 26645121, 28010881, | ||
320 | 29428489, 30899241, 32424449, 34005441, 35643561, 37340169, 39096641, | ||
321 | 40914369, 42794761, 44739241, 46749249, 48826241, 50971689, 53187081, | ||
322 | 55473921, 57833729, 60268041, 62778409, 65366401, 68033601, 70781609, | ||
323 | 73612041, 76526529, 79526721, 82614281, 85790889, 89058241, 92418049, | ||
324 | 95872041, 99421961, 103069569, 106816641, 110664969, 114616361, 118672641, | ||
325 | 122835649, 127107241, 131489289, 135983681, 140592321, 145317129, 150160041, | ||
326 | 155123009, 160208001, 165417001, 170752009, 176215041, 181808129, 187533321, | ||
327 | 193392681, 199388289, 205522241, 211796649, 218213641, 224775361, 231483969, | ||
328 | 238341641, 245350569, 252512961, 259831041, 267307049, 274943241, 282741889, | ||
329 | 290705281, 298835721, 307135529, 315607041, 324252609, 333074601, 342075401, | ||
330 | 351257409, 360623041, 370174729, 379914921, 389846081, 399970689, 410291241, | ||
331 | 420810249, 431530241, 442453761, 453583369, 464921641, 476471169, 488234561, | ||
332 | 500214441, 512413449, 524834241, 537479489, 550351881, 563454121, 576788929, | ||
333 | 590359041, 604167209, 618216201, 632508801, | ||
334 | #if defined(CUSTOM_MODES) | ||
335 | /*...208:*/ | ||
336 | 647047809, 661836041, 676876329, 692171521, 707724481, 723538089, 739615241, | ||
337 | 755958849, 772571841, 789457161, 806617769, 824056641, 841776769, 859781161, | ||
338 | 878072841, 896654849, 915530241, 934702089, 954173481, 973947521, 994027329, | ||
339 | 1014416041, 1035116809, 1056132801, 1077467201, 1099123209, 1121104041, | ||
340 | 1143412929, 1166053121, 1189027881, 1212340489, 1235994241, | ||
341 | #endif | ||
342 | /*N=6, K=6...96:*/ | ||
343 | 1683, 3653, 7183, 13073, 22363, 36365, 56695, 85305, 124515, 177045, 246047, | ||
344 | 335137, 448427, 590557, 766727, 982729, 1244979, 1560549, 1937199, 2383409, | ||
345 | 2908411, 3522221, 4235671, 5060441, 6009091, 7095093, 8332863, 9737793, | ||
346 | 11326283, 13115773, 15124775, 17372905, 19880915, 22670725, 25765455, | ||
347 | 29189457, 32968347, 37129037, 41699767, 46710137, 52191139, 58175189, | ||
348 | 64696159, 71789409, 79491819, 87841821, 96879431, 106646281, 117185651, | ||
349 | 128542501, 140763503, 153897073, 167993403, 183104493, 199284183, 216588185, | ||
350 | 235074115, 254801525, 275831935, 298228865, 322057867, 347386557, 374284647, | ||
351 | 402823977, 433078547, 465124549, 499040399, 534906769, 572806619, 612825229, | ||
352 | 655050231, 699571641, 746481891, 795875861, 847850911, 902506913, 959946283, | ||
353 | 1020274013, 1083597703, 1150027593, 1219676595, 1292660325, 1369097135, | ||
354 | 1449108145, 1532817275, 1620351277, 1711839767, 1807415257, 1907213187, | ||
355 | 2011371957, 2120032959, | ||
356 | #if defined(CUSTOM_MODES) | ||
357 | /*...109:*/ | ||
358 | 2233340609U, 2351442379U, 2474488829U, 2602633639U, 2736033641U, 2874848851U, | ||
359 | 3019242501U, 3169381071U, 3325434321U, 3487575323U, 3655980493U, 3830829623U, | ||
360 | 4012305913U, | ||
361 | #endif | ||
362 | /*N=7, K=7...54*/ | ||
363 | 8989, 19825, 40081, 75517, 134245, 227305, 369305, 579125, 880685, 1303777, | ||
364 | 1884961, 2668525, 3707509, 5064793, 6814249, 9041957, 11847485, 15345233, | ||
365 | 19665841, 24957661, 31388293, 39146185, 48442297, 59511829, 72616013, | ||
366 | 88043969, 106114625, 127178701, 151620757, 179861305, 212358985, 249612805, | ||
367 | 292164445, 340600625, 395555537, 457713341, 527810725, 606639529, 695049433, | ||
368 | 793950709, 904317037, 1027188385, 1163673953, 1314955181, 1482288821, | ||
369 | 1667010073, 1870535785, 2094367717, | ||
370 | #if defined(CUSTOM_MODES) | ||
371 | /*...60:*/ | ||
372 | 2340095869U, 2609401873U, 2904062449U, 3225952925U, 3577050821U, 3959439497U, | ||
373 | #endif | ||
374 | /*N=8, K=8...37*/ | ||
375 | 48639, 108545, 224143, 433905, 795455, 1392065, 2340495, 3800305, 5984767, | ||
376 | 9173505, 13726991, 20103025, 28875327, 40754369, 56610575, 77500017, | ||
377 | 104692735, 139703809, 184327311, 240673265, 311207743, 398796225, 506750351, | ||
378 | 638878193, 799538175, 993696769, 1226990095, 1505789553, 1837271615, | ||
379 | 2229491905U, | ||
380 | #if defined(CUSTOM_MODES) | ||
381 | /*...40:*/ | ||
382 | 2691463695U, 3233240945U, 3866006015U, | ||
383 | #endif | ||
384 | /*N=9, K=9...28:*/ | ||
385 | 265729, 598417, 1256465, 2485825, 4673345, 8405905, 14546705, 24331777, | ||
386 | 39490049, 62390545, 96220561, 145198913, 214828609, 312193553, 446304145, | ||
387 | 628496897, 872893441, 1196924561, 1621925137, 2173806145U, | ||
388 | #if defined(CUSTOM_MODES) | ||
389 | /*...29:*/ | ||
390 | 2883810113U, | ||
391 | #endif | ||
392 | /*N=10, K=10...24:*/ | ||
393 | 1462563, 3317445, 7059735, 14218905, 27298155, 50250765, 89129247, 152951073, | ||
394 | 254831667, 413442773, 654862247, 1014889769, 1541911931, 2300409629U, | ||
395 | 3375210671U, | ||
396 | /*N=11, K=11...19:*/ | ||
397 | 8097453, 18474633, 39753273, 81270333, 158819253, 298199265, 540279585, | ||
398 | 948062325, 1616336765, | ||
399 | #if defined(CUSTOM_MODES) | ||
400 | /*...20:*/ | ||
401 | 2684641785U, | ||
402 | #endif | ||
403 | /*N=12, K=12...18:*/ | ||
404 | 45046719, 103274625, 224298231, 464387817, 921406335, 1759885185, | ||
405 | 3248227095U, | ||
406 | /*N=13, K=13...16:*/ | ||
407 | 251595969, 579168825, 1267854873, 2653649025U, | ||
408 | /*N=14, K=14:*/ | ||
409 | 1409933619 | ||
410 | }; | ||
259 | 411 | ||
260 | /*Compute V(2,_k).*/ | 412 | #if defined(CUSTOM_MODES) |
261 | static inline opus_uint32 ncwrs2(int _k){ | 413 | const opus_uint32 *const CELT_PVQ_U_ROW[15]={ |
262 | celt_assert(_k>0); | 414 | CELT_PVQ_U_DATA+ 0,CELT_PVQ_U_DATA+ 208,CELT_PVQ_U_DATA+ 415, |
263 | return 4*(opus_uint32)_k; | 415 | CELT_PVQ_U_DATA+ 621,CELT_PVQ_U_DATA+ 826,CELT_PVQ_U_DATA+1030, |
416 | CELT_PVQ_U_DATA+1233,CELT_PVQ_U_DATA+1336,CELT_PVQ_U_DATA+1389, | ||
417 | CELT_PVQ_U_DATA+1421,CELT_PVQ_U_DATA+1441,CELT_PVQ_U_DATA+1455, | ||
418 | CELT_PVQ_U_DATA+1464,CELT_PVQ_U_DATA+1470,CELT_PVQ_U_DATA+1473 | ||
419 | }; | ||
420 | #else | ||
421 | const opus_uint32 *const CELT_PVQ_U_ROW[15]={ | ||
422 | CELT_PVQ_U_DATA+ 0,CELT_PVQ_U_DATA+ 176,CELT_PVQ_U_DATA+ 351, | ||
423 | CELT_PVQ_U_DATA+ 525,CELT_PVQ_U_DATA+ 698,CELT_PVQ_U_DATA+ 870, | ||
424 | CELT_PVQ_U_DATA+1041,CELT_PVQ_U_DATA+1131,CELT_PVQ_U_DATA+1178, | ||
425 | CELT_PVQ_U_DATA+1207,CELT_PVQ_U_DATA+1226,CELT_PVQ_U_DATA+1240, | ||
426 | CELT_PVQ_U_DATA+1248,CELT_PVQ_U_DATA+1254,CELT_PVQ_U_DATA+1257 | ||
427 | }; | ||
428 | #endif | ||
429 | |||
430 | #if defined(CUSTOM_MODES) | ||
431 | void get_required_bits(opus_int16 *_bits,int _n,int _maxk,int _frac){ | ||
432 | int k; | ||
433 | /*_maxk==0 => there's nothing to do.*/ | ||
434 | celt_assert(_maxk>0); | ||
435 | _bits[0]=0; | ||
436 | for(k=1;k<=_maxk;k++)_bits[k]=log2_frac(CELT_PVQ_V(_n,k),_frac); | ||
264 | } | 437 | } |
438 | #endif | ||
265 | 439 | ||
266 | /*Compute U(3,_k). | 440 | static opus_uint32 icwrs(int _n,const int *_y){ |
267 | Note that this may be called with _k=32768 (maxK[3]+1).*/ | 441 | opus_uint32 i; |
268 | static inline opus_uint32 ucwrs3(unsigned _k){ | 442 | int j; |
269 | celt_assert(_k>0); | 443 | int k; |
270 | return (2*(opus_uint32)_k-2)*_k+1; | 444 | celt_assert(_n>=2); |
445 | j=_n-1; | ||
446 | i=_y[j]<0; | ||
447 | k=abs(_y[j]); | ||
448 | do{ | ||
449 | j--; | ||
450 | i+=CELT_PVQ_U(_n-j,k); | ||
451 | k+=abs(_y[j]); | ||
452 | if(_y[j]<0)i+=CELT_PVQ_U(_n-j,k+1); | ||
453 | } | ||
454 | while(j>0); | ||
455 | return i; | ||
271 | } | 456 | } |
272 | 457 | ||
273 | /*Compute V(3,_k).*/ | 458 | void encode_pulses(const int *_y,int _n,int _k,ec_enc *_enc){ |
274 | static inline opus_uint32 ncwrs3(int _k){ | ||
275 | celt_assert(_k>0); | 459 | celt_assert(_k>0); |
276 | return 2*(2*(unsigned)_k*(opus_uint32)_k+1); | 460 | ec_enc_uint(_enc,icwrs(_n,_y),CELT_PVQ_V(_n,_k)); |
277 | } | 461 | } |
278 | 462 | ||
279 | /*Compute U(4,_k).*/ | 463 | static void cwrsi(int _n,int _k,opus_uint32 _i,int *_y){ |
280 | static inline opus_uint32 ucwrs4(int _k){ | 464 | opus_uint32 p; |
465 | int s; | ||
466 | int k0; | ||
281 | celt_assert(_k>0); | 467 | celt_assert(_k>0); |
282 | return imusdiv32odd(2*_k,(2*_k-3)*(opus_uint32)_k+4,3,1); | 468 | celt_assert(_n>1); |
469 | while(_n>2){ | ||
470 | opus_uint32 q; | ||
471 | /*Lots of pulses case:*/ | ||
472 | if(_k>=_n){ | ||
473 | const opus_uint32 *row; | ||
474 | row=CELT_PVQ_U_ROW[_n]; | ||
475 | /*Are the pulses in this dimension negative?*/ | ||
476 | p=row[_k+1]; | ||
477 | s=-(_i>=p); | ||
478 | _i-=p&s; | ||
479 | /*Count how many pulses were placed in this dimension.*/ | ||
480 | k0=_k; | ||
481 | q=row[_n]; | ||
482 | if(q>_i){ | ||
483 | celt_assert(p>q); | ||
484 | _k=_n; | ||
485 | do p=CELT_PVQ_U_ROW[--_k][_n]; | ||
486 | while(p>_i); | ||
487 | } | ||
488 | else for(p=row[_k];p>_i;p=row[_k])_k--; | ||
489 | _i-=p; | ||
490 | *_y++=(k0-_k+s)^s; | ||
491 | } | ||
492 | /*Lots of dimensions case:*/ | ||
493 | else{ | ||
494 | /*Are there any pulses in this dimension at all?*/ | ||
495 | p=CELT_PVQ_U_ROW[_k][_n]; | ||
496 | q=CELT_PVQ_U_ROW[_k+1][_n]; | ||
497 | if(p<=_i&&_i<q){ | ||
498 | _i-=p; | ||
499 | *_y++=0; | ||
500 | } | ||
501 | else{ | ||
502 | /*Are the pulses in this dimension negative?*/ | ||
503 | s=-(_i>=q); | ||
504 | _i-=q&s; | ||
505 | /*Count how many pulses were placed in this dimension.*/ | ||
506 | k0=_k; | ||
507 | do p=CELT_PVQ_U_ROW[--_k][_n]; | ||
508 | while(p>_i); | ||
509 | _i-=p; | ||
510 | *_y++=(k0-_k+s)^s; | ||
511 | } | ||
512 | } | ||
513 | _n--; | ||
514 | } | ||
515 | /*_n==2*/ | ||
516 | p=2*_k+1; | ||
517 | s=-(_i>=p); | ||
518 | _i-=p&s; | ||
519 | k0=_k; | ||
520 | _k=(_i+1)>>1; | ||
521 | if(_k)_i-=2*_k-1; | ||
522 | *_y++=(k0-_k+s)^s; | ||
523 | /*_n==1*/ | ||
524 | s=-(int)_i; | ||
525 | *_y=(_k+s)^s; | ||
283 | } | 526 | } |
284 | 527 | ||
285 | /*Compute V(4,_k).*/ | 528 | void decode_pulses(int *_y,int _n,int _k,ec_dec *_dec){ |
286 | static inline opus_uint32 ncwrs4(int _k){ | 529 | cwrsi(_n,_k,ec_dec_uint(_dec,CELT_PVQ_V(_n,_k)),_y); |
287 | celt_assert(_k>0); | ||
288 | return ((_k*(opus_uint32)_k+2)*_k)/3<<3; | ||
289 | } | 530 | } |
290 | 531 | ||
291 | #endif /* SMALL_FOOTPRINT */ | 532 | #else /* SMALL_FOOTPRINT */ |
292 | 533 | ||
293 | /*Computes the next row/column of any recurrence that obeys the relation | 534 | /*Computes the next row/column of any recurrence that obeys the relation |
294 | u[i][j]=u[i-1][j]+u[i][j-1]+u[i-1][j-1]. | 535 | u[i][j]=u[i-1][j]+u[i][j-1]+u[i-1][j-1]. |
@@ -333,125 +574,18 @@ static opus_uint32 ncwrs_urow(unsigned _n,unsigned _k,opus_uint32 *_u){ | |||
333 | celt_assert(len>=3); | 574 | celt_assert(len>=3); |
334 | _u[0]=0; | 575 | _u[0]=0; |
335 | _u[1]=um2=1; | 576 | _u[1]=um2=1; |
336 | #ifndef SMALL_FOOTPRINT | 577 | /*If _n==0, _u[0] should be 1 and the rest should be 0.*/ |
337 | /*_k>52 doesn't work in the false branch due to the limits of INV_TABLE, | 578 | /*If _n==1, _u[i] should be 1 for i>1.*/ |
338 | but _k isn't tested here because k<=52 for n=7*/ | 579 | celt_assert(_n>=2); |
339 | if(_n<=6) | 580 | /*If _k==0, the following do-while loop will overflow the buffer.*/ |
340 | #endif | 581 | celt_assert(_k>0); |
341 | { | 582 | k=2; |
342 | /*If _n==0, _u[0] should be 1 and the rest should be 0.*/ | 583 | do _u[k]=(k<<1)-1; |
343 | /*If _n==1, _u[i] should be 1 for i>1.*/ | 584 | while(++k<len); |
344 | celt_assert(_n>=2); | 585 | for(k=2;k<_n;k++)unext(_u+1,_k+1,1); |
345 | /*If _k==0, the following do-while loop will overflow the buffer.*/ | ||
346 | celt_assert(_k>0); | ||
347 | k=2; | ||
348 | do _u[k]=(k<<1)-1; | ||
349 | while(++k<len); | ||
350 | for(k=2;k<_n;k++)unext(_u+1,_k+1,1); | ||
351 | } | ||
352 | #ifndef SMALL_FOOTPRINT | ||
353 | else{ | ||
354 | opus_uint32 um1; | ||
355 | opus_uint32 n2m1; | ||
356 | _u[2]=n2m1=um1=(_n<<1)-1; | ||
357 | for(k=3;k<len;k++){ | ||
358 | /*U(N,K) = ((2*N-1)*U(N,K-1)-U(N,K-2))/(K-1) + U(N,K-2)*/ | ||
359 | _u[k]=um2=imusdiv32even(n2m1,um1,um2,k-1)+um2; | ||
360 | if(++k>=len)break; | ||
361 | _u[k]=um1=imusdiv32odd(n2m1,um2,um1,(k-1)>>1)+um1; | ||
362 | } | ||
363 | } | ||
364 | #endif /* SMALL_FOOTPRINT */ | ||
365 | return _u[_k]+_u[_k+1]; | 586 | return _u[_k]+_u[_k+1]; |
366 | } | 587 | } |
367 | 588 | ||
368 | #ifndef SMALL_FOOTPRINT | ||
369 | |||
370 | /*Returns the _i'th combination of _k elements (at most 32767) chosen from a | ||
371 | set of size 1 with associated sign bits. | ||
372 | _y: Returns the vector of pulses.*/ | ||
373 | static inline void cwrsi1(int _k,opus_uint32 _i,int *_y){ | ||
374 | int s; | ||
375 | s=-(int)_i; | ||
376 | _y[0]=(_k+s)^s; | ||
377 | } | ||
378 | |||
379 | /*Returns the _i'th combination of _k elements (at most 32767) chosen from a | ||
380 | set of size 2 with associated sign bits. | ||
381 | _y: Returns the vector of pulses.*/ | ||
382 | static inline void cwrsi2(int _k,opus_uint32 _i,int *_y){ | ||
383 | opus_uint32 p; | ||
384 | int s; | ||
385 | int yj; | ||
386 | p=ucwrs2(_k+1U); | ||
387 | s=-(_i>=p); | ||
388 | _i-=p&s; | ||
389 | yj=_k; | ||
390 | _k=(_i+1)>>1; | ||
391 | p=_k?ucwrs2(_k):0; | ||
392 | _i-=p; | ||
393 | yj-=_k; | ||
394 | _y[0]=(yj+s)^s; | ||
395 | cwrsi1(_k,_i,_y+1); | ||
396 | } | ||
397 | |||
398 | /*Returns the _i'th combination of _k elements (at most 32767) chosen from a | ||
399 | set of size 3 with associated sign bits. | ||
400 | _y: Returns the vector of pulses.*/ | ||
401 | static void cwrsi3(int _k,opus_uint32 _i,int *_y){ | ||
402 | opus_uint32 p; | ||
403 | int s; | ||
404 | int yj; | ||
405 | p=ucwrs3(_k+1U); | ||
406 | s=-(_i>=p); | ||
407 | _i-=p&s; | ||
408 | yj=_k; | ||
409 | /*Finds the maximum _k such that ucwrs3(_k)<=_i (tested for all | ||
410 | _i<2147418113=U(3,32768)).*/ | ||
411 | _k=_i>0?(isqrt32(2*_i-1)+1)>>1:0; | ||
412 | p=_k?ucwrs3(_k):0; | ||
413 | _i-=p; | ||
414 | yj-=_k; | ||
415 | _y[0]=(yj+s)^s; | ||
416 | cwrsi2(_k,_i,_y+1); | ||
417 | } | ||
418 | |||
419 | /*Returns the _i'th combination of _k elements (at most 1172) chosen from a set | ||
420 | of size 4 with associated sign bits. | ||
421 | _y: Returns the vector of pulses.*/ | ||
422 | static void cwrsi4(int _k,opus_uint32 _i,int *_y){ | ||
423 | opus_uint32 p; | ||
424 | int s; | ||
425 | int yj; | ||
426 | int kl; | ||
427 | int kr; | ||
428 | p=ucwrs4(_k+1); | ||
429 | s=-(_i>=p); | ||
430 | _i-=p&s; | ||
431 | yj=_k; | ||
432 | /*We could solve a cubic for k here, but the form of the direct solution does | ||
433 | not lend itself well to exact integer arithmetic. | ||
434 | Instead we do a binary search on U(4,K).*/ | ||
435 | kl=0; | ||
436 | kr=_k; | ||
437 | for(;;){ | ||
438 | _k=(kl+kr)>>1; | ||
439 | p=_k?ucwrs4(_k):0; | ||
440 | if(p<_i){ | ||
441 | if(_k>=kr)break; | ||
442 | kl=_k+1; | ||
443 | } | ||
444 | else if(p>_i)kr=_k-1; | ||
445 | else break; | ||
446 | } | ||
447 | _i-=p; | ||
448 | yj-=_k; | ||
449 | _y[0]=(yj+s)^s; | ||
450 | cwrsi3(_k,_i,_y+1); | ||
451 | } | ||
452 | |||
453 | #endif /* SMALL_FOOTPRINT */ | ||
454 | |||
455 | /*Returns the _i'th combination of _k elements chosen from a set of size _n | 589 | /*Returns the _i'th combination of _k elements chosen from a set of size _n |
456 | with associated sign bits. | 590 | with associated sign bits. |
457 | _y: Returns the vector of pulses. | 591 | _y: Returns the vector of pulses. |
@@ -488,55 +622,6 @@ static inline opus_uint32 icwrs1(const int *_y,int *_k){ | |||
488 | return _y[0]<0; | 622 | return _y[0]<0; |
489 | } | 623 | } |
490 | 624 | ||
491 | #ifndef SMALL_FOOTPRINT | ||
492 | |||
493 | /*Returns the index of the given combination of K elements chosen from a set | ||
494 | of size 2 with associated sign bits. | ||
495 | _y: The vector of pulses, whose sum of absolute values is K. | ||
496 | _k: Returns K.*/ | ||
497 | static inline opus_uint32 icwrs2(const int *_y,int *_k){ | ||
498 | opus_uint32 i; | ||
499 | int k; | ||
500 | i=icwrs1(_y+1,&k); | ||
501 | i+=k?ucwrs2(k):0; | ||
502 | k+=abs(_y[0]); | ||
503 | if(_y[0]<0)i+=ucwrs2(k+1U); | ||
504 | *_k=k; | ||
505 | return i; | ||
506 | } | ||
507 | |||
508 | /*Returns the index of the given combination of K elements chosen from a set | ||
509 | of size 3 with associated sign bits. | ||
510 | _y: The vector of pulses, whose sum of absolute values is K. | ||
511 | _k: Returns K.*/ | ||
512 | static inline opus_uint32 icwrs3(const int *_y,int *_k){ | ||
513 | opus_uint32 i; | ||
514 | int k; | ||
515 | i=icwrs2(_y+1,&k); | ||
516 | i+=k?ucwrs3(k):0; | ||
517 | k+=abs(_y[0]); | ||
518 | if(_y[0]<0)i+=ucwrs3(k+1U); | ||
519 | *_k=k; | ||
520 | return i; | ||
521 | } | ||
522 | |||
523 | /*Returns the index of the given combination of K elements chosen from a set | ||
524 | of size 4 with associated sign bits. | ||
525 | _y: The vector of pulses, whose sum of absolute values is K. | ||
526 | _k: Returns K.*/ | ||
527 | static inline opus_uint32 icwrs4(const int *_y,int *_k){ | ||
528 | opus_uint32 i; | ||
529 | int k; | ||
530 | i=icwrs3(_y+1,&k); | ||
531 | i+=k?ucwrs4(k):0; | ||
532 | k+=abs(_y[0]); | ||
533 | if(_y[0]<0)i+=ucwrs4(k+1); | ||
534 | *_k=k; | ||
535 | return i; | ||
536 | } | ||
537 | |||
538 | #endif /* SMALL_FOOTPRINT */ | ||
539 | |||
540 | /*Returns the index of the given combination of K elements chosen from a set | 625 | /*Returns the index of the given combination of K elements chosen from a set |
541 | of size _n with associated sign bits. | 626 | of size _n with associated sign bits. |
542 | _y: The vector of pulses, whose sum of absolute values must be _k. | 627 | _y: The vector of pulses, whose sum of absolute values must be _k. |
@@ -544,8 +629,8 @@ static inline opus_uint32 icwrs4(const int *_y,int *_k){ | |||
544 | static inline opus_uint32 icwrs(int _n,int _k,opus_uint32 *_nc,const int *_y, | 629 | static inline opus_uint32 icwrs(int _n,int _k,opus_uint32 *_nc,const int *_y, |
545 | opus_uint32 *_u){ | 630 | opus_uint32 *_u){ |
546 | opus_uint32 i; | 631 | opus_uint32 i; |
547 | int j; | 632 | int j; |
548 | int k; | 633 | int k; |
549 | /*We can't unroll the first two iterations of the loop unless _n>=2.*/ | 634 | /*We can't unroll the first two iterations of the loop unless _n>=2.*/ |
550 | celt_assert(_n>=2); | 635 | celt_assert(_n>=2); |
551 | _u[0]=0; | 636 | _u[0]=0; |
@@ -590,58 +675,23 @@ void get_required_bits(opus_int16 *_bits,int _n,int _maxk,int _frac){ | |||
590 | 675 | ||
591 | void encode_pulses(const int *_y,int _n,int _k,ec_enc *_enc){ | 676 | void encode_pulses(const int *_y,int _n,int _k,ec_enc *_enc){ |
592 | opus_uint32 i; | 677 | opus_uint32 i; |
678 | VARDECL(opus_uint32,u); | ||
679 | opus_uint32 nc; | ||
680 | SAVE_STACK; | ||
593 | celt_assert(_k>0); | 681 | celt_assert(_k>0); |
594 | #ifndef SMALL_FOOTPRINT | 682 | ALLOC(u,_k+2U,opus_uint32); |
595 | switch(_n){ | 683 | i=icwrs(_n,_k,&nc,_y,u); |
596 | case 2:{ | 684 | ec_enc_uint(_enc,i,nc); |
597 | i=icwrs2(_y,&_k); | 685 | RESTORE_STACK; |
598 | ec_enc_uint(_enc,i,ncwrs2(_k)); | ||
599 | }break; | ||
600 | case 3:{ | ||
601 | i=icwrs3(_y,&_k); | ||
602 | ec_enc_uint(_enc,i,ncwrs3(_k)); | ||
603 | }break; | ||
604 | case 4:{ | ||
605 | i=icwrs4(_y,&_k); | ||
606 | ec_enc_uint(_enc,i,ncwrs4(_k)); | ||
607 | }break; | ||
608 | default: | ||
609 | { | ||
610 | #endif | ||
611 | VARDECL(opus_uint32,u); | ||
612 | opus_uint32 nc; | ||
613 | SAVE_STACK; | ||
614 | ALLOC(u,_k+2U,opus_uint32); | ||
615 | i=icwrs(_n,_k,&nc,_y,u); | ||
616 | ec_enc_uint(_enc,i,nc); | ||
617 | RESTORE_STACK; | ||
618 | #ifndef SMALL_FOOTPRINT | ||
619 | } | ||
620 | break; | ||
621 | } | ||
622 | #endif | ||
623 | } | 686 | } |
624 | 687 | ||
625 | void decode_pulses(int *_y,int _n,int _k,ec_dec *_dec) | 688 | void decode_pulses(int *_y,int _n,int _k,ec_dec *_dec){ |
626 | { | 689 | VARDECL(opus_uint32,u); |
690 | SAVE_STACK; | ||
627 | celt_assert(_k>0); | 691 | celt_assert(_k>0); |
628 | #ifndef SMALL_FOOTPRINT | 692 | ALLOC(u,_k+2U,opus_uint32); |
629 | switch(_n){ | 693 | cwrsi(_n,_k,ec_dec_uint(_dec,ncwrs_urow(_n,_k,u)),_y,u); |
630 | case 2:cwrsi2(_k,ec_dec_uint(_dec,ncwrs2(_k)),_y);break; | 694 | RESTORE_STACK; |
631 | case 3:cwrsi3(_k,ec_dec_uint(_dec,ncwrs3(_k)),_y);break; | ||
632 | case 4:cwrsi4(_k,ec_dec_uint(_dec,ncwrs4(_k)),_y);break; | ||
633 | default: | ||
634 | { | ||
635 | #endif | ||
636 | /* VARDECL(opus_uint32,u); | ||
637 | SAVE_STACK; | ||
638 | ALLOC(u,_k+2U,opus_uint32); */ | ||
639 | opus_uint32 u[MAX_PULSES+2]; | ||
640 | cwrsi(_n,_k,ec_dec_uint(_dec,ncwrs_urow(_n,_k,u)),_y,u); | ||
641 | /* RESTORE_STACK; */ | ||
642 | #ifndef SMALL_FOOTPRINT | ||
643 | } | ||
644 | break; | ||
645 | } | ||
646 | #endif | ||
647 | } | 695 | } |
696 | |||
697 | #endif /* SMALL_FOOTPRINT */ | ||
diff --git a/lib/rbcodec/codecs/libopus/celt/ecintrin.h b/lib/rbcodec/codecs/libopus/celt/ecintrin.h index 3dffa5f95c..be57dd40de 100644 --- a/lib/rbcodec/codecs/libopus/celt/ecintrin.h +++ b/lib/rbcodec/codecs/libopus/celt/ecintrin.h | |||
@@ -48,7 +48,7 @@ | |||
48 | /*Count leading zeros. | 48 | /*Count leading zeros. |
49 | This macro should only be used for implementing ec_ilog(), if it is defined. | 49 | This macro should only be used for implementing ec_ilog(), if it is defined. |
50 | All other code should use EC_ILOG() instead.*/ | 50 | All other code should use EC_ILOG() instead.*/ |
51 | #if defined(_MSC_VER) | 51 | #if defined(_MSC_VER) && (_MSC_VER >= 1400) |
52 | # include <intrin.h> | 52 | # include <intrin.h> |
53 | /*In _DEBUG mode this is not an intrinsic by default.*/ | 53 | /*In _DEBUG mode this is not an intrinsic by default.*/ |
54 | # pragma intrinsic(_BitScanReverse) | 54 | # pragma intrinsic(_BitScanReverse) |
diff --git a/lib/rbcodec/codecs/libopus/celt/entcode.c b/lib/rbcodec/codecs/libopus/celt/entcode.c index 80e64fefaa..fa5d7c7c2c 100644 --- a/lib/rbcodec/codecs/libopus/celt/entcode.c +++ b/lib/rbcodec/codecs/libopus/celt/entcode.c | |||
@@ -26,13 +26,18 @@ | |||
26 | */ | 26 | */ |
27 | 27 | ||
28 | #ifdef HAVE_CONFIG_H | 28 | #ifdef HAVE_CONFIG_H |
29 | #include "opus_config.h" | 29 | #include "config.h" |
30 | #endif | 30 | #endif |
31 | 31 | ||
32 | #include "entcode.h" | 32 | #include "entcode.h" |
33 | #include "arch.h" | 33 | #include "arch.h" |
34 | 34 | ||
35 | #if !defined(EC_CLZ) | 35 | #if !defined(EC_CLZ) |
36 | /*This is a fallback for systems where we don't know how to access | ||
37 | a BSR or CLZ instruction (see ecintrin.h). | ||
38 | If you are optimizing Opus on a new platform and it has a native CLZ or | ||
39 | BZR (e.g. cell, MIPS, x86, etc) then making it available to Opus will be | ||
40 | an easy performance win.*/ | ||
36 | int ec_ilog(opus_uint32 _v){ | 41 | int ec_ilog(opus_uint32 _v){ |
37 | /*On a Pentium M, this branchless version tested as the fastest on | 42 | /*On a Pentium M, this branchless version tested as the fastest on |
38 | 1,000,000,000 random 32-bit integers, edging out a similar version with | 43 | 1,000,000,000 random 32-bit integers, edging out a similar version with |
diff --git a/lib/rbcodec/codecs/libopus/celt/entdec.c b/lib/rbcodec/codecs/libopus/celt/entdec.c index ff8442d534..3c264685c2 100644 --- a/lib/rbcodec/codecs/libopus/celt/entdec.c +++ b/lib/rbcodec/codecs/libopus/celt/entdec.c | |||
@@ -26,7 +26,7 @@ | |||
26 | */ | 26 | */ |
27 | 27 | ||
28 | #ifdef HAVE_CONFIG_H | 28 | #ifdef HAVE_CONFIG_H |
29 | #include "opus_config.h" | 29 | #include "config.h" |
30 | #endif | 30 | #endif |
31 | 31 | ||
32 | #include <stddef.h> | 32 | #include <stddef.h> |
@@ -85,7 +85,7 @@ | |||
85 | number=3, | 85 | number=3, |
86 | pages="256--294", | 86 | pages="256--294", |
87 | month=Jul, | 87 | month=Jul, |
88 | URL="http://www.stanford.edu/class/ee398/handouts/papers/Moffat98ArithmCoding.pdf" | 88 | URL="http://www.stanford.edu/class/ee398a/handouts/papers/Moffat98ArithmCoding.pdf" |
89 | }*/ | 89 | }*/ |
90 | 90 | ||
91 | static int ec_read_byte(ec_dec *_this){ | 91 | static int ec_read_byte(ec_dec *_this){ |
diff --git a/lib/rbcodec/codecs/libopus/celt/entenc.c b/lib/rbcodec/codecs/libopus/celt/entenc.c index 0ec6e91fd7..a7e34ecef9 100644 --- a/lib/rbcodec/codecs/libopus/celt/entenc.c +++ b/lib/rbcodec/codecs/libopus/celt/entenc.c | |||
@@ -26,7 +26,7 @@ | |||
26 | */ | 26 | */ |
27 | 27 | ||
28 | #if defined(HAVE_CONFIG_H) | 28 | #if defined(HAVE_CONFIG_H) |
29 | # include "opus_config.h" | 29 | # include "config.h" |
30 | #endif | 30 | #endif |
31 | #include "os_support.h" | 31 | #include "os_support.h" |
32 | #include "arch.h" | 32 | #include "arch.h" |
diff --git a/lib/rbcodec/codecs/libopus/celt/fixed_generic.h b/lib/rbcodec/codecs/libopus/celt/fixed_generic.h index 28a1598d3e..0e77976e83 100644 --- a/lib/rbcodec/codecs/libopus/celt/fixed_generic.h +++ b/lib/rbcodec/codecs/libopus/celt/fixed_generic.h | |||
@@ -42,64 +42,12 @@ | |||
42 | /** 16x32 multiplication, followed by a 16-bit shift right (round-to-nearest). Results fits in 32 bits */ | 42 | /** 16x32 multiplication, followed by a 16-bit shift right (round-to-nearest). Results fits in 32 bits */ |
43 | #define MULT16_32_P16(a,b) ADD32(MULT16_16((a),SHR((b),16)), PSHR(MULT16_16((a),((b)&0x0000ffff)),16)) | 43 | #define MULT16_32_P16(a,b) ADD32(MULT16_16((a),SHR((b),16)), PSHR(MULT16_16((a),((b)&0x0000ffff)),16)) |
44 | 44 | ||
45 | #if defined(CPU_COLDFIRE) | ||
46 | static inline int32_t MULT16_32_Q15(int32_t a, int32_t b) | ||
47 | { | ||
48 | int32_t r; | ||
49 | asm volatile ("mac.l %[a], %[b], %%acc0;" | ||
50 | "movclr.l %%acc0, %[r];" | ||
51 | : [r] "=r" (r) | ||
52 | : [a] "r" (a<<16), [b] "r" (b) | ||
53 | : "cc"); | ||
54 | return r; | ||
55 | } | ||
56 | |||
57 | #elif defined(CPU_ARM) | ||
58 | static inline int32_t MULT16_32_Q15(int32_t a, int32_t b) | ||
59 | { | ||
60 | int32_t lo, hi; | ||
61 | asm volatile("smull %[lo], %[hi], %[b], %[a] \n\t" | ||
62 | "mov %[lo], %[lo], lsr #15 \n\t" | ||
63 | "orr %[hi], %[lo], %[hi], lsl #17 \n\t" | ||
64 | : [lo] "=&r" (lo), [hi] "=&r" (hi) | ||
65 | : [a] "r" (a), [b] "r" (b) ); | ||
66 | return(hi); | ||
67 | } | ||
68 | |||
69 | #else | ||
70 | /** 16x32 multiplication, followed by a 15-bit shift right. Results fits in 32 bits */ | 45 | /** 16x32 multiplication, followed by a 15-bit shift right. Results fits in 32 bits */ |
71 | #define MULT16_32_Q15(a,b) ADD32(SHL(MULT16_16((a),SHR((b),16)),1), SHR(MULT16_16SU((a),((b)&0x0000ffff)),15)) | 46 | #define MULT16_32_Q15(a,b) ADD32(SHL(MULT16_16((a),SHR((b),16)),1), SHR(MULT16_16SU((a),((b)&0x0000ffff)),15)) |
72 | #endif | ||
73 | 47 | ||
74 | #if defined(CPU_COLDFIRE) | ||
75 | static inline int32_t MULT32_32_Q31(int32_t a, int32_t b) | ||
76 | { | ||
77 | int32_t r; | ||
78 | asm volatile ("mac.l %[a], %[b], %%acc0;" | ||
79 | "movclr.l %%acc0, %[r];" | ||
80 | : [r] "=r" (r) | ||
81 | : [a] "r" (a), [b] "r" (b) | ||
82 | : "cc"); | ||
83 | return r; | ||
84 | } | ||
85 | |||
86 | #elif defined(CPU_ARM) | ||
87 | static inline int32_t MULT32_32_Q31(int32_t a, int32_t b) | ||
88 | { | ||
89 | int32_t lo, hi; | ||
90 | asm volatile("smull %[lo], %[hi], %[a], %[b] \n\t" | ||
91 | "mov %[lo], %[lo], lsr #31 \n\t" | ||
92 | "orr %[hi], %[lo], %[hi], lsl #1 \n\t" | ||
93 | : [lo] "=&r" (lo), [hi] "=&r" (hi) | ||
94 | : [a] "r" (a), [b] "r" (b) ); | ||
95 | return(hi); | ||
96 | } | ||
97 | |||
98 | #else | ||
99 | /** 32x32 multiplication, followed by a 31-bit shift right. Results fits in 32 bits */ | 48 | /** 32x32 multiplication, followed by a 31-bit shift right. Results fits in 32 bits */ |
100 | //#define MULT32_32_Q31(a,b) ADD32(ADD32(SHL(MULT16_16(SHR((a),16),SHR((b),16)),1), SHR(MULT16_16SU(SHR((a),16),((b)&0x0000ffff)),15)), SHR(MULT16_16SU(SHR((b),16),((a)&0x0000ffff)),15)) | 49 | #define MULT32_32_Q31(a,b) ADD32(ADD32(SHL(MULT16_16(SHR((a),16),SHR((b),16)),1), SHR(MULT16_16SU(SHR((a),16),((b)&0x0000ffff)),15)), SHR(MULT16_16SU(SHR((b),16),((a)&0x0000ffff)),15)) |
101 | #define MULT32_32_Q31(a,b) (opus_val32)((((int64_t)(a)) * ((int64_t)(b)))>>31) | 50 | |
102 | #endif | ||
103 | /** Compile-time conversion of float constant to 16-bit value */ | 51 | /** Compile-time conversion of float constant to 16-bit value */ |
104 | #define QCONST16(x,bits) ((opus_val16)(.5+(x)*(((opus_val32)1)<<(bits)))) | 52 | #define QCONST16(x,bits) ((opus_val16)(.5+(x)*(((opus_val32)1)<<(bits)))) |
105 | 53 | ||
@@ -136,6 +84,8 @@ static inline int32_t MULT32_32_Q31(int32_t a, int32_t b) | |||
136 | #define PSHR(a,shift) (SHR((a)+((EXTEND32(1)<<((shift))>>1)),shift)) | 84 | #define PSHR(a,shift) (SHR((a)+((EXTEND32(1)<<((shift))>>1)),shift)) |
137 | #define SATURATE(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x))) | 85 | #define SATURATE(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x))) |
138 | 86 | ||
87 | #define SATURATE16(x) (EXTRACT16((x)>32767 ? 32767 : (x)<-32768 ? -32768 : (x))) | ||
88 | |||
139 | /** Shift by a and round-to-neareast 32-bit value. Result is a 16-bit value */ | 89 | /** Shift by a and round-to-neareast 32-bit value. Result is a 16-bit value */ |
140 | #define ROUND16(x,a) (EXTRACT16(PSHR32((x),(a)))) | 90 | #define ROUND16(x,a) (EXTRACT16(PSHR32((x),(a)))) |
141 | /** Divide by two */ | 91 | /** Divide by two */ |
@@ -160,7 +110,9 @@ static inline int32_t MULT32_32_Q31(int32_t a, int32_t b) | |||
160 | 110 | ||
161 | /** 16x16 multiply-add where the result fits in 32 bits */ | 111 | /** 16x16 multiply-add where the result fits in 32 bits */ |
162 | #define MAC16_16(c,a,b) (ADD32((c),MULT16_16((a),(b)))) | 112 | #define MAC16_16(c,a,b) (ADD32((c),MULT16_16((a),(b)))) |
163 | /** 16x32 multiply-add, followed by a 15-bit shift right. Results fits in 32 bits */ | 113 | /** 16x32 multiply, followed by a 15-bit shift right and 32-bit add. |
114 | b must fit in 31 bits. | ||
115 | Result fits in 32 bits. */ | ||
164 | #define MAC16_32_Q15(c,a,b) ADD32(c,ADD32(MULT16_16((a),SHR((b),15)), SHR(MULT16_16((a),((b)&0x00007fff)),15))) | 116 | #define MAC16_32_Q15(c,a,b) ADD32(c,ADD32(MULT16_16((a),SHR((b),15)), SHR(MULT16_16((a),((b)&0x00007fff)),15))) |
165 | 117 | ||
166 | #define MULT16_16_Q11_32(a,b) (SHR(MULT16_16((a),(b)),11)) | 118 | #define MULT16_16_Q11_32(a,b) (SHR(MULT16_16((a),(b)),11)) |
diff --git a/lib/rbcodec/codecs/libopus/celt/kiss_fft.c b/lib/rbcodec/codecs/libopus/celt/kiss_fft.c index 01049d5344..e2b8f3b3da 100644 --- a/lib/rbcodec/codecs/libopus/celt/kiss_fft.c +++ b/lib/rbcodec/codecs/libopus/celt/kiss_fft.c | |||
@@ -31,7 +31,7 @@ | |||
31 | 31 | ||
32 | #ifndef SKIP_CONFIG_H | 32 | #ifndef SKIP_CONFIG_H |
33 | # ifdef HAVE_CONFIG_H | 33 | # ifdef HAVE_CONFIG_H |
34 | # include "opus_config.h" | 34 | # include "config.h" |
35 | # endif | 35 | # endif |
36 | #endif | 36 | #endif |
37 | 37 | ||
@@ -40,7 +40,6 @@ | |||
40 | #include "os_support.h" | 40 | #include "os_support.h" |
41 | #include "mathops.h" | 41 | #include "mathops.h" |
42 | #include "stack_alloc.h" | 42 | #include "stack_alloc.h" |
43 | #include "os_support.h" | ||
44 | 43 | ||
45 | /* The guts header contains all the multiplication and addition macros that are defined for | 44 | /* The guts header contains all the multiplication and addition macros that are defined for |
46 | complex numbers. It also delares the kf_ internal functions. | 45 | complex numbers. It also delares the kf_ internal functions. |
@@ -145,8 +144,6 @@ static void kf_bfly4( | |||
145 | C_ADDTO(*Fout, scratch[1]); | 144 | C_ADDTO(*Fout, scratch[1]); |
146 | C_ADD( scratch[3] , scratch[0] , scratch[2] ); | 145 | C_ADD( scratch[3] , scratch[0] , scratch[2] ); |
147 | C_SUB( scratch[4] , scratch[0] , scratch[2] ); | 146 | C_SUB( scratch[4] , scratch[0] , scratch[2] ); |
148 | Fout[m2].r = PSHR32(Fout[m2].r, 2); | ||
149 | Fout[m2].i = PSHR32(Fout[m2].i, 2); | ||
150 | C_SUB( Fout[m2], *Fout, scratch[3] ); | 147 | C_SUB( Fout[m2], *Fout, scratch[3] ); |
151 | tw1 += fstride; | 148 | tw1 += fstride; |
152 | tw2 += fstride*2; | 149 | tw2 += fstride*2; |
diff --git a/lib/rbcodec/codecs/libopus/celt/kiss_fft.h b/lib/rbcodec/codecs/libopus/celt/kiss_fft.h index c6bb4bfd45..66cf1f2126 100644 --- a/lib/rbcodec/codecs/libopus/celt/kiss_fft.h +++ b/lib/rbcodec/codecs/libopus/celt/kiss_fft.h | |||
@@ -128,7 +128,14 @@ kiss_fft_state *opus_fft_alloc(int nfft,void * mem,size_t * lenmem); | |||
128 | f[k].r and f[k].i | 128 | f[k].r and f[k].i |
129 | * */ | 129 | * */ |
130 | void opus_fft(const kiss_fft_state *cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); | 130 | void opus_fft(const kiss_fft_state *cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); |
131 | void opus_ifft(const kiss_fft_state *cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) ICODE_ATTR; | 131 | |
132 | #if defined(CPU_COLDFIRE) | ||
133 | #define IFFT_ICODE ICODE_ATTR | ||
134 | #else | ||
135 | #define IFFT_ICODE | ||
136 | #endif | ||
137 | |||
138 | void opus_ifft(const kiss_fft_state *cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) IFFT_ICODE; | ||
132 | 139 | ||
133 | void opus_fft_free(const kiss_fft_state *cfg); | 140 | void opus_fft_free(const kiss_fft_state *cfg); |
134 | 141 | ||
diff --git a/lib/rbcodec/codecs/libopus/celt/laplace.c b/lib/rbcodec/codecs/libopus/celt/laplace.c index 6fa4009d57..a7bca874b6 100644 --- a/lib/rbcodec/codecs/libopus/celt/laplace.c +++ b/lib/rbcodec/codecs/libopus/celt/laplace.c | |||
@@ -27,7 +27,7 @@ | |||
27 | */ | 27 | */ |
28 | 28 | ||
29 | #ifdef HAVE_CONFIG_H | 29 | #ifdef HAVE_CONFIG_H |
30 | #include "opus_config.h" | 30 | #include "config.h" |
31 | #endif | 31 | #endif |
32 | 32 | ||
33 | #include "laplace.h" | 33 | #include "laplace.h" |
diff --git a/lib/rbcodec/codecs/libopus/celt/mathops.c b/lib/rbcodec/codecs/libopus/celt/mathops.c index 1af6672592..21fd942960 100644 --- a/lib/rbcodec/codecs/libopus/celt/mathops.c +++ b/lib/rbcodec/codecs/libopus/celt/mathops.c | |||
@@ -32,7 +32,7 @@ | |||
32 | */ | 32 | */ |
33 | 33 | ||
34 | #ifdef HAVE_CONFIG_H | 34 | #ifdef HAVE_CONFIG_H |
35 | #include "opus_config.h" | 35 | #include "config.h" |
36 | #endif | 36 | #endif |
37 | 37 | ||
38 | #include "mathops.h" | 38 | #include "mathops.h" |
@@ -123,6 +123,8 @@ opus_val32 celt_sqrt(opus_val32 x) | |||
123 | static const opus_val16 C[5] = {23175, 11561, -3011, 1699, -664}; | 123 | static const opus_val16 C[5] = {23175, 11561, -3011, 1699, -664}; |
124 | if (x==0) | 124 | if (x==0) |
125 | return 0; | 125 | return 0; |
126 | else if (x>=1073741824) | ||
127 | return 32767; | ||
126 | k = (celt_ilog2(x)>>1)-7; | 128 | k = (celt_ilog2(x)>>1)-7; |
127 | x = VSHR32(x, 2*k); | 129 | x = VSHR32(x, 2*k); |
128 | n = x-32768; | 130 | n = x-32768; |
diff --git a/lib/rbcodec/codecs/libopus/celt/mathops.h b/lib/rbcodec/codecs/libopus/celt/mathops.h index 4e97795606..44fa97c697 100644 --- a/lib/rbcodec/codecs/libopus/celt/mathops.h +++ b/lib/rbcodec/codecs/libopus/celt/mathops.h | |||
@@ -43,6 +43,41 @@ | |||
43 | 43 | ||
44 | unsigned isqrt32(opus_uint32 _val); | 44 | unsigned isqrt32(opus_uint32 _val); |
45 | 45 | ||
46 | #ifndef OVERRIDE_CELT_MAXABS16 | ||
47 | static inline opus_val32 celt_maxabs16(const opus_val16 *x, int len) | ||
48 | { | ||
49 | int i; | ||
50 | opus_val16 maxval = 0; | ||
51 | opus_val16 minval = 0; | ||
52 | for (i=0;i<len;i++) | ||
53 | { | ||
54 | maxval = MAX16(maxval, x[i]); | ||
55 | minval = MIN16(minval, x[i]); | ||
56 | } | ||
57 | return MAX32(EXTEND32(maxval),-EXTEND32(minval)); | ||
58 | } | ||
59 | #endif | ||
60 | |||
61 | #ifndef OVERRIDE_CELT_MAXABS32 | ||
62 | #ifdef FIXED_POINT | ||
63 | static inline opus_val32 celt_maxabs32(const opus_val32 *x, int len) | ||
64 | { | ||
65 | int i; | ||
66 | opus_val32 maxval = 0; | ||
67 | opus_val32 minval = 0; | ||
68 | for (i=0;i<len;i++) | ||
69 | { | ||
70 | maxval = MAX32(maxval, x[i]); | ||
71 | minval = MIN32(minval, x[i]); | ||
72 | } | ||
73 | return MAX32(maxval, -minval); | ||
74 | } | ||
75 | #else | ||
76 | #define celt_maxabs32(x,len) celt_maxabs16(x,len) | ||
77 | #endif | ||
78 | #endif | ||
79 | |||
80 | |||
46 | #ifndef FIXED_POINT | 81 | #ifndef FIXED_POINT |
47 | 82 | ||
48 | #define PI 3.141592653f | 83 | #define PI 3.141592653f |
@@ -117,27 +152,6 @@ static inline opus_int16 celt_ilog2(opus_int32 x) | |||
117 | } | 152 | } |
118 | #endif | 153 | #endif |
119 | 154 | ||
120 | #ifndef OVERRIDE_CELT_MAXABS16 | ||
121 | static inline opus_val16 celt_maxabs16(opus_val16 *x, int len) | ||
122 | { | ||
123 | int i; | ||
124 | opus_val16 maxval = 0; | ||
125 | for (i=0;i<len;i++) | ||
126 | maxval = MAX16(maxval, ABS16(x[i])); | ||
127 | return maxval; | ||
128 | } | ||
129 | #endif | ||
130 | |||
131 | #ifndef OVERRIDE_CELT_MAXABS32 | ||
132 | static inline opus_val32 celt_maxabs32(opus_val32 *x, int len) | ||
133 | { | ||
134 | int i; | ||
135 | opus_val32 maxval = 0; | ||
136 | for (i=0;i<len;i++) | ||
137 | maxval = MAX32(maxval, ABS32(x[i])); | ||
138 | return maxval; | ||
139 | } | ||
140 | #endif | ||
141 | 155 | ||
142 | /** Integer log in base2. Defined for zero, but not for negative numbers */ | 156 | /** Integer log in base2. Defined for zero, but not for negative numbers */ |
143 | static inline opus_int16 celt_zlog2(opus_val32 x) | 157 | static inline opus_int16 celt_zlog2(opus_val32 x) |
@@ -176,6 +190,13 @@ static inline opus_val16 celt_log2(opus_val32 x) | |||
176 | #define D1 22804 | 190 | #define D1 22804 |
177 | #define D2 14819 | 191 | #define D2 14819 |
178 | #define D3 10204 | 192 | #define D3 10204 |
193 | |||
194 | static inline opus_val32 celt_exp2_frac(opus_val16 x) | ||
195 | { | ||
196 | opus_val16 frac; | ||
197 | frac = SHL16(x, 4); | ||
198 | return ADD16(D0, MULT16_16_Q15(frac, ADD16(D1, MULT16_16_Q15(frac, ADD16(D2 , MULT16_16_Q15(D3,frac)))))); | ||
199 | } | ||
179 | /** Base-2 exponential approximation (2^x). (Q10 input, Q16 output) */ | 200 | /** Base-2 exponential approximation (2^x). (Q10 input, Q16 output) */ |
180 | static inline opus_val32 celt_exp2(opus_val16 x) | 201 | static inline opus_val32 celt_exp2(opus_val16 x) |
181 | { | 202 | { |
@@ -186,8 +207,7 @@ static inline opus_val32 celt_exp2(opus_val16 x) | |||
186 | return 0x7f000000; | 207 | return 0x7f000000; |
187 | else if (integer < -15) | 208 | else if (integer < -15) |
188 | return 0; | 209 | return 0; |
189 | frac = SHL16(x-SHL16(integer,10),4); | 210 | frac = celt_exp2_frac(x-SHL16(integer,10)); |
190 | frac = ADD16(D0, MULT16_16_Q15(frac, ADD16(D1, MULT16_16_Q15(frac, ADD16(D2 , MULT16_16_Q15(D3,frac)))))); | ||
191 | return VSHR32(EXTEND32(frac), -integer-2); | 211 | return VSHR32(EXTEND32(frac), -integer-2); |
192 | } | 212 | } |
193 | 213 | ||
diff --git a/lib/rbcodec/codecs/libopus/celt/mdct.c b/lib/rbcodec/codecs/libopus/celt/mdct.c index 0df77fd5ec..72ea180568 100644 --- a/lib/rbcodec/codecs/libopus/celt/mdct.c +++ b/lib/rbcodec/codecs/libopus/celt/mdct.c | |||
@@ -41,7 +41,7 @@ | |||
41 | 41 | ||
42 | #ifndef SKIP_CONFIG_H | 42 | #ifndef SKIP_CONFIG_H |
43 | #ifdef HAVE_CONFIG_H | 43 | #ifdef HAVE_CONFIG_H |
44 | #include "opus_config.h" | 44 | #include "config.h" |
45 | #endif | 45 | #endif |
46 | #endif | 46 | #endif |
47 | 47 | ||
@@ -110,12 +110,14 @@ void clt_mdct_forward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar | |||
110 | int N, N2, N4; | 110 | int N, N2, N4; |
111 | kiss_twiddle_scalar sine; | 111 | kiss_twiddle_scalar sine; |
112 | VARDECL(kiss_fft_scalar, f); | 112 | VARDECL(kiss_fft_scalar, f); |
113 | VARDECL(kiss_fft_scalar, f2); | ||
113 | SAVE_STACK; | 114 | SAVE_STACK; |
114 | N = l->n; | 115 | N = l->n; |
115 | N >>= shift; | 116 | N >>= shift; |
116 | N2 = N>>1; | 117 | N2 = N>>1; |
117 | N4 = N>>2; | 118 | N4 = N>>2; |
118 | ALLOC(f, N2, kiss_fft_scalar); | 119 | ALLOC(f, N2, kiss_fft_scalar); |
120 | ALLOC(f2, N2, kiss_fft_scalar); | ||
119 | /* sin(x) ~= x here */ | 121 | /* sin(x) ~= x here */ |
120 | #ifdef FIXED_POINT | 122 | #ifdef FIXED_POINT |
121 | sine = TRIG_UPSCALE*(QCONST16(0.7853981f, 15)+N2)/N; | 123 | sine = TRIG_UPSCALE*(QCONST16(0.7853981f, 15)+N2)/N; |
@@ -132,7 +134,7 @@ void clt_mdct_forward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar | |||
132 | kiss_fft_scalar * OPUS_RESTRICT yp = f; | 134 | kiss_fft_scalar * OPUS_RESTRICT yp = f; |
133 | const opus_val16 * OPUS_RESTRICT wp1 = window+(overlap>>1); | 135 | const opus_val16 * OPUS_RESTRICT wp1 = window+(overlap>>1); |
134 | const opus_val16 * OPUS_RESTRICT wp2 = window+(overlap>>1)-1; | 136 | const opus_val16 * OPUS_RESTRICT wp2 = window+(overlap>>1)-1; |
135 | for(i=0;i<(overlap>>2);i++) | 137 | for(i=0;i<((overlap+3)>>2);i++) |
136 | { | 138 | { |
137 | /* Real part arranged as -d-cR, Imag part arranged as -b+aR*/ | 139 | /* Real part arranged as -d-cR, Imag part arranged as -b+aR*/ |
138 | *yp++ = MULT16_32_Q15(*wp2, xp1[N2]) + MULT16_32_Q15(*wp1,*xp2); | 140 | *yp++ = MULT16_32_Q15(*wp2, xp1[N2]) + MULT16_32_Q15(*wp1,*xp2); |
@@ -144,7 +146,7 @@ void clt_mdct_forward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar | |||
144 | } | 146 | } |
145 | wp1 = window; | 147 | wp1 = window; |
146 | wp2 = window+overlap-1; | 148 | wp2 = window+overlap-1; |
147 | for(;i<N4-(overlap>>2);i++) | 149 | for(;i<N4-((overlap+3)>>2);i++) |
148 | { | 150 | { |
149 | /* Real part arranged as a-bR, Imag part arranged as -c-dR */ | 151 | /* Real part arranged as a-bR, Imag part arranged as -c-dR */ |
150 | *yp++ = *xp2; | 152 | *yp++ = *xp2; |
@@ -181,12 +183,12 @@ void clt_mdct_forward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar | |||
181 | } | 183 | } |
182 | 184 | ||
183 | /* N/4 complex FFT, down-scales by 4/N */ | 185 | /* N/4 complex FFT, down-scales by 4/N */ |
184 | opus_fft(l->kfft[shift], (kiss_fft_cpx *)f, (kiss_fft_cpx *)in); | 186 | opus_fft(l->kfft[shift], (kiss_fft_cpx *)f, (kiss_fft_cpx *)f2); |
185 | 187 | ||
186 | /* Post-rotate */ | 188 | /* Post-rotate */ |
187 | { | 189 | { |
188 | /* Temp pointers to make it really clear to the compiler what we're doing */ | 190 | /* Temp pointers to make it really clear to the compiler what we're doing */ |
189 | const kiss_fft_scalar * OPUS_RESTRICT fp = in; | 191 | const kiss_fft_scalar * OPUS_RESTRICT fp = f2; |
190 | kiss_fft_scalar * OPUS_RESTRICT yp1 = out; | 192 | kiss_fft_scalar * OPUS_RESTRICT yp1 = out; |
191 | kiss_fft_scalar * OPUS_RESTRICT yp2 = out+stride*(N2-1); | 193 | kiss_fft_scalar * OPUS_RESTRICT yp2 = out+stride*(N2-1); |
192 | const kiss_twiddle_scalar *t = &l->trig[0]; | 194 | const kiss_twiddle_scalar *t = &l->trig[0]; |
@@ -208,35 +210,20 @@ void clt_mdct_forward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar | |||
208 | } | 210 | } |
209 | #endif | 211 | #endif |
210 | 212 | ||
211 | #define S_F_BUF_SIZE (1920>>1) /* N = 1920 for static modes */ | ||
212 | static kiss_fft_scalar s_f2[S_F_BUF_SIZE] IBSS_ATTR MEM_ALIGN_ATTR; | ||
213 | void clt_mdct_backward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar * OPUS_RESTRICT out, | 213 | void clt_mdct_backward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar * OPUS_RESTRICT out, |
214 | const opus_val16 * OPUS_RESTRICT window, int overlap, int shift, int stride) | 214 | const opus_val16 * OPUS_RESTRICT window, int overlap, int shift, int stride) |
215 | { | 215 | { |
216 | int i; | 216 | int i; |
217 | int N, N2, N4; | 217 | int N, N2, N4; |
218 | int tstride = 1<<shift; | ||
219 | kiss_twiddle_scalar sine; | 218 | kiss_twiddle_scalar sine; |
220 | VARDECL(kiss_fft_scalar, f); | 219 | /* VARDECL(kiss_fft_scalar, f2); |
221 | VARDECL(kiss_fft_scalar, f2); | 220 | SAVE_STACK; */ |
222 | SAVE_STACK; | ||
223 | N = l->n; | 221 | N = l->n; |
224 | N >>= shift; | 222 | N >>= shift; |
225 | N2 = N>>1; | 223 | N2 = N>>1; |
226 | N4 = N>>2; | 224 | N4 = N>>2; |
227 | kiss_fft_scalar s_f[S_F_BUF_SIZE]; | 225 | /* ALLOC(f2, N2, kiss_fft_scalar); */ |
228 | 226 | kiss_fft_scalar f2[N2]; /* worst case 3840b */ | |
229 | if (S_F_BUF_SIZE >= N2) | ||
230 | { | ||
231 | f = s_f; | ||
232 | f2 = s_f2; | ||
233 | } | ||
234 | else | ||
235 | { | ||
236 | ALLOC(f , N2, kiss_fft_scalar); | ||
237 | ALLOC(f2, N2, kiss_fft_scalar); | ||
238 | } | ||
239 | |||
240 | /* sin(x) ~= x here */ | 227 | /* sin(x) ~= x here */ |
241 | #ifdef FIXED_POINT | 228 | #ifdef FIXED_POINT |
242 | sine = TRIG_UPSCALE*(QCONST16(0.7853981f, 15)+N2)/N; | 229 | sine = TRIG_UPSCALE*(QCONST16(0.7853981f, 15)+N2)/N; |
@@ -250,102 +237,78 @@ void clt_mdct_backward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scala | |||
250 | const kiss_fft_scalar * OPUS_RESTRICT xp1 = in; | 237 | const kiss_fft_scalar * OPUS_RESTRICT xp1 = in; |
251 | const kiss_fft_scalar * OPUS_RESTRICT xp2 = in+stride*(N2-1); | 238 | const kiss_fft_scalar * OPUS_RESTRICT xp2 = in+stride*(N2-1); |
252 | kiss_fft_scalar * OPUS_RESTRICT yp = f2; | 239 | kiss_fft_scalar * OPUS_RESTRICT yp = f2; |
253 | const kiss_twiddle_scalar *t0 = &l->trig[0]; | 240 | const kiss_twiddle_scalar *t = &l->trig[0]; |
254 | const kiss_twiddle_scalar *t1 = &l->trig[N4<<shift]; | ||
255 | for(i=0;i<N4;i++) | 241 | for(i=0;i<N4;i++) |
256 | { | 242 | { |
257 | kiss_fft_scalar yr, yi; | 243 | kiss_fft_scalar yr, yi; |
258 | yr = -S_MUL(*xp2, *t0) + S_MUL(*xp1, *t1); | 244 | yr = -S_MUL(*xp2, t[i<<shift]) + S_MUL(*xp1,t[(N4-i)<<shift]); |
259 | yi = -S_MUL(*xp2, *t1) - S_MUL(*xp1, *t0); | 245 | yi = -S_MUL(*xp2, t[(N4-i)<<shift]) - S_MUL(*xp1,t[i<<shift]); |
260 | /* works because the cos is nearly one */ | 246 | /* works because the cos is nearly one */ |
261 | *yp++ = yr - S_MUL(yi,sine); | 247 | *yp++ = yr - S_MUL(yi,sine); |
262 | *yp++ = yi + S_MUL(yr,sine); | 248 | *yp++ = yi + S_MUL(yr,sine); |
263 | xp1+=2*stride; | 249 | xp1+=2*stride; |
264 | xp2-=2*stride; | 250 | xp2-=2*stride; |
265 | t0 += tstride; | ||
266 | t1 -= tstride; | ||
267 | } | 251 | } |
268 | } | 252 | } |
269 | 253 | ||
270 | /* Inverse N/4 complex FFT. This one should *not* downscale even in fixed-point */ | 254 | /* Inverse N/4 complex FFT. This one should *not* downscale even in fixed-point */ |
271 | opus_ifft(l->kfft[shift], (kiss_fft_cpx *)f2, (kiss_fft_cpx *)f); | 255 | opus_ifft(l->kfft[shift], (kiss_fft_cpx *)f2, (kiss_fft_cpx *)(out+(overlap>>1))); |
272 | 256 | ||
273 | /* Post-rotate */ | 257 | /* Post-rotate and de-shuffle from both ends of the buffer at once to make |
258 | it in-place. */ | ||
274 | { | 259 | { |
275 | kiss_fft_scalar * OPUS_RESTRICT fp = f; | 260 | kiss_fft_scalar * OPUS_RESTRICT yp0 = out+(overlap>>1); |
276 | const kiss_twiddle_scalar *t0 = &l->trig[0]; | 261 | kiss_fft_scalar * OPUS_RESTRICT yp1 = out+(overlap>>1)+N2-2; |
277 | const kiss_twiddle_scalar *t1 = &l->trig[N4<<shift]; | 262 | const kiss_twiddle_scalar *t = &l->trig[0]; |
278 | for(i=0;i<N4;i++) | 263 | /* Loop to (N4+1)>>1 to handle odd N4. When N4 is odd, the |
264 | middle pair will be computed twice. */ | ||
265 | for(i=0;i<(N4+1)>>1;i++) | ||
279 | { | 266 | { |
280 | kiss_fft_scalar re, im, yr, yi; | 267 | kiss_fft_scalar re, im, yr, yi; |
281 | re = fp[0]; | 268 | kiss_twiddle_scalar t0, t1; |
282 | im = fp[1]; | 269 | re = yp0[0]; |
270 | im = yp0[1]; | ||
271 | t0 = t[i<<shift]; | ||
272 | t1 = t[(N4-i)<<shift]; | ||
283 | /* We'd scale up by 2 here, but instead it's done when mixing the windows */ | 273 | /* We'd scale up by 2 here, but instead it's done when mixing the windows */ |
284 | yr = S_MUL(re, *t0) - S_MUL(im, *t1); | 274 | yr = S_MUL(re,t0) - S_MUL(im,t1); |
285 | yi = S_MUL(im, *t0) + S_MUL(re, *t1); | 275 | yi = S_MUL(im,t0) + S_MUL(re,t1); |
276 | re = yp1[0]; | ||
277 | im = yp1[1]; | ||
286 | /* works because the cos is nearly one */ | 278 | /* works because the cos is nearly one */ |
287 | *fp++ = yr - S_MUL(yi,sine); | 279 | yp0[0] = -(yr - S_MUL(yi,sine)); |
288 | *fp++ = yi + S_MUL(yr,sine); | 280 | yp1[1] = yi + S_MUL(yr,sine); |
289 | t0 += tstride; | ||
290 | t1 -= tstride; | ||
291 | } | ||
292 | } | ||
293 | /* De-shuffle the components for the middle of the window only */ | ||
294 | { | ||
295 | const kiss_fft_scalar * OPUS_RESTRICT fp1 = f; | ||
296 | const kiss_fft_scalar * OPUS_RESTRICT fp2 = f+N2-1; | ||
297 | kiss_fft_scalar * OPUS_RESTRICT yp = f2; | ||
298 | for(i = 0; i < N4; i++) | ||
299 | { | ||
300 | *yp++ =-*fp1; | ||
301 | *yp++ = *fp2; | ||
302 | fp1 += 2; | ||
303 | fp2 -= 2; | ||
304 | } | ||
305 | } | ||
306 | out -= (N2-overlap)>>1; | ||
307 | /* Mirror on both sides for TDAC */ | ||
308 | { | ||
309 | kiss_fft_scalar * OPUS_RESTRICT fp1 = f2+N4-1; | ||
310 | kiss_fft_scalar * OPUS_RESTRICT xp1 = out+N2-1; | ||
311 | kiss_fft_scalar * OPUS_RESTRICT yp1 = out+N4-overlap/2; | ||
312 | const opus_val16 * OPUS_RESTRICT wp1 = window; | ||
313 | const opus_val16 * OPUS_RESTRICT wp2 = window+overlap-1; | ||
314 | 281 | ||
315 | i = N4-overlap/2; | 282 | t0 = t[(N4-i-1)<<shift]; |
316 | xp1 -= N4-overlap/2; | 283 | t1 = t[(i+1)<<shift]; |
317 | fp1 -= N4-overlap/2; | 284 | /* We'd scale up by 2 here, but instead it's done when mixing the windows */ |
318 | OPUS_COPY(xp1+1, fp1+1, N4-overlap/2); | 285 | yr = S_MUL(re,t0) - S_MUL(im,t1); |
319 | for(; i < N4; i++) | 286 | yi = S_MUL(im,t0) + S_MUL(re,t1); |
320 | { | 287 | /* works because the cos is nearly one */ |
321 | kiss_fft_scalar x1; | 288 | yp1[0] = -(yr - S_MUL(yi,sine)); |
322 | x1 = *fp1--; | 289 | yp0[1] = yi + S_MUL(yr,sine); |
323 | *yp1++ +=-MULT16_32_Q15(*wp1, x1); | 290 | yp0 += 2; |
324 | *xp1-- += MULT16_32_Q15(*wp2, x1); | 291 | yp1 -= 2; |
325 | wp1++; | ||
326 | wp2--; | ||
327 | } | 292 | } |
328 | } | 293 | } |
294 | |||
295 | /* Mirror on both sides for TDAC */ | ||
329 | { | 296 | { |
330 | kiss_fft_scalar * OPUS_RESTRICT fp2 = f2+N4; | 297 | kiss_fft_scalar * OPUS_RESTRICT xp1 = out+overlap-1; |
331 | kiss_fft_scalar * OPUS_RESTRICT xp2 = out+N2; | 298 | kiss_fft_scalar * OPUS_RESTRICT yp1 = out; |
332 | kiss_fft_scalar * OPUS_RESTRICT yp2 = out+N-1-(N4-overlap/2); | ||
333 | const opus_val16 * OPUS_RESTRICT wp1 = window; | 299 | const opus_val16 * OPUS_RESTRICT wp1 = window; |
334 | const opus_val16 * OPUS_RESTRICT wp2 = window+overlap-1; | 300 | const opus_val16 * OPUS_RESTRICT wp2 = window+overlap-1; |
335 | 301 | ||
336 | i = N4-overlap/2; | 302 | for(i = 0; i < overlap/2; i++) |
337 | OPUS_COPY(xp2, fp2, N4-overlap/2); | ||
338 | xp2 += N4-overlap/2; | ||
339 | fp2 += N4-overlap/2; | ||
340 | for(; i < N4; i++) | ||
341 | { | 303 | { |
342 | kiss_fft_scalar x2; | 304 | kiss_fft_scalar x1, x2; |
343 | x2 = *fp2++; | 305 | x1 = *xp1; |
344 | *yp2-- = MULT16_32_Q15(*wp1, x2); | 306 | x2 = *yp1; |
345 | *xp2++ = MULT16_32_Q15(*wp2, x2); | 307 | *yp1++ = MULT16_32_Q15(*wp2, x2) - MULT16_32_Q15(*wp1, x1); |
308 | *xp1-- = MULT16_32_Q15(*wp1, x2) + MULT16_32_Q15(*wp2, x1); | ||
346 | wp1++; | 309 | wp1++; |
347 | wp2--; | 310 | wp2--; |
348 | } | 311 | } |
349 | } | 312 | } |
350 | RESTORE_STACK; | 313 | /* RESTORE_STACK; */ |
351 | } | 314 | } |
diff --git a/lib/rbcodec/codecs/libopus/celt/mdct.h b/lib/rbcodec/codecs/libopus/celt/mdct.h index 933aafcda1..d72182138a 100644 --- a/lib/rbcodec/codecs/libopus/celt/mdct.h +++ b/lib/rbcodec/codecs/libopus/celt/mdct.h | |||
@@ -61,11 +61,6 @@ void clt_mdct_forward(const mdct_lookup *l, kiss_fft_scalar *in, | |||
61 | kiss_fft_scalar * OPUS_RESTRICT out, | 61 | kiss_fft_scalar * OPUS_RESTRICT out, |
62 | const opus_val16 *window, int overlap, int shift, int stride); | 62 | const opus_val16 *window, int overlap, int shift, int stride); |
63 | 63 | ||
64 | #if defined(CPU_COLDFIRE) | ||
65 | #define MDCT_ICODE ICODE_ATTR | ||
66 | #else | ||
67 | #define MDCT_ICODE | ||
68 | #endif | ||
69 | /** Compute a backward MDCT (no scaling) and performs weighted overlap-add | 64 | /** Compute a backward MDCT (no scaling) and performs weighted overlap-add |
70 | (scales implicitly by 1/2) */ | 65 | (scales implicitly by 1/2) */ |
71 | void clt_mdct_backward(const mdct_lookup *l, kiss_fft_scalar *in, | 66 | void clt_mdct_backward(const mdct_lookup *l, kiss_fft_scalar *in, |
diff --git a/lib/rbcodec/codecs/libopus/celt/modes.c b/lib/rbcodec/codecs/libopus/celt/modes.c index d44cb3b9de..42e68e1cb7 100644 --- a/lib/rbcodec/codecs/libopus/celt/modes.c +++ b/lib/rbcodec/codecs/libopus/celt/modes.c | |||
@@ -28,7 +28,7 @@ | |||
28 | */ | 28 | */ |
29 | 29 | ||
30 | #ifdef HAVE_CONFIG_H | 30 | #ifdef HAVE_CONFIG_H |
31 | #include "opus_config.h" | 31 | #include "config.h" |
32 | #endif | 32 | #endif |
33 | 33 | ||
34 | #include "celt.h" | 34 | #include "celt.h" |
@@ -345,6 +345,14 @@ CELTMode *opus_custom_mode_create(opus_int32 Fs, int frame_size, int *error) | |||
345 | mode->eBands = compute_ebands(Fs, mode->shortMdctSize, res, &mode->nbEBands); | 345 | mode->eBands = compute_ebands(Fs, mode->shortMdctSize, res, &mode->nbEBands); |
346 | if (mode->eBands==NULL) | 346 | if (mode->eBands==NULL) |
347 | goto failure; | 347 | goto failure; |
348 | #if !defined(SMALL_FOOTPRINT) | ||
349 | /* Make sure we don't allocate a band larger than our PVQ table. | ||
350 | 208 should be enough, but let's be paranoid. */ | ||
351 | if ((mode->eBands[mode->nbEBands] - mode->eBands[mode->nbEBands-1])<<LM > | ||
352 | 208) { | ||
353 | goto failure; | ||
354 | } | ||
355 | #endif | ||
348 | 356 | ||
349 | mode->effEBands = mode->nbEBands; | 357 | mode->effEBands = mode->nbEBands; |
350 | while (mode->eBands[mode->effEBands] > mode->shortMdctSize) | 358 | while (mode->eBands[mode->effEBands] > mode->shortMdctSize) |
diff --git a/lib/rbcodec/codecs/libopus/celt/pitch.c b/lib/rbcodec/codecs/libopus/celt/pitch.c index 1b7efd945d..0d8be13025 100644 --- a/lib/rbcodec/codecs/libopus/celt/pitch.c +++ b/lib/rbcodec/codecs/libopus/celt/pitch.c | |||
@@ -32,7 +32,7 @@ | |||
32 | */ | 32 | */ |
33 | 33 | ||
34 | #ifdef HAVE_CONFIG_H | 34 | #ifdef HAVE_CONFIG_H |
35 | #include "opus_config.h" | 35 | #include "config.h" |
36 | #endif | 36 | #endif |
37 | 37 | ||
38 | #include "pitch.h" | 38 | #include "pitch.h" |
@@ -77,7 +77,7 @@ static void find_best_pitch(opus_val32 *xcorr, opus_val16 *y, int len, | |||
77 | #ifndef FIXED_POINT | 77 | #ifndef FIXED_POINT |
78 | /* Considering the range of xcorr16, this should avoid both underflows | 78 | /* Considering the range of xcorr16, this should avoid both underflows |
79 | and overflows (inf) when squaring xcorr16 */ | 79 | and overflows (inf) when squaring xcorr16 */ |
80 | xcorr16 *= 1e-12; | 80 | xcorr16 *= 1e-12f; |
81 | #endif | 81 | #endif |
82 | num = MULT16_16_Q15(xcorr16,xcorr16); | 82 | num = MULT16_16_Q15(xcorr16,xcorr16); |
83 | if (MULT16_32_Q15(num,best_den[1]) > MULT16_32_Q15(best_num[1],Syy)) | 83 | if (MULT16_32_Q15(num,best_den[1]) > MULT16_32_Q15(best_num[1],Syy)) |
@@ -102,13 +102,57 @@ static void find_best_pitch(opus_val32 *xcorr, opus_val16 *y, int len, | |||
102 | } | 102 | } |
103 | } | 103 | } |
104 | 104 | ||
105 | static void celt_fir5(const opus_val16 *x, | ||
106 | const opus_val16 *num, | ||
107 | opus_val16 *y, | ||
108 | int N, | ||
109 | opus_val16 *mem) | ||
110 | { | ||
111 | int i; | ||
112 | opus_val16 num0, num1, num2, num3, num4; | ||
113 | opus_val32 mem0, mem1, mem2, mem3, mem4; | ||
114 | num0=num[0]; | ||
115 | num1=num[1]; | ||
116 | num2=num[2]; | ||
117 | num3=num[3]; | ||
118 | num4=num[4]; | ||
119 | mem0=mem[0]; | ||
120 | mem1=mem[1]; | ||
121 | mem2=mem[2]; | ||
122 | mem3=mem[3]; | ||
123 | mem4=mem[4]; | ||
124 | for (i=0;i<N;i++) | ||
125 | { | ||
126 | opus_val32 sum = SHL32(EXTEND32(x[i]), SIG_SHIFT); | ||
127 | sum = MAC16_16(sum,num0,mem0); | ||
128 | sum = MAC16_16(sum,num1,mem1); | ||
129 | sum = MAC16_16(sum,num2,mem2); | ||
130 | sum = MAC16_16(sum,num3,mem3); | ||
131 | sum = MAC16_16(sum,num4,mem4); | ||
132 | mem4 = mem3; | ||
133 | mem3 = mem2; | ||
134 | mem2 = mem1; | ||
135 | mem1 = mem0; | ||
136 | mem0 = x[i]; | ||
137 | y[i] = ROUND16(sum, SIG_SHIFT); | ||
138 | } | ||
139 | mem[0]=mem0; | ||
140 | mem[1]=mem1; | ||
141 | mem[2]=mem2; | ||
142 | mem[3]=mem3; | ||
143 | mem[4]=mem4; | ||
144 | } | ||
145 | |||
146 | |||
105 | void pitch_downsample(celt_sig * OPUS_RESTRICT x[], opus_val16 * OPUS_RESTRICT x_lp, | 147 | void pitch_downsample(celt_sig * OPUS_RESTRICT x[], opus_val16 * OPUS_RESTRICT x_lp, |
106 | int len, int C) | 148 | int len, int C) |
107 | { | 149 | { |
108 | int i; | 150 | int i; |
109 | opus_val32 ac[5]; | 151 | opus_val32 ac[5]; |
110 | opus_val16 tmp=Q15ONE; | 152 | opus_val16 tmp=Q15ONE; |
111 | opus_val16 lpc[4], mem[4]={0,0,0,0}; | 153 | opus_val16 lpc[4], mem[5]={0,0,0,0,0}; |
154 | opus_val16 lpc2[5]; | ||
155 | opus_val16 c1 = QCONST16(.8f,15); | ||
112 | #ifdef FIXED_POINT | 156 | #ifdef FIXED_POINT |
113 | int shift; | 157 | int shift; |
114 | opus_val32 maxabs = celt_maxabs32(x[0], len); | 158 | opus_val32 maxabs = celt_maxabs32(x[0], len); |
@@ -161,14 +205,89 @@ void pitch_downsample(celt_sig * OPUS_RESTRICT x[], opus_val16 * OPUS_RESTRICT x | |||
161 | tmp = MULT16_16_Q15(QCONST16(.9f,15), tmp); | 205 | tmp = MULT16_16_Q15(QCONST16(.9f,15), tmp); |
162 | lpc[i] = MULT16_16_Q15(lpc[i], tmp); | 206 | lpc[i] = MULT16_16_Q15(lpc[i], tmp); |
163 | } | 207 | } |
164 | celt_fir(x_lp, lpc, x_lp, len>>1, 4, mem); | 208 | /* Add a zero */ |
209 | lpc2[0] = lpc[0] + QCONST16(.8f,SIG_SHIFT); | ||
210 | lpc2[1] = lpc[1] + MULT16_16_Q15(c1,lpc[0]); | ||
211 | lpc2[2] = lpc[2] + MULT16_16_Q15(c1,lpc[1]); | ||
212 | lpc2[3] = lpc[3] + MULT16_16_Q15(c1,lpc[2]); | ||
213 | lpc2[4] = MULT16_16_Q15(c1,lpc[3]); | ||
214 | celt_fir5(x_lp, lpc2, x_lp, len>>1, mem); | ||
215 | } | ||
165 | 216 | ||
166 | mem[0]=0; | 217 | #if 0 /* This is a simple version of the pitch correlation that should work |
167 | lpc[0]=QCONST16(.8f,12); | 218 | well on DSPs like Blackfin and TI C5x/C6x */ |
168 | celt_fir(x_lp, lpc, x_lp, len>>1, 1, mem); | ||
169 | 219 | ||
220 | #ifdef FIXED_POINT | ||
221 | opus_val32 | ||
222 | #else | ||
223 | void | ||
224 | #endif | ||
225 | celt_pitch_xcorr(opus_val16 *x, opus_val16 *y, opus_val32 *xcorr, int len, int max_pitch) | ||
226 | { | ||
227 | int i, j; | ||
228 | #ifdef FIXED_POINT | ||
229 | opus_val32 maxcorr=1; | ||
230 | #endif | ||
231 | for (i=0;i<max_pitch;i++) | ||
232 | { | ||
233 | opus_val32 sum = 0; | ||
234 | for (j=0;j<len;j++) | ||
235 | sum = MAC16_16(sum, x[j],y[i+j]); | ||
236 | xcorr[i] = sum; | ||
237 | #ifdef FIXED_POINT | ||
238 | maxcorr = MAX32(maxcorr, sum); | ||
239 | #endif | ||
240 | } | ||
241 | #ifdef FIXED_POINT | ||
242 | return maxcorr; | ||
243 | #endif | ||
170 | } | 244 | } |
171 | 245 | ||
246 | #else /* Unrolled version of the pitch correlation -- runs faster on x86 and ARM */ | ||
247 | |||
248 | #ifdef FIXED_POINT | ||
249 | opus_val32 | ||
250 | #else | ||
251 | void | ||
252 | #endif | ||
253 | celt_pitch_xcorr(const opus_val16 *_x, const opus_val16 *_y, opus_val32 *xcorr, int len, int max_pitch) | ||
254 | { | ||
255 | int i,j; | ||
256 | #ifdef FIXED_POINT | ||
257 | opus_val32 maxcorr=1; | ||
258 | #endif | ||
259 | for (i=0;i<max_pitch-3;i+=4) | ||
260 | { | ||
261 | opus_val32 sum[4]={0,0,0,0}; | ||
262 | xcorr_kernel(_x, _y+i, sum, len); | ||
263 | xcorr[i]=sum[0]; | ||
264 | xcorr[i+1]=sum[1]; | ||
265 | xcorr[i+2]=sum[2]; | ||
266 | xcorr[i+3]=sum[3]; | ||
267 | #ifdef FIXED_POINT | ||
268 | sum[0] = MAX32(sum[0], sum[1]); | ||
269 | sum[2] = MAX32(sum[2], sum[3]); | ||
270 | sum[0] = MAX32(sum[0], sum[2]); | ||
271 | maxcorr = MAX32(maxcorr, sum[0]); | ||
272 | #endif | ||
273 | } | ||
274 | /* In case max_pitch isn't a multiple of 4, do non-unrolled version. */ | ||
275 | for (;i<max_pitch;i++) | ||
276 | { | ||
277 | opus_val32 sum = 0; | ||
278 | for (j=0;j<len;j++) | ||
279 | sum = MAC16_16(sum, _x[j],_y[i+j]); | ||
280 | xcorr[i] = sum; | ||
281 | #ifdef FIXED_POINT | ||
282 | maxcorr = MAX32(maxcorr, sum); | ||
283 | #endif | ||
284 | } | ||
285 | #ifdef FIXED_POINT | ||
286 | return maxcorr; | ||
287 | #endif | ||
288 | } | ||
289 | |||
290 | #endif | ||
172 | void pitch_search(const opus_val16 * OPUS_RESTRICT x_lp, opus_val16 * OPUS_RESTRICT y, | 291 | void pitch_search(const opus_val16 * OPUS_RESTRICT x_lp, opus_val16 * OPUS_RESTRICT y, |
173 | int len, int max_pitch, int *pitch) | 292 | int len, int max_pitch, int *pitch) |
174 | { | 293 | { |
@@ -179,8 +298,8 @@ void pitch_search(const opus_val16 * OPUS_RESTRICT x_lp, opus_val16 * OPUS_RESTR | |||
179 | VARDECL(opus_val16, y_lp4); | 298 | VARDECL(opus_val16, y_lp4); |
180 | VARDECL(opus_val32, xcorr); | 299 | VARDECL(opus_val32, xcorr); |
181 | #ifdef FIXED_POINT | 300 | #ifdef FIXED_POINT |
182 | opus_val32 maxcorr=1; | 301 | opus_val32 maxcorr; |
183 | opus_val16 xmax, ymax; | 302 | opus_val32 xmax, ymax; |
184 | int shift=0; | 303 | int shift=0; |
185 | #endif | 304 | #endif |
186 | int offset; | 305 | int offset; |
@@ -204,7 +323,7 @@ void pitch_search(const opus_val16 * OPUS_RESTRICT x_lp, opus_val16 * OPUS_RESTR | |||
204 | #ifdef FIXED_POINT | 323 | #ifdef FIXED_POINT |
205 | xmax = celt_maxabs16(x_lp4, len>>2); | 324 | xmax = celt_maxabs16(x_lp4, len>>2); |
206 | ymax = celt_maxabs16(y_lp4, lag>>2); | 325 | ymax = celt_maxabs16(y_lp4, lag>>2); |
207 | shift = celt_ilog2(MAX16(1, MAX16(xmax, ymax)))-11; | 326 | shift = celt_ilog2(MAX32(1, MAX32(xmax, ymax)))-11; |
208 | if (shift>0) | 327 | if (shift>0) |
209 | { | 328 | { |
210 | for (j=0;j<len>>2;j++) | 329 | for (j=0;j<len>>2;j++) |
@@ -220,16 +339,11 @@ void pitch_search(const opus_val16 * OPUS_RESTRICT x_lp, opus_val16 * OPUS_RESTR | |||
220 | 339 | ||
221 | /* Coarse search with 4x decimation */ | 340 | /* Coarse search with 4x decimation */ |
222 | 341 | ||
223 | for (i=0;i<max_pitch>>2;i++) | ||
224 | { | ||
225 | opus_val32 sum = 0; | ||
226 | for (j=0;j<len>>2;j++) | ||
227 | sum = MAC16_16(sum, x_lp4[j],y_lp4[i+j]); | ||
228 | xcorr[i] = MAX32(-1, sum); | ||
229 | #ifdef FIXED_POINT | 342 | #ifdef FIXED_POINT |
230 | maxcorr = MAX32(maxcorr, sum); | 343 | maxcorr = |
231 | #endif | 344 | #endif |
232 | } | 345 | celt_pitch_xcorr(x_lp4, y_lp4, xcorr, len>>2, max_pitch>>2); |
346 | |||
233 | find_best_pitch(xcorr, y_lp4, len>>2, max_pitch>>2, best_pitch | 347 | find_best_pitch(xcorr, y_lp4, len>>2, max_pitch>>2, best_pitch |
234 | #ifdef FIXED_POINT | 348 | #ifdef FIXED_POINT |
235 | , 0, maxcorr | 349 | , 0, maxcorr |
@@ -288,11 +402,13 @@ opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod, | |||
288 | int k, i, T, T0; | 402 | int k, i, T, T0; |
289 | opus_val16 g, g0; | 403 | opus_val16 g, g0; |
290 | opus_val16 pg; | 404 | opus_val16 pg; |
291 | opus_val32 xy,xx,yy; | 405 | opus_val32 xy,xx,yy,xy2; |
292 | opus_val32 xcorr[3]; | 406 | opus_val32 xcorr[3]; |
293 | opus_val32 best_xy, best_yy; | 407 | opus_val32 best_xy, best_yy; |
294 | int offset; | 408 | int offset; |
295 | int minperiod0; | 409 | int minperiod0; |
410 | VARDECL(opus_val32, yy_lookup); | ||
411 | SAVE_STACK; | ||
296 | 412 | ||
297 | minperiod0 = minperiod; | 413 | minperiod0 = minperiod; |
298 | maxperiod /= 2; | 414 | maxperiod /= 2; |
@@ -305,13 +421,16 @@ opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod, | |||
305 | *T0_=maxperiod-1; | 421 | *T0_=maxperiod-1; |
306 | 422 | ||
307 | T = T0 = *T0_; | 423 | T = T0 = *T0_; |
308 | xx=xy=yy=0; | 424 | ALLOC(yy_lookup, maxperiod+1, opus_val32); |
309 | for (i=0;i<N;i++) | 425 | dual_inner_prod(x, x, x-T0, N, &xx, &xy); |
426 | yy_lookup[0] = xx; | ||
427 | yy=xx; | ||
428 | for (i=1;i<=maxperiod;i++) | ||
310 | { | 429 | { |
311 | xy = MAC16_16(xy, x[i], x[i-T0]); | 430 | yy = yy+MULT16_16(x[-i],x[-i])-MULT16_16(x[N-i],x[N-i]); |
312 | xx = MAC16_16(xx, x[i], x[i]); | 431 | yy_lookup[i] = MAX32(0, yy); |
313 | yy = MAC16_16(yy, x[i-T0],x[i-T0]); | ||
314 | } | 432 | } |
433 | yy = yy_lookup[T0]; | ||
315 | best_xy = xy; | 434 | best_xy = xy; |
316 | best_yy = yy; | 435 | best_yy = yy; |
317 | #ifdef FIXED_POINT | 436 | #ifdef FIXED_POINT |
@@ -332,6 +451,7 @@ opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod, | |||
332 | int T1, T1b; | 451 | int T1, T1b; |
333 | opus_val16 g1; | 452 | opus_val16 g1; |
334 | opus_val16 cont=0; | 453 | opus_val16 cont=0; |
454 | opus_val16 thresh; | ||
335 | T1 = (2*T0+k)/(2*k); | 455 | T1 = (2*T0+k)/(2*k); |
336 | if (T1 < minperiod) | 456 | if (T1 < minperiod) |
337 | break; | 457 | break; |
@@ -346,15 +466,9 @@ opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod, | |||
346 | { | 466 | { |
347 | T1b = (2*second_check[k]*T0+k)/(2*k); | 467 | T1b = (2*second_check[k]*T0+k)/(2*k); |
348 | } | 468 | } |
349 | xy=yy=0; | 469 | dual_inner_prod(x, &x[-T1], &x[-T1b], N, &xy, &xy2); |
350 | for (i=0;i<N;i++) | 470 | xy += xy2; |
351 | { | 471 | yy = yy_lookup[T1] + yy_lookup[T1b]; |
352 | xy = MAC16_16(xy, x[i], x[i-T1]); | ||
353 | yy = MAC16_16(yy, x[i-T1], x[i-T1]); | ||
354 | |||
355 | xy = MAC16_16(xy, x[i], x[i-T1b]); | ||
356 | yy = MAC16_16(yy, x[i-T1b], x[i-T1b]); | ||
357 | } | ||
358 | #ifdef FIXED_POINT | 472 | #ifdef FIXED_POINT |
359 | { | 473 | { |
360 | opus_val32 x2y2; | 474 | opus_val32 x2y2; |
@@ -373,7 +487,14 @@ opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod, | |||
373 | cont = HALF32(prev_gain); | 487 | cont = HALF32(prev_gain); |
374 | else | 488 | else |
375 | cont = 0; | 489 | cont = 0; |
376 | if (g1 > QCONST16(.3f,15) + MULT16_16_Q15(QCONST16(.4f,15),g0)-cont) | 490 | thresh = MAX16(QCONST16(.3f,15), MULT16_16_Q15(QCONST16(.7f,15),g0)-cont); |
491 | /* Bias against very high pitch (very short period) to avoid false-positives | ||
492 | due to short-term correlation */ | ||
493 | if (T1<3*minperiod) | ||
494 | thresh = MAX16(QCONST16(.4f,15), MULT16_16_Q15(QCONST16(.85f,15),g0)-cont); | ||
495 | else if (T1<2*minperiod) | ||
496 | thresh = MAX16(QCONST16(.5f,15), MULT16_16_Q15(QCONST16(.9f,15),g0)-cont); | ||
497 | if (g1 > thresh) | ||
377 | { | 498 | { |
378 | best_xy = xy; | 499 | best_xy = xy; |
379 | best_yy = yy; | 500 | best_yy = yy; |
@@ -407,6 +528,7 @@ opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod, | |||
407 | 528 | ||
408 | if (*T0_<minperiod0) | 529 | if (*T0_<minperiod0) |
409 | *T0_=minperiod0; | 530 | *T0_=minperiod0; |
531 | RESTORE_STACK; | ||
410 | return pg; | 532 | return pg; |
411 | } | 533 | } |
412 | #endif | 534 | #endif |
diff --git a/lib/rbcodec/codecs/libopus/celt/pitch.h b/lib/rbcodec/codecs/libopus/celt/pitch.h index 2757071a6f..caffd24bc4 100644 --- a/lib/rbcodec/codecs/libopus/celt/pitch.h +++ b/lib/rbcodec/codecs/libopus/celt/pitch.h | |||
@@ -36,6 +36,10 @@ | |||
36 | 36 | ||
37 | #include "modes.h" | 37 | #include "modes.h" |
38 | 38 | ||
39 | #if defined(__SSE__) && !defined(FIXED_POINT) | ||
40 | #include "x86/pitch_sse.h" | ||
41 | #endif | ||
42 | |||
39 | void pitch_downsample(celt_sig * OPUS_RESTRICT x[], opus_val16 * OPUS_RESTRICT x_lp, | 43 | void pitch_downsample(celt_sig * OPUS_RESTRICT x[], opus_val16 * OPUS_RESTRICT x_lp, |
40 | int len, int C); | 44 | int len, int C); |
41 | 45 | ||
@@ -45,4 +49,97 @@ void pitch_search(const opus_val16 * OPUS_RESTRICT x_lp, opus_val16 * OPUS_RESTR | |||
45 | opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod, | 49 | opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod, |
46 | int N, int *T0, int prev_period, opus_val16 prev_gain); | 50 | int N, int *T0, int prev_period, opus_val16 prev_gain); |
47 | 51 | ||
52 | /* OPT: This is the kernel you really want to optimize. It gets used a lot | ||
53 | by the prefilter and by the PLC. */ | ||
54 | #ifndef OVERRIDE_XCORR_KERNEL | ||
55 | static inline void xcorr_kernel(const opus_val16 * x, const opus_val16 * y, opus_val32 sum[4], int len) | ||
56 | { | ||
57 | int j; | ||
58 | opus_val16 y_0, y_1, y_2, y_3; | ||
59 | y_3=0; /* gcc doesn't realize that y_3 can't be used uninitialized */ | ||
60 | y_0=*y++; | ||
61 | y_1=*y++; | ||
62 | y_2=*y++; | ||
63 | for (j=0;j<len-3;j+=4) | ||
64 | { | ||
65 | opus_val16 tmp; | ||
66 | tmp = *x++; | ||
67 | y_3=*y++; | ||
68 | sum[0] = MAC16_16(sum[0],tmp,y_0); | ||
69 | sum[1] = MAC16_16(sum[1],tmp,y_1); | ||
70 | sum[2] = MAC16_16(sum[2],tmp,y_2); | ||
71 | sum[3] = MAC16_16(sum[3],tmp,y_3); | ||
72 | tmp=*x++; | ||
73 | y_0=*y++; | ||
74 | sum[0] = MAC16_16(sum[0],tmp,y_1); | ||
75 | sum[1] = MAC16_16(sum[1],tmp,y_2); | ||
76 | sum[2] = MAC16_16(sum[2],tmp,y_3); | ||
77 | sum[3] = MAC16_16(sum[3],tmp,y_0); | ||
78 | tmp=*x++; | ||
79 | y_1=*y++; | ||
80 | sum[0] = MAC16_16(sum[0],tmp,y_2); | ||
81 | sum[1] = MAC16_16(sum[1],tmp,y_3); | ||
82 | sum[2] = MAC16_16(sum[2],tmp,y_0); | ||
83 | sum[3] = MAC16_16(sum[3],tmp,y_1); | ||
84 | tmp=*x++; | ||
85 | y_2=*y++; | ||
86 | sum[0] = MAC16_16(sum[0],tmp,y_3); | ||
87 | sum[1] = MAC16_16(sum[1],tmp,y_0); | ||
88 | sum[2] = MAC16_16(sum[2],tmp,y_1); | ||
89 | sum[3] = MAC16_16(sum[3],tmp,y_2); | ||
90 | } | ||
91 | if (j++<len) | ||
92 | { | ||
93 | opus_val16 tmp = *x++; | ||
94 | y_3=*y++; | ||
95 | sum[0] = MAC16_16(sum[0],tmp,y_0); | ||
96 | sum[1] = MAC16_16(sum[1],tmp,y_1); | ||
97 | sum[2] = MAC16_16(sum[2],tmp,y_2); | ||
98 | sum[3] = MAC16_16(sum[3],tmp,y_3); | ||
99 | } | ||
100 | if (j++<len) | ||
101 | { | ||
102 | opus_val16 tmp=*x++; | ||
103 | y_0=*y++; | ||
104 | sum[0] = MAC16_16(sum[0],tmp,y_1); | ||
105 | sum[1] = MAC16_16(sum[1],tmp,y_2); | ||
106 | sum[2] = MAC16_16(sum[2],tmp,y_3); | ||
107 | sum[3] = MAC16_16(sum[3],tmp,y_0); | ||
108 | } | ||
109 | if (j<len) | ||
110 | { | ||
111 | opus_val16 tmp=*x++; | ||
112 | y_1=*y++; | ||
113 | sum[0] = MAC16_16(sum[0],tmp,y_2); | ||
114 | sum[1] = MAC16_16(sum[1],tmp,y_3); | ||
115 | sum[2] = MAC16_16(sum[2],tmp,y_0); | ||
116 | sum[3] = MAC16_16(sum[3],tmp,y_1); | ||
117 | } | ||
118 | } | ||
119 | #endif /* OVERRIDE_XCORR_KERNEL */ | ||
120 | |||
121 | #ifndef OVERRIDE_DUAL_INNER_PROD | ||
122 | static inline void dual_inner_prod(const opus_val16 *x, const opus_val16 *y01, const opus_val16 *y02, | ||
123 | int N, opus_val32 *xy1, opus_val32 *xy2) | ||
124 | { | ||
125 | int i; | ||
126 | opus_val32 xy01=0; | ||
127 | opus_val32 xy02=0; | ||
128 | for (i=0;i<N;i++) | ||
129 | { | ||
130 | xy01 = MAC16_16(xy01, x[i], y01[i]); | ||
131 | xy02 = MAC16_16(xy02, x[i], y02[i]); | ||
132 | } | ||
133 | *xy1 = xy01; | ||
134 | *xy2 = xy02; | ||
135 | } | ||
136 | #endif | ||
137 | |||
138 | #ifdef FIXED_POINT | ||
139 | opus_val32 | ||
140 | #else | ||
141 | void | ||
142 | #endif | ||
143 | celt_pitch_xcorr(const opus_val16 *_x, const opus_val16 *_y, opus_val32 *xcorr, int len, int max_pitch); | ||
144 | |||
48 | #endif | 145 | #endif |
diff --git a/lib/rbcodec/codecs/libopus/celt/quant_bands.c b/lib/rbcodec/codecs/libopus/celt/quant_bands.c index 5ad5311f84..79685e17cb 100644 --- a/lib/rbcodec/codecs/libopus/celt/quant_bands.c +++ b/lib/rbcodec/codecs/libopus/celt/quant_bands.c | |||
@@ -27,7 +27,7 @@ | |||
27 | */ | 27 | */ |
28 | 28 | ||
29 | #ifdef HAVE_CONFIG_H | 29 | #ifdef HAVE_CONFIG_H |
30 | #include "opus_config.h" | 30 | #include "config.h" |
31 | #endif | 31 | #endif |
32 | 32 | ||
33 | #include "quant_bands.h" | 33 | #include "quant_bands.h" |
@@ -40,8 +40,8 @@ | |||
40 | #include "rate.h" | 40 | #include "rate.h" |
41 | 41 | ||
42 | #ifdef FIXED_POINT | 42 | #ifdef FIXED_POINT |
43 | /* Mean energy in each band quantized in Q6 */ | 43 | /* Mean energy in each band quantized in Q4 */ |
44 | static const signed char eMeans[25] = { | 44 | const signed char eMeans[25] = { |
45 | 103,100, 92, 85, 81, | 45 | 103,100, 92, 85, 81, |
46 | 77, 72, 70, 78, 75, | 46 | 77, 72, 70, 78, 75, |
47 | 73, 71, 78, 74, 69, | 47 | 73, 71, 78, 74, 69, |
@@ -49,8 +49,8 @@ static const signed char eMeans[25] = { | |||
49 | 60, 60, 60, 60, 60 | 49 | 60, 60, 60, 60, 60 |
50 | }; | 50 | }; |
51 | #else | 51 | #else |
52 | /* Mean energy in each band quantized in Q6 and converted back to float */ | 52 | /* Mean energy in each band quantized in Q4 and converted back to float */ |
53 | static const opus_val16 eMeans[25] = { | 53 | const opus_val16 eMeans[25] = { |
54 | 6.437500f, 6.250000f, 5.750000f, 5.312500f, 5.062500f, | 54 | 6.437500f, 6.250000f, 5.750000f, 5.312500f, 5.062500f, |
55 | 4.812500f, 4.500000f, 4.375000f, 4.875000f, 4.687500f, | 55 | 4.812500f, 4.500000f, 4.375000f, 4.875000f, 4.687500f, |
56 | 4.562500f, 4.437500f, 4.875000f, 4.625000f, 4.312500f, | 56 | 4.562500f, 4.437500f, 4.875000f, 4.625000f, 4.312500f, |
@@ -157,7 +157,7 @@ static int quant_coarse_energy_impl(const CELTMode *m, int start, int end, | |||
157 | const opus_val16 *eBands, opus_val16 *oldEBands, | 157 | const opus_val16 *eBands, opus_val16 *oldEBands, |
158 | opus_int32 budget, opus_int32 tell, | 158 | opus_int32 budget, opus_int32 tell, |
159 | const unsigned char *prob_model, opus_val16 *error, ec_enc *enc, | 159 | const unsigned char *prob_model, opus_val16 *error, ec_enc *enc, |
160 | int C, int LM, int intra, opus_val16 max_decay) | 160 | int C, int LM, int intra, opus_val16 max_decay, int lfe) |
161 | { | 161 | { |
162 | int i, c; | 162 | int i, c; |
163 | int badness = 0; | 163 | int badness = 0; |
@@ -222,6 +222,8 @@ static int quant_coarse_energy_impl(const CELTMode *m, int start, int end, | |||
222 | if (bits_left < 16) | 222 | if (bits_left < 16) |
223 | qi = IMAX(-1, qi); | 223 | qi = IMAX(-1, qi); |
224 | } | 224 | } |
225 | if (lfe && i>=2) | ||
226 | qi = IMIN(qi, 0); | ||
225 | if (budget-tell >= 15) | 227 | if (budget-tell >= 15) |
226 | { | 228 | { |
227 | int pi; | 229 | int pi; |
@@ -253,13 +255,13 @@ static int quant_coarse_energy_impl(const CELTMode *m, int start, int end, | |||
253 | prev[c] = prev[c] + SHL32(q,7) - MULT16_16(beta,PSHR32(q,8)); | 255 | prev[c] = prev[c] + SHL32(q,7) - MULT16_16(beta,PSHR32(q,8)); |
254 | } while (++c < C); | 256 | } while (++c < C); |
255 | } | 257 | } |
256 | return badness; | 258 | return lfe ? 0 : badness; |
257 | } | 259 | } |
258 | 260 | ||
259 | void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd, | 261 | void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd, |
260 | const opus_val16 *eBands, opus_val16 *oldEBands, opus_uint32 budget, | 262 | const opus_val16 *eBands, opus_val16 *oldEBands, opus_uint32 budget, |
261 | opus_val16 *error, ec_enc *enc, int C, int LM, int nbAvailableBytes, | 263 | opus_val16 *error, ec_enc *enc, int C, int LM, int nbAvailableBytes, |
262 | int force_intra, opus_val32 *delayedIntra, int two_pass, int loss_rate) | 264 | int force_intra, opus_val32 *delayedIntra, int two_pass, int loss_rate, int lfe) |
263 | { | 265 | { |
264 | int intra; | 266 | int intra; |
265 | opus_val16 max_decay; | 267 | opus_val16 max_decay; |
@@ -280,15 +282,17 @@ void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd, | |||
280 | if (tell+3 > budget) | 282 | if (tell+3 > budget) |
281 | two_pass = intra = 0; | 283 | two_pass = intra = 0; |
282 | 284 | ||
283 | /* Encode the global flags using a simple probability model | 285 | max_decay = QCONST16(16.f,DB_SHIFT); |
284 | (first symbols in the stream) */ | 286 | if (end-start>10) |
285 | 287 | { | |
286 | #ifdef FIXED_POINT | 288 | #ifdef FIXED_POINT |
287 | max_decay = MIN32(QCONST16(16.f,DB_SHIFT), SHL32(EXTEND32(nbAvailableBytes),DB_SHIFT-3)); | 289 | max_decay = MIN32(max_decay, SHL32(EXTEND32(nbAvailableBytes),DB_SHIFT-3)); |
288 | #else | 290 | #else |
289 | max_decay = MIN32(16.f, .125f*nbAvailableBytes); | 291 | max_decay = MIN32(max_decay, .125f*nbAvailableBytes); |
290 | #endif | 292 | #endif |
291 | 293 | } | |
294 | if (lfe) | ||
295 | max_decay=3; | ||
292 | enc_start_state = *enc; | 296 | enc_start_state = *enc; |
293 | 297 | ||
294 | ALLOC(oldEBands_intra, C*m->nbEBands, opus_val16); | 298 | ALLOC(oldEBands_intra, C*m->nbEBands, opus_val16); |
@@ -298,7 +302,7 @@ void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd, | |||
298 | if (two_pass || intra) | 302 | if (two_pass || intra) |
299 | { | 303 | { |
300 | badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget, | 304 | badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget, |
301 | tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay); | 305 | tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay, lfe); |
302 | } | 306 | } |
303 | 307 | ||
304 | if (!intra) | 308 | if (!intra) |
@@ -325,7 +329,7 @@ void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd, | |||
325 | *enc = enc_start_state; | 329 | *enc = enc_start_state; |
326 | 330 | ||
327 | badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget, | 331 | badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget, |
328 | tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay); | 332 | tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay, lfe); |
329 | 333 | ||
330 | if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra))) | 334 | if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra))) |
331 | { | 335 | { |
@@ -532,25 +536,6 @@ void unquant_energy_finalise(const CELTMode *m, int start, int end, opus_val16 * | |||
532 | } | 536 | } |
533 | } | 537 | } |
534 | 538 | ||
535 | void log2Amp(const CELTMode *m, int start, int end, | ||
536 | celt_ener *eBands, const opus_val16 *oldEBands, int C) | ||
537 | { | ||
538 | int c, i; | ||
539 | c=0; | ||
540 | do { | ||
541 | for (i=0;i<start;i++) | ||
542 | eBands[i+c*m->nbEBands] = 0; | ||
543 | for (;i<end;i++) | ||
544 | { | ||
545 | opus_val16 lg = ADD16(oldEBands[i+c*m->nbEBands], | ||
546 | SHL16((opus_val16)eMeans[i],6)); | ||
547 | eBands[i+c*m->nbEBands] = PSHR32(celt_exp2(lg),4); | ||
548 | } | ||
549 | for (;i<m->nbEBands;i++) | ||
550 | eBands[i+c*m->nbEBands] = 0; | ||
551 | } while (++c < C); | ||
552 | } | ||
553 | |||
554 | void amp2Log2(const CELTMode *m, int effEnd, int end, | 539 | void amp2Log2(const CELTMode *m, int effEnd, int end, |
555 | celt_ener *bandE, opus_val16 *bandLogE, int C) | 540 | celt_ener *bandE, opus_val16 *bandLogE, int C) |
556 | { | 541 | { |
diff --git a/lib/rbcodec/codecs/libopus/celt/quant_bands.h b/lib/rbcodec/codecs/libopus/celt/quant_bands.h index bec2855cf0..0490bca4b4 100644 --- a/lib/rbcodec/codecs/libopus/celt/quant_bands.h +++ b/lib/rbcodec/codecs/libopus/celt/quant_bands.h | |||
@@ -35,6 +35,12 @@ | |||
35 | #include "entdec.h" | 35 | #include "entdec.h" |
36 | #include "mathops.h" | 36 | #include "mathops.h" |
37 | 37 | ||
38 | #ifdef FIXED_POINT | ||
39 | extern const signed char eMeans[25]; | ||
40 | #else | ||
41 | extern const opus_val16 eMeans[25]; | ||
42 | #endif | ||
43 | |||
38 | void amp2Log2(const CELTMode *m, int effEnd, int end, | 44 | void amp2Log2(const CELTMode *m, int effEnd, int end, |
39 | celt_ener *bandE, opus_val16 *bandLogE, int C); | 45 | celt_ener *bandE, opus_val16 *bandLogE, int C); |
40 | 46 | ||
@@ -45,7 +51,7 @@ void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd, | |||
45 | const opus_val16 *eBands, opus_val16 *oldEBands, opus_uint32 budget, | 51 | const opus_val16 *eBands, opus_val16 *oldEBands, opus_uint32 budget, |
46 | opus_val16 *error, ec_enc *enc, int C, int LM, | 52 | opus_val16 *error, ec_enc *enc, int C, int LM, |
47 | int nbAvailableBytes, int force_intra, opus_val32 *delayedIntra, | 53 | int nbAvailableBytes, int force_intra, opus_val32 *delayedIntra, |
48 | int two_pass, int loss_rate); | 54 | int two_pass, int loss_rate, int lfe); |
49 | 55 | ||
50 | void quant_fine_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, opus_val16 *error, int *fine_quant, ec_enc *enc, int C); | 56 | void quant_fine_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, opus_val16 *error, int *fine_quant, ec_enc *enc, int C); |
51 | 57 | ||
diff --git a/lib/rbcodec/codecs/libopus/celt/rate.c b/lib/rbcodec/codecs/libopus/celt/rate.c index 3b056d8dc7..e474cf5004 100644 --- a/lib/rbcodec/codecs/libopus/celt/rate.c +++ b/lib/rbcodec/codecs/libopus/celt/rate.c | |||
@@ -27,7 +27,7 @@ | |||
27 | */ | 27 | */ |
28 | 28 | ||
29 | #ifdef HAVE_CONFIG_H | 29 | #ifdef HAVE_CONFIG_H |
30 | #include "opus_config.h" | 30 | #include "config.h" |
31 | #endif | 31 | #endif |
32 | 32 | ||
33 | #include <math.h> | 33 | #include <math.h> |
@@ -248,7 +248,7 @@ void compute_pulse_cache(CELTMode *m, int LM) | |||
248 | static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int skip_start, | 248 | static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int skip_start, |
249 | const int *bits1, const int *bits2, const int *thresh, const int *cap, opus_int32 total, opus_int32 *_balance, | 249 | const int *bits1, const int *bits2, const int *thresh, const int *cap, opus_int32 total, opus_int32 *_balance, |
250 | int skip_rsv, int *intensity, int intensity_rsv, int *dual_stereo, int dual_stereo_rsv, int *bits, | 250 | int skip_rsv, int *intensity, int intensity_rsv, int *dual_stereo, int dual_stereo_rsv, int *bits, |
251 | int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev) | 251 | int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev, int signalBandwidth) |
252 | { | 252 | { |
253 | opus_int32 psum; | 253 | opus_int32 psum; |
254 | int lo, hi; | 254 | int lo, hi; |
@@ -353,7 +353,7 @@ static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int | |||
353 | #ifdef FUZZING | 353 | #ifdef FUZZING |
354 | if ((rand()&0x1) == 0) | 354 | if ((rand()&0x1) == 0) |
355 | #else | 355 | #else |
356 | if (band_bits > ((j<prev?7:9)*band_width<<LM<<BITRES)>>4) | 356 | if (codedBands<=start+2 || (band_bits > ((j<prev?7:9)*band_width<<LM<<BITRES)>>4 && j<=signalBandwidth)) |
357 | #endif | 357 | #endif |
358 | { | 358 | { |
359 | ec_enc_bit_logp(ec, 1, 1); | 359 | ec_enc_bit_logp(ec, 1, 1); |
@@ -524,7 +524,7 @@ static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int | |||
524 | } | 524 | } |
525 | 525 | ||
526 | int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, const int *cap, int alloc_trim, int *intensity, int *dual_stereo, | 526 | int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, const int *cap, int alloc_trim, int *intensity, int *dual_stereo, |
527 | opus_int32 total, opus_int32 *balance, int *pulses, int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev) | 527 | opus_int32 total, opus_int32 *balance, int *pulses, int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev, int signalBandwidth) |
528 | { | 528 | { |
529 | int lo, hi, len, j; | 529 | int lo, hi, len, j; |
530 | int codedBands; | 530 | int codedBands; |
@@ -631,7 +631,7 @@ int compute_allocation(const CELTMode *m, int start, int end, const int *offsets | |||
631 | } | 631 | } |
632 | codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thresh, cap, | 632 | codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thresh, cap, |
633 | total, balance, skip_rsv, intensity, intensity_rsv, dual_stereo, dual_stereo_rsv, | 633 | total, balance, skip_rsv, intensity, intensity_rsv, dual_stereo, dual_stereo_rsv, |
634 | pulses, ebits, fine_priority, C, LM, ec, encode, prev); | 634 | pulses, ebits, fine_priority, C, LM, ec, encode, prev, signalBandwidth); |
635 | RESTORE_STACK; | 635 | RESTORE_STACK; |
636 | return codedBands; | 636 | return codedBands; |
637 | } | 637 | } |
diff --git a/lib/rbcodec/codecs/libopus/celt/rate.h b/lib/rbcodec/codecs/libopus/celt/rate.h index e0d5022326..263fde9820 100644 --- a/lib/rbcodec/codecs/libopus/celt/rate.h +++ b/lib/rbcodec/codecs/libopus/celt/rate.h | |||
@@ -96,6 +96,6 @@ static inline int pulses2bits(const CELTMode *m, int band, int LM, int pulses) | |||
96 | @return Total number of bits allocated | 96 | @return Total number of bits allocated |
97 | */ | 97 | */ |
98 | int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, const int *cap, int alloc_trim, int *intensity, int *dual_stero, | 98 | int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, const int *cap, int alloc_trim, int *intensity, int *dual_stero, |
99 | opus_int32 total, opus_int32 *balance, int *pulses, int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev); | 99 | opus_int32 total, opus_int32 *balance, int *pulses, int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev, int signalBandwidth); |
100 | 100 | ||
101 | #endif | 101 | #endif |
diff --git a/lib/rbcodec/codecs/libopus/celt/stack_alloc.h b/lib/rbcodec/codecs/libopus/celt/stack_alloc.h index a6f06d2263..1c093a8cdc 100644 --- a/lib/rbcodec/codecs/libopus/celt/stack_alloc.h +++ b/lib/rbcodec/codecs/libopus/celt/stack_alloc.h | |||
@@ -146,4 +146,26 @@ extern char *global_stack_top; | |||
146 | 146 | ||
147 | #endif /* VAR_ARRAYS */ | 147 | #endif /* VAR_ARRAYS */ |
148 | 148 | ||
149 | |||
150 | #ifdef ENABLE_VALGRIND | ||
151 | |||
152 | #include <valgrind/memcheck.h> | ||
153 | #define OPUS_CHECK_ARRAY(ptr, len) VALGRIND_CHECK_MEM_IS_DEFINED(ptr, len*sizeof(*ptr)) | ||
154 | #define OPUS_CHECK_VALUE(value) VALGRIND_CHECK_VALUE_IS_DEFINED(value) | ||
155 | #define OPUS_CHECK_ARRAY_COND(ptr, len) VALGRIND_CHECK_MEM_IS_DEFINED(ptr, len*sizeof(*ptr)) | ||
156 | #define OPUS_CHECK_VALUE_COND(value) VALGRIND_CHECK_VALUE_IS_DEFINED(value) | ||
157 | #define OPUS_PRINT_INT(value) do {fprintf(stderr, #value " = %d at %s:%d\n", value, __FILE__, __LINE__);}while(0) | ||
158 | #define OPUS_FPRINTF fprintf | ||
159 | |||
160 | #else | ||
161 | |||
162 | static inline int _opus_false(void) {return 0;} | ||
163 | #define OPUS_CHECK_ARRAY(ptr, len) _opus_false() | ||
164 | #define OPUS_CHECK_VALUE(value) _opus_false() | ||
165 | #define OPUS_PRINT_INT(value) do{}while(0) | ||
166 | #define OPUS_FPRINTF (void) | ||
167 | |||
168 | #endif | ||
169 | |||
170 | |||
149 | #endif /* STACK_ALLOC_H */ | 171 | #endif /* STACK_ALLOC_H */ |
diff --git a/lib/rbcodec/codecs/libopus/celt/vq.c b/lib/rbcodec/codecs/libopus/celt/vq.c index f6b6e4fc64..af991bb052 100644 --- a/lib/rbcodec/codecs/libopus/celt/vq.c +++ b/lib/rbcodec/codecs/libopus/celt/vq.c | |||
@@ -27,7 +27,7 @@ | |||
27 | */ | 27 | */ |
28 | 28 | ||
29 | #ifdef HAVE_CONFIG_H | 29 | #ifdef HAVE_CONFIG_H |
30 | #include "opus_config.h" | 30 | #include "config.h" |
31 | #endif | 31 | #endif |
32 | 32 | ||
33 | #include "mathops.h" | 33 | #include "mathops.h" |
diff --git a/lib/rbcodec/codecs/libopus/celt/vq.h b/lib/rbcodec/codecs/libopus/celt/vq.h index 1ceeeeb268..ffdc69cdc4 100644 --- a/lib/rbcodec/codecs/libopus/celt/vq.h +++ b/lib/rbcodec/codecs/libopus/celt/vq.h | |||
@@ -40,11 +40,9 @@ | |||
40 | /** Algebraic pulse-vector quantiser. The signal x is replaced by the sum of | 40 | /** Algebraic pulse-vector quantiser. The signal x is replaced by the sum of |
41 | * the pitch and a combination of pulses such that its norm is still equal | 41 | * the pitch and a combination of pulses such that its norm is still equal |
42 | * to 1. This is the function that will typically require the most CPU. | 42 | * to 1. This is the function that will typically require the most CPU. |
43 | * @param x Residual signal to quantise/encode (returns quantised version) | 43 | * @param X Residual signal to quantise/encode (returns quantised version) |
44 | * @param W Perceptual weight to use when optimising (currently unused) | ||
45 | * @param N Number of samples to encode | 44 | * @param N Number of samples to encode |
46 | * @param K Number of pulses to use | 45 | * @param K Number of pulses to use |
47 | * @param p Pitch vector (it is assumed that p+x is a unit vector) | ||
48 | * @param enc Entropy encoder state | 46 | * @param enc Entropy encoder state |
49 | * @ret A mask indicating which blocks in the band received pulses | 47 | * @ret A mask indicating which blocks in the band received pulses |
50 | */ | 48 | */ |
@@ -56,10 +54,9 @@ unsigned alg_quant(celt_norm *X, int N, int K, int spread, int B, | |||
56 | ); | 54 | ); |
57 | 55 | ||
58 | /** Algebraic pulse decoder | 56 | /** Algebraic pulse decoder |
59 | * @param x Decoded normalised spectrum (returned) | 57 | * @param X Decoded normalised spectrum (returned) |
60 | * @param N Number of samples to decode | 58 | * @param N Number of samples to decode |
61 | * @param K Number of pulses to use | 59 | * @param K Number of pulses to use |
62 | * @param p Pitch vector (automatically added to x) | ||
63 | * @param dec Entropy decoder state | 60 | * @param dec Entropy decoder state |
64 | * @ret A mask indicating which blocks in the band received pulses | 61 | * @ret A mask indicating which blocks in the band received pulses |
65 | */ | 62 | */ |