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.

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