xref: /aosp_15_r20/external/libyuv/unit_test/scale_rgb_test.cc (revision 4e366538070a3a6c5c163c31b791eab742e1657a)
1 /*
2  *  Copyright 2022 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_rgb.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.
RGBTestFilter(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 RGBTestFilter(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_rgb_plane_size =
45       (Abs(src_width) + b * 3) * (Abs(src_height) + b * 3) * 3LL;
46   int src_stride_rgb = (b * 3 + Abs(src_width)) * 3;
47 
48   align_buffer_page_end(src_rgb, src_rgb_plane_size);
49   if (!src_rgb) {
50     printf("Skipped.  Alloc failed " FILELINESTR(__FILE__, __LINE__) "\n");
51     return 0;
52   }
53   MemRandomize(src_rgb, src_rgb_plane_size);
54 
55   int64_t dst_rgb_plane_size = (dst_width + b * 3) * (dst_height + b * 3) * 3LL;
56   int dst_stride_rgb = (b * 3 + dst_width) * 3;
57 
58   align_buffer_page_end(dst_rgb_c, dst_rgb_plane_size);
59   align_buffer_page_end(dst_rgb_opt, dst_rgb_plane_size);
60   if (!dst_rgb_c || !dst_rgb_opt) {
61     printf("Skipped.  Alloc failed " FILELINESTR(__FILE__, __LINE__) "\n");
62     return 0;
63   }
64   memset(dst_rgb_c, 2, dst_rgb_plane_size);
65   memset(dst_rgb_opt, 3, dst_rgb_plane_size);
66 
67   // Warm up both versions for consistent benchmarks.
68   MaskCpuFlags(disable_cpu_flags);  // Disable all CPU optimization.
69   RGBScale(src_rgb + (src_stride_rgb * b) + b * 3, src_stride_rgb, src_width,
70            src_height, dst_rgb_c + (dst_stride_rgb * b) + b * 3, dst_stride_rgb,
71            dst_width, dst_height, f);
72   MaskCpuFlags(benchmark_cpu_info);  // Enable all CPU optimization.
73   RGBScale(src_rgb + (src_stride_rgb * b) + b * 3, src_stride_rgb, src_width,
74            src_height, dst_rgb_opt + (dst_stride_rgb * b) + b * 3,
75            dst_stride_rgb, dst_width, dst_height, f);
76 
77   MaskCpuFlags(disable_cpu_flags);  // Disable all CPU optimization.
78   double c_time = get_time();
79   RGBScale(src_rgb + (src_stride_rgb * b) + b * 3, src_stride_rgb, src_width,
80            src_height, dst_rgb_c + (dst_stride_rgb * b) + b * 3, dst_stride_rgb,
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     RGBScale(src_rgb + (src_stride_rgb * b) + b * 3, src_stride_rgb, src_width,
89              src_height, dst_rgb_opt + (dst_stride_rgb * b) + b * 3,
90              dst_stride_rgb, 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 * 3; j < (dst_width + b) * 3; ++j) {
105       int abs_diff = Abs(dst_rgb_c[(i * dst_stride_rgb) + j] -
106                          dst_rgb_opt[(i * dst_stride_rgb) + j]);
107       if (abs_diff > max_diff) {
108         max_diff = abs_diff;
109       }
110     }
111   }
112 
113   free_aligned_buffer_page_end(dst_rgb_c);
114   free_aligned_buffer_page_end(dst_rgb_opt);
115   free_aligned_buffer_page_end(src_rgb);
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, RGBScaleDownBy##name##_##filter) {                 \
126     int diff = RGBTestFilter(                                                \
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 #ifndef DISABLE_SLOW_TESTS
150 TEST_FACTOR(4, 1, 4)
151 // TEST_FACTOR(8, 1, 8)  Disable for benchmark performance.
152 TEST_FACTOR(3by4, 3, 4)
153 TEST_FACTOR(3by8, 3, 8)
154 TEST_FACTOR(3, 1, 3)
155 #endif
156 #undef TEST_FACTOR1
157 #undef TEST_FACTOR
158 #undef SX
159 #undef DX
160 
161 #define TEST_SCALETO1(name, width, height, filter, max_diff)                 \
162   TEST_F(LibYUVScaleTest, name##To##width##x##height##_##filter) {           \
163     int diff = RGBTestFilter(benchmark_width_, benchmark_height_, width,     \
164                              height, kFilter##filter, benchmark_iterations_, \
165                              disable_cpu_flags_, benchmark_cpu_info_);       \
166     EXPECT_LE(diff, max_diff);                                               \
167   }                                                                          \
168   TEST_F(LibYUVScaleTest, name##From##width##x##height##_##filter) {         \
169     int diff = RGBTestFilter(width, height, Abs(benchmark_width_),           \
170                              Abs(benchmark_height_), kFilter##filter,        \
171                              benchmark_iterations_, disable_cpu_flags_,      \
172                              benchmark_cpu_info_);                           \
173     EXPECT_LE(diff, max_diff);                                               \
174   }
175 
176 #if defined(ENABLE_FULL_TESTS)
177 /// Test scale to a specified size with all 4 filters.
178 #define TEST_SCALETO(name, width, height)       \
179   TEST_SCALETO1(name, width, height, None, 0)   \
180   TEST_SCALETO1(name, width, height, Linear, 3) \
181   TEST_SCALETO1(name, width, height, Bilinear, 3)
182 #else
183 #define TEST_SCALETO(name, width, height) \
184   TEST_SCALETO1(name, width, height, Bilinear, 3)
185 #endif
186 
187 TEST_SCALETO(RGBScale, 640, 360)
188 #ifndef DISABLE_SLOW_TESTS
189 TEST_SCALETO(RGBScale, 1, 1)
190 TEST_SCALETO(RGBScale, 256, 144) /* 128x72 * 3 */
191 TEST_SCALETO(RGBScale, 320, 240)
192 TEST_SCALETO(RGBScale, 569, 480)
193 TEST_SCALETO(RGBScale, 1280, 720)
194 TEST_SCALETO(RGBScale, 1920, 1080)
195 #endif  // DISABLE_SLOW_TESTS
196 #undef TEST_SCALETO1
197 #undef TEST_SCALETO
198 
199 #define TEST_SCALESWAPXY1(name, filter, max_diff)                      \
200   TEST_F(LibYUVScaleTest, name##SwapXY_##filter) {                     \
201     int diff = RGBTestFilter(benchmark_width_, benchmark_height_,      \
202                              benchmark_height_, benchmark_width_,      \
203                              kFilter##filter, benchmark_iterations_,   \
204                              disable_cpu_flags_, benchmark_cpu_info_); \
205     EXPECT_LE(diff, max_diff);                                         \
206   }
207 
208 #if defined(ENABLE_FULL_TESTS)
209 // Test scale with swapped width and height with all 3 filters.
210 TEST_SCALESWAPXY1(RGBScale, None, 0)
211 TEST_SCALESWAPXY1(RGBScale, Linear, 0)
212 TEST_SCALESWAPXY1(RGBScale, Bilinear, 0)
213 #else
214 TEST_SCALESWAPXY1(RGBScale, Bilinear, 0)
215 #endif
216 #undef TEST_SCALESWAPXY1
217 
TEST_F(LibYUVScaleTest,RGBTest3x)218 TEST_F(LibYUVScaleTest, RGBTest3x) {
219   const int kSrcStride = 480 * 3;
220   const int kDstStride = 160 * 3;
221   const int kSize = kSrcStride * 3;
222   align_buffer_page_end(orig_pixels, kSize);
223   for (int i = 0; i < 480 * 3; ++i) {
224     orig_pixels[i * 3 + 0] = i;
225     orig_pixels[i * 3 + 1] = 255 - i;
226   }
227   align_buffer_page_end(dest_pixels, kDstStride);
228 
229   int iterations160 = (benchmark_width_ * benchmark_height_ + (160 - 1)) / 160 *
230                       benchmark_iterations_;
231   for (int i = 0; i < iterations160; ++i) {
232     RGBScale(orig_pixels, kSrcStride, 480, 3, dest_pixels, kDstStride, 160, 1,
233              kFilterBilinear);
234   }
235 
236   EXPECT_EQ(225, dest_pixels[0]);
237   EXPECT_EQ(255 - 225, dest_pixels[1]);
238 
239   RGBScale(orig_pixels, kSrcStride, 480, 3, dest_pixels, kDstStride, 160, 1,
240            kFilterNone);
241 
242   EXPECT_EQ(225, dest_pixels[0]);
243   EXPECT_EQ(255 - 225, dest_pixels[1]);
244 
245   free_aligned_buffer_page_end(dest_pixels);
246   free_aligned_buffer_page_end(orig_pixels);
247 }
248 
TEST_F(LibYUVScaleTest,RGBTest4x)249 TEST_F(LibYUVScaleTest, RGBTest4x) {
250   const int kSrcStride = 640 * 3;
251   const int kDstStride = 160 * 3;
252   const int kSize = kSrcStride * 4;
253   align_buffer_page_end(orig_pixels, kSize);
254   for (int i = 0; i < 640 * 4; ++i) {
255     orig_pixels[i * 3 + 0] = i;
256     orig_pixels[i * 3 + 1] = 255 - i;
257   }
258   align_buffer_page_end(dest_pixels, kDstStride);
259 
260   int iterations160 = (benchmark_width_ * benchmark_height_ + (160 - 1)) / 160 *
261                       benchmark_iterations_;
262   for (int i = 0; i < iterations160; ++i) {
263     RGBScale(orig_pixels, kSrcStride, 640, 4, dest_pixels, kDstStride, 160, 1,
264              kFilterBilinear);
265   }
266 
267   EXPECT_EQ(66, dest_pixels[0]);
268   EXPECT_EQ(190, dest_pixels[1]);
269 
270   RGBScale(orig_pixels, kSrcStride, 64, 4, dest_pixels, kDstStride, 16, 1,
271            kFilterNone);
272 
273   EXPECT_EQ(2, dest_pixels[0]);  // expect the 3rd pixel of the 3rd row
274   EXPECT_EQ(255 - 2, dest_pixels[1]);
275 
276   free_aligned_buffer_page_end(dest_pixels);
277   free_aligned_buffer_page_end(orig_pixels);
278 }
279 
280 }  // namespace libyuv
281