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