swscale-test.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (C) 2003 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 "libavcore/imgutils.h"
  27. #include "libavutil/mem.h"
  28. #include "libavutil/avutil.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/lfg.h"
  31. #include "swscale.h"
  32. /* HACK Duplicated from swscale_internal.h.
  33. * Should be removed when a cleaner pixel format system exists. */
  34. #define isGray(x) ( \
  35. (x)==PIX_FMT_GRAY8 \
  36. || (x)==PIX_FMT_GRAY16BE \
  37. || (x)==PIX_FMT_GRAY16LE \
  38. )
  39. #define hasChroma(x) (!( \
  40. isGray(x) \
  41. || (x)==PIX_FMT_MONOBLACK \
  42. || (x)==PIX_FMT_MONOWHITE \
  43. ))
  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. )
  51. static uint64_t getSSD(uint8_t *src1, uint8_t *src2, int stride1, int stride2, int w, int h)
  52. {
  53. int x,y;
  54. uint64_t ssd=0;
  55. //printf("%d %d\n", w, h);
  56. for (y=0; y<h; y++) {
  57. for (x=0; x<w; x++) {
  58. int d= src1[x + y*stride1] - src2[x + y*stride2];
  59. ssd+= d*d;
  60. //printf("%d", abs(src1[x + y*stride1] - src2[x + y*stride2])/26 );
  61. }
  62. //printf("\n");
  63. }
  64. return ssd;
  65. }
  66. // test by ref -> src -> dst -> out & compare out against ref
  67. // ref & out are YV12
  68. static int doTest(uint8_t *ref[4], int refStride[4], int w, int h,
  69. uint8_t *src[4], int srcStride[4],
  70. enum PixelFormat srcFormat, enum PixelFormat dstFormat,
  71. int srcW, int srcH, int dstW, int dstH, int flags)
  72. {
  73. uint8_t *dst[4] = {0};
  74. uint8_t *out[4] = {0};
  75. int dstStride[4];
  76. int i;
  77. uint64_t ssdY, ssdU=0, ssdV=0, ssdA=0;
  78. struct SwsContext *dstContext = NULL, *outContext = NULL;
  79. int res = 0;
  80. av_fill_image_linesizes(dstStride, dstFormat, dstW);
  81. for (i=0; i<4; i++) {
  82. /* Image buffers passed into libswscale can be allocated any way you
  83. * prefer, as long as they're aligned enough for the architecture, and
  84. * they're freed appropriately (such as using av_free for buffers
  85. * allocated with av_malloc). */
  86. /* An extra 16 bytes is being allocated because some scalers may write
  87. * out of bounds. */
  88. if (dstStride[i])
  89. dst[i]= av_mallocz(dstStride[i]*dstH+16);
  90. if (refStride[i])
  91. out[i]= av_mallocz(refStride[i]*h);
  92. if ((dstStride[i] && !dst[i]) || (refStride[i] && !out[i])) {
  93. perror("Malloc");
  94. res = -1;
  95. goto end;
  96. }
  97. }
  98. dstContext= sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, NULL, NULL, NULL);
  99. if (!dstContext) {
  100. fprintf(stderr, "Failed to get %s ---> %s\n",
  101. av_pix_fmt_descriptors[srcFormat].name,
  102. av_pix_fmt_descriptors[dstFormat].name);
  103. res = -1;
  104. goto end;
  105. }
  106. outContext= sws_getContext(dstW, dstH, dstFormat, w, h, PIX_FMT_YUVA420P, flags, NULL, NULL, NULL);
  107. if (!outContext) {
  108. fprintf(stderr, "Failed to get %s ---> %s\n",
  109. av_pix_fmt_descriptors[dstFormat].name,
  110. av_pix_fmt_descriptors[PIX_FMT_YUVA420P].name);
  111. res = -1;
  112. goto end;
  113. }
  114. // printf("test %X %X %X -> %X %X %X\n", (int)ref[0], (int)ref[1], (int)ref[2],
  115. // (int)src[0], (int)src[1], (int)src[2]);
  116. printf(" %s %dx%d -> %s %3dx%3d flags=%2d",
  117. av_pix_fmt_descriptors[srcFormat].name, srcW, srcH,
  118. av_pix_fmt_descriptors[dstFormat].name, dstW, dstH,
  119. flags);
  120. fflush(stdout);
  121. sws_scale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
  122. sws_scale(outContext, dst, dstStride, 0, dstH, out, refStride);
  123. ssdY= getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
  124. if (hasChroma(srcFormat) && hasChroma(dstFormat)) {
  125. //FIXME check that output is really gray
  126. ssdU= getSSD(ref[1], out[1], refStride[1], refStride[1], (w+1)>>1, (h+1)>>1);
  127. ssdV= getSSD(ref[2], out[2], refStride[2], refStride[2], (w+1)>>1, (h+1)>>1);
  128. }
  129. if (isALPHA(srcFormat) && isALPHA(dstFormat))
  130. ssdA= getSSD(ref[3], out[3], refStride[3], refStride[3], w, h);
  131. ssdY/= w*h;
  132. ssdU/= w*h/4;
  133. ssdV/= w*h/4;
  134. ssdA/= w*h;
  135. printf(" SSD=%5"PRId64",%5"PRId64",%5"PRId64",%5"PRId64"\n",
  136. ssdY, ssdU, ssdV, ssdA);
  137. end:
  138. sws_freeContext(dstContext);
  139. sws_freeContext(outContext);
  140. for (i=0; i<4; i++) {
  141. if (dstStride[i])
  142. av_free(dst[i]);
  143. if (refStride[i])
  144. av_free(out[i]);
  145. }
  146. return res;
  147. }
  148. static void selfTest(uint8_t *ref[4], int refStride[4], int w, int h)
  149. {
  150. const int flags[] = { SWS_FAST_BILINEAR,
  151. SWS_BILINEAR, SWS_BICUBIC,
  152. SWS_X , SWS_POINT , SWS_AREA, 0 };
  153. const int srcW = w;
  154. const int srcH = h;
  155. const int dstW[] = { srcW - srcW/3, srcW, srcW + srcW/3, 0 };
  156. const int dstH[] = { srcH - srcH/3, srcH, srcH + srcH/3, 0 };
  157. enum PixelFormat srcFormat, dstFormat;
  158. for (srcFormat = 0; srcFormat < PIX_FMT_NB; srcFormat++) {
  159. if (!sws_isSupportedInput(srcFormat) || !sws_isSupportedOutput(srcFormat))
  160. continue;
  161. for (dstFormat = 0; dstFormat < PIX_FMT_NB; dstFormat++) {
  162. int i, j, k;
  163. int res = 0;
  164. if (!sws_isSupportedInput(dstFormat) || !sws_isSupportedOutput(dstFormat))
  165. continue;
  166. printf("%s -> %s\n",
  167. av_pix_fmt_descriptors[srcFormat].name,
  168. av_pix_fmt_descriptors[dstFormat].name);
  169. fflush(stdout);
  170. for (k = 0; flags[k] && !res; k++) {
  171. struct SwsContext *srcContext = NULL;
  172. uint8_t *src[4] = {0};
  173. int srcStride[4];
  174. int p;
  175. av_fill_image_linesizes(srcStride, srcFormat, srcW);
  176. for (p = 0; p < 4; p++) {
  177. if (srcStride[p])
  178. src[p] = av_mallocz(srcStride[p]*srcH+16);
  179. if (srcStride[p] && !src[p]) {
  180. perror("Malloc");
  181. return;
  182. }
  183. }
  184. srcContext = sws_getContext(w, h, PIX_FMT_YUVA420P, srcW, srcH,
  185. srcFormat, flags[k], NULL, NULL, NULL);
  186. if (!srcContext) {
  187. fprintf(stderr, "Failed to get %s ---> %s\n",
  188. av_pix_fmt_descriptors[PIX_FMT_YUVA420P].name,
  189. av_pix_fmt_descriptors[srcFormat].name);
  190. return;
  191. }
  192. sws_scale(srcContext, ref, refStride, 0, h, src, srcStride);
  193. for (i = 0; dstW[i] && !res; i++)
  194. for (j = 0; dstH[j] && !res; j++)
  195. res = doTest(ref, refStride, w, h, src, srcStride,
  196. srcFormat, dstFormat,
  197. srcW, srcH, dstW[i], dstH[j], flags[k]);
  198. sws_freeContext(srcContext);
  199. for (p = 0; p < 4; p++)
  200. if (srcStride[p])
  201. av_free(src[p]);
  202. }
  203. }
  204. }
  205. }
  206. #define W 96
  207. #define H 96
  208. int main(int argc, char **argv)
  209. {
  210. uint8_t *rgb_data = av_malloc (W*H*4);
  211. uint8_t *rgb_src[3]= {rgb_data, NULL, NULL};
  212. int rgb_stride[3]={4*W, 0, 0};
  213. uint8_t *data = av_malloc (4*W*H);
  214. uint8_t *src[4]= {data, data+W*H, data+W*H*2, data+W*H*3};
  215. int stride[4]={W, W, W, W};
  216. int x, y;
  217. struct SwsContext *sws;
  218. AVLFG rand;
  219. if (!rgb_data || !data)
  220. return -1;
  221. sws= sws_getContext(W/12, H/12, PIX_FMT_RGB32, W, H, PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
  222. av_lfg_init(&rand, 1);
  223. for (y=0; y<H; y++) {
  224. for (x=0; x<W*4; x++) {
  225. rgb_data[ x + y*4*W]= av_lfg_get(&rand);
  226. }
  227. }
  228. sws_scale(sws, rgb_src, rgb_stride, 0, H, src, stride);
  229. sws_freeContext(sws);
  230. av_free(rgb_data);
  231. selfTest(src, stride, W, H);
  232. av_free(data);
  233. return 0;
  234. }