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 module ffmpeg.libavutil.imgutils;
19 
20 import ffmpeg.libavutil.pixfmt;
21 import ffmpeg.libavutil.pixdesc;
22 import ffmpeg.libavutil.rational;
23 
24 extern (C) @nogc nothrow:
25 
26 /**
27  * @file
28  * misc image utilities
29  *
30  * @addtogroup lavu_picture
31  * @{
32  */
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 (
51     ref int[4] max_pixsteps,
52     ref int[4] max_pixstep_comps,
53     const(AVPixFmtDescriptor)* pixdesc);
54 
55 /**
56  * Compute the size of an image line with format pix_fmt and width
57  * width for the plane plane.
58  *
59  * @return the computed size in bytes
60  */
61 int av_image_get_linesize (AVPixelFormat pix_fmt, int width, int plane);
62 
63 /**
64  * Fill plane linesizes for an image with pixel format pix_fmt and
65  * width width.
66  *
67  * @param linesizes array to be filled with the linesize for each plane
68  * @return >= 0 in case of success, a negative error code otherwise
69  */
70 int av_image_fill_linesizes (ref int[4] linesizes, AVPixelFormat pix_fmt, int width);
71 
72 /**
73  * Fill plane sizes for an image with pixel format pix_fmt and height height.
74  *
75  * @param size the array to be filled with the size of each image plane
76  * @param linesizes the array containing the linesize for each
77  *        plane, should be filled by av_image_fill_linesizes()
78  * @return >= 0 in case of success, a negative error code otherwise
79  *
80  * @note The linesize parameters have the type ptrdiff_t here, while they are
81  *       int for av_image_fill_linesizes().
82  */
83 int av_image_fill_plane_sizes (
84     ref size_t[4] size,
85     AVPixelFormat pix_fmt,
86     int height,
87     ref const(ptrdiff_t)[4] linesizes);
88 
89 /**
90  * Fill plane data pointers for an image with pixel format pix_fmt and
91  * height height.
92  *
93  * @param data pointers array to be filled with the pointer for each image plane
94  * @param ptr the pointer to a buffer which will contain the image
95  * @param linesizes the array containing the linesize for each
96  * plane, should be filled by av_image_fill_linesizes()
97  * @return the size in bytes required for the image buffer, a negative
98  * error code in case of failure
99  */
100 int av_image_fill_pointers (
101     ref ubyte*[4] data,
102     AVPixelFormat pix_fmt,
103     int height,
104     ubyte* ptr,
105     ref const(int)[4] linesizes);
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 align the value to use for buffer size alignment
114  * @return the size in bytes required for the image buffer, a negative
115  * error code in case of failure
116  */
117 int av_image_alloc (
118     ref ubyte*[4] pointers,
119     ref int[4] linesizes,
120     int w,
121     int h,
122     AVPixelFormat pix_fmt,
123     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_linesize linesize for the image plane in dst
135  * @param src_linesize linesize for the image plane in src
136  */
137 void av_image_copy_plane (
138     ubyte* dst,
139     int dst_linesize,
140     const(ubyte)* src,
141     int src_linesize,
142     int bytewidth,
143     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 (
152     ref ubyte*[4] dst_data,
153     ref int[4] dst_linesizes,
154     ref const(ubyte)*[4] src_data,
155     ref const(int)[4] src_linesizes,
156     AVPixelFormat pix_fmt,
157     int width,
158     int height);
159 
160 /**
161  * Copy image data located in uncacheable (e.g. GPU mapped) memory. Where
162  * available, this function will use special functionality for reading from such
163  * memory, which may result in greatly improved performance compared to plain
164  * av_image_copy().
165  *
166  * The data pointers and the linesizes must be aligned to the maximum required
167  * by the CPU architecture.
168  *
169  * @note The linesize parameters have the type ptrdiff_t here, while they are
170  *       int for av_image_copy().
171  * @note On x86, the linesizes currently need to be aligned to the cacheline
172  *       size (i.e. 64) to get improved performance.
173  */
174 void av_image_copy_uc_from (
175     ref ubyte*[4] dst_data,
176     ref const(ptrdiff_t)[4] dst_linesizes,
177     ref const(ubyte)*[4] src_data,
178     ref const(ptrdiff_t)[4] src_linesizes,
179     AVPixelFormat pix_fmt,
180     int width,
181     int height);
182 
183 /**
184  * Setup the data pointers and linesizes based on the specified image
185  * parameters and the provided array.
186  *
187  * The fields of the given image are filled in by using the src
188  * address which points to the image data buffer. Depending on the
189  * specified pixel format, one or multiple image data pointers and
190  * line sizes will be set.  If a planar format is specified, several
191  * pointers will be set pointing to the different picture planes and
192  * the line sizes of the different planes will be stored in the
193  * lines_sizes array. Call with src == NULL to get the required
194  * size for the src buffer.
195  *
196  * To allocate the buffer and fill in the dst_data and dst_linesize in
197  * one call, use av_image_alloc().
198  *
199  * @param dst_data      data pointers to be filled in
200  * @param dst_linesize  linesizes for the image in dst_data to be filled in
201  * @param src           buffer which will contain or contains the actual image data, can be NULL
202  * @param pix_fmt       the pixel format of the image
203  * @param width         the width of the image in pixels
204  * @param height        the height of the image in pixels
205  * @param align         the value used in src for linesize alignment
206  * @return the size in bytes required for src, a negative error code
207  * in case of failure
208  */
209 int av_image_fill_arrays (
210     ref ubyte*[4] dst_data,
211     ref int[4] dst_linesize,
212     const(ubyte)* src,
213     AVPixelFormat pix_fmt,
214     int width,
215     int height,
216     int align_);
217 
218 /**
219  * Return the size in bytes of the amount of data required to store an
220  * image with the given parameters.
221  *
222  * @param pix_fmt  the pixel format of the image
223  * @param width    the width of the image in pixels
224  * @param height   the height of the image in pixels
225  * @param align    the assumed linesize alignment
226  * @return the buffer size in bytes, a negative error code in case of failure
227  */
228 int av_image_get_buffer_size (AVPixelFormat pix_fmt, int width, int height, int align_);
229 
230 /**
231  * Copy image data from an image into a buffer.
232  *
233  * av_image_get_buffer_size() can be used to compute the required size
234  * for the buffer to fill.
235  *
236  * @param dst           a buffer into which picture data will be copied
237  * @param dst_size      the size in bytes of dst
238  * @param src_data      pointers containing the source image data
239  * @param src_linesize  linesizes for the image in src_data
240  * @param pix_fmt       the pixel format of the source image
241  * @param width         the width of the source image in pixels
242  * @param height        the height of the source image in pixels
243  * @param align         the assumed linesize alignment for dst
244  * @return the number of bytes written to dst, or a negative value
245  * (error code) on error
246  */
247 int av_image_copy_to_buffer (
248     ubyte* dst,
249     int dst_size,
250     ref const(ubyte*)[4] src_data,
251     ref const(int)[4] src_linesize,
252     AVPixelFormat pix_fmt,
253     int width,
254     int height,
255     int align_);
256 
257 /**
258  * Check if the given dimension of an image is valid, meaning that all
259  * bytes of the image can be addressed with a signed int.
260  *
261  * @param w the width of the picture
262  * @param h the height of the picture
263  * @param log_offset the offset to sum to the log level for logging with log_ctx
264  * @param log_ctx the parent logging context, it may be NULL
265  * @return >= 0 if valid, a negative error code otherwise
266  */
267 int av_image_check_size (uint w, uint h, int log_offset, void* log_ctx);
268 
269 /**
270  * Check if the given dimension of an image is valid, meaning that all
271  * bytes of a plane of an image with the specified pix_fmt can be addressed
272  * with a signed int.
273  *
274  * @param w the width of the picture
275  * @param h the height of the picture
276  * @param max_pixels the maximum number of pixels the user wants to accept
277  * @param pix_fmt the pixel format, can be AV_PIX_FMT_NONE if unknown.
278  * @param log_offset the offset to sum to the log level for logging with log_ctx
279  * @param log_ctx the parent logging context, it may be NULL
280  * @return >= 0 if valid, a negative error code otherwise
281  */
282 int av_image_check_size2 (uint w, uint h, long max_pixels, AVPixelFormat pix_fmt, int log_offset, void* log_ctx);
283 
284 /**
285  * Check if the given sample aspect ratio of an image is valid.
286  *
287  * It is considered invalid if the denominator is 0 or if applying the ratio
288  * to the image size would make the smaller dimension less than 1. If the
289  * sar numerator is 0, it is considered unknown and will return as valid.
290  *
291  * @param w width of the image
292  * @param h height of the image
293  * @param sar sample aspect ratio of the image
294  * @return 0 if valid, a negative AVERROR code otherwise
295  */
296 int av_image_check_sar (uint w, uint h, AVRational sar);
297 
298 /**
299  * Overwrite the image data with black. This is suitable for filling a
300  * sub-rectangle of an image, meaning the padding between the right most pixel
301  * and the left most pixel on the next line will not be overwritten. For some
302  * formats, the image size might be rounded up due to inherent alignment.
303  *
304  * If the pixel format has alpha, the alpha is cleared to opaque.
305  *
306  * This can return an error if the pixel format is not supported. Normally, all
307  * non-hwaccel pixel formats should be supported.
308  *
309  * Passing NULL for dst_data is allowed. Then the function returns whether the
310  * operation would have succeeded. (It can return an error if the pix_fmt is
311  * not supported.)
312  *
313  * @param dst_data      data pointers to destination image
314  * @param dst_linesize  linesizes for the destination image
315  * @param pix_fmt       the pixel format of the image
316  * @param range         the color range of the image (important for colorspaces such as YUV)
317  * @param width         the width of the image in pixels
318  * @param height        the height of the image in pixels
319  * @return 0 if the image data was cleared, a negative AVERROR code otherwise
320  */
321 int av_image_fill_black (
322     ref ubyte*[4] dst_data,
323     ref const(ptrdiff_t)[4] dst_linesize,
324     AVPixelFormat pix_fmt,
325     AVColorRange range,
326     int width,
327     int height);
328 
329 /**
330  * @}
331  */
332 
333 /* AVUTIL_IMGUTILS_H */