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