dct32x32_test.cc 13.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  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>

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

#include "./vp9_rtcd.h"
18 19
#include "./vpx_config.h"
#include "./vpx_dsp_rtcd.h"
20 21 22 23
#include "test/acm_random.h"
#include "test/clear_system_state.h"
#include "test/register_state_check.h"
#include "test/util.h"
24
#include "vp9/common/vp9_entropy.h"
25
#include "vpx/vpx_codec.h"
26
#include "vpx/vpx_integer.h"
27
#include "vpx_ports/mem.h"
28
#include "vpx_ports/msvc.h"  // for round()
29 30 31 32 33

using libvpx_test::ACMRandom;

namespace {

34 35
const int kNumCoeffs = 1024;
const double kPi = 3.141592653589793238462643383279502884;
36
void reference_32x32_dct_1d(const double in[32], double out[32]) {
37 38 39
  const double kInvSqrt2 = 0.707106781186547524400844362104;
  for (int k = 0; k < 32; k++) {
    out[k] = 0.0;
40
    for (int n = 0; n < 32; n++) {
41
      out[k] += in[n] * cos(kPi * (2 * n + 1) * k / 64.0);
42
    }
clang-format's avatar
clang-format committed
43
    if (k == 0) out[k] = out[k] * kInvSqrt2;
44 45 46
  }
}

47 48
void reference_32x32_dct_2d(const int16_t input[kNumCoeffs],
                            double output[kNumCoeffs]) {
49 50 51
  // First transform columns
  for (int i = 0; i < 32; ++i) {
    double temp_in[32], temp_out[32];
clang-format's avatar
clang-format committed
52
    for (int j = 0; j < 32; ++j) temp_in[j] = input[j * 32 + i];
53
    reference_32x32_dct_1d(temp_in, temp_out);
clang-format's avatar
clang-format committed
54
    for (int j = 0; j < 32; ++j) output[j * 32 + i] = temp_out[j];
55 56 57 58
  }
  // Then transform rows
  for (int i = 0; i < 32; ++i) {
    double temp_in[32], temp_out[32];
clang-format's avatar
clang-format committed
59
    for (int j = 0; j < 32; ++j) temp_in[j] = output[j + i * 32];
60
    reference_32x32_dct_1d(temp_in, temp_out);
61
    // Scale by some magic number
clang-format's avatar
clang-format committed
62
    for (int j = 0; j < 32; ++j) output[j + i * 32] = temp_out[j] / 4;
63 64 65
  }
}

66 67
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);
68

69 70 71 72 73
typedef std::tr1::tuple<FwdTxfmFunc, InvTxfmFunc, int, vpx_bit_depth_t>
    Trans32x32Param;

#if CONFIG_VP9_HIGHBITDEPTH
void idct32x32_10(const tran_low_t *in, uint8_t *out, int stride) {
74
  vpx_highbd_idct32x32_1024_add_c(in, CAST_TO_SHORTPTR(out), stride, 10);
75 76 77
}

void idct32x32_12(const tran_low_t *in, uint8_t *out, int stride) {
78
  vpx_highbd_idct32x32_1024_add_c(in, CAST_TO_SHORTPTR(out), stride, 12);
79
}
80
#endif  // CONFIG_VP9_HIGHBITDEPTH
81

82
class Trans32x32Test : public ::testing::TestWithParam<Trans32x32Param> {
83 84 85 86 87
 public:
  virtual ~Trans32x32Test() {}
  virtual void SetUp() {
    fwd_txfm_ = GET_PARAM(0);
    inv_txfm_ = GET_PARAM(1);
clang-format's avatar
clang-format committed
88 89
    version_ = GET_PARAM(2);  // 0: high precision forward transform
                              // 1: low precision version for rd loop
90 91
    bit_depth_ = GET_PARAM(3);
    mask_ = (1 << bit_depth_) - 1;
92
  }
93

94 95 96 97
  virtual void TearDown() { libvpx_test::ClearSystemState(); }

