[v4l2videodecoder] Support TBM for output buffer
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2bufferpool.c
index 49f222b..6a3f5ea 100644 (file)
@@ -45,6 +45,9 @@
 #include "gstv4l2object.h"
 #include "gst/gst-i18n-plugin.h"
 #include <gst/glib-compat-private.h>
+#ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
+#include <gst/allocators/gsttizenmemory.h>
+#endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
 
 GST_DEBUG_CATEGORY_STATIC (v4l2bufferpool_debug);
 GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
@@ -52,7 +55,6 @@ GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
 
 #define GST_V4L2_IMPORT_QUARK gst_v4l2_buffer_pool_import_quark ()
 
-
 /*
  * GstV4l2BufferPool:
  */
@@ -68,6 +70,72 @@ enum _GstV4l2BufferPoolAcquireFlags
 
 static void gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool,
     GstBuffer * buffer);
+#ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
+typedef struct _GstV4l2TizenBuffer GstV4l2TizenBuffer;
+struct _GstV4l2TizenBuffer {
+  int index;
+  GstBuffer *gst_buffer;
+  GstBuffer *v4l2_buffer;
+  GstV4l2BufferPool *v4l2_pool;
+};
+
+static void gst_v4l2_tizen_buffer_finalize (GstV4l2TizenBuffer *tizen_buffer)
+{
+  GstV4l2BufferPool *pool = NULL;
+
+  if (!tizen_buffer) {
+    GST_ERROR ("NULL buffer");
+    return;
+  }
+
+  pool = tizen_buffer->v4l2_pool;
+
+  gst_v4l2_buffer_pool_release_buffer (GST_BUFFER_POOL_CAST (pool), tizen_buffer->v4l2_buffer);
+
+  g_mutex_lock (&pool->buffer_lock);
+
+  pool->live_buffer_count--;
+
+  GST_DEBUG_OBJECT (pool, "release buffer[%d][tizen:%p,v4l2:%p,gst:%p], live[%d]",
+      tizen_buffer->index, tizen_buffer, tizen_buffer->v4l2_buffer,
+      tizen_buffer->gst_buffer, pool->live_buffer_count);
+
+  g_cond_signal (&pool->buffer_cond);
+
+  g_mutex_unlock (&pool->buffer_lock);
+}
+
+static GstV4l2TizenBuffer *gst_v4l2_tizen_buffer_new (GstBuffer *v4l2_buffer, int index, GstV4l2BufferPool *v4l2_pool)
+{
+  GstV4l2TizenBuffer *tizen_buffer = NULL;
+  GstMemory *memory = NULL;
+
+  tizen_buffer = g_new0 (GstV4l2TizenBuffer, 1);
+  tizen_buffer->index = index;
+  tizen_buffer->v4l2_buffer = v4l2_buffer;
+  tizen_buffer->gst_buffer = gst_buffer_new ();
+  tizen_buffer->v4l2_pool = v4l2_pool;
+
+  memory = gst_tizen_allocator_alloc_surface (v4l2_pool->tallocator,
+      &v4l2_pool->obj->info, v4l2_pool->vallocator->groups[index]->surface, (gpointer)tizen_buffer,
+      (GDestroyNotify)gst_v4l2_tizen_buffer_finalize);
+
+  gst_buffer_append_memory (tizen_buffer->gst_buffer, memory);
+  gst_buffer_set_size (tizen_buffer->gst_buffer, v4l2_pool->vallocator->s_info.size);
+
+  g_mutex_lock (&v4l2_pool->buffer_lock);
+
+  v4l2_pool->live_buffer_count++;
+
+  GST_DEBUG_OBJECT (v4l2_pool, "new buffer[tizen:%p,v4l2:%p,gst:%p], size[%d], live[%d]",
+      tizen_buffer, v4l2_buffer, tizen_buffer->gst_buffer,
+      v4l2_pool->vallocator->s_info.size, v4l2_pool->live_buffer_count);
+
+  g_mutex_unlock (&v4l2_pool->buffer_lock);
+
+  return tizen_buffer;
+}
+#endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
 
 static gboolean
 gst_v4l2_is_buffer_valid (GstBuffer * buffer, GstV4l2MemoryGroup ** out_group)
@@ -649,6 +717,7 @@ gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
     case GST_V4L2_IO_USERPTR:
     case GST_V4L2_IO_DMABUF:
     case GST_V4L2_IO_DMABUF_IMPORT:
+#ifndef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
       if (!V4L2_TYPE_IS_OUTPUT (pool->obj->type)) {
         guint i;
 
@@ -659,6 +728,7 @@ gst_v4l2_buffer_pool_streamon (GstV4l2BufferPool * pool)
         for (i = 0; i < pool->num_allocated; i++)
           gst_v4l2_buffer_pool_resurrect_buffer (pool);
       }
