xref: /aosp_15_r20/external/libaom/test/av1_inv_txfm1d_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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 
14 #include "test/av1_txfm_test.h"
15 #include "test/util.h"
16 #include "av1/common/av1_inv_txfm1d.h"
17 #include "av1/encoder/av1_fwd_txfm1d.h"
18 
19 typedef TX_SIZE TxSize;
20 
21 using libaom_test::ACMRandom;
22 using libaom_test::input_base;
23 
24 namespace {
25 const int txfm_type_num = 2;
26 const int txfm_size_ls[] = { 4, 8, 16, 32, 64 };
27 
28 const TxfmFunc fwd_txfm_func_ls[][txfm_type_num] = {
29   { av1_fdct4, av1_fadst4 },   { av1_fdct8, av1_fadst8 },
30   { av1_fdct16, av1_fadst16 }, { av1_fdct32, nullptr },
31   { av1_fdct64, nullptr },
32 };
33 
34 const TxfmFunc inv_txfm_func_ls[][txfm_type_num] = {
35   { av1_idct4, av1_iadst4 },   { av1_idct8, av1_iadst8 },
36   { av1_idct16, av1_iadst16 }, { av1_idct32, nullptr },
37   { av1_idct64, nullptr },
38 };
39 
40 // the maximum stage number of fwd/inv 1d dct/adst txfm is 12
41 const int8_t cos_bit = 13;
42 const int8_t range_bit[12] = { 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 };
43 
reference_idct_1d_int(const int32_t * in,int32_t * out,int size)44 void 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 
51   for (int i = 0; i < size; ++i) {
52     ASSERT_GE(output[i], INT32_MIN);
53     ASSERT_LE(output[i], INT32_MAX);
54     out[i] = static_cast<int32_t>(round(output[i]));
55   }
56 }
57 
random_matrix(int32_t * dst,int len,ACMRandom * rnd)58 void 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 
TEST(av1_inv_txfm1d,InvAccuracyCheck)70 TEST(av1_inv_txfm1d, InvAccuracyCheck) {
71   ACMRandom rnd(ACMRandom::DeterministicSeed());
72   const int count_test_block = 20000;
73   const int max_error[] = { 6, 10, 19, 31, 40 };
74   ASSERT_EQ(NELEMENTS(max_error), TX_SIZES);
75   ASSERT_EQ(NELEMENTS(inv_txfm_func_ls), TX_SIZES);
76   for (int i = 0; i < count_test_block; ++i) {
77     // choose a random transform to test
78     const TxSize tx_size = static_cast<TxSize>(rnd.Rand8() % TX_SIZES);
79     const int txfm_size = txfm_size_ls[tx_size];
80     const TxfmFunc inv_txfm_func = inv_txfm_func_ls[tx_size][0];
81 
82     int32_t input[64];
83     random_matrix(input, txfm_size, &rnd);
84 
85     // 64x64 transform assumes last 32 values are zero.
86     memset(input + 32, 0, 32 * sizeof(input[0]));
87 
88     int32_t ref_output[64];
89     memset(ref_output, 0, sizeof(ref_output));
90     reference_idct_1d_int(input, ref_output, txfm_size);
91 
92     int32_t output[64];
93     memset(output, 0, sizeof(output));
94     inv_txfm_func(input, output, cos_bit, range_bit);
95 
96     for (int ni = 0; ni < txfm_size; ++ni) {
97       EXPECT_LE(abs(output[ni] - ref_output[ni]), max_error[tx_size])
98           << "tx_size = " << tx_size << ", ni = " << ni
99           << ", output[ni] = " << output[ni]
100           << ", ref_output[ni] = " << ref_output[ni];
101     }
102   }
103 }
104 
get_max_bit(int x)105 static 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 
TEST(av1_inv_txfm1d,get_max_bit)114 TEST(av1_inv_txfm1d, get_max_bit) {
115   int max_bit = get_max_bit(8);
116   EXPECT_EQ(max_bit, 3);
117 }
118 
TEST(av1_inv_txfm1d,round_trip)119 TEST(av1_inv_txfm1d, round_trip) {
120   ACMRandom rnd(ACMRandom::DeterministicSeed());
121   for (int si = 0; si < NELEMENTS(fwd_txfm_func_ls); ++si) {
122     int txfm_size = txfm_size_ls[si];
123 
124     for (int ti = 0; ti < txfm_type_num; ++ti) {
125       TxfmFunc fwd_txfm_func = fwd_txfm_func_ls[si][ti];
126       TxfmFunc inv_txfm_func = inv_txfm_func_ls[si][ti];
127       int max_error = 2;
128 
129       if (!fwd_txfm_func) continue;
130 
131       const int count_test_block = 5000;
132       for (int i = 0; i < count_test_block; ++i) {
133         int32_t input[64];
134         int32_t output[64];
135         int32_t round_trip_output[64];
136 
137         ASSERT_LE(txfm_size, NELEMENTS(input));
138 
139         for (int ni = 0; ni < txfm_size; ++ni) {
140           input[ni] = rnd.Rand16() % input_base - rnd.Rand16() % input_base;
141         }
142 
143         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);
151         }
152       }
153     }
154   }
155 }
156 
157 }  // namespace
158