scale_argb_test.cc 19 KB

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