You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
6.9KB

  1. /*
  2. * FFT/IFFT transforms
  3. * Copyright (c) 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file fft.c
  21. * FFT/IFFT transforms.
  22. */
  23. #include "dsputil.h"
  24. /**
  25. * The size of the FFT is 2^nbits. If inverse is TRUE, inverse FFT is
  26. * done
  27. */
  28. int ff_fft_init(FFTContext *s, int nbits, int inverse)
  29. {
  30. int i, j, m, n;
  31. float alpha, c1, s1, s2;
  32. s->nbits = nbits;
  33. n = 1 << nbits;
  34. s->exptab = av_malloc((n / 2) * sizeof(FFTComplex));
  35. if (!s->exptab)
  36. goto fail;
  37. s->revtab = av_malloc(n * sizeof(uint16_t));
  38. if (!s->revtab)
  39. goto fail;
  40. s->inverse = inverse;
  41. s2 = inverse ? 1.0 : -1.0;
  42. for(i=0;i<(n/2);i++) {
  43. alpha = 2 * M_PI * (float)i / (float)n;
  44. c1 = cos(alpha);
  45. s1 = sin(alpha) * s2;
  46. s->exptab[i].re = c1;
  47. s->exptab[i].im = s1;
  48. }
  49. s->fft_calc = ff_fft_calc_c;
  50. s->imdct_calc = ff_imdct_calc;
  51. s->exptab1 = NULL;
  52. /* compute constant table for HAVE_SSE version */
  53. #if (defined(HAVE_MMX) && (defined(HAVE_BUILTIN_VECTOR) || defined(HAVE_MM3DNOW))) || defined(HAVE_ALTIVEC)
  54. {
  55. int has_vectors = 0;
  56. #if defined(HAVE_MMX)
  57. has_vectors = mm_support() & (MM_3DNOW | MM_3DNOWEXT | MM_SSE | MM_SSE2);
  58. #endif
  59. #if defined(HAVE_ALTIVEC) && !defined(ALTIVEC_USE_REFERENCE_C_CODE)
  60. has_vectors = mm_support() & MM_ALTIVEC;
  61. #endif
  62. if (has_vectors) {
  63. int np, nblocks, np2, l;
  64. FFTComplex *q;
  65. np = 1 << nbits;
  66. nblocks = np >> 3;
  67. np2 = np >> 1;
  68. s->exptab1 = av_malloc(np * 2 * sizeof(FFTComplex));
  69. if (!s->exptab1)
  70. goto fail;
  71. q = s->exptab1;
  72. do {
  73. for(l = 0; l < np2; l += 2 * nblocks) {
  74. *q++ = s->exptab[l];
  75. *q++ = s->exptab[l + nblocks];
  76. q->re = -s->exptab[l].im;
  77. q->im = s->exptab[l].re;
  78. q++;
  79. q->re = -s->exptab[l + nblocks].im;
  80. q->im = s->exptab[l + nblocks].re;
  81. q++;
  82. }
  83. nblocks = nblocks >> 1;
  84. } while (nblocks != 0);
  85. av_freep(&s->exptab);
  86. #if defined(HAVE_MMX)
  87. if (has_vectors & MM_3DNOWEXT)
  88. s->imdct_calc = ff_imdct_calc_3dn2;
  89. #ifdef HAVE_MM3DNOW
  90. if (has_vectors & MM_3DNOWEXT)
  91. /* 3DNowEx for Athlon(XP) */
  92. s->fft_calc = ff_fft_calc_3dn2;
  93. else if (has_vectors & MM_3DNOW)
  94. /* 3DNow! for K6-2/3 */
  95. s->fft_calc = ff_fft_calc_3dn;
  96. #endif
  97. #ifdef HAVE_BUILTIN_VECTOR
  98. if (has_vectors & MM_SSE2)
  99. /* SSE for P4/K8 */
  100. s->fft_calc = ff_fft_calc_sse;
  101. else if ((has_vectors & MM_SSE) &&
  102. s->fft_calc == ff_fft_calc_c)
  103. /* SSE for P3 */
  104. s->fft_calc = ff_fft_calc_sse;
  105. #endif
  106. #else /* HAVE_MMX */
  107. s->fft_calc = ff_fft_calc_altivec;
  108. #endif
  109. }
  110. }
  111. #endif
  112. /* compute bit reverse table */
  113. for(i=0;i<n;i++) {
  114. m=0;
  115. for(j=0;j<nbits;j++) {
  116. m |= ((i >> j) & 1) << (nbits-j-1);
  117. }
  118. s->revtab[i]=m;
  119. }
  120. return 0;
  121. fail:
  122. av_freep(&s->revtab);
  123. av_freep(&s->exptab);
  124. av_freep(&s->exptab1);
  125. return -1;
  126. }
  127. /* butter fly op */
  128. #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
  129. {\
  130. FFTSample ax, ay, bx, by;\
  131. bx=pre1;\
  132. by=pim1;\
  133. ax=qre1;\
  134. ay=qim1;\
  135. pre = (bx + ax);\
  136. pim = (by + ay);\
  137. qre = (bx - ax);\
  138. qim = (by - ay);\
  139. }
  140. #define MUL16(a,b) ((a) * (b))
  141. #define CMUL(pre, pim, are, aim, bre, bim) \
  142. {\
  143. pre = (MUL16(are, bre) - MUL16(aim, bim));\
  144. pim = (MUL16(are, bim) + MUL16(bre, aim));\
  145. }
  146. /**
  147. * Do a complex FFT with the parameters defined in ff_fft_init(). The
  148. * input data must be permuted before with s->revtab table. No
  149. * 1.0/sqrt(n) normalization is done.
  150. */
  151. void ff_fft_calc_c(FFTContext *s, FFTComplex *z)
  152. {
  153. int ln = s->nbits;
  154. int j, np, np2;
  155. int nblocks, nloops;
  156. register FFTComplex *p, *q;
  157. FFTComplex *exptab = s->exptab;
  158. int l;
  159. FFTSample tmp_re, tmp_im;
  160. np = 1 << ln;
  161. /* pass 0 */
  162. p=&z[0];
  163. j=(np >> 1);
  164. do {
  165. BF(p[0].re, p[0].im, p[1].re, p[1].im,
  166. p[0].re, p[0].im, p[1].re, p[1].im);
  167. p+=2;
  168. } while (--j != 0);
  169. /* pass 1 */
  170. p=&z[0];
  171. j=np >> 2;
  172. if (s->inverse) {
  173. do {
  174. BF(p[0].re, p[0].im, p[2].re, p[2].im,
  175. p[0].re, p[0].im, p[2].re, p[2].im);
  176. BF(p[1].re, p[1].im, p[3].re, p[3].im,
  177. p[1].re, p[1].im, -p[3].im, p[3].re);
  178. p+=4;
  179. } while (--j != 0);
  180. } else {
  181. do {
  182. BF(p[0].re, p[0].im, p[2].re, p[2].im,
  183. p[0].re, p[0].im, p[2].re, p[2].im);
  184. BF(p[1].re, p[1].im, p[3].re, p[3].im,
  185. p[1].re, p[1].im, p[3].im, -p[3].re);
  186. p+=4;
  187. } while (--j != 0);
  188. }
  189. /* pass 2 .. ln-1 */
  190. nblocks = np >> 3;
  191. nloops = 1 << 2;
  192. np2 = np >> 1;
  193. do {
  194. p = z;
  195. q = z + nloops;
  196. for (j = 0; j < nblocks; ++j) {
  197. BF(p->re, p->im, q->re, q->im,
  198. p->re, p->im, q->re, q->im);
  199. p++;
  200. q++;
  201. for(l = nblocks; l < np2; l += nblocks) {
  202. CMUL(tmp_re, tmp_im, exptab[l].re, exptab[l].im, q->re, q->im);
  203. BF(p->re, p->im, q->re, q->im,
  204. p->re, p->im, tmp_re, tmp_im);
  205. p++;
  206. q++;
  207. }
  208. p += nloops;
  209. q += nloops;
  210. }
  211. nblocks = nblocks >> 1;
  212. nloops = nloops << 1;
  213. } while (nblocks != 0);
  214. }
  215. /**
  216. * Do the permutation needed BEFORE calling ff_fft_calc()
  217. */
  218. void ff_fft_permute(FFTContext *s, FFTComplex *z)
  219. {
  220. int j, k, np;
  221. FFTComplex tmp;
  222. const uint16_t *revtab = s->revtab;
  223. /* reverse */
  224. np = 1 << s->nbits;
  225. for(j=0;j<np;j++) {
  226. k = revtab[j];
  227. if (k < j) {
  228. tmp = z[k];
  229. z[k] = z[j];
  230. z[j] = tmp;
  231. }
  232. }
  233. }
  234. void ff_fft_end(FFTContext *s)
  235. {
  236. av_freep(&s->revtab);
  237. av_freep(&s->exptab);
  238. av_freep(&s->exptab1);
  239. }