summaryrefslogtreecommitdiff
path: root/test/vp9_quantize_test.cc
blob: 862edbe801b68e09f1d0bd3a3fb644b022c74c4d (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
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
/*
 *  Copyright (c) 2014 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 <math.h>
#include <stdlib.h>
#include <string.h>

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

#include "test/acm_random.h"
#include "test/clear_system_state.h"
#include "test/register_state_check.h"
#include "test/util.h"
#include "./vpx_config.h"
#include "./vp9_rtcd.h"
#include "vp9/common/vp9_entropy.h"
#include "vpx/vpx_integer.h"

using libvpx_test::ACMRandom;

namespace {
#if CONFIG_VP9_HIGHBITDEPTH
const int number_of_iterations = 100;

typedef void (*QuantizeFunc)(const tran_low_t *coeff, intptr_t count,
                             int skip_block, const int16_t *zbin,
                             const int16_t *round, const int16_t *quant,
                             const int16_t *quant_shift,
                             tran_low_t *qcoeff, tran_low_t *dqcoeff,
                             const int16_t *dequant,
                             uint16_t *eob, const int16_t *scan,
                             const int16_t *iscan);
typedef std::tr1::tuple<QuantizeFunc, QuantizeFunc, vpx_bit_depth_t>
    QuantizeParam;

class VP9QuantizeTest : public ::testing::TestWithParam<QuantizeParam> {
 public:
  virtual ~VP9QuantizeTest() {}
  virtual void SetUp() {
    quantize_op_   = GET_PARAM(0);
    ref_quantize_op_ = GET_PARAM(1);
    bit_depth_  = GET_PARAM(2);
    mask_ = (1 << bit_depth_) - 1;
  }

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

 protected:
  vpx_bit_depth_t bit_depth_;
  int mask_;
  QuantizeFunc quantize_op_;
  QuantizeFunc ref_quantize_op_;
};

class VP9Quantize32Test : public ::testing::TestWithParam<QuantizeParam> {
 public:
  virtual ~VP9Quantize32Test() {}
  virtual void SetUp() {
    quantize_op_   = GET_PARAM(0);
    ref_quantize_op_ = GET_PARAM(1);
    bit_depth_  = GET_PARAM(2);
    mask_ = (1 << bit_depth_) - 1;
  }

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

 protected:
  vpx_bit_depth_t bit_depth_;
  int mask_;
  QuantizeFunc quantize_op_;
  QuantizeFunc ref_quantize_op_;
};

TEST_P(VP9QuantizeTest, OperationCheck) {
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, coeff_ptr, 256);
  DECLARE_ALIGNED_ARRAY(16, int16_t, zbin_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, int16_t, round_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, int16_t, quant_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, int16_t, quant_shift_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, qcoeff_ptr, 256);
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, dqcoeff_ptr, 256);
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, ref_qcoeff_ptr, 256);
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, ref_dqcoeff_ptr, 256);
  DECLARE_ALIGNED_ARRAY(16, int16_t, dequant_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, uint16_t, eob_ptr, 1);
  DECLARE_ALIGNED_ARRAY(16, uint16_t, ref_eob_ptr, 1);
  int err_count_total = 0;
  int first_failure = -1;
  for (int i = 0; i < number_of_iterations; ++i) {
    const int skip_block = i == 0;
    const TX_SIZE sz = (TX_SIZE)(i % 3);  // TX_4X4, TX_8X8 TX_16X16
    const TX_TYPE tx_type = (TX_TYPE)((i >> 2) % 3);
    const scan_order *scan_order = &vp9_scan_orders[sz][tx_type];
    const int count = (4 << sz) * (4 << sz);  // 16, 64, 256
    int err_count = 0;
    *eob_ptr = rnd.Rand16();
    *ref_eob_ptr = *eob_ptr;
    for (int j = 0; j < count; j++) {
      coeff_ptr[j] = rnd.Rand16()&mask_;
    }
    for (int j = 0; j < 2; j++) {
      zbin_ptr[j] = rnd.Rand16()&mask_;
      round_ptr[j] = rnd.Rand16();
      quant_ptr[j] = rnd.Rand16();
      quant_shift_ptr[j] = rnd.Rand16();
      dequant_ptr[j] = rnd.Rand16();
    }
    ref_quantize_op_(coeff_ptr, count, skip_block, zbin_ptr, round_ptr,
                     quant_ptr, quant_shift_ptr, ref_qcoeff_ptr,
                     ref_dqcoeff_ptr, dequant_ptr,
                     ref_eob_ptr, scan_order->scan, scan_order->iscan);
    ASM_REGISTER_STATE_CHECK(quantize_op_(coeff_ptr, count, skip_block,
                                          zbin_ptr, round_ptr, quant_ptr,
                                          quant_shift_ptr, qcoeff_ptr,
                                          dqcoeff_ptr, dequant_ptr, eob_ptr,
                                          scan_order->scan, scan_order->iscan));
    for (int j = 0; j < sz; ++j) {
      err_count += (ref_qcoeff_ptr[j]  != qcoeff_ptr[j]) |
          (ref_dqcoeff_ptr[j] != dqcoeff_ptr[j]);
    }
    err_count += (*ref_eob_ptr != *eob_ptr);
    if (err_count && !err_count_total) {
      first_failure = i;
    }
    err_count_total += err_count;
  }
  EXPECT_EQ(0, err_count_total)
      << "Error: Quantization Test, C output doesn't match SSE2 output. "
      << "First failed at test case " << first_failure;
}

TEST_P(VP9Quantize32Test, OperationCheck) {
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, coeff_ptr, 1024);
  DECLARE_ALIGNED_ARRAY(16, int16_t, zbin_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, int16_t, round_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, int16_t, quant_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, int16_t, quant_shift_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, qcoeff_ptr, 1024);
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, dqcoeff_ptr, 1024);
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, ref_qcoeff_ptr, 1024);
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, ref_dqcoeff_ptr, 1024);
  DECLARE_ALIGNED_ARRAY(16, int16_t, dequant_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, uint16_t, eob_ptr, 1);
  DECLARE_ALIGNED_ARRAY(16, uint16_t, ref_eob_ptr, 1);
  int err_count_total = 0;
  int first_failure = -1;
  for (int i = 0; i < number_of_iterations; ++i) {
    const int skip_block = i == 0;
    const TX_SIZE sz = TX_32X32;
    const TX_TYPE tx_type = (TX_TYPE)(i % 4);
    const scan_order *scan_order = &vp9_scan_orders[sz][tx_type];
    const int count = (4 << sz) * (4 << sz);  // 1024
    int err_count = 0;
    *eob_ptr = rnd.Rand16();
    *ref_eob_ptr = *eob_ptr;
    for (int j = 0; j < count; j++) {
      coeff_ptr[j] = rnd.Rand16()&mask_;
    }
    for (int j = 0; j < 2; j++) {
      zbin_ptr[j] = rnd.Rand16()&mask_;
      round_ptr[j] = rnd.Rand16();
      quant_ptr[j] = rnd.Rand16();
      quant_shift_ptr[j] = rnd.Rand16();
      dequant_ptr[j] = rnd.Rand16();
    }
    ref_quantize_op_(coeff_ptr, count, skip_block, zbin_ptr, round_ptr,
                     quant_ptr, quant_shift_ptr, ref_qcoeff_ptr,
                     ref_dqcoeff_ptr, dequant_ptr,
                     ref_eob_ptr, scan_order->scan, scan_order->iscan);
    ASM_REGISTER_STATE_CHECK(quantize_op_(coeff_ptr, count, skip_block,
                                          zbin_ptr, round_ptr, quant_ptr,
                                          quant_shift_ptr, qcoeff_ptr,
                                          dqcoeff_ptr, dequant_ptr, eob_ptr,
                                          scan_order->scan, scan_order->iscan));
    for (int j = 0; j < sz; ++j) {
      err_count += (ref_qcoeff_ptr[j]  != qcoeff_ptr[j]) |
          (ref_dqcoeff_ptr[j] != dqcoeff_ptr[j]);
    }
    err_count += (*ref_eob_ptr != *eob_ptr);
    if (err_count && !err_count_total) {
      first_failure = i;
    }
    err_count_total += err_count;
  }
  EXPECT_EQ(0, err_count_total)
      << "Error: Quantization Test, C output doesn't match SSE2 output. "
      << "First failed at test case " << first_failure;
}

TEST_P(VP9QuantizeTest, EOBCheck) {
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, coeff_ptr, 256);
  DECLARE_ALIGNED_ARRAY(16, int16_t, zbin_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, int16_t, round_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, int16_t, quant_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, int16_t, quant_shift_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, qcoeff_ptr, 256);
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, dqcoeff_ptr, 256);
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, ref_qcoeff_ptr, 256);
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, ref_dqcoeff_ptr, 256);
  DECLARE_ALIGNED_ARRAY(16, int16_t, dequant_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, uint16_t, eob_ptr, 1);
  DECLARE_ALIGNED_ARRAY(16, uint16_t, ref_eob_ptr, 1);
  int err_count_total = 0;
  int first_failure = -1;
  for (int i = 0; i < number_of_iterations; ++i) {
    int skip_block = i == 0;
    TX_SIZE sz = (TX_SIZE)(i % 3);  // TX_4X4, TX_8X8 TX_16X16
    TX_TYPE tx_type = (TX_TYPE)((i >> 2) % 3);
    const scan_order *scan_order = &vp9_scan_orders[sz][tx_type];
    int count = (4 << sz) * (4 << sz);  // 16, 64, 256
    int err_count = 0;
    *eob_ptr = rnd.Rand16();
    *ref_eob_ptr = *eob_ptr;
    // Two random entries
    for (int j = 0; j < count; j++) {
      coeff_ptr[j] = 0;
    }
    coeff_ptr[rnd(count)] = rnd.Rand16()&mask_;
    coeff_ptr[rnd(count)] = rnd.Rand16()&mask_;
    for (int j = 0; j < 2; j++) {
      zbin_ptr[j] = rnd.Rand16()&mask_;
      round_ptr[j] = rnd.Rand16();
      quant_ptr[j] = rnd.Rand16();
      quant_shift_ptr[j] = rnd.Rand16();
      dequant_ptr[j] = rnd.Rand16();
    }

    ref_quantize_op_(coeff_ptr, count, skip_block, zbin_ptr, round_ptr,
                     quant_ptr, quant_shift_ptr, ref_qcoeff_ptr,
                     ref_dqcoeff_ptr, dequant_ptr,
                     ref_eob_ptr, scan_order->scan, scan_order->iscan);
    ASM_REGISTER_STATE_CHECK(quantize_op_(coeff_ptr, count, skip_block,
                                          zbin_ptr, round_ptr, quant_ptr,
                                          quant_shift_ptr, qcoeff_ptr,
                                          dqcoeff_ptr, dequant_ptr, eob_ptr,
                                          scan_order->scan, scan_order->iscan));

    for (int j = 0; j < sz; ++j) {
      err_count += (ref_qcoeff_ptr[j]  != qcoeff_ptr[j]) |
          (ref_dqcoeff_ptr[j] != dqcoeff_ptr[j]);
    }
    err_count += (*ref_eob_ptr != *eob_ptr);
    if (err_count && !err_count_total) {
      first_failure = i;
    }
    err_count_total += err_count;
  }
  EXPECT_EQ(0, err_count_total)
      << "Error: Quantization Test, C output doesn't match SSE2 output. "
      << "First failed at test case " << first_failure;
}

TEST_P(VP9Quantize32Test, EOBCheck) {
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, coeff_ptr, 1024);
  DECLARE_ALIGNED_ARRAY(16, int16_t, zbin_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, int16_t, round_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, int16_t, quant_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, int16_t, quant_shift_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, qcoeff_ptr, 1024);
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, dqcoeff_ptr, 1024);
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, ref_qcoeff_ptr, 1024);
  DECLARE_ALIGNED_ARRAY(16, tran_low_t, ref_dqcoeff_ptr, 1024);
  DECLARE_ALIGNED_ARRAY(16, int16_t, dequant_ptr, 2);
  DECLARE_ALIGNED_ARRAY(16, uint16_t, eob_ptr, 1);
  DECLARE_ALIGNED_ARRAY(16, uint16_t, ref_eob_ptr, 1);
  int err_count_total = 0;
  int first_failure = -1;
  for (int i = 0; i < number_of_iterations; ++i) {
    int skip_block = i == 0;
    TX_SIZE sz = TX_32X32;
    TX_TYPE tx_type = (TX_TYPE)(i % 4);
    const scan_order *scan_order = &vp9_scan_orders[sz][tx_type];
    int count = (4 << sz) * (4 << sz);  // 1024
    int err_count = 0;
    *eob_ptr = rnd.Rand16();
    *ref_eob_ptr = *eob_ptr;
    for (int j = 0; j < count; j++) {
      coeff_ptr[j] = 0;
    }
    // Two random entries
    coeff_ptr[rnd(count)] = rnd.Rand16()&mask_;
    coeff_ptr[rnd(count)] = rnd.Rand16()&mask_;
    for (int j = 0; j < 2; j++) {
      zbin_ptr[j] = rnd.Rand16()&mask_;
      round_ptr[j] = rnd.Rand16();
      quant_ptr[j] = rnd.Rand16();
      quant_shift_ptr[j] = rnd.Rand16();
      dequant_ptr[j] = rnd.Rand16();
    }

    ref_quantize_op_(coeff_ptr, count, skip_block, zbin_ptr, round_ptr,
                     quant_ptr, quant_shift_ptr, ref_qcoeff_ptr,
                     ref_dqcoeff_ptr, dequant_ptr,
                     ref_eob_ptr, scan_order->scan, scan_order->iscan);
    ASM_REGISTER_STATE_CHECK(quantize_op_(coeff_ptr, count, skip_block,
                                          zbin_ptr, round_ptr, quant_ptr,
                                          quant_shift_ptr, qcoeff_ptr,
                                          dqcoeff_ptr, dequant_ptr, eob_ptr,
                                          scan_order->scan, scan_order->iscan));

    for (int j = 0; j < sz; ++j) {
      err_count += (ref_qcoeff_ptr[j]  != qcoeff_ptr[j]) |
          (ref_dqcoeff_ptr[j] != dqcoeff_ptr[j]);
    }
    err_count += (*ref_eob_ptr != *eob_ptr);
    if (err_count && !err_count_total) {
      first_failure = i;
    }
    err_count_total += err_count;
  }
  EXPECT_EQ(0, err_count_total)
      << "Error: Quantization Test, C output doesn't match SSE2 output. "
      << "First failed at test case " << first_failure;
}
using std::tr1::make_tuple;

#if HAVE_SSE2
INSTANTIATE_TEST_CASE_P(
    SSE2, VP9QuantizeTest,
    ::testing::Values(
        make_tuple(&vp9_highbd_quantize_b_sse2,
                   &vp9_highbd_quantize_b_c, VPX_BITS_8),
        make_tuple(&vp9_highbd_quantize_b_sse2,
                   &vp9_highbd_quantize_b_c, VPX_BITS_10),
        make_tuple(&vp9_highbd_quantize_b_sse2,
                   &vp9_highbd_quantize_b_c, VPX_BITS_12)));
INSTANTIATE_TEST_CASE_P(
    SSE2, VP9Quantize32Test,
    ::testing::Values(
        make_tuple(&vp9_highbd_quantize_b_32x32_sse2,
                   &vp9_highbd_quantize_b_32x32_c, VPX_BITS_8),
        make_tuple(&vp9_highbd_quantize_b_32x32_sse2,
                   &vp9_highbd_quantize_b_32x32_c, VPX_BITS_10),
        make_tuple(&vp9_highbd_quantize_b_32x32_sse2,
                   &vp9_highbd_quantize_b_32x32_c, VPX_BITS_12)));
#endif  // HAVE_SSE2
#endif  // CONFIG_VP9_HIGHBITDEPTH
}  // namespace