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.

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