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