../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
650 / 706
92.1%
Branches:
471 / 536
87.9%
Functions:
20 / 20
100.0%

libswscale/ops_optimizer.c
Line Branch Exec Source
1 /**
2 * Copyright (C) 2025 Niklas Haas
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/attributes.h"
22 #include "libavutil/avassert.h"
23 #include "libavutil/bswap.h"
24 #include "libavutil/rational.h"
25
26 #include "ops.h"
27 #include "ops_internal.h"
28
29 #define RET(x) \
30 do { \
31 if ((ret = (x)) < 0) \
32 return ret; \
33 } while (0)
34
35 /**
36 * Try to commute a clear op with the next operation. Makes any adjustments
37 * to the operations as needed, but does not perform the actual commutation.
38 *
39 * Returns whether successful.
40 */
41 3486176 static bool op_commute_clear(SwsOp *op, SwsOp *next)
42 {
43 av_assert1(op->op == SWS_OP_CLEAR);
44
5/8
✓ Branch 2 → 3 taken 444062 times.
✓ Branch 2 → 6 taken 693960 times.
✗ Branch 2 → 8 not taken.
✓ Branch 2 → 12 taken 492145 times.
✓ Branch 2 → 18 taken 177700 times.
✓ Branch 2 → 23 taken 1678309 times.
✗ Branch 2 → 24 not taken.
✗ Branch 2 → 25 not taken.
3486176 switch (next->op) {
45 444062 case SWS_OP_CONVERT:
46
2/2
✓ Branch 5 → 4 taken 1776248 times.
✓ Branch 5 → 6 taken 444062 times.
2220310 for (int i = 0; i < 4; i++)
47 1776248 op->clear.type[i] = next->convert.to;
48 av_fallthrough;
49 case SWS_OP_LSHIFT:
50 case SWS_OP_RSHIFT:
51 case SWS_OP_DITHER:
52 case SWS_OP_MIN:
53 case SWS_OP_MAX:
54 case SWS_OP_SCALE:
55 case SWS_OP_READ:
56 1138022 ff_sws_apply_op_q(next, op->clear.value);
57 1138022 return true;
58 ✗ case SWS_OP_FILTER_H:
59 case SWS_OP_FILTER_V:
60
0/2
✗ Branch 10 → 9 not taken.
✗ Branch 10 → 11 not taken.
✗ for (int i = 0; i < 4; i++)
61 ✗ op->clear.type[i] = next->filter.type;
62 ✗ return true;
63 492145 case SWS_OP_SWIZZLE: {
64 492145 const SwsClearOp orig = op->clear;
65
2/2
✓ Branch 14 → 13 taken 1968580 times.
✓ Branch 14 → 15 taken 492145 times.
2460725 for (int i = 0; i < 4; i++)
66 1968580 op->clear.type[i] = orig.type[next->swizzle.in[i]];
67 492145 ff_sws_comp_mask_swizzle(&op->clear.mask, &next->swizzle);
68 492145 ff_sws_apply_op_q(next, op->clear.value);
69 492145 return true;
70 }
71 177700 case SWS_OP_SWAP_BYTES:
72
2/2
✓ Branch 19 → 20 taken 133258 times.
✓ Branch 19 → 22 taken 44442 times.
177700 switch (ff_sws_op_input_type(next)) {
73 133258 case SWS_PIXEL_U16:
74 case SWS_PIXEL_U32:
75 133258 ff_sws_apply_op_q(next, op->clear.value); /* always representable */
76 133258 return true;
77 44442 default:
78 44442 return false;
79 }
80 1678309 case SWS_OP_INVALID:
81 case SWS_OP_WRITE:
82 case SWS_OP_LINEAR:
83 case SWS_OP_PACK:
84 case SWS_OP_UNPACK:
85 case SWS_OP_CLEAR:
86 case SWS_OP_LUT_3D:
87 1678309 return false;
88 ✗ case SWS_OP_TYPE_NB:
89 ✗ break;
90 }
91
92 ✗ av_unreachable("Invalid operation type!");
93 return false;
94 }
95
96 /**
97 * Try to commute a swizzle op with the next operation. Makes any adjustments
98 * to the operations as needed, but does not perform the actual commutation.
99 *
100 * Returns whether successful.
101 */
102 3959567 static bool op_commute_swizzle(SwsOp *op, SwsOp *next)
103 {
104 3959567 bool seen[4] = {0};
105
106 av_assert1(op->op == SWS_OP_SWIZZLE);
107
4/6
✓ Branch 2 → 3 taken 1457126 times.
✓ Branch 2 → 4 taken 290696 times.
✓ Branch 2 → 16 taken 205617 times.
✓ Branch 2 → 27 taken 2006128 times.
✗ Branch 2 → 28 not taken.
✗ Branch 2 → 29 not taken.
3959567 switch (next->op) {
108 1457126 case SWS_OP_CONVERT:
109 case SWS_OP_SWAP_BYTES:
110 case SWS_OP_LSHIFT:
111 case SWS_OP_RSHIFT:
112 case SWS_OP_SCALE:
113 case SWS_OP_FILTER_H:
114 case SWS_OP_FILTER_V:
115 1457126 return true;
116
117 /**
118 * We can commute per-channel ops only if the per-channel constants are the
119 * same for all duplicated channels; e.g.:
120 * SWIZZLE {0, 0, 0, 3}
121 * NEXT {x, x, x, w}
122 * ->
123 * NEXT {x, _, _, w}
124 * SWIZZLE {0, 0, 0, 3}
125 */
126 290696 case SWS_OP_MIN:
127 case SWS_OP_MAX: {
128 290696 const SwsClampOp c = next->clamp;
129
2/2
✓ Branch 13 → 5 taken 1162784 times.
✓ Branch 13 → 14 taken 290696 times.
1453480 for (int i = 0; i < 4; i++) {
130
2/2
✓ Branch 5 → 6 taken 267645 times.
✓ Branch 5 → 7 taken 895139 times.
1162784 if (!SWS_OP_NEEDED(op, i))
131 267645 continue;
132 895139 const int j = op->swizzle.in[i];
133
3/4
✓ Branch 7 → 8 taken 186566 times.
✓ Branch 7 → 11 taken 708573 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 186566 times.
895139 if (seen[j] && ff_cmp_q64(next->clamp.limit[j], c.limit[i]))
134 ✗ return false;
135 895139 next->clamp.limit[j] = c.limit[i];
136 895139 seen[j] = true;
137 }
138 290696 return true;
139 }
140
141 205617 case SWS_OP_DITHER: {
142 205617 const SwsDitherOp d = next->dither;
143
2/2
✓ Branch 24 → 17 taken 775008 times.
✓ Branch 24 → 25 taken 181887 times.
956895 for (int i = 0; i < 4; i++) {
144
2/2
✓ Branch 17 → 18 taken 168948 times.
✓ Branch 17 → 19 taken 606060 times.
775008 if (!SWS_OP_NEEDED(op, i))
145 168948 continue;
146 606060 const int j = op->swizzle.in[i];
147
4/4
✓ Branch 19 → 20 taken 116820 times.
✓ Branch 19 → 22 taken 489240 times.
✓ Branch 20 → 21 taken 23730 times.
✓ Branch 20 → 22 taken 93090 times.
606060 if (seen[j] && next->dither.y_offset[j] != d.y_offset[i])
148 23730 return false;
149 582330 next->dither.y_offset[j] = d.y_offset[i];
150 582330 seen[j] = true;
151 }
152 181887 return true;
153 }
154
155 2006128 case SWS_OP_INVALID:
156 case SWS_OP_READ:
157 case SWS_OP_WRITE:
158 case SWS_OP_SWIZZLE:
159 case SWS_OP_CLEAR:
160 case SWS_OP_LINEAR:
161 case SWS_OP_PACK:
162 case SWS_OP_UNPACK:
163 case SWS_OP_LUT_3D:
164 2006128 return false;
165 ✗ case SWS_OP_TYPE_NB:
166 ✗ break;
167 }
168
169 ✗ av_unreachable("Invalid operation type!");
170 return false;
171 }
172
173 /**
174 * Try to commute a filter op with the previous operation. Makes any
175 * adjustments to the operations as needed, but does not perform the actual
176 * commutation.
177 *
178 * Returns whether successful.
179 */
180 21883434 static bool op_commute_filter(SwsOp *op, SwsOp *prev)
181 {
182
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 21883434 times.
21883434 av_assert0(!ff_sws_pixel_type_is_int(op->filter.type));
183
184
3/5
✓ Branch 5 → 6 taken 670814 times.
✓ Branch 5 → 7 taken 1995724 times.
✓ Branch 5 → 11 taken 19216896 times.
✗ Branch 5 → 12 not taken.
✗ Branch 5 → 13 not taken.
21883434 switch (prev->op) {
185 670814 case SWS_OP_SWIZZLE:
186 case SWS_OP_SCALE:
187 case SWS_OP_DITHER:
188 670814 return true;
189 1995724 case SWS_OP_LINEAR:
190
2/2
✓ Branch 9 → 8 taken 7982896 times.
✓ Branch 9 → 10 taken 1995724 times.
9978620 for (int i = 0; i < 4; i++)
191 7982896 prev->lin.type[i] = op->filter.type;
192 1995724 return true;
193 19216896 case SWS_OP_CONVERT:
194 case SWS_OP_INVALID:
195 case SWS_OP_READ:
196 case SWS_OP_WRITE:
197 case SWS_OP_SWAP_BYTES:
198 case SWS_OP_UNPACK:
199 case SWS_OP_PACK:
200 case SWS_OP_LSHIFT:
201 case SWS_OP_RSHIFT:
202 case SWS_OP_CLEAR:
203 case SWS_OP_MIN:
204 case SWS_OP_MAX:
205 case SWS_OP_FILTER_H:
206 case SWS_OP_FILTER_V:
207 case SWS_OP_LUT_3D:
208 19216896 return false;
209 ✗ case SWS_OP_TYPE_NB:
210 ✗ break;
211 }
212
213 ✗ av_unreachable("Invalid operation type!");
214 return false;
215 }
216
217 /* returns log2(x) only if x is a power of two, or 0 otherwise */
218 2680619 static int exact_log2(const int x)
219 {
220 int p;
221
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2680619 times.
2680619 if (x <= 0)
222 ✗ return 0;
223 2680619 p = av_log2(x);
224
2/2
✓ Branch 4 → 5 taken 1042703 times.
✓ Branch 4 → 6 taken 1637916 times.
2680619 return (1 << p) == x ? p : 0;
225 }
226
227 6475627 static int exact_log2_q64(const AVRational64 x)
228 {
229
2/2
✓ Branch 2 → 3 taken 1468972 times.
✓ Branch 2 → 5 taken 5006655 times.
6475627 if (x.den == 1)
230 1468972 return exact_log2(x.num);
231
2/2
✓ Branch 5 → 6 taken 1211647 times.
✓ Branch 5 → 8 taken 3795008 times.
5006655 else if (x.num == 1)
232 1211647 return -exact_log2(x.den);
233 else
234 3795008 return 0;
235 }
236
237 /**
238 * If a linear operation can be reduced to a scalar multiplication, returns
239 * the corresponding scaling factor, or 0 otherwise.
240 */
241 9001805 static bool extract_scalar(const SwsLinearOp *c,
242 const SwsOpPad outputs[4], const SwsOpPad inputs[4],
243 SwsScaleOp *out_scale)
244 {
245 9001805 SwsScaleOp scale = {0};
246
247 /* There are components not on the main diagonal */
248
2/2
✓ Branch 3 → 4 taken 6534155 times.
✓ Branch 3 → 5 taken 2467650 times.
9001805 if (ff_sws_linear_mask(c) & ~SWS_MASK_DIAG4)
249 6534155 return false;
250
251
2/2
✓ Branch 16 → 6 taken 7786248 times.
✓ Branch 16 → 17 taken 693488 times.
8479736 for (int i = 0; i < 4; i++) {
252 7786248 const AVRational64 s = c->m[i][i];
253
3/4
✓ Branch 6 → 7 taken 7786248 times.
✗ Branch 6 → 8 not taken.
✓ Branch 7 → 8 taken 710697 times.
✓ Branch 7 → 9 taken 7075551 times.
7786248 if ((inputs[i].flags & SWS_COMP_ZERO) || !outputs[i].dep_out)
254 710697 continue;
255
4/4
✓ Branch 9 → 10 taken 4607901 times.
✓ Branch 9 → 13 taken 2467650 times.
✓ Branch 11 → 12 taken 1774162 times.
✓ Branch 11 → 13 taken 2833739 times.
7075551 if (scale.factor.den && ff_cmp_q64(s, scale.factor))
256 1774162 return false;
257 5301389 scale.factor = s;
258 }
259
260
1/2
✓ Branch 17 → 18 taken 693488 times.
✗ Branch 17 → 19 not taken.
693488 if (scale.factor.den)
261 693488 *out_scale = scale;
262 693488 return scale.factor.den;
263 }
264
265 /* Extracts an integer clear operation (subset) from the given linear op. */
266 9501030 static bool extract_constant_rows(SwsLinearOp *c, const SwsOpPad inputs[4],
267 SwsClearOp *out_clear)
268 {
269 9501030 const uint32_t mask = ff_sws_linear_mask(c);
270 9501030 SwsClearOp clear = {0};
271 9501030 bool ret = false;
272
273
2/2
✓ Branch 18 → 4 taken 38004120 times.
✓ Branch 18 → 19 taken 9501030 times.
47505150 for (int i = 0; i < 4; i++) {
274 38004120 bool const_row = c->m[i][4].den == 1; /* offset is integer */
275
2/2
✓ Branch 10 → 5 taken 152016480 times.
✓ Branch 10 → 11 taken 38004120 times.
190020600 for (int j = 0; j < 4; j++) {
276
2/2
✓ Branch 5 → 6 taken 62140185 times.
✓ Branch 5 → 7 taken 89876295 times.
214156665 const_row &= c->m[i][j].num == 0 || /* scalar is zero */
277
2/2
✓ Branch 6 → 7 taken 986369 times.
✓ Branch 6 → 8 taken 61153816 times.
62140185 (inputs[j].flags & SWS_COMP_ZERO); /* input is zero */
278 }
279
4/4
✓ Branch 11 → 12 taken 635196 times.
✓ Branch 11 → 17 taken 37368924 times.
✓ Branch 12 → 13 taken 594615 times.
✓ Branch 12 → 17 taken 40581 times.
38004120 if (const_row && (mask & SWS_MASK_ROW(i))) {
280 594615 clear.mask |= SWS_COMP(i);
281 594615 clear.value[i] = c->m[i][4];
282 594615 clear.type[i] = c->type[i];
283
2/2
✓ Branch 15 → 14 taken 2973075 times.
✓ Branch 15 → 16 taken 594615 times.
3567690 for (int j = 0; j < 5; j++)
284 2973075 c->m[i][j] = Q(i == j);
285 594615 ret = true;
286 }
287 }
288
289
2/2
✓ Branch 19 → 20 taken 499225 times.
✓ Branch 19 → 21 taken 9001805 times.
9501030 if (ret)
290 499225 *out_clear = clear;
291 9501030 return ret;
292 }
293
294 /* Unswizzle a linear operation by aligning single-input rows with
295 * their corresponding diagonal */
296 8308317 static bool extract_swizzle(SwsLinearOp *op, const SwsOpPad inputs[4],
297 SwsSwizzleOp *out_swiz)
298 {
299 8308317 SwsSwizzleOp swiz = SWS_SWIZZLE(0, 1, 2, 3);
300 8308317 SwsLinearOp c = *op;
301
302 /* Find non-zero coefficients in the main 4x4 matrix */
303 8308317 uint32_t nonzero = 0;
304
2/2
✓ Branch 11 → 3 taken 33233268 times.
✓ Branch 11 → 12 taken 8308317 times.
41541585 for (int i = 0; i < 4; i++) {
305
2/2
✓ Branch 9 → 4 taken 132933072 times.
✓ Branch 9 → 10 taken 33233268 times.
166166340 for (int j = 0; j < 4; j++) {
306
4/4
✓ Branch 4 → 5 taken 56469370 times.
✓ Branch 4 → 6 taken 76463702 times.
✓ Branch 5 → 6 taken 341097 times.
✓ Branch 5 → 7 taken 56128273 times.
132933072 if (!c.m[i][j].num || (inputs[j].flags & SWS_COMP_ZERO))
307 76804799 continue;
308 56128273 nonzero |= SWS_MASK(i, j);
309 }
310 }
311
312 /* If a value is unique in its row and the target column is
313 * empty, move it there and update the input swizzle */
314
2/2
✓ Branch 22 → 13 taken 33233268 times.
✓ Branch 22 → 23 taken 8308317 times.
41541585 for (int i = 0; i < 4; i++) {
315
2/2
✓ Branch 13 → 14 taken 32865727 times.
✓ Branch 13 → 15 taken 367541 times.
33233268 if (nonzero & SWS_MASK_COL(i))
316 32865727 continue; /* target column is not empty */
317
2/2
✓ Branch 20 → 16 taken 489284 times.
✓ Branch 20 → 21 taken 40581 times.
529865 for (int j = 0; j < 4; j++) {
318
2/2
✓ Branch 16 → 17 taken 326960 times.
✓ Branch 16 → 19 taken 162324 times.
489284 if ((nonzero & SWS_MASK_ROW(i)) == SWS_MASK(i, j)) {
319 /* Move coefficient to the diagonal */
320 326960 c.m[i][i] = c.m[i][j];
321 326960 c.m[i][j] = Q(0);
322 326960 swiz.in[i] = j;
323 326960 break;
324 }
325 }
326 }
327
328
2/2
✓ Branch 23 → 24 taken 8144837 times.
✓ Branch 23 → 25 taken 163480 times.
8308317 if (swiz.mask == SWS_SWIZZLE(0, 1, 2, 3).mask)
329 8144837 return false; /* no swizzle was identified */
330
331 163480 *out_swiz = swiz;
332 163480 *op = c;
333 163480 return true;
334 }
335
336 397060 static int op_result_is_exact(const SwsOp *op)
337 {
338
2/2
✓ Branch 7 → 3 taken 569253 times.
✓ Branch 7 → 8 taken 55523 times.
624776 for (int i = 0; i < 4; i++) {
339
4/4
✓ Branch 3 → 4 taken 496605 times.
✓ Branch 3 → 6 taken 72648 times.
✓ Branch 4 → 5 taken 341537 times.
✓ Branch 4 → 6 taken 155068 times.
569253 if (SWS_OP_NEEDED(op, i) && !(op->outputs[i].flags & SWS_COMP_EXACT))
340 341537 return false;
341 }
342
343 55523 return true;
344 }
345
346 3868207 int ff_sws_op_list_optimize(SwsOpList *ops)
347 {
348 int ret;
349
350 28697151 retry:
351 32565358 ff_sws_op_list_update(ops);
352
353 /* Try to push filters towards the input; do this first to unblock
354 * in-place optimizations like linear op fusion */
355
2/2
✓ Branch 17 → 6 taken 306810839 times.
✓ Branch 17 → 18 taken 28965740 times.
335776579 for (int n = 1; n < ops->num_ops; n++) {
356 306810839 SwsOp *op = &ops->ops[n];
357 306810839 SwsOp *prev = &ops->ops[n - 1];
358
359
2/2
✓ Branch 6 → 7 taken 21883434 times.
✓ Branch 6 → 16 taken 284927405 times.
306810839 switch (op->op) {
360 21883434 case SWS_OP_FILTER_H:
361 case SWS_OP_FILTER_V:
362
2/2
✓ Branch 8 → 9 taken 2666538 times.
✓ Branch 8 → 10 taken 19216896 times.
21883434 if (op_commute_filter(op, prev)) {
363 2666538 FFSWAP(SwsOp, *op, *prev);
364 2666538 goto retry;
365 }
366
367 /* Merge filter with prior conversion */
368
2/2
✓ Branch 10 → 11 taken 2781157 times.
✓ Branch 10 → 15 taken 16435739 times.
19216896 if (prev->op == SWS_OP_CONVERT) {
369 2781157 const SwsPixelType input_type = ff_sws_op_input_type(prev);
370 2781157 int size_from = ff_sws_pixel_type_size(input_type);
371 2781157 int size_to = ff_sws_pixel_type_size(prev->convert.to);
372
2/2
✓ Branch 12 → 13 taken 933080 times.
✓ Branch 12 → 15 taken 1848077 times.
2781157 if (size_from < size_to) {
373 933080 ff_sws_op_list_remove_at(ops, n - 1, 1);
374 933080 goto retry;
375 }
376 }
377 18283816 break;
378 }
379 }
380
381 /* Apply all in-place optimizations (that do not re-order the list) */
382
2/2
✓ Branch 285 → 19 taken 165813108 times.
✓ Branch 285 → 286 taken 7616864 times.
173429972 for (int n = 0; n < ops->num_ops; n++) {
383 165813108 SwsOp dummy = {0};
384 165813108 SwsOp *op = &ops->ops[n];
385
2/2
✓ Branch 19 → 20 taken 136847368 times.
✓ Branch 19 → 21 taken 28965740 times.
165813108 SwsOp *prev = n ? &ops->ops[n - 1] : &dummy;
386
2/2
✓ Branch 22 → 23 taken 158196244 times.
✓ Branch 22 → 24 taken 7616864 times.
165813108 SwsOp *next = n + 1 < ops->num_ops ? &ops->ops[n + 1] : &dummy;
387
388 /* common helper variable */
389 165813108 const SwsCompMask needed = ff_sws_comp_mask_needed(op);
390 165813108 bool noop = true;
391
392
4/4
✓ Branch 26 → 27 taken 8900613 times.
✓ Branch 26 → 30 taken 156912495 times.
✓ Branch 27 → 28 taken 1283749 times.
✓ Branch 27 → 30 taken 7616864 times.
165813108 if (!needed && op->op != SWS_OP_WRITE) {
393 /* Remove any operation whose output is not needed */
394 1283749 ff_sws_op_list_remove_at(ops, n, 1);
395 21348876 goto retry;
396 }
397
398
15/15
✓ Branch 30 → 31 taken 27293847 times.
✓ Branch 30 → 45 taken 12469883 times.
✓ Branch 30 → 49 taken 4809456 times.
✓ Branch 30 → 57 taken 2482729 times.
✓ Branch 30 → 64 taken 9969194 times.
✓ Branch 30 → 108 taken 18165946 times.
✓ Branch 30 → 148 taken 20483260 times.
✓ Branch 30 → 156 taken 7511831 times.
✓ Branch 30 → 170 taken 5181123 times.
✓ Branch 30 → 184 taken 8844989 times.
✓ Branch 30 → 197 taken 15487880 times.
✓ Branch 30 → 254 taken 6475627 times.
✓ Branch 30 → 270 taken 14098056 times.
✓ Branch 30 → 278 taken 2347968 times.
✓ Branch 30 → 282 taken 8907570 times.
164529359 switch (op->op) {
399 27293847 case SWS_OP_READ:
400 /* "Compress" planar reads where not all components are needed */
401
2/2
✓ Branch 31 → 32 taken 15807738 times.
✓ Branch 31 → 44 taken 11486109 times.
27293847 if (op->rw.mode == SWS_RW_PLANAR) {
402 15807738 SwsSwizzleOp swiz = SWS_SWIZZLE(0, 1, 2, 3);
403 15807738 int nb_planes = 0;
404
2/2
✓ Branch 37 → 33 taken 39743364 times.
✓ Branch 37 → 38 taken 15807738 times.
55551102 for (int i = 0; i < op->rw.elems; i++) {
405
2/2
✓ Branch 33 → 34 taken 254750 times.
✓ Branch 33 → 35 taken 39488614 times.
39743364 if (!SWS_OP_NEEDED(op, i)) {
406 254750 swiz.in[i] = 3 - (i - nb_planes); /* map to unused plane */
407 254750 continue;
408 }
409
410 39488614 const int idx = nb_planes++;
411 av_assert1(idx <= i);
412 39488614 ops->plane_src[idx] = ops->plane_src[i];
413 39488614 swiz.in[i] = idx;
414 }
415
416
2/2
✓ Branch 38 → 39 taken 211695 times.
✓ Branch 38 → 43 taken 15596043 times.
15807738 if (nb_planes < op->rw.elems) {
417 211695 op->rw.elems = nb_planes;
418
1/2
✗ Branch 40 → 41 not taken.
✓ Branch 40 → 42 taken 211695 times.
211695 RET(ff_sws_op_list_insert_at(ops, n + 1, &(SwsOp) {
419 .op = SWS_OP_SWIZZLE,
420 .swizzle = swiz,
421 }));
422 211695 goto retry;
423 }
424 }
425 27082152 break;
426
427 12469883 case SWS_OP_SWAP_BYTES:
428 /* Redundant (double) swap */
429
2/2
✓ Branch 45 → 46 taken 4014 times.
✓ Branch 45 → 48 taken 12465869 times.
12469883 if (next->op == SWS_OP_SWAP_BYTES) {
430 4014 ff_sws_op_list_remove_at(ops, n, 2);
431 4014 goto retry;
432 }
433 12465869 break;
434
435 4809456 case SWS_OP_UNPACK:
436 /* Redundant unpack+pack */
437
2/2
✓ Branch 49 → 50 taken 849 times.
✓ Branch 49 → 56 taken 4808607 times.
4809456 if (next->op == SWS_OP_PACK &&
438
1/2
✓ Branch 50 → 51 taken 849 times.
✗ Branch 50 → 56 not taken.
849 next->pack.pattern[0] == op->pack.pattern[0] &&
439
1/2
✓ Branch 51 → 52 taken 849 times.
✗ Branch 51 → 56 not taken.
849 next->pack.pattern[1] == op->pack.pattern[1] &&
440
1/2
✓ Branch 52 → 53 taken 849 times.
✗ Branch 52 → 56 not taken.
849 next->pack.pattern[2] == op->pack.pattern[2] &&
441
1/2
✓ Branch 53 → 54 taken 849 times.
✗ Branch 53 → 56 not taken.
849 next->pack.pattern[3] == op->pack.pattern[3])
442 {
443 849 ff_sws_op_list_remove_at(ops, n, 2);
444 849 goto retry;
445 }
446 4808607 break;
447
448 2482729 case SWS_OP_LSHIFT:
449 case SWS_OP_RSHIFT:
450 /* Two shifts in the same direction */
451
2/2
✓ Branch 57 → 58 taken 4155 times.
✓ Branch 57 → 60 taken 2478574 times.
2482729 if (next->op == op->op) {
452 4155 op->shift.amount += next->shift.amount;
453 4155 ff_sws_op_list_remove_at(ops, n + 1, 1);
454 4155 goto retry;
455 }
456
457 /* No-op shift */
458
1/2
✗ Branch 60 → 61 not taken.
✓ Branch 60 → 63 taken 2478574 times.
2478574 if (!op->shift.amount) {
459 ✗ ff_sws_op_list_remove_at(ops, n, 1);
460 ✗ goto retry;
461 }
462 2478574 break;
463
464 9969194 case SWS_OP_CLEAR:
465
2/2
✓ Branch 75 → 65 taken 39876776 times.
✓ Branch 75 → 76 taken 9969194 times.
49845970 for (int i = 0; i < 4; i++) {
466
2/2
✓ Branch 65 → 66 taken 26782215 times.
✓ Branch 65 → 67 taken 13094561 times.
39876776 if (!SWS_COMP_TEST(op->clear.mask, i))
467 26782215 continue;
468
469
1/2
✗ Branch 67 → 68 not taken.
✓ Branch 67 → 71 taken 13094561 times.
13094561 if ((prev->outputs[i].flags & SWS_COMP_ZERO) &&
470
0/2
✗ Branch 68 → 69 not taken.
✗ Branch 68 → 71 not taken.
✗ prev->outputs[i].type == op->clear.type[i] &&
471
0/2
✗ Branch 69 → 70 not taken.
✗ Branch 69 → 71 not taken.
✗ op->clear.value[i].num == 0)
472 {
473 /* Redundant clear-to-zero of zero component */
474 ✗ op->clear.mask ^= SWS_COMP(i);
475
2/2
✓ Branch 71 → 72 taken 2075949 times.
✓ Branch 71 → 73 taken 11018612 times.
13094561 } else if (!SWS_OP_NEEDED(op, i)) {
476 /* Unnecessary clear of unused component */
477 2075949 op->clear.mask ^= SWS_COMP(i);
478 } else {
479 11018612 noop = false;
480 }
481 }
482
483
2/2
✓ Branch 76 → 77 taken 1896934 times.
✓ Branch 76 → 79 taken 8072260 times.
9969194 if (noop) {
484 1896934 ff_sws_op_list_remove_at(ops, n, 1);
485 1896934 goto retry;
486 }
487
488 /* Transitive clear */
489
2/2
✓ Branch 79 → 80 taken 8434 times.
✓ Branch 79 → 87 taken 8063826 times.
8072260 if (next->op == SWS_OP_CLEAR) {
490
2/2
✓ Branch 84 → 81 taken 33736 times.
✓ Branch 84 → 85 taken 8434 times.
42170 for (int i = 0; i < 4; i++) {
491
2/2
✓ Branch 81 → 82 taken 8434 times.
✓ Branch 81 → 83 taken 25302 times.
33736 if (SWS_COMP_TEST(next->clear.mask, i)) {
492 8434 op->clear.value[i] = next->clear.value[i];
493 8434 op->clear.type[i] = next->clear.type[i];
494 }
495 }
496 8434 op->clear.mask |= next->clear.mask;
497 8434 ff_sws_op_list_remove_at(ops, n + 1, 1);
498 8434 goto retry;
499 }
500
501 /* Fold into linear op */
502
2/2
✓ Branch 87 → 88 taken 34589 times.
✓ Branch 87 → 107 taken 8029237 times.
8063826 if (next->op == SWS_OP_LINEAR) {
503
2/2
✓ Branch 104 → 89 taken 138356 times.
✓ Branch 104 → 105 taken 34589 times.
172945 for (int j = 0; j < 4; j++) {
504
2/2
✓ Branch 89 → 90 taken 75405 times.
✓ Branch 89 → 91 taken 62951 times.
138356 if (!SWS_COMP_TEST(op->clear.mask, j))
505 75405 continue;
506
507 62951 const AVRational64 x = op->clear.value[j];
508
2/2
✓ Branch 100 → 92 taken 251804 times.
✓ Branch 100 → 101 taken 62951 times.
314755 for (int i = 0; i < 4; i++) {
509
2/2
✓ Branch 92 → 93 taken 188853 times.
✓ Branch 92 → 94 taken 62951 times.
251804 if (!next->lin.m[i][j].num)
510 188853 continue;
511
1/2
✗ Branch 94 → 95 not taken.
✓ Branch 94 → 97 taken 62951 times.
62951 av_assert0(next->lin.type[i] == op->clear.type[j]);
512 62951 const AVRational64 kx = ff_mul_q64(next->lin.m[i][j], x);
513 62951 next->lin.m[i][4] = ff_add_q64(next->lin.m[i][4], kx);
514 62951 next->lin.m[i][j] = Q(0);
515 }
516 }
517
518 34589 ff_sws_op_list_remove_at(ops, n, 1);
519 34589 goto retry;
520 }
521 8029237 break;
522
523 18165946 case SWS_OP_SWIZZLE:
524
2/2
✓ Branch 114 → 109 taken 72663784 times.
✓ Branch 114 → 115 taken 18165946 times.
90829730 for (int i = 0; i < 4; i++) {
525
2/2
✓ Branch 109 → 110 taken 18086747 times.
✓ Branch 109 → 111 taken 54577037 times.
72663784 if (!SWS_OP_NEEDED(op, i))
526 18086747 continue;
527
2/2
✓ Branch 111 → 112 taken 37404337 times.
✓ Branch 111 → 113 taken 17172700 times.
54577037 if (op->swizzle.in[i] != i)
528 37404337 noop = false;
529 }
530
531 /* Identity swizzle */
532
2/2
✓ Branch 115 → 116 taken 2812980 times.
✓ Branch 115 → 118 taken 15352966 times.
18165946 if (noop) {
533 2812980 ff_sws_op_list_remove_at(ops, n, 1);
534 2812980 goto retry;
535 }
536
537 /* Transitive swizzle */
538
2/2
✓ Branch 118 → 119 taken 286156 times.
✓ Branch 118 → 124 taken 15066810 times.
15352966 if (next->op == SWS_OP_SWIZZLE) {
539 286156 const SwsSwizzleOp orig = op->swizzle;
540
2/2
✓ Branch 121 → 120 taken 1144624 times.
✓ Branch 121 → 122 taken 286156 times.
1430780 for (int i = 0; i < 4; i++)
541 1144624 op->swizzle.in[i] = orig.in[next->swizzle.in[i]];
542 286156 ff_sws_op_list_remove_at(ops, n + 1, 1);
543 286156 goto retry;
544 }
545
546 /* Swizzle planes instead of components, if possible */
547
4/4
✓ Branch 124 → 125 taken 2335133 times.
✓ Branch 124 → 139 taken 12731677 times.
✓ Branch 125 → 126 taken 868238 times.
✓ Branch 125 → 139 taken 1466895 times.
15066810 if (prev->op == SWS_OP_READ && prev->rw.mode == SWS_RW_PLANAR) {
548
2/2
✓ Branch 138 → 127 taken 1217302 times.
✓ Branch 138 → 139 taken 233008 times.
1450310 for (int dst = 0; dst < prev->rw.elems; dst++) {
549 1217302 const int src = op->swizzle.in[dst];
550
4/4
✓ Branch 127 → 128 taken 663284 times.
✓ Branch 127 → 137 taken 554018 times.
✓ Branch 128 → 129 taken 635230 times.
✓ Branch 128 → 137 taken 28054 times.
1217302 if (src > dst && src < prev->rw.elems) {
551 635230 FFSWAP(int, ops->plane_src[dst], ops->plane_src[src]);
552
2/2
✓ Branch 135 → 130 taken 2261225 times.
✓ Branch 135 → 136 taken 635230 times.
2896455 for (int i = dst; i < 4; i++) {
553
2/2
✓ Branch 130 → 131 taken 659646 times.
✓ Branch 130 → 132 taken 1601579 times.
2261225 if (op->swizzle.in[i] == dst)
554 659646 op->swizzle.in[i] = src;
555
2/2
✓ Branch 132 → 133 taken 635230 times.
✓ Branch 132 → 134 taken 966349 times.
1601579 else if (op->swizzle.in[i] == src)
556 635230 op->swizzle.in[i] = dst;
557 }
558 635230 goto retry;
559 }
560 }
561 }
562
563
4/4
✓ Branch 139 → 140 taken 2052145 times.
✓ Branch 139 → 147 taken 12379435 times.
✓ Branch 140 → 141 taken 1300483 times.
✓ Branch 140 → 147 taken 751662 times.
14431580 if (next->op == SWS_OP_WRITE && next->rw.mode == SWS_RW_PLANAR) {
564
2/2
✓ Branch 146 → 142 taken 1738508 times.
✓ Branch 146 → 147 taken 365104 times.
2103612 for (int dst = 0; dst < next->rw.elems; dst++) {
565 1738508 const int src = op->swizzle.in[dst];
566
4/4
✓ Branch 142 → 143 taken 1246029 times.
✓ Branch 142 → 145 taken 492479 times.
✓ Branch 143 → 144 taken 935379 times.
✓ Branch 143 → 145 taken 310650 times.
1738508 if (src > dst && src < next->rw.elems) {
567 935379 FFSWAP(int, ops->plane_dst[dst], ops->plane_dst[src]);
568 935379 FFSWAP(int, op->swizzle.in[dst], op->swizzle.in[src]);
569 935379 goto retry;
570 }
571 }
572 }
573 13496201 break;
574
575 20483260 case SWS_OP_CONVERT:
576 /* No-op conversion */
577
2/2
✓ Branch 149 → 150 taken 505240 times.
✓ Branch 149 → 152 taken 19978020 times.
20483260 if (op->convert.to == ff_sws_op_input_type(op)) {
578 505240 ff_sws_op_list_remove_at(ops, n, 1);
579 505240 goto retry;
580 }
581
582 /* Transitive conversion */
583
2/2
✓ Branch 152 → 153 taken 495771 times.
✓ Branch 152 → 155 taken 19482249 times.
19978020 if (next->op == SWS_OP_CONVERT) {
584 495771 op->convert.to = next->convert.to;
585 495771 ff_sws_op_list_remove_at(ops, n + 1, 1);
586 495771 goto retry;
587 }
588 19482249 break;
589
590 7511831 case SWS_OP_MIN:
591
2/2
✓ Branch 165 → 157 taken 30047324 times.
✓ Branch 165 → 166 taken 7511831 times.
37559155 for (int i = 0; i < 4; i++) {
592
4/4
✓ Branch 157 → 158 taken 22406676 times.
✓ Branch 157 → 159 taken 7640648 times.
✓ Branch 158 → 159 taken 1313498 times.
✓ Branch 158 → 160 taken 21093178 times.
30047324 if (!SWS_OP_NEEDED(op, i) || !op->clamp.limit[i].den)
593 8954146 continue;
594
2/2
✓ Branch 161 → 162 taken 2519697 times.
✓ Branch 161 → 163 taken 18573481 times.
21093178 if (ff_cmp_q64(op->clamp.limit[i], prev->outputs[i].max) >= 0)
595 2519697 op->clamp.limit[i] = (AVRational64) {0}; /* no-op */
596 else
597 18573481 noop = false;
598 }
599
600
2/2
✓ Branch 166 → 167 taken 702447 times.
✓ Branch 166 → 169 taken 6809384 times.
7511831 if (noop) {
601 702447 ff_sws_op_list_remove_at(ops, n, 1);
602 702447 goto retry;
603 }
604 6809384 break;
605
606 5181123 case SWS_OP_MAX:
607
2/2
✓ Branch 179 → 171 taken 20724492 times.
✓ Branch 179 → 180 taken 5181123 times.
25905615 for (int i = 0; i < 4; i++) {
608
4/4
✓ Branch 171 → 172 taken 15413130 times.
✓ Branch 171 → 173 taken 5311362 times.
✓ Branch 172 → 173 taken 640173 times.
✓ Branch 172 → 174 taken 14772957 times.
20724492 if (!SWS_OP_NEEDED(op, i) || !op->clamp.limit[i].den)
609 5951535 continue;
610
2/2
✓ Branch 175 → 176 taken 3911862 times.
✓ Branch 175 → 177 taken 10861095 times.
14772957 if (ff_cmp_q64(prev->outputs[i].min, op->clamp.limit[i]) >= 0)
611 3911862 op->clamp.limit[i] = (AVRational64) {0};
612 else
613 10861095 noop = false;
614 }
615
616
2/2
✓ Branch 180 → 181 taken 1191000 times.
✓ Branch 180 → 183 taken 3990123 times.
5181123 if (noop) {
617 1191000 ff_sws_op_list_remove_at(ops, n, 1);
618 1191000 goto retry;
619 }
620 3990123 break;
621
622 8844989 case SWS_OP_DITHER:
623
2/2
✓ Branch 192 → 185 taken 34201988 times.
✓ Branch 192 → 193 taken 7058481 times.
41260469 for (int i = 0; i < 4; i++) {
624
2/2
✓ Branch 185 → 186 taken 10215365 times.
✓ Branch 185 → 187 taken 23986623 times.
34201988 if (op->dither.y_offset[i] < 0)
625 10215365 continue;
626
4/4
✓ Branch 187 → 188 taken 22789251 times.
✓ Branch 187 → 189 taken 1197372 times.
✓ Branch 188 → 189 taken 589136 times.
✓ Branch 188 → 190 taken 22200115 times.
23986623 if (!SWS_OP_NEEDED(op, i) || (prev->outputs[i].flags & SWS_COMP_EXACT)) {
627 1786508 op->dither.y_offset[i] = -1; /* unnecessary dither */
628 1786508 goto retry;
629 } else {
630 22200115 noop = false;
631 }
632 }
633
634
2/2
✓ Branch 193 → 194 taken 57783 times.
✓ Branch 193 → 196 taken 7000698 times.
7058481 if (noop) {
635 57783 ff_sws_op_list_remove_at(ops, n, 1);
636 57783 goto retry;
637 }
638 7000698 break;
639
640 15487880 case SWS_OP_LINEAR: {
641 15487880 const uint32_t mask = ff_sws_linear_mask(&op->lin);
642 SwsSwizzleOp swizzle;
643 SwsClearOp clear;
644 SwsScaleOp scale;
645
646 /* No-op (identity) linear operation */
647
2/2
✓ Branch 198 → 199 taken 179165 times.
✓ Branch 198 → 201 taken 15308715 times.
15487880 if (!mask) {
648 179165 ff_sws_op_list_remove_at(ops, n, 1);
649 7343043 goto retry;
650 }
651
652
2/2
✓ Branch 201 → 202 taken 3576892 times.
✓ Branch 201 → 215 taken 11731823 times.
15308715 if (next->op == SWS_OP_LINEAR) {
653 /* 5x5 matrix multiplication after appending [ 0 0 0 0 1 ] */
654 3576892 const SwsLinearOp m1 = op->lin;
655 3576892 const SwsLinearOp m2 = next->lin;
656
2/2
✓ Branch 212 → 203 taken 14307568 times.
✓ Branch 212 → 213 taken 3576892 times.
17884460 for (int i = 0; i < 4; i++) {
657 14307568 op->lin.type[i] = m2.type[i];
658
2/2
✓ Branch 210 → 204 taken 71537840 times.
✓ Branch 210 → 211 taken 14307568 times.
85845408 for (int j = 0; j < 5; j++) {
659 71537840 AVRational64 sum = Q(0);
660
2/2
✓ Branch 206 → 205 taken 286151360 times.
✓ Branch 206 → 207 taken 71537840 times.
357689200 for (int k = 0; k < 4; k++)
661 286151360 sum = ff_add_q64(sum, ff_mul_q64(m2.m[i][k], m1.m[k][j]));
662
2/2
✓ Branch 207 → 208 taken 14307568 times.
✓ Branch 207 → 209 taken 57230272 times.
71537840 if (j == 4) /* m1.m[4][j] == 1 */
663 14307568 sum = ff_add_q64(sum, m2.m[i][4]);
664 71537840 op->lin.m[i][j] = sum;
665 }
666 }
667 3576892 ff_sws_op_list_remove_at(ops, n + 1, 1);
668 3576892 goto retry;
669 }
670
671 /* Optimize away zero columns */
672
2/2
✓ Branch 224 → 216 taken 46286711 times.
✓ Branch 224 → 225 taken 11041512 times.
57328223 for (int j = 0; j < 4; j++) {
673 46286711 const uint32_t col = SWS_MASK_COL(j);
674
4/4
✓ Branch 216 → 217 taken 2274867 times.
✓ Branch 216 → 218 taken 44011844 times.
✓ Branch 217 → 218 taken 1584556 times.
✓ Branch 217 → 219 taken 690311 times.
46286711 if (!(prev->outputs[j].flags & SWS_COMP_ZERO) || !(mask & col))
675 45596400 continue;
676
2/2
✓ Branch 221 → 220 taken 2761244 times.
✓ Branch 221 → 222 taken 690311 times.
3451555 for (int i = 0; i < 4; i++)
677 2761244 op->lin.m[i][j] = Q(i == j);
678 690311 goto retry;
679 }
680
681 /* Optimize away unused rows */
682
2/2
✓ Branch 234 → 226 taken 43448781 times.
✓ Branch 234 → 235 taken 9501030 times.
52949811 for (int i = 0; i < 4; i++) {
683 43448781 const uint32_t row = SWS_MASK_ROW(i);
684
4/4
✓ Branch 226 → 227 taken 13996361 times.
✓ Branch 226 → 228 taken 29452420 times.
✓ Branch 227 → 228 taken 12455879 times.
✓ Branch 227 → 229 taken 1540482 times.
43448781 if (SWS_OP_NEEDED(op, i) || !(mask & row))
685 41908299 continue;
686
2/2
✓ Branch 231 → 230 taken 7702410 times.
✓ Branch 231 → 232 taken 1540482 times.
9242892 for (int j = 0; j < 5; j++)
687 7702410 op->lin.m[i][j] = Q(i == j);
688 1540482 goto retry;
689 }
690
691 /* Convert constant rows to explicit clear instruction */
692
2/2
✓ Branch 236 → 237 taken 499225 times.
✓ Branch 236 → 241 taken 9001805 times.
9501030 if (extract_constant_rows(&op->lin, prev->outputs, &clear)) {
693
1/2
✗ Branch 238 → 239 not taken.
✓ Branch 238 → 240 taken 499225 times.
499225 RET(ff_sws_op_list_insert_at(ops, n + 1, &(SwsOp) {
694 .op = SWS_OP_CLEAR,
695 .clear = clear,
696 }));
697 499225 goto retry;
698 }
699
700 /* Multiplication by scalar constant */
701
2/2
✓ Branch 242 → 243 taken 693488 times.
✓ Branch 242 → 244 taken 8308317 times.
9001805 if (extract_scalar(&op->lin, op->outputs, prev->outputs, &scale)) {
702 693488 op->op = SWS_OP_SCALE;
703 693488 op->scale = scale;
704 693488 goto retry;
705 }
706
707 /* Swizzle by fixed pattern */
708
2/2
✓ Branch 245 → 246 taken 163480 times.
✓ Branch 245 → 250 taken 8144837 times.
8308317 if (extract_swizzle(&op->lin, prev->outputs, &swizzle)) {
709
1/2
✗ Branch 247 → 248 not taken.
✓ Branch 247 → 249 taken 163480 times.
163480 RET(ff_sws_op_list_insert_at(ops, n, &(SwsOp) {
710 .op = SWS_OP_SWIZZLE,
711 .swizzle = swizzle,
712 }));
713 163480 goto retry;
714 }
715 8144837 break;
716 }
717
718 6475627 case SWS_OP_SCALE: {
719 6475627 const int factor2 = exact_log2_q64(op->scale.factor);
720
721 /* No-op scaling */
722
3/4
✓ Branch 255 → 256 taken 1211647 times.
✓ Branch 255 → 259 taken 5263980 times.
✗ Branch 256 → 257 not taken.
✓ Branch 256 → 259 taken 1211647 times.
6475627 if (op->scale.factor.num == 1 && op->scale.factor.den == 1) {
723 ✗ ff_sws_op_list_remove_at(ops, n, 1);
724 ✗ goto retry;
725 }
726
727 /* Merge consecutive scaling operations */
728
1/2
✗ Branch 259 → 260 not taken.
✓ Branch 259 → 262 taken 6475627 times.
6475627 if (next->op == SWS_OP_SCALE) {
729 ✗ op->scale.factor = ff_mul_q64(op->scale.factor, next->scale.factor);
730 ✗ ff_sws_op_list_remove_at(ops, n + 1, 1);
731 ✗ goto retry;
732 }
733
734 /* Scaling by exact power of two */
735
4/4
✓ Branch 262 → 263 taken 1042703 times.
✓ Branch 262 → 269 taken 5432924 times.
✓ Branch 264 → 265 taken 20174 times.
✓ Branch 264 → 269 taken 1022529 times.
6475627 if (factor2 && ff_sws_pixel_type_is_int(ff_sws_op_input_type(op))) {
736
1/2
✓ Branch 265 → 266 taken 20174 times.
✗ Branch 265 → 267 not taken.
20174 op->op = factor2 > 0 ? SWS_OP_LSHIFT : SWS_OP_RSHIFT;
737 20174 op->shift.amount = FFABS(factor2);
738 20174 goto retry;
739 }
740 6455453 break;
741 }
742
743 14098056 case SWS_OP_FILTER_H:
744 case SWS_OP_FILTER_V:
745 /* Merge with prior simple planar read */
746
4/4
✓ Branch 270 → 271 taken 4202350 times.
✓ Branch 270 → 277 taken 9895706 times.
✓ Branch 271 → 272 taken 3159506 times.
✓ Branch 271 → 277 taken 1042844 times.
14098056 if (prev->op == SWS_OP_READ && !prev->rw.filter.op &&
747
4/4
✓ Branch 272 → 273 taken 1267653 times.
✓ Branch 272 → 277 taken 1891853 times.
✓ Branch 273 → 274 taken 1131270 times.
✓ Branch 273 → 277 taken 136383 times.
3159506 prev->rw.mode == SWS_RW_PLANAR && !prev->rw.frac) {
748 1131270 prev->rw.filter.op = op->op;
749 1131270 prev->rw.filter.kernel = av_refstruct_ref(op->filter.kernel);
750 1131270 prev->rw.filter.type = op->filter.type;
751 1131270 ff_sws_op_list_remove_at(ops, n, 1);
752 1131270 goto retry;
753 }
754 12966786 break;
755
756 2347968 case SWS_OP_LUT_3D:
757 /* Eliminate unnecessary 3DLUT */
758
2/2
✓ Branch 278 → 279 taken 1476 times.
✓ Branch 278 → 281 taken 2346492 times.
2347968 if (!(needed & SWS_COMP_ELEMS(3))) {
759 1476 ff_sws_op_list_remove_at(ops, n, 1);
760 1476 goto retry;
761 }
762 2346492 break;
763 }
764 }
765
766 /* Push clears to the back to void any unused components */
767
2/2
✓ Branch 293 → 287 taken 53625810 times.
✓ Branch 293 → 294 taken 5853439 times.
59479249 for (int n = 0; n < ops->num_ops - 1; n++) {
768 53625810 SwsOp *op = &ops->ops[n];
769 53625810 SwsOp *next = &ops->ops[n + 1];
770
771
2/2
✓ Branch 287 → 288 taken 3486176 times.
✓ Branch 287 → 292 taken 50139634 times.
53625810 switch (op->op) {
772 3486176 case SWS_OP_CLEAR:
773
2/2
✓ Branch 289 → 290 taken 1763425 times.
✓ Branch 289 → 291 taken 1722751 times.
3486176 if (op_commute_clear(op, next)) {
774 1763425 FFSWAP(SwsOp, *op, *next);
775 1763425 goto retry;
776 }
777 1722751 break;
778 }
779 }
780
781 /* Apply any remaining preferential re-ordering optimizations; do these
782 * last because they are more likely to block other optimizations if done
783 * too aggressively */
784
2/2
✓ Branch 317 → 295 taken 34211980 times.
✓ Branch 317 → 318 taken 3868207 times.
38080187 for (int n = 0; n < ops->num_ops - 1; n++) {
785 34211980 SwsOp *op = &ops->ops[n];
786 34211980 SwsOp *next = &ops->ops[n + 1];
787
788
4/4
✓ Branch 295 → 296 taken 3959567 times.
✓ Branch 295 → 300 taken 2017599 times.
✓ Branch 295 → 307 taken 2334094 times.
✓ Branch 295 → 316 taken 25900720 times.
34211980 switch (op->op) {
789 3959567 case SWS_OP_SWIZZLE: {
790 /* Try to push swizzles towards the output */
791
2/2
✓ Branch 297 → 298 taken 1929709 times.
✓ Branch 297 → 299 taken 2029858 times.
3959567 if (op_commute_swizzle(op, next)) {
792 1929709 FFSWAP(SwsOp, *op, *next);
793 1929709 goto retry;
794 }
795 2029858 break;
796 }
797
798 2017599 case SWS_OP_SCALE:
799 /* Exact integer multiplication */
800
4/4
✓ Branch 300 → 301 taken 499480 times.
✓ Branch 300 → 306 taken 1518119 times.
✓ Branch 301 → 302 taken 68060 times.
✓ Branch 301 → 306 taken 431420 times.
2017599 if (op->scale.factor.den == 1 && next->op == SWS_OP_CONVERT &&
801
3/4
✓ Branch 302 → 303 taken 68060 times.
✗ Branch 302 → 306 not taken.
✓ Branch 304 → 305 taken 37402 times.
✓ Branch 304 → 306 taken 30658 times.
136120 ff_sws_pixel_type_is_int(next->convert.to) &&
802 68060 op_result_is_exact(op))
803 {
804 37402 FFSWAP(SwsOp, *op, *next);
805 37402 goto retry;
806 }
807 1980197 break;
808
809 2334094 case SWS_OP_LINEAR:
810 /* Exact integer linear transformation */
811
2/2
✓ Branch 307 → 308 taken 329000 times.
✓ Branch 307 → 315 taken 2005094 times.
2334094 if (next->op == SWS_OP_CONVERT &&
812
3/4
✓ Branch 308 → 309 taken 329000 times.
✗ Branch 308 → 315 not taken.
✓ Branch 310 → 311 taken 18121 times.
✓ Branch 310 → 315 taken 310879 times.
658000 ff_sws_pixel_type_is_int(next->convert.to) &&
813 329000 op_result_is_exact(op))
814 {
815
2/2
✓ Branch 313 → 312 taken 72484 times.
✓ Branch 313 → 314 taken 18121 times.
90605 for (int i = 0; i < 4; i++)
816 72484 op->lin.type[i] = next->convert.to;
817 18121 FFSWAP(SwsOp, *op, *next);
818 18121 goto retry;
819 }
820 2315973 break;
821 }
822 }
823
824 3868207 return 0;
825 }
826
827 328872 static int select_planes(SwsOpList *ops, SwsCompMask planes)
828 {
829 328872 SwsSwizzleOp swiz = SWS_SWIZZLE(0, 1, 2, 3);
830 328872 SwsOp *write = &ops->ops[ops->num_ops - 1];
831
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 328872 times.
328872 av_assert0(write->op == SWS_OP_WRITE);
832
833 328872 write->rw.elems = 0;
834
2/2
✓ Branch 10 → 6 taken 1315488 times.
✓ Branch 10 → 11 taken 328872 times.
1644360 for (int src = 0; src < 4; src++) {
835
2/2
✓ Branch 6 → 7 taken 680687 times.
✓ Branch 6 → 8 taken 634801 times.
1315488 if (!SWS_COMP_TEST(planes, src))
836 680687 continue; /* plane not selected */
837 634801 const int dst = write->rw.elems++;
838 av_assert2(src >= dst);
839 634801 swiz.in[dst] = src;
840 634801 FFSWAP(int, ops->plane_dst[dst], ops->plane_dst[src]);
841 }
842
843 /* Insert swizzle to select desired planes */
844 328872 int ret = ff_sws_op_list_insert_at(ops, ops->num_ops - 1, &(SwsOp) {
845 .op = SWS_OP_SWIZZLE,
846 .swizzle = swiz,
847 });
848
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 328872 times.
328872 if (ret < 0)
849 ✗ return ret;
850
851 /* The optimizer will take care of the rest */
852 328872 return ff_sws_op_list_optimize(ops);
853 }
854
855 3601061 int ff_sws_op_list_split_planes(SwsOpList *ops1, SwsOpList **out_ops2, SwsCompMask planes)
856 {
857 3601061 const SwsOp *write = ff_sws_op_list_output(ops1);
858
3/4
✓ Branch 3 → 4 taken 3601061 times.
✗ Branch 3 → 5 not taken.
✓ Branch 4 → 5 taken 1163194 times.
✓ Branch 4 → 6 taken 2437867 times.
3601061 if (!write || write->rw.mode != SWS_RW_PLANAR) {
859 1163194 *out_ops2 = NULL;
860 1163194 return 0;
861 }
862
863 2437867 const SwsCompMask full = SWS_COMP_ELEMS(write->rw.elems);
864 2437867 const SwsCompMask mask1 = planes & full;
865 2437867 const SwsCompMask mask2 = full ^ mask1;
866
4/4
✓ Branch 6 → 7 taken 330539 times.
✓ Branch 6 → 8 taken 2107328 times.
✓ Branch 7 → 8 taken 166103 times.
✓ Branch 7 → 9 taken 164436 times.
2437867 if (!mask1 || !mask2) {
867 /* Nothing to filter */
868 2273431 *out_ops2 = NULL;
869 2273431 return 0;
870 }
871
872 164436 SwsOpList *ops2 = ff_sws_op_list_duplicate(ops1);
873
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 164436 times.
164436 if (!ops2)
874 ✗ return AVERROR(ENOMEM);
875
876 int ret;
877
2/4
✓ Branch 13 → 14 taken 164436 times.
✗ Branch 13 → 16 not taken.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 164436 times.
328872 if ((ret = select_planes(ops1, mask1)) < 0 ||
878 164436 (ret = select_planes(ops2, mask2)) < 0)
879 {
880 ✗ ff_sws_op_list_free(&ops2);
881 ✗ return ret;
882 }
883
884 164436 *out_ops2 = ops2;
885 164436 return 0;
886 }
887
888 77688 int ff_sws_shuffle_mask(const SwsUOp *uop, int8_t shuffle[], int size)
889 {
890 77688 const SwsShuffleUOp *par = &uop->par.shuffle;
891 av_assert1(uop->uop == SWS_UOP_RW_SHUFFLE);
892 av_assert1(par->write_size <= sizeof(uop->data.shuffle.mask));
893 av_assert1(size <= INT8_MAX);
894
895 77688 const int num_groups = size / FFMAX(par->read_size, par->write_size);
896
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 77688 times.
77688 if (!num_groups)
897 ✗ return AVERROR(EINVAL);
898
899 77688 memset(shuffle, 0, size);
900
2/2
✓ Branch 9 → 5 taken 386991 times.
✓ Branch 9 → 10 taken 77688 times.
464679 for (int n = 0; n < num_groups; n++) {
901 386991 const int base_in = n * par->read_size;
902 386991 const int base_out = n * par->write_size;
903
2/2
✓ Branch 7 → 6 taken 1073886 times.
✓ Branch 7 → 8 taken 386991 times.
1460877 for (int i = 0; i < par->write_size; i++) {
904 1073886 const int8_t idx = uop->data.shuffle.mask[i];
905 1073886 shuffle[base_out + i] = idx + (idx >= 0) * base_in;
906 }
907 }
908
909 77688 return num_groups;
910 }
911
912 4634 static bool pixel_is_repeating(SwsPixelType type, SwsPixel val)
913 {
914
3/4
✓ Branch 2 → 3 taken 2213 times.
✓ Branch 2 → 4 taken 1993 times.
✓ Branch 2 → 5 taken 428 times.
✗ Branch 2 → 6 not taken.
4634 switch (ff_sws_pixel_type_size(type)) {
915 2213 case 1: return true;
916 1993 case 2: return val.u16 == val.u8 * 0x101ul;
917 428 case 4: return val.u32 == val.u8 * 0x1010101ul;
918 ✗ default: break;
919 }
920
921 ✗ av_unreachable("Invalid pixel type!");
922 return false;
923 }
924
925 2032425 static int solve_shuffle(const SwsUOpList *const uops, SwsUOp *out)
926 {
927
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2032425 times.
2032425 if (!uops->num_ops)
928 ✗ return AVERROR(EINVAL);
929 2032425 const SwsUOp *read = &uops->ops[0];
930
3/3
✓ Branch 4 → 5 taken 340021 times.
✓ Branch 4 → 6 taken 629043 times.
✓ Branch 4 → 9 taken 1063361 times.
2032425 switch (read->uop) {
931 340021 case SWS_UOP_READ_PACKED:
932 340021 break;
933 629043 case SWS_UOP_READ_PLANAR:
934
2/2
✓ Branch 6 → 7 taken 322282 times.
✓ Branch 6 → 8 taken 306761 times.
629043 if (read->mask != SWS_COMP_ELEMS(1))
935 322282 return AVERROR(ENOTSUP);
936 306761 break;
937 1063361 default:
938 1063361 return AVERROR(ENOTSUP);
939 }
940
941 646782 const int read_size = ff_sws_pixel_type_size(read->type);
942 646782 uint32_t mask[4] = {0};
943 646782 int clear_val = -1;
944 646782 int read_elems = 0;
945
2/2
✓ Branch 14 → 11 taken 2587128 times.
✓ Branch 14 → 15 taken 646782 times.
3233910 for (int i = 0; i < 4; i++) {
946
2/2
✓ Branch 11 → 12 taken 1494330 times.
✓ Branch 11 → 13 taken 1092798 times.
2587128 if (SWS_COMP_TEST(read->mask, i)) {
947 1494330 mask[i] = 0x01010101 * i * read_size + 0x03020100;
948 1494330 read_elems++;
949 }
950 }
951
952
1/2
✓ Branch 67 → 16 taken 950635 times.
✗ Branch 67 → 68 not taken.
950635 for (int opidx = 1; opidx < uops->num_ops; opidx++) {
953 950635 const SwsUOp *uop = &uops->ops[opidx];
954 950635 const SwsUOpParams *par = &uop->par;
955
7/7
✓ Branch 16 → 17 taken 41797 times.
✓ Branch 16 → 28 taken 254058 times.
✓ Branch 16 → 35 taken 4625 times.
✓ Branch 16 → 48 taken 3953 times.
✓ Branch 16 → 52 taken 227309 times.
✓ Branch 16 → 54 taken 9244 times.
✓ Branch 16 → 65 taken 409649 times.
950635 switch (uop->uop) {
956 41797 case SWS_UOP_COPY:
957 case SWS_UOP_PERMUTE: {
958 uint32_t tmp;
959
2/2
✓ Branch 25 → 18 taken 60554 times.
✓ Branch 25 → 26 taken 41797 times.
102351 for (int i = 0; i < par->move.num_moves; i++) {
960 60554 const int dst_idx = par->move.dst[i];
961 60554 const int src_idx = par->move.src[i];
962
2/2
✓ Branch 18 → 19 taken 4013 times.
✓ Branch 18 → 20 taken 56541 times.
60554 uint32_t *src = src_idx < 0 ? &tmp : &mask[src_idx];
963
2/2
✓ Branch 21 → 22 taken 4013 times.
✓ Branch 21 → 23 taken 56541 times.
60554 uint32_t *dst = dst_idx < 0 ? &tmp : &mask[dst_idx];
964 60554 *dst = *src;
965 }
966 41797 break;
967 }
968
969 254058 case SWS_UOP_SWAP_BYTES:
970
2/2
✓ Branch 33 → 29 taken 1016232 times.
✓ Branch 33 → 34 taken 254058 times.
1270290 for (int i = 0; i < 4; i++) {
971
2/3
✓ Branch 29 → 30 taken 715120 times.
✓ Branch 29 → 31 taken 301112 times.
✗ Branch 29 → 32 not taken.
1016232 switch (ff_sws_pixel_type_size(uop->type)) {
972 715120 case 2: mask[i] = av_bswap16(mask[i]); break;
973 301112 case 4: mask[i] = av_bswap32(mask[i]); break;
974 }
975 }
976 254058 break;
977
978 4625 case SWS_UOP_CLEAR:
979
2/2
✓ Branch 46 → 36 taken 18300 times.
✓ Branch 46 → 47 taken 4045 times.
22345 for (int i = 0; i < 4; i++) {
980
2/2
✓ Branch 36 → 37 taken 13666 times.
✓ Branch 36 → 38 taken 4634 times.
18300 if (!SWS_COMP_TEST(uop->mask, i))
981 13666 continue;
982 4634 SwsPixel val = uop->data.vec4[i];
983
4/4
✓ Branch 39 → 40 taken 4054 times.
✓ Branch 39 → 42 taken 580 times.
✓ Branch 40 → 41 taken 9 times.
✓ Branch 40 → 43 taken 4045 times.
4634 if (!pixel_is_repeating(uop->type, val) ||
984
1/2
✗ Branch 41 → 42 not taken.
✓ Branch 41 → 43 taken 9 times.
9 (clear_val >= 0 && clear_val != val.u8))
985 580 return AVERROR(ENOTSUP); /* would require different bytes */
986 4054 mask[i] = 0xFFFFFFFFul; /* (uint8_t[4]) { -1, -1, -1, -1 } */
987 4054 clear_val = val.u8;
988 }
989 4045 break;
990
991 3953 case SWS_UOP_EXPAND_PAIR:
992 case SWS_UOP_EXPAND_QUAD:
993
2/2
✓ Branch 50 → 49 taken 15812 times.
✓ Branch 50 → 51 taken 3953 times.
19765 for (int i = 0; i < 4; i++)
994 15812 mask[i] = 0x01010101 * (mask[i] & 0xFF);
995 3953 break;
996
997 227309 case SWS_UOP_WRITE_PLANAR:
998
2/2
✓ Branch 52 → 53 taken 170443 times.
✓ Branch 52 → 54 taken 56866 times.
227309 if (uop->mask != SWS_COMP_ELEMS(1))
999 170443 return AVERROR(ENOTSUP);
1000 av_fallthrough;
1001 case SWS_UOP_WRITE_PACKED: {
1002 66110 const int write_elems = av_popcount(uop->mask);
1003 66110 const int write_size = ff_sws_pixel_type_size(uop->type);
1004 66110 *out = (SwsUOp) {
1005 .uop = SWS_UOP_RW_SHUFFLE,
1006 .type = SWS_PIXEL_U8,
1007 .mask = SWS_COMP_ELEMS(1), /* single plane for now */
1008 };
1009
1010 66110 SwsShuffleUOp *par = &out->par.shuffle;
1011 66110 SwsShuffleMask *data = &out->data.shuffle;
1012 66110 *par = (SwsShuffleUOp) {
1013 .read_size = read_elems * read_size,
1014 .write_size = write_elems * write_size,
1015 66110 .clear_value = clear_val >= 0 ? clear_val : 0,
1016 };
1017
1018 /* Generate baseline shuffle for a single pixel */
1019 66110 data->pixels = 1;
1020
2/2
✓ Branch 59 → 55 taken 89684 times.
✓ Branch 59 → 60 taken 66110 times.
155794 for (int i = 0; i < write_elems; i++) {
1021 89684 const int offset = i * write_size;
1022
2/2
✓ Branch 57 → 56 taken 189250 times.
✓ Branch 57 → 58 taken 89684 times.
278934 for (int b = 0; b < write_size; b++)
1023 189250 data->mask[offset + b] = mask[i] >> (b * 8);
1024 }
1025
1026 /* Expand as many times as needed to round up to the size of the
1027 * shuffle uop data mask */
1028 int8_t tmp[FF_ARRAY_ELEMS(data->mask)];
1029 66110 const int num_groups = ff_sws_shuffle_mask(out, tmp, sizeof(tmp));
1030
1/2
✗ Branch 61 → 62 not taken.
✓ Branch 61 → 63 taken 66110 times.
66110 if (num_groups < 0)
1031 ✗ return num_groups;
1032 66110 memcpy(data->mask, tmp, sizeof(tmp));
1033 66110 par->read_size *= num_groups;
1034 66110 par->write_size *= num_groups;
1035 66110 data->pixels = num_groups;
1036 66110 return 0;
1037 }
1038
1039 409649 default:
1040 409649 return AVERROR(ENOTSUP);
1041 }
1042 }
1043
1044 ✗ return AVERROR(EINVAL);
1045 }
1046
1047 /* Only checks exact integer scale ops */
1048 823877 static bool is_scale(const SwsUOp *op, int64_t val)
1049 {
1050
2/2
✓ Branch 2 → 3 taken 88817 times.
✓ Branch 2 → 4 taken 735060 times.
823877 if (op->uop != SWS_UOP_SCALE)
1051 88817 return false;
1052
1053
4/4
✓ Branch 4 → 5 taken 2009 times.
✓ Branch 4 → 6 taken 12681 times.
✓ Branch 4 → 7 taken 1344 times.
✓ Branch 4 → 8 taken 719026 times.
735060 switch (op->type) {
1054 2009 case SWS_PIXEL_U8: return op->data.scalar.u8 == val;
1055 12681 case SWS_PIXEL_U16: return op->data.scalar.u16 == val;
1056 1344 case SWS_PIXEL_U32: return op->data.scalar.u32 == val;
1057 719026 default: return false;
1058 }
1059 }
1060
1061 3179420 int ff_sws_uop_list_optimize(SwsContext *ctx, SwsUOpFlags flags, SwsUOpList *uops)
1062 {
1063 static const SwsUOp dummy = {0};
1064
1065 7567 retry:
1066
2/2
✓ Branch 27 → 5 taken 18102690 times.
✓ Branch 27 → 28 taken 3179420 times.
21282110 for (int i = 0; i < uops->num_ops; i++) {
1067
2/2
✓ Branch 5 → 6 taken 14923270 times.
✓ Branch 5 → 7 taken 3179420 times.
18102690 const SwsUOp *next = i < uops->num_ops - 1 ? &uops->ops[i + 1] : &dummy;
1068 18102690 SwsUOp *op = &uops->ops[i];
1069
1070
4/4
✓ Branch 8 → 9 taken 1142202 times.
✓ Branch 8 → 15 taken 109028 times.
✓ Branch 8 → 21 taken 745688 times.
✓ Branch 8 → 26 taken 16105772 times.
18102690 switch (op->uop) {
1071 1142202 case SWS_UOP_TO_U16:
1072
4/4
✓ Branch 9 → 10 taken 93893 times.
✓ Branch 9 → 14 taken 1048309 times.
✓ Branch 11 → 12 taken 6564 times.
✓ Branch 11 → 14 taken 87329 times.
1142202 if (ff_sws_pixel_type_is_int(op->type) && is_scale(next, 0x101)) {
1073 6564 op->uop = SWS_UOP_EXPAND_PAIR;
1074 6564 ff_sws_uop_list_remove_at(uops, i + 1, 1);
1075 6564 goto retry;
1076 }
1077 1135638 break;
1078
1079 109028 case SWS_UOP_TO_U32:
1080
3/4
✓ Branch 15 → 16 taken 3880 times.
✓ Branch 15 → 20 taken 105148 times.
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 20 taken 3880 times.
109028 if (ff_sws_pixel_type_is_int(op->type) && is_scale(next, 0x1010101)) {
1081 ✗ op->uop = SWS_UOP_EXPAND_QUAD;
1082 ✗ ff_sws_uop_list_remove_at(uops, i + 1, 1);
1083 ✗ goto retry;
1084 }
1085 109028 break;
1086
1087 745688 case SWS_UOP_SCALE:
1088
2/2
✓ Branch 21 → 22 taken 726104 times.
✓ Branch 21 → 25 taken 19584 times.
745688 if (flags & SWS_UOP_FLAG_EXPAND_BIT) {
1089 726104 const int bits = 8 * ff_sws_pixel_type_size(op->type);
1090
2/2
✓ Branch 23 → 24 taken 1003 times.
✓ Branch 23 → 25 taken 725101 times.
726104 if (is_scale(op, UINT64_MAX >> (64 - bits))) {
1091 1003 op->uop = SWS_UOP_EXPAND_BIT;
1092 1003 memset(&op->par, 0, sizeof(op->par));
1093 1003 goto retry;
1094 }
1095 }
1096 744685 break;
1097 }
1098 }
1099
1100 /* Try promoting the entire uop list to a packed shuffle operation */
1101
2/2
✓ Branch 28 → 29 taken 2032425 times.
✓ Branch 28 → 39 taken 1146995 times.
3179420 if (flags & SWS_UOP_FLAG_PSHUFB) {
1102 SwsUOp shuffle;
1103 2032425 int ret = solve_shuffle(uops, &shuffle);
1104
2/2
✓ Branch 30 → 31 taken 66110 times.
✓ Branch 30 → 34 taken 1966315 times.
2032425 if (ret >= 0) {
1105 66110 ff_sws_uop_list_remove_at(uops, 0, uops->num_ops);
1106 66110 return ff_sws_uop_list_append(uops, &shuffle);
1107
2/4
✓ Branch 34 → 35 taken 1966315 times.
✗ Branch 34 → 37 not taken.
✗ Branch 35 → 36 not taken.
✓ Branch 35 → 37 taken 1966315 times.
1966315 } else if (ret < 0 && ret != AVERROR(ENOTSUP)) {
1108 ✗ return ret;
1109 }
1110 }
1111
1112 3113310 return 0;
1113 }
1114
1115 /**
1116 * Determine a suitable intermediate buffer format for a given combination
1117 * of pixel types and number of planes. The exact interpretation of these
1118 * formats does not matter at all; since they will only ever be used as
1119 * temporary intermediate buffers. We still need to pick *some* format as
1120 * a consequence of ff_sws_graph_add_pass() taking an AVPixelFormat for the
1121 * output buffer.
1122 */
1123 886701 static enum AVPixelFormat get_planar_fmt(SwsPixelType type, int nb_planes)
1124 {
1125
3/4
✓ Branch 2 → 3 taken 220527 times.
✓ Branch 2 → 9 taken 328782 times.
✓ Branch 2 → 15 taken 337392 times.
✗ Branch 2 → 21 not taken.
886701 switch (ff_sws_pixel_type_size(type)) {
1126 220527 case 1:
1127
4/5
✓ Branch 3 → 4 taken 18627 times.
✓ Branch 3 → 5 taken 2666 times.
✓ Branch 3 → 6 taken 182447 times.
✓ Branch 3 → 7 taken 16787 times.
✗ Branch 3 → 8 not taken.
220527 switch (nb_planes) {
1128 18627 case 1: return AV_PIX_FMT_GRAY8;
1129 2666 case 2: return AV_PIX_FMT_YUV444P; // FIXME: no 2-plane planar fmt
1130 182447 case 3: return AV_PIX_FMT_YUV444P;
1131 16787 case 4: return AV_PIX_FMT_YUVA444P;
1132 }
1133 ✗ break;
1134 328782 case 2:
1135
4/5
✓ Branch 9 → 10 taken 51636 times.
✓ Branch 9 → 11 taken 5416 times.
✓ Branch 9 → 12 taken 242142 times.
✓ Branch 9 → 13 taken 29588 times.
✗ Branch 9 → 14 not taken.
328782 switch (nb_planes) {
1136 51636 case 1: return AV_PIX_FMT_GRAY16;
1137 5416 case 2: return AV_PIX_FMT_YUV444P16; // FIXME: no 2-plane planar fmt
1138 242142 case 3: return AV_PIX_FMT_YUV444P16;
1139 29588 case 4: return AV_PIX_FMT_YUVA444P16;
1140 }
1141 ✗ break;
1142 337392 case 4:
1143
4/5
✓ Branch 15 → 16 taken 61020 times.
✓ Branch 15 → 17 taken 10261 times.
✓ Branch 15 → 18 taken 231084 times.
✓ Branch 15 → 19 taken 35027 times.
✗ Branch 15 → 20 not taken.
337392 switch (nb_planes) {
1144 61020 case 1: return AV_PIX_FMT_GRAYF32;
1145 10261 case 2: return AV_PIX_FMT_GBRPF32; // FIXME: no 2-plane planar fmt
1146 231084 case 3: return AV_PIX_FMT_GBRPF32;
1147 35027 case 4: return AV_PIX_FMT_GBRAPF32;
1148 }
1149 ✗ break;
1150 }
1151
1152 ✗ av_unreachable("Invalid pixel type or number of planes?");
1153 return AV_PIX_FMT_NONE;
1154 }
1155
1156 886701 static void get_input_size(const SwsOpList *ops, SwsFormat *fmt)
1157 {
1158 886701 fmt->width = ops->src.width;
1159 886701 fmt->height = ops->src.height;
1160
1161 886701 const SwsOp *read = ff_sws_op_list_input(ops);
1162
2/4
✓ Branch 3 → 4 taken 886701 times.
✗ Branch 3 → 6 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 886701 times.
886701 if (read && read->rw.filter.op == SWS_OP_FILTER_V) {
1163 ✗ fmt->height = read->rw.filter.kernel->dst_size;
1164
3/4
✓ Branch 6 → 7 taken 886701 times.
✗ Branch 6 → 9 not taken.
✓ Branch 7 → 8 taken 278637 times.
✓ Branch 7 → 9 taken 608064 times.
886701 } else if (read && read->rw.filter.op == SWS_OP_FILTER_H) {
1165 278637 fmt->width = read->rw.filter.kernel->dst_size;
1166 }
1167 886701 }
1168
1169 886701 int ff_sws_op_list_split_at(SwsOpList *ops1, SwsOpList **out_ops2, int index)
1170 {
1171 int ret;
1172
2/4
✓ Branch 2 → 3 taken 886701 times.
✗ Branch 2 → 4 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 886701 times.
886701 if (index <= 0 || index >= ops1->num_ops) {
1173 ✗ *out_ops2 = NULL;
1174 ✗ return 0;
1175 }
1176
1177 886701 const SwsOp *op = &ops1->ops[index];
1178 886701 const SwsOp *prev = &ops1->ops[index - 1];
1179
1180 886701 SwsOpList *ops2 = ff_sws_op_list_duplicate(ops1);
1181
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 886701 times.
886701 if (!ops2)
1182 ✗ return AVERROR(ENOMEM);
1183
1184 /**
1185 * Not all components may be needed; but we need the ones that *are*
1186 * used to be contiguous for the write/read operations. So, first
1187 * compress them into a linearly ascending list of components
1188 */
1189 886701 int nb_planes = 0;
1190 886701 SwsSwizzleOp swiz_wr = SWS_SWIZZLE(0, 1, 2, 3);
1191 886701 SwsSwizzleOp swiz_rd = SWS_SWIZZLE(0, 1, 2, 3);
1192
2/2
✓ Branch 12 → 9 taken 3546804 times.
✓ Branch 12 → 13 taken 886701 times.
4433505 for (int i = 0; i < 4; i++) {
1193
2/2
✓ Branch 9 → 10 taken 2460596 times.
✓ Branch 9 → 11 taken 1086208 times.
3546804 if (SWS_OP_NEEDED(prev, i)) {
1194 2460596 const int o = nb_planes++;
1195 2460596 swiz_wr.in[o] = i;
1196 2460596 swiz_rd.in[i] = o;
1197 }
1198 }
1199
1200 /* Determine metadata for the intermediate format */
1201 886701 const SwsPixelType type = ff_sws_op_input_type(op);
1202 886701 ops2->src.format = get_planar_fmt(type, nb_planes);
1203 886701 ops2->src.desc = av_pix_fmt_desc_get(ops2->src.format);
1204 886701 ops2->src.field = FIELD_TOP;
1205 886701 ops2->src.interlaced = 0;
1206 886701 get_input_size(ops1, &ops2->src);
1207 886701 ops1->dst = ops2->src;
1208
1209
2/2
✓ Branch 19 → 18 taken 2460596 times.
✓ Branch 19 → 20 taken 886701 times.
3347297 for (int i = 0; i < nb_planes; i++) {
1210 2460596 const int idx = swiz_wr.in[i];
1211 2460596 ops1->plane_dst[i] = ops2->plane_src[i] = i;
1212 2460596 ops2->inputs[i].flags = prev->outputs[idx].flags;
1213 2460596 ops2->inputs[i].min = prev->outputs[idx].min;
1214 2460596 ops2->inputs[i].max = prev->outputs[idx].max;
1215 }
1216
1217 886701 ff_sws_op_list_remove_at(ops1, index, ops1->num_ops - index);
1218 886701 ff_sws_op_list_remove_at(ops2, 0, index);
1219 886701 op = NULL; /* the above command may invalidate op */
1220
1221
2/2
✓ Branch 22 → 23 taken 73940 times.
✓ Branch 22 → 27 taken 812761 times.
886701 if (swiz_wr.mask != SWS_SWIZZLE(0, 1, 2, 3).mask) {
1222 73940 ret = ff_sws_op_list_append(ops1, &(SwsOp) {
1223 .op = SWS_OP_SWIZZLE,
1224 .swizzle = swiz_wr,
1225 });
1226
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 73940 times.
73940 if (ret < 0)
1227 ✗ goto fail;
1228 }
1229
1230 886701 ret = ff_sws_op_list_append(ops1, &(SwsOp) {
1231 .op = SWS_OP_WRITE,
1232 .rw.type = type,
1233 .rw.elems = nb_planes,
1234 });
1235
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 886701 times.
886701 if (ret < 0)
1236 ✗ goto fail;
1237
1238 886701 ret = ff_sws_op_list_insert_at(ops2, 0, &(SwsOp) {
1239 .op = SWS_OP_READ,
1240 .rw.type = type,
1241 .rw.elems = nb_planes,
1242 });
1243
1/2
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 33 taken 886701 times.
886701 if (ret < 0)
1244 ✗ goto fail;
1245
1246
2/2
✓ Branch 33 → 34 taken 73940 times.
✓ Branch 33 → 38 taken 812761 times.
886701 if (swiz_rd.mask != SWS_SWIZZLE(0, 1, 2, 3).mask) {
1247 73940 ret = ff_sws_op_list_insert_at(ops2, 1, &(SwsOp) {
1248 .op = SWS_OP_SWIZZLE,
1249 .swizzle = swiz_rd,
1250 });
1251
1/2
✗ Branch 35 → 36 not taken.
✓ Branch 35 → 37 taken 73940 times.
73940 if (ret < 0)
1252 ✗ goto fail;
1253 }
1254
1255 886701 ret = ff_sws_op_list_optimize(ops1);
1256
1/2
✗ Branch 39 → 40 not taken.
✓ Branch 39 → 41 taken 886701 times.
886701 if (ret < 0)
1257 ✗ goto fail;
1258
1259 886701 ret = ff_sws_op_list_optimize(ops2);
1260
1/2
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 44 taken 886701 times.
886701 if (ret < 0)
1261 ✗ goto fail;
1262
1263 886701 *out_ops2 = ops2;
1264 886701 return 0;
1265
1266 ✗ fail:
1267 ✗ ff_sws_op_list_free(&ops2);
1268 ✗ return ret;
1269 }
1270