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.

264 lines
6.4KB

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