Rename swscale-example to swscale-test, to better reflect the intended
[libswscale.git] / utils.c
blob95edcb42a76fc97d1daea755a6863c1571f72a9a
1 /*
2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * FFmpeg 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * the C code (not assembly, mmx, ...) of this file can be used
21 * under the LGPL license too
24 #define _SVID_SOURCE //needed for MAP_ANONYMOUS
25 #include <inttypes.h>
26 #include <string.h>
27 #include <math.h>
28 #include <stdio.h>
29 #include "config.h"
30 #include <assert.h>
31 #if HAVE_SYS_MMAN_H
32 #include <sys/mman.h>
33 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
34 #define MAP_ANONYMOUS MAP_ANON
35 #endif
36 #endif
37 #if HAVE_VIRTUALALLOC
38 #define WIN32_LEAN_AND_MEAN
39 #include <windows.h>
40 #endif
41 #include "swscale.h"
42 #include "swscale_internal.h"
43 #include "rgb2rgb.h"
44 #include "libavutil/intreadwrite.h"
45 #include "libavutil/x86_cpu.h"
46 #include "libavutil/avutil.h"
47 #include "libavutil/bswap.h"
48 #include "libavutil/pixdesc.h"
50 unsigned swscale_version(void)
52 return LIBSWSCALE_VERSION_INT;
55 const char *swscale_configuration(void)
57 return FFMPEG_CONFIGURATION;
60 const char *swscale_license(void)
62 #define LICENSE_PREFIX "libswscale license: "
63 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
66 #define RET 0xC3 //near return opcode for x86
68 #define isSupportedIn(x) ( \
69 (x)==PIX_FMT_YUV420P \
70 || (x)==PIX_FMT_YUVA420P \
71 || (x)==PIX_FMT_YUYV422 \
72 || (x)==PIX_FMT_UYVY422 \
73 || (x)==PIX_FMT_RGB48BE \
74 || (x)==PIX_FMT_RGB48LE \
75 || (x)==PIX_FMT_RGB32 \
76 || (x)==PIX_FMT_RGB32_1 \
77 || (x)==PIX_FMT_BGR24 \
78 || (x)==PIX_FMT_BGR565 \
79 || (x)==PIX_FMT_BGR555 \
80 || (x)==PIX_FMT_BGR32 \
81 || (x)==PIX_FMT_BGR32_1 \
82 || (x)==PIX_FMT_RGB24 \
83 || (x)==PIX_FMT_RGB565 \
84 || (x)==PIX_FMT_RGB555 \
85 || (x)==PIX_FMT_GRAY8 \
86 || (x)==PIX_FMT_YUV410P \
87 || (x)==PIX_FMT_YUV440P \
88 || (x)==PIX_FMT_NV12 \
89 || (x)==PIX_FMT_NV21 \
90 || (x)==PIX_FMT_GRAY16BE \
91 || (x)==PIX_FMT_GRAY16LE \
92 || (x)==PIX_FMT_YUV444P \
93 || (x)==PIX_FMT_YUV422P \
94 || (x)==PIX_FMT_YUV411P \
95 || (x)==PIX_FMT_PAL8 \
96 || (x)==PIX_FMT_BGR8 \
97 || (x)==PIX_FMT_RGB8 \
98 || (x)==PIX_FMT_BGR4_BYTE \
99 || (x)==PIX_FMT_RGB4_BYTE \
100 || (x)==PIX_FMT_YUV440P \
101 || (x)==PIX_FMT_MONOWHITE \
102 || (x)==PIX_FMT_MONOBLACK \
103 || (x)==PIX_FMT_YUV420P16LE \
104 || (x)==PIX_FMT_YUV422P16LE \
105 || (x)==PIX_FMT_YUV444P16LE \
106 || (x)==PIX_FMT_YUV420P16BE \
107 || (x)==PIX_FMT_YUV422P16BE \
108 || (x)==PIX_FMT_YUV444P16BE \
111 int sws_isSupportedInput(enum PixelFormat pix_fmt)
113 return isSupportedIn(pix_fmt);
116 #define isSupportedOut(x) ( \
117 (x)==PIX_FMT_YUV420P \
118 || (x)==PIX_FMT_YUVA420P \
119 || (x)==PIX_FMT_YUYV422 \
120 || (x)==PIX_FMT_UYVY422 \
121 || (x)==PIX_FMT_YUV444P \
122 || (x)==PIX_FMT_YUV422P \
123 || (x)==PIX_FMT_YUV411P \
124 || isAnyRGB(x) \
125 || (x)==PIX_FMT_NV12 \
126 || (x)==PIX_FMT_NV21 \
127 || (x)==PIX_FMT_GRAY16BE \
128 || (x)==PIX_FMT_GRAY16LE \
129 || (x)==PIX_FMT_GRAY8 \
130 || (x)==PIX_FMT_YUV410P \
131 || (x)==PIX_FMT_YUV440P \
132 || (x)==PIX_FMT_YUV420P16LE \
133 || (x)==PIX_FMT_YUV422P16LE \
134 || (x)==PIX_FMT_YUV444P16LE \
135 || (x)==PIX_FMT_YUV420P16BE \
136 || (x)==PIX_FMT_YUV422P16BE \
137 || (x)==PIX_FMT_YUV444P16BE \
140 int sws_isSupportedOutput(enum PixelFormat pix_fmt)
142 return isSupportedOut(pix_fmt);
145 #define usePal(x) (av_pix_fmt_descriptors[x].flags & PIX_FMT_PAL)
147 extern const int32_t ff_yuv2rgb_coeffs[8][4];
149 const char *sws_format_name(enum PixelFormat format)
151 if ((unsigned)format < PIX_FMT_NB && av_pix_fmt_descriptors[format].name)
152 return av_pix_fmt_descriptors[format].name;
153 else
154 return "Unknown format";
157 static double getSplineCoeff(double a, double b, double c, double d, double dist)
159 // printf("%f %f %f %f %f\n", a,b,c,d,dist);
160 if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
161 else return getSplineCoeff( 0.0,
162 b+ 2.0*c + 3.0*d,
163 c + 3.0*d,
164 -b- 3.0*c - 6.0*d,
165 dist-1.0);
168 static int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
169 int srcW, int dstW, int filterAlign, int one, int flags,
170 SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
172 int i;
173 int filterSize;
174 int filter2Size;
175 int minFilterSize;
176 int64_t *filter=NULL;
177 int64_t *filter2=NULL;
178 const int64_t fone= 1LL<<54;
179 int ret= -1;
180 #if ARCH_X86
181 if (flags & SWS_CPU_CAPS_MMX)
182 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
183 #endif
185 // NOTE: the +1 is for the MMX scaler which reads over the end
186 FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW+1)*sizeof(int16_t), fail);
188 if (FFABS(xInc - 0x10000) <10) { // unscaled
189 int i;
190 filterSize= 1;
191 FF_ALLOCZ_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
193 for (i=0; i<dstW; i++) {
194 filter[i*filterSize]= fone;
195 (*filterPos)[i]=i;
198 } else if (flags&SWS_POINT) { // lame looking point sampling mode
199 int i;
200 int xDstInSrc;
201 filterSize= 1;
202 FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
204 xDstInSrc= xInc/2 - 0x8000;
205 for (i=0; i<dstW; i++) {
206 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
208 (*filterPos)[i]= xx;
209 filter[i]= fone;
210 xDstInSrc+= xInc;
212 } else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) { // bilinear upscale
213 int i;
214 int xDstInSrc;
215 filterSize= 2;
216 FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
218 xDstInSrc= xInc/2 - 0x8000;
219 for (i=0; i<dstW; i++) {
220 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
221 int j;
223 (*filterPos)[i]= xx;
224 //bilinear upscale / linear interpolate / area averaging
225 for (j=0; j<filterSize; j++) {
226 int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
227 if (coeff<0) coeff=0;
228 filter[i*filterSize + j]= coeff;
229 xx++;
231 xDstInSrc+= xInc;
233 } else {
234 int xDstInSrc;
235 int sizeFactor;
237 if (flags&SWS_BICUBIC) sizeFactor= 4;
238 else if (flags&SWS_X) sizeFactor= 8;
239 else if (flags&SWS_AREA) sizeFactor= 1; //downscale only, for upscale it is bilinear
240 else if (flags&SWS_GAUSS) sizeFactor= 8; // infinite ;)
241 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
242 else if (flags&SWS_SINC) sizeFactor= 20; // infinite ;)
243 else if (flags&SWS_SPLINE) sizeFactor= 20; // infinite ;)
244 else if (flags&SWS_BILINEAR) sizeFactor= 2;
245 else {
246 sizeFactor= 0; //GCC warning killer
247 assert(0);
250 if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
251 else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
253 if (filterSize > srcW-2) filterSize=srcW-2;
255 FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
257 xDstInSrc= xInc - 0x10000;
258 for (i=0; i<dstW; i++) {
259 int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
260 int j;
261 (*filterPos)[i]= xx;
262 for (j=0; j<filterSize; j++) {
263 int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
264 double floatd;
265 int64_t coeff;
267 if (xInc > 1<<16)
268 d= d*dstW/srcW;
269 floatd= d * (1.0/(1<<30));
271 if (flags & SWS_BICUBIC) {
272 int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1<<24);
273 int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
274 int64_t dd = ( d*d)>>30;
275 int64_t ddd= (dd*d)>>30;
277 if (d < 1LL<<30)
278 coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
279 else if (d < 1LL<<31)
280 coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
281 else
282 coeff=0.0;
283 coeff *= fone>>(30+24);
285 /* else if (flags & SWS_X) {
286 double p= param ? param*0.01 : 0.3;
287 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
288 coeff*= pow(2.0, - p*d*d);
290 else if (flags & SWS_X) {
291 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
292 double c;
294 if (floatd<1.0)
295 c = cos(floatd*M_PI);
296 else
297 c=-1.0;
298 if (c<0.0) c= -pow(-c, A);
299 else c= pow( c, A);
300 coeff= (c*0.5 + 0.5)*fone;
301 } else if (flags & SWS_AREA) {
302 int64_t d2= d - (1<<29);
303 if (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
304 else if (d2*xInc < (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
305 else coeff=0.0;
306 coeff *= fone>>(30+16);
307 } else if (flags & SWS_GAUSS) {
308 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
309 coeff = (pow(2.0, - p*floatd*floatd))*fone;
310 } else if (flags & SWS_SINC) {
311 coeff = (d ? sin(floatd*M_PI)/(floatd*M_PI) : 1.0)*fone;
312 } else if (flags & SWS_LANCZOS) {
313 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
314 coeff = (d ? sin(floatd*M_PI)*sin(floatd*M_PI/p)/(floatd*floatd*M_PI*M_PI/p) : 1.0)*fone;
315 if (floatd>p) coeff=0;
316 } else if (flags & SWS_BILINEAR) {
317 coeff= (1<<30) - d;
318 if (coeff<0) coeff=0;
319 coeff *= fone >> 30;
320 } else if (flags & SWS_SPLINE) {
321 double p=-2.196152422706632;
322 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
323 } else {
324 coeff= 0.0; //GCC warning killer
325 assert(0);
328 filter[i*filterSize + j]= coeff;
329 xx++;
331 xDstInSrc+= 2*xInc;
335 /* apply src & dst Filter to filter -> filter2
336 av_free(filter);
338 assert(filterSize>0);
339 filter2Size= filterSize;
340 if (srcFilter) filter2Size+= srcFilter->length - 1;
341 if (dstFilter) filter2Size+= dstFilter->length - 1;
342 assert(filter2Size>0);
343 FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size*dstW*sizeof(*filter2), fail);
345 for (i=0; i<dstW; i++) {
346 int j, k;
348 if(srcFilter) {
349 for (k=0; k<srcFilter->length; k++) {
350 for (j=0; j<filterSize; j++)
351 filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
353 } else {
354 for (j=0; j<filterSize; j++)
355 filter2[i*filter2Size + j]= filter[i*filterSize + j];
357 //FIXME dstFilter
359 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
361 av_freep(&filter);
363 /* try to reduce the filter-size (step1 find size and shift left) */
364 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
365 minFilterSize= 0;
366 for (i=dstW-1; i>=0; i--) {
367 int min= filter2Size;
368 int j;
369 int64_t cutOff=0.0;
371 /* get rid of near zero elements on the left by shifting left */
372 for (j=0; j<filter2Size; j++) {
373 int k;
374 cutOff += FFABS(filter2[i*filter2Size]);
376 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
378 /* preserve monotonicity because the core can't handle the filter otherwise */
379 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
381 // move filter coefficients left
382 for (k=1; k<filter2Size; k++)
383 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
384 filter2[i*filter2Size + k - 1]= 0;
385 (*filterPos)[i]++;
388 cutOff=0;
389 /* count near zeros on the right */
390 for (j=filter2Size-1; j>0; j--) {
391 cutOff += FFABS(filter2[i*filter2Size + j]);
393 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
394 min--;
397 if (min>minFilterSize) minFilterSize= min;
400 if (flags & SWS_CPU_CAPS_ALTIVEC) {
401 // we can handle the special case 4,
402 // so we don't want to go to the full 8
403 if (minFilterSize < 5)
404 filterAlign = 4;
406 // We really don't want to waste our time
407 // doing useless computation, so fall back on
408 // the scalar C code for very small filters.
409 // Vectorizing is worth it only if you have a
410 // decent-sized vector.
411 if (minFilterSize < 3)
412 filterAlign = 1;
415 if (flags & SWS_CPU_CAPS_MMX) {
416 // special case for unscaled vertical filtering
417 if (minFilterSize == 1 && filterAlign == 2)
418 filterAlign= 1;
421 assert(minFilterSize > 0);
422 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
423 assert(filterSize > 0);
424 filter= av_malloc(filterSize*dstW*sizeof(*filter));
425 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
426 goto fail;
427 *outFilterSize= filterSize;
429 if (flags&SWS_PRINT_INFO)
430 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
431 /* try to reduce the filter-size (step2 reduce it) */
432 for (i=0; i<dstW; i++) {
433 int j;
435 for (j=0; j<filterSize; j++) {
436 if (j>=filter2Size) filter[i*filterSize + j]= 0;
437 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
438 if((flags & SWS_BITEXACT) && j>=minFilterSize)
439 filter[i*filterSize + j]= 0;
443 //FIXME try to align filterPos if possible
445 //fix borders
446 for (i=0; i<dstW; i++) {
447 int j;
448 if ((*filterPos)[i] < 0) {
449 // move filter coefficients left to compensate for filterPos
450 for (j=1; j<filterSize; j++) {
451 int left= FFMAX(j + (*filterPos)[i], 0);
452 filter[i*filterSize + left] += filter[i*filterSize + j];
453 filter[i*filterSize + j]=0;
455 (*filterPos)[i]= 0;
458 if ((*filterPos)[i] + filterSize > srcW) {
459 int shift= (*filterPos)[i] + filterSize - srcW;
460 // move filter coefficients right to compensate for filterPos
461 for (j=filterSize-2; j>=0; j--) {
462 int right= FFMIN(j + shift, filterSize-1);
463 filter[i*filterSize +right] += filter[i*filterSize +j];
464 filter[i*filterSize +j]=0;
466 (*filterPos)[i]= srcW - filterSize;
470 // Note the +1 is for the MMX scaler which reads over the end
471 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
472 FF_ALLOCZ_OR_GOTO(NULL, *outFilter, *outFilterSize*(dstW+1)*sizeof(int16_t), fail);
474 /* normalize & store in outFilter */
475 for (i=0; i<dstW; i++) {
476 int j;
477 int64_t error=0;
478 int64_t sum=0;
480 for (j=0; j<filterSize; j++) {
481 sum+= filter[i*filterSize + j];
483 sum= (sum + one/2)/ one;
484 for (j=0; j<*outFilterSize; j++) {
485 int64_t v= filter[i*filterSize + j] + error;
486 int intV= ROUNDED_DIV(v, sum);
487 (*outFilter)[i*(*outFilterSize) + j]= intV;
488 error= v - intV*sum;
492 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
493 for (i=0; i<*outFilterSize; i++) {
494 int j= dstW*(*outFilterSize);
495 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
498 ret=0;
499 fail:
500 av_free(filter);
501 av_free(filter2);
502 return ret;
505 #if ARCH_X86 && (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
506 static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode, int16_t *filter, int32_t *filterPos, int numSplits)
508 uint8_t *fragmentA;
509 x86_reg imm8OfPShufW1A;
510 x86_reg imm8OfPShufW2A;
511 x86_reg fragmentLengthA;
512 uint8_t *fragmentB;
513 x86_reg imm8OfPShufW1B;
514 x86_reg imm8OfPShufW2B;
515 x86_reg fragmentLengthB;
516 int fragmentPos;
518 int xpos, i;
520 // create an optimized horizontal scaling routine
521 /* This scaler is made of runtime-generated MMX2 code using specially
522 * tuned pshufw instructions. For every four output pixels, if four
523 * input pixels are enough for the fast bilinear scaling, then a chunk
524 * of fragmentB is used. If five input pixels are needed, then a chunk
525 * of fragmentA is used.
528 //code fragment
530 __asm__ volatile(
531 "jmp 9f \n\t"
532 // Begin
533 "0: \n\t"
534 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
535 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
536 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
537 "punpcklbw %%mm7, %%mm1 \n\t"
538 "punpcklbw %%mm7, %%mm0 \n\t"
539 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
540 "1: \n\t"
541 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
542 "2: \n\t"
543 "psubw %%mm1, %%mm0 \n\t"
544 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
545 "pmullw %%mm3, %%mm0 \n\t"
546 "psllw $7, %%mm1 \n\t"
547 "paddw %%mm1, %%mm0 \n\t"
549 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
551 "add $8, %%"REG_a" \n\t"
552 // End
553 "9: \n\t"
554 // "int $3 \n\t"
555 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
556 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
557 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
558 "dec %1 \n\t"
559 "dec %2 \n\t"
560 "sub %0, %1 \n\t"
561 "sub %0, %2 \n\t"
562 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
563 "sub %0, %3 \n\t"
566 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
567 "=r" (fragmentLengthA)
570 __asm__ volatile(
571 "jmp 9f \n\t"
572 // Begin
573 "0: \n\t"
574 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
575 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
576 "punpcklbw %%mm7, %%mm0 \n\t"
577 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
578 "1: \n\t"
579 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
580 "2: \n\t"
581 "psubw %%mm1, %%mm0 \n\t"
582 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
583 "pmullw %%mm3, %%mm0 \n\t"
584 "psllw $7, %%mm1 \n\t"
585 "paddw %%mm1, %%mm0 \n\t"
587 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
589 "add $8, %%"REG_a" \n\t"
590 // End
591 "9: \n\t"
592 // "int $3 \n\t"
593 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
594 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
595 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
596 "dec %1 \n\t"
597 "dec %2 \n\t"
598 "sub %0, %1 \n\t"
599 "sub %0, %2 \n\t"
600 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
601 "sub %0, %3 \n\t"
604 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
605 "=r" (fragmentLengthB)
608 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
609 fragmentPos=0;
611 for (i=0; i<dstW/numSplits; i++) {
612 int xx=xpos>>16;
614 if ((i&3) == 0) {
615 int a=0;
616 int b=((xpos+xInc)>>16) - xx;
617 int c=((xpos+xInc*2)>>16) - xx;
618 int d=((xpos+xInc*3)>>16) - xx;
619 int inc = (d+1<4);
620 uint8_t *fragment = (d+1<4) ? fragmentB : fragmentA;
621 x86_reg imm8OfPShufW1 = (d+1<4) ? imm8OfPShufW1B : imm8OfPShufW1A;
622 x86_reg imm8OfPShufW2 = (d+1<4) ? imm8OfPShufW2B : imm8OfPShufW2A;
623 x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
624 int maxShift= 3-(d+inc);
625 int shift=0;
627 if (filterCode) {
628 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
629 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
630 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
631 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
632 filterPos[i/2]= xx;
634 memcpy(filterCode + fragmentPos, fragment, fragmentLength);
636 filterCode[fragmentPos + imm8OfPShufW1]=
637 (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
638 filterCode[fragmentPos + imm8OfPShufW2]=
639 a | (b<<2) | (c<<4) | (d<<6);
641 if (i+4-inc>=dstW) shift=maxShift; //avoid overread
642 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
644 if (shift && i>=shift) {
645 filterCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
646 filterCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
647 filterPos[i/2]-=shift;
651 fragmentPos+= fragmentLength;
653 if (filterCode)
654 filterCode[fragmentPos]= RET;
656 xpos+=xInc;
658 if (filterCode)
659 filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
661 return fragmentPos + 1;
663 #endif /* ARCH_X86 && (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL */
665 static void getSubSampleFactors(int *h, int *v, enum PixelFormat format)
667 *h = av_pix_fmt_descriptors[format].log2_chroma_w;
668 *v = av_pix_fmt_descriptors[format].log2_chroma_h;
671 static uint16_t roundToInt16(int64_t f)
673 int r= (f + (1<<15))>>16;
674 if (r<-0x7FFF) return 0x8000;
675 else if (r> 0x7FFF) return 0x7FFF;
676 else return r;
679 int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation)
681 int64_t crv = inv_table[0];
682 int64_t cbu = inv_table[1];
683 int64_t cgu = -inv_table[2];
684 int64_t cgv = -inv_table[3];
685 int64_t cy = 1<<16;
686 int64_t oy = 0;
688 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
689 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
691 c->brightness= brightness;
692 c->contrast = contrast;
693 c->saturation= saturation;
694 c->srcRange = srcRange;
695 c->dstRange = dstRange;
696 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
698 c->uOffset= 0x0400040004000400LL;
699 c->vOffset= 0x0400040004000400LL;
701 if (!srcRange) {
702 cy= (cy*255) / 219;
703 oy= 16<<16;
704 } else {
705 crv= (crv*224) / 255;
706 cbu= (cbu*224) / 255;
707 cgu= (cgu*224) / 255;
708 cgv= (cgv*224) / 255;
711 cy = (cy *contrast )>>16;
712 crv= (crv*contrast * saturation)>>32;
713 cbu= (cbu*contrast * saturation)>>32;
714 cgu= (cgu*contrast * saturation)>>32;
715 cgv= (cgv*contrast * saturation)>>32;
717 oy -= 256*brightness;
719 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
720 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
721 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
722 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
723 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
724 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
726 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
727 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
728 c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
729 c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
730 c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
731 c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
733 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
734 //FIXME factorize
736 #if ARCH_PPC && (HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT)
737 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
738 ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
739 #endif
740 return 0;
743 int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation)
745 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
747 *inv_table = c->srcColorspaceTable;
748 *table = c->dstColorspaceTable;
749 *srcRange = c->srcRange;
750 *dstRange = c->dstRange;
751 *brightness= c->brightness;
752 *contrast = c->contrast;
753 *saturation= c->saturation;
755 return 0;
758 static int handle_jpeg(enum PixelFormat *format)
760 switch (*format) {
761 case PIX_FMT_YUVJ420P:
762 *format = PIX_FMT_YUV420P;
763 return 1;
764 case PIX_FMT_YUVJ422P:
765 *format = PIX_FMT_YUV422P;
766 return 1;
767 case PIX_FMT_YUVJ444P:
768 *format = PIX_FMT_YUV444P;
769 return 1;
770 case PIX_FMT_YUVJ440P:
771 *format = PIX_FMT_YUV440P;
772 return 1;
773 default:
774 return 0;
778 SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat,
779 int dstW, int dstH, enum PixelFormat dstFormat, int flags,
780 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
783 SwsContext *c;
784 int i;
785 int usesVFilter, usesHFilter;
786 int unscaled;
787 int srcRange, dstRange;
788 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
789 #if ARCH_X86
790 if (flags & SWS_CPU_CAPS_MMX)
791 __asm__ volatile("emms\n\t"::: "memory");
792 #endif
794 #if !CONFIG_RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
795 flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
796 flags |= ff_hardcodedcpuflags();
797 #endif /* CONFIG_RUNTIME_CPUDETECT */
798 if (!rgb15to16) sws_rgb2rgb_init(flags);
800 unscaled = (srcW == dstW && srcH == dstH);
802 srcRange = handle_jpeg(&srcFormat);
803 dstRange = handle_jpeg(&dstFormat);
805 if (!isSupportedIn(srcFormat)) {
806 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
807 return NULL;
809 if (!isSupportedOut(dstFormat)) {
810 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
811 return NULL;
814 i= flags & ( SWS_POINT
815 |SWS_AREA
816 |SWS_BILINEAR
817 |SWS_FAST_BILINEAR
818 |SWS_BICUBIC
819 |SWS_X
820 |SWS_GAUSS
821 |SWS_LANCZOS
822 |SWS_SINC
823 |SWS_SPLINE
824 |SWS_BICUBLIN);
825 if(!i || (i & (i-1))) {
826 av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
827 return NULL;
830 /* sanity check */
831 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
832 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
833 srcW, srcH, dstW, dstH);
834 return NULL;
836 if(srcW > VOFW || dstW > VOFW) {
837 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
838 return NULL;
841 if (!dstFilter) dstFilter= &dummyFilter;
842 if (!srcFilter) srcFilter= &dummyFilter;
844 FF_ALLOCZ_OR_GOTO(NULL, c, sizeof(SwsContext), fail);
846 c->av_class = &sws_context_class;
847 c->srcW= srcW;
848 c->srcH= srcH;
849 c->dstW= dstW;
850 c->dstH= dstH;
851 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
852 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
853 c->flags= flags;
854 c->dstFormat= dstFormat;
855 c->srcFormat= srcFormat;
856 c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[dstFormat]);
857 c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[srcFormat]);
858 c->vRounder= 4* 0x0001000100010001ULL;
860 usesHFilter= usesVFilter= 0;
861 if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
862 if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
863 if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
864 if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
865 if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
866 if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
867 if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
868 if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
870 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
871 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
873 // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
874 if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
876 // drop some chroma lines if the user wants it
877 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
878 c->chrSrcVSubSample+= c->vChrDrop;
880 // drop every other pixel for chroma calculation unless user wants full chroma
881 if (isAnyRGB(srcFormat) && !(flags&SWS_FULL_CHR_H_INP)
882 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
883 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
884 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
885 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
886 c->chrSrcHSubSample=1;
888 if (param) {
889 c->param[0] = param[0];
890 c->param[1] = param[1];
891 } else {
892 c->param[0] =
893 c->param[1] = SWS_PARAM_DEFAULT;
896 // Note the -((-x)>>y) is so that we always round toward +inf.
897 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
898 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
899 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
900 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
902 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
904 /* unscaled special cases */
905 if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isAnyRGB(dstFormat))) {
906 ff_get_unscaled_swscale(c);
908 if (c->swScale) {
909 if (flags&SWS_PRINT_INFO)
910 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
911 sws_format_name(srcFormat), sws_format_name(dstFormat));
912 return c;
916 if (flags & SWS_CPU_CAPS_MMX2) {
917 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
918 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR)) {
919 if (flags&SWS_PRINT_INFO)
920 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
922 if (usesHFilter) c->canMMX2BeUsed=0;
924 else
925 c->canMMX2BeUsed=0;
927 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
928 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
930 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
931 // but only for the FAST_BILINEAR mode otherwise do correct scaling
932 // n-2 is the last chrominance sample available
933 // this is not perfect, but no one should notice the difference, the more correct variant
934 // would be like the vertical one, but that would require some special code for the
935 // first and last pixel
936 if (flags&SWS_FAST_BILINEAR) {
937 if (c->canMMX2BeUsed) {
938 c->lumXInc+= 20;
939 c->chrXInc+= 20;
941 //we don't use the x86 asm scaler if MMX is available
942 else if (flags & SWS_CPU_CAPS_MMX) {
943 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
944 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
948 /* precalculate horizontal scaler filter coefficients */
950 #if ARCH_X86 && (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
951 // can't downscale !!!
952 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
953 c->lumMmx2FilterCodeSize = initMMX2HScaler( dstW, c->lumXInc, NULL, NULL, NULL, 8);
954 c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
956 #ifdef MAP_ANONYMOUS
957 c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
958 c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
959 #elif HAVE_VIRTUALALLOC
960 c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
961 c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
962 #else
963 c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
964 c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
965 #endif
967 FF_ALLOCZ_OR_GOTO(c, c->hLumFilter , (dstW /8+8)*sizeof(int16_t), fail);
968 FF_ALLOCZ_OR_GOTO(c, c->hChrFilter , (c->chrDstW /4+8)*sizeof(int16_t), fail);
969 FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW /2/8+8)*sizeof(int32_t), fail);
970 FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t), fail);
972 initMMX2HScaler( dstW, c->lumXInc, c->lumMmx2FilterCode, c->hLumFilter, c->hLumFilterPos, 8);
973 initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->hChrFilter, c->hChrFilterPos, 4);
975 #ifdef MAP_ANONYMOUS
976 mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
977 mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
978 #endif
979 } else
980 #endif /* ARCH_X86 && (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL */
982 const int filterAlign=
983 (flags & SWS_CPU_CAPS_MMX) ? 4 :
984 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
987 if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
988 srcW , dstW, filterAlign, 1<<14,
989 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
990 srcFilter->lumH, dstFilter->lumH, c->param) < 0)
991 goto fail;
992 if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
993 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
994 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
995 srcFilter->chrH, dstFilter->chrH, c->param) < 0)
996 goto fail;
998 } // initialize horizontal stuff
1000 /* precalculate vertical scaler filter coefficients */
1002 const int filterAlign=
1003 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
1004 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
1007 if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
1008 srcH , dstH, filterAlign, (1<<12),
1009 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
1010 srcFilter->lumV, dstFilter->lumV, c->param) < 0)
1011 goto fail;
1012 if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
1013 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
1014 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
1015 srcFilter->chrV, dstFilter->chrV, c->param) < 0)
1016 goto fail;
1018 #if ARCH_PPC && (HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT)
1019 FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH, fail);
1020 FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH, fail);
1022 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
1023 int j;
1024 short *p = (short *)&c->vYCoeffsBank[i];
1025 for (j=0;j<8;j++)
1026 p[j] = c->vLumFilter[i];
1029 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
1030 int j;
1031 short *p = (short *)&c->vCCoeffsBank[i];
1032 for (j=0;j<8;j++)
1033 p[j] = c->vChrFilter[i];
1035 #endif
1038 // calculate buffer sizes so that they won't run out while handling these damn slices
1039 c->vLumBufSize= c->vLumFilterSize;
1040 c->vChrBufSize= c->vChrFilterSize;
1041 for (i=0; i<dstH; i++) {
1042 int chrI= i*c->chrDstH / dstH;
1043 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
1044 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
1046 nextSlice>>= c->chrSrcVSubSample;
1047 nextSlice<<= c->chrSrcVSubSample;
1048 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
1049 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
1050 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
1051 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
1054 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
1055 // allocate several megabytes to handle all possible cases)
1056 FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
1057 FF_ALLOC_OR_GOTO(c, c->chrPixBuf, c->vChrBufSize*2*sizeof(int16_t*), fail);
1058 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
1059 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
1060 //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)
1061 /* align at 16 bytes for AltiVec */
1062 for (i=0; i<c->vLumBufSize; i++) {
1063 FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i+c->vLumBufSize], VOF+1, fail);
1064 c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
1066 for (i=0; i<c->vChrBufSize; i++) {
1067 FF_ALLOC_OR_GOTO(c, c->chrPixBuf[i+c->vChrBufSize], (VOF+1)*2, fail);
1068 c->chrPixBuf[i] = c->chrPixBuf[i+c->vChrBufSize];
1070 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1071 for (i=0; i<c->vLumBufSize; i++) {
1072 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i+c->vLumBufSize], VOF+1, fail);
1073 c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
1076 //try to avoid drawing green stuff between the right end and the stride end
1077 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
1079 assert(2*VOFW == VOF);
1081 assert(c->chrDstH <= dstH);
1083 if (flags&SWS_PRINT_INFO) {
1084 if (flags&SWS_FAST_BILINEAR)
1085 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
1086 else if (flags&SWS_BILINEAR)
1087 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
1088 else if (flags&SWS_BICUBIC)
1089 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
1090 else if (flags&SWS_X)
1091 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
1092 else if (flags&SWS_POINT)
1093 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
1094 else if (flags&SWS_AREA)
1095 av_log(c, AV_LOG_INFO, "Area Averaging scaler, ");
1096 else if (flags&SWS_BICUBLIN)
1097 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
1098 else if (flags&SWS_GAUSS)
1099 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
1100 else if (flags&SWS_SINC)
1101 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
1102 else if (flags&SWS_LANCZOS)
1103 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
1104 else if (flags&SWS_SPLINE)
1105 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
1106 else
1107 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
1109 av_log(c, AV_LOG_INFO, "from %s to %s%s ",
1110 sws_format_name(srcFormat),
1111 #ifdef DITHER1XBPP
1112 dstFormat == PIX_FMT_BGR555 || dstFormat == PIX_FMT_BGR565 ? "dithered " : "",
1113 #else
1115 #endif
1116 sws_format_name(dstFormat));
1118 if (flags & SWS_CPU_CAPS_MMX2)
1119 av_log(c, AV_LOG_INFO, "using MMX2\n");
1120 else if (flags & SWS_CPU_CAPS_3DNOW)
1121 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
1122 else if (flags & SWS_CPU_CAPS_MMX)
1123 av_log(c, AV_LOG_INFO, "using MMX\n");
1124 else if (flags & SWS_CPU_CAPS_ALTIVEC)
1125 av_log(c, AV_LOG_INFO, "using AltiVec\n");
1126 else
1127 av_log(c, AV_LOG_INFO, "using C\n");
1129 if (flags & SWS_CPU_CAPS_MMX) {
1130 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
1131 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
1132 else {
1133 if (c->hLumFilterSize==4)
1134 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
1135 else if (c->hLumFilterSize==8)
1136 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
1137 else
1138 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
1140 if (c->hChrFilterSize==4)
1141 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
1142 else if (c->hChrFilterSize==8)
1143 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
1144 else
1145 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
1147 } else {
1148 #if ARCH_X86
1149 av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
1150 #else
1151 if (flags & SWS_FAST_BILINEAR)
1152 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
1153 else
1154 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
1155 #endif
1157 if (isPlanarYUV(dstFormat)) {
1158 if (c->vLumFilterSize==1)
1159 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1160 else
1161 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1162 } else {
1163 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
1164 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
1165 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1166 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
1167 av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1168 else
1169 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1172 if (dstFormat==PIX_FMT_BGR24)
1173 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
1174 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
1175 else if (dstFormat==PIX_FMT_RGB32)
1176 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1177 else if (dstFormat==PIX_FMT_BGR565)
1178 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1179 else if (dstFormat==PIX_FMT_BGR555)
1180 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1182 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1183 av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1184 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
1185 av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1186 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
1189 c->swScale= ff_getSwsFunc(c);
1190 return c;
1192 fail:
1193 sws_freeContext(c);
1194 return NULL;
1197 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
1198 float lumaSharpen, float chromaSharpen,
1199 float chromaHShift, float chromaVShift,
1200 int verbose)
1202 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
1203 if (!filter)
1204 return NULL;
1206 if (lumaGBlur!=0.0) {
1207 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
1208 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
1209 } else {
1210 filter->lumH= sws_getIdentityVec();
1211 filter->lumV= sws_getIdentityVec();
1214 if (chromaGBlur!=0.0) {
1215 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
1216 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
1217 } else {
1218 filter->chrH= sws_getIdentityVec();
1219 filter->chrV= sws_getIdentityVec();
1222 if (chromaSharpen!=0.0) {
1223 SwsVector *id= sws_getIdentityVec();
1224 sws_scaleVec(filter->chrH, -chromaSharpen);
1225 sws_scaleVec(filter->chrV, -chromaSharpen);
1226 sws_addVec(filter->chrH, id);
1227 sws_addVec(filter->chrV, id);
1228 sws_freeVec(id);
1231 if (lumaSharpen!=0.0) {
1232 SwsVector *id= sws_getIdentityVec();
1233 sws_scaleVec(filter->lumH, -lumaSharpen);
1234 sws_scaleVec(filter->lumV, -lumaSharpen);
1235 sws_addVec(filter->lumH, id);
1236 sws_addVec(filter->lumV, id);
1237 sws_freeVec(id);
1240 if (chromaHShift != 0.0)
1241 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
1243 if (chromaVShift != 0.0)
1244 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
1246 sws_normalizeVec(filter->chrH, 1.0);
1247 sws_normalizeVec(filter->chrV, 1.0);
1248 sws_normalizeVec(filter->lumH, 1.0);
1249 sws_normalizeVec(filter->lumV, 1.0);
1251 if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
1252 if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
1254 return filter;
1257 SwsVector *sws_allocVec(int length)
1259 SwsVector *vec = av_malloc(sizeof(SwsVector));
1260 if (!vec)
1261 return NULL;
1262 vec->length = length;
1263 vec->coeff = av_malloc(sizeof(double) * length);
1264 if (!vec->coeff)
1265 av_freep(&vec);
1266 return vec;
1269 SwsVector *sws_getGaussianVec(double variance, double quality)
1271 const int length= (int)(variance*quality + 0.5) | 1;
1272 int i;
1273 double middle= (length-1)*0.5;
1274 SwsVector *vec= sws_allocVec(length);
1276 if (!vec)
1277 return NULL;
1279 for (i=0; i<length; i++) {
1280 double dist= i-middle;
1281 vec->coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*M_PI);
1284 sws_normalizeVec(vec, 1.0);
1286 return vec;
1289 SwsVector *sws_getConstVec(double c, int length)
1291 int i;
1292 SwsVector *vec= sws_allocVec(length);
1294 if (!vec)
1295 return NULL;
1297 for (i=0; i<length; i++)
1298 vec->coeff[i]= c;
1300 return vec;
1303 SwsVector *sws_getIdentityVec(void)
1305 return sws_getConstVec(1.0, 1);
1308 double sws_dcVec(SwsVector *a)
1310 int i;
1311 double sum=0;
1313 for (i=0; i<a->length; i++)
1314 sum+= a->coeff[i];
1316 return sum;
1319 void sws_scaleVec(SwsVector *a, double scalar)
1321 int i;
1323 for (i=0; i<a->length; i++)
1324 a->coeff[i]*= scalar;
1327 void sws_normalizeVec(SwsVector *a, double height)
1329 sws_scaleVec(a, height/sws_dcVec(a));
1332 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
1334 int length= a->length + b->length - 1;
1335 int i, j;
1336 SwsVector *vec= sws_getConstVec(0.0, length);
1338 if (!vec)
1339 return NULL;
1341 for (i=0; i<a->length; i++) {
1342 for (j=0; j<b->length; j++) {
1343 vec->coeff[i+j]+= a->coeff[i]*b->coeff[j];
1347 return vec;
1350 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
1352 int length= FFMAX(a->length, b->length);
1353 int i;
1354 SwsVector *vec= sws_getConstVec(0.0, length);
1356 if (!vec)
1357 return NULL;
1359 for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
1360 for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
1362 return vec;
1365 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
1367 int length= FFMAX(a->length, b->length);
1368 int i;
1369 SwsVector *vec= sws_getConstVec(0.0, length);
1371 if (!vec)
1372 return NULL;
1374 for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
1375 for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
1377 return vec;
1380 /* shift left / or right if "shift" is negative */
1381 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
1383 int length= a->length + FFABS(shift)*2;
1384 int i;
1385 SwsVector *vec= sws_getConstVec(0.0, length);
1387 if (!vec)
1388 return NULL;
1390 for (i=0; i<a->length; i++) {
1391 vec->coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
1394 return vec;
1397 void sws_shiftVec(SwsVector *a, int shift)
1399 SwsVector *shifted= sws_getShiftedVec(a, shift);
1400 av_free(a->coeff);
1401 a->coeff= shifted->coeff;
1402 a->length= shifted->length;
1403 av_free(shifted);
1406 void sws_addVec(SwsVector *a, SwsVector *b)
1408 SwsVector *sum= sws_sumVec(a, b);
1409 av_free(a->coeff);
1410 a->coeff= sum->coeff;
1411 a->length= sum->length;
1412 av_free(sum);
1415 void sws_subVec(SwsVector *a, SwsVector *b)
1417 SwsVector *diff= sws_diffVec(a, b);
1418 av_free(a->coeff);
1419 a->coeff= diff->coeff;
1420 a->length= diff->length;
1421 av_free(diff);
1424 void sws_convVec(SwsVector *a, SwsVector *b)
1426 SwsVector *conv= sws_getConvVec(a, b);
1427 av_free(a->coeff);
1428 a->coeff= conv->coeff;
1429 a->length= conv->length;
1430 av_free(conv);
1433 SwsVector *sws_cloneVec(SwsVector *a)
1435 int i;
1436 SwsVector *vec= sws_allocVec(a->length);
1438 if (!vec)
1439 return NULL;
1441 for (i=0; i<a->length; i++) vec->coeff[i]= a->coeff[i];
1443 return vec;
1446 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
1448 int i;
1449 double max=0;
1450 double min=0;
1451 double range;
1453 for (i=0; i<a->length; i++)
1454 if (a->coeff[i]>max) max= a->coeff[i];
1456 for (i=0; i<a->length; i++)
1457 if (a->coeff[i]<min) min= a->coeff[i];
1459 range= max - min;
1461 for (i=0; i<a->length; i++) {
1462 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
1463 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
1464 for (;x>0; x--) av_log(log_ctx, log_level, " ");
1465 av_log(log_ctx, log_level, "|\n");
1469 #if LIBSWSCALE_VERSION_MAJOR < 1
1470 void sws_printVec(SwsVector *a)
1472 sws_printVec2(a, NULL, AV_LOG_DEBUG);
1474 #endif
1476 void sws_freeVec(SwsVector *a)
1478 if (!a) return;
1479 av_freep(&a->coeff);
1480 a->length=0;
1481 av_free(a);
1484 void sws_freeFilter(SwsFilter *filter)
1486 if (!filter) return;
1488 if (filter->lumH) sws_freeVec(filter->lumH);
1489 if (filter->lumV) sws_freeVec(filter->lumV);
1490 if (filter->chrH) sws_freeVec(filter->chrH);
1491 if (filter->chrV) sws_freeVec(filter->chrV);
1492 av_free(filter);
1495 void sws_freeContext(SwsContext *c)
1497 int i;
1498 if (!c) return;
1500 if (c->lumPixBuf) {
1501 for (i=0; i<c->vLumBufSize; i++)
1502 av_freep(&c->lumPixBuf[i]);
1503 av_freep(&c->lumPixBuf);
1506 if (c->chrPixBuf) {
1507 for (i=0; i<c->vChrBufSize; i++)
1508 av_freep(&c->chrPixBuf[i]);
1509 av_freep(&c->chrPixBuf);
1512 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1513 for (i=0; i<c->vLumBufSize; i++)
1514 av_freep(&c->alpPixBuf[i]);
1515 av_freep(&c->alpPixBuf);
1518 av_freep(&c->vLumFilter);
1519 av_freep(&c->vChrFilter);
1520 av_freep(&c->hLumFilter);
1521 av_freep(&c->hChrFilter);
1522 #if ARCH_PPC && (HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT)
1523 av_freep(&c->vYCoeffsBank);
1524 av_freep(&c->vCCoeffsBank);
1525 #endif
1527 av_freep(&c->vLumFilterPos);
1528 av_freep(&c->vChrFilterPos);
1529 av_freep(&c->hLumFilterPos);
1530 av_freep(&c->hChrFilterPos);
1532 #if ARCH_X86 && CONFIG_GPL
1533 #ifdef MAP_ANONYMOUS
1534 if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
1535 if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
1536 #elif HAVE_VIRTUALALLOC
1537 if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, MEM_RELEASE);
1538 if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, MEM_RELEASE);
1539 #else
1540 av_free(c->lumMmx2FilterCode);
1541 av_free(c->chrMmx2FilterCode);
1542 #endif
1543 c->lumMmx2FilterCode=NULL;
1544 c->chrMmx2FilterCode=NULL;
1545 #endif /* ARCH_X86 && CONFIG_GPL */
1547 av_freep(&c->yuvTable);
1549 av_free(c);
1552 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
1553 int srcW, int srcH, enum PixelFormat srcFormat,
1554 int dstW, int dstH, enum PixelFormat dstFormat, int flags,
1555 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
1557 static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
1559 if (!param)
1560 param = default_param;
1562 if (context) {
1563 if (context->srcW != srcW || context->srcH != srcH ||
1564 context->srcFormat != srcFormat ||
1565 context->dstW != dstW || context->dstH != dstH ||
1566 context->dstFormat != dstFormat || context->flags != flags ||
1567 context->param[0] != param[0] || context->param[1] != param[1])
1569 sws_freeContext(context);
1570 context = NULL;
1573 if (!context) {
1574 return sws_getContext(srcW, srcH, srcFormat,
1575 dstW, dstH, dstFormat, flags,
1576 srcFilter, dstFilter, param);
1578 return context;