1 /*
2  * This file is part of mpv.
3  *
4  * mpv 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  * mpv 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
12  * GNU 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 mpv.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef MPLAYER_IMG_FORMAT_H
19 #define MPLAYER_IMG_FORMAT_H
20 
21 #include <inttypes.h>
22 
23 #include "osdep/endian.h"
24 #include "misc/bstr.h"
25 #include "video/csputils.h"
26 
27 #if BYTE_ORDER == BIG_ENDIAN
28 #define MP_SELECT_LE_BE(LE, BE) BE
29 #else
30 #define MP_SELECT_LE_BE(LE, BE) LE
31 #endif
32 
33 #define MP_MAX_PLANES 4
34 #define MP_NUM_COMPONENTS 4
35 
36 // mp_imgfmt_desc.comps[] is set to useful values. Some types of formats will
37 // use comps[], but not set this flag, because it doesn't cover all requirements
38 // (for example MP_IMGFLAG_PACKED_SS_YUV).
39 #define MP_IMGFLAG_HAS_COMPS    (1 << 0)
40 
41 // all components start on byte boundaries
42 #define MP_IMGFLAG_BYTES        (1 << 1)
43 
44 // all pixels start in byte boundaries
45 #define MP_IMGFLAG_BYTE_ALIGNED (1 << 2)
46 
47 // set if in little endian, or endian independent
48 #define MP_IMGFLAG_LE           (1 << 3)
49 
50 // set if in big endian, or endian independent
51 #define MP_IMGFLAG_BE           (1 << 4)
52 
53 // set if in native (host) endian, or endian independent
54 #define MP_IMGFLAG_NE           MP_SELECT_LE_BE(MP_IMGFLAG_LE, MP_IMGFLAG_BE)
55 
56 // set if an alpha component is included
57 #define MP_IMGFLAG_ALPHA        (1 << 5)
58 
59 // color class flags - can use via bit tests, or use the mask and compare
60 #define MP_IMGFLAG_COLOR_MASK   (15 << 6)
61 #define MP_IMGFLAG_COLOR_YUV    (1 << 6)
62 #define MP_IMGFLAG_COLOR_RGB    (2 << 6)
63 #define MP_IMGFLAG_COLOR_XYZ    (4 << 6)
64 
65 // component type flags (same access conventions as MP_IMGFLAG_COLOR_*)
66 #define MP_IMGFLAG_TYPE_MASK    (15 << 10)
67 #define MP_IMGFLAG_TYPE_UINT    (1 << 10)
68 #define MP_IMGFLAG_TYPE_FLOAT   (2 << 10)
69 #define MP_IMGFLAG_TYPE_PAL8    (4 << 10)
70 #define MP_IMGFLAG_TYPE_HW      (8 << 10)
71 
72 #define MP_IMGFLAG_YUV          MP_IMGFLAG_COLOR_YUV
73 #define MP_IMGFLAG_RGB          MP_IMGFLAG_COLOR_RGB
74 #define MP_IMGFLAG_PAL          MP_IMGFLAG_TYPE_PAL8
75 #define MP_IMGFLAG_HWACCEL      MP_IMGFLAG_TYPE_HW
76 
77 // 1 component format (or 2 components if MP_IMGFLAG_ALPHA is set).
78 // This should probably be a separate MP_IMGFLAG_COLOR_GRAY, but for now it
79 // is too much of a mess.
80 #define MP_IMGFLAG_GRAY         (1 << 14)
81 
82 // Packed, sub-sampled YUV format. Does not apply to packed non-subsampled YUV.
83 // These formats pack multiple pixels into one sample with strange organization.
84 // In this specific case, mp_imgfmt_desc.align_x gives the size of a "full"
85 // pixel, which has align_x luma samples, and 1 chroma sample of each Cb and Cr.
86 // mp_imgfmt_desc.comps describes the chroma samples, and the first luma sample.
87 // All luma samples have the same configuration as the first one, and you can
88 // get their offsets with mp_imgfmt_get_packed_yuv_locations(). Note that the
89 // component offsets can be >= bpp[0]; the actual range is bpp[0]*align_x.
90 // These formats have no alpha.
91 #define MP_IMGFLAG_PACKED_SS_YUV (1 << 15)
92 
93 // set if the format is in a standard YUV format:
94 // - planar and yuv colorspace
95 // - chroma shift 0-2
96 // - 1-4 planes (1: gray, 2: gray/alpha, 3: yuv, 4: yuv/alpha)
97 // - 8-16 bit per pixel/plane, all planes have same depth,
98 //   each plane has exactly one component
99 #define MP_IMGFLAG_YUV_P        (1 << 16)
100 
101 // Like MP_IMGFLAG_YUV_P, but RGB. This can be e.g. AV_PIX_FMT_GBRP. The planes
102 // are always shuffled (G - B - R [- A]).
103 #define MP_IMGFLAG_RGB_P        (1 << 17)
104 
105 // Semi-planar YUV formats, like AV_PIX_FMT_NV12.
106 #define MP_IMGFLAG_YUV_NV       (1 << 18)
107 
108 struct mp_imgfmt_comp_desc {
109     // Plane on which this component is.
110     uint8_t plane;
111     // Bit offset of first sample, from start of the pixel group (little endian).
112     uint8_t offset : 6;
113     // Number of bits used by each sample.
114     uint8_t size : 6;
115     // Internal padding. See mp_regular_imgfmt.component_pad.
116     int8_t pad : 4;
117 };
118 
119 struct mp_imgfmt_desc {
120     int id;                 // IMGFMT_*
121     int flags;              // MP_IMGFLAG_* bitfield
122     int8_t num_planes;
123     int8_t chroma_xs, chroma_ys; // chroma shift (i.e. log2 of chroma pixel size)
124     int8_t align_x, align_y;     // pixel count to get byte alignment and to get
125                                  // to a pixel pos where luma & chroma aligns
126                                  // always power of 2
127     int8_t bpp[MP_MAX_PLANES];   // bits per pixel (may be "average"; the real
128                                  // byte value is determined by align_x*bpp/8
129                                  // for align_x pixels)
130     // chroma shifts per plane (provided for convenience with planar formats)
131     // Packed YUV always uses xs[0]=ys[0]=0, because plane 0 contains luma in
132     // addition to chroma, and thus is not sub-sampled (uses align_x=2 instead).
133     int8_t xs[MP_MAX_PLANES];
134     int8_t ys[MP_MAX_PLANES];
135 
136     // Description for each component. Generally valid only if flags has
137     // MP_IMGFLAG_HAS_COMPS set.
138     // This is indexed by component_type-1 (so 0=R, 1=G, etc.), see
139     // mp_regular_imgfmt_plane.components[x] for component_type. Components not
140     // present use size=0. Bits not covered by any component are random and not
141     // interpreted by any software.
142     // In particular, don't make the mistake to index this by plane.
143     struct mp_imgfmt_comp_desc comps[MP_NUM_COMPONENTS];
144 
145     // log(2) of the word size in bytes for endian swapping that needs to be
146     // performed for converting to native endian. This is performed before any
147     // other unpacking steps, and for all data covered by bits.
148     // Always 0 if IMGFLAG_NE is set.
149     uint8_t endian_shift : 2;
150 };
151 
152 struct mp_imgfmt_desc mp_imgfmt_get_desc(int imgfmt);
153 
154 // Return the number of component types, or 0 if unknown.
155 int mp_imgfmt_desc_get_num_comps(struct mp_imgfmt_desc *desc);
156 
157 // For MP_IMGFLAG_PACKED_SS_YUV formats (packed sub-sampled YUV): positions of
158 // further luma samples. luma_offsets must be an array of align_x size, and the
159 // function will return the offset (like in mp_imgfmt_comp_desc.offset) of each
160 // luma pixel. luma_offsets[0] == mp_imgfmt_desc.comps[0].offset.
161 bool mp_imgfmt_get_packed_yuv_locations(int imgfmt, uint8_t *luma_offsets);
162 
163 // MP_CSP_AUTO for YUV, MP_CSP_RGB or MP_CSP_XYZ otherwise.
164 // (Because IMGFMT/AV_PIX_FMT conflate format and csp for RGB and XYZ.)
165 enum mp_csp mp_imgfmt_get_forced_csp(int imgfmt);
166 
167 enum mp_component_type {
168     MP_COMPONENT_TYPE_UNKNOWN = 0,
169     MP_COMPONENT_TYPE_UINT,
170     MP_COMPONENT_TYPE_FLOAT,
171 };
172 
173 enum mp_component_type mp_imgfmt_get_component_type(int imgfmt);
174 
175 struct mp_regular_imgfmt_plane {
176     uint8_t num_components;
177     // 1 is red/luminance/gray, 2 is green/Cb, 3 is blue/Cr, 4 is alpha.
178     // 0 is used for padding (undefined contents).
179     // It is guaranteed that non-0 values occur only once in the whole format.
180     uint8_t components[MP_NUM_COMPONENTS];
181 };
182 
183 // This describes pixel formats that are byte aligned, have byte aligned
184 // components, native endian, etc.
185 struct mp_regular_imgfmt {
186     // Type of each component.
187     enum mp_component_type component_type;
188 
189     // See mp_imgfmt_get_forced_csp(). Normally code should use
190     // mp_image_params.colors. This field is only needed to map the format
191     // unambiguously to FFmpeg formats.
192     enum mp_csp forced_csp;
193 
194     // Size of each component in bytes.
195     uint8_t component_size;
196 
197     // If >0, LSB padding, if <0, MSB padding. The padding bits are always 0.
198     // This applies: bit_depth = component_size * 8 - abs(component_pad)
199     //               bit_size  = component_size * 8 + MPMIN(0, component_pad)
200     //  E.g. P010: component_pad=6 (LSB always implied 0, all data in MSB)
201     //          => has a "depth" of 10 bit, but usually treated as 16 bit value
202     //       yuv420p10: component_pad=-6 (like a 10 bit value 0-extended to 16)
203     //          => has depth of 10 bit, needs <<6 to get a 16 bit value
204     int8_t component_pad;
205 
206     uint8_t num_planes;
207     struct mp_regular_imgfmt_plane planes[MP_MAX_PLANES];
208 
209     // Chroma shifts for chroma planes. 0/0 is 4:4:4 YUV or RGB. If not 0/0,
210     // then this is always a yuv format, with components 2/3 on separate planes
211     // (reduced by the shift), and planes for components 1/4 are full sized.
212     uint8_t chroma_xs, chroma_ys;
213 };
214 
215 bool mp_get_regular_imgfmt(struct mp_regular_imgfmt *dst, int imgfmt);
216 int mp_find_regular_imgfmt(struct mp_regular_imgfmt *src);
217 
218 // If imgfmt is valid, and there exists a format that is exactly the same, but
219 // has inverse endianness, return this other format. Otherwise return 0.
220 int mp_find_other_endian(int imgfmt);
221 
222 enum mp_imgfmt {
223     IMGFMT_NONE = 0,
224 
225     // Offset to make confusing with ffmpeg formats harder
226     IMGFMT_START = 1000,
227 
228     // Planar YUV formats
229     IMGFMT_444P,                // 1x1
230     IMGFMT_420P,                // 2x2
231 
232     // Gray
233     IMGFMT_Y8,
234     IMGFMT_Y16,
235 
236     // Packed YUV formats (components are byte-accessed)
237     IMGFMT_UYVY,                // U  Y0 V  Y1
238 
239     // Y plane + packed plane for chroma
240     IMGFMT_NV12,
241 
242     // Like IMGFMT_NV12, but with 10 bits per component (and 6 bits of padding)
243     IMGFMT_P010,
244 
245     // RGB/BGR Formats
246 
247     // Byte accessed (low address to high address)
248     IMGFMT_ARGB,
249     IMGFMT_BGRA,
250     IMGFMT_ABGR,
251     IMGFMT_RGBA,
252     IMGFMT_BGR24,               // 3 bytes per pixel
253     IMGFMT_RGB24,
254 
255     // Like e.g. IMGFMT_ARGB, but has a padding byte instead of alpha
256     IMGFMT_0RGB,
257     IMGFMT_BGR0,
258     IMGFMT_0BGR,
259     IMGFMT_RGB0,
260 
261     // Like IMGFMT_RGBA, but 2 bytes per component.
262     IMGFMT_RGBA64,
263 
264     // Accessed with bit-shifts after endian-swapping the uint16_t pixel
265     IMGFMT_RGB565,              // 5r 6g 5b (MSB to LSB)
266 
267     // AV_PIX_FMT_PAL8
268     IMGFMT_PAL8,
269 
270     // Hardware accelerated formats. Plane data points to special data
271     // structures, instead of pixel data.
272     IMGFMT_VDPAU,           // VdpVideoSurface
273     // plane 0: ID3D11Texture2D
274     // plane 1: slice index casted to pointer
275     IMGFMT_D3D11,
276     IMGFMT_DXVA2,           // IDirect3DSurface9 (NV12/P010/P016)
277     IMGFMT_MMAL,            // MMAL_BUFFER_HEADER_T
278     IMGFMT_MEDIACODEC,      // AVMediaCodecBuffer
279     IMGFMT_DRMPRIME,        // AVDRMFrameDescriptor
280     IMGFMT_CUDA,            // CUDA Buffer
281 
282     // Not an actual format; base for mpv-specific descriptor table.
283     // Some may still map to AV_PIX_FMT_*.
284     IMGFMT_CUST_BASE,
285 
286     // Planar gray/alpha.
287     IMGFMT_YAP8,
288     IMGFMT_YAP16,
289 
290     // Planar YUV/alpha formats. Sometimes useful for internal processing. There
291     // should be one for each subsampling factor, with and without alpha, gray.
292     IMGFMT_YAPF, // Note: non-alpha version exists in ffmpeg
293     IMGFMT_444PF,
294     IMGFMT_444APF,
295     IMGFMT_420PF,
296     IMGFMT_420APF,
297     IMGFMT_422PF,
298     IMGFMT_422APF,
299     IMGFMT_440PF,
300     IMGFMT_440APF,
301     IMGFMT_410PF,
302     IMGFMT_410APF,
303     IMGFMT_411PF,
304     IMGFMT_411APF,
305 
306     // Accessed with bit-shifts, uint32_t units.
307     IMGFMT_RGB30,               // 2pad 10r 10g 10b (MSB to LSB)
308 
309     // Fringe formats for fringe RGB format repacking.
310     IMGFMT_Y1,      // gray with 1 bit per pixel
311     IMGFMT_GBRP1,   // planar RGB with N bits per color component
312     IMGFMT_GBRP2,
313     IMGFMT_GBRP3,
314     IMGFMT_GBRP4,
315     IMGFMT_GBRP5,
316     IMGFMT_GBRP6,
317 
318     // Hardware accelerated formats (again).
319     IMGFMT_VDPAU_OUTPUT,    // VdpOutputSurface
320     IMGFMT_VAAPI,
321     IMGFMT_VIDEOTOOLBOX,    // CVPixelBufferRef
322 
323     // Generic pass-through of AV_PIX_FMT_*. Used for formats which don't have
324     // a corresponding IMGFMT_ value.
325     IMGFMT_AVPIXFMT_START,
326     IMGFMT_AVPIXFMT_END = IMGFMT_AVPIXFMT_START + 500,
327 
328     IMGFMT_END,
329 };
330 
331 #define IMGFMT_IS_HWACCEL(fmt) (!!(mp_imgfmt_get_desc(fmt).flags & MP_IMGFLAG_HWACCEL))
332 
333 int mp_imgfmt_from_name(bstr name);
334 char *mp_imgfmt_to_name_buf(char *buf, size_t buf_size, int fmt);
335 #define mp_imgfmt_to_name(fmt) mp_imgfmt_to_name_buf((char[16]){0}, 16, (fmt))
336 
337 char **mp_imgfmt_name_list(void);
338 
339 #define vo_format_name mp_imgfmt_to_name
340 
341 int mp_imgfmt_select_best(int dst1, int dst2, int src);
342 int mp_imgfmt_select_best_list(int *dst, int num_dst, int src);
343 
344 #endif /* MPLAYER_IMG_FORMAT_H */
345