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.

204 lines
5.1KB

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