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