3 * Copyright (C) 2012-2014 Matthew Waters <ystree00@gmail.com>
4 * Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
29 #include "gstglupload.h"
31 #if GST_GL_HAVE_PLATFORM_EGL
32 #include "egl/gsteglimage.h"
33 #include "egl/gstglmemoryegl.h"
34 #include "egl/gstglcontext_egl.h"
37 #if GST_GL_HAVE_DMABUF
38 #include <gst/allocators/gstdmabuf.h>
41 #if GST_GL_HAVE_VIV_DIRECTVIV
42 #include <gst/allocators/gstphysmemory.h>
43 #include <gst/gl/gstglfuncs.h>
49 * @short_description: an object that uploads to GL textures
50 * @see_also: #GstGLDownload, #GstGLMemory
52 * #GstGLUpload is an object that uploads data from system memory into GL textures.
54 * A #GstGLUpload can be created with gst_gl_upload_new()
57 #define USING_OPENGL(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL, 1, 0))
58 #define USING_OPENGL3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL3, 3, 1))
59 #define USING_GLES(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES, 1, 0))
60 #define USING_GLES2(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 2, 0))
61 #define USING_GLES3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 3, 0))
63 GST_DEBUG_CATEGORY_STATIC (gst_gl_upload_debug);
64 #define GST_CAT_DEFAULT gst_gl_upload_debug
66 static void gst_gl_upload_finalize (GObject * object);
68 static GstGLTextureTarget
69 _caps_get_texture_target (GstCaps * caps, GstGLTextureTarget default_target)
71 GstGLTextureTarget ret = 0;
72 GstStructure *s = gst_caps_get_structure (caps, 0);
74 if (gst_structure_has_field_typed (s, "texture-target", G_TYPE_STRING)) {
75 const gchar *target_str = gst_structure_get_string (s, "texture-target");
76 ret = gst_gl_texture_target_from_string (target_str);
85 /* Define the maximum number of planes we can upload - handle 2 views per buffer */
86 #define GST_GL_UPLOAD_MAX_PLANES (GST_VIDEO_MAX_PLANES * 2)
88 typedef struct _UploadMethod UploadMethod;
90 struct _GstGLUploadPrivate
93 GstVideoInfo out_info;
99 /* all method impl pointers */
100 gpointer *upload_impl;
103 const UploadMethod *method;
104 gpointer method_impl;
107 /* saved method for reconfigure */
112 GST_DEBUG_CATEGORY_INIT (gst_gl_upload_debug, "glupload", 0, "upload");
114 G_DEFINE_TYPE_WITH_CODE (GstGLUpload, gst_gl_upload, GST_TYPE_OBJECT,
115 G_ADD_PRIVATE (GstGLUpload) DEBUG_INIT);
118 _set_caps_features_with_passthrough (const GstCaps * caps,
119 const gchar * feature_name, GstCapsFeatures * passthrough)
124 tmp = gst_caps_new_empty ();
126 n = gst_caps_get_size (caps);
127 for (i = 0; i < n; i++) {
128 GstCapsFeatures *features, *orig_features;
129 GstStructure *s = gst_caps_get_structure (caps, i);
131 orig_features = gst_caps_get_features (caps, i);
132 features = gst_caps_features_new (feature_name, NULL);
134 if (gst_caps_features_is_any (orig_features)) {
135 /* if we have any features, we add both the features with and without @passthrough */
136 gst_caps_append_structure_full (tmp, gst_structure_copy (s),
137 gst_caps_features_copy (features));
139 m = gst_caps_features_get_size (passthrough);
140 for (j = 0; j < m; j++) {
141 const gchar *feature = gst_caps_features_get_nth (passthrough, j);
143 /* if we already have the features */
144 if (gst_caps_features_contains (features, feature))
147 gst_caps_features_add (features, feature);
150 m = gst_caps_features_get_size (orig_features);
151 for (j = 0; j < m; j++) {
152 const gchar *feature = gst_caps_features_get_nth (orig_features, j);
154 /* if we already have the features */
155 if (gst_caps_features_contains (features, feature))
158 if (g_strcmp0 (feature, GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY) == 0)
161 if (gst_caps_features_contains (passthrough, feature)) {
162 gst_caps_features_add (features, feature);
167 gst_caps_append_structure_full (tmp, gst_structure_copy (s), features);
174 _caps_intersect_texture_target (GstCaps * caps, GstGLTextureTarget target_mask)
176 GValue targets = G_VALUE_INIT;
177 GstCaps *ret, *target;
179 target = gst_caps_copy (caps);
180 gst_gl_value_set_texture_target_from_mask (&targets, target_mask);
181 gst_caps_set_value (target, "texture-target", &targets);
183 ret = gst_caps_intersect_full (caps, target, GST_CAPS_INTERSECT_FIRST);
185 g_value_unset (&targets);
186 gst_caps_unref (target);
192 METHOD_FLAG_CAN_SHARE_CONTEXT = 1,
193 } GstGLUploadMethodFlags;
198 GstGLUploadMethodFlags flags;
200 GstStaticCaps *input_template_caps;
202 gpointer (*new) (GstGLUpload * upload);
203 GstCaps *(*transform_caps) (gpointer impl, GstGLContext * context,
204 GstPadDirection direction, GstCaps * caps);
205 gboolean (*accept) (gpointer impl, GstBuffer * buffer, GstCaps * in_caps,
207 void (*propose_allocation) (gpointer impl, GstQuery * decide_query,
209 GstGLUploadReturn (*perform) (gpointer impl, GstBuffer * buffer,
210 GstBuffer ** outbuf);
211 void (*free) (gpointer impl);
214 struct GLMemoryUpload
217 GstGLTextureTarget input_target;
218 GstGLTextureTarget output_target;
222 _gl_memory_upload_new (GstGLUpload * upload)
224 struct GLMemoryUpload *mem = g_new0 (struct GLMemoryUpload, 1);
226 mem->upload = upload;
227 mem->input_target = GST_GL_TEXTURE_TARGET_NONE;
228 mem->output_target = GST_GL_TEXTURE_TARGET_NONE;
234 _gl_memory_upload_transform_caps (gpointer impl, GstGLContext * context,
235 GstPadDirection direction, GstCaps * caps)
237 struct GLMemoryUpload *upload = impl;
238 GstCapsFeatures *passthrough =
239 gst_caps_features_from_string
240 (GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
244 _set_caps_features_with_passthrough (caps,
245 GST_CAPS_FEATURE_MEMORY_GL_MEMORY, passthrough);
247 gst_caps_features_free (passthrough);
249 if (direction == GST_PAD_SINK) {
251 GstGLTextureTarget target_mask;
253 if (upload->input_target != GST_GL_TEXTURE_TARGET_NONE) {
254 target_mask = 1 << upload->input_target;
256 target_mask = 1 << GST_GL_TEXTURE_TARGET_2D |
257 1 << GST_GL_TEXTURE_TARGET_RECTANGLE |
258 1 << GST_GL_TEXTURE_TARGET_EXTERNAL_OES;
261 tmp = _caps_intersect_texture_target (ret, target_mask);
262 gst_caps_unref (ret);
267 n = gst_caps_get_size (ret);
268 for (i = 0; i < n; i++) {
269 GstStructure *s = gst_caps_get_structure (ret, i);
271 gst_structure_remove_fields (s, "texture-target", NULL);
279 _gl_memory_upload_accept (gpointer impl, GstBuffer * buffer, GstCaps * in_caps,
282 struct GLMemoryUpload *upload = impl;
283 GstCapsFeatures *features;
286 features = gst_caps_get_features (out_caps, 0);
287 if (!gst_caps_features_contains (features, GST_CAPS_FEATURE_MEMORY_GL_MEMORY))
290 features = gst_caps_get_features (in_caps, 0);
291 if (!gst_caps_features_contains (features, GST_CAPS_FEATURE_MEMORY_GL_MEMORY)
292 && !gst_caps_features_contains (features,
293 GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY))
297 GstVideoInfo *in_info = &upload->upload->priv->in_info;
298 guint expected_memories = GST_VIDEO_INFO_N_PLANES (in_info);
300 /* Support stereo views for separated multiview mode */
301 if (GST_VIDEO_INFO_MULTIVIEW_MODE (in_info) ==
302 GST_VIDEO_MULTIVIEW_MODE_SEPARATED)
303 expected_memories *= GST_VIDEO_INFO_VIEWS (in_info);
305 if (gst_buffer_n_memory (buffer) != expected_memories)
308 for (i = 0; i < expected_memories; i++) {
309 GstMemory *mem = gst_buffer_peek_memory (buffer, i);
311 if (!gst_is_gl_memory (mem))
320 _gl_memory_upload_propose_allocation (gpointer impl, GstQuery * decide_query,
323 struct GLMemoryUpload *upload = impl;
324 GstBufferPool *pool = NULL;
327 GstCapsFeatures *features;
329 gst_query_parse_allocation (query, &caps, NULL);
332 features = gst_caps_get_features (caps, 0);
334 /* Only offer our custom allocator if that type of memory was negotiated. */
335 if (gst_caps_features_contains (features, GST_CAPS_FEATURE_MEMORY_GL_MEMORY)) {
336 GstAllocator *allocator;
337 GstAllocationParams params;
338 gst_allocation_params_init (¶ms);
341 GST_ALLOCATOR (gst_gl_memory_allocator_get_default (upload->
343 gst_query_add_allocation_param (query, allocator, ¶ms);
344 gst_object_unref (allocator);
346 #if GST_GL_HAVE_PLATFORM_EGL
347 if (upload->upload->context
348 && gst_gl_context_get_gl_platform (upload->upload->context) ==
349 GST_GL_PLATFORM_EGL) {
351 GST_ALLOCATOR (gst_allocator_find (GST_GL_MEMORY_EGL_ALLOCATOR_NAME));
352 gst_query_add_allocation_param (query, allocator, ¶ms);
353 gst_object_unref (allocator);
358 n_pools = gst_query_get_n_allocation_pools (query);
359 for (i = 0; i < n_pools; i++) {
360 gst_query_parse_nth_allocation_pool (query, i, &pool, NULL, NULL, NULL);
361 if (!GST_IS_GL_BUFFER_POOL (pool)) {
362 gst_object_unref (pool);
368 GstStructure *config;
373 if (!gst_video_info_from_caps (&info, caps))
376 pool = gst_gl_buffer_pool_new (upload->upload->context);
377 config = gst_buffer_pool_get_config (pool);
379 /* the normal size of a frame */
381 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
382 gst_buffer_pool_config_add_option (config,
383 GST_BUFFER_POOL_OPTION_GL_SYNC_META);
384 if (upload->upload->priv->out_caps) {
385 GstGLTextureTarget target;
386 const gchar *target_pool_option_str;
389 _caps_get_texture_target (upload->upload->priv->out_caps,
390 GST_GL_TEXTURE_TARGET_2D);
391 target_pool_option_str =
392 gst_gl_texture_target_to_buffer_pool_option (target);
393 gst_buffer_pool_config_add_option (config, target_pool_option_str);
396 if (!gst_buffer_pool_set_config (pool, config)) {
397 gst_object_unref (pool);
401 gst_query_add_allocation_pool (query, pool, size, 1, 0);
405 gst_object_unref (pool);
411 GST_WARNING_OBJECT (upload->upload, "invalid caps specified");
416 GST_WARNING_OBJECT (upload->upload, "failed setting config");
421 static GstGLUploadReturn
422 _gl_memory_upload_perform (gpointer impl, GstBuffer * buffer,
425 struct GLMemoryUpload *upload = impl;
429 n = gst_buffer_n_memory (buffer);
430 for (i = 0; i < n; i++) {
431 GstMemory *mem = gst_buffer_peek_memory (buffer, i);
433 gl_mem = (GstGLMemory *) mem;
434 if (!gst_gl_context_can_share (upload->upload->context,
435 gl_mem->mem.context))
436 return GST_GL_UPLOAD_UNSHARED_GL_CONTEXT;
438 if (upload->output_target == GST_GL_TEXTURE_TARGET_NONE &&
439 upload->upload->priv->out_caps) {
440 upload->output_target =
441 _caps_get_texture_target (upload->upload->priv->out_caps,
442 GST_GL_TEXTURE_TARGET_NONE);
445 /* always track the last input texture target so ::transform_caps() can
446 * use it to build the output caps */
447 upload->input_target = gl_mem->tex_target;
448 if (upload->output_target != gl_mem->tex_target) {
450 return GST_GL_UPLOAD_RECONFIGURE;
453 if (gst_is_gl_memory_pbo (mem))
454 gst_gl_memory_pbo_upload_transfer ((GstGLMemoryPBO *) mem);
457 *outbuf = gst_buffer_ref (buffer);
459 return GST_GL_UPLOAD_DONE;
463 _gl_memory_upload_free (gpointer impl)
469 static GstStaticCaps _gl_memory_upload_caps =
470 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
471 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY, GST_GL_MEMORY_VIDEO_FORMATS_STR));
473 static const UploadMethod _gl_memory_upload = {
475 METHOD_FLAG_CAN_SHARE_CONTEXT,
476 &_gl_memory_upload_caps,
477 &_gl_memory_upload_new,
478 &_gl_memory_upload_transform_caps,
479 &_gl_memory_upload_accept,
480 &_gl_memory_upload_propose_allocation,
481 &_gl_memory_upload_perform,
482 &_gl_memory_upload_free
485 #if GST_GL_HAVE_DMABUF
490 GstEGLImage *eglimage[GST_VIDEO_MAX_PLANES];
492 GstGLVideoAllocationParams *params;
496 GstVideoInfo out_info;
497 /* only used for pointer comparision */
501 static GstStaticCaps _dma_buf_upload_caps =
502 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
503 (GST_CAPS_FEATURE_MEMORY_DMABUF,
504 GST_GL_MEMORY_VIDEO_FORMATS_STR) ";"
505 GST_VIDEO_CAPS_MAKE (GST_GL_MEMORY_VIDEO_FORMATS_STR));
508 _dma_buf_upload_new (GstGLUpload * upload)
510 struct DmabufUpload *dmabuf = g_new0 (struct DmabufUpload, 1);
511 dmabuf->upload = upload;
516 _dma_buf_upload_transform_caps (gpointer impl, GstGLContext * context,
517 GstPadDirection direction, GstCaps * caps)
519 GstCapsFeatures *passthrough =
520 gst_caps_features_from_string
521 (GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
525 /* Don't propose DMABuf caps feature unless it can be supported */
526 if (gst_gl_context_get_gl_platform (context) != GST_GL_PLATFORM_EGL)
529 if (!gst_gl_context_check_feature (context, "EGL_KHR_image_base"))
533 if (direction == GST_PAD_SINK) {
537 _set_caps_features_with_passthrough (caps,
538 GST_CAPS_FEATURE_MEMORY_GL_MEMORY, passthrough);
540 tmp = _caps_intersect_texture_target (ret, 1 << GST_GL_TEXTURE_TARGET_2D);
541 gst_caps_unref (ret);
548 _set_caps_features_with_passthrough (caps,
549 GST_CAPS_FEATURE_MEMORY_DMABUF, passthrough);
551 _set_caps_features_with_passthrough (caps,
552 GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY, passthrough);
553 gst_caps_append (ret, tmp);
556 n = gst_caps_get_size (ret);
557 for (i = 0; i < n; i++) {
558 GstStructure *s = gst_caps_get_structure (ret, i);
560 gst_structure_remove_fields (s, "texture-target", NULL);
564 gst_caps_features_free (passthrough);
570 _eglimage_quark (gint plane)
572 static GQuark quark[5] = { 0 };
573 static const gchar *quark_str[] = {
574 "GstGLDMABufEGLImage0",
575 "GstGLDMABufEGLImage1",
576 "GstGLDMABufEGLImage2",
577 "GstGLDMABufEGLImage3",
578 "GstGLDMABufEGLImage",
582 quark[plane] = g_quark_from_static_string (quark_str[plane]);
588 _get_cached_eglimage (GstMemory * mem, gint plane)
590 return gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
591 _eglimage_quark (plane));
595 _set_cached_eglimage (GstMemory * mem, GstEGLImage * eglimage, gint plane)
597 return gst_mini_object_set_qdata (GST_MINI_OBJECT (mem),
598 _eglimage_quark (plane), eglimage, (GDestroyNotify) gst_egl_image_unref);
602 _dma_buf_upload_accept (gpointer impl, GstBuffer * buffer, GstCaps * in_caps,
605 struct DmabufUpload *dmabuf = impl;
606 GstVideoInfo *in_info = &dmabuf->upload->priv->in_info;
607 GstVideoInfo *out_info = in_info;
608 guint n_planes = GST_VIDEO_INFO_N_PLANES (in_info);
611 GstMemory *mems[GST_VIDEO_MAX_PLANES];
612 gsize offset[GST_VIDEO_MAX_PLANES];
613 gint fd[GST_VIDEO_MAX_PLANES];
616 n_mem = gst_buffer_n_memory (buffer);
617 meta = gst_buffer_get_video_meta (buffer);
619 /* dmabuf upload is only supported with EGL contexts. */
620 if (gst_gl_context_get_gl_platform (dmabuf->upload->context) !=
624 if (!gst_gl_context_check_feature (dmabuf->upload->context,
625 "EGL_KHR_image_base"))
628 /* This will eliminate most non-dmabuf out there */
629 if (!gst_is_dmabuf_memory (gst_buffer_peek_memory (buffer, 0)))
632 /* We cannot have multiple dmabuf per plane */
633 if (n_mem > n_planes)
636 /* Update video info based on video meta */
638 in_info->width = meta->width;
639 in_info->height = meta->height;
641 for (i = 0; i < meta->n_planes; i++) {
642 in_info->offset[i] = meta->offset[i];
643 in_info->stride[i] = meta->stride[i];
647 if (dmabuf->direct) {
648 if (out_caps != dmabuf->out_caps) {
649 dmabuf->out_caps = out_caps;
650 if (!gst_video_info_from_caps (&dmabuf->out_info, out_caps))
653 out_info = &dmabuf->out_info;
657 gst_gl_allocation_params_free ((GstGLAllocationParams *) dmabuf->params);
658 if (!(dmabuf->params =
659 gst_gl_video_allocation_params_new_wrapped_gl_handle (dmabuf->
660 upload->context, NULL, out_info, -1, NULL,
661 GST_GL_TEXTURE_TARGET_2D, 0, NULL, NULL, NULL)))
664 /* Find and validate all memories */
665 for (i = 0; i < n_planes; i++) {
671 plane_size = gst_gl_get_plane_data_size (in_info, NULL, i);
673 if (!gst_buffer_find_memory (buffer, in_info->offset[i], plane_size,
674 &mem_idx, &length, &mem_skip))
677 /* We can't have more then one dmabuf per plane */
681 mems[i] = gst_buffer_peek_memory (buffer, mem_idx);
683 /* And all memory found must be dmabuf */
684 if (!gst_is_dmabuf_memory (mems[i]))
687 offset[i] = mems[i]->offset + mem_skip;
688 fd[i] = gst_dmabuf_memory_get_fd (mems[i]);
694 dmabuf->n_mem = n_planes;
696 /* Now create an EGLImage for each dmabufs */
697 for (i = 0; i < dmabuf->n_mem; i++) {
698 gint cache_id = dmabuf->direct ? 4 : i;
700 /* check if one is cached */
701 dmabuf->eglimage[i] = _get_cached_eglimage (mems[i], cache_id);
702 if (dmabuf->eglimage[i])
705 /* otherwise create one and cache it */
707 dmabuf->eglimage[i] =
708 gst_egl_image_from_dmabuf_direct (dmabuf->upload->context, fd, offset,
711 dmabuf->eglimage[i] = gst_egl_image_from_dmabuf (dmabuf->upload->context,
712 fd[i], in_info, i, offset[i]);
714 if (!dmabuf->eglimage[i])
717 _set_cached_eglimage (mems[i], dmabuf->eglimage[i], cache_id);
724 _dma_buf_upload_propose_allocation (gpointer impl, GstQuery * decide_query,
727 /* nothing to do for now. */
731 _dma_buf_upload_perform_gl_thread (GstGLContext * context,
732 struct DmabufUpload *dmabuf)
734 GstGLMemoryAllocator *allocator;
737 GST_GL_MEMORY_ALLOCATOR (gst_allocator_find
738 (GST_GL_MEMORY_EGL_ALLOCATOR_NAME));
740 /* FIXME: buffer pool */
741 dmabuf->outbuf = gst_buffer_new ();
742 gst_gl_memory_setup_buffer (allocator, dmabuf->outbuf, dmabuf->params, NULL,
743 (gpointer *) dmabuf->eglimage, dmabuf->n_mem);
744 gst_object_unref (allocator);
747 static GstGLUploadReturn
748 _dma_buf_upload_perform (gpointer impl, GstBuffer * buffer, GstBuffer ** outbuf)
750 struct DmabufUpload *dmabuf = impl;
752 gst_gl_context_thread_add (dmabuf->upload->context,
753 (GstGLContextThreadFunc) _dma_buf_upload_perform_gl_thread, dmabuf);
756 return GST_GL_UPLOAD_ERROR;
758 gst_buffer_add_parent_buffer_meta (dmabuf->outbuf, buffer);
760 *outbuf = dmabuf->outbuf;
761 dmabuf->outbuf = NULL;
763 return GST_GL_UPLOAD_DONE;
767 _dma_buf_upload_free (gpointer impl)
769 struct DmabufUpload *dmabuf = impl;
772 gst_gl_allocation_params_free ((GstGLAllocationParams *) dmabuf->params);
777 static const UploadMethod _dma_buf_upload = {
780 &_dma_buf_upload_caps,
781 &_dma_buf_upload_new,
782 &_dma_buf_upload_transform_caps,
783 &_dma_buf_upload_accept,
784 &_dma_buf_upload_propose_allocation,
785 &_dma_buf_upload_perform,
786 &_dma_buf_upload_free
789 /* a variant of the DMABuf uploader that relies on HW color convertion instead
793 _direct_dma_buf_upload_new (GstGLUpload * upload)
795 struct DmabufUpload *dmabuf = _dma_buf_upload_new (upload);
796 dmabuf->direct = TRUE;
797 gst_video_info_init (&dmabuf->out_info);
802 _direct_dma_buf_upload_transform_caps (gpointer impl, GstGLContext * context,
803 GstPadDirection direction, GstCaps * caps)
805 GstCapsFeatures *passthrough =
806 gst_caps_features_from_string
807 (GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
810 if (direction == GST_PAD_SINK) {
815 _set_caps_features_with_passthrough (caps,
816 GST_CAPS_FEATURE_MEMORY_GL_MEMORY, passthrough);
818 gst_caps_set_simple (ret, "format", G_TYPE_STRING, "RGBA", NULL);
820 n = gst_caps_get_size (ret);
821 for (i = 0; i < n; i++) {
822 GstStructure *s = gst_caps_get_structure (ret, i);
824 gst_structure_remove_fields (s, "chroma-site", NULL);
825 gst_structure_remove_fields (s, "colorimetry", NULL);
827 tmp = _caps_intersect_texture_target (ret, 1 << GST_GL_TEXTURE_TARGET_2D);
828 gst_caps_unref (ret);
833 GValue formats = G_VALUE_INIT;
834 gchar *format_str = g_strdup (GST_GL_MEMORY_VIDEO_FORMATS_STR);
837 _set_caps_features_with_passthrough (caps,
838 GST_CAPS_FEATURE_MEMORY_DMABUF, passthrough);
840 _set_caps_features_with_passthrough (caps,
841 GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY, passthrough);
842 gst_caps_append (ret, tmp);
844 g_value_init (&formats, GST_TYPE_LIST);
845 gst_value_deserialize (&formats, format_str);
846 gst_caps_set_value (ret, "format", &formats);
848 g_value_unset (&formats);
850 n = gst_caps_get_size (ret);
851 for (i = 0; i < n; i++) {
852 GstStructure *s = gst_caps_get_structure (ret, i);
854 gst_structure_remove_fields (s, "texture-target", NULL);
858 gst_caps_features_free (passthrough);
863 static const UploadMethod _direct_dma_buf_upload = {
866 &_dma_buf_upload_caps,
867 &_direct_dma_buf_upload_new,
868 &_direct_dma_buf_upload_transform_caps,
869 &_dma_buf_upload_accept,
870 &_dma_buf_upload_propose_allocation,
871 &_dma_buf_upload_perform,
872 &_dma_buf_upload_free
875 #endif /* GST_GL_HAVE_DMABUF */
882 GstVideoGLTextureUploadMeta *meta;
883 guint texture_ids[GST_GL_UPLOAD_MAX_PLANES];
888 _upload_meta_upload_new (GstGLUpload * upload)
890 struct GLUploadMeta *meta = g_new0 (struct GLUploadMeta, 1);
892 meta->upload = upload;
899 _upload_meta_upload_transform_caps (gpointer impl, GstGLContext * context,
900 GstPadDirection direction, GstCaps * caps)
902 GstCapsFeatures *passthrough =
903 gst_caps_features_from_string
904 (GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
907 if (direction == GST_PAD_SINK) {
911 _set_caps_features_with_passthrough (caps,
912 GST_CAPS_FEATURE_MEMORY_GL_MEMORY, passthrough);
914 tmp = _caps_intersect_texture_target (ret, 1 << GST_GL_TEXTURE_TARGET_2D);
915 gst_caps_unref (ret);
921 _set_caps_features_with_passthrough (caps,
922 GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META, passthrough);
923 gst_caps_set_simple (ret, "format", G_TYPE_STRING, "RGBA", NULL);
925 n = gst_caps_get_size (ret);
926 for (i = 0; i < n; i++) {
927 GstStructure *s = gst_caps_get_structure (ret, i);
929 gst_structure_remove_fields (s, "texture-target", NULL);
933 gst_caps_features_free (passthrough);
939 _upload_meta_upload_accept (gpointer impl, GstBuffer * buffer,
940 GstCaps * in_caps, GstCaps * out_caps)
942 struct GLUploadMeta *upload = impl;
943 GstCapsFeatures *features;
944 GstVideoGLTextureUploadMeta *meta;
946 GstStructure *config;
949 features = gst_caps_get_features (in_caps, 0);
951 if (!gst_caps_features_contains (features,
952 GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META))
955 features = gst_caps_get_features (out_caps, 0);
956 if (!gst_caps_features_contains (features, GST_CAPS_FEATURE_MEMORY_GL_MEMORY))
962 if (upload->pool == NULL)
963 upload->pool = gst_gl_buffer_pool_new (upload->upload->context);
965 if (!gst_buffer_pool_is_active (upload->pool)) {
966 config = gst_buffer_pool_get_config (upload->pool);
968 size = upload->upload->priv->in_info.size;
969 gst_buffer_pool_config_set_params (config, in_caps, size, 0, 0);
971 if (!gst_buffer_pool_set_config (upload->pool, config)) {
972 GST_WARNING_OBJECT (upload->upload, "failed to set bufferpool config");
975 gst_buffer_pool_set_active (upload->pool, TRUE);
979 if ((meta = gst_buffer_get_video_gl_texture_upload_meta (buffer)) == NULL)
982 if (meta->texture_type[0] != GST_VIDEO_GL_TEXTURE_TYPE_RGBA) {
983 GST_FIXME_OBJECT (upload, "only single rgba texture supported");
987 if (meta->texture_orientation !=
988 GST_VIDEO_GL_TEXTURE_ORIENTATION_X_NORMAL_Y_NORMAL) {
989 GST_FIXME_OBJECT (upload, "only x-normal, y-normal textures supported");
998 _upload_meta_upload_propose_allocation (gpointer impl, GstQuery * decide_query,
1001 struct GLUploadMeta *upload = impl;
1002 GstStructure *gl_context;
1003 gchar *platform, *gl_apis;
1007 gst_gl_api_to_string (gst_gl_context_get_gl_api (upload->upload->
1010 gst_gl_platform_to_string (gst_gl_context_get_gl_platform (upload->
1012 handle = (gpointer) gst_gl_context_get_gl_context (upload->upload->context);
1015 gst_structure_new ("GstVideoGLTextureUploadMeta", "gst.gl.GstGLContext",
1016 GST_TYPE_GL_CONTEXT, upload->upload->context, "gst.gl.context.handle",
1017 G_TYPE_POINTER, handle, "gst.gl.context.type", G_TYPE_STRING, platform,
1018 "gst.gl.context.apis", G_TYPE_STRING, gl_apis, NULL);
1019 gst_query_add_allocation_meta (query,
1020 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, gl_context);
1024 gst_structure_free (gl_context);
1028 * Uploads using gst_video_gl_texture_upload_meta_upload().
1029 * i.e. consumer of GstVideoGLTextureUploadMeta
1032 _do_upload_with_meta (GstGLContext * context, struct GLUploadMeta *upload)
1034 if (!gst_video_gl_texture_upload_meta_upload (upload->meta,
1035 upload->texture_ids)) {
1036 upload->result = FALSE;
1040 upload->result = TRUE;
1043 static GstGLUploadReturn
1044 _upload_meta_upload_perform (gpointer impl, GstBuffer * buffer,
1045 GstBuffer ** outbuf)
1047 struct GLUploadMeta *upload = impl;
1049 GstVideoInfo *in_info = &upload->upload->priv->in_info;
1050 guint max_planes = GST_VIDEO_INFO_N_PLANES (in_info);
1052 /* Support stereo views for separated multiview mode */
1053 if (GST_VIDEO_INFO_MULTIVIEW_MODE (in_info) ==
1054 GST_VIDEO_MULTIVIEW_MODE_SEPARATED)
1055 max_planes *= GST_VIDEO_INFO_VIEWS (in_info);
1057 GST_LOG_OBJECT (upload, "Attempting upload with GstVideoGLTextureUploadMeta");
1059 upload->meta = gst_buffer_get_video_gl_texture_upload_meta (buffer);
1061 if (gst_buffer_pool_acquire_buffer (upload->pool, outbuf,
1062 NULL) != GST_FLOW_OK) {
1063 GST_WARNING_OBJECT (upload, "failed to acquire buffer from bufferpool");
1064 return GST_GL_UPLOAD_ERROR;
1067 for (i = 0; i < GST_GL_UPLOAD_MAX_PLANES; i++) {
1070 if (i < max_planes) {
1071 GstMemory *mem = gst_buffer_peek_memory (*outbuf, i);
1072 tex_id = ((GstGLMemory *) mem)->tex_id;
1075 upload->texture_ids[i] = tex_id;
1078 GST_LOG ("Uploading with GLTextureUploadMeta with textures "
1079 "%i,%i,%i,%i / %i,%i,%i,%i",
1080 upload->texture_ids[0], upload->texture_ids[1],
1081 upload->texture_ids[2], upload->texture_ids[3],
1082 upload->texture_ids[4], upload->texture_ids[5],
1083 upload->texture_ids[6], upload->texture_ids[7]);
1085 gst_gl_context_thread_add (upload->upload->context,
1086 (GstGLContextThreadFunc) _do_upload_with_meta, upload);
1088 if (!upload->result)
1089 return GST_GL_UPLOAD_ERROR;
1091 return GST_GL_UPLOAD_DONE;
1095 _upload_meta_upload_free (gpointer impl)
1097 struct GLUploadMeta *upload = impl;
1099 g_return_if_fail (impl != NULL);
1102 gst_object_unref (upload->pool);
1107 static GstStaticCaps _upload_meta_upload_caps =
1108 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
1109 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META, "RGBA"));
1111 static const UploadMethod _upload_meta_upload = {
1113 METHOD_FLAG_CAN_SHARE_CONTEXT,
1114 &_upload_meta_upload_caps,
1115 &_upload_meta_upload_new,
1116 &_upload_meta_upload_transform_caps,
1117 &_upload_meta_upload_accept,
1118 &_upload_meta_upload_propose_allocation,
1119 &_upload_meta_upload_perform,
1120 &_upload_meta_upload_free
1123 struct RawUploadFrame
1126 GstVideoFrame frame;
1131 GstGLUpload *upload;
1132 struct RawUploadFrame *in_frame;
1133 GstGLVideoAllocationParams *params;
1136 static struct RawUploadFrame *
1137 _raw_upload_frame_new (struct RawUpload *raw, GstBuffer * buffer)
1139 struct RawUploadFrame *frame;
1146 frame = g_slice_new (struct RawUploadFrame);
1147 frame->ref_count = 1;
1149 if (!gst_video_frame_map (&frame->frame, &raw->upload->priv->in_info,
1150 buffer, GST_MAP_READ)) {
1151 g_slice_free (struct RawUploadFrame, frame);
1155 raw->upload->priv->in_info = frame->frame.info;
1156 info = &raw->upload->priv->in_info;
1158 /* Recalculate the offsets (and size) */
1160 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
1161 info->offset[i] = info->size;
1162 info->size += gst_gl_get_plane_data_size (info, NULL, i);
1169 _raw_upload_frame_ref (struct RawUploadFrame *frame)
1171 g_atomic_int_inc (&frame->ref_count);
1175 _raw_upload_frame_unref (struct RawUploadFrame *frame)
1177 if (g_atomic_int_dec_and_test (&frame->ref_count)) {
1178 gst_video_frame_unmap (&frame->frame);
1179 g_slice_free (struct RawUploadFrame, frame);
1184 _raw_data_upload_new (GstGLUpload * upload)
1186 struct RawUpload *raw = g_new0 (struct RawUpload, 1);
1188 raw->upload = upload;
1194 _raw_data_upload_transform_caps (gpointer impl, GstGLContext * context,
1195 GstPadDirection direction, GstCaps * caps)
1197 GstCapsFeatures *passthrough =
1198 gst_caps_features_from_string
1199 (GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
1202 if (direction == GST_PAD_SINK) {
1203 GstGLTextureTarget target_mask = 0;
1207 _set_caps_features_with_passthrough (caps,
1208 GST_CAPS_FEATURE_MEMORY_GL_MEMORY, passthrough);
1210 target_mask |= 1 << GST_GL_TEXTURE_TARGET_2D;
1211 target_mask |= 1 << GST_GL_TEXTURE_TARGET_RECTANGLE;
1212 tmp = _caps_intersect_texture_target (ret, target_mask);
1213 gst_caps_unref (ret);
1219 _set_caps_features_with_passthrough (caps,
1220 GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY, passthrough);
1222 n = gst_caps_get_size (ret);
1223 for (i = 0; i < n; i++) {
1224 GstStructure *s = gst_caps_get_structure (ret, i);
1226 gst_structure_remove_fields (s, "texture-target", NULL);
1230 gst_caps_features_free (passthrough);
1236 _raw_data_upload_accept (gpointer impl, GstBuffer * buffer, GstCaps * in_caps,
1239 struct RawUpload *raw = impl;
1240 GstCapsFeatures *features;
1242 features = gst_caps_get_features (out_caps, 0);
1243 if (!gst_caps_features_contains (features, GST_CAPS_FEATURE_MEMORY_GL_MEMORY))
1247 _raw_upload_frame_unref (raw->in_frame);
1248 raw->in_frame = _raw_upload_frame_new (raw, buffer);
1251 gst_gl_allocation_params_free ((GstGLAllocationParams *) raw->params);
1253 gst_gl_video_allocation_params_new_wrapped_data (raw->upload->context,
1254 NULL, &raw->upload->priv->in_info, -1, NULL,
1255 GST_GL_TEXTURE_TARGET_2D, 0, NULL, raw->in_frame,
1256 (GDestroyNotify) _raw_upload_frame_unref)))
1259 return (raw->in_frame != NULL);
1263 _raw_data_upload_propose_allocation (gpointer impl, GstQuery * decide_query,
1266 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);
1269 static GstGLUploadReturn
1270 _raw_data_upload_perform (gpointer impl, GstBuffer * buffer,
1271 GstBuffer ** outbuf)
1273 GstGLBaseMemoryAllocator *allocator;
1274 struct RawUpload *raw = impl;
1276 GstVideoInfo *in_info = &raw->upload->priv->in_info;
1277 guint n_mem = GST_VIDEO_INFO_N_PLANES (in_info);
1280 GST_GL_BASE_MEMORY_ALLOCATOR (gst_gl_memory_allocator_get_default
1281 (raw->upload->context));
1283 /* FIXME Use a buffer pool to cache the generated textures */
1284 /* FIXME: multiview support with separated left/right frames? */
1285 *outbuf = gst_buffer_new ();
1286 for (i = 0; i < n_mem; i++) {
1287 GstGLBaseMemory *tex;
1289 raw->params->parent.wrapped_data = raw->in_frame->frame.data[i];
1290 raw->params->plane = i;
1291 raw->params->tex_format =
1292 gst_gl_format_from_video_info (raw->upload->context, in_info, i);
1295 gst_gl_base_memory_alloc (allocator,
1296 (GstGLAllocationParams *) raw->params);
1298 gst_buffer_unref (*outbuf);
1300 GST_ERROR_OBJECT (raw->upload, "Failed to allocate wrapped texture");
1301 return GST_GL_UPLOAD_ERROR;
1304 _raw_upload_frame_ref (raw->in_frame);
1305 gst_buffer_append_memory (*outbuf, (GstMemory *) tex);
1307 gst_object_unref (allocator);
1309 _raw_upload_frame_unref (raw->in_frame);
1310 raw->in_frame = NULL;
1311 return GST_GL_UPLOAD_DONE;
1315 _raw_data_upload_free (gpointer impl)
1317 struct RawUpload *raw = impl;
1320 gst_gl_allocation_params_free ((GstGLAllocationParams *) raw->params);
1325 static GstStaticCaps _raw_data_upload_caps =
1326 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_GL_MEMORY_VIDEO_FORMATS_STR));
1328 static const UploadMethod _raw_data_upload = {
1331 &_raw_data_upload_caps,
1332 &_raw_data_upload_new,
1333 &_raw_data_upload_transform_caps,
1334 &_raw_data_upload_accept,
1335 &_raw_data_upload_propose_allocation,
1336 &_raw_data_upload_perform,
1337 &_raw_data_upload_free
1340 #if GST_GL_HAVE_VIV_DIRECTVIV
1342 #define GL_BGRA_EXT 0x80E1
1345 #define GL_VIV_YV12 0x8FC0
1348 #define GL_VIV_NV12 0x8FC1
1351 #define GL_VIV_YUY2 0x8FC2
1354 #define GL_VIV_UYVY 0x8FC3
1357 #define GL_VIV_NV21 0x8FC4
1360 #define GL_VIV_I420 0x8FC5
1363 struct DirectVIVUpload
1365 GstGLUpload *upload;
1367 GstGLVideoAllocationParams *params;
1368 GstBuffer *inbuf, *outbuf;
1369 void (*TexDirectVIVMap) (GLenum Target, GLsizei Width, GLsizei Height,
1370 GLenum Format, GLvoid ** Logical, const GLuint * Physical);
1371 void (*TexDirectInvalidateVIV) (GLenum Target);
1372 gboolean loaded_functions;
1375 #define GST_GL_DIRECTVIV_FORMAT "{RGBA, I420, YV12, NV12, NV21, YUY2, UYVY, BGRA, RGB16}"
1377 static GstStaticCaps _directviv_upload_caps =
1378 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_GL_DIRECTVIV_FORMAT));
1381 _directviv_upload_new (GstGLUpload * upload)
1383 struct DirectVIVUpload *directviv = g_new0 (struct DirectVIVUpload, 1);
1384 directviv->upload = upload;
1385 directviv->loaded_functions = FALSE;
1391 _directviv_upload_transform_caps (gpointer impl, GstGLContext * context,
1392 GstPadDirection direction, GstCaps * caps)
1394 GstCapsFeatures *passthrough =
1395 gst_caps_features_from_string
1396 (GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
1399 if (direction == GST_PAD_SINK) {
1403 _set_caps_features_with_passthrough (caps,
1404 GST_CAPS_FEATURE_MEMORY_GL_MEMORY, passthrough);
1406 gst_caps_set_simple (ret, "format", G_TYPE_STRING, "RGBA", NULL);
1407 tmp = _caps_intersect_texture_target (ret, 1 << GST_GL_TEXTURE_TARGET_2D);
1408 gst_caps_unref (ret);
1412 tmp = gst_caps_from_string (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
1413 (GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY, GST_GL_DIRECTVIV_FORMAT));
1415 _set_caps_features_with_passthrough (tmp,
1416 GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY, passthrough);
1417 gst_caps_unref (tmp);
1420 gst_caps_features_free (passthrough);
1426 _directviv_upload_load_functions_gl_thread (GstGLContext * context,
1427 struct DirectVIVUpload *directviv)
1429 directviv->TexDirectVIVMap =
1430 gst_gl_context_get_proc_address (context, "glTexDirectVIVMap");
1431 directviv->TexDirectInvalidateVIV =
1432 gst_gl_context_get_proc_address (context, "glTexDirectInvalidateVIV");
1436 _directviv_upload_accept (gpointer impl, GstBuffer * buffer, GstCaps * in_caps,
1439 struct DirectVIVUpload *directviv = impl;
1440 GstCapsFeatures *features;
1444 if (!directviv->loaded_functions && (!directviv->TexDirectInvalidateVIV ||
1445 !directviv->TexDirectVIVMap)) {
1446 gst_gl_context_thread_add (directviv->upload->context,
1447 (GstGLContextThreadFunc) _directviv_upload_load_functions_gl_thread,
1449 directviv->loaded_functions = TRUE;
1451 if (!directviv->TexDirectInvalidateVIV || !directviv->TexDirectVIVMap)
1454 features = gst_caps_get_features (out_caps, 0);
1455 if (!gst_caps_features_contains (features, GST_CAPS_FEATURE_MEMORY_GL_MEMORY))
1458 if (directviv->params)
1459 gst_gl_allocation_params_free ((GstGLAllocationParams *) directviv->params);
1460 if (!(directviv->params =
1461 gst_gl_video_allocation_params_new (directviv->upload->context, NULL,
1462 &directviv->upload->priv->out_info, -1, NULL,
1463 GST_GL_TEXTURE_TARGET_2D, GST_VIDEO_GL_TEXTURE_TYPE_RGBA)))
1466 /* We only support a single memory per buffer at this point */
1467 n_mem = gst_buffer_n_memory (buffer);
1469 mem = gst_buffer_peek_memory (buffer, 0);
1474 return n_mem == 1 && mem && gst_is_phys_memory (mem);
1478 _directviv_upload_propose_allocation (gpointer impl, GstQuery * decide_query,
1484 _directviv_upload_video_format_to_gl_format (GstVideoFormat format)
1487 case GST_VIDEO_FORMAT_I420:
1489 case GST_VIDEO_FORMAT_YV12:
1491 case GST_VIDEO_FORMAT_NV12:
1493 case GST_VIDEO_FORMAT_NV21:
1495 case GST_VIDEO_FORMAT_YUY2:
1497 case GST_VIDEO_FORMAT_UYVY:
1499 case GST_VIDEO_FORMAT_RGB16:
1501 case GST_VIDEO_FORMAT_RGBA:
1503 case GST_VIDEO_FORMAT_BGRA:
1505 case GST_VIDEO_FORMAT_RGBx:
1507 case GST_VIDEO_FORMAT_BGRx:
1520 } DirectVIVUnmapData;
1523 _directviv_memory_unmap (DirectVIVUnmapData * data)
1525 gst_memory_unmap (data->memory, &data->map);
1526 gst_memory_unref (data->memory);
1527 gst_buffer_unref (data->buffer);
1532 _directviv_upload_perform_gl_thread (GstGLContext * context,
1533 struct DirectVIVUpload *directviv)
1535 static GQuark directviv_unmap_quark = 0;
1536 GstGLMemoryAllocator *allocator;
1538 GstGLMemory *out_gl_mem;
1539 GstVideoInfo *in_info;
1540 DirectVIVUnmapData *unmap_data;
1541 GstVideoMeta *vmeta;
1542 gint width, height, gl_format;
1543 const GstGLFuncs *gl;
1545 if (!directviv_unmap_quark)
1546 directviv_unmap_quark = g_quark_from_static_string ("GstGLDirectVIVUnmap");
1548 gl = context->gl_vtable;
1550 g_assert (gst_buffer_n_memory (directviv->inbuf) == 1);
1551 in_info = &directviv->upload->priv->in_info;
1552 in_mem = gst_buffer_peek_memory (directviv->inbuf, 0);
1553 unmap_data = g_new0 (DirectVIVUnmapData, 1);
1554 if (!gst_memory_map (in_mem, &unmap_data->map, GST_MAP_READ)) {
1555 g_free (unmap_data);
1558 unmap_data->phys_addr = gst_phys_memory_get_phys_addr (in_mem);
1559 if (!unmap_data->phys_addr) {
1560 gst_memory_unmap (in_mem, &unmap_data->map);
1561 g_free (unmap_data);
1564 unmap_data->memory = gst_memory_ref (in_mem);
1565 unmap_data->buffer = gst_buffer_ref (directviv->inbuf);
1568 GST_GL_MEMORY_ALLOCATOR (gst_allocator_find
1569 (GST_GL_MEMORY_PBO_ALLOCATOR_NAME));
1571 /* FIXME: buffer pool */
1572 directviv->outbuf = gst_buffer_new ();
1573 gst_gl_memory_setup_buffer (allocator, directviv->outbuf, directviv->params,
1575 gst_object_unref (allocator);
1577 out_gl_mem = (GstGLMemory *) gst_buffer_peek_memory (directviv->outbuf, 0);
1579 /* Need to keep the input memory and buffer mapped and valid until
1580 * the GL memory is not used anymore */
1581 gst_mini_object_set_qdata ((GstMiniObject *) out_gl_mem,
1582 directviv_unmap_quark, unmap_data,
1583 (GDestroyNotify) _directviv_memory_unmap);
1584 gst_buffer_add_parent_buffer_meta (directviv->outbuf, directviv->inbuf);
1586 /* width/height need to compensate for stride/padding */
1587 vmeta = gst_buffer_get_video_meta (directviv->inbuf);
1589 width = vmeta->stride[0];
1590 if (GST_VIDEO_INFO_N_PLANES (in_info) == 1)
1591 height = gst_memory_get_sizes (in_mem, NULL, NULL) / width;
1593 height = vmeta->offset[1] / width;
1595 width = GST_VIDEO_INFO_PLANE_STRIDE (in_info, 0);
1596 if (GST_VIDEO_INFO_N_PLANES (in_info) == 1)
1597 height = gst_memory_get_sizes (in_mem, NULL, NULL) / width;
1599 height = GST_VIDEO_INFO_PLANE_OFFSET (in_info, 1) / width;
1601 width /= GST_VIDEO_INFO_COMP_PSTRIDE (in_info, 0);
1604 _directviv_upload_video_format_to_gl_format (GST_VIDEO_INFO_FORMAT
1607 gl->BindTexture (GL_TEXTURE_2D, out_gl_mem->tex_id);
1608 directviv->TexDirectVIVMap (GL_TEXTURE_2D, width, height,
1609 gl_format, (void **) &unmap_data->map.data, &unmap_data->phys_addr);
1610 directviv->TexDirectInvalidateVIV (GL_TEXTURE_2D);
1613 static GstGLUploadReturn
1614 _directviv_upload_perform (gpointer impl, GstBuffer * buffer,
1615 GstBuffer ** outbuf)
1617 struct DirectVIVUpload *directviv = impl;
1619 directviv->inbuf = buffer;
1620 directviv->outbuf = NULL;
1621 gst_gl_context_thread_add (directviv->upload->context,
1622 (GstGLContextThreadFunc) _directviv_upload_perform_gl_thread, directviv);
1623 directviv->inbuf = NULL;
1625 if (!directviv->outbuf)
1626 return GST_GL_UPLOAD_ERROR;
1628 *outbuf = directviv->outbuf;
1629 directviv->outbuf = NULL;
1631 return GST_GL_UPLOAD_DONE;
1635 _directviv_upload_free (gpointer impl)
1637 struct DirectVIVUpload *directviv = impl;
1639 if (directviv->params)
1640 gst_gl_allocation_params_free ((GstGLAllocationParams *) directviv->params);
1645 static const UploadMethod _directviv_upload = {
1648 &_directviv_upload_caps,
1649 &_directviv_upload_new,
1650 &_directviv_upload_transform_caps,
1651 &_directviv_upload_accept,
1652 &_directviv_upload_propose_allocation,
1653 &_directviv_upload_perform,
1654 &_directviv_upload_free
1657 #endif /* GST_GL_HAVE_VIV_DIRECTVIV */
1659 static const UploadMethod *upload_methods[] = { &_gl_memory_upload,
1660 #if GST_GL_HAVE_DMABUF
1661 &_direct_dma_buf_upload,
1664 #if GST_GL_HAVE_VIV_DIRECTVIV
1667 &_upload_meta_upload, &_raw_data_upload
1670 static GMutex upload_global_lock;
1673 gst_gl_upload_get_input_template_caps (void)
1675 GstCaps *ret = NULL;
1678 g_mutex_lock (&upload_global_lock);
1680 /* FIXME: cache this and invalidate on changes to upload_methods */
1681 for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
1683 gst_static_caps_get (upload_methods[i]->input_template_caps);
1684 ret = ret == NULL ? template : gst_caps_merge (ret, template);
1687 ret = gst_caps_simplify (ret);
1688 ret = gst_gl_overlay_compositor_add_caps (ret);
1689 g_mutex_unlock (&upload_global_lock);
1695 gst_gl_upload_class_init (GstGLUploadClass * klass)
1697 G_OBJECT_CLASS (klass)->finalize = gst_gl_upload_finalize;
1701 gst_gl_upload_init (GstGLUpload * upload)
1703 upload->priv = gst_gl_upload_get_instance_private (upload);
1707 * gst_gl_upload_new:
1708 * @context: a #GstGLContext
1710 * Returns: (transfer full): a new #GstGLUpload object
1713 gst_gl_upload_new (GstGLContext * context)
1715 GstGLUpload *upload = g_object_new (GST_TYPE_GL_UPLOAD, NULL);
1718 gst_object_ref_sink (upload);
1721 gst_gl_upload_set_context (upload, context);
1723 upload->context = NULL;
1725 n = G_N_ELEMENTS (upload_methods);
1726 upload->priv->upload_impl = g_malloc (sizeof (gpointer) * n);
1727 for (i = 0; i < n; i++) {
1728 upload->priv->upload_impl[i] = upload_methods[i]->new (upload);
1731 GST_DEBUG_OBJECT (upload, "Created new GLUpload for context %" GST_PTR_FORMAT,
1738 gst_gl_upload_set_context (GstGLUpload * upload, GstGLContext * context)
1740 g_return_if_fail (upload != NULL);
1742 gst_object_replace ((GstObject **) & upload->context, (GstObject *) context);
1746 gst_gl_upload_finalize (GObject * object)
1748 GstGLUpload *upload;
1751 upload = GST_GL_UPLOAD (object);
1753 upload->priv->method_i = 0;
1755 if (upload->context) {
1756 gst_object_unref (upload->context);
1757 upload->context = NULL;
1760 if (upload->priv->in_caps) {
1761 gst_caps_unref (upload->priv->in_caps);
1762 upload->priv->in_caps = NULL;
1765 if (upload->priv->out_caps) {
1766 gst_caps_unref (upload->priv->out_caps);
1767 upload->priv->out_caps = NULL;
1770 n = G_N_ELEMENTS (upload_methods);
1771 for (i = 0; i < n; i++) {
1772 if (upload->priv->upload_impl[i])
1773 upload_methods[i]->free (upload->priv->upload_impl[i]);
1775 g_free (upload->priv->upload_impl);
1777 G_OBJECT_CLASS (gst_gl_upload_parent_class)->finalize (object);
1781 gst_gl_upload_transform_caps (GstGLUpload * upload, GstGLContext * context,
1782 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
1784 GstCaps *result, *tmp;
1787 if (upload->priv->method)
1788 return upload->priv->method->transform_caps (upload->priv->method_impl,
1789 context, direction, caps);
1791 tmp = gst_caps_new_empty ();
1793 for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
1797 upload_methods[i]->transform_caps (upload->priv->upload_impl[i],
1798 context, direction, caps);
1801 tmp = gst_caps_merge (tmp, tmp2);
1805 result = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
1806 gst_caps_unref (tmp);
1815 * gst_gl_upload_propose_allocation:
1816 * @upload: a #GstGLUpload
1817 * @decide_query: (allow-none): a #GstQuery from a decide allocation
1818 * @query: the proposed allocation query
1820 * Adds the required allocation parameters to support uploading.
1823 gst_gl_upload_propose_allocation (GstGLUpload * upload, GstQuery * decide_query,
1828 for (i = 0; i < G_N_ELEMENTS (upload_methods); i++)
1829 upload_methods[i]->propose_allocation (upload->priv->upload_impl[i],
1830 decide_query, query);
1834 _gst_gl_upload_set_caps_unlocked (GstGLUpload * upload, GstCaps * in_caps,
1837 g_return_val_if_fail (upload != NULL, FALSE);
1838 g_return_val_if_fail (gst_caps_is_fixed (in_caps), FALSE);
1840 if (upload->priv->in_caps && upload->priv->out_caps
1841 && gst_caps_is_equal (upload->priv->in_caps, in_caps)
1842 && gst_caps_is_equal (upload->priv->out_caps, out_caps))
1845 gst_caps_replace (&upload->priv->in_caps, in_caps);
1846 gst_caps_replace (&upload->priv->out_caps, out_caps);
1848 gst_video_info_from_caps (&upload->priv->in_info, in_caps);
1849 gst_video_info_from_caps (&upload->priv->out_info, out_caps);
1851 upload->priv->method_impl = NULL;
1852 upload->priv->method_i = 0;
1858 * gst_gl_upload_set_caps:
1859 * @upload: a #GstGLUpload
1860 * @in_caps: input #GstCaps
1861 * @out_caps: output #GstCaps
1863 * Initializes @upload with the information required for upload.
1865 * Returns: whether @in_caps and @out_caps could be set on @upload
1868 gst_gl_upload_set_caps (GstGLUpload * upload, GstCaps * in_caps,
1873 GST_OBJECT_LOCK (upload);
1874 ret = _gst_gl_upload_set_caps_unlocked (upload, in_caps, out_caps);
1875 GST_OBJECT_UNLOCK (upload);
1881 * gst_gl_upload_get_caps:
1882 * @upload: a #GstGLUpload
1883 * @in_caps: (transfer full) (allow-none) (out): the input #GstCaps
1884 * @out_caps: (transfer full) (allow-none) (out): the output #GstCaps
1887 gst_gl_upload_get_caps (GstGLUpload * upload, GstCaps ** in_caps,
1888 GstCaps ** out_caps)
1890 GST_OBJECT_LOCK (upload);
1893 upload->priv->in_caps ? gst_caps_ref (upload->priv->in_caps) : NULL;
1896 upload->priv->out_caps ? gst_caps_ref (upload->priv->out_caps) : NULL;
1897 GST_OBJECT_UNLOCK (upload);
1901 _upload_find_method (GstGLUpload * upload, gpointer last_impl)
1905 /* start with the last used method after explicitly reconfiguring to
1906 * negotiate caps for this method */
1907 if (upload->priv->method_i == 0) {
1908 upload->priv->method_i = upload->priv->saved_method_i;
1909 upload->priv->saved_method_i = 0;
1912 if (upload->priv->method_i >= G_N_ELEMENTS (upload_methods)) {
1914 upload->priv->method_i = 0;
1919 method_i = upload->priv->method_i;
1921 if (last_impl == upload->priv->upload_impl[method_i])
1924 upload->priv->method = upload_methods[method_i];
1925 upload->priv->method_impl = upload->priv->upload_impl[method_i];
1927 GST_DEBUG_OBJECT (upload, "attempting upload with uploader %s",
1928 upload->priv->method->name);
1930 upload->priv->method_i++;
1936 * gst_gl_upload_perform_with_buffer:
1937 * @upload: a #GstGLUpload
1938 * @buffer: input #GstBuffer
1939 * @outbuf_ptr: (out): resulting #GstBuffer
1941 * Uploads @buffer using the transformation specified by
1942 * gst_gl_upload_set_caps() creating a new #GstBuffer in @outbuf_ptr.
1944 * Returns: whether the upload was successful
1947 gst_gl_upload_perform_with_buffer (GstGLUpload * upload, GstBuffer * buffer,
1948 GstBuffer ** outbuf_ptr)
1950 GstGLUploadReturn ret = GST_GL_UPLOAD_ERROR;
1952 gpointer last_impl = upload->priv->method_impl;
1954 g_return_val_if_fail (GST_IS_GL_UPLOAD (upload), FALSE);
1955 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1956 g_return_val_if_fail (outbuf_ptr != NULL, FALSE);
1958 GST_OBJECT_LOCK (upload);
1960 #define NEXT_METHOD \
1962 if (!_upload_find_method (upload, last_impl)) { \
1963 GST_OBJECT_UNLOCK (upload); \
1969 if (!upload->priv->method_impl)
1970 _upload_find_method (upload, last_impl);
1973 if (!upload->priv->method->accept (upload->priv->method_impl, buffer,
1974 upload->priv->in_caps, upload->priv->out_caps))
1978 upload->priv->method->perform (upload->priv->method_impl, buffer,
1980 if (ret == GST_GL_UPLOAD_UNSHARED_GL_CONTEXT) {
1983 for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
1984 if (upload_methods[i] == &_raw_data_upload) {
1985 upload->priv->method = &_raw_data_upload;
1986 upload->priv->method_impl = upload->priv->upload_impl[i];
1987 upload->priv->method_i = i;
1993 } else if (ret == GST_GL_UPLOAD_DONE || ret == GST_GL_UPLOAD_RECONFIGURE) {
1994 if (last_impl != upload->priv->method_impl) {
1995 GstCaps *caps = gst_gl_upload_transform_caps (upload, upload->context,
1996 GST_PAD_SINK, upload->priv->in_caps, NULL);
1997 if (!gst_caps_is_equal (upload->priv->out_caps, caps)) {
1998 gst_buffer_replace (&outbuf, NULL);
1999 ret = GST_GL_UPLOAD_RECONFIGURE;
2001 gst_caps_unref (caps);
2005 upload->priv->method_impl = NULL;
2009 if (outbuf && buffer != outbuf)
2010 gst_buffer_copy_into (outbuf, buffer,
2011 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
2012 *outbuf_ptr = outbuf;
2014 if (ret == GST_GL_UPLOAD_RECONFIGURE)
2015 upload->priv->saved_method_i = upload->priv->method_i - 1;
2017 GST_OBJECT_UNLOCK (upload);