• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • 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 FFmpeg.
00005  *
00006  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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/mem.h"
00029 #include "libavutil/avutil.h"
00030 #include "libavutil/lfg.h"
00031 #include "swscale.h"
00032 
00033 /* HACK Duplicated from swscale_internal.h.
00034  * Should be removed when a cleaner pixel format system exists. */
00035 const char *sws_format_name(enum PixelFormat format);
00036 #define isGray(x)       (           \
00037            (x)==PIX_FMT_GRAY8       \
00038         || (x)==PIX_FMT_GRAY16BE    \
00039         || (x)==PIX_FMT_GRAY16LE    \
00040     )
00041 #define hasChroma(x)   (!(          \
00042             isGray(x)               \
00043         || (x)==PIX_FMT_MONOBLACK   \
00044         || (x)==PIX_FMT_MONOWHITE   \
00045     ))
00046 #define isALPHA(x)      (           \
00047            (x)==PIX_FMT_BGR32       \
00048         || (x)==PIX_FMT_BGR32_1     \
00049         || (x)==PIX_FMT_RGB32       \
00050         || (x)==PIX_FMT_RGB32_1     \
00051         || (x)==PIX_FMT_YUVA420P    \
00052     )
00053 
00054 static uint64_t getSSD(uint8_t *src1, uint8_t *src2, int stride1, int stride2, int w, int h)
00055 {
00056     int x,y;
00057     uint64_t ssd=0;
00058 
00059 //printf("%d %d\n", w, h);
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 //printf("%d", abs(src1[x + y*stride1] - src2[x + y*stride2])/26 );
00066         }
00067 //printf("\n");
00068     }
00069     return ssd;
00070 }
00071 
00072 // test by ref -> src -> dst -> out & compare out against ref
00073 // ref & out are YV12
00074 static int doTest(uint8_t *ref[4], int refStride[4], int w, int h,
00075                   enum PixelFormat srcFormat, enum PixelFormat dstFormat,
00076                   int srcW, int srcH, int dstW, int dstH, int flags)
00077 {
00078     uint8_t *src[4] = {0};
00079     uint8_t *dst[4] = {0};
00080     uint8_t *out[4] = {0};
00081     int srcStride[4], dstStride[4];
00082     int i;
00083     uint64_t ssdY, ssdU=0, ssdV=0, ssdA=0;
00084     struct SwsContext *srcContext = NULL, *dstContext = NULL,
00085                       *outContext = NULL;
00086     int res;
00087 
00088     res = 0;
00089     for (i=0; i<4; i++) {
00090         // avoid stride % bpp != 0
00091         if (srcFormat==PIX_FMT_RGB24 || srcFormat==PIX_FMT_BGR24)
00092             srcStride[i]= srcW*3;
00093         else if (srcFormat==PIX_FMT_RGB48BE || srcFormat==PIX_FMT_RGB48LE)
00094             srcStride[i]= srcW*6;
00095         else
00096             srcStride[i]= srcW*4;
00097 
00098         if (dstFormat==PIX_FMT_RGB24 || dstFormat==PIX_FMT_BGR24)
00099             dstStride[i]= dstW*3;
00100         else if (dstFormat==PIX_FMT_RGB48BE || dstFormat==PIX_FMT_RGB48LE)
00101             dstStride[i]= dstW*6;
00102         else
00103             dstStride[i]= dstW*4;
00104 
00105         /* Image buffers passed into libswscale can be allocated any way you
00106          * prefer, as long as they're aligned enough for the architecture, and
00107          * they're freed appropriately (such as using av_free for buffers
00108          * allocated with av_malloc). */
00109         src[i]= av_mallocz(srcStride[i]*srcH);
00110         dst[i]= av_mallocz(dstStride[i]*dstH);
00111         out[i]= av_mallocz(refStride[i]*h);
00112         if (!src[i] || !dst[i] || !out[i]) {
00113             perror("Malloc");
00114             res = -1;
00115 
00116             goto end;
00117         }
00118     }
00119 
00120     srcContext= sws_getContext(w, h, PIX_FMT_YUVA420P, srcW, srcH, srcFormat, flags, NULL, NULL, NULL);
00121     if (!srcContext) {
00122         fprintf(stderr, "Failed to get %s ---> %s\n",
00123                 sws_format_name(PIX_FMT_YUVA420P),
00124                 sws_format_name(srcFormat));
00125         res = -1;
00126 
00127         goto end;
00128     }
00129     dstContext= sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, NULL, NULL, NULL);
00130     if (!dstContext) {
00131         fprintf(stderr, "Failed to get %s ---> %s\n",
00132                 sws_format_name(srcFormat),
00133                 sws_format_name(dstFormat));
00134         res = -1;
00135 
00136         goto end;
00137     }
00138     outContext= sws_getContext(dstW, dstH, dstFormat, w, h, PIX_FMT_YUVA420P, flags, NULL, NULL, NULL);
00139     if (!outContext) {
00140         fprintf(stderr, "Failed to get %s ---> %s\n",
00141                 sws_format_name(dstFormat),
00142                 sws_format_name(PIX_FMT_YUVA420P));
00143         res = -1;
00144 
00145         goto end;
00146     }
00147 //    printf("test %X %X %X -> %X %X %X\n", (int)ref[0], (int)ref[1], (int)ref[2],
00148 //        (int)src[0], (int)src[1], (int)src[2]);
00149 
00150     sws_scale(srcContext, ref, refStride, 0, h   , src, srcStride);
00151     sws_scale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
00152     sws_scale(outContext, dst, dstStride, 0, dstH, out, refStride);
00153 
00154     ssdY= getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
00155     if (hasChroma(srcFormat) && hasChroma(dstFormat)) {
00156         //FIXME check that output is really gray
00157         ssdU= getSSD(ref[1], out[1], refStride[1], refStride[1], (w+1)>>1, (h+1)>>1);
00158         ssdV= getSSD(ref[2], out[2], refStride[2], refStride[2], (w+1)>>1, (h+1)>>1);
00159     }
00160     if (isALPHA(srcFormat) && isALPHA(dstFormat))
00161         ssdA= getSSD(ref[3], out[3], refStride[3], refStride[3], w, h);
00162 
00163     ssdY/= w*h;
00164     ssdU/= w*h/4;
00165     ssdV/= w*h/4;
00166     ssdA/= w*h;
00167 
00168     printf(" %s %dx%d -> %s %4dx%4d flags=%2d SSD=%5"PRId64",%5"PRId64",%5"PRId64",%5"PRId64"\n",
00169            sws_format_name(srcFormat), srcW, srcH,
00170            sws_format_name(dstFormat), dstW, dstH,
00171            flags, ssdY, ssdU, ssdV, ssdA);
00172     fflush(stdout);
00173 
00174 end:
00175 
00176     sws_freeContext(srcContext);
00177     sws_freeContext(dstContext);
00178     sws_freeContext(outContext);
00179 
00180     for (i=0; i<4; i++) {
00181         av_free(src[i]);
00182         av_free(dst[i]);
00183         av_free(out[i]);
00184     }
00185 
00186     return res;
00187 }
00188 
00189 static void selfTest(uint8_t *ref[4], int refStride[4], int w, int h)
00190 {
00191     const int flags[] = { SWS_FAST_BILINEAR,
00192                           SWS_BILINEAR, SWS_BICUBIC,
00193                           SWS_X       , SWS_POINT  , SWS_AREA, 0 };
00194     const int srcW = w;
00195     const int srcH = h;
00196     const int dstW[] = { srcW - srcW/3, srcW, srcW + srcW/3, 0 };
00197     const int dstH[] = { srcH - srcH/3, srcH, srcH + srcH/3, 0 };
00198     enum PixelFormat srcFormat, dstFormat;
00199 
00200     for (srcFormat = 0; srcFormat < PIX_FMT_NB; srcFormat++) {
00201         if (!sws_isSupportedInput(srcFormat) || !sws_isSupportedOutput(srcFormat))
00202             continue;
00203 
00204         for (dstFormat = 0; dstFormat < PIX_FMT_NB; dstFormat++) {
00205             int i, j, k;
00206             int res = 0;
00207 
00208             if (!sws_isSupportedInput(dstFormat) || !sws_isSupportedOutput(dstFormat))
00209                 continue;
00210 
00211             printf("%s -> %s\n",
00212                    sws_format_name(srcFormat),
00213                    sws_format_name(dstFormat));
00214             fflush(stdout);
00215 
00216             for (i = 0; dstW[i] && !res; i++)
00217                 for (j = 0; dstH[j] && !res; j++)
00218                     for (k = 0; flags[k] && !res; k++)
00219                         res = doTest(ref, refStride, w, h, srcFormat, dstFormat,
00220                                      srcW, srcH, dstW[i], dstH[j], flags[k]);
00221         }
00222     }
00223 }
00224 
00225 #define W 96
00226 #define H 96
00227 
00228 int main(int argc, char **argv)
00229 {
00230     uint8_t *rgb_data = av_malloc (W*H*4);
00231     uint8_t *rgb_src[3]= {rgb_data, NULL, NULL};
00232     int rgb_stride[3]={4*W, 0, 0};
00233     uint8_t *data = av_malloc (4*W*H);
00234     uint8_t *src[4]= {data, data+W*H, data+W*H*2, data+W*H*3};
00235     int stride[4]={W, W, W, W};
00236     int x, y;
00237     struct SwsContext *sws;
00238     AVLFG rand;
00239 
00240     if (!rgb_data || !data)
00241         return -1;
00242 
00243     sws= sws_getContext(W/12, H/12, PIX_FMT_RGB32, W, H, PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
00244 
00245     av_lfg_init(&rand, 1);
00246 
00247     for (y=0; y<H; y++) {
00248         for (x=0; x<W*4; x++) {
00249             rgb_data[ x + y*4*W]= av_lfg_get(&rand);
00250         }
00251     }
00252     sws_scale(sws, rgb_src, rgb_stride, 0, H, src, stride);
00253     sws_freeContext(sws);
00254     av_free(rgb_data);
00255 
00256     selfTest(src, stride, W, H);
00257     av_free(data);
00258 
00259     return 0;
00260 }

Generated on Fri Sep 16 2011 17:17:52 for FFmpeg by  doxygen 1.7.1