dct16x16_test.cc 29.9 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 18 19
#include "test/acm_random.h"
#include "test/clear_system_state.h"
#include "test/register_state_check.h"
#include "test/util.h"
Daniel Kang's avatar
Daniel Kang committed
20

Yaowu Xu's avatar
Yaowu Xu committed
21
#include "./vp9_rtcd.h"
22
#include "vp9/common/vp9_entropy.h"
23
#include "vpx/vpx_codec.h"
24 25
#include "vpx/vpx_integer.h"

Daniel Kang's avatar
Daniel Kang committed
26 27 28
using libvpx_test::ACMRandom;

namespace {
29 30 31 32

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

39
const int kNumCoeffs = 256;
Daniel Kang's avatar
Daniel Kang committed
40 41 42 43 44 45 46 47
const double PI = 3.1415926535898;
void reference2_16x16_idct_2d(double *input, double *output) {
  double x;
  for (int l = 0; l < 16; ++l) {
    for (int k = 0; k < 16; ++k) {
      double s = 0;
      for (int i = 0; i < 16; ++i) {
        for (int j = 0; j < 16; ++j) {
Yaowu Xu's avatar
Yaowu Xu committed
48 49 50
          x = cos(PI * j * (l + 0.5) / 16.0) *
              cos(PI * i * (k + 0.5) / 16.0) *
              input[i * 16 + j] / 256;
Daniel Kang's avatar
Daniel Kang committed
51 52 53 54 55 56 57 58 59 60 61 62
          if (i != 0)
            x *= sqrt(2.0);
          if (j != 0)
            x *= sqrt(2.0);
          s += x;
        }
      }
      output[k*16+l] = s;
    }
  }
}

63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
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;
79

Jingning Han's avatar
Jingning Han committed
80
void butterfly_16x16_dct_1d(double input[16], double output[16]) {
Daniel Kang's avatar
Daniel Kang committed
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
  double step[16];
  double intermediate[16];
  double temp1, temp2;

  // step 1
  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];
  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];

113 114
  temp1 = step[ 8] * C7;
  temp2 = step[15] * C9;
Daniel Kang's avatar
Daniel Kang committed
115 116
  output[ 8] = temp1 + temp2;

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

121 122
  temp1 = step[10] * C3;
  temp2 = step[13] * C13;
Daniel Kang's avatar
Daniel Kang committed
123 124
  output[10] = temp1 + temp2;

125 126
  temp1 = step[11] * C15;
  temp2 = step[12] * C1;
Daniel Kang's avatar
Daniel Kang committed
127 128
  output[11] = temp1 - temp2;

129 130
  temp1 = step[11] * C1;
  temp2 = step[12] * C15;
Daniel Kang's avatar
Daniel Kang committed
131 132
  output[12] = temp2 + temp1;

133 134
  temp1 = step[10] * C13;
  temp2 = step[13] * C3;
Daniel Kang's avatar
Daniel Kang committed
135 136
  output[13] = temp2 - temp1;

137 138
  temp1 = step[ 9] * C5;
  temp2 = step[14] * C11;
Daniel Kang's avatar
Daniel Kang committed
139 140
  output[14] = temp2 + temp1;

141 142
  temp1 = step[ 8] * C9;
  temp2 = step[15] * C7;
Daniel Kang's avatar
Daniel Kang committed
143 144 145 146 147 148 149 150
  output[15] = temp2 - temp1;

  // step 3
  step[ 0] = output[0] + output[3];
  step[ 1] = output[1] + output[2];
  step[ 2] = output[1] - output[2];
  step[ 3] = output[0] - output[3];

151 152
  temp1 = output[4] * C14;
  temp2 = output[7] * C2;
Daniel Kang's avatar
Daniel Kang committed
153 154
  step[ 4] = temp1 + temp2;

155 156
  temp1 = output[5] * C10;
  temp2 = output[6] * C6;
Daniel Kang's avatar
Daniel Kang committed
157 158
  step[ 5] = temp1 + temp2;

159 160
  temp1 = output[5] * C6;
  temp2 = output[6] * C10;
Daniel Kang's avatar
Daniel Kang committed
161 162
  step[ 6] = temp2 - temp1;

163 164
  temp1 = output[4] * C2;
  temp2 = output[7] * C14;
Daniel Kang's avatar
Daniel Kang committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  step[ 7] = temp2 - temp1;

  step[ 8] = output[ 8] + output[11];
  step[ 9] = output[ 9] + output[10];
  step[10] = output[ 9] - output[10];
  step[11] = output[ 8] - output[11];

  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
  output[ 0] = (step[ 0] + step[ 1]);
  output[ 8] = (step[ 0] - step[ 1]);

