FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/format.h
Date: 2025-11-10 16:28:23
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 4013822 static inline int ff_q_isnan(const AVRational a)
32 {
33
4/4
✓ Branch 0 taken 3716314 times.
✓ Branch 1 taken 297508 times.
✓ Branch 2 taken 3428892 times.
✓ Branch 3 taken 287422 times.
4013822 return !a.num && !a.den;
34 }
35
36 /* Like av_cmp_q but considers NaN == NaN */
37 2299376 static inline int ff_q_equal(const AVRational a, const AVRational b)
38 {
39
4/6
✓ Branch 1 taken 1714446 times.
✓ Branch 2 taken 584930 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1714446 times.
✓ Branch 7 taken 584930 times.
✗ Branch 8 not taken.
2299376 return (ff_q_isnan(a) && ff_q_isnan(b)) || !av_cmp_q(a, b);
40 }
41
42 862266 static inline int ff_cie_xy_equal(const AVCIExy a, const AVCIExy b)
43 {
44
2/4
✓ Branch 1 taken 862266 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 862266 times.
✗ Branch 5 not taken.
862266 return ff_q_equal(a.x, b.x) && ff_q_equal(a.y, b.y);
45 }
46
47 287422 static inline int ff_prim_equal(const AVPrimaryCoefficients *a,
48 const AVPrimaryCoefficients *b)
49 {
50
1/2
✓ Branch 1 taken 287422 times.
✗ Branch 2 not taken.
574844 return ff_cie_xy_equal(a->r, b->r) &&
51
2/4
✓ Branch 0 taken 287422 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 287422 times.
✗ Branch 4 not taken.
574844 ff_cie_xy_equal(a->g, b->g) &&
52 287422 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 132672 static inline void ff_color_update_dynamic(SwsColor *dst, const SwsColor *src)
71 {
72 132672 dst->frame_peak = src->frame_peak;
73 132672 dst->frame_avg = src->frame_avg;
74 132672 }
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 AVColorRange range;
82 enum AVColorSpace csp;
83 enum AVChromaLocation loc;
84 const AVPixFmtDescriptor *desc; /* convenience */
85 SwsColor color;
86 } SwsFormat;
87
88 33540 static inline void ff_fmt_clear(SwsFormat *fmt)
89 {
90 33540 *fmt = (SwsFormat) {
91 .format = AV_PIX_FMT_NONE,
92 .range = AVCOL_RANGE_UNSPECIFIED,
93 .csp = AVCOL_SPC_UNSPECIFIED,
94 .loc = AVCHROMA_LOC_UNSPECIFIED,
95 .color = {
96 .prim = AVCOL_PRI_UNSPECIFIED,
97 .trc = AVCOL_TRC_UNSPECIFIED,
98 },
99 };
100 33540 }
101
102 /**
103 * This function also sanitizes and strips the input data, removing irrelevant
104 * fields for certain formats.
105 */
106 SwsFormat ff_fmt_from_frame(const AVFrame *frame, int field);
107
108 287422 static inline int ff_color_equal(const SwsColor *c1, const SwsColor *c2)
109 {
110 574844 return c1->prim == c2->prim &&
111
2/4
✓ Branch 0 taken 287422 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 287422 times.
✗ Branch 3 not taken.
574844 c1->trc == c2->trc &&
112
1/2
✓ Branch 1 taken 287422 times.
✗ Branch 2 not taken.
574844 ff_q_equal(c1->min_luma, c2->min_luma) &&
113
2/4
✓ Branch 0 taken 287422 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 287422 times.
✗ Branch 4 not taken.
862266 ff_q_equal(c1->max_luma, c2->max_luma) &&
114 287422 ff_prim_equal(&c1->gamut, &c2->gamut);
115 }
116
117 /* Tests only the static components of a colorspace, ignoring dimensions and per-frame data */
118 460090 static inline int ff_props_equal(const SwsFormat *fmt1, const SwsFormat *fmt2)
119 {
120 920180 return fmt1->interlaced == fmt2->interlaced &&
121
2/2
✓ Branch 0 taken 287673 times.
✓ Branch 1 taken 172417 times.
460090 fmt1->format == fmt2->format &&
122
2/2
✓ Branch 0 taken 287422 times.
✓ Branch 1 taken 251 times.
287673 fmt1->range == fmt2->range &&
123
1/2
✓ Branch 0 taken 287422 times.
✗ Branch 1 not taken.
287422 fmt1->csp == fmt2->csp &&
124
3/6
✓ Branch 0 taken 460090 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 287422 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 287422 times.
✗ Branch 5 not taken.
1207602 fmt1->loc == fmt2->loc &&
125 287422 ff_color_equal(&fmt1->color, &fmt2->color);
126 }
127
128 /* Tests only the static components of a colorspace, ignoring per-frame data */
129 466754 static inline int ff_fmt_equal(const SwsFormat *fmt1, const SwsFormat *fmt2)
130 {
131 926867 return fmt1->width == fmt2->width &&
132
6/6
✓ Branch 0 taken 460113 times.
✓ Branch 1 taken 6641 times.
✓ Branch 2 taken 460090 times.
✓ Branch 3 taken 23 times.
✓ Branch 4 taken 287422 times.
✓ Branch 5 taken 172668 times.
926844 fmt1->height == fmt2->height &&
133 460090 ff_props_equal(fmt1, fmt2);
134 }
135
136 static inline int ff_fmt_align(enum AVPixelFormat fmt)
137 {
138 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
139 if (desc->flags & AV_PIX_FMT_FLAG_BAYER) {
140 return 2;
141 } else {
142 return 1 << desc->log2_chroma_h;
143 }
144 }
145
146 int ff_test_fmt(const SwsFormat *fmt, int output);
147
148 /* Returns true if the formats are incomplete, false otherwise */
149 bool ff_infer_colors(SwsColor *src, SwsColor *dst);
150
151 typedef struct SwsOpList SwsOpList;
152 typedef enum SwsPixelType SwsPixelType;
153
154 /**
155 * Append a set of operations for decoding/encoding raw pixels. This will
156 * handle input read/write, swizzling, shifting and byte swapping.
157 *
158 * Returns 0 on success, or a negative error code on failure.
159 */
160 int ff_sws_decode_pixfmt(SwsOpList *ops, enum AVPixelFormat fmt);
161 int ff_sws_encode_pixfmt(SwsOpList *ops, enum AVPixelFormat fmt);
162
163 /**
164 * Append a set of operations for transforming decoded pixel values to/from
165 * normalized RGB in the specified gamut and pixel type.
166 *
167 * Returns 0 on success, or a negative error code on failure.
168 */
169 int ff_sws_decode_colors(SwsContext *ctx, SwsPixelType type, SwsOpList *ops,
170 const SwsFormat fmt, bool *incomplete);
171 int ff_sws_encode_colors(SwsContext *ctx, SwsPixelType type, SwsOpList *ops,
172 const SwsFormat fmt, bool *incomplete);
173
174 #endif /* SWSCALE_FORMAT_H */
175