bea19f18888e744641b2e9e1ff0f5b46cd46ecf2
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / test / fdct4x4_test.cc
1 /*
2  *  Copyright (c) 2012 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 <stdlib.h>
13 #include <string.h>
14
15 #include "third_party/googletest/src/include/gtest/gtest.h"
16 #include "test/acm_random.h"
17 #include "test/clear_system_state.h"
18 #include "test/register_state_check.h"
19 #include "test/util.h"
20
21 #include "./vp9_rtcd.h"
22 #include "vp9/common/vp9_entropy.h"
23 #include "vpx/vpx_integer.h"
24
25 extern "C" {
26 void vp9_idct4x4_16_add_c(const int16_t *input, uint8_t *output, int pitch);
27 }
28
29 using libvpx_test::ACMRandom;
30
31 namespace {
32 const int kNumCoeffs = 16;
33 typedef void (*FdctFunc)(const int16_t *in, int16_t *out, int stride);
34 typedef void (*IdctFunc)(const int16_t *in, uint8_t *out, int stride);
35 typedef void (*FhtFunc)(const int16_t *in, int16_t *out, int stride,
36                         int tx_type);
37 typedef void (*IhtFunc)(const int16_t *in, uint8_t *out, int stride,
38                         int tx_type);
39
40 typedef std::tr1::tuple<FdctFunc, IdctFunc, int> Dct4x4Param;
41 typedef std::tr1::tuple<FhtFunc, IhtFunc, int> Ht4x4Param;
42
43 void fdct4x4_ref(const int16_t *in, int16_t *out, int stride, int tx_type) {
44   vp9_fdct4x4_c(in, out, stride);
45 }
46
47 void fht4x4_ref(const int16_t *in, int16_t *out, int stride, int tx_type) {
48   vp9_fht4x4_c(in, out, stride, tx_type);
49 }
50
51 void fwht4x4_ref(const int16_t *in, int16_t *out, int stride, int tx_type) {
52   vp9_fwht4x4_c(in, out, stride);
53 }
54
55 class Trans4x4TestBase {
56  public:
57   virtual ~Trans4x4TestBase() {}
58
59  protected:
60   virtual void RunFwdTxfm(const int16_t *in, int16_t *out, int stride) = 0;
61
62   virtual void RunInvTxfm(const int16_t *out, uint8_t *dst, int stride) = 0;
63
64   void RunAccuracyCheck(int limit) {
65     ACMRandom rnd(ACMRandom::DeterministicSeed());
66     uint32_t max_error = 0;
67     int64_t total_error = 0;
68     const int count_test_block = 10000;
69     for (int i = 0; i < count_test_block; ++i) {
70       DECLARE_ALIGNED_ARRAY(16, int16_t, test_input_block, kNumCoeffs);
71       DECLARE_ALIGNED_ARRAY(16, int16_t, test_temp_block, kNumCoeffs);
72       DECLARE_ALIGNED_ARRAY(16, uint8_t, dst, kNumCoeffs);
73       DECLARE_ALIGNED_ARRAY(16, uint8_t, src, kNumCoeffs);
74
75       // Initialize a test block with input range [-255, 255].
76       for (int j = 0; j < kNumCoeffs; ++j) {
77         src[j] = rnd.Rand8();
78         dst[j] = rnd.Rand8();
79         test_input_block[j] = src[j] - dst[j];
80       }
81
82       ASM_REGISTER_STATE_CHECK(RunFwdTxfm(test_input_block,
83                                           test_temp_block, pitch_));
84       ASM_REGISTER_STATE_CHECK(RunInvTxfm(test_temp_block, dst, pitch_));
85
86       for (int j = 0; j < kNumCoeffs; ++j) {
87         const uint32_t diff = dst[j] - src[j];
88         const uint32_t error = diff * diff;
89         if (max_error < error)
90           max_error = error;
91         total_error += error;
92       }
93     }
94
95     EXPECT_GE(static_cast<uint32_t>(limit), max_error)
96         << "Error: 4x4 FHT/IHT has an individual round trip error > "
97         << limit;
98
99     EXPECT_GE(count_test_block * limit, total_error)
100         << "Error: 4x4 FHT/IHT has average round trip error > " << limit
101         << " per block";
102   }
103
104   void RunCoeffCheck() {
105     ACMRandom rnd(ACMRandom::DeterministicSeed());
106     const int count_test_block = 5000;
107     DECLARE_ALIGNED_ARRAY(16, int16_t, input_block, kNumCoeffs);
108     DECLARE_ALIGNED_ARRAY(16, int16_t, output_ref_block, kNumCoeffs);
109     DECLARE_ALIGNED_ARRAY(16, int16_t, output_block, kNumCoeffs);
110
111     for (int i = 0; i < count_test_block; ++i) {
112       // Initialize a test block with input range [-255, 255].
113       for (int j = 0; j < kNumCoeffs; ++j)
114         input_block[j] = rnd.Rand8() - rnd.Rand8();
115
116       fwd_txfm_ref(input_block, output_ref_block, pitch_, tx_type_);
117       ASM_REGISTER_STATE_CHECK(RunFwdTxfm(input_block, output_block, pitch_));
118
119       // The minimum quant value is 4.
120       for (int j = 0; j < kNumCoeffs; ++j)
121         EXPECT_EQ(output_block[j], output_ref_block[j]);
122     }
123   }
124
125   void RunMemCheck() {
126     ACMRandom rnd(ACMRandom::DeterministicSeed());
127     const int count_test_block = 5000;
128     DECLARE_ALIGNED_ARRAY(16, int16_t, input_block, kNumCoeffs);
129     DECLARE_ALIGNED_ARRAY(16, int16_t, input_extreme_block, kNumCoeffs);
130     DECLARE_ALIGNED_ARRAY(16, int16_t, output_ref_block, kNumCoeffs);
131     DECLARE_ALIGNED_ARRAY(16, int16_t, output_block, kNumCoeffs);
132
133     for (int i = 0; i < count_test_block; ++i) {
134       // Initialize a test block with input range [-255, 255].
135       for (int j = 0; j < kNumCoeffs; ++j) {
136         input_block[j] = rnd.Rand8() - rnd.Rand8();
137         input_extreme_block[j] = rnd.Rand8() % 2 ? 255 : -255;
138       }
139       if (i == 0) {
140         for (int j = 0; j < kNumCoeffs; ++j)
141           input_extreme_block[j] = 255;
142       } else if (i == 1) {
143         for (int j = 0; j < kNumCoeffs; ++j)
144           input_extreme_block[j] = -255;
145       }
146
147       fwd_txfm_ref(input_extreme_block, output_ref_block, pitch_, tx_type_);
148       ASM_REGISTER_STATE_CHECK(RunFwdTxfm(input_extreme_block,
149                                           output_block, pitch_));
150
151       // The minimum quant value is 4.
152       for (int j = 0; j < kNumCoeffs; ++j) {
153         EXPECT_EQ(output_block[j], output_ref_block[j]);
154         EXPECT_GE(4 * DCT_MAX_VALUE, abs(output_block[j]))
155             << "Error: 16x16 FDCT has coefficient larger than 4*DCT_MAX_VALUE";
156       }
157     }
158   }
159
160   void RunInvAccuracyCheck(int limit) {
161     ACMRandom rnd(ACMRandom::DeterministicSeed());
162     const int count_test_block = 1000;
163     DECLARE_ALIGNED_ARRAY(16, int16_t, in, kNumCoeffs);
164     DECLARE_ALIGNED_ARRAY(16, int16_t, coeff, kNumCoeffs);
165     DECLARE_ALIGNED_ARRAY(16, uint8_t, dst, kNumCoeffs);
166     DECLARE_ALIGNED_ARRAY(16, uint8_t, src, kNumCoeffs);
167
168     for (int i = 0; i < count_test_block; ++i) {
169       // Initialize a test block with input range [-255, 255].
170       for (int j = 0; j < kNumCoeffs; ++j) {
171         src[j] = rnd.Rand8();
172         dst[j] = rnd.Rand8();
173         in[j] = src[j] - dst[j];
174       }
175
176       fwd_txfm_ref(in, coeff, pitch_, tx_type_);
177
178       ASM_REGISTER_STATE_CHECK(RunInvTxfm(coeff, dst, pitch_));
179
180       for (int j = 0; j < kNumCoeffs; ++j) {
181         const uint32_t diff = dst[j] - src[j];
182         const uint32_t error = diff * diff;
183         EXPECT_GE(static_cast<uint32_t>(limit), error)
184             << "Error: 4x4 IDCT has error " << error
185             << " at index " << j;
186       }
187     }
188   }
189
190   int pitch_;
191   int tx_type_;
192   FhtFunc fwd_txfm_ref;
193 };
194
195 class Trans4x4DCT
196     : public Trans4x4TestBase,
197       public ::testing::TestWithParam<Dct4x4Param> {
198  public:
199   virtual ~Trans4x4DCT() {}
200
201   virtual void SetUp() {
202     fwd_txfm_ = GET_PARAM(0);
203     inv_txfm_ = GET_PARAM(1);
204     tx_type_  = GET_PARAM(2);
205     pitch_    = 4;
206     fwd_txfm_ref = fdct4x4_ref;
207   }
208   virtual void TearDown() { libvpx_test::ClearSystemState(); }
209
210  protected:
211   void RunFwdTxfm(const int16_t *in, int16_t *out, int stride) {
212     fwd_txfm_(in, out, stride);
213   }
214   void RunInvTxfm(const int16_t *out, uint8_t *dst, int stride) {
215     inv_txfm_(out, dst, stride);
216   }
217
218   FdctFunc fwd_txfm_;
219   IdctFunc inv_txfm_;
220 };
221
222 TEST_P(Trans4x4DCT, AccuracyCheck) {
223   RunAccuracyCheck(1);
224 }
225
226 TEST_P(Trans4x4DCT, CoeffCheck) {
227   RunCoeffCheck();
228 }
229
230 TEST_P(Trans4x4DCT, MemCheck) {
231   RunMemCheck();
232 }
233
234 TEST_P(Trans4x4DCT, InvAccuracyCheck) {
235   RunInvAccuracyCheck(1);
236 }
237
238 class Trans4x4HT
239     : public Trans4x4TestBase,
240       public ::testing::TestWithParam<Ht4x4Param> {
241  public:
242   virtual ~Trans4x4HT() {}
243
244   virtual void SetUp() {
245     fwd_txfm_ = GET_PARAM(0);
246     inv_txfm_ = GET_PARAM(1);
247     tx_type_  = GET_PARAM(2);
248     pitch_    = 4;
249     fwd_txfm_ref = fht4x4_ref;
250   }
251   virtual void TearDown() { libvpx_test::ClearSystemState(); }
252
253  protected:
254   void RunFwdTxfm(const int16_t *in, int16_t *out, int stride) {
255     fwd_txfm_(in, out, stride, tx_type_);
256   }
257
258   void RunInvTxfm(const int16_t *out, uint8_t *dst, int stride) {
259     inv_txfm_(out, dst, stride, tx_type_);
260   }
261
262   FhtFunc fwd_txfm_;
263   IhtFunc inv_txfm_;
264 };
265
266 TEST_P(Trans4x4HT, AccuracyCheck) {
267   RunAccuracyCheck(1);
268 }
269
270 TEST_P(Trans4x4HT, CoeffCheck) {
271   RunCoeffCheck();
272 }
273
274 TEST_P(Trans4x4HT, MemCheck) {
275   RunMemCheck();
276 }
277
278 TEST_P(Trans4x4HT, InvAccuracyCheck) {
279   RunInvAccuracyCheck(1);
280 }
281
282 class Trans4x4WHT
283     : public Trans4x4TestBase,
284       public ::testing::TestWithParam<Dct4x4Param> {
285  public:
286   virtual ~Trans4x4WHT() {}
287
288   virtual void SetUp() {
289     fwd_txfm_ = GET_PARAM(0);
290     inv_txfm_ = GET_PARAM(1);
291     tx_type_  = GET_PARAM(2);
292     pitch_    = 4;
293     fwd_txfm_ref = fwht4x4_ref;
294   }
295   virtual void TearDown() { libvpx_test::ClearSystemState(); }
296
297  protected:
298   void RunFwdTxfm(const int16_t *in, int16_t *out, int stride) {
299     fwd_txfm_(in, out, stride);
300   }
301   void RunInvTxfm(const int16_t *out, uint8_t *dst, int stride) {
302     inv_txfm_(out, dst, stride);
303   }
304
305   FdctFunc fwd_txfm_;
306   IdctFunc inv_txfm_;
307 };
308
309 TEST_P(Trans4x4WHT, AccuracyCheck) {
310   RunAccuracyCheck(0);
311 }
312
313 TEST_P(Trans4x4WHT, CoeffCheck) {
314   RunCoeffCheck();
315 }
316
317 TEST_P(Trans4x4WHT, MemCheck) {
318   RunMemCheck();
319 }
320
321 TEST_P(Trans4x4WHT, InvAccuracyCheck) {
322   RunInvAccuracyCheck(0);
323 }
324 using std::tr1::make_tuple;
325
326 INSTANTIATE_TEST_CASE_P(
327     C, Trans4x4DCT,
328     ::testing::Values(
329         make_tuple(&vp9_fdct4x4_c, &vp9_idct4x4_16_add_c, 0)));
330 INSTANTIATE_TEST_CASE_P(
331     C, Trans4x4HT,
332     ::testing::Values(
333         make_tuple(&vp9_fht4x4_c, &vp9_iht4x4_16_add_c, 0),
334         make_tuple(&vp9_fht4x4_c, &vp9_iht4x4_16_add_c, 1),
335         make_tuple(&vp9_fht4x4_c, &vp9_iht4x4_16_add_c, 2),
336         make_tuple(&vp9_fht4x4_c, &vp9_iht4x4_16_add_c, 3)));
337 INSTANTIATE_TEST_CASE_P(
338     C, Trans4x4WHT,
339     ::testing::Values(
340         make_tuple(&vp9_fwht4x4_c, &vp9_iwht4x4_16_add_c, 0)));
341
342 #if HAVE_NEON_ASM
343 INSTANTIATE_TEST_CASE_P(
344     NEON, Trans4x4DCT,
345     ::testing::Values(
346         make_tuple(&vp9_fdct4x4_c,
347                    &vp9_idct4x4_16_add_neon, 0)));
348 INSTANTIATE_TEST_CASE_P(
349     DISABLED_NEON, Trans4x4HT,
350     ::testing::Values(
351         make_tuple(&vp9_fht4x4_c, &vp9_iht4x4_16_add_neon, 0),
352         make_tuple(&vp9_fht4x4_c, &vp9_iht4x4_16_add_neon, 1),
353         make_tuple(&vp9_fht4x4_c, &vp9_iht4x4_16_add_neon, 2),
354         make_tuple(&vp9_fht4x4_c, &vp9_iht4x4_16_add_neon, 3)));
355 #endif
356
357 #if CONFIG_USE_X86INC && HAVE_MMX
358 INSTANTIATE_TEST_CASE_P(
359     MMX, Trans4x4WHT,
360     ::testing::Values(
361         make_tuple(&vp9_fwht4x4_mmx, &vp9_iwht4x4_16_add_c, 0)));
362 #endif
363
364 #if HAVE_SSE2
365 INSTANTIATE_TEST_CASE_P(
366     SSE2, Trans4x4DCT,
367     ::testing::Values(
368         make_tuple(&vp9_fdct4x4_sse2,
369                    &vp9_idct4x4_16_add_sse2, 0)));
370 INSTANTIATE_TEST_CASE_P(
371     SSE2, Trans4x4HT,
372     ::testing::Values(
373         make_tuple(&vp9_fht4x4_sse2, &vp9_iht4x4_16_add_sse2, 0),
374         make_tuple(&vp9_fht4x4_sse2, &vp9_iht4x4_16_add_sse2, 1),
375         make_tuple(&vp9_fht4x4_sse2, &vp9_iht4x4_16_add_sse2, 2),
376         make_tuple(&vp9_fht4x4_sse2, &vp9_iht4x4_16_add_sse2, 3)));
377 #endif
378
379 #if HAVE_AVX2
380 INSTANTIATE_TEST_CASE_P(
381     AVX2, Trans4x4DCT,
382     ::testing::Values(
383         make_tuple(&vp9_fdct4x4_avx2,
384                    &vp9_idct4x4_16_add_c, 0)));
385 INSTANTIATE_TEST_CASE_P(
386     AVX2, Trans4x4HT,
387     ::testing::Values(
388         make_tuple(&vp9_fht4x4_avx2, &vp9_iht4x4_16_add_c, 0),
389         make_tuple(&vp9_fht4x4_avx2, &vp9_iht4x4_16_add_c, 1),
390         make_tuple(&vp9_fht4x4_avx2, &vp9_iht4x4_16_add_c, 2),
391         make_tuple(&vp9_fht4x4_avx2, &vp9_iht4x4_16_add_c, 3)));
392 #endif
393
394 }  // namespace