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