FFmpeg  5.0
swscale.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2001-2011 Michael Niedermayer <michaelni@gmx.at>
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_SWSCALE_H
22 #define SWSCALE_SWSCALE_H
23 
24 /**
25  * @file
26  * @ingroup libsws
27  * external API header
28  */
29 
30 #include <stdint.h>
31 
32 #include "libavutil/avutil.h"
33 #include "libavutil/frame.h"
34 #include "libavutil/log.h"
35 #include "libavutil/pixfmt.h"
36 #include "version.h"
37 
38 /**
39  * @defgroup libsws libswscale
40  * Color conversion and scaling library.
41  *
42  * @{
43  *
44  * Return the LIBSWSCALE_VERSION_INT constant.
45  */
46 unsigned swscale_version(void);
47 
48 /**
49  * Return the libswscale build-time configuration.
50  */
51 const char *swscale_configuration(void);
52 
53 /**
54  * Return the libswscale license.
55  */
56 const char *swscale_license(void);
57 
58 /* values for the flags, the stuff on the command line is different */
59 #define SWS_FAST_BILINEAR 1
60 #define SWS_BILINEAR 2
61 #define SWS_BICUBIC 4
62 #define SWS_X 8
63 #define SWS_POINT 0x10
64 #define SWS_AREA 0x20
65 #define SWS_BICUBLIN 0x40
66 #define SWS_GAUSS 0x80
67 #define SWS_SINC 0x100
68 #define SWS_LANCZOS 0x200
69 #define SWS_SPLINE 0x400
70 
71 #define SWS_SRC_V_CHR_DROP_MASK 0x30000
72 #define SWS_SRC_V_CHR_DROP_SHIFT 16
73 
74 #define SWS_PARAM_DEFAULT 123456
75 
76 #define SWS_PRINT_INFO 0x1000
77 
78 //the following 3 flags are not completely implemented
79 //internal chrominance subsampling info
80 #define SWS_FULL_CHR_H_INT 0x2000
81 //input subsampling info
82 #define SWS_FULL_CHR_H_INP 0x4000
83 #define SWS_DIRECT_BGR 0x8000
84 #define SWS_ACCURATE_RND 0x40000
85 #define SWS_BITEXACT 0x80000
86 #define SWS_ERROR_DIFFUSION 0x800000
87 
88 #define SWS_MAX_REDUCE_CUTOFF 0.002
89 
90 #define SWS_CS_ITU709 1
91 #define SWS_CS_FCC 4
92 #define SWS_CS_ITU601 5
93 #define SWS_CS_ITU624 5
94 #define SWS_CS_SMPTE170M 5
95 #define SWS_CS_SMPTE240M 7
96 #define SWS_CS_DEFAULT 5
97 #define SWS_CS_BT2020 9
98 
99 /**
100  * Return a pointer to yuv<->rgb coefficients for the given colorspace
101  * suitable for sws_setColorspaceDetails().
102  *
103  * @param colorspace One of the SWS_CS_* macros. If invalid,
104  * SWS_CS_DEFAULT is used.
105  */
106 const int *sws_getCoefficients(int colorspace);
107 
108 // when used for filters they must have an odd number of elements
109 // coeffs cannot be shared between vectors
110 typedef struct SwsVector {
111  double *coeff; ///< pointer to the list of coefficients
112  int length; ///< number of coefficients in the vector
113 } SwsVector;
114 
115 // vectors can be shared
116 typedef struct SwsFilter {
121 } SwsFilter;
122 
123 struct SwsContext;
124 
125 /**
126  * Return a positive value if pix_fmt is a supported input format, 0
127  * otherwise.
128  */
130 
131 /**
132  * Return a positive value if pix_fmt is a supported output format, 0
133  * otherwise.
134  */
136 
137 /**
138  * @param[in] pix_fmt the pixel format
139  * @return a positive value if an endianness conversion for pix_fmt is
140  * supported, 0 otherwise.
141  */
143 
144 /**
145  * Allocate an empty SwsContext. This must be filled and passed to
146  * sws_init_context(). For filling see AVOptions, options.c and
147  * sws_setColorspaceDetails().
148  */
149 struct SwsContext *sws_alloc_context(void);
150 
151 /**
152  * Initialize the swscaler context sws_context.
153  *
154  * @return zero or positive value on success, a negative value on
155  * error
156  */
158 int sws_init_context(struct SwsContext *sws_context, SwsFilter *srcFilter, SwsFilter *dstFilter);
159 
160 /**
161  * Free the swscaler context swsContext.
162  * If swsContext is NULL, then does nothing.
163  */
164 void sws_freeContext(struct SwsContext *swsContext);
165 
166 /**
167  * Allocate and return an SwsContext. You need it to perform
168  * scaling/conversion operations using sws_scale().
169  *
170  * @param srcW the width of the source image
171  * @param srcH the height of the source image
172  * @param srcFormat the source image format
173  * @param dstW the width of the destination image
174  * @param dstH the height of the destination image
175  * @param dstFormat the destination image format
176  * @param flags specify which algorithm and options to use for rescaling
177  * @param param extra parameters to tune the used scaler
178  * For SWS_BICUBIC param[0] and [1] tune the shape of the basis
179  * function, param[0] tunes f(1) and param[1] f´(1)
180  * For SWS_GAUSS param[0] tunes the exponent and thus cutoff
181  * frequency
182  * For SWS_LANCZOS param[0] tunes the width of the window function
183  * @return a pointer to an allocated context, or NULL in case of error
184  * @note this function is to be removed after a saner alternative is
185  * written
186  */
187 struct SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
188  int dstW, int dstH, enum AVPixelFormat dstFormat,
189  int flags, SwsFilter *srcFilter,
190  SwsFilter *dstFilter, const double *param);
191 
192 /**
193  * Scale the image slice in srcSlice and put the resulting scaled
194  * slice in the image in dst. A slice is a sequence of consecutive
195  * rows in an image.
196  *
197  * Slices have to be provided in sequential order, either in
198  * top-bottom or bottom-top order. If slices are provided in
199  * non-sequential order the behavior of the function is undefined.
200  *
201  * @param c the scaling context previously created with
202  * sws_getContext()
203  * @param srcSlice the array containing the pointers to the planes of
204  * the source slice
205  * @param srcStride the array containing the strides for each plane of
206  * the source image
207  * @param srcSliceY the position in the source image of the slice to
208  * process, that is the number (counted starting from
209  * zero) in the image of the first row of the slice
210  * @param srcSliceH the height of the source slice, that is the number
211  * of rows in the slice
212  * @param dst the array containing the pointers to the planes of
213  * the destination image
214  * @param dstStride the array containing the strides for each plane of
215  * the destination image
216  * @return the height of the output slice
217  */
218 int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[],
219  const int srcStride[], int srcSliceY, int srcSliceH,
220  uint8_t *const dst[], const int dstStride[]);
221 
222 /**
223  * Scale source data from src and write the output to dst.
224  *
225  * This is merely a convenience wrapper around
226  * - sws_frame_start()
227  * - sws_send_slice(0, src->height)
228  * - sws_receive_slice(0, dst->height)
229  * - sws_frame_end()
230  *
231  * @param dst The destination frame. See documentation for sws_frame_start() for
232  * more details.
233  * @param src The source frame.
234  *
235  * @return 0 on success, a negative AVERROR code on failure
236  */
237 int sws_scale_frame(struct SwsContext *c, AVFrame *dst, const AVFrame *src);
238 
239 /**
240  * Initialize the scaling process for a given pair of source/destination frames.
241  * Must be called before any calls to sws_send_slice() and sws_receive_slice().
242  *
243  * This function will retain references to src and dst, so they must both use
244  * refcounted buffers (if allocated by the caller, in case of dst).
245  *
246  * @param dst The destination frame.
247  *
248  * The data buffers may either be already allocated by the caller or
249  * left clear, in which case they will be allocated by the scaler.
250  * The latter may have performance advantages - e.g. in certain cases
251  * some output planes may be references to input planes, rather than
252  * copies.
253  *
254  * Output data will be written into this frame in successful
255  * sws_receive_slice() calls.
256  * @param src The source frame. The data buffers must be allocated, but the
257  * frame data does not have to be ready at this point. Data
258  * availability is then signalled by sws_send_slice().
259  * @return 0 on success, a negative AVERROR code on failure
260  *
261  * @see sws_frame_end()
262  */
263 int sws_frame_start(struct SwsContext *c, AVFrame *dst, const AVFrame *src);
264 
265 /**
266  * Finish the scaling process for a pair of source/destination frames previously
267  * submitted with sws_frame_start(). Must be called after all sws_send_slice()
268  * and sws_receive_slice() calls are done, before any new sws_frame_start()
269  * calls.
270  */
271 void sws_frame_end(struct SwsContext *c);
272 
273 /**
274  * Indicate that a horizontal slice of input data is available in the source
275  * frame previously provided to sws_frame_start(). The slices may be provided in
276  * any order, but may not overlap. For vertically subsampled pixel formats, the
277  * slices must be aligned according to subsampling.
278  *
279  * @param slice_start first row of the slice
280  * @param slice_height number of rows in the slice
281  *
282  * @return a non-negative number on success, a negative AVERROR code on failure.
283  */
284 int sws_send_slice(struct SwsContext *c, unsigned int slice_start,
285  unsigned int slice_height);
286 
287 /**
288  * Request a horizontal slice of the output data to be written into the frame
289  * previously provided to sws_frame_start().
290  *
291  * @param slice_start first row of the slice; must be a multiple of
292  * sws_receive_slice_alignment()
293  * @param slice_height number of rows in the slice; must be a multiple of
294  * sws_receive_slice_alignment(), except for the last slice
295  * (i.e. when slice_start+slice_height is equal to output
296  * frame height)
297  *
298  * @return a non-negative number if the data was successfully written into the output
299  * AVERROR(EAGAIN) if more input data needs to be provided before the
300  * output can be produced
301  * another negative AVERROR code on other kinds of scaling failure
302  */
303 int sws_receive_slice(struct SwsContext *c, unsigned int slice_start,
304  unsigned int slice_height);
305 
306 /**
307  * @return alignment required for output slices requested with sws_receive_slice().
308  * Slice offsets and sizes passed to sws_receive_slice() must be
309  * multiples of the value returned from this function.
310  */
311 unsigned int sws_receive_slice_alignment(const struct SwsContext *c);
312 
313 /**
314  * @param dstRange flag indicating the while-black range of the output (1=jpeg / 0=mpeg)
315  * @param srcRange flag indicating the while-black range of the input (1=jpeg / 0=mpeg)
316  * @param table the yuv2rgb coefficients describing the output yuv space, normally ff_yuv2rgb_coeffs[x]
317  * @param inv_table the yuv2rgb coefficients describing the input yuv space, normally ff_yuv2rgb_coeffs[x]
318  * @param brightness 16.16 fixed point brightness correction
319  * @param contrast 16.16 fixed point contrast correction
320  * @param saturation 16.16 fixed point saturation correction
321 #if LIBSWSCALE_VERSION_MAJOR > 6
322  * @return negative error code on error, non negative otherwise
323 #else
324  * @return -1 if not supported
325 #endif
326  */
327 int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
328  int srcRange, const int table[4], int dstRange,
329  int brightness, int contrast, int saturation);
330 
331 /**
332 #if LIBSWSCALE_VERSION_MAJOR > 6
333  * @return negative error code on error, non negative otherwise
334 #else
335  * @return -1 if not supported
336 #endif
337  */
338 int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
339  int *srcRange, int **table, int *dstRange,
340  int *brightness, int *contrast, int *saturation);
341 
342 /**
343  * Allocate and return an uninitialized vector with length coefficients.
344  */
345 SwsVector *sws_allocVec(int length);
346 
347 /**
348  * Return a normalized Gaussian curve used to filter stuff
349  * quality = 3 is high quality, lower is lower quality.
350  */
351 SwsVector *sws_getGaussianVec(double variance, double quality);
352 
353 /**
354  * Scale all the coefficients of a by the scalar value.
355  */
356 void sws_scaleVec(SwsVector *a, double scalar);
357 
358 /**
359  * Scale all the coefficients of a so that their sum equals height.
360  */
362 
364 
365 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
366  float lumaSharpen, float chromaSharpen,
367  float chromaHShift, float chromaVShift,
368  int verbose);
369 void sws_freeFilter(SwsFilter *filter);
370 
371 /**
372  * Check if context can be reused, otherwise reallocate a new one.
373  *
374  * If context is NULL, just calls sws_getContext() to get a new
375  * context. Otherwise, checks if the parameters are the ones already
376  * saved in context. If that is the case, returns the current
377  * context. Otherwise, frees context and gets a new context with
378  * the new parameters.
379  *
380  * Be warned that srcFilter and dstFilter are not checked, they
381  * are assumed to remain the same.
382  */
383 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
384  int srcW, int srcH, enum AVPixelFormat srcFormat,
385  int dstW, int dstH, enum AVPixelFormat dstFormat,
386  int flags, SwsFilter *srcFilter,
387  SwsFilter *dstFilter, const double *param);
388 
389 /**
390  * Convert an 8-bit paletted frame into a frame with a color depth of 32 bits.
391  *
392  * The output frame will have the same packed format as the palette.
393  *
394  * @param src source frame buffer
395  * @param dst destination frame buffer
396  * @param num_pixels number of pixels to convert
397  * @param palette array with [256] entries, which must match color arrangement (RGB or BGR) of src
398  */
399 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette);
400 
401 /**
402  * Convert an 8-bit paletted frame into a frame with a color depth of 24 bits.
403  *
404  * With the palette format "ABCD", the destination frame ends up with the format "ABC".
405  *
406  * @param src source frame buffer
407  * @param dst destination frame buffer
408  * @param num_pixels number of pixels to convert
409  * @param palette array with [256] entries, which must match color arrangement (RGB or BGR) of src
410  */
411 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette);
412 
413 /**
414  * Get the AVClass for swsContext. It can be used in combination with
415  * AV_OPT_SEARCH_FAKE_OBJ for examining options.
416  *
417  * @see av_opt_find().
418  */
419 const AVClass *sws_get_class(void);
420 
421 /**
422  * @}
423  */
424 
425 #endif /* SWSCALE_SWSCALE_H */
#define av_warn_unused_result
Definition: attributes.h:62
Convenience header that includes libavutil's core.
static enum AVPixelFormat pix_fmt
static int height
reference-counted frame API
int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation)
#if LIBSWSCALE_VERSION_MAJOR > 6
const AVClass * sws_get_class(void)
Get the AVClass for swsContext.
void sws_freeFilter(SwsFilter *filter)
int sws_send_slice(struct SwsContext *c, unsigned int slice_start, unsigned int slice_height)
Indicate that a horizontal slice of input data is available in the source frame previously provided t...
int sws_scale_frame(struct SwsContext *c, AVFrame *dst, const AVFrame *src)
Scale source data from src and write the output to dst.
av_warn_unused_result int sws_init_context(struct SwsContext *sws_context, SwsFilter *srcFilter, SwsFilter *dstFilter)
Initialize the swscaler context sws_context.
unsigned int sws_receive_slice_alignment(const struct SwsContext *c)
SwsVector * sws_getGaussianVec(double variance, double quality)
Return a normalized Gaussian curve used to filter stuff quality = 3 is high quality,...
void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
Convert an 8-bit paletted frame into a frame with a color depth of 32 bits.
void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
Convert an 8-bit paletted frame into a frame with a color depth of 24 bits.
void sws_frame_end(struct SwsContext *c)
Finish the scaling process for a pair of source/destination frames previously submitted with sws_fram...
int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation)
void sws_freeVec(SwsVector *a)
int sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt)
int sws_isSupportedOutput(enum AVPixelFormat pix_fmt)
Return a positive value if pix_fmt is a supported output format, 0 otherwise.
SwsVector * sws_allocVec(int length)
Allocate and return an uninitialized vector with length coefficients.
int sws_receive_slice(struct SwsContext *c, unsigned int slice_start, unsigned int slice_height)
Request a horizontal slice of the output data to be written into the frame previously provided to sws...
const char * swscale_license(void)
Return the libswscale license.
const char * swscale_configuration(void)
Return the libswscale build-time configuration.
void sws_normalizeVec(SwsVector *a, double height)
Scale all the coefficients of a so that their sum equals height.
int sws_isSupportedInput(enum AVPixelFormat pix_fmt)
Return a positive value if pix_fmt is a supported input format, 0 otherwise.
SwsFilter * sws_getDefaultFilter(float lumaGBlur, float chromaGBlur, float lumaSharpen, float chromaSharpen, float chromaHShift, float chromaVShift, int verbose)
struct SwsContext * sws_alloc_context(void)
Allocate an empty SwsContext.
int sws_frame_start(struct SwsContext *c, AVFrame *dst, const AVFrame *src)
Initialize the scaling process for a given pair of source/destination frames.
const int * sws_getCoefficients(int colorspace)
Return a pointer to yuv<->rgb coefficients for the given colorspace suitable for sws_setColorspaceDet...
unsigned swscale_version(void)
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
struct SwsContext * sws_getCachedContext(struct SwsContext *context, int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
Check if context can be reused, otherwise reallocate a new one.
int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
Scale the image slice in srcSlice and put the resulting scaled slice in the image in dst.
struct SwsContext * sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
Allocate and return an SwsContext.
void sws_scaleVec(SwsVector *a, double scalar)
Scale all the coefficients of a by the scalar value.
Libavutil version macros.
pixel format definitions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
Describe the class of an AVClass context structure.
Definition: log.h:66
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
SwsVector * chrV
Definition: swscale.h:120
SwsVector * lumH
Definition: swscale.h:117
SwsVector * lumV
Definition: swscale.h:118
SwsVector * chrH
Definition: swscale.h:119
double * coeff
pointer to the list of coefficients
Definition: swscale.h:111
int length
number of coefficients in the vector
Definition: swscale.h:112