dctref.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * reference discrete cosine transform (double precision)
  3. * Copyright (C) 2009 Dylan Yudaken
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libavcodec/dctref.c
  23. * reference discrete cosine transform (double precision)
  24. *
  25. * @author Dylan Yudaken (dyudaken at gmail)
  26. *
  27. * @note This file could be optimized a lot, but is for
  28. * reference and so readability is better.
  29. */
  30. #include "libavutil/mathematics.h"
  31. static double coefficients[8 * 8];
  32. /**
  33. * Initialize the double precision discrete cosine transform
  34. * functions fdct & idct.
  35. */
  36. av_cold void ff_ref_dct_init(void)
  37. {
  38. unsigned int i, j;
  39. for (j = 0; j < 8; ++j) {
  40. coefficients[j] = sqrt(0.125);
  41. for (i = 8; i < 64; i += 8) {
  42. coefficients[i + j] = 0.5 * cos(i * (j + 0.5) * M_PI / 64.0);
  43. }
  44. }
  45. }
  46. /**
  47. * Transform 8x8 block of data with a double precision forward DCT <br>
  48. * This is a reference implementation.
  49. *
  50. * @param block pointer to 8x8 block of data to transform
  51. */
  52. void ff_ref_fdct(short *block)
  53. {
  54. /* implement the equation: block = coefficients * block * coefficients' */
  55. unsigned int i, j, k;
  56. double out[8 * 8];
  57. /* out = coefficients * block */
  58. for (i = 0; i < 64; i += 8) {
  59. for (j = 0; j < 8; ++j) {
  60. double tmp = 0;
  61. for (k = 0; k < 8; ++k) {
  62. tmp += coefficients[i + k] * block[k * 8 + j];
  63. }
  64. out[i + j] = tmp * 8;
  65. }
  66. }
  67. /* block = out * (coefficients') */
  68. for (j = 0; j < 8; ++j) {
  69. for (i = 0; i < 64; i += 8) {
  70. double tmp = 0;
  71. for (k = 0; k < 8; ++k) {
  72. tmp += out[i + k] * coefficients[j * 8 + k];
  73. }
  74. block[i + j] = floor(tmp + 0.499999999999);
  75. }
  76. }
  77. }
  78. /**
  79. * Transform 8x8 block of data with a double precision inverse DCT <br>
  80. * This is a reference implementation.
  81. *
  82. * @param block pointer to 8x8 block of data to transform
  83. */
  84. void ff_ref_idct(short *block)
  85. {
  86. /* implement the equation: block = (coefficients') * block * coefficients */
  87. unsigned int i, j, k;
  88. double out[8 * 8];
  89. /* out = block * coefficients */
  90. for (i = 0; i < 64; i += 8) {
  91. for (j = 0; j < 8; ++j) {
  92. double tmp = 0;
  93. for (k = 0; k < 8; ++k) {
  94. tmp += block[i + k] * coefficients[k * 8 + j];
  95. }
  96. out[i + j] = tmp;
  97. }
  98. }
  99. /* block = (coefficients') * out */
  100. for (i = 0; i < 8; ++i) {
  101. for (j = 0; j < 8; ++j) {
  102. double tmp = 0;
  103. for (k = 0; k < 64; k += 8) {
  104. tmp += coefficients[k + i] * out[k + j];
  105. }
  106. block[i * 8 + j] = floor(tmp + 0.5);
  107. }
  108. }
  109. }