FFmpeg
utils.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_UTILS_H
22 #define SWSCALE_UTILS_H
23 
24 #include "libavutil/csp.h"
25 #include "libavutil/pixdesc.h"
26 
27 #include "swscale.h"
28 
29 static inline int ff_q_isnan(const AVRational a)
30 {
31  return !a.num && !a.den;
32 }
33 
34 /* Like av_cmp_q but considers NaN == NaN */
35 static inline int ff_q_equal(const AVRational a, const AVRational b)
36 {
37  return (ff_q_isnan(a) && ff_q_isnan(b)) || !av_cmp_q(a, b);
38 }
39 
40 static inline int ff_cie_xy_equal(const AVCIExy a, const AVCIExy b)
41 {
42  return ff_q_equal(a.x, b.x) && ff_q_equal(a.y, b.y);
43 }
44 
45 static inline int ff_prim_equal(const AVPrimaryCoefficients *a,
46  const AVPrimaryCoefficients *b)
47 {
48  return ff_cie_xy_equal(a->r, b->r) &&
49  ff_cie_xy_equal(a->g, b->g) &&
50  ff_cie_xy_equal(a->b, b->b);
51 }
52 
53 enum {
54  FIELD_TOP, /* top/even rows, or progressive */
55  FIELD_BOTTOM, /* bottom/odd rows */
56 };
57 
58 typedef struct SwsColor {
61  AVPrimaryCoefficients gamut; /* mastering display gamut */
62  AVRational min_luma; /* minimum luminance in nits */
63  AVRational max_luma; /* maximum luminance in nits */
64  AVRational frame_peak; /* per-frame/scene peak luminance, or 0 */
65  AVRational frame_avg; /* per-frame/scene average luminance, or 0 */
66 } SwsColor;
67 
68 static inline void ff_color_update_dynamic(SwsColor *dst, const SwsColor *src)
69 {
70  dst->frame_peak = src->frame_peak;
71  dst->frame_avg = src->frame_avg;
72 }
73 
74 /* Subset of AVFrame parameters that uniquely determine pixel representation */
75 typedef struct SwsFormat {
76  int width, height;
82  const AVPixFmtDescriptor *desc; /* convenience */
84 } SwsFormat;
85 
86 /**
87  * This function also sanitizes and strips the input data, removing irrelevant
88  * fields for certain formats.
89  */
91 
92 static inline int ff_color_equal(const SwsColor *c1, const SwsColor *c2)
93 {
94  return c1->prim == c2->prim &&
95  c1->trc == c2->trc &&
96  ff_q_equal(c1->min_luma, c2->min_luma) &&
97  ff_q_equal(c1->max_luma, c2->max_luma) &&
98  ff_prim_equal(&c1->gamut, &c2->gamut);
99 }
100 
101 /* Tests only the static components of a colorspace, ignoring per-frame data */
102 static inline int ff_fmt_equal(const SwsFormat *fmt1, const SwsFormat *fmt2)
103 {
104  return fmt1->width == fmt2->width &&
105  fmt1->height == fmt2->height &&
106  fmt1->interlaced == fmt2->interlaced &&
107  fmt1->format == fmt2->format &&
108  fmt1->range == fmt2->range &&
109  fmt1->csp == fmt2->csp &&
110  fmt1->loc == fmt2->loc &&
111  ff_color_equal(&fmt1->color, &fmt2->color);
112 }
113 
114 static inline int ff_fmt_align(enum AVPixelFormat fmt)
115 {
117  if (desc->flags & AV_PIX_FMT_FLAG_BAYER) {
118  return 2;
119  } else {
120  return 1 << desc->log2_chroma_h;
121  }
122 }
123 
124 int ff_test_fmt(const SwsFormat *fmt, int output);
125 
126 /* Returns 1 if the formats are incomplete, 0 otherwise */
128 
129 #endif /* SWSCALE_UTILS_H */
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
ff_q_isnan
static int ff_q_isnan(const AVRational a)
Definition: utils.h:29
AVColorTransferCharacteristic
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:611
FIELD_TOP
@ FIELD_TOP
Definition: utils.h:54
SwsFormat::interlaced
int interlaced
Definition: utils.h:77
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3170
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:403
SwsFormat::range
enum AVColorRange range
Definition: utils.h:79
pixdesc.h
b
#define b
Definition: input.c:41
ff_prim_equal
static int ff_prim_equal(const AVPrimaryCoefficients *a, const AVPrimaryCoefficients *b)
Definition: utils.h:45
ff_test_fmt
int ff_test_fmt(const SwsFormat *fmt, int output)
Definition: utils.c:2919
AVColorPrimaries
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:586
c1
static const uint64_t c1
Definition: murmur3.c:52
SwsColor::gamut
AVPrimaryCoefficients gamut
Definition: utils.h:61
AVPrimaryCoefficients
Struct defining the red, green, and blue primary locations in terms of CIE 1931 chromaticity x and y.
Definition: csp.h:64
ff_infer_colors
int ff_infer_colors(SwsColor *src, SwsColor *dst)
Definition: utils.c:2857
SwsColor::trc
enum AVColorTransferCharacteristic trc
Definition: utils.h:60
SwsFormat::height
int height
Definition: utils.h:76
ff_fmt_from_frame
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: utils.c:2656
ff_fmt_equal
static int ff_fmt_equal(const SwsFormat *fmt1, const SwsFormat *fmt2)
Definition: utils.h:102
field
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this field
Definition: writing_filters.txt:78
SwsColor::frame_peak
AVRational frame_peak
Definition: utils.h:64
ff_color_equal
static int ff_color_equal(const SwsColor *c1, const SwsColor *c2)
Definition: utils.h:92
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
ff_q_equal
static int ff_q_equal(const AVRational a, const AVRational b)
Definition: utils.h:35
AVCIExy
Struct containing chromaticity x and y values for the standard CIE 1931 chromaticity definition.
Definition: csp.h:56
ff_fmt_align
static int ff_fmt_align(enum AVPixelFormat fmt)
Definition: utils.h:114
ff_color_update_dynamic
static void ff_color_update_dynamic(SwsColor *dst, const SwsColor *src)
Definition: utils.h:68
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
SwsFormat
Definition: utils.h:75
SwsFormat::loc
enum AVChromaLocation loc
Definition: utils.h:81
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
csp.h
SwsColor
Definition: utils.h:58
SwsColor::frame_avg
AVRational frame_avg
Definition: utils.h:65
AV_PIX_FMT_FLAG_BAYER
#define AV_PIX_FMT_FLAG_BAYER
The pixel format is following a Bayer pattern.
Definition: pixdesc.h:152
AVChromaLocation
AVChromaLocation
Location of chroma samples.
Definition: pixfmt.h:736
SwsFormat::format
enum AVPixelFormat format
Definition: utils.h:78
AVColorSpace
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:640
SwsFormat::desc
const AVPixFmtDescriptor * desc
Definition: utils.h:82
SwsFormat::width
int width
Definition: utils.h:76
av_cmp_q
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:89
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
c2
static const uint64_t c2
Definition: murmur3.c:53
SwsFormat::color
SwsColor color
Definition: utils.h:83
SwsFormat::csp
enum AVColorSpace csp
Definition: utils.h:80
SwsColor::min_luma
AVRational min_luma
Definition: utils.h:62
desc
const char * desc
Definition: libsvtav1.c:79
SwsColor::max_luma
AVRational max_luma
Definition: utils.h:63
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FIELD_BOTTOM
@ FIELD_BOTTOM
Definition: utils.h:55
AVColorRange
AVColorRange
Visual content value range.
Definition: pixfmt.h:682
src
#define src
Definition: vp8dsp.c:248
swscale.h
ff_cie_xy_equal
static int ff_cie_xy_equal(const AVCIExy a, const AVCIExy b)
Definition: utils.h:40
SwsColor::prim
enum AVColorPrimaries prim
Definition: utils.h:59