FFmpeg
Loading...
Searching...
No Matches
format.c File Reference
#include "libavutil/attributes.h"
#include "libavutil/avassert.h"
#include "libavutil/hdr_dynamic_metadata.h"
#include "libavutil/hwcontext.h"
#include "libavutil/mastering_display_metadata.h"
#include "libavutil/refstruct.h"
#include "format.h"
#include "csputils.h"
#include "ops_internal.h"
#include "config_components.h"
#include "swscale_internal.h"

Go to the source code of this file.

Data Structures

struct  LegacyFormatEntry
 
struct  FmtInfo
 
struct  comp
 

Macros

#define RET(x)
 
#define BITSTREAM_FMT(SWIZ, FRAC, MODE, ...)
 
#define SUBPACKED_FMT(SWIZ, ...)
 
#define PACKED_FMT(SWIZ, N, ...)
 
#define RGBA   SWS_SWIZZLE(0, 1, 2, 3)
 
#define BGRA   SWS_SWIZZLE(2, 1, 0, 3)
 
#define ARGB   SWS_SWIZZLE(3, 0, 1, 2)
 
#define ABGR   SWS_SWIZZLE(3, 2, 1, 0)
 
#define AVYU   SWS_SWIZZLE(3, 2, 0, 1)
 
#define VYUA   SWS_SWIZZLE(2, 0, 1, 3)
 
#define UYVA   SWS_SWIZZLE(1, 0, 2, 3)
 
#define VUYA   BGRA
 
#define NATIVE_ENDIAN_FLAG   0
 
#define LINEAR_OP_DATA(TYPE, ...)
 
#define Q64(x)
 

Functions

int sws_isSupportedInput (enum AVPixelFormat pix_fmt)
 Return a positive value if pix_fmt is a supported input format, 0 otherwise.
 
int sws_isSupportedOutput (enum AVPixelFormat pix_fmt)
 Return a positive value if pix_fmt is a supported output format, 0 otherwise.
 
int sws_isSupportedEndiannessConversion (enum AVPixelFormat pix_fmt)
 
static void sanitize_fmt (SwsFormat *fmt, const AVPixFmtDescriptor *desc)
 
SwsFormat ff_fmt_from_frame (const AVFrame *frame, int field)
 This function also sanitizes and strips the input data, removing irrelevant fields for certain formats.
 
void ff_fmt_from_pixfmt (enum AVPixelFormat pixfmt, SwsFormat *fmt)
 Subset of ff_fmt_from_frame() that sets default metadata for the format.
 
static int infer_prim_ref (SwsColor *csp, const SwsColor *ref)
 
static int infer_trc_ref (SwsColor *csp, const SwsColor *ref)
 
bool ff_infer_colors (SwsColor *src, SwsColor *dst)
 
static int infer_loc_ref (SwsFormat *fmt, const SwsFormat *ref)
 
bool ff_infer_chroma_loc (SwsFormat *src, SwsFormat *dst)
 Infers an unspecified chroma location of a subsampled format from the other format.
 
void ff_sws_chroma_pos (const SwsFormat *fmt, bool *incomplete, int *out_x_pos, int *out_y_pos)
 Wrapper around av_chroma_location_enum_to_pos() that accounts for the per-field offset introduced by interlacing.
 
static int test_format_ops (enum AVPixelFormat format, int output)
 
static int test_format_legacy (enum AVPixelFormat format, int output)
 
int ff_sws_test_pixfmt_backend (const SwsBackend backends, enum AVPixelFormat format, int output)
 
int sws_test_format (enum AVPixelFormat format, int output)
 Test if a given (software) pixel format is supported by any backend, excluding unstable backends.
 
int sws_test_hw_format (enum AVPixelFormat format)
 Test if a given hardware pixel format is supported by any backend, excluding unstable backends.
 
int sws_test_colorspace (enum AVColorSpace csp, int output)
 Test if a given color space is supported.
 
int sws_test_primaries (enum AVColorPrimaries prim, int output)
 Test if a given set of color primaries is supported.
 
