gstglformat.c 15.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * GStreamer
 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

Matthew Waters's avatar
Matthew Waters committed
21 22
/**
 * SECTION:gstglformat
23
 * @title: GstGLFormat
Matthew Waters's avatar
Matthew Waters committed
24 25 26 27 28 29 30
 * @short_description: utilities for dealing with OpenGL formats
 * @see_also: #GstGLBaseMemory, #GstGLMemory, #GstGLFramebuffer, #GstGLBuffer
 *
 * Some useful utilities for converting between various formats and OpenGL
 * formats.
 */

31 32 33 34
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

35 36 37 38
#include "gstglformat.h"

#include "gstglcontext.h"
#include "gstglfuncs.h"
39 40 41 42 43 44 45

#define USING_OPENGL(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL, 1, 0))
#define USING_OPENGL3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL3, 3, 1))
#define USING_GLES(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES, 1, 0))
#define USING_GLES2(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 2, 0))
#define USING_GLES3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 3, 0))

46 47 48 49 50 51 52
#ifndef GL_TEXTURE_RECTANGLE
#define GL_TEXTURE_RECTANGLE 0x84F5
#endif
#ifndef GL_TEXTURE_EXTERNAL_OES
#define GL_TEXTURE_EXTERNAL_OES 0x8D65
#endif

53 54 55 56 57
static inline guint
_gl_format_n_components (guint format)
{
  switch (format) {
    case GST_VIDEO_GL_TEXTURE_TYPE_RGBA:
58
    case GST_GL_RGBA:
59
    case GST_GL_RGBA8:
60
    case GST_GL_RGBA16:
61 62 63
      return 4;
    case GST_VIDEO_GL_TEXTURE_TYPE_RGB:
    case GST_VIDEO_GL_TEXTURE_TYPE_RGB16:
64
    case GST_GL_RGB:
65
    case GST_GL_RGB8:
66
    case GST_GL_RGB16:
67
    case GST_GL_RGB565:
68 69 70
      return 3;
    case GST_VIDEO_GL_TEXTURE_TYPE_LUMINANCE_ALPHA:
    case GST_VIDEO_GL_TEXTURE_TYPE_RG:
71 72
    case GST_GL_LUMINANCE_ALPHA:
    case GST_GL_RG:
73
    case GST_GL_RG8:
74 75 76
      return 2;
    case GST_VIDEO_GL_TEXTURE_TYPE_LUMINANCE:
    case GST_VIDEO_GL_TEXTURE_TYPE_R:
77
    case GST_GL_LUMINANCE:
78
    case GST_GL_ALPHA:
79
    case GST_GL_RED:
80
    case GST_GL_R8:
81 82 83 84 85 86 87 88 89 90 91
      return 1;
    default:
      return 0;
  }
}

static inline guint
_gl_type_n_components (guint type)
{
  switch (type) {
    case GL_UNSIGNED_BYTE:
92
    case GL_UNSIGNED_SHORT:
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
      return 1;
    case GL_UNSIGNED_SHORT_5_6_5:
      return 3;
    default:
      g_assert_not_reached ();
      return 0;
  }
}

static inline guint
_gl_type_n_bytes (guint type)
{
  switch (type) {
    case GL_UNSIGNED_BYTE:
      return 1;
108
    case GL_UNSIGNED_SHORT:
109 110 111 112 113 114 115 116
    case GL_UNSIGNED_SHORT_5_6_5:
      return 2;
    default:
      g_assert_not_reached ();
      return 0;
  }
}

117 118 119 120 121 122 123 124
/**
 * gst_gl_format_type_n_bytes:
 * @format: the OpenGL format, %GL_RGBA, %GL_LUMINANCE, etc
 * @type: the OpenGL type, %GL_UNSIGNED_BYTE, %GL_FLOAT, etc
 *
 * Returns: the number of bytes the specified @format, @type combination takes
 * per pixel
 */
125 126 127 128 129 130 131
guint
gst_gl_format_type_n_bytes (guint format, guint type)
{
  return _gl_format_n_components (format) / _gl_type_n_components (type) *
      _gl_type_n_bytes (type);
}

