gleglimage: Fix GL_OES_EGL_image_external name in debug trace
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-base / gst-libs / gst / gl / gstglupload.c
1 /*
2  * GStreamer
3  * Copyright (C) 2012-2014 Matthew Waters <ystree00@gmail.com>
4  * Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <stdio.h>
27
28 #include "gl.h"
29 #include "gstglupload.h"
30 #include "gstglfuncs.h"
31
32 #if GST_GL_HAVE_PLATFORM_EGL
33 #include "egl/gsteglimage.h"
34 #include "egl/gsteglimage_private.h"
35 #include "egl/gstglmemoryegl.h"
36 #include "egl/gstglcontext_egl.h"
37 #endif
38
39 #if GST_GL_HAVE_DMABUF
40 #include <gst/allocators/gstdmabuf.h>
41 #endif
42
43 #if GST_GL_HAVE_VIV_DIRECTVIV
44 #include <gst/allocators/gstphysmemory.h>
45 #include <gst/gl/gstglfuncs.h>
46 #endif
47
48 /**
49  * SECTION:gstglupload
50  * @title: GstGLUpload
51  * @short_description: an object that uploads to GL textures
52  * @see_also: #GstGLDownload, #GstGLMemory
53  *
54  * #GstGLUpload is an object that uploads data from system memory into GL textures.
55  *
56  * A #GstGLUpload can be created with gst_gl_upload_new()
57  */
58
59 #define USING_OPENGL(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL, 1, 0))
60 #define USING_OPENGL3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL3, 3, 1))
61 #define USING_GLES(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES, 1, 0))
62 #define USING_GLES2(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 2, 0))
63 #define USING_GLES3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 3, 0))
64
65 GST_DEBUG_CATEGORY_STATIC (gst_gl_upload_debug);
66 #define GST_CAT_DEFAULT gst_gl_upload_debug
67
68 static void gst_gl_upload_finalize (GObject * object);
69
70 static GstGLTextureTarget
71 _caps_get_texture_target (GstCaps * caps, GstGLTextureTarget default_target)
72 {
73   GstGLTextureTarget ret = 0;
74   GstStructure *s = gst_caps_get_structure (caps, 0);
75
76   if (gst_structure_has_field_typed (s, "texture-target", G_TYPE_STRING)) {
77     const gchar *target_str = gst_structure_get_string (s, "texture-target");
78     ret = gst_gl_texture_target_from_string (target_str);
79   }
80
81   if (!ret)
82     ret = default_target;
83
84   return ret;
85 }
86
87 /* Define the maximum number of planes we can upload - handle 2 views per buffer */
88 #define GST_GL_UPLOAD_MAX_PLANES (GST_VIDEO_MAX_PLANES * 2)
89
90 typedef struct _UploadMethod UploadMethod;
91
92 struct _GstGLUploadPrivate
93 {
94   GstVideoInfo in_info;
95   GstVideoInfo out_info;
96   GstCaps *in_caps;
97   GstCaps *out_caps;
98
99   GstBuffer *outbuf;
100
101   /* all method impl pointers */
102   gpointer *upload_impl;
103
104   /* current method */
105   const UploadMethod *method;
106   gpointer method_impl;
107   int method_i;
108
109   /* saved method for reconfigure */
110   int saved_method_i;
111 };
112
113 #define DEBUG_INIT \
114   GST_DEBUG_CATEGORY_INIT (gst_gl_upload_debug, "glupload", 0, "upload");
115
116 G_DEFINE_TYPE_WITH_CODE (GstGLUpload, gst_gl_upload, GST_TYPE_OBJECT,
117     G_ADD_PRIVATE (GstGLUpload) DEBUG_INIT);
118
119 static GstCaps *
120 _set_caps_features_with_passthrough (const GstCaps * caps,
121     const gchar * feature_name, GstCapsFeatures * passthrough)
122 {
123   guint i, j, m, n;
124   GstCaps *tmp;
125
126   tmp = gst_caps_new_empty ();
127
128   n = gst_caps_get_size (caps);
129   for (i = 0; i < n; i++) {
130     GstCapsFeatures *features, *orig_features;
131     GstStructure *s = gst_caps_get_structure (caps, i);
132
133     orig_features = gst_caps_get_features (caps, i);
134     features = gst_caps_features_new (feature_name, NULL);
135
136     if (gst_caps_features_is_any (orig_features)) {
137       /* if we have any features, we add both the features with and without @passthrough */
138       gst_caps_append_structure_full (tmp, gst_structure_copy (s),
139           gst_caps_features_copy (features));
140
141       m = gst_caps_features_get_size (passthrough);
142       for (j = 0; j < m; j++) {
143         const gchar *feature = gst_caps_features_get_nth (passthrough, j);
144
145         /* if we already have the features */
146         if (gst_caps_features_contains (features, feature))
147           continue;
148
149         gst_caps_features_add (features, feature);
150       }
151     } else {
152       m = gst_caps_features_get_size (orig_features);
153       for (j = 0; j < m; j++) {
154         const gchar *feature = gst_caps_features_get_nth (orig_features, j);
155
156         /* if we already have the features */
157         if (gst_caps_features_contains (features, feature))
158           continue;
159
160         if (g_strcmp0 (feature, GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY) == 0)
161           continue;
162
163         if (gst_caps_features_contains (passthrough, feature)) {
164           gst_caps_features_add (features, feature);
165         }
166       }
167     }
168
169     gst_caps_append_structure_full (tmp, gst_structure_copy (s), features);
170   }
171
172   return tmp;
173 }
174
175 static GstCaps *
176 _caps_intersect_texture_target (GstCaps * caps, GstGLTextureTarget target_mask)
177 {
178   GValue targets = G_VALUE_INIT;
179   GstCaps *ret, *target;
180
181   target = gst_caps_copy (caps);
182   gst_gl_value_set_texture_target_from_mask (&targets, target_mask);
183   gst_caps_set_value (target, "texture-target", &targets);
184
185   ret = gst_caps_intersect_full (caps, target, GST_CAPS_INTERSECT_FIRST);
186
187   g_value_unset (&targets);
188   gst_caps_unref (target);
189   return ret;
190 }
191
192 typedef enum
193 {
194   METHOD_FLAG_CAN_SHARE_CONTEXT = 1,
195   METHOD_FLAG_CAN_ACCEPT_RAW = 2,       /* This method can accept raw memory input caps */
196 } GstGLUploadMethodFlags;
197
198 struct _UploadMethod
199 {
200   const gchar *name;
201   GstGLUploadMethodFlags flags;
202
203   GstStaticCaps *input_template_caps;
204
205     gpointer (*new) (GstGLUpload * upload);
206   GstCaps *(*transform_caps) (gpointer impl, GstGLContext * context,
207       GstPadDirection direction, GstCaps * caps);
208     gboolean (*accept) (gpointer impl, GstBuffer * buffer, GstCaps * in_caps,
209       GstCaps * out_caps);
210   void (*propose_allocation) (gpointer impl, GstQuery * decide_query,
211       GstQuery * query);
212     GstGLUploadReturn (*perform) (gpointer impl, GstBuffer * buffer,
213       GstBuffer ** outbuf);
214   void (*free) (gpointer impl);
215 } _UploadMethod;
216
217 struct GLMemoryUpload
218 {
219   GstGLUpload *upload;
220   GstGLTextureTarget input_target;
221   GstGLTextureTarget output_target;
222 };
223
224 static gpointer
225 _gl_memory_upload_new (GstGLUpload * upload)
226 {
227   struct GLMemoryUpload *mem = g_new0 (struct GLMemoryUpload, 1);
228
229   mem->upload = upload;
230   mem->input_target = GST_GL_TEXTURE_TARGET_NONE;
231   mem->output_target = GST_GL_TEXTURE_TARGET_NONE;
232
233   return mem;
234 }
235
236 static GstCaps *
237 _gl_memory_upload_transform_caps (gpointer impl, GstGLContext * context,
238     GstPadDirection direction, GstCaps * caps)
239 {
240   struct GLMemoryUpload *upload = impl;
241   GstCapsFeatures *passthrough =
242       gst_caps_features_from_string
243       (GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
244   GstCaps *ret;
245
246   ret =
247       _set_caps_features_with_passthrough (caps,
248       GST_CAPS_FEATURE_MEMORY_GL_MEMORY, passthrough);
249
250   gst_caps_features_free (passthrough);
251
252   if (direction == GST_PAD_SINK) {
253     GstCaps *tmp;
254     GstGLTextureTarget target_mask;
255
256     if (upload->input_target != GST_GL_TEXTURE_TARGET_NONE) {
257       target_mask = 1 << upload->input_target;
258     } else {
259       target_mask = 1 << GST_GL_TEXTURE_TARGET_2D |
260           1 << GST_GL_TEXTURE_TARGET_RECTANGLE |
261           1 << GST_GL_TEXTURE_TARGET_EXTERNAL_OES;
262     }
263
264     tmp = _caps_intersect_texture_target (ret, target_mask);
265     gst_caps_unref (ret);
266     ret = tmp;
267   } else {
268     gint i, n;
269
270     n = gst_caps_get_size (ret);
271     for (i = 0; i < n; i++) {
272       GstStructure *s = gst_caps_get_structure (ret, i);
273
274       gst_structure_remove_fields (s, "texture-target", NULL);
275     }
276   }
277
278   return ret;
279 }
280
281 static gboolean
282 _gl_memory_upload_accept (gpointer impl, GstBuffer * buffer, GstCaps * in_caps,
283     GstCaps * out_caps)
284 {
285   struct GLMemoryUpload *upload = impl;
286   GstCapsFeatures *features;
287   int i;
288
289   features = gst_caps_get_features (out_caps, 0);
290   if (!gst_caps_features_contains (features, GST_CAPS_FEATURE_MEMORY_GL_MEMORY))
291     return FALSE;
292
293   features = gst_caps_get_features (in_caps, 0);
294   if (!gst_caps_features_contains (features, GST_CAPS_FEATURE_MEMORY_GL_MEMORY)
295       && !gst_caps_features_contains (features,
296           GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY))
297     return FALSE;
298
299   if (buffer) {
300     GstVideoInfo *in_info = &upload->upload->priv->in_info;
301     guint expected_memories = GST_VIDEO_INFO_N_PLANES (in_info);
302
303     /* Support stereo views for separated multiview mode */
304     if (GST_VIDEO_INFO_MULTIVIEW_MODE (in_info) ==
305         GST_VIDEO_MULTIVIEW_MODE_SEPARATED)
306       expected_memories *= GST_VIDEO_INFO_VIEWS (in_info);
307
308     if (gst_buffer_n_memory (buffer) != expected_memories)
309       return FALSE;
310
311     for (i = 0; i < expected_memories; i++) {
312       GstMemory *mem = gst_buffer_peek_memory (buffer, i);
313
314       if (!gst_is_gl_memory (mem))
315         return FALSE;
316     }
317   }
318
319   return TRUE;
320 }
321
322 static void
323 _gl_memory_upload_propose_allocation (gpointer impl, GstQuery * decide_query,
324     GstQuery * query)
325 {
326   struct GLMemoryUpload *upload = impl;
327   GstBufferPool *pool = NULL;
328   guint n_pools, i;
329   GstCaps *caps;
330   GstCapsFeatures *features;
331
332   gst_query_parse_allocation (query, &caps, NULL);
333   if (caps == NULL)
334     goto invalid_caps;
335   features = gst_caps_get_features (caps, 0);
336
337   /* Only offer our custom allocator if that type of memory was negotiated. */
338   if (gst_caps_features_contains (features, GST_CAPS_FEATURE_MEMORY_GL_MEMORY)) {
339     GstAllocator *allocator;
340     GstAllocationParams params;
341     gst_allocation_params_init (&params);
342
343     allocator =
344         GST_ALLOCATOR (gst_gl_memory_allocator_get_default (upload->
345             upload->context));
346     gst_query_add_allocation_param (query, allocator, &params);
347     gst_object_unref (allocator);
348
349 #if GST_GL_HAVE_PLATFORM_EGL
350     if (upload->upload->context
351         && gst_gl_context_get_gl_platform (upload->upload->context) ==
352         GST_GL_PLATFORM_EGL) {
353       allocator =
354           GST_ALLOCATOR (gst_allocator_find (GST_GL_MEMORY_EGL_ALLOCATOR_NAME));
355       gst_query_add_allocation_param (query, allocator, &params);
356       gst_object_unref (allocator);
357     }
358 #endif
359   }
360
361   n_pools = gst_query_get_n_allocation_pools (query);
362   for (i = 0; i < n_pools; i++) {
363     gst_query_parse_nth_allocation_pool (query, i, &pool, NULL, NULL, NULL);
364     if (!GST_IS_GL_BUFFER_POOL (pool)) {
365       gst_object_unref (pool);
366       pool = NULL;
367     }
368   }
369
370   if (!pool) {
371     GstStructure *config;
372     GstVideoInfo info;
373     gsize size;
374
375
376     if (!gst_video_info_from_caps (&info, caps))
377       goto invalid_caps;
378
379     pool = gst_gl_buffer_pool_new (upload->upload->context);
380     config = gst_buffer_pool_get_config (pool);
381
382     /* the normal size of a frame */
383     size = info.size;
384     gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
385     gst_buffer_pool_config_add_option (config,
386         GST_BUFFER_POOL_OPTION_GL_SYNC_META);
387     if (upload->upload->priv->out_caps) {
388       GstGLTextureTarget target;
389       const gchar *target_pool_option_str;
390
391       target =
392           _caps_get_texture_target (upload->upload->priv->out_caps,
393           GST_GL_TEXTURE_TARGET_2D);
394       target_pool_option_str =
395           gst_gl_texture_target_to_buffer_pool_option (target);
396       gst_buffer_pool_config_add_option (config, target_pool_option_str);
397     }
398
399     if (!gst_buffer_pool_set_config (pool, config)) {
400       gst_object_unref (pool);
401       goto config_failed;
402     }
403
404     gst_query_add_allocation_pool (query, pool, size, 1, 0);
405   }
406
407   if (pool)
408     gst_object_unref (pool);
409
410   return;
411
412 invalid_caps:
413   {
414     GST_WARNING_OBJECT (upload->upload, "invalid caps specified");
415     return;
416   }
417 config_failed:
418   {
419     GST_WARNING_OBJECT (upload->upload, "failed setting config");
420     return;
421   }
422 }
423
424 static GstGLUploadReturn
425 _gl_memory_upload_perform (gpointer impl, GstBuffer * buffer,
426     GstBuffer ** outbuf)
427 {
428   struct GLMemoryUpload *upload = impl;
429   GstGLMemory *gl_mem;
430   int i, n;
431
432   n = gst_buffer_n_memory (buffer);
433   for (i = 0; i < n; i++) {
434     GstMemory *mem = gst_buffer_peek_memory (buffer, i);
435
436     gl_mem = (GstGLMemory *) mem;
437     if (!gst_gl_context_can_share (upload->upload->context,
438             gl_mem->mem.context))
439       return GST_GL_UPLOAD_UNSHARED_GL_CONTEXT;
440
441     if (upload->output_target == GST_GL_TEXTURE_TARGET_NONE &&
442         upload->upload->priv->out_caps) {
443       upload->output_target =
444           _caps_get_texture_target (upload->upload->priv->out_caps,
445           GST_GL_TEXTURE_TARGET_NONE);
446     }
447
448     /* always track the last input texture target so ::transform_caps() can
449      * use it to build the output caps */
450     upload->input_target = gl_mem->tex_target;
451     if (upload->output_target != gl_mem->tex_target) {
452       *outbuf = NULL;
453       return GST_GL_UPLOAD_RECONFIGURE;
454     }
455
456     if (gst_is_gl_memory_pbo (mem))
457       gst_gl_memory_pbo_upload_transfer ((GstGLMemoryPBO *) mem);
458   }
459
460   *outbuf = gst_buffer_ref (buffer);
461
462   return GST_GL_UPLOAD_DONE;
463 }
464
465 static void
466 _gl_memory_upload_free (gpointer impl)
467 {
468   g_free (impl);
469 }
470
471
472 static GstStaticCaps _gl_memory_upload_caps =
473 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
474     (GST_CAPS_FEATURE_MEMORY_GL_MEMORY, GST_GL_MEMORY_VIDEO_FORMATS_STR));
475
476 static const UploadMethod _gl_memory_upload = {
477   "GLMemory",
478   METHOD_FLAG_CAN_SHARE_CONTEXT,
479   &_gl_memory_upload_caps,
480   &_gl_memory_upload_new,
481   &_gl_memory_upload_transform_caps,
482   &_gl_memory_upload_accept,
483   &_gl_memory_upload_propose_allocation,
484   &_gl_memory_upload_perform,
485   &_gl_memory_upload_free
486 };
487
488 #if GST_GL_HAVE_DMABUF
489 struct DmabufUpload
490 {
491   GstGLUpload *upload;
492
493   GstEGLImage *eglimage[GST_VIDEO_MAX_PLANES];
494   GstGLFormat formats[GST_VIDEO_MAX_PLANES];
495   GstBuffer *outbuf;
496   GstGLVideoAllocationParams *params;
497   guint n_mem;
498
499   gboolean direct;
500   GstGLTextureTarget target;
501   GstVideoInfo out_info;
502   /* only used for pointer comparison */
503   gpointer out_caps;
504 };
505
506 static GstStaticCaps _dma_buf_upload_caps =
507     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
508     (GST_CAPS_FEATURE_MEMORY_DMABUF,
509         GST_GL_MEMORY_VIDEO_FORMATS_STR) ";"
510     GST_VIDEO_CAPS_MAKE (GST_GL_MEMORY_VIDEO_FORMATS_STR));
511
512 static gpointer
513 _dma_buf_upload_new (GstGLUpload * upload)
514 {
515   struct DmabufUpload *dmabuf = g_new0 (struct DmabufUpload, 1);
516   dmabuf->upload = upload;
517   dmabuf->target = GST_GL_TEXTURE_TARGET_2D;
518   return dmabuf;
519 }
520
521 static GstCaps *
522 _dma_buf_upload_transform_caps (gpointer impl, GstGLContext * context,
523     GstPadDirection direction, GstCaps * caps)
524 {
525   struct DmabufUpload *dmabuf = impl;
526   GstCapsFeatures *passthrough;
527   GstCaps *ret;
528
529   if (context) {
530     const GstGLFuncs *gl = context->gl_vtable;
531
532     if (!gl->EGLImageTargetTexture2D)
533       return NULL;
534
535     /* Don't propose DMABuf caps feature unless it can be supported */
536     if (gst_gl_context_get_gl_platform (context) != GST_GL_PLATFORM_EGL)
537       return NULL;
538
539     if (!gst_gl_context_check_feature (context, "EGL_KHR_image_base"))
540       return NULL;
541   }
542
543   passthrough = gst_caps_features_from_string
544       (GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
545
546   if (direction == GST_PAD_SINK) {
547     GstCaps *tmp;
548
549     ret =
550         _set_caps_features_with_passthrough (caps,
551         GST_CAPS_FEATURE_MEMORY_GL_MEMORY, passthrough);
552
553     tmp = _caps_intersect_texture_target (ret, 1 << GST_GL_TEXTURE_TARGET_2D);
554     gst_caps_unref (ret);
555     ret = tmp;
556   } else {
557     gint i, n;
558
559     ret =
560         _set_caps_features_with_passthrough (caps,
561         GST_CAPS_FEATURE_MEMORY_DMABUF, passthrough);
562
563     n = gst_caps_get_size (ret);
564     for (i = 0; i < n; i++) {
565       GstStructure *s = gst_caps_get_structure (ret, i);
566
567       gst_structure_remove_fields (s, "texture-target", NULL);
568     }
569   }
570
571   gst_caps_features_free (passthrough);
572
573   GST_DEBUG_OBJECT (dmabuf->upload, "transformed %" GST_PTR_FORMAT " into %"
574       GST_PTR_FORMAT, caps, ret);
575
576   return ret;
577 }
578
579 static GQuark
580 _eglimage_quark (gint plane)
581 {
582   static GQuark quark[5] = { 0 };
583   static const gchar *quark_str[] = {
584     "GstGLDMABufEGLImage0",
585     "GstGLDMABufEGLImage1",
586     "GstGLDMABufEGLImage2",
587     "GstGLDMABufEGLImage3",
588     "GstGLDMABufEGLImage",
589   };
590
591   if (!quark[plane])
592     quark[plane] = g_quark_from_static_string (quark_str[plane]);
593
594   return quark[plane];
595 }
596
597 static GstEGLImage *
598 _get_cached_eglimage (GstMemory * mem, gint plane)
599 {
600   return gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
601       _eglimage_quark (plane));
602 }
603
604 static void
605 _set_cached_eglimage (GstMemory * mem, GstEGLImage * eglimage, gint plane)
606 {
607   return gst_mini_object_set_qdata (GST_MINI_OBJECT (mem),
608       _eglimage_quark (plane), eglimage, (GDestroyNotify) gst_egl_image_unref);
609 }
610
611 static gboolean
612 _dma_buf_upload_accept (gpointer impl, GstBuffer * buffer, GstCaps * in_caps,
613     GstCaps * out_caps)
614 {
615   struct DmabufUpload *dmabuf = impl;
616   GstVideoInfo *in_info = &dmabuf->upload->priv->in_info;
617   GstVideoInfo *out_info = &dmabuf->out_info;
618   guint n_planes = GST_VIDEO_INFO_N_PLANES (in_info);
619   GstVideoMeta *meta;
620   guint n_mem;
621   GstMemory *mems[GST_VIDEO_MAX_PLANES];
622   gsize offset[GST_VIDEO_MAX_PLANES];
623   gint fd[GST_VIDEO_MAX_PLANES];
624   guint i;
625
626   n_mem = gst_buffer_n_memory (buffer);
627   meta = gst_buffer_get_video_meta (buffer);
628
629   if (!dmabuf->upload->context->gl_vtable->EGLImageTargetTexture2D)
630     return FALSE;
631
632   /* dmabuf upload is only supported with EGL contexts. */
633   if (gst_gl_context_get_gl_platform (dmabuf->upload->context) !=
634       GST_GL_PLATFORM_EGL)
635     return FALSE;
636
637   if (!gst_gl_context_check_feature (dmabuf->upload->context,
638           "EGL_KHR_image_base")) {
639     GST_DEBUG_OBJECT (dmabuf->upload, "no EGL_KHR_image_base extension");
640     return FALSE;
641   }
642
643   if (dmabuf->target == GST_GL_TEXTURE_TARGET_EXTERNAL_OES &&
644       !gst_gl_context_check_feature (dmabuf->upload->context,
645           "GL_OES_EGL_image_external")) {
646     GST_DEBUG_OBJECT (dmabuf->upload, "no GL_OES_EGL_image_external extension");
647     return FALSE;
648   }
649
650   /* This will eliminate most non-dmabuf out there */
651   if (!gst_is_dmabuf_memory (gst_buffer_peek_memory (buffer, 0))) {
652     GST_DEBUG_OBJECT (dmabuf->upload, "input not dmabuf");
653     return FALSE;
654   }
655
656   /* We cannot have multiple dmabuf per plane */
657   if (n_mem > n_planes) {
658     GST_DEBUG_OBJECT (dmabuf->upload,
659         "number of memory (%u) != number of planes (%u)", n_mem, n_planes);
660     return FALSE;
661   }
662
663   /* Update video info based on video meta */
664   if (meta) {
665     in_info->width = meta->width;
666     in_info->height = meta->height;
667
668     for (i = 0; i < meta->n_planes; i++) {
669       in_info->offset[i] = meta->offset[i];
670       in_info->stride[i] = meta->stride[i];
671     }
672   }
673
674   if (out_caps != dmabuf->out_caps) {
675     dmabuf->out_caps = out_caps;
676     if (!gst_video_info_from_caps (out_info, out_caps))
677       return FALSE;
678
679     /*
680      * When we zero-copy tiles, we need to propagate the strides, which contains
681      * the tile dimension. This is because the shader needs to know the padded
682      * size in order to correctly sample into these special buffer.
683      */
684     if (meta && GST_VIDEO_FORMAT_INFO_IS_TILED (out_info->finfo)) {
685       out_info->width = meta->width;
686       out_info->height = meta->height;
687
688       for (i = 0; i < meta->n_planes; i++) {
689         out_info->offset[i] = meta->offset[i];
690         out_info->stride[i] = meta->stride[i];
691       }
692     }
693   }
694
695   if (dmabuf->params)
696     gst_gl_allocation_params_free ((GstGLAllocationParams *) dmabuf->params);
697   if (!(dmabuf->params =
698           gst_gl_video_allocation_params_new_wrapped_gl_handle (dmabuf->
699               upload->context, NULL, out_info, -1, NULL, dmabuf->target, 0,
700               NULL, NULL, NULL)))
701     return FALSE;
702
703   /* Find and validate all memories */
704   for (i = 0; i < n_planes; i++) {
705     guint plane_size;
706     guint length;
707     guint mem_idx;
708     gsize mem_skip;
709
710     plane_size = gst_gl_get_plane_data_size (in_info, NULL, i);
711
712     if (!gst_buffer_find_memory (buffer, in_info->offset[i], plane_size,
713             &mem_idx, &length, &mem_skip)) {
714       GST_DEBUG_OBJECT (dmabuf->upload, "could not find memory %u", i);
715       return FALSE;
716     }
717
718     /* We can't have more then one dmabuf per plane */
719     if (length != 1) {
720       GST_DEBUG_OBJECT (dmabuf->upload, "data for plane %u spans %u memories",
721           i, length);
722       return FALSE;
723     }
724
725     mems[i] = gst_buffer_peek_memory (buffer, mem_idx);
726
727     /* And all memory found must be dmabuf */
728     if (!gst_is_dmabuf_memory (mems[i])) {
729       GST_DEBUG_OBJECT (dmabuf->upload, "memory %u is not dmabuf", i);
730       return FALSE;
731     }
732
733     offset[i] = mems[i]->offset + mem_skip;
734     fd[i] = gst_dmabuf_memory_get_fd (mems[i]);
735   }
736
737   if (dmabuf->direct) {
738     /* Check if this format is supported by the driver */
739     dmabuf->n_mem = 1;
740     if (!gst_egl_image_check_dmabuf_direct (dmabuf->upload->context, in_info,
741             dmabuf->target)) {
742       GST_DEBUG_OBJECT (dmabuf->upload, "direct check failed");
743       return FALSE;
744     }
745   } else
746     dmabuf->n_mem = n_planes;
747
748   /* Now create an EGLImage for each dmabufs */
749   for (i = 0; i < dmabuf->n_mem; i++) {
750     gint cache_id = dmabuf->direct ? 4 : i;
751
752     /* check if one is cached */
753     dmabuf->eglimage[i] = _get_cached_eglimage (mems[i], cache_id);
754     if (dmabuf->eglimage[i]) {
755       dmabuf->formats[i] = dmabuf->eglimage[i]->format;
756       continue;
757     }
758
759     /* otherwise create one and cache it */
760     if (dmabuf->direct)
761       dmabuf->eglimage[i] =
762           gst_egl_image_from_dmabuf_direct_target (dmabuf->upload->context, fd,
763           offset, in_info, dmabuf->target);
764     else
765       dmabuf->eglimage[i] = gst_egl_image_from_dmabuf (dmabuf->upload->context,
766           fd[i], in_info, i, offset[i]);
767
768     if (!dmabuf->eglimage[i]) {
769       GST_DEBUG_OBJECT (dmabuf->upload, "could not create eglimage");
770       return FALSE;
771     }
772
773     _set_cached_eglimage (mems[i], dmabuf->eglimage[i], cache_id);
774     dmabuf->formats[i] = dmabuf->eglimage[i]->format;
775   }
776
777   return TRUE;
778 }
779
780 static void
781 _dma_buf_upload_propose_allocation (gpointer impl, GstQuery * decide_query,
782     GstQuery * query)
783 {
784   /* nothing to do for now. */
785 }
786
787 static void
788 _dma_buf_upload_perform_gl_thread (GstGLContext * context,
789     struct DmabufUpload *dmabuf)
790 {
791   GstGLMemoryAllocator *allocator;
792
793   allocator =
794       GST_GL_MEMORY_ALLOCATOR (gst_allocator_find
795       (GST_GL_MEMORY_EGL_ALLOCATOR_NAME));
796
797   /* FIXME: buffer pool */
798   dmabuf->outbuf = gst_buffer_new ();
799   gst_gl_memory_setup_buffer (allocator, dmabuf->outbuf, dmabuf->params,
800       dmabuf->formats, (gpointer *) dmabuf->eglimage, dmabuf->n_mem);
801   gst_object_unref (allocator);
802 }
803
804 static GstGLUploadReturn
805 _dma_buf_upload_perform (gpointer impl, GstBuffer * buffer, GstBuffer ** outbuf)
806 {
807   struct DmabufUpload *dmabuf = impl;
808
809   /* The direct path sets sinkpad caps to RGBA but this may be incorrect for
810    * the non-direct path, if that path fails to accept. In that case, we need
811    * to reconfigure.
812    */
813   if (!dmabuf->direct &&
814       GST_VIDEO_INFO_FORMAT (&dmabuf->upload->priv->in_info) !=
815       GST_VIDEO_INFO_FORMAT (&dmabuf->out_info))
816     return GST_GL_UPLOAD_RECONFIGURE;
817
818   gst_gl_context_thread_add (dmabuf->upload->context,
819       (GstGLContextThreadFunc) _dma_buf_upload_perform_gl_thread, dmabuf);
820
821   if (!dmabuf->outbuf)
822     return GST_GL_UPLOAD_ERROR;
823
824   gst_buffer_add_parent_buffer_meta (dmabuf->outbuf, buffer);
825
826   *outbuf = dmabuf->outbuf;
827   dmabuf->outbuf = NULL;
828
829   return GST_GL_UPLOAD_DONE;
830 }
831
832 static void
833 _dma_buf_upload_free (gpointer impl)
834 {
835   struct DmabufUpload *dmabuf = impl;
836
837   if (dmabuf->params)
838     gst_gl_allocation_params_free ((GstGLAllocationParams *) dmabuf->params);
839
840   g_free (impl);
841 }
842
843 static const UploadMethod _dma_buf_upload = {
844   "Dmabuf",
845   METHOD_FLAG_CAN_ACCEPT_RAW,
846   &_dma_buf_upload_caps,
847   &_dma_buf_upload_new,
848   &_dma_buf_upload_transform_caps,
849   &_dma_buf_upload_accept,
850   &_dma_buf_upload_propose_allocation,
851   &_dma_buf_upload_perform,
852   &_dma_buf_upload_free
853 };
854
855 /* a variant of the DMABuf uploader that relies on HW color conversion instead
856  * of shaders */
857
858 static gpointer
859 _direct_dma_buf_upload_new (GstGLUpload * upload)
860 {
861   struct DmabufUpload *dmabuf = _dma_buf_upload_new (upload);
862   dmabuf->direct = TRUE;
863   gst_video_info_init (&dmabuf->out_info);
864   return dmabuf;
865 }
866
867 static GstCaps *
868 _direct_dma_buf_upload_transform_caps (gpointer impl, GstGLContext * context,
869     GstPadDirection direction, GstCaps * caps)
870 {
871   struct DmabufUpload *dmabuf = impl;
872   GstCapsFeatures *passthrough;
873   GstCaps *ret;
874
875   if (context) {
876     const GstGLFuncs *gl = context->gl_vtable;
877
878     if (!gl->EGLImageTargetTexture2D)
879       return NULL;
880
881     /* Don't propose direct DMABuf caps feature unless it can be supported */
882     if (gst_gl_context_get_gl_platform (context) != GST_GL_PLATFORM_EGL)
883       return NULL;
884
885     if (dmabuf->target == GST_GL_TEXTURE_TARGET_EXTERNAL_OES &&
886         !gst_gl_context_check_feature (context, "GL_OES_EGL_image_external"))
887       return NULL;
888   }
889
890   passthrough = gst_caps_features_from_string
891       (GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
892
893   if (direction == GST_PAD_SINK) {
894     gint i, n;
895     GstCaps *tmp;
896     GstGLTextureTarget target_mask;
897
898     ret =
899         _set_caps_features_with_passthrough (caps,
900         GST_CAPS_FEATURE_MEMORY_GL_MEMORY, passthrough);
901
902     gst_caps_set_simple (ret, "format", G_TYPE_STRING, "RGBA", NULL);
903
904     n = gst_caps_get_size (ret);
905     for (i = 0; i < n; i++) {
906       GstStructure *s = gst_caps_get_structure (ret, i);
907
908       gst_structure_remove_fields (s, "chroma-site", NULL);
909       gst_structure_remove_fields (s, "colorimetry", NULL);
910     }
911
912     target_mask = 1 << dmabuf->target;
913     tmp = _caps_intersect_texture_target (ret, target_mask);
914     gst_caps_unref (ret);
915     ret = tmp;
916   } else {
917     gint i, n;
918     GstCaps *tmp;
919     GValue formats = G_VALUE_INIT;
920     gchar *format_str = g_strdup (GST_GL_MEMORY_VIDEO_FORMATS_STR);
921
922     ret =
923         _set_caps_features_with_passthrough (caps,
924         GST_CAPS_FEATURE_MEMORY_DMABUF, passthrough);
925
926     g_value_init (&formats, GST_TYPE_LIST);
927     gst_value_deserialize (&formats, format_str);
928     tmp = gst_caps_copy (ret);
929     gst_caps_set_value (tmp, "format", &formats);
930     gst_caps_append (ret, tmp);
931     g_free (format_str);
932     g_value_unset (&formats);
933
934     n = gst_caps_get_size (ret);
935     for (i = 0; i < n; i++) {
936       GstStructure *s = gst_caps_get_structure (ret, i);
937
938       gst_structure_remove_fields (s, "texture-target", NULL);
939     }
940   }
941
942   gst_caps_features_free (passthrough);
943
944   GST_DEBUG_OBJECT (dmabuf->upload, "transformed %" GST_PTR_FORMAT " into %"
945       GST_PTR_FORMAT, caps, ret);
946
947   return ret;
948 }
949
950 static const UploadMethod _direct_dma_buf_upload = {
951   "DirectDmabuf",
952   METHOD_FLAG_CAN_ACCEPT_RAW,
953   &_dma_buf_upload_caps,
954   &_direct_dma_buf_upload_new,
955   &_direct_dma_buf_upload_transform_caps,
956   &_dma_buf_upload_accept,
957   &_dma_buf_upload_propose_allocation,
958   &_dma_buf_upload_perform,
959   &_dma_buf_upload_free
960 };
961
962 /* a variant of the direct DMABuf uploader that uses external OES textures */
963
964 static gpointer
965 _direct_dma_buf_external_upload_new (GstGLUpload * upload)
966 {
967   struct DmabufUpload *dmabuf = _direct_dma_buf_upload_new (upload);
968   dmabuf->target = GST_GL_TEXTURE_TARGET_EXTERNAL_OES;
969   return dmabuf;
970 }
971
972 static const UploadMethod _direct_dma_buf_external_upload = {
973   "DirectDmabufExternal",
974   METHOD_FLAG_CAN_ACCEPT_RAW,
975   &_dma_buf_upload_caps,
976   &_direct_dma_buf_external_upload_new,
977   &_direct_dma_buf_upload_transform_caps,
978   &_dma_buf_upload_accept,
979   &_dma_buf_upload_propose_allocation,
980   &_dma_buf_upload_perform,
981   &_dma_buf_upload_free
982 };
983
984 #endif /* GST_GL_HAVE_DMABUF */
985
986 struct GLUploadMeta
987 {
988   GstGLUpload *upload;
989
990   gboolean result;
991   GstVideoGLTextureUploadMeta *meta;
992   guint texture_ids[GST_GL_UPLOAD_MAX_PLANES];
993   GstBufferPool *pool;
994 };
995
996 static gpointer
997 _upload_meta_upload_new (GstGLUpload * upload)
998 {
999   struct GLUploadMeta *meta = g_new0 (struct GLUploadMeta, 1);
1000
1001   meta->upload = upload;
1002   meta->pool = NULL;
1003
1004   return meta;
1005 }
1006
1007 static GstCaps *
1008 _upload_meta_upload_transform_caps (gpointer impl, GstGLContext * context,
1009     GstPadDirection direction, GstCaps * caps)
1010 {
1011   GstCapsFeatures *passthrough =
1012       gst_caps_features_from_string
1013       (GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
1014   GstCaps *ret;
1015
1016   if (direction == GST_PAD_SINK) {
1017     GstCaps *tmp;
1018
1019     ret =
1020         _set_caps_features_with_passthrough (caps,
1021         GST_CAPS_FEATURE_MEMORY_GL_MEMORY, passthrough);
1022
1023     tmp = _caps_intersect_texture_target (ret, 1 << GST_GL_TEXTURE_TARGET_2D);
1024     gst_caps_unref (ret);
1025     ret = tmp;
1026   } else {
1027     gint i, n;
1028
1029     ret =
1030         _set_caps_features_with_passthrough (caps,
1031         GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META, passthrough);
1032     gst_caps_set_simple (ret, "format", G_TYPE_STRING, "RGBA", NULL);
1033
1034     n = gst_caps_get_size (ret);
1035     for (i = 0; i < n; i++) {
1036       GstStructure *s = gst_caps_get_structure (ret, i);
1037
1038       gst_structure_remove_fields (s, "texture-target", NULL);
1039     }
1040   }
1041
1042   gst_caps_features_free (passthrough);
1043
1044   return ret;
1045 }
1046
1047 static gboolean
1048 _upload_meta_upload_accept (gpointer impl, GstBuffer * buffer,
1049     GstCaps * in_caps, GstCaps * out_caps)
1050 {
1051   struct GLUploadMeta *upload = impl;
1052   GstCapsFeatures *features;
1053   GstVideoGLTextureUploadMeta *meta;
1054   gboolean ret = TRUE;
1055   GstStructure *config;
1056   gsize size;
1057
1058   features = gst_caps_get_features (in_caps, 0);
1059
1060   if (!gst_caps_features_contains (features,
1061           GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META))
1062     ret = FALSE;
1063
1064   features = gst_caps_get_features (out_caps, 0);
1065   if (!gst_caps_features_contains (features, GST_CAPS_FEATURE_MEMORY_GL_MEMORY))
1066     ret = FALSE;
1067
1068   if (!ret)
1069     return ret;
1070
1071   if (upload->pool == NULL)
1072     upload->pool = gst_gl_buffer_pool_new (upload->upload->context);
1073
1074   if (!gst_buffer_pool_is_active (upload->pool)) {
1075     config = gst_buffer_pool_get_config (upload->pool);
1076
1077     size = upload->upload->priv->in_info.size;
1078     gst_buffer_pool_config_set_params (config, in_caps, size, 0, 0);
1079
1080     if (!gst_buffer_pool_set_config (upload->pool, config)) {
1081       GST_WARNING_OBJECT (upload->upload, "failed to set bufferpool config");
1082       return FALSE;
1083     }
1084     gst_buffer_pool_set_active (upload->pool, TRUE);
1085   }
1086
1087   if (buffer) {
1088     if ((meta = gst_buffer_get_video_gl_texture_upload_meta (buffer)) == NULL)
1089       return FALSE;
1090
1091     if (meta->texture_type[0] != GST_VIDEO_GL_TEXTURE_TYPE_RGBA) {
1092       GST_FIXME_OBJECT (upload, "only single rgba texture supported");
1093       return FALSE;
1094     }
1095
1096     if (meta->texture_orientation !=
1097         GST_VIDEO_GL_TEXTURE_ORIENTATION_X_NORMAL_Y_NORMAL) {
1098       GST_FIXME_OBJECT (upload, "only x-normal, y-normal textures supported");
1099       return FALSE;
1100     }
1101   }
1102
1103   return TRUE;
1104 }
1105
1106 static void
1107 _upload_meta_upload_propose_allocation (gpointer impl, GstQuery * decide_query,
1108     GstQuery * query)
1109 {
1110   struct GLUploadMeta *upload = impl;
1111   GstStructure *gl_context;
1112   gchar *platform, *gl_apis;
1113   gpointer handle;
1114
1115   gl_apis =
1116       gst_gl_api_to_string (gst_gl_context_get_gl_api (upload->upload->
1117           context));
1118   platform =
1119       gst_gl_platform_to_string (gst_gl_context_get_gl_platform (upload->
1120           upload->context));
1121   handle = (gpointer) gst_gl_context_get_gl_context (upload->upload->context);
1122
1123   gl_context =
1124       gst_structure_new ("GstVideoGLTextureUploadMeta", "gst.gl.GstGLContext",
1125       GST_TYPE_GL_CONTEXT, upload->upload->context, "gst.gl.context.handle",
1126       G_TYPE_POINTER, handle, "gst.gl.context.type", G_TYPE_STRING, platform,
1127       "gst.gl.context.apis", G_TYPE_STRING, gl_apis, NULL);
1128   gst_query_add_allocation_meta (query,
1129       GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, gl_context);
1130
1131   g_free (gl_apis);
1132   g_free (platform);
1133   gst_structure_free (gl_context);
1134 }
1135
1136 /*
1137  * Uploads using gst_video_gl_texture_upload_meta_upload().
1138  * i.e. consumer of GstVideoGLTextureUploadMeta
1139  */
1140 static void
1141 _do_upload_with_meta (GstGLContext * context, struct GLUploadMeta *upload)
1142 {
1143   if (!gst_video_gl_texture_upload_meta_upload (upload->meta,
1144           upload->texture_ids)) {
1145     upload->result = FALSE;
1146     return;
1147   }
1148
1149   upload->result = TRUE;
1150 }
1151
1152 static GstGLUploadReturn
1153 _upload_meta_upload_perform (gpointer impl, GstBuffer * buffer,
1154     GstBuffer ** outbuf)
1155 {
1156   struct GLUploadMeta *upload = impl;
1157   int i;
1158   GstVideoInfo *in_info = &upload->upload->priv->in_info;
1159   guint max_planes = GST_VIDEO_INFO_N_PLANES (in_info);
1160
1161   /* Support stereo views for separated multiview mode */
1162   if (GST_VIDEO_INFO_MULTIVIEW_MODE (in_info) ==
1163       GST_VIDEO_MULTIVIEW_MODE_SEPARATED)
1164     max_planes *= GST_VIDEO_INFO_VIEWS (in_info);
1165
1166   GST_LOG_OBJECT (upload, "Attempting upload with GstVideoGLTextureUploadMeta");
1167
1168   upload->meta = gst_buffer_get_video_gl_texture_upload_meta (buffer);
1169
1170   if (gst_buffer_pool_acquire_buffer (upload->pool, outbuf,
1171           NULL) != GST_FLOW_OK) {
1172     GST_WARNING_OBJECT (upload, "failed to acquire buffer from bufferpool");
1173     return GST_GL_UPLOAD_ERROR;
1174   }
1175
1176   for (i = 0; i < GST_GL_UPLOAD_MAX_PLANES; i++) {
1177     guint tex_id = 0;
1178
1179     if (i < max_planes) {
1180       GstMemory *mem = gst_buffer_peek_memory (*outbuf, i);
1181       tex_id = ((GstGLMemory *) mem)->tex_id;
1182     }
1183
1184     upload->texture_ids[i] = tex_id;
1185   }
1186
1187   GST_LOG ("Uploading with GLTextureUploadMeta with textures "
1188       "%i,%i,%i,%i / %i,%i,%i,%i",
1189       upload->texture_ids[0], upload->texture_ids[1],
1190       upload->texture_ids[2], upload->texture_ids[3],
1191       upload->texture_ids[4], upload->texture_ids[5],
1192       upload->texture_ids[6], upload->texture_ids[7]);
1193
1194   gst_gl_context_thread_add (upload->upload->context,
1195       (GstGLContextThreadFunc) _do_upload_with_meta, upload);
1196
1197   if (!upload->result)
1198     return GST_GL_UPLOAD_ERROR;
1199
1200   return GST_GL_UPLOAD_DONE;
1201 }
1202
1203 static void
1204 _upload_meta_upload_free (gpointer impl)
1205 {
1206   struct GLUploadMeta *upload = impl;
1207
1208   g_return_if_fail (impl != NULL);
1209
1210   if (upload->pool)
1211     gst_object_unref (upload->pool);
1212
1213   g_free (upload);
1214 }
1215
1216 static GstStaticCaps _upload_meta_upload_caps =
1217 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
1218     (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META, "RGBA"));
1219
1220 static const UploadMethod _upload_meta_upload = {
1221   "UploadMeta",
1222   METHOD_FLAG_CAN_SHARE_CONTEXT,
1223   &_upload_meta_upload_caps,
1224   &_upload_meta_upload_new,
1225   &_upload_meta_upload_transform_caps,
1226   &_upload_meta_upload_accept,
1227   &_upload_meta_upload_propose_allocation,
1228   &_upload_meta_upload_perform,
1229   &_upload_meta_upload_free
1230 };
1231
1232 struct RawUploadFrame
1233 {
1234   gint ref_count;
1235   GstVideoFrame frame;
1236 };
1237
1238 struct RawUpload
1239 {
1240   GstGLUpload *upload;
1241   struct RawUploadFrame *in_frame;
1242   GstGLVideoAllocationParams *params;
1243 };
1244
1245 static struct RawUploadFrame *
1246 _raw_upload_frame_new (struct RawUpload *raw, GstBuffer * buffer)
1247 {
1248   struct RawUploadFrame *frame;
1249   GstVideoInfo *info;
1250   gint i;
1251
1252   if (!buffer)
1253     return NULL;
1254
1255   frame = g_slice_new (struct RawUploadFrame);
1256   frame->ref_count = 1;
1257
1258   if (!gst_video_frame_map (&frame->frame, &raw->upload->priv->in_info,
1259           buffer, GST_MAP_READ)) {
1260     g_slice_free (struct RawUploadFrame, frame);
1261     return NULL;
1262   }
1263
1264   raw->upload->priv->in_info = frame->frame.info;
1265   info = &raw->upload->priv->in_info;
1266
1267   /* Recalculate the offsets (and size) */
1268   info->size = 0;
1269   for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
1270     info->offset[i] = info->size;
1271     info->size += gst_gl_get_plane_data_size (info, NULL, i);
1272   }
1273
1274   return frame;
1275 }
1276
1277 static void
1278 _raw_upload_frame_ref (struct RawUploadFrame *frame)
1279 {
1280   g_atomic_int_inc (&frame->ref_count);
1281 }
1282
1283 static void
1284 _raw_upload_frame_unref (struct RawUploadFrame *frame)
1285 {
1286   if (g_atomic_int_dec_and_test (&frame->ref_count)) {
1287     gst_video_frame_unmap (&frame->frame);
1288     g_slice_free (struct RawUploadFrame, frame);
1289   }
1290 }
1291
1292 static gpointer
1293 _raw_data_upload_new (GstGLUpload * upload)
1294 {
1295   struct RawUpload *raw = g_new0 (struct RawUpload, 1);
1296
1297   raw->upload = upload;
1298
1299   return raw;
1300 }
1301
1302 static GstCaps *
1303 _raw_data_upload_transform_caps (gpointer impl, GstGLContext * context,
1304     GstPadDirection direction, GstCaps * caps)
1305 {
1306   GstCapsFeatures *passthrough =
1307       gst_caps_features_from_string
1308       (GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
1309   GstCaps *ret;
1310
1311   if (direction == GST_PAD_SINK) {
1312     GstGLTextureTarget target_mask = 0;
1313     GstCaps *tmp;
1314
1315     ret =
1316         _set_caps_features_with_passthrough (caps,
1317         GST_CAPS_FEATURE_MEMORY_GL_MEMORY, passthrough);
1318
1319     target_mask |= 1 << GST_GL_TEXTURE_TARGET_2D;
1320     target_mask |= 1 << GST_GL_TEXTURE_TARGET_RECTANGLE;
1321     tmp = _caps_intersect_texture_target (ret, target_mask);
1322     gst_caps_unref (ret);
1323     ret = tmp;
1324   } else {
1325     gint i, n;
1326
1327     ret =
1328         _set_caps_features_with_passthrough (caps,
1329         GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY, passthrough);
1330
1331     n = gst_caps_get_size (ret);
1332     for (i = 0; i < n; i++) {
1333       GstStructure *s = gst_caps_get_structure (ret, i);
1334
1335       gst_structure_remove_fields (s, "texture-target", NULL);
1336     }
1337   }
1338
1339   gst_caps_features_free (passthrough);
1340
1341   return ret;
1342 }
1343
1344 static gboolean
1345 _raw_data_upload_accept (gpointer impl, GstBuffer * buffer, GstCaps * in_caps,
1346     GstCaps * out_caps)
1347 {
1348   struct RawUpload *raw = impl;
1349   GstCapsFeatures *features;
1350
1351   features = gst_caps_get_features (out_caps, 0);
1352   if (!gst_caps_features_contains (features, GST_CAPS_FEATURE_MEMORY_GL_MEMORY))
1353     return FALSE;
1354
1355   if (raw->in_frame)
1356     _raw_upload_frame_unref (raw->in_frame);
1357   raw->in_frame = _raw_upload_frame_new (raw, buffer);
1358
1359   if (raw->params)
1360     gst_gl_allocation_params_free ((GstGLAllocationParams *) raw->params);
1361   if (!(raw->params =
1362           gst_gl_video_allocation_params_new_wrapped_data (raw->upload->context,
1363               NULL, &raw->upload->priv->in_info, -1, NULL,
1364               GST_GL_TEXTURE_TARGET_2D, 0, NULL, raw->in_frame,
1365               (GDestroyNotify) _raw_upload_frame_unref)))
1366     return FALSE;
1367
1368   return (raw->in_frame != NULL);
1369 }
1370
1371 static void
1372 _raw_data_upload_propose_allocation (gpointer impl, GstQuery * decide_query,
1373     GstQuery * query)
1374 {
1375   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);
1376 }
1377
1378 static GstGLUploadReturn
1379 _raw_data_upload_perform (gpointer impl, GstBuffer * buffer,
1380     GstBuffer ** outbuf)
1381 {
1382   GstGLBaseMemoryAllocator *allocator;
1383   struct RawUpload *raw = impl;
1384   int i;
1385   GstVideoInfo *in_info = &raw->upload->priv->in_info;
1386   guint n_mem = GST_VIDEO_INFO_N_PLANES (in_info);
1387
1388   allocator =
1389       GST_GL_BASE_MEMORY_ALLOCATOR (gst_gl_memory_allocator_get_default
1390       (raw->upload->context));
1391
1392   /* FIXME Use a buffer pool to cache the generated textures */
1393   *outbuf = gst_buffer_new ();
1394   raw->params->parent.context = raw->upload->context;
1395   if (gst_gl_memory_setup_buffer ((GstGLMemoryAllocator *) allocator, *outbuf,
1396           raw->params, NULL, raw->in_frame->frame.data, n_mem)) {
1397
1398     for (i = 0; i < n_mem; i++)
1399       _raw_upload_frame_ref (raw->in_frame);
1400     gst_buffer_add_gl_sync_meta (raw->upload->context, *outbuf);
1401   } else {
1402     GST_ERROR_OBJECT (raw->upload, "Failed to allocate wrapped texture");
1403     gst_buffer_unref (*outbuf);
1404     gst_object_unref (allocator);
1405     return GST_GL_UPLOAD_ERROR;
1406   }
1407   gst_object_unref (allocator);
1408   _raw_upload_frame_unref (raw->in_frame);
1409   raw->in_frame = NULL;
1410
1411   return GST_GL_UPLOAD_DONE;
1412 }
1413
1414 static void
1415 _raw_data_upload_free (gpointer impl)
1416 {
1417   struct RawUpload *raw = impl;
1418
1419   if (raw->params)
1420     gst_gl_allocation_params_free ((GstGLAllocationParams *) raw->params);
1421
1422   g_free (raw);
1423 }
1424
1425 static GstStaticCaps _raw_data_upload_caps =
1426 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_GL_MEMORY_VIDEO_FORMATS_STR));
1427
1428 static const UploadMethod _raw_data_upload = {
1429   "Raw Data",
1430   0,
1431   &_raw_data_upload_caps,
1432   &_raw_data_upload_new,
1433   &_raw_data_upload_transform_caps,
1434   &_raw_data_upload_accept,
1435   &_raw_data_upload_propose_allocation,
1436   &_raw_data_upload_perform,
1437   &_raw_data_upload_free
1438 };
1439
1440 #if GST_GL_HAVE_VIV_DIRECTVIV
1441 #ifndef GL_BGRA_EXT
1442 #define GL_BGRA_EXT                                             0x80E1
1443 #endif
1444 #ifndef GL_VIV_YV12
1445 #define GL_VIV_YV12                                             0x8FC0
1446 #endif
1447 #ifndef GL_VIV_NV12
1448 #define GL_VIV_NV12                                             0x8FC1
1449 #endif
1450 #ifndef GL_VIV_YUY2
1451 #define GL_VIV_YUY2                                             0x8FC2
1452 #endif
1453 #ifndef GL_VIV_UYVY
1454 #define GL_VIV_UYVY                                             0x8FC3
1455 #endif
1456 #ifndef GL_VIV_NV21
1457 #define GL_VIV_NV21                                             0x8FC4
1458 #endif
1459 #ifndef GL_VIV_I420
1460 #define GL_VIV_I420                                             0x8FC5
1461 #endif
1462
1463 struct DirectVIVUpload
1464 {
1465   GstGLUpload *upload;
1466
1467   GstGLVideoAllocationParams *params;
1468   GstBuffer *inbuf, *outbuf;
1469   void (*TexDirectVIVMap) (GLenum Target, GLsizei Width, GLsizei Height,
1470       GLenum Format, GLvoid ** Logical, const GLuint * Physical);
1471   void (*TexDirectInvalidateVIV) (GLenum Target);
1472   gboolean loaded_functions;
1473 };
1474
1475 #define GST_GL_DIRECTVIV_FORMAT "{RGBA, I420, YV12, NV12, NV21, YUY2, UYVY, BGRA, RGB16}"
1476
1477 static GstStaticCaps _directviv_upload_caps =
1478 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_GL_DIRECTVIV_FORMAT));
1479
1480 static gpointer
1481 _directviv_upload_new (GstGLUpload * upload)
1482 {
1483   struct DirectVIVUpload *directviv = g_new0 (struct DirectVIVUpload, 1);
1484   directviv->upload = upload;
1485   directviv->loaded_functions = FALSE;
1486
1487   return directviv;
1488 }
1489
1490 static GstCaps *
1491 _directviv_upload_transform_caps (gpointer impl, GstGLContext * context,
1492     GstPadDirection direction, GstCaps * caps)
1493 {
1494   GstCapsFeatures *passthrough =
1495       gst_caps_features_from_string
1496       (GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
1497   GstCaps *ret;
1498
1499   if (direction == GST_PAD_SINK) {
1500     GstCaps *tmp;
1501
1502     ret =
1503         _set_caps_features_with_passthrough (caps,
1504         GST_CAPS_FEATURE_MEMORY_GL_MEMORY, passthrough);
1505
1506     gst_caps_set_simple (ret, "format", G_TYPE_STRING, "RGBA", NULL);
1507     tmp = _caps_intersect_texture_target (ret, 1 << GST_GL_TEXTURE_TARGET_2D);
1508     gst_caps_unref (ret);
1509     ret = tmp;
1510   } else {
1511     ret = gst_caps_from_string (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
1512         (GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY, GST_GL_DIRECTVIV_FORMAT));
1513   }
1514
1515   gst_caps_features_free (passthrough);
1516   return ret;
1517 }
1518
1519
1520 static void
1521 _directviv_upload_load_functions_gl_thread (GstGLContext * context,
1522     struct DirectVIVUpload *directviv)
1523 {
1524   directviv->TexDirectVIVMap =
1525       gst_gl_context_get_proc_address (context, "glTexDirectVIVMap");
1526   directviv->TexDirectInvalidateVIV =
1527       gst_gl_context_get_proc_address (context, "glTexDirectInvalidateVIV");
1528 }
1529
1530 static gboolean
1531 _directviv_upload_accept (gpointer impl, GstBuffer * buffer, GstCaps * in_caps,
1532     GstCaps * out_caps)
1533 {
1534   struct DirectVIVUpload *directviv = impl;
1535   GstCapsFeatures *features;
1536   guint n_mem;
1537   GstMemory *mem;
1538
1539   if (!directviv->loaded_functions && (!directviv->TexDirectInvalidateVIV ||
1540           !directviv->TexDirectVIVMap)) {
1541     gst_gl_context_thread_add (directviv->upload->context,
1542         (GstGLContextThreadFunc) _directviv_upload_load_functions_gl_thread,
1543         directviv);
1544     directviv->loaded_functions = TRUE;
1545   }
1546   if (!directviv->TexDirectInvalidateVIV || !directviv->TexDirectVIVMap)
1547     return FALSE;
1548
1549   features = gst_caps_get_features (out_caps, 0);
1550   if (!gst_caps_features_contains (features, GST_CAPS_FEATURE_MEMORY_GL_MEMORY))
1551     return FALSE;
1552
1553   if (directviv->params)
1554     gst_gl_allocation_params_free ((GstGLAllocationParams *) directviv->params);
1555   if (!(directviv->params =
1556           gst_gl_video_allocation_params_new (directviv->upload->context, NULL,
1557               &directviv->upload->priv->out_info, -1, NULL,
1558               GST_GL_TEXTURE_TARGET_2D, GST_VIDEO_GL_TEXTURE_TYPE_RGBA)))
1559     return FALSE;
1560
1561   /* We only support a single memory per buffer at this point */
1562   n_mem = gst_buffer_n_memory (buffer);
1563   if (n_mem == 1) {
1564     mem = gst_buffer_peek_memory (buffer, 0);
1565   } else {
1566     mem = NULL;
1567   }
1568
1569   return n_mem == 1 && mem && gst_is_phys_memory (mem);
1570 }
1571
1572 static void
1573 _directviv_upload_propose_allocation (gpointer impl, GstQuery * decide_query,
1574     GstQuery * query)
1575 {
1576 }
1577
1578 static GLenum
1579 _directviv_upload_video_format_to_gl_format (GstVideoFormat format)
1580 {
1581   switch (format) {
1582     case GST_VIDEO_FORMAT_I420:
1583       return GL_VIV_I420;
1584     case GST_VIDEO_FORMAT_YV12:
1585       return GL_VIV_YV12;
1586     case GST_VIDEO_FORMAT_NV12:
1587       return GL_VIV_NV12;
1588     case GST_VIDEO_FORMAT_NV21:
1589       return GL_VIV_NV21;
1590     case GST_VIDEO_FORMAT_YUY2:
1591       return GL_VIV_YUY2;
1592     case GST_VIDEO_FORMAT_UYVY:
1593       return GL_VIV_UYVY;
1594     case GST_VIDEO_FORMAT_RGB16:
1595       return GL_RGB565;
1596     case GST_VIDEO_FORMAT_RGBA:
1597       return GL_RGBA;
1598     case GST_VIDEO_FORMAT_BGRA:
1599       return GL_BGRA_EXT;
1600     case GST_VIDEO_FORMAT_RGBx:
1601       return GL_RGBA;
1602     case GST_VIDEO_FORMAT_BGRx:
1603       return GL_BGRA_EXT;
1604     default:
1605       return 0;
1606   }
1607 }
1608
1609 typedef struct
1610 {
1611   GstBuffer *buffer;
1612   GstMemory *memory;
1613   GstMapInfo map;
1614   guintptr phys_addr;
1615 } DirectVIVUnmapData;
1616
1617 static void
1618 _directviv_memory_unmap (DirectVIVUnmapData * data)
1619 {
1620   gst_memory_unmap (data->memory, &data->map);
1621   gst_memory_unref (data->memory);
1622   gst_buffer_unref (data->buffer);
1623   g_free (data);
1624 }
1625
1626 static void
1627 _directviv_upload_perform_gl_thread (GstGLContext * context,
1628     struct DirectVIVUpload *directviv)
1629 {
1630   static GQuark directviv_unmap_quark = 0;
1631   GstGLMemoryAllocator *allocator;
1632   GstMemory *in_mem;
1633   GstGLMemory *out_gl_mem;
1634   GstVideoInfo *in_info;
1635   DirectVIVUnmapData *unmap_data;
1636   GstVideoMeta *vmeta;
1637   gint width, height, gl_format;
1638   const GstGLFuncs *gl;
1639
1640   if (!directviv_unmap_quark)
1641     directviv_unmap_quark = g_quark_from_static_string ("GstGLDirectVIVUnmap");
1642
1643   gl = context->gl_vtable;
1644
1645   g_assert (gst_buffer_n_memory (directviv->inbuf) == 1);
1646   in_info = &directviv->upload->priv->in_info;
1647   in_mem = gst_buffer_peek_memory (directviv->inbuf, 0);
1648   unmap_data = g_new0 (DirectVIVUnmapData, 1);
1649   if (!gst_memory_map (in_mem, &unmap_data->map, GST_MAP_READ)) {
1650     g_free (unmap_data);
1651     return;
1652   }
1653   unmap_data->phys_addr = gst_phys_memory_get_phys_addr (in_mem);
1654   if (!unmap_data->phys_addr) {
1655     gst_memory_unmap (in_mem, &unmap_data->map);
1656     g_free (unmap_data);
1657     return;
1658   }
1659   unmap_data->memory = gst_memory_ref (in_mem);
1660   unmap_data->buffer = gst_buffer_ref (directviv->inbuf);
1661
1662   allocator =
1663       GST_GL_MEMORY_ALLOCATOR (gst_allocator_find
1664       (GST_GL_MEMORY_PBO_ALLOCATOR_NAME));
1665
1666   /* FIXME: buffer pool */
1667   directviv->outbuf = gst_buffer_new ();
1668   gst_gl_memory_setup_buffer (allocator, directviv->outbuf, directviv->params,
1669       NULL, NULL, 0);
1670   gst_object_unref (allocator);
1671
1672   out_gl_mem = (GstGLMemory *) gst_buffer_peek_memory (directviv->outbuf, 0);
1673
1674   /* Need to keep the input memory and buffer mapped and valid until
1675    * the GL memory is not used anymore */
1676   gst_mini_object_set_qdata ((GstMiniObject *) out_gl_mem,
1677       directviv_unmap_quark, unmap_data,
1678       (GDestroyNotify) _directviv_memory_unmap);
1679   gst_buffer_add_parent_buffer_meta (directviv->outbuf, directviv->inbuf);
1680
1681   /* width/height need to compensate for stride/padding */
1682   vmeta = gst_buffer_get_video_meta (directviv->inbuf);
1683   if (vmeta) {
1684     width = vmeta->stride[0];
1685     if (GST_VIDEO_INFO_N_PLANES (in_info) == 1)
1686       height = gst_memory_get_sizes (in_mem, NULL, NULL) / width;
1687     else
1688       height = vmeta->offset[1] / width;
1689   } else {
1690     width = GST_VIDEO_INFO_PLANE_STRIDE (in_info, 0);
1691     if (GST_VIDEO_INFO_N_PLANES (in_info) == 1)
1692       height = gst_memory_get_sizes (in_mem, NULL, NULL) / width;
1693     else
1694       height = GST_VIDEO_INFO_PLANE_OFFSET (in_info, 1) / width;
1695   }
1696   width /= GST_VIDEO_INFO_COMP_PSTRIDE (in_info, 0);
1697
1698   gl_format =
1699       _directviv_upload_video_format_to_gl_format (GST_VIDEO_INFO_FORMAT
1700       (in_info));
1701
1702   gl->BindTexture (GL_TEXTURE_2D, out_gl_mem->tex_id);
1703   directviv->TexDirectVIVMap (GL_TEXTURE_2D, width, height,
1704       gl_format, (void **) &unmap_data->map.data, &unmap_data->phys_addr);
1705   directviv->TexDirectInvalidateVIV (GL_TEXTURE_2D);
1706 }
1707
1708 static GstGLUploadReturn
1709 _directviv_upload_perform (gpointer impl, GstBuffer * buffer,
1710     GstBuffer ** outbuf)
1711 {
1712   struct DirectVIVUpload *directviv = impl;
1713
1714   directviv->inbuf = buffer;
1715   directviv->outbuf = NULL;
1716   gst_gl_context_thread_add (directviv->upload->context,
1717       (GstGLContextThreadFunc) _directviv_upload_perform_gl_thread, directviv);
1718   directviv->inbuf = NULL;
1719
1720   if (!directviv->outbuf)
1721     return GST_GL_UPLOAD_ERROR;
1722
1723   *outbuf = directviv->outbuf;
1724   directviv->outbuf = NULL;
1725
1726   return GST_GL_UPLOAD_DONE;
1727 }
1728
1729 static void
1730 _directviv_upload_free (gpointer impl)
1731 {
1732   struct DirectVIVUpload *directviv = impl;
1733
1734   if (directviv->params)
1735     gst_gl_allocation_params_free ((GstGLAllocationParams *) directviv->params);
1736
1737   g_free (impl);
1738 }
1739
1740 static const UploadMethod _directviv_upload = {
1741   "DirectVIV",
1742   0,
1743   &_directviv_upload_caps,
1744   &_directviv_upload_new,
1745   &_directviv_upload_transform_caps,
1746   &_directviv_upload_accept,
1747   &_directviv_upload_propose_allocation,
1748   &_directviv_upload_perform,
1749   &_directviv_upload_free
1750 };
1751
1752 #endif /* GST_GL_HAVE_VIV_DIRECTVIV */
1753
1754 #if defined(HAVE_NVMM)
1755 #include "nvbuf_utils.h"
1756
1757 struct NVMMUpload
1758 {
1759   GstGLUpload *upload;
1760
1761   GstGLVideoAllocationParams *params;
1762   guint n_mem;
1763
1764   GstGLTextureTarget target;
1765   GstVideoInfo out_info;
1766   /* only used for pointer comparison */
1767   gpointer out_caps;
1768 };
1769
1770 #define GST_CAPS_FEATURE_MEMORY_NVMM "memory:NVMM"
1771
1772 /* FIXME: other formats? */
1773 static GstStaticCaps _nvmm_upload_caps =
1774 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
1775     (GST_CAPS_FEATURE_MEMORY_NVMM,
1776         "RGBA"));
1777
1778 static gpointer
1779 _nvmm_upload_new (GstGLUpload * upload)
1780 {
1781   struct NVMMUpload *nvmm = g_new0 (struct NVMMUpload, 1);
1782   nvmm->upload = upload;
1783   nvmm->target = GST_GL_TEXTURE_TARGET_EXTERNAL_OES;
1784   return nvmm;
1785 }
1786
1787 static GstCaps *
1788 _nvmm_upload_transform_caps (gpointer impl, GstGLContext * context,
1789     GstPadDirection direction, GstCaps * caps)
1790 {
1791   struct NVMMUpload *nvmm = impl;
1792   GstCapsFeatures *passthrough;
1793   GstCaps *ret;
1794
1795   if (context) {
1796     const GstGLFuncs *gl = context->gl_vtable;
1797
1798     if (!gl->EGLImageTargetTexture2D)
1799       return NULL;
1800
1801     /* Don't propose NVMM caps feature unless it can be supported */
1802     if (gst_gl_context_get_gl_platform (context) != GST_GL_PLATFORM_EGL)
1803       return NULL;
1804
1805     if (!gst_gl_context_check_feature (context, "EGL_KHR_image_base"))
1806       return NULL;
1807   }
1808
1809   passthrough = gst_caps_features_from_string
1810       (GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
1811
1812   if (direction == GST_PAD_SINK) {
1813     GstCaps *tmp;
1814
1815     ret =
1816         _set_caps_features_with_passthrough (caps,
1817         GST_CAPS_FEATURE_MEMORY_GL_MEMORY, passthrough);
1818
1819     tmp =
1820         _caps_intersect_texture_target (ret,
1821         1 << GST_GL_TEXTURE_TARGET_EXTERNAL_OES);
1822     gst_caps_unref (ret);
1823     ret = tmp;
1824   } else {
1825     gint i, n;
1826
1827     ret =
1828         _set_caps_features_with_passthrough (caps,
1829         GST_CAPS_FEATURE_MEMORY_NVMM, passthrough);
1830
1831     n = gst_caps_get_size (ret);
1832     for (i = 0; i < n; i++) {
1833       GstStructure *s = gst_caps_get_structure (ret, i);
1834
1835       gst_structure_remove_fields (s, "texture-target", NULL);
1836     }
1837   }
1838
1839   gst_caps_features_free (passthrough);
1840
1841   GST_DEBUG_OBJECT (nvmm->upload, "transformed %" GST_PTR_FORMAT " into %"
1842       GST_PTR_FORMAT, caps, ret);
1843
1844   return ret;
1845 }
1846
1847 static gboolean
1848 _nvmm_upload_accept (gpointer impl, GstBuffer * buffer, GstCaps * in_caps,
1849     GstCaps * out_caps)
1850 {
1851   struct NVMMUpload *nvmm = impl;
1852   GstVideoInfo *in_info = &nvmm->upload->priv->in_info;
1853   GstVideoInfo *out_info = &nvmm->out_info;
1854   GstVideoMeta *meta;
1855   GstMapInfo in_map_info = GST_MAP_INFO_INIT;
1856   guint n_mem;
1857   guint i;
1858
1859   n_mem = gst_buffer_n_memory (buffer);
1860   if (n_mem != 1) {
1861     GST_DEBUG_OBJECT (nvmm->upload, "NVMM uploader only supports "
1862         "1 memory, not %u", n_mem);
1863     return FALSE;
1864   }
1865
1866   meta = gst_buffer_get_video_meta (buffer);
1867
1868   if (!nvmm->upload->context->gl_vtable->EGLImageTargetTexture2D)
1869     return FALSE;
1870
1871   /* NVMM upload is only supported with EGL contexts. */
1872   if (gst_gl_context_get_gl_platform (nvmm->upload->context) !=
1873       GST_GL_PLATFORM_EGL)
1874     return FALSE;
1875
1876   if (!gst_gl_context_check_feature (nvmm->upload->context,
1877           "EGL_KHR_image_base"))
1878     return FALSE;
1879
1880   if (!gst_buffer_map (buffer, &in_map_info, GST_MAP_READ)) {
1881     GST_DEBUG_OBJECT (nvmm->upload, "Failed to map readonly NvBuffer");
1882     return FALSE;
1883   }
1884   if (in_map_info.size != NvBufferGetSize ()) {
1885     GST_DEBUG_OBJECT (nvmm->upload, "Memory size (%" G_GSIZE_FORMAT ") is "
1886         "not the same as what NvBuffer advertises (%u)", in_map_info.size,
1887         NvBufferGetSize ());
1888     gst_buffer_unmap (buffer, &in_map_info);
1889     return FALSE;
1890   }
1891   gst_buffer_unmap (buffer, &in_map_info);
1892
1893   /* Update video info based on video meta */
1894   if (meta) {
1895     in_info->width = meta->width;
1896     in_info->height = meta->height;
1897
1898     for (i = 0; i < meta->n_planes; i++) {
1899       in_info->offset[i] = meta->offset[i];
1900       in_info->stride[i] = meta->stride[i];
1901     }
1902   }
1903
1904   if (out_caps != nvmm->out_caps) {
1905     nvmm->out_caps = out_caps;
1906     if (!gst_video_info_from_caps (out_info, out_caps))
1907       return FALSE;
1908   }
1909
1910   if (nvmm->params)
1911     gst_gl_allocation_params_free ((GstGLAllocationParams *) nvmm->params);
1912   if (!(nvmm->params =
1913           gst_gl_video_allocation_params_new_wrapped_gl_handle (nvmm->
1914               upload->context, NULL, out_info, -1, NULL, nvmm->target, 0, NULL,
1915               NULL, NULL))) {
1916     return FALSE;
1917   }
1918
1919   return TRUE;
1920 }
1921
1922 static void
1923 _nvmm_upload_propose_allocation (gpointer impl, GstQuery * decide_query,
1924     GstQuery * query)
1925 {
1926   /* nothing to do for now. */
1927 }
1928
1929 static void
1930 _egl_image_mem_unref (GstEGLImage * image, GstMemory * mem)
1931 {
1932   GstGLDisplayEGL *egl_display = NULL;
1933   EGLDisplay display;
1934
1935   egl_display = gst_gl_display_egl_from_gl_display (image->context->display);
1936   if (!egl_display) {
1937     GST_ERROR ("Could not retrieve GstGLDisplayEGL from GstGLDisplay");
1938     return;
1939   }
1940   display =
1941       (EGLDisplay) gst_gl_display_get_handle (GST_GL_DISPLAY (egl_display));
1942
1943   if (NvDestroyEGLImage (display, image->image)) {
1944     GST_ERROR ("Failed to destroy EGLImage %p from NvBuffer", image->image);
1945   } else {
1946     GST_DEBUG ("destroyed EGLImage %p from NvBuffer", image->image);
1947   }
1948
1949   gst_memory_unref (mem);
1950   gst_object_unref (egl_display);
1951 }
1952
1953 static const char *
1954 payload_type_to_string (NvBufferPayloadType ptype)
1955 {
1956   switch (ptype) {
1957     case NvBufferPayload_SurfArray:
1958       return "SurfArray";
1959     case NvBufferPayload_MemHandle:
1960       return "MemHandle";
1961     default:
1962       return "<unknown>";
1963   }
1964 }
1965
1966 static const char *
1967 pixel_format_to_string (NvBufferColorFormat fmt)
1968 {
1969   switch (fmt) {
1970     case NvBufferColorFormat_YUV420:
1971       return "YUV420";
1972     case NvBufferColorFormat_YVU420:
1973       return "YVU420";
1974     case NvBufferColorFormat_YUV422:
1975       return "YUV422";
1976     case NvBufferColorFormat_YUV420_ER:
1977       return "YUV420_ER";
1978     case NvBufferColorFormat_YVU420_ER:
1979       return "YVU420_ER";
1980     case NvBufferColorFormat_NV12:
1981       return "NV12";
1982     case NvBufferColorFormat_NV12_ER:
1983       return "NV12_ER";
1984     case NvBufferColorFormat_NV21:
1985       return "NV21";
1986     case NvBufferColorFormat_NV21_ER:
1987       return "NV21_ER";
1988     case NvBufferColorFormat_UYVY:
1989       return "UYVY";
1990     case NvBufferColorFormat_UYVY_ER:
1991       return "UYVY_ER";
1992     case NvBufferColorFormat_VYUY:
1993       return "VYUY";
1994     case NvBufferColorFormat_VYUY_ER:
1995       return "VYUY_ER";
1996     case NvBufferColorFormat_YUYV:
1997       return "YUYV";
1998     case NvBufferColorFormat_YUYV_ER:
1999       return "YUYV_ER";
2000     case NvBufferColorFormat_YVYU:
2001       return "YVYU";
2002     case NvBufferColorFormat_YVYU_ER:
2003       return "YVYU_ER";
2004     case NvBufferColorFormat_ABGR32:
2005       return "ABGR32";
2006     case NvBufferColorFormat_XRGB32:
2007       return "XRGB32";
2008     case NvBufferColorFormat_ARGB32:
2009       return "ARGB32";
2010     case NvBufferColorFormat_NV12_10LE:
2011       return "NV12_10LE";
2012     case NvBufferColorFormat_NV12_10LE_709:
2013       return "NV12_10LE_709";
2014     case NvBufferColorFormat_NV12_10LE_709_ER:
2015       return "NV12_10LE_709_ER";
2016     case NvBufferColorFormat_NV12_10LE_2020:
2017       return "NV12_2020";
2018     case NvBufferColorFormat_NV21_10LE:
2019       return "NV21_10LE";
2020     case NvBufferColorFormat_NV12_12LE:
2021       return "NV12_12LE";
2022     case NvBufferColorFormat_NV12_12LE_2020:
2023       return "NV12_12LE_2020";
2024     case NvBufferColorFormat_NV21_12LE:
2025       return "NV21_12LE";
2026     case NvBufferColorFormat_YUV420_709:
2027       return "YUV420_709";
2028     case NvBufferColorFormat_YUV420_709_ER:
2029       return "YUV420_709_ER";
2030     case NvBufferColorFormat_NV12_709:
2031       return "NV12_709";
2032     case NvBufferColorFormat_NV12_709_ER:
2033       return "NV12_709_ER";
2034     case NvBufferColorFormat_YUV420_2020:
2035       return "YUV420_2020";
2036     case NvBufferColorFormat_NV12_2020:
2037       return "NV12_2020";
2038     case NvBufferColorFormat_SignedR16G16:
2039       return "SignedR16G16";
2040     case NvBufferColorFormat_A32:
2041       return "A32";
2042     case NvBufferColorFormat_YUV444:
2043       return "YUV444";
2044     case NvBufferColorFormat_GRAY8:
2045       return "GRAY8";
2046     case NvBufferColorFormat_NV16:
2047       return "NV16";
2048     case NvBufferColorFormat_NV16_10LE:
2049       return "NV16_10LE";
2050     case NvBufferColorFormat_NV24:
2051       return "NV24";
2052     case NvBufferColorFormat_NV16_ER:
2053       return "NV16_ER";
2054     case NvBufferColorFormat_NV24_ER:
2055       return "NV24_ER";
2056     case NvBufferColorFormat_NV16_709:
2057       return "NV16_709";
2058     case NvBufferColorFormat_NV24_709:
2059       return "NV24_709";
2060     case NvBufferColorFormat_NV16_709_ER:
2061       return "NV16_709_ER";
2062     case NvBufferColorFormat_NV24_709_ER:
2063       return "NV24_709_ER";
2064     case NvBufferColorFormat_NV24_10LE_709:
2065       return "NV24_10LE_709";
2066     case NvBufferColorFormat_NV24_10LE_709_ER:
2067       return "NV24_10LE_709_ER";
2068     case NvBufferColorFormat_NV24_10LE_2020:
2069       return "NV24_10LE_2020";
2070     case NvBufferColorFormat_NV24_12LE_2020:
2071       return "NV24_12LE_2020";
2072     case NvBufferColorFormat_RGBA_10_10_10_2_709:
2073       return "RGBA_10_10_10_2_709";
2074     case NvBufferColorFormat_RGBA_10_10_10_2_2020:
2075       return "RGBA_10_10_10_2_2020";
2076     case NvBufferColorFormat_BGRA_10_10_10_2_709:
2077       return "BGRA_10_10_10_2_709";
2078     case NvBufferColorFormat_BGRA_10_10_10_2_2020:
2079       return "BGRA_10_10_10_2_2020";
2080     case NvBufferColorFormat_Invalid:
2081       return "Invalid";
2082     default:
2083       return "<unknown>";
2084   }
2085 }
2086
2087 static void
2088 dump_nv_buf_params (GstObject * debug_object, NvBufferParamsEx * params)
2089 {
2090   GST_DEBUG_OBJECT (debug_object, "nvbuffer fd: %u size %i nv_buffer: %p of "
2091       "size %u, payload: (0x%x) %s, pixel format: (0x%x) %s, n_planes: %u, "
2092       "plane 0 { wxh: %ux%u, pitch: %u, offset: %u, psize: %u, layout: %u } "
2093       "plane 1 { wxh: %ux%u, pitch: %u, offset: %u, psize: %u, layout: %u } "
2094       "plane 2 { wxh: %ux%u, pitch: %u, offset: %u, psize: %u, layout: %u }",
2095       params->params.dmabuf_fd, params->params.memsize,
2096       params->params.nv_buffer, params->params.nv_buffer_size,
2097       params->params.payloadType,
2098       payload_type_to_string (params->params.payloadType),
2099       params->params.pixel_format,
2100       pixel_format_to_string (params->params.pixel_format),
2101       params->params.num_planes, params->params.width[0],
2102       params->params.height[0], params->params.pitch[0],
2103       params->params.offset[0], params->params.psize[0],
2104       params->params.offset[0], params->params.width[1],
2105       params->params.height[1], params->params.pitch[1],
2106       params->params.offset[1], params->params.psize[1],
2107       params->params.offset[1], params->params.width[2],
2108       params->params.height[2], params->params.pitch[2],
2109       params->params.offset[2], params->params.psize[2],
2110       params->params.offset[2]);
2111 }
2112
2113 static GstGLUploadReturn
2114 _nvmm_upload_perform (gpointer impl, GstBuffer * buffer, GstBuffer ** outbuf)
2115 {
2116   struct NVMMUpload *nvmm = impl;
2117   GstGLMemoryAllocator *allocator = NULL;
2118   GstMapInfo in_map_info = GST_MAP_INFO_INIT;
2119   GstGLDisplayEGL *egl_display = NULL;
2120   GstEGLImage *eglimage = NULL;
2121   EGLDisplay display = EGL_NO_DISPLAY;
2122   EGLImageKHR image = EGL_NO_IMAGE;
2123   int in_dmabuf_fd;
2124   NvBufferParamsEx params = { 0, };
2125   GstGLUploadReturn ret = GST_GL_UPLOAD_ERROR;
2126
2127   if (!gst_buffer_map (buffer, &in_map_info, GST_MAP_READ)) {
2128     GST_DEBUG_OBJECT (nvmm->upload, "Failed to map readonly NvBuffer");
2129     goto done;
2130   }
2131
2132   if (ExtractFdFromNvBuffer (in_map_info.data, &in_dmabuf_fd)) {
2133     GST_DEBUG_OBJECT (nvmm->upload, "Failed to extract fd from NvBuffer");
2134     goto done;
2135   }
2136   if (NvBufferGetParamsEx (in_dmabuf_fd, &params)) {
2137     GST_WARNING_OBJECT (nvmm->upload, "Failed to get NvBuffer params");
2138     goto done;
2139   }
2140   dump_nv_buf_params ((GstObject *) nvmm->upload, &params);
2141
2142   egl_display =
2143       gst_gl_display_egl_from_gl_display (nvmm->upload->context->display);
2144   if (!egl_display) {
2145     GST_WARNING ("Failed to retrieve GstGLDisplayEGL from GstGLDisplay");
2146     goto done;
2147   }
2148   display =
2149       (EGLDisplay) gst_gl_display_get_handle (GST_GL_DISPLAY (egl_display));
2150
2151   image = NvEGLImageFromFd (display, in_dmabuf_fd);
2152   if (!image) {
2153     GST_DEBUG_OBJECT (nvmm->upload, "Failed construct EGLImage "
2154         "from NvBuffer fd %i", in_dmabuf_fd);
2155     goto done;
2156   }
2157   GST_DEBUG_OBJECT (nvmm->upload, "constructed EGLImage %p "
2158       "from NvBuffer fd %i", image, in_dmabuf_fd);
2159
2160   eglimage = gst_egl_image_new_wrapped (nvmm->upload->context, image,
2161       GST_GL_RGBA, gst_memory_ref (in_map_info.memory),
2162       (GstEGLImageDestroyNotify) _egl_image_mem_unref);
2163   if (!eglimage) {
2164     GST_WARNING_OBJECT (nvmm->upload, "Failed to wrap constructed "
2165         "EGLImage from NvBuffer");
2166     goto done;
2167   }
2168
2169   gst_buffer_unmap (buffer, &in_map_info);
2170   in_map_info = (GstMapInfo) GST_MAP_INFO_INIT;
2171
2172   allocator =
2173       GST_GL_MEMORY_ALLOCATOR (gst_allocator_find
2174       (GST_GL_MEMORY_EGL_ALLOCATOR_NAME));
2175
2176   /* TODO: buffer pool */
2177   *outbuf = gst_buffer_new ();
2178   if (!gst_gl_memory_setup_buffer (allocator, *outbuf, nvmm->params,
2179           NULL, (gpointer *) & eglimage, 1)) {
2180     GST_WARNING_OBJECT (nvmm->upload, "Failed to setup "
2181         "NVMM -> EGLImage buffer");
2182     goto done;
2183   }
2184   gst_egl_image_unref (eglimage);
2185
2186   gst_buffer_add_parent_buffer_meta (*outbuf, buffer);
2187
2188   /* TODO: NvBuffer has some sync functions that may be more useful here */
2189   {
2190     GstGLSyncMeta *sync_meta;
2191
2192     sync_meta = gst_buffer_add_gl_sync_meta (nvmm->upload->context, *outbuf);
2193     if (sync_meta) {
2194       gst_gl_sync_meta_set_sync_point (sync_meta, nvmm->upload->context);
2195     }
2196   }
2197
2198   ret = GST_GL_UPLOAD_DONE;
2199
2200 done:
2201   if (in_map_info.memory)
2202     gst_buffer_unmap (buffer, &in_map_info);
2203
2204   gst_clear_object (&egl_display);
2205   gst_clear_object (&allocator);
2206
2207   return ret;
2208 }
2209
2210 static void
2211 _nvmm_upload_free (gpointer impl)
2212 {
2213   struct NVMMUpload *nvmm = impl;
2214
2215   if (nvmm->params)
2216     gst_gl_allocation_params_free ((GstGLAllocationParams *) nvmm->params);
2217
2218   g_free (impl);
2219 }
2220
2221 static const UploadMethod _nvmm_upload = {
2222   "NVMM",
2223   0,
2224   &_nvmm_upload_caps,
2225   &_nvmm_upload_new,
2226   &_nvmm_upload_transform_caps,
2227   &_nvmm_upload_accept,
2228   &_nvmm_upload_propose_allocation,
2229   &_nvmm_upload_perform,
2230   &_nvmm_upload_free
2231 };
2232
2233 #endif /* HAVE_NVMM */
2234
2235 static const UploadMethod *upload_methods[] = { &_gl_memory_upload,
2236 #if GST_GL_HAVE_DMABUF
2237   &_direct_dma_buf_upload,
2238   &_direct_dma_buf_external_upload,
2239   &_dma_buf_upload,
2240 #endif
2241 #if GST_GL_HAVE_VIV_DIRECTVIV
2242   &_directviv_upload,
2243 #endif
2244 #if defined(HAVE_NVMM)
2245   &_nvmm_upload,
2246 #endif /* HAVE_NVMM */
2247   &_upload_meta_upload,
2248   /* Raw data must always be last / least preferred */
2249   &_raw_data_upload
2250 };
2251
2252 static GMutex upload_global_lock;
2253
2254 GstCaps *
2255 gst_gl_upload_get_input_template_caps (void)
2256 {
2257   GstCaps *ret = NULL;
2258   gint i;
2259
2260   g_mutex_lock (&upload_global_lock);
2261
2262   /* FIXME: cache this and invalidate on changes to upload_methods */
2263   for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
2264     GstCaps *template =
2265         gst_static_caps_get (upload_methods[i]->input_template_caps);
2266     ret = ret == NULL ? template : gst_caps_merge (ret, template);
2267   }
2268
2269   ret = gst_caps_simplify (ret);
2270   ret = gst_gl_overlay_compositor_add_caps (ret);
2271   g_mutex_unlock (&upload_global_lock);
2272
2273   return ret;
2274 }
2275
2276 static void
2277 gst_gl_upload_class_init (GstGLUploadClass * klass)
2278 {
2279   G_OBJECT_CLASS (klass)->finalize = gst_gl_upload_finalize;
2280 }
2281
2282 static void
2283 gst_gl_upload_init (GstGLUpload * upload)
2284 {
2285   upload->priv = gst_gl_upload_get_instance_private (upload);
2286 }
2287
2288 /**
2289  * gst_gl_upload_new:
2290  * @context: a #GstGLContext
2291  *
2292  * Returns: (transfer full): a new #GstGLUpload object
2293  */
2294 GstGLUpload *
2295 gst_gl_upload_new (GstGLContext * context)
2296 {
2297   GstGLUpload *upload = g_object_new (GST_TYPE_GL_UPLOAD, NULL);
2298   gint i, n;
2299
2300   gst_object_ref_sink (upload);
2301
2302   if (context)
2303     gst_gl_upload_set_context (upload, context);
2304   else
2305     upload->context = NULL;
2306
2307   n = G_N_ELEMENTS (upload_methods);
2308   upload->priv->upload_impl = g_malloc (sizeof (gpointer) * n);
2309   for (i = 0; i < n; i++) {
2310     upload->priv->upload_impl[i] = upload_methods[i]->new (upload);
2311   }
2312
2313   GST_DEBUG_OBJECT (upload, "Created new GLUpload for context %" GST_PTR_FORMAT,
2314       context);
2315
2316   return upload;
2317 }
2318
2319 void
2320 gst_gl_upload_set_context (GstGLUpload * upload, GstGLContext * context)
2321 {
2322   g_return_if_fail (upload != NULL);
2323
2324   gst_object_replace ((GstObject **) & upload->context, (GstObject *) context);
2325 }
2326
2327 static void
2328 gst_gl_upload_finalize (GObject * object)
2329 {
2330   GstGLUpload *upload;
2331   gint i, n;
2332
2333   upload = GST_GL_UPLOAD (object);
2334
2335   upload->priv->method_i = 0;
2336
2337   if (upload->context) {
2338     gst_object_unref (upload->context);
2339     upload->context = NULL;
2340   }
2341
2342   if (upload->priv->in_caps) {
2343     gst_caps_unref (upload->priv->in_caps);
2344     upload->priv->in_caps = NULL;
2345   }
2346
2347   if (upload->priv->out_caps) {
2348     gst_caps_unref (upload->priv->out_caps);
2349     upload->priv->out_caps = NULL;
2350   }
2351
2352   n = G_N_ELEMENTS (upload_methods);
2353   for (i = 0; i < n; i++) {
2354     if (upload->priv->upload_impl[i])
2355       upload_methods[i]->free (upload->priv->upload_impl[i]);
2356   }
2357   g_free (upload->priv->upload_impl);
2358
2359   G_OBJECT_CLASS (gst_gl_upload_parent_class)->finalize (object);
2360 }
2361
2362 GstCaps *
2363 gst_gl_upload_transform_caps (GstGLUpload * upload, GstGLContext * context,
2364     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
2365 {
2366   GstCaps *result, *tmp;
2367   gint i;
2368
2369   if (upload->priv->method) {
2370     tmp = upload->priv->method->transform_caps (upload->priv->method_impl,
2371         context, direction, caps);
2372
2373     if (tmp) {
2374       /* If we're generating sink pad caps, make sure to include raw caps if needed by
2375        * the current method */
2376       if (direction == GST_PAD_SRC
2377           && (upload->priv->method->flags & METHOD_FLAG_CAN_ACCEPT_RAW)) {
2378         GstCapsFeatures *passthrough =
2379             gst_caps_features_from_string
2380             (GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
2381         GstCaps *raw_tmp = _set_caps_features_with_passthrough (tmp,
2382             GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY, passthrough);
2383         gst_caps_append (tmp, raw_tmp);
2384         gst_caps_features_free (passthrough);
2385       }
2386
2387       if (filter) {
2388         result =
2389             gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
2390         gst_caps_unref (tmp);
2391       } else {
2392         result = tmp;
2393       }
2394       if (!gst_caps_is_empty (result))
2395         return result;
2396       else
2397         gst_caps_unref (result);
2398     }
2399   }
2400
2401   tmp = gst_caps_new_empty ();
2402
2403   for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
2404     GstCaps *tmp2;
2405
2406     tmp2 =
2407         upload_methods[i]->transform_caps (upload->priv->upload_impl[i],
2408         context, direction, caps);
2409
2410     if (tmp2)
2411       tmp = gst_caps_merge (tmp, tmp2);
2412   }
2413
2414   if (filter) {
2415     result = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
2416     gst_caps_unref (tmp);
2417   } else {
2418     result = tmp;
2419   }
2420
2421   return result;
2422 }
2423
2424 /**
2425  * gst_gl_upload_propose_allocation:
2426  * @upload: a #GstGLUpload
2427  * @decide_query: (allow-none): a #GstQuery from a decide allocation
2428  * @query: the proposed allocation query
2429  *
2430  * Adds the required allocation parameters to support uploading.
2431  */
2432 void
2433 gst_gl_upload_propose_allocation (GstGLUpload * upload, GstQuery * decide_query,
2434     GstQuery * query)
2435 {
2436   gint i;
2437
2438   for (i = 0; i < G_N_ELEMENTS (upload_methods); i++)
2439     upload_methods[i]->propose_allocation (upload->priv->upload_impl[i],
2440         decide_query, query);
2441 }
2442
2443 static gboolean
2444 _gst_gl_upload_set_caps_unlocked (GstGLUpload * upload, GstCaps * in_caps,
2445     GstCaps * out_caps)
2446 {
2447   g_return_val_if_fail (upload != NULL, FALSE);
2448   g_return_val_if_fail (gst_caps_is_fixed (in_caps), FALSE);
2449
2450   if (upload->priv->in_caps && upload->priv->out_caps
2451       && gst_caps_is_equal (upload->priv->in_caps, in_caps)
2452       && gst_caps_is_equal (upload->priv->out_caps, out_caps))
2453     return TRUE;
2454
2455   gst_caps_replace (&upload->priv->in_caps, in_caps);
2456   gst_caps_replace (&upload->priv->out_caps, out_caps);
2457
2458   gst_video_info_from_caps (&upload->priv->in_info, in_caps);
2459   gst_video_info_from_caps (&upload->priv->out_info, out_caps);
2460
2461   upload->priv->method = NULL;
2462   upload->priv->method_impl = NULL;
2463   upload->priv->method_i = 0;
2464
2465   return TRUE;
2466 }
2467
2468 /**
2469  * gst_gl_upload_set_caps:
2470  * @upload: a #GstGLUpload
2471  * @in_caps: input #GstCaps
2472  * @out_caps: output #GstCaps
2473  *
2474  * Initializes @upload with the information required for upload.
2475  *
2476  * Returns: whether @in_caps and @out_caps could be set on @upload
2477  */
2478 gboolean
2479 gst_gl_upload_set_caps (GstGLUpload * upload, GstCaps * in_caps,
2480     GstCaps * out_caps)
2481 {
2482   gboolean ret;
2483
2484   GST_OBJECT_LOCK (upload);
2485   ret = _gst_gl_upload_set_caps_unlocked (upload, in_caps, out_caps);
2486   GST_OBJECT_UNLOCK (upload);
2487
2488   return ret;
2489 }
2490
2491 /**
2492  * gst_gl_upload_get_caps:
2493  * @upload: a #GstGLUpload
2494  * @in_caps: (transfer full) (allow-none) (out): the input #GstCaps
2495  * @out_caps: (transfer full) (allow-none) (out): the output #GstCaps
2496  */
2497 void
2498 gst_gl_upload_get_caps (GstGLUpload * upload, GstCaps ** in_caps,
2499     GstCaps ** out_caps)
2500 {
2501   GST_OBJECT_LOCK (upload);
2502   if (in_caps)
2503     *in_caps =
2504         upload->priv->in_caps ? gst_caps_ref (upload->priv->in_caps) : NULL;
2505   if (out_caps)
2506     *out_caps =
2507         upload->priv->out_caps ? gst_caps_ref (upload->priv->out_caps) : NULL;
2508   GST_OBJECT_UNLOCK (upload);
2509 }
2510
2511 static gboolean
2512 _upload_find_method (GstGLUpload * upload, gpointer last_impl)
2513 {
2514   gint method_i;
2515
2516   /* start with the last used method after explicitly reconfiguring to
2517    * negotiate caps for this method */
2518   if (upload->priv->method_i == 0) {
2519     upload->priv->method_i = upload->priv->saved_method_i;
2520     upload->priv->saved_method_i = 0;
2521   }
2522
2523   if (upload->priv->method_i >= G_N_ELEMENTS (upload_methods)) {
2524     if (last_impl)
2525       upload->priv->method_i = 0;
2526     else
2527       return FALSE;
2528   }
2529
2530   method_i = upload->priv->method_i;
2531
2532   if (last_impl == upload->priv->upload_impl[method_i])
2533     return FALSE;
2534
2535   upload->priv->method = upload_methods[method_i];
2536   upload->priv->method_impl = upload->priv->upload_impl[method_i];
2537
2538   GST_DEBUG_OBJECT (upload, "attempting upload with uploader %s",
2539       upload->priv->method->name);
2540
2541   upload->priv->method_i++;
2542
2543   return TRUE;
2544 }
2545
2546 /**
2547  * gst_gl_upload_perform_with_buffer:
2548  * @upload: a #GstGLUpload
2549  * @buffer: input #GstBuffer
2550  * @outbuf_ptr: (out): resulting #GstBuffer
2551  *
2552  * Uploads @buffer using the transformation specified by
2553  * gst_gl_upload_set_caps() creating a new #GstBuffer in @outbuf_ptr.
2554  *
2555  * Returns: whether the upload was successful
2556  */
2557 GstGLUploadReturn
2558 gst_gl_upload_perform_with_buffer (GstGLUpload * upload, GstBuffer * buffer,
2559     GstBuffer ** outbuf_ptr)
2560 {
2561   GstGLUploadReturn ret = GST_GL_UPLOAD_ERROR;
2562   GstBuffer *outbuf = NULL;
2563   gpointer last_impl = upload->priv->method_impl;
2564 #if !defined (GST_DISABLE_DEBUG)
2565   const UploadMethod *last_method = upload->priv->method;
2566 #endif
2567
2568   g_return_val_if_fail (GST_IS_GL_UPLOAD (upload), FALSE);
2569   g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
2570   g_return_val_if_fail (outbuf_ptr != NULL, FALSE);
2571
2572   GST_OBJECT_LOCK (upload);
2573
2574 #define NEXT_METHOD \
2575 do { \
2576   if (!_upload_find_method (upload, last_impl)) { \
2577     GST_OBJECT_UNLOCK (upload); \
2578     return FALSE; \
2579   } \
2580   goto restart; \
2581 } while (0)
2582
2583   if (!upload->priv->method_impl)
2584     _upload_find_method (upload, last_impl);
2585
2586 restart:
2587   if (!upload->priv->method->accept (upload->priv->method_impl, buffer,
2588           upload->priv->in_caps, upload->priv->out_caps))
2589     NEXT_METHOD;
2590
2591   ret =
2592       upload->priv->method->perform (upload->priv->method_impl, buffer,
2593       &outbuf);
2594   GST_LOG_OBJECT (upload, "uploader %s returned %u, buffer: %p",
2595       upload->priv->method->name, ret, outbuf);
2596   if (ret == GST_GL_UPLOAD_UNSHARED_GL_CONTEXT) {
2597     gint i;
2598
2599     for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
2600       if (upload_methods[i] == &_raw_data_upload) {
2601         upload->priv->method = &_raw_data_upload;
2602         upload->priv->method_impl = upload->priv->upload_impl[i];
2603         upload->priv->method_i = i;
2604
2605         break;
2606       }
2607     }
2608
2609     gst_buffer_replace (&outbuf, NULL);
2610     goto restart;
2611   } else if (ret == GST_GL_UPLOAD_DONE || ret == GST_GL_UPLOAD_RECONFIGURE) {
2612     if (last_impl != upload->priv->method_impl
2613         && upload->priv->method_impl != NULL) {
2614       /* Transform the input caps using the new method. If they are compatible with the
2615        * existing upload method, we can skip reconfiguration */
2616       GstCaps *caps =
2617           upload->priv->method->transform_caps (upload->priv->method_impl,
2618           upload->context, GST_PAD_SINK, upload->priv->in_caps);
2619
2620       GST_LOG_OBJECT (upload,
2621           "Changing uploader from %s to %s with src caps %" GST_PTR_FORMAT
2622           " and old src caps %" GST_PTR_FORMAT,
2623           last_method != NULL ? last_method->name : "None",
2624           upload->priv->method->name, caps, upload->priv->out_caps);
2625
2626       if (caps == NULL || !gst_caps_is_subset (caps, upload->priv->out_caps)) {
2627         gst_buffer_replace (&outbuf, NULL);
2628         ret = GST_GL_UPLOAD_RECONFIGURE;
2629       }
2630       gst_caps_replace (&caps, NULL);
2631     }
2632     /* we are done */
2633   } else {
2634     upload->priv->method_impl = NULL;
2635     gst_buffer_replace (&outbuf, NULL);
2636     NEXT_METHOD;
2637   }
2638
2639   if (outbuf && buffer != outbuf)
2640     gst_buffer_copy_into (outbuf, buffer,
2641         GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
2642   *outbuf_ptr = outbuf;
2643
2644   if (ret == GST_GL_UPLOAD_RECONFIGURE)
2645     upload->priv->saved_method_i = upload->priv->method_i - 1;
2646
2647   GST_OBJECT_UNLOCK (upload);
2648
2649   return ret;
2650
2651 #undef NEXT_METHOD
2652 }