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.

417 lines
14KB

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