blob: 25465c53c2f998f2d8d7442d25f698372b84e5a5 [file] [log] [blame]
John Koleszar7b8dfcb2012-11-06 16:59:01 -08001/*
Frank Galligan38536f62013-12-12 08:36:34 -08002 * Copyright (c) 2013 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 */
John Koleszar7b8dfcb2012-11-06 16:59:01 -080010
11#include <math.h>
12#include <stddef.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <sys/types.h>
17
John Koleszar7b8dfcb2012-11-06 16:59:01 -080018extern "C" {
Yaowu Xuafffa3d2013-09-05 08:45:56 -070019#include "./vp8_rtcd.h"
John Koleszar7b8dfcb2012-11-06 16:59:01 -080020}
21
22#include "test/acm_random.h"
23#include "third_party/googletest/src/include/gtest/gtest.h"
24#include "vpx/vpx_integer.h"
25
John Koleszar7b8dfcb2012-11-06 16:59:01 -080026namespace {
27
28const int cospi8sqrt2minus1 = 20091;
29const int sinpi8sqrt2 = 35468;
30
31void reference_idct4x4(const int16_t *input, int16_t *output) {
32 const int16_t *ip = input;
33 int16_t *op = output;
34
35 for (int i = 0; i < 4; ++i) {
36 const int a1 = ip[0] + ip[8];
37 const int b1 = ip[0] - ip[8];
38 const int temp1 = (ip[4] * sinpi8sqrt2) >> 16;
39 const int temp2 = ip[12] + ((ip[12] * cospi8sqrt2minus1) >> 16);
40 const int c1 = temp1 - temp2;
41 const int temp3 = ip[4] + ((ip[4] * cospi8sqrt2minus1) >> 16);
42 const int temp4 = (ip[12] * sinpi8sqrt2) >> 16;
43 const int d1 = temp3 + temp4;
44 op[0] = a1 + d1;
45 op[12] = a1 - d1;
46 op[4] = b1 + c1;
47 op[8] = b1 - c1;
48 ++ip;
49 ++op;
50 }
51 ip = output;
52 op = output;
53 for (int i = 0; i < 4; ++i) {
54 const int a1 = ip[0] + ip[2];
55 const int b1 = ip[0] - ip[2];
56 const int temp1 = (ip[1] * sinpi8sqrt2) >> 16;
57 const int temp2 = ip[3] + ((ip[3] * cospi8sqrt2minus1) >> 16);
58 const int c1 = temp1 - temp2;
59 const int temp3 = ip[1] + ((ip[1] * cospi8sqrt2minus1) >> 16);
60 const int temp4 = (ip[3] * sinpi8sqrt2) >> 16;
61 const int d1 = temp3 + temp4;
62 op[0] = (a1 + d1 + 4) >> 3;
63 op[3] = (a1 - d1 + 4) >> 3;
64 op[1] = (b1 + c1 + 4) >> 3;
65 op[2] = (b1 - c1 + 4) >> 3;
66 ip += 4;
67 op += 4;
68 }
69}
70
71using libvpx_test::ACMRandom;
72
73TEST(Vp8FdctTest, SignBiasCheck) {
74 ACMRandom rnd(ACMRandom::DeterministicSeed());
75 int16_t test_input_block[16];
76 int16_t test_output_block[16];
77 const int pitch = 8;
78 int count_sign_block[16][2];
79 const int count_test_block = 1000000;
80
81 memset(count_sign_block, 0, sizeof(count_sign_block));
82
83 for (int i = 0; i < count_test_block; ++i) {
84 // Initialize a test block with input range [-255, 255].
85 for (int j = 0; j < 16; ++j)
86 test_input_block[j] = rnd.Rand8() - rnd.Rand8();
87
88 vp8_short_fdct4x4_c(test_input_block, test_output_block, pitch);
89
90 for (int j = 0; j < 16; ++j) {
91 if (test_output_block[j] < 0)
92 ++count_sign_block[j][0];
93 else if (test_output_block[j] > 0)
94 ++count_sign_block[j][1];
95 }
96 }
97
98 bool bias_acceptable = true;
99 for (int j = 0; j < 16; ++j)
100 bias_acceptable = bias_acceptable &&
101 (abs(count_sign_block[j][0] - count_sign_block[j][1]) < 10000);
102
103 EXPECT_EQ(true, bias_acceptable)
104 << "Error: 4x4 FDCT has a sign bias > 1% for input range [-255, 255]";
105
106 memset(count_sign_block, 0, sizeof(count_sign_block));
107
108 for (int i = 0; i < count_test_block; ++i) {
109 // Initialize a test block with input range [-15, 15].
110 for (int j = 0; j < 16; ++j)
111 test_input_block[j] = (rnd.Rand8() >> 4) - (rnd.Rand8() >> 4);
112
113 vp8_short_fdct4x4_c(test_input_block, test_output_block, pitch);
114
115 for (int j = 0; j < 16; ++j) {
116 if (test_output_block[j] < 0)
117 ++count_sign_block[j][0];
118 else if (test_output_block[j] > 0)
119 ++count_sign_block[j][1];
120 }
121 }
122
123 bias_acceptable = true;
124 for (int j = 0; j < 16; ++j)
125 bias_acceptable = bias_acceptable &&
126 (abs(count_sign_block[j][0] - count_sign_block[j][1]) < 100000);
127
128 EXPECT_EQ(true, bias_acceptable)
129 << "Error: 4x4 FDCT has a sign bias > 10% for input range [-15, 15]";
130};
131
132TEST(Vp8FdctTest, RoundTripErrorCheck) {
133 ACMRandom rnd(ACMRandom::DeterministicSeed());
134 int max_error = 0;
135 double total_error = 0;
136 const int count_test_block = 1000000;
137 for (int i = 0; i < count_test_block; ++i) {
138 int16_t test_input_block[16];
139 int16_t test_temp_block[16];
140 int16_t test_output_block[16];
141
142 // Initialize a test block with input range [-255, 255].
143 for (int j = 0; j < 16; ++j)
144 test_input_block[j] = rnd.Rand8() - rnd.Rand8();
145
146 const int pitch = 8;
147 vp8_short_fdct4x4_c(test_input_block, test_temp_block, pitch);
148 reference_idct4x4(test_temp_block, test_output_block);
149
150 for (int j = 0; j < 16; ++j) {
151 const int diff = test_input_block[j] - test_output_block[j];
152 const int error = diff * diff;
153 if (max_error < error)
154 max_error = error;
155 total_error += error;
156 }
157 }
158
159 EXPECT_GE(1, max_error )
160 << "Error: FDCT/IDCT has an individual roundtrip error > 1";
161
162 EXPECT_GE(count_test_block, total_error)
163 << "Error: FDCT/IDCT has average roundtrip error > 1 per block";
164};
165
166} // namespace