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.

532 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 FFT_FIXED_32
  33. #include "fft_table.h"
  34. #else /* 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 /* 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) && (!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 FFT_FIXED_32
  149. {
  150. int n=0;
  151. ff_fft_lut_init(fft_offsets_lut, 0, 1 << 16, &n);
  152. }
  153. #else /* FFT_FIXED_32 */
  154. #if FFT_FLOAT
  155. if (ARCH_AARCH64) ff_fft_init_aarch64(s);
  156. if (ARCH_ARM) ff_fft_init_arm(s);
  157. if (ARCH_PPC) ff_fft_init_ppc(s);
  158. if (ARCH_X86) ff_fft_init_x86(s);
  159. if (CONFIG_MDCT) s->mdct_calcw = s->mdct_calc;
  160. if (HAVE_MIPSFPU) ff_fft_init_mips(s);
  161. #else
  162. if (CONFIG_MDCT) s->mdct_calcw = ff_mdct_calcw_c;
  163. if (ARCH_ARM) ff_fft_fixed_init_arm(s);
  164. #endif
  165. for(j=4; j<=nbits; j++) {
  166. ff_init_ff_cos_tabs(j);
  167. }
  168. #endif /* FFT_FIXED_32 */
  169. if (s->fft_permutation == FF_FFT_PERM_AVX) {
  170. fft_perm_avx(s);
  171. } else {
  172. for(i=0; i<n; i++) {
  173. j = i;
  174. if (s->fft_permutation == FF_FFT_PERM_SWAP_LSBS)
  175. j = (j&~3) | ((j>>1)&1) | ((j<<1)&2);
  176. s->revtab[-split_radix_permutation(i, n, s->inverse) & (n-1)] = j;
  177. }
  178. }
  179. return 0;
  180. fail:
  181. av_freep(&s->revtab);
  182. av_freep(&s->tmp_buf);
  183. return -1;
  184. }
  185. static void fft_permute_c(FFTContext *s, FFTComplex *z)
  186. {
  187. int j, np;
  188. const uint16_t *revtab = s->revtab;
  189. np = 1 << s->nbits;
  190. /* TODO: handle split-radix permute in a more optimal way, probably in-place */
  191. for(j=0;j<np;j++) s->tmp_buf[revtab[j]] = z[j];
  192. memcpy(z, s->tmp_buf, np * sizeof(FFTComplex));
  193. }
  194. av_cold void ff_fft_end(FFTContext *s)
  195. {
  196. av_freep(&s->revtab);
  197. av_freep(&s->tmp_buf);
  198. }
  199. #if FFT_FIXED_32
  200. static void fft_calc_c(FFTContext *s, FFTComplex *z) {
  201. int nbits, i, n, num_transforms, offset, step;
  202. int n4, n2, n34;
  203. FFTSample tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
  204. FFTComplex *tmpz;
  205. FFTSample w_re, w_im;
  206. const FFTSample *w_re_ptr, *w_im_ptr;
  207. const int fft_size = (1 << s->nbits);
  208. int64_t accu;
  209. num_transforms = (0x2aab >> (16 - s->nbits)) | 1;
  210. for (n=0; n<num_transforms; n++){
  211. offset = fft_offsets_lut[n] << 2;
  212. tmpz = z + offset;
  213. tmp1 = tmpz[0].re + tmpz[1].re;
  214. tmp5 = tmpz[2].re + tmpz[3].re;
  215. tmp2 = tmpz[0].im + tmpz[1].im;
  216. tmp6 = tmpz[2].im + tmpz[3].im;
  217. tmp3 = tmpz[0].re - tmpz[1].re;
  218. tmp8 = tmpz[2].im - tmpz[3].im;
  219. tmp4 = tmpz[0].im - tmpz[1].im;
  220. tmp7 = tmpz[2].re - tmpz[3].re;
  221. tmpz[0].re = tmp1 + tmp5;
  222. tmpz[2].re = tmp1 - tmp5;
  223. tmpz[0].im = tmp2 + tmp6;
  224. tmpz[2].im = tmp2 - tmp6;
  225. tmpz[1].re = tmp3 + tmp8;
  226. tmpz[3].re = tmp3 - tmp8;
  227. tmpz[1].im = tmp4 - tmp7;
  228. tmpz[3].im = tmp4 + tmp7;
  229. }
  230. if (fft_size < 8)
  231. return;
  232. num_transforms = (num_transforms >> 1) | 1;
  233. for (n=0; n<num_transforms; n++){
  234. offset = fft_offsets_lut[n] << 3;
  235. tmpz = z + offset;
  236. tmp1 = tmpz[4].re + tmpz[5].re;
  237. tmp3 = tmpz[6].re + tmpz[7].re;
  238. tmp2 = tmpz[4].im + tmpz[5].im;
  239. tmp4 = tmpz[6].im + tmpz[7].im;
  240. tmp5 = tmp1 + tmp3;
  241. tmp7 = tmp1 - tmp3;
  242. tmp6 = tmp2 + tmp4;
  243. tmp8 = tmp2 - tmp4;
  244. tmp1 = tmpz[4].re - tmpz[5].re;
  245. tmp2 = tmpz[4].im - tmpz[5].im;
  246. tmp3 = tmpz[6].re - tmpz[7].re;
  247. tmp4 = tmpz[6].im - tmpz[7].im;
  248. tmpz[4].re = tmpz[0].re - tmp5;
  249. tmpz[0].re = tmpz[0].re + tmp5;
  250. tmpz[4].im = tmpz[0].im - tmp6;
  251. tmpz[0].im = tmpz[0].im + tmp6;
  252. tmpz[6].re = tmpz[2].re - tmp8;
  253. tmpz[2].re = tmpz[2].re + tmp8;
  254. tmpz[6].im = tmpz[2].im + tmp7;
  255. tmpz[2].im = tmpz[2].im - tmp7;
  256. accu = (int64_t)Q31(M_SQRT1_2)*(tmp1 + tmp2);
  257. tmp5 = (int32_t)((accu + 0x40000000) >> 31);
  258. accu = (int64_t)Q31(M_SQRT1_2)*(tmp3 - tmp4);
  259. tmp7 = (int32_t)((accu + 0x40000000) >> 31);
  260. accu = (int64_t)Q31(M_SQRT1_2)*(tmp2 - tmp1);
  261. tmp6 = (int32_t)((accu + 0x40000000) >> 31);
  262. accu = (int64_t)Q31(M_SQRT1_2)*(tmp3 + tmp4);
  263. tmp8 = (int32_t)((accu + 0x40000000) >> 31);
  264. tmp1 = tmp5 + tmp7;
  265. tmp3 = tmp5 - tmp7;
  266. tmp2 = tmp6 + tmp8;
  267. tmp4 = tmp6 - tmp8;
  268. tmpz[5].re = tmpz[1].re - tmp1;
  269. tmpz[1].re = tmpz[1].re + tmp1;
  270. tmpz[5].im = tmpz[1].im - tmp2;
  271. tmpz[1].im = tmpz[1].im + tmp2;
  272. tmpz[7].re = tmpz[3].re - tmp4;
  273. tmpz[3].re = tmpz[3].re + tmp4;
  274. tmpz[7].im = tmpz[3].im + tmp3;
  275. tmpz[3].im = tmpz[3].im - tmp3;
  276. }
  277. step = 1 << ((MAX_LOG2_NFFT-4) - 4);
  278. n4 = 4;
  279. for (nbits=4; nbits<=s->nbits; nbits++){
  280. n2 = 2*n4;
  281. n34 = 3*n4;
  282. num_transforms = (num_transforms >> 1) | 1;
  283. for (n=0; n<num_transforms; n++){
  284. offset = fft_offsets_lut[n] << nbits;
  285. tmpz = z + offset;
  286. tmp5 = tmpz[ n2].re + tmpz[n34].re;
  287. tmp1 = tmpz[ n2].re - tmpz[n34].re;
  288. tmp6 = tmpz[ n2].im + tmpz[n34].im;
  289. tmp2 = tmpz[ n2].im - tmpz[n34].im;
  290. tmpz[ n2].re = tmpz[ 0].re - tmp5;
  291. tmpz[ 0].re = tmpz[ 0].re + tmp5;
  292. tmpz[ n2].im = tmpz[ 0].im - tmp6;
  293. tmpz[ 0].im = tmpz[ 0].im + tmp6;
  294. tmpz[n34].re = tmpz[n4].re - tmp2;
  295. tmpz[ n4].re = tmpz[n4].re + tmp2;
  296. tmpz[n34].im = tmpz[n4].im + tmp1;
  297. tmpz[ n4].im = tmpz[n4].im - tmp1;
  298. w_re_ptr = w_tab_sr + step;
  299. w_im_ptr = w_tab_sr + MAX_FFT_SIZE/(4*16) - step;
  300. for (i=1; i<n4; i++){
  301. w_re = w_re_ptr[0];
  302. w_im = w_im_ptr[0];
  303. accu = (int64_t)w_re*tmpz[ n2+i].re;
  304. accu += (int64_t)w_im*tmpz[ n2+i].im;
  305. tmp1 = (int32_t)((accu + 0x40000000) >> 31);
  306. accu = (int64_t)w_re*tmpz[ n2+i].im;
  307. accu -= (int64_t)w_im*tmpz[ n2+i].re;
  308. tmp2 = (int32_t)((accu + 0x40000000) >> 31);
  309. accu = (int64_t)w_re*tmpz[n34+i].re;
  310. accu -= (int64_t)w_im*tmpz[n34+i].im;
  311. tmp3 = (int32_t)((accu + 0x40000000) >> 31);
  312. accu = (int64_t)w_re*tmpz[n34+i].im;
  313. accu += (int64_t)w_im*tmpz[n34+i].re;
  314. tmp4 = (int32_t)((accu + 0x40000000) >> 31);
  315. tmp5 = tmp1 + tmp3;
  316. tmp1 = tmp1 - tmp3;
  317. tmp6 = tmp2 + tmp4;
  318. tmp2 = tmp2 - tmp4;
  319. tmpz[ n2+i].re = tmpz[ i].re - tmp5;
  320. tmpz[ i].re = tmpz[ i].re + tmp5;
  321. tmpz[ n2+i].im = tmpz[ i].im - tmp6;
  322. tmpz[ i].im = tmpz[ i].im + tmp6;
  323. tmpz[n34+i].re = tmpz[n4+i].re - tmp2;
  324. tmpz[ n4+i].re = tmpz[n4+i].re + tmp2;
  325. tmpz[n34+i].im = tmpz[n4+i].im + tmp1;
  326. tmpz[ n4+i].im = tmpz[n4+i].im - tmp1;
  327. w_re_ptr += step;
  328. w_im_ptr -= step;
  329. }
  330. }
  331. step >>= 1;
  332. n4 <<= 1;
  333. }
  334. }
  335. #else /* FFT_FIXED_32 */
  336. #define BUTTERFLIES(a0,a1,a2,a3) {\
  337. BF(t3, t5, t5, t1);\
  338. BF(a2.re, a0.re, a0.re, t5);\
  339. BF(a3.im, a1.im, a1.im, t3);\
  340. BF(t4, t6, t2, t6);\
  341. BF(a3.re, a1.re, a1.re, t4);\
  342. BF(a2.im, a0.im, a0.im, t6);\
  343. }
  344. // force loading all the inputs before storing any.
  345. // this is slightly slower for small data, but avoids store->load aliasing
  346. // for addresses separated by large powers of 2.
  347. #define BUTTERFLIES_BIG(a0,a1,a2,a3) {\
  348. FFTSample r0=a0.re, i0=a0.im, r1=a1.re, i1=a1.im;\
  349. BF(t3, t5, t5, t1);\
  350. BF(a2.re, a0.re, r0, t5);\
  351. BF(a3.im, a1.im, i1, t3);\
  352. BF(t4, t6, t2, t6);\
  353. BF(a3.re, a1.re, r1, t4);\
  354. BF(a2.im, a0.im, i0, t6);\
  355. }
  356. #define TRANSFORM(a0,a1,a2,a3,wre,wim) {\
  357. CMUL(t1, t2, a2.re, a2.im, wre, -wim);\
  358. CMUL(t5, t6, a3.re, a3.im, wre, wim);\
  359. BUTTERFLIES(a0,a1,a2,a3)\
  360. }
  361. #define TRANSFORM_ZERO(a0,a1,a2,a3) {\
  362. t1 = a2.re;\
  363. t2 = a2.im;\
  364. t5 = a3.re;\
  365. t6 = a3.im;\
  366. BUTTERFLIES(a0,a1,a2,a3)\
  367. }
  368. /* z[0...8n-1], w[1...2n-1] */
  369. #define PASS(name)\
  370. static void name(FFTComplex *z, const FFTSample *wre, unsigned int n)\
  371. {\
  372. FFTDouble t1, t2, t3, t4, t5, t6;\
  373. int o1 = 2*n;\
  374. int o2 = 4*n;\
  375. int o3 = 6*n;\
  376. const FFTSample *wim = wre+o1;\
  377. n--;\
  378. \
  379. TRANSFORM_ZERO(z[0],z[o1],z[o2],z[o3]);\
  380. TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
  381. do {\
  382. z += 2;\
  383. wre += 2;\
  384. wim -= 2;\
  385. TRANSFORM(z[0],z[o1],z[o2],z[o3],wre[0],wim[0]);\
  386. TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
  387. } while(--n);\
  388. }
  389. PASS(pass)
  390. #undef BUTTERFLIES
  391. #define BUTTERFLIES BUTTERFLIES_BIG
  392. PASS(pass_big)
  393. #define DECL_FFT(n,n2,n4)\
  394. static void fft##n(FFTComplex *z)\
  395. {\
  396. fft##n2(z);\
  397. fft##n4(z+n4*2);\
  398. fft##n4(z+n4*3);\
  399. pass(z,FFT_NAME(ff_cos_##n),n4/2);\
  400. }
  401. static void fft4(FFTComplex *z)
  402. {
  403. FFTDouble t1, t2, t3, t4, t5, t6, t7, t8;
  404. BF(t3, t1, z[0].re, z[1].re);
  405. BF(t8, t6, z[3].re, z[2].re);
  406. BF(z[2].re, z[0].re, t1, t6);
  407. BF(t4, t2, z[0].im, z[1].im);
  408. BF(t7, t5, z[2].im, z[3].im);
  409. BF(z[3].im, z[1].im, t4, t8);
  410. BF(z[3].re, z[1].re, t3, t7);
  411. BF(z[2].im, z[0].im, t2, t5);
  412. }
  413. static void fft8(FFTComplex *z)
  414. {
  415. FFTDouble t1, t2, t3, t4, t5, t6;
  416. fft4(z);
  417. BF(t1, z[5].re, z[4].re, -z[5].re);
  418. BF(t2, z[5].im, z[4].im, -z[5].im);
  419. BF(t5, z[7].re, z[6].re, -z[7].re);
  420. BF(t6, z[7].im, z[6].im, -z[7].im);
  421. BUTTERFLIES(z[0],z[2],z[4],z[6]);
  422. TRANSFORM(z[1],z[3],z[5],z[7],sqrthalf,sqrthalf);
  423. }
  424. #if !CONFIG_SMALL
  425. static void fft16(FFTComplex *z)
  426. {
  427. FFTDouble t1, t2, t3, t4, t5, t6;
  428. FFTSample cos_16_1 = FFT_NAME(ff_cos_16)[1];
  429. FFTSample cos_16_3 = FFT_NAME(ff_cos_16)[3];
  430. fft8(z);
  431. fft4(z+8);
  432. fft4(z+12);
  433. TRANSFORM_ZERO(z[0],z[4],z[8],z[12]);
  434. TRANSFORM(z[2],z[6],z[10],z[14],sqrthalf,sqrthalf);
  435. TRANSFORM(z[1],z[5],z[9],z[13],cos_16_1,cos_16_3);
  436. TRANSFORM(z[3],z[7],z[11],z[15],cos_16_3,cos_16_1);
  437. }
  438. #else
  439. DECL_FFT(16,8,4)
  440. #endif
  441. DECL_FFT(32,16,8)
  442. DECL_FFT(64,32,16)
  443. DECL_FFT(128,64,32)
  444. DECL_FFT(256,128,64)
  445. DECL_FFT(512,256,128)
  446. #if !CONFIG_SMALL
  447. #define pass pass_big
  448. #endif
  449. DECL_FFT(1024,512,256)
  450. DECL_FFT(2048,1024,512)
  451. DECL_FFT(4096,2048,1024)
  452. DECL_FFT(8192,4096,2048)
  453. DECL_FFT(16384,8192,4096)
  454. DECL_FFT(32768,16384,8192)
  455. DECL_FFT(65536,32768,16384)
  456. static void (* const fft_dispatch[])(FFTComplex*) = {
  457. fft4, fft8, fft16, fft32, fft64, fft128, fft256, fft512, fft1024,
  458. fft2048, fft4096, fft8192, fft16384, fft32768, fft65536,
  459. };
  460. static void fft_calc_c(FFTContext *s, FFTComplex *z)
  461. {
  462. fft_dispatch[s->nbits-2](z);
  463. }
  464. #endif /* FFT_FIXED_32 */