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.

210 lines
5.2KB

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