Imdad Sardharwalla | 454697c | 2018-01-10 14:19:31 +0000 | [diff] [blame] | 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 <vector> |
| 13 | |
| 14 | #include "third_party/googletest/src/googletest/include/gtest/gtest.h" |
| 15 | |
| 16 | #include "./av1_rtcd.h" |
| 17 | #include "aom_ports/aom_timer.h" |
| 18 | #include "av1/common/convolve.h" |
| 19 | #include "av1/common/resize.h" |
| 20 | #include "test/acm_random.h" |
| 21 | #include "test/clear_system_state.h" |
| 22 | #include "test/register_state_check.h" |
| 23 | #include "test/util.h" |
| 24 | |
| 25 | namespace { |
| 26 | const int kTestIters = 10; |
| 27 | const int kPerfIters = 1000; |
| 28 | |
| 29 | const int kVPad = 32; |
| 30 | const int kHPad = 32; |
| 31 | |
Imdad Sardharwalla | 454697c | 2018-01-10 14:19:31 +0000 | [diff] [blame] | 32 | using libaom_test::ACMRandom; |
Johann | f152ff6 | 2018-02-08 14:33:07 -0800 | [diff] [blame] | 33 | using std::tr1::make_tuple; |
| 34 | using std::tr1::tuple; |
Imdad Sardharwalla | 454697c | 2018-01-10 14:19:31 +0000 | [diff] [blame] | 35 | |
| 36 | template <typename Pixel> |
| 37 | class TestImage { |
| 38 | public: |
| 39 | TestImage(int w_src, int h, int superres_denom, int x0, int bd) |
| 40 | : w_src_(w_src), h_(h), superres_denom_(superres_denom), x0_(x0), |
| 41 | bd_(bd) { |
| 42 | assert(bd < 16); |
| 43 | assert(bd <= 8 * static_cast<int>(sizeof(Pixel))); |
| 44 | assert(9 <= superres_denom && superres_denom <= 16); |
| 45 | assert(SCALE_NUMERATOR == 8); |
| 46 | assert(0 <= x0_ && x0_ <= RS_SCALE_SUBPEL_MASK); |
| 47 | |
| 48 | w_dst_ = w_src_; |
| 49 | av1_calculate_unscaled_superres_size(&w_dst_, NULL, superres_denom); |
| 50 | |
| 51 | src_stride_ = ALIGN_POWER_OF_TWO(w_src_ + 2 * kHPad, 4); |
| 52 | dst_stride_ = ALIGN_POWER_OF_TWO(w_dst_ + 2 * kHPad, 4); |
| 53 | |
| 54 | // Allocate image data |
| 55 | src_data_.resize(2 * src_block_size()); |
| 56 | dst_data_.resize(2 * dst_block_size()); |
| 57 | } |
| 58 | |
| 59 | void Initialize(ACMRandom *rnd); |
| 60 | void Check() const; |
| 61 | |
| 62 | int src_stride() const { return src_stride_; } |
| 63 | int dst_stride() const { return dst_stride_; } |
| 64 | |
| 65 | int src_block_size() const { return (h_ + 2 * kVPad) * src_stride(); } |
| 66 | int dst_block_size() const { return (h_ + 2 * kVPad) * dst_stride(); } |
| 67 | |
| 68 | int src_width() const { return w_src_; } |
| 69 | int dst_width() const { return w_dst_; } |
| 70 | int height() const { return h_; } |
| 71 | int x0() const { return x0_; } |
| 72 | |
| 73 | const Pixel *GetSrcData(bool ref, bool borders) const { |
| 74 | const Pixel *block = &src_data_[ref ? 0 : src_block_size()]; |
| 75 | return borders ? block : block + kHPad + src_stride_ * kVPad; |
| 76 | } |
| 77 | |
| 78 | Pixel *GetDstData(bool ref, bool borders) { |
| 79 | Pixel *block = &dst_data_[ref ? 0 : dst_block_size()]; |
| 80 | return borders ? block : block + kHPad + dst_stride_ * kVPad; |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | int w_src_, w_dst_, h_, superres_denom_, x0_, bd_; |
| 85 | int src_stride_, dst_stride_; |
| 86 | |
| 87 | std::vector<Pixel> src_data_; |
| 88 | std::vector<Pixel> dst_data_; |
| 89 | }; |
| 90 | |
| 91 | template <typename Pixel> |
| 92 | void FillEdge(ACMRandom *rnd, int num_pixels, int bd, bool trash, Pixel *data) { |
| 93 | if (!trash) { |
| 94 | memset(data, 0, sizeof(*data) * num_pixels); |
| 95 | return; |
| 96 | } |
| 97 | const Pixel mask = (1 << bd) - 1; |
| 98 | for (int i = 0; i < num_pixels; ++i) data[i] = rnd->Rand16() & mask; |
| 99 | } |
| 100 | |
| 101 | template <typename Pixel> |
| 102 | void PrepBuffers(ACMRandom *rnd, int w, int h, int stride, int bd, |
| 103 | bool trash_edges, Pixel *data) { |
| 104 | assert(rnd); |
| 105 | const Pixel mask = (1 << bd) - 1; |
| 106 | |
| 107 | // Fill in the first buffer with random data |
| 108 | // Top border |
| 109 | FillEdge(rnd, stride * kVPad, bd, trash_edges, data); |
| 110 | for (int r = 0; r < h; ++r) { |
| 111 | Pixel *row_data = data + (kVPad + r) * stride; |
| 112 | // Left border, contents, right border |
| 113 | FillEdge(rnd, kHPad, bd, trash_edges, row_data); |
| 114 | for (int c = 0; c < w; ++c) row_data[kHPad + c] = rnd->Rand16() & mask; |
| 115 | FillEdge(rnd, kHPad, bd, trash_edges, row_data + kHPad + w); |
| 116 | } |
| 117 | // Bottom border |
| 118 | FillEdge(rnd, stride * kVPad, bd, trash_edges, data + stride * (kVPad + h)); |
| 119 | |
| 120 | const int bpp = sizeof(*data); |
| 121 | const int block_elts = stride * (h + 2 * kVPad); |
| 122 | const int block_size = bpp * block_elts; |
| 123 | |
| 124 | // Now copy that to the second buffer |
| 125 | memcpy(data + block_elts, data, block_size); |
| 126 | } |
| 127 | |
| 128 | template <typename Pixel> |
| 129 | void TestImage<Pixel>::Initialize(ACMRandom *rnd) { |
| 130 | PrepBuffers(rnd, w_src_, h_, src_stride_, bd_, false, &src_data_[0]); |
| 131 | PrepBuffers(rnd, w_dst_, h_, dst_stride_, bd_, true, &dst_data_[0]); |
| 132 | } |
| 133 | |
| 134 | template <typename Pixel> |
| 135 | void TestImage<Pixel>::Check() const { |
| 136 | const int num_pixels = dst_block_size(); |
| 137 | const Pixel *ref_dst = &dst_data_[0]; |
| 138 | const Pixel *tst_dst = &dst_data_[num_pixels]; |
| 139 | |
| 140 | // If memcmp returns 0, there's nothing to do. |
| 141 | if (0 == memcmp(ref_dst, tst_dst, sizeof(*ref_dst) * num_pixels)) return; |
| 142 | |
| 143 | // Otherwise, iterate through the buffer looking for differences, *ignoring |
| 144 | // the edges* |
| 145 | const int stride = dst_stride_; |
| 146 | for (int r = kVPad; r < h_ + kVPad; ++r) { |
| 147 | for (int c = kVPad; c < w_dst_ + kHPad; ++c) { |
| 148 | const int32_t ref_value = ref_dst[r * stride + c]; |
| 149 | const int32_t tst_value = tst_dst[r * stride + c]; |
| 150 | |
| 151 | EXPECT_EQ(tst_value, ref_value) |
| 152 | << "Error at row: " << (r - kVPad) << ", col: " << (c - kHPad) |
| 153 | << ", superres_denom: " << superres_denom_ << ", height: " << h_ |
| 154 | << ", src_width: " << w_src_ << ", dst_width: " << w_dst_ |
| 155 | << ", x0: " << x0_; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | template <typename Pixel> |
| 161 | class ConvolveHorizRSTestBase : public ::testing::Test { |
| 162 | public: |
| 163 | ConvolveHorizRSTestBase() : image_(NULL) {} |
| 164 | virtual ~ConvolveHorizRSTestBase() {} |
| 165 | virtual void TearDown() { libaom_test::ClearSystemState(); } |
| 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 | virtual void SetUp() = 0; |
| 172 | virtual void RunOne(bool ref) = 0; |
| 173 | |
| 174 | protected: |
| 175 | void SetBitDepth(int bd) { bd_ = bd; } |
| 176 | |
| 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 | |
| 196 | Prep(&rnd); |
| 197 | RunOne(true); |
| 198 | RunOne(false); |
| 199 | image_->Check(); |
| 200 | |
| 201 | delete image_; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void SpeedTest() { |
| 207 | // Pick some specific parameters to test |
| 208 | int height = 767; |
| 209 | int width_src = 129; |
| 210 | int superres_denom = 13; |
| 211 | int x0 = RS_SCALE_SUBPEL_MASK >> 1; |
| 212 | |
| 213 | image_ = new TestImage<Pixel>(width_src, height, superres_denom, x0, bd_); |
| 214 | |
| 215 | ACMRandom rnd(ACMRandom::DeterministicSeed()); |
| 216 | Prep(&rnd); |
| 217 | |
| 218 | aom_usec_timer ref_timer; |
| 219 | aom_usec_timer_start(&ref_timer); |
| 220 | for (int i = 0; i < kPerfIters; ++i) RunOne(true); |
| 221 | aom_usec_timer_mark(&ref_timer); |
| 222 | const int64_t ref_time = aom_usec_timer_elapsed(&ref_timer); |
| 223 | |
| 224 | aom_usec_timer tst_timer; |
| 225 | aom_usec_timer_start(&tst_timer); |
| 226 | for (int i = 0; i < kPerfIters; ++i) RunOne(false); |
| 227 | aom_usec_timer_mark(&tst_timer); |
| 228 | const int64_t tst_time = aom_usec_timer_elapsed(&tst_timer); |
| 229 | |
| 230 | std::cout << "[ ] C time = " << ref_time / 1000 |
| 231 | << " ms, SIMD time = " << tst_time / 1000 << " ms\n"; |
| 232 | |
| 233 | EXPECT_GT(ref_time, tst_time) |
| 234 | << "Error: CDEFSpeedTest, SIMD slower than C.\n" |
| 235 | << "C time: " << ref_time << " us\n" |
| 236 | << "SIMD time: " << tst_time << " us\n"; |
| 237 | } |
| 238 | |
| 239 | void Prep(ACMRandom *rnd) { |
| 240 | assert(rnd); |
| 241 | image_->Initialize(rnd); |
| 242 | } |
| 243 | |
| 244 | int bd_; |
| 245 | TestImage<Pixel> *image_; |
| 246 | }; |
| 247 | |
| 248 | typedef void (*LowBDConvolveHorizRsFunc)(const uint8_t *src, int src_stride, |
| 249 | uint8_t *dst, int dst_stride, int w, |
| 250 | int h, const int16_t *x_filters, |
| 251 | const int x0_qn, const int x_step_qn); |
| 252 | |
| 253 | // Test parameter list: |
| 254 | // <tst_fun_> |
| 255 | typedef tuple<LowBDConvolveHorizRsFunc> LowBDParams; |
| 256 | |
| 257 | class LowBDConvolveHorizRSTest |
| 258 | : public ConvolveHorizRSTestBase<uint8_t>, |
| 259 | public ::testing::WithParamInterface<LowBDParams> { |
| 260 | public: |
| 261 | virtual ~LowBDConvolveHorizRSTest() {} |
| 262 | |
| 263 | void SetUp() { |
| 264 | tst_fun_ = GET_PARAM(0); |
| 265 | const int bd = 8; |
| 266 | SetBitDepth(bd); |
| 267 | } |
| 268 | |
| 269 | void RunOne(bool ref) { |
| 270 | const uint8_t *src = image_->GetSrcData(ref, false); |
| 271 | uint8_t *dst = image_->GetDstData(ref, false); |
| 272 | const int src_stride = image_->src_stride(); |
| 273 | const int dst_stride = image_->dst_stride(); |
| 274 | const int width_src = image_->src_width(); |
| 275 | const int width_dst = image_->dst_width(); |
| 276 | const int height = image_->height(); |
| 277 | const int x0_qn = image_->x0(); |
| 278 | |
| 279 | const int32_t x_step_qn = |
| 280 | av1_get_upscale_convolve_step(width_src, width_dst); |
| 281 | |
| 282 | if (ref) { |
| 283 | av1_convolve_horiz_rs_c(src, src_stride, dst, dst_stride, width_dst, |
| 284 | height, &av1_resize_filter_normative[0][0], x0_qn, |
| 285 | x_step_qn); |
| 286 | } else { |
| 287 | tst_fun_(src, src_stride, dst, dst_stride, width_dst, height, |
| 288 | &av1_resize_filter_normative[0][0], x0_qn, x_step_qn); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | private: |
| 293 | LowBDConvolveHorizRsFunc tst_fun_; |
| 294 | }; |
| 295 | |
| 296 | TEST_P(LowBDConvolveHorizRSTest, Correctness) { CorrectnessTest(); } |
| 297 | TEST_P(LowBDConvolveHorizRSTest, DISABLED_Speed) { SpeedTest(); } |
| 298 | |
| 299 | INSTANTIATE_TEST_CASE_P(SSE4_1, LowBDConvolveHorizRSTest, |
| 300 | ::testing::Values(av1_convolve_horiz_rs_sse4_1)); |
| 301 | |
| 302 | typedef void (*HighBDConvolveHorizRsFunc)(const uint16_t *src, int src_stride, |
| 303 | uint16_t *dst, int dst_stride, int w, |
| 304 | int h, const int16_t *x_filters, |
| 305 | const int x0_qn, const int x_step_qn, |
| 306 | int bd); |
| 307 | |
| 308 | // Test parameter list: |
| 309 | // <tst_fun_, bd_> |
| 310 | typedef tuple<HighBDConvolveHorizRsFunc, int> HighBDParams; |
| 311 | |
| 312 | class HighBDConvolveHorizRSTest |
| 313 | : public ConvolveHorizRSTestBase<uint16_t>, |
| 314 | public ::testing::WithParamInterface<HighBDParams> { |
| 315 | public: |
| 316 | virtual ~HighBDConvolveHorizRSTest() {} |
| 317 | |
| 318 | void SetUp() { |
| 319 | tst_fun_ = GET_PARAM(0); |
| 320 | const int bd = GET_PARAM(1); |
| 321 | SetBitDepth(bd); |
| 322 | } |
| 323 | |
| 324 | void RunOne(bool ref) { |
| 325 | const uint16_t *src = image_->GetSrcData(ref, false); |
| 326 | uint16_t *dst = image_->GetDstData(ref, false); |
| 327 | const int src_stride = image_->src_stride(); |
| 328 | const int dst_stride = image_->dst_stride(); |
| 329 | const int width_src = image_->src_width(); |
| 330 | const int width_dst = image_->dst_width(); |
| 331 | const int height = image_->height(); |
| 332 | const int x0_qn = image_->x0(); |
| 333 | |
| 334 | const int32_t x_step_qn = |
| 335 | av1_get_upscale_convolve_step(width_src, width_dst); |
| 336 | |
| 337 | if (ref) { |
| 338 | av1_highbd_convolve_horiz_rs_c( |
| 339 | src, src_stride, dst, dst_stride, width_dst, height, |
| 340 | &av1_resize_filter_normative[0][0], x0_qn, x_step_qn, bd_); |
| 341 | } else { |
| 342 | tst_fun_(src, src_stride, dst, dst_stride, width_dst, height, |
| 343 | &av1_resize_filter_normative[0][0], x0_qn, x_step_qn, bd_); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | private: |
| 348 | HighBDConvolveHorizRsFunc tst_fun_; |
| 349 | }; |
| 350 | |
| 351 | const int kBDs[] = { 8, 10, 12 }; |
| 352 | |
| 353 | TEST_P(HighBDConvolveHorizRSTest, Correctness) { CorrectnessTest(); } |
| 354 | TEST_P(HighBDConvolveHorizRSTest, DISABLED_Speed) { SpeedTest(); } |
| 355 | |
| 356 | INSTANTIATE_TEST_CASE_P( |
| 357 | SSE4_1, HighBDConvolveHorizRSTest, |
| 358 | ::testing::Combine(::testing::Values(av1_highbd_convolve_horiz_rs_sse4_1), |
| 359 | ::testing::ValuesIn(kBDs))); |
| 360 | |
| 361 | } // namespace |