Debian Codegraph @

gst-libs/gst/va/gstvavideoformat.c

raw symbols SCIP
1 /* GStreamer
2 * Copyright (C) 2020 Igalia, S.L.
3 * Author: Víctor Jáquez <[email protected]m>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gstvavideoformat.h"
26
27 #define GST_CAT_DEFAULT gst_va_display_debug
28 GST_DEBUG_CATEGORY_EXTERN (gst_va_display_debug);
29
30 #define VA_NSB_FIRST 0 /* No Significant Bit */
31
32 /* *INDENT-OFF* */
33 static struct FormatMap
34 {
35 GstVideoFormat format;
36 guint va_rtformat;
37 VAImageFormat va_format;
38 } format_map[] = {
39 #define F(format, fourcc, rtformat, order, bpp, depth, r, g, b, a) { \
40 G_PASTE (GST_VIDEO_FORMAT_, format), \
41 G_PASTE (VA_RT_FORMAT_, rtformat), \
42 { VA_FOURCC fourcc, G_PASTE (G_PASTE (VA_, order), _FIRST), \
43 bpp, depth, r, g, b, a } }
44 #define G(format, fourcc, rtformat, order, bpp) \
45 F (format, fourcc, rtformat, order, bpp, 0, 0, 0 ,0, 0)
46 G (NV12, ('N', 'V', '1', '2'), YUV420, NSB, 12),
47 G (NV21, ('N', 'V', '2', '1'), YUV420, NSB, 12),
48 G (VUYA, ('A', 'Y', 'U', 'V'), YUV444, LSB, 32),
49 F (RGBA, ('R', 'G', 'B', 'A'), RGB32, LSB, 32, 32, 0x000000ff,
50 0x0000ff00, 0x00ff0000, 0xff000000),
51 F (RGBx, ('R', 'G', 'B', 'X'), RGB32, LSB, 32, 24, 0x000000ff,
52 0x0000ff00, 0x00ff0000, 0x00000000),
53 F (BGRA, ('B', 'G', 'R', 'A'), RGB32, LSB, 32, 32, 0x00ff0000,
54 0x0000ff00, 0x000000ff, 0xff000000),
55 F (ARGB, ('A', 'R', 'G', 'B'), RGB32, LSB, 32, 32, 0x0000ff00,
56 0x00ff0000, 0xff000000, 0x000000ff),
57 F (xRGB, ('X', 'R', 'G', 'B'), RGB32, LSB, 32, 24, 0x0000ff00,
58 0x00ff0000, 0xff000000, 0x00000000),
59 F (ABGR, ('A', 'B', 'G', 'R'), RGB32, LSB, 32, 32, 0xff000000,
60 0x00ff0000, 0x0000ff00, 0x000000ff),
61 F (xBGR, ('X', 'B', 'G', 'R'), RGB32, LSB, 32, 24, 0xff000000,
62 0x00ff0000, 0x0000ff00, 0x00000000),
63 F (BGRx, ('B', 'G', 'R', 'X'), RGB32, LSB, 32, 24, 0x00ff0000,
64 0x0000ff00, 0x000000ff, 0x00000000),
65 G (UYVY, ('U', 'Y', 'V', 'Y'), YUV422, NSB, 16),
66 G (YUY2, ('Y', 'U', 'Y', '2'), YUV422, NSB, 16),
67 G (AYUV, ('A', 'Y', 'U', 'V'), YUV444, LSB, 32),
68 /* F (????, NV11), */
69 G (YV12, ('Y', 'V', '1', '2'), YUV420, NSB, 12),
70 /* F (????, P208), */
71 G (I420, ('I', '4', '2', '0'), YUV420, NSB, 12),
72 /* F (????, YV24), */
73 /* F (????, YV32), */
74 /* F (????, Y800), */
75 /* F (????, IMC3), */
76 /* F (????, 411P), */
77 /* F (????, 411R), */
78 G (Y42B, ('4', '2', '2', 'H'), YUV422, LSB, 16),
79 /* F (????, 422V), */
80 /* F (????, 444P), */
81 G (RGBP, ('R', 'G', 'B', 'P'), RGBP, LSB, 8),
82 /* F (????, BGRP), */
83 /* F (????, RGB565), */
84 /* F (????, BGR565), */
85 G (Y210, ('Y', '2', '1', '0'), YUV422_10, NSB, 32),
86 /* F (????, Y216), */
87 G (Y410, ('Y', '4', '1', '0'), YUV444_10, NSB, 32),
88 G (Y212_LE, ('Y', '2', '1', '2'), YUV422_12, NSB, 32),
89 G (Y412_LE, ('Y', '4', '1', '2'), YUV444_12, NSB, 32),
90 /* F (????, Y416), */
91 /* F (????, YV16), */
92 G (P010_10LE, ('P', '0', '1', '0'), YUV420_10, NSB, 24),
93 G (P012_LE, ('P', '0', '1', '2'), YUV420_12, NSB, 24),
94 /* F (P016_LE, P016, ????), */
95 /* F (????, I010), */
96 /* F (????, IYUV), */
97 /* F (????, A2R10G10B10), */
98 /* F (????, A2B10G10R10), */
99 /* F (????, X2R10G10B10), */
100 /* F (????, X2B10G10R10), */
101 G (GRAY8, ('Y', '8', '0', '0'), YUV400, NSB, 8),
102 G (Y444, ('4', '4', '4', 'P'), YUV444, NSB, 24),
103 /* F (????, Y16), */
104 /* G (VYUY, VYUY, YUV422), */
105 /* G (YVYU, YVYU, YUV422), */
106 /* F (ARGB64, ARGB64, ????), */
107 /* F (????, ABGR64), */
108 F (RGB16, ('R', 'G', '1', '6'), RGB16, NSB, 16, 16, 0x0000f800,
109 0x000007e0, 0x0000001f, 0x00000000),
110 F (RGB, ('R', 'G', '2', '4'), RGB32, NSB, 32, 24, 0x00ff0000,
111 0x0000ff00, 0x000000ff, 0x00000000),
112 F (BGR10A2_LE, ('A', 'R', '3', '0'), RGB32, LSB, 32, 30, 0x3ff00000,
113 0x000ffc00, 0x000003ff, 0x30000000),
114 #undef F
115 #undef G
116 };
117
118 static const struct RBG32FormatMap
119 {
120 GstVideoFormat format;
121 VAImageFormat va_format[2];
122 } rgb32_format_map[] = {
123 #define F(fourcc, order, bpp, depth, r, g, b, a) \
124 { VA_FOURCC fourcc, G_PASTE (G_PASTE (VA_, order), _FIRST), bpp, depth, r, g, b, a }
125 #define A(fourcc, order, r, g, b, a) F (fourcc, order, 32, 32, r, g, b, a)
126 #define X(fourcc, order, r, g, b) F (fourcc, order, 32, 24, r, g, b, 0x0)
127 { GST_VIDEO_FORMAT_ARGB, {
128 A (('B', 'G', 'R', 'A'), LSB, 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff),
129 A (('A', 'R', 'G', 'B'), MSB, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000),
130 } },
131 { GST_VIDEO_FORMAT_RGBA, {
132 A (('A', 'B', 'G', 'R'), LSB, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000),
133 A (('R', 'G', 'B', 'A'), MSB, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff),
134 } },
135 { GST_VIDEO_FORMAT_ABGR, {
136 A (('R', 'G', 'B', 'A'), LSB, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff),
137 A (('A', 'B', 'G', 'R'), MSB, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000),
138 } },
139 { GST_VIDEO_FORMAT_BGRA, {
140 A (('A', 'R', 'G', 'B'), LSB, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000),
141 A (('B', 'G', 'R', 'A'), MSB, 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff),
142 } },
143 { GST_VIDEO_FORMAT_xRGB, {
144 X (('B', 'G', 'R', 'X'), LSB, 0x0000ff00, 0x00ff0000, 0xff000000),
145 X (('X', 'R', 'G', 'B'), MSB, 0x00ff0000, 0x0000ff00, 0x000000ff),
146 } },
147 { GST_VIDEO_FORMAT_RGBx, {
148 X (('X', 'B', 'G', 'R'), LSB, 0x000000ff, 0x0000ff00, 0x00ff0000),
149 X (('R', 'G', 'B', 'X'), MSB, 0xff000000, 0x00ff0000, 0x0000ff00),
150 } },
151 { GST_VIDEO_FORMAT_xBGR, {
152 X (('R', 'G', 'B', 'X'), LSB, 0xff000000, 0x00ff0000, 0x0000ff00),
153 X (('X', 'B', 'G', 'R'), MSB, 0x000000ff, 0x0000ff00, 0x00ff0000),
154 } },
155 { GST_VIDEO_FORMAT_BGRx, {
156 X (('X', 'R', 'G', 'B'), LSB, 0x00ff0000, 0x0000ff00, 0x000000ff),
157 X (('B', 'G', 'R', 'X'), MSB, 0x0000ff00, 0x00ff0000, 0xff000000),
158 } },
159 #undef X
160 #undef A
161 #undef F
162 };
163 /* *INDENT-ON* */
164
165 static const struct FormatMap *
166 get_format_map_from_va_fourcc (guint va_fourcc)
167 {
168 int i;
169
170 for (i = 0; i < G_N_ELEMENTS (format_map); i++) {
171 if (format_map[i].va_format.fourcc == va_fourcc)
172 return &format_map[i];
173 }
174
175 return NULL;
176 }
177
178 static const struct FormatMap *
179 get_format_map_from_drm_fourcc (guint drm_fourcc)
180 {
181 int i;
182 GstVideoFormat format;
183
184 format = gst_video_dma_drm_fourcc_to_format (drm_fourcc);
185 for (i = 0; i < G_N_ELEMENTS (format_map); i++) {
186 if (format_map[i].format == format)
187 return &format_map[i];
188 }
189
190 return NULL;
191 }
192
193 static struct FormatMap *
194 get_format_map_from_video_format (GstVideoFormat format)
195 {
196 int i;
197
198 for (i = 0; i < G_N_ELEMENTS (format_map); i++) {
199 if (format_map[i].format == format)
200 return &format_map[i];
201 }
202
203 return NULL;
204 }
205
206 static inline gboolean
207 va_format_is_rgb (const VAImageFormat * va_format)
208 {
209 return va_format->depth != 0;
210 }
211
212 static inline gboolean
213 va_format_is_same_rgb (const VAImageFormat * fmt1, const VAImageFormat * fmt2)
214 {
215 return (fmt1->red_mask == fmt2->red_mask
216 && fmt1->green_mask == fmt2->green_mask
217 && fmt1->blue_mask == fmt2->blue_mask
218 && fmt1->alpha_mask == fmt2->alpha_mask);
219 }
220
221 static inline gboolean
222 va_format_is_same (const VAImageFormat * fmt1, const VAImageFormat * fmt2)
223 {
224 if (fmt1->fourcc != fmt2->fourcc)
225 return FALSE;
226 if (fmt1->byte_order != VA_NSB_FIRST
227 && fmt2->byte_order != VA_NSB_FIRST
228 && fmt1->byte_order != fmt2->byte_order)
229 return FALSE;
230 return va_format_is_rgb (fmt1) ? va_format_is_same_rgb (fmt1, fmt2) : TRUE;
231 }
232
233 static const struct FormatMap *
234 get_format_map_from_va_image_format (const VAImageFormat * va_format)
235 {
236 int i;
237
238 for (i = 0; i < G_N_ELEMENTS (format_map); i++) {
239 if (va_format_is_same (&format_map[i].va_format, va_format))
240 return &format_map[i];
241 }
242
243 return NULL;
244 }
245
246 /**
247 * XXX: there are two potentially different fourcc: the VA and the DRM.
248 *
249 * Normally they should be the same, but there are a couple formats where VA's
250 * fourcc is different from the DRM's fourcc. One example is
251 * GST_VIDEO_FORMAT_I420, where VA's fourcc is ('I', '4', '2', '0') while DRM's
252 * is ('Y', 'U', '1', '2').
253 *
254 * That's the reason there are two functions:
255 * gst_va_video_format_from_va_fourcc() and
256 * gst_va_video_format_from_drm_fourcc() They should be used depending where the
257 * value is going to be used: for VA concerns the first should be used, for
258 * DMABuf exportation, the last.
259 */
260 GstVideoFormat
261 gst_va_video_format_from_va_fourcc (guint fourcc)
262 {
263 const struct FormatMap *map = get_format_map_from_va_fourcc (fourcc);
264
265 return map ? map->format : GST_VIDEO_FORMAT_UNKNOWN;
266 }
267
268 guint
269 gst_va_fourcc_from_video_format (GstVideoFormat format)
270 {
271 const struct FormatMap *map = get_format_map_from_video_format (format);
272
273 return map ? map->va_format.fourcc : 0;
274 }
275
276 GstVideoFormat
277 gst_va_video_format_from_drm_fourcc (guint fourcc)
278 {
279 const struct FormatMap *map = get_format_map_from_drm_fourcc (fourcc);
280
281 return map ? map->format : GST_VIDEO_FORMAT_UNKNOWN;
282 }
283
284 guint
285 gst_va_drm_fourcc_from_video_format (GstVideoFormat format)
286 {
287 const struct FormatMap *map = get_format_map_from_video_format (format);
288
289 return map ? gst_video_dma_drm_fourcc_from_format (format) : 0;
290 }
291
292 guint
293 gst_va_chroma_from_video_format (GstVideoFormat format)
294 {
295 const struct FormatMap *map = get_format_map_from_video_format (format);
296
297 return map ? map->va_rtformat : 0;
298 }
299
300 guint
301 gst_va_chroma_from_va_fourcc (guint va_fourcc)
302 {
303 const struct FormatMap *map = get_format_map_from_va_fourcc (va_fourcc);
304
305 return map ? map->va_rtformat : 0;
306 }
307
308 const VAImageFormat *
309 gst_va_image_format_from_video_format (GstVideoFormat format)
310 {
311 const struct FormatMap *map = get_format_map_from_video_format (format);
312
313 return map ? &map->va_format : NULL;
314 }
315
316 GstVideoFormat
317 gst_va_video_format_from_va_image_format (const VAImageFormat * va_format)
318 {
319 const struct FormatMap *map = get_format_map_from_va_image_format (va_format);
320
321 return map ? map->format : GST_VIDEO_FORMAT_UNKNOWN;
322 }
323
324 /*
325 * XXX: Not all the surfaces formats can be converted into every image
326 * format when mapped. This funtion will return the #GstVideoFormat
327 * that a surface will map when it is asked for a @image_format.
328 *
329 * Current implementation only seeks for @image_format in
330 * @surface_formats.
331 */
332 GstVideoFormat
333 gst_va_video_surface_format_from_image_format (GstVideoFormat image_format,
334 GArray * surface_formats)
335 {
336 GstVideoFormat surface_format;
337 guint i, image_chroma;
338
339 if (image_format == GST_VIDEO_FORMAT_UNKNOWN)
340 return GST_VIDEO_FORMAT_UNKNOWN;
341
342 if (!surface_formats || surface_formats->len == 0)
343 return GST_VIDEO_FORMAT_UNKNOWN;
344
345 image_chroma = gst_va_chroma_from_video_format (image_format);
346 if (image_chroma == 0)
347 return GST_VIDEO_FORMAT_UNKNOWN;
348
349 for (i = 0; i < surface_formats->len; i++) {
350 surface_format = g_array_index (surface_formats, GstVideoFormat, i);
351
352 if (surface_format == image_format)
353 return surface_format;
354 }
355
356 return GST_VIDEO_FORMAT_UNKNOWN;
357 }
358
359 /* Convert the GstVideoInfoDmaDrm into a traditional GstVideoInfo
360 with recognized format. */
361 gboolean
362 gst_va_dma_drm_info_to_video_info (const GstVideoInfoDmaDrm * drm_info,
363 GstVideoInfo * info)
364 {
365 GstVideoFormat video_format;
366 GstVideoInfo tmp_info;
367 guint i;
368
369 g_return_val_if_fail (drm_info, FALSE);
370 g_return_val_if_fail (info, FALSE);
371
372 if (GST_VIDEO_INFO_FORMAT (&drm_info->vinfo) != GST_VIDEO_FORMAT_DMA_DRM) {
373 *info = drm_info->vinfo;
374 return TRUE;
375 }
376
377 /* The non linear DMA format will be recognized as FORMAT_ENCODED,
378 but we still need to know its real format to set the info such
379 as pitch and stride. Because va plugins have its internal mapping
380 between drm fourcc and video format, we do not use the standard
381 conversion API here. */
382 video_format = gst_va_video_format_from_drm_fourcc (drm_info->drm_fourcc);
383 if (video_format == GST_VIDEO_FORMAT_UNKNOWN)
384 return FALSE;
385
386 if (!gst_video_info_set_format (&tmp_info, video_format,
387 GST_VIDEO_INFO_WIDTH (&drm_info->vinfo),
388 GST_VIDEO_INFO_HEIGHT (&drm_info->vinfo)))
389 return FALSE;
390
391 *info = drm_info->vinfo;
392 info->finfo = tmp_info.finfo;
393 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
394 info->stride[i] = tmp_info.stride[i];
395 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
396 info->offset[i] = tmp_info.offset[i];
397 info->size = tmp_info.size;
398
399 return TRUE;
400 }
401
402 static GstVideoFormat
403 find_gst_video_format_in_rgb32_map (VAImageFormat * image_format)
404 {
405 guint i, j;
406
407 for (i = 0; i < G_N_ELEMENTS (rgb32_format_map); i++) {
408 for (j = 0; j < G_N_ELEMENTS (rgb32_format_map[i].va_format); j++) {
409 if (va_format_is_same (&rgb32_format_map[i].va_format[j], image_format)) {
410 return rgb32_format_map[i].format;
411 }
412 }
413 }
414
415 return GST_VIDEO_FORMAT_UNKNOWN;
416 }
417
418 struct ImageFormatArray
419 {
420 VAImageFormat *image_formats;
421 gint len;
422 };
423
424 static gpointer
425 fix_map (gpointer data)
426 {
427 struct ImageFormatArray *args = data;
428 GstVideoFormat format;
429 VAImageFormat *image_format;
430 struct FormatMap *map;
431 guint i;
432
433 for (i = 0; i < args->len; i++) {
434 image_format = &args->image_formats[i];
435 if (!va_format_is_rgb (image_format))
436 continue;
437 format = find_gst_video_format_in_rgb32_map (image_format);
438 if (format == GST_VIDEO_FORMAT_UNKNOWN)
439 continue;
440 map = get_format_map_from_video_format (format);
441 if (!map)
442 continue;
443 if (va_format_is_same (&map->va_format, image_format))
444 continue;
445
446 map->va_format = *image_format;
447
448 GST_INFO ("GST_VIDEO_FORMAT_%s => { fourcc %"
449 GST_FOURCC_FORMAT ", %s, bpp %d, depth %d, R %#010x, G %#010x, "
450 "B %#010x, A %#010x }", gst_video_format_to_string (map->format),
451 GST_FOURCC_ARGS (map->va_format.fourcc),
452 (map->va_format.byte_order == 1) ? "LSB" : "MSB",
453 map->va_format.bits_per_pixel, map->va_format.depth,
454 map->va_format.red_mask, map->va_format.green_mask,
455 map->va_format.blue_mask, map->va_format.alpha_mask);
456 }
457
458 return NULL;
459 }
460
461 /* XXX: RGB32 LSB VAImageFormats don't map statically with GStreamer
462 * color formats. Each driver does what they want.
463 *
464 * For MSB, there is no ambiguity: same order in define, memory and
465 * CPU. For example,
466 *
467 * RGBA is RGBA in memory and RGBA with channel mask R:0xFF0000
468 * G:0x00FF0000 B:0x0000FF00 A:0x000000FF in CPU.
469 *
470 * For LSB, CPU's perspective and memory's perspective are
471 * different. For example,
472 *
473 * From CPU's perspective, it's RGBA order in memory, but when it is
474 * stored in memory, because CPU's little endianness, it will be
475 * re-ordered, with mask R:0x000000FF G:0x0000FF00 B:0x00FF0000
476 * A:0xFF000000.
477 *
478 * In other words, from memory's perspective, RGBA LSB is equal as
479 * ABGR MSB.
480 *
481 * These definitions are mixed used all over the media system and we
482 * need to correct the mapping form VA video format to GStreamer
483 * video format in both manners.
484 *
485 * https://gitlab.freedesktop.org/gstreamer/gstreamer-vaapi/-/merge_requests/123
486 */
487 void
488 gst_va_video_format_fix_map (VAImageFormat * image_formats, gint num)
489 {
490 static GOnce once = G_ONCE_INIT;
491 struct ImageFormatArray args = { image_formats, num };
492
493 g_once (&once, fix_map, &args);
494 }