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 https://www.aomedia.org/license/software-license. 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 https://www.aomedia.org/license/patent-license.
10  */
11 
12 #include <stdlib.h>
13 #include "gtest/gtest.h"
14 #include "aom_dsp_rtcd.h"
15 #include "EbDefinitions.h"
16 #include "random.h"
17 #include "util.h"
18 #include "EbUnitTestUtility.h"
19 #include "acm_random.h"
20 #include "corner_match.h"
21 
22 using libaom_test::ACMRandom;
23 
24 namespace {
25 
26 typedef double (*ComputeCrossCorrFunc)(unsigned char *im1, int stride1, int x1,
27                                        int y1, unsigned char *im2, int stride2,
28                                        int x2, int y2);
29 
30 using ::testing::make_tuple;
31 using ::testing::tuple;
32 typedef tuple<int, ComputeCrossCorrFunc> CornerMatchParam;
33 
34 class AV1CornerMatchTest : public ::testing::TestWithParam<CornerMatchParam> {
35  public:
36   virtual ~AV1CornerMatchTest();
37   virtual void SetUp();
38 
39   virtual void TearDown();
40 
41  protected:
42   void RunCheckOutput(int run_times);
43   ComputeCrossCorrFunc target_func;
44 
45   libaom_test::ACMRandom rnd_;
46 };
47 
~AV1CornerMatchTest()48 AV1CornerMatchTest::~AV1CornerMatchTest() {}
SetUp()49 void AV1CornerMatchTest::SetUp() {
50   rnd_.Reset(ACMRandom::DeterministicSeed());
51     target_func = TEST_GET_PARAM(1);
52 }
TearDown()53 void AV1CornerMatchTest::TearDown() { }
54 
RunCheckOutput(int run_times)55 void AV1CornerMatchTest::RunCheckOutput(int run_times) {
56   const int w = 128, h = 128;
57   const int num_iters = 10000;
58   int i, j;
59   uint64_t start_time_seconds, start_time_useconds;
60   uint64_t middle_time_seconds, middle_time_useconds;
61   uint64_t finish_time_seconds, finish_time_useconds;
62   double time_c = 0, time_o = 0, time;
63 
64   uint8_t *input1 = new uint8_t[w * h];
65   uint8_t *input2 = new uint8_t[w * h];
66 
67   // Test the two extreme cases:
68   // i) Random data, should have correlation close to 0
69   // ii) Linearly related data + noise, should have correlation close to 1
70   int mode = TEST_GET_PARAM(0);
71   if (mode == 0) {
72     for (i = 0; i < h; ++i)
73       for (j = 0; j < w; ++j) {
74         input1[i * w + j] = rnd_.Rand8();
75         input2[i * w + j] = rnd_.Rand8();
76       }
77   } else if (mode == 1) {
78     for (i = 0; i < h; ++i)
79       for (j = 0; j < w; ++j) {
80         int v = rnd_.Rand8();
81         input1[i * w + j] = v;
82         input2[i * w + j] = (v / 2) + (rnd_.Rand8() & 15);
83       }
84   }
85 
86   for (i = 0; i < num_iters; ++i) {
87     int x1 = MATCH_SZ_BY2 + rnd_.PseudoUniform(w - 2 * MATCH_SZ_BY2);
88     int y1 = MATCH_SZ_BY2 + rnd_.PseudoUniform(h - 2 * MATCH_SZ_BY2);
89     int x2 = MATCH_SZ_BY2 + rnd_.PseudoUniform(w - 2 * MATCH_SZ_BY2);
90     int y2 = MATCH_SZ_BY2 + rnd_.PseudoUniform(h - 2 * MATCH_SZ_BY2);
91 
92     double res_c =
93         svt_av1_compute_cross_correlation_c(input1, w, x1, y1, input2, w, x2, y2);
94     double res_simd = target_func(input1, w, x1, y1, input2, w, x2, y2);
95 
96     if (run_times > 1) {
97       svt_av1_get_time(&start_time_seconds, &start_time_useconds);
98       for (j = 0; j < run_times; j++) {
99           svt_av1_compute_cross_correlation_c(
100               input1, w, x1, y1, input2, w, x2, y2);
101       }
102       svt_av1_get_time(&middle_time_seconds, &middle_time_useconds);
103 
104       for (j = 0; j < run_times; j++) {
105         target_func(input1, w, x1, y1, input2, w, x2, y2);
106       }
107 
108       svt_av1_get_time(&finish_time_seconds, &finish_time_useconds);
109 
110       time = svt_av1_compute_overall_elapsed_time_ms(start_time_seconds,
111                                                      start_time_useconds,
112                                                      middle_time_seconds,
113                                                      middle_time_useconds);
114       time_c += time;
115       time = svt_av1_compute_overall_elapsed_time_ms(middle_time_seconds,
116                                                      middle_time_useconds,
117                                                      finish_time_seconds,
118                                                      finish_time_useconds);
119       time_o += time;
120 
121 
122 
123     } else {
124       ASSERT_EQ(res_simd, res_c);
125     }
126   }
127 
128    if (run_times > 1) {
129       printf("Average Nanoseconds per Function Call\n");
130       printf("    svt_av1_compute_cross_correlation_c : %6.2f\n",
131              1000000 * time_c / run_times * num_iters);
132       printf(
133           "    av1_compute_cross_correlation (AVX2) : %6.2f   (Comparison: "
134           "%5.2fx)\n",
135           1000000 * time_o / run_times * num_iters,
136           time_c / time_o);
137 
138    }
139 
140   delete[] input1;
141   delete[] input2;
142 }
143 
TEST_P(AV1CornerMatchTest,CheckOutput)144 TEST_P(AV1CornerMatchTest, CheckOutput) { RunCheckOutput(1); }
TEST_P(AV1CornerMatchTest,DISABLED_Speed)145 TEST_P(AV1CornerMatchTest, DISABLED_Speed) { RunCheckOutput(1000); }
146 
147 INSTANTIATE_TEST_CASE_P(
148     AV1CornerMatchTest, AV1CornerMatchTest,
149     ::testing::Values(make_tuple(0, &svt_av1_compute_cross_correlation_avx2),
150                       make_tuple(1, &svt_av1_compute_cross_correlation_avx2)));
151 
152 }
153