summaryrefslogtreecommitdiffstats
path: root/third_party/aom/test/av1_wedge_utils_test.cc
blob: d4b560fc10cac176132aa2c807c021da1a6a130d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * 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 "third_party/googletest/src/googletest/include/gtest/gtest.h"

#include "./aom_config.h"

#include "./aom_dsp_rtcd.h"
#include "./av1_rtcd.h"

#include "aom_dsp/aom_dsp_common.h"

#include "av1/common/enums.h"

#include "test/acm_random.h"
#include "test/function_equivalence_test.h"
#include "test/register_state_check.h"

#define WEDGE_WEIGHT_BITS 6
#define MAX_MASK_VALUE (1 << (WEDGE_WEIGHT_BITS))

using libaom_test::ACMRandom;
using libaom_test::FunctionEquivalenceTest;

namespace {

static const int16_t kInt13Max = (1 << 12) - 1;

//////////////////////////////////////////////////////////////////////////////
// av1_wedge_sse_from_residuals - functionality
//////////////////////////////////////////////////////////////////////////////

class WedgeUtilsSSEFuncTest : public testing::Test {
 protected:
  WedgeUtilsSSEFuncTest() : rng_(ACMRandom::DeterministicSeed()) {}

  static const int kIterations = 1000;

  ACMRandom rng_;
};

static void equiv_blend_residuals(int16_t *r, const int16_t *r0,
                                  const int16_t *r1, const uint8_t *m, int N) {
  for (int i = 0; i < N; i++) {
    const int32_t m0 = m[i];
    const int32_t m1 = MAX_MASK_VALUE - m0;
    const int16_t R = m0 * r0[i] + m1 * r1[i];
    // Note that this rounding is designed to match the result
    // you would get when actually blending the 2 predictors and computing
    // the residuals.
    r[i] = ROUND_POWER_OF_TWO(R - 1, WEDGE_WEIGHT_BITS);
  }
}

static uint64_t equiv_sse_from_residuals(const int16_t *r0, const int16_t *r1,
                                         const uint8_t *m, int N) {
  uint64_t acc = 0;
  for (int i = 0; i < N; i++) {
    const int32_t m0 = m[i];
    const int32_t m1 = MAX_MASK_VALUE - m0;
    const int16_t R = m0 * r0[i] + m1 * r1[i];
    const int32_t r = ROUND_POWER_OF_TWO(R - 1, WEDGE_WEIGHT_BITS);
    acc += r * r;
  }
  return acc;
}

TEST_F(WedgeUtilsSSEFuncTest, ResidualBlendingEquiv) {
  DECLARE_ALIGNED(32, uint8_t, s[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, uint8_t, p0[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, uint8_t, p1[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, uint8_t, p[MAX_SB_SQUARE]);

  DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, int16_t, r_ref[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, int16_t, r_tst[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, uint8_t, m[MAX_SB_SQUARE]);

  for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) {
    for (int i = 0; i < MAX_SB_SQUARE; ++i) {
      s[i] = rng_.Rand8();
      m[i] = rng_(MAX_MASK_VALUE + 1);
    }

    const int w = 1 << (rng_(MAX_SB_SIZE_LOG2 + 1 - 3) + 3);
    const int h = 1 << (rng_(MAX_SB_SIZE_LOG2 + 1 - 3) + 3);
    const int N = w * h;

    for (int j = 0; j < N; j++) {
      p0[j] = clamp(s[j] + rng_(33) - 16, 0, UINT8_MAX);
      p1[j] = clamp(s[j] + rng_(33) - 16, 0, UINT8_MAX);
    }

    aom_blend_a64_mask(p, w, p0, w, p1, w, m, w, h, w, 0, 0);

    aom_subtract_block(h, w, r0, w, s, w, p0, w);
    aom_subtract_block(h, w, r1, w, s, w, p1, w);

    aom_subtract_block(h, w, r_ref, w, s, w, p, w);
    equiv_blend_residuals(r_tst, r0, r1, m, N);

    for (int i = 0; i < N; ++i) ASSERT_EQ(r_ref[i], r_tst[i]);

    uint64_t ref_sse = aom_sum_squares_i16(r_ref, N);
    uint64_t tst_sse = equiv_sse_from_residuals(r0, r1, m, N);

    ASSERT_EQ(ref_sse, tst_sse);
  }
}

static uint64_t sse_from_residuals(const int16_t *r0, const int16_t *r1,
                                   const uint8_t *m, int N) {
  uint64_t acc = 0;
  for (int i = 0; i < N; i++) {
    const int32_t m0 = m[i];
    const int32_t m1 = MAX_MASK_VALUE - m0;
    const int32_t r = m0 * r0[i] + m1 * r1[i];
    acc += r * r;
  }
  return ROUND_POWER_OF_TWO(acc, 2 * WEDGE_WEIGHT_BITS);
}

TEST_F(WedgeUtilsSSEFuncTest, ResidualBlendingMethod) {
  DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, int16_t, d[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, uint8_t, m[MAX_SB_SQUARE]);

  for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) {
    for (int i = 0; i < MAX_SB_SQUARE; ++i) {
      r1[i] = rng_(2 * INT8_MAX - 2 * INT8_MIN + 1) + 2 * INT8_MIN;
      d[i] = rng_(2 * INT8_MAX - 2 * INT8_MIN + 1) + 2 * INT8_MIN;
      m[i] = rng_(MAX_MASK_VALUE + 1);
    }

    const int N = 64 * (rng_(MAX_SB_SQUARE / 64) + 1);

    for (int i = 0; i < N; i++) r0[i] = r1[i] + d[i];

    const uint64_t ref_res = sse_from_residuals(r0, r1, m, N);
    const uint64_t tst_res = av1_wedge_sse_from_residuals(r1, d, m, N);

    ASSERT_EQ(ref_res, tst_res);
  }
}

//////////////////////////////////////////////////////////////////////////////
// av1_wedge_sse_from_residuals - optimizations
//////////////////////////////////////////////////////////////////////////////

typedef uint64_t (*FSSE)(const int16_t *r1, const int16_t *d, const uint8_t *m,
                         int N);
typedef libaom_test::FuncParam<FSSE> TestFuncsFSSE;

class WedgeUtilsSSEOptTest : public FunctionEquivalenceTest<FSSE> {
 protected:
  static const int kIterations = 10000;
};

TEST_P(WedgeUtilsSSEOptTest, RandomValues) {
  DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, int16_t, d[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, uint8_t, m[MAX_SB_SQUARE]);

  for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) {
    for (int i = 0; i < MAX_SB_SQUARE; ++i) {
      r1[i] = rng_(2 * kInt13Max + 1) - kInt13Max;
      d[i] = rng_(2 * kInt13Max + 1) - kInt13Max;
      m[i] = rng_(MAX_MASK_VALUE + 1);
    }

    const int N = 64 * (rng_(MAX_SB_SQUARE / 64) + 1);

    const uint64_t ref_res = params_.ref_func(r1, d, m, N);
    uint64_t tst_res;
    ASM_REGISTER_STATE_CHECK(tst_res = params_.tst_func(r1, d, m, N));

    ASSERT_EQ(ref_res, tst_res);
  }
}

TEST_P(WedgeUtilsSSEOptTest, ExtremeValues) {
  DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, int16_t, d[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, uint8_t, m[MAX_SB_SQUARE]);

  for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) {
    if (rng_(2)) {
      for (int i = 0; i < MAX_SB_SQUARE; ++i) r1[i] = kInt13Max;
    } else {
      for (int i = 0; i < MAX_SB_SQUARE; ++i) r1[i] = -kInt13Max;
    }

    if (rng_(2)) {
      for (int i = 0; i < MAX_SB_SQUARE; ++i) d[i] = kInt13Max;
    } else {
      for (int i = 0; i < MAX_SB_SQUARE; ++i) d[i] = -kInt13Max;
    }

    for (int i = 0; i < MAX_SB_SQUARE; ++i) m[i] = MAX_MASK_VALUE;

    const int N = 64 * (rng_(MAX_SB_SQUARE / 64) + 1);

    const uint64_t ref_res = params_.ref_func(r1, d, m, N);
    uint64_t tst_res;
    ASM_REGISTER_STATE_CHECK(tst_res = params_.tst_func(r1, d, m, N));

    ASSERT_EQ(ref_res, tst_res);
  }
}

#if HAVE_SSE2
INSTANTIATE_TEST_CASE_P(
    SSE2, WedgeUtilsSSEOptTest,
    ::testing::Values(TestFuncsFSSE(av1_wedge_sse_from_residuals_c,
                                    av1_wedge_sse_from_residuals_sse2)));

#endif  // HAVE_SSE2

//////////////////////////////////////////////////////////////////////////////
// av1_wedge_sign_from_residuals
//////////////////////////////////////////////////////////////////////////////

typedef int (*FSign)(const int16_t *ds, const uint8_t *m, int N, int64_t limit);
typedef libaom_test::FuncParam<FSign> TestFuncsFSign;

class WedgeUtilsSignOptTest : public FunctionEquivalenceTest<FSign> {
 protected:
  static const int kIterations = 10000;
  static const int kMaxSize = 8196;  // Size limited by SIMD implementation.
};

TEST_P(WedgeUtilsSignOptTest, RandomValues) {
  DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, int16_t, ds[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, uint8_t, m[MAX_SB_SQUARE]);

  for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) {
    for (int i = 0; i < MAX_SB_SQUARE; ++i) {
      r0[i] = rng_(2 * kInt13Max + 1) - kInt13Max;
      r1[i] = rng_(2 * kInt13Max + 1) - kInt13Max;
      m[i] = rng_(MAX_MASK_VALUE + 1);
    }

    const int maxN = AOMMIN(kMaxSize, MAX_SB_SQUARE);
    const int N = 64 * (rng_(maxN / 64 - 1) + 1);

    int64_t limit;
    limit = (int64_t)aom_sum_squares_i16(r0, N);
    limit -= (int64_t)aom_sum_squares_i16(r1, N);
    limit *= (1 << WEDGE_WEIGHT_BITS) / 2;

    for (int i = 0; i < N; i++)
      ds[i] = clamp(r0[i] * r0[i] - r1[i] * r1[i], INT16_MIN, INT16_MAX);

    const int ref_res = params_.ref_func(ds, m, N, limit);
    int tst_res;
    ASM_REGISTER_STATE_CHECK(tst_res = params_.tst_func(ds, m, N, limit));

    ASSERT_EQ(ref_res, tst_res);
  }
}

