mdct.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* Copyright (c) 2007-2008 CSIRO
  2. Copyright (c) 2007-2008 Xiph.Org Foundation
  3. Written by Jean-Marc Valin */
  4. /*
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. - Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. - Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  17. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  21. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /* This is a simple MDCT implementation that uses a N/4 complex FFT
  26. to do most of the work. It should be relatively straightforward to
  27. plug in pretty much and FFT here.
  28. This replaces the Vorbis FFT (and uses the exact same API), which
  29. was a bit too messy and that was ending up duplicating code
  30. (might as well use the same FFT everywhere).
  31. The algorithm is similar to (and inspired from) Fabrice Bellard's
  32. MDCT implementation in FFMPEG, but has differences in signs, ordering
  33. and scaling in many places.
  34. */
  35. #ifndef SKIP_CONFIG_H
  36. #ifdef HAVE_CONFIG_H
  37. #include "config.h"
  38. #endif
  39. #endif
  40. #include "mdct.h"
  41. #include "kiss_fft.h"
  42. #include "_kiss_fft_guts.h"
  43. #include <math.h>
  44. #include "os_support.h"
  45. #include "mathops.h"
  46. #include "stack_alloc.h"
  47. #if defined(MIPSr1_ASM)
  48. #include "mips/mdct_mipsr1.h"
  49. #endif
  50. #ifdef CUSTOM_MODES
  51. int clt_mdct_init(mdct_lookup *l,int N, int maxshift, int arch)
  52. {
  53. int i;
  54. kiss_twiddle_scalar *trig;
  55. int shift;
  56. int N2=N>>1;
  57. l->n = N;
  58. l->maxshift = maxshift;
  59. for (i=0;i<=maxshift;i++)
  60. {
  61. if (i==0)
  62. l->kfft[i] = opus_fft_alloc(N>>2>>i, 0, 0, arch);
  63. else
  64. l->kfft[i] = opus_fft_alloc_twiddles(N>>2>>i, 0, 0, l->kfft[0], arch);
  65. #ifndef ENABLE_TI_DSPLIB55
  66. if (l->kfft[i]==NULL)
  67. return 0;
  68. #endif
  69. }
  70. l->trig = trig = (kiss_twiddle_scalar*)opus_alloc((N-(N2>>maxshift))*sizeof(kiss_twiddle_scalar));
  71. if (l->trig==NULL)
  72. return 0;
  73. for (shift=0;shift<=maxshift;shift++)
  74. {
  75. /* We have enough points that sine isn't necessary */
  76. #if defined(FIXED_POINT)
  77. #if 1
  78. for (i=0;i<N2;i++)
  79. trig[i] = TRIG_UPSCALE*celt_cos_norm(DIV32(ADD32(SHL32(EXTEND32(i),17),N2+16384),N));
  80. #else
  81. for (i=0;i<N2;i++)
  82. trig[i] = (kiss_twiddle_scalar)MAX32(-32767,MIN32(32767,floor(.5+32768*cos(2*M_PI*(i+.125)/N))));
  83. #endif
  84. #else
  85. for (i=0;i<N2;i++)
  86. trig[i] = (kiss_twiddle_scalar)cos(2*PI*(i+.125)/N);
  87. #endif
  88. trig += N2;
  89. N2 >>= 1;
  90. N >>= 1;
  91. }
  92. return 1;
  93. }
  94. void clt_mdct_clear(mdct_lookup *l, int arch)
  95. {
  96. int i;
  97. for (i=0;i<=l->maxshift;i++)
  98. opus_fft_free(l->kfft[i], arch);
  99. opus_free((kiss_twiddle_scalar*)l->trig);
  100. }
  101. #endif /* CUSTOM_MODES */
  102. /* Forward MDCT trashes the input array */
  103. #ifndef OVERRIDE_clt_mdct_forward
  104. void clt_mdct_forward_c(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar * OPUS_RESTRICT out,
  105. const opus_val16 *window, int overlap, int shift, int stride, int arch)
  106. {
  107. int i;
  108. int N, N2, N4;
  109. VARDECL(kiss_fft_scalar, f);
  110. VARDECL(kiss_fft_cpx, f2);
  111. const kiss_fft_state *st = l->kfft[shift];
  112. const kiss_twiddle_scalar *trig;
  113. opus_val16 scale;
  114. #ifdef FIXED_POINT
  115. /* Allows us to scale with MULT16_32_Q16(), which is faster than
  116. MULT16_32_Q15() on ARM. */
  117. int scale_shift = st->scale_shift-1;
  118. #endif
  119. SAVE_STACK;
  120. (void)arch;
  121. scale = st->scale;
  122. N = l->n;
  123. trig = l->trig;
  124. for (i=0;i<shift;i++)
  125. {
  126. N >>= 1;
  127. trig += N;
  128. }
  129. N2 = N>>1;
  130. N4 = N>>2;
  131. ALLOC(f, N2, kiss_fft_scalar);
  132. ALLOC(f2, N4, kiss_fft_cpx);
  133. /* Consider the input to be composed of four blocks: [a, b, c, d] */
  134. /* Window, shuffle, fold */
  135. {
  136. /* Temp pointers to make it really clear to the compiler what we're doing */
  137. const kiss_fft_scalar * OPUS_RESTRICT xp1 = in+(overlap>>1);
  138. const kiss_fft_scalar * OPUS_RESTRICT xp2 = in+N2-1+(overlap>>1);
  139. kiss_fft_scalar * OPUS_RESTRICT yp = f;
  140. const opus_val16 * OPUS_RESTRICT wp1 = window+(overlap>>1);
  141. const opus_val16 * OPUS_RESTRICT wp2 = window+(overlap>>1)-1;
  142. for(i=0;i<((overlap+3)>>2);i++)
  143. {
  144. /* Real part arranged as -d-cR, Imag part arranged as -b+aR*/
  145. *yp++ = MULT16_32_Q15(*wp2, xp1[N2]) + MULT16_32_Q15(*wp1,*xp2);
  146. *yp++ = MULT16_32_Q15(*wp1, *xp1) - MULT16_32_Q15(*wp2, xp2[-N2]);
  147. xp1+=2;
  148. xp2-=2;
  149. wp1+=2;
  150. wp2-=2;
  151. }
  152. wp1 = window;
  153. wp2 = window+overlap-1;
  154. for(;i<N4-((overlap+3)>>2);i++)
  155. {
  156. /* Real part arranged as a-bR, Imag part arranged as -c-dR */
  157. *yp++ = *xp2;
  158. *yp++ = *xp1;
  159. xp1+=2;
  160. xp2-=2;
  161. }
  162. for(;i<N4;i++)
  163. {
  164. /* Real part arranged as a-bR, Imag part arranged as -c-dR */
  165. *yp++ = -MULT16_32_Q15(*wp1, xp1[-N2]) + MULT16_32_Q15(*wp2, *xp2);
  166. *yp++ = MULT16_32_Q15(*wp2, *xp1) + MULT16_32_Q15(*wp1, xp2[N2]);
  167. xp1+=2;
  168. xp2-=2;
  169. wp1+=2;
  170. wp2-=2;
  171. }
  172. }
  173. /* Pre-rotation */
  174. {
  175. kiss_fft_scalar * OPUS_RESTRICT yp = f;
  176. const kiss_twiddle_scalar *t = &trig[0];
  177. for(i=0;i<N4;i++)
  178. {
  179. kiss_fft_cpx yc;
  180. kiss_twiddle_scalar t0, t1;
  181. kiss_fft_scalar re, im, yr, yi;
  182. t0 = t[i];
  183. t1 = t[N4+i];
  184. re = *yp++;
  185. im = *yp++;
  186. yr = S_MUL(re,t0) - S_MUL(im,t1);
  187. yi = S_MUL(im,t0) + S_MUL(re,t1);
  188. yc.r = yr;
  189. yc.i = yi;
  190. yc.r = PSHR32(MULT16_32_Q16(scale, yc.r), scale_shift);
  191. yc.i = PSHR32(MULT16_32_Q16(scale, yc.i), scale_shift);
  192. f2[st->bitrev[i]] = yc;
  193. }
  194. }
  195. /* N/4 complex FFT, does not downscale anymore */
  196. opus_fft_impl(st, f2);
  197. /* Post-rotate */
  198. {
  199. /* Temp pointers to make it really clear to the compiler what we're doing */
  200. const kiss_fft_cpx * OPUS_RESTRICT fp = f2;
  201. kiss_fft_scalar * OPUS_RESTRICT yp1 = out;
  202. kiss_fft_scalar * OPUS_RESTRICT yp2 = out+stride*(N2-1);
  203. const kiss_twiddle_scalar *t = &trig[0];
  204. /* Temp pointers to make it really clear to the compiler what we're doing */
  205. for(i=0;i<N4;i++)
  206. {
  207. kiss_fft_scalar yr, yi;
  208. yr = S_MUL(fp->i,t[N4+i]) - S_MUL(fp->r,t[i]);
  209. yi = S_MUL(fp->r,t[N4+i]) + S_MUL(fp->i,t[i]);
  210. *yp1 = yr;
  211. *yp2 = yi;
  212. fp++;
  213. yp1 += 2*stride;
  214. yp2 -= 2*stride;
  215. }
  216. }
  217. RESTORE_STACK;
  218. }
  219. #endif /* OVERRIDE_clt_mdct_forward */
  220. #ifndef OVERRIDE_clt_mdct_backward
  221. void clt_mdct_backward_c(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar * OPUS_RESTRICT out,
  222. const opus_val16 * OPUS_RESTRICT window, int overlap, int shift, int stride, int arch)
  223. {
  224. int i;
  225. int N, N2, N4;
  226. const kiss_twiddle_scalar *trig;
  227. (void) arch;
  228. N = l->n;
  229. trig = l->trig;
  230. for (i=0;i<shift;i++)
  231. {
  232. N >>= 1;
  233. trig += N;
  234. }
  235. N2 = N>>1;
  236. N4 = N>>2;
  237. /* Pre-rotate */
  238. {
  239. /* Temp pointers to make it really clear to the compiler what we're doing */
  240. const kiss_fft_scalar * OPUS_RESTRICT xp1 = in;
  241. const kiss_fft_scalar * OPUS_RESTRICT xp2 = in+stride*(N2-1);
  242. kiss_fft_scalar * OPUS_RESTRICT yp = out+(overlap>>1);
  243. const kiss_twiddle_scalar * OPUS_RESTRICT t = &trig[0];
  244. const opus_int16 * OPUS_RESTRICT bitrev = l->kfft[shift]->bitrev;
  245. for(i=0;i<N4;i++)
  246. {
  247. int rev;
  248. kiss_fft_scalar yr, yi;
  249. rev = *bitrev++;
  250. yr = S_MUL(*xp2, t[i]) + S_MUL(*xp1, t[N4+i]);
  251. yi = S_MUL(*xp1, t[i]) - S_MUL(*xp2, t[N4+i]);
  252. /* We swap real and imag because we use an FFT instead of an IFFT. */
  253. yp[2*rev+1] = yr;
  254. yp[2*rev] = yi;
  255. /* Storing the pre-rotation directly in the bitrev order. */
  256. xp1+=2*stride;
  257. xp2-=2*stride;
  258. }
  259. }
  260. opus_fft_impl(l->kfft[shift], (kiss_fft_cpx*)(out+(overlap>>1)));
  261. /* Post-rotate and de-shuffle from both ends of the buffer at once to make
  262. it in-place. */
  263. {
  264. kiss_fft_scalar * yp0 = out+(overlap>>1);
  265. kiss_fft_scalar * yp1 = out+(overlap>>1)+N2-2;
  266. const kiss_twiddle_scalar *t = &trig[0];
  267. /* Loop to (N4+1)>>1 to handle odd N4. When N4 is odd, the
  268. middle pair will be computed twice. */
  269. for(i=0;i<(N4+1)>>1;i++)
  270. {
  271. kiss_fft_scalar re, im, yr, yi;
  272. kiss_twiddle_scalar t0, t1;
  273. /* We swap real and imag because we're using an FFT instead of an IFFT. */
  274. re = yp0[1];
  275. im = yp0[0];
  276. t0 = t[i];
  277. t1 = t[N4+i];
  278. /* We'd scale up by 2 here, but instead it's done when mixing the windows */
  279. yr = S_MUL(re,t0) + S_MUL(im,t1);
  280. yi = S_MUL(re,t1) - S_MUL(im,t0);
  281. /* We swap real and imag because we're using an FFT instead of an IFFT. */
  282. re = yp1[1];
  283. im = yp1[0];
  284. yp0[0] = yr;
  285. yp1[1] = yi;
  286. t0 = t[(N4-i-1)];
  287. t1 = t[(N2-i-1)];
  288. /* We'd scale up by 2 here, but instead it's done when mixing the windows */
  289. yr = S_MUL(re,t0) + S_MUL(im,t1);
  290. yi = S_MUL(re,t1) - S_MUL(im,t0);
  291. yp1[0] = yr;
  292. yp0[1] = yi;
  293. yp0 += 2;
  294. yp1 -= 2;
  295. }
  296. }
  297. /* Mirror on both sides for TDAC */
  298. {
  299. kiss_fft_scalar * OPUS_RESTRICT xp1 = out+overlap-1;
  300. kiss_fft_scalar * OPUS_RESTRICT yp1 = out;
  301. const opus_val16 * OPUS_RESTRICT wp1 = window;
  302. const opus_val16 * OPUS_RESTRICT wp2 = window+overlap-1;
  303. for(i = 0; i < overlap/2; i++)
  304. {
  305. kiss_fft_scalar x1, x2;
  306. x1 = *xp1;
  307. x2 = *yp1;
  308. *yp1++ = MULT16_32_Q15(*wp2, x2) - MULT16_32_Q15(*wp1, x1);
  309. *xp1-- = MULT16_32_Q15(*wp1, x2) + MULT16_32_Q15(*wp2, x1);
  310. wp1++;
  311. wp2--;
  312. }
  313. }
  314. }
  315. #endif /* OVERRIDE_clt_mdct_backward */