FFmpeg
Loading...
Searching...
No Matches
format.h
Go to the documentation of this file.
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
31static inline int ff_q_isnan(const AVRational a)
32{
33 return !a.num && !a.den;
34}
35
36/* Like av_cmp_q but considers NaN == NaN */
37static inline int ff_q_equal(const AVRational a, const AVRational b)
38{
39 return (ff_q_isnan(a) && ff_q_isnan(b)) || !av_cmp_q(a, b);
40}
41
42static inline int ff_cie_xy_equal(const AVCIExy a, const AVCIExy b)
43{
44 return ff_q_equal(a.x, b.x) && ff_q_equal(a.y, b.y);
45}
46
47static inline int ff_prim_equal(const AVPrimaryCoefficients *a,
49{
50 return ff_cie_xy_equal(a->r, b->r) &&
51 ff_cie_xy_equal(a->g, b->g) &&
52 ff_cie_xy_equal(a->b, b->b);
53}
54
55enum {
56 FIELD_TOP, /* top/even rows, or progressive */
57 FIELD_BOTTOM, /* bottom/odd rows */
58};
59
60typedef struct SwsColor {
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
70static inline void ff_color_update_dynamic(SwsColor *dst, const SwsColor *src)
71{
72 dst->frame_peak = src->frame_peak;
73 dst->frame_avg = src->frame_avg;
74}
75
76/* Subset of AVFrame parameters that uniquely determine pixel representation */
89
90static inline void ff_fmt_clear(SwsFormat *fmt)
91{
92 *fmt = (SwsFormat) {
93 .format = AV_PIX_FMT_NONE,
97 .color = {
100 },
101 };
102}
103
104/**
105 * This function also sanitizes and strips the input data, removing irrelevant
106 * fields for certain formats.
107 */
108SwsFormat 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 */
114
115static inline int ff_color_equal(const SwsColor *c1, const SwsColor *c2)
116{
117 return c1->prim == c2->prim &&
118 c1->trc == c2->trc &&
119 ff_q_equal(c1->min_luma, c2->min_luma) &&
120 ff_q_equal(c1->max_luma, c2->max_luma) &&
121 ff_prim_equal(&c1->gamut, &c2->gamut);
122}
123
124/* Tests only the static components of a colorspace, ignoring per-frame data */
125static inline int ff_fmt_equal(const SwsFormat *fmt1, const SwsFormat *fmt2)
126{
127 return fmt1->width == fmt2->width &&
128 fmt1->height == fmt2->height &&
129 fmt1->interlaced == fmt2->interlaced &&
130 fmt1->field == fmt2->field &&
131 fmt1->format == fmt2->format &&
132 fmt1->range == fmt2->range &&
133 fmt1->csp == fmt2->csp &&
134 fmt1->loc == fmt2->loc &&
135 ff_color_equal(&fmt1->color, &fmt2->color);
136}
137
138static inline int ff_fmt_align(enum AVPixelFormat fmt)
139{
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). */
150int 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 */
159int ff_test_fmt(SwsBackend backends, const SwsFormat *fmt, int output);
160
161/* Returns true if the formats are incomplete, false otherwise */
163
164/**
165 * Wrapper around av_chroma_location_enum_to_pos() that accounts for
166 * the per-field offset introduced by interlacing.
167 */
168void ff_sws_chroma_pos(const SwsFormat *fmt, bool *incomplete,
169 int *out_xpos, int *out_ypos);
170
171typedef struct SwsOpList SwsOpList;
173
174/**
175 * Append a set of operations for decoding/encoding raw pixels. This will
176 * handle input read/write, swizzling, shifting and byte swapping.
177 *
178 * Returns 0 on success, or a negative error code on failure.
179 */
182
183/**
184 * Append a set of operations for transforming decoded pixel values to/from
185 * normalized RGB in the specified gamut and pixel type.
186 *
187 * Returns 0 on success, or a negative error code on failure.
188 */
190 const SwsFormat *fmt, bool *incomplete);
192 const SwsFormat *src, const SwsFormat *dst,
193 bool *incomplete);
194
195/**
196 * Append a set of operations for scaling pixels to a different resolution.
197 *
198 * Returns 0 on success, or a negative error code on failure.
199 */
201 const SwsFormat *src, const SwsFormat *dst);
202
203/**
204 * Append a set of operations for applying a gamut/tone mapping 3D LUT to
205 * the pixels. The input and output domain are assumed to be normalized
206 * floating point RGBA in the range [0, 1].
207 *
208 * Returns 0 on success, or a negative error code on failure.
209 */
210typedef struct SwsLut3D SwsLut3D;
212 const SwsLut3D *lut3d);
213
214/**
215 * Generate an SwsOpList defining a conversion from `src` to `dst`, with an
216 * optional 3DLUT for converting between gamuts.
217 *
218 * Returns 0 on success, or a negative error code on failure.
219 */
221 const SwsFormat *dst, const SwsLut3D *lut3d,
222 SwsOpList **out_ops, bool *incomplete);
223
224/**
225 * Represents a view into a single field of frame data.
226 *
227 * Ostensibly, this is a (non-compatible) subset of AVFrame, however, the
228 * semantics are VERY different.
229 *
230 * Unlike AVFrame, this struct does NOT own any data references. All buffers
231 * referenced by a SwsFrame are managed externally. This merely represents
232 * a view into data.
233 *
234 * This struct is not refcounted, and may be freely copied onto the stack.
235 */
236typedef struct SwsFrame {
237 /* Data buffers and line stride */
238 uint8_t *data[4];
239 int linesize[4];
240
241 /**
242 * Dimensions and format
243 */
246
247 /**
248 * Pointer to the original AVFrame, if there is a 1:1 correspondence.
249 **/
251} SwsFrame;
252
253/**
254 * Initialize a SwsFrame from an AVFrame.
255 */
257
258#endif /* SWSCALE_FORMAT_H */
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static const char *const format[]
Definition af_aiir.c:444
static AVFormatContext * ctx
Colorspace value utility functions for libavutil.
static AVFrame * frame
@ FIELD_TOP
Definition format.h:56
@ FIELD_BOTTOM
Definition format.h:57
static void ff_fmt_clear(SwsFormat *fmt)
Definition format.h:90
static int ff_prim_equal(const AVPrimaryCoefficients *a, const AVPrimaryCoefficients *b)
Definition format.h:47
int ff_sws_test_pixfmt_backend(const SwsBackend backends, enum AVPixelFormat format, int output)
Definition format.c:602
static int ff_fmt_equal(const SwsFormat *fmt1, const SwsFormat *fmt2)
Definition format.h:125
int ff_sws_apply_lut3d(SwsContext *ctx, SwsPixelType type, SwsOpList *ops, const SwsLut3D *lut3d)
static int ff_q_isnan(const AVRational a)
Definition format.h:31
int ff_test_fmt(SwsBackend backends, const SwsFormat *fmt, int output)
Statically test if a given format is supported by the given set of backends.
Definition format.c:668
bool ff_infer_colors(SwsColor *src, SwsColor *dst)
Definition format.c:537
static int ff_cie_xy_equal(const AVCIExy a, const AVCIExy b)
Definition format.h:42
static int ff_fmt_align(enum AVPixelFormat fmt)
Definition format.h:138
static int ff_q_equal(const AVRational a, const AVRational b)
Definition format.h:37
void ff_sws_chroma_pos(const SwsFormat *fmt, bool *incomplete, int *out_xpos, int *out_ypos)
Wrapper around av_chroma_location_enum_to_pos() that accounts for the per-field offset introduced by ...
Definition format.c:554
int ff_sws_op_list_generate(SwsContext *ctx, const SwsFormat *src, const SwsFormat *dst, const SwsLut3D *lut3d, SwsOpList **out_ops, bool *incomplete)
Generate an SwsOpList defining a conversion from src to dst, with an optional 3DLUT for converting be...
int ff_sws_decode_pixfmt(SwsOpList *ops, const SwsFormat *fmt)
Append a set of operations for decoding/encoding raw pixels.
static int ff_color_equal(const SwsColor *c1, const SwsColor *c2)
Definition format.h:115
void ff_fmt_from_pixfmt(enum AVPixelFormat pixfmt, SwsFormat *fmt)
Subset of ff_fmt_from_frame() that sets default metadata for the format.
Definition format.c:480
int ff_sws_add_filters(SwsContext *ctx, SwsPixelType type, SwsOpList *ops, const SwsFormat *src, const SwsFormat *dst)
Append a set of operations for scaling pixels to a different resolution.
int ff_sws_encode_colors(SwsContext *ctx, SwsPixelType type, SwsOpList *ops, const SwsFormat *src, const SwsFormat *dst, bool *incomplete)
SwsFormat ff_fmt_from_frame(const AVFrame *frame, int field)
This function also sanitizes and strips the input data, removing irrelevant fields for certain format...
Definition format.c:345
int ff_sws_encode_pixfmt(SwsOpList *ops, const SwsFormat *fmt)
int ff_sws_decode_colors(SwsContext *ctx, SwsPixelType type, SwsOpList *ops, const SwsFormat *fmt, bool *incomplete)
Append a set of operations for transforming decoded pixel values to/from normalized RGB in the specif...
void ff_sws_frame_from_avframe(SwsFrame *dst, const AVFrame *src)
Initialize a SwsFrame from an AVFrame.
Definition format.c:707
static void ff_color_update_dynamic(SwsColor *dst, const SwsColor *src)
Definition format.h:70
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition rational.h:89
SwsBackend
Definition swscale.h:110
int a
cl_device_type type
#define b
Definition input.c:43
enum AVPixelFormat pixfmt
Definition kmsgrab.c:367
const char * desc
Definition libsvtav1.c:83
static const uint64_t c2
Definition murmur3.c:53
static const uint64_t c1
Definition murmur3.c:52
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_BAYER
The pixel format is following a Bayer pattern.
Definition pixdesc.h:152
AVChromaLocation
Location of chroma samples.
Definition pixfmt.h:802
@ AVCHROMA_LOC_UNSPECIFIED
Definition pixfmt.h:803
AVColorRange
Visual content value range.
Definition pixfmt.h:748
@ AVCOL_RANGE_UNSPECIFIED
Definition pixfmt.h:749
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition pixfmt.h:642
@ AVCOL_PRI_UNSPECIFIED
Definition pixfmt.h:645
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition pixfmt.h:672
@ AVCOL_TRC_UNSPECIFIED
Definition pixfmt.h:675
AVColorSpace
YUV colorspace type.
Definition pixfmt.h:706
@ AVCOL_SPC_UNSPECIFIED
Definition pixfmt.h:709
Struct containing chromaticity x and y values for the standard CIE 1931 chromaticity definition.
Definition csp.h:56
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
Struct defining the red, green, and blue primary locations in terms of CIE 1931 chromaticity x and y.
Definition csp.h:64
Rational number (pair of numerator and denominator).
Definition rational.h:58
AVPrimaryCoefficients gamut
Definition format.h:63
AVRational min_luma
Definition format.h:64
enum AVColorPrimaries prim
Definition format.h:61
AVRational frame_peak
Definition format.h:66
AVRational max_luma
Definition format.h:65
AVRational frame_avg
Definition format.h:67
enum AVColorTransferCharacteristic trc
Definition format.h:62
Main external API structure.
Definition swscale.h:227
SwsColor color
Definition format.h:87
enum AVPixelFormat format
Definition format.h:81
int interlaced
Definition format.h:79
int field
Definition format.h:80
enum AVPixelFormat hw_format
Definition format.h:82
const AVPixFmtDescriptor * desc
Definition format.h:86
enum AVColorSpace csp
Definition format.h:84
int width
Definition format.h:78
enum AVColorRange range
Definition format.h:83
int height
Definition format.h:78
enum AVChromaLocation loc
Definition format.h:85
Represents a view into a single field of frame data.
Definition format.h:236
uint8_t * data[4]
Definition format.h:238
int height
Definition format.h:244
const AVFrame * avframe
Pointer to the original AVFrame, if there is a 1:1 correspondence.
Definition format.h:250
int linesize[4]
Definition format.h:239
enum AVPixelFormat format
Definition format.h:245
int width
Dimensions and format.
Definition format.h:244
Append a set of operations for applying a gamut/tone mapping 3D LUT to the pixels.
Definition lut3d.h:50
Helper struct for representing a list of operations.
Definition ops.h:293
SwsOp * ops
Definition ops.h:294
external API header
#define src
Definition vp8dsp.c:248
SwsPixelType
Definition uops.h:39