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 "libavutil/slicethread.h"
25 #include "swscale.h"
26 #include "utils.h"
27 
28 /**
29  * Represents a view into a single field of frame data.
30  */
31 typedef struct SwsImg {
33  uint8_t *data[4]; /* points to y=0 */
34  int linesize[4];
35 } SwsImg;
36 
37 typedef struct SwsPass SwsPass;
38 typedef struct SwsGraph SwsGraph;
39 
40 /**
41  * Output `h` lines of filtered data. `out` and `in` point to the
42  * start of the image buffer for this pass.
43  */
44 typedef void (*sws_filter_run_t)(const SwsImg *out, const SwsImg *in,
45  int y, int h, const SwsPass *pass);
46 
47 /**
48  * Represents a single filter pass in the scaling graph. Each filter will
49  * read from some previous pass's output, and write to a buffer associated
50  * with the pass (or into the final output image).
51  */
52 struct SwsPass {
53  const SwsGraph *graph;
54 
55  /**
56  * Filter main execution function. Called from multiple threads, with
57  * the granularity dictated by `slice_h`. Individual slices sent to `run`
58  * are always equal to (or smaller than, for the last slice) `slice_h`.
59  */
61  enum AVPixelFormat format; /* new pixel format */
62  int width, height; /* new output size */
63  int slice_h; /* filter granularity */
65 
66  /**
67  * Filter input. This pass's output will be resolved to form this pass's.
68  * input. If NULL, the original input image is used.
69  */
70  const SwsPass *input;
71 
72  /**
73  * Filter output buffer. Allocated on demand and freed automatically.
74  */
76 
77  /**
78  * Called once from the main thread before running the filter. Optional.
79  * `out` and `in` always point to the main image input/output, regardless
80  * of `input` and `output` fields.
81  */
82  void (*setup)(const SwsImg *out, const SwsImg *in, const SwsPass *pass);
83 
84  /**
85  * Optional private state and associated free() function.
86  */
87  void (*free)(void *priv);
88  void *priv;
89 };
90 
91 /**
92  * Filter graph, which represents a 'baked' pixel format conversion.
93  */
94 typedef struct SwsGraph {
97  int num_threads; /* resolved at init() time */
98  int incomplete; /* set during init() if formats had to be inferred */
99  int noop; /* set during init() if the graph is a no-op */
100 
101  /** Sorted sequence of filter passes to apply */
104 
105  /**
106  * Cached copy of the public options that were used to construct this
107  * SwsGraph. Used only to detect when the graph needs to be reinitialized.
108  */
110 
111  /**
112  * Currently active format and processing parameters.
113  */
115  int field;
116 
117  /** Temporary execution state inside sws_graph_run */
118  struct {
119  const SwsPass *pass; /* current filter pass */
122  } exec;
123 } SwsGraph;
124 
125 /**
126  * Allocate and initialize the filter graph. Returns 0 or a negative error.
127  */
129  int field, SwsGraph **out_graph);
130 
131 /**
132  * Uninitialize any state associate with this filter graph and free it.
133  */
134 void sws_graph_free(SwsGraph **graph);
135 
136 /**
137  * Update dynamic per-frame HDR metadata without requiring a full reinit.
138  */
139 void sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color);
140 
141 /**
142  * Wrapper around sws_graph_create() that reuses the existing graph if the
143  * format is compatible. This will also update dynamic per-frame metadata.
144  * Must be called after changing any of the fields in `ctx`, or else they will
145  * have no effect.
146  */
148  int field, SwsGraph **graph);
149 
150 /**
151  * Dispatch the filter graph on a single field. Internally threaded.
152  */
153 void sws_graph_run(SwsGraph *graph, uint8_t *const out_data[4],
154  const int out_linesize[4],
155  const uint8_t *const in_data[4],
156  const int in_linesize[4]);
157 
158 #endif /* SWSCALE_GRAPH_H */
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
SwsGraph::incomplete
int incomplete
Definition: graph.h:98
SwsGraph::slicethread
AVSliceThread * slicethread
Definition: graph.h:96
SwsGraph::ctx
SwsContext * ctx
Definition: graph.h:95
SwsPass
Represents a single filter pass in the scaling graph.
Definition: graph.h:52
SwsGraph::pass
const SwsPass * pass
Definition: graph.h:119
SwsGraph::passes
SwsPass ** passes
Sorted sequence of filter passes to apply.
Definition: graph.h:102
out
FILE * out
Definition: movenc.c:55
color
Definition: vf_paletteuse.c:513
SwsPass::output
SwsImg output
Filter output buffer.
Definition: graph.h:75
sws_graph_free
void sws_graph_free(SwsGraph **graph)
Uninitialize any state associate with this filter graph and free it.
Definition: graph.c:648
SwsPass::format
enum AVPixelFormat format
Definition: graph.h:61
sws_filter_run_t
void(* sws_filter_run_t)(const SwsImg *out, const SwsImg *in, int y, int h, const SwsPass *pass)
Output h lines of filtered data.
Definition: graph.h:44
SwsGraph::src
SwsFormat src
Currently active format and processing parameters.
Definition: graph.h:114
SwsPass::free
void(* free)(void *priv)
Optional private state and associated free() function.
Definition: graph.h:87
SwsImg
Represents a view into a single field of frame data.
Definition: graph.h:31
AVSliceThread
struct AVSliceThread AVSliceThread
Definition: slicethread.h:22
SwsPass::width
int width
Definition: graph.h:62
SwsGraph::opts_copy
SwsContext opts_copy
Cached copy of the public options that were used to construct this SwsGraph.
Definition: graph.h:109
utils.h
sws_graph_update_metadata
void sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color)
Update dynamic per-frame HDR metadata without requiring a full reinit.
Definition: graph.c:703
SwsPass::priv
void * priv
Definition: graph.h:88
sws_graph_run
void sws_graph_run(SwsGraph *graph, uint8_t *const out_data[4], const int out_linesize[4], const uint8_t *const in_data[4], const int in_linesize[4])
Dispatch the filter graph on a single field.
Definition: graph.c:711
SwsPass::setup
void(* setup)(const SwsImg *out, const SwsImg *in, const SwsPass *pass)
Called once from the main thread before running the filter.
Definition: graph.h:82
SwsGraph::num_passes
int num_passes
Definition: graph.h:103
ctx
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
SwsGraph::noop
int noop
Definition: graph.h:99
SwsGraph::field
int field
Definition: graph.h:115
SwsGraph::input
SwsImg input
Definition: graph.h:120
SwsPass::graph
const SwsGraph * graph
Definition: graph.h:53
SwsPass::height
int height
Definition: graph.h:62
SwsImg::linesize
int linesize[4]
Definition: graph.h:34
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
sws_graph_reinit
int sws_graph_reinit(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src, int field, SwsGraph **graph)
Wrapper around sws_graph_create() that reuses the existing graph if the format is compatible.
Definition: graph.c:687
SwsGraph::output
SwsImg output
Definition: graph.h:121
SwsGraph::exec
struct SwsGraph::@461 exec
Temporary execution state inside sws_graph_run.
SwsFormat
Definition: utils.h:75
SwsColor
Definition: utils.h:58
slicethread.h
SwsGraph::dst
SwsFormat dst
Definition: graph.h:114
SwsPass::slice_h
int slice_h
Definition: graph.h:63
SwsGraph::num_threads
int num_threads
Definition: graph.h:97
SwsGraph
Filter graph, which represents a 'baked' pixel format conversion.
Definition: graph.h:94
SwsImg::fmt
enum AVPixelFormat fmt
Definition: graph.h:32
SwsPass::run
sws_filter_run_t run
Filter main execution function.
Definition: graph.h:60
sws_graph_create
int 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:610
SwsPass::input
const SwsPass * input
Filter input.
Definition: graph.h:70
h
h
Definition: vp9dsp_template.c:2070
SwsPass::num_slices
int num_slices
Definition: graph.h:64
SwsContext
Main external API structure.
Definition: swscale.h:182
src
#define src
Definition: vp8dsp.c:248
swscale.h
SwsImg::data
uint8_t * data[4]
Definition: graph.h:33