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