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