pitch.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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;
  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;
  264. sum = celt_inner_prod(_x, _y+i, len);
  265. xcorr[i] = sum;
  266. #ifdef FIXED_POINT
  267. maxcorr = MAX32(maxcorr, sum);
  268. #endif
  269. }
  270. #ifdef FIXED_POINT
  271. return maxcorr;
  272. #endif
  273. }
  274. #endif
  275. void pitch_search(const opus_val16 * OPUS_RESTRICT x_lp, opus_val16 * OPUS_RESTRICT y,
  276. int len, int max_pitch, int *pitch, int arch)
  277. {
  278. int i, j;
  279. int lag;
  280. int best_pitch[2]={0,0};
  281. VARDECL(opus_val16, x_lp4);
  282. VARDECL(opus_val16, y_lp4);
  283. VARDECL(opus_val32, xcorr);
  284. #ifdef FIXED_POINT
  285. opus_val32 maxcorr;
  286. opus_val32 xmax, ymax;
  287. int shift=0;
  288. #endif
  289. int offset;
  290. SAVE_STACK;
  291. celt_assert(len>0);
  292. celt_assert(max_pitch>0);
  293. lag = len+max_pitch;
  294. ALLOC(x_lp4, len>>2, opus_val16);
  295. ALLOC(y_lp4, lag>>2, opus_val16);
  296. ALLOC(xcorr, max_pitch>>1, opus_val32);
  297. /* Downsample by 2 again */
  298. for (j=0;j<len>>2;j++)
  299. x_lp4[j] = x_lp[2*j];
  300. for (j=0;j<lag>>2;j++)
  301. y_lp4[j] = y[2*j];
  302. #ifdef FIXED_POINT
  303. xmax = celt_maxabs16(x_lp4, len>>2);
  304. ymax = celt_maxabs16(y_lp4, lag>>2);
  305. shift = celt_ilog2(MAX32(1, MAX32(xmax, ymax)))-11;
  306. if (shift>0)
  307. {
  308. for (j=0;j<len>>2;j++)
  309. x_lp4[j] = SHR16(x_lp4[j], shift);
  310. for (j=0;j<lag>>2;j++)
  311. y_lp4[j] = SHR16(y_lp4[j], shift);
  312. /* Use double the shift for a MAC */
  313. shift *= 2;
  314. } else {
  315. shift = 0;
  316. }
  317. #endif
  318. /* Coarse search with 4x decimation */
  319. #ifdef FIXED_POINT
  320. maxcorr =
  321. #endif
  322. celt_pitch_xcorr(x_lp4, y_lp4, xcorr, len>>2, max_pitch>>2, arch);
  323. find_best_pitch(xcorr, y_lp4, len>>2, max_pitch>>2, best_pitch
  324. #ifdef FIXED_POINT
  325. , 0, maxcorr
  326. #endif
  327. );
  328. /* Finer search with 2x decimation */
  329. #ifdef FIXED_POINT
  330. maxcorr=1;
  331. #endif
  332. for (i=0;i<max_pitch>>1;i++)
  333. {
  334. opus_val32 sum;
  335. xcorr[i] = 0;
  336. if (abs(i-2*best_pitch[0])>2 && abs(i-2*best_pitch[1])>2)
  337. continue;
  338. #ifdef FIXED_POINT
  339. sum = 0;
  340. for (j=0;j<len>>1;j++)
  341. sum += SHR32(MULT16_16(x_lp[j],y[i+j]), shift);
  342. #else
  343. sum = celt_inner_prod(x_lp, y+i, len>>1);
  344. #endif
  345. xcorr[i] = MAX32(-1, sum);
  346. #ifdef FIXED_POINT
  347. maxcorr = MAX32(maxcorr, sum);
  348. #endif
  349. }
  350. find_best_pitch(xcorr, y, len>>1, max_pitch>>1, best_pitch
  351. #ifdef FIXED_POINT
  352. , shift+1, maxcorr
  353. #endif
  354. );
  355. /* Refine by pseudo-interpolation */
  356. if (best_pitch[0]>0 && best_pitch[0]<(max_pitch>>1)-1)
  357. {
  358. opus_val32 a, b, c;
  359. a = xcorr[best_pitch[0]-1];
  360. b = xcorr[best_pitch[0]];
  361. c = xcorr[best_pitch[0]+1];
  362. if ((c-a) > MULT16_32_Q15(QCONST16(.7f,15),b-a))
  363. offset = 1;
  364. else if ((a-c) > MULT16_32_Q15(QCONST16(.7f,15),b-c))
  365. offset = -1;
  366. else
  367. offset = 0;
  368. } else {
  369. offset = 0;
  370. }
  371. *pitch = 2*best_pitch[0]-offset;
  372. RESTORE_STACK;
  373. }
  374. static const int second_check[16] = {0, 0, 3, 2, 3, 2, 5, 2, 3, 2, 3, 2, 5, 2, 3, 2};
  375. opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod,
  376. int N, int *T0_, int prev_period, opus_val16 prev_gain)
  377. {
  378. int k, i, T, T0;
  379. opus_val16 g, g0;
  380. opus_val16 pg;
  381. opus_val32 xy,xx,yy,xy2;
  382. opus_val32 xcorr[3];
  383. opus_val32 best_xy, best_yy;
  384. int offset;
  385. int minperiod0;
  386. VARDECL(opus_val32, yy_lookup);
  387. SAVE_STACK;
  388. minperiod0 = minperiod;
  389. maxperiod /= 2;
  390. minperiod /= 2;
  391. *T0_ /= 2;
  392. prev_period /= 2;
  393. N /= 2;
  394. x += maxperiod;
  395. if (*T0_>=maxperiod)
  396. *T0_=maxperiod-1;
  397. T = T0 = *T0_;
  398. ALLOC(yy_lookup, maxperiod+1, opus_val32);
  399. dual_inner_prod(x, x, x-T0, N, &xx, &xy);
  400. yy_lookup[0] = xx;
  401. yy=xx;
  402. for (i=1;i<=maxperiod;i++)
  403. {
  404. yy = yy+MULT16_16(x[-i],x[-i])-MULT16_16(x[N-i],x[N-i]);
  405. yy_lookup[i] = MAX32(0, yy);
  406. }
  407. yy = yy_lookup[T0];
  408. best_xy = xy;
  409. best_yy = yy;
  410. #ifdef FIXED_POINT
  411. {
  412. opus_val32 x2y2;
  413. int sh, t;
  414. x2y2 = 1+HALF32(MULT32_32_Q31(xx,yy));
  415. sh = celt_ilog2(x2y2)>>1;
  416. t = VSHR32(x2y2, 2*(sh-7));
  417. g = g0 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
  418. }
  419. #else
  420. g = g0 = xy/celt_sqrt(1+xx*yy);
  421. #endif
  422. /* Look for any pitch at T/k */
  423. for (k=2;k<=15;k++)
  424. {
  425. int T1, T1b;
  426. opus_val16 g1;
  427. opus_val16 cont=0;
  428. opus_val16 thresh;
  429. T1 = (2*T0+k)/(2*k);
  430. if (T1 < minperiod)
  431. break;
  432. /* Look for another strong correlation at T1b */
  433. if (k==2)
  434. {
  435. if (T1+T0>maxperiod)
  436. T1b = T0;
  437. else
  438. T1b = T0+T1;
  439. } else
  440. {
  441. T1b = (2*second_check[k]*T0+k)/(2*k);
  442. }
  443. dual_inner_prod(x, &x[-T1], &x[-T1b], N, &xy, &xy2);
  444. xy += xy2;
  445. yy = yy_lookup[T1] + yy_lookup[T1b];
  446. #ifdef FIXED_POINT
  447. {
  448. opus_val32 x2y2;
  449. int sh, t;
  450. x2y2 = 1+MULT32_32_Q31(xx,yy);
  451. sh = celt_ilog2(x2y2)>>1;
  452. t = VSHR32(x2y2, 2*(sh-7));
  453. g1 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
  454. }
  455. #else
  456. g1 = xy/celt_sqrt(1+2.f*xx*1.f*yy);
  457. #endif
  458. if (abs(T1-prev_period)<=1)
  459. cont = prev_gain;
  460. else if (abs(T1-prev_period)<=2 && 5*k*k < T0)
  461. cont = HALF32(prev_gain);
  462. else
  463. cont = 0;
  464. thresh = MAX16(QCONST16(.3f,15), MULT16_16_Q15(QCONST16(.7f,15),g0)-cont);
  465. /* Bias against very high pitch (very short period) to avoid false-positives
  466. due to short-term correlation */
  467. if (T1<3*minperiod)
  468. thresh = MAX16(QCONST16(.4f,15), MULT16_16_Q15(QCONST16(.85f,15),g0)-cont);
  469. else if (T1<2*minperiod)
  470. thresh = MAX16(QCONST16(.5f,15), MULT16_16_Q15(QCONST16(.9f,15),g0)-cont);
  471. if (g1 > thresh)
  472. {
  473. best_xy = xy;
  474. best_yy = yy;
  475. T = T1;
  476. g = g1;
  477. }
  478. }
  479. best_xy = MAX32(0, best_xy);
  480. if (best_yy <= best_xy)
  481. pg = Q15ONE;
  482. else
  483. pg = SHR32(frac_div32(best_xy,best_yy+1),16);
  484. for (k=0;k<3;k++)
  485. xcorr[k] = celt_inner_prod(x, x-(T+k-1), N);
  486. if ((xcorr[2]-xcorr[0]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[0]))
  487. offset = 1;
  488. else if ((xcorr[0]-xcorr[2]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[2]))
  489. offset = -1;
  490. else
  491. offset = 0;
  492. if (pg > g)
  493. pg = g;
  494. *T0_ = 2*T+offset;
  495. if (*T0_<minperiod0)
  496. *T0_=minperiod0;
  497. RESTORE_STACK;
  498. return pg;
  499. }