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