av1_inv_txfm2d_test.cc 13.1 KB
Newer Older
Angie Chiang's avatar
Angie Chiang committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Angie Chiang's avatar
Angie Chiang committed
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Angie Chiang's avatar
Angie Chiang committed
10 11 12 13 14
 */

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
15
#include <vector>
Angie Chiang's avatar
Angie Chiang committed
16

Yaowu Xu's avatar
Yaowu Xu committed
17
#include "./av1_rtcd.h"
18 19 20

#include "aom_ports/aom_timer.h"
#include "av1/common/av1_inv_txfm1d_cfg.h"
Angie Chiang's avatar
Angie Chiang committed
21
#include "test/acm_random.h"
Yaowu Xu's avatar
Yaowu Xu committed
22
#include "test/av1_txfm_test.h"
23
#include "test/util.h"
Angie Chiang's avatar
Angie Chiang committed
24

25
using libaom_test::ACMRandom;
26 27 28
using libaom_test::FwdTxfm2dFunc;
using libaom_test::InvTxfm2dFunc;
using libaom_test::LbdInvTxfm2dFunc;
Johann's avatar
Johann committed
29 30 31
using libaom_test::bd;
using libaom_test::compute_avg_abs_error;
using libaom_test::input_base;
Angie Chiang's avatar
Angie Chiang committed
32

33 34 35 36
using ::testing::Combine;
using ::testing::Range;
using ::testing::Values;

37 38
using std::vector;

Angie Chiang's avatar
Angie Chiang committed
39 40
namespace {

Yaowu Xu's avatar
Yaowu Xu committed
41
// AV1InvTxfm2dParam argument list:
42
// tx_type_, tx_size_, max_error_, max_avg_error_
Yaowu Xu's avatar
Yaowu Xu committed
43
typedef std::tr1::tuple<TX_TYPE, TX_SIZE, int, double> AV1InvTxfm2dParam;
44

Yaowu Xu's avatar
Yaowu Xu committed
45
class AV1InvTxfm2d : public ::testing::TestWithParam<AV1InvTxfm2dParam> {
46 47 48 49 50 51 52 53 54
 public:
  virtual void SetUp() {
    tx_type_ = GET_PARAM(0);
    tx_size_ = GET_PARAM(1);
    max_error_ = GET_PARAM(2);
    max_avg_error_ = GET_PARAM(3);
  }

