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 #include <memory>
12 #include <new>
13 #include <tuple>
14
15 #include "config/aom_dsp_rtcd.h"
16
17 #include "gtest/gtest.h"
18 #include "test/acm_random.h"
19 #include "test/util.h"
20 #include "test/register_state_check.h"
21
22 #include "aom_dsp/flow_estimation/corner_match.h"
23
24 namespace test_libaom {
25
26 namespace AV1CornerMatch {
27
28 using libaom_test::ACMRandom;
29
30 typedef bool (*ComputeMeanStddevFunc)(const unsigned char *frame, int stride,
31 int x, int y, double *mean,
32 double *one_over_stddev);
33 typedef double (*ComputeCorrFunc)(const unsigned char *frame1, int stride1,
34 int x1, int y1, double mean1,
35 double one_over_stddev1,
36 const unsigned char *frame2, int stride2,
37 int x2, int y2, double mean2,
38 double one_over_stddev2);
39
40 using std::make_tuple;
41 using std::tuple;
42 typedef tuple<int, ComputeMeanStddevFunc, ComputeCorrFunc> CornerMatchParam;
43
44 class AV1CornerMatchTest : public ::testing::TestWithParam<CornerMatchParam> {
45 public:
46 ~AV1CornerMatchTest() override;
47 void SetUp() override;
48
49 protected:
50 void GenerateInput(uint8_t *input1, uint8_t *input2, int w, int h, int mode);
51 void RunCheckOutput();
52 void RunSpeedTest();
53 ComputeMeanStddevFunc target_compute_mean_stddev_func;
54 ComputeCorrFunc target_compute_corr_func;
55
56 libaom_test::ACMRandom rnd_;
57 };
58 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AV1CornerMatchTest);
59
60 AV1CornerMatchTest::~AV1CornerMatchTest() = default;
SetUp()61 void AV1CornerMatchTest::SetUp() {
62 rnd_.Reset(ACMRandom::DeterministicSeed());
63 target_compute_mean_stddev_func = GET_PARAM(1);
64 target_compute_corr_func = GET_PARAM(2);
65 }
66
GenerateInput(uint8_t * input1,uint8_t * input2,int w,int h,int mode)67 void AV1CornerMatchTest::GenerateInput(uint8_t *input1, uint8_t *input2, int w,
68 int h, int mode) {
69 if (mode == 0) {
70 for (int i = 0; i < h; ++i)
71 for (int j = 0; j < w; ++j) {
72 input1[i * w + j] = rnd_.Rand8();
73 input2[i * w + j] = rnd_.Rand8();
74 }
75 } else if (mode == 1) {
76 for (int i = 0; i < h; ++i)
77 for (int j = 0; j < w; ++j) {
78 int v = rnd_.Rand8();
79 input1[i * w + j] = v;
80 input2[i * w + j] = (v / 2) + (rnd_.Rand8() & 15);
81 }
82 }
83 }
84
RunCheckOutput()85 void AV1CornerMatchTest::RunCheckOutput() {
86 const int w = 128, h = 128;
87 const int num_iters = 1000;
88
89 std::unique_ptr<uint8_t[]> input1(new (std::nothrow) uint8_t[w * h]);
90 std::unique_ptr<uint8_t[]> input2(new (std::nothrow) uint8_t[w * h]);
91 ASSERT_NE(input1, nullptr);
92 ASSERT_NE(input2, nullptr);
93
94 // Test the two extreme cases:
95 // i) Random data, should have correlation close to 0
96 // ii) Linearly related data + noise, should have correlation close to 1
97 int mode = GET_PARAM(0);
98 GenerateInput(&input1[0], &input2[0], w, h, mode);
99
100 for (int i = 0; i < num_iters; ++i) {
101 int x1 = MATCH_SZ_BY2 + rnd_.PseudoUniform(w + 1 - MATCH_SZ);
102 int y1 = MATCH_SZ_BY2 + rnd_.PseudoUniform(h + 1 - MATCH_SZ);
103 int x2 = MATCH_SZ_BY2 + rnd_.PseudoUniform(w + 1 - MATCH_SZ);
104 int y2 = MATCH_SZ_BY2 + rnd_.PseudoUniform(h + 1 - MATCH_SZ);
105
106 double c_mean1, c_one_over_stddev1, c_mean2, c_one_over_stddev2;
107 bool c_valid1 = aom_compute_mean_stddev_c(input1.get(), w, x1, y1, &c_mean1,
108 &c_one_over_stddev1);
109 bool c_valid2 = aom_compute_mean_stddev_c(input2.get(), w, x2, y2, &c_mean2,
110 &c_one_over_stddev2);
111
112 double simd_mean1, simd_one_over_stddev1, simd_mean2, simd_one_over_stddev2;
113 bool simd_valid1 = target_compute_mean_stddev_func(
114 input1.get(), w, x1, y1, &simd_mean1, &simd_one_over_stddev1);
115 bool simd_valid2 = target_compute_mean_stddev_func(
116 input2.get(), w, x2, y2, &simd_mean2, &simd_one_over_stddev2);
117
118 // Run the correlation calculation even if one of the "valid" flags is
119 // false, i.e. if one of the patches doesn't have enough variance. This is
120 // safe because any potential division by 0 is caught in
121 // aom_compute_mean_stddev(), and one_over_stddev is set to 0 instead.
122 // This causes aom_compute_correlation() to return 0, without causing a
123 // division by 0.
124 const double c_corr = aom_compute_correlation_c(
125 input1.get(), w, x1, y1, c_mean1, c_one_over_stddev1, input2.get(), w,
126 x2, y2, c_mean2, c_one_over_stddev2);
127 const double simd_corr = target_compute_corr_func(
128 input1.get(), w, x1, y1, c_mean1, c_one_over_stddev1, input2.get(), w,
129 x2, y2, c_mean2, c_one_over_stddev2);
130
131 ASSERT_EQ(simd_valid1, c_valid1);
132 ASSERT_EQ(simd_valid2, c_valid2);
133 ASSERT_EQ(simd_mean1, c_mean1);
134 ASSERT_EQ(simd_one_over_stddev1, c_one_over_stddev1);
135 ASSERT_EQ(simd_mean2, c_mean2);
136 ASSERT_EQ(simd_one_over_stddev2, c_one_over_stddev2);
137 ASSERT_EQ(simd_corr, c_corr);
138 }
139 }
140
RunSpeedTest()141 void AV1CornerMatchTest::RunSpeedTest() {
142 const int w = 16, h = 16;
143 const int num_iters = 1000000;
144 aom_usec_timer ref_timer, test_timer;
145
146 std::unique_ptr<uint8_t[]> input1(new (std::nothrow) uint8_t[w * h]);
147 std::unique_ptr<uint8_t[]> input2(new (std::nothrow) uint8_t[w * h]);
148 ASSERT_NE(input1, nullptr);
149 ASSERT_NE(input2, nullptr);
150
151 // Test the two extreme cases:
152 // i) Random data, should have correlation close to 0
153 // ii) Linearly related data + noise, should have correlation close to 1
154 int mode = GET_PARAM(0);
155 GenerateInput(&input1[0], &input2[0], w, h, mode);
156
157 // Time aom_compute_mean_stddev()
158 double c_mean1, c_one_over_stddev1, c_mean2, c_one_over_stddev2;
159 aom_usec_timer_start(&ref_timer);
160 for (int i = 0; i < num_iters; i++) {
161 aom_compute_mean_stddev_c(input1.get(), w, 0, 0, &c_mean1,
162 &c_one_over_stddev1);
163 aom_compute_mean_stddev_c(input2.get(), w, 0, 0, &c_mean2,
164 &c_one_over_stddev2);
165 }
166 aom_usec_timer_mark(&ref_timer);
167 int elapsed_time_c = static_cast<int>(aom_usec_timer_elapsed(&ref_timer));
168
169 double simd_mean1, simd_one_over_stddev1, simd_mean2, simd_one_over_stddev2;
170 aom_usec_timer_start(&test_timer);
171 for (int i = 0; i < num_iters; i++) {
172 target_compute_mean_stddev_func(input1.get(), w, 0, 0, &simd_mean1,
173 &simd_one_over_stddev1);
174 target_compute_mean_stddev_func(input2.get(), w, 0, 0, &simd_mean2,
175 &simd_one_over_stddev2);
176 }
177 aom_usec_timer_mark(&test_timer);
178 int elapsed_time_simd = static_cast<int>(aom_usec_timer_elapsed(&test_timer));
179
180 printf(
181 "aom_compute_mean_stddev(): c_time=%6d simd_time=%6d "
182 "gain=%.3f\n",
183 elapsed_time_c, elapsed_time_simd,
184 (elapsed_time_c / (double)elapsed_time_simd));
185
186 // Time aom_compute_correlation
187 aom_usec_timer_start(&ref_timer);
188 for (int i = 0; i < num_iters; i++) {
189 aom_compute_correlation_c(input1.get(), w, 0, 0, c_mean1,
190 c_one_over_stddev1, input2.get(), w, 0, 0,
191 c_mean2, c_one_over_stddev2);
192 }
193 aom_usec_timer_mark(&ref_timer);
194 elapsed_time_c = static_cast<int>(aom_usec_timer_elapsed(&ref_timer));
195
196 aom_usec_timer_start(&test_timer);
197 for (int i = 0; i < num_iters; i++) {
198 target_compute_corr_func(input1.get(), w, 0, 0, c_mean1, c_one_over_stddev1,
199 input2.get(), w, 0, 0, c_mean2,
200 c_one_over_stddev2);
201 }
202 aom_usec_timer_mark(&test_timer);
203 elapsed_time_simd = static_cast<int>(aom_usec_timer_elapsed(&test_timer));
204
205 printf(
206 "aom_compute_correlation(): c_time=%6d simd_time=%6d "
207 "gain=%.3f\n",
208 elapsed_time_c, elapsed_time_simd,
209 (elapsed_time_c / (double)elapsed_time_simd));
210 }
211
TEST_P(AV1CornerMatchTest,CheckOutput)212 TEST_P(AV1CornerMatchTest, CheckOutput) { RunCheckOutput(); }
TEST_P(AV1CornerMatchTest,DISABLED_Speed)213 TEST_P(AV1CornerMatchTest, DISABLED_Speed) { RunSpeedTest(); }
214
215 #if HAVE_SSE4_1
216 INSTANTIATE_TEST_SUITE_P(
217 SSE4_1, AV1CornerMatchTest,
218 ::testing::Values(make_tuple(0, &aom_compute_mean_stddev_sse4_1,
219 &aom_compute_correlation_sse4_1),
220 make_tuple(1, &aom_compute_mean_stddev_sse4_1,
221 &aom_compute_correlation_sse4_1)));
222 #endif
223
224 #if HAVE_AVX2
225 INSTANTIATE_TEST_SUITE_P(
226 AVX2, AV1CornerMatchTest,
227 ::testing::Values(make_tuple(0, &aom_compute_mean_stddev_avx2,
228 &aom_compute_correlation_avx2),
229 make_tuple(1, &aom_compute_mean_stddev_avx2,
230 &aom_compute_correlation_avx2)));
231 #endif
232 } // namespace AV1CornerMatch
233
234 } // namespace test_libaom
235