tx.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. #include <stddef.h>
  25. #include "tx.h"
  26. #include "thread.h"
  27. #include "mem.h"
  28. #include "avassert.h"
  29. typedef float FFTSample;
  30. typedef AVComplexFloat FFTComplex;
  31. struct AVTXContext {
  32. int n; /* Nptwo part */
  33. int m; /* Ptwo part */
  34. FFTComplex *exptab; /* MDCT exptab */
  35. FFTComplex *tmp; /* Temporary buffer needed for all compound transforms */
  36. int *pfatab; /* Input/Output mapping for compound transforms */
  37. int *revtab; /* Input mapping for power of two transforms */
  38. };
  39. #define FFT_NAME(x) x
  40. #define COSTABLE(size) \
  41. static DECLARE_ALIGNED(32, FFTSample, FFT_NAME(ff_cos_##size))[size/2]
  42. static FFTSample * const FFT_NAME(ff_cos_tabs)[18];
  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 m = 1 << index;
  60. double freq = 2*M_PI/m;
  61. FFTSample *tab = FFT_NAME(ff_cos_tabs)[index];
  62. for(int i = 0; i <= m/4; i++)
  63. tab[i] = cos(i*freq);
  64. for(int i = 1; i < m/4; i++)
  65. tab[m/2 - i] = tab[i];
  66. }
  67. typedef struct CosTabsInitOnce {
  68. void (*func)(void);
  69. AVOnce control;
  70. } CosTabsInitOnce;
  71. #define INIT_FF_COS_TABS_FUNC(index, size) \
  72. static av_cold void init_ff_cos_tabs_ ## size (void) \
  73. { \
  74. init_ff_cos_tabs(index); \
  75. }
  76. INIT_FF_COS_TABS_FUNC(4, 16)
  77. INIT_FF_COS_TABS_FUNC(5, 32)
  78. INIT_FF_COS_TABS_FUNC(6, 64)
  79. INIT_FF_COS_TABS_FUNC(7, 128)
  80. INIT_FF_COS_TABS_FUNC(8, 256)
  81. INIT_FF_COS_TABS_FUNC(9, 512)
  82. INIT_FF_COS_TABS_FUNC(10, 1024)
  83. INIT_FF_COS_TABS_FUNC(11, 2048)
  84. INIT_FF_COS_TABS_FUNC(12, 4096)
  85. INIT_FF_COS_TABS_FUNC(13, 8192)
  86. INIT_FF_COS_TABS_FUNC(14, 16384)
  87. INIT_FF_COS_TABS_FUNC(15, 32768)
  88. INIT_FF_COS_TABS_FUNC(16, 65536)
  89. INIT_FF_COS_TABS_FUNC(17, 131072)
  90. static CosTabsInitOnce cos_tabs_init_once[] = {
  91. { NULL },
  92. { NULL },
  93. { NULL },
  94. { NULL },
  95. { init_ff_cos_tabs_16, AV_ONCE_INIT },
  96. { init_ff_cos_tabs_32, AV_ONCE_INIT },
  97. { init_ff_cos_tabs_64, AV_ONCE_INIT },
  98. { init_ff_cos_tabs_128, AV_ONCE_INIT },
  99. { init_ff_cos_tabs_256, AV_ONCE_INIT },
  100. { init_ff_cos_tabs_512, AV_ONCE_INIT },
  101. { init_ff_cos_tabs_1024, AV_ONCE_INIT },
  102. { init_ff_cos_tabs_2048, AV_ONCE_INIT },
  103. { init_ff_cos_tabs_4096, AV_ONCE_INIT },
  104. { init_ff_cos_tabs_8192, AV_ONCE_INIT },
  105. { init_ff_cos_tabs_16384, AV_ONCE_INIT },
  106. { init_ff_cos_tabs_32768, AV_ONCE_INIT },
  107. { init_ff_cos_tabs_65536, AV_ONCE_INIT },
  108. { init_ff_cos_tabs_131072, AV_ONCE_INIT },
  109. };
  110. static FFTSample * const FFT_NAME(ff_cos_tabs)[] = {
  111. NULL, NULL, NULL, NULL,
  112. FFT_NAME(ff_cos_16),
  113. FFT_NAME(ff_cos_32),
  114. FFT_NAME(ff_cos_64),
  115. FFT_NAME(ff_cos_128),
  116. FFT_NAME(ff_cos_256),
  117. FFT_NAME(ff_cos_512),
  118. FFT_NAME(ff_cos_1024),
  119. FFT_NAME(ff_cos_2048),
  120. FFT_NAME(ff_cos_4096),
  121. FFT_NAME(ff_cos_8192),
  122. FFT_NAME(ff_cos_16384),
  123. FFT_NAME(ff_cos_32768),
  124. FFT_NAME(ff_cos_65536),
  125. FFT_NAME(ff_cos_131072),
  126. };
  127. static av_cold void ff_init_ff_cos_tabs(int index)
  128. {
  129. ff_thread_once(&cos_tabs_init_once[index].control,
  130. cos_tabs_init_once[index].func);
  131. }
  132. static AVOnce tabs_53_once = AV_ONCE_INIT;
  133. static DECLARE_ALIGNED(32, FFTComplex, FFT_NAME(ff_53_tabs))[4];
  134. static av_cold void ff_init_53_tabs(void)
  135. {
  136. ff_53_tabs[0] = (FFTComplex){ cos(2 * M_PI / 12), cos(2 * M_PI / 12) };
  137. ff_53_tabs[1] = (FFTComplex){ 0.5, 0.5 };
  138. ff_53_tabs[2] = (FFTComplex){ cos(2 * M_PI / 5), sin(2 * M_PI / 5) };
  139. ff_53_tabs[3] = (FFTComplex){ cos(2 * M_PI / 10), sin(2 * M_PI / 10) };
  140. }
  141. #define BF(x, y, a, b) do { \
  142. x = (a) - (b); \
  143. y = (a) + (b); \
  144. } while (0)
  145. #define CMUL(dre, dim, are, aim, bre, bim) do { \
  146. (dre) = (are) * (bre) - (aim) * (bim); \
  147. (dim) = (are) * (bim) + (aim) * (bre); \
  148. } while (0)
  149. #define CMUL3(c, a, b) CMUL((c).re, (c).im, (a).re, (a).im, (b).re, (b).im)
  150. static av_always_inline void fft3(FFTComplex *out, FFTComplex *in,
  151. ptrdiff_t stride)
  152. {
  153. FFTComplex tmp[2];
  154. tmp[0].re = in[1].im - in[2].im;
  155. tmp[0].im = in[1].re - in[2].re;
  156. tmp[1].re = in[1].re + in[2].re;
  157. tmp[1].im = in[1].im + in[2].im;
  158. out[0*stride].re = in[0].re + tmp[1].re;
  159. out[0*stride].im = in[0].im + tmp[1].im;
  160. tmp[0].re *= ff_53_tabs[0].re;
  161. tmp[0].im *= ff_53_tabs[0].im;
  162. tmp[1].re *= ff_53_tabs[1].re;
  163. tmp[1].im *= ff_53_tabs[1].re;
  164. out[1*stride].re = in[0].re - tmp[1].re + tmp[0].re;
  165. out[1*stride].im = in[0].im - tmp[1].im - tmp[0].im;
  166. out[2*stride].re = in[0].re - tmp[1].re - tmp[0].re;
  167. out[2*stride].im = in[0].im - tmp[1].im + tmp[0].im;
  168. }
  169. #define DECL_FFT5(NAME, D0, D1, D2, D3, D4) \
  170. static av_always_inline void NAME(FFTComplex *out, FFTComplex *in, \
  171. ptrdiff_t stride) \
  172. { \
  173. FFTComplex z0[4], t[6]; \
  174. \
  175. t[0].re = in[1].re + in[4].re; \
  176. t[0].im = in[1].im + in[4].im; \
  177. t[1].im = in[1].re - in[4].re; \
  178. t[1].re = in[1].im - in[4].im; \
  179. t[2].re = in[2].re + in[3].re; \
  180. t[2].im = in[2].im + in[3].im; \
  181. t[3].im = in[2].re - in[3].re; \
  182. t[3].re = in[2].im - in[3].im; \
  183. \
  184. out[D0*stride].re = in[0].re + in[1].re + in[2].re + \
  185. in[3].re + in[4].re; \
  186. out[D0*stride].im = in[0].im + in[1].im + in[2].im + \
  187. in[3].im + in[4].im; \
  188. \
  189. t[4].re = ff_53_tabs[2].re * t[2].re - ff_53_tabs[3].re * t[0].re; \
  190. t[4].im = ff_53_tabs[2].re * t[2].im - ff_53_tabs[3].re * t[0].im; \
  191. t[0].re = ff_53_tabs[2].re * t[0].re - ff_53_tabs[3].re * t[2].re; \
  192. t[0].im = ff_53_tabs[2].re * t[0].im - ff_53_tabs[3].re * t[2].im; \
  193. t[5].re = ff_53_tabs[2].im * t[3].re - ff_53_tabs[3].im * t[1].re; \
  194. t[5].im = ff_53_tabs[2].im * t[3].im - ff_53_tabs[3].im * t[1].im; \
  195. t[1].re = ff_53_tabs[2].im * t[1].re + ff_53_tabs[3].im * t[3].re; \
  196. t[1].im = ff_53_tabs[2].im * t[1].im + ff_53_tabs[3].im * t[3].im; \
  197. \
  198. z0[0].re = t[0].re - t[1].re; \
  199. z0[0].im = t[0].im - t[1].im; \
  200. z0[1].re = t[4].re + t[5].re; \
  201. z0[1].im = t[4].im + t[5].im; \
  202. \
  203. z0[2].re = t[4].re - t[5].re; \
  204. z0[2].im = t[4].im - t[5].im; \
  205. z0[3].re = t[0].re + t[1].re; \
  206. z0[3].im = t[0].im + t[1].im; \
  207. \
  208. out[D1*stride].re = in[0].re + z0[3].re; \
  209. out[D1*stride].im = in[0].im + z0[0].im; \
  210. out[D2*stride].re = in[0].re + z0[2].re; \
  211. out[D2*stride].im = in[0].im + z0[1].im; \
  212. out[D3*stride].re = in[0].re + z0[1].re; \
  213. out[D3*stride].im = in[0].im + z0[2].im; \
  214. out[D4*stride].re = in[0].re + z0[0].re; \
  215. out[D4*stride].im = in[0].im + z0[3].im; \
  216. }
  217. DECL_FFT5(fft5, 0, 1, 2, 3, 4)
  218. DECL_FFT5(fft5_m1, 0, 6, 12, 3, 9)
  219. DECL_FFT5(fft5_m2, 10, 1, 7, 13, 4)
  220. DECL_FFT5(fft5_m3, 5, 11, 2, 8, 14)
  221. static av_always_inline void fft15(FFTComplex *out, FFTComplex *in,
  222. ptrdiff_t stride)
  223. {
  224. FFTComplex tmp[15];
  225. for (int i = 0; i < 5; i++)
  226. fft3(tmp + i, in + i*3, 5);
  227. fft5_m1(out, tmp + 0, stride);
  228. fft5_m2(out, tmp + 5, stride);
  229. fft5_m3(out, tmp + 10, stride);
  230. }
  231. #define BUTTERFLIES(a0,a1,a2,a3) {\
  232. BF(t3, t5, t5, t1);\
  233. BF(a2.re, a0.re, a0.re, t5);\
  234. BF(a3.im, a1.im, a1.im, t3);\
  235. BF(t4, t6, t2, t6);\
  236. BF(a3.re, a1.re, a1.re, t4);\
  237. BF(a2.im, a0.im, a0.im, t6);\
  238. }
  239. // force loading all the inputs before storing any.
  240. // this is slightly slower for small data, but avoids store->load aliasing
  241. // for addresses separated by large powers of 2.
  242. #define BUTTERFLIES_BIG(a0,a1,a2,a3) {\
  243. FFTSample r0=a0.re, i0=a0.im, r1=a1.re, i1=a1.im;\
  244. BF(t3, t5, t5, t1);\
  245. BF(a2.re, a0.re, r0, t5);\
  246. BF(a3.im, a1.im, i1, t3);\
  247. BF(t4, t6, t2, t6);\
  248. BF(a3.re, a1.re, r1, t4);\
  249. BF(a2.im, a0.im, i0, t6);\
  250. }
  251. #define TRANSFORM(a0,a1,a2,a3,wre,wim) {\
  252. CMUL(t1, t2, a2.re, a2.im, wre, -wim);\
  253. CMUL(t5, t6, a3.re, a3.im, wre, wim);\
  254. BUTTERFLIES(a0,a1,a2,a3)\
  255. }
  256. #define TRANSFORM_ZERO(a0,a1,a2,a3) {\
  257. t1 = a2.re;\
  258. t2 = a2.im;\
  259. t5 = a3.re;\
  260. t6 = a3.im;\
  261. BUTTERFLIES(a0,a1,a2,a3)\
  262. }
  263. /* z[0...8n-1], w[1...2n-1] */
  264. #define PASS(name)\
  265. static void name(FFTComplex *z, const FFTSample *wre, unsigned int n)\
  266. {\
  267. FFTSample t1, t2, t3, t4, t5, t6;\
  268. int o1 = 2*n;\
  269. int o2 = 4*n;\
  270. int o3 = 6*n;\
  271. const FFTSample *wim = wre+o1;\
  272. n--;\
  273. \
  274. TRANSFORM_ZERO(z[0],z[o1],z[o2],z[o3]);\
  275. TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
  276. do {\
  277. z += 2;\
  278. wre += 2;\
  279. wim -= 2;\
  280. TRANSFORM(z[0],z[o1],z[o2],z[o3],wre[0],wim[0]);\
  281. TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
  282. } while(--n);\
  283. }
  284. PASS(pass)
  285. #undef BUTTERFLIES
  286. #define BUTTERFLIES BUTTERFLIES_BIG
  287. PASS(pass_big)
  288. #define DECL_FFT(n,n2,n4)\
  289. static void fft##n(FFTComplex *z)\
  290. {\
  291. fft##n2(z);\
  292. fft##n4(z+n4*2);\
  293. fft##n4(z+n4*3);\
  294. pass(z,FFT_NAME(ff_cos_##n),n4/2);\
  295. }
  296. static void fft4(FFTComplex *z)
  297. {
  298. FFTSample t1, t2, t3, t4, t5, t6, t7, t8;
  299. BF(t3, t1, z[0].re, z[1].re);
  300. BF(t8, t6, z[3].re, z[2].re);
  301. BF(z[2].re, z[0].re, t1, t6);
  302. BF(t4, t2, z[0].im, z[1].im);
  303. BF(t7, t5, z[2].im, z[3].im);
  304. BF(z[3].im, z[1].im, t4, t8);
  305. BF(z[3].re, z[1].re, t3, t7);
  306. BF(z[2].im, z[0].im, t2, t5);
  307. }
  308. static void fft8(FFTComplex *z)
  309. {
  310. FFTSample t1, t2, t3, t4, t5, t6;
  311. fft4(z);
  312. BF(t1, z[5].re, z[4].re, -z[5].re);
  313. BF(t2, z[5].im, z[4].im, -z[5].im);
  314. BF(t5, z[7].re, z[6].re, -z[7].re);
  315. BF(t6, z[7].im, z[6].im, -z[7].im);
  316. BUTTERFLIES(z[0],z[2],z[4],z[6]);
  317. TRANSFORM(z[1],z[3],z[5],z[7],M_SQRT1_2,M_SQRT1_2);
  318. }
  319. static void fft16(FFTComplex *z)
  320. {
  321. FFTSample t1, t2, t3, t4, t5, t6;
  322. FFTSample cos_16_1 = FFT_NAME(ff_cos_16)[1];
  323. FFTSample cos_16_3 = FFT_NAME(ff_cos_16)[3];
  324. fft8(z);
  325. fft4(z+8);
  326. fft4(z+12);
  327. TRANSFORM_ZERO(z[0],z[4],z[8],z[12]);
  328. TRANSFORM(z[2],z[6],z[10],z[14],M_SQRT1_2,M_SQRT1_2);
  329. TRANSFORM(z[1],z[5],z[9],z[13],cos_16_1,cos_16_3);
  330. TRANSFORM(z[3],z[7],z[11],z[15],cos_16_3,cos_16_1);
  331. }
  332. DECL_FFT(32,16,8)
  333. DECL_FFT(64,32,16)
  334. DECL_FFT(128,64,32)
  335. DECL_FFT(256,128,64)
  336. DECL_FFT(512,256,128)
  337. #define pass pass_big
  338. DECL_FFT(1024,512,256)
  339. DECL_FFT(2048,1024,512)
  340. DECL_FFT(4096,2048,1024)
  341. DECL_FFT(8192,4096,2048)
  342. DECL_FFT(16384,8192,4096)
  343. DECL_FFT(32768,16384,8192)
  344. DECL_FFT(65536,32768,16384)
  345. DECL_FFT(131072,65536,32768)
  346. static void (* const fft_dispatch[])(FFTComplex*) = {
  347. fft4, fft8, fft16, fft32, fft64, fft128, fft256, fft512, fft1024,
  348. fft2048, fft4096, fft8192, fft16384, fft32768, fft65536, fft131072
  349. };
  350. #define DECL_COMP_FFT(N) \
  351. static void compound_fft_##N##xM(AVTXContext *s, void *_out, \
  352. void *_in, ptrdiff_t stride) \
  353. { \
  354. const int m = s->m, *in_map = s->pfatab, *out_map = in_map + N*m; \
  355. FFTComplex *in = _in; \
  356. FFTComplex *out = _out; \
  357. FFTComplex fft##N##in[N]; \
  358. void (*fftp)(FFTComplex *z) = fft_dispatch[av_log2(m) - 2]; \
  359. \
  360. for (int i = 0; i < m; i++) { \
  361. for (int j = 0; j < N; j++) \
  362. fft##N##in[j] = in[in_map[i*N + j]]; \
  363. fft##N(s->tmp + s->revtab[i], fft##N##in, m); \
  364. } \
  365. \
  366. for (int i = 0; i < N; i++) \
  367. fftp(s->tmp + m*i); \
  368. \
  369. for (int i = 0; i < N*m; i++) \
  370. out[i] = s->tmp[out_map[i]]; \
  371. }
  372. DECL_COMP_FFT(3)
  373. DECL_COMP_FFT(5)
  374. DECL_COMP_FFT(15)
  375. static void monolithic_fft(AVTXContext *s, void *_out, void *_in,
  376. ptrdiff_t stride)
  377. {
  378. FFTComplex *in = _in;
  379. FFTComplex *out = _out;
  380. int m = s->m, mb = av_log2(m) - 2;
  381. for (int i = 0; i < m; i++)
  382. out[s->revtab[i]] = in[i];
  383. fft_dispatch[mb](out);
  384. }
  385. #define DECL_COMP_IMDCT(N) \
  386. static void compound_imdct_##N##xM(AVTXContext *s, void *_dst, void *_src, \
  387. ptrdiff_t stride) \
  388. { \
  389. FFTComplex fft##N##in[N]; \
  390. FFTComplex *z = _dst, *exp = s->exptab; \
  391. const int m = s->m, len8 = N*m >> 1; \
  392. const int *in_map = s->pfatab, *out_map = in_map + N*m; \
  393. const float *src = _src, *in1, *in2; \
  394. void (*fftp)(FFTComplex *) = fft_dispatch[av_log2(m) - 2]; \
  395. \
  396. stride /= sizeof(*src); /* To convert it from bytes */ \
  397. in1 = src; \
  398. in2 = src + ((N*m*2) - 1) * stride; \
  399. \
  400. for (int i = 0; i < m; i++) { \
  401. for (int j = 0; j < N; j++) { \
  402. const int k = in_map[i*N + j]; \
  403. FFTComplex tmp = { in2[-k*stride], in1[k*stride] }; \
  404. CMUL3(fft##N##in[j], tmp, exp[k >> 1]); \
  405. } \
  406. fft##N(s->tmp + s->revtab[i], fft##N##in, m); \
  407. } \
  408. \
  409. for (int i = 0; i < N; i++) \
  410. fftp(s->tmp + m*i); \
  411. \
  412. for (int i = 0; i < len8; i++) { \
  413. const int i0 = len8 + i, i1 = len8 - i - 1; \
  414. const int s0 = out_map[i0], s1 = out_map[i1]; \
  415. FFTComplex src1 = { s->tmp[s1].im, s->tmp[s1].re }; \
  416. FFTComplex src0 = { s->tmp[s0].im, s->tmp[s0].re }; \
  417. \
  418. CMUL(z[i1].re, z[i0].im, src1.re, src1.im, exp[i1].im, exp[i1].re); \
  419. CMUL(z[i0].re, z[i1].im, src0.re, src0.im, exp[i0].im, exp[i0].re); \
  420. } \
  421. }
  422. DECL_COMP_IMDCT(3)
  423. DECL_COMP_IMDCT(5)
  424. DECL_COMP_IMDCT(15)
  425. #define DECL_COMP_MDCT(N) \
  426. static void compound_mdct_##N##xM(AVTXContext *s, void *_dst, void *_src, \
  427. ptrdiff_t stride) \
  428. { \
  429. float *src = _src, *dst = _dst; \
  430. FFTComplex *exp = s->exptab, tmp, fft##N##in[N]; \
  431. const int m = s->m, len4 = N*m, len3 = len4 * 3, len8 = len4 >> 1; \
  432. const int *in_map = s->pfatab, *out_map = in_map + N*m; \
  433. void (*fftp)(FFTComplex *) = fft_dispatch[av_log2(m) - 2]; \
  434. \
  435. stride /= sizeof(*dst); \
  436. \
  437. for (int i = 0; i < m; i++) { /* Folding and pre-reindexing */ \
  438. for (int j = 0; j < N; j++) { \
  439. const int k = in_map[i*N + j]; \
  440. if (k < len4) { \
  441. tmp.re = -src[ len4 + k] + src[1*len4 - 1 - k]; \
  442. tmp.im = -src[ len3 + k] - src[1*len3 - 1 - k]; \
  443. } else { \
  444. tmp.re = -src[ len4 + k] - src[5*len4 - 1 - k]; \
  445. tmp.im = src[-len4 + k] - src[1*len3 - 1 - k]; \
  446. } \
  447. CMUL(fft##N##in[j].im, fft##N##in[j].re, tmp.re, tmp.im, \
  448. exp[k >> 1].re, exp[k >> 1].im); \
  449. } \
  450. fft##N(s->tmp + s->revtab[i], fft##N##in, m); \
  451. } \
  452. \
  453. for (int i = 0; i < N; i++) \
  454. fftp(s->tmp + m*i); \
  455. \
  456. for (int i = 0; i < len8; i++) { \
  457. const int i0 = len8 + i, i1 = len8 - i - 1; \
  458. const int s0 = out_map[i0], s1 = out_map[i1]; \
  459. FFTComplex src1 = { s->tmp[s1].re, s->tmp[s1].im }; \
  460. FFTComplex src0 = { s->tmp[s0].re, s->tmp[s0].im }; \
  461. \
  462. CMUL(dst[2*i1*stride + stride], dst[2*i0*stride], src0.re, src0.im, \
  463. exp[i0].im, exp[i0].re); \
  464. CMUL(dst[2*i0*stride + stride], dst[2*i1*stride], src1.re, src1.im, \
  465. exp[i1].im, exp[i1].re); \
  466. } \
  467. }
  468. DECL_COMP_MDCT(3)
  469. DECL_COMP_MDCT(5)
  470. DECL_COMP_MDCT(15)
  471. static void monolithic_imdct(AVTXContext *s, void *_dst, void *_src,
  472. ptrdiff_t stride)
  473. {
  474. FFTComplex *z = _dst, *exp = s->exptab;
  475. const int m = s->m, len8 = m >> 1;
  476. const float *src = _src, *in1, *in2;
  477. void (*fftp)(FFTComplex *) = fft_dispatch[av_log2(m) - 2];
  478. stride /= sizeof(*src);
  479. in1 = src;
  480. in2 = src + ((m*2) - 1) * stride;
  481. for (int i = 0; i < m; i++) {
  482. FFTComplex tmp = { in2[-2*i*stride], in1[2*i*stride] };
  483. CMUL3(z[s->revtab[i]], tmp, exp[i]);
  484. }
  485. fftp(z);
  486. for (int i = 0; i < len8; i++) {
  487. const int i0 = len8 + i, i1 = len8 - i - 1;
  488. FFTComplex src1 = { z[i1].im, z[i1].re };
  489. FFTComplex src0 = { z[i0].im, z[i0].re };
  490. CMUL(z[i1].re, z[i0].im, src1.re, src1.im, exp[i1].im, exp[i1].re);
  491. CMUL(z[i0].re, z[i1].im, src0.re, src0.im, exp[i0].im, exp[i0].re);
  492. }
  493. }
  494. static void monolithic_mdct(AVTXContext *s, void *_dst, void *_src,
  495. ptrdiff_t stride)
  496. {
  497. float *src = _src, *dst = _dst;
  498. FFTComplex *exp = s->exptab, tmp, *z = _dst;
  499. const int m = s->m, len4 = m, len3 = len4 * 3, len8 = len4 >> 1;
  500. void (*fftp)(FFTComplex *) = fft_dispatch[av_log2(m) - 2];
  501. stride /= sizeof(*dst);
  502. for (int i = 0; i < m; i++) { /* Folding and pre-reindexing */
  503. const int k = 2*i;
  504. if (k < len4) {
  505. tmp.re = -src[ len4 + k] + src[1*len4 - 1 - k];
  506. tmp.im = -src[ len3 + k] - src[1*len3 - 1 - k];
  507. } else {
  508. tmp.re = -src[ len4 + k] - src[5*len4 - 1 - k];
  509. tmp.im = src[-len4 + k] - src[1*len3 - 1 - k];
  510. }
  511. CMUL(z[s->revtab[i]].im, z[s->revtab[i]].re, tmp.re, tmp.im,
  512. exp[i].re, exp[i].im);
  513. }
  514. fftp(z);
  515. for (int i = 0; i < len8; i++) {
  516. const int i0 = len8 + i, i1 = len8 - i - 1;
  517. FFTComplex src1 = { z[i1].re, z[i1].im };
  518. FFTComplex src0 = { z[i0].re, z[i0].im };
  519. CMUL(dst[2*i1*stride + stride], dst[2*i0*stride], src0.re, src0.im,
  520. exp[i0].im, exp[i0].re);
  521. CMUL(dst[2*i0*stride + stride], dst[2*i1*stride], src1.re, src1.im,
  522. exp[i1].im, exp[i1].re);
  523. }
  524. }
  525. /* Calculates the modular multiplicative inverse, not fast, replace */
  526. static int mulinv(int n, int m)
  527. {
  528. n = n % m;
  529. for (int x = 1; x < m; x++)
  530. if (((n * x) % m) == 1)
  531. return x;
  532. av_assert0(0); /* Never reached */
  533. }
  534. /* Guaranteed to work for any n, m where gcd(n, m) == 1 */
  535. static int gen_compound_mapping(AVTXContext *s, int n, int m, int inv,
  536. enum AVTXType type)
  537. {
  538. int *in_map, *out_map;
  539. const int len = n*m;
  540. const int m_inv = mulinv(m, n);
  541. const int n_inv = mulinv(n, m);
  542. const int mdct = type == AV_TX_FLOAT_MDCT;
  543. if (!(s->pfatab = av_malloc(2*len*sizeof(*s->pfatab))))
  544. return AVERROR(ENOMEM);
  545. in_map = s->pfatab;
  546. out_map = s->pfatab + n*m;
  547. /* Ruritanian map for input, CRT map for output, can be swapped */
  548. for (int j = 0; j < m; j++) {
  549. for (int i = 0; i < n; i++) {
  550. /* Shifted by 1 to simplify forward MDCTs */
  551. in_map[j*n + i] = ((i*m + j*n) % len) << mdct;
  552. out_map[(i*m*m_inv + j*n*n_inv) % len] = i*m + j;
  553. }
  554. }
  555. /* Change transform direction by reversing all ACs */
  556. if (inv) {
  557. for (int i = 0; i < m; i++) {
  558. int *in = &in_map[i*n + 1]; /* Skip the DC */
  559. for (int j = 0; j < ((n - 1) >> 1); j++)
  560. FFSWAP(int, in[j], in[n - j - 2]);
  561. }
  562. }
  563. /* Our 15-point transform is also a compound one, so embed its input map */
  564. if (n == 15) {
  565. for (int k = 0; k < m; k++) {
  566. int tmp[15];
  567. memcpy(tmp, &in_map[k*15], 15*sizeof(*tmp));
  568. for (int i = 0; i < 5; i++) {
  569. for (int j = 0; j < 3; j++)
  570. in_map[k*15 + i*3 + j] = tmp[(i*3 + j*5) % 15];
  571. }
  572. }
  573. }
  574. return 0;
  575. }
  576. static int split_radix_permutation(int i, int n, int inverse)
  577. {
  578. int m;
  579. if (n <= 2)
  580. return i & 1;
  581. m = n >> 1;
  582. if (!(i & m))
  583. return split_radix_permutation(i, m, inverse)*2;
  584. m >>= 1;
  585. if (inverse == !(i & m))
  586. return split_radix_permutation(i, m, inverse)*4 + 1;
  587. else
  588. return split_radix_permutation(i, m, inverse)*4 - 1;
  589. }
  590. static int get_ptwo_revtab(AVTXContext *s, int m, int inv)
  591. {
  592. if (!(s->revtab = av_malloc(m*sizeof(*s->revtab))))
  593. return AVERROR(ENOMEM);
  594. /* Default */
  595. for (int i = 0; i < m; i++) {
  596. int k = -split_radix_permutation(i, m, inv) & (m - 1);
  597. s->revtab[k] = i;
  598. }
  599. return 0;
  600. }
  601. static int gen_mdct_exptab(AVTXContext *s, int len4, double scale)
  602. {
  603. const double theta = (scale < 0 ? len4 : 0) + 1.0/8.0;
  604. if (!(s->exptab = av_malloc_array(len4, sizeof(*s->exptab))))
  605. return AVERROR(ENOMEM);
  606. scale = sqrt(fabs(scale));
  607. for (int i = 0; i < len4; i++) {
  608. const double alpha = M_PI_2 * (i + theta) / len4;
  609. s->exptab[i].re = cos(alpha) * scale;
  610. s->exptab[i].im = sin(alpha) * scale;
  611. }
  612. return 0;
  613. }
  614. av_cold void av_tx_uninit(AVTXContext **ctx)
  615. {
  616. if (!(*ctx))
  617. return;
  618. av_free((*ctx)->pfatab);
  619. av_free((*ctx)->exptab);
  620. av_free((*ctx)->revtab);
  621. av_free((*ctx)->tmp);
  622. av_freep(ctx);
  623. }
  624. static int init_mdct_fft(AVTXContext *s, av_tx_fn *tx, enum AVTXType type,
  625. int inv, int len, const void *scale, uint64_t flags)
  626. {
  627. int err, n = 1, m = 1, max_ptwo = 1 << (FF_ARRAY_ELEMS(fft_dispatch) + 1);
  628. if (type == AV_TX_FLOAT_MDCT)
  629. len >>= 1;
  630. #define CHECK_FACTOR(DST, FACTOR, SRC) \
  631. if (DST == 1 && !(SRC % FACTOR)) { \
  632. DST = FACTOR; \
  633. SRC /= FACTOR; \
  634. }
  635. CHECK_FACTOR(n, 15, len)
  636. CHECK_FACTOR(n, 5, len)
  637. CHECK_FACTOR(n, 3, len)
  638. #undef CHECK_NPTWO_FACTOR
  639. /* len must be a power of two now */
  640. if (!(len & (len - 1)) && len >= 4 && len <= max_ptwo) {
  641. m = len;
  642. len = 1;
  643. }
  644. /* Filter out direct 3, 5 and 15 transforms, too niche */
  645. if (len > 1 || m == 1) {
  646. av_log(NULL, AV_LOG_ERROR, "Unsupported transform size: n = %i, "
  647. "m = %i, residual = %i!\n", n, m, len);
  648. return AVERROR(EINVAL);
  649. } else if (n > 1 && m > 1) { /* 2D transform case */
  650. if ((err = gen_compound_mapping(s, n, m, inv, type)))
  651. return err;
  652. if (!(s->tmp = av_malloc(n*m*sizeof(*s->tmp))))
  653. return AVERROR(ENOMEM);
  654. *tx = n == 3 ? compound_fft_3xM :
  655. n == 5 ? compound_fft_5xM :
  656. compound_fft_15xM;
  657. if (type == AV_TX_FLOAT_MDCT)
  658. *tx = n == 3 ? inv ? compound_imdct_3xM : compound_mdct_3xM :
  659. n == 5 ? inv ? compound_imdct_5xM : compound_mdct_5xM :
  660. inv ? compound_imdct_15xM : compound_mdct_15xM;
  661. } else { /* Direct transform case */
  662. *tx = monolithic_fft;
  663. if (type == AV_TX_FLOAT_MDCT)
  664. *tx = inv ? monolithic_imdct : monolithic_mdct;
  665. }
  666. if (n != 1)
  667. ff_thread_once(&tabs_53_once, ff_init_53_tabs);
  668. if (m != 1) {
  669. get_ptwo_revtab(s, m, inv);
  670. for (int i = 4; i <= av_log2(m); i++)
  671. ff_init_ff_cos_tabs(i);
  672. }
  673. if (type == AV_TX_FLOAT_MDCT)
  674. if ((err = gen_mdct_exptab(s, n*m, *((float *)scale))))
  675. return err;
  676. s->n = n;
  677. s->m = m;
  678. return 0;
  679. }
  680. av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type,
  681. int inv, int len, const void *scale, uint64_t flags)
  682. {
  683. int err;
  684. AVTXContext *s = av_mallocz(sizeof(*s));
  685. if (!s)
  686. return AVERROR(ENOMEM);
  687. switch (type) {
  688. case AV_TX_FLOAT_FFT:
  689. case AV_TX_FLOAT_MDCT:
  690. if ((err = init_mdct_fft(s, tx, type, inv, len, scale, flags)))
  691. goto fail;
  692. break;
  693. default:
  694. err = AVERROR(EINVAL);
  695. goto fail;
  696. }
  697. *ctx = s;
  698. return 0;
  699. fail:
  700. av_tx_uninit(&s);
  701. *tx = NULL;
  702. return err;
  703. }