pitch.c 14 KB

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