fdct8x8_test.cc 8.04 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
/*
 *  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"
16
17
#include "test/clear_system_state.h"
#include "test/register_state_check.h"
18
#include "vpx_ports/mem.h"
Daniel Kang's avatar
Daniel Kang committed
19
20

extern "C" {
21
22
#include "./vp9_rtcd.h"
void vp9_short_idct8x8_add_c(int16_t *input, uint8_t *output, int pitch);
Daniel Kang's avatar
Daniel Kang committed
23
24
}

25
#include "test/acm_random.h"
Daniel Kang's avatar
Daniel Kang committed
26
27
28
29
30
#include "vpx/vpx_integer.h"

using libvpx_test::ACMRandom;

namespace {
James Zern's avatar
James Zern committed
31
32
void fdct8x8(int16_t *in, int16_t *out, uint8_t* /*dst*/,
             int stride, int /*tx_type*/) {
Jingning Han's avatar
Jingning Han committed
33
34
  vp9_short_fdct8x8_c(in, out, stride);
}
James Zern's avatar
James Zern committed
35
36
void idct8x8_add(int16_t* /*in*/, int16_t *out, uint8_t *dst,
                 int stride, int /*tx_type*/) {
Jingning Han's avatar
Jingning Han committed
37
38
  vp9_short_idct8x8_add_c(out, dst, stride >> 1);
}
James Zern's avatar
James Zern committed
39
40
void fht8x8(int16_t *in, int16_t *out, uint8_t* /*dst*/,
            int stride, int tx_type) {
41
42
43
44
45
  // TODO(jingning): need to refactor this to test both _c and _sse2 functions,
  // when we have all inverse dct functions done sse2.
#if HAVE_SSE2
  vp9_short_fht8x8_sse2(in, out, stride >> 1, tx_type);
#else
Jingning Han's avatar
Jingning Han committed
46
  vp9_short_fht8x8_c(in, out, stride >> 1, tx_type);
47
#endif
Jingning Han's avatar
Jingning Han committed
48
}
James Zern's avatar
James Zern committed
49
void iht8x8_add(int16_t* /*in*/, int16_t *out, uint8_t *dst,
Jingning Han's avatar
Jingning Han committed
50
51
52
53
54
55
                int stride, int tx_type) {
  vp9_short_iht8x8_add_c(out, dst, stride >> 1, tx_type);
}

class FwdTrans8x8Test : public ::testing::TestWithParam<int> {
 public:
56
57
  virtual ~FwdTrans8x8Test() {}
  virtual void SetUp() {
Jingning Han's avatar
Jingning Han committed
58
59
60
61
62
63
64
65
66
    tx_type_ = GetParam();
    if (tx_type_ == 0) {
      fwd_txfm = fdct8x8;
      inv_txfm = idct8x8_add;
    } else {
      fwd_txfm = fht8x8;
      inv_txfm = iht8x8_add;
    }
  }
67
  virtual void TearDown() { libvpx_test::ClearSystemState(); }
Jingning Han's avatar
Jingning Han committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

 protected:
  void RunFwdTxfm(int16_t *in, int16_t *out, uint8_t *dst,
                  int stride, int tx_type) {
    (*fwd_txfm)(in, out, dst, stride, tx_type);
  }
  void RunInvTxfm(int16_t *in, int16_t *out, uint8_t *dst,
                  int stride, int tx_type) {
    (*inv_txfm)(in, out, dst, stride, tx_type);
  }

  int tx_type_;
  void (*fwd_txfm)(int16_t*, int16_t*, uint8_t*, int, int);
  void (*inv_txfm)(int16_t*, int16_t*, uint8_t*, int, int);
};
Daniel Kang's avatar
Daniel Kang committed
83

Jingning Han's avatar
Jingning Han committed
84
TEST_P(FwdTrans8x8Test, SignBiasCheck) {
Daniel Kang's avatar
Daniel Kang committed
85
  ACMRandom rnd(ACMRandom::DeterministicSeed());
86
87
  DECLARE_ALIGNED_ARRAY(16, int16_t, test_input_block, 64);
  DECLARE_ALIGNED_ARRAY(16, int16_t, test_output_block, 64);
Daniel Kang's avatar
Daniel Kang committed
88
89
90
91
92
93
94
95
96
97
  const int pitch = 16;
  int count_sign_block[64][2];
  const int count_test_block = 100000;

  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 < 64; ++j)
      test_input_block[j] = rnd.Rand8() - rnd.Rand8();
