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