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