You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

412 lines
13KB

  1. /*
  2. * Copyright (C) 2003 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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_YA8 || \
  38. (x) == AV_PIX_FMT_GRAY16BE || \
  39. (x) == AV_PIX_FMT_GRAY16LE || \
  40. (x) == AV_PIX_FMT_YA16BE || \
  41. (x) == AV_PIX_FMT_YA16LE)
  42. #define hasChroma(x) \
  43. (!(isGray(x) || \
  44. (x) == AV_PIX_FMT_MONOBLACK || \
  45. (x) == AV_PIX_FMT_MONOWHITE))
  46. #define isALPHA(x) \
  47. ((x) == AV_PIX_FMT_BGR32 || \
  48. (x) == AV_PIX_FMT_BGR32_1 || \
  49. (x) == AV_PIX_FMT_RGB32 || \
  50. (x) == AV_PIX_FMT_RGB32_1 || \
  51. (x) == AV_PIX_FMT_YUVA420P)
  52. static uint64_t getSSD(uint8_t *src1, uint8_t *src2, int stride1,
  53. int stride2, int w, int h)
  54. {
  55. int x, y;
  56. uint64_t ssd = 0;
  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. }
  62. }
  63. return ssd;
  64. }
  65. struct Results {
  66. uint64_t ssdY;
  67. uint64_t ssdU;
  68. uint64_t ssdV;
  69. uint64_t ssdA;
  70. uint32_t crc;
  71. };
  72. // test by ref -> src -> dst -> out & compare out against ref
  73. // ref & out are YV12
  74. static int doTest(uint8_t *ref[4], int refStride[4], int w, int h,
  75. enum AVPixelFormat srcFormat, enum AVPixelFormat dstFormat,
  76. int srcW, int srcH, int dstW, int dstH, int flags,
  77. struct Results *r)
  78. {
  79. const AVPixFmtDescriptor *desc_yuva420p = av_pix_fmt_desc_get(AV_PIX_FMT_YUVA420P);
  80. const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(srcFormat);
  81. const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(dstFormat);
  82. static enum AVPixelFormat cur_srcFormat;
  83. static int cur_srcW, cur_srcH;
  84. static uint8_t *src[4];
  85. static int srcStride[4];
  86. uint8_t *dst[4] = { 0 };
  87. uint8_t *out[4] = { 0 };
  88. int dstStride[4];
  89. int i;
  90. uint64_t ssdY, ssdU = 0, ssdV = 0, ssdA = 0;
  91. struct SwsContext *dstContext = NULL, *outContext = NULL;
  92. uint32_t crc = 0;
  93. int res = 0;
  94. if (cur_srcFormat != srcFormat || cur_srcW != srcW || cur_srcH != srcH) {
  95. struct SwsContext *srcContext = NULL;
  96. int p;
  97. for (p = 0; p < 4; 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, AV_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. desc_yuva420p->name,
  114. desc_src->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,
  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, 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. if (refStride[i])
  165. out[i] = av_mallocz(refStride[i] * h);
  166. if (refStride[i] && !out[i]) {
  167. perror("Malloc");
  168. res = -1;
  169. goto end;
  170. }
  171. }
  172. outContext = sws_getContext(dstW, dstH, dstFormat, w, h,
  173. AV_PIX_FMT_YUVA420P, SWS_BILINEAR,
  174. NULL, NULL, NULL);
  175. if (!outContext) {
  176. fprintf(stderr, "Failed to get %s ---> %s\n",
  177. desc_dst->name,
  178. desc_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],
  187. (w + 1) >> 1, (h + 1) >> 1);
  188. ssdV = getSSD(ref[2], out[2], refStride[2], refStride[2],
  189. (w + 1) >> 1, (h + 1) >> 1);
  190. }
  191. if (isALPHA(srcFormat) && isALPHA(dstFormat))
  192. ssdA = getSSD(ref[3], out[3], refStride[3], refStride[3], w, h);
  193. ssdY /= w * h;
  194. ssdU /= w * h / 4;
  195. ssdV /= w * h / 4;
  196. ssdA /= w * h;
  197. sws_freeContext(outContext);
  198. for (i = 0; i < 4; i++)
  199. if (refStride[i])
  200. av_free(out[i]);
  201. }
  202. printf(" CRC=%08x SSD=%5"PRId64 ",%5"PRId64 ",%5"PRId64 ",%5"PRId64 "\n",
  203. crc, ssdY, ssdU, ssdV, ssdA);
  204. end:
  205. sws_freeContext(dstContext);
  206. for (i = 0; i < 4; i++)
  207. if (dstStride[i])
  208. av_free(dst[i]);
  209. return res;
  210. }
  211. static void selfTest(uint8_t *ref[4], int refStride[4], int w, int h,
  212. enum AVPixelFormat srcFormat_in,
  213. enum AVPixelFormat dstFormat_in)
  214. {
  215. const int flags[] = { SWS_FAST_BILINEAR, SWS_BILINEAR, SWS_BICUBIC,
  216. SWS_X, SWS_POINT, SWS_AREA, 0 };
  217. const int srcW = w;
  218. const int srcH = h;
  219. const int dstW[] = { srcW - srcW / 3, srcW, srcW + srcW / 3, 0 };
  220. const int dstH[] = { srcH - srcH / 3, srcH, srcH + srcH / 3, 0 };
  221. enum AVPixelFormat srcFormat, dstFormat;
  222. const AVPixFmtDescriptor *desc_src, *desc_dst;
  223. for (srcFormat = srcFormat_in != AV_PIX_FMT_NONE ? srcFormat_in : 0;
  224. srcFormat < AV_PIX_FMT_NB; srcFormat++) {
  225. if (!sws_isSupportedInput(srcFormat) ||
  226. !sws_isSupportedOutput(srcFormat))
  227. continue;
  228. desc_src = av_pix_fmt_desc_get(srcFormat);
  229. for (dstFormat = dstFormat_in != AV_PIX_FMT_NONE ? dstFormat_in : 0;
  230. dstFormat < AV_PIX_FMT_NB; dstFormat++) {
  231. int i, j, k;
  232. int res = 0;
  233. if (!sws_isSupportedInput(dstFormat) ||
  234. !sws_isSupportedOutput(dstFormat))
  235. continue;
  236. desc_dst = av_pix_fmt_desc_get(dstFormat);
  237. printf("%s -> %s\n", desc_src->name, desc_dst->name);
  238. fflush(stdout);
  239. for (k = 0; flags[k] && !res; k++)
  240. for (i = 0; dstW[i] && !res; i++)
  241. for (j = 0; dstH[j] && !res; j++)
  242. res = doTest(ref, refStride, w, h,
  243. srcFormat, dstFormat,
  244. srcW, srcH, dstW[i], dstH[j], flags[k],
  245. NULL);
  246. if (dstFormat_in != AV_PIX_FMT_NONE)
  247. break;
  248. }
  249. if (srcFormat_in != AV_PIX_FMT_NONE)
  250. break;
  251. }
  252. }
  253. static int fileTest(uint8_t *ref[4], int refStride[4], int w, int h, FILE *fp,
  254. enum AVPixelFormat srcFormat_in,
  255. enum AVPixelFormat dstFormat_in)
  256. {
  257. char buf[256];
  258. while (fgets(buf, sizeof(buf), fp)) {
  259. struct Results r;
  260. enum AVPixelFormat srcFormat;
  261. char srcStr[12];
  262. int srcW, srcH;
  263. enum AVPixelFormat dstFormat;
  264. char dstStr[12];
  265. int dstW, dstH;
  266. int flags;
  267. int ret;
  268. ret = sscanf(buf,
  269. " %12s %dx%d -> %12s %dx%d flags=%d CRC=%x"
  270. " SSD=%"PRId64 ", %"PRId64 ", %"PRId64 ", %"PRId64 "\n",
  271. srcStr, &srcW, &srcH, dstStr, &dstW, &dstH,
  272. &flags, &r.crc, &r.ssdY, &r.ssdU, &r.ssdV, &r.ssdA);
  273. if (ret != 12) {
  274. srcStr[0] = dstStr[0] = 0;
  275. ret = sscanf(buf, "%12s -> %12s\n", srcStr, dstStr);
  276. }
  277. srcFormat = av_get_pix_fmt(srcStr);
  278. dstFormat = av_get_pix_fmt(dstStr);
  279. if (srcFormat == AV_PIX_FMT_NONE || dstFormat == AV_PIX_FMT_NONE) {
  280. fprintf(stderr, "malformed input file\n");
  281. return -1;
  282. }
  283. if ((srcFormat_in != AV_PIX_FMT_NONE && srcFormat_in != srcFormat) ||
  284. (dstFormat_in != AV_PIX_FMT_NONE && dstFormat_in != dstFormat))
  285. continue;
  286. if (ret != 12) {
  287. printf("%s", buf);
  288. continue;
  289. }
  290. doTest(ref, refStride, w, h,
  291. srcFormat, dstFormat,
  292. srcW, srcH, dstW, dstH, flags,
  293. &r);
  294. }
  295. return 0;
  296. }
  297. #define W 96
  298. #define H 96
  299. int main(int argc, char **argv)
  300. {
  301. enum AVPixelFormat srcFormat = AV_PIX_FMT_NONE;
  302. enum AVPixelFormat dstFormat = AV_PIX_FMT_NONE;
  303. uint8_t *rgb_data = av_malloc(W * H * 4);
  304. uint8_t *rgb_src[4] = { rgb_data, NULL, NULL, NULL };
  305. int rgb_stride[4] = { 4 * W, 0, 0, 0 };
  306. uint8_t *data = av_malloc(4 * W * H);
  307. uint8_t *src[4] = { data, data + W * H, data + W * H * 2, data + W * H * 3 };
  308. int stride[4] = { W, W, W, W };
  309. int x, y;
  310. struct SwsContext *sws;
  311. AVLFG rand;
  312. int res = -1;
  313. int i;
  314. if (!rgb_data || !data)
  315. return -1;
  316. sws = sws_getContext(W / 12, H / 12, AV_PIX_FMT_RGB32, W, H,
  317. AV_PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
  318. av_lfg_init(&rand, 1);
  319. for (y = 0; y < H; y++)
  320. for (x = 0; x < W * 4; x++)
  321. rgb_data[ x + y * 4 * W] = av_lfg_get(&rand);
  322. sws_scale(sws, rgb_src, rgb_stride, 0, H, src, stride);
  323. sws_freeContext(sws);
  324. av_free(rgb_data);
  325. for (i = 1; i < argc; i += 2) {
  326. if (argv[i][0] != '-' || i + 1 == argc)
  327. goto bad_option;
  328. if (!strcmp(argv[i], "-ref")) {
  329. FILE *fp = fopen(argv[i + 1], "r");
  330. if (!fp) {
  331. fprintf(stderr, "could not open '%s'\n", argv[i + 1]);
  332. goto error;
  333. }
  334. res = fileTest(src, stride, W, H, fp, srcFormat, dstFormat);
  335. fclose(fp);
  336. goto end;
  337. } else if (!strcmp(argv[i], "-src")) {
  338. srcFormat = av_get_pix_fmt(argv[i + 1]);
  339. if (srcFormat == AV_PIX_FMT_NONE) {
  340. fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
  341. return -1;
  342. }
  343. } else if (!strcmp(argv[i], "-dst")) {
  344. dstFormat = av_get_pix_fmt(argv[i + 1]);
  345. if (dstFormat == AV_PIX_FMT_NONE) {
  346. fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
  347. return -1;
  348. }
  349. } else {
  350. bad_option:
  351. fprintf(stderr, "bad option or argument missing (%s)\n", argv[i]);
  352. goto error;
  353. }
  354. }
  355. selfTest(src, stride, W, H, srcFormat, dstFormat);
  356. end:
  357. res = 0;
  358. error:
  359. av_free(data);
  360. return res;
  361. }