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 <math.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <tuple>
16 #include <vector>
17
18 #include "config/av1_rtcd.h"
19
20 #include "aom_ports/aom_timer.h"
21 #include "av1/common/av1_inv_txfm1d_cfg.h"
22 #include "av1/common/scan.h"
23 #include "test/acm_random.h"
24 #include "test/av1_txfm_test.h"
25 #include "test/util.h"
26
27 using libaom_test::ACMRandom;
28 using libaom_test::bd;
29 using libaom_test::compute_avg_abs_error;
30 using libaom_test::input_base;
31 using libaom_test::InvTxfm2dFunc;
32 using libaom_test::LbdInvTxfm2dFunc;
33
34 using ::testing::Combine;
35 using ::testing::Range;
36 using ::testing::Values;
37
38 using std::vector;
39
40 typedef TX_TYPE TxType;
41 typedef TX_SIZE TxSize;
42
43 namespace {
44
45 static const char *tx_type_name[] = {
46 "DCT_DCT",
47 "ADST_DCT",
48 "DCT_ADST",
49 "ADST_ADST",
50 "FLIPADST_DCT",
51 "DCT_FLIPADST",
52 "FLIPADST_FLIPADST",
53 "ADST_FLIPADST",
54 "FLIPADST_ADST",
55 "IDTX",
56 "V_DCT",
57 "H_DCT",
58 "V_ADST",
59 "H_ADST",
60 "V_FLIPADST",
61 "H_FLIPADST",
62 };
63
64 // AV1InvTxfm2dParam argument list:
65 // tx_type_, tx_size_, max_error_, max_avg_error_
66 typedef std::tuple<TxType, TxSize, int, double> AV1InvTxfm2dParam;
67
68 class AV1InvTxfm2d : public ::testing::TestWithParam<AV1InvTxfm2dParam> {
69 public:
SetUp()70 virtual void SetUp() {
71 tx_type_ = GET_PARAM(0);
72 tx_size_ = GET_PARAM(1);
73 max_error_ = GET_PARAM(2);
74 max_avg_error_ = GET_PARAM(3);
75 }
76
RunRoundtripCheck()77 void RunRoundtripCheck() {
78 int tx_w = tx_size_wide[tx_size_];
79 int tx_h = tx_size_high[tx_size_];
80 int txfm2d_size = tx_w * tx_h;
81 const FwdTxfm2dFunc fwd_txfm_func = libaom_test::fwd_txfm_func_ls[tx_size_];
82 const InvTxfm2dFunc inv_txfm_func = libaom_test::inv_txfm_func_ls[tx_size_];
83 double avg_abs_error = 0;
84 ACMRandom rnd(ACMRandom::DeterministicSeed());
85
86 const int count = 500;
87
88 for (int ci = 0; ci < count; ci++) {
89 DECLARE_ALIGNED(16, int16_t, input[64 * 64]) = { 0 };
90 ASSERT_LE(txfm2d_size, NELEMENTS(input));
91
92 for (int ni = 0; ni < txfm2d_size; ++ni) {
93 if (ci == 0) {
94 int extreme_input = input_base - 1;
95 input[ni] = extreme_input; // extreme case
96 } else {
97 input[ni] = rnd.Rand16() % input_base;
98 }
99 }
100
101 DECLARE_ALIGNED(16, uint16_t, expected[64 * 64]) = { 0 };
102 ASSERT_LE(txfm2d_size, NELEMENTS(expected));
103 if (TxfmUsesApproximation()) {
104 // Compare reference forward HT + inverse HT vs forward HT + inverse HT.
105 double ref_input[64 * 64];
106 ASSERT_LE(txfm2d_size, NELEMENTS(ref_input));
107 for (int ni = 0; ni < txfm2d_size; ++ni) {
108 ref_input[ni] = input[ni];
109 }
110 double ref_coeffs[64 * 64] = { 0 };
111 ASSERT_LE(txfm2d_size, NELEMENTS(ref_coeffs));
112 ASSERT_EQ(tx_type_, static_cast<TxType>(DCT_DCT));
113 libaom_test::reference_hybrid_2d(ref_input, ref_coeffs, tx_type_,
114 tx_size_);
115 DECLARE_ALIGNED(16, int32_t, ref_coeffs_int[64 * 64]) = { 0 };
116 ASSERT_LE(txfm2d_size, NELEMENTS(ref_coeffs_int));
117 for (int ni = 0; ni < txfm2d_size; ++ni) {
118 ref_coeffs_int[ni] = (int32_t)round(ref_coeffs[ni]);
119 }
120 inv_txfm_func(ref_coeffs_int, expected, tx_w, tx_type_, bd);
121 } else {
122 // Compare original input vs forward HT + inverse HT.
123 for (int ni = 0; ni < txfm2d_size; ++ni) {
124 expected[ni] = input[ni];
125 }
126 }
127
128 DECLARE_ALIGNED(16, int32_t, coeffs[64 * 64]) = { 0 };
129 ASSERT_LE(txfm2d_size, NELEMENTS(coeffs));
130 fwd_txfm_func(input, coeffs, tx_w, tx_type_, bd);
131
132 DECLARE_ALIGNED(16, uint16_t, actual[64 * 64]) = { 0 };
133 ASSERT_LE(txfm2d_size, NELEMENTS(actual));
134 inv_txfm_func(coeffs, actual, tx_w, tx_type_, bd);
135
136 double actual_max_error = 0;
137 for (int ni = 0; ni < txfm2d_size; ++ni) {
138 const double this_error = abs(expected[ni] - actual[ni]);
139 actual_max_error = AOMMAX(actual_max_error, this_error);
140 }
141 EXPECT_GE(max_error_, actual_max_error)
142 << " tx_w: " << tx_w << " tx_h " << tx_h << " tx_type: " << tx_type_;
143 if (actual_max_error > max_error_) { // exit early.
144 break;
145 }
146 avg_abs_error += compute_avg_abs_error<uint16_t, uint16_t>(
147 expected, actual, txfm2d_size);
148 }
149
150 avg_abs_error /= count;
151 EXPECT_GE(max_avg_error_, avg_abs_error)
152 << " tx_w: " << tx_w << " tx_h " << tx_h << " tx_type: " << tx_type_;
153 }
154
155 private:
TxfmUsesApproximation()156 bool TxfmUsesApproximation() {
157 if (tx_size_wide[tx_size_] == 64 || tx_size_high[tx_size_] == 64) {
158 return true;
159 }
160 return false;
161 }
162
163 int max_error_;
164 double max_avg_error_;
165 TxType tx_type_;
166 TxSize tx_size_;
167 };
168
169 static int max_error_ls[TX_SIZES_ALL] = {
170 2, // 4x4 transform
171 2, // 8x8 transform
172 2, // 16x16 transform
173 4, // 32x32 transform
174 3, // 64x64 transform
175 2, // 4x8 transform
176 2, // 8x4 transform
177 2, // 8x16 transform
178 2, // 16x8 transform
179 3, // 16x32 transform
180 3, // 32x16 transform
181 5, // 32x64 transform
182 5, // 64x32 transform
183 2, // 4x16 transform
184 2, // 16x4 transform
185 2, // 8x32 transform
186 2, // 32x8 transform
187 3, // 16x64 transform
188 3, // 64x16 transform
189 };
190
191 static double avg_error_ls[TX_SIZES_ALL] = {
192 0.002, // 4x4 transform
193 0.05, // 8x8 transform
194 0.07, // 16x16 transform
195 0.4, // 32x32 transform
196 0.3, // 64x64 transform
197 0.02, // 4x8 transform
198 0.02, // 8x4 transform
199 0.04, // 8x16 transform
200 0.07, // 16x8 transform
201 0.4, // 16x32 transform
202 0.5, // 32x16 transform
203 0.38, // 32x64 transform
204 0.39, // 64x32 transform
205 0.2, // 4x16 transform
206 0.2, // 16x4 transform
207 0.2, // 8x32 transform
208 0.2, // 32x8 transform
209 0.38, // 16x64 transform
210 0.38, // 64x16 transform
211 };
212
GetInvTxfm2dParamList()213 vector<AV1InvTxfm2dParam> GetInvTxfm2dParamList() {
214 vector<AV1InvTxfm2dParam> param_list;
215 for (int s = 0; s < TX_SIZES; ++s) {
216 const int max_error = max_error_ls[s];
217 const double avg_error = avg_error_ls[s];
218 for (int t = 0; t < TX_TYPES; ++t) {
219 const TxType tx_type = static_cast<TxType>(t);
220 const TxSize tx_size = static_cast<TxSize>(s);
221 if (libaom_test::IsTxSizeTypeValid(tx_size, tx_type)) {
222 param_list.push_back(
223 AV1InvTxfm2dParam(tx_type, tx_size, max_error, avg_error));
224 }
225 }
226 }
227 return param_list;
228 }
229
230 INSTANTIATE_TEST_SUITE_P(C, AV1InvTxfm2d,
231 ::testing::ValuesIn(GetInvTxfm2dParamList()));
232
TEST_P(AV1InvTxfm2d,RunRoundtripCheck)233 TEST_P(AV1InvTxfm2d, RunRoundtripCheck) { RunRoundtripCheck(); }
234
TEST(AV1InvTxfm2d,CfgTest)235 TEST(AV1InvTxfm2d, CfgTest) {
236 for (int bd_idx = 0; bd_idx < BD_NUM; ++bd_idx) {
237 int bd = libaom_test::bd_arr[bd_idx];
238 int8_t low_range = libaom_test::low_range_arr[bd_idx];
239 int8_t high_range = libaom_test::high_range_arr[bd_idx];
240 for (int tx_size = 0; tx_size < TX_SIZES_ALL; ++tx_size) {
241 for (int tx_type = 0; tx_type < TX_TYPES; ++tx_type) {
242 if (libaom_test::IsTxSizeTypeValid(static_cast<TxSize>(tx_size),
243 static_cast<TxType>(tx_type)) ==
244 false) {
245 continue;
246 }
247 TXFM_2D_FLIP_CFG cfg;
248 av1_get_inv_txfm_cfg(static_cast<TxType>(tx_type),
249 static_cast<TxSize>(tx_size), &cfg);
250 int8_t stage_range_col[MAX_TXFM_STAGE_NUM];
251 int8_t stage_range_row[MAX_TXFM_STAGE_NUM];
252 av1_gen_inv_stage_range(stage_range_col, stage_range_row, &cfg,
253 static_cast<TxSize>(tx_size), bd);
254 libaom_test::txfm_stage_range_check(stage_range_col, cfg.stage_num_col,
255 cfg.cos_bit_col, low_range,
256 high_range);
257 libaom_test::txfm_stage_range_check(stage_range_row, cfg.stage_num_row,
258 cfg.cos_bit_row, low_range,
259 high_range);
260 }
261 }
262 }
263 }
264
265 typedef std::tuple<const LbdInvTxfm2dFunc> AV1LbdInvTxfm2dParam;
266 class AV1LbdInvTxfm2d : public ::testing::TestWithParam<AV1LbdInvTxfm2dParam> {
267 public:
SetUp()268 virtual void SetUp() { target_func_ = GET_PARAM(0); }
269 void RunAV1InvTxfm2dTest(TxType tx_type, TxSize tx_size, int run_times,
270 int gt_int16 = 0);
271
272 private:
273 LbdInvTxfm2dFunc target_func_;
274 };
275 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AV1LbdInvTxfm2d);
276
RunAV1InvTxfm2dTest(TxType tx_type,TxSize tx_size,int run_times,int gt_int16)277 void AV1LbdInvTxfm2d::RunAV1InvTxfm2dTest(TxType tx_type, TxSize tx_size,
278 int run_times, int gt_int16) {
279 FwdTxfm2dFunc fwd_func_ = libaom_test::fwd_txfm_func_ls[tx_size];
280 InvTxfm2dFunc ref_func_ = libaom_test::inv_txfm_func_ls[tx_size];
281 if (fwd_func_ == NULL || ref_func_ == NULL || target_func_ == NULL) {
282 return;
283 }
284 const int bd = 8;
285 const int BLK_WIDTH = 64;
286 const int BLK_SIZE = BLK_WIDTH * BLK_WIDTH;
287 DECLARE_ALIGNED(16, int16_t, input[BLK_SIZE]) = { 0 };
288 DECLARE_ALIGNED(32, int32_t, inv_input[BLK_SIZE]) = { 0 };
289 DECLARE_ALIGNED(16, uint8_t, output[BLK_SIZE]) = { 0 };
290 DECLARE_ALIGNED(16, uint16_t, ref_output[BLK_SIZE]) = { 0 };
291 int stride = BLK_WIDTH;
292 int rows = tx_size_high[tx_size];
293 int cols = tx_size_wide[tx_size];
294 const int rows_nonezero = AOMMIN(32, rows);
295 const int cols_nonezero = AOMMIN(32, cols);
296 run_times /= (rows * cols);
297 run_times = AOMMAX(1, run_times);
298 const SCAN_ORDER *scan_order = get_default_scan(tx_size, tx_type);
299 const int16_t *scan = scan_order->scan;
300 const int16_t eobmax = rows_nonezero * cols_nonezero;
301 ACMRandom rnd(ACMRandom::DeterministicSeed());
302 int randTimes = run_times == 1 ? (eobmax + 500) : 1;
303
304 for (int cnt = 0; cnt < randTimes; ++cnt) {
305 const int16_t max_in = (1 << (bd)) - 1;
306 for (int r = 0; r < BLK_WIDTH; ++r) {
307 for (int c = 0; c < BLK_WIDTH; ++c) {
308 input[r * cols + c] = (cnt == 0) ? max_in : rnd.Rand8Extremes();
309 output[r * stride + c] = (cnt == 0) ? 128 : rnd.Rand8();
310 ref_output[r * stride + c] = output[r * stride + c];
311 }
312 }
313 fwd_func_(input, inv_input, stride, tx_type, bd);
314
315 // produce eob input by setting high freq coeffs to zero
316 const int eob = AOMMIN(cnt + 1, eobmax);
317 for (int i = eob; i < eobmax; i++) {
318 inv_input[scan[i]] = 0;
319 }
320 if (gt_int16) {
321 inv_input[scan[eob - 1]] = ((int32_t)INT16_MAX * 100 / 141);
322 }
323 aom_usec_timer timer;
324 aom_usec_timer_start(&timer);
325 for (int i = 0; i < run_times; ++i) {
326 ref_func_(inv_input, ref_output, stride, tx_type, bd);
327 }
328 aom_usec_timer_mark(&timer);
329 const double time1 = static_cast<double>(aom_usec_timer_elapsed(&timer));
330 aom_usec_timer_start(&timer);
331 for (int i = 0; i < run_times; ++i) {
332 target_func_(inv_input, output, stride, tx_type, tx_size, eob);
333 }
334 aom_usec_timer_mark(&timer);
335 const double time2 = static_cast<double>(aom_usec_timer_elapsed(&timer));
336 if (run_times > 10) {
337 printf("txfm[%d] %3dx%-3d:%7.2f/%7.2fns", tx_type, cols, rows, time1,
338 time2);
339 printf("(%3.2f)\n", time1 / time2);
340 }
341 for (int r = 0; r < rows; ++r) {
342 for (int c = 0; c < cols; ++c) {
343 uint8_t ref_value = static_cast<uint8_t>(ref_output[r * stride + c]);
344 if (ref_value != output[r * stride + c]) {
345 printf(" ");
346 }
347 ASSERT_EQ(ref_value, output[r * stride + c])
348 << "[" << r << "," << c << "] " << cnt
349 << " tx_size: " << static_cast<int>(tx_size)
350 << " tx_type: " << tx_type_name[tx_type] << " eob " << eob;
351 }
352 }
353 }
354 }
355
TEST_P(AV1LbdInvTxfm2d,match)356 TEST_P(AV1LbdInvTxfm2d, match) {
357 for (int j = 0; j < (int)(TX_SIZES_ALL); ++j) {
358 for (int i = 0; i < (int)TX_TYPES; ++i) {
359 if (libaom_test::IsTxSizeTypeValid(static_cast<TxSize>(j),
360 static_cast<TxType>(i))) {
361 RunAV1InvTxfm2dTest(static_cast<TxType>(i), static_cast<TxSize>(j), 1);
362 }
363 }
364 }
365 }
366
TEST_P(AV1LbdInvTxfm2d,gt_int16)367 TEST_P(AV1LbdInvTxfm2d, gt_int16) {
368 static const TxType types[] = { DCT_DCT, ADST_DCT, FLIPADST_DCT, IDTX,
369 V_DCT, H_DCT, H_ADST, H_FLIPADST };
370 for (int j = 0; j < (int)(TX_SIZES_ALL); ++j) {
371 const TxSize sz = static_cast<TxSize>(j);
372 for (uint8_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) {
373 const TxType tp = types[i];
374 if (libaom_test::IsTxSizeTypeValid(sz, tp)) {
375 RunAV1InvTxfm2dTest(tp, sz, 1, 1);
376 }
377 }
378 }
379 }
380
TEST_P(AV1LbdInvTxfm2d,DISABLED_Speed)381 TEST_P(AV1LbdInvTxfm2d, DISABLED_Speed) {
382 for (int j = 1; j < (int)(TX_SIZES_ALL); ++j) {
383 for (int i = 0; i < (int)TX_TYPES; ++i) {
384 if (libaom_test::IsTxSizeTypeValid(static_cast<TxSize>(j),
385 static_cast<TxType>(i))) {
386 RunAV1InvTxfm2dTest(static_cast<TxType>(i), static_cast<TxSize>(j),
387 10000000);
388 }
389 }
390 }
391 }
392
393 #if HAVE_SSSE3
394 #if defined(_MSC_VER) || defined(__SSSE3__)
395 #include "av1/common/x86/av1_inv_txfm_ssse3.h"
396 INSTANTIATE_TEST_SUITE_P(SSSE3, AV1LbdInvTxfm2d,
397 ::testing::Values(av1_lowbd_inv_txfm2d_add_ssse3));
398 #endif // _MSC_VER || __SSSE3__
399 #endif // HAVE_SSSE3
400
401 #if HAVE_AVX2
402 extern "C" void av1_lowbd_inv_txfm2d_add_avx2(const int32_t *input,
403 uint8_t *output, int stride,
404 TxType tx_type, TxSize tx_size,
405 int eob);
406
407 INSTANTIATE_TEST_SUITE_P(AVX2, AV1LbdInvTxfm2d,
408 ::testing::Values(av1_lowbd_inv_txfm2d_add_avx2));
409 #endif // HAVE_AVX2
410
411 // TODO(yunqing): Re-enable this unit test for NEON version after the functions
412 // are fixed.
413 #if HAVE_NEON
414 extern "C" void av1_lowbd_inv_txfm2d_add_neon(const int32_t *input,
415 uint8_t *output, int stride,
416 TX_TYPE tx_type, TX_SIZE tx_size,
417 int eob);
418
419 INSTANTIATE_TEST_SUITE_P(NEON, AV1LbdInvTxfm2d,
420 ::testing::Values(av1_lowbd_inv_txfm2d_add_neon));
421 #endif // HAVE_NEON
422
423 } // namespace
424