avg_test.cc 11.7 KB
Newer Older
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4 5 6 7 8 9 10 11
 * 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.
*/

12 13 14

#include <limits.h>
#include <stdio.h>
15 16 17
#include <string.h>

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

Adrian Grange's avatar
Adrian Grange committed
19 20
#include "./aom_config.h"
#include "./aom_dsp_rtcd.h"
21 22 23 24 25

#include "test/acm_random.h"
#include "test/clear_system_state.h"
#include "test/register_state_check.h"
#include "test/util.h"
Adrian Grange's avatar
Adrian Grange committed
26
#include "aom_mem/aom_mem.h"
27

Yaowu Xu's avatar
Yaowu Xu committed
28
using libaom_test::ACMRandom;
29 30 31 32 33 34 35

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

  static void SetUpTestCase() {
clang-format's avatar
clang-format committed
36
    source_data_ = reinterpret_cast<uint8_t *>(
Adrian Grange's avatar
Adrian Grange committed
37
        aom_memalign(kDataAlignment, kDataBlockSize));
38 39 40
  }

  static void TearDownTestCase() {
Adrian Grange's avatar
Adrian Grange committed
41
    aom_free(source_data_);
42 43 44
    source_data_ = NULL;
  }

Yaowu Xu's avatar
Yaowu Xu committed
45
  virtual void TearDown() { libaom_test::ClearSystemState(); }
46 47 48 49 50 51 52 53 54 55 56 57

 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
clang-format's avatar
clang-format committed
58
  unsigned int ReferenceAverage8x8(const uint8_t *source, int pitch) {
59 60
    unsigned int average = 0;
    for (int h = 0; h < 8; ++h)
clang-format's avatar
clang-format committed
61
      for (int w = 0; w < 8; ++w) average += source[h * source_stride_ + w];
62 63 64
    return ((average + 32) >> 6);
  }

clang-format's avatar
clang-format committed
65
  unsigned int ReferenceAverage4x4(const uint8_t *source, int pitch) {
66 67
    unsigned int average = 0;
    for (int h = 0; h < 4; ++h)
clang-format's avatar
clang-format committed
68
      for (int w = 0; w < 4; ++w) average += source[h * source_stride_ + w];
69 70 71
    return ((average + 8) >> 4);
  }

72 73
  void FillConstant(uint8_t fill_constant) {
    for (int i = 0; i < width_ * height_; ++i) {
clang-format's avatar
clang-format committed
74
      source_data_[i] = fill_constant;
75 76 77 78 79
    }
  }

  void FillRandom() {
    for (int i = 0; i < width_ * height_; ++i) {
clang-format's avatar
clang-format committed
80
      source_data_[i] = rnd_.Rand8();
81 82 83 84
    }
  }

  int width_, height_;
clang-format's avatar
clang-format committed
85
  static uint8_t *source_data_;
86 87 88 89
  int source_stride_;

  ACMRandom rnd_;
};
clang-format's avatar
clang-format committed
90
typedef unsigned int (*AverageFunction)(const uint8_t *s, int pitch);
91

92
typedef std::tr1::tuple<int, int, int, int, AverageFunction> AvgFunc;
93

clang-format's avatar
clang-format committed
94 95
class AverageTest : public AverageTestBase,
                    public ::testing::WithParamInterface<AvgFunc> {
96 97 98 99 100
 public:
  AverageTest() : AverageTestBase(GET_PARAM(0), GET_PARAM(1)) {}

 protected:
  void CheckAverages() {
101 102
    unsigned int expected = 0;
    if (GET_PARAM(3) == 8) {
clang-format's avatar
clang-format committed
103 104 105 106 107
      expected =
          ReferenceAverage8x8(source_data_ + GET_PARAM(2), source_stride_);
    } else if (GET_PARAM(3) == 4) {
      expected =
          ReferenceAverage4x4(source_data_ + GET_PARAM(2), source_stride_);
108
    }
109

clang-format's avatar
clang-format committed
110 111 112 113
    ASM_REGISTER_STATE_CHECK(
        GET_PARAM(4)(source_data_ + GET_PARAM(2), source_stride_));
    unsigned int actual =
        GET_PARAM(4)(source_data_ + GET_PARAM(2), source_stride_);
114 115 116 117 118

    EXPECT_EQ(expected, actual);
  }
};

119 120 121 122 123
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;

