fft.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * FFT/IFFT transforms
  3. * Copyright (c) 2002 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file fft.c
  23. * FFT/IFFT transforms.
  24. */
  25. #include "dsputil.h"
  26. /**
  27. * The size of the FFT is 2^nbits. If inverse is TRUE, inverse FFT is
  28. * done
  29. */
  30. int ff_fft_init(FFTContext *s, int nbits, int inverse)
  31. {
  32. int i, j, m, n;
  33. float alpha, c1, s1, s2;
  34. s->nbits = nbits;
  35. n = 1 << nbits;
  36. s->exptab = av_malloc((n / 2) * sizeof(FFTComplex));
  37. if (!s->exptab)
  38. goto fail;
  39. s->revtab = av_malloc(n * sizeof(uint16_t));
  40. if (!s->revtab)
  41. goto fail;
  42. s->inverse = inverse;
  43. s2 = inverse ? 1.0 : -1.0;
  44. for(i=0;i<(n/2);i++) {
  45. alpha = 2 * M_PI * (float)i / (float)n;
  46. c1 = cos(alpha);
  47. s1 = sin(alpha) * s2;
  48. s->exptab[i].re = c1;
  49. s->exptab[i].im = s1;
  50. }
  51. s->fft_calc = ff_fft_calc_c;
  52. s->imdct_calc = ff_imdct_calc;
  53. s->exptab1 = NULL;
  54. /* compute constant table for HAVE_SSE version */
  55. #if defined(HAVE_MMX) \
  56. || (defined(HAVE_ALTIVEC) && !defined(ALTIVEC_USE_REFERENCE_C_CODE))
  57. {
  58. int has_vectors = mm_support();
  59. if (has_vectors) {
  60. #if defined(HAVE_MMX)
  61. if (has_vectors & MM_3DNOWEXT) {
  62. /* 3DNowEx for K7/K8 */
  63. s->imdct_calc = ff_imdct_calc_3dn2;
  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. } else if (has_vectors & MM_SSE) {
  69. /* SSE for P3/P4 */
  70. s->imdct_calc = ff_imdct_calc_sse;
  71. s->fft_calc = ff_fft_calc_sse;
  72. }
  73. #else /* HAVE_MMX */
  74. if (has_vectors & MM_ALTIVEC)
  75. s->fft_calc = ff_fft_calc_altivec;
  76. #endif
  77. }
  78. if (s->fft_calc != ff_fft_calc_c) {
  79. int np, nblocks, np2, l;
  80. FFTComplex *q;
  81. np = 1 << nbits;
  82. nblocks = np >> 3;
  83. np2 = np >> 1;
  84. s->exptab1 = av_malloc(np * 2 * sizeof(FFTComplex));
  85. if (!s->exptab1)
  86. goto fail;
  87. q = s->exptab1;
  88. do {
  89. for(l = 0; l < np2; l += 2 * nblocks) {
  90. *q++ = s->exptab[l];
  91. *q++ = s->exptab[l + nblocks];
  92. q->re = -s->exptab[l].im;
  93. q->im = s->exptab[l].re;
  94. q++;
  95. q->re = -s->exptab[l + nblocks].im;
  96. q->im = s->exptab[l + nblocks].re;
  97. q++;
  98. }
  99. nblocks = nblocks >> 1;
  100. } while (nblocks != 0);
  101. av_freep(&s->exptab);
  102. }
  103. }
  104. #endif
  105. /* compute bit reverse table */
  106. for(i=0;i<n;i++) {
  107. m=0;
  108. for(j=0;j<nbits;j++) {
  109. m |= ((i >> j) & 1) << (nbits-j-1);
  110. }
  111. s->revtab[i]=m;
  112. }
  113. return 0;
  114. fail:
  115. av_freep(&s->revtab);
  116. av_freep(&s->exptab);
  117. av_freep(&s->exptab1);
  118. return -1;
  119. }
  120. /* butter fly op */
  121. #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
  122. {\
  123. FFTSample ax, ay, bx, by;\
  124. bx=pre1;\
  125. by=pim1;\
  126. ax=qre1;\
  127. ay=qim1;\
  128. pre = (bx + ax);\
  129. pim = (by + ay);\
  130. qre = (bx - ax);\
  131. qim = (by - ay);\
  132. }
  133. #define MUL16(a,b) ((a) * (b))
  134. #define CMUL(pre, pim, are, aim, bre, bim) \
  135. {\
  136. pre = (MUL16(are, bre) - MUL16(aim, bim));\
  137. pim = (MUL16(are, bim) + MUL16(bre, aim));\
  138. }
  139. /**
  140. * Do a complex FFT with the parameters defined in ff_fft_init(). The
  141. * input data must be permuted before with s->revtab table. No
  142. * 1.0/sqrt(n) normalization is done.
  143. */
  144. void ff_fft_calc_c(FFTContext *s, FFTComplex *z)
  145. {
  146. int ln = s->nbits;
  147. int j, np, np2;
  148. int nblocks, nloops;
  149. register FFTComplex *p, *q;
  150. FFTComplex *exptab = s->exptab;
  151. int l;
  152. FFTSample tmp_re, tmp_im;
  153. np = 1 << ln;
  154. /* pass 0 */
  155. p=&z[0];
  156. j=(np >> 1);
  157. do {
  158. BF(p[0].re, p[0].im, p[1].re, p[1].im,
  159. p[0].re, p[0].im, p[1].re, p[1].im);
  160. p+=2;
  161. } while (--j != 0);
  162. /* pass 1 */
  163. p=&z[0];
  164. j=np >> 2;
  165. if (s->inverse) {
  166. do {
  167. BF(p[0].re, p[0].im, p[2].re, p[2].im,
  168. p[0].re, p[0].im, p[2].re, p[2].im);
  169. BF(p[1].re, p[1].im, p[3].re, p[3].im,
  170. p[1].re, p[1].im, -p[3].im, p[3].re);
  171. p+=4;
  172. } while (--j != 0);
  173. } else {
  174. do {
  175. BF(p[0].re, p[0].im, p[2].re, p[2].im,
  176. p[0].re, p[0].im, p[2].re, p[2].im);
  177. BF(p[1].re, p[1].im, p[3].re, p[3].im,
  178. p[1].re, p[1].im, p[3].im, -p[3].re);
  179. p+=4;
  180. } while (--j != 0);
  181. }
  182. /* pass 2 .. ln-1 */
  183. nblocks = np >> 3;
  184. nloops = 1 << 2;
  185. np2 = np >> 1;
  186. do {
  187. p = z;
  188. q = z + nloops;
  189. for (j = 0; j < nblocks; ++j) {
  190. BF(p->re, p->im, q->re, q->im,
  191. p->re, p->im, q->re, q->im);
  192. p++;
  193. q++;
  194. for(l = nblocks; l < np2; l += nblocks) {
  195. CMUL(tmp_re, tmp_im, exptab[l].re, exptab[l].im, q->re, q->im);
  196. BF(p->re, p->im, q->re, q->im,
  197. p->re, p->im, tmp_re, tmp_im);
  198. p++;
  199. q++;
  200. }
  201. p += nloops;
  202. q += nloops;
  203. }
  204. nblocks = nblocks >> 1;
  205. nloops = nloops << 1;
  206. } while (nblocks != 0);
  207. }
  208. /**
  209. * Do the permutation needed BEFORE calling ff_fft_calc()
  210. */
  211. void ff_fft_permute(FFTContext *s, FFTComplex *z)
  212. {
  213. int j, k, np;
  214. FFTComplex tmp;
  215. const uint16_t *revtab = s->revtab;
  216. /* reverse */
  217. np = 1 << s->nbits;
  218. for(j=0;j<np;j++) {
  219. k = revtab[j];
  220. if (k < j) {
  221. tmp = z[k];
  222. z[k] = z[j];
  223. z[j] = tmp;
  224. }
  225. }
  226. }
  227. void ff_fft_end(FFTContext *s)
  228. {
  229. av_freep(&s->revtab);
  230. av_freep(&s->exptab);
  231. av_freep(&s->exptab1);
  232. }