/src/ffmpeg/libswscale/ops_optimizer.c
Line | Count | 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 | 0 | do { \ |
31 | 0 | if ((ret = (x)) < 0) \ |
32 | 0 | return ret; \ |
33 | 0 | } 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 | | static bool op_commute_clear(SwsOp *op, SwsOp *next) |
42 | 0 | { |
43 | 0 | av_assert1(op->op == SWS_OP_CLEAR); |
44 | 0 | switch (next->op) { |
45 | 0 | case SWS_OP_CONVERT: |
46 | 0 | op->type = next->convert.to; |
47 | 0 | av_fallthrough; |
48 | 0 | case SWS_OP_LSHIFT: |
49 | 0 | case SWS_OP_RSHIFT: |
50 | 0 | case SWS_OP_DITHER: |
51 | 0 | case SWS_OP_MIN: |
52 | 0 | case SWS_OP_MAX: |
53 | 0 | case SWS_OP_SCALE: |
54 | 0 | case SWS_OP_READ: |
55 | 0 | ff_sws_apply_op_q(next, op->clear.value); |
56 | 0 | return true; |
57 | 0 | case SWS_OP_FILTER_H: |
58 | 0 | case SWS_OP_FILTER_V: |
59 | 0 | op->type = next->filter.type; |
60 | 0 | return true; |
61 | 0 | case SWS_OP_SWIZZLE: |
62 | 0 | ff_sws_comp_mask_swizzle(&op->clear.mask, &next->swizzle); |
63 | 0 | ff_sws_apply_op_q(next, op->clear.value); |
64 | 0 | return true; |
65 | 0 | case SWS_OP_SWAP_BYTES: |
66 | 0 | switch (next->type) { |
67 | 0 | case SWS_PIXEL_U16: |
68 | 0 | case SWS_PIXEL_U32: |
69 | 0 | ff_sws_apply_op_q(next, op->clear.value); /* always representable */ |
70 | 0 | return true; |
71 | 0 | default: |
72 | 0 | return false; |
73 | 0 | } |
74 | 0 | case SWS_OP_INVALID: |
75 | 0 | case SWS_OP_WRITE: |
76 | 0 | case SWS_OP_LINEAR: |
77 | 0 | case SWS_OP_PACK: |
78 | 0 | case SWS_OP_UNPACK: |
79 | 0 | case SWS_OP_CLEAR: |
80 | 0 | case SWS_OP_LUT_3D: |
81 | 0 | return false; |
82 | 0 | case SWS_OP_TYPE_NB: |
83 | 0 | break; |
84 | 0 | } |
85 | | |
86 | 0 | av_unreachable("Invalid operation type!"); |
87 | 0 | return false; |
88 | 0 | } |
89 | | |
90 | | /** |
91 | | * Try to commute a swizzle op with the next operation. Makes any adjustments |
92 | | * to the operations as needed, but does not perform the actual commutation. |
93 | | * |
94 | | * Returns whether successful. |
95 | | */ |
96 | | static bool op_commute_swizzle(SwsOp *op, SwsOp *next) |
97 | 0 | { |
98 | 0 | bool seen[4] = {0}; |
99 | |
|
100 | 0 | av_assert1(op->op == SWS_OP_SWIZZLE); |
101 | 0 | switch (next->op) { |
102 | 0 | case SWS_OP_CONVERT: |
103 | 0 | op->type = next->convert.to; |
104 | 0 | av_fallthrough; |
105 | 0 | case SWS_OP_SWAP_BYTES: |
106 | 0 | case SWS_OP_LSHIFT: |
107 | 0 | case SWS_OP_RSHIFT: |
108 | 0 | case SWS_OP_SCALE: |
109 | 0 | return true; |
110 | 0 | case SWS_OP_FILTER_H: |
111 | 0 | case SWS_OP_FILTER_V: |
112 | 0 | op->type = next->filter.type; |
113 | 0 | return true; |
114 | | |
115 | | /** |
116 | | * We can commute per-channel ops only if the per-channel constants are the |
117 | | * same for all duplicated channels; e.g.: |
118 | | * SWIZZLE {0, 0, 0, 3} |
119 | | * NEXT {x, x, x, w} |
120 | | * -> |
121 | | * NEXT {x, _, _, w} |
122 | | * SWIZZLE {0, 0, 0, 3} |
123 | | */ |
124 | 0 | case SWS_OP_MIN: |
125 | 0 | case SWS_OP_MAX: { |
126 | 0 | const SwsClampOp c = next->clamp; |
127 | 0 | for (int i = 0; i < 4; i++) { |
128 | 0 | if (!SWS_OP_NEEDED(op, i)) |
129 | 0 | continue; |
130 | 0 | const int j = op->swizzle.in[i]; |
131 | 0 | if (seen[j] && av_cmp_q64(next->clamp.limit[j], c.limit[i])) |
132 | 0 | return false; |
133 | 0 | next->clamp.limit[j] = c.limit[i]; |
134 | 0 | seen[j] = true; |
135 | 0 | } |
136 | 0 | return true; |
137 | 0 | } |
138 | | |
139 | 0 | case SWS_OP_DITHER: { |
140 | 0 | const SwsDitherOp d = next->dither; |
141 | 0 | for (int i = 0; i < 4; i++) { |
142 | 0 | if (!SWS_OP_NEEDED(op, i)) |
143 | 0 | continue; |
144 | 0 | const int j = op->swizzle.in[i]; |
145 | 0 | if (seen[j] && next->dither.y_offset[j] != d.y_offset[i]) |
146 | 0 | return false; |
147 | 0 | next->dither.y_offset[j] = d.y_offset[i]; |
148 | 0 | seen[j] = true; |
149 | 0 | } |
150 | 0 | return true; |
151 | 0 | } |
152 | | |
153 | 0 | case SWS_OP_INVALID: |
154 | 0 | case SWS_OP_READ: |
155 | 0 | case SWS_OP_WRITE: |
156 | 0 | case SWS_OP_SWIZZLE: |
157 | 0 | case SWS_OP_CLEAR: |
158 | 0 | case SWS_OP_LINEAR: |
159 | 0 | case SWS_OP_PACK: |
160 | 0 | case SWS_OP_UNPACK: |
161 | 0 | case SWS_OP_LUT_3D: |
162 | 0 | return false; |
163 | 0 | case SWS_OP_TYPE_NB: |
164 | 0 | break; |
165 | 0 | } |
166 | | |
167 | 0 | av_unreachable("Invalid operation type!"); |
168 | 0 | return false; |
169 | 0 | } |
170 | | |
171 | | /** |
172 | | * Try to commute a filter op with the previous operation. Makes any |
173 | | * adjustments to the operations as needed, but does not perform the actual |
174 | | * commutation. |
175 | | * |
176 | | * Returns whether successful. |
177 | | */ |
178 | | static bool op_commute_filter(SwsOp *op, SwsOp *prev) |
179 | 0 | { |
180 | 0 | av_assert0(!ff_sws_pixel_type_is_int(op->filter.type)); |
181 | | |
182 | 0 | switch (prev->op) { |
183 | 0 | case SWS_OP_SWIZZLE: |
184 | 0 | case SWS_OP_SCALE: |
185 | 0 | case SWS_OP_LINEAR: |
186 | 0 | case SWS_OP_DITHER: |
187 | 0 | prev->type = op->filter.type; |
188 | 0 | return true; |
189 | 0 | case SWS_OP_CONVERT: |
190 | 0 | case SWS_OP_INVALID: |
191 | 0 | case SWS_OP_READ: |
192 | 0 | case SWS_OP_WRITE: |
193 | 0 | case SWS_OP_SWAP_BYTES: |
194 | 0 | case SWS_OP_UNPACK: |
195 | 0 | case SWS_OP_PACK: |
196 | 0 | case SWS_OP_LSHIFT: |
197 | 0 | case SWS_OP_RSHIFT: |
198 | 0 | case SWS_OP_CLEAR: |
199 | 0 | case SWS_OP_MIN: |
200 | 0 | case SWS_OP_MAX: |
201 | 0 | case SWS_OP_FILTER_H: |
202 | 0 | case SWS_OP_FILTER_V: |
203 | 0 | case SWS_OP_LUT_3D: |
204 | 0 | return false; |
205 | 0 | case SWS_OP_TYPE_NB: |
206 | 0 | break; |
207 | 0 | } |
208 | | |
209 | 0 | av_unreachable("Invalid operation type!"); |
210 | 0 | return false; |
211 | 0 | } |
212 | | |
213 | | /* returns log2(x) only if x is a power of two, or 0 otherwise */ |
214 | | static int exact_log2(const int x) |
215 | 0 | { |
216 | 0 | int p; |
217 | 0 | if (x <= 0) |
218 | 0 | return 0; |
219 | 0 | p = av_log2(x); |
220 | 0 | return (1 << p) == x ? p : 0; |
221 | 0 | } |
222 | | |
223 | | static int exact_log2_q64(const AVRational64 x) |
224 | 0 | { |
225 | 0 | if (x.den == 1) |
226 | 0 | return exact_log2(x.num); |
227 | 0 | else if (x.num == 1) |
228 | 0 | return -exact_log2(x.den); |
229 | 0 | else |
230 | 0 | return 0; |
231 | 0 | } |
232 | | |
233 | | /** |
234 | | * If a linear operation can be reduced to a scalar multiplication, returns |
235 | | * the corresponding scaling factor, or 0 otherwise. |
236 | | */ |
237 | | static bool extract_scalar(const SwsLinearOp *c, |
238 | | const SwsComps *comps, const SwsComps *prev, |
239 | | SwsScaleOp *out_scale) |
240 | 0 | { |
241 | 0 | SwsScaleOp scale = {0}; |
242 | | |
243 | | /* There are components not on the main diagonal */ |
244 | 0 | if (ff_sws_linear_mask(c) & ~SWS_MASK_DIAG4) |
245 | 0 | return false; |
246 | | |
247 | 0 | for (int i = 0; i < 4; i++) { |
248 | 0 | const AVRational64 s = c->m[i][i]; |
249 | 0 | if ((prev->flags[i] & SWS_COMP_ZERO) || |
250 | 0 | (comps->flags[i] & SWS_COMP_GARBAGE)) |
251 | 0 | continue; |
252 | 0 | if (scale.factor.den && av_cmp_q64(s, scale.factor)) |
253 | 0 | return false; |
254 | 0 | scale.factor = s; |
255 | 0 | } |
256 | | |
257 | 0 | if (scale.factor.den) |
258 | 0 | *out_scale = scale; |
259 | 0 | return scale.factor.den; |
260 | 0 | } |
261 | | |
262 | | /* Extracts an integer clear operation (subset) from the given linear op. */ |
263 | | static bool extract_constant_rows(SwsLinearOp *c, const SwsComps *prev, |
264 | | SwsClearOp *out_clear) |
265 | 0 | { |
266 | 0 | const uint32_t mask = ff_sws_linear_mask(c); |
267 | 0 | SwsClearOp clear = {0}; |
268 | 0 | bool ret = false; |
269 | |
|
270 | 0 | for (int i = 0; i < 4; i++) { |
271 | 0 | bool const_row = c->m[i][4].den == 1; /* offset is integer */ |
272 | 0 | for (int j = 0; j < 4; j++) { |
273 | 0 | const_row &= c->m[i][j].num == 0 || /* scalar is zero */ |
274 | 0 | (prev->flags[j] & SWS_COMP_ZERO); /* input is zero */ |
275 | 0 | } |
276 | 0 | if (const_row && (mask & SWS_MASK_ROW(i))) { |
277 | 0 | clear.mask |= SWS_COMP(i); |
278 | 0 | clear.value[i] = c->m[i][4]; |
279 | 0 | for (int j = 0; j < 5; j++) |
280 | 0 | c->m[i][j] = Q(i == j); |
281 | 0 | ret = true; |
282 | 0 | } |
283 | 0 | } |
284 | |
|
285 | 0 | if (ret) |
286 | 0 | *out_clear = clear; |
287 | 0 | return ret; |
288 | 0 | } |
289 | | |
290 | | /* Unswizzle a linear operation by aligning single-input rows with |
291 | | * their corresponding diagonal */ |
292 | | static bool extract_swizzle(SwsLinearOp *op, const SwsComps *prev, |
293 | | SwsSwizzleOp *out_swiz) |
294 | 0 | { |
295 | 0 | SwsSwizzleOp swiz = SWS_SWIZZLE(0, 1, 2, 3); |
296 | 0 | SwsLinearOp c = *op; |
297 | | |
298 | | /* Find non-zero coefficients in the main 4x4 matrix */ |
299 | 0 | uint32_t nonzero = 0; |
300 | 0 | for (int i = 0; i < 4; i++) { |
301 | 0 | for (int j = 0; j < 4; j++) { |
302 | 0 | if (!c.m[i][j].num || (prev->flags[j] & SWS_COMP_ZERO)) |
303 | 0 | continue; |
304 | 0 | nonzero |= SWS_MASK(i, j); |
305 | 0 | } |
306 | 0 | } |
307 | | |
308 | | /* If a value is unique in its row and the target column is |
309 | | * empty, move it there and update the input swizzle */ |
310 | 0 | for (int i = 0; i < 4; i++) { |
311 | 0 | if (nonzero & SWS_MASK_COL(i)) |
312 | 0 | continue; /* target column is not empty */ |
313 | 0 | for (int j = 0; j < 4; j++) { |
314 | 0 | if ((nonzero & SWS_MASK_ROW(i)) == SWS_MASK(i, j)) { |
315 | | /* Move coefficient to the diagonal */ |
316 | 0 | c.m[i][i] = c.m[i][j]; |
317 | 0 | c.m[i][j] = Q(0); |
318 | 0 | swiz.in[i] = j; |
319 | 0 | break; |
320 | 0 | } |
321 | 0 | } |
322 | 0 | } |
323 | |
|
324 | 0 | if (swiz.mask == SWS_SWIZZLE(0, 1, 2, 3).mask) |
325 | 0 | return false; /* no swizzle was identified */ |
326 | | |
327 | 0 | *out_swiz = swiz; |
328 | 0 | *op = c; |
329 | 0 | return true; |
330 | 0 | } |
331 | | |
332 | | static int op_result_is_exact(const SwsOp *op) |
333 | 0 | { |
334 | 0 | for (int i = 0; i < 4; i++) { |
335 | 0 | if (SWS_OP_NEEDED(op, i) && !(op->comps.flags[i] & SWS_COMP_EXACT)) |
336 | 0 | return false; |
337 | 0 | } |
338 | | |
339 | 0 | return true; |
340 | 0 | } |
341 | | |
342 | | int ff_sws_op_list_optimize(SwsOpList *ops) |
343 | 0 | { |
344 | 0 | int ret; |
345 | |
|
346 | 0 | retry: |
347 | 0 | ff_sws_op_list_update_comps(ops); |
348 | | |
349 | | /* Try to push filters towards the input; do this first to unblock |
350 | | * in-place optimizations like linear op fusion */ |
351 | 0 | for (int n = 1; n < ops->num_ops; n++) { |
352 | 0 | SwsOp *op = &ops->ops[n]; |
353 | 0 | SwsOp *prev = &ops->ops[n - 1]; |
354 | |
|
355 | 0 | switch (op->op) { |
356 | 0 | case SWS_OP_FILTER_H: |
357 | 0 | case SWS_OP_FILTER_V: |
358 | 0 | if (op_commute_filter(op, prev)) { |
359 | 0 | FFSWAP(SwsOp, *op, *prev); |
360 | 0 | goto retry; |
361 | 0 | } |
362 | | |
363 | | /* Merge filter with prior conversion */ |
364 | 0 | if (prev->op == SWS_OP_CONVERT && !prev->convert.expand) { |
365 | 0 | int size_from = ff_sws_pixel_type_size(prev->type); |
366 | 0 | int size_to = ff_sws_pixel_type_size(op->type); |
367 | 0 | av_assert1(prev->convert.to == op->type); |
368 | 0 | if (size_from < size_to) { |
369 | 0 | op->type = prev->type; |
370 | 0 | ff_sws_op_list_remove_at(ops, n - 1, 1); |
371 | 0 | goto retry; |
372 | 0 | } |
373 | 0 | } |
374 | 0 | break; |
375 | 0 | } |
376 | 0 | } |
377 | | |
378 | | /* Apply all in-place optimizations (that do not re-order the list) */ |
379 | 0 | for (int n = 0; n < ops->num_ops; n++) { |
380 | 0 | SwsOp dummy = {0}; |
381 | 0 | SwsOp *op = &ops->ops[n]; |
382 | 0 | SwsOp *prev = n ? &ops->ops[n - 1] : &dummy; |
383 | 0 | SwsOp *next = n + 1 < ops->num_ops ? &ops->ops[n + 1] : &dummy; |
384 | | |
385 | | /* common helper variable */ |
386 | 0 | const SwsCompMask needed = ff_sws_comp_mask_needed(op); |
387 | 0 | bool noop = true; |
388 | |
|
389 | 0 | if (!needed && op->op != SWS_OP_WRITE) { |
390 | | /* Remove any operation whose output is not needed */ |
391 | 0 | ff_sws_op_list_remove_at(ops, n, 1); |
392 | 0 | goto retry; |
393 | 0 | } |
394 | | |
395 | 0 | switch (op->op) { |
396 | 0 | case SWS_OP_READ: |
397 | | /* "Compress" planar reads where not all components are needed */ |
398 | 0 | if (op->rw.mode == SWS_RW_PLANAR) { |
399 | 0 | SwsSwizzleOp swiz = SWS_SWIZZLE(0, 1, 2, 3); |
400 | 0 | int nb_planes = 0; |
401 | 0 | for (int i = 0; i < op->rw.elems; i++) { |
402 | 0 | if (!SWS_OP_NEEDED(op, i)) { |
403 | 0 | swiz.in[i] = 3 - (i - nb_planes); /* map to unused plane */ |
404 | 0 | continue; |
405 | 0 | } |
406 | | |
407 | 0 | const int idx = nb_planes++; |
408 | 0 | av_assert1(idx <= i); |
409 | 0 | ops->plane_src[idx] = ops->plane_src[i]; |
410 | 0 | swiz.in[i] = idx; |
411 | 0 | } |
412 | |
|
413 | 0 | if (nb_planes < op->rw.elems) { |
414 | 0 | op->rw.elems = nb_planes; |
415 | 0 | RET(ff_sws_op_list_insert_at(ops, n + 1, &(SwsOp) { |
416 | 0 | .op = SWS_OP_SWIZZLE, |
417 | 0 | .type = op->rw.filter.op ? op->rw.filter.type : op->type, |
418 | 0 | .swizzle = swiz, |
419 | 0 | })); |
420 | 0 | goto retry; |
421 | 0 | } |
422 | 0 | } |
423 | 0 | break; |
424 | | |
425 | 0 | case SWS_OP_SWAP_BYTES: |
426 | | /* Redundant (double) swap */ |
427 | 0 | if (next->op == SWS_OP_SWAP_BYTES) { |
428 | 0 | ff_sws_op_list_remove_at(ops, n, 2); |
429 | 0 | goto retry; |
430 | 0 | } |
431 | 0 | break; |
432 | | |
433 | 0 | case SWS_OP_UNPACK: |
434 | | /* Redundant unpack+pack */ |
435 | 0 | if (next->op == SWS_OP_PACK && next->type == op->type && |
436 | 0 | next->pack.pattern[0] == op->pack.pattern[0] && |
437 | 0 | next->pack.pattern[1] == op->pack.pattern[1] && |
438 | 0 | next->pack.pattern[2] == op->pack.pattern[2] && |
439 | 0 | next->pack.pattern[3] == op->pack.pattern[3]) |
440 | 0 | { |
441 | 0 | ff_sws_op_list_remove_at(ops, n, 2); |
442 | 0 | goto retry; |
443 | 0 | } |
444 | 0 | break; |
445 | | |
446 | 0 | case SWS_OP_LSHIFT: |
447 | 0 | case SWS_OP_RSHIFT: |
448 | | /* Two shifts in the same direction */ |
449 | 0 | if (next->op == op->op) { |
450 | 0 | op->shift.amount += next->shift.amount; |
451 | 0 | ff_sws_op_list_remove_at(ops, n + 1, 1); |
452 | 0 | goto retry; |
453 | 0 | } |
454 | | |
455 | | /* No-op shift */ |
456 | 0 | if (!op->shift.amount) { |
457 | 0 | ff_sws_op_list_remove_at(ops, n, 1); |
458 | 0 | goto retry; |
459 | 0 | } |
460 | 0 | break; |
461 | | |
462 | 0 | case SWS_OP_CLEAR: |
463 | 0 | for (int i = 0; i < 4; i++) { |
464 | 0 | if (!SWS_COMP_TEST(op->clear.mask, i)) |
465 | 0 | continue; |
466 | | |
467 | 0 | if ((prev->comps.flags[i] & SWS_COMP_ZERO) && |
468 | 0 | !(prev->comps.flags[i] & SWS_COMP_GARBAGE) && |
469 | 0 | op->clear.value[i].num == 0) |
470 | 0 | { |
471 | | /* Redundant clear-to-zero of zero component */ |
472 | 0 | op->clear.mask ^= SWS_COMP(i); |
473 | 0 | } else if (!SWS_OP_NEEDED(op, i)) { |
474 | | /* Unnecessary clear of unused component */ |
475 | 0 | op->clear.mask ^= SWS_COMP(i); |
476 | 0 | } else { |
477 | 0 | noop = false; |
478 | 0 | } |
479 | 0 | } |
480 | |
|
481 | 0 | if (noop) { |
482 | 0 | ff_sws_op_list_remove_at(ops, n, 1); |
483 | 0 | goto retry; |
484 | 0 | } |
485 | | |
486 | | /* Transitive clear */ |
487 | 0 | if (next->op == SWS_OP_CLEAR) { |
488 | 0 | for (int i = 0; i < 4; i++) { |
489 | 0 | if (SWS_COMP_TEST(next->clear.mask, i)) |
490 | 0 | op->clear.value[i] = next->clear.value[i]; |
491 | 0 | } |
492 | 0 | op->clear.mask |= next->clear.mask; |
493 | 0 | ff_sws_op_list_remove_at(ops, n + 1, 1); |
494 | 0 | goto retry; |
495 | 0 | } |
496 | 0 | break; |
497 | | |
498 | 0 | case SWS_OP_SWIZZLE: |
499 | 0 | for (int i = 0; i < 4; i++) { |
500 | 0 | if (!SWS_OP_NEEDED(op, i)) |
501 | 0 | continue; |
502 | 0 | if (op->swizzle.in[i] != i) |
503 | 0 | noop = false; |
504 | 0 | } |
505 | | |
506 | | /* Identity swizzle */ |
507 | 0 | if (noop) { |
508 | 0 | ff_sws_op_list_remove_at(ops, n, 1); |
509 | 0 | goto retry; |
510 | 0 | } |
511 | | |
512 | | /* Transitive swizzle */ |
513 | 0 | if (next->op == SWS_OP_SWIZZLE) { |
514 | 0 | const SwsSwizzleOp orig = op->swizzle; |
515 | 0 | for (int i = 0; i < 4; i++) |
516 | 0 | op->swizzle.in[i] = orig.in[next->swizzle.in[i]]; |
517 | 0 | ff_sws_op_list_remove_at(ops, n + 1, 1); |
518 | 0 | goto retry; |
519 | 0 | } |
520 | | |
521 | | /* Swizzle planes instead of components, if possible */ |
522 | 0 | if (prev->op == SWS_OP_READ && prev->rw.mode == SWS_RW_PLANAR) { |
523 | 0 | for (int dst = 0; dst < prev->rw.elems; dst++) { |
524 | 0 | const int src = op->swizzle.in[dst]; |
525 | 0 | if (src > dst && src < prev->rw.elems) { |
526 | 0 | FFSWAP(int, ops->plane_src[dst], ops->plane_src[src]); |
527 | 0 | for (int i = dst; i < 4; i++) { |
528 | 0 | if (op->swizzle.in[i] == dst) |
529 | 0 | op->swizzle.in[i] = src; |
530 | 0 | else if (op->swizzle.in[i] == src) |
531 | 0 | op->swizzle.in[i] = dst; |
532 | 0 | } |
533 | 0 | goto retry; |
534 | 0 | } |
535 | 0 | } |
536 | 0 | } |
537 | | |
538 | 0 | if (next->op == SWS_OP_WRITE && next->rw.mode == SWS_RW_PLANAR) { |
539 | 0 | for (int dst = 0; dst < next->rw.elems; dst++) { |
540 | 0 | const int src = op->swizzle.in[dst]; |
541 | 0 | if (src > dst && src < next->rw.elems) { |
542 | 0 | FFSWAP(int, ops->plane_dst[dst], ops->plane_dst[src]); |
543 | 0 | FFSWAP(int, op->swizzle.in[dst], op->swizzle.in[src]); |
544 | 0 | goto retry; |
545 | 0 | } |
546 | 0 | } |
547 | 0 | } |
548 | 0 | break; |
549 | | |
550 | 0 | case SWS_OP_CONVERT: |
551 | | /* No-op conversion */ |
552 | 0 | if (op->type == op->convert.to) { |
553 | 0 | ff_sws_op_list_remove_at(ops, n, 1); |
554 | 0 | goto retry; |
555 | 0 | } |
556 | | |
557 | | /* Transitive conversion */ |
558 | 0 | if (next->op == SWS_OP_CONVERT && |
559 | 0 | op->convert.expand == next->convert.expand) |
560 | 0 | { |
561 | 0 | av_assert1(op->convert.to == next->type); |
562 | 0 | op->convert.to = next->convert.to; |
563 | 0 | ff_sws_op_list_remove_at(ops, n + 1, 1); |
564 | 0 | goto retry; |
565 | 0 | } |
566 | | |
567 | | /* Conversion followed by integer expansion */ |
568 | 0 | if (next->op == SWS_OP_SCALE && !op->convert.expand && |
569 | 0 | ff_sws_pixel_type_is_int(op->type) && |
570 | 0 | ff_sws_pixel_type_is_int(op->convert.to) && |
571 | 0 | !av_cmp_q64(next->scale.factor, |
572 | 0 | ff_sws_pixel_expand(op->type, op->convert.to))) |
573 | 0 | { |
574 | 0 | op->convert.expand = true; |
575 | 0 | ff_sws_op_list_remove_at(ops, n + 1, 1); |
576 | 0 | goto retry; |
577 | 0 | } |
578 | 0 | break; |
579 | | |
580 | 0 | case SWS_OP_MIN: |
581 | 0 | for (int i = 0; i < 4; i++) { |
582 | 0 | if (!SWS_OP_NEEDED(op, i) || !op->clamp.limit[i].den) |
583 | 0 | continue; |
584 | 0 | if (av_cmp_q64(op->clamp.limit[i], prev->comps.max[i]) >= 0) |
585 | 0 | op->clamp.limit[i] = (AVRational64) {0}; /* no-op */ |
586 | 0 | else |
587 | 0 | noop = false; |
588 | 0 | } |
589 | |
|
590 | 0 | if (noop) { |
591 | 0 | ff_sws_op_list_remove_at(ops, n, 1); |
592 | 0 | goto retry; |
593 | 0 | } |
594 | 0 | break; |
595 | | |
596 | 0 | case SWS_OP_MAX: |
597 | 0 | for (int i = 0; i < 4; i++) { |
598 | 0 | if (!SWS_OP_NEEDED(op, i) || !op->clamp.limit[i].den) |
599 | 0 | continue; |
600 | 0 | if (av_cmp_q64(prev->comps.min[i], op->clamp.limit[i]) >= 0) |
601 | 0 | op->clamp.limit[i] = (AVRational64) {0}; |
602 | 0 | else |
603 | 0 | noop = false; |
604 | 0 | } |
605 | |
|
606 | 0 | if (noop) { |
607 | 0 | ff_sws_op_list_remove_at(ops, n, 1); |
608 | 0 | goto retry; |
609 | 0 | } |
610 | 0 | break; |
611 | | |
612 | 0 | case SWS_OP_DITHER: |
613 | 0 | for (int i = 0; i < 4; i++) { |
614 | 0 | if (op->dither.y_offset[i] < 0) |
615 | 0 | continue; |
616 | 0 | if (!SWS_OP_NEEDED(op, i) || (prev->comps.flags[i] & SWS_COMP_EXACT)) { |
617 | 0 | op->dither.y_offset[i] = -1; /* unnecessary dither */ |
618 | 0 | goto retry; |
619 | 0 | } else { |
620 | 0 | noop = false; |
621 | 0 | } |
622 | 0 | } |
623 | | |
624 | 0 | if (noop) { |
625 | 0 | ff_sws_op_list_remove_at(ops, n, 1); |
626 | 0 | goto retry; |
627 | 0 | } |
628 | 0 | break; |
629 | | |
630 | 0 | case SWS_OP_LINEAR: { |
631 | 0 | const uint32_t mask = ff_sws_linear_mask(&op->lin); |
632 | 0 | SwsSwizzleOp swizzle; |
633 | 0 | SwsClearOp clear; |
634 | 0 | SwsScaleOp scale; |
635 | | |
636 | | /* No-op (identity) linear operation */ |
637 | 0 | if (!mask) { |
638 | 0 | ff_sws_op_list_remove_at(ops, n, 1); |
639 | 0 | goto retry; |
640 | 0 | } |
641 | | |
642 | 0 | if (next->op == SWS_OP_LINEAR) { |
643 | | /* 5x5 matrix multiplication after appending [ 0 0 0 0 1 ] */ |
644 | 0 | const SwsLinearOp m1 = op->lin; |
645 | 0 | const SwsLinearOp m2 = next->lin; |
646 | 0 | for (int i = 0; i < 4; i++) { |
647 | 0 | for (int j = 0; j < 5; j++) { |
648 | 0 | AVRational64 sum = Q(0); |
649 | 0 | for (int k = 0; k < 4; k++) |
650 | 0 | sum = av_add_q64(sum, av_mul_q64(m2.m[i][k], m1.m[k][j])); |
651 | 0 | if (j == 4) /* m1.m[4][j] == 1 */ |
652 | 0 | sum = av_add_q64(sum, m2.m[i][4]); |
653 | 0 | op->lin.m[i][j] = sum; |
654 | 0 | } |
655 | 0 | } |
656 | 0 | ff_sws_op_list_remove_at(ops, n + 1, 1); |
657 | 0 | goto retry; |
658 | 0 | } |
659 | | |
660 | | /* Optimize away zero columns */ |
661 | 0 | for (int j = 0; j < 4; j++) { |
662 | 0 | const uint32_t col = SWS_MASK_COL(j); |
663 | 0 | if (!(prev->comps.flags[j] & SWS_COMP_ZERO) || !(mask & col)) |
664 | 0 | continue; |
665 | 0 | for (int i = 0; i < 4; i++) |
666 | 0 | op->lin.m[i][j] = Q(i == j); |
667 | 0 | goto retry; |
668 | 0 | } |
669 | | |
670 | | /* Optimize away unused rows */ |
671 | 0 | for (int i = 0; i < 4; i++) { |
672 | 0 | const uint32_t row = SWS_MASK_ROW(i); |
673 | 0 | if (SWS_OP_NEEDED(op, i) || !(mask & row)) |
674 | 0 | continue; |
675 | 0 | for (int j = 0; j < 5; j++) |
676 | 0 | op->lin.m[i][j] = Q(i == j); |
677 | 0 | goto retry; |
678 | 0 | } |
679 | | |
680 | | /* Convert constant rows to explicit clear instruction */ |
681 | 0 | if (extract_constant_rows(&op->lin, &prev->comps, &clear)) { |
682 | 0 | RET(ff_sws_op_list_insert_at(ops, n + 1, &(SwsOp) { |
683 | 0 | .op = SWS_OP_CLEAR, |
684 | 0 | .type = op->type, |
685 | 0 | .comps = op->comps, |
686 | 0 | .clear = clear, |
687 | 0 | })); |
688 | 0 | goto retry; |
689 | 0 | } |
690 | | |
691 | | /* Multiplication by scalar constant */ |
692 | 0 | if (extract_scalar(&op->lin, &op->comps, &prev->comps, &scale)) { |
693 | 0 | op->op = SWS_OP_SCALE; |
694 | 0 | op->scale = scale; |
695 | 0 | goto retry; |
696 | 0 | } |
697 | | |
698 | | /* Swizzle by fixed pattern */ |
699 | 0 | if (extract_swizzle(&op->lin, &prev->comps, &swizzle)) { |
700 | 0 | RET(ff_sws_op_list_insert_at(ops, n, &(SwsOp) { |
701 | 0 | .op = SWS_OP_SWIZZLE, |
702 | 0 | .type = op->type, |
703 | 0 | .swizzle = swizzle, |
704 | 0 | })); |
705 | 0 | goto retry; |
706 | 0 | } |
707 | 0 | break; |
708 | 0 | } |
709 | | |
710 | 0 | case SWS_OP_SCALE: { |
711 | 0 | const int factor2 = exact_log2_q64(op->scale.factor); |
712 | | |
713 | | /* No-op scaling */ |
714 | 0 | if (op->scale.factor.num == 1 && op->scale.factor.den == 1) { |
715 | 0 | ff_sws_op_list_remove_at(ops, n, 1); |
716 | 0 | goto retry; |
717 | 0 | } |
718 | | |
719 | | /* Merge consecutive scaling operations */ |
720 | 0 | if (next->op == SWS_OP_SCALE) { |
721 | 0 | op->scale.factor = av_mul_q64(op->scale.factor, next->scale.factor); |
722 | 0 | ff_sws_op_list_remove_at(ops, n + 1, 1); |
723 | 0 | goto retry; |
724 | 0 | } |
725 | | |
726 | | /* Scaling by exact power of two */ |
727 | 0 | if (factor2 && ff_sws_pixel_type_is_int(op->type)) { |
728 | 0 | op->op = factor2 > 0 ? SWS_OP_LSHIFT : SWS_OP_RSHIFT; |
729 | 0 | op->shift.amount = FFABS(factor2); |
730 | 0 | goto retry; |
731 | 0 | } |
732 | 0 | break; |
733 | 0 | } |
734 | | |
735 | 0 | case SWS_OP_FILTER_H: |
736 | 0 | case SWS_OP_FILTER_V: |
737 | | /* Merge with prior simple planar read */ |
738 | 0 | if (prev->op == SWS_OP_READ && !prev->rw.filter.op && |
739 | 0 | prev->rw.mode == SWS_RW_PLANAR && !prev->rw.frac) { |
740 | 0 | prev->rw.filter.op = op->op; |
741 | 0 | prev->rw.filter.kernel = av_refstruct_ref(op->filter.kernel); |
742 | 0 | prev->rw.filter.type = op->filter.type; |
743 | 0 | ff_sws_op_list_remove_at(ops, n, 1); |
744 | 0 | goto retry; |
745 | 0 | } |
746 | 0 | break; |
747 | | |
748 | 0 | case SWS_OP_LUT_3D: |
749 | | /* Eliminate unnecessary 3DLUT */ |
750 | 0 | if (!(needed & SWS_COMP_ELEMS(3))) { |
751 | 0 | ff_sws_op_list_remove_at(ops, n, 1); |
752 | 0 | goto retry; |
753 | 0 | } |
754 | 0 | break; |
755 | 0 | } |
756 | 0 | } |
757 | | |
758 | | /* Push clears to the back to void any unused components */ |
759 | 0 | for (int n = 0; n < ops->num_ops - 1; n++) { |
760 | 0 | SwsOp *op = &ops->ops[n]; |
761 | 0 | SwsOp *next = &ops->ops[n + 1]; |
762 | |
|
763 | 0 | switch (op->op) { |
764 | 0 | case SWS_OP_CLEAR: |
765 | 0 | if (op_commute_clear(op, next)) { |
766 | 0 | FFSWAP(SwsOp, *op, *next); |
767 | 0 | goto retry; |
768 | 0 | } |
769 | 0 | break; |
770 | 0 | } |
771 | 0 | } |
772 | | |
773 | | /* Apply any remaining preferential re-ordering optimizations; do these |
774 | | * last because they are more likely to block other optimizations if done |
775 | | * too aggressively */ |
776 | 0 | for (int n = 0; n < ops->num_ops - 1; n++) { |
777 | 0 | SwsOp *op = &ops->ops[n]; |
778 | 0 | SwsOp *next = &ops->ops[n + 1]; |
779 | |
|
780 | 0 | switch (op->op) { |
781 | 0 | case SWS_OP_SWIZZLE: { |
782 | | /* Try to push swizzles towards the output */ |
783 | 0 | if (op_commute_swizzle(op, next)) { |
784 | 0 | FFSWAP(SwsOp, *op, *next); |
785 | 0 | goto retry; |
786 | 0 | } |
787 | 0 | break; |
788 | 0 | } |
789 | | |
790 | 0 | case SWS_OP_SCALE: |
791 | | /* Exact integer multiplication */ |
792 | 0 | if (op->scale.factor.den == 1 && next->op == SWS_OP_CONVERT && |
793 | 0 | ff_sws_pixel_type_is_int(next->convert.to) && |
794 | 0 | op_result_is_exact(op)) |
795 | 0 | { |
796 | 0 | op->type = next->convert.to; |
797 | 0 | FFSWAP(SwsOp, *op, *next); |
798 | 0 | goto retry; |
799 | 0 | } |
800 | 0 | break; |
801 | 0 | } |
802 | 0 | } |
803 | | |
804 | 0 | return 0; |
805 | 0 | } |
806 | | |
807 | | static int select_planes(SwsOpList *ops, SwsCompMask planes) |
808 | 0 | { |
809 | 0 | SwsSwizzleOp swiz = SWS_SWIZZLE(0, 1, 2, 3); |
810 | 0 | SwsOp *write = &ops->ops[ops->num_ops - 1]; |
811 | 0 | av_assert0(write->op == SWS_OP_WRITE); |
812 | | |
813 | 0 | write->rw.elems = 0; |
814 | 0 | for (int src = 0; src < 4; src++) { |
815 | 0 | if (!SWS_COMP_TEST(planes, src)) |
816 | 0 | continue; /* plane not selected */ |
817 | 0 | const int dst = write->rw.elems++; |
818 | 0 | av_assert2(src >= dst); |
819 | 0 | swiz.in[dst] = src; |
820 | 0 | FFSWAP(int, ops->plane_dst[dst], ops->plane_dst[src]); |
821 | 0 | } |
822 | | |
823 | | /* Insert swizzle to select desired planes */ |
824 | 0 | int ret = ff_sws_op_list_insert_at(ops, ops->num_ops - 1, &(SwsOp) { |
825 | 0 | .op = SWS_OP_SWIZZLE, |
826 | 0 | .type = write->type, |
827 | 0 | .swizzle = swiz, |
828 | 0 | }); |
829 | 0 | if (ret < 0) |
830 | 0 | return ret; |
831 | | |
832 | | /* The optimizer will take care of the rest */ |
833 | 0 | return ff_sws_op_list_optimize(ops); |
834 | 0 | } |
835 | | |
836 | | int ff_sws_op_list_split_planes(SwsOpList *ops1, SwsOpList **out_ops2, SwsCompMask planes) |
837 | 0 | { |
838 | 0 | const SwsOp *write = ff_sws_op_list_output(ops1); |
839 | 0 | if (!write || write->rw.mode != SWS_RW_PLANAR) { |
840 | 0 | *out_ops2 = NULL; |
841 | 0 | return 0; |
842 | 0 | } |
843 | | |
844 | 0 | const SwsCompMask full = SWS_COMP_ELEMS(write->rw.elems); |
845 | 0 | const SwsCompMask mask1 = planes & full; |
846 | 0 | const SwsCompMask mask2 = full ^ mask1; |
847 | 0 | if (!mask1 || !mask2) { |
848 | | /* Nothing to filter */ |
849 | 0 | *out_ops2 = NULL; |
850 | 0 | return 0; |
851 | 0 | } |
852 | | |
853 | 0 | SwsOpList *ops2 = ff_sws_op_list_duplicate(ops1); |
854 | 0 | if (!ops2) |
855 | 0 | return AVERROR(ENOMEM); |
856 | | |
857 | 0 | int ret; |
858 | 0 | if ((ret = select_planes(ops1, mask1)) < 0 || |
859 | 0 | (ret = select_planes(ops2, mask2)) < 0) |
860 | 0 | { |
861 | 0 | ff_sws_op_list_free(&ops2); |
862 | 0 | return ret; |
863 | 0 | } |
864 | | |
865 | 0 | *out_ops2 = ops2; |
866 | 0 | return 0; |
867 | 0 | } |
868 | | |
869 | | int ff_sws_shuffle_mask(const SwsUOp *uop, int8_t shuffle[], int size) |
870 | 0 | { |
871 | 0 | const SwsShuffleUOp *par = &uop->par.shuffle; |
872 | 0 | av_assert1(uop->uop == SWS_UOP_RW_SHUFFLE); |
873 | 0 | av_assert1(par->write_size <= sizeof(uop->data.shuffle.mask)); |
874 | 0 | av_assert1(size <= INT8_MAX); |
875 | |
|
876 | 0 | const int num_groups = size / FFMAX(par->read_size, par->write_size); |
877 | 0 | if (!num_groups) |
878 | 0 | return AVERROR(EINVAL); |
879 | | |
880 | 0 | memset(shuffle, 0, size); |
881 | 0 | for (int n = 0; n < num_groups; n++) { |
882 | 0 | const int base_in = n * par->read_size; |
883 | 0 | const int base_out = n * par->write_size; |
884 | 0 | for (int i = 0; i < par->write_size; i++) { |
885 | 0 | const int8_t idx = uop->data.shuffle.mask[i]; |
886 | 0 | shuffle[base_out + i] = idx + (idx >= 0) * base_in; |
887 | 0 | } |
888 | 0 | } |
889 | |
|
890 | 0 | return num_groups; |
891 | 0 | } |
892 | | |
893 | | static bool pixel_is_repeating(SwsPixelType type, SwsPixel val) |
894 | 0 | { |
895 | 0 | switch (ff_sws_pixel_type_size(type)) { |
896 | 0 | case 1: return true; |
897 | 0 | case 2: return val.u16 == val.u8 * 0x101ul; |
898 | 0 | case 4: return val.u32 == val.u8 * 0x1010101ul; |
899 | 0 | default: break; |
900 | 0 | } |
901 | | |
902 | 0 | av_unreachable("Invalid pixel type!"); |
903 | 0 | return false; |
904 | 0 | } |
905 | | |
906 | | static int solve_shuffle(const SwsUOpList *const uops, SwsUOp *out) |
907 | 0 | { |
908 | 0 | if (!uops->num_ops) |
909 | 0 | return AVERROR(EINVAL); |
910 | 0 | const SwsUOp *read = &uops->ops[0]; |
911 | 0 | switch (read->uop) { |
912 | 0 | case SWS_UOP_READ_PACKED: |
913 | 0 | break; |
914 | 0 | case SWS_UOP_READ_PLANAR: |
915 | 0 | if (read->mask != SWS_COMP_ELEMS(1)) |
916 | 0 | return AVERROR(ENOTSUP); |
917 | 0 | break; |
918 | 0 | default: |
919 | 0 | return AVERROR(ENOTSUP); |
920 | 0 | } |
921 | | |
922 | 0 | const int read_size = ff_sws_pixel_type_size(read->type); |
923 | 0 | uint32_t mask[4] = {0}; |
924 | 0 | int clear_val = -1; |
925 | 0 | int read_elems = 0; |
926 | 0 | for (int i = 0; i < 4; i++) { |
927 | 0 | if (SWS_COMP_TEST(read->mask, i)) { |
928 | 0 | mask[i] = 0x01010101 * i * read_size + 0x03020100; |
929 | 0 | read_elems++; |
930 | 0 | } |
931 | 0 | } |
932 | |
|
933 | 0 | for (int opidx = 1; opidx < uops->num_ops; opidx++) { |
934 | 0 | const SwsUOp *uop = &uops->ops[opidx]; |
935 | 0 | const SwsUOpParams *par = &uop->par; |
936 | 0 | switch (uop->uop) { |
937 | 0 | case SWS_UOP_COPY: |
938 | 0 | case SWS_UOP_PERMUTE: { |
939 | 0 | uint32_t tmp; |
940 | 0 | for (int i = 0; i < par->move.num_moves; i++) { |
941 | 0 | const int dst_idx = par->move.dst[i]; |
942 | 0 | const int src_idx = par->move.src[i]; |
943 | 0 | uint32_t *src = src_idx < 0 ? &tmp : &mask[src_idx]; |
944 | 0 | uint32_t *dst = dst_idx < 0 ? &tmp : &mask[dst_idx]; |
945 | 0 | *dst = *src; |
946 | 0 | } |
947 | 0 | break; |
948 | 0 | } |
949 | | |
950 | 0 | case SWS_UOP_SWAP_BYTES: |
951 | 0 | for (int i = 0; i < 4; i++) { |
952 | 0 | switch (ff_sws_pixel_type_size(uop->type)) { |
953 | 0 | case 2: mask[i] = av_bswap16(mask[i]); break; |
954 | 0 | case 4: mask[i] = av_bswap32(mask[i]); break; |
955 | 0 | } |
956 | 0 | } |
957 | 0 | break; |
958 | | |
959 | 0 | case SWS_UOP_CLEAR: |
960 | 0 | for (int i = 0; i < 4; i++) { |
961 | 0 | if (!SWS_COMP_TEST(uop->mask, i)) |
962 | 0 | continue; |
963 | 0 | SwsPixel val = uop->data.vec4[i]; |
964 | 0 | if (!pixel_is_repeating(uop->type, val) || |
965 | 0 | (clear_val >= 0 && clear_val != val.u8)) |
966 | 0 | return AVERROR(ENOTSUP); /* would require different bytes */ |
967 | 0 | mask[i] = 0xFFFFFFFFul; /* (uint8_t[4]) { -1, -1, -1, -1 } */ |
968 | 0 | clear_val = val.u8; |
969 | 0 | } |
970 | 0 | break; |
971 | | |
972 | 0 | case SWS_UOP_EXPAND_PAIR: |
973 | 0 | case SWS_UOP_EXPAND_QUAD: |
974 | 0 | for (int i = 0; i < 4; i++) |
975 | 0 | mask[i] = 0x01010101 * (mask[i] & 0xFF); |
976 | 0 | break; |
977 | | |
978 | 0 | case SWS_UOP_WRITE_PLANAR: |
979 | 0 | if (uop->mask != SWS_COMP_ELEMS(1)) |
980 | 0 | return AVERROR(ENOTSUP); |
981 | 0 | av_fallthrough; |
982 | 0 | case SWS_UOP_WRITE_PACKED: { |
983 | 0 | const int write_elems = av_popcount(uop->mask); |
984 | 0 | const int write_size = ff_sws_pixel_type_size(uop->type); |
985 | 0 | *out = (SwsUOp) { |
986 | 0 | .uop = SWS_UOP_RW_SHUFFLE, |
987 | 0 | .type = SWS_PIXEL_U8, |
988 | 0 | .mask = SWS_COMP_ELEMS(1), /* single plane for now */ |
989 | 0 | }; |
990 | |
|
991 | 0 | SwsShuffleUOp *par = &out->par.shuffle; |
992 | 0 | SwsShuffleMask *data = &out->data.shuffle; |
993 | 0 | *par = (SwsShuffleUOp) { |
994 | 0 | .read_size = read_elems * read_size, |
995 | 0 | .write_size = write_elems * write_size, |
996 | 0 | .clear_value = clear_val >= 0 ? clear_val : 0, |
997 | 0 | }; |
998 | | |
999 | | /* Generate baseline shuffle for a single pixel */ |
1000 | 0 | data->pixels = 1; |
1001 | 0 | for (int i = 0; i < write_elems; i++) { |
1002 | 0 | const int offset = i * write_size; |
1003 | 0 | for (int b = 0; b < write_size; b++) |
1004 | 0 | data->mask[offset + b] = mask[i] >> (b * 8); |
1005 | 0 | } |
1006 | | |
1007 | | /* Expand as many times as needed to round up to the size of the |
1008 | | * shuffle uop data mask */ |
1009 | 0 | int8_t tmp[FF_ARRAY_ELEMS(data->mask)]; |
1010 | 0 | const int num_groups = ff_sws_shuffle_mask(out, tmp, sizeof(tmp)); |
1011 | 0 | if (num_groups < 0) |
1012 | 0 | return num_groups; |
1013 | 0 | memcpy(data->mask, tmp, sizeof(tmp)); |
1014 | 0 | par->read_size *= num_groups; |
1015 | 0 | par->write_size *= num_groups; |
1016 | 0 | data->pixels = num_groups; |
1017 | 0 | return 0; |
1018 | 0 | } |
1019 | | |
1020 | 0 | default: |
1021 | 0 | return AVERROR(ENOTSUP); |
1022 | 0 | } |
1023 | 0 | } |
1024 | | |
1025 | 0 | return AVERROR(EINVAL); |
1026 | 0 | } |
1027 | | |
1028 | | int ff_sws_uop_list_optimize(SwsContext *ctx, SwsUOpFlags flags, SwsUOpList *uops) |
1029 | 0 | { |
1030 | | /* Try promoting the entire uop list to a packed shuffle operation */ |
1031 | 0 | if (flags & SWS_UOP_FLAG_PSHUFB) { |
1032 | 0 | SwsUOp shuffle; |
1033 | 0 | int ret = solve_shuffle(uops, &shuffle); |
1034 | 0 | if (ret >= 0) { |
1035 | 0 | ff_sws_uop_list_remove_at(uops, 0, uops->num_ops); |
1036 | 0 | return ff_sws_uop_list_append(uops, &shuffle); |
1037 | 0 | } else if (ret < 0 && ret != AVERROR(ENOTSUP)) { |
1038 | 0 | return ret; |
1039 | 0 | } |
1040 | 0 | } |
1041 | | |
1042 | | #if 0 |
1043 | | static const SwsUOp dummy = {0}; |
1044 | | |
1045 | | retry: |
1046 | | for (int i = 0; i < uops->num_ops; i++) { |
1047 | | const SwsUOp *next = i < uops->num_ops - 1 ? &uops->ops[i + 1] : &dummy; |
1048 | | SwsUOp *op = &uops->ops[i]; |
1049 | | |
1050 | | switch (op->uop) { |
1051 | | /* placeholder */ |
1052 | | } |
1053 | | } |
1054 | | #endif |
1055 | | |
1056 | 0 | return 0; |
1057 | 0 | } |
1058 | | |
1059 | | /** |
1060 | | * Determine a suitable intermediate buffer format for a given combination |
1061 | | * of pixel types and number of planes. The exact interpretation of these |
1062 | | * formats does not matter at all; since they will only ever be used as |
1063 | | * temporary intermediate buffers. We still need to pick *some* format as |
1064 | | * a consequence of ff_sws_graph_add_pass() taking an AVPixelFormat for the |
1065 | | * output buffer. |
1066 | | */ |
1067 | | static enum AVPixelFormat get_planar_fmt(SwsPixelType type, int nb_planes) |
1068 | 0 | { |
1069 | 0 | switch (ff_sws_pixel_type_size(type)) { |
1070 | 0 | case 1: |
1071 | 0 | switch (nb_planes) { |
1072 | 0 | case 1: return AV_PIX_FMT_GRAY8; |
1073 | 0 | case 2: return AV_PIX_FMT_YUV444P; // FIXME: no 2-plane planar fmt |
1074 | 0 | case 3: return AV_PIX_FMT_YUV444P; |
1075 | 0 | case 4: return AV_PIX_FMT_YUVA444P; |
1076 | 0 | } |
1077 | 0 | break; |
1078 | 0 | case 2: |
1079 | 0 | switch (nb_planes) { |
1080 | 0 | case 1: return AV_PIX_FMT_GRAY16; |
1081 | 0 | case 2: return AV_PIX_FMT_YUV444P16; // FIXME: no 2-plane planar fmt |
1082 | 0 | case 3: return AV_PIX_FMT_YUV444P16; |
1083 | 0 | case 4: return AV_PIX_FMT_YUVA444P16; |
1084 | 0 | } |
1085 | 0 | break; |
1086 | 0 | case 4: |
1087 | 0 | switch (nb_planes) { |
1088 | 0 | case 1: return AV_PIX_FMT_GRAYF32; |
1089 | 0 | case 2: return AV_PIX_FMT_GBRPF32; // FIXME: no 2-plane planar fmt |
1090 | 0 | case 3: return AV_PIX_FMT_GBRPF32; |
1091 | 0 | case 4: return AV_PIX_FMT_GBRAPF32; |
1092 | 0 | } |
1093 | 0 | break; |
1094 | 0 | } |
1095 | | |
1096 | 0 | av_unreachable("Invalid pixel type or number of planes?"); |
1097 | 0 | return AV_PIX_FMT_NONE; |
1098 | 0 | } |
1099 | | |
1100 | | static void get_input_size(const SwsOpList *ops, SwsFormat *fmt) |
1101 | 0 | { |
1102 | 0 | fmt->width = ops->src.width; |
1103 | 0 | fmt->height = ops->src.height; |
1104 | |
|
1105 | 0 | const SwsOp *read = ff_sws_op_list_input(ops); |
1106 | 0 | if (read && read->rw.filter.op == SWS_OP_FILTER_V) { |
1107 | 0 | fmt->height = read->rw.filter.kernel->dst_size; |
1108 | 0 | } else if (read && read->rw.filter.op == SWS_OP_FILTER_H) { |
1109 | 0 | fmt->width = read->rw.filter.kernel->dst_size; |
1110 | 0 | } |
1111 | 0 | } |
1112 | | |
1113 | | int ff_sws_op_list_split_at(SwsOpList *ops1, SwsOpList **out_ops2, int index) |
1114 | 0 | { |
1115 | 0 | int ret; |
1116 | 0 | if (index <= 0 || index >= ops1->num_ops) { |
1117 | 0 | *out_ops2 = NULL; |
1118 | 0 | return 0; |
1119 | 0 | } |
1120 | | |
1121 | 0 | const SwsOp *op = &ops1->ops[index]; |
1122 | 0 | const SwsOp *prev = &ops1->ops[index - 1]; |
1123 | |
|
1124 | 0 | SwsOpList *ops2 = ff_sws_op_list_duplicate(ops1); |
1125 | 0 | if (!ops2) |
1126 | 0 | return AVERROR(ENOMEM); |
1127 | | |
1128 | | /** |
1129 | | * Not all components may be needed; but we need the ones that *are* |
1130 | | * used to be contiguous for the write/read operations. So, first |
1131 | | * compress them into a linearly ascending list of components |
1132 | | */ |
1133 | 0 | int nb_planes = 0; |
1134 | 0 | SwsSwizzleOp swiz_wr = SWS_SWIZZLE(0, 1, 2, 3); |
1135 | 0 | SwsSwizzleOp swiz_rd = SWS_SWIZZLE(0, 1, 2, 3); |
1136 | 0 | for (int i = 0; i < 4; i++) { |
1137 | 0 | if (SWS_OP_NEEDED(prev, i)) { |
1138 | 0 | const int o = nb_planes++; |
1139 | 0 | swiz_wr.in[o] = i; |
1140 | 0 | swiz_rd.in[i] = o; |
1141 | 0 | } |
1142 | 0 | } |
1143 | | |
1144 | | /* Determine metadata for the intermediate format */ |
1145 | 0 | const SwsPixelType type = op->type; |
1146 | 0 | ops2->src.format = get_planar_fmt(type, nb_planes); |
1147 | 0 | ops2->src.desc = av_pix_fmt_desc_get(ops2->src.format); |
1148 | 0 | get_input_size(ops1, &ops2->src); |
1149 | 0 | ops1->dst = ops2->src; |
1150 | |
|
1151 | 0 | for (int i = 0; i < nb_planes; i++) { |
1152 | 0 | const int idx = swiz_wr.in[i]; |
1153 | 0 | ops1->plane_dst[i] = ops2->plane_src[i] = i; |
1154 | 0 | ops2->comps_src.flags[i] = prev->comps.flags[idx]; |
1155 | 0 | ops2->comps_src.min[i] = prev->comps.min[idx]; |
1156 | 0 | ops2->comps_src.max[i] = prev->comps.max[idx]; |
1157 | 0 | } |
1158 | |
|
1159 | 0 | ff_sws_op_list_remove_at(ops1, index, ops1->num_ops - index); |
1160 | 0 | ff_sws_op_list_remove_at(ops2, 0, index); |
1161 | 0 | op = NULL; /* the above command may invalidate op */ |
1162 | |
|
1163 | 0 | if (swiz_wr.mask != SWS_SWIZZLE(0, 1, 2, 3).mask) { |
1164 | 0 | ret = ff_sws_op_list_append(ops1, &(SwsOp) { |
1165 | 0 | .op = SWS_OP_SWIZZLE, |
1166 | 0 | .type = type, |
1167 | 0 | .swizzle = swiz_wr, |
1168 | 0 | }); |
1169 | 0 | if (ret < 0) |
1170 | 0 | goto fail; |
1171 | 0 | } |
1172 | | |
1173 | 0 | ret = ff_sws_op_list_append(ops1, &(SwsOp) { |
1174 | 0 | .op = SWS_OP_WRITE, |
1175 | 0 | .type = type, |
1176 | 0 | .rw.elems = nb_planes, |
1177 | 0 | }); |
1178 | 0 | if (ret < 0) |
1179 | 0 | goto fail; |
1180 | | |
1181 | 0 | ret = ff_sws_op_list_insert_at(ops2, 0, &(SwsOp) { |
1182 | 0 | .op = SWS_OP_READ, |
1183 | 0 | .type = type, |
1184 | 0 | .rw.elems = nb_planes, |
1185 | 0 | }); |
1186 | 0 | if (ret < 0) |
1187 | 0 | goto fail; |
1188 | | |
1189 | 0 | if (swiz_rd.mask != SWS_SWIZZLE(0, 1, 2, 3).mask) { |
1190 | 0 | ret = ff_sws_op_list_insert_at(ops2, 1, &(SwsOp) { |
1191 | 0 | .op = SWS_OP_SWIZZLE, |
1192 | 0 | .type = type, |
1193 | 0 | .swizzle = swiz_rd, |
1194 | 0 | }); |
1195 | 0 | if (ret < 0) |
1196 | 0 | goto fail; |
1197 | 0 | } |
1198 | | |
1199 | 0 | ret = ff_sws_op_list_optimize(ops1); |
1200 | 0 | if (ret < 0) |
1201 | 0 | goto fail; |
1202 | | |
1203 | 0 | ret = ff_sws_op_list_optimize(ops2); |
1204 | 0 | if (ret < 0) |
1205 | 0 | goto fail; |
1206 | | |
1207 | 0 | *out_ops2 = ops2; |
1208 | 0 | return 0; |
1209 | | |
1210 | 0 | fail: |
1211 | 0 | ff_sws_op_list_free(&ops2); |
1212 | 0 | return ret; |
1213 | 0 | } |