clang-format's avatar
clang-format committed
124 125
class IntProRowTest : public AverageTestBase,
                      public ::testing::WithParamInterface<IntProRowParam> {
126 127
 public:
  IntProRowTest()
clang-format's avatar
clang-format committed
128
      : AverageTestBase(16, GET_PARAM(0)), hbuf_asm_(NULL), hbuf_c_(NULL) {
129 130 131 132 133 134
    asm_func_ = GET_PARAM(1);
    c_func_ = GET_PARAM(2);
  }

 protected:
  virtual void SetUp() {
clang-format's avatar
clang-format committed
135
    hbuf_asm_ = reinterpret_cast<int16_t *>(
Adrian Grange's avatar
Adrian Grange committed
136
        aom_memalign(kDataAlignment, sizeof(*hbuf_asm_) * 16));
clang-format's avatar
clang-format committed
137
    hbuf_c_ = reinterpret_cast<int16_t *>(
Adrian Grange's avatar
Adrian Grange committed
138
        aom_memalign(kDataAlignment, sizeof(*hbuf_c_) * 16));
139 140 141
  }

  virtual void TearDown() {
Adrian Grange's avatar
Adrian Grange committed
142
    aom_free(hbuf_c_);
143
    hbuf_c_ = NULL;
Adrian Grange's avatar
Adrian Grange committed
144
    aom_free(hbuf_asm_);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    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;

clang-format's avatar
clang-format committed
166 167
class IntProColTest : public AverageTestBase,
                      public ::testing::WithParamInterface<IntProColParam> {
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
 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_;
};

James Zern's avatar
James Zern committed
188 189 190
typedef int (*SatdFunc)(const int16_t *coeffs, int length);
typedef std::tr1::tuple<int, SatdFunc> SatdTestParam;

clang-format's avatar
clang-format committed
191 192
class SatdTest : public ::testing::Test,
                 public ::testing::WithParamInterface<SatdTestParam> {
James Zern's avatar
James Zern committed
193 194 195 196 197
 protected:
  virtual void SetUp() {
    satd_size_ = GET_PARAM(0);
    satd_func_ = GET_PARAM(1);
    rnd_.Reset(ACMRandom::DeterministicSeed());
clang-format's avatar
clang-format committed
198
    src_ = reinterpret_cast<int16_t *>(
Adrian Grange's avatar
Adrian Grange committed
199
        aom_memalign(16, sizeof(*src_) * satd_size_));
James Zern's avatar
James Zern committed
200 201 202 203
    ASSERT_TRUE(src_ != NULL);
  }

  virtual void TearDown() {
Yaowu Xu's avatar
Yaowu Xu committed
204
    libaom_test::ClearSystemState();
Adrian Grange's avatar
Adrian Grange committed
205
    aom_free(src_);
James Zern's avatar
James Zern committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  }

  void FillConstant(const int16_t val) {
    for (int i = 0; i < satd_size_; ++i) src_[i] = val;
  }

  void FillRandom() {
    for (int i = 0; i < satd_size_; ++i) src_[i] = rnd_.Rand16();
  }

  void Check(const int expected) {
    int total;
    ASM_REGISTER_STATE_CHECK(total = satd_func_(src_, satd_size_));
    EXPECT_EQ(expected, total);
  }

  int satd_size_;

 private:
  int16_t *src_;
  SatdFunc satd_func_;
  ACMRandom rnd_;
};
229

clang-format's avatar
clang-format committed
230
uint8_t *AverageTestBase::source_data_ = NULL;
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

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();
  }
}

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
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();
}

James Zern's avatar
James Zern committed
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
TEST_P(SatdTest, MinValue) {
  const int kMin = -32640;
  const int expected = -kMin * satd_size_;
  FillConstant(kMin);
  Check(expected);
}

TEST_P(SatdTest, MaxValue) {
  const int kMax = 32640;
  const int expected = kMax * satd_size_;
  FillConstant(kMax);
  Check(expected);
}

TEST_P(SatdTest, Random) {
  int expected;
  switch (satd_size_) {
    case 16: expected = 205298; break;
    case 64: expected = 1113950; break;
    case 256: expected = 4268415; break;
    case 1024: expected = 16954082; break;
    default:
      FAIL() << "Invalid satd size (" << satd_size_
             << ") valid: 16/64/256/1024";
  }
  FillRandom();
  Check(expected);
}

310 311 312 313
using std::tr1::make_tuple;

INSTANTIATE_TEST_CASE_P(
    C, AverageTest,
Adrian Grange's avatar
Adrian Grange committed
314 315
    ::testing::Values(make_tuple(16, 16, 1, 8, &aom_avg_8x8_c),
                      make_tuple(16, 16, 1, 4, &aom_avg_4x4_c)));
316

clang-format's avatar
clang-format committed
317
INSTANTIATE_TEST_CASE_P(C, SatdTest,
Adrian Grange's avatar
Adrian Grange committed
318 319 320 321
                        ::testing::Values(make_tuple(16, &aom_satd_c),
                                          make_tuple(64, &aom_satd_c),
                                          make_tuple(256, &aom_satd_c),
                                          make_tuple(1024, &aom_satd_c)));
James Zern's avatar
James Zern committed
322

323 324 325
#if HAVE_SSE2
INSTANTIATE_TEST_CASE_P(
    SSE2, AverageTest,
Adrian Grange's avatar
Adrian Grange committed
326 327 328 329 330 331
    ::testing::Values(make_tuple(16, 16, 0, 8, &aom_avg_8x8_sse2),
                      make_tuple(16, 16, 5, 8, &aom_avg_8x8_sse2),
                      make_tuple(32, 32, 15, 8, &aom_avg_8x8_sse2),
                      make_tuple(16, 16, 0, 4, &aom_avg_4x4_sse2),
                      make_tuple(16, 16, 5, 4, &aom_avg_4x4_sse2),
                      make_tuple(32, 32, 15, 4, &aom_avg_4x4_sse2)));
