imgutils.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef AVUTIL_IMGUTILS_H
  19. #define AVUTIL_IMGUTILS_H
  20. /**
  21. * @file
  22. * misc image utilities
  23. *
  24. * @addtogroup lavu_picture
  25. * @{
  26. */
  27. #include <stddef.h>
  28. #include <stdint.h>
  29. #include "pixdesc.h"
  30. #include "pixfmt.h"
  31. #include "rational.h"
  32. /**
  33. * Compute the max pixel step for each plane of an image with a
  34. * format described by pixdesc.
  35. *
  36. * The pixel step is the distance in bytes between the first byte of
  37. * the group of bytes which describe a pixel component and the first
  38. * byte of the successive group in the same plane for the same
  39. * component.
  40. *
  41. * @param max_pixsteps an array which is filled with the max pixel step
  42. * for each plane. Since a plane may contain different pixel
  43. * components, the computed max_pixsteps[plane] is relative to the
  44. * component in the plane with the max pixel step.
  45. * @param max_pixstep_comps an array which is filled with the component
  46. * for each plane which has the max pixel step. May be NULL.
  47. * @param pixdesc the AVPixFmtDescriptor for the image, describing its format
  48. */
  49. void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
  50. const AVPixFmtDescriptor *pixdesc);
  51. /**
  52. * Compute the size of an image line with format pix_fmt and width
  53. * width for the plane plane.
  54. *
  55. * @return the computed size in bytes
  56. */
  57. int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane);
  58. /**
  59. * Fill plane linesizes for an image with pixel format pix_fmt and
  60. * width width.
  61. *
  62. * @param linesizes array to be filled with the linesize for each plane
  63. * @param pix_fmt the AVPixelFormat of the image
  64. * @param width width of the image in pixels
  65. * @return >= 0 in case of success, a negative error code otherwise
  66. */
  67. int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width);
  68. /**
  69. * Fill plane sizes for an image with pixel format pix_fmt and height height.
  70. *
  71. * @param size the array to be filled with the size of each image plane
  72. * @param pix_fmt the AVPixelFormat of the image
  73. * @param height height of the image in pixels
  74. * @param linesizes the array containing the linesize for each
  75. * plane, should be filled by av_image_fill_linesizes()
  76. * @return >= 0 in case of success, a negative error code otherwise
  77. *
  78. * @note The linesize parameters have the type ptrdiff_t here, while they are
  79. * int for av_image_fill_linesizes().
  80. */
  81. int av_image_fill_plane_sizes(size_t size[4], enum AVPixelFormat pix_fmt,
  82. int height, const ptrdiff_t linesizes[4]);
  83. /**
  84. * Fill plane data pointers for an image with pixel format pix_fmt and
  85. * height height.
  86. *
  87. * @param data pointers array to be filled with the pointer for each image plane
  88. * @param pix_fmt the AVPixelFormat of the image
  89. * @param height height of the image in pixels
  90. * @param ptr the pointer to a buffer which will contain the image
  91. * @param linesizes the array containing the linesize for each
  92. * plane, should be filled by av_image_fill_linesizes()
  93. * @return the size in bytes required for the image buffer, a negative
  94. * error code in case of failure
  95. */
  96. int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height,
  97. uint8_t *ptr, const int linesizes[4]);
  98. /**
  99. * Allocate an image with size w and h and pixel format pix_fmt, and
  100. * fill pointers and linesizes accordingly.
  101. * The allocated image buffer has to be freed by using
  102. * av_freep(&pointers[0]).
  103. *
  104. * @param pointers array to be filled with the pointer for each image plane
  105. * @param linesizes the array filled with the linesize for each plane
  106. * @param w width of the image in pixels
  107. * @param h height of the image in pixels
  108. * @param pix_fmt the AVPixelFormat of the image
  109. * @param align the value to use for buffer size alignment
  110. * @return the size in bytes required for the image buffer, a negative
  111. * error code in case of failure
  112. */
  113. int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
  114. int w, int h, enum AVPixelFormat pix_fmt, int align);
  115. /**
  116. * Copy image plane from src to dst.
  117. * That is, copy "height" number of lines of "bytewidth" bytes each.
  118. * The first byte of each successive line is separated by *_linesize
  119. * bytes.
  120. *
  121. * bytewidth must be contained by both absolute values of dst_linesize
  122. * and src_linesize, otherwise the function behavior is undefined.
  123. *
  124. * @param dst destination plane to copy to
  125. * @param dst_linesize linesize for the image plane in dst
  126. * @param src source plane to copy from
  127. * @param src_linesize linesize for the image plane in src
  128. * @param height height (number of lines) of the plane
  129. */
  130. void av_image_copy_plane(uint8_t *dst, int dst_linesize,
  131. const uint8_t *src, int src_linesize,
  132. int bytewidth, int height);
  133. /**
  134. * Copy image data located in uncacheable (e.g. GPU mapped) memory. Where
  135. * available, this function will use special functionality for reading from such
  136. * memory, which may result in greatly improved performance compared to plain
  137. * av_image_copy_plane().
  138. *
  139. * bytewidth must be contained by both absolute values of dst_linesize
  140. * and src_linesize, otherwise the function behavior is undefined.
  141. *
  142. * @note The linesize parameters have the type ptrdiff_t here, while they are
  143. * int for av_image_copy_plane().
  144. * @note On x86, the linesizes currently need to be aligned to the cacheline
  145. * size (i.e. 64) to get improved performance.
  146. */
  147. void av_image_copy_plane_uc_from(uint8_t *dst, ptrdiff_t dst_linesize,
  148. const uint8_t *src, ptrdiff_t src_linesize,
  149. ptrdiff_t bytewidth, int height);
  150. /**
  151. * Copy image in src_data to dst_data.
  152. *
  153. * @param dst_data destination image data buffer to copy to
  154. * @param dst_linesizes linesizes for the image in dst_data
  155. * @param src_data source image data buffer to copy from
  156. * @param src_linesizes linesizes for the image in src_data
  157. * @param pix_fmt the AVPixelFormat of the image
  158. * @param width width of the image in pixels
  159. * @param height height of the image in pixels
  160. */
  161. void av_image_copy(uint8_t * const dst_data[4], const int dst_linesizes[4],
  162. const uint8_t * const src_data[4], const int src_linesizes[4],
  163. enum AVPixelFormat pix_fmt, int width, int height);
  164. /**
  165. * Wrapper around av_image_copy() to workaround the limitation
  166. * that the conversion from uint8_t * const * to const uint8_t * const *
  167. * is not performed automatically in C.
  168. * @see av_image_copy()
  169. */
  170. static inline
  171. void av_image_copy2(uint8_t * const dst_data[4], const int dst_linesizes[4],
  172. uint8_t * const src_data[4], const int src_linesizes[4],
  173. enum AVPixelFormat pix_fmt, int width, int height)
  174. {
  175. av_image_copy(dst_data, dst_linesizes,
  176. (const uint8_t * const *)src_data, src_linesizes,
  177. pix_fmt, width, height);
  178. }
  179. /**
  180. * Copy image data located in uncacheable (e.g. GPU mapped) memory. Where
  181. * available, this function will use special functionality for reading from such
  182. * memory, which may result in greatly improved performance compared to plain
  183. * av_image_copy().
  184. *
  185. * The data pointers and the linesizes must be aligned to the maximum required
  186. * by the CPU architecture.
  187. *
  188. * @note The linesize parameters have the type ptrdiff_t here, while they are
  189. * int for av_image_copy().
  190. * @note On x86, the linesizes currently need to be aligned to the cacheline
  191. * size (i.e. 64) to get improved performance.
  192. */
  193. void av_image_copy_uc_from(uint8_t * const dst_data[4], const ptrdiff_t dst_linesizes[4],
  194. const uint8_t * const src_data[4], const ptrdiff_t src_linesizes[4],
  195. enum AVPixelFormat pix_fmt, int width, int height);
  196. /**
  197. * Setup the data pointers and linesizes based on the specified image
  198. * parameters and the provided array.
  199. *
  200. * The fields of the given image are filled in by using the src
  201. * address which points to the image data buffer. Depending on the
  202. * specified pixel format, one or multiple image data pointers and
  203. * line sizes will be set. If a planar format is specified, several
  204. * pointers will be set pointing to the different picture planes and
  205. * the line sizes of the different planes will be stored in the
  206. * lines_sizes array. Call with src == NULL to get the required
  207. * size for the src buffer.
  208. *
  209. * To allocate the buffer and fill in the dst_data and dst_linesize in
  210. * one call, use av_image_alloc().
  211. *
  212. * @param dst_data data pointers to be filled in
  213. * @param dst_linesize linesizes for the image in dst_data to be filled in
  214. * @param src buffer which will contain or contains the actual image data, can be NULL
  215. * @param pix_fmt the pixel format of the image
  216. * @param width the width of the image in pixels
  217. * @param height the height of the image in pixels
  218. * @param align the value used in src for linesize alignment
  219. * @return the size in bytes required for src, a negative error code
  220. * in case of failure
  221. */
  222. int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
  223. const uint8_t *src,
  224. enum AVPixelFormat pix_fmt, int width, int height, int align);
  225. /**
  226. * Return the size in bytes of the amount of data required to store an
  227. * image with the given parameters.
  228. *
  229. * @param pix_fmt the pixel format of the image
  230. * @param width the width of the image in pixels
  231. * @param height the height of the image in pixels
  232. * @param align the assumed linesize alignment
  233. * @return the buffer size in bytes, a negative error code in case of failure
  234. */
  235. int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align);
  236. /**
  237. * Copy image data from an image into a buffer.
  238. *
  239. * av_image_get_buffer_size() can be used to compute the required size
  240. * for the buffer to fill.
  241. *
  242. * @param dst a buffer into which picture data will be copied
  243. * @param dst_size the size in bytes of dst
  244. * @param src_data pointers containing the source image data
  245. * @param src_linesize linesizes for the image in src_data
  246. * @param pix_fmt the pixel format of the source image
  247. * @param width the width of the source image in pixels
  248. * @param height the height of the source image in pixels
  249. * @param align the assumed linesize alignment for dst
  250. * @return the number of bytes written to dst, or a negative value
  251. * (error code) on error
  252. */
  253. int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
  254. const uint8_t * const src_data[4], const int src_linesize[4],
  255. enum AVPixelFormat pix_fmt, int width, int height, int align);
  256. /**
  257. * Check if the given dimension of an image is valid, meaning that all
  258. * bytes of the image can be addressed with a signed int.
  259. *
  260. * @param w the width of the picture
  261. * @param h the height of the picture
  262. * @param log_offset the offset to sum to the log level for logging with log_ctx
  263. * @param log_ctx the parent logging context, it may be NULL
  264. * @return >= 0 if valid, a negative error code otherwise
  265. */
  266. int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx);
  267. /**
  268. * Check if the given dimension of an image is valid, meaning that all
  269. * bytes of a plane of an image with the specified pix_fmt can be addressed
  270. * with a signed int.
  271. *
  272. * @param w the width of the picture
  273. * @param h the height of the picture
  274. * @param max_pixels the maximum number of pixels the user wants to accept
  275. * @param pix_fmt the pixel format, can be AV_PIX_FMT_NONE if unknown.
  276. * @param log_offset the offset to sum to the log level for logging with log_ctx
  277. * @param log_ctx the parent logging context, it may be NULL
  278. * @return >= 0 if valid, a negative error code otherwise
  279. */
  280. int av_image_check_size2(unsigned int w, unsigned int h, int64_t max_pixels, enum AVPixelFormat pix_fmt, int log_offset, void *log_ctx);
  281. /**
  282. * Check if the given sample aspect ratio of an image is valid.
  283. *
  284. * It is considered invalid if the denominator is 0 or if applying the ratio
  285. * to the image size would make the smaller dimension less than 1. If the
  286. * sar numerator is 0, it is considered unknown and will return as valid.
  287. *
  288. * @param w width of the image
  289. * @param h height of the image
  290. * @param sar sample aspect ratio of the image
  291. * @return 0 if valid, a negative AVERROR code otherwise
  292. */
  293. int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar);
  294. /**
  295. * Overwrite the image data with black. This is suitable for filling a
  296. * sub-rectangle of an image, meaning the padding between the right most pixel
  297. * and the left most pixel on the next line will not be overwritten. For some
  298. * formats, the image size might be rounded up due to inherent alignment.
  299. *
  300. * If the pixel format has alpha, the alpha is cleared to opaque.
  301. *
  302. * This can return an error if the pixel format is not supported. Normally, all
  303. * non-hwaccel pixel formats should be supported.
  304. *
  305. * Passing NULL for dst_data is allowed. Then the function returns whether the
  306. * operation would have succeeded. (It can return an error if the pix_fmt is
  307. * not supported.)
  308. *
  309. * @param dst_data data pointers to destination image
  310. * @param dst_linesize linesizes for the destination image
  311. * @param pix_fmt the pixel format of the image
  312. * @param range the color range of the image (important for colorspaces such as YUV)
  313. * @param width the width of the image in pixels
  314. * @param height the height of the image in pixels
  315. * @return 0 if the image data was cleared, a negative AVERROR code otherwise
  316. */
  317. int av_image_fill_black(uint8_t * const dst_data[4], const ptrdiff_t dst_linesize[4],
  318. enum AVPixelFormat pix_fmt, enum AVColorRange range,
  319. int width, int height);
  320. /**
  321. * Overwrite the image data with a color. This is suitable for filling a
  322. * sub-rectangle of an image, meaning the padding between the right most pixel
  323. * and the left most pixel on the next line will not be overwritten. For some
  324. * formats, the image size might be rounded up due to inherent alignment.
  325. *
  326. * If the pixel format has alpha, it is also replaced. Color component values
  327. * are interpreted as native integers (or intfloats) regardless of actual pixel
  328. * format endianness.
  329. *
  330. * This can return an error if the pixel format is not supported. Normally, all
  331. * non-hwaccel pixel formats should be supported.
  332. *
  333. * Passing NULL for dst_data is allowed. Then the function returns whether the
  334. * operation would have succeeded. (It can return an error if the pix_fmt is
  335. * not supported.)
  336. *
  337. * @param dst_data data pointers to destination image
  338. * @param dst_linesize linesizes for the destination image
  339. * @param pix_fmt the pixel format of the image
  340. * @param color the color components to be used for the fill
  341. * @param width the width of the image in pixels
  342. * @param height the height of the image in pixels
  343. * @param flags currently unused
  344. * @return 0 if the image data was filled, a negative AVERROR code otherwise
  345. */
  346. int av_image_fill_color(uint8_t * const dst_data[4], const ptrdiff_t dst_linesize[4],
  347. enum AVPixelFormat pix_fmt, const uint32_t color[4],
  348. int width, int height, int flags);
  349. /**
  350. * @}
  351. */
  352. #endif /* AVUTIL_IMGUTILS_H */