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.

140 lines
3.5KB

  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 libavcodec/dct.c
  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 "dsputil.h"
  30. av_cold int ff_dct_init(DCTContext *s, int nbits, int inverse)
  31. {
  32. int n = 1 << nbits;
  33. int i;
  34. s->nbits = nbits;
  35. s->inverse = inverse;
  36. ff_init_ff_cos_tabs(nbits+2);
  37. s->costab = ff_cos_tabs[nbits+2];
  38. s->csc2 = av_malloc(n/2 * sizeof(FFTSample));
  39. if (ff_rdft_init(&s->rdft, nbits, inverse) < 0) {
  40. av_free(s->csc2);
  41. return -1;
  42. }
  43. for (i = 0; i < n/2; i++)
  44. s->csc2[i] = 0.5 / sin((M_PI / (2*n) * (2*i + 1)));
  45. return 0;
  46. }
  47. /* sin((M_PI * x / (2*n)) */
  48. #define SIN(s,n,x) (s->costab[(n) - (x)])
  49. /* cos((M_PI * x / (2*n)) */
  50. #define COS(s,n,x) (s->costab[x])
  51. static void ff_dct_calc_c(DCTContext *ctx, FFTSample *data)
  52. {
  53. int n = 1 << ctx->nbits;
  54. int i;
  55. if (ctx->inverse) {
  56. float next = data[n - 1];
  57. float inv_n = 1.0f / n;
  58. for (i = n - 2; i >= 2; i -= 2) {
  59. float val1 = data[i ];
  60. float val2 = data[i - 1] - data[i + 1];
  61. float c = COS(ctx, n, i);
  62. float s = SIN(ctx, n, i);
  63. data[i ] = c * val1 + s * val2;
  64. data[i + 1] = s * val1 - c * val2;
  65. }
  66. data[1] = 2 * next;
  67. ff_rdft_calc(&ctx->rdft, data);
  68. for (i = 0; i < n / 2; i++) {
  69. float tmp1 = data[i ] * inv_n;
  70. float tmp2 = data[n - i - 1] * inv_n;
  71. float csc = ctx->csc2[i] * (tmp1 - tmp2);
  72. tmp1 += tmp2;
  73. data[i ] = tmp1 + csc;
  74. data[n - i - 1] = tmp1 - csc;
  75. }
  76. } else {
  77. float next;
  78. for (i=0; i < n/2; i++) {
  79. float tmp1 = data[i ];
  80. float tmp2 = data[n - i - 1];
  81. float s = SIN(ctx, n, 2*i + 1);
  82. s *= tmp1 - tmp2;
  83. tmp1 = (tmp1 + tmp2) * 0.5f;
  84. data[i ] = tmp1 + s;
  85. data[n-i-1] = tmp1 - s;
  86. }
  87. ff_rdft_calc(&ctx->rdft, data);
  88. next = data[1] * 0.5;
  89. data[1] *= -1;
  90. for (i = n - 2; i >= 0; i -= 2) {
  91. float inr = data[i ];
  92. float ini = data[i + 1];
  93. float c = COS(ctx, n, i);
  94. float s = SIN(ctx, n, i);
  95. data[i ] = c * inr + s * ini;
  96. data[i+1] = next;
  97. next += s * inr - c * ini;
  98. }
  99. }
  100. }
  101. void ff_dct_calc(DCTContext *s, FFTSample *data)
  102. {
  103. ff_dct_calc_c(s, data);
  104. }
  105. av_cold void ff_dct_end(DCTContext *s)
  106. {
  107. ff_rdft_end(&s->rdft);
  108. av_free(s->csc2);
  109. }