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 #ifndef AOM_TEST_TRANSFORM_TEST_BASE_H_
13 #define AOM_TEST_TRANSFORM_TEST_BASE_H_
14 
15 #include "config/aom_config.h"
16 
17 #include "aom_mem/aom_mem.h"
18 #include "aom/aom_codec.h"
19 #include "aom_dsp/txfm_common.h"
20 
21 namespace libaom_test {
22 
23 //  Note:
24 //   Same constant are defined in av1/common/av1_entropy.h and
25 //   av1/common/entropy.h.  Goal is to make this base class
26 //   to use for future codec transform testing.  But including
27 //   either of them would lead to compiling error when we do
28 //   unit test for another codec. Suggest to move the definition
29 //   to a aom header file.
30 const int kDctMaxValue = 16384;
31 
32 template <typename OutputType>
33 using FhtFunc = void (*)(const int16_t *in, OutputType *out, int stride,
34                          TxfmParam *txfm_param);
35 
36 template <typename OutputType>
37 using IhtFunc = void (*)(const tran_low_t *in, uint8_t *out, int stride,
38                          const TxfmParam *txfm_param);
39 
40 template <typename OutType>
41 class TransformTestBase {
42  public:
~TransformTestBase()43   virtual ~TransformTestBase() {}
44 
45  protected:
46   virtual void RunFwdTxfm(const int16_t *in, OutType *out, int stride) = 0;
47 
48   virtual void RunInvTxfm(const OutType *out, uint8_t *dst, int stride) = 0;
49 
RunAccuracyCheck(uint32_t ref_max_error,double ref_avg_error)50   void RunAccuracyCheck(uint32_t ref_max_error, double ref_avg_error) {
51     ACMRandom rnd(ACMRandom::DeterministicSeed());
52     uint32_t max_error = 0;
53     int64_t total_error = 0;
54     const int count_test_block = 10000;
55 
56     int16_t *test_input_block = reinterpret_cast<int16_t *>(
57         aom_memalign(16, sizeof(int16_t) * num_coeffs_));
58     OutType *test_temp_block = reinterpret_cast<OutType *>(
59         aom_memalign(16, sizeof(test_temp_block[0]) * num_coeffs_));
60     uint8_t *dst = reinterpret_cast<uint8_t *>(
61         aom_memalign(16, sizeof(uint8_t) * num_coeffs_));
62     uint8_t *src = reinterpret_cast<uint8_t *>(
63         aom_memalign(16, sizeof(uint8_t) * num_coeffs_));
64     uint16_t *dst16 = reinterpret_cast<uint16_t *>(
65         aom_memalign(16, sizeof(uint16_t) * num_coeffs_));
66     uint16_t *src16 = reinterpret_cast<uint16_t *>(
67         aom_memalign(16, sizeof(uint16_t) * num_coeffs_));
68 
69     for (int i = 0; i < count_test_block; ++i) {
70       // Initialize a test block with input range [-255, 255].
71       for (int j = 0; j < num_coeffs_; ++j) {
72         if (bit_depth_ == AOM_BITS_8) {
73           src[j] = rnd.Rand8();
74           dst[j] = rnd.Rand8();
75           test_input_block[j] = src[j] - dst[j];
76         } else {
77           src16[j] = rnd.Rand16() & mask_;
78           dst16[j] = rnd.Rand16() & mask_;
79           test_input_block[j] = src16[j] - dst16[j];
80         }
81       }
82 
83       API_REGISTER_STATE_CHECK(
84           RunFwdTxfm(test_input_block, test_temp_block, pitch_));
85       if (bit_depth_ == AOM_BITS_8) {
86         API_REGISTER_STATE_CHECK(RunInvTxfm(test_temp_block, dst, pitch_));
87       } else {
88         API_REGISTER_STATE_CHECK(
89             RunInvTxfm(test_temp_block, CONVERT_TO_BYTEPTR(dst16), pitch_));
90       }
91 
92       for (int j = 0; j < num_coeffs_; ++j) {
93         const int diff =
94             bit_depth_ == AOM_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
95         const uint32_t error = diff * diff;
96         if (max_error < error) max_error = error;
97         total_error += error;
98       }
99     }
100 
101     double avg_error = total_error * 1. / count_test_block / num_coeffs_;
102 
103     EXPECT_GE(ref_max_error, max_error)
104         << "Error: FHT/IHT has an individual round trip error > "
105         << ref_max_error;
106 
107     EXPECT_GE(ref_avg_error, avg_error)
108         << "Error: FHT/IHT has average round trip error > " << ref_avg_error
109         << " per block";
110 
111     aom_free(test_input_block);
112     aom_free(test_temp_block);
113     aom_free(dst);
114     aom_free(src);
115     aom_free(dst16);
116     aom_free(src16);
117   }
118 
RunCoeffCheck()119   void RunCoeffCheck() {
120     ACMRandom rnd(ACMRandom::DeterministicSeed());
121     const int count_test_block = 5000;
122 
123     // Use a stride value which is not the width of any transform, to catch
124     // cases where the transforms use the stride incorrectly.
125     int stride = 96;
126 
127     int16_t *input_block = reinterpret_cast<int16_t *>(
128         aom_memalign(16, sizeof(int16_t) * stride * height_));
129     OutType *output_ref_block = reinterpret_cast<OutType *>(
130         aom_memalign(16, sizeof(output_ref_block[0]) * num_coeffs_));
131     OutType *output_block = reinterpret_cast<OutType *>(
132         aom_memalign(16, sizeof(output_block[0]) * num_coeffs_));
133 
134     for (int i = 0; i < count_test_block; ++i) {
135       int j, k;
136       for (j = 0; j < height_; ++j) {
137         for (k = 0; k < pitch_; ++k) {
138           int in_idx = j * stride + k;
139           int out_idx = j * pitch_ + k;
140           input_block[in_idx] = (rnd.Rand16() & mask_) - (rnd.Rand16() & mask_);
141           if (bit_depth_ == AOM_BITS_8) {
142             output_block[out_idx] = output_ref_block[out_idx] = rnd.Rand8();
143           } else {
144             output_block[out_idx] = output_ref_block[out_idx] =
145                 rnd.Rand16() & mask_;
146           }
147         }
148       }
149 
150       fwd_txfm_ref(input_block, output_ref_block, stride, &txfm_param_);
151       API_REGISTER_STATE_CHECK(RunFwdTxfm(input_block, output_block, stride));
152 
153       // The minimum quant value is 4.
154       for (j = 0; j < height_; ++j) {
155         for (k = 0; k < pitch_; ++k) {
156           int out_idx = j * pitch_ + k;
157           ASSERT_EQ(output_block[out_idx], output_ref_block[out_idx])
158               << "Error: not bit-exact result at index: " << out_idx
159               << " at test block: " << i;
160         }
161       }
162     }
163     aom_free(input_block);
164     aom_free(output_ref_block);
165     aom_free(output_block);
166   }
167 
RunInvCoeffCheck()168   void RunInvCoeffCheck() {
169     ACMRandom rnd(ACMRandom::DeterministicSeed());
170     const int count_test_block = 5000;
171 
172     // Use a stride value which is not the width of any transform, to catch
173     // cases where the transforms use the stride incorrectly.
174     int stride = 96;
175 
176     int16_t *input_block = reinterpret_cast<int16_t *>(
177         aom_memalign(16, sizeof(int16_t) * num_coeffs_));
178     OutType *trans_block = reinterpret_cast<OutType *>(
179         aom_memalign(16, sizeof(trans_block[0]) * num_coeffs_));
180     uint8_t *output_block = reinterpret_cast<uint8_t *>(
181         aom_memalign(16, sizeof(uint8_t) * stride * height_));
182     uint8_t *output_ref_block = reinterpret_cast<uint8_t *>(
183         aom_memalign(16, sizeof(uint8_t) * stride * height_));
184 
185     for (int i = 0; i < count_test_block; ++i) {
186       // Initialize a test block with input range [-mask_, mask_].
187       int j, k;
188       for (j = 0; j < height_; ++j) {
189         for (k = 0; k < pitch_; ++k) {
190           int in_idx = j * pitch_ + k;
191           int out_idx = j * stride + k;
192           input_block[in_idx] = (rnd.Rand16() & mask_) - (rnd.Rand16() & mask_);
193           output_ref_block[out_idx] = rnd.Rand16() & mask_;
194           output_block[out_idx] = output_ref_block[out_idx];
195         }
196       }
197 
198       fwd_txfm_ref(input_block, trans_block, pitch_, &txfm_param_);
199 
200       inv_txfm_ref(trans_block, output_ref_block, stride, &txfm_param_);
201       API_REGISTER_STATE_CHECK(RunInvTxfm(trans_block, output_block, stride));
202 
203       for (j = 0; j < height_; ++j) {
204         for (k = 0; k < pitch_; ++k) {
205           int out_idx = j * stride + k;
206           ASSERT_EQ(output_block[out_idx], output_ref_block[out_idx])
207               << "Error: not bit-exact result at index: " << out_idx
208               << " j = " << j << " k = " << k << " at test block: " << i;
209         }
210       }
211     }
212     aom_free(input_block);
213     aom_free(trans_block);
214     aom_free(output_ref_block);
215     aom_free(output_block);
216   }
217 
RunMemCheck()218   void RunMemCheck() {
219     ACMRandom rnd(ACMRandom::DeterministicSeed());
220     const int count_test_block = 5000;
221 
222     int16_t *input_extreme_block = reinterpret_cast<int16_t *>(
223         aom_memalign(16, sizeof(int16_t) * num_coeffs_));
224     OutType *output_ref_block = reinterpret_cast<OutType *>(
225         aom_memalign(16, sizeof(output_ref_block[0]) * num_coeffs_));
226     OutType *output_block = reinterpret_cast<OutType *>(
227         aom_memalign(16, sizeof(output_block[0]) * num_coeffs_));
228 
229     for (int i = 0; i < count_test_block; ++i) {
230       // Initialize a test block with input range [-mask_, mask_].
231       for (int j = 0; j < num_coeffs_; ++j) {
232         input_extreme_block[j] = rnd.Rand8() % 2 ? mask_ : -mask_;
233       }
234       if (i == 0) {
235         for (int j = 0; j < num_coeffs_; ++j) input_extreme_block[j] = mask_;
236       } else if (i == 1) {
237         for (int j = 0; j < num_coeffs_; ++j) input_extreme_block[j] = -mask_;
238       }
239 
240       fwd_txfm_ref(input_extreme_block, output_ref_block, pitch_, &txfm_param_);
241       API_REGISTER_STATE_CHECK(
242           RunFwdTxfm(input_extreme_block, output_block, pitch_));
243 
244       int row_length = FindRowLength();
245       // The minimum quant value is 4.
246       for (int j = 0; j < num_coeffs_; ++j) {
247         ASSERT_EQ(output_block[j], output_ref_block[j])
248             << "Not bit-exact at test index: " << i << ", "
249             << "j = " << j << std::endl;
250         EXPECT_GE(row_length * kDctMaxValue << (bit_depth_ - 8),
251                   abs(output_block[j]))
252             << "Error: NxN FDCT has coefficient larger than N*DCT_MAX_VALUE";
253       }
254     }
255     aom_free(input_extreme_block);
256     aom_free(output_ref_block);
257     aom_free(output_block);
258   }
259 
RunInvAccuracyCheck(int limit)260   void RunInvAccuracyCheck(int limit) {
261     ACMRandom rnd(ACMRandom::DeterministicSeed());
262     const int count_test_block = 1000;
263 
264     int16_t *in = reinterpret_cast<int16_t *>(
265         aom_memalign(16, sizeof(int16_t) * num_coeffs_));
266     OutType *coeff = reinterpret_cast<OutType *>(
267         aom_memalign(16, sizeof(coeff[0]) * num_coeffs_));
268     uint8_t *dst = reinterpret_cast<uint8_t *>(
269         aom_memalign(16, sizeof(uint8_t) * num_coeffs_));
270     uint8_t *src = reinterpret_cast<uint8_t *>(
271         aom_memalign(16, sizeof(uint8_t) * num_coeffs_));
272 
273     uint16_t *dst16 = reinterpret_cast<uint16_t *>(
274         aom_memalign(16, sizeof(uint16_t) * num_coeffs_));
275     uint16_t *src16 = reinterpret_cast<uint16_t *>(
276         aom_memalign(16, sizeof(uint16_t) * num_coeffs_));
277 
278     for (int i = 0; i < count_test_block; ++i) {
279       // Initialize a test block with input range [-mask_, mask_].
280       for (int j = 0; j < num_coeffs_; ++j) {
281         if (bit_depth_ == AOM_BITS_8) {
282           src[j] = rnd.Rand8();
283           dst[j] = rnd.Rand8();
284           in[j] = src[j] - dst[j];
285         } else {
286           src16[j] = rnd.Rand16() & mask_;
287           dst16[j] = rnd.Rand16() & mask_;
288           in[j] = src16[j] - dst16[j];
289         }
290       }
291 
292       fwd_txfm_ref(in, coeff, pitch_, &txfm_param_);
293 
294       if (bit_depth_ == AOM_BITS_8) {
295         API_REGISTER_STATE_CHECK(RunInvTxfm(coeff, dst, pitch_));
296       } else {
297         API_REGISTER_STATE_CHECK(
298             RunInvTxfm(coeff, CONVERT_TO_BYTEPTR(dst16), pitch_));
299       }
300 
301       for (int j = 0; j < num_coeffs_; ++j) {
302         const int diff =
303             bit_depth_ == AOM_BITS_8 ? dst[j] - src[j] : dst16[j] - src16[j];
304         const uint32_t error = diff * diff;
305         ASSERT_GE(static_cast<uint32_t>(limit), error)
306             << "Error: 4x4 IDCT has error " << error << " at index " << j;
307       }
308     }
309     aom_free(in);
310     aom_free(coeff);
311     aom_free(dst);
312     aom_free(src);
313     aom_free(src16);
314     aom_free(dst16);
315   }
316 
317   int pitch_;
318   int height_;
319   FhtFunc<OutType> fwd_txfm_ref;
320   IhtFunc<OutType> inv_txfm_ref;
321   aom_bit_depth_t bit_depth_;
322   int mask_;
323   int num_coeffs_;
324   TxfmParam txfm_param_;
325 
326  private:
327   //  Assume transform size is 4x4, 8x8, 16x16,...
FindRowLength()328   int FindRowLength() const {
329     int row = 4;
330     if (16 == num_coeffs_) {
331       row = 4;
332     } else if (64 == num_coeffs_) {
333       row = 8;
334     } else if (256 == num_coeffs_) {
335       row = 16;
336     } else if (1024 == num_coeffs_) {
337       row = 32;
338     }
339     return row;
340   }
341 };
342 
343 }  // namespace libaom_test
344 
345 #endif  // AOM_TEST_TRANSFORM_TEST_BASE_H_
346