v4l2: bufferpool: reset buffer size in release_buffer
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2bufferpool.c
index 038ce51..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,40 +179,67 @@ 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_take_memory (newbuf, -1,
-          gst_memory_new_wrapped (0,
-              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;
         gsize offset[GST_VIDEO_MAX_PLANES];
-        gint stride[GST_VIDEO_MAX_PLANES];
+        gint width, height, n_planes, offs, i, stride[GST_VIDEO_MAX_PLANES];
+
+        width = GST_VIDEO_INFO_WIDTH (info);
+        height = GST_VIDEO_INFO_HEIGHT (info);
+        n_planes = GST_VIDEO_INFO_N_PLANES (info);
 
-        offset[0] = 0;
-        stride[0] = obj->bytesperline;
+        GST_DEBUG_OBJECT (pool, "adding video meta, bytesperline %d",
+            obj->bytesperline);
 
-        GST_DEBUG_OBJECT (pool, "adding video meta, stride %d", stride[0]);
-        gst_buffer_add_video_meta_full (newbuf, info->flags,
-            GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
-            GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
+        offs = 0;
+        for (i = 0; i < n_planes; i++) {
+          offset[i] = offs;
+          stride[i] =
+              GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, obj->bytesperline);
+
+          offs +=
+              stride[i] * GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, height);
+        }
+        gst_buffer_add_video_meta_full (newbuf, GST_VIDEO_FRAME_FLAG_NONE,
+            GST_VIDEO_INFO_FORMAT (info), width, height, n_planes,
             offset, stride);
       }
       break;
     }
     case GST_V4L2_IO_USERPTR:
     default:
+      newbuf = NULL;
       g_assert_not_reached ();
-      break;
   }
 
   pool->num_allocated++;
@@ -243,9 +275,10 @@ gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
   GstV4l2Object *obj = pool->obj;
   GstCaps *caps;
-  guint size, min_buffers, max_buffers;
+  guint size, min_buffers, max_buffers, num_buffers, copy_threshold;
   GstAllocator *allocator;
   GstAllocationParams params;
+  struct v4l2_requestbuffers breq;
 
   GST_DEBUG_OBJECT (pool, "set config");
 
@@ -253,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",
@@ -276,8 +311,81 @@ gst_v4l2_buffer_pool_set_config (GstBufferPool * bpool, GstStructure * config)
 
   GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config);
 
+  switch (obj->mode) {
+    case GST_V4L2_IO_RW:
+      /* we preallocate 1 buffer, this value also instructs the latency
+       * calculation to have 1 frame latency max */
+      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
+       * copy when we run out of buffers */
+      if (max_buffers == 0)
+        num_buffers = 4;
+      else
+        num_buffers = max_buffers;
+
+      /* first, lets request buffers, and see how many we can get: */
+      GST_DEBUG_OBJECT (pool, "starting, requesting %d MMAP buffers",
+          num_buffers);
+
+      memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
+      breq.type = obj->type;
+      breq.count = num_buffers;
+      breq.memory = V4L2_MEMORY_MMAP;
+
+      if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0)
+        goto reqbufs_failed;
+
+      GST_LOG_OBJECT (pool, " count:  %u", breq.count);
+      GST_LOG_OBJECT (pool, " type:   %d", breq.type);
+      GST_LOG_OBJECT (pool, " memory: %d", breq.memory);
+
+      if (breq.count < GST_V4L2_MIN_BUFFERS)
+        goto no_buffers;
+
+      if (num_buffers != breq.count) {
+        GST_WARNING_OBJECT (pool, "using %u buffers instead", breq.count);
+        num_buffers = breq.count;
+      }
+      /* update min buffers with the amount of buffers we just reserved. We need
+       * to configure this value in the bufferpool so that the default start
+       * implementation calls our allocate function */
+      min_buffers = breq.count;
+
+      if (max_buffers == 0 || num_buffers < max_buffers) {
+        /* if we are asked to provide more buffers than we have allocated, start
+         * copying buffers when we only have 2 buffers left in the pool */
+        copy_threshold = 2;
+      } else {
+        /* we are certain that we have enough buffers so we don't need to
+         * copy */
+        copy_threshold = 0;
+      }
+      break;
+    }
+    case GST_V4L2_IO_USERPTR:
+    default:
+      num_buffers = 0;
+      copy_threshold = 0;
+      g_assert_not_reached ();
+      break;
+  }
+
   pool->size = size;
