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.
15 #include "third_party/googletest/src/include/gtest/gtest.h"
16 #include "test/acm_random.h"
17 #include "test/clear_system_state.h"
18 #include "test/register_state_check.h"
19 #include "test/util.h"
22 #include "./vpx_config.h"
23 #include "vp9/common/vp9_entropy.h"
24 #include "./vp9_rtcd.h"
27 #include "vpx/vpx_integer.h"
29 using libvpx_test::ACMRandom;
33 static int round(double x) {
35 return static_cast<int>(ceil(x - 0.5));
37 return static_cast<int>(floor(x + 0.5));
41 const int kNumCoeffs = 1024;
42 const double kPi = 3.141592653589793238462643383279502884;
43 void reference_32x32_dct_1d(const double in[32], double out[32], int stride) {
44 const double kInvSqrt2 = 0.707106781186547524400844362104;
45 for (int k = 0; k < 32; k++) {
47 for (int n = 0; n < 32; n++)
48 out[k] += in[n] * cos(kPi * (2 * n + 1) * k / 64.0);
50 out[k] = out[k] * kInvSqrt2;
54 void reference_32x32_dct_2d(const int16_t input[kNumCoeffs],
55 double output[kNumCoeffs]) {
56 // First transform columns
57 for (int i = 0; i < 32; ++i) {
58 double temp_in[32], temp_out[32];
59 for (int j = 0; j < 32; ++j)
60 temp_in[j] = input[j*32 + i];
61 reference_32x32_dct_1d(temp_in, temp_out, 1);
62 for (int j = 0; j < 32; ++j)
63 output[j * 32 + i] = temp_out[j];
65 // Then transform rows
66 for (int i = 0; i < 32; ++i) {
67 double temp_in[32], temp_out[32];
68 for (int j = 0; j < 32; ++j)
69 temp_in[j] = output[j + i*32];
70 reference_32x32_dct_1d(temp_in, temp_out, 1);
71 // Scale by some magic number
72 for (int j = 0; j < 32; ++j)
73 output[j + i * 32] = temp_out[j] / 4;
77 typedef void (*fwd_txfm_t)(const int16_t *in, int16_t *out, int stride);
78 typedef void (*inv_txfm_t)(const int16_t *in, uint8_t *out, int stride);
80 typedef std::tr1::tuple<fwd_txfm_t, inv_txfm_t, int> trans_32x32_param_t;
82 class Trans32x32Test : public ::testing::TestWithParam<trans_32x32_param_t> {
84 virtual ~Trans32x32Test() {}
85 virtual void SetUp() {
86 fwd_txfm_ = GET_PARAM(0);
87 inv_txfm_ = GET_PARAM(1);
88 version_ = GET_PARAM(2); // 0: high precision forward transform
89 // 1: low precision version for rd loop
92 virtual void TearDown() { libvpx_test::ClearSystemState(); }
100 TEST_P(Trans32x32Test, AccuracyCheck) {
101 ACMRandom rnd(ACMRandom::DeterministicSeed());
102 uint32_t max_error = 0;
103 int64_t total_error = 0;
104 const int count_test_block = 1000;
105 DECLARE_ALIGNED_ARRAY(16, int16_t, test_input_block, kNumCoeffs);
106 DECLARE_ALIGNED_ARRAY(16, int16_t, test_temp_block, kNumCoeffs);
107 DECLARE_ALIGNED_ARRAY(16, uint8_t, dst, kNumCoeffs);
108 DECLARE_ALIGNED_ARRAY(16, uint8_t, src, kNumCoeffs);
110 for (int i = 0; i < count_test_block; ++i) {
111 // Initialize a test block with input range [-255, 255].
112 for (int j = 0; j < kNumCoeffs; ++j) {
113 src[j] = rnd.Rand8();
114 dst[j] = rnd.Rand8();
115 test_input_block[j] = src[j] - dst[j];
118 REGISTER_STATE_CHECK(fwd_txfm_(test_input_block, test_temp_block, 32));
119 REGISTER_STATE_CHECK(inv_txfm_(test_temp_block, dst, 32));
121 for (int j = 0; j < kNumCoeffs; ++j) {
122 const uint32_t diff = dst[j] - src[j];
123 const uint32_t error = diff * diff;
124 if (max_error < error)
126 total_error += error;
135 EXPECT_GE(1u, max_error)
136 << "Error: 32x32 FDCT/IDCT has an individual round-trip error > 1";
138 EXPECT_GE(count_test_block, total_error)
139 << "Error: 32x32 FDCT/IDCT has average round-trip error > 1 per block";
142 TEST_P(Trans32x32Test, CoeffCheck) {
143 ACMRandom rnd(ACMRandom::DeterministicSeed());
144 const int count_test_block = 1000;
146 DECLARE_ALIGNED_ARRAY(16, int16_t, input_block, kNumCoeffs);
147 DECLARE_ALIGNED_ARRAY(16, int16_t, output_ref_block, kNumCoeffs);
148 DECLARE_ALIGNED_ARRAY(16, int16_t, output_block, kNumCoeffs);
150 for (int i = 0; i < count_test_block; ++i) {
151 for (int j = 0; j < kNumCoeffs; ++j)
152 input_block[j] = rnd.Rand8() - rnd.Rand8();
154 const int stride = 32;
155 vp9_fdct32x32_c(input_block, output_ref_block, stride);
156 REGISTER_STATE_CHECK(fwd_txfm_(input_block, output_block, stride));
159 for (int j = 0; j < kNumCoeffs; ++j)
160 EXPECT_EQ(output_block[j], output_ref_block[j])
161 << "Error: 32x32 FDCT versions have mismatched coefficients";
163 for (int j = 0; j < kNumCoeffs; ++j)
164 EXPECT_GE(6, abs(output_block[j] - output_ref_block[j]))
165 << "Error: 32x32 FDCT rd has mismatched coefficients";
170 TEST_P(Trans32x32Test, MemCheck) {
171 ACMRandom rnd(ACMRandom::DeterministicSeed());
172 const int count_test_block = 2000;
174 DECLARE_ALIGNED_ARRAY(16, int16_t, input_block, kNumCoeffs);
175 DECLARE_ALIGNED_ARRAY(16, int16_t, input_extreme_block, kNumCoeffs);
176 DECLARE_ALIGNED_ARRAY(16, int16_t, output_ref_block, kNumCoeffs);
177 DECLARE_ALIGNED_ARRAY(16, int16_t, output_block, kNumCoeffs);
179 for (int i = 0; i < count_test_block; ++i) {
180 // Initialize a test block with input range [-255, 255].
181 for (int j = 0; j < kNumCoeffs; ++j) {
182 input_block[j] = rnd.Rand8() - rnd.Rand8();
183 input_extreme_block[j] = rnd.Rand8() & 1 ? 255 : -255;
186 for (int j = 0; j < kNumCoeffs; ++j)
187 input_extreme_block[j] = 255;
189 for (int j = 0; j < kNumCoeffs; ++j)
190 input_extreme_block[j] = -255;
192 const int stride = 32;
193 vp9_fdct32x32_c(input_extreme_block, output_ref_block, stride);
194 REGISTER_STATE_CHECK(fwd_txfm_(input_extreme_block, output_block, stride));
196 // The minimum quant value is 4.
197 for (int j = 0; j < kNumCoeffs; ++j) {
199 EXPECT_EQ(output_block[j], output_ref_block[j])
200 << "Error: 32x32 FDCT versions have mismatched coefficients";
202 EXPECT_GE(6, abs(output_block[j] - output_ref_block[j]))
203 << "Error: 32x32 FDCT rd has mismatched coefficients";
205 EXPECT_GE(4 * DCT_MAX_VALUE, abs(output_ref_block[j]))
206 << "Error: 32x32 FDCT C has coefficient larger than 4*DCT_MAX_VALUE";
207 EXPECT_GE(4 * DCT_MAX_VALUE, abs(output_block[j]))
208 << "Error: 32x32 FDCT has coefficient larger than "
209 << "4*DCT_MAX_VALUE";
214 TEST_P(Trans32x32Test, InverseAccuracy) {
215 ACMRandom rnd(ACMRandom::DeterministicSeed());
216 const int count_test_block = 1000;
217 DECLARE_ALIGNED_ARRAY(16, int16_t, in, kNumCoeffs);
218 DECLARE_ALIGNED_ARRAY(16, int16_t, coeff, kNumCoeffs);
219 DECLARE_ALIGNED_ARRAY(16, uint8_t, dst, kNumCoeffs);
220 DECLARE_ALIGNED_ARRAY(16, uint8_t, src, kNumCoeffs);
222 for (int i = 0; i < count_test_block; ++i) {
223 double out_r[kNumCoeffs];
225 // Initialize a test block with input range [-255, 255]
226 for (int j = 0; j < kNumCoeffs; ++j) {
227 src[j] = rnd.Rand8();
228 dst[j] = rnd.Rand8();
229 in[j] = src[j] - dst[j];
232 reference_32x32_dct_2d(in, out_r);
233 for (int j = 0; j < kNumCoeffs; ++j)
234 coeff[j] = round(out_r[j]);
235 REGISTER_STATE_CHECK(inv_txfm_(coeff, dst, 32));
236 for (int j = 0; j < kNumCoeffs; ++j) {
237 const int diff = dst[j] - src[j];
238 const int error = diff * diff;
240 << "Error: 32x32 IDCT has error " << error
241 << " at index " << j;
246 using std::tr1::make_tuple;
248 INSTANTIATE_TEST_CASE_P(
251 make_tuple(&vp9_fdct32x32_c, &vp9_idct32x32_1024_add_c, 0),
252 make_tuple(&vp9_fdct32x32_rd_c, &vp9_idct32x32_1024_add_c, 1)));
255 INSTANTIATE_TEST_CASE_P(
256 SSE2, Trans32x32Test,
258 make_tuple(&vp9_fdct32x32_sse2,
259 &vp9_idct32x32_1024_add_sse2, 0),
260 make_tuple(&vp9_fdct32x32_rd_sse2,
261 &vp9_idct32x32_1024_add_sse2, 1)));
265 INSTANTIATE_TEST_CASE_P(
266 AVX2, Trans32x32Test,
268 make_tuple(&vp9_fdct32x32_avx2,
269 &vp9_idct32x32_1024_add_sse2, 0),
270 make_tuple(&vp9_fdct32x32_rd_avx2,
271 &vp9_idct32x32_1024_add_sse2, 1)));