| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 1 | /* (C) 2007 Jean-Marc Valin, CSIRO |
| 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 | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 36 | #include <math.h> |
| 37 | #include <stdlib.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 | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 42 | |
| Jean-Marc Valin | 883bd8e | 2008-02-15 00:34:01 +1100 | [diff] [blame] | 43 | /* Enable this or define your own implementation if you want to speed up the |
| 44 | VQ search (used in inner loop only) */ |
| 45 | #if 0 |
| 46 | #include <xmmintrin.h> |
| 47 | static inline float approx_sqrt(float x) |
| 48 | { |
| 49 | _mm_store_ss(&x, _mm_sqrt_ss(_mm_set_ss(x))); |
| 50 | return x; |
| 51 | } |
| 52 | static inline float approx_inv(float x) |
| 53 | { |
| 54 | _mm_store_ss(&x, _mm_rcp_ss(_mm_set_ss(x))); |
| 55 | return x; |
| 56 | } |
| 57 | #else |
| 58 | #define approx_sqrt(x) (sqrt(x)) |
| 59 | #define approx_inv(x) (1.f/(x)) |
| 60 | #endif |
| 61 | |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 62 | /** Takes the pitch vector and the decoded residual vector (non-compressed), |
| 63 | applies the compression in the pitch direction, computes the gain that will |
| 64 | give ||p+g*y||=1 and mixes the residual with the pitch. */ |
| Jean-Marc Valin | 9d8d9b3 | 2008-02-27 16:17:39 +1100 | [diff] [blame^] | 65 | static void mix_pitch_and_residual(int *iy, celt_norm_t *X, int N, celt_norm_t *P, celt_word16_t alpha) |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 66 | { |
| 67 | int i; |
| 68 | float Rpp=0, Ryp=0, Ryy=0; |
| 69 | float g; |
| 70 | VARDECL(float *y); |
| 71 | VARDECL(float *x); |
| 72 | VARDECL(float *p); |
| Jean-Marc Valin | 9d8d9b3 | 2008-02-27 16:17:39 +1100 | [diff] [blame^] | 73 | float _alpha = Q15_ONE_1*alpha; |
| 74 | |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 75 | ALLOC(y, N, float); |
| 76 | ALLOC(x, N, float); |
| 77 | ALLOC(p, N, float); |
| 78 | |
| 79 | for (i=0;i<N;i++) |
| 80 | p[i] = P[i]*NORM_SCALING_1; |
| 81 | |
| 82 | /*for (i=0;i<N;i++) |
| 83 | printf ("%d ", iy[i]);*/ |
| 84 | for (i=0;i<N;i++) |
| 85 | Rpp += p[i]*p[i]; |
| 86 | |
| 87 | for (i=0;i<N;i++) |
| 88 | Ryp += iy[i]*p[i]; |
| 89 | |
| 90 | for (i=0;i<N;i++) |
| Jean-Marc Valin | 9d8d9b3 | 2008-02-27 16:17:39 +1100 | [diff] [blame^] | 91 | y[i] = iy[i] - _alpha*Ryp*p[i]; |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 92 | |
| 93 | /* Recompute after the projection (I think it's right) */ |
| 94 | Ryp = 0; |
| 95 | for (i=0;i<N;i++) |
| 96 | Ryp += y[i]*p[i]; |
| 97 | |
| 98 | for (i=0;i<N;i++) |
| 99 | Ryy += y[i]*y[i]; |
| 100 | |
| 101 | g = (sqrt(Ryp*Ryp + Ryy - Ryy*Rpp) - Ryp)/Ryy; |
| 102 | |
| 103 | for (i=0;i<N;i++) |
| 104 | X[i] = P[i] + NORM_SCALING*g*y[i]; |
| 105 | } |
| 106 | |
| Jean-Marc Valin | 879fbfd | 2008-02-20 17:17:13 +1100 | [diff] [blame] | 107 | /** All the info necessary to keep track of a hypothesis during the search */ |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 108 | struct NBest { |
| 109 | float score; |
| 110 | float gain; |
| 111 | int sign; |
| 112 | int pos; |
| 113 | int orig; |
| 114 | float xy; |
| 115 | float yy; |
| 116 | float yp; |
| 117 | }; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 118 | |
| Jean-Marc Valin | 9d8d9b3 | 2008-02-27 16:17:39 +1100 | [diff] [blame^] | 119 | void alg_quant(celt_norm_t *X, celt_mask_t *W, int N, int K, celt_norm_t *P, celt_word16_t alpha, ec_enc *enc) |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 120 | { |
| Jean-Marc Valin | 4a89768 | 2007-12-12 00:45:15 +1100 | [diff] [blame] | 121 | int L = 3; |
| Jean-Marc Valin | 2fa8aff | 2008-02-26 11:38:00 +1100 | [diff] [blame] | 122 | VARDECL(float *x); |
| 123 | VARDECL(float *p); |
| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 124 | VARDECL(float *_y); |
| 125 | VARDECL(float *_ny); |
| 126 | VARDECL(int *_iy); |
| 127 | VARDECL(int *_iny); |
| 128 | VARDECL(float **y); |
| 129 | VARDECL(float **ny); |
| 130 | VARDECL(int **iy); |
| 131 | VARDECL(int **iny); |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 132 | int i, j, k, m; |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 133 | int pulsesLeft; |
| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 134 | VARDECL(float *xy); |
| 135 | VARDECL(float *yy); |
| 136 | VARDECL(float *yp); |
| 137 | VARDECL(struct NBest *_nbest); |
| 138 | VARDECL(struct NBest **nbest); |
| Jean-Marc Valin | 1e7087e | 2007-12-04 13:05:43 +1100 | [diff] [blame] | 139 | float Rpp=0, Rxp=0; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 140 | int maxL = 1; |
| Jean-Marc Valin | 9d8d9b3 | 2008-02-27 16:17:39 +1100 | [diff] [blame^] | 141 | float _alpha = Q15_ONE_1*alpha; |
| 142 | |
| Jean-Marc Valin | 2fa8aff | 2008-02-26 11:38:00 +1100 | [diff] [blame] | 143 | ALLOC(x, N, float); |
| 144 | ALLOC(p, N, float); |
| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 145 | ALLOC(_y, L*N, float); |
| 146 | ALLOC(_ny, L*N, float); |
| 147 | ALLOC(_iy, L*N, int); |
| 148 | ALLOC(_iny, L*N, int); |
| 149 | ALLOC(y, L*N, float*); |
| 150 | ALLOC(ny, L*N, float*); |
| 151 | ALLOC(iy, L*N, int*); |
| 152 | ALLOC(iny, L*N, int*); |
| 153 | |
| 154 | ALLOC(xy, L, float); |
| 155 | ALLOC(yy, L, float); |
| 156 | ALLOC(yp, L, float); |
| 157 | ALLOC(_nbest, L, struct NBest); |
| 158 | ALLOC(nbest, L, struct NBest *); |
| 159 | |
| Jean-Marc Valin | b4dfce4 | 2008-02-25 17:41:30 +1100 | [diff] [blame] | 160 | for (j=0;j<N;j++) |
| 161 | { |
| Jean-Marc Valin | 2fa8aff | 2008-02-26 11:38:00 +1100 | [diff] [blame] | 162 | x[j] = X[j]*NORM_SCALING_1; |
| 163 | p[j] = P[j]*NORM_SCALING_1; |
| Jean-Marc Valin | b4dfce4 | 2008-02-25 17:41:30 +1100 | [diff] [blame] | 164 | } |
| 165 | |
| Jean-Marc Valin | fe41983 | 2008-02-12 15:05:01 +1100 | [diff] [blame] | 166 | for (m=0;m<L;m++) |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 167 | nbest[m] = &_nbest[m]; |
| 168 | |
| 169 | for (m=0;m<L;m++) |
| Jean-Marc Valin | fe41983 | 2008-02-12 15:05:01 +1100 | [diff] [blame] | 170 | { |
| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 171 | ny[m] = &_ny[m*N]; |
| 172 | iny[m] = &_iny[m*N]; |
| 173 | y[m] = &_y[m*N]; |
| 174 | iy[m] = &_iy[m*N]; |
| Jean-Marc Valin | fe41983 | 2008-02-12 15:05:01 +1100 | [diff] [blame] | 175 | } |
| 176 | |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 177 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 178 | { |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 179 | Rpp += p[j]*p[j]; |
| Jean-Marc Valin | 1e7087e | 2007-12-04 13:05:43 +1100 | [diff] [blame] | 180 | Rxp += x[j]*p[j]; |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 181 | } |
| Jean-Marc Valin | b60340f | 2008-02-26 15:41:51 +1100 | [diff] [blame] | 182 | if (Rpp>1) |
| 183 | celt_fatal("Rpp > 1"); |
| 184 | |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 185 | /* We only need to initialise the zero because the first iteration only uses that */ |
| 186 | for (i=0;i<N;i++) |
| 187 | y[0][i] = 0; |
| 188 | for (i=0;i<N;i++) |
| 189 | iy[0][i] = 0; |
| 190 | xy[0] = yy[0] = yp[0] = 0; |
| 191 | |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 192 | pulsesLeft = K; |
| 193 | while (pulsesLeft > 0) |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 194 | { |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 195 | int pulsesAtOnce=1; |
| Jean-Marc Valin | cab576e | 2008-02-12 17:21:14 +1100 | [diff] [blame] | 196 | int Lupdate = L; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 197 | int L2 = L; |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 198 | |
| 199 | /* Decide on complexity strategy */ |
| Jean-Marc Valin | cab576e | 2008-02-12 17:21:14 +1100 | [diff] [blame] | 200 | pulsesAtOnce = pulsesLeft/N; |
| 201 | if (pulsesAtOnce<1) |
| 202 | pulsesAtOnce = 1; |
| 203 | if (pulsesLeft-pulsesAtOnce > 3 || N > 30) |
| 204 | Lupdate = 1; |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 205 | /*printf ("%d %d %d/%d %d\n", Lupdate, pulsesAtOnce, pulsesLeft, K, N);*/ |
| Jean-Marc Valin | cab576e | 2008-02-12 17:21:14 +1100 | [diff] [blame] | 206 | L2 = Lupdate; |
| 207 | if (L2>maxL) |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 208 | { |
| 209 | L2 = maxL; |
| 210 | maxL *= N; |
| 211 | } |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 212 | |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 213 | for (m=0;m<Lupdate;m++) |
| Jean-Marc Valin | 883bd8e | 2008-02-15 00:34:01 +1100 | [diff] [blame] | 214 | nbest[m]->score = -1e10f; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 215 | |
| 216 | for (m=0;m<L2;m++) |
| 217 | { |
| 218 | for (j=0;j<N;j++) |
| 219 | { |
| Jean-Marc Valin | 1e7087e | 2007-12-04 13:05:43 +1100 | [diff] [blame] | 220 | int sign; |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 221 | /*if (x[j]>0) sign=1; else sign=-1;*/ |
| Jean-Marc Valin | 1e7087e | 2007-12-04 13:05:43 +1100 | [diff] [blame] | 222 | for (sign=-1;sign<=1;sign+=2) |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 223 | { |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 224 | /*fprintf (stderr, "%d/%d %d/%d %d/%d\n", i, K, m, L2, j, N);*/ |
| Jean-Marc Valin | 1e7087e | 2007-12-04 13:05:43 +1100 | [diff] [blame] | 225 | float tmp_xy, tmp_yy, tmp_yp; |
| 226 | float score; |
| 227 | float g; |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 228 | float s = sign*pulsesAtOnce; |
| Jean-Marc Valin | fe41983 | 2008-02-12 15:05:01 +1100 | [diff] [blame] | 229 | |
| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 230 | /* All pulses at one location must have the same sign. */ |
| 231 | if (iy[m][j]*sign < 0) |
| 232 | continue; |
| 233 | |
| Jean-Marc Valin | a073c7b | 2008-02-14 16:58:04 +1100 | [diff] [blame] | 234 | /* Updating the sums of the new pulse(s) */ |
| Jean-Marc Valin | 9d8d9b3 | 2008-02-27 16:17:39 +1100 | [diff] [blame^] | 235 | tmp_xy = xy[m] + s*x[j] - _alpha*s*p[j]*Rxp; |
| 236 | tmp_yy = yy[m] + 2.f*s*y[m][j] + s*s +s*s*_alpha*_alpha*p[j]*p[j]*Rpp - 2.f*_alpha*s*p[j]*yp[m] - 2.f*s*s*_alpha*p[j]*p[j]; |
| 237 | tmp_yp = yp[m] + s*p[j] *(1.f-_alpha*Rpp); |
| Jean-Marc Valin | a073c7b | 2008-02-14 16:58:04 +1100 | [diff] [blame] | 238 | |
| 239 | /* Compute the gain such that ||p + g*y|| = 1 */ |
| Jean-Marc Valin | 883bd8e | 2008-02-15 00:34:01 +1100 | [diff] [blame] | 240 | g = (approx_sqrt(tmp_yp*tmp_yp + tmp_yy - tmp_yy*Rpp) - tmp_yp)*approx_inv(tmp_yy); |
| Jean-Marc Valin | a073c7b | 2008-02-14 16:58:04 +1100 | [diff] [blame] | 241 | /* Knowing that gain, what the error: (x-g*y)^2 |
| 242 | (result is negated and we discard x^2 because it's constant) */ |
| Jean-Marc Valin | 883bd8e | 2008-02-15 00:34:01 +1100 | [diff] [blame] | 243 | score = 2.f*g*tmp_xy - g*g*tmp_yy; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 244 | |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 245 | if (score>nbest[Lupdate-1]->score) |
| Jean-Marc Valin | 1e7087e | 2007-12-04 13:05:43 +1100 | [diff] [blame] | 246 | { |
| Jean-Marc Valin | 472a5f0 | 2008-02-19 13:12:32 +1100 | [diff] [blame] | 247 | int k; |
| Jean-Marc Valin | cab576e | 2008-02-12 17:21:14 +1100 | [diff] [blame] | 248 | int id = Lupdate-1; |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 249 | struct NBest *tmp_best; |
| 250 | |
| 251 | /* Save some pointers that would be deleted and use them for the current entry*/ |
| 252 | tmp_best = nbest[Lupdate-1]; |
| 253 | while (id > 0 && score > nbest[id-1]->score) |
| Jean-Marc Valin | 1e7087e | 2007-12-04 13:05:43 +1100 | [diff] [blame] | 254 | id--; |
| 255 | |
| Jean-Marc Valin | cab576e | 2008-02-12 17:21:14 +1100 | [diff] [blame] | 256 | for (k=Lupdate-1;k>id;k--) |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 257 | nbest[k] = nbest[k-1]; |
| Jean-Marc Valin | 1e7087e | 2007-12-04 13:05:43 +1100 | [diff] [blame] | 258 | |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 259 | nbest[id] = tmp_best; |
| 260 | nbest[id]->score = score; |
| 261 | nbest[id]->pos = j; |
| 262 | nbest[id]->orig = m; |
| 263 | nbest[id]->sign = sign; |
| 264 | nbest[id]->gain = g; |
| 265 | nbest[id]->xy = tmp_xy; |
| 266 | nbest[id]->yy = tmp_yy; |
| 267 | nbest[id]->yp = tmp_yp; |
| Jean-Marc Valin | 1e7087e | 2007-12-04 13:05:43 +1100 | [diff] [blame] | 268 | } |
| Jean-Marc Valin | 636f5c8 | 2008-02-12 17:40:27 +1100 | [diff] [blame] | 269 | } |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 270 | } |
| Jean-Marc Valin | 636f5c8 | 2008-02-12 17:40:27 +1100 | [diff] [blame] | 271 | |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 272 | } |
| Jean-Marc Valin | b60340f | 2008-02-26 15:41:51 +1100 | [diff] [blame] | 273 | |
| 274 | if (!(nbest[0]->score > -1e10f)) |
| 275 | celt_fatal("Could not find any match in VQ codebook. Something got corrupted somewhere."); |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 276 | /* Only now that we've made the final choice, update ny/iny and others */ |
| Jean-Marc Valin | cab576e | 2008-02-12 17:21:14 +1100 | [diff] [blame] | 277 | for (k=0;k<Lupdate;k++) |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 278 | { |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 279 | int n; |
| 280 | int is; |
| 281 | float s; |
| 282 | is = nbest[k]->sign*pulsesAtOnce; |
| 283 | s = is; |
| 284 | for (n=0;n<N;n++) |
| Jean-Marc Valin | 9d8d9b3 | 2008-02-27 16:17:39 +1100 | [diff] [blame^] | 285 | ny[k][n] = y[nbest[k]->orig][n] - _alpha*s*p[nbest[k]->pos]*p[n]; |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 286 | ny[k][nbest[k]->pos] += s; |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 287 | |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 288 | for (n=0;n<N;n++) |
| 289 | iny[k][n] = iy[nbest[k]->orig][n]; |
| 290 | iny[k][nbest[k]->pos] += is; |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 291 | |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 292 | xy[k] = nbest[k]->xy; |
| 293 | yy[k] = nbest[k]->yy; |
| 294 | yp[k] = nbest[k]->yp; |
| 295 | } |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 296 | /* Swap ny/iny with y/iy */ |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 297 | for (k=0;k<Lupdate;k++) |
| 298 | { |
| Jean-Marc Valin | fe41983 | 2008-02-12 15:05:01 +1100 | [diff] [blame] | 299 | float *tmp_ny; |
| 300 | int *tmp_iny; |
| 301 | |
| Jean-Marc Valin | fe41983 | 2008-02-12 15:05:01 +1100 | [diff] [blame] | 302 | tmp_ny = ny[k]; |
| 303 | ny[k] = y[k]; |
| 304 | y[k] = tmp_ny; |
| 305 | tmp_iny = iny[k]; |
| 306 | iny[k] = iy[k]; |
| 307 | iy[k] = tmp_iny; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 308 | } |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 309 | pulsesLeft -= pulsesAtOnce; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 310 | } |
| 311 | |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 312 | #if 0 |
| Jean-Marc Valin | ac1e03d | 2008-02-12 23:00:18 +1100 | [diff] [blame] | 313 | if (0) { |
| 314 | float err=0; |
| 315 | for (i=0;i<N;i++) |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 316 | err += (x[i]-nbest[0]->gain*y[0][i])*(x[i]-nbest[0]->gain*y[0][i]); |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 317 | /*if (N<=10) |
| 318 | printf ("%f %d %d\n", err, K, N);*/ |
| Jean-Marc Valin | ac1e03d | 2008-02-12 23:00:18 +1100 | [diff] [blame] | 319 | } |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 320 | /* Sanity checks, don't bother */ |
| Jean-Marc Valin | 1e7087e | 2007-12-04 13:05:43 +1100 | [diff] [blame] | 321 | if (0) { |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 322 | for (i=0;i<N;i++) |
| 323 | x[i] = p[i]+nbest[0]->gain*y[0][i]; |
| Jean-Marc Valin | 1e7087e | 2007-12-04 13:05:43 +1100 | [diff] [blame] | 324 | float E=1e-15; |
| 325 | int ABS = 0; |
| 326 | for (i=0;i<N;i++) |
| 327 | ABS += abs(iy[0][i]); |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 328 | /*if (K != ABS) |
| 329 | printf ("%d %d\n", K, ABS);*/ |
| Jean-Marc Valin | 1e7087e | 2007-12-04 13:05:43 +1100 | [diff] [blame] | 330 | for (i=0;i<N;i++) |
| 331 | E += x[i]*x[i]; |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 332 | /*printf ("%f\n", E);*/ |
| Jean-Marc Valin | 1e7087e | 2007-12-04 13:05:43 +1100 | [diff] [blame] | 333 | E = 1/sqrt(E); |
| 334 | for (i=0;i<N;i++) |
| 335 | x[i] *= E; |
| 336 | } |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 337 | #endif |
| Jean-Marc Valin | 5fa5995 | 2008-02-14 13:50:44 +1100 | [diff] [blame] | 338 | |
| 339 | encode_pulses(iy[0], N, K, enc); |
| 340 | |
| Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 341 | /* Recompute the gain in one pass to reduce the encoder-decoder mismatch |
| Jean-Marc Valin | 636f5c8 | 2008-02-12 17:40:27 +1100 | [diff] [blame] | 342 | due to the recursive computation used in quantisation. |
| 343 | Not quite sure whether we need that or not */ |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 344 | mix_pitch_and_residual(iy[0], X, N, P, alpha); |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 345 | } |
| 346 | |
| Jean-Marc Valin | 879fbfd | 2008-02-20 17:17:13 +1100 | [diff] [blame] | 347 | /** Decode pulse vector and combine the result with the pitch vector to produce |
| 348 | the final normalised signal in the current band. */ |
| Jean-Marc Valin | 9d8d9b3 | 2008-02-27 16:17:39 +1100 | [diff] [blame^] | 349 | void alg_unquant(celt_norm_t *X, int N, int K, celt_norm_t *P, celt_word16_t alpha, ec_dec *dec) |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 350 | { |
| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 351 | VARDECL(int *iy); |
| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 352 | ALLOC(iy, N, int); |
| Jean-Marc Valin | 5fa5995 | 2008-02-14 13:50:44 +1100 | [diff] [blame] | 353 | decode_pulses(iy, N, K, dec); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 354 | mix_pitch_and_residual(iy, X, N, P, alpha); |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | |
| 358 | static const float pg[11] = {1.f, .75f, .65f, 0.6f, 0.6f, .6f, .55f, .55f, .5f, .5f, .5f}; |
| 359 | |
| Jean-Marc Valin | 5f09ea5 | 2008-02-26 16:43:04 +1100 | [diff] [blame] | 360 | void intra_prediction(celt_norm_t *x, celt_mask_t *W, int N, int K, celt_norm_t *Y, celt_norm_t *P, int B, int N0, ec_enc *enc) |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 361 | { |
| 362 | int i,j; |
| 363 | int best=0; |
| Jean-Marc Valin | 50d1116 | 2007-12-03 14:34:52 +1100 | [diff] [blame] | 364 | float best_score=0; |
| 365 | float s = 1; |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 366 | int sign; |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 367 | float E; |
| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 368 | float pred_gain; |
| Jean-Marc Valin | 8f0f4b9 | 2008-02-11 13:52:44 +1100 | [diff] [blame] | 369 | int max_pos = N0-N/B; |
| 370 | if (max_pos > 32) |
| 371 | max_pos = 32; |
| 372 | |
| 373 | for (i=0;i<max_pos*B;i+=B) |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 374 | { |
| 375 | int j; |
| 376 | float xy=0, yy=0; |
| 377 | float score; |
| 378 | for (j=0;j<N;j++) |
| 379 | { |
| Jean-Marc Valin | b4dfce4 | 2008-02-25 17:41:30 +1100 | [diff] [blame] | 380 | xy += 1.f*x[j]*Y[i+N-j-1]; |
| 381 | yy += 1.f*Y[i+N-j-1]*Y[i+N-j-1]; |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 382 | } |
| Jean-Marc Valin | 50d1116 | 2007-12-03 14:34:52 +1100 | [diff] [blame] | 383 | score = xy*xy/(.001+yy); |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 384 | if (score > best_score) |
| 385 | { |
| 386 | best_score = score; |
| 387 | best = i; |
| Jean-Marc Valin | 50d1116 | 2007-12-03 14:34:52 +1100 | [diff] [blame] | 388 | if (xy>0) |
| 389 | s = 1; |
| 390 | else |
| 391 | s = -1; |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 392 | } |
| 393 | } |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 394 | if (s<0) |
| 395 | sign = 1; |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 396 | else |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 397 | sign = 0; |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 398 | /*printf ("%d %d ", sign, best);*/ |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 399 | ec_enc_uint(enc,sign,2); |
| Jean-Marc Valin | 8f0f4b9 | 2008-02-11 13:52:44 +1100 | [diff] [blame] | 400 | ec_enc_uint(enc,best/B,max_pos); |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 401 | /*printf ("%d %f\n", best, best_score);*/ |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 402 | |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 403 | if (K>10) |
| 404 | pred_gain = pg[10]; |
| 405 | else |
| 406 | pred_gain = pg[K]; |
| 407 | E = 1e-10; |
| 408 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 409 | { |
| Jean-Marc Valin | d501f61 | 2008-02-21 12:16:57 +1100 | [diff] [blame] | 410 | P[j] = s*Y[best+N-j-1]; |
| Jean-Marc Valin | b4dfce4 | 2008-02-25 17:41:30 +1100 | [diff] [blame] | 411 | E += NORM_SCALING_1*NORM_SCALING_1*P[j]*P[j]; |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 412 | } |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 413 | E = pred_gain/sqrt(E); |
| 414 | for (j=0;j<N;j++) |
| 415 | P[j] *= E; |
| 416 | if (K>0) |
| 417 | { |
| 418 | for (j=0;j<N;j++) |
| 419 | x[j] -= P[j]; |
| 420 | } else { |
| 421 | for (j=0;j<N;j++) |
| 422 | x[j] = P[j]; |
| 423 | } |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 424 | /*printf ("quant ");*/ |
| 425 | /*for (j=0;j<N;j++) printf ("%f ", P[j]);*/ |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 426 | |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 427 | } |
| Jean-Marc Valin | fc08d0a | 2007-12-07 13:26:15 +1100 | [diff] [blame] | 428 | |
| Jean-Marc Valin | 18ddc02 | 2008-02-22 14:24:50 +1100 | [diff] [blame] | 429 | void intra_unquant(celt_norm_t *x, int N, int K, celt_norm_t *Y, celt_norm_t *P, int B, int N0, ec_dec *dec) |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 430 | { |
| Jean-Marc Valin | 11f0172 | 2007-12-09 01:19:36 +1100 | [diff] [blame] | 431 | int j; |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 432 | int sign; |
| 433 | float s; |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 434 | int best; |
| 435 | float E; |
| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 436 | float pred_gain; |
| Jean-Marc Valin | 8f0f4b9 | 2008-02-11 13:52:44 +1100 | [diff] [blame] | 437 | int max_pos = N0-N/B; |
| 438 | if (max_pos > 32) |
| 439 | max_pos = 32; |
| 440 | |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 441 | sign = ec_dec_uint(dec, 2); |
| 442 | if (sign == 0) |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 443 | s = 1; |
| 444 | else |
| 445 | s = -1; |
| 446 | |
| Jean-Marc Valin | 8f0f4b9 | 2008-02-11 13:52:44 +1100 | [diff] [blame] | 447 | best = B*ec_dec_uint(dec, max_pos); |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 448 | /*printf ("%d %d ", sign, best);*/ |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 449 | |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 450 | if (K>10) |
| 451 | pred_gain = pg[10]; |
| 452 | else |
| 453 | pred_gain = pg[K]; |
| 454 | E = 1e-10; |
| 455 | for (j=0;j<N;j++) |
| 456 | { |
| Jean-Marc Valin | d501f61 | 2008-02-21 12:16:57 +1100 | [diff] [blame] | 457 | P[j] = s*Y[best+N-j-1]; |
| Jean-Marc Valin | b4dfce4 | 2008-02-25 17:41:30 +1100 | [diff] [blame] | 458 | E += NORM_SCALING_1*NORM_SCALING_1*P[j]*P[j]; |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 459 | } |
| 460 | E = pred_gain/sqrt(E); |
| 461 | for (j=0;j<N;j++) |
| 462 | P[j] *= E; |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 463 | if (K==0) |
| 464 | { |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 465 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 466 | x[j] = P[j]; |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 467 | } |
| 468 | } |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 469 | |
| Jean-Marc Valin | 18ddc02 | 2008-02-22 14:24:50 +1100 | [diff] [blame] | 470 | void intra_fold(celt_norm_t *x, int N, celt_norm_t *Y, celt_norm_t *P, int B, int N0, int Nmax) |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 471 | { |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 472 | int i, j; |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 473 | float E; |
| 474 | |
| 475 | E = 1e-10; |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 476 | if (N0 >= Nmax/2) |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 477 | { |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 478 | for (i=0;i<B;i++) |
| 479 | { |
| 480 | for (j=0;j<N/B;j++) |
| 481 | { |
| 482 | P[j*B+i] = Y[(Nmax-N0-j-1)*B+i]; |
| Jean-Marc Valin | b4dfce4 | 2008-02-25 17:41:30 +1100 | [diff] [blame] | 483 | E += NORM_SCALING_1*NORM_SCALING_1*P[j*B+i]*P[j*B+i]; |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } else { |
| 487 | for (j=0;j<N;j++) |
| 488 | { |
| 489 | P[j] = Y[j]; |
| Jean-Marc Valin | b4dfce4 | 2008-02-25 17:41:30 +1100 | [diff] [blame] | 490 | E += NORM_SCALING_1*NORM_SCALING_1*P[j]*P[j]; |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 491 | } |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 492 | } |
| 493 | E = 1.f/sqrt(E); |
| 494 | for (j=0;j<N;j++) |
| 495 | P[j] *= E; |
| 496 | for (j=0;j<N;j++) |
| 497 | x[j] = P[j]; |
| 498 | } |
| 499 | |