-  pool->max_buffers = max_buffers;
+  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_object_unref (pool->allocator);
+  if ((pool->allocator = allocator))
+    gst_object_ref (allocator);
   pool->params = params;
 
   gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
@@ -298,6 +406,19 @@ wrong_config:
     GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config);
     return FALSE;
   }
+reqbufs_failed:
+  {
+    GST_ERROR_OBJECT (pool,
+        "error requesting %d buffers: %s", num_buffers, g_strerror (errno));
+    return FALSE;
+  }
+no_buffers:
+  {
+    GST_ERROR_OBJECT (pool,
+        "we received %d from device '%s', we want at least %d",
+        breq.count, obj->videodev, GST_V4L2_MIN_BUFFERS);
+    return FALSE;
+  }
 }
 
 static gboolean
@@ -310,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;
@@ -337,55 +459,9 @@ gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
 {
   GstV4l2BufferPool *pool = GST_V4L2_BUFFER_POOL (bpool);
   GstV4l2Object *obj = pool->obj;
-  struct v4l2_requestbuffers breq;
-  gint max_buffers;
-
-  max_buffers = pool->max_buffers;
-
-  switch (obj->mode) {
-    case GST_V4L2_IO_RW:
-    {
-      break;
-    }
-    case GST_V4L2_IO_MMAP:
-    {
-      /* first, lets request buffers, and see how many we can get: */
-      GST_DEBUG_OBJECT (pool, "starting, requesting %d MMAP buffers",
-          max_buffers);
-
-      if (max_buffers == 0)
-        max_buffers = 4;
-
-      memset (&breq, 0, sizeof (struct v4l2_requestbuffers));
-      breq.type = obj->type;
-      breq.count = max_buffers;
-      breq.memory = V4L2_MEMORY_MMAP;
-
-      if (v4l2_ioctl (pool->video_fd, VIDIOC_REQBUFS, &breq) < 0)
-        goto reqbufs_failed;
-
-      GST_LOG_OBJECT (pool, " count:  %u", breq.count);
-      GST_LOG_OBJECT (pool, " type:   %d", breq.type);
-      GST_LOG_OBJECT (pool, " memory: %d", breq.memory);
-
-      if (breq.count < GST_V4L2_MIN_BUFFERS)
-        goto no_buffers;
-
-      if (max_buffers != breq.count) {
-        GST_WARNING_OBJECT (pool, "using %u buffers instead", breq.count);
-        max_buffers = breq.count;
-      }
-      break;
-    }
-    case GST_V4L2_IO_USERPTR:
-    default:
-      g_assert_not_reached ();
-      break;
-  }
 
   pool->obj = obj;
-  pool->max_buffers = max_buffers;
-  pool->buffers = g_new0 (GstBuffer *, max_buffers);
+  pool->buffers = g_new0 (GstBuffer *, pool->num_buffers);
   pool->num_allocated = 0;
 
   /* now, allocate the buffers: */
@@ -403,19 +479,6 @@ gst_v4l2_buffer_pool_start (GstBufferPool * bpool)
   return TRUE;
 
   /* ERRORS */
-reqbufs_failed:
-  {
-    GST_ERROR_OBJECT (pool,
-        "error requesting %d buffers: %s", max_buffers, g_strerror (errno));
-    return FALSE;
-  }
-no_buffers:
-  {
-    GST_ERROR_OBJECT (pool,
-        "we received %d from device '%s', we want at least %d",
-        breq.count, obj->videodev, GST_V4L2_MIN_BUFFERS);
-    return FALSE;
-  }
 start_failed:
   {
     GST_ERROR_OBJECT (pool, "failed to start streaming");
@@ -441,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");
@@ -458,10 +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_allocated; 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;
 
@@ -520,16 +585,22 @@ gst_v4l2_buffer_pool_qbuf (GstV4l2BufferPool * pool, GstBuffer * buf)
   gint index;
 
   meta = GST_V4L2_META_GET (buf);
-  g_assert (meta != NULL);
+  if (meta == NULL) {
+    GST_LOG_OBJECT (pool, "unref copied buffer %p", buf);
+    /* no meta, it was a copied buffer that we can unref */
+    gst_buffer_unref (buf);
+    return GST_FLOW_OK;
+  }
 
   index = meta->vbuffer.index;
 
-  GST_LOG_OBJECT (pool, "enqueue buffer %p, index:%d, queued:%d", buf,
-      index, pool->num_queued);
+  GST_LOG_OBJECT (pool, "enqueue buffer %p, index:%d, queued:%d, flags:%08x",
+      buf, index, pool->num_queued, meta->vbuffer.flags);
 
   if (pool->buffers[index] != NULL)
     goto already_queued;
 
+  GST_LOG_OBJECT (pool, "doing QBUF");
   if (v4l2_ioctl (pool->video_fd, VIDIOC_QBUF, &meta->vbuffer) < 0)
     goto queue_failed;
 
@@ -559,6 +630,7 @@ gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
   GstBuffer *outbuf;
   struct v4l2_buffer vbuffer;
   GstV4l2Object *obj = pool->obj;
+  GstClockTime timestamp;
 
   if (obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
     /* select works for input devices when data is available. According to the
@@ -570,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)
@@ -587,26 +664,27 @@ gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer)
   pool->buffers[vbuffer.index] = NULL;
   pool->num_queued--;
 
+  timestamp = GST_TIMEVAL_TO_TIME (vbuffer.timestamp);
+
   GST_LOG_OBJECT (pool,
-      "dequeued buffer %p seq:%d (ix=%d), used %d, flags %08x, pool-queued=%d, buffer=%p",
-      outbuf, vbuffer.sequence, vbuffer.index, vbuffer.bytesused, vbuffer.flags,
-      pool->num_queued, outbuf);
+      "dequeued buffer %p seq:%d (ix=%d), used %d, flags %08x, ts %"
+      GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf, vbuffer.sequence,
+      vbuffer.index, vbuffer.bytesused, vbuffer.flags,
+      GST_TIME_ARGS (timestamp), pool->num_queued, outbuf);
 
   /* set top/bottom field first if v4l2_buffer has the information */
   if (vbuffer.field == V4L2_FIELD_INTERLACED_TB) {
     GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
-    GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
   }
   if (vbuffer.field == V4L2_FIELD_INTERLACED_BT) {
     GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF);
-    GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
   }
 
   /* 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;
 
   *buffer = outbuf;
 
@@ -696,16 +774,32 @@ 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
            * interrupt it fine. */
           ret = gst_v4l2_buffer_pool_dqbuf (pool, buffer);
+          if (G_UNLIKELY (ret != GST_FLOW_OK))
+            goto done;
+
+          /* start copying buffers when we are running low on buffers */
+          if (pool->num_queued < pool->copy_threshold) {
+            GstBuffer *copy;
+
+            /* copy the memory */
+            copy = gst_buffer_copy (*buffer);
+            GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buffer, copy);
+
+            /* and requeue so that we can continue capturing */
+            ret = gst_v4l2_buffer_pool_qbuf (pool, *buffer);
+            *buffer = copy;
+          }
           break;
 
         case GST_V4L2_IO_USERPTR:
         default:
+          ret = GST_FLOW_ERROR;
           g_assert_not_reached ();
           break;
       }
@@ -728,15 +822,18 @@ 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;
   }
+done:
   return ret;
 
   /* ERRORS */
@@ -765,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);
@@ -787,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);
@@ -800,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;
         }
@@ -825,7 +931,8 @@ gst_v4l2_buffer_pool_finalize (GObject * object)
 
   if (pool->video_fd >= 0)
     v4l2_close (pool->video_fd);
-
+  if (pool->allocator)
+    gst_object_unref (pool->allocator);
   g_free (pool->buffers);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
@@ -1016,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;