v4l2: bufferpool: reset buffer size in release_buffer
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2bufferpool.c
index 65d1a5c..f885156 100644 (file)
@@ -18,8 +18,8 @@
  *
  * You should have received a copy of the GNU Library General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
 #include <sys/mman.h>
 #include <string.h>
 #include <unistd.h>
+#if HAVE_DECL_V4L2_MEMORY_DMABUF
+#include <fcntl.h>
+#endif
 
 #include "gst/video/video.h"
 #include "gst/video/gstvideometa.h"
 #include "gst/video/gstvideopool.h"
+#include "gst/allocators/gstdmabuf.h"
 
 #include <gstv4l2bufferpool.h>
 
-#include "gstv4l2src.h"
-#include "gstv4l2sink.h"
 #include "v4l2_calls.h"
 #include "gst/gst-i18n-plugin.h"
 #include <gst/glib-compat-private.h>
@@ -75,11 +77,12 @@ gst_v4l2_meta_get_info (void)
 {
   static const GstMetaInfo *meta_info = NULL;
 
-  if (meta_info == NULL) {
-    meta_info =
+  if (g_once_init_enter (&meta_info)) {
+    const GstMetaInfo *meta =
         gst_meta_register (gst_v4l2_meta_api_get_type (), "GstV4l2Meta",
         sizeof (GstV4l2Meta), (GstMetaInitFunction) NULL,
         (GstMetaFreeFunction) NULL, (GstMetaTransformFunction) NULL);
+    g_once_init_leave (&meta_info, meta);
   }
   return meta_info;
 }
@@ -103,6 +106,7 @@ gst_v4l2_buffer_pool_free_buffer (GstBufferPool * bpool, GstBuffer * buffer)
 
   switch (obj->mode) {
     case GST_V4L2_IO_RW:
+    case GST_V4L2_IO_DMABUF:
       break;
     case GST_V4L2_IO_MMAP:
     {
@@ -114,7 +118,7 @@ gst_v4l2_buffer_pool_free_buffer (GstBufferPool * bpool, GstBuffer * buffer)
 
       index = meta->vbuffer.index;
       GST_LOG_OBJECT (pool,
-          "mmap buffer %p idx %d (data %p, len %u) freed, unmapping", buffer,
+          "unmap buffer %p idx %d (data %p, len %u)", buffer,
           index, meta->mem, meta->vbuffer.length);
 
       v4l2_munmap (meta->mem, meta->vbuffer.length);
@@ -151,6 +155,7 @@ gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
       break;
     }
     case GST_V4L2_IO_MMAP:
+    case GST_V4L2_IO_DMABUF:
     {
       newbuf = gst_buffer_new ();
       meta = GST_V4L2_META_ADD (newbuf);
@@ -174,20 +179,35 @@ gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
       GST_LOG_OBJECT (pool, "  memory:    %d", meta->vbuffer.memory);
       if (meta->vbuffer.memory == V4L2_MEMORY_MMAP)
         GST_LOG_OBJECT (pool, "  MMAP offset:  %u", meta->vbuffer.m.offset);
-      GST_LOG_OBJECT (pool, "  length:    %u", meta->vbuffer.length);
-      GST_LOG_OBJECT (pool, "  input:     %u", meta->vbuffer.input);
-
-      meta->mem = v4l2_mmap (0, meta->vbuffer.length,
-          PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
-          meta->vbuffer.m.offset);
-      if (meta->mem == MAP_FAILED)
-        goto mmap_failed;
-
-      gst_buffer_append_memory (newbuf,
-          gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE,
-              meta->mem, meta->vbuffer.length, 0, meta->vbuffer.length, NULL,
-              NULL));
 
+      if (obj->mode == GST_V4L2_IO_MMAP) {
+        meta->mem = v4l2_mmap (0, meta->vbuffer.length,
+            PROT_READ | PROT_WRITE, MAP_SHARED, pool->video_fd,
+            meta->vbuffer.m.offset);
+        if (meta->mem == MAP_FAILED)
+          goto mmap_failed;
+
+        gst_buffer_append_memory (newbuf,
+            gst_memory_new_wrapped (GST_MEMORY_FLAG_NO_SHARE,
+                meta->mem, meta->vbuffer.length, 0, meta->vbuffer.length, NULL,
+                NULL));
+      }
+#if HAVE_DECL_V4L2_MEMORY_DMABUF
+      if (obj->mode == GST_V4L2_IO_DMABUF) {
+        struct v4l2_exportbuffer expbuf;
+
+        expbuf.type = meta->vbuffer.type;
+        expbuf.index = meta->vbuffer.index;
+        expbuf.flags = O_CLOEXEC;
+        if (v4l2_ioctl (pool->video_fd, VIDIOC_EXPBUF, &expbuf) < 0)
+          goto mmap_failed;
+
+        meta->vbuffer.memory = V4L2_MEMORY_DMABUF;
+        gst_buffer_append_memory (newbuf,
+            gst_dmabuf_allocator_alloc (pool->allocator, expbuf.fd,
+                meta->vbuffer.length));
+      }
+#endif
       /* add metadata to raw video buffers */
       if (pool->add_videometa && info->finfo) {
         const GstVideoFormatInfo *finfo = info->finfo;
@@ -218,8 +238,8 @@ gst_v4l2_buffer_pool_alloc_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
     }
     case GST_V4L2_IO_USERPTR:
     default:
+      newbuf = NULL;
       g_assert_not_reached ();
-      break;
   }
 
   pool->num_allocated++;
@@ -266,10 +286,12 @@ gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
       gst_buffer_pool_config_has_option (config,
       GST_BUFFER_POOL_OPTION_VIDEO_META);
 
-  if (!pool->add_videometa) {
+  if (!pool->add_videometa &&
+      GST_VIDEO_INFO_FORMAT (&obj->info) != GST_VIDEO_FORMAT_ENCODED) {
     gint stride;
 
-    /* we don't have video metadata, see if the strides are compatible */
+    /* we don't have video metadata, and we are not dealing with raw video,
+     * see if the strides are compatible */
     stride = GST_VIDEO_INFO_PLANE_STRIDE (&obj->info, 0);
 
     GST_DEBUG_OBJECT (pool, "no videometadata, checking strides %d and %u",
@@ -296,6 +318,7 @@ gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
       num_buffers = 1;
       copy_threshold = 0;
       break;
+    case GST_V4L2_IO_DMABUF:
     case GST_V4L2_IO_MMAP:
     {
       /* request a reasonable number of buffers when no max specified. We will
@@ -305,17 +328,6 @@ gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
       else
         num_buffers = max_buffers;
 
-      /* Free the buffers from the kernel */
-      GST_DEBUG_OBJECT (pool, "Freeing the buffers from the kernel");
-
-      memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
-      breq.type = obj->type;
-      breq.count = 0;
-      breq.memory = V4L2_MEMORY_MMAP;
-
-      if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0)
-        goto reqbufs_failed;
-
       /* first, lets request buffers, and see how many we can get: */
       GST_DEBUG_OBJECT (pool, "starting, requesting %d MMAP buffers",
           num_buffers);
@@ -366,10 +378,14 @@ gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
   pool->size = size;
   pool->num_buffers = num_buffers;
   pool->copy_threshold = copy_threshold;
+
+  if (obj->mode == GST_V4L2_IO_DMABUF)
+    allocator = gst_dmabuf_allocator_obtain ();
+
   if (pool->allocator)
-    gst_allocator_unref (pool->allocator);
+    gst_object_unref (pool->allocator);
   if ((pool->allocator = allocator))
-    gst_allocator_ref (allocator);
+    gst_object_ref (allocator);
   pool->params = params;
 
   gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
@@ -415,6 +431,7 @@ start_streaming (GstV4l2BufferPool * pool)
       break;
     case GST_V4L2_IO_MMAP:
     case GST_V4L2_IO_USERPTR:
+    case GST_V4L2_IO_DMABUF:
       GST_DEBUG_OBJECT (pool, "STREAMON");
       if (v4l2_ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0)
         goto start_failed;
@@ -487,6 +504,7 @@ gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
         break;
       case GST_V4L2_IO_MMAP:
       case GST_V4L2_IO_USERPTR:
+      case GST_V4L2_IO_DMABUF:
         /* we actually need to sync on all queued buffers but not
          * on the non-queued ones */
         GST_DEBUG_OBJECT (pool, "STREAMOFF");
@@ -504,9 +522,11 @@ gst_v4l2_buffer_pool_stop (GstBufferPool * bpool)
   ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool);
 
   /* then free the remaining buffers */
-  for (n = 0; n < pool->num_queued; n++) {
-    gst_v4l2_buffer_pool_free_buffer (bpool, pool->buffers[n]);
+  for (n = 0; n < pool->num_buffers; n++) {
+    if (pool->buffers[n])
+      gst_v4l2_buffer_pool_free_buffer (bpool, pool->buffers[n]);
   }
+  pool->num_queued = 0;
   g_free (pool->buffers);
   pool->buffers = NULL;
 
@@ -622,7 +642,12 @@ gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
 
   memset (&vbuffer, 0x00, sizeof (vbuffer));
   vbuffer.type = obj->type;
-  vbuffer.memory = V4L2_MEMORY_MMAP;
+#if HAVE_DECL_V4L2_MEMORY_DMABUF
+  if (obj->mode == GST_V4L2_IO_DMABUF)
+    vbuffer.memory = V4L2_MEMORY_DMABUF;
+  else
+#endif
+    vbuffer.memory = V4L2_MEMORY_MMAP;
 
   GST_LOG_OBJECT (pool, "doing DQBUF");
   if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &vbuffer) < 0)
@@ -658,8 +683,6 @@ gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
   /* this can change at every frame, esp. with jpeg */
   if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
     gst_buffer_resize (outbuf, 0, vbuffer.bytesused);
-  else
-    gst_buffer_resize (outbuf, 0, vbuffer.length);
 
   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
 
@@ -751,7 +774,7 @@ gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
           ret = GST_BUFFER_POOL_CLASS (parent_class)->acquire_buffer (bpool,
               buffer, params);
           break;
-
+        case GST_V4L2_IO_DMABUF:
         case GST_V4L2_IO_MMAP:
           /* just dequeue a buffer, we basically use the queue of v4l2 as the
            * storage for our buffers. This function does poll first so we can
@@ -776,6 +799,7 @@ gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
 
         case GST_V4L2_IO_USERPTR:
         default:
+          ret = GST_FLOW_ERROR;
           g_assert_not_reached ();
           break;
       }
@@ -798,12 +822,14 @@ gst_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer,
 
         case GST_V4L2_IO_USERPTR:
         default:
+          ret = GST_FLOW_ERROR;
           g_assert_not_reached ();
           break;
       }
       break;
 
     default:
+      ret = GST_FLOW_ERROR;
       g_assert_not_reached ();
       break;
   }
@@ -836,6 +862,7 @@ gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
           GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool, buffer);
           break;
 
+        case GST_V4L2_IO_DMABUF:
         case GST_V4L2_IO_MMAP:
           /* queue back in the device */
           gst_v4l2_buffer_pool_qbuf (pool, buffer);
@@ -858,12 +885,20 @@ gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
         case GST_V4L2_IO_MMAP:
         {
           GstV4l2Meta *meta;
+          guint index;
 
           meta = GST_V4L2_META_GET (buffer);
           g_assert (meta != NULL);
 
-          if (pool->buffers[meta->vbuffer.index] == NULL) {
-            GST_LOG_OBJECT (pool, "buffer not queued, putting on free list");
+          index = meta->vbuffer.index;
+
+          if (pool->buffers[index] == NULL) {
+            GST_LOG_OBJECT (pool, "buffer %u not queued, putting on free list",
+                index);
+
+            /* reset to the full length, in case it was changed */
+            gst_buffer_resize (buffer, 0, meta->vbuffer.length);
+
             /* playback, put the buffer back in the queue to refill later. */
             GST_BUFFER_POOL_CLASS (parent_class)->release_buffer (bpool,
                 buffer);
@@ -871,7 +906,7 @@ gst_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer)
             /* the buffer is queued in the device but maybe not played yet. We just
              * leave it there and not make it available for future calls to acquire
              * for now. The buffer will be dequeued and reused later. */
-            GST_LOG_OBJECT (pool, "buffer is queued");
+            GST_LOG_OBJECT (pool, "buffer %u is queued", index);
           }
           break;
         }
@@ -897,7 +932,7 @@ gst_v4l2_buffer_pool_finalize (GObject * object)
   if (pool->video_fd >= 0)
     v4l2_close (pool->video_fd);
   if (pool->allocator)
-    gst_allocator_unref (pool->allocator);
+    gst_object_unref (pool->allocator);
   g_free (pool->buffers);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
@@ -1088,7 +1123,7 @@ gst_v4l2_buffer_pool_process (GstV4l2BufferPool * pool, GstBuffer * buf)
           /* FIXME, do write() */
           GST_WARNING_OBJECT (pool, "implement write()");
           break;
-
+        case GST_V4L2_IO_DMABUF:
         case GST_V4L2_IO_MMAP:
         {
           GstBuffer *to_queue;