/* Copyright (c) 2007-2008 CSIRO Copyright (c) 2007-2009 Xiph.Org Foundation Written by Jean-Marc Valin */ /* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of the Xiph.org Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "mathops.h" #include "cwrs.h" #include "vq.h" #include "arch.h" #include "os_support.h" #include "rate.h" #ifndef M_PI #define M_PI 3.141592653 #endif static void exp_rotation(celt_norm *X, int len, int dir, int stride, int K) { int i, k, iter; celt_word16 c, s; celt_word16 gain, theta; celt_norm *Xptr; gain = celt_div((celt_word32)MULT16_16(Q15_ONE,len),(celt_word32)(3+len+6*K)); /* FIXME: Make that HALF16 instead of HALF32 */ theta = SUB16(Q15ONE, HALF32(MULT16_16_Q15(gain,gain))); /*if (len==30) { for (i=0;i 8*stride) stride *= len/(8*stride); iter = 1; for (k=0;k=0;i--) { celt_norm x1, x2; x1 = Xptr[0]; x2 = Xptr[stride]; Xptr[stride] = MULT16_16_Q15(c,x2) + MULT16_16_Q15(s,x1); *Xptr-- = MULT16_16_Q15(c,x1) - MULT16_16_Q15(s,x2); } } /*if (len==30) { for (i=0;i>1; #endif t = VSHR32(Ryy, (k-7)<<1); g = celt_rsqrt_norm(t); i=0; do X[i] = EXTRACT16(PSHR32(MULT16_16(g, iy[i]), k+1)); while (++i < N); } void alg_quant(celt_norm *X, int N, int K, int spread, ec_enc *enc) { VARDECL(celt_norm, y); VARDECL(int, iy); VARDECL(celt_word16, signx); int j, is; celt_word16 s; int pulsesLeft; celt_word32 sum; celt_word32 xy, yy; int N_1; /* Inverse of N, in Q14 format (even for float) */ #ifdef FIXED_POINT int yshift; #endif SAVE_STACK; K = get_pulses(K); #ifdef FIXED_POINT yshift = 13-celt_ilog2(K); #endif ALLOC(y, N, celt_norm); ALLOC(iy, N, int); ALLOC(signx, N, celt_word16); N_1 = 512/N; if (spread) exp_rotation(X, N, 1, spread, K); sum = 0; j=0; do { if (X[j]>0) signx[j]=1; else { signx[j]=-1; X[j]=-X[j]; } iy[j] = 0; y[j] = 0; } while (++j (N>>1)) { celt_word16 rcp; j=0; do { sum += X[j]; } while (++j=1, "Allocated too many pulses in the quick pass"); while (pulsesLeft > 0) { int pulsesAtOnce=1; int best_id; celt_word16 magnitude; celt_word32 best_num = -VERY_LARGE16; celt_word16 best_den = 0; #ifdef FIXED_POINT int rshift; #endif /* Decide on how many pulses to find at once */ pulsesAtOnce = (pulsesLeft*N_1)>>9; /* pulsesLeft/N */ if (pulsesAtOnce<1) pulsesAtOnce = 1; #ifdef FIXED_POINT rshift = yshift+1+celt_ilog2(K-pulsesLeft+pulsesAtOnce); #endif magnitude = SHL16(pulsesAtOnce, yshift); best_id = 0; /* The squared magnitude term gets added anyway, so we might as well add it outside the loop */ yy = MAC16_16(yy, magnitude,magnitude); /* Choose between fast and accurate strategy depending on where we are in the search */ /* This should ensure that anything we can process will have a better score */ j=0; do { celt_word16 Rxy, Ryy; /* Select sign based on X[j] alone */ s = magnitude; /* Temporary sums of the new pulse(s) */ Rxy = EXTRACT16(SHR32(MAC16_16(xy, s,X[j]),rshift)); /* We're multiplying y[j] by two so we don't have to do it here */ Ryy = EXTRACT16(SHR32(MAC16_16(yy, s,y[j]),rshift)); /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that Rxy is positive because the sign is pre-computed) */ Rxy = MULT16_16_Q15(Rxy,Rxy); /* The idea is to check for num/den >= best_num/best_den, but that way we can do it without any division */ /* OPT: Make sure to use conditional moves here */ if (MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num)) { best_den = Ryy; best_num = Rxy; best_id = j; } } while (++j C*N0/16) offset = C*N0/16; offset -= offset % (C*B); if (offset < 0) offset = 0; //printf ("%d\n", offset); id += offset; }*/ if (id+N>N0) for (j=0;j