  void RunRoundtripCheck() {
Angie Chiang's avatar
Angie Chiang committed
55 56 57
    int tx_w = tx_size_wide[tx_size_];
    int tx_h = tx_size_high[tx_size_];
    int txfm2d_size = tx_w * tx_h;
58 59
    const FwdTxfm2dFunc fwd_txfm_func = libaom_test::fwd_txfm_func_ls[tx_size_];
    const InvTxfm2dFunc inv_txfm_func = libaom_test::inv_txfm_func_ls[tx_size_];
60 61
    double avg_abs_error = 0;
    ACMRandom rnd(ACMRandom::DeterministicSeed());
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
62

63
    const int count = 500;
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
64

65
    for (int ci = 0; ci < count; ci++) {
Yaowu Xu's avatar
Yaowu Xu committed
66
      DECLARE_ALIGNED(16, int16_t, input[64 * 64]) = { 0 };
67
      ASSERT_LE(txfm2d_size, NELEMENTS(input));
68

Angie Chiang's avatar
Angie Chiang committed
69
      for (int ni = 0; ni < txfm2d_size; ++ni) {
70 71
        if (ci == 0) {
          int extreme_input = input_base - 1;
72
          input[ni] = extreme_input;  // extreme case
73
        } else {
74 75 76 77
          input[ni] = rnd.Rand16() % input_base;
        }
      }

Yaowu Xu's avatar
Yaowu Xu committed
78
      DECLARE_ALIGNED(16, uint16_t, expected[64 * 64]) = { 0 };
79 80 81 82 83 84 85 86 87 88 89 90 91
      ASSERT_LE(txfm2d_size, NELEMENTS(expected));
      if (TxfmUsesApproximation()) {
        // Compare reference forward HT + inverse HT vs forward HT + inverse HT.
        double ref_input[64 * 64];
        ASSERT_LE(txfm2d_size, NELEMENTS(ref_input));
        for (int ni = 0; ni < txfm2d_size; ++ni) {
          ref_input[ni] = input[ni];
        }
        double ref_coeffs[64 * 64] = { 0 };
        ASSERT_LE(txfm2d_size, NELEMENTS(ref_coeffs));
        ASSERT_EQ(tx_type_, DCT_DCT);
        libaom_test::reference_hybrid_2d(ref_input, ref_coeffs, tx_type_,
                                         tx_size_);
Yaowu Xu's avatar
Yaowu Xu committed
92
        DECLARE_ALIGNED(16, int32_t, ref_coeffs_int[64 * 64]) = { 0 };
93 94 95 96 97 98 99 100 101
        ASSERT_LE(txfm2d_size, NELEMENTS(ref_coeffs_int));
        for (int ni = 0; ni < txfm2d_size; ++ni) {
          ref_coeffs_int[ni] = (int32_t)round(ref_coeffs[ni]);
        }
        inv_txfm_func(ref_coeffs_int, expected, tx_w, tx_type_, bd);
      } else {
        // Compare original input vs forward HT + inverse HT.
        for (int ni = 0; ni < txfm2d_size; ++ni) {
          expected[ni] = input[ni];
Angie Chiang's avatar
Angie Chiang committed
102
        }
103 104
      }

Yaowu Xu's avatar
Yaowu Xu committed
105
      DECLARE_ALIGNED(16, int32_t, coeffs[64 * 64]) = { 0 };
106
      ASSERT_LE(txfm2d_size, NELEMENTS(coeffs));
107
      fwd_txfm_func(input, coeffs, tx_w, tx_type_, bd);
108

Yaowu Xu's avatar
Yaowu Xu committed
109
      DECLARE_ALIGNED(16, uint16_t, actual[64 * 64]) = { 0 };
110
      ASSERT_LE(txfm2d_size, NELEMENTS(actual));
Angie Chiang's avatar
Angie Chiang committed
111
      inv_txfm_func(coeffs, actual, tx_w, tx_type_, bd);
Angie Chiang's avatar
Angie Chiang committed
112

113
      double actual_max_error = 0;
Angie Chiang's avatar
Angie Chiang committed
114
      for (int ni = 0; ni < txfm2d_size; ++ni) {
115 116 117 118 119 120 121
        const double this_error = abs(expected[ni] - actual[ni]);
        actual_max_error = AOMMAX(actual_max_error, this_error);
      }
      EXPECT_GE(max_error_, actual_max_error)
          << " tx_w: " << tx_w << " tx_h " << tx_h << " tx_type: " << tx_type_;
      if (actual_max_error > max_error_) {  // exit early.
        break;
Angie Chiang's avatar
Angie Chiang committed
122
      }
123
      avg_abs_error += compute_avg_abs_error<uint16_t, uint16_t>(
Angie Chiang's avatar
Angie Chiang committed
124
          expected, actual, txfm2d_size);
Angie Chiang's avatar
Angie Chiang committed
125 126
    }

127
    avg_abs_error /= count;
Angie Chiang's avatar
Angie Chiang committed
128 129
    EXPECT_GE(max_avg_error_, avg_abs_error)
        << " tx_w: " << tx_w << " tx_h " << tx_h << " tx_type: " << tx_type_;
Angie Chiang's avatar
Angie Chiang committed
130
  }
131 132

 private:
133 134 135 136 137 138 139 140 141
  bool TxfmUsesApproximation() {
#if CONFIG_TX64X64
    if (tx_size_wide[tx_size_] == 64 || tx_size_high[tx_size_] == 64) {
      return true;
    }
#endif  // CONFIG_TX64X64
    return false;
  }

142 143 144 145 146 147
  int max_error_;
  double max_avg_error_;
  TX_TYPE tx_type_;
  TX_SIZE tx_size_;
};

148 149
vector<AV1InvTxfm2dParam> GetInvTxfm2dParamList() {
  vector<AV1InvTxfm2dParam> param_list;
150
  for (int t = 0; t < TX_TYPES; ++t) {
151 152
    const TX_TYPE tx_type = static_cast<TX_TYPE>(t);
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_4X4, 2, 0.002));
Angie Chiang's avatar
Angie Chiang committed
153
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_8X8, 2, 0.05));
154
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_16X16, 2, 0.07));
155
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_32X32, 4, 0.4));
156 157
#if CONFIG_TX64X64
    if (tx_type == DCT_DCT) {  // Other types not supported by these tx sizes.
Angie Chiang's avatar
Angie Chiang committed
158
      param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_64X64, 3, 0.3));
