quantize_test.cc 6.0 KB

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