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.

230 lines
5.8KB

  1. /*
  2. * MDCT/IMDCT transforms
  3. * Copyright (c) 2002 Fabrice Bellard
  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. #include "libavutil/mathematics.h"
  22. #include "fft.h"
  23. /**
  24. * @file libavcodec/mdct.c
  25. * MDCT/IMDCT transforms.
  26. */
  27. // Generate a Kaiser-Bessel Derived Window.
  28. #define BESSEL_I0_ITER 50 // default: 50 iterations of Bessel I0 approximation
  29. av_cold void ff_kbd_window_init(float *window, float alpha, int n)
  30. {
  31. int i, j;
  32. double sum = 0.0, bessel, tmp;
  33. double local_window[n];
  34. double alpha2 = (alpha * M_PI / n) * (alpha * M_PI / n);
  35. for (i = 0; i < n; i++) {
  36. tmp = i * (n - i) * alpha2;
  37. bessel = 1.0;
  38. for (j = BESSEL_I0_ITER; j > 0; j--)
  39. bessel = bessel * tmp / (j * j) + 1;
  40. sum += bessel;
  41. local_window[i] = sum;
  42. }
  43. sum++;
  44. for (i = 0; i < n; i++)
  45. window[i] = sqrt(local_window[i] / sum);
  46. }
  47. #include "mdct_tablegen.h"
  48. /**
  49. * init MDCT or IMDCT computation.
  50. */
  51. av_cold int ff_mdct_init(FFTContext *s, int nbits, int inverse, double scale)
  52. {
  53. int n, n4, i;
  54. double alpha, theta;
  55. int tstep;
  56. memset(s, 0, sizeof(*s));
  57. n = 1 << nbits;
  58. s->mdct_bits = nbits;
  59. s->mdct_size = n;
  60. n4 = n >> 2;
  61. s->permutation = FF_MDCT_PERM_NONE;
  62. if (ff_fft_init(s, s->mdct_bits - 2, inverse) < 0)
  63. goto fail;
  64. s->tcos = av_malloc(n/2 * sizeof(FFTSample));
  65. if (!s->tcos)
  66. goto fail;
  67. switch (s->permutation) {
  68. case FF_MDCT_PERM_NONE:
  69. s->tsin = s->tcos + n4;
  70. tstep = 1;
  71. break;
  72. case FF_MDCT_PERM_INTERLEAVE:
  73. s->tsin = s->tcos + 1;
  74. tstep = 2;
  75. break;
  76. default:
  77. goto fail;
  78. }
  79. theta = 1.0 / 8.0 + (scale < 0 ? n4 : 0);
  80. scale = sqrt(fabs(scale));
  81. for(i=0;i<n4;i++) {
  82. alpha = 2 * M_PI * (i + theta) / n;
  83. s->tcos[i*tstep] = -cos(alpha) * scale;
  84. s->tsin[i*tstep] = -sin(alpha) * scale;
  85. }
  86. return 0;
  87. fail:
  88. ff_mdct_end(s);
  89. return -1;
  90. }
  91. /* complex multiplication: p = a * b */
  92. #define CMUL(pre, pim, are, aim, bre, bim) \
  93. {\
  94. FFTSample _are = (are);\
  95. FFTSample _aim = (aim);\
  96. FFTSample _bre = (bre);\
  97. FFTSample _bim = (bim);\
  98. (pre) = _are * _bre - _aim * _bim;\
  99. (pim) = _are * _bim + _aim * _bre;\
  100. }
  101. /**
  102. * Compute the middle half of the inverse MDCT of size N = 2^nbits,
  103. * thus excluding the parts that can be derived by symmetry
  104. * @param output N/2 samples
  105. * @param input N/2 samples
  106. */
  107. void ff_imdct_half_c(FFTContext *s, FFTSample *output, const FFTSample *input)
  108. {
  109. int k, n8, n4, n2, n, j;
  110. const uint16_t *revtab = s->revtab;
  111. const FFTSample *tcos = s->tcos;
  112. const FFTSample *tsin = s->tsin;
  113. const FFTSample *in1, *in2;
  114. FFTComplex *z = (FFTComplex *)output;
  115. n = 1 << s->mdct_bits;
  116. n2 = n >> 1;
  117. n4 = n >> 2;
  118. n8 = n >> 3;
  119. /* pre rotation */
  120. in1 = input;
  121. in2 = input + n2 - 1;
  122. for(k = 0; k < n4; k++) {
  123. j=revtab[k];
  124. CMUL(z[j].re, z[j].im, *in2, *in1, tcos[k], tsin[k]);
  125. in1 += 2;
  126. in2 -= 2;
  127. }
  128. ff_fft_calc(s, z);
  129. /* post rotation + reordering */
  130. for(k = 0; k < n8; k++) {
  131. FFTSample r0, i0, r1, i1;
  132. CMUL(r0, i1, z[n8-k-1].im, z[n8-k-1].re, tsin[n8-k-1], tcos[n8-k-1]);
  133. CMUL(r1, i0, z[n8+k ].im, z[n8+k ].re, tsin[n8+k ], tcos[n8+k ]);
  134. z[n8-k-1].re = r0;
  135. z[n8-k-1].im = i0;
  136. z[n8+k ].re = r1;
  137. z[n8+k ].im = i1;
  138. }
  139. }
  140. /**
  141. * Compute inverse MDCT of size N = 2^nbits
  142. * @param output N samples
  143. * @param input N/2 samples
  144. */
  145. void ff_imdct_calc_c(FFTContext *s, FFTSample *output, const FFTSample *input)
  146. {
  147. int k;
  148. int n = 1 << s->mdct_bits;
  149. int n2 = n >> 1;
  150. int n4 = n >> 2;
  151. ff_imdct_half_c(s, output+n4, input);
  152. for(k = 0; k < n4; k++) {
  153. output[k] = -output[n2-k-1];
  154. output[n-k-1] = output[n2+k];
  155. }
  156. }
  157. /**
  158. * Compute MDCT of size N = 2^nbits
  159. * @param input N samples
  160. * @param out N/2 samples
  161. */
  162. void ff_mdct_calc_c(FFTContext *s, FFTSample *out, const FFTSample *input)
  163. {
  164. int i, j, n, n8, n4, n2, n3;
  165. FFTSample re, im;
  166. const uint16_t *revtab = s->revtab;
  167. const FFTSample *tcos = s->tcos;
  168. const FFTSample *tsin = s->tsin;
  169. FFTComplex *x = (FFTComplex *)out;
  170. n = 1 << s->mdct_bits;
  171. n2 = n >> 1;
  172. n4 = n >> 2;
  173. n8 = n >> 3;
  174. n3 = 3 * n4;
  175. /* pre rotation */
  176. for(i=0;i<n8;i++) {
  177. re = -input[2*i+3*n4] - input[n3-1-2*i];
  178. im = -input[n4+2*i] + input[n4-1-2*i];
  179. j = revtab[i];
  180. CMUL(x[j].re, x[j].im, re, im, -tcos[i], tsin[i]);
  181. re = input[2*i] - input[n2-1-2*i];
  182. im = -(input[n2+2*i] + input[n-1-2*i]);
  183. j = revtab[n8 + i];
  184. CMUL(x[j].re, x[j].im, re, im, -tcos[n8 + i], tsin[n8 + i]);
  185. }
  186. ff_fft_calc(s, x);
  187. /* post rotation */
  188. for(i=0;i<n8;i++) {
  189. FFTSample r0, i0, r1, i1;
  190. CMUL(i1, r0, x[n8-i-1].re, x[n8-i-1].im, -tsin[n8-i-1], -tcos[n8-i-1]);
  191. CMUL(i0, r1, x[n8+i ].re, x[n8+i ].im, -tsin[n8+i ], -tcos[n8+i ]);
  192. x[n8-i-1].re = r0;
  193. x[n8-i-1].im = i0;
  194. x[n8+i ].re = r1;
  195. x[n8+i ].im = i1;
  196. }
  197. }
  198. av_cold void ff_mdct_end(FFTContext *s)
  199. {
  200. av_freep(&s->tcos);
  201. ff_fft_end(s);
  202. }