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

libswscale/utils.c

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

Generated on Wed Apr 11 2012 07:31:35 for FFmpeg by  doxygen 1.7.1