dct32x32_test.cc 13.3 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 29 30 31

using libvpx_test::ACMRandom;

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

41 42
const int kNumCoeffs = 1024;
const double kPi = 3.141592653589793238462643383279502884;
43
void reference_32x32_dct_1d(const double in[32], double out[32]) {
44 45 46 47 48 49 50 51 52 53
  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;
  }
}

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

77 78
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);
79

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

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

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

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

97
class Trans32x32Test : public ::testing::TestWithParam<Trans32x32Param> {
98 99 100 101 102 103 104
 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
105 106
    bit_depth_ = GET_PARAM(3);
    mask_ = (1 << bit_depth_) - 1;
107
  }
108

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

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

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

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

149
    ASM_REGISTER_STATE_CHECK(fwd_txfm_(test_input_block, test_temp_block, 32));
150 151 152 153 154 155 156 157
    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
    }
158

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

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

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

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

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

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

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

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

    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;

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

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

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

    // The minimum quant value is 4.
240 241 242 243 244 245 246 247
    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";
      }
248
      EXPECT_GE(4 * DCT_MAX_VALUE << (bit_depth_ - 8), abs(output_ref_block[j]))
249
          << "Error: 32x32 FDCT C has coefficient larger than 4*DCT_MAX_VALUE";
250
      EXPECT_GE(4 * DCT_MAX_VALUE << (bit_depth_ - 8), abs(output_block[j]))
251 252
          << "Error: 32x32 FDCT has coefficient larger than "
          << "4*DCT_MAX_VALUE";
253 254 255
    }
  }
}
256 257 258 259

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

  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) {
274 275 276 277 278 279 280 281 282 283 284
      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
      }
285 286 287 288
    }

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

using std::tr1::make_tuple;

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

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

350
#if HAVE_SSE2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
351 352 353
INSTANTIATE_TEST_CASE_P(
    SSE2, Trans32x32Test,
    ::testing::Values(
354
        make_tuple(&vpx_fdct32x32_sse2,
355
                   &vpx_idct32x32_1024_add_sse2, 0, VPX_BITS_8),
356
        make_tuple(&vpx_fdct32x32_rd_sse2,
357
                   &vpx_idct32x32_1024_add_sse2, 1, VPX_BITS_8)));
358 359 360 361 362 363
#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(
364 365
        make_tuple(&vpx_highbd_fdct32x32_sse2, &idct32x32_10, 0, VPX_BITS_10),
        make_tuple(&vpx_highbd_fdct32x32_rd_sse2, &idct32x32_10, 1,
366
                   VPX_BITS_10),
367 368
        make_tuple(&vpx_highbd_fdct32x32_sse2, &idct32x32_12, 0, VPX_BITS_12),
        make_tuple(&vpx_highbd_fdct32x32_rd_sse2, &idct32x32_12, 1,
369
                   VPX_BITS_12),
370
        make_tuple(&vpx_fdct32x32_sse2, &vpx_idct32x32_1024_add_c, 0,
371
                   VPX_BITS_8),
372
        make_tuple(&vpx_fdct32x32_rd_sse2, &vpx_idct32x32_1024_add_c, 1,
373 374
                   VPX_BITS_8)));
#endif  // HAVE_SSE2 && CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
375

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

386
#if HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
387 388 389
INSTANTIATE_TEST_CASE_P(
    MSA, Trans32x32Test,
    ::testing::Values(
390
        make_tuple(&vpx_fdct32x32_msa,
391
                   &vpx_idct32x32_1024_add_msa, 0, VPX_BITS_8),
392
        make_tuple(&vpx_fdct32x32_rd_msa,
393
                   &vpx_idct32x32_1024_add_msa, 1, VPX_BITS_8)));
394
#endif  // HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
395
}  // namespace