vp9[loongarch]: Optimize fdct4x4/8x8_lsx
[platform/upstream/libvpx.git] / test / dct32x32_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 #include <tuple>
15
16 #include "third_party/googletest/src/include/gtest/gtest.h"
17
18 #include "./vp9_rtcd.h"
19 #include "./vpx_config.h"
20 #include "./vpx_dsp_rtcd.h"
21 #include "test/acm_random.h"
22 #include "test/bench.h"
23 #include "test/clear_system_state.h"
24 #include "test/register_state_check.h"
25 #include "test/util.h"
26 #include "vp9/common/vp9_entropy.h"
27 #include "vpx/vpx_codec.h"
28 #include "vpx/vpx_integer.h"
29 #include "vpx_ports/mem.h"
30 #include "vpx_ports/msvc.h"  // for round()
31
32 using libvpx_test::ACMRandom;
33
34 namespace {
35
36 const int kNumCoeffs = 1024;
37 const double kPi = 3.141592653589793238462643383279502884;
38 void reference_32x32_dct_1d(const double in[32], double out[32]) {
39   const double kInvSqrt2 = 0.707106781186547524400844362104;
40   for (int k = 0; k < 32; k++) {
41     out[k] = 0.0;
42     for (int n = 0; n < 32; n++) {
43       out[k] += in[n] * cos(kPi * (2 * n + 1) * k / 64.0);
44     }
45     if (k == 0) out[k] = out[k] * kInvSqrt2;
46   }
47 }
48
49 void reference_32x32_dct_2d(const int16_t input[kNumCoeffs],
50                             double output[kNumCoeffs]) {
51   // First transform columns
52   for (int i = 0; i < 32; ++i) {
53     double temp_in[32], temp_out[32];
54     for (int j = 0; j < 32; ++j) temp_in[j] = input[j * 32 + i];
55     reference_32x32_dct_1d(temp_in, temp_out);
56     for (int j = 0; j < 32; ++j) output[j * 32 + i] = temp_out[j];
57   }
58   // Then transform rows
59   for (int i = 0; i < 32; ++i) {
60     double temp_in[32], temp_out[32];
61     for (int j = 0; j < 32; ++j) temp_in[j] = output[j + i * 32];
62     reference_32x32_dct_1d(temp_in, temp_out);
63     // Scale by some magic number
64     for (int j = 0; j < 32; ++j) output[j + i * 32] = temp_out[j] / 4;
65   }
66 }
67
68 typedef void (*FwdTxfmFunc)(const int16_t *in, tran_low_t *out, int stride);
69 typedef void (*InvTxfmFunc)(const tran_low_t *in, uint8_t *out, int stride);
70
71 typedef std::tuple<FwdTxfmFunc, InvTxfmFunc, int, vpx_bit_depth_t>
72     Trans32x32Param;
73
74 #if CONFIG_VP9_HIGHBITDEPTH
75 void idct32x32_10(const tran_low_t *in, uint8_t *out, int stride) {
76   vpx_highbd_idct32x32_1024_add_c(in, CAST_TO_SHORTPTR(out), stride, 10);
77 }
78
79 void idct32x32_12(const tran_low_t *in, uint8_t *out, int stride) {
80   vpx_highbd_idct32x32_1024_add_c(in, CAST_TO_SHORTPTR(out), stride, 12);
81 }
82 #endif  // CONFIG_VP9_HIGHBITDEPTH
83
84 class Trans32x32Test : public AbstractBench,
85                        public ::testing::TestWithParam<Trans32x32Param> {
86  public:
87   virtual ~Trans32x32Test() {}
88   virtual void SetUp() {
89     fwd_txfm_ = GET_PARAM(0);
90     inv_txfm_ = GET_PARAM(1);
91     version_ = GET_PARAM(2);  // 0: high precision forward transform
92                               // 1: low precision version for rd loop
93     bit_depth_ = GET_PARAM(3);
94     mask_ = (1 << bit_depth_) - 1;
95   }
96
97   virtual void TearDown() { libvpx_test::ClearSystemState(); }
98
99  protected:
100   int version_;
101   vpx_bit_depth_t bit_depth_;
102   int mask_;
103   FwdTxfmFunc fwd_txfm_;
104   InvTxfmFunc inv_txfm_;
105
106   int16_t *bench_in_;
107   tran_low_t *bench_out_;
108   virtual void Run();
109 };
110
111 void Trans32x32Test::Run() { fwd_txfm_(bench_in_, bench_out_, 32); }
112
113 TEST_P(Trans32x32Test, AccuracyCheck) {
114   ACMRandom rnd(ACMRandom::DeterministicSeed());
115   uint32_t max_error = 0;
116   int64_t total_error = 0;
117   const int count_test_block = 10000;
118   DECLARE_ALIGNED(16, int16_t, test_input_block[kNumCoeffs]);
119   DECLARE_ALIGNED(16, tran_low_t, test_temp_block[kNumCoeffs]);
120   DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
121   DECLARE_ALIGNED(16, uint8_t, src[kNumCoeffs]);
122 #if CONFIG_VP9_HIGHBITDEPTH
123   DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
124   DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
125 #endif
126
127   for (int i = 0; i < count_test_block; ++i) {
128     // Initialize a test block with input range [-mask_, mask_].
129     for (int j = 0; j < kNumCoeffs; ++j) {
130       if (bit_depth_ == VPX_BITS_8) {
131         src[j] = rnd.Rand8();
132         dst[j] = rnd.Rand8();
133         test_input_block[j] = src[j] - dst[j];
134 #if CONFIG_VP9_HIGHBITDEPTH
135       } else {
136         src16[j] = rnd.Rand16() & mask_;
137         dst16[j] = rnd.Rand16() & mask_;
138         test_input_block[j] = src16[j] - dst16[j];
139 #endif
140       }
141     }
142
143     ASM_REGISTER_STATE_CHECK(fwd_txfm_(test_input_block, test_temp_block, 32));
144     if (bit_depth_ == VPX_BITS_8) {
145       ASM_REGISTER_STATE_CHECK(inv_txfm_(test_temp_block, dst, 32));
146 #if CONFIG_VP9_HIGHBITDEPTH
147     } else {
148       ASM_REGISTER_STATE_CHECK(
149           inv_txfm_(test_temp_block, CAST_TO_BYTEPTR(dst16), 32));
150 #endif
151     }
152
153     for (int j = 0; j < kNumCoeffs; ++j) {
154 #if CONFIG_VP9_HIGHBITDEPTH
155       const int32_t diff =
156           bit_depth_ == VPX_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
157 #else
158       const int32_t diff = dst[j] - src[j];
159 #endif
160       const uint32_t error = diff * diff;
161       if (max_error < error) max_error = error;
162       total_error += error;
163     }
164   }
165
166   if (version_ == 1) {
167     max_error /= 2;
168     total_error /= 45;
169   }
170
171   EXPECT_GE(1u << 2 * (bit_depth_ - 8), max_error)
172       << "Error: 32x32 FDCT/IDCT has an individual round-trip error > 1";
173
174   EXPECT_GE(count_test_block << 2 * (bit_depth_ - 8), total_error)
175       << "Error: 32x32 FDCT/IDCT has average round-trip error > 1 per block";
176 }
177
178 TEST_P(Trans32x32Test, CoeffCheck) {
179   ACMRandom rnd(ACMRandom::DeterministicSeed());
180   const int count_test_block = 1000;
181
182   DECLARE_ALIGNED(16, int16_t, input_block[kNumCoeffs]);
183   DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kNumCoeffs]);
184   DECLARE_ALIGNED(16, tran_low_t, output_block[kNumCoeffs]);
185
186   for (int i = 0; i < count_test_block; ++i) {
187     for (int j = 0; j < kNumCoeffs; ++j) {
188       input_block[j] = (rnd.Rand16() & mask_) - (rnd.Rand16() & mask_);
189     }
190
191     const int stride = 32;
192     vpx_fdct32x32_c(input_block, output_ref_block, stride);
193     ASM_REGISTER_STATE_CHECK(fwd_txfm_(input_block, output_block, stride));
194
195     if (version_ == 0) {
196       for (int j = 0; j < kNumCoeffs; ++j)
197         EXPECT_EQ(output_block[j], output_ref_block[j])
198             << "Error: 32x32 FDCT versions have mismatched coefficients";
199     } else {
200       for (int j = 0; j < kNumCoeffs; ++j)
201         EXPECT_GE(6, abs(output_block[j] - output_ref_block[j]))
202             << "Error: 32x32 FDCT rd has mismatched coefficients";
203     }
204   }
205 }
206
207 TEST_P(Trans32x32Test, MemCheck) {
208   ACMRandom rnd(ACMRandom::DeterministicSeed());
209   const int count_test_block = 2000;
210
211   DECLARE_ALIGNED(16, int16_t, input_extreme_block[kNumCoeffs]);
212   DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kNumCoeffs]);
213   DECLARE_ALIGNED(16, tran_low_t, output_block[kNumCoeffs]);
214
215   for (int i = 0; i < count_test_block; ++i) {
216     // Initialize a test block with input range [-mask_, mask_].
217     for (int j = 0; j < kNumCoeffs; ++j) {
218       input_extreme_block[j] = rnd.Rand8() & 1 ? mask_ : -mask_;
219     }
220     if (i == 0) {
221       for (int j = 0; j < kNumCoeffs; ++j) input_extreme_block[j] = mask_;
222     } else if (i == 1) {
223       for (int j = 0; j < kNumCoeffs; ++j) input_extreme_block[j] = -mask_;
224     }
225
226     const int stride = 32;
227     vpx_fdct32x32_c(input_extreme_block, output_ref_block, stride);
228     ASM_REGISTER_STATE_CHECK(
229         fwd_txfm_(input_extreme_block, output_block, stride));
230
231     // The minimum quant value is 4.
232     for (int j = 0; j < kNumCoeffs; ++j) {
233       if (version_ == 0) {
234         EXPECT_EQ(output_block[j], output_ref_block[j])
235             << "Error: 32x32 FDCT versions have mismatched coefficients";
236       } else {
237         EXPECT_GE(6, abs(output_block[j] - output_ref_block[j]))
238             << "Error: 32x32 FDCT rd has mismatched coefficients";
239       }
240       EXPECT_GE(4 * DCT_MAX_VALUE << (bit_depth_ - 8), abs(output_ref_block[j]))
241           << "Error: 32x32 FDCT C has coefficient larger than 4*DCT_MAX_VALUE";
242       EXPECT_GE(4 * DCT_MAX_VALUE << (bit_depth_ - 8), abs(output_block[j]))
243           << "Error: 32x32 FDCT has coefficient larger than "
244           << "4*DCT_MAX_VALUE";
245     }
246   }
247 }
248
249 TEST_P(Trans32x32Test, DISABLED_Speed) {
250   ACMRandom rnd(ACMRandom::DeterministicSeed());
251
252   DECLARE_ALIGNED(16, int16_t, input_extreme_block[kNumCoeffs]);
253   DECLARE_ALIGNED(16, tran_low_t, output_block[kNumCoeffs]);
254
255   bench_in_ = input_extreme_block;
256   bench_out_ = output_block;
257
258   RunNTimes(INT16_MAX);
259   PrintMedian("32x32");
260 }
261
262 TEST_P(Trans32x32Test, InverseAccuracy) {
263   ACMRandom rnd(ACMRandom::DeterministicSeed());
264   const int count_test_block = 1000;
265   DECLARE_ALIGNED(16, int16_t, in[kNumCoeffs]);
266   DECLARE_ALIGNED(16, tran_low_t, coeff[kNumCoeffs]);
267   DECLARE_ALIGNED(16, uint8_t, dst[kNumCoeffs]);
268   DECLARE_ALIGNED(16, uint8_t, src[kNumCoeffs]);
269 #if CONFIG_VP9_HIGHBITDEPTH
270   DECLARE_ALIGNED(16, uint16_t, dst16[kNumCoeffs]);
271   DECLARE_ALIGNED(16, uint16_t, src16[kNumCoeffs]);
272 #endif
273
274   for (int i = 0; i < count_test_block; ++i) {
275     double out_r[kNumCoeffs];
276
277     // Initialize a test block with input range [-255, 255]
278     for (int j = 0; j < kNumCoeffs; ++j) {
279       if (bit_depth_ == VPX_BITS_8) {
280         src[j] = rnd.Rand8();
281         dst[j] = rnd.Rand8();
282         in[j] = src[j] - dst[j];
283 #if CONFIG_VP9_HIGHBITDEPTH
284       } else {
285         src16[j] = rnd.Rand16() & mask_;
286         dst16[j] = rnd.Rand16() & mask_;
287         in[j] = src16[j] - dst16[j];
288 #endif
289       }
290     }
291
292     reference_32x32_dct_2d(in, out_r);
293     for (int j = 0; j < kNumCoeffs; ++j) {
294       coeff[j] = static_cast<tran_low_t>(round(out_r[j]));
295     }
296     if (bit_depth_ == VPX_BITS_8) {
297       ASM_REGISTER_STATE_CHECK(inv_txfm_(coeff, dst, 32));
298 #if CONFIG_VP9_HIGHBITDEPTH
299     } else {
300       ASM_REGISTER_STATE_CHECK(inv_txfm_(coeff, CAST_TO_BYTEPTR(dst16), 32));
301 #endif
302     }
303     for (int j = 0; j < kNumCoeffs; ++j) {
304 #if CONFIG_VP9_HIGHBITDEPTH
305       const int diff =
306           bit_depth_ == VPX_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
307 #else
308       const int diff = dst[j] - src[j];
309 #endif
310       const int error = diff * diff;
311       EXPECT_GE(1, error) << "Error: 32x32 IDCT has error " << error
312                           << " at index " << j;
313     }
314   }
315 }
316
317 using std::make_tuple;
318
319 #if CONFIG_VP9_HIGHBITDEPTH
320 INSTANTIATE_TEST_SUITE_P(
321     C, Trans32x32Test,
322     ::testing::Values(
323         make_tuple(&vpx_highbd_fdct32x32_c, &idct32x32_10, 0, VPX_BITS_10),
324         make_tuple(&vpx_highbd_fdct32x32_rd_c, &idct32x32_10, 1, VPX_BITS_10),
325         make_tuple(&vpx_highbd_fdct32x32_c, &idct32x32_12, 0, VPX_BITS_12),
326         make_tuple(&vpx_highbd_fdct32x32_rd_c, &idct32x32_12, 1, VPX_BITS_12),
327         make_tuple(&vpx_fdct32x32_c, &vpx_idct32x32_1024_add_c, 0, VPX_BITS_8),
328         make_tuple(&vpx_fdct32x32_rd_c, &vpx_idct32x32_1024_add_c, 1,
329                    VPX_BITS_8)));
330 #else
331 INSTANTIATE_TEST_SUITE_P(
332     C, Trans32x32Test,
333     ::testing::Values(make_tuple(&vpx_fdct32x32_c, &vpx_idct32x32_1024_add_c, 0,
334                                  VPX_BITS_8),
335                       make_tuple(&vpx_fdct32x32_rd_c, &vpx_idct32x32_1024_add_c,
336                                  1, VPX_BITS_8)));
337 #endif  // CONFIG_VP9_HIGHBITDEPTH
338
339 #if HAVE_NEON && !CONFIG_EMULATE_HARDWARE
340 INSTANTIATE_TEST_SUITE_P(
341     NEON, Trans32x32Test,
342     ::testing::Values(make_tuple(&vpx_fdct32x32_neon,
343                                  &vpx_idct32x32_1024_add_neon, 0, VPX_BITS_8),
344                       make_tuple(&vpx_fdct32x32_rd_neon,
345                                  &vpx_idct32x32_1024_add_neon, 1, VPX_BITS_8)));
346 #endif  // HAVE_NEON && !CONFIG_EMULATE_HARDWARE
347
348 #if HAVE_SSE2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
349 INSTANTIATE_TEST_SUITE_P(
350     SSE2, Trans32x32Test,
351     ::testing::Values(make_tuple(&vpx_fdct32x32_sse2,
352                                  &vpx_idct32x32_1024_add_sse2, 0, VPX_BITS_8),
353                       make_tuple(&vpx_fdct32x32_rd_sse2,
354                                  &vpx_idct32x32_1024_add_sse2, 1, VPX_BITS_8)));
355 #endif  // HAVE_SSE2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
356
357 #if HAVE_SSE2 && CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
358 INSTANTIATE_TEST_SUITE_P(
359     SSE2, Trans32x32Test,
360     ::testing::Values(
361         make_tuple(&vpx_highbd_fdct32x32_sse2, &idct32x32_10, 0, VPX_BITS_10),
362         make_tuple(&vpx_highbd_fdct32x32_rd_sse2, &idct32x32_10, 1,
363                    VPX_BITS_10),
364         make_tuple(&vpx_highbd_fdct32x32_sse2, &idct32x32_12, 0, VPX_BITS_12),
365         make_tuple(&vpx_highbd_fdct32x32_rd_sse2, &idct32x32_12, 1,
366                    VPX_BITS_12),
367         make_tuple(&vpx_fdct32x32_sse2, &vpx_idct32x32_1024_add_c, 0,
368                    VPX_BITS_8),
369         make_tuple(&vpx_fdct32x32_rd_sse2, &vpx_idct32x32_1024_add_c, 1,
370                    VPX_BITS_8)));
371 #endif  // HAVE_SSE2 && CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
372
373 #if HAVE_AVX2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
374 INSTANTIATE_TEST_SUITE_P(
375     AVX2, Trans32x32Test,
376     ::testing::Values(make_tuple(&vpx_fdct32x32_avx2,
377                                  &vpx_idct32x32_1024_add_sse2, 0, VPX_BITS_8),
378                       make_tuple(&vpx_fdct32x32_rd_avx2,
379                                  &vpx_idct32x32_1024_add_sse2, 1, VPX_BITS_8)));
380 #endif  // HAVE_AVX2 && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
381
382 #if HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
383 INSTANTIATE_TEST_SUITE_P(
384     MSA, Trans32x32Test,
385     ::testing::Values(make_tuple(&vpx_fdct32x32_msa,
386                                  &vpx_idct32x32_1024_add_msa, 0, VPX_BITS_8),
387                       make_tuple(&vpx_fdct32x32_rd_msa,
388                                  &vpx_idct32x32_1024_add_msa, 1, VPX_BITS_8)));
389 #endif  // HAVE_MSA && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
390
391 #if HAVE_VSX && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
392 INSTANTIATE_TEST_SUITE_P(
393     VSX, Trans32x32Test,
394     ::testing::Values(make_tuple(&vpx_fdct32x32_c, &vpx_idct32x32_1024_add_vsx,
395                                  0, VPX_BITS_8),
396                       make_tuple(&vpx_fdct32x32_rd_vsx,
397                                  &vpx_idct32x32_1024_add_vsx, 1, VPX_BITS_8)));
398 #endif  // HAVE_VSX && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
399
400 #if HAVE_LSX && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
401 INSTANTIATE_TEST_SUITE_P(
402     LSX, Trans32x32Test,
403     ::testing::Values(make_tuple(&vpx_fdct32x32_lsx,
404                                  &vpx_idct32x32_1024_add_lsx, 0, VPX_BITS_8),
405                       make_tuple(&vpx_fdct32x32_rd_lsx,
406                                  &vpx_idct32x32_1024_add_lsx, 1, VPX_BITS_8)));
407 #endif  // HAVE_LSX && !CONFIG_VP9_HIGHBITDEPTH && !CONFIG_EMULATE_HARDWARE
408 }  // namespace