scale_test.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. #include <stdlib.h>
  11. #include <time.h>
  12. #include "libyuv/cpu_id.h"
  13. #include "libyuv/scale.h"
  14. #include "../unit_test/unit_test.h"
  15. #define STRINGIZE(line) #line
  16. #define FILELINESTR(file, line) file ":" STRINGIZE(line)
  17. namespace libyuv {
  18. // Test scaling with C vs Opt and return maximum pixel difference. 0 = exact.
  19. static int TestFilter(int src_width, int src_height,
  20. int dst_width, int dst_height,
  21. FilterMode f, int benchmark_iterations,
  22. int disable_cpu_flags, int benchmark_cpu_info) {
  23. if (!SizeValid(src_width, src_height, dst_width, dst_height)) {
  24. return 0;
  25. }
  26. int i, j;
  27. const int b = 0; // 128 to test for padding/stride.
  28. int src_width_uv = (Abs(src_width) + 1) >> 1;
  29. int src_height_uv = (Abs(src_height) + 1) >> 1;
  30. int64 src_y_plane_size = (Abs(src_width) + b * 2) * (Abs(src_height) + b * 2);
  31. int64 src_uv_plane_size = (src_width_uv + b * 2) * (src_height_uv + b * 2);
  32. int src_stride_y = b * 2 + Abs(src_width);
  33. int src_stride_uv = b * 2 + src_width_uv;
  34. align_buffer_page_end(src_y, src_y_plane_size)
  35. align_buffer_page_end(src_u, src_uv_plane_size)
  36. align_buffer_page_end(src_v, src_uv_plane_size)
  37. if (!src_y || !src_u || !src_v) {
  38. printf("Skipped. Alloc failed " FILELINESTR(__FILE__, __LINE__) "\n");
  39. return 0;
  40. }
  41. MemRandomize(src_y, src_y_plane_size);
  42. MemRandomize(src_u, src_uv_plane_size);
  43. MemRandomize(src_v, src_uv_plane_size);
  44. int dst_width_uv = (dst_width + 1) >> 1;
  45. int dst_height_uv = (dst_height + 1) >> 1;
  46. int64 dst_y_plane_size = (dst_width + b * 2) * (dst_height + b * 2);
  47. int64 dst_uv_plane_size = (dst_width_uv + b * 2) * (dst_height_uv + b * 2);
  48. int dst_stride_y = b * 2 + dst_width;
  49. int dst_stride_uv = b * 2 + dst_width_uv;
  50. align_buffer_page_end(dst_y_c, dst_y_plane_size)
  51. align_buffer_page_end(dst_u_c, dst_uv_plane_size)
  52. align_buffer_page_end(dst_v_c, dst_uv_plane_size)
  53. align_buffer_page_end(dst_y_opt, dst_y_plane_size)
  54. align_buffer_page_end(dst_u_opt, dst_uv_plane_size)
  55. align_buffer_page_end(dst_v_opt, dst_uv_plane_size)
  56. if (!dst_y_c || !dst_u_c || !dst_v_c ||
  57. !dst_y_opt|| !dst_u_opt|| !dst_v_opt) {
  58. printf("Skipped. Alloc failed " FILELINESTR(__FILE__, __LINE__) "\n");
  59. return 0;
  60. }
  61. MaskCpuFlags(disable_cpu_flags); // Disable all CPU optimization.
  62. double c_time = get_time();
  63. I420Scale(src_y + (src_stride_y * b) + b, src_stride_y,
  64. src_u + (src_stride_uv * b) + b, src_stride_uv,
  65. src_v + (src_stride_uv * b) + b, src_stride_uv,
  66. src_width, src_height,
  67. dst_y_c + (dst_stride_y * b) + b, dst_stride_y,
  68. dst_u_c + (dst_stride_uv * b) + b, dst_stride_uv,
  69. dst_v_c + (dst_stride_uv * b) + b, dst_stride_uv,
  70. dst_width, dst_height, f);
  71. c_time = (get_time() - c_time);
  72. MaskCpuFlags(benchmark_cpu_info); // Enable all CPU optimization.
  73. double opt_time = get_time();
  74. for (i = 0; i < benchmark_iterations; ++i) {
  75. I420Scale(src_y + (src_stride_y * b) + b, src_stride_y,
  76. src_u + (src_stride_uv * b) + b, src_stride_uv,
  77. src_v + (src_stride_uv * b) + b, src_stride_uv,
  78. src_width, src_height,
  79. dst_y_opt + (dst_stride_y * b) + b, dst_stride_y,
  80. dst_u_opt + (dst_stride_uv * b) + b, dst_stride_uv,
  81. dst_v_opt + (dst_stride_uv * b) + b, dst_stride_uv,
  82. dst_width, dst_height, f);
  83. }
  84. opt_time = (get_time() - opt_time) / benchmark_iterations;
  85. // Report performance of C vs OPT
  86. printf("filter %d - %8d us C - %8d us OPT\n",
  87. f,
  88. static_cast<int>(c_time * 1e6),
  89. static_cast<int>(opt_time * 1e6));
  90. // C version may be a little off from the optimized. Order of
  91. // operations may introduce rounding somewhere. So do a difference
  92. // of the buffers and look to see that the max difference isn't
  93. // over 2.
  94. int max_diff = 0;
  95. for (i = b; i < (dst_height + b); ++i) {
  96. for (j = b; j < (dst_width + b); ++j) {
  97. int abs_diff = Abs(dst_y_c[(i * dst_stride_y) + j] -
  98. dst_y_opt[(i * dst_stride_y) + j]);
  99. if (abs_diff > max_diff) {
  100. max_diff = abs_diff;
  101. }
  102. }
  103. }
  104. for (i = b; i < (dst_height_uv + b); ++i) {
  105. for (j = b; j < (dst_width_uv + b); ++j) {
  106. int abs_diff = Abs(dst_u_c[(i * dst_stride_uv) + j] -
  107. dst_u_opt[(i * dst_stride_uv) + j]);
  108. if (abs_diff > max_diff) {
  109. max_diff = abs_diff;
  110. }
  111. abs_diff = Abs(dst_v_c[(i * dst_stride_uv) + j] -
  112. dst_v_opt[(i * dst_stride_uv) + j]);
  113. if (abs_diff > max_diff) {
  114. max_diff = abs_diff;
  115. }
  116. }
  117. }
  118. free_aligned_buffer_page_end(dst_y_c)
  119. free_aligned_buffer_page_end(dst_u_c)
  120. free_aligned_buffer_page_end(dst_v_c)
  121. free_aligned_buffer_page_end(dst_y_opt)
  122. free_aligned_buffer_page_end(dst_u_opt)
  123. free_aligned_buffer_page_end(dst_v_opt)
  124. free_aligned_buffer_page_end(src_y)
  125. free_aligned_buffer_page_end(src_u)
  126. free_aligned_buffer_page_end(src_v)
  127. return max_diff;
  128. }
  129. // Test scaling with 8 bit C vs 16 bit C and return maximum pixel difference.
  130. // 0 = exact.
  131. static int TestFilter_16(int src_width, int src_height,
  132. int dst_width, int dst_height,
  133. FilterMode f, int benchmark_iterations) {
  134. if (!SizeValid(src_width, src_height, dst_width, dst_height)) {
  135. return 0;
  136. }
  137. int i, j;
  138. const int b = 0; // 128 to test for padding/stride.
  139. int src_width_uv = (Abs(src_width) + 1) >> 1;
  140. int src_height_uv = (Abs(src_height) + 1) >> 1;
  141. int64 src_y_plane_size = (Abs(src_width) + b * 2) *
  142. (Abs(src_height) + b * 2);
  143. int64 src_uv_plane_size = (src_width_uv + b * 2) * (src_height_uv + b * 2);
  144. int src_stride_y = b * 2 + Abs(src_width);
  145. int src_stride_uv = b * 2 + src_width_uv;
  146. align_buffer_page_end(src_y, src_y_plane_size)
  147. align_buffer_page_end(src_u, src_uv_plane_size)
  148. align_buffer_page_end(src_v, src_uv_plane_size)
  149. align_buffer_page_end(src_y_16, src_y_plane_size * 2)
  150. align_buffer_page_end(src_u_16, src_uv_plane_size * 2)
  151. align_buffer_page_end(src_v_16, src_uv_plane_size * 2)
  152. uint16* p_src_y_16 = reinterpret_cast<uint16*>(src_y_16);
  153. uint16* p_src_u_16 = reinterpret_cast<uint16*>(src_u_16);
  154. uint16* p_src_v_16 = reinterpret_cast<uint16*>(src_v_16);
  155. MemRandomize(src_y, src_y_plane_size);
  156. MemRandomize(src_u, src_uv_plane_size);
  157. MemRandomize(src_v, src_uv_plane_size);
  158. for (i = b; i < src_height + b; ++i) {
  159. for (j = b; j < src_width + b; ++j) {
  160. p_src_y_16[(i * src_stride_y) + j] = src_y[(i * src_stride_y) + j];
  161. }
  162. }
  163. for (i = b; i < (src_height_uv + b); ++i) {
  164. for (j = b; j < (src_width_uv + b); ++j) {
  165. p_src_u_16[(i * src_stride_uv) + j] = src_u[(i * src_stride_uv) + j];
  166. p_src_v_16[(i * src_stride_uv) + j] = src_v[(i * src_stride_uv) + j];
  167. }
  168. }
  169. int dst_width_uv = (dst_width + 1) >> 1;
  170. int dst_height_uv = (dst_height + 1) >> 1;
  171. int dst_y_plane_size = (dst_width + b * 2) * (dst_height + b * 2);
  172. int dst_uv_plane_size = (dst_width_uv + b * 2) * (dst_height_uv + b * 2);
  173. int dst_stride_y = b * 2 + dst_width;
  174. int dst_stride_uv = b * 2 + dst_width_uv;
  175. align_buffer_page_end(dst_y_8, dst_y_plane_size)
  176. align_buffer_page_end(dst_u_8, dst_uv_plane_size)
  177. align_buffer_page_end(dst_v_8, dst_uv_plane_size)
  178. align_buffer_page_end(dst_y_16, dst_y_plane_size * 2)
  179. align_buffer_page_end(dst_u_16, dst_uv_plane_size * 2)
  180. align_buffer_page_end(dst_v_16, dst_uv_plane_size * 2)
  181. uint16* p_dst_y_16 = reinterpret_cast<uint16*>(dst_y_16);
  182. uint16* p_dst_u_16 = reinterpret_cast<uint16*>(dst_u_16);
  183. uint16* p_dst_v_16 = reinterpret_cast<uint16*>(dst_v_16);
  184. I420Scale(src_y + (src_stride_y * b) + b, src_stride_y,
  185. src_u + (src_stride_uv * b) + b, src_stride_uv,
  186. src_v + (src_stride_uv * b) + b, src_stride_uv,
  187. src_width, src_height,
  188. dst_y_8 + (dst_stride_y * b) + b, dst_stride_y,
  189. dst_u_8 + (dst_stride_uv * b) + b, dst_stride_uv,
  190. dst_v_8 + (dst_stride_uv * b) + b, dst_stride_uv,
  191. dst_width, dst_height, f);
  192. for (i = 0; i < benchmark_iterations; ++i) {
  193. I420Scale_16(p_src_y_16 + (src_stride_y * b) + b, src_stride_y,
  194. p_src_u_16 + (src_stride_uv * b) + b, src_stride_uv,
  195. p_src_v_16 + (src_stride_uv * b) + b, src_stride_uv,
  196. src_width, src_height,
  197. p_dst_y_16 + (dst_stride_y * b) + b, dst_stride_y,
  198. p_dst_u_16 + (dst_stride_uv * b) + b, dst_stride_uv,
  199. p_dst_v_16 + (dst_stride_uv * b) + b, dst_stride_uv,
  200. dst_width, dst_height, f);
  201. }
  202. // Expect an exact match
  203. int max_diff = 0;
  204. for (i = b; i < (dst_height + b); ++i) {
  205. for (j = b; j < (dst_width + b); ++j) {
  206. int abs_diff = Abs(dst_y_8[(i * dst_stride_y) + j] -
  207. p_dst_y_16[(i * dst_stride_y) + j]);
  208. if (abs_diff > max_diff) {
  209. max_diff = abs_diff;
  210. }
  211. }
  212. }
  213. for (i = b; i < (dst_height_uv + b); ++i) {
  214. for (j = b; j < (dst_width_uv + b); ++j) {
  215. int abs_diff = Abs(dst_u_8[(i * dst_stride_uv) + j] -
  216. p_dst_u_16[(i * dst_stride_uv) + j]);
  217. if (abs_diff > max_diff) {
  218. max_diff = abs_diff;
  219. }
  220. abs_diff = Abs(dst_v_8[(i * dst_stride_uv) + j] -
  221. p_dst_v_16[(i * dst_stride_uv) + j]);
  222. if (abs_diff > max_diff) {
  223. max_diff = abs_diff;
  224. }
  225. }
  226. }
  227. free_aligned_buffer_page_end(dst_y_8)
  228. free_aligned_buffer_page_end(dst_u_8)
  229. free_aligned_buffer_page_end(dst_v_8)
  230. free_aligned_buffer_page_end(dst_y_16)
  231. free_aligned_buffer_page_end(dst_u_16)
  232. free_aligned_buffer_page_end(dst_v_16)
  233. free_aligned_buffer_page_end(src_y)
  234. free_aligned_buffer_page_end(src_u)
  235. free_aligned_buffer_page_end(src_v)
  236. free_aligned_buffer_page_end(src_y_16)
  237. free_aligned_buffer_page_end(src_u_16)
  238. free_aligned_buffer_page_end(src_v_16)
  239. return max_diff;
  240. }
  241. // The following adjustments in dimensions ensure the scale factor will be
  242. // exactly achieved.
  243. // 2 is chroma subsample
  244. #define DX(x, nom, denom) static_cast<int>(((Abs(x) / nom + 1) / 2) * nom * 2)
  245. #define SX(x, nom, denom) static_cast<int>(((x / nom + 1) / 2) * denom * 2)
  246. #define TEST_FACTOR1(name, filter, nom, denom, max_diff) \
  247. TEST_F(LibYUVScaleTest, ScaleDownBy##name##_##filter) { \
  248. int diff = TestFilter(SX(benchmark_width_, nom, denom), \
  249. SX(benchmark_height_, nom, denom), \
  250. DX(benchmark_width_, nom, denom), \
  251. DX(benchmark_height_, nom, denom), \
  252. kFilter##filter, benchmark_iterations_, \
  253. disable_cpu_flags_, benchmark_cpu_info_); \
  254. EXPECT_LE(diff, max_diff); \
  255. } \
  256. TEST_F(LibYUVScaleTest, DISABLED_ScaleDownBy##name##_##filter##_16) { \
  257. int diff = TestFilter_16(SX(benchmark_width_, nom, denom), \
  258. SX(benchmark_height_, nom, denom), \
  259. DX(benchmark_width_, nom, denom), \
  260. DX(benchmark_height_, nom, denom), \
  261. kFilter##filter, benchmark_iterations_); \
  262. EXPECT_LE(diff, max_diff); \
  263. }
  264. // Test a scale factor with all 4 filters. Expect unfiltered to be exact, but
  265. // filtering is different fixed point implementations for SSSE3, Neon and C.
  266. #define TEST_FACTOR(name, nom, denom, boxdiff) \
  267. TEST_FACTOR1(name, None, nom, denom, 0) \
  268. TEST_FACTOR1(name, Linear, nom, denom, 3) \
  269. TEST_FACTOR1(name, Bilinear, nom, denom, 3) \
  270. TEST_FACTOR1(name, Box, nom, denom, boxdiff)
  271. TEST_FACTOR(2, 1, 2, 0)
  272. TEST_FACTOR(4, 1, 4, 0)
  273. TEST_FACTOR(8, 1, 8, 0)
  274. TEST_FACTOR(3by4, 3, 4, 1)
  275. TEST_FACTOR(3by8, 3, 8, 1)
  276. TEST_FACTOR(3, 1, 3, 0)
  277. #undef TEST_FACTOR1
  278. #undef TEST_FACTOR
  279. #undef SX
  280. #undef DX
  281. #define TEST_SCALETO1(name, width, height, filter, max_diff) \
  282. TEST_F(LibYUVScaleTest, name##To##width##x##height##_##filter) { \
  283. int diff = TestFilter(benchmark_width_, benchmark_height_, \
  284. width, height, \
  285. kFilter##filter, benchmark_iterations_, \
  286. disable_cpu_flags_, benchmark_cpu_info_); \
  287. EXPECT_LE(diff, max_diff); \
  288. } \
  289. TEST_F(LibYUVScaleTest, name##From##width##x##height##_##filter) { \
  290. int diff = TestFilter(width, height, \
  291. Abs(benchmark_width_), Abs(benchmark_height_), \
  292. kFilter##filter, benchmark_iterations_, \
  293. disable_cpu_flags_, benchmark_cpu_info_); \
  294. EXPECT_LE(diff, max_diff); \
  295. } \
  296. TEST_F(LibYUVScaleTest, \
  297. DISABLED_##name##To##width##x##height##_##filter##_16) { \
  298. int diff = TestFilter_16(benchmark_width_, benchmark_height_, \
  299. width, height, \
  300. kFilter##filter, benchmark_iterations_); \
  301. EXPECT_LE(diff, max_diff); \
  302. } \
  303. TEST_F(LibYUVScaleTest, \
  304. DISABLED_##name##From##width##x##height##_##filter##_16) { \
  305. int diff = TestFilter_16(width, height, \
  306. Abs(benchmark_width_), Abs(benchmark_height_), \
  307. kFilter##filter, benchmark_iterations_); \
  308. EXPECT_LE(diff, max_diff); \
  309. }
  310. // Test scale to a specified size with all 4 filters.
  311. #define TEST_SCALETO(name, width, height) \
  312. TEST_SCALETO1(name, width, height, None, 0) \
  313. TEST_SCALETO1(name, width, height, Linear, 0) \
  314. TEST_SCALETO1(name, width, height, Bilinear, 0) \
  315. TEST_SCALETO1(name, width, height, Box, 0)
  316. TEST_SCALETO(Scale, 1, 1)
  317. TEST_SCALETO(Scale, 320, 240)
  318. TEST_SCALETO(Scale, 352, 288)
  319. TEST_SCALETO(Scale, 569, 480)
  320. TEST_SCALETO(Scale, 640, 360)
  321. TEST_SCALETO(Scale, 1280, 720)
  322. #undef TEST_SCALETO1
  323. #undef TEST_SCALETO
  324. } // namespace libyuv