1 /*
2  *  Copyright 2011 The LibYuv Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS. All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "../unit_test/unit_test.h"
12 
13 #include <stdlib.h>  // For getenv()
14 
15 #include <cstring>
16 
17 #ifdef LIBYUV_USE_GFLAGS
18 #include "gflags/gflags.h"
19 #endif
20 #include "libyuv/cpu_id.h"
21 
22 unsigned int fastrand_seed = 0xfb;
23 
24 #ifdef LIBYUV_USE_GFLAGS
25 DEFINE_int32(libyuv_width, 0, "width of test image.");
26 DEFINE_int32(libyuv_height, 0, "height of test image.");
27 DEFINE_int32(libyuv_repeat, 0, "number of times to repeat test.");
28 DEFINE_int32(libyuv_flags, 0, "cpu flags for reference code. 1 = C, -1 = SIMD");
29 DEFINE_int32(libyuv_cpu_info,
30              0,
31              "cpu flags for benchmark code. 1 = C, -1 = SIMD");
32 #else
33 // Disable command line parameters if gflags disabled.
34 static const int32_t FLAGS_libyuv_width = 0;
35 static const int32_t FLAGS_libyuv_height = 0;
36 static const int32_t FLAGS_libyuv_repeat = 0;
37 static const int32_t FLAGS_libyuv_flags = 0;
38 static const int32_t FLAGS_libyuv_cpu_info = 0;
39 #endif
40 
41 // For quicker unittests, default is 128 x 72.  But when benchmarking,
42 // default to 720p.  Allow size to specify.
43 // Set flags to -1 for benchmarking to avoid slower C code.
44 
LibYUVConvertTest()45 LibYUVConvertTest::LibYUVConvertTest()
46     : benchmark_iterations_(1),
47       benchmark_width_(128),
48       benchmark_height_(72),
49       disable_cpu_flags_(1),
50       benchmark_cpu_info_(-1) {
51   const char* repeat = getenv("LIBYUV_REPEAT");
52   if (repeat) {
53     benchmark_iterations_ = atoi(repeat);  // NOLINT
54   }
55   if (FLAGS_libyuv_repeat) {
56     benchmark_iterations_ = FLAGS_libyuv_repeat;
57   }
58   if (benchmark_iterations_ > 1) {
59     benchmark_width_ = 1280;
60     benchmark_height_ = 720;
61   }
62   const char* width = getenv("LIBYUV_WIDTH");
63   if (width) {
64     benchmark_width_ = atoi(width);  // NOLINT
65   }
66   if (FLAGS_libyuv_width) {
67     benchmark_width_ = FLAGS_libyuv_width;
68   }
69   const char* height = getenv("LIBYUV_HEIGHT");
70   if (height) {
71     benchmark_height_ = atoi(height);  // NOLINT
72   }
73   if (FLAGS_libyuv_height) {
74     benchmark_height_ = FLAGS_libyuv_height;
75   }
76   const char* cpu_flags = getenv("LIBYUV_FLAGS");
77   if (cpu_flags) {
78     disable_cpu_flags_ = atoi(cpu_flags);  // NOLINT
79   }
80   if (FLAGS_libyuv_flags) {
81     disable_cpu_flags_ = FLAGS_libyuv_flags;
82   }
83   const char* cpu_info = getenv("LIBYUV_CPU_INFO");
84   if (cpu_info) {
85     benchmark_cpu_info_ = atoi(cpu_flags);  // NOLINT
86   }
87   if (FLAGS_libyuv_cpu_info) {
88     benchmark_cpu_info_ = FLAGS_libyuv_cpu_info;
89   }
90   libyuv::MaskCpuFlags(benchmark_cpu_info_);
91   benchmark_pixels_div1280_ =
92       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
93                             static_cast<double>(Abs(benchmark_height_)) *
94                             static_cast<double>(benchmark_iterations_) +
95                         1279.0) /
96                        1280.0);
97 }
98 
LibYUVColorTest()99 LibYUVColorTest::LibYUVColorTest()
100     : benchmark_iterations_(1),
101       benchmark_width_(128),
102       benchmark_height_(72),
103       disable_cpu_flags_(1),
104       benchmark_cpu_info_(-1) {
105   const char* repeat = getenv("LIBYUV_REPEAT");
106   if (repeat) {
107     benchmark_iterations_ = atoi(repeat);  // NOLINT
108   }
109   if (FLAGS_libyuv_repeat) {
110     benchmark_iterations_ = FLAGS_libyuv_repeat;
111   }
112   if (benchmark_iterations_ > 1) {
113     benchmark_width_ = 1280;
114     benchmark_height_ = 720;
115   }
116   const char* width = getenv("LIBYUV_WIDTH");
117   if (width) {
118     benchmark_width_ = atoi(width);  // NOLINT
119   }
120   if (FLAGS_libyuv_width) {
121     benchmark_width_ = FLAGS_libyuv_width;
122   }
123   const char* height = getenv("LIBYUV_HEIGHT");
124   if (height) {
125     benchmark_height_ = atoi(height);  // NOLINT
126   }
127   if (FLAGS_libyuv_height) {
128     benchmark_height_ = FLAGS_libyuv_height;
129   }
130   const char* cpu_flags = getenv("LIBYUV_FLAGS");
131   if (cpu_flags) {
132     disable_cpu_flags_ = atoi(cpu_flags);  // NOLINT
133   }
134   if (FLAGS_libyuv_flags) {
135     disable_cpu_flags_ = FLAGS_libyuv_flags;
136   }
137   const char* cpu_info = getenv("LIBYUV_CPU_INFO");
138   if (cpu_info) {
139     benchmark_cpu_info_ = atoi(cpu_flags);  // NOLINT
140   }
141   if (FLAGS_libyuv_cpu_info) {
142     benchmark_cpu_info_ = FLAGS_libyuv_cpu_info;
143   }
144   libyuv::MaskCpuFlags(benchmark_cpu_info_);
145   benchmark_pixels_div1280_ =
146       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
147                             static_cast<double>(Abs(benchmark_height_)) *
148                             static_cast<double>(benchmark_iterations_) +
149                         1279.0) /
150                        1280.0);
151 }
152 
LibYUVScaleTest()153 LibYUVScaleTest::LibYUVScaleTest()
154     : benchmark_iterations_(1),
155       benchmark_width_(128),
156       benchmark_height_(72),
157       disable_cpu_flags_(1),
158       benchmark_cpu_info_(-1) {
159   const char* repeat = getenv("LIBYUV_REPEAT");
160   if (repeat) {
161     benchmark_iterations_ = atoi(repeat);  // NOLINT
162   }
163   if (FLAGS_libyuv_repeat) {
164     benchmark_iterations_ = FLAGS_libyuv_repeat;
165   }
166   if (benchmark_iterations_ > 1) {
167     benchmark_width_ = 1280;
168     benchmark_height_ = 720;
169   }
170   const char* width = getenv("LIBYUV_WIDTH");
171   if (width) {
172     benchmark_width_ = atoi(width);  // NOLINT
173   }
174   if (FLAGS_libyuv_width) {
175     benchmark_width_ = FLAGS_libyuv_width;
176   }
177   const char* height = getenv("LIBYUV_HEIGHT");
178   if (height) {
179     benchmark_height_ = atoi(height);  // NOLINT
180   }
181   if (FLAGS_libyuv_height) {
182     benchmark_height_ = FLAGS_libyuv_height;
183   }
184   const char* cpu_flags = getenv("LIBYUV_FLAGS");
185   if (cpu_flags) {
186     disable_cpu_flags_ = atoi(cpu_flags);  // NOLINT
187   }
188   if (FLAGS_libyuv_flags) {
189     disable_cpu_flags_ = FLAGS_libyuv_flags;
190   }
191   const char* cpu_info = getenv("LIBYUV_CPU_INFO");
192   if (cpu_info) {
193     benchmark_cpu_info_ = atoi(cpu_flags);  // NOLINT
194   }
195   if (FLAGS_libyuv_cpu_info) {
196     benchmark_cpu_info_ = FLAGS_libyuv_cpu_info;
197   }
198   libyuv::MaskCpuFlags(benchmark_cpu_info_);
199   benchmark_pixels_div1280_ =
200       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
201                             static_cast<double>(Abs(benchmark_height_)) *
202                             static_cast<double>(benchmark_iterations_) +
203                         1279.0) /
204                        1280.0);
205 }
206 
LibYUVRotateTest()207 LibYUVRotateTest::LibYUVRotateTest()
208     : benchmark_iterations_(1),
209       benchmark_width_(128),
210       benchmark_height_(72),
211       disable_cpu_flags_(1),
212       benchmark_cpu_info_(-1) {
213   const char* repeat = getenv("LIBYUV_REPEAT");
214   if (repeat) {
215     benchmark_iterations_ = atoi(repeat);  // NOLINT
216   }
217   if (FLAGS_libyuv_repeat) {
218     benchmark_iterations_ = FLAGS_libyuv_repeat;
219   }
220   if (benchmark_iterations_ > 1) {
221     benchmark_width_ = 1280;
222     benchmark_height_ = 720;
223   }
224   const char* width = getenv("LIBYUV_WIDTH");
225   if (width) {
226     benchmark_width_ = atoi(width);  // NOLINT
227   }
228   if (FLAGS_libyuv_width) {
229     benchmark_width_ = FLAGS_libyuv_width;
230   }
231   const char* height = getenv("LIBYUV_HEIGHT");
232   if (height) {
233     benchmark_height_ = atoi(height);  // NOLINT
234   }
235   if (FLAGS_libyuv_height) {
236     benchmark_height_ = FLAGS_libyuv_height;
237   }
238   const char* cpu_flags = getenv("LIBYUV_FLAGS");
239   if (cpu_flags) {
240     disable_cpu_flags_ = atoi(cpu_flags);  // NOLINT
241   }
242   if (FLAGS_libyuv_flags) {
243     disable_cpu_flags_ = FLAGS_libyuv_flags;
244   }
245   const char* cpu_info = getenv("LIBYUV_CPU_INFO");
246   if (cpu_info) {
247     benchmark_cpu_info_ = atoi(cpu_flags);  // NOLINT
248   }
249   if (FLAGS_libyuv_cpu_info) {
250     benchmark_cpu_info_ = FLAGS_libyuv_cpu_info;
251   }
252   libyuv::MaskCpuFlags(benchmark_cpu_info_);
253   benchmark_pixels_div1280_ =
254       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
255                             static_cast<double>(Abs(benchmark_height_)) *
256                             static_cast<double>(benchmark_iterations_) +
257                         1279.0) /
258                        1280.0);
259 }
260 
LibYUVPlanarTest()261 LibYUVPlanarTest::LibYUVPlanarTest()
262     : benchmark_iterations_(1),
263       benchmark_width_(128),
264       benchmark_height_(72),
265       disable_cpu_flags_(1),
266       benchmark_cpu_info_(-1) {
267   const char* repeat = getenv("LIBYUV_REPEAT");
268   if (repeat) {
269     benchmark_iterations_ = atoi(repeat);  // NOLINT
270   }
271   if (FLAGS_libyuv_repeat) {
272     benchmark_iterations_ = FLAGS_libyuv_repeat;
273   }
274   if (benchmark_iterations_ > 1) {
275     benchmark_width_ = 1280;
276     benchmark_height_ = 720;
277   }
278   const char* width = getenv("LIBYUV_WIDTH");
279   if (width) {
280     benchmark_width_ = atoi(width);  // NOLINT
281   }
282   if (FLAGS_libyuv_width) {
283     benchmark_width_ = FLAGS_libyuv_width;
284   }
285   const char* height = getenv("LIBYUV_HEIGHT");
286   if (height) {
287     benchmark_height_ = atoi(height);  // NOLINT
288   }
289   if (FLAGS_libyuv_height) {
290     benchmark_height_ = FLAGS_libyuv_height;
291   }
292   const char* cpu_flags = getenv("LIBYUV_FLAGS");
293   if (cpu_flags) {
294     disable_cpu_flags_ = atoi(cpu_flags);  // NOLINT
295   }
296   if (FLAGS_libyuv_flags) {
297     disable_cpu_flags_ = FLAGS_libyuv_flags;
298   }
299   const char* cpu_info = getenv("LIBYUV_CPU_INFO");
300   if (cpu_info) {
301     benchmark_cpu_info_ = atoi(cpu_flags);  // NOLINT
302   }
303   if (FLAGS_libyuv_cpu_info) {
304     benchmark_cpu_info_ = FLAGS_libyuv_cpu_info;
305   }
306   libyuv::MaskCpuFlags(benchmark_cpu_info_);
307   benchmark_pixels_div1280_ =
308       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
309                             static_cast<double>(Abs(benchmark_height_)) *
310                             static_cast<double>(benchmark_iterations_) +
311                         1279.0) /
312                        1280.0);
313 }
314 
LibYUVBaseTest()315 LibYUVBaseTest::LibYUVBaseTest()
316     : benchmark_iterations_(1),
317       benchmark_width_(128),
318       benchmark_height_(72),
319       disable_cpu_flags_(1),
320       benchmark_cpu_info_(-1) {
321   const char* repeat = getenv("LIBYUV_REPEAT");
322   if (repeat) {
323     benchmark_iterations_ = atoi(repeat);  // NOLINT
324   }
325   if (FLAGS_libyuv_repeat) {
326     benchmark_iterations_ = FLAGS_libyuv_repeat;
327   }
328   if (benchmark_iterations_ > 1) {
329     benchmark_width_ = 1280;
330     benchmark_height_ = 720;
331   }
332   const char* width = getenv("LIBYUV_WIDTH");
333   if (width) {
334     benchmark_width_ = atoi(width);  // NOLINT
335   }
336   if (FLAGS_libyuv_width) {
337     benchmark_width_ = FLAGS_libyuv_width;
338   }
339   const char* height = getenv("LIBYUV_HEIGHT");
340   if (height) {
341     benchmark_height_ = atoi(height);  // NOLINT
342   }
343   if (FLAGS_libyuv_height) {
344     benchmark_height_ = FLAGS_libyuv_height;
345   }
346   const char* cpu_flags = getenv("LIBYUV_FLAGS");
347   if (cpu_flags) {
348     disable_cpu_flags_ = atoi(cpu_flags);  // NOLINT
349   }
350   if (FLAGS_libyuv_flags) {
351     disable_cpu_flags_ = FLAGS_libyuv_flags;
352   }
353   const char* cpu_info = getenv("LIBYUV_CPU_INFO");
354   if (cpu_info) {
355     benchmark_cpu_info_ = atoi(cpu_flags);  // NOLINT
356   }
357   if (FLAGS_libyuv_cpu_info) {
358     benchmark_cpu_info_ = FLAGS_libyuv_cpu_info;
359   }
360   libyuv::MaskCpuFlags(benchmark_cpu_info_);
361   benchmark_pixels_div1280_ =
362       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
363                             static_cast<double>(Abs(benchmark_height_)) *
364                             static_cast<double>(benchmark_iterations_) +
365                         1279.0) /
366                        1280.0);
367 }
368 
LibYUVCompareTest()369 LibYUVCompareTest::LibYUVCompareTest()
370     : benchmark_iterations_(1),
371       benchmark_width_(128),
372       benchmark_height_(72),
373       disable_cpu_flags_(1),
374       benchmark_cpu_info_(-1) {
375   const char* repeat = getenv("LIBYUV_REPEAT");
376   if (repeat) {
377     benchmark_iterations_ = atoi(repeat);  // NOLINT
378   }
379   if (FLAGS_libyuv_repeat) {
380     benchmark_iterations_ = FLAGS_libyuv_repeat;
381   }
382   if (benchmark_iterations_ > 1) {
383     benchmark_width_ = 1280;
384     benchmark_height_ = 720;
385   }
386   const char* width = getenv("LIBYUV_WIDTH");
387   if (width) {
388     benchmark_width_ = atoi(width);  // NOLINT
389   }
390   if (FLAGS_libyuv_width) {
391     benchmark_width_ = FLAGS_libyuv_width;
392   }
393   const char* height = getenv("LIBYUV_HEIGHT");
394   if (height) {
395     benchmark_height_ = atoi(height);  // NOLINT
396   }
397   if (FLAGS_libyuv_height) {
398     benchmark_height_ = FLAGS_libyuv_height;
399   }
400   const char* cpu_flags = getenv("LIBYUV_FLAGS");
401   if (cpu_flags) {
402     disable_cpu_flags_ = atoi(cpu_flags);  // NOLINT
403   }
404   if (FLAGS_libyuv_flags) {
405     disable_cpu_flags_ = FLAGS_libyuv_flags;
406   }
407   const char* cpu_info = getenv("LIBYUV_CPU_INFO");
408   if (cpu_info) {
409     benchmark_cpu_info_ = atoi(cpu_flags);  // NOLINT
410   }
411   if (FLAGS_libyuv_cpu_info) {
412     benchmark_cpu_info_ = FLAGS_libyuv_cpu_info;
413   }
414   libyuv::MaskCpuFlags(benchmark_cpu_info_);
415   benchmark_pixels_div1280_ =
416       static_cast<int>((static_cast<double>(Abs(benchmark_width_)) *
417                             static_cast<double>(Abs(benchmark_height_)) *
418                             static_cast<double>(benchmark_iterations_) +
419                         1279.0) /
420                        1280.0);
421 }
422 
main(int argc,char ** argv)423 int main(int argc, char** argv) {
424   ::testing::InitGoogleTest(&argc, argv);
425 #ifdef LIBYUV_USE_GFLAGS
426   // AllowCommandLineParsing allows us to ignore flags passed on to us by
427   // Chromium build bots without having to explicitly disable them.
428   google::AllowCommandLineReparsing();
429   google::ParseCommandLineFlags(&argc, &argv, true);
430 #endif
431   return RUN_ALL_TESTS();
432 }
433