181 182
  temp1 = step[2] * C12;
  temp2 = step[3] * C4;
Daniel Kang's avatar
Daniel Kang committed
183
  temp1 = temp1 + temp2;
184
  output[ 4] = 2*(temp1 * C8);
Daniel Kang's avatar
Daniel Kang committed
185

186 187
  temp1 = step[2] * C4;
  temp2 = step[3] * C12;
Daniel Kang's avatar
Daniel Kang committed
188
  temp1 = temp2 - temp1;
189
  output[12] = 2 * (temp1 * C8);
Daniel Kang's avatar
Daniel Kang committed
190

191 192
  output[ 2] = 2 * ((step[4] + step[ 5]) * C8);
  output[14] = 2 * ((step[7] - step[ 6]) * C8);
Daniel Kang's avatar
Daniel Kang committed
193 194 195 196 197 198 199 200 201

  temp1 = step[4] - step[5];
  temp2 = step[6] + step[7];
  output[ 6] = (temp1 + temp2);
  output[10] = (temp1 - temp2);

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

202 203
  temp1 = intermediate[8] * C12;
  temp2 = intermediate[9] * C4;
Daniel Kang's avatar
Daniel Kang committed
204
  temp1 = temp1 - temp2;
205
  output[3] = 2 * (temp1 * C8);
Daniel Kang's avatar
Daniel Kang committed
206

207 208
  temp1 = intermediate[8] * C4;
  temp2 = intermediate[9] * C12;
Daniel Kang's avatar
Daniel Kang committed
209
  temp1 = temp2 + temp1;
210
  output[13] = 2 * (temp1 * C8);
Daniel Kang's avatar
Daniel Kang committed
211

212
  output[ 9] = 2 * ((step[10] + step[11]) * C8);
Daniel Kang's avatar
Daniel Kang committed
213 214 215 216 217 218 219 220 221 222

  intermediate[11] = step[10] - step[11];
  intermediate[12] = step[12] + step[13];
  intermediate[13] = step[12] - step[13];
  intermediate[14] = step[ 8] - step[14];
  intermediate[15] = step[ 9] - step[15];

  output[15] = (intermediate[11] + intermediate[12]);
  output[ 1] = -(intermediate[11] - intermediate[12]);

223
  output[ 7] = 2 * (intermediate[13] * C8);
Daniel Kang's avatar
Daniel Kang committed
224

225 226
  temp1 = intermediate[14] * C12;
  temp2 = intermediate[15] * C4;
Daniel Kang's avatar
Daniel Kang committed
227
  temp1 = temp1 - temp2;
228
  output[11] = -2 * (temp1 * C8);
Daniel Kang's avatar
Daniel Kang committed
229

230 231
  temp1 = intermediate[14] * C4;
  temp2 = intermediate[15] * C12;
Daniel Kang's avatar
Daniel Kang committed
232
  temp1 = temp2 + temp1;
233
  output[ 5] = 2 * (temp1 * C8);
Daniel Kang's avatar
Daniel Kang committed
234 235
}

236
void reference_16x16_dct_2d(int16_t input[256], double output[256]) {
Daniel Kang's avatar
Daniel Kang committed
237 238 239 240
  // First transform columns
  for (int i = 0; i < 16; ++i) {
    double temp_in[16], temp_out[16];
    for (int j = 0; j < 16; ++j)
241
      temp_in[j] = input[j * 16 + i];
Daniel Kang's avatar
Daniel Kang committed
242 243
    butterfly_16x16_dct_1d(temp_in, temp_out);
    for (int j = 0; j < 16; ++j)
244
      output[j * 16 + i] = temp_out[j];
Daniel Kang's avatar
Daniel Kang committed
245 246 247 248 249
  }
  // Then transform rows
  for (int i = 0; i < 16; ++i) {
    double temp_in[16], temp_out[16];
    for (int j = 0; j < 16; ++j)
250
      temp_in[j] = output[j + i * 16];
Daniel Kang's avatar
Daniel Kang committed
251 252 253
    butterfly_16x16_dct_1d(temp_in, temp_out);
    // Scale by some magic number
    for (int j = 0; j < 16; ++j)
254
      output[j + i * 16] = temp_out[j]/2;
Daniel Kang's avatar
Daniel Kang committed
255 256 257
  }
}

258 259 260
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,
261
                        int tx_type);
262
typedef void (*IhtFunc)(const tran_low_t *in, uint8_t *out, int stride,
263
                        int tx_type);
Daniel Kang's avatar
Daniel Kang committed
264

