dct32x32_test.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 "./vp9_rtcd.h"
  15. #include "./vpx_config.h"
  16. #include "./vpx_dsp_rtcd.h"
  17. #include "test/acm_random.h"
  18. #include "test/clear_system_state.h"
  19. #include "test/register_state_check.h"
  20. #include "test/util.h"
  21. #include "vp9/common/vp9_entropy.h"
  22. #include "vpx/vpx_codec.h"
  23. #include "vpx/vpx_integer.h"
  24. #include "vpx_ports/mem.h"
  25. #include "vpx_ports/msvc.h" // for round()
  26. using libvpx_test::ACMRandom;
  27. namespace {
  28. const int kNumCoeffs = 1024;
  29. const double kPi = 3.141592653589793238462643383279502884;
  30. void reference_32x32_dct_1d(const double in[32], double out[32]) {
  31. const double kInvSqrt2 = 0.707106781186547524400844362104;
  32. for (int k = 0; k < 32; k++) {
  33. out[k] = 0.0;
  34. for (int n = 0; n < 32; n++) {
  35. out[k] += in[n] * cos(kPi * (2 * n + 1) * k / 64.0);
  36. }
  37. if (k == 0) out[k] = out[k] * kInvSqrt2;
  38. }
  39. }
  40. void reference_32x32_dct_2d(const int16_t input[kNumCoeffs],
  41. double output[kNumCoeffs]) {
  42. // First transform columns
  43. for (int i = 0; i < 32; ++i) {
  44. double temp_in[32], temp_out[32];
  45. for (int j = 0; j < 32; ++j) temp_in[j] = input[j * 32 + i];
  46. reference_32x32_dct_1d(temp_in, temp_out);
  47. for (int j = 0; j < 32; ++j) output[j * 32 + i] = temp_out[j];
  48. }
  49. // Then transform rows
  50. for (int i = 0; i < 32; ++i) {
  51. double temp_in[32], temp_out[32];
  52. for (int j = 0; j < 32; ++j) temp_in[j] = output[j + i * 32];
  53. reference_32x32_dct_1d(temp_in, temp_out);
  54. // Scale by some magic number
  55. for (int j = 0; j < 32; ++j) output[j + i * 32] = temp_out[j] / 4;
  56. }
  57. }
  58. typedef void (*FwdTxfmFunc)(const int16_t *in, tran_low_t *out, int stride);
  59. typedef void (*InvTxfmFunc)(const tran_low_t *in, uint8_t *out, int stride);
  60. typedef std::tr1::tuple<FwdTxfmFunc, InvTxfmFunc, int, vpx_bit_depth_t>
  61. Trans32x32Param;
  62. #if CONFIG_VP9_HIGHBITDEPTH
  63. void idct32x32_10(const tran_low_t *in, uint8_t *out, int stride) {
  64. vpx_highbd_idct32x32_1024_add_c(in, CAST_TO_SHORTPTR(out), stride, 10);
  65. }
  66. void idct32x32_12(const tran_low_t *in, uint8_t *out, int stride) {
  67. vpx_highbd_idct32x32_1024_add_c(in, CAST_TO_SHORTPTR(out), stride, 12);
  68. }
  69. #endif // CONFIG_VP9_HIGHBITDEPTH
  70. class Trans32x32Test : public ::testing::TestWithParam<Trans32x32Param> {
  71. public:
  72. virtual ~Trans32x32Test() {}
  73. virtual void SetUp() {
  74. fwd_txfm_ = GET_PARAM(0);
  75. inv_txfm_ = GET_PARAM(1);
  76. version_ = GET_PARAM(2); // 0: high precision forward transform
  77. // 1: low precision version for rd loop
  78. bit_depth_ = GET_PARAM(3);
  79. mask_ = (1 << bit_depth_) - 1;
  80. }
  81. virtual void TearDown() { libvpx_test::ClearSystemState(); }
  82. protected:
  83. int version_;
  84. vpx_bit_depth_t bit_depth_;
  85. int mask_;
  86. FwdTxfmFunc fwd_txfm_;
  87. InvTxfmFunc inv_txfm_;
  88. };
  89. TEST_P(Trans32x32Test, AccuracyCheck) {
  90. ACMRandom rnd(ACMRandom::DeterministicSeed());
  91. uint32_t max_error = 0;
  92. int64_t total_error = 0;
  93. const int count_test_block = 10000;
  94. DECLARE_ALIGNED(16, int16_t, test_input_block[kNumCoeffs]);
  95. DECLARE_ALIGNED(16, tran_low_t, test_temp_block[kNumCoeffs]);
  96. DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
  97. DECLARE_ALIGNED(16, uint8_t, src[kNumCoeffs]);
  98. #if CONFIG_VP9_HIGHBITDEPTH
  99. DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
  100. DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
  101. #endif
  102. for (int i = 0; i < count_test_block; ++i) {
  103. // Initialize a test block with input range [-mask_, mask_].
  104. for (int j = 0; j < kNumCoeffs; ++j) {
  105. if (bit_depth_ == VPX_BITS_8) {
  106. src[j] = rnd.Rand8();
  107. dst[j] = rnd.Rand8();
  108. test_input_block[j] = src[j] - dst[j];
  109. #if CONFIG_VP9_HIGHBITDEPTH
  110. } else {
  111. src16[j] = rnd.Rand16() & mask_;
  112. dst16[j] = rnd.Rand16() & mask_;
  113. test_input_block[j] = src16[j] - dst16[j];
  114. #endif
  115. }
  116. }
  117. ASM_REGISTER_STATE_CHECK(fwd_txfm_(test_input_block, test_temp_block, 32));
  118. if (bit_depth_ == VPX_BITS_8) {
  119. ASM_REGISTER_STATE_CHECK(inv_txfm_(test_temp_block, dst, 32));
  120. #if CONFIG_VP9_HIGHBITDEPTH
  121. } else {
  122. ASM_REGISTER_STATE_CHECK(
  123. inv_txfm_(test_temp_block, CAST_TO_BYTEPTR(dst16), 32));
  124. #endif
  125. }
  126. for (int j = 0; j < kNumCoeffs; ++j) {
  127. #if CONFIG_VP9_HIGHBITDEPTH
  128. const int32_t diff =
  129. bit_depth_ == VPX_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
  130. #else
  131. const int32_t diff = dst[j] - src[j];
  132. #endif
  133. const uint32_t error = diff * diff;
  134. if (max_error < error) max_error = error;
  135. total_error += error;
  136. }
  137. }
  138. if (version_ == 1) {
  139. max_error /= 2;
  140. total_error /= 45;
  141. }
  142. EXPECT_GE(1u << 2 * (bit_depth_ - 8), max_error)
  143. << "Error: 32x32 FDCT/IDCT has an individual round-trip error > 1";
  144. EXPECT_GE(count_test_block << 2 * (bit_depth_ - 8), total_error)
  145. << "Error: 32x32 FDCT/IDCT has average round-trip error > 1 per block";
  146. }
  147. TEST_P(Trans32x32Test, CoeffCheck) {
  148. ACMRandom rnd(ACMRandom::DeterministicSeed());
  149. const int count_test_block = 1000;
  150. DECLARE_ALIGNED(16, int16_t, input_block[kNumCoeffs]);
  151. DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kNumCoeffs]);
  152. DECLARE_ALIGNED(16, tran_low_t, output_block[kNumCoeffs]);
  153. for (int i = 0; i < count_test_block; ++i) {
  154. for (int j = 0; j < kNumCoeffs; ++j) {
  155. input_block[j] = (rnd.Rand16() & mask_) - (rnd.Rand16() & mask_);
  156. }
  157. const int stride = 32;
  158. vpx_fdct32x32_c(input_block, output_ref_block, stride);
  159. ASM_REGISTER_STATE_CHECK(fwd_txfm_(input_block, output_block, stride));
  160. if (version_ == 0) {
  161. for (int j = 0; j < kNumCoeffs; ++j)
  162. EXPECT_EQ(output_block[j], output_ref_block[j])
  163. << "Error: 32x32 FDCT versions have mismatched coefficients";
  164. } else {
  165. for (int j = 0; j < kNumCoeffs; ++j)
  166. EXPECT_GE(6, abs(output_block[j] - output_ref_block[j]))
  167. << "Error: 32x32 FDCT rd has mismatched coefficients";
  168. }
  169. }
  170. }
  171. TEST_P(Trans32x32Test, MemCheck) {
  172. ACMRandom rnd(ACMRandom::DeterministicSeed());
  173. const int count_test_block = 2000;
  174. DECLARE_ALIGNED(16, int16_t, input_extreme_block[kNumCoeffs]);
  175. DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kNumCoeffs]);
  176. DECLARE_ALIGNED(16, tran_low_t, output_block[kNumCoeffs]);
  177. for (int i = 0; i < count_test_block; ++i) {
  178. // Initialize a test block with input range [-mask_, mask_].
  179. for (int j = 0; j < kNumCoeffs; ++j) {
  180. input_extreme_block[j] = rnd.Rand8() & 1 ? mask_ : -mask_;
  181. }
  182. if (i == 0) {
  183. for (int j = 0; j < kNumCoeffs; ++j) input_extreme_block[j] = mask_;
  184. } else if (i == 1) {
  185. for (int j = 0; j < kNumCoeffs; ++j) input_extreme_block[j] = -mask_;
  186. }
  187. const int stride = 32;
  188. vpx_fdct32x32_c(input_extreme_block, output_ref_block, stride);
  189. ASM_REGISTER_STATE_CHECK(
  190. fwd_txfm_(input_extreme_block, output_block, stride));
  191. // The minimum quant value is 4.
  192. for (int j = 0; j < kNumCoeffs; ++j) {
  193. if (version_ == 0) {
  194. EXPECT_EQ(output_block[j], output_ref_block[j])
  195. << "Error: 32x32 FDCT versions have mismatched coefficients";
  196. } else {
  197. EXPECT_GE(6, abs(output_block[j] - output_ref_block[j]))
  198. << "Error: 32x32 FDCT rd has mismatched coefficients";
  199. }
  200. EXPECT_GE(4 * DCT_MAX_VALUE << (bit_depth_ - 8), abs(output_ref_block[j]))
  201. << "Error: 32x32 FDCT C has coefficient larger than 4*DCT_MAX_VALUE";
  202. EXPECT_GE(4 * DCT_MAX_VALUE << (bit_depth_ - 8), abs(output_block[j]))
  203. << "Error: 32x32 FDCT has coefficient larger than "
  204. << "4*DCT_MAX_VALUE";
  205. }
  206. }
  207. }
  208. TEST_P(Trans32x32Test, InverseAccuracy) {
  209. ACMRandom rnd(ACMRandom::DeterministicSeed());
  210. const int count_test_block = 1000;
  211. DECLARE_ALIGNED(16, int16_t, in[kNumCoeffs]);
  212. DECLARE_ALIGNED(16, tran_low_t, coeff[kNumCoeffs]);
  213. DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
  214. DECLARE_ALIGNED(16, uint8_t, src[kNumCoeffs]);
  215. #if CONFIG_VP9_HIGHBITDEPTH
  216. DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
  217. DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
  218. #endif
  219. for (int i = 0; i < count_test_block; ++i) {
  220. double out_r[kNumCoeffs];
  221. // Initialize a test block with input range [-255, 255]
  222. for (int j = 0; j < kNumCoeffs; ++j) {
  223. if (bit_depth_ == VPX_BITS_8) {
  224. src[j] = rnd.Rand8();
  225. dst[j] = rnd.Rand8();
  226. in[j] = src[j] - dst[j];
  227. #if CONFIG_VP9_HIGHBITDEPTH
  228. } else {
  229. src16[j] = rnd.Rand16() & mask_;
  230. dst16[j] = rnd.Rand16() & mask_;
  231. in[j] = src16[j] - dst16[j];
  232. #endif
  233. }
  234. }
  235. reference_32x32_dct_2d(in, out_r);
  236. for (int j = 0; j < kNumCoeffs; ++j) {
  237. coeff[j] = static_cast<tran_low_t>(round(out_r[j]));
  238. }
  239. if (bit_depth_ == VPX_BITS_8) {
  240. ASM_REGISTER_STATE_CHECK(inv_txfm_(coeff, dst, 32));
  241. #if CONFIG_VP9_HIGHBITDEPTH
  242. } else {
  243. ASM_REGISTER_STATE_CHECK(inv_txfm_(coeff, CAST_TO_BYTEPTR(dst16), 32));
  244. #endif
  245. }
  246. for (int j = 0; j < kNumCoeffs; ++j) {
  247. #if CONFIG_VP9_HIGHBITDEPTH
  248. const int diff =
  249. bit_depth_ == VPX_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
  250. #else
  251. const int diff = dst[j] - src[j];
  252. #endif
  253. const int error = diff * diff;
  254. EXPECT_GE(1, error) << "Error: 32x32 IDCT has error " << error
  255. << " at index " << j;
  256. }
  257. }
  258. }
  259. using std::tr1::make_tuple;
  260. #if CONFIG_VP9_HIGHBITDEPTH
  261. INSTANTIATE_TEST_CASE_P(
  262. C, Trans32x32Test,
  263. ::testing::Values(
  264. make_tuple(&vpx_highbd_fdct32x32_c, &idct32x32_10, 0, VPX_BITS_10),
  265. make_tuple(&vpx_highbd_fdct32x32_rd_c, &idct32x32_10, 1, VPX_BITS_10),
  266. make_tuple(&vpx_highbd_fdct32x32_c, &idct32x32_12, 0, VPX_BITS_12),
  267. make_tuple(&vpx_highbd_fdct32x32_rd_c, &idct32x32_12, 1, VPX_BITS_12),
  268. make_tuple(&vpx_fdct32x32_c, &vpx_idct32x32_1024_add_c, 0, VPX_BITS_8),
  269. make_tuple(&vpx_fdct32x32_rd_c, &vpx_idct32x32_1024_add_c, 1,
  270. VPX_BITS_8)));
  271. #else
  272. INSTANTIATE_TEST_CASE_P(
  273. C, Trans32x32Test,
  274. ::testing::Values(make_tuple(&vpx_fdct32x32_c, &vpx_idct32x32_1024_add_c, 0,
  275. VPX_BITS_8),
  276. make_tuple(&vpx_fdct32x32_rd_c, &vpx_idct32x32_1024_add_c,
  277. 1, VPX_BITS_8)));
  278. #endif // CONFIG_VP9_HIGHBITDEPTH
  279. #if HAVE_NEON && !CONFIG_EMULATE_HARDWARE
  280. INSTANTIATE_TEST_CASE_P(
  281. NEON, Trans32x32Test,
  282. ::testing::Values(make_tuple(&vpx_fdct32x32_neon,
  283. &vpx_idct32x32_1024_add_neon, 0, VPX_BITS_8),
  284. make_tuple(&vpx_fdct32x32_rd_neon,
  285. &vpx_idct32x32_1024_add_neon, 1, VPX_BITS_8)));
  286. #endif // HAVE_NEON && !CONFIG_EMULATE_HARDWARE
  287. #if HAVE_SSE2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
  288. INSTANTIATE_TEST_CASE_P(
  289. SSE2, Trans32x32Test,
  290. ::testing::Values(make_tuple(&vpx_fdct32x32_sse2,
  291. &vpx_idct32x32_1024_add_sse2, 0, VPX_BITS_8),
  292. make_tuple(&vpx_fdct32x32_rd_sse2,
  293. &vpx_idct32x32_1024_add_sse2, 1, VPX_BITS_8)));
  294. #endif // HAVE_SSE2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
  295. #if HAVE_SSE2 && CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
  296. INSTANTIATE_TEST_CASE_P(
  297. SSE2, Trans32x32Test,
  298. ::testing::Values(
  299. make_tuple(&vpx_highbd_fdct32x32_sse2, &idct32x32_10, 0, VPX_BITS_10),
  300. make_tuple(&vpx_highbd_fdct32x32_rd_sse2, &idct32x32_10, 1,
  301. VPX_BITS_10),
  302. make_tuple(&vpx_highbd_fdct32x32_sse2, &idct32x32_12, 0, VPX_BITS_12),
  303. make_tuple(&vpx_highbd_fdct32x32_rd_sse2, &idct32x32_12, 1,
  304. VPX_BITS_12),
  305. make_tuple(&vpx_fdct32x32_sse2, &vpx_idct32x32_1024_add_c, 0,
  306. VPX_BITS_8),
  307. make_tuple(&vpx_fdct32x32_rd_sse2, &vpx_idct32x32_1024_add_c, 1,
  308. VPX_BITS_8)));
  309. #endif // HAVE_SSE2 && CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
  310. #if HAVE_AVX2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
  311. INSTANTIATE_TEST_CASE_P(
  312. AVX2, Trans32x32Test,
  313. ::testing::Values(make_tuple(&vpx_fdct32x32_avx2,
  314. &vpx_idct32x32_1024_add_sse2, 0, VPX_BITS_8),
  315. make_tuple(&vpx_fdct32x32_rd_avx2,
  316. &vpx_idct32x32_1024_add_sse2, 1, VPX_BITS_8)));
  317. #endif // HAVE_AVX2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
  318. #if HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
  319. INSTANTIATE_TEST_CASE_P(
  320. MSA, Trans32x32Test,
  321. ::testing::Values(make_tuple(&vpx_fdct32x32_msa,
  322. &vpx_idct32x32_1024_add_msa, 0, VPX_BITS_8),
  323. make_tuple(&vpx_fdct32x32_rd_msa,
  324. &vpx_idct32x32_1024_add_msa, 1, VPX_BITS_8)));
  325. #endif // HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
  326. } // namespace