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