summaryrefslogtreecommitdiffstats
path: root/third_party/aom/test/subtract_test.cc
blob: e591e6543d654c93ebb78503c4ec24ed37adf7e4 (plain)
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
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
/*
 * Copyright (c) 2016, 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 <cstdint>
#include <tuple>

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

#include "config/aom_config.h"
#include "config/aom_dsp_rtcd.h"

#include "test/acm_random.h"
#include "test/register_state_check.h"
#include "test/util.h"
#include "av1/common/blockd.h"
#include "aom_mem/aom_mem.h"
#include "aom_ports/mem.h"

typedef void (*SubtractFunc)(int rows, int cols, int16_t *diff_ptr,
                             ptrdiff_t diff_stride, const uint8_t *src_ptr,
                             ptrdiff_t src_stride, const uint8_t *pred_ptr,
                             ptrdiff_t pred_stride);

namespace {

using std::get;
using std::make_tuple;
using std::tuple;

using libaom_test::ACMRandom;

// <BLOCK_SIZE, optimized subtract func, reference subtract func>
using Params = tuple<BLOCK_SIZE, SubtractFunc, SubtractFunc>;

class AV1SubtractBlockTestBase : public ::testing::Test {
 public:
  AV1SubtractBlockTestBase(BLOCK_SIZE bs, int bit_depth, SubtractFunc func,
                           SubtractFunc ref_func) {
    block_width_ = block_size_wide[bs];
    block_height_ = block_size_high[bs];
    func_ = func;
    ref_func_ = ref_func;
    if (bit_depth == -1) {
      hbd_ = false;
      bit_depth_ = AOM_BITS_8;
    } else {
      hbd_ = true;
      bit_depth_ = static_cast<aom_bit_depth_t>(bit_depth);
    }
  }

  void SetUp() override {
    rnd_.Reset(ACMRandom::DeterministicSeed());

    const size_t max_width = 128;
    const size_t max_block_size = max_width * max_width;
    if (hbd_) {
      src_ = CONVERT_TO_BYTEPTR(reinterpret_cast<uint16_t *>(
          aom_memalign(16, max_block_size * sizeof(uint16_t))));
      ASSERT_NE(src_, nullptr);
      pred_ = CONVERT_TO_BYTEPTR(reinterpret_cast<uint16_t *>(
          aom_memalign(16, max_block_size * sizeof(uint16_t))));
      ASSERT_NE(pred_, nullptr);
    } else {
      src_ = reinterpret_cast<uint8_t *>(
          aom_memalign(16, max_block_size * sizeof(uint8_t)));
      ASSERT_NE(src_, nullptr);
      pred_ = reinterpret_cast<uint8_t *>(
          aom_memalign(16, max_block_size * sizeof(uint8_t)));
      ASSERT_NE(pred_, nullptr);
    }
    diff_ = reinterpret_cast<int16_t *>(
        aom_memalign(32, max_block_size * sizeof(int16_t)));
    ASSERT_NE(diff_, nullptr);
  }

  void TearDown() override {
    if (hbd_) {
      aom_free(CONVERT_TO_SHORTPTR(src_));
      aom_free(CONVERT_TO_SHORTPTR(pred_));
    } else {
      aom_free(src_);
      aom_free(pred_);
    }
    aom_free(diff_);
  }

 protected:
  void CheckResult();
  void RunForSpeed();

 private:
  void FillInputs();

  ACMRandom rnd_;
  int block_height_;
  int block_width_;
  bool hbd_;
  aom_bit_depth_t bit_depth_;
  SubtractFunc func_;
  SubtractFunc ref_func_;
  uint8_t *src_;
  uint8_t *pred_;
  int16_t *diff_;
};

void AV1SubtractBlockTestBase::FillInputs() {
  const size_t max_width = 128;
  const int max_block_size = max_width * max_width;
  if (hbd_) {
    const int mask = (1 << bit_depth_) - 1;
    for (int i = 0; i < max_block_size; ++i) {
      CONVERT_TO_SHORTPTR(src_)[i] = rnd_.Rand16() & mask;
      CONVERT_TO_SHORTPTR(pred_)[i] = rnd_.Rand16() & mask;
    }
  } else {
    if (src_ == nullptr) {
      std::cerr << "gadfg" << std::endl;
    }
    for (int i = 0; i < max_block_size; ++i) {
      src_[i] = rnd_.Rand8();
      pred_[i] = rnd_.Rand8();
    }
  }
}

void AV1SubtractBlockTestBase::CheckResult() {
  const int test_num = 100;
  int i;

  for (i = 0; i < test_num; ++i) {
    FillInputs();

    func_(block_height_, block_width_, diff_, block_width_, src_, block_width_,
          pred_, block_width_);

    if (hbd_)
      for (int r = 0; r < block_height_; ++r) {
        for (int c = 0; c < block_width_; ++c) {
          EXPECT_EQ(diff_[r * block_width_ + c],
                    (CONVERT_TO_SHORTPTR(src_)[r * block_width_ + c] -
                     CONVERT_TO_SHORTPTR(pred_)[r * block_width_ + c]))
              << "r = " << r << ", c = " << c << ", test: " << i;
        }
      }
    else {
      for (int r = 0; r < block_height_; ++r) {
        for (int c = 0; c < block_width_; ++c) {
          EXPECT_EQ(diff_[r * block_width_ + c],
                    src_[r * block_width_ + c] - pred_[r * block_width_ + c])
              << "r = " << r << ", c = " << c << ", test: " << i;
        }
      }
    }
  }
}

void AV1SubtractBlockTestBase::RunForSpeed() {
  const int test_num = 200000;
  int i;

  if (ref_func_ == func_) GTEST_SKIP();

  FillInputs();

  aom_usec_timer ref_timer;
  aom_usec_timer_start(&ref_timer);
  for (i = 0; i < test_num; ++i) {
    ref_func_(block_height_, block_width_, diff_, block_width_, src_,
              block_width_, pred_, block_width_);
  }
  aom_usec_timer_mark(&ref_timer);
  const int64_t ref_elapsed_time = aom_usec_timer_elapsed(&ref_timer);

  FillInputs();

  aom_usec_timer timer;
  aom_usec_timer_start(&timer);
  for (i = 0; i < test_num; ++i) {
    func_(block_height_, block_width_, diff_, block_width_, src_, block_width_,
          pred_, block_width_);
  }
  aom_usec_timer_mark(&timer);
  const int64_t elapsed_time = aom_usec_timer_elapsed(&timer);

  printf(
      "[%dx%d]: "
      "ref_time=%6" PRId64 " \t simd_time=%6" PRId64
      " \t "
      "gain=%f \n",
      block_width_, block_height_, ref_elapsed_time, elapsed_time,
      static_cast<double>(ref_elapsed_time) /
          static_cast<double>(elapsed_time));
}

class AV1SubtractBlockTest : public ::testing::WithParamInterface<Params>,
                             public AV1SubtractBlockTestBase {
 public:
  AV1SubtractBlockTest()
      : AV1SubtractBlockTestBase(GET_PARAM(0), -1, GET_PARAM(1), GET_PARAM(2)) {
  }
};
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AV1SubtractBlockTest);

TEST_P(AV1SubtractBlockTest, CheckResult) { CheckResult(); }
TEST_P(AV1SubtractBlockTest, DISABLED_Speed) { RunForSpeed(); }

const BLOCK_SIZE kValidBlockSize[] = { BLOCK_4X4,    BLOCK_4X8,    BLOCK_8X4,
                                       BLOCK_8X8,    BLOCK_8X16,   BLOCK_16X8,
                                       BLOCK_16X16,  BLOCK_16X32,  BLOCK_32X16,
                                       BLOCK_32X32,  BLOCK_32X64,  BLOCK_64X32,
                                       BLOCK_64X64,  BLOCK_64X128, BLOCK_128X64,
                                       BLOCK_128X128 };

INSTANTIATE_TEST_SUITE_P(
    C, AV1SubtractBlockTest,
    ::testing::Combine(::testing::ValuesIn(kValidBlockSize),
                       ::testing::Values(&aom_subtract_block_c),
                       ::testing::Values(&aom_subtract_block_c)));

#if HAVE_SSE2
INSTANTIATE_TEST_SUITE_P(
    SSE2, AV1SubtractBlockTest,
    ::testing::Combine(::testing::ValuesIn(kValidBlockSize),
                       ::testing::Values(&aom_subtract_block_sse2),
                       ::testing::Values(&aom_subtract_block_c)));
#endif
#if HAVE_AVX2
INSTANTIATE_TEST_SUITE_P(
    AVX2, AV1SubtractBlockTest,
    ::testing::Combine(::testing::ValuesIn(kValidBlockSize),
                       ::testing::Values(&aom_subtract_block_avx2),
                       ::testing::Values(&aom_subtract_block_c)));

#endif
#if HAVE_NEON
INSTANTIATE_TEST_SUITE_P(
    NEON, AV1SubtractBlockTest,
    ::testing::Combine(::testing::ValuesIn(kValidBlockSize),
                       ::testing::Values(&aom_subtract_block_neon),
                       ::testing::Values(&aom_subtract_block_c)));

#endif

#if CONFIG_AV1_HIGHBITDEPTH

// <BLOCK_SIZE, bit_depth, optimized subtract func, reference subtract func>
using ParamsHBD = tuple<BLOCK_SIZE, int, SubtractFunc, SubtractFunc>;

class AV1HBDSubtractBlockTest : public ::testing::WithParamInterface<ParamsHBD>,
                                public AV1SubtractBlockTestBase {
 public:
  AV1HBDSubtractBlockTest()
      : AV1SubtractBlockTestBase(GET_PARAM(0), GET_PARAM(1), GET_PARAM(2),
                                 GET_PARAM(3)) {}
};
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AV1HBDSubtractBlockTest);

INSTANTIATE_TEST_SUITE_P(
    C, AV1HBDSubtractBlockTest,
    ::testing::Combine(::testing::ValuesIn(kValidBlockSize),
                       ::testing::Values(12),
                       ::testing::Values(&aom_highbd_subtract_block_c),
                       ::testing::Values(&aom_highbd_subtract_block_c)));

#if HAVE_SSE2
INSTANTIATE_TEST_SUITE_P(
    SSE2, AV1HBDSubtractBlockTest,
    ::testing::Combine(::testing::ValuesIn(kValidBlockSize),
                       ::testing::Values(12),
                       ::testing::Values(&aom_highbd_subtract_block_sse2),
                       ::testing::Values(&aom_highbd_subtract_block_c)));
#endif  // HAVE_SSE2

#if HAVE_NEON
INSTANTIATE_TEST_SUITE_P(
    NEON, AV1HBDSubtractBlockTest,
    ::testing::Combine(::testing::ValuesIn(kValidBlockSize),
                       ::testing::Values(12),
                       ::testing::Values(&aom_highbd_subtract_block_neon),
                       ::testing::Values(&aom_highbd_subtract_block_c)));
#endif
#endif  // CONFIG_AV1_HIGHBITDEPTH
}  // namespace