dct16x16_test.cc 29.1 KB
Newer Older
Daniel Kang's avatar
Daniel Kang committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 *  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 <math.h>
#include <stdlib.h>
#include <string.h>

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

17
#include "./vp10_rtcd.h"
18
#include "./vpx_dsp_rtcd.h"
19
20
21
22
#include "test/acm_random.h"
#include "test/clear_system_state.h"
#include "test/register_state_check.h"
#include "test/util.h"
Yaowu Xu's avatar
Yaowu Xu committed
23
24
#include "av1/common/entropy.h"
#include "av1/common/scan.h"
Yaowu Xu's avatar
Yaowu Xu committed
25
26
27
#include "aom/vpx_codec.h"
#include "aom/vpx_integer.h"
#include "aom_ports/mem.h"
28

Daniel Kang's avatar
Daniel Kang committed
29
30
31
using libvpx_test::ACMRandom;

namespace {
32
33
34
35

#ifdef _MSC_VER
static int round(double x) {
  if (x < 0)
Yaowu Xu's avatar
Yaowu Xu committed
36
    return static_cast<int>(ceil(x - 0.5));
37
  else
Yaowu Xu's avatar
Yaowu Xu committed
38
    return static_cast<int>(floor(x + 0.5));
39
40
}
#endif
Daniel Kang's avatar
Daniel Kang committed
41

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const int kNumCoeffs = 256;
const double C1 = 0.995184726672197;
const double C2 = 0.98078528040323;
const double C3 = 0.956940335732209;
const double C4 = 0.923879532511287;
const double C5 = 0.881921264348355;
const double C6 = 0.831469612302545;
const double C7 = 0.773010453362737;
const double C8 = 0.707106781186548;
const double C9 = 0.634393284163646;
const double C10 = 0.555570233019602;
const double C11 = 0.471396736825998;
const double C12 = 0.38268343236509;
const double C13 = 0.290284677254462;
const double C14 = 0.195090322016128;
const double C15 = 0.098017140329561;
58

Jingning Han's avatar
Jingning Han committed
59
void butterfly_16x16_dct_1d(double input[16], double output[16]) {
Daniel Kang's avatar
Daniel Kang committed
60
61
62
63
64
  double step[16];
  double intermediate[16];
  double temp1, temp2;

  // step 1
clang-format's avatar
clang-format committed
65
66
67
68
69
70
71
72
73
74
  step[0] = input[0] + input[15];
  step[1] = input[1] + input[14];
  step[2] = input[2] + input[13];
  step[3] = input[3] + input[12];
  step[4] = input[4] + input[11];
  step[5] = input[5] + input[10];
  step[6] = input[6] + input[9];
  step[7] = input[7] + input[8];
  step[8] = input[7] - input[8];
  step[9] = input[6] - input[9];
Daniel Kang's avatar
Daniel Kang committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  step[10] = input[5] - input[10];
  step[11] = input[4] - input[11];
  step[12] = input[3] - input[12];
  step[13] = input[2] - input[13];
  step[14] = input[1] - input[14];
  step[15] = input[0] - input[15];

  // step 2
  output[0] = step[0] + step[7];
  output[1] = step[1] + step[6];
  output[2] = step[2] + step[5];
  output[3] = step[3] + step[4];
  output[4] = step[3] - step[4];
  output[5] = step[2] - step[5];
  output[6] = step[1] - step[6];
  output[7] = step[0] - step[7];

clang-format's avatar
clang-format committed
92
  temp1 = step[8] * C7;
93
  temp2 = step[15] * C9;
clang-format's avatar
clang-format committed
94
  output[8] = temp1 + temp2;
Daniel Kang's avatar
Daniel Kang committed
95

clang-format's avatar
clang-format committed
96
  temp1 = step[9] * C11;
97
  temp2 = step[14] * C5;
clang-format's avatar
clang-format committed
98
  output[9] = temp1 - temp2;
Daniel Kang's avatar
Daniel Kang committed
99

100
101
  temp1 = step[10] * C3;
  temp2 = step[13] * C13;
Daniel Kang's avatar
Daniel Kang committed
102
103
  output[10] = temp1 + temp2;

104
105
  temp1 = step[11] * C15;
  temp2 = step[12] * C1;
Daniel Kang's avatar
Daniel Kang committed
106
107
  output[11] = temp1 - temp2;

108
109
  temp1 = step[11] * C1;
  temp2 = step[12] * C15;
Daniel Kang's avatar
Daniel Kang committed
110
111
  output[12] = temp2 + temp1;

112
113
  temp1 = step[10] * C13;
  temp2 = step[13] * C3;
Daniel Kang's avatar
Daniel Kang committed
114
115
  output[13] = temp2 - temp1;

clang-format's avatar
clang-format committed
116
  temp1 = step[9] * C5;
117
  temp2 = step[14] * C11;
Daniel Kang's avatar
Daniel Kang committed
118
119
  output[14] = temp2 + temp1;

clang-format's avatar
clang-format committed
120
  temp1 = step[8] * C9;
121
  temp2 = step[15] * C7;
Daniel Kang's avatar
Daniel Kang committed
122
123
124
  output[15] = temp2 - temp1;

  // step 3
clang-format's avatar
clang-format committed
125
126
127
128
  step[0] = output[0] + output[3];
  step[1] = output[1] + output[2];
  step[2] = output[1] - output[2];
  step[3] = output[0] - output[3];
Daniel Kang's avatar
Daniel Kang committed
129

130
131
  temp1 = output[4] * C14;
  temp2 = output[7] * C2;
clang-format's avatar
clang-format committed
132
  step[4] = temp1 + temp2;
Daniel Kang's avatar
Daniel Kang committed
133

134
135
  temp1 = output[5] * C10;
  temp2 = output[6] * C6;
clang-format's avatar
clang-format committed
136
  step[5] = temp1 + temp2;
Daniel Kang's avatar
Daniel Kang committed
137

138
139
  temp1 = output[5] * C6;
  temp2 = output[6] * C10;
clang-format's avatar
clang-format committed
140
  step[6] = temp2 - temp1;
Daniel Kang's avatar
Daniel Kang committed
141

142
143
  temp1 = output[4] * C2;
  temp2 = output[7] * C14;
clang-format's avatar
clang-format committed
144
  step[7] = temp2 - temp1;
Daniel Kang's avatar
Daniel Kang committed
145

clang-format's avatar
clang-format committed
146
147
148
149
  step[8] = output[8] + output[11];
  step[9] = output[9] + output[10];
  step[10] = output[9] - output[10];
  step[11] = output[8] - output[11];
Daniel Kang's avatar
Daniel Kang committed
150
151
152
153
154
155
156

  step[12] = output[12] + output[15];
  step[13] = output[13] + output[14];
  step[14] = output[13] - output[14];
  step[15] = output[12] - output[15];

  // step 4
clang-format's avatar
clang-format committed
157
158
  output[0] = (step[0] + step[1]);
  output[8] = (step[0] - step[1]);
Daniel Kang's avatar
Daniel Kang committed
159

160
161
  temp1 = step[2] * C12;
  temp2 = step[3] * C4;
Daniel Kang's avatar
Daniel Kang committed
162
  temp1 = temp1 + temp2;
clang-format's avatar
clang-format committed
163
  output[4] = 2 * (temp1 * C8);
Daniel Kang's avatar
Daniel Kang committed
164

165
166
  temp1 = step[2] * C4;
  temp2 = step[3] * C12;
Daniel Kang's avatar
Daniel Kang committed
167
  temp1 = temp2 - temp1;
168
  output[12] = 2 * (temp1 * C8);
Daniel Kang's avatar
Daniel Kang committed
169

clang-format's avatar
clang-format committed
170
171
  output[2] = 2 * ((step[4] + step[5]) * C8);
  output[14] = 2 * ((step[7] - step[6]) * C8);
Daniel Kang's avatar
Daniel Kang committed
172
173
174

  temp1 = step[4] - step[5];
  temp2 = step[6] + step[7];
clang-format's avatar
clang-format committed
175
  output[6] = (temp1 + temp2);
Daniel Kang's avatar
Daniel Kang committed
176
177
178
179
180
  output[10] = (temp1 - temp2);

  intermediate[8] = step[8] + step[14];
  intermediate[9] = step[9] + step[15];

181
182
  temp1 = intermediate[8] * C12;
  temp2 = intermediate[9] * C4;
Daniel Kang's avatar
Daniel Kang committed
183
  temp1 = temp1 - temp2;
184
  output[3] = 2 * (temp1 * C8);
Daniel Kang's avatar
Daniel Kang committed
185

186
187
  temp1 = intermediate[8] * C4;
  temp2 = intermediate[9] * C12;
Daniel Kang's avatar
Daniel Kang committed
188
  temp1 = temp2 + temp1;
189
  output[13] = 2 * (temp1 * C8);
Daniel Kang's avatar
Daniel Kang committed
190

clang-format's avatar
clang-format committed
191
  output[9] = 2 * ((step[10] + step[11]) * C8);
Daniel Kang's avatar
Daniel Kang committed
192
193
194
195

  intermediate[11] = step[10] - step[11];
  intermediate[12] = step[12] + step[13];
  intermediate[13] = step[12] - step[13];
clang-format's avatar
clang-format committed
196
197
  intermediate[14] = step[8] - step[14];
  intermediate[15] = step[9] - step[15];
Daniel Kang's avatar
Daniel Kang committed
198
199

  output[15] = (intermediate[11] + intermediate[12]);
clang-format's avatar
clang-format committed
200
  output[1] = -(intermediate[11] - intermediate[12]);
Daniel Kang's avatar
Daniel Kang committed
201

clang-format's avatar
clang-format committed
202
  output[7] = 2 * (intermediate[13] * C8);
Daniel Kang's avatar
Daniel Kang committed
203

204
205
  temp1 = intermediate[14] * C12;
  temp2 = intermediate[15] * C4;
Daniel Kang's avatar
Daniel Kang committed
206
  temp1 = temp1 - temp2;
207
  output[11] = -2 * (temp1 * C8);
Daniel Kang's avatar
Daniel Kang committed
208

209
210
  temp1 = intermediate[14] * C4;
  temp2 = intermediate[15] * C12;
Daniel Kang's avatar
Daniel Kang committed
211
  temp1 = temp2 + temp1;
clang-format's avatar
clang-format committed
212
  output[5] = 2 * (temp1 * C8);
Daniel Kang's avatar
Daniel Kang committed
213
214
}

215
void reference_16x16_dct_2d(int16_t input[256], double output[256]) {
Daniel Kang's avatar
Daniel Kang committed
216
217
218
  // First transform columns
  for (int i = 0; i < 16; ++i) {
    double temp_in[16], temp_out[16];
clang-format's avatar
clang-format committed
219
    for (int j = 0; j < 16; ++j) temp_in[j] = input[j * 16 + i];
Daniel Kang's avatar
Daniel Kang committed
220
    butterfly_16x16_dct_1d(temp_in, temp_out);
clang-format's avatar
clang-format committed
221
    for (int j = 0; j < 16; ++j) output[j * 16 + i] = temp_out[j];
Daniel Kang's avatar
Daniel Kang committed
222
223
224
225
  }
  // Then transform rows
  for (int i = 0; i < 16; ++i) {
    double temp_in[16], temp_out[16];
clang-format's avatar
clang-format committed
226
    for (int j = 0; j < 16; ++j) temp_in[j] = output[j + i * 16];
Daniel Kang's avatar
Daniel Kang committed
227
228
    butterfly_16x16_dct_1d(temp_in, temp_out);
    // Scale by some magic number
clang-format's avatar
clang-format committed
229
    for (int j = 0; j < 16; ++j) output[j + i * 16] = temp_out[j] / 2;
Daniel Kang's avatar
Daniel Kang committed
230
231
232
  }
}

233
234
235
typedef void (*FdctFunc)(const int16_t *in, tran_low_t *out, int stride);
typedef void (*IdctFunc)(const tran_low_t *in, uint8_t *out, int stride);
typedef void (*FhtFunc)(const int16_t *in, tran_low_t *out, int stride,
236
                        int tx_type);
237
typedef void (*IhtFunc)(const tran_low_t *in, uint8_t *out, int stride,
238
                        int tx_type);
Daniel Kang's avatar
Daniel Kang committed
239

240
241
typedef std::tr1::tuple<FdctFunc, IdctFunc, int, vpx_bit_depth_t> Dct16x16Param;
typedef std::tr1::tuple<FhtFunc, IhtFunc, int, vpx_bit_depth_t> Ht16x16Param;
242
243
typedef std::tr1::tuple<IdctFunc, IdctFunc, int, vpx_bit_depth_t>
    Idct16x16Param;
244

245
void fdct16x16_ref(const int16_t *in, tran_low_t *out, int stride,
246
                   int /*tx_type*/) {
247
  vpx_fdct16x16_c(in, out, stride);
248
249
}

250
void idct16x16_ref(const tran_low_t *in, uint8_t *dest, int stride,
251
                   int /*tx_type*/) {
252
  vpx_idct16x16_256_add_c(in, dest, stride);
253
254
}

clang-format's avatar
clang-format committed
255
void fht16x16_ref(const int16_t *in, tran_low_t *out, int stride, int tx_type) {
256
  vp10_fht16x16_c(in, out, stride, tx_type);
257
258
}

259
260
void iht16x16_ref(const tran_low_t *in, uint8_t *dest, int stride,
                  int tx_type) {
261
  vp10_iht16x16_256_add_c(in, dest, stride, tx_type);
262
263
}

264
#if CONFIG_VPX_HIGHBITDEPTH
265
void idct16x16_10(const tran_low_t *in, uint8_t *out, int stride) {
266
  vpx_highbd_idct16x16_256_add_c(in, out, stride, 10);
267
268
269
}

void idct16x16_12(const tran_low_t *in, uint8_t *out, int stride) {
270
  vpx_highbd_idct16x16_256_add_c(in, out, stride, 12);
271
272
273
274
275
276
277
278
279
280
281
282
283
}

void idct16x16_10_ref(const tran_low_t *in, uint8_t *out, int stride,
                      int tx_type) {
  idct16x16_10(in, out, stride);
}

void idct16x16_12_ref(const tran_low_t *in, uint8_t *out, int stride,
                      int tx_type) {
  idct16x16_12(in, out, stride);
}

void iht16x16_10(const tran_low_t *in, uint8_t *out, int stride, int tx_type) {
284
  vp10_highbd_iht16x16_256_add_c(in, out, stride, tx_type, 10);
285
286
287
}

void iht16x16_12(const tran_low_t *in, uint8_t *out, int stride, int tx_type) {
288
  vp10_highbd_iht16x16_256_add_c(in, out, stride, tx_type, 12);
289
}
290
291

void idct16x16_10_add_10_c(const tran_low_t *in, uint8_t *out, int stride) {
292
  vpx_highbd_idct16x16_10_add_c(in, out, stride, 10);
293
294
295
}

void idct16x16_10_add_12_c(const tran_low_t *in, uint8_t *out, int stride) {
296
  vpx_highbd_idct16x16_10_add_c(in, out, stride, 12);
297
298
299
300
}

#if HAVE_SSE2
void idct16x16_256_add_10_sse2(const tran_low_t *in, uint8_t *out, int stride) {
301
  vpx_highbd_idct16x16_256_add_sse2(in, out, stride, 10);
302
303
304
}

void idct16x16_256_add_12_sse2(const tran_low_t *in, uint8_t *out, int stride) {
305
  vpx_highbd_idct16x16_256_add_sse2(in, out, stride, 12);
306
307
308
}

void idct16x16_10_add_10_sse2(const tran_low_t *in, uint8_t *out, int stride) {
309
  vpx_highbd_idct16x16_10_add_sse2(in, out, stride, 10);
310
311
312
}

void idct16x16_10_add_12_sse2(const tran_low_t *in, uint8_t *out, int stride) {
313
  vpx_highbd_idct16x16_10_add_sse2(in, out, stride, 12);
314
315
}
#endif  // HAVE_SSE2
316
#endif  // CONFIG_VPX_HIGHBITDEPTH
317

318
class Trans16x16TestBase {
319
 public:
320
  virtual ~Trans16x16TestBase() {}
321

322
 protected:
323
  virtual void RunFwdTxfm(int16_t *in, tran_low_t *out, int stride) = 0;
324

325
  virtual void RunInvTxfm(tran_low_t *out, uint8_t *dst, int stride) = 0;
326
327
328

