FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/utils.c
Date: 2026-02-18 00:28:21
Exec Total Coverage
Lines: 772 1341 57.6%
Functions: 22 43 51.2%
Branches: 661 1201 55.0%

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