av1_horz_only_frame_superres_test.cc 11.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Copyright (c) 2018, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
 */

#include <vector>

#include "third_party/googletest/src/googletest/include/gtest/gtest.h"

#include "./av1_rtcd.h"
#include "aom_ports/aom_timer.h"
#include "av1/common/convolve.h"
#include "av1/common/resize.h"
#include "test/acm_random.h"
#include "test/clear_system_state.h"
#include "test/register_state_check.h"
#include "test/util.h"

namespace {
const int kTestIters = 10;
const int kPerfIters = 1000;

const int kVPad = 32;
const int kHPad = 32;

using libaom_test::ACMRandom;
Johann's avatar
Johann committed
33 34
using std::tr1::make_tuple;
using std::tr1::tuple;
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

template <typename Pixel>
class TestImage {
 public:
  TestImage(int w_src, int h, int superres_denom, int x0, int bd)
      : w_src_(w_src), h_(h), superres_denom_(superres_denom), x0_(x0),
        bd_(bd) {
    assert(bd < 16);
    assert(bd <= 8 * static_cast<int>(sizeof(Pixel)));
    assert(9 <= superres_denom && superres_denom <= 16);
    assert(SCALE_NUMERATOR == 8);
    assert(0 <= x0_ && x0_ <= RS_SCALE_SUBPEL_MASK);

    w_dst_ = w_src_;
    av1_calculate_unscaled_superres_size(&w_dst_, NULL, superres_denom);

    src_stride_ = ALIGN_POWER_OF_TWO(w_src_ + 2 * kHPad, 4);
    dst_stride_ = ALIGN_POWER_OF_TWO(w_dst_ + 2 * kHPad, 4);

    // Allocate image data
    src_data_.resize(2 * src_block_size());
    dst_data_.resize(2 * dst_block_size());
  }

  void Initialize(ACMRandom *rnd);
  void Check() const;

  int src_stride() const { return src_stride_; }
  int dst_stride() const { return dst_stride_; }

  int src_block_size() const { return (h_ + 2 * kVPad) * src_stride(); }
  int dst_block_size() const { return (h_ + 2 * kVPad) * dst_stride(); }

  int src_width() const { return w_src_; }
  int dst_width() const { return w_dst_; }
  int height() const { return h_; }
  int x0() const { return x0_; }

  const Pixel *GetSrcData(bool ref, bool borders) const {
    const Pixel *block = &src_data_[ref ? 0 : src_block_size()];
    return borders ? block : block + kHPad + src_stride_ * kVPad;
  }

  Pixel *GetDstData(bool ref, bool borders) {
    Pixel *block = &dst_data_[ref ? 0 : dst_block_size()];
    return borders ? block : block + kHPad + dst_stride_ * kVPad;
  }

 private:
  int w_src_, w_dst_, h_, superres_denom_, x0_, bd_;
  int src_stride_, dst_stride_;

