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