00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <inttypes.h>
00025 #include <stdarg.h>
00026
00027 #undef HAVE_AV_CONFIG_H
00028 #include "libavcore/imgutils.h"
00029 #include "libavutil/mem.h"
00030 #include "libavutil/avutil.h"
00031 #include "libavutil/crc.h"
00032 #include "libavutil/pixdesc.h"
00033 #include "libavutil/lfg.h"
00034 #include "swscale.h"
00035
00036
00037
00038 #define isGray(x) ( \
00039 (x)==PIX_FMT_GRAY8 \
00040 || (x)==PIX_FMT_GRAY16BE \
00041 || (x)==PIX_FMT_GRAY16LE \
00042 )
00043 #define hasChroma(x) (!( \
00044 isGray(x) \
00045 || (x)==PIX_FMT_MONOBLACK \
00046 || (x)==PIX_FMT_MONOWHITE \
00047 ))
00048 #define isALPHA(x) ( \
00049 (x)==PIX_FMT_BGR32 \
00050 || (x)==PIX_FMT_BGR32_1 \
00051 || (x)==PIX_FMT_RGB32 \
00052 || (x)==PIX_FMT_RGB32_1 \
00053 || (x)==PIX_FMT_YUVA420P \
00054 )
00055
00056 static uint64_t getSSD(uint8_t *src1, uint8_t *src2, int stride1, int stride2, int w, int h)
00057 {
00058 int x,y;
00059 uint64_t ssd=0;
00060
00061
00062
00063 for (y=0; y<h; y++) {
00064 for (x=0; x<w; x++) {
00065 int d= src1[x + y*stride1] - src2[x + y*stride2];
00066 ssd+= d*d;
00067
00068 }
00069
00070 }
00071 return ssd;
00072 }
00073
00074 struct Results {
00075 uint64_t ssdY;
00076 uint64_t ssdU;
00077 uint64_t ssdV;
00078 uint64_t ssdA;
00079 uint32_t crc;
00080 };
00081
00082
00083
00084 static int doTest(uint8_t *ref[4], int refStride[4], int w, int h,
00085 enum PixelFormat srcFormat, enum PixelFormat dstFormat,
00086 int srcW, int srcH, int dstW, int dstH, int flags,
00087 struct Results *r)
00088 {
00089 static enum PixelFormat cur_srcFormat;
00090 static int cur_srcW, cur_srcH;
00091 static uint8_t *src[4];
00092 static int srcStride[4];
00093 uint8_t *dst[4] = {0};
00094 uint8_t *out[4] = {0};
00095 int dstStride[4];
00096 int i;
00097 uint64_t ssdY, ssdU=0, ssdV=0, ssdA=0;
00098 struct SwsContext *dstContext = NULL, *outContext = NULL;
00099 uint32_t crc = 0;
00100 int res = 0;
00101
00102 if (cur_srcFormat != srcFormat || cur_srcW != srcW || cur_srcH != srcH) {
00103 struct SwsContext *srcContext = NULL;
00104 int p;
00105
00106 for (p = 0; p < 4; p++)
00107 if (src[p])
00108 av_freep(&src[p]);
00109
00110 av_image_fill_linesizes(srcStride, srcFormat, srcW);
00111 for (p = 0; p < 4; p++) {
00112 if (srcStride[p])
00113 src[p] = av_mallocz(srcStride[p]*srcH+16);
00114 if (srcStride[p] && !src[p]) {
00115 perror("Malloc");
00116 res = -1;
00117
00118 goto end;
00119 }
00120 }
00121 srcContext = sws_getContext(w, h, PIX_FMT_YUVA420P, srcW, srcH,
00122 srcFormat, SWS_BILINEAR, NULL, NULL, NULL);
00123 if (!srcContext) {
00124 fprintf(stderr, "Failed to get %s ---> %s\n",
00125 av_pix_fmt_descriptors[PIX_FMT_YUVA420P].name,
00126 av_pix_fmt_descriptors[srcFormat].name);
00127 res = -1;
00128
00129 goto end;
00130 }
00131 sws_scale(srcContext, ref, refStride, 0, h, src, srcStride);
00132 sws_freeContext(srcContext);
00133
00134 cur_srcFormat = srcFormat;
00135 cur_srcW = srcW;
00136 cur_srcH = srcH;
00137 }
00138
00139 av_image_fill_linesizes(dstStride, dstFormat, dstW);
00140 for (i=0; i<4; i++) {
00141
00142
00143
00144
00145
00146
00147 if (dstStride[i])
00148 dst[i]= av_mallocz(dstStride[i]*dstH+16);
00149 if (dstStride[i] && !dst[i]) {
00150 perror("Malloc");
00151 res = -1;
00152
00153 goto end;
00154 }
00155 }
00156
00157 dstContext= sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, NULL, NULL, NULL);
00158 if (!dstContext) {
00159 fprintf(stderr, "Failed to get %s ---> %s\n",
00160 av_pix_fmt_descriptors[srcFormat].name,
00161 av_pix_fmt_descriptors[dstFormat].name);
00162 res = -1;
00163
00164 goto end;
00165 }
00166
00167
00168
00169 printf(" %s %dx%d -> %s %3dx%3d flags=%2d",
00170 av_pix_fmt_descriptors[srcFormat].name, srcW, srcH,
00171 av_pix_fmt_descriptors[dstFormat].name, dstW, dstH,
00172 flags);
00173 fflush(stdout);
00174
00175 sws_scale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
00176
00177 for (i = 0; i < 4 && dstStride[i]; i++) {
00178 crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), crc, dst[i], dstStride[i] * dstH);
00179 }
00180
00181 if (r && crc == r->crc) {
00182 ssdY = r->ssdY;
00183 ssdU = r->ssdU;
00184 ssdV = r->ssdV;
00185 ssdA = r->ssdA;
00186 } else {
00187 for (i=0; i<4; i++) {
00188 if (refStride[i])
00189 out[i]= av_mallocz(refStride[i]*h);
00190 if (refStride[i] && !out[i]) {
00191 perror("Malloc");
00192 res = -1;
00193
00194 goto end;
00195 }
00196 }
00197 outContext= sws_getContext(dstW, dstH, dstFormat, w, h, PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
00198 if (!outContext) {
00199 fprintf(stderr, "Failed to get %s ---> %s\n",
00200 av_pix_fmt_descriptors[dstFormat].name,
00201 av_pix_fmt_descriptors[PIX_FMT_YUVA420P].name);
00202 res = -1;
00203
00204 goto end;
00205 }
00206 sws_scale(outContext, dst, dstStride, 0, dstH, out, refStride);
00207
00208 ssdY= getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
00209 if (hasChroma(srcFormat) && hasChroma(dstFormat)) {
00210
00211 ssdU= getSSD(ref[1], out[1], refStride[1], refStride[1], (w+1)>>1, (h+1)>>1);
00212 ssdV= getSSD(ref[2], out[2], refStride[2], refStride[2], (w+1)>>1, (h+1)>>1);
00213 }
00214 if (isALPHA(srcFormat) && isALPHA(dstFormat))
00215 ssdA= getSSD(ref[3], out[3], refStride[3], refStride[3], w, h);
00216
00217 ssdY/= w*h;
00218 ssdU/= w*h/4;
00219 ssdV/= w*h/4;
00220 ssdA/= w*h;
00221
00222 sws_freeContext(outContext);
00223
00224 for (i=0; i<4; i++) {
00225 if (refStride[i])
00226 av_free(out[i]);
00227 }
00228 }
00229
00230 printf(" CRC=%08x SSD=%5"PRId64",%5"PRId64",%5"PRId64",%5"PRId64"\n",
00231 crc, ssdY, ssdU, ssdV, ssdA);
00232
00233 end:
00234
00235 sws_freeContext(dstContext);
00236
00237 for (i=0; i<4; i++) {
00238 if (dstStride[i])
00239 av_free(dst[i]);
00240 }
00241
00242 return res;
00243 }
00244
00245 static void selfTest(uint8_t *ref[4], int refStride[4], int w, int h,
00246 enum PixelFormat srcFormat_in,
00247 enum PixelFormat dstFormat_in)
00248 {
00249 const int flags[] = { SWS_FAST_BILINEAR,
00250 SWS_BILINEAR, SWS_BICUBIC,
00251 SWS_X , SWS_POINT , SWS_AREA, 0 };
00252 const int srcW = w;
00253 const int srcH = h;
00254 const int dstW[] = { srcW - srcW/3, srcW, srcW + srcW/3, 0 };
00255 const int dstH[] = { srcH - srcH/3, srcH, srcH + srcH/3, 0 };
00256 enum PixelFormat srcFormat, dstFormat;
00257
00258 for (srcFormat = srcFormat_in != PIX_FMT_NONE ? srcFormat_in : 0;
00259 srcFormat < PIX_FMT_NB; srcFormat++) {
00260 if (!sws_isSupportedInput(srcFormat) || !sws_isSupportedOutput(srcFormat))
00261 continue;
00262
00263 for (dstFormat = dstFormat_in != PIX_FMT_NONE ? dstFormat_in : 0;
00264 dstFormat < PIX_FMT_NB; dstFormat++) {
00265 int i, j, k;
00266 int res = 0;
00267
00268 if (!sws_isSupportedInput(dstFormat) || !sws_isSupportedOutput(dstFormat))
00269 continue;
00270
00271 printf("%s -> %s\n",
00272 av_pix_fmt_descriptors[srcFormat].name,
00273 av_pix_fmt_descriptors[dstFormat].name);
00274 fflush(stdout);
00275
00276 for (k = 0; flags[k] && !res; k++) {
00277 for (i = 0; dstW[i] && !res; i++)
00278 for (j = 0; dstH[j] && !res; j++)
00279 res = doTest(ref, refStride, w, h,
00280 srcFormat, dstFormat,
00281 srcW, srcH, dstW[i], dstH[j], flags[k],
00282 NULL);
00283 }
00284 if (dstFormat_in != PIX_FMT_NONE)
00285 break;
00286 }
00287 if (srcFormat_in != PIX_FMT_NONE)
00288 break;
00289 }
00290 }
00291
00292 static int fileTest(uint8_t *ref[4], int refStride[4], int w, int h, FILE *fp,
00293 enum PixelFormat srcFormat_in,
00294 enum PixelFormat dstFormat_in)
00295 {
00296 char buf[256];
00297
00298 while (fgets(buf, sizeof(buf), fp)) {
00299 struct Results r;
00300 enum PixelFormat srcFormat;
00301 char srcStr[12];
00302 int srcW, srcH;
00303 enum PixelFormat dstFormat;
00304 char dstStr[12];
00305 int dstW, dstH;
00306 int flags;
00307 int ret;
00308
00309 ret = sscanf(buf, " %12s %dx%d -> %12s %dx%d flags=%d CRC=%x"
00310 " SSD=%"PRId64", %"PRId64", %"PRId64", %"PRId64"\n",
00311 srcStr, &srcW, &srcH, dstStr, &dstW, &dstH,
00312 &flags, &r.crc, &r.ssdY, &r.ssdU, &r.ssdV, &r.ssdA);
00313 if (ret != 12) {
00314 srcStr[0] = dstStr[0] = 0;
00315 ret = sscanf(buf, "%12s -> %12s\n", srcStr, dstStr);
00316 }
00317
00318 srcFormat = av_get_pix_fmt(srcStr);
00319 dstFormat = av_get_pix_fmt(dstStr);
00320
00321 if (srcFormat == PIX_FMT_NONE || dstFormat == PIX_FMT_NONE) {
00322 fprintf(stderr, "malformed input file\n");
00323 return -1;
00324 }
00325 if ((srcFormat_in != PIX_FMT_NONE && srcFormat_in != srcFormat) ||
00326 (dstFormat_in != PIX_FMT_NONE && dstFormat_in != dstFormat))
00327 continue;
00328 if (ret != 12) {
00329 printf("%s", buf);
00330 continue;
00331 }
00332
00333 doTest(ref, refStride, w, h,
00334 srcFormat, dstFormat,
00335 srcW, srcH, dstW, dstH, flags,
00336 &r);
00337 }
00338
00339 return 0;
00340 }
00341
00342 #define W 96
00343 #define H 96
00344
00345 int main(int argc, char **argv)
00346 {
00347 enum PixelFormat srcFormat = PIX_FMT_NONE;
00348 enum PixelFormat dstFormat = PIX_FMT_NONE;
00349 uint8_t *rgb_data = av_malloc (W*H*4);
00350 uint8_t *rgb_src[3]= {rgb_data, NULL, NULL};
00351 int rgb_stride[3]={4*W, 0, 0};
00352 uint8_t *data = av_malloc (4*W*H);
00353 uint8_t *src[4]= {data, data+W*H, data+W*H*2, data+W*H*3};
00354 int stride[4]={W, W, W, W};
00355 int x, y;
00356 struct SwsContext *sws;
00357 AVLFG rand;
00358 int res = -1;
00359 int i;
00360
00361 if (!rgb_data || !data)
00362 return -1;
00363
00364 sws= sws_getContext(W/12, H/12, PIX_FMT_RGB32, W, H, PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
00365
00366 av_lfg_init(&rand, 1);
00367
00368 for (y=0; y<H; y++) {
00369 for (x=0; x<W*4; x++) {
00370 rgb_data[ x + y*4*W]= av_lfg_get(&rand);
00371 }
00372 }
00373 sws_scale(sws, rgb_src, rgb_stride, 0, H, src, stride);
00374 sws_freeContext(sws);
00375 av_free(rgb_data);
00376
00377 for (i = 1; i < argc; i += 2) {
00378 if (argv[i][0] != '-' || i+1 == argc)
00379 goto bad_option;
00380 if (!strcmp(argv[i], "-ref")) {
00381 FILE *fp = fopen(argv[i+1], "r");
00382 if (!fp) {
00383 fprintf(stderr, "could not open '%s'\n", argv[i+1]);
00384 goto error;
00385 }
00386 res = fileTest(src, stride, W, H, fp, srcFormat, dstFormat);
00387 fclose(fp);
00388 goto end;
00389 } else if (!strcmp(argv[i], "-src")) {
00390 srcFormat = av_get_pix_fmt(argv[i+1]);
00391 if (srcFormat == PIX_FMT_NONE) {
00392 fprintf(stderr, "invalid pixel format %s\n", argv[i+1]);
00393 return -1;
00394 }
00395 } else if (!strcmp(argv[i], "-dst")) {
00396 dstFormat = av_get_pix_fmt(argv[i+1]);
00397 if (dstFormat == PIX_FMT_NONE) {
00398 fprintf(stderr, "invalid pixel format %s\n", argv[i+1]);
00399 return -1;
00400 }
00401 } else {
00402 bad_option:
00403 fprintf(stderr, "bad option or argument missing (%s)\n", argv[i]);
00404 goto error;
00405 }
00406 }
00407
00408 selfTest(src, stride, W, H, srcFormat, dstFormat);
00409 end:
00410 res = 0;
00411 error:
00412 av_free(data);
00413
00414 return res;
00415 }