blob: e9836555e69e079bb4a1d643935ee8dc3ce36cb1 [file] [log] [blame]
Jean-Marc Valin35a1f882008-03-26 10:34:23 +11001/* (C) 2007-2008 Jean-Marc Valin, CSIRO
Jean-Marc Valin41af4212007-11-30 18:35:37 +11002*/
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 Valin02fa9132008-02-20 12:09:29 +110032#ifdef HAVE_CONFIG_H
33#include "config.h"
34#endif
35
Jean-Marc Valin3ca9b1d2008-02-27 23:50:31 +110036#include "mathops.h"
Jean-Marc Valin29ccab82007-12-06 15:39:38 +110037#include "cwrs.h"
Jean-Marc Valin9cace642007-12-06 17:44:09 +110038#include "vq.h"
Jean-Marc Valin9a0bba12008-02-20 14:08:50 +110039#include "arch.h"
Jean-Marc Valinb60340f2008-02-26 15:41:51 +110040#include "os_support.h"
Jean-Marc Valin41af4212007-11-30 18:35:37 +110041
Jean-Marc Valin35a1f882008-03-26 10:34:23 +110042/** 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 Valin5de868c2008-03-25 22:38:58 +110044static 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 Valind4018c32008-02-27 10:09:48 +110045{
46 int i;
Jean-Marc Valinb50c5412008-02-27 17:05:43 +110047 celt_word32_t Ryp, Ryy, Rpp;
Jean-Marc Valin1dab60c2008-09-16 13:29:37 -040048 celt_word16_t ryp, ryy, rpp;
Jean-Marc Valina847b772008-02-27 17:46:49 +110049 celt_word32_t g;
Jean-Marc Valin31b79d12008-03-12 17:17:23 +110050 VARDECL(celt_norm_t, y);
Jean-Marc Valind9de5932008-03-05 08:11:57 +110051#ifdef FIXED_POINT
52 int yshift;
53#endif
Jean-Marc Valin8600f692008-02-29 15:14:12 +110054 SAVE_STACK;
Jean-Marc Valind17edd32008-02-27 16:52:30 +110055#ifdef FIXED_POINT
Jean-Marc Valin98c86c72008-03-27 08:40:45 +110056 yshift = 13-celt_ilog2(K);
Jean-Marc Valind17edd32008-02-27 16:52:30 +110057#endif
58 ALLOC(y, N, celt_norm_t);
Jean-Marc Valind4018c32008-02-27 10:09:48 +110059
60 /*for (i=0;i<N;i++)
61 printf ("%d ", iy[i]);*/
Jean-Marc Valinb50c5412008-02-27 17:05:43 +110062 Rpp = 0;
Jean-Marc Valin6ea8bae2008-04-15 08:01:33 +100063 i=0;
64 do {
Jean-Marc Valinb50c5412008-02-27 17:05:43 +110065 Rpp = MAC16_16(Rpp,P[i],P[i]);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +110066 y[i] = SHL16(iy[i],yshift);
Jean-Marc Valin6ea8bae2008-04-15 08:01:33 +100067 } while (++i < N);
68
Jean-Marc Valind4018c32008-02-27 10:09:48 +110069 Ryp = 0;
Jean-Marc Valinb50c5412008-02-27 17:05:43 +110070 Ryy = 0;
Jean-Marc Valindf7ab432008-03-26 18:03:22 +110071 /* If this doesn't generate a dual MAC (on supported archs), fire the compiler guy */
Jean-Marc Valin6ea8bae2008-04-15 08:01:33 +100072 i=0;
73 do {
Jean-Marc Valindf7ab432008-03-26 18:03:22 +110074 Ryp = MAC16_16(Ryp, y[i], P[i]);
75 Ryy = MAC16_16(Ryy, y[i], y[i]);
Jean-Marc Valin6ea8bae2008-04-15 08:01:33 +100076 } while (++i < N);
77
Jean-Marc Valin1dab60c2008-09-16 13:29:37 -040078 ryp = ROUND16(Ryp,14);
79 ryy = ROUND16(Ryy,14);
80 rpp = ROUND16(Rpp,14);
Jean-Marc Valin1ca07222008-02-27 17:23:04 +110081 /* g = (sqrt(Ryp^2 + Ryy - Rpp*Ryy)-Ryp)/Ryy */
Jean-Marc Valin1dab60c2008-09-16 13:29:37 -040082 g = MULT16_32_Q15(celt_sqrt(MAC16_16(Ryy, ryp,ryp) - MULT16_16(ryy,rpp)) - ryp,
83 celt_rcp(SHR32(Ryy,9)));
Jean-Marc Valind4018c32008-02-27 10:09:48 +110084
Jean-Marc Valin6ea8bae2008-04-15 08:01:33 +100085 i=0;
86 do
Jean-Marc Valin1dab60c2008-09-16 13:29:37 -040087 X[i] = ADD16(P[i], ROUND16(MULT16_16(y[i], g),11));
Jean-Marc Valin6ea8bae2008-04-15 08:01:33 +100088 while (++i < N);
89
Jean-Marc Valin8600f692008-02-29 15:14:12 +110090 RESTORE_STACK;
Jean-Marc Valind4018c32008-02-27 10:09:48 +110091}
92
Jean-Marc Valin41af4212007-11-30 18:35:37 +110093
Jean-Marc Valin6cde5dd2008-12-04 21:21:41 -050094void alg_quant(celt_norm_t *X, celt_mask_t *W, int N, int K, celt_norm_t *P, ec_enc *enc)
Jean-Marc Valin41af4212007-11-30 18:35:37 +110095{
Jean-Marc Valin44c63352008-03-25 21:28:40 +110096 VARDECL(celt_norm_t, y);
97 VARDECL(int, iy);
Jean-Marc Valin1dab60c2008-09-16 13:29:37 -040098 VARDECL(celt_word16_t, signx);
Jean-Marc Valin6ea8bae2008-04-15 08:01:33 +100099 int j, is;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100100 celt_word16_t s;
Jean-Marc Valin846d4e22008-02-12 13:48:48 +1100101 int pulsesLeft;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100102 celt_word32_t sum;
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100103 celt_word32_t xy, yy, yp;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100104 celt_word16_t Rpp;
Jean-Marc Valinf9584772008-03-27 12:22:44 +1100105 int N_1; /* Inverse of N, in Q14 format (even for float) */
Jean-Marc Valinf675adc2008-02-28 12:15:17 +1100106#ifdef FIXED_POINT
Jean-Marc Valind748cd52008-03-01 07:27:03 +1100107 int yshift;
108#endif
109 SAVE_STACK;
110
111#ifdef FIXED_POINT
Jean-Marc Valin98c86c72008-03-27 08:40:45 +1100112 yshift = 13-celt_ilog2(K);
Jean-Marc Valinf675adc2008-02-28 12:15:17 +1100113#endif
Jean-Marc Valin9d8d9b32008-02-27 16:17:39 +1100114
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100115 ALLOC(y, N, celt_norm_t);
116 ALLOC(iy, N, int);
Jean-Marc Valin1dab60c2008-09-16 13:29:37 -0400117 ALLOC(signx, N, celt_word16_t);
Jean-Marc Valin124d1cd2008-03-28 00:33:04 +1100118 N_1 = 512/N;
Jean-Marc Valin3d152a52008-04-15 07:46:48 +1000119
120 sum = 0;
Jean-Marc Valindff9b7e2008-04-21 11:43:51 +1000121 j=0; do {
Jean-Marc Valinbf2d6482008-05-23 16:57:34 +1000122 X[j] -= P[j];
Jean-Marc Valin49134382008-03-25 16:07:05 +1100123 if (X[j]>0)
124 signx[j]=1;
Jean-Marc Valin6cde5dd2008-12-04 21:21:41 -0500125 else {
Jean-Marc Valin49134382008-03-25 16:07:05 +1100126 signx[j]=-1;
Jean-Marc Valin6cde5dd2008-12-04 21:21:41 -0500127 X[j]=-X[j];
128 P[j]=-P[j];
129 }
Jean-Marc Valin3d152a52008-04-15 07:46:48 +1000130 iy[j] = 0;
131 y[j] = 0;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100132 sum = MAC16_16(sum, P[j],P[j]);
Jean-Marc Valindff9b7e2008-04-21 11:43:51 +1000133 } while (++j<N);
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100134 Rpp = ROUND16(sum, NORM_SHIFT);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100135
Jean-Marc Valin4ff068e2008-03-15 23:34:39 +1100136 celt_assert2(Rpp<=NORM_SCALING, "Rpp should never have a norm greater than unity");
Jean-Marc Valinb60340f2008-02-26 15:41:51 +1100137
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100138 xy = yy = yp = 0;
Jean-Marc Valin0d587d82008-02-14 21:29:50 +1100139
Jean-Marc Valin846d4e22008-02-12 13:48:48 +1100140 pulsesLeft = K;
Jean-Marc Valina733f082008-12-04 22:52:26 -0500141#if 0
142 if (K > (N>>1))
143 {
Jean-Marc Valin09dc5a12008-12-05 00:28:28 -0500144 celt_word32_t sum=0;
Jean-Marc Valina733f082008-12-04 22:52:26 -0500145 j=0; do {
146 sum += X[j];
147 } while (++j<N);
Jean-Marc Valin137241d2008-12-06 23:44:55 -0500148 sum = DIV32(SHL32(EXTEND32(K-1),15),EPSILON+sum);
Jean-Marc Valina733f082008-12-04 22:52:26 -0500149 j=0; do {
Jean-Marc Valin09dc5a12008-12-05 00:28:28 -0500150#ifdef FIXED_POINT
Jean-Marc Valin137241d2008-12-06 23:44:55 -0500151 /* It's really important to round *towards zero* here */
Jean-Marc Valin09dc5a12008-12-05 00:28:28 -0500152 iy[j] = MULT16_32_Q15(X[j],sum);
153#else
Jean-Marc Valina733f082008-12-04 22:52:26 -0500154 iy[j] = floor(sum*X[j]);
Jean-Marc Valin09dc5a12008-12-05 00:28:28 -0500155#endif
Jean-Marc Valinc7635b42008-12-04 23:26:32 -0500156 y[j] = SHL16(iy[j],yshift);
157 yy = MAC16_16(yy, y[j],y[j]);
158 xy = MAC16_16(xy, X[j],y[j]);
Jean-Marc Valina733f082008-12-04 22:52:26 -0500159 yp += P[j]*y[j];
Jean-Marc Valin09dc5a12008-12-05 00:28:28 -0500160 y[j] *= 2;
Jean-Marc Valina733f082008-12-04 22:52:26 -0500161 pulsesLeft -= iy[j];
162 } while (++j<N);
163 }
Jean-Marc Valin137241d2008-12-06 23:44:55 -0500164 /*printf ("%d / %d (%d)\n", pulsesLeft, K, N);*/
165 celt_assert2(pulsesLeft>=1, "Allocated too many pulses in the quick pass");
Jean-Marc Valina733f082008-12-04 22:52:26 -0500166#endif
Jean-Marc Valin7bb339d2008-09-21 21:11:39 -0400167 while (pulsesLeft > 1)
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100168 {
Jean-Marc Valin846d4e22008-02-12 13:48:48 +1100169 int pulsesAtOnce=1;
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100170 int best_id;
Jean-Marc Valined317c92008-04-15 17:31:23 +1000171 celt_word16_t magnitude;
Jean-Marc Valin7bb339d2008-09-21 21:11:39 -0400172 celt_word32_t best_num = -VERY_LARGE16;
173 celt_word16_t best_den = 0;
Jean-Marc Valin0bc5f7f2008-04-20 17:16:18 +1000174#ifdef FIXED_POINT
175 int rshift;
176#endif
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100177 /* Decide on how many pulses to find at once */
Jean-Marc Valin124d1cd2008-03-28 00:33:04 +1100178 pulsesAtOnce = (pulsesLeft*N_1)>>9; /* pulsesLeft/N */
Jean-Marc Valincab576e2008-02-12 17:21:14 +1100179 if (pulsesAtOnce<1)
180 pulsesAtOnce = 1;
Jean-Marc Valin0bc5f7f2008-04-20 17:16:18 +1000181#ifdef FIXED_POINT
182 rshift = yshift+1+celt_ilog2(K-pulsesLeft+pulsesAtOnce);
183#endif
Jean-Marc Valined317c92008-04-15 17:31:23 +1000184 magnitude = SHL16(pulsesAtOnce, yshift);
Jean-Marc Valin846d4e22008-02-12 13:48:48 +1100185
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100186 best_id = 0;
Jean-Marc Valined317c92008-04-15 17:31:23 +1000187 /* The squared magnitude term gets added anyway, so we might as well
188 add it outside the loop */
Jean-Marc Valin1dab60c2008-09-16 13:29:37 -0400189 yy = MAC16_16(yy, magnitude,magnitude);
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100190 /* Choose between fast and accurate strategy depending on where we are in the search */
Jean-Marc Valined317c92008-04-15 17:31:23 +1000191 /* This should ensure that anything we can process will have a better score */
Jean-Marc Valin7bb339d2008-09-21 21:11:39 -0400192 j=0;
193 do {
194 celt_word16_t Rxy, Ryy;
195 /* Select sign based on X[j] alone */
Jean-Marc Valin6cde5dd2008-12-04 21:21:41 -0500196 s = magnitude;
Jean-Marc Valin7bb339d2008-09-21 21:11:39 -0400197 /* Temporary sums of the new pulse(s) */
198 Rxy = EXTRACT16(SHR32(MAC16_16(xy, s,X[j]),rshift));
199 /* We're multiplying y[j] by two so we don't have to do it here */
200 Ryy = EXTRACT16(SHR32(MAC16_16(yy, s,y[j]),rshift));
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100201
Jean-Marc Valined317c92008-04-15 17:31:23 +1000202 /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that
Jean-Marc Valin7bb339d2008-09-21 21:11:39 -0400203 Rxy is positive because the sign is pre-computed) */
204 Rxy = MULT16_16_Q15(Rxy,Rxy);
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100205 /* The idea is to check for num/den >= best_num/best_den, but that way
Jean-Marc Valin7bb339d2008-09-21 21:11:39 -0400206 we can do it without any division */
207 /* OPT: Make sure to use conditional moves here */
208 if (MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num))
209 {
210 best_den = Ryy;
211 best_num = Rxy;
212 best_id = j;
213 }
214 } while (++j<N);
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100215
Jean-Marc Valin35a1f882008-03-26 10:34:23 +1100216 j = best_id;
Jean-Marc Valin6cde5dd2008-12-04 21:21:41 -0500217 is = pulsesAtOnce;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100218 s = SHL16(is, yshift);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100219
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100220 /* Updating the sums of the new pulse(s) */
221 xy = xy + MULT16_16(s,X[j]);
Jean-Marc Valined317c92008-04-15 17:31:23 +1000222 /* We're multiplying y[j] by two so we don't have to do it here */
223 yy = yy + MULT16_16(s,y[j]);
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100224 yp = yp + MULT16_16(s, P[j]);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100225
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100226 /* Only now that we've made the final choice, update y/iy */
Jean-Marc Valined317c92008-04-15 17:31:23 +1000227 /* Multiplying y[j] by 2 so we don't have to do it everywhere else */
228 y[j] += 2*s;
Jean-Marc Valin44c63352008-03-25 21:28:40 +1100229 iy[j] += is;
Jean-Marc Valin846d4e22008-02-12 13:48:48 +1100230 pulsesLeft -= pulsesAtOnce;
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100231 }
232
Jean-Marc Valin7bb339d2008-09-21 21:11:39 -0400233 {
234 celt_word16_t g;
235 celt_word16_t best_num = -VERY_LARGE16;
236 celt_word16_t best_den = 0;
237 int best_id = 0;
Jean-Marc Valin0ec7c142008-09-22 10:25:46 -0400238 celt_word16_t magnitude = SHL16(1, yshift);
Jean-Marc Valin7bb339d2008-09-21 21:11:39 -0400239
240 /* The squared magnitude term gets added anyway, so we might as well
241 add it outside the loop */
Jean-Marc Valin0ec7c142008-09-22 10:25:46 -0400242 yy = MAC16_16(yy, magnitude,magnitude);
Jean-Marc Valin7bb339d2008-09-21 21:11:39 -0400243 j=0;
244 do {
245 celt_word16_t Rxy, Ryy, Ryp;
246 celt_word16_t num;
247 /* Select sign based on X[j] alone */
Jean-Marc Valin6cde5dd2008-12-04 21:21:41 -0500248 s = magnitude;
Jean-Marc Valin7bb339d2008-09-21 21:11:39 -0400249 /* Temporary sums of the new pulse(s) */
250 Rxy = ROUND16(MAC16_16(xy, s,X[j]), 14);
251 /* We're multiplying y[j] by two so we don't have to do it here */
252 Ryy = ROUND16(MAC16_16(yy, s,y[j]), 14);
253 Ryp = ROUND16(MAC16_16(yp, s,P[j]), 14);
254
255 /* Compute the gain such that ||p + g*y|| = 1
256 ...but instead, we compute g*Ryy to avoid dividing */
257 g = celt_psqrt(MULT16_16(Ryp,Ryp) + MULT16_16(Ryy,QCONST16(1.f,14)-Rpp)) - Ryp;
258 /* Knowing that gain, what's the error: (x-g*y)^2
259 (result is negated and we discard x^2 because it's constant) */
260 /* score = 2*g*Rxy - g*g*Ryy;*/
261#ifdef FIXED_POINT
262 /* No need to multiply Rxy by 2 because we did it earlier */
263 num = MULT16_16_Q15(ADD16(SUB16(Rxy,g),Rxy),g);
264#else
265 num = g*(2*Rxy-g);
266#endif
267 if (MULT16_16(best_den, num) > MULT16_16(Ryy, best_num))
268 {
269 best_den = Ryy;
270 best_num = num;
271 best_id = j;
272 }
273 } while (++j<N);
Jean-Marc Valin6cde5dd2008-12-04 21:21:41 -0500274 iy[best_id] += 1;
Jean-Marc Valin7bb339d2008-09-21 21:11:39 -0400275 }
Jean-Marc Valin6cde5dd2008-12-04 21:21:41 -0500276 j=0;
277 do {
278 P[j] = MULT16_16(signx[j],P[j]);
279 X[j] = MULT16_16(signx[j],X[j]);
280 if (signx[j] < 0)
281 iy[j] = -iy[j];
282 } while (++j<N);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100283 encode_pulses(iy, N, K, enc);
Jean-Marc Valin5fa59952008-02-14 13:50:44 +1100284
Jean-Marc Valina4833ff2008-01-10 15:34:00 +1100285 /* Recompute the gain in one pass to reduce the encoder-decoder mismatch
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100286 due to the recursive computation used in quantisation. */
287 mix_pitch_and_residual(iy, X, N, K, P);
Jean-Marc Valin8600f692008-02-29 15:14:12 +1100288 RESTORE_STACK;
Jean-Marc Valin41af4212007-11-30 18:35:37 +1100289}
290
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100291
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +1100292/** Decode pulse vector and combine the result with the pitch vector to produce
293 the final normalised signal in the current band. */
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100294void alg_unquant(celt_norm_t *X, int N, int K, celt_norm_t *P, ec_dec *dec)
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100295{
Jean-Marc Valin31b79d12008-03-12 17:17:23 +1100296 VARDECL(int, iy);
Jean-Marc Valin8600f692008-02-29 15:14:12 +1100297 SAVE_STACK;
Jean-Marc Valin9a0bba12008-02-20 14:08:50 +1100298 ALLOC(iy, N, int);
Jean-Marc Valin5fa59952008-02-14 13:50:44 +1100299 decode_pulses(iy, N, K, dec);
Jean-Marc Valinbd718ba2008-03-25 14:15:41 +1100300 mix_pitch_and_residual(iy, X, N, K, P);
Jean-Marc Valin8600f692008-02-29 15:14:12 +1100301 RESTORE_STACK;
Jean-Marc Valin0d227d82007-12-31 16:12:12 +1100302}
303
Jean-Marc Valin6361ad82008-07-20 23:14:31 -0400304void renormalise_vector(celt_norm_t *X, celt_word16_t value, int N, int stride)
305{
306 int i;
307 celt_word32_t E = EPSILON;
308 celt_word16_t g;
309 celt_norm_t *xptr = X;
310 for (i=0;i<N;i++)
311 {
312 E = MAC16_16(E, *xptr, *xptr);
313 xptr += stride;
314 }
315
316 g = MULT16_16_Q15(value,celt_rcp(SHL32(celt_sqrt(E),9)));
317 xptr = X;
318 for (i=0;i<N;i++)
319 {
320 *xptr = PSHR32(MULT16_16(g, *xptr),8);
321 xptr += stride;
322 }
323}
324
325static void fold(const CELTMode *m, int N, celt_norm_t *Y, celt_norm_t * restrict P, int N0, int B)
Jean-Marc Valin4841a0a2007-12-03 13:54:30 +1100326{
Jean-Marc Valindf38f2b2008-07-20 20:36:54 -0400327 int j;
Jean-Marc Valinba11d782008-04-21 21:59:37 +1000328 const int C = CHANNELS(m);
Jean-Marc Valindf38f2b2008-07-20 20:36:54 -0400329 int id = N0 % (C*B);
330 /* Here, we assume that id will never be greater than N0, i.e. that
Jean-Marc Valin5eef2642008-08-06 23:06:31 -0400331 no band is wider than N0. In the unlikely case it happens, we set
332 everything to zero */
333 if (id+C*N>N0)
334 for (j=0;j<C*N;j++)
335 P[j] = 0;
336 else
337 for (j=0;j<C*N;j++)
338 P[j] = Y[id++];
Jean-Marc Valin2c733062008-07-17 16:22:23 -0400339}
340
341#define KGAIN 6
342
Jean-Marc Valin896471d2008-11-06 21:55:41 -0500343void 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 Valin2c733062008-07-17 16:22:23 -0400344{
Jean-Marc Valin9455d1b2008-03-07 17:17:37 +1100345 celt_word16_t pred_gain;
Jean-Marc Valinba11d782008-04-21 21:59:37 +1000346 const int C = CHANNELS(m);
Jean-Marc Valin896471d2008-11-06 21:55:41 -0500347
348 if (K==0)
349 pred_gain = Q15ONE;
Jean-Marc Valin6e9058a2007-12-07 14:59:06 +1100350 else
Jean-Marc Valin896471d2008-11-06 21:55:41 -0500351 pred_gain = celt_div((celt_word32_t)MULT16_16(Q15_ONE,N),(celt_word32_t)(N+KGAIN*K));
Jean-Marc Valinba11d782008-04-21 21:59:37 +1000352
Jean-Marc Valin6361ad82008-07-20 23:14:31 -0400353 fold(m, N, Y, P, N0, B);
Jean-Marc Valin896471d2008-11-06 21:55:41 -0500354
355 renormalise_vector(P, pred_gain, C*N, 1);
Jean-Marc Valin0e20ca02008-02-11 15:33:53 +1100356}
357