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.

267 lines
6.7KB

  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->exptab1 = NULL;
  51. /* compute constant table for HAVE_SSE version */
  52. #if (defined(HAVE_MMX) && (defined(HAVE_BUILTIN_VECTOR) || defined(HAVE_MM3DNOW))) || defined(HAVE_ALTIVEC)
  53. {
  54. int has_vectors = 0;
  55. #if defined(HAVE_MMX)
  56. has_vectors = mm_support() & (MM_3DNOW | MM_3DNOWEXT | MM_SSE | MM_SSE2);
  57. #endif
  58. #if defined(HAVE_ALTIVEC) && !defined(ALTIVEC_USE_REFERENCE_C_CODE)
  59. has_vectors = mm_support() & MM_ALTIVEC;
  60. #endif
  61. if (has_vectors) {
  62. int np, nblocks, np2, l;
  63. FFTComplex *q;
  64. np = 1 << nbits;
  65. nblocks = np >> 3;
  66. np2 = np >> 1;
  67. s->exptab1 = av_malloc(np * 2 * sizeof(FFTComplex));
  68. if (!s->exptab1)
  69. goto fail;
  70. q = s->exptab1;
  71. do {
  72. for(l = 0; l < np2; l += 2 * nblocks) {
  73. *q++ = s->exptab[l];
  74. *q++ = s->exptab[l + nblocks];
  75. q->re = -s->exptab[l].im;
  76. q->im = s->exptab[l].re;
  77. q++;
  78. q->re = -s->exptab[l + nblocks].im;
  79. q->im = s->exptab[l + nblocks].re;
  80. q++;
  81. }
  82. nblocks = nblocks >> 1;
  83. } while (nblocks != 0);
  84. av_freep(&s->exptab);
  85. #if defined(HAVE_MMX)
  86. #ifdef HAVE_MM3DNOW
  87. if (has_vectors & MM_3DNOWEXT)
  88. /* 3DNowEx for Athlon(XP) */
  89. s->fft_calc = ff_fft_calc_3dn2;
  90. else if (has_vectors & MM_3DNOW)
  91. /* 3DNow! for K6-2/3 */
  92. s->fft_calc = ff_fft_calc_3dn;
  93. #endif
  94. #ifdef HAVE_BUILTIN_VECTOR
  95. if (has_vectors & MM_SSE2)
  96. /* SSE for P4/K8 */
  97. s->fft_calc = ff_fft_calc_sse;
  98. else if ((has_vectors & MM_SSE) &&
  99. s->fft_calc == ff_fft_calc_c)
  100. /* SSE for P3 */
  101. s->fft_calc = ff_fft_calc_sse;
  102. #endif
  103. #else /* HAVE_MMX */
  104. s->fft_calc = ff_fft_calc_altivec;
  105. #endif
  106. }
  107. }
  108. #endif
  109. /* compute bit reverse table */
  110. for(i=0;i<n;i++) {
  111. m=0;
  112. for(j=0;j<nbits;j++) {
  113. m |= ((i >> j) & 1) << (nbits-j-1);
  114. }
  115. s->revtab[i]=m;
  116. }
  117. return 0;
  118. fail:
  119. av_freep(&s->revtab);
  120. av_freep(&s->exptab);
  121. av_freep(&s->exptab1);
  122. return -1;
  123. }
  124. /* butter fly op */
  125. #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
  126. {\
  127. FFTSample ax, ay, bx, by;\
  128. bx=pre1;\
  129. by=pim1;\
  130. ax=qre1;\
  131. ay=qim1;\
  132. pre = (bx + ax);\
  133. pim = (by + ay);\
  134. qre = (bx - ax);\
  135. qim = (by - ay);\
  136. }
  137. #define MUL16(a,b) ((a) * (b))
  138. #define CMUL(pre, pim, are, aim, bre, bim) \
  139. {\
  140. pre = (MUL16(are, bre) - MUL16(aim, bim));\
  141. pim = (MUL16(are, bim) + MUL16(bre, aim));\
  142. }
  143. /**
  144. * Do a complex FFT with the parameters defined in ff_fft_init(). The
  145. * input data must be permuted before with s->revtab table. No
  146. * 1.0/sqrt(n) normalization is done.
  147. */
  148. void ff_fft_calc_c(FFTContext *s, FFTComplex *z)
  149. {
  150. int ln = s->nbits;
  151. int j, np, np2;
  152. int nblocks, nloops;
  153. register FFTComplex *p, *q;
  154. FFTComplex *exptab = s->exptab;
  155. int l;
  156. FFTSample tmp_re, tmp_im;
  157. np = 1 << ln;
  158. /* pass 0 */
  159. p=&z[0];
  160. j=(np >> 1);
  161. do {
  162. BF(p[0].re, p[0].im, p[1].re, p[1].im,
  163. p[0].re, p[0].im, p[1].re, p[1].im);
  164. p+=2;
  165. } while (--j != 0);
  166. /* pass 1 */
  167. p=&z[0];
  168. j=np >> 2;
  169. if (s->inverse) {
  170. do {
  171. BF(p[0].re, p[0].im, p[2].re, p[2].im,
  172. p[0].re, p[0].im, p[2].re, p[2].im);
  173. BF(p[1].re, p[1].im, p[3].re, p[3].im,
  174. p[1].re, p[1].im, -p[3].im, p[3].re);
  175. p+=4;
  176. } while (--j != 0);
  177. } else {
  178. do {
  179. BF(p[0].re, p[0].im, p[2].re, p[2].im,
  180. p[0].re, p[0].im, p[2].re, p[2].im);
  181. BF(p[1].re, p[1].im, p[3].re, p[3].im,
  182. p[1].re, p[1].im, p[3].im, -p[3].re);
  183. p+=4;
  184. } while (--j != 0);
  185. }
  186. /* pass 2 .. ln-1 */
  187. nblocks = np >> 3;
  188. nloops = 1 << 2;
  189. np2 = np >> 1;
  190. do {
  191. p = z;
  192. q = z + nloops;
  193. for (j = 0; j < nblocks; ++j) {
  194. BF(p->re, p->im, q->re, q->im,
  195. p->re, p->im, q->re, q->im);
  196. p++;
  197. q++;
  198. for(l = nblocks; l < np2; l += nblocks) {
  199. CMUL(tmp_re, tmp_im, exptab[l].re, exptab[l].im, q->re, q->im);
  200. BF(p->re, p->im, q->re, q->im,
  201. p->re, p->im, tmp_re, tmp_im);
  202. p++;
  203. q++;
  204. }
  205. p += nloops;
  206. q += nloops;
  207. }
  208. nblocks = nblocks >> 1;
  209. nloops = nloops << 1;
  210. } while (nblocks != 0);
  211. }
  212. /**
  213. * Do the permutation needed BEFORE calling ff_fft_calc()
  214. */
  215. void ff_fft_permute(FFTContext *s, FFTComplex *z)
  216. {
  217. int j, k, np;
  218. FFTComplex tmp;
  219. const uint16_t *revtab = s->revtab;
  220. /* reverse */
  221. np = 1 << s->nbits;
  222. for(j=0;j<np;j++) {
  223. k = revtab[j];
  224. if (k < j) {
  225. tmp = z[k];
  226. z[k] = z[j];
  227. z[j] = tmp;
  228. }
  229. }
  230. }
  231. void ff_fft_end(FFTContext *s)
  232. {
  233. av_freep(&s->revtab);
  234. av_freep(&s->exptab);
  235. av_freep(&s->exptab1);
  236. }