swscale.c 15 KB

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