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_Y400A] = { 1, 0 },
165  [AV_PIX_FMT_BGR48BE] = { 1, 1 },
166  [AV_PIX_FMT_BGR48LE] = { 1, 1 },
167  [AV_PIX_FMT_BGRA64BE] = { 1, 1, 1 },
168  [AV_PIX_FMT_BGRA64LE] = { 1, 1, 1 },
169  [AV_PIX_FMT_YUV420P9BE] = { 1, 1 },
170  [AV_PIX_FMT_YUV420P9LE] = { 1, 1 },
171  [AV_PIX_FMT_YUV420P10BE] = { 1, 1 },
172  [AV_PIX_FMT_YUV420P10LE] = { 1, 1 },
173  [AV_PIX_FMT_YUV420P12BE] = { 1, 1 },
174  [AV_PIX_FMT_YUV420P12LE] = { 1, 1 },
175  [AV_PIX_FMT_YUV420P14BE] = { 1, 1 },
176  [AV_PIX_FMT_YUV420P14LE] = { 1, 1 },
177  [AV_PIX_FMT_YUV422P9BE] = { 1, 1 },
178  [AV_PIX_FMT_YUV422P9LE] = { 1, 1 },
179  [AV_PIX_FMT_YUV422P10BE] = { 1, 1 },
180  [AV_PIX_FMT_YUV422P10LE] = { 1, 1 },
181  [AV_PIX_FMT_YUV422P12BE] = { 1, 1 },
182  [AV_PIX_FMT_YUV422P12LE] = { 1, 1 },
183  [AV_PIX_FMT_YUV422P14BE] = { 1, 1 },
184  [AV_PIX_FMT_YUV422P14LE] = { 1, 1 },
185  [AV_PIX_FMT_YUV444P9BE] = { 1, 1 },
186  [AV_PIX_FMT_YUV444P9LE] = { 1, 1 },
187  [AV_PIX_FMT_YUV444P10BE] = { 1, 1 },
188  [AV_PIX_FMT_YUV444P10LE] = { 1, 1 },
189  [AV_PIX_FMT_YUV444P12BE] = { 1, 1 },
190  [AV_PIX_FMT_YUV444P12LE] = { 1, 1 },
191  [AV_PIX_FMT_YUV444P14BE] = { 1, 1 },
192  [AV_PIX_FMT_YUV444P14LE] = { 1, 1 },
193  [AV_PIX_FMT_GBRP] = { 1, 1 },
194  [AV_PIX_FMT_GBRP9LE] = { 1, 1 },
195  [AV_PIX_FMT_GBRP9BE] = { 1, 1 },
196  [AV_PIX_FMT_GBRP10LE] = { 1, 1 },
197  [AV_PIX_FMT_GBRP10BE] = { 1, 1 },
198  [AV_PIX_FMT_GBRP12LE] = { 1, 1 },
199  [AV_PIX_FMT_GBRP12BE] = { 1, 1 },
200  [AV_PIX_FMT_GBRP14LE] = { 1, 1 },
201  [AV_PIX_FMT_GBRP14BE] = { 1, 1 },
202  [AV_PIX_FMT_GBRP16LE] = { 1, 0 },
203  [AV_PIX_FMT_GBRP16BE] = { 1, 0 },
204  [AV_PIX_FMT_XYZ12BE] = { 1, 1, 1 },
205  [AV_PIX_FMT_XYZ12LE] = { 1, 1, 1 },
206  [AV_PIX_FMT_GBRAP] = { 1, 1 },
207  [AV_PIX_FMT_GBRAP16LE] = { 1, 0 },
208  [AV_PIX_FMT_GBRAP16BE] = { 1, 0 },
209  [AV_PIX_FMT_BAYER_BGGR8] = { 1, 0 },
210  [AV_PIX_FMT_BAYER_RGGB8] = { 1, 0 },
211  [AV_PIX_FMT_BAYER_GBRG8] = { 1, 0 },
212  [AV_PIX_FMT_BAYER_GRBG8] = { 1, 0 },
213  [AV_PIX_FMT_BAYER_BGGR16LE] = { 1, 0 },
214  [AV_PIX_FMT_BAYER_BGGR16BE] = { 1, 0 },
215  [AV_PIX_FMT_BAYER_RGGB16LE] = { 1, 0 },
216  [AV_PIX_FMT_BAYER_RGGB16BE] = { 1, 0 },
217  [AV_PIX_FMT_BAYER_GBRG16LE] = { 1, 0 },
218  [AV_PIX_FMT_BAYER_GBRG16BE] = { 1, 0 },
219  [AV_PIX_FMT_BAYER_GRBG16LE] = { 1, 0 },
220  [AV_PIX_FMT_BAYER_GRBG16BE] = { 1, 0 },
221 };
222 
224 {
225  return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
226  format_entries[pix_fmt].is_supported_in : 0;
227 }
228 
230 {
231  return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
232  format_entries[pix_fmt].is_supported_out : 0;
233 }
234 
236 {
237  return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
238  format_entries[pix_fmt].is_supported_endianness : 0;
239 }
240 
241 #if FF_API_SWS_FORMAT_NAME
242 const char *sws_format_name(enum AVPixelFormat format)
243 {
244  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
245  if (desc)
246  return desc->name;
247  else
248  return "Unknown format";
249 }
250 #endif
251 
252 static double getSplineCoeff(double a, double b, double c, double d,
253  double dist)
254 {
255  if (dist <= 1.0)
256  return ((d * dist + c) * dist + b) * dist + a;
257  else
258  return getSplineCoeff(0.0,
259  b + 2.0 * c + 3.0 * d,
260  c + 3.0 * d,
261  -b - 3.0 * c - 6.0 * d,
262  dist - 1.0);
263 }
264 
265 static av_cold int get_local_pos(SwsContext *s, int chr_subsample, int pos, int dir)
266 {
267  if (pos < 0) {
268  pos = (128 << chr_subsample) - 128;
269  }
270  pos += 128; // relative to ideal left edge
271  return pos >> chr_subsample;
272 }
273 
274 typedef struct {
275  int flag; ///< flag associated to the algorithm
276  const char *description; ///< human-readable description
277  int size_factor; ///< size factor used when initing the filters
279 
281  { SWS_AREA, "area averaging", 1 /* downscale only, for upscale it is bilinear */ },
282  { SWS_BICUBIC, "bicubic", 4 },
283  { SWS_BICUBLIN, "luma bicubic / chroma bilinear", -1 },
284  { SWS_BILINEAR, "bilinear", 2 },
285  { SWS_FAST_BILINEAR, "fast bilinear", -1 },
286  { SWS_GAUSS, "Gaussian", 8 /* infinite ;) */ },
287  { SWS_LANCZOS, "Lanczos", -1 /* custom */ },
288  { SWS_POINT, "nearest neighbor / point", -1 },
289  { SWS_SINC, "sinc", 20 /* infinite ;) */ },
290  { SWS_SPLINE, "bicubic spline", 20 /* infinite :)*/ },
291  { SWS_X, "experimental", 8 },
292 };
293 
294 static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
295  int *outFilterSize, int xInc, int srcW,
296  int dstW, int filterAlign, int one,
297  int flags, int cpu_flags,
298  SwsVector *srcFilter, SwsVector *dstFilter,
299  double param[2], int srcPos, int dstPos)
300 {
301  int i;
302  int filterSize;
303  int filter2Size;
304  int minFilterSize;
305  int64_t *filter = NULL;
306  int64_t *filter2 = NULL;
307  const int64_t fone = 1LL << (54 - FFMIN(av_log2(srcW/dstW), 8));
308  int ret = -1;
309 
310  emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
311 
312  // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
313  FF_ALLOC_ARRAY_OR_GOTO(NULL, *filterPos, (dstW + 3), sizeof(**filterPos), fail);
314 
315  if (FFABS(xInc - 0x10000) < 10 && srcPos == dstPos) { // unscaled
316  int i;
317  filterSize = 1;
318  FF_ALLOCZ_ARRAY_OR_GOTO(NULL, filter,
319  dstW, sizeof(*filter) * filterSize, fail);
320 
321  for (i = 0; i < dstW; i++) {
322  filter[i * filterSize] = fone;
323  (*filterPos)[i] = i;
324  }
325  } else if (flags & SWS_POINT) { // lame looking point sampling mode
326  int i;
327  int64_t xDstInSrc;
328  filterSize = 1;
329  FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
330  dstW, sizeof(*filter) * filterSize, fail);
331 
332  xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
333  for (i = 0; i < dstW; i++) {
334  int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
335 
336  (*filterPos)[i] = xx;
337  filter[i] = fone;
338  xDstInSrc += xInc;
339  }
340  } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
341  (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
342  int i;
343  int64_t xDstInSrc;
344  filterSize = 2;
345  FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
346  dstW, sizeof(*filter) * filterSize, fail);
347 
348  xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
349  for (i = 0; i < dstW; i++) {
350  int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
351  int j;
352 
353  (*filterPos)[i] = xx;
354  // bilinear upscale / linear interpolate / area averaging
355  for (j = 0; j < filterSize; j++) {
356  int64_t coeff= fone - FFABS(((int64_t)xx<<16) - xDstInSrc)*(fone>>16);
357  if (coeff < 0)
358  coeff = 0;
359  filter[i * filterSize + j] = coeff;
360  xx++;
361  }
362  xDstInSrc += xInc;
363  }
364  } else {
365  int64_t xDstInSrc;
366  int sizeFactor = -1;
367 
368  for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
369  if (flags & scale_algorithms[i].flag && scale_algorithms[i].size_factor > 0) {
370  sizeFactor = scale_algorithms[i].size_factor;
371  break;
372  }
373  }
374  if (flags & SWS_LANCZOS)
375  sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
376  av_assert0(sizeFactor > 0);
377 
378  if (xInc <= 1 << 16)
379  filterSize = 1 + sizeFactor; // upscale
380  else
381  filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
382 
383  filterSize = FFMIN(filterSize, srcW - 2);
384  filterSize = FFMAX(filterSize, 1);
385 
386  FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
387  dstW, sizeof(*filter) * filterSize, fail);
388 
389  xDstInSrc = ((dstPos*(int64_t)xInc)>>7) - ((srcPos*0x10000LL)>>7);
390  for (i = 0; i < dstW; i++) {
391  int xx = (xDstInSrc - ((int64_t)(filterSize - 2) << 16)) / (1 << 17);
392  int j;
393  (*filterPos)[i] = xx;
394  for (j = 0; j < filterSize; j++) {
395  int64_t d = (FFABS(((int64_t)xx << 17) - xDstInSrc)) << 13;
396  double floatd;
397  int64_t coeff;
398 
399  if (xInc > 1 << 16)
400  d = d * dstW / srcW;
401  floatd = d * (1.0 / (1 << 30));
402 
403  if (flags & SWS_BICUBIC) {
404  int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1 << 24);
405  int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
406 
407  if (d >= 1LL << 31) {
408  coeff = 0.0;
409  } else {
410  int64_t dd = (d * d) >> 30;
411  int64_t ddd = (dd * d) >> 30;
412 
413  if (d < 1LL << 30)
414  coeff = (12 * (1 << 24) - 9 * B - 6 * C) * ddd +
415  (-18 * (1 << 24) + 12 * B + 6 * C) * dd +
416  (6 * (1 << 24) - 2 * B) * (1 << 30);
417  else
418  coeff = (-B - 6 * C) * ddd +
419  (6 * B + 30 * C) * dd +
420  (-12 * B - 48 * C) * d +
421  (8 * B + 24 * C) * (1 << 30);
422  }
423  coeff /= (1LL<<54)/fone;
424  }
425 #if 0
426  else if (flags & SWS_X) {
427  double p = param ? param * 0.01 : 0.3;
428  coeff = d ? sin(d * M_PI) / (d * M_PI) : 1.0;
429  coeff *= pow(2.0, -p * d * d);
430  }
431 #endif
432  else if (flags & SWS_X) {
433  double A = param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
434  double c;
435 
436  if (floatd < 1.0)
437  c = cos(floatd * M_PI);
438  else
439  c = -1.0;
440  if (c < 0.0)
441  c = -pow(-c, A);
442  else
443  c = pow(c, A);
444  coeff = (c * 0.5 + 0.5) * fone;
445  } else if (flags & SWS_AREA) {
446  int64_t d2 = d - (1 << 29);
447  if (d2 * xInc < -(1LL << (29 + 16)))
448  coeff = 1.0 * (1LL << (30 + 16));
449  else if (d2 * xInc < (1LL << (29 + 16)))
450  coeff = -d2 * xInc + (1LL << (29 + 16));
451  else
452  coeff = 0.0;
453  coeff *= fone >> (30 + 16);
454  } else if (flags & SWS_GAUSS) {
455  double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
456  coeff = (pow(2.0, -p * floatd * floatd)) * fone;
457  } else if (flags & SWS_SINC) {
458  coeff = (d ? sin(floatd * M_PI) / (floatd * M_PI) : 1.0) * fone;
459  } else if (flags & SWS_LANCZOS) {
460  double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
461  coeff = (d ? sin(floatd * M_PI) * sin(floatd * M_PI / p) /
462  (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
463  if (floatd > p)
464  coeff = 0;
465  } else if (flags & SWS_BILINEAR) {
466  coeff = (1 << 30) - d;
467  if (coeff < 0)
468  coeff = 0;
469  coeff *= fone >> 30;
470  } else if (flags & SWS_SPLINE) {
471  double p = -2.196152422706632;
472  coeff = getSplineCoeff(1.0, 0.0, p, -p - 1.0, floatd) * fone;
473  } else {
474  av_assert0(0);
475  }
476 
477  filter[i * filterSize + j] = coeff;
478  xx++;
479  }
480  xDstInSrc += 2 * xInc;
481  }
482  }
483 
484  /* apply src & dst Filter to filter -> filter2
485  * av_free(filter);
486  */
487  av_assert0(filterSize > 0);
488  filter2Size = filterSize;
489  if (srcFilter)
490  filter2Size += srcFilter->length - 1;
491  if (dstFilter)
492  filter2Size += dstFilter->length - 1;
493  av_assert0(filter2Size > 0);
494  FF_ALLOCZ_ARRAY_OR_GOTO(NULL, filter2, dstW, filter2Size * sizeof(*filter2), fail);
495 
496  for (i = 0; i < dstW; i++) {
497  int j, k;
498 
499  if (srcFilter) {
500  for (k = 0; k < srcFilter->length; k++) {
501  for (j = 0; j < filterSize; j++)
502  filter2[i * filter2Size + k + j] +=
503  srcFilter->coeff[k] * filter[i * filterSize + j];
504  }
505  } else {
506  for (j = 0; j < filterSize; j++)
507  filter2[i * filter2Size + j] = filter[i * filterSize + j];
508  }
509  // FIXME dstFilter
510 
511  (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
512  }
513  av_freep(&filter);
514 
515  /* try to reduce the filter-size (step1 find size and shift left) */
516  // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
517  minFilterSize = 0;
518  for (i = dstW - 1; i >= 0; i--) {
519  int min = filter2Size;
520  int j;
521  int64_t cutOff = 0.0;
522 
523  /* get rid of near zero elements on the left by shifting left */
524  for (j = 0; j < filter2Size; j++) {
525  int k;
526  cutOff += FFABS(filter2[i * filter2Size]);
527 
528  if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
529  break;
530 
531  /* preserve monotonicity because the core can't handle the
532  * filter otherwise */
533  if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
534  break;
535 
536  // move filter coefficients left
537  for (k = 1; k < filter2Size; k++)
538  filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
539  filter2[i * filter2Size + k - 1] = 0;
540  (*filterPos)[i]++;
541  }
542 
543  cutOff = 0;
544  /* count near zeros on the right */
545  for (j = filter2Size - 1; j > 0; j--) {
546  cutOff += FFABS(filter2[i * filter2Size + j]);
547 
548  if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
549  break;
550  min--;
551  }
552 
553  if (min > minFilterSize)
554  minFilterSize = min;
555  }
556 
557  if (PPC_ALTIVEC(cpu_flags)) {
558  // we can handle the special case 4, so we don't want to go the full 8
559  if (minFilterSize < 5)
560  filterAlign = 4;
561 
562  /* We really don't want to waste our time doing useless computation, so
563  * fall back on the scalar C code for very small filters.
564  * Vectorizing is worth it only if you have a decent-sized vector. */
565  if (minFilterSize < 3)
566  filterAlign = 1;
567  }
568 
569  if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
570  // special case for unscaled vertical filtering
571  if (minFilterSize == 1 && filterAlign == 2)
572  filterAlign = 1;
573  }
574 
575  av_assert0(minFilterSize > 0);
576  filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
577  av_assert0(filterSize > 0);
578  filter = av_malloc_array(dstW, filterSize * sizeof(*filter));
579  if (!filter)
580  goto fail;
581  if (filterSize >= MAX_FILTER_SIZE * 16 /
582  ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16)) {
583  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",
584  FF_CEIL_RSHIFT((filterSize+1) * ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16), 4));
585  goto fail;
586  }
587  *outFilterSize = filterSize;
588 
589  if (flags & SWS_PRINT_INFO)
590  av_log(NULL, AV_LOG_VERBOSE,
591  "SwScaler: reducing / aligning filtersize %d -> %d\n",
592  filter2Size, filterSize);
593  /* try to reduce the filter-size (step2 reduce it) */
594  for (i = 0; i < dstW; i++) {
595  int j;
596 
597  for (j = 0; j < filterSize; j++) {
598  if (j >= filter2Size)
599  filter[i * filterSize + j] = 0;
600  else
601  filter[i * filterSize + j] = filter2[i * filter2Size + j];
602  if ((flags & SWS_BITEXACT) && j >= minFilterSize)
603  filter[i * filterSize + j] = 0;
604  }
605  }
606 
607  // FIXME try to align filterPos if possible
608 
609  // fix borders
610  for (i = 0; i < dstW; i++) {
611  int j;
612  if ((*filterPos)[i] < 0) {
613  // move filter coefficients left to compensate for filterPos
614  for (j = 1; j < filterSize; j++) {
615  int left = FFMAX(j + (*filterPos)[i], 0);
616  filter[i * filterSize + left] += filter[i * filterSize + j];
617  filter[i * filterSize + j] = 0;
618  }
619  (*filterPos)[i]= 0;
620  }
621 
622  if ((*filterPos)[i] + filterSize > srcW) {
623  int shift = (*filterPos)[i] + filterSize - srcW;
624  // move filter coefficients right to compensate for filterPos
625  for (j = filterSize - 2; j >= 0; j--) {
626  int right = FFMIN(j + shift, filterSize - 1);
627  filter[i * filterSize + right] += filter[i * filterSize + j];
628  filter[i * filterSize + j] = 0;
629  }
630  (*filterPos)[i]= srcW - filterSize;
631  }
632  }
633 
634  // Note the +1 is for the MMX scaler which reads over the end
635  /* align at 16 for AltiVec (needed by hScale_altivec_real) */
636  FF_ALLOCZ_ARRAY_OR_GOTO(NULL, *outFilter,
637  (dstW + 3), *outFilterSize * sizeof(int16_t), fail);
638 
639  /* normalize & store in outFilter */
640  for (i = 0; i < dstW; i++) {
641  int j;
642  int64_t error = 0;
643  int64_t sum = 0;
644 
645  for (j = 0; j < filterSize; j++) {
646  sum += filter[i * filterSize + j];
647  }
648  sum = (sum + one / 2) / one;
649  if (!sum) {
650  av_log(NULL, AV_LOG_WARNING, "SwScaler: zero vector in scaling\n");
651  sum = 1;
652  }
653  for (j = 0; j < *outFilterSize; j++) {
654  int64_t v = filter[i * filterSize + j] + error;
655  int intV = ROUNDED_DIV(v, sum);
656  (*outFilter)[i * (*outFilterSize) + j] = intV;
657  error = v - intV * sum;
658  }
659  }
660 
661  (*filterPos)[dstW + 0] =
662  (*filterPos)[dstW + 1] =
663  (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1]; /* the MMX/SSE scaler will
664  * read over the end */
665  for (i = 0; i < *outFilterSize; i++) {
666  int k = (dstW - 1) * (*outFilterSize) + i;
667  (*outFilter)[k + 1 * (*outFilterSize)] =
668  (*outFilter)[k + 2 * (*outFilterSize)] =
669  (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
670  }
671 
672  ret = 0;
673 
674 fail:
675  if(ret < 0)
676  av_log(NULL, AV_LOG_ERROR, "sws: initFilter failed\n");
677  av_free(filter);
678  av_free(filter2);
679  return ret;
680 }
681 
682 static void fill_rgb2yuv_table(SwsContext *c, const int table[4], int dstRange)
683 {
684  int64_t W, V, Z, Cy, Cu, Cv;
685  int64_t vr = table[0];
686  int64_t ub = table[1];
687  int64_t ug = -table[2];
688  int64_t vg = -table[3];
689  int64_t ONE = 65536;
690  int64_t cy = ONE;
692  int i;
693  static const int8_t map[] = {
694  BY_IDX, GY_IDX, -1 , BY_IDX, BY_IDX, GY_IDX, -1 , BY_IDX,
695  RY_IDX, -1 , GY_IDX, RY_IDX, RY_IDX, -1 , GY_IDX, RY_IDX,
696  RY_IDX, GY_IDX, -1 , RY_IDX, RY_IDX, GY_IDX, -1 , RY_IDX,
697  BY_IDX, -1 , GY_IDX, BY_IDX, BY_IDX, -1 , GY_IDX, BY_IDX,
698  BU_IDX, GU_IDX, -1 , BU_IDX, BU_IDX, GU_IDX, -1 , BU_IDX,
699  RU_IDX, -1 , GU_IDX, RU_IDX, RU_IDX, -1 , GU_IDX, RU_IDX,
700  RU_IDX, GU_IDX, -1 , RU_IDX, RU_IDX, GU_IDX, -1 , RU_IDX,
701  BU_IDX, -1 , GU_IDX, BU_IDX, BU_IDX, -1 , GU_IDX, BU_IDX,
702  BV_IDX, GV_IDX, -1 , BV_IDX, BV_IDX, GV_IDX, -1 , BV_IDX,
703  RV_IDX, -1 , GV_IDX, RV_IDX, RV_IDX, -1 , GV_IDX, RV_IDX,
704  RV_IDX, GV_IDX, -1 , RV_IDX, RV_IDX, GV_IDX, -1 , RV_IDX,
705  BV_IDX, -1 , GV_IDX, BV_IDX, BV_IDX, -1 , GV_IDX, BV_IDX,
708  GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 ,
709  -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX,
712  GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 ,
713  -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX,
716  GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 ,
717  -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, //23
718  -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //24
719  -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //25
720  -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //26
721  -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //27
722  -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //28
723  -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //29
724  -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //30
725  -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //31
726  BY_IDX, GY_IDX, RY_IDX, -1 , -1 , -1 , -1 , -1 , //32
727  BU_IDX, GU_IDX, RU_IDX, -1 , -1 , -1 , -1 , -1 , //33
728  BV_IDX, GV_IDX, RV_IDX, -1 , -1 , -1 , -1 , -1 , //34
729  };
730 
731  dstRange = 0; //FIXME range = 1 is handled elsewhere
732 
733  if (!dstRange) {
734  cy = cy * 255 / 219;
735  } else {
736  vr = vr * 224 / 255;
737  ub = ub * 224 / 255;
738  ug = ug * 224 / 255;
739  vg = vg * 224 / 255;
740  }
741  W = ROUNDED_DIV(ONE*ONE*ug, ub);
742  V = ROUNDED_DIV(ONE*ONE*vg, vr);
743  Z = ONE*ONE-W-V;
744 
745  Cy = ROUNDED_DIV(cy*Z, ONE);
746  Cu = ROUNDED_DIV(ub*Z, ONE);
747  Cv = ROUNDED_DIV(vr*Z, ONE);
748 
749  c->input_rgb2yuv_table[RY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cy);
750  c->input_rgb2yuv_table[GY_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cy);
751  c->input_rgb2yuv_table[BY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cy);
752 
753  c->input_rgb2yuv_table[RU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cu);
754  c->input_rgb2yuv_table[GU_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cu);
755  c->input_rgb2yuv_table[BU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(Z+W) , Cu);
756 
757  c->input_rgb2yuv_table[RV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(V+Z) , Cv);
758  c->input_rgb2yuv_table[GV_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cv);
759  c->input_rgb2yuv_table[BV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cv);
760 
761  if(/*!dstRange && */!memcmp(table, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], sizeof(ff_yuv2rgb_coeffs[SWS_CS_DEFAULT]))) {
762  c->input_rgb2yuv_table[BY_IDX] = ((int)(0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
763  c->input_rgb2yuv_table[BV_IDX] = (-(int)(0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
764  c->input_rgb2yuv_table[BU_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
765  c->input_rgb2yuv_table[GY_IDX] = ((int)(0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
766  c->input_rgb2yuv_table[GV_IDX] = (-(int)(0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
767  c->input_rgb2yuv_table[GU_IDX] = (-(int)(0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
768  c->input_rgb2yuv_table[RY_IDX] = ((int)(0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
769  c->input_rgb2yuv_table[RV_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
770  c->input_rgb2yuv_table[RU_IDX] = (-(int)(0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
771  }
772  for(i=0; i<FF_ARRAY_ELEMS(map); i++)
773  AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ? c->input_rgb2yuv_table[map[i]] : 0);
774 }
775 
776 static void fill_xyztables(struct SwsContext *c)
777 {
778  int i;
779  double xyzgamma = XYZ_GAMMA;
780  double rgbgamma = 1.0 / RGB_GAMMA;
781  double xyzgammainv = 1.0 / XYZ_GAMMA;
782  double rgbgammainv = RGB_GAMMA;
783  static const int16_t xyz2rgb_matrix[3][4] = {
784  {13270, -6295, -2041},
785  {-3969, 7682, 170},
786  { 228, -835, 4329} };
787  static const int16_t rgb2xyz_matrix[3][4] = {
788  {1689, 1464, 739},
789  { 871, 2929, 296},
790  { 79, 488, 3891} };
791  static int16_t xyzgamma_tab[4096], rgbgamma_tab[4096], xyzgammainv_tab[4096], rgbgammainv_tab[4096];
792 
793  memcpy(c->xyz2rgb_matrix, xyz2rgb_matrix, sizeof(c->xyz2rgb_matrix));
794  memcpy(c->rgb2xyz_matrix, rgb2xyz_matrix, sizeof(c->rgb2xyz_matrix));
795  c->xyzgamma = xyzgamma_tab;
796  c->rgbgamma = rgbgamma_tab;
797  c->xyzgammainv = xyzgammainv_tab;
798  c->rgbgammainv = rgbgammainv_tab;
799 
800  if (rgbgamma_tab[4095])
801  return;
802 
803  /* set gamma vectors */
804  for (i = 0; i < 4096; i++) {
805  xyzgamma_tab[i] = lrint(pow(i / 4095.0, xyzgamma) * 4095.0);
806  rgbgamma_tab[i] = lrint(pow(i / 4095.0, rgbgamma) * 4095.0);
807  xyzgammainv_tab[i] = lrint(pow(i / 4095.0, xyzgammainv) * 4095.0);
808  rgbgammainv_tab[i] = lrint(pow(i / 4095.0, rgbgammainv) * 4095.0);
809  }
810 }
811 
812 int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
813  int srcRange, const int table[4], int dstRange,
814  int brightness, int contrast, int saturation)
815 {
816  const AVPixFmtDescriptor *desc_dst;
817  const AVPixFmtDescriptor *desc_src;
818  int need_reinit = 0;
819  memmove(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
820  memmove(c->dstColorspaceTable, table, sizeof(int) * 4);
821 
822  handle_formats(c);
823  desc_dst = av_pix_fmt_desc_get(c->dstFormat);
824  desc_src = av_pix_fmt_desc_get(c->srcFormat);
825 
826  if(!isYUV(c->dstFormat) && !isGray(c->dstFormat))
827  dstRange = 0;
828  if(!isYUV(c->srcFormat) && !isGray(c->srcFormat))
829  srcRange = 0;
830 
831  c->brightness = brightness;
832  c->contrast = contrast;
833  c->saturation = saturation;
834  if (c->srcRange != srcRange || c->dstRange != dstRange)
835  need_reinit = 1;
836  c->srcRange = srcRange;
837  c->dstRange = dstRange;
838 
839  //The srcBpc check is possibly wrong but we seem to lack a definitive reference to test this
840  //and what we have in ticket 2939 looks better with this check
841  if (need_reinit && (c->srcBpc == 8 || !isYUV(c->srcFormat)))
843 
844  if ((isYUV(c->dstFormat) || isGray(c->dstFormat)) && (isYUV(c->srcFormat) || isGray(c->srcFormat)))
845  return -1;
846 
847  c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
848  c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
849 
850  if (!isYUV(c->dstFormat) && !isGray(c->dstFormat)) {
851  ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
852  contrast, saturation);
853  // FIXME factorize
854 
855  if (ARCH_PPC)
856  ff_yuv2rgb_init_tables_ppc(c, inv_table, brightness,
857  contrast, saturation);
858  }
859 
860  fill_rgb2yuv_table(c, table, dstRange);
861 
862  return 0;
863 }
864 
865 int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
866  int *srcRange, int **table, int *dstRange,
867  int *brightness, int *contrast, int *saturation)
868 {
869  if (!c )
870  return -1;
871 
872  *inv_table = c->srcColorspaceTable;
873  *table = c->dstColorspaceTable;
874  *srcRange = c->srcRange;
875  *dstRange = c->dstRange;
876  *brightness = c->brightness;
877  *contrast = c->contrast;
878  *saturation = c->saturation;
879 
880  return 0;
881 }
882 
883 static int handle_jpeg(enum AVPixelFormat *format)
884 {
885  switch (*format) {
886  case AV_PIX_FMT_YUVJ420P:
887  *format = AV_PIX_FMT_YUV420P;
888  return 1;
889  case AV_PIX_FMT_YUVJ411P:
890  *format = AV_PIX_FMT_YUV411P;
891  return 1;
892  case AV_PIX_FMT_YUVJ422P:
893  *format = AV_PIX_FMT_YUV422P;
894  return 1;
895  case AV_PIX_FMT_YUVJ444P:
896  *format = AV_PIX_FMT_YUV444P;
897  return 1;
898  case AV_PIX_FMT_YUVJ440P:
899  *format = AV_PIX_FMT_YUV440P;
900  return 1;
901  case AV_PIX_FMT_GRAY8:
902  case AV_PIX_FMT_GRAY16LE:
903  case AV_PIX_FMT_GRAY16BE:
904  return 1;
905  default:
906  return 0;
907  }
908 }
909 
910 static int handle_0alpha(enum AVPixelFormat *format)
911 {
912  switch (*format) {
913  case AV_PIX_FMT_0BGR : *format = AV_PIX_FMT_ABGR ; return 1;
914  case AV_PIX_FMT_BGR0 : *format = AV_PIX_FMT_BGRA ; return 4;
915  case AV_PIX_FMT_0RGB : *format = AV_PIX_FMT_ARGB ; return 1;
916  case AV_PIX_FMT_RGB0 : *format = AV_PIX_FMT_RGBA ; return 4;
917  default: return 0;
918  }
919 }
920 
921 static int handle_xyz(enum AVPixelFormat *format)
922 {
923  switch (*format) {
924  case AV_PIX_FMT_XYZ12BE : *format = AV_PIX_FMT_RGB48BE; return 1;
925  case AV_PIX_FMT_XYZ12LE : *format = AV_PIX_FMT_RGB48LE; return 1;
926  default: return 0;
927  }
928 }
929 
931 {
932  c->src0Alpha |= handle_0alpha(&c->srcFormat);
933  c->dst0Alpha |= handle_0alpha(&c->dstFormat);
934  c->srcXYZ |= handle_xyz(&c->srcFormat);
935  c->dstXYZ |= handle_xyz(&c->dstFormat);
936  if (c->srcXYZ || c->dstXYZ)
937  fill_xyztables(c);
938 }
939 
941 {
942  SwsContext *c = av_mallocz(sizeof(SwsContext));
943 
944  av_assert0(offsetof(SwsContext, redDither) + DITHER32_INT == offsetof(SwsContext, dither32));
945 
946  if (c) {
949  }
950 
951  return c;
952 }
953 
955  SwsFilter *dstFilter)
956 {
957  int i, j;
958  int usesVFilter, usesHFilter;
959  int unscaled;
960  SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
961  int srcW = c->srcW;
962  int srcH = c->srcH;
963  int dstW = c->dstW;
964  int dstH = c->dstH;
965  int dst_stride = FFALIGN(dstW * sizeof(int16_t) + 66, 16);
966  int flags, cpu_flags;
967  enum AVPixelFormat srcFormat = c->srcFormat;
968  enum AVPixelFormat dstFormat = c->dstFormat;
969  const AVPixFmtDescriptor *desc_src;
970  const AVPixFmtDescriptor *desc_dst;
971 
972  cpu_flags = av_get_cpu_flags();
973  flags = c->flags;
974  emms_c();
975  if (!rgb15to16)
977 
978  unscaled = (srcW == dstW && srcH == dstH);
979 
980  c->srcRange |= handle_jpeg(&c->srcFormat);
981  c->dstRange |= handle_jpeg(&c->dstFormat);
982 
983  if(srcFormat!=c->srcFormat || dstFormat!=c->dstFormat)
984  av_log(c, AV_LOG_WARNING, "deprecated pixel format used, make sure you did set range correctly\n");
985 
986  if (!c->contrast && !c->saturation && !c->dstFormatBpp)
987  sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
988  ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
989  c->dstRange, 0, 1 << 16, 1 << 16);
990 
991  handle_formats(c);
992  srcFormat = c->srcFormat;
993  dstFormat = c->dstFormat;
994  desc_src = av_pix_fmt_desc_get(srcFormat);
995  desc_dst = av_pix_fmt_desc_get(dstFormat);
996 
997  if (!(unscaled && sws_isSupportedEndiannessConversion(srcFormat) &&
998  av_pix_fmt_swap_endianness(srcFormat) == dstFormat)) {
999  if (!sws_isSupportedInput(srcFormat)) {
1000  av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
1001  av_get_pix_fmt_name(srcFormat));
1002  return AVERROR(EINVAL);
1003  }
1004  if (!sws_isSupportedOutput(dstFormat)) {
1005  av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
1006  av_get_pix_fmt_name(dstFormat));
1007  return AVERROR(EINVAL);
1008  }
1009  }
1010 
1011  i = flags & (SWS_POINT |
1012  SWS_AREA |
1013  SWS_BILINEAR |
1015  SWS_BICUBIC |
1016  SWS_X |
1017  SWS_GAUSS |
1018  SWS_LANCZOS |
1019  SWS_SINC |
1020  SWS_SPLINE |
1021  SWS_BICUBLIN);
1022 
1023  /* provide a default scaler if not set by caller */
1024  if (!i) {
1025  if (dstW < srcW && dstH < srcH)
1026  flags |= SWS_BICUBIC;
1027  else if (dstW > srcW && dstH > srcH)
1028  flags |= SWS_BICUBIC;
1029  else
1030  flags |= SWS_BICUBIC;
1031  c->flags = flags;
1032  } else if (i & (i - 1)) {
1033  av_log(c, AV_LOG_ERROR,
1034  "Exactly one scaler algorithm must be chosen, got %X\n", i);
1035  return AVERROR(EINVAL);
1036  }
1037  /* sanity check */
1038  if (srcW < 1 || srcH < 1 || dstW < 1 || dstH < 1) {
1039  /* FIXME check if these are enough and try to lower them after
1040  * fixing the relevant parts of the code */
1041  av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
1042  srcW, srcH, dstW, dstH);
1043  return AVERROR(EINVAL);
1044  }
1045 
1046  if (!dstFilter)
1047  dstFilter = &dummyFilter;
1048  if (!srcFilter)
1049  srcFilter = &dummyFilter;
1050 
1051  c->lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
1052  c->lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
1053  c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
1054  c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
1055  c->vRounder = 4 * 0x0001000100010001ULL;
1056 
1057  usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
1058  (srcFilter->chrV && srcFilter->chrV->length > 1) ||
1059  (dstFilter->lumV && dstFilter->lumV->length > 1) ||
1060  (dstFilter->chrV && dstFilter->chrV->length > 1);
1061  usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
1062  (srcFilter->chrH && srcFilter->chrH->length > 1) ||
1063  (dstFilter->lumH && dstFilter->lumH->length > 1) ||
1064  (dstFilter->chrH && dstFilter->chrH->length > 1);
1065 
1068 
1069  if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) {
1070  if (dstW&1) {
1071  av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to odd output size\n");
1072  flags |= SWS_FULL_CHR_H_INT;
1073  c->flags = flags;
1074  }
1075 
1076  if ( c->chrSrcHSubSample == 0
1077  && c->chrSrcVSubSample == 0
1078  && c->dither != SWS_DITHER_BAYER //SWS_FULL_CHR_H_INT is currently not supported with SWS_DITHER_BAYER
1079  && !(c->flags & SWS_FAST_BILINEAR)
1080  ) {
1081  av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to input having non subsampled chroma\n");
1082  flags |= SWS_FULL_CHR_H_INT;
1083  c->flags = flags;
1084  }
1085  }
1086 
1087  if (c->dither == SWS_DITHER_AUTO) {
1088  if (flags & SWS_ERROR_DIFFUSION)
1089  c->dither = SWS_DITHER_ED;
1090  }
1091 
1092  if(dstFormat == AV_PIX_FMT_BGR4_BYTE ||
1093  dstFormat == AV_PIX_FMT_RGB4_BYTE ||
1094  dstFormat == AV_PIX_FMT_BGR8 ||
1095  dstFormat == AV_PIX_FMT_RGB8) {
1096  if (c->dither == SWS_DITHER_AUTO)
1098  if (!(flags & SWS_FULL_CHR_H_INT)) {
1100  av_log(c, AV_LOG_DEBUG,
1101  "Desired dithering only supported in full chroma interpolation for destination format '%s'\n",
1102  av_get_pix_fmt_name(dstFormat));
1103  flags |= SWS_FULL_CHR_H_INT;
1104  c->flags = flags;
1105  }
1106  }
1107  if (flags & SWS_FULL_CHR_H_INT) {
1108  if (c->dither == SWS_DITHER_BAYER) {
1109  av_log(c, AV_LOG_DEBUG,
1110  "Ordered dither is not supported in full chroma interpolation for destination format '%s'\n",
1111  av_get_pix_fmt_name(dstFormat));
1112  c->dither = SWS_DITHER_ED;
1113  }
1114  }
1115  }
1116  if (isPlanarRGB(dstFormat)) {
1117  if (!(flags & SWS_FULL_CHR_H_INT)) {
1118  av_log(c, AV_LOG_DEBUG,
1119  "%s output is not supported with half chroma resolution, switching to full\n",
1120  av_get_pix_fmt_name(dstFormat));
1121  flags |= SWS_FULL_CHR_H_INT;
1122  c->flags = flags;
1123  }
1124  }
1125 
1126  /* reuse chroma for 2 pixels RGB/BGR unless user wants full
1127  * chroma interpolation */
1128  if (flags & SWS_FULL_CHR_H_INT &&
1129  isAnyRGB(dstFormat) &&
1130  !isPlanarRGB(dstFormat) &&
1131  dstFormat != AV_PIX_FMT_RGBA &&
1132  dstFormat != AV_PIX_FMT_ARGB &&
1133  dstFormat != AV_PIX_FMT_BGRA &&
1134  dstFormat != AV_PIX_FMT_ABGR &&
1135  dstFormat != AV_PIX_FMT_RGB24 &&
1136  dstFormat != AV_PIX_FMT_BGR24 &&
1137  dstFormat != AV_PIX_FMT_BGR4_BYTE &&
1138  dstFormat != AV_PIX_FMT_RGB4_BYTE &&
1139  dstFormat != AV_PIX_FMT_BGR8 &&
1140  dstFormat != AV_PIX_FMT_RGB8
1141  ) {
1143  "full chroma interpolation for destination format '%s' not yet implemented\n",
1144  av_get_pix_fmt_name(dstFormat));
1145  flags &= ~SWS_FULL_CHR_H_INT;
1146  c->flags = flags;
1147  }
1148  if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
1149  c->chrDstHSubSample = 1;
1150 
1151  // drop some chroma lines if the user wants it
1152  c->vChrDrop = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
1154  c->chrSrcVSubSample += c->vChrDrop;
1155 
1156  /* drop every other pixel for chroma calculation unless user
1157  * wants full chroma */
1158  if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP) &&
1159  srcFormat != AV_PIX_FMT_RGB8 && srcFormat != AV_PIX_FMT_BGR8 &&
1160  srcFormat != AV_PIX_FMT_RGB4 && srcFormat != AV_PIX_FMT_BGR4 &&
1161  srcFormat != AV_PIX_FMT_RGB4_BYTE && srcFormat != AV_PIX_FMT_BGR4_BYTE &&
1162  srcFormat != AV_PIX_FMT_GBRP9BE && srcFormat != AV_PIX_FMT_GBRP9LE &&
1163  srcFormat != AV_PIX_FMT_GBRP10BE && srcFormat != AV_PIX_FMT_GBRP10LE &&
1164  srcFormat != AV_PIX_FMT_GBRP12BE && srcFormat != AV_PIX_FMT_GBRP12LE &&
1165  srcFormat != AV_PIX_FMT_GBRP14BE && srcFormat != AV_PIX_FMT_GBRP14LE &&
1166  srcFormat != AV_PIX_FMT_GBRP16BE && srcFormat != AV_PIX_FMT_GBRP16LE &&
1167  ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
1168  (flags & SWS_FAST_BILINEAR)))
1169  c->chrSrcHSubSample = 1;
1170 
1171  // Note the FF_CEIL_RSHIFT is so that we always round toward +inf.
1172  c->chrSrcW = FF_CEIL_RSHIFT(srcW, c->chrSrcHSubSample);
1173  c->chrSrcH = FF_CEIL_RSHIFT(srcH, c->chrSrcVSubSample);
1174  c->chrDstW = FF_CEIL_RSHIFT(dstW, c->chrDstHSubSample);
1175  c->chrDstH = FF_CEIL_RSHIFT(dstH, c->chrDstVSubSample);
1176 
1177  FF_ALLOC_OR_GOTO(c, c->formatConvBuffer, FFALIGN(srcW*2+78, 16) * 2, fail);
1178 
1179  c->srcBpc = 1 + desc_src->comp[0].depth_minus1;
1180  if (c->srcBpc < 8)
1181  c->srcBpc = 8;
1182  c->dstBpc = 1 + desc_dst->comp[0].depth_minus1;
1183  if (c->dstBpc < 8)
1184  c->dstBpc = 8;
1185  if (isAnyRGB(srcFormat) || srcFormat == AV_PIX_FMT_PAL8)
1186  c->srcBpc = 16;
1187  if (c->dstBpc == 16)
1188  dst_stride <<= 1;
1189 
1190  if (INLINE_MMXEXT(cpu_flags) && c->srcBpc == 8 && c->dstBpc <= 14) {
1191  c->canMMXEXTBeUsed = dstW >= srcW && (dstW & 31) == 0 &&
1192  c->chrDstW >= c->chrSrcW &&
1193  (srcW & 15) == 0;
1194  if (!c->canMMXEXTBeUsed && dstW >= srcW && c->chrDstW >= c->chrSrcW && (srcW & 15) == 0
1195 
1196  && (flags & SWS_FAST_BILINEAR)) {
1197  if (flags & SWS_PRINT_INFO)
1198  av_log(c, AV_LOG_INFO,
1199  "output width is not a multiple of 32 -> no MMXEXT scaler\n");
1200  }
1201  if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat) || isAnyRGB(c->srcFormat))
1202  c->canMMXEXTBeUsed = 0;
1203  } else
1204  c->canMMXEXTBeUsed = 0;
1205 
1206  c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
1207  c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
1208 
1209  /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
1210  * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
1211  * correct scaling.
1212  * n-2 is the last chrominance sample available.
1213  * This is not perfect, but no one should notice the difference, the more
1214  * correct variant would be like the vertical one, but that would require
1215  * some special code for the first and last pixel */
1216  if (flags & SWS_FAST_BILINEAR) {
1217  if (c->canMMXEXTBeUsed) {
1218  c->lumXInc += 20;
1219  c->chrXInc += 20;
1220  }
1221  // we don't use the x86 asm scaler if MMX is available
1222  else if (INLINE_MMX(cpu_flags) && c->dstBpc <= 14) {
1223  c->lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20;
1224  c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
1225  }
1226  }
1227 
1228 #define USE_MMAP (HAVE_MMAP && HAVE_MPROTECT && defined MAP_ANONYMOUS)
1229 
1230  /* precalculate horizontal scaler filter coefficients */
1231  {
1232 #if HAVE_MMXEXT_INLINE
1233 // can't downscale !!!
1234  if (c->canMMXEXTBeUsed && (flags & SWS_FAST_BILINEAR)) {
1236  NULL, NULL, 8);
1238  NULL, NULL, NULL, 4);
1239 
1240 #if USE_MMAP
1241  c->lumMmxextFilterCode = mmap(NULL, c->lumMmxextFilterCodeSize,
1242  PROT_READ | PROT_WRITE,
1243  MAP_PRIVATE | MAP_ANONYMOUS,
1244  -1, 0);
1245  c->chrMmxextFilterCode = mmap(NULL, c->chrMmxextFilterCodeSize,
1246  PROT_READ | PROT_WRITE,
1247  MAP_PRIVATE | MAP_ANONYMOUS,
1248  -1, 0);
1249 #elif HAVE_VIRTUALALLOC
1250  c->lumMmxextFilterCode = VirtualAlloc(NULL,
1252  MEM_COMMIT,
1253  PAGE_EXECUTE_READWRITE);
1254  c->chrMmxextFilterCode = VirtualAlloc(NULL,
1256  MEM_COMMIT,
1257  PAGE_EXECUTE_READWRITE);
1258 #else
1261 #endif
1262 
1263 #ifdef MAP_ANONYMOUS
1264  if (c->lumMmxextFilterCode == MAP_FAILED || c->chrMmxextFilterCode == MAP_FAILED)
1265 #else
1267 #endif
1268  {
1269  av_log(c, AV_LOG_ERROR, "Failed to allocate MMX2FilterCode\n");
1270  return AVERROR(ENOMEM);
1271  }
1272 
1273  FF_ALLOCZ_OR_GOTO(c, c->hLumFilter, (dstW / 8 + 8) * sizeof(int16_t), fail);
1274  FF_ALLOCZ_OR_GOTO(c, c->hChrFilter, (c->chrDstW / 4 + 8) * sizeof(int16_t), fail);
1275  FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW / 2 / 8 + 8) * sizeof(int32_t), fail);
1276  FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW / 2 / 4 + 8) * sizeof(int32_t), fail);
1277 
1279  c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
1281  c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4);
1282 
1283 #if USE_MMAP
1284  if ( mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1
1285  || mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1) {
1286  av_log(c, AV_LOG_ERROR, "mprotect failed, cannot use fast bilinear scaler\n");
1287  goto fail;
1288  }
1289 #endif
1290  } else
1291 #endif /* HAVE_MMXEXT_INLINE */
1292  {
1293  const int filterAlign = X86_MMX(cpu_flags) ? 4 :
1294  PPC_ALTIVEC(cpu_flags) ? 8 : 1;
1295 
1296  if (initFilter(&c->hLumFilter, &c->hLumFilterPos,
1297  &c->hLumFilterSize, c->lumXInc,
1298  srcW, dstW, filterAlign, 1 << 14,
1299  (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1300  cpu_flags, srcFilter->lumH, dstFilter->lumH,
1301  c->param,
1302  get_local_pos(c, 0, 0, 0),
1303  get_local_pos(c, 0, 0, 0)) < 0)
1304  goto fail;
1305  if (initFilter(&c->hChrFilter, &c->hChrFilterPos,
1306  &c->hChrFilterSize, c->chrXInc,
1307  c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
1308  (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1309  cpu_flags, srcFilter->chrH, dstFilter->chrH,
1310  c->param,
1312  get_local_pos(c, c->chrDstHSubSample, c->dst_h_chr_pos, 0)) < 0)
1313  goto fail;
1314  }
1315  } // initialize horizontal stuff
1316 
1317  /* precalculate vertical scaler filter coefficients */
1318  {
1319  const int filterAlign = X86_MMX(cpu_flags) ? 2 :
1320  PPC_ALTIVEC(cpu_flags) ? 8 : 1;
1321 
1323  c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
1324  (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1325  cpu_flags, srcFilter->lumV, dstFilter->lumV,
1326  c->param,
1327  get_local_pos(c, 0, 0, 1),
1328  get_local_pos(c, 0, 0, 1)) < 0)
1329  goto fail;
1331  c->chrYInc, c->chrSrcH, c->chrDstH,
1332  filterAlign, (1 << 12),
1333  (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1334  cpu_flags, srcFilter->chrV, dstFilter->chrV,
1335  c->param,
1337  get_local_pos(c, c->chrDstVSubSample, c->dst_v_chr_pos, 1)) < 0)
1338 
1339  goto fail;
1340 
1341 #if HAVE_ALTIVEC
1342  FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof(vector signed short) * c->vLumFilterSize * c->dstH, fail);
1343  FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof(vector signed short) * c->vChrFilterSize * c->chrDstH, fail);
1344 
1345  for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
1346  int j;
1347  short *p = (short *)&c->vYCoeffsBank[i];
1348  for (j = 0; j < 8; j++)
1349  p[j] = c->vLumFilter[i];
1350  }
1351 
1352  for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
1353  int j;
1354  short *p = (short *)&c->vCCoeffsBank[i];
1355  for (j = 0; j < 8; j++)
1356  p[j] = c->vChrFilter[i];
1357  }
1358 #endif
1359  }
1360 
1361  // calculate buffer sizes so that they won't run out while handling these damn slices
1362  c->vLumBufSize = c->vLumFilterSize;
1363  c->vChrBufSize = c->vChrFilterSize;
1364  for (i = 0; i < dstH; i++) {
1365  int chrI = (int64_t)i * c->chrDstH / dstH;
1366  int nextSlice = FFMAX(c->vLumFilterPos[i] + c->vLumFilterSize - 1,
1367  ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)
1368  << c->chrSrcVSubSample));
1369 
1370  nextSlice >>= c->chrSrcVSubSample;
1371  nextSlice <<= c->chrSrcVSubSample;
1372  if (c->vLumFilterPos[i] + c->vLumBufSize < nextSlice)
1373  c->vLumBufSize = nextSlice - c->vLumFilterPos[i];
1374  if (c->vChrFilterPos[chrI] + c->vChrBufSize <
1375  (nextSlice >> c->chrSrcVSubSample))
1376  c->vChrBufSize = (nextSlice >> c->chrSrcVSubSample) -
1377  c->vChrFilterPos[chrI];
1378  }
1379 
1380  for (i = 0; i < 4; i++)
1381  FF_ALLOCZ_OR_GOTO(c, c->dither_error[i], (c->dstW+2) * sizeof(int), fail);
1382 
1383  /* Allocate pixbufs (we use dynamic allocation because otherwise we would
1384  * need to allocate several megabytes to handle all possible cases) */
1385  FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
1386  FF_ALLOC_OR_GOTO(c, c->chrUPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
1387  FF_ALLOC_OR_GOTO(c, c->chrVPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
1388  if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
1389  FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
1390  /* Note we need at least one pixel more at the end because of the MMX code
1391  * (just in case someone wants to replace the 4000/8000). */
1392  /* align at 16 bytes for AltiVec */
1393  for (i = 0; i < c->vLumBufSize; i++) {
1394  FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i + c->vLumBufSize],
1395  dst_stride + 16, fail);
1396  c->lumPixBuf[i] = c->lumPixBuf[i + c->vLumBufSize];
1397  }
1398  // 64 / c->scalingBpp is the same as 16 / sizeof(scaling_intermediate)
1399  c->uv_off = (dst_stride>>1) + 64 / (c->dstBpc &~ 7);
1400  c->uv_offx2 = dst_stride + 16;
1401  for (i = 0; i < c->vChrBufSize; i++) {
1402  FF_ALLOC_OR_GOTO(c, c->chrUPixBuf[i + c->vChrBufSize],
1403  dst_stride * 2 + 32, fail);
1404  c->chrUPixBuf[i] = c->chrUPixBuf[i + c->vChrBufSize];
1405  c->chrVPixBuf[i] = c->chrVPixBuf[i + c->vChrBufSize]
1406  = c->chrUPixBuf[i] + (dst_stride >> 1) + 8;
1407  }
1408  if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1409  for (i = 0; i < c->vLumBufSize; i++) {
1410  FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i + c->vLumBufSize],
1411  dst_stride + 16, fail);
1412  c->alpPixBuf[i] = c->alpPixBuf[i + c->vLumBufSize];
1413  }
1414 
1415  // try to avoid drawing green stuff between the right end and the stride end
1416  for (i = 0; i < c->vChrBufSize; i++)
1417  if(desc_dst->comp[0].depth_minus1 == 15){
1418  av_assert0(c->dstBpc > 14);
1419  for(j=0; j<dst_stride/2+1; j++)
1420  ((int32_t*)(c->chrUPixBuf[i]))[j] = 1<<18;
1421  } else
1422  for(j=0; j<dst_stride+1; j++)
1423  ((int16_t*)(c->chrUPixBuf[i]))[j] = 1<<14;
1424 
1425  av_assert0(c->chrDstH <= dstH);
1426 
1427  if (flags & SWS_PRINT_INFO) {
1428  const char *scaler = NULL, *cpucaps;
1429 
1430  for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
1431  if (flags & scale_algorithms[i].flag) {
1432  scaler = scale_algorithms[i].description;
1433  break;
1434  }
1435  }
1436  if (!scaler)
1437  scaler = "ehh flags invalid?!";
1438  av_log(c, AV_LOG_INFO, "%s scaler, from %s to %s%s ",
1439  scaler,
1440  av_get_pix_fmt_name(srcFormat),
1441 #ifdef DITHER1XBPP
1442  dstFormat == AV_PIX_FMT_BGR555 || dstFormat == AV_PIX_FMT_BGR565 ||
1443  dstFormat == AV_PIX_FMT_RGB444BE || dstFormat == AV_PIX_FMT_RGB444LE ||
1444  dstFormat == AV_PIX_FMT_BGR444BE || dstFormat == AV_PIX_FMT_BGR444LE ?
1445  "dithered " : "",
1446 #else
1447  "",
1448 #endif
1449  av_get_pix_fmt_name(dstFormat));
1450 
1451  if (INLINE_MMXEXT(cpu_flags))
1452  cpucaps = "MMXEXT";
1453  else if (INLINE_AMD3DNOW(cpu_flags))
1454  cpucaps = "3DNOW";
1455  else if (INLINE_MMX(cpu_flags))
1456  cpucaps = "MMX";
1457  else if (PPC_ALTIVEC(cpu_flags))
1458  cpucaps = "AltiVec";
1459  else
1460  cpucaps = "C";
1461 
1462  av_log(c, AV_LOG_INFO, "using %s\n", cpucaps);
1463 
1464  av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1465  av_log(c, AV_LOG_DEBUG,
1466  "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1467  c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
1468  av_log(c, AV_LOG_DEBUG,
1469  "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1470  c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
1471  c->chrXInc, c->chrYInc);
1472  }
1473 
1474  /* unscaled special cases */
1475  if (unscaled && !usesHFilter && !usesVFilter &&
1476  (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
1478 
1479  if (c->swscale) {
1480  if (flags & SWS_PRINT_INFO)
1481  av_log(c, AV_LOG_INFO,
1482  "using unscaled %s -> %s special converter\n",
1483  av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1484  return 0;
1485  }
1486  }
1487 
1488  c->swscale = ff_getSwsFunc(c);
1489  return 0;
1490 fail: // FIXME replace things by appropriate error codes
1491  return -1;
1492 }
1493 
1494 #if FF_API_SWS_GETCONTEXT
1495 SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
1496  int dstW, int dstH, enum AVPixelFormat dstFormat,
1497  int flags, SwsFilter *srcFilter,
1498  SwsFilter *dstFilter, const double *param)
1499 {
1500  SwsContext *c;
1501 
1502  if (!(c = sws_alloc_context()))
1503  return NULL;
1504 
1505  c->flags = flags;
1506  c->srcW = srcW;
1507  c->srcH = srcH;
1508  c->dstW = dstW;
1509  c->dstH = dstH;
1510  c->srcFormat = srcFormat;
1511  c->dstFormat = dstFormat;
1512 
1513  if (param) {
1514  c->param[0] = param[0];
1515  c->param[1] = param[1];
1516  }
1517 
1518  if (sws_init_context(c, srcFilter, dstFilter) < 0) {
1519  sws_freeContext(c);
1520  return NULL;
1521  }
1522 
1523  return c;
1524 }
1525 #endif
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 }