xref: /aosp_15_r20/external/libaom/test/av1_fwd_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 <memory>
13 #include <new>
14 
15 #include "av1/encoder/av1_fwd_txfm1d.h"
16 #include "test/av1_txfm_test.h"
17 
18 using libaom_test::ACMRandom;
19 using libaom_test::input_base;
20 using libaom_test::reference_hybrid_1d;
21 using libaom_test::TYPE_ADST;
22 using libaom_test::TYPE_DCT;
23 using libaom_test::TYPE_IDTX;
24 using libaom_test::TYPE_TXFM;
25 
26 namespace {
27 const int txfm_type_num = 3;
28 const TYPE_TXFM txfm_type_ls[txfm_type_num] = { TYPE_DCT, TYPE_ADST,
29                                                 TYPE_IDTX };
30 
31 const int txfm_size_num = 5;
32 
33 const int txfm_size_ls[] = { 4, 8, 16, 32, 64 };
34 
35 const TxfmFunc fwd_txfm_func_ls[][txfm_type_num] = {
36   { av1_fdct4, av1_fadst4, av1_fidentity4_c },
37   { av1_fdct8, av1_fadst8, av1_fidentity8_c },
38   { av1_fdct16, av1_fadst16, av1_fidentity16_c },
39   { av1_fdct32, nullptr, av1_fidentity32_c },
40   { av1_fdct64, nullptr, nullptr },
41 };
42 
43 // the maximum stage number of fwd/inv 1d dct/adst txfm is 12
44 const int8_t cos_bit = 13;
45 const int8_t range_bit[12] = { 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 };
46 
TEST(av1_fwd_txfm1d,round_shift)47 TEST(av1_fwd_txfm1d, round_shift) {
48   EXPECT_EQ(round_shift(7, 1), 4);
49   EXPECT_EQ(round_shift(-7, 1), -3);
50 
51   EXPECT_EQ(round_shift(7, 2), 2);
52   EXPECT_EQ(round_shift(-7, 2), -2);
53 
54   EXPECT_EQ(round_shift(8, 2), 2);
55   EXPECT_EQ(round_shift(-8, 2), -2);
56 }
57 
TEST(av1_fwd_txfm1d,av1_cospi_arr_data)58 TEST(av1_fwd_txfm1d, av1_cospi_arr_data) {
59   for (int i = 0; i < 4; i++) {
60     for (int j = 0; j < 64; j++) {
61       EXPECT_EQ(av1_cospi_arr_data[i][j],
62                 (int32_t)round(cos(PI * j / 128) * (1 << (cos_bit_min + i))));
63     }
64   }
65 }
66 
TEST(av1_fwd_txfm1d,accuracy)67 TEST(av1_fwd_txfm1d, accuracy) {
68   ACMRandom rnd(ACMRandom::DeterministicSeed());
69   for (int si = 0; si < txfm_size_num; ++si) {
70     int txfm_size = txfm_size_ls[si];
71     std::unique_ptr<int32_t[]> input(new (std::nothrow) int32_t[txfm_size]);
72     std::unique_ptr<int32_t[]> output(new (std::nothrow) int32_t[txfm_size]);
73     std::unique_ptr<double[]> ref_input(new (std::nothrow) double[txfm_size]);
74     std::unique_ptr<double[]> ref_output(new (std::nothrow) double[txfm_size]);
75     ASSERT_NE(input, nullptr);
76     ASSERT_NE(output, nullptr);
77     ASSERT_NE(ref_input, nullptr);
78     ASSERT_NE(ref_output, nullptr);
79 
80     for (int ti = 0; ti < txfm_type_num; ++ti) {
81       TYPE_TXFM txfm_type = txfm_type_ls[ti];
82       TxfmFunc fwd_txfm_func = fwd_txfm_func_ls[si][ti];
83       int max_error = 7;
84 
85       const int count_test_block = 5000;
86       if (fwd_txfm_func != nullptr) {
87         for (int i = 0; i < count_test_block; ++i) {
88           for (int ni = 0; ni < txfm_size; ++ni) {
89             input[ni] = rnd.Rand16() % input_base - rnd.Rand16() % input_base;
90             ref_input[ni] = static_cast<double>(input[ni]);
91           }
92 
93           fwd_txfm_func(input.get(), output.get(), cos_bit, range_bit);
94           reference_hybrid_1d(ref_input.get(), ref_output.get(), txfm_size,
95                               txfm_type);
96 
97           for (int ni = 0; ni < txfm_size; ++ni) {
98             ASSERT_LE(
99                 abs(output[ni] - static_cast<int32_t>(round(ref_output[ni]))),
100                 max_error)
101                 << "tx size = " << txfm_size << ", tx type = " << txfm_type;
102           }
103         }
104       }
105     }
106   }
107 }
108 }  // namespace
109