int sws_test_transfer (enum AVColorTransferCharacteristic trc, int output)
 Test if a given color transfer function is supported.
 
static int test_range (enum AVColorRange range)
 
static int test_loc (enum AVChromaLocation loc)
 
int ff_test_fmt (const SwsBackend backends, const SwsFormat *fmt, int output)
 Statically test if a given format is supported by the given set of backends.
 
int sws_test_frame (const AVFrame *frame, int output)
 Helper function to run all sws_test_* against a frame, as well as testing the basic frame properties for sanity.
 
int sws_is_noop (const AVFrame *dst, const AVFrame *src)
 Check if a given conversion is a noop.
 
void ff_sws_frame_from_avframe (SwsFrame *dst, const AVFrame *src)
 Initialize a SwsFrame from an AVFrame.
 
static const AVPixFmtDescriptor * fmt_desc_decoded (enum AVPixelFormat fmt)
 Returns the underlying descriptor for fake formats like PAL8 whose descriptors alone do not fully describe the pixel data.
 
static SwsPixelType fmt_pixel_type (const AVPixFmtDescriptor *desc)
 
static int is_regular_fmt (enum AVPixelFormat fmt)
 
static FmtInfo fmt_info_irregular (enum AVPixelFormat fmt)
 
static int cmp_comp (const void *a, const void *b)
 
static int fmt_analyze_regular (const AVPixFmtDescriptor *desc, SwsReadWriteOp *rw_op, SwsSwizzleOp *swizzle, SwsShiftOp *shift)
 
static int fmt_analyze (enum AVPixelFormat fmt, SwsReadWriteOp *rw_op, SwsPackOp *pack_op, SwsSwizzleOp *swizzle, SwsShiftOp *shift, SwsPixelType *pixel_type)
 
static void swizzle_inv (SwsSwizzleOp *swiz)
 
static SwsClearOp fmt_clear (const SwsFormat *fmt)
 This initializes all absent components explicitly to zero.
 
static AVRational64 intmax_q64 (int bits)
 
static int decode_pixfmt (SwsOpList *ops, const SwsFormat *fmt)
 
static int encode_pixfmt (SwsOpList *ops, const SwsFormat *fmt)
 
static AVRational64 av_neg_q64 (AVRational64 x)
 
static SwsLinearOp fmt_encode_range (const SwsFormat *fmt, SwsPixelType type, bool *incomplete)
 
static SwsLinearOp fmt_decode_range (const SwsFormat *fmt, SwsPixelType type, bool *incomplete)
 
static AVRational64 * generate_bayer_matrix (const int size_log2)
 
static bool trc_is_hdr (enum AVColorTransferCharacteristic trc)
 
static int fmt_dither (SwsContext *ctx, SwsOpList *ops, const SwsFormat *src, const SwsFormat *dst)
 
static SwsLinearOp linear_mat3 (const SwsPixelType type, const AVRational m00, const AVRational m01, const AVRational m02, const AVRational m10, const AVRational m11, const AVRational m12, const AVRational m20, const AVRational m21, const AVRational m22)
 
static int decode_colors (SwsContext *ctx, SwsPixelType type, SwsOpList *ops, const SwsFormat *fmt, bool *incomplete)
 
static int encode_colors (SwsContext *ctx, SwsPixelType type, SwsOpList *ops, const SwsFormat *src, const SwsFormat *dst, bool *incomplete)
 
static SwsScaler get_scaler_fallback (SwsContext *ctx)
 
static int add_filter (SwsContext *ctx, SwsPixelType type, SwsOpList *ops, SwsOpType filter, int src_size, int dst_size)
 
static int add_filters (SwsContext *ctx, SwsPixelType type, SwsOpList *ops, const SwsFormat *src, const SwsFormat *dst)
 
static int apply_lut3d (SwsContext *ctx, SwsPixelType type, SwsOpList *ops, const SwsLut3D *lut3d)
 