  void RunAccuracyCheck() {
    ACMRandom rnd(ACMRandom::DeterministicSeed());
329
330
    uint32_t max_error = 0;
    int64_t total_error = 0;
331
332
    const int count_test_block = 10000;
    for (int i = 0; i < count_test_block; ++i) {
333
334
335
336
      DECLARE_ALIGNED(16, int16_t, test_input_block[kNumCoeffs]);
      DECLARE_ALIGNED(16, tran_low_t, test_temp_block[kNumCoeffs]);
      DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
      DECLARE_ALIGNED(16, uint8_t, src[kNumCoeffs]);
337
#if CONFIG_VPX_HIGHBITDEPTH
338
339
      DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
      DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
340
#endif
341

342
      // Initialize a test block with input range [-mask_, mask_].
343
      for (int j = 0; j < kNumCoeffs; ++j) {
344
345
346
347
        if (bit_depth_ == VPX_BITS_8) {
          src[j] = rnd.Rand8();
          dst[j] = rnd.Rand8();
          test_input_block[j] = src[j] - dst[j];
348
#if CONFIG_VPX_HIGHBITDEPTH
349
350
351
352
353
354
        } else {
          src16[j] = rnd.Rand16() & mask_;
          dst16[j] = rnd.Rand16() & mask_;
          test_input_block[j] = src16[j] - dst16[j];
#endif
        }
355
356
      }

clang-format's avatar
clang-format committed
357
358
      ASM_REGISTER_STATE_CHECK(
          RunFwdTxfm(test_input_block, test_temp_block, pitch_));
359
      if (bit_depth_ == VPX_BITS_8) {
clang-format's avatar
clang-format committed
360
        ASM_REGISTER_STATE_CHECK(RunInvTxfm(test_temp_block, dst, pitch_));
361
#if CONFIG_VPX_HIGHBITDEPTH
362
363
364
365
366
      } else {
        ASM_REGISTER_STATE_CHECK(
            RunInvTxfm(test_temp_block, CONVERT_TO_BYTEPTR(dst16), pitch_));
#endif
      }
367
368

      for (int j = 0; j < kNumCoeffs; ++j) {
369
#if CONFIG_VPX_HIGHBITDEPTH
370
        const uint32_t diff =
clang-format's avatar
clang-format committed
371
            bit_depth_ == VPX_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
372
#else
373
        const uint32_t diff = dst[j] - src[j];
374
#endif
375
        const uint32_t error = diff * diff;
clang-format's avatar
clang-format committed
376
        if (max_error < error) max_error = error;
377
378
        total_error += error;
      }
Scott LaVarnway's avatar
Scott LaVarnway committed
379
    }
Daniel Kang's avatar
Daniel Kang committed
380

clang-format's avatar
clang-format committed
381
    EXPECT_GE(1u << 2 * (bit_depth_ - 8), max_error)
382
383
        << "Error: 16x16 FHT/IHT has an individual round trip error > 1";

384
    EXPECT_GE(count_test_block << 2 * (bit_depth_ - 8), total_error)
385
        << "Error: 16x16 FHT/IHT has average round trip error > 1 per block";
386
387
  }

388
  void RunCoeffCheck() {
389
390
    ACMRandom rnd(ACMRandom::DeterministicSeed());
    const int count_test_block = 1000;
391
392
393
    DECLARE_ALIGNED(16, int16_t, input_block[kNumCoeffs]);
    DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kNumCoeffs]);
    DECLARE_ALIGNED(16, tran_low_t, output_block[kNumCoeffs]);
394

395
    for (int i = 0; i < count_test_block; ++i) {
396
      // Initialize a test block with input range [-mask_, mask_].
397
      for (int j = 0; j < kNumCoeffs; ++j)
398
        input_block[j] = (rnd.Rand16() & mask_) - (rnd.Rand16() & mask_);
399
400

      fwd_txfm_ref(input_block, output_ref_block, pitch_, tx_type_);
401
      ASM_REGISTER_STATE_CHECK(RunFwdTxfm(input_block, output_block, pitch_));
402
403
404
405
406
407
408
409
410
411

      // The minimum quant value is 4.
      for (int j = 0; j < kNumCoeffs; ++j)
        EXPECT_EQ(output_block[j], output_ref_block[j]);
    }
  }

  void RunMemCheck() {
    ACMRandom rnd(ACMRandom::DeterministicSeed());
    const int count_test_block = 1000;
412
413
414
    DECLARE_ALIGNED(16, int16_t, input_extreme_block[kNumCoeffs]);
    DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kNumCoeffs]);
    DECLARE_ALIGNED(16, tran_low_t, output_block[kNumCoeffs]);
