aboutsummaryrefslogtreecommitdiff
path: root/third_party/libyuv/unit_test/unit_test.h
blob: 87907fa1603922f7f518e94719973e463cd1f8ee (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
/*
 *  Copyright 2011 The LibYuv Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS. All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef UNIT_TEST_UNIT_TEST_H_  // NOLINT
#define UNIT_TEST_UNIT_TEST_H_

#ifdef WIN32
#include <windows.h>
#else
#include <sys/resource.h>
#include <sys/time.h>
#endif

#include <gtest/gtest.h>

#include "libyuv/basic_types.h"

#ifndef SIMD_ALIGNED
#if defined(_MSC_VER) && !defined(__CLR_VER)
#define SIMD_ALIGNED(var) __declspec(align(16)) var
#elif defined(__GNUC__) && !defined(__pnacl__)
#define SIMD_ALIGNED(var) var __attribute__((aligned(16)))
#else
#define SIMD_ALIGNED(var) var
#endif
#endif

static __inline int Abs(int v) {
  return v >= 0 ? v : -v;
}

static __inline float FAbs(float v) {
  return v >= 0 ? v : -v;
}
#define OFFBY 0

// Scaling uses 16.16 fixed point to step thru the source image, so a
// maximum size of 32767.999 can be expressed.  32768 is valid because
// the step is 1 beyond the image but not used.
// Destination size is mainly constrained by valid scale step not the
// absolute size, so it may be possible to relax the destination size
// constraint.
// Source size is unconstrained for most specialized scalers.  e.g.
// An image of 65536 scaled to half size would be valid.  The test
// could be relaxed for special scale factors.
// If this test is removed, the scaling function should gracefully
// fail with a return code.  The test could be changed to know that
// libyuv failed in a controlled way.

static const int kMaxWidth = 32768;
static const int kMaxHeight = 32768;

static inline bool SizeValid(int src_width,
                             int src_height,
                             int dst_width,
                             int dst_height) {
  if (src_width > kMaxWidth || src_height > kMaxHeight ||
      dst_width > kMaxWidth || dst_height > kMaxHeight) {
    printf("Warning - size too large to test.  Skipping\n");
    return false;
  }
  return true;
}

#define align_buffer_page_end(var, size)                                \
  uint8_t* var##_mem =                                                  \
      reinterpret_cast<uint8_t*>(malloc(((size) + 4095 + 63) & ~4095)); \
  uint8_t* var = reinterpret_cast<uint8_t*>(                            \
      (intptr_t)(var##_mem + (((size) + 4095 + 63) & ~4095) - (size)) & ~63)

#define free_aligned_buffer_page_end(var) \
  free(var##_mem);                        \
  var = 0

#ifdef WIN32
static inline double get_time() {
  LARGE_INTEGER t, f;
  QueryPerformanceCounter(&t);
  QueryPerformanceFrequency(&f);
  return static_cast<double>(t.QuadPart) / static_cast<double>(f.QuadPart);
}
#else
static inline double get_time() {
  struct timeval t;
  struct timezone tzp;
  gettimeofday(&t, &tzp);
  return t.tv_sec + t.tv_usec * 1e-6;
}
#endif

#ifndef SIMD_ALIGNED
#if defined(_MSC_VER) && !defined(__CLR_VER)
#define SIMD_ALIGNED(var) __declspec(align(16)) var
#elif defined(__GNUC__) && !defined(__pnacl__)
#define SIMD_ALIGNED(var) var __attribute__((aligned(16)))
#else
#define SIMD_ALIGNED(var) var
#endif
#endif

extern unsigned int fastrand_seed;
inline int fastrand() {
  fastrand_seed = fastrand_seed * 214013u + 2531011u;
  return static_cast<int>((fastrand_seed >> 16) & 0xffff);
}

static inline void MemRandomize(uint8_t* dst, int64_t len) {
  int64_t i;
  for (i = 0; i < len - 1; i += 2) {
    *reinterpret_cast<uint16_t*>(dst) = fastrand();
    dst += 2;
  }
  for (; i < len; ++i) {
    *dst++ = fastrand();
  }
}

class LibYUVColorTest : public ::testing::Test {
 protected:
  LibYUVColorTest();

  int benchmark_iterations_;  // Default 1. Use 1000 for benchmarking.
  int benchmark_width_;       // Default 1280.  Use 640 for benchmarking VGA.
  int benchmark_height_;      // Default 720.  Use 360 for benchmarking VGA.
  int benchmark_pixels_div1280_;  // Total pixels to benchmark / 1280.
  int disable_cpu_flags_;         // Default 1.  Use -1 for benchmarking.
  int benchmark_cpu_info_;        // Default -1.  Use 1 to disable SIMD.
};

class LibYUVConvertTest : public ::testing::Test {
 protected:
  LibYUVConvertTest();

  int benchmark_iterations_;  // Default 1. Use 1000 for benchmarking.
  int benchmark_width_;       // Default 1280.  Use 640 for benchmarking VGA.
  int benchmark_height_;      // Default 720.  Use 360 for benchmarking VGA.
  int benchmark_pixels_div1280_;  // Total pixels to benchmark / 1280.
  int disable_cpu_flags_;         // Default 1.  Use -1 for benchmarking.
  int benchmark_cpu_info_;        // Default -1.  Use 1 to disable SIMD.
};

class LibYUVScaleTest : public ::testing::Test {
 protected:
  LibYUVScaleTest();

  int benchmark_iterations_;  // Default 1. Use 1000 for benchmarking.
  int benchmark_width_;       // Default 1280.  Use 640 for benchmarking VGA.
  int benchmark_height_;      // Default 720.  Use 360 for benchmarking VGA.
  int benchmark_pixels_div1280_;  // Total pixels to benchmark / 1280.
  int disable_cpu_flags_;         // Default 1.  Use -1 for benchmarking.
  int benchmark_cpu_info_;        // Default -1.  Use 1 to disable SIMD.
};

class LibYUVRotateTest : public ::testing::Test {
 protected:
  LibYUVRotateTest();

  int benchmark_iterations_;  // Default 1. Use 1000 for benchmarking.
  int benchmark_width_;       // Default 1280.  Use 640 for benchmarking VGA.
  int benchmark_height_;      // Default 720.  Use 360 for benchmarking VGA.
  int benchmark_pixels_div1280_;  // Total pixels to benchmark / 1280.
  int disable_cpu_flags_;         // Default 1.  Use -1 for benchmarking.
  int benchmark_cpu_info_;        // Default -1.  Use 1 to disable SIMD.
};

class LibYUVPlanarTest : public ::testing::Test {
 protected:
  LibYUVPlanarTest();

  int benchmark_iterations_;  // Default 1. Use 1000 for benchmarking.
  int benchmark_width_;       // Default 1280.  Use 640 for benchmarking VGA.
  int benchmark_height_;      // Default 720.  Use 360 for benchmarking VGA.
  int benchmark_pixels_div1280_;  // Total pixels to benchmark / 1280.
  int disable_cpu_flags_;         // Default 1.  Use -1 for benchmarking.
  int benchmark_cpu_info_;        // Default -1.  Use 1 to disable SIMD.
};

class LibYUVBaseTest : public ::testing::Test {
 protected:
  LibYUVBaseTest();

  int benchmark_iterations_;  // Default 1. Use 1000 for benchmarking.
  int benchmark_width_;       // Default 1280.  Use 640 for benchmarking VGA.
  int benchmark_height_;      // Default 720.  Use 360 for benchmarking VGA.
  int benchmark_pixels_div1280_;  // Total pixels to benchmark / 1280.
  int disable_cpu_flags_;         // Default 1.  Use -1 for benchmarking.
  int benchmark_cpu_info_;        // Default -1.  Use 1 to disable SIMD.
};

class LibYUVCompareTest : public ::testing::Test {
 protected:
  LibYUVCompareTest();

  int benchmark_iterations_;  // Default 1. Use 1000 for benchmarking.
  int benchmark_width_;       // Default 1280.  Use 640 for benchmarking VGA.
  int benchmark_height_;      // Default 720.  Use 360 for benchmarking VGA.
  int benchmark_pixels_div1280_;  // Total pixels to benchmark / 1280.
  int disable_cpu_flags_;         // Default 1.  Use -1 for benchmarking.
  int benchmark_cpu_info_;        // Default -1.  Use 1 to disable SIMD.
};

#endif  // UNIT_TEST_UNIT_TEST_H_  NOLINT