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