1 /*
2 * Copyright (c) 2023, 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 "aom_dsp/flow_estimation/disflow.h"
13
14 #include "gtest/gtest.h"
15
16 #include "config/aom_dsp_rtcd.h"
17 #include "test/acm_random.h"
18 #include "test/register_state_check.h"
19 #include "test/util.h"
20 #include "test/yuv_video_source.h"
21
22 namespace {
23
24 using ComputeFlowAtPointFunc = void (*)(const uint8_t *src, const uint8_t *ref,
25 int x, int y, int width, int height,
26 int stride, double *u, double *v);
27
28 class ComputeFlowTest
29 : public ::testing::TestWithParam<ComputeFlowAtPointFunc> {
30 public:
ComputeFlowTest()31 ComputeFlowTest()
32 : target_func_(GetParam()),
33 rnd_(libaom_test::ACMRandom::DeterministicSeed()) {}
34
35 protected:
36 void RunCheckOutput(int run_times);
37 ComputeFlowAtPointFunc target_func_;
38
39 libaom_test::ACMRandom rnd_;
40 };
41 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ComputeFlowTest);
42
RunCheckOutput(int run_times)43 void ComputeFlowTest::RunCheckOutput(int run_times) {
44 constexpr int kWidth = 352;
45 constexpr int kHeight = 288;
46
47 ::libaom_test::YUVVideoSource video("bus_352x288_420_f20_b8.yuv",
48 AOM_IMG_FMT_I420, kWidth, kHeight, 30, 1,
49 0, 2);
50 // Use Y (Luminance) plane.
51 video.Begin();
52 uint8_t *src = video.img()->planes[0];
53 ASSERT_NE(src, nullptr);
54 video.Next();
55 uint8_t *ref = video.img()->planes[0];
56 ASSERT_NE(ref, nullptr);
57
58 // Pick a random value between -5 and 5. The range was chosen arbitrarily as
59 // u and v can take any kind of value in practise, but it shouldn't change the
60 // outcome of the tests.
61 const double u_rand = (static_cast<double>(rnd_.Rand8()) / 255) * 10 - 5;
62 double u_ref = u_rand;
63 double u_test = u_rand;
64
65 const double v_rand = (static_cast<double>(rnd_.Rand8()) / 255) * 10 - 5;
66 double v_ref = v_rand;
67 double v_test = v_rand;
68
69 // Pick a random point in the frame. If the frame is 352x288, that means we
70 // can call the function on all values of x comprised between 8 and 344, and
71 // all values of y comprised between 8 and 280.
72 const int x = rnd_((kWidth - 8) - 8 + 1) + 8;
73 const int y = rnd_((kHeight - 8) - 8 + 1) + 8;
74
75 aom_usec_timer ref_timer, test_timer;
76
77 aom_compute_flow_at_point_c(src, ref, x, y, kWidth, kHeight, kWidth, &u_ref,
78 &v_ref);
79
80 target_func_(src, ref, x, y, kWidth, kHeight, kWidth, &u_test, &v_test);
81
82 if (run_times > 1) {
83 aom_usec_timer_start(&ref_timer);
84 for (int i = 0; i < run_times; ++i) {
85 aom_compute_flow_at_point_c(src, ref, x, y, kWidth, kHeight, kWidth,
86 &u_ref, &v_ref);
87 }
88 aom_usec_timer_mark(&ref_timer);
89 const double elapsed_time_c =
90 static_cast<double>(aom_usec_timer_elapsed(&ref_timer));
91
92 aom_usec_timer_start(&test_timer);
93 for (int i = 0; i < run_times; ++i) {
94 target_func_(src, ref, x, y, kWidth, kHeight, kWidth, &u_test, &v_test);
95 }
96 aom_usec_timer_mark(&test_timer);
97 const double elapsed_time_simd =
98 static_cast<double>(aom_usec_timer_elapsed(&test_timer));
99
100 printf("c_time=%fns \t simd_time=%fns \t speedup=%.2f\n", elapsed_time_c,
101 elapsed_time_simd, (elapsed_time_c / elapsed_time_simd));
102 } else {
103 ASSERT_EQ(u_ref, u_test);
104 ASSERT_EQ(v_ref, v_test);
105 }
106 }
107
TEST_P(ComputeFlowTest,CheckOutput)108 TEST_P(ComputeFlowTest, CheckOutput) { RunCheckOutput(1); }
109
TEST_P(ComputeFlowTest,DISABLED_Speed)110 TEST_P(ComputeFlowTest, DISABLED_Speed) { RunCheckOutput(10000000); }
111
112 #if HAVE_SSE4_1
113 INSTANTIATE_TEST_SUITE_P(SSE4_1, ComputeFlowTest,
114 ::testing::Values(aom_compute_flow_at_point_sse4_1));
115 #endif
116
117 #if HAVE_AVX2
118 INSTANTIATE_TEST_SUITE_P(AVX2, ComputeFlowTest,
119 ::testing::Values(aom_compute_flow_at_point_avx2));
120 #endif
121
122 #if HAVE_NEON
123 INSTANTIATE_TEST_SUITE_P(NEON, ComputeFlowTest,
124 ::testing::Values(aom_compute_flow_at_point_neon));
125 #endif
126
127 #if HAVE_SVE
128 INSTANTIATE_TEST_SUITE_P(SVE, ComputeFlowTest,
129 ::testing::Values(aom_compute_flow_at_point_sve));
130 #endif
131
132 } // namespace
133