media/libopus/celt/pitch.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libopus/celt/pitch.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,537 @@
     1.4 +/* Copyright (c) 2007-2008 CSIRO
     1.5 +   Copyright (c) 2007-2009 Xiph.Org Foundation
     1.6 +   Written by Jean-Marc Valin */
     1.7 +/**
     1.8 +   @file pitch.c
     1.9 +   @brief Pitch analysis
    1.10 + */
    1.11 +
    1.12 +/*
    1.13 +   Redistribution and use in source and binary forms, with or without
    1.14 +   modification, are permitted provided that the following conditions
    1.15 +   are met:
    1.16 +
    1.17 +   - Redistributions of source code must retain the above copyright
    1.18 +   notice, this list of conditions and the following disclaimer.
    1.19 +
    1.20 +   - Redistributions in binary form must reproduce the above copyright
    1.21 +   notice, this list of conditions and the following disclaimer in the
    1.22 +   documentation and/or other materials provided with the distribution.
    1.23 +
    1.24 +   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.25 +   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.26 +   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.27 +   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
    1.28 +   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.29 +   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.30 +   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.31 +   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    1.32 +   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.33 +   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.34 +   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.35 +*/
    1.36 +
    1.37 +#ifdef HAVE_CONFIG_H
    1.38 +#include "config.h"
    1.39 +#endif
    1.40 +
    1.41 +#include "pitch.h"
    1.42 +#include "os_support.h"
    1.43 +#include "modes.h"
    1.44 +#include "stack_alloc.h"
    1.45 +#include "mathops.h"
    1.46 +#include "celt_lpc.h"
    1.47 +
    1.48 +static void find_best_pitch(opus_val32 *xcorr, opus_val16 *y, int len,
    1.49 +                            int max_pitch, int *best_pitch
    1.50 +#ifdef FIXED_POINT
    1.51 +                            , int yshift, opus_val32 maxcorr
    1.52 +#endif
    1.53 +                            )
    1.54 +{
    1.55 +   int i, j;
    1.56 +   opus_val32 Syy=1;
    1.57 +   opus_val16 best_num[2];
    1.58 +   opus_val32 best_den[2];
    1.59 +#ifdef FIXED_POINT
    1.60 +   int xshift;
    1.61 +
    1.62 +   xshift = celt_ilog2(maxcorr)-14;
    1.63 +#endif
    1.64 +
    1.65 +   best_num[0] = -1;
    1.66 +   best_num[1] = -1;
    1.67 +   best_den[0] = 0;
    1.68 +   best_den[1] = 0;
    1.69 +   best_pitch[0] = 0;
    1.70 +   best_pitch[1] = 1;
    1.71 +   for (j=0;j<len;j++)
    1.72 +      Syy = ADD32(Syy, SHR32(MULT16_16(y[j],y[j]), yshift));
    1.73 +   for (i=0;i<max_pitch;i++)
    1.74 +   {
    1.75 +      if (xcorr[i]>0)
    1.76 +      {
    1.77 +         opus_val16 num;
    1.78 +         opus_val32 xcorr16;
    1.79 +         xcorr16 = EXTRACT16(VSHR32(xcorr[i], xshift));
    1.80 +#ifndef FIXED_POINT
    1.81 +         /* Considering the range of xcorr16, this should avoid both underflows
    1.82 +            and overflows (inf) when squaring xcorr16 */
    1.83 +         xcorr16 *= 1e-12f;
    1.84 +#endif
    1.85 +         num = MULT16_16_Q15(xcorr16,xcorr16);
    1.86 +         if (MULT16_32_Q15(num,best_den[1]) > MULT16_32_Q15(best_num[1],Syy))
    1.87 +         {
    1.88 +            if (MULT16_32_Q15(num,best_den[0]) > MULT16_32_Q15(best_num[0],Syy))
    1.89 +            {
    1.90 +               best_num[1] = best_num[0];
    1.91 +               best_den[1] = best_den[0];
    1.92 +               best_pitch[1] = best_pitch[0];
    1.93 +               best_num[0] = num;
    1.94 +               best_den[0] = Syy;
    1.95 +               best_pitch[0] = i;
    1.96 +            } else {
    1.97 +               best_num[1] = num;
    1.98 +               best_den[1] = Syy;
    1.99 +               best_pitch[1] = i;
   1.100 +            }
   1.101 +         }
   1.102 +      }
   1.103 +      Syy += SHR32(MULT16_16(y[i+len],y[i+len]),yshift) - SHR32(MULT16_16(y[i],y[i]),yshift);
   1.104 +      Syy = MAX32(1, Syy);
   1.105 +   }
   1.106 +}
   1.107 +
   1.108 +static void celt_fir5(const opus_val16 *x,
   1.109 +         const opus_val16 *num,
   1.110 +         opus_val16 *y,
   1.111 +         int N,
   1.112 +         opus_val16 *mem)
   1.113 +{
   1.114 +   int i;
   1.115 +   opus_val16 num0, num1, num2, num3, num4;
   1.116 +   opus_val32 mem0, mem1, mem2, mem3, mem4;
   1.117 +   num0=num[0];
   1.118 +   num1=num[1];
   1.119 +   num2=num[2];
   1.120 +   num3=num[3];
   1.121 +   num4=num[4];
   1.122 +   mem0=mem[0];
   1.123 +   mem1=mem[1];
   1.124 +   mem2=mem[2];
   1.125 +   mem3=mem[3];
   1.126 +   mem4=mem[4];
   1.127 +   for (i=0;i<N;i++)
   1.128 +   {
   1.129 +      opus_val32 sum = SHL32(EXTEND32(x[i]), SIG_SHIFT);
   1.130 +      sum = MAC16_16(sum,num0,mem0);
   1.131 +      sum = MAC16_16(sum,num1,mem1);
   1.132 +      sum = MAC16_16(sum,num2,mem2);
   1.133 +      sum = MAC16_16(sum,num3,mem3);
   1.134 +      sum = MAC16_16(sum,num4,mem4);
   1.135 +      mem4 = mem3;
   1.136 +      mem3 = mem2;
   1.137 +      mem2 = mem1;
   1.138 +      mem1 = mem0;
   1.139 +      mem0 = x[i];
   1.140 +      y[i] = ROUND16(sum, SIG_SHIFT);
   1.141 +   }
   1.142 +   mem[0]=mem0;
   1.143 +   mem[1]=mem1;
   1.144 +   mem[2]=mem2;
   1.145 +   mem[3]=mem3;
   1.146 +   mem[4]=mem4;
   1.147 +}
   1.148 +
   1.149 +
   1.150 +void pitch_downsample(celt_sig * OPUS_RESTRICT x[], opus_val16 * OPUS_RESTRICT x_lp,
   1.151 +      int len, int C, int arch)
   1.152 +{
   1.153 +   int i;
   1.154 +   opus_val32 ac[5];
   1.155 +   opus_val16 tmp=Q15ONE;
   1.156 +   opus_val16 lpc[4], mem[5]={0,0,0,0,0};
   1.157 +   opus_val16 lpc2[5];
   1.158 +   opus_val16 c1 = QCONST16(.8f,15);
   1.159 +#ifdef FIXED_POINT
   1.160 +   int shift;
   1.161 +   opus_val32 maxabs = celt_maxabs32(x[0], len);
   1.162 +   if (C==2)
   1.163 +   {
   1.164 +      opus_val32 maxabs_1 = celt_maxabs32(x[1], len);
   1.165 +      maxabs = MAX32(maxabs, maxabs_1);
   1.166 +   }
   1.167 +   if (maxabs<1)
   1.168 +      maxabs=1;
   1.169 +   shift = celt_ilog2(maxabs)-10;
   1.170 +   if (shift<0)
   1.171 +      shift=0;
   1.172 +   if (C==2)
   1.173 +      shift++;
   1.174 +#endif
   1.175 +   for (i=1;i<len>>1;i++)
   1.176 +      x_lp[i] = SHR32(HALF32(HALF32(x[0][(2*i-1)]+x[0][(2*i+1)])+x[0][2*i]), shift);
   1.177 +   x_lp[0] = SHR32(HALF32(HALF32(x[0][1])+x[0][0]), shift);
   1.178 +   if (C==2)
   1.179 +   {
   1.180 +      for (i=1;i<len>>1;i++)
   1.181 +         x_lp[i] += SHR32(HALF32(HALF32(x[1][(2*i-1)]+x[1][(2*i+1)])+x[1][2*i]), shift);
   1.182 +      x_lp[0] += SHR32(HALF32(HALF32(x[1][1])+x[1][0]), shift);
   1.183 +   }
   1.184 +
   1.185 +   _celt_autocorr(x_lp, ac, NULL, 0,
   1.186 +                  4, len>>1, arch);
   1.187 +
   1.188 +   /* Noise floor -40 dB */
   1.189 +#ifdef FIXED_POINT
   1.190 +   ac[0] += SHR32(ac[0],13);
   1.191 +#else
   1.192 +   ac[0] *= 1.0001f;
   1.193 +#endif
   1.194 +   /* Lag windowing */
   1.195 +   for (i=1;i<=4;i++)
   1.196 +   {
   1.197 +      /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
   1.198 +#ifdef FIXED_POINT
   1.199 +      ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
   1.200 +#else
   1.201 +      ac[i] -= ac[i]*(.008f*i)*(.008f*i);
   1.202 +#endif
   1.203 +   }
   1.204 +
   1.205 +   _celt_lpc(lpc, ac, 4);
   1.206 +   for (i=0;i<4;i++)
   1.207 +   {
   1.208 +      tmp = MULT16_16_Q15(QCONST16(.9f,15), tmp);
   1.209 +      lpc[i] = MULT16_16_Q15(lpc[i], tmp);
   1.210 +   }
   1.211 +   /* Add a zero */
   1.212 +   lpc2[0] = lpc[0] + QCONST16(.8f,SIG_SHIFT);
   1.213 +   lpc2[1] = lpc[1] + MULT16_16_Q15(c1,lpc[0]);
   1.214 +   lpc2[2] = lpc[2] + MULT16_16_Q15(c1,lpc[1]);
   1.215 +   lpc2[3] = lpc[3] + MULT16_16_Q15(c1,lpc[2]);
   1.216 +   lpc2[4] = MULT16_16_Q15(c1,lpc[3]);
   1.217 +   celt_fir5(x_lp, lpc2, x_lp, len>>1, mem);
   1.218 +}
   1.219 +
   1.220 +#if 0 /* This is a simple version of the pitch correlation that should work
   1.221 +         well on DSPs like Blackfin and TI C5x/C6x */
   1.222 +
   1.223 +#ifdef FIXED_POINT
   1.224 +opus_val32
   1.225 +#else
   1.226 +void
   1.227 +#endif
   1.228 +celt_pitch_xcorr(opus_val16 *x, opus_val16 *y, opus_val32 *xcorr, int len, int max_pitch)
   1.229 +{
   1.230 +   int i, j;
   1.231 +#ifdef FIXED_POINT
   1.232 +   opus_val32 maxcorr=1;
   1.233 +#endif
   1.234 +   for (i=0;i<max_pitch;i++)
   1.235 +   {
   1.236 +      opus_val32 sum = 0;
   1.237 +      for (j=0;j<len;j++)
   1.238 +         sum = MAC16_16(sum, x[j],y[i+j]);
   1.239 +      xcorr[i] = sum;
   1.240 +#ifdef FIXED_POINT
   1.241 +      maxcorr = MAX32(maxcorr, sum);
   1.242 +#endif
   1.243 +   }
   1.244 +#ifdef FIXED_POINT
   1.245 +   return maxcorr;
   1.246 +#endif
   1.247 +}
   1.248 +
   1.249 +#else /* Unrolled version of the pitch correlation -- runs faster on x86 and ARM */
   1.250 +
   1.251 +#ifdef FIXED_POINT
   1.252 +opus_val32
   1.253 +#else
   1.254 +void
   1.255 +#endif
   1.256 +celt_pitch_xcorr_c(const opus_val16 *_x, const opus_val16 *_y, opus_val32 *xcorr, int len, int max_pitch)
   1.257 +{
   1.258 +   int i,j;
   1.259 +   /*The EDSP version requires that max_pitch is at least 1, and that _x is
   1.260 +      32-bit aligned.
   1.261 +     Since it's hard to put asserts in assembly, put them here.*/
   1.262 +   celt_assert(max_pitch>0);
   1.263 +   celt_assert((((unsigned char *)_x-(unsigned char *)NULL)&3)==0);
   1.264 +#ifdef FIXED_POINT
   1.265 +   opus_val32 maxcorr=1;
   1.266 +#endif
   1.267 +   for (i=0;i<max_pitch-3;i+=4)
   1.268 +   {
   1.269 +      opus_val32 sum[4]={0,0,0,0};
   1.270 +      xcorr_kernel(_x, _y+i, sum, len);
   1.271 +      xcorr[i]=sum[0];
   1.272 +      xcorr[i+1]=sum[1];
   1.273 +      xcorr[i+2]=sum[2];
   1.274 +      xcorr[i+3]=sum[3];
   1.275 +#ifdef FIXED_POINT
   1.276 +      sum[0] = MAX32(sum[0], sum[1]);
   1.277 +      sum[2] = MAX32(sum[2], sum[3]);
   1.278 +      sum[0] = MAX32(sum[0], sum[2]);
   1.279 +      maxcorr = MAX32(maxcorr, sum[0]);
   1.280 +#endif
   1.281 +   }
   1.282 +   /* In case max_pitch isn't a multiple of 4, do non-unrolled version. */
   1.283 +   for (;i<max_pitch;i++)
   1.284 +   {
   1.285 +      opus_val32 sum = 0;
   1.286 +      for (j=0;j<len;j++)
   1.287 +         sum = MAC16_16(sum, _x[j],_y[i+j]);
   1.288 +      xcorr[i] = sum;
   1.289 +#ifdef FIXED_POINT
   1.290 +      maxcorr = MAX32(maxcorr, sum);
   1.291 +#endif
   1.292 +   }
   1.293 +#ifdef FIXED_POINT
   1.294 +   return maxcorr;
   1.295 +#endif
   1.296 +}
   1.297 +
   1.298 +#endif
   1.299 +void pitch_search(const opus_val16 * OPUS_RESTRICT x_lp, opus_val16 * OPUS_RESTRICT y,
   1.300 +                  int len, int max_pitch, int *pitch, int arch)
   1.301 +{
   1.302 +   int i, j;
   1.303 +   int lag;
   1.304 +   int best_pitch[2]={0,0};
   1.305 +   VARDECL(opus_val16, x_lp4);
   1.306 +   VARDECL(opus_val16, y_lp4);
   1.307 +   VARDECL(opus_val32, xcorr);
   1.308 +#ifdef FIXED_POINT
   1.309 +   opus_val32 maxcorr;
   1.310 +   opus_val32 xmax, ymax;
   1.311 +   int shift=0;
   1.312 +#endif
   1.313 +   int offset;
   1.314 +
   1.315 +   SAVE_STACK;
   1.316 +
   1.317 +   celt_assert(len>0);
   1.318 +   celt_assert(max_pitch>0);
   1.319 +   lag = len+max_pitch;
   1.320 +
   1.321 +   ALLOC(x_lp4, len>>2, opus_val16);
   1.322 +   ALLOC(y_lp4, lag>>2, opus_val16);
   1.323 +   ALLOC(xcorr, max_pitch>>1, opus_val32);
   1.324 +
   1.325 +   /* Downsample by 2 again */
   1.326 +   for (j=0;j<len>>2;j++)
   1.327 +      x_lp4[j] = x_lp[2*j];
   1.328 +   for (j=0;j<lag>>2;j++)
   1.329 +      y_lp4[j] = y[2*j];
   1.330 +
   1.331 +#ifdef FIXED_POINT
   1.332 +   xmax = celt_maxabs16(x_lp4, len>>2);
   1.333 +   ymax = celt_maxabs16(y_lp4, lag>>2);
   1.334 +   shift = celt_ilog2(MAX32(1, MAX32(xmax, ymax)))-11;
   1.335 +   if (shift>0)
   1.336 +   {
   1.337 +      for (j=0;j<len>>2;j++)
   1.338 +         x_lp4[j] = SHR16(x_lp4[j], shift);
   1.339 +      for (j=0;j<lag>>2;j++)
   1.340 +         y_lp4[j] = SHR16(y_lp4[j], shift);
   1.341 +      /* Use double the shift for a MAC */
   1.342 +      shift *= 2;
   1.343 +   } else {
   1.344 +      shift = 0;
   1.345 +   }
   1.346 +#endif
   1.347 +
   1.348 +   /* Coarse search with 4x decimation */
   1.349 +
   1.350 +#ifdef FIXED_POINT
   1.351 +   maxcorr =
   1.352 +#endif
   1.353 +   celt_pitch_xcorr(x_lp4, y_lp4, xcorr, len>>2, max_pitch>>2, arch);
   1.354 +
   1.355 +   find_best_pitch(xcorr, y_lp4, len>>2, max_pitch>>2, best_pitch
   1.356 +#ifdef FIXED_POINT
   1.357 +                   , 0, maxcorr
   1.358 +#endif
   1.359 +                   );
   1.360 +
   1.361 +   /* Finer search with 2x decimation */
   1.362 +#ifdef FIXED_POINT
   1.363 +   maxcorr=1;
   1.364 +#endif
   1.365 +   for (i=0;i<max_pitch>>1;i++)
   1.366 +   {
   1.367 +      opus_val32 sum=0;
   1.368 +      xcorr[i] = 0;
   1.369 +      if (abs(i-2*best_pitch[0])>2 && abs(i-2*best_pitch[1])>2)
   1.370 +         continue;
   1.371 +      for (j=0;j<len>>1;j++)
   1.372 +         sum += SHR32(MULT16_16(x_lp[j],y[i+j]), shift);
   1.373 +      xcorr[i] = MAX32(-1, sum);
   1.374 +#ifdef FIXED_POINT
   1.375 +      maxcorr = MAX32(maxcorr, sum);
   1.376 +#endif
   1.377 +   }
   1.378 +   find_best_pitch(xcorr, y, len>>1, max_pitch>>1, best_pitch
   1.379 +#ifdef FIXED_POINT
   1.380 +                   , shift+1, maxcorr
   1.381 +#endif
   1.382 +                   );
   1.383 +
   1.384 +   /* Refine by pseudo-interpolation */
   1.385 +   if (best_pitch[0]>0 && best_pitch[0]<(max_pitch>>1)-1)
   1.386 +   {
   1.387 +      opus_val32 a, b, c;
   1.388 +      a = xcorr[best_pitch[0]-1];
   1.389 +      b = xcorr[best_pitch[0]];
   1.390 +      c = xcorr[best_pitch[0]+1];
   1.391 +      if ((c-a) > MULT16_32_Q15(QCONST16(.7f,15),b-a))
   1.392 +         offset = 1;
   1.393 +      else if ((a-c) > MULT16_32_Q15(QCONST16(.7f,15),b-c))
   1.394 +         offset = -1;
   1.395 +      else
   1.396 +         offset = 0;
   1.397 +   } else {
   1.398 +      offset = 0;
   1.399 +   }
   1.400 +   *pitch = 2*best_pitch[0]-offset;
   1.401 +
   1.402 +   RESTORE_STACK;
   1.403 +}
   1.404 +
   1.405 +static const int second_check[16] = {0, 0, 3, 2, 3, 2, 5, 2, 3, 2, 3, 2, 5, 2, 3, 2};
   1.406 +opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod,
   1.407 +      int N, int *T0_, int prev_period, opus_val16 prev_gain)
   1.408 +{
   1.409 +   int k, i, T, T0;
   1.410 +   opus_val16 g, g0;
   1.411 +   opus_val16 pg;
   1.412 +   opus_val32 xy,xx,yy,xy2;
   1.413 +   opus_val32 xcorr[3];
   1.414 +   opus_val32 best_xy, best_yy;
   1.415 +   int offset;
   1.416 +   int minperiod0;
   1.417 +   VARDECL(opus_val32, yy_lookup);
   1.418 +   SAVE_STACK;
   1.419 +
   1.420 +   minperiod0 = minperiod;
   1.421 +   maxperiod /= 2;
   1.422 +   minperiod /= 2;
   1.423 +   *T0_ /= 2;
   1.424 +   prev_period /= 2;
   1.425 +   N /= 2;
   1.426 +   x += maxperiod;
   1.427 +   if (*T0_>=maxperiod)
   1.428 +      *T0_=maxperiod-1;
   1.429 +
   1.430 +   T = T0 = *T0_;
   1.431 +   ALLOC(yy_lookup, maxperiod+1, opus_val32);
   1.432 +   dual_inner_prod(x, x, x-T0, N, &xx, &xy);
   1.433 +   yy_lookup[0] = xx;
   1.434 +   yy=xx;
   1.435 +   for (i=1;i<=maxperiod;i++)
   1.436 +   {
   1.437 +      yy = yy+MULT16_16(x[-i],x[-i])-MULT16_16(x[N-i],x[N-i]);
   1.438 +      yy_lookup[i] = MAX32(0, yy);
   1.439 +   }
   1.440 +   yy = yy_lookup[T0];
   1.441 +   best_xy = xy;
   1.442 +   best_yy = yy;
   1.443 +#ifdef FIXED_POINT
   1.444 +      {
   1.445 +         opus_val32 x2y2;
   1.446 +         int sh, t;
   1.447 +         x2y2 = 1+HALF32(MULT32_32_Q31(xx,yy));
   1.448 +         sh = celt_ilog2(x2y2)>>1;
   1.449 +         t = VSHR32(x2y2, 2*(sh-7));
   1.450 +         g = g0 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
   1.451 +      }
   1.452 +#else
   1.453 +      g = g0 = xy/celt_sqrt(1+xx*yy);
   1.454 +#endif
   1.455 +   /* Look for any pitch at T/k */
   1.456 +   for (k=2;k<=15;k++)
   1.457 +   {
   1.458 +      int T1, T1b;
   1.459 +      opus_val16 g1;
   1.460 +      opus_val16 cont=0;
   1.461 +      opus_val16 thresh;
   1.462 +      T1 = (2*T0+k)/(2*k);
   1.463 +      if (T1 < minperiod)
   1.464 +         break;
   1.465 +      /* Look for another strong correlation at T1b */
   1.466 +      if (k==2)
   1.467 +      {
   1.468 +         if (T1+T0>maxperiod)
   1.469 +            T1b = T0;
   1.470 +         else
   1.471 +            T1b = T0+T1;
   1.472 +      } else
   1.473 +      {
   1.474 +         T1b = (2*second_check[k]*T0+k)/(2*k);
   1.475 +      }
   1.476 +      dual_inner_prod(x, &x[-T1], &x[-T1b], N, &xy, &xy2);
   1.477 +      xy += xy2;
   1.478 +      yy = yy_lookup[T1] + yy_lookup[T1b];
   1.479 +#ifdef FIXED_POINT
   1.480 +      {
   1.481 +         opus_val32 x2y2;
   1.482 +         int sh, t;
   1.483 +         x2y2 = 1+MULT32_32_Q31(xx,yy);
   1.484 +         sh = celt_ilog2(x2y2)>>1;
   1.485 +         t = VSHR32(x2y2, 2*(sh-7));
   1.486 +         g1 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
   1.487 +      }
   1.488 +#else
   1.489 +      g1 = xy/celt_sqrt(1+2.f*xx*1.f*yy);
   1.490 +#endif
   1.491 +      if (abs(T1-prev_period)<=1)
   1.492 +         cont = prev_gain;
   1.493 +      else if (abs(T1-prev_period)<=2 && 5*k*k < T0)
   1.494 +         cont = HALF32(prev_gain);
   1.495 +      else
   1.496 +         cont = 0;
   1.497 +      thresh = MAX16(QCONST16(.3f,15), MULT16_16_Q15(QCONST16(.7f,15),g0)-cont);
   1.498 +      /* Bias against very high pitch (very short period) to avoid false-positives
   1.499 +         due to short-term correlation */
   1.500 +      if (T1<3*minperiod)
   1.501 +         thresh = MAX16(QCONST16(.4f,15), MULT16_16_Q15(QCONST16(.85f,15),g0)-cont);
   1.502 +      else if (T1<2*minperiod)
   1.503 +         thresh = MAX16(QCONST16(.5f,15), MULT16_16_Q15(QCONST16(.9f,15),g0)-cont);
   1.504 +      if (g1 > thresh)
   1.505 +      {
   1.506 +         best_xy = xy;
   1.507 +         best_yy = yy;
   1.508 +         T = T1;
   1.509 +         g = g1;
   1.510 +      }
   1.511 +   }
   1.512 +   best_xy = MAX32(0, best_xy);
   1.513 +   if (best_yy <= best_xy)
   1.514 +      pg = Q15ONE;
   1.515 +   else
   1.516 +      pg = SHR32(frac_div32(best_xy,best_yy+1),16);
   1.517 +
   1.518 +   for (k=0;k<3;k++)
   1.519 +   {
   1.520 +      int T1 = T+k-1;
   1.521 +      xy = 0;
   1.522 +      for (i=0;i<N;i++)
   1.523 +         xy = MAC16_16(xy, x[i], x[i-T1]);
   1.524 +      xcorr[k] = xy;
   1.525 +   }
   1.526 +   if ((xcorr[2]-xcorr[0]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[0]))
   1.527 +      offset = 1;
   1.528 +   else if ((xcorr[0]-xcorr[2]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[2]))
   1.529 +      offset = -1;
   1.530 +   else
   1.531 +      offset = 0;
   1.532 +   if (pg > g)
   1.533 +      pg = g;
   1.534 +   *T0_ = 2*T+offset;
   1.535 +
   1.536 +   if (*T0_<minperiod0)
   1.537 +      *T0_=minperiod0;
   1.538 +   RESTORE_STACK;
   1.539 +   return pg;
   1.540 +}

mercurial