swscale-test.c 13 KB

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