265 266
typedef std::tr1::tuple<FdctFunc, IdctFunc, int, vpx_bit_depth_t> Dct16x16Param;
typedef std::tr1::tuple<FhtFunc, IhtFunc, int, vpx_bit_depth_t> Ht16x16Param;
267 268
typedef std::tr1::tuple<IdctFunc, IdctFunc, int, vpx_bit_depth_t>
    Idct16x16Param;
269

270
void fdct16x16_ref(const int16_t *in, tran_low_t *out, int stride,
271
                   int /*tx_type*/) {
272
  vp9_fdct16x16_c(in, out, stride);
273 274
}

275
void idct16x16_ref(const tran_low_t *in, uint8_t *dest, int stride,
276
                   int /*tx_type*/) {
277 278 279
  vp9_idct16x16_256_add_c(in, dest, stride);
}

280 281
void fht16x16_ref(const int16_t *in, tran_low_t *out, int stride,
                  int tx_type) {
282
  vp9_fht16x16_c(in, out, stride, tx_type);
283 284
}

285 286
void iht16x16_ref(const tran_low_t *in, uint8_t *dest, int stride,
                  int tx_type) {
287 288 289
  vp9_iht16x16_256_add_c(in, dest, stride, tx_type);
}

290 291
#if CONFIG_VP9_HIGHBITDEPTH
void idct16x16_10(const tran_low_t *in, uint8_t *out, int stride) {
292
  vp9_highbd_idct16x16_256_add_c(in, out, stride, 10);
293 294 295
}

void idct16x16_12(const tran_low_t *in, uint8_t *out, int stride) {
296
  vp9_highbd_idct16x16_256_add_c(in, out, stride, 12);
297 298 299 300 301 302 303 304 305 306 307 308 309
}

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) {
310
  vp9_highbd_iht16x16_256_add_c(in, out, stride, tx_type, 10);
311 312 313
}

void iht16x16_12(const tran_low_t *in, uint8_t *out, int stride, int tx_type) {
314
  vp9_highbd_iht16x16_256_add_c(in, out, stride, tx_type, 12);
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

void idct16x16_10_add_10_c(const tran_low_t *in, uint8_t *out, int stride) {
  vp9_highbd_idct16x16_10_add_c(in, out, stride, 10);
}

void idct16x16_10_add_12_c(const tran_low_t *in, uint8_t *out, int stride) {
  vp9_highbd_idct16x16_10_add_c(in, out, stride, 12);
}

#if HAVE_SSE2
void idct16x16_256_add_10_sse2(const tran_low_t *in, uint8_t *out, int stride) {
  vp9_highbd_idct16x16_256_add_sse2(in, out, stride, 10);
}

void idct16x16_256_add_12_sse2(const tran_low_t *in, uint8_t *out, int stride) {
  vp9_highbd_idct16x16_256_add_sse2(in, out, stride, 12);
}

void idct16x16_10_add_10_sse2(const tran_low_t *in, uint8_t *out, int stride) {
  vp9_highbd_idct16x16_10_add_sse2(in, out, stride, 10);
}

void idct16x16_10_add_12_sse2(const tran_low_t *in, uint8_t *out, int stride) {
  vp9_highbd_idct16x16_10_add_sse2(in, out, stride, 12);
}
#endif  // HAVE_SSE2
#endif  // CONFIG_VP9_HIGHBITDEPTH
343

344
class Trans16x16TestBase {
345
 public:
346
  virtual ~Trans16x16TestBase() {}
347

348
 protected:
349
  virtual void RunFwdTxfm(int16_t *in, tran_low_t *out, int stride) = 0;
350

351
  virtual void RunInvTxfm(tran_low_t *out, uint8_t *dst, int stride) = 0;
352 353 354

  void RunAccuracyCheck() {
    ACMRandom rnd(ACMRandom::DeterministicSeed());
355 356
    uint32_t max_error = 0;
    int64_t total_error = 0;
357 358
    const int count_test_block = 10000;
    for (int i = 0; i < count_test_block; ++i) {
359 360 361 362
      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]);
363
#if CONFIG_VP9_HIGHBITDEPTH
364 365
      DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
      DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
366
#endif
367

368
      // Initialize a test block with input range [-mask_, mask_].
369
      for (int j = 0; j < kNumCoeffs; ++j) {
370 371 372 373 374 375 376 377 378 379 380
        if (bit_depth_ == VPX_BITS_8) {
          src[j] = rnd.Rand8();
          dst[j] = rnd.Rand8();
          test_input_block[j] = src[j] - dst[j];
#if CONFIG_VP9_HIGHBITDEPTH
        } else {
          src16[j] = rnd.Rand16() & mask_;
          dst16[j] = rnd.Rand16() & mask_;
          test_input_block[j] = src16[j] - dst16[j];
#endif
        }
381 382
      }

383 384
      ASM_REGISTER_STATE_CHECK(RunFwdTxfm(test_input_block,
                                          test_temp_block, pitch_));
385 386 387 388 389 390 391 392 393
      if (bit_depth_ == VPX_BITS_8) {
        ASM_REGISTER_STATE_CHECK(
            RunInvTxfm(test_temp_block, dst, pitch_));
#if CONFIG_VP9_HIGHBITDEPTH
      } else {
        ASM_REGISTER_STATE_CHECK(
            RunInvTxfm(test_temp_block, CONVERT_TO_BYTEPTR(dst16), pitch_));
#endif
      }
394 395

      for (int j = 0; j < kNumCoeffs; ++j) {
396 397 398 399
#if CONFIG_VP9_HIGHBITDEPTH
        const uint32_t diff =
            bit_depth_ == VPX_BITS_8 ?  dst[j] - src[j] : dst16[j] - src16[j];
#else
400
        const uint32_t diff = dst[j] - src[j];
401
#endif
402
        const uint32_t error = diff * diff;
403 404 405 406
        if (max_error < error)
          max_error = error;
        total_error += error;
      }
Scott LaVarnway's avatar
Scott LaVarnway committed
407
    }
Daniel Kang's avatar
Daniel Kang committed
408

409
    EXPECT_GE(1u  << 2 * (bit_depth_ - 8), max_error)
410 411
        << "Error: 16x16 FHT/IHT has an individual round trip error > 1";

412
    EXPECT_GE(count_test_block << 2 * (bit_depth_ - 8), total_error)
413
        << "Error: 16x16 FHT/IHT has average round trip error > 1 per block";
414 415
  }

