2
0

dct.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <string.h>
  30. #include "libavutil/mathematics.h"
  31. #include "dct.h"
  32. #include "dct32.h"
  33. /* sin((M_PI * x / (2 * n)) */
  34. #define SIN(s, n, x) (s->costab[(n) - (x)])
  35. /* cos((M_PI * x / (2 * n)) */
  36. #define COS(s, n, x) (s->costab[x])
  37. static void dst_calc_I_c(DCTContext *ctx, FFTSample *data)
  38. {
  39. int n = 1 << ctx->nbits;
  40. int i;
  41. data[0] = 0;
  42. for (i = 1; i < n / 2; i++) {
  43. float tmp1 = data[i ];
  44. float tmp2 = data[n - i];
  45. float s = SIN(ctx, n, 2 * i);
  46. s *= tmp1 + tmp2;
  47. tmp1 = (tmp1 - tmp2) * 0.5f;
  48. data[i] = s + tmp1;
  49. data[n - i] = s - tmp1;
  50. }
  51. data[n / 2] *= 2;
  52. ctx->rdft.rdft_calc(&ctx->rdft, data);
  53. data[0] *= 0.5f;
  54. for (i = 1; i < n - 2; i += 2) {
  55. data[i + 1] += data[i - 1];
  56. data[i] = -data[i + 2];
  57. }
  58. data[n - 1] = 0;
  59. }
  60. static void dct_calc_I_c(DCTContext *ctx, FFTSample *data)
  61. {
  62. int n = 1 << ctx->nbits;
  63. int i;
  64. float next = -0.5f * (data[0] - data[n]);
  65. for (i = 0; i < n / 2; i++) {
  66. float tmp1 = data[i];
  67. float tmp2 = data[n - i];
  68. float s = SIN(ctx, n, 2 * i);
  69. float c = COS(ctx, n, 2 * i);
  70. c *= tmp1 - tmp2;
  71. s *= tmp1 - tmp2;
  72. next += c;
  73. tmp1 = (tmp1 + tmp2) * 0.5f;
  74. data[i] = tmp1 - s;
  75. data[n - i] = tmp1 + s;
  76. }
  77. ctx->rdft.rdft_calc(&ctx->rdft, data);
  78. data[n] = data[1];
  79. data[1] = next;
  80. for (i = 3; i <= n; i += 2)
  81. data[i] = data[i - 2] - data[i];
  82. }
  83. static void dct_calc_III_c(DCTContext *ctx, FFTSample *data)
  84. {
  85. int n = 1 << ctx->nbits;
  86. int i;
  87. float next = data[n - 1];
  88. float inv_n = 1.0f / n;
  89. for (i = n - 2; i >= 2; i -= 2) {
  90. float val1 = data[i];
  91. float val2 = data[i - 1] - data[i + 1];
  92. float c = COS(ctx, n, i);
  93. float s = SIN(ctx, n, i);
  94. data[i] = c * val1 + s * val2;
  95. data[i + 1] = s * val1 - c * val2;
  96. }
  97. data[1] = 2 * next;
  98. ctx->rdft.rdft_calc(&ctx->rdft, data);
  99. for (i = 0; i < n / 2; i++) {
  100. float tmp1 = data[i] * inv_n;
  101. float tmp2 = data[n - i - 1] * inv_n;
  102. float csc = ctx->csc2[i] * (tmp1 - tmp2);
  103. tmp1 += tmp2;
  104. data[i] = tmp1 + csc;
  105. data[n - i - 1] = tmp1 - csc;
  106. }
  107. }
  108. static void dct_calc_II_c(DCTContext *ctx, FFTSample *data)
  109. {
  110. int n = 1 << ctx->nbits;
  111. int i;
  112. float next;
  113. for (i = 0; i < n / 2; i++) {
  114. float tmp1 = data[i];
  115. float tmp2 = data[n - i - 1];
  116. float s = SIN(ctx, n, 2 * i + 1);
  117. s *= tmp1 - tmp2;
  118. tmp1 = (tmp1 + tmp2) * 0.5f;
  119. data[i] = tmp1 + s;
  120. data[n-i-1] = tmp1 - s;
  121. }
  122. ctx->rdft.rdft_calc(&ctx->rdft, data);
  123. next = data[1] * 0.5;
  124. data[1] *= -1;
  125. for (i = n - 2; i >= 0; i -= 2) {
  126. float inr = data[i ];
  127. float ini = data[i + 1];
  128. float c = COS(ctx, n, i);
  129. float s = SIN(ctx, n, i);
  130. data[i] = c * inr + s * ini;
  131. data[i + 1] = next;
  132. next += s * inr - c * ini;
  133. }
  134. }
  135. static void dct32_func(DCTContext *ctx, FFTSample *data)
  136. {
  137. ctx->dct32(data, data);
  138. }
  139. av_cold int ff_dct_init(DCTContext *s, int nbits, enum DCTTransformType inverse)
  140. {
  141. int n = 1 << nbits;
  142. int i;
  143. int ret;
  144. memset(s, 0, sizeof(*s));
  145. s->nbits = nbits;
  146. s->inverse = inverse;
  147. if (inverse == DCT_II && nbits == 5) {
  148. s->dct_calc = dct32_func;
  149. } else {
  150. ff_init_ff_cos_tabs(nbits + 2);
  151. s->costab = ff_cos_tabs[nbits + 2];
  152. s->csc2 = av_malloc_array(n / 2, sizeof(FFTSample));
  153. if (!s->csc2)
  154. return AVERROR(ENOMEM);
  155. if ((ret = ff_rdft_init(&s->rdft, nbits, inverse == DCT_III)) < 0) {
  156. av_freep(&s->csc2);
  157. return ret;
  158. }
  159. for (i = 0; i < n / 2; i++)
  160. s->csc2[i] = 0.5 / sin((M_PI / (2 * n) * (2 * i + 1)));
  161. switch (inverse) {
  162. case DCT_I : s->dct_calc = dct_calc_I_c; break;
  163. case DCT_II : s->dct_calc = dct_calc_II_c; break;
  164. case DCT_III: s->dct_calc = dct_calc_III_c; break;
  165. case DST_I : s->dct_calc = dst_calc_I_c; break;
  166. }
  167. }
  168. s->dct32 = ff_dct32_float;
  169. if (ARCH_X86)
  170. ff_dct_init_x86(s);
  171. return 0;
  172. }
  173. av_cold void ff_dct_end(DCTContext *s)
  174. {
  175. ff_rdft_end(&s->rdft);
  176. av_freep(&s->csc2);
  177. }