Libav

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