Scott LaVarnway's avatar
Scott LaVarnway committed
415

416
    for (int i = 0; i < count_test_block; ++i) {
417
      // Initialize a test block with input range [-mask_, mask_].
418
      for (int j = 0; j < kNumCoeffs; ++j) {
419
        input_extreme_block[j] = rnd.Rand8() % 2 ? mask_ : -mask_;
420
      }
421
      if (i == 0) {
clang-format's avatar
clang-format committed
422
        for (int j = 0; j < kNumCoeffs; ++j) input_extreme_block[j] = mask_;
423
      } else if (i == 1) {
clang-format's avatar
clang-format committed
424
        for (int j = 0; j < kNumCoeffs; ++j) input_extreme_block[j] = -mask_;
425
      }
426

427
      fwd_txfm_ref(input_extreme_block, output_ref_block, pitch_, tx_type_);
clang-format's avatar
clang-format committed
428
429
      ASM_REGISTER_STATE_CHECK(
          RunFwdTxfm(input_extreme_block, output_block, pitch_));
430
431
432

      // The minimum quant value is 4.
      for (int j = 0; j < kNumCoeffs; ++j) {
433
        EXPECT_EQ(output_block[j], output_ref_block[j]);
434
        EXPECT_GE(4 * DCT_MAX_VALUE << (bit_depth_ - 8), abs(output_block[j]))
435
436
            << "Error: 16x16 FDCT has coefficient larger than 4*DCT_MAX_VALUE";
      }
437
    }
