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