dct32x32_test.cc 13.2 KB
Newer Older
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"
20

21
#include "./vpx_config.h"
22
#include "./vp9_rtcd.h"
23
#include "vp9/common/vp9_entropy.h"
24
#include "vpx/vpx_codec.h"
25
#include "vpx/vpx_integer.h"
26
#include "vpx_ports/mem.h"
27 28 29 30

using libvpx_test::ACMRandom;

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

40 41
const int kNumCoeffs = 1024;
const double kPi = 3.141592653589793238462643383279502884;
42
void reference_32x32_dct_1d(const double in[32], double out[32]) {
43 44 45 46 47 48 49 50 51 52
  const double kInvSqrt2 = 0.707106781186547524400844362104;
  for (int k = 0; k < 32; k++) {
    out[k] = 0.0;
    for (int n = 0; n < 32; n++)
      out[k] += in[n] * cos(kPi * (2 * n + 1) * k / 64.0);
    if (k == 0)
      out[k] = out[k] * kInvSqrt2;
  }
}

53 54
void reference_32x32_dct_2d(const int16_t input[kNumCoeffs],
                            double output[kNumCoeffs]) {
55 56 57 58 59
  // First transform columns
  for (int i = 0; i < 32; ++i) {
    double temp_in[32], temp_out[32];
    for (int j = 0; j < 32; ++j)
      temp_in[j] = input[j*32 + i];
60
    reference_32x32_dct_1d(temp_in, temp_out);
61 62 63 64 65 66 67 68
    for (int j = 0; j < 32; ++j)
      output[j * 32 + i] = temp_out[j];
  }
  // Then transform rows
  for (int i = 0; i < 32; ++i) {
    double temp_in[32], temp_out[32];
    for (int j = 0; j < 32; ++j)
      temp_in[j] = output[j + i*32];
69
    reference_32x32_dct_1d(temp_in, temp_out);
70 71 72 73 74 75
    // Scale by some magic number
    for (int j = 0; j < 32; ++j)
      output[j + i * 32] = temp_out[j] / 4;
  }
}

76 77
typedef void (*FwdTxfmFunc)(const int16_t *in, tran_low_t *out, int stride);
typedef void (*InvTxfmFunc)(const tran_low_t *in, uint8_t *out, int stride);
78

79 80 81 82
typedef std::tr1::tuple<FwdTxfmFunc, InvTxfmFunc, int, vpx_bit_depth_t>
    Trans32x32Param;

#if CONFIG_VP9_HIGHBITDEPTH
83 84 85 86
void idct32x32_8(const tran_low_t *in, uint8_t *out, int stride) {
  vp9_highbd_idct32x32_1024_add_c(in, out, stride, 8);
}

87
void idct32x32_10(const tran_low_t *in, uint8_t *out, int stride) {
88
  vp9_highbd_idct32x32_1024_add_c(in, out, stride, 10);
89 90 91
}

void idct32x32_12(const tran_low_t *in, uint8_t *out, int stride) {
92
  vp9_highbd_idct32x32_1024_add_c(in, out, stride, 12);
93
}
94
#endif  // CONFIG_VP9_HIGHBITDEPTH
95

96
class Trans32x32Test : public ::testing::TestWithParam<Trans32x32Param> {
97 98 99 100 101 102 103
 public:
  virtual ~Trans32x32Test() {}
  virtual void SetUp() {
    fwd_txfm_ = GET_PARAM(0);
    inv_txfm_ = GET_PARAM(1);
    version_  = GET_PARAM(2);  // 0: high precision forward transform
                               // 1: low precision version for rd loop
104 105
    bit_depth_ = GET_PARAM(3);
    mask_ = (1 << bit_depth_) - 1;
106
  }
107

108 109 110 111
  virtual void TearDown() { libvpx_test::ClearSystemState(); }

 protected:
  int version_;
112 113
  vpx_bit_depth_t bit_depth_;
  int mask_;
114 115
  FwdTxfmFunc fwd_txfm_;
  InvTxfmFunc inv_txfm_;
116 117 118
};