98
99
100
    REGISTER_STATE_CHECK(
        RunFwdTxfm(test_input_block, test_output_block,
                   NULL, pitch, tx_type_));
Daniel Kang's avatar
Daniel Kang committed
101
102
103
104
105
106
107
108
109
110

    for (int j = 0; j < 64; ++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 < 64; ++j) {
111
112
113
    const int diff = abs(count_sign_block[j][0] - count_sign_block[j][1]);
    const int max_diff = 1125;
    EXPECT_LT(diff, max_diff)
Jingning Han's avatar
Jingning Han committed
114
        << "Error: 8x8 FDCT/FHT has a sign bias > "
115
116
117
118
119
        << 1. * max_diff / count_test_block * 100 << "%"
        << " for input range [-255, 255] at index " << j
        << " count0: " << count_sign_block[j][0]
        << " count1: " << count_sign_block[j][1]
        << " diff: " << diff;
Daniel Kang's avatar
Daniel Kang committed
120
121
122
123
124
125
126
127
  }

  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 < 64; ++j)
      test_input_block[j] = (rnd.Rand8() >> 4) - (rnd.Rand8() >> 4);
128
129
130
    REGISTER_STATE_CHECK(
        RunFwdTxfm(test_input_block, test_output_block,
                   NULL, pitch, tx_type_));
Daniel Kang's avatar
Daniel Kang committed
131
132
133
134
135
136
137
138
139
140

    for (int j = 0; j < 64; ++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 < 64; ++j) {
141
142
143
    const int diff = abs(count_sign_block[j][0] - count_sign_block[j][1]);
    const int max_diff = 10000;
    EXPECT_LT(diff, max_diff)
Jingning Han's avatar
Jingning Han committed
144
        << "Error: 4x4 FDCT/FHT has a sign bias > "
145
146
147
148
149
        << 1. * max_diff / count_test_block * 100 << "%"
        << " for input range [-15, 15] at index " << j
        << " count0: " << count_sign_block[j][0]
        << " count1: " << count_sign_block[j][1]
        << " diff: " << diff;
Daniel Kang's avatar
Daniel Kang committed
150
  }
Jingning Han's avatar
Jingning Han committed
151
}
Daniel Kang's avatar
Daniel Kang committed
152

