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