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