static int fmt_convert (SwsContext *ctx, const SwsFormat *src, const SwsFormat *dst, const SwsLut3D *lut3d, SwsOpList *ops, bool *incomplete)
 
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 between gamuts.
 

Variables

static const LegacyFormatEntry legacy_format_entries []
 

Macro Definition Documentation

◆ RET

#define RET ( x)
Value:
do { \
int _ret = (x); \
if (_ret < 0) \
return _ret; \
} while (0)

Definition at line 34 of file format.c.

Referenced by apply_lut3d(), decode_colors(), decode_pixfmt(), encode_colors(), encode_pixfmt(), and fmt_convert().

◆ BITSTREAM_FMT

#define BITSTREAM_FMT ( SWIZ,
FRAC,
MODE,
... )
Value:
(FmtInfo) { \
.rw = { .elems = 1, .frac = FRAC, .mode = MODE }, \
.swizzle = SWIZ, \
__VA_ARGS__ \
}

Definition at line 812 of file format.c.

Referenced by fmt_info_irregular().

◆ SUBPACKED_FMT

#define SUBPACKED_FMT ( SWIZ,
... )
Value:
(FmtInfo) { \
.rw = { .elems = 1, .mode = SWS_RW_PACKED }, \
.swizzle = SWIZ, \
.pack.pattern = {__VA_ARGS__}, \
}
@ SWS_RW_PACKED
Definition ops.h:88

Definition at line 818 of file format.c.

Referenced by fmt_info_irregular().

◆ PACKED_FMT

#define PACKED_FMT ( SWIZ,
N,
... )
Value:
(FmtInfo) { \
.rw = { .elems = N, .mode = SWS_RW_PACKED }, \
.swizzle = SWIZ, \
__VA_ARGS__ \
}
#define N
Definition af_mcompand.c:54

Definition at line 824 of file format.c.

Referenced by fmt_info_irregular().

◆ RGBA

#define RGBA   SWS_SWIZZLE(0, 1, 2, 3)

Definition at line 830 of file format.c.

◆ BGRA

#define BGRA   SWS_SWIZZLE(2, 1, 0, 3)

◆ ARGB

#define ARGB   SWS_SWIZZLE(3, 0, 1, 2)

◆ ABGR

#define ABGR   SWS_SWIZZLE(3, 2, 1, 0)

◆ AVYU

#define AVYU   SWS_SWIZZLE(3, 2, 0, 1)

Definition at line 834 of file format.c.

Referenced by fmt_info_irregular().

◆ VYUA

#define VYUA   SWS_SWIZZLE(2, 0, 1, 3)

Definition at line 835 of file format.c.

Referenced by fmt_info_irregular().

◆ UYVA

#define UYVA   SWS_SWIZZLE(1, 0, 2, 3)

Definition at line 836 of file format.c.

Referenced by findRgbConvFn(), and fmt_info_irregular().

◆ VUYA

#define VUYA   BGRA

Definition at line 837 of file format.c.

Referenced by findRgbConvFn(), and fmt_info_irregular().

◆ NATIVE_ENDIAN_FLAG

#define NATIVE_ENDIAN_FLAG   0

Definition at line 1086 of file format.c.

Referenced by decode_pixfmt(), and encode_pixfmt().

◆ LINEAR_OP_DATA

#define LINEAR_OP_DATA ( TYPE,
... )
Value:
((SwsLinearOp) { \
.type = { TYPE, TYPE, TYPE, TYPE }, \
.m = { __VA_ARGS__ }, \
})
#define TYPE
Definition ffv1dec.c:92

Definition at line 1244 of file format.c.

Referenced by apply_lut3d(), fmt_encode_range(), and linear_mat3().

◆ Q64

#define Q64 ( x)
Value:
ff_make_q64((x).num, (x).den)
static AVRational64 ff_make_q64(int64_t num, int64_t den)
Create an AVRational64.
Definition rational64.h:64
int num
Definition error.c:23

Definition at line 1480 of file format.c.

Referenced by linear_mat3().

Function Documentation

◆ sanitize_fmt()

