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