• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <vector>
14 
15 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
16 
17 #include "config/av1_rtcd.h"
18 
19 #include "aom_ports/aom_timer.h"
20 #include "av1/common/convolve.h"
21 #include "av1/common/resize.h"
22 #include "test/acm_random.h"
23 #include "test/register_state_check.h"
24 #include "test/util.h"
25 
26 namespace {
27 const int kTestIters = 10;
28 const int kPerfIters = 1000;
29 
30 const int kVPad = 32;
31 const int kHPad = 32;
32 
33 using libaom_test::ACMRandom;
34 using std::make_tuple;
35 using std::tuple;
36 
37 template <typename Pixel>
38 class TestImage {
39  public:
TestImage(int w_src,int h,int superres_denom,int x0,int bd)40   TestImage(int w_src, int h, int superres_denom, int x0, int bd)
41       : w_src_(w_src), h_(h), superres_denom_(superres_denom), x0_(x0),
42         bd_(bd) {
43     assert(bd < 16);
44     assert(bd <= 8 * static_cast<int>(sizeof(Pixel)));
45     assert(9 <= superres_denom && superres_denom <= 16);
46     assert(SCALE_NUMERATOR == 8);
47     assert(0 <= x0_ && x0_ <= RS_SCALE_SUBPEL_MASK);
48 
49     w_dst_ = w_src_;
50     av1_calculate_unscaled_superres_size(&w_dst_, nullptr, superres_denom);
51 
52     src_stride_ = ALIGN_POWER_OF_TWO(w_src_ + 2 * kHPad, 4);
53     dst_stride_ = ALIGN_POWER_OF_TWO(w_dst_ + 2 * kHPad, 4);
54 
55     // Allocate image data
56     src_data_.resize(2 * src_block_size());
57     dst_data_.resize(2 * dst_block_size());
58   }
59 
60   void Initialize(ACMRandom *rnd);
61   void Check() const;
62 
src_stride() const63   int src_stride() const { return src_stride_; }
dst_stride() const64   int dst_stride() const { return dst_stride_; }
65 
src_block_size() const66   int src_block_size() const { return (h_ + 2 * kVPad) * src_stride(); }
dst_block_size() const67   int dst_block_size() const { return (h_ + 2 * kVPad) * dst_stride(); }
68 
src_width() const69   int src_width() const { return w_src_; }
dst_width() const70   int dst_width() const { return w_dst_; }
height() const71   int height() const { return h_; }
x0() const72   int x0() const { return x0_; }
73 
GetSrcData(bool ref,bool borders) const74   const Pixel *GetSrcData(bool ref, bool borders) const {
75     const Pixel *block = &src_data_[ref ? 0 : src_block_size()];
76     return borders ? block : block + kHPad + src_stride_ * kVPad;
77   }
78 
GetDstData(bool ref,bool borders)79   Pixel *GetDstData(bool ref, bool borders) {
80     Pixel *block = &dst_data_[ref ? 0 : dst_block_size()];
81     return borders ? block : block + kHPad + dst_stride_ * kVPad;
82   }
83 
84  private:
85   int w_src_, w_dst_, h_, superres_denom_, x0_, bd_;
86   int src_stride_, dst_stride_;
87 
88   std::vector<Pixel> src_data_;
89   std::vector<Pixel> dst_data_;
90 };
91 
92 template <typename Pixel>
FillEdge(ACMRandom * rnd,int num_pixels,int bd,bool trash,Pixel * data)93 void FillEdge(ACMRandom *rnd, int num_pixels, int bd, bool trash, Pixel *data) {
94   if (!trash) {
95     memset(data, 0, sizeof(*data) * num_pixels);
96     return;
97   }
98   const Pixel mask = (1 << bd) - 1;
99   for (int i = 0; i < num_pixels; ++i) data[i] = rnd->Rand16() & mask;
100 }
101 
102 template <typename Pixel>
PrepBuffers(ACMRandom * rnd,int w,int h,int stride,int bd,bool trash_edges,Pixel * data)103 void PrepBuffers(ACMRandom *rnd, int w, int h, int stride, int bd,
104                  bool trash_edges, Pixel *data) {
105   assert(rnd);
106   const Pixel mask = (1 << bd) - 1;
107 
108   // Fill in the first buffer with random data
109   // Top border
110   FillEdge(rnd, stride * kVPad, bd, trash_edges, data);
111   for (int r = 0; r < h; ++r) {
112     Pixel *row_data = data + (kVPad + r) * stride;
113     // Left border, contents, right border
114     FillEdge(rnd, kHPad, bd, trash_edges, row_data);
115     for (int c = 0; c < w; ++c) row_data[kHPad + c] = rnd->Rand16() & mask;
116     FillEdge(rnd, kHPad, bd, trash_edges, row_data + kHPad + w);
117   }
118   // Bottom border
119   FillEdge(rnd, stride * kVPad, bd, trash_edges, data + stride * (kVPad + h));
120 
121   const int bpp = sizeof(*data);
122   const int block_elts = stride * (h + 2 * kVPad);
123   const int block_size = bpp * block_elts;
124 
125   // Now copy that to the second buffer
126   memcpy(data + block_elts, data, block_size);
127 }
128 
129 template <typename Pixel>
Initialize(ACMRandom * rnd)130 void TestImage<Pixel>::Initialize(ACMRandom *rnd) {
131   PrepBuffers(rnd, w_src_, h_, src_stride_, bd_, false, &src_data_[0]);
132   PrepBuffers(rnd, w_dst_, h_, dst_stride_, bd_, true, &dst_data_[0]);
133 }
134 
135 template <typename Pixel>
Check() const136 void TestImage<Pixel>::Check() const {
137   const int num_pixels = dst_block_size();
138   const Pixel *ref_dst = &dst_data_[0];
139   const Pixel *tst_dst = &dst_data_[num_pixels];
140 
141   // If memcmp returns 0, there's nothing to do.
142   if (0 == memcmp(ref_dst, tst_dst, sizeof(*ref_dst) * num_pixels)) return;
143 
144   // Otherwise, iterate through the buffer looking for differences, *ignoring
145   // the edges*
146   const int stride = dst_stride_;
147   for (int r = kVPad; r < h_ + kVPad; ++r) {
148     for (int c = kVPad; c < w_dst_ + kHPad; ++c) {
149       const int32_t ref_value = ref_dst[r * stride + c];
150       const int32_t tst_value = tst_dst[r * stride + c];
151 
152       EXPECT_EQ(tst_value, ref_value)
153           << "Error at row: " << (r - kVPad) << ", col: " << (c - kHPad)
154           << ", superres_denom: " << superres_denom_ << ", height: " << h_
155           << ", src_width: " << w_src_ << ", dst_width: " << w_dst_
156           << ", x0: " << x0_;
157     }
158   }
159 }
160 
161 template <typename Pixel>
162 class ConvolveHorizRSTestBase : public ::testing::Test {
163  public:
ConvolveHorizRSTestBase()164   ConvolveHorizRSTestBase() : image_(nullptr) {}
165   ~ConvolveHorizRSTestBase() override = default;
166 
167   // Implemented by subclasses (SetUp depends on the parameters passed
168   // in and RunOne depends on the function to be tested. These can't
169   // be templated for low/high bit depths because they have different
170   // numbers of parameters)
171   void SetUp() override = 0;
172   virtual void RunOne(bool ref) = 0;
173 
174  protected:
SetBitDepth(int bd)175   void SetBitDepth(int bd) { bd_ = bd; }
176 
CorrectnessTest()177   void CorrectnessTest() {
178     ACMRandom rnd(ACMRandom::DeterministicSeed());
179     for (int i = 0; i < kTestIters; ++i) {
180       for (int superres_denom = 9; superres_denom <= 16; superres_denom++) {
181         // Get a random height between 512 and 767
182         int height = rnd.Rand8() + 512;
183 
184         // Get a random src width between 128 and 383
185         int width_src = rnd.Rand8() + 128;
186 
187         // x0 is normally calculated by get_upscale_convolve_x0 in
188         // av1/common/resize.c. However, this test should work for
189         // any value of x0 between 0 and RS_SCALE_SUBPEL_MASK
190         // (inclusive), so we choose one at random.
191         int x0 = rnd.Rand16() % (RS_SCALE_SUBPEL_MASK + 1);
192 
193         image_ =
194             new TestImage<Pixel>(width_src, height, superres_denom, x0, bd_);
195         ASSERT_NE(image_, nullptr);
196 
197         Prep(&rnd);
198         RunOne(true);
199         RunOne(false);
200         image_->Check();
201 
202         delete image_;
203       }
204     }
205   }
206 
SpeedTest()207   void SpeedTest() {
208     // Pick some specific parameters to test
209     int height = 767;
210     int width_src = 129;
211     int superres_denom = 13;
212     int x0 = RS_SCALE_SUBPEL_MASK >> 1;
213 
214     image_ = new TestImage<Pixel>(width_src, height, superres_denom, x0, bd_);
215     ASSERT_NE(image_, nullptr);
216 
217     ACMRandom rnd(ACMRandom::DeterministicSeed());
218     Prep(&rnd);
219 
220     aom_usec_timer ref_timer;
221     aom_usec_timer_start(&ref_timer);
222     for (int i = 0; i < kPerfIters; ++i) RunOne(true);
223     aom_usec_timer_mark(&ref_timer);
224     const int64_t ref_time = aom_usec_timer_elapsed(&ref_timer);
225 
226     aom_usec_timer tst_timer;
227     aom_usec_timer_start(&tst_timer);
228     for (int i = 0; i < kPerfIters; ++i) RunOne(false);
229     aom_usec_timer_mark(&tst_timer);
230     const int64_t tst_time = aom_usec_timer_elapsed(&tst_timer);
231 
232     std::cout << "[          ] C time = " << ref_time / 1000
233               << " ms, SIMD time = " << tst_time / 1000 << " ms\n";
234 
235     EXPECT_GT(ref_time, tst_time)
236         << "Error: ConvolveHorizRSTest (Speed Test), SIMD slower than C.\n"
237         << "C time: " << ref_time << " us\n"
238         << "SIMD time: " << tst_time << " us\n";
239   }
240 
Prep(ACMRandom * rnd)241   void Prep(ACMRandom *rnd) {
242     assert(rnd);
243     image_->Initialize(rnd);
244   }
245 
246   int bd_;
247   TestImage<Pixel> *image_;
248 };
249 
250 typedef void (*LowBDConvolveHorizRsFunc)(const uint8_t *src, int src_stride,
251                                          uint8_t *dst, int dst_stride, int w,
252                                          int h, const int16_t *x_filters,
253                                          const int x0_qn, const int x_step_qn);
254 
255 // Test parameter list:
256 //  <tst_fun_>
257 typedef tuple<LowBDConvolveHorizRsFunc> LowBDParams;
258 
259 class LowBDConvolveHorizRSTest
260     : public ConvolveHorizRSTestBase<uint8_t>,
261       public ::testing::WithParamInterface<LowBDParams> {
262  public:
263   ~LowBDConvolveHorizRSTest() override = default;
264 
SetUp()265   void SetUp() override {
266     tst_fun_ = GET_PARAM(0);
267     const int bd = 8;
268     SetBitDepth(bd);
269   }
270 
RunOne(bool ref)271   void RunOne(bool ref) override {
272     const uint8_t *src = image_->GetSrcData(ref, false);
273     uint8_t *dst = image_->GetDstData(ref, false);
274     const int src_stride = image_->src_stride();
275     const int dst_stride = image_->dst_stride();
276     const int width_src = image_->src_width();
277     const int width_dst = image_->dst_width();
278     const int height = image_->height();
279     const int x0_qn = image_->x0();
280 
281     const int32_t x_step_qn =
282         av1_get_upscale_convolve_step(width_src, width_dst);
283 
284     if (ref) {
285       av1_convolve_horiz_rs_c(src, src_stride, dst, dst_stride, width_dst,
286                               height, &av1_resize_filter_normative[0][0], x0_qn,
287                               x_step_qn);
288     } else {
289       tst_fun_(src, src_stride, dst, dst_stride, width_dst, height,
290                &av1_resize_filter_normative[0][0], x0_qn, x_step_qn);
291     }
292   }
293 
294  private:
295   LowBDConvolveHorizRsFunc tst_fun_;
296 };
297 
TEST_P(LowBDConvolveHorizRSTest,Correctness)298 TEST_P(LowBDConvolveHorizRSTest, Correctness) { CorrectnessTest(); }
TEST_P(LowBDConvolveHorizRSTest,DISABLED_Speed)299 TEST_P(LowBDConvolveHorizRSTest, DISABLED_Speed) { SpeedTest(); }
300 
301 INSTANTIATE_TEST_SUITE_P(C, LowBDConvolveHorizRSTest,
302                          ::testing::Values(av1_convolve_horiz_rs_c));
303 
304 #if HAVE_SSE4_1
305 INSTANTIATE_TEST_SUITE_P(SSE4_1, LowBDConvolveHorizRSTest,
306                          ::testing::Values(av1_convolve_horiz_rs_sse4_1));
307 #endif
308 
309 #if CONFIG_AV1_HIGHBITDEPTH
310 typedef void (*HighBDConvolveHorizRsFunc)(const uint16_t *src, int src_stride,
311                                           uint16_t *dst, int dst_stride, int w,
312                                           int h, const int16_t *x_filters,
313                                           const int x0_qn, const int x_step_qn,
314                                           int bd);
315 
316 // Test parameter list:
317 //  <tst_fun_, bd_>
318 typedef tuple<HighBDConvolveHorizRsFunc, int> HighBDParams;
319 
320 class HighBDConvolveHorizRSTest
321     : public ConvolveHorizRSTestBase<uint16_t>,
322       public ::testing::WithParamInterface<HighBDParams> {
323  public:
324   ~HighBDConvolveHorizRSTest() override = default;
325 
SetUp()326   void SetUp() override {
327     tst_fun_ = GET_PARAM(0);
328     const int bd = GET_PARAM(1);
329     SetBitDepth(bd);
330   }
331 
RunOne(bool ref)332   void RunOne(bool ref) override {
333     const uint16_t *src = image_->GetSrcData(ref, false);
334     uint16_t *dst = image_->GetDstData(ref, false);
335     const int src_stride = image_->src_stride();
336     const int dst_stride = image_->dst_stride();
337     const int width_src = image_->src_width();
338     const int width_dst = image_->dst_width();
339     const int height = image_->height();
340     const int x0_qn = image_->x0();
341 
342     const int32_t x_step_qn =
343         av1_get_upscale_convolve_step(width_src, width_dst);
344 
345     if (ref) {
346       av1_highbd_convolve_horiz_rs_c(
347           src, src_stride, dst, dst_stride, width_dst, height,
348           &av1_resize_filter_normative[0][0], x0_qn, x_step_qn, bd_);
349     } else {
350       tst_fun_(src, src_stride, dst, dst_stride, width_dst, height,
351                &av1_resize_filter_normative[0][0], x0_qn, x_step_qn, bd_);
352     }
353   }
354 
355  private:
356   HighBDConvolveHorizRsFunc tst_fun_;
357 };
358 
359 const int kBDs[] = { 8, 10, 12 };
360 
TEST_P(HighBDConvolveHorizRSTest,Correctness)361 TEST_P(HighBDConvolveHorizRSTest, Correctness) { CorrectnessTest(); }
TEST_P(HighBDConvolveHorizRSTest,DISABLED_Speed)362 TEST_P(HighBDConvolveHorizRSTest, DISABLED_Speed) { SpeedTest(); }
363 
364 INSTANTIATE_TEST_SUITE_P(
365     C, HighBDConvolveHorizRSTest,
366     ::testing::Combine(::testing::Values(av1_highbd_convolve_horiz_rs_c),
367                        ::testing::ValuesIn(kBDs)));
368 
369 #if HAVE_SSE4_1
370 INSTANTIATE_TEST_SUITE_P(
371     SSE4_1, HighBDConvolveHorizRSTest,
372     ::testing::Combine(::testing::Values(av1_highbd_convolve_horiz_rs_sse4_1),
373                        ::testing::ValuesIn(kBDs)));
374 #endif  // HAVE_SSE4_1
375 
376 #if HAVE_NEON
377 INSTANTIATE_TEST_SUITE_P(
378     NEON, HighBDConvolveHorizRSTest,
379     ::testing::Combine(::testing::Values(av1_highbd_convolve_horiz_rs_neon),
380                        ::testing::ValuesIn(kBDs)));
381 #endif  // HAVE_NEON
382 
383 #endif  // CONFIG_AV1_HIGHBITDEPTH
384 
385 }  // namespace
386