libswscale/utils.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-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 #define _SVID_SOURCE //needed for MAP_ANONYMOUS
00022 #define _DARWIN_C_SOURCE // needed for MAP_ANON
00023 #include <inttypes.h>
00024 #include <string.h>
00025 #include <math.h>
00026 #include <stdio.h>
00027 #include "config.h"
00028 #include <assert.h>
00029 #if HAVE_SYS_MMAN_H
00030 #include <sys/mman.h>
00031 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
00032 #define MAP_ANONYMOUS MAP_ANON
00033 #endif
00034 #endif
00035 #if HAVE_VIRTUALALLOC
00036 #define WIN32_LEAN_AND_MEAN
00037 #include <windows.h>
00038 #endif
00039 #include "swscale.h"
00040 #include "swscale_internal.h"
00041 #include "rgb2rgb.h"
00042 #include "libavutil/intreadwrite.h"
00043 #include "libavutil/x86_cpu.h"
00044 #include "libavutil/cpu.h"
00045 #include "libavutil/avutil.h"
00046 #include "libavutil/bswap.h"
00047 #include "libavutil/mathematics.h"
00048 #include "libavutil/opt.h"
00049 #include "libavutil/pixdesc.h"
00050 #include "libavutil/avassert.h"
00051 
00052 unsigned swscale_version(void)
00053 {
00054     av_assert0(LIBSWSCALE_VERSION_MICRO >= 100);
00055     return LIBSWSCALE_VERSION_INT;
00056 }
00057 
00058 const char *swscale_configuration(void)
00059 {
00060     return FFMPEG_CONFIGURATION;
00061 }
00062 
00063 const char *swscale_license(void)
00064 {
00065 #define LICENSE_PREFIX "libswscale license: "
00066     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
00067 }
00068 
00069 #define RET 0xC3 //near return opcode for x86
00070 
00071 typedef struct FormatEntry {
00072     int is_supported_in, is_supported_out;
00073 } FormatEntry;
00074 
00075 static const FormatEntry format_entries[PIX_FMT_NB] = {
00076     [PIX_FMT_YUV420P]     = { 1 , 1 },
00077     [PIX_FMT_YUYV422]     = { 1 , 1 },
00078     [PIX_FMT_RGB24]       = { 1 , 1 },
00079     [PIX_FMT_BGR24]       = { 1 , 1 },
00080     [PIX_FMT_YUV422P]     = { 1 , 1 },
00081     [PIX_FMT_YUV444P]     = { 1 , 1 },
00082     [PIX_FMT_YUV410P]     = { 1 , 1 },
00083     [PIX_FMT_YUV411P]     = { 1 , 1 },
00084     [PIX_FMT_GRAY8]       = { 1 , 1 },
00085     [PIX_FMT_MONOWHITE]   = { 1 , 1 },
00086     [PIX_FMT_MONOBLACK]   = { 1 , 1 },
00087     [PIX_FMT_PAL8]        = { 1 , 0 },
00088     [PIX_FMT_YUVJ420P]    = { 1 , 1 },
00089     [PIX_FMT_YUVJ422P]    = { 1 , 1 },
00090     [PIX_FMT_YUVJ444P]    = { 1 , 1 },
00091     [PIX_FMT_UYVY422]     = { 1 , 1 },
00092     [PIX_FMT_UYYVYY411]   = { 0 , 0 },
00093     [PIX_FMT_BGR8]        = { 1 , 1 },
00094     [PIX_FMT_BGR4]        = { 0 , 1 },
00095     [PIX_FMT_BGR4_BYTE]   = { 1 , 1 },
00096     [PIX_FMT_RGB8]        = { 1 , 1 },
00097     [PIX_FMT_RGB4]        = { 0 , 1 },
00098     [PIX_FMT_RGB4_BYTE]   = { 1 , 1 },
00099     [PIX_FMT_NV12]        = { 1 , 1 },
00100     [PIX_FMT_NV21]        = { 1 , 1 },
00101     [PIX_FMT_ARGB]        = { 1 , 1 },
00102     [PIX_FMT_RGBA]        = { 1 , 1 },
00103     [PIX_FMT_ABGR]        = { 1 , 1 },
00104     [PIX_FMT_BGRA]        = { 1 , 1 },
00105     [PIX_FMT_0RGB]        = { 1 , 1 },
00106     [PIX_FMT_RGB0]        = { 1 , 1 },
00107     [PIX_FMT_0BGR]        = { 1 , 1 },
00108     [PIX_FMT_BGR0]        = { 1 , 1 },
00109     [PIX_FMT_GRAY16BE]    = { 1 , 1 },
00110     [PIX_FMT_GRAY16LE]    = { 1 , 1 },
00111     [PIX_FMT_YUV440P]     = { 1 , 1 },
00112     [PIX_FMT_YUVJ440P]    = { 1 , 1 },
00113     [PIX_FMT_YUVA420P]    = { 1 , 1 },
00114     [PIX_FMT_RGB48BE]     = { 1 , 1 },
00115     [PIX_FMT_RGB48LE]     = { 1 , 1 },
00116     [PIX_FMT_RGBA64BE]    = { 0 , 0 },
00117     [PIX_FMT_RGBA64LE]    = { 0 , 0 },
00118     [PIX_FMT_RGB565BE]    = { 1 , 1 },
00119     [PIX_FMT_RGB565LE]    = { 1 , 1 },
00120     [PIX_FMT_RGB555BE]    = { 1 , 1 },
00121     [PIX_FMT_RGB555LE]    = { 1 , 1 },
00122     [PIX_FMT_BGR565BE]    = { 1 , 1 },
00123     [PIX_FMT_BGR565LE]    = { 1 , 1 },
00124     [PIX_FMT_BGR555BE]    = { 1 , 1 },
00125     [PIX_FMT_BGR555LE]    = { 1 , 1 },
00126     [PIX_FMT_YUV420P16LE] = { 1 , 1 },
00127     [PIX_FMT_YUV420P16BE] = { 1 , 1 },
00128     [PIX_FMT_YUV422P16LE] = { 1 , 1 },
00129     [PIX_FMT_YUV422P16BE] = { 1 , 1 },
00130     [PIX_FMT_YUV444P16LE] = { 1 , 1 },
00131     [PIX_FMT_YUV444P16BE] = { 1 , 1 },
00132     [PIX_FMT_RGB444LE]    = { 1 , 1 },
00133     [PIX_FMT_RGB444BE]    = { 1 , 1 },
00134     [PIX_FMT_BGR444LE]    = { 1 , 1 },
00135     [PIX_FMT_BGR444BE]    = { 1 , 1 },
00136     [PIX_FMT_Y400A]       = { 1 , 0 },
00137     [PIX_FMT_BGR48BE]     = { 1 , 1 },
00138     [PIX_FMT_BGR48LE]     = { 1 , 1 },
00139     [PIX_FMT_BGRA64BE]    = { 0 , 0 },
00140     [PIX_FMT_BGRA64LE]    = { 0 , 0 },
00141     [PIX_FMT_YUV420P9BE]  = { 1 , 1 },
00142     [PIX_FMT_YUV420P9LE]  = { 1 , 1 },
00143     [PIX_FMT_YUV420P10BE] = { 1 , 1 },
00144     [PIX_FMT_YUV420P10LE] = { 1 , 1 },
00145     [PIX_FMT_YUV422P9BE]  = { 1 , 1 },
00146     [PIX_FMT_YUV422P9LE]  = { 1 , 1 },
00147     [PIX_FMT_YUV422P10BE] = { 1 , 1 },
00148     [PIX_FMT_YUV422P10LE] = { 1 , 1 },
00149     [PIX_FMT_YUV444P9BE]  = { 1 , 1 },
00150     [PIX_FMT_YUV444P9LE]  = { 1 , 1 },
00151     [PIX_FMT_YUV444P10BE] = { 1 , 1 },
00152     [PIX_FMT_YUV444P10LE] = { 1 , 1 },
00153     [PIX_FMT_GBR24P]      = { 1 , 0 },
00154     [PIX_FMT_GBRP]        = { 1 , 0 },
00155     [PIX_FMT_GBRP9LE]     = { 1 , 0 },
00156     [PIX_FMT_GBRP9BE]     = { 1 , 0 },
00157     [PIX_FMT_GBRP10LE]    = { 1 , 0 },
00158     [PIX_FMT_GBRP10BE]    = { 1 , 0 },
00159     [PIX_FMT_GBRP16LE]    = { 1 , 0 },
00160     [PIX_FMT_GBRP16BE]    = { 1 , 0 },
00161 };
00162 
00163 int sws_isSupportedInput(enum PixelFormat pix_fmt)
00164 {
00165     return (unsigned)pix_fmt < PIX_FMT_NB ?
00166         format_entries[pix_fmt].is_supported_in : 0;
00167 }
00168 
00169 int sws_isSupportedOutput(enum PixelFormat pix_fmt)
00170 {
00171     return (unsigned)pix_fmt < PIX_FMT_NB ?
00172         format_entries[pix_fmt].is_supported_out : 0;
00173 }
00174 
00175 extern const int32_t ff_yuv2rgb_coeffs[8][4];
00176 
00177 #if FF_API_SWS_FORMAT_NAME
00178 const char *sws_format_name(enum PixelFormat format)
00179 {
00180     return av_get_pix_fmt_name(format);
00181 }
00182 #endif
00183 
00184 static double getSplineCoeff(double a, double b, double c, double d, double dist)
00185 {
00186     if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
00187     else           return getSplineCoeff(        0.0,
00188                                           b+ 2.0*c + 3.0*d,
00189                                                  c + 3.0*d,
00190                                          -b- 3.0*c - 6.0*d,
00191                                          dist-1.0);
00192 }
00193 
00194 static int initFilter(int16_t **outFilter, int32_t **filterPos, int *outFilterSize, int xInc,
00195                       int srcW, int dstW, int filterAlign, int one, int flags, int cpu_flags,
00196                       SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
00197 {
00198     int i;
00199     int filterSize;
00200     int filter2Size;
00201     int minFilterSize;
00202     int64_t *filter=NULL;
00203     int64_t *filter2=NULL;
00204     const int64_t fone= 1LL<<54;
00205     int ret= -1;
00206 
00207     emms_c(); //FIXME this should not be required but it IS (even for non-MMX versions)
00208 
00209     // NOTE: the +3 is for the MMX(+1)/SSE(+3) scaler which reads over the end
00210     FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW+3)*sizeof(**filterPos), fail);
00211 
00212     if (FFABS(xInc - 0x10000) <10) { // unscaled
00213         int i;
00214         filterSize= 1;
00215         FF_ALLOCZ_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
00216 
00217         for (i=0; i<dstW; i++) {
00218             filter[i*filterSize]= fone;
00219             (*filterPos)[i]=i;
00220         }
00221 
00222     } else if (flags&SWS_POINT) { // lame looking point sampling mode
00223         int i;
00224         int64_t xDstInSrc;
00225         filterSize= 1;
00226         FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
00227 
00228         xDstInSrc= xInc/2 - 0x8000;
00229         for (i=0; i<dstW; i++) {
00230             int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
00231 
00232             (*filterPos)[i]= xx;
00233             filter[i]= fone;
00234             xDstInSrc+= xInc;
00235         }
00236     } else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) { // bilinear upscale
00237         int i;
00238         int64_t xDstInSrc;
00239         filterSize= 2;
00240         FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
00241 
00242         xDstInSrc= xInc/2 - 0x8000;
00243         for (i=0; i<dstW; i++) {
00244             int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
00245             int j;
00246 
00247             (*filterPos)[i]= xx;
00248             //bilinear upscale / linear interpolate / area averaging
00249             for (j=0; j<filterSize; j++) {
00250                 int64_t coeff= fone - FFABS(((int64_t)xx<<16) - xDstInSrc)*(fone>>16);
00251                 if (coeff<0) coeff=0;
00252                 filter[i*filterSize + j]= coeff;
00253                 xx++;
00254             }
00255             xDstInSrc+= xInc;
00256         }
00257     } else {
00258         int64_t xDstInSrc;
00259         int sizeFactor;
00260 
00261         if      (flags&SWS_BICUBIC)      sizeFactor=  4;
00262         else if (flags&SWS_X)            sizeFactor=  8;
00263         else if (flags&SWS_AREA)         sizeFactor=  1; //downscale only, for upscale it is bilinear
00264         else if (flags&SWS_GAUSS)        sizeFactor=  8;   // infinite ;)
00265         else if (flags&SWS_LANCZOS)      sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
00266         else if (flags&SWS_SINC)         sizeFactor= 20; // infinite ;)
00267         else if (flags&SWS_SPLINE)       sizeFactor= 20;  // infinite ;)
00268         else if (flags&SWS_BILINEAR)     sizeFactor=  2;
00269         else {
00270             sizeFactor= 0; //GCC warning killer
00271             assert(0);
00272         }
00273 
00274         if (xInc <= 1<<16)      filterSize= 1 + sizeFactor; // upscale
00275         else                    filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
00276 
00277         filterSize = av_clip(filterSize, 1, srcW - 2);
00278 
00279         FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
00280 
00281         xDstInSrc= xInc - 0x10000;
00282         for (i=0; i<dstW; i++) {
00283             int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
00284             int j;
00285             (*filterPos)[i]= xx;
00286             for (j=0; j<filterSize; j++) {
00287                 int64_t d= (FFABS(((int64_t)xx<<17) - xDstInSrc))<<13;
00288                 double floatd;
00289                 int64_t coeff;
00290 
00291                 if (xInc > 1<<16)
00292                     d= d*dstW/srcW;
00293                 floatd= d * (1.0/(1<<30));
00294 
00295                 if (flags & SWS_BICUBIC) {
00296                     int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] :   0) * (1<<24);
00297                     int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
00298 
00299                     if (d >= 1LL<<31) {
00300                         coeff = 0.0;
00301                     } else {
00302                         int64_t dd  = (d  * d) >> 30;
00303                         int64_t ddd = (dd * d) >> 30;
00304 
00305                         if (d < 1LL<<30)
00306                             coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
00307                         else
00308                             coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
00309                     }
00310                     coeff *= fone>>(30+24);
00311                 }
00312 /*                else if (flags & SWS_X) {
00313                     double p= param ? param*0.01 : 0.3;
00314                     coeff = d ? sin(d*M_PI)/(d*M_PI) : 1.0;
00315                     coeff*= pow(2.0, - p*d*d);
00316                 }*/
00317                 else if (flags & SWS_X) {
00318                     double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
00319                     double c;
00320 
00321                     if (floatd<1.0)
00322                         c = cos(floatd*M_PI);
00323                     else
00324                         c=-1.0;
00325                     if (c<0.0)      c= -pow(-c, A);
00326                     else            c=  pow( c, A);
00327                     coeff= (c*0.5 + 0.5)*fone;
00328                 } else if (flags & SWS_AREA) {
00329                     int64_t d2= d - (1<<29);
00330                     if      (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
00331                     else if (d2*xInc <  (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
00332                     else coeff=0.0;
00333                     coeff *= fone>>(30+16);
00334                 } else if (flags & SWS_GAUSS) {
00335                     double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
00336                     coeff = (pow(2.0, - p*floatd*floatd))*fone;
00337                 } else if (flags & SWS_SINC) {
00338                     coeff = (d ? sin(floatd*M_PI)/(floatd*M_PI) : 1.0)*fone;
00339                 } else if (flags & SWS_LANCZOS) {
00340                     double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
00341                     coeff = (d ? sin(floatd*M_PI)*sin(floatd*M_PI/p)/(floatd*floatd*M_PI*M_PI/p) : 1.0)*fone;
00342                     if (floatd>p) coeff=0;
00343                 } else if (flags & SWS_BILINEAR) {
00344                     coeff= (1<<30) - d;
00345                     if (coeff<0) coeff=0;
00346                     coeff *= fone >> 30;
00347                 } else if (flags & SWS_SPLINE) {
00348                     double p=-2.196152422706632;
00349                     coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
00350                 } else {
00351                     coeff= 0.0; //GCC warning killer
00352                     assert(0);
00353                 }
00354 
00355                 filter[i*filterSize + j]= coeff;
00356                 xx++;
00357             }
00358             xDstInSrc+= 2*xInc;
00359         }
00360     }
00361 
00362     /* apply src & dst Filter to filter -> filter2
00363        av_free(filter);
00364     */
00365     assert(filterSize>0);
00366     filter2Size= filterSize;
00367     if (srcFilter) filter2Size+= srcFilter->length - 1;
00368     if (dstFilter) filter2Size+= dstFilter->length - 1;
00369     assert(filter2Size>0);
00370     FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size*dstW*sizeof(*filter2), fail);
00371 
00372     for (i=0; i<dstW; i++) {
00373         int j, k;
00374 
00375         if(srcFilter) {
00376             for (k=0; k<srcFilter->length; k++) {
00377                 for (j=0; j<filterSize; j++)
00378                     filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
00379             }
00380         } else {
00381             for (j=0; j<filterSize; j++)
00382                 filter2[i*filter2Size + j]= filter[i*filterSize + j];
00383         }
00384         //FIXME dstFilter
00385 
00386         (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
00387     }
00388     av_freep(&filter);
00389 
00390     /* try to reduce the filter-size (step1 find size and shift left) */
00391     // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
00392     minFilterSize= 0;
00393     for (i=dstW-1; i>=0; i--) {
00394         int min= filter2Size;
00395         int j;
00396         int64_t cutOff=0.0;
00397 
00398         /* get rid of near zero elements on the left by shifting left */
00399         for (j=0; j<filter2Size; j++) {
00400             int k;
00401             cutOff += FFABS(filter2[i*filter2Size]);
00402 
00403             if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
00404 
00405             /* preserve monotonicity because the core can't handle the filter otherwise */
00406             if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
00407 
00408             // move filter coefficients left
00409             for (k=1; k<filter2Size; k++)
00410                 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
00411             filter2[i*filter2Size + k - 1]= 0;
00412             (*filterPos)[i]++;
00413         }
00414 
00415         cutOff=0;
00416         /* count near zeros on the right */
00417         for (j=filter2Size-1; j>0; j--) {
00418             cutOff += FFABS(filter2[i*filter2Size + j]);
00419 
00420             if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
00421             min--;
00422         }
00423 
00424         if (min>minFilterSize) minFilterSize= min;
00425     }
00426 
00427     if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) {
00428         // we can handle the special case 4,
00429         // so we don't want to go to the full 8
00430         if (minFilterSize < 5)
00431             filterAlign = 4;
00432 
00433         // We really don't want to waste our time
00434         // doing useless computation, so fall back on
00435         // the scalar C code for very small filters.
00436         // Vectorizing is worth it only if you have a
00437         // decent-sized vector.
00438         if (minFilterSize < 3)
00439             filterAlign = 1;
00440     }
00441 
00442     if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
00443         // special case for unscaled vertical filtering
00444         if (minFilterSize == 1 && filterAlign == 2)
00445             filterAlign= 1;
00446     }
00447 
00448     assert(minFilterSize > 0);
00449     filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
00450     assert(filterSize > 0);
00451     filter= av_malloc(filterSize*dstW*sizeof(*filter));
00452     if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
00453         goto fail;
00454     *outFilterSize= filterSize;
00455 
00456     if (flags&SWS_PRINT_INFO)
00457         av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
00458     /* try to reduce the filter-size (step2 reduce it) */
00459     for (i=0; i<dstW; i++) {
00460         int j;
00461 
00462         for (j=0; j<filterSize; j++) {
00463             if (j>=filter2Size) filter[i*filterSize + j]= 0;
00464             else               filter[i*filterSize + j]= filter2[i*filter2Size + j];
00465             if((flags & SWS_BITEXACT) && j>=minFilterSize)
00466                 filter[i*filterSize + j]= 0;
00467         }
00468     }
00469 
00470     //FIXME try to align filterPos if possible
00471 
00472     //fix borders
00473     for (i=0; i<dstW; i++) {
00474         int j;
00475         if ((*filterPos)[i] < 0) {
00476             // move filter coefficients left to compensate for filterPos
00477             for (j=1; j<filterSize; j++) {
00478                 int left= FFMAX(j + (*filterPos)[i], 0);
00479                 filter[i*filterSize + left] += filter[i*filterSize + j];
00480                 filter[i*filterSize + j]=0;
00481             }
00482             (*filterPos)[i]= 0;
00483         }
00484 
00485         if ((*filterPos)[i] + filterSize > srcW) {
00486             int shift= (*filterPos)[i] + filterSize - srcW;
00487             // move filter coefficients right to compensate for filterPos
00488             for (j=filterSize-2; j>=0; j--) {
00489                 int right= FFMIN(j + shift, filterSize-1);
00490                 filter[i*filterSize +right] += filter[i*filterSize +j];
00491                 filter[i*filterSize +j]=0;
00492             }
00493             (*filterPos)[i]= srcW - filterSize;
00494         }
00495     }
00496 
00497     // Note the +1 is for the MMX scaler which reads over the end
00498     /* align at 16 for AltiVec (needed by hScale_altivec_real) */
00499     FF_ALLOCZ_OR_GOTO(NULL, *outFilter, *outFilterSize*(dstW+3)*sizeof(int16_t), fail);
00500 
00501     /* normalize & store in outFilter */
00502     for (i=0; i<dstW; i++) {
00503         int j;
00504         int64_t error=0;
00505         int64_t sum=0;
00506 
00507         for (j=0; j<filterSize; j++) {
00508             sum+= filter[i*filterSize + j];
00509         }
00510         sum= (sum + one/2)/ one;
00511         for (j=0; j<*outFilterSize; j++) {
00512             int64_t v= filter[i*filterSize + j] + error;
00513             int intV= ROUNDED_DIV(v, sum);
00514             (*outFilter)[i*(*outFilterSize) + j]= intV;
00515             error= v - intV*sum;
00516         }
00517     }
00518 
00519     (*filterPos)[dstW+0] =
00520     (*filterPos)[dstW+1] =
00521     (*filterPos)[dstW+2] = (*filterPos)[dstW-1]; // the MMX/SSE scaler will read over the end
00522     for (i=0; i<*outFilterSize; i++) {
00523         int k= (dstW - 1) * (*outFilterSize) + i;
00524         (*outFilter)[k + 1 * (*outFilterSize)] =
00525         (*outFilter)[k + 2 * (*outFilterSize)] =
00526         (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
00527     }
00528 
00529     ret=0;
00530 fail:
00531     av_free(filter);
00532     av_free(filter2);
00533     return ret;
00534 }
00535 
00536 #if HAVE_MMX2
00537 static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode, int16_t *filter, int32_t *filterPos, int numSplits)
00538 {
00539     uint8_t *fragmentA;
00540     x86_reg imm8OfPShufW1A;
00541     x86_reg imm8OfPShufW2A;
00542     x86_reg fragmentLengthA;
00543     uint8_t *fragmentB;
00544     x86_reg imm8OfPShufW1B;
00545     x86_reg imm8OfPShufW2B;
00546     x86_reg fragmentLengthB;
00547     int fragmentPos;
00548 
00549     int xpos, i;
00550 
00551     // create an optimized horizontal scaling routine
00552     /* This scaler is made of runtime-generated MMX2 code using specially
00553      * tuned pshufw instructions. For every four output pixels, if four
00554      * input pixels are enough for the fast bilinear scaling, then a chunk
00555      * of fragmentB is used. If five input pixels are needed, then a chunk
00556      * of fragmentA is used.
00557      */
00558 
00559     //code fragment
00560 
00561     __asm__ volatile(
00562         "jmp                         9f                 \n\t"
00563     // Begin
00564         "0:                                             \n\t"
00565         "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
00566         "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
00567         "movd   1(%%"REG_c", %%"REG_S"), %%mm1          \n\t"
00568         "punpcklbw                %%mm7, %%mm1          \n\t"
00569         "punpcklbw                %%mm7, %%mm0          \n\t"
00570         "pshufw                   $0xFF, %%mm1, %%mm1   \n\t"
00571         "1:                                             \n\t"
00572         "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
00573         "2:                                             \n\t"
00574         "psubw                    %%mm1, %%mm0          \n\t"
00575         "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
00576         "pmullw                   %%mm3, %%mm0          \n\t"
00577         "psllw                       $7, %%mm1          \n\t"
00578         "paddw                    %%mm1, %%mm0          \n\t"
00579 
00580         "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
00581 
00582         "add                         $8, %%"REG_a"      \n\t"
00583     // End
00584         "9:                                             \n\t"
00585 //        "int $3                                         \n\t"
00586         "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
00587         "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
00588         "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
00589         "dec                         %1                 \n\t"
00590         "dec                         %2                 \n\t"
00591         "sub                         %0, %1             \n\t"
00592         "sub                         %0, %2             \n\t"
00593         "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
00594         "sub                         %0, %3             \n\t"
00595 
00596 
00597         :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
00598         "=r" (fragmentLengthA)
00599     );
00600 
00601     __asm__ volatile(
00602         "jmp                         9f                 \n\t"
00603     // Begin
00604         "0:                                             \n\t"
00605         "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
00606         "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
00607         "punpcklbw                %%mm7, %%mm0          \n\t"
00608         "pshufw                   $0xFF, %%mm0, %%mm1   \n\t"
00609         "1:                                             \n\t"
00610         "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
00611         "2:                                             \n\t"
00612         "psubw                    %%mm1, %%mm0          \n\t"
00613         "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
00614         "pmullw                   %%mm3, %%mm0          \n\t"
00615         "psllw                       $7, %%mm1          \n\t"
00616         "paddw                    %%mm1, %%mm0          \n\t"
00617 
00618         "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
00619 
00620         "add                         $8, %%"REG_a"      \n\t"
00621     // End
00622         "9:                                             \n\t"
00623 //        "int                       $3                   \n\t"
00624         "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
00625         "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
00626         "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
00627         "dec                         %1                 \n\t"
00628         "dec                         %2                 \n\t"
00629         "sub                         %0, %1             \n\t"
00630         "sub                         %0, %2             \n\t"
00631         "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
00632         "sub                         %0, %3             \n\t"
00633 
00634 
00635         :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
00636         "=r" (fragmentLengthB)
00637     );
00638 
00639     xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
00640     fragmentPos=0;
00641 
00642     for (i=0; i<dstW/numSplits; i++) {
00643         int xx=xpos>>16;
00644 
00645         if ((i&3) == 0) {
00646             int a=0;
00647             int b=((xpos+xInc)>>16) - xx;
00648             int c=((xpos+xInc*2)>>16) - xx;
00649             int d=((xpos+xInc*3)>>16) - xx;
00650             int inc                = (d+1<4);
00651             uint8_t *fragment      = (d+1<4) ? fragmentB       : fragmentA;
00652             x86_reg imm8OfPShufW1  = (d+1<4) ? imm8OfPShufW1B  : imm8OfPShufW1A;
00653             x86_reg imm8OfPShufW2  = (d+1<4) ? imm8OfPShufW2B  : imm8OfPShufW2A;
00654             x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
00655             int maxShift= 3-(d+inc);
00656             int shift=0;
00657 
00658             if (filterCode) {
00659                 filter[i  ] = (( xpos         & 0xFFFF) ^ 0xFFFF)>>9;
00660                 filter[i+1] = (((xpos+xInc  ) & 0xFFFF) ^ 0xFFFF)>>9;
00661                 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
00662                 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
00663                 filterPos[i/2]= xx;
00664 
00665                 memcpy(filterCode + fragmentPos, fragment, fragmentLength);
00666 
00667                 filterCode[fragmentPos + imm8OfPShufW1]=
00668                     (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
00669                 filterCode[fragmentPos + imm8OfPShufW2]=
00670                     a | (b<<2) | (c<<4) | (d<<6);
00671 
00672                 if (i+4-inc>=dstW) shift=maxShift; //avoid overread
00673                 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
00674 
00675                 if (shift && i>=shift) {
00676                     filterCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
00677                     filterCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
00678                     filterPos[i/2]-=shift;
00679                 }
00680             }
00681 
00682             fragmentPos+= fragmentLength;
00683 
00684             if (filterCode)
00685                 filterCode[fragmentPos]= RET;
00686         }
00687         xpos+=xInc;
00688     }
00689     if (filterCode)
00690         filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
00691 
00692     return fragmentPos + 1;
00693 }
00694 #endif /* HAVE_MMX2 */
00695 
00696 static void getSubSampleFactors(int *h, int *v, enum PixelFormat format)
00697 {
00698     *h = av_pix_fmt_descriptors[format].log2_chroma_w;
00699     *v = av_pix_fmt_descriptors[format].log2_chroma_h;
00700 }
00701 
00702 int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
00703                              int srcRange, const int table[4], int dstRange,
00704                              int brightness, int contrast, int saturation)
00705 {
00706     memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
00707     memcpy(c->dstColorspaceTable,     table, sizeof(int)*4);
00708 
00709     c->brightness= brightness;
00710     c->contrast  = contrast;
00711     c->saturation= saturation;
00712     c->srcRange  = srcRange;
00713     c->dstRange  = dstRange;
00714     if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
00715 
00716     c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->dstFormat]);
00717     c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->srcFormat]);
00718 
00719     ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
00720     //FIXME factorize
00721 
00722     if (HAVE_ALTIVEC && av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC)
00723         ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
00724     return 0;
00725 }
00726 
00727 int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
00728                              int *srcRange, int **table, int *dstRange,
00729                              int *brightness, int *contrast, int *saturation)
00730 {
00731     if (!c || isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
00732 
00733     *inv_table = c->srcColorspaceTable;
00734     *table     = c->dstColorspaceTable;
00735     *srcRange  = c->srcRange;
00736     *dstRange  = c->dstRange;
00737     *brightness= c->brightness;
00738     *contrast  = c->contrast;
00739     *saturation= c->saturation;
00740 
00741     return 0;
00742 }
00743 
00744 static int handle_jpeg(enum PixelFormat *format)
00745 {
00746     switch (*format) {
00747     case PIX_FMT_YUVJ420P: *format = PIX_FMT_YUV420P; return 1;
00748     case PIX_FMT_YUVJ422P: *format = PIX_FMT_YUV422P; return 1;
00749     case PIX_FMT_YUVJ444P: *format = PIX_FMT_YUV444P; return 1;
00750     case PIX_FMT_YUVJ440P: *format = PIX_FMT_YUV440P; return 1;
00751     default:                                          return 0;
00752     }
00753 }
00754 
00755 static int handle_0alpha(enum PixelFormat *format)
00756 {
00757     switch (*format) {
00758     case PIX_FMT_0BGR    : *format = PIX_FMT_ABGR   ; return 1;
00759     case PIX_FMT_BGR0    : *format = PIX_FMT_BGRA   ; return 4;
00760     case PIX_FMT_0RGB    : *format = PIX_FMT_ARGB   ; return 1;
00761     case PIX_FMT_RGB0    : *format = PIX_FMT_RGBA   ; return 4;
00762     default:                                          return 0;
00763     }
00764 }
00765 
00766 SwsContext *sws_alloc_context(void)
00767 {
00768     SwsContext *c= av_mallocz(sizeof(SwsContext));
00769 
00770     c->av_class = &sws_context_class;
00771     av_opt_set_defaults(c);
00772 
00773     return c;
00774 }
00775 
00776 int sws_init_context(SwsContext *c, SwsFilter *srcFilter, SwsFilter *dstFilter)
00777 {
00778     int i, j;
00779     int usesVFilter, usesHFilter;
00780     int unscaled;
00781     SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
00782     int srcW= c->srcW;
00783     int srcH= c->srcH;
00784     int dstW= c->dstW;
00785     int dstH= c->dstH;
00786     int dst_stride = FFALIGN(dstW * sizeof(int16_t)+66, 16);
00787     int flags, cpu_flags;
00788     enum PixelFormat srcFormat= c->srcFormat;
00789     enum PixelFormat dstFormat= c->dstFormat;
00790 
00791     cpu_flags = av_get_cpu_flags();
00792     flags     = c->flags;
00793     emms_c();
00794     if (!rgb15to16) sws_rgb2rgb_init();
00795 
00796     unscaled = (srcW == dstW && srcH == dstH);
00797 
00798     handle_jpeg(&srcFormat);
00799     handle_jpeg(&dstFormat);
00800     handle_0alpha(&srcFormat);
00801     handle_0alpha(&dstFormat);
00802 
00803     if(srcFormat!=c->srcFormat || dstFormat!=c->dstFormat){
00804         av_log(c, AV_LOG_WARNING, "deprecated pixel format used, make sure you did set range correctly\n");
00805         c->srcFormat= srcFormat;
00806         c->dstFormat= dstFormat;
00807     }
00808 
00809     if (!sws_isSupportedInput(srcFormat)) {
00810         av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n", av_get_pix_fmt_name(srcFormat));
00811         return AVERROR(EINVAL);
00812     }
00813     if (!sws_isSupportedOutput(dstFormat)) {
00814         av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n", av_get_pix_fmt_name(dstFormat));
00815         return AVERROR(EINVAL);
00816     }
00817 
00818     i= flags & ( SWS_POINT
00819                 |SWS_AREA
00820                 |SWS_BILINEAR
00821                 |SWS_FAST_BILINEAR
00822                 |SWS_BICUBIC
00823                 |SWS_X
00824                 |SWS_GAUSS
00825                 |SWS_LANCZOS
00826                 |SWS_SINC
00827                 |SWS_SPLINE
00828                 |SWS_BICUBLIN);
00829     if(!i || (i & (i-1))) {
00830         av_log(c, AV_LOG_ERROR, "Exactly one scaler algorithm must be chosen, got %X\n", i);
00831         return AVERROR(EINVAL);
00832     }
00833     /* sanity check */
00834     if (srcW<4 || srcH<1 || dstW<8 || dstH<1) { //FIXME check if these are enough and try to lowwer them after fixing the relevant parts of the code
00835         av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
00836                srcW, srcH, dstW, dstH);
00837         return AVERROR(EINVAL);
00838     }
00839 
00840     if (!dstFilter) dstFilter= &dummyFilter;
00841     if (!srcFilter) srcFilter= &dummyFilter;
00842 
00843     c->lumXInc= (((int64_t)srcW<<16) + (dstW>>1))/dstW;
00844     c->lumYInc= (((int64_t)srcH<<16) + (dstH>>1))/dstH;
00845     c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[dstFormat]);
00846     c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[srcFormat]);
00847     c->vRounder= 4* 0x0001000100010001ULL;
00848 
00849     usesVFilter = (srcFilter->lumV && srcFilter->lumV->length>1) ||
00850                   (srcFilter->chrV && srcFilter->chrV->length>1) ||
00851                   (dstFilter->lumV && dstFilter->lumV->length>1) ||
00852                   (dstFilter->chrV && dstFilter->chrV->length>1);
00853     usesHFilter = (srcFilter->lumH && srcFilter->lumH->length>1) ||
00854                   (srcFilter->chrH && srcFilter->chrH->length>1) ||
00855                   (dstFilter->lumH && dstFilter->lumH->length>1) ||
00856                   (dstFilter->chrH && dstFilter->chrH->length>1);
00857 
00858     getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
00859     getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
00860 
00861     // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
00862     if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) {
00863         if (dstW&1) {
00864             av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to odd output size\n");
00865             flags |= SWS_FULL_CHR_H_INT;
00866             c->flags = flags;
00867         } else
00868             c->chrDstHSubSample = 1;
00869     }
00870 
00871     // drop some chroma lines if the user wants it
00872     c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
00873     c->chrSrcVSubSample+= c->vChrDrop;
00874 
00875     // drop every other pixel for chroma calculation unless user wants full chroma
00876     if (isAnyRGB(srcFormat) && !(flags&SWS_FULL_CHR_H_INP)
00877       && srcFormat!=PIX_FMT_RGB8      && srcFormat!=PIX_FMT_BGR8
00878       && srcFormat!=PIX_FMT_RGB4      && srcFormat!=PIX_FMT_BGR4
00879       && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
00880       && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&SWS_FAST_BILINEAR)))
00881         c->chrSrcHSubSample=1;
00882 
00883     // Note the -((-x)>>y) is so that we always round toward +inf.
00884     c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
00885     c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
00886     c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
00887     c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
00888 
00889     /* unscaled special cases */
00890     if (unscaled && !usesHFilter && !usesVFilter && (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
00891         ff_get_unscaled_swscale(c);
00892 
00893         if (c->swScale) {
00894             if (flags&SWS_PRINT_INFO)
00895                 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
00896                        av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
00897             return 0;
00898         }
00899     }
00900 
00901     c->srcBpc = 1 + av_pix_fmt_descriptors[srcFormat].comp[0].depth_minus1;
00902     if (c->srcBpc < 8)
00903         c->srcBpc = 8;
00904     c->dstBpc = 1 + av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1;
00905     if (c->dstBpc < 8)
00906         c->dstBpc = 8;
00907     if (isAnyRGB(srcFormat) || srcFormat == PIX_FMT_PAL8)
00908         c->srcBpc = 16;
00909     if (c->dstBpc == 16)
00910         dst_stride <<= 1;
00911     FF_ALLOC_OR_GOTO(c, c->formatConvBuffer, FFALIGN(srcW*2+78, 16) * 2, fail);
00912     if (HAVE_MMX2 && cpu_flags & AV_CPU_FLAG_MMX2 && c->srcBpc == 8 && c->dstBpc <= 10) {
00913         c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
00914         if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR)) {
00915             if (flags&SWS_PRINT_INFO)
00916                 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
00917         }
00918         if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat) || isAnyRGB(c->srcFormat)) c->canMMX2BeUsed=0;
00919     }
00920     else
00921         c->canMMX2BeUsed=0;
00922 
00923     c->chrXInc= (((int64_t)c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
00924     c->chrYInc= (((int64_t)c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
00925 
00926     // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
00927     // but only for the FAST_BILINEAR mode otherwise do correct scaling
00928     // n-2 is the last chrominance sample available
00929     // this is not perfect, but no one should notice the difference, the more correct variant
00930     // would be like the vertical one, but that would require some special code for the
00931     // first and last pixel
00932     if (flags&SWS_FAST_BILINEAR) {
00933         if (c->canMMX2BeUsed) {
00934             c->lumXInc+= 20;
00935             c->chrXInc+= 20;
00936         }
00937         //we don't use the x86 asm scaler if MMX is available
00938         else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX && c->dstBpc <= 10) {
00939             c->lumXInc = ((int64_t)(srcW-2)<<16)/(dstW-2) - 20;
00940             c->chrXInc = ((int64_t)(c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
00941         }
00942     }
00943 
00944     /* precalculate horizontal scaler filter coefficients */
00945     {
00946 #if HAVE_MMX2
00947 // can't downscale !!!
00948         if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
00949             c->lumMmx2FilterCodeSize = initMMX2HScaler(      dstW, c->lumXInc, NULL, NULL, NULL, 8);
00950             c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
00951 
00952 #ifdef MAP_ANONYMOUS
00953             c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
00954             c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
00955 #elif HAVE_VIRTUALALLOC
00956             c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
00957             c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
00958 #else
00959             c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
00960             c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
00961 #endif
00962 
00963 #ifdef MAP_ANONYMOUS
00964             if (c->lumMmx2FilterCode == MAP_FAILED || c->chrMmx2FilterCode == MAP_FAILED)
00965 #else
00966             if (!c->lumMmx2FilterCode || !c->chrMmx2FilterCode)
00967 #endif
00968             {
00969                 av_log(c, AV_LOG_ERROR, "Failed to allocate MMX2FilterCode\n");
00970                 return AVERROR(ENOMEM);
00971             }
00972             FF_ALLOCZ_OR_GOTO(c, c->hLumFilter   , (dstW        /8+8)*sizeof(int16_t), fail);
00973             FF_ALLOCZ_OR_GOTO(c, c->hChrFilter   , (c->chrDstW  /4+8)*sizeof(int16_t), fail);
00974             FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW      /2/8+8)*sizeof(int32_t), fail);
00975             FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t), fail);
00976 
00977             initMMX2HScaler(      dstW, c->lumXInc, c->lumMmx2FilterCode, c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
00978             initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4);
00979 
00980 #ifdef MAP_ANONYMOUS
00981             mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
00982             mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
00983 #endif
00984         } else
00985 #endif /* HAVE_MMX2 */
00986         {
00987             const int filterAlign=
00988                 (HAVE_MMX     && cpu_flags & AV_CPU_FLAG_MMX) ? 4 :
00989                 (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
00990                 1;
00991 
00992             if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
00993                            srcW      ,       dstW, filterAlign, 1<<14,
00994                            (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags, cpu_flags,
00995                            srcFilter->lumH, dstFilter->lumH, c->param) < 0)
00996                 goto fail;
00997             if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
00998                            c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
00999                            (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags, cpu_flags,
01000                            srcFilter->chrH, dstFilter->chrH, c->param) < 0)
01001                 goto fail;
01002         }
01003     } // initialize horizontal stuff
01004 
01005     /* precalculate vertical scaler filter coefficients */
01006     {
01007         const int filterAlign=
01008             (HAVE_MMX     && cpu_flags & AV_CPU_FLAG_MMX) ? 2 :
01009             (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
01010             1;
01011 
01012         if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
01013                        srcH      ,        dstH, filterAlign, (1<<12),
01014                        (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags, cpu_flags,
01015                        srcFilter->lumV, dstFilter->lumV, c->param) < 0)
01016             goto fail;
01017         if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
01018                        c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
01019                        (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags, cpu_flags,
01020                        srcFilter->chrV, dstFilter->chrV, c->param) < 0)
01021             goto fail;
01022 
01023 #if HAVE_ALTIVEC
01024         FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH, fail);
01025         FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH, fail);
01026 
01027         for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
01028             int j;
01029             short *p = (short *)&c->vYCoeffsBank[i];
01030             for (j=0;j<8;j++)
01031                 p[j] = c->vLumFilter[i];
01032         }
01033 
01034         for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
01035             int j;
01036             short *p = (short *)&c->vCCoeffsBank[i];
01037             for (j=0;j<8;j++)
01038                 p[j] = c->vChrFilter[i];
01039         }
01040 #endif
01041     }
01042 
01043     // calculate buffer sizes so that they won't run out while handling these damn slices
01044     c->vLumBufSize= c->vLumFilterSize;
01045     c->vChrBufSize= c->vChrFilterSize;
01046     for (i=0; i<dstH; i++) {
01047         int chrI = (int64_t) i * c->chrDstH / dstH;
01048         int nextSlice= FFMAX(c->vLumFilterPos[i   ] + c->vLumFilterSize - 1,
01049                            ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
01050 
01051         nextSlice>>= c->chrSrcVSubSample;
01052         nextSlice<<= c->chrSrcVSubSample;
01053         if (c->vLumFilterPos[i   ] + c->vLumBufSize < nextSlice)
01054             c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
01055         if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
01056             c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
01057     }
01058 
01059     // allocate pixbufs (we use dynamic allocation because otherwise we would need to
01060     // allocate several megabytes to handle all possible cases)
01061     FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
01062     FF_ALLOC_OR_GOTO(c, c->chrUPixBuf, c->vChrBufSize*2*sizeof(int16_t*), fail);
01063     FF_ALLOC_OR_GOTO(c, c->chrVPixBuf, c->vChrBufSize*2*sizeof(int16_t*), fail);
01064     if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
01065         FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
01066     //Note we need at least one pixel more at the end because of the MMX code (just in case someone wanna replace the 4000/8000)
01067     /* align at 16 bytes for AltiVec */
01068     for (i=0; i<c->vLumBufSize; i++) {
01069         FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i+c->vLumBufSize], dst_stride+16, fail);
01070         c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
01071     }
01072     // 64 / c->scalingBpp is the same as 16 / sizeof(scaling_intermediate)
01073     c->uv_off   = (dst_stride>>1) + 64 / (c->dstBpc &~ 7);
01074     c->uv_offx2 = dst_stride + 16;
01075     for (i=0; i<c->vChrBufSize; i++) {
01076         FF_ALLOC_OR_GOTO(c, c->chrUPixBuf[i+c->vChrBufSize], dst_stride*2+32, fail);
01077         c->chrUPixBuf[i] = c->chrUPixBuf[i+c->vChrBufSize];
01078         c->chrVPixBuf[i] = c->chrVPixBuf[i+c->vChrBufSize] = c->chrUPixBuf[i] + (dst_stride >> 1) + 8;
01079     }
01080     if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
01081         for (i=0; i<c->vLumBufSize; i++) {
01082             FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i+c->vLumBufSize], dst_stride+16, fail);
01083             c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
01084         }
01085 
01086     //try to avoid drawing green stuff between the right end and the stride end
01087     for (i=0; i<c->vChrBufSize; i++)
01088         if(av_pix_fmt_descriptors[c->dstFormat].comp[0].depth_minus1 == 15){
01089             av_assert0(c->dstBpc > 10);
01090             for(j=0; j<dst_stride/2+1; j++)
01091                 ((int32_t*)(c->chrUPixBuf[i]))[j] = 1<<18;
01092         } else
01093             for(j=0; j<dst_stride+1; j++)
01094                 ((int16_t*)(c->chrUPixBuf[i]))[j] = 1<<14;
01095 
01096     assert(c->chrDstH <= dstH);
01097 
01098     if (flags&SWS_PRINT_INFO) {
01099         if      (flags&SWS_FAST_BILINEAR) av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
01100         else if (flags&SWS_BILINEAR)      av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
01101         else if (flags&SWS_BICUBIC)       av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
01102         else if (flags&SWS_X)             av_log(c, AV_LOG_INFO, "Experimental scaler, ");
01103         else if (flags&SWS_POINT)         av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
01104         else if (flags&SWS_AREA)          av_log(c, AV_LOG_INFO, "Area Averaging scaler, ");
01105         else if (flags&SWS_BICUBLIN)      av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
01106         else if (flags&SWS_GAUSS)         av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
01107         else if (flags&SWS_SINC)          av_log(c, AV_LOG_INFO, "Sinc scaler, ");
01108         else if (flags&SWS_LANCZOS)       av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
01109         else if (flags&SWS_SPLINE)        av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
01110         else                              av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
01111 
01112         av_log(c, AV_LOG_INFO, "from %s to %s%s ",
01113                av_get_pix_fmt_name(srcFormat),
01114 #ifdef DITHER1XBPP
01115                dstFormat == PIX_FMT_BGR555 || dstFormat == PIX_FMT_BGR565 ||
01116                dstFormat == PIX_FMT_RGB444BE || dstFormat == PIX_FMT_RGB444LE ||
01117                dstFormat == PIX_FMT_BGR444BE || dstFormat == PIX_FMT_BGR444LE ? "dithered " : "",
01118 #else
01119                "",
01120 #endif
01121                av_get_pix_fmt_name(dstFormat));
01122 
01123         if      (HAVE_MMX2     && cpu_flags & AV_CPU_FLAG_MMX2)    av_log(c, AV_LOG_INFO, "using MMX2\n");
01124         else if (HAVE_AMD3DNOW && cpu_flags & AV_CPU_FLAG_3DNOW)   av_log(c, AV_LOG_INFO, "using 3DNOW\n");
01125         else if (HAVE_MMX      && cpu_flags & AV_CPU_FLAG_MMX)     av_log(c, AV_LOG_INFO, "using MMX\n");
01126         else if (HAVE_ALTIVEC  && cpu_flags & AV_CPU_FLAG_ALTIVEC) av_log(c, AV_LOG_INFO, "using AltiVec\n");
01127         else                                   av_log(c, AV_LOG_INFO, "using C\n");
01128 
01129         av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
01130         av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
01131                c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
01132         av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
01133                c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
01134     }
01135 
01136     c->swScale= ff_getSwsFunc(c);
01137     return 0;
01138 fail: //FIXME replace things by appropriate error codes
01139     return -1;
01140 }
01141 
01142 #if FF_API_SWS_GETCONTEXT
01143 SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat,
01144                            int dstW, int dstH, enum PixelFormat dstFormat, int flags,
01145                            SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
01146 {
01147     SwsContext *c;
01148 
01149     if(!(c=sws_alloc_context()))
01150         return NULL;
01151 
01152     c->flags= flags;
01153     c->srcW= srcW;
01154     c->srcH= srcH;
01155     c->dstW= dstW;
01156     c->dstH= dstH;
01157     c->srcRange = handle_jpeg(&srcFormat);
01158     c->dstRange = handle_jpeg(&dstFormat);
01159     c->src0Alpha = handle_0alpha(&srcFormat);
01160     c->dst0Alpha = handle_0alpha(&dstFormat);
01161     c->srcFormat= srcFormat;
01162     c->dstFormat= dstFormat;
01163 
01164     if (param) {
01165         c->param[0] = param[0];
01166         c->param[1] = param[1];
01167     }
01168     sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, c->dstRange, 0, 1<<16, 1<<16);
01169 
01170     if(sws_init_context(c, srcFilter, dstFilter) < 0){
01171         sws_freeContext(c);
01172         return NULL;
01173     }
01174 
01175     return c;
01176 }
01177 #endif
01178 
01179 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
01180                                 float lumaSharpen, float chromaSharpen,
01181                                 float chromaHShift, float chromaVShift,
01182                                 int verbose)
01183 {
01184     SwsFilter *filter= av_malloc(sizeof(SwsFilter));
01185     if (!filter)
01186         return NULL;
01187 
01188     if (lumaGBlur!=0.0) {
01189         filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
01190         filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
01191     } else {
01192         filter->lumH= sws_getIdentityVec();
01193         filter->lumV= sws_getIdentityVec();
01194     }
01195 
01196     if (chromaGBlur!=0.0) {
01197         filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
01198         filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
01199     } else {
01200         filter->chrH= sws_getIdentityVec();
01201         filter->chrV= sws_getIdentityVec();
01202     }
01203 
01204     if (chromaSharpen!=0.0) {
01205         SwsVector *id= sws_getIdentityVec();
01206         sws_scaleVec(filter->chrH, -chromaSharpen);
01207         sws_scaleVec(filter->chrV, -chromaSharpen);
01208         sws_addVec(filter->chrH, id);
01209         sws_addVec(filter->chrV, id);
01210         sws_freeVec(id);
01211     }
01212 
01213     if (lumaSharpen!=0.0) {
01214         SwsVector *id= sws_getIdentityVec();
01215         sws_scaleVec(filter->lumH, -lumaSharpen);
01216         sws_scaleVec(filter->lumV, -lumaSharpen);
01217         sws_addVec(filter->lumH, id);
01218         sws_addVec(filter->lumV, id);
01219         sws_freeVec(id);
01220     }
01221 
01222     if (chromaHShift != 0.0)
01223         sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
01224 
01225     if (chromaVShift != 0.0)
01226         sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
01227 
01228     sws_normalizeVec(filter->chrH, 1.0);
01229     sws_normalizeVec(filter->chrV, 1.0);
01230     sws_normalizeVec(filter->lumH, 1.0);
01231     sws_normalizeVec(filter->lumV, 1.0);
01232 
01233     if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
01234     if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
01235 
01236     return filter;
01237 }
01238 
01239 SwsVector *sws_allocVec(int length)
01240 {
01241     SwsVector *vec = av_malloc(sizeof(SwsVector));
01242     if (!vec)
01243         return NULL;
01244     vec->length = length;
01245     vec->coeff  = av_malloc(sizeof(double) * length);
01246     if (!vec->coeff)
01247         av_freep(&vec);
01248     return vec;
01249 }
01250 
01251 SwsVector *sws_getGaussianVec(double variance, double quality)
01252 {
01253     const int length= (int)(variance*quality + 0.5) | 1;
01254     int i;
01255     double middle= (length-1)*0.5;
01256     SwsVector *vec= sws_allocVec(length);
01257 
01258     if (!vec)
01259         return NULL;
01260 
01261     for (i=0; i<length; i++) {
01262         double dist= i-middle;
01263         vec->coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*M_PI);
01264     }
01265 
01266     sws_normalizeVec(vec, 1.0);
01267 
01268     return vec;
01269 }
01270 
01271 SwsVector *sws_getConstVec(double c, int length)
01272 {
01273     int i;
01274     SwsVector *vec= sws_allocVec(length);
01275 
01276     if (!vec)
01277         return NULL;
01278 
01279     for (i=0; i<length; i++)
01280         vec->coeff[i]= c;
01281 
01282     return vec;
01283 }
01284 
01285 SwsVector *sws_getIdentityVec(void)
01286 {
01287     return sws_getConstVec(1.0, 1);
01288 }
01289 
01290 static double sws_dcVec(SwsVector *a)
01291 {
01292     int i;
01293     double sum=0;
01294 
01295     for (i=0; i<a->length; i++)
01296         sum+= a->coeff[i];
01297 
01298     return sum;
01299 }
01300 
01301 void sws_scaleVec(SwsVector *a, double scalar)
01302 {
01303     int i;
01304 
01305     for (i=0; i<a->length; i++)
01306         a->coeff[i]*= scalar;
01307 }
01308 
01309 void sws_normalizeVec(SwsVector *a, double height)
01310 {
01311     sws_scaleVec(a, height/sws_dcVec(a));
01312 }
01313 
01314 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
01315 {
01316     int length= a->length + b->length - 1;
01317     int i, j;
01318     SwsVector *vec= sws_getConstVec(0.0, length);
01319 
01320     if (!vec)
01321         return NULL;
01322 
01323     for (i=0; i<a->length; i++) {
01324         for (j=0; j<b->length; j++) {
01325             vec->coeff[i+j]+= a->coeff[i]*b->coeff[j];
01326         }
01327     }
01328 
01329     return vec;
01330 }
01331 
01332 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
01333 {
01334     int length= FFMAX(a->length, b->length);
01335     int i;
01336     SwsVector *vec= sws_getConstVec(0.0, length);
01337 
01338     if (!vec)
01339         return NULL;
01340 
01341     for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
01342     for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
01343 
01344     return vec;
01345 }
01346 
01347 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
01348 {
01349     int length= FFMAX(a->length, b->length);
01350     int i;
01351     SwsVector *vec= sws_getConstVec(0.0, length);
01352 
01353     if (!vec)
01354         return NULL;
01355 
01356     for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
01357     for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
01358 
01359     return vec;
01360 }
01361 
01362 /* shift left / or right if "shift" is negative */
01363 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
01364 {
01365     int length= a->length + FFABS(shift)*2;
01366     int i;
01367     SwsVector *vec= sws_getConstVec(0.0, length);
01368 
01369     if (!vec)
01370         return NULL;
01371 
01372     for (i=0; i<a->length; i++) {
01373         vec->coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
01374     }
01375 
01376     return vec;
01377 }
01378 
01379 void sws_shiftVec(SwsVector *a, int shift)
01380 {
01381     SwsVector *shifted= sws_getShiftedVec(a, shift);
01382     av_free(a->coeff);
01383     a->coeff= shifted->coeff;
01384     a->length= shifted->length;
01385     av_free(shifted);
01386 }
01387 
01388 void sws_addVec(SwsVector *a, SwsVector *b)
01389 {
01390     SwsVector *sum= sws_sumVec(a, b);
01391     av_free(a->coeff);
01392     a->coeff= sum->coeff;
01393     a->length= sum->length;
01394     av_free(sum);
01395 }
01396 
01397 void sws_subVec(SwsVector *a, SwsVector *b)
01398 {
01399     SwsVector *diff= sws_diffVec(a, b);
01400     av_free(a->coeff);
01401     a->coeff= diff->coeff;
01402     a->length= diff->length;
01403     av_free(diff);
01404 }
01405 
01406 void sws_convVec(SwsVector *a, SwsVector *b)
01407 {
01408     SwsVector *conv= sws_getConvVec(a, b);
01409     av_free(a->coeff);
01410     a->coeff= conv->coeff;
01411     a->length= conv->length;
01412     av_free(conv);
01413 }
01414 
01415 SwsVector *sws_cloneVec(SwsVector *a)
01416 {
01417     int i;
01418     SwsVector *vec= sws_allocVec(a->length);
01419 
01420     if (!vec)
01421         return NULL;
01422 
01423     for (i=0; i<a->length; i++) vec->coeff[i]= a->coeff[i];
01424 
01425     return vec;
01426 }
01427 
01428 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
01429 {
01430     int i;
01431     double max=0;
01432     double min=0;
01433     double range;
01434 
01435     for (i=0; i<a->length; i++)
01436         if (a->coeff[i]>max) max= a->coeff[i];
01437 
01438     for (i=0; i<a->length; i++)
01439         if (a->coeff[i]<min) min= a->coeff[i];
01440 
01441     range= max - min;
01442 
01443     for (i=0; i<a->length; i++) {
01444         int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
01445         av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
01446         for (;x>0; x--) av_log(log_ctx, log_level, " ");
01447         av_log(log_ctx, log_level, "|\n");
01448     }
01449 }
01450 
01451 void sws_freeVec(SwsVector *a)
01452 {
01453     if (!a) return;
01454     av_freep(&a->coeff);
01455     a->length=0;
01456     av_free(a);
01457 }
01458 
01459 void sws_freeFilter(SwsFilter *filter)
01460 {
01461     if (!filter) return;
01462 
01463     if (filter->lumH) sws_freeVec(filter->lumH);
01464     if (filter->lumV) sws_freeVec(filter->lumV);
01465     if (filter->chrH) sws_freeVec(filter->chrH);
01466     if (filter->chrV) sws_freeVec(filter->chrV);
01467     av_free(filter);
01468 }
01469 
01470 void sws_freeContext(SwsContext *c)
01471 {
01472     int i;
01473     if (!c) return;
01474 
01475     if (c->lumPixBuf) {
01476         for (i=0; i<c->vLumBufSize; i++)
01477             av_freep(&c->lumPixBuf[i]);
01478         av_freep(&c->lumPixBuf);
01479     }
01480 
01481     if (c->chrUPixBuf) {
01482         for (i=0; i<c->vChrBufSize; i++)
01483             av_freep(&c->chrUPixBuf[i]);
01484         av_freep(&c->chrUPixBuf);
01485         av_freep(&c->chrVPixBuf);
01486     }
01487 
01488     if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
01489         for (i=0; i<c->vLumBufSize; i++)
01490             av_freep(&c->alpPixBuf[i]);
01491         av_freep(&c->alpPixBuf);
01492     }
01493 
01494     av_freep(&c->vLumFilter);
01495     av_freep(&c->vChrFilter);
01496     av_freep(&c->hLumFilter);
01497     av_freep(&c->hChrFilter);
01498 #if HAVE_ALTIVEC
01499     av_freep(&c->vYCoeffsBank);
01500     av_freep(&c->vCCoeffsBank);
01501 #endif
01502 
01503     av_freep(&c->vLumFilterPos);
01504     av_freep(&c->vChrFilterPos);
01505     av_freep(&c->hLumFilterPos);
01506     av_freep(&c->hChrFilterPos);
01507 
01508 #if HAVE_MMX
01509 #ifdef MAP_ANONYMOUS
01510     if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
01511     if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
01512 #elif HAVE_VIRTUALALLOC
01513     if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, 0, MEM_RELEASE);
01514     if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, 0, MEM_RELEASE);
01515 #else
01516     av_free(c->lumMmx2FilterCode);
01517     av_free(c->chrMmx2FilterCode);
01518 #endif
01519     c->lumMmx2FilterCode=NULL;
01520     c->chrMmx2FilterCode=NULL;
01521 #endif /* HAVE_MMX */
01522 
01523     av_freep(&c->yuvTable);
01524     av_freep(&c->formatConvBuffer);
01525 
01526     av_free(c);
01527 }
01528 
01529 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
01530                                         int srcW, int srcH, enum PixelFormat srcFormat,
01531                                         int dstW, int dstH, enum PixelFormat dstFormat, int flags,
01532                                         SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
01533 {
01534     static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
01535 
01536     if (!param)
01537         param = default_param;
01538 
01539     if (context &&
01540         (context->srcW      != srcW      ||
01541          context->srcH      != srcH      ||
01542          context->srcFormat != srcFormat ||
01543          context->dstW      != dstW      ||
01544          context->dstH      != dstH      ||
01545          context->dstFormat != dstFormat ||
01546          context->flags     != flags     ||
01547          context->param[0]  != param[0]  ||
01548          context->param[1]  != param[1])) {
01549         sws_freeContext(context);
01550         context = NULL;
01551     }
01552 
01553     if (!context) {
01554         if (!(context = sws_alloc_context()))
01555             return NULL;
01556         context->srcW      = srcW;
01557         context->srcH      = srcH;
01558         context->srcRange  = handle_jpeg(&srcFormat);
01559         context->src0Alpha = handle_0alpha(&srcFormat);
01560         context->srcFormat = srcFormat;
01561         context->dstW      = dstW;
01562         context->dstH      = dstH;
01563         context->dstRange  = handle_jpeg(&dstFormat);
01564         context->dst0Alpha = handle_0alpha(&dstFormat);
01565         context->dstFormat = dstFormat;
01566         context->flags     = flags;
01567         context->param[0]  = param[0];
01568         context->param[1]  = param[1];
01569         sws_setColorspaceDetails(context, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], context->srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, context->dstRange, 0, 1<<16, 1<<16);
01570         if (sws_init_context(context, srcFilter, dstFilter) < 0) {
01571             sws_freeContext(context);
01572             return NULL;
01573         }
01574     }
01575     return context;
01576 }
01577