TEST_P(Trans32x32Test, AccuracyCheck) {
119
  ACMRandom rnd(ACMRandom::DeterministicSeed());
120
  uint32_t max_error = 0;
121
  int64_t total_error = 0;
122
  const int count_test_block = 10000;
123 124 125 126
  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]);
127
#if CONFIG_VP9_HIGHBITDEPTH
128 129
  DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
  DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
130
#endif
131

132
  for (int i = 0; i < count_test_block; ++i) {
133
    // Initialize a test block with input range [-mask_, mask_].
134
    for (int j = 0; j < kNumCoeffs; ++j) {
135
      if (bit_depth_ == VPX_BITS_8) {
136 137 138 139 140 141 142 143 144 145
        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
      }
146
    }
147

148
    ASM_REGISTER_STATE_CHECK(fwd_txfm_(test_input_block, test_temp_block, 32));
149 150 151 152 153 154 155 156
    if (bit_depth_ == VPX_BITS_8) {
      ASM_REGISTER_STATE_CHECK(inv_txfm_(test_temp_block, dst, 32));
#if CONFIG_VP9_HIGHBITDEPTH
    } else {
      ASM_REGISTER_STATE_CHECK(inv_txfm_(test_temp_block,
                                         CONVERT_TO_BYTEPTR(dst16), 32));
#endif
    }
157

158
    for (int j = 0; j < kNumCoeffs; ++j) {
159 160 161 162
#if CONFIG_VP9_HIGHBITDEPTH
      const uint32_t diff =
          bit_depth_ == VPX_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
#else
163
      const uint32_t diff = dst[j] - src[j];
164
#endif
165
      const uint32_t error = diff * diff;
166 167 168 169 170 171
      if (max_error < error)
        max_error = error;
      total_error += error;
    }
  }

172 173 174 175 176
  if (version_ == 1) {
    max_error /= 2;
    total_error /= 45;
  }

177
  EXPECT_GE(1u << 2 * (bit_depth_ - 8), max_error)
178
      << "Error: 32x32 FDCT/IDCT has an individual round-trip error > 1";
179

180
  EXPECT_GE(count_test_block << 2 * (bit_depth_ - 8), total_error)
181
      << "Error: 32x32 FDCT/IDCT has average round-trip error > 1 per block";
182 183
}

184
TEST_P(Trans32x32Test, CoeffCheck) {
185 186
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  const int count_test_block = 1000;
187

188 189 190
  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]);
191

192
  for (int i = 0; i < count_test_block; ++i) {
193
    for (int j = 0; j < kNumCoeffs; ++j)
194
      input_block[j] = (rnd.Rand16() & mask_) - (rnd.Rand16() & mask_);
195

196
    const int stride = 32;
197
    vp9_fdct32x32_c(input_block, output_ref_block, stride);
198
    ASM_REGISTER_STATE_CHECK(fwd_txfm_(input_block, output_block, stride));
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

    if (version_ == 0) {
      for (int j = 0; j < kNumCoeffs; ++j)
        EXPECT_EQ(output_block[j], output_ref_block[j])
            << "Error: 32x32 FDCT versions have mismatched coefficients";
    } else {
      for (int j = 0; j < kNumCoeffs; ++j)
        EXPECT_GE(6, abs(output_block[j] - output_ref_block[j]))
            << "Error: 32x32 FDCT rd has mismatched coefficients";
    }
  }
}

TEST_P(Trans32x32Test, MemCheck) {
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  const int count_test_block = 2000;

216 217 218
  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]);
219

