warp_filter_test_util.cc 12.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
 *
 * 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.
 */

#include "test/warp_filter_test_util.h"

using std::tr1::make_tuple;
15
using std::tr1::tuple;
16

Yaowu Xu's avatar
Yaowu Xu committed
17 18
namespace libaom_test {

19
int32_t random_warped_param(libaom_test::ACMRandom *rnd, int bits) {
20
  // 1 in 8 chance of generating zero (arbitrarily chosen)
21
  if (((rnd->Rand8()) & 7) == 0) return 0;
22 23
  // Otherwise, enerate uniform values in the range
  // [-(1 << bits), 1] U [1, 1<<bits]
24 25
  int32_t v = 1 + (rnd->Rand16() & ((1 << bits) - 1));
  if ((rnd->Rand8()) & 1) return -v;
26 27
  return v;
}
28

29 30 31
void generate_warped_model(libaom_test::ACMRandom *rnd, int32_t *mat,
                           int16_t *alpha, int16_t *beta, int16_t *gamma,
                           int16_t *delta) {
32
  while (1) {
33 34 35
    mat[0] = random_warped_param(rnd, WARPEDMODEL_PREC_BITS + 6);
    mat[1] = random_warped_param(rnd, WARPEDMODEL_PREC_BITS + 6);
    mat[2] = (random_warped_param(rnd, WARPEDMODEL_PREC_BITS - 3)) +
36
             (1 << WARPEDMODEL_PREC_BITS);
37
    mat[3] = random_warped_param(rnd, WARPEDMODEL_PREC_BITS - 3);
38
    // 50/50 chance of generating ROTZOOM vs. AFFINE models
39
    if (rnd->Rand8() & 1) {
40
      // AFFINE
41 42
      mat[4] = random_warped_param(rnd, WARPEDMODEL_PREC_BITS - 3);
      mat[5] = (random_warped_param(rnd, WARPEDMODEL_PREC_BITS - 3)) +
43 44 45 46 47 48 49 50 51 52
               (1 << WARPEDMODEL_PREC_BITS);
    } else {
      mat[4] = -mat[3];
      mat[5] = mat[2];
    }

    // Calculate the derived parameters and check that they are suitable
    // for the warp filter.
    assert(mat[2] != 0);

53 54
    *alpha = clamp(mat[2] - (1 << WARPEDMODEL_PREC_BITS), INT16_MIN, INT16_MAX);
    *beta = clamp(mat[3], INT16_MIN, INT16_MAX);
55
    *gamma = clamp(((int64_t)mat[4] * (1 << WARPEDMODEL_PREC_BITS)) / mat[2],
56 57 58 59 60
                   INT16_MIN, INT16_MAX);
    *delta =
        clamp(mat[5] - (((int64_t)mat[3] * mat[4] + (mat[2] / 2)) / mat[2]) -
                  (1 << WARPEDMODEL_PREC_BITS),
              INT16_MIN, INT16_MAX);
61

62 63
    if ((4 * abs(*alpha) + 7 * abs(*beta) >= (1 << WARPEDMODEL_PREC_BITS)) ||
        (4 * abs(*gamma) + 4 * abs(*delta) >= (1 << WARPEDMODEL_PREC_BITS)))
64 65
      continue;

66 67 68 69 70 71 72 73 74
    *alpha = ROUND_POWER_OF_TWO_SIGNED(*alpha, WARP_PARAM_REDUCE_BITS) *
             (1 << WARP_PARAM_REDUCE_BITS);
    *beta = ROUND_POWER_OF_TWO_SIGNED(*beta, WARP_PARAM_REDUCE_BITS) *
            (1 << WARP_PARAM_REDUCE_BITS);
    *gamma = ROUND_POWER_OF_TWO_SIGNED(*gamma, WARP_PARAM_REDUCE_BITS) *
             (1 << WARP_PARAM_REDUCE_BITS);
    *delta = ROUND_POWER_OF_TWO_SIGNED(*delta, WARP_PARAM_REDUCE_BITS) *
             (1 << WARP_PARAM_REDUCE_BITS);

75 76 77 78 79
    // We have a valid model, so finish
    return;
  }
}

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
namespace AV1WarpFilter {

::testing::internal::ParamGenerator<WarpTestParam> BuildParams(
    warp_affine_func filter) {
  const WarpTestParam params[] = {
    make_tuple(4, 4, 50000, filter),  make_tuple(8, 8, 50000, filter),
    make_tuple(64, 64, 1000, filter), make_tuple(4, 16, 20000, filter),
    make_tuple(32, 8, 10000, filter),
  };
  return ::testing::ValuesIn(params);
}

AV1WarpFilterTest::~AV1WarpFilterTest() {}
void AV1WarpFilterTest::SetUp() { rnd_.Reset(ACMRandom::DeterministicSeed()); }

void AV1WarpFilterTest::TearDown() { libaom_test::ClearSystemState(); }

97 98 99 100 101 102 103
void AV1WarpFilterTest::RunCheckOutput(warp_affine_func test_impl) {
  const int w = 128, h = 128;
  const int border = 16;
  const int stride = w + 2 * border;
  const int out_w = GET_PARAM(0), out_h = GET_PARAM(1);
  const int num_iters = GET_PARAM(2);
  int i, j, sub_x, sub_y;
104
  const int bd = 8;
105 106 107

  uint8_t *input_ = new uint8_t[h * stride];
  uint8_t *input = input_ + border;
108 109

  // The warp functions always write rows with widths that are multiples of 8.
110 111
  // So to avoid a buffer overflow, we may need to pad rows to a multiple of 8.
  int output_n = ((out_w + 7) & ~7) * out_h;
112 113
  uint8_t *output = new uint8_t[output_n];
  uint8_t *output2 = new uint8_t[output_n];
114 115
  int32_t mat[8];
  int16_t alpha, beta, gamma, delta;
116
  ConvolveParams conv_params = get_conv_params(0, 0, 0, bd);
117 118
  int32_t *dsta = new int32_t[output_n];
  int32_t *dstb = new int32_t[output_n];
119 120

  for (i = 0; i < num_iters; ++i) {
121 122 123 124 125 126 127
    // Generate an input block and extend its borders horizontally
    for (int r = 0; r < h; ++r)
      for (int c = 0; c < w; ++c) input[r * stride + c] = rnd_.Rand8();
    for (int r = 0; r < h; ++r) {
      memset(input + r * stride - border, input[r * stride], border);
      memset(input + r * stride + w, input[r * stride + (w - 1)], border);
    }
128

129
    const int use_no_round = rnd_.Rand8() & 1;
130 131
    for (sub_x = 0; sub_x < 2; ++sub_x)
      for (sub_y = 0; sub_y < 2; ++sub_y) {
132
        generate_warped_model(&rnd_, mat, &alpha, &beta, &gamma, &delta);
133
#if CONFIG_JNT_COMP
134 135
        for (int ii = 0; ii < 2; ++ii) {
          for (int jj = 0; jj < 5; ++jj) {
136
#endif  // CONFIG_JNT_COMP
137 138 139 140 141 142 143
            if (use_no_round) {
              // Prepare two copies of the destination
              for (j = 0; j < out_w * out_h; ++j) {
                int32_t v = rnd_.Rand16();
                dsta[j] = v;
                dstb[j] = v;
              }
144 145
              conv_params =
                  get_conv_params_no_round(0, 0, 0, dsta, out_w, 1, bd);
146
            } else {
147
              conv_params = get_conv_params(0, 0, 0, bd);
148
            }
149
#if CONFIG_JNT_COMP
150
            if (jj >= 4) {
151
              conv_params.use_jnt_comp_avg = 0;
152
            } else {
153
              conv_params.use_jnt_comp_avg = 1;
154 155 156
              conv_params.fwd_offset = quant_dist_lookup_table[ii][jj][0];
              conv_params.bck_offset = quant_dist_lookup_table[ii][jj][1];
            }
157
#endif  // CONFIG_JNT_COMP
158 159 160 161 162

            av1_warp_affine_c(mat, input, w, h, stride, output, 32, 32, out_w,
                              out_h, out_w, sub_x, sub_y, &conv_params, alpha,
                              beta, gamma, delta);
            if (use_no_round) {
163 164
              conv_params =
                  get_conv_params_no_round(0, 0, 0, dstb, out_w, 1, bd);
165
            }
166
#if CONFIG_JNT_COMP
167
            if (jj >= 4) {
168
              conv_params.use_jnt_comp_avg = 0;
169
            } else {
170
              conv_params.use_jnt_comp_avg = 1;
171 172 173
              conv_params.fwd_offset = quant_dist_lookup_table[ii][jj][0];
              conv_params.bck_offset = quant_dist_lookup_table[ii][jj][1];
            }
174
#endif  // CONFIG_JNT_COMP
175 176 177
            test_impl(mat, input, w, h, stride, output2, 32, 32, out_w, out_h,
                      out_w, sub_x, sub_y, &conv_params, alpha, beta, gamma,
                      delta);
178

179 180 181 182 183 184 185 186 187 188 189
            if (use_no_round) {
              for (j = 0; j < out_w * out_h; ++j)
                ASSERT_EQ(dsta[j], dstb[j])
                    << "Pixel mismatch at index " << j << " = (" << (j % out_w)
                    << ", " << (j / out_w) << ") on iteration " << i;
            } else {
              for (j = 0; j < out_w * out_h; ++j)
                ASSERT_EQ(output[j], output2[j])
                    << "Pixel mismatch at index " << j << " = (" << (j % out_w)
                    << ", " << (j / out_w) << ") on iteration " << i;
            }
190
#if CONFIG_JNT_COMP
191 192
          }
        }
193
#endif  // CONFIG_JNT_COMP
194 195
      }
  }
196 197 198
  delete[] input_;
  delete[] output;
  delete[] output2;
199 200
  delete[] dsta;
  delete[] dstb;
201
}
Yaowu Xu's avatar
Yaowu Xu committed
202
}  // namespace AV1WarpFilter
203

Yaowu Xu's avatar
Yaowu Xu committed
204 205 206
namespace AV1HighbdWarpFilter {

::testing::internal::ParamGenerator<HighbdWarpTestParam> GetDefaultParams() {
207
  const HighbdWarpTestParam defaultParams[] = {
208 209 210 211 212 213 214 215
    make_tuple(4, 4, 100, 8),    make_tuple(8, 8, 100, 8),
    make_tuple(64, 64, 100, 8),  make_tuple(4, 16, 100, 8),
    make_tuple(32, 8, 100, 8),   make_tuple(4, 4, 100, 10),
    make_tuple(8, 8, 100, 10),   make_tuple(64, 64, 100, 10),
    make_tuple(4, 16, 100, 10),  make_tuple(32, 8, 100, 10),
    make_tuple(4, 4, 100, 12),   make_tuple(8, 8, 100, 12),
    make_tuple(64, 64, 100, 12), make_tuple(4, 16, 100, 12),
    make_tuple(32, 8, 100, 12),
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  };
  return ::testing::ValuesIn(defaultParams);
}

AV1HighbdWarpFilterTest::~AV1HighbdWarpFilterTest() {}
void AV1HighbdWarpFilterTest::SetUp() {
  rnd_.Reset(ACMRandom::DeterministicSeed());
}

void AV1HighbdWarpFilterTest::TearDown() { libaom_test::ClearSystemState(); }

void AV1HighbdWarpFilterTest::RunCheckOutput(
    highbd_warp_affine_func test_impl) {
  const int w = 128, h = 128;
  const int border = 16;
  const int stride = w + 2 * border;
  const int out_w = GET_PARAM(0), out_h = GET_PARAM(1);
  const int num_iters = GET_PARAM(2);
  const int bd = GET_PARAM(3);
  const int mask = (1 << bd) - 1;
  int i, j, sub_x, sub_y;

238
  // The warp functions always write rows with widths that are multiples of 8.
239 240
  // So to avoid a buffer overflow, we may need to pad rows to a multiple of 8.
  int output_n = ((out_w + 7) & ~7) * out_h;
241 242
  uint16_t *input_ = new uint16_t[h * stride];
  uint16_t *input = input_ + border;
243 244
  uint16_t *output = new uint16_t[output_n];
  uint16_t *output2 = new uint16_t[output_n];
245 246
  int32_t mat[8];
  int16_t alpha, beta, gamma, delta;
247
  ConvolveParams conv_params = get_conv_params(0, 0, 0, bd);
248 249
  int32_t *dsta = new int32_t[output_n];
  int32_t *dstb = new int32_t[output_n];
250 251

  for (i = 0; i < num_iters; ++i) {
252 253 254 255 256 257 258 259 260
    // Generate an input block and extend its borders horizontally
    for (int r = 0; r < h; ++r)
      for (int c = 0; c < w; ++c) input[r * stride + c] = rnd_.Rand16() & mask;
    for (int r = 0; r < h; ++r) {
      for (int c = 0; c < border; ++c) {
        input[r * stride - border + c] = input[r * stride];
        input[r * stride + w + c] = input[r * stride + (w - 1)];
      }
    }
261
    const int use_no_round = rnd_.Rand8() & 1;
262 263
    for (sub_x = 0; sub_x < 2; ++sub_x)
      for (sub_y = 0; sub_y < 2; ++sub_y) {
264
        generate_warped_model(&rnd_, mat, &alpha, &beta, &gamma, &delta);
265
#if CONFIG_JNT_COMP
266 267
        for (int ii = 0; ii < 2; ++ii) {
          for (int jj = 0; jj < 5; ++jj) {
268
#endif  // CONFIG_JNT_COMP
269 270 271 272 273 274 275
            if (use_no_round) {
              // Prepare two copies of the destination
              for (j = 0; j < out_w * out_h; ++j) {
                int32_t v = rnd_.Rand16();
                dsta[j] = v;
                dstb[j] = v;
              }
276 277
              conv_params =
                  get_conv_params_no_round(0, 0, 0, dsta, out_w, 1, bd);
278
            } else {
279
              conv_params = get_conv_params(0, 0, 0, bd);
280
            }
281
#if CONFIG_JNT_COMP
282
            if (jj >= 4) {
283
              conv_params.use_jnt_comp_avg = 0;
284
            } else {
285
              conv_params.use_jnt_comp_avg = 1;
286 287 288
              conv_params.fwd_offset = quant_dist_lookup_table[ii][jj][0];
              conv_params.bck_offset = quant_dist_lookup_table[ii][jj][1];
            }
289
#endif  // CONFIG_JNT_COMP
290 291 292 293 294 295
            av1_highbd_warp_affine_c(mat, input, w, h, stride, output, 32, 32,
                                     out_w, out_h, out_w, sub_x, sub_y, bd,
                                     &conv_params, alpha, beta, gamma, delta);
            if (use_no_round) {
              // TODO(angiebird): Change this to test_impl once we have SIMD
              // implementation
296 297
              conv_params =
                  get_conv_params_no_round(0, 0, 0, dstb, out_w, 1, bd);
298
            }
299
#if CONFIG_JNT_COMP
300
            if (jj >= 4) {
301
              conv_params.use_jnt_comp_avg = 0;
302
            } else {
303
              conv_params.use_jnt_comp_avg = 1;
304 305 306
              conv_params.fwd_offset = quant_dist_lookup_table[ii][jj][0];
              conv_params.bck_offset = quant_dist_lookup_table[ii][jj][1];
            }
307
#endif  // CONFIG_JNT_COMP
308 309 310
            test_impl(mat, input, w, h, stride, output2, 32, 32, out_w, out_h,
                      out_w, sub_x, sub_y, bd, &conv_params, alpha, beta, gamma,
                      delta);
311

312 313 314 315 316 317 318 319 320 321 322
            if (use_no_round) {
              for (j = 0; j < out_w * out_h; ++j)
                ASSERT_EQ(dsta[j], dstb[j])
                    << "Pixel mismatch at index " << j << " = (" << (j % out_w)
                    << ", " << (j / out_w) << ") on iteration " << i;
            } else {
              for (j = 0; j < out_w * out_h; ++j)
                ASSERT_EQ(output[j], output2[j])
                    << "Pixel mismatch at index " << j << " = (" << (j % out_w)
                    << ", " << (j / out_w) << ") on iteration " << i;
            }
323
#if CONFIG_JNT_COMP
324 325
          }
        }
326
#endif  // CONFIG_JNT_COMP
327 328
      }
  }
329 330 331 332

  delete[] input_;
  delete[] output;
  delete[] output2;
333 334
  delete[] dsta;
  delete[] dstb;
335
}
Yaowu Xu's avatar
Yaowu Xu committed
336 337
}  // namespace AV1HighbdWarpFilter
}  // namespace libaom_test