fdct4x4_test.cc 5.9 KB
Newer Older
Daniel Kang's avatar
Daniel Kang committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "third_party/googletest/src/include/gtest/gtest.h"

extern "C" {
18
#include "vp9_rtcd.h"
Daniel Kang's avatar
Daniel Kang committed
19
20
}

Daniel Kang's avatar
Daniel Kang committed
21
#include "acm_random.h"
Daniel Kang's avatar
Daniel Kang committed
22
#include "vpx/vpx_integer.h"
23
#include "vpx_ports/mem.h"
Daniel Kang's avatar
Daniel Kang committed
24

Daniel Kang's avatar
Daniel Kang committed
25
using libvpx_test::ACMRandom;
Daniel Kang's avatar
Daniel Kang committed
26

Daniel Kang's avatar
Daniel Kang committed
27
namespace {
James Zern's avatar
James Zern committed
28
void fdct4x4(int16_t *in, int16_t *out, uint8_t* /*dst*/,
29
             int stride, int /*tx_type*/) {
Jingning Han's avatar
Jingning Han committed
30
31
  vp9_short_fdct4x4_c(in, out, stride);
}
James Zern's avatar
James Zern committed
32
void idct4x4_add(int16_t* /*in*/, int16_t *out, uint8_t *dst,
33
                 int stride, int /*tx_type*/) {
Jingning Han's avatar
Jingning Han committed
34
35
  vp9_short_idct4x4_add_c(out, dst, stride >> 1);
}
James Zern's avatar
James Zern committed
36
void fht4x4(int16_t *in, int16_t *out, uint8_t* /*dst*/,
37
            int stride, int tx_type) {
Jingning Han's avatar
Jingning Han committed
38
39
  vp9_short_fht4x4_c(in, out, stride >> 1, tx_type);
}
James Zern's avatar
James Zern committed
40
void iht4x4_add(int16_t* /*in*/, int16_t *out, uint8_t *dst,
Jingning Han's avatar
Jingning Han committed
41
42
43
44
45
46
                int stride, int tx_type) {
  vp9_short_iht4x4_add_c(out, dst, stride >> 1, tx_type);
}

class FwdTrans4x4Test : public ::testing::TestWithParam<int> {
 public:
Jingning Han's avatar
Jingning Han committed
47
  FwdTrans4x4Test() { SetUpTestTxfm(); }
Jingning Han's avatar
Jingning Han committed
48
49
50
  ~FwdTrans4x4Test() {}

  void SetUpTestTxfm() {
Jingning Han's avatar
Jingning Han committed
51
52
    tx_type_ = GetParam();
    if (tx_type_ == 0) {
53
54
      fwd_txfm_ = fdct4x4;
      inv_txfm_ = idct4x4_add;
Jingning Han's avatar
Jingning Han committed
55
    } else {
56
57
      fwd_txfm_ = fht4x4;
      inv_txfm_ = iht4x4_add;
Jingning Han's avatar
Jingning Han committed
58
59
60
61
62
63
    }
  }

 protected:
  void RunFwdTxfm(int16_t *in, int16_t *out, uint8_t *dst,
                  int stride, int tx_type) {
64
    (*fwd_txfm_)(in, out, dst, stride, tx_type);
Jingning Han's avatar
Jingning Han committed
65
66
67
68
  }

