FFmpeg
Loading...
Searching...
No Matches
ops_dispatch.c
Go to the documentation of this file.
1/**
2 * Copyright (C) 2025 Niklas Haas
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "libavutil/avassert.h"
22#include "libavutil/cpu.h"
24#include "libavutil/mem.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 do { \
35 if ((ret = (x)) < 0) \
36 goto fail; \
37 } while (0)
38
65
66static int compile_backend(SwsContext *ctx, const SwsOpBackend *backend,
67 const SwsOpList *ops, SwsCompiledOp *out)
68{
70 SwsCompiledOp compiled = {0};
71 int ret = 0;
72
74 if (!copy)
75 return AVERROR(ENOMEM);
76
77 /* Ensure these are always set during compilation */
79
80 ret = backend->compile(ctx, copy, &compiled);
81 if (ret < 0) {
82 int msg_lev = ret == AVERROR(ENOTSUP) ? AV_LOG_TRACE : AV_LOG_ERROR;
83 av_log(ctx, msg_lev, "Backend '%s' failed to compile operations: %s\n",
84 backend->name, av_err2str(ret));
85 goto fail;
86 }
87
88 compiled.backend = backend;
89 *out = compiled;
90
91 av_log(ctx, AV_LOG_VERBOSE, "Compiled using backend '%s': "
92 "block size = %d, over-read = {%d %d %d %d}, over-write = {%d %d %d %d}, "
93 "cpu flags = 0x%x\n", backend->name, out->block_size,
94 out->over_read[0], out->over_read[1],
95 out->over_read[2], out->over_read[3],
96 out->over_write[0], out->over_write[1],
97 out->over_write[2], out->over_write[3],
98 out->cpu_flags);
99
101
102fail:
104 return ret;
105}
106
108 const SwsOpList *ops, SwsCompiledOp *out)
109{
110 if (backend)
111 return compile_backend(ctx, backend, ops, out);
112
113 const SwsBackend enabled = ff_sws_enabled_backends(ctx);
114 for (int n = 0; ff_sws_op_backends[n]; n++) {
115 const SwsOpBackend *backend = ff_sws_op_backends[n];
116 if (ops->src.hw_format != backend->hw_format ||
117 ops->dst.hw_format != backend->hw_format ||
118 !(enabled & backend->flags))
119 continue;
120 if (compile_backend(ctx, backend, ops, out) < 0)
121 continue;
122
123 return 0;
124 }
125
126 return AVERROR(ENOTSUP);
127}
128
130{
131 if (comp->free)
132 comp->free(comp->priv);
133
134 *comp = (SwsCompiledOp) {0};
135}
136
137static void op_pass_free(void *ptr)
138{
139 SwsOpPass *p = ptr;
140 if (!p)
141 return;
142
143 ff_sws_compiled_op_unref(&p->comp);
144 av_refstruct_unref(&p->offsets_y);
145 av_free(p->exec_base.in_bump_y);
146 av_free(p->exec_base.in_offset_x);
147 av_free(p->tail_buf);
148 av_free(p);
149}
150
151static inline void get_row_data(const SwsOpPass *p, const int y_dst,
152 const uint8_t *in[4], uint8_t *out[4])
153{
154 const SwsOpExec *base = &p->exec_base;
155 const int y_src = p->offsets_y ? p->offsets_y[y_dst] : y_dst;
156 for (int i = 0; i < p->planes_in; i++)
157 in[i] = base->in[i] + (y_src >> base->in_sub_y[i]) * base->in_stride[i];
158 for (int i = 0; i < p->planes_out; i++)
159 out[i] = base->out[i] + (y_dst >> base->out_sub_y[i]) * base->out_stride[i];
160}
161
162static inline int get_lines_in(const SwsOpPass *p, const int y, const int h,
163 const int plane)
164{
165 const SwsOpExec *base = &p->exec_base;
166 if (!p->offsets_y)
167 return h >> base->in_sub_y[plane];
168
169 const int y0 = p->offsets_y[y] >> base->in_sub_y[plane];
170 const int y1 = (p->offsets_y[y + h - 1] + p->filter_size_v - 1) >> base->in_sub_y[plane];
171 return y1 - y0 + 1;
172}
173
174static inline size_t pixel_bytes(size_t pixels, int pixel_bits,
175 enum AVRounding rounding)
176{
177 const uint64_t bits = (uint64_t) pixels * pixel_bits;
178 switch (rounding) {
179 case AV_ROUND_ZERO:
180 case AV_ROUND_DOWN:
181 return bits >> 3;
182 case AV_ROUND_INF:
183 case AV_ROUND_UP:
184 return (bits + 7) >> 3;
185 default:
186 av_unreachable("Invalid rounding mode");
187 return (size_t) -1;
188 }
189}
190
191static size_t safe_bytes_pad(int linesize, int plane_pad)
192{
193 av_assert1(linesize);
194 int64_t safe_bytes = FFABS((int64_t) linesize) - plane_pad;
195 return FFMAX(safe_bytes, 0);
196}
197
198static size_t safe_blocks_offset(size_t num_blocks, unsigned block_size,
199 ptrdiff_t safe_offset,
200 const int32_t *offset_bytes)
201{
202 size_t safe_blocks = num_blocks;
203 while (safe_blocks && offset_bytes[safe_blocks * block_size - 1] > safe_offset)
204 safe_blocks--;
205 return safe_blocks;
206}
207
208static int op_pass_setup(const SwsFrame *out, const SwsFrame *in,
209 const SwsPass *pass)
210{
211 const AVPixFmtDescriptor *indesc = av_pix_fmt_desc_get(in->format);
212 const bool float_in = indesc->flags & AV_PIX_FMT_FLAG_FLOAT;
213 const int width = out->width;
214
215 SwsOpPass *p = pass->priv;
216 SwsOpExec *exec = &p->exec_base;
217 const SwsCompiledOp *comp = &p->comp;
218
219 /* Set up main loop parameters */
220 const unsigned block_size = comp->block_size;
221 const size_t num_blocks = (width + block_size - 1) / block_size;
222 const size_t aligned_w = num_blocks * block_size;
223 if (aligned_w < width) /* overflow */
224 return AVERROR(EINVAL);
225 p->num_blocks = num_blocks;
226 p->memcpy_first = false;
227 p->memcpy_last = false;
228 p->memcpy_out = false;
229
230 size_t safe_blocks = num_blocks;
231 for (int i = 0; i < p->planes_in; i++) {
232 const int idx = p->idx_in[i];
233 size_t input_bytes = in->linesize[idx];
234 if (p->filter_size_h && float_in) {
235 /* Floating point inputs may contain NaN / Infinity in the padding */
236 const int plane_w = AV_CEIL_RSHIFT(in->width, exec->in_sub_x[i]);
237 input_bytes = pixel_bytes(plane_w, p->pixel_bits_in, AV_ROUND_UP);
238 }
239
240 size_t safe_bytes = safe_bytes_pad(input_bytes, comp->over_read[i]);
241 size_t safe_blocks_in;
242 if (exec->in_offset_x) {
243 size_t filter_size = pixel_bytes(p->filter_size_h, p->pixel_bits_in,
245 safe_blocks_in = safe_blocks_offset(num_blocks, block_size,
246 safe_bytes - filter_size,
247 exec->in_offset_x);
248 } else {
249 safe_blocks_in = safe_bytes / exec->block_size_in[i];
250 }
251
252 if (safe_blocks_in < num_blocks) {
253 p->memcpy_first |= in->linesize[idx] < 0;
254 p->memcpy_last |= in->linesize[idx] > 0;
255 safe_blocks = FFMIN(safe_blocks, safe_blocks_in);
256 }
257
258 size_t loop_size = num_blocks * exec->block_size_in[i];
259 exec->in[i] = in->data[idx];
260 exec->in_stride[i] = in->linesize[idx];
261 exec->in_bump[i] = in->linesize[idx] - loop_size;
262 }
263
264 for (int i = 0; i < p->planes_out; i++) {
265 const int idx = p->idx_out[i];
266 size_t safe_bytes = safe_bytes_pad(out->linesize[idx], comp->over_write[i]);
267 size_t safe_blocks_out = safe_bytes / exec->block_size_out[i];
268 if (safe_blocks_out < num_blocks) {
269 p->memcpy_out = true;
270 safe_blocks = FFMIN(safe_blocks, safe_blocks_out);
271 }
272
273 size_t loop_size = num_blocks * exec->block_size_out[i];
274 exec->out[i] = out->data[idx];
275 exec->out_stride[i] = out->linesize[idx];
276 exec->out_bump[i] = out->linesize[idx] - loop_size;
277 }
278
279 if (p->palette_idx >= 0) {
280 exec->in[1] = in->data[p->palette_idx];
281 exec->in_stride[1] = exec->in_bump[1] = 0;
282 }
283
284 const bool memcpy_in = p->memcpy_first || p->memcpy_last;
285 if (!memcpy_in && !p->memcpy_out) {
286 av_assert0(safe_blocks == num_blocks);
287 return 0;
288 }
289
290 /* Set-up tail section parameters and buffers */
291 SwsOpExec *tail = &p->exec_tail;
292 const int align = av_cpu_max_align();
293 size_t alloc_size = 0;
294 *tail = *exec;
295
296 const size_t safe_width = safe_blocks * block_size;
297 const size_t tail_size = width - safe_width;
298 p->tail_off_out = pixel_bytes(safe_width, p->pixel_bits_out, AV_ROUND_DOWN);
299 p->tail_size_out = pixel_bytes(tail_size, p->pixel_bits_out, AV_ROUND_UP);
300 p->tail_blocks = num_blocks - safe_blocks;
301
302 if (exec->in_offset_x) {
303 p->tail_off_in = exec->in_offset_x[safe_width];
304 p->tail_size_in = exec->in_offset_x[width - 1] - p->tail_off_in;
305 p->tail_size_in += pixel_bytes(p->filter_size_h, p->pixel_bits_in, AV_ROUND_UP);
306 } else {
307 p->tail_off_in = pixel_bytes(safe_width, p->pixel_bits_in, AV_ROUND_DOWN);
308 p->tail_size_in = pixel_bytes(tail_size, p->pixel_bits_in, AV_ROUND_UP);
309 }
310
311 const size_t alloc_width = aligned_w - safe_width;
312 for (int i = 0; memcpy_in && i < p->planes_in; i++) {
313 size_t needed_size;
314 if (exec->in_offset_x) {
315 /* The input offset map is already padded to multiples of the block
316 * size, and clamps the input offsets to the image boundaries; so
317 * we just need to compensate for the comp->over_read */
318 needed_size = p->tail_size_in;
319 } else {
320 needed_size = pixel_bytes(alloc_width, p->pixel_bits_in, AV_ROUND_UP);
321 }
322 size_t loop_size = p->tail_blocks * exec->block_size_in[i];
323 tail->in_stride[i] = FFALIGN(needed_size + comp->over_read[i], align);
324 tail->in_bump[i] = tail->in_stride[i] - loop_size;
325 alloc_size += tail->in_stride[i] * in->height;
326 }
327
328 for (int i = 0; p->memcpy_out && i < p->planes_out; i++) {
329 size_t needed_size = pixel_bytes(alloc_width, p->pixel_bits_out, AV_ROUND_UP);
330 size_t loop_size = p->tail_blocks * exec->block_size_out[i];
331 tail->out_stride[i] = FFALIGN(needed_size + comp->over_write[i], align);
332 tail->out_bump[i] = tail->out_stride[i] - loop_size;
333 alloc_size += tail->out_stride[i] * out->height;
334 }
335
336 if (memcpy_in && exec->in_offset_x) {
337 /* `in_offset_x` is indexed relative to the line start, not the start
338 * of the section being processed; so we need to over-allocate this
339 * array to the full width of the image, even though we will only
340 * partially fill in the offsets relevant to the tail region */
341 alloc_size += aligned_w * sizeof(*exec->in_offset_x);
342 }
343
344 av_fast_mallocz(&p->tail_buf, &p->tail_buf_size, alloc_size);
345 if (!p->tail_buf)
346 return AVERROR(ENOMEM);
347
348 uint8_t *tail_buf = p->tail_buf;
349 for (int i = 0; memcpy_in && i < p->planes_in; i++) {
350 tail->in[i] = tail_buf;
351 tail_buf += tail->in_stride[i] * in->height;
352 }
353
354 for (int i = 0; p->memcpy_out && i < p->planes_out; i++) {
355 tail->out[i] = tail_buf;
356 tail_buf += tail->out_stride[i] * out->height;
357 }
358
359 if (memcpy_in && exec->in_offset_x) {
360 tail->in_offset_x = (int32_t *) tail_buf;
361 for (int i = safe_width; i < aligned_w; i++)
362 tail->in_offset_x[i] = exec->in_offset_x[i] - p->tail_off_in;
363 }
364
365 return 0;
366}
367
368static void copy_lines(uint8_t *dst, const ptrdiff_t dst_stride,
369 const uint8_t *src, const ptrdiff_t src_stride,
370 const int h, const size_t bytes)
371{
372 for (int y = 0; y < h; y++) {
373 memcpy(dst, src, bytes);
374 dst += dst_stride;
375 src += src_stride;
376 }
377}
378
379static void op_pass_run(const SwsFrame *out, const SwsFrame *in, const int y,
380 const int h, const SwsPass *pass)
381{
382 const SwsOpPass *p = pass->priv;
383 const SwsCompiledOp *comp = &p->comp;
384
385 /* Fill exec metadata for this slice */
386 DECLARE_ALIGNED_32(SwsOpExec, exec) = p->exec_base;
387 exec.slice_y = y;
388 exec.slice_h = h;
389
390 /**
391 * To ensure safety, we need to consider the following:
392 *
393 * 1. We can overread the input, unless this is the last line of an
394 * unpadded buffer. All defined operations can handle arbitrary pixel
395 * input, so overread of arbitrary data is fine. For flipped images,
396 * this condition is actually *inverted* to where the first line is
397 * the one at the end of the buffer.
398 *
399 * 2. We can overwrite the output, as long as we don't write more than the
400 * amount of pixels that fit into one linesize. So we always need to
401 * memcpy the last column on the output side if unpadded.
402 */
403
404 const int y_in_first = p->offsets_y ? p->offsets_y[y] : y;
405 const int y_in_last = p->offsets_y ? p->offsets_y[y + h - 1] + p->filter_size_v - 1
406 : y + h - 1;
407 const bool memcpy_in = p->memcpy_last && y_in_last == in->height - 1 ||
408 p->memcpy_first && y_in_first == 0;
409 const bool memcpy_out = p->memcpy_out;
410 const size_t num_blocks = p->num_blocks;
411 const size_t tail_blocks = p->tail_blocks;
412
413 get_row_data(p, y, exec.in, exec.out);
414 if (!memcpy_in && !memcpy_out) {
415 /* Fast path (fully aligned/padded inputs and outputs) */
416 comp->func(&exec, comp->priv, 0, y, num_blocks, y + h);
417 return;
418 }
419
420 /* Non-aligned case (slow path); process main blocks as normal, and
421 * a separate tail (via memcpy into an appropriately padded buffer) */
422 if (num_blocks > tail_blocks) {
423 for (int i = 0; i < 4; i++) {
424 /* We process fewer blocks, so the in_bump needs to be increased
425 * to reflect that the plane pointers are left on the last block,
426 * not the end of the processed line, after each loop iteration */
427 exec.in_bump[i] += exec.block_size_in[i] * tail_blocks;
428 exec.out_bump[i] += exec.block_size_out[i] * tail_blocks;
429 }
430
431 comp->func(&exec, comp->priv, 0, y, num_blocks - tail_blocks, y + h);
432 }
433
434 DECLARE_ALIGNED_32(SwsOpExec, tail) = p->exec_tail;
435 tail.slice_y = y;
436 tail.slice_h = h;
437
438 for (int i = 0; i < p->planes_in; i++) {
439 /* Input offsets are relative to the base pointer */
440 if (!exec.in_offset_x || memcpy_in)
441 exec.in[i] += p->tail_off_in;
442 tail.in[i] += (y_in_first >> exec.in_sub_y[i]) * tail.in_stride[i];
443 }
444 for (int i = 0; i < p->planes_out; i++) {
445 exec.out[i] += p->tail_off_out;
446 tail.out[i] += y * tail.out_stride[i];
447 }
448
449 for (int i = 0; i < p->planes_in; i++) {
450 if (memcpy_in) {
451 const int lines = get_lines_in(p, y, h, i);
452 copy_lines((uint8_t *) tail.in[i], tail.in_stride[i],
453 exec.in[i], exec.in_stride[i], lines, p->tail_size_in);
454 } else {
455 /* Reuse input pointers directly */
456 const size_t loop_size = tail_blocks * exec.block_size_in[i];
457 tail.in[i] = exec.in[i];
458 tail.in_stride[i] = exec.in_stride[i];
459 tail.in_bump[i] = exec.in_stride[i] - loop_size;
460 }
461 }
462
463 for (int i = 0; !memcpy_out && i < p->planes_out; i++) {
464 /* Reuse output pointers directly */
465 const size_t loop_size = tail_blocks * exec.block_size_out[i];
466 tail.out[i] = exec.out[i];
467 tail.out_stride[i] = exec.out_stride[i];
468 tail.out_bump[i] = exec.out_stride[i] - loop_size;
469 }
470
471 /* Dispatch kernel over tail */
472 av_assert1(tail_blocks > 0);
473 comp->func(&tail, comp->priv, num_blocks - tail_blocks, y, num_blocks, y + h);
474
475 for (int i = 0; memcpy_out && i < p->planes_out; i++) {
476 const int lines = h >> tail.out_sub_y[i];
477 copy_lines(exec.out[i], exec.out_stride[i],
478 tail.out[i], tail.out_stride[i], lines, p->tail_size_out);
479 }
480}
481
482/* Updates the plane copy (no-op) map for this operation list */
483static void op_list_get_plane_copy(const SwsOpList *ops, SwsPass *pass)
484{
485 const SwsOp *write = ff_sws_op_list_output(ops);
486 const SwsOp *read = ff_sws_op_list_input(ops);
487 if (!write || write->rw.mode != SWS_RW_PLANAR ||
488 !read || read->rw.mode != SWS_RW_PLANAR ||
489 read->type != write->type ||
490 read->rw.frac != write->rw.frac)
491 return; /* only regular planes can be directly ref'd */
492
493 const SwsOp *prev = &ops->ops[ops->num_ops - 2];
494 int *plane_copy = pass->output->plane_copy;
495 for (int i = 0; i < write->rw.elems; i++) {
496 SwsCompFlags flags = prev->comps.flags[i];
497 if (!(flags & SWS_COMP_COPY))
498 continue;
499
500 const int out_idx = ops->plane_dst[i];
501 switch (prev->comps.dep_in[i]) {
502 case SWS_COMP(0): plane_copy[out_idx] = ops->plane_src[0]; break;
503 case SWS_COMP(1): plane_copy[out_idx] = ops->plane_src[1]; break;
504 case SWS_COMP(2): plane_copy[out_idx] = ops->plane_src[2]; break;
505 case SWS_COMP(3): plane_copy[out_idx] = ops->plane_src[3]; break;
506 }
507 }
508}
509
510static int rw_data_planes(const SwsOp *op)
511{
512 /* Exclude the palette plane from the plane count, since it does not need
513 * to be directly processed/adjusted by the dispatch layer */
514 return op->rw.mode == SWS_RW_PALETTE ? 1 : ff_sws_rw_op_planes(op);
515}
516
517static int rw_pixel_bits(const SwsOp *op)
518{
519 if (op->rw.mode == SWS_RW_PALETTE)
520 return 8; /* index size */
521
522 int elems = 0;
523 switch (op->rw.mode) {
524 case SWS_RW_PLANAR: elems = 1; break;
525 case SWS_RW_PACKED: elems = op->rw.elems; break;
526 }
527
528 const int size = ff_sws_pixel_type_size(op->type);
529 const int bits = 8 >> op->rw.frac;
530 av_assert1(bits >= 1);
531 return elems * size * bits;
532}
533
534static void align_pass(SwsPass *pass, int block_size, const int *over_rw,
535 int pixel_bits)
536{
537 if (!pass || pixel_bits <= 0)
538 return;
539
540 /* Add at least as many pixels as needed to cover the padding requirement */
541 int pad_max = 0;
542 for (int i = 0; i < 4; i++) {
543 const int pad = (over_rw[i] * 8 + pixel_bits - 1) / pixel_bits;
544 pad_max = FFMAX(pad_max, pad);
545 }
546
547 SwsPassBuffer *buf = pass->output;
548 buf->width_align = FFMAX(buf->width_align, block_size);
549 buf->width_pad = FFMAX(buf->width_pad, pad_max);
550}
551
552/* Unchanging part of parameter list */
558
559static int compile_single(const CompileArgs *args, const SwsOpList *ops,
560 SwsPass *link, SwsPass *input, SwsPass **output)
561{
562 SwsGraph *graph = args->graph;
563 SwsContext *ctx = graph->ctx;
564 SwsOpPass *p = av_mallocz(sizeof(*p));
565 if (!p)
566 return AVERROR(ENOMEM);
567
568 int ret = ff_sws_ops_compile(ctx, args->backend, ops, &p->comp);
569 if (ret < 0)
570 goto fail;
571 else if (args->flags & SWS_OP_FLAG_DRY_RUN)
572 goto fail; /* nothing to do, just return */
573
574 const SwsCompiledOp *comp = &p->comp;
575 const SwsFormat *src = &ops->src;
576 const SwsFormat *dst = &ops->dst;
577 av_assert0(!link || link->format == dst->format);
578 if (p->comp.opaque) {
580 av_free(p);
581 ret = ff_sws_graph_add_pass(graph, dst->format, dst->width, dst->height,
582 input, 0, c.slice_align, c.func_opaque,
583 NULL, c.priv, c.free, output);
584 if (ret >= 0) {
585 (*output)->backend = c.backend->flags;
586 op_list_get_plane_copy(ops, *output);
587 ff_sws_pass_link_output(*output, link);
588 }
589 return ret;
590 }
591
592 const AVPixFmtDescriptor *indesc = av_pix_fmt_desc_get(src->format);
593 const AVPixFmtDescriptor *outdesc = av_pix_fmt_desc_get(dst->format);
594 const SwsOp *write = ff_sws_op_list_output(ops);
595 p->planes_out = rw_data_planes(write);
596 p->pixel_bits_out = rw_pixel_bits(write);
597 p->palette_idx = -1;
598 p->exec_base = (SwsOpExec) {
599 .width = dst->width,
600 .height = dst->height,
601 };
602
603 const SwsOp *read = ff_sws_op_list_input(ops);
604 if (read) {
605 p->planes_in = rw_data_planes(read);
606 p->pixel_bits_in = rw_pixel_bits(read);
607 if (read->rw.mode == SWS_RW_PALETTE)
608 p->palette_idx = ops->plane_src[1];
609 }
610
611 const int64_t block_bits_in = (int64_t) comp->block_size * p->pixel_bits_in;
612 const int64_t block_bits_out = (int64_t) comp->block_size * p->pixel_bits_out;
613 if (block_bits_in & 0x7 || block_bits_out & 0x7) {
614 av_log(ctx, AV_LOG_ERROR, "Block size must be byte-aligned.\n");
615 ret = AVERROR(EINVAL);
616 goto fail;
617 }
618
619 for (int i = 0; i < 4; i++)
620 p->idx_in[i] = p->idx_out[i] = -1;
621
622 for (int i = 0; i < p->planes_in; i++) {
623 const int idx = ops->plane_src[i];
624 const int chroma = idx == 1 || idx == 2;
625 const int sub_x = chroma ? indesc->log2_chroma_w : 0;
626 const int sub_y = chroma ? indesc->log2_chroma_h : 0;
627 p->exec_base.in_sub_x[i] = sub_x;
628 p->exec_base.in_sub_y[i] = sub_y;
629 p->exec_base.block_size_in[i] = block_bits_in >> 3;
630 p->idx_in[i] = idx;
631 }
632
633 for (int i = 0; i < p->planes_out; i++) {
634 const int idx = ops->plane_dst[i];
635 const int chroma = idx == 1 || idx == 2;
636 const int sub_x = chroma ? outdesc->log2_chroma_w : 0;
637 const int sub_y = chroma ? outdesc->log2_chroma_h : 0;
638 p->exec_base.out_sub_x[i] = sub_x;
639 p->exec_base.out_sub_y[i] = sub_y;
640 p->exec_base.block_size_out[i] = block_bits_out >> 3;
641 p->idx_out[i] = idx;
642 }
643
644 const SwsFilterWeights *filter = read ? read->rw.filter.kernel : NULL;
645 if (read && read->rw.filter.op == SWS_OP_FILTER_V) {
646 p->offsets_y = av_refstruct_ref(filter->offsets);
647 p->filter_size_v = filter->filter_size;
648
649 /* Compute relative pointer bumps for each output line */
650 int32_t *bump = av_malloc_array(filter->dst_size, sizeof(*bump));
651 if (!bump) {
652 ret = AVERROR(ENOMEM);
653 goto fail;
654 }
655
656 int line = filter->offsets[0];
657 for (int y = 0; y < filter->dst_size - 1; y++) {
658 int next = filter->offsets[y + 1];
659 bump[y] = next - line - 1;
660 line = next;
661 }
662 bump[filter->dst_size - 1] = 0;
663 p->exec_base.in_bump_y = bump;
664 } else if (read && read->rw.filter.op == SWS_OP_FILTER_H) {
665 /* Compute pixel offset map for each output line */
666 const int pixels = FFALIGN(filter->dst_size, p->comp.block_size);
667 int32_t *offset = av_malloc_array(pixels, sizeof(*offset));
668 if (!offset) {
669 ret = AVERROR(ENOMEM);
670 goto fail;
671 }
672 p->exec_base.in_offset_x = offset;
673
674 for (int x = 0; x < filter->dst_size; x++) {
675 /* Sanity check; if the tap would land on a half-pixel, we cannot
676 * reasonably expect the implementation to know about this. Just
677 * error out in such (theoretical) cases. */
678 int64_t bits = (int64_t) filter->offsets[x] * p->pixel_bits_in;
679 if ((bits & 0x7) || (bits >> 3) > INT32_MAX) {
680 ret = AVERROR(EINVAL);
681 goto fail;
682 }
683 offset[x] = bits >> 3;
684 }
685 for (int x = filter->dst_size; x < pixels; x++)
686 offset[x] = offset[filter->dst_size - 1];
687 for (int i = 0; i < 4; i++)
688 p->exec_base.block_size_in[i] = 0; /* ptr does not advance */
689 p->filter_size_h = filter->filter_size;
690 }
691
692 ret = ff_sws_graph_add_pass(graph, dst->format, dst->width, dst->height,
693 input, 0, comp->slice_align, op_pass_run,
694 op_pass_setup, p, op_pass_free, output);
695 if (ret < 0)
696 return ret;
697
698 (*output)->backend = comp->backend->flags;
699 op_list_get_plane_copy(ops, *output);
700 ff_sws_pass_link_output(*output, link);
701 align_pass(*output, comp->block_size, comp->over_write, p->pixel_bits_out);
702 if (read)
703 align_pass(input, comp->block_size, comp->over_read, p->pixel_bits_in);
704 return 0;
705
706fail:
707 op_pass_free(p);
708 return ret;
709}
710
711/* Return a mask of all planes matching any flag in `flags` */
713{
715 for (int c = 0; c < 4; c++) {
716 if (op->comps.flags[c] & flags)
717 planes |= SWS_COMP(c);
718 }
719
720 return planes;
721}
722
723/* Takes over ownership of *pops, even on failure */
724static int compile_subpass(const CompileArgs *args, SwsOpList **pops,
725 SwsPass *link, SwsPass *input, SwsPass **output)
726{
727 int ret;
728 SwsContext *ctx = args->graph->ctx;
729 SwsOpList *ops = *pops;
730 SwsOpList *rest = NULL;
731 SwsPass *tmp = NULL;
732 *pops = NULL;
733
734 if (args->flags & SWS_OP_FLAG_SPLIT_MEMCPY) {
735 /* Split off copied and constant planes into a separate subpass,
736 * since these are likely to be handled by the memcpy backend */
737 av_assert0(ops->num_ops >= 2);
738 const SwsOp *prev = &ops->ops[ops->num_ops - 2];
741 if (rest) {
742 /* Parallel split: share input and link all outputs together */
743 av_log(ctx, AV_LOG_DEBUG, "Splitting const/memcpy planes: %s\n",
745 RET(compile_subpass(args, &ops, link, input, &tmp));
746 RET(compile_subpass(args, &rest, tmp, input, output));
747 return 0;
748 }
749 }
750
751 ret = compile_single(args, ops, link, input, output);
752 if (ret != AVERROR(ENOTSUP))
753 goto fail; /* either success or a hard error */
754
755 /* Find any unresolved filter */
756 for (int idx = 1; idx < ops->num_ops - 1; idx++) {
757 const SwsOp *op = &ops->ops[idx];
758 if (op->op == SWS_OP_FILTER_H || op->op == SWS_OP_FILTER_V) {
759 RET(ff_sws_op_list_split_at(ops, &rest, idx));
760 if (ff_sws_op_list_is_noop(ops)) {
761 /* Prevent infinite recursion by avoiding splitting in a way
762 * that does not meaningfully reduce the number of operations
763 * performed in the second part. */
764 FFSWAP(SwsOpList *, ops, rest);
765 break;
766 }
767 /* Serial split: feed first pass into second */
768 RET(compile_subpass(args, &ops, NULL, input, &tmp));
769 RET(compile_subpass(args, &rest, link, tmp, output));
770 return 0;
771 }
772 }
773
774 /* If we didn't find any more operations to eliminate, then this ops list
775 * is simply unsupported by any of the available backends */
776 av_log(ctx, AV_LOG_WARNING, "No backend found for operations:\n");
778 ret = AVERROR(ENOTSUP);
779
780fail:
782 ff_sws_op_list_free(&rest);
783 return ret;
784}
785
786int ff_sws_compile_pass(SwsGraph *graph, const SwsOpBackend *backend,
787 SwsOpList **pops, int flags, SwsPass *input,
788 SwsPass **output)
789{
790 const int passes_orig = graph->num_passes;
791 SwsContext *ctx = graph->ctx;
792 SwsOpList *ops = *pops;
793 int ret = 0;
794
795 const SwsOp *write = ff_sws_op_list_output(ops);
796 if (!write) {
797 av_log(ctx, AV_LOG_ERROR, "Last operation must be SWS_OP_WRITE.\n");
798 ret = AVERROR(EINVAL);
799 goto out;
800 }
801
803 ret = ff_sws_op_list_optimize(ops);
804 if (ret < 0)
805 goto out;
806 av_log(ctx, AV_LOG_DEBUG, "Operation list after optimizing:\n");
808 }
809
810 /* Check if the whole operation graph is an end-to-end no-op */
811 if (ff_sws_op_list_is_noop(ops)) {
812 if (output)
813 *output = input;
814 goto out;
815 }
816
817 const CompileArgs args = {
818 .backend = backend,
819 .graph = graph,
820 .flags = flags,
821 };
822
823 ret = compile_subpass(&args, &ops, NULL, input, output);
824 if (ret < 0)
825 goto out;
826
827 const int num_passes = graph->num_passes - passes_orig;
828 if (num_passes > 1)
829 av_log(ctx, AV_LOG_VERBOSE, "Using %d separate passes.\n", num_passes);
830
831out:
832 if (ret < 0)
833 ff_sws_graph_rollback(graph, passes_orig);
835 *pops = NULL;
836 return ret;
837}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static FILE * out
static AVFormatContext * ctx
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition avassert.h:109
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
static const uint8_t *BS_FUNC align(BSCTX *bc)
Skip bits to a byte boundary.
static uint32_t BS_FUNC read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition eamad.c:79
static const uint8_t bits[8]
Definition fastaudio.c:100
int ff_sws_graph_add_pass(SwsGraph *graph, enum AVPixelFormat fmt, int width, int height, SwsPass *input, int lines, int align, SwsPassFunc run, SwsPassSetup setup, void *priv, void(*free_cb)(void *priv), SwsPass **out_pass)
Allocate and add a new pass to the filter graph.
Definition graph.c:191
void ff_sws_pass_link_output(SwsPass *dst, const SwsPass *src)
Link the output buffers to a different pass, rather than allocating new image buffers.
Definition graph.c:249
void ff_sws_graph_rollback(SwsGraph *graph, int since_idx)
Remove all passes added since the given index.
Definition graph.c:935
#define fail
Definition test.h:479
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
AVRounding
Rounding methods.
@ AV_ROUND_INF
Round away from zero.
@ AV_ROUND_ZERO
Round toward zero.
@ AV_ROUND_DOWN
Round toward -infinity.
@ AV_ROUND_UP
Round toward +infinity.
void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size)
Allocate and clear a buffer, reusing the given one if large enough.
Definition mem.c:560
SwsBackend
Definition swscale.h:110
unsigned offset
Definition libaomenc.c:763
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition anm.c:76
size_t av_cpu_max_align(void)
Get the maximum data alignment that may be required by FFmpeg.
Definition cpu.c:287
static const struct @257111027162314367033347246032313251342043035002 planes[]
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
Memory handling functions.
#define DECLARE_ALIGNED_32(t, v)
void ff_sws_op_list_update_comps(SwsOpList *ops)
Infer + propagate known information about components.
Definition ops.c:315
const SwsOp * ff_sws_op_list_input(const SwsOpList *ops)
Returns the input operation for a given op list, or NULL if there is none (e.g.
Definition ops.c:713
void ff_sws_op_list_free(SwsOpList **p_ops)
Definition ops.c:659
const SwsOpBackend *const ff_sws_op_backends[]
Definition ops.c:42
int ff_sws_rw_op_planes(const SwsOp *op)
Return the number of planes involved in a read/write operation.
Definition ops.c:133
const SwsOp * ff_sws_op_list_output(const SwsOpList *ops)
Returns the output operation for a given op list, or NULL if there is none.
Definition ops.c:722
bool ff_sws_op_list_is_noop(const SwsOpList *ops)
Returns whether an op list represents a true no-op operation, i.e.
Definition ops.c:761
void ff_sws_op_list_print(void *log, int lev, int lev_extra, const SwsOpList *ops)
Print out the contents of an operation list.
Definition ops.c:987
SwsOpList * ff_sws_op_list_duplicate(const SwsOpList *ops)
Returns a duplicate of ops, or NULL on OOM.
Definition ops.c:673
int ff_sws_op_list_optimize(SwsOpList *ops)
Fuse compatible and eliminate redundant operations, as well as replacing some operations with more ef...
@ SWS_OP_FILTER_V
Definition ops.h:64
@ SWS_OP_FILTER_H
Definition ops.h:63
@ SWS_RW_PALETTE
Definition ops.h:109
@ SWS_RW_PLANAR
Note: 1-component reads are either SWS_RW_PLANAR or SWS_RW_PACKED, depending on the underlying interp...
Definition ops.h:107
@ SWS_RW_PACKED
Definition ops.h:108
SwsCompFlags
Definition ops.h:77
@ SWS_COMP_CONST
Definition ops.h:83
@ SWS_COMP_COPY
Definition ops.h:82
static void copy_lines(uint8_t *dst, const ptrdiff_t dst_stride, const uint8_t *src, const ptrdiff_t src_stride, const int h, const size_t bytes)
static size_t safe_blocks_offset(size_t num_blocks, unsigned block_size, ptrdiff_t safe_offset, const int32_t *offset_bytes)
static int rw_data_planes(const SwsOp *op)
static int get_lines_in(const SwsOpPass *p, const int y, const int h, const int plane)
static int rw_pixel_bits(const SwsOp *op)
static size_t pixel_bytes(size_t pixels, int pixel_bits, enum AVRounding rounding)
static int compile_single(const CompileArgs *args, const SwsOpList *ops, SwsPass *link, SwsPass *input, SwsPass **output)
void ff_sws_compiled_op_unref(SwsCompiledOp *comp)
static void op_pass_free(void *ptr)
static void op_list_get_plane_copy(const SwsOpList *ops, SwsPass *pass)
#define RET(x)
Copyright (C) 2025 Niklas Haas.
static void op_pass_run(const SwsFrame *out, const SwsFrame *in, const int y, const int h, const SwsPass *pass)
static int compile_backend(SwsContext *ctx, const SwsOpBackend *backend, const SwsOpList *ops, SwsCompiledOp *out)
static int op_pass_setup(const SwsFrame *out, const SwsFrame *in, const SwsPass *pass)
static int compile_subpass(const CompileArgs *args, SwsOpList **pops, SwsPass *link, SwsPass *input, SwsPass **output)
static void get_row_data(const SwsOpPass *p, const int y_dst, const uint8_t *in[4], uint8_t *out[4])
static SwsCompMask plane_mask_flags(const SwsOp *op, SwsCompFlags flags)
int ff_sws_compile_pass(SwsGraph *graph, const SwsOpBackend *backend, SwsOpList **pops, int flags, SwsPass *input, SwsPass **output)
Resolves an operation list to a graph pass.
static void align_pass(SwsPass *pass, int block_size, const int *over_rw, int pixel_bits)
int ff_sws_ops_compile(SwsContext *ctx, const SwsOpBackend *backend, const SwsOpList *ops, SwsCompiledOp *out)
Attempt to compile a list of operations using a specific backend, or the best available backend if ba...
static size_t safe_bytes_pad(int linesize, int plane_pad)
@ SWS_OP_FLAG_DRY_RUN
@ SWS_OP_FLAG_OPTIMIZE
@ SWS_OP_FLAG_SPLIT_MEMCPY
int ff_sws_op_list_split_at(SwsOpList *ops1, SwsOpList **ops2, int index)
Split an op list into two at the given index.
int ff_sws_op_list_split_planes(SwsOpList *ops1, SwsOpList **ops2, SwsCompMask planes)
Reduce an op list into a reduced subset that operates only on a given subset of planes.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_FLOAT
The pixel format contains IEEE-754 floating point values.
Definition pixdesc.h:158
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:120
void * av_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
Definition refstruct.c:140
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition pixdesc.h:80
uint64_t flags
Combination of AV_PIX_FMT_FLAG_... flags.
Definition pixdesc.h:94
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition pixdesc.h:89
SwsGraph * graph
const SwsOpBackend * backend
const struct SwsOpBackend * backend
SwsCompMask dep_in[4]
Definition ops.h:94
SwsCompFlags flags[4]
Definition ops.h:87
Main external API structure.
Definition swscale.h:227
Represents a computed filter kernel.
Definition filters.h:85
enum AVPixelFormat hw_format
Definition format.h:82
Represents a view into a single field of frame data.
Definition format.h:236
uint8_t * data[4]
Definition format.h:238
int height
Definition format.h:244
int linesize[4]
Definition format.h:239
enum AVPixelFormat format
Definition format.h:245
int width
Dimensions and format.
Definition format.h:244
Filter graph, which represents a 'baked' pixel format conversion.
Definition graph.h:132
int num_passes
Definition graph.h:154
SwsContext * ctx
Definition graph.h:133
const char * name
int(* compile)(SwsContext *ctx, const SwsOpList *ops, SwsCompiledOp *out)
Compile an operation list to an implementation chain.
SwsBackend flags
enum AVPixelFormat hw_format
If NONE, backend only supports software frames.
Copyright (C) 2026 Niklas Haas.
int32_t block_size_out[4]
ptrdiff_t out_stride[4]
uint8_t * out[4]
int32_t * in_offset_x
Pixel offset map; for horizontal scaling, in bytes.
const uint8_t * in[4]
ptrdiff_t in_stride[4]
int32_t block_size_in[4]
uint8_t in_sub_x[4]
ptrdiff_t in_bump[4]
Pointer bump, difference between stride and processed line size.
ptrdiff_t out_bump[4]
Helper struct for representing a list of operations.
Definition ops.h:293
SwsFormat dst
Definition ops.h:298
uint8_t plane_src[4]
Definition ops.h:301
uint8_t plane_dst[4]
Definition ops.h:301
SwsOp * ops
Definition ops.h:294
int num_ops
Definition ops.h:295
SwsFormat src
Definition ops.h:298
bool memcpy_first
int tail_size_in
int filter_size_h
int * offsets_y
int tail_off_in
int tail_size_out
size_t tail_blocks
uint8_t * tail_buf
int pixel_bits_out
int pixel_bits_in
size_t num_blocks
bool memcpy_last
int idx_in[4]
int filter_size_v
SwsCompiledOp comp
int tail_off_out
int idx_out[4]
int palette_idx
bool memcpy_out
SwsOpExec exec_base
unsigned int tail_buf_size
SwsOpExec exec_tail
Definition ops.h:237
SwsComps comps
Metadata about the operation's input/output components.
Definition ops.h:262
SwsPixelType type
Definition ops.h:239
SwsReadWriteOp rw
Definition ops.h:242
Represents an output buffer for a filter pass.
Definition graph.h:60
int width_pad
Definition graph.h:68
int width_align
Definition graph.h:67
int plane_copy[4]
Map of planes which are directly copied from the pass input.
Definition graph.h:77
Represents a single filter pass in the scaling graph.
Definition graph.h:85
void * priv
Definition graph.h:121
enum AVPixelFormat format
Definition graph.h:95
SwsPassBuffer * output
Filter output buffer.
Definition graph.h:109
SwsReadWriteMode mode
Examples: rgba = 4x u8 packed yuv444p = 3x u8 rgb565 = 1x u16 <- use SWS_OP_UNPACK to unpack monow = ...
Definition ops.h:122
uint8_t frac
Definition ops.h:124
uint8_t elems
Definition ops.h:123
SwsBackend ff_sws_enabled_backends(const SwsContext *ctx)
Definition utils.c:59
#define av_free(p)
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
#define src
Definition vp8dsp.c:248
#define width
Definition dsp.h:89
int size
#define SWS_COMP(X)
Definition uops.h:97
#define ff_sws_comp_mask_str(mask)
Definition uops.h:110
uint8_t SwsCompMask
Bit-mask of components.
Definition uops.h:93
static av_const int ff_sws_pixel_type_size(SwsPixelType type)
Definition uops.h:50
static void copy(const float *p1, float *p2, const int length)
static av_always_inline void chroma(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, int column, int mirror, int jobnr, int nb_jobs)
uint8_t base
Definition vp3data.h:128
static double c[64]