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.

224 lines
5.5KB

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