1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
13
14 #include "config/aom_config.h"
15 #include "config/aom_dsp_rtcd.h"
16 #include "config/av1_rtcd.h"
17
18 #include "aom_dsp/aom_dsp_common.h"
19
20 #include "av1/common/enums.h"
21
22 #include "test/acm_random.h"
23 #include "test/function_equivalence_test.h"
24 #include "test/register_state_check.h"
25
26 #define WEDGE_WEIGHT_BITS 6
27 #define MAX_MASK_VALUE (1 << (WEDGE_WEIGHT_BITS))
28
29 using libaom_test::ACMRandom;
30 using libaom_test::FunctionEquivalenceTest;
31
32 namespace {
33
34 static const int16_t kInt13Max = (1 << 12) - 1;
35
36 //////////////////////////////////////////////////////////////////////////////
37 // av1_wedge_sse_from_residuals - functionality
38 //////////////////////////////////////////////////////////////////////////////
39
40 class WedgeUtilsSSEFuncTest : public testing::Test {
41 protected:
WedgeUtilsSSEFuncTest()42 WedgeUtilsSSEFuncTest() : rng_(ACMRandom::DeterministicSeed()) {}
43
44 static const int kIterations = 1000;
45
46 ACMRandom rng_;
47 };
48
equiv_blend_residuals(int16_t * r,const int16_t * r0,const int16_t * r1,const uint8_t * m,int N)49 static void equiv_blend_residuals(int16_t *r, const int16_t *r0,
50 const int16_t *r1, const uint8_t *m, int N) {
51 for (int i = 0; i < N; i++) {
52 const int32_t m0 = m[i];
53 const int32_t m1 = MAX_MASK_VALUE - m0;
54 const int16_t R = m0 * r0[i] + m1 * r1[i];
55 // Note that this rounding is designed to match the result
56 // you would get when actually blending the 2 predictors and computing
57 // the residuals.
58 r[i] = ROUND_POWER_OF_TWO(R - 1, WEDGE_WEIGHT_BITS);
59 }
60 }
61
equiv_sse_from_residuals(const int16_t * r0,const int16_t * r1,const uint8_t * m,int N)62 static uint64_t equiv_sse_from_residuals(const int16_t *r0, const int16_t *r1,
63 const uint8_t *m, int N) {
64 uint64_t acc = 0;
65 for (int i = 0; i < N; i++) {
66 const int32_t m0 = m[i];
67 const int32_t m1 = MAX_MASK_VALUE - m0;
68 const int16_t R = m0 * r0[i] + m1 * r1[i];
69 const int32_t r = ROUND_POWER_OF_TWO(R - 1, WEDGE_WEIGHT_BITS);
70 acc += r * r;
71 }
72 return acc;
73 }
74
TEST_F(WedgeUtilsSSEFuncTest,ResidualBlendingEquiv)75 TEST_F(WedgeUtilsSSEFuncTest, ResidualBlendingEquiv) {
76 DECLARE_ALIGNED(32, uint8_t, s[MAX_SB_SQUARE]);
77 DECLARE_ALIGNED(32, uint8_t, p0[MAX_SB_SQUARE]);
78 DECLARE_ALIGNED(32, uint8_t, p1[MAX_SB_SQUARE]);
79 DECLARE_ALIGNED(32, uint8_t, p[MAX_SB_SQUARE]);
80
81 DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]);
82 DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]);
83 DECLARE_ALIGNED(32, int16_t, r_ref[MAX_SB_SQUARE]);
84 DECLARE_ALIGNED(32, int16_t, r_tst[MAX_SB_SQUARE]);
85 DECLARE_ALIGNED(32, uint8_t, m[MAX_SB_SQUARE]);
86
87 for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) {
88 for (int i = 0; i < MAX_SB_SQUARE; ++i) {
89 s[i] = rng_.Rand8();
90 m[i] = rng_(MAX_MASK_VALUE + 1);
91 }
92
93 const int w = 1 << (rng_(MAX_SB_SIZE_LOG2 + 1 - 3) + 3);
94 const int h = 1 << (rng_(MAX_SB_SIZE_LOG2 + 1 - 3) + 3);
95 const int N = w * h;
96
97 for (int j = 0; j < N; j++) {
98 p0[j] = clamp(s[j] + rng_(33) - 16, 0, UINT8_MAX);
99 p1[j] = clamp(s[j] + rng_(33) - 16, 0, UINT8_MAX);
100 }
101
102 aom_blend_a64_mask(p, w, p0, w, p1, w, m, w, w, h, 0, 0);
103
104 aom_subtract_block(h, w, r0, w, s, w, p0, w);
105 aom_subtract_block(h, w, r1, w, s, w, p1, w);
106
107 aom_subtract_block(h, w, r_ref, w, s, w, p, w);
108 equiv_blend_residuals(r_tst, r0, r1, m, N);
109
110 for (int i = 0; i < N; ++i) ASSERT_EQ(r_ref[i], r_tst[i]);
111
112 uint64_t ref_sse = aom_sum_squares_i16(r_ref, N);
113 uint64_t tst_sse = equiv_sse_from_residuals(r0, r1, m, N);
114
115 ASSERT_EQ(ref_sse, tst_sse);
116 }
117 }
118
sse_from_residuals(const int16_t * r0,const int16_t * r1,const uint8_t * m,int N)119 static uint64_t sse_from_residuals(const int16_t *r0, const int16_t *r1,
120 const uint8_t *m, int N) {
121 uint64_t acc = 0;
122 for (int i = 0; i < N; i++) {
123 const int32_t m0 = m[i];
124 const int32_t m1 = MAX_MASK_VALUE - m0;
125 const int32_t r = m0 * r0[i] + m1 * r1[i];
126 acc += r * r;
127 }
128 return ROUND_POWER_OF_TWO(acc, 2 * WEDGE_WEIGHT_BITS);
129 }
130
TEST_F(WedgeUtilsSSEFuncTest,ResidualBlendingMethod)131 TEST_F(WedgeUtilsSSEFuncTest, ResidualBlendingMethod) {
132 DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]);
133 DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]);
134 DECLARE_ALIGNED(32, int16_t, d[MAX_SB_SQUARE]);
135 DECLARE_ALIGNED(32, uint8_t, m[MAX_SB_SQUARE]);
136
137 for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) {
138 for (int i = 0; i < MAX_SB_SQUARE; ++i) {
139 r1[i] = rng_(2 * INT8_MAX - 2 * INT8_MIN + 1) + 2 * INT8_MIN;
140 d[i] = rng_(2 * INT8_MAX - 2 * INT8_MIN + 1) + 2 * INT8_MIN;
141 m[i] = rng_(MAX_MASK_VALUE + 1);
142 }
143
144 const int N = 64 * (rng_(MAX_SB_SQUARE / 64) + 1);
145
146 for (int i = 0; i < N; i++) r0[i] = r1[i] + d[i];
147
148 const uint64_t ref_res = sse_from_residuals(r0, r1, m, N);
149 const uint64_t tst_res = av1_wedge_sse_from_residuals(r1, d, m, N);
150
151 ASSERT_EQ(ref_res, tst_res);
152 }
153 }
154
155 //////////////////////////////////////////////////////////////////////////////
156 // av1_wedge_sse_from_residuals - optimizations
157 //////////////////////////////////////////////////////////////////////////////
158
159 typedef uint64_t (*FSSE)(const int16_t *r1, const int16_t *d, const uint8_t *m,
160 int N);
161 typedef libaom_test::FuncParam<FSSE> TestFuncsFSSE;
162
163 class WedgeUtilsSSEOptTest : public FunctionEquivalenceTest<FSSE> {
164 protected:
165 static const int kIterations = 10000;
166 };
167
TEST_P(WedgeUtilsSSEOptTest,RandomValues)168 TEST_P(WedgeUtilsSSEOptTest, RandomValues) {
169 DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]);
170 DECLARE_ALIGNED(32, int16_t, d[MAX_SB_SQUARE]);
171 DECLARE_ALIGNED(32, uint8_t, m[MAX_SB_SQUARE]);
172
173 for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) {
174 for (int i = 0; i < MAX_SB_SQUARE; ++i) {
175 r1[i] = rng_(2 * kInt13Max + 1) - kInt13Max;
176 d[i] = rng_(2 * kInt13Max + 1) - kInt13Max;
177 m[i] = rng_(MAX_MASK_VALUE + 1);
178 }
179
180 const int N = 64 * (rng_(MAX_SB_SQUARE / 64) + 1);
181
182 const uint64_t ref_res = params_.ref_func(r1, d, m, N);
183 uint64_t tst_res;
184 ASM_REGISTER_STATE_CHECK(tst_res = params_.tst_func(r1, d, m, N));
185
186 ASSERT_EQ(ref_res, tst_res);
187 }
188 }
189
TEST_P(WedgeUtilsSSEOptTest,ExtremeValues)190 TEST_P(WedgeUtilsSSEOptTest, ExtremeValues) {
191 DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]);
192 DECLARE_ALIGNED(32, int16_t, d[MAX_SB_SQUARE]);
193 DECLARE_ALIGNED(32, uint8_t, m[MAX_SB_SQUARE]);
194
195 for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) {
196 if (rng_(2)) {
197 for (int i = 0; i < MAX_SB_SQUARE; ++i) r1[i] = kInt13Max;
198 } else {
199 for (int i = 0; i < MAX_SB_SQUARE; ++i) r1[i] = -kInt13Max;
200 }
201
202 if (rng_(2)) {
203 for (int i = 0; i < MAX_SB_SQUARE; ++i) d[i] = kInt13Max;
204 } else {
205 for (int i = 0; i < MAX_SB_SQUARE; ++i) d[i] = -kInt13Max;
206 }
207
208 for (int i = 0; i < MAX_SB_SQUARE; ++i) m[i] = MAX_MASK_VALUE;
209
210 const int N = 64 * (rng_(MAX_SB_SQUARE / 64) + 1);
211
212 const uint64_t ref_res = params_.ref_func(r1, d, m, N);
213 uint64_t tst_res;
214 ASM_REGISTER_STATE_CHECK(tst_res = params_.tst_func(r1, d, m, N));
215
216 ASSERT_EQ(ref_res, tst_res);
217 }
218 }
219
220 //////////////////////////////////////////////////////////////////////////////
221 // av1_wedge_sign_from_residuals
222 //////////////////////////////////////////////////////////////////////////////
223
224 typedef int8_t (*FSign)(const int16_t *ds, const uint8_t *m, int N,
225 int64_t limit);
226 typedef libaom_test::FuncParam<FSign> TestFuncsFSign;
227
228 class WedgeUtilsSignOptTest : public FunctionEquivalenceTest<FSign> {
229 protected:
230 static const int kIterations = 10000;
231 static const int kMaxSize = 8196; // Size limited by SIMD implementation.
232 };
233
TEST_P(WedgeUtilsSignOptTest,RandomValues)234 TEST_P(WedgeUtilsSignOptTest, RandomValues) {
235 DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]);
236 DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]);
237 DECLARE_ALIGNED(32, int16_t, ds[MAX_SB_SQUARE]);
238 DECLARE_ALIGNED(32, uint8_t, m[MAX_SB_SQUARE]);
239
240 for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) {
241 for (int i = 0; i < MAX_SB_SQUARE; ++i) {
242 r0[i] = rng_(2 * kInt13Max + 1) - kInt13Max;
243 r1[i] = rng_(2 * kInt13Max + 1) - kInt13Max;
244 m[i] = rng_(MAX_MASK_VALUE + 1);
245 }
246
247 const int maxN = AOMMIN(kMaxSize, MAX_SB_SQUARE);
248 const int N = 64 * (rng_(maxN / 64 - 1) + 1);
249
250 int64_t limit;
251 limit = (int64_t)aom_sum_squares_i16(r0, N);
252 limit -= (int64_t)aom_sum_squares_i16(r1, N);
253 limit *= (1 << WEDGE_WEIGHT_BITS) / 2;
254
255 for (int i = 0; i < N; i++)
256 ds[i] = clamp(r0[i] * r0[i] - r1[i] * r1[i], INT16_MIN, INT16_MAX);
257
258 const int ref_res = params_.ref_func(ds, m, N, limit);
259 int tst_res;
260 ASM_REGISTER_STATE_CHECK(tst_res = params_.tst_func(ds, m, N, limit));
261
262 ASSERT_EQ(ref_res, tst_res);
263 }
264 }
265
TEST_P(WedgeUtilsSignOptTest,ExtremeValues)266 TEST_P(WedgeUtilsSignOptTest, ExtremeValues) {
267 DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]);
268 DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]);
269 DECLARE_ALIGNED(32, int16_t, ds[MAX_SB_SQUARE]);
270 DECLARE_ALIGNED(32, uint8_t, m[MAX_SB_SQUARE]);
271
272 for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) {
273 switch (rng_(4)) {
274 case 0:
275 for (int i = 0; i < MAX_SB_SQUARE; ++i) {
276 r0[i] = 0;
277 r1[i] = kInt13Max;
278 }
279 break;
280 case 1:
281 for (int i = 0; i < MAX_SB_SQUARE; ++i) {
282 r0[i] = kInt13Max;
283 r1[i] = 0;
284 }
285 break;
286 case 2:
287 for (int i = 0; i < MAX_SB_SQUARE; ++i) {
288 r0[i] = 0;
289 r1[i] = -kInt13Max;
290 }
291 break;
292 default:
293 for (int i = 0; i < MAX_SB_SQUARE; ++i) {
294 r0[i] = -kInt13Max;
295 r1[i] = 0;
296 }
297 break;
298 }
299
300 for (int i = 0; i < MAX_SB_SQUARE; ++i) m[i] = MAX_MASK_VALUE;
301
302 const int maxN = AOMMIN(kMaxSize, MAX_SB_SQUARE);
303 const int N = 64 * (rng_(maxN / 64 - 1) + 1);
304
305 int64_t limit;
306 limit = (int64_t)aom_sum_squares_i16(r0, N);
307 limit -= (int64_t)aom_sum_squares_i16(r1, N);
308 limit *= (1 << WEDGE_WEIGHT_BITS) / 2;
309
310 for (int i = 0; i < N; i++)
311 ds[i] = clamp(r0[i] * r0[i] - r1[i] * r1[i], INT16_MIN, INT16_MAX);
312
313 const int ref_res = params_.ref_func(ds, m, N, limit);
314 int tst_res;
315 ASM_REGISTER_STATE_CHECK(tst_res = params_.tst_func(ds, m, N, limit));
316
317 ASSERT_EQ(ref_res, tst_res);
318 }
319 }
320
321 //////////////////////////////////////////////////////////////////////////////
322 // av1_wedge_compute_delta_squares
323 //////////////////////////////////////////////////////////////////////////////
324
325 typedef void (*FDS)(int16_t *d, const int16_t *a, const int16_t *b, int N);
326 typedef libaom_test::FuncParam<FDS> TestFuncsFDS;
327
328 class WedgeUtilsDeltaSquaresOptTest : public FunctionEquivalenceTest<FDS> {
329 protected:
330 static const int kIterations = 10000;
331 };
332
TEST_P(WedgeUtilsDeltaSquaresOptTest,RandomValues)333 TEST_P(WedgeUtilsDeltaSquaresOptTest, RandomValues) {
334 DECLARE_ALIGNED(32, int16_t, a[MAX_SB_SQUARE]);
335 DECLARE_ALIGNED(32, int16_t, b[MAX_SB_SQUARE]);
336 DECLARE_ALIGNED(32, int16_t, d_ref[MAX_SB_SQUARE]);
337 DECLARE_ALIGNED(32, int16_t, d_tst[MAX_SB_SQUARE]);
338
339 for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) {
340 for (int i = 0; i < MAX_SB_SQUARE; ++i) {
341 a[i] = rng_.Rand16();
342 b[i] = rng_(2 * INT16_MAX + 1) - INT16_MAX;
343 }
344
345 const int N = 64 * (rng_(MAX_SB_SQUARE / 64) + 1);
346
347 memset(&d_ref, INT16_MAX, sizeof(d_ref));
348 memset(&d_tst, INT16_MAX, sizeof(d_tst));
349
350 params_.ref_func(d_ref, a, b, N);
351 ASM_REGISTER_STATE_CHECK(params_.tst_func(d_tst, a, b, N));
352
353 for (int i = 0; i < MAX_SB_SQUARE; ++i) ASSERT_EQ(d_ref[i], d_tst[i]);
354 }
355 }
356
357 #if HAVE_SSE2
358 INSTANTIATE_TEST_SUITE_P(
359 SSE2, WedgeUtilsSSEOptTest,
360 ::testing::Values(TestFuncsFSSE(av1_wedge_sse_from_residuals_c,
361 av1_wedge_sse_from_residuals_sse2)));
362
363 INSTANTIATE_TEST_SUITE_P(
364 SSE2, WedgeUtilsSignOptTest,
365 ::testing::Values(TestFuncsFSign(av1_wedge_sign_from_residuals_c,
366 av1_wedge_sign_from_residuals_sse2)));
367
368 INSTANTIATE_TEST_SUITE_P(
369 SSE2, WedgeUtilsDeltaSquaresOptTest,
370 ::testing::Values(TestFuncsFDS(av1_wedge_compute_delta_squares_c,
371 av1_wedge_compute_delta_squares_sse2)));
372 #endif // HAVE_SSE2
373
374 #if HAVE_AVX2
375 INSTANTIATE_TEST_SUITE_P(
376 AVX2, WedgeUtilsSSEOptTest,
377 ::testing::Values(TestFuncsFSSE(av1_wedge_sse_from_residuals_sse2,
378 av1_wedge_sse_from_residuals_avx2)));
379
380 INSTANTIATE_TEST_SUITE_P(
381 AVX2, WedgeUtilsSignOptTest,
382 ::testing::Values(TestFuncsFSign(av1_wedge_sign_from_residuals_sse2,
383 av1_wedge_sign_from_residuals_avx2)));
384
385 INSTANTIATE_TEST_SUITE_P(
386 AVX2, WedgeUtilsDeltaSquaresOptTest,
387 ::testing::Values(TestFuncsFDS(av1_wedge_compute_delta_squares_sse2,
388 av1_wedge_compute_delta_squares_avx2)));
389 #endif // HAVE_AVX2
390
391 } // namespace
392