| Jean-Marc Valin | 8b2ff0d | 2009-10-17 21:40:10 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2007-2008 CSIRO |
| 2 | Copyright (c) 2007-2009 Xiph.Org Foundation |
| 3 | Written by Jean-Marc Valin */ |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 4 | /* |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions |
| 7 | are met: |
| 8 | |
| 9 | - Redistributions of source code must retain the above copyright |
| 10 | notice, this list of conditions and the following disclaimer. |
| 11 | |
| 12 | - Redistributions in binary form must reproduce the above copyright |
| 13 | notice, this list of conditions and the following disclaimer in the |
| 14 | documentation and/or other materials provided with the distribution. |
| 15 | |
| 16 | - Neither the name of the Xiph.org Foundation nor the names of its |
| 17 | contributors may be used to endorse or promote products derived from |
| 18 | this software without specific prior written permission. |
| 19 | |
| 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 24 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| Jean-Marc Valin | 02fa913 | 2008-02-20 12:09:29 +1100 | [diff] [blame] | 33 | #ifdef HAVE_CONFIG_H |
| 34 | #include "config.h" |
| 35 | #endif |
| 36 | |
| Jean-Marc Valin | 3ca9b1d | 2008-02-27 23:50:31 +1100 | [diff] [blame] | 37 | #include "mathops.h" |
| Jean-Marc Valin | 29ccab8 | 2007-12-06 15:39:38 +1100 | [diff] [blame] | 38 | #include "cwrs.h" |
| Jean-Marc Valin | 9cace64 | 2007-12-06 17:44:09 +1100 | [diff] [blame] | 39 | #include "vq.h" |
| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 40 | #include "arch.h" |
| Jean-Marc Valin | b60340f | 2008-02-26 15:41:51 +1100 | [diff] [blame] | 41 | #include "os_support.h" |
| Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 42 | #include "rate.h" |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 43 | |
| Jean-Marc Valin | d5e5436 | 2009-09-30 20:50:41 -0400 | [diff] [blame] | 44 | #ifndef M_PI |
| 45 | #define M_PI 3.141592653 |
| 46 | #endif |
| 47 | |
| Jean-Marc Valin | 354bf60 | 2010-04-03 09:23:29 -0400 | [diff] [blame] | 48 | static void frac_hadamard1(celt_norm *X, int len, int stride, celt_word16 c, celt_word16 s) |
| Jean-Marc Valin | a7750b9 | 2009-08-29 22:52:03 +0100 | [diff] [blame] | 49 | { |
| Jean-Marc Valin | 354bf60 | 2010-04-03 09:23:29 -0400 | [diff] [blame] | 50 | int j; |
| 51 | celt_norm *x, *y; |
| 52 | celt_norm * end; |
| 53 | |
| 54 | j = 0; |
| 55 | x = X; |
| 56 | y = X+stride; |
| 57 | end = X+len; |
| 58 | do |
| Jean-Marc Valin | a7750b9 | 2009-08-29 22:52:03 +0100 | [diff] [blame] | 59 | { |
| Jean-Marc Valin | 354bf60 | 2010-04-03 09:23:29 -0400 | [diff] [blame] | 60 | celt_norm x1, x2; |
| 61 | x1 = *x; |
| 62 | x2 = *y; |
| 63 | *x++ = EXTRACT16(SHR32(MULT16_16(c,x1) + MULT16_16(s,x2),15)); |
| 64 | *y++ = EXTRACT16(SHR32(MULT16_16(s,x1) - MULT16_16(c,x2),15)); |
| 65 | j++; |
| 66 | if (j>=stride) |
| 67 | { |
| 68 | j=0; |
| 69 | x+=stride; |
| 70 | y+=stride; |
| 71 | } |
| 72 | } while (y<end); |
| 73 | |
| 74 | /* Reverse samples so that the next level starts from the other end */ |
| 75 | for (j=0;j<len>>1;j++) |
| Jean-Marc Valin | a7750b9 | 2009-08-29 22:52:03 +0100 | [diff] [blame] | 76 | { |
| Jean-Marc Valin | 354bf60 | 2010-04-03 09:23:29 -0400 | [diff] [blame] | 77 | celt_norm tmp = X[j]; |
| 78 | X[j] = X[len-j-1]; |
| 79 | X[len-j-1] = tmp; |
| Jean-Marc Valin | a7750b9 | 2009-08-29 22:52:03 +0100 | [diff] [blame] | 80 | } |
| Jean-Marc Valin | a7750b9 | 2009-08-29 22:52:03 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| Jean-Marc Valin | 354bf60 | 2010-04-03 09:23:29 -0400 | [diff] [blame] | 83 | #define MAX_LEVELS 8 |
| 84 | static void exp_rotation(celt_norm *X, int len, int dir, int stride, int K) |
| 85 | { |
| 86 | int i, N=0; |
| Jean-Marc Valin | 9a92d61 | 2010-04-03 16:30:49 -0400 | [diff] [blame] | 87 | int transient; |
| Jean-Marc Valin | 354bf60 | 2010-04-03 09:23:29 -0400 | [diff] [blame] | 88 | celt_word16 gain, theta; |
| 89 | int istride[MAX_LEVELS]; |
| Jean-Marc Valin | 9a92d61 | 2010-04-03 16:30:49 -0400 | [diff] [blame] | 90 | celt_word16 c[MAX_LEVELS], s[MAX_LEVELS]; |
| 91 | |
| 92 | if (K >= len) |
| 93 | return; |
| 94 | transient = stride>1; |
| 95 | /*if (len>=30) |
| 96 | { |
| 97 | for (i=0;i<len;i++) |
| 98 | X[i] = 0; |
| 99 | X[30] = 1; |
| 100 | dir = -1; |
| 101 | transient = 1; |
| 102 | }*/ |
| 103 | gain = celt_div((celt_word32)MULT16_16(Q15_ONE,len),(celt_word32)(3+len+4*K)); |
| 104 | /* FIXME: Make that HALF16 instead of HALF32 */ |
| 105 | theta = HALF32(MULT16_16_Q15(gain,gain)); |
| 106 | c[0] = celt_cos_norm(EXTEND32(theta)); |
| 107 | s[0] = celt_cos_norm(EXTEND32(SUB16(Q15ONE,theta))); /* sin(theta) */ |
| Jean-Marc Valin | 354bf60 | 2010-04-03 09:23:29 -0400 | [diff] [blame] | 108 | |
| 109 | do { |
| 110 | istride[N] = stride; |
| 111 | stride *= 2; |
| Jean-Marc Valin | 9a92d61 | 2010-04-03 16:30:49 -0400 | [diff] [blame] | 112 | if (K==1) |
| 113 | theta = QCONST16(.25f,15); |
| 114 | c[N] = c[0]; |
| 115 | s[N] = s[0]; |
| Jean-Marc Valin | 354bf60 | 2010-04-03 09:23:29 -0400 | [diff] [blame] | 116 | N++; |
| 117 | } while (N<MAX_LEVELS && stride < len); |
| 118 | |
| Jean-Marc Valin | 9a92d61 | 2010-04-03 16:30:49 -0400 | [diff] [blame] | 119 | /* This should help a little bit with the transients */ |
| 120 | if (transient) |
| 121 | c[0] = s[0] = QCONST16(.7071068, 15); |
| Jean-Marc Valin | 354bf60 | 2010-04-03 09:23:29 -0400 | [diff] [blame] | 122 | |
| Jean-Marc Valin | 9a92d61 | 2010-04-03 16:30:49 -0400 | [diff] [blame] | 123 | /* Needs to be < 0 to prevent gaps on the side of the spreading */ |
| 124 | if (dir < 0) |
| Jean-Marc Valin | 354bf60 | 2010-04-03 09:23:29 -0400 | [diff] [blame] | 125 | { |
| 126 | for (i=0;i<N;i++) |
| Jean-Marc Valin | 9a92d61 | 2010-04-03 16:30:49 -0400 | [diff] [blame] | 127 | frac_hadamard1(X, len, istride[i], c[i], s[i]); |
| Jean-Marc Valin | 354bf60 | 2010-04-03 09:23:29 -0400 | [diff] [blame] | 128 | } else { |
| 129 | for (i=N-1;i>=0;i--) |
| Jean-Marc Valin | 9a92d61 | 2010-04-03 16:30:49 -0400 | [diff] [blame] | 130 | frac_hadamard1(X, len, istride[i], c[i], s[i]); |
| Jean-Marc Valin | 354bf60 | 2010-04-03 09:23:29 -0400 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* Undo last reversal */ |
| 134 | for (i=0;i<len>>1;i++) |
| 135 | { |
| 136 | celt_norm tmp = X[i]; |
| 137 | X[i] = X[len-i-1]; |
| 138 | X[len-i-1] = tmp; |
| 139 | } |
| Jean-Marc Valin | 9a92d61 | 2010-04-03 16:30:49 -0400 | [diff] [blame] | 140 | /*if (len>=30) |
| 141 | { |
| 142 | for (i=0;i<len;i++) |
| 143 | printf ("%f ", X[i]); |
| 144 | printf ("\n"); |
| 145 | exit(0); |
| 146 | }*/ |
| Jean-Marc Valin | 354bf60 | 2010-04-03 09:23:29 -0400 | [diff] [blame] | 147 | } |
| Jean-Marc Valin | a7750b9 | 2009-08-29 22:52:03 +0100 | [diff] [blame] | 148 | |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 149 | /** Takes the pitch vector and the decoded residual vector, computes the gain |
| 150 | that will give ||p+g*y||=1 and mixes the residual with the pitch. */ |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 151 | static void normalise_residual(int * restrict iy, celt_norm * restrict X, int N, int K, celt_word32 Ryy) |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 152 | { |
| 153 | int i; |
| Timothy Terriberry | 8c7bb4c | 2009-10-31 10:19:06 -0700 | [diff] [blame] | 154 | #ifdef FIXED_POINT |
| 155 | int k; |
| 156 | #endif |
| 157 | celt_word32 t; |
| 158 | celt_word16 g; |
| Jean-Marc Valin | f6dc1eb | 2009-10-06 20:08:49 -0400 | [diff] [blame] | 159 | |
| Timothy Terriberry | 8c7bb4c | 2009-10-31 10:19:06 -0700 | [diff] [blame] | 160 | #ifdef FIXED_POINT |
| 161 | k = celt_ilog2(Ryy)>>1; |
| 162 | #endif |
| 163 | t = VSHR32(Ryy, (k-7)<<1); |
| 164 | g = celt_rsqrt_norm(t); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 165 | |
| Jean-Marc Valin | 6ea8bae | 2008-04-15 08:01:33 +1000 | [diff] [blame] | 166 | i=0; |
| Jean-Marc Valin | f6dc1eb | 2009-10-06 20:08:49 -0400 | [diff] [blame] | 167 | do |
| Timothy Terriberry | 8c7bb4c | 2009-10-31 10:19:06 -0700 | [diff] [blame] | 168 | X[i] = EXTRACT16(PSHR32(MULT16_16(g, iy[i]), k+1)); |
| Jean-Marc Valin | 6ea8bae | 2008-04-15 08:01:33 +1000 | [diff] [blame] | 169 | while (++i < N); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 170 | } |
| 171 | |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 172 | void alg_quant(celt_norm *X, int N, int K, int spread, ec_enc *enc) |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 173 | { |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 174 | VARDECL(celt_norm, y); |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 175 | VARDECL(int, iy); |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 176 | VARDECL(celt_word16, signx); |
| Jean-Marc Valin | 6ea8bae | 2008-04-15 08:01:33 +1000 | [diff] [blame] | 177 | int j, is; |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 178 | celt_word16 s; |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 179 | int pulsesLeft; |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 180 | celt_word32 sum; |
| 181 | celt_word32 xy, yy; |
| Jean-Marc Valin | f958477 | 2008-03-27 12:22:44 +1100 | [diff] [blame] | 182 | int N_1; /* Inverse of N, in Q14 format (even for float) */ |
| Jean-Marc Valin | f675adc | 2008-02-28 12:15:17 +1100 | [diff] [blame] | 183 | #ifdef FIXED_POINT |
| Jean-Marc Valin | d748cd5 | 2008-03-01 07:27:03 +1100 | [diff] [blame] | 184 | int yshift; |
| 185 | #endif |
| 186 | SAVE_STACK; |
| 187 | |
| Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 188 | K = get_pulses(K); |
| Jean-Marc Valin | d748cd5 | 2008-03-01 07:27:03 +1100 | [diff] [blame] | 189 | #ifdef FIXED_POINT |
| Jean-Marc Valin | 98c86c7 | 2008-03-27 08:40:45 +1100 | [diff] [blame] | 190 | yshift = 13-celt_ilog2(K); |
| Jean-Marc Valin | f675adc | 2008-02-28 12:15:17 +1100 | [diff] [blame] | 191 | #endif |
| Jean-Marc Valin | 9d8d9b3 | 2008-02-27 16:17:39 +1100 | [diff] [blame] | 192 | |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 193 | ALLOC(y, N, celt_norm); |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 194 | ALLOC(iy, N, int); |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 195 | ALLOC(signx, N, celt_word16); |
| Jean-Marc Valin | 124d1cd | 2008-03-28 00:33:04 +1100 | [diff] [blame] | 196 | N_1 = 512/N; |
| Jean-Marc Valin | a7750b9 | 2009-08-29 22:52:03 +0100 | [diff] [blame] | 197 | |
| 198 | if (spread) |
| 199 | exp_rotation(X, N, 1, spread, K); |
| Jean-Marc Valin | 3d152a5 | 2008-04-15 07:46:48 +1000 | [diff] [blame] | 200 | |
| 201 | sum = 0; |
| Jean-Marc Valin | dff9b7e | 2008-04-21 11:43:51 +1000 | [diff] [blame] | 202 | j=0; do { |
| Jean-Marc Valin | 4913438 | 2008-03-25 16:07:05 +1100 | [diff] [blame] | 203 | if (X[j]>0) |
| 204 | signx[j]=1; |
| Jean-Marc Valin | 6cde5dd | 2008-12-04 21:21:41 -0500 | [diff] [blame] | 205 | else { |
| Jean-Marc Valin | 4913438 | 2008-03-25 16:07:05 +1100 | [diff] [blame] | 206 | signx[j]=-1; |
| Jean-Marc Valin | 6cde5dd | 2008-12-04 21:21:41 -0500 | [diff] [blame] | 207 | X[j]=-X[j]; |
| Jean-Marc Valin | 6cde5dd | 2008-12-04 21:21:41 -0500 | [diff] [blame] | 208 | } |
| Jean-Marc Valin | 3d152a5 | 2008-04-15 07:46:48 +1000 | [diff] [blame] | 209 | iy[j] = 0; |
| 210 | y[j] = 0; |
| Jean-Marc Valin | dff9b7e | 2008-04-21 11:43:51 +1000 | [diff] [blame] | 211 | } while (++j<N); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 212 | |
| Jean-Marc Valin | 095c178 | 2009-09-17 22:38:34 -0400 | [diff] [blame] | 213 | xy = yy = 0; |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 214 | |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 215 | pulsesLeft = K; |
| Jean-Marc Valin | 8256ed4 | 2008-12-12 20:50:56 -0500 | [diff] [blame] | 216 | |
| 217 | /* Do a pre-search by projecting on the pyramid */ |
| Jean-Marc Valin | a733f08 | 2008-12-04 22:52:26 -0500 | [diff] [blame] | 218 | if (K > (N>>1)) |
| 219 | { |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 220 | celt_word16 rcp; |
| Jean-Marc Valin | a733f08 | 2008-12-04 22:52:26 -0500 | [diff] [blame] | 221 | j=0; do { |
| 222 | sum += X[j]; |
| 223 | } while (++j<N); |
| Jean-Marc Valin | 6d454d8 | 2009-06-30 10:31:00 -0400 | [diff] [blame] | 224 | |
| 225 | #ifdef FIXED_POINT |
| 226 | if (sum <= K) |
| 227 | #else |
| 228 | if (sum <= EPSILON) |
| 229 | #endif |
| Jean-Marc Valin | 8256ed4 | 2008-12-12 20:50:56 -0500 | [diff] [blame] | 230 | { |
| Jean-Marc Valin | da1156a | 2009-07-01 01:27:48 -0400 | [diff] [blame] | 231 | X[0] = QCONST16(1.f,14); |
| Jean-Marc Valin | 6d454d8 | 2009-06-30 10:31:00 -0400 | [diff] [blame] | 232 | j=1; do |
| 233 | X[j]=0; |
| 234 | while (++j<N); |
| Jean-Marc Valin | da1156a | 2009-07-01 01:27:48 -0400 | [diff] [blame] | 235 | sum = QCONST16(1.f,14); |
| Jean-Marc Valin | 8256ed4 | 2008-12-12 20:50:56 -0500 | [diff] [blame] | 236 | } |
| 237 | /* Do we have sufficient accuracy here? */ |
| 238 | rcp = EXTRACT16(MULT16_32_Q16(K-1, celt_rcp(sum))); |
| Jean-Marc Valin | a733f08 | 2008-12-04 22:52:26 -0500 | [diff] [blame] | 239 | j=0; do { |
| Jean-Marc Valin | 09dc5a1 | 2008-12-05 00:28:28 -0500 | [diff] [blame] | 240 | #ifdef FIXED_POINT |
| Jean-Marc Valin | 137241d | 2008-12-06 23:44:55 -0500 | [diff] [blame] | 241 | /* It's really important to round *towards zero* here */ |
| Jean-Marc Valin | 8256ed4 | 2008-12-12 20:50:56 -0500 | [diff] [blame] | 242 | iy[j] = MULT16_16_Q15(X[j],rcp); |
| Jean-Marc Valin | 09dc5a1 | 2008-12-05 00:28:28 -0500 | [diff] [blame] | 243 | #else |
| Jean-Marc Valin | 8256ed4 | 2008-12-12 20:50:56 -0500 | [diff] [blame] | 244 | iy[j] = floor(rcp*X[j]); |
| Jean-Marc Valin | 09dc5a1 | 2008-12-05 00:28:28 -0500 | [diff] [blame] | 245 | #endif |
| Jean-Marc Valin | c7635b4 | 2008-12-04 23:26:32 -0500 | [diff] [blame] | 246 | y[j] = SHL16(iy[j],yshift); |
| 247 | yy = MAC16_16(yy, y[j],y[j]); |
| 248 | xy = MAC16_16(xy, X[j],y[j]); |
| Jean-Marc Valin | 09dc5a1 | 2008-12-05 00:28:28 -0500 | [diff] [blame] | 249 | y[j] *= 2; |
| Jean-Marc Valin | a733f08 | 2008-12-04 22:52:26 -0500 | [diff] [blame] | 250 | pulsesLeft -= iy[j]; |
| 251 | } while (++j<N); |
| 252 | } |
| Jean-Marc Valin | 137241d | 2008-12-06 23:44:55 -0500 | [diff] [blame] | 253 | celt_assert2(pulsesLeft>=1, "Allocated too many pulses in the quick pass"); |
| Jean-Marc Valin | 8256ed4 | 2008-12-12 20:50:56 -0500 | [diff] [blame] | 254 | |
| Jean-Marc Valin | 095c178 | 2009-09-17 22:38:34 -0400 | [diff] [blame] | 255 | while (pulsesLeft > 0) |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 256 | { |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 257 | int pulsesAtOnce=1; |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 258 | int best_id; |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 259 | celt_word16 magnitude; |
| 260 | celt_word32 best_num = -VERY_LARGE16; |
| 261 | celt_word16 best_den = 0; |
| Jean-Marc Valin | 0bc5f7f | 2008-04-20 17:16:18 +1000 | [diff] [blame] | 262 | #ifdef FIXED_POINT |
| 263 | int rshift; |
| 264 | #endif |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 265 | /* Decide on how many pulses to find at once */ |
| Jean-Marc Valin | 124d1cd | 2008-03-28 00:33:04 +1100 | [diff] [blame] | 266 | pulsesAtOnce = (pulsesLeft*N_1)>>9; /* pulsesLeft/N */ |
| Jean-Marc Valin | cab576e | 2008-02-12 17:21:14 +1100 | [diff] [blame] | 267 | if (pulsesAtOnce<1) |
| 268 | pulsesAtOnce = 1; |
| Jean-Marc Valin | 0bc5f7f | 2008-04-20 17:16:18 +1000 | [diff] [blame] | 269 | #ifdef FIXED_POINT |
| 270 | rshift = yshift+1+celt_ilog2(K-pulsesLeft+pulsesAtOnce); |
| 271 | #endif |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 272 | magnitude = SHL16(pulsesAtOnce, yshift); |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 273 | |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 274 | best_id = 0; |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 275 | /* The squared magnitude term gets added anyway, so we might as well |
| 276 | add it outside the loop */ |
| Jean-Marc Valin | 1dab60c | 2008-09-16 13:29:37 -0400 | [diff] [blame] | 277 | yy = MAC16_16(yy, magnitude,magnitude); |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 278 | /* Choose between fast and accurate strategy depending on where we are in the search */ |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 279 | /* This should ensure that anything we can process will have a better score */ |
| Jean-Marc Valin | 7bb339d | 2008-09-21 21:11:39 -0400 | [diff] [blame] | 280 | j=0; |
| 281 | do { |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 282 | celt_word16 Rxy, Ryy; |
| Jean-Marc Valin | 7bb339d | 2008-09-21 21:11:39 -0400 | [diff] [blame] | 283 | /* Select sign based on X[j] alone */ |
| Jean-Marc Valin | 6cde5dd | 2008-12-04 21:21:41 -0500 | [diff] [blame] | 284 | s = magnitude; |
| Jean-Marc Valin | 7bb339d | 2008-09-21 21:11:39 -0400 | [diff] [blame] | 285 | /* Temporary sums of the new pulse(s) */ |
| 286 | Rxy = EXTRACT16(SHR32(MAC16_16(xy, s,X[j]),rshift)); |
| 287 | /* We're multiplying y[j] by two so we don't have to do it here */ |
| 288 | Ryy = EXTRACT16(SHR32(MAC16_16(yy, s,y[j]),rshift)); |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 289 | |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 290 | /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that |
| Jean-Marc Valin | 7bb339d | 2008-09-21 21:11:39 -0400 | [diff] [blame] | 291 | Rxy is positive because the sign is pre-computed) */ |
| 292 | Rxy = MULT16_16_Q15(Rxy,Rxy); |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 293 | /* The idea is to check for num/den >= best_num/best_den, but that way |
| Jean-Marc Valin | 7bb339d | 2008-09-21 21:11:39 -0400 | [diff] [blame] | 294 | we can do it without any division */ |
| 295 | /* OPT: Make sure to use conditional moves here */ |
| 296 | if (MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num)) |
| 297 | { |
| 298 | best_den = Ryy; |
| 299 | best_num = Rxy; |
| 300 | best_id = j; |
| 301 | } |
| 302 | } while (++j<N); |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 303 | |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 304 | j = best_id; |
| Jean-Marc Valin | 6cde5dd | 2008-12-04 21:21:41 -0500 | [diff] [blame] | 305 | is = pulsesAtOnce; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 306 | s = SHL16(is, yshift); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 307 | |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 308 | /* Updating the sums of the new pulse(s) */ |
| 309 | xy = xy + MULT16_16(s,X[j]); |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 310 | /* We're multiplying y[j] by two so we don't have to do it here */ |
| 311 | yy = yy + MULT16_16(s,y[j]); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 312 | |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 313 | /* Only now that we've made the final choice, update y/iy */ |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 314 | /* Multiplying y[j] by 2 so we don't have to do it everywhere else */ |
| 315 | y[j] += 2*s; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 316 | iy[j] += is; |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 317 | pulsesLeft -= pulsesAtOnce; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 318 | } |
| Jean-Marc Valin | 6cde5dd | 2008-12-04 21:21:41 -0500 | [diff] [blame] | 319 | j=0; |
| 320 | do { |
| Jean-Marc Valin | 6cde5dd | 2008-12-04 21:21:41 -0500 | [diff] [blame] | 321 | X[j] = MULT16_16(signx[j],X[j]); |
| 322 | if (signx[j] < 0) |
| 323 | iy[j] = -iy[j]; |
| 324 | } while (++j<N); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 325 | encode_pulses(iy, N, K, enc); |
| Jean-Marc Valin | 5fa5995 | 2008-02-14 13:50:44 +1100 | [diff] [blame] | 326 | |
| Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 327 | /* Recompute the gain in one pass to reduce the encoder-decoder mismatch |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 328 | due to the recursive computation used in quantisation. */ |
| Jean-Marc Valin | f7a1e16 | 2009-10-07 06:56:03 -0400 | [diff] [blame] | 329 | normalise_residual(iy, X, N, K, EXTRACT16(SHR32(yy,2*yshift))); |
| Jean-Marc Valin | a7750b9 | 2009-08-29 22:52:03 +0100 | [diff] [blame] | 330 | if (spread) |
| 331 | exp_rotation(X, N, -1, spread, K); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 332 | RESTORE_STACK; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 333 | } |
| 334 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 335 | |
| Jean-Marc Valin | 879fbfd | 2008-02-20 17:17:13 +1100 | [diff] [blame] | 336 | /** Decode pulse vector and combine the result with the pitch vector to produce |
| 337 | the final normalised signal in the current band. */ |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 338 | void alg_unquant(celt_norm *X, int N, int K, int spread, ec_dec *dec) |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 339 | { |
| Jean-Marc Valin | f6dc1eb | 2009-10-06 20:08:49 -0400 | [diff] [blame] | 340 | int i; |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 341 | celt_word32 Ryy; |
| Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 342 | VARDECL(int, iy); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 343 | SAVE_STACK; |
| Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 344 | K = get_pulses(K); |
| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 345 | ALLOC(iy, N, int); |
| Jean-Marc Valin | 5fa5995 | 2008-02-14 13:50:44 +1100 | [diff] [blame] | 346 | decode_pulses(iy, N, K, dec); |
| Jean-Marc Valin | f6dc1eb | 2009-10-06 20:08:49 -0400 | [diff] [blame] | 347 | Ryy = 0; |
| 348 | i=0; |
| 349 | do { |
| 350 | Ryy = MAC16_16(Ryy, iy[i], iy[i]); |
| 351 | } while (++i < N); |
| Jean-Marc Valin | f7a1e16 | 2009-10-07 06:56:03 -0400 | [diff] [blame] | 352 | normalise_residual(iy, X, N, K, Ryy); |
| Jean-Marc Valin | a7750b9 | 2009-08-29 22:52:03 +0100 | [diff] [blame] | 353 | if (spread) |
| 354 | exp_rotation(X, N, -1, spread, K); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 355 | RESTORE_STACK; |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 356 | } |
| 357 | |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 358 | celt_word16 renormalise_vector(celt_norm *X, celt_word16 value, int N, int stride) |
| Jean-Marc Valin | 6361ad8 | 2008-07-20 23:14:31 -0400 | [diff] [blame] | 359 | { |
| 360 | int i; |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 361 | celt_word32 E = EPSILON; |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 362 | celt_word16 g; |
| Jean-Marc Valin | 137f336 | 2010-04-14 17:42:22 -0400 | [diff] [blame] | 363 | celt_word32 t; |
| Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 364 | celt_norm *xptr = X; |
| Jean-Marc Valin | 6361ad8 | 2008-07-20 23:14:31 -0400 | [diff] [blame] | 365 | for (i=0;i<N;i++) |
| 366 | { |
| 367 | E = MAC16_16(E, *xptr, *xptr); |
| 368 | xptr += stride; |
| 369 | } |
| Jean-Marc Valin | cd29b02 | 2009-07-01 09:59:21 -0400 | [diff] [blame] | 370 | #ifdef FIXED_POINT |
| Jean-Marc Valin | 3a4a463 | 2010-03-15 22:55:51 -0400 | [diff] [blame] | 371 | int k = celt_ilog2(E)>>1; |
| Jean-Marc Valin | cd29b02 | 2009-07-01 09:59:21 -0400 | [diff] [blame] | 372 | #endif |
| Jean-Marc Valin | 137f336 | 2010-04-14 17:42:22 -0400 | [diff] [blame] | 373 | t = VSHR32(E, (k-7)<<1); |
| Jean-Marc Valin | 3a4a463 | 2010-03-15 22:55:51 -0400 | [diff] [blame] | 374 | g = MULT16_16_Q15(value, celt_rsqrt_norm(t)); |
| 375 | |
| Jean-Marc Valin | 6361ad8 | 2008-07-20 23:14:31 -0400 | [diff] [blame] | 376 | xptr = X; |
| 377 | for (i=0;i<N;i++) |
| 378 | { |
| Jean-Marc Valin | 3a4a463 | 2010-03-15 22:55:51 -0400 | [diff] [blame] | 379 | *xptr = EXTRACT16(PSHR32(MULT16_16(g, *xptr), k+1)); |
| Jean-Marc Valin | 6361ad8 | 2008-07-20 23:14:31 -0400 | [diff] [blame] | 380 | xptr += stride; |
| 381 | } |
| Jean-Marc Valin | 3a4a463 | 2010-03-15 22:55:51 -0400 | [diff] [blame] | 382 | return celt_sqrt(E); |
| Jean-Marc Valin | 6361ad8 | 2008-07-20 23:14:31 -0400 | [diff] [blame] | 383 | } |
| 384 | |
| Jean-Marc Valin | 3a0bc3d | 2010-02-21 15:10:22 -0500 | [diff] [blame] | 385 | static void fold(const CELTMode *m, int start, int N, const celt_norm * restrict Y, celt_norm * restrict P, int N0, int B) |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 386 | { |
| Jean-Marc Valin | df38f2b | 2008-07-20 20:36:54 -0400 | [diff] [blame] | 387 | int j; |
| Jean-Marc Valin | d5e5436 | 2009-09-30 20:50:41 -0400 | [diff] [blame] | 388 | int id = N0 % B; |
| Jean-Marc Valin | 3a0bc3d | 2010-02-21 15:10:22 -0500 | [diff] [blame] | 389 | while (id < m->eBands[start]) |
| 390 | id += B; |
| Jean-Marc Valin | df38f2b | 2008-07-20 20:36:54 -0400 | [diff] [blame] | 391 | /* Here, we assume that id will never be greater than N0, i.e. that |
| Jean-Marc Valin | 5eef264 | 2008-08-06 23:06:31 -0400 | [diff] [blame] | 392 | no band is wider than N0. In the unlikely case it happens, we set |
| 393 | everything to zero */ |
| Jean-Marc Valin | 4e5b7bc | 2009-07-03 15:09:07 -0400 | [diff] [blame] | 394 | /*{ |
| 395 | int offset = (N0*C - (id+C*N))/2; |
| 396 | if (offset > C*N0/16) |
| 397 | offset = C*N0/16; |
| 398 | offset -= offset % (C*B); |
| 399 | if (offset < 0) |
| 400 | offset = 0; |
| 401 | //printf ("%d\n", offset); |
| 402 | id += offset; |
| 403 | }*/ |
| Jean-Marc Valin | d5e5436 | 2009-09-30 20:50:41 -0400 | [diff] [blame] | 404 | if (id+N>N0) |
| 405 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 5eef264 | 2008-08-06 23:06:31 -0400 | [diff] [blame] | 406 | P[j] = 0; |
| 407 | else |
| Jean-Marc Valin | d5e5436 | 2009-09-30 20:50:41 -0400 | [diff] [blame] | 408 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 5eef264 | 2008-08-06 23:06:31 -0400 | [diff] [blame] | 409 | P[j] = Y[id++]; |
| Jean-Marc Valin | 2c73306 | 2008-07-17 16:22:23 -0400 | [diff] [blame] | 410 | } |
| 411 | |
| Jean-Marc Valin | 3a0bc3d | 2010-02-21 15:10:22 -0500 | [diff] [blame] | 412 | void intra_fold(const CELTMode *m, int start, int N, const celt_norm * restrict Y, celt_norm * restrict P, int N0, int B) |
| Jean-Marc Valin | 2c73306 | 2008-07-17 16:22:23 -0400 | [diff] [blame] | 413 | { |
| Jean-Marc Valin | 3a0bc3d | 2010-02-21 15:10:22 -0500 | [diff] [blame] | 414 | fold(m, start, N, Y, P, N0, B); |
| Jean-Marc Valin | d5e5436 | 2009-09-30 20:50:41 -0400 | [diff] [blame] | 415 | renormalise_vector(P, Q15ONE, N, 1); |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 416 | } |
| 417 | |