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