static void sanitize_fmt ( SwsFormat * fmt,
const AVPixFmtDescriptor * desc )
static

Definition at line 305 of file format.c.

Referenced by ff_fmt_from_frame(), and ff_fmt_from_pixfmt().

◆ 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 formats.

Calculate largest value from histogram to use as fallback for clips with missing MaxSCL information. Note that this may end up picking the "reserved" value at the 5% percentile, which in practice appears to track the brightest pixel in the scene.

Definition at line 345 of file format.c.

Referenced by sws_frame_setup(), sws_is_noop(), and sws_test_frame().

◆ ff_fmt_from_pixfmt()

void ff_fmt_from_pixfmt ( enum AVPixelFormat pixfmt,
SwsFormat * fmt )

Subset of ff_fmt_from_frame() that sets default metadata for the format.

Definition at line 483 of file format.c.

Referenced by enum_ops_fmt().

◆ infer_prim_ref()

static int infer_prim_ref ( SwsColor * csp,
const SwsColor * ref )
static

Definition at line 491 of file format.c.

Referenced by ff_infer_colors().

◆ infer_trc_ref()

static int infer_trc_ref ( SwsColor * csp,
const SwsColor * ref )
static

Definition at line 515 of file format.c.

Referenced by ff_infer_colors().

◆ ff_infer_colors()

bool ff_infer_colors ( SwsColor * src,
SwsColor * dst )

Definition at line 540 of file format.c.

Referenced by enum_ops_fmt(), and generate_3dlut().

◆ infer_loc_ref()

static int infer_loc_ref ( SwsFormat * fmt,
const SwsFormat * ref )
static

Definition at line 557 of file format.c.

Referenced by ff_infer_chroma_loc().

◆ ff_infer_chroma_loc()

bool ff_infer_chroma_loc ( SwsFormat * src,
SwsFormat * dst )

Infers an unspecified chroma location of a subsampled format from the other format.

Returns true if a location was inferred, false otherwise.

Definition at line 568 of file format.c.

Referenced by init_passes(), and sws_is_noop().

◆ ff_sws_chroma_pos()

void ff_sws_chroma_pos ( const SwsFormat * fmt,
bool * incomplete,
int * out_x_pos,
int * out_y_pos )

Wrapper around av_chroma_location_enum_to_pos() that accounts for the per-field offset introduced by interlacing.

Definition at line 578 of file format.c.

Referenced by add_legacy_sws_pass().

◆ test_format_ops()

static int test_format_ops ( enum AVPixelFormat format,
int output )
static

Definition at line 1023 of file format.c.

Referenced by ff_sws_test_pixfmt_backend().

◆ test_format_legacy()

static int test_format_legacy ( enum AVPixelFormat format,
int output )
static

Definition at line 621 of file format.c.

Referenced by ff_sws_test_pixfmt_backend().

◆ ff_sws_test_pixfmt_backend()

int ff_sws_test_pixfmt_backend ( const SwsBackend backends,
enum AVPixelFormat format,
int output )

Definition at line 626 of file format.c.

Referenced by ff_test_fmt(), sws_test_format(), and test_formats().

◆ test_range()

static int test_range ( enum AVColorRange range)
static

Definition at line 682 of file format.c.

Referenced by ff_test_fmt().

◆ test_loc()

static int test_loc ( enum AVChromaLocation loc)
static

Definition at line 687 of file format.c.

Referenced by ff_test_fmt().

◆ ff_test_fmt()

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.

This is a heuristic, which may have false positives if a specific backend does not actually implement all operations that would be required to support the format.

Definition at line 692 of file format.c.

Referenced by sws_frame_setup(), and sws_test_frame().

◆ ff_sws_frame_from_avframe()

void ff_sws_frame_from_avframe ( SwsFrame * dst,
const AVFrame * src )

Initialize a SwsFrame from an AVFrame.

Definition at line 732 of file format.c.

Referenced by get_field(), and pass_alloc_output().

◆ fmt_desc_decoded()

