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.

410 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_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(uint8_t *src1, 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];
  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. if (srcStride[p])
  100. src[p] = av_mallocz(srcStride[p] * srcH + 16);
  101. if (srcStride[p] && !src[p]) {
  102. perror("Malloc");
  103. res = -1;
  104. goto end;
  105. }
  106. }
  107. srcContext = sws_getContext(w, h, AV_PIX_FMT_YUVA420P, srcW, srcH,
  108. srcFormat, SWS_BILINEAR, NULL, NULL, NULL);
  109. if (!srcContext) {
  110. fprintf(stderr, "Failed to get %s ---> %s\n",
  111. desc_yuva420p->name,
  112. desc_src->name);
  113. res = -1;
  114. goto end;
  115. }
  116. sws_scale(srcContext, ref, refStride, 0, h, src, srcStride);
  117. sws_freeContext(srcContext);
  118. cur_srcFormat = srcFormat;
  119. cur_srcW = srcW;
  120. cur_srcH = srcH;
  121. }
  122. av_image_fill_linesizes(dstStride, dstFormat, dstW);
  123. for (i = 0; i < 4; i++) {
  124. /* Image buffers passed into libswscale can be allocated any way you
  125. * prefer, as long as they're aligned enough for the architecture, and
  126. * they're freed appropriately (such as using av_free for buffers
  127. * allocated with av_malloc). */
  128. /* An extra 16 bytes is being allocated because some scalers may write
  129. * out of bounds. */
  130. if (dstStride[i])
  131. dst[i] = av_mallocz(dstStride[i] * dstH + 16);
  132. if (dstStride[i] && !dst[i]) {
  133. perror("Malloc");
  134. res = -1;
  135. goto end;
  136. }
  137. }
  138. dstContext = sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat,
  139. flags, NULL, NULL, NULL);
  140. if (!dstContext) {
  141. fprintf(stderr, "Failed to get %s ---> %s\n",
  142. desc_src->name, desc_dst->name);
  143. res = -1;
  144. goto end;
  145. }
  146. printf(" %s %dx%d -> %s %3dx%3d flags=%2d",
  147. desc_src->name, srcW, srcH,
  148. desc_dst->name, dstW, dstH,
  149. flags);
  150. fflush(stdout);
  151. sws_scale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
  152. for (i = 0; i < 4 && dstStride[i]; i++)
  153. crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), crc, dst[i],
  154. dstStride[i] * dstH);
  155. if (r && crc == r->crc) {
  156. ssdY = r->ssdY;
  157. ssdU = r->ssdU;
  158. ssdV = r->ssdV;
  159. ssdA = r->ssdA;
  160. } else {
  161. for (i = 0; i < 4; i++) {
  162. if (refStride[i])
  163. out[i] = av_mallocz(refStride[i] * h);
  164. if (refStride[i] && !out[i]) {
  165. perror("Malloc");
  166. res = -1;
  167. goto end;
  168. }
  169. }
  170. outContext = sws_getContext(dstW, dstH, dstFormat, w, h,
  171. AV_PIX_FMT_YUVA420P, SWS_BILINEAR,
  172. NULL, NULL, NULL);
  173. if (!outContext) {
  174. fprintf(stderr, "Failed to get %s ---> %s\n",
  175. desc_dst->name,
  176. desc_yuva420p->name);
  177. res = -1;
  178. goto end;
  179. }
  180. sws_scale(outContext, dst, dstStride, 0, dstH, out, refStride);
  181. ssdY = getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
  182. if (hasChroma(srcFormat) && hasChroma(dstFormat)) {
  183. //FIXME check that output is really gray
  184. ssdU = getSSD(ref[1], out[1], refStride[1], refStride[1],
  185. (w + 1) >> 1, (h + 1) >> 1);
  186. ssdV = getSSD(ref[2], out[2], refStride[2], refStride[2],
  187. (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. printf(" CRC=%08x SSD=%5"PRId64 ",%5"PRId64 ",%5"PRId64 ",%5"PRId64 "\n",
  201. crc, ssdY, ssdU, ssdV, ssdA);
  202. end:
  203. sws_freeContext(dstContext);
  204. for (i = 0; i < 4; i++)
  205. if (dstStride[i])
  206. av_free(dst[i]);
  207. return res;
  208. }
  209. static void selfTest(uint8_t *ref[4], int refStride[4], int w, int h,
  210. enum AVPixelFormat srcFormat_in,
  211. enum AVPixelFormat dstFormat_in)
  212. {
  213. const int flags[] = { SWS_FAST_BILINEAR, SWS_BILINEAR, SWS_BICUBIC,
  214. SWS_X, SWS_POINT, SWS_AREA, 0 };
  215. const int srcW = w;
  216. const int srcH = h;
  217. const int dstW[] = { srcW - srcW / 3, srcW, srcW + srcW / 3, 0 };
  218. const int dstH[] = { srcH - srcH / 3, srcH, srcH + srcH / 3, 0 };
  219. enum AVPixelFormat srcFormat, dstFormat;
  220. const AVPixFmtDescriptor *desc_src, *desc_dst;
  221. for (srcFormat = srcFormat_in != AV_PIX_FMT_NONE ? srcFormat_in : 0;
  222. srcFormat < AV_PIX_FMT_NB; srcFormat++) {
  223. if (!sws_isSupportedInput(srcFormat) ||
  224. !sws_isSupportedOutput(srcFormat))
  225. continue;
  226. desc_src = av_pix_fmt_desc_get(srcFormat);
  227. for (dstFormat = dstFormat_in != AV_PIX_FMT_NONE ? dstFormat_in : 0;
  228. dstFormat < AV_PIX_FMT_NB; dstFormat++) {
  229. int i, j, k;
  230. int res = 0;
  231. if (!sws_isSupportedInput(dstFormat) ||
  232. !sws_isSupportedOutput(dstFormat))
  233. continue;
  234. desc_dst = av_pix_fmt_desc_get(dstFormat);
  235. printf("%s -> %s\n", desc_src->name, desc_dst->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. if (dstFormat_in != AV_PIX_FMT_NONE)
  245. break;
  246. }
  247. if (srcFormat_in != AV_PIX_FMT_NONE)
  248. break;
  249. }
  250. }
  251. static int fileTest(uint8_t *ref[4], int refStride[4], int w, int h, FILE *fp,
  252. enum AVPixelFormat srcFormat_in,
  253. enum AVPixelFormat dstFormat_in)
  254. {
  255. char buf[256];
  256. while (fgets(buf, sizeof(buf), fp)) {
  257. struct Results r;
  258. enum AVPixelFormat srcFormat;
  259. char srcStr[12];
  260. int srcW, srcH;
  261. enum AVPixelFormat dstFormat;
  262. char dstStr[12];
  263. int dstW, dstH;
  264. int flags;
  265. int ret;
  266. ret = sscanf(buf,
  267. " %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 == AV_PIX_FMT_NONE || dstFormat == AV_PIX_FMT_NONE) {
  278. fprintf(stderr, "malformed input file\n");
  279. return -1;
  280. }
  281. if ((srcFormat_in != AV_PIX_FMT_NONE && srcFormat_in != srcFormat) ||
  282. (dstFormat_in != AV_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 AVPixelFormat srcFormat = AV_PIX_FMT_NONE;
  300. enum AVPixelFormat dstFormat = AV_PIX_FMT_NONE;
  301. uint8_t *rgb_data = av_malloc(W * H * 4);
  302. uint8_t *rgb_src[4] = { rgb_data, NULL, NULL, NULL };
  303. int rgb_stride[4] = { 4 * W, 0, 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, AV_PIX_FMT_RGB32, W, H,
  315. AV_PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
  316. av_lfg_init(&rand, 1);
  317. for (y = 0; y < H; y++)
  318. for (x = 0; x < W * 4; x++)
  319. rgb_data[ x + y * 4 * W] = av_lfg_get(&rand);
  320. sws_scale(sws, rgb_src, rgb_stride, 0, H, src, stride);
  321. sws_freeContext(sws);
  322. av_free(rgb_data);
  323. for (i = 1; i < argc; i += 2) {
  324. if (argv[i][0] != '-' || i + 1 == argc)
  325. goto bad_option;
  326. if (!strcmp(argv[i], "-ref")) {
  327. FILE *fp = fopen(argv[i + 1], "r");
  328. if (!fp) {
  329. fprintf(stderr, "could not open '%s'\n", argv[i + 1]);
  330. goto error;
  331. }
  332. res = fileTest(src, stride, W, H, fp, srcFormat, dstFormat);
  333. fclose(fp);
  334. goto end;
  335. } else if (!strcmp(argv[i], "-src")) {
  336. srcFormat = av_get_pix_fmt(argv[i + 1]);
  337. if (srcFormat == AV_PIX_FMT_NONE) {
  338. fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
  339. return -1;
  340. }
  341. } else if (!strcmp(argv[i], "-dst")) {
  342. dstFormat = av_get_pix_fmt(argv[i + 1]);
  343. if (dstFormat == AV_PIX_FMT_NONE) {
  344. fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
  345. return -1;
  346. }
  347. } else {
  348. bad_option:
  349. fprintf(stderr, "bad option or argument missing (%s)\n", argv[i]);
  350. goto error;
  351. }
  352. }
  353. selfTest(src, stride, W, H, srcFormat, dstFormat);
  354. end:
  355. res = 0;
  356. error:
  357. av_free(data);
  358. return res;
  359. }