416
  void RunCoeffCheck() {
417 418
    ACMRandom rnd(ACMRandom::DeterministicSeed());
    const int count_test_block = 1000;
419 420 421
    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]);
422

423
    for (int i = 0; i < count_test_block; ++i) {
424
      // Initialize a test block with input range [-mask_, mask_].
425
      for (int j = 0; j < kNumCoeffs; ++j)
426
        input_block[j] = (rnd.Rand16() & mask_) - (rnd.Rand16() & mask_);
427 428

      fwd_txfm_ref(input_block, output_ref_block, pitch_, tx_type_);
429
      ASM_REGISTER_STATE_CHECK(RunFwdTxfm(input_block, output_block, pitch_));
430 431 432 433 434 435 436 437 438 439

      // 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;
440 441 442
    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
443

444
    for (int i = 0; i < count_test_block; ++i) {
445
      // Initialize a test block with input range [-mask_, mask_].
446
      for (int j = 0; j < kNumCoeffs; ++j) {
447
        input_extreme_block[j] = rnd.Rand8() % 2 ? mask_ : -mask_;
448
      }
449
      if (i == 0) {
450
        for (int j = 0; j < kNumCoeffs; ++j)
451
          input_extreme_block[j] = mask_;
452
      } else if (i == 1) {
453
        for (int j = 0; j < kNumCoeffs; ++j)
454
          input_extreme_block[j] = -mask_;
455
      }
456

457
      fwd_txfm_ref(input_extreme_block, output_ref_block, pitch_, tx_type_);
458 459
      ASM_REGISTER_STATE_CHECK(RunFwdTxfm(input_extreme_block,
                                          output_block, pitch_));
460 461 462

      // The minimum quant value is 4.
      for (int j = 0; j < kNumCoeffs; ++j) {
463
        EXPECT_EQ(output_block[j], output_ref_block[j]);
464
        EXPECT_GE(4 * DCT_MAX_VALUE << (bit_depth_ - 8), abs(output_block[j]))
465 466
            << "Error: 16x16 FDCT has coefficient larger than 4*DCT_MAX_VALUE";
      }
467
    }
468 469
  }

470 471
  void RunQuantCheck(int dc_thred, int ac_thred) {
    ACMRandom rnd(ACMRandom::DeterministicSeed());
472
    const int count_test_block = 100000;
473 474
    DECLARE_ALIGNED(16, int16_t, input_extreme_block[kNumCoeffs]);
    DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kNumCoeffs]);
475

476 477
    DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
    DECLARE_ALIGNED(16, uint8_t, ref[kNumCoeffs]);
478
#if CONFIG_VP9_HIGHBITDEPTH
479 480
    DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
    DECLARE_ALIGNED(16, uint16_t, ref16[kNumCoeffs]);
