idct8x8_test.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "third_party/googletest/src/include/gtest/gtest.h"
  14. #include "./vpx_dsp_rtcd.h"
  15. #include "test/acm_random.h"
  16. #include "vpx/vpx_integer.h"
  17. #include "vpx_ports/msvc.h" // for round()
  18. using libvpx_test::ACMRandom;
  19. namespace {
  20. void reference_dct_1d(double input[8], double output[8]) {
  21. const double kPi = 3.141592653589793238462643383279502884;
  22. const double kInvSqrt2 = 0.707106781186547524400844362104;
  23. for (int k = 0; k < 8; k++) {
  24. output[k] = 0.0;
  25. for (int n = 0; n < 8; n++) {
  26. output[k] += input[n] * cos(kPi * (2 * n + 1) * k / 16.0);
  27. }
  28. if (k == 0) output[k] = output[k] * kInvSqrt2;
  29. }
  30. }
  31. void reference_dct_2d(int16_t input[64], double output[64]) {
  32. // First transform columns
  33. for (int i = 0; i < 8; ++i) {
  34. double temp_in[8], temp_out[8];
  35. for (int j = 0; j < 8; ++j) temp_in[j] = input[j * 8 + i];
  36. reference_dct_1d(temp_in, temp_out);
  37. for (int j = 0; j < 8; ++j) output[j * 8 + i] = temp_out[j];
  38. }
  39. // Then transform rows
  40. for (int i = 0; i < 8; ++i) {
  41. double temp_in[8], temp_out[8];
  42. for (int j = 0; j < 8; ++j) temp_in[j] = output[j + i * 8];
  43. reference_dct_1d(temp_in, temp_out);
  44. for (int j = 0; j < 8; ++j) output[j + i * 8] = temp_out[j];
  45. }
  46. // Scale by some magic number
  47. for (int i = 0; i < 64; ++i) output[i] *= 2;
  48. }
  49. TEST(VP9Idct8x8Test, AccuracyCheck) {
  50. ACMRandom rnd(ACMRandom::DeterministicSeed());
  51. const int count_test_block = 10000;
  52. for (int i = 0; i < count_test_block; ++i) {
  53. int16_t input[64];
  54. tran_low_t coeff[64];
  55. double output_r[64];
  56. uint8_t dst[64], src[64];
  57. for (int j = 0; j < 64; ++j) {
  58. src[j] = rnd.Rand8();
  59. dst[j] = rnd.Rand8();
  60. }
  61. // Initialize a test block with input range [-255, 255].
  62. for (int j = 0; j < 64; ++j) input[j] = src[j] - dst[j];
  63. reference_dct_2d(input, output_r);
  64. for (int j = 0; j < 64; ++j) {
  65. coeff[j] = static_cast<tran_low_t>(round(output_r[j]));
  66. }
  67. vpx_idct8x8_64_add_c(coeff, dst, 8);
  68. for (int j = 0; j < 64; ++j) {
  69. const int diff = dst[j] - src[j];
  70. const int error = diff * diff;
  71. EXPECT_GE(1, error) << "Error: 8x8 FDCT/IDCT has error " << error
  72. << " at index " << j;
  73. }
  74. }
  75. }
  76. } // namespace