swscale.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Copyright (C) 2024 Nikles Haas
  3. * Copyright (C) 2003-2011 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <inttypes.h>
  25. #include <stdarg.h>
  26. #undef HAVE_AV_CONFIG_H
  27. #include "libavutil/cpu.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavutil/lfg.h"
  30. #include "libavutil/sfc64.h"
  31. #include "libavutil/frame.h"
  32. #include "libavutil/opt.h"
  33. #include "libavutil/time.h"
  34. #include "libavutil/pixfmt.h"
  35. #include "libavutil/avassert.h"
  36. #include "libavutil/macros.h"
  37. #include "libswscale/swscale.h"
  38. struct options {
  39. enum AVPixelFormat src_fmt;
  40. enum AVPixelFormat dst_fmt;
  41. double prob;
  42. int w, h;
  43. int threads;
  44. int iters;
  45. int bench;
  46. };
  47. struct mode {
  48. SwsFlags flags;
  49. SwsDither dither;
  50. };
  51. const struct mode modes[] = {
  52. { SWS_FAST_BILINEAR },
  53. { SWS_BILINEAR },
  54. { SWS_BICUBIC },
  55. { SWS_X | SWS_BITEXACT },
  56. { SWS_POINT },
  57. { SWS_AREA | SWS_ACCURATE_RND },
  58. { SWS_BICUBIC | SWS_FULL_CHR_H_INT | SWS_FULL_CHR_H_INP },
  59. {0}, // test defaults
  60. };
  61. static FFSFC64 prng_state;
  62. static SwsContext *sws[3]; /* reused between tests for efficiency */
  63. static int fmt_comps(enum AVPixelFormat fmt)
  64. {
  65. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  66. int comps = desc->nb_components >= 3 ? 0b111 : 0b1;
  67. if (desc->flags & AV_PIX_FMT_FLAG_ALPHA)
  68. comps |= 0b1000;
  69. return comps;
  70. }
  71. static void get_mse(int mse[4], const AVFrame *a, const AVFrame *b, int comps)
  72. {
  73. av_assert1(a->format == AV_PIX_FMT_YUVA420P);
  74. av_assert1(b->format == a->format);
  75. av_assert1(b->width == a->width && b->height == a->height);
  76. for (int p = 0; p < 4; p++) {
  77. const int is_chroma = p == 1 || p == 2;
  78. const int stride_a = a->linesize[p];
  79. const int stride_b = b->linesize[p];
  80. const int w = (a->width + is_chroma) >> is_chroma;
  81. const int h = (a->height + is_chroma) >> is_chroma;
  82. uint64_t sum = 0;
  83. if (comps & (1 << p)) {
  84. for (int y = 0; y < h; y++) {
  85. for (int x = 0; x < w; x++) {
  86. int d = a->data[p][y * stride_a + x] - b->data[p][y * stride_b + x];
  87. sum += d * d;
  88. }
  89. }
  90. } else {
  91. const int ref = is_chroma ? 128 : 0xFF;
  92. for (int y = 0; y < h; y++) {
  93. for (int x = 0; x < w; x++) {
  94. int d = a->data[p][y * stride_a + x] - ref;
  95. sum += d * d;
  96. }
  97. }
  98. }
  99. mse[p] = sum / (w * h);
  100. }
  101. }
  102. static int scale_legacy(AVFrame *dst, const AVFrame *src, struct mode mode,
  103. struct options opts)
  104. {
  105. SwsContext *sws_legacy;
  106. int ret;
  107. sws_legacy = sws_alloc_context();
  108. if (!sws_legacy)
  109. return -1;
  110. sws_legacy->src_w = src->width;
  111. sws_legacy->src_h = src->height;
  112. sws_legacy->src_format = src->format;
  113. sws_legacy->dst_w = dst->width;
  114. sws_legacy->dst_h = dst->height;
  115. sws_legacy->dst_format = dst->format;
  116. sws_legacy->flags = mode.flags;
  117. sws_legacy->dither = mode.dither;
  118. sws_legacy->threads = opts.threads;
  119. if ((ret = sws_init_context(sws_legacy, NULL, NULL)) < 0)
  120. goto error;
  121. for (int i = 0; ret >= 0 && i < opts.iters; i++)
  122. ret = sws_scale_frame(sws_legacy, dst, src);
  123. error:
  124. sws_freeContext(sws_legacy);
  125. return ret;
  126. }
  127. /* Runs a series of ref -> src -> dst -> out, and compares out vs ref */
  128. static int run_test(enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt,
  129. int dst_w, int dst_h, struct mode mode, struct options opts,
  130. const AVFrame *ref, const int mse_ref[4])
  131. {
  132. AVFrame *src = NULL, *dst = NULL, *out = NULL;
  133. int mse[4], mse_sws[4], ret = -1;
  134. const int comps = fmt_comps(src_fmt) & fmt_comps(dst_fmt);
  135. int64_t time, time_ref = 0;
  136. src = av_frame_alloc();
  137. dst = av_frame_alloc();
  138. out = av_frame_alloc();
  139. if (!src || !dst || !out)
  140. goto error;
  141. av_frame_copy_props(src, ref);
  142. av_frame_copy_props(dst, ref);
  143. av_frame_copy_props(out, ref);
  144. src->width = out->width = ref->width;
  145. src->height = out->height = ref->height;
  146. out->format = ref->format;
  147. src->format = src_fmt;
  148. dst->format = dst_fmt;
  149. dst->width = dst_w;
  150. dst->height = dst_h;
  151. if (sws_scale_frame(sws[0], src, ref) < 0) {
  152. fprintf(stderr, "Failed %s ---> %s\n", av_get_pix_fmt_name(ref->format),
  153. av_get_pix_fmt_name(src->format));
  154. goto error;
  155. }
  156. sws[1]->flags = mode.flags;
  157. sws[1]->dither = mode.dither;
  158. sws[1]->threads = opts.threads;
  159. time = av_gettime_relative();
  160. for (int i = 0; i < opts.iters; i++) {
  161. if (sws_scale_frame(sws[1], dst, src) < 0) {
  162. fprintf(stderr, "Failed %s ---> %s\n", av_get_pix_fmt_name(src->format),
  163. av_get_pix_fmt_name(dst->format));
  164. goto error;
  165. }
  166. }
  167. time = av_gettime_relative() - time;
  168. if (sws_scale_frame(sws[2], out, dst) < 0) {
  169. fprintf(stderr, "Failed %s ---> %s\n", av_get_pix_fmt_name(dst->format),
  170. av_get_pix_fmt_name(out->format));
  171. goto error;
  172. }
  173. get_mse(mse, out, ref, comps);
  174. printf("%s %dx%d -> %s %3dx%3d, flags=%u dither=%u, "
  175. "MSE={%5d %5d %5d %5d}\n",
  176. av_get_pix_fmt_name(src->format), src->width, src->height,
  177. av_get_pix_fmt_name(dst->format), dst->width, dst->height,
  178. mode.flags, mode.dither,
  179. mse[0], mse[1], mse[2], mse[3]);
  180. if (!mse_ref) {
  181. /* Compare against the legacy swscale API as a reference */
  182. time_ref = av_gettime_relative();
  183. if (scale_legacy(dst, src, mode, opts) < 0) {
  184. fprintf(stderr, "Failed ref %s ---> %s\n", av_get_pix_fmt_name(src->format),
  185. av_get_pix_fmt_name(dst->format));
  186. goto error;
  187. }
  188. time_ref = av_gettime_relative() - time_ref;
  189. if (sws_scale_frame(sws[2], out, dst) < 0)
  190. goto error;
  191. get_mse(mse_sws, out, ref, comps);
  192. mse_ref = mse_sws;
  193. }
  194. for (int i = 0; i < 4; i++) {
  195. if (mse[i] > mse_ref[i]) {
  196. int bad = mse[i] > mse_ref[i] * 1.02 + 1;
  197. printf("\033[1;31m %s, ref MSE={%5d %5d %5d %5d}\033[0m\n",
  198. bad ? "WORSE" : "worse",
  199. mse_ref[0], mse_ref[1], mse_ref[2], mse_ref[3]);
  200. if (bad)
  201. goto error;
  202. break;
  203. }
  204. }
  205. if (opts.bench && time_ref) {
  206. printf(" time=%"PRId64" us, ref=%"PRId64" us, speedup=%.3fx %s\n",
  207. time / opts.iters, time_ref / opts.iters,
  208. (double) time_ref / time,
  209. time <= time_ref ? "faster" : "\033[1;33mslower\033[0m");
  210. } else if (opts.bench) {
  211. printf(" time=%"PRId64" us\n", time / opts.iters);
  212. }
  213. fflush(stdout);
  214. ret = 0; /* fall through */
  215. error:
  216. av_frame_free(&src);
  217. av_frame_free(&dst);
  218. av_frame_free(&out);
  219. return ret;
  220. }
  221. static int run_self_tests(const AVFrame *ref, struct options opts)
  222. {
  223. const int dst_w[] = { opts.w, opts.w - opts.w / 3, opts.w + opts.w / 3 };
  224. const int dst_h[] = { opts.h, opts.h - opts.h / 3, opts.h + opts.h / 3 };
  225. enum AVPixelFormat src_fmt, dst_fmt,
  226. src_fmt_min = 0,
  227. dst_fmt_min = 0,
  228. src_fmt_max = AV_PIX_FMT_NB - 1,
  229. dst_fmt_max = AV_PIX_FMT_NB - 1;
  230. if (opts.src_fmt != AV_PIX_FMT_NONE)
  231. src_fmt_min = src_fmt_max = opts.src_fmt;
  232. if (opts.dst_fmt != AV_PIX_FMT_NONE)
  233. dst_fmt_min = dst_fmt_max = opts.dst_fmt;
  234. for (src_fmt = src_fmt_min; src_fmt <= src_fmt_max; src_fmt++) {
  235. if (!sws_test_format(src_fmt, 0) || !sws_test_format(src_fmt, 1))
  236. continue;
  237. for (dst_fmt = dst_fmt_min; dst_fmt <= dst_fmt_max; dst_fmt++) {
  238. if (!sws_test_format(dst_fmt, 0) || !sws_test_format(dst_fmt, 1))
  239. continue;
  240. for (int h = 0; h < FF_ARRAY_ELEMS(dst_h); h++)
  241. for (int w = 0; w < FF_ARRAY_ELEMS(dst_w); w++)
  242. for (int m = 0; m < FF_ARRAY_ELEMS(modes); m++) {
  243. if (ff_sfc64_get(&prng_state) > UINT64_MAX * opts.prob)
  244. continue;
  245. if (run_test(src_fmt, dst_fmt, dst_w[w], dst_h[h],
  246. modes[m], opts, ref, NULL) < 0)
  247. return -1;
  248. }
  249. }
  250. }
  251. return 0;
  252. }
  253. static int run_file_tests(const AVFrame *ref, FILE *fp, struct options opts)
  254. {
  255. char buf[256];
  256. int ret;
  257. while (fgets(buf, sizeof(buf), fp)) {
  258. char src_fmt_str[20], dst_fmt_str[20];
  259. enum AVPixelFormat src_fmt;
  260. enum AVPixelFormat dst_fmt;
  261. int sw, sh, dw, dh, mse[4];
  262. struct mode mode;
  263. ret = sscanf(buf,
  264. " %20s %dx%d -> %20s %dx%d, flags=%u dither=%u, "
  265. "MSE={%d %d %d %d}\n",
  266. src_fmt_str, &sw, &sh, dst_fmt_str, &dw, &dh,
  267. &mode.flags, &mode.dither,
  268. &mse[0], &mse[1], &mse[2], &mse[3]);
  269. if (ret != 12) {
  270. printf("%s", buf);
  271. continue;
  272. }
  273. src_fmt = av_get_pix_fmt(src_fmt_str);
  274. dst_fmt = av_get_pix_fmt(dst_fmt_str);
  275. if (src_fmt == AV_PIX_FMT_NONE || dst_fmt == AV_PIX_FMT_NONE ||
  276. sw != ref->width || sh != ref->height || dw > 8192 || dh > 8192 ||
  277. mode.dither >= SWS_DITHER_NB) {
  278. fprintf(stderr, "malformed input file\n");
  279. return -1;
  280. }
  281. if (opts.src_fmt != AV_PIX_FMT_NONE && src_fmt != opts.src_fmt ||
  282. opts.dst_fmt != AV_PIX_FMT_NONE && dst_fmt != opts.dst_fmt)
  283. continue;
  284. if (run_test(src_fmt, dst_fmt, dw, dh, mode, opts, ref, mse) < 0)
  285. return -1;
  286. }
  287. return 0;
  288. }
  289. int main(int argc, char **argv)
  290. {
  291. struct options opts = {
  292. .src_fmt = AV_PIX_FMT_NONE,
  293. .dst_fmt = AV_PIX_FMT_NONE,
  294. .w = 96,
  295. .h = 96,
  296. .threads = 1,
  297. .iters = 1,
  298. .prob = 1.0,
  299. };
  300. AVFrame *rgb = NULL, *ref = NULL;
  301. FILE *fp = NULL;
  302. AVLFG rand;
  303. int ret = -1;
  304. for (int i = 1; i < argc; i += 2) {
  305. if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "--help")) {
  306. fprintf(stderr,
  307. "swscale [options...]\n"
  308. " -help\n"
  309. " This text\n"
  310. " -ref <file>\n"
  311. " Uses file as reference to compare tests againsts. Tests that have become worse will contain the string worse or WORSE\n"
  312. " -p <number between 0.0 and 1.0>\n"
  313. " The percentage of tests or comparisons to perform. Doing all tests will take long and generate over a hundred MB text output\n"
  314. " It is often convenient to perform a random subset\n"
  315. " -dst <pixfmt>\n"
  316. " Only test the specified destination pixel format\n"
  317. " -src <pixfmt>\n"
  318. " Only test the specified source pixel format\n"
  319. " -bench <iters>\n"
  320. " Run benchmarks with the specified number of iterations. This mode also increases the size of the test images\n"
  321. " -threads <threads>\n"
  322. " Use the specified number of threads\n"
  323. " -cpuflags <cpuflags>\n"
  324. " Uses the specified cpuflags in the tests\n"
  325. );
  326. return 0;
  327. }
  328. if (argv[i][0] != '-' || i + 1 == argc)
  329. goto bad_option;
  330. if (!strcmp(argv[i], "-ref")) {
  331. fp = fopen(argv[i + 1], "r");
  332. if (!fp) {
  333. fprintf(stderr, "could not open '%s'\n", argv[i + 1]);
  334. goto error;
  335. }
  336. } else if (!strcmp(argv[i], "-cpuflags")) {
  337. unsigned flags = av_get_cpu_flags();
  338. int res = av_parse_cpu_caps(&flags, argv[i + 1]);
  339. if (res < 0) {
  340. fprintf(stderr, "invalid cpu flags %s\n", argv[i + 1]);
  341. goto error;
  342. }
  343. av_force_cpu_flags(flags);
  344. } else if (!strcmp(argv[i], "-src")) {
  345. opts.src_fmt = av_get_pix_fmt(argv[i + 1]);
  346. if (opts.src_fmt == AV_PIX_FMT_NONE) {
  347. fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
  348. goto error;
  349. }
  350. } else if (!strcmp(argv[i], "-dst")) {
  351. opts.dst_fmt = av_get_pix_fmt(argv[i + 1]);
  352. if (opts.dst_fmt == AV_PIX_FMT_NONE) {
  353. fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
  354. goto error;
  355. }
  356. } else if (!strcmp(argv[i], "-bench")) {
  357. opts.bench = 1;
  358. opts.iters = atoi(argv[i + 1]);
  359. opts.iters = FFMAX(opts.iters, 1);
  360. opts.w = 1920;
  361. opts.h = 1080;
  362. } else if (!strcmp(argv[i], "-threads")) {
  363. opts.threads = atoi(argv[i + 1]);
  364. } else if (!strcmp(argv[i], "-p")) {
  365. opts.prob = atof(argv[i + 1]);
  366. } else {
  367. bad_option:
  368. fprintf(stderr, "bad option or argument missing (%s) see -help\n", argv[i]);
  369. goto error;
  370. }
  371. }
  372. ff_sfc64_init(&prng_state, 0, 0, 0, 12);
  373. av_lfg_init(&rand, 1);
  374. for (int i = 0; i < 3; i++) {
  375. sws[i] = sws_alloc_context();
  376. if (!sws[i])
  377. goto error;
  378. sws[i]->flags = SWS_BILINEAR;
  379. }
  380. rgb = av_frame_alloc();
  381. if (!rgb)
  382. goto error;
  383. rgb->width = opts.w / 12;
  384. rgb->height = opts.h / 12;
  385. rgb->format = AV_PIX_FMT_RGBA;
  386. if (av_frame_get_buffer(rgb, 32) < 0)
  387. goto error;
  388. for (int y = 0; y < rgb->height; y++) {
  389. for (int x = 0; x < rgb->width; x++) {
  390. for (int c = 0; c < 4; c++)
  391. rgb->data[0][y * rgb->linesize[0] + x * 4 + c] = av_lfg_get(&rand);
  392. }
  393. }
  394. ref = av_frame_alloc();
  395. if (!ref)
  396. goto error;
  397. ref->width = opts.w;
  398. ref->height = opts.h;
  399. ref->format = AV_PIX_FMT_YUVA420P;
  400. if (sws_scale_frame(sws[0], ref, rgb) < 0)
  401. goto error;
  402. ret = fp ? run_file_tests(ref, fp, opts)
  403. : run_self_tests(ref, opts);
  404. /* fall through */
  405. error:
  406. for (int i = 0; i < 3; i++)
  407. sws_free_context(&sws[i]);
  408. av_frame_free(&rgb);
  409. av_frame_free(&ref);
  410. if (fp)
  411. fclose(fp);
  412. return ret;
  413. }