 protected:
  int version_;
98 99
  vpx_bit_depth_t bit_depth_;
  int mask_;
100 101
  FwdTxfmFunc fwd_txfm_;
  InvTxfmFunc inv_txfm_;
102 103 104
};

TEST_P(Trans32x32Test, AccuracyCheck) {
105
  ACMRandom rnd(ACMRandom::DeterministicSeed());
106
  uint32_t max_error = 0;
107
  int64_t total_error = 0;
108
  const int count_test_block = 10000;
109 110 111 112
  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]);
113
#if CONFIG_VP9_HIGHBITDEPTH
114 115
  DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
  DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
116
#endif
117

118
  for (int i = 0; i < count_test_block; ++i) {
119
    // Initialize a test block with input range [-mask_, mask_].
120
    for (int j = 0; j < kNumCoeffs; ++j) {
121
      if (bit_depth_ == VPX_BITS_8) {
122 123 124 125 126 127 128 129 130 131
        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
      }
132
    }
133

134
    ASM_REGISTER_STATE_CHECK(fwd_txfm_(test_input_block, test_temp_block, 32));
135 136 137 138
    if (bit_depth_ == VPX_BITS_8) {
      ASM_REGISTER_STATE_CHECK(inv_txfm_(test_temp_block, dst, 32));
#if CONFIG_VP9_HIGHBITDEPTH
    } else {
clang-format's avatar
clang-format committed
139
      ASM_REGISTER_STATE_CHECK(
140
          inv_txfm_(test_temp_block, CAST_TO_BYTEPTR(dst16), 32));
141 142
#endif
    }
143

144
    for (int j = 0; j < kNumCoeffs; ++j) {
145
#if CONFIG_VP9_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
146
      const int32_t diff =
147 148
          bit_depth_ == VPX_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
#else
Yaowu Xu's avatar
Yaowu Xu committed
149
      const int32_t diff = dst[j] - src[j];
150
#endif
151
      const uint32_t error = diff * diff;
clang-format's avatar
clang-format committed
152
      if (max_error < error) max_error = error;
153 154 155 156
      total_error += error;
    }
  }

157 158 159 160 161
  if (version_ == 1) {
    max_error /= 2;
    total_error /= 45;
  }

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

165
  EXPECT_GE(count_test_block << 2 * (bit_depth_ - 8), total_error)
166
      << "Error: 32x32 FDCT/IDCT has average round-trip error > 1 per block";
167 168
}

169
TEST_P(Trans32x32Test, CoeffCheck) {
170 171
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  const int count_test_block = 1000;
172

173 174 175
  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]);
176

177
  for (int i = 0; i < count_test_block; ++i) {
178
    for (int j = 0; j < kNumCoeffs; ++j) {
179
      input_block[j] = (rnd.Rand16() & mask_) - (rnd.Rand16() & mask_);
180
    }
181

182
    const int stride = 32;
183
    vpx_fdct32x32_c(input_block, output_ref_block, stride);
184
    ASM_REGISTER_STATE_CHECK(fwd_txfm_(input_block, output_block, stride));
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

    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;

202 203 204
  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]);
205

206
  for (int i = 0; i < count_test_block; ++i) {
207
    // Initialize a test block with input range [-mask_, mask_].
208
    for (int j = 0; j < kNumCoeffs; ++j) {
209
      input_extreme_block[j] = rnd.Rand8() & 1 ? mask_ : -mask_;
210
    }
211
    if (i == 0) {
clang-format's avatar
clang-format committed
212
      for (int j = 0; j < kNumCoeffs; ++j) input_extreme_block[j] = mask_;
213
    } else if (i == 1) {
clang-format's avatar
clang-format committed
214
      for (int j = 0; j < kNumCoeffs; ++j) input_extreme_block[j] = -mask_;
215
    }
216

217
    const int stride = 32;
218
    vpx_fdct32x32_c(input_extreme_block, output_ref_block, stride);
219 220
    ASM_REGISTER_STATE_CHECK(
        fwd_txfm_(input_extreme_block, output_block, stride));
221 222

    // The minimum quant value is 4.
223 224 225 226 227 228 229 230
    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";
      }