438
439
  }

440
441
  void RunQuantCheck(int dc_thred, int ac_thred) {
    ACMRandom rnd(ACMRandom::DeterministicSeed());
442
    const int count_test_block = 100000;
443
444
    DECLARE_ALIGNED(16, int16_t, input_extreme_block[kNumCoeffs]);
    DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kNumCoeffs]);
445

446
447
    DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
    DECLARE_ALIGNED(16, uint8_t, ref[kNumCoeffs]);
448
#if CONFIG_VPX_HIGHBITDEPTH
449
450
    DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
    DECLARE_ALIGNED(16, uint16_t, ref16[kNumCoeffs]);
451
#endif
452
453

    for (int i = 0; i < count_test_block; ++i) {
454
      // Initialize a test block with input range [-mask_, mask_].
455
      for (int j = 0; j < kNumCoeffs; ++j) {
456
        input_extreme_block[j] = rnd.Rand8() % 2 ? mask_ : -mask_;
457
458
      }
      if (i == 0)
clang-format's avatar
clang-format committed
459
        for (int j = 0; j < kNumCoeffs; ++j) input_extreme_block[j] = mask_;
460
      if (i == 1)
clang-format's avatar
clang-format committed
461
        for (int j = 0; j < kNumCoeffs; ++j) input_extreme_block[j] = -mask_;
462
463
464
465

      fwd_txfm_ref(input_extreme_block, output_ref_block, pitch_, tx_type_);

      // clear reconstructed pixel buffers
James Zern's avatar
James Zern committed
466
467
      memset(dst, 0, kNumCoeffs * sizeof(uint8_t));
      memset(ref, 0, kNumCoeffs * sizeof(uint8_t));
468
#if CONFIG_VPX_HIGHBITDEPTH
James Zern's avatar
James Zern committed
469
470
      memset(dst16, 0, kNumCoeffs * sizeof(uint16_t));
      memset(ref16, 0, kNumCoeffs * sizeof(uint16_t));
471
#endif
472
473
474
475
476

      // quantization with maximum allowed step sizes
      output_ref_block[0] = (output_ref_block[0] / dc_thred) * dc_thred;
      for (int j = 1; j < kNumCoeffs; ++j)
        output_ref_block[j] = (output_ref_block[j] / ac_thred) * ac_thred;
477
478
479
      if (bit_depth_ == VPX_BITS_8) {
        inv_txfm_ref(output_ref_block, ref, pitch_, tx_type_);
        ASM_REGISTER_STATE_CHECK(RunInvTxfm(output_ref_block, dst, pitch_));
480
#if CONFIG_VPX_HIGHBITDEPTH
481
482
483
      } else {
        inv_txfm_ref(output_ref_block, CONVERT_TO_BYTEPTR(ref16), pitch_,
                     tx_type_);
clang-format's avatar
clang-format committed
484
485
        ASM_REGISTER_STATE_CHECK(
            RunInvTxfm(output_ref_block, CONVERT_TO_BYTEPTR(dst16), pitch_));
486
487
488
#endif
      }
      if (bit_depth_ == VPX_BITS_8) {
clang-format's avatar
clang-format committed
489
        for (int j = 0; j < kNumCoeffs; ++j) EXPECT_EQ(ref[j], dst[j]);
490
#if CONFIG_VPX_HIGHBITDEPTH
491
      } else {
clang-format's avatar
clang-format committed
492
        for (int j = 0; j < kNumCoeffs; ++j) EXPECT_EQ(ref16[j], dst16[j]);
493
494
#endif
      }
495
496
497
    }
  }

