Coverage Report

Created: 2026-04-01 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libswscale/graph.h
Line
Count
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
static av_always_inline av_const int ff_fmt_vshift(enum AVPixelFormat fmt, int plane)
33
0
{
34
0
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
35
0
    return (plane == 1 || plane == 2) ? desc->log2_chroma_h : 0;
36
0
}
Unexecuted instantiation: swscale.c:ff_fmt_vshift
Unexecuted instantiation: utils.c:ff_fmt_vshift
Unexecuted instantiation: vscale.c:ff_fmt_vshift
Unexecuted instantiation: yuv2rgb.c:ff_fmt_vshift
Unexecuted instantiation: alphablend.c:ff_fmt_vshift
Unexecuted instantiation: format.c:ff_fmt_vshift
Unexecuted instantiation: graph.c:ff_fmt_vshift
Unexecuted instantiation: hscale_fast_bilinear.c:ff_fmt_vshift
Unexecuted instantiation: input.c:ff_fmt_vshift
Unexecuted instantiation: ops.c:ff_fmt_vshift
Unexecuted instantiation: ops_backend.c:ff_fmt_vshift
Unexecuted instantiation: ops_chain.c:ff_fmt_vshift
Unexecuted instantiation: ops_dispatch.c:ff_fmt_vshift
Unexecuted instantiation: ops_memcpy.c:ff_fmt_vshift
Unexecuted instantiation: ops_optimizer.c:ff_fmt_vshift
Unexecuted instantiation: options.c:ff_fmt_vshift
Unexecuted instantiation: output.c:ff_fmt_vshift
Unexecuted instantiation: rgb2rgb.c:ff_fmt_vshift
Unexecuted instantiation: slice.c:ff_fmt_vshift
Unexecuted instantiation: swscale_unscaled.c:ff_fmt_vshift
Unexecuted instantiation: gamma.c:ff_fmt_vshift
Unexecuted instantiation: hscale.c:ff_fmt_vshift
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 {
60
    SwsFrame frame;
61
62
    int width, height; /* dimensions of this buffer */
63
    AVFrame *avframe;  /* backing storage for `frame` */
64
} SwsPassBuffer;
65
66
/**
67
 * Represents a single filter pass in the scaling graph. Each filter will
68
 * read from some previous pass's output, and write to a buffer associated
69
 * with the pass (or into the final output image).
70
 */
71
struct SwsPass {
72
    const SwsGraph *graph;
73
74
    /**
75
     * Filter main execution function. Called from multiple threads, with
76
     * the granularity dictated by `slice_h`. Individual slices sent to `run`
77
     * are always equal to (or smaller than, for the last slice) `slice_h`.
78
     */
79
    SwsPassFunc run;
80
    enum AVPixelFormat format; /* new pixel format */
81
    int width, height; /* new output size */
82
    int slice_h;       /* filter granularity */
83
    int num_slices;
84
85
    /**
86
     * Filter input. This pass's output will be resolved to form this pass's.
87
     * input. If NULL, the original input image is used.
88
     */
89
    SwsPass *input;
90
91
    /**
92
     * Filter output buffer. This struct is always allocated.
93
     */
94
    SwsPassBuffer *output; /* refstruct */
95
96
    /**
97
     * Called once from the main thread before running the filter. Optional.
98
     * Returns 0 or a negative error code.
99
     */
100
    SwsPassSetup setup;
101
102
    /**
103
     * Optional private state and associated free() function.
104
     */
105
    void (*free)(void *priv);
106
    void *priv;
107
};
108
109
/**
110
 * Filter graph, which represents a 'baked' pixel format conversion.
111
 */
112
typedef struct SwsGraph {
113
    SwsContext *ctx;
114
    AVSliceThread *slicethread;
115
    int num_threads; /* resolved at init() time */
116
    bool incomplete; /* set during init() if formats had to be inferred */
117
    bool noop;       /* set during init() if the graph is a no-op */
118
119
    AVBufferRef *hw_frames_ref;
120
121
    /** Sorted sequence of filter passes to apply */
122
    SwsPass **passes;
123
    int num_passes;
124
125
    /**
126
     * Cached copy of the public options that were used to construct this
127
     * SwsGraph. Used only to detect when the graph needs to be reinitialized.
128
     */
129
    SwsContext opts_copy;
130
131
    /**
132
     * Currently active format and processing parameters.
133
     */
134
    SwsFormat src, dst;
135
    int field;
136
137
    /**
138
     * Temporary execution state inside ff_sws_graph_run(); used to pass
139
     * data to worker threads.
140
     */
141
    struct {
142
        const SwsPass *pass; /* current filter pass */
143
        const SwsFrame *input; /* current filter pass input/output */
144
        const SwsFrame *output;
145
    } exec;
146
} SwsGraph;
147
148
/**
149
 * Allocate and initialize the filter graph. Returns 0 or a negative error.
150
 */
151
int ff_sws_graph_create(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
152
                        int field, SwsGraph **out_graph);
153
154
155
/**
156
 * Allocate and add a new pass to the filter graph. Takes over ownership of
157
 * `priv`, even on failure.
158
 *
159
 * @param graph  Filter graph to add the pass to.
160
 * @param fmt    Pixel format of the output image.
161
 * @param w      Width of the output image.
162
 * @param h      Height of the output image.
163
 * @param input  Previous pass to read from, or NULL for the input image.
164
 * @param align  Minimum slice alignment for this pass, or 0 for no threading.
165
 * @param run    Filter function to run.
166
 * @param setup  Optional setup function to run from the main thread.
167
 * @param priv   Private state for the filter run function.
168
 * @param free   Function to free the private state.
169
 * @param out_pass The newly added pass will be written here on success.
170
 * @return 0 or a negative error code
171
 */
172
int ff_sws_graph_add_pass(SwsGraph *graph, enum AVPixelFormat fmt,
173
                          int width, int height, SwsPass *input,
174
                          int align, SwsPassFunc run, SwsPassSetup setup,
175
                          void *priv, void (*free)(void *priv),
176
                          SwsPass **out_pass);
177
178
/**
179
 * Remove all passes added since the given index.
180
 */
181
void ff_sws_graph_rollback(SwsGraph *graph, int since_idx);
182
183
/**
184
 * Uninitialize any state associate with this filter graph and free it.
185
 */
186
void ff_sws_graph_free(SwsGraph **graph);
187
188
/**
189
 * Update dynamic per-frame HDR metadata without requiring a full reinit.
190
 */
191
void ff_sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color);
192
193
/**
194
 * Wrapper around ff_sws_graph_create() that reuses the existing graph if the
195
 * format is compatible. This will also update dynamic per-frame metadata.
196
 * Must be called after changing any of the fields in `ctx`, or else they will
197
 * have no effect.
198
 */
199
int ff_sws_graph_reinit(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
200
                        int field, SwsGraph **graph);
201
202
/**
203
 * Dispatch the filter graph on a single field of the given frames. Internally
204
 * threaded.
205
 */
206
int ff_sws_graph_run(SwsGraph *graph, const AVFrame *dst, const AVFrame *src);
207
208
#endif /* SWSCALE_GRAPH_H */