summaryrefslogtreecommitdiff
path: root/gst-libs/gst/vaapi/video-format.c
blob: a798cd995d5a18c23f8173f74bb85ecfd1970723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 *  video-format.h - Video format helpers for VA-API
 *
 *  Copyright (C) 2010-2011 Splitted-Desktop Systems
 *    Author: Gwenole Beauchesne <gwenole.beauchesne@splitted-desktop.com>
 *  Copyright (C) 2011-2013 Intel Corporation
 *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1
 *  of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301 USA
 */

/**
 * SECTION:videoformat
 * @short_description: Video format helpers for VA-API
 */

#include "sysdeps.h"
#include "gstvaapicompat.h"
#include "gstvaapisurface.h"
#include "video-format.h"

#if G_BYTE_ORDER == G_BIG_ENDIAN
# define VIDEO_VA_ENDIANESS VA_MSB_FIRST
#elif G_BYTE_ORDER == G_LITTLE_ENDIAN
# define VIDEO_VA_ENDIANESS VA_LSB_FIRST
#endif

typedef struct
{
  GstVideoFormat format;
  GstVaapiChromaType chroma_type;
  VAImageFormat va_format;
} GstVideoFormatMap;

#define DEF_YUV(FORMAT, FOURCC, BPP, SUB)                               \
  { G_PASTE(GST_VIDEO_FORMAT_,FORMAT),                                  \
    G_PASTE(GST_VAAPI_CHROMA_TYPE_YUV,SUB),                             \
    { VA_FOURCC FOURCC, VIDEO_VA_ENDIANESS, BPP, }, }

#define DEF_RGB(FORMAT, FOURCC, BPP, DEPTH, R,G,B,A)                    \
  { G_PASTE(GST_VIDEO_FORMAT_,FORMAT),                                  \
    G_PASTE(GST_VAAPI_CHROMA_TYPE_RGB,BPP),                             \
    { VA_FOURCC FOURCC, VIDEO_VA_ENDIANESS, BPP, DEPTH, R,G,B,A }, }

/* Image formats, listed in HW order preference */
/* *INDENT-OFF* */
static const GstVideoFormatMap gst_vaapi_video_formats[] = {
  /* YUV formats */
  DEF_YUV (NV12, ('N', 'V', '1', '2'), 12, 420),
  DEF_YUV (YV12, ('Y', 'V', '1', '2'), 12, 420),
  DEF_YUV (I420, ('I', '4', '2', '0'), 12, 420),
  DEF_YUV (YUY2, ('Y', 'U', 'Y', '2'), 16, 422),
  DEF_YUV (UYVY, ('U', 'Y', 'V', 'Y'), 16, 422),
  DEF_YUV (Y210, ('Y', '2', '1', '0'), 32, 422_10BPP),
  DEF_YUV (Y410, ('Y', '4', '1', '0'), 32, 444_10BPP),
  DEF_YUV (AYUV, ('A', 'Y', 'U', 'V'), 32, 444),
  DEF_YUV (Y444, ('4', '4', '4', 'P'), 24, 444),
  DEF_YUV (GRAY8, ('Y', '8', '0', '0'), 8, 400),
  DEF_YUV (P010_10LE, ('P', '0', '1', '0'), 24, 420_10BPP),
  /* RGB formats */
  DEF_RGB (ARGB, ('A', 'R', 'G', 'B'), 32,
      32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000),
  DEF_RGB (ABGR, ('A', 'B', 'G', 'R'), 32,
      32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000),
  DEF_RGB (xRGB, ('X', 'R', 'G', 'B'), 32,
      24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000),
  DEF_RGB (xBGR, ('X', 'B', 'G', 'R'), 32,
      24, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000),
  DEF_RGB (BGRA, ('B', 'G', 'R', 'A'), 32,
      32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000),
  DEF_RGB (RGBA, ('R', 'G', 'B', 'A'), 32,
      32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000),
  DEF_RGB (BGRx, ('B', 'G', 'R', 'X'), 32,
      24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000),
  DEF_RGB (RGBx, ('R', 'G', 'B', 'X'), 32,
      24, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000),
  DEF_RGB (RGB16, ('R', 'G', '1', '6'), 16,
      16, 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000),
  DEF_RGB (RGB, ('R', 'G', '2', '4'), 32,
      24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000),
  {0,}
};
/* *INDENT-ON* */

