Path: blob/master/dep/ffmpeg/include/libswscale/swscale.h
7519 views
/*1* Copyright (C) 2024 Niklas Haas2* Copyright (C) 2001-2011 Michael Niedermayer <[email protected]>3*4* This file is part of FFmpeg.5*6* FFmpeg is free software; you can redistribute it and/or7* modify it under the terms of the GNU Lesser General Public8* License as published by the Free Software Foundation; either9* 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 of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU14* Lesser General Public License for more details.15*16* You should have received a copy of the GNU Lesser General Public17* License along with FFmpeg; if not, write to the Free Software18* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA19*/2021#ifndef SWSCALE_SWSCALE_H22#define SWSCALE_SWSCALE_H2324/**25* @file26* @ingroup libsws27* external API header28*/2930#include <stdint.h>3132#include "libavutil/avutil.h"33#include "libavutil/frame.h"34#include "libavutil/log.h"35#include "libavutil/pixfmt.h"36#include "version_major.h"37#ifndef HAVE_AV_CONFIG_H38/* When included as part of the ffmpeg build, only include the major version39* to avoid unnecessary rebuilds. When included externally, keep including40* the full version information. */41#include "version.h"42#endif4344/**45* @defgroup libsws libswscale46* Color conversion and scaling library.47*48* @{49*50* Return the LIBSWSCALE_VERSION_INT constant.51*/52unsigned swscale_version(void);5354/**55* Return the libswscale build-time configuration.56*/57const char *swscale_configuration(void);5859/**60* Return the libswscale license.61*/62const char *swscale_license(void);6364/**65* Get the AVClass for SwsContext. It can be used in combination with66* AV_OPT_SEARCH_FAKE_OBJ for examining options.67*68* @see av_opt_find().69*/70const AVClass *sws_get_class(void);7172/******************************73* Flags and quality settings *74******************************/7576typedef enum SwsDither {77SWS_DITHER_NONE = 0, /* disable dithering */78SWS_DITHER_AUTO, /* auto-select from preset */79SWS_DITHER_BAYER, /* ordered dither matrix */80SWS_DITHER_ED, /* error diffusion */81SWS_DITHER_A_DITHER, /* arithmetic addition */82SWS_DITHER_X_DITHER, /* arithmetic xor */83SWS_DITHER_NB, /* not part of the ABI */84} SwsDither;8586typedef enum SwsAlphaBlend {87SWS_ALPHA_BLEND_NONE = 0,88SWS_ALPHA_BLEND_UNIFORM,89SWS_ALPHA_BLEND_CHECKERBOARD,90SWS_ALPHA_BLEND_NB, /* not part of the ABI */91} SwsAlphaBlend;9293typedef enum SwsFlags {94/**95* Scaler selection options. Only one may be active at a time.96*/97SWS_FAST_BILINEAR = 1 << 0, ///< fast bilinear filtering98SWS_BILINEAR = 1 << 1, ///< bilinear filtering99SWS_BICUBIC = 1 << 2, ///< 2-tap cubic B-spline100SWS_X = 1 << 3, ///< experimental101SWS_POINT = 1 << 4, ///< nearest neighbor102SWS_AREA = 1 << 5, ///< area averaging103SWS_BICUBLIN = 1 << 6, ///< bicubic luma, bilinear chroma104SWS_GAUSS = 1 << 7, ///< gaussian approximation105SWS_SINC = 1 << 8, ///< unwindowed sinc106SWS_LANCZOS = 1 << 9, ///< 3-tap sinc/sinc107SWS_SPLINE = 1 << 10, ///< cubic Keys spline108109/**110* Return an error on underspecified conversions. Without this flag,111* unspecified fields are defaulted to sensible values.112*/113SWS_STRICT = 1 << 11,114115/**116* Emit verbose log of scaling parameters.117*/118SWS_PRINT_INFO = 1 << 12,119120/**121* Perform full chroma upsampling when upscaling to RGB.122*123* For example, when converting 50x50 yuv420p to 100x100 rgba, setting this flag124* will scale the chroma plane from 25x25 to 100x100 (4:4:4), and then convert125* the 100x100 yuv444p image to rgba in the final output step.126*127* Without this flag, the chroma plane is instead scaled to 50x100 (4:2:2),128* with a single chroma sample being reused for both of the horizontally129* adjacent RGBA output pixels.130*/131SWS_FULL_CHR_H_INT = 1 << 13,132133/**134* Perform full chroma interpolation when downscaling RGB sources.135*136* For example, when converting a 100x100 rgba source to 50x50 yuv444p, setting137* this flag will generate a 100x100 (4:4:4) chroma plane, which is then138* downscaled to the required 50x50.139*140* Without this flag, the chroma plane is instead generated at 50x100 (dropping141* every other pixel), before then being downscaled to the required 50x50142* resolution.143*/144SWS_FULL_CHR_H_INP = 1 << 14,145146/**147* Force bit-exact output. This will prevent the use of platform-specific148* optimizations that may lead to slight difference in rounding, in favor149* of always maintaining exact bit output compatibility with the reference150* C code.151*152* Note: It is recommended to set both of these flags simultaneously.153*/154SWS_ACCURATE_RND = 1 << 18,155SWS_BITEXACT = 1 << 19,156157/**158* Deprecated flags.159*/160SWS_DIRECT_BGR = 1 << 15, ///< This flag has no effect161SWS_ERROR_DIFFUSION = 1 << 23, ///< Set `SwsContext.dither` instead162} SwsFlags;163164typedef enum SwsIntent {165SWS_INTENT_PERCEPTUAL = 0, ///< Perceptual tone mapping166SWS_INTENT_RELATIVE_COLORIMETRIC = 1, ///< Relative colorimetric clipping167SWS_INTENT_SATURATION = 2, ///< Saturation mapping168SWS_INTENT_ABSOLUTE_COLORIMETRIC = 3, ///< Absolute colorimetric clipping169SWS_INTENT_NB, ///< not part of the ABI170} SwsIntent;171172/***********************************173* Context creation and management *174***********************************/175176/**177* Main external API structure. New fields can be added to the end with178* minor version bumps. Removal, reordering and changes to existing fields179* require a major version bump. sizeof(SwsContext) is not part of the ABI.180*/181typedef struct SwsContext {182const AVClass *av_class;183184/**185* Private data of the user, can be used to carry app specific stuff.186*/187void *opaque;188189/**190* Bitmask of SWS_*. See `SwsFlags` for details.191*/192unsigned flags;193194/**195* Extra parameters for fine-tuning certain scalers.196*/197double scaler_params[2];198199/**200* How many threads to use for processing, or 0 for automatic selection.201*/202int threads;203204/**205* Dither mode.206*/207SwsDither dither;208209/**210* Alpha blending mode. See `SwsAlphaBlend` for details.211*/212SwsAlphaBlend alpha_blend;213214/**215* Use gamma correct scaling.216*/217int gamma_flag;218219/**220* Deprecated frame property overrides, for the legacy API only.221*222* Ignored by sws_scale_frame() when used in dynamic mode, in which223* case all properties are instead taken from the frame directly.224*/225int src_w, src_h; ///< Width and height of the source frame226int dst_w, dst_h; ///< Width and height of the destination frame227int src_format; ///< Source pixel format228int dst_format; ///< Destination pixel format229int src_range; ///< Source is full range230int dst_range; ///< Destination is full range231int src_v_chr_pos; ///< Source vertical chroma position in luma grid / 256232int src_h_chr_pos; ///< Source horizontal chroma position233int dst_v_chr_pos; ///< Destination vertical chroma position234int dst_h_chr_pos; ///< Destination horizontal chroma position235236/**237* Desired ICC intent for color space conversions.238*/239int intent;240241/* Remember to add new fields to graph.c:opts_equal() */242} SwsContext;243244/**245* Allocate an empty SwsContext and set its fields to default values.246*/247SwsContext *sws_alloc_context(void);248249/**250* Free the context and everything associated with it, and write NULL251* to the provided pointer.252*/253void sws_free_context(SwsContext **ctx);254255/***************************256* Supported frame formats *257***************************/258259/**260* Test if a given pixel format is supported.261*262* @param output If 0, test if compatible with the source/input frame;263* otherwise, with the destination/output frame.264* @param format The format to check.265*266* @return A positive integer if supported, 0 otherwise.267*/268int sws_test_format(enum AVPixelFormat format, int output);269270/**271* Test if a given color space is supported.272*273* @param output If 0, test if compatible with the source/input frame;274* otherwise, with the destination/output frame.275* @param colorspace The colorspace to check.276*277* @return A positive integer if supported, 0 otherwise.278*/279int sws_test_colorspace(enum AVColorSpace colorspace, int output);280281/**282* Test if a given set of color primaries is supported.283*284* @param output If 0, test if compatible with the source/input frame;285* otherwise, with the destination/output frame.286* @param primaries The color primaries to check.287*288* @return A positive integer if supported, 0 otherwise.289*/290int sws_test_primaries(enum AVColorPrimaries primaries, int output);291292/**293* Test if a given color transfer function is supported.294*295* @param output If 0, test if compatible with the source/input frame;296* otherwise, with the destination/output frame.297* @param trc The color transfer function to check.298*299* @return A positive integer if supported, 0 otherwise.300*/301int sws_test_transfer(enum AVColorTransferCharacteristic trc, int output);302303/**304* Helper function to run all sws_test_* against a frame, as well as testing305* the basic frame properties for sanity. Ignores irrelevant properties - for306* example, AVColorSpace is not checked for RGB frames.307*/308int sws_test_frame(const AVFrame *frame, int output);309310/**311* Like `sws_scale_frame`, but without actually scaling. It will instead312* merely initialize internal state that *would* be required to perform the313* operation, as well as returning the correct error code for unsupported314* frame combinations.315*316* @param ctx The scaling context.317* @param dst The destination frame to consider.318* @param src The source frame to consider.319* @return 0 on success, a negative AVERROR code on failure.320*/321int sws_frame_setup(SwsContext *ctx, const AVFrame *dst, const AVFrame *src);322323/********************324* Main scaling API *325********************/326327/**328* Check if a given conversion is a noop. Returns a positive integer if329* no operation needs to be performed, 0 otherwise.330*/331int sws_is_noop(const AVFrame *dst, const AVFrame *src);332333/**334* Scale source data from `src` and write the output to `dst`.335*336* This function can be used directly on an allocated context, without setting337* up any frame properties or calling `sws_init_context()`. Such usage is fully338* dynamic and does not require reallocation if the frame properties change.339*340* Alternatively, this function can be called on a context that has been341* explicitly initialized. However, this is provided only for backwards342* compatibility. In this usage mode, all frame properties must be correctly343* set at init time, and may no longer change after initialization.344*345* @param ctx The scaling context.346* @param dst The destination frame. The data buffers may either be already347* allocated by the caller or left clear, in which case they will348* be allocated by the scaler. The latter may have performance349* advantages - e.g. in certain cases some (or all) output planes350* may be references to input planes, rather than copies.351* @param src The source frame. If the data buffers are set to NULL, then352* this function behaves identically to `sws_frame_setup`.353* @return >= 0 on success, a negative AVERROR code on failure.354*/355int sws_scale_frame(SwsContext *c, AVFrame *dst, const AVFrame *src);356357/*************************358* Legacy (stateful) API *359*************************/360361#define SWS_SRC_V_CHR_DROP_MASK 0x30000362#define SWS_SRC_V_CHR_DROP_SHIFT 16363364#define SWS_PARAM_DEFAULT 123456365366#define SWS_MAX_REDUCE_CUTOFF 0.002367368#define SWS_CS_ITU709 1369#define SWS_CS_FCC 4370#define SWS_CS_ITU601 5371#define SWS_CS_ITU624 5372#define SWS_CS_SMPTE170M 5373#define SWS_CS_SMPTE240M 7374#define SWS_CS_DEFAULT 5375#define SWS_CS_BT2020 9376377/**378* Return a pointer to yuv<->rgb coefficients for the given colorspace379* suitable for sws_setColorspaceDetails().380*381* @param colorspace One of the SWS_CS_* macros. If invalid,382* SWS_CS_DEFAULT is used.383*/384const int *sws_getCoefficients(int colorspace);385386// when used for filters they must have an odd number of elements387// coeffs cannot be shared between vectors388typedef struct SwsVector {389double *coeff; ///< pointer to the list of coefficients390int length; ///< number of coefficients in the vector391} SwsVector;392393// vectors can be shared394typedef struct SwsFilter {395SwsVector *lumH;396SwsVector *lumV;397SwsVector *chrH;398SwsVector *chrV;399} SwsFilter;400401/**402* Return a positive value if pix_fmt is a supported input format, 0403* otherwise.404*/405int sws_isSupportedInput(enum AVPixelFormat pix_fmt);406407/**408* Return a positive value if pix_fmt is a supported output format, 0409* otherwise.410*/411int sws_isSupportedOutput(enum AVPixelFormat pix_fmt);412413/**414* @param[in] pix_fmt the pixel format415* @return a positive value if an endianness conversion for pix_fmt is416* supported, 0 otherwise.417*/418int sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt);419420/**421* Initialize the swscaler context sws_context.422*423* This function is considered deprecated, and provided only for backwards424* compatibility with sws_scale() and sws_start_frame(). The preferred way to425* use libswscale is to set all frame properties correctly and call426* sws_scale_frame() directly, without explicitly initializing the context.427*428* @return zero or positive value on success, a negative value on429* error430*/431av_warn_unused_result432int sws_init_context(SwsContext *sws_context, SwsFilter *srcFilter, SwsFilter *dstFilter);433434/**435* Free the swscaler context swsContext.436* If swsContext is NULL, then does nothing.437*/438void sws_freeContext(SwsContext *swsContext);439440/**441* Allocate and return an SwsContext. You need it to perform442* scaling/conversion operations using sws_scale().443*444* @param srcW the width of the source image445* @param srcH the height of the source image446* @param srcFormat the source image format447* @param dstW the width of the destination image448* @param dstH the height of the destination image449* @param dstFormat the destination image format450* @param flags specify which algorithm and options to use for rescaling451* @param param extra parameters to tune the used scaler452* For SWS_BICUBIC param[0] and [1] tune the shape of the basis453* function, param[0] tunes f(1) and param[1] f´(1)454* For SWS_GAUSS param[0] tunes the exponent and thus cutoff455* frequency456* For SWS_LANCZOS param[0] tunes the width of the window function457* @return a pointer to an allocated context, or NULL in case of error458* @note this function is to be removed after a saner alternative is459* written460*/461SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,462int dstW, int dstH, enum AVPixelFormat dstFormat,463int flags, SwsFilter *srcFilter,464SwsFilter *dstFilter, const double *param);465466/**467* Scale the image slice in srcSlice and put the resulting scaled468* slice in the image in dst. A slice is a sequence of consecutive469* rows in an image. Requires a context that has been previously470* been initialized with sws_init_context().471*472* Slices have to be provided in sequential order, either in473* top-bottom or bottom-top order. If slices are provided in474* non-sequential order the behavior of the function is undefined.475*476* @param c the scaling context previously created with477* sws_getContext()478* @param srcSlice the array containing the pointers to the planes of479* the source slice480* @param srcStride the array containing the strides for each plane of481* the source image482* @param srcSliceY the position in the source image of the slice to483* process, that is the number (counted starting from484* zero) in the image of the first row of the slice485* @param srcSliceH the height of the source slice, that is the number486* of rows in the slice487* @param dst the array containing the pointers to the planes of488* the destination image489* @param dstStride the array containing the strides for each plane of490* the destination image491* @return the height of the output slice492*/493int sws_scale(SwsContext *c, const uint8_t *const srcSlice[],494const int srcStride[], int srcSliceY, int srcSliceH,495uint8_t *const dst[], const int dstStride[]);496497/**498* Initialize the scaling process for a given pair of source/destination frames.499* Must be called before any calls to sws_send_slice() and sws_receive_slice().500* Requires a context that has been previously been initialized with501* sws_init_context().502*503* This function will retain references to src and dst, so they must both use504* refcounted buffers (if allocated by the caller, in case of dst).505*506* @param c The scaling context507* @param dst The destination frame.508*509* The data buffers may either be already allocated by the caller or510* left clear, in which case they will be allocated by the scaler.511* The latter may have performance advantages - e.g. in certain cases512* some output planes may be references to input planes, rather than513* copies.514*515* Output data will be written into this frame in successful516* sws_receive_slice() calls.517* @param src The source frame. The data buffers must be allocated, but the518* frame data does not have to be ready at this point. Data519* availability is then signalled by sws_send_slice().520* @return 0 on success, a negative AVERROR code on failure521*522* @see sws_frame_end()523*/524int sws_frame_start(SwsContext *c, AVFrame *dst, const AVFrame *src);525526/**527* Finish the scaling process for a pair of source/destination frames previously528* submitted with sws_frame_start(). Must be called after all sws_send_slice()529* and sws_receive_slice() calls are done, before any new sws_frame_start()530* calls.531*532* @param c The scaling context533*/534void sws_frame_end(SwsContext *c);535536/**537* Indicate that a horizontal slice of input data is available in the source538* frame previously provided to sws_frame_start(). The slices may be provided in539* any order, but may not overlap. For vertically subsampled pixel formats, the540* slices must be aligned according to subsampling.541*542* @param c The scaling context543* @param slice_start first row of the slice544* @param slice_height number of rows in the slice545*546* @return a non-negative number on success, a negative AVERROR code on failure.547*/548int sws_send_slice(SwsContext *c, unsigned int slice_start,549unsigned int slice_height);550551/**552* Request a horizontal slice of the output data to be written into the frame553* previously provided to sws_frame_start().554*555* @param c The scaling context556* @param slice_start first row of the slice; must be a multiple of557* sws_receive_slice_alignment()558* @param slice_height number of rows in the slice; must be a multiple of559* sws_receive_slice_alignment(), except for the last slice560* (i.e. when slice_start+slice_height is equal to output561* frame height)562*563* @return a non-negative number if the data was successfully written into the output564* AVERROR(EAGAIN) if more input data needs to be provided before the565* output can be produced566* another negative AVERROR code on other kinds of scaling failure567*/568int sws_receive_slice(SwsContext *c, unsigned int slice_start,569unsigned int slice_height);570571/**572* Get the alignment required for slices. Requires a context that has been573* previously been initialized with sws_init_context().574*575* @param c The scaling context576* @return alignment required for output slices requested with sws_receive_slice().577* Slice offsets and sizes passed to sws_receive_slice() must be578* multiples of the value returned from this function.579*/580unsigned int sws_receive_slice_alignment(const SwsContext *c);581582/**583* @param c the scaling context584* @param dstRange flag indicating the while-black range of the output (1=jpeg / 0=mpeg)585* @param srcRange flag indicating the while-black range of the input (1=jpeg / 0=mpeg)586* @param table the yuv2rgb coefficients describing the output yuv space, normally ff_yuv2rgb_coeffs[x]587* @param inv_table the yuv2rgb coefficients describing the input yuv space, normally ff_yuv2rgb_coeffs[x]588* @param brightness 16.16 fixed point brightness correction589* @param contrast 16.16 fixed point contrast correction590* @param saturation 16.16 fixed point saturation correction591*592* @return A negative error code on error, non negative otherwise.593* If `LIBSWSCALE_VERSION_MAJOR < 7`, returns -1 if not supported.594*/595int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4],596int srcRange, const int table[4], int dstRange,597int brightness, int contrast, int saturation);598599/**600* @return A negative error code on error, non negative otherwise.601* If `LIBSWSCALE_VERSION_MAJOR < 7`, returns -1 if not supported.602*/603int sws_getColorspaceDetails(SwsContext *c, int **inv_table,604int *srcRange, int **table, int *dstRange,605int *brightness, int *contrast, int *saturation);606607/**608* Allocate and return an uninitialized vector with length coefficients.609*/610SwsVector *sws_allocVec(int length);611612/**613* Return a normalized Gaussian curve used to filter stuff614* quality = 3 is high quality, lower is lower quality.615*/616SwsVector *sws_getGaussianVec(double variance, double quality);617618/**619* Scale all the coefficients of a by the scalar value.620*/621void sws_scaleVec(SwsVector *a, double scalar);622623/**624* Scale all the coefficients of a so that their sum equals height.625*/626void sws_normalizeVec(SwsVector *a, double height);627628void sws_freeVec(SwsVector *a);629630SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,631float lumaSharpen, float chromaSharpen,632float chromaHShift, float chromaVShift,633int verbose);634void sws_freeFilter(SwsFilter *filter);635636/**637* Check if context can be reused, otherwise reallocate a new one.638*639* If context is NULL, just calls sws_getContext() to get a new640* context. Otherwise, checks if the parameters are the ones already641* saved in context. If that is the case, returns the current642* context. Otherwise, frees context and gets a new context with643* the new parameters.644*645* Be warned that srcFilter and dstFilter are not checked, they646* are assumed to remain the same.647*/648SwsContext *sws_getCachedContext(SwsContext *context, int srcW, int srcH,649enum AVPixelFormat srcFormat, int dstW, int dstH,650enum AVPixelFormat dstFormat, int flags,651SwsFilter *srcFilter, SwsFilter *dstFilter,652const double *param);653654/**655* Convert an 8-bit paletted frame into a frame with a color depth of 32 bits.656*657* The output frame will have the same packed format as the palette.658*659* @param src source frame buffer660* @param dst destination frame buffer661* @param num_pixels number of pixels to convert662* @param palette array with [256] entries, which must match color arrangement (RGB or BGR) of src663*/664void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette);665666/**667* Convert an 8-bit paletted frame into a frame with a color depth of 24 bits.668*669* With the palette format "ABCD", the destination frame ends up with the format "ABC".670*671* @param src source frame buffer672* @param dst destination frame buffer673* @param num_pixels number of pixels to convert674* @param palette array with [256] entries, which must match color arrangement (RGB or BGR) of src675*/676void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette);677678/**679* @}680*/681682#endif /* SWSCALE_SWSCALE_H */683684685