• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libswscale/swscale-test.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003 Michael Niedermayer <michaelni@gmx.at>
00003  *
00004  * This file is part of Libav.
00005  *
00006  * Libav is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * Libav is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with Libav; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 /* HACK Duplicated from swscale_internal.h.
00037  * Should be removed when a cleaner pixel format system exists. */
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 // test by ref -> src -> dst -> out & compare out against ref
00078 // ref & out are YV12
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];
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             if (srcStride[p])
00107                 src[p] = av_mallocz(srcStride[p] * srcH + 16);
00108             if (srcStride[p] && !src[p]) {
00109                 perror("Malloc");
00110                 res = -1;
00111                 goto end;
00112             }
00113         }
00114         srcContext = sws_getContext(w, h, PIX_FMT_YUVA420P, srcW, srcH,
00115                                     srcFormat, SWS_BILINEAR, NULL, NULL, NULL);
00116         if (!srcContext) {
00117             fprintf(stderr, "Failed to get %s ---> %s\n",
00118                     av_pix_fmt_descriptors[PIX_FMT_YUVA420P].name,
00119                     av_pix_fmt_descriptors[srcFormat].name);
00120             res = -1;
00121             goto end;
00122         }
00123         sws_scale(srcContext, ref, refStride, 0, h, src, srcStride);
00124         sws_freeContext(srcContext);
00125 
00126         cur_srcFormat = srcFormat;
00127         cur_srcW      = srcW;
00128         cur_srcH      = srcH;
00129     }
00130 
00131     av_image_fill_linesizes(dstStride, dstFormat, dstW);
00132     for (i = 0; i < 4; i++) {
00133         /* Image buffers passed into libswscale can be allocated any way you
00134          * prefer, as long as they're aligned enough for the architecture, and
00135          * they're freed appropriately (such as using av_free for buffers
00136          * allocated with av_malloc). */
00137         /* An extra 16 bytes is being allocated because some scalers may write
00138          * out of bounds. */
00139         if (dstStride[i])
00140             dst[i] = av_mallocz(dstStride[i] * dstH + 16);
00141         if (dstStride[i] && !dst[i]) {
00142             perror("Malloc");
00143             res = -1;
00144 
00145             goto end;
00146         }
00147     }
00148 
00149     dstContext = sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat,
00150                                 flags, NULL, NULL, NULL);
00151     if (!dstContext) {
00152         fprintf(stderr, "Failed to get %s ---> %s\n",
00153                 av_pix_fmt_descriptors[srcFormat].name,
00154                 av_pix_fmt_descriptors[dstFormat].name);
00155         res = -1;
00156         goto end;
00157     }
00158 
00159     printf(" %s %dx%d -> %s %3dx%3d flags=%2d",
00160            av_pix_fmt_descriptors[srcFormat].name, srcW, srcH,
00161            av_pix_fmt_descriptors[dstFormat].name, dstW, dstH,
00162            flags);
00163     fflush(stdout);
00164 
00165     sws_scale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
00166 
00167     for (i = 0; i < 4 && dstStride[i]; i++)
00168         crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), crc, dst[i],
00169                      dstStride[i] * dstH);
00170 
00171     if (r && crc == r->crc) {
00172         ssdY = r->ssdY;
00173         ssdU = r->ssdU;
00174         ssdV = r->ssdV;
00175         ssdA = r->ssdA;
00176     } else {
00177         for (i = 0; i < 4; i++) {
00178             if (refStride[i])
00179                 out[i] = av_mallocz(refStride[i] * h);
00180             if (refStride[i] && !out[i]) {
00181                 perror("Malloc");
00182                 res = -1;
00183                 goto end;
00184             }
00185         }
00186         outContext = sws_getContext(dstW, dstH, dstFormat, w, h,
00187                                     PIX_FMT_YUVA420P, SWS_BILINEAR,
00188                                     NULL, NULL, NULL);
00189         if (!outContext) {
00190             fprintf(stderr, "Failed to get %s ---> %s\n",
00191                     av_pix_fmt_descriptors[dstFormat].name,
00192                     av_pix_fmt_descriptors[PIX_FMT_YUVA420P].name);
00193             res = -1;
00194             goto end;
00195         }
00196         sws_scale(outContext, dst, dstStride, 0, dstH, out, refStride);
00197 
00198         ssdY = getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
00199         if (hasChroma(srcFormat) && hasChroma(dstFormat)) {
00200             //FIXME check that output is really gray
00201             ssdU = getSSD(ref[1], out[1], refStride[1], refStride[1],
00202                           (w + 1) >> 1, (h + 1) >> 1);
00203             ssdV = getSSD(ref[2], out[2], refStride[2], refStride[2],
00204                           (w + 1) >> 1, (h + 1) >> 1);
00205         }
00206         if (isALPHA(srcFormat) && isALPHA(dstFormat))
00207             ssdA = getSSD(ref[3], out[3], refStride[3], refStride[3], w, h);
00208 
00209         ssdY /= w * h;
00210         ssdU /= w * h / 4;
00211         ssdV /= w * h / 4;
00212         ssdA /= w * h;
00213 
00214         sws_freeContext(outContext);
00215 
00216         for (i = 0; i < 4; i++)
00217             if (refStride[i])
00218                 av_free(out[i]);
00219     }
00220 
00221     printf(" CRC=%08x SSD=%5"PRId64 ",%5"PRId64 ",%5"PRId64 ",%5"PRId64 "\n",
00222            crc, ssdY, ssdU, ssdV, ssdA);
00223 
00224 end:
00225     sws_freeContext(dstContext);
00226 
00227     for (i = 0; i < 4; i++)
00228         if (dstStride[i])
00229             av_free(dst[i]);
00230 
00231     return res;
00232 }
00233 
00234 static void selfTest(uint8_t *ref[4], int refStride[4], int w, int h,
00235                      enum PixelFormat srcFormat_in,
00236                      enum PixelFormat dstFormat_in)
00237 {
00238     const int flags[] = { SWS_FAST_BILINEAR, SWS_BILINEAR, SWS_BICUBIC,
00239                           SWS_X, SWS_POINT, SWS_AREA, 0 };
00240     const int srcW   = w;
00241     const int srcH   = h;
00242     const int dstW[] = { srcW - srcW / 3, srcW, srcW + srcW / 3, 0 };
00243     const int dstH[] = { srcH - srcH / 3, srcH, srcH + srcH / 3, 0 };
00244     enum PixelFormat srcFormat, dstFormat;
00245 
00246     for (srcFormat = srcFormat_in != PIX_FMT_NONE ? srcFormat_in : 0;
00247          srcFormat < PIX_FMT_NB; srcFormat++) {
00248         if (!sws_isSupportedInput(srcFormat) ||
00249             !sws_isSupportedOutput(srcFormat))
00250             continue;
00251 
00252         for (dstFormat = dstFormat_in != PIX_FMT_NONE ? dstFormat_in : 0;
00253              dstFormat < PIX_FMT_NB; dstFormat++) {
00254             int i, j, k;
00255             int res = 0;
00256 
00257             if (!sws_isSupportedInput(dstFormat) ||
00258                 !sws_isSupportedOutput(dstFormat))
00259                 continue;
00260 
00261             printf("%s -> %s\n",
00262                    av_pix_fmt_descriptors[srcFormat].name,
00263                    av_pix_fmt_descriptors[dstFormat].name);
00264             fflush(stdout);
00265 
00266             for (k = 0; flags[k] && !res; k++)
00267                 for (i = 0; dstW[i] && !res; i++)
00268                     for (j = 0; dstH[j] && !res; j++)
00269                         res = doTest(ref, refStride, w, h,
00270                                      srcFormat, dstFormat,
00271                                      srcW, srcH, dstW[i], dstH[j], flags[k],
00272                                      NULL);
00273             if (dstFormat_in != PIX_FMT_NONE)
00274                 break;
00275         }
00276         if (srcFormat_in != PIX_FMT_NONE)
00277             break;
00278     }
00279 }
00280 
00281 static int fileTest(uint8_t *ref[4], int refStride[4], int w, int h, FILE *fp,
00282                     enum PixelFormat srcFormat_in,
00283                     enum PixelFormat dstFormat_in)
00284 {
00285     char buf[256];
00286 
00287     while (fgets(buf, sizeof(buf), fp)) {
00288         struct Results r;
00289         enum PixelFormat srcFormat;
00290         char srcStr[12];
00291         int srcW, srcH;
00292         enum PixelFormat dstFormat;
00293         char dstStr[12];
00294         int dstW, dstH;
00295         int flags;
00296         int ret;
00297 
00298         ret = sscanf(buf,
00299                      " %12s %dx%d -> %12s %dx%d flags=%d CRC=%x"
00300                      " SSD=%"PRId64 ", %"PRId64 ", %"PRId64 ", %"PRId64 "\n",
00301                      srcStr, &srcW, &srcH, dstStr, &dstW, &dstH,
00302                      &flags, &r.crc, &r.ssdY, &r.ssdU, &r.ssdV, &r.ssdA);
00303         if (ret != 12) {
00304             srcStr[0] = dstStr[0] = 0;
00305             ret       = sscanf(buf, "%12s -> %12s\n", srcStr, dstStr);
00306         }
00307 
00308         srcFormat = av_get_pix_fmt(srcStr);
00309         dstFormat = av_get_pix_fmt(dstStr);
00310 
00311         if (srcFormat == PIX_FMT_NONE || dstFormat == PIX_FMT_NONE) {
00312             fprintf(stderr, "malformed input file\n");
00313             return -1;
00314         }
00315         if ((srcFormat_in != PIX_FMT_NONE && srcFormat_in != srcFormat) ||
00316             (dstFormat_in != PIX_FMT_NONE && dstFormat_in != dstFormat))
00317             continue;
00318         if (ret != 12) {
00319             printf("%s", buf);
00320             continue;
00321         }
00322 
00323         doTest(ref, refStride, w, h,
00324                srcFormat, dstFormat,
00325                srcW, srcH, dstW, dstH, flags,
00326                &r);
00327     }
00328 
00329     return 0;
00330 }
00331 
00332 #define W 96
00333 #define H 96
00334 
00335 int main(int argc, char **argv)
00336 {
00337     enum PixelFormat srcFormat = PIX_FMT_NONE;
00338     enum PixelFormat dstFormat = PIX_FMT_NONE;
00339     uint8_t *rgb_data   = av_malloc(W * H * 4);
00340     uint8_t *rgb_src[4] = { rgb_data, NULL, NULL, NULL };
00341     int rgb_stride[4]   = { 4 * W, 0, 0, 0 };
00342     uint8_t *data       = av_malloc(4 * W * H);
00343     uint8_t *src[4]     = { data, data + W * H, data + W * H * 2, data + W * H * 3 };
00344     int stride[4]       = { W, W, W, W };
00345     int x, y;
00346     struct SwsContext *sws;
00347     AVLFG rand;
00348     int res = -1;
00349     int i;
00350 
00351     if (!rgb_data || !data)
00352         return -1;
00353 
00354     sws = sws_getContext(W / 12, H / 12, PIX_FMT_RGB32, W, H,
00355                          PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
00356 
00357     av_lfg_init(&rand, 1);
00358 
00359     for (y = 0; y < H; y++)
00360         for (x = 0; x < W * 4; x++)
00361             rgb_data[ x + y * 4 * W] = av_lfg_get(&rand);
00362     sws_scale(sws, rgb_src, rgb_stride, 0, H, src, stride);
00363     sws_freeContext(sws);
00364     av_free(rgb_data);
00365 
00366     for (i = 1; i < argc; i += 2) {
00367         if (argv[i][0] != '-' || i + 1 == argc)
00368             goto bad_option;
00369         if (!strcmp(argv[i], "-ref")) {
00370             FILE *fp = fopen(argv[i + 1], "r");
00371             if (!fp) {
00372                 fprintf(stderr, "could not open '%s'\n", argv[i + 1]);
00373                 goto error;
00374             }
00375             res = fileTest(src, stride, W, H, fp, srcFormat, dstFormat);
00376             fclose(fp);
00377             goto end;
00378         } else if (!strcmp(argv[i], "-src")) {
00379             srcFormat = av_get_pix_fmt(argv[i + 1]);
00380             if (srcFormat == PIX_FMT_NONE) {
00381                 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
00382                 return -1;
00383             }
00384         } else if (!strcmp(argv[i], "-dst")) {
00385             dstFormat = av_get_pix_fmt(argv[i + 1]);
00386             if (dstFormat == PIX_FMT_NONE) {
00387                 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
00388                 return -1;
00389             }
00390         } else {
00391 bad_option:
00392             fprintf(stderr, "bad option or argument missing (%s)\n", argv[i]);
00393             goto error;
00394         }
00395     }
00396 
00397     selfTest(src, stride, W, H, srcFormat, dstFormat);
00398 end:
00399     res = 0;
00400 error:
00401     av_free(data);
00402 
00403     return res;
00404 }
Generated on Sat Mar 17 2012 12:57:58 for Libav by doxygen 1.7.1