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) == AV_PIX_FMT_GRAY8 || \
00040 (x) == AV_PIX_FMT_Y400A || \
00041 (x) == AV_PIX_FMT_GRAY16BE || \
00042 (x) == AV_PIX_FMT_GRAY16LE)
00043 #define hasChroma(x) \
00044 (!(isGray(x) || \
00045 (x) == AV_PIX_FMT_MONOBLACK || \
00046 (x) == AV_PIX_FMT_MONOWHITE))
00047 #define isALPHA(x) \
00048 ((x) == AV_PIX_FMT_BGR32 || \
00049 (x) == AV_PIX_FMT_BGR32_1 || \
00050 (x) == AV_PIX_FMT_RGB32 || \
00051 (x) == AV_PIX_FMT_RGB32_1 || \
00052 (x) == AV_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 AVPixelFormat srcFormat, enum AVPixelFormat dstFormat,
00081 int srcW, int srcH, int dstW, int dstH, int flags,
00082 struct Results *r)
00083 {
00084 const AVPixFmtDescriptor *desc_yuva420p = av_pix_fmt_desc_get(AV_PIX_FMT_YUVA420P);
00085 const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(srcFormat);
00086 const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(dstFormat);
00087 static enum AVPixelFormat cur_srcFormat;
00088 static int cur_srcW, cur_srcH;
00089 static uint8_t *src[4];
00090 static int srcStride[4];
00091 uint8_t *dst[4] = { 0 };
00092 uint8_t *out[4] = { 0 };
00093 int dstStride[4] = {0};
00094 int i;
00095 uint64_t ssdY, ssdU = 0, ssdV = 0, ssdA = 0;
00096 struct SwsContext *dstContext = NULL, *outContext = NULL;
00097 uint32_t crc = 0;
00098 int res = 0;
00099
00100 if (cur_srcFormat != srcFormat || cur_srcW != srcW || cur_srcH != srcH) {
00101 struct SwsContext *srcContext = NULL;
00102 int p;
00103
00104 for (p = 0; p < 4; p++)
00105 av_freep(&src[p]);
00106
00107 av_image_fill_linesizes(srcStride, srcFormat, srcW);
00108 for (p = 0; p < 4; p++) {
00109 srcStride[p] = FFALIGN(srcStride[p], 16);
00110 if (srcStride[p])
00111 src[p] = av_mallocz(srcStride[p] * srcH + 16);
00112 if (srcStride[p] && !src[p]) {
00113 perror("Malloc");
00114 res = -1;
00115 goto end;
00116 }
00117 }
00118 srcContext = sws_getContext(w, h, AV_PIX_FMT_YUVA420P, srcW, srcH,
00119 srcFormat, SWS_BILINEAR, NULL, NULL, NULL);
00120 if (!srcContext) {
00121 fprintf(stderr, "Failed to get %s ---> %s\n",
00122 desc_yuva420p->name,
00123 desc_src->name);
00124 res = -1;
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,
00155 flags, NULL, NULL, NULL);
00156 if (!dstContext) {
00157 fprintf(stderr, "Failed to get %s ---> %s\n",
00158 desc_src->name, desc_dst->name);
00159 res = -1;
00160 goto end;
00161 }
00162
00163 printf(" %s %dx%d -> %s %3dx%3d flags=%2d",
00164 desc_src->name, srcW, srcH,
00165 desc_dst->name, dstW, dstH,
00166 flags);
00167 fflush(stdout);
00168
00169 sws_scale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
00170
00171 for (i = 0; i < 4 && dstStride[i]; i++)
00172 crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), crc, dst[i],
00173 dstStride[i] * dstH);
00174
00175 if (r && crc == r->crc) {
00176 ssdY = r->ssdY;
00177 ssdU = r->ssdU;
00178 ssdV = r->ssdV;
00179 ssdA = r->ssdA;
00180 } else {
00181 for (i = 0; i < 4; i++) {
00182 refStride[i] = FFALIGN(refStride[i], 16);
00183 if (refStride[i])
00184 out[i] = av_mallocz(refStride[i] * h);
00185 if (refStride[i] && !out[i]) {
00186 perror("Malloc");
00187 res = -1;
00188 goto end;
00189 }
00190 }
00191 outContext = sws_getContext(dstW, dstH, dstFormat, w, h,
00192 AV_PIX_FMT_YUVA420P, SWS_BILINEAR,
00193 NULL, NULL, NULL);
00194 if (!outContext) {
00195 fprintf(stderr, "Failed to get %s ---> %s\n",
00196 desc_dst->name,
00197 desc_yuva420p->name);
00198 res = -1;
00199 goto end;
00200 }
00201 sws_scale(outContext, dst, dstStride, 0, dstH, out, refStride);
00202
00203 ssdY = getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
00204 if (hasChroma(srcFormat) && hasChroma(dstFormat)) {
00205
00206 ssdU = getSSD(ref[1], out[1], refStride[1], refStride[1],
00207 (w + 1) >> 1, (h + 1) >> 1);
00208 ssdV = getSSD(ref[2], out[2], refStride[2], refStride[2],
00209 (w + 1) >> 1, (h + 1) >> 1);
00210 }
00211 if (isALPHA(srcFormat) && isALPHA(dstFormat))
00212 ssdA = getSSD(ref[3], out[3], refStride[3], refStride[3], w, h);
00213
00214 ssdY /= w * h;
00215 ssdU /= w * h / 4;
00216 ssdV /= w * h / 4;
00217 ssdA /= w * h;
00218
00219 sws_freeContext(outContext);
00220
00221 for (i = 0; i < 4; i++)
00222 if (refStride[i])
00223 av_free(out[i]);
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 sws_freeContext(dstContext);
00231
00232 for (i = 0; i < 4; i++)
00233 if (dstStride[i])
00234 av_free(dst[i]);
00235
00236 return res;
00237 }
00238
00239 static void selfTest(uint8_t *ref[4], int refStride[4], int w, int h,
00240 enum AVPixelFormat srcFormat_in,
00241 enum AVPixelFormat dstFormat_in)
00242 {
00243 const int flags[] = { SWS_FAST_BILINEAR, SWS_BILINEAR, SWS_BICUBIC,
00244 SWS_X, SWS_POINT, SWS_AREA, 0 };
00245 const int srcW = w;
00246 const int srcH = h;
00247 const int dstW[] = { srcW - srcW / 3, srcW, srcW + srcW / 3, 0 };
00248 const int dstH[] = { srcH - srcH / 3, srcH, srcH + srcH / 3, 0 };
00249 enum AVPixelFormat srcFormat, dstFormat;
00250 const AVPixFmtDescriptor *desc_src, *desc_dst;
00251
00252 for (srcFormat = srcFormat_in != AV_PIX_FMT_NONE ? srcFormat_in : 0;
00253 srcFormat < AV_PIX_FMT_NB; srcFormat++) {
00254 if (!sws_isSupportedInput(srcFormat) ||
00255 !sws_isSupportedOutput(srcFormat))
00256 continue;
00257
00258 desc_src = av_pix_fmt_desc_get(srcFormat);
00259
00260 for (dstFormat = dstFormat_in != AV_PIX_FMT_NONE ? dstFormat_in : 0;
00261 dstFormat < AV_PIX_FMT_NB; dstFormat++) {
00262 int i, j, k;
00263 int res = 0;
00264
00265 if (!sws_isSupportedInput(dstFormat) ||
00266 !sws_isSupportedOutput(dstFormat))
00267 continue;
00268
00269 desc_dst = av_pix_fmt_desc_get(dstFormat);
00270
00271 printf("%s -> %s\n", desc_src->name, desc_dst->name);
00272 fflush(stdout);
00273
00274 for (k = 0; flags[k] && !res; k++)
00275 for (i = 0; dstW[i] && !res; i++)
00276 for (j = 0; dstH[j] && !res; j++)
00277 res = doTest(ref, refStride, w, h,
00278 srcFormat, dstFormat,
00279 srcW, srcH, dstW[i], dstH[j], flags[k],
00280 NULL);
00281 if (dstFormat_in != AV_PIX_FMT_NONE)
00282 break;
00283 }
00284 if (srcFormat_in != AV_PIX_FMT_NONE)
00285 break;
00286 }
00287 }
00288
00289 static int fileTest(uint8_t *ref[4], int refStride[4], int w, int h, FILE *fp,
00290 enum AVPixelFormat srcFormat_in,
00291 enum AVPixelFormat dstFormat_in)
00292 {
00293 char buf[256];
00294
00295 while (fgets(buf, sizeof(buf), fp)) {
00296 struct Results r;
00297 enum AVPixelFormat srcFormat;
00298 char srcStr[12];
00299 int srcW, srcH;
00300 enum AVPixelFormat dstFormat;
00301 char dstStr[12];
00302 int dstW, dstH;
00303 int flags;
00304 int ret;
00305
00306 ret = sscanf(buf,
00307 " %12s %dx%d -> %12s %dx%d flags=%d CRC=%x"
00308 " SSD=%"PRId64 ", %"PRId64 ", %"PRId64 ", %"PRId64 "\n",
00309 srcStr, &srcW, &srcH, dstStr, &dstW, &dstH,
00310 &flags, &r.crc, &r.ssdY, &r.ssdU, &r.ssdV, &r.ssdA);
00311 if (ret != 12) {
00312 srcStr[0] = dstStr[0] = 0;
00313 ret = sscanf(buf, "%12s -> %12s\n", srcStr, dstStr);
00314 }
00315
00316 srcFormat = av_get_pix_fmt(srcStr);
00317 dstFormat = av_get_pix_fmt(dstStr);
00318
00319 if (srcFormat == AV_PIX_FMT_NONE || dstFormat == AV_PIX_FMT_NONE ||
00320 srcW > 8192U || srcH > 8192U || dstW > 8192U || dstH > 8192U) {
00321 fprintf(stderr, "malformed input file\n");
00322 return -1;
00323 }
00324 if ((srcFormat_in != AV_PIX_FMT_NONE && srcFormat_in != srcFormat) ||
00325 (dstFormat_in != AV_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 AVPixelFormat srcFormat = AV_PIX_FMT_NONE;
00347 enum AVPixelFormat dstFormat = AV_PIX_FMT_NONE;
00348 uint8_t *rgb_data = av_malloc(W * H * 4);
00349 uint8_t *rgb_src[4] = { rgb_data, NULL, NULL, NULL };
00350 int rgb_stride[4] = { 4 * W, 0, 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 FILE *fp = NULL;
00360
00361 if (!rgb_data || !data)
00362 return -1;
00363
00364 for (i = 1; i < argc; i += 2) {
00365 if (argv[i][0] != '-' || i + 1 == argc)
00366 goto bad_option;
00367 if (!strcmp(argv[i], "-ref")) {
00368 fp = fopen(argv[i + 1], "r");
00369 if (!fp) {
00370 fprintf(stderr, "could not open '%s'\n", argv[i + 1]);
00371 goto error;
00372 }
00373 } else if (!strcmp(argv[i], "-src")) {
00374 srcFormat = av_get_pix_fmt(argv[i + 1]);
00375 if (srcFormat == AV_PIX_FMT_NONE) {
00376 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
00377 return -1;
00378 }
00379 } else if (!strcmp(argv[i], "-dst")) {
00380 dstFormat = av_get_pix_fmt(argv[i + 1]);
00381 if (dstFormat == AV_PIX_FMT_NONE) {
00382 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
00383 return -1;
00384 }
00385 } else {
00386 bad_option:
00387 fprintf(stderr, "bad option or argument missing (%s)\n", argv[i]);
00388 goto error;
00389 }
00390 }
00391
00392 sws = sws_getContext(W / 12, H / 12, AV_PIX_FMT_RGB32, W, H,
00393 AV_PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
00394
00395 av_lfg_init(&rand, 1);
00396
00397 for (y = 0; y < H; y++)
00398 for (x = 0; x < W * 4; x++)
00399 rgb_data[ x + y * 4 * W] = av_lfg_get(&rand);
00400 sws_scale(sws, rgb_src, rgb_stride, 0, H, src, stride);
00401 sws_freeContext(sws);
00402 av_free(rgb_data);
00403
00404 if(fp) {
00405 res = fileTest(src, stride, W, H, fp, srcFormat, dstFormat);
00406 fclose(fp);
00407 } else {
00408 selfTest(src, stride, W, H, srcFormat, dstFormat);
00409 res = 0;
00410 }
00411 error:
00412 av_free(data);
00413
00414 return res;
00415 }