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.

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