Merge changes I02279405,I87e1c3f0,Id70235c8,I62602aa4,I5722d262
[platform/upstream/libvpx.git] / test / comp_avg_pred_test.cc
1 /*
2  *  Copyright (c) 2017 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include "third_party/googletest/src/include/gtest/gtest.h"
12
13 #include "./vpx_dsp_rtcd.h"
14
15 #include "test/acm_random.h"
16 #include "test/buffer.h"
17 #include "test/register_state_check.h"
18 #include "vpx_ports/vpx_timer.h"
19
20 namespace {
21
22 using ::libvpx_test::ACMRandom;
23 using ::libvpx_test::Buffer;
24
25 typedef void (*AvgPredFunc)(uint8_t *a, const uint8_t *b, int w, int h,
26                             const uint8_t *c, int c_stride);
27
28 uint8_t avg_with_rounding(uint8_t a, uint8_t b) { return (a + b + 1) >> 1; }
29
30 void reference_pred(const Buffer<uint8_t> &pred, const Buffer<uint8_t> &ref,
31                     int width, int height, Buffer<uint8_t> *avg) {
32   ASSERT_TRUE(avg->TopLeftPixel() != NULL);
33   ASSERT_TRUE(pred.TopLeftPixel() != NULL);
34   ASSERT_TRUE(ref.TopLeftPixel() != NULL);
35
36   for (int y = 0; y < height; ++y) {
37     for (int x = 0; x < width; ++x) {
38       avg->TopLeftPixel()[y * avg->stride() + x] =
39           avg_with_rounding(pred.TopLeftPixel()[y * pred.stride() + x],
40                             ref.TopLeftPixel()[y * ref.stride() + x]);
41     }
42   }
43 }
44
45 class AvgPredTest : public ::testing::TestWithParam<AvgPredFunc> {
46  public:
47   virtual void SetUp() {
48     avg_pred_func_ = GetParam();
49     rnd_.Reset(ACMRandom::DeterministicSeed());
50   }
51
52  protected:
53   AvgPredFunc avg_pred_func_;
54   ACMRandom rnd_;
55 };
56
57 TEST_P(AvgPredTest, SizeCombinations) {
58   // This is called as part of the sub pixel variance. As such it must be one of
59   // the variance block sizes.
60
61   for (int width_pow = 2; width_pow <= 6; ++width_pow) {
62     for (int height_pow = width_pow - 1; height_pow <= width_pow + 1;
63          ++height_pow) {
64       // Don't test 4x2 or 64x128
65       if (height_pow == 1 || height_pow == 7) continue;
66
67       // The sse2 special-cases when ref width == stride, so make sure to test
68       // it.
69       for (int ref_padding = 0; ref_padding < 2; ref_padding++) {
70         const int width = 1 << width_pow;
71         const int height = 1 << height_pow;
72         // Only the reference buffer may have a stride not equal to width.
73         Buffer<uint8_t> ref =
74             Buffer<uint8_t>(width, height, ref_padding ? 8 : 0);
75         ASSERT_TRUE(ref.Init());
76         Buffer<uint8_t> pred = Buffer<uint8_t>(width, height, 0, 16);
77         ASSERT_TRUE(pred.Init());
78         Buffer<uint8_t> avg_ref = Buffer<uint8_t>(width, height, 0, 16);
79         ASSERT_TRUE(avg_ref.Init());
80         Buffer<uint8_t> avg_chk = Buffer<uint8_t>(width, height, 0, 16);
81         ASSERT_TRUE(avg_chk.Init());
82
83         ref.Set(&rnd_, &ACMRandom::Rand8);
84         pred.Set(&rnd_, &ACMRandom::Rand8);
85
86         reference_pred(pred, ref, width, height, &avg_ref);
87         ASM_REGISTER_STATE_CHECK(
88             avg_pred_func_(avg_chk.TopLeftPixel(), pred.TopLeftPixel(), width,
89                            height, ref.TopLeftPixel(), ref.stride()));
90
91         EXPECT_TRUE(avg_chk.CheckValues(avg_ref));
92         if (HasFailure()) {
93           printf("Width: %d Height: %d\n", width, height);
94           avg_chk.PrintDifference(avg_ref);
95           return;
96         }
97       }
98     }
99   }
100 }
101
102 TEST_P(AvgPredTest, CompareReferenceRandom) {
103   const int width = 64;
104   const int height = 32;
105   Buffer<uint8_t> ref = Buffer<uint8_t>(width, height, 8);
106   ASSERT_TRUE(ref.Init());
107   Buffer<uint8_t> pred = Buffer<uint8_t>(width, height, 0, 16);
108   ASSERT_TRUE(pred.Init());
109   Buffer<uint8_t> avg_ref = Buffer<uint8_t>(width, height, 0, 16);
110   ASSERT_TRUE(avg_ref.Init());
111   Buffer<uint8_t> avg_chk = Buffer<uint8_t>(width, height, 0, 16);
112   ASSERT_TRUE(avg_chk.Init());
113
114   for (int i = 0; i < 500; ++i) {
115     ref.Set(&rnd_, &ACMRandom::Rand8);
116     pred.Set(&rnd_, &ACMRandom::Rand8);
117
118     reference_pred(pred, ref, width, height, &avg_ref);
119     ASM_REGISTER_STATE_CHECK(avg_pred_func_(avg_chk.TopLeftPixel(),
120                                             pred.TopLeftPixel(), width, height,
121                                             ref.TopLeftPixel(), ref.stride()));
122     EXPECT_TRUE(avg_chk.CheckValues(avg_ref));
123     if (HasFailure()) {
124       printf("Width: %d Height: %d\n", width, height);
125       avg_chk.PrintDifference(avg_ref);
126       return;
127     }
128   }
129 }
130
131 TEST_P(AvgPredTest, DISABLED_Speed) {
132   for (int width_pow = 2; width_pow <= 6; ++width_pow) {
133     for (int height_pow = width_pow - 1; height_pow <= width_pow + 1;
134          ++height_pow) {
135       // Don't test 4x2 or 64x128
136       if (height_pow == 1 || height_pow == 7) continue;
137
138       for (int ref_padding = 0; ref_padding < 2; ref_padding++) {
139         const int width = 1 << width_pow;
140         const int height = 1 << height_pow;
141         Buffer<uint8_t> ref =
142             Buffer<uint8_t>(width, height, ref_padding ? 8 : 0);
143         ASSERT_TRUE(ref.Init());
144         Buffer<uint8_t> pred = Buffer<uint8_t>(width, height, 0, 16);
145         ASSERT_TRUE(pred.Init());
146         Buffer<uint8_t> avg = Buffer<uint8_t>(width, height, 0, 16);
147         ASSERT_TRUE(avg.Init());
148
149         ref.Set(&rnd_, &ACMRandom::Rand8);
150         pred.Set(&rnd_, &ACMRandom::Rand8);
151
152         vpx_usec_timer timer;
153         vpx_usec_timer_start(&timer);
154         for (int i = 0; i < 10000000 / (width * height); ++i) {
155           avg_pred_func_(avg.TopLeftPixel(), pred.TopLeftPixel(), width, height,
156                          ref.TopLeftPixel(), ref.stride());
157         }
158         vpx_usec_timer_mark(&timer);
159
160         const int elapsed_time =
161             static_cast<int>(vpx_usec_timer_elapsed(&timer));
162         printf("Average Test (ref_padding: %d) %dx%d time: %5d us\n",
163                ref_padding, width, height, elapsed_time);
164       }
165     }
166   }
167 }
168
169 INSTANTIATE_TEST_CASE_P(C, AvgPredTest,
170                         ::testing::Values(&vpx_comp_avg_pred_c));
171
172 #if HAVE_SSE2
173 INSTANTIATE_TEST_CASE_P(SSE2, AvgPredTest,
174                         ::testing::Values(&vpx_comp_avg_pred_sse2));
175 #endif  // HAVE_SSE2
176
177 #if HAVE_NEON
178 INSTANTIATE_TEST_CASE_P(NEON, AvgPredTest,
179                         ::testing::Values(&vpx_comp_avg_pred_neon));
180 #endif  // HAVE_NEON
181
182 #if HAVE_VSX
183 INSTANTIATE_TEST_CASE_P(VSX, AvgPredTest,
184                         ::testing::Values(&vpx_comp_avg_pred_vsx));
185 #endif  // HAVE_VSX
186 }  // namespace