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.

758 lines
29KB

  1. /*
  2. * Copyright (c) 2019 Lynne <dev@lynne.ee>
  3. * Power of two FFT:
  4. * Copyright (c) 2008 Loren Merritt
  5. * Copyright (c) 2002 Fabrice Bellard
  6. * Partly based on libdjbfft by D. J. Bernstein
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /* All costabs for a type are defined here */
  25. COSTABLE(16);
  26. COSTABLE(32);
  27. COSTABLE(64);
  28. COSTABLE(128);
  29. COSTABLE(256);
  30. COSTABLE(512);
  31. COSTABLE(1024);
  32. COSTABLE(2048);
  33. COSTABLE(4096);
  34. COSTABLE(8192);
  35. COSTABLE(16384);
  36. COSTABLE(32768);
  37. COSTABLE(65536);
  38. COSTABLE(131072);
  39. DECLARE_ALIGNED(32, FFTComplex, TX_NAME(ff_cos_53))[4];
  40. static FFTSample * const cos_tabs[18] = {
  41. NULL,
  42. NULL,
  43. NULL,
  44. NULL,
  45. TX_NAME(ff_cos_16),
  46. TX_NAME(ff_cos_32),
  47. TX_NAME(ff_cos_64),
  48. TX_NAME(ff_cos_128),
  49. TX_NAME(ff_cos_256),
  50. TX_NAME(ff_cos_512),
  51. TX_NAME(ff_cos_1024),
  52. TX_NAME(ff_cos_2048),
  53. TX_NAME(ff_cos_4096),
  54. TX_NAME(ff_cos_8192),
  55. TX_NAME(ff_cos_16384),
  56. TX_NAME(ff_cos_32768),
  57. TX_NAME(ff_cos_65536),
  58. TX_NAME(ff_cos_131072),
  59. };
  60. static av_always_inline void init_cos_tabs_idx(int index)
  61. {
  62. int m = 1 << index;
  63. double freq = 2*M_PI/m;
  64. FFTSample *tab = cos_tabs[index];
  65. for(int i = 0; i <= m/4; i++)
  66. tab[i] = RESCALE(cos(i*freq));
  67. for(int i = 1; i < m/4; i++)
  68. tab[m/2 - i] = tab[i];
  69. }
  70. #define INIT_FF_COS_TABS_FUNC(index, size) \
  71. static av_cold void init_cos_tabs_ ## size (void) \
  72. { \
  73. init_cos_tabs_idx(index); \
  74. }
  75. INIT_FF_COS_TABS_FUNC(4, 16)
  76. INIT_FF_COS_TABS_FUNC(5, 32)
  77. INIT_FF_COS_TABS_FUNC(6, 64)
  78. INIT_FF_COS_TABS_FUNC(7, 128)
  79. INIT_FF_COS_TABS_FUNC(8, 256)
  80. INIT_FF_COS_TABS_FUNC(9, 512)
  81. INIT_FF_COS_TABS_FUNC(10, 1024)
  82. INIT_FF_COS_TABS_FUNC(11, 2048)
  83. INIT_FF_COS_TABS_FUNC(12, 4096)
  84. INIT_FF_COS_TABS_FUNC(13, 8192)
  85. INIT_FF_COS_TABS_FUNC(14, 16384)
  86. INIT_FF_COS_TABS_FUNC(15, 32768)
  87. INIT_FF_COS_TABS_FUNC(16, 65536)
  88. INIT_FF_COS_TABS_FUNC(17, 131072)
  89. static av_cold void ff_init_53_tabs(void)
  90. {
  91. TX_NAME(ff_cos_53)[0] = (FFTComplex){ RESCALE(cos(2 * M_PI / 12)), RESCALE(cos(2 * M_PI / 12)) };
  92. TX_NAME(ff_cos_53)[1] = (FFTComplex){ RESCALE(cos(2 * M_PI / 6)), RESCALE(cos(2 * M_PI / 6)) };
  93. TX_NAME(ff_cos_53)[2] = (FFTComplex){ RESCALE(cos(2 * M_PI / 5)), RESCALE(sin(2 * M_PI / 5)) };
  94. TX_NAME(ff_cos_53)[3] = (FFTComplex){ RESCALE(cos(2 * M_PI / 10)), RESCALE(sin(2 * M_PI / 10)) };
  95. }
  96. static CosTabsInitOnce cos_tabs_init_once[] = {
  97. { ff_init_53_tabs, AV_ONCE_INIT },
  98. { NULL },
  99. { NULL },
  100. { NULL },
  101. { init_cos_tabs_16, AV_ONCE_INIT },
  102. { init_cos_tabs_32, AV_ONCE_INIT },
  103. { init_cos_tabs_64, AV_ONCE_INIT },
  104. { init_cos_tabs_128, AV_ONCE_INIT },
  105. { init_cos_tabs_256, AV_ONCE_INIT },
  106. { init_cos_tabs_512, AV_ONCE_INIT },
  107. { init_cos_tabs_1024, AV_ONCE_INIT },
  108. { init_cos_tabs_2048, AV_ONCE_INIT },
  109. { init_cos_tabs_4096, AV_ONCE_INIT },
  110. { init_cos_tabs_8192, AV_ONCE_INIT },
  111. { init_cos_tabs_16384, AV_ONCE_INIT },
  112. { init_cos_tabs_32768, AV_ONCE_INIT },
  113. { init_cos_tabs_65536, AV_ONCE_INIT },
  114. { init_cos_tabs_131072, AV_ONCE_INIT },
  115. };
  116. static av_cold void init_cos_tabs(int index)
  117. {
  118. ff_thread_once(&cos_tabs_init_once[index].control,
  119. cos_tabs_init_once[index].func);
  120. }
  121. static av_always_inline void fft3(FFTComplex *out, FFTComplex *in,
  122. ptrdiff_t stride)
  123. {
  124. FFTComplex tmp[2];
  125. #ifdef TX_INT32
  126. int64_t mtmp[4];
  127. #endif
  128. BF(tmp[0].re, tmp[1].im, in[1].im, in[2].im);
  129. BF(tmp[0].im, tmp[1].re, in[1].re, in[2].re);
  130. out[0*stride].re = in[0].re + tmp[1].re;
  131. out[0*stride].im = in[0].im + tmp[1].im;
  132. #ifdef TX_INT32
  133. mtmp[0] = (int64_t)TX_NAME(ff_cos_53)[0].re * tmp[0].re;
  134. mtmp[1] = (int64_t)TX_NAME(ff_cos_53)[0].im * tmp[0].im;
  135. mtmp[2] = (int64_t)TX_NAME(ff_cos_53)[1].re * tmp[1].re;
  136. mtmp[3] = (int64_t)TX_NAME(ff_cos_53)[1].re * tmp[1].im;
  137. out[1*stride].re = in[0].re - (mtmp[2] + mtmp[0] + 0x40000000 >> 31);
  138. out[1*stride].im = in[0].im - (mtmp[3] - mtmp[1] + 0x40000000 >> 31);
  139. out[2*stride].re = in[0].re - (mtmp[2] - mtmp[0] + 0x40000000 >> 31);
  140. out[2*stride].im = in[0].im - (mtmp[3] + mtmp[1] + 0x40000000 >> 31);
  141. #else
  142. tmp[0].re = TX_NAME(ff_cos_53)[0].re * tmp[0].re;
  143. tmp[0].im = TX_NAME(ff_cos_53)[0].im * tmp[0].im;
  144. tmp[1].re = TX_NAME(ff_cos_53)[1].re * tmp[1].re;
  145. tmp[1].im = TX_NAME(ff_cos_53)[1].re * tmp[1].im;
  146. out[1*stride].re = in[0].re - tmp[1].re + tmp[0].re;
  147. out[1*stride].im = in[0].im - tmp[1].im - tmp[0].im;
  148. out[2*stride].re = in[0].re - tmp[1].re - tmp[0].re;
  149. out[2*stride].im = in[0].im - tmp[1].im + tmp[0].im;
  150. #endif
  151. }
  152. #define DECL_FFT5(NAME, D0, D1, D2, D3, D4) \
  153. static av_always_inline void NAME(FFTComplex *out, FFTComplex *in, \
  154. ptrdiff_t stride) \
  155. { \
  156. FFTComplex z0[4], t[6]; \
  157. \
  158. BF(t[1].im, t[0].re, in[1].re, in[4].re); \
  159. BF(t[1].re, t[0].im, in[1].im, in[4].im); \
  160. BF(t[3].im, t[2].re, in[2].re, in[3].re); \
  161. BF(t[3].re, t[2].im, in[2].im, in[3].im); \
  162. \
  163. out[D0*stride].re = in[0].re + t[0].re + t[2].re; \
  164. out[D0*stride].im = in[0].im + t[0].im + t[2].im; \
  165. \
  166. SMUL(t[4].re, t[0].re, TX_NAME(ff_cos_53)[2].re, TX_NAME(ff_cos_53)[3].re, t[2].re, t[0].re); \
  167. SMUL(t[4].im, t[0].im, TX_NAME(ff_cos_53)[2].re, TX_NAME(ff_cos_53)[3].re, t[2].im, t[0].im); \
  168. CMUL(t[5].re, t[1].re, TX_NAME(ff_cos_53)[2].im, TX_NAME(ff_cos_53)[3].im, t[3].re, t[1].re); \
  169. CMUL(t[5].im, t[1].im, TX_NAME(ff_cos_53)[2].im, TX_NAME(ff_cos_53)[3].im, t[3].im, t[1].im); \
  170. \
  171. BF(z0[0].re, z0[3].re, t[0].re, t[1].re); \
  172. BF(z0[0].im, z0[3].im, t[0].im, t[1].im); \
  173. BF(z0[2].re, z0[1].re, t[4].re, t[5].re); \
  174. BF(z0[2].im, z0[1].im, t[4].im, t[5].im); \
  175. \
  176. out[D1*stride].re = in[0].re + z0[3].re; \
  177. out[D1*stride].im = in[0].im + z0[0].im; \
  178. out[D2*stride].re = in[0].re + z0[2].re; \
  179. out[D2*stride].im = in[0].im + z0[1].im; \
  180. out[D3*stride].re = in[0].re + z0[1].re; \
  181. out[D3*stride].im = in[0].im + z0[2].im; \
  182. out[D4*stride].re = in[0].re + z0[0].re; \
  183. out[D4*stride].im = in[0].im + z0[3].im; \
  184. }
  185. DECL_FFT5(fft5, 0, 1, 2, 3, 4)
  186. DECL_FFT5(fft5_m1, 0, 6, 12, 3, 9)
  187. DECL_FFT5(fft5_m2, 10, 1, 7, 13, 4)
  188. DECL_FFT5(fft5_m3, 5, 11, 2, 8, 14)
  189. static av_always_inline void fft15(FFTComplex *out, FFTComplex *in,
  190. ptrdiff_t stride)
  191. {
  192. FFTComplex tmp[15];
  193. for (int i = 0; i < 5; i++)
  194. fft3(tmp + i, in + i*3, 5);
  195. fft5_m1(out, tmp + 0, stride);
  196. fft5_m2(out, tmp + 5, stride);
  197. fft5_m3(out, tmp + 10, stride);
  198. }
  199. #define BUTTERFLIES(a0,a1,a2,a3) {\
  200. BF(t3, t5, t5, t1);\
  201. BF(a2.re, a0.re, a0.re, t5);\
  202. BF(a3.im, a1.im, a1.im, t3);\
  203. BF(t4, t6, t2, t6);\
  204. BF(a3.re, a1.re, a1.re, t4);\
  205. BF(a2.im, a0.im, a0.im, t6);\
  206. }
  207. // force loading all the inputs before storing any.
  208. // this is slightly slower for small data, but avoids store->load aliasing
  209. // for addresses separated by large powers of 2.
  210. #define BUTTERFLIES_BIG(a0,a1,a2,a3) {\
  211. FFTSample r0=a0.re, i0=a0.im, r1=a1.re, i1=a1.im;\
  212. BF(t3, t5, t5, t1);\
  213. BF(a2.re, a0.re, r0, t5);\
  214. BF(a3.im, a1.im, i1, t3);\
  215. BF(t4, t6, t2, t6);\
  216. BF(a3.re, a1.re, r1, t4);\
  217. BF(a2.im, a0.im, i0, t6);\
  218. }
  219. #define TRANSFORM(a0,a1,a2,a3,wre,wim) {\
  220. CMUL(t1, t2, a2.re, a2.im, wre, -wim);\
  221. CMUL(t5, t6, a3.re, a3.im, wre, wim);\
  222. BUTTERFLIES(a0,a1,a2,a3)\
  223. }
  224. #define TRANSFORM_ZERO(a0,a1,a2,a3) {\
  225. t1 = a2.re;\
  226. t2 = a2.im;\
  227. t5 = a3.re;\
  228. t6 = a3.im;\
  229. BUTTERFLIES(a0,a1,a2,a3)\
  230. }
  231. /* z[0...8n-1], w[1...2n-1] */
  232. #define PASS(name)\
  233. static void name(FFTComplex *z, const FFTSample *wre, unsigned int n)\
  234. {\
  235. FFTSample t1, t2, t3, t4, t5, t6;\
  236. int o1 = 2*n;\
  237. int o2 = 4*n;\
  238. int o3 = 6*n;\
  239. const FFTSample *wim = wre+o1;\
  240. n--;\
  241. \
  242. TRANSFORM_ZERO(z[0],z[o1],z[o2],z[o3]);\
  243. TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
  244. do {\
  245. z += 2;\
  246. wre += 2;\
  247. wim -= 2;\
  248. TRANSFORM(z[0],z[o1],z[o2],z[o3],wre[0],wim[0]);\
  249. TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
  250. } while(--n);\
  251. }
  252. PASS(pass)
  253. #undef BUTTERFLIES
  254. #define BUTTERFLIES BUTTERFLIES_BIG
  255. PASS(pass_big)
  256. #define DECL_FFT(n,n2,n4)\
  257. static void fft##n(FFTComplex *z)\
  258. {\
  259. fft##n2(z);\
  260. fft##n4(z+n4*2);\
  261. fft##n4(z+n4*3);\
  262. pass(z,TX_NAME(ff_cos_##n),n4/2);\
  263. }
  264. static void fft2(FFTComplex *z)
  265. {
  266. FFTComplex tmp;
  267. BF(tmp.re, z[0].re, z[0].re, z[1].re);
  268. BF(tmp.im, z[0].im, z[0].im, z[1].im);
  269. z[1] = tmp;
  270. }
  271. static void fft4(FFTComplex *z)
  272. {
  273. FFTSample t1, t2, t3, t4, t5, t6, t7, t8;
  274. BF(t3, t1, z[0].re, z[1].re);
  275. BF(t8, t6, z[3].re, z[2].re);
  276. BF(z[2].re, z[0].re, t1, t6);
  277. BF(t4, t2, z[0].im, z[1].im);
  278. BF(t7, t5, z[2].im, z[3].im);
  279. BF(z[3].im, z[1].im, t4, t8);
  280. BF(z[3].re, z[1].re, t3, t7);
  281. BF(z[2].im, z[0].im, t2, t5);
  282. }
  283. static void fft8(FFTComplex *z)
  284. {
  285. FFTSample t1, t2, t3, t4, t5, t6;
  286. fft4(z);
  287. BF(t1, z[5].re, z[4].re, -z[5].re);
  288. BF(t2, z[5].im, z[4].im, -z[5].im);
  289. BF(t5, z[7].re, z[6].re, -z[7].re);
  290. BF(t6, z[7].im, z[6].im, -z[7].im);
  291. BUTTERFLIES(z[0],z[2],z[4],z[6]);
  292. TRANSFORM(z[1],z[3],z[5],z[7],RESCALE(M_SQRT1_2),RESCALE(M_SQRT1_2));
  293. }
  294. static void fft16(FFTComplex *z)
  295. {
  296. FFTSample t1, t2, t3, t4, t5, t6;
  297. FFTSample cos_16_1 = TX_NAME(ff_cos_16)[1];
  298. FFTSample cos_16_3 = TX_NAME(ff_cos_16)[3];
  299. fft8(z);
  300. fft4(z+8);
  301. fft4(z+12);
  302. TRANSFORM_ZERO(z[0],z[4],z[8],z[12]);
  303. TRANSFORM(z[2],z[6],z[10],z[14],RESCALE(M_SQRT1_2),RESCALE(M_SQRT1_2));
  304. TRANSFORM(z[1],z[5],z[9],z[13],cos_16_1,cos_16_3);
  305. TRANSFORM(z[3],z[7],z[11],z[15],cos_16_3,cos_16_1);
  306. }
  307. DECL_FFT(32,16,8)
  308. DECL_FFT(64,32,16)
  309. DECL_FFT(128,64,32)
  310. DECL_FFT(256,128,64)
  311. DECL_FFT(512,256,128)
  312. #define pass pass_big
  313. DECL_FFT(1024,512,256)
  314. DECL_FFT(2048,1024,512)
  315. DECL_FFT(4096,2048,1024)
  316. DECL_FFT(8192,4096,2048)
  317. DECL_FFT(16384,8192,4096)
  318. DECL_FFT(32768,16384,8192)
  319. DECL_FFT(65536,32768,16384)
  320. DECL_FFT(131072,65536,32768)
  321. static void (* const fft_dispatch[])(FFTComplex*) = {
  322. NULL, fft2, fft4, fft8, fft16, fft32, fft64, fft128, fft256, fft512,
  323. fft1024, fft2048, fft4096, fft8192, fft16384, fft32768, fft65536, fft131072
  324. };
  325. #define DECL_COMP_FFT(N) \
  326. static void compound_fft_##N##xM(AVTXContext *s, void *_out, \
  327. void *_in, ptrdiff_t stride) \
  328. { \
  329. const int m = s->m, *in_map = s->pfatab, *out_map = in_map + N*m; \
  330. FFTComplex *in = _in; \
  331. FFTComplex *out = _out; \
  332. FFTComplex fft##N##in[N]; \
  333. void (*fftp)(FFTComplex *z) = fft_dispatch[av_log2(m)]; \
  334. \
  335. for (int i = 0; i < m; i++) { \
  336. for (int j = 0; j < N; j++) \
  337. fft##N##in[j] = in[in_map[i*N + j]]; \
  338. fft##N(s->tmp + s->revtab[i], fft##N##in, m); \
  339. } \
  340. \
  341. for (int i = 0; i < N; i++) \
  342. fftp(s->tmp + m*i); \
  343. \
  344. for (int i = 0; i < N*m; i++) \
  345. out[i] = s->tmp[out_map[i]]; \
  346. }
  347. DECL_COMP_FFT(3)
  348. DECL_COMP_FFT(5)
  349. DECL_COMP_FFT(15)
  350. static void monolithic_fft(AVTXContext *s, void *_out, void *_in,
  351. ptrdiff_t stride)
  352. {
  353. FFTComplex *in = _in;
  354. FFTComplex *out = _out;
  355. int m = s->m, mb = av_log2(m);
  356. if (s->flags & AV_TX_INPLACE) {
  357. FFTComplex tmp;
  358. int src, dst, *inplace_idx = s->inplace_idx;
  359. src = *inplace_idx++;
  360. do {
  361. tmp = out[src];
  362. dst = s->revtab[src];
  363. do {
  364. FFSWAP(FFTComplex, tmp, out[dst]);
  365. dst = s->revtab[dst];
  366. } while (dst != src); /* Can be > as well, but is less predictable */
  367. out[dst] = tmp;
  368. } while ((src = *inplace_idx++));
  369. } else {
  370. for (int i = 0; i < m; i++)
  371. out[i] = in[s->revtab[i]];
  372. }
  373. fft_dispatch[mb](out);
  374. }
  375. static void naive_fft(AVTXContext *s, void *_out, void *_in,
  376. ptrdiff_t stride)
  377. {
  378. FFTComplex *in = _in;
  379. FFTComplex *out = _out;
  380. const int n = s->n;
  381. double phase = s->inv ? 2.0*M_PI/n : -2.0*M_PI/n;
  382. for(int i = 0; i < n; i++) {
  383. FFTComplex tmp = { 0 };
  384. for(int j = 0; j < n; j++) {
  385. const double factor = phase*i*j;
  386. const FFTComplex mult = {
  387. RESCALE(cos(factor)),
  388. RESCALE(sin(factor)),
  389. };
  390. FFTComplex res;
  391. CMUL3(res, in[j], mult);
  392. tmp.re += res.re;
  393. tmp.im += res.im;
  394. }
  395. out[i] = tmp;
  396. }
  397. }
  398. #define DECL_COMP_IMDCT(N) \
  399. static void compound_imdct_##N##xM(AVTXContext *s, void *_dst, void *_src, \
  400. ptrdiff_t stride) \
  401. { \
  402. FFTComplex fft##N##in[N]; \
  403. FFTComplex *z = _dst, *exp = s->exptab; \
  404. const int m = s->m, len8 = N*m >> 1; \
  405. const int *in_map = s->pfatab, *out_map = in_map + N*m; \
  406. const FFTSample *src = _src, *in1, *in2; \
  407. void (*fftp)(FFTComplex *) = fft_dispatch[av_log2(m)]; \
  408. \
  409. stride /= sizeof(*src); /* To convert it from bytes */ \
  410. in1 = src; \
  411. in2 = src + ((N*m*2) - 1) * stride; \
  412. \
  413. for (int i = 0; i < m; i++) { \
  414. for (int j = 0; j < N; j++) { \
  415. const int k = in_map[i*N + j]; \
  416. FFTComplex tmp = { in2[-k*stride], in1[k*stride] }; \
  417. CMUL3(fft##N##in[j], tmp, exp[k >> 1]); \
  418. } \
  419. fft##N(s->tmp + s->revtab[i], fft##N##in, m); \
  420. } \
  421. \
  422. for (int i = 0; i < N; i++) \
  423. fftp(s->tmp + m*i); \
  424. \
  425. for (int i = 0; i < len8; i++) { \
  426. const int i0 = len8 + i, i1 = len8 - i - 1; \
  427. const int s0 = out_map[i0], s1 = out_map[i1]; \
  428. FFTComplex src1 = { s->tmp[s1].im, s->tmp[s1].re }; \
  429. FFTComplex src0 = { s->tmp[s0].im, s->tmp[s0].re }; \
  430. \
  431. CMUL(z[i1].re, z[i0].im, src1.re, src1.im, exp[i1].im, exp[i1].re); \
  432. CMUL(z[i0].re, z[i1].im, src0.re, src0.im, exp[i0].im, exp[i0].re); \
  433. } \
  434. }
  435. DECL_COMP_IMDCT(3)
  436. DECL_COMP_IMDCT(5)
  437. DECL_COMP_IMDCT(15)
  438. #define DECL_COMP_MDCT(N) \
  439. static void compound_mdct_##N##xM(AVTXContext *s, void *_dst, void *_src, \
  440. ptrdiff_t stride) \
  441. { \
  442. FFTSample *src = _src, *dst = _dst; \
  443. FFTComplex *exp = s->exptab, tmp, fft##N##in[N]; \
  444. const int m = s->m, len4 = N*m, len3 = len4 * 3, len8 = len4 >> 1; \
  445. const int *in_map = s->pfatab, *out_map = in_map + N*m; \
  446. void (*fftp)(FFTComplex *) = fft_dispatch[av_log2(m)]; \
  447. \
  448. stride /= sizeof(*dst); \
  449. \
  450. for (int i = 0; i < m; i++) { /* Folding and pre-reindexing */ \
  451. for (int j = 0; j < N; j++) { \
  452. const int k = in_map[i*N + j]; \
  453. if (k < len4) { \
  454. tmp.re = FOLD(-src[ len4 + k], src[1*len4 - 1 - k]); \
  455. tmp.im = FOLD(-src[ len3 + k], -src[1*len3 - 1 - k]); \
  456. } else { \
  457. tmp.re = FOLD(-src[ len4 + k], -src[5*len4 - 1 - k]); \
  458. tmp.im = FOLD( src[-len4 + k], -src[1*len3 - 1 - k]); \
  459. } \
  460. CMUL(fft##N##in[j].im, fft##N##in[j].re, tmp.re, tmp.im, \
  461. exp[k >> 1].re, exp[k >> 1].im); \
  462. } \
  463. fft##N(s->tmp + s->revtab[i], fft##N##in, m); \
  464. } \
  465. \
  466. for (int i = 0; i < N; i++) \
  467. fftp(s->tmp + m*i); \
  468. \
  469. for (int i = 0; i < len8; i++) { \
  470. const int i0 = len8 + i, i1 = len8 - i - 1; \
  471. const int s0 = out_map[i0], s1 = out_map[i1]; \
  472. FFTComplex src1 = { s->tmp[s1].re, s->tmp[s1].im }; \
  473. FFTComplex src0 = { s->tmp[s0].re, s->tmp[s0].im }; \
  474. \
  475. CMUL(dst[2*i1*stride + stride], dst[2*i0*stride], src0.re, src0.im, \
  476. exp[i0].im, exp[i0].re); \
  477. CMUL(dst[2*i0*stride + stride], dst[2*i1*stride], src1.re, src1.im, \
  478. exp[i1].im, exp[i1].re); \
  479. } \
  480. }
  481. DECL_COMP_MDCT(3)
  482. DECL_COMP_MDCT(5)
  483. DECL_COMP_MDCT(15)
  484. static void monolithic_imdct(AVTXContext *s, void *_dst, void *_src,
  485. ptrdiff_t stride)
  486. {
  487. FFTComplex *z = _dst, *exp = s->exptab;
  488. const int m = s->m, len8 = m >> 1;
  489. const FFTSample *src = _src, *in1, *in2;
  490. void (*fftp)(FFTComplex *) = fft_dispatch[av_log2(m)];
  491. stride /= sizeof(*src);
  492. in1 = src;
  493. in2 = src + ((m*2) - 1) * stride;
  494. for (int i = 0; i < m; i++) {
  495. FFTComplex tmp = { in2[-2*i*stride], in1[2*i*stride] };
  496. CMUL3(z[s->revtab[i]], tmp, exp[i]);
  497. }
  498. fftp(z);
  499. for (int i = 0; i < len8; i++) {
  500. const int i0 = len8 + i, i1 = len8 - i - 1;
  501. FFTComplex src1 = { z[i1].im, z[i1].re };
  502. FFTComplex src0 = { z[i0].im, z[i0].re };
  503. CMUL(z[i1].re, z[i0].im, src1.re, src1.im, exp[i1].im, exp[i1].re);
  504. CMUL(z[i0].re, z[i1].im, src0.re, src0.im, exp[i0].im, exp[i0].re);
  505. }
  506. }
  507. static void monolithic_mdct(AVTXContext *s, void *_dst, void *_src,
  508. ptrdiff_t stride)
  509. {
  510. FFTSample *src = _src, *dst = _dst;
  511. FFTComplex *exp = s->exptab, tmp, *z = _dst;
  512. const int m = s->m, len4 = m, len3 = len4 * 3, len8 = len4 >> 1;
  513. void (*fftp)(FFTComplex *) = fft_dispatch[av_log2(m)];
  514. stride /= sizeof(*dst);
  515. for (int i = 0; i < m; i++) { /* Folding and pre-reindexing */
  516. const int k = 2*i;
  517. if (k < len4) {
  518. tmp.re = FOLD(-src[ len4 + k], src[1*len4 - 1 - k]);
  519. tmp.im = FOLD(-src[ len3 + k], -src[1*len3 - 1 - k]);
  520. } else {
  521. tmp.re = FOLD(-src[ len4 + k], -src[5*len4 - 1 - k]);
  522. tmp.im = FOLD( src[-len4 + k], -src[1*len3 - 1 - k]);
  523. }
  524. CMUL(z[s->revtab[i]].im, z[s->revtab[i]].re, tmp.re, tmp.im,
  525. exp[i].re, exp[i].im);
  526. }
  527. fftp(z);
  528. for (int i = 0; i < len8; i++) {
  529. const int i0 = len8 + i, i1 = len8 - i - 1;
  530. FFTComplex src1 = { z[i1].re, z[i1].im };
  531. FFTComplex src0 = { z[i0].re, z[i0].im };
  532. CMUL(dst[2*i1*stride + stride], dst[2*i0*stride], src0.re, src0.im,
  533. exp[i0].im, exp[i0].re);
  534. CMUL(dst[2*i0*stride + stride], dst[2*i1*stride], src1.re, src1.im,
  535. exp[i1].im, exp[i1].re);
  536. }
  537. }
  538. static void naive_imdct(AVTXContext *s, void *_dst, void *_src,
  539. ptrdiff_t stride)
  540. {
  541. int len = s->n;
  542. int len2 = len*2;
  543. FFTSample *src = _src;
  544. FFTSample *dst = _dst;
  545. double scale = s->scale;
  546. const double phase = M_PI/(4.0*len2);
  547. stride /= sizeof(*src);
  548. for (int i = 0; i < len; i++) {
  549. double sum_d = 0.0;
  550. double sum_u = 0.0;
  551. double i_d = phase * (4*len - 2*i - 1);
  552. double i_u = phase * (3*len2 + 2*i + 1);
  553. for (int j = 0; j < len2; j++) {
  554. double a = (2 * j + 1);
  555. double a_d = cos(a * i_d);
  556. double a_u = cos(a * i_u);
  557. double val = UNSCALE(src[j*stride]);
  558. sum_d += a_d * val;
  559. sum_u += a_u * val;
  560. }
  561. dst[i + 0] = RESCALE( sum_d*scale);
  562. dst[i + len] = RESCALE(-sum_u*scale);
  563. }
  564. }
  565. static void naive_mdct(AVTXContext *s, void *_dst, void *_src,
  566. ptrdiff_t stride)
  567. {
  568. int len = s->n*2;
  569. FFTSample *src = _src;
  570. FFTSample *dst = _dst;
  571. double scale = s->scale;
  572. const double phase = M_PI/(4.0*len);
  573. stride /= sizeof(*dst);
  574. for (int i = 0; i < len; i++) {
  575. double sum = 0.0;
  576. for (int j = 0; j < len*2; j++) {
  577. int a = (2*j + 1 + len) * (2*i + 1);
  578. sum += UNSCALE(src[j]) * cos(a * phase);
  579. }
  580. dst[i*stride] = RESCALE(sum*scale);
  581. }
  582. }
  583. static int gen_mdct_exptab(AVTXContext *s, int len4, double scale)
  584. {
  585. const double theta = (scale < 0 ? len4 : 0) + 1.0/8.0;
  586. if (!(s->exptab = av_malloc_array(len4, sizeof(*s->exptab))))
  587. return AVERROR(ENOMEM);
  588. scale = sqrt(fabs(scale));
  589. for (int i = 0; i < len4; i++) {
  590. const double alpha = M_PI_2 * (i + theta) / len4;
  591. s->exptab[i].re = RESCALE(cos(alpha) * scale);
  592. s->exptab[i].im = RESCALE(sin(alpha) * scale);
  593. }
  594. return 0;
  595. }
  596. int TX_NAME(ff_tx_init_mdct_fft)(AVTXContext *s, av_tx_fn *tx,
  597. enum AVTXType type, int inv, int len,
  598. const void *scale, uint64_t flags)
  599. {
  600. const int is_mdct = ff_tx_type_is_mdct(type);
  601. int err, l, n = 1, m = 1, max_ptwo = 1 << (FF_ARRAY_ELEMS(fft_dispatch) - 1);
  602. if (is_mdct)
  603. len >>= 1;
  604. l = len;
  605. #define CHECK_FACTOR(DST, FACTOR, SRC) \
  606. if (DST == 1 && !(SRC % FACTOR)) { \
  607. DST = FACTOR; \
  608. SRC /= FACTOR; \
  609. }
  610. CHECK_FACTOR(n, 15, len)
  611. CHECK_FACTOR(n, 5, len)
  612. CHECK_FACTOR(n, 3, len)
  613. #undef CHECK_FACTOR
  614. /* len must be a power of two now */
  615. if (!(len & (len - 1)) && len >= 2 && len <= max_ptwo) {
  616. m = len;
  617. len = 1;
  618. }
  619. s->n = n;
  620. s->m = m;
  621. s->inv = inv;
  622. s->type = type;
  623. s->flags = flags;
  624. /* If we weren't able to split the length into factors we can handle,
  625. * resort to using the naive and slow FT. This also filters out
  626. * direct 3, 5 and 15 transforms as they're too niche. */
  627. if (len > 1 || m == 1) {
  628. if (is_mdct && (l & 1)) /* Odd (i)MDCTs are not supported yet */
  629. return AVERROR(ENOSYS);
  630. if (flags & AV_TX_INPLACE) /* Neither are in-place naive transforms */
  631. return AVERROR(ENOSYS);
  632. s->n = l;
  633. s->m = 1;
  634. *tx = naive_fft;
  635. if (is_mdct) {
  636. s->scale = *((SCALE_TYPE *)scale);
  637. *tx = inv ? naive_imdct : naive_mdct;
  638. }
  639. return 0;
  640. }
  641. if (n > 1 && m > 1) { /* 2D transform case */
  642. if ((err = ff_tx_gen_compound_mapping(s)))
  643. return err;
  644. if (!(s->tmp = av_malloc(n*m*sizeof(*s->tmp))))
  645. return AVERROR(ENOMEM);
  646. *tx = n == 3 ? compound_fft_3xM :
  647. n == 5 ? compound_fft_5xM :
  648. compound_fft_15xM;
  649. if (is_mdct)
  650. *tx = n == 3 ? inv ? compound_imdct_3xM : compound_mdct_3xM :
  651. n == 5 ? inv ? compound_imdct_5xM : compound_mdct_5xM :
  652. inv ? compound_imdct_15xM : compound_mdct_15xM;
  653. } else { /* Direct transform case */
  654. *tx = monolithic_fft;
  655. if (is_mdct)
  656. *tx = inv ? monolithic_imdct : monolithic_mdct;
  657. }
  658. if (n != 1)
  659. init_cos_tabs(0);
  660. if (m != 1) {
  661. if ((err = ff_tx_gen_ptwo_revtab(s, n == 1 && !is_mdct && !(flags & AV_TX_INPLACE))))
  662. return err;
  663. if (flags & AV_TX_INPLACE) {
  664. if (is_mdct) /* In-place MDCTs are not supported yet */
  665. return AVERROR(ENOSYS);
  666. if ((err = ff_tx_gen_ptwo_inplace_revtab_idx(s)))
  667. return err;
  668. }
  669. for (int i = 4; i <= av_log2(m); i++)
  670. init_cos_tabs(i);
  671. }
  672. if (is_mdct)
  673. return gen_mdct_exptab(s, n*m, *((SCALE_TYPE *)scale));
  674. return 0;
  675. }