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