132
/**
133
 * gst_gl_format_from_video_info:
134
 * @context: a #GstGLContext
135 136
 * @vinfo: a #GstVideoInfo
 * @plane: the plane number in @vinfo
137
 *
138
 * Returns: the #GstGLFormat necessary for holding the data in @plane of @vinfo
139
 */
140 141 142
GstGLFormat
gst_gl_format_from_video_info (GstGLContext * context, GstVideoInfo * vinfo,
    guint plane)
143 144 145 146 147 148
{
  gboolean texture_rg =
      gst_gl_context_check_feature (context, "GL_EXT_texture_rg")
      || gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 3, 0)
      || gst_gl_context_check_feature (context, "GL_ARB_texture_rg")
      || gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL3, 3, 0);
149
  GstVideoFormat v_format = GST_VIDEO_INFO_FORMAT (vinfo);
150 151 152 153 154 155 156 157 158 159 160 161
  guint n_plane_components;

  switch (v_format) {
    case GST_VIDEO_FORMAT_RGBx:
    case GST_VIDEO_FORMAT_BGRx:
    case GST_VIDEO_FORMAT_xRGB:
    case GST_VIDEO_FORMAT_xBGR:
    case GST_VIDEO_FORMAT_RGBA:
    case GST_VIDEO_FORMAT_BGRA:
    case GST_VIDEO_FORMAT_ARGB:
    case GST_VIDEO_FORMAT_ABGR:
    case GST_VIDEO_FORMAT_AYUV:
162
    case GST_VIDEO_FORMAT_VUYA:
163 164
      n_plane_components = 4;
      break;
165 166
    case GST_VIDEO_FORMAT_ARGB64:
      return GST_GL_RGBA16;
167 168 169 170 171 172
    case GST_VIDEO_FORMAT_RGB:
    case GST_VIDEO_FORMAT_BGR:
      n_plane_components = 3;
      break;
    case GST_VIDEO_FORMAT_RGB16:
    case GST_VIDEO_FORMAT_BGR16:
173
      return GST_GL_RGB565;
174
      break;
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
    case GST_VIDEO_FORMAT_GRAY16_BE:
    case GST_VIDEO_FORMAT_GRAY16_LE:
    case GST_VIDEO_FORMAT_YUY2:
    case GST_VIDEO_FORMAT_UYVY:
      n_plane_components = 2;
      break;
    case GST_VIDEO_FORMAT_NV12:
    case GST_VIDEO_FORMAT_NV21:
      n_plane_components = plane == 0 ? 1 : 2;
      break;
    case GST_VIDEO_FORMAT_GRAY8:
    case GST_VIDEO_FORMAT_Y444:
    case GST_VIDEO_FORMAT_Y42B:
    case GST_VIDEO_FORMAT_Y41B:
    case GST_VIDEO_FORMAT_I420:
    case GST_VIDEO_FORMAT_YV12:
      n_plane_components = 1;
      break;
    default:
      n_plane_components = 4;
      g_assert_not_reached ();
      break;
  }

  switch (n_plane_components) {
    case 4:
201
      return GST_GL_RGBA;
202
    case 3:
203
      return GST_GL_RGB;
204
    case 2:
205
      return texture_rg ? GST_GL_RG : GST_GL_LUMINANCE_ALPHA;
206
    case 1:
207
      return texture_rg ? GST_GL_RED : GST_GL_LUMINANCE;
208 209 210 211
    default:
      break;
  }

212 213
  g_critical ("Unknown video format 0x%x provided", v_format);
  return 0;
214 215
}

216 217 218 219 220 221 222 223 224
/**
 * gst_gl_sized_gl_format_from_gl_format_type:
 * @context: a #GstGLContext
 * @format: an OpenGL format, %GL_RGBA, %GL_LUMINANCE, etc
 * @type: an OpenGL type, %GL_UNSIGNED_BYTE, %GL_FLOAT, etc
 *
 * Returns: the sized internal format specified by @format and @type that can
 *          be used in @context
 */