Jingning Han's avatar
Jingning Han committed
153
TEST_P(FwdTrans8x8Test, RoundTripErrorCheck) {
Daniel Kang's avatar
Daniel Kang committed
154
155
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  int max_error = 0;
156
  int total_error = 0;
Daniel Kang's avatar
Daniel Kang committed
157
158
  const int count_test_block = 100000;
  for (int i = 0; i < count_test_block; ++i) {
159
160
161
162
    DECLARE_ALIGNED_ARRAY(16, int16_t, test_input_block, 64);
    DECLARE_ALIGNED_ARRAY(16, int16_t, test_temp_block, 64);
    DECLARE_ALIGNED_ARRAY(16, uint8_t, dst, 64);
    DECLARE_ALIGNED_ARRAY(16, uint8_t, src, 64);
Daniel Kang's avatar
Daniel Kang committed
163

Scott LaVarnway's avatar
Scott LaVarnway committed
164
165
166
167
    for (int j = 0; j < 64; ++j) {
      src[j] = rnd.Rand8();
      dst[j] = rnd.Rand8();
    }
Daniel Kang's avatar
Daniel Kang committed
168
169
    // Initialize a test block with input range [-255, 255].
    for (int j = 0; j < 64; ++j)
Scott LaVarnway's avatar
Scott LaVarnway committed
170
      test_input_block[j] = src[j] - dst[j];
Daniel Kang's avatar
Daniel Kang committed
171
172

    const int pitch = 16;
173
174
175
176
177
    REGISTER_STATE_CHECK(
        RunFwdTxfm(test_input_block, test_temp_block,
                   dst, pitch, tx_type_));
    for (int j = 0; j < 64; ++j) {
        if (test_temp_block[j] > 0) {
178
179
180
181
182
183
184
185
186
          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;
        }
    }
187
188
189
    REGISTER_STATE_CHECK(
        RunInvTxfm(test_input_block, test_temp_block,
                   dst, pitch, tx_type_));
Daniel Kang's avatar
Daniel Kang committed
190
191

    for (int j = 0; j < 64; ++j) {
Scott LaVarnway's avatar
Scott LaVarnway committed
192
      const int diff = dst[j] - src[j];
Daniel Kang's avatar
Daniel Kang committed
193
194
195
196
197
198
199
200
      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
201
    << "Error: 8x8 FDCT/IDCT or FHT/IHT has an individual roundtrip error > 1";
Daniel Kang's avatar
Daniel Kang committed
202

Daniel Kang's avatar
Daniel Kang committed
203
  EXPECT_GE(count_test_block/5, total_error)
Jingning Han's avatar
Jingning Han committed
204
205
206
    << "Error: 8x8 FDCT/IDCT or FHT/IHT has average roundtrip "
        "error > 1/5 per block";
}
Daniel Kang's avatar
Daniel Kang committed
207

Jingning Han's avatar
Jingning Han committed
208
TEST_P(FwdTrans8x8Test, ExtremalCheck) {
Daniel Kang's avatar
Daniel Kang committed
209
210
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  int max_error = 0;
211
  int total_error = 0;
Daniel Kang's avatar
Daniel Kang committed
212
213
  const int count_test_block = 100000;
  for (int i = 0; i < count_test_block; ++i) {
214
215
216
217
    DECLARE_ALIGNED_ARRAY(16, int16_t, test_input_block, 64);
    DECLARE_ALIGNED_ARRAY(16, int16_t, test_temp_block, 64);
    DECLARE_ALIGNED_ARRAY(16, uint8_t, dst, 64);
    DECLARE_ALIGNED_ARRAY(16, uint8_t, src, 64);
Daniel Kang's avatar
Daniel Kang committed
218

Scott LaVarnway's avatar
Scott LaVarnway committed
219
220
221
222
223
    for (int j = 0; j < 64; ++j) {
      src[j] = rnd.Rand8() % 2 ? 255 : 0;
      dst[j] = src[j] > 0 ? 0 : 255;
    }
    // Initialize a test block with input range [-255, 255].
Daniel Kang's avatar
Daniel Kang committed
224
    for (int j = 0; j < 64; ++j)
Scott LaVarnway's avatar
Scott LaVarnway committed
225
      test_input_block[j] = src[j] - dst[j];
Daniel Kang's avatar
Daniel Kang committed
226
227

    const int pitch = 16;
228
229
230
231
232
233
    REGISTER_STATE_CHECK(
        RunFwdTxfm(test_input_block, test_temp_block,
                   dst, pitch, tx_type_));
    REGISTER_STATE_CHECK(
        RunInvTxfm(test_input_block, test_temp_block,
                   dst, pitch, tx_type_));
Daniel Kang's avatar
Daniel Kang committed
234
235

    for (int j = 0; j < 64; ++j) {
Scott LaVarnway's avatar
Scott LaVarnway committed
236
      const int diff = dst[j] - src[j];
Daniel Kang's avatar
Daniel Kang committed
237
238
239
240
241
242
243
      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
244
        << "Error: Extremal 8x8 FDCT/IDCT or FHT/IHT has an"
Daniel Kang's avatar
Daniel Kang committed
245
246
        << " individual roundtrip error > 1";

Daniel Kang's avatar
Daniel Kang committed
247
    EXPECT_GE(count_test_block/5, total_error)
Jingning Han's avatar
Jingning Han committed
248
        << "Error: Extremal 8x8 FDCT/IDCT or FHT/IHT has average"
Daniel Kang's avatar
Daniel Kang committed
249
        << " roundtrip error > 1/5 per block";
Daniel Kang's avatar
Daniel Kang committed
250
  }
Jingning Han's avatar
Jingning Han committed
251
}
Daniel Kang's avatar
Daniel Kang committed
252

Jingning Han's avatar
Jingning Han committed
253
INSTANTIATE_TEST_CASE_P(VP9, FwdTrans8x8Test, ::testing::Range(0, 4));
Daniel Kang's avatar
Daniel Kang committed
254
}  // namespace