blob: ab8a6f88ffadb573bdbda703249fbf93d20e7c38 [file] [log] [blame]
Angie Chiang444acd72015-10-27 16:59:02 -07001/*
Yaowu Xubde4ac82016-11-28 15:26:06 -08002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Angie Chiang444acd72015-10-27 16:59:02 -07003 *
Yaowu Xubde4ac82016-11-28 15:26:06 -08004 * 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.
Angie Chiang444acd72015-10-27 16:59:02 -070010 */
11
Sebastien Alaiwand02642f2017-07-10 11:41:44 +020012#include <math.h>
13
Yaowu Xuf883b422016-08-30 14:01:10 -070014#include "test/av1_txfm_test.h"
Sebastien Alaiwanb5138be2017-07-04 12:03:01 +020015#include "test/util.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070016#include "av1/common/av1_inv_txfm1d.h"
Urvang Joshi23145662017-12-13 12:25:46 -080017#include "av1/encoder/av1_fwd_txfm1d.h"
Angie Chiang444acd72015-10-27 16:59:02 -070018
Pascal Massiminoa6fe4322019-04-09 09:49:24 +020019typedef TX_SIZE TxSize;
20
Yaowu Xuc27fc142016-08-22 16:08:15 -070021using libaom_test::ACMRandom;
22using libaom_test::input_base;
Angie Chiang444acd72015-10-27 16:59:02 -070023
24namespace {
Angie Chiang29a06a12016-03-29 14:27:50 -070025const int txfm_type_num = 2;
Yaowu Xud3d41592018-02-14 13:26:52 -080026const int txfm_size_ls[] = { 4, 8, 16, 32, 64 };
Angie Chiang444acd72015-10-27 16:59:02 -070027
Debargha Mukherjeeaa84f3e2018-01-04 12:45:10 -080028const TxfmFunc fwd_txfm_func_ls[][txfm_type_num] = {
Yaowu Xueb5e4e22020-04-06 14:17:55 -070029 { av1_fdct4, av1_fadst4 }, { av1_fdct8, av1_fadst8 },
James Zern664f04d2022-05-24 17:30:58 -070030 { av1_fdct16, av1_fadst16 }, { av1_fdct32, nullptr },
31 { av1_fdct64, nullptr },
clang-format3a826f12016-08-11 17:46:05 -070032};
Angie Chiang444acd72015-10-27 16:59:02 -070033
Debargha Mukherjeeaa84f3e2018-01-04 12:45:10 -080034const TxfmFunc inv_txfm_func_ls[][txfm_type_num] = {
Yaowu Xueb5e4e22020-04-06 14:17:55 -070035 { av1_idct4, av1_iadst4 }, { av1_idct8, av1_iadst8 },
James Zern664f04d2022-05-24 17:30:58 -070036 { av1_idct16, av1_iadst16 }, { av1_idct32, nullptr },
37 { av1_idct64, nullptr },
clang-format3a826f12016-08-11 17:46:05 -070038};
Angie Chiang444acd72015-10-27 16:59:02 -070039
40// the maximum stage number of fwd/inv 1d dct/adst txfm is 12
Angie Chiangd4327bc2018-01-22 20:54:04 -080041const int8_t cos_bit = 13;
Angie Chianga87dc762018-02-06 17:16:48 -080042const int8_t range_bit[12] = { 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 };
Angie Chiang444acd72015-10-27 16:59:02 -070043
Sebastien Alaiwand02642f2017-07-10 11:41:44 +020044void reference_idct_1d_int(const int32_t *in, int32_t *out, int size) {
45 double input[64];
46 for (int i = 0; i < size; ++i) input[i] = in[i];
47
48 double output[64];
49 libaom_test::reference_idct_1d(input, output, size);
50
Urvang Joshiab2b36e2017-10-03 11:01:06 -070051 for (int i = 0; i < size; ++i) {
52 ASSERT_GE(output[i], INT32_MIN);
53 ASSERT_LE(output[i], INT32_MAX);
Sebastien Alaiwand02642f2017-07-10 11:41:44 +020054 out[i] = static_cast<int32_t>(round(output[i]));
Urvang Joshiab2b36e2017-10-03 11:01:06 -070055 }
Sebastien Alaiwand02642f2017-07-10 11:41:44 +020056}
57
58void random_matrix(int32_t *dst, int len, ACMRandom *rnd) {
59 const int bits = 16;
60 const int maxVal = (1 << (bits - 1)) - 1;
61 const int minVal = -(1 << (bits - 1));
62 for (int i = 0; i < len; ++i) {
63 if (rnd->Rand8() % 10)
64 dst[i] = minVal + rnd->Rand16() % (1 << bits);
65 else
66 dst[i] = rnd->Rand8() % 2 ? minVal : maxVal;
67 }
68}
69
70TEST(av1_inv_txfm1d, InvAccuracyCheck) {
71 ACMRandom rnd(ACMRandom::DeterministicSeed());
72 const int count_test_block = 20000;
Yaowu Xud3d41592018-02-14 13:26:52 -080073 const int max_error[] = { 6, 10, 19, 31, 40 };
Urvang Joshiab2b36e2017-10-03 11:01:06 -070074 ASSERT_EQ(NELEMENTS(max_error), TX_SIZES);
75 ASSERT_EQ(NELEMENTS(inv_txfm_func_ls), TX_SIZES);
Sebastien Alaiwand02642f2017-07-10 11:41:44 +020076 for (int k = 0; k < count_test_block; ++k) {
77 // choose a random transform to test
Pascal Massiminoa6fe4322019-04-09 09:49:24 +020078 const TxSize tx_size = static_cast<TxSize>(rnd.Rand8() % TX_SIZES);
Urvang Joshiab2b36e2017-10-03 11:01:06 -070079 const int tx_size_pix = txfm_size_ls[tx_size];
80 const TxfmFunc inv_txfm_func = inv_txfm_func_ls[tx_size][0];
Sebastien Alaiwand02642f2017-07-10 11:41:44 +020081
82 int32_t input[64];
Urvang Joshiab2b36e2017-10-03 11:01:06 -070083 random_matrix(input, tx_size_pix, &rnd);
84
Urvang Joshiab2b36e2017-10-03 11:01:06 -070085 // 64x64 transform assumes last 32 values are zero.
86 memset(input + 32, 0, 32 * sizeof(input[0]));
Sebastien Alaiwand02642f2017-07-10 11:41:44 +020087
88 int32_t ref_output[64];
kyslov6fdf9342019-04-09 14:53:43 -070089 memset(ref_output, 0, sizeof(ref_output));
Urvang Joshiab2b36e2017-10-03 11:01:06 -070090 reference_idct_1d_int(input, ref_output, tx_size_pix);
Sebastien Alaiwand02642f2017-07-10 11:41:44 +020091
92 int32_t output[64];
kyslov6fdf9342019-04-09 14:53:43 -070093 memset(output, 0, sizeof(output));
Urvang Joshiab2b36e2017-10-03 11:01:06 -070094 inv_txfm_func(input, output, cos_bit, range_bit);
Sebastien Alaiwand02642f2017-07-10 11:41:44 +020095
Urvang Joshiab2b36e2017-10-03 11:01:06 -070096 for (int i = 0; i < tx_size_pix; ++i) {
97 EXPECT_LE(abs(output[i] - ref_output[i]), max_error[tx_size])
98 << "tx_size = " << tx_size << ", i = " << i
99 << ", output[i] = " << output[i]
100 << ", ref_output[i] = " << ref_output[i];
Sebastien Alaiwand02642f2017-07-10 11:41:44 +0200101 }
102 }
103}
104
Sebastien Alaiwan36967372017-09-27 17:02:20 +0200105static INLINE int get_max_bit(int x) {
106 int max_bit = -1;
107 while (x) {
108 x = x >> 1;
109 max_bit++;
110 }
111 return max_bit;
112}
113
114TEST(av1_inv_txfm1d, get_max_bit) {
115 int max_bit = get_max_bit(8);
116 EXPECT_EQ(max_bit, 3);
117}
118
Yaowu Xuf883b422016-08-30 14:01:10 -0700119TEST(av1_inv_txfm1d, round_trip) {
Angie Chiang444acd72015-10-27 16:59:02 -0700120 ACMRandom rnd(ACMRandom::DeterministicSeed());
Sebastien Alaiwaned9e0d02017-07-13 10:44:58 +0200121 for (int si = 0; si < NELEMENTS(fwd_txfm_func_ls); ++si) {
Angie Chiang444acd72015-10-27 16:59:02 -0700122 int txfm_size = txfm_size_ls[si];
Angie Chiang444acd72015-10-27 16:59:02 -0700123
124 for (int ti = 0; ti < txfm_type_num; ++ti) {
Sebastien Alaiwan13f77be2017-03-08 15:25:10 +0100125 TxfmFunc fwd_txfm_func = fwd_txfm_func_ls[si][ti];
126 TxfmFunc inv_txfm_func = inv_txfm_func_ls[si][ti];
Angie Chiang444acd72015-10-27 16:59:02 -0700127 int max_error = 2;
128
Sebastien Alaiwan7889e5f2017-03-08 15:26:27 +0100129 if (!fwd_txfm_func) continue;
Angie Chiang444acd72015-10-27 16:59:02 -0700130
Sebastien Alaiwan7889e5f2017-03-08 15:26:27 +0100131 const int count_test_block = 5000;
132 for (int ci = 0; ci < count_test_block; ++ci) {
Sebastien Alaiwan1f40e222017-03-08 15:27:20 +0100133 int32_t input[64];
134 int32_t output[64];
135 int32_t round_trip_output[64];
136
Sebastien Alaiwan8b730a52017-07-14 08:12:54 +0200137 ASSERT_LE(txfm_size, NELEMENTS(input));
Sebastien Alaiwan1f40e222017-03-08 15:27:20 +0100138
Sebastien Alaiwan7889e5f2017-03-08 15:26:27 +0100139 for (int ni = 0; ni < txfm_size; ++ni) {
140 input[ni] = rnd.Rand16() % input_base - rnd.Rand16() % input_base;
141 }
Angie Chiang444acd72015-10-27 16:59:02 -0700142
Sebastien Alaiwan7889e5f2017-03-08 15:26:27 +0100143 fwd_txfm_func(input, output, cos_bit, range_bit);
144 inv_txfm_func(output, round_trip_output, cos_bit, range_bit);
145
146 for (int ni = 0; ni < txfm_size; ++ni) {
147 int node_err =
148 abs(input[ni] - round_shift(round_trip_output[ni],
149 get_max_bit(txfm_size) - 1));
150 EXPECT_LE(node_err, max_error);
Angie Chiang444acd72015-10-27 16:59:02 -0700151 }
152 }
153 }
Angie Chiang444acd72015-10-27 16:59:02 -0700154 }
155}
156
157} // namespace