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"
46 #define M_PI 3.141592653
49 static void exp_rotation1(celt_norm *X, int len, int stride, celt_word16 c, celt_word16 s)
54 for (i=0;i<len-stride;i++)
59 Xptr[stride] = EXTRACT16(SHR32(MULT16_16(c,x2) + MULT16_16(s,x1), 15));
60 *Xptr++ = EXTRACT16(SHR32(MULT16_16(c,x1) - MULT16_16(s,x2), 15));
62 Xptr = &X[len-2*stride-1];
63 for (i=len-2*stride-1;i>=0;i--)
68 Xptr[stride] = EXTRACT16(SHR32(MULT16_16(c,x2) + MULT16_16(s,x1), 15));
69 *Xptr-- = EXTRACT16(SHR32(MULT16_16(c,x1) - MULT16_16(s,x2), 15));
73 static void exp_rotation(celt_norm *X, int len, int dir, int stride, int K, int spread)
75 static const int SPREAD_FACTOR[3]={5,10,15};
78 celt_word16 gain, theta;
89 if (2*K>=len || spread==SPREAD_NONE)
91 factor = SPREAD_FACTOR[spread-1];
93 gain = celt_div((celt_word32)MULT16_16(Q15_ONE,len),(celt_word32)(len+factor*K));
94 /* FIXME: Make that HALF16 instead of HALF32 */
95 theta = HALF32(MULT16_16_Q15(gain,gain));
97 c = celt_cos_norm(EXTEND32(theta));
98 s = celt_cos_norm(EXTEND32(SUB16(Q15ONE,theta))); /* sin(theta) */
103 /* This is just a simple way of computing sqrt(len/stride) with rounding.
104 It's basically incrementing long as (stride2+0.5)^2 < len/stride.
105 I _think_ it is bit-exact */
106 while ((stride2*stride2+stride2)*stride + (stride>>2) < len)
110 for (i=0;i<stride;i++)
115 exp_rotation1(X+i*len, len, stride2, s, c);
116 exp_rotation1(X+i*len, len, 1, c, s);
118 exp_rotation1(X+i*len, len, 1, c, -s);
120 exp_rotation1(X+i*len, len, stride2, s, -c);
126 printf ("%f ", X[i]);
132 /** Takes the pitch vector and the decoded residual vector, computes the gain
133 that will give ||p+g*y||=1 and mixes the residual with the pitch. */
134 static void normalise_residual(int * restrict iy, celt_norm * restrict X,
135 int N, int K, celt_word32 Ryy, celt_word16 gain)
145 k = celt_ilog2(Ryy)>>1;
147 t = VSHR32(Ryy, (k-7)<<1);
148 g = MULT16_16_P15(celt_rsqrt_norm(t),gain);
152 X[i] = EXTRACT16(PSHR32(MULT16_16(g, iy[i]), k+1));
156 void alg_quant(celt_norm *X, int N, int K, int spread, int B, celt_norm *lowband,
157 int resynth, ec_enc *enc, celt_int32 *seed, celt_word16 gain)
159 VARDECL(celt_norm, y);
161 VARDECL(celt_word16, signx);
170 celt_assert2(K!=0, "alg_quant() needs at least one pulse");
172 ALLOC(y, N, celt_norm);
174 ALLOC(signx, N, celt_word16);
176 exp_rotation(X, N, 1, B, K, spread);
178 /* Get rid of the sign */
195 /* Do a pre-search by projecting on the pyramid */
203 /* If X is too small, just replace it with a pulse at 0 */
210 X[0] = QCONST16(1.f,14);
214 sum = QCONST16(1.f,14);
216 /* Do we have sufficient accuracy here? */
217 rcp = EXTRACT16(MULT16_32_Q16(K-1, celt_rcp(sum)));
220 /* It's really important to round *towards zero* here */
221 iy[j] = MULT16_16_Q15(X[j],rcp);
223 iy[j] = (int)floor(rcp*X[j]);
226 yy = MAC16_16(yy, y[j],y[j]);
227 xy = MAC16_16(xy, X[j],y[j]);
232 celt_assert2(pulsesLeft>=1, "Allocated too many pulses in the quick pass");
234 /* This should never happen, but just in case it does (e.g. on silence)
235 we fill the first bin with pulses. */
236 #ifdef FIXED_POINT_DEBUG
237 celt_assert2(pulsesLeft<=N+3, "Not enough pulses in the quick pass");
239 if (pulsesLeft > N+3)
241 celt_word16 tmp = pulsesLeft;
242 yy = MAC16_16(yy, tmp, tmp);
243 yy = MAC16_16(yy, tmp, y[0]);
249 for (i=0;i<pulsesLeft;i++)
252 celt_word32 best_num = -VERY_LARGE16;
253 celt_word16 best_den = 0;
258 rshift = 1+celt_ilog2(K-pulsesLeft+i+1);
261 /* The squared magnitude term gets added anyway, so we might as well
262 add it outside the loop */
266 celt_word16 Rxy, Ryy;
267 /* Temporary sums of the new pulse(s) */
268 Rxy = EXTRACT16(SHR32(ADD32(xy, EXTEND32(X[j])),rshift));
269 /* We're multiplying y[j] by two so we don't have to do it here */
270 Ryy = ADD16(yy, y[j]);
272 /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that
273 Rxy is positive because the sign is pre-computed) */
274 Rxy = MULT16_16_Q15(Rxy,Rxy);
275 /* The idea is to check for num/den >= best_num/best_den, but that way
276 we can do it without any division */
277 /* OPT: Make sure to use conditional moves here */
278 if (MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num))
286 /* Updating the sums of the new pulse(s) */
287 xy = ADD32(xy, EXTEND32(X[best_id]));
288 /* We're multiplying y[j] by two so we don't have to do it here */
289 yy = ADD16(yy, y[best_id]);
291 /* Only now that we've made the final choice, update y/iy */
292 /* Multiplying y[j] by 2 so we don't have to do it everywhere else */
297 /* Put the original sign back */
300 X[j] = MULT16_16(signx[j],X[j]);
304 encode_pulses(iy, N, K, enc);
308 normalise_residual(iy, X, N, K, yy, gain);
309 exp_rotation(X, N, -1, B, K, spread);
315 /** Decode pulse vector and combine the result with the pitch vector to produce
316 the final normalised signal in the current band. */
317 void alg_unquant(celt_norm *X, int N, int K, int spread, int B,
318 celt_norm *lowband, ec_dec *dec, celt_int32 *seed, celt_word16 gain)
325 celt_assert2(K!=0, "alg_unquant() needs at least one pulse");
327 decode_pulses(iy, N, K, dec);
331 Ryy = MAC16_16(Ryy, iy[i], iy[i]);
333 normalise_residual(iy, X, N, K, Ryy, gain);
334 exp_rotation(X, N, -1, B, K, spread);
338 void renormalise_vector(celt_norm *X, int N, celt_word16 gain)
344 celt_word32 E = EPSILON;
350 E = MAC16_16(E, *xptr, *xptr);
354 k = celt_ilog2(E)>>1;
356 t = VSHR32(E, (k-7)<<1);
357 g = MULT16_16_P15(celt_rsqrt_norm(t),gain);
362 *xptr = EXTRACT16(PSHR32(MULT16_16(g, *xptr), k+1));
365 /*return celt_sqrt(E);*/
368 int stereo_itheta(celt_norm *X, celt_norm *Y, int stereo, int N)
372 celt_word16 mid, side;
373 celt_word32 Emid, Eside;
375 Emid = Eside = EPSILON;
381 m = ADD16(SHR16(X[i],1),SHR16(Y[i],1));
382 s = SUB16(SHR16(X[i],1),SHR16(Y[i],1));
383 Emid = MAC16_16(Emid, m, m);
384 Eside = MAC16_16(Eside, s, s);
392 Emid = MAC16_16(Emid, m, m);
393 Eside = MAC16_16(Eside, s, s);
396 mid = celt_sqrt(Emid);
397 side = celt_sqrt(Eside);
400 itheta = MULT16_16_Q15(QCONST16(0.63662f,15),celt_atan2p(side, mid));
402 itheta = (int)floor(.5f+16384*0.63662f*atan2(side,mid));