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 - Neither the name of the Xiph.org Foundation nor the names of its
22 contributors may be used to endorse or promote products derived from
23 this software without specific prior written permission.
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
29 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 #include "os_support.h"
46 #include "stack_alloc.h"
49 void find_best_pitch(celt_word32 *xcorr, celt_word32 maxcorr, celt_word16 *y, int yshift, int len, int max_pitch, int best_pitch[2])
53 celt_word16 best_num[2];
54 celt_word32 best_den[2];
58 xshift = celt_ilog2(maxcorr)-14;
68 Syy = MAC16_16(Syy, y[j],y[j]);
69 for (i=0;i<max_pitch;i++)
76 xcorr16 = EXTRACT16(VSHR32(xcorr[i], xshift));
77 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);
101 void pitch_downsample(const celt_sig * restrict x, celt_word16 * restrict x_lp, int len, int end, int _C, celt_sig * restrict xmem, celt_word16 * restrict filt_mem)
104 const int C = CHANNELS(_C);
105 for (i=1;i<len>>1;i++)
106 x_lp[i] = SHR32(HALF32(HALF32(x[(2*i-1)*C]+x[(2*i+1)*C])+x[2*i*C]), SIG_SHIFT);
107 x_lp[0] = SHR32(HALF32(HALF32(*xmem+x[C])+x[0]), SIG_SHIFT);
111 for (i=1;i<len>>1;i++)
112 x_lp[i] = SHR32(HALF32(HALF32(x[(2*i-1)*C+1]+x[(2*i+1)*C+1])+x[2*i*C+1]), SIG_SHIFT);
113 x_lp[0] += SHR32(HALF32(HALF32(x[C+1])+x[1]), SIG_SHIFT);
126 for (j=0;j<(len>>1)-i;j++)
128 ac[i] += x_lp[j]*x_lp[j+i];
131 det = 1./(.1+ac[0]*ac[0]-ac[1]*ac[1]);
132 ak[0] = .9*det*(ac[0]*ac[1] - ac[1]*ac[2]);
133 ak[1] = .81*det*(-ac[1]*ac[1] + ac[0]*ac[2]);
134 /*printf ("%f %f %f\n", 1., -ak[0], -ak[1]);*/
137 filt_mem[0]=x_lp[(end>>1)-1];
138 filt_mem[1]=x_lp[(end>>1)-2];
139 for (j=0;j<len>>1;j++)
142 x_lp[j] = x_lp[j] - ak[0]*mem[0] - ak[1]*mem[1];
151 void pitch_search(const CELTMode *m, const celt_word16 * restrict x_lp, celt_word16 * restrict y, int len, int max_pitch, int *pitch, celt_sig *xmem, int M)
154 const int lag = MAX_PERIOD;
155 const int N = M*m->eBands[m->nbEBands+1];
156 int best_pitch[2]={0};
157 VARDECL(celt_word16, x_lp4);
158 VARDECL(celt_word16, y_lp4);
159 VARDECL(celt_word32, xcorr);
160 celt_word32 maxcorr=1;
166 ALLOC(x_lp4, len>>2, celt_word16);
167 ALLOC(y_lp4, lag>>2, celt_word16);
168 ALLOC(xcorr, max_pitch>>1, celt_word32);
170 /* Downsample by 2 again */
171 for (j=0;j<len>>2;j++)
172 x_lp4[j] = x_lp[2*j];
173 for (j=0;j<lag>>2;j++)
177 shift = celt_ilog2(MAX16(celt_maxabs16(x_lp4, len>>2), celt_maxabs16(y_lp4, lag>>2)))-11;
180 for (j=0;j<len>>2;j++)
181 x_lp4[j] = SHR16(x_lp4[j], shift);
182 for (j=0;j<lag>>2;j++)
183 y_lp4[j] = SHR16(y_lp4[j], shift);
184 /* Use double the shift for a MAC */
191 /* Coarse search with 4x decimation */
193 for (i=0;i<max_pitch>>2;i++)
196 for (j=0;j<len>>2;j++)
197 sum = MAC16_16(sum, x_lp4[j],y_lp4[i+j]);
198 xcorr[i] = MAX32(-1, sum);
199 maxcorr = MAX32(maxcorr, sum);
201 find_best_pitch(xcorr, maxcorr, y_lp4, 0, len>>2, max_pitch>>2, best_pitch);
203 /* Finer search with 2x decimation */
205 for (i=0;i<max_pitch>>1;i++)
209 if (abs(i-2*best_pitch[0])>2 && abs(i-2*best_pitch[1])>2)
211 for (j=0;j<len>>1;j++)
212 sum += SHR32(MULT16_16(x_lp[j],y[i+j]), shift);
213 xcorr[i] = MAX32(-1, sum);
214 maxcorr = MAX32(maxcorr, sum);
216 find_best_pitch(xcorr, maxcorr, y, shift, len>>1, max_pitch>>1, best_pitch);
218 /* Refine by pseudo-interpolation */
219 if (best_pitch[0]>0 && best_pitch[0]<(max_pitch>>1)-1)
222 a = xcorr[best_pitch[0]-1];
223 b = xcorr[best_pitch[0]];
224 c = xcorr[best_pitch[0]+1];
225 if ((c-a) > MULT16_32_Q15(QCONST16(.7f,15),b-a))
227 else if ((a-c) > MULT16_32_Q15(QCONST16(.7f,15),b-c))
234 *pitch = 2*best_pitch[0]-offset;
236 CELT_MOVE(y, y+(N>>1), (lag-N)>>1);
237 CELT_MOVE(y+((lag-N)>>1), x_lp, N>>1);
241 /*printf ("%d\n", *pitch);*/