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