1 /* (C) 2007-2008 Jean-Marc Valin, CSIRO
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
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.
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.
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.
40 #include "os_support.h"
43 static void exp_rotation(celt_norm_t *X, int len, int dir, int stride, int K)
47 celt_word16_t gain, theta;
49 gain = celt_div((celt_word32_t)MULT16_16(Q15_ONE,len),(celt_word32_t)(len+2*K*((K>>1)+1)));
50 /* FIXME: Make that HALF16 instead of HALF32 */
51 theta = SUB16(Q15ONE, HALF32(MULT16_16_Q15(gain,gain)));
58 c = celt_cos_norm(EXTEND32(theta));
59 s = dir*celt_cos_norm(EXTEND32(SUB16(Q15ONE,theta))); /* sin(theta) */
65 /* We could use MULT16_16_P15 instead of MULT16_16_Q15 for more accuracy,
66 but at this point, I really don't think it's necessary */
68 for (i=0;i<len-stride;i++)
73 Xptr[stride] = MULT16_16_Q15(c,x2) + MULT16_16_Q15(s,x1);
74 *Xptr++ = MULT16_16_Q15(c,x1) - MULT16_16_Q15(s,x2);
76 Xptr = &X[len-2*stride-1];
77 for (i=len-2*stride-1;i>=0;i--)
82 Xptr[stride] = MULT16_16_Q15(c,x2) + MULT16_16_Q15(s,x1);
83 *Xptr-- = MULT16_16_Q15(c,x1) - MULT16_16_Q15(s,x2);
96 /** Takes the pitch vector and the decoded residual vector, computes the gain
97 that will give ||p+g*y||=1 and mixes the residual with the pitch. */
98 static void mix_pitch_and_residual(int * restrict iy, celt_norm_t * restrict X, int N, int K, const celt_norm_t * restrict P)
101 celt_word32_t Ryp, Ryy, Rpp;
102 celt_word16_t ryp, ryy, rpp;
104 VARDECL(celt_norm_t, y);
110 yshift = 13-celt_ilog2(K);
112 ALLOC(y, N, celt_norm_t);
117 Rpp = MAC16_16(Rpp,P[i],P[i]);
118 y[i] = SHL16(iy[i],yshift);
123 /* If this doesn't generate a dual MAC (on supported archs), fire the compiler guy */
126 Ryp = MAC16_16(Ryp, y[i], P[i]);
127 Ryy = MAC16_16(Ryy, y[i], y[i]);
130 ryp = ROUND16(Ryp,14);
131 ryy = ROUND16(Ryy,14);
132 rpp = ROUND16(Rpp,14);
133 /* g = (sqrt(Ryp^2 + Ryy - Rpp*Ryy)-Ryp)/Ryy */
134 g = MULT16_32_Q15(celt_sqrt(MAC16_16(Ryy, ryp,ryp) - MULT16_16(ryy,rpp)) - ryp,
135 celt_rcp(SHR32(Ryy,9)));
139 X[i] = ADD16(P[i], ROUND16(MULT16_16(y[i], g),11));
146 void alg_quant(celt_norm_t *X, celt_mask_t *W, int N, int K, int spread, celt_norm_t *P, ec_enc *enc)
148 VARDECL(celt_norm_t, y);
150 VARDECL(celt_word16_t, signx);
155 celt_word32_t xy, yy, yp;
157 int N_1; /* Inverse of N, in Q14 format (even for float) */
165 yshift = 13-celt_ilog2(K);
168 ALLOC(y, N, celt_norm_t);
170 ALLOC(signx, N, celt_word16_t);
174 exp_rotation(X, N, 1, spread, K);
188 sum = MAC16_16(sum, P[j],P[j]);
190 Rpp = ROUND16(sum, NORM_SHIFT);
192 celt_assert2(Rpp<=NORM_SCALING, "Rpp should never have a norm greater than unity");
198 /* Do a pre-search by projecting on the pyramid */
213 X[0] = QCONST16(1.f,14);
217 sum = QCONST16(1.f,14);
219 /* Do we have sufficient accuracy here? */
220 rcp = EXTRACT16(MULT16_32_Q16(K-1, celt_rcp(sum)));
223 /* It's really important to round *towards zero* here */
224 iy[j] = MULT16_16_Q15(X[j],rcp);
226 iy[j] = floor(rcp*X[j]);
228 y[j] = SHL16(iy[j],yshift);
229 yy = MAC16_16(yy, y[j],y[j]);
230 xy = MAC16_16(xy, X[j],y[j]);
236 celt_assert2(pulsesLeft>=1, "Allocated too many pulses in the quick pass");
238 while (pulsesLeft > 1)
242 celt_word16_t magnitude;
243 celt_word32_t best_num = -VERY_LARGE16;
244 celt_word16_t best_den = 0;
248 /* Decide on how many pulses to find at once */
249 pulsesAtOnce = (pulsesLeft*N_1)>>9; /* pulsesLeft/N */
253 rshift = yshift+1+celt_ilog2(K-pulsesLeft+pulsesAtOnce);
255 magnitude = SHL16(pulsesAtOnce, yshift);
258 /* The squared magnitude term gets added anyway, so we might as well
259 add it outside the loop */
260 yy = MAC16_16(yy, magnitude,magnitude);
261 /* Choose between fast and accurate strategy depending on where we are in the search */
262 /* This should ensure that anything we can process will have a better score */
265 celt_word16_t Rxy, Ryy;
266 /* Select sign based on X[j] alone */
268 /* Temporary sums of the new pulse(s) */
269 Rxy = EXTRACT16(SHR32(MAC16_16(xy, s,X[j]),rshift));
270 /* We're multiplying y[j] by two so we don't have to do it here */
271 Ryy = EXTRACT16(SHR32(MAC16_16(yy, s,y[j]),rshift));
273 /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that
274 Rxy is positive because the sign is pre-computed) */
275 Rxy = MULT16_16_Q15(Rxy,Rxy);
276 /* The idea is to check for num/den >= best_num/best_den, but that way
277 we can do it without any division */
278 /* OPT: Make sure to use conditional moves here */
279 if (MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num))
289 s = SHL16(is, yshift);
291 /* Updating the sums of the new pulse(s) */
292 xy = xy + MULT16_16(s,X[j]);
293 /* We're multiplying y[j] by two so we don't have to do it here */
294 yy = yy + MULT16_16(s,y[j]);
295 yp = yp + MULT16_16(s, P[j]);
297 /* Only now that we've made the final choice, update y/iy */
298 /* Multiplying y[j] by 2 so we don't have to do it everywhere else */
301 pulsesLeft -= pulsesAtOnce;
307 celt_word16_t best_num = -VERY_LARGE16;
308 celt_word16_t best_den = 0;
310 celt_word16_t magnitude = SHL16(1, yshift);
312 /* The squared magnitude term gets added anyway, so we might as well
313 add it outside the loop */
314 yy = MAC16_16(yy, magnitude,magnitude);
317 celt_word16_t Rxy, Ryy, Ryp;
319 /* Select sign based on X[j] alone */
321 /* Temporary sums of the new pulse(s) */
322 Rxy = ROUND16(MAC16_16(xy, s,X[j]), 14);
323 /* We're multiplying y[j] by two so we don't have to do it here */
324 Ryy = ROUND16(MAC16_16(yy, s,y[j]), 14);
325 Ryp = ROUND16(MAC16_16(yp, s,P[j]), 14);
327 /* Compute the gain such that ||p + g*y|| = 1
328 ...but instead, we compute g*Ryy to avoid dividing */
329 g = celt_psqrt(MULT16_16(Ryp,Ryp) + MULT16_16(Ryy,QCONST16(1.f,14)-Rpp)) - Ryp;
330 /* Knowing that gain, what's the error: (x-g*y)^2
331 (result is negated and we discard x^2 because it's constant) */
332 /* score = 2*g*Rxy - g*g*Ryy;*/
334 /* No need to multiply Rxy by 2 because we did it earlier */
335 num = MULT16_16_Q15(ADD16(SUB16(Rxy,g),Rxy),g);
339 if (MULT16_16(best_den, num) > MULT16_16(Ryy, best_num))
350 P[j] = MULT16_16(signx[j],P[j]);
351 X[j] = MULT16_16(signx[j],X[j]);
355 encode_pulses(iy, N, K, enc);
357 /* Recompute the gain in one pass to reduce the encoder-decoder mismatch
358 due to the recursive computation used in quantisation. */
359 mix_pitch_and_residual(iy, X, N, K, P);
361 exp_rotation(X, N, -1, spread, K);
366 /** Decode pulse vector and combine the result with the pitch vector to produce
367 the final normalised signal in the current band. */
368 void alg_unquant(celt_norm_t *X, int N, int K, int spread, celt_norm_t *P, ec_dec *dec)
374 decode_pulses(iy, N, K, dec);
375 mix_pitch_and_residual(iy, X, N, K, P);
377 exp_rotation(X, N, -1, spread, K);
381 celt_word16_t renormalise_vector(celt_norm_t *X, celt_word16_t value, int N, int stride)
384 celt_word32_t E = EPSILON;
387 celt_norm_t *xptr = X;
390 E = MAC16_16(E, *xptr, *xptr);
400 g = MULT16_16_Q15(value,celt_rcp(SHL32(rE,9)));
404 *xptr = PSHR32(MULT16_16(g, *xptr),8);
410 static void fold(const CELTMode *m, int N, celt_norm_t *Y, celt_norm_t * restrict P, int N0, int B)
413 const int C = CHANNELS(m);
414 int id = (N0*C) % (C*B);
415 /* Here, we assume that id will never be greater than N0, i.e. that
416 no band is wider than N0. In the unlikely case it happens, we set
417 everything to zero */
419 int offset = (N0*C - (id+C*N))/2;
420 if (offset > C*N0/16)
422 offset -= offset % (C*B);
425 //printf ("%d\n", offset);
436 void intra_fold(const CELTMode *m, celt_norm_t * restrict x, int N, int *pulses, celt_norm_t *Y, celt_norm_t * restrict P, int N0, int B)
439 const int C = CHANNELS(m);
441 fold(m, N, Y, P, N0, B);
444 int K = get_pulses(pulses[c]);
445 renormalise_vector(P+c, K==0 ? Q15ONE : 0, N, C);