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.

267 lines
6.5KB

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