159 160
    }
#endif  // CONFIG_TX64X64
161

162 163 164
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_4X8, 2, 0.02));
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_8X4, 2, 0.02));
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_8X16, 2, 0.04));
165
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_16X8, 2, 0.07));
166
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_16X32, 3, 0.4));
167
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_32X16, 3, 0.5));
168

169 170
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_4X16, 2, 0.2));
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_16X4, 2, 0.2));
171
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_8X32, 2, 0.2));
172
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_32X8, 2, 0.2));
173

174
#if CONFIG_TX64X64
175
    if (tx_type == DCT_DCT) {  // Other types not supported by these tx sizes.
176
      param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_32X64, 5, 0.38));
177
      param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_64X32, 5, 0.39));
178 179
      param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_16X64, 3, 0.38));
      param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_64X16, 3, 0.38));
180
    }
181
#endif  // CONFIG_TX64X64
182 183 184
  }
  return param_list;
}
185

Yaowu Xu's avatar
Yaowu Xu committed
186
INSTANTIATE_TEST_CASE_P(C, AV1InvTxfm2d,
187 188 189
                        ::testing::ValuesIn(GetInvTxfm2dParamList()));

TEST_P(AV1InvTxfm2d, RunRoundtripCheck) { RunRoundtripCheck(); }
190

Angie Chiang's avatar
Angie Chiang committed
191 192 193 194 195
TEST(AV1InvTxfm2d, CfgTest) {
  for (int bd_idx = 0; bd_idx < BD_NUM; ++bd_idx) {
    int bd = libaom_test::bd_arr[bd_idx];
    int8_t low_range = libaom_test::low_range_arr[bd_idx];
    int8_t high_range = libaom_test::high_range_arr[bd_idx];
196
    for (int tx_size = 0; tx_size < TX_SIZES_ALL; ++tx_size) {
Angie Chiang's avatar
Angie Chiang committed
197
      for (int tx_type = 0; tx_type < TX_TYPES; ++tx_type) {
Urvang Joshi's avatar
Urvang Joshi committed
198
#if CONFIG_TX64X64
199 200 201 202
        if ((tx_size_wide[tx_size] == 64 || tx_size_high[tx_size] == 64) &&
            tx_type != DCT_DCT) {
          continue;
        }
Urvang Joshi's avatar
Urvang Joshi committed
203
#endif  // CONFIG_TX64X64
204 205 206
        TXFM_2D_FLIP_CFG cfg;
        av1_get_inv_txfm_cfg(static_cast<TX_TYPE>(tx_type),
                             static_cast<TX_SIZE>(tx_size), &cfg);
Angie Chiang's avatar
Angie Chiang committed
207 208 209
        int8_t stage_range_col[MAX_TXFM_STAGE_NUM];
        int8_t stage_range_row[MAX_TXFM_STAGE_NUM];
        av1_gen_inv_stage_range(stage_range_col, stage_range_row, &cfg,
210
                                (TX_SIZE)tx_size, bd);
Angie Chiang's avatar
Angie Chiang committed
211
        libaom_test::txfm_stage_range_check(stage_range_col, cfg.stage_num_col,
212
                                            cfg.cos_bit_col, low_range,
Angie Chiang's avatar
Angie Chiang committed
213
                                            high_range);
Angie Chiang's avatar
Angie Chiang committed
214
        libaom_test::txfm_stage_range_check(stage_range_row, cfg.stage_num_row,
215
                                            cfg.cos_bit_row, low_range,
Angie Chiang's avatar
Angie Chiang committed
216 217 218 219 220
                                            high_range);
      }
    }
  }
}
221

