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, 0 },
189  [AV_PIX_FMT_GBRP9LE] = { 1, 0 },
190  [AV_PIX_FMT_GBRP9BE] = { 1, 0 },
191  [AV_PIX_FMT_GBRP10LE] = { 1, 0 },
192  [AV_PIX_FMT_GBRP10BE] = { 1, 0 },
193  [AV_PIX_FMT_GBRP12LE] = { 1, 0 },
194  [AV_PIX_FMT_GBRP12BE] = { 1, 0 },
195  [AV_PIX_FMT_GBRP14LE] = { 1, 0 },
196  [AV_PIX_FMT_GBRP14BE] = { 1, 0 },
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 
995  if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) {
996  if (dstW&1) {
997  av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to odd output size\n");
998  flags |= SWS_FULL_CHR_H_INT;
999  c->flags = flags;
1000  }
1001  }
1002  /* reuse chroma for 2 pixels RGB/BGR unless user wants full
1003  * chroma interpolation */
1004  if (flags & SWS_FULL_CHR_H_INT &&
1005  isAnyRGB(dstFormat) &&
1006  dstFormat != AV_PIX_FMT_RGBA &&
1007  dstFormat != AV_PIX_FMT_ARGB &&
1008  dstFormat != AV_PIX_FMT_BGRA &&
1009  dstFormat != AV_PIX_FMT_ABGR &&
1010  dstFormat != AV_PIX_FMT_RGB24 &&
1011  dstFormat != AV_PIX_FMT_BGR24) {
1013  "full chroma interpolation for destination format '%s' not yet implemented\n",
1014  av_get_pix_fmt_name(dstFormat));
1015  flags &= ~SWS_FULL_CHR_H_INT;
1016  c->flags = flags;
1017  }
1018  if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
1019  c->chrDstHSubSample = 1;
1020 
1021  // drop some chroma lines if the user wants it
1022  c->vChrDrop = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
1024  c->chrSrcVSubSample += c->vChrDrop;
1025 
1026  /* drop every other pixel for chroma calculation unless user
1027  * wants full chroma */
1028  if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP) &&
1029  srcFormat != AV_PIX_FMT_RGB8 && srcFormat != AV_PIX_FMT_BGR8 &&
1030  srcFormat != AV_PIX_FMT_RGB4 && srcFormat != AV_PIX_FMT_BGR4 &&
1031  srcFormat != AV_PIX_FMT_RGB4_BYTE && srcFormat != AV_PIX_FMT_BGR4_BYTE &&
1032  ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
1033  (flags & SWS_FAST_BILINEAR)))
1034  c->chrSrcHSubSample = 1;
1035 
1036  // Note the -((-x)>>y) is so that we always round toward +inf.
1037  c->chrSrcW = -((-srcW) >> c->chrSrcHSubSample);
1038  c->chrSrcH = -((-srcH) >> c->chrSrcVSubSample);
1039  c->chrDstW = -((-dstW) >> c->chrDstHSubSample);
1040  c->chrDstH = -((-dstH) >> c->chrDstVSubSample);
1041 
1042  FF_ALLOC_OR_GOTO(c, c->formatConvBuffer, FFALIGN(srcW*2+78, 16) * 2, fail);
1043 
1044  /* unscaled special cases */
1045  if (unscaled && !usesHFilter && !usesVFilter &&
1046  (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
1048 
1049  if (c->swScale) {
1050  if (flags & SWS_PRINT_INFO)
1051  av_log(c, AV_LOG_INFO,
1052  "using unscaled %s -> %s special converter\n",
1053  av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1054  return 0;
1055  }
1056  }
1057 
1058  c->srcBpc = 1 + desc_src->comp[0].depth_minus1;
1059  if (c->srcBpc < 8)
1060  c->srcBpc = 8;
1061  c->dstBpc = 1 + desc_dst->comp[0].depth_minus1;
1062  if (c->dstBpc < 8)
1063  c->dstBpc = 8;
1064  if (isAnyRGB(srcFormat) || srcFormat == AV_PIX_FMT_PAL8)
1065  c->srcBpc = 16;
1066  if (c->dstBpc == 16)
1067  dst_stride <<= 1;
1068 
1069  if (INLINE_MMXEXT(cpu_flags) && c->srcBpc == 8 && c->dstBpc <= 14) {
1070  c->canMMXEXTBeUsed = (dstW >= srcW && (dstW & 31) == 0 &&
1071  (srcW & 15) == 0) ? 1 : 0;
1072  if (!c->canMMXEXTBeUsed && dstW >= srcW && (srcW & 15) == 0
1073 
1074  && (flags & SWS_FAST_BILINEAR)) {
1075  if (flags & SWS_PRINT_INFO)
1076  av_log(c, AV_LOG_INFO,
1077  "output width is not a multiple of 32 -> no MMXEXT scaler\n");
1078  }
1079  if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat) || isAnyRGB(c->srcFormat))
1080  c->canMMXEXTBeUsed = 0;
1081  } else
1082  c->canMMXEXTBeUsed = 0;
1083 
1084  c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
1085  c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
1086 
1087  /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
1088  * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
1089  * correct scaling.
1090  * n-2 is the last chrominance sample available.
1091  * This is not perfect, but no one should notice the difference, the more
1092  * correct variant would be like the vertical one, but that would require
1093  * some special code for the first and last pixel */
1094  if (flags & SWS_FAST_BILINEAR) {
1095  if (c->canMMXEXTBeUsed) {
1096  c->lumXInc += 20;
1097  c->chrXInc += 20;
1098  }
1099  // we don't use the x86 asm scaler if MMX is available
1100  else if (INLINE_MMX(cpu_flags) && c->dstBpc <= 14) {
1101  c->lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20;
1102  c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
1103  }
1104  }
1105 
1106 #define USE_MMAP (HAVE_MMAP && HAVE_MPROTECT && defined MAP_ANONYMOUS)
1107 
1108  /* precalculate horizontal scaler filter coefficients */
1109  {
1110 #if HAVE_MMXEXT_INLINE
1111 // can't downscale !!!
1112  if (c->canMMXEXTBeUsed && (flags & SWS_FAST_BILINEAR)) {
1113  c->lumMmxextFilterCodeSize = init_hscaler_mmxext(dstW, c->lumXInc, NULL,
1114  NULL, NULL, 8);
1115  c->chrMmxextFilterCodeSize = init_hscaler_mmxext(c->chrDstW, c->chrXInc,
1116  NULL, NULL, NULL, 4);
1117 
1118 #if USE_MMAP
1120  PROT_READ | PROT_WRITE,
1121  MAP_PRIVATE | MAP_ANONYMOUS,
1122  -1, 0);
1124  PROT_READ | PROT_WRITE,
1125  MAP_PRIVATE | MAP_ANONYMOUS,
1126  -1, 0);
1127 #elif HAVE_VIRTUALALLOC
1128  c->lumMmxextFilterCode = VirtualAlloc(NULL,
1130  MEM_COMMIT,
1131  PAGE_EXECUTE_READWRITE);
1132  c->chrMmxextFilterCode = VirtualAlloc(NULL,
1134  MEM_COMMIT,
1135  PAGE_EXECUTE_READWRITE);
1136 #else
1139 #endif
1140 
1141 #ifdef MAP_ANONYMOUS
1142  if (c->lumMmxextFilterCode == MAP_FAILED || c->chrMmxextFilterCode == MAP_FAILED)
1143 #else
1145 #endif
1146  {
1147  av_log(c, AV_LOG_ERROR, "Failed to allocate MMX2FilterCode\n");
1148  return AVERROR(ENOMEM);
1149  }
1150 
1151  FF_ALLOCZ_OR_GOTO(c, c->hLumFilter, (dstW / 8 + 8) * sizeof(int16_t), fail);
1152  FF_ALLOCZ_OR_GOTO(c, c->hChrFilter, (c->chrDstW / 4 + 8) * sizeof(int16_t), fail);
1153  FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW / 2 / 8 + 8) * sizeof(int32_t), fail);
1154  FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW / 2 / 4 + 8) * sizeof(int32_t), fail);
1155 
1156  init_hscaler_mmxext( dstW, c->lumXInc, c->lumMmxextFilterCode,
1157  c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
1158  init_hscaler_mmxext(c->chrDstW, c->chrXInc, c->chrMmxextFilterCode,
1159  c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4);
1160 
1161 #if USE_MMAP
1162  mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ);
1163  mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ);
1164 #endif
1165  } else
1166 #endif /* HAVE_MMXEXT_INLINE */
1167  {
1168  const int filterAlign =
1169  (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 4 :
1170  (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
1171  1;
1172 
1173  if (initFilter(&c->hLumFilter, &c->hLumFilterPos,
1174  &c->hLumFilterSize, c->lumXInc,
1175  srcW, dstW, filterAlign, 1 << 14,
1176  (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1177  cpu_flags, srcFilter->lumH, dstFilter->lumH,
1178  c->param) < 0)
1179  goto fail;
1180  if (initFilter(&c->hChrFilter, &c->hChrFilterPos,
1181  &c->hChrFilterSize, c->chrXInc,
1182  c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
1183  (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1184  cpu_flags, srcFilter->chrH, dstFilter->chrH,
1185  c->param) < 0)
1186  goto fail;
1187  }
1188  } // initialize horizontal stuff
1189 
1190  /* precalculate vertical scaler filter coefficients */
1191  {
1192  const int filterAlign =
1193  (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 2 :
1194  (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
1195  1;
1196 
1198  c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
1199  (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1200  cpu_flags, srcFilter->lumV, dstFilter->lumV,
1201  c->param) < 0)
1202  goto fail;
1204  c->chrYInc, c->chrSrcH, c->chrDstH,
1205  filterAlign, (1 << 12),
1206  (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1207  cpu_flags, srcFilter->chrV, dstFilter->chrV,
1208  c->param) < 0)
1209  goto fail;
1210 
1211 #if HAVE_ALTIVEC
1212  FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof(vector signed short) * c->vLumFilterSize * c->dstH, fail);
1213  FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof(vector signed short) * c->vChrFilterSize * c->chrDstH, fail);
1214 
1215  for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
1216  int j;
1217  short *p = (short *)&c->vYCoeffsBank[i];
1218  for (j = 0; j < 8; j++)
1219  p[j] = c->vLumFilter[i];
1220  }
1221 
1222  for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
1223  int j;
1224  short *p = (short *)&c->vCCoeffsBank[i];
1225  for (j = 0; j < 8; j++)
1226  p[j] = c->vChrFilter[i];
1227  }
1228 #endif
1229  }
1230 
1231  // calculate buffer sizes so that they won't run out while handling these damn slices
1232  c->vLumBufSize = c->vLumFilterSize;
1233  c->vChrBufSize = c->vChrFilterSize;
1234  for (i = 0; i < dstH; i++) {
1235  int chrI = (int64_t)i * c->chrDstH / dstH;
1236  int nextSlice = FFMAX(c->vLumFilterPos[i] + c->vLumFilterSize - 1,
1237  ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)
1238  << c->chrSrcVSubSample));
1239 
1240  nextSlice >>= c->chrSrcVSubSample;
1241  nextSlice <<= c->chrSrcVSubSample;
1242  if (c->vLumFilterPos[i] + c->vLumBufSize < nextSlice)
1243  c->vLumBufSize = nextSlice - c->vLumFilterPos[i];
1244  if (c->vChrFilterPos[chrI] + c->vChrBufSize <
1245  (nextSlice >> c->chrSrcVSubSample))
1246  c->vChrBufSize = (nextSlice >> c->chrSrcVSubSample) -
1247  c->vChrFilterPos[chrI];
1248  }
1249 
1250  /* Allocate pixbufs (we use dynamic allocation because otherwise we would
1251  * need to allocate several megabytes to handle all possible cases) */
1252  FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
1253  FF_ALLOC_OR_GOTO(c, c->chrUPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
1254  FF_ALLOC_OR_GOTO(c, c->chrVPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
1255  if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
1256  FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
1257  /* Note we need at least one pixel more at the end because of the MMX code
1258  * (just in case someone wants to replace the 4000/8000). */
1259  /* align at 16 bytes for AltiVec */
1260  for (i = 0; i < c->vLumBufSize; i++) {
1261  FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i + c->vLumBufSize],
1262  dst_stride + 16, fail);
1263  c->lumPixBuf[i] = c->lumPixBuf[i + c->vLumBufSize];
1264  }
1265  // 64 / c->scalingBpp is the same as 16 / sizeof(scaling_intermediate)
1266  c->uv_off = (dst_stride>>1) + 64 / (c->dstBpc &~ 7);
1267  c->uv_offx2 = dst_stride + 16;
1268  for (i = 0; i < c->vChrBufSize; i++) {
1269  FF_ALLOC_OR_GOTO(c, c->chrUPixBuf[i + c->vChrBufSize],
1270  dst_stride * 2 + 32, fail);
1271  c->chrUPixBuf[i] = c->chrUPixBuf[i + c->vChrBufSize];
1272  c->chrVPixBuf[i] = c->chrVPixBuf[i + c->vChrBufSize]
1273  = c->chrUPixBuf[i] + (dst_stride >> 1) + 8;
1274  }
1275  if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1276  for (i = 0; i < c->vLumBufSize; i++) {
1277  FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i + c->vLumBufSize],
1278  dst_stride + 16, fail);
1279  c->alpPixBuf[i] = c->alpPixBuf[i + c->vLumBufSize];
1280  }
1281 
1282  // try to avoid drawing green stuff between the right end and the stride end
1283  for (i = 0; i < c->vChrBufSize; i++)
1284  if(desc_dst->comp[0].depth_minus1 == 15){
1285  av_assert0(c->dstBpc > 14);
1286  for(j=0; j<dst_stride/2+1; j++)
1287  ((int32_t*)(c->chrUPixBuf[i]))[j] = 1<<18;
1288  } else
1289  for(j=0; j<dst_stride+1; j++)
1290  ((int16_t*)(c->chrUPixBuf[i]))[j] = 1<<14;
1291 
1292  av_assert0(c->chrDstH <= dstH);
1293 
1294  if (flags & SWS_PRINT_INFO) {
1295  if (flags & SWS_FAST_BILINEAR)
1296  av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
1297  else if (flags & SWS_BILINEAR)
1298  av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
1299  else if (flags & SWS_BICUBIC)
1300  av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
1301  else if (flags & SWS_X)
1302  av_log(c, AV_LOG_INFO, "Experimental scaler, ");
1303  else if (flags & SWS_POINT)
1304  av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
1305  else if (flags & SWS_AREA)
1306  av_log(c, AV_LOG_INFO, "Area Averaging scaler, ");
1307  else if (flags & SWS_BICUBLIN)
1308  av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
1309  else if (flags & SWS_GAUSS)
1310  av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
1311  else if (flags & SWS_SINC)
1312  av_log(c, AV_LOG_INFO, "Sinc scaler, ");
1313  else if (flags & SWS_LANCZOS)
1314  av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
1315  else if (flags & SWS_SPLINE)
1316  av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
1317  else
1318  av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
1319 
1320  av_log(c, AV_LOG_INFO, "from %s to %s%s ",
1321  av_get_pix_fmt_name(srcFormat),
1322 #ifdef DITHER1XBPP
1323  dstFormat == AV_PIX_FMT_BGR555 || dstFormat == AV_PIX_FMT_BGR565 ||
1324  dstFormat == AV_PIX_FMT_RGB444BE || dstFormat == AV_PIX_FMT_RGB444LE ||
1325  dstFormat == AV_PIX_FMT_BGR444BE || dstFormat == AV_PIX_FMT_BGR444LE ?
1326  "dithered " : "",
1327 #else
1328  "",
1329 #endif
1330  av_get_pix_fmt_name(dstFormat));
1331 
1332  if (INLINE_MMXEXT(cpu_flags))
1333  av_log(c, AV_LOG_INFO, "using MMXEXT\n");
1334  else if (INLINE_AMD3DNOW(cpu_flags))
1335  av_log(c, AV_LOG_INFO, "using 3DNOW\n");
1336  else if (INLINE_MMX(cpu_flags))
1337  av_log(c, AV_LOG_INFO, "using MMX\n");
1338  else if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC)
1339  av_log(c, AV_LOG_INFO, "using AltiVec\n");
1340  else
1341  av_log(c, AV_LOG_INFO, "using C\n");
1342 
1343  av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1344  av_log(c, AV_LOG_DEBUG,
1345  "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1346  c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
1347  av_log(c, AV_LOG_DEBUG,
1348  "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1349  c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
1350  c->chrXInc, c->chrYInc);
1351  }
1352 
1353  c->swScale = ff_getSwsFunc(c);
1354  return 0;
1355 fail: // FIXME replace things by appropriate error codes
1356  return -1;
1357 }
1358 
1359 #if FF_API_SWS_GETCONTEXT
1360 SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
1361  int dstW, int dstH, enum AVPixelFormat dstFormat,
1362  int flags, SwsFilter *srcFilter,
1363  SwsFilter *dstFilter, const double *param)
1364 {
1365  SwsContext *c;
1366 
1367  if (!(c = sws_alloc_context()))
1368  return NULL;
1369 
1370  c->flags = flags;
1371  c->srcW = srcW;
1372  c->srcH = srcH;
1373  c->dstW = dstW;
1374  c->dstH = dstH;
1375  c->srcRange = handle_jpeg(&srcFormat);
1376  c->dstRange = handle_jpeg(&dstFormat);
1377  c->src0Alpha = handle_0alpha(&srcFormat);
1378  c->dst0Alpha = handle_0alpha(&dstFormat);
1379  c->srcFormat = srcFormat;
1380  c->dstFormat = dstFormat;
1381 
1382  if (param) {
1383  c->param[0] = param[0];
1384  c->param[1] = param[1];
1385  }
1387  ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/,
1388  c->dstRange, 0, 1 << 16, 1 << 16);
1389 
1390  if (sws_init_context(c, srcFilter, dstFilter) < 0) {
1391  sws_freeContext(c);
1392  return NULL;
1393  }
1394 
1395  return c;
1396 }
1397 #endif
1398 
1399 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
1400  float lumaSharpen, float chromaSharpen,
1401  float chromaHShift, float chromaVShift,
1402  int verbose)
1403 {
1404  SwsFilter *filter = av_malloc(sizeof(SwsFilter));
1405  if (!filter)
1406  return NULL;
1407 
1408  if (lumaGBlur != 0.0) {
1409  filter->lumH = sws_getGaussianVec(lumaGBlur, 3.0);
1410  filter->lumV = sws_getGaussianVec(lumaGBlur, 3.0);
1411  } else {
1412  filter->lumH = sws_getIdentityVec();
1413  filter->lumV = sws_getIdentityVec();
1414  }
1415 
1416  if (chromaGBlur != 0.0) {
1417  filter->chrH = sws_getGaussianVec(chromaGBlur, 3.0);
1418  filter->chrV = sws_getGaussianVec(chromaGBlur, 3.0);
1419  } else {
1420  filter->chrH = sws_getIdentityVec();
1421  filter->chrV = sws_getIdentityVec();
1422  }
1423 
1424  if (chromaSharpen != 0.0) {
1425  SwsVector *id = sws_getIdentityVec();
1426  sws_scaleVec(filter->chrH, -chromaSharpen);
1427  sws_scaleVec(filter->chrV, -chromaSharpen);
1428  sws_addVec(filter->chrH, id);
1429  sws_addVec(filter->chrV, id);
1430  sws_freeVec(id);
1431  }
1432 
1433  if (lumaSharpen != 0.0) {
1434  SwsVector *id = sws_getIdentityVec();
1435  sws_scaleVec(filter->lumH, -lumaSharpen);
1436  sws_scaleVec(filter->lumV, -lumaSharpen);
1437  sws_addVec(filter->lumH, id);
1438  sws_addVec(filter->lumV, id);
1439  sws_freeVec(id);
1440  }
1441 
1442  if (chromaHShift != 0.0)
1443  sws_shiftVec(filter->chrH, (int)(chromaHShift + 0.5));
1444 
1445  if (chromaVShift != 0.0)
1446  sws_shiftVec(filter->chrV, (int)(chromaVShift + 0.5));
1447 
1448  sws_normalizeVec(filter->chrH, 1.0);
1449  sws_normalizeVec(filter->chrV, 1.0);
1450  sws_normalizeVec(filter->lumH, 1.0);
1451  sws_normalizeVec(filter->lumV, 1.0);
1452 
1453  if (verbose)
1454  sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
1455  if (verbose)
1456  sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
1457 
1458  return filter;
1459 }
1460 
1462 {
1463  SwsVector *vec;
1464 
1465  if(length <= 0 || length > INT_MAX/ sizeof(double))
1466  return NULL;
1467 
1468  vec = av_malloc(sizeof(SwsVector));
1469  if (!vec)
1470  return NULL;
1471  vec->length = length;
1472  vec->coeff = av_malloc(sizeof(double) * length);
1473  if (!vec->coeff)
1474  av_freep(&vec);
1475  return vec;
1476 }
1477 
1478 SwsVector *sws_getGaussianVec(double variance, double quality)
1479 {
1480  const int length = (int)(variance * quality + 0.5) | 1;
1481  int i;
1482  double middle = (length - 1) * 0.5;
1483  SwsVector *vec;
1484 
1485  if(variance < 0 || quality < 0)
1486  return NULL;
1487 
1488  vec = sws_allocVec(length);
1489 
1490  if (!vec)
1491  return NULL;
1492 
1493  for (i = 0; i < length; i++) {
1494  double dist = i - middle;
1495  vec->coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
1496  sqrt(2 * variance * M_PI);
1497  }
1498 
1499  sws_normalizeVec(vec, 1.0);
1500 
1501  return vec;
1502 }
1503 
1504 SwsVector *sws_getConstVec(double c, int length)
1505 {
1506  int i;
1507  SwsVector *vec = sws_allocVec(length);
1508 
1509  if (!vec)
1510  return NULL;
1511 
1512  for (i = 0; i < length; i++)
1513  vec->coeff[i] = c;
1514 
1515  return vec;
1516 }
1517 
1519 {
1520  return sws_getConstVec(1.0, 1);
1521 }
1522 
1523 static double sws_dcVec(SwsVector *a)
1524 {
1525  int i;
1526  double sum = 0;
1527 
1528  for (i = 0; i < a->length; i++)
1529  sum += a->coeff[i];
1530 
1531  return sum;
1532 }
1533 
1534 void sws_scaleVec(SwsVector *a, double scalar)
1535 {
1536  int i;
1537 
1538  for (i = 0; i < a->length; i++)
1539  a->coeff[i] *= scalar;
1540 }
1541 
1543 {
1544  sws_scaleVec(a, height / sws_dcVec(a));
1545 }
1546 
1548 {
1549  int length = a->length + b->length - 1;
1550  int i, j;
1551  SwsVector *vec = sws_getConstVec(0.0, length);
1552 
1553  if (!vec)
1554  return NULL;
1555 
1556  for (i = 0; i < a->length; i++) {
1557  for (j = 0; j < b->length; j++) {
1558  vec->coeff[i + j] += a->coeff[i] * b->coeff[j];
1559  }
1560  }
1561 
1562  return vec;
1563 }
1564 
1566 {
1567  int length = FFMAX(a->length, b->length);
1568  int i;
1569  SwsVector *vec = sws_getConstVec(0.0, length);
1570 
1571  if (!vec)
1572  return NULL;
1573 
1574  for (i = 0; i < a->length; i++)
1575  vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
1576  for (i = 0; i < b->length; i++)
1577  vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] += b->coeff[i];
1578 
1579  return vec;
1580 }
1581 
1583 {
1584  int length = FFMAX(a->length, b->length);
1585  int i;
1586  SwsVector *vec = sws_getConstVec(0.0, length);
1587 
1588  if (!vec)
1589  return NULL;
1590 
1591  for (i = 0; i < a->length; i++)
1592  vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
1593  for (i = 0; i < b->length; i++)
1594  vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] -= b->coeff[i];
1595 
1596  return vec;
1597 }
1598 
1599 /* shift left / or right if "shift" is negative */
1600 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
1601 {
1602  int length = a->length + FFABS(shift) * 2;
1603  int i;
1604  SwsVector *vec = sws_getConstVec(0.0, length);
1605 
1606  if (!vec)
1607  return NULL;
1608 
1609  for (i = 0; i < a->length; i++) {
1610  vec->coeff[i + (length - 1) / 2 -
1611  (a->length - 1) / 2 - shift] = a->coeff[i];
1612  }
1613 
1614  return vec;
1615 }
1616 
1617 void sws_shiftVec(SwsVector *a, int shift)
1618 {
1619  SwsVector *shifted = sws_getShiftedVec(a, shift);
1620  av_free(a->coeff);
1621  a->coeff = shifted->coeff;
1622  a->length = shifted->length;
1623  av_free(shifted);
1624 }
1625 
1627 {
1628  SwsVector *sum = sws_sumVec(a, b);
1629  av_free(a->coeff);
1630  a->coeff = sum->coeff;
1631  a->length = sum->length;
1632  av_free(sum);
1633 }
1634 
1636 {
1637  SwsVector *diff = sws_diffVec(a, b);
1638  av_free(a->coeff);
1639  a->coeff = diff->coeff;
1640  a->length = diff->length;
1641  av_free(diff);
1642 }
1643 
1645 {
1646  SwsVector *conv = sws_getConvVec(a, b);
1647  av_free(a->coeff);
1648  a->coeff = conv->coeff;
1649  a->length = conv->length;
1650  av_free(conv);
1651 }
1652 
1654 {
1655  int i;
1656  SwsVector *vec = sws_allocVec(a->length);
1657 
1658  if (!vec)
1659  return NULL;
1660 
1661  for (i = 0; i < a->length; i++)
1662  vec->coeff[i] = a->coeff[i];
1663 
1664  return vec;
1665 }
1666 
1667 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
1668 {
1669  int i;
1670  double max = 0;
1671  double min = 0;
1672  double range;
1673 
1674  for (i = 0; i < a->length; i++)
1675  if (a->coeff[i] > max)
1676  max = a->coeff[i];
1677 
1678  for (i = 0; i < a->length; i++)
1679  if (a->coeff[i] < min)
1680  min = a->coeff[i];
1681 
1682  range = max - min;
1683 
1684  for (i = 0; i < a->length; i++) {
1685  int x = (int)((a->coeff[i] - min) * 60.0 / range + 0.5);
1686  av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
1687  for (; x > 0; x--)
1688  av_log(log_ctx, log_level, " ");
1689  av_log(log_ctx, log_level, "|\n");
1690  }
1691 }
1692 
1694 {
1695  if (!a)
1696  return;
1697  av_freep(&a->coeff);
1698  a->length = 0;
1699  av_free(a);
1700 }
1701 
1703 {
1704  if (!filter)
1705  return;
1706 
1707  if (filter->lumH)
1708  sws_freeVec(filter->lumH);
1709  if (filter->lumV)
1710  sws_freeVec(filter->lumV);
1711  if (filter->chrH)
1712  sws_freeVec(filter->chrH);
1713  if (filter->chrV)
1714  sws_freeVec(filter->chrV);
1715  av_free(filter);
1716 }
1717 
1719 {
1720  int i;
1721  if (!c)
1722  return;
1723 
1724  if (c->lumPixBuf) {
1725  for (i = 0; i < c->vLumBufSize; i++)
1726  av_freep(&c->lumPixBuf[i]);
1727  av_freep(&c->lumPixBuf);
1728  }
1729 
1730  if (c->chrUPixBuf) {
1731  for (i = 0; i < c->vChrBufSize; i++)
1732  av_freep(&c->chrUPixBuf[i]);
1733  av_freep(&c->chrUPixBuf);
1734  av_freep(&c->chrVPixBuf);
1735  }
1736 
1737  if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1738  for (i = 0; i < c->vLumBufSize; i++)
1739  av_freep(&c->alpPixBuf[i]);
1740  av_freep(&c->alpPixBuf);
1741  }
1742 
1743  av_freep(&c->vLumFilter);
1744  av_freep(&c->vChrFilter);
1745  av_freep(&c->hLumFilter);
1746  av_freep(&c->hChrFilter);
1747 #if HAVE_ALTIVEC
1748  av_freep(&c->vYCoeffsBank);
1749  av_freep(&c->vCCoeffsBank);
1750 #endif
1751 
1752  av_freep(&c->vLumFilterPos);
1753  av_freep(&c->vChrFilterPos);
1754  av_freep(&c->hLumFilterPos);
1755  av_freep(&c->hChrFilterPos);
1756 
1757 #if HAVE_MMX_INLINE
1758 #if USE_MMAP
1759  if (c->lumMmxextFilterCode)
1761  if (c->chrMmxextFilterCode)
1763 #elif HAVE_VIRTUALALLOC
1764  if (c->lumMmxextFilterCode)
1765  VirtualFree(c->lumMmxextFilterCode, 0, MEM_RELEASE);
1766  if (c->chrMmxextFilterCode)
1767  VirtualFree(c->chrMmxextFilterCode, 0, MEM_RELEASE);
1768 #else
1771 #endif
1774 #endif /* HAVE_MMX_INLINE */
1775 
1776  av_freep(&c->yuvTable);
1778 
1779  av_free(c);
1780 }
1781 
1782 struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW,
1783  int srcH, enum AVPixelFormat srcFormat,
1784  int dstW, int dstH,
1785  enum AVPixelFormat dstFormat, int flags,
1786  SwsFilter *srcFilter,
1787  SwsFilter *dstFilter,
1788  const double *param)
1789 {
1790  static const double default_param[2] = { SWS_PARAM_DEFAULT,
1792 
1793  if (!param)
1794  param = default_param;
1795 
1796  if (context &&
1797  (context->srcW != srcW ||
1798  context->srcH != srcH ||
1799  context->srcFormat != srcFormat ||
1800  context->dstW != dstW ||
1801  context->dstH != dstH ||
1802  context->dstFormat != dstFormat ||
1803  context->flags != flags ||
1804  context->param[0] != param[0] ||
1805  context->param[1] != param[1])) {
1806  sws_freeContext(context);
1807  context = NULL;
1808  }
1809 
1810  if (!context) {
1811  if (!(context = sws_alloc_context()))
1812  return NULL;
1813  context->srcW = srcW;
1814  context->srcH = srcH;
1815  context->srcRange = handle_jpeg(&srcFormat);
1816  context->src0Alpha = handle_0alpha(&srcFormat);
1817  context->srcFormat = srcFormat;
1818  context->dstW = dstW;
1819  context->dstH = dstH;
1820  context->dstRange = handle_jpeg(&dstFormat);
1821  context->dst0Alpha = handle_0alpha(&dstFormat);
1822  context->dstFormat = dstFormat;
1823  context->flags = flags;
1824  context->param[0] = param[0];
1825  context->param[1] = param[1];
1827  context->srcRange,
1828  ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/,
1829  context->dstRange, 0, 1 << 16, 1 << 16);
1830  if (sws_init_context(context, srcFilter, dstFilter) < 0) {
1831  sws_freeContext(context);
1832  return NULL;
1833  }
1834  }
1835  return context;
1836 }