FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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  * `out` and `in` always point to the main image input/output, regardless
96  * of `input` and `output` fields.
97  */
98  void (*setup)(const SwsImg *out, const SwsImg *in, const SwsPass *pass);
99 
100  /**
101  * Optional private state and associated free() function.
102  */
103  void (*free)(void *priv);
104  void *priv;
105 };
106 
107 /**
108  * Filter graph, which represents a 'baked' pixel format conversion.
109  */
110 typedef struct SwsGraph {
113  int num_threads; /* resolved at init() time */
114  bool incomplete; /* set during init() if formats had to be inferred */
115  bool noop; /* set during init() if the graph is a no-op */
116 
117  /** Sorted sequence of filter passes to apply */
120 
121  /**
122  * Cached copy of the public options that were used to construct this
123  * SwsGraph. Used only to detect when the graph needs to be reinitialized.
124  */
126 
127  /**
128  * Currently active format and processing parameters.
129  */
131  int field;
132 
133  /** Temporary execution state inside ff_sws_graph_run */
134  struct {
135  const SwsPass *pass; /* current filter pass */
138  } exec;
139 } SwsGraph;
140 
141 /**
142  * Allocate and initialize the filter graph. Returns 0 or a negative error.
143  */
145  int field, SwsGraph **out_graph);
146 
147 
148 /**
149  * Allocate and add a new pass to the filter graph.
150  *
151  * @param graph Filter graph to add the pass to.
152  * @param fmt Pixel format of the output image.
153  * @param w Width of the output image.
154  * @param h Height of the output image.
155  * @param input Previous pass to read from, or NULL for the input image.
156  * @param align Minimum slice alignment for this pass, or 0 for no threading.
157  * @param priv Private state for the filter run function.
158  * @param run Filter function to run.
159  * @return The newly created pass, or NULL on error.
160  */
162  int width, int height, SwsPass *input,
163  int align, void *priv, sws_filter_run_t run);
164 
165 /**
166  * Uninitialize any state associate with this filter graph and free it.
167  */
168 void ff_sws_graph_free(SwsGraph **graph);
169 
170 /**
171  * Update dynamic per-frame HDR metadata without requiring a full reinit.
172  */
174 
175 /**
176  * Wrapper around ff_sws_graph_create() that reuses the existing graph if the
177  * format is compatible. This will also update dynamic per-frame metadata.
178  * Must be called after changing any of the fields in `ctx`, or else they will
179  * have no effect.
180  */
182  int field, SwsGraph **graph);
183 
184 /**
185  * Dispatch the filter graph on a single field. Internally threaded.
186  */
187 void ff_sws_graph_run(SwsGraph *graph, uint8_t *const out_data[4],
188  const int out_linesize[4],
189  const uint8_t *const in_data[4],
190  const int in_linesize[4]);
191 
192 #endif /* SWSCALE_GRAPH_H */
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
SwsGraph::slicethread
AVSliceThread * slicethread
Definition: graph.h:112
SwsGraph::ctx
SwsContext * ctx
Definition: graph.h:111
SwsPass
Represents a single filter pass in the scaling graph.
Definition: graph.h:68
SwsGraph::pass
const SwsPass * pass
Definition: graph.h:135
SwsGraph::passes
SwsPass ** passes
Sorted sequence of filter passes to apply.
Definition: graph.h:118
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:3341
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:130
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:599
SwsPass::free
void(* free)(void *priv)
Optional private state and associated free() function.
Definition: graph.h:103
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:125
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:676
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:47
SwsPass::priv
void * priv
Definition: graph.h:104
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:98
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:692
SwsGraph::num_passes
int num_passes
Definition: graph.h:119
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:131
run
uint8_t run
Definition: svq3.c:207
SwsGraph::input
SwsImg input
Definition: graph.h:136
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:700
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:137
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:411
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:130
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:113
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:115
desc
const char * desc
Definition: libsvtav1.c:79
SwsGraph::incomplete
bool incomplete
Definition: graph.h:114
SwsGraph
Filter graph, which represents a 'baked' pixel format conversion.
Definition: graph.h:110
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
SwsPass::num_slices
int num_slices
Definition: graph.h:80
width
#define width
Definition: dsp.h:89
SwsGraph::exec
struct SwsGraph::@499 exec
Temporary execution state inside ff_sws_graph_run.
SwsContext
Main external API structure.
Definition: swscale.h:182
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:637
src
#define src
Definition: vp8dsp.c:248
swscale.h
SwsImg::data
uint8_t * data[4]
Definition: graph.h:35