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