  std::vector<Pixel> src_data_;
  std::vector<Pixel> dst_data_;
};

template <typename Pixel>
void FillEdge(ACMRandom *rnd, int num_pixels, int bd, bool trash, Pixel *data) {
  if (!trash) {
    memset(data, 0, sizeof(*data) * num_pixels);
    return;
  }
  const Pixel mask = (1 << bd) - 1;
  for (int i = 0; i < num_pixels; ++i) data[i] = rnd->Rand16() & mask;
}

template <typename Pixel>
void PrepBuffers(ACMRandom *rnd, int w, int h, int stride, int bd,
                 bool trash_edges, Pixel *data) {
  assert(rnd);
  const Pixel mask = (1 << bd) - 1;

  // Fill in the first buffer with random data
  // Top border
  FillEdge(rnd, stride * kVPad, bd, trash_edges, data);
  for (int r = 0; r < h; ++r) {
    Pixel *row_data = data + (kVPad + r) * stride;
    // Left border, contents, right border
    FillEdge(rnd, kHPad, bd, trash_edges, row_data);
    for (int c = 0; c < w; ++c) row_data[kHPad + c] = rnd->Rand16() & mask;
    FillEdge(rnd, kHPad, bd, trash_edges, row_data + kHPad + w);
  }
  // Bottom border
  FillEdge(rnd, stride * kVPad, bd, trash_edges, data + stride * (kVPad + h));

  const int bpp = sizeof(*data);
  const int block_elts = stride * (h + 2 * kVPad);
  const int block_size = bpp * block_elts;

  // Now copy that to the second buffer
  memcpy(data + block_elts, data, block_size);
}

template <typename Pixel>
void TestImage<Pixel>::Initialize(ACMRandom *rnd) {
  PrepBuffers(rnd, w_src_, h_, src_stride_, bd_, false, &src_data_[0]);
  PrepBuffers(rnd, w_dst_, h_, dst_stride_, bd_, true, &dst_data_[0]);
}

template <typename Pixel>
void TestImage<Pixel>::Check() const {
  const int num_pixels = dst_block_size();
  const Pixel *ref_dst = &dst_data_[0];
  const Pixel *tst_dst = &dst_data_[num_pixels];

  // If memcmp returns 0, there's nothing to do.
  if (0 == memcmp(ref_dst, tst_dst, sizeof(*ref_dst) * num_pixels)) return;

  // Otherwise, iterate through the buffer looking for differences, *ignoring
  // the edges*
  const int stride = dst_stride_;
  for (int r = kVPad; r < h_ + kVPad; ++r) {
    for (int c = kVPad; c < w_dst_ + kHPad; ++c) {
      const int32_t ref_value = ref_dst[r * stride + c];
      const int32_t tst_value = tst_dst[r * stride + c];

      EXPECT_EQ(tst_value, ref_value)
          << "Error at row: " << (r - kVPad) << ", col: " << (c - kHPad)
          << ", superres_denom: " << superres_denom_ << ", height: " << h_
          << ", src_width: " << w_src_ << ", dst_width: " << w_dst_
          << ", x0: " << x0_;
    }
  }
}

template <typename Pixel>
class ConvolveHorizRSTestBase : public ::testing::Test {
 public:
  ConvolveHorizRSTestBase() : image_(NULL) {}
  virtual ~ConvolveHorizRSTestBase() {}
  virtual void TearDown() { libaom_test::ClearSystemState(); }

  // Implemented by subclasses (SetUp depends on the parameters passed
  // in and RunOne depends on the function to be tested. These can't
  // be templated for low/high bit depths because they have different
  // numbers of parameters)
  virtual void SetUp() = 0;
  virtual void RunOne(bool ref) = 0;

 protected:
  void SetBitDepth(int bd) { bd_ = bd; }

  void CorrectnessTest() {
    ACMRandom rnd(ACMRandom::DeterministicSeed());
    for (int i = 0; i < kTestIters; ++i) {
      for (int superres_denom = 9; superres_denom <= 16; superres_denom++) {
        // Get a random height between 512 and 767
        int height = rnd.Rand8() + 512;

        // Get a random src width between 128 and 383
        int width_src = rnd.Rand8() + 128;

        // x0 is normally calculated by get_upscale_convolve_x0 in
        // av1/common/resize.c. However, this test should work for
        // any value of x0 between 0 and RS_SCALE_SUBPEL_MASK
        // (inclusive), so we choose one at random.
        int x0 = rnd.Rand16() % (RS_SCALE_SUBPEL_MASK + 1);

        image_ =
            new TestImage<Pixel>(width_src, height, superres_denom, x0, bd_);

        Prep(&rnd);
        RunOne(true);
        RunOne(false);
        image_->Check();

        delete image_;
      }
    }
  }

  void SpeedTest() {
    // Pick some specific parameters to test
    int height = 767;
    int width_src = 129;
    int superres_denom = 13;
    int x0 = RS_SCALE_SUBPEL_MASK >> 1;

    image_ = new TestImage<Pixel>(width_src, height, superres_denom, x0, bd_);

    ACMRandom rnd(ACMRandom::DeterministicSeed());
    Prep(&rnd);

    aom_usec_timer ref_timer;
    aom_usec_timer_start(&ref_timer);
    for (int i = 0; i < kPerfIters; ++i) RunOne(true);
    aom_usec_timer_mark(&ref_timer);
    const int64_t ref_time = aom_usec_timer_elapsed(&ref_timer);

    aom_usec_timer tst_timer;
    aom_usec_timer_start(&tst_timer);
    for (int i = 0; i < kPerfIters; ++i) RunOne(false);
    aom_usec_timer_mark(&tst_timer);
    const int64_t tst_time = aom_usec_timer_elapsed(&tst_timer);

    std::cout << "[          ] C time = " << ref_time / 1000
              << " ms, SIMD time = " << tst_time / 1000 << " ms\n";

    EXPECT_GT(ref_time, tst_time)
        << "Error: CDEFSpeedTest, SIMD slower than C.\n"
        << "C time: " << ref_time << " us\n"
        << "SIMD time: " << tst_time << " us\n";
  }

  void Prep(ACMRandom *rnd) {
    assert(rnd);
    image_->Initialize(rnd);
  }

