mdct15.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (c) 2013-2014 Mozilla Corporation
  3. * Copyright (c) 2017 Rostislav Pehlivanov <atomnuker@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Celt non-power of 2 iMDCT
  24. */
  25. #include <float.h>
  26. #include <math.h>
  27. #include <stddef.h>
  28. #include "config.h"
  29. #include "libavutil/attributes.h"
  30. #include "libavutil/common.h"
  31. #include "mdct15.h"
  32. #define FFT_FLOAT 1
  33. #include "fft-internal.h"
  34. #define CMUL3(c, a, b) CMUL((c).re, (c).im, (a).re, (a).im, (b).re, (b).im)
  35. av_cold void ff_mdct15_uninit(MDCT15Context **ps)
  36. {
  37. MDCT15Context *s = *ps;
  38. if (!s)
  39. return;
  40. ff_fft_end(&s->ptwo_fft);
  41. av_freep(&s->pfa_prereindex);
  42. av_freep(&s->pfa_postreindex);
  43. av_freep(&s->twiddle_exptab);
  44. av_freep(&s->tmp);
  45. av_freep(ps);
  46. }
  47. static inline int init_pfa_reindex_tabs(MDCT15Context *s)
  48. {
  49. int i, j;
  50. const int b_ptwo = s->ptwo_fft.nbits; /* Bits for the power of two FFTs */
  51. const int l_ptwo = 1 << b_ptwo; /* Total length for the power of two FFTs */
  52. const int inv_1 = l_ptwo << ((4 - b_ptwo) & 3); /* (2^b_ptwo)^-1 mod 15 */
  53. const int inv_2 = 0xeeeeeeef & ((1U << b_ptwo) - 1); /* 15^-1 mod 2^b_ptwo */
  54. s->pfa_prereindex = av_malloc_array(15 * l_ptwo, sizeof(*s->pfa_prereindex));
  55. if (!s->pfa_prereindex)
  56. return 1;
  57. s->pfa_postreindex = av_malloc_array(15 * l_ptwo, sizeof(*s->pfa_postreindex));
  58. if (!s->pfa_postreindex)
  59. return 1;
  60. /* Pre/Post-reindex */
  61. for (i = 0; i < l_ptwo; i++) {
  62. for (j = 0; j < 15; j++) {
  63. const int q_pre = ((l_ptwo * j)/15 + i) >> b_ptwo;
  64. const int q_post = (((j*inv_1)/15) + (i*inv_2)) >> b_ptwo;
  65. const int k_pre = 15*i + (j - q_pre*15)*(1 << b_ptwo);
  66. const int k_post = i*inv_2*15 + j*inv_1 - 15*q_post*l_ptwo;
  67. s->pfa_prereindex[i*15 + j] = k_pre << 1;
  68. s->pfa_postreindex[k_post] = l_ptwo*j + i;
  69. }
  70. }
  71. return 0;
  72. }
  73. /* Stride is hardcoded to 3 */
  74. static inline void fft5(FFTComplex *out, FFTComplex *in, FFTComplex exptab[2])
  75. {
  76. FFTComplex z0[4], t[6];
  77. t[0].re = in[3].re + in[12].re;
  78. t[0].im = in[3].im + in[12].im;
  79. t[1].im = in[3].re - in[12].re;
  80. t[1].re = in[3].im - in[12].im;
  81. t[2].re = in[6].re + in[ 9].re;
  82. t[2].im = in[6].im + in[ 9].im;
  83. t[3].im = in[6].re - in[ 9].re;
  84. t[3].re = in[6].im - in[ 9].im;
  85. out[0].re = in[0].re + in[3].re + in[6].re + in[9].re + in[12].re;
  86. out[0].im = in[0].im + in[3].im + in[6].im + in[9].im + in[12].im;
  87. t[4].re = exptab[0].re * t[2].re - exptab[1].re * t[0].re;
  88. t[4].im = exptab[0].re * t[2].im - exptab[1].re * t[0].im;
  89. t[0].re = exptab[0].re * t[0].re - exptab[1].re * t[2].re;
  90. t[0].im = exptab[0].re * t[0].im - exptab[1].re * t[2].im;
  91. t[5].re = exptab[0].im * t[3].re - exptab[1].im * t[1].re;
  92. t[5].im = exptab[0].im * t[3].im - exptab[1].im * t[1].im;
  93. t[1].re = exptab[0].im * t[1].re + exptab[1].im * t[3].re;
  94. t[1].im = exptab[0].im * t[1].im + exptab[1].im * t[3].im;
  95. z0[0].re = t[0].re - t[1].re;
  96. z0[0].im = t[0].im - t[1].im;
  97. z0[1].re = t[4].re + t[5].re;
  98. z0[1].im = t[4].im + t[5].im;
  99. z0[2].re = t[4].re - t[5].re;
  100. z0[2].im = t[4].im - t[5].im;
  101. z0[3].re = t[0].re + t[1].re;
  102. z0[3].im = t[0].im + t[1].im;
  103. out[1].re = in[0].re + z0[3].re;
  104. out[1].im = in[0].im + z0[0].im;
  105. out[2].re = in[0].re + z0[2].re;
  106. out[2].im = in[0].im + z0[1].im;
  107. out[3].re = in[0].re + z0[1].re;
  108. out[3].im = in[0].im + z0[2].im;
  109. out[4].re = in[0].re + z0[0].re;
  110. out[4].im = in[0].im + z0[3].im;
  111. }
  112. static void fft15_c(FFTComplex *out, FFTComplex *in, FFTComplex *exptab, ptrdiff_t stride)
  113. {
  114. int k;
  115. FFTComplex tmp1[5], tmp2[5], tmp3[5];
  116. fft5(tmp1, in + 0, exptab + 19);
  117. fft5(tmp2, in + 1, exptab + 19);
  118. fft5(tmp3, in + 2, exptab + 19);
  119. for (k = 0; k < 5; k++) {
  120. FFTComplex t[2];
  121. CMUL3(t[0], tmp2[k], exptab[k]);
  122. CMUL3(t[1], tmp3[k], exptab[2 * k]);
  123. out[stride*k].re = tmp1[k].re + t[0].re + t[1].re;
  124. out[stride*k].im = tmp1[k].im + t[0].im + t[1].im;
  125. CMUL3(t[0], tmp2[k], exptab[k + 5]);
  126. CMUL3(t[1], tmp3[k], exptab[2 * (k + 5)]);
  127. out[stride*(k + 5)].re = tmp1[k].re + t[0].re + t[1].re;
  128. out[stride*(k + 5)].im = tmp1[k].im + t[0].im + t[1].im;
  129. CMUL3(t[0], tmp2[k], exptab[k + 10]);
  130. CMUL3(t[1], tmp3[k], exptab[2 * k + 5]);
  131. out[stride*(k + 10)].re = tmp1[k].re + t[0].re + t[1].re;
  132. out[stride*(k + 10)].im = tmp1[k].im + t[0].im + t[1].im;
  133. }
  134. }
  135. static void mdct15(MDCT15Context *s, float *dst, const float *src, ptrdiff_t stride)
  136. {
  137. int i, j;
  138. const int len4 = s->len4, len3 = len4 * 3, len8 = len4 >> 1;
  139. const int l_ptwo = 1 << s->ptwo_fft.nbits;
  140. FFTComplex fft15in[15];
  141. /* Folding and pre-reindexing */
  142. for (i = 0; i < l_ptwo; i++) {
  143. for (j = 0; j < 15; j++) {
  144. const int k = s->pfa_prereindex[i*15 + j];
  145. FFTComplex tmp, exp = s->twiddle_exptab[k >> 1];
  146. if (k < len4) {
  147. tmp.re = -src[ len4 + k] + src[1*len4 - 1 - k];
  148. tmp.im = -src[ len3 + k] - src[1*len3 - 1 - k];
  149. } else {
  150. tmp.re = -src[ len4 + k] - src[5*len4 - 1 - k];
  151. tmp.im = src[-len4 + k] - src[1*len3 - 1 - k];
  152. }
  153. CMUL(fft15in[j].im, fft15in[j].re, tmp.re, tmp.im, exp.re, exp.im);
  154. }
  155. s->fft15(s->tmp + s->ptwo_fft.revtab[i], fft15in, s->exptab, l_ptwo);
  156. }
  157. /* Then a 15xN FFT (where N is a power of two) */
  158. for (i = 0; i < 15; i++)
  159. s->ptwo_fft.fft_calc(&s->ptwo_fft, s->tmp + l_ptwo*i);
  160. /* Reindex again, apply twiddles and output */
  161. for (i = 0; i < len8; i++) {
  162. const int i0 = len8 + i, i1 = len8 - i - 1;
  163. const int s0 = s->pfa_postreindex[i0], s1 = s->pfa_postreindex[i1];
  164. CMUL(dst[2*i1*stride + stride], dst[2*i0*stride], s->tmp[s0].re, s->tmp[s0].im,
  165. s->twiddle_exptab[i0].im, s->twiddle_exptab[i0].re);
  166. CMUL(dst[2*i0*stride + stride], dst[2*i1*stride], s->tmp[s1].re, s->tmp[s1].im,
  167. s->twiddle_exptab[i1].im, s->twiddle_exptab[i1].re);
  168. }
  169. }
  170. static void imdct15_half(MDCT15Context *s, float *dst, const float *src,
  171. ptrdiff_t stride)
  172. {
  173. FFTComplex fft15in[15];
  174. FFTComplex *z = (FFTComplex *)dst;
  175. int i, j, len8 = s->len4 >> 1, l_ptwo = 1 << s->ptwo_fft.nbits;
  176. const float *in1 = src, *in2 = src + (s->len2 - 1) * stride;
  177. /* Reindex input, putting it into a buffer and doing an Nx15 FFT */
  178. for (i = 0; i < l_ptwo; i++) {
  179. for (j = 0; j < 15; j++) {
  180. const int k = s->pfa_prereindex[i*15 + j];
  181. FFTComplex tmp = { in2[-k*stride], in1[k*stride] };
  182. CMUL3(fft15in[j], tmp, s->twiddle_exptab[k >> 1]);
  183. }
  184. s->fft15(s->tmp + s->ptwo_fft.revtab[i], fft15in, s->exptab, l_ptwo);
  185. }
  186. /* Then a 15xN FFT (where N is a power of two) */
  187. for (i = 0; i < 15; i++)
  188. s->ptwo_fft.fft_calc(&s->ptwo_fft, s->tmp + l_ptwo*i);
  189. /* Reindex again, apply twiddles and output */
  190. s->postreindex(z, s->tmp, s->twiddle_exptab, s->pfa_postreindex, len8);
  191. }
  192. static void postrotate_c(FFTComplex *out, FFTComplex *in, FFTComplex *exp,
  193. int *lut, ptrdiff_t len8)
  194. {
  195. int i;
  196. /* Reindex again, apply twiddles and output */
  197. for (i = 0; i < len8; i++) {
  198. const int i0 = len8 + i, i1 = len8 - i - 1;
  199. const int s0 = lut[i0], s1 = lut[i1];
  200. CMUL(out[i1].re, out[i0].im, in[s1].im, in[s1].re, exp[i1].im, exp[i1].re);
  201. CMUL(out[i0].re, out[i1].im, in[s0].im, in[s0].re, exp[i0].im, exp[i0].re);
  202. }
  203. }
  204. av_cold int ff_mdct15_init(MDCT15Context **ps, int inverse, int N, double scale)
  205. {
  206. MDCT15Context *s;
  207. double alpha, theta;
  208. int len2 = 15 * (1 << N);
  209. int len = 2 * len2;
  210. int i;
  211. /* Tested and verified to work on everything in between */
  212. if ((N < 2) || (N > 13))
  213. return AVERROR(EINVAL);
  214. s = av_mallocz(sizeof(*s));
  215. if (!s)
  216. return AVERROR(ENOMEM);
  217. s->fft_n = N - 1;
  218. s->len4 = len2 / 2;
  219. s->len2 = len2;
  220. s->inverse = inverse;
  221. s->fft15 = fft15_c;
  222. s->mdct = mdct15;
  223. s->imdct_half = imdct15_half;
  224. s->postreindex = postrotate_c;
  225. if (ff_fft_init(&s->ptwo_fft, N - 1, s->inverse) < 0)
  226. goto fail;
  227. if (init_pfa_reindex_tabs(s))
  228. goto fail;
  229. s->tmp = av_malloc_array(len, 2 * sizeof(*s->tmp));
  230. if (!s->tmp)
  231. goto fail;
  232. s->twiddle_exptab = av_malloc_array(s->len4, sizeof(*s->twiddle_exptab));
  233. if (!s->twiddle_exptab)
  234. goto fail;
  235. theta = 0.125f + (scale < 0 ? s->len4 : 0);
  236. scale = sqrt(fabs(scale));
  237. for (i = 0; i < s->len4; i++) {
  238. alpha = 2 * M_PI * (i + theta) / len;
  239. s->twiddle_exptab[i].re = cosf(alpha) * scale;
  240. s->twiddle_exptab[i].im = sinf(alpha) * scale;
  241. }
  242. /* 15-point FFT exptab */
  243. for (i = 0; i < 19; i++) {
  244. if (i < 15) {
  245. double theta = (2.0f * M_PI * i) / 15.0f;
  246. if (!s->inverse)
  247. theta *= -1;
  248. s->exptab[i].re = cosf(theta);
  249. s->exptab[i].im = sinf(theta);
  250. } else { /* Wrap around to simplify fft15 */
  251. s->exptab[i] = s->exptab[i - 15];
  252. }
  253. }
  254. /* 5-point FFT exptab */
  255. s->exptab[19].re = cosf(2.0f * M_PI / 5.0f);
  256. s->exptab[19].im = sinf(2.0f * M_PI / 5.0f);
  257. s->exptab[20].re = cosf(1.0f * M_PI / 5.0f);
  258. s->exptab[20].im = sinf(1.0f * M_PI / 5.0f);
  259. /* Invert the phase for an inverse transform, do nothing for a forward transform */
  260. if (s->inverse) {
  261. s->exptab[19].im *= -1;
  262. s->exptab[20].im *= -1;
  263. }
  264. if (ARCH_X86)
  265. ff_mdct15_init_x86(s);
  266. *ps = s;
  267. return 0;
  268. fail:
  269. ff_mdct15_uninit(&s);
  270. return AVERROR(ENOMEM);
  271. }