quantize_test.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (c) 2014 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 <string.h>
  11. #include <tuple>
  12. #include "third_party/googletest/src/include/gtest/gtest.h"
  13. #include "./vp8_rtcd.h"
  14. #include "./vpx_config.h"
  15. #include "test/acm_random.h"
  16. #include "test/bench.h"
  17. #include "test/clear_system_state.h"
  18. #include "test/register_state_check.h"
  19. #include "test/util.h"
  20. #include "vp8/common/blockd.h"
  21. #include "vp8/common/onyx.h"
  22. #include "vp8/encoder/block.h"
  23. #include "vp8/encoder/onyx_int.h"
  24. #include "vp8/encoder/quantize.h"
  25. #include "vpx/vpx_integer.h"
  26. #include "vpx_mem/vpx_mem.h"
  27. namespace {
  28. const int kNumBlocks = 25;
  29. const int kNumBlockEntries = 16;
  30. typedef void (*VP8Quantize)(BLOCK *b, BLOCKD *d);
  31. typedef std::tuple<VP8Quantize, VP8Quantize> VP8QuantizeParam;
  32. using libvpx_test::ACMRandom;
  33. using std::make_tuple;
  34. // Create and populate a VP8_COMP instance which has a complete set of
  35. // quantization inputs as well as a second MACROBLOCKD for output.
  36. class QuantizeTestBase {
  37. public:
  38. virtual ~QuantizeTestBase() {
  39. vp8_remove_compressor(&vp8_comp_);
  40. vp8_comp_ = NULL;
  41. vpx_free(macroblockd_dst_);
  42. macroblockd_dst_ = NULL;
  43. libvpx_test::ClearSystemState();
  44. }
  45. protected:
  46. void SetupCompressor() {
  47. rnd_.Reset(ACMRandom::DeterministicSeed());
  48. // The full configuration is necessary to generate the quantization tables.
  49. VP8_CONFIG vp8_config;
  50. memset(&vp8_config, 0, sizeof(vp8_config));
  51. vp8_comp_ = vp8_create_compressor(&vp8_config);
  52. // Set the tables based on a quantizer of 0.
  53. vp8_set_quantizer(vp8_comp_, 0);
  54. // Set up all the block/blockd pointers for the mb in vp8_comp_.
  55. vp8cx_frame_init_quantizer(vp8_comp_);
  56. // Copy macroblockd from the reference to get pre-set-up dequant values.
  57. macroblockd_dst_ = reinterpret_cast<MACROBLOCKD *>(
  58. vpx_memalign(32, sizeof(*macroblockd_dst_)));
  59. memcpy(macroblockd_dst_, &vp8_comp_->mb.e_mbd, sizeof(*macroblockd_dst_));
  60. // Fix block pointers - currently they point to the blocks in the reference
  61. // structure.
  62. vp8_setup_block_dptrs(macroblockd_dst_);
  63. }
  64. void UpdateQuantizer(int q) {
  65. vp8_set_quantizer(vp8_comp_, q);
  66. memcpy(macroblockd_dst_, &vp8_comp_->mb.e_mbd, sizeof(*macroblockd_dst_));
  67. vp8_setup_block_dptrs(macroblockd_dst_);
  68. }
  69. void FillCoeffConstant(int16_t c) {
  70. for (int i = 0; i < kNumBlocks * kNumBlockEntries; ++i) {
  71. vp8_comp_->mb.coeff[i] = c;
  72. }
  73. }
  74. void FillCoeffRandom() {
  75. for (int i = 0; i < kNumBlocks * kNumBlockEntries; ++i) {
  76. vp8_comp_->mb.coeff[i] = rnd_.Rand8();
  77. }
  78. }
  79. void CheckOutput() {
  80. EXPECT_EQ(0, memcmp(vp8_comp_->mb.e_mbd.qcoeff, macroblockd_dst_->qcoeff,
  81. sizeof(*macroblockd_dst_->qcoeff) * kNumBlocks *
  82. kNumBlockEntries))
  83. << "qcoeff mismatch";
  84. EXPECT_EQ(0, memcmp(vp8_comp_->mb.e_mbd.dqcoeff, macroblockd_dst_->dqcoeff,
  85. sizeof(*macroblockd_dst_->dqcoeff) * kNumBlocks *
  86. kNumBlockEntries))
  87. << "dqcoeff mismatch";
  88. EXPECT_EQ(0, memcmp(vp8_comp_->mb.e_mbd.eobs, macroblockd_dst_->eobs,
  89. sizeof(*macroblockd_dst_->eobs) * kNumBlocks))
  90. << "eobs mismatch";
  91. }
  92. VP8_COMP *vp8_comp_;
  93. MACROBLOCKD *macroblockd_dst_;
  94. private:
  95. ACMRandom rnd_;
  96. };
  97. class QuantizeTest : public QuantizeTestBase,
  98. public ::testing::TestWithParam<VP8QuantizeParam>,
  99. public AbstractBench {
  100. protected:
  101. virtual void SetUp() {
  102. SetupCompressor();
  103. asm_quant_ = GET_PARAM(0);
  104. c_quant_ = GET_PARAM(1);
  105. }
  106. virtual void Run() {
  107. asm_quant_(&vp8_comp_->mb.block[0], &macroblockd_dst_->block[0]);
  108. }
  109. void RunComparison() {
  110. for (int i = 0; i < kNumBlocks; ++i) {
  111. ASM_REGISTER_STATE_CHECK(
  112. c_quant_(&vp8_comp_->mb.block[i], &vp8_comp_->mb.e_mbd.block[i]));
  113. ASM_REGISTER_STATE_CHECK(
  114. asm_quant_(&vp8_comp_->mb.block[i], &macroblockd_dst_->block[i]));
  115. }
  116. CheckOutput();
  117. }
  118. private:
  119. VP8Quantize asm_quant_;
  120. VP8Quantize c_quant_;
  121. };
  122. TEST_P(QuantizeTest, TestZeroInput) {
  123. FillCoeffConstant(0);
  124. RunComparison();
  125. }
  126. TEST_P(QuantizeTest, TestLargeNegativeInput) {
  127. FillCoeffConstant(0);
  128. // Generate a qcoeff which contains 512/-512 (0x0100/0xFE00) to catch issues
  129. // like BUG=883 where the constant being compared was incorrectly initialized.
  130. vp8_comp_->mb.coeff[0] = -8191;
  131. RunComparison();
  132. }
  133. TEST_P(QuantizeTest, TestRandomInput) {
  134. FillCoeffRandom();
  135. RunComparison();
  136. }
  137. TEST_P(QuantizeTest, TestMultipleQ) {
  138. for (int q = 0; q < QINDEX_RANGE; ++q) {
  139. UpdateQuantizer(q);
  140. FillCoeffRandom();
  141. RunComparison();
  142. }
  143. }
  144. TEST_P(QuantizeTest, DISABLED_Speed) {
  145. FillCoeffRandom();
  146. RunNTimes(10000000);
  147. PrintMedian("vp8 quantize");
  148. }
  149. #if HAVE_SSE2
  150. INSTANTIATE_TEST_CASE_P(
  151. SSE2, QuantizeTest,
  152. ::testing::Values(
  153. make_tuple(&vp8_fast_quantize_b_sse2, &vp8_fast_quantize_b_c),
  154. make_tuple(&vp8_regular_quantize_b_sse2, &vp8_regular_quantize_b_c)));
  155. #endif // HAVE_SSE2
  156. #if HAVE_SSSE3
  157. INSTANTIATE_TEST_CASE_P(SSSE3, QuantizeTest,
  158. ::testing::Values(make_tuple(&vp8_fast_quantize_b_ssse3,
  159. &vp8_fast_quantize_b_c)));
  160. #endif // HAVE_SSSE3
  161. #if HAVE_SSE4_1
  162. INSTANTIATE_TEST_CASE_P(
  163. SSE4_1, QuantizeTest,
  164. ::testing::Values(make_tuple(&vp8_regular_quantize_b_sse4_1,
  165. &vp8_regular_quantize_b_c)));
  166. #endif // HAVE_SSE4_1
  167. #if HAVE_NEON
  168. INSTANTIATE_TEST_CASE_P(NEON, QuantizeTest,
  169. ::testing::Values(make_tuple(&vp8_fast_quantize_b_neon,
  170. &vp8_fast_quantize_b_c)));
  171. #endif // HAVE_NEON
  172. #if HAVE_MSA
  173. INSTANTIATE_TEST_CASE_P(
  174. MSA, QuantizeTest,
  175. ::testing::Values(
  176. make_tuple(&vp8_fast_quantize_b_msa, &vp8_fast_quantize_b_c),
  177. make_tuple(&vp8_regular_quantize_b_msa, &vp8_regular_quantize_b_c)));
  178. #endif // HAVE_MSA
  179. #if HAVE_MMI
  180. INSTANTIATE_TEST_CASE_P(
  181. MMI, QuantizeTest,
  182. ::testing::Values(
  183. make_tuple(&vp8_fast_quantize_b_mmi, &vp8_fast_quantize_b_c),
  184. make_tuple(&vp8_regular_quantize_b_mmi, &vp8_regular_quantize_b_c)));
  185. #endif // HAVE_MMI
  186. } // namespace