FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/format.h
Date: 2026-04-17 04:27:32
Exec Total Coverage
Lines: 34 34 100.0%
Functions: 9 9 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 3547918 static inline int ff_q_isnan(const AVRational a)
32 {
33
4/4
✓ Branch 0 taken 3283676 times.
✓ Branch 1 taken 264242 times.
✓ Branch 2 taken 3029532 times.
✓ Branch 3 taken 254144 times.
3547918 return !a.num && !a.den;
34 }
35
36 /* Like av_cmp_q but considers NaN == NaN */
37 2033152 static inline int ff_q_equal(const AVRational a, const AVRational b)
38 {
39
4/6
✓ Branch 1 taken 1514766 times.
✓ Branch 2 taken 518386 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1514766 times.
✓ Branch 7 taken 518386 times.
✗ Branch 8 not taken.
2033152 return (ff_q_isnan(a) && ff_q_isnan(b)) || !av_cmp_q(a, b);
40 }
41
42 762432 static inline int ff_cie_xy_equal(const AVCIExy a, const AVCIExy b)
43 {
44
2/4
✓ Branch 1 taken 762432 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 762432 times.
✗ Branch 5 not taken.
762432 return ff_q_equal(a.x, b.x) && ff_q_equal(a.y, b.y);
45 }
46
47 254144 static inline int ff_prim_equal(const AVPrimaryCoefficients *a,
48 const AVPrimaryCoefficients *b)
49 {
50
1/2
✓ Branch 1 taken 254144 times.
✗ Branch 2 not taken.
508288 return ff_cie_xy_equal(a->r, b->r) &&
51
2/4
✓ Branch 0 taken 254144 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 254144 times.
✗ Branch 4 not taken.
508288 ff_cie_xy_equal(a->g, b->g) &&
52 254144 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 116033 static inline void ff_color_update_dynamic(SwsColor *dst, const SwsColor *src)
71 {
72 116033 dst->frame_peak = src->frame_peak;
73 116033 dst->frame_avg = src->frame_avg;
74 116033 }
75
76 /* Subset of AVFrame parameters that uniquely determine pixel representation */
77 typedef struct SwsFormat {
78 int width, height;
79 int interlaced;
80 enum AVPixelFormat format;
81 enum AVPixelFormat hw_format;
82 enum AVColorRange range;
83 enum AVColorSpace csp;
84 enum AVChromaLocation loc;
85 const AVPixFmtDescriptor *desc; /* convenience */
86 SwsColor color;
87 } SwsFormat;
88
89 318696 static inline void ff_fmt_clear(SwsFormat *fmt)
90 {
91 318696 *fmt = (SwsFormat) {
92 .format = AV_PIX_FMT_NONE,
93 .range = AVCOL_RANGE_UNSPECIFIED,
94 .csp = AVCOL_SPC_UNSPECIFIED,
95 .loc = AVCHROMA_LOC_UNSPECIFIED,
96 .color = {
97 .prim = AVCOL_PRI_UNSPECIFIED,
98 .trc = AVCOL_TRC_UNSPECIFIED,
99 },
100 };
101 318696 }
102
103 /**
104 * This function also sanitizes and strips the input data, removing irrelevant
105 * fields for certain formats.
106 */
107 SwsFormat ff_fmt_from_frame(const AVFrame *frame, int field);
108
109 /**
110 * Subset of ff_fmt_from_frame() that sets default metadata for the format.
111 */
112 void ff_fmt_from_pixfmt(enum AVPixelFormat pixfmt, SwsFormat *fmt);
113
114 254144 static inline int ff_color_equal(const SwsColor *c1, const SwsColor *c2)
115 {
116 508288 return c1->prim == c2->prim &&
117
2/4
✓ Branch 0 taken 254144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 254144 times.
✗ Branch 3 not taken.
508288 c1->trc == c2->trc &&
118
1/2
✓ Branch 1 taken 254144 times.
✗ Branch 2 not taken.
508288 ff_q_equal(c1->min_luma, c2->min_luma) &&
119
2/4
✓ Branch 0 taken 254144 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 254144 times.
✗ Branch 4 not taken.
762432 ff_q_equal(c1->max_luma, c2->max_luma) &&
120 254144 ff_prim_equal(&c1->gamut, &c2->gamut);
121 }
122
123 /* Tests only the static components of a colorspace, ignoring dimensions and per-frame data */
124 426964 static inline int ff_props_equal(const SwsFormat *fmt1, const SwsFormat *fmt2)
125 {
126 853928 return fmt1->interlaced == fmt2->interlaced &&
127
2/2
✓ Branch 0 taken 254395 times.
✓ Branch 1 taken 172569 times.
426964 fmt1->format == fmt2->format &&
128
2/2
✓ Branch 0 taken 254144 times.
✓ Branch 1 taken 251 times.
254395 fmt1->range == fmt2->range &&
129
1/2
✓ Branch 0 taken 254144 times.
✗ Branch 1 not taken.
254144 fmt1->csp == fmt2->csp &&
130
3/6
✓ Branch 0 taken 426964 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 254144 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 254144 times.
✗ Branch 5 not taken.
1108072 fmt1->loc == fmt2->loc &&
131 254144 ff_color_equal(&fmt1->color, &fmt2->color);
132 }
133
134 /* Tests only the static components of a colorspace, ignoring per-frame data */
135 433626 static inline int ff_fmt_equal(const SwsFormat *fmt1, const SwsFormat *fmt2)
136 {
137 860613 return fmt1->width == fmt2->width &&
138
6/6
✓ Branch 0 taken 426987 times.
✓ Branch 1 taken 6639 times.
✓ Branch 2 taken 426964 times.
✓ Branch 3 taken 23 times.
✓ Branch 4 taken 254144 times.
✓ Branch 5 taken 172820 times.
860590 fmt1->height == fmt2->height &&
139 426964 ff_props_equal(fmt1, fmt2);
140 }
141
142 static inline int ff_fmt_align(enum AVPixelFormat fmt)
143 {
144 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
145 if (desc->flags & AV_PIX_FMT_FLAG_BAYER) {
146 return 2;
147 } else {
148 return 1 << desc->log2_chroma_h;
149 }
150 }
151
152 int ff_test_fmt(const SwsFormat *fmt, int output);
153
154 /* Returns true if the formats are incomplete, false otherwise */
155 bool ff_infer_colors(SwsColor *src, SwsColor *dst);
156
157 typedef struct SwsOpList SwsOpList;
158 typedef enum SwsPixelType SwsPixelType;
159
160 /**
161 * Append a set of operations for decoding/encoding raw pixels. This will
162 * handle input read/write, swizzling, shifting and byte swapping.
163 *
164 * Returns 0 on success, or a negative error code on failure.
165 */
166 int ff_sws_decode_pixfmt(SwsOpList *ops, enum AVPixelFormat fmt);
167 int ff_sws_encode_pixfmt(SwsOpList *ops, enum AVPixelFormat fmt);
168
169 /**
170 * Append a set of operations for transforming decoded pixel values to/from
171 * normalized RGB in the specified gamut and pixel type.
172 *
173 * Returns 0 on success, or a negative error code on failure.
174 */
175 int ff_sws_decode_colors(SwsContext *ctx, SwsPixelType type, SwsOpList *ops,
176 const SwsFormat *fmt, bool *incomplete);
177 int ff_sws_encode_colors(SwsContext *ctx, SwsPixelType type, SwsOpList *ops,
178 const SwsFormat *src, const SwsFormat *dst,
179 bool *incomplete);
180
181 /**
182 * Represents a view into a single field of frame data.
183 *
184 * Ostensibly, this is a (non-compatible) subset of AVFrame, however, the
185 * semantics are VERY different.
186 *
187 * Unlike AVFrame, this struct does NOT own any data references. All buffers
188 * referenced by a SwsFrame are managed externally. This merely represents
189 * a view into data.
190 *
191 * This struct is not refcounted, and may be freely copied onto the stack.
192 */
193 typedef struct SwsFrame {
194 /* Data buffers and line stride */
195 uint8_t *data[4];
196 int linesize[4];
197
198 /**
199 * Dimensions and format
200 */
201 int width, height;
202 enum AVPixelFormat format;
203
204 /**
205 * Pointer to the original AVFrame, if there is a 1:1 correspondence.
206 **/
207 const AVFrame *avframe;
208 } SwsFrame;
209
210 /**
211 * Initialize a SwsFrame from an AVFrame.
212 */
213 void ff_sws_frame_from_avframe(SwsFrame *dst, const AVFrame *src);
214
215 #endif /* SWSCALE_FORMAT_H */
216