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 "vp9/common/vp9_entropy.h"
23 #include "./vp9_rtcd.h"
24 void vp9_idct16x16_256_add_c(const int16_t *input, uint8_t *output, int pitch);
26 #include "vpx/vpx_integer.h"
28 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 = 256;
42 const double PI = 3.1415926535898;
43 void reference2_16x16_idct_2d(double *input, double *output) {
45 for (int l = 0; l < 16; ++l) {
46 for (int k = 0; k < 16; ++k) {
48 for (int i = 0; i < 16; ++i) {
49 for (int j = 0; j < 16; ++j) {
50 x = cos(PI * j * (l + 0.5) / 16.0) *
51 cos(PI * i * (k + 0.5) / 16.0) *
52 input[i * 16 + j] / 256;
66 const double C1 = 0.995184726672197;
67 const double C2 = 0.98078528040323;
68 const double C3 = 0.956940335732209;
69 const double C4 = 0.923879532511287;
70 const double C5 = 0.881921264348355;
71 const double C6 = 0.831469612302545;
72 const double C7 = 0.773010453362737;
73 const double C8 = 0.707106781186548;
74 const double C9 = 0.634393284163646;
75 const double C10 = 0.555570233019602;
76 const double C11 = 0.471396736825998;
77 const double C12 = 0.38268343236509;
78 const double C13 = 0.290284677254462;
79 const double C14 = 0.195090322016128;
80 const double C15 = 0.098017140329561;
82 void butterfly_16x16_dct_1d(double input[16], double output[16]) {
84 double intermediate[16];
88 step[ 0] = input[0] + input[15];
89 step[ 1] = input[1] + input[14];
90 step[ 2] = input[2] + input[13];
91 step[ 3] = input[3] + input[12];
92 step[ 4] = input[4] + input[11];
93 step[ 5] = input[5] + input[10];
94 step[ 6] = input[6] + input[ 9];
95 step[ 7] = input[7] + input[ 8];
96 step[ 8] = input[7] - input[ 8];
97 step[ 9] = input[6] - input[ 9];
98 step[10] = input[5] - input[10];
99 step[11] = input[4] - input[11];
100 step[12] = input[3] - input[12];
101 step[13] = input[2] - input[13];
102 step[14] = input[1] - input[14];
103 step[15] = input[0] - input[15];
106 output[0] = step[0] + step[7];
107 output[1] = step[1] + step[6];
108 output[2] = step[2] + step[5];
109 output[3] = step[3] + step[4];
110 output[4] = step[3] - step[4];
111 output[5] = step[2] - step[5];
112 output[6] = step[1] - step[6];
113 output[7] = step[0] - step[7];
115 temp1 = step[ 8] * C7;
116 temp2 = step[15] * C9;
117 output[ 8] = temp1 + temp2;
119 temp1 = step[ 9] * C11;
120 temp2 = step[14] * C5;
121 output[ 9] = temp1 - temp2;
123 temp1 = step[10] * C3;
124 temp2 = step[13] * C13;
125 output[10] = temp1 + temp2;
127 temp1 = step[11] * C15;
128 temp2 = step[12] * C1;
129 output[11] = temp1 - temp2;
131 temp1 = step[11] * C1;
132 temp2 = step[12] * C15;
133 output[12] = temp2 + temp1;
135 temp1 = step[10] * C13;
136 temp2 = step[13] * C3;
137 output[13] = temp2 - temp1;
139 temp1 = step[ 9] * C5;
140 temp2 = step[14] * C11;
141 output[14] = temp2 + temp1;
143 temp1 = step[ 8] * C9;
144 temp2 = step[15] * C7;
145 output[15] = temp2 - temp1;
148 step[ 0] = output[0] + output[3];
149 step[ 1] = output[1] + output[2];
150 step[ 2] = output[1] - output[2];
151 step[ 3] = output[0] - output[3];
153 temp1 = output[4] * C14;
154 temp2 = output[7] * C2;
155 step[ 4] = temp1 + temp2;
157 temp1 = output[5] * C10;
158 temp2 = output[6] * C6;
159 step[ 5] = temp1 + temp2;
161 temp1 = output[5] * C6;
162 temp2 = output[6] * C10;
163 step[ 6] = temp2 - temp1;
165 temp1 = output[4] * C2;
166 temp2 = output[7] * C14;
167 step[ 7] = temp2 - temp1;
169 step[ 8] = output[ 8] + output[11];
170 step[ 9] = output[ 9] + output[10];
171 step[10] = output[ 9] - output[10];
172 step[11] = output[ 8] - output[11];
174 step[12] = output[12] + output[15];
175 step[13] = output[13] + output[14];
176 step[14] = output[13] - output[14];
177 step[15] = output[12] - output[15];
180 output[ 0] = (step[ 0] + step[ 1]);
181 output[ 8] = (step[ 0] - step[ 1]);
183 temp1 = step[2] * C12;
184 temp2 = step[3] * C4;
185 temp1 = temp1 + temp2;
186 output[ 4] = 2*(temp1 * C8);
188 temp1 = step[2] * C4;
189 temp2 = step[3] * C12;
190 temp1 = temp2 - temp1;
191 output[12] = 2 * (temp1 * C8);
193 output[ 2] = 2 * ((step[4] + step[ 5]) * C8);
194 output[14] = 2 * ((step[7] - step[ 6]) * C8);
196 temp1 = step[4] - step[5];
197 temp2 = step[6] + step[7];
198 output[ 6] = (temp1 + temp2);
199 output[10] = (temp1 - temp2);
201 intermediate[8] = step[8] + step[14];
202 intermediate[9] = step[9] + step[15];
204 temp1 = intermediate[8] * C12;
205 temp2 = intermediate[9] * C4;
206 temp1 = temp1 - temp2;
207 output[3] = 2 * (temp1 * C8);
209 temp1 = intermediate[8] * C4;
210 temp2 = intermediate[9] * C12;
211 temp1 = temp2 + temp1;
212 output[13] = 2 * (temp1 * C8);
214 output[ 9] = 2 * ((step[10] + step[11]) * C8);
216 intermediate[11] = step[10] - step[11];
217 intermediate[12] = step[12] + step[13];
218 intermediate[13] = step[12] - step[13];
219 intermediate[14] = step[ 8] - step[14];
220 intermediate[15] = step[ 9] - step[15];
222 output[15] = (intermediate[11] + intermediate[12]);
223 output[ 1] = -(intermediate[11] - intermediate[12]);
225 output[ 7] = 2 * (intermediate[13] * C8);
227 temp1 = intermediate[14] * C12;
228 temp2 = intermediate[15] * C4;
229 temp1 = temp1 - temp2;
230 output[11] = -2 * (temp1 * C8);
232 temp1 = intermediate[14] * C4;
233 temp2 = intermediate[15] * C12;
234 temp1 = temp2 + temp1;
235 output[ 5] = 2 * (temp1 * C8);
238 void reference_16x16_dct_2d(int16_t input[256], double output[256]) {
239 // First transform columns
240 for (int i = 0; i < 16; ++i) {
241 double temp_in[16], temp_out[16];
242 for (int j = 0; j < 16; ++j)
243 temp_in[j] = input[j * 16 + i];
244 butterfly_16x16_dct_1d(temp_in, temp_out);
245 for (int j = 0; j < 16; ++j)
246 output[j * 16 + i] = temp_out[j];
248 // Then transform rows
249 for (int i = 0; i < 16; ++i) {
250 double temp_in[16], temp_out[16];
251 for (int j = 0; j < 16; ++j)
252 temp_in[j] = output[j + i * 16];
253 butterfly_16x16_dct_1d(temp_in, temp_out);
254 // Scale by some magic number
255 for (int j = 0; j < 16; ++j)
256 output[j + i * 16] = temp_out[j]/2;
260 typedef void (*fdct_t)(const int16_t *in, int16_t *out, int stride);
261 typedef void (*idct_t)(const int16_t *in, uint8_t *out, int stride);
262 typedef void (*fht_t) (const int16_t *in, int16_t *out, int stride,
264 typedef void (*iht_t) (const int16_t *in, uint8_t *out, int stride,
267 void fdct16x16_ref(const int16_t *in, int16_t *out, int stride, int tx_type) {
268 vp9_fdct16x16_c(in, out, stride);
271 void fht16x16_ref(const int16_t *in, int16_t *out, int stride, int tx_type) {
272 vp9_short_fht16x16_c(in, out, stride, tx_type);
275 class Trans16x16TestBase {
277 virtual ~Trans16x16TestBase() {}
280 virtual void RunFwdTxfm(int16_t *in, int16_t *out, int stride) = 0;
282 virtual void RunInvTxfm(int16_t *out, uint8_t *dst, int stride) = 0;
284 void RunAccuracyCheck() {
285 ACMRandom rnd(ACMRandom::DeterministicSeed());
286 uint32_t max_error = 0;
287 int64_t total_error = 0;
288 const int count_test_block = 10000;
289 for (int i = 0; i < count_test_block; ++i) {
290 DECLARE_ALIGNED_ARRAY(16, int16_t, test_input_block, kNumCoeffs);
291 DECLARE_ALIGNED_ARRAY(16, int16_t, test_temp_block, kNumCoeffs);
292 DECLARE_ALIGNED_ARRAY(16, uint8_t, dst, kNumCoeffs);
293 DECLARE_ALIGNED_ARRAY(16, uint8_t, src, kNumCoeffs);
295 // Initialize a test block with input range [-255, 255].
296 for (int j = 0; j < kNumCoeffs; ++j) {
297 src[j] = rnd.Rand8();
298 dst[j] = rnd.Rand8();
299 test_input_block[j] = src[j] - dst[j];
302 REGISTER_STATE_CHECK(RunFwdTxfm(test_input_block,
303 test_temp_block, pitch_));
304 REGISTER_STATE_CHECK(RunInvTxfm(test_temp_block, dst, pitch_));
306 for (int j = 0; j < kNumCoeffs; ++j) {
307 const uint32_t diff = dst[j] - src[j];
308 const uint32_t error = diff * diff;
309 if (max_error < error)
311 total_error += error;
315 EXPECT_GE(1u, max_error)
316 << "Error: 16x16 FHT/IHT has an individual round trip error > 1";
318 EXPECT_GE(count_test_block , total_error)
319 << "Error: 16x16 FHT/IHT has average round trip error > 1 per block";
322 void RunCoeffCheck() {
323 ACMRandom rnd(ACMRandom::DeterministicSeed());
324 const int count_test_block = 1000;
325 DECLARE_ALIGNED_ARRAY(16, int16_t, input_block, kNumCoeffs);
326 DECLARE_ALIGNED_ARRAY(16, int16_t, output_ref_block, kNumCoeffs);
327 DECLARE_ALIGNED_ARRAY(16, int16_t, output_block, kNumCoeffs);
329 for (int i = 0; i < count_test_block; ++i) {
330 // Initialize a test block with input range [-255, 255].
331 for (int j = 0; j < kNumCoeffs; ++j)
332 input_block[j] = rnd.Rand8() - rnd.Rand8();
334 fwd_txfm_ref(input_block, output_ref_block, pitch_, tx_type_);
335 REGISTER_STATE_CHECK(RunFwdTxfm(input_block, output_block, pitch_));
337 // The minimum quant value is 4.
338 for (int j = 0; j < kNumCoeffs; ++j)
339 EXPECT_EQ(output_block[j], output_ref_block[j]);
344 ACMRandom rnd(ACMRandom::DeterministicSeed());
345 const int count_test_block = 1000;
346 DECLARE_ALIGNED_ARRAY(16, int16_t, input_block, kNumCoeffs);
347 DECLARE_ALIGNED_ARRAY(16, int16_t, input_extreme_block, kNumCoeffs);
348 DECLARE_ALIGNED_ARRAY(16, int16_t, output_ref_block, kNumCoeffs);
349 DECLARE_ALIGNED_ARRAY(16, int16_t, output_block, kNumCoeffs);
351 for (int i = 0; i < count_test_block; ++i) {
352 // Initialize a test block with input range [-255, 255].
353 for (int j = 0; j < kNumCoeffs; ++j) {
354 input_block[j] = rnd.Rand8() - rnd.Rand8();
355 input_extreme_block[j] = rnd.Rand8() % 2 ? 255 : -255;
358 for (int j = 0; j < kNumCoeffs; ++j)
359 input_extreme_block[j] = 255;
361 for (int j = 0; j < kNumCoeffs; ++j)
362 input_extreme_block[j] = -255;
364 fwd_txfm_ref(input_extreme_block, output_ref_block, pitch_, tx_type_);
365 REGISTER_STATE_CHECK(RunFwdTxfm(input_extreme_block,
366 output_block, pitch_));
368 // The minimum quant value is 4.
369 for (int j = 0; j < kNumCoeffs; ++j) {
370 EXPECT_EQ(output_block[j], output_ref_block[j]);
371 EXPECT_GE(4 * DCT_MAX_VALUE, abs(output_block[j]))
372 << "Error: 16x16 FDCT has coefficient larger than 4*DCT_MAX_VALUE";
377 void RunInvAccuracyCheck() {
378 ACMRandom rnd(ACMRandom::DeterministicSeed());
379 const int count_test_block = 1000;
380 DECLARE_ALIGNED_ARRAY(16, int16_t, in, kNumCoeffs);
381 DECLARE_ALIGNED_ARRAY(16, int16_t, coeff, kNumCoeffs);
382 DECLARE_ALIGNED_ARRAY(16, uint8_t, dst, kNumCoeffs);
383 DECLARE_ALIGNED_ARRAY(16, uint8_t, src, kNumCoeffs);
385 for (int i = 0; i < count_test_block; ++i) {
386 double out_r[kNumCoeffs];
388 // Initialize a test block with input range [-255, 255].
389 for (int j = 0; j < kNumCoeffs; ++j) {
390 src[j] = rnd.Rand8();
391 dst[j] = rnd.Rand8();
392 in[j] = src[j] - dst[j];
395 reference_16x16_dct_2d(in, out_r);
396 for (int j = 0; j < kNumCoeffs; ++j)
397 coeff[j] = round(out_r[j]);
399 REGISTER_STATE_CHECK(RunInvTxfm(coeff, dst, 16));
401 for (int j = 0; j < kNumCoeffs; ++j) {
402 const uint32_t diff = dst[j] - src[j];
403 const uint32_t error = diff * diff;
405 << "Error: 16x16 IDCT has error " << error
406 << " at index " << j;
415 class Trans16x16DCT : public Trans16x16TestBase,
416 public PARAMS(fdct_t, idct_t, int) {
418 virtual ~Trans16x16DCT() {}
420 virtual void SetUp() {
421 fwd_txfm_ = GET_PARAM(0);
422 inv_txfm_ = GET_PARAM(1);
423 tx_type_ = GET_PARAM(2);
425 fwd_txfm_ref = fdct16x16_ref;
427 virtual void TearDown() { libvpx_test::ClearSystemState(); }
430 void RunFwdTxfm(int16_t *in, int16_t *out, int stride) {
431 fwd_txfm_(in, out, stride);
433 void RunInvTxfm(int16_t *out, uint8_t *dst, int stride) {
434 inv_txfm_(out, dst, stride);
441 TEST_P(Trans16x16DCT, AccuracyCheck) {
445 TEST_P(Trans16x16DCT, CoeffCheck) {
449 TEST_P(Trans16x16DCT, MemCheck) {
453 TEST_P(Trans16x16DCT, InvAccuracyCheck) {
454 RunInvAccuracyCheck();
457 class Trans16x16HT : public Trans16x16TestBase,
458 public PARAMS(fht_t, iht_t, int) {
460 virtual ~Trans16x16HT() {}
462 virtual void SetUp() {
463 fwd_txfm_ = GET_PARAM(0);
464 inv_txfm_ = GET_PARAM(1);
465 tx_type_ = GET_PARAM(2);
467 fwd_txfm_ref = fht16x16_ref;
469 virtual void TearDown() { libvpx_test::ClearSystemState(); }
472 void RunFwdTxfm(int16_t *in, int16_t *out, int stride) {
473 fwd_txfm_(in, out, stride, tx_type_);
475 void RunInvTxfm(int16_t *out, uint8_t *dst, int stride) {
476 inv_txfm_(out, dst, stride, tx_type_);
483 TEST_P(Trans16x16HT, AccuracyCheck) {
487 TEST_P(Trans16x16HT, CoeffCheck) {
491 TEST_P(Trans16x16HT, MemCheck) {
495 using std::tr1::make_tuple;
497 INSTANTIATE_TEST_CASE_P(
500 make_tuple(&vp9_fdct16x16_c, &vp9_idct16x16_256_add_c, 0)));
501 INSTANTIATE_TEST_CASE_P(
504 make_tuple(&vp9_short_fht16x16_c, &vp9_iht16x16_256_add_c, 0),
505 make_tuple(&vp9_short_fht16x16_c, &vp9_iht16x16_256_add_c, 1),
506 make_tuple(&vp9_short_fht16x16_c, &vp9_iht16x16_256_add_c, 2),
507 make_tuple(&vp9_short_fht16x16_c, &vp9_iht16x16_256_add_c, 3)));
510 INSTANTIATE_TEST_CASE_P(
513 make_tuple(&vp9_fdct16x16_sse2,
514 &vp9_idct16x16_256_add_sse2, 0)));
515 INSTANTIATE_TEST_CASE_P(
518 make_tuple(&vp9_short_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 0),
519 make_tuple(&vp9_short_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 1),
520 make_tuple(&vp9_short_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 2),
521 make_tuple(&vp9_short_fht16x16_sse2, &vp9_iht16x16_256_add_sse2, 3)));