2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
16 #include "third_party/googletest/src/include/gtest/gtest.h"
18 #include "./vp9_rtcd.h"
19 #include "./vpx_config.h"
20 #include "./vpx_dsp_rtcd.h"
21 #include "test/acm_random.h"
22 #include "test/bench.h"
23 #include "test/clear_system_state.h"
24 #include "test/register_state_check.h"
25 #include "test/util.h"
26 #include "vp9/common/vp9_entropy.h"
27 #include "vpx/vpx_codec.h"
28 #include "vpx/vpx_integer.h"
29 #include "vpx_ports/mem.h"
30 #include "vpx_ports/msvc.h" // for round()
32 using libvpx_test::ACMRandom;
36 const int kNumCoeffs = 1024;
37 const double kPi = 3.141592653589793238462643383279502884;
38 void reference_32x32_dct_1d(const double in[32], double out[32]) {
39 const double kInvSqrt2 = 0.707106781186547524400844362104;
40 for (int k = 0; k < 32; k++) {
42 for (int n = 0; n < 32; n++) {
43 out[k] += in[n] * cos(kPi * (2 * n + 1) * k / 64.0);
45 if (k == 0) out[k] = out[k] * kInvSqrt2;
49 void reference_32x32_dct_2d(const int16_t input[kNumCoeffs],
50 double output[kNumCoeffs]) {
51 // First transform columns
52 for (int i = 0; i < 32; ++i) {
53 double temp_in[32], temp_out[32];
54 for (int j = 0; j < 32; ++j) temp_in[j] = input[j * 32 + i];
55 reference_32x32_dct_1d(temp_in, temp_out);
56 for (int j = 0; j < 32; ++j) output[j * 32 + i] = temp_out[j];
58 // Then transform rows
59 for (int i = 0; i < 32; ++i) {
60 double temp_in[32], temp_out[32];
61 for (int j = 0; j < 32; ++j) temp_in[j] = output[j + i * 32];
62 reference_32x32_dct_1d(temp_in, temp_out);
63 // Scale by some magic number
64 for (int j = 0; j < 32; ++j) output[j + i * 32] = temp_out[j] / 4;
68 typedef void (*FwdTxfmFunc)(const int16_t *in, tran_low_t *out, int stride);
69 typedef void (*InvTxfmFunc)(const tran_low_t *in, uint8_t *out, int stride);
71 typedef std::tuple<FwdTxfmFunc, InvTxfmFunc, int, vpx_bit_depth_t>
74 #if CONFIG_VP9_HIGHBITDEPTH
75 void idct32x32_10(const tran_low_t *in, uint8_t *out, int stride) {
76 vpx_highbd_idct32x32_1024_add_c(in, CAST_TO_SHORTPTR(out), stride, 10);
79 void idct32x32_12(const tran_low_t *in, uint8_t *out, int stride) {
80 vpx_highbd_idct32x32_1024_add_c(in, CAST_TO_SHORTPTR(out), stride, 12);
82 #endif // CONFIG_VP9_HIGHBITDEPTH
84 class Trans32x32Test : public AbstractBench,
85 public ::testing::TestWithParam<Trans32x32Param> {
87 virtual ~Trans32x32Test() {}
88 virtual void SetUp() {
89 fwd_txfm_ = GET_PARAM(0);
90 inv_txfm_ = GET_PARAM(1);
91 version_ = GET_PARAM(2); // 0: high precision forward transform
92 // 1: low precision version for rd loop
93 bit_depth_ = GET_PARAM(3);
94 mask_ = (1 << bit_depth_) - 1;
97 virtual void TearDown() { libvpx_test::ClearSystemState(); }
101 vpx_bit_depth_t bit_depth_;
103 FwdTxfmFunc fwd_txfm_;
104 InvTxfmFunc inv_txfm_;
107 tran_low_t *bench_out_;
111 void Trans32x32Test::Run() { fwd_txfm_(bench_in_, bench_out_, 32); }
113 TEST_P(Trans32x32Test, AccuracyCheck) {
114 ACMRandom rnd(ACMRandom::DeterministicSeed());
115 uint32_t max_error = 0;
116 int64_t total_error = 0;
117 const int count_test_block = 10000;
118 DECLARE_ALIGNED(16, int16_t, test_input_block[kNumCoeffs]);
119 DECLARE_ALIGNED(16, tran_low_t, test_temp_block[kNumCoeffs]);
120 DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
121 DECLARE_ALIGNED(16, uint8_t, src[kNumCoeffs]);
122 #if CONFIG_VP9_HIGHBITDEPTH
123 DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
124 DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
127 for (int i = 0; i < count_test_block; ++i) {
128 // Initialize a test block with input range [-mask_, mask_].
129 for (int j = 0; j < kNumCoeffs; ++j) {
130 if (bit_depth_ == VPX_BITS_8) {
131 src[j] = rnd.Rand8();
132 dst[j] = rnd.Rand8();
133 test_input_block[j] = src[j] - dst[j];
134 #if CONFIG_VP9_HIGHBITDEPTH
136 src16[j] = rnd.Rand16() & mask_;
137 dst16[j] = rnd.Rand16() & mask_;
138 test_input_block[j] = src16[j] - dst16[j];
143 ASM_REGISTER_STATE_CHECK(fwd_txfm_(test_input_block, test_temp_block, 32));
144 if (bit_depth_ == VPX_BITS_8) {
145 ASM_REGISTER_STATE_CHECK(inv_txfm_(test_temp_block, dst, 32));
146 #if CONFIG_VP9_HIGHBITDEPTH
148 ASM_REGISTER_STATE_CHECK(
149 inv_txfm_(test_temp_block, CAST_TO_BYTEPTR(dst16), 32));
153 for (int j = 0; j < kNumCoeffs; ++j) {
154 #if CONFIG_VP9_HIGHBITDEPTH
156 bit_depth_ == VPX_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
158 const int32_t diff = dst[j] - src[j];
160 const uint32_t error = diff * diff;
161 if (max_error < error) max_error = error;
162 total_error += error;
171 EXPECT_GE(1u << 2 * (bit_depth_ - 8), max_error)
172 << "Error: 32x32 FDCT/IDCT has an individual round-trip error > 1";
174 EXPECT_GE(count_test_block << 2 * (bit_depth_ - 8), total_error)
175 << "Error: 32x32 FDCT/IDCT has average round-trip error > 1 per block";
178 TEST_P(Trans32x32Test, CoeffCheck) {
179 ACMRandom rnd(ACMRandom::DeterministicSeed());
180 const int count_test_block = 1000;
182 DECLARE_ALIGNED(16, int16_t, input_block[kNumCoeffs]);
183 DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kNumCoeffs]);
184 DECLARE_ALIGNED(16, tran_low_t, output_block[kNumCoeffs]);
186 for (int i = 0; i < count_test_block; ++i) {
187 for (int j = 0; j < kNumCoeffs; ++j) {
188 input_block[j] = (rnd.Rand16() & mask_) - (rnd.Rand16() & mask_);
191 const int stride = 32;
192 vpx_fdct32x32_c(input_block, output_ref_block, stride);
193 ASM_REGISTER_STATE_CHECK(fwd_txfm_(input_block, output_block, stride));
196 for (int j = 0; j < kNumCoeffs; ++j)
197 EXPECT_EQ(output_block[j], output_ref_block[j])
198 << "Error: 32x32 FDCT versions have mismatched coefficients";
200 for (int j = 0; j < kNumCoeffs; ++j)
201 EXPECT_GE(6, abs(output_block[j] - output_ref_block[j]))
202 << "Error: 32x32 FDCT rd has mismatched coefficients";
207 TEST_P(Trans32x32Test, MemCheck) {
208 ACMRandom rnd(ACMRandom::DeterministicSeed());
209 const int count_test_block = 2000;
211 DECLARE_ALIGNED(16, int16_t, input_extreme_block[kNumCoeffs]);
212 DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kNumCoeffs]);
213 DECLARE_ALIGNED(16, tran_low_t, output_block[kNumCoeffs]);
215 for (int i = 0; i < count_test_block; ++i) {
216 // Initialize a test block with input range [-mask_, mask_].
217 for (int j = 0; j < kNumCoeffs; ++j) {
218 input_extreme_block[j] = rnd.Rand8() & 1 ? mask_ : -mask_;
221 for (int j = 0; j < kNumCoeffs; ++j) input_extreme_block[j] = mask_;
223 for (int j = 0; j < kNumCoeffs; ++j) input_extreme_block[j] = -mask_;
226 const int stride = 32;
227 vpx_fdct32x32_c(input_extreme_block, output_ref_block, stride);
228 ASM_REGISTER_STATE_CHECK(
229 fwd_txfm_(input_extreme_block, output_block, stride));
231 // The minimum quant value is 4.
232 for (int j = 0; j < kNumCoeffs; ++j) {
234 EXPECT_EQ(output_block[j], output_ref_block[j])
235 << "Error: 32x32 FDCT versions have mismatched coefficients";
237 EXPECT_GE(6, abs(output_block[j] - output_ref_block[j]))
238 << "Error: 32x32 FDCT rd has mismatched coefficients";
240 EXPECT_GE(4 * DCT_MAX_VALUE << (bit_depth_ - 8), abs(output_ref_block[j]))
241 << "Error: 32x32 FDCT C has coefficient larger than 4*DCT_MAX_VALUE";
242 EXPECT_GE(4 * DCT_MAX_VALUE << (bit_depth_ - 8), abs(output_block[j]))
243 << "Error: 32x32 FDCT has coefficient larger than "
244 << "4*DCT_MAX_VALUE";
249 TEST_P(Trans32x32Test, DISABLED_Speed) {
250 ACMRandom rnd(ACMRandom::DeterministicSeed());
252 DECLARE_ALIGNED(16, int16_t, input_extreme_block[kNumCoeffs]);
253 DECLARE_ALIGNED(16, tran_low_t, output_block[kNumCoeffs]);
255 bench_in_ = input_extreme_block;
256 bench_out_ = output_block;
258 RunNTimes(INT16_MAX);
259 PrintMedian("32x32");
262 TEST_P(Trans32x32Test, InverseAccuracy) {
263 ACMRandom rnd(ACMRandom::DeterministicSeed());
264 const int count_test_block = 1000;
265 DECLARE_ALIGNED(16, int16_t, in[kNumCoeffs]);
266 DECLARE_ALIGNED(16, tran_low_t, coeff[kNumCoeffs]);
267 DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
268 DECLARE_ALIGNED(16, uint8_t, src[kNumCoeffs]);
269 #if CONFIG_VP9_HIGHBITDEPTH
270 DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
271 DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
274 for (int i = 0; i < count_test_block; ++i) {
275 double out_r[kNumCoeffs];
277 // Initialize a test block with input range [-255, 255]
278 for (int j = 0; j < kNumCoeffs; ++j) {
279 if (bit_depth_ == VPX_BITS_8) {
280 src[j] = rnd.Rand8();
281 dst[j] = rnd.Rand8();
282 in[j] = src[j] - dst[j];
283 #if CONFIG_VP9_HIGHBITDEPTH
285 src16[j] = rnd.Rand16() & mask_;
286 dst16[j] = rnd.Rand16() & mask_;
287 in[j] = src16[j] - dst16[j];
292 reference_32x32_dct_2d(in, out_r);
293 for (int j = 0; j < kNumCoeffs; ++j) {
294 coeff[j] = static_cast<tran_low_t>(round(out_r[j]));
296 if (bit_depth_ == VPX_BITS_8) {
297 ASM_REGISTER_STATE_CHECK(inv_txfm_(coeff, dst, 32));
298 #if CONFIG_VP9_HIGHBITDEPTH
300 ASM_REGISTER_STATE_CHECK(inv_txfm_(coeff, CAST_TO_BYTEPTR(dst16), 32));
303 for (int j = 0; j < kNumCoeffs; ++j) {
304 #if CONFIG_VP9_HIGHBITDEPTH
306 bit_depth_ == VPX_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
308 const int diff = dst[j] - src[j];
310 const int error = diff * diff;
311 EXPECT_GE(1, error) << "Error: 32x32 IDCT has error " << error
312 << " at index " << j;
317 using std::make_tuple;
319 #if CONFIG_VP9_HIGHBITDEPTH
320 INSTANTIATE_TEST_SUITE_P(
323 make_tuple(&vpx_highbd_fdct32x32_c, &idct32x32_10, 0, VPX_BITS_10),
324 make_tuple(&vpx_highbd_fdct32x32_rd_c, &idct32x32_10, 1, VPX_BITS_10),
325 make_tuple(&vpx_highbd_fdct32x32_c, &idct32x32_12, 0, VPX_BITS_12),
326 make_tuple(&vpx_highbd_fdct32x32_rd_c, &idct32x32_12, 1, VPX_BITS_12),
327 make_tuple(&vpx_fdct32x32_c, &vpx_idct32x32_1024_add_c, 0, VPX_BITS_8),
328 make_tuple(&vpx_fdct32x32_rd_c, &vpx_idct32x32_1024_add_c, 1,
331 INSTANTIATE_TEST_SUITE_P(
333 ::testing::Values(make_tuple(&vpx_fdct32x32_c, &vpx_idct32x32_1024_add_c, 0,
335 make_tuple(&vpx_fdct32x32_rd_c, &vpx_idct32x32_1024_add_c,
337 #endif // CONFIG_VP9_HIGHBITDEPTH
339 #if HAVE_NEON && !CONFIG_EMULATE_HARDWARE
340 INSTANTIATE_TEST_SUITE_P(
341 NEON, Trans32x32Test,
342 ::testing::Values(make_tuple(&vpx_fdct32x32_neon,
343 &vpx_idct32x32_1024_add_neon, 0, VPX_BITS_8),
344 make_tuple(&vpx_fdct32x32_rd_neon,
345 &vpx_idct32x32_1024_add_neon, 1, VPX_BITS_8)));
346 #endif // HAVE_NEON && !CONFIG_EMULATE_HARDWARE
348 #if HAVE_SSE2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
349 INSTANTIATE_TEST_SUITE_P(
350 SSE2, Trans32x32Test,
351 ::testing::Values(make_tuple(&vpx_fdct32x32_sse2,
352 &vpx_idct32x32_1024_add_sse2, 0, VPX_BITS_8),
353 make_tuple(&vpx_fdct32x32_rd_sse2,
354 &vpx_idct32x32_1024_add_sse2, 1, VPX_BITS_8)));
355 #endif // HAVE_SSE2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
357 #if HAVE_SSE2 && CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
358 INSTANTIATE_TEST_SUITE_P(
359 SSE2, Trans32x32Test,
361 make_tuple(&vpx_highbd_fdct32x32_sse2, &idct32x32_10, 0, VPX_BITS_10),
362 make_tuple(&vpx_highbd_fdct32x32_rd_sse2, &idct32x32_10, 1,
364 make_tuple(&vpx_highbd_fdct32x32_sse2, &idct32x32_12, 0, VPX_BITS_12),
365 make_tuple(&vpx_highbd_fdct32x32_rd_sse2, &idct32x32_12, 1,
367 make_tuple(&vpx_fdct32x32_sse2, &vpx_idct32x32_1024_add_c, 0,
369 make_tuple(&vpx_fdct32x32_rd_sse2, &vpx_idct32x32_1024_add_c, 1,
371 #endif // HAVE_SSE2 && CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
373 #if HAVE_AVX2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
374 INSTANTIATE_TEST_SUITE_P(
375 AVX2, Trans32x32Test,
376 ::testing::Values(make_tuple(&vpx_fdct32x32_avx2,
377 &vpx_idct32x32_1024_add_sse2, 0, VPX_BITS_8),
378 make_tuple(&vpx_fdct32x32_rd_avx2,
379 &vpx_idct32x32_1024_add_sse2, 1, VPX_BITS_8)));
380 #endif // HAVE_AVX2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
382 #if HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
383 INSTANTIATE_TEST_SUITE_P(
385 ::testing::Values(make_tuple(&vpx_fdct32x32_msa,
386 &vpx_idct32x32_1024_add_msa, 0, VPX_BITS_8),
387 make_tuple(&vpx_fdct32x32_rd_msa,
388 &vpx_idct32x32_1024_add_msa, 1, VPX_BITS_8)));
389 #endif // HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
391 #if HAVE_VSX && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
392 INSTANTIATE_TEST_SUITE_P(
394 ::testing::Values(make_tuple(&vpx_fdct32x32_c, &vpx_idct32x32_1024_add_vsx,
396 make_tuple(&vpx_fdct32x32_rd_vsx,
397 &vpx_idct32x32_1024_add_vsx, 1, VPX_BITS_8)));
398 #endif // HAVE_VSX && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
400 #if HAVE_LSX && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
401 INSTANTIATE_TEST_SUITE_P(
403 ::testing::Values(make_tuple(&vpx_fdct32x32_lsx,
404 &vpx_idct32x32_1024_add_lsx, 0, VPX_BITS_8),
405 make_tuple(&vpx_fdct32x32_rd_lsx,
406 &vpx_idct32x32_1024_add_lsx, 1, VPX_BITS_8)));
407 #endif // HAVE_LSX && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE