fdct8x8_test.cc 11.1 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
#include "test/acm_random.h"
17
18
#include "test/clear_system_state.h"
#include "test/register_state_check.h"
19
#include "test/util.h"
Daniel Kang's avatar
Daniel Kang committed
20

21
#include "./vp9_rtcd.h"
22
23
24
25
#include "vp9/common/vp9_entropy.h"
#include "vpx/vpx_integer.h"

extern "C" {
26
void vp9_idct8x8_64_add_c(const int16_t *input, uint8_t *output, int pitch);
Daniel Kang's avatar
Daniel Kang committed
27
28
29
30
31
}

using libvpx_test::ACMRandom;

namespace {
32
33
34
35
36
37
38
typedef void (*fdct_t)(const int16_t *in, int16_t *out, int stride);
typedef void (*idct_t)(const int16_t *in, uint8_t *out, int stride);
typedef void (*fht_t) (const int16_t *in, int16_t *out, int stride,
                       int tx_type);
typedef void (*iht_t) (const int16_t *in, uint8_t *out, int stride,
                       int tx_type);

39
40
41
typedef std::tr1::tuple<fdct_t, idct_t, int> dct_8x8_param_t;
typedef std::tr1::tuple<fht_t, iht_t, int> ht_8x8_param_t;

42
void fdct8x8_ref(const int16_t *in, int16_t *out, int stride, int tx_type) {
43
  vp9_fdct8x8_c(in, out, stride);
Jingning Han's avatar
Jingning Han committed
44
}
45

46
void fht8x8_ref(const int16_t *in, int16_t *out, int stride, int tx_type) {
47
  vp9_fht8x8_c(in, out, stride, tx_type);
Jingning Han's avatar
Jingning Han committed
48
49
}

50
class FwdTrans8x8TestBase {
Jingning Han's avatar
Jingning Han committed
51
 public:
52
  virtual ~FwdTrans8x8TestBase() {}
Jingning Han's avatar
Jingning Han committed
53
54

 protected:
55
56
  virtual void RunFwdTxfm(int16_t *in, int16_t *out, int stride) = 0;
  virtual void RunInvTxfm(int16_t *out, uint8_t *dst, int stride) = 0;
Jingning Han's avatar
Jingning Han committed
57

58
59
60
61
62
63
  void RunSignBiasCheck() {
    ACMRandom rnd(ACMRandom::DeterministicSeed());
    DECLARE_ALIGNED_ARRAY(16, int16_t, test_input_block, 64);
    DECLARE_ALIGNED_ARRAY(16, int16_t, test_output_block, 64);
    int count_sign_block[64][2];
    const int count_test_block = 100000;
Daniel Kang's avatar
Daniel Kang committed
64

65
    memset(count_sign_block, 0, sizeof(count_sign_block));
Daniel Kang's avatar
Daniel Kang committed
66

67
68
69
70
71
72
    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();
      REGISTER_STATE_CHECK(
          RunFwdTxfm(test_input_block, test_output_block, pitch_));
Daniel Kang's avatar
Daniel Kang committed
73

74
75
76
77
78
79
80
      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];
      }
    }
Daniel Kang's avatar
Daniel Kang committed
81
82

    for (int j = 0; j < 64; ++j) {
83
84
85
86
87
88
89
90
91
      const int diff = abs(count_sign_block[j][0] - count_sign_block[j][1]);
      const int max_diff = 1125;
      EXPECT_LT(diff, max_diff)
          << "Error: 8x8 FDCT/FHT has a sign bias > "
          << 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
92
93
    }

94
    memset(count_sign_block, 0, sizeof(count_sign_block));
Daniel Kang's avatar
Daniel Kang committed
95

96
97
98
99
100
101
    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);
      REGISTER_STATE_CHECK(
          RunFwdTxfm(test_input_block, test_output_block, pitch_));
Daniel Kang's avatar
Daniel Kang committed
102

103
104
105
106
107
108
109
      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];
      }
    }
Daniel Kang's avatar
Daniel Kang committed
110
111

    for (int j = 0; j < 64; ++j) {
112
113
114
115
116
117
118
119
120
      const int diff = abs(count_sign_block[j][0] - count_sign_block[j][1]);
      const int max_diff = 10000;
      EXPECT_LT(diff, max_diff)
          << "Error: 4x4 FDCT/FHT has a sign bias > "
          << 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
121
122
123
    }
  }

124
125
126
127
128
  void RunRoundTripErrorCheck() {
    ACMRandom rnd(ACMRandom::DeterministicSeed());
    int max_error = 0;
    int total_error = 0;
    const int count_test_block = 100000;
129
130
131
132
    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
133

134
135
136
137
138
139
140
    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) {
        src[j] = rnd.Rand8();
        dst[j] = rnd.Rand8();
        test_input_block[j] = src[j] - dst[j];
      }
Daniel Kang's avatar
Daniel Kang committed
141

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
      REGISTER_STATE_CHECK(
          RunFwdTxfm(test_input_block, test_temp_block, pitch_));
      for (int j = 0; j < 64; ++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;
          }
      }
      REGISTER_STATE_CHECK(
          RunInvTxfm(test_temp_block, dst, pitch_));

      for (int j = 0; j < 64; ++j) {
        const int diff = dst[j] - src[j];
        const int error = diff * diff;
        if (max_error < error)
          max_error = error;
        total_error += error;
      }
Daniel Kang's avatar
Daniel Kang committed
165
166
    }

167
168
169
    EXPECT_GE(1, max_error)
      << "Error: 8x8 FDCT/IDCT or FHT/IHT has an individual"
      << " roundtrip error > 1";
Daniel Kang's avatar
Daniel Kang committed
170

171
172
173
174
    EXPECT_GE(count_test_block/5, total_error)
      << "Error: 8x8 FDCT/IDCT or FHT/IHT has average roundtrip "
      << "error > 1/5 per block";
  }
Daniel Kang's avatar
Daniel Kang committed
175

176
177
178
179
  void RunExtremalCheck() {
    ACMRandom rnd(ACMRandom::DeterministicSeed());
    int max_error = 0;
    int total_error = 0;
180
    int total_coeff_error = 0;
181
    const int count_test_block = 100000;
182
183
    DECLARE_ALIGNED_ARRAY(16, int16_t, test_input_block, 64);
    DECLARE_ALIGNED_ARRAY(16, int16_t, test_temp_block, 64);
184
    DECLARE_ALIGNED_ARRAY(16, int16_t, ref_temp_block, 64);
185
186
    DECLARE_ALIGNED_ARRAY(16, uint8_t, dst, 64);
    DECLARE_ALIGNED_ARRAY(16, uint8_t, src, 64);
Daniel Kang's avatar
Daniel Kang committed
187

188
189
190
191
    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) {
        src[j] = rnd.Rand8() % 2 ? 255 : 0;
192
193
194
195
196
197
198
199
200
201
        dst[j] = rnd.Rand8() % 2 ? 255 : 0;

        if (i == 0) {
          src[j] = 255;
          dst[j] = 0;
        } else if (i == 1) {
          src[j] = 0;
          dst[j] = 255;
        }

202
203
        test_input_block[j] = src[j] - dst[j];
      }
Daniel Kang's avatar
Daniel Kang committed
204

205
206
      REGISTER_STATE_CHECK(
          RunFwdTxfm(test_input_block, test_temp_block, pitch_));
207
208
      REGISTER_STATE_CHECK(
          fwd_txfm_ref(test_input_block, ref_temp_block, pitch_, tx_type_));
209
210
211
212
213
214
215
216
217
      REGISTER_STATE_CHECK(
          RunInvTxfm(test_temp_block, dst, pitch_));

      for (int j = 0; j < 64; ++j) {
        const int diff = dst[j] - src[j];
        const int error = diff * diff;
        if (max_error < error)
          max_error = error;
        total_error += error;
218
219
220

        const int coeff_diff = test_temp_block[j] - ref_temp_block[j];
        total_coeff_error += abs(coeff_diff);
221
222
223
224
225
226
227
228
229
      }

      EXPECT_GE(1, max_error)
          << "Error: Extremal 8x8 FDCT/IDCT or FHT/IHT has"
          << "an individual roundtrip error > 1";

      EXPECT_GE(count_test_block/5, total_error)
          << "Error: Extremal 8x8 FDCT/IDCT or FHT/IHT has average"
          << " roundtrip error > 1/5 per block";
230
231
232
233

      EXPECT_EQ(0, total_coeff_error)
          << "Error: Extremal 8x8 FDCT/FHT has"
          << "overflow issues in the intermediate steps > 1";
Daniel Kang's avatar
Daniel Kang committed
234
    }
235
  }
Daniel Kang's avatar
Daniel Kang committed
236

237
238
239
240
  int pitch_;
  int tx_type_;
  fht_t fwd_txfm_ref;
};
Daniel Kang's avatar
Daniel Kang committed
241

242
243
244
class FwdTrans8x8DCT
    : public FwdTrans8x8TestBase,
      public ::testing::TestWithParam<dct_8x8_param_t> {
245
246
247
248
249
250
251
 public:
  virtual ~FwdTrans8x8DCT() {}

  virtual void SetUp() {
    fwd_txfm_ = GET_PARAM(0);
    inv_txfm_ = GET_PARAM(1);
    tx_type_  = GET_PARAM(2);
252
    pitch_    = 8;
253
254
255
256
257
258
259
260
261
262
    fwd_txfm_ref = fdct8x8_ref;
  }

  virtual void TearDown() { libvpx_test::ClearSystemState(); }

 protected:
  void RunFwdTxfm(int16_t *in, int16_t *out, int stride) {
    fwd_txfm_(in, out, stride);
  }
  void RunInvTxfm(int16_t *out, uint8_t *dst, int stride) {
263
    inv_txfm_(out, dst, stride);
Daniel Kang's avatar
Daniel Kang committed
264
  }
265
266
267
268
269
270
271

  fdct_t fwd_txfm_;
  idct_t inv_txfm_;
};

TEST_P(FwdTrans8x8DCT, SignBiasCheck) {
  RunSignBiasCheck();
Jingning Han's avatar
Jingning Han committed
272
}
Daniel Kang's avatar
Daniel Kang committed
273

274
275
276
277
278
279
280
281
TEST_P(FwdTrans8x8DCT, RoundTripErrorCheck) {
  RunRoundTripErrorCheck();
}

TEST_P(FwdTrans8x8DCT, ExtremalCheck) {
  RunExtremalCheck();
}

282
283
284
class FwdTrans8x8HT
    : public FwdTrans8x8TestBase,
      public ::testing::TestWithParam<ht_8x8_param_t> {
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
 public:
  virtual ~FwdTrans8x8HT() {}

  virtual void SetUp() {
    fwd_txfm_ = GET_PARAM(0);
    inv_txfm_ = GET_PARAM(1);
    tx_type_  = GET_PARAM(2);
    pitch_    = 8;
    fwd_txfm_ref = fht8x8_ref;
  }

  virtual void TearDown() { libvpx_test::ClearSystemState(); }

 protected:
  void RunFwdTxfm(int16_t *in, int16_t *out, int stride) {
    fwd_txfm_(in, out, stride, tx_type_);
  }
  void RunInvTxfm(int16_t *out, uint8_t *dst, int stride) {
    inv_txfm_(out, dst, stride, tx_type_);
  }

  fht_t fwd_txfm_;
  iht_t inv_txfm_;
};

TEST_P(FwdTrans8x8HT, SignBiasCheck) {
  RunSignBiasCheck();
}

TEST_P(FwdTrans8x8HT, RoundTripErrorCheck) {
  RunRoundTripErrorCheck();
}

TEST_P(FwdTrans8x8HT, ExtremalCheck) {
  RunExtremalCheck();
}

using std::tr1::make_tuple;

INSTANTIATE_TEST_CASE_P(
    C, FwdTrans8x8DCT,
    ::testing::Values(
327
        make_tuple(&vp9_fdct8x8_c, &vp9_idct8x8_64_add_c, 0)));
328
329
330
INSTANTIATE_TEST_CASE_P(
    C, FwdTrans8x8HT,
    ::testing::Values(
331
332
333
334
        make_tuple(&vp9_fht8x8_c, &vp9_iht8x8_64_add_c, 0),
        make_tuple(&vp9_fht8x8_c, &vp9_iht8x8_64_add_c, 1),
        make_tuple(&vp9_fht8x8_c, &vp9_iht8x8_64_add_c, 2),
        make_tuple(&vp9_fht8x8_c, &vp9_iht8x8_64_add_c, 3)));
335

336
#if HAVE_NEON_ASM
James Zern's avatar
James Zern committed
337
338
339
340
341
342
343
344
345
346
347
348
349
INSTANTIATE_TEST_CASE_P(
    NEON, FwdTrans8x8DCT,
    ::testing::Values(
        make_tuple(&vp9_fdct8x8_c, &vp9_idct8x8_64_add_neon, 0)));
INSTANTIATE_TEST_CASE_P(
    DISABLED_NEON, FwdTrans8x8HT,
    ::testing::Values(
        make_tuple(&vp9_fht8x8_c, &vp9_iht8x8_64_add_neon, 0),
        make_tuple(&vp9_fht8x8_c, &vp9_iht8x8_64_add_neon, 1),
        make_tuple(&vp9_fht8x8_c, &vp9_iht8x8_64_add_neon, 2),
        make_tuple(&vp9_fht8x8_c, &vp9_iht8x8_64_add_neon, 3)));
#endif

350
351
352
353
#if HAVE_SSE2
INSTANTIATE_TEST_CASE_P(
    SSE2, FwdTrans8x8DCT,
    ::testing::Values(
354
        make_tuple(&vp9_fdct8x8_sse2, &vp9_idct8x8_64_add_sse2, 0)));
355
356
357
INSTANTIATE_TEST_CASE_P(
    SSE2, FwdTrans8x8HT,
    ::testing::Values(
358
359
360
361
        make_tuple(&vp9_fht8x8_sse2, &vp9_iht8x8_64_add_sse2, 0),
        make_tuple(&vp9_fht8x8_sse2, &vp9_iht8x8_64_add_sse2, 1),
        make_tuple(&vp9_fht8x8_sse2, &vp9_iht8x8_64_add_sse2, 2),
        make_tuple(&vp9_fht8x8_sse2, &vp9_iht8x8_64_add_sse2, 3)));
362
#endif
363
364
365

#if HAVE_SSSE3 && ARCH_X86_64
INSTANTIATE_TEST_CASE_P(
366
    DISABLED_SSSE3, FwdTrans8x8DCT,
367
368
369
    ::testing::Values(
        make_tuple(&vp9_fdct8x8_ssse3, &vp9_idct8x8_64_add_ssse3, 0)));
#endif
Daniel Kang's avatar
Daniel Kang committed
370
}  // namespace