pitch.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. /* Pure C implementation. */
  202. #ifdef FIXED_POINT
  203. opus_val32
  204. #else
  205. void
  206. #endif
  207. celt_pitch_xcorr_c(const opus_val16 *_x, const opus_val16 *_y,
  208. opus_val32 *xcorr, int len, int max_pitch, int arch)
  209. {
  210. #if 0 /* This is a simple version of the pitch correlation that should work
  211. well on DSPs like Blackfin and TI C5x/C6x */
  212. int i, j;
  213. #ifdef FIXED_POINT
  214. opus_val32 maxcorr=1;
  215. #endif
  216. #if !defined(OVERRIDE_PITCH_XCORR)
  217. (void)arch;
  218. #endif
  219. for (i=0;i<max_pitch;i++)
  220. {
  221. opus_val32 sum = 0;
  222. for (j=0;j<len;j++)
  223. sum = MAC16_16(sum, _x[j], _y[i+j]);
  224. xcorr[i] = sum;
  225. #ifdef FIXED_POINT
  226. maxcorr = MAX32(maxcorr, sum);
  227. #endif
  228. }
  229. #ifdef FIXED_POINT
  230. return maxcorr;
  231. #endif
  232. #else /* Unrolled version of the pitch correlation -- runs faster on x86 and ARM */
  233. int i;
  234. /*The EDSP version requires that max_pitch is at least 1, and that _x is
  235. 32-bit aligned.
  236. Since it's hard to put asserts in assembly, put them here.*/
  237. #ifdef FIXED_POINT
  238. opus_val32 maxcorr=1;
  239. #endif
  240. celt_assert(max_pitch>0);
  241. celt_assert((((unsigned char *)_x-(unsigned char *)NULL)&3)==0);
  242. for (i=0;i<max_pitch-3;i+=4)
  243. {
  244. opus_val32 sum[4]={0,0,0,0};
  245. xcorr_kernel(_x, _y+i, sum, len, arch);
  246. xcorr[i]=sum[0];
  247. xcorr[i+1]=sum[1];
  248. xcorr[i+2]=sum[2];
  249. xcorr[i+3]=sum[3];
  250. #ifdef FIXED_POINT
  251. sum[0] = MAX32(sum[0], sum[1]);
  252. sum[2] = MAX32(sum[2], sum[3]);
  253. sum[0] = MAX32(sum[0], sum[2]);
  254. maxcorr = MAX32(maxcorr, sum[0]);
  255. #endif
  256. }
  257. /* In case max_pitch isn't a multiple of 4, do non-unrolled version. */
  258. for (;i<max_pitch;i++)
  259. {
  260. opus_val32 sum;
  261. sum = celt_inner_prod(_x, _y+i, len, arch);
  262. xcorr[i] = sum;
  263. #ifdef FIXED_POINT
  264. maxcorr = MAX32(maxcorr, sum);
  265. #endif
  266. }
  267. #ifdef FIXED_POINT
  268. return maxcorr;
  269. #endif
  270. #endif
  271. }
  272. void pitch_search(const opus_val16 * OPUS_RESTRICT x_lp, opus_val16 * OPUS_RESTRICT y,
  273. int len, int max_pitch, int *pitch, int arch)
  274. {
  275. int i, j;
  276. int lag;
  277. int best_pitch[2]={0,0};
  278. VARDECL(opus_val16, x_lp4);
  279. VARDECL(opus_val16, y_lp4);
  280. VARDECL(opus_val32, xcorr);
  281. #ifdef FIXED_POINT
  282. opus_val32 maxcorr;
  283. opus_val32 xmax, ymax;
  284. int shift=0;
  285. #endif
  286. int offset;
  287. SAVE_STACK;
  288. celt_assert(len>0);
  289. celt_assert(max_pitch>0);
  290. lag = len+max_pitch;
  291. ALLOC(x_lp4, len>>2, opus_val16);
  292. ALLOC(y_lp4, lag>>2, opus_val16);
  293. ALLOC(xcorr, max_pitch>>1, opus_val32);
  294. /* Downsample by 2 again */
  295. for (j=0;j<len>>2;j++)
  296. x_lp4[j] = x_lp[2*j];
  297. for (j=0;j<lag>>2;j++)
  298. y_lp4[j] = y[2*j];
  299. #ifdef FIXED_POINT
  300. xmax = celt_maxabs16(x_lp4, len>>2);
  301. ymax = celt_maxabs16(y_lp4, lag>>2);
  302. shift = celt_ilog2(MAX32(1, MAX32(xmax, ymax)))-11;
  303. if (shift>0)
  304. {
  305. for (j=0;j<len>>2;j++)
  306. x_lp4[j] = SHR16(x_lp4[j], shift);
  307. for (j=0;j<lag>>2;j++)
  308. y_lp4[j] = SHR16(y_lp4[j], shift);
  309. /* Use double the shift for a MAC */
  310. shift *= 2;
  311. } else {
  312. shift = 0;
  313. }
  314. #endif
  315. /* Coarse search with 4x decimation */
  316. #ifdef FIXED_POINT
  317. maxcorr =
  318. #endif
  319. celt_pitch_xcorr(x_lp4, y_lp4, xcorr, len>>2, max_pitch>>2, arch);
  320. find_best_pitch(xcorr, y_lp4, len>>2, max_pitch>>2, best_pitch
  321. #ifdef FIXED_POINT
  322. , 0, maxcorr
  323. #endif
  324. );
  325. /* Finer search with 2x decimation */
  326. #ifdef FIXED_POINT
  327. maxcorr=1;
  328. #endif
  329. for (i=0;i<max_pitch>>1;i++)
  330. {
  331. opus_val32 sum;
  332. xcorr[i] = 0;
  333. if (abs(i-2*best_pitch[0])>2 && abs(i-2*best_pitch[1])>2)
  334. continue;
  335. #ifdef FIXED_POINT
  336. sum = 0;
  337. for (j=0;j<len>>1;j++)
  338. sum += SHR32(MULT16_16(x_lp[j],y[i+j]), shift);
  339. #else
  340. sum = celt_inner_prod(x_lp, y+i, len>>1, arch);
  341. #endif
  342. xcorr[i] = MAX32(-1, sum);
  343. #ifdef FIXED_POINT
  344. maxcorr = MAX32(maxcorr, sum);
  345. #endif
  346. }
  347. find_best_pitch(xcorr, y, len>>1, max_pitch>>1, best_pitch
  348. #ifdef FIXED_POINT
  349. , shift+1, maxcorr
  350. #endif
  351. );
  352. /* Refine by pseudo-interpolation */
  353. if (best_pitch[0]>0 && best_pitch[0]<(max_pitch>>1)-1)
  354. {
  355. opus_val32 a, b, c;
  356. a = xcorr[best_pitch[0]-1];
  357. b = xcorr[best_pitch[0]];
  358. c = xcorr[best_pitch[0]+1];
  359. if ((c-a) > MULT16_32_Q15(QCONST16(.7f,15),b-a))
  360. offset = 1;
  361. else if ((a-c) > MULT16_32_Q15(QCONST16(.7f,15),b-c))
  362. offset = -1;
  363. else
  364. offset = 0;
  365. } else {
  366. offset = 0;
  367. }
  368. *pitch = 2*best_pitch[0]-offset;
  369. RESTORE_STACK;
  370. }
  371. #ifdef FIXED_POINT
  372. static opus_val16 compute_pitch_gain(opus_val32 xy, opus_val32 xx, opus_val32 yy)
  373. {
  374. opus_val32 x2y2;
  375. int sx, sy, shift;
  376. opus_val32 g;
  377. opus_val16 den;
  378. if (xy == 0 || xx == 0 || yy == 0)
  379. return 0;
  380. sx = celt_ilog2(xx)-14;
  381. sy = celt_ilog2(yy)-14;
  382. shift = sx + sy;
  383. x2y2 = SHR32(MULT16_16(VSHR32(xx, sx), VSHR32(yy, sy)), 14);
  384. if (shift & 1) {
  385. if (x2y2 < 32768)
  386. {
  387. x2y2 <<= 1;
  388. shift--;
  389. } else {
  390. x2y2 >>= 1;
  391. shift++;
  392. }
  393. }
  394. den = celt_rsqrt_norm(x2y2);
  395. g = MULT16_32_Q15(den, xy);
  396. g = VSHR32(g, (shift>>1)-1);
  397. return EXTRACT16(MIN32(g, Q15ONE));
  398. }
  399. #else
  400. static opus_val16 compute_pitch_gain(opus_val32 xy, opus_val32 xx, opus_val32 yy)
  401. {
  402. return xy/celt_sqrt(1+xx*yy);
  403. }
  404. #endif
  405. static const int second_check[16] = {0, 0, 3, 2, 3, 2, 5, 2, 3, 2, 3, 2, 5, 2, 3, 2};
  406. opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod,
  407. int N, int *T0_, int prev_period, opus_val16 prev_gain, int arch)
  408. {
  409. int k, i, T, T0;
  410. opus_val16 g, g0;
  411. opus_val16 pg;
  412. opus_val32 xy,xx,yy,xy2;
  413. opus_val32 xcorr[3];
  414. opus_val32 best_xy, best_yy;
  415. int offset;
  416. int minperiod0;
  417. VARDECL(opus_val32, yy_lookup);
  418. SAVE_STACK;
  419. minperiod0 = minperiod;
  420. maxperiod /= 2;
  421. minperiod /= 2;
  422. *T0_ /= 2;
  423. prev_period /= 2;
  424. N /= 2;
  425. x += maxperiod;
  426. if (*T0_>=maxperiod)
  427. *T0_=maxperiod-1;
  428. T = T0 = *T0_;
  429. ALLOC(yy_lookup, maxperiod+1, opus_val32);
  430. dual_inner_prod(x, x, x-T0, N, &xx, &xy, arch);
  431. yy_lookup[0] = xx;
  432. yy=xx;
  433. for (i=1;i<=maxperiod;i++)
  434. {
  435. yy = yy+MULT16_16(x[-i],x[-i])-MULT16_16(x[N-i],x[N-i]);
  436. yy_lookup[i] = MAX32(0, yy);
  437. }
  438. yy = yy_lookup[T0];
  439. best_xy = xy;
  440. best_yy = yy;
  441. g = g0 = compute_pitch_gain(xy, xx, yy);
  442. /* Look for any pitch at T/k */
  443. for (k=2;k<=15;k++)
  444. {
  445. int T1, T1b;
  446. opus_val16 g1;
  447. opus_val16 cont=0;
  448. opus_val16 thresh;
  449. T1 = celt_udiv(2*T0+k, 2*k);
  450. if (T1 < minperiod)
  451. break;
  452. /* Look for another strong correlation at T1b */
  453. if (k==2)
  454. {
  455. if (T1+T0>maxperiod)
  456. T1b = T0;
  457. else
  458. T1b = T0+T1;
  459. } else
  460. {
  461. T1b = celt_udiv(2*second_check[k]*T0+k, 2*k);
  462. }
  463. dual_inner_prod(x, &x[-T1], &x[-T1b], N, &xy, &xy2, arch);
  464. xy = HALF32(xy + xy2);
  465. yy = HALF32(yy_lookup[T1] + yy_lookup[T1b]);
  466. g1 = compute_pitch_gain(xy, xx, yy);
  467. if (abs(T1-prev_period)<=1)
  468. cont = prev_gain;
  469. else if (abs(T1-prev_period)<=2 && 5*k*k < T0)
  470. cont = HALF16(prev_gain);
  471. else
  472. cont = 0;
  473. thresh = MAX16(QCONST16(.3f,15), MULT16_16_Q15(QCONST16(.7f,15),g0)-cont);
  474. /* Bias against very high pitch (very short period) to avoid false-positives
  475. due to short-term correlation */
  476. if (T1<3*minperiod)
  477. thresh = MAX16(QCONST16(.4f,15), MULT16_16_Q15(QCONST16(.85f,15),g0)-cont);
  478. else if (T1<2*minperiod)
  479. thresh = MAX16(QCONST16(.5f,15), MULT16_16_Q15(QCONST16(.9f,15),g0)-cont);
  480. if (g1 > thresh)
  481. {
  482. best_xy = xy;
  483. best_yy = yy;
  484. T = T1;
  485. g = g1;
  486. }
  487. }
  488. best_xy = MAX32(0, best_xy);
  489. if (best_yy <= best_xy)
  490. pg = Q15ONE;
  491. else
  492. pg = SHR32(frac_div32(best_xy,best_yy+1),16);
  493. for (k=0;k<3;k++)
  494. xcorr[k] = celt_inner_prod(x, x-(T+k-1), N, arch);
  495. if ((xcorr[2]-xcorr[0]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[0]))
  496. offset = 1;
  497. else if ((xcorr[0]-xcorr[2]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[2]))
  498. offset = -1;
  499. else
  500. offset = 0;
  501. if (pg > g)
  502. pg = g;
  503. *T0_ = 2*T+offset;
  504. if (*T0_<minperiod0)
  505. *T0_=minperiod0;
  506. RESTORE_STACK;
  507. return pg;
  508. }