celt_lpc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. if (ac[0] != 0)
  47. {
  48. for (i = 0; i < p; i++) {
  49. /* Sum up this iteration's reflection coefficient */
  50. opus_val32 rr = 0;
  51. for (j = 0; j < i; j++)
  52. rr += MULT32_32_Q31(lpc[j],ac[i - j]);
  53. rr += SHR32(ac[i + 1],3);
  54. r = -frac_div32(SHL32(rr,3), error);
  55. /* Update LPC coefficients and total error */
  56. lpc[i] = SHR32(r,3);
  57. for (j = 0; j < (i+1)>>1; j++)
  58. {
  59. opus_val32 tmp1, tmp2;
  60. tmp1 = lpc[j];
  61. tmp2 = lpc[i-1-j];
  62. lpc[j] = tmp1 + MULT32_32_Q31(r,tmp2);
  63. lpc[i-1-j] = tmp2 + MULT32_32_Q31(r,tmp1);
  64. }
  65. error = error - MULT32_32_Q31(MULT32_32_Q31(r,r),error);
  66. /* Bail out once we get 30 dB gain */
  67. #ifdef FIXED_POINT
  68. if (error<SHR32(ac[0],10))
  69. break;
  70. #else
  71. if (error<.001f*ac[0])
  72. break;
  73. #endif
  74. }
  75. }
  76. #ifdef FIXED_POINT
  77. for (i=0;i<p;i++)
  78. _lpc[i] = ROUND16(lpc[i],16);
  79. #endif
  80. }
  81. void celt_fir_c(
  82. const opus_val16 *x,
  83. const opus_val16 *num,
  84. opus_val16 *y,
  85. int N,
  86. int ord,
  87. int arch)
  88. {
  89. int i,j;
  90. VARDECL(opus_val16, rnum);
  91. SAVE_STACK;
  92. celt_assert(x != y);
  93. ALLOC(rnum, ord, opus_val16);
  94. for(i=0;i<ord;i++)
  95. rnum[i] = num[ord-i-1];
  96. for (i=0;i<N-3;i+=4)
  97. {
  98. opus_val32 sum[4];
  99. sum[0] = SHL32(EXTEND32(x[i ]), SIG_SHIFT);
  100. sum[1] = SHL32(EXTEND32(x[i+1]), SIG_SHIFT);
  101. sum[2] = SHL32(EXTEND32(x[i+2]), SIG_SHIFT);
  102. sum[3] = SHL32(EXTEND32(x[i+3]), SIG_SHIFT);
  103. xcorr_kernel(rnum, x+i-ord, sum, ord, arch);
  104. y[i ] = ROUND16(sum[0], SIG_SHIFT);
  105. y[i+1] = ROUND16(sum[1], SIG_SHIFT);
  106. y[i+2] = ROUND16(sum[2], SIG_SHIFT);
  107. y[i+3] = ROUND16(sum[3], SIG_SHIFT);
  108. }
  109. for (;i<N;i++)
  110. {
  111. opus_val32 sum = SHL32(EXTEND32(x[i]), SIG_SHIFT);
  112. for (j=0;j<ord;j++)
  113. sum = MAC16_16(sum,rnum[j],x[i+j-ord]);
  114. y[i] = ROUND16(sum, SIG_SHIFT);
  115. }
  116. RESTORE_STACK;
  117. }
  118. void celt_iir(const opus_val32 *_x,
  119. const opus_val16 *den,
  120. opus_val32 *_y,
  121. int N,
  122. int ord,
  123. opus_val16 *mem,
  124. int arch)
  125. {
  126. #ifdef SMALL_FOOTPRINT
  127. int i,j;
  128. (void)arch;
  129. for (i=0;i<N;i++)
  130. {
  131. opus_val32 sum = _x[i];
  132. for (j=0;j<ord;j++)
  133. {
  134. sum -= MULT16_16(den[j],mem[j]);
  135. }
  136. for (j=ord-1;j>=1;j--)
  137. {
  138. mem[j]=mem[j-1];
  139. }
  140. mem[0] = SROUND16(sum, SIG_SHIFT);
  141. _y[i] = sum;
  142. }
  143. #else
  144. int i,j;
  145. VARDECL(opus_val16, rden);
  146. VARDECL(opus_val16, y);
  147. SAVE_STACK;
  148. celt_assert((ord&3)==0);
  149. ALLOC(rden, ord, opus_val16);
  150. ALLOC(y, N+ord, opus_val16);
  151. for(i=0;i<ord;i++)
  152. rden[i] = den[ord-i-1];
  153. for(i=0;i<ord;i++)
  154. y[i] = -mem[ord-i-1];
  155. for(;i<N+ord;i++)
  156. y[i]=0;
  157. for (i=0;i<N-3;i+=4)
  158. {
  159. /* Unroll by 4 as if it were an FIR filter */
  160. opus_val32 sum[4];
  161. sum[0]=_x[i];
  162. sum[1]=_x[i+1];
  163. sum[2]=_x[i+2];
  164. sum[3]=_x[i+3];
  165. xcorr_kernel(rden, y+i, sum, ord, arch);
  166. /* Patch up the result to compensate for the fact that this is an IIR */
  167. y[i+ord ] = -SROUND16(sum[0],SIG_SHIFT);
  168. _y[i ] = sum[0];
  169. sum[1] = MAC16_16(sum[1], y[i+ord ], den[0]);
  170. y[i+ord+1] = -SROUND16(sum[1],SIG_SHIFT);
  171. _y[i+1] = sum[1];
  172. sum[2] = MAC16_16(sum[2], y[i+ord+1], den[0]);
  173. sum[2] = MAC16_16(sum[2], y[i+ord ], den[1]);
  174. y[i+ord+2] = -SROUND16(sum[2],SIG_SHIFT);
  175. _y[i+2] = sum[2];
  176. sum[3] = MAC16_16(sum[3], y[i+ord+2], den[0]);
  177. sum[3] = MAC16_16(sum[3], y[i+ord+1], den[1]);
  178. sum[3] = MAC16_16(sum[3], y[i+ord ], den[2]);
  179. y[i+ord+3] = -SROUND16(sum[3],SIG_SHIFT);
  180. _y[i+3] = sum[3];
  181. }
  182. for (;i<N;i++)
  183. {
  184. opus_val32 sum = _x[i];
  185. for (j=0;j<ord;j++)
  186. sum -= MULT16_16(rden[j],y[i+j]);
  187. y[i+ord] = SROUND16(sum,SIG_SHIFT);
  188. _y[i] = sum;
  189. }
  190. for(i=0;i<ord;i++)
  191. mem[i] = _y[N-i-1];
  192. RESTORE_STACK;
  193. #endif
  194. }
  195. int _celt_autocorr(
  196. const opus_val16 *x, /* in: [0...n-1] samples x */
  197. opus_val32 *ac, /* out: [0...lag-1] ac values */
  198. const opus_val16 *window,
  199. int overlap,
  200. int lag,
  201. int n,
  202. int arch
  203. )
  204. {
  205. opus_val32 d;
  206. int i, k;
  207. int fastN=n-lag;
  208. int shift;
  209. const opus_val16 *xptr;
  210. VARDECL(opus_val16, xx);
  211. SAVE_STACK;
  212. ALLOC(xx, n, opus_val16);
  213. celt_assert(n>0);
  214. celt_assert(overlap>=0);
  215. if (overlap == 0)
  216. {
  217. xptr = x;
  218. } else {
  219. for (i=0;i<n;i++)
  220. xx[i] = x[i];
  221. for (i=0;i<overlap;i++)
  222. {
  223. xx[i] = MULT16_16_Q15(x[i],window[i]);
  224. xx[n-i-1] = MULT16_16_Q15(x[n-i-1],window[i]);
  225. }
  226. xptr = xx;
  227. }
  228. shift=0;
  229. #ifdef FIXED_POINT
  230. {
  231. opus_val32 ac0;
  232. ac0 = 1+(n<<7);
  233. if (n&1) ac0 += SHR32(MULT16_16(xptr[0],xptr[0]),9);
  234. for(i=(n&1);i<n;i+=2)
  235. {
  236. ac0 += SHR32(MULT16_16(xptr[i],xptr[i]),9);
  237. ac0 += SHR32(MULT16_16(xptr[i+1],xptr[i+1]),9);
  238. }
  239. shift = celt_ilog2(ac0)-30+10;
  240. shift = (shift)/2;
  241. if (shift>0)
  242. {
  243. for(i=0;i<n;i++)
  244. xx[i] = PSHR32(xptr[i], shift);
  245. xptr = xx;
  246. } else
  247. shift = 0;
  248. }
  249. #endif
  250. celt_pitch_xcorr(xptr, xptr, ac, fastN, lag+1, arch);
  251. for (k=0;k<=lag;k++)
  252. {
  253. for (i = k+fastN, d = 0; i < n; i++)
  254. d = MAC16_16(d, xptr[i], xptr[i-k]);
  255. ac[k] += d;
  256. }
  257. #ifdef FIXED_POINT
  258. shift = 2*shift;
  259. if (shift<=0)
  260. ac[0] += SHL32((opus_int32)1, -shift);
  261. if (ac[0] < 268435456)
  262. {
  263. int shift2 = 29 - EC_ILOG(ac[0]);
  264. for (i=0;i<=lag;i++)
  265. ac[i] = SHL32(ac[i], shift2);
  266. shift -= shift2;
  267. } else if (ac[0] >= 536870912)
  268. {
  269. int shift2=1;
  270. if (ac[0] >= 1073741824)
  271. shift2++;
  272. for (i=0;i<=lag;i++)
  273. ac[i] = SHR32(ac[i], shift2);
  274. shift += shift2;
  275. }
  276. #endif
  277. RESTORE_STACK;
  278. return shift;
  279. }