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