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 
17 #include "./vp9_rtcd.h"
18 
19 #include "test/acm_random.h"
20 #include "vpx/vpx_integer.h"
21 
22 using libvpx_test::ACMRandom;
23 
24 namespace {
25 
26 #ifdef _MSC_VER
round(double x)27 static int round(double x) {
28   if (x < 0)
29     return static_cast<int>(ceil(x - 0.5));
30   else
31     return static_cast<int>(floor(x + 0.5));
32 }
33 #endif
34 
reference_dct_1d(double input[8],double output[8])35 void reference_dct_1d(double input[8], double output[8]) {
36   const double kPi = 3.141592653589793238462643383279502884;
37   const double kInvSqrt2 = 0.707106781186547524400844362104;
38   for (int k = 0; k < 8; k++) {
39     output[k] = 0.0;
40     for (int n = 0; n < 8; n++)
41       output[k] += input[n]*cos(kPi*(2*n+1)*k/16.0);
42     if (k == 0)
43       output[k] = output[k]*kInvSqrt2;
44   }
45 }
46 
reference_dct_2d(int16_t input[64],double output[64])47 void reference_dct_2d(int16_t input[64], double output[64]) {
48   // First transform columns
49   for (int i = 0; i < 8; ++i) {
50     double temp_in[8], temp_out[8];
51     for (int j = 0; j < 8; ++j)
52       temp_in[j] = input[j*8 + i];
53     reference_dct_1d(temp_in, temp_out);
54     for (int j = 0; j < 8; ++j)
55       output[j*8 + i] = temp_out[j];
56   }
57   // Then transform rows
58   for (int i = 0; i < 8; ++i) {
59     double temp_in[8], temp_out[8];
60     for (int j = 0; j < 8; ++j)
61       temp_in[j] = output[j + i*8];
62     reference_dct_1d(temp_in, temp_out);
63     for (int j = 0; j < 8; ++j)
64       output[j + i*8] = temp_out[j];
65   }
66   // Scale by some magic number
67   for (int i = 0; i < 64; ++i)
68     output[i] *= 2;
69 }
70 
reference_idct_1d(double input[8],double output[8])71 void reference_idct_1d(double input[8], double output[8]) {
72   const double kPi = 3.141592653589793238462643383279502884;
73   const double kSqrt2 = 1.414213562373095048801688724209698;
74   for (int k = 0; k < 8; k++) {
75     output[k] = 0.0;
76     for (int n = 0; n < 8; n++) {
77       output[k] += input[n]*cos(kPi*(2*k+1)*n/16.0);
78       if (n == 0)
79         output[k] = output[k]/kSqrt2;
80     }
81   }
82 }
83 
reference_idct_2d(double input[64],int16_t output[64])84 void reference_idct_2d(double input[64], int16_t output[64]) {
85   double out[64], out2[64];
86   // First transform rows
87   for (int i = 0; i < 8; ++i) {
88     double temp_in[8], temp_out[8];
89     for (int j = 0; j < 8; ++j)
90       temp_in[j] = input[j + i*8];
91     reference_idct_1d(temp_in, temp_out);
92     for (int j = 0; j < 8; ++j)
93       out[j + i*8] = temp_out[j];
94   }
95   // Then transform columns
96   for (int i = 0; i < 8; ++i) {
97     double temp_in[8], temp_out[8];
98     for (int j = 0; j < 8; ++j)
99       temp_in[j] = out[j*8 + i];
100     reference_idct_1d(temp_in, temp_out);
101     for (int j = 0; j < 8; ++j)
102       out2[j*8 + i] = temp_out[j];
103   }
104   for (int i = 0; i < 64; ++i)
105     output[i] = round(out2[i]/32);
106 }
107 
TEST(VP9Idct8x8Test,AccuracyCheck)108 TEST(VP9Idct8x8Test, AccuracyCheck) {
109   ACMRandom rnd(ACMRandom::DeterministicSeed());
110   const int count_test_block = 10000;
111   for (int i = 0; i < count_test_block; ++i) {
112     int16_t input[64], coeff[64];
113     double output_r[64];
114     uint8_t dst[64], src[64];
115 
116     for (int j = 0; j < 64; ++j) {
117       src[j] = rnd.Rand8();
118       dst[j] = rnd.Rand8();
119     }
120     // Initialize a test block with input range [-255, 255].
121     for (int j = 0; j < 64; ++j)
122       input[j] = src[j] - dst[j];
123 
124     reference_dct_2d(input, output_r);
125     for (int j = 0; j < 64; ++j)
126       coeff[j] = round(output_r[j]);
127     vp9_idct8x8_64_add_c(coeff, dst, 8);
128     for (int j = 0; j < 64; ++j) {
129       const int diff = dst[j] - src[j];
130       const int error = diff * diff;
131       EXPECT_GE(1, error)
132           << "Error: 8x8 FDCT/IDCT has error " << error
133           << " at index " << j;
134     }
135   }
136 }
137 
138 }  // namespace
139