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 |
| 18 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 19 | 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 | #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 | 6c3788c | 2010-06-20 22:48:50 -0400 | [diff] [blame] | 32 | #include "plc.h" |
| 33 | #include "stack_alloc.h" |
| 34 | #include "mathops.h" |
Jean-Marc Valin | 3a9699e | 2010-06-17 00:35:26 -0400 | [diff] [blame] | 35 | |
Jean-Marc Valin | 6c3788c | 2010-06-20 22:48:50 -0400 | [diff] [blame] | 36 | |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 37 | |
| 38 | |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 39 | void _celt_lpc( |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 40 | opus_val16 *_lpc, /* out: [0...p-1] LPC coefficients */ |
| 41 | const opus_val32 *ac, /* in: [0...p] autocorrelation values */ |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 42 | int p |
| 43 | ) |
| 44 | { |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame^] | 45 | int i, j; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 46 | opus_val32 r; |
| 47 | opus_val32 error = ac[0]; |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 48 | #ifdef FIXED_POINT |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 49 | opus_val32 lpc[LPC_ORDER]; |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 50 | #else |
| 51 | float *lpc = _lpc; |
| 52 | #endif |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 53 | |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 54 | for (i = 0; i < p; i++) |
| 55 | lpc[i] = 0; |
| 56 | if (ac[0] != 0) |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 57 | { |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 58 | for (i = 0; i < p; i++) { |
| 59 | /* Sum up this iteration's reflection coefficient */ |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 60 | opus_val32 rr = 0; |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 61 | for (j = 0; j < i; j++) |
| 62 | rr += MULT32_32_Q31(lpc[j],ac[i - j]); |
| 63 | rr += SHR32(ac[i + 1],3); |
Jean-Marc Valin | bd82ca8 | 2010-06-17 00:11:54 -0400 | [diff] [blame] | 64 | r = -frac_div32(SHL32(rr,3), error); |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 65 | /* Update LPC coefficients and total error */ |
| 66 | lpc[i] = SHR32(r,3); |
| 67 | for (j = 0; j < (i+1)>>1; j++) |
| 68 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 69 | opus_val32 tmp1, tmp2; |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 70 | tmp1 = lpc[j]; |
| 71 | tmp2 = lpc[i-1-j]; |
| 72 | lpc[j] = tmp1 + MULT32_32_Q31(r,tmp2); |
| 73 | lpc[i-1-j] = tmp2 + MULT32_32_Q31(r,tmp1); |
| 74 | } |
| 75 | |
| 76 | error = error - MULT32_32_Q31(MULT32_32_Q31(r,r),error); |
Jean-Marc Valin | bd82ca8 | 2010-06-17 00:11:54 -0400 | [diff] [blame] | 77 | /* Bail out once we get 30 dB gain */ |
| 78 | #ifdef FIXED_POINT |
| 79 | if (error<SHR32(ac[0],10)) |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 80 | break; |
Jean-Marc Valin | bd82ca8 | 2010-06-17 00:11:54 -0400 | [diff] [blame] | 81 | #else |
Gregory Maxwell | 60c316b | 2010-11-04 20:14:19 -0400 | [diff] [blame] | 82 | if (error<.001f*ac[0]) |
Jean-Marc Valin | bd82ca8 | 2010-06-17 00:11:54 -0400 | [diff] [blame] | 83 | break; |
| 84 | #endif |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 85 | } |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 86 | } |
Jean-Marc Valin | 303b3b6 | 2009-12-30 22:40:24 -0500 | [diff] [blame] | 87 | #ifdef FIXED_POINT |
| 88 | for (i=0;i<p;i++) |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 89 | _lpc[i] = ROUND16(lpc[i],16); |
Jean-Marc Valin | 303b3b6 | 2009-12-30 22:40:24 -0500 | [diff] [blame] | 90 | #endif |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 91 | } |
| 92 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 93 | void fir(const opus_val16 *x, |
| 94 | const opus_val16 *num, |
| 95 | opus_val16 *y, |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 96 | int N, |
| 97 | int ord, |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 98 | opus_val16 *mem) |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 99 | { |
| 100 | int i,j; |
| 101 | |
| 102 | for (i=0;i<N;i++) |
| 103 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 104 | opus_val32 sum = SHL32(EXTEND32(x[i]), SIG_SHIFT); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 105 | for (j=0;j<ord;j++) |
| 106 | { |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 107 | sum += MULT16_16(num[j],mem[j]); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 108 | } |
| 109 | for (j=ord-1;j>=1;j--) |
| 110 | { |
| 111 | mem[j]=mem[j-1]; |
| 112 | } |
| 113 | mem[0] = x[i]; |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 114 | y[i] = ROUND16(sum, SIG_SHIFT); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 118 | void iir(const opus_val32 *x, |
| 119 | const opus_val16 *den, |
| 120 | opus_val32 *y, |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 121 | int N, |
| 122 | int ord, |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 123 | opus_val16 *mem) |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 124 | { |
| 125 | int i,j; |
| 126 | for (i=0;i<N;i++) |
| 127 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 128 | opus_val32 sum = x[i]; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 129 | for (j=0;j<ord;j++) |
| 130 | { |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 131 | sum -= MULT16_16(den[j],mem[j]); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 132 | } |
| 133 | for (j=ord-1;j>=1;j--) |
| 134 | { |
| 135 | mem[j]=mem[j-1]; |
| 136 | } |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 137 | mem[0] = ROUND16(sum,SIG_SHIFT); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 138 | y[i] = sum; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void _celt_autocorr( |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 143 | const opus_val16 *x, /* in: [0...n-1] samples x */ |
| 144 | opus_val32 *ac, /* out: [0...lag-1] ac values */ |
| 145 | const opus_val16 *window, |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 146 | int overlap, |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame^] | 147 | int lag, |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 148 | int n |
| 149 | ) |
| 150 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 151 | opus_val32 d; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 152 | int i; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 153 | VARDECL(opus_val16, xx); |
Thorvald Natvig | b8002a0 | 2009-12-11 13:19:09 +0100 | [diff] [blame] | 154 | SAVE_STACK; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 155 | ALLOC(xx, n, opus_val16); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 156 | for (i=0;i<n;i++) |
| 157 | xx[i] = x[i]; |
| 158 | for (i=0;i<overlap;i++) |
| 159 | { |
Jean-Marc Valin | 0da0d91 | 2010-06-17 07:32:20 -0400 | [diff] [blame] | 160 | xx[i] = MULT16_16_Q15(x[i],window[i]); |
| 161 | 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] | 162 | } |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 163 | #ifdef FIXED_POINT |
| 164 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 165 | opus_val32 ac0=0; |
Jean-Marc Valin | 0da0d91 | 2010-06-17 07:32:20 -0400 | [diff] [blame] | 166 | int shift; |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 167 | for(i=0;i<n;i++) |
Jean-Marc Valin | 0da0d91 | 2010-06-17 07:32:20 -0400 | [diff] [blame] | 168 | ac0 += SHR32(MULT16_16(xx[i],xx[i]),8); |
| 169 | ac0 += 1+n; |
| 170 | |
Jean-Marc Valin | 1ad93cf | 2010-11-06 22:02:32 -0400 | [diff] [blame] | 171 | shift = celt_ilog2(ac0)-30+9; |
Jean-Marc Valin | 0da0d91 | 2010-06-17 07:32:20 -0400 | [diff] [blame] | 172 | shift = (shift+1)/2; |
| 173 | for(i=0;i<n;i++) |
| 174 | xx[i] = VSHR32(xx[i], shift); |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 175 | } |
| 176 | #endif |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 177 | while (lag>=0) |
| 178 | { |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame^] | 179 | for (i = lag, d = 0; i < n; i++) |
Jean-Marc Valin | 0da0d91 | 2010-06-17 07:32:20 -0400 | [diff] [blame] | 180 | d += xx[i] * xx[i-lag]; |
| 181 | ac[lag] = d; |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 182 | /*printf ("%f ", ac[lag]);*/ |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 183 | lag--; |
| 184 | } |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 185 | /*printf ("\n");*/ |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 186 | ac[0] += 10; |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 187 | |
Thorvald Natvig | b8002a0 | 2009-12-11 13:19:09 +0100 | [diff] [blame] | 188 | RESTORE_STACK; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 189 | } |