498
499
500
  void RunInvAccuracyCheck() {
    ACMRandom rnd(ACMRandom::DeterministicSeed());
    const int count_test_block = 1000;
501
502
503
504
    DECLARE_ALIGNED(16, int16_t, in[kNumCoeffs]);
    DECLARE_ALIGNED(16, tran_low_t, coeff[kNumCoeffs]);
    DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
    DECLARE_ALIGNED(16, uint8_t, src[kNumCoeffs]);
505
#if CONFIG_VPX_HIGHBITDEPTH
506
507
    DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
    DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
508
#endif  // CONFIG_VPX_HIGHBITDEPTH
Daniel Kang's avatar
Daniel Kang committed
509

510
511
    for (int i = 0; i < count_test_block; ++i) {
      double out_r[kNumCoeffs];
Daniel Kang's avatar
Daniel Kang committed
512

513
514
      // Initialize a test block with input range [-255, 255].
      for (int j = 0; j < kNumCoeffs; ++j) {
515
516
517
518
        if (bit_depth_ == VPX_BITS_8) {
          src[j] = rnd.Rand8();
          dst[j] = rnd.Rand8();
          in[j] = src[j] - dst[j];
519
#if CONFIG_VPX_HIGHBITDEPTH
520
521
522
523
        } else {
          src16[j] = rnd.Rand16() & mask_;
          dst16[j] = rnd.Rand16() & mask_;
          in[j] = src16[j] - dst16[j];
524
#endif  // CONFIG_VPX_HIGHBITDEPTH
525
        }
526
527
528
529
      }

      reference_16x16_dct_2d(in, out_r);
      for (int j = 0; j < kNumCoeffs; ++j)
530
        coeff[j] = static_cast<tran_low_t>(round(out_r[j]));
531

532
533
      if (bit_depth_ == VPX_BITS_8) {
        ASM_REGISTER_STATE_CHECK(RunInvTxfm(coeff, dst, 16));
534
#if CONFIG_VPX_HIGHBITDEPTH
535
      } else {
clang-format's avatar
clang-format committed
536
537
        ASM_REGISTER_STATE_CHECK(
            RunInvTxfm(coeff, CONVERT_TO_BYTEPTR(dst16), 16));
538
#endif  // CONFIG_VPX_HIGHBITDEPTH
539
      }
540
541

      for (int j = 0; j < kNumCoeffs; ++j) {
542
#if CONFIG_VPX_HIGHBITDEPTH
543
544
545
        const uint32_t diff =
            bit_depth_ == VPX_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
#else
546
        const uint32_t diff = dst[j] - src[j];
547
#endif  // CONFIG_VPX_HIGHBITDEPTH
548
        const uint32_t error = diff * diff;
clang-format's avatar
clang-format committed
549
550
        EXPECT_GE(1u, error) << "Error: 16x16 IDCT has error " << error
                             << " at index " << j;
551
      }
Daniel Kang's avatar
Daniel Kang committed
552
553
    }
  }
