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