pitch.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* Copyright (c) 2007-2012 IETF Trust, CSIRO, Xiph.Org Foundation. All rights reserved.
  2. Written by Jean-Marc Valin */
  3. /**
  4. @file pitch.c
  5. @brief Pitch analysis
  6. */
  7. /*
  8. This file is extracted from RFC6716. Please see that RFC for additional
  9. information.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. - Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. - Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in the
  17. documentation and/or other materials provided with the distribution.
  18. - Neither the name of Internet Society, IETF or IETF Trust, nor the
  19. names of specific contributors, may be used to endorse or promote
  20. products derived from this software without specific prior written
  21. permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  26. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifdef HAVE_CONFIG_H
  35. #include "config.h"
  36. #endif
  37. #include "pitch.h"
  38. #include "os_support.h"
  39. #include "modes.h"
  40. #include "stack_alloc.h"
  41. #include "mathops.h"
  42. #include "celt_lpc.h"
  43. static void find_best_pitch(opus_val32 *xcorr, opus_val16 *y, int len,
  44. int max_pitch, int *best_pitch
  45. #ifdef FIXED_POINT
  46. , int yshift, opus_val32 maxcorr
  47. #endif
  48. )
  49. {
  50. int i, j;
  51. opus_val32 Syy=1;
  52. opus_val16 best_num[2];
  53. opus_val32 best_den[2];
  54. #ifdef FIXED_POINT
  55. int xshift;
  56. xshift = celt_ilog2(maxcorr)-14;
  57. #endif
  58. best_num[0] = -1;
  59. best_num[1] = -1;
  60. best_den[0] = 0;
  61. best_den[1] = 0;
  62. best_pitch[0] = 0;
  63. best_pitch[1] = 1;
  64. for (j=0;j<len;j++)
  65. Syy = MAC16_16(Syy, y[j],y[j]);
  66. for (i=0;i<max_pitch;i++)
  67. {
  68. if (xcorr[i]>0)
  69. {
  70. opus_val16 num;
  71. opus_val32 xcorr16;
  72. xcorr16 = EXTRACT16(VSHR32(xcorr[i], xshift));
  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. void pitch_downsample(celt_sig * restrict x[], opus_val16 * restrict x_lp,
  96. int len, int C)
  97. {
  98. int i;
  99. opus_val32 ac[5];
  100. opus_val16 tmp=Q15ONE;
  101. opus_val16 lpc[4], mem[4]={0,0,0,0};
  102. for (i=1;i<len>>1;i++)
  103. x_lp[i] = SHR32(HALF32(HALF32(x[0][(2*i-1)]+x[0][(2*i+1)])+x[0][2*i]), SIG_SHIFT+3);
  104. x_lp[0] = SHR32(HALF32(HALF32(x[0][1])+x[0][0]), SIG_SHIFT+3);
  105. if (C==2)
  106. {
  107. for (i=1;i<len>>1;i++)
  108. x_lp[i] += SHR32(HALF32(HALF32(x[1][(2*i-1)]+x[1][(2*i+1)])+x[1][2*i]), SIG_SHIFT+3);
  109. x_lp[0] += SHR32(HALF32(HALF32(x[1][1])+x[1][0]), SIG_SHIFT+3);
  110. }
  111. _celt_autocorr(x_lp, ac, NULL, 0,
  112. 4, len>>1);
  113. /* Noise floor -40 dB */
  114. #ifdef FIXED_POINT
  115. ac[0] += SHR32(ac[0],13);
  116. #else
  117. ac[0] *= 1.0001f;
  118. #endif
  119. /* Lag windowing */
  120. for (i=1;i<=4;i++)
  121. {
  122. /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
  123. #ifdef FIXED_POINT
  124. ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
  125. #else
  126. ac[i] -= ac[i]*(.008f*i)*(.008f*i);
  127. #endif
  128. }
  129. _celt_lpc(lpc, ac, 4);
  130. for (i=0;i<4;i++)
  131. {
  132. tmp = MULT16_16_Q15(QCONST16(.9f,15), tmp);
  133. lpc[i] = MULT16_16_Q15(lpc[i], tmp);
  134. }
  135. celt_fir(x_lp, lpc, x_lp, len>>1, 4, mem);
  136. mem[0]=0;
  137. lpc[0]=QCONST16(.8f,12);
  138. celt_fir(x_lp, lpc, x_lp, len>>1, 1, mem);
  139. }
  140. void pitch_search(const opus_val16 * restrict x_lp, opus_val16 * restrict y,
  141. int len, int max_pitch, int *pitch)
  142. {
  143. int i, j;
  144. int lag;
  145. int best_pitch[2]={0,0};
  146. VARDECL(opus_val16, x_lp4);
  147. VARDECL(opus_val16, y_lp4);
  148. VARDECL(opus_val32, xcorr);
  149. #ifdef FIXED_POINT
  150. opus_val32 maxcorr=1;
  151. int shift=0;
  152. #endif
  153. int offset;
  154. SAVE_STACK;
  155. celt_assert(len>0);
  156. celt_assert(max_pitch>0);
  157. lag = len+max_pitch;
  158. ALLOC(x_lp4, len>>2, opus_val16);
  159. ALLOC(y_lp4, lag>>2, opus_val16);
  160. ALLOC(xcorr, max_pitch>>1, opus_val32);
  161. /* Downsample by 2 again */
  162. for (j=0;j<len>>2;j++)
  163. x_lp4[j] = x_lp[2*j];
  164. for (j=0;j<lag>>2;j++)
  165. y_lp4[j] = y[2*j];
  166. #ifdef FIXED_POINT
  167. shift = celt_ilog2(MAX16(1, MAX16(celt_maxabs16(x_lp4, len>>2), celt_maxabs16(y_lp4, lag>>2))))-11;
  168. if (shift>0)
  169. {
  170. for (j=0;j<len>>2;j++)
  171. x_lp4[j] = SHR16(x_lp4[j], shift);
  172. for (j=0;j<lag>>2;j++)
  173. y_lp4[j] = SHR16(y_lp4[j], shift);
  174. /* Use double the shift for a MAC */
  175. shift *= 2;
  176. } else {
  177. shift = 0;
  178. }
  179. #endif
  180. /* Coarse search with 4x decimation */
  181. for (i=0;i<max_pitch>>2;i++)
  182. {
  183. opus_val32 sum = 0;
  184. for (j=0;j<len>>2;j++)
  185. sum = MAC16_16(sum, x_lp4[j],y_lp4[i+j]);
  186. xcorr[i] = MAX32(-1, sum);
  187. #ifdef FIXED_POINT
  188. maxcorr = MAX32(maxcorr, sum);
  189. #endif
  190. }
  191. find_best_pitch(xcorr, y_lp4, len>>2, max_pitch>>2, best_pitch
  192. #ifdef FIXED_POINT
  193. , 0, maxcorr
  194. #endif
  195. );
  196. /* Finer search with 2x decimation */
  197. #ifdef FIXED_POINT
  198. maxcorr=1;
  199. #endif
  200. for (i=0;i<max_pitch>>1;i++)
  201. {
  202. opus_val32 sum=0;
  203. xcorr[i] = 0;
  204. if (abs(i-2*best_pitch[0])>2 && abs(i-2*best_pitch[1])>2)
  205. continue;
  206. for (j=0;j<len>>1;j++)
  207. sum += SHR32(MULT16_16(x_lp[j],y[i+j]), shift);
  208. xcorr[i] = MAX32(-1, sum);
  209. #ifdef FIXED_POINT
  210. maxcorr = MAX32(maxcorr, sum);
  211. #endif
  212. }
  213. find_best_pitch(xcorr, y, len>>1, max_pitch>>1, best_pitch
  214. #ifdef FIXED_POINT
  215. , shift, maxcorr
  216. #endif
  217. );
  218. /* Refine by pseudo-interpolation */
  219. if (best_pitch[0]>0 && best_pitch[0]<(max_pitch>>1)-1)
  220. {
  221. opus_val32 a, b, c;
  222. a = xcorr[best_pitch[0]-1];
  223. b = xcorr[best_pitch[0]];
  224. c = xcorr[best_pitch[0]+1];
  225. if ((c-a) > MULT16_32_Q15(QCONST16(.7f,15),b-a))
  226. offset = 1;
  227. else if ((a-c) > MULT16_32_Q15(QCONST16(.7f,15),b-c))
  228. offset = -1;
  229. else
  230. offset = 0;
  231. } else {
  232. offset = 0;
  233. }
  234. *pitch = 2*best_pitch[0]-offset;
  235. RESTORE_STACK;
  236. }
  237. static const int second_check[16] = {0, 0, 3, 2, 3, 2, 5, 2, 3, 2, 3, 2, 5, 2, 3, 2};
  238. opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod,
  239. int N, int *T0_, int prev_period, opus_val16 prev_gain)
  240. {
  241. int k, i, T, T0;
  242. opus_val16 g, g0;
  243. opus_val16 pg;
  244. opus_val32 xy,xx,yy;
  245. opus_val32 xcorr[3];
  246. opus_val32 best_xy, best_yy;
  247. int offset;
  248. int minperiod0;
  249. minperiod0 = minperiod;
  250. maxperiod /= 2;
  251. minperiod /= 2;
  252. *T0_ /= 2;
  253. prev_period /= 2;
  254. N /= 2;
  255. x += maxperiod;
  256. if (*T0_>=maxperiod)
  257. *T0_=maxperiod-1;
  258. T = T0 = *T0_;
  259. xx=xy=yy=0;
  260. for (i=0;i<N;i++)
  261. {
  262. xy = MAC16_16(xy, x[i], x[i-T0]);
  263. xx = MAC16_16(xx, x[i], x[i]);
  264. yy = MAC16_16(yy, x[i-T0],x[i-T0]);
  265. }
  266. best_xy = xy;
  267. best_yy = yy;
  268. #ifdef FIXED_POINT
  269. {
  270. opus_val32 x2y2;
  271. int sh, t;
  272. x2y2 = 1+HALF32(MULT32_32_Q31(xx,yy));
  273. sh = celt_ilog2(x2y2)>>1;
  274. t = VSHR32(x2y2, 2*(sh-7));
  275. g = g0 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
  276. }
  277. #else
  278. g = g0 = xy/celt_sqrt(1+xx*yy);
  279. #endif
  280. /* Look for any pitch at T/k */
  281. for (k=2;k<=15;k++)
  282. {
  283. int T1, T1b;
  284. opus_val16 g1;
  285. opus_val16 cont=0;
  286. T1 = (2*T0+k)/(2*k);
  287. if (T1 < minperiod)
  288. break;
  289. /* Look for another strong correlation at T1b */
  290. if (k==2)
  291. {
  292. if (T1+T0>maxperiod)
  293. T1b = T0;
  294. else
  295. T1b = T0+T1;
  296. } else
  297. {
  298. T1b = (2*second_check[k]*T0+k)/(2*k);
  299. }
  300. xy=yy=0;
  301. for (i=0;i<N;i++)
  302. {
  303. xy = MAC16_16(xy, x[i], x[i-T1]);
  304. yy = MAC16_16(yy, x[i-T1], x[i-T1]);
  305. xy = MAC16_16(xy, x[i], x[i-T1b]);
  306. yy = MAC16_16(yy, x[i-T1b], x[i-T1b]);
  307. }
  308. #ifdef FIXED_POINT
  309. {
  310. opus_val32 x2y2;
  311. int sh, t;
  312. x2y2 = 1+MULT32_32_Q31(xx,yy);
  313. sh = celt_ilog2(x2y2)>>1;
  314. t = VSHR32(x2y2, 2*(sh-7));
  315. g1 = VSHR32(MULT16_32_Q15(celt_rsqrt_norm(t), xy),sh+1);
  316. }
  317. #else
  318. g1 = xy/celt_sqrt(1+2.f*xx*1.f*yy);
  319. #endif
  320. if (abs(T1-prev_period)<=1)
  321. cont = prev_gain;
  322. else if (abs(T1-prev_period)<=2 && 5*k*k < T0)
  323. cont = HALF32(prev_gain);
  324. else
  325. cont = 0;
  326. if (g1 > QCONST16(.3f,15) + MULT16_16_Q15(QCONST16(.4f,15),g0)-cont)
  327. {
  328. best_xy = xy;
  329. best_yy = yy;
  330. T = T1;
  331. g = g1;
  332. }
  333. }
  334. if (best_yy <= best_xy)
  335. pg = Q15ONE;
  336. else
  337. pg = SHR32(frac_div32(best_xy,best_yy+1),16);
  338. for (k=0;k<3;k++)
  339. {
  340. int T1 = T+k-1;
  341. xy = 0;
  342. for (i=0;i<N;i++)
  343. xy = MAC16_16(xy, x[i], x[i-T1]);
  344. xcorr[k] = xy;
  345. }
  346. if ((xcorr[2]-xcorr[0]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[0]))
  347. offset = 1;
  348. else if ((xcorr[0]-xcorr[2]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[2]))
  349. offset = -1;
  350. else
  351. offset = 0;
  352. if (pg > g)
  353. pg = g;
  354. *T0_ = 2*T+offset;
  355. if (*T0_<minperiod0)
  356. *T0_=minperiod0;
  357. return pg;
  358. }