• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/cpu_id.h"
16 #include "libyuv/scale_uv.h"
17 
18 namespace libyuv {
19 
20 #define STRINGIZE(line) #line
21 #define FILELINESTR(file, line) file ":" STRINGIZE(line)
22 
23 #if !defined(DISABLE_SLOW_TESTS) || defined(__x86_64__) || defined(__i386__)
24 // SLOW TESTS are those that are unoptimized C code.
25 // FULL TESTS are optimized but test many variations of the same code.
26 #define ENABLE_FULL_TESTS
27 #endif
28 
29 // Test scaling with C vs Opt and return maximum pixel difference. 0 = exact.
UVTestFilter(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)30 static int UVTestFilter(int src_width,
31                         int src_height,
32                         int dst_width,
33                         int dst_height,
34                         FilterMode f,
35                         int benchmark_iterations,
36                         int disable_cpu_flags,
37                         int benchmark_cpu_info) {
38   if (!SizeValid(src_width, src_height, dst_width, dst_height)) {
39     return 0;
40   }
41 
42   int i, j;
43   const int b = 0;  // 128 to test for padding/stride.
44   int64_t src_uv_plane_size =
45       (Abs(src_width) + b * 2) * (Abs(src_height) + b * 2) * 2LL;
46   int src_stride_uv = (b * 2 + Abs(src_width)) * 2;
47 
48   align_buffer_page_end(src_uv, src_uv_plane_size);
49   if (!src_uv) {
50     printf("Skipped.  Alloc failed " FILELINESTR(__FILE__, __LINE__) "\n");
51     return 0;
52   }
53   MemRandomize(src_uv, src_uv_plane_size);
54 
55   int64_t dst_uv_plane_size = (dst_width + b * 2) * (dst_height + b * 2) * 2LL;
56   int dst_stride_uv = (b * 2 + dst_width) * 2;
57 
58   align_buffer_page_end(dst_uv_c, dst_uv_plane_size);
59   align_buffer_page_end(dst_uv_opt, dst_uv_plane_size);
60   if (!dst_uv_c || !dst_uv_opt) {
61     printf("Skipped.  Alloc failed " FILELINESTR(__FILE__, __LINE__) "\n");
62     return 0;
63   }
64   memset(dst_uv_c, 2, dst_uv_plane_size);
65   memset(dst_uv_opt, 3, dst_uv_plane_size);
66 
67   // Warm up both versions for consistent benchmarks.
68   MaskCpuFlags(disable_cpu_flags);  // Disable all CPU optimization.
69   UVScale(src_uv + (src_stride_uv * b) + b * 2, src_stride_uv, src_width,
70           src_height, dst_uv_c + (dst_stride_uv * b) + b * 2, dst_stride_uv,
71           dst_width, dst_height, f);
72   MaskCpuFlags(benchmark_cpu_info);  // Enable all CPU optimization.
73   UVScale(src_uv + (src_stride_uv * b) + b * 2, src_stride_uv, src_width,
74           src_height, dst_uv_opt + (dst_stride_uv * b) + b * 2, dst_stride_uv,
75           dst_width, dst_height, f);
76 
77   MaskCpuFlags(disable_cpu_flags);  // Disable all CPU optimization.
78   double c_time = get_time();
79   UVScale(src_uv + (src_stride_uv * b) + b * 2, src_stride_uv, src_width,
80           src_height, dst_uv_c + (dst_stride_uv * b) + b * 2, dst_stride_uv,
81           dst_width, dst_height, f);
82 
83   c_time = (get_time() - c_time);
84 
85   MaskCpuFlags(benchmark_cpu_info);  // Enable all CPU optimization.
86   double opt_time = get_time();
87   for (i = 0; i < benchmark_iterations; ++i) {
88     UVScale(src_uv + (src_stride_uv * b) + b * 2, src_stride_uv, src_width,
89             src_height, dst_uv_opt + (dst_stride_uv * b) + b * 2, dst_stride_uv,
90             dst_width, dst_height, f);
91   }
92   opt_time = (get_time() - opt_time) / benchmark_iterations;
93 
94   // Report performance of C vs OPT
95   printf("filter %d - %8d us C - %8d us OPT\n", f,
96          static_cast<int>(c_time * 1e6), static_cast<int>(opt_time * 1e6));
97 
98   // C version may be a little off from the optimized. Order of
99   //  operations may introduce rounding somewhere. So do a difference
100   //  of the buffers and look to see that the max difference isn't
101   //  over 2.
102   int max_diff = 0;
103   for (i = b; i < (dst_height + b); ++i) {
104     for (j = b * 2; j < (dst_width + b) * 2; ++j) {
105       int abs_diff = Abs(dst_uv_c[(i * dst_stride_uv) + j] -
106                          dst_uv_opt[(i * dst_stride_uv) + j]);
107       if (abs_diff > max_diff) {
108         max_diff = abs_diff;
109       }
110     }
111   }
112 
113   free_aligned_buffer_page_end(dst_uv_c);
114   free_aligned_buffer_page_end(dst_uv_opt);
115   free_aligned_buffer_page_end(src_uv);
116   return max_diff;
117 }
118 
119 // The following adjustments in dimensions ensure the scale factor will be
120 // exactly achieved.
121 #define DX(x, nom, denom) static_cast<int>((Abs(x) / nom) * nom)
122 #define SX(x, nom, denom) static_cast<int>((x / nom) * denom)
123 
124 #define TEST_FACTOR1(name, filter, nom, denom, max_diff)                     \
125   TEST_F(LibYUVScaleTest, UVScaleDownBy##name##_##filter) {                  \
126     int diff = UVTestFilter(                                                 \
127         SX(benchmark_width_, nom, denom), SX(benchmark_height_, nom, denom), \
128         DX(benchmark_width_, nom, denom), DX(benchmark_height_, nom, denom), \
129         kFilter##filter, benchmark_iterations_, disable_cpu_flags_,          \
130         benchmark_cpu_info_);                                                \
131     EXPECT_LE(diff, max_diff);                                               \
132   }
133 
134 #if defined(ENABLE_FULL_TESTS)
135 // Test a scale factor with all 4 filters.  Expect unfiltered to be exact, but
136 // filtering is different fixed point implementations for SSSE3, Neon and C.
137 #define TEST_FACTOR(name, nom, denom)         \
138   TEST_FACTOR1(name, None, nom, denom, 0)     \
139   TEST_FACTOR1(name, Linear, nom, denom, 3)   \
140   TEST_FACTOR1(name, Bilinear, nom, denom, 3) \
141   TEST_FACTOR1(name, Box, nom, denom, 3)
142 #else
143 // Test a scale factor with Bilinear.
144 #define TEST_FACTOR(name, nom, denom) \
145   TEST_FACTOR1(name, Bilinear, nom, denom, 3)
146 #endif
147 
148 TEST_FACTOR(2, 1, 2)
149 TEST_FACTOR(4, 1, 4)
150 // TEST_FACTOR(8, 1, 8)  Disable for benchmark performance.
151 TEST_FACTOR(3by4, 3, 4)
152 TEST_FACTOR(3by8, 3, 8)
153 TEST_FACTOR(3, 1, 3)
154 #undef TEST_FACTOR1
155 #undef TEST_FACTOR
156 #undef SX
157 #undef DX
158 
159 #define TEST_SCALETO1(name, width, height, filter, max_diff)                \
160   TEST_F(LibYUVScaleTest, name##To##width##x##height##_##filter) {          \
161     int diff = UVTestFilter(benchmark_width_, benchmark_height_, width,     \
162                             height, kFilter##filter, benchmark_iterations_, \
163                             disable_cpu_flags_, benchmark_cpu_info_);       \
164     EXPECT_LE(diff, max_diff);                                              \
165   }                                                                         \
166   TEST_F(LibYUVScaleTest, name##From##width##x##height##_##filter) {        \
167     int diff = UVTestFilter(width, height, Abs(benchmark_width_),           \
168                             Abs(benchmark_height_), kFilter##filter,        \
169                             benchmark_iterations_, disable_cpu_flags_,      \
170                             benchmark_cpu_info_);                           \
171     EXPECT_LE(diff, max_diff);                                              \
172   }
173 
174 #if defined(ENABLE_FULL_TESTS)
175 /// Test scale to a specified size with all 4 filters.
176 #define TEST_SCALETO(name, width, height)       \
177   TEST_SCALETO1(name, width, height, None, 0)   \
178   TEST_SCALETO1(name, width, height, Linear, 3) \
179   TEST_SCALETO1(name, width, height, Bilinear, 3)
180 #else
181 #define TEST_SCALETO(name, width, height) \
182   TEST_SCALETO1(name, width, height, Bilinear, 3)
183 #endif
184 
185 TEST_SCALETO(UVScale, 1, 1)
186 TEST_SCALETO(UVScale, 569, 480)
187 TEST_SCALETO(UVScale, 640, 360)
188 #ifndef DISABLE_SLOW_TESTS
189 TEST_SCALETO(UVScale, 256, 144) /* 128x72 * 2 */
190 TEST_SCALETO(UVScale, 320, 240)
191 TEST_SCALETO(UVScale, 1280, 720)
192 TEST_SCALETO(UVScale, 1920, 1080)
193 #endif  // DISABLE_SLOW_TESTS
194 #undef TEST_SCALETO1
195 #undef TEST_SCALETO
196 
197 #define TEST_SCALESWAPXY1(name, filter, max_diff)                              \
198   TEST_F(LibYUVScaleTest, name##SwapXY_##filter) {                             \
199     int diff =                                                                 \
200         UVTestFilter(benchmark_width_, benchmark_height_, benchmark_height_,   \
201                      benchmark_width_, kFilter##filter, benchmark_iterations_, \
202                      disable_cpu_flags_, benchmark_cpu_info_);                 \
203     EXPECT_LE(diff, max_diff);                                                 \
204   }
205 
206 #if defined(ENABLE_FULL_TESTS)
207 // Test scale with swapped width and height with all 3 filters.
208 TEST_SCALESWAPXY1(UVScale, None, 0)
209 TEST_SCALESWAPXY1(UVScale, Linear, 0)
210 TEST_SCALESWAPXY1(UVScale, Bilinear, 0)
211 #else
212 TEST_SCALESWAPXY1(UVScale, Bilinear, 0)
213 #endif
214 #undef TEST_SCALESWAPXY1
215 
TEST_F(LibYUVScaleTest,UVTest3x)216 TEST_F(LibYUVScaleTest, UVTest3x) {
217   const int kSrcStride = 480 * 2;
218   const int kDstStride = 160 * 2;
219   const int kSize = kSrcStride * 3;
220   align_buffer_page_end(orig_pixels, kSize);
221   for (int i = 0; i < 480 * 3; ++i) {
222     orig_pixels[i * 2 + 0] = i;
223     orig_pixels[i * 2 + 1] = 255 - i;
224   }
225   align_buffer_page_end(dest_pixels, kDstStride);
226 
227   int iterations160 = (benchmark_width_ * benchmark_height_ + (160 - 1)) / 160 *
228                       benchmark_iterations_;
229   for (int i = 0; i < iterations160; ++i) {
230     UVScale(orig_pixels, kSrcStride, 480, 3, dest_pixels, kDstStride, 160, 1,
231             kFilterBilinear);
232   }
233 
234   EXPECT_EQ(225, dest_pixels[0]);
235   EXPECT_EQ(255 - 225, dest_pixels[1]);
236 
237   UVScale(orig_pixels, kSrcStride, 480, 3, dest_pixels, kDstStride, 160, 1,
238           kFilterNone);
239 
240   EXPECT_EQ(225, dest_pixels[0]);
241   EXPECT_EQ(255 - 225, dest_pixels[1]);
242 
243   free_aligned_buffer_page_end(dest_pixels);
244   free_aligned_buffer_page_end(orig_pixels);
245 }
246 
TEST_F(LibYUVScaleTest,UVTest4x)247 TEST_F(LibYUVScaleTest, UVTest4x) {
248   const int kSrcStride = 640 * 2;
249   const int kDstStride = 160 * 2;
250   const int kSize = kSrcStride * 4;
251   align_buffer_page_end(orig_pixels, kSize);
252   for (int i = 0; i < 640 * 4; ++i) {
253     orig_pixels[i * 2 + 0] = i;
254     orig_pixels[i * 2 + 1] = 255 - i;
255   }
256   align_buffer_page_end(dest_pixels, kDstStride);
257 
258   int iterations160 = (benchmark_width_ * benchmark_height_ + (160 - 1)) / 160 *
259                       benchmark_iterations_;
260   for (int i = 0; i < iterations160; ++i) {
261     UVScale(orig_pixels, kSrcStride, 640, 4, dest_pixels, kDstStride, 160, 1,
262             kFilterBilinear);
263   }
264 
265   EXPECT_EQ(66, dest_pixels[0]);
266   EXPECT_EQ(190, dest_pixels[1]);
267 
268   UVScale(orig_pixels, kSrcStride, 64, 4, dest_pixels, kDstStride, 16, 1,
269           kFilterNone);
270 
271   EXPECT_EQ(2, dest_pixels[0]);  // expect the 3rd pixel of the 3rd row
272   EXPECT_EQ(255 - 2, dest_pixels[1]);
273 
274   free_aligned_buffer_page_end(dest_pixels);
275   free_aligned_buffer_page_end(orig_pixels);
276 }
277 
278 }  // namespace libyuv
279