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.

178 lines
4.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 "dsputil.h"
  22. /**
  23. * @file mdct.c
  24. * MDCT/IMDCT transforms.
  25. */
  26. /**
  27. * init MDCT or IMDCT computation.
  28. */
  29. int ff_mdct_init(MDCTContext *s, int nbits, int inverse)
  30. {
  31. int n, n4, i;
  32. float alpha;
  33. memset(s, 0, sizeof(*s));
  34. n = 1 << nbits;
  35. s->nbits = nbits;
  36. s->n = n;
  37. n4 = n >> 2;
  38. s->tcos = av_malloc(n4 * sizeof(FFTSample));
  39. if (!s->tcos)
  40. goto fail;
  41. s->tsin = av_malloc(n4 * sizeof(FFTSample));
  42. if (!s->tsin)
  43. goto fail;
  44. for(i=0;i<n4;i++) {
  45. alpha = 2 * M_PI * (i + 1.0 / 8.0) / n;
  46. s->tcos[i] = -cos(alpha);
  47. s->tsin[i] = -sin(alpha);
  48. }
  49. if (ff_fft_init(&s->fft, s->nbits - 2, inverse) < 0)
  50. goto fail;
  51. return 0;
  52. fail:
  53. av_freep(&s->tcos);
  54. av_freep(&s->tsin);
  55. return -1;
  56. }
  57. /* complex multiplication: p = a * b */
  58. #define CMUL(pre, pim, are, aim, bre, bim) \
  59. {\
  60. float _are = (are);\
  61. float _aim = (aim);\
  62. float _bre = (bre);\
  63. float _bim = (bim);\
  64. (pre) = _are * _bre - _aim * _bim;\
  65. (pim) = _are * _bim + _aim * _bre;\
  66. }
  67. /**
  68. * Compute inverse MDCT of size N = 2^nbits
  69. * @param output N samples
  70. * @param input N/2 samples
  71. * @param tmp N/2 samples
  72. */
  73. void ff_imdct_calc(MDCTContext *s, FFTSample *output,
  74. const FFTSample *input, FFTSample *tmp)
  75. {
  76. int k, n8, n4, n2, n, j;
  77. const uint16_t *revtab = s->fft.revtab;
  78. const FFTSample *tcos = s->tcos;
  79. const FFTSample *tsin = s->tsin;
  80. const FFTSample *in1, *in2;
  81. FFTComplex *z = (FFTComplex *)tmp;
  82. n = 1 << s->nbits;
  83. n2 = n >> 1;
  84. n4 = n >> 2;
  85. n8 = n >> 3;
  86. /* pre rotation */
  87. in1 = input;
  88. in2 = input + n2 - 1;
  89. for(k = 0; k < n4; k++) {
  90. j=revtab[k];
  91. CMUL(z[j].re, z[j].im, *in2, *in1, tcos[k], tsin[k]);
  92. in1 += 2;
  93. in2 -= 2;
  94. }
  95. ff_fft_calc(&s->fft, z);
  96. /* post rotation + reordering */
  97. /* XXX: optimize */
  98. for(k = 0; k < n4; k++) {
  99. CMUL(z[k].re, z[k].im, z[k].re, z[k].im, tcos[k], tsin[k]);
  100. }
  101. for(k = 0; k < n8; k++) {
  102. output[2*k] = -z[n8 + k].im;
  103. output[n2-1-2*k] = z[n8 + k].im;
  104. output[2*k+1] = z[n8-1-k].re;
  105. output[n2-1-2*k-1] = -z[n8-1-k].re;
  106. output[n2 + 2*k]=-z[k+n8].re;
  107. output[n-1- 2*k]=-z[k+n8].re;
  108. output[n2 + 2*k+1]=z[n8-k-1].im;
  109. output[n-2 - 2 * k] = z[n8-k-1].im;
  110. }
  111. }
  112. /**
  113. * Compute MDCT of size N = 2^nbits
  114. * @param input N samples
  115. * @param out N/2 samples
  116. * @param tmp temporary storage of N/2 samples
  117. */
  118. void ff_mdct_calc(MDCTContext *s, FFTSample *out,
  119. const FFTSample *input, FFTSample *tmp)
  120. {
  121. int i, j, n, n8, n4, n2, n3;
  122. FFTSample re, im, re1, im1;
  123. const uint16_t *revtab = s->fft.revtab;
  124. const FFTSample *tcos = s->tcos;
  125. const FFTSample *tsin = s->tsin;
  126. FFTComplex *x = (FFTComplex *)tmp;
  127. n = 1 << s->nbits;
  128. n2 = n >> 1;
  129. n4 = n >> 2;
  130. n8 = n >> 3;
  131. n3 = 3 * n4;
  132. /* pre rotation */
  133. for(i=0;i<n8;i++) {
  134. re = -input[2*i+3*n4] - input[n3-1-2*i];
  135. im = -input[n4+2*i] + input[n4-1-2*i];
  136. j = revtab[i];
  137. CMUL(x[j].re, x[j].im, re, im, -tcos[i], tsin[i]);
  138. re = input[2*i] - input[n2-1-2*i];
  139. im = -(input[n2+2*i] + input[n-1-2*i]);
  140. j = revtab[n8 + i];
  141. CMUL(x[j].re, x[j].im, re, im, -tcos[n8 + i], tsin[n8 + i]);
  142. }
  143. ff_fft_calc(&s->fft, x);
  144. /* post rotation */
  145. for(i=0;i<n4;i++) {
  146. re = x[i].re;
  147. im = x[i].im;
  148. CMUL(re1, im1, re, im, -tsin[i], -tcos[i]);
  149. out[2*i] = im1;
  150. out[n2-1-2*i] = re1;
  151. }
  152. }
  153. void ff_mdct_end(MDCTContext *s)
  154. {
  155. av_freep(&s->tcos);
  156. av_freep(&s->tsin);
  157. ff_fft_end(&s->fft);
  158. }