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