FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/utils.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 778 1414 55.0%
Functions: 24 55 43.6%
Branches: 638 1241 51.4%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2024 Niklas Haas
3 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "config.h"
23
24 #define _DEFAULT_SOURCE
25 #define _SVID_SOURCE // needed for MAP_ANONYMOUS
26 #define _DARWIN_C_SOURCE // needed for MAP_ANON
27 #include <inttypes.h>
28 #include <math.h>
29 #include <stdio.h>
30 #include <string.h>
31 #if HAVE_MMAP
32 #include <sys/mman.h>
33 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
34 #define MAP_ANONYMOUS MAP_ANON
35 #endif
36 #endif
37 #if HAVE_VIRTUALALLOC
38 #include <windows.h>
39 #endif
40
41 #include "libavutil/attributes.h"
42 #include "libavutil/avassert.h"
43 #include "libavutil/cpu.h"
44 #include "libavutil/emms.h"
45 #include "libavutil/imgutils.h"
46 #include "libavutil/intreadwrite.h"
47 #include "libavutil/libm.h"
48 #include "libavutil/mathematics.h"
49 #include "libavutil/mem.h"
50 #include "libavutil/opt.h"
51 #include "libavutil/pixdesc.h"
52 #include "libavutil/slicethread.h"
53 #include "libavutil/thread.h"
54 #include "libavutil/aarch64/cpu.h"
55 #include "libavutil/ppc/cpu.h"
56 #include "libavutil/x86/asm.h"
57 #include "libavutil/x86/cpu.h"
58 #include "libavutil/loongarch/cpu.h"
59
60 #include "rgb2rgb.h"
61 #include "swscale.h"
62 #include "swscale_internal.h"
63 #include "utils.h"
64
65 typedef struct FormatEntry {
66 uint8_t is_supported_in :1;
67 uint8_t is_supported_out :1;
68 uint8_t is_supported_endianness :1;
69 } FormatEntry;
70
71 static const FormatEntry format_entries[] = {
72 [AV_PIX_FMT_YUV420P] = { 1, 1 },
73 [AV_PIX_FMT_YUYV422] = { 1, 1 },
74 [AV_PIX_FMT_RGB24] = { 1, 1 },
75 [AV_PIX_FMT_BGR24] = { 1, 1 },
76 [AV_PIX_FMT_YUV422P] = { 1, 1 },
77 [AV_PIX_FMT_YUV444P] = { 1, 1 },
78 [AV_PIX_FMT_YUV410P] = { 1, 1 },
79 [AV_PIX_FMT_YUV411P] = { 1, 1 },
80 [AV_PIX_FMT_GRAY8] = { 1, 1 },
81 [AV_PIX_FMT_MONOWHITE] = { 1, 1 },
82 [AV_PIX_FMT_MONOBLACK] = { 1, 1 },
83 [AV_PIX_FMT_PAL8] = { 1, 0 },
84 [AV_PIX_FMT_YUVJ420P] = { 1, 1 },
85 [AV_PIX_FMT_YUVJ411P] = { 1, 1 },
86 [AV_PIX_FMT_YUVJ422P] = { 1, 1 },
87 [AV_PIX_FMT_YUVJ444P] = { 1, 1 },
88 [AV_PIX_FMT_YVYU422] = { 1, 1 },
89 [AV_PIX_FMT_UYVY422] = { 1, 1 },
90 [AV_PIX_FMT_UYYVYY411] = { 0, 0 },
91 [AV_PIX_FMT_BGR8] = { 1, 1 },
92 [AV_PIX_FMT_BGR4] = { 0, 1 },
93 [AV_PIX_FMT_BGR4_BYTE] = { 1, 1 },
94 [AV_PIX_FMT_RGB8] = { 1, 1 },
95 [AV_PIX_FMT_RGB4] = { 0, 1 },
96 [AV_PIX_FMT_RGB4_BYTE] = { 1, 1 },
97 [AV_PIX_FMT_NV12] = { 1, 1 },
98 [AV_PIX_FMT_NV21] = { 1, 1 },
99 [AV_PIX_FMT_ARGB] = { 1, 1 },
100 [AV_PIX_FMT_RGBA] = { 1, 1 },
101 [AV_PIX_FMT_ABGR] = { 1, 1 },
102 [AV_PIX_FMT_BGRA] = { 1, 1 },
103 [AV_PIX_FMT_0RGB] = { 1, 1 },
104 [AV_PIX_FMT_RGB0] = { 1, 1 },
105 [AV_PIX_FMT_0BGR] = { 1, 1 },
106 [AV_PIX_FMT_BGR0] = { 1, 1 },
107 [AV_PIX_FMT_GRAY9BE] = { 1, 1 },
108 [AV_PIX_FMT_GRAY9LE] = { 1, 1 },
109 [AV_PIX_FMT_GRAY10BE] = { 1, 1 },
110 [AV_PIX_FMT_GRAY10LE] = { 1, 1 },
111 [AV_PIX_FMT_GRAY12BE] = { 1, 1 },
112 [AV_PIX_FMT_GRAY12LE] = { 1, 1 },
113 [AV_PIX_FMT_GRAY14BE] = { 1, 1 },
114 [AV_PIX_FMT_GRAY14LE] = { 1, 1 },
115 [AV_PIX_FMT_GRAY16BE] = { 1, 1 },
116 [AV_PIX_FMT_GRAY16LE] = { 1, 1 },
117 [AV_PIX_FMT_YUV440P] = { 1, 1 },
118 [AV_PIX_FMT_YUVJ440P] = { 1, 1 },
119 [AV_PIX_FMT_YUV440P10LE] = { 1, 1 },
120 [AV_PIX_FMT_YUV440P10BE] = { 1, 1 },
121 [AV_PIX_FMT_YUV440P12LE] = { 1, 1 },
122 [AV_PIX_FMT_YUV440P12BE] = { 1, 1 },
123 [AV_PIX_FMT_YUVA420P] = { 1, 1 },
124 [AV_PIX_FMT_YUVA422P] = { 1, 1 },
125 [AV_PIX_FMT_YUVA444P] = { 1, 1 },
126 [AV_PIX_FMT_YUVA420P9BE] = { 1, 1 },
127 [AV_PIX_FMT_YUVA420P9LE] = { 1, 1 },
128 [AV_PIX_FMT_YUVA422P9BE] = { 1, 1 },
129 [AV_PIX_FMT_YUVA422P9LE] = { 1, 1 },
130 [AV_PIX_FMT_YUVA444P9BE] = { 1, 1 },
131 [AV_PIX_FMT_YUVA444P9LE] = { 1, 1 },
132 [AV_PIX_FMT_YUVA420P10BE]= { 1, 1 },
133 [AV_PIX_FMT_YUVA420P10LE]= { 1, 1 },
134 [AV_PIX_FMT_YUVA422P10BE]= { 1, 1 },
135 [AV_PIX_FMT_YUVA422P10LE]= { 1, 1 },
136 [AV_PIX_FMT_YUVA444P10BE]= { 1, 1 },
137 [AV_PIX_FMT_YUVA444P10LE]= { 1, 1 },
138 [AV_PIX_FMT_YUVA420P16BE]= { 1, 1 },
139 [AV_PIX_FMT_YUVA420P16LE]= { 1, 1 },
140 [AV_PIX_FMT_YUVA422P16BE]= { 1, 1 },
141 [AV_PIX_FMT_YUVA422P16LE]= { 1, 1 },
142 [AV_PIX_FMT_YUVA444P16BE]= { 1, 1 },
143 [AV_PIX_FMT_YUVA444P16LE]= { 1, 1 },
144 [AV_PIX_FMT_RGB48BE] = { 1, 1 },
145 [AV_PIX_FMT_RGB48LE] = { 1, 1 },
146 [AV_PIX_FMT_RGBA64BE] = { 1, 1, 1 },
147 [AV_PIX_FMT_RGBA64LE] = { 1, 1, 1 },
148 [AV_PIX_FMT_RGB565BE] = { 1, 1 },
149 [AV_PIX_FMT_RGB565LE] = { 1, 1 },
150 [AV_PIX_FMT_RGB555BE] = { 1, 1 },
151 [AV_PIX_FMT_RGB555LE] = { 1, 1 },
152 [AV_PIX_FMT_BGR565BE] = { 1, 1 },
153 [AV_PIX_FMT_BGR565LE] = { 1, 1 },
154 [AV_PIX_FMT_BGR555BE] = { 1, 1 },
155 [AV_PIX_FMT_BGR555LE] = { 1, 1 },
156 [AV_PIX_FMT_YUV420P16LE] = { 1, 1 },
157 [AV_PIX_FMT_YUV420P16BE] = { 1, 1 },
158 [AV_PIX_FMT_YUV422P16LE] = { 1, 1 },
159 [AV_PIX_FMT_YUV422P16BE] = { 1, 1 },
160 [AV_PIX_FMT_YUV444P16LE] = { 1, 1 },
161 [AV_PIX_FMT_YUV444P16BE] = { 1, 1 },
162 [AV_PIX_FMT_RGB444LE] = { 1, 1 },
163 [AV_PIX_FMT_RGB444BE] = { 1, 1 },
164 [AV_PIX_FMT_BGR444LE] = { 1, 1 },
165 [AV_PIX_FMT_BGR444BE] = { 1, 1 },
166 [AV_PIX_FMT_YA8] = { 1, 1 },
167 [AV_PIX_FMT_YA16BE] = { 1, 1 },
168 [AV_PIX_FMT_YA16LE] = { 1, 1 },
169 [AV_PIX_FMT_BGR48BE] = { 1, 1 },
170 [AV_PIX_FMT_BGR48LE] = { 1, 1 },
171 [AV_PIX_FMT_BGRA64BE] = { 1, 1, 1 },
172 [AV_PIX_FMT_BGRA64LE] = { 1, 1, 1 },
173 [AV_PIX_FMT_YUV420P9BE] = { 1, 1 },
174 [AV_PIX_FMT_YUV420P9LE] = { 1, 1 },
175 [AV_PIX_FMT_YUV420P10BE] = { 1, 1 },
176 [AV_PIX_FMT_YUV420P10LE] = { 1, 1 },
177 [AV_PIX_FMT_YUV420P12BE] = { 1, 1 },
178 [AV_PIX_FMT_YUV420P12LE] = { 1, 1 },
179 [AV_PIX_FMT_YUV420P14BE] = { 1, 1 },
180 [AV_PIX_FMT_YUV420P14LE] = { 1, 1 },
181 [AV_PIX_FMT_YUV422P9BE] = { 1, 1 },
182 [AV_PIX_FMT_YUV422P9LE] = { 1, 1 },
183 [AV_PIX_FMT_YUV422P10BE] = { 1, 1 },
184 [AV_PIX_FMT_YUV422P10LE] = { 1, 1 },
185 [AV_PIX_FMT_YUV422P12BE] = { 1, 1 },
186 [AV_PIX_FMT_YUV422P12LE] = { 1, 1 },
187 [AV_PIX_FMT_YUV422P14BE] = { 1, 1 },
188 [AV_PIX_FMT_YUV422P14LE] = { 1, 1 },
189 [AV_PIX_FMT_YUV444P9BE] = { 1, 1 },
190 [AV_PIX_FMT_YUV444P9LE] = { 1, 1 },
191 [AV_PIX_FMT_YUV444P10BE] = { 1, 1 },
192 [AV_PIX_FMT_YUV444P10LE] = { 1, 1 },
193 [AV_PIX_FMT_YUV444P12BE] = { 1, 1 },
194 [AV_PIX_FMT_YUV444P12LE] = { 1, 1 },
195 [AV_PIX_FMT_YUV444P14BE] = { 1, 1 },
196 [AV_PIX_FMT_YUV444P14LE] = { 1, 1 },
197 [AV_PIX_FMT_GBRP] = { 1, 1 },
198 [AV_PIX_FMT_GBRP9LE] = { 1, 1 },
199 [AV_PIX_FMT_GBRP9BE] = { 1, 1 },
200 [AV_PIX_FMT_GBRP10LE] = { 1, 1 },
201 [AV_PIX_FMT_GBRP10BE] = { 1, 1 },
202 [AV_PIX_FMT_GBRAP10LE] = { 1, 1 },
203 [AV_PIX_FMT_GBRAP10BE] = { 1, 1 },
204 [AV_PIX_FMT_GBRP12LE] = { 1, 1 },
205 [AV_PIX_FMT_GBRP12BE] = { 1, 1 },
206 [AV_PIX_FMT_GBRAP12LE] = { 1, 1 },
207 [AV_PIX_FMT_GBRAP12BE] = { 1, 1 },
208 [AV_PIX_FMT_GBRP14LE] = { 1, 1 },
209 [AV_PIX_FMT_GBRP14BE] = { 1, 1 },
210 [AV_PIX_FMT_GBRAP14LE] = { 1, 1 },
211 [AV_PIX_FMT_GBRAP14BE] = { 1, 1 },
212 [AV_PIX_FMT_GBRP16LE] = { 1, 1 },
213 [AV_PIX_FMT_GBRP16BE] = { 1, 1 },
214 [AV_PIX_FMT_GBRPF32LE] = { 1, 1 },
215 [AV_PIX_FMT_GBRPF32BE] = { 1, 1 },
216 [AV_PIX_FMT_GBRAPF32LE] = { 1, 1 },
217 [AV_PIX_FMT_GBRAPF32BE] = { 1, 1 },
218 [AV_PIX_FMT_GBRAP] = { 1, 1 },
219 [AV_PIX_FMT_GBRAP16LE] = { 1, 1 },
220 [AV_PIX_FMT_GBRAP16BE] = { 1, 1 },
221 [AV_PIX_FMT_BAYER_BGGR8] = { 1, 0 },
222 [AV_PIX_FMT_BAYER_RGGB8] = { 1, 0 },
223 [AV_PIX_FMT_BAYER_GBRG8] = { 1, 0 },
224 [AV_PIX_FMT_BAYER_GRBG8] = { 1, 0 },
225 [AV_PIX_FMT_BAYER_BGGR16LE] = { 1, 0 },
226 [AV_PIX_FMT_BAYER_BGGR16BE] = { 1, 0 },
227 [AV_PIX_FMT_BAYER_RGGB16LE] = { 1, 0 },
228 [AV_PIX_FMT_BAYER_RGGB16BE] = { 1, 0 },
229 [AV_PIX_FMT_BAYER_GBRG16LE] = { 1, 0 },
230 [AV_PIX_FMT_BAYER_GBRG16BE] = { 1, 0 },
231 [AV_PIX_FMT_BAYER_GRBG16LE] = { 1, 0 },
232 [AV_PIX_FMT_BAYER_GRBG16BE] = { 1, 0 },
233 [AV_PIX_FMT_XYZ12BE] = { 1, 1, 1 },
234 [AV_PIX_FMT_XYZ12LE] = { 1, 1, 1 },
235 [AV_PIX_FMT_AYUV64LE] = { 1, 1},
236 [AV_PIX_FMT_AYUV64BE] = { 1, 1 },
237 [AV_PIX_FMT_P010LE] = { 1, 1 },
238 [AV_PIX_FMT_P010BE] = { 1, 1 },
239 [AV_PIX_FMT_P012LE] = { 1, 1 },
240 [AV_PIX_FMT_P012BE] = { 1, 1 },
241 [AV_PIX_FMT_P016LE] = { 1, 1 },
242 [AV_PIX_FMT_P016BE] = { 1, 1 },
243 [AV_PIX_FMT_GRAYF32LE] = { 1, 1 },
244 [AV_PIX_FMT_GRAYF32BE] = { 1, 1 },
245 [AV_PIX_FMT_YUVA422P12BE] = { 1, 1 },
246 [AV_PIX_FMT_YUVA422P12LE] = { 1, 1 },
247 [AV_PIX_FMT_YUVA444P12BE] = { 1, 1 },
248 [AV_PIX_FMT_YUVA444P12LE] = { 1, 1 },
249 [AV_PIX_FMT_NV24] = { 1, 1 },
250 [AV_PIX_FMT_NV42] = { 1, 1 },
251 [AV_PIX_FMT_Y210LE] = { 1, 1 },
252 [AV_PIX_FMT_Y212LE] = { 1, 1 },
253 [AV_PIX_FMT_Y216LE] = { 1, 1 },
254 [AV_PIX_FMT_X2RGB10LE] = { 1, 1 },
255 [AV_PIX_FMT_X2BGR10LE] = { 1, 1 },
256 [AV_PIX_FMT_P210BE] = { 1, 1 },
257 [AV_PIX_FMT_P210LE] = { 1, 1 },
258 [AV_PIX_FMT_P212BE] = { 1, 1 },
259 [AV_PIX_FMT_P212LE] = { 1, 1 },
260 [AV_PIX_FMT_P410BE] = { 1, 1 },
261 [AV_PIX_FMT_P410LE] = { 1, 1 },
262 [AV_PIX_FMT_P412BE] = { 1, 1 },
263 [AV_PIX_FMT_P412LE] = { 1, 1 },
264 [AV_PIX_FMT_P216BE] = { 1, 1 },
265 [AV_PIX_FMT_P216LE] = { 1, 1 },
266 [AV_PIX_FMT_P416BE] = { 1, 1 },
267 [AV_PIX_FMT_P416LE] = { 1, 1 },
268 [AV_PIX_FMT_NV16] = { 1, 1 },
269 [AV_PIX_FMT_VUYA] = { 1, 1 },
270 [AV_PIX_FMT_VUYX] = { 1, 1 },
271 [AV_PIX_FMT_RGBAF16BE] = { 1, 0 },
272 [AV_PIX_FMT_RGBAF16LE] = { 1, 0 },
273 [AV_PIX_FMT_RGBF16BE] = { 1, 0 },
274 [AV_PIX_FMT_RGBF16LE] = { 1, 0 },
275 [AV_PIX_FMT_RGBF32BE] = { 1, 0 },
276 [AV_PIX_FMT_RGBF32LE] = { 1, 0 },
277 [AV_PIX_FMT_XV30LE] = { 1, 1 },
278 [AV_PIX_FMT_XV36LE] = { 1, 1 },
279 [AV_PIX_FMT_XV36BE] = { 1, 1 },
280 [AV_PIX_FMT_XV48LE] = { 1, 1 },
281 [AV_PIX_FMT_XV48BE] = { 1, 1 },
282 [AV_PIX_FMT_AYUV] = { 1, 1 },
283 [AV_PIX_FMT_UYVA] = { 1, 1 },
284 [AV_PIX_FMT_VYU444] = { 1, 1 },
285 [AV_PIX_FMT_V30XLE] = { 1, 1 },
286 };
287
288 /**
289 * Allocate and return an SwsContext without performing initialization.
290 */
291 1913 static SwsContext *alloc_set_opts(int srcW, int srcH, enum AVPixelFormat srcFormat,
292 int dstW, int dstH, enum AVPixelFormat dstFormat,
293 int flags, const double *param)
294 {
295 1913 SwsContext *sws = sws_alloc_context();
296 1913 SwsInternal *c = sws_internal(sws);
297
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1913 times.
1913 if (!c)
299 return NULL;
300
301 1913 c->flags = flags;
302 1913 c->srcW = srcW;
303 1913 c->srcH = srcH;
304 1913 c->dstW = dstW;
305 1913 c->dstH = dstH;
306 1913 c->srcFormat = srcFormat;
307 1913 c->dstFormat = dstFormat;
308
309
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1895 times.
1913 if (param) {
310 18 c->param[0] = param[0];
311 18 c->param[1] = param[1];
312 }
313
314 1913 return sws;
315 }
316
317 36478 int ff_shuffle_filter_coefficients(SwsInternal *c, int *filterPos,
318 int filterSize, int16_t *filter,
319 int dstW)
320 {
321 #if ARCH_X86_64
322 int i, j, k;
323 36478 int cpu_flags = av_get_cpu_flags();
324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36478 times.
36478 if (!filter)
325 return 0;
326
4/6
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 36280 times.
✓ Branch 2 taken 198 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 198 times.
✗ Branch 5 not taken.
36478 if (EXTERNAL_AVX2_FAST(cpu_flags) && !(cpu_flags & AV_CPU_FLAG_SLOW_GATHER)) {
327
4/4
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 124 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 38 times.
198 if ((c->srcBpc == 8) && (c->dstBpc <= 14)) {
328 36 int16_t *filterCopy = NULL;
329
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 6 times.
36 if (filterSize > 4) {
330
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if (!FF_ALLOC_TYPED_ARRAY(filterCopy, dstW * filterSize))
331 return AVERROR(ENOMEM);
332 30 memcpy(filterCopy, filter, dstW * filterSize * sizeof(int16_t));
333 }
334 // Do not swap filterPos for pixels which won't be processed by
335 // the main loop.
336
2/2
✓ Branch 0 taken 396 times.
✓ Branch 1 taken 36 times.
432 for (i = 0; i + 16 <= dstW; i += 16) {
337 396 FFSWAP(int, filterPos[i + 2], filterPos[i + 4]);
338 396 FFSWAP(int, filterPos[i + 3], filterPos[i + 5]);
339 396 FFSWAP(int, filterPos[i + 10], filterPos[i + 12]);
340 396 FFSWAP(int, filterPos[i + 11], filterPos[i + 13]);
341 }
342
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 6 times.
36 if (filterSize > 4) {
343 // 16 pixels are processed at a time.
344
2/2
✓ Branch 0 taken 330 times.
✓ Branch 1 taken 30 times.
360 for (i = 0; i + 16 <= dstW; i += 16) {
345 // 4 filter coeffs are processed at a time.
346
2/2
✓ Branch 0 taken 1782 times.
✓ Branch 1 taken 330 times.
2112 for (k = 0; k + 4 <= filterSize; k += 4) {
347
2/2
✓ Branch 0 taken 28512 times.
✓ Branch 1 taken 1782 times.
30294 for (j = 0; j < 16; ++j) {
348 28512 int from = (i + j) * filterSize + k;
349 28512 int to = i * filterSize + j * 4 + k * 16;
350 28512 memcpy(&filter[to], &filterCopy[from], 4 * sizeof(int16_t));
351 }
352 }
353 }
354 // 4 pixels are processed at a time in the tail.
355
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 30 times.
50 for (; i < dstW; i += 4) {
356 // 4 filter coeffs are processed at a time.
357 20 int rem = dstW - i >= 4 ? 4 : dstW - i;
358
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 20 times.
128 for (k = 0; k + 4 <= filterSize; k += 4) {
359
2/2
✓ Branch 0 taken 432 times.
✓ Branch 1 taken 108 times.
540 for (j = 0; j < rem; ++j) {
360 432 int from = (i + j) * filterSize + k;
361 432 int to = i * filterSize + j * 4 + k * 4;
362 432 memcpy(&filter[to], &filterCopy[from], 4 * sizeof(int16_t));
363 }
364 }
365 }
366 }
367 36 av_free(filterCopy);
368 }
369 }
370 #endif
371 36478 return 0;
372 }
373
374 1438399 int sws_isSupportedInput(enum AVPixelFormat pix_fmt)
375 {
376 1438399 return (unsigned)pix_fmt < FF_ARRAY_ELEMS(format_entries) ?
377
1/2
✓ Branch 0 taken 1438399 times.
✗ Branch 1 not taken.
1438399 format_entries[pix_fmt].is_supported_in : 0;
378 }
379
380 1438399 int sws_isSupportedOutput(enum AVPixelFormat pix_fmt)
381 {
382 1438399 return (unsigned)pix_fmt < FF_ARRAY_ELEMS(format_entries) ?
383
1/2
✓ Branch 0 taken 1438399 times.
✗ Branch 1 not taken.
1438399 format_entries[pix_fmt].is_supported_out : 0;
384 }
385
386 487768 int sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt)
387 {
388 487768 return (unsigned)pix_fmt < FF_ARRAY_ELEMS(format_entries) ?
389
1/2
✓ Branch 0 taken 487768 times.
✗ Branch 1 not taken.
487768 format_entries[pix_fmt].is_supported_endianness : 0;
390 }
391
392 static double getSplineCoeff(double a, double b, double c, double d,
393 double dist)
394 {
395 if (dist <= 1.0)
396 return ((d * dist + c) * dist + b) * dist + a;
397 else
398 return getSplineCoeff(0.0,
399 b + 2.0 * c + 3.0 * d,
400 c + 3.0 * d,
401 -b - 3.0 * c - 6.0 * d,
402 dist - 1.0);
403 }
404
405 142168 static av_cold int get_local_pos(SwsInternal *s, int chr_subsample, int pos, int dir)
406 {
407
3/4
✓ Branch 0 taken 142168 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 51513 times.
✓ Branch 3 taken 90655 times.
142168 if (pos == -1 || pos <= -513) {
408 51513 pos = (128 << chr_subsample) - 128;
409 }
410 142168 pos += 128; // relative to ideal left edge
411 142168 return pos >> chr_subsample;
412 }
413
414 typedef struct {
415 int flag; ///< flag associated to the algorithm
416 const char *description; ///< human-readable description
417 int size_factor; ///< size factor used when initing the filters
418 } ScaleAlgorithm;
419
420 static const ScaleAlgorithm scale_algorithms[] = {
421 { SWS_AREA, "area averaging", 1 /* downscale only, for upscale it is bilinear */ },
422 { SWS_BICUBIC, "bicubic", 4 },
423 { SWS_BICUBLIN, "luma bicubic / chroma bilinear", -1 },
424 { SWS_BILINEAR, "bilinear", 2 },
425 { SWS_FAST_BILINEAR, "fast bilinear", -1 },
426 { SWS_GAUSS, "Gaussian", 8 /* infinite ;) */ },
427 { SWS_LANCZOS, "Lanczos", -1 /* custom */ },
428 { SWS_POINT, "nearest neighbor / point", -1 },
429 { SWS_SINC, "sinc", 20 /* infinite ;) */ },
430 { SWS_SPLINE, "bicubic spline", 20 /* infinite :)*/ },
431 { SWS_X, "experimental", 8 },
432 };
433
434 71084 static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
435 int *outFilterSize, int xInc, int srcW,
436 int dstW, int filterAlign, int one,
437 int flags, int cpu_flags,
438 SwsVector *srcFilter, SwsVector *dstFilter,
439 double param[2], int srcPos, int dstPos)
440 {
441 int i;
442 int filterSize;
443 int filter2Size;
444 int minFilterSize;
445 71084 int64_t *filter = NULL;
446 71084 int64_t *filter2 = NULL;
447 71084 const int64_t fone = 1LL << (54 - FFMIN(av_log2(srcW/dstW), 8));
448 71084 int ret = -1;
449
450 71084 emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
451
452 // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
453
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 71084 times.
71084 if (!FF_ALLOC_TYPED_ARRAY(*filterPos, dstW + 3))
454 goto nomem;
455
456
6/8
✓ Branch 0 taken 61434 times.
✓ Branch 1 taken 9650 times.
✓ Branch 2 taken 54715 times.
✓ Branch 3 taken 6719 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 9650 times.
✓ Branch 6 taken 54715 times.
✗ Branch 7 not taken.
125799 if (FFABS(xInc - 0x10000) < 10 && srcPos == dstPos) { // unscaled
457 int i;
458 54715 filterSize = 1;
459
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 54715 times.
54715 if (!FF_ALLOCZ_TYPED_ARRAY(filter, dstW * filterSize))
460 goto nomem;
461
462
2/2
✓ Branch 0 taken 16556530 times.
✓ Branch 1 taken 54715 times.
16611245 for (i = 0; i < dstW; i++) {
463 16556530 filter[i * filterSize] = fone;
464 16556530 (*filterPos)[i] = i;
465 }
466
2/2
✓ Branch 0 taken 292 times.
✓ Branch 1 taken 16077 times.
16369 } else if (flags & SWS_POINT) { // lame looking point sampling mode
467 int i;
468 int64_t xDstInSrc;
469 292 filterSize = 1;
470
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 292 times.
292 if (!FF_ALLOC_TYPED_ARRAY(filter, dstW * filterSize))
471 goto nomem;
472
473 292 xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
474
2/2
✓ Branch 0 taken 85885 times.
✓ Branch 1 taken 292 times.
86177 for (i = 0; i < dstW; i++) {
475 85885 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
476
477 85885 (*filterPos)[i] = xx;
478 85885 filter[i] = fone;
479 85885 xDstInSrc += xInc;
480 }
481
4/4
✓ Branch 0 taken 9490 times.
✓ Branch 1 taken 6587 times.
✓ Branch 2 taken 9406 times.
✓ Branch 3 taken 84 times.
16077 } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
482
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15993 times.
16077 (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
483 int i;
484 int64_t xDstInSrc;
485 84 filterSize = 2;
486
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
84 if (!FF_ALLOC_TYPED_ARRAY(filter, dstW * filterSize))
487 goto nomem;
488
489 84 xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
490
2/2
✓ Branch 0 taken 142992 times.
✓ Branch 1 taken 84 times.
143076 for (i = 0; i < dstW; i++) {
491 142992 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
492 int j;
493
494 142992 (*filterPos)[i] = xx;
495 // bilinear upscale / linear interpolate / area averaging
496
2/2
✓ Branch 0 taken 285984 times.
✓ Branch 1 taken 142992 times.
428976 for (j = 0; j < filterSize; j++) {
497 285984 int64_t coeff = fone - FFABS((int64_t)xx * (1 << 16) - xDstInSrc) * (fone >> 16);
498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 285984 times.
285984 if (coeff < 0)
499 coeff = 0;
500 285984 filter[i * filterSize + j] = coeff;
501 285984 xx++;
502 }
503 142992 xDstInSrc += xInc;
504 }
505 } else {
506 int64_t xDstInSrc;
507 15993 int sizeFactor = -1;
508
509
1/2
✓ Branch 0 taken 32478 times.
✗ Branch 1 not taken.
32478 for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
510
3/4
✓ Branch 0 taken 15993 times.
✓ Branch 1 taken 16485 times.
✓ Branch 2 taken 15993 times.
✗ Branch 3 not taken.
32478 if (flags & scale_algorithms[i].flag && scale_algorithms[i].size_factor > 0) {
511 15993 sizeFactor = scale_algorithms[i].size_factor;
512 15993 break;
513 }
514 }
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15993 times.
15993 if (flags & SWS_LANCZOS)
516 sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
517
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15993 times.
15993 av_assert0(sizeFactor > 0);
518
519
2/2
✓ Branch 0 taken 9406 times.
✓ Branch 1 taken 6587 times.
15993 if (xInc <= 1 << 16)
520 9406 filterSize = 1 + sizeFactor; // upscale
521 else
522 6587 filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
523
524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15993 times.
15993 filterSize = FFMIN(filterSize, srcW - 2);
525 15993 filterSize = FFMAX(filterSize, 1);
526
527
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15993 times.
15993 if (!FF_ALLOC_TYPED_ARRAY(filter, dstW * filterSize))
528 goto nomem;
529 15993 xDstInSrc = ((dstPos*(int64_t)xInc)>>7) - ((srcPos*0x10000LL)>>7);
530
2/2
✓ Branch 0 taken 4270089 times.
✓ Branch 1 taken 15993 times.
4286082 for (i = 0; i < dstW; i++) {
531 4270089 int xx = (xDstInSrc - (filterSize - 2) * (1LL<<16)) / (1 << 17);
532 int j;
533 4270089 (*filterPos)[i] = xx;
534
2/2
✓ Branch 0 taken 25872820 times.
✓ Branch 1 taken 4270089 times.
30142909 for (j = 0; j < filterSize; j++) {
535 25872820 int64_t d = (FFABS(((int64_t)xx * (1 << 17)) - xDstInSrc)) << 13;
536 double floatd;
537 int64_t coeff;
538
539
2/2
✓ Branch 0 taken 9466745 times.
✓ Branch 1 taken 16406075 times.
25872820 if (xInc > 1 << 16)
540 9466745 d = d * dstW / srcW;
541 25872820 floatd = d * (1.0 / (1 << 30));
542
543
2/2
✓ Branch 0 taken 25219402 times.
✓ Branch 1 taken 653418 times.
25872820 if (flags & SWS_BICUBIC) {
544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25219402 times.
25219402 int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1 << 24);
545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25219402 times.
25219402 int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
546
547
2/2
✓ Branch 0 taken 4343332 times.
✓ Branch 1 taken 20876070 times.
25219402 if (d >= 1LL << 31) {
548 4343332 coeff = 0.0;
549 } else {
550 20876070 int64_t dd = (d * d) >> 30;
551 20876070 int64_t ddd = (dd * d) >> 30;
552
553
2/2
✓ Branch 0 taken 10447146 times.
✓ Branch 1 taken 10428924 times.
20876070 if (d < 1LL << 30)
554 10447146 coeff = (12 * (1 << 24) - 9 * B - 6 * C) * ddd +
555 10447146 (-18 * (1 << 24) + 12 * B + 6 * C) * dd +
556 10447146 (6 * (1 << 24) - 2 * B) * (1 << 30);
557 else
558 10428924 coeff = (-B - 6 * C) * ddd +
559 10428924 (6 * B + 30 * C) * dd +
560 10428924 (-12 * B - 48 * C) * d +
561 10428924 (8 * B + 24 * C) * (1 << 30);
562 }
563 25219402 coeff /= (1LL<<54)/fone;
564
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 653418 times.
653418 } else if (flags & SWS_X) {
565 double A = param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
566 double c;
567
568 if (floatd < 1.0)
569 c = cos(floatd * M_PI);
570 else
571 c = -1.0;
572 if (c < 0.0)
573 c = -pow(-c, A);
574 else
575 c = pow(c, A);
576 coeff = (c * 0.5 + 0.5) * fone;
577
2/2
✓ Branch 0 taken 292968 times.
✓ Branch 1 taken 360450 times.
653418 } else if (flags & SWS_AREA) {
578 292968 int64_t d2 = d - (1 << 29);
579
2/2
✓ Branch 0 taken 218924 times.
✓ Branch 1 taken 74044 times.
292968 if (d2 * xInc < -(1LL << (29 + 16)))
580 218924 coeff = 1.0 * (1LL << (30 + 16));
581
2/2
✓ Branch 0 taken 63602 times.
✓ Branch 1 taken 10442 times.
74044 else if (d2 * xInc < (1LL << (29 + 16)))
582 63602 coeff = -d2 * xInc + (1LL << (29 + 16));
583 else
584 10442 coeff = 0.0;
585 292968 coeff *= fone >> (30 + 16);
586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 360450 times.
360450 } else if (flags & SWS_GAUSS) {
587 double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
588 coeff = exp2(-p * floatd * floatd) * fone;
589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 360450 times.
360450 } else if (flags & SWS_SINC) {
590 coeff = (d ? sin(floatd * M_PI) / (floatd * M_PI) : 1.0) * fone;
591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 360450 times.
360450 } else if (flags & SWS_LANCZOS) {
592 double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
593 coeff = (d ? sin(floatd * M_PI) * sin(floatd * M_PI / p) /
594 (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
595 if (floatd > p)
596 coeff = 0;
597
1/2
✓ Branch 0 taken 360450 times.
✗ Branch 1 not taken.
360450 } else if (flags & SWS_BILINEAR) {
598 360450 coeff = (1 << 30) - d;
599
2/2
✓ Branch 0 taken 43542 times.
✓ Branch 1 taken 316908 times.
360450 if (coeff < 0)
600 43542 coeff = 0;
601 360450 coeff *= fone >> 30;
602 } else if (flags & SWS_SPLINE) {
603 double p = -2.196152422706632;
604 coeff = getSplineCoeff(1.0, 0.0, p, -p - 1.0, floatd) * fone;
605 } else {
606 av_assert0(0);
607 }
608
609 25872820 filter[i * filterSize + j] = coeff;
610 25872820 xx++;
611 }
612 4270089 xDstInSrc += 2LL * xInc;
613 }
614 }
615
616 /* apply src & dst Filter to filter -> filter2
617 * av_free(filter);
618 */
619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71084 times.
71084 av_assert0(filterSize > 0);
620 71084 filter2Size = filterSize;
621
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71084 times.
71084 if (srcFilter)
622 filter2Size += srcFilter->length - 1;
623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71084 times.
71084 if (dstFilter)
624 filter2Size += dstFilter->length - 1;
625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71084 times.
71084 av_assert0(filter2Size > 0);
626
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 71084 times.
71084 if (!FF_ALLOCZ_TYPED_ARRAY(filter2, dstW * filter2Size))
627 goto nomem;
628
2/2
✓ Branch 0 taken 21055496 times.
✓ Branch 1 taken 71084 times.
21126580 for (i = 0; i < dstW; i++) {
629 int j, k;
630
631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21055496 times.
21055496 if (srcFilter) {
632 for (k = 0; k < srcFilter->length; k++) {
633 for (j = 0; j < filterSize; j++)
634 filter2[i * filter2Size + k + j] +=
635 srcFilter->coeff[k] * filter[i * filterSize + j];
636 }
637 } else {
638
2/2
✓ Branch 0 taken 42801219 times.
✓ Branch 1 taken 21055496 times.
63856715 for (j = 0; j < filterSize; j++)
639 42801219 filter2[i * filter2Size + j] = filter[i * filterSize + j];
640 }
641 // FIXME dstFilter
642
643 21055496 (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
644 }
645 71084 av_freep(&filter);
646
647 /* try to reduce the filter-size (step1 find size and shift left) */
648 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
649 71084 minFilterSize = 0;
650
2/2
✓ Branch 0 taken 21055496 times.
✓ Branch 1 taken 71084 times.
21126580 for (i = dstW - 1; i >= 0; i--) {
651 21055496 int min = filter2Size;
652 int j;
653 21055496 int64_t cutOff = 0.0;
654
655 /* get rid of near zero elements on the left by shifting left */
656
1/2
✓ Branch 0 taken 22866909 times.
✗ Branch 1 not taken.
22866909 for (j = 0; j < filter2Size; j++) {
657 int k;
658 22866909 cutOff += FFABS(filter2[i * filter2Size]);
659
660
2/2
✓ Branch 0 taken 21052923 times.
✓ Branch 1 taken 1813986 times.
22866909 if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
661 21052923 break;
662
663 /* preserve monotonicity because the core can't handle the
664 * filter otherwise */
665
4/4
✓ Branch 0 taken 1803301 times.
✓ Branch 1 taken 10685 times.
✓ Branch 2 taken 2573 times.
✓ Branch 3 taken 1800728 times.
1813986 if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
666 2573 break;
667
668 // move filter coefficients left
669
2/2
✓ Branch 0 taken 8081273 times.
✓ Branch 1 taken 1811413 times.
9892686 for (k = 1; k < filter2Size; k++)
670 8081273 filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
671 1811413 filter2[i * filter2Size + k - 1] = 0;
672 1811413 (*filterPos)[i]++;
673 }
674
675 21055496 cutOff = 0;
676 /* count near zeros on the right */
677
2/2
✓ Branch 0 taken 8871539 times.
✓ Branch 1 taken 16643246 times.
25514785 for (j = filter2Size - 1; j > 0; j--) {
678 8871539 cutOff += FFABS(filter2[i * filter2Size + j]);
679
680
2/2
✓ Branch 0 taken 4412250 times.
✓ Branch 1 taken 4459289 times.
8871539 if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
681 4412250 break;
682 4459289 min--;
683 }
684
685
2/2
✓ Branch 0 taken 71758 times.
✓ Branch 1 taken 20983738 times.
21055496 if (min > minFilterSize)
686 71758 minFilterSize = min;
687 }
688
689 if (PPC_ALTIVEC(cpu_flags)) {
690 // we can handle the special case 4, so we don't want to go the full 8
691 if (minFilterSize < 5)
692 filterAlign = 4;
693
694 /* We really don't want to waste our time doing useless computation, so
695 * fall back on the scalar C code for very small filters.
696 * Vectorizing is worth it only if you have a decent-sized vector. */
697 if (minFilterSize < 3)
698 filterAlign = 1;
699 }
700
701
2/2
✓ Branch 0 taken 296 times.
✓ Branch 1 taken 70788 times.
71084 if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX || have_neon(cpu_flags)) {
702 // special case for unscaled vertical filtering
703
4/4
✓ Branch 0 taken 277 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 133 times.
✓ Branch 3 taken 144 times.
296 if (minFilterSize == 1 && filterAlign == 2)
704 133 filterAlign = 1;
705 }
706
707 if (have_lasx(cpu_flags) || have_lsx(cpu_flags)) {
708 int reNum = minFilterSize & (0x07);
709
710 if (minFilterSize < 5)
711 filterAlign = 4;
712 if (reNum < 3)
713 filterAlign = 1;
714 }
715
716
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71084 times.
71084 av_assert0(minFilterSize > 0);
717 71084 filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
718
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71084 times.
71084 av_assert0(filterSize > 0);
719 71084 filter = av_malloc_array(dstW, filterSize * sizeof(*filter));
720
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71084 times.
71084 if (!filter)
721 goto nomem;
722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71084 times.
71084 if (filterSize >= MAX_FILTER_SIZE * 16 /
723
2/2
✓ Branch 0 taken 68528 times.
✓ Branch 1 taken 2556 times.
71084 ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16)) {
724 ret = RETCODE_USE_CASCADE;
725 goto fail;
726 }
727 71084 *outFilterSize = filterSize;
728
729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71084 times.
71084 if (flags & SWS_PRINT_INFO)
730 av_log(NULL, AV_LOG_VERBOSE,
731 "SwScaler: reducing / aligning filtersize %d -> %d\n",
732 filter2Size, filterSize);
733 /* try to reduce the filter-size (step2 reduce it) */
734
2/2
✓ Branch 0 taken 21055496 times.
✓ Branch 1 taken 71084 times.
21126580 for (i = 0; i < dstW; i++) {
735 int j;
736
737
2/2
✓ Branch 0 taken 38610309 times.
✓ Branch 1 taken 21055496 times.
59665805 for (j = 0; j < filterSize; j++) {
738
2/2
✓ Branch 0 taken 138690 times.
✓ Branch 1 taken 38471619 times.
38610309 if (j >= filter2Size)
739 138690 filter[i * filterSize + j] = 0;
740 else
741 38471619 filter[i * filterSize + j] = filter2[i * filter2Size + j];
742
4/4
✓ Branch 0 taken 37144803 times.
✓ Branch 1 taken 1465506 times.
✓ Branch 2 taken 103680 times.
✓ Branch 3 taken 37041123 times.
38610309 if ((flags & SWS_BITEXACT) && j >= minFilterSize)
743 103680 filter[i * filterSize + j] = 0;
744 }
745 }
746
747 // FIXME try to align filterPos if possible
748
749 // fix borders
750
2/2
✓ Branch 0 taken 21055496 times.
✓ Branch 1 taken 71084 times.
21126580 for (i = 0; i < dstW; i++) {
751 int j;
752
2/2
✓ Branch 0 taken 34612 times.
✓ Branch 1 taken 21020884 times.
21055496 if ((*filterPos)[i] < 0) {
753 // move filter coefficients left to compensate for filterPos
754
2/2
✓ Branch 0 taken 165788 times.
✓ Branch 1 taken 34612 times.
200400 for (j = 1; j < filterSize; j++) {
755 165788 int left = FFMAX(j + (*filterPos)[i], 0);
756 165788 filter[i * filterSize + left] += filter[i * filterSize + j];
757 165788 filter[i * filterSize + j] = 0;
758 }
759 34612 (*filterPos)[i]= 0;
760 }
761
762
2/2
✓ Branch 0 taken 46607 times.
✓ Branch 1 taken 21008889 times.
21055496 if ((*filterPos)[i] + filterSize > srcW) {
763 46607 int shift = (*filterPos)[i] + FFMIN(filterSize - srcW, 0);
764 46607 int64_t acc = 0;
765
766
2/2
✓ Branch 0 taken 250790 times.
✓ Branch 1 taken 46607 times.
297397 for (j = filterSize - 1; j >= 0; j--) {
767
2/2
✓ Branch 0 taken 74888 times.
✓ Branch 1 taken 175902 times.
250790 if ((*filterPos)[i] + j >= srcW) {
768 74888 acc += filter[i * filterSize + j];
769 74888 filter[i * filterSize + j] = 0;
770 }
771 }
772
2/2
✓ Branch 0 taken 250790 times.
✓ Branch 1 taken 46607 times.
297397 for (j = filterSize - 1; j >= 0; j--) {
773
2/2
✓ Branch 0 taken 74888 times.
✓ Branch 1 taken 175902 times.
250790 if (j < shift) {
774 74888 filter[i * filterSize + j] = 0;
775 } else {
776 175902 filter[i * filterSize + j] = filter[i * filterSize + j - shift];
777 }
778 }
779
780 46607 (*filterPos)[i]-= shift;
781 46607 filter[i * filterSize + srcW - 1 - (*filterPos)[i]] += acc;
782 }
783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21055496 times.
21055496 av_assert0((*filterPos)[i] >= 0);
784
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21055496 times.
21055496 av_assert0((*filterPos)[i] < srcW);
785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21055496 times.
21055496 if ((*filterPos)[i] + filterSize > srcW) {
786 for (j = 0; j < filterSize; j++) {
787 av_assert0((*filterPos)[i] + j < srcW || !filter[i * filterSize + j]);
788 }
789 }
790 }
791
792 // Note the +1 is for the MMX scaler which reads over the end
793 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
794
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 71084 times.
71084 if (!FF_ALLOCZ_TYPED_ARRAY(*outFilter, *outFilterSize * (dstW + 3)))
795 goto nomem;
796
797 /* normalize & store in outFilter */
798
2/2
✓ Branch 0 taken 21055496 times.
✓ Branch 1 taken 71084 times.
21126580 for (i = 0; i < dstW; i++) {
799 int j;
800 21055496 int64_t error = 0;
801 21055496 int64_t sum = 0;
802
803
2/2
✓ Branch 0 taken 38610309 times.
✓ Branch 1 taken 21055496 times.
59665805 for (j = 0; j < filterSize; j++) {
804 38610309 sum += filter[i * filterSize + j];
805 }
806 21055496 sum = (sum + one / 2) / one;
807
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21055496 times.
21055496 if (!sum) {
808 av_log(NULL, AV_LOG_WARNING, "SwScaler: zero vector in scaling\n");
809 sum = 1;
810 }
811
2/2
✓ Branch 0 taken 38610309 times.
✓ Branch 1 taken 21055496 times.
59665805 for (j = 0; j < *outFilterSize; j++) {
812 38610309 int64_t v = filter[i * filterSize + j] + error;
813
2/2
✓ Branch 0 taken 28256131 times.
✓ Branch 1 taken 10354178 times.
38610309 int intV = ROUNDED_DIV(v, sum);
814 38610309 (*outFilter)[i * (*outFilterSize) + j] = intV;
815 38610309 error = v - intV * sum;
816 }
817 }
818
819 71084 (*filterPos)[dstW + 0] =
820 71084 (*filterPos)[dstW + 1] =
821 71084 (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1]; /* the MMX/SSE scaler will
822 * read over the end */
823
2/2
✓ Branch 0 taken 154794 times.
✓ Branch 1 taken 71084 times.
225878 for (i = 0; i < *outFilterSize; i++) {
824 154794 int k = (dstW - 1) * (*outFilterSize) + i;
825 154794 (*outFilter)[k + 1 * (*outFilterSize)] =
826 154794 (*outFilter)[k + 2 * (*outFilterSize)] =
827 154794 (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
828 }
829
830 71084 ret = 0;
831 71084 goto done;
832 nomem:
833 ret = AVERROR(ENOMEM);
834 fail:
835 if(ret < 0)
836 av_log(NULL, ret == RETCODE_USE_CASCADE ? AV_LOG_DEBUG : AV_LOG_ERROR, "sws: initFilter failed\n");
837 done:
838 71084 av_free(filter);
839 71084 av_free(filter2);
840 71084 return ret;
841 }
842
843 14821 static void fill_rgb2yuv_table(SwsInternal *c, const int table[4], int dstRange)
844 {
845 int64_t W, V, Z, Cy, Cu, Cv;
846 14821 int64_t vr = table[0];
847 14821 int64_t ub = table[1];
848 14821 int64_t ug = -table[2];
849 14821 int64_t vg = -table[3];
850 14821 int64_t ONE = 65536;
851 14821 int64_t cy = ONE;
852 14821 uint8_t *p = (uint8_t*)c->input_rgb2yuv_table;
853 int i;
854 static const int8_t map[] = {
855 BY_IDX, GY_IDX, -1 , BY_IDX, BY_IDX, GY_IDX, -1 , BY_IDX,
856 RY_IDX, -1 , GY_IDX, RY_IDX, RY_IDX, -1 , GY_IDX, RY_IDX,
857 RY_IDX, GY_IDX, -1 , RY_IDX, RY_IDX, GY_IDX, -1 , RY_IDX,
858 BY_IDX, -1 , GY_IDX, BY_IDX, BY_IDX, -1 , GY_IDX, BY_IDX,
859 BU_IDX, GU_IDX, -1 , BU_IDX, BU_IDX, GU_IDX, -1 , BU_IDX,
860 RU_IDX, -1 , GU_IDX, RU_IDX, RU_IDX, -1 , GU_IDX, RU_IDX,
861 RU_IDX, GU_IDX, -1 , RU_IDX, RU_IDX, GU_IDX, -1 , RU_IDX,
862 BU_IDX, -1 , GU_IDX, BU_IDX, BU_IDX, -1 , GU_IDX, BU_IDX,
863 BV_IDX, GV_IDX, -1 , BV_IDX, BV_IDX, GV_IDX, -1 , BV_IDX,
864 RV_IDX, -1 , GV_IDX, RV_IDX, RV_IDX, -1 , GV_IDX, RV_IDX,
865 RV_IDX, GV_IDX, -1 , RV_IDX, RV_IDX, GV_IDX, -1 , RV_IDX,
866 BV_IDX, -1 , GV_IDX, BV_IDX, BV_IDX, -1 , GV_IDX, BV_IDX,
867 RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX,
868 BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX,
869 GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 ,
870 -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX,
871 RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX,
872 BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX,
873 GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 ,
874 -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX,
875 RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX,
876 BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX,
877 GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 ,
878 -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, //23
879 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //24
880 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //25
881 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //26
882 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //27
883 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //28
884 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //29
885 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //30
886 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //31
887 BY_IDX, GY_IDX, RY_IDX, -1 , -1 , -1 , -1 , -1 , //32
888 BU_IDX, GU_IDX, RU_IDX, -1 , -1 , -1 , -1 , -1 , //33
889 BV_IDX, GV_IDX, RV_IDX, -1 , -1 , -1 , -1 , -1 , //34
890 };
891
892 14821 dstRange = 0; //FIXME range = 1 is handled elsewhere
893
894
1/2
✓ Branch 0 taken 14821 times.
✗ Branch 1 not taken.
14821 if (!dstRange) {
895 14821 cy = cy * 255 / 219;
896 } else {
897 vr = vr * 224 / 255;
898 ub = ub * 224 / 255;
899 ug = ug * 224 / 255;
900 vg = vg * 224 / 255;
901 }
902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14821 times.
14821 W = ROUNDED_DIV(ONE*ONE*ug, ub);
903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14821 times.
14821 V = ROUNDED_DIV(ONE*ONE*vg, vr);
904 14821 Z = ONE*ONE-W-V;
905
906
1/2
✓ Branch 0 taken 14821 times.
✗ Branch 1 not taken.
14821 Cy = ROUNDED_DIV(cy*Z, ONE);
907
1/2
✓ Branch 0 taken 14821 times.
✗ Branch 1 not taken.
14821 Cu = ROUNDED_DIV(ub*Z, ONE);
908
1/2
✓ Branch 0 taken 14821 times.
✗ Branch 1 not taken.
14821 Cv = ROUNDED_DIV(vr*Z, ONE);
909
910
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14821 times.
14821 c->input_rgb2yuv_table[RY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cy);
911 14821 c->input_rgb2yuv_table[GY_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cy);
912
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14821 times.
14821 c->input_rgb2yuv_table[BY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cy);
913
914
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14821 times.
14821 c->input_rgb2yuv_table[RU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cu);
915 14821 c->input_rgb2yuv_table[GU_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cu);
916
1/2
✓ Branch 0 taken 14821 times.
✗ Branch 1 not taken.
14821 c->input_rgb2yuv_table[BU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(Z+W) , Cu);
917
918
1/2
✓ Branch 0 taken 14821 times.
✗ Branch 1 not taken.
14821 c->input_rgb2yuv_table[RV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(V+Z) , Cv);
919 14821 c->input_rgb2yuv_table[GV_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cv);
920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14821 times.
14821 c->input_rgb2yuv_table[BV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cv);
921
922
1/2
✓ Branch 0 taken 14821 times.
✗ Branch 1 not taken.
14821 if(/*!dstRange && */!memcmp(table, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], sizeof(ff_yuv2rgb_coeffs[SWS_CS_DEFAULT]))) {
923 14821 c->input_rgb2yuv_table[BY_IDX] = ((int)(0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
924 14821 c->input_rgb2yuv_table[BV_IDX] = (-(int)(0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
925 14821 c->input_rgb2yuv_table[BU_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
926 14821 c->input_rgb2yuv_table[GY_IDX] = ((int)(0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
927 14821 c->input_rgb2yuv_table[GV_IDX] = (-(int)(0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
928 14821 c->input_rgb2yuv_table[GU_IDX] = (-(int)(0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
929 14821 c->input_rgb2yuv_table[RY_IDX] = ((int)(0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
930 14821 c->input_rgb2yuv_table[RV_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
931 14821 c->input_rgb2yuv_table[RU_IDX] = (-(int)(0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
932 }
933
2/2
✓ Branch 0 taken 4149880 times.
✓ Branch 1 taken 14821 times.
4164701 for(i=0; i<FF_ARRAY_ELEMS(map); i++)
934
2/2
✓ Branch 0 taken 2267613 times.
✓ Branch 1 taken 1882267 times.
4149880 AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ? c->input_rgb2yuv_table[map[i]] : 0);
935 14821 }
936
937 516 static void fill_xyztables(SwsInternal *c)
938 {
939 int i;
940 516 double xyzgamma = XYZ_GAMMA;
941 516 double rgbgamma = 1.0 / RGB_GAMMA;
942 516 double xyzgammainv = 1.0 / XYZ_GAMMA;
943 516 double rgbgammainv = RGB_GAMMA;
944 static const int16_t xyz2rgb_matrix[3][4] = {
945 {13270, -6295, -2041},
946 {-3969, 7682, 170},
947 { 228, -835, 4329} };
948 static const int16_t rgb2xyz_matrix[3][4] = {
949 {1689, 1464, 739},
950 { 871, 2929, 296},
951 { 79, 488, 3891} };
952 static int16_t xyzgamma_tab[4096], rgbgamma_tab[4096], xyzgammainv_tab[4096], rgbgammainv_tab[4096];
953
954 516 memcpy(c->xyz2rgb_matrix, xyz2rgb_matrix, sizeof(c->xyz2rgb_matrix));
955 516 memcpy(c->rgb2xyz_matrix, rgb2xyz_matrix, sizeof(c->rgb2xyz_matrix));
956 516 c->xyzgamma = xyzgamma_tab;
957 516 c->rgbgamma = rgbgamma_tab;
958 516 c->xyzgammainv = xyzgammainv_tab;
959 516 c->rgbgammainv = rgbgammainv_tab;
960
961
2/2
✓ Branch 0 taken 482 times.
✓ Branch 1 taken 34 times.
516 if (rgbgamma_tab[4095])
962 482 return;
963
964 /* set gamma vectors */
965
2/2
✓ Branch 0 taken 139264 times.
✓ Branch 1 taken 34 times.
139298 for (i = 0; i < 4096; i++) {
966 139264 xyzgamma_tab[i] = lrint(pow(i / 4095.0, xyzgamma) * 4095.0);
967 139264 rgbgamma_tab[i] = lrint(pow(i / 4095.0, rgbgamma) * 4095.0);
968 139264 xyzgammainv_tab[i] = lrint(pow(i / 4095.0, xyzgammainv) * 4095.0);
969 139264 rgbgammainv_tab[i] = lrint(pow(i / 4095.0, rgbgammainv) * 4095.0);
970 }
971 }
972
973 15264 static int handle_jpeg(enum AVPixelFormat *format)
974 {
975
7/7
✓ Branch 0 taken 89 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 47 times.
✓ Branch 3 taken 56 times.
✓ Branch 4 taken 38 times.
✓ Branch 5 taken 388 times.
✓ Branch 6 taken 14630 times.
15264 switch (*format) {
976 89 case AV_PIX_FMT_YUVJ420P:
977 89 *format = AV_PIX_FMT_YUV420P;
978 89 return 1;
979 16 case AV_PIX_FMT_YUVJ411P:
980 16 *format = AV_PIX_FMT_YUV411P;
981 16 return 1;
982 47 case AV_PIX_FMT_YUVJ422P:
983 47 *format = AV_PIX_FMT_YUV422P;
984 47 return 1;
985 56 case AV_PIX_FMT_YUVJ444P:
986 56 *format = AV_PIX_FMT_YUV444P;
987 56 return 1;
988 38 case AV_PIX_FMT_YUVJ440P:
989 38 *format = AV_PIX_FMT_YUV440P;
990 38 return 1;
991 388 case AV_PIX_FMT_GRAY8:
992 case AV_PIX_FMT_YA8:
993 case AV_PIX_FMT_GRAY9LE:
994 case AV_PIX_FMT_GRAY9BE:
995 case AV_PIX_FMT_GRAY10LE:
996 case AV_PIX_FMT_GRAY10BE:
997 case AV_PIX_FMT_GRAY12LE:
998 case AV_PIX_FMT_GRAY12BE:
999 case AV_PIX_FMT_GRAY14LE:
1000 case AV_PIX_FMT_GRAY14BE:
1001 case AV_PIX_FMT_GRAY16LE:
1002 case AV_PIX_FMT_GRAY16BE:
1003 case AV_PIX_FMT_YA16BE:
1004 case AV_PIX_FMT_YA16LE:
1005 388 return 1;
1006 14630 default:
1007 14630 return 0;
1008 }
1009 }
1010
1011 131958 static int handle_0alpha(enum AVPixelFormat *format)
1012 {
1013
5/5
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 203 times.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 131698 times.
131958 switch (*format) {
1014 19 case AV_PIX_FMT_0BGR : *format = AV_PIX_FMT_ABGR ; return 1;
1015 203 case AV_PIX_FMT_BGR0 : *format = AV_PIX_FMT_BGRA ; return 4;
1016 19 case AV_PIX_FMT_0RGB : *format = AV_PIX_FMT_ARGB ; return 1;
1017 19 case AV_PIX_FMT_RGB0 : *format = AV_PIX_FMT_RGBA ; return 4;
1018 131698 default: return 0;
1019 }
1020 }
1021
1022 131958 static int handle_xyz(enum AVPixelFormat *format)
1023 {
1024
3/3
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 160 times.
✓ Branch 2 taken 131784 times.
131958 switch (*format) {
1025 14 case AV_PIX_FMT_XYZ12BE : *format = AV_PIX_FMT_RGB48BE; return 1;
1026 160 case AV_PIX_FMT_XYZ12LE : *format = AV_PIX_FMT_RGB48LE; return 1;
1027 131784 default: return 0;
1028 }
1029 }
1030
1031 65979 static void handle_formats(SwsInternal *c)
1032 {
1033 65979 c->src0Alpha |= handle_0alpha(&c->srcFormat);
1034 65979 c->dst0Alpha |= handle_0alpha(&c->dstFormat);
1035 65979 c->srcXYZ |= handle_xyz(&c->srcFormat);
1036 65979 c->dstXYZ |= handle_xyz(&c->dstFormat);
1037
4/4
✓ Branch 0 taken 65754 times.
✓ Branch 1 taken 225 times.
✓ Branch 2 taken 291 times.
✓ Branch 3 taken 65463 times.
65979 if (c->srcXYZ || c->dstXYZ)
1038 516 fill_xyztables(c);
1039 65979 }
1040
1041 97696 static int range_override_needed(enum AVPixelFormat format)
1042 {
1043
4/4
✓ Branch 1 taken 40703 times.
✓ Branch 2 taken 56993 times.
✓ Branch 4 taken 37321 times.
✓ Branch 5 taken 3382 times.
97696 return !isYUV(format) && !isGray(format);
1044 }
1045
1046 45186 int sws_setColorspaceDetails(SwsContext *sws, const int inv_table[4],
1047 int srcRange, const int table[4], int dstRange,
1048 int brightness, int contrast, int saturation)
1049 {
1050 45186 SwsInternal *c = sws_internal(sws);
1051 const AVPixFmtDescriptor *desc_dst;
1052 const AVPixFmtDescriptor *desc_src;
1053 45186 int need_reinit = 0;
1054
1055
2/2
✓ Branch 0 taken 1888 times.
✓ Branch 1 taken 43298 times.
45186 if (c->nb_slice_ctx) {
1056 1888 int parent_ret = 0;
1057
2/2
✓ Branch 0 taken 16937 times.
✓ Branch 1 taken 1888 times.
18825 for (int i = 0; i < c->nb_slice_ctx; i++) {
1058 16937 int ret = sws_setColorspaceDetails(c->slice_ctx[i], inv_table,
1059 srcRange, table, dstRange,
1060 brightness, contrast, saturation);
1061
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16937 times.
16937 if (ret < 0)
1062 parent_ret = ret;
1063 }
1064
1065 1888 return parent_ret;
1066 }
1067
1068 43298 handle_formats(c);
1069 43298 desc_dst = av_pix_fmt_desc_get(c->dstFormat);
1070 43298 desc_src = av_pix_fmt_desc_get(c->srcFormat);
1071
1072
2/2
✓ Branch 1 taken 19259 times.
✓ Branch 2 taken 24039 times.
43298 if(range_override_needed(c->dstFormat))
1073 19259 dstRange = 0;
1074
2/2
✓ Branch 1 taken 15100 times.
✓ Branch 2 taken 28198 times.
43298 if(range_override_needed(c->srcFormat))
1075 15100 srcRange = 0;
1076
1077
2/2
✓ Branch 0 taken 35796 times.
✓ Branch 1 taken 7502 times.
43298 if (c->srcRange != srcRange ||
1078
2/2
✓ Branch 0 taken 30368 times.
✓ Branch 1 taken 5428 times.
35796 c->dstRange != dstRange ||
1079
1/2
✓ Branch 0 taken 30368 times.
✗ Branch 1 not taken.
30368 c->brightness != brightness ||
1080
2/2
✓ Branch 0 taken 20617 times.
✓ Branch 1 taken 9751 times.
30368 c->contrast != contrast ||
1081
1/2
✓ Branch 0 taken 20617 times.
✗ Branch 1 not taken.
20617 c->saturation != saturation ||
1082
2/2
✓ Branch 0 taken 20472 times.
✓ Branch 1 taken 145 times.
20617 memcmp(c->srcColorspaceTable, inv_table, sizeof(int) * 4) ||
1083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20472 times.
20472 memcmp(c->dstColorspaceTable, table, sizeof(int) * 4)
1084 )
1085 22826 need_reinit = 1;
1086
1087 43298 memmove(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
1088 43298 memmove(c->dstColorspaceTable, table, sizeof(int) * 4);
1089
1090
1091
1092 43298 c->brightness = brightness;
1093 43298 c->contrast = contrast;
1094 43298 c->saturation = saturation;
1095 43298 c->srcRange = srcRange;
1096 43298 c->dstRange = dstRange;
1097
1098
2/2
✓ Branch 0 taken 22826 times.
✓ Branch 1 taken 20472 times.
43298 if (need_reinit)
1099 22826 ff_sws_init_range_convert(c);
1100
1101 43298 c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
1102 43298 c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
1103
1104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43298 times.
43298 if (c->cascaded_context[c->cascaded_mainindex])
1105 return sws_setColorspaceDetails(c->cascaded_context[c->cascaded_mainindex],inv_table, srcRange,table, dstRange, brightness, contrast, saturation);
1106
1107
2/2
✓ Branch 0 taken 20472 times.
✓ Branch 1 taken 22826 times.
43298 if (!need_reinit)
1108 20472 return 0;
1109
1110
8/8
✓ Branch 1 taken 11338 times.
✓ Branch 2 taken 11488 times.
✓ Branch 4 taken 813 times.
✓ Branch 5 taken 10525 times.
✓ Branch 7 taken 4566 times.
✓ Branch 8 taken 7735 times.
✓ Branch 10 taken 270 times.
✓ Branch 11 taken 4296 times.
22826 if ((isYUV(c->dstFormat) || isGray(c->dstFormat)) && (isYUV(c->srcFormat) || isGray(c->srcFormat))) {
1111
1/2
✓ Branch 0 taken 8005 times.
✗ Branch 1 not taken.
8005 if (!c->cascaded_context[0] &&
1112
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 7996 times.
8005 memcmp(c->dstColorspaceTable, c->srcColorspaceTable, sizeof(int) * 4) &&
1113
4/8
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
9 c->srcW && c->srcH && c->dstW && c->dstH) {
1114 enum AVPixelFormat tmp_format;
1115 int tmp_width, tmp_height;
1116 9 int srcW = c->srcW;
1117 9 int srcH = c->srcH;
1118 9 int dstW = c->dstW;
1119 9 int dstH = c->dstH;
1120 int ret;
1121 9 av_log(c, AV_LOG_VERBOSE, "YUV color matrix differs for YUV->YUV, using intermediate RGB to convert\n");
1122
1123
2/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 9 times.
9 if (isNBPS(c->dstFormat) || is16BPS(c->dstFormat)) {
1124 if (isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) {
1125 tmp_format = AV_PIX_FMT_BGRA64;
1126 } else {
1127 tmp_format = AV_PIX_FMT_BGR48;
1128 }
1129 } else {
1130
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
9 if (isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) {
1131 tmp_format = AV_PIX_FMT_BGRA;
1132 } else {
1133 9 tmp_format = AV_PIX_FMT_BGR24;
1134 }
1135 }
1136
1137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (srcW*srcH > dstW*dstH) {
1138 tmp_width = dstW;
1139 tmp_height = dstH;
1140 } else {
1141 9 tmp_width = srcW;
1142 9 tmp_height = srcH;
1143 }
1144
1145 9 ret = av_image_alloc(c->cascaded_tmp[0], c->cascaded_tmpStride[0],
1146 tmp_width, tmp_height, tmp_format, 64);
1147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
1148 return ret;
1149
1150 18 c->cascaded_context[0] = alloc_set_opts(srcW, srcH, c->srcFormat,
1151 tmp_width, tmp_height, tmp_format,
1152 9 c->flags, c->param);
1153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!c->cascaded_context[0])
1154 return -1;
1155
1156 9 sws_internal(c->cascaded_context[0])->alphablend = c->alphablend;
1157 9 ret = sws_init_context(c->cascaded_context[0], NULL , NULL);
1158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
1159 return ret;
1160 //we set both src and dst depending on that the RGB side will be ignored
1161 9 sws_setColorspaceDetails(c->cascaded_context[0], inv_table,
1162 srcRange, table, dstRange,
1163 brightness, contrast, saturation);
1164
1165 18 c->cascaded_context[1] = alloc_set_opts(tmp_width, tmp_height, tmp_format,
1166 dstW, dstH, c->dstFormat,
1167 9 c->flags, c->param);
1168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!c->cascaded_context[1])
1169 return -1;
1170 9 sws_internal(c->cascaded_context[1])->srcRange = srcRange;
1171 9 sws_internal(c->cascaded_context[1])->dstRange = dstRange;
1172 9 ret = sws_init_context(c->cascaded_context[1], NULL , NULL);
1173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
1174 return ret;
1175 9 sws_setColorspaceDetails(c->cascaded_context[1], inv_table,
1176 srcRange, table, dstRange,
1177 0, 1 << 16, 1 << 16);
1178 9 return 0;
1179 }
1180 //We do not support this combination currently, we need to cascade more contexts to compensate
1181
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7996 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7996 if (c->cascaded_context[0] && memcmp(c->dstColorspaceTable, c->srcColorspaceTable, sizeof(int) * 4))
1182 return -1; //AVERROR_PATCHWELCOME;
1183 7996 return 0;
1184 }
1185
1186
4/4
✓ Branch 1 taken 10903 times.
✓ Branch 2 taken 3918 times.
✓ Branch 4 taken 10525 times.
✓ Branch 5 taken 378 times.
14821 if (!isYUV(c->dstFormat) && !isGray(c->dstFormat)) {
1187 10525 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
1188 contrast, saturation);
1189 // FIXME factorize
1190
1191 #if ARCH_PPC
1192 ff_yuv2rgb_init_tables_ppc(c, inv_table, brightness,
1193 contrast, saturation);
1194 #endif
1195 }
1196
1197 14821 fill_rgb2yuv_table(c, table, dstRange);
1198
1199 14821 return 0;
1200 }
1201
1202 7438 int sws_getColorspaceDetails(SwsContext *sws, int **inv_table,
1203 int *srcRange, int **table, int *dstRange,
1204 int *brightness, int *contrast, int *saturation)
1205 {
1206 7438 SwsInternal *c = sws_internal(sws);
1207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7438 times.
7438 if (!c)
1208 return -1;
1209
1210
2/2
✓ Branch 0 taken 1888 times.
✓ Branch 1 taken 5550 times.
7438 if (c->nb_slice_ctx) {
1211 1888 return sws_getColorspaceDetails(c->slice_ctx[0], inv_table, srcRange,
1212 table, dstRange, brightness, contrast,
1213 saturation);
1214 }
1215
1216 5550 *inv_table = c->srcColorspaceTable;
1217 5550 *table = c->dstColorspaceTable;
1218
2/2
✓ Branch 1 taken 4531 times.
✓ Branch 2 taken 1019 times.
5550 *srcRange = range_override_needed(c->srcFormat) ? 1 : c->srcRange;
1219
2/2
✓ Branch 1 taken 3607 times.
✓ Branch 2 taken 1943 times.
5550 *dstRange = range_override_needed(c->dstFormat) ? 1 : c->dstRange;
1220 5550 *brightness = c->brightness;
1221 5550 *contrast = c->contrast;
1222 5550 *saturation = c->saturation;
1223
1224 5550 return 0;
1225 }
1226
1227 34473 SwsContext *sws_alloc_context(void)
1228 {
1229 34473 SwsInternal *c = av_mallocz(sizeof(SwsInternal));
1230
1231 av_assert0(offsetof(SwsInternal, redDither) + DITHER32_INT == offsetof(SwsInternal, dither32));
1232
1233
1/2
✓ Branch 0 taken 34473 times.
✗ Branch 1 not taken.
34473 if (c) {
1234 34473 c->av_class = &ff_sws_context_class;
1235 34473 av_opt_set_defaults(c);
1236 34473 atomic_init(&c->stride_unaligned_warned, 0);
1237 34473 atomic_init(&c->data_unaligned_warned, 0);
1238 }
1239
1240 34473 return (SwsContext *) c;
1241 }
1242
1243 static uint16_t * alloc_gamma_tbl(double e)
1244 {
1245 int i = 0;
1246 uint16_t * tbl;
1247 tbl = (uint16_t*)av_malloc(sizeof(uint16_t) * 1 << 16);
1248 if (!tbl)
1249 return NULL;
1250
1251 for (i = 0; i < 65536; ++i) {
1252 tbl[i] = pow(i / 65535.0, e) * 65535.0;
1253 }
1254 return tbl;
1255 }
1256
1257 1415 static enum AVPixelFormat alphaless_fmt(enum AVPixelFormat fmt)
1258 {
1259
19/42
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 175 times.
✓ Branch 4 taken 45 times.
✓ Branch 5 taken 364 times.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 46 times.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 1 times.
✓ Branch 16 taken 9 times.
✓ Branch 17 taken 1 times.
✓ Branch 18 taken 9 times.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 21 taken 45 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✓ Branch 39 taken 9 times.
✓ Branch 40 taken 4 times.
✓ Branch 41 taken 677 times.
1415 switch(fmt) {
1260 11 case AV_PIX_FMT_ARGB: return AV_PIX_FMT_RGB24;
1261 11 case AV_PIX_FMT_RGBA: return AV_PIX_FMT_RGB24;
1262 2 case AV_PIX_FMT_ABGR: return AV_PIX_FMT_BGR24;
1263 175 case AV_PIX_FMT_BGRA: return AV_PIX_FMT_BGR24;
1264 45 case AV_PIX_FMT_YA8: return AV_PIX_FMT_GRAY8;
1265
1266 364 case AV_PIX_FMT_YUVA420P: return AV_PIX_FMT_YUV420P;
1267 3 case AV_PIX_FMT_YUVA422P: return AV_PIX_FMT_YUV422P;
1268 case AV_PIX_FMT_YUVA444P: return AV_PIX_FMT_YUV444P;
1269
1270 46 case AV_PIX_FMT_GBRAP: return AV_PIX_FMT_GBRP;
1271
1272 1 case AV_PIX_FMT_GBRAP10LE: return AV_PIX_FMT_GBRP10;
1273 case AV_PIX_FMT_GBRAP10BE: return AV_PIX_FMT_GBRP10;
1274
1275 1 case AV_PIX_FMT_GBRAP12LE: return AV_PIX_FMT_GBRP12;
1276 case AV_PIX_FMT_GBRAP12BE: return AV_PIX_FMT_GBRP12;
1277
1278 case AV_PIX_FMT_GBRAP14LE: return AV_PIX_FMT_GBRP14;
1279 case AV_PIX_FMT_GBRAP14BE: return AV_PIX_FMT_GBRP14;
1280
1281 1 case AV_PIX_FMT_GBRAP16LE: return AV_PIX_FMT_GBRP16;
1282 9 case AV_PIX_FMT_GBRAP16BE: return AV_PIX_FMT_GBRP16;
1283
1284 1 case AV_PIX_FMT_RGBA64LE: return AV_PIX_FMT_RGB48;
1285 9 case AV_PIX_FMT_RGBA64BE: return AV_PIX_FMT_RGB48;
1286 1 case AV_PIX_FMT_BGRA64LE: return AV_PIX_FMT_BGR48;
1287 case AV_PIX_FMT_BGRA64BE: return AV_PIX_FMT_BGR48;
1288
1289 45 case AV_PIX_FMT_YA16BE: return AV_PIX_FMT_GRAY16;
1290 case AV_PIX_FMT_YA16LE: return AV_PIX_FMT_GRAY16;
1291
1292 case AV_PIX_FMT_YUVA420P9BE: return AV_PIX_FMT_YUV420P9;
1293 case AV_PIX_FMT_YUVA422P9BE: return AV_PIX_FMT_YUV422P9;
1294 case AV_PIX_FMT_YUVA444P9BE: return AV_PIX_FMT_YUV444P9;
1295 case AV_PIX_FMT_YUVA420P9LE: return AV_PIX_FMT_YUV420P9;
1296 case AV_PIX_FMT_YUVA422P9LE: return AV_PIX_FMT_YUV422P9;
1297 case AV_PIX_FMT_YUVA444P9LE: return AV_PIX_FMT_YUV444P9;
1298 case AV_PIX_FMT_YUVA420P10BE: return AV_PIX_FMT_YUV420P10;
1299 case AV_PIX_FMT_YUVA422P10BE: return AV_PIX_FMT_YUV422P10;
1300 case AV_PIX_FMT_YUVA444P10BE: return AV_PIX_FMT_YUV444P10;
1301 case AV_PIX_FMT_YUVA420P10LE: return AV_PIX_FMT_YUV420P10;
1302 case AV_PIX_FMT_YUVA422P10LE: return AV_PIX_FMT_YUV422P10;
1303 case AV_PIX_FMT_YUVA444P10LE: return AV_PIX_FMT_YUV444P10;
1304 case AV_PIX_FMT_YUVA420P16BE: return AV_PIX_FMT_YUV420P16;
1305 case AV_PIX_FMT_YUVA422P16BE: return AV_PIX_FMT_YUV422P16;
1306 case AV_PIX_FMT_YUVA444P16BE: return AV_PIX_FMT_YUV444P16;
1307 case AV_PIX_FMT_YUVA420P16LE: return AV_PIX_FMT_YUV420P16;
1308 9 case AV_PIX_FMT_YUVA422P16LE: return AV_PIX_FMT_YUV422P16;
1309 4 case AV_PIX_FMT_YUVA444P16LE: return AV_PIX_FMT_YUV444P16;
1310
1311 // case AV_PIX_FMT_AYUV64LE:
1312 // case AV_PIX_FMT_AYUV64BE:
1313 // case AV_PIX_FMT_PAL8:
1314 677 default: return AV_PIX_FMT_NONE;
1315 }
1316 }
1317
1318 22681 static av_cold int sws_init_single_context(SwsContext *sws, SwsFilter *srcFilter,
1319 SwsFilter *dstFilter)
1320 {
1321 int i;
1322 int usesVFilter, usesHFilter;
1323 int unscaled;
1324 22681 SwsInternal *c = sws_internal(sws);
1325 22681 SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
1326 22681 int srcW = c->srcW;
1327 22681 int srcH = c->srcH;
1328 22681 int dstW = c->dstW;
1329 22681 int dstH = c->dstH;
1330 22681 int dst_stride = FFALIGN(dstW * sizeof(int16_t) + 66, 16);
1331 int flags, cpu_flags;
1332 enum AVPixelFormat srcFormat, dstFormat;
1333 const AVPixFmtDescriptor *desc_src;
1334 const AVPixFmtDescriptor *desc_dst;
1335 22681 int ret = 0;
1336 enum AVPixelFormat tmpFmt;
1337 static const float float_mult = 1.0f / 255.0f;
1338
1339 22681 cpu_flags = av_get_cpu_flags();
1340 22681 flags = c->flags;
1341 22681 emms_c();
1342
1343
4/4
✓ Branch 0 taken 21858 times.
✓ Branch 1 taken 823 times.
✓ Branch 2 taken 21768 times.
✓ Branch 3 taken 90 times.
22681 unscaled = (srcW == dstW && srcH == dstH);
1344
1345
3/6
✓ Branch 0 taken 22681 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22681 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22681 times.
✗ Branch 5 not taken.
22681 if (!c->contrast && !c->saturation && !c->dstFormatBpp)
1346 22681 sws_setColorspaceDetails(sws, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
1347 ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
1348 c->dstRange, 0, 1 << 16, 1 << 16);
1349
1350 22681 handle_formats(c);
1351 22681 srcFormat = c->srcFormat;
1352 22681 dstFormat = c->dstFormat;
1353 22681 desc_src = av_pix_fmt_desc_get(srcFormat);
1354 22681 desc_dst = av_pix_fmt_desc_get(dstFormat);
1355
1356 // If the source has no alpha then disable alpha blendaway
1357
2/2
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 22547 times.
22681 if (c->src0Alpha)
1358 134 c->alphablend = SWS_ALPHA_BLEND_NONE;
1359
1360
5/6
✓ Branch 0 taken 21768 times.
✓ Branch 1 taken 913 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 21756 times.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
22693 if (!(unscaled && sws_isSupportedEndiannessConversion(srcFormat) &&
1361 12 av_pix_fmt_swap_endianness(srcFormat) == dstFormat)) {
1362
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22681 times.
22681 if (!sws_isSupportedInput(srcFormat)) {
1363 av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
1364 av_get_pix_fmt_name(srcFormat));
1365 return AVERROR(EINVAL);
1366 }
1367
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22681 times.
22681 if (!sws_isSupportedOutput(dstFormat)) {
1368 av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
1369 av_get_pix_fmt_name(dstFormat));
1370 return AVERROR(EINVAL);
1371 }
1372 }
1373 av_assert2(desc_src && desc_dst);
1374
1375 22681 i = flags & (SWS_POINT |
1376 SWS_AREA |
1377 SWS_BILINEAR |
1378 SWS_FAST_BILINEAR |
1379 SWS_BICUBIC |
1380 SWS_X |
1381 SWS_GAUSS |
1382 SWS_LANCZOS |
1383 SWS_SINC |
1384 SWS_SPLINE |
1385 SWS_BICUBLIN);
1386
1387 /* provide a default scaler if not set by caller */
1388
2/2
✓ Branch 0 taken 1844 times.
✓ Branch 1 taken 20837 times.
22681 if (!i) {
1389
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1842 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
1844 if (dstW < srcW && dstH < srcH)
1390 flags |= SWS_BICUBIC;
1391
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1844 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1844 else if (dstW > srcW && dstH > srcH)
1392 flags |= SWS_BICUBIC;
1393 else
1394 1844 flags |= SWS_BICUBIC;
1395 1844 c->flags = flags;
1396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20837 times.
20837 } else if (i & (i - 1)) {
1397 av_log(c, AV_LOG_ERROR,
1398 "Exactly one scaler algorithm must be chosen, got %X\n", i);
1399 return AVERROR(EINVAL);
1400 }
1401 /* sanity check */
1402
4/8
✓ Branch 0 taken 22681 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22681 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22681 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 22681 times.
22681 if (srcW < 1 || srcH < 1 || dstW < 1 || dstH < 1) {
1403 /* FIXME check if these are enough and try to lower them after
1404 * fixing the relevant parts of the code */
1405 av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
1406 srcW, srcH, dstW, dstH);
1407 return AVERROR(EINVAL);
1408 }
1409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22681 times.
22681 if (flags & SWS_FAST_BILINEAR) {
1410 if (srcW < 8 || dstW < 8) {
1411 flags ^= SWS_FAST_BILINEAR | SWS_BILINEAR;
1412 c->flags = flags;
1413 }
1414 }
1415
1416
1/2
✓ Branch 0 taken 22681 times.
✗ Branch 1 not taken.
22681 if (!dstFilter)
1417 22681 dstFilter = &dummyFilter;
1418
1/2
✓ Branch 0 taken 22681 times.
✗ Branch 1 not taken.
22681 if (!srcFilter)
1419 22681 srcFilter = &dummyFilter;
1420
1421 22681 c->lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
1422 22681 c->lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
1423 22681 c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
1424 22681 c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
1425 22681 c->vRounder = 4 * 0x0001000100010001ULL;
1426
1427 usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
1428
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 22681 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
22681 (srcFilter->chrV && srcFilter->chrV->length > 1) ||
1429
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 22681 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22681 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
68043 (dstFilter->lumV && dstFilter->lumV->length > 1) ||
1430
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 22681 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
22681 (dstFilter->chrV && dstFilter->chrV->length > 1);
1431 usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
1432
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 22681 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
22681 (srcFilter->chrH && srcFilter->chrH->length > 1) ||
1433
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 22681 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22681 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
68043 (dstFilter->lumH && dstFilter->lumH->length > 1) ||
1434
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 22681 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
22681 (dstFilter->chrH && dstFilter->chrH->length > 1);
1435
1436 22681 av_pix_fmt_get_chroma_sub_sample(srcFormat, &c->chrSrcHSubSample, &c->chrSrcVSubSample);
1437 22681 av_pix_fmt_get_chroma_sub_sample(dstFormat, &c->chrDstHSubSample, &c->chrDstVSubSample);
1438
1439 22681 c->dst_slice_align = 1 << c->chrDstVSubSample;
1440
1441
4/4
✓ Branch 1 taken 10515 times.
✓ Branch 2 taken 12166 times.
✓ Branch 3 taken 10503 times.
✓ Branch 4 taken 12 times.
22681 if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) {
1442
2/2
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 10242 times.
10503 if (dstW&1) {
1443 261 av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to odd output size\n");
1444 261 flags |= SWS_FULL_CHR_H_INT;
1445 261 c->flags = flags;
1446 }
1447
1448
2/2
✓ Branch 0 taken 5715 times.
✓ Branch 1 taken 4788 times.
10503 if ( c->chrSrcHSubSample == 0
1449
2/2
✓ Branch 0 taken 5445 times.
✓ Branch 1 taken 270 times.
5715 && c->chrSrcVSubSample == 0
1450
1/2
✓ Branch 0 taken 5445 times.
✗ Branch 1 not taken.
5445 && c->dither != SWS_DITHER_BAYER //SWS_FULL_CHR_H_INT is currently not supported with SWS_DITHER_BAYER
1451
1/2
✓ Branch 0 taken 5445 times.
✗ Branch 1 not taken.
5445 && !(c->flags & SWS_FAST_BILINEAR)
1452 ) {
1453 5445 av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to input having non subsampled chroma\n");
1454 5445 flags |= SWS_FULL_CHR_H_INT;
1455 5445 c->flags = flags;
1456 }
1457 }
1458
1459
2/2
✓ Branch 0 taken 15850 times.
✓ Branch 1 taken 6831 times.
22681 if (c->dither == SWS_DITHER_AUTO) {
1460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15850 times.
15850 if (flags & SWS_ERROR_DIFFUSION)
1461 c->dither = SWS_DITHER_ED;
1462 }
1463
1464
4/4
✓ Branch 0 taken 22667 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 22652 times.
✓ Branch 3 taken 15 times.
22681 if(dstFormat == AV_PIX_FMT_BGR4_BYTE ||
1465
2/2
✓ Branch 0 taken 22621 times.
✓ Branch 1 taken 31 times.
22652 dstFormat == AV_PIX_FMT_RGB4_BYTE ||
1466
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 22604 times.
22621 dstFormat == AV_PIX_FMT_BGR8 ||
1467 dstFormat == AV_PIX_FMT_RGB8) {
1468
1/2
✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
77 if (c->dither == SWS_DITHER_AUTO)
1469
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 66 times.
77 c->dither = (flags & SWS_FULL_CHR_H_INT) ? SWS_DITHER_ED : SWS_DITHER_BAYER;
1470
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 11 times.
77 if (!(flags & SWS_FULL_CHR_H_INT)) {
1471
4/8
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 66 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 66 times.
66 if (c->dither == SWS_DITHER_ED || c->dither == SWS_DITHER_A_DITHER || c->dither == SWS_DITHER_X_DITHER || c->dither == SWS_DITHER_NONE) {
1472 av_log(c, AV_LOG_DEBUG,
1473 "Desired dithering only supported in full chroma interpolation for destination format '%s'\n",
1474 av_get_pix_fmt_name(dstFormat));
1475 flags |= SWS_FULL_CHR_H_INT;
1476 c->flags = flags;
1477 }
1478 }
1479
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 66 times.
77 if (flags & SWS_FULL_CHR_H_INT) {
1480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (c->dither == SWS_DITHER_BAYER) {
1481 av_log(c, AV_LOG_DEBUG,
1482 "Ordered dither is not supported in full chroma interpolation for destination format '%s'\n",
1483 av_get_pix_fmt_name(dstFormat));
1484 c->dither = SWS_DITHER_ED;
1485 }
1486 }
1487 }
1488
2/2
✓ Branch 1 taken 3867 times.
✓ Branch 2 taken 18814 times.
22681 if (isPlanarRGB(dstFormat)) {
1489
2/2
✓ Branch 0 taken 1833 times.
✓ Branch 1 taken 2034 times.
3867 if (!(flags & SWS_FULL_CHR_H_INT)) {
1490 1833 av_log(c, AV_LOG_DEBUG,
1491 "%s output is not supported with half chroma resolution, switching to full\n",
1492 av_get_pix_fmt_name(dstFormat));
1493 1833 flags |= SWS_FULL_CHR_H_INT;
1494 1833 c->flags = flags;
1495 }
1496 }
1497
1498 /* reuse chroma for 2 pixels RGB/BGR unless user wants full
1499 * chroma interpolation */
1500
3/4
✓ Branch 0 taken 7294 times.
✓ Branch 1 taken 15387 times.
✓ Branch 2 taken 7294 times.
✗ Branch 3 not taken.
29975 if (flags & SWS_FULL_CHR_H_INT &&
1501
2/2
✓ Branch 1 taken 3427 times.
✓ Branch 2 taken 3867 times.
14588 isAnyRGB(dstFormat) &&
1502
2/2
✓ Branch 1 taken 3425 times.
✓ Branch 2 taken 2 times.
10721 !isPlanarRGB(dstFormat) &&
1503
2/2
✓ Branch 0 taken 3424 times.
✓ Branch 1 taken 1 times.
3425 dstFormat != AV_PIX_FMT_RGBA64LE &&
1504
2/2
✓ Branch 0 taken 3422 times.
✓ Branch 1 taken 2 times.
3424 dstFormat != AV_PIX_FMT_RGBA64BE &&
1505
2/2
✓ Branch 0 taken 3421 times.
✓ Branch 1 taken 1 times.
3422 dstFormat != AV_PIX_FMT_BGRA64LE &&
1506
2/2
✓ Branch 0 taken 2841 times.
✓ Branch 1 taken 580 times.
3421 dstFormat != AV_PIX_FMT_BGRA64BE &&
1507
2/2
✓ Branch 0 taken 2838 times.
✓ Branch 1 taken 3 times.
2841 dstFormat != AV_PIX_FMT_RGB48LE &&
1508
2/2
✓ Branch 0 taken 2836 times.
✓ Branch 1 taken 2 times.
2838 dstFormat != AV_PIX_FMT_RGB48BE &&
1509
2/2
✓ Branch 0 taken 2835 times.
✓ Branch 1 taken 1 times.
2836 dstFormat != AV_PIX_FMT_BGR48LE &&
1510
2/2
✓ Branch 0 taken 2786 times.
✓ Branch 1 taken 49 times.
2835 dstFormat != AV_PIX_FMT_BGR48BE &&
1511
2/2
✓ Branch 0 taken 2773 times.
✓ Branch 1 taken 13 times.
2786 dstFormat != AV_PIX_FMT_RGBA &&
1512
2/2
✓ Branch 0 taken 2193 times.
✓ Branch 1 taken 580 times.
2773 dstFormat != AV_PIX_FMT_ARGB &&
1513
2/2
✓ Branch 0 taken 2189 times.
✓ Branch 1 taken 4 times.
2193 dstFormat != AV_PIX_FMT_BGRA &&
1514
2/2
✓ Branch 0 taken 561 times.
✓ Branch 1 taken 1628 times.
2189 dstFormat != AV_PIX_FMT_ABGR &&
1515
2/2
✓ Branch 0 taken 459 times.
✓ Branch 1 taken 102 times.
561 dstFormat != AV_PIX_FMT_RGB24 &&
1516
2/2
✓ Branch 0 taken 457 times.
✓ Branch 1 taken 2 times.
459 dstFormat != AV_PIX_FMT_BGR24 &&
1517
2/2
✓ Branch 0 taken 455 times.
✓ Branch 1 taken 2 times.
457 dstFormat != AV_PIX_FMT_BGR4_BYTE &&
1518
2/2
✓ Branch 0 taken 451 times.
✓ Branch 1 taken 4 times.
455 dstFormat != AV_PIX_FMT_RGB4_BYTE &&
1519
2/2
✓ Branch 0 taken 448 times.
✓ Branch 1 taken 3 times.
451 dstFormat != AV_PIX_FMT_BGR8 &&
1520
2/2
✓ Branch 0 taken 375 times.
✓ Branch 1 taken 73 times.
448 dstFormat != AV_PIX_FMT_RGB8 &&
1521
2/2
✓ Branch 0 taken 302 times.
✓ Branch 1 taken 73 times.
375 dstFormat != AV_PIX_FMT_X2RGB10LE &&
1522 dstFormat != AV_PIX_FMT_X2BGR10LE
1523 ) {
1524 302 av_log(c, AV_LOG_WARNING,
1525 "full chroma interpolation for destination format '%s' not yet implemented\n",
1526 av_get_pix_fmt_name(dstFormat));
1527 302 flags &= ~SWS_FULL_CHR_H_INT;
1528 302 c->flags = flags;
1529 }
1530
4/4
✓ Branch 1 taken 10515 times.
✓ Branch 2 taken 12166 times.
✓ Branch 3 taken 3523 times.
✓ Branch 4 taken 6992 times.
22681 if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
1531 3523 c->chrDstHSubSample = 1;
1532
1533 // drop some chroma lines if the user wants it
1534 22681 c->vChrDrop = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
1535 SWS_SRC_V_CHR_DROP_SHIFT;
1536 22681 c->chrSrcVSubSample += c->vChrDrop;
1537
1538 /* drop every other pixel for chroma calculation unless user
1539 * wants full chroma */
1540
5/6
✓ Branch 1 taken 6722 times.
✓ Branch 2 taken 15959 times.
✓ Branch 3 taken 6722 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6721 times.
✓ Branch 6 taken 1 times.
22681 if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP) &&
1541
3/4
✓ Branch 0 taken 6720 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6720 times.
✗ Branch 3 not taken.
6721 srcFormat != AV_PIX_FMT_RGB8 && srcFormat != AV_PIX_FMT_BGR8 &&
1542
3/4
✓ Branch 0 taken 6720 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6719 times.
✓ Branch 3 taken 1 times.
6720 srcFormat != AV_PIX_FMT_RGB4 && srcFormat != AV_PIX_FMT_BGR4 &&
1543
4/4
✓ Branch 0 taken 6718 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6717 times.
✓ Branch 3 taken 1 times.
6719 srcFormat != AV_PIX_FMT_RGB4_BYTE && srcFormat != AV_PIX_FMT_BGR4_BYTE &&
1544
4/4
✓ Branch 0 taken 6715 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 6642 times.
✓ Branch 3 taken 73 times.
6717 srcFormat != AV_PIX_FMT_GBRP9BE && srcFormat != AV_PIX_FMT_GBRP9LE &&
1545
4/4
✓ Branch 0 taken 5753 times.
✓ Branch 1 taken 889 times.
✓ Branch 2 taken 5752 times.
✓ Branch 3 taken 1 times.
6642 srcFormat != AV_PIX_FMT_GBRP10BE && srcFormat != AV_PIX_FMT_GBRP10LE &&
1546
4/4
✓ Branch 0 taken 5750 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 5677 times.
✓ Branch 3 taken 73 times.
5752 srcFormat != AV_PIX_FMT_GBRAP10BE && srcFormat != AV_PIX_FMT_GBRAP10LE &&
1547
4/4
✓ Branch 0 taken 4802 times.
✓ Branch 1 taken 875 times.
✓ Branch 2 taken 4801 times.
✓ Branch 3 taken 1 times.
5677 srcFormat != AV_PIX_FMT_GBRP12BE && srcFormat != AV_PIX_FMT_GBRP12LE &&
1548
4/4
✓ Branch 0 taken 4799 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4798 times.
✓ Branch 3 taken 1 times.
4801 srcFormat != AV_PIX_FMT_GBRAP12BE && srcFormat != AV_PIX_FMT_GBRAP12LE &&
1549
4/4
✓ Branch 0 taken 4797 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4796 times.
✓ Branch 3 taken 1 times.
4798 srcFormat != AV_PIX_FMT_GBRAP14BE && srcFormat != AV_PIX_FMT_GBRAP14LE &&
1550
4/4
✓ Branch 0 taken 4794 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4694 times.
✓ Branch 3 taken 100 times.
4796 srcFormat != AV_PIX_FMT_GBRP14BE && srcFormat != AV_PIX_FMT_GBRP14LE &&
1551
4/4
✓ Branch 0 taken 4434 times.
✓ Branch 1 taken 260 times.
✓ Branch 2 taken 4424 times.
✓ Branch 3 taken 10 times.
4694 srcFormat != AV_PIX_FMT_GBRP16BE && srcFormat != AV_PIX_FMT_GBRP16LE &&
1552
4/4
✓ Branch 0 taken 4420 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4419 times.
✓ Branch 3 taken 1 times.
4424 srcFormat != AV_PIX_FMT_GBRAP16BE && srcFormat != AV_PIX_FMT_GBRAP16LE &&
1553
4/4
✓ Branch 0 taken 4387 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 4386 times.
✓ Branch 3 taken 1 times.
4419 srcFormat != AV_PIX_FMT_GBRPF32BE && srcFormat != AV_PIX_FMT_GBRPF32LE &&
1554
2/2
✓ Branch 0 taken 4385 times.
✓ Branch 1 taken 1 times.
4386 srcFormat != AV_PIX_FMT_GBRAPF32BE && srcFormat != AV_PIX_FMT_GBRAPF32LE &&
1555
2/2
✓ Branch 0 taken 3014 times.
✓ Branch 1 taken 1371 times.
4385 ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
1556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3014 times.
3014 (flags & SWS_FAST_BILINEAR)))
1557 1371 c->chrSrcHSubSample = 1;
1558
1559 // Note the AV_CEIL_RSHIFT is so that we always round toward +inf.
1560 22681 c->chrSrcW = AV_CEIL_RSHIFT(srcW, c->chrSrcHSubSample);
1561 22681 c->chrSrcH = AV_CEIL_RSHIFT(srcH, c->chrSrcVSubSample);
1562 22681 c->chrDstW = AV_CEIL_RSHIFT(dstW, c->chrDstHSubSample);
1563 22681 c->chrDstH = AV_CEIL_RSHIFT(dstH, c->chrDstVSubSample);
1564
1565
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22681 times.
22681 if (!FF_ALLOCZ_TYPED_ARRAY(c->formatConvBuffer, FFALIGN(srcW * 2 + 78, 16) * 2))
1566 goto nomem;
1567
1568 22681 c->srcBpc = desc_src->comp[0].depth;
1569
2/2
✓ Branch 0 taken 468 times.
✓ Branch 1 taken 22213 times.
22681 if (c->srcBpc < 8)
1570 468 c->srcBpc = 8;
1571 22681 c->dstBpc = desc_dst->comp[0].depth;
1572
2/2
✓ Branch 0 taken 1210 times.
✓ Branch 1 taken 21471 times.
22681 if (c->dstBpc < 8)
1573 1210 c->dstBpc = 8;
1574
4/4
✓ Branch 1 taken 15959 times.
✓ Branch 2 taken 6722 times.
✓ Branch 3 taken 862 times.
✓ Branch 4 taken 15097 times.
22681 if (isAnyRGB(srcFormat) || srcFormat == AV_PIX_FMT_PAL8)
1575 7584 c->srcBpc = 16;
1576
2/2
✓ Branch 0 taken 3710 times.
✓ Branch 1 taken 18971 times.
22681 if (c->dstBpc == 16)
1577 3710 dst_stride <<= 1;
1578
1579
6/6
✓ Branch 0 taken 1774 times.
✓ Branch 1 taken 20907 times.
✓ Branch 2 taken 1684 times.
✓ Branch 3 taken 90 times.
✓ Branch 4 taken 1683 times.
✓ Branch 5 taken 1 times.
22681 if (INLINE_MMXEXT(cpu_flags) && c->srcBpc == 8 && c->dstBpc <= 14) {
1580
2/2
✓ Branch 0 taken 770 times.
✓ Branch 1 taken 913 times.
1683 c->canMMXEXTBeUsed = dstW >= srcW && (dstW & 31) == 0 &&
1581
3/4
✓ Branch 0 taken 1683 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 726 times.
✓ Branch 3 taken 44 times.
4092 c->chrDstW >= c->chrSrcW &&
1582
1/2
✓ Branch 0 taken 726 times.
✗ Branch 1 not taken.
726 (srcW & 15) == 0;
1583
7/8
✓ Branch 0 taken 957 times.
✓ Branch 1 taken 726 times.
✓ Branch 2 taken 957 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 869 times.
✓ Branch 5 taken 88 times.
✓ Branch 6 taken 143 times.
✓ Branch 7 taken 726 times.
1683 if (!c->canMMXEXTBeUsed && dstW >= srcW && c->chrDstW >= c->chrSrcW && (srcW & 15) == 0
1584
1585
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143 times.
143 && (flags & SWS_FAST_BILINEAR)) {
1586 if (flags & SWS_PRINT_INFO)
1587 av_log(c, AV_LOG_INFO,
1588 "output width is not a multiple of 32 -> no MMXEXT scaler\n");
1589 }
1590
4/8
✓ Branch 0 taken 1683 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1683 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1683 times.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1683 times.
1683 if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat) || isAnyRGB(c->srcFormat))
1591 c->canMMXEXTBeUsed = 0;
1592 } else
1593 20998 c->canMMXEXTBeUsed = 0;
1594
1595 22681 c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
1596 22681 c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
1597
1598 /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
1599 * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
1600 * correct scaling.
1601 * n-2 is the last chrominance sample available.
1602 * This is not perfect, but no one should notice the difference, the more
1603 * correct variant would be like the vertical one, but that would require
1604 * some special code for the first and last pixel */
1605
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22681 times.
22681 if (flags & SWS_FAST_BILINEAR) {
1606 if (c->canMMXEXTBeUsed) {
1607 c->lumXInc += 20;
1608 c->chrXInc += 20;
1609 }
1610 // we don't use the x86 asm scaler if MMX is available
1611 else if (INLINE_MMX(cpu_flags) && c->dstBpc <= 14) {
1612 c->lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20;
1613 c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
1614 }
1615 }
1616
1617 // hardcoded for now
1618 22681 c->gamma_value = 2.2;
1619 22681 tmpFmt = AV_PIX_FMT_RGBA64LE;
1620
1621
1622
3/8
✓ Branch 0 taken 913 times.
✓ Branch 1 taken 21768 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 913 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
22681 if (!unscaled && c->gamma_flag && (srcFormat != tmpFmt || dstFormat != tmpFmt)) {
1623 SwsInternal *c2;
1624 c->cascaded_context[0] = NULL;
1625
1626 ret = av_image_alloc(c->cascaded_tmp[0], c->cascaded_tmpStride[0],
1627 srcW, srcH, tmpFmt, 64);
1628 if (ret < 0)
1629 return ret;
1630
1631 c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
1632 srcW, srcH, tmpFmt,
1633 flags, NULL, NULL, c->param);
1634 if (!c->cascaded_context[0]) {
1635 return AVERROR(ENOMEM);
1636 }
1637
1638 c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFmt,
1639 dstW, dstH, tmpFmt,
1640 flags, srcFilter, dstFilter, c->param);
1641
1642 if (!c->cascaded_context[1])
1643 return AVERROR(ENOMEM);
1644
1645 c2 = sws_internal(c->cascaded_context[1]);
1646 c2->is_internal_gamma = 1;
1647 c2->gamma = alloc_gamma_tbl( c->gamma_value);
1648 c2->inv_gamma = alloc_gamma_tbl(1.f/c->gamma_value);
1649 if (!c2->gamma || !c2->inv_gamma)
1650 return AVERROR(ENOMEM);
1651
1652 // is_internal_flag is set after creating the context
1653 // to properly create the gamma convert FilterDescriptor
1654 // we have to re-initialize it
1655 ff_free_filters(c2);
1656 if ((ret = ff_init_filters(c2)) < 0) {
1657 sws_freeContext(c->cascaded_context[1]);
1658 c->cascaded_context[1] = NULL;
1659 return ret;
1660 }
1661
1662 c->cascaded_context[2] = NULL;
1663 if (dstFormat != tmpFmt) {
1664 ret = av_image_alloc(c->cascaded_tmp[1], c->cascaded_tmpStride[1],
1665 dstW, dstH, tmpFmt, 64);
1666 if (ret < 0)
1667 return ret;
1668
1669 c->cascaded_context[2] = sws_getContext(dstW, dstH, tmpFmt,
1670 dstW, dstH, dstFormat,
1671 flags, NULL, NULL, c->param);
1672 if (!c->cascaded_context[2])
1673 return AVERROR(ENOMEM);
1674 }
1675 return 0;
1676 }
1677
1678
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22681 times.
22681 if (isBayer(srcFormat)) {
1679 if (!unscaled ||
1680 (dstFormat != AV_PIX_FMT_RGB24 && dstFormat != AV_PIX_FMT_YUV420P &&
1681 dstFormat != AV_PIX_FMT_RGB48)) {
1682 enum AVPixelFormat tmpFormat = isBayer16BPS(srcFormat) ? AV_PIX_FMT_RGB48 : AV_PIX_FMT_RGB24;
1683
1684 ret = av_image_alloc(c->cascaded_tmp[0], c->cascaded_tmpStride[0],
1685 srcW, srcH, tmpFormat, 64);
1686 if (ret < 0)
1687 return ret;
1688
1689 c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
1690 srcW, srcH, tmpFormat,
1691 flags, srcFilter, NULL, c->param);
1692 if (!c->cascaded_context[0])
1693 return AVERROR(ENOMEM);
1694
1695 c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFormat,
1696 dstW, dstH, dstFormat,
1697 flags, NULL, dstFilter, c->param);
1698 if (!c->cascaded_context[1])
1699 return AVERROR(ENOMEM);
1700 return 0;
1701 }
1702 }
1703
1704
6/6
✓ Branch 0 taken 21768 times.
✓ Branch 1 taken 913 times.
✓ Branch 2 taken 7969 times.
✓ Branch 3 taken 13799 times.
✓ Branch 4 taken 17 times.
✓ Branch 5 taken 7952 times.
22681 if (unscaled && c->srcBpc == 8 && dstFormat == AV_PIX_FMT_GRAYF32){
1705
2/2
✓ Branch 0 taken 4352 times.
✓ Branch 1 taken 17 times.
4369 for (i = 0; i < 256; ++i){
1706 4352 c->uint2float_lut[i] = (float)i * float_mult;
1707 }
1708 }
1709
1710 // float will be converted to uint16_t
1711
6/6
✓ Branch 0 taken 22680 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 22678 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 2 times.
22681 if ((srcFormat == AV_PIX_FMT_GRAYF32BE || srcFormat == AV_PIX_FMT_GRAYF32LE) &&
1712
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
1 (!unscaled || unscaled && dstFormat != srcFormat && (srcFormat != AV_PIX_FMT_GRAYF32 ||
1713 dstFormat != AV_PIX_FMT_GRAY8))){
1714 3 c->srcBpc = 16;
1715 }
1716
1717
4/4
✓ Branch 1 taken 2276 times.
✓ Branch 2 taken 20405 times.
✓ Branch 4 taken 1415 times.
✓ Branch 5 taken 861 times.
22681 if (CONFIG_SWSCALE_ALPHA && isALPHA(srcFormat) && !isALPHA(dstFormat)) {
1718 1415 enum AVPixelFormat tmpFormat = alphaless_fmt(srcFormat);
1719
1720
3/4
✓ Branch 0 taken 738 times.
✓ Branch 1 taken 677 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 738 times.
1415 if (tmpFormat != AV_PIX_FMT_NONE && c->alphablend != SWS_ALPHA_BLEND_NONE) {
1721 if (!unscaled ||
1722 dstFormat != tmpFormat ||
1723 usesHFilter || usesVFilter ||
1724 c->srcRange != c->dstRange
1725 ) {
1726 c->cascaded_mainindex = 1;
1727 ret = av_image_alloc(c->cascaded_tmp[0], c->cascaded_tmpStride[0],
1728 srcW, srcH, tmpFormat, 64);
1729 if (ret < 0)
1730 return ret;
1731
1732 c->cascaded_context[0] = alloc_set_opts(srcW, srcH, srcFormat,
1733 srcW, srcH, tmpFormat,
1734 flags, c->param);
1735 if (!c->cascaded_context[0])
1736 return AVERROR(EINVAL);
1737 sws_internal(c->cascaded_context[0])->alphablend = c->alphablend;
1738 ret = sws_init_context(c->cascaded_context[0], NULL , NULL);
1739 if (ret < 0)
1740 return ret;
1741
1742 c->cascaded_context[1] = alloc_set_opts(srcW, srcH, tmpFormat,
1743 dstW, dstH, dstFormat,
1744 flags, c->param);
1745 if (!c->cascaded_context[1])
1746 return AVERROR(EINVAL);
1747
1748 sws_internal(c->cascaded_context[1])->srcRange = c->srcRange;
1749 sws_internal(c->cascaded_context[1])->dstRange = c->dstRange;
1750 ret = sws_init_context(c->cascaded_context[1], srcFilter , dstFilter);
1751 if (ret < 0)
1752 return ret;
1753
1754 return 0;
1755 }
1756 }
1757 }
1758
1759 /* alpha blend special case, note this has been split via cascaded contexts if its scaled */
1760
4/6
✓ Branch 0 taken 21768 times.
✓ Branch 1 taken 913 times.
✓ Branch 2 taken 21768 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 21768 times.
✗ Branch 5 not taken.
22681 if (unscaled && !usesHFilter && !usesVFilter &&
1761
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 21768 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
21768 c->alphablend != SWS_ALPHA_BLEND_NONE &&
1762 isALPHA(srcFormat) &&
1763 (c->srcRange == c->dstRange || isAnyRGB(dstFormat)) &&
1764 alphaless_fmt(srcFormat) == dstFormat
1765 ) {
1766 c->convert_unscaled = ff_sws_alphablendaway;
1767
1768 if (flags & SWS_PRINT_INFO)
1769 av_log(c, AV_LOG_INFO,
1770 "using alpha blendaway %s -> %s special converter\n",
1771 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1772 return 0;
1773 }
1774
1775 /* unscaled special cases */
1776
4/6
✓ Branch 0 taken 21768 times.
✓ Branch 1 taken 913 times.
✓ Branch 2 taken 21768 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 21768 times.
✗ Branch 5 not taken.
22681 if (unscaled && !usesHFilter && !usesVFilter &&
1777
5/6
✓ Branch 0 taken 2345 times.
✓ Branch 1 taken 19423 times.
✓ Branch 3 taken 1645 times.
✓ Branch 4 taken 700 times.
✓ Branch 5 taken 1645 times.
✗ Branch 6 not taken.
23413 (c->srcRange == c->dstRange || isAnyRGB(dstFormat) ||
1778
2/4
✓ Branch 2 taken 1645 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1645 times.
3290 isFloat(srcFormat) || isFloat(dstFormat) || isBayer(srcFormat))){
1779
1780 20123 ff_get_unscaled_swscale(c);
1781
1782
2/2
✓ Branch 0 taken 4910 times.
✓ Branch 1 taken 15213 times.
20123 if (c->convert_unscaled) {
1783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4910 times.
4910 if (flags & SWS_PRINT_INFO)
1784 av_log(c, AV_LOG_INFO,
1785 "using unscaled %s -> %s special converter\n",
1786 av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1787 4910 return 0;
1788 }
1789 }
1790
1791 #if HAVE_MMAP && HAVE_MPROTECT && defined(MAP_ANONYMOUS)
1792 #define USE_MMAP 1
1793 #else
1794 #define USE_MMAP 0
1795 #endif
1796
1797 /* precalculate horizontal scaler filter coefficients */
1798 {
1799 #if HAVE_MMXEXT_INLINE
1800 // can't downscale !!!
1801
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
17771 if (c->canMMXEXTBeUsed && (flags & SWS_FAST_BILINEAR)) {
1802 c->lumMmxextFilterCodeSize = ff_init_hscaler_mmxext(dstW, c->lumXInc, NULL,
1803 NULL, NULL, 8);
1804 c->chrMmxextFilterCodeSize = ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc,
1805 NULL, NULL, NULL, 4);
1806
1807 #if USE_MMAP
1808 c->lumMmxextFilterCode = mmap(NULL, c->lumMmxextFilterCodeSize,
1809 PROT_READ | PROT_WRITE,
1810 MAP_PRIVATE | MAP_ANONYMOUS,
1811 -1, 0);
1812 c->chrMmxextFilterCode = mmap(NULL, c->chrMmxextFilterCodeSize,
1813 PROT_READ | PROT_WRITE,
1814 MAP_PRIVATE | MAP_ANONYMOUS,
1815 -1, 0);
1816 #elif HAVE_VIRTUALALLOC
1817 c->lumMmxextFilterCode = VirtualAlloc(NULL,
1818 c->lumMmxextFilterCodeSize,
1819 MEM_COMMIT,
1820 PAGE_EXECUTE_READWRITE);
1821 c->chrMmxextFilterCode = VirtualAlloc(NULL,
1822 c->chrMmxextFilterCodeSize,
1823 MEM_COMMIT,
1824 PAGE_EXECUTE_READWRITE);
1825 #else
1826 c->lumMmxextFilterCode = av_malloc(c->lumMmxextFilterCodeSize);
1827 c->chrMmxextFilterCode = av_malloc(c->chrMmxextFilterCodeSize);
1828 #endif
1829
1830 #ifdef MAP_ANONYMOUS
1831 if (c->lumMmxextFilterCode == MAP_FAILED || c->chrMmxextFilterCode == MAP_FAILED)
1832 #else
1833 if (!c->lumMmxextFilterCode || !c->chrMmxextFilterCode)
1834 #endif
1835 {
1836 av_log(c, AV_LOG_ERROR, "Failed to allocate MMX2FilterCode\n");
1837 return AVERROR(ENOMEM);
1838 }
1839
1840 if (!FF_ALLOCZ_TYPED_ARRAY(c->hLumFilter, dstW / 8 + 8) ||
1841 !FF_ALLOCZ_TYPED_ARRAY(c->hChrFilter, c->chrDstW / 4 + 8) ||
1842 !FF_ALLOCZ_TYPED_ARRAY(c->hLumFilterPos, dstW / 2 / 8 + 8) ||
1843 !FF_ALLOCZ_TYPED_ARRAY(c->hChrFilterPos, c->chrDstW / 2 / 4 + 8))
1844 goto nomem;
1845
1846 ff_init_hscaler_mmxext( dstW, c->lumXInc, c->lumMmxextFilterCode,
1847 c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
1848 ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc, c->chrMmxextFilterCode,
1849 c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4);
1850
1851 #if USE_MMAP
1852 if ( mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1
1853 || mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1) {
1854 av_log(c, AV_LOG_ERROR, "mprotect failed, cannot use fast bilinear scaler\n");
1855 ret = AVERROR(EINVAL);
1856 goto fail;
1857 }
1858 #endif
1859 } else
1860 #endif /* HAVE_MMXEXT_INLINE */
1861 {
1862
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 17697 times.
17771 const int filterAlign = X86_MMX(cpu_flags) ? 4 :
1863 PPC_ALTIVEC(cpu_flags) ? 8 :
1864 have_neon(cpu_flags) ? 4 :
1865 have_lsx(cpu_flags) ? 8 :
1866 have_lasx(cpu_flags) ? 8 : 1;
1867
1868
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 17771 times.
35542 if ((ret = initFilter(&c->hLumFilter, &c->hLumFilterPos,
1869 &c->hLumFilterSize, c->lumXInc,
1870 srcW, dstW, filterAlign, 1 << 14,
1871 17771 (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1872 cpu_flags, srcFilter->lumH, dstFilter->lumH,
1873
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 c->param,
1874 get_local_pos(c, 0, 0, 0),
1875 get_local_pos(c, 0, 0, 0))) < 0)
1876 goto fail;
1877
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17771 times.
17771 if (ff_shuffle_filter_coefficients(c, c->hLumFilterPos, c->hLumFilterSize, c->hLumFilter, dstW) < 0)
1878 goto nomem;
1879
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 17771 times.
35542 if ((ret = initFilter(&c->hChrFilter, &c->hChrFilterPos,
1880 &c->hChrFilterSize, c->chrXInc,
1881 c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
1882 17771 (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1883 cpu_flags, srcFilter->chrH, dstFilter->chrH,
1884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 c->param,
1885 get_local_pos(c, c->chrSrcHSubSample, c->src_h_chr_pos, 0),
1886 get_local_pos(c, c->chrDstHSubSample, c->dst_h_chr_pos, 0))) < 0)
1887 goto fail;
1888
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17771 times.
17771 if (ff_shuffle_filter_coefficients(c, c->hChrFilterPos, c->hChrFilterSize, c->hChrFilter, c->chrDstW) < 0)
1889 goto nomem;
1890 }
1891 } // initialize horizontal stuff
1892
1893 /* precalculate vertical scaler filter coefficients */
1894 {
1895
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 17697 times.
17771 const int filterAlign = X86_MMX(cpu_flags) ? 2 :
1896 PPC_ALTIVEC(cpu_flags) ? 8 :
1897 have_neon(cpu_flags) ? 2 : 1;
1898
1899
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 17771 times.
35542 if ((ret = initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize,
1900 c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
1901 17771 (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
1902 cpu_flags, srcFilter->lumV, dstFilter->lumV,
1903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 c->param,
1904 get_local_pos(c, 0, 0, 1),
1905 get_local_pos(c, 0, 0, 1))) < 0)
1906 goto fail;
1907
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 17771 times.
35542 if ((ret = initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize,
1908 c->chrYInc, c->chrSrcH, c->chrDstH,
1909 filterAlign, (1 << 12),
1910 17771 (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
1911 cpu_flags, srcFilter->chrV, dstFilter->chrV,
1912
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 c->param,
1913 get_local_pos(c, c->chrSrcVSubSample, c->src_v_chr_pos, 1),
1914 get_local_pos(c, c->chrDstVSubSample, c->dst_v_chr_pos, 1))) < 0)
1915
1916 goto fail;
1917
1918 #if HAVE_ALTIVEC
1919 if (!FF_ALLOC_TYPED_ARRAY(c->vYCoeffsBank, c->vLumFilterSize * c->dstH) ||
1920 !FF_ALLOC_TYPED_ARRAY(c->vCCoeffsBank, c->vChrFilterSize * c->chrDstH))
1921 goto nomem;
1922
1923 for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
1924 int j;
1925 short *p = (short *)&c->vYCoeffsBank[i];
1926 for (j = 0; j < 8; j++)
1927 p[j] = c->vLumFilter[i];
1928 }
1929
1930 for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
1931 int j;
1932 short *p = (short *)&c->vCCoeffsBank[i];
1933 for (j = 0; j < 8; j++)
1934 p[j] = c->vChrFilter[i];
1935 }
1936 #endif
1937 }
1938
1939
2/2
✓ Branch 0 taken 71084 times.
✓ Branch 1 taken 17771 times.
88855 for (i = 0; i < 4; i++)
1940
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 71084 times.
71084 if (!FF_ALLOCZ_TYPED_ARRAY(c->dither_error[i], c->dstW + 3))
1941 goto nomem;
1942
1943
4/4
✓ Branch 1 taken 495 times.
✓ Branch 2 taken 17276 times.
✓ Branch 4 taken 218 times.
✓ Branch 5 taken 277 times.
17771 c->needAlpha = (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) ? 1 : 0;
1944
1945 // 64 / c->scalingBpp is the same as 16 / sizeof(scaling_intermediate)
1946 17771 c->uv_off = (dst_stride>>1) + 64 / (c->dstBpc &~ 7);
1947 17771 c->uv_offx2 = dst_stride + 16;
1948
1949
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 av_assert0(c->chrDstH <= dstH);
1950
1951
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 if (flags & SWS_PRINT_INFO) {
1952 const char *scaler = NULL, *cpucaps;
1953
1954 for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
1955 if (flags & scale_algorithms[i].flag) {
1956 scaler = scale_algorithms[i].description;
1957 break;
1958 }
1959 }
1960 if (!scaler)
1961 scaler = "ehh flags invalid?!";
1962 av_log(c, AV_LOG_INFO, "%s scaler, from %s to %s%s ",
1963 scaler,
1964 av_get_pix_fmt_name(srcFormat),
1965 dstFormat == AV_PIX_FMT_BGR555 || dstFormat == AV_PIX_FMT_BGR565 ||
1966 dstFormat == AV_PIX_FMT_RGB444BE || dstFormat == AV_PIX_FMT_RGB444LE ||
1967 dstFormat == AV_PIX_FMT_BGR444BE || dstFormat == AV_PIX_FMT_BGR444LE ?
1968 "dithered " : "",
1969 av_get_pix_fmt_name(dstFormat));
1970
1971 if (INLINE_MMXEXT(cpu_flags))
1972 cpucaps = "MMXEXT";
1973 else if (INLINE_MMX(cpu_flags))
1974 cpucaps = "MMX";
1975 else if (PPC_ALTIVEC(cpu_flags))
1976 cpucaps = "AltiVec";
1977 else
1978 cpucaps = "C";
1979
1980 av_log(c, AV_LOG_INFO, "using %s\n", cpucaps);
1981
1982 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1983 av_log(c, AV_LOG_DEBUG,
1984 "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1985 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
1986 av_log(c, AV_LOG_DEBUG,
1987 "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1988 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
1989 c->chrXInc, c->chrYInc);
1990 }
1991
1992 17771 ff_sws_init_scale(c);
1993
1994 17771 return ff_init_filters(c);
1995 nomem:
1996 ret = AVERROR(ENOMEM);
1997 fail: // FIXME replace things by appropriate error codes
1998 if (ret == RETCODE_USE_CASCADE) {
1999 int tmpW = sqrt(srcW * (int64_t)dstW);
2000 int tmpH = sqrt(srcH * (int64_t)dstH);
2001 enum AVPixelFormat tmpFormat = AV_PIX_FMT_YUV420P;
2002
2003 if (isALPHA(srcFormat))
2004 tmpFormat = AV_PIX_FMT_YUVA420P;
2005
2006 if (srcW*(int64_t)srcH <= 4LL*dstW*dstH)
2007 return AVERROR(EINVAL);
2008
2009 ret = av_image_alloc(c->cascaded_tmp[0], c->cascaded_tmpStride[0],
2010 tmpW, tmpH, tmpFormat, 64);
2011 if (ret < 0)
2012 return ret;
2013
2014 c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
2015 tmpW, tmpH, tmpFormat,
2016 flags, srcFilter, NULL, c->param);
2017 if (!c->cascaded_context[0])
2018 return AVERROR(ENOMEM);
2019
2020 c->cascaded_context[1] = sws_getContext(tmpW, tmpH, tmpFormat,
2021 dstW, dstH, dstFormat,
2022 flags, NULL, dstFilter, c->param);
2023 if (!c->cascaded_context[1])
2024 return AVERROR(ENOMEM);
2025 return 0;
2026 }
2027 return ret;
2028 }
2029
2030 1888 static int context_init_threaded(SwsContext *sws,
2031 SwsFilter *src_filter, SwsFilter *dst_filter)
2032 {
2033 1888 SwsInternal *c = sws_internal(sws);
2034 int ret;
2035
2036 1888 ret = avpriv_slicethread_create(&c->slicethread, (void*) sws,
2037 ff_sws_slice_worker, NULL, c->nb_threads);
2038
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1888 times.
1888 if (ret == AVERROR(ENOSYS)) {
2039 c->nb_threads = 1;
2040 return 0;
2041
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1888 times.
1888 } else if (ret < 0)
2042 return ret;
2043
2044 1888 c->nb_threads = ret;
2045
2046 1888 c->slice_ctx = av_calloc(c->nb_threads, sizeof(*c->slice_ctx));
2047 1888 c->slice_err = av_calloc(c->nb_threads, sizeof(*c->slice_err));
2048
2/4
✓ Branch 0 taken 1888 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1888 times.
1888 if (!c->slice_ctx || !c->slice_err)
2049 return AVERROR(ENOMEM);
2050
2051
2/2
✓ Branch 0 taken 16937 times.
✓ Branch 1 taken 1883 times.
18820 for (int i = 0; i < c->nb_threads; i++) {
2052 SwsInternal *c2;
2053 16937 c->slice_ctx[i] = sws_alloc_context();
2054
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16937 times.
16937 if (!c->slice_ctx[i])
2055 return AVERROR(ENOMEM);
2056 16937 c2 = sws_internal(c->slice_ctx[i]);
2057
2058 16937 c->nb_slice_ctx++;
2059 16937 c2->parent = sws;
2060
2061 16937 ret = av_opt_copy((void*)c->slice_ctx[i], (void*)c);
2062
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16937 times.
16937 if (ret < 0)
2063 return ret;
2064
2065 16937 c2->nb_threads = 1;
2066
2067 16937 ret = sws_init_single_context(c->slice_ctx[i], src_filter, dst_filter);
2068
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16937 times.
16937 if (ret < 0)
2069 return ret;
2070
2071
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 16932 times.
16937 if (c2->dither == SWS_DITHER_ED) {
2072 5 av_log(c, AV_LOG_VERBOSE,
2073 "Error-diffusion dither is in use, scaling will be single-threaded.");
2074 5 break;
2075 }
2076 }
2077
2078 1888 return 0;
2079 }
2080
2081 7632 av_cold int sws_init_context(SwsContext *sws, SwsFilter *srcFilter,
2082 SwsFilter *dstFilter)
2083 {
2084 7632 SwsInternal *c = sws_internal(sws);
2085 static AVOnce rgb2rgb_once = AV_ONCE_INIT;
2086 enum AVPixelFormat src_format, dst_format;
2087 int ret;
2088
2089 7632 c->frame_src = av_frame_alloc();
2090 7632 c->frame_dst = av_frame_alloc();
2091
2/4
✓ Branch 0 taken 7632 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7632 times.
7632 if (!c->frame_src || !c->frame_dst)
2092 return AVERROR(ENOMEM);
2093
2094
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7632 times.
7632 if (ff_thread_once(&rgb2rgb_once, ff_sws_rgb2rgb_init) != 0)
2095 return AVERROR_UNKNOWN;
2096
2097 7632 src_format = c->srcFormat;
2098 7632 dst_format = c->dstFormat;
2099 7632 c->srcRange |= handle_jpeg(&c->srcFormat);
2100 7632 c->dstRange |= handle_jpeg(&c->dstFormat);
2101
2102
4/4
✓ Branch 0 taken 7552 times.
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 161 times.
✓ Branch 3 taken 7391 times.
7632 if (src_format != c->srcFormat || dst_format != c->dstFormat)
2103 241 av_log(c, AV_LOG_WARNING, "deprecated pixel format used, make sure you did set range correctly\n");
2104
2105
2/2
✓ Branch 0 taken 1888 times.
✓ Branch 1 taken 5744 times.
7632 if (c->nb_threads != 1) {
2106 1888 ret = context_init_threaded(sws, srcFilter, dstFilter);
2107
2/4
✓ Branch 0 taken 1888 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1888 times.
✗ Branch 3 not taken.
1888 if (ret < 0 || c->nb_threads > 1)
2108 1888 return ret;
2109 // threading disabled in this build, init as single-threaded
2110 }
2111
2112 5744 return sws_init_single_context(sws, srcFilter, dstFilter);
2113 }
2114
2115 1895 SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
2116 int dstW, int dstH, enum AVPixelFormat dstFormat,
2117 int flags, SwsFilter *srcFilter,
2118 SwsFilter *dstFilter, const double *param)
2119 {
2120 SwsContext *sws;
2121
2122 1895 sws = alloc_set_opts(srcW, srcH, srcFormat,
2123 dstW, dstH, dstFormat,
2124 flags, param);
2125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1895 times.
1895 if (!sws)
2126 return NULL;
2127
2128
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1895 times.
1895 if (sws_init_context(sws, srcFilter, dstFilter) < 0) {
2129 sws_freeContext(sws);
2130 return NULL;
2131 }
2132
2133 1895 return sws;
2134 }
2135
2136 static int isnan_vec(SwsVector *a)
2137 {
2138 int i;
2139 for (i=0; i<a->length; i++)
2140 if (isnan(a->coeff[i]))
2141 return 1;
2142 return 0;
2143 }
2144
2145 static void makenan_vec(SwsVector *a)
2146 {
2147 int i;
2148 for (i=0; i<a->length; i++)
2149 a->coeff[i] = NAN;
2150 }
2151
2152 SwsVector *sws_allocVec(int length)
2153 {
2154 SwsVector *vec;
2155
2156 if(length <= 0 || length > INT_MAX/ sizeof(double))
2157 return NULL;
2158
2159 vec = av_malloc(sizeof(SwsVector));
2160 if (!vec)
2161 return NULL;
2162 vec->length = length;
2163 vec->coeff = av_malloc(sizeof(double) * length);
2164 if (!vec->coeff)
2165 av_freep(&vec);
2166 return vec;
2167 }
2168
2169 SwsVector *sws_getGaussianVec(double variance, double quality)
2170 {
2171 const int length = (int)(variance * quality + 0.5) | 1;
2172 int i;
2173 double middle = (length - 1) * 0.5;
2174 SwsVector *vec;
2175
2176 if(variance < 0 || quality < 0)
2177 return NULL;
2178
2179 vec = sws_allocVec(length);
2180
2181 if (!vec)
2182 return NULL;
2183
2184 for (i = 0; i < length; i++) {
2185 double dist = i - middle;
2186 vec->coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
2187 sqrt(2 * variance * M_PI);
2188 }
2189
2190 sws_normalizeVec(vec, 1.0);
2191
2192 return vec;
2193 }
2194
2195 /**
2196 * Allocate and return a vector with length coefficients, all
2197 * with the same value c.
2198 */
2199 static
2200 SwsVector *sws_getConstVec(double c, int length)
2201 {
2202 int i;
2203 SwsVector *vec = sws_allocVec(length);
2204
2205 if (!vec)
2206 return NULL;
2207
2208 for (i = 0; i < length; i++)
2209 vec->coeff[i] = c;
2210
2211 return vec;
2212 }
2213
2214 /**
2215 * Allocate and return a vector with just one coefficient, with
2216 * value 1.0.
2217 */
2218 static
2219 SwsVector *sws_getIdentityVec(void)
2220 {
2221 return sws_getConstVec(1.0, 1);
2222 }
2223
2224 static double sws_dcVec(SwsVector *a)
2225 {
2226 int i;
2227 double sum = 0;
2228
2229 for (i = 0; i < a->length; i++)
2230 sum += a->coeff[i];
2231
2232 return sum;
2233 }
2234
2235 void sws_scaleVec(SwsVector *a, double scalar)
2236 {
2237 int i;
2238
2239 for (i = 0; i < a->length; i++)
2240 a->coeff[i] *= scalar;
2241 }
2242
2243 void sws_normalizeVec(SwsVector *a, double height)
2244 {
2245 sws_scaleVec(a, height / sws_dcVec(a));
2246 }
2247
2248 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
2249 {
2250 int length = FFMAX(a->length, b->length);
2251 int i;
2252 SwsVector *vec = sws_getConstVec(0.0, length);
2253
2254 if (!vec)
2255 return NULL;
2256
2257 for (i = 0; i < a->length; i++)
2258 vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
2259 for (i = 0; i < b->length; i++)
2260 vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] += b->coeff[i];
2261
2262 return vec;
2263 }
2264
2265 /* shift left / or right if "shift" is negative */
2266 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
2267 {
2268 int length = a->length + FFABS(shift) * 2;
2269 int i;
2270 SwsVector *vec = sws_getConstVec(0.0, length);
2271
2272 if (!vec)
2273 return NULL;
2274
2275 for (i = 0; i < a->length; i++) {
2276 vec->coeff[i + (length - 1) / 2 -
2277 (a->length - 1) / 2 - shift] = a->coeff[i];
2278 }
2279
2280 return vec;
2281 }
2282
2283 static
2284 void sws_shiftVec(SwsVector *a, int shift)
2285 {
2286 SwsVector *shifted = sws_getShiftedVec(a, shift);
2287 if (!shifted) {
2288 makenan_vec(a);
2289 return;
2290 }
2291 av_free(a->coeff);
2292 a->coeff = shifted->coeff;
2293 a->length = shifted->length;
2294 av_free(shifted);
2295 }
2296
2297 static
2298 void sws_addVec(SwsVector *a, SwsVector *b)
2299 {
2300 SwsVector *sum = sws_sumVec(a, b);
2301 if (!sum) {
2302 makenan_vec(a);
2303 return;
2304 }
2305 av_free(a->coeff);
2306 a->coeff = sum->coeff;
2307 a->length = sum->length;
2308 av_free(sum);
2309 }
2310
2311 /**
2312 * Print with av_log() a textual representation of the vector a
2313 * if log_level <= av_log_level.
2314 */
2315 static
2316 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
2317 {
2318 int i;
2319 double max = 0;
2320 double min = 0;
2321 double range;
2322
2323 for (i = 0; i < a->length; i++)
2324 if (a->coeff[i] > max)
2325 max = a->coeff[i];
2326
2327 for (i = 0; i < a->length; i++)
2328 if (a->coeff[i] < min)
2329 min = a->coeff[i];
2330
2331 range = max - min;
2332
2333 for (i = 0; i < a->length; i++) {
2334 int x = (int)((a->coeff[i] - min) * 60.0 / range + 0.5);
2335 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
2336 for (; x > 0; x--)
2337 av_log(log_ctx, log_level, " ");
2338 av_log(log_ctx, log_level, "|\n");
2339 }
2340 }
2341
2342 void sws_freeVec(SwsVector *a)
2343 {
2344 if (!a)
2345 return;
2346 av_freep(&a->coeff);
2347 a->length = 0;
2348 av_free(a);
2349 }
2350
2351 void sws_freeFilter(SwsFilter *filter)
2352 {
2353 if (!filter)
2354 return;
2355
2356 sws_freeVec(filter->lumH);
2357 sws_freeVec(filter->lumV);
2358 sws_freeVec(filter->chrH);
2359 sws_freeVec(filter->chrV);
2360 av_free(filter);
2361 }
2362
2363 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
2364 float lumaSharpen, float chromaSharpen,
2365 float chromaHShift, float chromaVShift,
2366 int verbose)
2367 {
2368 SwsFilter *filter = av_malloc(sizeof(SwsFilter));
2369 if (!filter)
2370 return NULL;
2371
2372 if (lumaGBlur != 0.0) {
2373 filter->lumH = sws_getGaussianVec(lumaGBlur, 3.0);
2374 filter->lumV = sws_getGaussianVec(lumaGBlur, 3.0);
2375 } else {
2376 filter->lumH = sws_getIdentityVec();
2377 filter->lumV = sws_getIdentityVec();
2378 }
2379
2380 if (chromaGBlur != 0.0) {
2381 filter->chrH = sws_getGaussianVec(chromaGBlur, 3.0);
2382 filter->chrV = sws_getGaussianVec(chromaGBlur, 3.0);
2383 } else {
2384 filter->chrH = sws_getIdentityVec();
2385 filter->chrV = sws_getIdentityVec();
2386 }
2387
2388 if (!filter->lumH || !filter->lumV || !filter->chrH || !filter->chrV)
2389 goto fail;
2390
2391 if (chromaSharpen != 0.0) {
2392 SwsVector *id = sws_getIdentityVec();
2393 if (!id)
2394 goto fail;
2395 sws_scaleVec(filter->chrH, -chromaSharpen);
2396 sws_scaleVec(filter->chrV, -chromaSharpen);
2397 sws_addVec(filter->chrH, id);
2398 sws_addVec(filter->chrV, id);
2399 sws_freeVec(id);
2400 }
2401
2402 if (lumaSharpen != 0.0) {
2403 SwsVector *id = sws_getIdentityVec();
2404 if (!id)
2405 goto fail;
2406 sws_scaleVec(filter->lumH, -lumaSharpen);
2407 sws_scaleVec(filter->lumV, -lumaSharpen);
2408 sws_addVec(filter->lumH, id);
2409 sws_addVec(filter->lumV, id);
2410 sws_freeVec(id);
2411 }
2412
2413 if (chromaHShift != 0.0)
2414 sws_shiftVec(filter->chrH, (int)(chromaHShift + 0.5));
2415
2416 if (chromaVShift != 0.0)
2417 sws_shiftVec(filter->chrV, (int)(chromaVShift + 0.5));
2418
2419 sws_normalizeVec(filter->chrH, 1.0);
2420 sws_normalizeVec(filter->chrV, 1.0);
2421 sws_normalizeVec(filter->lumH, 1.0);
2422 sws_normalizeVec(filter->lumV, 1.0);
2423
2424 if (isnan_vec(filter->chrH) ||
2425 isnan_vec(filter->chrV) ||
2426 isnan_vec(filter->lumH) ||
2427 isnan_vec(filter->lumV))
2428 goto fail;
2429
2430 if (verbose)
2431 sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
2432 if (verbose)
2433 sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
2434
2435 return filter;
2436
2437 fail:
2438 sws_freeVec(filter->lumH);
2439 sws_freeVec(filter->lumV);
2440 sws_freeVec(filter->chrH);
2441 sws_freeVec(filter->chrV);
2442 av_freep(&filter);
2443 return NULL;
2444 }
2445
2446 162076 void sws_freeContext(SwsContext *sws)
2447 {
2448 162076 SwsInternal *c = sws_internal(sws);
2449 int i;
2450
2/2
✓ Branch 0 taken 127603 times.
✓ Branch 1 taken 34473 times.
162076 if (!c)
2451 127603 return;
2452
2453
2/2
✓ Branch 0 taken 16937 times.
✓ Branch 1 taken 34473 times.
51410 for (i = 0; i < c->nb_slice_ctx; i++)
2454 16937 sws_freeContext(c->slice_ctx[i]);
2455 34473 av_freep(&c->slice_ctx);
2456 34473 av_freep(&c->slice_err);
2457
2458 34473 avpriv_slicethread_free(&c->slicethread);
2459
2460
2/2
✓ Branch 0 taken 137892 times.
✓ Branch 1 taken 34473 times.
172365 for (i = 0; i < 4; i++)
2461 137892 av_freep(&c->dither_error[i]);
2462
2463 34473 av_frame_free(&c->frame_src);
2464 34473 av_frame_free(&c->frame_dst);
2465
2466 34473 av_freep(&c->src_ranges.ranges);
2467
2468 34473 av_freep(&c->vLumFilter);
2469 34473 av_freep(&c->vChrFilter);
2470 34473 av_freep(&c->hLumFilter);
2471 34473 av_freep(&c->hChrFilter);
2472 #if HAVE_ALTIVEC
2473 av_freep(&c->vYCoeffsBank);
2474 av_freep(&c->vCCoeffsBank);
2475 #endif
2476
2477 34473 av_freep(&c->vLumFilterPos);
2478 34473 av_freep(&c->vChrFilterPos);
2479 34473 av_freep(&c->hLumFilterPos);
2480 34473 av_freep(&c->hChrFilterPos);
2481
2482 #if HAVE_MMX_INLINE
2483 #if USE_MMAP
2484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34473 times.
34473 if (c->lumMmxextFilterCode)
2485 munmap(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize);
2486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34473 times.
34473 if (c->chrMmxextFilterCode)
2487 munmap(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize);
2488 #elif HAVE_VIRTUALALLOC
2489 if (c->lumMmxextFilterCode)
2490 VirtualFree(c->lumMmxextFilterCode, 0, MEM_RELEASE);
2491 if (c->chrMmxextFilterCode)
2492 VirtualFree(c->chrMmxextFilterCode, 0, MEM_RELEASE);
2493 #else
2494 av_free(c->lumMmxextFilterCode);
2495 av_free(c->chrMmxextFilterCode);
2496 #endif
2497 34473 c->lumMmxextFilterCode = NULL;
2498 34473 c->chrMmxextFilterCode = NULL;
2499 #endif /* HAVE_MMX_INLINE */
2500
2501 34473 av_freep(&c->yuvTable);
2502 34473 av_freep(&c->formatConvBuffer);
2503
2504 34473 sws_freeContext(c->cascaded_context[0]);
2505 34473 sws_freeContext(c->cascaded_context[1]);
2506 34473 sws_freeContext(c->cascaded_context[2]);
2507 34473 memset(c->cascaded_context, 0, sizeof(c->cascaded_context));
2508 34473 av_freep(&c->cascaded_tmp[0][0]);
2509 34473 av_freep(&c->cascaded_tmp[1][0]);
2510
2511 34473 av_freep(&c->gamma);
2512 34473 av_freep(&c->inv_gamma);
2513
2514 34473 av_freep(&c->rgb0_scratch);
2515 34473 av_freep(&c->xyz_scratch);
2516
2517 34473 ff_free_filters(c);
2518
2519 34473 av_free(sws);
2520 }
2521
2522 void sws_free_context(SwsContext **pctx)
2523 {
2524 SwsContext *ctx = *pctx;
2525 if (!ctx)
2526 return;
2527
2528 sws_freeContext(ctx);
2529 *pctx = NULL;
2530 }
2531
2532 SwsContext *sws_getCachedContext(SwsContext *sws, int srcW,
2533 int srcH, enum AVPixelFormat srcFormat,
2534 int dstW, int dstH,
2535 enum AVPixelFormat dstFormat, int flags,
2536 SwsFilter *srcFilter,
2537 SwsFilter *dstFilter,
2538 const double *param)
2539 {
2540 SwsInternal *context;
2541
2542 static const double default_param[2] = { SWS_PARAM_DEFAULT,
2543 SWS_PARAM_DEFAULT };
2544 int64_t src_h_chr_pos = -513, dst_h_chr_pos = -513,
2545 src_v_chr_pos = -513, dst_v_chr_pos = -513;
2546
2547 if (!param)
2548 param = default_param;
2549
2550 if ((context = sws_internal(sws)) &&
2551 (context->srcW != srcW ||
2552 context->srcH != srcH ||
2553 context->srcFormat != srcFormat ||
2554 context->dstW != dstW ||
2555 context->dstH != dstH ||
2556 context->dstFormat != dstFormat ||
2557 context->flags != flags ||
2558 context->param[0] != param[0] ||
2559 context->param[1] != param[1])) {
2560
2561 av_opt_get_int(context, "src_h_chr_pos", 0, &src_h_chr_pos);
2562 av_opt_get_int(context, "src_v_chr_pos", 0, &src_v_chr_pos);
2563 av_opt_get_int(context, "dst_h_chr_pos", 0, &dst_h_chr_pos);
2564 av_opt_get_int(context, "dst_v_chr_pos", 0, &dst_v_chr_pos);
2565 sws_freeContext(sws);
2566 sws = NULL;
2567 }
2568
2569 if (!sws) {
2570 if (!(sws = sws_alloc_context()))
2571 return NULL;
2572 context = sws_internal(sws);
2573 context->srcW = srcW;
2574 context->srcH = srcH;
2575 context->srcFormat = srcFormat;
2576 context->dstW = dstW;
2577 context->dstH = dstH;
2578 context->dstFormat = dstFormat;
2579 context->flags = flags;
2580 context->param[0] = param[0];
2581 context->param[1] = param[1];
2582
2583 av_opt_set_int(context, "src_h_chr_pos", src_h_chr_pos, 0);
2584 av_opt_set_int(context, "src_v_chr_pos", src_v_chr_pos, 0);
2585 av_opt_set_int(context, "dst_h_chr_pos", dst_h_chr_pos, 0);
2586 av_opt_set_int(context, "dst_v_chr_pos", dst_v_chr_pos, 0);
2587
2588 if (sws_init_context(sws, srcFilter, dstFilter) < 0) {
2589 sws_freeContext(sws);
2590 return NULL;
2591 }
2592 }
2593 return sws;
2594 }
2595
2596 84597 int ff_range_add(RangeList *rl, unsigned int start, unsigned int len)
2597 {
2598 Range *tmp;
2599 unsigned int idx;
2600
2601 /* find the first existing range after the new one */
2602
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84597 times.
84597 for (idx = 0; idx < rl->nb_ranges; idx++)
2603 if (rl->ranges[idx].start > start)
2604 break;
2605
2606 /* check for overlap */
2607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84597 times.
84597 if (idx > 0) {
2608 Range *prev = &rl->ranges[idx - 1];
2609 if (prev->start + prev->len > start)
2610 return AVERROR(EINVAL);
2611 }
2612
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84597 times.
84597 if (idx < rl->nb_ranges) {
2613 Range *next = &rl->ranges[idx];
2614 if (start + len > next->start)
2615 return AVERROR(EINVAL);
2616 }
2617
2618 84597 tmp = av_fast_realloc(rl->ranges, &rl->ranges_allocated,
2619 84597 (rl->nb_ranges + 1) * sizeof(*rl->ranges));
2620
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84597 times.
84597 if (!tmp)
2621 return AVERROR(ENOMEM);
2622 84597 rl->ranges = tmp;
2623
2624 84597 memmove(rl->ranges + idx + 1, rl->ranges + idx,
2625 84597 sizeof(*rl->ranges) * (rl->nb_ranges - idx));
2626 84597 rl->ranges[idx].start = start;
2627 84597 rl->ranges[idx].len = len;
2628 84597 rl->nb_ranges++;
2629
2630 /* merge ranges */
2631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84597 times.
84597 if (idx > 0) {
2632 Range *prev = &rl->ranges[idx - 1];
2633 Range *cur = &rl->ranges[idx];
2634 if (prev->start + prev->len == cur->start) {
2635 prev->len += cur->len;
2636 memmove(rl->ranges + idx - 1, rl->ranges + idx,
2637 sizeof(*rl->ranges) * (rl->nb_ranges - idx));
2638 rl->nb_ranges--;
2639 idx--;
2640 }
2641 }
2642
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84597 times.
84597 if (idx < rl->nb_ranges - 1) {
2643 Range *cur = &rl->ranges[idx];
2644 Range *next = &rl->ranges[idx + 1];
2645 if (cur->start + cur->len == next->start) {
2646 cur->len += next->len;
2647 memmove(rl->ranges + idx, rl->ranges + idx + 1,
2648 sizeof(*rl->ranges) * (rl->nb_ranges - idx - 1));
2649 rl->nb_ranges--;
2650 }
2651 }
2652
2653 84597 return 0;
2654 }
2655
2656 /**
2657 * This function also sanitizes and strips the input data, removing irrelevant
2658 * fields for certain formats.
2659 */
2660 SwsFormat ff_fmt_from_frame(const AVFrame *frame, int field)
2661 {
2662 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
2663 SwsFormat fmt = {
2664 .width = frame->width,
2665 .height = frame->height,
2666 .format = frame->format,
2667 .range = frame->color_range,
2668 .prim = frame->color_primaries,
2669 .trc = frame->color_trc,
2670 .csp = frame->colorspace,
2671 .loc = frame->chroma_location,
2672 .desc = desc,
2673 };
2674
2675 av_assert1(fmt.width > 0);
2676 av_assert1(fmt.height > 0);
2677 av_assert1(fmt.format != AV_PIX_FMT_NONE);
2678 av_assert0(desc);
2679 if (desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_BAYER)) {
2680 /* RGB-like family */
2681 fmt.csp = AVCOL_SPC_RGB;
2682 fmt.range = AVCOL_RANGE_JPEG;
2683 } else if (desc->flags & AV_PIX_FMT_FLAG_XYZ) {
2684 fmt.csp = AVCOL_SPC_UNSPECIFIED;
2685 fmt.prim = AVCOL_PRI_SMPTE428;
2686 fmt.trc = AVCOL_TRC_SMPTE428;
2687 } else if (desc->nb_components < 3) {
2688 /* Grayscale formats */
2689 fmt.prim = AVCOL_PRI_UNSPECIFIED;
2690 fmt.csp = AVCOL_SPC_UNSPECIFIED;
2691 if (desc->flags & AV_PIX_FMT_FLAG_FLOAT)
2692 fmt.range = AVCOL_RANGE_UNSPECIFIED;
2693 else
2694 fmt.range = AVCOL_RANGE_JPEG; // FIXME: this restriction should be lifted
2695 }
2696
2697 switch (frame->format) {
2698 case AV_PIX_FMT_YUVJ420P:
2699 case AV_PIX_FMT_YUVJ411P:
2700 case AV_PIX_FMT_YUVJ422P:
2701 case AV_PIX_FMT_YUVJ444P:
2702 case AV_PIX_FMT_YUVJ440P:
2703 fmt.range = AVCOL_RANGE_JPEG;
2704 break;
2705 }
2706
2707 if (!desc->log2_chroma_w && !desc->log2_chroma_h)
2708 fmt.loc = AVCHROMA_LOC_UNSPECIFIED;
2709
2710 if (frame->flags & AV_FRAME_FLAG_INTERLACED) {
2711 fmt.height = (fmt.height + (field == FIELD_TOP)) >> 1;
2712 fmt.interlaced = 1;
2713 }
2714
2715 return fmt;
2716 }
2717
2718 int sws_test_format(enum AVPixelFormat format, int output)
2719 {
2720 return output ? sws_isSupportedOutput(format) : sws_isSupportedInput(format);
2721 }
2722
2723 int sws_test_colorspace(enum AVColorSpace csp, int output)
2724 {
2725 switch (csp) {
2726 case AVCOL_SPC_UNSPECIFIED:
2727 case AVCOL_SPC_RGB:
2728 case AVCOL_SPC_BT709:
2729 case AVCOL_SPC_BT470BG:
2730 case AVCOL_SPC_SMPTE170M:
2731 case AVCOL_SPC_FCC:
2732 case AVCOL_SPC_SMPTE240M:
2733 case AVCOL_SPC_BT2020_NCL:
2734 return 1;
2735 default:
2736 return 0;
2737 }
2738 }
2739
2740 int sws_test_primaries(enum AVColorPrimaries prim, int output)
2741 {
2742 return prim > AVCOL_PRI_RESERVED0 && prim < AVCOL_PRI_NB &&
2743 prim != AVCOL_PRI_RESERVED;
2744 }
2745
2746 int sws_test_transfer(enum AVColorTransferCharacteristic trc, int output)
2747 {
2748 return trc > AVCOL_TRC_RESERVED0 && trc < AVCOL_TRC_NB &&
2749 trc != AVCOL_TRC_RESERVED;
2750 }
2751
2752 static int test_range(enum AVColorRange range)
2753 {
2754 return range >= 0 && range < AVCOL_RANGE_NB;
2755 }
2756
2757 static int test_loc(enum AVChromaLocation loc)
2758 {
2759 return loc >= 0 && loc < AVCHROMA_LOC_NB;
2760 }
2761
2762 int ff_test_fmt(const SwsFormat *fmt, int output)
2763 {
2764 return fmt->width > 0 && fmt->height > 0 &&
2765 sws_test_format (fmt->format, output) &&
2766 sws_test_colorspace(fmt->csp, output) &&
2767 sws_test_primaries (fmt->prim, output) &&
2768 sws_test_transfer (fmt->trc, output) &&
2769 test_range (fmt->range) &&
2770 test_loc (fmt->loc);
2771 }
2772
2773 int sws_test_frame(const AVFrame *frame, int output)
2774 {
2775 for (int field = 0; field < 2; field++) {
2776 const SwsFormat fmt = ff_fmt_from_frame(frame, field);
2777 if (!ff_test_fmt(&fmt, output))
2778 return 0;
2779 if (!fmt.interlaced)
2780 break;
2781 }
2782
2783 return 1;
2784 }
2785
2786 int sws_is_noop(const AVFrame *dst, const AVFrame *src)
2787 {
2788 for (int field = 0; field < 2; field++) {
2789 SwsFormat dst_fmt = ff_fmt_from_frame(dst, field);
2790 SwsFormat src_fmt = ff_fmt_from_frame(src, field);
2791 if (!ff_fmt_equal(&dst_fmt, &src_fmt))
2792 return 0;
2793 if (!dst_fmt.interlaced)
2794 break;
2795 }
2796
2797 return 1;
2798 }
2799