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)
109 /*TODO: We should be passing around log2(B), not B, for both this and for
110 extract_collapse_mask().*/
112 for (i=0;i<stride;i++)
117 exp_rotation1(X+i*len, len, stride2, s, c);
118 exp_rotation1(X+i*len, len, 1, c, s);
120 exp_rotation1(X+i*len, len, 1, c, -s);
122 exp_rotation1(X+i*len, len, stride2, s, -c);
128 printf ("%f ", X[i]);
134 /** Takes the pitch vector and the decoded residual vector, computes the gain
135 that will give ||p+g*y||=1 and mixes the residual with the pitch. */
136 static void normalise_residual(int * restrict iy, celt_norm * restrict X,
137 int N, celt_word32 Ryy, celt_word16 gain)
147 k = celt_ilog2(Ryy)>>1;
149 t = VSHR32(Ryy, (k-7)<<1);
150 g = MULT16_16_P15(celt_rsqrt_norm(t),gain);
154 X[i] = EXTRACT16(PSHR32(MULT16_16(g, iy[i]), k+1));
158 static unsigned extract_collapse_mask(int *iy, int N, int B)
160 unsigned collapse_mask;
165 /*TODO: We should be passing around log2(B), not B, for both this and for
172 collapse_mask |= (iy[i*N0+j]!=0)<<i;
175 return collapse_mask;
178 unsigned alg_quant(celt_norm *X, int N, int K, int spread, int B,
179 int resynth, ec_enc *enc, celt_word16 gain)
181 VARDECL(celt_norm, y);
183 VARDECL(celt_word16, signx);
190 unsigned collapse_mask;
193 celt_assert2(K!=0, "alg_quant() needs at least one pulse");
195 ALLOC(y, N, celt_norm);
197 ALLOC(signx, N, celt_word16);
199 exp_rotation(X, N, 1, B, K, spread);
201 /* Get rid of the sign */
218 /* Do a pre-search by projecting on the pyramid */
226 /* If X is too small, just replace it with a pulse at 0 */
233 X[0] = QCONST16(1.f,14);
237 sum = QCONST16(1.f,14);
239 /* Do we have sufficient accuracy here? */
240 rcp = EXTRACT16(MULT16_32_Q16(K-1, celt_rcp(sum)));
243 /* It's really important to round *towards zero* here */
244 iy[j] = MULT16_16_Q15(X[j],rcp);
246 iy[j] = (int)floor(rcp*X[j]);
249 yy = MAC16_16(yy, y[j],y[j]);
250 xy = MAC16_16(xy, X[j],y[j]);
255 celt_assert2(pulsesLeft>=1, "Allocated too many pulses in the quick pass");
257 /* This should never happen, but just in case it does (e.g. on silence)
258 we fill the first bin with pulses. */
259 #ifdef FIXED_POINT_DEBUG
260 celt_assert2(pulsesLeft<=N+3, "Not enough pulses in the quick pass");
262 if (pulsesLeft > N+3)
264 celt_word16 tmp = pulsesLeft;
265 yy = MAC16_16(yy, tmp, tmp);
266 yy = MAC16_16(yy, tmp, y[0]);
272 for (i=0;i<pulsesLeft;i++)
275 celt_word32 best_num = -VERY_LARGE16;
276 celt_word16 best_den = 0;
281 rshift = 1+celt_ilog2(K-pulsesLeft+i+1);
284 /* The squared magnitude term gets added anyway, so we might as well
285 add it outside the loop */
289 celt_word16 Rxy, Ryy;
290 /* Temporary sums of the new pulse(s) */
291 Rxy = EXTRACT16(SHR32(ADD32(xy, EXTEND32(X[j])),rshift));
292 /* We're multiplying y[j] by two so we don't have to do it here */
293 Ryy = ADD16(yy, y[j]);
295 /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that
296 Rxy is positive because the sign is pre-computed) */
297 Rxy = MULT16_16_Q15(Rxy,Rxy);
298 /* The idea is to check for num/den >= best_num/best_den, but that way
299 we can do it without any division */
300 /* OPT: Make sure to use conditional moves here */
301 if (MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num))
309 /* Updating the sums of the new pulse(s) */
310 xy = ADD32(xy, EXTEND32(X[best_id]));
311 /* We're multiplying y[j] by two so we don't have to do it here */
312 yy = ADD16(yy, y[best_id]);
314 /* Only now that we've made the final choice, update y/iy */
315 /* Multiplying y[j] by 2 so we don't have to do it everywhere else */
320 /* Put the original sign back */
323 X[j] = MULT16_16(signx[j],X[j]);
327 encode_pulses(iy, N, K, enc);
331 normalise_residual(iy, X, N, yy, gain);
332 exp_rotation(X, N, -1, B, K, spread);
334 collapse_mask = extract_collapse_mask(iy, N, B);
336 return collapse_mask;
340 /** Decode pulse vector and combine the result with the pitch vector to produce
341 the final normalised signal in the current band. */
342 unsigned alg_unquant(celt_norm *X, int N, int K, int spread, int B,
343 ec_dec *dec, celt_word16 gain)
347 unsigned collapse_mask;
351 celt_assert2(K!=0, "alg_unquant() needs at least one pulse");
353 decode_pulses(iy, N, K, dec);
357 Ryy = MAC16_16(Ryy, iy[i], iy[i]);
359 normalise_residual(iy, X, N, Ryy, gain);
360 exp_rotation(X, N, -1, B, K, spread);
361 collapse_mask = extract_collapse_mask(iy, N, B);
363 return collapse_mask;
366 void renormalise_vector(celt_norm *X, int N, celt_word16 gain)
372 celt_word32 E = EPSILON;
378 E = MAC16_16(E, *xptr, *xptr);
382 k = celt_ilog2(E)>>1;
384 t = VSHR32(E, (k-7)<<1);
385 g = MULT16_16_P15(celt_rsqrt_norm(t),gain);
390 *xptr = EXTRACT16(PSHR32(MULT16_16(g, *xptr), k+1));
393 /*return celt_sqrt(E);*/
396 int stereo_itheta(celt_norm *X, celt_norm *Y, int stereo, int N)
400 celt_word16 mid, side;
401 celt_word32 Emid, Eside;
403 Emid = Eside = EPSILON;
409 m = ADD16(SHR16(X[i],1),SHR16(Y[i],1));
410 s = SUB16(SHR16(X[i],1),SHR16(Y[i],1));
411 Emid = MAC16_16(Emid, m, m);
412 Eside = MAC16_16(Eside, s, s);
420 Emid = MAC16_16(Emid, m, m);
421 Eside = MAC16_16(Eside, s, s);
424 mid = celt_sqrt(Emid);
425 side = celt_sqrt(Eside);
428 itheta = MULT16_16_Q15(QCONST16(0.63662f,15),celt_atan2p(side, mid));
430 itheta = (int)floor(.5f+16384*0.63662f*atan2(side,mid));