554
555
556
557
558

  void CompareInvReference(IdctFunc ref_txfm, int thresh) {
    ACMRandom rnd(ACMRandom::DeterministicSeed());
    const int count_test_block = 10000;
    const int eob = 10;
559
    const int16_t *scan = vp10_default_scan_orders[TX_16X16].scan;
560
561
562
    DECLARE_ALIGNED(16, tran_low_t, coeff[kNumCoeffs]);
    DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
    DECLARE_ALIGNED(16, uint8_t, ref[kNumCoeffs]);
563
#if CONFIG_VPX_HIGHBITDEPTH
564
565
    DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
    DECLARE_ALIGNED(16, uint16_t, ref16[kNumCoeffs]);
566
#endif  // CONFIG_VPX_HIGHBITDEPTH
567
568
569
570
571
572
573
574
575
576
577
578

    for (int i = 0; i < count_test_block; ++i) {
      for (int j = 0; j < kNumCoeffs; ++j) {
        if (j < eob) {
          // Random values less than the threshold, either positive or negative
          coeff[scan[j]] = rnd(thresh) * (1 - 2 * (i % 2));
        } else {
          coeff[scan[j]] = 0;
        }
        if (bit_depth_ == VPX_BITS_8) {
          dst[j] = 0;
          ref[j] = 0;
579
#if CONFIG_VPX_HIGHBITDEPTH
580
581
582
        } else {
          dst16[j] = 0;
          ref16[j] = 0;
583
#endif  // CONFIG_VPX_HIGHBITDEPTH
584
585
586
587
588
589
        }
      }
      if (bit_depth_ == VPX_BITS_8) {
        ref_txfm(coeff, ref, pitch_);
        ASM_REGISTER_STATE_CHECK(RunInvTxfm(coeff, dst, pitch_));
      } else {
590
#if CONFIG_VPX_HIGHBITDEPTH
591
        ref_txfm(coeff, CONVERT_TO_BYTEPTR(ref16), pitch_);
clang-format's avatar
clang-format committed
592
593
        ASM_REGISTER_STATE_CHECK(
            RunInvTxfm(coeff, CONVERT_TO_BYTEPTR(dst16), pitch_));
594
#endif  // CONFIG_VPX_HIGHBITDEPTH
595
596
597
      }

      for (int j = 0; j < kNumCoeffs; ++j) {
598
#if CONFIG_VPX_HIGHBITDEPTH
599
600
601
602
        const uint32_t diff =
            bit_depth_ == VPX_BITS_8 ? dst[j] - ref[j] : dst16[j] - ref16[j];
#else
        const uint32_t diff = dst[j] - ref[j];
603
#endif  // CONFIG_VPX_HIGHBITDEPTH
604
        const uint32_t error = diff * diff;
clang-format's avatar
clang-format committed
605
606
        EXPECT_EQ(0u, error) << "Error: 16x16 IDCT Comparison has error "
                             << error << " at index " << j;
607
608
609
610
      }
    }
  }

611
612
  int pitch_;
  int tx_type_;
613
614
  vpx_bit_depth_t bit_depth_;
  int mask_;
615
616
  FhtFunc fwd_txfm_ref;
  IhtFunc inv_txfm_ref;
617
};
Daniel Kang's avatar
Daniel Kang committed
618

clang-format's avatar
clang-format committed
619
620
class Trans16x16DCT : public Trans16x16TestBase,
                      public ::testing::TestWithParam<Dct16x16Param> {
621
622
 public:
  virtual ~Trans16x16DCT() {}
Daniel Kang's avatar
Daniel Kang committed
623

624
625
626
  virtual void SetUp() {
    fwd_txfm_ = GET_PARAM(0);
    inv_txfm_ = GET_PARAM(1);
clang-format's avatar
clang-format committed
627
    tx_type_ = GET_PARAM(2);
628
    bit_depth_ = GET_PARAM(3);
clang-format's avatar
clang-format committed
629
    pitch_ = 16;
630
    fwd_txfm_ref = fdct16x16_ref;
631
    inv_txfm_ref = idct16x16_ref;
632
    mask_ = (1 << bit_depth_) - 1;
633
#if CONFIG_VPX_HIGHBITDEPTH
634
    switch (bit_depth_) {
clang-format's avatar
clang-format committed
635
636
637
      case VPX_BITS_10: inv_txfm_ref = idct16x16_10_ref; break;
      case VPX_BITS_12: inv_txfm_ref = idct16x16_12_ref; break;
      default: inv_txfm_ref = idct16x16_ref; break;
638
639
640
641
    }
#else
    inv_txfm_ref = idct16x16_ref;
#endif
642
643
  }
  virtual void TearDown() { libvpx_test::ClearSystemState(); }
Daniel Kang's avatar
Daniel Kang committed
644

645
 protected:
646
  void RunFwdTxfm(int16_t *in, tran_low_t *out, int stride) {
647
648
    fwd_txfm_(in, out, stride);
  }
649
  void RunInvTxfm(tran_low_t *out, uint8_t *dst, int stride) {
650
    inv_txfm_(out, dst, stride);
Daniel Kang's avatar
Daniel Kang committed
651
  }
652

653
654
  FdctFunc fwd_txfm_;
  IdctFunc inv_txfm_;
655
656
};

clang-format's avatar
clang-format committed
657
TEST_P(Trans16x16DCT, AccuracyCheck) { RunAccuracyCheck(); }
658

clang-format's avatar
clang-format committed
659
TEST_P(Trans16x16DCT, CoeffCheck) { RunCoeffCheck(); }
660

clang-format's avatar
clang-format committed
661
TEST_P(Trans16x16DCT, MemCheck) { RunMemCheck(); }
662

663
664
665
666
667
668
TEST_P(Trans16x16DCT, QuantCheck) {
  // Use maximally allowed quantization step sizes for DC and AC
  // coefficients respectively.
  RunQuantCheck(1336, 1828);
}

clang-format's avatar
clang-format committed
669
TEST_P(Trans16x16DCT, InvAccuracyCheck) { RunInvAccuracyCheck(); }
670