222
typedef std::tr1::tuple<const LbdInvTxfm2dFunc *, int> AV1LbdInvTxfm2dParam;
223 224
class AV1LbdInvTxfm2d : public ::testing::TestWithParam<AV1LbdInvTxfm2dParam> {
 public:
225 226 227 228 229 230 231
  virtual void SetUp() {
    const LbdInvTxfm2dFunc *target_list = GET_PARAM(0);
    tx_size_ = GET_PARAM(1);
    fwd_func_ = libaom_test::fwd_txfm_func_ls[tx_size_];
    ref_func_ = libaom_test::inv_txfm_func_ls[tx_size_];
    target_func_ = target_list[tx_size_];
  }
Peng Bin's avatar
Peng Bin committed
232 233 234 235 236 237

  bool ValidTypeSize(TX_TYPE tx_type) const {
    const int rows = tx_size_wide[tx_size_];
    const int cols = tx_size_high[tx_size_];
    const TX_TYPE_1D vtype = vtx_tab[tx_type];
    const TX_TYPE_1D htype = htx_tab[tx_type];
Peng Bin's avatar
Peng Bin committed
238
    if (rows >= 32 && (htype == ADST_1D || htype == FLIPADST_1D)) {
Peng Bin's avatar
Peng Bin committed
239
      return false;
Peng Bin's avatar
Peng Bin committed
240
    } else if (cols >= 32 && (vtype == ADST_1D || vtype == FLIPADST_1D)) {
Peng Bin's avatar
Peng Bin committed
241
      return false;
242
    }
Peng Bin's avatar
Peng Bin committed
243
    return true;
244
  }
Peng Bin's avatar
Peng Bin committed
245

246 247 248 249 250 251 252
  void RunAV1InvTxfm2dTest(TX_TYPE tx_type, int run_times);

 private:
  FwdTxfm2dFunc fwd_func_;
  InvTxfm2dFunc ref_func_;
  LbdInvTxfm2dFunc target_func_;
  int tx_size_;
253 254
};

255 256 257 258
void AV1LbdInvTxfm2d::RunAV1InvTxfm2dTest(TX_TYPE tx_type, int run_times) {
  if (fwd_func_ == NULL || ref_func_ == NULL || target_func_ == NULL) {
    return;
  }
259
  const int bd = 8;
260 261 262 263 264 265 266 267 268
  const int BLK_WIDTH = 64;
  const int BLK_SIZE = BLK_WIDTH * BLK_WIDTH;
  DECLARE_ALIGNED(16, int16_t, input[BLK_SIZE]) = { 0 };
  DECLARE_ALIGNED(32, int32_t, inv_input[BLK_SIZE]) = { 0 };
  DECLARE_ALIGNED(16, uint8_t, output[BLK_SIZE]) = { 0 };
  DECLARE_ALIGNED(16, uint16_t, ref_output[BLK_SIZE]) = { 0 };
  int stride = BLK_WIDTH;
  int rows = tx_size_high[tx_size_];
  int cols = tx_size_wide[tx_size_];
269

270 271 272 273 274 275 276 277 278 279 280 281
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  int randTimes = run_times == 1 ? 500 : 2;
  for (int cnt = 0; cnt < randTimes; ++cnt) {
    const int16_t max_in = (1 << (bd + 1)) - 1;
    for (int r = 0; r < rows; ++r) {
      for (int c = 0; c < cols; ++c) {
        input[r * cols + c] = (cnt == 0) ? max_in : rnd.Rand8Extremes();
        output[r * stride + c] = (cnt == 0) ? 128 : rnd.Rand8();
        ref_output[r * stride + c] = output[r * stride + c];
      }
    }
    fwd_func_(input, inv_input, stride, tx_type, bd);
282

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    aom_usec_timer timer;
    aom_usec_timer_start(&timer);
    for (int i = 0; i < run_times; ++i) {
      ref_func_(inv_input, ref_output, stride, tx_type, bd);
    }
    aom_usec_timer_mark(&timer);
    double time1 = static_cast<double>(aom_usec_timer_elapsed(&timer));
    aom_usec_timer_start(&timer);
    for (int i = 0; i < run_times; ++i) {
      target_func_(inv_input, output, stride, tx_type, bd);
    }
    aom_usec_timer_mark(&timer);
    double time2 = static_cast<double>(aom_usec_timer_elapsed(&timer));
    if (run_times > 10) {
      printf("txfm[%d] %3dx%-3d:%7.2f/%7.2fns", tx_type, cols, rows, time1,
             time2);
      printf("(%3.2f)\n", time1 / time2);
    }
    for (int r = 0; r < rows; ++r) {
      for (int c = 0; c < cols; ++c) {
303 304
        uint8_t ref_value = static_cast<uint8_t>(ref_output[r * stride + c]);
        ASSERT_EQ(ref_value, output[r * stride + c])
305 306
            << "[" << r << "," << c << "] " << cnt << " tx_size: " << tx_size_
            << " tx_type: " << tx_type;
307 308 309 310 311
      }
    }
  }
}

