| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 1 | /* (C) 2007-2008 Jean-Marc Valin, CSIRO |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 2 | */ |
| 3 | /* |
| 4 | Redistribution and use in source and binary forms, with or without |
| 5 | modification, are permitted provided that the following conditions |
| 6 | are met: |
| 7 | |
| 8 | - Redistributions of source code must retain the above copyright |
| 9 | notice, this list of conditions and the following disclaimer. |
| 10 | |
| 11 | - Redistributions in binary form must reproduce the above copyright |
| 12 | notice, this list of conditions and the following disclaimer in the |
| 13 | documentation and/or other materials provided with the distribution. |
| 14 | |
| 15 | - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | contributors may be used to endorse or promote products derived from |
| 17 | this software without specific prior written permission. |
| 18 | |
| 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| Jean-Marc Valin | 02fa913 | 2008-02-20 12:09:29 +1100 | [diff] [blame] | 32 | #ifdef HAVE_CONFIG_H |
| 33 | #include "config.h" |
| 34 | #endif |
| 35 | |
| Jean-Marc Valin | 3ca9b1d | 2008-02-27 23:50:31 +1100 | [diff] [blame] | 36 | #include "mathops.h" |
| Jean-Marc Valin | 29ccab8 | 2007-12-06 15:39:38 +1100 | [diff] [blame] | 37 | #include "cwrs.h" |
| Jean-Marc Valin | 9cace64 | 2007-12-06 17:44:09 +1100 | [diff] [blame] | 38 | #include "vq.h" |
| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 39 | #include "arch.h" |
| Jean-Marc Valin | b60340f | 2008-02-26 15:41:51 +1100 | [diff] [blame] | 40 | #include "os_support.h" |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 41 | |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 42 | /** Takes the pitch vector and the decoded residual vector, computes the gain |
| 43 | that will give ||p+g*y||=1 and mixes the residual with the pitch. */ |
| Jean-Marc Valin | 5de868c | 2008-03-25 22:38:58 +1100 | [diff] [blame] | 44 | static void mix_pitch_and_residual(int * restrict iy, celt_norm_t * restrict X, int N, int K, const celt_norm_t * restrict P) |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 45 | { |
| 46 | int i; |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 47 | celt_word32_t Ryp, Ryy, Rpp; |
| Jean-Marc Valin | a847b77 | 2008-02-27 17:46:49 +1100 | [diff] [blame] | 48 | celt_word32_t g; |
| Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 49 | VARDECL(celt_norm_t, y); |
| Jean-Marc Valin | d9de593 | 2008-03-05 08:11:57 +1100 | [diff] [blame] | 50 | #ifdef FIXED_POINT |
| 51 | int yshift; |
| 52 | #endif |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 53 | SAVE_STACK; |
| Jean-Marc Valin | d17edd3 | 2008-02-27 16:52:30 +1100 | [diff] [blame] | 54 | #ifdef FIXED_POINT |
| Jean-Marc Valin | d9de593 | 2008-03-05 08:11:57 +1100 | [diff] [blame] | 55 | yshift = 14-EC_ILOG(K); |
| Jean-Marc Valin | d17edd3 | 2008-02-27 16:52:30 +1100 | [diff] [blame] | 56 | #endif |
| 57 | ALLOC(y, N, celt_norm_t); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 58 | |
| 59 | /*for (i=0;i<N;i++) |
| 60 | printf ("%d ", iy[i]);*/ |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 61 | Rpp = 0; |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 62 | for (i=0;i<N;i++) |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 63 | Rpp = MAC16_16(Rpp,P[i],P[i]); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 64 | |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 65 | for (i=0;i<N;i++) |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 66 | y[i] = SHL16(iy[i],yshift); |
| Jean-Marc Valin | 95088d4 | 2008-03-26 17:57:49 +1100 | [diff] [blame^] | 67 | |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 68 | Ryp = 0; |
| 69 | for (i=0;i<N;i++) |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 70 | Ryp = MAC16_16(Ryp,y[i],P[i]); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 71 | |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 72 | Ryy = 0; |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 73 | for (i=0;i<N;i++) |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 74 | Ryy = MAC16_16(Ryy, y[i],y[i]); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 75 | |
| Jean-Marc Valin | 1ca0722 | 2008-02-27 17:23:04 +1100 | [diff] [blame] | 76 | /* g = (sqrt(Ryp^2 + Ryy - Rpp*Ryy)-Ryp)/Ryy */ |
| Jean-Marc Valin | 9d5b4a6 | 2008-03-13 11:36:45 +1100 | [diff] [blame] | 77 | g = MULT16_32_Q15( |
| Jean-Marc Valin | f5b0587 | 2008-03-21 10:46:17 +1100 | [diff] [blame] | 78 | celt_sqrt(MULT16_16(ROUND16(Ryp,14),ROUND16(Ryp,14)) + Ryy - |
| 79 | MULT16_16(ROUND16(Ryy,14),ROUND16(Rpp,14))) |
| 80 | - ROUND16(Ryp,14), |
| Jean-Marc Valin | 9d5b4a6 | 2008-03-13 11:36:45 +1100 | [diff] [blame] | 81 | celt_rcp(SHR32(Ryy,9))); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 82 | |
| 83 | for (i=0;i<N;i++) |
| Jean-Marc Valin | f5b0587 | 2008-03-21 10:46:17 +1100 | [diff] [blame] | 84 | X[i] = P[i] + ROUND16(MULT16_16(y[i], g),11); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 85 | RESTORE_STACK; |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 86 | } |
| 87 | |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 88 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 89 | 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] | 90 | { |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 91 | VARDECL(celt_norm_t, y); |
| 92 | VARDECL(int, iy); |
| Jean-Marc Valin | 4913438 | 2008-03-25 16:07:05 +1100 | [diff] [blame] | 93 | VARDECL(int, signx); |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 94 | int i, j, is; |
| 95 | celt_word16_t s; |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 96 | int pulsesLeft; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 97 | celt_word32_t sum; |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 98 | celt_word32_t xy, yy, yp; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 99 | celt_word16_t Rpp; |
| Jean-Marc Valin | f675adc | 2008-02-28 12:15:17 +1100 | [diff] [blame] | 100 | #ifdef FIXED_POINT |
| Jean-Marc Valin | d748cd5 | 2008-03-01 07:27:03 +1100 | [diff] [blame] | 101 | int yshift; |
| 102 | #endif |
| 103 | SAVE_STACK; |
| 104 | |
| 105 | #ifdef FIXED_POINT |
| 106 | yshift = 14-EC_ILOG(K); |
| Jean-Marc Valin | f675adc | 2008-02-28 12:15:17 +1100 | [diff] [blame] | 107 | #endif |
| Jean-Marc Valin | 9d8d9b3 | 2008-02-27 16:17:39 +1100 | [diff] [blame] | 108 | |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 109 | ALLOC(y, N, celt_norm_t); |
| 110 | ALLOC(iy, N, int); |
| Jean-Marc Valin | 4913438 | 2008-03-25 16:07:05 +1100 | [diff] [blame] | 111 | ALLOC(signx, N, int); |
| 112 | |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 113 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 114 | { |
| Jean-Marc Valin | 4913438 | 2008-03-25 16:07:05 +1100 | [diff] [blame] | 115 | if (X[j]>0) |
| 116 | signx[j]=1; |
| 117 | else |
| 118 | signx[j]=-1; |
| 119 | } |
| 120 | |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 121 | sum = 0; |
| Jean-Marc Valin | 4913438 | 2008-03-25 16:07:05 +1100 | [diff] [blame] | 122 | for (j=0;j<N;j++) |
| 123 | { |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 124 | sum = MAC16_16(sum, P[j],P[j]); |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 125 | } |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 126 | Rpp = ROUND16(sum, NORM_SHIFT); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 127 | |
| Jean-Marc Valin | 4ff068e | 2008-03-15 23:34:39 +1100 | [diff] [blame] | 128 | 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] | 129 | |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 130 | for (i=0;i<N;i++) |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 131 | y[i] = 0; |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 132 | for (i=0;i<N;i++) |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 133 | iy[i] = 0; |
| 134 | xy = yy = yp = 0; |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 135 | |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 136 | pulsesLeft = K; |
| 137 | while (pulsesLeft > 0) |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 138 | { |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 139 | int pulsesAtOnce=1; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 140 | int sign; |
| 141 | celt_word32_t Rxy, Ryy, Ryp; |
| 142 | celt_word32_t g; |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 143 | celt_word32_t best_num; |
| 144 | celt_word16_t best_den; |
| 145 | int best_id; |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 146 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 147 | /* Decide on how many pulses to find at once */ |
| Jean-Marc Valin | cab576e | 2008-02-12 17:21:14 +1100 | [diff] [blame] | 148 | pulsesAtOnce = pulsesLeft/N; |
| 149 | if (pulsesAtOnce<1) |
| 150 | pulsesAtOnce = 1; |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 151 | |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 152 | /* This should ensure that anything we can process will have a better score */ |
| 153 | best_num = -SHR32(VERY_LARGE32,4); |
| 154 | best_den = 0; |
| 155 | best_id = 0; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 156 | /* Choose between fast and accurate strategy depending on where we are in the search */ |
| 157 | if (pulsesLeft>1) |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 158 | { |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 159 | for (j=0;j<N;j++) |
| 160 | { |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 161 | celt_word32_t num; |
| 162 | celt_word16_t den; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 163 | /* Select sign based on X[j] alone */ |
| 164 | sign = signx[j]; |
| 165 | s = SHL16(sign*pulsesAtOnce, yshift); |
| 166 | /* Temporary sums of the new pulse(s) */ |
| 167 | Rxy = xy + MULT16_16(s,X[j]); |
| 168 | Ryy = yy + 2*MULT16_16(s,y[j]) + MULT16_16(s,s); |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 169 | |
| 170 | /* Approximate score: we maximise Rxy/sqrt(Ryy) */ |
| 171 | num = MULT16_16(ROUND16(Rxy,14),ABS16(ROUND16(Rxy,14))); |
| 172 | den = ROUND16(Ryy,14); |
| 173 | /* The idea is to check for num/den >= best_num/best_den, but that way |
| 174 | we can do it without any division */ |
| Jean-Marc Valin | 233e317 | 2008-03-26 15:46:51 +1100 | [diff] [blame] | 175 | if (MULT16_32_Q15(best_den, num) > MULT16_32_Q15(den, best_num)) |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 176 | { |
| 177 | best_den = den; |
| 178 | best_num = num; |
| 179 | best_id = j; |
| 180 | } |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 181 | } |
| 182 | } else { |
| 183 | for (j=0;j<N;j++) |
| 184 | { |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 185 | celt_word32_t num; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 186 | /* Select sign based on X[j] alone */ |
| 187 | sign = signx[j]; |
| 188 | s = SHL16(sign*pulsesAtOnce, yshift); |
| 189 | /* Temporary sums of the new pulse(s) */ |
| 190 | Rxy = xy + MULT16_16(s,X[j]); |
| 191 | Ryy = yy + 2*MULT16_16(s,y[j]) + MULT16_16(s,s); |
| 192 | Ryp = yp + MULT16_16(s, P[j]); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 193 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 194 | /* Compute the gain such that ||p + g*y|| = 1 */ |
| 195 | g = MULT16_32_Q15( |
| 196 | celt_sqrt(MULT16_16(ROUND16(Ryp,14),ROUND16(Ryp,14)) + Ryy - |
| 197 | MULT16_16(ROUND16(Ryy,14),Rpp)) |
| 198 | - ROUND16(Ryp,14), |
| 199 | celt_rcp(SHR32(Ryy,12))); |
| 200 | /* Knowing that gain, what's the error: (x-g*y)^2 |
| 201 | (result is negated and we discard x^2 because it's constant) */ |
| 202 | /* score = 2.f*g*Rxy - 1.f*g*g*Ryy*NORM_SCALING_1;*/ |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 203 | num = 2*MULT16_32_Q14(ROUND16(Rxy,14),g) |
| 204 | - MULT16_32_Q14(EXTRACT16(MULT16_32_Q14(ROUND16(Ryy,14),g)),g); |
| 205 | if (num >= best_num) |
| 206 | { |
| 207 | best_num = num; |
| 208 | best_id = j; |
| 209 | } |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 210 | } |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 211 | } |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 212 | |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 213 | j = best_id; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 214 | is = signx[j]*pulsesAtOnce; |
| 215 | s = SHL16(is, yshift); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 216 | |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 217 | /* Updating the sums of the new pulse(s) */ |
| 218 | xy = xy + MULT16_16(s,X[j]); |
| 219 | yy = yy + 2*MULT16_16(s,y[j]) + MULT16_16(s,s); |
| 220 | yp = yp + MULT16_16(s, P[j]); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 221 | |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 222 | /* Only now that we've made the final choice, update y/iy */ |
| 223 | y[j] += s; |
| 224 | iy[j] += is; |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 225 | pulsesLeft -= pulsesAtOnce; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 226 | } |
| 227 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 228 | encode_pulses(iy, N, K, enc); |
| Jean-Marc Valin | 5fa5995 | 2008-02-14 13:50:44 +1100 | [diff] [blame] | 229 | |
| Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 230 | /* 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] | 231 | due to the recursive computation used in quantisation. */ |
| 232 | mix_pitch_and_residual(iy, X, N, K, P); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 233 | RESTORE_STACK; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 234 | } |
| 235 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 236 | |
| Jean-Marc Valin | 879fbfd | 2008-02-20 17:17:13 +1100 | [diff] [blame] | 237 | /** Decode pulse vector and combine the result with the pitch vector to produce |
| 238 | the final normalised signal in the current band. */ |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 239 | 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] | 240 | { |
| Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 241 | VARDECL(int, iy); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 242 | SAVE_STACK; |
| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 243 | ALLOC(iy, N, int); |
| Jean-Marc Valin | 5fa5995 | 2008-02-14 13:50:44 +1100 | [diff] [blame] | 244 | decode_pulses(iy, N, K, dec); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 245 | mix_pitch_and_residual(iy, X, N, K, P); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 246 | RESTORE_STACK; |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 247 | } |
| 248 | |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 249 | #ifdef FIXED_POINT |
| 250 | static const celt_word16_t pg[11] = {32767, 24576, 21299, 19661, 19661, 19661, 18022, 18022, 16384, 16384, 16384}; |
| 251 | #else |
| Jean-Marc Valin | 3e65097 | 2008-03-07 17:38:58 +1100 | [diff] [blame] | 252 | 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] | 253 | #endif |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 254 | |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 255 | #define MAX_INTRA 32 |
| 256 | #define LOG_MAX_INTRA 5 |
| 257 | |
| Jean-Marc Valin | 5de868c | 2008-03-25 22:38:58 +1100 | [diff] [blame] | 258 | void intra_prediction(celt_norm_t *x, celt_mask_t *W, int N, int K, celt_norm_t *Y, celt_norm_t * restrict P, int B, int N0, ec_enc *enc) |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 259 | { |
| 260 | int i,j; |
| 261 | int best=0; |
| Jean-Marc Valin | 89c5fd1 | 2008-03-26 12:16:00 +1100 | [diff] [blame] | 262 | celt_word32_t best_num=-SHR32(VERY_LARGE32,4); |
| 263 | celt_word16_t best_den=0; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 264 | celt_word16_t s = 1; |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 265 | int sign; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 266 | celt_word32_t E; |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 267 | celt_word16_t pred_gain; |
| Jean-Marc Valin | 8f0f4b9 | 2008-02-11 13:52:44 +1100 | [diff] [blame] | 268 | int max_pos = N0-N/B; |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 269 | if (max_pos > MAX_INTRA) |
| 270 | max_pos = MAX_INTRA; |
| Jean-Marc Valin | 8f0f4b9 | 2008-02-11 13:52:44 +1100 | [diff] [blame] | 271 | |
| 272 | for (i=0;i<max_pos*B;i+=B) |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 273 | { |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 274 | celt_word32_t xy=0, yy=0; |
| Jean-Marc Valin | 89c5fd1 | 2008-03-26 12:16:00 +1100 | [diff] [blame] | 275 | celt_word32_t num; |
| 276 | celt_word16_t den; |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 277 | /* If this doesn't generate a double-MAC on supported architectures, |
| 278 | complain to your compilor vendor */ |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 279 | for (j=0;j<N;j++) |
| 280 | { |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 281 | xy = MAC16_16(xy, x[j], Y[i+N-j-1]); |
| 282 | 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] | 283 | } |
| Jean-Marc Valin | 89c5fd1 | 2008-03-26 12:16:00 +1100 | [diff] [blame] | 284 | /* Using xy^2/yy as the score but without having to do the division */ |
| 285 | num = MULT16_16(ROUND16(xy,14),ROUND16(xy,14)); |
| 286 | den = ROUND16(yy,14); |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 287 | /* If you're really desperate for speed, just use xy as the score */ |
| Jean-Marc Valin | 89c5fd1 | 2008-03-26 12:16:00 +1100 | [diff] [blame] | 288 | if (MULT16_32_Q15(best_den, num) > MULT16_32_Q15(den, best_num)) |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 289 | { |
| Jean-Marc Valin | 89c5fd1 | 2008-03-26 12:16:00 +1100 | [diff] [blame] | 290 | best_num = num; |
| 291 | best_den = den; |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 292 | best = i; |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 293 | /* Store xy as the sign. We'll normalise it to +/- 1 later. */ |
| 294 | s = ROUND16(xy,14); |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 295 | } |
| 296 | } |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 297 | if (s<0) |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 298 | { |
| 299 | s = -1; |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 300 | sign = 1; |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 301 | } else { |
| 302 | s = 1; |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 303 | sign = 0; |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 304 | } |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 305 | /*printf ("%d %d ", sign, best);*/ |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 306 | ec_enc_bits(enc,sign,1); |
| 307 | if (max_pos == MAX_INTRA) |
| 308 | ec_enc_bits(enc,best/B,LOG_MAX_INTRA); |
| 309 | else |
| 310 | ec_enc_uint(enc,best/B,max_pos); |
| 311 | |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 312 | /*printf ("%d %f\n", best, best_score);*/ |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 313 | |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 314 | if (K>10) |
| 315 | pred_gain = pg[10]; |
| 316 | else |
| 317 | pred_gain = pg[K]; |
| Jean-Marc Valin | 03892c1 | 2008-03-07 17:25:47 +1100 | [diff] [blame] | 318 | E = EPSILON; |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 319 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 320 | { |
| Jean-Marc Valin | d501f61 | 2008-02-21 12:16:57 +1100 | [diff] [blame] | 321 | P[j] = s*Y[best+N-j-1]; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 322 | E = MAC16_16(E, P[j],P[j]); |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 323 | } |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 324 | /*pred_gain = pred_gain/sqrt(E);*/ |
| Jean-Marc Valin | 23e82b2 | 2008-03-24 08:15:40 +1100 | [diff] [blame] | 325 | 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] | 326 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 327 | P[j] = PSHR32(MULT16_16(pred_gain, P[j]),8); |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 328 | if (K>0) |
| 329 | { |
| 330 | for (j=0;j<N;j++) |
| 331 | x[j] -= P[j]; |
| 332 | } else { |
| 333 | for (j=0;j<N;j++) |
| 334 | x[j] = P[j]; |
| 335 | } |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 336 | /*printf ("quant ");*/ |
| 337 | /*for (j=0;j<N;j++) printf ("%f ", P[j]);*/ |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 338 | |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 339 | } |
| Jean-Marc Valin | fc08d0a | 2007-12-07 13:26:15 +1100 | [diff] [blame] | 340 | |
| Jean-Marc Valin | 5de868c | 2008-03-25 22:38:58 +1100 | [diff] [blame] | 341 | void intra_unquant(celt_norm_t *x, int N, int K, celt_norm_t *Y, celt_norm_t * restrict P, int B, int N0, ec_dec *dec) |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 342 | { |
| Jean-Marc Valin | 11f0172 | 2007-12-09 01:19:36 +1100 | [diff] [blame] | 343 | int j; |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 344 | int sign; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 345 | celt_word16_t s; |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 346 | int best; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 347 | celt_word32_t E; |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 348 | celt_word16_t pred_gain; |
| Jean-Marc Valin | 8f0f4b9 | 2008-02-11 13:52:44 +1100 | [diff] [blame] | 349 | int max_pos = N0-N/B; |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 350 | if (max_pos > MAX_INTRA) |
| 351 | max_pos = MAX_INTRA; |
| Jean-Marc Valin | 8f0f4b9 | 2008-02-11 13:52:44 +1100 | [diff] [blame] | 352 | |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 353 | sign = ec_dec_bits(dec, 1); |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 354 | if (sign == 0) |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 355 | s = 1; |
| 356 | else |
| 357 | s = -1; |
| 358 | |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 359 | if (max_pos == MAX_INTRA) |
| 360 | best = B*ec_dec_bits(dec, LOG_MAX_INTRA); |
| 361 | else |
| 362 | best = B*ec_dec_uint(dec, max_pos); |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 363 | /*printf ("%d %d ", sign, best);*/ |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 364 | |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 365 | if (K>10) |
| 366 | pred_gain = pg[10]; |
| 367 | else |
| 368 | pred_gain = pg[K]; |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 369 | E = EPSILON; |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 370 | for (j=0;j<N;j++) |
| 371 | { |
| Jean-Marc Valin | d501f61 | 2008-02-21 12:16:57 +1100 | [diff] [blame] | 372 | P[j] = s*Y[best+N-j-1]; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 373 | E = MAC16_16(E, P[j],P[j]); |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 374 | } |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 375 | /*pred_gain = pred_gain/sqrt(E);*/ |
| Jean-Marc Valin | 23e82b2 | 2008-03-24 08:15:40 +1100 | [diff] [blame] | 376 | 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] | 377 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 378 | P[j] = PSHR32(MULT16_16(pred_gain, P[j]),8); |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 379 | if (K==0) |
| 380 | { |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 381 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 382 | x[j] = P[j]; |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 383 | } |
| 384 | } |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 385 | |
| Jean-Marc Valin | 5de868c | 2008-03-25 22:38:58 +1100 | [diff] [blame] | 386 | void intra_fold(celt_norm_t *x, int N, celt_norm_t *Y, celt_norm_t * restrict P, int B, int N0, int Nmax) |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 387 | { |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 388 | int i, j; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 389 | celt_word32_t E; |
| Jean-Marc Valin | ec9b6df | 2008-03-07 17:05:47 +1100 | [diff] [blame] | 390 | celt_word16_t g; |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 391 | |
| Jean-Marc Valin | ec9b6df | 2008-03-07 17:05:47 +1100 | [diff] [blame] | 392 | E = EPSILON; |
| Jean-Marc Valin | a536f77 | 2008-03-22 09:01:50 +1100 | [diff] [blame] | 393 | if (N0 >= (Nmax>>1)) |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 394 | { |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 395 | for (i=0;i<B;i++) |
| 396 | { |
| 397 | for (j=0;j<N/B;j++) |
| 398 | { |
| 399 | P[j*B+i] = Y[(Nmax-N0-j-1)*B+i]; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 400 | E += P[j*B+i]*P[j*B+i]; |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } else { |
| 404 | for (j=0;j<N;j++) |
| 405 | { |
| 406 | P[j] = Y[j]; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 407 | E = MAC16_16(E, P[j],P[j]); |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 408 | } |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 409 | } |
| Jean-Marc Valin | 23e82b2 | 2008-03-24 08:15:40 +1100 | [diff] [blame] | 410 | g = celt_rcp(SHL32(celt_sqrt(E),9)); |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 411 | for (j=0;j<N;j++) |
| Jean-Marc Valin | ec9b6df | 2008-03-07 17:05:47 +1100 | [diff] [blame] | 412 | P[j] = PSHR32(MULT16_16(g, P[j]),8); |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 413 | for (j=0;j<N;j++) |
| 414 | x[j] = P[j]; |
| 415 | } |
| 416 | |