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.

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