tor-browser

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

av1_inv_txfm1d_test.cc (4840B)


      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 
     14 #include "test/av1_txfm_test.h"
     15 #include "test/util.h"
     16 #include "av1/common/av1_inv_txfm1d.h"
     17 #include "av1/encoder/av1_fwd_txfm1d.h"
     18 
     19 using libaom_test::ACMRandom;
     20 using libaom_test::input_base;
     21 
     22 namespace {
     23 const int txfm_type_num = 2;
     24 const int txfm_size_ls[] = { 4, 8, 16, 32, 64 };
     25 
     26 const TxfmFunc fwd_txfm_func_ls[][txfm_type_num] = {
     27  { av1_fdct4, av1_fadst4 },   { av1_fdct8, av1_fadst8 },
     28  { av1_fdct16, av1_fadst16 }, { av1_fdct32, nullptr },
     29  { av1_fdct64, nullptr },
     30 };
     31 
     32 const TxfmFunc inv_txfm_func_ls[][txfm_type_num] = {
     33  { av1_idct4, av1_iadst4 },   { av1_idct8, av1_iadst8 },
     34  { av1_idct16, av1_iadst16 }, { av1_idct32, nullptr },
     35  { av1_idct64, nullptr },
     36 };
     37 
     38 // the maximum stage number of fwd/inv 1d dct/adst txfm is 12
     39 const int8_t cos_bit = 13;
     40 const int8_t range_bit[12] = { 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 };
     41 
     42 void reference_idct_1d_int(const int32_t *in, int32_t *out, int size) {
     43  double input[64];
     44  for (int i = 0; i < size; ++i) input[i] = in[i];
     45 
     46  double output[64];
     47  libaom_test::reference_idct_1d(input, output, size);
     48 
     49  for (int i = 0; i < size; ++i) {
     50    ASSERT_GE(output[i], INT32_MIN);
     51    ASSERT_LE(output[i], INT32_MAX);
     52    out[i] = static_cast<int32_t>(round(output[i]));
     53  }
     54 }
     55 
     56 void random_matrix(int32_t *dst, int len, ACMRandom *rnd) {
     57  const int bits = 16;
     58  const int maxVal = (1 << (bits - 1)) - 1;
     59  const int minVal = -(1 << (bits - 1));
     60  for (int i = 0; i < len; ++i) {
     61    if (rnd->Rand8() % 10)
     62      dst[i] = minVal + rnd->Rand16() % (1 << bits);
     63    else
     64      dst[i] = rnd->Rand8() % 2 ? minVal : maxVal;
     65  }
     66 }
     67 
     68 TEST(av1_inv_txfm1d, InvAccuracyCheck) {
     69  ACMRandom rnd(ACMRandom::DeterministicSeed());
     70  const int count_test_block = 20000;
     71  const int max_error[] = { 6, 10, 19, 31, 40 };
     72  ASSERT_EQ(NELEMENTS(max_error), TX_SIZES);
     73  ASSERT_EQ(NELEMENTS(inv_txfm_func_ls), TX_SIZES);
     74  for (int i = 0; i < count_test_block; ++i) {
     75    // choose a random transform to test
     76    const TX_SIZE tx_size = static_cast<TX_SIZE>(rnd.Rand8() % TX_SIZES);
     77    const int txfm_size = txfm_size_ls[tx_size];
     78    const TxfmFunc inv_txfm_func = inv_txfm_func_ls[tx_size][0];
     79 
     80    int32_t input[64];
     81    random_matrix(input, txfm_size, &rnd);
     82 
     83    // 64x64 transform assumes last 32 values are zero.
     84    memset(input + 32, 0, 32 * sizeof(input[0]));
     85 
     86    int32_t ref_output[64];
     87    memset(ref_output, 0, sizeof(ref_output));
     88    reference_idct_1d_int(input, ref_output, txfm_size);
     89 
     90    int32_t output[64];
     91    memset(output, 0, sizeof(output));
     92    inv_txfm_func(input, output, cos_bit, range_bit);
     93 
     94    for (int ni = 0; ni < txfm_size; ++ni) {
     95      EXPECT_LE(abs(output[ni] - ref_output[ni]), max_error[tx_size])
     96          << "tx_size = " << tx_size << ", ni = " << ni
     97          << ", output[ni] = " << output[ni]
     98          << ", ref_output[ni] = " << ref_output[ni];
     99    }
    100  }
    101 }
    102 
    103 static inline int get_max_bit(int x) {
    104  int max_bit = -1;
    105  while (x) {
    106    x = x >> 1;
    107    max_bit++;
    108  }
    109  return max_bit;
    110 }
    111 
    112 TEST(av1_inv_txfm1d, get_max_bit) {
    113  int max_bit = get_max_bit(8);
    114  EXPECT_EQ(max_bit, 3);
    115 }
    116 
    117 TEST(av1_inv_txfm1d, round_trip) {
    118  ACMRandom rnd(ACMRandom::DeterministicSeed());
    119  for (int si = 0; si < NELEMENTS(fwd_txfm_func_ls); ++si) {
    120    int txfm_size = txfm_size_ls[si];
    121 
    122    for (int ti = 0; ti < txfm_type_num; ++ti) {
    123      TxfmFunc fwd_txfm_func = fwd_txfm_func_ls[si][ti];
    124      TxfmFunc inv_txfm_func = inv_txfm_func_ls[si][ti];
    125      int max_error = 2;
    126 
    127      if (!fwd_txfm_func) continue;
    128 
    129      const int count_test_block = 5000;
    130      for (int i = 0; i < count_test_block; ++i) {
    131        int32_t input[64];
    132        int32_t output[64];
    133        int32_t round_trip_output[64];
    134 
    135        ASSERT_LE(txfm_size, NELEMENTS(input));
    136 
    137        for (int ni = 0; ni < txfm_size; ++ni) {
    138          input[ni] = rnd.Rand16() % input_base - rnd.Rand16() % input_base;
    139        }
    140 
    141        fwd_txfm_func(input, output, cos_bit, range_bit);
    142        inv_txfm_func(output, round_trip_output, cos_bit, range_bit);
    143 
    144        for (int ni = 0; ni < txfm_size; ++ni) {
    145          int node_err =
    146              abs(input[ni] - round_shift(round_trip_output[ni],
    147                                          get_max_bit(txfm_size) - 1));
    148          EXPECT_LE(node_err, max_error);
    149        }
    150      }
    151    }
    152  }
    153 }
    154 
    155 }  // namespace