2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #define _SVID_SOURCE //needed for MAP_ANONYMOUS
30 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
31 #define MAP_ANONYMOUS MAP_ANON
35 #define WIN32_LEAN_AND_MEAN
39 #include "swscale_internal.h"
41 #include "libavutil/intreadwrite.h"
42 #include "libavutil/x86_cpu.h"
43 #include "libavutil/cpu.h"
44 #include "libavutil/avutil.h"
45 #include "libavutil/bswap.h"
46 #include "libavutil/mathematics.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/pixdesc.h"
50 unsigned swscale_version(void)
52 return LIBSWSCALE_VERSION_INT;
55 const char *swscale_configuration(void)
57 return LIBAV_CONFIGURATION;
60 const char *swscale_license(void)
62 #define LICENSE_PREFIX "libswscale license: "
63 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
66 #define RET 0xC3 //near return opcode for x86
68 typedef struct FormatEntry {
69 int is_supported_in, is_supported_out;
72 static const FormatEntry format_entries[PIX_FMT_NB] = {
73 [PIX_FMT_YUV420P] = { 1 , 1 },
74 [PIX_FMT_YUYV422] = { 1 , 1 },
75 [PIX_FMT_RGB24] = { 1 , 1 },
76 [PIX_FMT_BGR24] = { 1 , 1 },
77 [PIX_FMT_YUV422P] = { 1 , 1 },
78 [PIX_FMT_YUV444P] = { 1 , 1 },
79 [PIX_FMT_YUV410P] = { 1 , 1 },
80 [PIX_FMT_YUV411P] = { 1 , 1 },
81 [PIX_FMT_GRAY8] = { 1 , 1 },
82 [PIX_FMT_MONOWHITE] = { 1 , 1 },
83 [PIX_FMT_MONOBLACK] = { 1 , 1 },
84 [PIX_FMT_PAL8] = { 1 , 0 },
85 [PIX_FMT_YUVJ420P] = { 1 , 1 },
86 [PIX_FMT_YUVJ422P] = { 1 , 1 },
87 [PIX_FMT_YUVJ444P] = { 1 , 1 },
88 [PIX_FMT_UYVY422] = { 1 , 1 },
89 [PIX_FMT_UYYVYY411] = { 0 , 0 },
90 [PIX_FMT_BGR8] = { 1 , 1 },
91 [PIX_FMT_BGR4] = { 0 , 1 },
92 [PIX_FMT_BGR4_BYTE] = { 1 , 1 },
93 [PIX_FMT_RGB8] = { 1 , 1 },
94 [PIX_FMT_RGB4] = { 0 , 1 },
95 [PIX_FMT_RGB4_BYTE] = { 1 , 1 },
96 [PIX_FMT_NV12] = { 1 , 1 },
97 [PIX_FMT_NV21] = { 1 , 1 },
98 [PIX_FMT_ARGB] = { 1 , 1 },
99 [PIX_FMT_RGBA] = { 1 , 1 },
100 [PIX_FMT_ABGR] = { 1 , 1 },
101 [PIX_FMT_BGRA] = { 1 , 1 },
102 [PIX_FMT_GRAY16BE] = { 1 , 1 },
103 [PIX_FMT_GRAY16LE] = { 1 , 1 },
104 [PIX_FMT_YUV440P] = { 1 , 1 },
105 [PIX_FMT_YUVJ440P] = { 1 , 1 },
106 [PIX_FMT_YUVA420P] = { 1 , 1 },
107 [PIX_FMT_RGB48BE] = { 1 , 1 },
108 [PIX_FMT_RGB48LE] = { 1 , 1 },
109 [PIX_FMT_RGB565BE] = { 1 , 1 },
110 [PIX_FMT_RGB565LE] = { 1 , 1 },
111 [PIX_FMT_RGB555BE] = { 1 , 1 },
112 [PIX_FMT_RGB555LE] = { 1 , 1 },
113 [PIX_FMT_BGR565BE] = { 1 , 1 },
114 [PIX_FMT_BGR565LE] = { 1 , 1 },
115 [PIX_FMT_BGR555BE] = { 1 , 1 },
116 [PIX_FMT_BGR555LE] = { 1 , 1 },
117 [PIX_FMT_YUV420P16LE] = { 1 , 1 },
118 [PIX_FMT_YUV420P16BE] = { 1 , 1 },
119 [PIX_FMT_YUV422P16LE] = { 1 , 1 },
120 [PIX_FMT_YUV422P16BE] = { 1 , 1 },
121 [PIX_FMT_YUV444P16LE] = { 1 , 1 },
122 [PIX_FMT_YUV444P16BE] = { 1 , 1 },
123 [PIX_FMT_RGB444LE] = { 1 , 1 },
124 [PIX_FMT_RGB444BE] = { 1 , 1 },
125 [PIX_FMT_BGR444LE] = { 1 , 1 },
126 [PIX_FMT_BGR444BE] = { 1 , 1 },
127 [PIX_FMT_Y400A] = { 1 , 0 },
128 [PIX_FMT_BGR48BE] = { 1 , 1 },
129 [PIX_FMT_BGR48LE] = { 1 , 1 },
130 [PIX_FMT_YUV420P9BE] = { 1 , 1 },
131 [PIX_FMT_YUV420P9LE] = { 1 , 1 },
132 [PIX_FMT_YUV420P10BE] = { 1 , 1 },
133 [PIX_FMT_YUV420P10LE] = { 1 , 1 },
134 [PIX_FMT_YUV422P9BE] = { 1 , 1 },
135 [PIX_FMT_YUV422P9LE] = { 1 , 1 },
136 [PIX_FMT_YUV422P10BE] = { 1 , 1 },
137 [PIX_FMT_YUV422P10LE] = { 1 , 1 },
138 [PIX_FMT_YUV444P9BE] = { 1 , 1 },
139 [PIX_FMT_YUV444P9LE] = { 1 , 1 },
140 [PIX_FMT_YUV444P10BE] = { 1 , 1 },
141 [PIX_FMT_YUV444P10LE] = { 1 , 1 },
142 [PIX_FMT_GBRP] = { 1 , 0 },
143 [PIX_FMT_GBRP9LE] = { 1 , 0 },
144 [PIX_FMT_GBRP9BE] = { 1 , 0 },
145 [PIX_FMT_GBRP10LE] = { 1 , 0 },
146 [PIX_FMT_GBRP10BE] = { 1 , 0 },
147 [PIX_FMT_GBRP16LE] = { 1 , 0 },
148 [PIX_FMT_GBRP16BE] = { 1 , 0 },
151 int sws_isSupportedInput(enum PixelFormat pix_fmt)
153 return (unsigned)pix_fmt < PIX_FMT_NB ?
154 format_entries[pix_fmt].is_supported_in : 0;
157 int sws_isSupportedOutput(enum PixelFormat pix_fmt)
159 return (unsigned)pix_fmt < PIX_FMT_NB ?
160 format_entries[pix_fmt].is_supported_out : 0;
163 extern const int32_t ff_yuv2rgb_coeffs[8][4];
165 const char *sws_format_name(enum PixelFormat format)
167 if ((unsigned)format < PIX_FMT_NB && av_pix_fmt_descriptors[format].name)
168 return av_pix_fmt_descriptors[format].name;
170 return "Unknown format";
173 static double getSplineCoeff(double a, double b, double c, double d, double dist)
175 if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
176 else return getSplineCoeff( 0.0,
183 static int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
184 int srcW, int dstW, int filterAlign, int one, int flags, int cpu_flags,
185 SwsVector *srcFilter, SwsVector *dstFilter, double param[2], int is_horizontal)
191 int64_t *filter=NULL;
192 int64_t *filter2=NULL;
193 const int64_t fone= 1LL<<54;
196 emms_c(); //FIXME this should not be required but it IS (even for non-MMX versions)
198 // NOTE: the +3 is for the MMX(+1)/SSE(+3) scaler which reads over the end
199 FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW+3)*sizeof(int16_t), fail);
201 if (FFABS(xInc - 0x10000) <10) { // unscaled
204 FF_ALLOCZ_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
206 for (i=0; i<dstW; i++) {
207 filter[i*filterSize]= fone;
211 } else if (flags&SWS_POINT) { // lame looking point sampling mode
215 FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
217 xDstInSrc= xInc/2 - 0x8000;
218 for (i=0; i<dstW; i++) {
219 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
225 } else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) { // bilinear upscale
229 FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
231 xDstInSrc= xInc/2 - 0x8000;
232 for (i=0; i<dstW; i++) {
233 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
237 //bilinear upscale / linear interpolate / area averaging
238 for (j=0; j<filterSize; j++) {
239 int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
240 if (coeff<0) coeff=0;
241 filter[i*filterSize + j]= coeff;
250 if (flags&SWS_BICUBIC) sizeFactor= 4;
251 else if (flags&SWS_X) sizeFactor= 8;
252 else if (flags&SWS_AREA) sizeFactor= 1; //downscale only, for upscale it is bilinear
253 else if (flags&SWS_GAUSS) sizeFactor= 8; // infinite ;)
254 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
255 else if (flags&SWS_SINC) sizeFactor= 20; // infinite ;)
256 else if (flags&SWS_SPLINE) sizeFactor= 20; // infinite ;)
257 else if (flags&SWS_BILINEAR) sizeFactor= 2;
259 sizeFactor= 0; //GCC warning killer
263 if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
264 else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
266 filterSize = FFMIN(filterSize, srcW - 2);
267 filterSize = FFMAX(filterSize, 1);
269 FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
271 xDstInSrc= xInc - 0x10000;
272 for (i=0; i<dstW; i++) {
273 int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
276 for (j=0; j<filterSize; j++) {
277 int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
283 floatd= d * (1.0/(1<<30));
285 if (flags & SWS_BICUBIC) {
286 int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1<<24);
287 int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
292 int64_t dd = (d * d) >> 30;
293 int64_t ddd = (dd * d) >> 30;
296 coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
298 coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
300 coeff *= fone>>(30+24);
302 /* else if (flags & SWS_X) {
303 double p= param ? param*0.01 : 0.3;
304 coeff = d ? sin(d*M_PI)/(d*M_PI) : 1.0;
305 coeff*= pow(2.0, - p*d*d);
307 else if (flags & SWS_X) {
308 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
312 c = cos(floatd*M_PI);
315 if (c<0.0) c= -pow(-c, A);
317 coeff= (c*0.5 + 0.5)*fone;
318 } else if (flags & SWS_AREA) {
319 int64_t d2= d - (1<<29);
320 if (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
321 else if (d2*xInc < (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
323 coeff *= fone>>(30+16);
324 } else if (flags & SWS_GAUSS) {
325 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
326 coeff = (pow(2.0, - p*floatd*floatd))*fone;
327 } else if (flags & SWS_SINC) {
328 coeff = (d ? sin(floatd*M_PI)/(floatd*M_PI) : 1.0)*fone;
329 } else if (flags & SWS_LANCZOS) {
330 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
331 coeff = (d ? sin(floatd*M_PI)*sin(floatd*M_PI/p)/(floatd*floatd*M_PI*M_PI/p) : 1.0)*fone;
332 if (floatd>p) coeff=0;
333 } else if (flags & SWS_BILINEAR) {
335 if (coeff<0) coeff=0;
337 } else if (flags & SWS_SPLINE) {
338 double p=-2.196152422706632;
339 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
341 coeff= 0.0; //GCC warning killer
345 filter[i*filterSize + j]= coeff;
352 /* apply src & dst Filter to filter -> filter2
355 assert(filterSize>0);
356 filter2Size= filterSize;
357 if (srcFilter) filter2Size+= srcFilter->length - 1;
358 if (dstFilter) filter2Size+= dstFilter->length - 1;
359 assert(filter2Size>0);
360 FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size*dstW*sizeof(*filter2), fail);
362 for (i=0; i<dstW; i++) {
366 for (k=0; k<srcFilter->length; k++) {
367 for (j=0; j<filterSize; j++)
368 filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
371 for (j=0; j<filterSize; j++)
372 filter2[i*filter2Size + j]= filter[i*filterSize + j];
376 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
380 /* try to reduce the filter-size (step1 find size and shift left) */
381 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
383 for (i=dstW-1; i>=0; i--) {
384 int min= filter2Size;
388 /* get rid of near zero elements on the left by shifting left */
389 for (j=0; j<filter2Size; j++) {
391 cutOff += FFABS(filter2[i*filter2Size]);
393 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
395 /* preserve monotonicity because the core can't handle the filter otherwise */
396 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
398 // move filter coefficients left
399 for (k=1; k<filter2Size; k++)
400 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
401 filter2[i*filter2Size + k - 1]= 0;
406 /* count near zeros on the right */
407 for (j=filter2Size-1; j>0; j--) {
408 cutOff += FFABS(filter2[i*filter2Size + j]);
410 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
414 if (min>minFilterSize) minFilterSize= min;
417 if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) {
418 // we can handle the special case 4,
419 // so we don't want to go to the full 8
420 if (minFilterSize < 5)
423 // We really don't want to waste our time
424 // doing useless computation, so fall back on
425 // the scalar C code for very small filters.
426 // Vectorizing is worth it only if you have a
427 // decent-sized vector.
428 if (minFilterSize < 3)
432 if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
433 // special case for unscaled vertical filtering
434 if (minFilterSize == 1 && filterAlign == 2)
438 assert(minFilterSize > 0);
439 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
440 assert(filterSize > 0);
441 filter= av_malloc(filterSize*dstW*sizeof(*filter));
442 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
444 *outFilterSize= filterSize;
446 if (flags&SWS_PRINT_INFO)
447 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
448 /* try to reduce the filter-size (step2 reduce it) */
449 for (i=0; i<dstW; i++) {
452 for (j=0; j<filterSize; j++) {
453 if (j>=filter2Size) filter[i*filterSize + j]= 0;
454 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
455 if((flags & SWS_BITEXACT) && j>=minFilterSize)
456 filter[i*filterSize + j]= 0;
460 //FIXME try to align filterPos if possible
464 for (i = 0; i < dstW; i++) {
466 if ((*filterPos)[i] < 0) {
467 // move filter coefficients left to compensate for filterPos
468 for (j = 1; j < filterSize; j++) {
469 int left = FFMAX(j + (*filterPos)[i], 0);
470 filter[i * filterSize + left] += filter[i * filterSize + j];
471 filter[i * filterSize + j ] = 0;
476 if ((*filterPos)[i] + filterSize > srcW) {
477 int shift = (*filterPos)[i] + filterSize - srcW;
478 // move filter coefficients right to compensate for filterPos
479 for (j = filterSize - 2; j >= 0; j--) {
480 int right = FFMIN(j + shift, filterSize - 1);
481 filter[i * filterSize + right] += filter[i * filterSize + j];
482 filter[i * filterSize + j ] = 0;
484 (*filterPos)[i] = srcW - filterSize;
489 // Note the +1 is for the MMX scaler which reads over the end
490 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
491 FF_ALLOCZ_OR_GOTO(NULL, *outFilter, *outFilterSize*(dstW+3)*sizeof(int16_t), fail);
493 /* normalize & store in outFilter */
494 for (i=0; i<dstW; i++) {
499 for (j=0; j<filterSize; j++) {
500 sum+= filter[i*filterSize + j];
502 sum= (sum + one/2)/ one;
503 for (j=0; j<*outFilterSize; j++) {
504 int64_t v= filter[i*filterSize + j] + error;
505 int intV= ROUNDED_DIV(v, sum);
506 (*outFilter)[i*(*outFilterSize) + j]= intV;
511 (*filterPos)[dstW+0] =
512 (*filterPos)[dstW+1] =
513 (*filterPos)[dstW+2] = (*filterPos)[dstW-1]; // the MMX/SSE scaler will read over the end
514 for (i=0; i<*outFilterSize; i++) {
515 int k= (dstW - 1) * (*outFilterSize) + i;
516 (*outFilter)[k + 1 * (*outFilterSize)] =
517 (*outFilter)[k + 2 * (*outFilterSize)] =
518 (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
529 static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode, int16_t *filter, int32_t *filterPos, int numSplits)
532 x86_reg imm8OfPShufW1A;
533 x86_reg imm8OfPShufW2A;
534 x86_reg fragmentLengthA;
536 x86_reg imm8OfPShufW1B;
537 x86_reg imm8OfPShufW2B;
538 x86_reg fragmentLengthB;
543 // create an optimized horizontal scaling routine
544 /* This scaler is made of runtime-generated MMX2 code using specially
545 * tuned pshufw instructions. For every four output pixels, if four
546 * input pixels are enough for the fast bilinear scaling, then a chunk
547 * of fragmentB is used. If five input pixels are needed, then a chunk
548 * of fragmentA is used.
557 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
558 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
559 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
560 "punpcklbw %%mm7, %%mm1 \n\t"
561 "punpcklbw %%mm7, %%mm0 \n\t"
562 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
564 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
566 "psubw %%mm1, %%mm0 \n\t"
567 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
568 "pmullw %%mm3, %%mm0 \n\t"
569 "psllw $7, %%mm1 \n\t"
570 "paddw %%mm1, %%mm0 \n\t"
572 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
574 "add $8, %%"REG_a" \n\t"
578 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
579 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
580 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
585 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
589 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
590 "=r" (fragmentLengthA)
597 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
598 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
599 "punpcklbw %%mm7, %%mm0 \n\t"
600 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
602 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
604 "psubw %%mm1, %%mm0 \n\t"
605 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
606 "pmullw %%mm3, %%mm0 \n\t"
607 "psllw $7, %%mm1 \n\t"
608 "paddw %%mm1, %%mm0 \n\t"
610 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
612 "add $8, %%"REG_a" \n\t"
616 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
617 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
618 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
623 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
627 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
628 "=r" (fragmentLengthB)
631 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
634 for (i=0; i<dstW/numSplits; i++) {
639 int b=((xpos+xInc)>>16) - xx;
640 int c=((xpos+xInc*2)>>16) - xx;
641 int d=((xpos+xInc*3)>>16) - xx;
643 uint8_t *fragment = (d+1<4) ? fragmentB : fragmentA;
644 x86_reg imm8OfPShufW1 = (d+1<4) ? imm8OfPShufW1B : imm8OfPShufW1A;
645 x86_reg imm8OfPShufW2 = (d+1<4) ? imm8OfPShufW2B : imm8OfPShufW2A;
646 x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
647 int maxShift= 3-(d+inc);
651 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
652 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
653 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
654 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
657 memcpy(filterCode + fragmentPos, fragment, fragmentLength);
659 filterCode[fragmentPos + imm8OfPShufW1]=
660 (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
661 filterCode[fragmentPos + imm8OfPShufW2]=
662 a | (b<<2) | (c<<4) | (d<<6);
664 if (i+4-inc>=dstW) shift=maxShift; //avoid overread
665 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
667 if (shift && i>=shift) {
668 filterCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
669 filterCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
670 filterPos[i/2]-=shift;
674 fragmentPos+= fragmentLength;
677 filterCode[fragmentPos]= RET;
682 filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
684 return fragmentPos + 1;
686 #endif /* HAVE_MMX2 */
688 static void getSubSampleFactors(int *h, int *v, enum PixelFormat format)
690 *h = av_pix_fmt_descriptors[format].log2_chroma_w;
691 *v = av_pix_fmt_descriptors[format].log2_chroma_h;
694 int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
695 int srcRange, const int table[4], int dstRange,
696 int brightness, int contrast, int saturation)
698 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
699 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
701 c->brightness= brightness;
702 c->contrast = contrast;
703 c->saturation= saturation;
704 c->srcRange = srcRange;
705 c->dstRange = dstRange;
706 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
708 c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->dstFormat]);
709 c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->srcFormat]);
711 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
714 if (HAVE_ALTIVEC && av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC)
715 ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
719 int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
720 int *srcRange, int **table, int *dstRange,
721 int *brightness, int *contrast, int *saturation)
723 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
725 *inv_table = c->srcColorspaceTable;
726 *table = c->dstColorspaceTable;
727 *srcRange = c->srcRange;
728 *dstRange = c->dstRange;
729 *brightness= c->brightness;
730 *contrast = c->contrast;
731 *saturation= c->saturation;
736 static int handle_jpeg(enum PixelFormat *format)
739 case PIX_FMT_YUVJ420P: *format = PIX_FMT_YUV420P; return 1;
740 case PIX_FMT_YUVJ422P: *format = PIX_FMT_YUV422P; return 1;
741 case PIX_FMT_YUVJ444P: *format = PIX_FMT_YUV444P; return 1;
742 case PIX_FMT_YUVJ440P: *format = PIX_FMT_YUV440P; return 1;
747 SwsContext *sws_alloc_context(void)
749 SwsContext *c= av_mallocz(sizeof(SwsContext));
751 c->av_class = &sws_context_class;
752 av_opt_set_defaults(c);
757 int sws_init_context(SwsContext *c, SwsFilter *srcFilter, SwsFilter *dstFilter)
760 int usesVFilter, usesHFilter;
762 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
767 int dst_stride = FFALIGN(dstW * sizeof(int16_t) + 16, 16), dst_stride_px = dst_stride >> 1;
768 int flags, cpu_flags;
769 enum PixelFormat srcFormat= c->srcFormat;
770 enum PixelFormat dstFormat= c->dstFormat;
772 cpu_flags = av_get_cpu_flags();
775 if (!rgb15to16) sws_rgb2rgb_init();
777 unscaled = (srcW == dstW && srcH == dstH);
779 if (!sws_isSupportedInput(srcFormat)) {
780 av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n", sws_format_name(srcFormat));
781 return AVERROR(EINVAL);
783 if (!sws_isSupportedOutput(dstFormat)) {
784 av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n", sws_format_name(dstFormat));
785 return AVERROR(EINVAL);
788 i= flags & ( SWS_POINT
799 if(!i || (i & (i-1))) {
800 av_log(c, AV_LOG_ERROR, "Exactly one scaler algorithm must be chosen\n");
801 return AVERROR(EINVAL);
804 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
805 av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
806 srcW, srcH, dstW, dstH);
807 return AVERROR(EINVAL);
810 if (!dstFilter) dstFilter= &dummyFilter;
811 if (!srcFilter) srcFilter= &dummyFilter;
813 c->lumXInc= (((int64_t)srcW<<16) + (dstW>>1))/dstW;
814 c->lumYInc= (((int64_t)srcH<<16) + (dstH>>1))/dstH;
815 c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[dstFormat]);
816 c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[srcFormat]);
817 c->vRounder= 4* 0x0001000100010001ULL;
819 usesVFilter = (srcFilter->lumV && srcFilter->lumV->length>1) ||
820 (srcFilter->chrV && srcFilter->chrV->length>1) ||
821 (dstFilter->lumV && dstFilter->lumV->length>1) ||
822 (dstFilter->chrV && dstFilter->chrV->length>1);
823 usesHFilter = (srcFilter->lumH && srcFilter->lumH->length>1) ||
824 (srcFilter->chrH && srcFilter->chrH->length>1) ||
825 (dstFilter->lumH && dstFilter->lumH->length>1) ||
826 (dstFilter->chrH && dstFilter->chrH->length>1);
828 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
829 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
831 // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
832 if (flags & SWS_FULL_CHR_H_INT &&
833 isAnyRGB(dstFormat) &&
834 dstFormat != PIX_FMT_RGBA &&
835 dstFormat != PIX_FMT_ARGB &&
836 dstFormat != PIX_FMT_BGRA &&
837 dstFormat != PIX_FMT_ABGR &&
838 dstFormat != PIX_FMT_RGB24 &&
839 dstFormat != PIX_FMT_BGR24) {
840 av_log(c, AV_LOG_ERROR,
841 "full chroma interpolation for destination format '%s' not yet implemented\n",
842 sws_format_name(dstFormat));
843 flags &= ~SWS_FULL_CHR_H_INT;
846 if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
848 // drop some chroma lines if the user wants it
849 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
850 c->chrSrcVSubSample+= c->vChrDrop;
852 // drop every other pixel for chroma calculation unless user wants full chroma
853 if (isAnyRGB(srcFormat) && !(flags&SWS_FULL_CHR_H_INP)
854 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
855 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
856 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
857 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&SWS_FAST_BILINEAR)))
858 c->chrSrcHSubSample=1;
860 // Note the -((-x)>>y) is so that we always round toward +inf.
861 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
862 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
863 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
864 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
866 /* unscaled special cases */
867 if (unscaled && !usesHFilter && !usesVFilter && (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
868 ff_get_unscaled_swscale(c);
871 if (flags&SWS_PRINT_INFO)
872 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
873 sws_format_name(srcFormat), sws_format_name(dstFormat));
878 c->srcBpc = 1 + av_pix_fmt_descriptors[srcFormat].comp[0].depth_minus1;
881 c->dstBpc = 1 + av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1;
886 FF_ALLOC_OR_GOTO(c, c->formatConvBuffer,
887 (FFALIGN(srcW, 16) * 2 * FFALIGN(c->srcBpc, 8) >> 3) + 16,
889 if (HAVE_MMX2 && cpu_flags & AV_CPU_FLAG_MMX2 && c->srcBpc == 8 && c->dstBpc <= 10) {
890 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
891 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR)) {
892 if (flags&SWS_PRINT_INFO)
893 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
895 if (usesHFilter) c->canMMX2BeUsed=0;
900 c->chrXInc= (((int64_t)c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
901 c->chrYInc= (((int64_t)c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
903 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
904 // but only for the FAST_BILINEAR mode otherwise do correct scaling
905 // n-2 is the last chrominance sample available
906 // this is not perfect, but no one should notice the difference, the more correct variant
907 // would be like the vertical one, but that would require some special code for the
908 // first and last pixel
909 if (flags&SWS_FAST_BILINEAR) {
910 if (c->canMMX2BeUsed) {
914 //we don't use the x86 asm scaler if MMX is available
915 else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
916 c->lumXInc = ((int64_t)(srcW-2)<<16)/(dstW-2) - 20;
917 c->chrXInc = ((int64_t)(c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
921 /* precalculate horizontal scaler filter coefficients */
924 // can't downscale !!!
925 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
926 c->lumMmx2FilterCodeSize = initMMX2HScaler( dstW, c->lumXInc, NULL, NULL, NULL, 8);
927 c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
930 c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
931 c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
932 #elif HAVE_VIRTUALALLOC
933 c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
934 c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
936 c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
937 c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
940 if (!c->lumMmx2FilterCode || !c->chrMmx2FilterCode)
941 return AVERROR(ENOMEM);
942 FF_ALLOCZ_OR_GOTO(c, c->hLumFilter , (dstW /8+8)*sizeof(int16_t), fail);
943 FF_ALLOCZ_OR_GOTO(c, c->hChrFilter , (c->chrDstW /4+8)*sizeof(int16_t), fail);
944 FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW /2/8+8)*sizeof(int32_t), fail);
945 FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t), fail);
947 initMMX2HScaler( dstW, c->lumXInc, c->lumMmx2FilterCode, c->hLumFilter, c->hLumFilterPos, 8);
948 initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->hChrFilter, c->hChrFilterPos, 4);
951 mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
952 mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
955 #endif /* HAVE_MMX2 */
957 const int filterAlign=
958 (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 4 :
959 (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
962 if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
963 srcW , dstW, filterAlign, 1<<14,
964 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags, cpu_flags,
965 srcFilter->lumH, dstFilter->lumH, c->param, 1) < 0)
967 if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
968 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
969 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags, cpu_flags,
970 srcFilter->chrH, dstFilter->chrH, c->param, 1) < 0)
973 } // initialize horizontal stuff
975 /* precalculate vertical scaler filter coefficients */
977 const int filterAlign=
978 (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 2 :
979 (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
982 if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
983 srcH , dstH, filterAlign, (1<<12),
984 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags, cpu_flags,
985 srcFilter->lumV, dstFilter->lumV, c->param, 0) < 0)
987 if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
988 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
989 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags, cpu_flags,
990 srcFilter->chrV, dstFilter->chrV, c->param, 0) < 0)
994 FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH, fail);
995 FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH, fail);
997 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
999 short *p = (short *)&c->vYCoeffsBank[i];
1001 p[j] = c->vLumFilter[i];
1004 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
1006 short *p = (short *)&c->vCCoeffsBank[i];
1008 p[j] = c->vChrFilter[i];
1013 // calculate buffer sizes so that they won't run out while handling these damn slices
1014 c->vLumBufSize= c->vLumFilterSize;
1015 c->vChrBufSize= c->vChrFilterSize;
1016 for (i=0; i<dstH; i++) {
1017 int chrI= i*c->chrDstH / dstH;
1018 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
1019 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
1021 nextSlice>>= c->chrSrcVSubSample;
1022 nextSlice<<= c->chrSrcVSubSample;
1023 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
1024 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
1025 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
1026 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
1029 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
1030 // allocate several megabytes to handle all possible cases)
1031 FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize*3*sizeof(int16_t*), fail);
1032 FF_ALLOC_OR_GOTO(c, c->chrUPixBuf, c->vChrBufSize*3*sizeof(int16_t*), fail);
1033 FF_ALLOC_OR_GOTO(c, c->chrVPixBuf, c->vChrBufSize*3*sizeof(int16_t*), fail);
1034 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
1035 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize*3*sizeof(int16_t*), fail);
1036 //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)
1037 /* align at 16 bytes for AltiVec */
1038 for (i=0; i<c->vLumBufSize; i++) {
1039 FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i+c->vLumBufSize], dst_stride+16, fail);
1040 c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
1042 // 64 / (c->dstBpc & ~7) is the same as 16 / sizeof(scaling_intermediate)
1043 c->uv_off_px = dst_stride_px + 64 / (c->dstBpc &~ 7);
1044 c->uv_off_byte = dst_stride + 16;
1045 for (i=0; i<c->vChrBufSize; i++) {
1046 FF_ALLOC_OR_GOTO(c, c->chrUPixBuf[i+c->vChrBufSize], dst_stride*2+32, fail);
1047 c->chrUPixBuf[i] = c->chrUPixBuf[i+c->vChrBufSize];
1048 c->chrVPixBuf[i] = c->chrVPixBuf[i+c->vChrBufSize] = c->chrUPixBuf[i] + (dst_stride >> 1) + 8;
1050 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1051 for (i=0; i<c->vLumBufSize; i++) {
1052 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i+c->vLumBufSize], dst_stride+16, fail);
1053 c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
1056 //try to avoid drawing green stuff between the right end and the stride end
1057 for (i=0; i<c->vChrBufSize; i++)
1058 memset(c->chrUPixBuf[i], 64, dst_stride*2+1);
1060 assert(c->chrDstH <= dstH);
1062 if (flags&SWS_PRINT_INFO) {
1063 if (flags&SWS_FAST_BILINEAR) av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
1064 else if (flags&SWS_BILINEAR) av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
1065 else if (flags&SWS_BICUBIC) av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
1066 else if (flags&SWS_X) av_log(c, AV_LOG_INFO, "Experimental scaler, ");
1067 else if (flags&SWS_POINT) av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
1068 else if (flags&SWS_AREA) av_log(c, AV_LOG_INFO, "Area Averaging scaler, ");
1069 else if (flags&SWS_BICUBLIN) av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
1070 else if (flags&SWS_GAUSS) av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
1071 else if (flags&SWS_SINC) av_log(c, AV_LOG_INFO, "Sinc scaler, ");
1072 else if (flags&SWS_LANCZOS) av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
1073 else if (flags&SWS_SPLINE) av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
1074 else av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
1076 av_log(c, AV_LOG_INFO, "from %s to %s%s ",
1077 sws_format_name(srcFormat),
1079 dstFormat == PIX_FMT_BGR555 || dstFormat == PIX_FMT_BGR565 ||
1080 dstFormat == PIX_FMT_RGB444BE || dstFormat == PIX_FMT_RGB444LE ||
1081 dstFormat == PIX_FMT_BGR444BE || dstFormat == PIX_FMT_BGR444LE ? "dithered " : "",
1085 sws_format_name(dstFormat));
1087 if (HAVE_MMX2 && cpu_flags & AV_CPU_FLAG_MMX2) av_log(c, AV_LOG_INFO, "using MMX2\n");
1088 else if (HAVE_AMD3DNOW && cpu_flags & AV_CPU_FLAG_3DNOW) av_log(c, AV_LOG_INFO, "using 3DNOW\n");
1089 else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) av_log(c, AV_LOG_INFO, "using MMX\n");
1090 else if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) av_log(c, AV_LOG_INFO, "using AltiVec\n");
1091 else av_log(c, AV_LOG_INFO, "using C\n");
1093 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1094 av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1095 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
1096 av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1097 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
1100 c->swScale= ff_getSwsFunc(c);
1102 fail: //FIXME replace things by appropriate error codes
1106 #if FF_API_SWS_GETCONTEXT
1107 SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat,
1108 int dstW, int dstH, enum PixelFormat dstFormat, int flags,
1109 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
1113 if(!(c=sws_alloc_context()))
1121 c->srcRange = handle_jpeg(&srcFormat);
1122 c->dstRange = handle_jpeg(&dstFormat);
1123 c->srcFormat= srcFormat;
1124 c->dstFormat= dstFormat;
1127 c->param[0] = param[0];
1128 c->param[1] = param[1];
1130 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);
1132 if(sws_init_context(c, srcFilter, dstFilter) < 0){
1141 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
1142 float lumaSharpen, float chromaSharpen,
1143 float chromaHShift, float chromaVShift,
1146 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
1150 if (lumaGBlur!=0.0) {
1151 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
1152 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
1154 filter->lumH= sws_getIdentityVec();
1155 filter->lumV= sws_getIdentityVec();
1158 if (chromaGBlur!=0.0) {
1159 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
1160 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
1162 filter->chrH= sws_getIdentityVec();
1163 filter->chrV= sws_getIdentityVec();
1166 if (chromaSharpen!=0.0) {
1167 SwsVector *id= sws_getIdentityVec();
1168 sws_scaleVec(filter->chrH, -chromaSharpen);
1169 sws_scaleVec(filter->chrV, -chromaSharpen);
1170 sws_addVec(filter->chrH, id);
1171 sws_addVec(filter->chrV, id);
1175 if (lumaSharpen!=0.0) {
1176 SwsVector *id= sws_getIdentityVec();
1177 sws_scaleVec(filter->lumH, -lumaSharpen);
1178 sws_scaleVec(filter->lumV, -lumaSharpen);
1179 sws_addVec(filter->lumH, id);
1180 sws_addVec(filter->lumV, id);
1184 if (chromaHShift != 0.0)
1185 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
1187 if (chromaVShift != 0.0)
1188 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
1190 sws_normalizeVec(filter->chrH, 1.0);
1191 sws_normalizeVec(filter->chrV, 1.0);
1192 sws_normalizeVec(filter->lumH, 1.0);
1193 sws_normalizeVec(filter->lumV, 1.0);
1195 if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
1196 if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
1201 SwsVector *sws_allocVec(int length)
1203 SwsVector *vec = av_malloc(sizeof(SwsVector));
1206 vec->length = length;
1207 vec->coeff = av_malloc(sizeof(double) * length);
1213 SwsVector *sws_getGaussianVec(double variance, double quality)
1215 const int length= (int)(variance*quality + 0.5) | 1;
1217 double middle= (length-1)*0.5;
1218 SwsVector *vec= sws_allocVec(length);
1223 for (i=0; i<length; i++) {
1224 double dist= i-middle;
1225 vec->coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*M_PI);
1228 sws_normalizeVec(vec, 1.0);
1233 SwsVector *sws_getConstVec(double c, int length)
1236 SwsVector *vec= sws_allocVec(length);
1241 for (i=0; i<length; i++)
1247 SwsVector *sws_getIdentityVec(void)
1249 return sws_getConstVec(1.0, 1);
1252 static double sws_dcVec(SwsVector *a)
1257 for (i=0; i<a->length; i++)
1263 void sws_scaleVec(SwsVector *a, double scalar)
1267 for (i=0; i<a->length; i++)
1268 a->coeff[i]*= scalar;
1271 void sws_normalizeVec(SwsVector *a, double height)
1273 sws_scaleVec(a, height/sws_dcVec(a));
1276 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
1278 int length= a->length + b->length - 1;
1280 SwsVector *vec= sws_getConstVec(0.0, length);
1285 for (i=0; i<a->length; i++) {
1286 for (j=0; j<b->length; j++) {
1287 vec->coeff[i+j]+= a->coeff[i]*b->coeff[j];
1294 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
1296 int length= FFMAX(a->length, b->length);
1298 SwsVector *vec= sws_getConstVec(0.0, length);
1303 for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
1304 for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
1309 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
1311 int length= FFMAX(a->length, b->length);
1313 SwsVector *vec= sws_getConstVec(0.0, length);
1318 for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
1319 for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
1324 /* shift left / or right if "shift" is negative */
1325 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
1327 int length= a->length + FFABS(shift)*2;
1329 SwsVector *vec= sws_getConstVec(0.0, length);
1334 for (i=0; i<a->length; i++) {
1335 vec->coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
1341 void sws_shiftVec(SwsVector *a, int shift)
1343 SwsVector *shifted= sws_getShiftedVec(a, shift);
1345 a->coeff= shifted->coeff;
1346 a->length= shifted->length;
1350 void sws_addVec(SwsVector *a, SwsVector *b)
1352 SwsVector *sum= sws_sumVec(a, b);
1354 a->coeff= sum->coeff;
1355 a->length= sum->length;
1359 void sws_subVec(SwsVector *a, SwsVector *b)
1361 SwsVector *diff= sws_diffVec(a, b);
1363 a->coeff= diff->coeff;
1364 a->length= diff->length;
1368 void sws_convVec(SwsVector *a, SwsVector *b)
1370 SwsVector *conv= sws_getConvVec(a, b);
1372 a->coeff= conv->coeff;
1373 a->length= conv->length;
1377 SwsVector *sws_cloneVec(SwsVector *a)
1380 SwsVector *vec= sws_allocVec(a->length);
1385 for (i=0; i<a->length; i++) vec->coeff[i]= a->coeff[i];
1390 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
1397 for (i=0; i<a->length; i++)
1398 if (a->coeff[i]>max) max= a->coeff[i];
1400 for (i=0; i<a->length; i++)
1401 if (a->coeff[i]<min) min= a->coeff[i];
1405 for (i=0; i<a->length; i++) {
1406 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
1407 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
1408 for (;x>0; x--) av_log(log_ctx, log_level, " ");
1409 av_log(log_ctx, log_level, "|\n");
1413 void sws_freeVec(SwsVector *a)
1416 av_freep(&a->coeff);
1421 void sws_freeFilter(SwsFilter *filter)
1423 if (!filter) return;
1425 if (filter->lumH) sws_freeVec(filter->lumH);
1426 if (filter->lumV) sws_freeVec(filter->lumV);
1427 if (filter->chrH) sws_freeVec(filter->chrH);
1428 if (filter->chrV) sws_freeVec(filter->chrV);
1432 void sws_freeContext(SwsContext *c)
1438 for (i=0; i<c->vLumBufSize; i++)
1439 av_freep(&c->lumPixBuf[i]);
1440 av_freep(&c->lumPixBuf);
1443 if (c->chrUPixBuf) {
1444 for (i=0; i<c->vChrBufSize; i++)
1445 av_freep(&c->chrUPixBuf[i]);
1446 av_freep(&c->chrUPixBuf);
1447 av_freep(&c->chrVPixBuf);
1450 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1451 for (i=0; i<c->vLumBufSize; i++)
1452 av_freep(&c->alpPixBuf[i]);
1453 av_freep(&c->alpPixBuf);
1456 av_freep(&c->vLumFilter);
1457 av_freep(&c->vChrFilter);
1458 av_freep(&c->hLumFilter);
1459 av_freep(&c->hChrFilter);
1461 av_freep(&c->vYCoeffsBank);
1462 av_freep(&c->vCCoeffsBank);
1465 av_freep(&c->vLumFilterPos);
1466 av_freep(&c->vChrFilterPos);
1467 av_freep(&c->hLumFilterPos);
1468 av_freep(&c->hChrFilterPos);
1471 #ifdef MAP_ANONYMOUS
1472 if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
1473 if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
1474 #elif HAVE_VIRTUALALLOC
1475 if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, 0, MEM_RELEASE);
1476 if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, 0, MEM_RELEASE);
1478 av_free(c->lumMmx2FilterCode);
1479 av_free(c->chrMmx2FilterCode);
1481 c->lumMmx2FilterCode=NULL;
1482 c->chrMmx2FilterCode=NULL;
1483 #endif /* HAVE_MMX */
1485 av_freep(&c->yuvTable);
1486 av_free(c->formatConvBuffer);
1491 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
1492 int srcW, int srcH, enum PixelFormat srcFormat,
1493 int dstW, int dstH, enum PixelFormat dstFormat, int flags,
1494 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
1496 static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
1499 param = default_param;
1502 (context->srcW != srcW ||
1503 context->srcH != srcH ||
1504 context->srcFormat != srcFormat ||
1505 context->dstW != dstW ||
1506 context->dstH != dstH ||
1507 context->dstFormat != dstFormat ||
1508 context->flags != flags ||
1509 context->param[0] != param[0] ||
1510 context->param[1] != param[1])) {
1511 sws_freeContext(context);
1516 if (!(context = sws_alloc_context()))
1518 context->srcW = srcW;
1519 context->srcH = srcH;
1520 context->srcRange = handle_jpeg(&srcFormat);
1521 context->srcFormat = srcFormat;
1522 context->dstW = dstW;
1523 context->dstH = dstH;
1524 context->dstRange = handle_jpeg(&dstFormat);
1525 context->dstFormat = dstFormat;
1526 context->flags = flags;
1527 context->param[0] = param[0];
1528 context->param[1] = param[1];
1529 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);
1530 if (sws_init_context(context, srcFilter, dstFilter) < 0) {
1531 sws_freeContext(context);