FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/utils.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 892 1507 59.2%
Functions: 35 58 60.3%
Branches: 705 1308 53.9%

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