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.

230 lines
5.5KB

  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) && 0
  49. if (mm_flags & MM_SSE) {
  50. int np, nblocks, np2, l;
  51. FFTComplex *q;
  52. np = 1 << nbits;
  53. nblocks = np >> 3;
  54. np2 = np >> 1;
  55. s->exptab1 = av_malloc(np * 2 * sizeof(FFTComplex));
  56. if (!s->exptab1)
  57. goto fail;
  58. q = s->exptab1;
  59. do {
  60. for(l = 0; l < np2; l += 2 * nblocks) {
  61. *q++ = s->exptab[l];
  62. *q++ = s->exptab[l + nblocks];
  63. q->re = -s->exptab[l].im;
  64. q->im = s->exptab[l].re;
  65. q++;
  66. q->re = -s->exptab[l + nblocks].im;
  67. q->im = s->exptab[l + nblocks].re;
  68. q++;
  69. }
  70. nblocks = nblocks >> 1;
  71. } while (nblocks != 0);
  72. av_freep(&s->exptab);
  73. }
  74. #endif
  75. /* compute bit reverse table */
  76. for(i=0;i<n;i++) {
  77. m=0;
  78. for(j=0;j<nbits;j++) {
  79. m |= ((i >> j) & 1) << (nbits-j-1);
  80. }
  81. s->revtab[i]=m;
  82. }
  83. return 0;
  84. fail:
  85. av_freep(&s->revtab);
  86. av_freep(&s->exptab);
  87. av_freep(&s->exptab1);
  88. return -1;
  89. }
  90. /* butter fly op */
  91. #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
  92. {\
  93. FFTSample ax, ay, bx, by;\
  94. bx=pre1;\
  95. by=pim1;\
  96. ax=qre1;\
  97. ay=qim1;\
  98. pre = (bx + ax);\
  99. pim = (by + ay);\
  100. qre = (bx - ax);\
  101. qim = (by - ay);\
  102. }
  103. #define MUL16(a,b) ((a) * (b))
  104. #define CMUL(pre, pim, are, aim, bre, bim) \
  105. {\
  106. pre = (MUL16(are, bre) - MUL16(aim, bim));\
  107. pim = (MUL16(are, bim) + MUL16(bre, aim));\
  108. }
  109. /**
  110. * Do a complex FFT with the parameters defined in fft_init(). The
  111. * input data must be permuted before with s->revtab table. No
  112. * 1.0/sqrt(n) normalization is done.
  113. */
  114. void fft_calc_c(FFTContext *s, FFTComplex *z)
  115. {
  116. int ln = s->nbits;
  117. int j, np, np2;
  118. int nblocks, nloops;
  119. register FFTComplex *p, *q;
  120. FFTComplex *exptab = s->exptab;
  121. int l;
  122. FFTSample tmp_re, tmp_im;
  123. np = 1 << ln;
  124. /* pass 0 */
  125. p=&z[0];
  126. j=(np >> 1);
  127. do {
  128. BF(p[0].re, p[0].im, p[1].re, p[1].im,
  129. p[0].re, p[0].im, p[1].re, p[1].im);
  130. p+=2;
  131. } while (--j != 0);
  132. /* pass 1 */
  133. p=&z[0];
  134. j=np >> 2;
  135. if (s->inverse) {
  136. do {
  137. BF(p[0].re, p[0].im, p[2].re, p[2].im,
  138. p[0].re, p[0].im, p[2].re, p[2].im);
  139. BF(p[1].re, p[1].im, p[3].re, p[3].im,
  140. p[1].re, p[1].im, -p[3].im, p[3].re);
  141. p+=4;
  142. } while (--j != 0);
  143. } else {
  144. do {
  145. BF(p[0].re, p[0].im, p[2].re, p[2].im,
  146. p[0].re, p[0].im, p[2].re, p[2].im);
  147. BF(p[1].re, p[1].im, p[3].re, p[3].im,
  148. p[1].re, p[1].im, p[3].im, -p[3].re);
  149. p+=4;
  150. } while (--j != 0);
  151. }
  152. /* pass 2 .. ln-1 */
  153. nblocks = np >> 3;
  154. nloops = 1 << 2;
  155. np2 = np >> 1;
  156. do {
  157. p = z;
  158. q = z + nloops;
  159. for (j = 0; j < nblocks; ++j) {
  160. BF(p->re, p->im, q->re, q->im,
  161. p->re, p->im, q->re, q->im);
  162. p++;
  163. q++;
  164. for(l = nblocks; l < np2; l += nblocks) {
  165. CMUL(tmp_re, tmp_im, exptab[l].re, exptab[l].im, q->re, q->im);
  166. BF(p->re, p->im, q->re, q->im,
  167. p->re, p->im, tmp_re, tmp_im);
  168. p++;
  169. q++;
  170. }
  171. p += nloops;
  172. q += nloops;
  173. }
  174. nblocks = nblocks >> 1;
  175. nloops = nloops << 1;
  176. } while (nblocks != 0);
  177. }
  178. /**
  179. * Do the permutation needed BEFORE calling fft_calc()
  180. */
  181. void fft_permute(FFTContext *s, FFTComplex *z)
  182. {
  183. int j, k, np;
  184. FFTComplex tmp;
  185. const uint16_t *revtab = s->revtab;
  186. /* reverse */
  187. np = 1 << s->nbits;
  188. for(j=0;j<np;j++) {
  189. k = revtab[j];
  190. if (k < j) {
  191. tmp = z[k];
  192. z[k] = z[j];
  193. z[j] = tmp;
  194. }
  195. }
  196. }
  197. void fft_end(FFTContext *s)
  198. {
  199. av_freep(&s->revtab);
  200. av_freep(&s->exptab);
  201. av_freep(&s->exptab1);
  202. }