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