Jean-Marc Valin | 6c3788c | 2010-06-20 22:48:50 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2009-2010 Xiph.Org Foundation |
| 2 | Written by Jean-Marc Valin */ |
| 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: |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 7 | |
Jean-Marc Valin | 6c3788c | 2010-06-20 22:48:50 -0400 | [diff] [blame] | 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 | |
Jean-Marc Valin | 6c3788c | 2010-06-20 22:48:50 -0400 | [diff] [blame] | 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 |
Jean-Marc Valin | cb05e7c | 2012-04-20 16:40:24 -0400 | [diff] [blame] | 18 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
| 19 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
Jean-Marc Valin | 6c3788c | 2010-06-20 22:48:50 -0400 | [diff] [blame] | 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 | #ifdef HAVE_CONFIG_H |
| 29 | #include "config.h" |
Jean-Marc Valin | d69c1cb | 2009-12-28 00:34:29 -0500 | [diff] [blame] | 30 | #endif |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 31 | |
Jean-Marc Valin | 2779df7 | 2011-10-04 13:26:53 -0400 | [diff] [blame] | 32 | #include "celt_lpc.h" |
Jean-Marc Valin | 6c3788c | 2010-06-20 22:48:50 -0400 | [diff] [blame] | 33 | #include "stack_alloc.h" |
| 34 | #include "mathops.h" |
Jean-Marc Valin | e8e57a3 | 2013-05-25 02:14:25 -0400 | [diff] [blame] | 35 | #include "pitch.h" |
Jean-Marc Valin | 3a9699e | 2010-06-17 00:35:26 -0400 | [diff] [blame] | 36 | |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 37 | void _celt_lpc( |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 38 | opus_val16 *_lpc, /* out: [0...p-1] LPC coefficients */ |
| 39 | const opus_val32 *ac, /* in: [0...p] autocorrelation values */ |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 40 | int p |
| 41 | ) |
| 42 | { |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 43 | int i, j; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 44 | opus_val32 r; |
| 45 | opus_val32 error = ac[0]; |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 46 | #ifdef FIXED_POINT |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 47 | opus_val32 lpc[LPC_ORDER]; |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 48 | #else |
| 49 | float *lpc = _lpc; |
| 50 | #endif |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 51 | |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 52 | for (i = 0; i < p; i++) |
| 53 | lpc[i] = 0; |
| 54 | if (ac[0] != 0) |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 55 | { |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 56 | for (i = 0; i < p; i++) { |
| 57 | /* Sum up this iteration's reflection coefficient */ |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 58 | opus_val32 rr = 0; |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 59 | for (j = 0; j < i; j++) |
| 60 | rr += MULT32_32_Q31(lpc[j],ac[i - j]); |
| 61 | rr += SHR32(ac[i + 1],3); |
Jean-Marc Valin | bd82ca8 | 2010-06-17 00:11:54 -0400 | [diff] [blame] | 62 | r = -frac_div32(SHL32(rr,3), error); |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 63 | /* Update LPC coefficients and total error */ |
| 64 | lpc[i] = SHR32(r,3); |
| 65 | for (j = 0; j < (i+1)>>1; j++) |
| 66 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 67 | opus_val32 tmp1, tmp2; |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 68 | tmp1 = lpc[j]; |
| 69 | tmp2 = lpc[i-1-j]; |
| 70 | lpc[j] = tmp1 + MULT32_32_Q31(r,tmp2); |
| 71 | lpc[i-1-j] = tmp2 + MULT32_32_Q31(r,tmp1); |
| 72 | } |
| 73 | |
| 74 | error = error - MULT32_32_Q31(MULT32_32_Q31(r,r),error); |
Jean-Marc Valin | bd82ca8 | 2010-06-17 00:11:54 -0400 | [diff] [blame] | 75 | /* Bail out once we get 30 dB gain */ |
| 76 | #ifdef FIXED_POINT |
| 77 | if (error<SHR32(ac[0],10)) |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 78 | break; |
Jean-Marc Valin | bd82ca8 | 2010-06-17 00:11:54 -0400 | [diff] [blame] | 79 | #else |
Gregory Maxwell | 60c316b | 2010-11-04 20:14:19 -0400 | [diff] [blame] | 80 | if (error<.001f*ac[0]) |
Jean-Marc Valin | bd82ca8 | 2010-06-17 00:11:54 -0400 | [diff] [blame] | 81 | break; |
| 82 | #endif |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 83 | } |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 84 | } |
Jean-Marc Valin | 303b3b6 | 2009-12-30 22:40:24 -0500 | [diff] [blame] | 85 | #ifdef FIXED_POINT |
| 86 | for (i=0;i<p;i++) |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 87 | _lpc[i] = ROUND16(lpc[i],16); |
Jean-Marc Valin | 303b3b6 | 2009-12-30 22:40:24 -0500 | [diff] [blame] | 88 | #endif |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Jean-Marc Valin | e2374a8 | 2013-05-25 04:25:54 -0400 | [diff] [blame^] | 91 | void celt_fir(const opus_val16 *_x, |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 92 | const opus_val16 *num, |
Jean-Marc Valin | e2374a8 | 2013-05-25 04:25:54 -0400 | [diff] [blame^] | 93 | opus_val16 *_y, |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 94 | int N, |
| 95 | int ord, |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 96 | opus_val16 *mem) |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 97 | { |
| 98 | int i,j; |
Jean-Marc Valin | e2374a8 | 2013-05-25 04:25:54 -0400 | [diff] [blame^] | 99 | VARDECL(opus_val16, rnum); |
| 100 | VARDECL(opus_val16, x); |
| 101 | SAVE_STACK; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 102 | |
Jean-Marc Valin | e2374a8 | 2013-05-25 04:25:54 -0400 | [diff] [blame^] | 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 |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 114 | for (i=0;i<N;i++) |
| 115 | { |
Jean-Marc Valin | e2374a8 | 2013-05-25 04:25:54 -0400 | [diff] [blame^] | 116 | opus_val32 sum = SHL32(EXTEND32(_x[i]), SIG_SHIFT); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 117 | for (j=0;j<ord;j++) |
| 118 | { |
Jean-Marc Valin | e2374a8 | 2013-05-25 04:25:54 -0400 | [diff] [blame^] | 119 | sum = MAC16_16(sum,rnum[j],x[i+j]); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 120 | } |
Jean-Marc Valin | e2374a8 | 2013-05-25 04:25:54 -0400 | [diff] [blame^] | 121 | _y[i] = ROUND16(sum, SIG_SHIFT); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 122 | } |
Jean-Marc Valin | e2374a8 | 2013-05-25 04:25:54 -0400 | [diff] [blame^] | 123 | #else |
| 124 | celt_assert((ord&3)==0); |
| 125 | for (i=0;i<N-3;i+=4) |
| 126 | { |
| 127 | opus_val32 sum1=0; |
| 128 | opus_val32 sum2=0; |
| 129 | opus_val32 sum3=0; |
| 130 | opus_val32 sum4=0; |
| 131 | const opus_val16 *xx = x+i; |
| 132 | const opus_val16 *z = rnum; |
| 133 | opus_val16 y_0, y_1, y_2, y_3; |
| 134 | y_3=0; /* gcc doesn't realize that y_3 can't be used uninitialized */ |
| 135 | y_0=*xx++; |
| 136 | y_1=*xx++; |
| 137 | y_2=*xx++; |
| 138 | for (j=0;j<ord-3;j+=4) |
| 139 | { |
| 140 | opus_val16 tmp; |
| 141 | tmp = *z++; |
| 142 | y_3=*xx++; |
| 143 | sum1 = MAC16_16(sum1,tmp,y_0); |
| 144 | sum2 = MAC16_16(sum2,tmp,y_1); |
| 145 | sum3 = MAC16_16(sum3,tmp,y_2); |
| 146 | sum4 = MAC16_16(sum4,tmp,y_3); |
| 147 | tmp=*z++; |
| 148 | y_0=*xx++; |
| 149 | sum1 = MAC16_16(sum1,tmp,y_1); |
| 150 | sum2 = MAC16_16(sum2,tmp,y_2); |
| 151 | sum3 = MAC16_16(sum3,tmp,y_3); |
| 152 | sum4 = MAC16_16(sum4,tmp,y_0); |
| 153 | tmp=*z++; |
| 154 | y_1=*xx++; |
| 155 | sum1 = MAC16_16(sum1,tmp,y_2); |
| 156 | sum2 = MAC16_16(sum2,tmp,y_3); |
| 157 | sum3 = MAC16_16(sum3,tmp,y_0); |
| 158 | sum4 = MAC16_16(sum4,tmp,y_1); |
| 159 | tmp=*z++; |
| 160 | y_2=*xx++; |
| 161 | sum1 = MAC16_16(sum1,tmp,y_3); |
| 162 | sum2 = MAC16_16(sum2,tmp,y_0); |
| 163 | sum3 = MAC16_16(sum3,tmp,y_1); |
| 164 | sum4 = MAC16_16(sum4,tmp,y_2); |
| 165 | } |
| 166 | _y[i ] = ADD16(_x[i ], ROUND16(sum1, SIG_SHIFT)); |
| 167 | _y[i+1] = ADD16(_x[i+1], ROUND16(sum2, SIG_SHIFT)); |
| 168 | _y[i+2] = ADD16(_x[i+2], ROUND16(sum3, SIG_SHIFT)); |
| 169 | _y[i+3] = ADD16(_x[i+3], ROUND16(sum4, SIG_SHIFT)); |
| 170 | } |
| 171 | for (;i<N;i++) |
| 172 | { |
| 173 | opus_val32 sum = 0; |
| 174 | for (j=0;j<ord;j++) |
| 175 | sum = MAC16_16(sum,rnum[j],x[i+j]); |
| 176 | _y[i] = ADD16(_x[i ], ROUND16(sum, SIG_SHIFT)); |
| 177 | } |
| 178 | #endif |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Jean-Marc Valin | 7fc2fbd | 2011-09-01 13:40:39 -0400 | [diff] [blame] | 181 | void celt_iir(const opus_val32 *x, |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 182 | const opus_val16 *den, |
| 183 | opus_val32 *y, |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 184 | int N, |
| 185 | int ord, |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 186 | opus_val16 *mem) |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 187 | { |
| 188 | int i,j; |
| 189 | for (i=0;i<N;i++) |
| 190 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 191 | opus_val32 sum = x[i]; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 192 | for (j=0;j<ord;j++) |
| 193 | { |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 194 | sum -= MULT16_16(den[j],mem[j]); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 195 | } |
| 196 | for (j=ord-1;j>=1;j--) |
| 197 | { |
| 198 | mem[j]=mem[j-1]; |
| 199 | } |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 200 | mem[0] = ROUND16(sum,SIG_SHIFT); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 201 | y[i] = sum; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void _celt_autocorr( |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 206 | const opus_val16 *x, /* in: [0...n-1] samples x */ |
| 207 | opus_val32 *ac, /* out: [0...lag-1] ac values */ |
| 208 | const opus_val16 *window, |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 209 | int overlap, |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 210 | int lag, |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 211 | int n |
| 212 | ) |
| 213 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 214 | opus_val32 d; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 215 | int i; |
Jean-Marc Valin | e8e57a3 | 2013-05-25 02:14:25 -0400 | [diff] [blame] | 216 | int fastN=n-lag; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 217 | VARDECL(opus_val16, xx); |
Thorvald Natvig | b8002a0 | 2009-12-11 13:19:09 +0100 | [diff] [blame] | 218 | SAVE_STACK; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 219 | ALLOC(xx, n, opus_val16); |
Gregory Maxwell | 5d5875a | 2011-10-03 21:07:39 -0400 | [diff] [blame] | 220 | celt_assert(n>0); |
| 221 | celt_assert(overlap>=0); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 222 | for (i=0;i<n;i++) |
| 223 | xx[i] = x[i]; |
| 224 | for (i=0;i<overlap;i++) |
| 225 | { |
Jean-Marc Valin | 0da0d91 | 2010-06-17 07:32:20 -0400 | [diff] [blame] | 226 | xx[i] = MULT16_16_Q15(x[i],window[i]); |
| 227 | xx[n-i-1] = MULT16_16_Q15(x[n-i-1],window[i]); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 228 | } |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 229 | #ifdef FIXED_POINT |
| 230 | { |
Timothy B. Terriberry | 85ede2c | 2013-05-22 15:26:12 -0700 | [diff] [blame] | 231 | opus_val32 ac0; |
Jean-Marc Valin | 0da0d91 | 2010-06-17 07:32:20 -0400 | [diff] [blame] | 232 | int shift; |
Timothy B. Terriberry | 85ede2c | 2013-05-22 15:26:12 -0700 | [diff] [blame] | 233 | ac0 = 1+n; |
| 234 | if (n&1) ac0 += SHR32(MULT16_16(xx[0],xx[0]),9); |
| 235 | for(i=(n&1);i<n;i+=2) |
| 236 | { |
Jean-Marc Valin | e1be192 | 2011-11-28 22:48:01 -0500 | [diff] [blame] | 237 | ac0 += SHR32(MULT16_16(xx[i],xx[i]),9); |
Timothy B. Terriberry | 85ede2c | 2013-05-22 15:26:12 -0700 | [diff] [blame] | 238 | ac0 += SHR32(MULT16_16(xx[i+1],xx[i+1]),9); |
| 239 | } |
Jean-Marc Valin | 0da0d91 | 2010-06-17 07:32:20 -0400 | [diff] [blame] | 240 | |
Jean-Marc Valin | e1be192 | 2011-11-28 22:48:01 -0500 | [diff] [blame] | 241 | shift = celt_ilog2(ac0)-30+10; |
Jean-Marc Valin | 0da0d91 | 2010-06-17 07:32:20 -0400 | [diff] [blame] | 242 | shift = (shift+1)/2; |
| 243 | for(i=0;i<n;i++) |
| 244 | xx[i] = VSHR32(xx[i], shift); |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 245 | } |
| 246 | #endif |
Jean-Marc Valin | e8e57a3 | 2013-05-25 02:14:25 -0400 | [diff] [blame] | 247 | pitch_xcorr(xx, xx, ac, fastN, lag+1); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 248 | while (lag>=0) |
| 249 | { |
Jean-Marc Valin | e8e57a3 | 2013-05-25 02:14:25 -0400 | [diff] [blame] | 250 | for (i = lag+fastN, d = 0; i < n; i++) |
Timothy B. Terriberry | 85ede2c | 2013-05-22 15:26:12 -0700 | [diff] [blame] | 251 | d = MAC16_16(d, xx[i], xx[i-lag]); |
Jean-Marc Valin | e8e57a3 | 2013-05-25 02:14:25 -0400 | [diff] [blame] | 252 | ac[lag] += d; |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 253 | /*printf ("%f ", ac[lag]);*/ |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 254 | lag--; |
| 255 | } |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 256 | /*printf ("\n");*/ |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 257 | ac[0] += 10; |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 258 | |
Thorvald Natvig | b8002a0 | 2009-12-11 13:19:09 +0100 | [diff] [blame] | 259 | RESTORE_STACK; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 260 | } |