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.

213 lines
5.4KB

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