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.

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