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