pitch.c 14 KB

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