vp9_avg_test.cc 8.48 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 33 34 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
/*
 *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */


#include <string.h>
#include <limits.h>
#include <stdio.h>

#include "./vpx_config.h"
#if CONFIG_VP9_ENCODER
#include "./vp9_rtcd.h"
#endif
#include "vpx_mem/vpx_mem.h"

#include "test/acm_random.h"
#include "test/clear_system_state.h"
#include "test/register_state_check.h"
#include "test/util.h"
#include "third_party/googletest/src/include/gtest/gtest.h"

using libvpx_test::ACMRandom;

namespace {
class AverageTestBase : public ::testing::Test {
 public:
  AverageTestBase(int width, int height) : width_(width), height_(height) {}

  static void SetUpTestCase() {
    source_data_ = reinterpret_cast<uint8_t*>(
        vpx_memalign(kDataAlignment, kDataBlockSize));
  }

  static void TearDownTestCase() {
    vpx_free(source_data_);
    source_data_ = NULL;
  }

  virtual void TearDown() {
    libvpx_test::ClearSystemState();
  }

 protected:
  // Handle blocks up to 4 blocks 64x64 with stride up to 128
  static const int kDataAlignment = 16;
  static const int kDataBlockSize = 64 * 128;

  virtual void SetUp() {
    source_stride_ = (width_ + 31) & ~31;
    rnd_.Reset(ACMRandom::DeterministicSeed());
  }

  // Sum Pixels
60
  unsigned int ReferenceAverage8x8(const uint8_t* source, int pitch ) {
61 62 63 64 65 66 67
    unsigned int average = 0;
    for (int h = 0; h < 8; ++h)
      for (int w = 0; w < 8; ++w)
        average += source[h * source_stride_ + w];
    return ((average + 32) >> 6);
  }

68 69 70 71 72 73 74 75
  unsigned int ReferenceAverage4x4(const uint8_t* source, int pitch ) {
    unsigned int average = 0;
    for (int h = 0; h < 4; ++h)
      for (int w = 0; w < 4; ++w)
        average += source[h * source_stride_ + w];
    return ((average + 8) >> 4);
  }

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  void FillConstant(uint8_t fill_constant) {
    for (int i = 0; i < width_ * height_; ++i) {
        source_data_[i] = fill_constant;
    }
  }

  void FillRandom() {
    for (int i = 0; i < width_ * height_; ++i) {
        source_data_[i] = rnd_.Rand8();
    }
  }

  int width_, height_;
  static uint8_t* source_data_;
  int source_stride_;

