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