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