FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/format.h
Date: 2026-10-13 05:06:32
Exec Total Coverage
Lines: 33 33 100.0%
Functions: 8 8 100.0%
Branches: 32 48 66.7%

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_FORMAT_H
22 #define SWSCALE_FORMAT_H
23
24 #include <stdbool.h>
25
26 #include "libavutil/csp.h"
27 #include "libavutil/pixdesc.h"
28
29 #include "swscale.h"
30
31 6419248 static inline int ff_q_isnan(const AVRational a)
32 {
33
4/4
✓ Branch 0 taken 5948786 times.
✓ Branch 1 taken 470462 times.
✓ Branch 2 taken 5489472 times.
✓ Branch 3 taken 459314 times.
6419248 return !a.num && !a.den;
34 }
35
36 /* Like av_cmp_q but considers NaN == NaN */
37 3674512 static inline int ff_q_equal(const AVRational a, const AVRational b)
38 {
39
4/6
✓ Branch 1 taken 2744736 times.
✓ Branch 2 taken 929776 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2744736 times.
✓ Branch 7 taken 929776 times.
✗ Branch 8 not taken.
3674512 return (ff_q_isnan(a) && ff_q_isnan(b)) || !av_cmp_q(a, b);
40 }
41
42 1377942 static inline int ff_cie_xy_equal(const AVCIExy a, const AVCIExy b)
43 {
44
2/4
✓ Branch 1 taken 1377942 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1377942 times.
✗ Branch 5 not taken.
1377942 return ff_q_equal(a.x, b.x) && ff_q_equal(a.y, b.y);
45 }
46
47 459314 static inline int ff_prim_equal(const AVPrimaryCoefficients *a,
48 const AVPrimaryCoefficients *b)
49 {
50
1/2
✓ Branch 1 taken 459314 times.
✗ Branch 2 not taken.
918628 return ff_cie_xy_equal(a->r, b->r) &&
51
2/4
✓ Branch 0 taken 459314 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 459314 times.
✗ Branch 4 not taken.
918628 ff_cie_xy_equal(a->g, b->g) &&
52 459314 ff_cie_xy_equal(a->b, b->b);
53 }
54
55 enum {
56 FIELD_TOP, /* top/even rows, or progressive */
57 FIELD_BOTTOM, /* bottom/odd rows */
58 };
59
60 typedef struct SwsColor {
61 enum AVColorPrimaries prim;
62 enum AVColorTransferCharacteristic trc;
63 AVPrimaryCoefficients gamut; /* mastering display gamut */
64 AVRational min_luma; /* minimum luminance in nits */
65 AVRational max_luma; /* maximum luminance in nits */
66 AVRational frame_peak; /* per-frame/scene peak luminance, or 0 */
67 AVRational frame_avg; /* per-frame/scene average luminance, or 0 */
68 } SwsColor;
69
70 186121 static inline void ff_color_update_dynamic(SwsColor *dst, const SwsColor *src)
71 {
72 186121 dst->frame_peak = src->frame_peak;
73 186121 dst->frame_avg = src->frame_avg;
74 186121 }
75
76 /* Subset of AVFrame parameters that uniquely determine pixel representation */
77 typedef struct SwsFormat {
78 int width, height;
79 int interlaced;
80 int field;
81 enum AVPixelFormat format;
82 enum AVPixelFormat hw_format;
83 enum AVColorRange range;
84 enum AVColorSpace csp;
85 enum AVChromaLocation loc;
86 const AVPixFmtDescriptor *desc; /* convenience */
87 SwsColor color;
88 } SwsFormat;
89
90 3413774 static inline void ff_fmt_clear(SwsFormat *fmt)
91 {
92 3413774 *fmt = (SwsFormat) {
93 .format = AV_PIX_FMT_NONE,
94 .range = AVCOL_RANGE_UNSPECIFIED,
95 .csp = AVCOL_SPC_UNSPECIFIED,
96 .loc = AVCHROMA_LOC_UNSPECIFIED,
97 .color = {
98 .prim = AVCOL_PRI_UNSPECIFIED,
99 .trc = AVCOL_TRC_UNSPECIFIED,
100 },
101 };
102 3413774 }
103
104 /**
105 * This function also sanitizes and strips the input data, removing irrelevant
106 * fields for certain formats.
107 */
108 SwsFormat ff_fmt_from_frame(const AVFrame *frame, int field);
109
110 /**
111 * Subset of ff_fmt_from_frame() that sets default metadata for the format.
112 */
113 void ff_fmt_from_pixfmt(enum AVPixelFormat pixfmt, SwsFormat *fmt);
114
115 459314 static inline int ff_color_equal(const SwsColor *c1, const SwsColor *c2)
116 {
117 918628 return c1->prim == c2->prim &&
118
2/4
✓ Branch 0 taken 459314 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 459314 times.
✗ Branch 3 not taken.
918628 c1->trc == c2->trc &&
119
1/2
✓ Branch 1 taken 459314 times.
✗ Branch 2 not taken.
918628 ff_q_equal(c1->min_luma, c2->min_luma) &&
120
2/4
✓ Branch 0 taken 459314 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 459314 times.
✗ Branch 4 not taken.
1377942 ff_q_equal(c1->max_luma, c2->max_luma) &&
121 459314 ff_prim_equal(&c1->gamut, &c2->gamut);
122 }
123
124 /* Tests only the static components of a colorspace, ignoring per-frame data */
125 994336 static inline int ff_fmt_equal(const SwsFormat *fmt1, const SwsFormat *fmt2)
126 {
127 1779644 return fmt1->width == fmt2->width &&
128
2/2
✓ Branch 0 taken 739187 times.
✓ Branch 1 taken 46121 times.
785308 fmt1->height == fmt2->height &&
129
1/2
✓ Branch 0 taken 739187 times.
✗ Branch 1 not taken.
739187 fmt1->interlaced == fmt2->interlaced &&
130
1/2
✓ Branch 0 taken 739187 times.
✗ Branch 1 not taken.
739187 fmt1->field == fmt2->field &&
131
2/2
✓ Branch 0 taken 466200 times.
✓ Branch 1 taken 272987 times.
739187 fmt1->format == fmt2->format &&
132
2/2
✓ Branch 0 taken 463855 times.
✓ Branch 1 taken 2345 times.
466200 fmt1->range == fmt2->range &&
133
1/2
✓ Branch 0 taken 463855 times.
✗ Branch 1 not taken.
463855 fmt1->csp == fmt2->csp &&
134
5/6
✓ Branch 0 taken 785308 times.
✓ Branch 1 taken 209028 times.
✓ Branch 2 taken 459314 times.
✓ Branch 3 taken 4541 times.
✓ Branch 4 taken 459314 times.
✗ Branch 5 not taken.
2238958 fmt1->loc == fmt2->loc &&
135 459314 ff_color_equal(&fmt1->color, &fmt2->color);
136 }
137
138 static inline int ff_fmt_align(enum AVPixelFormat fmt)
139 {
140 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
141 if (desc->flags & AV_PIX_FMT_FLAG_BAYER) {
142 return 2;
143 } else {
144 return 1 << desc->log2_chroma_h;
145 }
146 }
147
148 /* Internal helper to test a format for either the legacy backend or the
149 * ops-based backends, depending on `backends` (must be nonzero). */
150 int ff_sws_test_pixfmt_backend(const SwsBackend backends,
151 enum AVPixelFormat format, int output);
152
153 /**
154 * Statically test if a given format is supported by the given set of
155 * backends. This is a heuristic, which may have false positives if a
156 * specific backend does not actually implement all operations that would be
157 * required to support the format.
158 */
159 int ff_test_fmt(SwsBackend backends, const SwsFormat *fmt, int output);
160
161 /* Returns true if the formats are incomplete, false otherwise */
162 bool ff_infer_colors(SwsColor *src, SwsColor *dst);
163
164 /**
165 * Infers an unspecified chroma location of a subsampled format from the
166 * other format. Returns true if a location was inferred, false otherwise.
167 */
168 bool ff_infer_chroma_loc(SwsFormat *src, SwsFormat *dst);
169
170 /**
171 * Wrapper around av_chroma_location_enum_to_pos() that accounts for
172 * the per-field offset introduced by interlacing.
173 */
174 void ff_sws_chroma_pos(const SwsFormat *fmt, bool *incomplete,
175 int *out_xpos, int *out_ypos);
176
177 typedef struct SwsOpList SwsOpList;
178 typedef enum SwsPixelType SwsPixelType;
179
180 /**
181 * Append a set of operations for decoding/encoding raw pixels. This will
182 * handle input read/write, swizzling, shifting and byte swapping.
183 *
184 * Returns 0 on success, or a negative error code on failure.
185 */
186 int ff_sws_decode_pixfmt(SwsOpList *ops, const SwsFormat *fmt);
187 int ff_sws_encode_pixfmt(SwsOpList *ops, const SwsFormat *fmt);
188
189 /**
190 * Append a set of operations for transforming decoded pixel values to/from
191 * normalized RGB in the specified gamut and pixel type.
192 *
193 * Returns 0 on success, or a negative error code on failure.
194 */
195 int ff_sws_decode_colors(SwsContext *ctx, SwsPixelType type, SwsOpList *ops,
196 const SwsFormat *fmt, bool *incomplete);
197 int ff_sws_encode_colors(SwsContext *ctx, SwsPixelType type, SwsOpList *ops,
198 const SwsFormat *src, const SwsFormat *dst,
199 bool *incomplete);
200
201 /**
202 * Append a set of operations for scaling pixels to a different resolution.
203 *
204 * Returns 0 on success, or a negative error code on failure.
205 */
206 int ff_sws_add_filters(SwsContext *ctx, SwsPixelType type, SwsOpList *ops,
207 const SwsFormat *src, const SwsFormat *dst);
208
209 /**
210 * Append a set of operations for applying a gamut/tone mapping 3D LUT to
211 * the pixels. The input and output domain are assumed to be normalized
212 * floating point RGBA in the range [0, 1].
213 *
214 * Returns 0 on success, or a negative error code on failure.
215 */
216 typedef struct SwsLut3D SwsLut3D;
217 int ff_sws_apply_lut3d(SwsContext *ctx, SwsPixelType type, SwsOpList *ops,
218 const SwsLut3D *lut3d);
219
220 /**
221 * Generate an SwsOpList defining a conversion from `src` to `dst`, with an
222 * optional 3DLUT for converting between gamuts.
223 *
224 * Returns 0 on success, or a negative error code on failure.
225 */
226 int ff_sws_op_list_generate(SwsContext *ctx, const SwsFormat *src,
227 const SwsFormat *dst, const SwsLut3D *lut3d,
228 SwsOpList **out_ops, bool *incomplete);
229
230 /**
231 * Represents a view into a single field of frame data.
232 *
233 * Ostensibly, this is a (non-compatible) subset of AVFrame, however, the
234 * semantics are VERY different.
235 *
236 * Unlike AVFrame, this struct does NOT own any data references. All buffers
237 * referenced by a SwsFrame are managed externally. This merely represents
238 * a view into data.
239 *
240 * This struct is not refcounted, and may be freely copied onto the stack.
241 */
242 typedef struct SwsFrame {
243 /* Data buffers and line stride */
244 uint8_t *data[4];
245 int linesize[4];
246
247 /**
248 * Dimensions and format
249 */
250 int width, height;
251 enum AVPixelFormat format;
252
253 /**
254 * Pointer to the original AVFrame, if there is a 1:1 correspondence.
255 **/
256 const AVFrame *avframe;
257 } SwsFrame;
258
259 /**
260 * Initialize a SwsFrame from an AVFrame.
261 */
262 void ff_sws_frame_from_avframe(SwsFrame *dst, const AVFrame *src);
263
264 #endif /* SWSCALE_FORMAT_H */
265