tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

av1_inv_txfm2d_test.cc (14444B)


      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 #include <math.h>
     13 #include <stdio.h>
     14 #include <stdlib.h>
     15 #include <tuple>
     16 #include <vector>
     17 
     18 #include "config/av1_rtcd.h"
     19 
     20 #include "aom_ports/aom_timer.h"
     21 #include "av1/common/av1_inv_txfm1d_cfg.h"
     22 #include "av1/common/scan.h"
     23 #include "test/acm_random.h"
     24 #include "test/av1_txfm_test.h"
     25 #include "test/util.h"
     26 
     27 using libaom_test::ACMRandom;
     28 using libaom_test::bd;
     29 using libaom_test::compute_avg_abs_error;
     30 using libaom_test::input_base;
     31 using libaom_test::InvTxfm2dFunc;
     32 using libaom_test::LbdInvTxfm2dFunc;
     33 using libaom_test::tx_type_name;
     34 
     35 using ::testing::Combine;
     36 using ::testing::Range;
     37 using ::testing::Values;
     38 
     39 using std::vector;
     40 
     41 namespace {
     42 
     43 // AV1InvTxfm2dParam argument list:
     44 // tx_type_, tx_size_, max_error_, max_avg_error_
     45 using AV1InvTxfm2dParam = std::tuple<TX_TYPE, TX_SIZE, int, double>;
     46 
     47 class AV1InvTxfm2d : public ::testing::TestWithParam<AV1InvTxfm2dParam> {
     48 public:
     49  void SetUp() override {
     50    tx_type_ = GET_PARAM(0);
     51    tx_size_ = GET_PARAM(1);
     52    max_error_ = GET_PARAM(2);
     53    max_avg_error_ = GET_PARAM(3);
     54  }
     55 
     56  void RunRoundtripCheck() {
     57    int tx_w = tx_size_wide[tx_size_];
     58    int tx_h = tx_size_high[tx_size_];
     59    int txfm2d_size = tx_w * tx_h;
     60    const FwdTxfm2dFunc fwd_txfm_func = libaom_test::fwd_txfm_func_ls[tx_size_];
     61    const InvTxfm2dFunc inv_txfm_func = libaom_test::inv_txfm_func_ls[tx_size_];
     62    double avg_abs_error = 0;
     63    ACMRandom rnd(ACMRandom::DeterministicSeed());
     64 
     65    const int count = 500;
     66 
     67    for (int ci = 0; ci < count; ci++) {
     68      DECLARE_ALIGNED(16, int16_t, input[64 * 64]) = { 0 };
     69      ASSERT_LE(txfm2d_size, NELEMENTS(input));
     70 
     71      for (int ni = 0; ni < txfm2d_size; ++ni) {
     72        if (ci == 0) {
     73          int extreme_input = input_base - 1;
     74          input[ni] = extreme_input;  // extreme case
     75        } else {
     76          input[ni] = rnd.Rand16() % input_base;
     77        }
     78      }
     79 
     80      DECLARE_ALIGNED(16, uint16_t, expected[64 * 64]) = { 0 };
     81      ASSERT_LE(txfm2d_size, NELEMENTS(expected));
     82      if (TxfmUsesApproximation()) {
     83        // Compare reference forward HT + inverse HT vs forward HT + inverse HT.
     84        double ref_input[64 * 64];
     85        ASSERT_LE(txfm2d_size, NELEMENTS(ref_input));
     86        for (int ni = 0; ni < txfm2d_size; ++ni) {
     87          ref_input[ni] = input[ni];
     88        }
     89        double ref_coeffs[64 * 64] = { 0 };
     90        ASSERT_LE(txfm2d_size, NELEMENTS(ref_coeffs));
     91        ASSERT_EQ(tx_type_, static_cast<TX_TYPE>(DCT_DCT));
     92        libaom_test::reference_hybrid_2d(ref_input, ref_coeffs, tx_type_,
     93                                         tx_size_);
     94        DECLARE_ALIGNED(16, int32_t, ref_coeffs_int[64 * 64]) = { 0 };
     95        ASSERT_LE(txfm2d_size, NELEMENTS(ref_coeffs_int));
     96        for (int ni = 0; ni < txfm2d_size; ++ni) {
     97          ref_coeffs_int[ni] = (int32_t)round(ref_coeffs[ni]);
     98        }
     99        inv_txfm_func(ref_coeffs_int, expected, tx_w, tx_type_, bd);
    100      } else {
    101        // Compare original input vs forward HT + inverse HT.
    102        for (int ni = 0; ni < txfm2d_size; ++ni) {
    103          expected[ni] = input[ni];
    104        }
    105      }
    106 
    107      DECLARE_ALIGNED(16, int32_t, coeffs[64 * 64]) = { 0 };
    108      ASSERT_LE(txfm2d_size, NELEMENTS(coeffs));
    109      fwd_txfm_func(input, coeffs, tx_w, tx_type_, bd);
    110 
    111      DECLARE_ALIGNED(16, uint16_t, actual[64 * 64]) = { 0 };
    112      ASSERT_LE(txfm2d_size, NELEMENTS(actual));
    113      inv_txfm_func(coeffs, actual, tx_w, tx_type_, bd);
    114 
    115      double actual_max_error = 0;
    116      for (int ni = 0; ni < txfm2d_size; ++ni) {
    117        const double this_error = abs(expected[ni] - actual[ni]);
    118        actual_max_error = AOMMAX(actual_max_error, this_error);
    119      }
    120      EXPECT_GE(max_error_, actual_max_error)
    121          << " tx_w: " << tx_w << " tx_h " << tx_h
    122          << " tx_type: " << tx_type_name[tx_type_];
    123      if (actual_max_error > max_error_) {  // exit early.
    124        break;
    125      }
    126      avg_abs_error += compute_avg_abs_error<uint16_t, uint16_t>(
    127          expected, actual, txfm2d_size);
    128    }
    129 
    130    avg_abs_error /= count;
    131    EXPECT_GE(max_avg_error_, avg_abs_error)
    132        << " tx_w: " << tx_w << " tx_h " << tx_h
    133        << " tx_type: " << tx_type_name[tx_type_];
    134  }
    135 
    136 private:
    137  bool TxfmUsesApproximation() {
    138    if (tx_size_wide[tx_size_] == 64 || tx_size_high[tx_size_] == 64) {
    139      return true;
    140    }
    141    return false;
    142  }
    143 
    144  int max_error_;
    145  double max_avg_error_;
    146  TX_TYPE tx_type_;
    147  TX_SIZE tx_size_;
    148 };
    149 
    150 static const int max_error_ls[TX_SIZES_ALL] = {
    151  2,  // 4x4 transform
    152  2,  // 8x8 transform
    153  2,  // 16x16 transform
    154  4,  // 32x32 transform
    155  3,  // 64x64 transform
    156  2,  // 4x8 transform
    157  2,  // 8x4 transform
    158  2,  // 8x16 transform
    159  2,  // 16x8 transform
    160  3,  // 16x32 transform
    161  3,  // 32x16 transform
    162  5,  // 32x64 transform
    163  5,  // 64x32 transform
    164  2,  // 4x16 transform
    165  2,  // 16x4 transform
    166  2,  // 8x32 transform
    167  2,  // 32x8 transform
    168  3,  // 16x64 transform
    169  3,  // 64x16 transform
    170 };
    171 
    172 static const double avg_error_ls[TX_SIZES_ALL] = {
    173  0.002,  // 4x4 transform
    174  0.05,   // 8x8 transform
    175  0.07,   // 16x16 transform
    176  0.4,    // 32x32 transform
    177  0.3,    // 64x64 transform
    178  0.02,   // 4x8 transform
    179  0.02,   // 8x4 transform
    180  0.04,   // 8x16 transform
    181  0.07,   // 16x8 transform
    182  0.4,    // 16x32 transform
    183  0.5,    // 32x16 transform
    184  0.38,   // 32x64 transform
    185  0.39,   // 64x32 transform
    186  0.2,    // 4x16 transform
    187  0.2,    // 16x4 transform
    188  0.2,    // 8x32 transform
    189  0.2,    // 32x8 transform
    190  0.38,   // 16x64 transform
    191  0.38,   // 64x16 transform
    192 };
    193 
    194 vector<AV1InvTxfm2dParam> GetInvTxfm2dParamList() {
    195  vector<AV1InvTxfm2dParam> param_list;
    196  for (int s = 0; s < TX_SIZES; ++s) {
    197    const int max_error = max_error_ls[s];
    198    const double avg_error = avg_error_ls[s];
    199    for (int t = 0; t < TX_TYPES; ++t) {
    200      const TX_TYPE tx_type = static_cast<TX_TYPE>(t);
    201      const TX_SIZE tx_size = static_cast<TX_SIZE>(s);
    202      if (libaom_test::IsTxSizeTypeValid(tx_size, tx_type)) {
    203        param_list.push_back(
    204            AV1InvTxfm2dParam(tx_type, tx_size, max_error, avg_error));
    205      }
    206    }
    207  }
    208  return param_list;
    209 }
    210 
    211 INSTANTIATE_TEST_SUITE_P(C, AV1InvTxfm2d,
    212                         ::testing::ValuesIn(GetInvTxfm2dParamList()));
    213 
    214 TEST_P(AV1InvTxfm2d, RunRoundtripCheck) { RunRoundtripCheck(); }
    215 
    216 TEST(AV1InvTxfm2d, CfgTest) {
    217  for (int bd_idx = 0; bd_idx < BD_NUM; ++bd_idx) {
    218    int bd = libaom_test::bd_arr[bd_idx];
    219    int8_t low_range = libaom_test::low_range_arr[bd_idx];
    220    int8_t high_range = libaom_test::high_range_arr[bd_idx];
    221    for (int tx_size = 0; tx_size < TX_SIZES_ALL; ++tx_size) {
    222      for (int tx_type = 0; tx_type < TX_TYPES; ++tx_type) {
    223        if (libaom_test::IsTxSizeTypeValid(static_cast<TX_SIZE>(tx_size),
    224                                           static_cast<TX_TYPE>(tx_type)) ==
    225            false) {
    226          continue;
    227        }
    228        TXFM_2D_FLIP_CFG cfg;
    229        av1_get_inv_txfm_cfg(static_cast<TX_TYPE>(tx_type),
    230                             static_cast<TX_SIZE>(tx_size), &cfg);
    231        int8_t stage_range_col[MAX_TXFM_STAGE_NUM];
    232        int8_t stage_range_row[MAX_TXFM_STAGE_NUM];
    233        av1_gen_inv_stage_range(stage_range_col, stage_range_row, &cfg,
    234                                static_cast<TX_SIZE>(tx_size), bd);
    235        libaom_test::txfm_stage_range_check(stage_range_col, cfg.stage_num_col,
    236                                            cfg.cos_bit_col, low_range,
    237                                            high_range);
    238        libaom_test::txfm_stage_range_check(stage_range_row, cfg.stage_num_row,
    239                                            cfg.cos_bit_row, low_range,
    240                                            high_range);
    241      }
    242    }
    243  }
    244 }
    245 
    246 using AV1LbdInvTxfm2dParam = std::tuple<const LbdInvTxfm2dFunc>;
    247 class AV1LbdInvTxfm2d : public ::testing::TestWithParam<AV1LbdInvTxfm2dParam> {
    248 public:
    249  void SetUp() override { target_func_ = GET_PARAM(0); }
    250  void RunAV1InvTxfm2dTest(TX_TYPE tx_type, TX_SIZE tx_size, int run_times,
    251                           int gt_int16 = 0);
    252 
    253 private:
    254  LbdInvTxfm2dFunc target_func_;
    255 };
    256 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AV1LbdInvTxfm2d);
    257 
    258 void AV1LbdInvTxfm2d::RunAV1InvTxfm2dTest(TX_TYPE tx_type, TX_SIZE tx_size,
    259                                          int run_times, int gt_int16) {
    260  FwdTxfm2dFunc fwd_func_ = libaom_test::fwd_txfm_func_ls[tx_size];
    261  InvTxfm2dFunc ref_func_ = libaom_test::inv_txfm_func_ls[tx_size];
    262  if (fwd_func_ == nullptr || ref_func_ == nullptr || target_func_ == nullptr) {
    263    return;
    264  }
    265  const int bd = 8;
    266  const int BLK_WIDTH = 64;
    267  const int BLK_SIZE = BLK_WIDTH * BLK_WIDTH;
    268  DECLARE_ALIGNED(16, int16_t, input[BLK_SIZE]) = { 0 };
    269  DECLARE_ALIGNED(32, int32_t, inv_input[BLK_SIZE]) = { 0 };
    270  DECLARE_ALIGNED(16, uint8_t, output[BLK_SIZE]) = { 0 };
    271  DECLARE_ALIGNED(16, uint16_t, ref_output[BLK_SIZE]) = { 0 };
    272  int stride = BLK_WIDTH;
    273  int rows = tx_size_high[tx_size];
    274  int cols = tx_size_wide[tx_size];
    275  const int rows_nonezero = AOMMIN(32, rows);
    276  const int cols_nonezero = AOMMIN(32, cols);
    277  run_times /= (rows * cols);
    278  run_times = AOMMAX(1, run_times);
    279  const SCAN_ORDER *scan_order = get_default_scan(tx_size, tx_type);
    280  const int16_t *scan = scan_order->scan;
    281  const int16_t eobmax = rows_nonezero * cols_nonezero;
    282  ACMRandom rnd(ACMRandom::DeterministicSeed());
    283  int randTimes = run_times == 1 ? (eobmax + 500) : 1;
    284 
    285  for (int cnt = 0; cnt < randTimes; ++cnt) {
    286    const int16_t max_in = (1 << (bd)) - 1;
    287    for (int r = 0; r < BLK_WIDTH; ++r) {
    288      for (int c = 0; c < BLK_WIDTH; ++c) {
    289        input[r * cols + c] = (cnt == 0) ? max_in : rnd.Rand8Extremes();
    290        output[r * stride + c] = (cnt == 0) ? 128 : rnd.Rand8();
    291        ref_output[r * stride + c] = output[r * stride + c];
    292      }
    293    }
    294    fwd_func_(input, inv_input, stride, tx_type, bd);
    295 
    296    // produce eob input by setting high freq coeffs to zero
    297    const int eob = AOMMIN(cnt + 1, eobmax);
    298    for (int i = eob; i < eobmax; i++) {
    299      inv_input[scan[i]] = 0;
    300    }
    301    if (gt_int16) {
    302      inv_input[scan[eob - 1]] = ((int32_t)INT16_MAX * 100 / 141);
    303    }
    304    aom_usec_timer timer;
    305    aom_usec_timer_start(&timer);
    306    for (int i = 0; i < run_times; ++i) {
    307      ref_func_(inv_input, ref_output, stride, tx_type, bd);
    308    }
    309    aom_usec_timer_mark(&timer);
    310    const double time1 = static_cast<double>(aom_usec_timer_elapsed(&timer));
    311    aom_usec_timer_start(&timer);
    312    for (int i = 0; i < run_times; ++i) {
    313      target_func_(inv_input, output, stride, tx_type, tx_size, eob);
    314    }
    315    aom_usec_timer_mark(&timer);
    316    const double time2 = static_cast<double>(aom_usec_timer_elapsed(&timer));
    317    if (run_times > 10) {
    318      printf("txfm[%d] %3dx%-3d:%7.2f/%7.2fns", tx_type, cols, rows, time1,
    319             time2);
    320      printf("(%3.2f)\n", time1 / time2);
    321    }
    322    for (int r = 0; r < rows; ++r) {
    323      for (int c = 0; c < cols; ++c) {
    324        uint8_t ref_value = static_cast<uint8_t>(ref_output[r * stride + c]);
    325        if (ref_value != output[r * stride + c]) {
    326          printf(" ");
    327        }
    328        ASSERT_EQ(ref_value, output[r * stride + c])
    329            << "[" << r << "," << c << "] " << cnt << " tx_size: " << cols
    330            << "x" << rows << " tx_type: " << tx_type_name[tx_type] << " eob "
    331            << eob;
    332      }
    333    }
    334  }
    335 }
    336 
    337 TEST_P(AV1LbdInvTxfm2d, match) {
    338  for (int j = 0; j < (int)(TX_SIZES_ALL); ++j) {
    339    for (int i = 0; i < (int)TX_TYPES; ++i) {
    340      if (libaom_test::IsTxSizeTypeValid(static_cast<TX_SIZE>(j),
    341                                         static_cast<TX_TYPE>(i))) {
    342        RunAV1InvTxfm2dTest(static_cast<TX_TYPE>(i), static_cast<TX_SIZE>(j),
    343                            1);
    344      }
    345    }
    346  }
    347 }
    348 
    349 TEST_P(AV1LbdInvTxfm2d, gt_int16) {
    350  static const TX_TYPE types[] = {
    351    DCT_DCT, ADST_DCT, FLIPADST_DCT, IDTX, V_DCT, H_DCT, H_ADST, H_FLIPADST
    352  };
    353  for (int j = 0; j < (int)(TX_SIZES_ALL); ++j) {
    354    const TX_SIZE sz = static_cast<TX_SIZE>(j);
    355    for (uint8_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) {
    356      const TX_TYPE tp = types[i];
    357      if (libaom_test::IsTxSizeTypeValid(sz, tp)) {
    358        RunAV1InvTxfm2dTest(tp, sz, 1, 1);
    359      }
    360    }
    361  }
    362 }
    363 
    364 TEST_P(AV1LbdInvTxfm2d, DISABLED_Speed) {
    365  for (int j = 1; j < (int)(TX_SIZES_ALL); ++j) {
    366    for (int i = 0; i < (int)TX_TYPES; ++i) {
    367      if (libaom_test::IsTxSizeTypeValid(static_cast<TX_SIZE>(j),
    368                                         static_cast<TX_TYPE>(i))) {
    369        RunAV1InvTxfm2dTest(static_cast<TX_TYPE>(i), static_cast<TX_SIZE>(j),
    370                            10000000);
    371      }
    372    }
    373  }
    374 }
    375 
    376 #if HAVE_SSSE3
    377 extern "C" void av1_lowbd_inv_txfm2d_add_ssse3(const int32_t *input,
    378                                               uint8_t *output, int stride,
    379                                               TX_TYPE tx_type, TX_SIZE tx_size,
    380                                               int eob);
    381 INSTANTIATE_TEST_SUITE_P(SSSE3, AV1LbdInvTxfm2d,
    382                         ::testing::Values(av1_lowbd_inv_txfm2d_add_ssse3));
    383 #endif  // HAVE_SSSE3
    384 
    385 #if HAVE_AVX2
    386 extern "C" void av1_lowbd_inv_txfm2d_add_avx2(const int32_t *input,
    387                                              uint8_t *output, int stride,
    388                                              TX_TYPE tx_type, TX_SIZE tx_size,
    389                                              int eob);
    390 
    391 INSTANTIATE_TEST_SUITE_P(AVX2, AV1LbdInvTxfm2d,
    392                         ::testing::Values(av1_lowbd_inv_txfm2d_add_avx2));
    393 #endif  // HAVE_AVX2
    394 
    395 #if HAVE_NEON
    396 extern "C" void av1_lowbd_inv_txfm2d_add_neon(const int32_t *input,
    397                                              uint8_t *output, int stride,
    398                                              TX_TYPE tx_type, TX_SIZE tx_size,
    399                                              int eob);
    400 
    401 INSTANTIATE_TEST_SUITE_P(NEON, AV1LbdInvTxfm2d,
    402                         ::testing::Values(av1_lowbd_inv_txfm2d_add_neon));
    403 #endif  // HAVE_NEON
    404 
    405 }  // namespace