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.

251 lines
6.1KB

  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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_ALTIVEC)
  53. {
  54. int has_vectors = 0;
  55. #if defined(HAVE_MMX)
  56. has_vectors = mm_support() & MM_SSE;
  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. s->fft_calc = ff_fft_calc_sse;
  87. #else
  88. s->fft_calc = ff_fft_calc_altivec;
  89. #endif
  90. }
  91. }
  92. #endif
  93. /* compute bit reverse table */
  94. for(i=0;i<n;i++) {
  95. m=0;
  96. for(j=0;j<nbits;j++) {
  97. m |= ((i >> j) & 1) << (nbits-j-1);
  98. }
  99. s->revtab[i]=m;
  100. }
  101. return 0;
  102. fail:
  103. av_freep(&s->revtab);
  104. av_freep(&s->exptab);
  105. av_freep(&s->exptab1);
  106. return -1;
  107. }
  108. /* butter fly op */
  109. #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
  110. {\
  111. FFTSample ax, ay, bx, by;\
  112. bx=pre1;\
  113. by=pim1;\
  114. ax=qre1;\
  115. ay=qim1;\
  116. pre = (bx + ax);\
  117. pim = (by + ay);\
  118. qre = (bx - ax);\
  119. qim = (by - ay);\
  120. }
  121. #define MUL16(a,b) ((a) * (b))
  122. #define CMUL(pre, pim, are, aim, bre, bim) \
  123. {\
  124. pre = (MUL16(are, bre) - MUL16(aim, bim));\
  125. pim = (MUL16(are, bim) + MUL16(bre, aim));\
  126. }
  127. /**
  128. * Do a complex FFT with the parameters defined in ff_fft_init(). The
  129. * input data must be permuted before with s->revtab table. No
  130. * 1.0/sqrt(n) normalization is done.
  131. */
  132. void ff_fft_calc_c(FFTContext *s, FFTComplex *z)
  133. {
  134. int ln = s->nbits;
  135. int j, np, np2;
  136. int nblocks, nloops;
  137. register FFTComplex *p, *q;
  138. FFTComplex *exptab = s->exptab;
  139. int l;
  140. FFTSample tmp_re, tmp_im;
  141. np = 1 << ln;
  142. /* pass 0 */
  143. p=&z[0];
  144. j=(np >> 1);
  145. do {
  146. BF(p[0].re, p[0].im, p[1].re, p[1].im,
  147. p[0].re, p[0].im, p[1].re, p[1].im);
  148. p+=2;
  149. } while (--j != 0);
  150. /* pass 1 */
  151. p=&z[0];
  152. j=np >> 2;
  153. if (s->inverse) {
  154. do {
  155. BF(p[0].re, p[0].im, p[2].re, p[2].im,
  156. p[0].re, p[0].im, p[2].re, p[2].im);
  157. BF(p[1].re, p[1].im, p[3].re, p[3].im,
  158. p[1].re, p[1].im, -p[3].im, p[3].re);
  159. p+=4;
  160. } while (--j != 0);
  161. } else {
  162. do {
  163. BF(p[0].re, p[0].im, p[2].re, p[2].im,
  164. p[0].re, p[0].im, p[2].re, p[2].im);
  165. BF(p[1].re, p[1].im, p[3].re, p[3].im,
  166. p[1].re, p[1].im, p[3].im, -p[3].re);
  167. p+=4;
  168. } while (--j != 0);
  169. }
  170. /* pass 2 .. ln-1 */
  171. nblocks = np >> 3;
  172. nloops = 1 << 2;
  173. np2 = np >> 1;
  174. do {
  175. p = z;
  176. q = z + nloops;
  177. for (j = 0; j < nblocks; ++j) {
  178. BF(p->re, p->im, q->re, q->im,
  179. p->re, p->im, q->re, q->im);
  180. p++;
  181. q++;
  182. for(l = nblocks; l < np2; l += nblocks) {
  183. CMUL(tmp_re, tmp_im, exptab[l].re, exptab[l].im, q->re, q->im);
  184. BF(p->re, p->im, q->re, q->im,
  185. p->re, p->im, tmp_re, tmp_im);
  186. p++;
  187. q++;
  188. }
  189. p += nloops;
  190. q += nloops;
  191. }
  192. nblocks = nblocks >> 1;
  193. nloops = nloops << 1;
  194. } while (nblocks != 0);
  195. }
  196. /**
  197. * Do the permutation needed BEFORE calling ff_fft_calc()
  198. */
  199. void ff_fft_permute(FFTContext *s, FFTComplex *z)
  200. {
  201. int j, k, np;
  202. FFTComplex tmp;
  203. const uint16_t *revtab = s->revtab;
  204. /* reverse */
  205. np = 1 << s->nbits;
  206. for(j=0;j<np;j++) {
  207. k = revtab[j];
  208. if (k < j) {
  209. tmp = z[k];
  210. z[k] = z[j];
  211. z[j] = tmp;
  212. }
  213. }
  214. }
  215. void ff_fft_end(FFTContext *s)
  216. {
  217. av_freep(&s->revtab);
  218. av_freep(&s->exptab);
  219. av_freep(&s->exptab1);
  220. }