• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 _DEFAULT_SOURCE
24 #define _SVID_SOURCE // needed for MAP_ANONYMOUS
25 #define _DARWIN_C_SOURCE // needed for MAP_ANON
26 #include <inttypes.h>
27 #include <math.h>
28 #include <stdio.h>
29 #include <string.h>
30 #if HAVE_MMAP
31 #include <sys/mman.h>
32 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
33 #define MAP_ANONYMOUS MAP_ANON
34 #endif
35 #endif
36 #if HAVE_VIRTUALALLOC
37 #define WIN32_LEAN_AND_MEAN
38 #include <windows.h>
39 #endif
40 
41 #include "libavutil/attributes.h"
42 #include "libavutil/avassert.h"
43 #include "libavutil/avutil.h"
44 #include "libavutil/bswap.h"
45 #include "libavutil/cpu.h"
46 #include "libavutil/imgutils.h"
47 #include "libavutil/intreadwrite.h"
48 #include "libavutil/libm.h"
49 #include "libavutil/mathematics.h"
50 #include "libavutil/opt.h"
51 #include "libavutil/pixdesc.h"
52 #include "libavutil/aarch64/cpu.h"
53 #include "libavutil/ppc/cpu.h"
54 #include "libavutil/x86/asm.h"
55 #include "libavutil/x86/cpu.h"
56 
57 // We have to implement deprecated functions until they are removed, this is the
58 // simplest way to prevent warnings
59 #undef attribute_deprecated
60 #define attribute_deprecated
61 
62 #include "rgb2rgb.h"
63 #include "swscale.h"
64 #include "swscale_internal.h"
65 
66 #if !FF_API_SWS_VECTOR
67 static SwsVector *sws_getIdentityVec(void);
68 static void sws_addVec(SwsVector *a, SwsVector *b);
69 static void sws_shiftVec(SwsVector *a, int shift);
70 static void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level);
71 #endif
72 
73 static void handle_formats(SwsContext *c);
74 
swscale_version(void)75 unsigned swscale_version(void)
76 {
77     av_assert0(LIBSWSCALE_VERSION_MICRO >= 100);
78     return LIBSWSCALE_VERSION_INT;
79 }
80 
swscale_configuration(void)81 const char *swscale_configuration(void)
82 {
83     return FFMPEG_CONFIGURATION;
84 }
85 
swscale_license(void)86 const char *swscale_license(void)
87 {
88 #define LICENSE_PREFIX "libswscale license: "
89     return &LICENSE_PREFIX FFMPEG_LICENSE[sizeof(LICENSE_PREFIX) - 1];
90 }
91 
92 typedef struct FormatEntry {
93     uint8_t is_supported_in         :1;
94     uint8_t is_supported_out        :1;
95     uint8_t is_supported_endianness :1;
96 } FormatEntry;
97 
98 static const FormatEntry format_entries[] = {
99     [AV_PIX_FMT_YUV420P]     = { 1, 1 },
100     [AV_PIX_FMT_YUYV422]     = { 1, 1 },
101     [AV_PIX_FMT_RGB24]       = { 1, 1 },
102     [AV_PIX_FMT_BGR24]       = { 1, 1 },
103     [AV_PIX_FMT_YUV422P]     = { 1, 1 },
104     [AV_PIX_FMT_YUV444P]     = { 1, 1 },
105     [AV_PIX_FMT_YUV410P]     = { 1, 1 },
106     [AV_PIX_FMT_YUV411P]     = { 1, 1 },
107     [AV_PIX_FMT_GRAY8]       = { 1, 1 },
108     [AV_PIX_FMT_MONOWHITE]   = { 1, 1 },
109     [AV_PIX_FMT_MONOBLACK]   = { 1, 1 },
110     [AV_PIX_FMT_PAL8]        = { 1, 0 },
111     [AV_PIX_FMT_YUVJ420P]    = { 1, 1 },
112     [AV_PIX_FMT_YUVJ411P]    = { 1, 1 },
113     [AV_PIX_FMT_YUVJ422P]    = { 1, 1 },
114     [AV_PIX_FMT_YUVJ444P]    = { 1, 1 },
115     [AV_PIX_FMT_YVYU422]     = { 1, 1 },
116     [AV_PIX_FMT_UYVY422]     = { 1, 1 },
117     [AV_PIX_FMT_UYYVYY411]   = { 0, 0 },
118     [AV_PIX_FMT_BGR8]        = { 1, 1 },
119     [AV_PIX_FMT_BGR4]        = { 0, 1 },
120     [AV_PIX_FMT_BGR4_BYTE]   = { 1, 1 },
121     [AV_PIX_FMT_RGB8]        = { 1, 1 },
122     [AV_PIX_FMT_RGB4]        = { 0, 1 },
123     [AV_PIX_FMT_RGB4_BYTE]   = { 1, 1 },
124     [AV_PIX_FMT_NV12]        = { 1, 1 },
125     [AV_PIX_FMT_NV21]        = { 1, 1 },
126     [AV_PIX_FMT_ARGB]        = { 1, 1 },
127     [AV_PIX_FMT_RGBA]        = { 1, 1 },
128     [AV_PIX_FMT_ABGR]        = { 1, 1 },
129     [AV_PIX_FMT_BGRA]        = { 1, 1 },
130     [AV_PIX_FMT_0RGB]        = { 1, 1 },
131     [AV_PIX_FMT_RGB0]        = { 1, 1 },
132     [AV_PIX_FMT_0BGR]        = { 1, 1 },
133     [AV_PIX_FMT_BGR0]        = { 1, 1 },
134     [AV_PIX_FMT_GRAY9BE]     = { 1, 1 },
135     [AV_PIX_FMT_GRAY9LE]     = { 1, 1 },
136     [AV_PIX_FMT_GRAY10BE]    = { 1, 1 },
137     [AV_PIX_FMT_GRAY10LE]    = { 1, 1 },
138     [AV_PIX_FMT_GRAY12BE]    = { 1, 1 },
139     [AV_PIX_FMT_GRAY12LE]    = { 1, 1 },
140     [AV_PIX_FMT_GRAY14BE]    = { 1, 1 },
141     [AV_PIX_FMT_GRAY14LE]    = { 1, 1 },
142     [AV_PIX_FMT_GRAY16BE]    = { 1, 1 },
143     [AV_PIX_FMT_GRAY16LE]    = { 1, 1 },
144     [AV_PIX_FMT_YUV440P]     = { 1, 1 },
145     [AV_PIX_FMT_YUVJ440P]    = { 1, 1 },
146     [AV_PIX_FMT_YUV440P10LE] = { 1, 1 },
147     [AV_PIX_FMT_YUV440P10BE] = { 1, 1 },
148     [AV_PIX_FMT_YUV440P12LE] = { 1, 1 },
149     [AV_PIX_FMT_YUV440P12BE] = { 1, 1 },
150     [AV_PIX_FMT_YUVA420P]    = { 1, 1 },
151     [AV_PIX_FMT_YUVA422P]    = { 1, 1 },
152     [AV_PIX_FMT_YUVA444P]    = { 1, 1 },
153     [AV_PIX_FMT_YUVA420P9BE] = { 1, 1 },
154     [AV_PIX_FMT_YUVA420P9LE] = { 1, 1 },
155     [AV_PIX_FMT_YUVA422P9BE] = { 1, 1 },
156     [AV_PIX_FMT_YUVA422P9LE] = { 1, 1 },
157     [AV_PIX_FMT_YUVA444P9BE] = { 1, 1 },
158     [AV_PIX_FMT_YUVA444P9LE] = { 1, 1 },
159     [AV_PIX_FMT_YUVA420P10BE]= { 1, 1 },
160     [AV_PIX_FMT_YUVA420P10LE]= { 1, 1 },
161     [AV_PIX_FMT_YUVA422P10BE]= { 1, 1 },
162     [AV_PIX_FMT_YUVA422P10LE]= { 1, 1 },
163     [AV_PIX_FMT_YUVA444P10BE]= { 1, 1 },
164     [AV_PIX_FMT_YUVA444P10LE]= { 1, 1 },
165     [AV_PIX_FMT_YUVA420P16BE]= { 1, 1 },
166     [AV_PIX_FMT_YUVA420P16LE]= { 1, 1 },
167     [AV_PIX_FMT_YUVA422P16BE]= { 1, 1 },
168     [AV_PIX_FMT_YUVA422P16LE]= { 1, 1 },
169     [AV_PIX_FMT_YUVA444P16BE]= { 1, 1 },
170     [AV_PIX_FMT_YUVA444P16LE]= { 1, 1 },
171     [AV_PIX_FMT_RGB48BE]     = { 1, 1 },
172     [AV_PIX_FMT_RGB48LE]     = { 1, 1 },
173     [AV_PIX_FMT_RGBA64BE]    = { 1, 1, 1 },
174     [AV_PIX_FMT_RGBA64LE]    = { 1, 1, 1 },
175     [AV_PIX_FMT_RGB565BE]    = { 1, 1 },
176     [AV_PIX_FMT_RGB565LE]    = { 1, 1 },
177     [AV_PIX_FMT_RGB555BE]    = { 1, 1 },
178     [AV_PIX_FMT_RGB555LE]    = { 1, 1 },
179     [AV_PIX_FMT_BGR565BE]    = { 1, 1 },
180     [AV_PIX_FMT_BGR565LE]    = { 1, 1 },
181     [AV_PIX_FMT_BGR555BE]    = { 1, 1 },
182     [AV_PIX_FMT_BGR555LE]    = { 1, 1 },
183     [AV_PIX_FMT_YUV420P16LE] = { 1, 1 },
184     [AV_PIX_FMT_YUV420P16BE] = { 1, 1 },
185     [AV_PIX_FMT_YUV422P16LE] = { 1, 1 },
186     [AV_PIX_FMT_YUV422P16BE] = { 1, 1 },
187     [AV_PIX_FMT_YUV444P16LE] = { 1, 1 },
188     [AV_PIX_FMT_YUV444P16BE] = { 1, 1 },
189     [AV_PIX_FMT_RGB444LE]    = { 1, 1 },
190     [AV_PIX_FMT_RGB444BE]    = { 1, 1 },
191     [AV_PIX_FMT_BGR444LE]    = { 1, 1 },
192     [AV_PIX_FMT_BGR444BE]    = { 1, 1 },
193     [AV_PIX_FMT_YA8]         = { 1, 1 },
194     [AV_PIX_FMT_YA16BE]      = { 1, 1 },
195     [AV_PIX_FMT_YA16LE]      = { 1, 1 },
196     [AV_PIX_FMT_BGR48BE]     = { 1, 1 },
197     [AV_PIX_FMT_BGR48LE]     = { 1, 1 },
198     [AV_PIX_FMT_BGRA64BE]    = { 1, 1, 1 },
199     [AV_PIX_FMT_BGRA64LE]    = { 1, 1, 1 },
200     [AV_PIX_FMT_YUV420P9BE]  = { 1, 1 },
201     [AV_PIX_FMT_YUV420P9LE]  = { 1, 1 },
202     [AV_PIX_FMT_YUV420P10BE] = { 1, 1 },
203     [AV_PIX_FMT_YUV420P10LE] = { 1, 1 },
204     [AV_PIX_FMT_YUV420P12BE] = { 1, 1 },
205     [AV_PIX_FMT_YUV420P12LE] = { 1, 1 },
206     [AV_PIX_FMT_YUV420P14BE] = { 1, 1 },
207     [AV_PIX_FMT_YUV420P14LE] = { 1, 1 },
208     [AV_PIX_FMT_YUV422P9BE]  = { 1, 1 },
209     [AV_PIX_FMT_YUV422P9LE]  = { 1, 1 },
210     [AV_PIX_FMT_YUV422P10BE] = { 1, 1 },
211     [AV_PIX_FMT_YUV422P10LE] = { 1, 1 },
212     [AV_PIX_FMT_YUV422P12BE] = { 1, 1 },
213     [AV_PIX_FMT_YUV422P12LE] = { 1, 1 },
214     [AV_PIX_FMT_YUV422P14BE] = { 1, 1 },
215     [AV_PIX_FMT_YUV422P14LE] = { 1, 1 },
216     [AV_PIX_FMT_YUV444P9BE]  = { 1, 1 },
217     [AV_PIX_FMT_YUV444P9LE]  = { 1, 1 },
218     [AV_PIX_FMT_YUV444P10BE] = { 1, 1 },
219     [AV_PIX_FMT_YUV444P10LE] = { 1, 1 },
220     [AV_PIX_FMT_YUV444P12BE] = { 1, 1 },
221     [AV_PIX_FMT_YUV444P12LE] = { 1, 1 },
222     [AV_PIX_FMT_YUV444P14BE] = { 1, 1 },
223     [AV_PIX_FMT_YUV444P14LE] = { 1, 1 },
224     [AV_PIX_FMT_GBRP]        = { 1, 1 },
225     [AV_PIX_FMT_GBRP9LE]     = { 1, 1 },
226     [AV_PIX_FMT_GBRP9BE]     = { 1, 1 },
227     [AV_PIX_FMT_GBRP10LE]    = { 1, 1 },
228     [AV_PIX_FMT_GBRP10BE]    = { 1, 1 },
229     [AV_PIX_FMT_GBRAP10LE]   = { 1, 1 },
230     [AV_PIX_FMT_GBRAP10BE]   = { 1, 1 },
231     [AV_PIX_FMT_GBRP12LE]    = { 1, 1 },
232     [AV_PIX_FMT_GBRP12BE]    = { 1, 1 },
233     [AV_PIX_FMT_GBRAP12LE]   = { 1, 1 },
234     [AV_PIX_FMT_GBRAP12BE]   = { 1, 1 },
235     [AV_PIX_FMT_GBRP14LE]    = { 1, 1 },
236     [AV_PIX_FMT_GBRP14BE]    = { 1, 1 },
237     [AV_PIX_FMT_GBRP16LE]    = { 1, 1 },
238     [AV_PIX_FMT_GBRP16BE]    = { 1, 1 },
239     [AV_PIX_FMT_GBRPF32LE]   = { 1, 1 },
240     [AV_PIX_FMT_GBRPF32BE]   = { 1, 1 },
241     [AV_PIX_FMT_GBRAPF32LE]  = { 1, 1 },
242     [AV_PIX_FMT_GBRAPF32BE]  = { 1, 1 },
243     [AV_PIX_FMT_GBRAP]       = { 1, 1 },
244     [AV_PIX_FMT_GBRAP16LE]   = { 1, 1 },
245     [AV_PIX_FMT_GBRAP16BE]   = { 1, 1 },
246     [AV_PIX_FMT_BAYER_BGGR8] = { 1, 0 },
247     [AV_PIX_FMT_BAYER_RGGB8] = { 1, 0 },
248     [AV_PIX_FMT_BAYER_GBRG8] = { 1, 0 },
249     [AV_PIX_FMT_BAYER_GRBG8] = { 1, 0 },
250     [AV_PIX_FMT_BAYER_BGGR16LE] = { 1, 0 },
251     [AV_PIX_FMT_BAYER_BGGR16BE] = { 1, 0 },
252     [AV_PIX_FMT_BAYER_RGGB16LE] = { 1, 0 },
253     [AV_PIX_FMT_BAYER_RGGB16BE] = { 1, 0 },
254     [AV_PIX_FMT_BAYER_GBRG16LE] = { 1, 0 },
255     [AV_PIX_FMT_BAYER_GBRG16BE] = { 1, 0 },
256     [AV_PIX_FMT_BAYER_GRBG16LE] = { 1, 0 },
257     [AV_PIX_FMT_BAYER_GRBG16BE] = { 1, 0 },
258     [AV_PIX_FMT_XYZ12BE]     = { 1, 1, 1 },
259     [AV_PIX_FMT_XYZ12LE]     = { 1, 1, 1 },
260     [AV_PIX_FMT_AYUV64LE]    = { 1, 1},
261     [AV_PIX_FMT_P010LE]      = { 1, 1 },
262     [AV_PIX_FMT_P010BE]      = { 1, 1 },
263     [AV_PIX_FMT_P016LE]      = { 1, 1 },
264     [AV_PIX_FMT_P016BE]      = { 1, 1 },
265     [AV_PIX_FMT_GRAYF32LE]   = { 1, 1 },
266     [AV_PIX_FMT_GRAYF32BE]   = { 1, 1 },
267     [AV_PIX_FMT_YUVA422P12BE] = { 1, 1 },
268     [AV_PIX_FMT_YUVA422P12LE] = { 1, 1 },
269     [AV_PIX_FMT_YUVA444P12BE] = { 1, 1 },
270     [AV_PIX_FMT_YUVA444P12LE] = { 1, 1 },
271     [AV_PIX_FMT_NV24]        = { 1, 1 },
272     [AV_PIX_FMT_NV42]        = { 1, 1 },
273     [AV_PIX_FMT_Y210LE]      = { 1, 0 },
274     [AV_PIX_FMT_X2RGB10LE]   = { 1, 1 },
275 };
276 
sws_isSupportedInput(enum AVPixelFormat pix_fmt)277 int sws_isSupportedInput(enum AVPixelFormat pix_fmt)
278 {
279     return (unsigned)pix_fmt < FF_ARRAY_ELEMS(format_entries) ?
280            format_entries[pix_fmt].is_supported_in : 0;
281 }
282 
sws_isSupportedOutput(enum AVPixelFormat pix_fmt)283 int sws_isSupportedOutput(enum AVPixelFormat pix_fmt)
284 {
285     return (unsigned)pix_fmt < FF_ARRAY_ELEMS(format_entries) ?
286            format_entries[pix_fmt].is_supported_out : 0;
287 }
288 
sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt)289 int sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt)
290 {
291     return (unsigned)pix_fmt < FF_ARRAY_ELEMS(format_entries) ?
292            format_entries[pix_fmt].is_supported_endianness : 0;
293 }
294 
getSplineCoeff(double a,double b,double c,double d,double dist)295 static double getSplineCoeff(double a, double b, double c, double d,
296                              double dist)
297 {
298     if (dist <= 1.0)
299         return ((d * dist + c) * dist + b) * dist + a;
300     else
301         return getSplineCoeff(0.0,
302                                b + 2.0 * c + 3.0 * d,
303                                c + 3.0 * d,
304                               -b - 3.0 * c - 6.0 * d,
305                               dist - 1.0);
306 }
307 
get_local_pos(SwsContext * s,int chr_subsample,int pos,int dir)308 static av_cold int get_local_pos(SwsContext *s, int chr_subsample, int pos, int dir)
309 {
310     if (pos == -1 || pos <= -513) {
311         pos = (128 << chr_subsample) - 128;
312     }
313     pos += 128; // relative to ideal left edge
314     return pos >> chr_subsample;
315 }
316 
317 typedef struct {
318     int flag;                   ///< flag associated to the algorithm
319     const char *description;    ///< human-readable description
320     int size_factor;            ///< size factor used when initing the filters
321 } ScaleAlgorithm;
322 
323 static const ScaleAlgorithm scale_algorithms[] = {
324     { SWS_AREA,          "area averaging",                  1 /* downscale only, for upscale it is bilinear */ },
325     { SWS_BICUBIC,       "bicubic",                         4 },
326     { SWS_BICUBLIN,      "luma bicubic / chroma bilinear", -1 },
327     { SWS_BILINEAR,      "bilinear",                        2 },
328     { SWS_FAST_BILINEAR, "fast bilinear",                  -1 },
329     { SWS_GAUSS,         "Gaussian",                        8 /* infinite ;) */ },
330     { SWS_LANCZOS,       "Lanczos",                        -1 /* custom */ },
331     { SWS_POINT,         "nearest neighbor / point",       -1 },
332     { SWS_SINC,          "sinc",                           20 /* infinite ;) */ },
333     { SWS_SPLINE,        "bicubic spline",                 20 /* infinite :)*/ },
334     { SWS_X,             "experimental",                    8 },
335 };
336 
initFilter(int16_t ** outFilter,int32_t ** filterPos,int * outFilterSize,int xInc,int srcW,int dstW,int filterAlign,int one,int flags,int cpu_flags,SwsVector * srcFilter,SwsVector * dstFilter,double param[2],int srcPos,int dstPos)337 static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
338                               int *outFilterSize, int xInc, int srcW,
339                               int dstW, int filterAlign, int one,
340                               int flags, int cpu_flags,
341                               SwsVector *srcFilter, SwsVector *dstFilter,
342                               double param[2], int srcPos, int dstPos)
343 {
344     int i;
345     int filterSize;
346     int filter2Size;
347     int minFilterSize;
348     int64_t *filter    = NULL;
349     int64_t *filter2   = NULL;
350     const int64_t fone = 1LL << (54 - FFMIN(av_log2(srcW/dstW), 8));
351     int ret            = -1;
352 
353     emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
354 
355     // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
356     if (!FF_ALLOC_TYPED_ARRAY(*filterPos, dstW + 3))
357         goto nomem;
358 
359     if (FFABS(xInc - 0x10000) < 10 && srcPos == dstPos) { // unscaled
360         int i;
361         filterSize = 1;
362         if (!FF_ALLOCZ_TYPED_ARRAY(filter, dstW * filterSize))
363             goto nomem;
364 
365         for (i = 0; i < dstW; i++) {
366             filter[i * filterSize] = fone;
367             (*filterPos)[i]        = i;
368         }
369     } else if (flags & SWS_POINT) { // lame looking point sampling mode
370         int i;
371         int64_t xDstInSrc;
372         filterSize = 1;
373         if (!FF_ALLOC_TYPED_ARRAY(filter, dstW * filterSize))
374             goto nomem;
375 
376         xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
377         for (i = 0; i < dstW; i++) {
378             int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
379 
380             (*filterPos)[i] = xx;
381             filter[i]       = fone;
382             xDstInSrc      += xInc;
383         }
384     } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
385                (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
386         int i;
387         int64_t xDstInSrc;
388         filterSize = 2;
389         if (!FF_ALLOC_TYPED_ARRAY(filter, dstW * filterSize))
390             goto nomem;
391 
392         xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
393         for (i = 0; i < dstW; i++) {
394             int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
395             int j;
396 
397             (*filterPos)[i] = xx;
398             // bilinear upscale / linear interpolate / area averaging
399             for (j = 0; j < filterSize; j++) {
400                 int64_t coeff = fone - FFABS((int64_t)xx * (1 << 16) - xDstInSrc) * (fone >> 16);
401                 if (coeff < 0)
402                     coeff = 0;
403                 filter[i * filterSize + j] = coeff;
404                 xx++;
405             }
406             xDstInSrc += xInc;
407         }
408     } else {
409         int64_t xDstInSrc;
410         int sizeFactor = -1;
411 
412         for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
413             if (flags & scale_algorithms[i].flag && scale_algorithms[i].size_factor > 0) {
414                 sizeFactor = scale_algorithms[i].size_factor;
415                 break;
416             }
417         }
418         if (flags & SWS_LANCZOS)
419             sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
420         av_assert0(sizeFactor > 0);
421 
422         if (xInc <= 1 << 16)
423             filterSize = 1 + sizeFactor;    // upscale
424         else
425             filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
426 
427         filterSize = FFMIN(filterSize, srcW - 2);
428         filterSize = FFMAX(filterSize, 1);
429 
430         if (!FF_ALLOC_TYPED_ARRAY(filter, dstW * filterSize))
431             goto nomem;
432         xDstInSrc = ((dstPos*(int64_t)xInc)>>7) - ((srcPos*0x10000LL)>>7);
433         for (i = 0; i < dstW; i++) {
434             int xx = (xDstInSrc - (filterSize - 2) * (1LL<<16)) / (1 << 17);
435             int j;
436             (*filterPos)[i] = xx;
437             for (j = 0; j < filterSize; j++) {
438                 int64_t d = (FFABS(((int64_t)xx * (1 << 17)) - xDstInSrc)) << 13;
439                 double floatd;
440                 int64_t coeff;
441 
442                 if (xInc > 1 << 16)
443                     d = d * dstW / srcW;
444                 floatd = d * (1.0 / (1 << 30));
445 
446                 if (flags & SWS_BICUBIC) {
447                     int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] :   0) * (1 << 24);
448                     int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
449 
450                     if (d >= 1LL << 31) {
451                         coeff = 0.0;
452                     } else {
453                         int64_t dd  = (d  * d) >> 30;
454                         int64_t ddd = (dd * d) >> 30;
455 
456                         if (d < 1LL << 30)
457                             coeff =  (12 * (1 << 24) -  9 * B - 6 * C) * ddd +
458                                     (-18 * (1 << 24) + 12 * B + 6 * C) *  dd +
459                                       (6 * (1 << 24) -  2 * B)         * (1 << 30);
460                         else
461                             coeff =      (-B -  6 * C) * ddd +
462                                       (6 * B + 30 * C) * dd  +
463                                     (-12 * B - 48 * C) * d   +
464                                       (8 * B + 24 * C) * (1 << 30);
465                     }
466                     coeff /= (1LL<<54)/fone;
467                 } else if (flags & SWS_X) {
468                     double A = param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
469                     double c;
470 
471                     if (floatd < 1.0)
472                         c = cos(floatd * M_PI);
473                     else
474                         c = -1.0;
475                     if (c < 0.0)
476                         c = -pow(-c, A);
477                     else
478                         c = pow(c, A);
479                     coeff = (c * 0.5 + 0.5) * fone;
480                 } else if (flags & SWS_AREA) {
481                     int64_t d2 = d - (1 << 29);
482                     if (d2 * xInc < -(1LL << (29 + 16)))
483                         coeff = 1.0 * (1LL << (30 + 16));
484                     else if (d2 * xInc < (1LL << (29 + 16)))
485                         coeff = -d2 * xInc + (1LL << (29 + 16));
486                     else
487                         coeff = 0.0;
488                     coeff *= fone >> (30 + 16);
489                 } else if (flags & SWS_GAUSS) {
490                     double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
491                     coeff = exp2(-p * floatd * floatd) * fone;
492                 } else if (flags & SWS_SINC) {
493                     coeff = (d ? sin(floatd * M_PI) / (floatd * M_PI) : 1.0) * fone;
494                 } else if (flags & SWS_LANCZOS) {
495                     double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
496                     coeff = (d ? sin(floatd * M_PI) * sin(floatd * M_PI / p) /
497                              (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
498                     if (floatd > p)
499                         coeff = 0;
500                 } else if (flags & SWS_BILINEAR) {
501                     coeff = (1 << 30) - d;
502                     if (coeff < 0)
503                         coeff = 0;
504                     coeff *= fone >> 30;
505                 } else if (flags & SWS_SPLINE) {
506                     double p = -2.196152422706632;
507                     coeff = getSplineCoeff(1.0, 0.0, p, -p - 1.0, floatd) * fone;
508                 } else {
509                     av_assert0(0);
510                 }
511 
512                 filter[i * filterSize + j] = coeff;
513                 xx++;
514             }
515             xDstInSrc += 2 * xInc;
516         }
517     }
518 
519     /* apply src & dst Filter to filter -> filter2
520      * av_free(filter);
521      */
522     av_assert0(filterSize > 0);
523     filter2Size = filterSize;
524     if (srcFilter)
525         filter2Size += srcFilter->length - 1;
526     if (dstFilter)
527         filter2Size += dstFilter->length - 1;
528     av_assert0(filter2Size > 0);
529     if (!FF_ALLOCZ_TYPED_ARRAY(filter2, dstW * filter2Size))
530         goto nomem;
531     for (i = 0; i < dstW; i++) {
532         int j, k;
533 
534         if (srcFilter) {
535             for (k = 0; k < srcFilter->length; k++) {
536                 for (j = 0; j < filterSize; j++)
537                     filter2[i * filter2Size + k + j] +=
538                         srcFilter->coeff[k] * filter[i * filterSize + j];
539             }
540         } else {
541             for (j = 0; j < filterSize; j++)
542                 filter2[i * filter2Size + j] = filter[i * filterSize + j];
543         }
544         // FIXME dstFilter
545 
546         (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
547     }
548     av_freep(&filter);
549 
550     /* try to reduce the filter-size (step1 find size and shift left) */
551     // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
552     minFilterSize = 0;
553     for (i = dstW - 1; i >= 0; i--) {
554         int min = filter2Size;
555         int j;
556         int64_t cutOff = 0.0;
557 
558         /* get rid of near zero elements on the left by shifting left */
559         for (j = 0; j < filter2Size; j++) {
560             int k;
561             cutOff += FFABS(filter2[i * filter2Size]);
562 
563             if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
564                 break;
565 
566             /* preserve monotonicity because the core can't handle the
567              * filter otherwise */
568             if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
569                 break;
570 
571             // move filter coefficients left
572             for (k = 1; k < filter2Size; k++)
573                 filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
574             filter2[i * filter2Size + k - 1] = 0;
575             (*filterPos)[i]++;
576         }
577 
578         cutOff = 0;
579         /* count near zeros on the right */
580         for (j = filter2Size - 1; j > 0; j--) {
581             cutOff += FFABS(filter2[i * filter2Size + j]);
582 
583             if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
584                 break;
585             min--;
586         }
587 
588         if (min > minFilterSize)
589             minFilterSize = min;
590     }
591 
592     if (PPC_ALTIVEC(cpu_flags)) {
593         // we can handle the special case 4, so we don't want to go the full 8
594         if (minFilterSize < 5)
595             filterAlign = 4;
596 
597         /* We really don't want to waste our time doing useless computation, so
598          * fall back on the scalar C code for very small filters.
599          * Vectorizing is worth it only if you have a decent-sized vector. */
600         if (minFilterSize < 3)
601             filterAlign = 1;
602     }
603 
604     if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
605         // special case for unscaled vertical filtering
606         if (minFilterSize == 1 && filterAlign == 2)
607             filterAlign = 1;
608     }
609 
610     av_assert0(minFilterSize > 0);
611     filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
612     av_assert0(filterSize > 0);
613     filter = av_malloc_array(dstW, filterSize * sizeof(*filter));
614     if (!filter)
615         goto nomem;
616     if (filterSize >= MAX_FILTER_SIZE * 16 /
617                       ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16)) {
618         ret = RETCODE_USE_CASCADE;
619         goto fail;
620     }
621     *outFilterSize = filterSize;
622 
623     if (flags & SWS_PRINT_INFO)
624         av_log(NULL, AV_LOG_VERBOSE,
625                "SwScaler: reducing / aligning filtersize %d -> %d\n",
626                filter2Size, filterSize);
627     /* try to reduce the filter-size (step2 reduce it) */
628     for (i = 0; i < dstW; i++) {
629         int j;
630 
631         for (j = 0; j < filterSize; j++) {
632             if (j >= filter2Size)
633                 filter[i * filterSize + j] = 0;
634             else
635                 filter[i * filterSize + j] = filter2[i * filter2Size + j];
636             if ((flags & SWS_BITEXACT) && j >= minFilterSize)
637                 filter[i * filterSize + j] = 0;
638         }
639     }
640 
641     // FIXME try to align filterPos if possible
642 
643     // fix borders
644     for (i = 0; i < dstW; i++) {
645         int j;
646         if ((*filterPos)[i] < 0) {
647             // move filter coefficients left to compensate for filterPos
648             for (j = 1; j < filterSize; j++) {
649                 int left = FFMAX(j + (*filterPos)[i], 0);
650                 filter[i * filterSize + left] += filter[i * filterSize + j];
651                 filter[i * filterSize + j]     = 0;
652             }
653             (*filterPos)[i]= 0;
654         }
655 
656         if ((*filterPos)[i] + filterSize > srcW) {
657             int shift = (*filterPos)[i] + FFMIN(filterSize - srcW, 0);
658             int64_t acc = 0;
659 
660             for (j = filterSize - 1; j >= 0; j--) {
661                 if ((*filterPos)[i] + j >= srcW) {
662                     acc += filter[i * filterSize + j];
663                     filter[i * filterSize + j] = 0;
664                 }
665             }
666             for (j = filterSize - 1; j >= 0; j--) {
667                 if (j < shift) {
668                     filter[i * filterSize + j] = 0;
669                 } else {
670                     filter[i * filterSize + j] = filter[i * filterSize + j - shift];
671                 }
672             }
673 
674             (*filterPos)[i]-= shift;
675             filter[i * filterSize + srcW - 1 - (*filterPos)[i]] += acc;
676         }
677         av_assert0((*filterPos)[i] >= 0);
678         av_assert0((*filterPos)[i] < srcW);
679         if ((*filterPos)[i] + filterSize > srcW) {
680             for (j = 0; j < filterSize; j++) {
681                 av_assert0((*filterPos)[i] + j < srcW || !filter[i * filterSize + j]);
682             }
683         }
684     }
685 
686     // Note the +1 is for the MMX scaler which reads over the end
687     /* align at 16 for AltiVec (needed by hScale_altivec_real) */
688     if (!FF_ALLOCZ_TYPED_ARRAY(*outFilter, *outFilterSize * (dstW + 3)))
689         goto nomem;
690 
691     /* normalize & store in outFilter */
692     for (i = 0; i < dstW; i++) {
693         int j;
694         int64_t error = 0;
695         int64_t sum   = 0;
696 
697         for (j = 0; j < filterSize; j++) {
698             sum += filter[i * filterSize + j];
699         }
700         sum = (sum + one / 2) / one;
701         if (!sum) {
702             av_log(NULL, AV_LOG_WARNING, "SwScaler: zero vector in scaling\n");
703             sum = 1;
704         }
705         for (j = 0; j < *outFilterSize; j++) {
706             int64_t v = filter[i * filterSize + j] + error;
707             int intV  = ROUNDED_DIV(v, sum);
708             (*outFilter)[i * (*outFilterSize) + j] = intV;
709             error                                  = v - intV * sum;
710         }
711     }
712 
713     (*filterPos)[dstW + 0] =
714     (*filterPos)[dstW + 1] =
715     (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1]; /* the MMX/SSE scaler will
716                                                       * read over the end */
717     for (i = 0; i < *outFilterSize; i++) {
718         int k = (dstW - 1) * (*outFilterSize) + i;
719         (*outFilter)[k + 1 * (*outFilterSize)] =
720         (*outFilter)[k + 2 * (*outFilterSize)] =
721         (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
722     }
723 
724     ret = 0;
725     goto done;
726 nomem:
727     ret = AVERROR(ENOMEM);
728 fail:
729     if(ret < 0)
730         av_log(NULL, ret == RETCODE_USE_CASCADE ? AV_LOG_DEBUG : AV_LOG_ERROR, "sws: initFilter failed\n");
731 done:
732     av_free(filter);
733     av_free(filter2);
734     return ret;
735 }
736 
fill_rgb2yuv_table(SwsContext * c,const int table[4],int dstRange)737 static void fill_rgb2yuv_table(SwsContext *c, const int table[4], int dstRange)
738 {
739     int64_t W, V, Z, Cy, Cu, Cv;
740     int64_t vr =  table[0];
741     int64_t ub =  table[1];
742     int64_t ug = -table[2];
743     int64_t vg = -table[3];
744     int64_t ONE = 65536;
745     int64_t cy = ONE;
746     uint8_t *p = (uint8_t*)c->input_rgb2yuv_table;
747     int i;
748     static const int8_t map[] = {
749     BY_IDX, GY_IDX, -1    , BY_IDX, BY_IDX, GY_IDX, -1    , BY_IDX,
750     RY_IDX, -1    , GY_IDX, RY_IDX, RY_IDX, -1    , GY_IDX, RY_IDX,
751     RY_IDX, GY_IDX, -1    , RY_IDX, RY_IDX, GY_IDX, -1    , RY_IDX,
752     BY_IDX, -1    , GY_IDX, BY_IDX, BY_IDX, -1    , GY_IDX, BY_IDX,
753     BU_IDX, GU_IDX, -1    , BU_IDX, BU_IDX, GU_IDX, -1    , BU_IDX,
754     RU_IDX, -1    , GU_IDX, RU_IDX, RU_IDX, -1    , GU_IDX, RU_IDX,
755     RU_IDX, GU_IDX, -1    , RU_IDX, RU_IDX, GU_IDX, -1    , RU_IDX,
756     BU_IDX, -1    , GU_IDX, BU_IDX, BU_IDX, -1    , GU_IDX, BU_IDX,
757     BV_IDX, GV_IDX, -1    , BV_IDX, BV_IDX, GV_IDX, -1    , BV_IDX,
758     RV_IDX, -1    , GV_IDX, RV_IDX, RV_IDX, -1    , GV_IDX, RV_IDX,
759     RV_IDX, GV_IDX, -1    , RV_IDX, RV_IDX, GV_IDX, -1    , RV_IDX,
760     BV_IDX, -1    , GV_IDX, BV_IDX, BV_IDX, -1    , GV_IDX, BV_IDX,
761     RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX,
762     BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX,
763     GY_IDX, -1    , GY_IDX, -1    , GY_IDX, -1    , GY_IDX, -1    ,
764     -1    , GY_IDX, -1    , GY_IDX, -1    , GY_IDX, -1    , GY_IDX,
765     RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX,
766     BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX,
767     GU_IDX, -1    , GU_IDX, -1    , GU_IDX, -1    , GU_IDX, -1    ,
768     -1    , GU_IDX, -1    , GU_IDX, -1    , GU_IDX, -1    , GU_IDX,
769     RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX,
770     BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX,
771     GV_IDX, -1    , GV_IDX, -1    , GV_IDX, -1    , GV_IDX, -1    ,
772     -1    , GV_IDX, -1    , GV_IDX, -1    , GV_IDX, -1    , GV_IDX, //23
773     -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //24
774     -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //25
775     -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //26
776     -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //27
777     -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //28
778     -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //29
779     -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //30
780     -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //31
781     BY_IDX, GY_IDX, RY_IDX, -1    , -1    , -1    , -1    , -1    , //32
782     BU_IDX, GU_IDX, RU_IDX, -1    , -1    , -1    , -1    , -1    , //33
783     BV_IDX, GV_IDX, RV_IDX, -1    , -1    , -1    , -1    , -1    , //34
784     };
785 
786     dstRange = 0; //FIXME range = 1 is handled elsewhere
787 
788     if (!dstRange) {
789         cy = cy * 255 / 219;
790     } else {
791         vr = vr * 224 / 255;
792         ub = ub * 224 / 255;
793         ug = ug * 224 / 255;
794         vg = vg * 224 / 255;
795     }
796     W = ROUNDED_DIV(ONE*ONE*ug, ub);
797     V = ROUNDED_DIV(ONE*ONE*vg, vr);
798     Z = ONE*ONE-W-V;
799 
800     Cy = ROUNDED_DIV(cy*Z, ONE);
801     Cu = ROUNDED_DIV(ub*Z, ONE);
802     Cv = ROUNDED_DIV(vr*Z, ONE);
803 
804     c->input_rgb2yuv_table[RY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V        , Cy);
805     c->input_rgb2yuv_table[GY_IDX] =  ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE  , Cy);
806     c->input_rgb2yuv_table[BY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W        , Cy);
807 
808     c->input_rgb2yuv_table[RU_IDX] =  ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V        , Cu);
809     c->input_rgb2yuv_table[GU_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE  , Cu);
810     c->input_rgb2yuv_table[BU_IDX] =  ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(Z+W)    , Cu);
811 
812     c->input_rgb2yuv_table[RV_IDX] =  ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(V+Z)    , Cv);
813     c->input_rgb2yuv_table[GV_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE  , Cv);
814     c->input_rgb2yuv_table[BV_IDX] =  ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W        , Cv);
815 
816     if(/*!dstRange && */!memcmp(table, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], sizeof(ff_yuv2rgb_coeffs[SWS_CS_DEFAULT]))) {
817         c->input_rgb2yuv_table[BY_IDX] =  ((int)(0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
818         c->input_rgb2yuv_table[BV_IDX] = (-(int)(0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
819         c->input_rgb2yuv_table[BU_IDX] =  ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
820         c->input_rgb2yuv_table[GY_IDX] =  ((int)(0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
821         c->input_rgb2yuv_table[GV_IDX] = (-(int)(0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
822         c->input_rgb2yuv_table[GU_IDX] = (-(int)(0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
823         c->input_rgb2yuv_table[RY_IDX] =  ((int)(0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
824         c->input_rgb2yuv_table[RV_IDX] =  ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
825         c->input_rgb2yuv_table[RU_IDX] = (-(int)(0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
826     }
827     for(i=0; i<FF_ARRAY_ELEMS(map); i++)
828         AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ? c->input_rgb2yuv_table[map[i]] : 0);
829 }
830 
fill_xyztables(struct SwsContext * c)831 static void fill_xyztables(struct SwsContext *c)
832 {
833     int i;
834     double xyzgamma = XYZ_GAMMA;
835     double rgbgamma = 1.0 / RGB_GAMMA;
836     double xyzgammainv = 1.0 / XYZ_GAMMA;
837     double rgbgammainv = RGB_GAMMA;
838     static const int16_t xyz2rgb_matrix[3][4] = {
839         {13270, -6295, -2041},
840         {-3969,  7682,   170},
841         {  228,  -835,  4329} };
842     static const int16_t rgb2xyz_matrix[3][4] = {
843         {1689, 1464,  739},
844         { 871, 2929,  296},
845         {  79,  488, 3891} };
846     static int16_t xyzgamma_tab[4096], rgbgamma_tab[4096], xyzgammainv_tab[4096], rgbgammainv_tab[4096];
847 
848     memcpy(c->xyz2rgb_matrix, xyz2rgb_matrix, sizeof(c->xyz2rgb_matrix));
849     memcpy(c->rgb2xyz_matrix, rgb2xyz_matrix, sizeof(c->rgb2xyz_matrix));
850     c->xyzgamma = xyzgamma_tab;
851     c->rgbgamma = rgbgamma_tab;
852     c->xyzgammainv = xyzgammainv_tab;
853     c->rgbgammainv = rgbgammainv_tab;
854 
855     if (rgbgamma_tab[4095])
856         return;
857 
858     /* set gamma vectors */
859     for (i = 0; i < 4096; i++) {
860         xyzgamma_tab[i] = lrint(pow(i / 4095.0, xyzgamma) * 4095.0);
861         rgbgamma_tab[i] = lrint(pow(i / 4095.0, rgbgamma) * 4095.0);
862         xyzgammainv_tab[i] = lrint(pow(i / 4095.0, xyzgammainv) * 4095.0);
863         rgbgammainv_tab[i] = lrint(pow(i / 4095.0, rgbgammainv) * 4095.0);
864     }
865 }
866 
range_override_needed(enum AVPixelFormat format)867 static int range_override_needed(enum AVPixelFormat format)
868 {
869     return !isYUV(format) && !isGray(format);
870 }
871 
sws_setColorspaceDetails(struct SwsContext * c,const int inv_table[4],int srcRange,const int table[4],int dstRange,int brightness,int contrast,int saturation)872 int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
873                              int srcRange, const int table[4], int dstRange,
874                              int brightness, int contrast, int saturation)
875 {
876     const AVPixFmtDescriptor *desc_dst;
877     const AVPixFmtDescriptor *desc_src;
878     int need_reinit = 0;
879 
880     handle_formats(c);
881     desc_dst = av_pix_fmt_desc_get(c->dstFormat);
882     desc_src = av_pix_fmt_desc_get(c->srcFormat);
883 
884     if(range_override_needed(c->dstFormat))
885         dstRange = 0;
886     if(range_override_needed(c->srcFormat))
887         srcRange = 0;
888 
889     if (c->srcRange != srcRange ||
890         c->dstRange != dstRange ||
891         c->brightness != brightness ||
892         c->contrast   != contrast ||
893         c->saturation != saturation ||
894         memcmp(c->srcColorspaceTable, inv_table, sizeof(int) * 4) ||
895         memcmp(c->dstColorspaceTable,     table, sizeof(int) * 4)
896     )
897         need_reinit = 1;
898 
899     memmove(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
900     memmove(c->dstColorspaceTable, table, sizeof(int) * 4);
901 
902 
903 
904     c->brightness = brightness;
905     c->contrast   = contrast;
906     c->saturation = saturation;
907     c->srcRange   = srcRange;
908     c->dstRange   = dstRange;
909 
910     //The srcBpc check is possibly wrong but we seem to lack a definitive reference to test this
911     //and what we have in ticket 2939 looks better with this check
912     if (need_reinit && (c->srcBpc == 8 || !isYUV(c->srcFormat)))
913         ff_sws_init_range_convert(c);
914 
915     c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
916     c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
917 
918     if (c->cascaded_context[c->cascaded_mainindex])
919         return sws_setColorspaceDetails(c->cascaded_context[c->cascaded_mainindex],inv_table, srcRange,table, dstRange, brightness,  contrast, saturation);
920 
921     if (!need_reinit)
922         return 0;
923 
924     if ((isYUV(c->dstFormat) || isGray(c->dstFormat)) && (isYUV(c->srcFormat) || isGray(c->srcFormat))) {
925         if (!c->cascaded_context[0] &&
926             memcmp(c->dstColorspaceTable, c->srcColorspaceTable, sizeof(int) * 4) &&
927             c->srcW && c->srcH && c->dstW && c->dstH) {
928             enum AVPixelFormat tmp_format;
929             int tmp_width, tmp_height;
930             int srcW = c->srcW;
931             int srcH = c->srcH;
932             int dstW = c->dstW;
933             int dstH = c->dstH;
934             int ret;
935             av_log(c, AV_LOG_VERBOSE, "YUV color matrix differs for YUV->YUV, using intermediate RGB to convert\n");
936 
937             if (isNBPS(c->dstFormat) || is16BPS(c->dstFormat)) {
938                 if (isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) {
939                     tmp_format = AV_PIX_FMT_BGRA64;
940                 } else {
941                     tmp_format = AV_PIX_FMT_BGR48;
942                 }
943             } else {
944                 if (isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) {
945                     tmp_format = AV_PIX_FMT_BGRA;
946                 } else {
947                     tmp_format = AV_PIX_FMT_BGR24;
948                 }
949             }
950 
951             if (srcW*srcH > dstW*dstH) {
952                 tmp_width  = dstW;
953                 tmp_height = dstH;
954             } else {
955                 tmp_width  = srcW;
956                 tmp_height = srcH;
957             }
958 
959             ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
960                                 tmp_width, tmp_height, tmp_format, 64);
961             if (ret < 0)
962                 return ret;
963 
964             c->cascaded_context[0] = sws_alloc_set_opts(srcW, srcH, c->srcFormat,
965                                                         tmp_width, tmp_height, tmp_format,
966                                                         c->flags, c->param);
967             if (!c->cascaded_context[0])
968                 return -1;
969 
970             c->cascaded_context[0]->alphablend = c->alphablend;
971             ret = sws_init_context(c->cascaded_context[0], NULL , NULL);
972             if (ret < 0)
973                 return ret;
974             //we set both src and dst depending on that the RGB side will be ignored
975             sws_setColorspaceDetails(c->cascaded_context[0], inv_table,
976                                      srcRange, table, dstRange,
977                                      brightness, contrast, saturation);
978 
979             c->cascaded_context[1] = sws_getContext(tmp_width, tmp_height, tmp_format,
980                                                     dstW, dstH, c->dstFormat,
981                                                     c->flags, NULL, NULL, c->param);
982             if (!c->cascaded_context[1])
983                 return -1;
984             sws_setColorspaceDetails(c->cascaded_context[1], inv_table,
985                                      srcRange, table, dstRange,
986                                      0, 1 << 16, 1 << 16);
987             return 0;
988         }
989         return -1;
990     }
991 
992     if (!isYUV(c->dstFormat) && !isGray(c->dstFormat)) {
993         ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
994                                  contrast, saturation);
995         // FIXME factorize
996 
997         if (ARCH_PPC)
998             ff_yuv2rgb_init_tables_ppc(c, inv_table, brightness,
999                                        contrast, saturation);
1000     }
1001 
1002     fill_rgb2yuv_table(c, table, dstRange);
1003 
1004     return 0;
1005 }
1006 
sws_getColorspaceDetails(struct SwsContext * c,int ** inv_table,int * srcRange,int ** table,int * dstRange,int * brightness,int * contrast,int * saturation)1007 int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
1008                              int *srcRange, int **table, int *dstRange,
1009                              int *brightness, int *contrast, int *saturation)
1010 {
1011     if (!c )
1012         return -1;
1013 
1014     *inv_table  = c->srcColorspaceTable;
1015     *table      = c->dstColorspaceTable;
1016     *srcRange   = range_override_needed(c->srcFormat) ? 1 : c->srcRange;
1017     *dstRange   = range_override_needed(c->dstFormat) ? 1 : c->dstRange;
1018     *brightness = c->brightness;
1019     *contrast   = c->contrast;
1020     *saturation = c->saturation;
1021 
1022     return 0;
1023 }
1024 
handle_jpeg(enum AVPixelFormat * format)1025 static int handle_jpeg(enum AVPixelFormat *format)
1026 {
1027     switch (*format) {
1028     case AV_PIX_FMT_YUVJ420P:
1029         *format = AV_PIX_FMT_YUV420P;
1030         return 1;
1031     case AV_PIX_FMT_YUVJ411P:
1032         *format = AV_PIX_FMT_YUV411P;
1033         return 1;
1034     case AV_PIX_FMT_YUVJ422P:
1035         *format = AV_PIX_FMT_YUV422P;
1036         return 1;
1037     case AV_PIX_FMT_YUVJ444P:
1038         *format = AV_PIX_FMT_YUV444P;
1039         return 1;
1040     case AV_PIX_FMT_YUVJ440P:
1041         *format = AV_PIX_FMT_YUV440P;
1042         return 1;
1043     case AV_PIX_FMT_GRAY8:
1044     case AV_PIX_FMT_YA8:
1045     case AV_PIX_FMT_GRAY9LE:
1046     case AV_PIX_FMT_GRAY9BE:
1047     case AV_PIX_FMT_GRAY10LE:
1048     case AV_PIX_FMT_GRAY10BE:
1049     case AV_PIX_FMT_GRAY12LE:
1050     case AV_PIX_FMT_GRAY12BE:
1051     case AV_PIX_FMT_GRAY14LE:
1052     case AV_PIX_FMT_GRAY14BE:
1053     case AV_PIX_FMT_GRAY16LE:
1054     case AV_PIX_FMT_GRAY16BE:
1055     case AV_PIX_FMT_YA16BE:
1056     case AV_PIX_FMT_YA16LE:
1057         return 1;
1058     default:
1059         return 0;
1060     }
1061 }
1062 
handle_0alpha(enum AVPixelFormat * format)1063 static int handle_0alpha(enum AVPixelFormat *format)
1064 {
1065     switch (*format) {
1066     case AV_PIX_FMT_0BGR    : *format = AV_PIX_FMT_ABGR   ; return 1;
1067     case AV_PIX_FMT_BGR0    : *format = AV_PIX_FMT_BGRA   ; return 4;
1068     case AV_PIX_FMT_0RGB    : *format = AV_PIX_FMT_ARGB   ; return 1;
1069     case AV_PIX_FMT_RGB0    : *format = AV_PIX_FMT_RGBA   ; return 4;
1070     default:                                          return 0;
1071     }
1072 }
1073 
handle_xyz(enum AVPixelFormat * format)1074 static int handle_xyz(enum AVPixelFormat *format)
1075 {
1076     switch (*format) {
1077     case AV_PIX_FMT_XYZ12BE : *format = AV_PIX_FMT_RGB48BE; return 1;
1078     case AV_PIX_FMT_XYZ12LE : *format = AV_PIX_FMT_RGB48LE; return 1;
1079     default:                                                return 0;
1080     }
1081 }
1082 
handle_formats(SwsContext * c)1083 static void handle_formats(SwsContext *c)
1084 {
1085     c->src0Alpha |= handle_0alpha(&c->srcFormat);
1086     c->dst0Alpha |= handle_0alpha(&c->dstFormat);
1087     c->srcXYZ    |= handle_xyz(&c->srcFormat);
1088     c->dstXYZ    |= handle_xyz(&c->dstFormat);
1089     if (c->srcXYZ || c->dstXYZ)
1090         fill_xyztables(c);
1091 }
1092 
sws_alloc_context(void)1093 SwsContext *sws_alloc_context(void)
1094 {
1095     SwsContext *c = av_mallocz(sizeof(SwsContext));
1096 
1097     av_assert0(offsetof(SwsContext, redDither) + DITHER32_INT == offsetof(SwsContext, dither32));
1098 
1099     if (c) {
1100         c->av_class = &ff_sws_context_class;
1101         av_opt_set_defaults(c);
1102     }
1103 
1104     return c;
1105 }
1106 
alloc_gamma_tbl(double e)1107 static uint16_t * alloc_gamma_tbl(double e)
1108 {
1109     int i = 0;
1110     uint16_t * tbl;
1111     tbl = (uint16_t*)av_malloc(sizeof(uint16_t) * 1 << 16);
1112     if (!tbl)
1113         return NULL;
1114 
1115     for (i = 0; i < 65536; ++i) {
1116         tbl[i] = pow(i / 65535.0, e) * 65535.0;
1117     }
1118     return tbl;
1119 }
1120 
alphaless_fmt(enum AVPixelFormat fmt)1121 static enum AVPixelFormat alphaless_fmt(enum AVPixelFormat fmt)
1122 {
1123     switch(fmt) {
1124     case AV_PIX_FMT_ARGB:       return AV_PIX_FMT_RGB24;
1125     case AV_PIX_FMT_RGBA:       return AV_PIX_FMT_RGB24;
1126     case AV_PIX_FMT_ABGR:       return AV_PIX_FMT_BGR24;
1127     case AV_PIX_FMT_BGRA:       return AV_PIX_FMT_BGR24;
1128     case AV_PIX_FMT_YA8:        return AV_PIX_FMT_GRAY8;
1129 
1130     case AV_PIX_FMT_YUVA420P:   return AV_PIX_FMT_YUV420P;
1131     case AV_PIX_FMT_YUVA422P:   return AV_PIX_FMT_YUV422P;
1132     case AV_PIX_FMT_YUVA444P:           return AV_PIX_FMT_YUV444P;
1133 
1134     case AV_PIX_FMT_GBRAP:              return AV_PIX_FMT_GBRP;
1135 
1136     case AV_PIX_FMT_GBRAP10LE:          return AV_PIX_FMT_GBRP10;
1137     case AV_PIX_FMT_GBRAP10BE:          return AV_PIX_FMT_GBRP10;
1138 
1139     case AV_PIX_FMT_GBRAP12LE:          return AV_PIX_FMT_GBRP12;
1140     case AV_PIX_FMT_GBRAP12BE:          return AV_PIX_FMT_GBRP12;
1141 
1142     case AV_PIX_FMT_GBRAP16LE:          return AV_PIX_FMT_GBRP16;
1143     case AV_PIX_FMT_GBRAP16BE:          return AV_PIX_FMT_GBRP16;
1144 
1145     case AV_PIX_FMT_RGBA64LE:   return AV_PIX_FMT_RGB48;
1146     case AV_PIX_FMT_RGBA64BE:   return AV_PIX_FMT_RGB48;
1147     case AV_PIX_FMT_BGRA64LE:   return AV_PIX_FMT_BGR48;
1148     case AV_PIX_FMT_BGRA64BE:   return AV_PIX_FMT_BGR48;
1149 
1150     case AV_PIX_FMT_YA16BE:             return AV_PIX_FMT_GRAY16;
1151     case AV_PIX_FMT_YA16LE:             return AV_PIX_FMT_GRAY16;
1152 
1153     case AV_PIX_FMT_YUVA420P9BE:        return AV_PIX_FMT_YUV420P9;
1154     case AV_PIX_FMT_YUVA422P9BE:        return AV_PIX_FMT_YUV422P9;
1155     case AV_PIX_FMT_YUVA444P9BE:        return AV_PIX_FMT_YUV444P9;
1156     case AV_PIX_FMT_YUVA420P9LE:        return AV_PIX_FMT_YUV420P9;
1157     case AV_PIX_FMT_YUVA422P9LE:        return AV_PIX_FMT_YUV422P9;
1158     case AV_PIX_FMT_YUVA444P9LE:        return AV_PIX_FMT_YUV444P9;
1159     case AV_PIX_FMT_YUVA420P10BE:       return AV_PIX_FMT_YUV420P10;
1160     case AV_PIX_FMT_YUVA422P10BE:       return AV_PIX_FMT_YUV422P10;
1161     case AV_PIX_FMT_YUVA444P10BE:       return AV_PIX_FMT_YUV444P10;
1162     case AV_PIX_FMT_YUVA420P10LE:       return AV_PIX_FMT_YUV420P10;
1163     case AV_PIX_FMT_YUVA422P10LE:       return AV_PIX_FMT_YUV422P10;
1164     case AV_PIX_FMT_YUVA444P10LE:       return AV_PIX_FMT_YUV444P10;
1165     case AV_PIX_FMT_YUVA420P16BE:       return AV_PIX_FMT_YUV420P16;
1166     case AV_PIX_FMT_YUVA422P16BE:       return AV_PIX_FMT_YUV422P16;
1167     case AV_PIX_FMT_YUVA444P16BE:       return AV_PIX_FMT_YUV444P16;
1168     case AV_PIX_FMT_YUVA420P16LE:       return AV_PIX_FMT_YUV420P16;
1169     case AV_PIX_FMT_YUVA422P16LE:       return AV_PIX_FMT_YUV422P16;
1170     case AV_PIX_FMT_YUVA444P16LE:       return AV_PIX_FMT_YUV444P16;
1171 
1172 //     case AV_PIX_FMT_AYUV64LE:
1173 //     case AV_PIX_FMT_AYUV64BE:
1174 //     case AV_PIX_FMT_PAL8:
1175     default: return AV_PIX_FMT_NONE;
1176     }
1177 }
1178 
sws_init_context(SwsContext * c,SwsFilter * srcFilter,SwsFilter * dstFilter)1179 av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
1180                              SwsFilter *dstFilter)
1181 {
1182     int i;
1183     int usesVFilter, usesHFilter;
1184     int unscaled;
1185     SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
1186     int srcW              = c->srcW;
1187     int srcH              = c->srcH;
1188     int dstW              = c->dstW;
1189     int dstH              = c->dstH;
1190     int dst_stride        = FFALIGN(dstW * sizeof(int16_t) + 66, 16);
1191     int flags, cpu_flags;
1192     enum AVPixelFormat srcFormat = c->srcFormat;
1193     enum AVPixelFormat dstFormat = c->dstFormat;
1194     const AVPixFmtDescriptor *desc_src;
1195     const AVPixFmtDescriptor *desc_dst;
1196     int ret = 0;
1197     enum AVPixelFormat tmpFmt;
1198     static const float float_mult = 1.0f / 255.0f;
1199 
1200     cpu_flags = av_get_cpu_flags();
1201     flags     = c->flags;
1202     emms_c();
1203     if (!rgb15to16)
1204         ff_sws_rgb2rgb_init();
1205 
1206     unscaled = (srcW == dstW && srcH == dstH);
1207 
1208     c->srcRange |= handle_jpeg(&c->srcFormat);
1209     c->dstRange |= handle_jpeg(&c->dstFormat);
1210 
1211     if(srcFormat!=c->srcFormat || dstFormat!=c->dstFormat)
1212         av_log(c, AV_LOG_WARNING, "deprecated pixel format used, make sure you did set range correctly\n");
1213 
1214     if (!c->contrast && !c->saturation && !c->dstFormatBpp)
1215         sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
1216                                  ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
1217                                  c->dstRange, 0, 1 << 16, 1 << 16);
1218 
1219     handle_formats(c);
1220     srcFormat = c->srcFormat;
1221     dstFormat = c->dstFormat;
1222     desc_src = av_pix_fmt_desc_get(srcFormat);
1223     desc_dst = av_pix_fmt_desc_get(dstFormat);
1224 
1225     // If the source has no alpha then disable alpha blendaway
1226     if (c->src0Alpha)
1227         c->alphablend = SWS_ALPHA_BLEND_NONE;
1228 
1229     if (!(unscaled && sws_isSupportedEndiannessConversion(srcFormat) &&
1230           av_pix_fmt_swap_endianness(srcFormat) == dstFormat)) {
1231     if (!sws_isSupportedInput(srcFormat)) {
1232         av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
1233                av_get_pix_fmt_name(srcFormat));
1234         return AVERROR(EINVAL);
1235     }
1236     if (!sws_isSupportedOutput(dstFormat)) {
1237         av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
1238                av_get_pix_fmt_name(dstFormat));
1239         return AVERROR(EINVAL);
1240     }
1241     }
1242     av_assert2(desc_src && desc_dst);
1243 
1244     i = flags & (SWS_POINT         |
1245                  SWS_AREA          |
1246                  SWS_BILINEAR      |
1247                  SWS_FAST_BILINEAR |
1248                  SWS_BICUBIC       |
1249                  SWS_X             |
1250                  SWS_GAUSS         |
1251                  SWS_LANCZOS       |
1252                  SWS_SINC          |
1253                  SWS_SPLINE        |
1254                  SWS_BICUBLIN);
1255 
1256     /* provide a default scaler if not set by caller */
1257     if (!i) {
1258         if (dstW < srcW && dstH < srcH)
1259             flags |= SWS_BICUBIC;
1260         else if (dstW > srcW && dstH > srcH)
1261             flags |= SWS_BICUBIC;
1262         else
1263             flags |= SWS_BICUBIC;
1264         c->flags = flags;
1265     } else if (i & (i - 1)) {
1266         av_log(c, AV_LOG_ERROR,
1267                "Exactly one scaler algorithm must be chosen, got %X\n", i);
1268         return AVERROR(EINVAL);
1269     }
1270     /* sanity check */
1271     if (srcW < 1 || srcH < 1 || dstW < 1 || dstH < 1) {
1272         /* FIXME check if these are enough and try to lower them after
1273          * fixing the relevant parts of the code */
1274         av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
1275                srcW, srcH, dstW, dstH);
1276         return AVERROR(EINVAL);
1277     }
1278     if (flags & SWS_FAST_BILINEAR) {
1279         if (srcW < 8 || dstW < 8) {
1280             flags ^= SWS_FAST_BILINEAR | SWS_BILINEAR;
1281             c->flags = flags;
1282         }
1283     }
1284 
1285     if (!dstFilter)
1286         dstFilter = &dummyFilter;
1287     if (!srcFilter)
1288         srcFilter = &dummyFilter;
1289 
1290     c->lumXInc      = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
1291     c->lumYInc      = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
1292     c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
1293     c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
1294     c->vRounder     = 4 * 0x0001000100010001ULL;
1295 
1296     usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
1297                   (srcFilter->chrV && srcFilter->chrV->length > 1) ||
1298                   (dstFilter->lumV && dstFilter->lumV->length > 1) ||
1299                   (dstFilter->chrV && dstFilter->chrV->length > 1);
1300     usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
1301                   (srcFilter->chrH && srcFilter->chrH->length > 1) ||
1302                   (dstFilter->lumH && dstFilter->lumH->length > 1) ||
1303                   (dstFilter->chrH && dstFilter->chrH->length > 1);
1304 
1305     av_pix_fmt_get_chroma_sub_sample(srcFormat, &c->chrSrcHSubSample, &c->chrSrcVSubSample);
1306     av_pix_fmt_get_chroma_sub_sample(dstFormat, &c->chrDstHSubSample, &c->chrDstVSubSample);
1307 
1308     if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) {
1309         if (dstW&1) {
1310             av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to odd output size\n");
1311             flags |= SWS_FULL_CHR_H_INT;
1312             c->flags = flags;
1313         }
1314 
1315         if (   c->chrSrcHSubSample == 0
1316             && c->chrSrcVSubSample == 0
1317             && c->dither != SWS_DITHER_BAYER //SWS_FULL_CHR_H_INT is currently not supported with SWS_DITHER_BAYER
1318             && !(c->flags & SWS_FAST_BILINEAR)
1319         ) {
1320             av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to input having non subsampled chroma\n");
1321             flags |= SWS_FULL_CHR_H_INT;
1322             c->flags = flags;
1323         }
1324     }
1325 
1326     if (c->dither == SWS_DITHER_AUTO) {
1327         if (flags & SWS_ERROR_DIFFUSION)
1328             c->dither = SWS_DITHER_ED;
1329     }
1330 
1331     if(dstFormat == AV_PIX_FMT_BGR4_BYTE ||
1332        dstFormat == AV_PIX_FMT_RGB4_BYTE ||
1333        dstFormat == AV_PIX_FMT_BGR8 ||
1334        dstFormat == AV_PIX_FMT_RGB8) {
1335         if (c->dither == SWS_DITHER_AUTO)
1336             c->dither = (flags & SWS_FULL_CHR_H_INT) ? SWS_DITHER_ED : SWS_DITHER_BAYER;
1337         if (!(flags & SWS_FULL_CHR_H_INT)) {
1338             if (c->dither == SWS_DITHER_ED || c->dither == SWS_DITHER_A_DITHER || c->dither == SWS_DITHER_X_DITHER) {
1339                 av_log(c, AV_LOG_DEBUG,
1340                     "Desired dithering only supported in full chroma interpolation for destination format '%s'\n",
1341                     av_get_pix_fmt_name(dstFormat));
1342                 flags   |= SWS_FULL_CHR_H_INT;
1343                 c->flags = flags;
1344             }
1345         }
1346         if (flags & SWS_FULL_CHR_H_INT) {
1347             if (c->dither == SWS_DITHER_BAYER) {
1348                 av_log(c, AV_LOG_DEBUG,
1349                     "Ordered dither is not supported in full chroma interpolation for destination format '%s'\n",
1350                     av_get_pix_fmt_name(dstFormat));
1351                 c->dither = SWS_DITHER_ED;
1352             }
1353         }
1354     }
1355     if (isPlanarRGB(dstFormat)) {
1356         if (!(flags & SWS_FULL_CHR_H_INT)) {
1357             av_log(c, AV_LOG_DEBUG,
1358                    "%s output is not supported with half chroma resolution, switching to full\n",
1359                    av_get_pix_fmt_name(dstFormat));
1360             flags   |= SWS_FULL_CHR_H_INT;
1361             c->flags = flags;
1362         }
1363     }
1364 
1365     /* reuse chroma for 2 pixels RGB/BGR unless user wants full
1366      * chroma interpolation */
1367     if (flags & SWS_FULL_CHR_H_INT &&
1368         isAnyRGB(dstFormat)        &&
1369         !isPlanarRGB(dstFormat)    &&
1370         dstFormat != AV_PIX_FMT_RGBA64LE &&
1371         dstFormat != AV_PIX_FMT_RGBA64BE &&
1372         dstFormat != AV_PIX_FMT_BGRA64LE &&
1373         dstFormat != AV_PIX_FMT_BGRA64BE &&
1374         dstFormat != AV_PIX_FMT_RGB48LE &&
1375         dstFormat != AV_PIX_FMT_RGB48BE &&
1376         dstFormat != AV_PIX_FMT_BGR48LE &&
1377         dstFormat != AV_PIX_FMT_BGR48BE &&
1378         dstFormat != AV_PIX_FMT_RGBA  &&
1379         dstFormat != AV_PIX_FMT_ARGB  &&
1380         dstFormat != AV_PIX_FMT_BGRA  &&
1381         dstFormat != AV_PIX_FMT_ABGR  &&
1382         dstFormat != AV_PIX_FMT_RGB24 &&
1383         dstFormat != AV_PIX_FMT_BGR24 &&
1384         dstFormat != AV_PIX_FMT_BGR4_BYTE &&
1385         dstFormat != AV_PIX_FMT_RGB4_BYTE &&
1386         dstFormat != AV_PIX_FMT_BGR8 &&
1387         dstFormat != AV_PIX_FMT_RGB8
1388     ) {
1389         av_log(c, AV_LOG_WARNING,
1390                "full chroma interpolation for destination format '%s' not yet implemented\n",
1391                av_get_pix_fmt_name(dstFormat));
1392         flags   &= ~SWS_FULL_CHR_H_INT;
1393         c->flags = flags;
1394     }
1395     if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
1396         c->chrDstHSubSample = 1;
1397 
1398     // drop some chroma lines if the user wants it
1399     c->vChrDrop          = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
1400                            SWS_SRC_V_CHR_DROP_SHIFT;
1401     c->chrSrcVSubSample += c->vChrDrop;
1402 
1403     /* drop every other pixel for chroma calculation unless user
1404      * wants full chroma */
1405     if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP)   &&
1406         srcFormat != AV_PIX_FMT_RGB8 && srcFormat != AV_PIX_FMT_BGR8 &&
1407         srcFormat != AV_PIX_FMT_RGB4 && srcFormat != AV_PIX_FMT_BGR4 &&
1408         srcFormat != AV_PIX_FMT_RGB4_BYTE && srcFormat != AV_PIX_FMT_BGR4_BYTE &&
1409         srcFormat != AV_PIX_FMT_GBRP9BE   && srcFormat != AV_PIX_FMT_GBRP9LE  &&
1410         srcFormat != AV_PIX_FMT_GBRP10BE  && srcFormat != AV_PIX_FMT_GBRP10LE &&
1411         srcFormat != AV_PIX_FMT_GBRAP10BE && srcFormat != AV_PIX_FMT_GBRAP10LE &&
1412         srcFormat != AV_PIX_FMT_GBRP12BE  && srcFormat != AV_PIX_FMT_GBRP12LE &&
1413         srcFormat != AV_PIX_FMT_GBRAP12BE && srcFormat != AV_PIX_FMT_GBRAP12LE &&
1414         srcFormat != AV_PIX_FMT_GBRP14BE  && srcFormat != AV_PIX_FMT_GBRP14LE &&
1415         srcFormat != AV_PIX_FMT_GBRP16BE  && srcFormat != AV_PIX_FMT_GBRP16LE &&
1416         srcFormat != AV_PIX_FMT_GBRAP16BE  && srcFormat != AV_PIX_FMT_GBRAP16LE &&
1417         srcFormat != AV_PIX_FMT_GBRPF32BE  && srcFormat != AV_PIX_FMT_GBRPF32LE &&
1418         srcFormat != AV_PIX_FMT_GBRAPF32BE && srcFormat != AV_PIX_FMT_GBRAPF32LE &&
1419         ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
1420          (flags & SWS_FAST_BILINEAR)))
1421         c->chrSrcHSubSample = 1;
1422 
1423     // Note the AV_CEIL_RSHIFT is so that we always round toward +inf.
1424     c->chrSrcW = AV_CEIL_RSHIFT(srcW, c->chrSrcHSubSample);
1425     c->chrSrcH = AV_CEIL_RSHIFT(srcH, c->chrSrcVSubSample);
1426     c->chrDstW = AV_CEIL_RSHIFT(dstW, c->chrDstHSubSample);
1427     c->chrDstH = AV_CEIL_RSHIFT(dstH, c->chrDstVSubSample);
1428 
1429     if (!FF_ALLOCZ_TYPED_ARRAY(c->formatConvBuffer, FFALIGN(srcW * 2 + 78, 16) * 2))
1430         goto nomem;
1431 
1432     c->srcBpc = desc_src->comp[0].depth;
1433     if (c->srcBpc < 8)
1434         c->srcBpc = 8;
1435     c->dstBpc = desc_dst->comp[0].depth;
1436     if (c->dstBpc < 8)
1437         c->dstBpc = 8;
1438     if (isAnyRGB(srcFormat) || srcFormat == AV_PIX_FMT_PAL8)
1439         c->srcBpc = 16;
1440     if (c->dstBpc == 16)
1441         dst_stride <<= 1;
1442 
1443     if (INLINE_MMXEXT(cpu_flags) && c->srcBpc == 8 && c->dstBpc <= 14) {
1444         c->canMMXEXTBeUsed = dstW >= srcW && (dstW & 31) == 0 &&
1445                              c->chrDstW >= c->chrSrcW &&
1446                              (srcW & 15) == 0;
1447         if (!c->canMMXEXTBeUsed && dstW >= srcW && c->chrDstW >= c->chrSrcW && (srcW & 15) == 0
1448 
1449             && (flags & SWS_FAST_BILINEAR)) {
1450             if (flags & SWS_PRINT_INFO)
1451                 av_log(c, AV_LOG_INFO,
1452                        "output width is not a multiple of 32 -> no MMXEXT scaler\n");
1453         }
1454         if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat) || isAnyRGB(c->srcFormat))
1455             c->canMMXEXTBeUsed = 0;
1456     } else
1457         c->canMMXEXTBeUsed = 0;
1458 
1459     c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
1460     c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
1461 
1462     /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
1463      * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
1464      * correct scaling.
1465      * n-2 is the last chrominance sample available.
1466      * This is not perfect, but no one should notice the difference, the more
1467      * correct variant would be like the vertical one, but that would require
1468      * some special code for the first and last pixel */
1469     if (flags & SWS_FAST_BILINEAR) {
1470         if (c->canMMXEXTBeUsed) {
1471             c->lumXInc += 20;
1472             c->chrXInc += 20;
1473         }
1474         // we don't use the x86 asm scaler if MMX is available
1475         else if (INLINE_MMX(cpu_flags) && c->dstBpc <= 14) {
1476             c->lumXInc = ((int64_t)(srcW       - 2) << 16) / (dstW       - 2) - 20;
1477             c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
1478         }
1479     }
1480 
1481     // hardcoded for now
1482     c->gamma_value = 2.2;
1483     tmpFmt = AV_PIX_FMT_RGBA64LE;
1484 
1485 
1486     if (!unscaled && c->gamma_flag && (srcFormat != tmpFmt || dstFormat != tmpFmt)) {
1487         SwsContext *c2;
1488         c->cascaded_context[0] = NULL;
1489 
1490         ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
1491                             srcW, srcH, tmpFmt, 64);
1492         if (ret < 0)
1493             return ret;
1494 
1495         c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
1496                                                 srcW, srcH, tmpFmt,
1497                                                 flags, NULL, NULL, c->param);
1498         if (!c->cascaded_context[0]) {
1499             return AVERROR(ENOMEM);
1500         }
1501 
1502         c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFmt,
1503                                                 dstW, dstH, tmpFmt,
1504                                                 flags, srcFilter, dstFilter, c->param);
1505 
1506         if (!c->cascaded_context[1])
1507             return AVERROR(ENOMEM);
1508 
1509         c2 = c->cascaded_context[1];
1510         c2->is_internal_gamma = 1;
1511         c2->gamma     = alloc_gamma_tbl(    c->gamma_value);
1512         c2->inv_gamma = alloc_gamma_tbl(1.f/c->gamma_value);
1513         if (!c2->gamma || !c2->inv_gamma)
1514             return AVERROR(ENOMEM);
1515 
1516         // is_internal_flag is set after creating the context
1517         // to properly create the gamma convert FilterDescriptor
1518         // we have to re-initialize it
1519         ff_free_filters(c2);
1520         if ((ret = ff_init_filters(c2)) < 0) {
1521             sws_freeContext(c2);
1522             c->cascaded_context[1] = NULL;
1523             return ret;
1524         }
1525 
1526         c->cascaded_context[2] = NULL;
1527         if (dstFormat != tmpFmt) {
1528             ret = av_image_alloc(c->cascaded1_tmp, c->cascaded1_tmpStride,
1529                                 dstW, dstH, tmpFmt, 64);
1530             if (ret < 0)
1531                 return ret;
1532 
1533             c->cascaded_context[2] = sws_getContext(dstW, dstH, tmpFmt,
1534                                                 dstW, dstH, dstFormat,
1535                                                 flags, NULL, NULL, c->param);
1536             if (!c->cascaded_context[2])
1537                 return AVERROR(ENOMEM);
1538         }
1539         return 0;
1540     }
1541 
1542     if (isBayer(srcFormat)) {
1543         if (!unscaled ||
1544             (dstFormat != AV_PIX_FMT_RGB24 && dstFormat != AV_PIX_FMT_YUV420P &&
1545              dstFormat != AV_PIX_FMT_RGB48)) {
1546             enum AVPixelFormat tmpFormat = isBayer16BPS(srcFormat) ? AV_PIX_FMT_RGB48 : AV_PIX_FMT_RGB24;
1547 
1548             ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
1549                                 srcW, srcH, tmpFormat, 64);
1550             if (ret < 0)
1551                 return ret;
1552 
1553             c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
1554                                                     srcW, srcH, tmpFormat,
1555                                                     flags, srcFilter, NULL, c->param);
1556             if (!c->cascaded_context[0])
1557                 return AVERROR(ENOMEM);
1558 
1559             c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFormat,
1560                                                     dstW, dstH, dstFormat,
1561                                                     flags, NULL, dstFilter, c->param);
1562             if (!c->cascaded_context[1])
1563                 return AVERROR(ENOMEM);
1564             return 0;
1565         }
1566     }
1567 
1568     if (unscaled && c->srcBpc == 8 && dstFormat == AV_PIX_FMT_GRAYF32){
1569         for (i = 0; i < 256; ++i){
1570             c->uint2float_lut[i] = (float)i * float_mult;
1571         }
1572     }
1573 
1574     // float will be converted to uint16_t
1575     if ((srcFormat == AV_PIX_FMT_GRAYF32BE || srcFormat == AV_PIX_FMT_GRAYF32LE) &&
1576         (!unscaled || unscaled && dstFormat != srcFormat && (srcFormat != AV_PIX_FMT_GRAYF32 ||
1577         dstFormat != AV_PIX_FMT_GRAY8))){
1578         c->srcBpc = 16;
1579     }
1580 
1581     if (CONFIG_SWSCALE_ALPHA && isALPHA(srcFormat) && !isALPHA(dstFormat)) {
1582         enum AVPixelFormat tmpFormat = alphaless_fmt(srcFormat);
1583 
1584         if (tmpFormat != AV_PIX_FMT_NONE && c->alphablend != SWS_ALPHA_BLEND_NONE) {
1585             if (!unscaled ||
1586                 dstFormat != tmpFormat ||
1587                 usesHFilter || usesVFilter ||
1588                 c->srcRange != c->dstRange
1589             ) {
1590                 c->cascaded_mainindex = 1;
1591                 ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
1592                                      srcW, srcH, tmpFormat, 64);
1593                 if (ret < 0)
1594                     return ret;
1595 
1596                 c->cascaded_context[0] = sws_alloc_set_opts(srcW, srcH, srcFormat,
1597                                                             srcW, srcH, tmpFormat,
1598                                                             flags, c->param);
1599                 if (!c->cascaded_context[0])
1600                     return AVERROR(EINVAL);
1601                 c->cascaded_context[0]->alphablend = c->alphablend;
1602                 ret = sws_init_context(c->cascaded_context[0], NULL , NULL);
1603                 if (ret < 0)
1604                     return ret;
1605 
1606                 c->cascaded_context[1] = sws_alloc_set_opts(srcW, srcH, tmpFormat,
1607                                                             dstW, dstH, dstFormat,
1608                                                             flags, c->param);
1609                 if (!c->cascaded_context[1])
1610                     return AVERROR(EINVAL);
1611 
1612                 c->cascaded_context[1]->srcRange = c->srcRange;
1613                 c->cascaded_context[1]->dstRange = c->dstRange;
1614                 ret = sws_init_context(c->cascaded_context[1], srcFilter , dstFilter);
1615                 if (ret < 0)
1616                     return ret;
1617 
1618                 return 0;
1619             }
1620         }
1621     }
1622 
1623 #if HAVE_MMAP && HAVE_MPROTECT && defined(MAP_ANONYMOUS)
1624 #define USE_MMAP 1
1625 #else
1626 #define USE_MMAP 0
1627 #endif
1628 
1629     /* precalculate horizontal scaler filter coefficients */
1630     {
1631 #if HAVE_MMXEXT_INLINE
1632 // can't downscale !!!
1633         if (c->canMMXEXTBeUsed && (flags & SWS_FAST_BILINEAR)) {
1634             c->lumMmxextFilterCodeSize = ff_init_hscaler_mmxext(dstW, c->lumXInc, NULL,
1635                                                              NULL, NULL, 8);
1636             c->chrMmxextFilterCodeSize = ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc,
1637                                                              NULL, NULL, NULL, 4);
1638 
1639 #if USE_MMAP
1640             c->lumMmxextFilterCode = mmap(NULL, c->lumMmxextFilterCodeSize,
1641                                           PROT_READ | PROT_WRITE,
1642                                           MAP_PRIVATE | MAP_ANONYMOUS,
1643                                           -1, 0);
1644             c->chrMmxextFilterCode = mmap(NULL, c->chrMmxextFilterCodeSize,
1645                                           PROT_READ | PROT_WRITE,
1646                                           MAP_PRIVATE | MAP_ANONYMOUS,
1647                                           -1, 0);
1648 #elif HAVE_VIRTUALALLOC
1649             c->lumMmxextFilterCode = VirtualAlloc(NULL,
1650                                                   c->lumMmxextFilterCodeSize,
1651                                                   MEM_COMMIT,
1652                                                   PAGE_EXECUTE_READWRITE);
1653             c->chrMmxextFilterCode = VirtualAlloc(NULL,
1654                                                   c->chrMmxextFilterCodeSize,
1655                                                   MEM_COMMIT,
1656                                                   PAGE_EXECUTE_READWRITE);
1657 #else
1658             c->lumMmxextFilterCode = av_malloc(c->lumMmxextFilterCodeSize);
1659             c->chrMmxextFilterCode = av_malloc(c->chrMmxextFilterCodeSize);
1660 #endif
1661 
1662 #ifdef MAP_ANONYMOUS
1663             if (c->lumMmxextFilterCode == MAP_FAILED || c->chrMmxextFilterCode == MAP_FAILED)
1664 #else
1665             if (!c->lumMmxextFilterCode || !c->chrMmxextFilterCode)
1666 #endif
1667             {
1668                 av_log(c, AV_LOG_ERROR, "Failed to allocate MMX2FilterCode\n");
1669                 return AVERROR(ENOMEM);
1670             }
1671 
1672             if (!FF_ALLOCZ_TYPED_ARRAY(c->hLumFilter,    dstW           / 8 + 8) ||
1673                 !FF_ALLOCZ_TYPED_ARRAY(c->hChrFilter,    c->chrDstW     / 4 + 8) ||
1674                 !FF_ALLOCZ_TYPED_ARRAY(c->hLumFilterPos, dstW       / 2 / 8 + 8) ||
1675                 !FF_ALLOCZ_TYPED_ARRAY(c->hChrFilterPos, c->chrDstW / 2 / 4 + 8))
1676                 goto nomem;
1677 
1678             ff_init_hscaler_mmxext(      dstW, c->lumXInc, c->lumMmxextFilterCode,
1679                                 c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
1680             ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc, c->chrMmxextFilterCode,
1681                                 c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4);
1682 
1683 #if USE_MMAP
1684             if (   mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1
1685                 || mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1) {
1686                 av_log(c, AV_LOG_ERROR, "mprotect failed, cannot use fast bilinear scaler\n");
1687                 ret = AVERROR(EINVAL);
1688                 goto fail;
1689             }
1690 #endif
1691         } else
1692 #endif /* HAVE_MMXEXT_INLINE */
1693         {
1694             const int filterAlign = X86_MMX(cpu_flags)     ? 4 :
1695                                     PPC_ALTIVEC(cpu_flags) ? 8 :
1696                                     have_neon(cpu_flags)   ? 8 : 1;
1697 
1698             if ((ret = initFilter(&c->hLumFilter, &c->hLumFilterPos,
1699                            &c->hLumFilterSize, c->lumXInc,
1700                            srcW, dstW, filterAlign, 1 << 14,
1701                            (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1702                            cpu_flags, srcFilter->lumH, dstFilter->lumH,
1703                            c->param,
1704                            get_local_pos(c, 0, 0, 0),
1705                            get_local_pos(c, 0, 0, 0))) < 0)
1706                 goto fail;
1707             if ((ret = initFilter(&c->hChrFilter, &c->hChrFilterPos,
1708                            &c->hChrFilterSize, c->chrXInc,
1709                            c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
1710                            (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1711                            cpu_flags, srcFilter->chrH, dstFilter->chrH,
1712                            c->param,
1713                            get_local_pos(c, c->chrSrcHSubSample, c->src_h_chr_pos, 0),
1714                            get_local_pos(c, c->chrDstHSubSample, c->dst_h_chr_pos, 0))) < 0)
1715                 goto fail;
1716         }
1717     } // initialize horizontal stuff
1718 
1719     /* precalculate vertical scaler filter coefficients */
1720     {
1721         const int filterAlign = X86_MMX(cpu_flags)     ? 2 :
1722                                 PPC_ALTIVEC(cpu_flags) ? 8 :
1723                                 have_neon(cpu_flags)   ? 2 : 1;
1724 
1725         if ((ret = initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize,
1726                        c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
1727                        (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1728                        cpu_flags, srcFilter->lumV, dstFilter->lumV,
1729                        c->param,
1730                        get_local_pos(c, 0, 0, 1),
1731                        get_local_pos(c, 0, 0, 1))) < 0)
1732             goto fail;
1733         if ((ret = initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize,
1734                        c->chrYInc, c->chrSrcH, c->chrDstH,
1735                        filterAlign, (1 << 12),
1736                        (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1737                        cpu_flags, srcFilter->chrV, dstFilter->chrV,
1738                        c->param,
1739                        get_local_pos(c, c->chrSrcVSubSample, c->src_v_chr_pos, 1),
1740                        get_local_pos(c, c->chrDstVSubSample, c->dst_v_chr_pos, 1))) < 0)
1741 
1742             goto fail;
1743 
1744 #if HAVE_ALTIVEC
1745         if (!FF_ALLOC_TYPED_ARRAY(c->vYCoeffsBank, c->vLumFilterSize * c->dstH) ||
1746             !FF_ALLOC_TYPED_ARRAY(c->vCCoeffsBank, c->vChrFilterSize * c->chrDstH))
1747             goto nomem;
1748 
1749         for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
1750             int j;
1751             short *p = (short *)&c->vYCoeffsBank[i];
1752             for (j = 0; j < 8; j++)
1753                 p[j] = c->vLumFilter[i];
1754         }
1755 
1756         for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
1757             int j;
1758             short *p = (short *)&c->vCCoeffsBank[i];
1759             for (j = 0; j < 8; j++)
1760                 p[j] = c->vChrFilter[i];
1761         }
1762 #endif
1763     }
1764 
1765     for (i = 0; i < 4; i++)
1766         if (!FF_ALLOCZ_TYPED_ARRAY(c->dither_error[i], c->dstW + 2))
1767             goto nomem;
1768 
1769     c->needAlpha = (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) ? 1 : 0;
1770 
1771     // 64 / c->scalingBpp is the same as 16 / sizeof(scaling_intermediate)
1772     c->uv_off   = (dst_stride>>1) + 64 / (c->dstBpc &~ 7);
1773     c->uv_offx2 = dst_stride + 16;
1774 
1775     av_assert0(c->chrDstH <= dstH);
1776 
1777     if (flags & SWS_PRINT_INFO) {
1778         const char *scaler = NULL, *cpucaps;
1779 
1780         for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
1781             if (flags & scale_algorithms[i].flag) {
1782                 scaler = scale_algorithms[i].description;
1783                 break;
1784             }
1785         }
1786         if (!scaler)
1787             scaler =  "ehh flags invalid?!";
1788         av_log(c, AV_LOG_INFO, "%s scaler, from %s to %s%s ",
1789                scaler,
1790                av_get_pix_fmt_name(srcFormat),
1791 #ifdef DITHER1XBPP
1792                dstFormat == AV_PIX_FMT_BGR555   || dstFormat == AV_PIX_FMT_BGR565   ||
1793                dstFormat == AV_PIX_FMT_RGB444BE || dstFormat == AV_PIX_FMT_RGB444LE ||
1794                dstFormat == AV_PIX_FMT_BGR444BE || dstFormat == AV_PIX_FMT_BGR444LE ?
1795                                                              "dithered " : "",
1796 #else
1797                "",
1798 #endif
1799                av_get_pix_fmt_name(dstFormat));
1800 
1801         if (INLINE_MMXEXT(cpu_flags))
1802             cpucaps = "MMXEXT";
1803         else if (INLINE_AMD3DNOW(cpu_flags))
1804             cpucaps = "3DNOW";
1805         else if (INLINE_MMX(cpu_flags))
1806             cpucaps = "MMX";
1807         else if (PPC_ALTIVEC(cpu_flags))
1808             cpucaps = "AltiVec";
1809         else
1810             cpucaps = "C";
1811 
1812         av_log(c, AV_LOG_INFO, "using %s\n", cpucaps);
1813 
1814         av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1815         av_log(c, AV_LOG_DEBUG,
1816                "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1817                c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
1818         av_log(c, AV_LOG_DEBUG,
1819                "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1820                c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
1821                c->chrXInc, c->chrYInc);
1822     }
1823 
1824     /* alpha blend special case, note this has been split via cascaded contexts if its scaled */
1825     if (unscaled && !usesHFilter && !usesVFilter &&
1826         c->alphablend != SWS_ALPHA_BLEND_NONE &&
1827         isALPHA(srcFormat) &&
1828         (c->srcRange == c->dstRange || isAnyRGB(dstFormat)) &&
1829         alphaless_fmt(srcFormat) == dstFormat
1830     ) {
1831         c->swscale = ff_sws_alphablendaway;
1832 
1833         if (flags & SWS_PRINT_INFO)
1834             av_log(c, AV_LOG_INFO,
1835                     "using alpha blendaway %s -> %s special converter\n",
1836                     av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1837         return 0;
1838     }
1839 
1840     /* unscaled special cases */
1841     if (unscaled && !usesHFilter && !usesVFilter &&
1842         (c->srcRange == c->dstRange || isAnyRGB(dstFormat) ||
1843          isFloat(srcFormat) || isFloat(dstFormat))){
1844         ff_get_unscaled_swscale(c);
1845 
1846         if (c->swscale) {
1847             if (flags & SWS_PRINT_INFO)
1848                 av_log(c, AV_LOG_INFO,
1849                        "using unscaled %s -> %s special converter\n",
1850                        av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1851             return 0;
1852         }
1853     }
1854 
1855     c->swscale = ff_getSwsFunc(c);
1856     return ff_init_filters(c);
1857 nomem:
1858     ret = AVERROR(ENOMEM);
1859 fail: // FIXME replace things by appropriate error codes
1860     if (ret == RETCODE_USE_CASCADE)  {
1861         int tmpW = sqrt(srcW * (int64_t)dstW);
1862         int tmpH = sqrt(srcH * (int64_t)dstH);
1863         enum AVPixelFormat tmpFormat = AV_PIX_FMT_YUV420P;
1864 
1865         if (isALPHA(srcFormat))
1866             tmpFormat = AV_PIX_FMT_YUVA420P;
1867 
1868         if (srcW*(int64_t)srcH <= 4LL*dstW*dstH)
1869             return AVERROR(EINVAL);
1870 
1871         ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
1872                              tmpW, tmpH, tmpFormat, 64);
1873         if (ret < 0)
1874             return ret;
1875 
1876         c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
1877                                                 tmpW, tmpH, tmpFormat,
1878                                                 flags, srcFilter, NULL, c->param);
1879         if (!c->cascaded_context[0])
1880             return AVERROR(ENOMEM);
1881 
1882         c->cascaded_context[1] = sws_getContext(tmpW, tmpH, tmpFormat,
1883                                                 dstW, dstH, dstFormat,
1884                                                 flags, NULL, dstFilter, c->param);
1885         if (!c->cascaded_context[1])
1886             return AVERROR(ENOMEM);
1887         return 0;
1888     }
1889     return ret;
1890 }
1891 
sws_alloc_set_opts(int srcW,int srcH,enum AVPixelFormat srcFormat,int dstW,int dstH,enum AVPixelFormat dstFormat,int flags,const double * param)1892 SwsContext *sws_alloc_set_opts(int srcW, int srcH, enum AVPixelFormat srcFormat,
1893                                int dstW, int dstH, enum AVPixelFormat dstFormat,
1894                                int flags, const double *param)
1895 {
1896     SwsContext *c;
1897 
1898     if (!(c = sws_alloc_context()))
1899         return NULL;
1900 
1901     c->flags     = flags;
1902     c->srcW      = srcW;
1903     c->srcH      = srcH;
1904     c->dstW      = dstW;
1905     c->dstH      = dstH;
1906     c->srcFormat = srcFormat;
1907     c->dstFormat = dstFormat;
1908 
1909     if (param) {
1910         c->param[0] = param[0];
1911         c->param[1] = param[1];
1912     }
1913 
1914     return c;
1915 }
1916 
sws_getContext(int srcW,int srcH,enum AVPixelFormat srcFormat,int dstW,int dstH,enum AVPixelFormat dstFormat,int flags,SwsFilter * srcFilter,SwsFilter * dstFilter,const double * param)1917 SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
1918                            int dstW, int dstH, enum AVPixelFormat dstFormat,
1919                            int flags, SwsFilter *srcFilter,
1920                            SwsFilter *dstFilter, const double *param)
1921 {
1922     SwsContext *c;
1923 
1924     c = sws_alloc_set_opts(srcW, srcH, srcFormat,
1925                            dstW, dstH, dstFormat,
1926                            flags, param);
1927     if (!c)
1928         return NULL;
1929 
1930     if (sws_init_context(c, srcFilter, dstFilter) < 0) {
1931         sws_freeContext(c);
1932         return NULL;
1933     }
1934 
1935     return c;
1936 }
1937 
isnan_vec(SwsVector * a)1938 static int isnan_vec(SwsVector *a)
1939 {
1940     int i;
1941     for (i=0; i<a->length; i++)
1942         if (isnan(a->coeff[i]))
1943             return 1;
1944     return 0;
1945 }
1946 
makenan_vec(SwsVector * a)1947 static void makenan_vec(SwsVector *a)
1948 {
1949     int i;
1950     for (i=0; i<a->length; i++)
1951         a->coeff[i] = NAN;
1952 }
1953 
sws_getDefaultFilter(float lumaGBlur,float chromaGBlur,float lumaSharpen,float chromaSharpen,float chromaHShift,float chromaVShift,int verbose)1954 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
1955                                 float lumaSharpen, float chromaSharpen,
1956                                 float chromaHShift, float chromaVShift,
1957                                 int verbose)
1958 {
1959     SwsFilter *filter = av_malloc(sizeof(SwsFilter));
1960     if (!filter)
1961         return NULL;
1962 
1963     if (lumaGBlur != 0.0) {
1964         filter->lumH = sws_getGaussianVec(lumaGBlur, 3.0);
1965         filter->lumV = sws_getGaussianVec(lumaGBlur, 3.0);
1966     } else {
1967         filter->lumH = sws_getIdentityVec();
1968         filter->lumV = sws_getIdentityVec();
1969     }
1970 
1971     if (chromaGBlur != 0.0) {
1972         filter->chrH = sws_getGaussianVec(chromaGBlur, 3.0);
1973         filter->chrV = sws_getGaussianVec(chromaGBlur, 3.0);
1974     } else {
1975         filter->chrH = sws_getIdentityVec();
1976         filter->chrV = sws_getIdentityVec();
1977     }
1978 
1979     if (!filter->lumH || !filter->lumV || !filter->chrH || !filter->chrV)
1980         goto fail;
1981 
1982     if (chromaSharpen != 0.0) {
1983         SwsVector *id = sws_getIdentityVec();
1984         if (!id)
1985             goto fail;
1986         sws_scaleVec(filter->chrH, -chromaSharpen);
1987         sws_scaleVec(filter->chrV, -chromaSharpen);
1988         sws_addVec(filter->chrH, id);
1989         sws_addVec(filter->chrV, id);
1990         sws_freeVec(id);
1991     }
1992 
1993     if (lumaSharpen != 0.0) {
1994         SwsVector *id = sws_getIdentityVec();
1995         if (!id)
1996             goto fail;
1997         sws_scaleVec(filter->lumH, -lumaSharpen);
1998         sws_scaleVec(filter->lumV, -lumaSharpen);
1999         sws_addVec(filter->lumH, id);
2000         sws_addVec(filter->lumV, id);
2001         sws_freeVec(id);
2002     }
2003 
2004     if (chromaHShift != 0.0)
2005         sws_shiftVec(filter->chrH, (int)(chromaHShift + 0.5));
2006 
2007     if (chromaVShift != 0.0)
2008         sws_shiftVec(filter->chrV, (int)(chromaVShift + 0.5));
2009 
2010     sws_normalizeVec(filter->chrH, 1.0);
2011     sws_normalizeVec(filter->chrV, 1.0);
2012     sws_normalizeVec(filter->lumH, 1.0);
2013     sws_normalizeVec(filter->lumV, 1.0);
2014 
2015     if (isnan_vec(filter->chrH) ||
2016         isnan_vec(filter->chrV) ||
2017         isnan_vec(filter->lumH) ||
2018         isnan_vec(filter->lumV))
2019         goto fail;
2020 
2021     if (verbose)
2022         sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
2023     if (verbose)
2024         sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
2025 
2026     return filter;
2027 
2028 fail:
2029     sws_freeVec(filter->lumH);
2030     sws_freeVec(filter->lumV);
2031     sws_freeVec(filter->chrH);
2032     sws_freeVec(filter->chrV);
2033     av_freep(&filter);
2034     return NULL;
2035 }
2036 
sws_allocVec(int length)2037 SwsVector *sws_allocVec(int length)
2038 {
2039     SwsVector *vec;
2040 
2041     if(length <= 0 || length > INT_MAX/ sizeof(double))
2042         return NULL;
2043 
2044     vec = av_malloc(sizeof(SwsVector));
2045     if (!vec)
2046         return NULL;
2047     vec->length = length;
2048     vec->coeff  = av_malloc(sizeof(double) * length);
2049     if (!vec->coeff)
2050         av_freep(&vec);
2051     return vec;
2052 }
2053 
sws_getGaussianVec(double variance,double quality)2054 SwsVector *sws_getGaussianVec(double variance, double quality)
2055 {
2056     const int length = (int)(variance * quality + 0.5) | 1;
2057     int i;
2058     double middle  = (length - 1) * 0.5;
2059     SwsVector *vec;
2060 
2061     if(variance < 0 || quality < 0)
2062         return NULL;
2063 
2064     vec = sws_allocVec(length);
2065 
2066     if (!vec)
2067         return NULL;
2068 
2069     for (i = 0; i < length; i++) {
2070         double dist = i - middle;
2071         vec->coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
2072                         sqrt(2 * variance * M_PI);
2073     }
2074 
2075     sws_normalizeVec(vec, 1.0);
2076 
2077     return vec;
2078 }
2079 
2080 /**
2081  * Allocate and return a vector with length coefficients, all
2082  * with the same value c.
2083  */
2084 #if !FF_API_SWS_VECTOR
2085 static
2086 #endif
sws_getConstVec(double c,int length)2087 SwsVector *sws_getConstVec(double c, int length)
2088 {
2089     int i;
2090     SwsVector *vec = sws_allocVec(length);
2091 
2092     if (!vec)
2093         return NULL;
2094 
2095     for (i = 0; i < length; i++)
2096         vec->coeff[i] = c;
2097 
2098     return vec;
2099 }
2100 
2101 /**
2102  * Allocate and return a vector with just one coefficient, with
2103  * value 1.0.
2104  */
2105 #if !FF_API_SWS_VECTOR
2106 static
2107 #endif
sws_getIdentityVec(void)2108 SwsVector *sws_getIdentityVec(void)
2109 {
2110     return sws_getConstVec(1.0, 1);
2111 }
2112 
sws_dcVec(SwsVector * a)2113 static double sws_dcVec(SwsVector *a)
2114 {
2115     int i;
2116     double sum = 0;
2117 
2118     for (i = 0; i < a->length; i++)
2119         sum += a->coeff[i];
2120 
2121     return sum;
2122 }
2123 
sws_scaleVec(SwsVector * a,double scalar)2124 void sws_scaleVec(SwsVector *a, double scalar)
2125 {
2126     int i;
2127 
2128     for (i = 0; i < a->length; i++)
2129         a->coeff[i] *= scalar;
2130 }
2131 
sws_normalizeVec(SwsVector * a,double height)2132 void sws_normalizeVec(SwsVector *a, double height)
2133 {
2134     sws_scaleVec(a, height / sws_dcVec(a));
2135 }
2136 
2137 #if FF_API_SWS_VECTOR
sws_getConvVec(SwsVector * a,SwsVector * b)2138 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
2139 {
2140     int length = a->length + b->length - 1;
2141     int i, j;
2142     SwsVector *vec = sws_getConstVec(0.0, length);
2143 
2144     if (!vec)
2145         return NULL;
2146 
2147     for (i = 0; i < a->length; i++) {
2148         for (j = 0; j < b->length; j++) {
2149             vec->coeff[i + j] += a->coeff[i] * b->coeff[j];
2150         }
2151     }
2152 
2153     return vec;
2154 }
2155 #endif
2156 
sws_sumVec(SwsVector * a,SwsVector * b)2157 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
2158 {
2159     int length = FFMAX(a->length, b->length);
2160     int i;
2161     SwsVector *vec = sws_getConstVec(0.0, length);
2162 
2163     if (!vec)
2164         return NULL;
2165 
2166     for (i = 0; i < a->length; i++)
2167         vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
2168     for (i = 0; i < b->length; i++)
2169         vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] += b->coeff[i];
2170 
2171     return vec;
2172 }
2173 
2174 #if FF_API_SWS_VECTOR
sws_diffVec(SwsVector * a,SwsVector * b)2175 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
2176 {
2177     int length = FFMAX(a->length, b->length);
2178     int i;
2179     SwsVector *vec = sws_getConstVec(0.0, length);
2180 
2181     if (!vec)
2182         return NULL;
2183 
2184     for (i = 0; i < a->length; i++)
2185         vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
2186     for (i = 0; i < b->length; i++)
2187         vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] -= b->coeff[i];
2188 
2189     return vec;
2190 }
2191 #endif
2192 
2193 /* shift left / or right if "shift" is negative */
sws_getShiftedVec(SwsVector * a,int shift)2194 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
2195 {
2196     int length = a->length + FFABS(shift) * 2;
2197     int i;
2198     SwsVector *vec = sws_getConstVec(0.0, length);
2199 
2200     if (!vec)
2201         return NULL;
2202 
2203     for (i = 0; i < a->length; i++) {
2204         vec->coeff[i + (length    - 1) / 2 -
2205                        (a->length - 1) / 2 - shift] = a->coeff[i];
2206     }
2207 
2208     return vec;
2209 }
2210 
2211 #if !FF_API_SWS_VECTOR
2212 static
2213 #endif
sws_shiftVec(SwsVector * a,int shift)2214 void sws_shiftVec(SwsVector *a, int shift)
2215 {
2216     SwsVector *shifted = sws_getShiftedVec(a, shift);
2217     if (!shifted) {
2218         makenan_vec(a);
2219         return;
2220     }
2221     av_free(a->coeff);
2222     a->coeff  = shifted->coeff;
2223     a->length = shifted->length;
2224     av_free(shifted);
2225 }
2226 
2227 #if !FF_API_SWS_VECTOR
2228 static
2229 #endif
sws_addVec(SwsVector * a,SwsVector * b)2230 void sws_addVec(SwsVector *a, SwsVector *b)
2231 {
2232     SwsVector *sum = sws_sumVec(a, b);
2233     if (!sum) {
2234         makenan_vec(a);
2235         return;
2236     }
2237     av_free(a->coeff);
2238     a->coeff  = sum->coeff;
2239     a->length = sum->length;
2240     av_free(sum);
2241 }
2242 
2243 #if FF_API_SWS_VECTOR
sws_subVec(SwsVector * a,SwsVector * b)2244 void sws_subVec(SwsVector *a, SwsVector *b)
2245 {
2246     SwsVector *diff = sws_diffVec(a, b);
2247     if (!diff) {
2248         makenan_vec(a);
2249         return;
2250     }
2251     av_free(a->coeff);
2252     a->coeff  = diff->coeff;
2253     a->length = diff->length;
2254     av_free(diff);
2255 }
2256 
sws_convVec(SwsVector * a,SwsVector * b)2257 void sws_convVec(SwsVector *a, SwsVector *b)
2258 {
2259     SwsVector *conv = sws_getConvVec(a, b);
2260     if (!conv) {
2261         makenan_vec(a);
2262         return;
2263     }
2264     av_free(a->coeff);
2265     a->coeff  = conv->coeff;
2266     a->length = conv->length;
2267     av_free(conv);
2268 }
2269 
sws_cloneVec(SwsVector * a)2270 SwsVector *sws_cloneVec(SwsVector *a)
2271 {
2272     SwsVector *vec = sws_allocVec(a->length);
2273 
2274     if (!vec)
2275         return NULL;
2276 
2277     memcpy(vec->coeff, a->coeff, a->length * sizeof(*a->coeff));
2278 
2279     return vec;
2280 }
2281 #endif
2282 
2283 /**
2284  * Print with av_log() a textual representation of the vector a
2285  * if log_level <= av_log_level.
2286  */
2287 #if !FF_API_SWS_VECTOR
2288 static
2289 #endif
sws_printVec2(SwsVector * a,AVClass * log_ctx,int log_level)2290 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
2291 {
2292     int i;
2293     double max = 0;
2294     double min = 0;
2295     double range;
2296 
2297     for (i = 0; i < a->length; i++)
2298         if (a->coeff[i] > max)
2299             max = a->coeff[i];
2300 
2301     for (i = 0; i < a->length; i++)
2302         if (a->coeff[i] < min)
2303             min = a->coeff[i];
2304 
2305     range = max - min;
2306 
2307     for (i = 0; i < a->length; i++) {
2308         int x = (int)((a->coeff[i] - min) * 60.0 / range + 0.5);
2309         av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
2310         for (; x > 0; x--)
2311             av_log(log_ctx, log_level, " ");
2312         av_log(log_ctx, log_level, "|\n");
2313     }
2314 }
2315 
sws_freeVec(SwsVector * a)2316 void sws_freeVec(SwsVector *a)
2317 {
2318     if (!a)
2319         return;
2320     av_freep(&a->coeff);
2321     a->length = 0;
2322     av_free(a);
2323 }
2324 
sws_freeFilter(SwsFilter * filter)2325 void sws_freeFilter(SwsFilter *filter)
2326 {
2327     if (!filter)
2328         return;
2329 
2330     sws_freeVec(filter->lumH);
2331     sws_freeVec(filter->lumV);
2332     sws_freeVec(filter->chrH);
2333     sws_freeVec(filter->chrV);
2334     av_free(filter);
2335 }
2336 
sws_freeContext(SwsContext * c)2337 void sws_freeContext(SwsContext *c)
2338 {
2339     int i;
2340     if (!c)
2341         return;
2342 
2343     for (i = 0; i < 4; i++)
2344         av_freep(&c->dither_error[i]);
2345 
2346     av_freep(&c->vLumFilter);
2347     av_freep(&c->vChrFilter);
2348     av_freep(&c->hLumFilter);
2349     av_freep(&c->hChrFilter);
2350 #if HAVE_ALTIVEC
2351     av_freep(&c->vYCoeffsBank);
2352     av_freep(&c->vCCoeffsBank);
2353 #endif
2354 
2355     av_freep(&c->vLumFilterPos);
2356     av_freep(&c->vChrFilterPos);
2357     av_freep(&c->hLumFilterPos);
2358     av_freep(&c->hChrFilterPos);
2359 
2360 #if HAVE_MMX_INLINE
2361 #if USE_MMAP
2362     if (c->lumMmxextFilterCode)
2363         munmap(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize);
2364     if (c->chrMmxextFilterCode)
2365         munmap(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize);
2366 #elif HAVE_VIRTUALALLOC
2367     if (c->lumMmxextFilterCode)
2368         VirtualFree(c->lumMmxextFilterCode, 0, MEM_RELEASE);
2369     if (c->chrMmxextFilterCode)
2370         VirtualFree(c->chrMmxextFilterCode, 0, MEM_RELEASE);
2371 #else
2372     av_free(c->lumMmxextFilterCode);
2373     av_free(c->chrMmxextFilterCode);
2374 #endif
2375     c->lumMmxextFilterCode = NULL;
2376     c->chrMmxextFilterCode = NULL;
2377 #endif /* HAVE_MMX_INLINE */
2378 
2379     av_freep(&c->yuvTable);
2380     av_freep(&c->formatConvBuffer);
2381 
2382     sws_freeContext(c->cascaded_context[0]);
2383     sws_freeContext(c->cascaded_context[1]);
2384     sws_freeContext(c->cascaded_context[2]);
2385     memset(c->cascaded_context, 0, sizeof(c->cascaded_context));
2386     av_freep(&c->cascaded_tmp[0]);
2387     av_freep(&c->cascaded1_tmp[0]);
2388 
2389     av_freep(&c->gamma);
2390     av_freep(&c->inv_gamma);
2391 
2392     ff_free_filters(c);
2393 
2394     av_free(c);
2395 }
2396 
sws_getCachedContext(struct SwsContext * context,int srcW,int srcH,enum AVPixelFormat srcFormat,int dstW,int dstH,enum AVPixelFormat dstFormat,int flags,SwsFilter * srcFilter,SwsFilter * dstFilter,const double * param)2397 struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW,
2398                                         int srcH, enum AVPixelFormat srcFormat,
2399                                         int dstW, int dstH,
2400                                         enum AVPixelFormat dstFormat, int flags,
2401                                         SwsFilter *srcFilter,
2402                                         SwsFilter *dstFilter,
2403                                         const double *param)
2404 {
2405     static const double default_param[2] = { SWS_PARAM_DEFAULT,
2406                                              SWS_PARAM_DEFAULT };
2407     int64_t src_h_chr_pos = -513, dst_h_chr_pos = -513,
2408             src_v_chr_pos = -513, dst_v_chr_pos = -513;
2409 
2410     if (!param)
2411         param = default_param;
2412 
2413     if (context &&
2414         (context->srcW      != srcW      ||
2415          context->srcH      != srcH      ||
2416          context->srcFormat != srcFormat ||
2417          context->dstW      != dstW      ||
2418          context->dstH      != dstH      ||
2419          context->dstFormat != dstFormat ||
2420          context->flags     != flags     ||
2421          context->param[0]  != param[0]  ||
2422          context->param[1]  != param[1])) {
2423 
2424         av_opt_get_int(context, "src_h_chr_pos", 0, &src_h_chr_pos);
2425         av_opt_get_int(context, "src_v_chr_pos", 0, &src_v_chr_pos);
2426         av_opt_get_int(context, "dst_h_chr_pos", 0, &dst_h_chr_pos);
2427         av_opt_get_int(context, "dst_v_chr_pos", 0, &dst_v_chr_pos);
2428         sws_freeContext(context);
2429         context = NULL;
2430     }
2431 
2432     if (!context) {
2433         if (!(context = sws_alloc_context()))
2434             return NULL;
2435         context->srcW      = srcW;
2436         context->srcH      = srcH;
2437         context->srcFormat = srcFormat;
2438         context->dstW      = dstW;
2439         context->dstH      = dstH;
2440         context->dstFormat = dstFormat;
2441         context->flags     = flags;
2442         context->param[0]  = param[0];
2443         context->param[1]  = param[1];
2444 
2445         av_opt_set_int(context, "src_h_chr_pos", src_h_chr_pos, 0);
2446         av_opt_set_int(context, "src_v_chr_pos", src_v_chr_pos, 0);
2447         av_opt_set_int(context, "dst_h_chr_pos", dst_h_chr_pos, 0);
2448         av_opt_set_int(context, "dst_v_chr_pos", dst_v_chr_pos, 0);
2449 
2450         if (sws_init_context(context, srcFilter, dstFilter) < 0) {
2451             sws_freeContext(context);
2452             return NULL;
2453         }
2454     }
2455     return context;
2456 }
2457