av1_inv_txfm2d_test.cc 12.7 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

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"
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 {

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

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
  bool TxfmUsesApproximation() {
    if (tx_size_wide[tx_size_] == 64 || tx_size_high[tx_size_] == 64) {
      return true;
    }
    return false;
  }

140 141 142 143 144 145
  int max_error_;
  double max_avg_error_;
  TX_TYPE tx_type_;
  TX_SIZE tx_size_;
};

146 147
vector<AV1InvTxfm2dParam> GetInvTxfm2dParamList() {
  vector<AV1InvTxfm2dParam> param_list;
148
  for (int t = 0; t < TX_TYPES; ++t) {
149 150
    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
151
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_8X8, 2, 0.05));
152
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_16X16, 2, 0.07));
153
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_32X32, 4, 0.4));
154
    if (tx_type == DCT_DCT) {  // Other types not supported by these tx sizes.
Angie Chiang's avatar
Angie Chiang committed
155
      param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_64X64, 3, 0.3));
156
    }
157

158 159 160
    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));
161
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_16X8, 2, 0.07));
162
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_16X32, 3, 0.4));
163
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_32X16, 3, 0.5));
164

165 166
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_4X16, 2, 0.2));
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_16X4, 2, 0.2));
167
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_8X32, 2, 0.2));
168
    param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_32X8, 2, 0.2));
169

170
    if (tx_type == DCT_DCT) {  // Other types not supported by these tx sizes.
171
      param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_32X64, 5, 0.38));
172
      param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_64X32, 5, 0.39));
173 174
      param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_16X64, 3, 0.38));
      param_list.push_back(AV1InvTxfm2dParam(tx_type, TX_64X16, 3, 0.38));
175 176 177 178
    }
  }
  return param_list;
}
179

180
INSTANTIATE_TEST_CASE_P(C, AV1InvTxfm2d,
181 182 183
                        ::testing::ValuesIn(GetInvTxfm2dParamList()));

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

Angie Chiang's avatar
Angie Chiang committed
185 186 187 188 189
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];
190
    for (int tx_size = 0; tx_size < TX_SIZES_ALL; ++tx_size) {
Angie Chiang's avatar
Angie Chiang committed
191
      for (int tx_type = 0; tx_type < TX_TYPES; ++tx_type) {
192 193 194 195
        if ((tx_size_wide[tx_size] == 64 || tx_size_high[tx_size] == 64) &&
            tx_type != DCT_DCT) {
          continue;
        }
196 197 198
        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
199 200 201
        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,
202
                                (TX_SIZE)tx_size, bd);
Angie Chiang's avatar
Angie Chiang committed
203
        libaom_test::txfm_stage_range_check(stage_range_col, cfg.stage_num_col,
204
                                            cfg.cos_bit_col, low_range,
Angie Chiang's avatar
Angie Chiang committed
205
                                            high_range);
Angie Chiang's avatar
Angie Chiang committed
206
        libaom_test::txfm_stage_range_check(stage_range_row, cfg.stage_num_row,
207
                                            cfg.cos_bit_row, low_range,
Angie Chiang's avatar
Angie Chiang committed
208 209 210 211 212
                                            high_range);
      }
    }
  }
}
213

214
typedef std::tr1::tuple<const LbdInvTxfm2dFunc *, int> AV1LbdInvTxfm2dParam;
215 216
class AV1LbdInvTxfm2d : public ::testing::TestWithParam<AV1LbdInvTxfm2dParam> {
 public:
217 218 219 220 221 222 223
  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_];
  }
224 225 226 227 228 229

  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
230
    if (rows >= 32 && (htype == ADST_1D || htype == FLIPADST_1D)) {
231
      return false;
Peng Bin's avatar
Peng Bin committed
232
    } else if (cols >= 32 && (vtype == ADST_1D || vtype == FLIPADST_1D)) {
233
      return false;
234
    }
235
    return true;
236
  }
237

238 239 240 241 242 243 244
  void RunAV1InvTxfm2dTest(TX_TYPE tx_type, int run_times);

 private:
  FwdTxfm2dFunc fwd_func_;
  InvTxfm2dFunc ref_func_;
  LbdInvTxfm2dFunc target_func_;
  int tx_size_;
245 246
};

247 248 249 250
void AV1LbdInvTxfm2d::RunAV1InvTxfm2dTest(TX_TYPE tx_type, int run_times) {
  if (fwd_func_ == NULL || ref_func_ == NULL || target_func_ == NULL) {
    return;
  }
251
  const int bd = 8;
252 253 254 255 256 257 258 259 260
  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_];
261

262 263 264 265 266 267 268 269 270 271 272 273
  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);
274

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    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) {
295 296
        uint8_t ref_value = static_cast<uint8_t>(ref_output[r * stride + c]);
        ASSERT_EQ(ref_value, output[r * stride + c])
297 298
            << "[" << r << "," << c << "] " << cnt << " tx_size: " << tx_size_
            << " tx_type: " << tx_type;
299 300 301 302 303
      }
    }
  }
}

304 305
TEST_P(AV1LbdInvTxfm2d, match) {
  for (int i = 0; i < (int)TX_TYPES; ++i) {
306 307 308
    if (ValidTypeSize((TX_TYPE)(i))) {
      RunAV1InvTxfm2dTest((TX_TYPE)i, 1);
    }
309 310
  }
}
311

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

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

323
const LbdInvTxfm2dFunc kLbdInvFuncSSE2List[TX_SIZES_ALL] = {
324
  av1_lowbd_inv_txfm2d_add_4x4_sse2,    // TX_4X4
325 326
  av1_lowbd_inv_txfm2d_add_8x8_sse2,    // TX_8X8
  av1_lowbd_inv_txfm2d_add_16x16_sse2,  // TX_16X16
327
  av1_lowbd_inv_txfm2d_add_32x32_sse2,  // TX_32X32
Peng Bin's avatar
Peng Bin committed
328
  av1_lowbd_inv_txfm2d_add_64x64_sse2,  // 64x64
329 330
  av1_lowbd_inv_txfm2d_add_4x8_sse2,    // TX_4X8
  av1_lowbd_inv_txfm2d_add_8x4_sse2,    // TX_8X4
331 332 333 334
  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
Peng Bin's avatar
Peng Bin committed
335 336
  av1_lowbd_inv_txfm2d_add_32x64_sse2,  // TX_32X64
  av1_lowbd_inv_txfm2d_add_64x32_sse2,  // TX_64X32
337 338
  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
339 340 341 342
  av1_lowbd_inv_txfm2d_add_8x32_sse2,   // 8x32
  av1_lowbd_inv_txfm2d_add_32x8_sse2,   // 32x8
  av1_lowbd_inv_txfm2d_add_16x64_sse2,  // 16x64
  av1_lowbd_inv_txfm2d_add_64x16_sse2,  // 64x16
343 344
};
INSTANTIATE_TEST_CASE_P(SSE2, AV1LbdInvTxfm2d,
345 346
                        Combine(Values(kLbdInvFuncSSE2List),
                                Range(0, (int)TX_SIZES_ALL, 1)));
347 348 349

#endif  // HAVE_SSE2

350
}  // namespace