FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/graph.h
Date: 2026-03-14 06:54:08
Exec Total Coverage
Lines: 3 3 100.0%
Functions: 1 1 100.0%
Branches: 4 4 100.0%

Line Branch Exec Source
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 1516879 static av_always_inline av_const int ff_fmt_vshift(enum AVPixelFormat fmt, int plane)
33 {
34 1516879 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
35
4/4
✓ Branch 0 taken 1020801 times.
✓ Branch 1 taken 496078 times.
✓ Branch 2 taken 331542 times.
✓ Branch 3 taken 689259 times.
1516879 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 allocated output buffer for a filter pass.
56 */
57 typedef struct SwsPassBuffer {
58 SwsFrame frame;
59
60 int width, height; /* dimensions of this buffer */
61 AVFrame *avframe; /* backing storage for `frame` */
62 } SwsPassBuffer;
63
64 /**
65 * Represents a single filter pass in the scaling graph. Each filter will
66 * read from some previous pass's output, and write to a buffer associated
67 * with the pass (or into the final output image).
68 */
69 struct SwsPass {
70 const SwsGraph *graph;
71
72 /**
73 * Filter main execution function. Called from multiple threads, with
74 * the granularity dictated by `slice_h`. Individual slices sent to `run`
75 * are always equal to (or smaller than, for the last slice) `slice_h`.
76 */
77 SwsPassFunc run;
78 enum AVPixelFormat format; /* new pixel format */
79 int width, height; /* new output size */
80 int slice_h; /* filter granularity */
81 int num_slices;
82
83 /**
84 * Filter input. This pass's output will be resolved to form this pass's.
85 * input. If NULL, the original input image is used.
86 */
87 const SwsPass *input;
88
89 /**
90 * Filter output buffer. Allocated on demand and freed automatically.
91 */
92 SwsPassBuffer *output; /* refstruct */
93
94 /**
95 * Called once from the main thread before running the filter. Optional.
96 * Returns 0 or a negative error code.
97 */
98 SwsPassSetup setup;
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 {
111 SwsContext *ctx;
112 AVSliceThread *slicethread;
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 AVBufferRef *hw_frames_ref;
118
119 /** Sorted sequence of filter passes to apply */
120 SwsPass **passes;
121 int num_passes;
122
123 /**
124 * Cached copy of the public options that were used to construct this
125 * SwsGraph. Used only to detect when the graph needs to be reinitialized.
126 */
127 SwsContext opts_copy;
128
129 /**
130 * Currently active format and processing parameters.
131 */
132 SwsFormat src, dst;
133 int field;
134
135 /**
136 * Temporary execution state inside ff_sws_graph_run(); used to pass
137 * data to worker threads.
138 */
139 struct {
140 const SwsPass *pass; /* current filter pass */
141 const SwsFrame *input; /* current filter pass input/output */
142 const SwsFrame *output;
143 } exec;
144 } SwsGraph;
145
146 /**
147 * Allocate and initialize the filter graph. Returns 0 or a negative error.
148 */
149 int ff_sws_graph_create(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
150 int field, SwsGraph **out_graph);
151
152
153 /**
154 * Allocate and add a new pass to the filter graph. Takes over ownership of
155 * `priv`, even on failure.
156 *
157 * @param graph Filter graph to add the pass to.
158 * @param fmt Pixel format of the output image.
159 * @param w Width of the output image.
160 * @param h Height of the output image.
161 * @param input Previous pass to read from, or NULL for the input image.
162 * @param align Minimum slice alignment for this pass, or 0 for no threading.
163 * @param run Filter function to run.
164 * @param setup Optional setup function to run from the main thread.
165 * @param priv Private state for the filter run function.
166 * @param free Function to free the private state.
167 * @param out_pass The newly added pass will be written here on success.
168 * @return 0 or a negative error code
169 */
170 int ff_sws_graph_add_pass(SwsGraph *graph, enum AVPixelFormat fmt,
171 int width, int height, SwsPass *input,
172 int align, SwsPassFunc run, SwsPassSetup setup,
173 void *priv, void (*free)(void *priv),
174 SwsPass **out_pass);
175
176 /**
177 * Uninitialize any state associate with this filter graph and free it.
178 */
179 void ff_sws_graph_free(SwsGraph **graph);
180
181 /**
182 * Update dynamic per-frame HDR metadata without requiring a full reinit.
183 */
184 void ff_sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color);
185
186 /**
187 * Wrapper around ff_sws_graph_create() that reuses the existing graph if the
188 * format is compatible. This will also update dynamic per-frame metadata.
189 * Must be called after changing any of the fields in `ctx`, or else they will
190 * have no effect.
191 */
192 int ff_sws_graph_reinit(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
193 int field, SwsGraph **graph);
194
195 /**
196 * Dispatch the filter graph on a single field of the given frames. Internally
197 * threaded.
198 */
199 int ff_sws_graph_run(SwsGraph *graph, const AVFrame *dst, const AVFrame *src);
200
201 #endif /* SWSCALE_GRAPH_H */
202