220
  for (int i = 0; i < count_test_block; ++i) {
221
    // Initialize a test block with input range [-mask_, mask_].
222
    for (int j = 0; j < kNumCoeffs; ++j) {
223
      input_extreme_block[j] = rnd.Rand8() & 1 ? mask_ : -mask_;
224
    }
225
    if (i == 0) {
226
      for (int j = 0; j < kNumCoeffs; ++j)
227
        input_extreme_block[j] = mask_;
228
    } else if (i == 1) {
229
      for (int j = 0; j < kNumCoeffs; ++j)
230
        input_extreme_block[j] = -mask_;
231
    }
232

233
    const int stride = 32;
234
    vp9_fdct32x32_c(input_extreme_block, output_ref_block, stride);
235 236
    ASM_REGISTER_STATE_CHECK(
        fwd_txfm_(input_extreme_block, output_block, stride));
237 238

    // The minimum quant value is 4.
239 240 241 242 243 244 245 246
    for (int j = 0; j < kNumCoeffs; ++j) {
      if (version_ == 0) {
        EXPECT_EQ(output_block[j], output_ref_block[j])
            << "Error: 32x32 FDCT versions have mismatched coefficients";
      } else {
        EXPECT_GE(6, abs(output_block[j] - output_ref_block[j]))
            << "Error: 32x32 FDCT rd has mismatched coefficients";
      }
247
      EXPECT_GE(4 * DCT_MAX_VALUE << (bit_depth_ - 8), abs(output_ref_block[j]))
248
          << "Error: 32x32 FDCT C has coefficient larger than 4*DCT_MAX_VALUE";
249
      EXPECT_GE(4 * DCT_MAX_VALUE << (bit_depth_ - 8), abs(output_block[j]))
250 251
          << "Error: 32x32 FDCT has coefficient larger than "
          << "4*DCT_MAX_VALUE";
252 253 254
    }
  }
}
255 256 257 258

TEST_P(Trans32x32Test, InverseAccuracy) {
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  const int count_test_block = 1000;
259 260 261 262
  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]);
263
#if CONFIG_VP9_HIGHBITDEPTH
264 265
  DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
  DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
266
#endif
267 268 269 270 271 272

  for (int i = 0; i < count_test_block; ++i) {
    double out_r[kNumCoeffs];

    // Initialize a test block with input range [-255, 255]
    for (int j = 0; j < kNumCoeffs; ++j) {
273 274 275 276 277 278 279 280 281 282 283
      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];
#endif
      }
284 285 286 287
    }

    reference_32x32_dct_2d(in, out_r);
    for (int j = 0; j < kNumCoeffs; ++j)
288
      coeff[j] = static_cast<tran_low_t>(round(out_r[j]));
289 290 291 292 293 294 295
    if (bit_depth_ == VPX_BITS_8) {
      ASM_REGISTER_STATE_CHECK(inv_txfm_(coeff, dst, 32));
#if CONFIG_VP9_HIGHBITDEPTH
    } else {
      ASM_REGISTER_STATE_CHECK(inv_txfm_(coeff, CONVERT_TO_BYTEPTR(dst16), 32));
#endif
    }
296
    for (int j = 0; j < kNumCoeffs; ++j) {
297 298 299 300
#if CONFIG_VP9_HIGHBITDEPTH
      const int diff =
          bit_depth_ == VPX_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
#else
301
      const int diff = dst[j] - src[j];
302
#endif
303 304 305 306 307 308 309 310 311 312
      const int error = diff * diff;
      EXPECT_GE(1, error)
          << "Error: 32x32 IDCT has error " << error
          << " at index " << j;
    }
  }
}

using std::tr1::make_tuple;

313 314 315 316
#if CONFIG_VP9_HIGHBITDEPTH
INSTANTIATE_TEST_CASE_P(
    C, Trans32x32Test,
    ::testing::Values(
317
        make_tuple(&vp9_highbd_fdct32x32_c,
318
                   &idct32x32_10, 0, VPX_BITS_10),
319
        make_tuple(&vp9_highbd_fdct32x32_rd_c,
320
                   &idct32x32_10, 1, VPX_BITS_10),
321
        make_tuple(&vp9_highbd_fdct32x32_c,
322
                   &idct32x32_12, 0, VPX_BITS_12),
323
        make_tuple(&vp9_highbd_fdct32x32_rd_c,
324 325 326 327 328 329
                   &idct32x32_12, 1, VPX_BITS_12),
        make_tuple(&vp9_fdct32x32_c,
                   &vp9_idct32x32_1024_add_c, 0, VPX_BITS_8),
        make_tuple(&vp9_fdct32x32_rd_c,
                   &vp9_idct32x32_1024_add_c, 1, VPX_BITS_8)));