#undef DEF_RGB
#undef DEF_YUV

static inline gboolean
va_format_is_rgb (const VAImageFormat * va_format)
{
  return va_format->depth != 0;
}

static inline gboolean
va_format_is_yuv (const VAImageFormat * va_format)
{
  return va_format->depth == 0;
}

static inline gboolean
va_format_is_same_rgb (const VAImageFormat * fmt1, const VAImageFormat * fmt2)
{
  return (fmt1->byte_order == fmt2->byte_order &&
      fmt1->red_mask == fmt2->red_mask &&
      fmt1->green_mask == fmt2->green_mask &&
      fmt1->blue_mask == fmt2->blue_mask &&
      fmt1->alpha_mask == fmt2->alpha_mask);
}

static inline gboolean
va_format_is_same (const VAImageFormat * fmt1, const VAImageFormat * fmt2)
{
  if (fmt1->fourcc != fmt2->fourcc)
    return FALSE;
  return va_format_is_rgb (fmt1) ? va_format_is_same_rgb (fmt1, fmt2) : TRUE;
}

static const GstVideoFormatMap *
get_map (GstVideoFormat format)
{
  const GstVideoFormatMap *m;

  for (m = gst_vaapi_video_formats; m->format; m++) {
    if (m->format == format)
      return m;
  }
  return NULL;
}

/**
 * gst_vaapi_video_format_to_string:
 * @format: a #GstVideoFormat
 *
 * Returns the string representation of the @format argument.
 *
 * Return value: string representation of @format, or %NULL if unknown
 *   or unsupported.
 */
const gchar *
gst_vaapi_video_format_to_string (GstVideoFormat format)
{
  return gst_video_format_to_string (format);
}

/**
 * gst_vaapi_video_format_is_rgb:
 * @format: a #GstVideoFormat
 *
 * Checks whether the format is an RGB format.
 *
 * Return value: %TRUE if @format is RGB format
 */
gboolean
gst_vaapi_video_format_is_rgb (GstVideoFormat format)
{
  const GstVideoFormatMap *const m = get_map (format);

  return m && va_format_is_rgb (&m->va_format);
}

/**
 * gst_vaapi_video_format_is_yuv:
 * @format: a #GstVideoFormat
 *
 * Checks whether the format is an YUV format.
 *
 * Return value: %TRUE if @format is YUV format
 */
gboolean
gst_vaapi_video_format_is_yuv (GstVideoFormat format)
{
  const GstVideoFormatMap *const m = get_map (format);

  return m && va_format_is_yuv (&m->va_format);
}

/**
 * gst_vaapi_video_format_from_va_fourcc:
 * @fourcc: a FOURCC value
 *
 * Converts a VA fourcc into the corresponding #GstVideoFormat. If no
 * matching fourcc was found, then zero is returned.
 *
 * Return value: the #GstVideoFormat corresponding to the VA @fourcc
 */
GstVideoFormat
gst_vaapi_video_format_from_va_fourcc (guint32 fourcc)
{
  const GstVideoFormatMap *m;

  /* Note: VA fourcc values are now standardized and shall represent
     a unique format. The associated VAImageFormat is just a hint to
     determine RGBA component ordering */
  for (m = gst_vaapi_video_formats; m->format; m++) {
    if (m->va_format.fourcc == fourcc)
      return m->format;
  }
  return GST_VIDEO_FORMAT_UNKNOWN;
}

/**
 * gst_vaapi_video_format_from_va_format:
 * @va_format: a #VAImageFormat
 *
 * Converts a VA image format into the corresponding #GstVideoFormat.
 * If the image format cannot be represented by #GstVideoFormat,
 * then zero is returned.
 *
 * Return value: the #GstVideoFormat describing the @va_format
 */
