| 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 | 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 | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 41 | |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 42 | /** Takes the pitch vector and the decoded residual vector (non-compressed), |
| 43 | applies the compression in the pitch direction, computes the gain that will |
| 44 | give ||p+g*y||=1 and mixes the residual with the pitch. */ |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 45 | static void mix_pitch_and_residual(int *iy, celt_norm_t *X, int N, int K, const celt_norm_t *P) |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 46 | { |
| 47 | int i; |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 48 | celt_word32_t Ryp, Ryy, Rpp; |
| Jean-Marc Valin | a847b77 | 2008-02-27 17:46:49 +1100 | [diff] [blame] | 49 | celt_word32_t g; |
| Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 50 | VARDECL(celt_norm_t, y); |
| Jean-Marc Valin | d9de593 | 2008-03-05 08:11:57 +1100 | [diff] [blame] | 51 | #ifdef FIXED_POINT |
| 52 | int yshift; |
| 53 | #endif |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 54 | SAVE_STACK; |
| Jean-Marc Valin | d17edd3 | 2008-02-27 16:52:30 +1100 | [diff] [blame] | 55 | #ifdef FIXED_POINT |
| Jean-Marc Valin | d9de593 | 2008-03-05 08:11:57 +1100 | [diff] [blame] | 56 | yshift = 14-EC_ILOG(K); |
| Jean-Marc Valin | d17edd3 | 2008-02-27 16:52:30 +1100 | [diff] [blame] | 57 | #endif |
| 58 | ALLOC(y, N, celt_norm_t); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 59 | |
| 60 | /*for (i=0;i<N;i++) |
| 61 | printf ("%d ", iy[i]);*/ |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 62 | Rpp = 0; |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 63 | for (i=0;i<N;i++) |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 64 | Rpp = MAC16_16(Rpp,P[i],P[i]); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 65 | |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 66 | Ryp = 0; |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 67 | for (i=0;i<N;i++) |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 68 | Ryp = MAC16_16(Ryp,SHL16(iy[i],yshift),P[i]); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 69 | |
| Jean-Marc Valin | d17edd3 | 2008-02-27 16:52:30 +1100 | [diff] [blame] | 70 | /* Remove part of the pitch component to compute the real residual from |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 71 | the encoded (int) one */ |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 72 | for (i=0;i<N;i++) |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 73 | y[i] = SHL16(iy[i],yshift); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 74 | |
| 75 | /* Recompute after the projection (I think it's right) */ |
| 76 | Ryp = 0; |
| 77 | for (i=0;i<N;i++) |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 78 | Ryp = MAC16_16(Ryp,y[i],P[i]); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 79 | |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 80 | Ryy = 0; |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 81 | for (i=0;i<N;i++) |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 82 | Ryy = MAC16_16(Ryy, y[i],y[i]); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 83 | |
| Jean-Marc Valin | 1ca0722 | 2008-02-27 17:23:04 +1100 | [diff] [blame] | 84 | /* g = (sqrt(Ryp^2 + Ryy - Rpp*Ryy)-Ryp)/Ryy */ |
| Jean-Marc Valin | 9d5b4a6 | 2008-03-13 11:36:45 +1100 | [diff] [blame] | 85 | g = MULT16_32_Q15( |
| Jean-Marc Valin | f5b0587 | 2008-03-21 10:46:17 +1100 | [diff] [blame] | 86 | celt_sqrt(MULT16_16(ROUND16(Ryp,14),ROUND16(Ryp,14)) + Ryy - |
| 87 | MULT16_16(ROUND16(Ryy,14),ROUND16(Rpp,14))) |
| 88 | - ROUND16(Ryp,14), |
| Jean-Marc Valin | 9d5b4a6 | 2008-03-13 11:36:45 +1100 | [diff] [blame] | 89 | celt_rcp(SHR32(Ryy,9))); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 90 | |
| 91 | for (i=0;i<N;i++) |
| Jean-Marc Valin | f5b0587 | 2008-03-21 10:46:17 +1100 | [diff] [blame] | 92 | X[i] = P[i] + ROUND16(MULT16_16(y[i], g),11); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 93 | RESTORE_STACK; |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 94 | } |
| 95 | |
| Jean-Marc Valin | 879fbfd | 2008-02-20 17:17:13 +1100 | [diff] [blame] | 96 | /** 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] | 97 | struct NBest { |
| Jean-Marc Valin | 4e1328b | 2008-02-28 15:14:07 +1100 | [diff] [blame] | 98 | celt_word32_t score; |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 99 | int sign; |
| 100 | int pos; |
| Jean-Marc Valin | 4e1328b | 2008-02-28 15:14:07 +1100 | [diff] [blame] | 101 | celt_word32_t xy; |
| 102 | celt_word32_t yy; |
| 103 | celt_word32_t yp; |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 104 | }; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 105 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 106 | void alg_quant(celt_norm_t *X, celt_mask_t *W, int N, int K, const celt_norm_t *P, ec_enc *enc) |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 107 | { |
| Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 108 | VARDECL(celt_norm_t, _y); |
| 109 | VARDECL(celt_norm_t, _ny); |
| 110 | VARDECL(int, _iy); |
| 111 | VARDECL(int, _iny); |
| Jean-Marc Valin | 4913438 | 2008-03-25 16:07:05 +1100 | [diff] [blame^] | 112 | VARDECL(int, signx); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 113 | celt_norm_t *y, *ny; |
| 114 | int *iy, *iny; |
| 115 | int i, j; |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 116 | int pulsesLeft; |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 117 | celt_word32_t xy, yy, yp; |
| 118 | struct NBest nbest; |
| Jean-Marc Valin | f675adc | 2008-02-28 12:15:17 +1100 | [diff] [blame] | 119 | celt_word32_t Rpp=0, Rxp=0; |
| Jean-Marc Valin | f675adc | 2008-02-28 12:15:17 +1100 | [diff] [blame] | 120 | #ifdef FIXED_POINT |
| Jean-Marc Valin | d748cd5 | 2008-03-01 07:27:03 +1100 | [diff] [blame] | 121 | int yshift; |
| 122 | #endif |
| 123 | SAVE_STACK; |
| 124 | |
| 125 | #ifdef FIXED_POINT |
| 126 | yshift = 14-EC_ILOG(K); |
| Jean-Marc Valin | f675adc | 2008-02-28 12:15:17 +1100 | [diff] [blame] | 127 | #endif |
| Jean-Marc Valin | 9d8d9b3 | 2008-02-27 16:17:39 +1100 | [diff] [blame] | 128 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 129 | ALLOC(_y, N, celt_norm_t); |
| 130 | ALLOC(_ny, N, celt_norm_t); |
| 131 | ALLOC(_iy, N, int); |
| 132 | ALLOC(_iny, N, int); |
| Jean-Marc Valin | 4913438 | 2008-03-25 16:07:05 +1100 | [diff] [blame^] | 133 | ALLOC(signx, N, int); |
| 134 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 135 | y = _y; |
| 136 | ny = _ny; |
| 137 | iy = _iy; |
| 138 | iny = _iny; |
| Jean-Marc Valin | fe41983 | 2008-02-12 15:05:01 +1100 | [diff] [blame] | 139 | |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 140 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 141 | { |
| Jean-Marc Valin | 4913438 | 2008-03-25 16:07:05 +1100 | [diff] [blame^] | 142 | if (X[j]>0) |
| 143 | signx[j]=1; |
| 144 | else |
| 145 | signx[j]=-1; |
| 146 | } |
| 147 | |
| 148 | for (j=0;j<N;j++) |
| 149 | { |
| Jean-Marc Valin | f675adc | 2008-02-28 12:15:17 +1100 | [diff] [blame] | 150 | Rpp = MAC16_16(Rpp, P[j],P[j]); |
| 151 | Rxp = MAC16_16(Rxp, X[j],P[j]); |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 152 | } |
| Jean-Marc Valin | f5b0587 | 2008-03-21 10:46:17 +1100 | [diff] [blame] | 153 | Rpp = ROUND16(Rpp, NORM_SHIFT); |
| 154 | Rxp = ROUND16(Rxp, NORM_SHIFT); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 155 | |
| Jean-Marc Valin | 4ff068e | 2008-03-15 23:34:39 +1100 | [diff] [blame] | 156 | 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] | 157 | |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 158 | for (i=0;i<N;i++) |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 159 | y[i] = 0; |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 160 | for (i=0;i<N;i++) |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 161 | iy[i] = 0; |
| 162 | xy = yy = yp = 0; |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 163 | |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 164 | pulsesLeft = K; |
| 165 | while (pulsesLeft > 0) |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 166 | { |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 167 | int pulsesAtOnce=1; |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 168 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 169 | /* Decide on how many pulses to find at once */ |
| Jean-Marc Valin | cab576e | 2008-02-12 17:21:14 +1100 | [diff] [blame] | 170 | pulsesAtOnce = pulsesLeft/N; |
| 171 | if (pulsesAtOnce<1) |
| 172 | pulsesAtOnce = 1; |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 173 | /*printf ("%d %d %d/%d %d\n", Lupdate, pulsesAtOnce, pulsesLeft, K, N);*/ |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 174 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 175 | nbest.score = -VERY_LARGE32; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 176 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 177 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 178 | { |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 179 | int sign; |
| 180 | /*fprintf (stderr, "%d/%d %d/%d %d/%d\n", i, K, m, L2, j, N);*/ |
| 181 | celt_word32_t Rxy, Ryy, Ryp; |
| 182 | celt_word32_t score; |
| 183 | celt_word32_t g; |
| 184 | celt_word16_t s; |
| 185 | |
| 186 | /* Select sign based on X[j] alone */ |
| Jean-Marc Valin | 4913438 | 2008-03-25 16:07:05 +1100 | [diff] [blame^] | 187 | sign = signx[j]; |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 188 | s = SHL16(sign*pulsesAtOnce, yshift); |
| 189 | |
| 190 | /* Updating the sums of the new pulse(s) */ |
| 191 | Rxy = xy + MULT16_16(s,X[j]); |
| 192 | Ryy = yy + 2*MULT16_16(s,y[j]) + MULT16_16(s,s); |
| 193 | Ryp = yp + MULT16_16(s, P[j]); |
| 194 | |
| 195 | if (pulsesLeft>1) |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 196 | { |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 197 | score = MULT32_32_Q31(MULT16_16(ROUND16(Rxy,14),ABS16(ROUND16(Rxy,14))), celt_rcp(SHR32(Ryy,12))); |
| 198 | } else |
| 199 | { |
| 200 | /* Compute the gain such that ||p + g*y|| = 1 */ |
| 201 | g = MULT16_32_Q15( |
| 202 | celt_sqrt(MULT16_16(ROUND16(Ryp,14),ROUND16(Ryp,14)) + Ryy - |
| 203 | MULT16_16(ROUND16(Ryy,14),Rpp)) |
| 204 | - ROUND16(Ryp,14), |
| 205 | celt_rcp(SHR32(Ryy,12))); |
| 206 | /* Knowing that gain, what's the error: (x-g*y)^2 |
| 207 | (result is negated and we discard x^2 because it's constant) */ |
| 208 | /* score = 2.f*g*Rxy - 1.f*g*g*Ryy*NORM_SCALING_1;*/ |
| 209 | score = 2*MULT16_32_Q14(ROUND16(Rxy,14),g) |
| 210 | - MULT16_32_Q14(EXTRACT16(MULT16_32_Q14(ROUND16(Ryy,14),g)),g); |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 211 | } |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 212 | |
| 213 | if (score>nbest.score) |
| 214 | { |
| 215 | nbest.score = score; |
| 216 | nbest.pos = j; |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 217 | nbest.sign = sign; |
| 218 | nbest.xy = Rxy; |
| 219 | nbest.yy = Ryy; |
| 220 | nbest.yp = Ryp; |
| 221 | } |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 222 | } |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 223 | |
| Jean-Marc Valin | 4913438 | 2008-03-25 16:07:05 +1100 | [diff] [blame^] | 224 | celt_assert2(nbest.score > -VERY_LARGE32, "Could not find any match in VQ codebook. Something got corrupted somewhere."); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 225 | |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 226 | /* Only now that we've made the final choice, update ny/iny and others */ |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 227 | { |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 228 | int n; |
| 229 | int is; |
| Jean-Marc Valin | 8b158f5 | 2008-02-28 14:44:19 +1100 | [diff] [blame] | 230 | celt_norm_t s; |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 231 | is = nbest.sign*pulsesAtOnce; |
| Jean-Marc Valin | f675adc | 2008-02-28 12:15:17 +1100 | [diff] [blame] | 232 | s = SHL16(is, yshift); |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 233 | for (n=0;n<N;n++) |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 234 | ny[n] = y[n]; |
| 235 | ny[nbest.pos] += s; |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 236 | |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 237 | for (n=0;n<N;n++) |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 238 | iny[n] = iy[n]; |
| 239 | iny[nbest.pos] += is; |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 240 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 241 | xy = nbest.xy; |
| 242 | yy = nbest.yy; |
| 243 | yp = nbest.yp; |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 244 | } |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 245 | /* Swap ny/iny with y/iy */ |
| Jean-Marc Valin | 3b277dc | 2008-02-14 16:46:50 +1100 | [diff] [blame] | 246 | { |
| Jean-Marc Valin | 8b158f5 | 2008-02-28 14:44:19 +1100 | [diff] [blame] | 247 | celt_norm_t *tmp_ny; |
| Jean-Marc Valin | fe41983 | 2008-02-12 15:05:01 +1100 | [diff] [blame] | 248 | int *tmp_iny; |
| 249 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 250 | tmp_ny = ny; |
| 251 | ny = y; |
| 252 | y = tmp_ny; |
| 253 | tmp_iny = iny; |
| 254 | iny = iy; |
| 255 | iy = tmp_iny; |
| 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 | pulsesLeft -= pulsesAtOnce; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 258 | } |
| 259 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 260 | encode_pulses(iy, N, K, enc); |
| Jean-Marc Valin | 5fa5995 | 2008-02-14 13:50:44 +1100 | [diff] [blame] | 261 | |
| Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 262 | /* 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] | 263 | due to the recursive computation used in quantisation. */ |
| 264 | mix_pitch_and_residual(iy, X, N, K, P); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 265 | RESTORE_STACK; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 266 | } |
| 267 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 268 | |
| Jean-Marc Valin | 879fbfd | 2008-02-20 17:17:13 +1100 | [diff] [blame] | 269 | /** Decode pulse vector and combine the result with the pitch vector to produce |
| 270 | the final normalised signal in the current band. */ |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 271 | void alg_unquant(celt_norm_t *X, int N, int K, celt_norm_t *P, ec_dec *dec) |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 272 | { |
| Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 273 | VARDECL(int, iy); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 274 | SAVE_STACK; |
| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 275 | ALLOC(iy, N, int); |
| Jean-Marc Valin | 5fa5995 | 2008-02-14 13:50:44 +1100 | [diff] [blame] | 276 | decode_pulses(iy, N, K, dec); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 277 | mix_pitch_and_residual(iy, X, N, K, P); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 278 | RESTORE_STACK; |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 279 | } |
| 280 | |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 281 | #ifdef FIXED_POINT |
| 282 | static const celt_word16_t pg[11] = {32767, 24576, 21299, 19661, 19661, 19661, 18022, 18022, 16384, 16384, 16384}; |
| 283 | #else |
| Jean-Marc Valin | 3e65097 | 2008-03-07 17:38:58 +1100 | [diff] [blame] | 284 | static const celt_word16_t pg[11] = {1.f, .75f, .65f, 0.6f, 0.6f, .6f, .55f, .55f, .5f, .5f, .5f}; |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 285 | #endif |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 286 | |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 287 | #define MAX_INTRA 32 |
| 288 | #define LOG_MAX_INTRA 5 |
| 289 | |
| Jean-Marc Valin | 5f09ea5 | 2008-02-26 16:43:04 +1100 | [diff] [blame] | 290 | 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] | 291 | { |
| 292 | int i,j; |
| 293 | int best=0; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 294 | celt_word32_t best_score=0; |
| 295 | celt_word16_t s = 1; |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 296 | int sign; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 297 | celt_word32_t E; |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 298 | celt_word16_t pred_gain; |
| Jean-Marc Valin | 8f0f4b9 | 2008-02-11 13:52:44 +1100 | [diff] [blame] | 299 | int max_pos = N0-N/B; |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 300 | if (max_pos > MAX_INTRA) |
| 301 | max_pos = MAX_INTRA; |
| Jean-Marc Valin | 8f0f4b9 | 2008-02-11 13:52:44 +1100 | [diff] [blame] | 302 | |
| 303 | for (i=0;i<max_pos*B;i+=B) |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 304 | { |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 305 | celt_word32_t xy=0, yy=0; |
| Jean-Marc Valin | 03892c1 | 2008-03-07 17:25:47 +1100 | [diff] [blame] | 306 | celt_word32_t score; |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 307 | /* If this doesn't generate a double-MAC on supported architectures, |
| 308 | complain to your compilor vendor */ |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 309 | for (j=0;j<N;j++) |
| 310 | { |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 311 | xy = MAC16_16(xy, x[j], Y[i+N-j-1]); |
| 312 | yy = MAC16_16(yy, Y[i+N-j-1], Y[i+N-j-1]); |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 313 | } |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 314 | /* If you're really desperate for speed, just use xy as the score */ |
| Jean-Marc Valin | 2282383 | 2008-03-22 21:17:45 +1100 | [diff] [blame] | 315 | score = celt_div(MULT16_16(ROUND16(xy,14),ROUND16(xy,14)), ROUND16(yy,14)); |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 316 | if (score > best_score) |
| 317 | { |
| 318 | best_score = score; |
| 319 | best = i; |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 320 | /* Store xy as the sign. We'll normalise it to +/- 1 later. */ |
| 321 | s = ROUND16(xy,14); |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 322 | } |
| 323 | } |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 324 | if (s<0) |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 325 | { |
| 326 | s = -1; |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 327 | sign = 1; |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 328 | } else { |
| 329 | s = 1; |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 330 | sign = 0; |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 331 | } |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 332 | /*printf ("%d %d ", sign, best);*/ |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 333 | ec_enc_bits(enc,sign,1); |
| 334 | if (max_pos == MAX_INTRA) |
| 335 | ec_enc_bits(enc,best/B,LOG_MAX_INTRA); |
| 336 | else |
| 337 | ec_enc_uint(enc,best/B,max_pos); |
| 338 | |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 339 | /*printf ("%d %f\n", best, best_score);*/ |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 340 | |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 341 | if (K>10) |
| 342 | pred_gain = pg[10]; |
| 343 | else |
| 344 | pred_gain = pg[K]; |
| Jean-Marc Valin | 03892c1 | 2008-03-07 17:25:47 +1100 | [diff] [blame] | 345 | E = EPSILON; |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 346 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 347 | { |
| Jean-Marc Valin | d501f61 | 2008-02-21 12:16:57 +1100 | [diff] [blame] | 348 | P[j] = s*Y[best+N-j-1]; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 349 | E = MAC16_16(E, P[j],P[j]); |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 350 | } |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 351 | /*pred_gain = pred_gain/sqrt(E);*/ |
| Jean-Marc Valin | 23e82b2 | 2008-03-24 08:15:40 +1100 | [diff] [blame] | 352 | pred_gain = MULT16_16_Q15(pred_gain,celt_rcp(SHL32(celt_sqrt(E),9))); |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 353 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 354 | P[j] = PSHR32(MULT16_16(pred_gain, P[j]),8); |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 355 | if (K>0) |
| 356 | { |
| 357 | for (j=0;j<N;j++) |
| 358 | x[j] -= P[j]; |
| 359 | } else { |
| 360 | for (j=0;j<N;j++) |
| 361 | x[j] = P[j]; |
| 362 | } |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 363 | /*printf ("quant ");*/ |
| 364 | /*for (j=0;j<N;j++) printf ("%f ", P[j]);*/ |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 365 | |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 366 | } |
| Jean-Marc Valin | fc08d0a | 2007-12-07 13:26:15 +1100 | [diff] [blame] | 367 | |
| Jean-Marc Valin | 18ddc02 | 2008-02-22 14:24:50 +1100 | [diff] [blame] | 368 | 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] | 369 | { |
| Jean-Marc Valin | 11f0172 | 2007-12-09 01:19:36 +1100 | [diff] [blame] | 370 | int j; |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 371 | int sign; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 372 | celt_word16_t s; |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 373 | int best; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 374 | celt_word32_t E; |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 375 | celt_word16_t pred_gain; |
| Jean-Marc Valin | 8f0f4b9 | 2008-02-11 13:52:44 +1100 | [diff] [blame] | 376 | int max_pos = N0-N/B; |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 377 | if (max_pos > MAX_INTRA) |
| 378 | max_pos = MAX_INTRA; |
| Jean-Marc Valin | 8f0f4b9 | 2008-02-11 13:52:44 +1100 | [diff] [blame] | 379 | |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 380 | sign = ec_dec_bits(dec, 1); |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 381 | if (sign == 0) |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 382 | s = 1; |
| 383 | else |
| 384 | s = -1; |
| 385 | |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 386 | if (max_pos == MAX_INTRA) |
| 387 | best = B*ec_dec_bits(dec, LOG_MAX_INTRA); |
| 388 | else |
| 389 | best = B*ec_dec_uint(dec, max_pos); |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 390 | /*printf ("%d %d ", sign, best);*/ |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 391 | |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 392 | if (K>10) |
| 393 | pred_gain = pg[10]; |
| 394 | else |
| 395 | pred_gain = pg[K]; |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 396 | E = EPSILON; |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 397 | for (j=0;j<N;j++) |
| 398 | { |
| Jean-Marc Valin | d501f61 | 2008-02-21 12:16:57 +1100 | [diff] [blame] | 399 | P[j] = s*Y[best+N-j-1]; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 400 | E = MAC16_16(E, P[j],P[j]); |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 401 | } |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 402 | /*pred_gain = pred_gain/sqrt(E);*/ |
| Jean-Marc Valin | 23e82b2 | 2008-03-24 08:15:40 +1100 | [diff] [blame] | 403 | pred_gain = MULT16_16_Q15(pred_gain,celt_rcp(SHL32(celt_sqrt(E),9))); |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 404 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 405 | P[j] = PSHR32(MULT16_16(pred_gain, P[j]),8); |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 406 | if (K==0) |
| 407 | { |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 408 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 409 | x[j] = P[j]; |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 410 | } |
| 411 | } |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 412 | |
| Jean-Marc Valin | 18ddc02 | 2008-02-22 14:24:50 +1100 | [diff] [blame] | 413 | 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] | 414 | { |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 415 | int i, j; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 416 | celt_word32_t E; |
| Jean-Marc Valin | ec9b6df | 2008-03-07 17:05:47 +1100 | [diff] [blame] | 417 | celt_word16_t g; |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 418 | |
| Jean-Marc Valin | ec9b6df | 2008-03-07 17:05:47 +1100 | [diff] [blame] | 419 | E = EPSILON; |
| Jean-Marc Valin | a536f77 | 2008-03-22 09:01:50 +1100 | [diff] [blame] | 420 | if (N0 >= (Nmax>>1)) |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 421 | { |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 422 | for (i=0;i<B;i++) |
| 423 | { |
| 424 | for (j=0;j<N/B;j++) |
| 425 | { |
| 426 | P[j*B+i] = Y[(Nmax-N0-j-1)*B+i]; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 427 | E += P[j*B+i]*P[j*B+i]; |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | } else { |
| 431 | for (j=0;j<N;j++) |
| 432 | { |
| 433 | P[j] = Y[j]; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 434 | E = MAC16_16(E, P[j],P[j]); |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 435 | } |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 436 | } |
| Jean-Marc Valin | 23e82b2 | 2008-03-24 08:15:40 +1100 | [diff] [blame] | 437 | g = celt_rcp(SHL32(celt_sqrt(E),9)); |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 438 | for (j=0;j<N;j++) |
| Jean-Marc Valin | ec9b6df | 2008-03-07 17:05:47 +1100 | [diff] [blame] | 439 | P[j] = PSHR32(MULT16_16(g, P[j]),8); |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 440 | for (j=0;j<N;j++) |
| 441 | x[j] = P[j]; |
| 442 | } |
| 443 | |