• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2016 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 <algorithm>
12 
13 #include "third_party/googletest/src/include/gtest/gtest.h"
14 
15 #include "./vpx_dsp_rtcd.h"
16 #include "vpx_ports/vpx_timer.h"
17 
18 #include "test/acm_random.h"
19 #include "test/register_state_check.h"
20 
21 namespace {
22 
23 using ::libvpx_test::ACMRandom;
24 
25 typedef void (*HadamardFunc)(const int16_t *a, ptrdiff_t a_stride,
26                              tran_low_t *b);
27 
hadamard_loop(const tran_low_t * a,tran_low_t * out)28 void hadamard_loop(const tran_low_t *a, tran_low_t *out) {
29   tran_low_t b[8];
30   for (int i = 0; i < 8; i += 2) {
31     b[i + 0] = a[i * 8] + a[(i + 1) * 8];
32     b[i + 1] = a[i * 8] - a[(i + 1) * 8];
33   }
34   tran_low_t c[8];
35   for (int i = 0; i < 8; i += 4) {
36     c[i + 0] = b[i + 0] + b[i + 2];
37     c[i + 1] = b[i + 1] + b[i + 3];
38     c[i + 2] = b[i + 0] - b[i + 2];
39     c[i + 3] = b[i + 1] - b[i + 3];
40   }
41   out[0] = c[0] + c[4];
42   out[7] = c[1] + c[5];
43   out[3] = c[2] + c[6];
44   out[4] = c[3] + c[7];
45   out[2] = c[0] - c[4];
46   out[6] = c[1] - c[5];
47   out[1] = c[2] - c[6];
48   out[5] = c[3] - c[7];
49 }
50 
reference_hadamard8x8(const int16_t * a,int a_stride,tran_low_t * b)51 void reference_hadamard8x8(const int16_t *a, int a_stride, tran_low_t *b) {
52   tran_low_t input[64];
53   tran_low_t buf[64];
54   for (int i = 0; i < 8; ++i) {
55     for (int j = 0; j < 8; ++j) {
56       input[i * 8 + j] = static_cast<tran_low_t>(a[i * a_stride + j]);
57     }
58   }
59   for (int i = 0; i < 8; ++i) hadamard_loop(input + i, buf + i * 8);
60   for (int i = 0; i < 8; ++i) hadamard_loop(buf + i, b + i * 8);
61 }
62 
reference_hadamard16x16(const int16_t * a,int a_stride,tran_low_t * b)63 void reference_hadamard16x16(const int16_t *a, int a_stride, tran_low_t *b) {
64   /* The source is a 16x16 block. The destination is rearranged to 8x32.
65    * Input is 9 bit. */
66   reference_hadamard8x8(a + 0 + 0 * a_stride, a_stride, b + 0);
67   reference_hadamard8x8(a + 8 + 0 * a_stride, a_stride, b + 64);
68   reference_hadamard8x8(a + 0 + 8 * a_stride, a_stride, b + 128);
69   reference_hadamard8x8(a + 8 + 8 * a_stride, a_stride, b + 192);
70 
71   /* Overlay the 8x8 blocks and combine. */
72   for (int i = 0; i < 64; ++i) {
73     /* 8x8 steps the range up to 15 bits. */
74     const tran_low_t a0 = b[0];
75     const tran_low_t a1 = b[64];
76     const tran_low_t a2 = b[128];
77     const tran_low_t a3 = b[192];
78 
79     /* Prevent the result from escaping int16_t. */
80     const tran_low_t b0 = (a0 + a1) >> 1;
81     const tran_low_t b1 = (a0 - a1) >> 1;
82     const tran_low_t b2 = (a2 + a3) >> 1;
83     const tran_low_t b3 = (a2 - a3) >> 1;
84 
85     /* Store a 16 bit value. */
86     b[0] = b0 + b2;
87     b[64] = b1 + b3;
88     b[128] = b0 - b2;
89     b[192] = b1 - b3;
90 
91     ++b;
92   }
93 }
94 
reference_hadamard32x32(const int16_t * a,int a_stride,tran_low_t * b)95 void reference_hadamard32x32(const int16_t *a, int a_stride, tran_low_t *b) {
96   reference_hadamard16x16(a + 0 + 0 * a_stride, a_stride, b + 0);
97   reference_hadamard16x16(a + 16 + 0 * a_stride, a_stride, b + 256);
98   reference_hadamard16x16(a + 0 + 16 * a_stride, a_stride, b + 512);
99   reference_hadamard16x16(a + 16 + 16 * a_stride, a_stride, b + 768);
100 
101   for (int i = 0; i < 256; ++i) {
102     const tran_low_t a0 = b[0];
103     const tran_low_t a1 = b[256];
104     const tran_low_t a2 = b[512];
105     const tran_low_t a3 = b[768];
106 
107     const tran_low_t b0 = (a0 + a1) >> 2;
108     const tran_low_t b1 = (a0 - a1) >> 2;
109     const tran_low_t b2 = (a2 + a3) >> 2;
110     const tran_low_t b3 = (a2 - a3) >> 2;
111 
112     b[0] = b0 + b2;
113     b[256] = b1 + b3;
114     b[512] = b0 - b2;
115     b[768] = b1 - b3;
116 
117     ++b;
118   }
119 }
120 
121 struct HadamardFuncWithSize {
HadamardFuncWithSize__anon8e21151d0111::HadamardFuncWithSize122   HadamardFuncWithSize(HadamardFunc f, int s) : func(f), block_size(s) {}
123   HadamardFunc func;
124   int block_size;
125 };
126 
operator <<(std::ostream & os,const HadamardFuncWithSize & hfs)127 std::ostream &operator<<(std::ostream &os, const HadamardFuncWithSize &hfs) {
128   return os << "block size: " << hfs.block_size;
129 }
130 
131 class HadamardTestBase : public ::testing::TestWithParam<HadamardFuncWithSize> {
132  public:
SetUp()133   void SetUp() override {
134     h_func_ = GetParam().func;
135     bwh_ = GetParam().block_size;
136     block_size_ = bwh_ * bwh_;
137     rnd_.Reset(ACMRandom::DeterministicSeed());
138   }
139 
140   // The Rand() function generates values in the range [-((1 << BitDepth) - 1),
141   // (1 << BitDepth) - 1]. This is because the input to the Hadamard transform
142   // is the residual pixel, which is defined as 'source pixel - predicted
143   // pixel'. Source pixel and predicted pixel take values in the range
144   // [0, (1 << BitDepth) - 1] and thus the residual pixel ranges from
145   // -((1 << BitDepth) - 1) to ((1 << BitDepth) - 1).
146   virtual int16_t Rand() = 0;
147 
ReferenceHadamard(const int16_t * a,int a_stride,tran_low_t * b,int bwh)148   void ReferenceHadamard(const int16_t *a, int a_stride, tran_low_t *b,
149                          int bwh) {
150     if (bwh == 32)
151       reference_hadamard32x32(a, a_stride, b);
152     else if (bwh == 16)
153       reference_hadamard16x16(a, a_stride, b);
154     else
155       reference_hadamard8x8(a, a_stride, b);
156   }
157 
CompareReferenceRandom()158   void CompareReferenceRandom() {
159     const int kMaxBlockSize = 32 * 32;
160     DECLARE_ALIGNED(16, int16_t, a[kMaxBlockSize]);
161     DECLARE_ALIGNED(16, tran_low_t, b[kMaxBlockSize]);
162     memset(a, 0, sizeof(a));
163     memset(b, 0, sizeof(b));
164 
165     tran_low_t b_ref[kMaxBlockSize];
166     memset(b_ref, 0, sizeof(b_ref));
167 
168     for (int i = 0; i < block_size_; ++i) a[i] = Rand();
169 
170     ReferenceHadamard(a, bwh_, b_ref, bwh_);
171     ASM_REGISTER_STATE_CHECK(h_func_(a, bwh_, b));
172 
173     // The order of the output is not important. Sort before checking.
174     std::sort(b, b + block_size_);
175     std::sort(b_ref, b_ref + block_size_);
176     EXPECT_EQ(0, memcmp(b, b_ref, sizeof(b)));
177   }
178 
ExtremeValuesTest()179   void ExtremeValuesTest() {
180     const int kMaxBlockSize = 32 * 32;
181     DECLARE_ALIGNED(16, int16_t, input_extreme_block[kMaxBlockSize]);
182     DECLARE_ALIGNED(16, tran_low_t, b[kMaxBlockSize]);
183     memset(b, 0, sizeof(b));
184 
185     tran_low_t b_ref[kMaxBlockSize];
186     memset(b_ref, 0, sizeof(b_ref));
187 
188     for (int i = 0; i < 2; ++i) {
189       // Initialize a test block with input range [-mask_, mask_].
190       const int sign = (i == 0) ? 1 : -1;
191       for (int j = 0; j < kMaxBlockSize; ++j)
192         input_extreme_block[j] = sign * 255;
193 
194       ReferenceHadamard(input_extreme_block, bwh_, b_ref, bwh_);
195       ASM_REGISTER_STATE_CHECK(h_func_(input_extreme_block, bwh_, b));
196 
197       // The order of the output is not important. Sort before checking.
198       std::sort(b, b + block_size_);
199       std::sort(b_ref, b_ref + block_size_);
200       EXPECT_EQ(0, memcmp(b, b_ref, sizeof(b)));
201     }
202   }
203 
VaryStride()204   void VaryStride() {
205     const int kMaxBlockSize = 32 * 32;
206     DECLARE_ALIGNED(16, int16_t, a[kMaxBlockSize * 8]);
207     DECLARE_ALIGNED(16, tran_low_t, b[kMaxBlockSize]);
208     memset(a, 0, sizeof(a));
209     for (int i = 0; i < block_size_ * 8; ++i) a[i] = Rand();
210 
211     tran_low_t b_ref[kMaxBlockSize];
212     for (int i = 8; i < 64; i += 8) {
213       memset(b, 0, sizeof(b));
214       memset(b_ref, 0, sizeof(b_ref));
215 
216       ReferenceHadamard(a, i, b_ref, bwh_);
217       ASM_REGISTER_STATE_CHECK(h_func_(a, i, b));
218 
219       // The order of the output is not important. Sort before checking.
220       std::sort(b, b + block_size_);
221       std::sort(b_ref, b_ref + block_size_);
222       EXPECT_EQ(0, memcmp(b, b_ref, sizeof(b)));
223     }
224   }
225 
SpeedTest(int times)226   void SpeedTest(int times) {
227     const int kMaxBlockSize = 32 * 32;
228     DECLARE_ALIGNED(16, int16_t, input[kMaxBlockSize]);
229     DECLARE_ALIGNED(16, tran_low_t, output[kMaxBlockSize]);
230     memset(input, 1, sizeof(input));
231     memset(output, 0, sizeof(output));
232 
233     vpx_usec_timer timer;
234     vpx_usec_timer_start(&timer);
235     for (int i = 0; i < times; ++i) {
236       h_func_(input, bwh_, output);
237     }
238     vpx_usec_timer_mark(&timer);
239 
240     const int elapsed_time = static_cast<int>(vpx_usec_timer_elapsed(&timer));
241     printf("Hadamard%dx%d[%12d runs]: %d us\n", bwh_, bwh_, times,
242            elapsed_time);
243   }
244 
245  protected:
246   int bwh_;
247   int block_size_;
248   HadamardFunc h_func_;
249   ACMRandom rnd_;
250 };
251 
252 class HadamardLowbdTest : public HadamardTestBase {
253  protected:
254   // Use values between -255 (0xFF01) and 255 (0x00FF)
Rand()255   int16_t Rand() override {
256     int16_t src = rnd_.Rand8();
257     int16_t pred = rnd_.Rand8();
258     return src - pred;
259   }
260 };
261 
TEST_P(HadamardLowbdTest,CompareReferenceRandom)262 TEST_P(HadamardLowbdTest, CompareReferenceRandom) { CompareReferenceRandom(); }
263 
TEST_P(HadamardLowbdTest,ExtremeValuesTest)264 TEST_P(HadamardLowbdTest, ExtremeValuesTest) { ExtremeValuesTest(); }
265 
TEST_P(HadamardLowbdTest,VaryStride)266 TEST_P(HadamardLowbdTest, VaryStride) { VaryStride(); }
267 
TEST_P(HadamardLowbdTest,DISABLED_Speed)268 TEST_P(HadamardLowbdTest, DISABLED_Speed) {
269   SpeedTest(10);
270   SpeedTest(10000);
271   SpeedTest(10000000);
272 }
273 
274 INSTANTIATE_TEST_SUITE_P(
275     C, HadamardLowbdTest,
276     ::testing::Values(HadamardFuncWithSize(&vpx_hadamard_8x8_c, 8),
277                       HadamardFuncWithSize(&vpx_hadamard_16x16_c, 16),
278                       HadamardFuncWithSize(&vpx_hadamard_32x32_c, 32)));
279 
280 #if HAVE_SSE2
281 INSTANTIATE_TEST_SUITE_P(
282     SSE2, HadamardLowbdTest,
283     ::testing::Values(HadamardFuncWithSize(&vpx_hadamard_8x8_sse2, 8),
284                       HadamardFuncWithSize(&vpx_hadamard_16x16_sse2, 16),
285                       HadamardFuncWithSize(&vpx_hadamard_32x32_sse2, 32)));
286 #endif  // HAVE_SSE2
287 
288 #if HAVE_AVX2
289 INSTANTIATE_TEST_SUITE_P(
290     AVX2, HadamardLowbdTest,
291     ::testing::Values(HadamardFuncWithSize(&vpx_hadamard_16x16_avx2, 16),
292                       HadamardFuncWithSize(&vpx_hadamard_32x32_avx2, 32)));
293 #endif  // HAVE_AVX2
294 
295 #if HAVE_SSSE3 && VPX_ARCH_X86_64
296 INSTANTIATE_TEST_SUITE_P(
297     SSSE3, HadamardLowbdTest,
298     ::testing::Values(HadamardFuncWithSize(&vpx_hadamard_8x8_ssse3, 8)));
299 #endif  // HAVE_SSSE3 && VPX_ARCH_X86_64
300 
301 #if HAVE_NEON
302 INSTANTIATE_TEST_SUITE_P(
303     NEON, HadamardLowbdTest,
304     ::testing::Values(HadamardFuncWithSize(&vpx_hadamard_8x8_neon, 8),
305                       HadamardFuncWithSize(&vpx_hadamard_16x16_neon, 16),
306                       HadamardFuncWithSize(&vpx_hadamard_32x32_neon, 32)));
307 #endif  // HAVE_NEON
308 
309 // TODO(jingning): Remove highbitdepth flag when the SIMD functions are
310 // in place and turn on the unit test.
311 #if !CONFIG_VP9_HIGHBITDEPTH
312 #if HAVE_MSA
313 INSTANTIATE_TEST_SUITE_P(
314     MSA, HadamardLowbdTest,
315     ::testing::Values(HadamardFuncWithSize(&vpx_hadamard_8x8_msa, 8),
316                       HadamardFuncWithSize(&vpx_hadamard_16x16_msa, 16)));
317 #endif  // HAVE_MSA
318 #endif  // !CONFIG_VP9_HIGHBITDEPTH
319 
320 #if HAVE_VSX
321 INSTANTIATE_TEST_SUITE_P(
322     VSX, HadamardLowbdTest,
323     ::testing::Values(HadamardFuncWithSize(&vpx_hadamard_8x8_vsx, 8),
324                       HadamardFuncWithSize(&vpx_hadamard_16x16_vsx, 16)));
325 #endif  // HAVE_VSX
326 
327 #if HAVE_LSX
328 INSTANTIATE_TEST_SUITE_P(
329     LSX, HadamardLowbdTest,
330     ::testing::Values(HadamardFuncWithSize(&vpx_hadamard_8x8_lsx, 8),
331                       HadamardFuncWithSize(&vpx_hadamard_16x16_lsx, 16)));
332 #endif  // HAVE_LSX
333 
334 #if CONFIG_VP9_HIGHBITDEPTH
335 class HadamardHighbdTest : public HadamardTestBase {
336  protected:
337   // Use values between -4095 (0xF001) and 4095 (0x0FFF)
Rand()338   int16_t Rand() override {
339     int16_t src = rnd_.Rand12();
340     int16_t pred = rnd_.Rand12();
341     return src - pred;
342   }
343 };
344 
TEST_P(HadamardHighbdTest,CompareReferenceRandom)345 TEST_P(HadamardHighbdTest, CompareReferenceRandom) { CompareReferenceRandom(); }
346 
TEST_P(HadamardHighbdTest,VaryStride)347 TEST_P(HadamardHighbdTest, VaryStride) { VaryStride(); }
348 
TEST_P(HadamardHighbdTest,DISABLED_Speed)349 TEST_P(HadamardHighbdTest, DISABLED_Speed) {
350   SpeedTest(10);
351   SpeedTest(10000);
352   SpeedTest(10000000);
353 }
354 
355 INSTANTIATE_TEST_SUITE_P(
356     C, HadamardHighbdTest,
357     ::testing::Values(HadamardFuncWithSize(&vpx_highbd_hadamard_8x8_c, 8),
358                       HadamardFuncWithSize(&vpx_highbd_hadamard_16x16_c, 16),
359                       HadamardFuncWithSize(&vpx_highbd_hadamard_32x32_c, 32)));
360 
361 #if HAVE_AVX2
362 INSTANTIATE_TEST_SUITE_P(
363     AVX2, HadamardHighbdTest,
364     ::testing::Values(HadamardFuncWithSize(&vpx_highbd_hadamard_8x8_avx2, 8),
365                       HadamardFuncWithSize(&vpx_highbd_hadamard_16x16_avx2, 16),
366                       HadamardFuncWithSize(&vpx_highbd_hadamard_32x32_avx2,
367                                            32)));
368 #endif  // HAVE_AVX2
369 
370 #if HAVE_NEON
371 INSTANTIATE_TEST_SUITE_P(
372     NEON, HadamardHighbdTest,
373     ::testing::Values(HadamardFuncWithSize(&vpx_highbd_hadamard_8x8_neon, 8),
374                       HadamardFuncWithSize(&vpx_highbd_hadamard_16x16_neon, 16),
375                       HadamardFuncWithSize(&vpx_highbd_hadamard_32x32_neon,
376                                            32)));
377 #endif
378 
379 #endif  // CONFIG_VP9_HIGHBITDEPTH
380 }  // namespace
381