pitch.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* Copyright (c) 2007-2008 CSIRO
  2. Copyright (c) 2007-2009 Xiph.Org Foundation
  3. Written by Jean-Marc Valin */
  4. /**
  5. @file pitch.c
  6. @brief Pitch analysis
  7. */
  8. /*
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. - Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. - Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  21. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifdef HAVE_CONFIG_H
  30. #include "config.h"
  31. #endif
  32. #include "pitch.h"
  33. #include "os_support.h"
  34. #include "modes.h"
  35. #include "stack_alloc.h"
  36. #include "mathops.h"
  37. static void find_best_pitch(opus_val32 *xcorr, opus_val32 maxcorr, opus_val16 *y,
  38. int yshift, int len, int max_pitch, int *best_pitch)
  39. {
  40. int i, j;
  41. opus_val32 Syy=1;
  42. opus_val16 best_num[2];
  43. opus_val32 best_den[2];
  44. #ifdef FIXED_POINT
  45. int xshift;
  46. xshift = celt_ilog2(maxcorr)-14;
  47. #endif
  48. best_num[0] = -1;
  49. best_num[1] = -1;
  50. best_den[0] = 0;
  51. best_den[1] = 0;
  52. best_pitch[0] = 0;
  53. best_pitch[1] = 1;
  54. for (j=0;j<len;j++)
  55. Syy = MAC16_16(Syy, y[j],y[j]);
  56. for (i=0;i<max_pitch;i++)
  57. {
  58. if (xcorr[i]>0)
  59. {
  60. opus_val16 num;
  61. opus_val32 xcorr16;
  62. xcorr16 = EXTRACT16(VSHR32(xcorr[i], xshift));
  63. num = MULT16_16_Q15(xcorr16,xcorr16);
  64. if (MULT16_32_Q15(num,best_den[1]) > MULT16_32_Q15(best_num[1],Syy))
  65. {
  66. if (MULT16_32_Q15(num,best_den[0]) > MULT16_32_Q15(best_num[0],Syy))
  67. {
  68. best_num[1] = best_num[0];
  69. best_den[1] = best_den[0];
  70. best_pitch[1] = best_pitch[0];
  71. best_num[0] = num;
  72. best_den[0] = Syy;
  73. best_pitch[0] = i;
  74. } else {
  75. best_num[1] = num;
  76. best_den[1] = Syy;
  77. best_pitch[1] = i;
  78. }
  79. }
  80. }
  81. Syy += SHR32(MULT16_16(y[i+len],y[i+len]),yshift) - SHR32(MULT16_16(y[i],y[i]),yshift);
  82. Syy = MAX32(1, Syy);
  83. }
  84. }
  85. #include "plc.h"
  86. void pitch_downsample(celt_sig * restrict x[], opus_val16 * restrict x_lp,
  87. int len, int _C)
  88. {
  89. int i;
  90. opus_val32 ac[5];
  91. opus_val16 tmp=Q15ONE;
  92. opus_val16 lpc[4], mem[4]={0,0,0,0};
  93. const int C = CHANNELS(_C);
  94. for (i=1;i<len>>1;i++)
  95. x_lp[i] = SHR32(HALF32(HALF32(x[0][(2*i-1)]+x[0][(2*i+1)])+x[0][2*i]), SIG_SHIFT+3);
  96. x_lp[0] = SHR32(HALF32(HALF32(x[0][1])+x[0][0]), SIG_SHIFT+3);
  97. if (C==2)
  98. {
  99. for (i=1;i<len>>1;i++)
  100. x_lp[i] += SHR32(HALF32(HALF32(x[1][(2*i-1)]+x[1][(2*i+1)])+x[1][2*i]), SIG_SHIFT+3);
  101. x_lp[0] += SHR32(HALF32(HALF32(x[1][1])+x[1][0]), SIG_SHIFT+3);
  102. }
  103. _celt_autocorr(x_lp, ac, NULL, 0,
  104. 4, len>>1);
  105. /* Noise floor -40 dB */
  106. #ifdef FIXED_POINT
  107. ac[0] += SHR32(ac[0],13);
  108. #else
  109. ac[0] *= 1.0001f;
  110. #endif
  111. /* Lag windowing */
  112. for (i=1;i<=4;i++)
  113. {
  114. /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
  115. #ifdef FIXED_POINT
  116. ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
  117. #else
  118. ac[i] -= ac[i]*(.008f*i)*(.008f*i);
  119. #endif
  120. }
  121. _celt_lpc(lpc, ac, 4);
  122. for (i=0;i<4;i++)
  123. {
  124. tmp = MULT16_16_Q15(QCONST16(.9f,15), tmp);
  125. lpc[i] = MULT16_16_Q15(lpc[i], tmp);
  126. }
  127. celt_fir(x_lp, lpc, x_lp, len>>1, 4, mem);
  128. mem[0]=0;
  129. lpc[0]=QCONST16(.8f,12);
  130. celt_fir(x_lp, lpc, x_lp, len>>1, 1, mem);
  131. }
  132. void pitch_search(const opus_val16 * restrict x_lp, opus_val16 * restrict y,
  133. int len, int max_pitch, int *pitch)
  134. {
  135. int i, j;
  136. int lag;
  137. int best_pitch[2]={0,0};
  138. VARDECL(opus_val16, x_lp4);
  139. VARDECL(opus_val16, y_lp4);
  140. VARDECL(opus_val32, xcorr);
  141. opus_val32 maxcorr=1;
  142. int offset;
  143. int shift=0;
  144. SAVE_STACK;
  145. lag = len+max_pitch;
  146. ALLOC(x_lp4, len>>2, opus_val16);
  147. ALLOC(y_lp4, lag>>2, opus_val16);
  148. ALLOC(xcorr, max_pitch>>1, opus_val32);
  149. /* Downsample by 2 again */
  150. for (j=0;j<len>>2;j++)
  151. x_lp4[j] = x_lp[2*j];
  152. for (j=0;j<lag>>2;j++)
  153. y_lp4[j] = y[2*j];
  154. #ifdef FIXED_POINT
  155. shift = celt_ilog2(MAX16(1, MAX16(celt_maxabs16(x_lp4, len>>2), celt_maxabs16(y_lp4, lag>>2))))-11;
  156. if (shift>0)
  157. {
  158. for (j=0;j<len>>2;j++)
  159. x_lp4[j] = SHR16(x_lp4[j], shift);
  160. for (j=0;j<lag>>2;j++)
  161. y_lp4[j] = SHR16(y_lp4[j], shift);
  162. /* Use double the shift for a MAC */
  163. shift *= 2;
  164. } else {
  165. shift = 0;
  166. }
  167. #endif
  168. /* Coarse search with 4x decimation */
  169. for (i=0;i<max_pitch>>2;i++)
  170. {
  171. opus_val32 sum = 0;
  172. for (j=0;j<len>>2;j++)
  173. sum = MAC16_16(sum, x_lp4[j],y_lp4[i+j]);
  174. xcorr[i] = MAX32(-1, sum);
  175. maxcorr = MAX32(maxcorr, sum);
  176. }
  177. find_best_pitch(xcorr, maxcorr, y_lp4, 0, len>>2, max_pitch>>2, best_pitch);
  178. /* Finer search with 2x decimation */
  179. maxcorr=1;
  180. for (i=0;i<max_pitch>>1;i++)
  181. {
  182. opus_val32 sum=0;
  183. xcorr[i] = 0;
  184. if (abs(i-2*best_pitch[0])>2 && abs(i-2*best_pitch[1])>2)
  185. continue;
  186. for (j=0;j<len>>1;j++)
  187. sum += SHR32(MULT16_16(x_lp[j],y[i+j]), shift);
  188. xcorr[i] = MAX32(-1, sum);
  189. maxcorr = MAX32(maxcorr, sum);
  190. }
  191. find_best_pitch(xcorr, maxcorr, y, shift, len>>1, max_pitch>>1, best_pitch);
  192. /* Refine by pseudo-interpolation */
  193. if (best_pitch[0]>0 && best_pitch[0]<(max_pitch>>1)-1)
  194. {
  195. opus_val32 a, b, c;
  196. a = xcorr[best_pitch[0]-1];
  197. b = xcorr[best_pitch[0]];
  198. c = xcorr[best_pitch[0]+1];
  199. if ((c-a) > MULT16_32_Q15(QCONST16(.7f,15),b-a))
  200. offset = 1;
  201. else if ((a-c) > MULT16_32_Q15(QCONST16(.7f,15),b-c))
  202. offset = -1;
  203. else
  204. offset = 0;
  205. } else {
  206. offset = 0;
  207. }
  208. *pitch = 2*best_pitch[0]-offset;
  209. RESTORE_STACK;
  210. }
  211. static const int second_check[16] = {0, 0, 3, 2, 3, 2, 5, 2, 3, 2, 3, 2, 5, 2, 3, 2};
  212. opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod,
  213. int N, int *_T0, int prev_period, opus_val16 prev_gain)
  214. {
  215. int k, i, T, T0;
  216. opus_val16 g, g0;
  217. opus_val16 pg;
  218. opus_val32 xy,xx,yy;
  219. opus_val32 xcorr[3];
  220. opus_val32 best_xy, best_yy;
  221. int offset;
  222. int minperiod0;
  223. minperiod0 = minperiod;
  224. maxperiod /= 2;
  225. minperiod /= 2;
  226. *_T0 /= 2;
  227. prev_period /= 2;
  228. N /= 2;
  229. x += maxperiod;
  230. if (*_T0>=maxperiod)
  231. *_T0=maxperiod-1;
  232. T = T0 = *_T0;
  233. xx=xy=yy=0;
  234. for (i=0;i<N;i++)
  235. {
  236. xy = MAC16_16(xy, x[i], x[i-T0]);
  237. xx = MAC16_16(xx, x[i], x[i]);
  238. yy = MAC16_16(yy, x[i-T0],x[i-T0]);
  239. }
  240. best_xy = xy;
  241. best_yy = yy;
  242. #ifdef FIXED_POINT
  243. {
  244. opus_val32 x2y2;
  245. int sh, t;
  246. x2y2 = 1+HALF32(MULT32_32_Q31(xx,yy));
  247. sh = celt_ilog2(x2y2)>>1;
  248. t = VSHR32(x2y2, 2*(sh-7));
  249. g = g0 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
  250. }
  251. #else
  252. g = g0 = xy/celt_sqrt(1+xx*yy);
  253. #endif
  254. /* Look for any pitch at T/k */
  255. for (k=2;k<=15;k++)
  256. {
  257. int T1, T1b;
  258. opus_val16 g1;
  259. opus_val16 cont=0;
  260. T1 = (2*T0+k)/(2*k);
  261. if (T1 < minperiod)
  262. break;
  263. /* Look for another strong correlation at T1b */
  264. if (k==2)
  265. {
  266. if (T1+T0>maxperiod)
  267. T1b = T0;
  268. else
  269. T1b = T0+T1;
  270. } else
  271. {
  272. T1b = (2*second_check[k]*T0+k)/(2*k);
  273. }
  274. xy=yy=0;
  275. for (i=0;i<N;i++)
  276. {
  277. xy = MAC16_16(xy, x[i], x[i-T1]);
  278. yy = MAC16_16(yy, x[i-T1], x[i-T1]);
  279. xy = MAC16_16(xy, x[i], x[i-T1b]);
  280. yy = MAC16_16(yy, x[i-T1b], x[i-T1b]);
  281. }
  282. #ifdef FIXED_POINT
  283. {
  284. opus_val32 x2y2;
  285. int sh, t;
  286. x2y2 = 1+MULT32_32_Q31(xx,yy);
  287. sh = celt_ilog2(x2y2)>>1;
  288. t = VSHR32(x2y2, 2*(sh-7));
  289. g1 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
  290. }
  291. #else
  292. g1 = xy/celt_sqrt(1+2.f*xx*1.f*yy);
  293. #endif
  294. if (abs(T1-prev_period)<=1)
  295. cont = prev_gain;
  296. else if (abs(T1-prev_period)<=2 && 5*k*k < T0)
  297. cont = HALF32(prev_gain);
  298. else
  299. cont = 0;
  300. if (g1 > QCONST16(.3f,15) + MULT16_16_Q15(QCONST16(.4f,15),g0)-cont)
  301. {
  302. best_xy = xy;
  303. best_yy = yy;
  304. T = T1;
  305. g = g1;
  306. }
  307. }
  308. if (best_yy <= best_xy)
  309. pg = Q15ONE;
  310. else
  311. pg = SHR32(frac_div32(best_xy,best_yy+1),16);
  312. for (k=0;k<3;k++)
  313. {
  314. int T1 = T+k-1;
  315. xy = 0;
  316. for (i=0;i<N;i++)
  317. xy = MAC16_16(xy, x[i], x[i-T1]);
  318. xcorr[k] = xy;
  319. }
  320. if ((xcorr[2]-xcorr[0]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[0]))
  321. offset = 1;
  322. else if ((xcorr[0]-xcorr[2]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[2]))
  323. offset = -1;
  324. else
  325. offset = 0;
  326. if (pg > g)
  327. pg = g;
  328. *_T0 = 2*T+offset;
  329. if (*_T0<minperiod0)
  330. *_T0=minperiod0;
  331. return pg;
  332. }