GstVideoFormat
gst_vaapi_video_format_from_va_format (const VAImageFormat * va_format)
{
  const GstVideoFormatMap *m;

  for (m = gst_vaapi_video_formats; m->format; m++) {
    if (va_format_is_same (&m->va_format, va_format))
      return m->format;
  }
  return GST_VIDEO_FORMAT_UNKNOWN;
}

/**
 * gst_vaapi_video_format_to_va_format:
 * @format: a #GstVideoFormat
 *
 * Converts a #GstVideoFormat into the corresponding VA image
 * format. If no matching VA image format was found, %NULL is returned
 * and this error must be reported to be fixed.
 *
 * Return value: the VA image format, or %NULL if none was found
 */
const VAImageFormat *
gst_vaapi_video_format_to_va_format (GstVideoFormat format)
{
  const GstVideoFormatMap *const m = get_map (format);

  return m ? &m->va_format : NULL;
}

/**
 * gst_vaapi_video_format_get_chroma_type:
 * @format: a #GstVideoFormat
 *
 * Converts a #GstVideoFormat into the corresponding #GstVaapiChromaType
 * format.
 *
 * Return value: the #GstVaapiChromaType format, or zero if no match
 *   was found.
 */
guint
gst_vaapi_video_format_get_chroma_type (GstVideoFormat format)
{
  const GstVideoFormatMap *const m = get_map (format);

  return m ? m->chroma_type : 0;
}

/**
 * gst_vaapi_video_format_get_score:
 * @format: a #GstVideoFormat
 *
 * Determines how "native" is this @format. The lower is the returned
 * score, the best format this is for the underlying hardware.
 *
 * Return value: the @format score, or %G_MAXUINT if none was found
 */
guint
gst_vaapi_video_format_get_score (GstVideoFormat format)
{
  const GstVideoFormatMap *const m = get_map (format);

  return m ? (m - &gst_vaapi_video_formats[0]) : G_MAXUINT;
}

/**
 * gst_vaapi_video_format_from_chroma:
 * @chroma_type: a #GstVaapiChromaType
 *
 * Returns the "preferred" pixel format that matches with
 * @chroma_type.
 *
 * Returns: the preferred pixel format for @chroma_type
 **/
GstVideoFormat
gst_vaapi_video_format_from_chroma (guint chroma_type)
{
  switch (chroma_type) {
    case GST_VAAPI_CHROMA_TYPE_YUV422:
      return GST_VIDEO_FORMAT_YUY2;
    case GST_VAAPI_CHROMA_TYPE_YUV400:
      return GST_VIDEO_FORMAT_GRAY8;
    case GST_VAAPI_CHROMA_TYPE_YUV420:
    case GST_VAAPI_CHROMA_TYPE_RGB32:  /* GstVideoGLTextureUploadMeta */
      return GST_VIDEO_FORMAT_NV12;
    case GST_VAAPI_CHROMA_TYPE_YUV420_10BPP:
      return GST_VIDEO_FORMAT_P010_10LE;
    case GST_VAAPI_CHROMA_TYPE_YUV444:
      return GST_VIDEO_FORMAT_AYUV;
    case GST_VAAPI_CHROMA_TYPE_YUV422_10BPP:
      return GST_VIDEO_FORMAT_Y210;
    case GST_VAAPI_CHROMA_TYPE_YUV444_10BPP:
      return GST_VIDEO_FORMAT_Y410;
    default:
      return GST_VIDEO_FORMAT_UNKNOWN;
  }
}

/**
 * gst_vaapi_video_format_get_best_native:
 * @format: a #GstVideoFormat
 *
 * Returns the best "native" pixel format that matches a particular
 * color-space.
 *
 * Returns: the #GstVideoFormat with the corresponding best native
 * format for #GstVaapiSurface
 **/
GstVideoFormat
gst_vaapi_video_format_get_best_native (GstVideoFormat format)
{
  GstVaapiChromaType chroma_type;

  if (format == GST_VIDEO_FORMAT_ENCODED)
    return GST_VIDEO_FORMAT_NV12;

  chroma_type = gst_vaapi_video_format_get_chroma_type (format);
  return gst_vaapi_video_format_from_chroma (chroma_type);
}