- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / test / dct16x16_test.cc
1 /*
2  *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3  *
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.
9  */
10
11 #include <math.h>
12 #include <stdlib.h>
13 #include <string.h>
14
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"
20
21 extern "C" {
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);
25 }
26 #include "vpx/vpx_integer.h"
27
28 using libvpx_test::ACMRandom;
29
30 namespace {
31
32 #ifdef _MSC_VER
33 static int round(double x) {
34   if (x < 0)
35     return static_cast<int>(ceil(x - 0.5));
36   else
37     return static_cast<int>(floor(x + 0.5));
38 }
39 #endif
40
41 const int kNumCoeffs = 256;
42 const double PI = 3.1415926535898;
43 void reference2_16x16_idct_2d(double *input, double *output) {
44   double x;
45   for (int l = 0; l < 16; ++l) {
46     for (int k = 0; k < 16; ++k) {
47       double s = 0;
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;
53           if (i != 0)
54             x *= sqrt(2.0);
55           if (j != 0)
56             x *= sqrt(2.0);
57           s += x;
58         }
59       }
60       output[k*16+l] = s;
61     }
62   }
63 }
64
65
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;
81
82 void butterfly_16x16_dct_1d(double input[16], double output[16]) {
83   double step[16];
84   double intermediate[16];
85   double temp1, temp2;
86
87   // step 1
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];
104
105   // step 2
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];
114
115   temp1 = step[ 8] * C7;
116   temp2 = step[15] * C9;
117   output[ 8] = temp1 + temp2;
118
119   temp1 = step[ 9] * C11;
120   temp2 = step[14] * C5;
121   output[ 9] = temp1 - temp2;
122
123   temp1 = step[10] * C3;
124   temp2 = step[13] * C13;
125   output[10] = temp1 + temp2;
126
127   temp1 = step[11] * C15;
128   temp2 = step[12] * C1;
129   output[11] = temp1 - temp2;
130
131   temp1 = step[11] * C1;
132   temp2 = step[12] * C15;
133   output[12] = temp2 + temp1;
134
135   temp1 = step[10] * C13;
136   temp2 = step[13] * C3;
137   output[13] = temp2 - temp1;
138
139   temp1 = step[ 9] * C5;
140   temp2 = step[14] * C11;
141   output[14] = temp2 + temp1;
142
143   temp1 = step[ 8] * C9;
144   temp2 = step[15] * C7;
145   output[15] = temp2 - temp1;
146
147   // step 3
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];
152
153   temp1 = output[4] * C14;
154   temp2 = output[7] * C2;
155   step[ 4] = temp1 + temp2;
156
157   temp1 = output[5] * C10;
158   temp2 = output[6] * C6;
159   step[ 5] = temp1 + temp2;
160
161   temp1 = output[5] * C6;
162   temp2 = output[6] * C10;
163   step[ 6] = temp2 - temp1;
164
165   temp1 = output[4] * C2;
166   temp2 = output[7] * C14;
167   step[ 7] = temp2 - temp1;
168
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];
173
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];
178
179   // step 4
180   output[ 0] = (step[ 0] + step[ 1]);
181   output[ 8] = (step[ 0] - step[ 1]);
182
183   temp1 = step[2] * C12;
184   temp2 = step[3] * C4;
185   temp1 = temp1 + temp2;
186   output[ 4] = 2*(temp1 * C8);
187
188   temp1 = step[2] * C4;
189   temp2 = step[3] * C12;
190   temp1 = temp2 - temp1;
191   output[12] = 2 * (temp1 * C8);
192
193   output[ 2] = 2 * ((step[4] + step[ 5]) * C8);
194   output[14] = 2 * ((step[7] - step[ 6]) * C8);
195
196   temp1 = step[4] - step[5];
197   temp2 = step[6] + step[7];
198   output[ 6] = (temp1 + temp2);
199   output[10] = (temp1 - temp2);
200
201   intermediate[8] = step[8] + step[14];
202   intermediate[9] = step[9] + step[15];
203
204   temp1 = intermediate[8] * C12;
205   temp2 = intermediate[9] * C4;
206   temp1 = temp1 - temp2;
207   output[3] = 2 * (temp1 * C8);
208
209   temp1 = intermediate[8] * C4;
210   temp2 = intermediate[9] * C12;
211   temp1 = temp2 + temp1;
212   output[13] = 2 * (temp1 * C8);
213
214   output[ 9] = 2 * ((step[10] + step[11]) * C8);
215
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];
221
222   output[15] = (intermediate[11] + intermediate[12]);
223   output[ 1] = -(intermediate[11] - intermediate[12]);
224
225   output[ 7] = 2 * (intermediate[13] * C8);
226
227   temp1 = intermediate[14] * C12;
228   temp2 = intermediate[15] * C4;
229   temp1 = temp1 - temp2;
230   output[11] = -2 * (temp1 * C8);
231
232   temp1 = intermediate[14] * C4;
233   temp2 = intermediate[15] * C12;
234   temp1 = temp2 + temp1;
235   output[ 5] = 2 * (temp1 * C8);
236 }
237
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];
247   }
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;
257   }
258 }
259
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,
263                        int tx_type);
264 typedef void (*iht_t) (const int16_t *in, uint8_t *out, int stride,
265                        int tx_type);
266
267 void fdct16x16_ref(const int16_t *in, int16_t *out, int stride, int tx_type) {
268   vp9_fdct16x16_c(in, out, stride);
269 }
270
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);
273 }
274
275 class Trans16x16TestBase {
276  public:
277   virtual ~Trans16x16TestBase() {}
278
279  protected:
280   virtual void RunFwdTxfm(int16_t *in, int16_t *out, int stride) = 0;
281
282   virtual void RunInvTxfm(int16_t *out, uint8_t *dst, int stride) = 0;
283
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);
294
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];
300       }
301
302       REGISTER_STATE_CHECK(RunFwdTxfm(test_input_block,
303                                       test_temp_block, pitch_));
304       REGISTER_STATE_CHECK(RunInvTxfm(test_temp_block, dst, pitch_));
305
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)
310           max_error = error;
311         total_error += error;
312       }
313     }
314
315     EXPECT_GE(1u, max_error)
316         << "Error: 16x16 FHT/IHT has an individual round trip error > 1";
317
318     EXPECT_GE(count_test_block , total_error)
319         << "Error: 16x16 FHT/IHT has average round trip error > 1 per block";
320   }
321
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);
328
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();
333
334       fwd_txfm_ref(input_block, output_ref_block, pitch_, tx_type_);
335       REGISTER_STATE_CHECK(RunFwdTxfm(input_block, output_block, pitch_));
336
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]);
340     }
341   }
342
343   void RunMemCheck() {
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);
350
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;
356       }
357       if (i == 0)
358         for (int j = 0; j < kNumCoeffs; ++j)
359           input_extreme_block[j] = 255;
360       if (i == 1)
361         for (int j = 0; j < kNumCoeffs; ++j)
362           input_extreme_block[j] = -255;
363
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_));
367
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";
373       }
374     }
375   }
376
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);
384
385     for (int i = 0; i < count_test_block; ++i) {
386       double out_r[kNumCoeffs];
387
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];
393       }
394
395       reference_16x16_dct_2d(in, out_r);
396       for (int j = 0; j < kNumCoeffs; ++j)
397         coeff[j] = round(out_r[j]);
398
399       REGISTER_STATE_CHECK(RunInvTxfm(coeff, dst, 16));
400
401       for (int j = 0; j < kNumCoeffs; ++j) {
402         const uint32_t diff = dst[j] - src[j];
403         const uint32_t error = diff * diff;
404         EXPECT_GE(1u, error)
405             << "Error: 16x16 IDCT has error " << error
406             << " at index " << j;
407       }
408     }
409   }
410   int pitch_;
411   int tx_type_;
412   fht_t fwd_txfm_ref;
413 };
414
415 class Trans16x16DCT : public Trans16x16TestBase,
416                       public PARAMS(fdct_t, idct_t, int) {
417  public:
418   virtual ~Trans16x16DCT() {}
419
420   virtual void SetUp() {
421     fwd_txfm_ = GET_PARAM(0);
422     inv_txfm_ = GET_PARAM(1);
423     tx_type_  = GET_PARAM(2);
424     pitch_    = 16;
425     fwd_txfm_ref = fdct16x16_ref;
426   }
427   virtual void TearDown() { libvpx_test::ClearSystemState(); }
428
429  protected:
430   void RunFwdTxfm(int16_t *in, int16_t *out, int stride) {
431     fwd_txfm_(in, out, stride);
432   }
433   void RunInvTxfm(int16_t *out, uint8_t *dst, int stride) {
434     inv_txfm_(out, dst, stride);
435   }
436
437   fdct_t fwd_txfm_;
438   idct_t inv_txfm_;
439 };
440
441 TEST_P(Trans16x16DCT, AccuracyCheck) {
442   RunAccuracyCheck();
443 }
444
445 TEST_P(Trans16x16DCT, CoeffCheck) {
446   RunCoeffCheck();
447 }
448
449 TEST_P(Trans16x16DCT, MemCheck) {
450   RunMemCheck();
451 }
452
453 TEST_P(Trans16x16DCT, InvAccuracyCheck) {
454   RunInvAccuracyCheck();
455 }
456
457 class Trans16x16HT : public Trans16x16TestBase,
458                      public PARAMS(fht_t, iht_t, int) {
459  public:
460   virtual ~Trans16x16HT() {}
461
462   virtual void SetUp() {
463     fwd_txfm_ = GET_PARAM(0);
464     inv_txfm_ = GET_PARAM(1);
465     tx_type_  = GET_PARAM(2);
466     pitch_    = 16;
467     fwd_txfm_ref = fht16x16_ref;
468   }
469   virtual void TearDown() { libvpx_test::ClearSystemState(); }
470
471  protected:
472   void RunFwdTxfm(int16_t *in, int16_t *out, int stride) {
473     fwd_txfm_(in, out, stride, tx_type_);
474   }
475   void RunInvTxfm(int16_t *out, uint8_t *dst, int stride) {
476     inv_txfm_(out, dst, stride, tx_type_);
477   }
478
479   fht_t fwd_txfm_;
480   iht_t inv_txfm_;
481 };
482
483 TEST_P(Trans16x16HT, AccuracyCheck) {
484   RunAccuracyCheck();
485 }
486
487 TEST_P(Trans16x16HT, CoeffCheck) {
488   RunCoeffCheck();
489 }
490
491 TEST_P(Trans16x16HT, MemCheck) {
492   RunMemCheck();
493 }
494
495 using std::tr1::make_tuple;
496
497 INSTANTIATE_TEST_CASE_P(
498     C, Trans16x16DCT,
499     ::testing::Values(
500         make_tuple(&vp9_fdct16x16_c, &vp9_idct16x16_256_add_c, 0)));
501 INSTANTIATE_TEST_CASE_P(
502     C, Trans16x16HT,
503     ::testing::Values(
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)));
508
509 #if HAVE_SSE2
510 INSTANTIATE_TEST_CASE_P(
511     SSE2, Trans16x16DCT,
512     ::testing::Values(
513         make_tuple(&vp9_fdct16x16_sse2,
514                    &vp9_idct16x16_256_add_sse2, 0)));
515 INSTANTIATE_TEST_CASE_P(
516     SSE2, Trans16x16HT,
517     ::testing::Values(
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)));
522 #endif
523 }  // namespace