231
      EXPECT_GE(4 * DCT_MAX_VALUE << (bit_depth_ - 8), abs(output_ref_block[j]))
232
          << "Error: 32x32 FDCT C has coefficient larger than 4*DCT_MAX_VALUE";
233
      EXPECT_GE(4 * DCT_MAX_VALUE << (bit_depth_ - 8), abs(output_block[j]))
234 235
          << "Error: 32x32 FDCT has coefficient larger than "
          << "4*DCT_MAX_VALUE";
236 237 238
    }
  }
}
239 240 241 242

TEST_P(Trans32x32Test, InverseAccuracy) {
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  const int count_test_block = 1000;
243 244 245 246
  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]);
247
#if CONFIG_VP9_HIGHBITDEPTH
248 249
  DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
  DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
250
#endif
251 252 253 254 255 256

  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) {
257 258 259 260 261 262 263 264 265 266 267
      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
      }
268 269 270
    }

    reference_32x32_dct_2d(in, out_r);
271
    for (int j = 0; j < kNumCoeffs; ++j) {
272
      coeff[j] = static_cast<tran_low_t>(round(out_r[j]));
273
    }
274 275 276 277
    if (bit_depth_ == VPX_BITS_8) {
      ASM_REGISTER_STATE_CHECK(inv_txfm_(coeff, dst, 32));
#if CONFIG_VP9_HIGHBITDEPTH
    } else {
278
      ASM_REGISTER_STATE_CHECK(inv_txfm_(coeff, CAST_TO_BYTEPTR(dst16), 32));
279 280
#endif
    }
281
    for (int j = 0; j < kNumCoeffs; ++j) {
282 283 284 285
#if CONFIG_VP9_HIGHBITDEPTH
      const int diff =
          bit_depth_ == VPX_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
#else
286
      const int diff = dst[j] - src[j];
287
#endif
288
      const int error = diff * diff;
clang-format's avatar
clang-format committed
289 290
      EXPECT_GE(1, error) << "Error: 32x32 IDCT has error " << error
                          << " at index " << j;
291 292 293 294 295 296
    }
  }
}

using std::tr1::make_tuple;

297 298 299 300
#if CONFIG_VP9_HIGHBITDEPTH
INSTANTIATE_TEST_CASE_P(
    C, Trans32x32Test,
    ::testing::Values(
clang-format's avatar
clang-format committed
301 302 303 304 305 306 307
        make_tuple(&vpx_highbd_fdct32x32_c, &idct32x32_10, 0, VPX_BITS_10),
        make_tuple(&vpx_highbd_fdct32x32_rd_c, &idct32x32_10, 1, VPX_BITS_10),
        make_tuple(&vpx_highbd_fdct32x32_c, &idct32x32_12, 0, VPX_BITS_12),
        make_tuple(&vpx_highbd_fdct32x32_rd_c, &idct32x32_12, 1, VPX_BITS_12),
        make_tuple(&vpx_fdct32x32_c, &vpx_idct32x32_1024_add_c, 0, VPX_BITS_8),
        make_tuple(&vpx_fdct32x32_rd_c, &vpx_idct32x32_1024_add_c, 1,
                   VPX_BITS_8)));
308
#else
309 310
INSTANTIATE_TEST_CASE_P(
    C, Trans32x32Test,
clang-format's avatar
clang-format committed
311 312 313 314
    ::testing::Values(make_tuple(&vpx_fdct32x32_c, &vpx_idct32x32_1024_add_c, 0,
                                 VPX_BITS_8),
                      make_tuple(&vpx_fdct32x32_rd_c, &vpx_idct32x32_1024_add_c,
                                 1, VPX_BITS_8)));
315
#endif  // CONFIG_VP9_HIGHBITDEPTH
316

