1 /*
2  * pixel format descriptor
3  * Copyright (c) 2009 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 
22 module ffmpeg.libavutil.pixdesc;
23 
24 import ffmpeg.libavutil.pixfmt;
25 
26 extern (C):
27 import ffmpeg; @nogc nothrow:
28 
29 struct AVComponentDescriptor
30 {
31     /**
32      * Which of the 4 planes contains the component.
33      */
34     int plane;
35 
36     /**
37      * Number of elements between 2 horizontally consecutive pixels.
38      * Elements are bits for bitstream formats, bytes otherwise.
39      */
40     int step;
41 
42     /**
43      * Number of elements before the component of the first pixel.
44      * Elements are bits for bitstream formats, bytes otherwise.
45      */
46     int offset;
47 
48     /**
49      * Number of least significant bits that must be shifted away
50      * to get the value.
51      */
52     int shift;
53 
54     /**
55      * Number of bits in the component.
56      */
57     int depth;
58 
59     /** deprecated, use step instead */
60     int step_minus1;
61 
62     /** deprecated, use depth instead */
63     int depth_minus1;
64 
65     /** deprecated, use offset instead */
66     int offset_plus1;
67 }
68 
69 /**
70  * Descriptor that unambiguously describes how the bits of a pixel are
71  * stored in the up to 4 data planes of an image. It also stores the
72  * subsampling factors and number of components.
73  *
74  * @note This is separate of the colorspace (RGB, YCbCr, YPbPr, JPEG-style YUV
75  *       and all the YUV variants) AVPixFmtDescriptor just stores how values
76  *       are stored not what these values represent.
77  */
78 struct AVPixFmtDescriptor
79 {
80     const(char)* name;
81     ubyte nb_components; ///< The number of components each pixel has, (1-4)
82 
83     /**
84      * Amount to shift the luma width right to find the chroma width.
85      * For YV12 this is 1 for example.
86      * chroma_width = AV_CEIL_RSHIFT(luma_width, log2_chroma_w)
87      * The note above is needed to ensure rounding up.
88      * This value only refers to the chroma components.
89      */
90     ubyte log2_chroma_w;
91 
92     /**
93      * Amount to shift the luma height right to find the chroma height.
94      * For YV12 this is 1 for example.
95      * chroma_height= AV_CEIL_RSHIFT(luma_height, log2_chroma_h)
96      * The note above is needed to ensure rounding up.
97      * This value only refers to the chroma components.
98      */
99     ubyte log2_chroma_h;
100 
101     /**
102      * Combination of AV_PIX_FMT_FLAG_... flags.
103      */
104     ulong flags;
105 
106     /**
107      * Parameters that describe how pixels are packed.
108      * If the format has 1 or 2 components, then luma is 0.
109      * If the format has 3 or 4 components:
110      *   if the RGB flag is set then 0 is red, 1 is green and 2 is blue;
111      *   otherwise 0 is luma, 1 is chroma-U and 2 is chroma-V.
112      *
113      * If present, the Alpha channel is always the last component.
114      */
115     AVComponentDescriptor[4] comp;
116 
117     /**
118      * Alternative comma-separated names.
119      */
120     const(char)* alias_;
121 }
122 
123 /**
124  * Pixel format is big-endian.
125  */
126 enum AV_PIX_FMT_FLAG_BE = 1 << 0;
127 /**
128  * Pixel format has a palette in data[1], values are indexes in this palette.
129  */
130 enum AV_PIX_FMT_FLAG_PAL = 1 << 1;
131 /**
132  * All values of a component are bit-wise packed end to end.
133  */
134 enum AV_PIX_FMT_FLAG_BITSTREAM = 1 << 2;
135 /**
136  * Pixel format is an HW accelerated format.
137  */
138 enum AV_PIX_FMT_FLAG_HWACCEL = 1 << 3;
139 /**
140  * At least one pixel component is not in the first data plane.
141  */
142 enum AV_PIX_FMT_FLAG_PLANAR = 1 << 4;
143 /**
144  * The pixel format contains RGB-like data (as opposed to YUV/grayscale).
145  */
146 enum AV_PIX_FMT_FLAG_RGB = 1 << 5;
147 
148 /**
149  * The pixel format is "pseudo-paletted". This means that it contains a
150  * fixed palette in the 2nd plane but the palette is fixed/constant for each
151  * PIX_FMT. This allows interpreting the data as if it was PAL8, which can
152  * in some cases be simpler. Or the data can be interpreted purely based on
153  * the pixel format without using the palette.
154  * An example of a pseudo-paletted format is AV_PIX_FMT_GRAY8
155  *
156  * @deprecated This flag is deprecated, and will be removed. When it is removed,
157  * the extra palette allocation in AVFrame.data[1] is removed as well. Only
158  * actual paletted formats (as indicated by AV_PIX_FMT_FLAG_PAL) will have a
159  * palette. Starting with FFmpeg versions which have this flag deprecated, the
160  * extra "pseudo" palette is already ignored, and API users are not required to
161  * allocate a palette for AV_PIX_FMT_FLAG_PSEUDOPAL formats (it was required
162  * before the deprecation, though).
163  */
164 enum AV_PIX_FMT_FLAG_PSEUDOPAL = 1 << 6;
165 
166 /**
167  * The pixel format has an alpha channel. This is set on all formats that
168  * support alpha in some way, including AV_PIX_FMT_PAL8. The alpha is always
169  * straight, never pre-multiplied.
170  *
171  * If a codec or a filter does not support alpha, it should set all alpha to
172  * opaque, or use the equivalent pixel formats without alpha component, e.g.
173  * AV_PIX_FMT_RGB0 (or AV_PIX_FMT_RGB24 etc.) instead of AV_PIX_FMT_RGBA.
174  */
175 enum AV_PIX_FMT_FLAG_ALPHA = 1 << 7;
176 
177 /**
178  * The pixel format is following a Bayer pattern
179  */
180 enum AV_PIX_FMT_FLAG_BAYER = 1 << 8;
181 
182 /**
183  * The pixel format contains IEEE-754 floating point values. Precision (double,
184  * single, or half) should be determined by the pixel size (64, 32, or 16 bits).
185  */
186 enum AV_PIX_FMT_FLAG_FLOAT = 1 << 9;
187 
188 /**
189  * Return the number of bits per pixel used by the pixel format
190  * described by pixdesc. Note that this is not the same as the number
191  * of bits per sample.
192  *
193  * The returned number of bits refers to the number of bits actually
194  * used for storing the pixel information, that is padding bits are
195  * not counted.
196  */
197 int av_get_bits_per_pixel (const(AVPixFmtDescriptor)* pixdesc);
198 
199 /**
200  * Return the number of bits per pixel for the pixel format
201  * described by pixdesc, including any padding or unused bits.
202  */
203 int av_get_padded_bits_per_pixel (const(AVPixFmtDescriptor)* pixdesc);
204 
205 /**
206  * @return a pixel format descriptor for provided pixel format or NULL if
207  * this pixel format is unknown.
208  */
209 const(AVPixFmtDescriptor)* av_pix_fmt_desc_get (AVPixelFormat pix_fmt);
210 
211 /**
212  * Iterate over all pixel format descriptors known to libavutil.
213  *
214  * @param prev previous descriptor. NULL to get the first descriptor.
215  *
216  * @return next descriptor or NULL after the last descriptor
217  */
218 const(AVPixFmtDescriptor)* av_pix_fmt_desc_next (const(AVPixFmtDescriptor)* prev);
219 
220 /**
221  * @return an AVPixelFormat id described by desc, or AV_PIX_FMT_NONE if desc
222  * is not a valid pointer to a pixel format descriptor.
223  */
224 AVPixelFormat av_pix_fmt_desc_get_id (const(AVPixFmtDescriptor)* desc);
225 
226 /**
227  * Utility function to access log2_chroma_w log2_chroma_h from
228  * the pixel format AVPixFmtDescriptor.
229  *
230  * @param[in]  pix_fmt the pixel format
231  * @param[out] h_shift store log2_chroma_w (horizontal/width shift)
232  * @param[out] v_shift store log2_chroma_h (vertical/height shift)
233  *
234  * @return 0 on success, AVERROR(ENOSYS) on invalid or unknown pixel format
235  */
236 int av_pix_fmt_get_chroma_sub_sample (
237     AVPixelFormat pix_fmt,
238     int* h_shift,
239     int* v_shift);
240 
241 /**
242  * @return number of planes in pix_fmt, a negative AVERROR if pix_fmt is not a
243  * valid pixel format.
244  */
245 int av_pix_fmt_count_planes (AVPixelFormat pix_fmt);
246 
247 /**
248  * @return the name for provided color range or NULL if unknown.
249  */
250 const(char)* av_color_range_name (AVColorRange range);
251 
252 /**
253  * @return the AVColorRange value for name or an AVError if not found.
254  */
255 int av_color_range_from_name (const(char)* name);
256 
257 /**
258  * @return the name for provided color primaries or NULL if unknown.
259  */
260 const(char)* av_color_primaries_name (AVColorPrimaries primaries);
261 
262 /**
263  * @return the AVColorPrimaries value for name or an AVError if not found.
264  */
265 int av_color_primaries_from_name (const(char)* name);
266 
267 /**
268  * @return the name for provided color transfer or NULL if unknown.
269  */
270 const(char)* av_color_transfer_name (AVColorTransferCharacteristic transfer);
271 
272 /**
273  * @return the AVColorTransferCharacteristic value for name or an AVError if not found.
274  */
275 int av_color_transfer_from_name (const(char)* name);
276 
277 /**
278  * @return the name for provided color space or NULL if unknown.
279  */
280 const(char)* av_color_space_name (AVColorSpace space);
281 
282 /**
283  * @return the AVColorSpace value for name or an AVError if not found.
284  */
285 int av_color_space_from_name (const(char)* name);
286 
287 /**
288  * @return the name for provided chroma location or NULL if unknown.
289  */
290 const(char)* av_chroma_location_name (AVChromaLocation location);
291 
292 /**
293  * @return the AVChromaLocation value for name or an AVError if not found.
294  */
295 int av_chroma_location_from_name (const(char)* name);
296 
297 /**
298  * Return the pixel format corresponding to name.
299  *
300  * If there is no pixel format with name name, then looks for a
301  * pixel format with the name corresponding to the native endian
302  * format of name.
303  * For example in a little-endian system, first looks for "gray16",
304  * then for "gray16le".
305  *
306  * Finally if no pixel format has been found, returns AV_PIX_FMT_NONE.
307  */
308 AVPixelFormat av_get_pix_fmt (const(char)* name);
309 
310 /**
311  * Return the short name for a pixel format, NULL in case pix_fmt is
312  * unknown.
313  *
314  * @see av_get_pix_fmt(), av_get_pix_fmt_string()
315  */
316 const(char)* av_get_pix_fmt_name (AVPixelFormat pix_fmt);
317 
318 /**
319  * Print in buf the string corresponding to the pixel format with
320  * number pix_fmt, or a header if pix_fmt is negative.
321  *
322  * @param buf the buffer where to write the string
323  * @param buf_size the size of buf
324  * @param pix_fmt the number of the pixel format to print the
325  * corresponding info string, or a negative value to print the
326  * corresponding header.
327  */
328 char* av_get_pix_fmt_string (char* buf, int buf_size, AVPixelFormat pix_fmt);
329 
330 /**
331  * Read a line from an image, and write the values of the
332  * pixel format component c to dst.
333  *
334  * @param data the array containing the pointers to the planes of the image
335  * @param linesize the array containing the linesizes of the image
336  * @param desc the pixel format descriptor for the image
337  * @param x the horizontal coordinate of the first pixel to read
338  * @param y the vertical coordinate of the first pixel to read
339  * @param w the width of the line to read, that is the number of
340  * values to write to dst
341  * @param read_pal_component if not zero and the format is a paletted
342  * format writes the values corresponding to the palette
343  * component c in data[1] to dst, rather than the palette indexes in
344  * data[0]. The behavior is undefined if the format is not paletted.
345  * @param dst_element_size size of elements in dst array (2 or 4 byte)
346  */
347 void av_read_image_line2 (
348     void* dst,
349     ref const(ubyte)*[4] data,
350     ref const(int)[4] linesize,
351     const(AVPixFmtDescriptor)* desc,
352     int x,
353     int y,
354     int c,
355     int w,
356     int read_pal_component,
357     int dst_element_size);
358 
359 void av_read_image_line (
360     ushort* dst,
361     ref const(ubyte)*[4] data,
362     ref const(int)[4] linesize,
363     const(AVPixFmtDescriptor)* desc,
364     int x,
365     int y,
366     int c,
367     int w,
368     int read_pal_component);
369 
370 /**
371  * Write the values from src to the pixel format component c of an
372  * image line.
373  *
374  * @param src array containing the values to write
375  * @param data the array containing the pointers to the planes of the
376  * image to write into. It is supposed to be zeroed.
377  * @param linesize the array containing the linesizes of the image
378  * @param desc the pixel format descriptor for the image
379  * @param x the horizontal coordinate of the first pixel to write
380  * @param y the vertical coordinate of the first pixel to write
381  * @param w the width of the line to write, that is the number of
382  * values to write to the image line
383  * @param src_element_size size of elements in src array (2 or 4 byte)
384  */
385 void av_write_image_line2 (
386     const(void)* src,
387     ref ubyte*[4] data,
388     ref const(int)[4] linesize,
389     const(AVPixFmtDescriptor)* desc,
390     int x,
391     int y,
392     int c,
393     int w,
394     int src_element_size);
395 
396 void av_write_image_line (
397     const(ushort)* src,
398     ref ubyte*[4] data,
399     ref const(int)[4] linesize,
400     const(AVPixFmtDescriptor)* desc,
401     int x,
402     int y,
403     int c,
404     int w);
405 
406 /**
407  * Utility function to swap the endianness of a pixel format.
408  *
409  * @param[in]  pix_fmt the pixel format
410  *
411  * @return pixel format with swapped endianness if it exists,
412  * otherwise AV_PIX_FMT_NONE
413  */
414 AVPixelFormat av_pix_fmt_swap_endianness (AVPixelFormat pix_fmt);
415 
416 enum FF_LOSS_RESOLUTION = 0x0001; /**< loss due to resolution change */
417 enum FF_LOSS_DEPTH = 0x0002; /**< loss due to color depth change */
418 enum FF_LOSS_COLORSPACE = 0x0004; /**< loss due to color space conversion */
419 enum FF_LOSS_ALPHA = 0x0008; /**< loss of alpha bits */
420 enum FF_LOSS_COLORQUANT = 0x0010; /**< loss due to color quantization */
421 enum FF_LOSS_CHROMA = 0x0020; /**< loss of chroma (e.g. RGB to gray conversion) */
422 
423 /**
424  * Compute what kind of losses will occur when converting from one specific
425  * pixel format to another.
426  * When converting from one pixel format to another, information loss may occur.
427  * For example, when converting from RGB24 to GRAY, the color information will
428  * be lost. Similarly, other losses occur when converting from some formats to
429  * other formats. These losses can involve loss of chroma, but also loss of
430  * resolution, loss of color depth, loss due to the color space conversion, loss
431  * of the alpha bits or loss due to color quantization.
432  * av_get_fix_fmt_loss() informs you about the various types of losses
433  * which will occur when converting from one pixel format to another.
434  *
435  * @param[in] dst_pix_fmt destination pixel format
436  * @param[in] src_pix_fmt source pixel format
437  * @param[in] has_alpha Whether the source pixel format alpha channel is used.
438  * @return Combination of flags informing you what kind of losses will occur
439  * (maximum loss for an invalid dst_pix_fmt).
440  */
441 int av_get_pix_fmt_loss (
442     AVPixelFormat dst_pix_fmt,
443     AVPixelFormat src_pix_fmt,
444     int has_alpha);
445 
446 /**
447  * Compute what kind of losses will occur when converting from one specific
448  * pixel format to another.
449  * When converting from one pixel format to another, information loss may occur.
450  * For example, when converting from RGB24 to GRAY, the color information will
451  * be lost. Similarly, other losses occur when converting from some formats to
452  * other formats. These losses can involve loss of chroma, but also loss of
453  * resolution, loss of color depth, loss due to the color space conversion, loss
454  * of the alpha bits or loss due to color quantization.
455  * av_get_fix_fmt_loss() informs you about the various types of losses
456  * which will occur when converting from one pixel format to another.
457  *
458  * @param[in] dst_pix_fmt destination pixel format
459  * @param[in] src_pix_fmt source pixel format
460  * @param[in] has_alpha Whether the source pixel format alpha channel is used.
461  * @return Combination of flags informing you what kind of losses will occur
462  * (maximum loss for an invalid dst_pix_fmt).
463  */
464 AVPixelFormat av_find_best_pix_fmt_of_2 (
465     AVPixelFormat dst_pix_fmt1,
466     AVPixelFormat dst_pix_fmt2,
467     AVPixelFormat src_pix_fmt,
468     int has_alpha,
469     int* loss_ptr);
470 
471 /* AVUTIL_PIXDESC_H */