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.

211 lines
4.9KB

  1. /*
  2. * (I)DCT Transforms
  3. * Copyright (c) 2009 Peter Ross <pross@xvid.org>
  4. * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
  5. * Copyright (c) 2010 Vitor Sessak
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * (Inverse) Discrete Cosine Transforms. These are also known as the
  26. * type II and type III DCTs respectively.
  27. */
  28. #include <math.h>
  29. #include "libavutil/mathematics.h"
  30. #include "fft.h"
  31. /* sin((M_PI * x / (2*n)) */
  32. #define SIN(s,n,x) (s->costab[(n) - (x)])
  33. /* cos((M_PI * x / (2*n)) */
  34. #define COS(s,n,x) (s->costab[x])
  35. static void ff_dst_calc_I_c(DCTContext *ctx, FFTSample *data)
  36. {
  37. int n = 1 << ctx->nbits;
  38. int i;
  39. data[0] = 0;
  40. for(i = 1; i < n/2; i++) {
  41. float tmp1 = data[i ];
  42. float tmp2 = data[n - i];
  43. float s = SIN(ctx, n, 2*i);
  44. s *= tmp1 + tmp2;
  45. tmp1 = (tmp1 - tmp2) * 0.5f;
  46. data[i ] = s + tmp1;
  47. data[n - i] = s - tmp1;
  48. }
  49. data[n/2] *= 2;
  50. ff_rdft_calc(&ctx->rdft, data);
  51. data[0] *= 0.5f;
  52. for(i = 1; i < n-2; i += 2) {
  53. data[i + 1] += data[i - 1];
  54. data[i ] = -data[i + 2];
  55. }
  56. data[n-1] = 0;
  57. }
  58. static void ff_dct_calc_I_c(DCTContext *ctx, FFTSample *data)
  59. {
  60. int n = 1 << ctx->nbits;
  61. int i;
  62. float next = -0.5f * (data[0] - data[n]);
  63. for(i = 0; i < n/2; i++) {
  64. float tmp1 = data[i ];
  65. float tmp2 = data[n - i];
  66. float s = SIN(ctx, n, 2*i);
  67. float c = COS(ctx, n, 2*i);
  68. c *= tmp1 - tmp2;
  69. s *= tmp1 - tmp2;
  70. next += c;
  71. tmp1 = (tmp1 + tmp2) * 0.5f;
  72. data[i ] = tmp1 - s;
  73. data[n - i] = tmp1 + s;
  74. }
  75. ff_rdft_calc(&ctx->rdft, data);
  76. data[n] = data[1];
  77. data[1] = next;
  78. for(i = 3; i <= n; i += 2)
  79. data[i] = data[i - 2] - data[i];
  80. }
  81. static void ff_dct_calc_III_c(DCTContext *ctx, FFTSample *data)
  82. {
  83. int n = 1 << ctx->nbits;
  84. int i;
  85. float next = data[n - 1];
  86. float inv_n = 1.0f / n;
  87. for (i = n - 2; i >= 2; i -= 2) {
  88. float val1 = data[i ];
  89. float val2 = data[i - 1] - data[i + 1];
  90. float c = COS(ctx, n, i);
  91. float s = SIN(ctx, n, i);
  92. data[i ] = c * val1 + s * val2;
  93. data[i + 1] = s * val1 - c * val2;
  94. }
  95. data[1] = 2 * next;
  96. ff_rdft_calc(&ctx->rdft, data);
  97. for (i = 0; i < n / 2; i++) {
  98. float tmp1 = data[i ] * inv_n;
  99. float tmp2 = data[n - i - 1] * inv_n;
  100. float csc = ctx->csc2[i] * (tmp1 - tmp2);
  101. tmp1 += tmp2;
  102. data[i ] = tmp1 + csc;
  103. data[n - i - 1] = tmp1 - csc;
  104. }
  105. }
  106. static void ff_dct_calc_II_c(DCTContext *ctx, FFTSample *data)
  107. {
  108. int n = 1 << ctx->nbits;
  109. int i;
  110. float next;
  111. for (i=0; i < n/2; i++) {
  112. float tmp1 = data[i ];
  113. float tmp2 = data[n - i - 1];
  114. float s = SIN(ctx, n, 2*i + 1);
  115. s *= tmp1 - tmp2;
  116. tmp1 = (tmp1 + tmp2) * 0.5f;
  117. data[i ] = tmp1 + s;
  118. data[n-i-1] = tmp1 - s;
  119. }
  120. ff_rdft_calc(&ctx->rdft, data);
  121. next = data[1] * 0.5;
  122. data[1] *= -1;
  123. for (i = n - 2; i >= 0; i -= 2) {
  124. float inr = data[i ];
  125. float ini = data[i + 1];
  126. float c = COS(ctx, n, i);
  127. float s = SIN(ctx, n, i);
  128. data[i ] = c * inr + s * ini;
  129. data[i+1] = next;
  130. next += s * inr - c * ini;
  131. }
  132. }
  133. void ff_dct_calc(DCTContext *s, FFTSample *data)
  134. {
  135. s->dct_calc(s, data);
  136. }
  137. av_cold int ff_dct_init(DCTContext *s, int nbits, enum DCTTransformType inverse)
  138. {
  139. int n = 1 << nbits;
  140. int i;
  141. s->nbits = nbits;
  142. s->inverse = inverse;
  143. ff_init_ff_cos_tabs(nbits+2);
  144. s->costab = ff_cos_tabs[nbits+2];
  145. s->csc2 = av_malloc(n/2 * sizeof(FFTSample));
  146. if (ff_rdft_init(&s->rdft, nbits, inverse == DCT_III) < 0) {
  147. av_free(s->csc2);
  148. return -1;
  149. }
  150. for (i = 0; i < n/2; i++)
  151. s->csc2[i] = 0.5 / sin((M_PI / (2*n) * (2*i + 1)));
  152. switch(inverse) {
  153. case DCT_I : s->dct_calc = ff_dct_calc_I_c; break;
  154. case DCT_II : s->dct_calc = ff_dct_calc_II_c ; break;
  155. case DCT_III: s->dct_calc = ff_dct_calc_III_c; break;
  156. case DST_I : s->dct_calc = ff_dst_calc_I_c; break;
  157. }
  158. return 0;
  159. }
  160. av_cold void ff_dct_end(DCTContext *s)
  161. {
  162. ff_rdft_end(&s->rdft);
  163. av_free(s->csc2);
  164. }