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