225 226 227 228 229 230 231 232
guint
gst_gl_sized_gl_format_from_gl_format_type (GstGLContext * context,
    guint format, guint type)
{
  gboolean ext_texture_rg =
      gst_gl_context_check_feature (context, "GL_EXT_texture_rg");

  switch (format) {
233
    case GST_GL_RGBA:
234 235 236
      switch (type) {
        case GL_UNSIGNED_BYTE:
          return USING_GLES2 (context)
237
              && !USING_GLES3 (context) ? GST_GL_RGBA : GST_GL_RGBA8;
238
          break;
239 240
        case GL_UNSIGNED_SHORT:
          return GST_GL_RGBA16;
241 242
      }
      break;
243
    case GST_GL_RGB:
244 245
      switch (type) {
        case GL_UNSIGNED_BYTE:
246 247
          return USING_GLES2 (context)
              && !USING_GLES3 (context) ? GST_GL_RGB : GST_GL_RGB8;
248 249
          break;
        case GL_UNSIGNED_SHORT_5_6_5:
250
          return GST_GL_RGB565;
251 252
        case GL_UNSIGNED_SHORT:
          return GST_GL_RGB16;
253 254
      }
      break;
255
    case GST_GL_RG:
256 257 258
      switch (type) {
        case GL_UNSIGNED_BYTE:
          if (!USING_GLES3 (context) && USING_GLES2 (context) && ext_texture_rg)
259 260
            return GST_GL_RG;
          return GST_GL_RG8;
261 262 263
          break;
      }
      break;
264
    case GST_GL_RED:
265 266 267
      switch (type) {
        case GL_UNSIGNED_BYTE:
          if (!USING_GLES3 (context) && USING_GLES2 (context) && ext_texture_rg)
268 269
            return GST_GL_RED;
          return GST_GL_R8;
270 271 272
          break;
      }
      break;
273
    case GST_GL_RGBA8:
274
    case GST_GL_RGBA16:
275
    case GST_GL_RGB8:
276
    case GST_GL_RGB16:
277
    case GST_GL_RGB565:
278 279 280 281 282 283 284 285
    case GST_GL_RG8:
    case GST_GL_R8:
    case GST_GL_LUMINANCE:
    case GST_GL_LUMINANCE_ALPHA:
    case GST_GL_ALPHA:
    case GST_GL_DEPTH_COMPONENT16:
    case GST_GL_DEPTH24_STENCIL8:
      return format;
286
    default:
287 288
      g_critical ("Unknown GL format 0x%x type 0x%x provided", format, type);
      return format;
289 290 291 292 293 294
  }

  g_assert_not_reached ();
  return 0;
}

295 296 297 298 299 300 301 302
/**
 * gst_gl_format_type_from_sized_gl_format:
 * @format: the sized internal #GstGLFormat
 * @unsized_format: (out): location for the resulting unsized #GstGLFormat
 * @gl_type: (out): location for the resulting GL type
 *
 * Get the unsized format and type from @format for usage in glReadPixels,
 * glTex{Sub}Image*, glTexImage* and similar functions.
303 304
 *
 * Since: 1.16
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
 */