Johann's avatar
Johann committed
317
#if HAVE_NEON && !CONFIG_EMULATE_HARDWARE
318 319
INSTANTIATE_TEST_CASE_P(
    NEON, Trans32x32Test,
Johann's avatar
Johann committed
320 321
    ::testing::Values(make_tuple(&vpx_fdct32x32_neon,
                                 &vpx_idct32x32_1024_add_neon, 0, VPX_BITS_8),
Johann's avatar
Johann committed
322
                      make_tuple(&vpx_fdct32x32_rd_neon,
clang-format's avatar
clang-format committed
323
                                 &vpx_idct32x32_1024_add_neon, 1, VPX_BITS_8)));
Johann's avatar
Johann committed
324
#endif  // HAVE_NEON && !CONFIG_EMULATE_HARDWARE
325

326
#if HAVE_SSE2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
327 328
INSTANTIATE_TEST_CASE_P(
    SSE2, Trans32x32Test,
clang-format's avatar
clang-format committed
329 330 331 332
    ::testing::Values(make_tuple(&vpx_fdct32x32_sse2,
                                 &vpx_idct32x32_1024_add_sse2, 0, VPX_BITS_8),
                      make_tuple(&vpx_fdct32x32_rd_sse2,
                                 &vpx_idct32x32_1024_add_sse2, 1, VPX_BITS_8)));
333 334 335 336 337 338
#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(
339 340
        make_tuple(&vpx_highbd_fdct32x32_sse2, &idct32x32_10, 0, VPX_BITS_10),
        make_tuple(&vpx_highbd_fdct32x32_rd_sse2, &idct32x32_10, 1,
341
                   VPX_BITS_10),
342 343
        make_tuple(&vpx_highbd_fdct32x32_sse2, &idct32x32_12, 0, VPX_BITS_12),
        make_tuple(&vpx_highbd_fdct32x32_rd_sse2, &idct32x32_12, 1,
344
                   VPX_BITS_12),
345
        make_tuple(&vpx_fdct32x32_sse2, &vpx_idct32x32_1024_add_c, 0,
346
                   VPX_BITS_8),
347
        make_tuple(&vpx_fdct32x32_rd_sse2, &vpx_idct32x32_1024_add_c, 1,
348 349
                   VPX_BITS_8)));
#endif  // HAVE_SSE2 && CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
350

351
#if HAVE_AVX2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
352 353
INSTANTIATE_TEST_CASE_P(
    AVX2, Trans32x32Test,
clang-format's avatar
clang-format committed
354 355 356 357
    ::testing::Values(make_tuple(&vpx_fdct32x32_avx2,
                                 &vpx_idct32x32_1024_add_sse2, 0, VPX_BITS_8),
                      make_tuple(&vpx_fdct32x32_rd_avx2,
                                 &vpx_idct32x32_1024_add_sse2, 1, VPX_BITS_8)));
358
#endif  // HAVE_AVX2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
359

360
#if HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
361 362
INSTANTIATE_TEST_CASE_P(
    MSA, Trans32x32Test,
clang-format's avatar
clang-format committed
363 364 365 366
    ::testing::Values(make_tuple(&vpx_fdct32x32_msa,
                                 &vpx_idct32x32_1024_add_msa, 0, VPX_BITS_8),
                      make_tuple(&vpx_fdct32x32_rd_msa,
                                 &vpx_idct32x32_1024_add_msa, 1, VPX_BITS_8)));
367
#endif  // HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
368 369 370 371 372 373 374 375 376

#if HAVE_VSX && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
INSTANTIATE_TEST_CASE_P(
    VSX, Trans32x32Test,
    ::testing::Values(make_tuple(&vpx_fdct32x32_c, &vpx_idct32x32_1024_add_vsx,
                                 0, VPX_BITS_8),
                      make_tuple(&vpx_fdct32x32_rd_c,
                                 &vpx_idct32x32_1024_add_vsx, 1, VPX_BITS_8)));
#endif  // HAVE_VSX && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
377
}  // namespace