  void RunInvTxfm(int16_t *in, int16_t *out, uint8_t *dst,
                  int stride, int tx_type) {
69
    (*inv_txfm_)(in, out, dst, stride, tx_type);
Jingning Han's avatar
Jingning Han committed
70
71
  }

Jingning Han's avatar
Jingning Han committed
72
  int tx_type_;
73
  void (*fwd_txfm_)(int16_t *in, int16_t *out, uint8_t *dst,
Jingning Han's avatar
Jingning Han committed
74
                   int stride, int tx_type);
75
  void (*inv_txfm_)(int16_t *in, int16_t *out, uint8_t *dst,
Jingning Han's avatar
Jingning Han committed
76
77
                   int stride, int tx_type);
};
Daniel Kang's avatar
Daniel Kang committed
78

Jingning Han's avatar
Jingning Han committed
79
TEST_P(FwdTrans4x4Test, SignBiasCheck) {
Daniel Kang's avatar
Daniel Kang committed
80
  ACMRandom rnd(ACMRandom::DeterministicSeed());
81
82
  DECLARE_ALIGNED_ARRAY(16, int16_t, test_input_block, 16);
  DECLARE_ALIGNED_ARRAY(16, int16_t, test_output_block, 16);
Daniel Kang's avatar
Daniel Kang committed
83
84
85
86
87
88
89
90
91
92
  const int pitch = 8;
  int count_sign_block[16][2];
  const int count_test_block = 1000000;

  memset(count_sign_block, 0, sizeof(count_sign_block));
  for (int i = 0; i < count_test_block; ++i) {
    // Initialize a test block with input range [-255, 255].
    for (int j = 0; j < 16; ++j)
      test_input_block[j] = rnd.Rand8() - rnd.Rand8();

Jingning Han's avatar
Jingning Han committed
93
    RunFwdTxfm(test_input_block, test_output_block, NULL, pitch, tx_type_);
Daniel Kang's avatar
Daniel Kang committed
94
95
96
97
98
99
100
101
102
103
104
105
106

    for (int j = 0; j < 16; ++j) {
      if (test_output_block[j] < 0)
        ++count_sign_block[j][0];
      else if (test_output_block[j] > 0)
        ++count_sign_block[j][1];
    }
  }

  for (int j = 0; j < 16; ++j) {
    const bool bias_acceptable = (abs(count_sign_block[j][0] -
                                      count_sign_block[j][1]) < 10000);
    EXPECT_TRUE(bias_acceptable)
Jingning Han's avatar
Jingning Han committed
107
108
        << "Error: 4x4 FDCT/FHT has a sign bias > 1%"
        << " for input range [-255, 255] at index " << j
Jingning Han's avatar
Jingning Han committed
109
        << " tx_type " << tx_type_;
Daniel Kang's avatar
Daniel Kang committed
110
111
112
113
114
115
116
117
  }

  memset(count_sign_block, 0, sizeof(count_sign_block));
  for (int i = 0; i < count_test_block; ++i) {
    // Initialize a test block with input range [-15, 15].
    for (int j = 0; j < 16; ++j)
      test_input_block[j] = (rnd.Rand8() >> 4) - (rnd.Rand8() >> 4);

Jingning Han's avatar
Jingning Han committed
118
    RunFwdTxfm(test_input_block, test_output_block, NULL, pitch, tx_type_);
Daniel Kang's avatar
Daniel Kang committed
119
120
121
122
123
124
125
126
127
128
129
130
131

    for (int j = 0; j < 16; ++j) {
      if (test_output_block[j] < 0)
        ++count_sign_block[j][0];
      else if (test_output_block[j] > 0)
        ++count_sign_block[j][1];
    }
  }

  for (int j = 0; j < 16; ++j) {
    const bool bias_acceptable = (abs(count_sign_block[j][0] -
                                      count_sign_block[j][1]) < 100000);
    EXPECT_TRUE(bias_acceptable)
Jingning Han's avatar
Jingning Han committed
132
        << "Error: 4x4 FDCT/FHT has a sign bias > 10%"
Daniel Kang's avatar
Daniel Kang committed
133
134
        << " for input range [-15, 15] at index " << j;
  }
Jingning Han's avatar
Jingning Han committed
135
}
Daniel Kang's avatar
Daniel Kang committed
136

Jingning Han's avatar
Jingning Han committed
137
TEST_P(FwdTrans4x4Test, RoundTripErrorCheck) {
Daniel Kang's avatar
Daniel Kang committed
138
  ACMRandom rnd(ACMRandom::DeterministicSeed());
Jingning Han's avatar
Jingning Han committed
139

Daniel Kang's avatar
Daniel Kang committed
140
141
142
143
  int max_error = 0;
  double total_error = 0;
  const int count_test_block = 1000000;
  for (int i = 0; i < count_test_block; ++i) {
144
145
146
147
    DECLARE_ALIGNED_ARRAY(16, int16_t, test_input_block, 16);
    DECLARE_ALIGNED_ARRAY(16, int16_t, test_temp_block, 16);
    DECLARE_ALIGNED_ARRAY(16, uint8_t, dst, 16);
    DECLARE_ALIGNED_ARRAY(16, uint8_t, src, 16);
Daniel Kang's avatar
Daniel Kang committed
148

Scott LaVarnway's avatar
Scott LaVarnway committed
149
150
151
152
    for (int j = 0; j < 16; ++j) {
      src[j] = rnd.Rand8();
      dst[j] = rnd.Rand8();
    }
Daniel Kang's avatar
Daniel Kang committed
153
154
    // Initialize a test block with input range [-255, 255].
    for (int j = 0; j < 16; ++j)
Scott LaVarnway's avatar
Scott LaVarnway committed
155
      test_input_block[j] = src[j] - dst[j];
Daniel Kang's avatar
Daniel Kang committed
156
157

    const int pitch = 8;
Jingning Han's avatar
Jingning Han committed
158
    RunFwdTxfm(test_input_block, test_temp_block, dst, pitch, tx_type_);
159
160
161
162
163
164
165
166
167
168
169
170
171

    for (int j = 0; j < 16; ++j) {
        if(test_temp_block[j] > 0) {
          test_temp_block[j] += 2;
          test_temp_block[j] /= 4;
          test_temp_block[j] *= 4;
        } else {
          test_temp_block[j] -= 2;
          test_temp_block[j] /= 4;
          test_temp_block[j] *= 4;
        }
    }

Jingning Han's avatar
Jingning Han committed
172
    // inverse transform and reconstruct the pixel block
Jingning Han's avatar
Jingning Han committed
173
    RunInvTxfm(test_input_block, test_temp_block, dst, pitch, tx_type_);
Daniel Kang's avatar
Daniel Kang committed
174
175

    for (int j = 0; j < 16; ++j) {
Scott LaVarnway's avatar
Scott LaVarnway committed
176
      const int diff = dst[j] - src[j];
Daniel Kang's avatar
Daniel Kang committed
177
178
179
180
181
182
183
      const int error = diff * diff;
      if (max_error < error)
        max_error = error;
      total_error += error;
    }
  }
  EXPECT_GE(1, max_error)
Jingning Han's avatar
Jingning Han committed
184
      << "Error: FDCT/IDCT or FHT/IHT has an individual roundtrip error > 1";
Daniel Kang's avatar
Daniel Kang committed
185
186

  EXPECT_GE(count_test_block, total_error)
Jingning Han's avatar
Jingning Han committed
187
      << "Error: FDCT/IDCT or FHT/IHT has average "
188
      << "roundtrip error > 1 per block";
Jingning Han's avatar
Jingning Han committed
189
}
Daniel Kang's avatar
Daniel Kang committed
190

Jingning Han's avatar
Jingning Han committed
191
INSTANTIATE_TEST_CASE_P(VP9, FwdTrans4x4Test, ::testing::Range(0, 4));
Daniel Kang's avatar
Daniel Kang committed
192
}  // namespace