mdct.c 5.9 KB

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