332

333
INSTANTIATE_TEST_CASE_P(
clang-format's avatar
clang-format committed
334
    SSE2, IntProRowTest,
Adrian Grange's avatar
Adrian Grange committed
335 336 337 338
    ::testing::Values(make_tuple(16, &aom_int_pro_row_sse2, &aom_int_pro_row_c),
                      make_tuple(32, &aom_int_pro_row_sse2, &aom_int_pro_row_c),
                      make_tuple(64, &aom_int_pro_row_sse2,
                                 &aom_int_pro_row_c)));
339 340

INSTANTIATE_TEST_CASE_P(
clang-format's avatar
clang-format committed
341
    SSE2, IntProColTest,
Adrian Grange's avatar
Adrian Grange committed
342 343 344 345
    ::testing::Values(make_tuple(16, &aom_int_pro_col_sse2, &aom_int_pro_col_c),
                      make_tuple(32, &aom_int_pro_col_sse2, &aom_int_pro_col_c),
                      make_tuple(64, &aom_int_pro_col_sse2,
                                 &aom_int_pro_col_c)));
clang-format's avatar
clang-format committed
346 347

INSTANTIATE_TEST_CASE_P(SSE2, SatdTest,
Adrian Grange's avatar
Adrian Grange committed
348 349 350 351
                        ::testing::Values(make_tuple(16, &aom_satd_sse2),
                                          make_tuple(64, &aom_satd_sse2),
                                          make_tuple(256, &aom_satd_sse2),
                                          make_tuple(1024, &aom_satd_sse2)));
352 353
#endif

354 355 356
#if HAVE_NEON
INSTANTIATE_TEST_CASE_P(
    NEON, AverageTest,
Adrian Grange's avatar
Adrian Grange committed
357 358 359 360 361 362
    ::testing::Values(make_tuple(16, 16, 0, 8, &aom_avg_8x8_neon),
                      make_tuple(16, 16, 5, 8, &aom_avg_8x8_neon),
                      make_tuple(32, 32, 15, 8, &aom_avg_8x8_neon),
                      make_tuple(16, 16, 0, 4, &aom_avg_4x4_neon),
                      make_tuple(16, 16, 5, 4, &aom_avg_4x4_neon),
                      make_tuple(32, 32, 15, 4, &aom_avg_4x4_neon)));
Frank Galligan's avatar
Frank Galligan committed
363 364

INSTANTIATE_TEST_CASE_P(
clang-format's avatar
clang-format committed
365
    NEON, IntProRowTest,
Adrian Grange's avatar
Adrian Grange committed
366 367 368 369
    ::testing::Values(make_tuple(16, &aom_int_pro_row_neon, &aom_int_pro_row_c),
                      make_tuple(32, &aom_int_pro_row_neon, &aom_int_pro_row_c),
                      make_tuple(64, &aom_int_pro_row_neon,
                                 &aom_int_pro_row_c)));
James Zern's avatar
James Zern committed
370 371

INSTANTIATE_TEST_CASE_P(
clang-format's avatar
clang-format committed
372
    NEON, IntProColTest,
Adrian Grange's avatar
Adrian Grange committed
373 374 375 376
    ::testing::Values(make_tuple(16, &aom_int_pro_col_neon, &aom_int_pro_col_c),
                      make_tuple(32, &aom_int_pro_col_neon, &aom_int_pro_col_c),
                      make_tuple(64, &aom_int_pro_col_neon,
                                 &aom_int_pro_col_c)));
clang-format's avatar
clang-format committed
377 378

INSTANTIATE_TEST_CASE_P(NEON, SatdTest,
Adrian Grange's avatar
Adrian Grange committed
379 380 381 382
                        ::testing::Values(make_tuple(16, &aom_satd_neon),
                                          make_tuple(64, &aom_satd_neon),
                                          make_tuple(256, &aom_satd_neon),
                                          make_tuple(1024, &aom_satd_neon)));
383 384
#endif

385 386 387
#if HAVE_MSA
INSTANTIATE_TEST_CASE_P(
    MSA, AverageTest,
Adrian Grange's avatar
Adrian Grange committed
388 389 390 391 392 393
    ::testing::Values(make_tuple(16, 16, 0, 8, &aom_avg_8x8_msa),
                      make_tuple(16, 16, 5, 8, &aom_avg_8x8_msa),
                      make_tuple(32, 32, 15, 8, &aom_avg_8x8_msa),
                      make_tuple(16, 16, 0, 4, &aom_avg_4x4_msa),
                      make_tuple(16, 16, 5, 4, &aom_avg_4x4_msa),
                      make_tuple(32, 32, 15, 4, &aom_avg_4x4_msa)));
394 395
#endif

396
}  // namespace