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.

124 lines
3.4KB

  1. /*
  2. * reference discrete cosine transform (double precision)
  3. * Copyright (C) 2009 Dylan Yudaken
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  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. #include "dctref.h"
  32. static double coefficients[8 * 8];
  33. /**
  34. * Initialize the double precision discrete cosine transform
  35. * functions fdct & idct.
  36. */
  37. av_cold void ff_ref_dct_init(void)
  38. {
  39. unsigned int i, j;
  40. for (j = 0; j < 8; ++j) {
  41. coefficients[j] = sqrt(0.125);
  42. for (i = 8; i < 64; i += 8) {
  43. coefficients[i + j] = 0.5 * cos(i * (j + 0.5) * M_PI / 64.0);
  44. }
  45. }
  46. }
  47. /**
  48. * Transform 8x8 block of data with a double precision forward DCT <br>
  49. * This is a reference implementation.
  50. *
  51. * @param block pointer to 8x8 block of data to transform
  52. */
  53. void ff_ref_fdct(short *block)
  54. {
  55. /* implement the equation: block = coefficients * block * coefficients' */
  56. unsigned int i, j, k;
  57. double out[8 * 8];
  58. /* out = coefficients * block */
  59. for (i = 0; i < 64; i += 8) {
  60. for (j = 0; j < 8; ++j) {
  61. double tmp = 0;
  62. for (k = 0; k < 8; ++k) {
  63. tmp += coefficients[i + k] * block[k * 8 + j];
  64. }
  65. out[i + j] = tmp * 8;
  66. }
  67. }
  68. /* block = out * (coefficients') */
  69. for (j = 0; j < 8; ++j) {
  70. for (i = 0; i < 64; i += 8) {
  71. double tmp = 0;
  72. for (k = 0; k < 8; ++k) {
  73. tmp += out[i + k] * coefficients[j * 8 + k];
  74. }
  75. block[i + j] = floor(tmp + 0.499999999999);
  76. }
  77. }
  78. }
  79. /**
  80. * Transform 8x8 block of data with a double precision inverse DCT <br>
  81. * This is a reference implementation.
  82. *
  83. * @param block pointer to 8x8 block of data to transform
  84. */
  85. void ff_ref_idct(short *block)
  86. {
  87. /* implement the equation: block = (coefficients') * block * coefficients */
  88. unsigned int i, j, k;
  89. double out[8 * 8];
  90. /* out = block * coefficients */
  91. for (i = 0; i < 64; i += 8) {
  92. for (j = 0; j < 8; ++j) {
  93. double tmp = 0;
  94. for (k = 0; k < 8; ++k) {
  95. tmp += block[i + k] * coefficients[k * 8 + j];
  96. }
  97. out[i + j] = tmp;
  98. }
  99. }
  100. /* block = (coefficients') * out */
  101. for (i = 0; i < 8; ++i) {
  102. for (j = 0; j < 8; ++j) {
  103. double tmp = 0;
  104. for (k = 0; k < 64; k += 8) {
  105. tmp += coefficients[k + i] * out[k + j];
  106. }
  107. block[i * 8 + j] = floor(tmp + 0.5);
  108. }
  109. }
  110. }