swscale.h 19 KB

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