clang-format's avatar
clang-format committed
671
672
class Trans16x16HT : public Trans16x16TestBase,
                     public ::testing::TestWithParam<Ht16x16Param> {
673
674
675
676
677
678
 public:
  virtual ~Trans16x16HT() {}

  virtual void SetUp() {
    fwd_txfm_ = GET_PARAM(0);
    inv_txfm_ = GET_PARAM(1);
clang-format's avatar
clang-format committed
679
    tx_type_ = GET_PARAM(2);
680
    bit_depth_ = GET_PARAM(3);
clang-format's avatar
clang-format committed
681
    pitch_ = 16;
682
    fwd_txfm_ref = fht16x16_ref;
683
    inv_txfm_ref = iht16x16_ref;
684
    mask_ = (1 << bit_depth_) - 1;
685
#if CONFIG_VPX_HIGHBITDEPTH
686
    switch (bit_depth_) {
clang-format's avatar
clang-format committed
687
688
689
      case VPX_BITS_10: inv_txfm_ref = iht16x16_10; break;
      case VPX_BITS_12: inv_txfm_ref = iht16x16_12; break;
      default: inv_txfm_ref = iht16x16_ref; break;
690
691
692
693
    }
#else
    inv_txfm_ref = iht16x16_ref;
#endif
694
  }
695
696
697
  virtual void TearDown() { libvpx_test::ClearSystemState(); }

 protected:
698
  void RunFwdTxfm(int16_t *in, tran_low_t *out, int stride) {
699
    fwd_txfm_(in, out, stride, tx_type_);
700
  }
701
  void RunInvTxfm(tran_low_t *out, uint8_t *dst, int stride) {
702
    inv_txfm_(out, dst, stride, tx_type_);
703
704
  }

705
706
  FhtFunc fwd_txfm_;
  IhtFunc inv_txfm_;
707
708
};

clang-format's avatar
clang-format committed
709
TEST_P(Trans16x16HT, AccuracyCheck) { RunAccuracyCheck(); }
710

clang-format's avatar
clang-format committed
711
TEST_P(Trans16x16HT, CoeffCheck) { RunCoeffCheck(); }
712

clang-format's avatar
clang-format committed
713
TEST_P(Trans16x16HT, MemCheck) { RunMemCheck(); }
714

715
716
717
TEST_P(Trans16x16HT, QuantCheck) {
  // The encoder skips any non-DC intra prediction modes,
  // when the quantization step size goes beyond 988.
718
  RunQuantCheck(429, 729);
719
720
}

clang-format's avatar
clang-format committed
721
722
class InvTrans16x16DCT : public Trans16x16TestBase,
                         public ::testing::TestWithParam<Idct16x16Param> {
723
724
725
726
727
728
729
730
731
732
 public:
  virtual ~InvTrans16x16DCT() {}

  virtual void SetUp() {
    ref_txfm_ = GET_PARAM(0);
    inv_txfm_ = GET_PARAM(1);
    thresh_ = GET_PARAM(2);
    bit_depth_ = GET_PARAM(3);
    pitch_ = 16;
    mask_ = (1 << bit_depth_) - 1;
clang-format's avatar
clang-format committed
733
  }
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
  virtual void TearDown() { libvpx_test::ClearSystemState(); }

 protected:
  void RunFwdTxfm(int16_t *in, tran_low_t *out, int stride) {}
  void RunInvTxfm(tran_low_t *out, uint8_t *dst, int stride) {
    inv_txfm_(out, dst, stride);
  }

  IdctFunc ref_txfm_;
  IdctFunc inv_txfm_;
  int thresh_;
};

TEST_P(InvTrans16x16DCT, CompareReference) {
  CompareInvReference(ref_txfm_, thresh_);
}

751
752
using std::tr1::make_tuple;

753
#if CONFIG_VPX_HIGHBITDEPTH
754
755
756
INSTANTIATE_TEST_CASE_P(
    C, Trans16x16DCT,
    ::testing::Values(
757
758
        make_tuple(&vpx_highbd_fdct16x16_c, &idct16x16_10, 0, VPX_BITS_10),
        make_tuple(&vpx_highbd_fdct16x16_c, &idct16x16_12, 0, VPX_BITS_12),
759
        make_tuple(&vpx_fdct16x16_c, &vpx_idct16x16_256_add_c, 0, VPX_BITS_8)));
760
#else
clang-format's avatar
clang-format committed
761
762
763
764
INSTANTIATE_TEST_CASE_P(C, Trans16x16DCT,
                        ::testing::Values(make_tuple(&vpx_fdct16x16_c,
                                                     &vpx_idct16x16_256_add_c,
                                                     0, VPX_BITS_8)));
765
#endif  // CONFIG_VPX_HIGHBITDEPTH
766

767
#if CONFIG_VPX_HIGHBITDEPTH
768
769
770
INSTANTIATE_TEST_CASE_P(
    C, Trans16x16HT,
    ::testing::Values(
771
772
773
774
775
776
777
778
779
780
781
782
        make_tuple(&vp10_highbd_fht16x16_c, &iht16x16_10, 0, VPX_BITS_10),
        make_tuple(&vp10_highbd_fht16x16_c, &iht16x16_10, 1, VPX_BITS_10),
        make_tuple(&vp10_highbd_fht16x16_c, &iht16x16_10, 2, VPX_BITS_10),
        make_tuple(&vp10_highbd_fht16x16_c, &iht16x16_10, 3, VPX_BITS_10),
        make_tuple(&vp10_highbd_fht16x16_c, &iht16x16_12, 0, VPX_BITS_12),
        make_tuple(&vp10_highbd_fht16x16_c, &iht16x16_12, 1, VPX_BITS_12),
        make_tuple(&vp10_highbd_fht16x16_c, &iht16x16_12, 2, VPX_BITS_12),
        make_tuple(&vp10_highbd_fht16x16_c, &iht16x16_12, 3, VPX_BITS_12),
        make_tuple(&vp10_fht16x16_c, &vp10_iht16x16_256_add_c, 0, VPX_BITS_8),
        make_tuple(&vp10_fht16x16_c, &vp10_iht16x16_256_add_c, 1, VPX_BITS_8),
        make_tuple(&vp10_fht16x16_c, &vp10_iht16x16_256_add_c, 2, VPX_BITS_8),
        make_tuple(&vp10_fht16x16_c, &vp10_iht16x16_256_add_c, 3, VPX_BITS_8)));
783
784
785
786
#else
INSTANTIATE_TEST_CASE_P(
    C, Trans16x16HT,
    ::testing::Values(
787
788
789
790
        make_tuple(&vp10_fht16x16_c, &vp10_iht16x16_256_add_c, 0, VPX_BITS_8),
        make_tuple(&vp10_fht16x16_c, &vp10_iht16x16_256_add_c, 1, VPX_BITS_8),
        make_tuple(&vp10_fht16x16_c, &vp10_iht16x16_256_add_c, 2, VPX_BITS_8),
        make_tuple(&vp10_fht16x16_c, &vp10_iht16x16_256_add_c, 3, VPX_BITS_8)));
