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 FOUNDATION OR
25 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 /* Always enable postfilter for Opus */
40 #if defined(OPUS_BUILD) && !defined(ENABLE_POSTFILTER)
41 #define ENABLE_POSTFILTER
45 #include "os_support.h"
47 #include "stack_alloc.h"
50 static void find_best_pitch(celt_word32 *xcorr, celt_word32 maxcorr, celt_word16 *y,
51 int yshift, int len, int max_pitch, int best_pitch[2])
55 celt_word16 best_num[2];
56 celt_word32 best_den[2];
60 xshift = celt_ilog2(maxcorr)-14;
70 Syy = MAC16_16(Syy, y[j],y[j]);
71 for (i=0;i<max_pitch;i++)
77 xcorr16 = EXTRACT16(VSHR32(xcorr[i], xshift));
78 num = MULT16_16_Q15(xcorr16,xcorr16);
79 if (MULT16_32_Q15(num,best_den[1]) > MULT16_32_Q15(best_num[1],Syy))
81 if (MULT16_32_Q15(num,best_den[0]) > MULT16_32_Q15(best_num[0],Syy))
83 best_num[1] = best_num[0];
84 best_den[1] = best_den[0];
85 best_pitch[1] = best_pitch[0];
96 Syy += SHR32(MULT16_16(y[i+len],y[i+len]),yshift) - SHR32(MULT16_16(y[i],y[i]),yshift);
102 void pitch_downsample(celt_sig * restrict x[], celt_word16 * restrict x_lp,
107 celt_word16 tmp=Q15ONE;
108 celt_word16 lpc[4], mem[4]={0,0,0,0};
109 const int C = CHANNELS(_C);
110 for (i=1;i<len>>1;i++)
111 x_lp[i] = SHR32(HALF32(HALF32(x[0][(2*i-1)]+x[0][(2*i+1)])+x[0][2*i]), SIG_SHIFT+3);
112 x_lp[0] = SHR32(HALF32(HALF32(x[0][1])+x[0][0]), SIG_SHIFT+3);
115 for (i=1;i<len>>1;i++)
116 x_lp[i] += SHR32(HALF32(HALF32(x[1][(2*i-1)]+x[1][(2*i+1)])+x[1][2*i]), SIG_SHIFT+3);
117 x_lp[0] += SHR32(HALF32(HALF32(x[1][1])+x[1][0]), SIG_SHIFT+3);
120 _celt_autocorr(x_lp, ac, NULL, 0,
123 /* Noise floor -40 dB */
125 ac[0] += SHR32(ac[0],13);
132 /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
134 ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
136 ac[i] -= ac[i]*(.008f*i)*(.008f*i);
140 _celt_lpc(lpc, ac, 4);
143 tmp = MULT16_16_Q15(QCONST16(.9f,15), tmp);
144 lpc[i] = MULT16_16_Q15(lpc[i], tmp);
146 fir(x_lp, lpc, x_lp, len>>1, 4, mem);
149 lpc[0]=QCONST16(.8f,12);
150 fir(x_lp, lpc, x_lp, len>>1, 1, mem);
154 void pitch_search(const celt_word16 * restrict x_lp, celt_word16 * restrict y,
155 int len, int max_pitch, int *pitch)
159 int best_pitch[2]={0};
160 VARDECL(celt_word16, x_lp4);
161 VARDECL(celt_word16, y_lp4);
162 VARDECL(celt_word32, xcorr);
163 celt_word32 maxcorr=1;
171 ALLOC(x_lp4, len>>2, celt_word16);
172 ALLOC(y_lp4, lag>>2, celt_word16);
173 ALLOC(xcorr, max_pitch>>1, celt_word32);
175 /* Downsample by 2 again */
176 for (j=0;j<len>>2;j++)
177 x_lp4[j] = x_lp[2*j];
178 for (j=0;j<lag>>2;j++)
182 shift = celt_ilog2(MAX16(1, MAX16(celt_maxabs16(x_lp4, len>>2), celt_maxabs16(y_lp4, lag>>2))))-11;
185 for (j=0;j<len>>2;j++)
186 x_lp4[j] = SHR16(x_lp4[j], shift);
187 for (j=0;j<lag>>2;j++)
188 y_lp4[j] = SHR16(y_lp4[j], shift);
189 /* Use double the shift for a MAC */
196 /* Coarse search with 4x decimation */
198 for (i=0;i<max_pitch>>2;i++)
201 for (j=0;j<len>>2;j++)
202 sum = MAC16_16(sum, x_lp4[j],y_lp4[i+j]);
203 xcorr[i] = MAX32(-1, sum);
204 maxcorr = MAX32(maxcorr, sum);
206 find_best_pitch(xcorr, maxcorr, y_lp4, 0, len>>2, max_pitch>>2, best_pitch);
208 /* Finer search with 2x decimation */
210 for (i=0;i<max_pitch>>1;i++)
214 if (abs(i-2*best_pitch[0])>2 && abs(i-2*best_pitch[1])>2)
216 for (j=0;j<len>>1;j++)
217 sum += SHR32(MULT16_16(x_lp[j],y[i+j]), shift);
218 xcorr[i] = MAX32(-1, sum);
219 maxcorr = MAX32(maxcorr, sum);
221 find_best_pitch(xcorr, maxcorr, y, shift, len>>1, max_pitch>>1, best_pitch);
223 /* Refine by pseudo-interpolation */
224 if (best_pitch[0]>0 && best_pitch[0]<(max_pitch>>1)-1)
227 a = xcorr[best_pitch[0]-1];
228 b = xcorr[best_pitch[0]];
229 c = xcorr[best_pitch[0]+1];
230 if ((c-a) > MULT16_32_Q15(QCONST16(.7f,15),b-a))
232 else if ((a-c) > MULT16_32_Q15(QCONST16(.7f,15),b-c))
239 *pitch = 2*best_pitch[0]-offset;
244 #ifdef ENABLE_POSTFILTER
245 static const int second_check[16] = {0, 0, 3, 2, 3, 2, 5, 2, 3, 2, 3, 2, 5, 2, 3, 2};
246 celt_word16 remove_doubling(celt_word16 *x, int maxperiod, int minperiod,
247 int N, int *_T0, int prev_period, celt_word16 prev_gain)
252 celt_word32 xy,xx,yy;
253 celt_word32 xcorr[3];
254 celt_word32 best_xy, best_yy;
258 minperiod0 = minperiod;
272 xy = MAC16_16(xy, x[i], x[i-T0]);
273 xx = MAC16_16(xx, x[i], x[i]);
274 yy = MAC16_16(yy, x[i-T0],x[i-T0]);
282 x2y2 = 1+HALF32(MULT32_32_Q31(xx,yy));
283 sh = celt_ilog2(x2y2)>>1;
284 t = VSHR32(x2y2, 2*(sh-7));
285 g = g0 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
288 g = g0 = xy/sqrt(1+xx*yy);
290 /* Look for any pitch at T/k */
299 /* Look for another strong correlation at T1b */
308 T1b = (2*second_check[k]*T0+k)/(2*k);
313 xy = MAC16_16(xy, x[i], x[i-T1]);
314 yy = MAC16_16(yy, x[i-T1], x[i-T1]);
316 xy = MAC16_16(xy, x[i], x[i-T1b]);
317 yy = MAC16_16(yy, x[i-T1b], x[i-T1b]);
323 x2y2 = 1+MULT32_32_Q31(xx,yy);
324 sh = celt_ilog2(x2y2)>>1;
325 t = VSHR32(x2y2, 2*(sh-7));
326 g1 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
329 g1 = xy/sqrt(1+2.f*xx*1.f*yy);
331 if (abs(T1-prev_period)<=1)
333 else if (abs(T1-prev_period)<=2 && 5*k*k < T0)
334 cont = HALF32(prev_gain);
337 if (g1 > QCONST16(.3f,15) + MULT16_16_Q15(QCONST16(.4f,15),g0)-cont)
345 if (best_yy <= best_xy)
348 pg = SHR32(frac_div32(best_xy,best_yy+1),16);
355 xy = MAC16_16(xy, x[i], x[i-T1]);
358 if ((xcorr[2]-xcorr[0]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[0]))
360 else if ((xcorr[0]-xcorr[2]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[2]))
373 #endif /* ENABLE_POSTFILTER */