swscale.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * Copyright (C) 2003-2011 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <inttypes.h>
  24. #include <stdarg.h>
  25. #undef HAVE_AV_CONFIG_H
  26. #include "libavutil/cpu.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/mem.h"
  29. #include "libavutil/avutil.h"
  30. #include "libavutil/crc.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "libavutil/lfg.h"
  34. #include "libavutil/sfc64.h"
  35. #include "libswscale/swscale.h"
  36. /* HACK Duplicated from swscale_internal.h.
  37. * Should be removed when a cleaner pixel format system exists. */
  38. #define isGray(x) \
  39. ((x) == AV_PIX_FMT_GRAY8 || \
  40. (x) == AV_PIX_FMT_YA8 || \
  41. (x) == AV_PIX_FMT_GRAY16BE || \
  42. (x) == AV_PIX_FMT_GRAY16LE || \
  43. (x) == AV_PIX_FMT_YA16BE || \
  44. (x) == AV_PIX_FMT_YA16LE)
  45. #define hasChroma(x) \
  46. (!(isGray(x) || \
  47. (x) == AV_PIX_FMT_MONOBLACK || \
  48. (x) == AV_PIX_FMT_MONOWHITE))
  49. static av_always_inline int isALPHA(enum AVPixelFormat pix_fmt)
  50. {
  51. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  52. return desc->flags & AV_PIX_FMT_FLAG_ALPHA;
  53. }
  54. static double prob = 1;
  55. FFSFC64 prng_state;
  56. static uint64_t getSSD(const uint8_t *src1, const uint8_t *src2,
  57. int stride1, int stride2, int w, int h)
  58. {
  59. int x, y;
  60. uint64_t ssd = 0;
  61. for (y = 0; y < h; y++) {
  62. for (x = 0; x < w; x++) {
  63. int d = src1[x + y * stride1] - src2[x + y * stride2];
  64. ssd += d * d;
  65. }
  66. }
  67. return ssd;
  68. }
  69. static uint64_t getSSD0(int ref, const uint8_t *src1, int stride1,
  70. int w, int h)
  71. {
  72. int x, y;
  73. uint64_t ssd = 0;
  74. for (y = 0; y < h; y++) {
  75. for (x = 0; x < w; x++) {
  76. int d = src1[x + y * stride1] - ref;
  77. ssd += d * d;
  78. }
  79. }
  80. return ssd;
  81. }
  82. struct Results {
  83. uint64_t ssdY;
  84. uint64_t ssdU;
  85. uint64_t ssdV;
  86. uint64_t ssdA;
  87. uint32_t crc;
  88. };
  89. // test by ref -> src -> dst -> out & compare out against ref
  90. // ref & out are YV12
  91. static int doTest(const uint8_t * const ref[4], int refStride[4], int w, int h,
  92. enum AVPixelFormat srcFormat, enum AVPixelFormat dstFormat,
  93. int srcW, int srcH, int dstW, int dstH, int flags,
  94. struct Results *r)
  95. {
  96. const AVPixFmtDescriptor *desc_yuva420p = av_pix_fmt_desc_get(AV_PIX_FMT_YUVA420P);
  97. const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(srcFormat);
  98. const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(dstFormat);
  99. static enum AVPixelFormat cur_srcFormat;
  100. static int cur_srcW, cur_srcH;
  101. static const uint8_t *src[4];
  102. static int srcStride[4];
  103. uint8_t *dst[4] = { 0 };
  104. uint8_t *out[4] = { 0 };
  105. int dstStride[4] = {0};
  106. int i;
  107. uint64_t ssdY, ssdU = 0, ssdV = 0, ssdA = 0;
  108. struct SwsContext *dstContext = NULL, *outContext = NULL;
  109. uint32_t crc = 0;
  110. int res = 0;
  111. if (ff_sfc64_get(&prng_state) > UINT64_MAX * prob)
  112. return 0;
  113. if (cur_srcFormat != srcFormat || cur_srcW != srcW || cur_srcH != srcH) {
  114. struct SwsContext *srcContext = NULL;
  115. int p;
  116. for (p = 0; p < 4; p++)
  117. av_freep(&src[p]);
  118. res = av_image_fill_linesizes(srcStride, srcFormat, srcW);
  119. if (res < 0) {
  120. fprintf(stderr, "av_image_fill_linesizes failed\n");
  121. goto end;
  122. }
  123. for (p = 0; p < 4; p++) {
  124. srcStride[p] = FFALIGN(srcStride[p], 16);
  125. if (srcStride[p])
  126. src[p] = av_mallocz(srcStride[p] * srcH + 16);
  127. if (srcStride[p] && !src[p]) {
  128. perror("Malloc");
  129. res = -1;
  130. goto end;
  131. }
  132. }
  133. srcContext = sws_getContext(w, h, AV_PIX_FMT_YUVA420P, srcW, srcH,
  134. srcFormat, SWS_BILINEAR, NULL, NULL, NULL);
  135. if (!srcContext) {
  136. fprintf(stderr, "Failed to get %s ---> %s\n",
  137. desc_yuva420p->name,
  138. desc_src->name);
  139. res = -1;
  140. goto end;
  141. }
  142. sws_scale(srcContext, ref, refStride, 0, h,
  143. (uint8_t * const *) src, srcStride);
  144. sws_freeContext(srcContext);
  145. cur_srcFormat = srcFormat;
  146. cur_srcW = srcW;
  147. cur_srcH = srcH;
  148. }
  149. res = av_image_fill_linesizes(dstStride, dstFormat, dstW);
  150. if (res < 0) {
  151. fprintf(stderr, "av_image_fill_linesizes failed\n");
  152. goto end;
  153. }
  154. for (i = 0; i < 4; i++) {
  155. /* Image buffers passed into libswscale can be allocated any way you
  156. * prefer, as long as they're aligned enough for the architecture, and
  157. * they're freed appropriately (such as using av_free for buffers
  158. * allocated with av_malloc). */
  159. /* An extra 16 bytes is being allocated because some scalers may write
  160. * out of bounds. */
  161. dstStride[i] = FFALIGN(dstStride[i], 16);
  162. if (dstStride[i])
  163. dst[i] = av_mallocz(dstStride[i] * dstH + 16);
  164. if (dstStride[i] && !dst[i]) {
  165. perror("Malloc");
  166. res = -1;
  167. goto end;
  168. }
  169. }
  170. dstContext = sws_alloc_context();
  171. if (!dstContext) {
  172. fprintf(stderr, "Failed to alloc %s ---> %s\n",
  173. desc_src->name, desc_dst->name);
  174. res = -1;
  175. goto end;
  176. }
  177. av_opt_set_int(dstContext, "sws_flags", flags, 0);
  178. av_opt_set_int(dstContext, "srcw", srcW, 0);
  179. av_opt_set_int(dstContext, "srch", srcH, 0);
  180. av_opt_set_int(dstContext, "dstw", dstW, 0);
  181. av_opt_set_int(dstContext, "dsth", dstH, 0);
  182. av_opt_set_int(dstContext, "src_format", srcFormat, 0);
  183. av_opt_set_int(dstContext, "dst_format", dstFormat, 0);
  184. av_opt_set(dstContext, "alphablend", "none", 0);
  185. if (sws_init_context(dstContext, NULL, NULL) < 0) {
  186. sws_freeContext(dstContext);
  187. fprintf(stderr, "Failed to init %s ---> %s\n",
  188. desc_src->name, desc_dst->name);
  189. res = -1;
  190. goto end;
  191. }
  192. printf(" %s %dx%d -> %s %3dx%3d flags=%2d",
  193. desc_src->name, srcW, srcH,
  194. desc_dst->name, dstW, dstH,
  195. flags);
  196. fflush(stdout);
  197. sws_scale(dstContext, (const uint8_t * const*)src, srcStride, 0, srcH, dst, dstStride);
  198. for (i = 0; i < 4 && dstStride[i]; i++)
  199. crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), crc, dst[i],
  200. dstStride[i] * dstH);
  201. if (r && crc == r->crc) {
  202. ssdY = r->ssdY;
  203. ssdU = r->ssdU;
  204. ssdV = r->ssdV;
  205. ssdA = r->ssdA;
  206. } else {
  207. for (i = 0; i < 4; i++) {
  208. refStride[i] = FFALIGN(refStride[i], 16);
  209. if (refStride[i])
  210. out[i] = av_mallocz(refStride[i] * h);
  211. if (refStride[i] && !out[i]) {
  212. perror("Malloc");
  213. res = -1;
  214. goto end;
  215. }
  216. }
  217. outContext = sws_getContext(dstW, dstH, dstFormat, w, h,
  218. AV_PIX_FMT_YUVA420P, SWS_BILINEAR,
  219. NULL, NULL, NULL);
  220. if (!outContext) {
  221. fprintf(stderr, "Failed to get %s ---> %s\n",
  222. desc_dst->name,
  223. desc_yuva420p->name);
  224. res = -1;
  225. goto end;
  226. }
  227. sws_scale(outContext, (const uint8_t * const *) dst, dstStride, 0, dstH,
  228. out, refStride);
  229. ssdY = getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
  230. if (hasChroma(srcFormat) && hasChroma(dstFormat)) {
  231. //FIXME check that output is really gray
  232. ssdU = getSSD(ref[1], out[1], refStride[1], refStride[1],
  233. (w + 1) >> 1, (h + 1) >> 1);
  234. ssdV = getSSD(ref[2], out[2], refStride[2], refStride[2],
  235. (w + 1) >> 1, (h + 1) >> 1);
  236. } else {
  237. ssdU = getSSD0(128, out[1], refStride[1],
  238. (w + 1) >> 1, (h + 1) >> 1);
  239. ssdV = getSSD0(128, out[2], refStride[2],
  240. (w + 1) >> 1, (h + 1) >> 1);
  241. }
  242. if (isALPHA(srcFormat) && isALPHA(dstFormat)) {
  243. ssdA = getSSD(ref[3], out[3], refStride[3], refStride[3], w, h);
  244. } else {
  245. ssdA = getSSD0(0xFF, out[3], refStride[3], w, h);
  246. }
  247. ssdY /= w * h;
  248. ssdU /= w * h / 4;
  249. ssdV /= w * h / 4;
  250. ssdA /= w * h;
  251. sws_freeContext(outContext);
  252. for (i = 0; i < 4; i++)
  253. if (refStride[i])
  254. av_free(out[i]);
  255. }
  256. if(r){
  257. if(ssdY>r->ssdY*1.02+1 || ssdU>r->ssdU*1.02+1 || ssdV>r->ssdV*1.02+1|| ssdA>r->ssdA*1.02+1)
  258. printf("WORSE SSD=%5"PRId64",%5"PRId64",%5"PRId64",%5"PRId64"",
  259. r->ssdY, r->ssdU, r->ssdV, r->ssdA);
  260. else if(ssdY>r->ssdY || ssdU>r->ssdU || ssdV>r->ssdV|| ssdA>r->ssdA)
  261. printf("worse SSD=%5"PRId64",%5"PRId64",%5"PRId64",%5"PRId64"",
  262. r->ssdY, r->ssdU, r->ssdV, r->ssdA);
  263. }
  264. printf(" CRC=%08x SSD=%5"PRId64 ",%5"PRId64 ",%5"PRId64 ",%5"PRId64 "\n",
  265. crc, ssdY, ssdU, ssdV, ssdA);
  266. end:
  267. sws_freeContext(dstContext);
  268. for (i = 0; i < 4; i++)
  269. if (dstStride[i])
  270. av_free(dst[i]);
  271. return !!res;
  272. }
  273. static void selfTest(const uint8_t * const ref[4], int refStride[4],
  274. int w, int h,
  275. enum AVPixelFormat srcFormat_in,
  276. enum AVPixelFormat dstFormat_in)
  277. {
  278. const int flags[] = { SWS_FAST_BILINEAR,
  279. SWS_BILINEAR, SWS_BICUBIC,
  280. SWS_X|SWS_BITEXACT , SWS_POINT , SWS_AREA|SWS_ACCURATE_RND,
  281. SWS_BICUBIC|SWS_FULL_CHR_H_INT|SWS_FULL_CHR_H_INP, 0};
  282. const int srcW = w;
  283. const int srcH = h;
  284. const int dstW[] = { srcW - srcW / 3, srcW, srcW + srcW / 3, 0 };
  285. const int dstH[] = { srcH - srcH / 3, srcH, srcH + srcH / 3, 0 };
  286. enum AVPixelFormat srcFormat, dstFormat;
  287. const AVPixFmtDescriptor *desc_src, *desc_dst;
  288. for (srcFormat = srcFormat_in != AV_PIX_FMT_NONE ? srcFormat_in : 0;
  289. srcFormat < AV_PIX_FMT_NB; srcFormat++) {
  290. if (!sws_isSupportedInput(srcFormat) ||
  291. !sws_isSupportedOutput(srcFormat))
  292. continue;
  293. desc_src = av_pix_fmt_desc_get(srcFormat);
  294. for (dstFormat = dstFormat_in != AV_PIX_FMT_NONE ? dstFormat_in : 0;
  295. dstFormat < AV_PIX_FMT_NB; dstFormat++) {
  296. int i, j, k;
  297. int res = 0;
  298. if (!sws_isSupportedInput(dstFormat) ||
  299. !sws_isSupportedOutput(dstFormat))
  300. continue;
  301. desc_dst = av_pix_fmt_desc_get(dstFormat);
  302. printf("%s -> %s\n", desc_src->name, desc_dst->name);
  303. fflush(stdout);
  304. for (k = 0; flags[k] && !res; k++)
  305. for (i = 0; dstW[i] && !res; i++)
  306. for (j = 0; dstH[j] && !res; j++)
  307. res = doTest(ref, refStride, w, h,
  308. srcFormat, dstFormat,
  309. srcW, srcH, dstW[i], dstH[j], flags[k],
  310. NULL);
  311. if (dstFormat_in != AV_PIX_FMT_NONE)
  312. break;
  313. }
  314. if (srcFormat_in != AV_PIX_FMT_NONE)
  315. break;
  316. }
  317. }
  318. static int fileTest(const uint8_t * const ref[4], int refStride[4],
  319. int w, int h, FILE *fp,
  320. enum AVPixelFormat srcFormat_in,
  321. enum AVPixelFormat dstFormat_in)
  322. {
  323. char buf[256];
  324. while (fgets(buf, sizeof(buf), fp)) {
  325. struct Results r;
  326. enum AVPixelFormat srcFormat;
  327. char srcStr[21];
  328. int srcW = 0, srcH = 0;
  329. enum AVPixelFormat dstFormat;
  330. char dstStr[21];
  331. int dstW = 0, dstH = 0;
  332. int flags;
  333. int ret;
  334. ret = sscanf(buf,
  335. " %20s %dx%d -> %20s %dx%d flags=%d CRC=%x"
  336. " SSD=%"SCNu64 ", %"SCNu64 ", %"SCNu64 ", %"SCNu64 "\n",
  337. srcStr, &srcW, &srcH, dstStr, &dstW, &dstH,
  338. &flags, &r.crc, &r.ssdY, &r.ssdU, &r.ssdV, &r.ssdA);
  339. if (ret != 12) {
  340. srcStr[0] = dstStr[0] = 0;
  341. ret = sscanf(buf, "%20s -> %20s\n", srcStr, dstStr);
  342. }
  343. srcFormat = av_get_pix_fmt(srcStr);
  344. dstFormat = av_get_pix_fmt(dstStr);
  345. if (srcFormat == AV_PIX_FMT_NONE || dstFormat == AV_PIX_FMT_NONE ||
  346. srcW > 8192U || srcH > 8192U || dstW > 8192U || dstH > 8192U) {
  347. fprintf(stderr, "malformed input file\n");
  348. return -1;
  349. }
  350. if ((srcFormat_in != AV_PIX_FMT_NONE && srcFormat_in != srcFormat) ||
  351. (dstFormat_in != AV_PIX_FMT_NONE && dstFormat_in != dstFormat))
  352. continue;
  353. if (ret != 12) {
  354. printf("%s", buf);
  355. continue;
  356. }
  357. doTest(ref, refStride, w, h,
  358. srcFormat, dstFormat,
  359. srcW, srcH, dstW, dstH, flags,
  360. &r);
  361. }
  362. return 0;
  363. }
  364. #define W 96
  365. #define H 96
  366. int main(int argc, char **argv)
  367. {
  368. enum AVPixelFormat srcFormat = AV_PIX_FMT_NONE;
  369. enum AVPixelFormat dstFormat = AV_PIX_FMT_NONE;
  370. uint8_t *rgb_data = av_malloc(W * H * 4);
  371. const uint8_t * const rgb_src[4] = { rgb_data, NULL, NULL, NULL };
  372. int rgb_stride[4] = { 4 * W, 0, 0, 0 };
  373. uint8_t *data = av_malloc(4 * W * H);
  374. const uint8_t * const src[4] = { data, data + W * H, data + W * H * 2, data + W * H * 3 };
  375. int stride[4] = { W, W, W, W };
  376. int x, y;
  377. struct SwsContext *sws;
  378. AVLFG rand;
  379. int res = -1;
  380. int i;
  381. FILE *fp = NULL;
  382. if (!rgb_data || !data)
  383. return -1;
  384. for (i = 1; i < argc; i += 2) {
  385. if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "--help")) {
  386. fprintf(stderr,
  387. "swscale [options...]\n"
  388. " -help\n"
  389. " This text\n"
  390. " -ref <file>\n"
  391. " Uses file as reference to compae tests againsts. Tests that have become worse will contain the string worse or WORSE\n"
  392. " -p <number between 0.0 and 1.0>\n"
  393. " The percentage of tests or comparissions to perform. Doing all tests will take long and generate over a hundread MB text output\n"
  394. " It is often convenient to perform a random subset\n"
  395. " -dst <pixfmt>\n"
  396. " Only test the specified destination pixel format\n"
  397. " -src <pixfmt>\n"
  398. " Only test the specified source pixel format\n"
  399. " -cpuflags <cpuflags>\n"
  400. " Uses the specified cpuflags in teh tests\n"
  401. );
  402. goto error;
  403. }
  404. if (argv[i][0] != '-' || i + 1 == argc)
  405. goto bad_option;
  406. if (!strcmp(argv[i], "-ref")) {
  407. fp = fopen(argv[i + 1], "r");
  408. if (!fp) {
  409. fprintf(stderr, "could not open '%s'\n", argv[i + 1]);
  410. goto error;
  411. }
  412. } else if (!strcmp(argv[i], "-cpuflags")) {
  413. unsigned flags = av_get_cpu_flags();
  414. int ret = av_parse_cpu_caps(&flags, argv[i + 1]);
  415. if (ret < 0) {
  416. fprintf(stderr, "invalid cpu flags %s\n", argv[i + 1]);
  417. return ret;
  418. }
  419. av_force_cpu_flags(flags);
  420. } else if (!strcmp(argv[i], "-src")) {
  421. srcFormat = av_get_pix_fmt(argv[i + 1]);
  422. if (srcFormat == AV_PIX_FMT_NONE) {
  423. fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
  424. return -1;
  425. }
  426. } else if (!strcmp(argv[i], "-dst")) {
  427. dstFormat = av_get_pix_fmt(argv[i + 1]);
  428. if (dstFormat == AV_PIX_FMT_NONE) {
  429. fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
  430. return -1;
  431. }
  432. } else if (!strcmp(argv[i], "-p")) {
  433. prob = atof(argv[i + 1]);
  434. } else {
  435. bad_option:
  436. fprintf(stderr, "bad option or argument missing (%s) see -help\n", argv[i]);
  437. goto error;
  438. }
  439. }
  440. ff_sfc64_init(&prng_state, 0, 0, 0, 12);
  441. sws = sws_getContext(W / 12, H / 12, AV_PIX_FMT_RGB32, W, H,
  442. AV_PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
  443. av_lfg_init(&rand, 1);
  444. for (y = 0; y < H; y++)
  445. for (x = 0; x < W * 4; x++)
  446. rgb_data[ x + y * 4 * W] = av_lfg_get(&rand);
  447. res = sws_scale(sws, rgb_src, rgb_stride, 0, H / 12, (uint8_t * const *) src, stride);
  448. if (res < 0 || res != H) {
  449. res = -1;
  450. goto error;
  451. }
  452. sws_freeContext(sws);
  453. av_free(rgb_data);
  454. if(fp) {
  455. res = fileTest(src, stride, W, H, fp, srcFormat, dstFormat);
  456. fclose(fp);
  457. } else {
  458. selfTest(src, stride, W, H, srcFormat, dstFormat);
  459. res = 0;
  460. }
  461. error:
  462. av_free(data);
  463. return res;
  464. }