| 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 | 98c86c7 | 2008-03-27 08:40:45 +1100 | [diff] [blame] | 55 | yshift = 13-celt_ilog2(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 | 6ea8bae | 2008-04-15 08:01:33 +1000 | [diff] [blame] | 62 | i=0; |
| 63 | do { |
| 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 | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 65 | y[i] = SHL16(iy[i],yshift); |
| Jean-Marc Valin | 6ea8bae | 2008-04-15 08:01:33 +1000 | [diff] [blame] | 66 | } while (++i < N); |
| 67 | |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 68 | Ryp = 0; |
| Jean-Marc Valin | b50c541 | 2008-02-27 17:05:43 +1100 | [diff] [blame] | 69 | Ryy = 0; |
| Jean-Marc Valin | df7ab43 | 2008-03-26 18:03:22 +1100 | [diff] [blame] | 70 | /* 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] | 71 | i=0; |
| 72 | do { |
| Jean-Marc Valin | df7ab43 | 2008-03-26 18:03:22 +1100 | [diff] [blame] | 73 | Ryp = MAC16_16(Ryp, y[i], P[i]); |
| 74 | Ryy = MAC16_16(Ryy, y[i], y[i]); |
| Jean-Marc Valin | 6ea8bae | 2008-04-15 08:01:33 +1000 | [diff] [blame] | 75 | } while (++i < N); |
| 76 | |
| Jean-Marc Valin | 1ca0722 | 2008-02-27 17:23:04 +1100 | [diff] [blame] | 77 | /* g = (sqrt(Ryp^2 + Ryy - Rpp*Ryy)-Ryp)/Ryy */ |
| Jean-Marc Valin | 9d5b4a6 | 2008-03-13 11:36:45 +1100 | [diff] [blame] | 78 | g = MULT16_32_Q15( |
| Jean-Marc Valin | f5b0587 | 2008-03-21 10:46:17 +1100 | [diff] [blame] | 79 | celt_sqrt(MULT16_16(ROUND16(Ryp,14),ROUND16(Ryp,14)) + Ryy - |
| 80 | MULT16_16(ROUND16(Ryy,14),ROUND16(Rpp,14))) |
| 81 | - ROUND16(Ryp,14), |
| Jean-Marc Valin | 9d5b4a6 | 2008-03-13 11:36:45 +1100 | [diff] [blame] | 82 | celt_rcp(SHR32(Ryy,9))); |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 83 | |
| Jean-Marc Valin | 6ea8bae | 2008-04-15 08:01:33 +1000 | [diff] [blame] | 84 | i=0; |
| 85 | do |
| Jean-Marc Valin | f5b0587 | 2008-03-21 10:46:17 +1100 | [diff] [blame] | 86 | X[i] = P[i] + ROUND16(MULT16_16(y[i], g),11); |
| Jean-Marc Valin | 6ea8bae | 2008-04-15 08:01:33 +1000 | [diff] [blame] | 87 | while (++i < N); |
| 88 | |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 89 | RESTORE_STACK; |
| Jean-Marc Valin | d4018c3 | 2008-02-27 10:09:48 +1100 | [diff] [blame] | 90 | } |
| 91 | |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 92 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 93 | 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] | 94 | { |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 95 | VARDECL(celt_norm_t, y); |
| 96 | VARDECL(int, iy); |
| Jean-Marc Valin | 4913438 | 2008-03-25 16:07:05 +1100 | [diff] [blame] | 97 | VARDECL(int, signx); |
| Jean-Marc Valin | 6ea8bae | 2008-04-15 08:01:33 +1000 | [diff] [blame] | 98 | int j, is; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 99 | celt_word16_t s; |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 100 | int pulsesLeft; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 101 | celt_word32_t sum; |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 102 | celt_word32_t xy, yy, yp; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 103 | celt_word16_t Rpp; |
| Jean-Marc Valin | f958477 | 2008-03-27 12:22:44 +1100 | [diff] [blame] | 104 | 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] | 105 | #ifdef FIXED_POINT |
| Jean-Marc Valin | d748cd5 | 2008-03-01 07:27:03 +1100 | [diff] [blame] | 106 | int yshift; |
| 107 | #endif |
| 108 | SAVE_STACK; |
| 109 | |
| 110 | #ifdef FIXED_POINT |
| Jean-Marc Valin | 98c86c7 | 2008-03-27 08:40:45 +1100 | [diff] [blame] | 111 | yshift = 13-celt_ilog2(K); |
| Jean-Marc Valin | f675adc | 2008-02-28 12:15:17 +1100 | [diff] [blame] | 112 | #endif |
| Jean-Marc Valin | 9d8d9b3 | 2008-02-27 16:17:39 +1100 | [diff] [blame] | 113 | |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 114 | ALLOC(y, N, celt_norm_t); |
| 115 | ALLOC(iy, N, int); |
| Jean-Marc Valin | 4913438 | 2008-03-25 16:07:05 +1100 | [diff] [blame] | 116 | ALLOC(signx, N, int); |
| Jean-Marc Valin | 124d1cd | 2008-03-28 00:33:04 +1100 | [diff] [blame] | 117 | N_1 = 512/N; |
| Jean-Marc Valin | 3d152a5 | 2008-04-15 07:46:48 +1000 | [diff] [blame] | 118 | |
| 119 | sum = 0; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 120 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 121 | { |
| Jean-Marc Valin | 4913438 | 2008-03-25 16:07:05 +1100 | [diff] [blame] | 122 | if (X[j]>0) |
| 123 | signx[j]=1; |
| 124 | else |
| 125 | signx[j]=-1; |
| Jean-Marc Valin | 3d152a5 | 2008-04-15 07:46:48 +1000 | [diff] [blame] | 126 | iy[j] = 0; |
| 127 | y[j] = 0; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 128 | sum = MAC16_16(sum, P[j],P[j]); |
| Jean-Marc Valin | 0d587d8 | 2008-02-14 21:29:50 +1100 | [diff] [blame] | 129 | } |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 130 | Rpp = ROUND16(sum, NORM_SHIFT); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 131 | |
| Jean-Marc Valin | 4ff068e | 2008-03-15 23:34:39 +1100 | [diff] [blame] | 132 | 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] | 133 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 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 | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 140 | int best_id; |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 141 | celt_word16_t magnitude; |
| 142 | #ifdef FIXED_POINT |
| 143 | int rshift; |
| 144 | #endif |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 145 | /* Decide on how many pulses to find at once */ |
| Jean-Marc Valin | 124d1cd | 2008-03-28 00:33:04 +1100 | [diff] [blame] | 146 | pulsesAtOnce = (pulsesLeft*N_1)>>9; /* pulsesLeft/N */ |
| Jean-Marc Valin | cab576e | 2008-02-12 17:21:14 +1100 | [diff] [blame] | 147 | if (pulsesAtOnce<1) |
| 148 | pulsesAtOnce = 1; |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 149 | #ifdef FIXED_POINT |
| 150 | rshift = yshift+1+celt_ilog2(K-pulsesLeft+pulsesAtOnce); |
| 151 | #endif |
| 152 | magnitude = SHL16(pulsesAtOnce, yshift); |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 153 | |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 154 | best_id = 0; |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 155 | /* The squared magnitude term gets added anyway, so we might as well |
| 156 | add it outside the loop */ |
| 157 | yy = ADD32(yy, MULT16_16(magnitude,magnitude)); |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 158 | /* Choose between fast and accurate strategy depending on where we are in the search */ |
| 159 | if (pulsesLeft>1) |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 160 | { |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 161 | /* This should ensure that anything we can process will have a better score */ |
| 162 | celt_word32_t best_num = -VERY_LARGE16; |
| 163 | celt_word16_t best_den = 0; |
| Jean-Marc Valin | fed97d5 | 2008-03-26 21:31:56 +1100 | [diff] [blame] | 164 | j=0; |
| 165 | do { |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 166 | celt_word16_t Rxy, Ryy; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 167 | /* Select sign based on X[j] alone */ |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 168 | s = signx[j]*magnitude; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 169 | /* Temporary sums of the new pulse(s) */ |
| Jean-Marc Valin | d568303 | 2008-04-15 18:04:33 +1000 | [diff] [blame] | 170 | Rxy = EXTRACT16(SHR32(xy + MULT16_16(s,X[j]),rshift)); |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 171 | /* We're multiplying y[j] by two so we don't have to do it here */ |
| Jean-Marc Valin | d568303 | 2008-04-15 18:04:33 +1000 | [diff] [blame] | 172 | Ryy = EXTRACT16(SHR32(yy + MULT16_16(s,y[j]),rshift)); |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 173 | |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 174 | /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that |
| 175 | Rxy is positive because the sign is pre-computed) */ |
| 176 | Rxy = MULT16_16_Q15(Rxy,Rxy); |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 177 | /* The idea is to check for num/den >= best_num/best_den, but that way |
| 178 | we can do it without any division */ |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 179 | /* OPT: Make sure to use conditional moves here */ |
| 180 | if (MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num)) |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 181 | { |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 182 | best_den = Ryy; |
| 183 | best_num = Rxy; |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 184 | best_id = j; |
| 185 | } |
| Jean-Marc Valin | 96069fd | 2008-04-15 21:14:18 +1000 | [diff] [blame] | 186 | } while (++j<N); |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 187 | } else { |
| Jean-Marc Valin | 684fb28 | 2008-04-15 18:06:59 +1000 | [diff] [blame] | 188 | celt_word16_t g; |
| Jean-Marc Valin | 96069fd | 2008-04-15 21:14:18 +1000 | [diff] [blame] | 189 | celt_word16_t best_num = -VERY_LARGE16; |
| 190 | celt_word16_t best_den = 0; |
| 191 | j=0; |
| 192 | do { |
| Jean-Marc Valin | d568303 | 2008-04-15 18:04:33 +1000 | [diff] [blame] | 193 | celt_word16_t Rxy, Ryy, Ryp; |
| Jean-Marc Valin | 96069fd | 2008-04-15 21:14:18 +1000 | [diff] [blame] | 194 | celt_word16_t num; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 195 | /* Select sign based on X[j] alone */ |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 196 | s = signx[j]*magnitude; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 197 | /* Temporary sums of the new pulse(s) */ |
| Jean-Marc Valin | 96069fd | 2008-04-15 21:14:18 +1000 | [diff] [blame] | 198 | /* We only shift by 13 so we don't have to multiply by 2 later */ |
| 199 | Rxy = ROUND16(xy + MULT16_16(s,X[j]), 13); |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 200 | /* We're multiplying y[j] by two so we don't have to do it here */ |
| Jean-Marc Valin | d568303 | 2008-04-15 18:04:33 +1000 | [diff] [blame] | 201 | Ryy = ROUND16(yy + MULT16_16(s,y[j]), 14); |
| 202 | Ryp = ROUND16(yp + MULT16_16(s,P[j]), 14); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 203 | |
| Jean-Marc Valin | 96069fd | 2008-04-15 21:14:18 +1000 | [diff] [blame] | 204 | /* Compute the gain such that ||p + g*y|| = 1 |
| 205 | ...but instead, we compute g*Ryy to avoid dividing */ |
| 206 | g = celt_psqrt(MULT16_16(Ryp,Ryp) + MULT16_16(Ryy,QCONST16(1.f,14)-Rpp)) - Ryp; |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 207 | /* Knowing that gain, what's the error: (x-g*y)^2 |
| 208 | (result is negated and we discard x^2 because it's constant) */ |
| Jean-Marc Valin | d568303 | 2008-04-15 18:04:33 +1000 | [diff] [blame] | 209 | /* score = 2*g*Rxy - g*g*Ryy;*/ |
| 210 | #ifdef FIXED_POINT |
| Jean-Marc Valin | 96069fd | 2008-04-15 21:14:18 +1000 | [diff] [blame] | 211 | /* No need to multiply Rxy by 2 because we did it earlier */ |
| 212 | num = EXTRACT16(SHR32(MULT16_16(SUB16(Rxy,g),g),15)); |
| Jean-Marc Valin | d568303 | 2008-04-15 18:04:33 +1000 | [diff] [blame] | 213 | #else |
| Jean-Marc Valin | 96069fd | 2008-04-15 21:14:18 +1000 | [diff] [blame] | 214 | num = g*(2*Rxy-g); |
| Jean-Marc Valin | d568303 | 2008-04-15 18:04:33 +1000 | [diff] [blame] | 215 | #endif |
| Jean-Marc Valin | 96069fd | 2008-04-15 21:14:18 +1000 | [diff] [blame] | 216 | if (MULT16_16(best_den, num) > MULT16_16(Ryy, best_num)) |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 217 | { |
| Jean-Marc Valin | 96069fd | 2008-04-15 21:14:18 +1000 | [diff] [blame] | 218 | best_den = Ryy; |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 219 | best_num = num; |
| 220 | best_id = j; |
| Jean-Marc Valin | 96069fd | 2008-04-15 21:14:18 +1000 | [diff] [blame] | 221 | } |
| 222 | } while (++j<N); |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 223 | } |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 224 | |
| Jean-Marc Valin | 35a1f88 | 2008-03-26 10:34:23 +1100 | [diff] [blame] | 225 | j = best_id; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 226 | is = signx[j]*pulsesAtOnce; |
| 227 | s = SHL16(is, yshift); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 228 | |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 229 | /* Updating the sums of the new pulse(s) */ |
| 230 | xy = xy + MULT16_16(s,X[j]); |
| Jean-Marc Valin | ed317c9 | 2008-04-15 17:31:23 +1000 | [diff] [blame] | 231 | /* We're multiplying y[j] by two so we don't have to do it here */ |
| 232 | yy = yy + MULT16_16(s,y[j]); |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 233 | yp = yp + MULT16_16(s, P[j]); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 234 | |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 235 | /* 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] | 236 | /* Multiplying y[j] by 2 so we don't have to do it everywhere else */ |
| 237 | y[j] += 2*s; |
| Jean-Marc Valin | 44c6335 | 2008-03-25 21:28:40 +1100 | [diff] [blame] | 238 | iy[j] += is; |
| Jean-Marc Valin | 846d4e2 | 2008-02-12 13:48:48 +1100 | [diff] [blame] | 239 | pulsesLeft -= pulsesAtOnce; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 240 | } |
| 241 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 242 | encode_pulses(iy, N, K, enc); |
| Jean-Marc Valin | 5fa5995 | 2008-02-14 13:50:44 +1100 | [diff] [blame] | 243 | |
| Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 244 | /* 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] | 245 | due to the recursive computation used in quantisation. */ |
| 246 | mix_pitch_and_residual(iy, X, N, K, P); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 247 | RESTORE_STACK; |
| Jean-Marc Valin | 41af421 | 2007-11-30 18:35:37 +1100 | [diff] [blame] | 248 | } |
| 249 | |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 250 | |
| Jean-Marc Valin | 879fbfd | 2008-02-20 17:17:13 +1100 | [diff] [blame] | 251 | /** Decode pulse vector and combine the result with the pitch vector to produce |
| 252 | the final normalised signal in the current band. */ |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 253 | 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] | 254 | { |
| Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 255 | VARDECL(int, iy); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 256 | SAVE_STACK; |
| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 257 | ALLOC(iy, N, int); |
| Jean-Marc Valin | 5fa5995 | 2008-02-14 13:50:44 +1100 | [diff] [blame] | 258 | decode_pulses(iy, N, K, dec); |
| Jean-Marc Valin | bd718ba | 2008-03-25 14:15:41 +1100 | [diff] [blame] | 259 | mix_pitch_and_residual(iy, X, N, K, P); |
| Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 260 | RESTORE_STACK; |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 261 | } |
| 262 | |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 263 | #ifdef FIXED_POINT |
| 264 | static const celt_word16_t pg[11] = {32767, 24576, 21299, 19661, 19661, 19661, 18022, 18022, 16384, 16384, 16384}; |
| 265 | #else |
| Jean-Marc Valin | 3e65097 | 2008-03-07 17:38:58 +1100 | [diff] [blame] | 266 | 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] | 267 | #endif |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 268 | |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 269 | #define MAX_INTRA 32 |
| 270 | #define LOG_MAX_INTRA 5 |
| 271 | |
| Jean-Marc Valin | 5de868c | 2008-03-25 22:38:58 +1100 | [diff] [blame] | 272 | 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] | 273 | { |
| Jean-Marc Valin | 381b29c | 2008-04-10 11:00:51 +1000 | [diff] [blame] | 274 | int i,j,c; |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 275 | int best=0; |
| Jean-Marc Valin | 0a86464 | 2008-04-16 10:29:01 +1000 | [diff] [blame] | 276 | celt_word16_t best_num=-VERY_LARGE16; |
| Jean-Marc Valin | 89c5fd1 | 2008-03-26 12:16:00 +1100 | [diff] [blame] | 277 | celt_word16_t best_den=0; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 278 | celt_word16_t s = 1; |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 279 | int sign; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 280 | celt_word32_t E; |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 281 | celt_word16_t pred_gain; |
| Jean-Marc Valin | e28f25f | 2008-03-27 14:18:28 +1100 | [diff] [blame] | 282 | int max_pos = N0-N; |
| Jean-Marc Valin | a66c320 | 2008-04-16 10:43:32 +1000 | [diff] [blame] | 283 | celt_word32_t yy=0; |
| Jean-Marc Valin | 3956de9 | 2008-04-12 06:55:39 +1000 | [diff] [blame] | 284 | VARDECL(celt_norm_t, Xr); |
| Jean-Marc Valin | 381b29c | 2008-04-10 11:00:51 +1000 | [diff] [blame] | 285 | SAVE_STACK; |
| 286 | |
| 287 | ALLOC(Xr, B*N, celt_norm_t); |
| 288 | |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 289 | if (max_pos > MAX_INTRA) |
| 290 | max_pos = MAX_INTRA; |
| Jean-Marc Valin | 8f0f4b9 | 2008-02-11 13:52:44 +1100 | [diff] [blame] | 291 | |
| Jean-Marc Valin | c8e3b67 | 2008-04-10 12:21:26 +1000 | [diff] [blame] | 292 | /* Reverse the samples of x without reversing the channels */ |
| Jean-Marc Valin | 381b29c | 2008-04-10 11:00:51 +1000 | [diff] [blame] | 293 | for (c=0;c<B;c++) |
| Jean-Marc Valin | 381b29c | 2008-04-10 11:00:51 +1000 | [diff] [blame] | 294 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 381b29c | 2008-04-10 11:00:51 +1000 | [diff] [blame] | 295 | Xr[B*N-B*j-B+c] = x[B*j+c]; |
| Jean-Marc Valin | 381b29c | 2008-04-10 11:00:51 +1000 | [diff] [blame] | 296 | |
| Jean-Marc Valin | a66c320 | 2008-04-16 10:43:32 +1000 | [diff] [blame] | 297 | /* Compute yy for i=0 */ |
| 298 | j=0; |
| 299 | do { |
| 300 | yy = MAC16_16(yy, Y[j], Y[j]); |
| 301 | } while (++j<B*N); /* Promises we loop at least once */ |
| 302 | |
| Jean-Marc Valin | c8e3b67 | 2008-04-10 12:21:26 +1000 | [diff] [blame] | 303 | for (i=0;i<max_pos;i++) |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 304 | { |
| Jean-Marc Valin | a66c320 | 2008-04-16 10:43:32 +1000 | [diff] [blame] | 305 | celt_word32_t xy=0; |
| 306 | celt_word16_t num, den; |
| Jean-Marc Valin | c8e3b67 | 2008-04-10 12:21:26 +1000 | [diff] [blame] | 307 | const celt_word16_t * restrict xp = Xr; |
| 308 | const celt_word16_t * restrict yp = Y+B*i; |
| Jean-Marc Valin | fed97d5 | 2008-03-26 21:31:56 +1100 | [diff] [blame] | 309 | j=0; |
| 310 | do { |
| Jean-Marc Valin | a66c320 | 2008-04-16 10:43:32 +1000 | [diff] [blame] | 311 | xy = MAC16_16(xy, *xp++, *yp++); |
| Jean-Marc Valin | e28f25f | 2008-03-27 14:18:28 +1100 | [diff] [blame] | 312 | } while (++j<B*N); /* Promises we loop at least once */ |
| Jean-Marc Valin | 89c5fd1 | 2008-03-26 12:16:00 +1100 | [diff] [blame] | 313 | /* Using xy^2/yy as the score but without having to do the division */ |
| Jean-Marc Valin | 0a86464 | 2008-04-16 10:29:01 +1000 | [diff] [blame] | 314 | num = MULT16_16_Q15(ROUND16(xy,14),ROUND16(xy,14)); |
| Jean-Marc Valin | 89c5fd1 | 2008-03-26 12:16:00 +1100 | [diff] [blame] | 315 | den = ROUND16(yy,14); |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 316 | /* If you're really desperate for speed, just use xy as the score */ |
| Jean-Marc Valin | fed97d5 | 2008-03-26 21:31:56 +1100 | [diff] [blame] | 317 | /* OPT: Make sure to use a conditional move here */ |
| Jean-Marc Valin | 0a86464 | 2008-04-16 10:29:01 +1000 | [diff] [blame] | 318 | if (MULT16_16(best_den, num) > MULT16_16(den, best_num)) |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 319 | { |
| Jean-Marc Valin | 89c5fd1 | 2008-03-26 12:16:00 +1100 | [diff] [blame] | 320 | best_num = num; |
| 321 | best_den = den; |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 322 | best = i; |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 323 | /* Store xy as the sign. We'll normalise it to +/- 1 later. */ |
| 324 | s = ROUND16(xy,14); |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 325 | } |
| Jean-Marc Valin | a66c320 | 2008-04-16 10:43:32 +1000 | [diff] [blame] | 326 | /* Update yy for the next iteration */ |
| 327 | yp = Y+B*i; |
| 328 | j=0; |
| 329 | do { |
| 330 | yy = yy - MULT16_16(*yp, *yp) + MULT16_16(yp[B*N], yp[B*N]); |
| 331 | yp++; |
| 332 | } while (++j<B); |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 333 | } |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 334 | if (s<0) |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 335 | { |
| 336 | s = -1; |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 337 | sign = 1; |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 338 | } else { |
| 339 | s = 1; |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 340 | sign = 0; |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 341 | } |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 342 | /*printf ("%d %d ", sign, best);*/ |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 343 | ec_enc_bits(enc,sign,1); |
| 344 | if (max_pos == MAX_INTRA) |
| Jean-Marc Valin | c8e3b67 | 2008-04-10 12:21:26 +1000 | [diff] [blame] | 345 | ec_enc_bits(enc,best,LOG_MAX_INTRA); |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 346 | else |
| Jean-Marc Valin | c8e3b67 | 2008-04-10 12:21:26 +1000 | [diff] [blame] | 347 | ec_enc_uint(enc,best,max_pos); |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 348 | |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 349 | /*printf ("%d %f\n", best, best_score);*/ |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 350 | |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 351 | if (K>10) |
| 352 | pred_gain = pg[10]; |
| 353 | else |
| 354 | pred_gain = pg[K]; |
| Jean-Marc Valin | 03892c1 | 2008-03-07 17:25:47 +1100 | [diff] [blame] | 355 | E = EPSILON; |
| Jean-Marc Valin | 381b29c | 2008-04-10 11:00:51 +1000 | [diff] [blame] | 356 | for (c=0;c<B;c++) |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 357 | { |
| Jean-Marc Valin | 381b29c | 2008-04-10 11:00:51 +1000 | [diff] [blame] | 358 | for (j=0;j<N;j++) |
| 359 | { |
| Jean-Marc Valin | c8e3b67 | 2008-04-10 12:21:26 +1000 | [diff] [blame] | 360 | P[B*j+c] = s*Y[B*best+B*(N-j-1)+c]; |
| Jean-Marc Valin | b2b71a5 | 2008-04-16 10:45:52 +1000 | [diff] [blame^] | 361 | E = MAC16_16(E, P[B*j+c],P[B*j+c]); |
| Jean-Marc Valin | 381b29c | 2008-04-10 11:00:51 +1000 | [diff] [blame] | 362 | } |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 363 | } |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 364 | /*pred_gain = pred_gain/sqrt(E);*/ |
| Jean-Marc Valin | 23e82b2 | 2008-03-24 08:15:40 +1100 | [diff] [blame] | 365 | pred_gain = MULT16_16_Q15(pred_gain,celt_rcp(SHL32(celt_sqrt(E),9))); |
| Jean-Marc Valin | e28f25f | 2008-03-27 14:18:28 +1100 | [diff] [blame] | 366 | for (j=0;j<B*N;j++) |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 367 | P[j] = PSHR32(MULT16_16(pred_gain, P[j]),8); |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 368 | if (K>0) |
| 369 | { |
| Jean-Marc Valin | e28f25f | 2008-03-27 14:18:28 +1100 | [diff] [blame] | 370 | for (j=0;j<B*N;j++) |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 371 | x[j] -= P[j]; |
| 372 | } else { |
| Jean-Marc Valin | e28f25f | 2008-03-27 14:18:28 +1100 | [diff] [blame] | 373 | for (j=0;j<B*N;j++) |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 374 | x[j] = P[j]; |
| 375 | } |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 376 | /*printf ("quant ");*/ |
| 377 | /*for (j=0;j<N;j++) printf ("%f ", P[j]);*/ |
| Jean-Marc Valin | 6d3289c | 2008-04-10 14:43:59 +1000 | [diff] [blame] | 378 | RESTORE_STACK; |
| Jean-Marc Valin | 4841a0a | 2007-12-03 13:54:30 +1100 | [diff] [blame] | 379 | } |
| Jean-Marc Valin | fc08d0a | 2007-12-07 13:26:15 +1100 | [diff] [blame] | 380 | |
| Jean-Marc Valin | 5de868c | 2008-03-25 22:38:58 +1100 | [diff] [blame] | 381 | 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] | 382 | { |
| Jean-Marc Valin | 381b29c | 2008-04-10 11:00:51 +1000 | [diff] [blame] | 383 | int j, c; |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 384 | int sign; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 385 | celt_word16_t s; |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 386 | int best; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 387 | celt_word32_t E; |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 388 | celt_word16_t pred_gain; |
| Jean-Marc Valin | e28f25f | 2008-03-27 14:18:28 +1100 | [diff] [blame] | 389 | int max_pos = N0-N; |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 390 | if (max_pos > MAX_INTRA) |
| 391 | max_pos = MAX_INTRA; |
| Jean-Marc Valin | 8f0f4b9 | 2008-02-11 13:52:44 +1100 | [diff] [blame] | 392 | |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 393 | sign = ec_dec_bits(dec, 1); |
| Jean-Marc Valin | 0aa3903 | 2007-12-07 15:09:58 +1100 | [diff] [blame] | 394 | if (sign == 0) |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 395 | s = 1; |
| 396 | else |
| 397 | s = -1; |
| 398 | |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 399 | if (max_pos == MAX_INTRA) |
| Jean-Marc Valin | 15588ad | 2008-04-10 09:00:12 +1000 | [diff] [blame] | 400 | best = B*ec_dec_bits(dec, LOG_MAX_INTRA); |
| Jean-Marc Valin | 208ae6e | 2008-03-25 15:25:08 +1100 | [diff] [blame] | 401 | else |
| Jean-Marc Valin | 15588ad | 2008-04-10 09:00:12 +1000 | [diff] [blame] | 402 | best = B*ec_dec_uint(dec, max_pos); |
| Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 403 | /*printf ("%d %d ", sign, best);*/ |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 404 | |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 405 | if (K>10) |
| 406 | pred_gain = pg[10]; |
| 407 | else |
| 408 | pred_gain = pg[K]; |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 409 | E = EPSILON; |
| Jean-Marc Valin | 381b29c | 2008-04-10 11:00:51 +1000 | [diff] [blame] | 410 | for (c=0;c<B;c++) |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 411 | { |
| Jean-Marc Valin | 381b29c | 2008-04-10 11:00:51 +1000 | [diff] [blame] | 412 | for (j=0;j<N;j++) |
| 413 | { |
| 414 | P[B*j+c] = s*Y[best+B*(N-j-1)+c]; |
| Jean-Marc Valin | b2b71a5 | 2008-04-16 10:45:52 +1000 | [diff] [blame^] | 415 | E = MAC16_16(E, P[B*j+c],P[B*j+c]); |
| Jean-Marc Valin | 381b29c | 2008-04-10 11:00:51 +1000 | [diff] [blame] | 416 | } |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 417 | } |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 418 | /*pred_gain = pred_gain/sqrt(E);*/ |
| Jean-Marc Valin | 23e82b2 | 2008-03-24 08:15:40 +1100 | [diff] [blame] | 419 | pred_gain = MULT16_16_Q15(pred_gain,celt_rcp(SHL32(celt_sqrt(E),9))); |
| Jean-Marc Valin | e28f25f | 2008-03-27 14:18:28 +1100 | [diff] [blame] | 420 | for (j=0;j<B*N;j++) |
| Jean-Marc Valin | 9455d1b | 2008-03-07 17:17:37 +1100 | [diff] [blame] | 421 | P[j] = PSHR32(MULT16_16(pred_gain, P[j]),8); |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 422 | if (K==0) |
| 423 | { |
| Jean-Marc Valin | e28f25f | 2008-03-27 14:18:28 +1100 | [diff] [blame] | 424 | for (j=0;j<B*N;j++) |
| Jean-Marc Valin | 0d227d8 | 2007-12-31 16:12:12 +1100 | [diff] [blame] | 425 | x[j] = P[j]; |
| Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 426 | } |
| 427 | } |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 428 | |
| Jean-Marc Valin | 5de868c | 2008-03-25 22:38:58 +1100 | [diff] [blame] | 429 | 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] | 430 | { |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 431 | int i, j; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 432 | celt_word32_t E; |
| Jean-Marc Valin | ec9b6df | 2008-03-07 17:05:47 +1100 | [diff] [blame] | 433 | celt_word16_t g; |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 434 | |
| Jean-Marc Valin | ec9b6df | 2008-03-07 17:05:47 +1100 | [diff] [blame] | 435 | E = EPSILON; |
| Jean-Marc Valin | a536f77 | 2008-03-22 09:01:50 +1100 | [diff] [blame] | 436 | if (N0 >= (Nmax>>1)) |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 437 | { |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 438 | for (i=0;i<B;i++) |
| 439 | { |
| Jean-Marc Valin | e28f25f | 2008-03-27 14:18:28 +1100 | [diff] [blame] | 440 | for (j=0;j<N;j++) |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 441 | { |
| 442 | P[j*B+i] = Y[(Nmax-N0-j-1)*B+i]; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 443 | E += P[j*B+i]*P[j*B+i]; |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | } else { |
| Jean-Marc Valin | e28f25f | 2008-03-27 14:18:28 +1100 | [diff] [blame] | 447 | for (j=0;j<B*N;j++) |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 448 | { |
| 449 | P[j] = Y[j]; |
| Jean-Marc Valin | 877b197 | 2008-02-29 16:40:39 +1100 | [diff] [blame] | 450 | E = MAC16_16(E, P[j],P[j]); |
| Jean-Marc Valin | 0df0eb4 | 2008-02-13 16:00:10 +1100 | [diff] [blame] | 451 | } |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 452 | } |
| Jean-Marc Valin | 23e82b2 | 2008-03-24 08:15:40 +1100 | [diff] [blame] | 453 | g = celt_rcp(SHL32(celt_sqrt(E),9)); |
| Jean-Marc Valin | e28f25f | 2008-03-27 14:18:28 +1100 | [diff] [blame] | 454 | for (j=0;j<B*N;j++) |
| Jean-Marc Valin | ec9b6df | 2008-03-07 17:05:47 +1100 | [diff] [blame] | 455 | P[j] = PSHR32(MULT16_16(g, P[j]),8); |
| Jean-Marc Valin | e28f25f | 2008-03-27 14:18:28 +1100 | [diff] [blame] | 456 | for (j=0;j<B*N;j++) |
| Jean-Marc Valin | 0e20ca0 | 2008-02-11 15:33:53 +1100 | [diff] [blame] | 457 | x[j] = P[j]; |
| 458 | } |
| 459 | |