void
gst_gl_format_type_from_sized_gl_format (GstGLFormat format,
    GstGLFormat * unsized_format, guint * gl_type)
{
  g_return_if_fail (unsized_format != NULL);
  g_return_if_fail (gl_type != NULL);

  switch (format) {
    case GST_GL_RGBA8:
      *unsized_format = GST_GL_RGBA;
      *gl_type = GL_UNSIGNED_BYTE;
      break;
    case GST_GL_RGB8:
      *unsized_format = GST_GL_RGB;
      *gl_type = GL_UNSIGNED_BYTE;
      break;
322 323 324 325 326 327 328 329
    case GST_GL_RGBA16:
      *unsized_format = GST_GL_RGBA;
      *gl_type = GL_UNSIGNED_SHORT;
      break;
    case GST_GL_RGB16:
      *unsized_format = GST_GL_RGB;
      *gl_type = GL_UNSIGNED_SHORT;
      break;
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    case GST_GL_RGB565:
      *unsized_format = GST_GL_RGB;
      *gl_type = GL_UNSIGNED_SHORT_5_6_5;
      break;
    case GST_GL_RG8:
      *unsized_format = GST_GL_RG;
      *gl_type = GL_UNSIGNED_BYTE;
      break;
    case GST_GL_R8:
      *unsized_format = GST_GL_RED;
      *gl_type = GL_UNSIGNED_BYTE;
      break;
    case GST_GL_RGBA:
    case GST_GL_RGB:
    case GST_GL_RG:
    case GST_GL_RED:
    case GST_GL_LUMINANCE:
    case GST_GL_LUMINANCE_ALPHA:
    case GST_GL_ALPHA:
      *unsized_format = format;
      *gl_type = GL_UNSIGNED_BYTE;
      break;
    default:
353 354 355
      g_critical ("Unknown GL format 0x%x provided", format);
      *unsized_format = format;
      *gl_type = GL_UNSIGNED_BYTE;
356 357 358 359
      return;
  }
}

360 361 362 363 364 365 366
/**
 * gst_gl_format_is_supported:
 * @context: a #GstGLContext
 * @format: the #GstGLFormat to check is supported by @context
 *
 * Returns: Whether @format is supported by @context based on the OpenGL API,
 *          version, or available OpenGL extension/s.
367 368
 *
 * Since: 1.16
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
 */
gboolean
gst_gl_format_is_supported (GstGLContext * context, GstGLFormat format)
{
  g_return_val_if_fail (GST_IS_GL_CONTEXT (context), FALSE);

  switch (format) {
    case GST_GL_RGBA:
    case GST_GL_RGB:
      return TRUE;
    case GST_GL_LUMINANCE:
    case GST_GL_ALPHA:
    case GST_GL_LUMINANCE_ALPHA:
      /* deprecated/removed in core GL3 contexts */
      return USING_OPENGL (context) || USING_GLES2 (context);
    case GST_GL_RG:
    case GST_GL_RED:
      return gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 3, 0)
          || gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL3, 3, 0)
          || gst_gl_context_check_feature (context, "GL_EXT_texture_rg")
          || gst_gl_context_check_feature (context, "GL_ARB_texture_rg");
    case GST_GL_R8:
    case GST_GL_RG8:
      return USING_GLES3 (context)
          || gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL3, 3, 0)
          || gst_gl_context_check_feature (context, "GL_ARB_texture_rg");
    case GST_GL_RGB8:
    case GST_GL_RGBA8:
      return (USING_GLES3 (context) && !USING_GLES2 (context))
          || USING_OPENGL (context) || USING_OPENGL3 (context);
    case GST_GL_RGB16:
    case GST_GL_RGBA16:
      return USING_OPENGL (context) || USING_OPENGL3 (context)
          || USING_GLES3 (context);
    case GST_GL_RGB565:
      return USING_GLES2 (context) || (USING_OPENGL3 (context)
          && gst_gl_context_check_feature (context,
              "GL_ARB_ES2_compatibility"));
    case GST_GL_DEPTH_COMPONENT16:
      return gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL, 1, 4)
          || USING_GLES2 (context)
          || gst_gl_context_check_feature (context, "GL_ARB_depth_texture")
          || gst_gl_context_check_feature (context, "GL_OES_depth_texture");
    case GST_GL_DEPTH24_STENCIL8:
      return gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL, 3, 0)
          || USING_GLES3 (context)
          || gst_gl_context_check_feature (context,
          "GL_OES_packed_depth_stencil")
          || gst_gl_context_check_feature (context,
          "GL_EXT_packed_depth_stencil");
    default:
      g_assert_not_reached ();
      return FALSE;
  }
}

