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 <cstdlib>
13 #include <string>
14 
15 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
16 
17 #include "./aom_config.h"
18 #include "./av1_rtcd.h"
19 #include "aom_ports/aom_timer.h"
20 #include "av1/common/cdef_block.h"
21 #include "test/acm_random.h"
22 #include "test/clear_system_state.h"
23 #include "test/register_state_check.h"
24 #include "test/util.h"
25 
26 using libaom_test::ACMRandom;
27 
28 namespace {
29 
30 typedef std::tr1::tuple<cdef_direction_func, cdef_direction_func, int>
31     dering_dir_param_t;
32 
33 class CDEFDeringDirTest : public ::testing::TestWithParam<dering_dir_param_t> {
34  public:
~CDEFDeringDirTest()35   virtual ~CDEFDeringDirTest() {}
SetUp()36   virtual void SetUp() {
37     dering = GET_PARAM(0);
38     ref_dering = GET_PARAM(1);
39     bsize = GET_PARAM(2);
40   }
41 
TearDown()42   virtual void TearDown() { libaom_test::ClearSystemState(); }
43 
44  protected:
45   int bsize;
46   cdef_direction_func dering;
47   cdef_direction_func ref_dering;
48 };
49 
50 typedef CDEFDeringDirTest CDEFDeringSpeedTest;
51 
test_dering(int bsize,int iterations,cdef_direction_func dering,cdef_direction_func ref_dering)52 void test_dering(int bsize, int iterations, cdef_direction_func dering,
53                  cdef_direction_func ref_dering) {
54   const int size = 8;
55   const int ysize = size + 2 * CDEF_VBORDER;
56   ACMRandom rnd(ACMRandom::DeterministicSeed());
57   DECLARE_ALIGNED(16, uint16_t, s[ysize * CDEF_BSTRIDE]);
58   DECLARE_ALIGNED(16, static uint16_t, d[size * size]);
59   DECLARE_ALIGNED(16, static uint16_t, ref_d[size * size]);
60   memset(ref_d, 0, sizeof(ref_d));
61   memset(d, 0, sizeof(d));
62 
63   int error = 0, threshold = 0, dir;
64   int boundary, damping, depth, bits, level, count,
65       errdepth = 0, errthreshold = 0, errboundary = 0, errdamping = 0;
66   unsigned int pos = 0;
67 
68   for (boundary = 0; boundary < 16; boundary++) {
69     for (depth = 8; depth <= 12; depth += 2) {
70       for (damping = 5 + depth - 8; damping < 7 + depth - 8; damping++) {
71         for (count = 0; count < iterations; count++) {
72           for (level = 0; level < (1 << depth) && !error;
73                level += (1 + 4 * !!boundary) << (depth - 8)) {
74             for (bits = 1; bits <= depth && !error; bits++) {
75               for (unsigned int i = 0; i < sizeof(s) / sizeof(*s); i++)
76                 s[i] = clamp((rnd.Rand16() & ((1 << bits) - 1)) + level, 0,
77                              (1 << depth) - 1);
78               if (boundary) {
79                 if (boundary & 1) {  // Left
80                   for (int i = 0; i < ysize; i++)
81                     for (int j = 0; j < CDEF_HBORDER; j++)
82                       s[i * CDEF_BSTRIDE + j] = CDEF_VERY_LARGE;
83                 }
84                 if (boundary & 2) {  // Right
85                   for (int i = 0; i < ysize; i++)
86                     for (int j = CDEF_HBORDER + size; j < CDEF_BSTRIDE; j++)
87                       s[i * CDEF_BSTRIDE + j] = CDEF_VERY_LARGE;
88                 }
89                 if (boundary & 4) {  // Above
90                   for (int i = 0; i < CDEF_VBORDER; i++)
91                     for (int j = 0; j < CDEF_BSTRIDE; j++)
92                       s[i * CDEF_BSTRIDE + j] = CDEF_VERY_LARGE;
93                 }
94                 if (boundary & 8) {  // Below
95                   for (int i = CDEF_VBORDER + size; i < ysize; i++)
96                     for (int j = 0; j < CDEF_BSTRIDE; j++)
97                       s[i * CDEF_BSTRIDE + j] = CDEF_VERY_LARGE;
98                 }
99               }
100               for (dir = 0; dir < 8; dir++) {
101                 for (threshold = 0; threshold < 64 << (depth - 8) && !error;
102                      threshold += (1 + 4 * !!boundary) << (depth - 8)) {
103                   ref_dering(ref_d, size,
104                              s + CDEF_HBORDER + CDEF_VBORDER * CDEF_BSTRIDE,
105                              threshold, dir, damping);
106                   // If dering and ref_dering are the same, we're just testing
107                   // speed
108                   if (dering != ref_dering)
109                     ASM_REGISTER_STATE_CHECK(dering(
110                         d, size, s + CDEF_HBORDER + CDEF_VBORDER * CDEF_BSTRIDE,
111                         threshold, dir, damping));
112                   if (ref_dering != dering) {
113                     for (pos = 0; pos < sizeof(d) / sizeof(*d) && !error;
114                          pos++) {
115                       error = ref_d[pos] != d[pos];
116                       errdepth = depth;
117                       errthreshold = threshold;
118                       errboundary = boundary;
119                       errdamping = damping;
120                     }
121                   }
122                 }
123               }
124             }
125           }
126         }
127       }
128     }
129   }
130 
131   pos--;
132   EXPECT_EQ(0, error) << "Error: CDEFDeringDirTest, SIMD and C mismatch."
133                       << std::endl
134                       << "First error at " << pos % size << "," << pos / size
135                       << " (" << (int16_t)ref_d[pos] << " : " << (int16_t)d[pos]
136                       << ") " << std::endl
137                       << "threshold: " << errthreshold << std::endl
138                       << "damping: " << errdamping << std::endl
139                       << "depth: " << errdepth << std::endl
140                       << "size: " << bsize << std::endl
141                       << "boundary: " << errboundary << std::endl
142                       << std::endl;
143 }
144 
test_dering_speed(int bsize,int iterations,cdef_direction_func dering,cdef_direction_func ref_dering)145 void test_dering_speed(int bsize, int iterations, cdef_direction_func dering,
146                        cdef_direction_func ref_dering) {
147   aom_usec_timer ref_timer;
148   aom_usec_timer timer;
149 
150   aom_usec_timer_start(&ref_timer);
151   test_dering(bsize, iterations, ref_dering, ref_dering);
152   aom_usec_timer_mark(&ref_timer);
153   int ref_elapsed_time = (int)aom_usec_timer_elapsed(&ref_timer);
154 
155   aom_usec_timer_start(&timer);
156   test_dering(bsize, iterations, dering, dering);
157   aom_usec_timer_mark(&timer);
158   int elapsed_time = (int)aom_usec_timer_elapsed(&timer);
159 
160 #if 0
161   std::cout << "[          ] C time = " << ref_elapsed_time / 1000
162             << " ms, SIMD time = " << elapsed_time / 1000 << " ms" << std::endl;
163 #endif
164 
165   EXPECT_GT(ref_elapsed_time, elapsed_time)
166       << "Error: CDEFDeringSpeedTest, SIMD slower than C." << std::endl
167       << "C time: " << ref_elapsed_time << " us" << std::endl
168       << "SIMD time: " << elapsed_time << " us" << std::endl;
169 }
170 
171 typedef int (*find_dir_t)(const uint16_t *img, int stride, int32_t *var,
172                           int coeff_shift);
173 
174 typedef std::tr1::tuple<find_dir_t, find_dir_t> find_dir_param_t;
175 
176 class CDEFDeringFindDirTest
177     : public ::testing::TestWithParam<find_dir_param_t> {
178  public:
~CDEFDeringFindDirTest()179   virtual ~CDEFDeringFindDirTest() {}
SetUp()180   virtual void SetUp() {
181     finddir = GET_PARAM(0);
182     ref_finddir = GET_PARAM(1);
183   }
184 
TearDown()185   virtual void TearDown() { libaom_test::ClearSystemState(); }
186 
187  protected:
188   find_dir_t finddir;
189   find_dir_t ref_finddir;
190 };
191 
192 typedef CDEFDeringFindDirTest CDEFDeringFindDirSpeedTest;
193 
test_finddir(int (* finddir)(const uint16_t * img,int stride,int32_t * var,int coeff_shift),int (* ref_finddir)(const uint16_t * img,int stride,int32_t * var,int coeff_shift))194 void test_finddir(int (*finddir)(const uint16_t *img, int stride, int32_t *var,
195                                  int coeff_shift),
196                   int (*ref_finddir)(const uint16_t *img, int stride,
197                                      int32_t *var, int coeff_shift)) {
198   const int size = 8;
199   ACMRandom rnd(ACMRandom::DeterministicSeed());
200   DECLARE_ALIGNED(16, uint16_t, s[size * size]);
201 
202   int error = 0;
203   int depth, bits, level, count, errdepth = 0;
204   int ref_res = 0, res = 0;
205   int32_t ref_var = 0, var = 0;
206 
207   for (depth = 8; depth <= 12 && !error; depth += 2) {
208     for (count = 0; count < 512 && !error; count++) {
209       for (level = 0; level < (1 << depth) && !error;
210            level += 1 << (depth - 8)) {
211         for (bits = 1; bits <= depth && !error; bits++) {
212           for (unsigned int i = 0; i < sizeof(s) / sizeof(*s); i++)
213             s[i] = clamp((rnd.Rand16() & ((1 << bits) - 1)) + level, 0,
214                          (1 << depth) - 1);
215           for (int c = 0; c < 1 + 9 * (finddir == ref_finddir); c++)
216             ref_res = ref_finddir(s, size, &ref_var, depth - 8);
217           if (finddir != ref_finddir)
218             ASM_REGISTER_STATE_CHECK(res = finddir(s, size, &var, depth - 8));
219           if (ref_finddir != finddir) {
220             if (res != ref_res || var != ref_var) error = 1;
221             errdepth = depth;
222           }
223         }
224       }
225     }
226   }
227 
228   EXPECT_EQ(0, error) << "Error: CDEFDeringFindDirTest, SIMD and C mismatch."
229                       << std::endl
230                       << "return: " << res << " : " << ref_res << std::endl
231                       << "var: " << var << " : " << ref_var << std::endl
232                       << "depth: " << errdepth << std::endl
233                       << std::endl;
234 }
235 
test_finddir_speed(int (* finddir)(const uint16_t * img,int stride,int32_t * var,int coeff_shift),int (* ref_finddir)(const uint16_t * img,int stride,int32_t * var,int coeff_shift))236 void test_finddir_speed(int (*finddir)(const uint16_t *img, int stride,
237                                        int32_t *var, int coeff_shift),
238                         int (*ref_finddir)(const uint16_t *img, int stride,
239                                            int32_t *var, int coeff_shift)) {
240   aom_usec_timer ref_timer;
241   aom_usec_timer timer;
242 
243   aom_usec_timer_start(&ref_timer);
244   test_finddir(ref_finddir, ref_finddir);
245   aom_usec_timer_mark(&ref_timer);
246   int ref_elapsed_time = (int)aom_usec_timer_elapsed(&ref_timer);
247 
248   aom_usec_timer_start(&timer);
249   test_finddir(finddir, finddir);
250   aom_usec_timer_mark(&timer);
251   int elapsed_time = (int)aom_usec_timer_elapsed(&timer);
252 
253 #if 0
254   std::cout << "[          ] C time = " << ref_elapsed_time / 1000
255             << " ms, SIMD time = " << elapsed_time / 1000 << " ms" << std::endl;
256 #endif
257 
258   EXPECT_GT(ref_elapsed_time, elapsed_time)
259       << "Error: CDEFDeringFindDirSpeedTest, SIMD slower than C." << std::endl
260       << "C time: " << ref_elapsed_time << " us" << std::endl
261       << "SIMD time: " << elapsed_time << " us" << std::endl;
262 }
263 
TEST_P(CDEFDeringDirTest,TestSIMDNoMismatch)264 TEST_P(CDEFDeringDirTest, TestSIMDNoMismatch) {
265   test_dering(bsize, 1, dering, ref_dering);
266 }
267 
TEST_P(CDEFDeringSpeedTest,DISABLED_TestSpeed)268 TEST_P(CDEFDeringSpeedTest, DISABLED_TestSpeed) {
269   test_dering_speed(bsize, 4, dering, ref_dering);
270 }
271 
TEST_P(CDEFDeringFindDirTest,TestSIMDNoMismatch)272 TEST_P(CDEFDeringFindDirTest, TestSIMDNoMismatch) {
273   test_finddir(finddir, ref_finddir);
274 }
275 
TEST_P(CDEFDeringFindDirSpeedTest,DISABLED_TestSpeed)276 TEST_P(CDEFDeringFindDirSpeedTest, DISABLED_TestSpeed) {
277   test_finddir_speed(finddir, ref_finddir);
278 }
279 
280 using std::tr1::make_tuple;
281 
282 // VS compiling for 32 bit targets does not support vector types in
283 // structs as arguments, which makes the v256 type of the intrinsics
284 // hard to support, so optimizations for this target are disabled.
285 #if defined(_WIN64) || !defined(_MSC_VER) || defined(__clang__)
286 #if HAVE_SSE2
287 INSTANTIATE_TEST_CASE_P(SSE2, CDEFDeringDirTest,
288                         ::testing::Values(make_tuple(&cdef_direction_4x4_sse2,
289                                                      &cdef_direction_4x4_c, 4),
290                                           make_tuple(&cdef_direction_8x8_sse2,
291                                                      &cdef_direction_8x8_c,
292                                                      8)));
293 INSTANTIATE_TEST_CASE_P(SSE2, CDEFDeringFindDirTest,
294                         ::testing::Values(make_tuple(&cdef_find_dir_sse2,
295                                                      &cdef_find_dir_c)));
296 #endif
297 #if HAVE_SSSE3
298 INSTANTIATE_TEST_CASE_P(SSSE3, CDEFDeringDirTest,
299                         ::testing::Values(make_tuple(&cdef_direction_4x4_ssse3,
300                                                      &cdef_direction_4x4_c, 4),
301                                           make_tuple(&cdef_direction_8x8_ssse3,
302                                                      &cdef_direction_8x8_c,
303                                                      8)));
304 INSTANTIATE_TEST_CASE_P(SSSE3, CDEFDeringFindDirTest,
305                         ::testing::Values(make_tuple(&cdef_find_dir_ssse3,
306                                                      &cdef_find_dir_c)));
307 #endif
308 
309 #if HAVE_SSE4_1
310 INSTANTIATE_TEST_CASE_P(SSE4_1, CDEFDeringDirTest,
311                         ::testing::Values(make_tuple(&cdef_direction_4x4_sse4_1,
312                                                      &cdef_direction_4x4_c, 4),
313                                           make_tuple(&cdef_direction_8x8_sse4_1,
314                                                      &cdef_direction_8x8_c,
315                                                      8)));
316 INSTANTIATE_TEST_CASE_P(SSE4_1, CDEFDeringFindDirTest,
317                         ::testing::Values(make_tuple(&cdef_find_dir_sse4_1,
318                                                      &cdef_find_dir_c)));
319 #endif
320 
321 #if HAVE_NEON
322 INSTANTIATE_TEST_CASE_P(NEON, CDEFDeringDirTest,
323                         ::testing::Values(make_tuple(&cdef_direction_4x4_neon,
324                                                      &cdef_direction_4x4_c, 4),
325                                           make_tuple(&cdef_direction_8x8_neon,
326                                                      &cdef_direction_8x8_c,
327                                                      8)));
328 INSTANTIATE_TEST_CASE_P(NEON, CDEFDeringFindDirTest,
329                         ::testing::Values(make_tuple(&cdef_find_dir_neon,
330                                                      &cdef_find_dir_c)));
331 #endif
332 
333 // Test speed for all supported architectures
334 #if HAVE_SSE2
335 INSTANTIATE_TEST_CASE_P(SSE2, CDEFDeringSpeedTest,
336                         ::testing::Values(make_tuple(&cdef_direction_4x4_sse2,
337                                                      &cdef_direction_4x4_c, 4),
338                                           make_tuple(&cdef_direction_8x8_sse2,
339                                                      &cdef_direction_8x8_c,
340                                                      8)));
341 INSTANTIATE_TEST_CASE_P(SSE2, CDEFDeringFindDirSpeedTest,
342                         ::testing::Values(make_tuple(&cdef_find_dir_sse2,
343                                                      &cdef_find_dir_c)));
344 #endif
345 
346 #if HAVE_SSSE3
347 INSTANTIATE_TEST_CASE_P(SSSE3, CDEFDeringSpeedTest,
348                         ::testing::Values(make_tuple(&cdef_direction_4x4_ssse3,
349                                                      &cdef_direction_4x4_c, 4),
350                                           make_tuple(&cdef_direction_8x8_ssse3,
351                                                      &cdef_direction_8x8_c,
352                                                      8)));
353 INSTANTIATE_TEST_CASE_P(SSSE3, CDEFDeringFindDirSpeedTest,
354                         ::testing::Values(make_tuple(&cdef_find_dir_ssse3,
355                                                      &cdef_find_dir_c)));
356 #endif
357 
358 #if HAVE_SSE4_1
359 INSTANTIATE_TEST_CASE_P(SSE4_1, CDEFDeringSpeedTest,
360                         ::testing::Values(make_tuple(&cdef_direction_4x4_sse4_1,
361                                                      &cdef_direction_4x4_c, 4),
362                                           make_tuple(&cdef_direction_8x8_sse4_1,
363                                                      &cdef_direction_8x8_c,
364                                                      8)));
365 INSTANTIATE_TEST_CASE_P(SSE4_1, CDEFDeringFindDirSpeedTest,
366                         ::testing::Values(make_tuple(&cdef_find_dir_sse4_1,
367                                                      &cdef_find_dir_c)));
368 #endif
369 
370 #if HAVE_NEON
371 INSTANTIATE_TEST_CASE_P(NEON, CDEFDeringSpeedTest,
372                         ::testing::Values(make_tuple(&cdef_direction_4x4_neon,
373                                                      &cdef_direction_4x4_c, 4),
374                                           make_tuple(&cdef_direction_8x8_neon,
375                                                      &cdef_direction_8x8_c,
376                                                      8)));
377 INSTANTIATE_TEST_CASE_P(NEON, CDEFDeringFindDirSpeedTest,
378                         ::testing::Values(make_tuple(&cdef_find_dir_neon,
379                                                      &cdef_find_dir_c)));
380 #endif
381 
382 #endif  // defined(_WIN64) || !defined(_MSC_VER)
383 }  // namespace
384