celt_lpc.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* Copyright (c) 2009-2010 Xiph.Org Foundation
  2. Written by Jean-Marc Valin */
  3. /*
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  16. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include "celt_lpc.h"
  28. #include "stack_alloc.h"
  29. #include "mathops.h"
  30. #include "pitch.h"
  31. void _celt_lpc(
  32. opus_val16 *_lpc, /* out: [0...p-1] LPC coefficients */
  33. const opus_val32 *ac, /* in: [0...p] autocorrelation values */
  34. int p
  35. )
  36. {
  37. int i, j;
  38. opus_val32 r;
  39. opus_val32 error = ac[0];
  40. #ifdef FIXED_POINT
  41. opus_val32 lpc[LPC_ORDER];
  42. #else
  43. float *lpc = _lpc;
  44. #endif
  45. OPUS_CLEAR(lpc, p);
  46. #ifdef FIXED_POINT
  47. if (ac[0] != 0)
  48. #else
  49. if (ac[0] > 1e-10f)
  50. #endif
  51. {
  52. for (i = 0; i < p; i++) {
  53. /* Sum up this iteration's reflection coefficient */
  54. opus_val32 rr = 0;
  55. for (j = 0; j < i; j++)
  56. rr += MULT32_32_Q31(lpc[j],ac[i - j]);
  57. rr += SHR32(ac[i + 1],6);
  58. r = -frac_div32(SHL32(rr,6), error);
  59. /* Update LPC coefficients and total error */
  60. lpc[i] = SHR32(r,6);
  61. for (j = 0; j < (i+1)>>1; j++)
  62. {
  63. opus_val32 tmp1, tmp2;
  64. tmp1 = lpc[j];
  65. tmp2 = lpc[i-1-j];
  66. lpc[j] = tmp1 + MULT32_32_Q31(r,tmp2);
  67. lpc[i-1-j] = tmp2 + MULT32_32_Q31(r,tmp1);
  68. }
  69. error = error - MULT32_32_Q31(MULT32_32_Q31(r,r),error);
  70. /* Bail out once we get 30 dB gain */
  71. #ifdef FIXED_POINT
  72. if (error<=SHR32(ac[0],10))
  73. break;
  74. #else
  75. if (error<=.001f*ac[0])
  76. break;
  77. #endif
  78. }
  79. }
  80. #ifdef FIXED_POINT
  81. {
  82. /* Convert the int32 lpcs to int16 and ensure there are no wrap-arounds.
  83. This reuses the logic in silk_LPC_fit() and silk_bwexpander_32(). Any bug
  84. fixes should also be applied there. */
  85. int iter, idx = 0;
  86. opus_val32 maxabs, absval, chirp_Q16, chirp_minus_one_Q16;
  87. for (iter = 0; iter < 10; iter++) {
  88. maxabs = 0;
  89. for (i = 0; i < p; i++) {
  90. absval = ABS32(lpc[i]);
  91. if (absval > maxabs) {
  92. maxabs = absval;
  93. idx = i;
  94. }
  95. }
  96. maxabs = PSHR32(maxabs, 13); /* Q25->Q12 */
  97. if (maxabs > 32767) {
  98. maxabs = MIN32(maxabs, 163838);
  99. chirp_Q16 = QCONST32(0.999, 16) - DIV32(SHL32(maxabs - 32767, 14),
  100. SHR32(MULT32_32_32(maxabs, idx + 1), 2));
  101. chirp_minus_one_Q16 = chirp_Q16 - 65536;
  102. /* Apply bandwidth expansion. */
  103. for (i = 0; i < p - 1; i++) {
  104. lpc[i] = MULT32_32_Q16(chirp_Q16, lpc[i]);
  105. chirp_Q16 += PSHR32(MULT32_32_32(chirp_Q16, chirp_minus_one_Q16), 16);
  106. }
  107. lpc[p - 1] = MULT32_32_Q16(chirp_Q16, lpc[p - 1]);
  108. } else {
  109. break;
  110. }
  111. }
  112. if (iter == 10) {
  113. /* If the coeffs still do not fit into the 16 bit range after 10 iterations,
  114. fall back to the A(z)=1 filter. */
  115. OPUS_CLEAR(lpc, p);
  116. _lpc[0] = 4096; /* Q12 */
  117. } else {
  118. for (i = 0; i < p; i++) {
  119. _lpc[i] = EXTRACT16(PSHR32(lpc[i], 13)); /* Q25->Q12 */
  120. }
  121. }
  122. }
  123. #endif
  124. }
  125. void celt_fir_c(
  126. const opus_val16 *x,
  127. const opus_val16 *num,
  128. opus_val16 *y,
  129. int N,
  130. int ord,
  131. int arch)
  132. {
  133. int i,j;
  134. VARDECL(opus_val16, rnum);
  135. SAVE_STACK;
  136. celt_assert(x != y);
  137. ALLOC(rnum, ord, opus_val16);
  138. for(i=0;i<ord;i++)
  139. rnum[i] = num[ord-i-1];
  140. for (i=0;i<N-3;i+=4)
  141. {
  142. opus_val32 sum[4];
  143. sum[0] = SHL32(EXTEND32(x[i ]), SIG_SHIFT);
  144. sum[1] = SHL32(EXTEND32(x[i+1]), SIG_SHIFT);
  145. sum[2] = SHL32(EXTEND32(x[i+2]), SIG_SHIFT);
  146. sum[3] = SHL32(EXTEND32(x[i+3]), SIG_SHIFT);
  147. xcorr_kernel(rnum, x+i-ord, sum, ord, arch);
  148. y[i ] = ROUND16(sum[0], SIG_SHIFT);
  149. y[i+1] = ROUND16(sum[1], SIG_SHIFT);
  150. y[i+2] = ROUND16(sum[2], SIG_SHIFT);
  151. y[i+3] = ROUND16(sum[3], SIG_SHIFT);
  152. }
  153. for (;i<N;i++)
  154. {
  155. opus_val32 sum = SHL32(EXTEND32(x[i]), SIG_SHIFT);
  156. for (j=0;j<ord;j++)
  157. sum = MAC16_16(sum,rnum[j],x[i+j-ord]);
  158. y[i] = ROUND16(sum, SIG_SHIFT);
  159. }
  160. RESTORE_STACK;
  161. }
  162. void celt_iir(const opus_val32 *_x,
  163. const opus_val16 *den,
  164. opus_val32 *_y,
  165. int N,
  166. int ord,
  167. opus_val16 *mem,
  168. int arch)
  169. {
  170. #ifdef SMALL_FOOTPRINT
  171. int i,j;
  172. (void)arch;
  173. for (i=0;i<N;i++)
  174. {
  175. opus_val32 sum = _x[i];
  176. for (j=0;j<ord;j++)
  177. {
  178. sum -= MULT16_16(den[j],mem[j]);
  179. }
  180. for (j=ord-1;j>=1;j--)
  181. {
  182. mem[j]=mem[j-1];
  183. }
  184. mem[0] = SROUND16(sum, SIG_SHIFT);
  185. _y[i] = sum;
  186. }
  187. #else
  188. int i,j;
  189. VARDECL(opus_val16, rden);
  190. VARDECL(opus_val16, y);
  191. SAVE_STACK;
  192. celt_assert((ord&3)==0);
  193. ALLOC(rden, ord, opus_val16);
  194. ALLOC(y, N+ord, opus_val16);
  195. for(i=0;i<ord;i++)
  196. rden[i] = den[ord-i-1];
  197. for(i=0;i<ord;i++)
  198. y[i] = -mem[ord-i-1];
  199. for(;i<N+ord;i++)
  200. y[i]=0;
  201. for (i=0;i<N-3;i+=4)
  202. {
  203. /* Unroll by 4 as if it were an FIR filter */
  204. opus_val32 sum[4];
  205. sum[0]=_x[i];
  206. sum[1]=_x[i+1];
  207. sum[2]=_x[i+2];
  208. sum[3]=_x[i+3];
  209. xcorr_kernel(rden, y+i, sum, ord, arch);
  210. /* Patch up the result to compensate for the fact that this is an IIR */
  211. y[i+ord ] = -SROUND16(sum[0],SIG_SHIFT);
  212. _y[i ] = sum[0];
  213. sum[1] = MAC16_16(sum[1], y[i+ord ], den[0]);
  214. y[i+ord+1] = -SROUND16(sum[1],SIG_SHIFT);
  215. _y[i+1] = sum[1];
  216. sum[2] = MAC16_16(sum[2], y[i+ord+1], den[0]);
  217. sum[2] = MAC16_16(sum[2], y[i+ord ], den[1]);
  218. y[i+ord+2] = -SROUND16(sum[2],SIG_SHIFT);
  219. _y[i+2] = sum[2];
  220. sum[3] = MAC16_16(sum[3], y[i+ord+2], den[0]);
  221. sum[3] = MAC16_16(sum[3], y[i+ord+1], den[1]);
  222. sum[3] = MAC16_16(sum[3], y[i+ord ], den[2]);
  223. y[i+ord+3] = -SROUND16(sum[3],SIG_SHIFT);
  224. _y[i+3] = sum[3];
  225. }
  226. for (;i<N;i++)
  227. {
  228. opus_val32 sum = _x[i];
  229. for (j=0;j<ord;j++)
  230. sum -= MULT16_16(rden[j],y[i+j]);
  231. y[i+ord] = SROUND16(sum,SIG_SHIFT);
  232. _y[i] = sum;
  233. }
  234. for(i=0;i<ord;i++)
  235. mem[i] = _y[N-i-1];
  236. RESTORE_STACK;
  237. #endif
  238. }
  239. int _celt_autocorr(
  240. const opus_val16 *x, /* in: [0...n-1] samples x */
  241. opus_val32 *ac, /* out: [0...lag-1] ac values */
  242. const opus_val16 *window,
  243. int overlap,
  244. int lag,
  245. int n,
  246. int arch
  247. )
  248. {
  249. opus_val32 d;
  250. int i, k;
  251. int fastN=n-lag;
  252. int shift;
  253. const opus_val16 *xptr;
  254. VARDECL(opus_val16, xx);
  255. SAVE_STACK;
  256. ALLOC(xx, n, opus_val16);
  257. celt_assert(n>0);
  258. celt_assert(overlap>=0);
  259. if (overlap == 0)
  260. {
  261. xptr = x;
  262. } else {
  263. for (i=0;i<n;i++)
  264. xx[i] = x[i];
  265. for (i=0;i<overlap;i++)
  266. {
  267. xx[i] = MULT16_16_Q15(x[i],window[i]);
  268. xx[n-i-1] = MULT16_16_Q15(x[n-i-1],window[i]);
  269. }
  270. xptr = xx;
  271. }
  272. shift=0;
  273. #ifdef FIXED_POINT
  274. {
  275. opus_val32 ac0;
  276. ac0 = 1+(n<<7);
  277. if (n&1) ac0 += SHR32(MULT16_16(xptr[0],xptr[0]),9);
  278. for(i=(n&1);i<n;i+=2)
  279. {
  280. ac0 += SHR32(MULT16_16(xptr[i],xptr[i]),9);
  281. ac0 += SHR32(MULT16_16(xptr[i+1],xptr[i+1]),9);
  282. }
  283. shift = celt_ilog2(ac0)-30+10;
  284. shift = (shift)/2;
  285. if (shift>0)
  286. {
  287. for(i=0;i<n;i++)
  288. xx[i] = PSHR32(xptr[i], shift);
  289. xptr = xx;
  290. } else
  291. shift = 0;
  292. }
  293. #endif
  294. celt_pitch_xcorr(xptr, xptr, ac, fastN, lag+1, arch);
  295. for (k=0;k<=lag;k++)
  296. {
  297. for (i = k+fastN, d = 0; i < n; i++)
  298. d = MAC16_16(d, xptr[i], xptr[i-k]);
  299. ac[k] += d;
  300. }
  301. #ifdef FIXED_POINT
  302. shift = 2*shift;
  303. if (shift<=0)
  304. ac[0] += SHL32((opus_int32)1, -shift);
  305. if (ac[0] < 268435456)
  306. {
  307. int shift2 = 29 - EC_ILOG(ac[0]);
  308. for (i=0;i<=lag;i++)
  309. ac[i] = SHL32(ac[i], shift2);
  310. shift -= shift2;
  311. } else if (ac[0] >= 536870912)
  312. {
  313. int shift2=1;
  314. if (ac[0] >= 1073741824)
  315. shift2++;
  316. for (i=0;i<=lag;i++)
  317. ac[i] = SHR32(ac[i], shift2);
  318. shift += shift2;
  319. }
  320. #endif
  321. RESTORE_STACK;
  322. return shift;
  323. }