pitch.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 COPYRIGHT OWNER
  21. OR 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. #include "celt_lpc.h"
  38. static void find_best_pitch(opus_val32 *xcorr, opus_val16 *y, int len,
  39. int max_pitch, int *best_pitch
  40. #ifdef FIXED_POINT
  41. , int yshift, opus_val32 maxcorr
  42. #endif
  43. )
  44. {
  45. int i, j;
  46. opus_val32 Syy=1;
  47. opus_val16 best_num[2];
  48. opus_val32 best_den[2];
  49. #ifdef FIXED_POINT
  50. int xshift;
  51. xshift = celt_ilog2(maxcorr)-14;
  52. #endif
  53. best_num[0] = -1;
  54. best_num[1] = -1;
  55. best_den[0] = 0;
  56. best_den[1] = 0;
  57. best_pitch[0] = 0;
  58. best_pitch[1] = 1;
  59. for (j=0;j<len;j++)
  60. Syy = ADD32(Syy, SHR32(MULT16_16(y[j],y[j]), yshift));
  61. for (i=0;i<max_pitch;i++)
  62. {
  63. if (xcorr[i]>0)
  64. {
  65. opus_val16 num;
  66. opus_val32 xcorr16;
  67. xcorr16 = EXTRACT16(VSHR32(xcorr[i], xshift));
  68. #ifndef FIXED_POINT
  69. /* Considering the range of xcorr16, this should avoid both underflows
  70. and overflows (inf) when squaring xcorr16 */
  71. xcorr16 *= 1e-12f;
  72. #endif
  73. num = MULT16_16_Q15(xcorr16,xcorr16);
  74. if (MULT16_32_Q15(num,best_den[1]) > MULT16_32_Q15(best_num[1],Syy))
  75. {
  76. if (MULT16_32_Q15(num,best_den[0]) > MULT16_32_Q15(best_num[0],Syy))
  77. {
  78. best_num[1] = best_num[0];
  79. best_den[1] = best_den[0];
  80. best_pitch[1] = best_pitch[0];
  81. best_num[0] = num;
  82. best_den[0] = Syy;
  83. best_pitch[0] = i;
  84. } else {
  85. best_num[1] = num;
  86. best_den[1] = Syy;
  87. best_pitch[1] = i;
  88. }
  89. }
  90. }
  91. Syy += SHR32(MULT16_16(y[i+len],y[i+len]),yshift) - SHR32(MULT16_16(y[i],y[i]),yshift);
  92. Syy = MAX32(1, Syy);
  93. }
  94. }
  95. void pitch_downsample(celt_sig * OPUS_RESTRICT x[], opus_val16 * OPUS_RESTRICT x_lp,
  96. int len, int C)
  97. {
  98. int i;
  99. opus_val32 ac[5];
  100. opus_val16 tmp=Q15ONE;
  101. opus_val16 lpc[4], mem[4]={0,0,0,0};
  102. #ifdef FIXED_POINT
  103. int shift;
  104. opus_val32 maxabs = celt_maxabs32(x[0], len);
  105. if (C==2)
  106. {
  107. opus_val32 maxabs_1 = celt_maxabs32(x[1], len);
  108. maxabs = MAX32(maxabs, maxabs_1);
  109. }
  110. if (maxabs<1)
  111. maxabs=1;
  112. shift = celt_ilog2(maxabs)-10;
  113. if (shift<0)
  114. shift=0;
  115. if (C==2)
  116. shift++;
  117. #endif
  118. for (i=1;i<len>>1;i++)
  119. x_lp[i] = SHR32(HALF32(HALF32(x[0][(2*i-1)]+x[0][(2*i+1)])+x[0][2*i]), shift);
  120. x_lp[0] = SHR32(HALF32(HALF32(x[0][1])+x[0][0]), shift);
  121. if (C==2)
  122. {
  123. for (i=1;i<len>>1;i++)
  124. x_lp[i] += SHR32(HALF32(HALF32(x[1][(2*i-1)]+x[1][(2*i+1)])+x[1][2*i]), shift);
  125. x_lp[0] += SHR32(HALF32(HALF32(x[1][1])+x[1][0]), shift);
  126. }
  127. _celt_autocorr(x_lp, ac, NULL, 0,
  128. 4, len>>1);
  129. /* Noise floor -40 dB */
  130. #ifdef FIXED_POINT
  131. ac[0] += SHR32(ac[0],13);
  132. #else
  133. ac[0] *= 1.0001f;
  134. #endif
  135. /* Lag windowing */
  136. for (i=1;i<=4;i++)
  137. {
  138. /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
  139. #ifdef FIXED_POINT
  140. ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
  141. #else
  142. ac[i] -= ac[i]*(.008f*i)*(.008f*i);
  143. #endif
  144. }
  145. _celt_lpc(lpc, ac, 4);
  146. for (i=0;i<4;i++)
  147. {
  148. tmp = MULT16_16_Q15(QCONST16(.9f,15), tmp);
  149. lpc[i] = MULT16_16_Q15(lpc[i], tmp);
  150. }
  151. celt_fir(x_lp, lpc, x_lp, len>>1, 4, mem);
  152. mem[0]=0;
  153. lpc[0]=QCONST16(.8f,12);
  154. celt_fir(x_lp, lpc, x_lp, len>>1, 1, mem);
  155. }
  156. void pitch_search(const opus_val16 * OPUS_RESTRICT x_lp, opus_val16 * OPUS_RESTRICT y,
  157. int len, int max_pitch, int *pitch)
  158. {
  159. int i, j;
  160. int lag;
  161. int best_pitch[2]={0,0};
  162. VARDECL(opus_val16, x_lp4);
  163. VARDECL(opus_val16, y_lp4);
  164. VARDECL(opus_val32, xcorr);
  165. #ifdef FIXED_POINT
  166. opus_val32 maxcorr=1;
  167. opus_val16 xmax, ymax;
  168. int shift=0;
  169. #endif
  170. int offset;
  171. SAVE_STACK;
  172. celt_assert(len>0);
  173. celt_assert(max_pitch>0);
  174. lag = len+max_pitch;
  175. ALLOC(x_lp4, len>>2, opus_val16);
  176. ALLOC(y_lp4, lag>>2, opus_val16);
  177. ALLOC(xcorr, max_pitch>>1, opus_val32);
  178. /* Downsample by 2 again */
  179. for (j=0;j<len>>2;j++)
  180. x_lp4[j] = x_lp[2*j];
  181. for (j=0;j<lag>>2;j++)
  182. y_lp4[j] = y[2*j];
  183. #ifdef FIXED_POINT
  184. xmax = celt_maxabs16(x_lp4, len>>2);
  185. ymax = celt_maxabs16(y_lp4, lag>>2);
  186. shift = celt_ilog2(MAX16(1, MAX16(xmax, ymax)))-11;
  187. if (shift>0)
  188. {
  189. for (j=0;j<len>>2;j++)
  190. x_lp4[j] = SHR16(x_lp4[j], shift);
  191. for (j=0;j<lag>>2;j++)
  192. y_lp4[j] = SHR16(y_lp4[j], shift);
  193. /* Use double the shift for a MAC */
  194. shift *= 2;
  195. } else {
  196. shift = 0;
  197. }
  198. #endif
  199. /* Coarse search with 4x decimation */
  200. for (i=0;i<max_pitch>>2;i++)
  201. {
  202. opus_val32 sum = 0;
  203. for (j=0;j<len>>2;j++)
  204. sum = MAC16_16(sum, x_lp4[j],y_lp4[i+j]);
  205. xcorr[i] = MAX32(-1, sum);
  206. #ifdef FIXED_POINT
  207. maxcorr = MAX32(maxcorr, sum);
  208. #endif
  209. }
  210. find_best_pitch(xcorr, y_lp4, len>>2, max_pitch>>2, best_pitch
  211. #ifdef FIXED_POINT
  212. , 0, maxcorr
  213. #endif
  214. );
  215. /* Finer search with 2x decimation */
  216. #ifdef FIXED_POINT
  217. maxcorr=1;
  218. #endif
  219. for (i=0;i<max_pitch>>1;i++)
  220. {
  221. opus_val32 sum=0;
  222. xcorr[i] = 0;
  223. if (abs(i-2*best_pitch[0])>2 && abs(i-2*best_pitch[1])>2)
  224. continue;
  225. for (j=0;j<len>>1;j++)
  226. sum += SHR32(MULT16_16(x_lp[j],y[i+j]), shift);
  227. xcorr[i] = MAX32(-1, sum);
  228. #ifdef FIXED_POINT
  229. maxcorr = MAX32(maxcorr, sum);
  230. #endif
  231. }
  232. find_best_pitch(xcorr, y, len>>1, max_pitch>>1, best_pitch
  233. #ifdef FIXED_POINT
  234. , shift+1, maxcorr
  235. #endif
  236. );
  237. /* Refine by pseudo-interpolation */
  238. if (best_pitch[0]>0 && best_pitch[0]<(max_pitch>>1)-1)
  239. {
  240. opus_val32 a, b, c;
  241. a = xcorr[best_pitch[0]-1];
  242. b = xcorr[best_pitch[0]];
  243. c = xcorr[best_pitch[0]+1];
  244. if ((c-a) > MULT16_32_Q15(QCONST16(.7f,15),b-a))
  245. offset = 1;
  246. else if ((a-c) > MULT16_32_Q15(QCONST16(.7f,15),b-c))
  247. offset = -1;
  248. else
  249. offset = 0;
  250. } else {
  251. offset = 0;
  252. }
  253. *pitch = 2*best_pitch[0]-offset;
  254. RESTORE_STACK;
  255. }
  256. static const int second_check[16] = {0, 0, 3, 2, 3, 2, 5, 2, 3, 2, 3, 2, 5, 2, 3, 2};
  257. opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod,
  258. int N, int *T0_, int prev_period, opus_val16 prev_gain)
  259. {
  260. int k, i, T, T0;
  261. opus_val16 g, g0;
  262. opus_val16 pg;
  263. opus_val32 xy,xx,yy;
  264. opus_val32 xcorr[3];
  265. opus_val32 best_xy, best_yy;
  266. int offset;
  267. int minperiod0;
  268. minperiod0 = minperiod;
  269. maxperiod /= 2;
  270. minperiod /= 2;
  271. *T0_ /= 2;
  272. prev_period /= 2;
  273. N /= 2;
  274. x += maxperiod;
  275. if (*T0_>=maxperiod)
  276. *T0_=maxperiod-1;
  277. T = T0 = *T0_;
  278. xx=xy=yy=0;
  279. for (i=0;i<N;i++)
  280. {
  281. xy = MAC16_16(xy, x[i], x[i-T0]);
  282. xx = MAC16_16(xx, x[i], x[i]);
  283. yy = MAC16_16(yy, x[i-T0],x[i-T0]);
  284. }
  285. best_xy = xy;
  286. best_yy = yy;
  287. #ifdef FIXED_POINT
  288. {
  289. opus_val32 x2y2;
  290. int sh, t;
  291. x2y2 = 1+HALF32(MULT32_32_Q31(xx,yy));
  292. sh = celt_ilog2(x2y2)>>1;
  293. t = VSHR32(x2y2, 2*(sh-7));
  294. g = g0 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
  295. }
  296. #else
  297. g = g0 = xy/celt_sqrt(1+xx*yy);
  298. #endif
  299. /* Look for any pitch at T/k */
  300. for (k=2;k<=15;k++)
  301. {
  302. int T1, T1b;
  303. opus_val16 g1;
  304. opus_val16 cont=0;
  305. opus_val16 thresh;
  306. T1 = (2*T0+k)/(2*k);
  307. if (T1 < minperiod)
  308. break;
  309. /* Look for another strong correlation at T1b */
  310. if (k==2)
  311. {
  312. if (T1+T0>maxperiod)
  313. T1b = T0;
  314. else
  315. T1b = T0+T1;
  316. } else
  317. {
  318. T1b = (2*second_check[k]*T0+k)/(2*k);
  319. }
  320. xy=yy=0;
  321. for (i=0;i<N;i++)
  322. {
  323. xy = MAC16_16(xy, x[i], x[i-T1]);
  324. yy = MAC16_16(yy, x[i-T1], x[i-T1]);
  325. xy = MAC16_16(xy, x[i], x[i-T1b]);
  326. yy = MAC16_16(yy, x[i-T1b], x[i-T1b]);
  327. }
  328. #ifdef FIXED_POINT
  329. {
  330. opus_val32 x2y2;
  331. int sh, t;
  332. x2y2 = 1+MULT32_32_Q31(xx,yy);
  333. sh = celt_ilog2(x2y2)>>1;
  334. t = VSHR32(x2y2, 2*(sh-7));
  335. g1 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
  336. }
  337. #else
  338. g1 = xy/celt_sqrt(1+2.f*xx*1.f*yy);
  339. #endif
  340. if (abs(T1-prev_period)<=1)
  341. cont = prev_gain;
  342. else if (abs(T1-prev_period)<=2 && 5*k*k < T0)
  343. cont = HALF32(prev_gain);
  344. else
  345. cont = 0;
  346. thresh = MAX16(QCONST16(.3f,15), MULT16_16_Q15(QCONST16(.7f,15),g0)-cont);
  347. /* Bias against very high pitch (very short period) to avoid false-positives
  348. due to short-term correlation */
  349. if (T1<3*minperiod)
  350. thresh = MAX16(QCONST16(.4f,15), MULT16_16_Q15(QCONST16(.85f,15),g0)-cont);
  351. else if (T1<2*minperiod)
  352. thresh = MAX16(QCONST16(.5f,15), MULT16_16_Q15(QCONST16(.9f,15),g0)-cont);
  353. if (g1 > thresh)
  354. {
  355. best_xy = xy;
  356. best_yy = yy;
  357. T = T1;
  358. g = g1;
  359. }
  360. }
  361. best_xy = MAX32(0, best_xy);
  362. if (best_yy <= best_xy)
  363. pg = Q15ONE;
  364. else
  365. pg = SHR32(frac_div32(best_xy,best_yy+1),16);
  366. for (k=0;k<3;k++)
  367. {
  368. int T1 = T+k-1;
  369. xy = 0;
  370. for (i=0;i<N;i++)
  371. xy = MAC16_16(xy, x[i], x[i-T1]);
  372. xcorr[k] = xy;
  373. }
  374. if ((xcorr[2]-xcorr[0]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[0]))
  375. offset = 1;
  376. else if ((xcorr[0]-xcorr[2]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[2]))
  377. offset = -1;
  378. else
  379. offset = 0;
  380. if (pg > g)
  381. pg = g;
  382. *T0_ = 2*T+offset;
  383. if (*T0_<minperiod0)
  384. *T0_=minperiod0;
  385. return pg;
  386. }