312 313
TEST_P(AV1LbdInvTxfm2d, match) {
  for (int i = 0; i < (int)TX_TYPES; ++i) {
314 315 316
    if (ValidTypeSize((TX_TYPE)(i))) {
      RunAV1InvTxfm2dTest((TX_TYPE)i, 1);
    }
317 318
  }
}
319

320
TEST_P(AV1LbdInvTxfm2d, DISABLED_Speed) {
321
  for (int i = 0; i < (int)TX_TYPES; ++i) {
322 323 324
    if (ValidTypeSize((TX_TYPE)(i))) {
      RunAV1InvTxfm2dTest((TX_TYPE)i, 1000000);
    }
325
  }
326 327 328 329 330
}

#if HAVE_SSE2 && defined(__SSE2__)
#include "av1/common/x86/av1_txfm_sse2.h"

331
const LbdInvTxfm2dFunc kLbdInvFuncSSE2List[TX_SIZES_ALL] = {
332
  av1_lowbd_inv_txfm2d_add_4x4_sse2,    // TX_4X4
333 334
  av1_lowbd_inv_txfm2d_add_8x8_sse2,    // TX_8X8
  av1_lowbd_inv_txfm2d_add_16x16_sse2,  // TX_16X16
335
  av1_lowbd_inv_txfm2d_add_32x32_sse2,  // TX_32X32
336
#if CONFIG_TX64X64
Peng Bin's avatar
Peng Bin committed
337
  av1_lowbd_inv_txfm2d_add_64x64_sse2,  // 64x64
338
#endif                                  // CONFIG_TX64X64
339 340
  av1_lowbd_inv_txfm2d_add_4x8_sse2,    // TX_4X8
  av1_lowbd_inv_txfm2d_add_8x4_sse2,    // TX_8X4
341 342 343 344
  av1_lowbd_inv_txfm2d_add_8x16_sse2,   // TX_8X16
  av1_lowbd_inv_txfm2d_add_16x8_sse2,   // TX_16X8
  av1_lowbd_inv_txfm2d_add_16x32_sse2,  // TX_16X32
  av1_lowbd_inv_txfm2d_add_32x16_sse2,  // TX_32X16
345
#if CONFIG_TX64X64
Peng Bin's avatar
Peng Bin committed
346 347 348
  av1_lowbd_inv_txfm2d_add_32x64_sse2,  // TX_32X64
  av1_lowbd_inv_txfm2d_add_64x32_sse2,  // TX_64X32
#endif                                  // CONFIG_TX64X64
349 350
  av1_lowbd_inv_txfm2d_add_4x16_sse2,   // TX_4X16
  av1_lowbd_inv_txfm2d_add_16x4_sse2,   // TX_16X4
Peng Bin's avatar
Peng Bin committed
351 352
  av1_lowbd_inv_txfm2d_add_8x32_sse2,   // 8x32
  av1_lowbd_inv_txfm2d_add_32x8_sse2,   // 32x8
353
#if CONFIG_TX64X64
Peng Bin's avatar
Peng Bin committed
354 355 356
  av1_lowbd_inv_txfm2d_add_16x64_sse2,  // 16x64
  av1_lowbd_inv_txfm2d_add_64x16_sse2,  // 64x16
#endif                                  // CONFIG_TX64X64
357 358
};
INSTANTIATE_TEST_CASE_P(SSE2, AV1LbdInvTxfm2d,
359 360
                        Combine(Values(kLbdInvFuncSSE2List),
                                Range(0, (int)TX_SIZES_ALL, 1)));
361 362 363

#endif  // HAVE_SSE2

364
}  // namespace