/src/ffmpeg/libswscale/ops_dispatch.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/avassert.h" |
22 | | #include "libavutil/cpu.h" |
23 | | #include "libavutil/mathematics.h" |
24 | | #include "libavutil/mem.h" |
25 | | #include "libavutil/mem_internal.h" |
26 | | #include "libavutil/refstruct.h" |
27 | | |
28 | | #include "ops.h" |
29 | | #include "ops_internal.h" |
30 | | #include "ops_dispatch.h" |
31 | | #include "swscale_internal.h" |
32 | | |
33 | | #define RET(x) \ |
34 | 0 | do { \ |
35 | 0 | if ((ret = (x)) < 0) \ |
36 | 0 | goto fail; \ |
37 | 0 | } while (0) |
38 | | |
39 | | typedef struct SwsOpPass { |
40 | | SwsCompiledOp comp; |
41 | | SwsOpExec exec_base; |
42 | | SwsOpExec exec_tail; |
43 | | size_t num_blocks; |
44 | | int tail_off_in; |
45 | | int tail_off_out; |
46 | | int tail_size_in; |
47 | | int tail_size_out; |
48 | | int planes_in; |
49 | | int planes_out; |
50 | | int pixel_bits_in; |
51 | | int pixel_bits_out; |
52 | | int idx_in[4]; |
53 | | int idx_out[4]; |
54 | | int palette_idx; |
55 | | int *offsets_y; |
56 | | int filter_size_h; |
57 | | bool memcpy_first; |
58 | | bool memcpy_last; |
59 | | bool memcpy_out; |
60 | | size_t tail_blocks; |
61 | | uint8_t *tail_buf; /* extra memory for fixing unpadded tails */ |
62 | | unsigned int tail_buf_size; |
63 | | } SwsOpPass; |
64 | | |
65 | | static int compile_backend(SwsContext *ctx, const SwsOpBackend *backend, |
66 | | const SwsOpList *ops, SwsCompiledOp *out) |
67 | 0 | { |
68 | 0 | SwsOpList *copy; |
69 | 0 | SwsCompiledOp compiled = {0}; |
70 | 0 | int ret = 0; |
71 | |
|
72 | 0 | copy = ff_sws_op_list_duplicate(ops); |
73 | 0 | if (!copy) |
74 | 0 | return AVERROR(ENOMEM); |
75 | | |
76 | | /* Ensure these are always set during compilation */ |
77 | 0 | ff_sws_op_list_update_comps(copy); |
78 | |
|
79 | 0 | ret = backend->compile(ctx, copy, &compiled); |
80 | 0 | if (ret < 0) { |
81 | 0 | int msg_lev = ret == AVERROR(ENOTSUP) ? AV_LOG_TRACE : AV_LOG_ERROR; |
82 | 0 | av_log(ctx, msg_lev, "Backend '%s' failed to compile operations: %s\n", |
83 | 0 | backend->name, av_err2str(ret)); |
84 | 0 | goto fail; |
85 | 0 | } |
86 | | |
87 | 0 | compiled.backend = backend; |
88 | 0 | *out = compiled; |
89 | |
|
90 | 0 | av_log(ctx, AV_LOG_VERBOSE, "Compiled using backend '%s': " |
91 | 0 | "block size = %d, over-read = {%d %d %d %d}, over-write = {%d %d %d %d}, " |
92 | 0 | "cpu flags = 0x%x\n", backend->name, out->block_size, |
93 | 0 | out->over_read[0], out->over_read[1], |
94 | 0 | out->over_read[2], out->over_read[3], |
95 | 0 | out->over_write[0], out->over_write[1], |
96 | 0 | out->over_write[2], out->over_write[3], |
97 | 0 | out->cpu_flags); |
98 | |
|
99 | 0 | ff_sws_op_list_print(ctx, AV_LOG_VERBOSE, AV_LOG_TRACE, ops); |
100 | |
|
101 | 0 | fail: |
102 | 0 | ff_sws_op_list_free(©); |
103 | 0 | return ret; |
104 | 0 | } |
105 | | |
106 | | int ff_sws_ops_compile(SwsContext *ctx, const SwsOpBackend *backend, |
107 | | const SwsOpList *ops, SwsCompiledOp *out) |
108 | 0 | { |
109 | 0 | if (backend) |
110 | 0 | return compile_backend(ctx, backend, ops, out); |
111 | | |
112 | 0 | const SwsBackend enabled = ff_sws_enabled_backends(ctx); |
113 | 0 | for (int n = 0; ff_sws_op_backends[n]; n++) { |
114 | 0 | const SwsOpBackend *backend = ff_sws_op_backends[n]; |
115 | 0 | if (ops->src.hw_format != backend->hw_format || |
116 | 0 | ops->dst.hw_format != backend->hw_format || |
117 | 0 | !(enabled & backend->flags)) |
118 | 0 | continue; |
119 | 0 | if (compile_backend(ctx, backend, ops, out) < 0) |
120 | 0 | continue; |
121 | | |
122 | 0 | return 0; |
123 | 0 | } |
124 | | |
125 | 0 | return AVERROR(ENOTSUP); |
126 | 0 | } |
127 | | |
128 | | void ff_sws_compiled_op_unref(SwsCompiledOp *comp) |
129 | 0 | { |
130 | 0 | if (comp->free) |
131 | 0 | comp->free(comp->priv); |
132 | |
|
133 | 0 | *comp = (SwsCompiledOp) {0}; |
134 | 0 | } |
135 | | |
136 | | static void op_pass_free(void *ptr) |
137 | 0 | { |
138 | 0 | SwsOpPass *p = ptr; |
139 | 0 | if (!p) |
140 | 0 | return; |
141 | | |
142 | 0 | ff_sws_compiled_op_unref(&p->comp); |
143 | 0 | av_refstruct_unref(&p->offsets_y); |
144 | 0 | av_free(p->exec_base.in_bump_y); |
145 | 0 | av_free(p->exec_base.in_offset_x); |
146 | 0 | av_free(p->tail_buf); |
147 | 0 | av_free(p); |
148 | 0 | } |
149 | | |
150 | | static inline void get_row_data(const SwsOpPass *p, const int y_dst, |
151 | | const uint8_t *in[4], uint8_t *out[4]) |
152 | 0 | { |
153 | 0 | const SwsOpExec *base = &p->exec_base; |
154 | 0 | const int y_src = p->offsets_y ? p->offsets_y[y_dst] : y_dst; |
155 | 0 | for (int i = 0; i < p->planes_in; i++) |
156 | 0 | in[i] = base->in[i] + (y_src >> base->in_sub_y[i]) * base->in_stride[i]; |
157 | 0 | for (int i = 0; i < p->planes_out; i++) |
158 | 0 | out[i] = base->out[i] + (y_dst >> base->out_sub_y[i]) * base->out_stride[i]; |
159 | 0 | } |
160 | | |
161 | | static inline int get_lines_in(const SwsOpPass *p, const int y, const int h, |
162 | | const int plane) |
163 | 0 | { |
164 | 0 | const SwsOpExec *base = &p->exec_base; |
165 | 0 | if (!p->offsets_y) |
166 | 0 | return h >> base->in_sub_y[plane]; |
167 | | |
168 | 0 | const int y0 = p->offsets_y[y] >> base->in_sub_y[plane]; |
169 | 0 | const int y1 = p->offsets_y[y + h - 1] >> base->in_sub_y[plane]; |
170 | 0 | return y1 - y0 + 1; |
171 | 0 | } |
172 | | |
173 | | static inline size_t pixel_bytes(size_t pixels, int pixel_bits, |
174 | | enum AVRounding rounding) |
175 | 0 | { |
176 | 0 | const uint64_t bits = (uint64_t) pixels * pixel_bits; |
177 | 0 | switch (rounding) { |
178 | 0 | case AV_ROUND_ZERO: |
179 | 0 | case AV_ROUND_DOWN: |
180 | 0 | return bits >> 3; |
181 | 0 | case AV_ROUND_INF: |
182 | 0 | case AV_ROUND_UP: |
183 | 0 | return (bits + 7) >> 3; |
184 | 0 | default: |
185 | 0 | av_unreachable("Invalid rounding mode"); |
186 | 0 | return (size_t) -1; |
187 | 0 | } |
188 | 0 | } |
189 | | |
190 | | static size_t safe_bytes_pad(int linesize, int plane_pad) |
191 | 0 | { |
192 | 0 | av_assert1(linesize); |
193 | 0 | int64_t safe_bytes = FFABS((int64_t) linesize) - plane_pad; |
194 | 0 | return FFMAX(safe_bytes, 0); |
195 | 0 | } |
196 | | |
197 | | static size_t safe_blocks_offset(size_t num_blocks, unsigned block_size, |
198 | | ptrdiff_t safe_offset, |
199 | | const int32_t *offset_bytes) |
200 | 0 | { |
201 | 0 | size_t safe_blocks = num_blocks; |
202 | 0 | while (safe_blocks && offset_bytes[safe_blocks * block_size - 1] > safe_offset) |
203 | 0 | safe_blocks--; |
204 | 0 | return safe_blocks; |
205 | 0 | } |
206 | | |
207 | | static int op_pass_setup(const SwsFrame *out, const SwsFrame *in, |
208 | | const SwsPass *pass) |
209 | 0 | { |
210 | 0 | const AVPixFmtDescriptor *indesc = av_pix_fmt_desc_get(in->format); |
211 | 0 | const bool float_in = indesc->flags & AV_PIX_FMT_FLAG_FLOAT; |
212 | 0 | const int width = out->width; |
213 | |
|
214 | 0 | SwsOpPass *p = pass->priv; |
215 | 0 | SwsOpExec *exec = &p->exec_base; |
216 | 0 | const SwsCompiledOp *comp = &p->comp; |
217 | | |
218 | | /* Set up main loop parameters */ |
219 | 0 | const unsigned block_size = comp->block_size; |
220 | 0 | const size_t num_blocks = (width + block_size - 1) / block_size; |
221 | 0 | const size_t aligned_w = num_blocks * block_size; |
222 | 0 | if (aligned_w < width) /* overflow */ |
223 | 0 | return AVERROR(EINVAL); |
224 | 0 | p->num_blocks = num_blocks; |
225 | 0 | p->memcpy_first = false; |
226 | 0 | p->memcpy_last = false; |
227 | 0 | p->memcpy_out = false; |
228 | |
|
229 | 0 | size_t safe_blocks = num_blocks; |
230 | 0 | for (int i = 0; i < p->planes_in; i++) { |
231 | 0 | const int idx = p->idx_in[i]; |
232 | 0 | size_t input_bytes = in->linesize[idx]; |
233 | 0 | if (p->filter_size_h && float_in) { |
234 | | /* Floating point inputs may contain NaN / Infinity in the padding */ |
235 | 0 | const int plane_w = AV_CEIL_RSHIFT(in->width, exec->in_sub_x[i]); |
236 | 0 | input_bytes = pixel_bytes(plane_w, p->pixel_bits_in, AV_ROUND_UP); |
237 | 0 | } |
238 | |
|
239 | 0 | size_t safe_bytes = safe_bytes_pad(input_bytes, comp->over_read[i]); |
240 | 0 | size_t safe_blocks_in; |
241 | 0 | if (exec->in_offset_x) { |
242 | 0 | size_t filter_size = pixel_bytes(p->filter_size_h, p->pixel_bits_in, |
243 | 0 | AV_ROUND_UP); |
244 | 0 | safe_blocks_in = safe_blocks_offset(num_blocks, block_size, |
245 | 0 | safe_bytes - filter_size, |
246 | 0 | exec->in_offset_x); |
247 | 0 | } else { |
248 | 0 | safe_blocks_in = safe_bytes / exec->block_size_in[i]; |
249 | 0 | } |
250 | |
|
251 | 0 | if (safe_blocks_in < num_blocks) { |
252 | 0 | p->memcpy_first |= in->linesize[idx] < 0; |
253 | 0 | p->memcpy_last |= in->linesize[idx] > 0; |
254 | 0 | safe_blocks = FFMIN(safe_blocks, safe_blocks_in); |
255 | 0 | } |
256 | |
|
257 | 0 | size_t loop_size = num_blocks * exec->block_size_in[i]; |
258 | 0 | exec->in[i] = in->data[idx]; |
259 | 0 | exec->in_stride[i] = in->linesize[idx]; |
260 | 0 | exec->in_bump[i] = in->linesize[idx] - loop_size; |
261 | 0 | } |
262 | |
|
263 | 0 | for (int i = 0; i < p->planes_out; i++) { |
264 | 0 | const int idx = p->idx_out[i]; |
265 | 0 | size_t safe_bytes = safe_bytes_pad(out->linesize[idx], comp->over_write[i]); |
266 | 0 | size_t safe_blocks_out = safe_bytes / exec->block_size_out[i]; |
267 | 0 | if (safe_blocks_out < num_blocks) { |
268 | 0 | p->memcpy_out = true; |
269 | 0 | safe_blocks = FFMIN(safe_blocks, safe_blocks_out); |
270 | 0 | } |
271 | |
|
272 | 0 | size_t loop_size = num_blocks * exec->block_size_out[i]; |
273 | 0 | exec->out[i] = out->data[idx]; |
274 | 0 | exec->out_stride[i] = out->linesize[idx]; |
275 | 0 | exec->out_bump[i] = out->linesize[idx] - loop_size; |
276 | 0 | } |
277 | |
|
278 | 0 | if (p->palette_idx >= 0) { |
279 | 0 | exec->in[1] = in->data[p->palette_idx]; |
280 | 0 | exec->in_stride[1] = exec->in_bump[1] = 0; |
281 | 0 | } |
282 | |
|
283 | 0 | const bool memcpy_in = p->memcpy_first || p->memcpy_last; |
284 | 0 | if (!memcpy_in && !p->memcpy_out) { |
285 | 0 | av_assert0(safe_blocks == num_blocks); |
286 | 0 | return 0; |
287 | 0 | } |
288 | | |
289 | | /* Set-up tail section parameters and buffers */ |
290 | 0 | SwsOpExec *tail = &p->exec_tail; |
291 | 0 | const int align = av_cpu_max_align(); |
292 | 0 | size_t alloc_size = 0; |
293 | 0 | *tail = *exec; |
294 | |
|
295 | 0 | const size_t safe_width = safe_blocks * block_size; |
296 | 0 | const size_t tail_size = width - safe_width; |
297 | 0 | p->tail_off_out = pixel_bytes(safe_width, p->pixel_bits_out, AV_ROUND_DOWN); |
298 | 0 | p->tail_size_out = pixel_bytes(tail_size, p->pixel_bits_out, AV_ROUND_UP); |
299 | 0 | p->tail_blocks = num_blocks - safe_blocks; |
300 | |
|
301 | 0 | if (exec->in_offset_x) { |
302 | 0 | p->tail_off_in = exec->in_offset_x[safe_width]; |
303 | 0 | p->tail_size_in = exec->in_offset_x[width - 1] - p->tail_off_in; |
304 | 0 | p->tail_size_in += pixel_bytes(p->filter_size_h, p->pixel_bits_in, AV_ROUND_UP); |
305 | 0 | } else { |
306 | 0 | p->tail_off_in = pixel_bytes(safe_width, p->pixel_bits_in, AV_ROUND_DOWN); |
307 | 0 | p->tail_size_in = pixel_bytes(tail_size, p->pixel_bits_in, AV_ROUND_UP); |
308 | 0 | } |
309 | |
|
310 | 0 | const size_t alloc_width = aligned_w - safe_width; |
311 | 0 | for (int i = 0; memcpy_in && i < p->planes_in; i++) { |
312 | 0 | size_t needed_size; |
313 | 0 | if (exec->in_offset_x) { |
314 | | /* The input offset map is already padded to multiples of the block |
315 | | * size, and clamps the input offsets to the image boundaries; so |
316 | | * we just need to compensate for the comp->over_read */ |
317 | 0 | needed_size = p->tail_size_in; |
318 | 0 | } else { |
319 | 0 | needed_size = pixel_bytes(alloc_width, p->pixel_bits_in, AV_ROUND_UP); |
320 | 0 | } |
321 | 0 | size_t loop_size = p->tail_blocks * exec->block_size_in[i]; |
322 | 0 | tail->in_stride[i] = FFALIGN(needed_size + comp->over_read[i], align); |
323 | 0 | tail->in_bump[i] = tail->in_stride[i] - loop_size; |
324 | 0 | alloc_size += tail->in_stride[i] * in->height; |
325 | 0 | } |
326 | |
|
327 | 0 | for (int i = 0; p->memcpy_out && i < p->planes_out; i++) { |
328 | 0 | size_t needed_size = pixel_bytes(alloc_width, p->pixel_bits_out, AV_ROUND_UP); |
329 | 0 | size_t loop_size = p->tail_blocks * exec->block_size_out[i]; |
330 | 0 | tail->out_stride[i] = FFALIGN(needed_size + comp->over_write[i], align); |
331 | 0 | tail->out_bump[i] = tail->out_stride[i] - loop_size; |
332 | 0 | alloc_size += tail->out_stride[i] * out->height; |
333 | 0 | } |
334 | |
|
335 | 0 | if (memcpy_in && exec->in_offset_x) { |
336 | | /* `in_offset_x` is indexed relative to the line start, not the start |
337 | | * of the section being processed; so we need to over-allocate this |
338 | | * array to the full width of the image, even though we will only |
339 | | * partially fill in the offsets relevant to the tail region */ |
340 | 0 | alloc_size += aligned_w * sizeof(*exec->in_offset_x); |
341 | 0 | } |
342 | |
|
343 | 0 | av_fast_mallocz(&p->tail_buf, &p->tail_buf_size, alloc_size); |
344 | 0 | if (!p->tail_buf) |
345 | 0 | return AVERROR(ENOMEM); |
346 | | |
347 | 0 | uint8_t *tail_buf = p->tail_buf; |
348 | 0 | for (int i = 0; memcpy_in && i < p->planes_in; i++) { |
349 | 0 | tail->in[i] = tail_buf; |
350 | 0 | tail_buf += tail->in_stride[i] * in->height; |
351 | 0 | } |
352 | |
|
353 | 0 | for (int i = 0; p->memcpy_out && i < p->planes_out; i++) { |
354 | 0 | tail->out[i] = tail_buf; |
355 | 0 | tail_buf += tail->out_stride[i] * out->height; |
356 | 0 | } |
357 | |
|
358 | 0 | if (memcpy_in && exec->in_offset_x) { |
359 | 0 | tail->in_offset_x = (int32_t *) tail_buf; |
360 | 0 | for (int i = safe_width; i < aligned_w; i++) |
361 | 0 | tail->in_offset_x[i] = exec->in_offset_x[i] - p->tail_off_in; |
362 | 0 | } |
363 | |
|
364 | 0 | return 0; |
365 | 0 | } |
366 | | |
367 | | static void copy_lines(uint8_t *dst, const size_t dst_stride, |
368 | | const uint8_t *src, const size_t src_stride, |
369 | | const int h, const size_t bytes) |
370 | 0 | { |
371 | 0 | for (int y = 0; y < h; y++) { |
372 | 0 | memcpy(dst, src, bytes); |
373 | 0 | dst += dst_stride; |
374 | 0 | src += src_stride; |
375 | 0 | } |
376 | 0 | } |
377 | | |
378 | | static void op_pass_run(const SwsFrame *out, const SwsFrame *in, const int y, |
379 | | const int h, const SwsPass *pass) |
380 | 0 | { |
381 | 0 | const SwsOpPass *p = pass->priv; |
382 | 0 | const SwsCompiledOp *comp = &p->comp; |
383 | | |
384 | | /* Fill exec metadata for this slice */ |
385 | 0 | DECLARE_ALIGNED_32(SwsOpExec, exec) = p->exec_base; |
386 | 0 | exec.slice_y = y; |
387 | 0 | exec.slice_h = h; |
388 | | |
389 | | /** |
390 | | * To ensure safety, we need to consider the following: |
391 | | * |
392 | | * 1. We can overread the input, unless this is the last line of an |
393 | | * unpadded buffer. All defined operations can handle arbitrary pixel |
394 | | * input, so overread of arbitrary data is fine. For flipped images, |
395 | | * this condition is actually *inverted* to where the first line is |
396 | | * the one at the end of the buffer. |
397 | | * |
398 | | * 2. We can overwrite the output, as long as we don't write more than the |
399 | | * amount of pixels that fit into one linesize. So we always need to |
400 | | * memcpy the last column on the output side if unpadded. |
401 | | */ |
402 | |
|
403 | 0 | const bool memcpy_in = p->memcpy_last && y + h == pass->lines || |
404 | 0 | p->memcpy_first && y == 0; |
405 | 0 | const bool memcpy_out = p->memcpy_out; |
406 | 0 | const size_t num_blocks = p->num_blocks; |
407 | 0 | const size_t tail_blocks = p->tail_blocks; |
408 | |
|
409 | 0 | get_row_data(p, y, exec.in, exec.out); |
410 | 0 | if (!memcpy_in && !memcpy_out) { |
411 | | /* Fast path (fully aligned/padded inputs and outputs) */ |
412 | 0 | comp->func(&exec, comp->priv, 0, y, num_blocks, y + h); |
413 | 0 | return; |
414 | 0 | } |
415 | | |
416 | | /* Non-aligned case (slow path); process main blocks as normal, and |
417 | | * a separate tail (via memcpy into an appropriately padded buffer) */ |
418 | 0 | if (num_blocks > tail_blocks) { |
419 | 0 | for (int i = 0; i < 4; i++) { |
420 | | /* We process fewer blocks, so the in_bump needs to be increased |
421 | | * to reflect that the plane pointers are left on the last block, |
422 | | * not the end of the processed line, after each loop iteration */ |
423 | 0 | exec.in_bump[i] += exec.block_size_in[i] * tail_blocks; |
424 | 0 | exec.out_bump[i] += exec.block_size_out[i] * tail_blocks; |
425 | 0 | } |
426 | |
|
427 | 0 | comp->func(&exec, comp->priv, 0, y, num_blocks - tail_blocks, y + h); |
428 | 0 | } |
429 | |
|
430 | 0 | DECLARE_ALIGNED_32(SwsOpExec, tail) = p->exec_tail; |
431 | 0 | tail.slice_y = y; |
432 | 0 | tail.slice_h = h; |
433 | |
|
434 | 0 | for (int i = 0; i < p->planes_in; i++) { |
435 | | /* Input offsets are relative to the base pointer */ |
436 | 0 | if (!exec.in_offset_x || memcpy_in) |
437 | 0 | exec.in[i] += p->tail_off_in; |
438 | 0 | tail.in[i] += y * tail.in_stride[i]; |
439 | 0 | } |
440 | 0 | for (int i = 0; i < p->planes_out; i++) { |
441 | 0 | exec.out[i] += p->tail_off_out; |
442 | 0 | tail.out[i] += y * tail.out_stride[i]; |
443 | 0 | } |
444 | |
|
445 | 0 | for (int i = 0; i < p->planes_in; i++) { |
446 | 0 | if (memcpy_in) { |
447 | 0 | const int lines = get_lines_in(p, y, h, i); |
448 | 0 | copy_lines((uint8_t *) tail.in[i], tail.in_stride[i], |
449 | 0 | exec.in[i], exec.in_stride[i], lines, p->tail_size_in); |
450 | 0 | } else { |
451 | | /* Reuse input pointers directly */ |
452 | 0 | const size_t loop_size = tail_blocks * exec.block_size_in[i]; |
453 | 0 | tail.in[i] = exec.in[i]; |
454 | 0 | tail.in_stride[i] = exec.in_stride[i]; |
455 | 0 | tail.in_bump[i] = exec.in_stride[i] - loop_size; |
456 | 0 | } |
457 | 0 | } |
458 | |
|
459 | 0 | for (int i = 0; !memcpy_out && i < p->planes_out; i++) { |
460 | | /* Reuse output pointers directly */ |
461 | 0 | const size_t loop_size = tail_blocks * exec.block_size_out[i]; |
462 | 0 | tail.out[i] = exec.out[i]; |
463 | 0 | tail.out_stride[i] = exec.out_stride[i]; |
464 | 0 | tail.out_bump[i] = exec.out_stride[i] - loop_size; |
465 | 0 | } |
466 | | |
467 | | /* Dispatch kernel over tail */ |
468 | 0 | av_assert1(tail_blocks > 0); |
469 | 0 | comp->func(&tail, comp->priv, num_blocks - tail_blocks, y, num_blocks, y + h); |
470 | |
|
471 | 0 | for (int i = 0; memcpy_out && i < p->planes_out; i++) { |
472 | 0 | const int lines = h >> tail.out_sub_y[i]; |
473 | 0 | copy_lines(exec.out[i], exec.out_stride[i], |
474 | 0 | tail.out[i], tail.out_stride[i], lines, p->tail_size_out); |
475 | 0 | } |
476 | 0 | } |
477 | | |
478 | | static int rw_data_planes(const SwsOp *op) |
479 | 0 | { |
480 | | /* Exclude the palette plane from the plane count, since it does not need |
481 | | * to be directly processed/adjusted by the dispatch layer */ |
482 | 0 | return op->rw.mode == SWS_RW_PALETTE ? 1 : ff_sws_rw_op_planes(op); |
483 | 0 | } |
484 | | |
485 | | static int rw_pixel_bits(const SwsOp *op) |
486 | 0 | { |
487 | 0 | if (op->rw.mode == SWS_RW_PALETTE) |
488 | 0 | return 8; /* index size */ |
489 | | |
490 | 0 | int elems = 0; |
491 | 0 | switch (op->rw.mode) { |
492 | 0 | case SWS_RW_PLANAR: elems = 1; break; |
493 | 0 | case SWS_RW_PACKED: elems = op->rw.elems; break; |
494 | 0 | } |
495 | | |
496 | 0 | const int size = ff_sws_pixel_type_size(op->type); |
497 | 0 | const int bits = 8 >> op->rw.frac; |
498 | 0 | av_assert1(bits >= 1); |
499 | 0 | return elems * size * bits; |
500 | 0 | } |
501 | | |
502 | | static void align_pass(SwsPass *pass, int block_size, const int *over_rw, |
503 | | int pixel_bits) |
504 | 0 | { |
505 | 0 | if (!pass || pixel_bits <= 0) |
506 | 0 | return; |
507 | | |
508 | | /* Add at least as many pixels as needed to cover the padding requirement */ |
509 | 0 | int pad_max = 0; |
510 | 0 | for (int i = 0; i < 4; i++) { |
511 | 0 | const int pad = (over_rw[i] * 8 + pixel_bits - 1) / pixel_bits; |
512 | 0 | pad_max = FFMAX(pad_max, pad); |
513 | 0 | } |
514 | |
|
515 | 0 | SwsPassBuffer *buf = pass->output; |
516 | 0 | buf->width_align = FFMAX(buf->width_align, block_size); |
517 | 0 | buf->width_pad = FFMAX(buf->width_pad, pad_max); |
518 | 0 | } |
519 | | |
520 | | /* Unchanging part of parameter list */ |
521 | | typedef struct CompileArgs { |
522 | | const SwsOpBackend *backend; |
523 | | SwsGraph *graph; |
524 | | int flags; |
525 | | } CompileArgs; |
526 | | |
527 | | static int compile_single(const CompileArgs *args, const SwsOpList *ops, |
528 | | SwsPass *link, SwsPass *input, SwsPass **output) |
529 | 0 | { |
530 | 0 | SwsGraph *graph = args->graph; |
531 | 0 | SwsContext *ctx = graph->ctx; |
532 | 0 | SwsOpPass *p = av_mallocz(sizeof(*p)); |
533 | 0 | if (!p) |
534 | 0 | return AVERROR(ENOMEM); |
535 | | |
536 | 0 | int ret = ff_sws_ops_compile(ctx, args->backend, ops, &p->comp); |
537 | 0 | if (ret < 0) |
538 | 0 | goto fail; |
539 | 0 | else if (args->flags & SWS_OP_FLAG_DRY_RUN) |
540 | 0 | goto fail; /* nothing to do, just return */ |
541 | | |
542 | 0 | const SwsCompiledOp *comp = &p->comp; |
543 | 0 | const SwsFormat *src = &ops->src; |
544 | 0 | const SwsFormat *dst = &ops->dst; |
545 | 0 | av_assert0(!link || link->format == dst->format); |
546 | 0 | if (p->comp.opaque) { |
547 | 0 | SwsCompiledOp c = *comp; |
548 | 0 | av_free(p); |
549 | 0 | ret = ff_sws_graph_add_pass(graph, dst->format, dst->width, dst->height, |
550 | 0 | input, 0, c.slice_align, c.func_opaque, |
551 | 0 | NULL, c.priv, c.free, output); |
552 | 0 | if (ret >= 0) { |
553 | 0 | (*output)->backend = c.backend->flags; |
554 | 0 | ff_sws_pass_link_output(*output, link); |
555 | 0 | } |
556 | 0 | return ret; |
557 | 0 | } |
558 | | |
559 | 0 | const AVPixFmtDescriptor *indesc = av_pix_fmt_desc_get(src->format); |
560 | 0 | const AVPixFmtDescriptor *outdesc = av_pix_fmt_desc_get(dst->format); |
561 | 0 | const SwsOp *write = ff_sws_op_list_output(ops); |
562 | 0 | p->planes_out = rw_data_planes(write); |
563 | 0 | p->pixel_bits_out = rw_pixel_bits(write); |
564 | 0 | p->palette_idx = -1; |
565 | 0 | p->exec_base = (SwsOpExec) { |
566 | 0 | .width = dst->width, |
567 | 0 | .height = dst->height, |
568 | 0 | }; |
569 | |
|
570 | 0 | const SwsOp *read = ff_sws_op_list_input(ops); |
571 | 0 | if (read) { |
572 | 0 | p->planes_in = rw_data_planes(read); |
573 | 0 | p->pixel_bits_in = rw_pixel_bits(read); |
574 | 0 | if (read->rw.mode == SWS_RW_PALETTE) |
575 | 0 | p->palette_idx = ops->plane_src[1]; |
576 | 0 | } |
577 | |
|
578 | 0 | const int64_t block_bits_in = (int64_t) comp->block_size * p->pixel_bits_in; |
579 | 0 | const int64_t block_bits_out = (int64_t) comp->block_size * p->pixel_bits_out; |
580 | 0 | if (block_bits_in & 0x7 || block_bits_out & 0x7) { |
581 | 0 | av_log(ctx, AV_LOG_ERROR, "Block size must be byte-aligned.\n"); |
582 | 0 | ret = AVERROR(EINVAL); |
583 | 0 | goto fail; |
584 | 0 | } |
585 | | |
586 | 0 | for (int i = 0; i < 4; i++) |
587 | 0 | p->idx_in[i] = p->idx_out[i] = -1; |
588 | |
|
589 | 0 | for (int i = 0; i < p->planes_in; i++) { |
590 | 0 | const int idx = ops->plane_src[i]; |
591 | 0 | const int chroma = idx == 1 || idx == 2; |
592 | 0 | const int sub_x = chroma ? indesc->log2_chroma_w : 0; |
593 | 0 | const int sub_y = chroma ? indesc->log2_chroma_h : 0; |
594 | 0 | p->exec_base.in_sub_x[i] = sub_x; |
595 | 0 | p->exec_base.in_sub_y[i] = sub_y; |
596 | 0 | p->exec_base.block_size_in[i] = block_bits_in >> 3; |
597 | 0 | p->idx_in[i] = idx; |
598 | 0 | } |
599 | |
|
600 | 0 | for (int i = 0; i < p->planes_out; i++) { |
601 | 0 | const int idx = ops->plane_dst[i]; |
602 | 0 | const int chroma = idx == 1 || idx == 2; |
603 | 0 | const int sub_x = chroma ? outdesc->log2_chroma_w : 0; |
604 | 0 | const int sub_y = chroma ? outdesc->log2_chroma_h : 0; |
605 | 0 | p->exec_base.out_sub_x[i] = sub_x; |
606 | 0 | p->exec_base.out_sub_y[i] = sub_y; |
607 | 0 | p->exec_base.block_size_out[i] = block_bits_out >> 3; |
608 | 0 | p->idx_out[i] = idx; |
609 | 0 | } |
610 | |
|
611 | 0 | const SwsFilterWeights *filter = read ? read->rw.filter.kernel : NULL; |
612 | 0 | if (read && read->rw.filter.op == SWS_OP_FILTER_V) { |
613 | 0 | p->offsets_y = av_refstruct_ref(filter->offsets); |
614 | | |
615 | | /* Compute relative pointer bumps for each output line */ |
616 | 0 | int32_t *bump = av_malloc_array(filter->dst_size, sizeof(*bump)); |
617 | 0 | if (!bump) { |
618 | 0 | ret = AVERROR(ENOMEM); |
619 | 0 | goto fail; |
620 | 0 | } |
621 | | |
622 | 0 | int line = filter->offsets[0]; |
623 | 0 | for (int y = 0; y < filter->dst_size - 1; y++) { |
624 | 0 | int next = filter->offsets[y + 1]; |
625 | 0 | bump[y] = next - line - 1; |
626 | 0 | line = next; |
627 | 0 | } |
628 | 0 | bump[filter->dst_size - 1] = 0; |
629 | 0 | p->exec_base.in_bump_y = bump; |
630 | 0 | } else if (read && read->rw.filter.op == SWS_OP_FILTER_H) { |
631 | | /* Compute pixel offset map for each output line */ |
632 | 0 | const int pixels = FFALIGN(filter->dst_size, p->comp.block_size); |
633 | 0 | int32_t *offset = av_malloc_array(pixels, sizeof(*offset)); |
634 | 0 | if (!offset) { |
635 | 0 | ret = AVERROR(ENOMEM); |
636 | 0 | goto fail; |
637 | 0 | } |
638 | 0 | p->exec_base.in_offset_x = offset; |
639 | |
|
640 | 0 | for (int x = 0; x < filter->dst_size; x++) { |
641 | | /* Sanity check; if the tap would land on a half-pixel, we cannot |
642 | | * reasonably expect the implementation to know about this. Just |
643 | | * error out in such (theoretical) cases. */ |
644 | 0 | int64_t bits = (int64_t) filter->offsets[x] * p->pixel_bits_in; |
645 | 0 | if ((bits & 0x7) || (bits >> 3) > INT32_MAX) { |
646 | 0 | ret = AVERROR(EINVAL); |
647 | 0 | goto fail; |
648 | 0 | } |
649 | 0 | offset[x] = bits >> 3; |
650 | 0 | } |
651 | 0 | for (int x = filter->dst_size; x < pixels; x++) |
652 | 0 | offset[x] = offset[filter->dst_size - 1]; |
653 | 0 | for (int i = 0; i < 4; i++) |
654 | 0 | p->exec_base.block_size_in[i] = 0; /* ptr does not advance */ |
655 | 0 | p->filter_size_h = filter->filter_size; |
656 | 0 | } |
657 | | |
658 | 0 | ret = ff_sws_graph_add_pass(graph, dst->format, dst->width, dst->height, |
659 | 0 | input, 0, comp->slice_align, op_pass_run, |
660 | 0 | op_pass_setup, p, op_pass_free, output); |
661 | 0 | if (ret < 0) |
662 | 0 | return ret; |
663 | | |
664 | 0 | (*output)->backend = comp->backend->flags; |
665 | 0 | ff_sws_pass_link_output(*output, link); |
666 | 0 | align_pass(*output, comp->block_size, comp->over_write, p->pixel_bits_out); |
667 | 0 | if (read) |
668 | 0 | align_pass(input, comp->block_size, comp->over_read, p->pixel_bits_in); |
669 | 0 | return 0; |
670 | | |
671 | 0 | fail: |
672 | 0 | op_pass_free(p); |
673 | 0 | return ret; |
674 | 0 | } |
675 | | |
676 | | /* Return a mask of all planes matching any flag in `flags` */ |
677 | | static SwsCompMask plane_mask_flags(const SwsOp *op, SwsCompFlags flags) |
678 | 0 | { |
679 | 0 | SwsCompMask planes = 0; |
680 | 0 | for (int c = 0; c < 4; c++) { |
681 | 0 | if (op->comps.flags[c] & flags) |
682 | 0 | planes |= SWS_COMP(c); |
683 | 0 | } |
684 | |
|
685 | 0 | return planes; |
686 | 0 | } |
687 | | |
688 | | /* Takes over ownership of *pops, even on failure */ |
689 | | static int compile_subpass(const CompileArgs *args, SwsOpList **pops, |
690 | | SwsPass *link, SwsPass *input, SwsPass **output) |
691 | 0 | { |
692 | 0 | int ret; |
693 | 0 | SwsContext *ctx = args->graph->ctx; |
694 | 0 | SwsOpList *ops = *pops; |
695 | 0 | SwsOpList *rest = NULL; |
696 | 0 | SwsPass *tmp = NULL; |
697 | 0 | *pops = NULL; |
698 | |
|
699 | 0 | if (args->flags & SWS_OP_FLAG_SPLIT_MEMCPY) { |
700 | | /* Split off copied and constant planes into a separate subpass, |
701 | | * since these are likely to be handled by the memcpy backend */ |
702 | 0 | av_assert0(ops->num_ops >= 2); |
703 | 0 | const SwsOp *prev = &ops->ops[ops->num_ops - 2]; |
704 | 0 | SwsCompMask planes = plane_mask_flags(prev, SWS_COMP_COPY | SWS_COMP_CONST); |
705 | 0 | RET(ff_sws_op_list_split_planes(ops, &rest, planes)); |
706 | 0 | if (rest) { |
707 | | /* Parallel split: share input and link all outputs together */ |
708 | 0 | av_log(ctx, AV_LOG_DEBUG, "Splitting const/memcpy planes: %s\n", |
709 | 0 | ff_sws_comp_mask_str(planes)); |
710 | 0 | RET(compile_subpass(args, &ops, link, input, &tmp)); |
711 | 0 | RET(compile_subpass(args, &rest, tmp, input, output)); |
712 | 0 | return 0; |
713 | 0 | } |
714 | 0 | } |
715 | | |
716 | 0 | ret = compile_single(args, ops, link, input, output); |
717 | 0 | if (ret != AVERROR(ENOTSUP)) |
718 | 0 | goto fail; /* either success or a hard error */ |
719 | | |
720 | | /* Find any unresolved filter */ |
721 | 0 | for (int idx = 1; idx < ops->num_ops - 1; idx++) { |
722 | 0 | const SwsOp *op = &ops->ops[idx]; |
723 | 0 | if (op->op == SWS_OP_FILTER_H || op->op == SWS_OP_FILTER_V) { |
724 | 0 | RET(ff_sws_op_list_split_at(ops, &rest, idx)); |
725 | 0 | if (ff_sws_op_list_is_noop(ops)) { |
726 | | /* Prevent infinite recursion by avoiding splitting in a way |
727 | | * that does not meaningfully reduce the number of operations |
728 | | * performed in the second part. */ |
729 | 0 | FFSWAP(SwsOpList *, ops, rest); |
730 | 0 | break; |
731 | 0 | } |
732 | | /* Serial split: feed first pass into second */ |
733 | 0 | RET(compile_subpass(args, &ops, NULL, input, &tmp)); |
734 | 0 | RET(compile_subpass(args, &rest, link, tmp, output)); |
735 | 0 | return 0; |
736 | 0 | } |
737 | 0 | } |
738 | | |
739 | | /* If we didn't find any more operations to eliminate, then this ops list |
740 | | * is simply unsupported by any of the available backends */ |
741 | 0 | av_log(ctx, AV_LOG_WARNING, "No backend found for operations:\n"); |
742 | 0 | ff_sws_op_list_print(ctx, AV_LOG_WARNING, AV_LOG_TRACE, ops); |
743 | 0 | ret = AVERROR(ENOTSUP); |
744 | |
|
745 | 0 | fail: |
746 | 0 | ff_sws_op_list_free(&ops); |
747 | 0 | ff_sws_op_list_free(&rest); |
748 | 0 | return ret; |
749 | 0 | } |
750 | | |
751 | | int ff_sws_compile_pass(SwsGraph *graph, const SwsOpBackend *backend, |
752 | | SwsOpList **pops, int flags, SwsPass *input, |
753 | | SwsPass **output) |
754 | 0 | { |
755 | 0 | const int passes_orig = graph->num_passes; |
756 | 0 | SwsContext *ctx = graph->ctx; |
757 | 0 | SwsOpList *ops = *pops; |
758 | 0 | int ret = 0; |
759 | |
|
760 | 0 | const SwsOp *write = ff_sws_op_list_output(ops); |
761 | 0 | if (!write) { |
762 | 0 | av_log(ctx, AV_LOG_ERROR, "Last operation must be SWS_OP_WRITE.\n"); |
763 | 0 | ret = AVERROR(EINVAL); |
764 | 0 | goto out; |
765 | 0 | } |
766 | | |
767 | 0 | if (flags & SWS_OP_FLAG_OPTIMIZE) { |
768 | 0 | ret = ff_sws_op_list_optimize(ops); |
769 | 0 | if (ret < 0) |
770 | 0 | goto out; |
771 | 0 | av_log(ctx, AV_LOG_DEBUG, "Operation list after optimizing:\n"); |
772 | 0 | ff_sws_op_list_print(ctx, AV_LOG_DEBUG, AV_LOG_TRACE, ops); |
773 | 0 | } |
774 | | |
775 | | /* Check if the whole operation graph is an end-to-end no-op */ |
776 | 0 | if (ff_sws_op_list_is_noop(ops)) { |
777 | 0 | if (output) |
778 | 0 | *output = input; |
779 | 0 | goto out; |
780 | 0 | } |
781 | | |
782 | 0 | const CompileArgs args = { |
783 | 0 | .backend = backend, |
784 | 0 | .graph = graph, |
785 | 0 | .flags = flags, |
786 | 0 | }; |
787 | |
|
788 | 0 | ret = compile_subpass(&args, &ops, NULL, input, output); |
789 | 0 | if (ret < 0) |
790 | 0 | goto out; |
791 | | |
792 | 0 | const int num_passes = graph->num_passes - passes_orig; |
793 | 0 | if (num_passes > 1) |
794 | 0 | av_log(ctx, AV_LOG_VERBOSE, "Using %d separate passes.\n", num_passes); |
795 | |
|
796 | 0 | out: |
797 | 0 | if (ret < 0) |
798 | 0 | ff_sws_graph_rollback(graph, passes_orig); |
799 | 0 | ff_sws_op_list_free(&ops); |
800 | | *pops = NULL; |
801 | 0 | return ret; |
802 | 0 | } |