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