+#endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
 
       if (obj->ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
         goto streamon_failed;
@@ -688,10 +758,38 @@ gst_v4l2_buffer_pool_streamoff (GstV4l2BufferPool * pool)
   GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class);
   GstV4l2Object *obj = pool->obj;
   gint i;
+#ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
+  gint64 end_time = 0;
+#endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
 
   if (!pool->streaming)
     return;
 
+#ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
+  if (!V4L2_TYPE_IS_OUTPUT(pool->obj->type)) {
+    g_mutex_lock (&pool->buffer_lock);
+
+    GST_INFO_OBJECT (pool, "live buffer[%d]", pool->live_buffer_count);
+
+    if (pool->live_buffer_count > 0) {
+      end_time = g_get_monotonic_time () + G_TIME_SPAN_SECOND;
+
+      do {
+        GST_WARNING_OBJECT (pool, "wait for live buffer[%d]", pool->live_buffer_count);
+
+        if (!g_cond_wait_until (&pool->buffer_cond, &pool->buffer_lock, end_time)) {
+          GST_ERROR_OBJECT (pool, "failed to wait live buffer[%d]", pool->live_buffer_count);
+          break;
+        }
+
+        GST_WARNING_OBJECT (pool, "signal received, check again : live count[%d]",
+            pool->live_buffer_count);
+      } while (pool->live_buffer_count > 0);
+    }
+
+    g_mutex_unlock (&pool->buffer_lock);
+  }
+#endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
   switch (obj->mode) {
     case GST_V4L2_IO_MMAP:
     case GST_V4L2_IO_USERPTR:
@@ -1215,6 +1313,9 @@ gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer,
   GstVideoMeta *vmeta;
   gsize size;
   gint i;
+#ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
+  GstV4l2TizenBuffer *tizen_buffer = NULL;
+#endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
 
   if ((res = gst_v4l2_buffer_pool_poll (pool, wait)) < GST_FLOW_OK)
     goto poll_failed;
@@ -1347,6 +1448,16 @@ gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer,
   GST_BUFFER_OFFSET (outbuf) = group->buffer.sequence;
   GST_BUFFER_OFFSET_END (outbuf) = group->buffer.sequence + 1;
 
+#ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
+  if (group->surface) {
+    tizen_buffer = gst_v4l2_tizen_buffer_new (outbuf, group->buffer.index, pool);
+    if (!tizen_buffer) {
+      GST_ERROR_OBJECT (pool, "tizen buffer failed for index[%d]", group->buffer.index);
+      goto no_buffer;
+    }
+    outbuf = tizen_buffer->gst_buffer;
+  }
+#endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
 done:
   *buffer = outbuf;
 
@@ -1585,6 +1696,14 @@ gst_v4l2_buffer_pool_dispose (GObject * object)
     gst_object_unref (pool->allocator);
   pool->allocator = NULL;
 
+#ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
+  g_cond_clear (&pool->buffer_cond);
+  g_mutex_clear (&pool->buffer_lock);
+
+  if (pool->tallocator)
+    gst_object_unref (pool->tallocator);
+  pool->tallocator = NULL;
+#endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
   if (pool->other_pool)
     gst_object_unref (pool->other_pool);
   pool->other_pool = NULL;
@@ -1690,6 +1809,14 @@ gst_v4l2_buffer_pool_new (GstV4l2Object * obj, GstCaps * caps)
   pool->obj = obj;
   pool->can_poll_device = TRUE;
 
+#ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
+  pool->tallocator = gst_tizen_allocator_new ();
+  if (pool->tallocator == NULL)
+    goto allocator_failed;
+
+  g_mutex_init (&pool->buffer_lock);
+  g_cond_init (&pool->buffer_cond);
+#endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
   pool->vallocator = gst_v4l2_allocator_new (GST_OBJECT (pool), obj);
   if (pool->vallocator == NULL)
     goto allocator_failed;
@@ -1712,6 +1839,12 @@ dup_failed:
   }
 allocator_failed:
   {
+#ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
+    if (pool->tallocator) {
+      gst_object_unref (pool->tallocator);
+      pool->tallocator = NULL;
+    }
+#endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
     GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator");
     gst_object_unref (pool);
     return NULL;
@@ -1881,11 +2014,19 @@ gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer ** buf)
             if (GST_V4L2_IS_M2M (obj->device_caps))
               goto eos;
           }
-
+#ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
+          if (pool->obj->mode == GST_V4L2_IO_DMABUF) {
+            gst_buffer_unref (*buf);
+            *buf = tmp;
+          } else {
+#endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
           ret = gst_v4l2_buffer_pool_copy_buffer (pool, *buf, tmp);
 
           /* an queue the buffer again after the copy */
           gst_v4l2_buffer_pool_release_buffer (bpool, tmp);
+#ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
+          }
+#endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
 
           if (ret != GST_FLOW_OK)
             goto copy_failed;