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