av1_inv_txfm1d_test.cc 4.72 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
#include <math.h>

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"
Angie Chiang's avatar
Angie Chiang committed
18

19 20
using libaom_test::ACMRandom;
using libaom_test::input_base;
Angie Chiang's avatar
Angie Chiang committed
21 22

namespace {
23
const int txfm_type_num = 2;
Yaowu Xu's avatar
Yaowu Xu committed
24
const int txfm_size_ls[] = { 4, 8, 16, 32, 64 };
Angie Chiang's avatar
Angie Chiang committed
25

26
const TxfmFunc fwd_txfm_func_ls[][txfm_type_num] = {
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
27 28 29 30 31
  { av1_fdct4_new, av1_fadst4_new },
  { av1_fdct8_new, av1_fadst8_new },
  { av1_fdct16_new, av1_fadst16_new },
  { av1_fdct32_new, av1_fadst32_new },
  { av1_fdct64_new, NULL },
clang-format's avatar
clang-format committed
32
};
Angie Chiang's avatar
Angie Chiang committed
33

34
const TxfmFunc inv_txfm_func_ls[][txfm_type_num] = {
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
35 36 37 38 39
  { av1_idct4_new, av1_iadst4_new },
  { av1_idct8_new, av1_iadst8_new },
  { av1_idct16_new, av1_iadst16_new },
  { av1_idct32_new, av1_iadst32_new },
  { av1_idct64_new, NULL },
clang-format's avatar
clang-format committed
40
};
Angie Chiang's avatar
Angie Chiang committed
41 42

// the maximum stage number of fwd/inv 1d dct/adst txfm is 12
43
const int8_t cos_bit = 13;
44
const int8_t range_bit[12] = { 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 };
Angie Chiang's avatar
Angie Chiang committed
45

46 47 48 49 50 51 52
void reference_idct_1d_int(const int32_t *in, int32_t *out, int size) {
  double input[64];
  for (int i = 0; i < size; ++i) input[i] = in[i];

  double output[64];
  libaom_test::reference_idct_1d(input, output, size);

Urvang Joshi's avatar
Urvang Joshi committed
53 54 55
  for (int i = 0; i < size; ++i) {
    ASSERT_GE(output[i], INT32_MIN);
    ASSERT_LE(output[i], INT32_MAX);
56
    out[i] = static_cast<int32_t>(round(output[i]));
Urvang Joshi's avatar
Urvang Joshi committed
57
  }
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
}

void random_matrix(int32_t *dst, int len, ACMRandom *rnd) {
  const int bits = 16;
  const int maxVal = (1 << (bits - 1)) - 1;
  const int minVal = -(1 << (bits - 1));
  for (int i = 0; i < len; ++i) {
    if (rnd->Rand8() % 10)
      dst[i] = minVal + rnd->Rand16() % (1 << bits);
    else
      dst[i] = rnd->Rand8() % 2 ? minVal : maxVal;
  }
}

TEST(av1_inv_txfm1d, InvAccuracyCheck) {
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  const int count_test_block = 20000;
Yaowu Xu's avatar
Yaowu Xu committed
75
  const int max_error[] = { 6, 10, 19, 31, 40 };
Urvang Joshi's avatar
Urvang Joshi committed
76 77
  ASSERT_EQ(NELEMENTS(max_error), TX_SIZES);
  ASSERT_EQ(NELEMENTS(inv_txfm_func_ls), TX_SIZES);
78 79
  for (int k = 0; k < count_test_block; ++k) {
    // choose a random transform to test
Urvang Joshi's avatar
Urvang Joshi committed
80 81 82
    const TX_SIZE tx_size = static_cast<TX_SIZE>(rnd.Rand8() % TX_SIZES);
    const int tx_size_pix = txfm_size_ls[tx_size];
    const TxfmFunc inv_txfm_func = inv_txfm_func_ls[tx_size][0];
83 84

    int32_t input[64];
Urvang Joshi's avatar
Urvang Joshi committed
85 86 87 88
    random_matrix(input, tx_size_pix, &rnd);

    // 64x64 transform assumes last 32 values are zero.
    memset(input + 32, 0, 32 * sizeof(input[0]));
89 90

    int32_t ref_output[64];
Urvang Joshi's avatar
Urvang Joshi committed
91
    reference_idct_1d_int(input, ref_output, tx_size_pix);
92 93

    int32_t output[64];
Urvang Joshi's avatar
Urvang Joshi committed
94
    inv_txfm_func(input, output, cos_bit, range_bit);
95

Urvang Joshi's avatar
Urvang Joshi committed
96 97 98 99 100
    for (int i = 0; i < tx_size_pix; ++i) {
      EXPECT_LE(abs(output[i] - ref_output[i]), max_error[tx_size])
          << "tx_size = " << tx_size << ", i = " << i
          << ", output[i] = " << output[i]
          << ", ref_output[i] = " << ref_output[i];
101 102 103 104
    }
  }
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118
static INLINE int get_max_bit(int x) {
  int max_bit = -1;
  while (x) {
    x = x >> 1;
    max_bit++;
  }
  return max_bit;
}

TEST(av1_inv_txfm1d, get_max_bit) {
  int max_bit = get_max_bit(8);
  EXPECT_EQ(max_bit, 3);
}

119
TEST(av1_inv_txfm1d, round_trip) {
Angie Chiang's avatar
Angie Chiang committed
120
  ACMRandom rnd(ACMRandom::DeterministicSeed());
121
  for (int si = 0; si < NELEMENTS(fwd_txfm_func_ls); ++si) {
Angie Chiang's avatar
Angie Chiang committed
122 123 124
    int txfm_size = txfm_size_ls[si];

    for (int ti = 0; ti < txfm_type_num; ++ti) {
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
125 126
      TxfmFunc fwd_txfm_func = fwd_txfm_func_ls[si][ti];
      TxfmFunc inv_txfm_func = inv_txfm_func_ls[si][ti];
Angie Chiang's avatar
Angie Chiang committed
127 128
      int max_error = 2;

129
      if (!fwd_txfm_func) continue;
Angie Chiang's avatar
Angie Chiang committed
130

131 132
      const int count_test_block = 5000;
      for (int ci = 0; ci < count_test_block; ++ci) {
133 134 135 136
        int32_t input[64];
        int32_t output[64];
        int32_t round_trip_output[64];

137
        ASSERT_LE(txfm_size, NELEMENTS(input));
138

139 140 141 142 143 144
        for (int ni = 0; ni < txfm_size; ++ni) {
          input[ni] = rnd.Rand16() % input_base - rnd.Rand16() % input_base;
        }

        fwd_txfm_func(input, output, cos_bit, range_bit);
        inv_txfm_func(output, round_trip_output, cos_bit, range_bit);
Angie Chiang's avatar
Angie Chiang committed
145

146 147 148 149 150
        for (int ni = 0; ni < txfm_size; ++ni) {
          int node_err =
              abs(input[ni] - round_shift(round_trip_output[ni],
                                          get_max_bit(txfm_size) - 1));
          EXPECT_LE(node_err, max_error);
Angie Chiang's avatar
Angie Chiang committed
151 152 153 154 155 156 157
        }
      }
    }
  }
}

}  // namespace