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.

245 lines
6.0KB

  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. #include "dsputil.h"
  20. /**
  21. * The size of the FFT is 2^nbits. If inverse is TRUE, inverse FFT is
  22. * done
  23. */
  24. int fft_init(FFTContext *s, int nbits, int inverse)
  25. {
  26. int i, j, m, n;
  27. float alpha, c1, s1, s2;
  28. s->nbits = nbits;
  29. n = 1 << nbits;
  30. s->exptab = av_malloc((n / 2) * sizeof(FFTComplex));
  31. if (!s->exptab)
  32. goto fail;
  33. s->revtab = av_malloc(n * sizeof(uint16_t));
  34. if (!s->revtab)
  35. goto fail;
  36. s->inverse = inverse;
  37. s2 = inverse ? 1.0 : -1.0;
  38. for(i=0;i<(n/2);i++) {
  39. alpha = 2 * M_PI * (float)i / (float)n;
  40. c1 = cos(alpha);
  41. s1 = sin(alpha) * s2;
  42. s->exptab[i].re = c1;
  43. s->exptab[i].im = s1;
  44. }
  45. s->fft_calc = fft_calc_c;
  46. s->exptab1 = NULL;
  47. /* compute constant table for HAVE_SSE version */
  48. #if (defined(HAVE_MMX) && defined(HAVE_BUILTIN_VECTOR)) || defined(HAVE_ALTIVEC)
  49. {
  50. int has_vectors;
  51. #if defined(HAVE_MMX)
  52. has_vectors = mm_support() & MM_SSE;
  53. #endif
  54. #if defined(HAVE_ALTIVEC)
  55. has_vectors = mm_support() & MM_ALTIVEC;
  56. #endif
  57. if (has_vectors) {
  58. int np, nblocks, np2, l;
  59. FFTComplex *q;
  60. np = 1 << nbits;
  61. nblocks = np >> 3;
  62. np2 = np >> 1;
  63. s->exptab1 = av_malloc(np * 2 * sizeof(FFTComplex));
  64. if (!s->exptab1)
  65. goto fail;
  66. q = s->exptab1;
  67. do {
  68. for(l = 0; l < np2; l += 2 * nblocks) {
  69. *q++ = s->exptab[l];
  70. *q++ = s->exptab[l + nblocks];
  71. q->re = -s->exptab[l].im;
  72. q->im = s->exptab[l].re;
  73. q++;
  74. q->re = -s->exptab[l + nblocks].im;
  75. q->im = s->exptab[l + nblocks].re;
  76. q++;
  77. }
  78. nblocks = nblocks >> 1;
  79. } while (nblocks != 0);
  80. av_freep(&s->exptab);
  81. #if defined(HAVE_MMX)
  82. s->fft_calc = fft_calc_sse;
  83. #else
  84. s->fft_calc = fft_calc_altivec;
  85. #endif
  86. }
  87. }
  88. #endif
  89. /* compute bit reverse table */
  90. for(i=0;i<n;i++) {
  91. m=0;
  92. for(j=0;j<nbits;j++) {
  93. m |= ((i >> j) & 1) << (nbits-j-1);
  94. }
  95. s->revtab[i]=m;
  96. }
  97. return 0;
  98. fail:
  99. av_freep(&s->revtab);
  100. av_freep(&s->exptab);
  101. av_freep(&s->exptab1);
  102. return -1;
  103. }
  104. /* butter fly op */
  105. #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
  106. {\
  107. FFTSample ax, ay, bx, by;\
  108. bx=pre1;\
  109. by=pim1;\
  110. ax=qre1;\
  111. ay=qim1;\
  112. pre = (bx + ax);\
  113. pim = (by + ay);\
  114. qre = (bx - ax);\
  115. qim = (by - ay);\
  116. }
  117. #define MUL16(a,b) ((a) * (b))
  118. #define CMUL(pre, pim, are, aim, bre, bim) \
  119. {\
  120. pre = (MUL16(are, bre) - MUL16(aim, bim));\
  121. pim = (MUL16(are, bim) + MUL16(bre, aim));\
  122. }
  123. /**
  124. * Do a complex FFT with the parameters defined in fft_init(). The
  125. * input data must be permuted before with s->revtab table. No
  126. * 1.0/sqrt(n) normalization is done.
  127. */
  128. void fft_calc_c(FFTContext *s, FFTComplex *z)
  129. {
  130. int ln = s->nbits;
  131. int j, np, np2;
  132. int nblocks, nloops;
  133. register FFTComplex *p, *q;
  134. FFTComplex *exptab = s->exptab;
  135. int l;
  136. FFTSample tmp_re, tmp_im;
  137. np = 1 << ln;
  138. /* pass 0 */
  139. p=&z[0];
  140. j=(np >> 1);
  141. do {
  142. BF(p[0].re, p[0].im, p[1].re, p[1].im,
  143. p[0].re, p[0].im, p[1].re, p[1].im);
  144. p+=2;
  145. } while (--j != 0);
  146. /* pass 1 */
  147. p=&z[0];
  148. j=np >> 2;
  149. if (s->inverse) {
  150. do {
  151. BF(p[0].re, p[0].im, p[2].re, p[2].im,
  152. p[0].re, p[0].im, p[2].re, p[2].im);
  153. BF(p[1].re, p[1].im, p[3].re, p[3].im,
  154. p[1].re, p[1].im, -p[3].im, p[3].re);
  155. p+=4;
  156. } while (--j != 0);
  157. } else {
  158. do {
  159. BF(p[0].re, p[0].im, p[2].re, p[2].im,
  160. p[0].re, p[0].im, p[2].re, p[2].im);
  161. BF(p[1].re, p[1].im, p[3].re, p[3].im,
  162. p[1].re, p[1].im, p[3].im, -p[3].re);
  163. p+=4;
  164. } while (--j != 0);
  165. }
  166. /* pass 2 .. ln-1 */
  167. nblocks = np >> 3;
  168. nloops = 1 << 2;
  169. np2 = np >> 1;
  170. do {
  171. p = z;
  172. q = z + nloops;
  173. for (j = 0; j < nblocks; ++j) {
  174. BF(p->re, p->im, q->re, q->im,
  175. p->re, p->im, q->re, q->im);
  176. p++;
  177. q++;
  178. for(l = nblocks; l < np2; l += nblocks) {
  179. CMUL(tmp_re, tmp_im, exptab[l].re, exptab[l].im, q->re, q->im);
  180. BF(p->re, p->im, q->re, q->im,
  181. p->re, p->im, tmp_re, tmp_im);
  182. p++;
  183. q++;
  184. }
  185. p += nloops;
  186. q += nloops;
  187. }
  188. nblocks = nblocks >> 1;
  189. nloops = nloops << 1;
  190. } while (nblocks != 0);
  191. }
  192. /**
  193. * Do the permutation needed BEFORE calling fft_calc()
  194. */
  195. void fft_permute(FFTContext *s, FFTComplex *z)
  196. {
  197. int j, k, np;
  198. FFTComplex tmp;
  199. const uint16_t *revtab = s->revtab;
  200. /* reverse */
  201. np = 1 << s->nbits;
  202. for(j=0;j<np;j++) {
  203. k = revtab[j];
  204. if (k < j) {
  205. tmp = z[k];
  206. z[k] = z[j];
  207. z[j] = tmp;
  208. }
  209. }
  210. }
  211. void fft_end(FFTContext *s)
  212. {
  213. av_freep(&s->revtab);
  214. av_freep(&s->exptab);
  215. av_freep(&s->exptab1);
  216. }