791
#endif  // CONFIG_VPX_HIGHBITDEPTH
792

793
#if HAVE_NEON_ASM && !CONFIG_VPX_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
James Zern's avatar
James Zern committed
794
795
INSTANTIATE_TEST_CASE_P(
    NEON, Trans16x16DCT,
clang-format's avatar
clang-format committed
796
797
    ::testing::Values(make_tuple(&vpx_fdct16x16_c, &vpx_idct16x16_256_add_neon,
                                 0, VPX_BITS_8)));
James Zern's avatar
James Zern committed
798
799
#endif

800
#if HAVE_SSE2 && !CONFIG_VPX_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
801
802
INSTANTIATE_TEST_CASE_P(
    SSE2, Trans16x16DCT,
clang-format's avatar
clang-format committed
803
804
    ::testing::Values(make_tuple(&vpx_fdct16x16_sse2,
                                 &vpx_idct16x16_256_add_sse2, 0, VPX_BITS_8)));
805
806
INSTANTIATE_TEST_CASE_P(
    SSE2, Trans16x16HT,
clang-format's avatar
clang-format committed
807
808
809
810
811
812
813
814
    ::testing::Values(make_tuple(&vp10_fht16x16_sse2,
                                 &vp10_iht16x16_256_add_sse2, 0, VPX_BITS_8),
                      make_tuple(&vp10_fht16x16_sse2,
                                 &vp10_iht16x16_256_add_sse2, 1, VPX_BITS_8),
                      make_tuple(&vp10_fht16x16_sse2,
                                 &vp10_iht16x16_256_add_sse2, 2, VPX_BITS_8),
                      make_tuple(&vp10_fht16x16_sse2,
                                 &vp10_iht16x16_256_add_sse2, 3, VPX_BITS_8)));
815
#endif  // HAVE_SSE2 && !CONFIG_VPX_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
816

817
#if HAVE_SSE2 && CONFIG_VPX_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
818
819
820
INSTANTIATE_TEST_CASE_P(
    SSE2, Trans16x16DCT,
    ::testing::Values(
clang-format's avatar
clang-format committed
821
822
823
824
825
826
827
828
        make_tuple(&vpx_highbd_fdct16x16_sse2, &idct16x16_10, 0, VPX_BITS_10),
        make_tuple(&vpx_highbd_fdct16x16_c, &idct16x16_256_add_10_sse2, 0,
                   VPX_BITS_10),
        make_tuple(&vpx_highbd_fdct16x16_sse2, &idct16x16_12, 0, VPX_BITS_12),
        make_tuple(&vpx_highbd_fdct16x16_c, &idct16x16_256_add_12_sse2, 0,
                   VPX_BITS_12),
        make_tuple(&vpx_fdct16x16_sse2, &vpx_idct16x16_256_add_c, 0,
                   VPX_BITS_8)));
829
830
INSTANTIATE_TEST_CASE_P(
    SSE2, Trans16x16HT,
clang-format's avatar
clang-format committed
831
832
833
834
835
836
837
838
    ::testing::Values(make_tuple(&vp10_fht16x16_sse2, &vp10_iht16x16_256_add_c,
                                 0, VPX_BITS_8),
                      make_tuple(&vp10_fht16x16_sse2, &vp10_iht16x16_256_add_c,
                                 1, VPX_BITS_8),
                      make_tuple(&vp10_fht16x16_sse2, &vp10_iht16x16_256_add_c,
                                 2, VPX_BITS_8),
                      make_tuple(&vp10_fht16x16_sse2, &vp10_iht16x16_256_add_c,
                                 3, VPX_BITS_8)));
839
840
841
842
// Optimizations take effect at a threshold of 3155, so we use a value close to
// that to test both branches.
INSTANTIATE_TEST_CASE_P(
    SSE2, InvTrans16x16DCT,
clang-format's avatar
clang-format committed
843
844
845
846
847
848
849
850
    ::testing::Values(make_tuple(&idct16x16_10_add_10_c,
                                 &idct16x16_10_add_10_sse2, 3167, VPX_BITS_10),
                      make_tuple(&idct16x16_10, &idct16x16_256_add_10_sse2,
                                 3167, VPX_BITS_10),
                      make_tuple(&idct16x16_10_add_12_c,
                                 &idct16x16_10_add_12_sse2, 3167, VPX_BITS_12),
                      make_tuple(&idct16x16_12, &idct16x16_256_add_12_sse2,
                                 3167, VPX_BITS_12)));
851
#endif  // HAVE_SSE2 && CONFIG_VPX_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
852

853
#if HAVE_MSA && !CONFIG_VPX_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
clang-format's avatar
clang-format committed
854
855
856
857
INSTANTIATE_TEST_CASE_P(MSA, Trans16x16DCT,
                        ::testing::Values(make_tuple(&vpx_fdct16x16_msa,
                                                     &vpx_idct16x16_256_add_msa,
                                                     0, VPX_BITS_8)));
858
859
INSTANTIATE_TEST_CASE_P(
    MSA, Trans16x16HT,
clang-format's avatar
clang-format committed
860
861
862
863
864
865
866
867
    ::testing::Values(make_tuple(&vp10_fht16x16_msa, &vp10_iht16x16_256_add_msa,
                                 0, VPX_BITS_8),
                      make_tuple(&vp10_fht16x16_msa, &vp10_iht16x16_256_add_msa,
                                 1, VPX_BITS_8),
                      make_tuple(&vp10_fht16x16_msa, &vp10_iht16x16_256_add_msa,
                                 2, VPX_BITS_8),
                      make_tuple(&vp10_fht16x16_msa, &vp10_iht16x16_256_add_msa,
                                 3, VPX_BITS_8)));
868
#endif  // HAVE_MSA && !CONFIG_VPX_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
Daniel Kang's avatar
Daniel Kang committed
869
}  // namespace