static const AVPixFmtDescriptor * fmt_desc_decoded ( enum AVPixelFormat fmt)
inlinestatic

Returns the underlying descriptor for fake formats like PAL8 whose descriptors alone do not fully describe the pixel data.

Definition at line 749 of file format.c.

Referenced by decode_pixfmt(), encode_pixfmt(), fmt_clear(), fmt_dither(), and fmt_encode_range().

◆ fmt_pixel_type()

static SwsPixelType fmt_pixel_type ( const AVPixFmtDescriptor * desc)
static

Definition at line 760 of file format.c.

Referenced by decode_colors(), encode_colors(), fmt_analyze(), fmt_analyze_regular(), and fmt_clear().

◆ is_regular_fmt()

static int is_regular_fmt ( enum AVPixelFormat fmt)
static

Definition at line 782 of file format.c.

Referenced by fmt_analyze().

◆ fmt_info_irregular()

static FmtInfo fmt_info_irregular ( enum AVPixelFormat fmt)
static

Definition at line 839 of file format.c.

Referenced by fmt_analyze().

◆ cmp_comp()

static int cmp_comp ( const void * a,
const void * b )
static

Definition at line 925 of file format.c.

Referenced by fmt_analyze_regular().

◆ fmt_analyze_regular()

static int fmt_analyze_regular ( const AVPixFmtDescriptor * desc,
SwsReadWriteOp * rw_op,
SwsSwizzleOp * swizzle,
SwsShiftOp * shift )
static

Definition at line 933 of file format.c.

Referenced by fmt_analyze().

◆ fmt_analyze()

static int fmt_analyze ( enum AVPixelFormat fmt,
SwsReadWriteOp * rw_op,
SwsPackOp * pack_op,
SwsSwizzleOp * swizzle,
SwsShiftOp * shift,
SwsPixelType * pixel_type )
static

Definition at line 968 of file format.c.

Referenced by decode_pixfmt(), encode_pixfmt(), and test_format_ops().

◆ swizzle_inv()

static void swizzle_inv ( SwsSwizzleOp * swiz)
static

Definition at line 1038 of file format.c.

Referenced by decode_pixfmt().

◆ fmt_clear()

static SwsClearOp fmt_clear ( const SwsFormat * fmt)
static

This initializes all absent components explicitly to zero.

There is no need to worry about the correct neutral value as fmt_decode() will implicitly ignore and overwrite absent components in any case. This function is just to ensure that we don't operate on undefined memory. In most cases, it will end up getting pushed towards the output or optimized away entirely by the optimization pass.

Definition at line 1057 of file format.c.

Referenced by decode_pixfmt().

◆ intmax_q64()

static AVRational64 intmax_q64 ( int bits)
inlinestatic

Definition at line 1089 of file format.c.

Referenced by decode_pixfmt(), encode_colors(), and fmt_encode_range().

◆ decode_pixfmt()

static int decode_pixfmt ( SwsOpList * ops,
const SwsFormat * fmt )
static

Definition at line 1096 of file format.c.

Referenced by fmt_convert().

◆ encode_pixfmt()

static int encode_pixfmt ( SwsOpList * ops,
const SwsFormat * fmt )
static

Definition at line 1176 of file format.c.

Referenced by fmt_convert().

◆ av_neg_q64()

static AVRational64 av_neg_q64 ( AVRational64 x)
inlinestatic

Definition at line 1239 of file format.c.

Referenced by fmt_decode_range(), and fmt_encode_range().

◆ fmt_encode_range()

static SwsLinearOp fmt_encode_range ( const SwsFormat * fmt,
SwsPixelType type,
bool * incomplete )
static

Definition at line 1249 of file format.c.

Referenced by encode_colors(), and fmt_decode_range().

◆ fmt_decode_range()

static SwsLinearOp fmt_decode_range ( const SwsFormat * fmt,
SwsPixelType type,
bool * incomplete )
static

Definition at line 1314 of file format.c.

Referenced by decode_colors().

◆ generate_bayer_matrix()

