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.

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