swscale-test.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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/imgutils.h"
  27. #include "libavutil/mem.h"
  28. #include "libavutil/avutil.h"
  29. #include "libavutil/crc.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/lfg.h"
  32. #include "swscale.h"
  33. /* HACK Duplicated from swscale_internal.h.
  34. * Should be removed when a cleaner pixel format system exists. */
  35. #define isGray(x) \
  36. ((x) == AV_PIX_FMT_GRAY8 || \
  37. (x) == AV_PIX_FMT_Y400A || \
  38. (x) == AV_PIX_FMT_GRAY16BE || \
  39. (x) == AV_PIX_FMT_GRAY16LE)
  40. #define hasChroma(x) \
  41. (!(isGray(x) || \
  42. (x) == AV_PIX_FMT_MONOBLACK || \
  43. (x) == AV_PIX_FMT_MONOWHITE))
  44. #define isALPHA(x) \
  45. ((x) == AV_PIX_FMT_BGR32 || \
  46. (x) == AV_PIX_FMT_BGR32_1 || \
  47. (x) == AV_PIX_FMT_RGB32 || \
  48. (x) == AV_PIX_FMT_RGB32_1 || \
  49. (x) == AV_PIX_FMT_YUVA420P)
  50. static uint64_t getSSD(const uint8_t *src1, const uint8_t *src2, int stride1,
  51. int stride2, int w, int h)
  52. {
  53. int x, y;
  54. uint64_t ssd = 0;
  55. for (y = 0; y < h; y++) {
  56. for (x = 0; x < w; x++) {
  57. int d = src1[x + y * stride1] - src2[x + y * stride2];
  58. ssd += d * d;
  59. }
  60. }
  61. return ssd;
  62. }
  63. struct Results {
  64. uint64_t ssdY;
  65. uint64_t ssdU;
  66. uint64_t ssdV;
  67. uint64_t ssdA;
  68. uint32_t crc;
  69. };
  70. // test by ref -> src -> dst -> out & compare out against ref
  71. // ref & out are YV12
  72. static int doTest(uint8_t *ref[4], int refStride[4], int w, int h,
  73. enum AVPixelFormat srcFormat, enum AVPixelFormat dstFormat,
  74. int srcW, int srcH, int dstW, int dstH, int flags,
  75. struct Results *r)
  76. {
  77. const AVPixFmtDescriptor *desc_yuva420p = av_pix_fmt_desc_get(AV_PIX_FMT_YUVA420P);
  78. const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(srcFormat);
  79. const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(dstFormat);
  80. static enum AVPixelFormat cur_srcFormat;
  81. static int cur_srcW, cur_srcH;
  82. static uint8_t *src[4];
  83. static int srcStride[4];
  84. uint8_t *dst[4] = { 0 };
  85. uint8_t *out[4] = { 0 };
  86. int dstStride[4] = {0};
  87. int i;
  88. uint64_t ssdY, ssdU = 0, ssdV = 0, ssdA = 0;
  89. struct SwsContext *dstContext = NULL, *outContext = NULL;
  90. uint32_t crc = 0;
  91. int res = 0;
  92. if (cur_srcFormat != srcFormat || cur_srcW != srcW || cur_srcH != srcH) {
  93. struct SwsContext *srcContext = NULL;
  94. int p;
  95. for (p = 0; p < 4; p++)
  96. av_freep(&src[p]);
  97. av_image_fill_linesizes(srcStride, srcFormat, srcW);
  98. for (p = 0; p < 4; p++) {
  99. srcStride[p] = FFALIGN(srcStride[p], 16);
  100. if (srcStride[p])
  101. src[p] = av_mallocz(srcStride[p] * srcH + 16);
  102. if (srcStride[p] && !src[p]) {
  103. perror("Malloc");
  104. res = -1;
  105. goto end;
  106. }
  107. }
  108. srcContext = sws_getContext(w, h, AV_PIX_FMT_YUVA420P, srcW, srcH,
  109. srcFormat, SWS_BILINEAR, NULL, NULL, NULL);
  110. if (!srcContext) {
  111. fprintf(stderr, "Failed to get %s ---> %s\n",
  112. desc_yuva420p->name,
  113. desc_src->name);
  114. res = -1;
  115. goto end;
  116. }
  117. sws_scale(srcContext, (const uint8_t * const*)ref, refStride, 0, h, src, srcStride);
  118. sws_freeContext(srcContext);
  119. cur_srcFormat = srcFormat;
  120. cur_srcW = srcW;
  121. cur_srcH = srcH;
  122. }
  123. av_image_fill_linesizes(dstStride, dstFormat, dstW);
  124. for (i = 0; i < 4; i++) {
  125. /* Image buffers passed into libswscale can be allocated any way you
  126. * prefer, as long as they're aligned enough for the architecture, and
  127. * they're freed appropriately (such as using av_free for buffers
  128. * allocated with av_malloc). */
  129. /* An extra 16 bytes is being allocated because some scalers may write
  130. * out of bounds. */
  131. dstStride[i] = FFALIGN(dstStride[i], 16);
  132. if (dstStride[i])
  133. dst[i] = av_mallocz(dstStride[i] * dstH + 16);
  134. if (dstStride[i] && !dst[i]) {
  135. perror("Malloc");
  136. res = -1;
  137. goto end;
  138. }
  139. }
  140. dstContext = sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat,
  141. flags, NULL, NULL, NULL);
  142. if (!dstContext) {
  143. fprintf(stderr, "Failed to get %s ---> %s\n",
  144. desc_src->name, desc_dst->name);
  145. res = -1;
  146. goto end;
  147. }
  148. printf(" %s %dx%d -> %s %3dx%3d flags=%2d",
  149. desc_src->name, srcW, srcH,
  150. desc_dst->name, dstW, dstH,
  151. flags);
  152. fflush(stdout);
  153. sws_scale(dstContext, (const uint8_t * const*)src, srcStride, 0, srcH, dst, dstStride);
  154. for (i = 0; i < 4 && dstStride[i]; i++)
  155. crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), crc, dst[i],
  156. dstStride[i] * dstH);
  157. if (r && crc == r->crc) {
  158. ssdY = r->ssdY;
  159. ssdU = r->ssdU;
  160. ssdV = r->ssdV;
  161. ssdA = r->ssdA;
  162. } else {
  163. for (i = 0; i < 4; i++) {
  164. refStride[i] = FFALIGN(refStride[i], 16);
  165. if (refStride[i])
  166. out[i] = av_mallocz(refStride[i] * h);
  167. if (refStride[i] && !out[i]) {
  168. perror("Malloc");
  169. res = -1;
  170. goto end;
  171. }
  172. }
  173. outContext = sws_getContext(dstW, dstH, dstFormat, w, h,
  174. AV_PIX_FMT_YUVA420P, SWS_BILINEAR,
  175. NULL, NULL, NULL);
  176. if (!outContext) {
  177. fprintf(stderr, "Failed to get %s ---> %s\n",
  178. desc_dst->name,
  179. desc_yuva420p->name);
  180. res = -1;
  181. goto end;
  182. }
  183. sws_scale(outContext, (const uint8_t * const*)dst, dstStride, 0, dstH, out, refStride);
  184. ssdY = getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
  185. if (hasChroma(srcFormat) && hasChroma(dstFormat)) {
  186. //FIXME check that output is really gray
  187. ssdU = getSSD(ref[1], out[1], refStride[1], refStride[1],
  188. (w + 1) >> 1, (h + 1) >> 1);
  189. ssdV = getSSD(ref[2], out[2], refStride[2], refStride[2],
  190. (w + 1) >> 1, (h + 1) >> 1);
  191. }
  192. if (isALPHA(srcFormat) && isALPHA(dstFormat))
  193. ssdA = getSSD(ref[3], out[3], refStride[3], refStride[3], w, h);
  194. ssdY /= w * h;
  195. ssdU /= w * h / 4;
  196. ssdV /= w * h / 4;
  197. ssdA /= w * h;
  198. sws_freeContext(outContext);
  199. for (i = 0; i < 4; i++)
  200. if (refStride[i])
  201. av_free(out[i]);
  202. }
  203. printf(" CRC=%08x SSD=%5"PRId64 ",%5"PRId64 ",%5"PRId64 ",%5"PRId64 "\n",
  204. crc, ssdY, ssdU, ssdV, ssdA);
  205. end:
  206. sws_freeContext(dstContext);
  207. for (i = 0; i < 4; i++)
  208. if (dstStride[i])
  209. av_free(dst[i]);
  210. return res;
  211. }
  212. static void selfTest(uint8_t *ref[4], int refStride[4], int w, int h,
  213. enum AVPixelFormat srcFormat_in,
  214. enum AVPixelFormat dstFormat_in)
  215. {
  216. const int flags[] = { SWS_FAST_BILINEAR, SWS_BILINEAR, SWS_BICUBIC,
  217. SWS_X, SWS_POINT, SWS_AREA, 0 };
  218. const int srcW = w;
  219. const int srcH = h;
  220. const int dstW[] = { srcW - srcW / 3, srcW, srcW + srcW / 3, 0 };
  221. const int dstH[] = { srcH - srcH / 3, srcH, srcH + srcH / 3, 0 };
  222. enum AVPixelFormat srcFormat, dstFormat;
  223. const AVPixFmtDescriptor *desc_src, *desc_dst;
  224. for (srcFormat = srcFormat_in != AV_PIX_FMT_NONE ? srcFormat_in : 0;
  225. srcFormat < AV_PIX_FMT_NB; srcFormat++) {
  226. if (!sws_isSupportedInput(srcFormat) ||
  227. !sws_isSupportedOutput(srcFormat))
  228. continue;
  229. desc_src = av_pix_fmt_desc_get(srcFormat);
  230. for (dstFormat = dstFormat_in != AV_PIX_FMT_NONE ? dstFormat_in : 0;
  231. dstFormat < AV_PIX_FMT_NB; dstFormat++) {
  232. int i, j, k;
  233. int res = 0;
  234. if (!sws_isSupportedInput(dstFormat) ||
  235. !sws_isSupportedOutput(dstFormat))
  236. continue;
  237. desc_dst = av_pix_fmt_desc_get(dstFormat);
  238. printf("%s -> %s\n", desc_src->name, desc_dst->name);
  239. fflush(stdout);
  240. for (k = 0; flags[k] && !res; k++)
  241. for (i = 0; dstW[i] && !res; i++)
  242. for (j = 0; dstH[j] && !res; j++)
  243. res = doTest(ref, refStride, w, h,
  244. srcFormat, dstFormat,
  245. srcW, srcH, dstW[i], dstH[j], flags[k],
  246. NULL);
  247. if (dstFormat_in != AV_PIX_FMT_NONE)
  248. break;
  249. }
  250. if (srcFormat_in != AV_PIX_FMT_NONE)
  251. break;
  252. }
  253. }
  254. static int fileTest(uint8_t *ref[4], int refStride[4], int w, int h, FILE *fp,
  255. enum AVPixelFormat srcFormat_in,
  256. enum AVPixelFormat dstFormat_in)
  257. {
  258. char buf[256];
  259. while (fgets(buf, sizeof(buf), fp)) {
  260. struct Results r;
  261. enum AVPixelFormat srcFormat;
  262. char srcStr[12];
  263. int srcW, srcH;
  264. enum AVPixelFormat dstFormat;
  265. char dstStr[12];
  266. int dstW, dstH;
  267. int flags;
  268. int ret;
  269. ret = sscanf(buf,
  270. " %12s %dx%d -> %12s %dx%d flags=%d CRC=%x"
  271. " SSD=%"SCNd64 ", %"SCNd64 ", %"SCNd64 ", %"SCNd64 "\n",
  272. srcStr, &srcW, &srcH, dstStr, &dstW, &dstH,
  273. &flags, &r.crc, &r.ssdY, &r.ssdU, &r.ssdV, &r.ssdA);
  274. if (ret != 12) {
  275. srcStr[0] = dstStr[0] = 0;
  276. ret = sscanf(buf, "%12s -> %12s\n", srcStr, dstStr);
  277. }
  278. srcFormat = av_get_pix_fmt(srcStr);
  279. dstFormat = av_get_pix_fmt(dstStr);
  280. if (srcFormat == AV_PIX_FMT_NONE || dstFormat == AV_PIX_FMT_NONE ||
  281. srcW > 8192U || srcH > 8192U || dstW > 8192U || dstH > 8192U) {
  282. fprintf(stderr, "malformed input file\n");
  283. return -1;
  284. }
  285. if ((srcFormat_in != AV_PIX_FMT_NONE && srcFormat_in != srcFormat) ||
  286. (dstFormat_in != AV_PIX_FMT_NONE && dstFormat_in != dstFormat))
  287. continue;
  288. if (ret != 12) {
  289. printf("%s", buf);
  290. continue;
  291. }
  292. doTest(ref, refStride, w, h,
  293. srcFormat, dstFormat,
  294. srcW, srcH, dstW, dstH, flags,
  295. &r);
  296. }
  297. return 0;
  298. }
  299. #define W 96
  300. #define H 96
  301. int main(int argc, char **argv)
  302. {
  303. enum AVPixelFormat srcFormat = AV_PIX_FMT_NONE;
  304. enum AVPixelFormat dstFormat = AV_PIX_FMT_NONE;
  305. uint8_t *rgb_data = av_malloc(W * H * 4);
  306. const uint8_t * const rgb_src[4] = { rgb_data, NULL, NULL, NULL };
  307. int rgb_stride[4] = { 4 * W, 0, 0, 0 };
  308. uint8_t *data = av_malloc(4 * W * H);
  309. uint8_t *src[4] = { data, data + W * H, data + W * H * 2, data + W * H * 3 };
  310. int stride[4] = { W, W, W, W };
  311. int x, y;
  312. struct SwsContext *sws;
  313. AVLFG rand;
  314. int res = -1;
  315. int i;
  316. FILE *fp = NULL;
  317. if (!rgb_data || !data)
  318. return -1;
  319. for (i = 1; i < argc; i += 2) {
  320. if (argv[i][0] != '-' || i + 1 == argc)
  321. goto bad_option;
  322. if (!strcmp(argv[i], "-ref")) {
  323. fp = fopen(argv[i + 1], "r");
  324. if (!fp) {
  325. fprintf(stderr, "could not open '%s'\n", argv[i + 1]);
  326. goto error;
  327. }
  328. } else if (!strcmp(argv[i], "-src")) {
  329. srcFormat = av_get_pix_fmt(argv[i + 1]);
  330. if (srcFormat == AV_PIX_FMT_NONE) {
  331. fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
  332. return -1;
  333. }
  334. } else if (!strcmp(argv[i], "-dst")) {
  335. dstFormat = av_get_pix_fmt(argv[i + 1]);
  336. if (dstFormat == AV_PIX_FMT_NONE) {
  337. fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
  338. return -1;
  339. }
  340. } else {
  341. bad_option:
  342. fprintf(stderr, "bad option or argument missing (%s)\n", argv[i]);
  343. goto error;
  344. }
  345. }
  346. sws = sws_getContext(W / 12, H / 12, AV_PIX_FMT_RGB32, W, H,
  347. AV_PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
  348. av_lfg_init(&rand, 1);
  349. for (y = 0; y < H; y++)
  350. for (x = 0; x < W * 4; x++)
  351. rgb_data[ x + y * 4 * W] = av_lfg_get(&rand);
  352. sws_scale(sws, rgb_src, rgb_stride, 0, H, src, stride);
  353. sws_freeContext(sws);
  354. av_free(rgb_data);
  355. if(fp) {
  356. res = fileTest(src, stride, W, H, fp, srcFormat, dstFormat);
  357. fclose(fp);
  358. } else {
  359. selfTest(src, stride, W, H, srcFormat, dstFormat);
  360. res = 0;
  361. }
  362. error:
  363. av_free(data);
  364. return res;
  365. }