#else
330 331 332
INSTANTIATE_TEST_CASE_P(
    C, Trans32x32Test,
    ::testing::Values(
333 334 335 336
        make_tuple(&vp9_fdct32x32_c,
                   &vp9_idct32x32_1024_add_c, 0, VPX_BITS_8),
        make_tuple(&vp9_fdct32x32_rd_c,
                   &vp9_idct32x32_1024_add_c, 1, VPX_BITS_8)));
337
#endif  // CONFIG_VP9_HIGHBITDEPTH
338

339
#if HAVE_NEON_ASM && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
340 341 342 343
INSTANTIATE_TEST_CASE_P(
    NEON, Trans32x32Test,
    ::testing::Values(
        make_tuple(&vp9_fdct32x32_c,
344
                   &vp9_idct32x32_1024_add_neon, 0, VPX_BITS_8),
345
        make_tuple(&vp9_fdct32x32_rd_c,
346
                   &vp9_idct32x32_1024_add_neon, 1, VPX_BITS_8)));
347
#endif  // HAVE_NEON_ASM && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
348

349
#if HAVE_SSE2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
350 351 352
INSTANTIATE_TEST_CASE_P(
    SSE2, Trans32x32Test,
    ::testing::Values(
353
        make_tuple(&vp9_fdct32x32_sse2,
354
                   &vp9_idct32x32_1024_add_sse2, 0, VPX_BITS_8),
355
        make_tuple(&vp9_fdct32x32_rd_sse2,
356
                   &vp9_idct32x32_1024_add_sse2, 1, VPX_BITS_8)));
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
#endif  // HAVE_SSE2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE

#if HAVE_SSE2 && CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
INSTANTIATE_TEST_CASE_P(
    SSE2, Trans32x32Test,
    ::testing::Values(
        make_tuple(&vp9_highbd_fdct32x32_sse2, &idct32x32_10, 0, VPX_BITS_10),
        make_tuple(&vp9_highbd_fdct32x32_rd_sse2, &idct32x32_10, 1,
                   VPX_BITS_10),
        make_tuple(&vp9_highbd_fdct32x32_sse2, &idct32x32_12, 0, VPX_BITS_12),
        make_tuple(&vp9_highbd_fdct32x32_rd_sse2, &idct32x32_12, 1,
                   VPX_BITS_12),
        make_tuple(&vp9_fdct32x32_sse2, &vp9_idct32x32_1024_add_c, 0,
                   VPX_BITS_8),
        make_tuple(&vp9_fdct32x32_rd_sse2, &vp9_idct32x32_1024_add_c, 1,
                   VPX_BITS_8)));
#endif  // HAVE_SSE2 && CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
374

375
#if HAVE_AVX2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
376 377 378 379
INSTANTIATE_TEST_CASE_P(
    AVX2, Trans32x32Test,
    ::testing::Values(
        make_tuple(&vp9_fdct32x32_avx2,
380
                   &vp9_idct32x32_1024_add_sse2, 0, VPX_BITS_8),
381
        make_tuple(&vp9_fdct32x32_rd_avx2,
382
                   &vp9_idct32x32_1024_add_sse2, 1, VPX_BITS_8)));
383
#endif  // HAVE_AVX2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
384

385
#if HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
386 387 388 389 390 391
INSTANTIATE_TEST_CASE_P(
    MSA, Trans32x32Test,
    ::testing::Values(
        make_tuple(&vp9_fdct32x32_c,
                   &vp9_idct32x32_1024_add_msa, 0, VPX_BITS_8)));
#endif  // HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
392
}  // namespace