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