| 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 | 1dab60c | 2008-09-16 13:29:37 -0400 | [diff] [blame] | 48 | celt_word16_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 | 98c86c7 | 2008-03-27 08:40:45 +1100 | [diff] [blame] | 56 | yshift = 13-celt_ilog2(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 | 6ea8bae | 2008-04-15 08:01:33 +1000 | [diff] [blame] | 63 | i=0; |
| 64 | do { |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 65 | Rpp = MAC16_16(Rpp,P[i],P[i]); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 66 | y[i] = SHL16(iy[i],yshift); |
| Jean-Marc Valin | 6ea8bae | 2008-04-15 08:01:33 +1000 | [diff] [blame] | 67 | } while (++i < N); |
| 68 | |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 69 | Ryp = 0; |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 70 | Ryy = 0; |
| Jean-Marc Valin | df7ab43 | 2008-03-26 18:03:22 +1100 | [diff] [blame] | 71 | /* If this doesn't generate a dual MAC (on supported archs), fire the compiler guy */ |
| Jean-Marc Valin | 6ea8bae | 2008-04-15 08:01:33 +1000 | [diff] [blame] | 72 | i=0; |
| 73 | do { |
| Jean-Marc Valin | df7ab43 | 2008-03-26 18:03:22 +1100 | [diff] [blame] | 74 | Ryp = MAC16_16(Ryp, y[i], P[i]); |
| 75 | Ryy = MAC16_16(Ryy, y[i], y[i]); |
| Jean-Marc Valin | 6ea8bae | 2008-04-15 08:01:33 +1000 | [diff] [blame] | 76 | } while (++i < N); |
| 77 | |
| Jean-Marc Valin | 1dab60c | 2008-09-16 13:29:37 -0400 | [diff] [blame] | 78 | ryp = ROUND16(Ryp,14); |
| 79 | ryy = ROUND16(Ryy,14); |
| 80 | rpp = ROUND16(Rpp,14); |
| Jean-Marc Valin | 1ca0722 | 2008-02-27 17:23:04 +1100 | [diff] [blame] | 81 | /* g = (sqrt(Ryp^2 + Ryy - Rpp*Ryy)-Ryp)/Ryy */ |
| Jean-Marc Valin | 1dab60c | 2008-09-16 13:29:37 -0400 | [diff] [blame] | 82 | g = MULT16_32_Q15(celt_sqrt(MAC16_16(Ryy, ryp,ryp) - MULT16_16(ryy,rpp)) - ryp, |
| 83 | celt_rcp(SHR32(Ryy,9))); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 84 | |
| Jean-Marc Valin | 6ea8bae | 2008-04-15 08:01:33 +1000 | [diff] [blame] | 85 | i=0; |
| 86 | do |
| Jean-Marc Valin | 1dab60c | 2008-09-16 13:29:37 -0400 | [diff] [blame] | 87 | X[i] = ADD16(P[i], ROUND16(MULT16_16(y[i], g),11)); |
| Jean-Marc Valin | 6ea8bae | 2008-04-15 08:01:33 +1000 | [diff] [blame] | 88 | while (++i < N); |
| 89 | |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 90 | RESTORE_STACK; |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 91 | } |
| 92 | |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 93 | |
| Jean-Marc Valin | 6cde5dd | 2008-12-04 21:21:41 -0500 | [diff] [blame] | 94 | void alg_quant(celt_norm_t *X, celt_mask_t *W, int N, int K, celt_norm_t *P, ec_enc *enc) |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 95 | { |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 96 | VARDECL(celt_norm_t, y); |
| 97 | VARDECL(int, iy); |
| Jean-Marc Valin | 1dab60c | 2008-09-16 13:29:37 -0400 | [diff] [blame] | 98 | VARDECL(celt_word16_t, signx); |
| Jean-Marc Valin | 6ea8bae | 2008-04-15 08:01:33 +1000 | [diff] [blame] | 99 | int j, is; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 100 | celt_word16_t s; |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 101 | int pulsesLeft; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 102 | celt_word32_t sum; |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 103 | celt_word32_t xy, yy, yp; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 104 | celt_word16_t Rpp; |
| Jean-Marc Valin | f958477 | 2008-03-27 12:22:44 +1100 | [diff] [blame] | 105 | int N_1; /* Inverse of N, in Q14 format (even for float) */ |
| Jean-Marc Valin | f675adc | 2008-02-28 12:15:17 +1100 | [diff] [blame] | 106 | #ifdef FIXED_POINT |
| Jean-Marc Valin | d748cd5 | 2008-03-01 07:27:03 +1100 | [diff] [blame] | 107 | int yshift; |
| 108 | #endif |
| 109 | SAVE_STACK; |
| 110 | |
| 111 | #ifdef FIXED_POINT |
| Jean-Marc Valin | 98c86c7 | 2008-03-27 08:40:45 +1100 | [diff] [blame] | 112 | yshift = 13-celt_ilog2(K); |
| Jean-Marc Valin | f675adc | 2008-02-28 12:15:17 +1100 | [diff] [blame] | 113 | #endif |
| Jean-Marc Valin | 9d8d9b3 | 2008-02-27 16:17:39 +1100 | [diff] [blame] | 114 | |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 115 | ALLOC(y, N, celt_norm_t); |
| 116 | ALLOC(iy, N, int); |
| Jean-Marc Valin | 1dab60c | 2008-09-16 13:29:37 -0400 | [diff] [blame] | 117 | ALLOC(signx, N, celt_word16_t); |
| Jean-Marc Valin | 124d1cd | 2008-03-28 00:33:04 +1100 | [diff] [blame] | 118 | N_1 = 512/N; |
| Jean-Marc Valin | 3d152a5 | 2008-04-15 07:46:48 +1000 | [diff] [blame] | 119 | |
| 120 | sum = 0; |
| Jean-Marc Valin | dff9b7e | 2008-04-21 11:43:51 +1000 | [diff] [blame] | 121 | j=0; do { |
| Jean-Marc Valin | bf2d648 | 2008-05-23 16:57:34 +1000 | [diff] [blame] | 122 | X[j] -= P[j]; |
| Jean-Marc Valin | 4913438 | 2008-03-25 16:07:05 +1100 | [diff] [blame] | 123 | if (X[j]>0) |
| 124 | signx[j]=1; |
| Jean-Marc Valin | 6cde5dd | 2008-12-04 21:21:41 -0500 | [diff] [blame] | 125 | else { |
| Jean-Marc Valin | 4913438 | 2008-03-25 16:07:05 +1100 | [diff] [blame] | 126 | signx[j]=-1; |
| Jean-Marc Valin | 6cde5dd | 2008-12-04 21:21:41 -0500 | [diff] [blame] | 127 | X[j]=-X[j]; |
| 128 | P[j]=-P[j]; |
| 129 | } |
| Jean-Marc Valin | 3d152a5 | 2008-04-15 07:46:48 +1000 | [diff] [blame] | 130 | iy[j] = 0; |
| 131 | y[j] = 0; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 132 | sum = MAC16_16(sum, P[j],P[j]); |
| Jean-Marc Valin | dff9b7e | 2008-04-21 11:43:51 +1000 | [diff] [blame] | 133 | } while (++j<N); |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 134 | Rpp = ROUND16(sum, NORM_SHIFT); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 135 | |
| Jean-Marc Valin | 4ff068e | 2008-03-15 23:34:39 +1100 | [diff] [blame] | 136 | 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] | 137 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 138 | xy = yy = yp = 0; |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 139 | |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 140 | pulsesLeft = K; |
| Jean-Marc Valin | 8256ed4 | 2008-12-12 20:50:56 -0500 | [diff] [blame] | 141 | |
| 142 | /* Do a pre-search by projecting on the pyramid */ |
| Jean-Marc Valin | a733f08 | 2008-12-04 22:52:26 -0500 | [diff] [blame] | 143 | if (K > (N>>1)) |
| 144 | { |
| Jean-Marc Valin | 8256ed4 | 2008-12-12 20:50:56 -0500 | [diff] [blame] | 145 | celt_word16_t rcp; |
| Gregory Maxwell | 61832f1 | 2008-12-22 18:15:42 -0500 | [diff] [blame] | 146 | sum=0; |
| Jean-Marc Valin | a733f08 | 2008-12-04 22:52:26 -0500 | [diff] [blame] | 147 | j=0; do { |
| 148 | sum += X[j]; |
| 149 | } while (++j<N); |
| Jean-Marc Valin | 6d454d8 | 2009-06-30 10:31:00 -0400 | [diff] [blame^] | 150 | |
| 151 | #ifdef FIXED_POINT |
| 152 | if (sum <= K) |
| 153 | #else |
| 154 | if (sum <= EPSILON) |
| 155 | #endif |
| Jean-Marc Valin | 8256ed4 | 2008-12-12 20:50:56 -0500 | [diff] [blame] | 156 | { |
| 157 | X[0] = 16384; |
| Jean-Marc Valin | 6d454d8 | 2009-06-30 10:31:00 -0400 | [diff] [blame^] | 158 | j=1; do |
| 159 | X[j]=0; |
| 160 | while (++j<N); |
| Jean-Marc Valin | 8256ed4 | 2008-12-12 20:50:56 -0500 | [diff] [blame] | 161 | sum = 16384; |
| 162 | } |
| 163 | /* Do we have sufficient accuracy here? */ |
| 164 | rcp = EXTRACT16(MULT16_32_Q16(K-1, celt_rcp(sum))); |
| 165 | /*rcp = DIV32(SHL32(EXTEND32(K-1),15),EPSILON+sum);*/ |
| 166 | /*printf ("%d (%d %d)\n", rcp, N, K);*/ |
| Jean-Marc Valin | a733f08 | 2008-12-04 22:52:26 -0500 | [diff] [blame] | 167 | j=0; do { |
| Jean-Marc Valin | 09dc5a1 | 2008-12-05 00:28:28 -0500 | [diff] [blame] | 168 | #ifdef FIXED_POINT |
| Jean-Marc Valin | 137241d | 2008-12-06 23:44:55 -0500 | [diff] [blame] | 169 | /* It's really important to round *towards zero* here */ |
| Jean-Marc Valin | 8256ed4 | 2008-12-12 20:50:56 -0500 | [diff] [blame] | 170 | iy[j] = MULT16_16_Q15(X[j],rcp); |
| Jean-Marc Valin | 09dc5a1 | 2008-12-05 00:28:28 -0500 | [diff] [blame] | 171 | #else |
| Jean-Marc Valin | 8256ed4 | 2008-12-12 20:50:56 -0500 | [diff] [blame] | 172 | iy[j] = floor(rcp*X[j]); |
| Jean-Marc Valin | 09dc5a1 | 2008-12-05 00:28:28 -0500 | [diff] [blame] | 173 | #endif |
| Jean-Marc Valin | c7635b4 | 2008-12-04 23:26:32 -0500 | [diff] [blame] | 174 | y[j] = SHL16(iy[j],yshift); |
| 175 | yy = MAC16_16(yy, y[j],y[j]); |
| 176 | xy = MAC16_16(xy, X[j],y[j]); |
| Jean-Marc Valin | a733f08 | 2008-12-04 22:52:26 -0500 | [diff] [blame] | 177 | yp += P[j]*y[j]; |
| Jean-Marc Valin | 09dc5a1 | 2008-12-05 00:28:28 -0500 | [diff] [blame] | 178 | y[j] *= 2; |
| Jean-Marc Valin | a733f08 | 2008-12-04 22:52:26 -0500 | [diff] [blame] | 179 | pulsesLeft -= iy[j]; |
| 180 | } while (++j<N); |
| 181 | } |
| Jean-Marc Valin | 8256ed4 | 2008-12-12 20:50:56 -0500 | [diff] [blame] | 182 | /*if (pulsesLeft > N+2) |
| 183 | printf ("%d / %d (%d)\n", pulsesLeft, K, N);*/ |
| Jean-Marc Valin | 137241d | 2008-12-06 23:44:55 -0500 | [diff] [blame] | 184 | celt_assert2(pulsesLeft>=1, "Allocated too many pulses in the quick pass"); |
| Jean-Marc Valin | 8256ed4 | 2008-12-12 20:50:56 -0500 | [diff] [blame] | 185 | |
| Jean-Marc Valin | 7bb339d | 2008-09-21 21:11:39 -0400 | [diff] [blame] | 186 | while (pulsesLeft > 1) |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 187 | { |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 188 | int pulsesAtOnce=1; |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 189 | int best_id; |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 190 | celt_word16_t magnitude; |
| Jean-Marc Valin | 7bb339d | 2008-09-21 21:11:39 -0400 | [diff] [blame] | 191 | celt_word32_t best_num = -VERY_LARGE16; |
| 192 | celt_word16_t best_den = 0; |
| Jean-Marc Valin | 0bc5f7f | 2008-04-20 17:16:18 +1000 | [diff] [blame] | 193 | #ifdef FIXED_POINT |
| 194 | int rshift; |
| 195 | #endif |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 196 | /* Decide on how many pulses to find at once */ |
| Jean-Marc Valin | 124d1cd | 2008-03-28 00:33:04 +1100 | [diff] [blame] | 197 | pulsesAtOnce = (pulsesLeft*N_1)>>9; /* pulsesLeft/N */ |
| Jean-Marc Valin | cab576e | 2008-02-12 17:21:14 +1100 | [diff] [blame] | 198 | if (pulsesAtOnce<1) |
| 199 | pulsesAtOnce = 1; |
| Jean-Marc Valin | 0bc5f7f | 2008-04-20 17:16:18 +1000 | [diff] [blame] | 200 | #ifdef FIXED_POINT |
| 201 | rshift = yshift+1+celt_ilog2(K-pulsesLeft+pulsesAtOnce); |
| 202 | #endif |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 203 | magnitude = SHL16(pulsesAtOnce, yshift); |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 204 | |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 205 | best_id = 0; |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 206 | /* The squared magnitude term gets added anyway, so we might as well |
| 207 | add it outside the loop */ |
| Jean-Marc Valin | 1dab60c | 2008-09-16 13:29:37 -0400 | [diff] [blame] | 208 | yy = MAC16_16(yy, magnitude,magnitude); |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 209 | /* Choose between fast and accurate strategy depending on where we are in the search */ |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 210 | /* This should ensure that anything we can process will have a better score */ |
| Jean-Marc Valin | 7bb339d | 2008-09-21 21:11:39 -0400 | [diff] [blame] | 211 | j=0; |
| 212 | do { |
| 213 | celt_word16_t Rxy, Ryy; |
| 214 | /* Select sign based on X[j] alone */ |
| Jean-Marc Valin | 6cde5dd | 2008-12-04 21:21:41 -0500 | [diff] [blame] | 215 | s = magnitude; |
| Jean-Marc Valin | 7bb339d | 2008-09-21 21:11:39 -0400 | [diff] [blame] | 216 | /* Temporary sums of the new pulse(s) */ |
| 217 | Rxy = EXTRACT16(SHR32(MAC16_16(xy, s,X[j]),rshift)); |
| 218 | /* We're multiplying y[j] by two so we don't have to do it here */ |
| 219 | Ryy = EXTRACT16(SHR32(MAC16_16(yy, s,y[j]),rshift)); |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 220 | |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 221 | /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that |
| Jean-Marc Valin | 7bb339d | 2008-09-21 21:11:39 -0400 | [diff] [blame] | 222 | Rxy is positive because the sign is pre-computed) */ |
| 223 | Rxy = MULT16_16_Q15(Rxy,Rxy); |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 224 | /* The idea is to check for num/den >= best_num/best_den, but that way |
| Jean-Marc Valin | 7bb339d | 2008-09-21 21:11:39 -0400 | [diff] [blame] | 225 | we can do it without any division */ |
| 226 | /* OPT: Make sure to use conditional moves here */ |
| 227 | if (MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num)) |
| 228 | { |
| 229 | best_den = Ryy; |
| 230 | best_num = Rxy; |
| 231 | best_id = j; |
| 232 | } |
| 233 | } while (++j<N); |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 234 | |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 235 | j = best_id; |
| Jean-Marc Valin | 6cde5dd | 2008-12-04 21:21:41 -0500 | [diff] [blame] | 236 | is = pulsesAtOnce; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 237 | s = SHL16(is, yshift); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 238 | |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 239 | /* Updating the sums of the new pulse(s) */ |
| 240 | xy = xy + MULT16_16(s,X[j]); |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 241 | /* We're multiplying y[j] by two so we don't have to do it here */ |
| 242 | yy = yy + MULT16_16(s,y[j]); |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 243 | yp = yp + MULT16_16(s, P[j]); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 244 | |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 245 | /* Only now that we've made the final choice, update y/iy */ |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 246 | /* Multiplying y[j] by 2 so we don't have to do it everywhere else */ |
| 247 | y[j] += 2*s; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 248 | iy[j] += is; |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 249 | pulsesLeft -= pulsesAtOnce; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 250 | } |
| 251 | |
| Jean-Marc Valin | 8256ed4 | 2008-12-12 20:50:56 -0500 | [diff] [blame] | 252 | if (pulsesLeft > 0) |
| Jean-Marc Valin | 7bb339d | 2008-09-21 21:11:39 -0400 | [diff] [blame] | 253 | { |
| 254 | celt_word16_t g; |
| 255 | celt_word16_t best_num = -VERY_LARGE16; |
| 256 | celt_word16_t best_den = 0; |
| 257 | int best_id = 0; |
| Jean-Marc Valin | 0ec7c14 | 2008-09-22 10:25:46 -0400 | [diff] [blame] | 258 | celt_word16_t magnitude = SHL16(1, yshift); |
| Jean-Marc Valin | 7bb339d | 2008-09-21 21:11:39 -0400 | [diff] [blame] | 259 | |
| 260 | /* The squared magnitude term gets added anyway, so we might as well |
| 261 | add it outside the loop */ |
| Jean-Marc Valin | 0ec7c14 | 2008-09-22 10:25:46 -0400 | [diff] [blame] | 262 | yy = MAC16_16(yy, magnitude,magnitude); |
| Jean-Marc Valin | 7bb339d | 2008-09-21 21:11:39 -0400 | [diff] [blame] | 263 | j=0; |
| 264 | do { |
| 265 | celt_word16_t Rxy, Ryy, Ryp; |
| 266 | celt_word16_t num; |
| 267 | /* Select sign based on X[j] alone */ |
| Jean-Marc Valin | 6cde5dd | 2008-12-04 21:21:41 -0500 | [diff] [blame] | 268 | s = magnitude; |
| Jean-Marc Valin | 7bb339d | 2008-09-21 21:11:39 -0400 | [diff] [blame] | 269 | /* Temporary sums of the new pulse(s) */ |
| 270 | Rxy = ROUND16(MAC16_16(xy, s,X[j]), 14); |
| 271 | /* We're multiplying y[j] by two so we don't have to do it here */ |
| 272 | Ryy = ROUND16(MAC16_16(yy, s,y[j]), 14); |
| 273 | Ryp = ROUND16(MAC16_16(yp, s,P[j]), 14); |
| 274 | |
| 275 | /* Compute the gain such that ||p + g*y|| = 1 |
| 276 | ...but instead, we compute g*Ryy to avoid dividing */ |
| 277 | g = celt_psqrt(MULT16_16(Ryp,Ryp) + MULT16_16(Ryy,QCONST16(1.f,14)-Rpp)) - Ryp; |
| 278 | /* Knowing that gain, what's the error: (x-g*y)^2 |
| 279 | (result is negated and we discard x^2 because it's constant) */ |
| 280 | /* score = 2*g*Rxy - g*g*Ryy;*/ |
| 281 | #ifdef FIXED_POINT |
| 282 | /* No need to multiply Rxy by 2 because we did it earlier */ |
| 283 | num = MULT16_16_Q15(ADD16(SUB16(Rxy,g),Rxy),g); |
| 284 | #else |
| 285 | num = g*(2*Rxy-g); |
| 286 | #endif |
| 287 | if (MULT16_16(best_den, num) > MULT16_16(Ryy, best_num)) |
| 288 | { |
| 289 | best_den = Ryy; |
| 290 | best_num = num; |
| 291 | best_id = j; |
| 292 | } |
| 293 | } while (++j<N); |
| Jean-Marc Valin | 6cde5dd | 2008-12-04 21:21:41 -0500 | [diff] [blame] | 294 | iy[best_id] += 1; |
| Jean-Marc Valin | 7bb339d | 2008-09-21 21:11:39 -0400 | [diff] [blame] | 295 | } |
| Jean-Marc Valin | 6cde5dd | 2008-12-04 21:21:41 -0500 | [diff] [blame] | 296 | j=0; |
| 297 | do { |
| 298 | P[j] = MULT16_16(signx[j],P[j]); |
| 299 | X[j] = MULT16_16(signx[j],X[j]); |
| 300 | if (signx[j] < 0) |
| 301 | iy[j] = -iy[j]; |
| 302 | } while (++j<N); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 303 | encode_pulses(iy, N, K, enc); |
| Jean-Marc Valin | 5fa5995 | 2008-02-14 13:50:44 +1100 | [diff] [blame] | 304 | |
| Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 305 | /* 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] | 306 | due to the recursive computation used in quantisation. */ |
| 307 | mix_pitch_and_residual(iy, X, N, K, P); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 308 | RESTORE_STACK; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 309 | } |
| 310 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 311 | |
| Jean-Marc Valin | 879fbfd | 2008-02-20 17:17:13 +1100 | [diff] [blame] | 312 | /** Decode pulse vector and combine the result with the pitch vector to produce |
| 313 | the final normalised signal in the current band. */ |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 314 | 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] | 315 | { |
| Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 316 | VARDECL(int, iy); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 317 | SAVE_STACK; |
| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 318 | ALLOC(iy, N, int); |
| Jean-Marc Valin | 5fa5995 | 2008-02-14 13:50:44 +1100 | [diff] [blame] | 319 | decode_pulses(iy, N, K, dec); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 320 | mix_pitch_and_residual(iy, X, N, K, P); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 321 | RESTORE_STACK; |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 322 | } |
| 323 | |
| Jean-Marc Valin | ca53b7c | 2009-03-26 20:23:14 -0400 | [diff] [blame] | 324 | celt_word16_t renormalise_vector(celt_norm_t *X, celt_word16_t value, int N, int stride) |
| Jean-Marc Valin | 6361ad8 | 2008-07-20 23:14:31 -0400 | [diff] [blame] | 325 | { |
| 326 | int i; |
| 327 | celt_word32_t E = EPSILON; |
| Jean-Marc Valin | ca53b7c | 2009-03-26 20:23:14 -0400 | [diff] [blame] | 328 | celt_word16_t rE; |
| Jean-Marc Valin | 6361ad8 | 2008-07-20 23:14:31 -0400 | [diff] [blame] | 329 | celt_word16_t g; |
| 330 | celt_norm_t *xptr = X; |
| 331 | for (i=0;i<N;i++) |
| 332 | { |
| 333 | E = MAC16_16(E, *xptr, *xptr); |
| 334 | xptr += stride; |
| 335 | } |
| 336 | |
| Jean-Marc Valin | ca53b7c | 2009-03-26 20:23:14 -0400 | [diff] [blame] | 337 | rE = celt_sqrt(E); |
| 338 | g = MULT16_16_Q15(value,celt_rcp(SHL32(rE,9))); |
| Jean-Marc Valin | 6361ad8 | 2008-07-20 23:14:31 -0400 | [diff] [blame] | 339 | xptr = X; |
| 340 | for (i=0;i<N;i++) |
| 341 | { |
| 342 | *xptr = PSHR32(MULT16_16(g, *xptr),8); |
| 343 | xptr += stride; |
| 344 | } |
| Jean-Marc Valin | ca53b7c | 2009-03-26 20:23:14 -0400 | [diff] [blame] | 345 | return rE; |
| Jean-Marc Valin | 6361ad8 | 2008-07-20 23:14:31 -0400 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | static void fold(const CELTMode *m, int N, celt_norm_t *Y, celt_norm_t * restrict P, int N0, int B) |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 349 | { |
| Jean-Marc Valin | df38f2b | 2008-07-20 20:36:54 -0400 | [diff] [blame] | 350 | int j; |
| Jean-Marc Valin | ba11d78 | 2008-04-21 21:59:37 +1000 | [diff] [blame] | 351 | const int C = CHANNELS(m); |
| Jean-Marc Valin | 9edb7b4 | 2009-06-15 11:22:01 -0400 | [diff] [blame] | 352 | int id = (N0*C) % (C*B); |
| Jean-Marc Valin | df38f2b | 2008-07-20 20:36:54 -0400 | [diff] [blame] | 353 | /* Here, we assume that id will never be greater than N0, i.e. that |
| Jean-Marc Valin | 5eef264 | 2008-08-06 23:06:31 -0400 | [diff] [blame] | 354 | no band is wider than N0. In the unlikely case it happens, we set |
| 355 | everything to zero */ |
| Jean-Marc Valin | 9edb7b4 | 2009-06-15 11:22:01 -0400 | [diff] [blame] | 356 | if (id+C*N>N0*C) |
| Jean-Marc Valin | 5eef264 | 2008-08-06 23:06:31 -0400 | [diff] [blame] | 357 | for (j=0;j<C*N;j++) |
| 358 | P[j] = 0; |
| 359 | else |
| 360 | for (j=0;j<C*N;j++) |
| 361 | P[j] = Y[id++]; |
| Jean-Marc Valin | 2c73306 | 2008-07-17 16:22:23 -0400 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | #define KGAIN 6 |
| 365 | |
| Jean-Marc Valin | 896471d | 2008-11-06 21:55:41 -0500 | [diff] [blame] | 366 | void intra_fold(const CELTMode *m, celt_norm_t * restrict x, int N, int K, celt_norm_t *Y, celt_norm_t * restrict P, int N0, int B) |
| Jean-Marc Valin | 2c73306 | 2008-07-17 16:22:23 -0400 | [diff] [blame] | 367 | { |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 368 | celt_word16_t pred_gain; |
| Jean-Marc Valin | ba11d78 | 2008-04-21 21:59:37 +1000 | [diff] [blame] | 369 | const int C = CHANNELS(m); |
| Jean-Marc Valin | 896471d | 2008-11-06 21:55:41 -0500 | [diff] [blame] | 370 | |
| 371 | if (K==0) |
| 372 | pred_gain = Q15ONE; |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 373 | else |
| Jean-Marc Valin | 896471d | 2008-11-06 21:55:41 -0500 | [diff] [blame] | 374 | pred_gain = celt_div((celt_word32_t)MULT16_16(Q15_ONE,N),(celt_word32_t)(N+KGAIN*K)); |
| Jean-Marc Valin | ba11d78 | 2008-04-21 21:59:37 +1000 | [diff] [blame] | 375 | |
| Jean-Marc Valin | 6361ad8 | 2008-07-20 23:14:31 -0400 | [diff] [blame] | 376 | fold(m, N, Y, P, N0, B); |
| Jean-Marc Valin | 896471d | 2008-11-06 21:55:41 -0500 | [diff] [blame] | 377 | |
| 378 | renormalise_vector(P, pred_gain, C*N, 1); |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 379 | } |
| 380 | |