FFmpeg
imgutils.h
Go to the documentation of this file.
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 
19 #ifndef AVUTIL_IMGUTILS_H
20 #define AVUTIL_IMGUTILS_H
21 
22 /**
23  * @file
24  * misc image utilities
25  *
26  * @addtogroup lavu_picture
27  * @{
28  */
29 
30 #include "avutil.h"
31 #include "pixdesc.h"
32 #include "rational.h"
33 
34 /**
35  * Compute the max pixel step for each plane of an image with a
36  * format described by pixdesc.
37  *
38  * The pixel step is the distance in bytes between the first byte of
39  * the group of bytes which describe a pixel component and the first
40  * byte of the successive group in the same plane for the same
41  * component.
42  *
43  * @param max_pixsteps an array which is filled with the max pixel step
44  * for each plane. Since a plane may contain different pixel
45  * components, the computed max_pixsteps[plane] is relative to the
46  * component in the plane with the max pixel step.
47  * @param max_pixstep_comps an array which is filled with the component
48  * for each plane which has the max pixel step. May be NULL.
49  */
50 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
51  const AVPixFmtDescriptor *pixdesc);
52 
53 /**
54  * Compute the size of an image line with format pix_fmt and width
55  * width for the plane plane.
56  *
57  * @return the computed size in bytes
58  */
59 int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane);
60 
61 /**
62  * Fill plane linesizes for an image with pixel format pix_fmt and
63  * width width.
64  *
65  * @param linesizes array to be filled with the linesize for each plane
66  * @return >= 0 in case of success, a negative error code otherwise
67  */
68 int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width);
69 
70 /**
71  * Fill plane sizes for an image with pixel format pix_fmt and height height.
72  *
73  * @param size the array to be filled with the size of each image plane
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  */
82  int height, const ptrdiff_t linesizes[4]);
83 
84 /**
85  * Fill plane data pointers for an image with pixel format pix_fmt and
86  * height height.
87  *
88  * @param data pointers array to be filled with the pointer for each image plane
89  * @param ptr the pointer to a buffer which will contain the image
90  * @param linesizes the array containing the linesize for each
91  * plane, should be filled by av_image_fill_linesizes()
92  * @return the size in bytes required for the image buffer, a negative
93  * error code in case of failure
94  */
95 int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height,
96  uint8_t *ptr, const int linesizes[4]);
97 
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 align the value to use for buffer size alignment
105  * @return the size in bytes required for the image buffer, a negative
106  * error code in case of failure
107  */
108 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
109  int w, int h, enum AVPixelFormat pix_fmt, int align);
110 
111 /**
112  * Copy image plane from src to dst.
113  * That is, copy "height" number of lines of "bytewidth" bytes each.
114  * The first byte of each successive line is separated by *_linesize
115  * bytes.
116  *
117  * bytewidth must be contained by both absolute values of dst_linesize
118  * and src_linesize, otherwise the function behavior is undefined.
119  *
120  * @param dst_linesize linesize for the image plane in dst
121  * @param src_linesize linesize for the image plane in src
122  */
123 void av_image_copy_plane(uint8_t *dst, int dst_linesize,
124  const uint8_t *src, int src_linesize,
125  int bytewidth, int height);
126 
127 /**
128  * Copy image data located in uncacheable (e.g. GPU mapped) memory. Where
129  * available, this function will use special functionality for reading from such
130  * memory, which may result in greatly improved performance compared to plain
131  * av_image_copy_plane().
132  *
133  * bytewidth must be contained by both absolute values of dst_linesize
134  * and src_linesize, otherwise the function behavior is undefined.
135  *
136  * @note The linesize parameters have the type ptrdiff_t here, while they are
137  * int for av_image_copy_plane().
138  * @note On x86, the linesizes currently need to be aligned to the cacheline
139  * size (i.e. 64) to get improved performance.
140  */
141 void av_image_copy_plane_uc_from(uint8_t *dst, ptrdiff_t dst_linesize,
142  const uint8_t *src, ptrdiff_t src_linesize,
143  ptrdiff_t bytewidth, int height);
144 
145 /**
146  * Copy image in src_data to dst_data.
147  *
148  * @param dst_linesizes linesizes for the image in dst_data
149  * @param src_linesizes linesizes for the image in src_data
150  */
151 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
152  const uint8_t *src_data[4], const int src_linesizes[4],
153  enum AVPixelFormat pix_fmt, int width, int height);
154 
155 /**
156  * Copy image data located in uncacheable (e.g. GPU mapped) memory. Where
157  * available, this function will use special functionality for reading from such
158  * memory, which may result in greatly improved performance compared to plain
159  * av_image_copy().
160  *
161  * The data pointers and the linesizes must be aligned to the maximum required
162  * by the CPU architecture.
163  *
164  * @note The linesize parameters have the type ptrdiff_t here, while they are
165  * int for av_image_copy().
166  * @note On x86, the linesizes currently need to be aligned to the cacheline
167  * size (i.e. 64) to get improved performance.
168  */
169 void av_image_copy_uc_from(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
170  const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
171  enum AVPixelFormat pix_fmt, int width, int height);
172 
173 /**
174  * Setup the data pointers and linesizes based on the specified image
175  * parameters and the provided array.
176  *
177  * The fields of the given image are filled in by using the src
178  * address which points to the image data buffer. Depending on the
179  * specified pixel format, one or multiple image data pointers and
180  * line sizes will be set. If a planar format is specified, several
181  * pointers will be set pointing to the different picture planes and
182  * the line sizes of the different planes will be stored in the
183  * lines_sizes array. Call with src == NULL to get the required
184  * size for the src buffer.
185  *
186  * To allocate the buffer and fill in the dst_data and dst_linesize in
187  * one call, use av_image_alloc().
188  *
189  * @param dst_data data pointers to be filled in
190  * @param dst_linesize linesizes for the image in dst_data to be filled in
191  * @param src buffer which will contain or contains the actual image data, can be NULL
192  * @param pix_fmt the pixel format of the image
193  * @param width the width of the image in pixels
194  * @param height the height of the image in pixels
195  * @param align the value used in src for linesize alignment
196  * @return the size in bytes required for src, a negative error code
197  * in case of failure
198  */
199 int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
200  const uint8_t *src,
201  enum AVPixelFormat pix_fmt, int width, int height, int align);
202 
203 /**
204  * Return the size in bytes of the amount of data required to store an
205  * image with the given parameters.
206  *
207  * @param pix_fmt the pixel format of the image
208  * @param width the width of the image in pixels
209  * @param height the height of the image in pixels
210  * @param align the assumed linesize alignment
211  * @return the buffer size in bytes, a negative error code in case of failure
212  */
213 int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align);
214 
215 /**
216  * Copy image data from an image into a buffer.
217  *
218  * av_image_get_buffer_size() can be used to compute the required size
219  * for the buffer to fill.
220  *
221  * @param dst a buffer into which picture data will be copied
222  * @param dst_size the size in bytes of dst
223  * @param src_data pointers containing the source image data
224  * @param src_linesize linesizes for the image in src_data
225  * @param pix_fmt the pixel format of the source image
226  * @param width the width of the source image in pixels
227  * @param height the height of the source image in pixels
228  * @param align the assumed linesize alignment for dst
229  * @return the number of bytes written to dst, or a negative value
230  * (error code) on error
231  */
232 int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
233  const uint8_t * const src_data[4], const int src_linesize[4],
234  enum AVPixelFormat pix_fmt, int width, int height, int align);
235 
236 /**
237  * Check if the given dimension of an image is valid, meaning that all
238  * bytes of the image can be addressed with a signed int.
239  *
240  * @param w the width of the picture
241  * @param h the height of the picture
242  * @param log_offset the offset to sum to the log level for logging with log_ctx
243  * @param log_ctx the parent logging context, it may be NULL
244  * @return >= 0 if valid, a negative error code otherwise
245  */
246 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx);
247 
248 /**
249  * Check if the given dimension of an image is valid, meaning that all
250  * bytes of a plane of an image with the specified pix_fmt can be addressed
251  * with a signed int.
252  *
253  * @param w the width of the picture
254  * @param h the height of the picture
255  * @param max_pixels the maximum number of pixels the user wants to accept
256  * @param pix_fmt the pixel format, can be AV_PIX_FMT_NONE if unknown.
257  * @param log_offset the offset to sum to the log level for logging with log_ctx
258  * @param log_ctx the parent logging context, it may be NULL
259  * @return >= 0 if valid, a negative error code otherwise
260  */
261 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);
262 
263 /**
264  * Check if the given sample aspect ratio of an image is valid.
265  *
266  * It is considered invalid if the denominator is 0 or if applying the ratio
267  * to the image size would make the smaller dimension less than 1. If the
268  * sar numerator is 0, it is considered unknown and will return as valid.
269  *
270  * @param w width of the image
271  * @param h height of the image
272  * @param sar sample aspect ratio of the image
273  * @return 0 if valid, a negative AVERROR code otherwise
274  */
275 int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar);
276 
277 /**
278  * Overwrite the image data with black. This is suitable for filling a
279  * sub-rectangle of an image, meaning the padding between the right most pixel
280  * and the left most pixel on the next line will not be overwritten. For some
281  * formats, the image size might be rounded up due to inherent alignment.
282  *
283  * If the pixel format has alpha, the alpha is cleared to opaque.
284  *
285  * This can return an error if the pixel format is not supported. Normally, all
286  * non-hwaccel pixel formats should be supported.
287  *
288  * Passing NULL for dst_data is allowed. Then the function returns whether the
289  * operation would have succeeded. (It can return an error if the pix_fmt is
290  * not supported.)
291  *
292  * @param dst_data data pointers to destination image
293  * @param dst_linesize linesizes for the destination image
294  * @param pix_fmt the pixel format of the image
295  * @param range the color range of the image (important for colorspaces such as YUV)
296  * @param width the width of the image in pixels
297  * @param height the height of the image in pixels
298  * @return 0 if the image data was cleared, a negative AVERROR code otherwise
299  */
300 int av_image_fill_black(uint8_t *dst_data[4], const ptrdiff_t dst_linesize[4],
301  enum AVPixelFormat pix_fmt, enum AVColorRange range,
302  int width, int height);
303 
304 /**
305  * @}
306  */
307 
308 
309 #endif /* AVUTIL_IMGUTILS_H */
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
rational.h
pixdesc.h
w
uint8_t w
Definition: llviddspenc.c:38
data
const char data[16]
Definition: mxf.c:143
av_image_copy_plane
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:374
av_image_fill_pointers
int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height, uint8_t *ptr, const int linesizes[4])
Fill plane data pointers for an image with pixel format pix_fmt and height height.
Definition: imgutils.c:145
av_image_check_size2
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)
Check if the given dimension of an image is valid, meaning that all bytes of a plane of an image with...
Definition: imgutils.c:289
width
#define width
av_image_fill_linesizes
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition: imgutils.c:89
pointers
Undefined Behavior In the C some operations are like signed integer dereferencing freed pointers
Definition: undefined.txt:4
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demuxing_decoding.c:41
av_image_fill_plane_sizes
int av_image_fill_plane_sizes(size_t size[4], enum AVPixelFormat pix_fmt, int height, const ptrdiff_t linesizes[4])
Fill plane sizes for an image with pixel format pix_fmt and height height.
Definition: imgutils.c:111
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
src
#define src
Definition: vp8dsp.c:255
av_image_fill_black
int av_image_fill_black(uint8_t *dst_data[4], const ptrdiff_t dst_linesize[4], enum AVPixelFormat pix_fmt, enum AVColorRange range, int width, int height)
Overwrite the image data with black.
Definition: imgutils.c:582
av_image_alloc
int av_image_alloc(uint8_t *pointers[4], int linesizes[4], int w, int h, enum AVPixelFormat pix_fmt, int align)
Allocate an image with size w and h and pixel format pix_fmt, and fill pointers and linesizes accordi...
Definition: imgutils.c:218
av_image_fill_arrays
int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4], const uint8_t *src, enum AVPixelFormat pix_fmt, int width, int height, int align)
Setup the data pointers and linesizes based on the specified image parameters and the provided array.
Definition: imgutils.c:446
size
int size
Definition: twinvq_data.h:10344
height
#define height
av_image_get_buffer_size
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters.
Definition: imgutils.c:466
av_image_get_linesize
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane.
Definition: imgutils.c:76
av_image_copy_uc_from
void av_image_copy_uc_from(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4], const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image data located in uncacheable (e.g.
Definition: imgutils.c:438
av_image_copy_plane_uc_from
void av_image_copy_plane_uc_from(uint8_t *dst, ptrdiff_t dst_linesize, const uint8_t *src, ptrdiff_t src_linesize, ptrdiff_t bytewidth, int height)
Copy image data located in uncacheable (e.g.
Definition: imgutils.c:359
av_image_copy
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:422
av_image_fill_max_pixsteps
void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4], const AVPixFmtDescriptor *pixdesc)
Compute the max pixel step for each plane of an image with a format described by pixdesc.
Definition: imgutils.c:35
avutil.h
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
av_image_copy_to_buffer
int av_image_copy_to_buffer(uint8_t *dst, int dst_size, const uint8_t *const src_data[4], const int src_linesize[4], enum AVPixelFormat pix_fmt, int width, int height, int align)
Copy image data from an image into a buffer.
Definition: imgutils.c:501
h
h
Definition: vp9dsp_template.c:2038
av_image_check_sar
int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
Check if the given sample aspect ratio of an image is valid.
Definition: imgutils.c:323
av_image_check_size
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:318
AVColorRange
AVColorRange
Visual content value range.
Definition: pixfmt.h:541