FFmpeg
graph.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 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_GRAPH_H
22 #define SWSCALE_GRAPH_H
23 
24 #include <stdbool.h>
25 
26 #include "libavutil/slicethread.h"
27 #include "libavutil/buffer.h"
28 
29 #include "swscale.h"
30 #include "format.h"
31 
32 static av_always_inline av_const int ff_fmt_vshift(enum AVPixelFormat fmt, int plane)
33 {
35  return (plane == 1 || plane == 2) ? desc->log2_chroma_h : 0;
36 }
37 
38 typedef struct SwsPass SwsPass;
39 typedef struct SwsGraph SwsGraph;
40 
41 /**
42  * Output `h` lines of filtered data. `out` and `in` point to the
43  * start of the image buffer for this pass.
44  */
45 typedef void (*SwsPassFunc)(const SwsFrame *out, const SwsFrame *in,
46  int y, int h, const SwsPass *pass);
47 
48 /**
49  * Function to run from the main thread before processing any lines.
50  */
51 typedef int (*SwsPassSetup)(const SwsFrame *out, const SwsFrame *in,
52  const SwsPass *pass);
53 
54 /**
55  * Represents an output buffer for a filter pass. During filter graph
56  * construction, these merely hold the metadata. Allocation of the underlying
57  * storage is deferred until after all filter passes are settled.
58  */
59 typedef struct SwsPassBuffer {
61 
62  int width, height; /* dimensions of this buffer */
63  AVFrame *avframe; /* backing storage for `frame` */
64 
65  /* Optional allocation hints for optimal performance */
66  int width_align; /* Align width to multiple of this */
67  int width_pad; /* Extra padding pixels */
69 
70 /**
71  * Represents a single filter pass in the scaling graph. Each filter will
72  * read from some previous pass's output, and write to a buffer associated
73  * with the pass (or into the final output image).
74  */
75 struct SwsPass {
76  const SwsGraph *graph;
77 
78  /**
79  * Filter main execution function. Called from multiple threads, with
80  * the granularity dictated by `slice_h`. Individual slices sent to `run`
81  * are always equal to (or smaller than, for the last slice) `slice_h`.
82  */
84  SwsBackend backend; /* backend this pass is using, or 0 */
85  enum AVPixelFormat format; /* new pixel format */
86  int width, height; /* new output size */
87  int slice_h; /* filter granularity */
89 
90  /**
91  * Filter input. This pass's output will be resolved to form this pass's.
92  * input. If NULL, the original input image is used.
93  */
95 
96  /**
97  * Filter output buffer. This struct is always allocated.
98  */
99  SwsPassBuffer *output; /* refstruct */
100 
101  /**
102  * Called once from the main thread before running the filter. Optional.
103  * Returns 0 or a negative error code.
104  */
106 
107  /**
108  * Optional private state and associated free() function.
109  */
110  void (*free)(void *priv);
111  void *priv;
112 };
113 
114 /**
115  * Align `width` to the optimal size for `pass`.
116  */
117 int ff_sws_pass_aligned_width(const SwsPass *pass, int width);
118 
119 /**
120  * Filter graph, which represents a 'baked' pixel format conversion.
121  */
122 typedef struct SwsGraph {
125  int num_threads; /* resolved at init() time */
126  bool incomplete; /* set during init() if formats had to be inferred */
127  bool noop; /* set during init() if the graph is a no-op */
128  SwsBackend backend; /* backends this graph is using, set during init() */
129 
131 
132  /** Sorted sequence of filter passes to apply */
135 
136  /**
137  * Cached copy of the public options that were used to construct this
138  * SwsGraph. Used only to detect when the graph needs to be reinitialized.
139  */
141 
142  /**
143  * Currently active format and processing parameters.
144  */
146  int field;
147 
148  /**
149  * Temporary execution state inside ff_sws_graph_run(); used to pass
150  * data to worker threads.
151  */
152  struct {
153  const SwsPass *pass; /* current filter pass */
154  const SwsFrame *input; /* current filter pass input/output */
155  const SwsFrame *output;
156  } exec;
157 } SwsGraph;
158 
159 /**
160  * Allocate an empty SwsGraph. Returns NULL on failure.
161  */
163 
164 /**
165  * Initialize the filter graph for a given pair of formats. Returns 0 or a
166  * negative error.
167  */
169  const SwsFormat *src, int field);
170 
171 /**
172  * Allocate and initialize the filter graph. Returns 0 or a negative error.
173  */
175  int field, SwsGraph **out_graph);
176 
177 
178 /**
179  * Allocate and add a new pass to the filter graph. Takes over ownership of
180  * `priv`, even on failure.
181  *
182  * @param graph Filter graph to add the pass to.
183  * @param fmt Pixel format of the output image.
184  * @param w Width of the output image.
185  * @param h Height of the output image.
186  * @param input Previous pass to read from, or NULL for the input image.
187  * @param align Minimum slice alignment for this pass, or 0 for no threading.
188  * @param run Filter function to run.
189  * @param setup Optional setup function to run from the main thread.
190  * @param priv Private state for the filter run function.
191  * @param free Function to free the private state.
192  * @param out_pass The newly added pass will be written here on success.
193  * @return 0 or a negative error code
194  */
195 int ff_sws_graph_add_pass(SwsGraph *graph, enum AVPixelFormat fmt,
196  int width, int height, SwsPass *input,
197  int align, SwsPassFunc run, SwsPassSetup setup,
198  void *priv, void (*free)(void *priv),
199  SwsPass **out_pass);
200 
201 /**
202  * Remove all passes added since the given index.
203  */
204 void ff_sws_graph_rollback(SwsGraph *graph, int since_idx);
205 
206 /**
207  * Uninitialize any state associate with this filter graph and free it.
208  */
209 void ff_sws_graph_free(SwsGraph **graph);
210 
211 /**
212  * Update dynamic per-frame HDR metadata without requiring a full reinit.
213  */
215 
216 /**
217  * Wrapper around ff_sws_graph_init() that reuses the existing graph if the
218  * format is compatible. This will also update dynamic per-frame metadata.
219  *
220  * Must also be called after changing any of the fields in `ctx`, or else they
221  * will have no effect.
222  */
224  const SwsFormat *src, int field);
225 
226 /**
227  * Dispatch the filter graph on a single field of the given frames. Internally
228  * threaded.
229  */
230 int ff_sws_graph_run(SwsGraph *graph, const AVFrame *dst, const AVFrame *src);
231 
232 #endif /* SWSCALE_GRAPH_H */
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
SwsGraph::slicethread
AVSliceThread * slicethread
Definition: graph.h:124
SwsGraph::ctx
SwsContext * ctx
Definition: graph.h:123
SwsPass
Represents a single filter pass in the scaling graph.
Definition: graph.h:75
SwsGraph::pass
const SwsPass * pass
Definition: graph.h:153
SwsGraph::passes
SwsPass ** passes
Sorted sequence of filter passes to apply.
Definition: graph.h:133
out
static FILE * out
Definition: movenc.c:55
color
Definition: vf_paletteuse.c:513
ff_sws_graph_alloc
SwsGraph * ff_sws_graph_alloc(void)
Allocate an empty SwsGraph.
Definition: graph.c:827
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3456
SwsPass::format
enum AVPixelFormat format
Definition: graph.h:85
SwsGraph::src
SwsFormat src
Currently active format and processing parameters.
Definition: graph.h:145
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:459
ff_sws_pass_aligned_width
int ff_sws_pass_aligned_width(const SwsPass *pass, int width)
Align width to the optimal size for pass.
Definition: graph.c:46
SwsGraph::output
const SwsFrame * output
Definition: graph.h:155
SwsPass::backend
SwsBackend backend
Definition: graph.h:84
SwsPass::setup
SwsPassSetup setup
Called once from the main thread before running the filter.
Definition: graph.h:105
ff_sws_graph_init
int ff_sws_graph_init(SwsGraph *graph, SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src, int field)
Initialize the filter graph for a given pair of formats.
Definition: graph.c:843
ff_sws_graph_add_pass
int ff_sws_graph_add_pass(SwsGraph *graph, enum AVPixelFormat fmt, int width, int height, SwsPass *input, int align, SwsPassFunc run, SwsPassSetup setup, void *priv, void(*free)(void *priv), SwsPass **out_pass)
Allocate and add a new pass to the filter graph.
Definition: graph.c:175
ff_sws_graph_create
int ff_sws_graph_create(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src, int field, SwsGraph **out_graph)
Allocate and initialize the filter graph.
Definition: graph.c:892
SwsPass::free
void(* free)(void *priv)
Optional private state and associated free() function.
Definition: graph.h:110
format.h
AVSliceThread
struct AVSliceThread AVSliceThread
Definition: slicethread.h:22
SwsPass::input
SwsPass * input
Filter input.
Definition: graph.h:94
av_always_inline
#define av_always_inline
Definition: attributes.h:76
ff_sws_graph_rollback
void ff_sws_graph_rollback(SwsGraph *graph, int since_idx)
Remove all passes added since the given index.
Definition: graph.c:909
SwsPass::width
int width
Definition: graph.h:86
SwsGraph::opts_copy
SwsContext opts_copy
Cached copy of the public options that were used to construct this SwsGraph.
Definition: graph.h:140
SwsPassBuffer::frame
SwsFrame frame
Definition: graph.h:60
SwsFrame
Represents a view into a single field of frame data.
Definition: format.h:221
SwsBackend
SwsBackend
Definition: swscale.h:110
av_const
#define av_const
Definition: attributes.h:113
SwsPass::priv
void * priv
Definition: graph.h:111
ff_sws_graph_update_metadata
void ff_sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color)
Update dynamic per-frame HDR metadata without requiring a full reinit.
Definition: graph.c:961
SwsGraph::num_passes
int num_passes
Definition: graph.h:134
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
field
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this field
Definition: writing_filters.txt:78
SwsPass::run
SwsPassFunc run
Filter main execution function.
Definition: graph.h:83
SwsGraph::field
int field
Definition: graph.h:146
run
uint8_t run
Definition: svq3.c:207
SwsGraph::exec
struct SwsGraph::@564 exec
Temporary execution state inside ff_sws_graph_run(); used to pass data to worker threads.
SwsPass::graph
const SwsGraph * graph
Definition: graph.h:76
SwsPassBuffer::avframe
AVFrame * avframe
Definition: graph.h:63
SwsPass::height
int height
Definition: graph.h:86
SwsGraph::hw_frames_ref
AVBufferRef * hw_frames_ref
Definition: graph.h:130
height
#define height
Definition: dsp.h:89
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
ff_sws_graph_reinit
int ff_sws_graph_reinit(SwsGraph *graph, SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src, int field)
Wrapper around ff_sws_graph_init() that reuses the existing graph if the format is compatible.
Definition: graph.c:947
SwsGraph::backend
SwsBackend backend
Definition: graph.h:128
SwsPassBuffer::height
int height
Definition: graph.h:62
SwsFormat
Definition: format.h:77
buffer.h
align
static const uint8_t *BS_FUNC() align(BSCTX *bc)
Skip bits to a byte boundary.
Definition: bitstream_template.h:419
SwsColor
Definition: format.h:60
SwsPass::output
SwsPassBuffer * output
Filter output buffer.
Definition: graph.h:99
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
slicethread.h
SwsGraph::input
const SwsFrame * input
Definition: graph.h:154
SwsPassBuffer::width_align
int width_align
Definition: graph.h:66
SwsGraph::dst
SwsFormat dst
Definition: graph.h:145
ff_fmt_vshift
static av_always_inline av_const int ff_fmt_vshift(enum AVPixelFormat fmt, int plane)
Definition: graph.h:32
SwsPass::slice_h
int slice_h
Definition: graph.h:87
SwsGraph::num_threads
int num_threads
Definition: graph.h:125
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
SwsPassBuffer::width_pad
int width_pad
Definition: graph.h:67
SwsGraph::noop
bool noop
Definition: graph.h:127
SwsPassBuffer::width
int width
Definition: graph.h:62
SwsPassSetup
int(* SwsPassSetup)(const SwsFrame *out, const SwsFrame *in, const SwsPass *pass)
Function to run from the main thread before processing any lines.
Definition: graph.h:51
desc
const char * desc
Definition: libsvtav1.c:83
SwsGraph::incomplete
bool incomplete
Definition: graph.h:126
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
SwsGraph
Filter graph, which represents a 'baked' pixel format conversion.
Definition: graph.h:122
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
SwsPassBuffer
Represents an output buffer for a filter pass.
Definition: graph.h:59
h
h
Definition: vp9dsp_template.c:2070
SwsPass::num_slices
int num_slices
Definition: graph.h:88
width
#define width
Definition: dsp.h:89
SwsContext
Main external API structure.
Definition: swscale.h:229
ff_sws_graph_run
int ff_sws_graph_run(SwsGraph *graph, const AVFrame *dst, const AVFrame *src)
Dispatch the filter graph on a single field of the given frames.
Definition: graph.c:996
ff_sws_graph_free
void ff_sws_graph_free(SwsGraph **graph)
Uninitialize any state associate with this filter graph and free it.
Definition: graph.c:916
src
#define src
Definition: vp8dsp.c:248
swscale.h