TEST_P(WedgeUtilsSignOptTest, ExtremeValues) {
  DECLARE_ALIGNED(32, int16_t, r0[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, int16_t, r1[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, int16_t, ds[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, uint8_t, m[MAX_SB_SQUARE]);

  for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) {
    switch (rng_(4)) {
      case 0:
        for (int i = 0; i < MAX_SB_SQUARE; ++i) {
          r0[i] = 0;
          r1[i] = kInt13Max;
        }
        break;
      case 1:
        for (int i = 0; i < MAX_SB_SQUARE; ++i) {
          r0[i] = kInt13Max;
          r1[i] = 0;
        }
        break;
      case 2:
        for (int i = 0; i < MAX_SB_SQUARE; ++i) {
          r0[i] = 0;
          r1[i] = -kInt13Max;
        }
        break;
      default:
        for (int i = 0; i < MAX_SB_SQUARE; ++i) {
          r0[i] = -kInt13Max;
          r1[i] = 0;
        }
        break;
    }

    for (int i = 0; i < MAX_SB_SQUARE; ++i) m[i] = MAX_MASK_VALUE;

    const int maxN = AOMMIN(kMaxSize, MAX_SB_SQUARE);
    const int N = 64 * (rng_(maxN / 64 - 1) + 1);

    int64_t limit;
    limit = (int64_t)aom_sum_squares_i16(r0, N);
    limit -= (int64_t)aom_sum_squares_i16(r1, N);
    limit *= (1 << WEDGE_WEIGHT_BITS) / 2;

    for (int i = 0; i < N; i++)
      ds[i] = clamp(r0[i] * r0[i] - r1[i] * r1[i], INT16_MIN, INT16_MAX);

    const int ref_res = params_.ref_func(ds, m, N, limit);
    int tst_res;
    ASM_REGISTER_STATE_CHECK(tst_res = params_.tst_func(ds, m, N, limit));

    ASSERT_EQ(ref_res, tst_res);
  }
}

#if HAVE_SSE2

INSTANTIATE_TEST_CASE_P(
    SSE2, WedgeUtilsSignOptTest,
    ::testing::Values(TestFuncsFSign(av1_wedge_sign_from_residuals_c,
                                     av1_wedge_sign_from_residuals_sse2)));

#endif  // HAVE_SSE2

//////////////////////////////////////////////////////////////////////////////
// av1_wedge_compute_delta_squares
//////////////////////////////////////////////////////////////////////////////

typedef void (*FDS)(int16_t *d, const int16_t *a, const int16_t *b, int N);
typedef libaom_test::FuncParam<FDS> TestFuncsFDS;

class WedgeUtilsDeltaSquaresOptTest : public FunctionEquivalenceTest<FDS> {
 protected:
  static const int kIterations = 10000;
};

TEST_P(WedgeUtilsDeltaSquaresOptTest, RandomValues) {
  DECLARE_ALIGNED(32, int16_t, a[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, int16_t, b[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, int16_t, d_ref[MAX_SB_SQUARE]);
  DECLARE_ALIGNED(32, int16_t, d_tst[MAX_SB_SQUARE]);

  for (int iter = 0; iter < kIterations && !HasFatalFailure(); ++iter) {
    for (int i = 0; i < MAX_SB_SQUARE; ++i) {
      a[i] = rng_.Rand16();
      b[i] = rng_(2 * INT16_MAX + 1) - INT16_MAX;
    }

    const int N = 64 * (rng_(MAX_SB_SQUARE / 64) + 1);

    memset(&d_ref, INT16_MAX, sizeof(d_ref));
    memset(&d_tst, INT16_MAX, sizeof(d_tst));

    params_.ref_func(d_ref, a, b, N);
    ASM_REGISTER_STATE_CHECK(params_.tst_func(d_tst, a, b, N));

    for (int i = 0; i < MAX_SB_SQUARE; ++i) ASSERT_EQ(d_ref[i], d_tst[i]);
  }
}

#if HAVE_SSE2

INSTANTIATE_TEST_CASE_P(
    SSE2, WedgeUtilsDeltaSquaresOptTest,
    ::testing::Values(TestFuncsFDS(av1_wedge_compute_delta_squares_c,
                                   av1_wedge_compute_delta_squares_sse2)));

#endif  // HAVE_SSE2

}  // namespace