FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/utils.c
Date: 2026-03-12 12:45:31
Exec Total Coverage
Lines: 785 1357 57.8%
Functions: 22 43 51.2%
Branches: 670 1221 54.9%

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