mdct_template.c 5.5 KB

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