FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
utils.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "config.h"
22 
23 #define _SVID_SOURCE // needed for MAP_ANONYMOUS
24 #define _DARWIN_C_SOURCE // needed for MAP_ANON
25 #include <inttypes.h>
26 #include <math.h>
27 #include <stdio.h>
28 #include <string.h>
29 #if HAVE_SYS_MMAN_H
30 #include <sys/mman.h>
31 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
32 #define MAP_ANONYMOUS MAP_ANON
33 #endif
34 #endif
35 #if HAVE_VIRTUALALLOC
36 #define WIN32_LEAN_AND_MEAN
37 #include <windows.h>
38 #endif
39 
40 #include "libavutil/attributes.h"
41 #include "libavutil/avassert.h"
42 #include "libavutil/avutil.h"
43 #include "libavutil/bswap.h"
44 #include "libavutil/cpu.h"
45 #include "libavutil/intreadwrite.h"
46 #include "libavutil/mathematics.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/pixdesc.h"
49 #include "libavutil/x86/asm.h"
50 #include "libavutil/x86/cpu.h"
51 #include "rgb2rgb.h"
52 #include "swscale.h"
53 #include "swscale_internal.h"
54 
55 unsigned swscale_version(void)
56 {
59 }
60 
61 const char *swscale_configuration(void)
62 {
63  return FFMPEG_CONFIGURATION;
64 }
65 
66 const char *swscale_license(void)
67 {
68 #define LICENSE_PREFIX "libswscale license: "
69  return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
70 }
71 
72 #define RET 0xC3 // near return opcode for x86
73 
74 typedef struct FormatEntry {
78 } FormatEntry;
79 
81  [AV_PIX_FMT_YUV420P] = { 1, 1 },
82  [AV_PIX_FMT_YUYV422] = { 1, 1 },
83  [AV_PIX_FMT_RGB24] = { 1, 1 },
84  [AV_PIX_FMT_BGR24] = { 1, 1 },
85  [AV_PIX_FMT_YUV422P] = { 1, 1 },
86  [AV_PIX_FMT_YUV444P] = { 1, 1 },
87  [AV_PIX_FMT_YUV410P] = { 1, 1 },
88  [AV_PIX_FMT_YUV411P] = { 1, 1 },
89  [AV_PIX_FMT_GRAY8] = { 1, 1 },
90  [AV_PIX_FMT_MONOWHITE] = { 1, 1 },
91  [AV_PIX_FMT_MONOBLACK] = { 1, 1 },
92  [AV_PIX_FMT_PAL8] = { 1, 0 },
93  [AV_PIX_FMT_YUVJ420P] = { 1, 1 },
94  [AV_PIX_FMT_YUVJ411P] = { 1, 1 },
95  [AV_PIX_FMT_YUVJ422P] = { 1, 1 },
96  [AV_PIX_FMT_YUVJ444P] = { 1, 1 },
97  [AV_PIX_FMT_UYVY422] = { 1, 1 },
98  [AV_PIX_FMT_UYYVYY411] = { 0, 0 },
99  [AV_PIX_FMT_BGR8] = { 1, 1 },
100  [AV_PIX_FMT_BGR4] = { 0, 1 },
101  [AV_PIX_FMT_BGR4_BYTE] = { 1, 1 },
102  [AV_PIX_FMT_RGB8] = { 1, 1 },
103  [AV_PIX_FMT_RGB4] = { 0, 1 },
104  [AV_PIX_FMT_RGB4_BYTE] = { 1, 1 },
105  [AV_PIX_FMT_NV12] = { 1, 1 },
106  [AV_PIX_FMT_NV21] = { 1, 1 },
107  [AV_PIX_FMT_ARGB] = { 1, 1 },
108  [AV_PIX_FMT_RGBA] = { 1, 1 },
109  [AV_PIX_FMT_ABGR] = { 1, 1 },
110  [AV_PIX_FMT_BGRA] = { 1, 1 },
111  [AV_PIX_FMT_0RGB] = { 1, 1 },
112  [AV_PIX_FMT_RGB0] = { 1, 1 },
113  [AV_PIX_FMT_0BGR] = { 1, 1 },
114  [AV_PIX_FMT_BGR0] = { 1, 1 },
115  [AV_PIX_FMT_GRAY16BE] = { 1, 1 },
116  [AV_PIX_FMT_GRAY16LE] = { 1, 1 },
117  [AV_PIX_FMT_YUV440P] = { 1, 1 },
118  [AV_PIX_FMT_YUVJ440P] = { 1, 1 },
119  [AV_PIX_FMT_YUVA420P] = { 1, 1 },
120  [AV_PIX_FMT_YUVA422P] = { 1, 1 },
121  [AV_PIX_FMT_YUVA444P] = { 1, 1 },
122  [AV_PIX_FMT_YUVA420P9BE] = { 1, 1 },
123  [AV_PIX_FMT_YUVA420P9LE] = { 1, 1 },
124  [AV_PIX_FMT_YUVA422P9BE] = { 1, 1 },
125  [AV_PIX_FMT_YUVA422P9LE] = { 1, 1 },
126  [AV_PIX_FMT_YUVA444P9BE] = { 1, 1 },
127  [AV_PIX_FMT_YUVA444P9LE] = { 1, 1 },
128  [AV_PIX_FMT_YUVA420P10BE]= { 1, 1 },
129  [AV_PIX_FMT_YUVA420P10LE]= { 1, 1 },
130  [AV_PIX_FMT_YUVA422P10BE]= { 1, 1 },
131  [AV_PIX_FMT_YUVA422P10LE]= { 1, 1 },
132  [AV_PIX_FMT_YUVA444P10BE]= { 1, 1 },
133  [AV_PIX_FMT_YUVA444P10LE]= { 1, 1 },
134  [AV_PIX_FMT_YUVA420P16BE]= { 1, 1 },
135  [AV_PIX_FMT_YUVA420P16LE]= { 1, 1 },
136  [AV_PIX_FMT_YUVA422P16BE]= { 1, 1 },
137  [AV_PIX_FMT_YUVA422P16LE]= { 1, 1 },
138  [AV_PIX_FMT_YUVA444P16BE]= { 1, 1 },
139  [AV_PIX_FMT_YUVA444P16LE]= { 1, 1 },
140  [AV_PIX_FMT_RGB48BE] = { 1, 1 },
141  [AV_PIX_FMT_RGB48LE] = { 1, 1 },
142  [AV_PIX_FMT_RGBA64BE] = { 1, 1 },
143  [AV_PIX_FMT_RGBA64LE] = { 1, 1 },
144  [AV_PIX_FMT_RGB565BE] = { 1, 1 },
145  [AV_PIX_FMT_RGB565LE] = { 1, 1 },
146  [AV_PIX_FMT_RGB555BE] = { 1, 1 },
147  [AV_PIX_FMT_RGB555LE] = { 1, 1 },
148  [AV_PIX_FMT_BGR565BE] = { 1, 1 },
149  [AV_PIX_FMT_BGR565LE] = { 1, 1 },
150  [AV_PIX_FMT_BGR555BE] = { 1, 1 },
151  [AV_PIX_FMT_BGR555LE] = { 1, 1 },
152  [AV_PIX_FMT_YUV420P16LE] = { 1, 1 },
153  [AV_PIX_FMT_YUV420P16BE] = { 1, 1 },
154  [AV_PIX_FMT_YUV422P16LE] = { 1, 1 },
155  [AV_PIX_FMT_YUV422P16BE] = { 1, 1 },
156  [AV_PIX_FMT_YUV444P16LE] = { 1, 1 },
157  [AV_PIX_FMT_YUV444P16BE] = { 1, 1 },
158  [AV_PIX_FMT_RGB444LE] = { 1, 1 },
159  [AV_PIX_FMT_RGB444BE] = { 1, 1 },
160  [AV_PIX_FMT_BGR444LE] = { 1, 1 },
161  [AV_PIX_FMT_BGR444BE] = { 1, 1 },
162  [AV_PIX_FMT_Y400A] = { 1, 0 },
163  [AV_PIX_FMT_BGR48BE] = { 1, 1 },
164  [AV_PIX_FMT_BGR48LE] = { 1, 1 },
165  [AV_PIX_FMT_BGRA64BE] = { 0, 0 },
166  [AV_PIX_FMT_BGRA64LE] = { 0, 0 },
167  [AV_PIX_FMT_YUV420P9BE] = { 1, 1 },
168  [AV_PIX_FMT_YUV420P9LE] = { 1, 1 },
169  [AV_PIX_FMT_YUV420P10BE] = { 1, 1 },
170  [AV_PIX_FMT_YUV420P10LE] = { 1, 1 },
171  [AV_PIX_FMT_YUV420P12BE] = { 1, 1 },
172  [AV_PIX_FMT_YUV420P12LE] = { 1, 1 },
173  [AV_PIX_FMT_YUV420P14BE] = { 1, 1 },
174  [AV_PIX_FMT_YUV420P14LE] = { 1, 1 },
175  [AV_PIX_FMT_YUV422P9BE] = { 1, 1 },
176  [AV_PIX_FMT_YUV422P9LE] = { 1, 1 },
177  [AV_PIX_FMT_YUV422P10BE] = { 1, 1 },
178  [AV_PIX_FMT_YUV422P10LE] = { 1, 1 },
179  [AV_PIX_FMT_YUV422P12BE] = { 1, 1 },
180  [AV_PIX_FMT_YUV422P12LE] = { 1, 1 },
181  [AV_PIX_FMT_YUV422P14BE] = { 1, 1 },
182  [AV_PIX_FMT_YUV422P14LE] = { 1, 1 },
183  [AV_PIX_FMT_YUV444P9BE] = { 1, 1 },
184  [AV_PIX_FMT_YUV444P9LE] = { 1, 1 },
185  [AV_PIX_FMT_YUV444P10BE] = { 1, 1 },
186  [AV_PIX_FMT_YUV444P10LE] = { 1, 1 },
187  [AV_PIX_FMT_YUV444P12BE] = { 1, 1 },
188  [AV_PIX_FMT_YUV444P12LE] = { 1, 1 },
189  [AV_PIX_FMT_YUV444P14BE] = { 1, 1 },
190  [AV_PIX_FMT_YUV444P14LE] = { 1, 1 },
191  [AV_PIX_FMT_GBRP] = { 1, 1 },
192  [AV_PIX_FMT_GBRP9LE] = { 1, 1 },
193  [AV_PIX_FMT_GBRP9BE] = { 1, 1 },
194  [AV_PIX_FMT_GBRP10LE] = { 1, 1 },
195  [AV_PIX_FMT_GBRP10BE] = { 1, 1 },
196  [AV_PIX_FMT_GBRP12LE] = { 1, 1 },
197  [AV_PIX_FMT_GBRP12BE] = { 1, 1 },
198  [AV_PIX_FMT_GBRP14LE] = { 1, 1 },
199  [AV_PIX_FMT_GBRP14BE] = { 1, 1 },
200  [AV_PIX_FMT_GBRP16LE] = { 1, 0 },
201  [AV_PIX_FMT_GBRP16BE] = { 1, 0 },
202  [AV_PIX_FMT_XYZ12BE] = { 1, 0, 1 },
203  [AV_PIX_FMT_XYZ12LE] = { 1, 0, 1 },
204  [AV_PIX_FMT_GBRAP] = { 1, 1 },
205  [AV_PIX_FMT_GBRAP16LE] = { 1, 0 },
206  [AV_PIX_FMT_GBRAP16BE] = { 1, 0 },
207 };
208 
210 {
211  return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
212  format_entries[pix_fmt].is_supported_in : 0;
213 }
214 
216 {
217  return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
218  format_entries[pix_fmt].is_supported_out : 0;
219 }
220 
222 {
223  return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
224  format_entries[pix_fmt].is_supported_endianness : 0;
225 }
226 
227 extern const int32_t ff_yuv2rgb_coeffs[8][4];
228 
229 #if FF_API_SWS_FORMAT_NAME
230 const char *sws_format_name(enum AVPixelFormat format)
231 {
232  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
233  if (desc)
234  return desc->name;
235  else
236  return "Unknown format";
237 }
238 #endif
239 
240 static double getSplineCoeff(double a, double b, double c, double d,
241  double dist)
242 {
243  if (dist <= 1.0)
244  return ((d * dist + c) * dist + b) * dist + a;
245  else
246  return getSplineCoeff(0.0,
247  b + 2.0 * c + 3.0 * d,
248  c + 3.0 * d,
249  -b - 3.0 * c - 6.0 * d,
250  dist - 1.0);
251 }
252 
253 static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
254  int *outFilterSize, int xInc, int srcW,
255  int dstW, int filterAlign, int one,
256  int flags, int cpu_flags,
257  SwsVector *srcFilter, SwsVector *dstFilter,
258  double param[2])
259 {
260  int i;
261  int filterSize;
262  int filter2Size;
263  int minFilterSize;
264  int64_t *filter = NULL;
265  int64_t *filter2 = NULL;
266  const int64_t fone = 1LL << (54 - FFMIN(av_log2(srcW/dstW), 8));
267  int ret = -1;
268 
269  emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
270 
271  // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
272  FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW + 3) * sizeof(**filterPos), fail);
273 
274  if (FFABS(xInc - 0x10000) < 10) { // unscaled
275  int i;
276  filterSize = 1;
277  FF_ALLOCZ_OR_GOTO(NULL, filter,
278  dstW * sizeof(*filter) * filterSize, fail);
279 
280  for (i = 0; i < dstW; i++) {
281  filter[i * filterSize] = fone;
282  (*filterPos)[i] = i;
283  }
284  } else if (flags & SWS_POINT) { // lame looking point sampling mode
285  int i;
286  int64_t xDstInSrc;
287  filterSize = 1;
288  FF_ALLOC_OR_GOTO(NULL, filter,
289  dstW * sizeof(*filter) * filterSize, fail);
290 
291  xDstInSrc = xInc / 2 - 0x8000;
292  for (i = 0; i < dstW; i++) {
293  int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
294 
295  (*filterPos)[i] = xx;
296  filter[i] = fone;
297  xDstInSrc += xInc;
298  }
299  } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
300  (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
301  int i;
302  int64_t xDstInSrc;
303  filterSize = 2;
304  FF_ALLOC_OR_GOTO(NULL, filter,
305  dstW * sizeof(*filter) * filterSize, fail);
306 
307  xDstInSrc = xInc / 2 - 0x8000;
308  for (i = 0; i < dstW; i++) {
309  int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
310  int j;
311 
312  (*filterPos)[i] = xx;
313  // bilinear upscale / linear interpolate / area averaging
314  for (j = 0; j < filterSize; j++) {
315  int64_t coeff= fone - FFABS(((int64_t)xx<<16) - xDstInSrc)*(fone>>16);
316  if (coeff < 0)
317  coeff = 0;
318  filter[i * filterSize + j] = coeff;
319  xx++;
320  }
321  xDstInSrc += xInc;
322  }
323  } else {
324  int64_t xDstInSrc;
325  int sizeFactor;
326 
327  if (flags & SWS_BICUBIC)
328  sizeFactor = 4;
329  else if (flags & SWS_X)
330  sizeFactor = 8;
331  else if (flags & SWS_AREA)
332  sizeFactor = 1; // downscale only, for upscale it is bilinear
333  else if (flags & SWS_GAUSS)
334  sizeFactor = 8; // infinite ;)
335  else if (flags & SWS_LANCZOS)
336  sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
337  else if (flags & SWS_SINC)
338  sizeFactor = 20; // infinite ;)
339  else if (flags & SWS_SPLINE)
340  sizeFactor = 20; // infinite ;)
341  else if (flags & SWS_BILINEAR)
342  sizeFactor = 2;
343  else {
344  av_assert0(0);
345  }
346 
347  if (xInc <= 1 << 16)
348  filterSize = 1 + sizeFactor; // upscale
349  else
350  filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
351 
352  filterSize = FFMIN(filterSize, srcW - 2);
353  filterSize = FFMAX(filterSize, 1);
354 
355  FF_ALLOC_OR_GOTO(NULL, filter,
356  dstW * sizeof(*filter) * filterSize, fail);
357 
358  xDstInSrc = xInc - 0x10000;
359  for (i = 0; i < dstW; i++) {
360  int xx = (xDstInSrc - ((filterSize - 2) << 16)) / (1 << 17);
361  int j;
362  (*filterPos)[i] = xx;
363  for (j = 0; j < filterSize; j++) {
364  int64_t d = (FFABS(((int64_t)xx << 17) - xDstInSrc)) << 13;
365  double floatd;
366  int64_t coeff;
367 
368  if (xInc > 1 << 16)
369  d = d * dstW / srcW;
370  floatd = d * (1.0 / (1 << 30));
371 
372  if (flags & SWS_BICUBIC) {
373  int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1 << 24);
374  int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
375 
376  if (d >= 1LL << 31) {
377  coeff = 0.0;
378  } else {
379  int64_t dd = (d * d) >> 30;
380  int64_t ddd = (dd * d) >> 30;
381 
382  if (d < 1LL << 30)
383  coeff = (12 * (1 << 24) - 9 * B - 6 * C) * ddd +
384  (-18 * (1 << 24) + 12 * B + 6 * C) * dd +
385  (6 * (1 << 24) - 2 * B) * (1 << 30);
386  else
387  coeff = (-B - 6 * C) * ddd +
388  (6 * B + 30 * C) * dd +
389  (-12 * B - 48 * C) * d +
390  (8 * B + 24 * C) * (1 << 30);
391  }
392  coeff /= (1LL<<54)/fone;
393  }
394 #if 0
395  else if (flags & SWS_X) {
396  double p = param ? param * 0.01 : 0.3;
397  coeff = d ? sin(d * M_PI) / (d * M_PI) : 1.0;
398  coeff *= pow(2.0, -p * d * d);
399  }
400 #endif
401  else if (flags & SWS_X) {
402  double A = param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
403  double c;
404 
405  if (floatd < 1.0)
406  c = cos(floatd * M_PI);
407  else
408  c = -1.0;
409  if (c < 0.0)
410  c = -pow(-c, A);
411  else
412  c = pow(c, A);
413  coeff = (c * 0.5 + 0.5) * fone;
414  } else if (flags & SWS_AREA) {
415  int64_t d2 = d - (1 << 29);
416  if (d2 * xInc < -(1LL << (29 + 16)))
417  coeff = 1.0 * (1LL << (30 + 16));
418  else if (d2 * xInc < (1LL << (29 + 16)))
419  coeff = -d2 * xInc + (1LL << (29 + 16));
420  else
421  coeff = 0.0;
422  coeff *= fone >> (30 + 16);
423  } else if (flags & SWS_GAUSS) {
424  double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
425  coeff = (pow(2.0, -p * floatd * floatd)) * fone;
426  } else if (flags & SWS_SINC) {
427  coeff = (d ? sin(floatd * M_PI) / (floatd * M_PI) : 1.0) * fone;
428  } else if (flags & SWS_LANCZOS) {
429  double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
430  coeff = (d ? sin(floatd * M_PI) * sin(floatd * M_PI / p) /
431  (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
432  if (floatd > p)
433  coeff = 0;
434  } else if (flags & SWS_BILINEAR) {
435  coeff = (1 << 30) - d;
436  if (coeff < 0)
437  coeff = 0;
438  coeff *= fone >> 30;
439  } else if (flags & SWS_SPLINE) {
440  double p = -2.196152422706632;
441  coeff = getSplineCoeff(1.0, 0.0, p, -p - 1.0, floatd) * fone;
442  } else {
443  av_assert0(0);
444  }
445 
446  filter[i * filterSize + j] = coeff;
447  xx++;
448  }
449  xDstInSrc += 2 * xInc;
450  }
451  }
452 
453  /* apply src & dst Filter to filter -> filter2
454  * av_free(filter);
455  */
456  av_assert0(filterSize > 0);
457  filter2Size = filterSize;
458  if (srcFilter)
459  filter2Size += srcFilter->length - 1;
460  if (dstFilter)
461  filter2Size += dstFilter->length - 1;
462  av_assert0(filter2Size > 0);
463  FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size * dstW * sizeof(*filter2), fail);
464 
465  for (i = 0; i < dstW; i++) {
466  int j, k;
467 
468  if (srcFilter) {
469  for (k = 0; k < srcFilter->length; k++) {
470  for (j = 0; j < filterSize; j++)
471  filter2[i * filter2Size + k + j] +=
472  srcFilter->coeff[k] * filter[i * filterSize + j];
473  }
474  } else {
475  for (j = 0; j < filterSize; j++)
476  filter2[i * filter2Size + j] = filter[i * filterSize + j];
477  }
478  // FIXME dstFilter
479 
480  (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
481  }
482  av_freep(&filter);
483 
484  /* try to reduce the filter-size (step1 find size and shift left) */
485  // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
486  minFilterSize = 0;
487  for (i = dstW - 1; i >= 0; i--) {
488  int min = filter2Size;
489  int j;
490  int64_t cutOff = 0.0;
491 
492  /* get rid of near zero elements on the left by shifting left */
493  for (j = 0; j < filter2Size; j++) {
494  int k;
495  cutOff += FFABS(filter2[i * filter2Size]);
496 
497  if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
498  break;
499 
500  /* preserve monotonicity because the core can't handle the
501  * filter otherwise */
502  if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
503  break;
504 
505  // move filter coefficients left
506  for (k = 1; k < filter2Size; k++)
507  filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
508  filter2[i * filter2Size + k - 1] = 0;
509  (*filterPos)[i]++;
510  }
511 
512  cutOff = 0;
513  /* count near zeros on the right */
514  for (j = filter2Size - 1; j > 0; j--) {
515  cutOff += FFABS(filter2[i * filter2Size + j]);
516 
517  if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
518  break;
519  min--;
520  }
521 
522  if (min > minFilterSize)
523  minFilterSize = min;
524  }
525 
526  if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) {
527  // we can handle the special case 4, so we don't want to go the full 8
528  if (minFilterSize < 5)
529  filterAlign = 4;
530 
531  /* We really don't want to waste our time doing useless computation, so
532  * fall back on the scalar C code for very small filters.
533  * Vectorizing is worth it only if you have a decent-sized vector. */
534  if (minFilterSize < 3)
535  filterAlign = 1;
536  }
537 
538  if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
539  // special case for unscaled vertical filtering
540  if (minFilterSize == 1 && filterAlign == 2)
541  filterAlign = 1;
542  }
543 
544  av_assert0(minFilterSize > 0);
545  filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
546  av_assert0(filterSize > 0);
547  filter = av_malloc(filterSize * dstW * sizeof(*filter));
548  if (filterSize >= MAX_FILTER_SIZE * 16 /
549  ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter) {
550  av_log(NULL, AV_LOG_ERROR, "sws: filterSize %d is too large, try less extreem scaling or increase MAX_FILTER_SIZE and recompile\n", filterSize);
551  goto fail;
552  }
553  *outFilterSize = filterSize;
554 
555  if (flags & SWS_PRINT_INFO)
556  av_log(NULL, AV_LOG_VERBOSE,
557  "SwScaler: reducing / aligning filtersize %d -> %d\n",
558  filter2Size, filterSize);
559  /* try to reduce the filter-size (step2 reduce it) */
560  for (i = 0; i < dstW; i++) {
561  int j;
562 
563  for (j = 0; j < filterSize; j++) {
564  if (j >= filter2Size)
565  filter[i * filterSize + j] = 0;
566  else
567  filter[i * filterSize + j] = filter2[i * filter2Size + j];
568  if ((flags & SWS_BITEXACT) && j >= minFilterSize)
569  filter[i * filterSize + j] = 0;
570  }
571  }
572 
573  // FIXME try to align filterPos if possible
574 
575  // fix borders
576  for (i = 0; i < dstW; i++) {
577  int j;
578  if ((*filterPos)[i] < 0) {
579  // move filter coefficients left to compensate for filterPos
580  for (j = 1; j < filterSize; j++) {
581  int left = FFMAX(j + (*filterPos)[i], 0);
582  filter[i * filterSize + left] += filter[i * filterSize + j];
583  filter[i * filterSize + j] = 0;
584  }
585  (*filterPos)[i]= 0;
586  }
587 
588  if ((*filterPos)[i] + filterSize > srcW) {
589  int shift = (*filterPos)[i] + filterSize - srcW;
590  // move filter coefficients right to compensate for filterPos
591  for (j = filterSize - 2; j >= 0; j--) {
592  int right = FFMIN(j + shift, filterSize - 1);
593  filter[i * filterSize + right] += filter[i * filterSize + j];
594  filter[i * filterSize + j] = 0;
595  }
596  (*filterPos)[i]= srcW - filterSize;
597  }
598  }
599 
600  // Note the +1 is for the MMX scaler which reads over the end
601  /* align at 16 for AltiVec (needed by hScale_altivec_real) */
602  FF_ALLOCZ_OR_GOTO(NULL, *outFilter,
603  *outFilterSize * (dstW + 3) * sizeof(int16_t), fail);
604 
605  /* normalize & store in outFilter */
606  for (i = 0; i < dstW; i++) {
607  int j;
608  int64_t error = 0;
609  int64_t sum = 0;
610 
611  for (j = 0; j < filterSize; j++) {
612  sum += filter[i * filterSize + j];
613  }
614  sum = (sum + one / 2) / one;
615  for (j = 0; j < *outFilterSize; j++) {
616  int64_t v = filter[i * filterSize + j] + error;
617  int intV = ROUNDED_DIV(v, sum);
618  (*outFilter)[i * (*outFilterSize) + j] = intV;
619  error = v - intV * sum;
620  }
621  }
622 
623  (*filterPos)[dstW + 0] =
624  (*filterPos)[dstW + 1] =
625  (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1]; /* the MMX/SSE scaler will
626  * read over the end */
627  for (i = 0; i < *outFilterSize; i++) {
628  int k = (dstW - 1) * (*outFilterSize) + i;
629  (*outFilter)[k + 1 * (*outFilterSize)] =
630  (*outFilter)[k + 2 * (*outFilterSize)] =
631  (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
632  }
633 
634  ret = 0;
635 
636 fail:
637  if(ret < 0)
638  av_log(NULL, AV_LOG_ERROR, "sws: initFilter failed\n");
639  av_free(filter);
640  av_free(filter2);
641  return ret;
642 }
643 
644 #if HAVE_MMXEXT_INLINE
645 static av_cold int init_hscaler_mmxext(int dstW, int xInc, uint8_t *filterCode,
646  int16_t *filter, int32_t *filterPos,
647  int numSplits)
648 {
649  uint8_t *fragmentA;
650  x86_reg imm8OfPShufW1A;
651  x86_reg imm8OfPShufW2A;
652  x86_reg fragmentLengthA;
653  uint8_t *fragmentB;
654  x86_reg imm8OfPShufW1B;
655  x86_reg imm8OfPShufW2B;
656  x86_reg fragmentLengthB;
657  int fragmentPos;
658 
659  int xpos, i;
660 
661  // create an optimized horizontal scaling routine
662  /* This scaler is made of runtime-generated MMXEXT code using specially tuned
663  * pshufw instructions. For every four output pixels, if four input pixels
664  * are enough for the fast bilinear scaling, then a chunk of fragmentB is
665  * used. If five input pixels are needed, then a chunk of fragmentA is used.
666  */
667 
668  // code fragment
669 
670  __asm__ volatile (
671  "jmp 9f \n\t"
672  // Begin
673  "0: \n\t"
674  "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
675  "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
676  "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
677  "punpcklbw %%mm7, %%mm1 \n\t"
678  "punpcklbw %%mm7, %%mm0 \n\t"
679  "pshufw $0xFF, %%mm1, %%mm1 \n\t"
680  "1: \n\t"
681  "pshufw $0xFF, %%mm0, %%mm0 \n\t"
682  "2: \n\t"
683  "psubw %%mm1, %%mm0 \n\t"
684  "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
685  "pmullw %%mm3, %%mm0 \n\t"
686  "psllw $7, %%mm1 \n\t"
687  "paddw %%mm1, %%mm0 \n\t"
688 
689  "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
690 
691  "add $8, %%"REG_a" \n\t"
692  // End
693  "9: \n\t"
694  // "int $3 \n\t"
695  "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
696  "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
697  "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
698  "dec %1 \n\t"
699  "dec %2 \n\t"
700  "sub %0, %1 \n\t"
701  "sub %0, %2 \n\t"
702  "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
703  "sub %0, %3 \n\t"
704 
705 
706  : "=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
707  "=r" (fragmentLengthA)
708  );
709 
710  __asm__ volatile (
711  "jmp 9f \n\t"
712  // Begin
713  "0: \n\t"
714  "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
715  "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
716  "punpcklbw %%mm7, %%mm0 \n\t"
717  "pshufw $0xFF, %%mm0, %%mm1 \n\t"
718  "1: \n\t"
719  "pshufw $0xFF, %%mm0, %%mm0 \n\t"
720  "2: \n\t"
721  "psubw %%mm1, %%mm0 \n\t"
722  "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
723  "pmullw %%mm3, %%mm0 \n\t"
724  "psllw $7, %%mm1 \n\t"
725  "paddw %%mm1, %%mm0 \n\t"
726 
727  "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
728 
729  "add $8, %%"REG_a" \n\t"
730  // End
731  "9: \n\t"
732  // "int $3 \n\t"
733  "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
734  "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
735  "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
736  "dec %1 \n\t"
737  "dec %2 \n\t"
738  "sub %0, %1 \n\t"
739  "sub %0, %2 \n\t"
740  "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
741  "sub %0, %3 \n\t"
742 
743 
744  : "=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
745  "=r" (fragmentLengthB)
746  );
747 
748  xpos = 0; // lumXInc/2 - 0x8000; // difference between pixel centers
749  fragmentPos = 0;
750 
751  for (i = 0; i < dstW / numSplits; i++) {
752  int xx = xpos >> 16;
753 
754  if ((i & 3) == 0) {
755  int a = 0;
756  int b = ((xpos + xInc) >> 16) - xx;
757  int c = ((xpos + xInc * 2) >> 16) - xx;
758  int d = ((xpos + xInc * 3) >> 16) - xx;
759  int inc = (d + 1 < 4);
760  uint8_t *fragment = (d + 1 < 4) ? fragmentB : fragmentA;
761  x86_reg imm8OfPShufW1 = (d + 1 < 4) ? imm8OfPShufW1B : imm8OfPShufW1A;
762  x86_reg imm8OfPShufW2 = (d + 1 < 4) ? imm8OfPShufW2B : imm8OfPShufW2A;
763  x86_reg fragmentLength = (d + 1 < 4) ? fragmentLengthB : fragmentLengthA;
764  int maxShift = 3 - (d + inc);
765  int shift = 0;
766 
767  if (filterCode) {
768  filter[i] = ((xpos & 0xFFFF) ^ 0xFFFF) >> 9;
769  filter[i + 1] = (((xpos + xInc) & 0xFFFF) ^ 0xFFFF) >> 9;
770  filter[i + 2] = (((xpos + xInc * 2) & 0xFFFF) ^ 0xFFFF) >> 9;
771  filter[i + 3] = (((xpos + xInc * 3) & 0xFFFF) ^ 0xFFFF) >> 9;
772  filterPos[i / 2] = xx;
773 
774  memcpy(filterCode + fragmentPos, fragment, fragmentLength);
775 
776  filterCode[fragmentPos + imm8OfPShufW1] = (a + inc) |
777  ((b + inc) << 2) |
778  ((c + inc) << 4) |
779  ((d + inc) << 6);
780  filterCode[fragmentPos + imm8OfPShufW2] = a | (b << 2) |
781  (c << 4) |
782  (d << 6);
783 
784  if (i + 4 - inc >= dstW)
785  shift = maxShift; // avoid overread
786  else if ((filterPos[i / 2] & 3) <= maxShift)
787  shift = filterPos[i / 2] & 3; // align
788 
789  if (shift && i >= shift) {
790  filterCode[fragmentPos + imm8OfPShufW1] += 0x55 * shift;
791  filterCode[fragmentPos + imm8OfPShufW2] += 0x55 * shift;
792  filterPos[i / 2] -= shift;
793  }
794  }
795 
796  fragmentPos += fragmentLength;
797 
798  if (filterCode)
799  filterCode[fragmentPos] = RET;
800  }
801  xpos += xInc;
802  }
803  if (filterCode)
804  filterPos[((i / 2) + 1) & (~1)] = xpos >> 16; // needed to jump to the next part
805 
806  return fragmentPos + 1;
807 }
808 #endif /* HAVE_MMXEXT_INLINE */
809 
810 static void getSubSampleFactors(int *h, int *v, enum AVPixelFormat format)
811 {
812  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
813  *h = desc->log2_chroma_w;
814  *v = desc->log2_chroma_h;
815 }
816 
817 static void fill_rgb2yuv_table(SwsContext *c, const int table[4], int dstRange)
818 {
819  int64_t W, V, Z, Cy, Cu, Cv;
820  int64_t vr = table[0];
821  int64_t ub = table[1];
822  int64_t ug = -table[2];
823  int64_t vg = -table[3];
824  int64_t ONE = 65536;
825  int64_t cy = ONE;
827  int i;
828  static const int8_t map[] = {
829  BY_IDX, GY_IDX, -1 , BY_IDX, BY_IDX, GY_IDX, -1 , BY_IDX,
830  RY_IDX, -1 , GY_IDX, RY_IDX, RY_IDX, -1 , GY_IDX, RY_IDX,
831  RY_IDX, GY_IDX, -1 , RY_IDX, RY_IDX, GY_IDX, -1 , RY_IDX,
832  BY_IDX, -1 , GY_IDX, BY_IDX, BY_IDX, -1 , GY_IDX, BY_IDX,
833  BU_IDX, GU_IDX, -1 , BU_IDX, BU_IDX, GU_IDX, -1 , BU_IDX,
834  RU_IDX, -1 , GU_IDX, RU_IDX, RU_IDX, -1 , GU_IDX, RU_IDX,
835  RU_IDX, GU_IDX, -1 , RU_IDX, RU_IDX, GU_IDX, -1 , RU_IDX,
836  BU_IDX, -1 , GU_IDX, BU_IDX, BU_IDX, -1 , GU_IDX, BU_IDX,
837  BV_IDX, GV_IDX, -1 , BV_IDX, BV_IDX, GV_IDX, -1 , BV_IDX,
838  RV_IDX, -1 , GV_IDX, RV_IDX, RV_IDX, -1 , GV_IDX, RV_IDX,
839  RV_IDX, GV_IDX, -1 , RV_IDX, RV_IDX, GV_IDX, -1 , RV_IDX,
840  BV_IDX, -1 , GV_IDX, BV_IDX, BV_IDX, -1 , GV_IDX, BV_IDX,
843  GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 ,
844  -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX,
847  GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 ,
848  -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX,
851  GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 ,
852  -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, //23
853  -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //24
854  -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //25
855  -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //26
856  -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //27
857  -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //28
858  -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //29
859  -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //30
860  -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //31
861  BY_IDX, GY_IDX, RY_IDX, -1 , -1 , -1 , -1 , -1 , //32
862  BU_IDX, GU_IDX, RU_IDX, -1 , -1 , -1 , -1 , -1 , //33
863  BV_IDX, GV_IDX, RV_IDX, -1 , -1 , -1 , -1 , -1 , //34
864  };
865 
866  dstRange = 0; //FIXME range = 1 is handled elsewhere
867 
868  if (!dstRange) {
869  cy = cy * 255 / 219;
870  } else {
871  vr = vr * 224 / 255;
872  ub = ub * 224 / 255;
873  ug = ug * 224 / 255;
874  vg = vg * 224 / 255;
875  }
876  W = ROUNDED_DIV(ONE*ONE*ug, ub);
877  V = ROUNDED_DIV(ONE*ONE*vg, vr);
878  Z = ONE*ONE-W-V;
879 
880  Cy = ROUNDED_DIV(cy*Z, ONE);
881  Cu = ROUNDED_DIV(ub*Z, ONE);
882  Cv = ROUNDED_DIV(vr*Z, ONE);
883 
884  c->input_rgb2yuv_table[RY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cy);
885  c->input_rgb2yuv_table[GY_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cy);
886  c->input_rgb2yuv_table[BY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cy);
887 
888  c->input_rgb2yuv_table[RU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cu);
889  c->input_rgb2yuv_table[GU_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cu);
890  c->input_rgb2yuv_table[BU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(Z+W) , Cu);
891 
892  c->input_rgb2yuv_table[RV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(V+Z) , Cv);
893  c->input_rgb2yuv_table[GV_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cv);
894  c->input_rgb2yuv_table[BV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cv);
895 
896  if(/*!dstRange && */table == ff_yuv2rgb_coeffs[SWS_CS_DEFAULT]) {
897  c->input_rgb2yuv_table[BY_IDX] = ((int)(0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
898  c->input_rgb2yuv_table[BV_IDX] = (-(int)(0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
899  c->input_rgb2yuv_table[BU_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
900  c->input_rgb2yuv_table[GY_IDX] = ((int)(0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
901  c->input_rgb2yuv_table[GV_IDX] = (-(int)(0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
902  c->input_rgb2yuv_table[GU_IDX] = (-(int)(0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
903  c->input_rgb2yuv_table[RY_IDX] = ((int)(0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
904  c->input_rgb2yuv_table[RV_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
905  c->input_rgb2yuv_table[RU_IDX] = (-(int)(0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
906  }
907  for(i=0; i<FF_ARRAY_ELEMS(map); i++)
908  AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ? c->input_rgb2yuv_table[map[i]] : 0);
909 }
910 
911 static void fill_xyztables(struct SwsContext *c)
912 {
913  int i;
914  double xyzgamma = XYZ_GAMMA;
915  double rgbgamma = 1.0 / RGB_GAMMA;
916  static const int16_t xyz2rgb_matrix[3][4] = {
917  {13270, -6295, -2041},
918  {-3969, 7682, 170},
919  { 228, -835, 4329} };
920  static int16_t xyzgamma_tab[4096], rgbgamma_tab[4096];
921 
922  memcpy(c->xyz2rgb_matrix, xyz2rgb_matrix, sizeof(c->xyz2rgb_matrix));
923  c->xyzgamma = xyzgamma_tab;
924  c->rgbgamma = rgbgamma_tab;
925 
926  if (rgbgamma_tab[4095])
927  return;
928 
929  /* set gamma vectors */
930  for (i = 0; i < 4096; i++) {
931  xyzgamma_tab[i] = lrint(pow(i / 4095.0, xyzgamma) * 4095.0);
932  rgbgamma_tab[i] = lrint(pow(i / 4095.0, rgbgamma) * 4095.0);
933  }
934 }
935 
936 int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
937  int srcRange, const int table[4], int dstRange,
938  int brightness, int contrast, int saturation)
939 {
940  const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(c->dstFormat);
941  const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(c->srcFormat);
942  memcpy(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
943  memcpy(c->dstColorspaceTable, table, sizeof(int) * 4);
944 
945  if(!isYUV(c->dstFormat) && !isGray(c->dstFormat))
946  dstRange = 0;
947  if(!isYUV(c->srcFormat) && !isGray(c->srcFormat))
948  srcRange = 0;
949 
950  c->brightness = brightness;
951  c->contrast = contrast;
952  c->saturation = saturation;
953  c->srcRange = srcRange;
954  c->dstRange = dstRange;
955 
956  fill_xyztables(c);
957 
958  if ((isYUV(c->dstFormat) || isGray(c->dstFormat)) && (isYUV(c->srcFormat) || isGray(c->srcFormat)))
959  return -1;
960 
961  c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
962  c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
963 
964  if (!isYUV(c->dstFormat) && !isGray(c->dstFormat)) {
965  ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
966  contrast, saturation);
967  // FIXME factorize
968 
969  if (HAVE_ALTIVEC && av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC)
970  ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness,
971  contrast, saturation);
972  }
973 
974  fill_rgb2yuv_table(c, table, dstRange);
975 
976  return 0;
977 }
978 
979 int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
980  int *srcRange, int **table, int *dstRange,
981  int *brightness, int *contrast, int *saturation)
982 {
983  if (!c || isYUV(c->dstFormat) || isGray(c->dstFormat))
984  return -1;
985 
986  *inv_table = c->srcColorspaceTable;
987  *table = c->dstColorspaceTable;
988  *srcRange = c->srcRange;
989  *dstRange = c->dstRange;
990  *brightness = c->brightness;
991  *contrast = c->contrast;
992  *saturation = c->saturation;
993 
994  return 0;
995 }
996 
997 static int handle_jpeg(enum AVPixelFormat *format)
998 {
999  switch (*format) {
1000  case AV_PIX_FMT_YUVJ420P:
1001  *format = AV_PIX_FMT_YUV420P;
1002  return 1;
1003  case AV_PIX_FMT_YUVJ411P:
1004  *format = AV_PIX_FMT_YUV411P;
1005  return 1;
1006  case AV_PIX_FMT_YUVJ422P:
1007  *format = AV_PIX_FMT_YUV422P;
1008  return 1;
1009  case AV_PIX_FMT_YUVJ444P:
1010  *format = AV_PIX_FMT_YUV444P;
1011  return 1;
1012  case AV_PIX_FMT_YUVJ440P:
1013  *format = AV_PIX_FMT_YUV440P;
1014  return 1;
1015  case AV_PIX_FMT_GRAY8:
1016  return 1;
1017  default:
1018  return 0;
1019  }
1020 }
1021 
1022 static int handle_0alpha(enum AVPixelFormat *format)
1023 {
1024  switch (*format) {
1025  case AV_PIX_FMT_0BGR : *format = AV_PIX_FMT_ABGR ; return 1;
1026  case AV_PIX_FMT_BGR0 : *format = AV_PIX_FMT_BGRA ; return 4;
1027  case AV_PIX_FMT_0RGB : *format = AV_PIX_FMT_ARGB ; return 1;
1028  case AV_PIX_FMT_RGB0 : *format = AV_PIX_FMT_RGBA ; return 4;
1029  default: return 0;
1030  }
1031 }
1032 
1033 static int handle_xyz(enum AVPixelFormat *format)
1034 {
1035  switch (*format) {
1036  case AV_PIX_FMT_XYZ12BE : *format = AV_PIX_FMT_RGB48BE; return 1;
1037  case AV_PIX_FMT_XYZ12LE : *format = AV_PIX_FMT_RGB48LE; return 1;
1038  default: return 0;
1039  }
1040 }
1041 
1043 {
1044  SwsContext *c = av_mallocz(sizeof(SwsContext));
1045 
1046  if (c) {
1049  }
1050 
1051  return c;
1052 }
1053 
1055  SwsFilter *dstFilter)
1056 {
1057  int i, j;
1058  int usesVFilter, usesHFilter;
1059  int unscaled;
1060  SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
1061  int srcW = c->srcW;
1062  int srcH = c->srcH;
1063  int dstW = c->dstW;
1064  int dstH = c->dstH;
1065  int dst_stride = FFALIGN(dstW * sizeof(int16_t) + 66, 16);
1066  int flags, cpu_flags;
1067  enum AVPixelFormat srcFormat = c->srcFormat;
1068  enum AVPixelFormat dstFormat = c->dstFormat;
1069  const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(srcFormat);
1070  const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(dstFormat);
1071 
1072  cpu_flags = av_get_cpu_flags();
1073  flags = c->flags;
1074  emms_c();
1075  if (!rgb15to16)
1076  sws_rgb2rgb_init();
1077 
1078  unscaled = (srcW == dstW && srcH == dstH);
1079 
1080  handle_jpeg(&srcFormat);
1081  handle_jpeg(&dstFormat);
1082  if(srcFormat!=c->srcFormat || dstFormat!=c->dstFormat)
1083  av_log(c, AV_LOG_WARNING, "deprecated pixel format used, make sure you did set range correctly\n");
1084  handle_0alpha(&srcFormat);
1085  handle_0alpha(&dstFormat);
1086  handle_xyz(&srcFormat);
1087  handle_xyz(&dstFormat);
1088 
1089  if(srcFormat!=c->srcFormat || dstFormat!=c->dstFormat){
1090  c->srcFormat= srcFormat;
1091  c->dstFormat= dstFormat;
1092  }
1093 
1094  if (!(unscaled && sws_isSupportedEndiannessConversion(srcFormat) &&
1095  av_pix_fmt_swap_endianness(srcFormat) == dstFormat)) {
1096  if (!sws_isSupportedInput(srcFormat)) {
1097  av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
1098  av_get_pix_fmt_name(srcFormat));
1099  return AVERROR(EINVAL);
1100  }
1101  if (!sws_isSupportedOutput(dstFormat)) {
1102  av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
1103  av_get_pix_fmt_name(dstFormat));
1104  return AVERROR(EINVAL);
1105  }
1106  }
1107 
1108  i = flags & (SWS_POINT |
1109  SWS_AREA |
1110  SWS_BILINEAR |
1112  SWS_BICUBIC |
1113  SWS_X |
1114  SWS_GAUSS |
1115  SWS_LANCZOS |
1116  SWS_SINC |
1117  SWS_SPLINE |
1118  SWS_BICUBLIN);
1119  if (!i || (i & (i - 1))) {
1120  av_log(c, AV_LOG_ERROR, "Exactly one scaler algorithm must be chosen, got %X\n", i);
1121  return AVERROR(EINVAL);
1122  }
1123  /* sanity check */
1124  if (srcW < 1 || srcH < 1 || dstW < 1 || dstH < 1) {
1125  /* FIXME check if these are enough and try to lower them after
1126  * fixing the relevant parts of the code */
1127  av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
1128  srcW, srcH, dstW, dstH);
1129  return AVERROR(EINVAL);
1130  }
1131 
1132  if (!dstFilter)
1133  dstFilter = &dummyFilter;
1134  if (!srcFilter)
1135  srcFilter = &dummyFilter;
1136 
1137  c->lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
1138  c->lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
1139  c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
1140  c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
1141  c->vRounder = 4 * 0x0001000100010001ULL;
1142 
1143  usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
1144  (srcFilter->chrV && srcFilter->chrV->length > 1) ||
1145  (dstFilter->lumV && dstFilter->lumV->length > 1) ||
1146  (dstFilter->chrV && dstFilter->chrV->length > 1);
1147  usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
1148  (srcFilter->chrH && srcFilter->chrH->length > 1) ||
1149  (dstFilter->lumH && dstFilter->lumH->length > 1) ||
1150  (dstFilter->chrH && dstFilter->chrH->length > 1);
1151 
1154 
1155  if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) {
1156  if (dstW&1) {
1157  av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to odd output size\n");
1158  flags |= SWS_FULL_CHR_H_INT;
1159  c->flags = flags;
1160  }
1161  }
1162 
1163  if(dstFormat == AV_PIX_FMT_BGR4_BYTE ||
1164  dstFormat == AV_PIX_FMT_RGB4_BYTE ||
1165  dstFormat == AV_PIX_FMT_BGR8 ||
1166  dstFormat == AV_PIX_FMT_RGB8) {
1167  if (flags & SWS_ERROR_DIFFUSION && !(flags & SWS_FULL_CHR_H_INT)) {
1168  av_log(c, AV_LOG_DEBUG,
1169  "Error diffusion dither is only supported in full chroma interpolation for destination format '%s'\n",
1170  av_get_pix_fmt_name(dstFormat));
1171  flags |= SWS_FULL_CHR_H_INT;
1172  c->flags = flags;
1173  }
1174  if (!(flags & SWS_ERROR_DIFFUSION) && (flags & SWS_FULL_CHR_H_INT)) {
1175  av_log(c, AV_LOG_DEBUG,
1176  "Ordered dither is not supported in full chroma interpolation for destination format '%s'\n",
1177  av_get_pix_fmt_name(dstFormat));
1178  flags |= SWS_ERROR_DIFFUSION;
1179  c->flags = flags;
1180  }
1181  }
1182  if (isPlanarRGB(dstFormat)) {
1183  if (!(flags & SWS_FULL_CHR_H_INT)) {
1184  av_log(c, AV_LOG_DEBUG,
1185  "%s output is not supported with half chroma resolution, switching to full\n",
1186  av_get_pix_fmt_name(dstFormat));
1187  flags |= SWS_FULL_CHR_H_INT;
1188  c->flags = flags;
1189  }
1190  }
1191 
1192  /* reuse chroma for 2 pixels RGB/BGR unless user wants full
1193  * chroma interpolation */
1194  if (flags & SWS_FULL_CHR_H_INT &&
1195  isAnyRGB(dstFormat) &&
1196  !isPlanarRGB(dstFormat) &&
1197  dstFormat != AV_PIX_FMT_RGBA &&
1198  dstFormat != AV_PIX_FMT_ARGB &&
1199  dstFormat != AV_PIX_FMT_BGRA &&
1200  dstFormat != AV_PIX_FMT_ABGR &&
1201  dstFormat != AV_PIX_FMT_RGB24 &&
1202  dstFormat != AV_PIX_FMT_BGR24 &&
1203  dstFormat != AV_PIX_FMT_BGR4_BYTE &&
1204  dstFormat != AV_PIX_FMT_RGB4_BYTE &&
1205  dstFormat != AV_PIX_FMT_BGR8 &&
1206  dstFormat != AV_PIX_FMT_RGB8
1207  ) {
1209  "full chroma interpolation for destination format '%s' not yet implemented\n",
1210  av_get_pix_fmt_name(dstFormat));
1211  flags &= ~SWS_FULL_CHR_H_INT;
1212  c->flags = flags;
1213  }
1214  if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
1215  c->chrDstHSubSample = 1;
1216 
1217  // drop some chroma lines if the user wants it
1218  c->vChrDrop = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
1220  c->chrSrcVSubSample += c->vChrDrop;
1221 
1222  /* drop every other pixel for chroma calculation unless user
1223  * wants full chroma */
1224  if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP) &&
1225  srcFormat != AV_PIX_FMT_RGB8 && srcFormat != AV_PIX_FMT_BGR8 &&
1226  srcFormat != AV_PIX_FMT_RGB4 && srcFormat != AV_PIX_FMT_BGR4 &&
1227  srcFormat != AV_PIX_FMT_RGB4_BYTE && srcFormat != AV_PIX_FMT_BGR4_BYTE &&
1228  srcFormat != AV_PIX_FMT_GBRP9BE && srcFormat != AV_PIX_FMT_GBRP9LE &&
1229  srcFormat != AV_PIX_FMT_GBRP10BE && srcFormat != AV_PIX_FMT_GBRP10LE &&
1230  srcFormat != AV_PIX_FMT_GBRP12BE && srcFormat != AV_PIX_FMT_GBRP12LE &&
1231  srcFormat != AV_PIX_FMT_GBRP14BE && srcFormat != AV_PIX_FMT_GBRP14LE &&
1232  srcFormat != AV_PIX_FMT_GBRP16BE && srcFormat != AV_PIX_FMT_GBRP16LE &&
1233  ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
1234  (flags & SWS_FAST_BILINEAR)))
1235  c->chrSrcHSubSample = 1;
1236 
1237  // Note the FF_CEIL_RSHIFT is so that we always round toward +inf.
1238  c->chrSrcW = FF_CEIL_RSHIFT(srcW, c->chrSrcHSubSample);
1239  c->chrSrcH = FF_CEIL_RSHIFT(srcH, c->chrSrcVSubSample);
1240  c->chrDstW = FF_CEIL_RSHIFT(dstW, c->chrDstHSubSample);
1241  c->chrDstH = FF_CEIL_RSHIFT(dstH, c->chrDstVSubSample);
1242 
1243  FF_ALLOC_OR_GOTO(c, c->formatConvBuffer, FFALIGN(srcW*2+78, 16) * 2, fail);
1244 
1245  /* unscaled special cases */
1246  if (unscaled && !usesHFilter && !usesVFilter &&
1247  (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
1249 
1250  if (c->swScale) {
1251  if (flags & SWS_PRINT_INFO)
1252  av_log(c, AV_LOG_INFO,
1253  "using unscaled %s -> %s special converter\n",
1254  av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1255  return 0;
1256  }
1257  }
1258 
1259  c->srcBpc = 1 + desc_src->comp[0].depth_minus1;
1260  if (c->srcBpc < 8)
1261  c->srcBpc = 8;
1262  c->dstBpc = 1 + desc_dst->comp[0].depth_minus1;
1263  if (c->dstBpc < 8)
1264  c->dstBpc = 8;
1265  if (isAnyRGB(srcFormat) || srcFormat == AV_PIX_FMT_PAL8)
1266  c->srcBpc = 16;
1267  if (c->dstBpc == 16)
1268  dst_stride <<= 1;
1269 
1270  if (INLINE_MMXEXT(cpu_flags) && c->srcBpc == 8 && c->dstBpc <= 14) {
1271  c->canMMXEXTBeUsed = (dstW >= srcW && (dstW & 31) == 0 &&
1272  (srcW & 15) == 0) ? 1 : 0;
1273  if (!c->canMMXEXTBeUsed && dstW >= srcW && (srcW & 15) == 0
1274 
1275  && (flags & SWS_FAST_BILINEAR)) {
1276  if (flags & SWS_PRINT_INFO)
1277  av_log(c, AV_LOG_INFO,
1278  "output width is not a multiple of 32 -> no MMXEXT scaler\n");
1279  }
1280  if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat) || isAnyRGB(c->srcFormat))
1281  c->canMMXEXTBeUsed = 0;
1282  } else
1283  c->canMMXEXTBeUsed = 0;
1284 
1285  c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
1286  c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
1287 
1288  /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
1289  * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
1290  * correct scaling.
1291  * n-2 is the last chrominance sample available.
1292  * This is not perfect, but no one should notice the difference, the more
1293  * correct variant would be like the vertical one, but that would require
1294  * some special code for the first and last pixel */
1295  if (flags & SWS_FAST_BILINEAR) {
1296  if (c->canMMXEXTBeUsed) {
1297  c->lumXInc += 20;
1298  c->chrXInc += 20;
1299  }
1300  // we don't use the x86 asm scaler if MMX is available
1301  else if (INLINE_MMX(cpu_flags) && c->dstBpc <= 14) {
1302  c->lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20;
1303  c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
1304  }
1305  }
1306 
1307 #define USE_MMAP (HAVE_MMAP && HAVE_MPROTECT && defined MAP_ANONYMOUS)
1308 
1309  /* precalculate horizontal scaler filter coefficients */
1310  {
1311 #if HAVE_MMXEXT_INLINE
1312 // can't downscale !!!
1313  if (c->canMMXEXTBeUsed && (flags & SWS_FAST_BILINEAR)) {
1314  c->lumMmxextFilterCodeSize = init_hscaler_mmxext(dstW, c->lumXInc, NULL,
1315  NULL, NULL, 8);
1316  c->chrMmxextFilterCodeSize = init_hscaler_mmxext(c->chrDstW, c->chrXInc,
1317  NULL, NULL, NULL, 4);
1318 
1319 #if USE_MMAP
1320  c->lumMmxextFilterCode = mmap(NULL, c->lumMmxextFilterCodeSize,
1321  PROT_READ | PROT_WRITE,
1322  MAP_PRIVATE | MAP_ANONYMOUS,
1323  -1, 0);
1324  c->chrMmxextFilterCode = mmap(NULL, c->chrMmxextFilterCodeSize,
1325  PROT_READ | PROT_WRITE,
1326  MAP_PRIVATE | MAP_ANONYMOUS,
1327  -1, 0);
1328 #elif HAVE_VIRTUALALLOC
1329  c->lumMmxextFilterCode = VirtualAlloc(NULL,
1331  MEM_COMMIT,
1332  PAGE_EXECUTE_READWRITE);
1333  c->chrMmxextFilterCode = VirtualAlloc(NULL,
1335  MEM_COMMIT,
1336  PAGE_EXECUTE_READWRITE);
1337 #else
1340 #endif
1341 
1342 #ifdef MAP_ANONYMOUS
1343  if (c->lumMmxextFilterCode == MAP_FAILED || c->chrMmxextFilterCode == MAP_FAILED)
1344 #else
1346 #endif
1347  {
1348  av_log(c, AV_LOG_ERROR, "Failed to allocate MMX2FilterCode\n");
1349  return AVERROR(ENOMEM);
1350  }
1351 
1352  FF_ALLOCZ_OR_GOTO(c, c->hLumFilter, (dstW / 8 + 8) * sizeof(int16_t), fail);
1353  FF_ALLOCZ_OR_GOTO(c, c->hChrFilter, (c->chrDstW / 4 + 8) * sizeof(int16_t), fail);
1354  FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW / 2 / 8 + 8) * sizeof(int32_t), fail);
1355  FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW / 2 / 4 + 8) * sizeof(int32_t), fail);
1356 
1357  init_hscaler_mmxext( dstW, c->lumXInc, c->lumMmxextFilterCode,
1358  c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
1359  init_hscaler_mmxext(c->chrDstW, c->chrXInc, c->chrMmxextFilterCode,
1360  c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4);
1361 
1362 #if USE_MMAP
1363  mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ);
1364  mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ);
1365 #endif
1366  } else
1367 #endif /* HAVE_MMXEXT_INLINE */
1368  {
1369  const int filterAlign =
1370  (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 4 :
1371  (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
1372  1;
1373 
1374  if (initFilter(&c->hLumFilter, &c->hLumFilterPos,
1375  &c->hLumFilterSize, c->lumXInc,
1376  srcW, dstW, filterAlign, 1 << 14,
1377  (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1378  cpu_flags, srcFilter->lumH, dstFilter->lumH,
1379  c->param) < 0)
1380  goto fail;
1381  if (initFilter(&c->hChrFilter, &c->hChrFilterPos,
1382  &c->hChrFilterSize, c->chrXInc,
1383  c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
1384  (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1385  cpu_flags, srcFilter->chrH, dstFilter->chrH,
1386  c->param) < 0)
1387  goto fail;
1388  }
1389  } // initialize horizontal stuff
1390 
1391  /* precalculate vertical scaler filter coefficients */
1392  {
1393  const int filterAlign =
1394  (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 2 :
1395  (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
1396  1;
1397 
1399  c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
1400  (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1401  cpu_flags, srcFilter->lumV, dstFilter->lumV,
1402  c->param) < 0)
1403  goto fail;
1405  c->chrYInc, c->chrSrcH, c->chrDstH,
1406  filterAlign, (1 << 12),
1407  (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1408  cpu_flags, srcFilter->chrV, dstFilter->chrV,
1409  c->param) < 0)
1410  goto fail;
1411 
1412 #if HAVE_ALTIVEC
1413  FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof(vector signed short) * c->vLumFilterSize * c->dstH, fail);
1414  FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof(vector signed short) * c->vChrFilterSize * c->chrDstH, fail);
1415 
1416  for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
1417  int j;
1418  short *p = (short *)&c->vYCoeffsBank[i];
1419  for (j = 0; j < 8; j++)
1420  p[j] = c->vLumFilter[i];
1421  }
1422 
1423  for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
1424  int j;
1425  short *p = (short *)&c->vCCoeffsBank[i];
1426  for (j = 0; j < 8; j++)
1427  p[j] = c->vChrFilter[i];
1428  }
1429 #endif
1430  }
1431 
1432  // calculate buffer sizes so that they won't run out while handling these damn slices
1433  c->vLumBufSize = c->vLumFilterSize;
1434  c->vChrBufSize = c->vChrFilterSize;
1435  for (i = 0; i < dstH; i++) {
1436  int chrI = (int64_t)i * c->chrDstH / dstH;
1437  int nextSlice = FFMAX(c->vLumFilterPos[i] + c->vLumFilterSize - 1,
1438  ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)
1439  << c->chrSrcVSubSample));
1440 
1441  nextSlice >>= c->chrSrcVSubSample;
1442  nextSlice <<= c->chrSrcVSubSample;
1443  if (c->vLumFilterPos[i] + c->vLumBufSize < nextSlice)
1444  c->vLumBufSize = nextSlice - c->vLumFilterPos[i];
1445  if (c->vChrFilterPos[chrI] + c->vChrBufSize <
1446  (nextSlice >> c->chrSrcVSubSample))
1447  c->vChrBufSize = (nextSlice >> c->chrSrcVSubSample) -
1448  c->vChrFilterPos[chrI];
1449  }
1450 
1451  for (i = 0; i < 4; i++)
1452  FF_ALLOCZ_OR_GOTO(c, c->dither_error[i], (c->dstW+2) * sizeof(int), fail);
1453 
1454  /* Allocate pixbufs (we use dynamic allocation because otherwise we would
1455  * need to allocate several megabytes to handle all possible cases) */
1456  FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
1457  FF_ALLOC_OR_GOTO(c, c->chrUPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
1458  FF_ALLOC_OR_GOTO(c, c->chrVPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
1459  if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
1460  FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
1461  /* Note we need at least one pixel more at the end because of the MMX code
1462  * (just in case someone wants to replace the 4000/8000). */
1463  /* align at 16 bytes for AltiVec */
1464  for (i = 0; i < c->vLumBufSize; i++) {
1465  FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i + c->vLumBufSize],
1466  dst_stride + 16, fail);
1467  c->lumPixBuf[i] = c->lumPixBuf[i + c->vLumBufSize];
1468  }
1469  // 64 / c->scalingBpp is the same as 16 / sizeof(scaling_intermediate)
1470  c->uv_off = (dst_stride>>1) + 64 / (c->dstBpc &~ 7);
1471  c->uv_offx2 = dst_stride + 16;
1472  for (i = 0; i < c->vChrBufSize; i++) {
1473  FF_ALLOC_OR_GOTO(c, c->chrUPixBuf[i + c->vChrBufSize],
1474  dst_stride * 2 + 32, fail);
1475  c->chrUPixBuf[i] = c->chrUPixBuf[i + c->vChrBufSize];
1476  c->chrVPixBuf[i] = c->chrVPixBuf[i + c->vChrBufSize]
1477  = c->chrUPixBuf[i] + (dst_stride >> 1) + 8;
1478  }
1479  if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1480  for (i = 0; i < c->vLumBufSize; i++) {
1481  FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i + c->vLumBufSize],
1482  dst_stride + 16, fail);
1483  c->alpPixBuf[i] = c->alpPixBuf[i + c->vLumBufSize];
1484  }
1485 
1486  // try to avoid drawing green stuff between the right end and the stride end
1487  for (i = 0; i < c->vChrBufSize; i++)
1488  if(desc_dst->comp[0].depth_minus1 == 15){
1489  av_assert0(c->dstBpc > 14);
1490  for(j=0; j<dst_stride/2+1; j++)
1491  ((int32_t*)(c->chrUPixBuf[i]))[j] = 1<<18;
1492  } else
1493  for(j=0; j<dst_stride+1; j++)
1494  ((int16_t*)(c->chrUPixBuf[i]))[j] = 1<<14;
1495 
1496  av_assert0(c->chrDstH <= dstH);
1497 
1498  if (flags & SWS_PRINT_INFO) {
1499  if (flags & SWS_FAST_BILINEAR)
1500  av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
1501  else if (flags & SWS_BILINEAR)
1502  av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
1503  else if (flags & SWS_BICUBIC)
1504  av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
1505  else if (flags & SWS_X)
1506  av_log(c, AV_LOG_INFO, "Experimental scaler, ");
1507  else if (flags & SWS_POINT)
1508  av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
1509  else if (flags & SWS_AREA)
1510  av_log(c, AV_LOG_INFO, "Area Averaging scaler, ");
1511  else if (flags & SWS_BICUBLIN)
1512  av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
1513  else if (flags & SWS_GAUSS)
1514  av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
1515  else if (flags & SWS_SINC)
1516  av_log(c, AV_LOG_INFO, "Sinc scaler, ");
1517  else if (flags & SWS_LANCZOS)
1518  av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
1519  else if (flags & SWS_SPLINE)
1520  av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
1521  else
1522  av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
1523 
1524  av_log(c, AV_LOG_INFO, "from %s to %s%s ",
1525  av_get_pix_fmt_name(srcFormat),
1526 #ifdef DITHER1XBPP
1527  dstFormat == AV_PIX_FMT_BGR555 || dstFormat == AV_PIX_FMT_BGR565 ||
1528  dstFormat == AV_PIX_FMT_RGB444BE || dstFormat == AV_PIX_FMT_RGB444LE ||
1529  dstFormat == AV_PIX_FMT_BGR444BE || dstFormat == AV_PIX_FMT_BGR444LE ?
1530  "dithered " : "",
1531 #else
1532  "",
1533 #endif
1534  av_get_pix_fmt_name(dstFormat));
1535 
1536  if (INLINE_MMXEXT(cpu_flags))
1537  av_log(c, AV_LOG_INFO, "using MMXEXT\n");
1538  else if (INLINE_AMD3DNOW(cpu_flags))
1539  av_log(c, AV_LOG_INFO, "using 3DNOW\n");
1540  else if (INLINE_MMX(cpu_flags))
1541  av_log(c, AV_LOG_INFO, "using MMX\n");
1542  else if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC)
1543  av_log(c, AV_LOG_INFO, "using AltiVec\n");
1544  else
1545  av_log(c, AV_LOG_INFO, "using C\n");
1546 
1547  av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1548  av_log(c, AV_LOG_DEBUG,
1549  "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1550  c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
1551  av_log(c, AV_LOG_DEBUG,
1552  "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1553  c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
1554  c->chrXInc, c->chrYInc);
1555  }
1556 
1557  c->swScale = ff_getSwsFunc(c);
1558  return 0;
1559 fail: // FIXME replace things by appropriate error codes
1560  return -1;
1561 }
1562 
1563 #if FF_API_SWS_GETCONTEXT
1564 SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
1565  int dstW, int dstH, enum AVPixelFormat dstFormat,
1566  int flags, SwsFilter *srcFilter,
1567  SwsFilter *dstFilter, const double *param)
1568 {
1569  SwsContext *c;
1570 
1571  if (!(c = sws_alloc_context()))
1572  return NULL;
1573 
1574  c->flags = flags;
1575  c->srcW = srcW;
1576  c->srcH = srcH;
1577  c->dstW = dstW;
1578  c->dstH = dstH;
1579  c->srcRange = handle_jpeg(&srcFormat);
1580  c->dstRange = handle_jpeg(&dstFormat);
1581  c->src0Alpha = handle_0alpha(&srcFormat);
1582  c->dst0Alpha = handle_0alpha(&dstFormat);
1583  c->srcXYZ = handle_xyz(&srcFormat);
1584  c->dstXYZ = handle_xyz(&dstFormat);
1585  c->srcFormat = srcFormat;
1586  c->dstFormat = dstFormat;
1587 
1588  if (param) {
1589  c->param[0] = param[0];
1590  c->param[1] = param[1];
1591  }
1592  sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
1593  ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/,
1594  c->dstRange, 0, 1 << 16, 1 << 16);
1595 
1596  if (sws_init_context(c, srcFilter, dstFilter) < 0) {
1597  sws_freeContext(c);
1598  return NULL;
1599  }
1600 
1601  return c;
1602 }
1603 #endif
1604 
1605 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
1606  float lumaSharpen, float chromaSharpen,
1607  float chromaHShift, float chromaVShift,
1608  int verbose)
1609 {
1610  SwsFilter *filter = av_malloc(sizeof(SwsFilter));
1611  if (!filter)
1612  return NULL;
1613 
1614  if (lumaGBlur != 0.0) {
1615  filter->lumH = sws_getGaussianVec(lumaGBlur, 3.0);
1616  filter->lumV = sws_getGaussianVec(lumaGBlur, 3.0);
1617  } else {
1618  filter->lumH = sws_getIdentityVec();
1619  filter->lumV = sws_getIdentityVec();
1620  }
1621 
1622  if (chromaGBlur != 0.0) {
1623  filter->chrH = sws_getGaussianVec(chromaGBlur, 3.0);
1624  filter->chrV = sws_getGaussianVec(chromaGBlur, 3.0);
1625  } else {
1626  filter->chrH = sws_getIdentityVec();
1627  filter->chrV = sws_getIdentityVec();
1628  }
1629 
1630  if (chromaSharpen != 0.0) {
1631  SwsVector *id = sws_getIdentityVec();
1632  sws_scaleVec(filter->chrH, -chromaSharpen);
1633  sws_scaleVec(filter->chrV, -chromaSharpen);
1634  sws_addVec(filter->chrH, id);
1635  sws_addVec(filter->chrV, id);
1636  sws_freeVec(id);
1637  }
1638 
1639  if (lumaSharpen != 0.0) {
1640  SwsVector *id = sws_getIdentityVec();
1641  sws_scaleVec(filter->lumH, -lumaSharpen);
1642  sws_scaleVec(filter->lumV, -lumaSharpen);
1643  sws_addVec(filter->lumH, id);
1644  sws_addVec(filter->lumV, id);
1645  sws_freeVec(id);
1646  }
1647 
1648  if (chromaHShift != 0.0)
1649  sws_shiftVec(filter->chrH, (int)(chromaHShift + 0.5));
1650 
1651  if (chromaVShift != 0.0)
1652  sws_shiftVec(filter->chrV, (int)(chromaVShift + 0.5));
1653 
1654  sws_normalizeVec(filter->chrH, 1.0);
1655  sws_normalizeVec(filter->chrV, 1.0);
1656  sws_normalizeVec(filter->lumH, 1.0);
1657  sws_normalizeVec(filter->lumV, 1.0);
1658 
1659  if (verbose)
1660  sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
1661  if (verbose)
1662  sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
1663 
1664  return filter;
1665 }
1666 
1668 {
1669  SwsVector *vec;
1670 
1671  if(length <= 0 || length > INT_MAX/ sizeof(double))
1672  return NULL;
1673 
1674  vec = av_malloc(sizeof(SwsVector));
1675  if (!vec)
1676  return NULL;
1677  vec->length = length;
1678  vec->coeff = av_malloc(sizeof(double) * length);
1679  if (!vec->coeff)
1680  av_freep(&vec);
1681  return vec;
1682 }
1683 
1684 SwsVector *sws_getGaussianVec(double variance, double quality)
1685 {
1686  const int length = (int)(variance * quality + 0.5) | 1;
1687  int i;
1688  double middle = (length - 1) * 0.5;
1689  SwsVector *vec;
1690 
1691  if(variance < 0 || quality < 0)
1692  return NULL;
1693 
1694  vec = sws_allocVec(length);
1695 
1696  if (!vec)
1697  return NULL;
1698 
1699  for (i = 0; i < length; i++) {
1700  double dist = i - middle;
1701  vec->coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
1702  sqrt(2 * variance * M_PI);
1703  }
1704 
1705  sws_normalizeVec(vec, 1.0);
1706 
1707  return vec;
1708 }
1709 
1711 {
1712  int i;
1713  SwsVector *vec = sws_allocVec(length);
1714 
1715  if (!vec)
1716  return NULL;
1717 
1718  for (i = 0; i < length; i++)
1719  vec->coeff[i] = c;
1720 
1721  return vec;
1722 }
1723 
1725 {
1726  return sws_getConstVec(1.0, 1);
1727 }
1728 
1729 static double sws_dcVec(SwsVector *a)
1730 {
1731  int i;
1732  double sum = 0;
1733 
1734  for (i = 0; i < a->length; i++)
1735  sum += a->coeff[i];
1736 
1737  return sum;
1738 }
1739 
1740 void sws_scaleVec(SwsVector *a, double scalar)
1741 {
1742  int i;
1743 
1744  for (i = 0; i < a->length; i++)
1745  a->coeff[i] *= scalar;
1746 }
1747 
1749 {
1750  sws_scaleVec(a, height / sws_dcVec(a));
1751 }
1752 
1754 {
1755  int length = a->length + b->length - 1;
1756  int i, j;
1757  SwsVector *vec = sws_getConstVec(0.0, length);
1758 
1759  if (!vec)
1760  return NULL;
1761 
1762  for (i = 0; i < a->length; i++) {
1763  for (j = 0; j < b->length; j++) {
1764  vec->coeff[i + j] += a->coeff[i] * b->coeff[j];
1765  }
1766  }
1767 
1768  return vec;
1769 }
1770 
1772 {
1773  int length = FFMAX(a->length, b->length);
1774  int i;
1775  SwsVector *vec = sws_getConstVec(0.0, length);
1776 
1777  if (!vec)
1778  return NULL;
1779 
1780  for (i = 0; i < a->length; i++)
1781  vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
1782  for (i = 0; i < b->length; i++)
1783  vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] += b->coeff[i];
1784 
1785  return vec;
1786 }
1787 
1789 {
1790  int length = FFMAX(a->length, b->length);
1791  int i;
1792  SwsVector *vec = sws_getConstVec(0.0, length);
1793 
1794  if (!vec)
1795  return NULL;
1796 
1797  for (i = 0; i < a->length; i++)
1798  vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
1799  for (i = 0; i < b->length; i++)
1800  vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] -= b->coeff[i];
1801 
1802  return vec;
1803 }
1804 
1805 /* shift left / or right if "shift" is negative */
1806 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
1807 {
1808  int length = a->length + FFABS(shift) * 2;
1809  int i;
1810  SwsVector *vec = sws_getConstVec(0.0, length);
1811 
1812  if (!vec)
1813  return NULL;
1814 
1815  for (i = 0; i < a->length; i++) {
1816  vec->coeff[i + (length - 1) / 2 -
1817  (a->length - 1) / 2 - shift] = a->coeff[i];
1818  }
1819 
1820  return vec;
1821 }
1822 
1823 void sws_shiftVec(SwsVector *a, int shift)
1824 {
1825  SwsVector *shifted = sws_getShiftedVec(a, shift);
1826  av_free(a->coeff);
1827  a->coeff = shifted->coeff;
1828  a->length = shifted->length;
1829  av_free(shifted);
1830 }
1831 
1833 {
1834  SwsVector *sum = sws_sumVec(a, b);
1835  av_free(a->coeff);
1836  a->coeff = sum->coeff;
1837  a->length = sum->length;
1838  av_free(sum);
1839 }
1840 
1842 {
1843  SwsVector *diff = sws_diffVec(a, b);
1844  av_free(a->coeff);
1845  a->coeff = diff->coeff;
1846  a->length = diff->length;
1847  av_free(diff);
1848 }
1849 
1851 {
1852  SwsVector *conv = sws_getConvVec(a, b);
1853  av_free(a->coeff);
1854  a->coeff = conv->coeff;
1855  a->length = conv->length;
1856  av_free(conv);
1857 }
1858 
1860 {
1861  int i;
1862  SwsVector *vec = sws_allocVec(a->length);
1863 
1864  if (!vec)
1865  return NULL;
1866 
1867  for (i = 0; i < a->length; i++)
1868  vec->coeff[i] = a->coeff[i];
1869 
1870  return vec;
1871 }
1872 
1873 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
1874 {
1875  int i;
1876  double max = 0;
1877  double min = 0;
1878  double range;
1879 
1880  for (i = 0; i < a->length; i++)
1881  if (a->coeff[i] > max)
1882  max = a->coeff[i];
1883 
1884  for (i = 0; i < a->length; i++)
1885  if (a->coeff[i] < min)
1886  min = a->coeff[i];
1887 
1888  range = max - min;
1889 
1890  for (i = 0; i < a->length; i++) {
1891  int x = (int)((a->coeff[i] - min) * 60.0 / range + 0.5);
1892  av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
1893  for (; x > 0; x--)
1894  av_log(log_ctx, log_level, " ");
1895  av_log(log_ctx, log_level, "|\n");
1896  }
1897 }
1898 
1900 {
1901  if (!a)
1902  return;
1903  av_freep(&a->coeff);
1904  a->length = 0;
1905  av_free(a);
1906 }
1907 
1909 {
1910  if (!filter)
1911  return;
1912 
1913  if (filter->lumH)
1914  sws_freeVec(filter->lumH);
1915  if (filter->lumV)
1916  sws_freeVec(filter->lumV);
1917  if (filter->chrH)
1918  sws_freeVec(filter->chrH);
1919  if (filter->chrV)
1920  sws_freeVec(filter->chrV);
1921  av_free(filter);
1922 }
1923 
1925 {
1926  int i;
1927  if (!c)
1928  return;
1929 
1930  if (c->lumPixBuf) {
1931  for (i = 0; i < c->vLumBufSize; i++)
1932  av_freep(&c->lumPixBuf[i]);
1933  av_freep(&c->lumPixBuf);
1934  }
1935 
1936  if (c->chrUPixBuf) {
1937  for (i = 0; i < c->vChrBufSize; i++)
1938  av_freep(&c->chrUPixBuf[i]);
1939  av_freep(&c->chrUPixBuf);
1940  av_freep(&c->chrVPixBuf);
1941  }
1942 
1943  if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1944  for (i = 0; i < c->vLumBufSize; i++)
1945  av_freep(&c->alpPixBuf[i]);
1946  av_freep(&c->alpPixBuf);
1947  }
1948 
1949  for (i = 0; i < 4; i++)
1950  av_freep(&c->dither_error[i]);
1951 
1952  av_freep(&c->vLumFilter);
1953  av_freep(&c->vChrFilter);
1954  av_freep(&c->hLumFilter);
1955  av_freep(&c->hChrFilter);
1956 #if HAVE_ALTIVEC
1957  av_freep(&c->vYCoeffsBank);
1958  av_freep(&c->vCCoeffsBank);
1959 #endif
1960 
1961  av_freep(&c->vLumFilterPos);
1962  av_freep(&c->vChrFilterPos);
1963  av_freep(&c->hLumFilterPos);
1964  av_freep(&c->hChrFilterPos);
1965 
1966 #if HAVE_MMX_INLINE
1967 #if USE_MMAP
1968  if (c->lumMmxextFilterCode)
1970  if (c->chrMmxextFilterCode)
1972 #elif HAVE_VIRTUALALLOC
1973  if (c->lumMmxextFilterCode)
1974  VirtualFree(c->lumMmxextFilterCode, 0, MEM_RELEASE);
1975  if (c->chrMmxextFilterCode)
1976  VirtualFree(c->chrMmxextFilterCode, 0, MEM_RELEASE);
1977 #else
1980 #endif
1981  c->lumMmxextFilterCode = NULL;
1982  c->chrMmxextFilterCode = NULL;
1983 #endif /* HAVE_MMX_INLINE */
1984 
1985  av_freep(&c->yuvTable);
1987 
1988  av_free(c);
1989 }
1990 
1991 struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW,
1992  int srcH, enum AVPixelFormat srcFormat,
1993  int dstW, int dstH,
1994  enum AVPixelFormat dstFormat, int flags,
1995  SwsFilter *srcFilter,
1996  SwsFilter *dstFilter,
1997  const double *param)
1998 {
1999  static const double default_param[2] = { SWS_PARAM_DEFAULT,
2001 
2002  if (!param)
2003  param = default_param;
2004 
2005  if (context &&
2006  (context->srcW != srcW ||
2007  context->srcH != srcH ||
2008  context->srcFormat != srcFormat ||
2009  context->dstW != dstW ||
2010  context->dstH != dstH ||
2011  context->dstFormat != dstFormat ||
2012  context->flags != flags ||
2013  context->param[0] != param[0] ||
2014  context->param[1] != param[1])) {
2015  sws_freeContext(context);
2016  context = NULL;
2017  }
2018 
2019  if (!context) {
2020  if (!(context = sws_alloc_context()))
2021  return NULL;
2022  context->srcW = srcW;
2023  context->srcH = srcH;
2024  context->srcRange = handle_jpeg(&srcFormat);
2025  context->src0Alpha = handle_0alpha(&srcFormat);
2026  context->srcXYZ = handle_xyz(&srcFormat);
2027  context->srcFormat = srcFormat;
2028  context->dstW = dstW;
2029  context->dstH = dstH;
2030  context->dstRange = handle_jpeg(&dstFormat);
2031  context->dst0Alpha = handle_0alpha(&dstFormat);
2032  context->dstXYZ = handle_xyz(&dstFormat);
2033  context->dstFormat = dstFormat;
2034  context->flags = flags;
2035  context->param[0] = param[0];
2036  context->param[1] = param[1];
2037  sws_setColorspaceDetails(context, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
2038  context->srcRange,
2039  ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/,
2040  context->dstRange, 0, 1 << 16, 1 << 16);
2041  if (sws_init_context(context, srcFilter, dstFilter) < 0) {
2042  sws_freeContext(context);
2043  return NULL;
2044  }
2045  }
2046  return context;
2047 }