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