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.

235 lines
5.9KB

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