481
#endif
482 483

    for (int i = 0; i < count_test_block; ++i) {
484
      // Initialize a test block with input range [-mask_, mask_].
485
      for (int j = 0; j < kNumCoeffs; ++j) {
486
        input_extreme_block[j] = rnd.Rand8() % 2 ? mask_ : -mask_;
487 488 489
      }
      if (i == 0)
        for (int j = 0; j < kNumCoeffs; ++j)
490
          input_extreme_block[j] = mask_;
491 492
      if (i == 1)
        for (int j = 0; j < kNumCoeffs; ++j)
493
          input_extreme_block[j] = -mask_;
494 495 496 497

      fwd_txfm_ref(input_extreme_block, output_ref_block, pitch_, tx_type_);

      // clear reconstructed pixel buffers
James Zern's avatar
James Zern committed
498 499
      memset(dst, 0, kNumCoeffs * sizeof(uint8_t));
      memset(ref, 0, kNumCoeffs * sizeof(uint8_t));
500
#if CONFIG_VP9_HIGHBITDEPTH
James Zern's avatar
James Zern committed
501 502
      memset(dst16, 0, kNumCoeffs * sizeof(uint16_t));
      memset(ref16, 0, kNumCoeffs * sizeof(uint16_t));
503
#endif
504 505 506 507 508

      // 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;
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
      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_));
#if CONFIG_VP9_HIGHBITDEPTH
      } else {
        inv_txfm_ref(output_ref_block, CONVERT_TO_BYTEPTR(ref16), pitch_,
                     tx_type_);
        ASM_REGISTER_STATE_CHECK(RunInvTxfm(output_ref_block,
                                            CONVERT_TO_BYTEPTR(dst16), pitch_));
#endif
      }
      if (bit_depth_ == VPX_BITS_8) {
        for (int j = 0; j < kNumCoeffs; ++j)
          EXPECT_EQ(ref[j], dst[j]);
#if CONFIG_VP9_HIGHBITDEPTH
      } else {
        for (int j = 0; j < kNumCoeffs; ++j)
          EXPECT_EQ(ref16[j], dst16[j]);
#endif
      }
529 530 531
    }
  }

532 533 534
  void RunInvAccuracyCheck() {
    ACMRandom rnd(ACMRandom::DeterministicSeed());
    const int count_test_block = 1000;
535 536 537 538
    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]);
539
#if CONFIG_VP9_HIGHBITDEPTH
540 541
    DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
    DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
542
#endif  // CONFIG_VP9_HIGHBITDEPTH
Daniel Kang's avatar
Daniel Kang committed
543

544 545
    for (int i = 0; i < count_test_block; ++i) {
      double out_r[kNumCoeffs];
Daniel Kang's avatar
Daniel Kang committed
546

547 548
      // Initialize a test block with input range [-255, 255].
      for (int j = 0; j < kNumCoeffs; ++j) {
549 550 551 552 553 554 555 556 557
        if (bit_depth_ == VPX_BITS_8) {
          src[j] = rnd.Rand8();
          dst[j] = rnd.Rand8();
          in[j] = src[j] - dst[j];
#if CONFIG_VP9_HIGHBITDEPTH
        } else {
          src16[j] = rnd.Rand16() & mask_;
          dst16[j] = rnd.Rand16() & mask_;
          in[j] = src16[j] - dst16[j];
558
#endif  // CONFIG_VP9_HIGHBITDEPTH
559
        }
560 561 562 563
      }

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

566 567 568 569 570 571
      if (bit_depth_ == VPX_BITS_8) {
        ASM_REGISTER_STATE_CHECK(RunInvTxfm(coeff, dst, 16));
#if CONFIG_VP9_HIGHBITDEPTH
      } else {
        ASM_REGISTER_STATE_CHECK(RunInvTxfm(coeff, CONVERT_TO_BYTEPTR(dst16),
                                            16));
572
#endif  // CONFIG_VP9_HIGHBITDEPTH
573
      }
574 575

      for (int j = 0; j < kNumCoeffs; ++j) {
576 577 578 579
#if CONFIG_VP9_HIGHBITDEPTH
        const uint32_t diff =
            bit_depth_ == VPX_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
#else
580
        const uint32_t diff = dst[j] - src[j];
581
#endif  // CONFIG_VP9_HIGHBITDEPTH
582 583
        const uint32_t error = diff * diff;
        EXPECT_GE(1u, error)
584 585 586
            << "Error: 16x16 IDCT has error " << error
            << " at index " << j;
      }
Daniel Kang's avatar
Daniel Kang committed
587 588
    }
  }
