3 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
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.
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.
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.
23 * @short_description: utilities for dealing with OpenGL formats
24 * @see_also: #GstGLBaseMemory, #GstGLMemory, #GstGLFramebuffer, #GstGLBuffer
26 * Some useful utilities for converting between various formats and OpenGL
34 #include <gst/gl/gstglformat.h>
35 #include <gst/gl/gstglcontext.h>
37 #define USING_OPENGL(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL, 1, 0))
38 #define USING_OPENGL3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL3, 3, 1))
39 #define USING_GLES(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES, 1, 0))
40 #define USING_GLES2(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 2, 0))
41 #define USING_GLES3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 3, 0))
43 #ifndef GL_TEXTURE_RECTANGLE
44 #define GL_TEXTURE_RECTANGLE 0x84F5
46 #ifndef GL_TEXTURE_EXTERNAL_OES
47 #define GL_TEXTURE_EXTERNAL_OES 0x8D65
51 _gl_format_n_components (guint format)
54 case GST_VIDEO_GL_TEXTURE_TYPE_RGBA:
57 case GST_VIDEO_GL_TEXTURE_TYPE_RGB:
58 case GST_VIDEO_GL_TEXTURE_TYPE_RGB16:
61 case GST_VIDEO_GL_TEXTURE_TYPE_LUMINANCE_ALPHA:
62 case GST_VIDEO_GL_TEXTURE_TYPE_RG:
63 case GST_GL_LUMINANCE_ALPHA:
66 case GST_VIDEO_GL_TEXTURE_TYPE_LUMINANCE:
67 case GST_VIDEO_GL_TEXTURE_TYPE_R:
68 case GST_GL_LUMINANCE:
77 _gl_type_n_components (guint type)
80 case GL_UNSIGNED_BYTE:
82 case GL_UNSIGNED_SHORT_5_6_5:
85 g_assert_not_reached ();
91 _gl_type_n_bytes (guint type)
94 case GL_UNSIGNED_BYTE:
96 case GL_UNSIGNED_SHORT_5_6_5:
99 g_assert_not_reached ();
105 * gst_gl_format_type_n_bytes:
106 * @format: the OpenGL format, %GL_RGBA, %GL_LUMINANCE, etc
107 * @type: the OpenGL type, %GL_UNSIGNED_BYTE, %GL_FLOAT, etc
109 * Returns: the number of bytes the specified @format, @type combination takes
113 gst_gl_format_type_n_bytes (guint format, guint type)
115 return _gl_format_n_components (format) / _gl_type_n_components (type) *
116 _gl_type_n_bytes (type);
120 * gst_gl_format_from_video_info:
121 * @context: a #GstGLContext
122 * @vinfo: a #GstVideoInfo
123 * @plane: the plane number in @vinfo
125 * Returns: the #GstGLFormat necessary for holding the data in @plane of @vinfo
128 gst_gl_format_from_video_info (GstGLContext * context, GstVideoInfo * vinfo,
131 gboolean texture_rg =
132 gst_gl_context_check_feature (context, "GL_EXT_texture_rg")
133 || gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 3, 0)
134 || gst_gl_context_check_feature (context, "GL_ARB_texture_rg")
135 || gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL3, 3, 0);
136 GstVideoFormat v_format = GST_VIDEO_INFO_FORMAT (vinfo);
137 guint n_plane_components;
140 case GST_VIDEO_FORMAT_RGBx:
141 case GST_VIDEO_FORMAT_BGRx:
142 case GST_VIDEO_FORMAT_xRGB:
143 case GST_VIDEO_FORMAT_xBGR:
144 case GST_VIDEO_FORMAT_RGBA:
145 case GST_VIDEO_FORMAT_BGRA:
146 case GST_VIDEO_FORMAT_ARGB:
147 case GST_VIDEO_FORMAT_ABGR:
148 case GST_VIDEO_FORMAT_AYUV:
149 n_plane_components = 4;
151 case GST_VIDEO_FORMAT_RGB:
152 case GST_VIDEO_FORMAT_BGR:
153 n_plane_components = 3;
155 case GST_VIDEO_FORMAT_RGB16:
156 case GST_VIDEO_FORMAT_BGR16:
157 return GST_GL_RGB565;
158 case GST_VIDEO_FORMAT_GRAY16_BE:
159 case GST_VIDEO_FORMAT_GRAY16_LE:
160 case GST_VIDEO_FORMAT_YUY2:
161 case GST_VIDEO_FORMAT_UYVY:
162 n_plane_components = 2;
164 case GST_VIDEO_FORMAT_NV12:
165 case GST_VIDEO_FORMAT_NV21:
166 n_plane_components = plane == 0 ? 1 : 2;
168 case GST_VIDEO_FORMAT_GRAY8:
169 case GST_VIDEO_FORMAT_Y444:
170 case GST_VIDEO_FORMAT_Y42B:
171 case GST_VIDEO_FORMAT_Y41B:
172 case GST_VIDEO_FORMAT_I420:
173 case GST_VIDEO_FORMAT_YV12:
174 n_plane_components = 1;
177 n_plane_components = 4;
178 g_assert_not_reached ();
182 switch (n_plane_components) {
190 return texture_rg ? GST_GL_RG : GST_GL_LUMINANCE_ALPHA;
193 return texture_rg ? GST_GL_RED : GST_GL_LUMINANCE;
196 g_assert_not_reached ();
204 * gst_gl_sized_gl_format_from_gl_format_type:
205 * @context: a #GstGLContext
206 * @format: an OpenGL format, %GL_RGBA, %GL_LUMINANCE, etc
207 * @type: an OpenGL type, %GL_UNSIGNED_BYTE, %GL_FLOAT, etc
209 * Returns: the sized internal format specified by @format and @type that can
210 * be used in @context
213 gst_gl_sized_gl_format_from_gl_format_type (GstGLContext * context,
214 guint format, guint type)
216 gboolean ext_texture_rg =
217 gst_gl_context_check_feature (context, "GL_EXT_texture_rg");
222 case GL_UNSIGNED_BYTE:
223 return USING_GLES2 (context)
224 && !USING_GLES3 (context) ? GST_GL_RGBA : GST_GL_RGBA8;
230 case GL_UNSIGNED_BYTE:
231 return USING_GLES2 (context)
232 && !USING_GLES3 (context) ? GST_GL_RGB : GST_GL_RGB8;
234 case GL_UNSIGNED_SHORT_5_6_5:
241 case GL_UNSIGNED_BYTE:
242 if (!USING_GLES3 (context) && USING_GLES2 (context) && ext_texture_rg)
250 case GL_UNSIGNED_BYTE:
251 if (!USING_GLES3 (context) && USING_GLES2 (context) && ext_texture_rg)
261 case GST_GL_LUMINANCE:
262 case GST_GL_LUMINANCE_ALPHA:
264 case GST_GL_DEPTH_COMPONENT16:
265 case GST_GL_DEPTH24_STENCIL8:
271 g_assert_not_reached ();
276 * gst_gl_texture_target_to_string:
277 * @target: a #GstGLTextureTarget
279 * Returns: the stringified version of @target or %NULL
282 gst_gl_texture_target_to_string (GstGLTextureTarget target)
285 case GST_GL_TEXTURE_TARGET_2D:
286 return GST_GL_TEXTURE_TARGET_2D_STR;
287 case GST_GL_TEXTURE_TARGET_RECTANGLE:
288 return GST_GL_TEXTURE_TARGET_RECTANGLE_STR;
289 case GST_GL_TEXTURE_TARGET_EXTERNAL_OES:
290 return GST_GL_TEXTURE_TARGET_EXTERNAL_OES_STR;
297 * gst_gl_texture_target_from_string:
298 * @str: a string equivalant to one of the GST_GL_TEXTURE_TARGET_*_STR values
300 * Returns: the #GstGLTextureTarget represented by @str or
301 * %GST_GL_TEXTURE_TARGET_NONE
304 gst_gl_texture_target_from_string (const gchar * str)
307 return GST_GL_TEXTURE_TARGET_NONE;
309 if (g_strcmp0 (str, GST_GL_TEXTURE_TARGET_2D_STR) == 0)
310 return GST_GL_TEXTURE_TARGET_2D;
311 if (g_strcmp0 (str, GST_GL_TEXTURE_TARGET_RECTANGLE_STR) == 0)
312 return GST_GL_TEXTURE_TARGET_RECTANGLE;
313 if (g_strcmp0 (str, GST_GL_TEXTURE_TARGET_EXTERNAL_OES_STR) == 0)
314 return GST_GL_TEXTURE_TARGET_EXTERNAL_OES;
316 return GST_GL_TEXTURE_TARGET_NONE;
320 * gst_gl_texture_target_to_gl:
321 * @target: a #GstGLTextureTarget
323 * Returns: the OpenGL value for binding the @target with glBindTexture() and
324 * similar functions or 0
327 gst_gl_texture_target_to_gl (GstGLTextureTarget target)
330 case GST_GL_TEXTURE_TARGET_2D:
331 return GL_TEXTURE_2D;
332 case GST_GL_TEXTURE_TARGET_RECTANGLE:
333 return GL_TEXTURE_RECTANGLE;
334 case GST_GL_TEXTURE_TARGET_EXTERNAL_OES:
335 return GL_TEXTURE_EXTERNAL_OES;
342 * gst_gl_texture_target_from_gl:
343 * @target: an OpenGL texture binding target
345 * Returns: the #GstGLTextureTarget that's equiavalant to @target or
346 * %GST_GL_TEXTURE_TARGET_NONE
349 gst_gl_texture_target_from_gl (guint target)
353 return GST_GL_TEXTURE_TARGET_2D;
354 case GL_TEXTURE_RECTANGLE:
355 return GST_GL_TEXTURE_TARGET_RECTANGLE;
356 case GL_TEXTURE_EXTERNAL_OES:
357 return GST_GL_TEXTURE_TARGET_EXTERNAL_OES;
359 return GST_GL_TEXTURE_TARGET_NONE;
364 * gst_gl_texture_target_to_buffer_pool_option:
365 * @target: a #GstGLTextureTarget
367 * Returns: a string representing the @GstBufferPoolOption specified by @target
370 gst_gl_texture_target_to_buffer_pool_option (GstGLTextureTarget target)
373 case GST_GL_TEXTURE_TARGET_2D:
374 return GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_2D;
375 case GST_GL_TEXTURE_TARGET_RECTANGLE:
376 return GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_RECTANGLE;
377 case GST_GL_TEXTURE_TARGET_EXTERNAL_OES:
378 return GST_BUFFER_POOL_OPTION_GL_TEXTURE_TARGET_EXTERNAL_OES;