1 /*
2 * Copyright (c) 2018, 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 <tuple>
13
14 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
15
16 #include "test/acm_random.h"
17 #include "test/register_state_check.h"
18 #include "test/util.h"
19
20 #include "config/aom_config.h"
21 #include "config/aom_dsp_rtcd.h"
22 #include "config/av1_rtcd.h"
23
24 #include "aom/aom_integer.h"
25
26 using libaom_test::ACMRandom;
27
28 namespace {
29 typedef void (*HorverFunc)(const int16_t *diff, int stride, int w, int h,
30 float *hcorr, float *vcorr);
31
32 typedef std::tuple<const HorverFunc> HorverTestParam;
33
34 class HorverTest : public ::testing::TestWithParam<HorverTestParam> {
35 public:
SetUp()36 virtual void SetUp() {
37 data_buf_ = (int16_t *)aom_malloc(MAX_SB_SQUARE * sizeof(int16_t));
38 ASSERT_NE(data_buf_, nullptr);
39 target_func_ = GET_PARAM(0);
40 }
TearDown()41 virtual void TearDown() { aom_free(data_buf_); }
42 void RunHorverTest(void);
43 void RunHorverTest_ExtremeValues(void);
44 void RunHorverSpeedTest(int run_times);
45
46 private:
47 HorverFunc target_func_;
48 ACMRandom rng_;
49 int16_t *data_buf_;
50 };
51
RunHorverTest(void)52 void HorverTest::RunHorverTest(void) {
53 for (int block_size = 0; block_size < BLOCK_SIZES_ALL; block_size++) {
54 const int w = block_size_wide[block_size];
55 const int h = block_size_high[block_size];
56 for (int iter = 0; iter < 1000 && !HasFatalFailure(); ++iter) {
57 float hcorr_ref = 0.0, vcorr_ref = 0.0;
58 float hcorr_test = 0.0, vcorr_test = 0.0;
59
60 for (int i = 0; i < MAX_SB_SQUARE; ++i) {
61 data_buf_[i] = (rng_.Rand16() % (1 << 12)) - (1 << 11);
62 }
63
64 av1_get_horver_correlation_full_c(data_buf_, MAX_SB_SIZE, w, h,
65 &hcorr_ref, &vcorr_ref);
66
67 target_func_(data_buf_, MAX_SB_SIZE, w, h, &hcorr_test, &vcorr_test);
68
69 ASSERT_LE(fabs(hcorr_ref - hcorr_test), 1e-6)
70 << "hcorr incorrect (" << w << "x" << h << ")";
71 ASSERT_LE(fabs(vcorr_ref - vcorr_test), 1e-6)
72 << "vcorr incorrect (" << w << "x" << h << ")";
73 }
74 // printf("(%3dx%-3d) passed\n", w, h);
75 }
76 }
77
RunHorverSpeedTest(int run_times)78 void HorverTest::RunHorverSpeedTest(int run_times) {
79 for (int i = 0; i < MAX_SB_SQUARE; ++i) {
80 data_buf_[i] = rng_.Rand16() % (1 << 12);
81 }
82
83 for (int block_size = 0; block_size < BLOCK_SIZES_ALL; block_size++) {
84 const int w = block_size_wide[block_size];
85 const int h = block_size_high[block_size];
86 float hcorr_ref = 0.0, vcorr_ref = 0.0;
87 float hcorr_test = 0.0, vcorr_test = 0.0;
88
89 aom_usec_timer timer;
90 aom_usec_timer_start(&timer);
91 for (int i = 0; i < run_times; ++i) {
92 av1_get_horver_correlation_full_c(data_buf_, MAX_SB_SIZE, w, h,
93 &hcorr_ref, &vcorr_ref);
94 }
95 aom_usec_timer_mark(&timer);
96 const double time1 = static_cast<double>(aom_usec_timer_elapsed(&timer));
97 aom_usec_timer_start(&timer);
98 for (int i = 0; i < run_times; ++i) {
99 target_func_(data_buf_, MAX_SB_SIZE, w, h, &hcorr_test, &vcorr_test);
100 }
101 aom_usec_timer_mark(&timer);
102 const double time2 = static_cast<double>(aom_usec_timer_elapsed(&timer));
103
104 printf("%3dx%-3d:%7.2f/%7.2fns (%3.2f)\n", w, h, time1, time2,
105 time1 / time2);
106 }
107 }
108
RunHorverTest_ExtremeValues(void)109 void HorverTest::RunHorverTest_ExtremeValues(void) {
110 for (int i = 0; i < MAX_SB_SQUARE; ++i) {
111 // Most of get_horver_test is squaring and summing, so simply saturating
112 // the whole buffer is mostly likely to cause an overflow.
113 data_buf_[i] = (1 << 12) - 1;
114 }
115
116 for (int block_size = 0; block_size < BLOCK_SIZES_ALL; block_size++) {
117 const int w = block_size_wide[block_size];
118 const int h = block_size_high[block_size];
119 float hcorr_ref = 0.0, vcorr_ref = 0.0;
120 float hcorr_test = 0.0, vcorr_test = 0.0;
121
122 av1_get_horver_correlation_full_c(data_buf_, MAX_SB_SIZE, w, h, &hcorr_ref,
123 &vcorr_ref);
124 target_func_(data_buf_, MAX_SB_SIZE, w, h, &hcorr_test, &vcorr_test);
125
126 ASSERT_LE(fabs(hcorr_ref - hcorr_test), 1e-6) << "hcorr incorrect";
127 ASSERT_LE(fabs(vcorr_ref - vcorr_test), 1e-6) << "vcorr incorrect";
128 }
129 }
130
TEST_P(HorverTest,RandomValues)131 TEST_P(HorverTest, RandomValues) { RunHorverTest(); }
132
TEST_P(HorverTest,ExtremeValues)133 TEST_P(HorverTest, ExtremeValues) { RunHorverTest_ExtremeValues(); }
134
TEST_P(HorverTest,DISABLED_Speed)135 TEST_P(HorverTest, DISABLED_Speed) { RunHorverSpeedTest(100000); }
136
137 #if HAVE_SSE4_1
138 INSTANTIATE_TEST_SUITE_P(
139 SSE4_1, HorverTest,
140 ::testing::Values(av1_get_horver_correlation_full_sse4_1));
141 #endif // HAVE_SSE4_1
142
143 #if HAVE_AVX2
144 INSTANTIATE_TEST_SUITE_P(
145 AVX2, HorverTest, ::testing::Values(av1_get_horver_correlation_full_avx2));
146 #endif // HAVE_AVX2
147
148 } // namespace
149