Angie Chiang | 87175ed | 2015-09-04 14:51:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebM project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include <math.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | #include "third_party/googletest/src/include/gtest/gtest.h" |
| 16 | |
| 17 | #include "./vp10_rtcd.h" |
| 18 | #include "./vpx_dsp_rtcd.h" |
| 19 | #include "test/acm_random.h" |
| 20 | #include "test/clear_system_state.h" |
| 21 | #include "test/register_state_check.h" |
| 22 | #include "test/util.h" |
| 23 | #include "vp10/common/blockd.h" |
| 24 | #include "vp10/common/scan.h" |
| 25 | #include "vpx/vpx_integer.h" |
| 26 | #include "vp10/common/vp10_inv_txfm.h" |
| 27 | |
| 28 | using libvpx_test::ACMRandom; |
| 29 | |
| 30 | namespace { |
| 31 | const double PI = 3.141592653589793238462643383279502884; |
| 32 | const double kInvSqrt2 = 0.707106781186547524400844362104; |
| 33 | |
| 34 | void reference_idct_1d(const double *in, double *out, int size) { |
| 35 | for (int n = 0; n < size; ++n) { |
| 36 | out[n] = 0; |
| 37 | for (int k = 0; k < size; ++k) { |
| 38 | if (k == 0) |
| 39 | out[n] += kInvSqrt2 * in[k] * cos(PI * (2 * n + 1) * k / (2 * size)); |
| 40 | else |
| 41 | out[n] += in[k] * cos(PI * (2 * n + 1) * k / (2 * size)); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | typedef void (*IdctFuncRef)(const double *in, double *out, int size); |
| 47 | typedef void (*IdctFunc)(const tran_low_t *in, tran_low_t *out); |
| 48 | |
| 49 | class TransTestBase { |
| 50 | public: |
| 51 | virtual ~TransTestBase() {} |
| 52 | |
| 53 | protected: |
| 54 | void RunInvAccuracyCheck() { |
| 55 | tran_low_t *input = new tran_low_t[txfm_size_]; |
| 56 | tran_low_t *output = new tran_low_t[txfm_size_]; |
| 57 | double *ref_input = new double[txfm_size_]; |
| 58 | double *ref_output = new double[txfm_size_]; |
| 59 | |
| 60 | ACMRandom rnd(ACMRandom::DeterministicSeed()); |
| 61 | const int count_test_block = 5000; |
| 62 | for (int ti = 0; ti < count_test_block; ++ti) { |
| 63 | for (int ni = 0; ni < txfm_size_; ++ni) { |
| 64 | input[ni] = rnd.Rand8() - rnd.Rand8(); |
| 65 | ref_input[ni] = static_cast<double>(input[ni]); |
| 66 | } |
| 67 | |
| 68 | fwd_txfm_(input, output); |
| 69 | fwd_txfm_ref_(ref_input, ref_output, txfm_size_); |
| 70 | |
| 71 | for (int ni = 0; ni < txfm_size_; ++ni) { |
| 72 | EXPECT_LE( |
| 73 | abs(output[ni] - static_cast<tran_low_t>(round(ref_output[ni]))), |
| 74 | max_error_); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | delete[] input; |
| 79 | delete[] output; |
| 80 | delete[] ref_input; |
| 81 | delete[] ref_output; |
| 82 | } |
| 83 | |
| 84 | double max_error_; |
| 85 | int txfm_size_; |
| 86 | IdctFunc fwd_txfm_; |
| 87 | IdctFuncRef fwd_txfm_ref_; |
| 88 | }; |
| 89 | |
| 90 | typedef std::tr1::tuple<IdctFunc, IdctFuncRef, int, int> IdctParam; |
| 91 | class Vp10InvTxfm |
| 92 | : public TransTestBase, |
| 93 | public ::testing::TestWithParam<IdctParam> { |
| 94 | public: |
| 95 | virtual void SetUp() { |
| 96 | fwd_txfm_ = GET_PARAM(0); |
| 97 | fwd_txfm_ref_ = GET_PARAM(1); |
| 98 | txfm_size_ = GET_PARAM(2); |
| 99 | max_error_ = GET_PARAM(3); |
| 100 | } |
| 101 | virtual void TearDown() {} |
| 102 | }; |
| 103 | |
| 104 | TEST_P(Vp10InvTxfm, RunInvAccuracyCheck) { |
| 105 | RunInvAccuracyCheck(); |
| 106 | } |
| 107 | |
| 108 | INSTANTIATE_TEST_CASE_P( |
| 109 | C, Vp10InvTxfm, |
| 110 | ::testing::Values( |
| 111 | IdctParam(&vp10_idct4_c, &reference_idct_1d, 4, 1), |
| 112 | IdctParam(&vp10_idct8_c, &reference_idct_1d, 8, 2), |
| 113 | IdctParam(&vp10_idct16_c, &reference_idct_1d, 16, 4), |
| 114 | IdctParam(&vp10_idct32_c, &reference_idct_1d, 32, 6)) |
| 115 | ); |
| 116 | |
| 117 | typedef void (*FwdTxfmFunc)(const int16_t *in, tran_low_t *out, int stride); |
| 118 | typedef void (*InvTxfmFunc)(const tran_low_t *in, uint8_t *out, int stride); |
| 119 | typedef std::tr1::tuple<FwdTxfmFunc, |
| 120 | InvTxfmFunc, |
| 121 | InvTxfmFunc, |
| 122 | TX_SIZE, int> PartialInvTxfmParam; |
| 123 | const int kMaxNumCoeffs = 1024; |
| 124 | class Vp10PartialIDctTest |
| 125 | : public ::testing::TestWithParam<PartialInvTxfmParam> { |
| 126 | public: |
| 127 | virtual ~Vp10PartialIDctTest() {} |
| 128 | virtual void SetUp() { |
| 129 | ftxfm_ = GET_PARAM(0); |
| 130 | full_itxfm_ = GET_PARAM(1); |
| 131 | partial_itxfm_ = GET_PARAM(2); |
| 132 | tx_size_ = GET_PARAM(3); |
| 133 | last_nonzero_ = GET_PARAM(4); |
| 134 | } |
| 135 | |
| 136 | virtual void TearDown() { libvpx_test::ClearSystemState(); } |
| 137 | |
| 138 | protected: |
| 139 | int last_nonzero_; |
| 140 | TX_SIZE tx_size_; |
| 141 | FwdTxfmFunc ftxfm_; |
| 142 | InvTxfmFunc full_itxfm_; |
| 143 | InvTxfmFunc partial_itxfm_; |
| 144 | }; |
| 145 | |
| 146 | TEST_P(Vp10PartialIDctTest, RunQuantCheck) { |
| 147 | ACMRandom rnd(ACMRandom::DeterministicSeed()); |
| 148 | int size; |
| 149 | switch (tx_size_) { |
| 150 | case TX_4X4: |
| 151 | size = 4; |
| 152 | break; |
| 153 | case TX_8X8: |
| 154 | size = 8; |
| 155 | break; |
| 156 | case TX_16X16: |
| 157 | size = 16; |
| 158 | break; |
| 159 | case TX_32X32: |
| 160 | size = 32; |
| 161 | break; |
| 162 | default: |
| 163 | FAIL() << "Wrong Size!"; |
| 164 | break; |
| 165 | } |
| 166 | DECLARE_ALIGNED(16, tran_low_t, test_coef_block1[kMaxNumCoeffs]); |
| 167 | DECLARE_ALIGNED(16, tran_low_t, test_coef_block2[kMaxNumCoeffs]); |
| 168 | DECLARE_ALIGNED(16, uint8_t, dst1[kMaxNumCoeffs]); |
| 169 | DECLARE_ALIGNED(16, uint8_t, dst2[kMaxNumCoeffs]); |
| 170 | |
| 171 | const int count_test_block = 1000; |
| 172 | const int block_size = size * size; |
| 173 | |
| 174 | DECLARE_ALIGNED(16, int16_t, input_extreme_block[kMaxNumCoeffs]); |
| 175 | DECLARE_ALIGNED(16, tran_low_t, output_ref_block[kMaxNumCoeffs]); |
| 176 | |
| 177 | int max_error = 0; |
| 178 | for (int i = 0; i < count_test_block; ++i) { |
| 179 | // clear out destination buffer |
| 180 | memset(dst1, 0, sizeof(*dst1) * block_size); |
| 181 | memset(dst2, 0, sizeof(*dst2) * block_size); |
| 182 | memset(test_coef_block1, 0, sizeof(*test_coef_block1) * block_size); |
| 183 | memset(test_coef_block2, 0, sizeof(*test_coef_block2) * block_size); |
| 184 | |
| 185 | ACMRandom rnd(ACMRandom::DeterministicSeed()); |
| 186 | |
| 187 | for (int i = 0; i < count_test_block; ++i) { |
| 188 | // Initialize a test block with input range [-255, 255]. |
| 189 | if (i == 0) { |
| 190 | for (int j = 0; j < block_size; ++j) |
| 191 | input_extreme_block[j] = 255; |
| 192 | } else if (i == 1) { |
| 193 | for (int j = 0; j < block_size; ++j) |
| 194 | input_extreme_block[j] = -255; |
| 195 | } else { |
| 196 | for (int j = 0; j < block_size; ++j) { |
| 197 | input_extreme_block[j] = rnd.Rand8() % 2 ? 255 : -255; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | ftxfm_(input_extreme_block, output_ref_block, size); |
| 202 | |
| 203 | // quantization with maximum allowed step sizes |
| 204 | test_coef_block1[0] = (output_ref_block[0] / 1336) * 1336; |
| 205 | for (int j = 1; j < last_nonzero_; ++j) |
Yaowu Xu | 7c514e2 | 2015-09-28 15:55:46 -0700 | [diff] [blame] | 206 | test_coef_block1[get_scan(tx_size_, DCT_DCT, 0)->scan[j]] |
Angie Chiang | 87175ed | 2015-09-04 14:51:54 -0700 | [diff] [blame] | 207 | = (output_ref_block[j] / 1828) * 1828; |
| 208 | } |
| 209 | |
| 210 | ASM_REGISTER_STATE_CHECK(full_itxfm_(test_coef_block1, dst1, size)); |
| 211 | ASM_REGISTER_STATE_CHECK(partial_itxfm_(test_coef_block1, dst2, size)); |
| 212 | |
| 213 | for (int j = 0; j < block_size; ++j) { |
| 214 | const int diff = dst1[j] - dst2[j]; |
| 215 | const int error = diff * diff; |
| 216 | if (max_error < error) |
| 217 | max_error = error; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | EXPECT_EQ(0, max_error) |
| 222 | << "Error: partial inverse transform produces different results"; |
| 223 | } |
| 224 | |
| 225 | TEST_P(Vp10PartialIDctTest, ResultsMatch) { |
| 226 | ACMRandom rnd(ACMRandom::DeterministicSeed()); |
| 227 | int size; |
| 228 | switch (tx_size_) { |
| 229 | case TX_4X4: |
| 230 | size = 4; |
| 231 | break; |
| 232 | case TX_8X8: |
| 233 | size = 8; |
| 234 | break; |
| 235 | case TX_16X16: |
| 236 | size = 16; |
| 237 | break; |
| 238 | case TX_32X32: |
| 239 | size = 32; |
| 240 | break; |
| 241 | default: |
| 242 | FAIL() << "Wrong Size!"; |
| 243 | break; |
| 244 | } |
| 245 | DECLARE_ALIGNED(16, tran_low_t, test_coef_block1[kMaxNumCoeffs]); |
| 246 | DECLARE_ALIGNED(16, tran_low_t, test_coef_block2[kMaxNumCoeffs]); |
| 247 | DECLARE_ALIGNED(16, uint8_t, dst1[kMaxNumCoeffs]); |
| 248 | DECLARE_ALIGNED(16, uint8_t, dst2[kMaxNumCoeffs]); |
| 249 | const int count_test_block = 1000; |
| 250 | const int max_coeff = 32766 / 4; |
| 251 | const int block_size = size * size; |
| 252 | int max_error = 0; |
| 253 | for (int i = 0; i < count_test_block; ++i) { |
| 254 | // clear out destination buffer |
| 255 | memset(dst1, 0, sizeof(*dst1) * block_size); |
| 256 | memset(dst2, 0, sizeof(*dst2) * block_size); |
| 257 | memset(test_coef_block1, 0, sizeof(*test_coef_block1) * block_size); |
| 258 | memset(test_coef_block2, 0, sizeof(*test_coef_block2) * block_size); |
| 259 | int max_energy_leftover = max_coeff * max_coeff; |
| 260 | for (int j = 0; j < last_nonzero_; ++j) { |
| 261 | int16_t coef = static_cast<int16_t>(sqrt(1.0 * max_energy_leftover) * |
| 262 | (rnd.Rand16() - 32768) / 65536); |
| 263 | max_energy_leftover -= coef * coef; |
| 264 | if (max_energy_leftover < 0) { |
| 265 | max_energy_leftover = 0; |
| 266 | coef = 0; |
| 267 | } |
Yaowu Xu | 7c514e2 | 2015-09-28 15:55:46 -0700 | [diff] [blame] | 268 | test_coef_block1[get_scan(tx_size_, DCT_DCT, 0)->scan[j]] = coef; |
Angie Chiang | 87175ed | 2015-09-04 14:51:54 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | memcpy(test_coef_block2, test_coef_block1, |
| 272 | sizeof(*test_coef_block2) * block_size); |
| 273 | |
| 274 | ASM_REGISTER_STATE_CHECK(full_itxfm_(test_coef_block1, dst1, size)); |
| 275 | ASM_REGISTER_STATE_CHECK(partial_itxfm_(test_coef_block2, dst2, size)); |
| 276 | |
| 277 | for (int j = 0; j < block_size; ++j) { |
| 278 | const int diff = dst1[j] - dst2[j]; |
| 279 | const int error = diff * diff; |
| 280 | if (max_error < error) |
| 281 | max_error = error; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | EXPECT_EQ(0, max_error) |
| 286 | << "Error: partial inverse transform produces different results"; |
| 287 | } |
| 288 | using std::tr1::make_tuple; |
| 289 | |
| 290 | INSTANTIATE_TEST_CASE_P( |
| 291 | C, Vp10PartialIDctTest, |
| 292 | ::testing::Values( |
| 293 | make_tuple(&vpx_fdct32x32_c, |
| 294 | &vp10_idct32x32_1024_add_c, |
| 295 | &vp10_idct32x32_34_add_c, |
| 296 | TX_32X32, 34), |
| 297 | make_tuple(&vpx_fdct32x32_c, |
| 298 | &vp10_idct32x32_1024_add_c, |
| 299 | &vp10_idct32x32_1_add_c, |
| 300 | TX_32X32, 1), |
| 301 | make_tuple(&vpx_fdct16x16_c, |
| 302 | &vp10_idct16x16_256_add_c, |
| 303 | &vp10_idct16x16_10_add_c, |
| 304 | TX_16X16, 10), |
| 305 | make_tuple(&vpx_fdct16x16_c, |
| 306 | &vp10_idct16x16_256_add_c, |
| 307 | &vp10_idct16x16_1_add_c, |
| 308 | TX_16X16, 1), |
| 309 | make_tuple(&vpx_fdct8x8_c, |
| 310 | &vp10_idct8x8_64_add_c, |
| 311 | &vp10_idct8x8_12_add_c, |
| 312 | TX_8X8, 12), |
| 313 | make_tuple(&vpx_fdct8x8_c, |
| 314 | &vp10_idct8x8_64_add_c, |
| 315 | &vp10_idct8x8_1_add_c, |
| 316 | TX_8X8, 1), |
| 317 | make_tuple(&vpx_fdct4x4_c, |
| 318 | &vp10_idct4x4_16_add_c, |
| 319 | &vp10_idct4x4_1_add_c, |
| 320 | TX_4X4, 1))); |
| 321 | } // namespace |