xref: /aosp_15_r20/external/libaom/test/horver_correlation_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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 "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   void SetUp() override {
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   void TearDown() override { aom_free(data_buf_); }
42   void RunHorverTest();
43   void RunHorverTest_ExtremeValues();
44   void RunHorverSpeedTest(int run_times);
45 
46  private:
47   HorverFunc target_func_;
48   ACMRandom rng_;
49   int16_t *data_buf_;
50 };
51 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(HorverTest);
52 
RunHorverTest()53 void HorverTest::RunHorverTest() {
54   for (int block_size = 0; block_size < BLOCK_SIZES_ALL; block_size++) {
55     const int w = block_size_wide[block_size];
56     const int h = block_size_high[block_size];
57     for (int iter = 0; iter < 1000 && !HasFatalFailure(); ++iter) {
58       float hcorr_ref = 0.0, vcorr_ref = 0.0;
59       float hcorr_test = 0.0, vcorr_test = 0.0;
60 
61       for (int i = 0; i < MAX_SB_SQUARE; ++i) {
62         data_buf_[i] = (rng_.Rand16() % (1 << 12)) - (1 << 11);
63       }
64 
65       av1_get_horver_correlation_full_c(data_buf_, MAX_SB_SIZE, w, h,
66                                         &hcorr_ref, &vcorr_ref);
67 
68       target_func_(data_buf_, MAX_SB_SIZE, w, h, &hcorr_test, &vcorr_test);
69 
70       ASSERT_LE(fabs(hcorr_ref - hcorr_test), 1e-6)
71           << "hcorr incorrect (" << w << "x" << h << ")";
72       ASSERT_LE(fabs(vcorr_ref - vcorr_test), 1e-6)
73           << "vcorr incorrect (" << w << "x" << h << ")";
74     }
75     //    printf("(%3dx%-3d) passed\n", w, h);
76   }
77 }
78 
RunHorverSpeedTest(int run_times)79 void HorverTest::RunHorverSpeedTest(int run_times) {
80   for (int i = 0; i < MAX_SB_SQUARE; ++i) {
81     data_buf_[i] = rng_.Rand16() % (1 << 12);
82   }
83 
84   for (int block_size = 0; block_size < BLOCK_SIZES_ALL; block_size++) {
85     const int w = block_size_wide[block_size];
86     const int h = block_size_high[block_size];
87     float hcorr_ref = 0.0, vcorr_ref = 0.0;
88     float hcorr_test = 0.0, vcorr_test = 0.0;
89 
90     aom_usec_timer timer;
91     aom_usec_timer_start(&timer);
92     for (int i = 0; i < run_times; ++i) {
93       av1_get_horver_correlation_full_c(data_buf_, MAX_SB_SIZE, w, h,
94                                         &hcorr_ref, &vcorr_ref);
95     }
96     aom_usec_timer_mark(&timer);
97     const double time1 = static_cast<double>(aom_usec_timer_elapsed(&timer));
98     aom_usec_timer_start(&timer);
99     for (int i = 0; i < run_times; ++i) {
100       target_func_(data_buf_, MAX_SB_SIZE, w, h, &hcorr_test, &vcorr_test);
101     }
102     aom_usec_timer_mark(&timer);
103     const double time2 = static_cast<double>(aom_usec_timer_elapsed(&timer));
104 
105     printf("%3dx%-3d:%7.2f/%7.2fns (%3.2f)\n", w, h, time1, time2,
106            time1 / time2);
107   }
108 }
109 
RunHorverTest_ExtremeValues()110 void HorverTest::RunHorverTest_ExtremeValues() {
111   for (int i = 0; i < MAX_SB_SQUARE; ++i) {
112     // Most of get_horver_test is squaring and summing, so simply saturating
113     // the whole buffer is mostly likely to cause an overflow.
114     data_buf_[i] = (1 << 12) - 1;
115   }
116 
117   for (int block_size = 0; block_size < BLOCK_SIZES_ALL; block_size++) {
118     const int w = block_size_wide[block_size];
119     const int h = block_size_high[block_size];
120     float hcorr_ref = 0.0, vcorr_ref = 0.0;
121     float hcorr_test = 0.0, vcorr_test = 0.0;
122 
123     av1_get_horver_correlation_full_c(data_buf_, MAX_SB_SIZE, w, h, &hcorr_ref,
124                                       &vcorr_ref);
125     target_func_(data_buf_, MAX_SB_SIZE, w, h, &hcorr_test, &vcorr_test);
126 
127     ASSERT_LE(fabs(hcorr_ref - hcorr_test), 1e-6) << "hcorr incorrect";
128     ASSERT_LE(fabs(vcorr_ref - vcorr_test), 1e-6) << "vcorr incorrect";
129   }
130 }
131 
TEST_P(HorverTest,RandomValues)132 TEST_P(HorverTest, RandomValues) { RunHorverTest(); }
133 
TEST_P(HorverTest,ExtremeValues)134 TEST_P(HorverTest, ExtremeValues) { RunHorverTest_ExtremeValues(); }
135 
TEST_P(HorverTest,DISABLED_Speed)136 TEST_P(HorverTest, DISABLED_Speed) { RunHorverSpeedTest(100000); }
137 
138 #if HAVE_SSE4_1
139 INSTANTIATE_TEST_SUITE_P(
140     SSE4_1, HorverTest,
141     ::testing::Values(av1_get_horver_correlation_full_sse4_1));
142 #endif  // HAVE_SSE4_1
143 
144 #if HAVE_NEON
145 INSTANTIATE_TEST_SUITE_P(
146     NEON, HorverTest, ::testing::Values(av1_get_horver_correlation_full_neon));
147 #endif  // HAVE_NEON
148 
149 #if HAVE_AVX2
150 INSTANTIATE_TEST_SUITE_P(
151     AVX2, HorverTest, ::testing::Values(av1_get_horver_correlation_full_avx2));
152 #endif  // HAVE_AVX2
153 
154 }  // namespace
155