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