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.

171 lines
4.4KB

  1. /*
  2. * MDCT/IMDCT transforms
  3. * Copyright (c) 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "dsputil.h"
  20. /*
  21. * init MDCT or IMDCT computation
  22. */
  23. int ff_mdct_init(MDCTContext *s, int nbits, int inverse)
  24. {
  25. int n, n4, i;
  26. float alpha;
  27. memset(s, 0, sizeof(*s));
  28. n = 1 << nbits;
  29. s->nbits = nbits;
  30. s->n = n;
  31. n4 = n >> 2;
  32. s->tcos = malloc(n4 * sizeof(FFTSample));
  33. if (!s->tcos)
  34. goto fail;
  35. s->tsin = malloc(n4 * sizeof(FFTSample));
  36. if (!s->tsin)
  37. goto fail;
  38. for(i=0;i<n4;i++) {
  39. alpha = 2 * M_PI * (i + 1.0 / 8.0) / n;
  40. s->tcos[i] = -cos(alpha);
  41. s->tsin[i] = -sin(alpha);
  42. }
  43. if (fft_init(&s->fft, s->nbits - 2, inverse) < 0)
  44. goto fail;
  45. return 0;
  46. fail:
  47. av_freep(&s->tcos);
  48. av_freep(&s->tsin);
  49. return -1;
  50. }
  51. /* complex multiplication: p = a * b */
  52. #define CMUL(pre, pim, are, aim, bre, bim) \
  53. {\
  54. float _are = (are);\
  55. float _aim = (aim);\
  56. float _bre = (bre);\
  57. float _bim = (bim);\
  58. (pre) = _are * _bre - _aim * _bim;\
  59. (pim) = _are * _bim + _aim * _bre;\
  60. }
  61. /**
  62. * Compute inverse MDCT of size N = 2^nbits
  63. * @param output N samples
  64. * @param input N/2 samples
  65. * @param tmp N/2 samples
  66. */
  67. void ff_imdct_calc(MDCTContext *s, FFTSample *output,
  68. const FFTSample *input, FFTSample *tmp)
  69. {
  70. int k, n8, n4, n2, n, j;
  71. const uint16_t *revtab = s->fft.revtab;
  72. const FFTSample *tcos = s->tcos;
  73. const FFTSample *tsin = s->tsin;
  74. const FFTSample *in1, *in2;
  75. FFTComplex *z = (FFTComplex *)tmp;
  76. n = 1 << s->nbits;
  77. n2 = n >> 1;
  78. n4 = n >> 2;
  79. n8 = n >> 3;
  80. /* pre rotation */
  81. in1 = input;
  82. in2 = input + n2 - 1;
  83. for(k = 0; k < n4; k++) {
  84. j=revtab[k];
  85. CMUL(z[j].re, z[j].im, *in2, *in1, tcos[k], tsin[k]);
  86. in1 += 2;
  87. in2 -= 2;
  88. }
  89. fft_calc(&s->fft, z);
  90. /* post rotation + reordering */
  91. /* XXX: optimize */
  92. for(k = 0; k < n4; k++) {
  93. CMUL(z[k].re, z[k].im, z[k].re, z[k].im, tcos[k], tsin[k]);
  94. }
  95. for(k = 0; k < n8; k++) {
  96. output[2*k] = -z[n8 + k].im;
  97. output[n2-1-2*k] = z[n8 + k].im;
  98. output[2*k+1] = z[n8-1-k].re;
  99. output[n2-1-2*k-1] = -z[n8-1-k].re;
  100. output[n2 + 2*k]=-z[k+n8].re;
  101. output[n-1- 2*k]=-z[k+n8].re;
  102. output[n2 + 2*k+1]=z[n8-k-1].im;
  103. output[n-2 - 2 * k] = z[n8-k-1].im;
  104. }
  105. }
  106. /**
  107. * Compute MDCT of size N = 2^nbits
  108. * @param input N samples
  109. * @param out N/2 samples
  110. * @param tmp temporary storage of N/2 samples
  111. */
  112. void ff_mdct_calc(MDCTContext *s, FFTSample *out,
  113. const FFTSample *input, FFTSample *tmp)
  114. {
  115. int i, j, n, n8, n4, n2, n3;
  116. FFTSample re, im, re1, im1;
  117. const uint16_t *revtab = s->fft.revtab;
  118. const FFTSample *tcos = s->tcos;
  119. const FFTSample *tsin = s->tsin;
  120. FFTComplex *x = (FFTComplex *)tmp;
  121. n = 1 << s->nbits;
  122. n2 = n >> 1;
  123. n4 = n >> 2;
  124. n8 = n >> 3;
  125. n3 = 3 * n4;
  126. /* pre rotation */
  127. for(i=0;i<n8;i++) {
  128. re = -input[2*i+3*n4] - input[n3-1-2*i];
  129. im = -input[n4+2*i] + input[n4-1-2*i];
  130. j = revtab[i];
  131. CMUL(x[j].re, x[j].im, re, im, -tcos[i], tsin[i]);
  132. re = input[2*i] - input[n2-1-2*i];
  133. im = -(input[n2+2*i] + input[n-1-2*i]);
  134. j = revtab[n8 + i];
  135. CMUL(x[j].re, x[j].im, re, im, -tcos[n8 + i], tsin[n8 + i]);
  136. }
  137. fft_calc(&s->fft, x);
  138. /* post rotation */
  139. for(i=0;i<n4;i++) {
  140. re = x[i].re;
  141. im = x[i].im;
  142. CMUL(re1, im1, re, im, -tsin[i], -tcos[i]);
  143. out[2*i] = im1;
  144. out[n2-1-2*i] = re1;
  145. }
  146. }
  147. void ff_mdct_end(MDCTContext *s)
  148. {
  149. av_freep(&s->tcos);
  150. av_freep(&s->tsin);
  151. fft_end(&s->fft);
  152. }