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 <cmath>
12 #include <cstdlib>
13 #include <string>
14 #include <tuple>
15
16 #include "gtest/gtest.h"
17
18 #include "./vpx_config.h"
19 #include "./vp9_rtcd.h"
20 #include "test/acm_random.h"
21 #include "test/clear_system_state.h"
22 #include "test/register_state_check.h"
23 #include "test/util.h"
24 #include "vp9/common/vp9_entropy.h"
25 #include "vpx/vpx_codec.h"
26 #include "vpx/vpx_integer.h"
27 #include "vpx_dsp/vpx_dsp_common.h"
28
29 using libvpx_test::ACMRandom;
30
31 namespace {
32 const int kNumIterations = 1000;
33
34 typedef int64_t (*HBDBlockErrorFunc)(const tran_low_t *coeff,
35 const tran_low_t *dqcoeff,
36 intptr_t block_size, int64_t *ssz,
37 int bps);
38
39 typedef std::tuple<HBDBlockErrorFunc, HBDBlockErrorFunc, vpx_bit_depth_t>
40 BlockErrorParam;
41
42 typedef int64_t (*BlockErrorFunc)(const tran_low_t *coeff,
43 const tran_low_t *dqcoeff,
44 intptr_t block_size, int64_t *ssz);
45
46 template <BlockErrorFunc fn>
BlockError8BitWrapper(const tran_low_t * coeff,const tran_low_t * dqcoeff,intptr_t block_size,int64_t * ssz,int bps)47 int64_t BlockError8BitWrapper(const tran_low_t *coeff,
48 const tran_low_t *dqcoeff, intptr_t block_size,
49 int64_t *ssz, int bps) {
50 EXPECT_EQ(bps, 8);
51 return fn(coeff, dqcoeff, block_size, ssz);
52 }
53
54 class BlockErrorTest : public ::testing::TestWithParam<BlockErrorParam> {
55 public:
56 ~BlockErrorTest() override = default;
SetUp()57 void SetUp() override {
58 error_block_op_ = GET_PARAM(0);
59 ref_error_block_op_ = GET_PARAM(1);
60 bit_depth_ = GET_PARAM(2);
61 }
62
TearDown()63 void TearDown() override { libvpx_test::ClearSystemState(); }
64
65 protected:
66 vpx_bit_depth_t bit_depth_;
67 HBDBlockErrorFunc error_block_op_;
68 HBDBlockErrorFunc ref_error_block_op_;
69 };
70 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BlockErrorTest);
71
TEST_P(BlockErrorTest,OperationCheck)72 TEST_P(BlockErrorTest, OperationCheck) {
73 ACMRandom rnd(ACMRandom::DeterministicSeed());
74 DECLARE_ALIGNED(16, tran_low_t, coeff[4096]);
75 DECLARE_ALIGNED(16, tran_low_t, dqcoeff[4096]);
76 int err_count_total = 0;
77 int first_failure = -1;
78 intptr_t block_size;
79 int64_t ssz;
80 int64_t ret;
81 int64_t ref_ssz;
82 int64_t ref_ret;
83 const int msb = bit_depth_ + 8 - 1;
84 for (int i = 0; i < kNumIterations; ++i) {
85 int err_count = 0;
86 block_size = 16 << (i % 9); // All block sizes from 4x4, 8x4 ..64x64
87 for (int j = 0; j < block_size; j++) {
88 // coeff and dqcoeff will always have at least the same sign, and this
89 // can be used for optimization, so generate test input precisely.
90 if (rnd(2)) {
91 // Positive number
92 coeff[j] = rnd(1 << msb);
93 dqcoeff[j] = rnd(1 << msb);
94 } else {
95 // Negative number
96 coeff[j] = -rnd(1 << msb);
97 dqcoeff[j] = -rnd(1 << msb);
98 }
99 }
100 ref_ret =
101 ref_error_block_op_(coeff, dqcoeff, block_size, &ref_ssz, bit_depth_);
102 ASM_REGISTER_STATE_CHECK(
103 ret = error_block_op_(coeff, dqcoeff, block_size, &ssz, bit_depth_));
104 err_count += (ref_ret != ret) | (ref_ssz != ssz);
105 if (err_count && !err_count_total) {
106 first_failure = i;
107 }
108 err_count_total += err_count;
109 }
110 EXPECT_EQ(0, err_count_total)
111 << "Error: Error Block Test, C output doesn't match optimized output. "
112 << "First failed at test case " << first_failure;
113 }
114
TEST_P(BlockErrorTest,ExtremeValues)115 TEST_P(BlockErrorTest, ExtremeValues) {
116 ACMRandom rnd(ACMRandom::DeterministicSeed());
117 DECLARE_ALIGNED(16, tran_low_t, coeff[4096]);
118 DECLARE_ALIGNED(16, tran_low_t, dqcoeff[4096]);
119 int err_count_total = 0;
120 int first_failure = -1;
121 intptr_t block_size;
122 int64_t ssz;
123 int64_t ret;
124 int64_t ref_ssz;
125 int64_t ref_ret;
126 const int msb = bit_depth_ + 8 - 1;
127 int max_val = ((1 << msb) - 1);
128 for (int i = 0; i < kNumIterations; ++i) {
129 int err_count = 0;
130 int k = (i / 9) % 9;
131
132 // Change the maximum coeff value, to test different bit boundaries
133 if (k == 8 && (i % 9) == 0) {
134 max_val >>= 1;
135 }
136 block_size = 16 << (i % 9); // All block sizes from 4x4, 8x4 ..64x64
137 for (int j = 0; j < block_size; j++) {
138 if (k < 4) {
139 // Test at positive maximum values
140 coeff[j] = k % 2 ? max_val : 0;
141 dqcoeff[j] = (k >> 1) % 2 ? max_val : 0;
142 } else if (k < 8) {
143 // Test at negative maximum values
144 coeff[j] = k % 2 ? -max_val : 0;
145 dqcoeff[j] = (k >> 1) % 2 ? -max_val : 0;
146 } else {
147 if (rnd(2)) {
148 // Positive number
149 coeff[j] = rnd(1 << 14);
150 dqcoeff[j] = rnd(1 << 14);
151 } else {
152 // Negative number
153 coeff[j] = -rnd(1 << 14);
154 dqcoeff[j] = -rnd(1 << 14);
155 }
156 }
157 }
158 ref_ret =
159 ref_error_block_op_(coeff, dqcoeff, block_size, &ref_ssz, bit_depth_);
160 ASM_REGISTER_STATE_CHECK(
161 ret = error_block_op_(coeff, dqcoeff, block_size, &ssz, bit_depth_));
162 err_count += (ref_ret != ret) | (ref_ssz != ssz);
163 if (err_count && !err_count_total) {
164 first_failure = i;
165 }
166 err_count_total += err_count;
167 }
168 EXPECT_EQ(0, err_count_total)
169 << "Error: Error Block Test, C output doesn't match optimized output. "
170 << "First failed at test case " << first_failure;
171 }
172
173 using std::make_tuple;
174
175 #if HAVE_SSE2
176 const BlockErrorParam sse2_block_error_tests[] = {
177 #if CONFIG_VP9_HIGHBITDEPTH
178 make_tuple(&vp9_highbd_block_error_sse2, &vp9_highbd_block_error_c,
179 VPX_BITS_10),
180 make_tuple(&vp9_highbd_block_error_sse2, &vp9_highbd_block_error_c,
181 VPX_BITS_12),
182 make_tuple(&vp9_highbd_block_error_sse2, &vp9_highbd_block_error_c,
183 VPX_BITS_8),
184 #endif // CONFIG_VP9_HIGHBITDEPTH
185 make_tuple(&BlockError8BitWrapper<vp9_block_error_sse2>,
186 &BlockError8BitWrapper<vp9_block_error_c>, VPX_BITS_8)
187 };
188
189 INSTANTIATE_TEST_SUITE_P(SSE2, BlockErrorTest,
190 ::testing::ValuesIn(sse2_block_error_tests));
191 #endif // HAVE_SSE2
192
193 #if HAVE_AVX2
194 INSTANTIATE_TEST_SUITE_P(
195 AVX2, BlockErrorTest,
196 ::testing::Values(make_tuple(&BlockError8BitWrapper<vp9_block_error_avx2>,
197 &BlockError8BitWrapper<vp9_block_error_c>,
198 VPX_BITS_8)));
199 #endif // HAVE_AVX2
200
201 #if HAVE_NEON
202 const BlockErrorParam neon_block_error_tests[] = {
203 #if CONFIG_VP9_HIGHBITDEPTH
204 make_tuple(&vp9_highbd_block_error_neon, &vp9_highbd_block_error_c,
205 VPX_BITS_10),
206 make_tuple(&vp9_highbd_block_error_neon, &vp9_highbd_block_error_c,
207 VPX_BITS_12),
208 make_tuple(&vp9_highbd_block_error_neon, &vp9_highbd_block_error_c,
209 VPX_BITS_8),
210 #endif // CONFIG_VP9_HIGHBITDEPTH
211 make_tuple(&BlockError8BitWrapper<vp9_block_error_neon>,
212 &BlockError8BitWrapper<vp9_block_error_c>, VPX_BITS_8)
213 };
214
215 INSTANTIATE_TEST_SUITE_P(NEON, BlockErrorTest,
216 ::testing::ValuesIn(neon_block_error_tests));
217 #endif // HAVE_NEON
218
219 #if HAVE_SVE
220 const BlockErrorParam sve_block_error_tests[] = { make_tuple(
221 &BlockError8BitWrapper<vp9_block_error_sve>,
222 &BlockError8BitWrapper<vp9_block_error_c>, VPX_BITS_8) };
223
224 INSTANTIATE_TEST_SUITE_P(SVE, BlockErrorTest,
225 ::testing::ValuesIn(sve_block_error_tests));
226 #endif // HAVE_SVE
227 } // namespace
228