589 590 591 592 593 594

  void CompareInvReference(IdctFunc ref_txfm, int thresh) {
    ACMRandom rnd(ACMRandom::DeterministicSeed());
    const int count_test_block = 10000;
    const int eob = 10;
    const int16_t *scan = vp9_default_scan_orders[TX_16X16].scan;
595 596 597
    DECLARE_ALIGNED(16, tran_low_t, coeff[kNumCoeffs]);
    DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
    DECLARE_ALIGNED(16, uint8_t, ref[kNumCoeffs]);
598
#if CONFIG_VP9_HIGHBITDEPTH
599 600
    DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
    DECLARE_ALIGNED(16, uint16_t, ref16[kNumCoeffs]);
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
#endif  // CONFIG_VP9_HIGHBITDEPTH

    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;
#if CONFIG_VP9_HIGHBITDEPTH
        } else {
          dst16[j] = 0;
          ref16[j] = 0;
#endif  // CONFIG_VP9_HIGHBITDEPTH
        }
      }
      if (bit_depth_ == VPX_BITS_8) {
        ref_txfm(coeff, ref, pitch_);
        ASM_REGISTER_STATE_CHECK(RunInvTxfm(coeff, dst, pitch_));
      } else {
#if CONFIG_VP9_HIGHBITDEPTH
        ref_txfm(coeff, CONVERT_TO_BYTEPTR(ref16), pitch_);
        ASM_REGISTER_STATE_CHECK(RunInvTxfm(coeff, CONVERT_TO_BYTEPTR(dst16),
                                 pitch_));
#endif  // CONFIG_VP9_HIGHBITDEPTH
      }

      for (int j = 0; j < kNumCoeffs; ++j) {
#if CONFIG_VP9_HIGHBITDEPTH
        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];
#endif  // CONFIG_VP9_HIGHBITDEPTH
        const uint32_t error = diff * diff;
        EXPECT_EQ(0u, error)
            << "Error: 16x16 IDCT Comparison has error " << error
            << " at index " << j;
      }
    }
  }

647 648
  int pitch_;
  int tx_type_;
649 650
  vpx_bit_depth_t bit_depth_;
  int mask_;
651 652
  FhtFunc fwd_txfm_ref;
  IhtFunc inv_txfm_ref;
653
};
Daniel Kang's avatar
Daniel Kang committed
654

655 656
class Trans16x16DCT
    : public Trans16x16TestBase,
657
      public ::testing::TestWithParam<Dct16x16Param> {
658 659
 public:
  virtual ~Trans16x16DCT() {}
Daniel Kang's avatar
Daniel Kang committed
660

661 662 663 664
  virtual void SetUp() {
    fwd_txfm_ = GET_PARAM(0);
    inv_txfm_ = GET_PARAM(1);
    tx_type_  = GET_PARAM(2);
665
    bit_depth_ = GET_PARAM(3);
666
    pitch_    = 16;
667
    fwd_txfm_ref = fdct16x16_ref;
668
    inv_txfm_ref = idct16x16_ref;
669 670 671
    mask_ = (1 << bit_depth_) - 1;
#if CONFIG_VP9_HIGHBITDEPTH
    switch (bit_depth_) {
672
      case VPX_BITS_10:
673 674
        inv_txfm_ref = idct16x16_10_ref;
        break;
675
      case VPX_BITS_12:
676 677 678 679 680 681 682 683 684
        inv_txfm_ref = idct16x16_12_ref;
        break;
      default:
        inv_txfm_ref = idct16x16_ref;
        break;
    }
#else
    inv_txfm_ref = idct16x16_ref;
#endif
685 686
  }
  virtual void TearDown() { libvpx_test::ClearSystemState(); }
Daniel Kang's avatar
Daniel Kang committed
687

688
 protected:
689
  void RunFwdTxfm(int16_t *in, tran_low_t *out, int stride) {
690 691
    fwd_txfm_(in, out, stride);
  }
692
  void RunInvTxfm(tran_low_t *out, uint8_t *dst, int stride) {
693
    inv_txfm_(out, dst, stride);
Daniel Kang's avatar
Daniel Kang committed
694
  }
695

696 697
  FdctFunc fwd_txfm_;
  IdctFunc inv_txfm_;
698 699 700 701
};

TEST_P(Trans16x16DCT, AccuracyCheck) {
  RunAccuracyCheck();
Daniel Kang's avatar
Daniel Kang committed
702
}
703

704 705 706 707 708 709
TEST_P(Trans16x16DCT, CoeffCheck) {
  RunCoeffCheck();
}

