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