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