1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
13 
14 #include "config/av1_rtcd.h"
15 
16 #include "test/acm_random.h"
17 #include "test/av1_txfm_test.h"
18 #include "test/clear_system_state.h"
19 #include "test/register_state_check.h"
20 #include "test/util.h"
21 #include "av1/common/enums.h"
22 #include "av1/common/scan.h"
23 #include "aom_dsp/aom_dsp_common.h"
24 #include "aom_ports/mem.h"
25 
26 namespace {
27 
28 using ::testing::tuple;
29 using libaom_test::ACMRandom;
30 
31 typedef void (*HbdHtFunc)(const int16_t *input, int32_t *output, int stride,
32                           TX_TYPE tx_type, int bd);
33 
34 typedef void (*IHbdHtFunc)(const int32_t *coeff, uint16_t *output, int stride,
35                            TX_TYPE tx_type, int bd);
36 
37 // Test parameter argument list:
38 //   <transform reference function,
39 //    optimized inverse transform function,
40 //    inverse transform reference function,
41 //    num_coeffs,
42 //    tx_type,
43 //    bit_depth>
44 typedef tuple<HbdHtFunc, IHbdHtFunc, IHbdHtFunc, int, TX_TYPE, int> IHbdHtParam;
45 
46 class AV1HighbdInvHTNxN : public ::testing::TestWithParam<IHbdHtParam> {
47  public:
~AV1HighbdInvHTNxN()48   virtual ~AV1HighbdInvHTNxN() {}
49 
SetUp()50   virtual void SetUp() {
51     txfm_ref_ = GET_PARAM(0);
52     inv_txfm_ = GET_PARAM(1);
53     inv_txfm_ref_ = GET_PARAM(2);
54     num_coeffs_ = GET_PARAM(3);
55     tx_type_ = GET_PARAM(4);
56     bit_depth_ = GET_PARAM(5);
57 
58     input_ = reinterpret_cast<int16_t *>(
59         aom_memalign(16, sizeof(input_[0]) * num_coeffs_));
60 
61     // Note:
62     // Inverse transform input buffer is 32-byte aligned
63     // Refer to <root>/av1/encoder/context_tree.c, function,
64     // void alloc_mode_context().
65     coeffs_ = reinterpret_cast<int32_t *>(
66         aom_memalign(32, sizeof(coeffs_[0]) * num_coeffs_));
67     output_ = reinterpret_cast<uint16_t *>(
68         aom_memalign(32, sizeof(output_[0]) * num_coeffs_));
69     output_ref_ = reinterpret_cast<uint16_t *>(
70         aom_memalign(32, sizeof(output_ref_[0]) * num_coeffs_));
71   }
72 
TearDown()73   virtual void TearDown() {
74     aom_free(input_);
75     aom_free(coeffs_);
76     aom_free(output_);
77     aom_free(output_ref_);
78     libaom_test::ClearSystemState();
79   }
80 
81  protected:
82   void RunBitexactCheck();
83 
84  private:
GetStride() const85   int GetStride() const {
86     if (16 == num_coeffs_) {
87       return 4;
88     } else if (64 == num_coeffs_) {
89       return 8;
90     } else if (256 == num_coeffs_) {
91       return 16;
92     } else if (1024 == num_coeffs_) {
93       return 32;
94     } else if (4096 == num_coeffs_) {
95       return 64;
96     } else {
97       return 0;
98     }
99   }
100 
101   HbdHtFunc txfm_ref_;
102   IHbdHtFunc inv_txfm_;
103   IHbdHtFunc inv_txfm_ref_;
104   int num_coeffs_;
105   TX_TYPE tx_type_;
106   int bit_depth_;
107 
108   int16_t *input_;
109   int32_t *coeffs_;
110   uint16_t *output_;
111   uint16_t *output_ref_;
112 };
113 
RunBitexactCheck()114 void AV1HighbdInvHTNxN::RunBitexactCheck() {
115   ACMRandom rnd(ACMRandom::DeterministicSeed());
116   const int stride = GetStride();
117   const int num_tests = 20000;
118   const uint16_t mask = (1 << bit_depth_) - 1;
119 
120   for (int i = 0; i < num_tests; ++i) {
121     for (int j = 0; j < num_coeffs_; ++j) {
122       input_[j] = (rnd.Rand16() & mask) - (rnd.Rand16() & mask);
123       output_ref_[j] = rnd.Rand16() & mask;
124       output_[j] = output_ref_[j];
125     }
126 
127     txfm_ref_(input_, coeffs_, stride, tx_type_, bit_depth_);
128     inv_txfm_ref_(coeffs_, output_ref_, stride, tx_type_, bit_depth_);
129     ASM_REGISTER_STATE_CHECK(
130         inv_txfm_(coeffs_, output_, stride, tx_type_, bit_depth_));
131 
132     for (int j = 0; j < num_coeffs_; ++j) {
133       EXPECT_EQ(output_ref_[j], output_[j])
134           << "Not bit-exact result at index: " << j << " At test block: " << i;
135     }
136   }
137 }
138 
TEST_P(AV1HighbdInvHTNxN,InvTransResultCheck)139 TEST_P(AV1HighbdInvHTNxN, InvTransResultCheck) { RunBitexactCheck(); }
140 
141 using ::testing::make_tuple;
142 
143 #if HAVE_SSE4_1
144 #define PARAM_LIST_4X4                                   \
145   &av1_fwd_txfm2d_4x4_c, &av1_inv_txfm2d_add_4x4_sse4_1, \
146       &av1_inv_txfm2d_add_4x4_c, 16
147 
148 const IHbdHtParam kArrayIhtParam[] = {
149   // 4x4
150   make_tuple(PARAM_LIST_4X4, DCT_DCT, 10),
151   make_tuple(PARAM_LIST_4X4, DCT_DCT, 12),
152   make_tuple(PARAM_LIST_4X4, ADST_DCT, 10),
153   make_tuple(PARAM_LIST_4X4, ADST_DCT, 12),
154   make_tuple(PARAM_LIST_4X4, DCT_ADST, 10),
155   make_tuple(PARAM_LIST_4X4, DCT_ADST, 12),
156   make_tuple(PARAM_LIST_4X4, ADST_ADST, 10),
157   make_tuple(PARAM_LIST_4X4, ADST_ADST, 12),
158   make_tuple(PARAM_LIST_4X4, FLIPADST_DCT, 10),
159   make_tuple(PARAM_LIST_4X4, FLIPADST_DCT, 12),
160   make_tuple(PARAM_LIST_4X4, DCT_FLIPADST, 10),
161   make_tuple(PARAM_LIST_4X4, DCT_FLIPADST, 12),
162   make_tuple(PARAM_LIST_4X4, FLIPADST_FLIPADST, 10),
163   make_tuple(PARAM_LIST_4X4, FLIPADST_FLIPADST, 12),
164   make_tuple(PARAM_LIST_4X4, ADST_FLIPADST, 10),
165   make_tuple(PARAM_LIST_4X4, ADST_FLIPADST, 12),
166   make_tuple(PARAM_LIST_4X4, FLIPADST_ADST, 10),
167   make_tuple(PARAM_LIST_4X4, FLIPADST_ADST, 12),
168 };
169 
170 INSTANTIATE_TEST_CASE_P(SSE4_1, AV1HighbdInvHTNxN,
171                         ::testing::ValuesIn(kArrayIhtParam));
172 #endif  // HAVE_SSE4_1
173 
174 typedef void (*HighbdInvTxfm2dFunc)(const int32_t *input, uint8_t *output,
175                                     int stride, const TxfmParam *txfm_param);
176 
177 typedef ::testing::tuple<const HighbdInvTxfm2dFunc> AV1HighbdInvTxfm2dParam;
178 class AV1HighbdInvTxfm2d
179     : public ::testing::TestWithParam<AV1HighbdInvTxfm2dParam> {
180  public:
SetUp()181   virtual void SetUp() { target_func_ = GET_PARAM(0); }
182   void RunAV1InvTxfm2dTest(TX_TYPE tx_type, TX_SIZE tx_size, int run_times,
183                            int bit_depth);
184 
185  private:
186   HighbdInvTxfm2dFunc target_func_;
187 };
188 
RunAV1InvTxfm2dTest(TX_TYPE tx_type_,TX_SIZE tx_size_,int run_times,int bit_depth_)189 void AV1HighbdInvTxfm2d::RunAV1InvTxfm2dTest(TX_TYPE tx_type_, TX_SIZE tx_size_,
190                                              int run_times, int bit_depth_) {
191   FwdTxfm2dFunc fwd_func_ = libaom_test::fwd_txfm_func_ls[tx_size_];
192   TxfmParam txfm_param;
193   const int BLK_WIDTH = 64;
194   const int BLK_SIZE = BLK_WIDTH * BLK_WIDTH;
195   DECLARE_ALIGNED(16, int16_t, input[BLK_SIZE]) = { 0 };
196   DECLARE_ALIGNED(32, int32_t, inv_input[BLK_SIZE]) = { 0 };
197   DECLARE_ALIGNED(32, uint16_t, output[BLK_SIZE]) = { 0 };
198   DECLARE_ALIGNED(32, uint16_t, ref_output[BLK_SIZE]) = { 0 };
199   int stride = BLK_WIDTH;
200   int rows = tx_size_high[tx_size_];
201   int cols = tx_size_wide[tx_size_];
202   const int rows_nonezero = AOMMIN(32, rows);
203   const int cols_nonezero = AOMMIN(32, cols);
204   const uint16_t mask = (1 << bit_depth_) - 1;
205   run_times /= (rows * cols);
206   run_times = AOMMAX(1, run_times);
207   const SCAN_ORDER *scan_order = get_default_scan(tx_size_, tx_type_);
208   const int16_t *scan = scan_order->scan;
209   const int16_t eobmax = rows_nonezero * cols_nonezero;
210   ACMRandom rnd(ACMRandom::DeterministicSeed());
211   int randTimes = run_times == 1 ? (eobmax) : 1;
212 
213   txfm_param.tx_type = tx_type_;
214   txfm_param.tx_size = tx_size_;
215   txfm_param.lossless = 0;
216   txfm_param.bd = bit_depth_;
217   txfm_param.is_hbd = 1;
218   txfm_param.tx_set_type = EXT_TX_SET_ALL16;
219 
220   for (int cnt = 0; cnt < randTimes; ++cnt) {
221     for (int r = 0; r < BLK_WIDTH; ++r) {
222       for (int c = 0; c < BLK_WIDTH; ++c) {
223         input[r * cols + c] = (rnd.Rand16() & mask) - (rnd.Rand16() & mask);
224         output[r * stride + c] = rnd.Rand16() & mask;
225 
226         ref_output[r * stride + c] = output[r * stride + c];
227       }
228     }
229     fwd_func_(input, inv_input, stride, tx_type_, bit_depth_);
230 
231     // produce eob input by setting high freq coeffs to zero
232     const int eob = AOMMIN(cnt + 1, eobmax);
233     for (int i = eob; i < eobmax; i++) {
234       inv_input[scan[i]] = 0;
235     }
236     txfm_param.eob = eob;
237     aom_usec_timer ref_timer, test_timer;
238 
239     aom_usec_timer_start(&ref_timer);
240     for (int i = 0; i < run_times; ++i) {
241       av1_highbd_inv_txfm_add_c(inv_input, CONVERT_TO_BYTEPTR(ref_output),
242                                 stride, &txfm_param);
243     }
244     aom_usec_timer_mark(&ref_timer);
245     const int elapsed_time_c =
246         static_cast<int>(aom_usec_timer_elapsed(&ref_timer));
247 
248     aom_usec_timer_start(&test_timer);
249     for (int i = 0; i < run_times; ++i) {
250       target_func_(inv_input, CONVERT_TO_BYTEPTR(output), stride, &txfm_param);
251     }
252     aom_usec_timer_mark(&test_timer);
253     const int elapsed_time_simd =
254         static_cast<int>(aom_usec_timer_elapsed(&test_timer));
255     if (run_times > 10) {
256       printf(
257           "txfm_size[%d] \t txfm_type[%d] \t c_time=%d \t simd_time=%d \t "
258           "gain=%d \n",
259           tx_size_, tx_type_, elapsed_time_c, elapsed_time_simd,
260           (elapsed_time_c / elapsed_time_simd));
261     } else {
262       for (int r = 0; r < rows; ++r) {
263         for (int c = 0; c < cols; ++c) {
264           ASSERT_EQ(ref_output[r * stride + c], output[r * stride + c])
265               << "[" << r << "," << c << "] " << cnt
266               << " tx_size: " << static_cast<int>(tx_size_)
267               << " tx_type: " << tx_type_ << " eob " << eob;
268         }
269       }
270     }
271   }
272 }
273 
TEST_P(AV1HighbdInvTxfm2d,match)274 TEST_P(AV1HighbdInvTxfm2d, match) {
275   int bitdepth_ar[2] = { 10, 12 };
276   for (int k = 0; k < 2; ++k) {
277     int bd = bitdepth_ar[k];
278     for (int j = 0; j < (int)(TX_SIZES_ALL); ++j) {
279       for (int i = 0; i < (int)TX_TYPES; ++i) {
280         if (libaom_test::IsTxSizeTypeValid(static_cast<TX_SIZE>(j),
281                                            static_cast<TX_TYPE>(i))) {
282           RunAV1InvTxfm2dTest(static_cast<TX_TYPE>(i), static_cast<TX_SIZE>(j),
283                               1, bd);
284         }
285       }
286     }
287   }
288 }
289 
TEST_P(AV1HighbdInvTxfm2d,DISABLED_Speed)290 TEST_P(AV1HighbdInvTxfm2d, DISABLED_Speed) {
291   int bitdepth_ar[2] = { 10, 12 };
292   for (int k = 0; k < 2; ++k) {
293     int bd = bitdepth_ar[k];
294     for (int j = 0; j < (int)(TX_SIZES_ALL); ++j) {
295       for (int i = 0; i < (int)TX_TYPES; ++i) {
296         if (libaom_test::IsTxSizeTypeValid(static_cast<TX_SIZE>(j),
297                                            static_cast<TX_TYPE>(i))) {
298           RunAV1InvTxfm2dTest(static_cast<TX_TYPE>(i), static_cast<TX_SIZE>(j),
299                               1000000, bd);
300         }
301       }
302     }
303   }
304 }
305 
306 #if HAVE_SSE4_1
307 INSTANTIATE_TEST_CASE_P(SSE4_1, AV1HighbdInvTxfm2d,
308                         ::testing::Values(av1_highbd_inv_txfm_add_sse4_1));
309 #endif
310 
311 #if HAVE_AVX2
312 INSTANTIATE_TEST_CASE_P(AVX2, AV1HighbdInvTxfm2d,
313                         ::testing::Values(av1_highbd_inv_txfm_add_avx2));
314 #endif
315 }  // namespace
316