FFmpeg
ops_dispatch.h
Go to the documentation of this file.
1 /**
2  * Copyright (C) 2026 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 #ifndef SWSCALE_OPS_DISPATCH_H
22 #define SWSCALE_OPS_DISPATCH_H
23 
24 #include <assert.h>
25 
26 #include "libavutil/frame.h"
27 #include "graph.h"
28 #include "uops.h"
29 
30 /**
31  * Global execution context for all compiled functions.
32  *
33  * Note: This struct is hard-coded in assembly, so do not change the layout
34  * without updating the corresponding assembly definitions.
35  */
36 typedef struct SwsOpExec {
37  /* The data pointers point to the first pixel to process */
38  const uint8_t *in[4];
39  uint8_t *out[4];
40 
41  /* Separation between lines in bytes */
42  ptrdiff_t in_stride[4];
43  ptrdiff_t out_stride[4];
44 
45  /**
46  * Pointer bump, difference between stride and processed line size.
47  *
48  * Assumes that each read kernel increments pointers by the processed
49  * block size, except when using horizontal filtering, in which case
50  * this is always equal to the input stride.
51  */
52  ptrdiff_t in_bump[4];
53  ptrdiff_t out_bump[4];
54 
55  /* Extra metadata, may or may not be useful */
56  int32_t width, height; /* Overall output image dimensions */
57  int32_t slice_y, slice_h; /* Start and height of current slice */
58  int32_t block_size_in[4]; /* Size of a block of pixels in bytes */
60 
61  /* Subsampling factors for each plane */
62  uint8_t in_sub_y[4], out_sub_y[4];
63  uint8_t in_sub_x[4], out_sub_x[4];
64 
65  /**
66  * Line bump; determines how many additional lines to advance (after
67  * incrementing normally to the next line), for each filtered output line.
68  *
69  * Indexed by the line's true y coordinate. If NULL, then the bumps are
70  * effectively all zero. Note that these bumps still need to be
71  * multiplied by the corresponding line stride.
72  */
74 
75  /**
76  * Pixel offset map; for horizontal scaling, in bytes. Indexed by the x
77  * coordinate of the output pixel. This is always aligned up to a multiple
78  * of the block size, so implementations may safely over-read up to the
79  * next block boundary.
80  */
82 } SwsOpExec;
83 
84 static_assert(sizeof(SwsOpExec) == 24 * sizeof(void *) +
85  12 * sizeof(int32_t) +
86  16 * sizeof(uint8_t) +
87  2 * sizeof(void *),
88  "SwsOpExec layout mismatch");
89 
90 /**
91  * Process a given range of pixel blocks.
92  *
93  * Note: `bx_start` and `bx_end` are in units of `SwsCompiledOp.block_size`.
94  */
95 typedef void (*SwsOpFunc)(const SwsOpExec *exec, const void *priv,
96  int bx_start, int y_start, int bx_end, int y_end);
97 
98 #define SWS_DECL_FUNC(NAME) \
99  void NAME(const SwsOpExec *, const void *, int, int, int, int)
100 
101 typedef struct SwsCompiledOp {
102  /* Function to execute */
103  union {
106  };
107 
108  /**
109  * If `opaque` is true, then `func_opaque`, `priv` and `free` are directly
110  * forwarded as `SwsPass.run`, `SwsPass.priv` and `SwsPass.free`
111  * respectively.
112  */
113  bool opaque;
114 
115  /* Set by ff_sws_ops_compile(), informative */
116  const struct SwsOpBackend *backend;
117 
118  /* Execution parameters for all functions */
119  int slice_align; /* slice height alignment */
120  int cpu_flags; /* active set of CPU flags (informative) */
121 
122  /* Execution parameters for non-opaque functions only */
123  int block_size; /* number of pixels processed per iteration */
124  int over_read[4]; /* implementation over-reads input by this many bytes */
125  int over_write[4]; /* implementation over-writes output by this many bytes */
126 
127  /* Arbitrary private data */
128  void *priv;
129  void (*free)(void *priv);
130 } SwsCompiledOp;
131 
133 
134 typedef struct SwsOpBackend {
135  const char *name; /* Descriptive name for this backend */
136  SwsBackend flags; /* Set of SWS_BACKEND_* */
137 
138  /**
139  * Compile an operation list to an implementation chain.
140  *
141  * Returns 0 or a negative error code.
142  */
144 
145  /**
146  * Alternative to `compile` that takes a list of micro-ops directly.
147  */
149 
150  /**
151  * If NONE, backend only supports software frames.
152  * Otherwise, frame hardware format must match hw_format for the backend
153  * to be used.
154  */
156 } SwsOpBackend;
157 
158 /* List of all backends, terminated by NULL */
159 extern const SwsOpBackend *const ff_sws_op_backends[];
160 
161 /**
162  * Attempt to compile a list of operations using a specific backend, or
163  * the best available backend if `backend` is NULL.
164  *
165  * Returns 0 on success, or a negative error code on failure.
166  */
167 int ff_sws_ops_compile(SwsContext *ctx, const SwsOpBackend *backend,
168  const SwsOpList *ops, SwsCompiledOp *out);
169 
171  /* Automatically optimize the operations when compiling */
173 
174  /* Discard the compiled op lists instead of generating passes */
176 
177  /* Split off copied/cleared planes into separate subpasses */
179 };
180 
181 /**
182  * Resolves an operation list to a graph pass. The last op must be a write.
183  *
184  * @param backend Force the use of a specific backend (Optional)
185  * @param ops Operations to compile. Ownership passes to this function, and
186  * will be set to NULL, even on failure.
187  * @param flags Set of SwsOpCompileFlags
188  * @param input The input for the compiled passes. (Optional)
189  * @param output The resulting final output pass will be stored here.
190  * Optional if using SWS_OP_FLAG_DRY_RUN.
191  */
192 int ff_sws_compile_pass(SwsGraph *graph, const SwsOpBackend *backend,
193  SwsOpList **ops, int flags, SwsPass *input,
194  SwsPass **output);
195 
196 #endif /* SWSCALE_OPS_DISPATCH_H */
ff_sws_compile_pass
int ff_sws_compile_pass(SwsGraph *graph, const SwsOpBackend *backend, SwsOpList **ops, int flags, SwsPass *input, SwsPass **output)
Resolves an operation list to a graph pass.
Definition: ops_dispatch.c:751
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
ff_sws_ops_compile
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...
Definition: ops_dispatch.c:106
SwsPass
Represents a single filter pass in the scaling graph.
Definition: graph.h:75
SwsCompiledOp::func
SwsOpFunc func
Definition: ops_dispatch.h:104
out
static FILE * out
Definition: movenc.c:55
comp
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:79
SwsOpBackend::flags
SwsBackend flags
Definition: ops_dispatch.h:136
SwsOpExec::in_bump
ptrdiff_t in_bump[4]
Pointer bump, difference between stride and processed line size.
Definition: ops_dispatch.h:52
SwsOpExec::out_stride
ptrdiff_t out_stride[4]
Definition: ops_dispatch.h:43
SwsOpExec::in
const uint8_t * in[4]
Definition: ops_dispatch.h:38
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:226
SwsCompiledOp::opaque
bool opaque
If opaque is true, then func_opaque, priv and free are directly forwarded as SwsPass....
Definition: ops_dispatch.h:113
SwsOpExec::block_size_out
int32_t block_size_out[4]
Definition: ops_dispatch.h:59
SwsCompiledOp::cpu_flags
int cpu_flags
Definition: ops_dispatch.h:120
SwsOpExec::in_stride
ptrdiff_t in_stride[4]
Definition: ops_dispatch.h:42
SwsOpBackend::name
const char * name
Definition: ops_dispatch.h:135
SwsCompiledOp::over_write
int over_write[4]
Definition: ops_dispatch.h:125
SwsOpFunc
void(* SwsOpFunc)(const SwsOpExec *exec, const void *priv, int bx_start, int y_start, int bx_end, int y_end)
Process a given range of pixel blocks.
Definition: ops_dispatch.h:95
SwsCompiledOp::func_opaque
SwsPassFunc func_opaque
Definition: ops_dispatch.h:105
SwsOpExec::block_size_in
int32_t block_size_in[4]
Definition: ops_dispatch.h:58
SwsOpBackend::hw_format
enum AVPixelFormat hw_format
If NONE, backend only supports software frames.
Definition: ops_dispatch.h:155
SwsBackend
SwsBackend
Definition: swscale.h:110
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:504
SwsOpExec::in_bump_y
int32_t * in_bump_y
Line bump; determines how many additional lines to advance (after incrementing normally to the next l...
Definition: ops_dispatch.h:73
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
SwsOpBackend
Definition: ops_dispatch.h:134
SwsOpExec::height
int32_t height
Definition: ops_dispatch.h:56
SwsOpExec
Copyright (C) 2026 Niklas Haas.
Definition: ops_dispatch.h:36
SwsOpExec::slice_h
int32_t slice_h
Definition: ops_dispatch.h:57
SwsOpBackend::compile
int(* compile)(SwsContext *ctx, const SwsOpList *ops, SwsCompiledOp *out)
Compile an operation list to an implementation chain.
Definition: ops_dispatch.h:143
SWS_OP_FLAG_SPLIT_MEMCPY
@ SWS_OP_FLAG_SPLIT_MEMCPY
Definition: ops_dispatch.h:178
SwsOpExec::in_sub_x
uint8_t in_sub_x[4]
Definition: ops_dispatch.h:63
SwsCompiledOp::over_read
int over_read[4]
Definition: ops_dispatch.h:124
ff_sws_compiled_op_unref
void ff_sws_compiled_op_unref(SwsCompiledOp *comp)
Definition: ops_dispatch.c:128
ff_sws_op_backends
const SwsOpBackend *const ff_sws_op_backends[]
Definition: ops.c:42
SwsOpBackend::compile_uops
int(* compile_uops)(SwsContext *ctx, const SwsUOpList *uops, SwsCompiledOp *out)
Alternative to compile that takes a list of micro-ops directly.
Definition: ops_dispatch.h:148
frame.h
SwsCompiledOp::backend
const struct SwsOpBackend * backend
Definition: ops_dispatch.h:116
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
SwsOpExec::out
uint8_t * out[4]
Definition: ops_dispatch.h:39
SwsOpExec::in_offset_x
int32_t * in_offset_x
Pixel offset map; for horizontal scaling, in bytes.
Definition: ops_dispatch.h:81
graph.h
SwsOpExec::out_sub_y
uint8_t out_sub_y[4]
Definition: ops_dispatch.h:62
SwsOpExec::out_sub_x
uint8_t out_sub_x[4]
Definition: ops_dispatch.h:63
SwsOpExec::width
int32_t width
Definition: ops_dispatch.h:56
SwsCompiledOp::priv
void * priv
Definition: ops_dispatch.h:128
SwsCompiledOp::block_size
int block_size
Definition: ops_dispatch.h:123
SwsPassFunc
void(* SwsPassFunc)(const SwsFrame *out, const SwsFrame *in, int y, int h, const SwsPass *pass)
Output h lines of filtered data.
Definition: graph.h:45
SwsCompiledOp
Definition: ops_dispatch.h:101
SwsCompiledOp::slice_align
int slice_align
Definition: ops_dispatch.h:119
SwsUOpList
Definition: uops.h:292
SwsOpExec::in_sub_y
uint8_t in_sub_y[4]
Definition: ops_dispatch.h:62
SwsGraph
Filter graph, which represents a 'baked' pixel format conversion.
Definition: graph.h:122
uops.h
int32_t
int32_t
Definition: audioconvert.c:56
SWS_OP_FLAG_OPTIMIZE
@ SWS_OP_FLAG_OPTIMIZE
Definition: ops_dispatch.h:172
SwsOpList
Helper struct for representing a list of operations.
Definition: ops.h:265
SwsOpExec::slice_y
int32_t slice_y
Definition: ops_dispatch.h:57
SwsContext
Main external API structure.
Definition: swscale.h:227
SWS_OP_FLAG_DRY_RUN
@ SWS_OP_FLAG_DRY_RUN
Definition: ops_dispatch.h:175
SwsOpCompileFlags
SwsOpCompileFlags
Definition: ops_dispatch.h:170
SwsOpExec::out_bump
ptrdiff_t out_bump[4]
Definition: ops_dispatch.h:53
SwsCompiledOp::free
void(* free)(void *priv)
Definition: ops_dispatch.h:129