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 "swscale.h"
28 #include "format.h"
29 
30 /**
31  * Represents a view into a single field of frame data.
32  */
33 typedef struct SwsImg {
35  uint8_t *data[4]; /* points to y=0 */
36  int linesize[4];
37 } SwsImg;
38 
39 static av_always_inline av_const int ff_fmt_vshift(enum AVPixelFormat fmt, int plane)
40 {
42  return (plane == 1 || plane == 2) ? desc->log2_chroma_h : 0;
43 }
44 
45 static av_const inline SwsImg ff_sws_img_shift(const SwsImg *base, const int y)
46 {
47  SwsImg img = *base;
48  for (int i = 0; i < 4 && img.data[i]; i++)
49  img.data[i] += (y >> ff_fmt_vshift(img.fmt, i)) * img.linesize[i];
50  return img;
51 }
52 
53 typedef struct SwsPass SwsPass;
54 typedef struct SwsGraph SwsGraph;
55 
56 /**
57  * Output `h` lines of filtered data. `out` and `in` point to the
58  * start of the image buffer for this pass.
59  */
60 typedef void (*sws_filter_run_t)(const SwsImg *out, const SwsImg *in,
61  int y, int h, const SwsPass *pass);
62 
63 /**
64  * Represents a single filter pass in the scaling graph. Each filter will
65  * read from some previous pass's output, and write to a buffer associated
66  * with the pass (or into the final output image).
67  */
68 struct SwsPass {
69  const SwsGraph *graph;
70 
71  /**
72  * Filter main execution function. Called from multiple threads, with
73  * the granularity dictated by `slice_h`. Individual slices sent to `run`
74  * are always equal to (or smaller than, for the last slice) `slice_h`.
75  */
77  enum AVPixelFormat format; /* new pixel format */
78  int width, height; /* new output size */
79  int slice_h; /* filter granularity */
81 
82  /**
83  * Filter input. This pass's output will be resolved to form this pass's.
84  * input. If NULL, the original input image is used.
85  */
86  const SwsPass *input;
87 
88  /**
89  * Filter output buffer. Allocated on demand and freed automatically.
90  */
92 
93  /**
94  * Called once from the main thread before running the filter. Optional.
95  */
96  void (*setup)(const SwsImg *out, const SwsImg *in, const SwsPass *pass);
97 
98  /**
99  * Optional private state and associated free() function.
100  */
101  void (*free)(void *priv);
102  void *priv;
103 };
104 
105 /**
106  * Filter graph, which represents a 'baked' pixel format conversion.
107  */
108 typedef struct SwsGraph {
111  int num_threads; /* resolved at init() time */
112  bool incomplete; /* set during init() if formats had to be inferred */
113  bool noop; /* set during init() if the graph is a no-op */
114 
115  /** Sorted sequence of filter passes to apply */
118 
119  /**
120  * Cached copy of the public options that were used to construct this
121  * SwsGraph. Used only to detect when the graph needs to be reinitialized.
122  */
124 
125  /**
126  * Currently active format and processing parameters.
127  */
129  int field;
130 
131  /** Temporary execution state inside ff_sws_graph_run */
132  struct {
133  const SwsPass *pass; /* current filter pass */
136  } exec;
137 } SwsGraph;
138 
139 /**
140  * Allocate and initialize the filter graph. Returns 0 or a negative error.
141  */
143  int field, SwsGraph **out_graph);
144 
145 
146 /**
147  * Allocate and add a new pass to the filter graph.
148  *
149  * @param graph Filter graph to add the pass to.
150  * @param fmt Pixel format of the output image.
151  * @param w Width of the output image.
152  * @param h Height of the output image.
153  * @param input Previous pass to read from, or NULL for the input image.
154  * @param align Minimum slice alignment for this pass, or 0 for no threading.
155  * @param priv Private state for the filter run function.
156  * @param run Filter function to run.
157  * @return The newly created pass, or NULL on error.
158  */
160  int width, int height, SwsPass *input,
161  int align, void *priv, sws_filter_run_t run);
162 
163 /**
164  * Uninitialize any state associate with this filter graph and free it.
165  */
166 void ff_sws_graph_free(SwsGraph **graph);
167 
168 /**
169  * Update dynamic per-frame HDR metadata without requiring a full reinit.
170  */
172 
173 /**
174  * Wrapper around ff_sws_graph_create() that reuses the existing graph if the
175  * format is compatible. This will also update dynamic per-frame metadata.
176  * Must be called after changing any of the fields in `ctx`, or else they will
177  * have no effect.
178  */
180  int field, SwsGraph **graph);
181 
182 /**
183  * Dispatch the filter graph on a single field. Internally threaded.
184  */
185 void ff_sws_graph_run(SwsGraph *graph, uint8_t *const out_data[4],
186  const int out_linesize[4],
187  const uint8_t *const in_data[4],
188  const int in_linesize[4]);
189 
190 #endif /* SWSCALE_GRAPH_H */
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
SwsGraph::slicethread
AVSliceThread * slicethread
Definition: graph.h:110
SwsGraph::ctx
SwsContext * ctx
Definition: graph.h:109
SwsPass
Represents a single filter pass in the scaling graph.
Definition: graph.h:68
SwsGraph::pass
const SwsPass * pass
Definition: graph.h:133
SwsGraph::passes
SwsPass ** passes
Sorted sequence of filter passes to apply.
Definition: graph.h:116
out
FILE * out
Definition: movenc.c:55
color
Definition: vf_paletteuse.c:513
SwsPass::output
SwsImg output
Filter output buffer.
Definition: graph.h:91
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3447
SwsPass::format
enum AVPixelFormat format
Definition: graph.h:77
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:60
SwsGraph::src
SwsFormat src
Currently active format and processing parameters.
Definition: graph.h:128
av_const
#define av_const
Definition: attributes.h:84
base
uint8_t base
Definition: vp3data.h:128
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:688
SwsPass::free
void(* free)(void *priv)
Optional private state and associated free() function.
Definition: graph.h:101
SwsImg
Represents a view into a single field of frame data.
Definition: graph.h:33
format.h
AVSliceThread
struct AVSliceThread AVSliceThread
Definition: slicethread.h:22
SwsPass::width
int width
Definition: graph.h:78
SwsGraph::opts_copy
SwsContext opts_copy
Cached copy of the public options that were used to construct this SwsGraph.
Definition: graph.h:123
ff_sws_graph_reinit
int ff_sws_graph_reinit(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src, int field, SwsGraph **graph)
Wrapper around ff_sws_graph_create() that reuses the existing graph if the format is compatible.
Definition: graph.c:765
ff_sws_graph_add_pass
SwsPass * ff_sws_graph_add_pass(SwsGraph *graph, enum AVPixelFormat fmt, int width, int height, SwsPass *input, int align, void *priv, sws_filter_run_t run)
Allocate and add a new pass to the filter graph.
Definition: graph.c:48
SwsPass::priv
void * priv
Definition: graph.h:102
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:96
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:781
SwsGraph::num_passes
int num_passes
Definition: graph.h:117
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::field
int field
Definition: graph.h:129
run
uint8_t run
Definition: svq3.c:207
SwsGraph::input
SwsImg input
Definition: graph.h:134
SwsPass::graph
const SwsGraph * graph
Definition: graph.h:69
ff_sws_graph_run
void ff_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:789
SwsPass::height
int height
Definition: graph.h:78
SwsImg::linesize
int linesize[4]
Definition: graph.h:36
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
SwsGraph::output
SwsImg output
Definition: graph.h:135
SwsFormat
Definition: format.h:77
align
static const uint8_t *BS_FUNC() align(BSCTX *bc)
Skip bits to a byte boundary.
Definition: bitstream_template.h:419
img
#define img
Definition: vf_colormatrix.c:114
SwsColor
Definition: format.h:60
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::dst
SwsFormat dst
Definition: graph.h:128
ff_fmt_vshift
static av_always_inline av_const int ff_fmt_vshift(enum AVPixelFormat fmt, int plane)
Definition: graph.h:39
SwsPass::slice_h
int slice_h
Definition: graph.h:79
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
SwsGraph::num_threads
int num_threads
Definition: graph.h:111
av_always_inline
#define av_always_inline
Definition: attributes.h:49
ff_sws_img_shift
static av_const SwsImg ff_sws_img_shift(const SwsImg *base, const int y)
Definition: graph.h:45
SwsGraph::noop
bool noop
Definition: graph.h:113
desc
const char * desc
Definition: libsvtav1.c:79
SwsGraph::incomplete
bool incomplete
Definition: graph.h:112
SwsGraph
Filter graph, which represents a 'baked' pixel format conversion.
Definition: graph.h:108
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
SwsImg::fmt
enum AVPixelFormat fmt
Definition: graph.h:34
SwsPass::run
sws_filter_run_t run
Filter main execution function.
Definition: graph.h:76
SwsPass::input
const SwsPass * input
Filter input.
Definition: graph.h:86
h
h
Definition: vp9dsp_template.c:2070
SwsGraph::exec
struct SwsGraph::@507 exec
Temporary execution state inside ff_sws_graph_run.
SwsPass::num_slices
int num_slices
Definition: graph.h:80
width
#define width
Definition: dsp.h:89
SwsContext
Main external API structure.
Definition: swscale.h:189
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:726
src
#define src
Definition: vp8dsp.c:248
swscale.h
SwsImg::data
uint8_t * data[4]
Definition: graph.h:35