1 /* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 - Neither the name of the Xiph.org Foundation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include "os_support.h"
45 #define M_PI 3.141592653
48 static celt_uint32 lcg_rand(celt_uint32 seed)
50 return 1664525 * seed + 1013904223;
53 static void exp_rotation1(celt_norm *X, int len, int stride, celt_word16 c, celt_word16 s)
58 for (i=0;i<len-stride;i++)
63 Xptr[stride] = EXTRACT16(SHR32(MULT16_16(c,x2) + MULT16_16(s,x1), 15));
64 *Xptr++ = EXTRACT16(SHR32(MULT16_16(c,x1) - MULT16_16(s,x2), 15));
66 Xptr = &X[len-2*stride-1];
67 for (i=len-2*stride-1;i>=0;i--)
72 Xptr[stride] = EXTRACT16(SHR32(MULT16_16(c,x2) + MULT16_16(s,x1), 15));
73 *Xptr-- = EXTRACT16(SHR32(MULT16_16(c,x1) - MULT16_16(s,x2), 15));
77 static void exp_rotation(celt_norm *X, int len, int dir, int stride, int K, int spread)
81 celt_word16 gain, theta;
92 if (2*K>=len || spread==0)
101 gain = celt_div((celt_word32)MULT16_16(Q15_ONE,len),(celt_word32)(len+factor*K));
102 /* FIXME: Make that HALF16 instead of HALF32 */
103 theta = HALF32(MULT16_16_Q15(gain,gain));
105 c = celt_cos_norm(EXTEND32(theta));
106 s = celt_cos_norm(EXTEND32(SUB16(Q15ONE,theta))); /* sin(theta) */
111 /* This is just a simple way of computing sqrt(len/stride) with rounding.
112 It's basically incrementing long as (stride2+0.5)^2 < len/stride.
113 I _think_ it is bit-exact */
114 while ((stride2*stride2+stride2)*stride + (stride>>2) < len)
118 for (i=0;i<stride;i++)
123 exp_rotation1(X+i*len, len, stride2, s, c);
124 exp_rotation1(X+i*len, len, 1, c, s);
126 exp_rotation1(X+i*len, len, 1, c, -s);
128 exp_rotation1(X+i*len, len, stride2, s, -c);
134 printf ("%f ", X[i]);
140 /** Takes the pitch vector and the decoded residual vector, computes the gain
141 that will give ||p+g*y||=1 and mixes the residual with the pitch. */
142 static void normalise_residual(int * restrict iy, celt_norm * restrict X, int N, int K, celt_word32 Ryy)
152 k = celt_ilog2(Ryy)>>1;
154 t = VSHR32(Ryy, (k-7)<<1);
155 g = celt_rsqrt_norm(t);
159 X[i] = EXTRACT16(PSHR32(MULT16_16(g, iy[i]), k+1));
163 void alg_quant(celt_norm *X, int N, int K, int spread, int B, celt_norm *lowband, int resynth, ec_enc *enc, celt_int32 *seed)
165 VARDECL(celt_norm, y);
167 VARDECL(celt_word16, signx);
173 int N_1; /* Inverse of N, in Q14 format (even for float) */
179 /* When there's no pulse, fill with noise or folded spectrum */
182 if (lowband != NULL && resynth)
187 /* This is important for encoding the side in stereo mode */
190 *seed = lcg_rand(*seed);
191 X[j] = (int)(*seed)>>20;
194 renormalise_vector(X, Q15ONE, N, 1);
199 yshift = 13-celt_ilog2(K);
202 ALLOC(y, N, celt_norm);
204 ALLOC(signx, N, celt_word16);
207 exp_rotation(X, N, 1, B, K, spread);
209 /* Get rid of the sign */
226 /* Do a pre-search by projecting on the pyramid */
234 /* If X is too small, just replace it with a pulse at 0 */
241 X[0] = QCONST16(1.f,14);
245 sum = QCONST16(1.f,14);
247 /* Do we have sufficient accuracy here? */
248 rcp = EXTRACT16(MULT16_32_Q16(K-1, celt_rcp(sum)));
251 /* It's really important to round *towards zero* here */
252 iy[j] = MULT16_16_Q15(X[j],rcp);
254 iy[j] = (int)floor(rcp*X[j]);
256 y[j] = SHL16(iy[j],yshift);
257 yy = MAC16_16(yy, y[j],y[j]);
258 xy = MAC16_16(xy, X[j],y[j]);
263 celt_assert2(pulsesLeft>=1, "Allocated too many pulses in the quick pass");
265 while (pulsesLeft > 0)
269 celt_word16 magnitude;
270 celt_word32 best_num = -VERY_LARGE16;
271 celt_word16 best_den = 0;
275 /* Decide on how many pulses to find at once */
276 pulsesAtOnce = (pulsesLeft*N_1)>>9; /* pulsesLeft/N */
280 rshift = yshift+1+celt_ilog2(K-pulsesLeft+pulsesAtOnce);
282 magnitude = SHL16(pulsesAtOnce, yshift);
285 /* The squared magnitude term gets added anyway, so we might as well
286 add it outside the loop */
287 yy = MAC16_16(yy, magnitude,magnitude);
288 /* Choose between fast and accurate strategy depending on where we are in the search */
289 /* This should ensure that anything we can process will have a better score */
292 celt_word16 Rxy, Ryy;
293 /* Select sign based on X[j] alone */
295 /* Temporary sums of the new pulse(s) */
296 Rxy = EXTRACT16(SHR32(MAC16_16(xy, s,X[j]),rshift));
297 /* We're multiplying y[j] by two so we don't have to do it here */
298 Ryy = EXTRACT16(SHR32(MAC16_16(yy, s,y[j]),rshift));
300 /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that
301 Rxy is positive because the sign is pre-computed) */
302 Rxy = MULT16_16_Q15(Rxy,Rxy);
303 /* The idea is to check for num/den >= best_num/best_den, but that way
304 we can do it without any division */
305 /* OPT: Make sure to use conditional moves here */
306 if (MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num))
316 s = SHL16(is, yshift);
318 /* Updating the sums of the new pulse(s) */
319 xy = xy + MULT16_16(s,X[j]);
320 /* We're multiplying y[j] by two so we don't have to do it here */
321 yy = yy + MULT16_16(s,y[j]);
323 /* Only now that we've made the final choice, update y/iy */
324 /* Multiplying y[j] by 2 so we don't have to do it everywhere else */
327 pulsesLeft -= pulsesAtOnce;
330 /* Put the original sign back */
333 X[j] = MULT16_16(signx[j],X[j]);
337 encode_pulses(iy, N, K, enc);
341 normalise_residual(iy, X, N, K, EXTRACT16(SHR32(yy,2*yshift)));
342 exp_rotation(X, N, -1, B, K, spread);
348 /** Decode pulse vector and combine the result with the pitch vector to produce
349 the final normalised signal in the current band. */
350 void alg_unquant(celt_norm *X, int N, int K, int spread, int B, celt_norm *lowband, ec_dec *dec, celt_int32 *seed)
364 /* This is important for encoding the side in stereo mode */
367 *seed = lcg_rand(*seed);
368 X[i] = (int)(*seed)>>20;
371 renormalise_vector(X, Q15ONE, N, 1);
376 decode_pulses(iy, N, K, dec);
380 Ryy = MAC16_16(Ryy, iy[i], iy[i]);
382 normalise_residual(iy, X, N, K, Ryy);
383 exp_rotation(X, N, -1, B, K, spread);
387 celt_word16 renormalise_vector(celt_norm *X, celt_word16 value, int N, int stride)
393 celt_word32 E = EPSILON;
399 E = MAC16_16(E, *xptr, *xptr);
403 k = celt_ilog2(E)>>1;
405 t = VSHR32(E, (k-7)<<1);
406 g = MULT16_16_Q15(value, celt_rsqrt_norm(t));
411 *xptr = EXTRACT16(PSHR32(MULT16_16(g, *xptr), k+1));