  ACMRandom rnd_;
};
typedef unsigned int (*AverageFunction)(const uint8_t* s, int pitch);

96
typedef std::tr1::tuple<int, int, int, int, AverageFunction> AvgFunc;
97 98 99 100 101 102 103 104 105

class AverageTest
    : public AverageTestBase,
      public ::testing::WithParamInterface<AvgFunc>{
 public:
  AverageTest() : AverageTestBase(GET_PARAM(0), GET_PARAM(1)) {}

 protected:
  void CheckAverages() {
106 107 108 109 110 111 112 113
    unsigned int expected = 0;
    if (GET_PARAM(3) == 8) {
      expected = ReferenceAverage8x8(source_data_+ GET_PARAM(2),
                                     source_stride_);
    } else  if (GET_PARAM(3) == 4) {
      expected = ReferenceAverage4x4(source_data_+ GET_PARAM(2),
                                     source_stride_);
    }
114

115
    ASM_REGISTER_STATE_CHECK(GET_PARAM(4)(source_data_+ GET_PARAM(2),
116
                                          source_stride_));
117
    unsigned int actual = GET_PARAM(4)(source_data_+ GET_PARAM(2),
118 119 120 121 122 123
                                       source_stride_);

    EXPECT_EQ(expected, actual);
  }
};

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
typedef void (*IntProRowFunc)(int16_t hbuf[16], uint8_t const *ref,
                              const int ref_stride, const int height);

typedef std::tr1::tuple<int, IntProRowFunc, IntProRowFunc> IntProRowParam;

class IntProRowTest
    : public AverageTestBase,
      public ::testing::WithParamInterface<IntProRowParam> {
 public:
  IntProRowTest()
    : AverageTestBase(16, GET_PARAM(0)),
      hbuf_asm_(NULL),
      hbuf_c_(NULL) {
    asm_func_ = GET_PARAM(1);
    c_func_ = GET_PARAM(2);
  }

 protected:
  virtual void SetUp() {
    hbuf_asm_ = reinterpret_cast<int16_t*>(
        vpx_memalign(kDataAlignment, sizeof(*hbuf_asm_) * 16));
    hbuf_c_ = reinterpret_cast<int16_t*>(
        vpx_memalign(kDataAlignment, sizeof(*hbuf_c_) * 16));
  }

  virtual void TearDown() {
    vpx_free(hbuf_c_);
    hbuf_c_ = NULL;
    vpx_free(hbuf_asm_);
    hbuf_asm_ = NULL;
  }

  void RunComparison() {
    ASM_REGISTER_STATE_CHECK(c_func_(hbuf_c_, source_data_, 0, height_));
    ASM_REGISTER_STATE_CHECK(asm_func_(hbuf_asm_, source_data_, 0, height_));
    EXPECT_EQ(0, memcmp(hbuf_c_, hbuf_asm_, sizeof(*hbuf_c_) * 16))
        << "Output mismatch";
  }

 private:
  IntProRowFunc asm_func_;
  IntProRowFunc c_func_;
  int16_t *hbuf_asm_;
  int16_t *hbuf_c_;
};

typedef int16_t (*IntProColFunc)(uint8_t const *ref, const int width);

typedef std::tr1::tuple<int, IntProColFunc, IntProColFunc> IntProColParam;

class IntProColTest
    : public AverageTestBase,
      public ::testing::WithParamInterface<IntProColParam> {
 public:
  IntProColTest() : AverageTestBase(GET_PARAM(0), 1), sum_asm_(0), sum_c_(0) {
    asm_func_ = GET_PARAM(1);
    c_func_ = GET_PARAM(2);
  }

 protected:
  void RunComparison() {
    ASM_REGISTER_STATE_CHECK(sum_c_ = c_func_(source_data_, width_));
    ASM_REGISTER_STATE_CHECK(sum_asm_ = asm_func_(source_data_, width_));
    EXPECT_EQ(sum_c_, sum_asm_) << "Output mismatch";
  }

 private:
  IntProColFunc asm_func_;
  IntProColFunc c_func_;
  int16_t sum_asm_;
  int16_t sum_c_;
};

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

uint8_t* AverageTestBase::source_data_ = NULL;

TEST_P(AverageTest, MinValue) {
  FillConstant(0);
  CheckAverages();
}

TEST_P(AverageTest, MaxValue) {
  FillConstant(255);
  CheckAverages();
}

TEST_P(AverageTest, Random) {
  // The reference frame, but not the source frame, may be unaligned for
  // certain types of searches.
  for (int i = 0; i < 1000; i++) {
    FillRandom();
    CheckAverages();
  }
}

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
TEST_P(IntProRowTest, MinValue) {
  FillConstant(0);
  RunComparison();
}

TEST_P(IntProRowTest, MaxValue) {
  FillConstant(255);
  RunComparison();
}

TEST_P(IntProRowTest, Random) {
  FillRandom();
  RunComparison();
}

TEST_P(IntProColTest, MinValue) {
  FillConstant(0);
  RunComparison();
}

TEST_P(IntProColTest, MaxValue) {
  FillConstant(255);
  RunComparison();
}

TEST_P(IntProColTest, Random) {
  FillRandom();
  RunComparison();
}

249 250 251 252 253
using std::tr1::make_tuple;

INSTANTIATE_TEST_CASE_P(
    C, AverageTest,
    ::testing::Values(
254 255
        make_tuple(16, 16, 1, 8, &vp9_avg_8x8_c),
        make_tuple(16, 16, 1, 4, &vp9_avg_4x4_c)));
256 257 258 259 260

#if HAVE_SSE2
INSTANTIATE_TEST_CASE_P(
    SSE2, AverageTest,
    ::testing::Values(
261 262 263 264 265 266
        make_tuple(16, 16, 0, 8, &vp9_avg_8x8_sse2),
        make_tuple(16, 16, 5, 8, &vp9_avg_8x8_sse2),
        make_tuple(32, 32, 15, 8, &vp9_avg_8x8_sse2),
        make_tuple(16, 16, 0, 4, &vp9_avg_4x4_sse2),
        make_tuple(16, 16, 5, 4, &vp9_avg_4x4_sse2),
        make_tuple(32, 32, 15, 4, &vp9_avg_4x4_sse2)));
267

268 269 270 271 272 273 274 275 276 277 278
INSTANTIATE_TEST_CASE_P(
    SSE2, IntProRowTest, ::testing::Values(
        make_tuple(16, &vp9_int_pro_row_sse2, &vp9_int_pro_row_c),
        make_tuple(32, &vp9_int_pro_row_sse2, &vp9_int_pro_row_c),
        make_tuple(64, &vp9_int_pro_row_sse2, &vp9_int_pro_row_c)));

INSTANTIATE_TEST_CASE_P(
    SSE2, IntProColTest, ::testing::Values(
        make_tuple(16, &vp9_int_pro_col_sse2, &vp9_int_pro_col_c),
        make_tuple(32, &vp9_int_pro_col_sse2, &vp9_int_pro_col_c),
        make_tuple(64, &vp9_int_pro_col_sse2, &vp9_int_pro_col_c)));
279 280
#endif

281 282 283 284 285 286 287 288
#if HAVE_NEON
INSTANTIATE_TEST_CASE_P(
    NEON, AverageTest,
    ::testing::Values(
        make_tuple(16, 16, 0, 8, &vp9_avg_8x8_neon),
        make_tuple(16, 16, 5, 8, &vp9_avg_8x8_neon),
        make_tuple(32, 32, 15, 8, &vp9_avg_8x8_neon)));

Frank Galligan's avatar
Frank Galligan committed
289 290 291 292 293
INSTANTIATE_TEST_CASE_P(
    NEON, IntProRowTest, ::testing::Values(
        make_tuple(16, &vp9_int_pro_row_neon, &vp9_int_pro_row_c),
        make_tuple(32, &vp9_int_pro_row_neon, &vp9_int_pro_row_c),
        make_tuple(64, &vp9_int_pro_row_neon, &vp9_int_pro_row_c)));
Frank Galligan's avatar
Frank Galligan committed
294 295 296 297 298 299

INSTANTIATE_TEST_CASE_P(
    NEON, IntProColTest, ::testing::Values(
        make_tuple(16, &vp9_int_pro_col_neon, &vp9_int_pro_col_c),
        make_tuple(32, &vp9_int_pro_col_neon, &vp9_int_pro_col_c),
        make_tuple(64, &vp9_int_pro_col_neon, &vp9_int_pro_col_c)));
300 301
#endif

302 303 304 305 306 307 308 309 310 311 312 313
#if HAVE_MSA
INSTANTIATE_TEST_CASE_P(
    MSA, AverageTest,
    ::testing::Values(
        make_tuple(16, 16, 0, 8, &vp9_avg_8x8_msa),
        make_tuple(16, 16, 5, 8, &vp9_avg_8x8_msa),
        make_tuple(32, 32, 15, 8, &vp9_avg_8x8_msa),
        make_tuple(16, 16, 0, 4, &vp9_avg_4x4_msa),
        make_tuple(16, 16, 5, 4, &vp9_avg_4x4_msa),
        make_tuple(32, 32, 15, 4, &vp9_avg_4x4_msa)));
#endif

314
}  // namespace