TEST_P(Trans16x16DCT, MemCheck) {
  RunMemCheck();
710 711
}

712 713 714 715 716 717
TEST_P(Trans16x16DCT, QuantCheck) {
  // Use maximally allowed quantization step sizes for DC and AC
  // coefficients respectively.
  RunQuantCheck(1336, 1828);
}

718 719 720 721
TEST_P(Trans16x16DCT, InvAccuracyCheck) {
  RunInvAccuracyCheck();
}

722 723
class Trans16x16HT
    : public Trans16x16TestBase,
724
      public ::testing::TestWithParam<Ht16x16Param> {
725 726 727 728 729 730 731
 public:
  virtual ~Trans16x16HT() {}

  virtual void SetUp() {
    fwd_txfm_ = GET_PARAM(0);
    inv_txfm_ = GET_PARAM(1);
    tx_type_  = GET_PARAM(2);
732
    bit_depth_ = GET_PARAM(3);
733 734
    pitch_    = 16;
    fwd_txfm_ref = fht16x16_ref;
735
    inv_txfm_ref = iht16x16_ref;
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
    mask_ = (1 << bit_depth_) - 1;
#if CONFIG_VP9_HIGHBITDEPTH
    switch (bit_depth_) {
      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;
    }
#else
    inv_txfm_ref = iht16x16_ref;
#endif
752
  }
753 754 755
  virtual void TearDown() { libvpx_test::ClearSystemState(); }

 protected:
756
  void RunFwdTxfm(int16_t *in, tran_low_t *out, int stride) {
757
    fwd_txfm_(in, out, stride, tx_type_);
758
  }
759
  void RunInvTxfm(tran_low_t *out, uint8_t *dst, int stride) {
760
    inv_txfm_(out, dst, stride, tx_type_);
761 762
  }

763 764
  FhtFunc fwd_txfm_;
  IhtFunc inv_txfm_;
765 766 767 768 769 770
};

TEST_P(Trans16x16HT, AccuracyCheck) {
  RunAccuracyCheck();
}

771 772 773 774 775 776
TEST_P(Trans16x16HT, CoeffCheck) {
  RunCoeffCheck();
}

TEST_P(Trans16x16HT, MemCheck) {
  RunMemCheck();
777 778
}

779 780 781
TEST_P(Trans16x16HT, QuantCheck) {
  // The encoder skips any non-DC intra prediction modes,
  // when the quantization step size goes beyond 988.
782
  RunQuantCheck(429, 729);
783 784
}

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
class InvTrans16x16DCT
    : public Trans16x16TestBase,
      public ::testing::TestWithParam<Idct16x16Param> {
 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;
}
  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_);
}

816 817
using std::tr1::make_tuple;

818 819 820 821
#if CONFIG_VP9_HIGHBITDEPTH
INSTANTIATE_TEST_CASE_P(
    C, Trans16x16DCT,
    ::testing::Values(
822 823
        make_tuple(&vp9_highbd_fdct16x16_c, &idct16x16_10, 0, VPX_BITS_10),
        make_tuple(&vp9_highbd_fdct16x16_c, &idct16x16_12, 0, VPX_BITS_12),
824 825
        make_tuple(&vp9_fdct16x16_c, &vp9_idct16x16_256_add_c, 0, VPX_BITS_8)));
#else
826 827 828
INSTANTIATE_TEST_CASE_P(
    C, Trans16x16DCT,
    ::testing::Values(
829
        make_tuple(&vp9_fdct16x16_c, &vp9_idct16x16_256_add_c, 0, VPX_BITS_8)));
830
#endif  // CONFIG_VP9_HIGHBITDEPTH
831 832

#if CONFIG_VP9_HIGHBITDEPTH
833 834 835
INSTANTIATE_TEST_CASE_P(
    C, Trans16x16HT,
    ::testing::Values(
836 837 838 839 840 841 842 843
        make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_10, 0, VPX_BITS_10),
        make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_10, 1, VPX_BITS_10),
        make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_10, 2, VPX_BITS_10),
        make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_10, 3, VPX_BITS_10),
        make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_12, 0, VPX_BITS_12),
        make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_12, 1, VPX_BITS_12),
        make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_12, 2, VPX_BITS_12),
        make_tuple(&vp9_highbd_fht16x16_c, &iht16x16_12, 3, VPX_BITS_12),
844 845 846 847 848 849 850 851 852 853 854 855
        make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 0, VPX_BITS_8),
        make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 1, VPX_BITS_8),
        make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 2, VPX_BITS_8),
        make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 3, VPX_BITS_8)));
