FFmpeg
Loading...
Searching...
No Matches
ops_optimizer.c
Go to the documentation of this file.
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
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 */
41static bool op_commute_clear(SwsOp *op, SwsOp *next)
42{
44 switch (next->op) {
45 case SWS_OP_CONVERT:
46 for (int i = 0; i < 4; i++)
47 op->clear.type[i] = next->convert.to;
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 ff_sws_apply_op_q(next, op->clear.value);
57 return true;
58 case SWS_OP_FILTER_H:
59 case SWS_OP_FILTER_V:
60 for (int i = 0; i < 4; i++)
61 op->clear.type[i] = next->filter.type;
62 return true;
63 case SWS_OP_SWIZZLE: {
64 const SwsClearOp orig = op->clear;
65 for (int i = 0; i < 4; i++)
66 op->clear.type[i] = orig.type[next->swizzle.in[i]];
67 ff_sws_comp_mask_swizzle(&op->clear.mask, &next->swizzle);
68 ff_sws_apply_op_q(next, op->clear.value);
69 return true;
70 }
72 switch (ff_sws_op_input_type(next)) {
73 case SWS_PIXEL_U16:
74 case SWS_PIXEL_U32:
75 ff_sws_apply_op_q(next, op->clear.value); /* always representable */
76 return true;
77 default:
78 return false;
79 }
80 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 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 */
102static bool op_commute_swizzle(SwsOp *op, SwsOp *next)
103{
104 bool seen[4] = {0};
105
107 switch (next->op) {
108 case SWS_OP_CONVERT:
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 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 case SWS_OP_MIN:
127 case SWS_OP_MAX: {
128 const SwsClampOp c = next->clamp;
129 for (int i = 0; i < 4; i++) {
130 if (!SWS_OP_NEEDED(op, i))
131 continue;
132 const int j = op->swizzle.in[i];
133 if (seen[j] && ff_cmp_q64(next->clamp.limit[j], c.limit[i]))
134 return false;
135 next->clamp.limit[j] = c.limit[i];
136 seen[j] = true;
137 }
138 return true;
139 }
140
141 case SWS_OP_DITHER: {
142 const SwsDitherOp d = next->dither;
143 for (int i = 0; i < 4; i++) {
144 if (!SWS_OP_NEEDED(op, i))
145 continue;
146 const int j = op->swizzle.in[i];
147 if (seen[j] && next->dither.y_offset[j] != d.y_offset[i])
148 return false;
149 next->dither.y_offset[j] = d.y_offset[i];
150 seen[j] = true;
151 }
152 return true;
153 }
154
155 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 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 */
180static bool op_commute_filter(SwsOp *op, SwsOp *prev)
181{
182 av_assert0(!ff_sws_pixel_type_is_int(op->filter.type));
183
184 switch (prev->op) {
185 case SWS_OP_SWIZZLE:
186 case SWS_OP_SCALE:
187 case SWS_OP_DITHER:
188 return true;
189 case SWS_OP_LINEAR:
190 for (int i = 0; i < 4; i++)
191 prev->lin.type[i] = op->filter.type;
192 return true;
193 case SWS_OP_CONVERT:
194 case SWS_OP_INVALID:
195 case SWS_OP_READ:
196 case SWS_OP_WRITE:
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 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 */
218static int exact_log2(const int x)
219{
220 int p;
221 if (x <= 0)
222 return 0;
223 p = av_log2(x);
224 return (1 << p) == x ? p : 0;
225}
226
227static int exact_log2_q64(const AVRational64 x)
228{
229 if (x.den == 1)
230 return exact_log2(x.num);
231 else if (x.num == 1)
232 return -exact_log2(x.den);
233 else
234 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 */
241static bool extract_scalar(const SwsLinearOp *c,
242 const SwsOpPad outputs[4], const SwsOpPad inputs[4],
243 SwsScaleOp *out_scale)
244{
245 SwsScaleOp scale = {0};
246
247 /* There are components not on the main diagonal */
249 return false;
250
251 for (int i = 0; i < 4; i++) {
252 const AVRational64 s = c->m[i][i];
253 if ((inputs[i].flags & SWS_COMP_ZERO) || !outputs[i].dep_out)
254 continue;
255 if (scale.factor.den && ff_cmp_q64(s, scale.factor))
256 return false;
257 scale.factor = s;
258 }
259
260 if (scale.factor.den)
261 *out_scale = scale;
262 return scale.factor.den;
263}
264
265/* Extracts an integer clear operation (subset) from the given linear op. */
267 SwsClearOp *out_clear)
268{
269 const uint32_t mask = ff_sws_linear_mask(c);
270 SwsClearOp clear = {0};
271 bool ret = false;
272
273 for (int i = 0; i < 4; i++) {
274 bool const_row = c->m[i][4].den == 1; /* offset is integer */
275 for (int j = 0; j < 4; j++) {
276 const_row &= c->m[i][j].num == 0 || /* scalar is zero */
277 (inputs[j].flags & SWS_COMP_ZERO); /* input is zero */
278 }
279 if (const_row && (mask & SWS_MASK_ROW(i))) {
280 clear.mask |= SWS_COMP(i);
281 clear.value[i] = c->m[i][4];
282 clear.type[i] = c->type[i];
283 for (int j = 0; j < 5; j++)
284 c->m[i][j] = Q(i == j);
285 ret = true;
286 }
287 }
288
289 if (ret)
290 *out_clear = clear;
291 return ret;
292}
293
294/* Unswizzle a linear operation by aligning single-input rows with
295 * their corresponding diagonal */
297 SwsSwizzleOp *out_swiz)
298{
299 SwsSwizzleOp swiz = SWS_SWIZZLE(0, 1, 2, 3);
300 SwsLinearOp c = *op;
301
302 /* Find non-zero coefficients in the main 4x4 matrix */
303 uint32_t nonzero = 0;
304 for (int i = 0; i < 4; i++) {
305 for (int j = 0; j < 4; j++) {
306 if (!c.m[i][j].num || (inputs[j].flags & SWS_COMP_ZERO))
307 continue;
308 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 for (int i = 0; i < 4; i++) {
315 if (nonzero & SWS_MASK_COL(i))
316 continue; /* target column is not empty */
317 for (int j = 0; j < 4; j++) {
318 if ((nonzero & SWS_MASK_ROW(i)) == SWS_MASK(i, j)) {
319 /* Move coefficient to the diagonal */
320 c.m[i][i] = c.m[i][j];
321 c.m[i][j] = Q(0);
322 swiz.in[i] = j;
323 break;
324 }
325 }
326 }
327
328 if (swiz.mask == SWS_SWIZZLE(0, 1, 2, 3).mask)
329 return false; /* no swizzle was identified */
330
331 *out_swiz = swiz;
332 *op = c;
333 return true;
334}
335
336static int op_result_is_exact(const SwsOp *op)
337{
338 for (int i = 0; i < 4; i++) {
339 if (SWS_OP_NEEDED(op, i) && !(op->outputs[i].flags & SWS_COMP_EXACT))
340 return false;
341 }
342
343 return true;
344}
345
347{
348 int ret;
349
350retry:
352
353 /* Try to push filters towards the input; do this first to unblock
354 * in-place optimizations like linear op fusion */
355 for (int n = 1; n < ops->num_ops; n++) {
356 SwsOp *op = &ops->ops[n];
357 SwsOp *prev = &ops->ops[n - 1];
358
359 switch (op->op) {
360 case SWS_OP_FILTER_H:
361 case SWS_OP_FILTER_V:
362 if (op_commute_filter(op, prev)) {
363 FFSWAP(SwsOp, *op, *prev);
364 goto retry;
365 }
366
367 /* Merge filter with prior conversion */
368 if (prev->op == SWS_OP_CONVERT) {
369 const SwsPixelType input_type = ff_sws_op_input_type(prev);
370 int size_from = ff_sws_pixel_type_size(input_type);
371 int size_to = ff_sws_pixel_type_size(prev->convert.to);
372 if (size_from < size_to) {
373 ff_sws_op_list_remove_at(ops, n - 1, 1);
374 goto retry;
375 }
376 }
377 break;
378 }
379 }
380
381 /* Apply all in-place optimizations (that do not re-order the list) */
382 for (int n = 0; n < ops->num_ops; n++) {
383 SwsOp dummy = {0};
384 SwsOp *op = &ops->ops[n];
385 SwsOp *prev = n ? &ops->ops[n - 1] : &dummy;
386 SwsOp *next = n + 1 < ops->num_ops ? &ops->ops[n + 1] : &dummy;
387
388 /* common helper variable */
389 const SwsCompMask needed = ff_sws_comp_mask_needed(op);
390 bool noop = true;
391
392 if (!needed && op->op != SWS_OP_WRITE) {
393 /* Remove any operation whose output is not needed */
394 ff_sws_op_list_remove_at(ops, n, 1);
395 goto retry;
396 }
397
398 switch (op->op) {
399 case SWS_OP_READ:
400 /* "Compress" planar reads where not all components are needed */
401 if (op->rw.mode == SWS_RW_PLANAR) {
402 SwsSwizzleOp swiz = SWS_SWIZZLE(0, 1, 2, 3);
403 int nb_planes = 0;
404 for (int i = 0; i < op->rw.elems; i++) {
405 if (!SWS_OP_NEEDED(op, i)) {
406 swiz.in[i] = 3 - (i - nb_planes); /* map to unused plane */
407 continue;
408 }
409
410 const int idx = nb_planes++;
411 av_assert1(idx <= i);
412 ops->plane_src[idx] = ops->plane_src[i];
413 swiz.in[i] = idx;
414 }
415
416 if (nb_planes < op->rw.elems) {
417 op->rw.elems = nb_planes;
418 RET(ff_sws_op_list_insert_at(ops, n + 1, &(SwsOp) {
419 .op = SWS_OP_SWIZZLE,
420 .swizzle = swiz,
421 }));
422 goto retry;
423 }
424 }
425 break;
426
428 /* Redundant (double) swap */
429 if (next->op == SWS_OP_SWAP_BYTES) {
430 ff_sws_op_list_remove_at(ops, n, 2);
431 goto retry;
432 }
433 break;
434
435 case SWS_OP_UNPACK:
436 /* Redundant unpack+pack */
437 if (next->op == SWS_OP_PACK &&
438 next->pack.pattern[0] == op->pack.pattern[0] &&
439 next->pack.pattern[1] == op->pack.pattern[1] &&
440 next->pack.pattern[2] == op->pack.pattern[2] &&
441 next->pack.pattern[3] == op->pack.pattern[3])
442 {
443 ff_sws_op_list_remove_at(ops, n, 2);
444 goto retry;
445 }
446 break;
447
448 case SWS_OP_LSHIFT:
449 case SWS_OP_RSHIFT:
450 /* Two shifts in the same direction */
451 if (next->op == op->op) {
452 op->shift.amount += next->shift.amount;
453 ff_sws_op_list_remove_at(ops, n + 1, 1);
454 goto retry;
455 }
456
457 /* No-op shift */
458 if (!op->shift.amount) {
459 ff_sws_op_list_remove_at(ops, n, 1);
460 goto retry;
461 }
462 break;
463
464 case SWS_OP_CLEAR:
465 for (int i = 0; i < 4; i++) {
466 if (!SWS_COMP_TEST(op->clear.mask, i))
467 continue;
468
469 if ((prev->outputs[i].flags & SWS_COMP_ZERO) &&
470 prev->outputs[i].type == op->clear.type[i] &&
471 op->clear.value[i].num == 0)
472 {
473 /* Redundant clear-to-zero of zero component */
474 op->clear.mask ^= SWS_COMP(i);
475 } else if (!SWS_OP_NEEDED(op, i)) {
476 /* Unnecessary clear of unused component */
477 op->clear.mask ^= SWS_COMP(i);
478 } else {
479 noop = false;
480 }
481 }
482
483 if (noop) {
484 ff_sws_op_list_remove_at(ops, n, 1);
485 goto retry;
486 }
487
488 /* Transitive clear */
489 if (next->op == SWS_OP_CLEAR) {
490 for (int i = 0; i < 4; i++) {
491 if (SWS_COMP_TEST(next->clear.mask, i)) {
492 op->clear.value[i] = next->clear.value[i];
493 op->clear.type[i] = next->clear.type[i];
494 }
495 }
496 op->clear.mask |= next->clear.mask;
497 ff_sws_op_list_remove_at(ops, n + 1, 1);
498 goto retry;
499 }
500
501 /* Fold into linear op */
502 if (next->op == SWS_OP_LINEAR) {
503 for (int j = 0; j < 4; j++) {
504 if (!SWS_COMP_TEST(op->clear.mask, j))
505 continue;
506
507 const AVRational64 x = op->clear.value[j];
508 for (int i = 0; i < 4; i++) {
509 if (!next->lin.m[i][j].num)
510 continue;
511 av_assert0(next->lin.type[i] == op->clear.type[j]);
512 const AVRational64 kx = ff_mul_q64(next->lin.m[i][j], x);
513 next->lin.m[i][4] = ff_add_q64(next->lin.m[i][4], kx);
514 next->lin.m[i][j] = Q(0);
515 }
516 }
517
518 ff_sws_op_list_remove_at(ops, n, 1);
519 goto retry;
520 }
521 break;
522
523 case SWS_OP_SWIZZLE:
524 for (int i = 0; i < 4; i++) {
525 if (!SWS_OP_NEEDED(op, i))
526 continue;
527 if (op->swizzle.in[i] != i)
528 noop = false;
529 }
530
531 /* Identity swizzle */
532 if (noop) {
533 ff_sws_op_list_remove_at(ops, n, 1);
534 goto retry;
535 }
536
537 /* Transitive swizzle */
538 if (next->op == SWS_OP_SWIZZLE) {
539 const SwsSwizzleOp orig = op->swizzle;
540 for (int i = 0; i < 4; i++)
541 op->swizzle.in[i] = orig.in[next->swizzle.in[i]];
542 ff_sws_op_list_remove_at(ops, n + 1, 1);
543 goto retry;
544 }
545
546 /* Swizzle planes instead of components, if possible */
547 if (prev->op == SWS_OP_READ && prev->rw.mode == SWS_RW_PLANAR) {
548 for (int dst = 0; dst < prev->rw.elems; dst++) {
549 const int src = op->swizzle.in[dst];
550 if (src > dst && src < prev->rw.elems) {
551 FFSWAP(int, ops->plane_src[dst], ops->plane_src[src]);
552 for (int i = dst; i < 4; i++) {
553 if (op->swizzle.in[i] == dst)
554 op->swizzle.in[i] = src;
555 else if (op->swizzle.in[i] == src)
556 op->swizzle.in[i] = dst;
557 }
558 goto retry;
559 }
560 }
561 }
562
563 if (next->op == SWS_OP_WRITE && next->rw.mode == SWS_RW_PLANAR) {
564 for (int dst = 0; dst < next->rw.elems; dst++) {
565 const int src = op->swizzle.in[dst];
566 if (src > dst && src < next->rw.elems) {
567 FFSWAP(int, ops->plane_dst[dst], ops->plane_dst[src]);
568 FFSWAP(int, op->swizzle.in[dst], op->swizzle.in[src]);
569 goto retry;
570 }
571 }
572 }
573 break;
574
575 case SWS_OP_CONVERT:
576 /* No-op conversion */
577 if (op->convert.to == ff_sws_op_input_type(op)) {
578 ff_sws_op_list_remove_at(ops, n, 1);
579 goto retry;
580 }
581
582 /* Transitive conversion */
583 if (next->op == SWS_OP_CONVERT) {
584 op->convert.to = next->convert.to;
585 ff_sws_op_list_remove_at(ops, n + 1, 1);
586 goto retry;
587 }
588 break;
589
590 case SWS_OP_MIN:
591 for (int i = 0; i < 4; i++) {
592 if (!SWS_OP_NEEDED(op, i) || !op->clamp.limit[i].den)
593 continue;
594 if (ff_cmp_q64(op->clamp.limit[i], prev->outputs[i].max) >= 0)
595 op->clamp.limit[i] = (AVRational64) {0}; /* no-op */
596 else
597 noop = false;
598 }
599
600 if (noop) {
601 ff_sws_op_list_remove_at(ops, n, 1);
602 goto retry;
603 }
604 break;
605
606 case SWS_OP_MAX:
607 for (int i = 0; i < 4; i++) {
608 if (!SWS_OP_NEEDED(op, i) || !op->clamp.limit[i].den)
609 continue;
610 if (ff_cmp_q64(prev->outputs[i].min, op->clamp.limit[i]) >= 0)
611 op->clamp.limit[i] = (AVRational64) {0};
612 else
613 noop = false;
614 }
615
616 if (noop) {
617 ff_sws_op_list_remove_at(ops, n, 1);
618 goto retry;
619 }
620 break;
621
622 case SWS_OP_DITHER:
623 for (int i = 0; i < 4; i++) {
624 if (op->dither.y_offset[i] < 0)
625 continue;
626 if (!SWS_OP_NEEDED(op, i) || (prev->outputs[i].flags & SWS_COMP_EXACT)) {
627 op->dither.y_offset[i] = -1; /* unnecessary dither */
628 goto retry;
629 } else {
630 noop = false;
631 }
632 }
633
634 if (noop) {
635 ff_sws_op_list_remove_at(ops, n, 1);
636 goto retry;
637 }
638 break;
639
640 case SWS_OP_LINEAR: {
641 const uint32_t mask = ff_sws_linear_mask(&op->lin);
642 SwsSwizzleOp swizzle;
643 SwsClearOp clear;
645
646 /* No-op (identity) linear operation */
647 if (!mask) {
648 ff_sws_op_list_remove_at(ops, n, 1);
649 goto retry;
650 }
651
652 if (next->op == SWS_OP_LINEAR) {
653 /* 5x5 matrix multiplication after appending [ 0 0 0 0 1 ] */
654 const SwsLinearOp m1 = op->lin;
655 const SwsLinearOp m2 = next->lin;
656 for (int i = 0; i < 4; i++) {
657 op->lin.type[i] = m2.type[i];
658 for (int j = 0; j < 5; j++) {
659 AVRational64 sum = Q(0);
660 for (int k = 0; k < 4; k++)
661 sum = ff_add_q64(sum, ff_mul_q64(m2.m[i][k], m1.m[k][j]));
662 if (j == 4) /* m1.m[4][j] == 1 */
663 sum = ff_add_q64(sum, m2.m[i][4]);
664 op->lin.m[i][j] = sum;
665 }
666 }
667 ff_sws_op_list_remove_at(ops, n + 1, 1);
668 goto retry;
669 }
670
671 /* Optimize away zero columns */
672 for (int j = 0; j < 4; j++) {
673 const uint32_t col = SWS_MASK_COL(j);
674 if (!(prev->outputs[j].flags & SWS_COMP_ZERO) || !(mask & col))
675 continue;
676 for (int i = 0; i < 4; i++)
677 op->lin.m[i][j] = Q(i == j);
678 goto retry;
679 }
680
681 /* Optimize away unused rows */
682 for (int i = 0; i < 4; i++) {
683 const uint32_t row = SWS_MASK_ROW(i);
684 if (SWS_OP_NEEDED(op, i) || !(mask & row))
685 continue;
686 for (int j = 0; j < 5; j++)
687 op->lin.m[i][j] = Q(i == j);
688 goto retry;
689 }
690
691 /* Convert constant rows to explicit clear instruction */
692 if (extract_constant_rows(&op->lin, prev->outputs, &clear)) {
693 RET(ff_sws_op_list_insert_at(ops, n + 1, &(SwsOp) {
694 .op = SWS_OP_CLEAR,
695 .clear = clear,
696 }));
697 goto retry;
698 }
699
700 /* Multiplication by scalar constant */
701 if (extract_scalar(&op->lin, op->outputs, prev->outputs, &scale)) {
702 op->op = SWS_OP_SCALE;
703 op->scale = scale;
704 goto retry;
705 }
706
707 /* Swizzle by fixed pattern */
708 if (extract_swizzle(&op->lin, prev->outputs, &swizzle)) {
710 .op = SWS_OP_SWIZZLE,
711 .swizzle = swizzle,
712 }));
713 goto retry;
714 }
715 break;
716 }
717
718 case SWS_OP_SCALE: {
719 const int factor2 = exact_log2_q64(op->scale.factor);
720
721 /* No-op scaling */
722 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 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 */
736 op->op = factor2 > 0 ? SWS_OP_LSHIFT : SWS_OP_RSHIFT;
737 op->shift.amount = FFABS(factor2);
738 goto retry;
739 }
740 break;
741 }
742
743 case SWS_OP_FILTER_H:
744 case SWS_OP_FILTER_V:
745 /* Merge with prior simple planar read */
746 if (prev->op == SWS_OP_READ && !prev->rw.filter.op &&
747 prev->rw.mode == SWS_RW_PLANAR && !prev->rw.frac) {
748 prev->rw.filter.op = op->op;
749 prev->rw.filter.kernel = av_refstruct_ref(op->filter.kernel);
750 prev->rw.filter.type = op->filter.type;
751 ff_sws_op_list_remove_at(ops, n, 1);
752 goto retry;
753 }
754 break;
755
756 case SWS_OP_LUT_3D:
757 /* Eliminate unnecessary 3DLUT */
758 if (!(needed & SWS_COMP_ELEMS(3))) {
759 ff_sws_op_list_remove_at(ops, n, 1);
760 goto retry;
761 }
762 break;
763 }
764 }
765
766 /* Push clears to the back to void any unused components */
767 for (int n = 0; n < ops->num_ops - 1; n++) {
768 SwsOp *op = &ops->ops[n];
769 SwsOp *next = &ops->ops[n + 1];
770
771 switch (op->op) {
772 case SWS_OP_CLEAR:
773 if (op_commute_clear(op, next)) {
774 FFSWAP(SwsOp, *op, *next);
775 goto retry;
776 }
777 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 for (int n = 0; n < ops->num_ops - 1; n++) {
785 SwsOp *op = &ops->ops[n];
786 SwsOp *next = &ops->ops[n + 1];
787
788 switch (op->op) {
789 case SWS_OP_SWIZZLE: {
790 /* Try to push swizzles towards the output */
791 if (op_commute_swizzle(op, next)) {
792 FFSWAP(SwsOp, *op, *next);
793 goto retry;
794 }
795 break;
796 }
797
798 case SWS_OP_SCALE:
799 /* Exact integer multiplication */
800 if (op->scale.factor.den == 1 && next->op == SWS_OP_CONVERT &&
803 {
804 FFSWAP(SwsOp, *op, *next);
805 goto retry;
806 }
807 break;
808
809 case SWS_OP_LINEAR:
810 /* Exact integer linear transformation */
811 if (next->op == SWS_OP_CONVERT &&
814 {
815 for (int i = 0; i < 4; i++)
816 op->lin.type[i] = next->convert.to;
817 FFSWAP(SwsOp, *op, *next);
818 goto retry;
819 }
820 break;
821 }
822 }
823
824 return 0;
825}
826
828{
829 SwsSwizzleOp swiz = SWS_SWIZZLE(0, 1, 2, 3);
830 SwsOp *write = &ops->ops[ops->num_ops - 1];
831 av_assert0(write->op == SWS_OP_WRITE);
832
833 write->rw.elems = 0;
834 for (int src = 0; src < 4; src++) {
835 if (!SWS_COMP_TEST(planes, src))
836 continue; /* plane not selected */
837 const int dst = write->rw.elems++;
838 av_assert2(src >= dst);
839 swiz.in[dst] = src;
840 FFSWAP(int, ops->plane_dst[dst], ops->plane_dst[src]);
841 }
842
843 /* Insert swizzle to select desired planes */
844 int ret = ff_sws_op_list_insert_at(ops, ops->num_ops - 1, &(SwsOp) {
845 .op = SWS_OP_SWIZZLE,
846 .swizzle = swiz,
847 });
848 if (ret < 0)
849 return ret;
850
851 /* The optimizer will take care of the rest */
852 return ff_sws_op_list_optimize(ops);
853}
854
856{
857 const SwsOp *write = ff_sws_op_list_output(ops1);
858 if (!write || write->rw.mode != SWS_RW_PLANAR) {
859 *out_ops2 = NULL;
860 return 0;
861 }
862
863 const SwsCompMask full = SWS_COMP_ELEMS(write->rw.elems);
864 const SwsCompMask mask1 = planes & full;
865 const SwsCompMask mask2 = full ^ mask1;
866 if (!mask1 || !mask2) {
867 /* Nothing to filter */
868 *out_ops2 = NULL;
869 return 0;
870 }
871
873 if (!ops2)
874 return AVERROR(ENOMEM);
875
876 int ret;
877 if ((ret = select_planes(ops1, mask1)) < 0 ||
878 (ret = select_planes(ops2, mask2)) < 0)
879 {
880 ff_sws_op_list_free(&ops2);
881 return ret;
882 }
883
884 *out_ops2 = ops2;
885 return 0;
886}
887
888int ff_sws_shuffle_mask(const SwsUOp *uop, int8_t shuffle[], int size)
889{
890 const SwsShuffleUOp *par = &uop->par.shuffle;
892 av_assert1(par->write_size <= sizeof(uop->data.shuffle.mask));
893 av_assert1(size <= INT8_MAX);
894
895 const int num_groups = size / FFMAX(par->read_size, par->write_size);
896 if (!num_groups)
897 return AVERROR(EINVAL);
898
899 memset(shuffle, 0, size);
900 for (int n = 0; n < num_groups; n++) {
901 const int base_in = n * par->read_size;
902 const int base_out = n * par->write_size;
903 for (int i = 0; i < par->write_size; i++) {
904 const int8_t idx = uop->data.shuffle.mask[i];
905 shuffle[base_out + i] = idx + (idx >= 0) * base_in;
906 }
907 }
908
909 return num_groups;
910}
911
913{
914 switch (ff_sws_pixel_type_size(type)) {
915 case 1: return true;
916 case 2: return val.u16 == val.u8 * 0x101ul;
917 case 4: return val.u32 == val.u8 * 0x1010101ul;
918 default: break;
919 }
920
921 av_unreachable("Invalid pixel type!");
922 return false;
923}
924
925static int solve_shuffle(const SwsUOpList *const uops, SwsUOp *out)
926{
927 if (!uops->num_ops)
928 return AVERROR(EINVAL);
929 const SwsUOp *read = &uops->ops[0];
930 switch (read->uop) {
932 break;
934 if (read->mask != SWS_COMP_ELEMS(1))
935 return AVERROR(ENOTSUP);
936 break;
937 default:
938 return AVERROR(ENOTSUP);
939 }
940
941 const int read_size = ff_sws_pixel_type_size(read->type);
942 uint32_t mask[4] = {0};
943 int clear_val = -1;
944 int read_elems = 0;
945 for (int i = 0; i < 4; i++) {
946 if (SWS_COMP_TEST(read->mask, i)) {
947 mask[i] = 0x01010101 * i * read_size + 0x03020100;
948 read_elems++;
949 }
950 }
951
952 for (int opidx = 1; opidx < uops->num_ops; opidx++) {
953 const SwsUOp *uop = &uops->ops[opidx];
954 const SwsUOpParams *par = &uop->par;
955 switch (uop->uop) {
956 case SWS_UOP_COPY:
957 case SWS_UOP_PERMUTE: {
958 uint32_t tmp;
959 for (int i = 0; i < par->move.num_moves; i++) {
960 const int dst_idx = par->move.dst[i];
961 const int src_idx = par->move.src[i];
962 uint32_t *src = src_idx < 0 ? &tmp : &mask[src_idx];
963 uint32_t *dst = dst_idx < 0 ? &tmp : &mask[dst_idx];
964 *dst = *src;
965 }
966 break;
967 }
968
970 for (int i = 0; i < 4; i++) {
971 switch (ff_sws_pixel_type_size(uop->type)) {
972 case 2: mask[i] = av_bswap16(mask[i]); break;
973 case 4: mask[i] = av_bswap32(mask[i]); break;
974 }
975 }
976 break;
977
978 case SWS_UOP_CLEAR:
979 for (int i = 0; i < 4; i++) {
980 if (!SWS_COMP_TEST(uop->mask, i))
981 continue;
982 SwsPixel val = uop->data.vec4[i];
983 if (!pixel_is_repeating(uop->type, val) ||
984 (clear_val >= 0 && clear_val != val.u8))
985 return AVERROR(ENOTSUP); /* would require different bytes */
986 mask[i] = 0xFFFFFFFFul; /* (uint8_t[4]) { -1, -1, -1, -1 } */
987 clear_val = val.u8;
988 }
989 break;
990
993 for (int i = 0; i < 4; i++)
994 mask[i] = 0x01010101 * (mask[i] & 0xFF);
995 break;
996
998 if (uop->mask != SWS_COMP_ELEMS(1))
999 return AVERROR(ENOTSUP);
1001 case SWS_UOP_WRITE_PACKED: {
1002 const int write_elems = av_popcount(uop->mask);
1003 const int write_size = ff_sws_pixel_type_size(uop->type);
1004 *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 SwsShuffleUOp *par = &out->par.shuffle;
1011 SwsShuffleMask *data = &out->data.shuffle;
1012 *par = (SwsShuffleUOp) {
1013 .read_size = read_elems * read_size,
1014 .write_size = write_elems * write_size,
1015 .clear_value = clear_val >= 0 ? clear_val : 0,
1016 };
1017
1018 /* Generate baseline shuffle for a single pixel */
1019 data->pixels = 1;
1020 for (int i = 0; i < write_elems; i++) {
1021 const int offset = i * write_size;
1022 for (int b = 0; b < write_size; b++)
1023 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 const int num_groups = ff_sws_shuffle_mask(out, tmp, sizeof(tmp));
1030 if (num_groups < 0)
1031 return num_groups;
1032 memcpy(data->mask, tmp, sizeof(tmp));
1033 par->read_size *= num_groups;
1034 par->write_size *= num_groups;
1035 data->pixels = num_groups;
1036 return 0;
1037 }
1038
1039 default:
1040 return AVERROR(ENOTSUP);
1041 }
1042 }
1043
1044 return AVERROR(EINVAL);
1045}
1046
1047/* Only checks exact integer scale ops */
1048static bool is_scale(const SwsUOp *op, int64_t val)
1049{
1050 if (op->uop != SWS_UOP_SCALE)
1051 return false;
1052
1053 switch (op->type) {
1054 case SWS_PIXEL_U8: return op->data.scalar.u8 == val;
1055 case SWS_PIXEL_U16: return op->data.scalar.u16 == val;
1056 case SWS_PIXEL_U32: return op->data.scalar.u32 == val;
1057 default: return false;
1058 }
1059}
1060
1062{
1063 static const SwsUOp dummy = {0};
1064
1065retry:
1066 for (int i = 0; i < uops->num_ops; i++) {
1067 const SwsUOp *next = i < uops->num_ops - 1 ? &uops->ops[i + 1] : &dummy;
1068 SwsUOp *op = &uops->ops[i];
1069
1070 switch (op->uop) {
1071 case SWS_UOP_TO_U16:
1072 if (ff_sws_pixel_type_is_int(op->type) && is_scale(next, 0x101)) {
1073 op->uop = SWS_UOP_EXPAND_PAIR;
1074 ff_sws_uop_list_remove_at(uops, i + 1, 1);
1075 goto retry;
1076 }
1077 break;
1078
1079 case SWS_UOP_TO_U32:
1080 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 break;
1086
1087 case SWS_UOP_SCALE:
1089 const int bits = 8 * ff_sws_pixel_type_size(op->type);
1090 if (is_scale(op, UINT64_MAX >> (64 - bits))) {
1091 op->uop = SWS_UOP_EXPAND_BIT;
1092 memset(&op->par, 0, sizeof(op->par));
1093 goto retry;
1094 }
1095 }
1096 break;
1097 }
1098 }
1099
1100 /* Try promoting the entire uop list to a packed shuffle operation */
1101 if (flags & SWS_UOP_FLAG_PSHUFB) {
1103 int ret = solve_shuffle(uops, &shuffle);
1104 if (ret >= 0) {
1105 ff_sws_uop_list_remove_at(uops, 0, uops->num_ops);
1106 return ff_sws_uop_list_append(uops, &shuffle);
1107 } else if (ret < 0 && ret != AVERROR(ENOTSUP)) {
1108 return ret;
1109 }
1110 }
1111
1112 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 */
1124{
1125 switch (ff_sws_pixel_type_size(type)) {
1126 case 1:
1127 switch (nb_planes) {
1128 case 1: return AV_PIX_FMT_GRAY8;
1129 case 2: return AV_PIX_FMT_YUV444P; // FIXME: no 2-plane planar fmt
1130 case 3: return AV_PIX_FMT_YUV444P;
1131 case 4: return AV_PIX_FMT_YUVA444P;
1132 }
1133 break;
1134 case 2:
1135 switch (nb_planes) {
1136 case 1: return AV_PIX_FMT_GRAY16;
1137 case 2: return AV_PIX_FMT_YUV444P16; // FIXME: no 2-plane planar fmt
1138 case 3: return AV_PIX_FMT_YUV444P16;
1139 case 4: return AV_PIX_FMT_YUVA444P16;
1140 }
1141 break;
1142 case 4:
1143 switch (nb_planes) {
1144 case 1: return AV_PIX_FMT_GRAYF32;
1145 case 2: return AV_PIX_FMT_GBRPF32; // FIXME: no 2-plane planar fmt
1146 case 3: return AV_PIX_FMT_GBRPF32;
1147 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
1156static void get_input_size(const SwsOpList *ops, SwsFormat *fmt)
1157{
1158 fmt->width = ops->src.width;
1159 fmt->height = ops->src.height;
1160
1161 const SwsOp *read = ff_sws_op_list_input(ops);
1162 if (read && read->rw.filter.op == SWS_OP_FILTER_V) {
1163 fmt->height = read->rw.filter.kernel->dst_size;
1164 } else if (read && read->rw.filter.op == SWS_OP_FILTER_H) {
1165 fmt->width = read->rw.filter.kernel->dst_size;
1166 }
1167}
1168
1170{
1171 int ret;
1172 if (index <= 0 || index >= ops1->num_ops) {
1173 *out_ops2 = NULL;
1174 return 0;
1175 }
1176
1177 const SwsOp *op = &ops1->ops[index];
1178 const SwsOp *prev = &ops1->ops[index - 1];
1179
1180 SwsOpList *ops2 = ff_sws_op_list_duplicate(ops1);
1181 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 int nb_planes = 0;
1190 SwsSwizzleOp swiz_wr = SWS_SWIZZLE(0, 1, 2, 3);
1191 SwsSwizzleOp swiz_rd = SWS_SWIZZLE(0, 1, 2, 3);
1192 for (int i = 0; i < 4; i++) {
1193 if (SWS_OP_NEEDED(prev, i)) {
1194 const int o = nb_planes++;
1195 swiz_wr.in[o] = i;
1196 swiz_rd.in[i] = o;
1197 }
1198 }
1199
1200 /* Determine metadata for the intermediate format */
1202 ops2->src.format = get_planar_fmt(type, nb_planes);
1203 ops2->src.desc = av_pix_fmt_desc_get(ops2->src.format);
1204 ops2->src.field = FIELD_TOP;
1205 ops2->src.interlaced = 0;
1206 get_input_size(ops1, &ops2->src);
1207 ops1->dst = ops2->src;
1208
1209 for (int i = 0; i < nb_planes; i++) {
1210 const int idx = swiz_wr.in[i];
1211 ops1->plane_dst[i] = ops2->plane_src[i] = i;
1212 ops2->inputs[i].flags = prev->outputs[idx].flags;
1213 ops2->inputs[i].min = prev->outputs[idx].min;
1214 ops2->inputs[i].max = prev->outputs[idx].max;
1215 }
1216
1219 op = NULL; /* the above command may invalidate op */
1220
1221 if (swiz_wr.mask != SWS_SWIZZLE(0, 1, 2, 3).mask) {
1222 ret = ff_sws_op_list_append(ops1, &(SwsOp) {
1223 .op = SWS_OP_SWIZZLE,
1224 .swizzle = swiz_wr,
1225 });
1226 if (ret < 0)
1227 goto fail;
1228 }
1229
1230 ret = ff_sws_op_list_append(ops1, &(SwsOp) {
1231 .op = SWS_OP_WRITE,
1232 .rw.type = type,
1233 .rw.elems = nb_planes,
1234 });
1235 if (ret < 0)
1236 goto fail;
1237
1238 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 if (ret < 0)
1244 goto fail;
1245
1246 if (swiz_rd.mask != SWS_SWIZZLE(0, 1, 2, 3).mask) {
1247 ret = ff_sws_op_list_insert_at(ops2, 1, &(SwsOp) {
1248 .op = SWS_OP_SWIZZLE,
1249 .swizzle = swiz_rd,
1250 });
1251 if (ret < 0)
1252 goto fail;
1253 }
1254
1256 if (ret < 0)
1257 goto fail;
1258
1260 if (ret < 0)
1261 goto fail;
1262
1263 *out_ops2 = ops2;
1264 return 0;
1265
1266fail:
1267 ff_sws_op_list_free(&ops2);
1268 return ret;
1269}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
static double val(void *priv, double ch)
Definition aeval.c:77
static const AVFilterPad inputs[]
Definition af_aap.c:299
static const AVFilterPad outputs[]
Definition af_aap.c:310
static FILE * out
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition avassert.h:109
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
static uint32_t BS_FUNC read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
byte swapping routines
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define av_popcount
Definition common.h:154
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static uint64_t shuffle(uint64_t in, const uint8_t *shuffle, int shuffle_len)
Definition des.c:179
static const uint8_t bits[8]
Definition fastaudio.c:100
int ret
Definition ffplay.c:391
static int dummy
Definition ffplay.c:4429
@ FIELD_TOP
Definition format.h:56
AVRational64 ff_mul_q64(AVRational64 b, AVRational64 c)
Multiply two 64-bit rationals.
Definition rational64.c:208
AVRational64 ff_add_q64(AVRational64 b, AVRational64 c)
Add two 64-bit rationals.
Definition rational64.c:222
int ff_cmp_q64(AVRational64 a, AVRational64 b)
Compare two 64-bit rationals.
Definition rational64.c:185
#define fail
Definition test.h:479
#define AVERROR(e)
Definition error.h:45
int index
Definition gxfenc.c:90
static av_always_inline void scale(vec_s32 res[4], vec_s16 res_packed[2], const int shift)
#define Q(q)
#define b
Definition input.c:43
#define av_log2
Definition intmath.h:84
enum MPFType type
Definition jpegmpfenc.c:55
unsigned offset
Definition libaomenc.c:763
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition anm.c:76
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
static const struct @257111027162314367033347246032313251342043035002 planes[]
static const uint16_t mask[17]
Definition lzw.c:38
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMAX(a, b)
Definition macros.h:47
const char data[16]
Definition mxf.c:155
uint32_t ff_sws_linear_mask(const SwsLinearOp *c)
Definition ops.c:861
int ff_sws_op_list_insert_at(SwsOpList *ops, int index, SwsOp *op)
Definition ops.c:811
const SwsOp * ff_sws_op_list_input(const SwsOpList *ops)
Returns the input operation for a given op list, or NULL if there is none (e.g.
Definition ops.c:782
void ff_sws_op_list_free(SwsOpList **p_ops)
Definition ops.c:743
void ff_sws_op_list_update(SwsOpList *ops)
Infer + propagate known information about components.
Definition ops.c:677
int ff_sws_op_list_append(SwsOpList *ops, SwsOp *op)
These will take over ownership of op and set it to {0}, even on failure.
Definition ops.c:826
void ff_sws_comp_mask_swizzle(SwsCompMask *mask, const SwsSwizzleOp *swiz)
Definition ops.c:110
SwsPixelType ff_sws_op_input_type(const SwsOp *op)
Return the input/output type of an operation, if all inputs/outputs are the same type,...
Definition ops.c:186
SwsCompMask ff_sws_comp_mask_needed(const SwsOp *op)
Definition ops.c:123
const SwsOp * ff_sws_op_list_output(const SwsOpList *ops)
Returns the output operation for a given op list, or NULL if there is none.
Definition ops.c:791
void ff_sws_apply_op_q(const SwsOp *op, AVRational64 x[4])
Apply an operation to an AVRational64.
Definition ops.c:202
void ff_sws_op_list_remove_at(SwsOpList *ops, int index, int count)
Definition ops.c:800
SwsOpList * ff_sws_op_list_duplicate(const SwsOpList *ops)
Returns a duplicate of ops, or NULL on OOM.
Definition ops.c:757
@ SWS_OP_TYPE_NB
Definition ops.h:69
@ SWS_OP_INVALID
Definition ops.h:37
@ SWS_OP_RSHIFT
Definition ops.h:49
@ SWS_OP_SWIZZLE
Definition ops.h:43
@ SWS_OP_LSHIFT
Definition ops.h:48
@ SWS_OP_FILTER_V
Definition ops.h:64
@ SWS_OP_SCALE
Definition ops.h:56
@ SWS_OP_FILTER_H
Definition ops.h:63
@ SWS_OP_WRITE
Definition ops.h:41
@ SWS_OP_READ
Definition ops.h:40
@ SWS_OP_CLEAR
Definition ops.h:52
@ SWS_OP_SWAP_BYTES
Definition ops.h:42
@ SWS_OP_MIN
Definition ops.h:54
@ SWS_OP_UNPACK
Definition ops.h:46
@ SWS_OP_LINEAR
Definition ops.h:57
@ SWS_OP_PACK
Definition ops.h:47
@ SWS_OP_DITHER
Definition ops.h:60
@ SWS_OP_MAX
Definition ops.h:55
@ SWS_OP_LUT_3D
Definition ops.h:67
@ SWS_OP_CONVERT
Definition ops.h:53
@ SWS_RW_PLANAR
Note: 1-component reads are either SWS_RW_PLANAR or SWS_RW_PACKED, depending on the underlying interp...
Definition ops.h:87
@ SWS_COMP_ZERO
Definition ops.h:231
@ SWS_COMP_EXACT
Definition ops.h:230
#define SWS_SWIZZLE(X, Y, Z, W)
Definition ops.h:141
#define SWS_OP_NEEDED(op, idx)
Definition ops.h:282
static bool pixel_is_repeating(SwsPixelType type, SwsPixel val)
int ff_sws_op_list_split_planes(SwsOpList *ops1, SwsOpList **out_ops2, SwsCompMask planes)
Reduce an op list into a reduced subset that operates only on a given subset of planes.
static int exact_log2(const int x)
static enum AVPixelFormat get_planar_fmt(SwsPixelType type, int nb_planes)
Determine a suitable intermediate buffer format for a given combination of pixel types and number of ...
static int solve_shuffle(const SwsUOpList *const uops, SwsUOp *out)
int ff_sws_op_list_optimize(SwsOpList *ops)
Fuse compatible and eliminate redundant operations, as well as replacing some operations with more ef...
#define RET(x)
Copyright (C) 2025 Niklas Haas.
static bool op_commute_filter(SwsOp *op, SwsOp *prev)
Try to commute a filter op with the previous operation.
static bool op_commute_clear(SwsOp *op, SwsOp *next)
Try to commute a clear op with the next operation.
static bool is_scale(const SwsUOp *op, int64_t val)
int ff_sws_uop_list_optimize(SwsContext *ctx, SwsUOpFlags flags, SwsUOpList *uops)
Called internally by ff_sws_ops_translate().
static void get_input_size(const SwsOpList *ops, SwsFormat *fmt)
static int exact_log2_q64(const AVRational64 x)
int ff_sws_op_list_split_at(SwsOpList *ops1, SwsOpList **out_ops2, int index)
Split an op list into two at the given index.
static int select_planes(SwsOpList *ops, SwsCompMask planes)
static bool extract_swizzle(SwsLinearOp *op, const SwsOpPad inputs[4], SwsSwizzleOp *out_swiz)
static bool extract_scalar(const SwsLinearOp *c, const SwsOpPad outputs[4], const SwsOpPad inputs[4], SwsScaleOp *out_scale)
If a linear operation can be reduced to a scalar multiplication, returns the corresponding scaling fa...
static bool extract_constant_rows(SwsLinearOp *c, const SwsOpPad inputs[4], SwsClearOp *out_clear)
static int op_result_is_exact(const SwsOp *op)
static bool op_commute_swizzle(SwsOp *op, SwsOp *next)
Try to commute a swizzle op with the next operation.
int ff_sws_shuffle_mask(const SwsUOp *uop, int8_t shuffle[], int size)
Compute a shuffle mask for pshufb-style ASM functions, by repeating the shuffle pattern for as many g...
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_GBRPF32
Definition pixfmt.h:587
#define AV_PIX_FMT_GRAYF32
Definition pixfmt.h:591
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:531
#define AV_PIX_FMT_YUVA444P16
Definition pixfmt.h:606
#define AV_PIX_FMT_GBRAPF32
Definition pixfmt.h:588
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:561
#define noop(a)
Utilities for rational number calculation.
void * av_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
Definition refstruct.c:141
#define av_bswap32
Definition bswap.h:47
#define av_bswap16
Definition bswap.h:28
#define FF_ARRAY_ELEMS(a)
64-bit Rational number (pair of numerator and denominator).
Definition rational64.h:52
int64_t num
Numerator.
Definition rational64.h:53
int64_t den
Denominator.
Definition rational64.h:54
AVRational64 limit[4]
Definition ops.h:159
SwsPixelType type[4]
Definition ops.h:149
SwsCompMask mask
Definition ops.h:150
AVRational64 value[4]
Definition ops.h:151
Main external API structure.
Definition swscale.h:227
SwsPixelType to
Definition ops.h:155
int8_t y_offset[4]
Definition ops.h:170
SwsPixelType type
Definition ops.h:200
enum AVPixelFormat format
Definition format.h:85
int interlaced
Definition format.h:83
int field
Definition format.h:84
const AVPixFmtDescriptor * desc
Definition format.h:90
int width
Definition format.h:82
int height
Definition format.h:82
SwsPixelType type[4]
Pixel type per output component.
Definition ops.h:178
AVRational64 m[4][5]
Generalized 5x5 affine transformation: [ Out.x ] = [ A B C D E ] [ Out.y ] = [ F G H I J ] * [ x y z ...
Definition ops.h:192
int8_t src[SWS_UOP_MOVE_MAX]
Definition uops.h:251
int num_moves
Definition uops.h:247
int8_t dst[SWS_UOP_MOVE_MAX]
Definition uops.h:250
Helper struct for representing a list of operations.
Definition ops.h:318
SwsFormat dst
Definition ops.h:323
uint8_t plane_src[4]
Definition ops.h:326
uint8_t plane_dst[4]
Definition ops.h:326
SwsOp * ops
Definition ops.h:319
SwsOpPad inputs[4]
Source component metadata associated with pixel values from each corresponding component (in plane/me...
Definition ops.h:337
int num_ops
Definition ops.h:320
SwsFormat src
Definition ops.h:323
SwsCompFlags flags
Definition ops.h:239
AVRational64 min
Definition ops.h:243
AVRational64 max
Definition ops.h:243
SwsPixelType type
Definition ops.h:238
Definition ops.h:252
SwsClampOp clamp
Definition ops.h:262
SwsScaleOp scale
Definition ops.h:263
SwsSwizzleOp swizzle
Definition ops.h:258
SwsOpType op
Definition ops.h:253
SwsConvertOp convert
Definition ops.h:261
SwsPackOp pack
Definition ops.h:257
SwsClearOp clear
Definition ops.h:260
SwsLinearOp lin
Definition ops.h:255
SwsReadWriteOp rw
Definition ops.h:256
SwsFilterOp filter
Definition ops.h:265
SwsOpPad outputs[4]
Metadata about the operation's input/output components.
Definition ops.h:276
SwsShiftOp shift
Definition ops.h:259
SwsDitherOp dither
Definition ops.h:264
uint8_t pattern[4]
Packed bits are assumed to be LSB-aligned within the underlying integer type; i.e.
Definition ops.h:126
SwsFilterWeights * kernel
Definition ops.h:116
SwsReadWriteMode mode
Definition ops.h:103
SwsPixelType type
Examples: rgba = 4x u8 packed yuv444p = 3x u8 rgb565 = 1x u16 <- use SWS_OP_UNPACK to unpack monow = ...
Definition ops.h:102
SwsOpType op
Definition ops.h:115
uint8_t frac
Definition ops.h:105
uint8_t elems
Definition ops.h:104
struct SwsReadWriteOp::@312356277033155231251141106170064356360153154105 filter
Filter kernel to apply to each plane while sampling.
AVRational64 factor
Definition ops.h:163
uint8_t amount
Definition ops.h:145
int8_t mask[16]
Definition uops.h:232
uint8_t write_size
Definition uops.h:228
uint8_t read_size
Definition uops.h:227
uint8_t in[4]
Definition ops.h:136
uint32_t mask
Definition ops.h:135
SwsUOp * ops
Definition uops.h:345
int num_ops
Definition uops.h:346
Definition uops.h:308
SwsCompMask mask
Definition uops.h:312
SwsUOpType uop
Definition uops.h:311
SwsUOpParams par
Definition uops.h:313
union SwsUOp::@242237116251216327057105100216205033300341206345 data
SwsPixelType type
Definition uops.h:310
SwsShuffleMask shuffle
Definition uops.h:322
SwsPixel vec4[4]
Definition uops.h:320
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
int size
SwsMoveUOp move
Definition uops.h:300
SwsShuffleUOp shuffle
Definition uops.h:297
int ff_sws_uop_list_append(SwsUOpList *uops, SwsUOp *uop)
Definition uops.c:181
void ff_sws_uop_list_remove_at(SwsUOpList *uops, int index, int count)
Definition uops.c:194
uint32_t SwsUOpFlags
Definition uops.h:158
SwsPixelType
Definition uops.h:40
@ SWS_PIXEL_U32
Definition uops.h:44
@ SWS_PIXEL_U16
Definition uops.h:43
@ SWS_PIXEL_U8
Definition uops.h:42
#define SWS_COMP(X)
Definition uops.h:133
#define SWS_MASK_ROW(I)
Definition uops.h:277
#define SWS_MASK_DIAG4
Definition uops.h:279
#define SWS_COMP_TEST(mask, X)
Definition uops.h:134
#define SWS_COMP_ELEMS(N)
Definition uops.h:136
#define SWS_MASK(I, J)
Definition uops.h:275
#define SWS_MASK_COL(J)
Definition uops.h:278
@ SWS_UOP_PERMUTE
Definition uops.h:191
@ SWS_UOP_EXPAND_QUAD
Definition uops.h:198
@ SWS_UOP_READ_PLANAR
Definition uops.h:173
@ SWS_UOP_WRITE_PLANAR
Definition uops.h:182
@ SWS_UOP_SWAP_BYTES
Definition uops.h:195
@ SWS_UOP_COPY
Definition uops.h:192
@ SWS_UOP_SCALE
Definition uops.h:205
@ SWS_UOP_WRITE_PACKED
Definition uops.h:183
@ SWS_UOP_READ_PACKED
Definition uops.h:177
@ SWS_UOP_EXPAND_PAIR
Definition uops.h:197
@ SWS_UOP_TO_U16
Definition uops.h:200
@ SWS_UOP_CLEAR
Definition uops.h:215
@ SWS_UOP_EXPAND_BIT
Definition uops.h:196
@ SWS_UOP_TO_U32
Definition uops.h:201
@ SWS_UOP_RW_SHUFFLE
Definition uops.h:188
@ SWS_UOP_FLAG_PSHUFB
Definition uops.h:162
@ SWS_UOP_FLAG_EXPAND_BIT
Definition uops.h:163
static av_const bool ff_sws_pixel_type_is_int(SwsPixelType type)
Definition uops.h:64
uint8_t SwsCompMask
Bit-mask of components.
Definition uops.h:129
static av_const int ff_sws_pixel_type_size(SwsPixelType type)
Definition uops.h:51
char full[32]
static double c[64]