fft_template.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * FFT/IFFT transforms
  3. * Copyright (c) 2008 Loren Merritt
  4. * Copyright (c) 2002 Fabrice Bellard
  5. * Partly based on libdjbfft by D. J. Bernstein
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * FFT/IFFT transforms.
  26. */
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/thread.h"
  31. #include "fft.h"
  32. #include "fft-internal.h"
  33. #if FFT_FIXED_32
  34. #include "fft_table.h"
  35. static void av_cold fft_lut_init(void)
  36. {
  37. int n = 0;
  38. ff_fft_lut_init(ff_fft_offsets_lut, 0, 1 << 17, &n);
  39. }
  40. #else /* FFT_FIXED_32 */
  41. /* cos(2*pi*x/n) for 0<=x<=n/4, followed by its reverse */
  42. #if !CONFIG_HARDCODED_TABLES
  43. COSTABLE(16);
  44. COSTABLE(32);
  45. COSTABLE(64);
  46. COSTABLE(128);
  47. COSTABLE(256);
  48. COSTABLE(512);
  49. COSTABLE(1024);
  50. COSTABLE(2048);
  51. COSTABLE(4096);
  52. COSTABLE(8192);
  53. COSTABLE(16384);
  54. COSTABLE(32768);
  55. COSTABLE(65536);
  56. COSTABLE(131072);
  57. static av_cold void init_ff_cos_tabs(int index)
  58. {
  59. int i;
  60. int m = 1<<index;
  61. double freq = 2*M_PI/m;
  62. FFTSample *tab = FFT_NAME(ff_cos_tabs)[index];
  63. for(i=0; i<=m/4; i++)
  64. tab[i] = FIX15(cos(i*freq));
  65. for(i=1; i<m/4; i++)
  66. tab[m/2-i] = tab[i];
  67. }
  68. typedef struct CosTabsInitOnce {
  69. void (*func)(void);
  70. AVOnce control;
  71. } CosTabsInitOnce;
  72. #define INIT_FF_COS_TABS_FUNC(index, size) \
  73. static av_cold void init_ff_cos_tabs_ ## size (void)\
  74. { \
  75. init_ff_cos_tabs(index); \
  76. }
  77. INIT_FF_COS_TABS_FUNC(4, 16)
  78. INIT_FF_COS_TABS_FUNC(5, 32)
  79. INIT_FF_COS_TABS_FUNC(6, 64)
  80. INIT_FF_COS_TABS_FUNC(7, 128)
  81. INIT_FF_COS_TABS_FUNC(8, 256)
  82. INIT_FF_COS_TABS_FUNC(9, 512)
  83. INIT_FF_COS_TABS_FUNC(10, 1024)
  84. INIT_FF_COS_TABS_FUNC(11, 2048)
  85. INIT_FF_COS_TABS_FUNC(12, 4096)
  86. INIT_FF_COS_TABS_FUNC(13, 8192)
  87. INIT_FF_COS_TABS_FUNC(14, 16384)
  88. INIT_FF_COS_TABS_FUNC(15, 32768)
  89. INIT_FF_COS_TABS_FUNC(16, 65536)
  90. INIT_FF_COS_TABS_FUNC(17, 131072)
  91. static CosTabsInitOnce cos_tabs_init_once[] = {
  92. { NULL },
  93. { NULL },
  94. { NULL },
  95. { NULL },
  96. { init_ff_cos_tabs_16, AV_ONCE_INIT },
  97. { init_ff_cos_tabs_32, AV_ONCE_INIT },
  98. { init_ff_cos_tabs_64, AV_ONCE_INIT },
  99. { init_ff_cos_tabs_128, AV_ONCE_INIT },
  100. { init_ff_cos_tabs_256, AV_ONCE_INIT },
  101. { init_ff_cos_tabs_512, AV_ONCE_INIT },
  102. { init_ff_cos_tabs_1024, AV_ONCE_INIT },
  103. { init_ff_cos_tabs_2048, AV_ONCE_INIT },
  104. { init_ff_cos_tabs_4096, AV_ONCE_INIT },
  105. { init_ff_cos_tabs_8192, AV_ONCE_INIT },
  106. { init_ff_cos_tabs_16384, AV_ONCE_INIT },
  107. { init_ff_cos_tabs_32768, AV_ONCE_INIT },
  108. { init_ff_cos_tabs_65536, AV_ONCE_INIT },
  109. { init_ff_cos_tabs_131072, AV_ONCE_INIT },
  110. };
  111. #endif
  112. COSTABLE_CONST FFTSample * const FFT_NAME(ff_cos_tabs)[] = {
  113. NULL, NULL, NULL, NULL,
  114. FFT_NAME(ff_cos_16),
  115. FFT_NAME(ff_cos_32),
  116. FFT_NAME(ff_cos_64),
  117. FFT_NAME(ff_cos_128),
  118. FFT_NAME(ff_cos_256),
  119. FFT_NAME(ff_cos_512),
  120. FFT_NAME(ff_cos_1024),
  121. FFT_NAME(ff_cos_2048),
  122. FFT_NAME(ff_cos_4096),
  123. FFT_NAME(ff_cos_8192),
  124. FFT_NAME(ff_cos_16384),
  125. FFT_NAME(ff_cos_32768),
  126. FFT_NAME(ff_cos_65536),
  127. FFT_NAME(ff_cos_131072),
  128. };
  129. #endif /* FFT_FIXED_32 */
  130. static void fft_permute_c(FFTContext *s, FFTComplex *z);
  131. static void fft_calc_c(FFTContext *s, FFTComplex *z);
  132. static int split_radix_permutation(int i, int n, int inverse)
  133. {
  134. int m;
  135. if(n <= 2) return i&1;
  136. m = n >> 1;
  137. if(!(i&m)) return split_radix_permutation(i, m, inverse)*2;
  138. m >>= 1;
  139. if(inverse == !(i&m)) return split_radix_permutation(i, m, inverse)*4 + 1;
  140. else return split_radix_permutation(i, m, inverse)*4 - 1;
  141. }
  142. av_cold void ff_init_ff_cos_tabs(int index)
  143. {
  144. #if (!CONFIG_HARDCODED_TABLES) && (!FFT_FIXED_32)
  145. ff_thread_once(&cos_tabs_init_once[index].control, cos_tabs_init_once[index].func);
  146. #endif
  147. }
  148. static const int avx_tab[] = {
  149. 0, 4, 1, 5, 8, 12, 9, 13, 2, 6, 3, 7, 10, 14, 11, 15
  150. };
  151. static int is_second_half_of_fft32(int i, int n)
  152. {
  153. if (n <= 32)
  154. return i >= 16;
  155. else if (i < n/2)
  156. return is_second_half_of_fft32(i, n/2);
  157. else if (i < 3*n/4)
  158. return is_second_half_of_fft32(i - n/2, n/4);
  159. else
  160. return is_second_half_of_fft32(i - 3*n/4, n/4);
  161. }
  162. static av_cold void fft_perm_avx(FFTContext *s)
  163. {
  164. int i;
  165. int n = 1 << s->nbits;
  166. for (i = 0; i < n; i += 16) {
  167. int k;
  168. if (is_second_half_of_fft32(i, n)) {
  169. for (k = 0; k < 16; k++)
  170. s->revtab[-split_radix_permutation(i + k, n, s->inverse) & (n - 1)] =
  171. i + avx_tab[k];
  172. } else {
  173. for (k = 0; k < 16; k++) {
  174. int j = i + k;
  175. j = (j & ~7) | ((j >> 1) & 3) | ((j << 2) & 4);
  176. s->revtab[-split_radix_permutation(i + k, n, s->inverse) & (n - 1)] = j;
  177. }
  178. }
  179. }
  180. }
  181. av_cold int ff_fft_init(FFTContext *s, int nbits, int inverse)
  182. {
  183. int i, j, n;
  184. s->revtab = NULL;
  185. s->revtab32 = NULL;
  186. if (nbits < 2 || nbits > 17)
  187. goto fail;
  188. s->nbits = nbits;
  189. n = 1 << nbits;
  190. if (nbits <= 16) {
  191. s->revtab = av_malloc(n * sizeof(uint16_t));
  192. if (!s->revtab)
  193. goto fail;
  194. } else {
  195. s->revtab32 = av_malloc(n * sizeof(uint32_t));
  196. if (!s->revtab32)
  197. goto fail;
  198. }
  199. s->tmp_buf = av_malloc(n * sizeof(FFTComplex));
  200. if (!s->tmp_buf)
  201. goto fail;
  202. s->inverse = inverse;
  203. s->fft_permutation = FF_FFT_PERM_DEFAULT;
  204. s->fft_permute = fft_permute_c;
  205. s->fft_calc = fft_calc_c;
  206. #if CONFIG_MDCT
  207. s->imdct_calc = ff_imdct_calc_c;
  208. s->imdct_half = ff_imdct_half_c;
  209. s->mdct_calc = ff_mdct_calc_c;
  210. #endif
  211. #if FFT_FIXED_32
  212. {
  213. static AVOnce control = AV_ONCE_INIT;
  214. ff_thread_once(&control, fft_lut_init);
  215. }
  216. #else /* FFT_FIXED_32 */
  217. #if FFT_FLOAT
  218. if (ARCH_AARCH64) ff_fft_init_aarch64(s);
  219. if (ARCH_ARM) ff_fft_init_arm(s);
  220. if (ARCH_PPC) ff_fft_init_ppc(s);
  221. if (ARCH_X86) ff_fft_init_x86(s);
  222. if (CONFIG_MDCT) s->mdct_calcw = s->mdct_calc;
  223. if (HAVE_MIPSFPU) ff_fft_init_mips(s);
  224. #else
  225. if (CONFIG_MDCT) s->mdct_calcw = ff_mdct_calcw_c;
  226. if (ARCH_ARM) ff_fft_fixed_init_arm(s);
  227. #endif
  228. for(j=4; j<=nbits; j++) {
  229. ff_init_ff_cos_tabs(j);
  230. }
  231. #endif /* FFT_FIXED_32 */
  232. if (s->fft_permutation == FF_FFT_PERM_AVX) {
  233. fft_perm_avx(s);
  234. } else {
  235. for(i=0; i<n; i++) {
  236. int k;
  237. j = i;
  238. if (s->fft_permutation == FF_FFT_PERM_SWAP_LSBS)
  239. j = (j&~3) | ((j>>1)&1) | ((j<<1)&2);
  240. k = -split_radix_permutation(i, n, s->inverse) & (n-1);
  241. if (s->revtab)
  242. s->revtab[k] = j;
  243. if (s->revtab32)
  244. s->revtab32[k] = j;
  245. }
  246. }
  247. return 0;
  248. fail:
  249. av_freep(&s->revtab);
  250. av_freep(&s->revtab32);
  251. av_freep(&s->tmp_buf);
  252. return -1;
  253. }
  254. static void fft_permute_c(FFTContext *s, FFTComplex *z)
  255. {
  256. int j, np;
  257. const uint16_t *revtab = s->revtab;
  258. const uint32_t *revtab32 = s->revtab32;
  259. np = 1 << s->nbits;
  260. /* TODO: handle split-radix permute in a more optimal way, probably in-place */
  261. if (revtab) {
  262. for(j=0;j<np;j++) s->tmp_buf[revtab[j]] = z[j];
  263. } else
  264. for(j=0;j<np;j++) s->tmp_buf[revtab32[j]] = z[j];
  265. memcpy(z, s->tmp_buf, np * sizeof(FFTComplex));
  266. }
  267. av_cold void ff_fft_end(FFTContext *s)
  268. {
  269. av_freep(&s->revtab);
  270. av_freep(&s->revtab32);
  271. av_freep(&s->tmp_buf);
  272. }
  273. #if FFT_FIXED_32
  274. static void fft_calc_c(FFTContext *s, FFTComplex *z) {
  275. int nbits, i, n, num_transforms, offset, step;
  276. int n4, n2, n34;
  277. unsigned tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
  278. FFTComplex *tmpz;
  279. const int fft_size = (1 << s->nbits);
  280. int64_t accu;
  281. num_transforms = (0x2aab >> (16 - s->nbits)) | 1;
  282. for (n=0; n<num_transforms; n++){
  283. offset = ff_fft_offsets_lut[n] << 2;
  284. tmpz = z + offset;
  285. tmp1 = tmpz[0].re + (unsigned)tmpz[1].re;
  286. tmp5 = tmpz[2].re + (unsigned)tmpz[3].re;
  287. tmp2 = tmpz[0].im + (unsigned)tmpz[1].im;
  288. tmp6 = tmpz[2].im + (unsigned)tmpz[3].im;
  289. tmp3 = tmpz[0].re - (unsigned)tmpz[1].re;
  290. tmp8 = tmpz[2].im - (unsigned)tmpz[3].im;
  291. tmp4 = tmpz[0].im - (unsigned)tmpz[1].im;
  292. tmp7 = tmpz[2].re - (unsigned)tmpz[3].re;
  293. tmpz[0].re = tmp1 + tmp5;
  294. tmpz[2].re = tmp1 - tmp5;
  295. tmpz[0].im = tmp2 + tmp6;
  296. tmpz[2].im = tmp2 - tmp6;
  297. tmpz[1].re = tmp3 + tmp8;
  298. tmpz[3].re = tmp3 - tmp8;
  299. tmpz[1].im = tmp4 - tmp7;
  300. tmpz[3].im = tmp4 + tmp7;
  301. }
  302. if (fft_size < 8)
  303. return;
  304. num_transforms = (num_transforms >> 1) | 1;
  305. for (n=0; n<num_transforms; n++){
  306. offset = ff_fft_offsets_lut[n] << 3;
  307. tmpz = z + offset;
  308. tmp1 = tmpz[4].re + (unsigned)tmpz[5].re;
  309. tmp3 = tmpz[6].re + (unsigned)tmpz[7].re;
  310. tmp2 = tmpz[4].im + (unsigned)tmpz[5].im;
  311. tmp4 = tmpz[6].im + (unsigned)tmpz[7].im;
  312. tmp5 = tmp1 + tmp3;
  313. tmp7 = tmp1 - tmp3;
  314. tmp6 = tmp2 + tmp4;
  315. tmp8 = tmp2 - tmp4;
  316. tmp1 = tmpz[4].re - (unsigned)tmpz[5].re;
  317. tmp2 = tmpz[4].im - (unsigned)tmpz[5].im;
  318. tmp3 = tmpz[6].re - (unsigned)tmpz[7].re;
  319. tmp4 = tmpz[6].im - (unsigned)tmpz[7].im;
  320. tmpz[4].re = tmpz[0].re - tmp5;
  321. tmpz[0].re = tmpz[0].re + tmp5;
  322. tmpz[4].im = tmpz[0].im - tmp6;
  323. tmpz[0].im = tmpz[0].im + tmp6;
  324. tmpz[6].re = tmpz[2].re - tmp8;
  325. tmpz[2].re = tmpz[2].re + tmp8;
  326. tmpz[6].im = tmpz[2].im + tmp7;
  327. tmpz[2].im = tmpz[2].im - tmp7;
  328. accu = (int64_t)Q31(M_SQRT1_2)*(int)(tmp1 + tmp2);
  329. tmp5 = (int32_t)((accu + 0x40000000) >> 31);
  330. accu = (int64_t)Q31(M_SQRT1_2)*(int)(tmp3 - tmp4);
  331. tmp7 = (int32_t)((accu + 0x40000000) >> 31);
  332. accu = (int64_t)Q31(M_SQRT1_2)*(int)(tmp2 - tmp1);
  333. tmp6 = (int32_t)((accu + 0x40000000) >> 31);
  334. accu = (int64_t)Q31(M_SQRT1_2)*(int)(tmp3 + tmp4);
  335. tmp8 = (int32_t)((accu + 0x40000000) >> 31);
  336. tmp1 = tmp5 + tmp7;
  337. tmp3 = tmp5 - tmp7;
  338. tmp2 = tmp6 + tmp8;
  339. tmp4 = tmp6 - tmp8;
  340. tmpz[5].re = tmpz[1].re - tmp1;
  341. tmpz[1].re = tmpz[1].re + tmp1;
  342. tmpz[5].im = tmpz[1].im - tmp2;
  343. tmpz[1].im = tmpz[1].im + tmp2;
  344. tmpz[7].re = tmpz[3].re - tmp4;
  345. tmpz[3].re = tmpz[3].re + tmp4;
  346. tmpz[7].im = tmpz[3].im + tmp3;
  347. tmpz[3].im = tmpz[3].im - tmp3;
  348. }
  349. step = 1 << ((MAX_LOG2_NFFT-4) - 4);
  350. n4 = 4;
  351. for (nbits=4; nbits<=s->nbits; nbits++){
  352. n2 = 2*n4;
  353. n34 = 3*n4;
  354. num_transforms = (num_transforms >> 1) | 1;
  355. for (n=0; n<num_transforms; n++){
  356. const FFTSample *w_re_ptr = ff_w_tab_sr + step;
  357. const FFTSample *w_im_ptr = ff_w_tab_sr + MAX_FFT_SIZE/(4*16) - step;
  358. offset = ff_fft_offsets_lut[n] << nbits;
  359. tmpz = z + offset;
  360. tmp5 = tmpz[ n2].re + (unsigned)tmpz[n34].re;
  361. tmp1 = tmpz[ n2].re - (unsigned)tmpz[n34].re;
  362. tmp6 = tmpz[ n2].im + (unsigned)tmpz[n34].im;
  363. tmp2 = tmpz[ n2].im - (unsigned)tmpz[n34].im;
  364. tmpz[ n2].re = tmpz[ 0].re - tmp5;
  365. tmpz[ 0].re = tmpz[ 0].re + tmp5;
  366. tmpz[ n2].im = tmpz[ 0].im - tmp6;
  367. tmpz[ 0].im = tmpz[ 0].im + tmp6;
  368. tmpz[n34].re = tmpz[n4].re - tmp2;
  369. tmpz[ n4].re = tmpz[n4].re + tmp2;
  370. tmpz[n34].im = tmpz[n4].im + tmp1;
  371. tmpz[ n4].im = tmpz[n4].im - tmp1;
  372. for (i=1; i<n4; i++){
  373. FFTSample w_re = w_re_ptr[0];
  374. FFTSample w_im = w_im_ptr[0];
  375. accu = (int64_t)w_re*tmpz[ n2+i].re;
  376. accu += (int64_t)w_im*tmpz[ n2+i].im;
  377. tmp1 = (int32_t)((accu + 0x40000000) >> 31);
  378. accu = (int64_t)w_re*tmpz[ n2+i].im;
  379. accu -= (int64_t)w_im*tmpz[ n2+i].re;
  380. tmp2 = (int32_t)((accu + 0x40000000) >> 31);
  381. accu = (int64_t)w_re*tmpz[n34+i].re;
  382. accu -= (int64_t)w_im*tmpz[n34+i].im;
  383. tmp3 = (int32_t)((accu + 0x40000000) >> 31);
  384. accu = (int64_t)w_re*tmpz[n34+i].im;
  385. accu += (int64_t)w_im*tmpz[n34+i].re;
  386. tmp4 = (int32_t)((accu + 0x40000000) >> 31);
  387. tmp5 = tmp1 + tmp3;
  388. tmp1 = tmp1 - tmp3;
  389. tmp6 = tmp2 + tmp4;
  390. tmp2 = tmp2 - tmp4;
  391. tmpz[ n2+i].re = tmpz[ i].re - tmp5;
  392. tmpz[ i].re = tmpz[ i].re + tmp5;
  393. tmpz[ n2+i].im = tmpz[ i].im - tmp6;
  394. tmpz[ i].im = tmpz[ i].im + tmp6;
  395. tmpz[n34+i].re = tmpz[n4+i].re - tmp2;
  396. tmpz[ n4+i].re = tmpz[n4+i].re + tmp2;
  397. tmpz[n34+i].im = tmpz[n4+i].im + tmp1;
  398. tmpz[ n4+i].im = tmpz[n4+i].im - tmp1;
  399. w_re_ptr += step;
  400. w_im_ptr -= step;
  401. }
  402. }
  403. step >>= 1;
  404. n4 <<= 1;
  405. }
  406. }
  407. #else /* FFT_FIXED_32 */
  408. #define BUTTERFLIES(a0,a1,a2,a3) {\
  409. BF(t3, t5, t5, t1);\
  410. BF(a2.re, a0.re, a0.re, t5);\
  411. BF(a3.im, a1.im, a1.im, t3);\
  412. BF(t4, t6, t2, t6);\
  413. BF(a3.re, a1.re, a1.re, t4);\
  414. BF(a2.im, a0.im, a0.im, t6);\
  415. }
  416. // force loading all the inputs before storing any.
  417. // this is slightly slower for small data, but avoids store->load aliasing
  418. // for addresses separated by large powers of 2.
  419. #define BUTTERFLIES_BIG(a0,a1,a2,a3) {\
  420. FFTSample r0=a0.re, i0=a0.im, r1=a1.re, i1=a1.im;\
  421. BF(t3, t5, t5, t1);\
  422. BF(a2.re, a0.re, r0, t5);\
  423. BF(a3.im, a1.im, i1, t3);\
  424. BF(t4, t6, t2, t6);\
  425. BF(a3.re, a1.re, r1, t4);\
  426. BF(a2.im, a0.im, i0, t6);\
  427. }
  428. #define TRANSFORM(a0,a1,a2,a3,wre,wim) {\
  429. CMUL(t1, t2, a2.re, a2.im, wre, -wim);\
  430. CMUL(t5, t6, a3.re, a3.im, wre, wim);\
  431. BUTTERFLIES(a0,a1,a2,a3)\
  432. }
  433. #define TRANSFORM_ZERO(a0,a1,a2,a3) {\
  434. t1 = a2.re;\
  435. t2 = a2.im;\
  436. t5 = a3.re;\
  437. t6 = a3.im;\
  438. BUTTERFLIES(a0,a1,a2,a3)\
  439. }
  440. /* z[0...8n-1], w[1...2n-1] */
  441. #define PASS(name)\
  442. static void name(FFTComplex *z, const FFTSample *wre, unsigned int n)\
  443. {\
  444. FFTDouble t1, t2, t3, t4, t5, t6;\
  445. int o1 = 2*n;\
  446. int o2 = 4*n;\
  447. int o3 = 6*n;\
  448. const FFTSample *wim = wre+o1;\
  449. n--;\
  450. \
  451. TRANSFORM_ZERO(z[0],z[o1],z[o2],z[o3]);\
  452. TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
  453. do {\
  454. z += 2;\
  455. wre += 2;\
  456. wim -= 2;\
  457. TRANSFORM(z[0],z[o1],z[o2],z[o3],wre[0],wim[0]);\
  458. TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
  459. } while(--n);\
  460. }
  461. PASS(pass)
  462. #if !CONFIG_SMALL
  463. #undef BUTTERFLIES
  464. #define BUTTERFLIES BUTTERFLIES_BIG
  465. PASS(pass_big)
  466. #endif
  467. #define DECL_FFT(n,n2,n4)\
  468. static void fft##n(FFTComplex *z)\
  469. {\
  470. fft##n2(z);\
  471. fft##n4(z+n4*2);\
  472. fft##n4(z+n4*3);\
  473. pass(z,FFT_NAME(ff_cos_##n),n4/2);\
  474. }
  475. static void fft4(FFTComplex *z)
  476. {
  477. FFTDouble t1, t2, t3, t4, t5, t6, t7, t8;
  478. BF(t3, t1, z[0].re, z[1].re);
  479. BF(t8, t6, z[3].re, z[2].re);
  480. BF(z[2].re, z[0].re, t1, t6);
  481. BF(t4, t2, z[0].im, z[1].im);
  482. BF(t7, t5, z[2].im, z[3].im);
  483. BF(z[3].im, z[1].im, t4, t8);
  484. BF(z[3].re, z[1].re, t3, t7);
  485. BF(z[2].im, z[0].im, t2, t5);
  486. }
  487. static void fft8(FFTComplex *z)
  488. {
  489. FFTDouble t1, t2, t3, t4, t5, t6;
  490. fft4(z);
  491. BF(t1, z[5].re, z[4].re, -z[5].re);
  492. BF(t2, z[5].im, z[4].im, -z[5].im);
  493. BF(t5, z[7].re, z[6].re, -z[7].re);
  494. BF(t6, z[7].im, z[6].im, -z[7].im);
  495. BUTTERFLIES(z[0],z[2],z[4],z[6]);
  496. TRANSFORM(z[1],z[3],z[5],z[7],sqrthalf,sqrthalf);
  497. }
  498. #if !CONFIG_SMALL
  499. static void fft16(FFTComplex *z)
  500. {
  501. FFTDouble t1, t2, t3, t4, t5, t6;
  502. FFTSample cos_16_1 = FFT_NAME(ff_cos_16)[1];
  503. FFTSample cos_16_3 = FFT_NAME(ff_cos_16)[3];
  504. fft8(z);
  505. fft4(z+8);
  506. fft4(z+12);
  507. TRANSFORM_ZERO(z[0],z[4],z[8],z[12]);
  508. TRANSFORM(z[2],z[6],z[10],z[14],sqrthalf,sqrthalf);
  509. TRANSFORM(z[1],z[5],z[9],z[13],cos_16_1,cos_16_3);
  510. TRANSFORM(z[3],z[7],z[11],z[15],cos_16_3,cos_16_1);
  511. }
  512. #else
  513. DECL_FFT(16,8,4)
  514. #endif
  515. DECL_FFT(32,16,8)
  516. DECL_FFT(64,32,16)
  517. DECL_FFT(128,64,32)
  518. DECL_FFT(256,128,64)
  519. DECL_FFT(512,256,128)
  520. #if !CONFIG_SMALL
  521. #define pass pass_big
  522. #endif
  523. DECL_FFT(1024,512,256)
  524. DECL_FFT(2048,1024,512)
  525. DECL_FFT(4096,2048,1024)
  526. DECL_FFT(8192,4096,2048)
  527. DECL_FFT(16384,8192,4096)
  528. DECL_FFT(32768,16384,8192)
  529. DECL_FFT(65536,32768,16384)
  530. DECL_FFT(131072,65536,32768)
  531. static void (* const fft_dispatch[])(FFTComplex*) = {
  532. fft4, fft8, fft16, fft32, fft64, fft128, fft256, fft512, fft1024,
  533. fft2048, fft4096, fft8192, fft16384, fft32768, fft65536, fft131072
  534. };
  535. static void fft_calc_c(FFTContext *s, FFTComplex *z)
  536. {
  537. fft_dispatch[s->nbits-2](z);
  538. }
  539. #endif /* FFT_FIXED_32 */