425 426 427 428 429 430
/**
 * gst_gl_texture_target_to_string:
 * @target: a #GstGLTextureTarget
 *
 * Returns: the stringified version of @target or %NULL
 */
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
const gchar *
gst_gl_texture_target_to_string (GstGLTextureTarget target)
{
  switch (target) {
    case GST_GL_TEXTURE_TARGET_2D:
      return GST_GL_TEXTURE_TARGET_2D_STR;
    case GST_GL_TEXTURE_TARGET_RECTANGLE:
      return GST_GL_TEXTURE_TARGET_RECTANGLE_STR;
    case GST_GL_TEXTURE_TARGET_EXTERNAL_OES:
      return GST_GL_TEXTURE_TARGET_EXTERNAL_OES_STR;
    default:
      return NULL;
  }
}

446
/**
Matthew Waters's avatar
Matthew Waters committed
447
 * gst_gl_texture_target_from_string:
448 449 450 451 452
 * @str: a string equivalant to one of the GST_GL_TEXTURE_TARGET_*_STR values
 *
 * Returns: the #GstGLTextureTarget represented by @str or
 *          %GST_GL_TEXTURE_TARGET_NONE
 */
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
GstGLTextureTarget
gst_gl_texture_target_from_string (const gchar * str)
{
  if (!str)
    return GST_GL_TEXTURE_TARGET_NONE;

  if (g_strcmp0 (str, GST_GL_TEXTURE_TARGET_2D_STR) == 0)
    return GST_GL_TEXTURE_TARGET_2D;
  if (g_strcmp0 (str, GST_GL_TEXTURE_TARGET_RECTANGLE_STR) == 0)
    return GST_GL_TEXTURE_TARGET_RECTANGLE;
  if (g_strcmp0 (str, GST_GL_TEXTURE_TARGET_EXTERNAL_OES_STR) == 0)
    return GST_GL_TEXTURE_TARGET_EXTERNAL_OES;

  return GST_GL_TEXTURE_TARGET_NONE;
}

469 470 471 472 473 474 475
/**
 * gst_gl_texture_target_to_gl:
 * @target: a #GstGLTextureTarget
 *
 * Returns: the OpenGL value for binding the @target with glBindTexture() and
 *          similar functions or 0
 */
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
guint
gst_gl_texture_target_to_gl (GstGLTextureTarget target)
{
  switch (target) {
    case GST_GL_TEXTURE_TARGET_2D:
      return GL_TEXTURE_2D;
    case GST_GL_TEXTURE_TARGET_RECTANGLE:
      return GL_TEXTURE_RECTANGLE;
    case GST_GL_TEXTURE_TARGET_EXTERNAL_OES:
      return GL_TEXTURE_EXTERNAL_OES;
    default:
      return 0;
  }
}

491 492 493 494 495 496 497
/**
 * gst_gl_texture_target_from_gl:
 * @target: an OpenGL texture binding target
 *
 * Returns: the #GstGLTextureTarget that's equiavalant to @target or
 *          %GST_GL_TEXTURE_TARGET_NONE
 */
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
GstGLTextureTarget
gst_gl_texture_target_from_gl (guint target)
{
  switch (target) {
    case GL_TEXTURE_2D:
      return GST_GL_TEXTURE_TARGET_2D;
    case GL_TEXTURE_RECTANGLE:
      return GST_GL_TEXTURE_TARGET_RECTANGLE;
    case GL_TEXTURE_EXTERNAL_OES:
      return GST_GL_TEXTURE_TARGET_EXTERNAL_OES;
    default:
      return GST_GL_TEXTURE_TARGET_NONE;
  }
}

513 514 515 516 517 518
/**
 * gst_gl_texture_target_to_buffer_pool_option:
 * @target: a #GstGLTextureTarget
 *
 * Returns: a string representing the @GstBufferPoolOption specified by @target
 */
519 520 521 522 523 524 525 526 527 528 529 530 531 532
const gchar *
gst_gl_texture_target_to_buffer_pool_option (GstGLTextureTarget target)
{
  switch (target) {
    case GST_GL_TEXTURE_TARGET_2D:
      return GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_2D;
    case GST_GL_TEXTURE_TARGET_RECTANGLE:
      return GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_RECTANGLE;
    case GST_GL_TEXTURE_TARGET_EXTERNAL_OES:
      return GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_EXTERNAL_OES;
    default:
      return NULL;
  }
}