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.

271 lines
6.8KB

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