  int bd_;
  TestImage<Pixel> *image_;
};

typedef void (*LowBDConvolveHorizRsFunc)(const uint8_t *src, int src_stride,
                                         uint8_t *dst, int dst_stride, int w,
                                         int h, const int16_t *x_filters,
                                         const int x0_qn, const int x_step_qn);

// Test parameter list:
//  <tst_fun_>
typedef tuple<LowBDConvolveHorizRsFunc> LowBDParams;

class LowBDConvolveHorizRSTest
    : public ConvolveHorizRSTestBase<uint8_t>,
      public ::testing::WithParamInterface<LowBDParams> {
 public:
  virtual ~LowBDConvolveHorizRSTest() {}

  void SetUp() {
    tst_fun_ = GET_PARAM(0);
    const int bd = 8;
    SetBitDepth(bd);
  }

  void RunOne(bool ref) {
    const uint8_t *src = image_->GetSrcData(ref, false);
    uint8_t *dst = image_->GetDstData(ref, false);
    const int src_stride = image_->src_stride();
    const int dst_stride = image_->dst_stride();
    const int width_src = image_->src_width();
    const int width_dst = image_->dst_width();
    const int height = image_->height();
    const int x0_qn = image_->x0();

    const int32_t x_step_qn =
        av1_get_upscale_convolve_step(width_src, width_dst);

    if (ref) {
      av1_convolve_horiz_rs_c(src, src_stride, dst, dst_stride, width_dst,
                              height, &av1_resize_filter_normative[0][0], x0_qn,
                              x_step_qn);
    } else {
      tst_fun_(src, src_stride, dst, dst_stride, width_dst, height,
               &av1_resize_filter_normative[0][0], x0_qn, x_step_qn);
    }
  }

 private:
  LowBDConvolveHorizRsFunc tst_fun_;
};

TEST_P(LowBDConvolveHorizRSTest, Correctness) { CorrectnessTest(); }
TEST_P(LowBDConvolveHorizRSTest, DISABLED_Speed) { SpeedTest(); }

INSTANTIATE_TEST_CASE_P(SSE4_1, LowBDConvolveHorizRSTest,
                        ::testing::Values(av1_convolve_horiz_rs_sse4_1));

typedef void (*HighBDConvolveHorizRsFunc)(const uint16_t *src, int src_stride,
                                          uint16_t *dst, int dst_stride, int w,
                                          int h, const int16_t *x_filters,
                                          const int x0_qn, const int x_step_qn,
                                          int bd);

// Test parameter list:
//  <tst_fun_, bd_>
typedef tuple<HighBDConvolveHorizRsFunc, int> HighBDParams;

class HighBDConvolveHorizRSTest
    : public ConvolveHorizRSTestBase<uint16_t>,
      public ::testing::WithParamInterface<HighBDParams> {
 public:
  virtual ~HighBDConvolveHorizRSTest() {}

  void SetUp() {
    tst_fun_ = GET_PARAM(0);
    const int bd = GET_PARAM(1);
    SetBitDepth(bd);
  }

  void RunOne(bool ref) {
    const uint16_t *src = image_->GetSrcData(ref, false);
    uint16_t *dst = image_->GetDstData(ref, false);
    const int src_stride = image_->src_stride();
    const int dst_stride = image_->dst_stride();
    const int width_src = image_->src_width();
    const int width_dst = image_->dst_width();
    const int height = image_->height();
    const int x0_qn = image_->x0();

    const int32_t x_step_qn =
        av1_get_upscale_convolve_step(width_src, width_dst);

    if (ref) {
      av1_highbd_convolve_horiz_rs_c(
          src, src_stride, dst, dst_stride, width_dst, height,
          &av1_resize_filter_normative[0][0], x0_qn, x_step_qn, bd_);
    } else {
      tst_fun_(src, src_stride, dst, dst_stride, width_dst, height,
               &av1_resize_filter_normative[0][0], x0_qn, x_step_qn, bd_);
    }
  }

 private:
  HighBDConvolveHorizRsFunc tst_fun_;
};

const int kBDs[] = { 8, 10, 12 };

TEST_P(HighBDConvolveHorizRSTest, Correctness) { CorrectnessTest(); }
TEST_P(HighBDConvolveHorizRSTest, DISABLED_Speed) { SpeedTest(); }

INSTANTIATE_TEST_CASE_P(
    SSE4_1, HighBDConvolveHorizRSTest,
    ::testing::Combine(::testing::Values(av1_highbd_convolve_horiz_rs_sse4_1),
                       ::testing::ValuesIn(kBDs)));

}  // namespace