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 <stdlib.h>
12 #include <time.h>
13
14 #include "../unit_test/unit_test.h"
15 #include "libyuv/convert_argb.h"
16 #include "libyuv/cpu_id.h"
17 #include "libyuv/scale_argb.h"
18 #include "libyuv/video_common.h"
19
20 namespace libyuv {
21
22 #define STRINGIZE(line) #line
23 #define FILELINESTR(file, line) file ":" STRINGIZE(line)
24
25 // Test scaling with C vs Opt and return maximum pixel difference. 0 = exact.
ARGBTestFilter(int src_width,int src_height,int dst_width,int dst_height,FilterMode f,int benchmark_iterations,int disable_cpu_flags,int benchmark_cpu_info)26 static int ARGBTestFilter(int src_width,
27 int src_height,
28 int dst_width,
29 int dst_height,
30 FilterMode f,
31 int benchmark_iterations,
32 int disable_cpu_flags,
33 int benchmark_cpu_info) {
34 if (!SizeValid(src_width, src_height, dst_width, dst_height)) {
35 return 0;
36 }
37
38 int i, j;
39 const int b = 0; // 128 to test for padding/stride.
40 int64_t src_argb_plane_size =
41 (Abs(src_width) + b * 2) * (Abs(src_height) + b * 2) * 4LL;
42 int src_stride_argb = (b * 2 + Abs(src_width)) * 4;
43
44 align_buffer_page_end(src_argb, src_argb_plane_size);
45 if (!src_argb) {
46 printf("Skipped. Alloc failed " FILELINESTR(__FILE__, __LINE__) "\n");
47 return 0;
48 }
49 MemRandomize(src_argb, src_argb_plane_size);
50
51 int64_t dst_argb_plane_size =
52 (dst_width + b * 2) * (dst_height + b * 2) * 4LL;
53 int dst_stride_argb = (b * 2 + dst_width) * 4;
54
55 align_buffer_page_end(dst_argb_c, dst_argb_plane_size);
56 align_buffer_page_end(dst_argb_opt, dst_argb_plane_size);
57 if (!dst_argb_c || !dst_argb_opt) {
58 printf("Skipped. Alloc failed " FILELINESTR(__FILE__, __LINE__) "\n");
59 return 0;
60 }
61 memset(dst_argb_c, 2, dst_argb_plane_size);
62 memset(dst_argb_opt, 3, dst_argb_plane_size);
63
64 // Warm up both versions for consistent benchmarks.
65 MaskCpuFlags(disable_cpu_flags); // Disable all CPU optimization.
66 ARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
67 src_width, src_height, dst_argb_c + (dst_stride_argb * b) + b * 4,
68 dst_stride_argb, dst_width, dst_height, f);
69 MaskCpuFlags(benchmark_cpu_info); // Enable all CPU optimization.
70 ARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
71 src_width, src_height, dst_argb_opt + (dst_stride_argb * b) + b * 4,
72 dst_stride_argb, dst_width, dst_height, f);
73
74 MaskCpuFlags(disable_cpu_flags); // Disable all CPU optimization.
75 double c_time = get_time();
76 ARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
77 src_width, src_height, dst_argb_c + (dst_stride_argb * b) + b * 4,
78 dst_stride_argb, dst_width, dst_height, f);
79
80 c_time = (get_time() - c_time);
81
82 MaskCpuFlags(benchmark_cpu_info); // Enable all CPU optimization.
83 double opt_time = get_time();
84 for (i = 0; i < benchmark_iterations; ++i) {
85 ARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
86 src_width, src_height,
87 dst_argb_opt + (dst_stride_argb * b) + b * 4, dst_stride_argb,
88 dst_width, dst_height, f);
89 }
90 opt_time = (get_time() - opt_time) / benchmark_iterations;
91
92 // Report performance of C vs OPT
93 printf("filter %d - %8d us C - %8d us OPT\n", f,
94 static_cast<int>(c_time * 1e6), static_cast<int>(opt_time * 1e6));
95
96 // C version may be a little off from the optimized. Order of
97 // operations may introduce rounding somewhere. So do a difference
98 // of the buffers and look to see that the max difference isn't
99 // over 2.
100 int max_diff = 0;
101 for (i = b; i < (dst_height + b); ++i) {
102 for (j = b * 4; j < (dst_width + b) * 4; ++j) {
103 int abs_diff = Abs(dst_argb_c[(i * dst_stride_argb) + j] -
104 dst_argb_opt[(i * dst_stride_argb) + j]);
105 if (abs_diff > max_diff) {
106 max_diff = abs_diff;
107 }
108 }
109 }
110
111 free_aligned_buffer_page_end(dst_argb_c);
112 free_aligned_buffer_page_end(dst_argb_opt);
113 free_aligned_buffer_page_end(src_argb);
114 return max_diff;
115 }
116
117 static const int kTileX = 8;
118 static const int kTileY = 8;
119
TileARGBScale(const uint8_t * src_argb,int src_stride_argb,int src_width,int src_height,uint8_t * dst_argb,int dst_stride_argb,int dst_width,int dst_height,FilterMode filtering)120 static int TileARGBScale(const uint8_t* src_argb,
121 int src_stride_argb,
122 int src_width,
123 int src_height,
124 uint8_t* dst_argb,
125 int dst_stride_argb,
126 int dst_width,
127 int dst_height,
128 FilterMode filtering) {
129 for (int y = 0; y < dst_height; y += kTileY) {
130 for (int x = 0; x < dst_width; x += kTileX) {
131 int clip_width = kTileX;
132 if (x + clip_width > dst_width) {
133 clip_width = dst_width - x;
134 }
135 int clip_height = kTileY;
136 if (y + clip_height > dst_height) {
137 clip_height = dst_height - y;
138 }
139 int r = ARGBScaleClip(src_argb, src_stride_argb, src_width, src_height,
140 dst_argb, dst_stride_argb, dst_width, dst_height, x,
141 y, clip_width, clip_height, filtering);
142 if (r) {
143 return r;
144 }
145 }
146 }
147 return 0;
148 }
149
ARGBClipTestFilter(int src_width,int src_height,int dst_width,int dst_height,FilterMode f,int benchmark_iterations)150 static int ARGBClipTestFilter(int src_width,
151 int src_height,
152 int dst_width,
153 int dst_height,
154 FilterMode f,
155 int benchmark_iterations) {
156 if (!SizeValid(src_width, src_height, dst_width, dst_height)) {
157 return 0;
158 }
159
160 const int b = 128;
161 int64_t src_argb_plane_size =
162 (Abs(src_width) + b * 2) * (Abs(src_height) + b * 2) * 4;
163 int src_stride_argb = (b * 2 + Abs(src_width)) * 4;
164
165 align_buffer_page_end(src_argb, src_argb_plane_size);
166 if (!src_argb) {
167 printf("Skipped. Alloc failed " FILELINESTR(__FILE__, __LINE__) "\n");
168 return 0;
169 }
170 memset(src_argb, 1, src_argb_plane_size);
171
172 int64_t dst_argb_plane_size = (dst_width + b * 2) * (dst_height + b * 2) * 4;
173 int dst_stride_argb = (b * 2 + dst_width) * 4;
174
175 int i, j;
176 for (i = b; i < (Abs(src_height) + b); ++i) {
177 for (j = b; j < (Abs(src_width) + b) * 4; ++j) {
178 src_argb[(i * src_stride_argb) + j] = (fastrand() & 0xff);
179 }
180 }
181
182 align_buffer_page_end(dst_argb_c, dst_argb_plane_size);
183 align_buffer_page_end(dst_argb_opt, dst_argb_plane_size);
184 if (!dst_argb_c || !dst_argb_opt) {
185 printf("Skipped. Alloc failed " FILELINESTR(__FILE__, __LINE__) "\n");
186 return 0;
187 }
188 memset(dst_argb_c, 2, dst_argb_plane_size);
189 memset(dst_argb_opt, 3, dst_argb_plane_size);
190
191 // Do full image, no clipping.
192 double c_time = get_time();
193 ARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
194 src_width, src_height, dst_argb_c + (dst_stride_argb * b) + b * 4,
195 dst_stride_argb, dst_width, dst_height, f);
196 c_time = (get_time() - c_time);
197
198 // Do tiled image, clipping scale to a tile at a time.
199 double opt_time = get_time();
200 for (i = 0; i < benchmark_iterations; ++i) {
201 TileARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
202 src_width, src_height,
203 dst_argb_opt + (dst_stride_argb * b) + b * 4, dst_stride_argb,
204 dst_width, dst_height, f);
205 }
206 opt_time = (get_time() - opt_time) / benchmark_iterations;
207
208 // Report performance of Full vs Tiled.
209 printf("filter %d - %8d us Full - %8d us Tiled\n", f,
210 static_cast<int>(c_time * 1e6), static_cast<int>(opt_time * 1e6));
211
212 // Compare full scaled image vs tiled image.
213 int max_diff = 0;
214 for (i = b; i < (dst_height + b); ++i) {
215 for (j = b * 4; j < (dst_width + b) * 4; ++j) {
216 int abs_diff = Abs(dst_argb_c[(i * dst_stride_argb) + j] -
217 dst_argb_opt[(i * dst_stride_argb) + j]);
218 if (abs_diff > max_diff) {
219 max_diff = abs_diff;
220 }
221 }
222 }
223
224 free_aligned_buffer_page_end(dst_argb_c);
225 free_aligned_buffer_page_end(dst_argb_opt);
226 free_aligned_buffer_page_end(src_argb);
227 return max_diff;
228 }
229
230 // The following adjustments in dimensions ensure the scale factor will be
231 // exactly achieved.
232 #define DX(x, nom, denom) static_cast<int>((Abs(x) / nom) * nom)
233 #define SX(x, nom, denom) static_cast<int>((x / nom) * denom)
234
235 #define TEST_FACTOR1(name, filter, nom, denom, max_diff) \
236 TEST_F(LibYUVScaleTest, ARGBScaleDownBy##name##_##filter) { \
237 int diff = ARGBTestFilter( \
238 SX(benchmark_width_, nom, denom), SX(benchmark_height_, nom, denom), \
239 DX(benchmark_width_, nom, denom), DX(benchmark_height_, nom, denom), \
240 kFilter##filter, benchmark_iterations_, disable_cpu_flags_, \
241 benchmark_cpu_info_); \
242 EXPECT_LE(diff, max_diff); \
243 } \
244 TEST_F(LibYUVScaleTest, ARGBScaleDownClipBy##name##_##filter) { \
245 int diff = ARGBClipTestFilter( \
246 SX(benchmark_width_, nom, denom), SX(benchmark_height_, nom, denom), \
247 DX(benchmark_width_, nom, denom), DX(benchmark_height_, nom, denom), \
248 kFilter##filter, benchmark_iterations_); \
249 EXPECT_LE(diff, max_diff); \
250 }
251
252 // Test a scale factor with all 4 filters. Expect unfiltered to be exact, but
253 // filtering is different fixed point implementations for SSSE3, Neon and C.
254 #define TEST_FACTOR(name, nom, denom) \
255 TEST_FACTOR1(name, None, nom, denom, 0) \
256 TEST_FACTOR1(name, Linear, nom, denom, 3) \
257 TEST_FACTOR1(name, Bilinear, nom, denom, 3) \
258 TEST_FACTOR1(name, Box, nom, denom, 3)
259
260 TEST_FACTOR(2, 1, 2)
261 TEST_FACTOR(4, 1, 4)
262 TEST_FACTOR(8, 1, 8)
263 TEST_FACTOR(3by4, 3, 4)
264 TEST_FACTOR(3by8, 3, 8)
265 TEST_FACTOR(3, 1, 3)
266 #undef TEST_FACTOR1
267 #undef TEST_FACTOR
268 #undef SX
269 #undef DX
270
271 #define TEST_SCALETO1(name, width, height, filter, max_diff) \
272 TEST_F(LibYUVScaleTest, name##To##width##x##height##_##filter) { \
273 int diff = ARGBTestFilter(benchmark_width_, benchmark_height_, width, \
274 height, kFilter##filter, benchmark_iterations_, \
275 disable_cpu_flags_, benchmark_cpu_info_); \
276 EXPECT_LE(diff, max_diff); \
277 } \
278 TEST_F(LibYUVScaleTest, name##From##width##x##height##_##filter) { \
279 int diff = ARGBTestFilter(width, height, Abs(benchmark_width_), \
280 Abs(benchmark_height_), kFilter##filter, \
281 benchmark_iterations_, disable_cpu_flags_, \
282 benchmark_cpu_info_); \
283 EXPECT_LE(diff, max_diff); \
284 } \
285 TEST_F(LibYUVScaleTest, name##ClipTo##width##x##height##_##filter) { \
286 int diff = \
287 ARGBClipTestFilter(benchmark_width_, benchmark_height_, width, height, \
288 kFilter##filter, benchmark_iterations_); \
289 EXPECT_LE(diff, max_diff); \
290 } \
291 TEST_F(LibYUVScaleTest, name##ClipFrom##width##x##height##_##filter) { \
292 int diff = ARGBClipTestFilter(width, height, Abs(benchmark_width_), \
293 Abs(benchmark_height_), kFilter##filter, \
294 benchmark_iterations_); \
295 EXPECT_LE(diff, max_diff); \
296 }
297
298 /// Test scale to a specified size with all 4 filters.
299 #define TEST_SCALETO(name, width, height) \
300 TEST_SCALETO1(name, width, height, None, 0) \
301 TEST_SCALETO1(name, width, height, Linear, 3) \
302 TEST_SCALETO1(name, width, height, Bilinear, 3)
303
304 TEST_SCALETO(ARGBScale, 1, 1)
305 TEST_SCALETO(ARGBScale, 320, 240)
306 TEST_SCALETO(ARGBScale, 569, 480)
307 TEST_SCALETO(ARGBScale, 640, 360)
308 TEST_SCALETO(ARGBScale, 1280, 720)
309 TEST_SCALETO(ARGBScale, 1920, 1080)
310 #undef TEST_SCALETO1
311 #undef TEST_SCALETO
312
313 // Scale with YUV conversion to ARGB and clipping.
314 // TODO(fbarchard): Add fourcc support. All 4 ARGB formats is easy to support.
315 LIBYUV_API
YUVToARGBScaleReference2(const uint8_t * src_y,int src_stride_y,const uint8_t * src_u,int src_stride_u,const uint8_t * src_v,int src_stride_v,uint32_t,int src_width,int src_height,uint8_t * dst_argb,int dst_stride_argb,uint32_t,int dst_width,int dst_height,int clip_x,int clip_y,int clip_width,int clip_height,enum FilterMode filtering)316 int YUVToARGBScaleReference2(const uint8_t* src_y,
317 int src_stride_y,
318 const uint8_t* src_u,
319 int src_stride_u,
320 const uint8_t* src_v,
321 int src_stride_v,
322 uint32_t /* src_fourcc */,
323 int src_width,
324 int src_height,
325 uint8_t* dst_argb,
326 int dst_stride_argb,
327 uint32_t /* dst_fourcc */,
328 int dst_width,
329 int dst_height,
330 int clip_x,
331 int clip_y,
332 int clip_width,
333 int clip_height,
334 enum FilterMode filtering) {
335 uint8_t* argb_buffer =
336 static_cast<uint8_t*>(malloc(src_width * src_height * 4));
337 int r;
338 I420ToARGB(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
339 argb_buffer, src_width * 4, src_width, src_height);
340
341 r = ARGBScaleClip(argb_buffer, src_width * 4, src_width, src_height, dst_argb,
342 dst_stride_argb, dst_width, dst_height, clip_x, clip_y,
343 clip_width, clip_height, filtering);
344 free(argb_buffer);
345 return r;
346 }
347
FillRamp(uint8_t * buf,int width,int height,int v,int dx,int dy)348 static void FillRamp(uint8_t* buf,
349 int width,
350 int height,
351 int v,
352 int dx,
353 int dy) {
354 int rv = v;
355 for (int y = 0; y < height; ++y) {
356 for (int x = 0; x < width; ++x) {
357 *buf++ = v;
358 v += dx;
359 if (v < 0 || v > 255) {
360 dx = -dx;
361 v += dx;
362 }
363 }
364 v = rv + dy;
365 if (v < 0 || v > 255) {
366 dy = -dy;
367 v += dy;
368 }
369 rv = v;
370 }
371 }
372
373 // Test scaling with C vs Opt and return maximum pixel difference. 0 = exact.
YUVToARGBTestFilter(int src_width,int src_height,int dst_width,int dst_height,FilterMode f,int benchmark_iterations)374 static int YUVToARGBTestFilter(int src_width,
375 int src_height,
376 int dst_width,
377 int dst_height,
378 FilterMode f,
379 int benchmark_iterations) {
380 int64_t src_y_plane_size = Abs(src_width) * Abs(src_height);
381 int64_t src_uv_plane_size =
382 ((Abs(src_width) + 1) / 2) * ((Abs(src_height) + 1) / 2);
383 int src_stride_y = Abs(src_width);
384 int src_stride_uv = (Abs(src_width) + 1) / 2;
385
386 align_buffer_page_end(src_y, src_y_plane_size);
387 align_buffer_page_end(src_u, src_uv_plane_size);
388 align_buffer_page_end(src_v, src_uv_plane_size);
389
390 int64_t dst_argb_plane_size = (dst_width) * (dst_height)*4LL;
391 int dst_stride_argb = (dst_width)*4;
392 align_buffer_page_end(dst_argb_c, dst_argb_plane_size);
393 align_buffer_page_end(dst_argb_opt, dst_argb_plane_size);
394 if (!dst_argb_c || !dst_argb_opt || !src_y || !src_u || !src_v) {
395 printf("Skipped. Alloc failed " FILELINESTR(__FILE__, __LINE__) "\n");
396 return 0;
397 }
398 // Fill YUV image with continuous ramp, which is less sensitive to
399 // subsampling and filtering differences for test purposes.
400 FillRamp(src_y, Abs(src_width), Abs(src_height), 128, 1, 1);
401 FillRamp(src_u, (Abs(src_width) + 1) / 2, (Abs(src_height) + 1) / 2, 3, 1, 1);
402 FillRamp(src_v, (Abs(src_width) + 1) / 2, (Abs(src_height) + 1) / 2, 4, 1, 1);
403 memset(dst_argb_c, 2, dst_argb_plane_size);
404 memset(dst_argb_opt, 3, dst_argb_plane_size);
405
406 YUVToARGBScaleReference2(src_y, src_stride_y, src_u, src_stride_uv, src_v,
407 src_stride_uv, libyuv::FOURCC_I420, src_width,
408 src_height, dst_argb_c, dst_stride_argb,
409 libyuv::FOURCC_I420, dst_width, dst_height, 0, 0,
410 dst_width, dst_height, f);
411
412 for (int i = 0; i < benchmark_iterations; ++i) {
413 YUVToARGBScaleClip(src_y, src_stride_y, src_u, src_stride_uv, src_v,
414 src_stride_uv, libyuv::FOURCC_I420, src_width,
415 src_height, dst_argb_opt, dst_stride_argb,
416 libyuv::FOURCC_I420, dst_width, dst_height, 0, 0,
417 dst_width, dst_height, f);
418 }
419 int max_diff = 0;
420 for (int i = 0; i < dst_height; ++i) {
421 for (int j = 0; j < dst_width * 4; ++j) {
422 int abs_diff = Abs(dst_argb_c[(i * dst_stride_argb) + j] -
423 dst_argb_opt[(i * dst_stride_argb) + j]);
424 if (abs_diff > max_diff) {
425 printf("error %d at %d,%d c %d opt %d", abs_diff, j, i,
426 dst_argb_c[(i * dst_stride_argb) + j],
427 dst_argb_opt[(i * dst_stride_argb) + j]);
428 EXPECT_LE(abs_diff, 40);
429 max_diff = abs_diff;
430 }
431 }
432 }
433
434 free_aligned_buffer_page_end(dst_argb_c);
435 free_aligned_buffer_page_end(dst_argb_opt);
436 free_aligned_buffer_page_end(src_y);
437 free_aligned_buffer_page_end(src_u);
438 free_aligned_buffer_page_end(src_v);
439 return max_diff;
440 }
441
TEST_F(LibYUVScaleTest,YUVToRGBScaleUp)442 TEST_F(LibYUVScaleTest, YUVToRGBScaleUp) {
443 int diff =
444 YUVToARGBTestFilter(benchmark_width_, benchmark_height_,
445 benchmark_width_ * 3 / 2, benchmark_height_ * 3 / 2,
446 libyuv::kFilterBilinear, benchmark_iterations_);
447 EXPECT_LE(diff, 10);
448 }
449
TEST_F(LibYUVScaleTest,YUVToRGBScaleDown)450 TEST_F(LibYUVScaleTest, YUVToRGBScaleDown) {
451 int diff = YUVToARGBTestFilter(
452 benchmark_width_ * 3 / 2, benchmark_height_ * 3 / 2, benchmark_width_,
453 benchmark_height_, libyuv::kFilterBilinear, benchmark_iterations_);
454 EXPECT_LE(diff, 10);
455 }
456
457 } // namespace libyuv
458