vp8_fdct4x4_test.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2013 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 <stddef.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include "third_party/googletest/src/include/gtest/gtest.h"
  17. #include "./vpx_config.h"
  18. #include "./vp8_rtcd.h"
  19. #include "test/acm_random.h"
  20. #include "vpx/vpx_integer.h"
  21. #include "vpx_ports/mem.h"
  22. namespace {
  23. typedef void (*FdctFunc)(int16_t *a, int16_t *b, int a_stride);
  24. const int cospi8sqrt2minus1 = 20091;
  25. const int sinpi8sqrt2 = 35468;
  26. void reference_idct4x4(const int16_t *input, int16_t *output) {
  27. const int16_t *ip = input;
  28. int16_t *op = output;
  29. for (int i = 0; i < 4; ++i) {
  30. const int a1 = ip[0] + ip[8];
  31. const int b1 = ip[0] - ip[8];
  32. const int temp1 = (ip[4] * sinpi8sqrt2) >> 16;
  33. const int temp2 = ip[12] + ((ip[12] * cospi8sqrt2minus1) >> 16);
  34. const int c1 = temp1 - temp2;
  35. const int temp3 = ip[4] + ((ip[4] * cospi8sqrt2minus1) >> 16);
  36. const int temp4 = (ip[12] * sinpi8sqrt2) >> 16;
  37. const int d1 = temp3 + temp4;
  38. op[0] = a1 + d1;
  39. op[12] = a1 - d1;
  40. op[4] = b1 + c1;
  41. op[8] = b1 - c1;
  42. ++ip;
  43. ++op;
  44. }
  45. ip = output;
  46. op = output;
  47. for (int i = 0; i < 4; ++i) {
  48. const int a1 = ip[0] + ip[2];
  49. const int b1 = ip[0] - ip[2];
  50. const int temp1 = (ip[1] * sinpi8sqrt2) >> 16;
  51. const int temp2 = ip[3] + ((ip[3] * cospi8sqrt2minus1) >> 16);
  52. const int c1 = temp1 - temp2;
  53. const int temp3 = ip[1] + ((ip[1] * cospi8sqrt2minus1) >> 16);
  54. const int temp4 = (ip[3] * sinpi8sqrt2) >> 16;
  55. const int d1 = temp3 + temp4;
  56. op[0] = (a1 + d1 + 4) >> 3;
  57. op[3] = (a1 - d1 + 4) >> 3;
  58. op[1] = (b1 + c1 + 4) >> 3;
  59. op[2] = (b1 - c1 + 4) >> 3;
  60. ip += 4;
  61. op += 4;
  62. }
  63. }
  64. using libvpx_test::ACMRandom;
  65. class FdctTest : public ::testing::TestWithParam<FdctFunc> {
  66. public:
  67. virtual void SetUp() {
  68. fdct_func_ = GetParam();
  69. rnd_.Reset(ACMRandom::DeterministicSeed());
  70. }
  71. protected:
  72. FdctFunc fdct_func_;
  73. ACMRandom rnd_;
  74. };
  75. TEST_P(FdctTest, SignBiasCheck) {
  76. int16_t test_input_block[16];
  77. DECLARE_ALIGNED(16, int16_t, test_output_block[16]);
  78. const int pitch = 8;
  79. int count_sign_block[16][2];
  80. const int count_test_block = 1000000;
  81. memset(count_sign_block, 0, sizeof(count_sign_block));
  82. for (int i = 0; i < count_test_block; ++i) {
  83. // Initialize a test block with input range [-255, 255].
  84. for (int j = 0; j < 16; ++j) {
  85. test_input_block[j] = rnd_.Rand8() - rnd_.Rand8();
  86. }
  87. fdct_func_(test_input_block, test_output_block, pitch);
  88. for (int j = 0; j < 16; ++j) {
  89. if (test_output_block[j] < 0) {
  90. ++count_sign_block[j][0];
  91. } else if (test_output_block[j] > 0) {
  92. ++count_sign_block[j][1];
  93. }
  94. }
  95. }
  96. bool bias_acceptable = true;
  97. for (int j = 0; j < 16; ++j) {
  98. bias_acceptable =
  99. bias_acceptable &&
  100. (abs(count_sign_block[j][0] - count_sign_block[j][1]) < 10000);
  101. }
  102. EXPECT_EQ(true, bias_acceptable)
  103. << "Error: 4x4 FDCT has a sign bias > 1% for input range [-255, 255]";
  104. memset(count_sign_block, 0, sizeof(count_sign_block));
  105. for (int i = 0; i < count_test_block; ++i) {
  106. // Initialize a test block with input range [-15, 15].
  107. for (int j = 0; j < 16; ++j) {
  108. test_input_block[j] = (rnd_.Rand8() >> 4) - (rnd_.Rand8() >> 4);
  109. }
  110. fdct_func_(test_input_block, test_output_block, pitch);
  111. for (int j = 0; j < 16; ++j) {
  112. if (test_output_block[j] < 0) {
  113. ++count_sign_block[j][0];
  114. } else if (test_output_block[j] > 0) {
  115. ++count_sign_block[j][1];
  116. }
  117. }
  118. }
  119. bias_acceptable = true;
  120. for (int j = 0; j < 16; ++j) {
  121. bias_acceptable =
  122. bias_acceptable &&
  123. (abs(count_sign_block[j][0] - count_sign_block[j][1]) < 100000);
  124. }
  125. EXPECT_EQ(true, bias_acceptable)
  126. << "Error: 4x4 FDCT has a sign bias > 10% for input range [-15, 15]";
  127. };
  128. TEST_P(FdctTest, RoundTripErrorCheck) {
  129. int max_error = 0;
  130. double total_error = 0;
  131. const int count_test_block = 1000000;
  132. for (int i = 0; i < count_test_block; ++i) {
  133. int16_t test_input_block[16];
  134. int16_t test_output_block[16];
  135. DECLARE_ALIGNED(16, int16_t, test_temp_block[16]);
  136. // Initialize a test block with input range [-255, 255].
  137. for (int j = 0; j < 16; ++j) {
  138. test_input_block[j] = rnd_.Rand8() - rnd_.Rand8();
  139. }
  140. const int pitch = 8;
  141. fdct_func_(test_input_block, test_temp_block, pitch);
  142. reference_idct4x4(test_temp_block, test_output_block);
  143. for (int j = 0; j < 16; ++j) {
  144. const int diff = test_input_block[j] - test_output_block[j];
  145. const int error = diff * diff;
  146. if (max_error < error) max_error = error;
  147. total_error += error;
  148. }
  149. }
  150. EXPECT_GE(1, max_error)
  151. << "Error: FDCT/IDCT has an individual roundtrip error > 1";
  152. EXPECT_GE(count_test_block, total_error)
  153. << "Error: FDCT/IDCT has average roundtrip error > 1 per block";
  154. };
  155. INSTANTIATE_TEST_CASE_P(C, FdctTest, ::testing::Values(vp8_short_fdct4x4_c));
  156. #if HAVE_NEON
  157. INSTANTIATE_TEST_CASE_P(NEON, FdctTest,
  158. ::testing::Values(vp8_short_fdct4x4_neon));
  159. #endif // HAVE_NEON
  160. #if HAVE_SSE2
  161. INSTANTIATE_TEST_CASE_P(SSE2, FdctTest,
  162. ::testing::Values(vp8_short_fdct4x4_sse2));
  163. #endif // HAVE_SSE2
  164. #if HAVE_MSA
  165. INSTANTIATE_TEST_CASE_P(MSA, FdctTest,
  166. ::testing::Values(vp8_short_fdct4x4_msa));
  167. #endif // HAVE_MSA
  168. #if HAVE_MMI
  169. INSTANTIATE_TEST_CASE_P(MMI, FdctTest,
  170. ::testing::Values(vp8_short_fdct4x4_mmi));
  171. #endif // HAVE_MMI
  172. } // namespace