pitch.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. static void celt_fir5(const opus_val16 *x,
  96. const opus_val16 *num,
  97. opus_val16 *y,
  98. int N,
  99. opus_val16 *mem)
  100. {
  101. int i;
  102. opus_val16 num0, num1, num2, num3, num4;
  103. opus_val32 mem0, mem1, mem2, mem3, mem4;
  104. num0=num[0];
  105. num1=num[1];
  106. num2=num[2];
  107. num3=num[3];
  108. num4=num[4];
  109. mem0=mem[0];
  110. mem1=mem[1];
  111. mem2=mem[2];
  112. mem3=mem[3];
  113. mem4=mem[4];
  114. for (i=0;i<N;i++)
  115. {
  116. opus_val32 sum = SHL32(EXTEND32(x[i]), SIG_SHIFT);
  117. sum = MAC16_16(sum,num0,mem0);
  118. sum = MAC16_16(sum,num1,mem1);
  119. sum = MAC16_16(sum,num2,mem2);
  120. sum = MAC16_16(sum,num3,mem3);
  121. sum = MAC16_16(sum,num4,mem4);
  122. mem4 = mem3;
  123. mem3 = mem2;
  124. mem2 = mem1;
  125. mem1 = mem0;
  126. mem0 = x[i];
  127. y[i] = ROUND16(sum, SIG_SHIFT);
  128. }
  129. mem[0]=mem0;
  130. mem[1]=mem1;
  131. mem[2]=mem2;
  132. mem[3]=mem3;
  133. mem[4]=mem4;
  134. }
  135. void pitch_downsample(celt_sig * OPUS_RESTRICT x[], opus_val16 * OPUS_RESTRICT x_lp,
  136. int len, int C, int arch)
  137. {
  138. int i;
  139. opus_val32 ac[5];
  140. opus_val16 tmp=Q15ONE;
  141. opus_val16 lpc[4], mem[5]={0,0,0,0,0};
  142. opus_val16 lpc2[5];
  143. opus_val16 c1 = QCONST16(.8f,15);
  144. #ifdef FIXED_POINT
  145. int shift;
  146. opus_val32 maxabs = celt_maxabs32(x[0], len);
  147. if (C==2)
  148. {
  149. opus_val32 maxabs_1 = celt_maxabs32(x[1], len);
  150. maxabs = MAX32(maxabs, maxabs_1);
  151. }
  152. if (maxabs<1)
  153. maxabs=1;
  154. shift = celt_ilog2(maxabs)-10;
  155. if (shift<0)
  156. shift=0;
  157. if (C==2)
  158. shift++;
  159. #endif
  160. for (i=1;i<len>>1;i++)
  161. x_lp[i] = SHR32(HALF32(HALF32(x[0][(2*i-1)]+x[0][(2*i+1)])+x[0][2*i]), shift);
  162. x_lp[0] = SHR32(HALF32(HALF32(x[0][1])+x[0][0]), shift);
  163. if (C==2)
  164. {
  165. for (i=1;i<len>>1;i++)
  166. x_lp[i] += SHR32(HALF32(HALF32(x[1][(2*i-1)]+x[1][(2*i+1)])+x[1][2*i]), shift);
  167. x_lp[0] += SHR32(HALF32(HALF32(x[1][1])+x[1][0]), shift);
  168. }
  169. _celt_autocorr(x_lp, ac, NULL, 0,
  170. 4, len>>1, arch);
  171. /* Noise floor -40 dB */
  172. #ifdef FIXED_POINT
  173. ac[0] += SHR32(ac[0],13);
  174. #else
  175. ac[0] *= 1.0001f;
  176. #endif
  177. /* Lag windowing */
  178. for (i=1;i<=4;i++)
  179. {
  180. /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
  181. #ifdef FIXED_POINT
  182. ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
  183. #else
  184. ac[i] -= ac[i]*(.008f*i)*(.008f*i);
  185. #endif
  186. }
  187. _celt_lpc(lpc, ac, 4);
  188. for (i=0;i<4;i++)
  189. {
  190. tmp = MULT16_16_Q15(QCONST16(.9f,15), tmp);
  191. lpc[i] = MULT16_16_Q15(lpc[i], tmp);
  192. }
  193. /* Add a zero */
  194. lpc2[0] = lpc[0] + QCONST16(.8f,SIG_SHIFT);
  195. lpc2[1] = lpc[1] + MULT16_16_Q15(c1,lpc[0]);
  196. lpc2[2] = lpc[2] + MULT16_16_Q15(c1,lpc[1]);
  197. lpc2[3] = lpc[3] + MULT16_16_Q15(c1,lpc[2]);
  198. lpc2[4] = MULT16_16_Q15(c1,lpc[3]);
  199. celt_fir5(x_lp, lpc2, x_lp, len>>1, mem);
  200. }
  201. #if 0 /* This is a simple version of the pitch correlation that should work
  202. well on DSPs like Blackfin and TI C5x/C6x */
  203. #ifdef FIXED_POINT
  204. opus_val32
  205. #else
  206. void
  207. #endif
  208. celt_pitch_xcorr(opus_val16 *x, opus_val16 *y, opus_val32 *xcorr, int len, int max_pitch)
  209. {
  210. int i, j;
  211. #ifdef FIXED_POINT
  212. opus_val32 maxcorr=1;
  213. #endif
  214. for (i=0;i<max_pitch;i++)
  215. {
  216. opus_val32 sum = 0;
  217. for (j=0;j<len;j++)
  218. sum = MAC16_16(sum, x[j],y[i+j]);
  219. xcorr[i] = sum;
  220. #ifdef FIXED_POINT
  221. maxcorr = MAX32(maxcorr, sum);
  222. #endif
  223. }
  224. #ifdef FIXED_POINT
  225. return maxcorr;
  226. #endif
  227. }
  228. #else /* Unrolled version of the pitch correlation -- runs faster on x86 and ARM */
  229. #ifdef FIXED_POINT
  230. opus_val32
  231. #else
  232. void
  233. #endif
  234. celt_pitch_xcorr_c(const opus_val16 *_x, const opus_val16 *_y, opus_val32 *xcorr, int len, int max_pitch)
  235. {
  236. int i,j;
  237. /*The EDSP version requires that max_pitch is at least 1, and that _x is
  238. 32-bit aligned.
  239. Since it's hard to put asserts in assembly, put them here.*/
  240. celt_assert(max_pitch>0);
  241. celt_assert((((unsigned char *)_x-(unsigned char *)NULL)&3)==0);
  242. #ifdef FIXED_POINT
  243. opus_val32 maxcorr=1;
  244. #endif
  245. for (i=0;i<max_pitch-3;i+=4)
  246. {
  247. opus_val32 sum[4]={0,0,0,0};
  248. xcorr_kernel(_x, _y+i, sum, len);
  249. xcorr[i]=sum[0];
  250. xcorr[i+1]=sum[1];
  251. xcorr[i+2]=sum[2];
  252. xcorr[i+3]=sum[3];
  253. #ifdef FIXED_POINT
  254. sum[0] = MAX32(sum[0], sum[1]);
  255. sum[2] = MAX32(sum[2], sum[3]);
  256. sum[0] = MAX32(sum[0], sum[2]);
  257. maxcorr = MAX32(maxcorr, sum[0]);
  258. #endif
  259. }
  260. /* In case max_pitch isn't a multiple of 4, do non-unrolled version. */
  261. for (;i<max_pitch;i++)
  262. {
  263. opus_val32 sum = 0;
  264. for (j=0;j<len;j++)
  265. sum = MAC16_16(sum, _x[j],_y[i+j]);
  266. xcorr[i] = sum;
  267. #ifdef FIXED_POINT
  268. maxcorr = MAX32(maxcorr, sum);
  269. #endif
  270. }
  271. #ifdef FIXED_POINT
  272. return maxcorr;
  273. #endif
  274. }
  275. #endif
  276. void pitch_search(const opus_val16 * OPUS_RESTRICT x_lp, opus_val16 * OPUS_RESTRICT y,
  277. int len, int max_pitch, int *pitch, int arch)
  278. {
  279. int i, j;
  280. int lag;
  281. int best_pitch[2]={0,0};
  282. VARDECL(opus_val16, x_lp4);
  283. VARDECL(opus_val16, y_lp4);
  284. VARDECL(opus_val32, xcorr);
  285. #ifdef FIXED_POINT
  286. opus_val32 maxcorr;
  287. opus_val32 xmax, ymax;
  288. int shift=0;
  289. #endif
  290. int offset;
  291. SAVE_STACK;
  292. celt_assert(len>0);
  293. celt_assert(max_pitch>0);
  294. lag = len+max_pitch;
  295. ALLOC(x_lp4, len>>2, opus_val16);
  296. ALLOC(y_lp4, lag>>2, opus_val16);
  297. ALLOC(xcorr, max_pitch>>1, opus_val32);
  298. /* Downsample by 2 again */
  299. for (j=0;j<len>>2;j++)
  300. x_lp4[j] = x_lp[2*j];
  301. for (j=0;j<lag>>2;j++)
  302. y_lp4[j] = y[2*j];
  303. #ifdef FIXED_POINT
  304. xmax = celt_maxabs16(x_lp4, len>>2);
  305. ymax = celt_maxabs16(y_lp4, lag>>2);
  306. shift = celt_ilog2(MAX32(1, MAX32(xmax, ymax)))-11;
  307. if (shift>0)
  308. {
  309. for (j=0;j<len>>2;j++)
  310. x_lp4[j] = SHR16(x_lp4[j], shift);
  311. for (j=0;j<lag>>2;j++)
  312. y_lp4[j] = SHR16(y_lp4[j], shift);
  313. /* Use double the shift for a MAC */
  314. shift *= 2;
  315. } else {
  316. shift = 0;
  317. }
  318. #endif
  319. /* Coarse search with 4x decimation */
  320. #ifdef FIXED_POINT
  321. maxcorr =
  322. #endif
  323. celt_pitch_xcorr(x_lp4, y_lp4, xcorr, len>>2, max_pitch>>2, arch);
  324. find_best_pitch(xcorr, y_lp4, len>>2, max_pitch>>2, best_pitch
  325. #ifdef FIXED_POINT
  326. , 0, maxcorr
  327. #endif
  328. );
  329. /* Finer search with 2x decimation */
  330. #ifdef FIXED_POINT
  331. maxcorr=1;
  332. #endif
  333. for (i=0;i<max_pitch>>1;i++)
  334. {
  335. opus_val32 sum=0;
  336. xcorr[i] = 0;
  337. if (abs(i-2*best_pitch[0])>2 && abs(i-2*best_pitch[1])>2)
  338. continue;
  339. for (j=0;j<len>>1;j++)
  340. sum += SHR32(MULT16_16(x_lp[j],y[i+j]), shift);
  341. xcorr[i] = MAX32(-1, sum);
  342. #ifdef FIXED_POINT
  343. maxcorr = MAX32(maxcorr, sum);
  344. #endif
  345. }
  346. find_best_pitch(xcorr, y, len>>1, max_pitch>>1, best_pitch
  347. #ifdef FIXED_POINT
  348. , shift+1, maxcorr
  349. #endif
  350. );
  351. /* Refine by pseudo-interpolation */
  352. if (best_pitch[0]>0 && best_pitch[0]<(max_pitch>>1)-1)
  353. {
  354. opus_val32 a, b, c;
  355. a = xcorr[best_pitch[0]-1];
  356. b = xcorr[best_pitch[0]];
  357. c = xcorr[best_pitch[0]+1];
  358. if ((c-a) > MULT16_32_Q15(QCONST16(.7f,15),b-a))
  359. offset = 1;
  360. else if ((a-c) > MULT16_32_Q15(QCONST16(.7f,15),b-c))
  361. offset = -1;
  362. else
  363. offset = 0;
  364. } else {
  365. offset = 0;
  366. }
  367. *pitch = 2*best_pitch[0]-offset;
  368. RESTORE_STACK;
  369. }
  370. static const int second_check[16] = {0, 0, 3, 2, 3, 2, 5, 2, 3, 2, 3, 2, 5, 2, 3, 2};
  371. opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod,
  372. int N, int *T0_, int prev_period, opus_val16 prev_gain)
  373. {
  374. int k, i, T, T0;
  375. opus_val16 g, g0;
  376. opus_val16 pg;
  377. opus_val32 xy,xx,yy,xy2;
  378. opus_val32 xcorr[3];
  379. opus_val32 best_xy, best_yy;
  380. int offset;
  381. int minperiod0;
  382. VARDECL(opus_val32, yy_lookup);
  383. SAVE_STACK;
  384. minperiod0 = minperiod;
  385. maxperiod /= 2;
  386. minperiod /= 2;
  387. *T0_ /= 2;
  388. prev_period /= 2;
  389. N /= 2;
  390. x += maxperiod;
  391. if (*T0_>=maxperiod)
  392. *T0_=maxperiod-1;
  393. T = T0 = *T0_;
  394. ALLOC(yy_lookup, maxperiod+1, opus_val32);
  395. dual_inner_prod(x, x, x-T0, N, &xx, &xy);
  396. yy_lookup[0] = xx;
  397. yy=xx;
  398. for (i=1;i<=maxperiod;i++)
  399. {
  400. yy = yy+MULT16_16(x[-i],x[-i])-MULT16_16(x[N-i],x[N-i]);
  401. yy_lookup[i] = MAX32(0, yy);
  402. }
  403. yy = yy_lookup[T0];
  404. best_xy = xy;
  405. best_yy = yy;
  406. #ifdef FIXED_POINT
  407. {
  408. opus_val32 x2y2;
  409. int sh, t;
  410. x2y2 = 1+HALF32(MULT32_32_Q31(xx,yy));
  411. sh = celt_ilog2(x2y2)>>1;
  412. t = VSHR32(x2y2, 2*(sh-7));
  413. g = g0 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
  414. }
  415. #else
  416. g = g0 = xy/celt_sqrt(1+xx*yy);
  417. #endif
  418. /* Look for any pitch at T/k */
  419. for (k=2;k<=15;k++)
  420. {
  421. int T1, T1b;
  422. opus_val16 g1;
  423. opus_val16 cont=0;
  424. opus_val16 thresh;
  425. T1 = (2*T0+k)/(2*k);
  426. if (T1 < minperiod)
  427. break;
  428. /* Look for another strong correlation at T1b */
  429. if (k==2)
  430. {
  431. if (T1+T0>maxperiod)
  432. T1b = T0;
  433. else
  434. T1b = T0+T1;
  435. } else
  436. {
  437. T1b = (2*second_check[k]*T0+k)/(2*k);
  438. }
  439. dual_inner_prod(x, &x[-T1], &x[-T1b], N, &xy, &xy2);
  440. xy += xy2;
  441. yy = yy_lookup[T1] + yy_lookup[T1b];
  442. #ifdef FIXED_POINT
  443. {
  444. opus_val32 x2y2;
  445. int sh, t;
  446. x2y2 = 1+MULT32_32_Q31(xx,yy);
  447. sh = celt_ilog2(x2y2)>>1;
  448. t = VSHR32(x2y2, 2*(sh-7));
  449. g1 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
  450. }
  451. #else
  452. g1 = xy/celt_sqrt(1+2.f*xx*1.f*yy);
  453. #endif
  454. if (abs(T1-prev_period)<=1)
  455. cont = prev_gain;
  456. else if (abs(T1-prev_period)<=2 && 5*k*k < T0)
  457. cont = HALF32(prev_gain);
  458. else
  459. cont = 0;
  460. thresh = MAX16(QCONST16(.3f,15), MULT16_16_Q15(QCONST16(.7f,15),g0)-cont);
  461. /* Bias against very high pitch (very short period) to avoid false-positives
  462. due to short-term correlation */
  463. if (T1<3*minperiod)
  464. thresh = MAX16(QCONST16(.4f,15), MULT16_16_Q15(QCONST16(.85f,15),g0)-cont);
  465. else if (T1<2*minperiod)
  466. thresh = MAX16(QCONST16(.5f,15), MULT16_16_Q15(QCONST16(.9f,15),g0)-cont);
  467. if (g1 > thresh)
  468. {
  469. best_xy = xy;
  470. best_yy = yy;
  471. T = T1;
  472. g = g1;
  473. }
  474. }
  475. best_xy = MAX32(0, best_xy);
  476. if (best_yy <= best_xy)
  477. pg = Q15ONE;
  478. else
  479. pg = SHR32(frac_div32(best_xy,best_yy+1),16);
  480. for (k=0;k<3;k++)
  481. {
  482. int T1 = T+k-1;
  483. xy = 0;
  484. for (i=0;i<N;i++)
  485. xy = MAC16_16(xy, x[i], x[i-T1]);
  486. xcorr[k] = xy;
  487. }
  488. if ((xcorr[2]-xcorr[0]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[0]))
  489. offset = 1;
  490. else if ((xcorr[0]-xcorr[2]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[2]))
  491. offset = -1;
  492. else
  493. offset = 0;
  494. if (pg > g)
  495. pg = g;
  496. *T0_ = 2*T+offset;
  497. if (*T0_<minperiod0)
  498. *T0_=minperiod0;
  499. RESTORE_STACK;
  500. return pg;
  501. }