]> granicus.if.org Git - libvpx/blob - test/quantize_test.cc
Align structures in quantize test
[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
13 #include "third_party/googletest/src/include/gtest/gtest.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
19 #include "./vpx_config.h"
20 #include "./vp8_rtcd.h"
21 #include "vp8/common/blockd.h"
22 #include "vp8/common/onyx.h"
23 #include "vp8/encoder/block.h"
24 #include "vp8/encoder/onyx_int.h"
25 #include "vp8/encoder/quantize.h"
26 #include "vpx/vpx_integer.h"
27 #include "vpx_mem/vpx_mem.h"
28
29 namespace {
30
31 const int kNumBlocks = 25;
32 const int kNumBlockEntries = 16;
33
34 typedef void (*VP8Quantize)(BLOCK *b, BLOCKD *d);
35 typedef void (*VP8QuantizePair)(BLOCK *b0, BLOCK *b1, BLOCKD *d0, BLOCKD *d1);
36
37 typedef std::tr1::tuple<VP8Quantize, VP8Quantize> VP8QuantizeParam;
38 typedef std::tr1::tuple<VP8QuantizePair, VP8QuantizePair> VP8QuantizePairParam;
39
40 using libvpx_test::ACMRandom;
41 using std::tr1::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_ = NULL;
50     vpx_free(macroblockd_dst_);
51     macroblockd_dst_ = NULL;
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 *const vp8_config =
61         reinterpret_cast<VP8_CONFIG *>(vpx_calloc(sizeof(*vp8_config), 1));
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     vpx_memcpy(macroblockd_dst_, &vp8_comp_->mb.e_mbd,
75                sizeof(*macroblockd_dst_));
76     // Fix block pointers - currently they point to the blocks in the reference
77     // structure.
78     vp8_setup_block_dptrs(macroblockd_dst_);
79   }
80
81   void UpdateQuantizer(int q) {
82     vp8_set_quantizer(vp8_comp_, q);
83
84     vpx_memcpy(macroblockd_dst_, &vp8_comp_->mb.e_mbd,
85                sizeof(*macroblockd_dst_));
86     vp8_setup_block_dptrs(macroblockd_dst_);
87   }
88
89   void FillCoeffConstant(int16_t c) {
90     for (int i = 0; i < kNumBlocks * kNumBlockEntries; ++i) {
91       vp8_comp_->mb.coeff[i] = c;
92     }
93   }
94
95   void FillCoeffRandom() {
96     for (int i = 0; i < kNumBlocks * kNumBlockEntries; ++i) {
97       vp8_comp_->mb.coeff[i] = rnd_.Rand8();
98     }
99   }
100
101   void CheckOutput() {
102     EXPECT_EQ(0, memcmp(vp8_comp_->mb.e_mbd.qcoeff, macroblockd_dst_->qcoeff,
103                         sizeof(*macroblockd_dst_->qcoeff) * kNumBlocks *
104                             kNumBlockEntries))
105         << "qcoeff mismatch";
106     EXPECT_EQ(0, memcmp(vp8_comp_->mb.e_mbd.dqcoeff, macroblockd_dst_->dqcoeff,
107                         sizeof(*macroblockd_dst_->dqcoeff) * kNumBlocks *
108                             kNumBlockEntries))
109         << "dqcoeff mismatch";
110     EXPECT_EQ(0, memcmp(vp8_comp_->mb.e_mbd.eobs, macroblockd_dst_->eobs,
111                         sizeof(*macroblockd_dst_->eobs) * kNumBlocks))
112         << "eobs mismatch";
113   }
114
115   VP8_COMP *vp8_comp_;
116   MACROBLOCKD *macroblockd_dst_;
117
118  private:
119   ACMRandom rnd_;
120 };
121
122 class QuantizeTest : public QuantizeTestBase,
123                      public ::testing::TestWithParam<VP8QuantizeParam> {
124  protected:
125   virtual void SetUp() {
126     SetupCompressor();
127     asm_quant_ = GET_PARAM(0);
128     c_quant_ = GET_PARAM(1);
129   }
130
131   void RunComparison() {
132     for (int i = 0; i < kNumBlocks; ++i) {
133       ASM_REGISTER_STATE_CHECK(
134           c_quant_(&vp8_comp_->mb.block[i], &vp8_comp_->mb.e_mbd.block[i]));
135       ASM_REGISTER_STATE_CHECK(
136           asm_quant_(&vp8_comp_->mb.block[i], &macroblockd_dst_->block[i]));
137     }
138
139     CheckOutput();
140   }
141
142  private:
143   VP8Quantize asm_quant_;
144   VP8Quantize c_quant_;
145 };
146
147 class QuantizeTestPair : public QuantizeTestBase,
148                          public ::testing::TestWithParam<VP8QuantizePairParam> {
149  protected:
150   virtual void SetUp() {
151     SetupCompressor();
152     asm_quant_pair_ = GET_PARAM(0);
153     c_quant_pair_ = GET_PARAM(1);
154   }
155
156   void RunComparison() {
157     // Skip the last, unpaired, block.
158     for (int i = 0; i < kNumBlocks - 1; i += 2) {
159       ASM_REGISTER_STATE_CHECK(c_quant_pair_(
160           &vp8_comp_->mb.block[i], &vp8_comp_->mb.block[i + 1],
161           &vp8_comp_->mb.e_mbd.block[i], &vp8_comp_->mb.e_mbd.block[i + 1]));
162       ASM_REGISTER_STATE_CHECK(asm_quant_pair_(
163           &vp8_comp_->mb.block[i], &vp8_comp_->mb.block[i + 1],
164           &macroblockd_dst_->block[i], &macroblockd_dst_->block[i + 1]));
165     }
166
167     CheckOutput();
168   }
169
170  private:
171   VP8QuantizePair asm_quant_pair_;
172   VP8QuantizePair c_quant_pair_;
173 };
174
175 TEST_P(QuantizeTest, TestZeroInput) {
176   FillCoeffConstant(0);
177   RunComparison();
178 }
179
180 TEST_P(QuantizeTest, TestRandomInput) {
181   FillCoeffRandom();
182   RunComparison();
183 }
184
185 TEST_P(QuantizeTest, TestMultipleQ) {
186   for (int q = 0; q < QINDEX_RANGE; ++q) {
187     UpdateQuantizer(q);
188     FillCoeffRandom();
189     RunComparison();
190   }
191 }
192
193 TEST_P(QuantizeTestPair, TestZeroInput) {
194   FillCoeffConstant(0);
195   RunComparison();
196 }
197
198 TEST_P(QuantizeTestPair, TestRandomInput) {
199   FillCoeffRandom();
200   RunComparison();
201 }
202
203 TEST_P(QuantizeTestPair, TestMultipleQ) {
204   for (int q = 0; q < QINDEX_RANGE; ++q) {
205     UpdateQuantizer(q);
206     FillCoeffRandom();
207     RunComparison();
208   }
209 }
210
211 #if HAVE_SSE2
212 INSTANTIATE_TEST_CASE_P(
213     SSE2, QuantizeTest,
214     ::testing::Values(
215         make_tuple(&vp8_fast_quantize_b_sse2, &vp8_fast_quantize_b_c),
216         make_tuple(&vp8_regular_quantize_b_sse2, &vp8_regular_quantize_b_c)));
217 #endif  // HAVE_SSE2
218
219 #if HAVE_SSSE3
220 INSTANTIATE_TEST_CASE_P(SSSE3, QuantizeTest,
221                         ::testing::Values(make_tuple(&vp8_fast_quantize_b_ssse3,
222                                                      &vp8_fast_quantize_b_c)));
223 #endif  // HAVE_SSSE3
224
225 #if HAVE_SSE4_1
226 INSTANTIATE_TEST_CASE_P(
227     SSE4_1, QuantizeTest,
228     ::testing::Values(make_tuple(&vp8_regular_quantize_b_sse4_1,
229                                  &vp8_regular_quantize_b_c)));
230 #endif  // HAVE_SSE4_1
231
232 #if HAVE_MEDIA
233 INSTANTIATE_TEST_CASE_P(MEDIA, QuantizeTest,
234                         ::testing::Values(make_tuple(&vp8_fast_quantize_b_armv6,
235                                                      &vp8_fast_quantize_b_c)));
236 #endif  // HAVE_MEDIA
237
238 #if HAVE_NEON_ASM
239 INSTANTIATE_TEST_CASE_P(NEON, QuantizeTest,
240                         ::testing::Values(make_tuple(&vp8_fast_quantize_b_neon,
241                                                      &vp8_fast_quantize_b_c)));
242
243 INSTANTIATE_TEST_CASE_P(
244     NEON, QuantizeTestPair,
245     ::testing::Values(make_tuple(&vp8_fast_quantize_b_pair_neon,
246                                  &vp8_fast_quantize_b_pair_c)));
247 #endif  // HAVE_NEON_ASM
248 }  // namespace