#else
INSTANTIATE_TEST_CASE_P(
    C, Trans16x16HT,
    ::testing::Values(
        make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 0, VPX_BITS_8),
        make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 1, VPX_BITS_8),
        make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 2, VPX_BITS_8),
        make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_c, 3, VPX_BITS_8)));
856
#endif  // CONFIG_VP9_HIGHBITDEPTH
857

858
#if HAVE_NEON_ASM && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
James Zern's avatar
James Zern committed
859 860 861 862
INSTANTIATE_TEST_CASE_P(
    NEON, Trans16x16DCT,
    ::testing::Values(
        make_tuple(&vp9_fdct16x16_c,
863
                   &vp9_idct16x16_256_add_neon, 0, VPX_BITS_8)));
James Zern's avatar
James Zern committed
864 865
#endif

866
#if HAVE_SSE2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
867 868 869
INSTANTIATE_TEST_CASE_P(
    SSE2, Trans16x16DCT,
    ::testing::Values(
870
        make_tuple(&vp9_fdct16x16_sse2,
871
                   &vp9_idct16x16_256_add_sse2, 0, VPX_BITS_8)));
872 873 874
INSTANTIATE_TEST_CASE_P(
    SSE2, Trans16x16HT,
    ::testing::Values(
875 876 877 878 879 880 881 882
        make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 0,
                   VPX_BITS_8),
        make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 1,
                   VPX_BITS_8),
        make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 2,
                   VPX_BITS_8),
        make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 3,
                   VPX_BITS_8)));
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
#endif  // HAVE_SSE2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE

#if HAVE_SSE2 && CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
INSTANTIATE_TEST_CASE_P(
    SSE2, Trans16x16DCT,
    ::testing::Values(
        make_tuple(&vp9_highbd_fdct16x16_sse2,
                   &idct16x16_10, 0, VPX_BITS_10),
        make_tuple(&vp9_highbd_fdct16x16_c,
                   &idct16x16_256_add_10_sse2, 0, VPX_BITS_10),
        make_tuple(&vp9_highbd_fdct16x16_sse2,
                   &idct16x16_12, 0, VPX_BITS_12),
        make_tuple(&vp9_highbd_fdct16x16_c,
                   &idct16x16_256_add_12_sse2, 0, VPX_BITS_12),
        make_tuple(&vp9_fdct16x16_sse2,
                   &vp9_idct16x16_256_add_c, 0, VPX_BITS_8)));
INSTANTIATE_TEST_CASE_P(
    SSE2, Trans16x16HT,
    ::testing::Values(
        make_tuple(&vp9_highbd_fht16x16_sse2, &iht16x16_10, 0, VPX_BITS_10),
        make_tuple(&vp9_highbd_fht16x16_sse2, &iht16x16_10, 1, VPX_BITS_10),
        make_tuple(&vp9_highbd_fht16x16_sse2, &iht16x16_10, 2, VPX_BITS_10),
        make_tuple(&vp9_highbd_fht16x16_sse2, &iht16x16_10, 3, VPX_BITS_10),
        make_tuple(&vp9_highbd_fht16x16_sse2, &iht16x16_12, 0, VPX_BITS_12),
        make_tuple(&vp9_highbd_fht16x16_sse2, &iht16x16_12, 1, VPX_BITS_12),
        make_tuple(&vp9_highbd_fht16x16_sse2, &iht16x16_12, 2, VPX_BITS_12),
        make_tuple(&vp9_highbd_fht16x16_sse2, &iht16x16_12, 3, VPX_BITS_12),
        make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_c, 0, VPX_BITS_8),
        make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_c, 1, VPX_BITS_8),
        make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_c, 2, VPX_BITS_8),
        make_tuple(&vp9_fht16x16_sse2, &vp9_iht16x16_256_add_c, 3,
                   VPX_BITS_8)));
// 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,
    ::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)));
#endif  // HAVE_SSE2 && CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943

#if HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
INSTANTIATE_TEST_CASE_P(
    MSA, Trans16x16DCT,
    ::testing::Values(
        make_tuple(&vp9_fdct16x16_c,
                   &vp9_idct16x16_256_add_msa, 0, VPX_BITS_8)));
INSTANTIATE_TEST_CASE_P(
    MSA, Trans16x16HT,
    ::testing::Values(
        make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_msa, 0, VPX_BITS_8),
        make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_msa, 1, VPX_BITS_8),
        make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_msa, 2, VPX_BITS_8),
        make_tuple(&vp9_fht16x16_c, &vp9_iht16x16_256_add_msa, 3, VPX_BITS_8)));
#endif  // HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
Daniel Kang's avatar
Daniel Kang committed
944
}  // namespace