aboutsummaryrefslogtreecommitdiff
path: root/test/error_block_test.cc
blob: a6b442fbb048544b6fe1d5dac5efb1d0de8f9f33 (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
293
294
295
296
297
298
299
300
301
302
/*
 * 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 <cmath>
#include <cstdlib>
#include <string>
#include <tuple>

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

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

#include "test/acm_random.h"
#include "test/register_state_check.h"
#include "test/util.h"
#include "av1/common/entropy.h"
#include "aom/aom_codec.h"
#include "aom/aom_integer.h"

using libaom_test::ACMRandom;

namespace {
const int kNumIterations = 1000;

using ErrorBlockFunc = int64_t (*)(const tran_low_t *coeff,
                                   const tran_low_t *dqcoeff,
                                   intptr_t block_size, int64_t *ssz, int bps);

using ErrorBlockFunc8Bits = int64_t (*)(const tran_low_t *coeff,
                                        const tran_low_t *dqcoeff,
                                        intptr_t block_size, int64_t *ssz);

using ErrorBlockLpFunc = int64_t (*)(const int16_t *coeff,
                                     const int16_t *dqcoeff,
                                     intptr_t block_size);

using ErrorBlockParam =
    std::tuple<ErrorBlockFunc, ErrorBlockFunc, aom_bit_depth_t>;

template <ErrorBlockFunc8Bits fn>
int64_t BlockError8BitWrapper(const tran_low_t *coeff,
                              const tran_low_t *dqcoeff, intptr_t block_size,
                              int64_t *ssz, int bps) {
  EXPECT_EQ(bps, 8);
  return fn(coeff, dqcoeff, block_size, ssz);
}

template <ErrorBlockLpFunc fn>
int64_t BlockErrorLpWrapper(const tran_low_t *coeff, const tran_low_t *dqcoeff,
                            intptr_t block_size, int64_t *ssz, int bps) {
  EXPECT_EQ(bps, 8);
  *ssz = -1;
  return fn(reinterpret_cast<const int16_t *>(coeff),
            reinterpret_cast<const int16_t *>(dqcoeff), block_size);
}

class ErrorBlockTest : public ::testing::TestWithParam<ErrorBlockParam> {
 public:
  virtual ~ErrorBlockTest() {}
  virtual void SetUp() {
    error_block_op_ = GET_PARAM(0);
    ref_error_block_op_ = GET_PARAM(1);
    bit_depth_ = GET_PARAM(2);
  }

  virtual void TearDown() {}

 protected:
  aom_bit_depth_t bit_depth_;
  ErrorBlockFunc error_block_op_;
  ErrorBlockFunc ref_error_block_op_;
};
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ErrorBlockTest);

TEST_P(ErrorBlockTest, OperationCheck) {
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  DECLARE_ALIGNED(16, tran_low_t, coeff[4096]);
  DECLARE_ALIGNED(16, tran_low_t, dqcoeff[4096]);
  int err_count_total = 0;
  int first_failure = -1;
  intptr_t block_size;
  int64_t ssz;
  int64_t ret;
  int64_t ref_ssz;
  int64_t ref_ret;
  const int msb = bit_depth_ + 8 - 1;
  for (int i = 0; i < kNumIterations; ++i) {
    int err_count = 0;
    block_size = 16 << (i % 9);  // All block sizes from 4x4, 8x4 ..64x64
    for (int j = 0; j < block_size; j++) {
      // coeff and dqcoeff will always have at least the same sign, and this
      // can be used for optimization, so generate test input precisely.
      if (rnd(2)) {
        // Positive number
        coeff[j] = rnd(1 << msb);
        dqcoeff[j] = rnd(1 << msb);
      } else {
        // Negative number
        coeff[j] = -rnd(1 << msb);
        dqcoeff[j] = -rnd(1 << msb);
      }
    }
    ref_ret =
        ref_error_block_op_(coeff, dqcoeff, block_size, &ref_ssz, bit_depth_);
    API_REGISTER_STATE_CHECK(
        ret = error_block_op_(coeff, dqcoeff, block_size, &ssz, bit_depth_));
    err_count += (ref_ret != ret) | (ref_ssz != ssz);
    if (err_count && !err_count_total) {
      first_failure = i;
    }
    err_count_total += err_count;
  }
  EXPECT_EQ(0, err_count_total)
      << "Error: Error Block Test, C output doesn't match optimized output. "
      << "First failed at test case " << first_failure;
}

TEST_P(ErrorBlockTest, ExtremeValues) {
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  DECLARE_ALIGNED(16, tran_low_t, coeff[4096]);
  DECLARE_ALIGNED(16, tran_low_t, dqcoeff[4096]);
  int err_count_total = 0;
  int first_failure = -1;
  intptr_t block_size;
  int64_t ssz;
  int64_t ret;
  int64_t ref_ssz;
  int64_t ref_ret;
  const int msb = bit_depth_ + 8 - 1;
  int max_val = ((1 << msb) - 1);
  for (int i = 0; i < kNumIterations; ++i) {
    int err_count = 0;
    int k = (i / 9) % 9;

    // Change the maximum coeff value, to test different bit boundaries
    if (k == 8 && (i % 9) == 0) {
      max_val >>= 1;
    }
    block_size = 16 << (i % 9);  // All block sizes from 4x4, 8x4 ..64x64
    for (int j = 0; j < block_size; j++) {
      if (k < 4) {
        // Test at positive maximum values
        coeff[j] = k % 2 ? max_val : 0;
        dqcoeff[j] = (k >> 1) % 2 ? max_val : 0;
      } else if (k < 8) {
        // Test at negative maximum values
        coeff[j] = k % 2 ? -max_val : 0;
        dqcoeff[j] = (k >> 1) % 2 ? -max_val : 0;
      } else {
        if (rnd(2)) {
          // Positive number
          coeff[j] = rnd(1 << 14);
          dqcoeff[j] = rnd(1 << 14);
        } else {
          // Negative number
          coeff[j] = -rnd(1 << 14);
          dqcoeff[j] = -rnd(1 << 14);
        }
      }
    }
    ref_ret =
        ref_error_block_op_(coeff, dqcoeff, block_size, &ref_ssz, bit_depth_);
    API_REGISTER_STATE_CHECK(
        ret = error_block_op_(coeff, dqcoeff, block_size, &ssz, bit_depth_));
    err_count += (ref_ret != ret) | (ref_ssz != ssz);
    if (err_count && !err_count_total) {
      first_failure = i;
    }
    err_count_total += err_count;
  }
  EXPECT_EQ(0, err_count_total)
      << "Error: Error Block Test, C output doesn't match optimized output. "
      << "First failed at test case " << first_failure;
}

TEST_P(ErrorBlockTest, DISABLED_Speed) {
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  DECLARE_ALIGNED(16, tran_low_t, coeff[4096]);
  DECLARE_ALIGNED(16, tran_low_t, dqcoeff[4096]);
  intptr_t block_size;
  int64_t ssz;
  int num_iters = 100000;
  int64_t ref_ssz;
  int k;
  const int msb = bit_depth_ + 8 - 1;
  for (int i = 0; i < 9; ++i) {
    block_size = 16 << (i % 9);  // All block sizes from 4x4, 8x4 ..64x64
    for (k = 0; k < 9; k++) {
      for (int j = 0; j < block_size; j++) {
        if (k < 5) {
          if (rnd(2)) {
            // Positive number
            coeff[j] = rnd(1 << msb);
            dqcoeff[j] = rnd(1 << msb);
          } else {
            // Negative number
            coeff[j] = -rnd(1 << msb);
            dqcoeff[j] = -rnd(1 << msb);
          }
        } else {
          if (rnd(2)) {
            // Positive number
            coeff[j] = rnd(1 << 14);
            dqcoeff[j] = rnd(1 << 14);
          } else {
            // Negative number
            coeff[j] = -rnd(1 << 14);
            dqcoeff[j] = -rnd(1 << 14);
          }
        }
      }
      aom_usec_timer ref_timer, test_timer;

      aom_usec_timer_start(&ref_timer);
      for (int i = 0; i < num_iters; ++i) {
        ref_error_block_op_(coeff, dqcoeff, block_size, &ref_ssz, bit_depth_);
      }
      aom_usec_timer_mark(&ref_timer);
      const int elapsed_time_c =
          static_cast<int>(aom_usec_timer_elapsed(&ref_timer));

      aom_usec_timer_start(&test_timer);
      for (int i = 0; i < num_iters; ++i) {
        error_block_op_(coeff, dqcoeff, block_size, &ssz, bit_depth_);
      }
      aom_usec_timer_mark(&test_timer);

      const int elapsed_time_simd =
          static_cast<int>(aom_usec_timer_elapsed(&test_timer));

      printf(
          " c_time=%d \t simd_time=%d \t "
          "gain=%d \n",
          elapsed_time_c, elapsed_time_simd,
          (elapsed_time_c / elapsed_time_simd));
    }
  }
}

using std::make_tuple;

#if (HAVE_SSE2)
const ErrorBlockParam kErrorBlockTestParamsSse2[] = {
#if CONFIG_AV1_HIGHBITDEPTH
  make_tuple(&av1_highbd_block_error_sse2, &av1_highbd_block_error_c,
             AOM_BITS_10),
  make_tuple(&av1_highbd_block_error_sse2, &av1_highbd_block_error_c,
             AOM_BITS_12),
  make_tuple(&av1_highbd_block_error_sse2, &av1_highbd_block_error_c,
             AOM_BITS_8),
#endif
  make_tuple(&BlockError8BitWrapper<av1_block_error_sse2>,
             &BlockError8BitWrapper<av1_block_error_c>, AOM_BITS_8),
  make_tuple(&BlockErrorLpWrapper<av1_block_error_lp_sse2>,
             &BlockErrorLpWrapper<av1_block_error_lp_c>, AOM_BITS_8)
};

INSTANTIATE_TEST_SUITE_P(SSE2, ErrorBlockTest,
                         ::testing::ValuesIn(kErrorBlockTestParamsSse2));
#endif  // HAVE_SSE2

#if (HAVE_AVX2)
const ErrorBlockParam kErrorBlockTestParamsAvx2[] = {
#if CONFIG_AV1_HIGHBITDEPTH
  make_tuple(&av1_highbd_block_error_avx2, &av1_highbd_block_error_c,
             AOM_BITS_10),
  make_tuple(&av1_highbd_block_error_avx2, &av1_highbd_block_error_c,
             AOM_BITS_12),
  make_tuple(&av1_highbd_block_error_avx2, &av1_highbd_block_error_c,
             AOM_BITS_8),
#endif
  make_tuple(&BlockError8BitWrapper<av1_block_error_avx2>,
             &BlockError8BitWrapper<av1_block_error_c>, AOM_BITS_8),
  make_tuple(&BlockErrorLpWrapper<av1_block_error_lp_avx2>,
             &BlockErrorLpWrapper<av1_block_error_lp_c>, AOM_BITS_8)
};

INSTANTIATE_TEST_SUITE_P(AVX2, ErrorBlockTest,
                         ::testing::ValuesIn(kErrorBlockTestParamsAvx2));
#endif  // HAVE_AVX2

#if (HAVE_NEON)
const ErrorBlockParam kErrorBlockTestParamsNeon[] = {
  make_tuple(&BlockError8BitWrapper<av1_block_error_neon>,
             &BlockError8BitWrapper<av1_block_error_c>, AOM_BITS_8),
  make_tuple(&BlockErrorLpWrapper<av1_block_error_lp_neon>,
             &BlockErrorLpWrapper<av1_block_error_lp_c>, AOM_BITS_8)
};

INSTANTIATE_TEST_SUITE_P(NEON, ErrorBlockTest,
                         ::testing::ValuesIn(kErrorBlockTestParamsNeon));
#endif  // HAVE_NEON
}  // namespace