1 /* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
14 - Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
17 - Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include "os_support.h"
41 #include "stack_alloc.h"
45 static void find_best_pitch(opus_val32 *xcorr, opus_val16 *y, int len,
46 int max_pitch, int *best_pitch
48 , int yshift, opus_val32 maxcorr
54 opus_val16 best_num[2];
55 opus_val32 best_den[2];
59 xshift = celt_ilog2(maxcorr)-14;
69 Syy = ADD32(Syy, SHR32(MULT16_16(y[j],y[j]), yshift));
70 for (i=0;i<max_pitch;i++)
76 xcorr16 = EXTRACT16(VSHR32(xcorr[i], xshift));
78 /* Considering the range of xcorr16, this should avoid both underflows
79 and overflows (inf) when squaring xcorr16 */
82 num = MULT16_16_Q15(xcorr16,xcorr16);
83 if (MULT16_32_Q15(num,best_den[1]) > MULT16_32_Q15(best_num[1],Syy))
85 if (MULT16_32_Q15(num,best_den[0]) > MULT16_32_Q15(best_num[0],Syy))
87 best_num[1] = best_num[0];
88 best_den[1] = best_den[0];
89 best_pitch[1] = best_pitch[0];
100 Syy += SHR32(MULT16_16(y[i+len],y[i+len]),yshift) - SHR32(MULT16_16(y[i],y[i]),yshift);
105 void pitch_downsample(celt_sig * restrict x[], opus_val16 * restrict x_lp,
110 opus_val16 tmp=Q15ONE;
111 opus_val16 lpc[4], mem[4]={0,0,0,0};
114 opus_val32 maxabs = celt_maxabs32(x[0], len);
117 opus_val32 maxabs_1 = celt_maxabs32(x[1], len);
118 maxabs = MAX32(maxabs, maxabs_1);
122 shift = celt_ilog2(maxabs)-10;
128 for (i=1;i<len>>1;i++)
129 x_lp[i] = SHR32(HALF32(HALF32(x[0][(2*i-1)]+x[0][(2*i+1)])+x[0][2*i]), shift);
130 x_lp[0] = SHR32(HALF32(HALF32(x[0][1])+x[0][0]), shift);
133 for (i=1;i<len>>1;i++)
134 x_lp[i] += SHR32(HALF32(HALF32(x[1][(2*i-1)]+x[1][(2*i+1)])+x[1][2*i]), shift);
135 x_lp[0] += SHR32(HALF32(HALF32(x[1][1])+x[1][0]), shift);
138 _celt_autocorr(x_lp, ac, NULL, 0,
141 /* Noise floor -40 dB */
143 ac[0] += SHR32(ac[0],13);
150 /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
152 ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
154 ac[i] -= ac[i]*(.008f*i)*(.008f*i);
158 _celt_lpc(lpc, ac, 4);
161 tmp = MULT16_16_Q15(QCONST16(.9f,15), tmp);
162 lpc[i] = MULT16_16_Q15(lpc[i], tmp);
164 celt_fir(x_lp, lpc, x_lp, len>>1, 4, mem);
167 lpc[0]=QCONST16(.8f,12);
168 celt_fir(x_lp, lpc, x_lp, len>>1, 1, mem);
172 void pitch_search(const opus_val16 * restrict x_lp, opus_val16 * restrict y,
173 int len, int max_pitch, int *pitch)
177 int best_pitch[2]={0,0};
178 VARDECL(opus_val16, x_lp4);
179 VARDECL(opus_val16, y_lp4);
180 VARDECL(opus_val32, xcorr);
182 opus_val32 maxcorr=1;
183 opus_val16 xmax, ymax;
191 celt_assert(max_pitch>0);
194 ALLOC(x_lp4, len>>2, opus_val16);
195 ALLOC(y_lp4, lag>>2, opus_val16);
196 ALLOC(xcorr, max_pitch>>1, opus_val32);
198 /* Downsample by 2 again */
199 for (j=0;j<len>>2;j++)
200 x_lp4[j] = x_lp[2*j];
201 for (j=0;j<lag>>2;j++)
205 xmax = celt_maxabs16(x_lp4, len>>2);
206 ymax = celt_maxabs16(y_lp4, lag>>2);
207 shift = celt_ilog2(MAX16(1, MAX16(xmax, ymax)))-11;
210 for (j=0;j<len>>2;j++)
211 x_lp4[j] = SHR16(x_lp4[j], shift);
212 for (j=0;j<lag>>2;j++)
213 y_lp4[j] = SHR16(y_lp4[j], shift);
214 /* Use double the shift for a MAC */
221 /* Coarse search with 4x decimation */
223 for (i=0;i<max_pitch>>2;i++)
226 for (j=0;j<len>>2;j++)
227 sum = MAC16_16(sum, x_lp4[j],y_lp4[i+j]);
228 xcorr[i] = MAX32(-1, sum);
230 maxcorr = MAX32(maxcorr, sum);
233 find_best_pitch(xcorr, y_lp4, len>>2, max_pitch>>2, best_pitch
239 /* Finer search with 2x decimation */
243 for (i=0;i<max_pitch>>1;i++)
247 if (abs(i-2*best_pitch[0])>2 && abs(i-2*best_pitch[1])>2)
249 for (j=0;j<len>>1;j++)
250 sum += SHR32(MULT16_16(x_lp[j],y[i+j]), shift);
251 xcorr[i] = MAX32(-1, sum);
253 maxcorr = MAX32(maxcorr, sum);
256 find_best_pitch(xcorr, y, len>>1, max_pitch>>1, best_pitch
262 /* Refine by pseudo-interpolation */
263 if (best_pitch[0]>0 && best_pitch[0]<(max_pitch>>1)-1)
266 a = xcorr[best_pitch[0]-1];
267 b = xcorr[best_pitch[0]];
268 c = xcorr[best_pitch[0]+1];
269 if ((c-a) > MULT16_32_Q15(QCONST16(.7f,15),b-a))
271 else if ((a-c) > MULT16_32_Q15(QCONST16(.7f,15),b-c))
278 *pitch = 2*best_pitch[0]-offset;
283 static const int second_check[16] = {0, 0, 3, 2, 3, 2, 5, 2, 3, 2, 3, 2, 5, 2, 3, 2};
284 opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod,
285 int N, int *T0_, int prev_period, opus_val16 prev_gain)
292 opus_val32 best_xy, best_yy;
296 minperiod0 = minperiod;
310 xy = MAC16_16(xy, x[i], x[i-T0]);
311 xx = MAC16_16(xx, x[i], x[i]);
312 yy = MAC16_16(yy, x[i-T0],x[i-T0]);
320 x2y2 = 1+HALF32(MULT32_32_Q31(xx,yy));
321 sh = celt_ilog2(x2y2)>>1;
322 t = VSHR32(x2y2, 2*(sh-7));
323 g = g0 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
326 g = g0 = xy/celt_sqrt(1+xx*yy);
328 /* Look for any pitch at T/k */
338 /* Look for another strong correlation at T1b */
347 T1b = (2*second_check[k]*T0+k)/(2*k);
352 xy = MAC16_16(xy, x[i], x[i-T1]);
353 yy = MAC16_16(yy, x[i-T1], x[i-T1]);
355 xy = MAC16_16(xy, x[i], x[i-T1b]);
356 yy = MAC16_16(yy, x[i-T1b], x[i-T1b]);
362 x2y2 = 1+MULT32_32_Q31(xx,yy);
363 sh = celt_ilog2(x2y2)>>1;
364 t = VSHR32(x2y2, 2*(sh-7));
365 g1 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
368 g1 = xy/celt_sqrt(1+2.f*xx*1.f*yy);
370 if (abs(T1-prev_period)<=1)
372 else if (abs(T1-prev_period)<=2 && 5*k*k < T0)
373 cont = HALF32(prev_gain);
376 thresh = MAX16(QCONST16(.3f,15), MULT16_16_Q15(QCONST16(.7,15),g0)-cont);
377 /* Bias against very high pitch (very short period) to avoid false-positives
378 due to short-term correlation */
380 thresh = MAX16(QCONST16(.4f,15), MULT16_16_Q15(QCONST16(.85,15),g0)-cont);
381 else if (T1<2*minperiod)
382 thresh = MAX16(QCONST16(.5f,15), MULT16_16_Q15(QCONST16(.9,15),g0)-cont);
391 best_xy = MAX32(0, best_xy);
392 if (best_yy <= best_xy)
395 pg = SHR32(frac_div32(best_xy,best_yy+1),16);
402 xy = MAC16_16(xy, x[i], x[i-T1]);
405 if ((xcorr[2]-xcorr[0]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[0]))
407 else if ((xcorr[0]-xcorr[2]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[2]))