static AVRational64 * generate_bayer_matrix ( const int size_log2)
static

To correctly round, we need to evenly distribute the result on [0, 1), giving an average value of 1/2.

After the above construction, we have a matrix with average value: [ 0/N + 1/N + 2/N + ... (N-1)/N ] / N = (N-1)/(2N) where N = size * size is the total number of entries.

To make the average value equal to 1/2 = N/(2N), add a bias of 1/(2N).

Definition at line 1332 of file format.c.

Referenced by fmt_dither().

◆ trc_is_hdr()

static bool trc_is_hdr ( enum AVColorTransferCharacteristic trc)
static

Definition at line 1373 of file format.c.

Referenced by fmt_dither().

◆ fmt_dither()

static int fmt_dither ( SwsContext * ctx,
SwsOpList * ops,
const SwsFormat * src,
const SwsFormat * dst )
static

For high-bit-depth sources without chroma, use same matrix offset for all color channels. This prevents introducing color noise in grayscale images; and also allows optimizing the dither operation. Skipped for low bit depth (<8 bpc) as the loss in PSNR, from the inability to diffuse error among all three channels, can be substantial.

This shifts: { X, Y, Z, W } -> { X, X, X, Y }

Definition at line 1389 of file format.c.

Referenced by encode_colors().

◆ linear_mat3()

static SwsLinearOp linear_mat3 ( const SwsPixelType type,
const AVRational m00,
const AVRational m01,
const AVRational m02,
const AVRational m10,
const AVRational m11,
const AVRational m12,
const AVRational m20,
const AVRational m21,
const AVRational m22 )
inlinestatic

Definition at line 1483 of file format.c.

Referenced by decode_colors(), and encode_colors().

◆ decode_colors()

static int decode_colors ( SwsContext * ctx,
SwsPixelType type,
SwsOpList * ops,
const SwsFormat * fmt,
bool * incomplete )
static

Definition at line 1497 of file format.c.

Referenced by fmt_convert().

◆ encode_colors()

static int encode_colors ( SwsContext * ctx,
SwsPixelType type,
SwsOpList * ops,
const SwsFormat * src,
const SwsFormat * dst,
bool * incomplete )
static

Definition at line 1583 of file format.c.

Referenced by fmt_convert().

◆ get_scaler_fallback()

static SwsScaler get_scaler_fallback ( SwsContext * ctx)
static

Definition at line 1683 of file format.c.

Referenced by add_filter().

◆ add_filter()

static int add_filter ( SwsContext * ctx,
SwsPixelType type,
SwsOpList * ops,
SwsOpType filter,
int src_size,
int dst_size )
static

Definition at line 1710 of file format.c.

Referenced by add_filter(), and add_filters().

◆ add_filters()

static int add_filters ( SwsContext * ctx,
SwsPixelType type,
SwsOpList * ops,
const SwsFormat * src,
const SwsFormat * dst )
static

Always perform horizontal scaling first, since it's much more likely to benefit from small integer optimizations; we should maybe flip the order here if we're downscaling the vertical resolution by a lot, though.

Definition at line 1747 of file format.c.

Referenced by fmt_convert().

◆ apply_lut3d()

static int apply_lut3d ( SwsContext * ctx,
SwsPixelType type,
SwsOpList * ops,
const SwsLut3D * lut3d )
static

Definition at line 1763 of file format.c.

Referenced by fmt_convert().

◆ fmt_convert()

static int fmt_convert ( SwsContext * ctx,
const SwsFormat * src,
const SwsFormat * dst,
const SwsLut3D * lut3d,
SwsOpList * ops,
bool * incomplete )
static

Definition at line 1811 of file format.c.

Referenced by ff_sws_op_list_generate().

◆ ff_sws_op_list_generate()

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 between gamuts.

Returns 0 on success, or a negative error code on failure.

Definition at line 1825 of file format.c.

Referenced by add_ops_convert_pass(), and enum_ops_fmt().

Variable Documentation

◆ legacy_format_entries

const LegacyFormatEntry legacy_format_entries[]
static