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