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.

531 lines
15KB

  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 "fft.h"
  31. #include "fft-internal.h"
  32. #if CONFIG_FFT_FIXED_32
  33. #include "fft_table.h"
  34. #else /* CONFIG_FFT_FIXED_32 */
  35. /* cos(2*pi*x/n) for 0<=x<=n/4, followed by its reverse */
  36. #if !CONFIG_HARDCODED_TABLES
  37. COSTABLE(16);
  38. COSTABLE(32);
  39. COSTABLE(64);
  40. COSTABLE(128);
  41. COSTABLE(256);
  42. COSTABLE(512);
  43. COSTABLE(1024);
  44. COSTABLE(2048);
  45. COSTABLE(4096);
  46. COSTABLE(8192);
  47. COSTABLE(16384);
  48. COSTABLE(32768);
  49. COSTABLE(65536);
  50. #endif
  51. COSTABLE_CONST FFTSample * const FFT_NAME(ff_cos_tabs)[] = {
  52. NULL, NULL, NULL, NULL,
  53. FFT_NAME(ff_cos_16),
  54. FFT_NAME(ff_cos_32),
  55. FFT_NAME(ff_cos_64),
  56. FFT_NAME(ff_cos_128),
  57. FFT_NAME(ff_cos_256),
  58. FFT_NAME(ff_cos_512),
  59. FFT_NAME(ff_cos_1024),
  60. FFT_NAME(ff_cos_2048),
  61. FFT_NAME(ff_cos_4096),
  62. FFT_NAME(ff_cos_8192),
  63. FFT_NAME(ff_cos_16384),
  64. FFT_NAME(ff_cos_32768),
  65. FFT_NAME(ff_cos_65536),
  66. };
  67. #endif /* CONFIG_FFT_FIXED_32 */
  68. static void fft_permute_c(FFTContext *s, FFTComplex *z);
  69. static void fft_calc_c(FFTContext *s, FFTComplex *z);
  70. static int split_radix_permutation(int i, int n, int inverse)
  71. {
  72. int m;
  73. if(n <= 2) return i&1;
  74. m = n >> 1;
  75. if(!(i&m)) return split_radix_permutation(i, m, inverse)*2;
  76. m >>= 1;
  77. if(inverse == !(i&m)) return split_radix_permutation(i, m, inverse)*4 + 1;
  78. else return split_radix_permutation(i, m, inverse)*4 - 1;
  79. }
  80. av_cold void ff_init_ff_cos_tabs(int index)
  81. {
  82. #if (!CONFIG_HARDCODED_TABLES) && (!CONFIG_FFT_FIXED_32)
  83. int i;
  84. int m = 1<<index;
  85. double freq = 2*M_PI/m;
  86. FFTSample *tab = FFT_NAME(ff_cos_tabs)[index];
  87. for(i=0; i<=m/4; i++)
  88. tab[i] = FIX15(cos(i*freq));
  89. for(i=1; i<m/4; i++)
  90. tab[m/2-i] = tab[i];
  91. #endif
  92. }
  93. static const int avx_tab[] = {
  94. 0, 4, 1, 5, 8, 12, 9, 13, 2, 6, 3, 7, 10, 14, 11, 15
  95. };
  96. static int is_second_half_of_fft32(int i, int n)
  97. {
  98. if (n <= 32)
  99. return i >= 16;
  100. else if (i < n/2)
  101. return is_second_half_of_fft32(i, n/2);
  102. else if (i < 3*n/4)
  103. return is_second_half_of_fft32(i - n/2, n/4);
  104. else
  105. return is_second_half_of_fft32(i - 3*n/4, n/4);
  106. }
  107. static av_cold void fft_perm_avx(FFTContext *s)
  108. {
  109. int i;
  110. int n = 1 << s->nbits;
  111. for (i = 0; i < n; i += 16) {
  112. int k;
  113. if (is_second_half_of_fft32(i, n)) {
  114. for (k = 0; k < 16; k++)
  115. s->revtab[-split_radix_permutation(i + k, n, s->inverse) & (n - 1)] =
  116. i + avx_tab[k];
  117. } else {
  118. for (k = 0; k < 16; k++) {
  119. int j = i + k;
  120. j = (j & ~7) | ((j >> 1) & 3) | ((j << 2) & 4);
  121. s->revtab[-split_radix_permutation(i + k, n, s->inverse) & (n - 1)] = j;
  122. }
  123. }
  124. }
  125. }
  126. av_cold int ff_fft_init(FFTContext *s, int nbits, int inverse)
  127. {
  128. int i, j, n;
  129. if (nbits < 2 || nbits > 16)
  130. goto fail;
  131. s->nbits = nbits;
  132. n = 1 << nbits;
  133. s->revtab = av_malloc(n * sizeof(uint16_t));
  134. if (!s->revtab)
  135. goto fail;
  136. s->tmp_buf = av_malloc(n * sizeof(FFTComplex));
  137. if (!s->tmp_buf)
  138. goto fail;
  139. s->inverse = inverse;
  140. s->fft_permutation = FF_FFT_PERM_DEFAULT;
  141. s->fft_permute = fft_permute_c;
  142. s->fft_calc = fft_calc_c;
  143. #if CONFIG_MDCT
  144. s->imdct_calc = ff_imdct_calc_c;
  145. s->imdct_half = ff_imdct_half_c;
  146. s->mdct_calc = ff_mdct_calc_c;
  147. #endif
  148. #if CONFIG_FFT_FIXED_32
  149. {
  150. int n=0;
  151. ff_fft_lut_init(fft_offsets_lut, 0, 1 << 16, &n);
  152. }
  153. #else /* CONFIG_FFT_FIXED_32 */
  154. #if CONFIG_FFT_FLOAT
  155. if (ARCH_ARM) ff_fft_init_arm(s);
  156. if (ARCH_PPC) ff_fft_init_ppc(s);
  157. if (ARCH_X86) ff_fft_init_x86(s);
  158. if (CONFIG_MDCT) s->mdct_calcw = s->mdct_calc;
  159. if (HAVE_MIPSFPU) ff_fft_init_mips(s);
  160. #else
  161. if (CONFIG_MDCT) s->mdct_calcw = ff_mdct_calcw_c;
  162. if (ARCH_ARM) ff_fft_fixed_init_arm(s);
  163. #endif
  164. for(j=4; j<=nbits; j++) {
  165. ff_init_ff_cos_tabs(j);
  166. }
  167. #endif /* CONFIG_FFT_FIXED_32 */
  168. if (s->fft_permutation == FF_FFT_PERM_AVX) {
  169. fft_perm_avx(s);
  170. } else {
  171. for(i=0; i<n; i++) {
  172. int j = i;
  173. if (s->fft_permutation == FF_FFT_PERM_SWAP_LSBS)
  174. j = (j&~3) | ((j>>1)&1) | ((j<<1)&2);
  175. s->revtab[-split_radix_permutation(i, n, s->inverse) & (n-1)] = j;
  176. }
  177. }
  178. return 0;
  179. fail:
  180. av_freep(&s->revtab);
  181. av_freep(&s->tmp_buf);
  182. return -1;
  183. }
  184. static void fft_permute_c(FFTContext *s, FFTComplex *z)
  185. {
  186. int j, np;
  187. const uint16_t *revtab = s->revtab;
  188. np = 1 << s->nbits;
  189. /* TODO: handle split-radix permute in a more optimal way, probably in-place */
  190. for(j=0;j<np;j++) s->tmp_buf[revtab[j]] = z[j];
  191. memcpy(z, s->tmp_buf, np * sizeof(FFTComplex));
  192. }
  193. av_cold void ff_fft_end(FFTContext *s)
  194. {
  195. av_freep(&s->revtab);
  196. av_freep(&s->tmp_buf);
  197. }
  198. #if CONFIG_FFT_FIXED_32
  199. static void fft_calc_c(FFTContext *s, FFTComplex *z) {
  200. int nbits, i, n, num_transforms, offset, step;
  201. int n4, n2, n34;
  202. FFTSample tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
  203. FFTComplex *tmpz;
  204. FFTSample w_re, w_im;
  205. FFTSample *w_re_ptr, *w_im_ptr;
  206. const int fft_size = (1 << s->nbits);
  207. int64_t accu;
  208. num_transforms = (0x2aab >> (16 - s->nbits)) | 1;
  209. for (n=0; n<num_transforms; n++){
  210. offset = fft_offsets_lut[n] << 2;
  211. tmpz = z + offset;
  212. tmp1 = tmpz[0].re + tmpz[1].re;
  213. tmp5 = tmpz[2].re + tmpz[3].re;
  214. tmp2 = tmpz[0].im + tmpz[1].im;
  215. tmp6 = tmpz[2].im + tmpz[3].im;
  216. tmp3 = tmpz[0].re - tmpz[1].re;
  217. tmp8 = tmpz[2].im - tmpz[3].im;
  218. tmp4 = tmpz[0].im - tmpz[1].im;
  219. tmp7 = tmpz[2].re - tmpz[3].re;
  220. tmpz[0].re = tmp1 + tmp5;
  221. tmpz[2].re = tmp1 - tmp5;
  222. tmpz[0].im = tmp2 + tmp6;
  223. tmpz[2].im = tmp2 - tmp6;
  224. tmpz[1].re = tmp3 + tmp8;
  225. tmpz[3].re = tmp3 - tmp8;
  226. tmpz[1].im = tmp4 - tmp7;
  227. tmpz[3].im = tmp4 + tmp7;
  228. }
  229. if (fft_size < 8)
  230. return;
  231. num_transforms = (num_transforms >> 1) | 1;
  232. for (n=0; n<num_transforms; n++){
  233. offset = fft_offsets_lut[n] << 3;
  234. tmpz = z + offset;
  235. tmp1 = tmpz[4].re + tmpz[5].re;
  236. tmp3 = tmpz[6].re + tmpz[7].re;
  237. tmp2 = tmpz[4].im + tmpz[5].im;
  238. tmp4 = tmpz[6].im + tmpz[7].im;
  239. tmp5 = tmp1 + tmp3;
  240. tmp7 = tmp1 - tmp3;
  241. tmp6 = tmp2 + tmp4;
  242. tmp8 = tmp2 - tmp4;
  243. tmp1 = tmpz[4].re - tmpz[5].re;
  244. tmp2 = tmpz[4].im - tmpz[5].im;
  245. tmp3 = tmpz[6].re - tmpz[7].re;
  246. tmp4 = tmpz[6].im - tmpz[7].im;
  247. tmpz[4].re = tmpz[0].re - tmp5;
  248. tmpz[0].re = tmpz[0].re + tmp5;
  249. tmpz[4].im = tmpz[0].im - tmp6;
  250. tmpz[0].im = tmpz[0].im + tmp6;
  251. tmpz[6].re = tmpz[2].re - tmp8;
  252. tmpz[2].re = tmpz[2].re + tmp8;
  253. tmpz[6].im = tmpz[2].im + tmp7;
  254. tmpz[2].im = tmpz[2].im - tmp7;
  255. accu = (int64_t)Q31(M_SQRT1_2)*(tmp1 + tmp2);
  256. tmp5 = (int32_t)((accu + 0x40000000) >> 31);
  257. accu = (int64_t)Q31(M_SQRT1_2)*(tmp3 - tmp4);
  258. tmp7 = (int32_t)((accu + 0x40000000) >> 31);
  259. accu = (int64_t)Q31(M_SQRT1_2)*(tmp2 - tmp1);
  260. tmp6 = (int32_t)((accu + 0x40000000) >> 31);
  261. accu = (int64_t)Q31(M_SQRT1_2)*(tmp3 + tmp4);
  262. tmp8 = (int32_t)((accu + 0x40000000) >> 31);
  263. tmp1 = tmp5 + tmp7;
  264. tmp3 = tmp5 - tmp7;
  265. tmp2 = tmp6 + tmp8;
  266. tmp4 = tmp6 - tmp8;
  267. tmpz[5].re = tmpz[1].re - tmp1;
  268. tmpz[1].re = tmpz[1].re + tmp1;
  269. tmpz[5].im = tmpz[1].im - tmp2;
  270. tmpz[1].im = tmpz[1].im + tmp2;
  271. tmpz[7].re = tmpz[3].re - tmp4;
  272. tmpz[3].re = tmpz[3].re + tmp4;
  273. tmpz[7].im = tmpz[3].im + tmp3;
  274. tmpz[3].im = tmpz[3].im - tmp3;
  275. }
  276. step = 1 << ((MAX_LOG2_NFFT-4) - 4);
  277. n4 = 4;
  278. for (nbits=4; nbits<=s->nbits; nbits++){
  279. n2 = 2*n4;
  280. n34 = 3*n4;
  281. num_transforms = (num_transforms >> 1) | 1;
  282. for (n=0; n<num_transforms; n++){
  283. offset = fft_offsets_lut[n] << nbits;
  284. tmpz = z + offset;
  285. tmp5 = tmpz[ n2].re + tmpz[n34].re;
  286. tmp1 = tmpz[ n2].re - tmpz[n34].re;
  287. tmp6 = tmpz[ n2].im + tmpz[n34].im;
  288. tmp2 = tmpz[ n2].im - tmpz[n34].im;
  289. tmpz[ n2].re = tmpz[ 0].re - tmp5;
  290. tmpz[ 0].re = tmpz[ 0].re + tmp5;
  291. tmpz[ n2].im = tmpz[ 0].im - tmp6;
  292. tmpz[ 0].im = tmpz[ 0].im + tmp6;
  293. tmpz[n34].re = tmpz[n4].re - tmp2;
  294. tmpz[ n4].re = tmpz[n4].re + tmp2;
  295. tmpz[n34].im = tmpz[n4].im + tmp1;
  296. tmpz[ n4].im = tmpz[n4].im - tmp1;
  297. w_re_ptr = w_tab_sr + step;
  298. w_im_ptr = w_tab_sr + MAX_FFT_SIZE/(4*16) - step;
  299. for (i=1; i<n4; i++){
  300. w_re = w_re_ptr[0];
  301. w_im = w_im_ptr[0];
  302. accu = (int64_t)w_re*tmpz[ n2+i].re;
  303. accu += (int64_t)w_im*tmpz[ n2+i].im;
  304. tmp1 = (int32_t)((accu + 0x40000000) >> 31);
  305. accu = (int64_t)w_re*tmpz[ n2+i].im;
  306. accu -= (int64_t)w_im*tmpz[ n2+i].re;
  307. tmp2 = (int32_t)((accu + 0x40000000) >> 31);
  308. accu = (int64_t)w_re*tmpz[n34+i].re;
  309. accu -= (int64_t)w_im*tmpz[n34+i].im;
  310. tmp3 = (int32_t)((accu + 0x40000000) >> 31);
  311. accu = (int64_t)w_re*tmpz[n34+i].im;
  312. accu += (int64_t)w_im*tmpz[n34+i].re;
  313. tmp4 = (int32_t)((accu + 0x40000000) >> 31);
  314. tmp5 = tmp1 + tmp3;
  315. tmp1 = tmp1 - tmp3;
  316. tmp6 = tmp2 + tmp4;
  317. tmp2 = tmp2 - tmp4;
  318. tmpz[ n2+i].re = tmpz[ i].re - tmp5;
  319. tmpz[ i].re = tmpz[ i].re + tmp5;
  320. tmpz[ n2+i].im = tmpz[ i].im - tmp6;
  321. tmpz[ i].im = tmpz[ i].im + tmp6;
  322. tmpz[n34+i].re = tmpz[n4+i].re - tmp2;
  323. tmpz[ n4+i].re = tmpz[n4+i].re + tmp2;
  324. tmpz[n34+i].im = tmpz[n4+i].im + tmp1;
  325. tmpz[ n4+i].im = tmpz[n4+i].im - tmp1;
  326. w_re_ptr += step;
  327. w_im_ptr -= step;
  328. }
  329. }
  330. step >>= 1;
  331. n4 <<= 1;
  332. }
  333. }
  334. #else /* CONFIG_FFT_FIXED_32 */
  335. #define BUTTERFLIES(a0,a1,a2,a3) {\
  336. BF(t3, t5, t5, t1);\
  337. BF(a2.re, a0.re, a0.re, t5);\
  338. BF(a3.im, a1.im, a1.im, t3);\
  339. BF(t4, t6, t2, t6);\
  340. BF(a3.re, a1.re, a1.re, t4);\
  341. BF(a2.im, a0.im, a0.im, t6);\
  342. }
  343. // force loading all the inputs before storing any.
  344. // this is slightly slower for small data, but avoids store->load aliasing
  345. // for addresses separated by large powers of 2.
  346. #define BUTTERFLIES_BIG(a0,a1,a2,a3) {\
  347. FFTSample r0=a0.re, i0=a0.im, r1=a1.re, i1=a1.im;\
  348. BF(t3, t5, t5, t1);\
  349. BF(a2.re, a0.re, r0, t5);\
  350. BF(a3.im, a1.im, i1, t3);\
  351. BF(t4, t6, t2, t6);\
  352. BF(a3.re, a1.re, r1, t4);\
  353. BF(a2.im, a0.im, i0, t6);\
  354. }
  355. #define TRANSFORM(a0,a1,a2,a3,wre,wim) {\
  356. CMUL(t1, t2, a2.re, a2.im, wre, -wim);\
  357. CMUL(t5, t6, a3.re, a3.im, wre, wim);\
  358. BUTTERFLIES(a0,a1,a2,a3)\
  359. }
  360. #define TRANSFORM_ZERO(a0,a1,a2,a3) {\
  361. t1 = a2.re;\
  362. t2 = a2.im;\
  363. t5 = a3.re;\
  364. t6 = a3.im;\
  365. BUTTERFLIES(a0,a1,a2,a3)\
  366. }
  367. /* z[0...8n-1], w[1...2n-1] */
  368. #define PASS(name)\
  369. static void name(FFTComplex *z, const FFTSample *wre, unsigned int n)\
  370. {\
  371. FFTDouble t1, t2, t3, t4, t5, t6;\
  372. int o1 = 2*n;\
  373. int o2 = 4*n;\
  374. int o3 = 6*n;\
  375. const FFTSample *wim = wre+o1;\
  376. n--;\
  377. \
  378. TRANSFORM_ZERO(z[0],z[o1],z[o2],z[o3]);\
  379. TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
  380. do {\
  381. z += 2;\
  382. wre += 2;\
  383. wim -= 2;\
  384. TRANSFORM(z[0],z[o1],z[o2],z[o3],wre[0],wim[0]);\
  385. TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
  386. } while(--n);\
  387. }
  388. PASS(pass)
  389. #undef BUTTERFLIES
  390. #define BUTTERFLIES BUTTERFLIES_BIG
  391. PASS(pass_big)
  392. #define DECL_FFT(n,n2,n4)\
  393. static void fft##n(FFTComplex *z)\
  394. {\
  395. fft##n2(z);\
  396. fft##n4(z+n4*2);\
  397. fft##n4(z+n4*3);\
  398. pass(z,FFT_NAME(ff_cos_##n),n4/2);\
  399. }
  400. static void fft4(FFTComplex *z)
  401. {
  402. FFTDouble t1, t2, t3, t4, t5, t6, t7, t8;
  403. BF(t3, t1, z[0].re, z[1].re);
  404. BF(t8, t6, z[3].re, z[2].re);
  405. BF(z[2].re, z[0].re, t1, t6);
  406. BF(t4, t2, z[0].im, z[1].im);
  407. BF(t7, t5, z[2].im, z[3].im);
  408. BF(z[3].im, z[1].im, t4, t8);
  409. BF(z[3].re, z[1].re, t3, t7);
  410. BF(z[2].im, z[0].im, t2, t5);
  411. }
  412. static void fft8(FFTComplex *z)
  413. {
  414. FFTDouble t1, t2, t3, t4, t5, t6;
  415. fft4(z);
  416. BF(t1, z[5].re, z[4].re, -z[5].re);
  417. BF(t2, z[5].im, z[4].im, -z[5].im);
  418. BF(t5, z[7].re, z[6].re, -z[7].re);
  419. BF(t6, z[7].im, z[6].im, -z[7].im);
  420. BUTTERFLIES(z[0],z[2],z[4],z[6]);
  421. TRANSFORM(z[1],z[3],z[5],z[7],sqrthalf,sqrthalf);
  422. }
  423. #if !CONFIG_SMALL
  424. static void fft16(FFTComplex *z)
  425. {
  426. FFTDouble t1, t2, t3, t4, t5, t6;
  427. FFTSample cos_16_1 = FFT_NAME(ff_cos_16)[1];
  428. FFTSample cos_16_3 = FFT_NAME(ff_cos_16)[3];
  429. fft8(z);
  430. fft4(z+8);
  431. fft4(z+12);
  432. TRANSFORM_ZERO(z[0],z[4],z[8],z[12]);
  433. TRANSFORM(z[2],z[6],z[10],z[14],sqrthalf,sqrthalf);
  434. TRANSFORM(z[1],z[5],z[9],z[13],cos_16_1,cos_16_3);
  435. TRANSFORM(z[3],z[7],z[11],z[15],cos_16_3,cos_16_1);
  436. }
  437. #else
  438. DECL_FFT(16,8,4)
  439. #endif
  440. DECL_FFT(32,16,8)
  441. DECL_FFT(64,32,16)
  442. DECL_FFT(128,64,32)
  443. DECL_FFT(256,128,64)
  444. DECL_FFT(512,256,128)
  445. #if !CONFIG_SMALL
  446. #define pass pass_big
  447. #endif
  448. DECL_FFT(1024,512,256)
  449. DECL_FFT(2048,1024,512)
  450. DECL_FFT(4096,2048,1024)
  451. DECL_FFT(8192,4096,2048)
  452. DECL_FFT(16384,8192,4096)
  453. DECL_FFT(32768,16384,8192)
  454. DECL_FFT(65536,32768,16384)
  455. static void (* const fft_dispatch[])(FFTComplex*) = {
  456. fft4, fft8, fft16, fft32, fft64, fft128, fft256, fft512, fft1024,
  457. fft2048, fft4096, fft8192, fft16384, fft32768, fft65536,
  458. };
  459. static void fft_calc_c(FFTContext *s, FFTComplex *z)
  460. {
  461. fft_dispatch[s->nbits-2](z);
  462. }
  463. #endif /* CONFIG_FFT_FIXED_32 */