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 void frac_hadamard1(celt_norm *X, int len, int stride, celt_word16 c, celt_word16 s)
63 *x++ = EXTRACT16(SHR32(MULT16_16(c,x1) + MULT16_16(s,x2),15));
64 *y++ = EXTRACT16(SHR32(MULT16_16(s,x1) - MULT16_16(c,x2),15));
74 /* Reverse samples so that the next level starts from the other end */
75 for (j=0;j<len>>1;j++)
84 static void exp_rotation(celt_norm *X, int len, int dir, int stride, int K)
87 celt_word16 gain, theta;
88 int istride[MAX_LEVELS];
95 } while (N<MAX_LEVELS && stride < len);
97 gain = celt_div((celt_word32)MULT16_16(Q15_ONE,len),(celt_word32)(3+len+4*K));
98 /* FIXME: Make that HALF16 instead of HALF32 */
99 theta = HALF32(MULT16_16_Q15(gain,gain));
100 c = celt_cos_norm(EXTEND32(theta));
101 s = celt_cos_norm(EXTEND32(SUB16(Q15ONE,theta))); /* sin(theta) */
106 frac_hadamard1(X, len, istride[i], c, s);
109 frac_hadamard1(X, len, istride[i], c, s);
112 /* Undo last reversal */
113 for (i=0;i<len>>1;i++)
115 celt_norm tmp = X[i];
121 /** Takes the pitch vector and the decoded residual vector, computes the gain
122 that will give ||p+g*y||=1 and mixes the residual with the pitch. */
123 static void normalise_residual(int * restrict iy, celt_norm * restrict X, int N, int K, celt_word32 Ryy)
133 k = celt_ilog2(Ryy)>>1;
135 t = VSHR32(Ryy, (k-7)<<1);
136 g = celt_rsqrt_norm(t);
140 X[i] = EXTRACT16(PSHR32(MULT16_16(g, iy[i]), k+1));
144 void alg_quant(celt_norm *X, int N, int K, int spread, ec_enc *enc)
146 VARDECL(celt_norm, y);
148 VARDECL(celt_word16, signx);
154 int N_1; /* Inverse of N, in Q14 format (even for float) */
162 yshift = 13-celt_ilog2(K);
165 ALLOC(y, N, celt_norm);
167 ALLOC(signx, N, celt_word16);
171 exp_rotation(X, N, 1, spread, K);
189 /* Do a pre-search by projecting on the pyramid */
203 X[0] = QCONST16(1.f,14);
207 sum = QCONST16(1.f,14);
209 /* Do we have sufficient accuracy here? */
210 rcp = EXTRACT16(MULT16_32_Q16(K-1, celt_rcp(sum)));
213 /* It's really important to round *towards zero* here */
214 iy[j] = MULT16_16_Q15(X[j],rcp);
216 iy[j] = floor(rcp*X[j]);
218 y[j] = SHL16(iy[j],yshift);
219 yy = MAC16_16(yy, y[j],y[j]);
220 xy = MAC16_16(xy, X[j],y[j]);
225 celt_assert2(pulsesLeft>=1, "Allocated too many pulses in the quick pass");
227 while (pulsesLeft > 0)
231 celt_word16 magnitude;
232 celt_word32 best_num = -VERY_LARGE16;
233 celt_word16 best_den = 0;
237 /* Decide on how many pulses to find at once */
238 pulsesAtOnce = (pulsesLeft*N_1)>>9; /* pulsesLeft/N */
242 rshift = yshift+1+celt_ilog2(K-pulsesLeft+pulsesAtOnce);
244 magnitude = SHL16(pulsesAtOnce, yshift);
247 /* The squared magnitude term gets added anyway, so we might as well
248 add it outside the loop */
249 yy = MAC16_16(yy, magnitude,magnitude);
250 /* Choose between fast and accurate strategy depending on where we are in the search */
251 /* This should ensure that anything we can process will have a better score */
254 celt_word16 Rxy, Ryy;
255 /* Select sign based on X[j] alone */
257 /* Temporary sums of the new pulse(s) */
258 Rxy = EXTRACT16(SHR32(MAC16_16(xy, s,X[j]),rshift));
259 /* We're multiplying y[j] by two so we don't have to do it here */
260 Ryy = EXTRACT16(SHR32(MAC16_16(yy, s,y[j]),rshift));
262 /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that
263 Rxy is positive because the sign is pre-computed) */
264 Rxy = MULT16_16_Q15(Rxy,Rxy);
265 /* The idea is to check for num/den >= best_num/best_den, but that way
266 we can do it without any division */
267 /* OPT: Make sure to use conditional moves here */
268 if (MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num))
278 s = SHL16(is, yshift);
280 /* Updating the sums of the new pulse(s) */
281 xy = xy + MULT16_16(s,X[j]);
282 /* We're multiplying y[j] by two so we don't have to do it here */
283 yy = yy + MULT16_16(s,y[j]);
285 /* Only now that we've made the final choice, update y/iy */
286 /* Multiplying y[j] by 2 so we don't have to do it everywhere else */
289 pulsesLeft -= pulsesAtOnce;
293 X[j] = MULT16_16(signx[j],X[j]);
297 encode_pulses(iy, N, K, enc);
299 /* Recompute the gain in one pass to reduce the encoder-decoder mismatch
300 due to the recursive computation used in quantisation. */
301 normalise_residual(iy, X, N, K, EXTRACT16(SHR32(yy,2*yshift)));
303 exp_rotation(X, N, -1, spread, K);
308 /** Decode pulse vector and combine the result with the pitch vector to produce
309 the final normalised signal in the current band. */
310 void alg_unquant(celt_norm *X, int N, int K, int spread, ec_dec *dec)
318 decode_pulses(iy, N, K, dec);
322 Ryy = MAC16_16(Ryy, iy[i], iy[i]);
324 normalise_residual(iy, X, N, K, Ryy);
326 exp_rotation(X, N, -1, spread, K);
330 celt_word16 renormalise_vector(celt_norm *X, celt_word16 value, int N, int stride)
333 celt_word32 E = EPSILON;
338 E = MAC16_16(E, *xptr, *xptr);
342 int k = celt_ilog2(E)>>1;
344 celt_word32 t = VSHR32(E, (k-7)<<1);
345 g = MULT16_16_Q15(value, celt_rsqrt_norm(t));
350 *xptr = EXTRACT16(PSHR32(MULT16_16(g, *xptr), k+1));
356 static void fold(const CELTMode *m, int start, int N, const celt_norm * restrict Y, celt_norm * restrict P, int N0, int B)
360 while (id < m->eBands[start])
362 /* Here, we assume that id will never be greater than N0, i.e. that
363 no band is wider than N0. In the unlikely case it happens, we set
364 everything to zero */
366 int offset = (N0*C - (id+C*N))/2;
367 if (offset > C*N0/16)
369 offset -= offset % (C*B);
372 //printf ("%d\n", offset);
383 void intra_fold(const CELTMode *m, int start, int N, const celt_norm * restrict Y, celt_norm * restrict P, int N0, int B)
385 fold(m, start, N, Y, P, N0, B);
386 renormalise_vector(P, Q15ONE, N, 1);