2 * Copyright (C) 2014 Collabora Ltd.
3 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
25 # define _GNU_SOURCE /* O_CLOEXEC */
28 #include "ext/videodev2.h"
29 #include "gstv4l2allocator.h"
30 #include "v4l2_calls.h"
32 #include <gst/allocators/gstdmabuf.h>
37 #include <sys/types.h>
40 #define GST_V4L2_MEMORY_TYPE "V4l2Memory"
42 #define gst_v4l2_allocator_parent_class parent_class
43 G_DEFINE_TYPE (GstV4l2Allocator, gst_v4l2_allocator, GST_TYPE_ALLOCATOR);
45 GST_DEBUG_CATEGORY_STATIC (v4l2allocator_debug);
46 #define GST_CAT_DEFAULT v4l2allocator_debug
48 #define UNSET_QUEUED(buffer) \
49 ((buffer).flags &= ~(V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
51 #define SET_QUEUED(buffer) ((buffer).flags |= V4L2_BUF_FLAG_QUEUED)
53 #define IS_QUEUED(buffer) \
54 ((buffer).flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
62 static guint gst_v4l2_allocator_signals[LAST_SIGNAL] = { 0 };
64 static void gst_v4l2_allocator_release (GstV4l2Allocator * allocator,
68 memory_type_to_str (guint32 memory)
71 case V4L2_MEMORY_MMAP:
73 case V4L2_MEMORY_USERPTR:
75 case V4L2_MEMORY_DMABUF:
82 /*************************************/
83 /* GstV4lMemory implementation */
84 /*************************************/
87 _v4l2mem_map (GstV4l2Memory * mem, gsize maxsize, GstMapFlags flags)
91 switch (mem->group->buffer.memory) {
92 case V4L2_MEMORY_MMAP:
93 case V4L2_MEMORY_USERPTR:
96 case V4L2_MEMORY_DMABUF:
97 /* v4l2 dmabuf memory are not shared with downstream */
98 g_assert_not_reached ();
101 GST_WARNING ("Unknown memory type %i", mem->group->buffer.memory);
108 _v4l2mem_unmap (GstV4l2Memory * mem)
110 gboolean ret = FALSE;
112 switch (mem->group->buffer.memory) {
113 case V4L2_MEMORY_MMAP:
114 case V4L2_MEMORY_USERPTR:
117 case V4L2_MEMORY_DMABUF:
118 /* v4l2 dmabuf memory are not share with downstream */
119 g_assert_not_reached ();
122 GST_WARNING ("Unknown memory type %i", mem->group->buffer.memory);
129 _v4l2mem_dispose (GstV4l2Memory * mem)
131 GstV4l2Allocator *allocator = (GstV4l2Allocator *) mem->mem.allocator;
132 GstV4l2MemoryGroup *group = mem->group;
135 if (group->mem[mem->plane]) {
136 /* We may have a dmabuf, replace it with returned original memory */
137 group->mem[mem->plane] = gst_memory_ref ((GstMemory *) mem);
138 gst_v4l2_allocator_release (allocator, mem);
141 gst_object_ref (allocator);
149 _v4l2mem_free (GstV4l2Memory * mem)
153 g_slice_free (GstV4l2Memory, mem);
156 static inline GstV4l2Memory *
157 _v4l2mem_new (GstMemoryFlags flags, GstAllocator * allocator,
158 GstMemory * parent, gsize maxsize, gsize align, gsize offset, gsize size,
159 gint plane, gpointer data, int dmafd, GstV4l2MemoryGroup * group)
163 mem = g_slice_new0 (GstV4l2Memory);
164 gst_memory_init (GST_MEMORY_CAST (mem),
165 flags, allocator, parent, maxsize, align, offset, size);
168 mem->mem.mini_object.dispose =
169 (GstMiniObjectDisposeFunction) _v4l2mem_dispose;
179 static GstV4l2Memory *
180 _v4l2mem_share (GstV4l2Memory * mem, gssize offset, gsize size)
185 /* find the real parent */
186 if ((parent = mem->mem.parent) == NULL)
187 parent = (GstMemory *) mem;
190 size = mem->mem.size - offset;
192 /* the shared memory is always readonly */
193 sub = _v4l2mem_new (GST_MINI_OBJECT_FLAGS (parent) |
194 GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->mem.allocator, parent,
195 mem->mem.maxsize, mem->mem.align, offset, size, mem->plane, mem->data,
202 _v4l2mem_is_span (GstV4l2Memory * mem1, GstV4l2Memory * mem2, gsize * offset)
205 *offset = mem1->mem.offset - mem1->mem.parent->offset;
207 /* and memory is contiguous */
208 return mem1->mem.offset + mem1->mem.size == mem2->mem.offset;
212 gst_is_v4l2_memory (GstMemory * mem)
214 return gst_memory_is_type (mem, GST_V4L2_MEMORY_TYPE);
218 gst_v4l2_memory_quark (void)
220 static GQuark quark = 0;
223 quark = g_quark_from_string ("GstV4l2Memory");
229 /*************************************/
230 /* GstV4l2MemoryGroup implementation */
231 /*************************************/
234 gst_v4l2_memory_group_free (GstV4l2MemoryGroup * group)
238 for (i = 0; i < group->n_mem; i++) {
239 GstMemory *mem = group->mem[i];
240 group->mem[i] = NULL;
242 gst_memory_unref (mem);
245 g_slice_free (GstV4l2MemoryGroup, group);
248 static GstV4l2MemoryGroup *
249 gst_v4l2_memory_group_new (GstV4l2Allocator * allocator, guint32 index)
251 gint video_fd = allocator->video_fd;
252 guint32 memory = allocator->memory;
253 struct v4l2_format *format = &allocator->format;
254 GstV4l2MemoryGroup *group;
255 gsize img_size, buf_size;
257 group = g_slice_new0 (GstV4l2MemoryGroup);
259 group->buffer.type = format->type;
260 group->buffer.index = index;
261 group->buffer.memory = memory;
263 if (V4L2_TYPE_IS_MULTIPLANAR (format->type)) {
264 group->n_mem = group->buffer.length = format->fmt.pix_mp.num_planes;
265 group->buffer.m.planes = group->planes;
270 if (v4l2_ioctl (video_fd, VIDIOC_QUERYBUF, &group->buffer) < 0)
271 goto querybuf_failed;
273 if (group->buffer.index != index) {
274 GST_ERROR_OBJECT (allocator, "Buffer index returned by VIDIOC_QUERYBUF "
275 "didn't match, this indicate the presence of a bug in your driver or "
277 g_slice_free (GstV4l2MemoryGroup, group);
281 /* Check that provided size matches the format we have negotiation. Failing
282 * there usually means a driver of libv4l bug. */
283 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
286 for (i = 0; i < group->n_mem; i++) {
287 img_size = allocator->format.fmt.pix_mp.plane_fmt[i].sizeimage;
288 buf_size = group->planes[i].length;
289 if (buf_size < img_size)
290 goto buffer_too_short;
293 img_size = allocator->format.fmt.pix.sizeimage;
294 buf_size = group->buffer.length;
295 if (buf_size < img_size)
296 goto buffer_too_short;
299 /* We save non planar buffer information into the multi-planar plane array
300 * to avoid duplicating the code later */
301 if (!V4L2_TYPE_IS_MULTIPLANAR (format->type)) {
302 group->planes[0].bytesused = group->buffer.bytesused;
303 group->planes[0].length = group->buffer.length;
304 g_assert (sizeof (group->planes[0].m) == sizeof (group->buffer.m));
305 memcpy (&group->planes[0].m, &group->buffer.m, sizeof (group->buffer.m));
308 GST_LOG_OBJECT (allocator, "Got %s buffer", memory_type_to_str (memory));
309 GST_LOG_OBJECT (allocator, " index: %u", group->buffer.index);
310 GST_LOG_OBJECT (allocator, " type: %d", group->buffer.type);
311 GST_LOG_OBJECT (allocator, " flags: %08x", group->buffer.flags);
312 GST_LOG_OBJECT (allocator, " field: %d", group->buffer.field);
313 GST_LOG_OBJECT (allocator, " memory: %d", group->buffer.memory);
314 GST_LOG_OBJECT (allocator, " planes: %d", group->n_mem);
316 #ifndef GST_DISABLE_GST_DEBUG
317 if (memory == V4L2_MEMORY_MMAP) {
319 for (i = 0; i < group->n_mem; i++) {
320 GST_LOG_OBJECT (allocator, " [%u] bytesused: %u, length: %u", i,
321 group->planes[i].bytesused, group->planes[i].length);
322 GST_LOG_OBJECT (allocator, " [%u] MMAP offset: %u", i,
323 group->planes[i].m.mem_offset);
332 GST_ERROR ("error querying buffer %d: %s", index, g_strerror (errno));
337 GST_ERROR ("buffer size %" G_GSIZE_FORMAT
338 " is smaller then negotiated size %" G_GSIZE_FORMAT
339 ", this is usually the result of a bug in the v4l2 driver or libv4l.",
344 gst_v4l2_memory_group_free (group);
349 /*************************************/
350 /* GstV4lAllocator implementation */
351 /*************************************/
354 gst_v4l2_allocator_release (GstV4l2Allocator * allocator, GstV4l2Memory * mem)
356 GstV4l2MemoryGroup *group = mem->group;
358 GST_LOG_OBJECT (allocator, "plane %i of buffer %u released",
359 mem->plane, group->buffer.index);
361 switch (allocator->memory) {
362 case V4L2_MEMORY_DMABUF:
366 case V4L2_MEMORY_USERPTR:
373 /* When all memory are back, put the group back in the free queue */
374 if (g_atomic_int_dec_and_test (&group->mems_allocated)) {
375 GST_LOG_OBJECT (allocator, "buffer %u released", group->buffer.index);
376 gst_atomic_queue_push (allocator->free_queue, group);
377 g_signal_emit (allocator, gst_v4l2_allocator_signals[GROUP_RELEASED], 0);
380 /* Keep last, allocator may be freed after this call */
381 g_object_unref (allocator);
385 gst_v4l2_allocator_free (GstAllocator * gallocator, GstMemory * gmem)
387 GstV4l2Allocator *allocator = (GstV4l2Allocator *) gallocator;
388 GstV4l2Memory *mem = (GstV4l2Memory *) gmem;
389 GstV4l2MemoryGroup *group = mem->group;
391 GST_LOG_OBJECT (allocator, "freeing plane %i of buffer %u",
392 mem->plane, group->buffer.index);
394 switch (allocator->memory) {
395 case V4L2_MEMORY_MMAP:
397 v4l2_munmap (mem->data, group->planes[mem->plane].length);
398 } else if (group->planes[mem->plane].m.fd > 0) {
399 close (group->planes[mem->plane].m.fd);
411 gst_v4l2_allocator_dispose (GObject * obj)
413 GstV4l2Allocator *allocator = (GstV4l2Allocator *) obj;
416 GST_LOG_OBJECT (obj, "called");
418 for (i = 0; i < allocator->count; i++) {
419 GstV4l2MemoryGroup *group = allocator->groups[i];
420 allocator->groups[i] = NULL;
422 gst_v4l2_memory_group_free (group);
425 G_OBJECT_CLASS (parent_class)->dispose (obj);
429 gst_v4l2_allocator_finalize (GObject * obj)
431 GstV4l2Allocator *allocator = (GstV4l2Allocator *) obj;
433 GST_LOG_OBJECT (obj, "called");
435 v4l2_close (allocator->video_fd);
436 gst_atomic_queue_unref (allocator->free_queue);
438 G_OBJECT_CLASS (parent_class)->finalize (obj);
442 gst_v4l2_allocator_class_init (GstV4l2AllocatorClass * klass)
444 GObjectClass *object_class;
445 GstAllocatorClass *allocator_class;
447 allocator_class = (GstAllocatorClass *) klass;
448 object_class = (GObjectClass *) klass;
450 allocator_class->alloc = NULL;
451 allocator_class->free = gst_v4l2_allocator_free;
453 object_class->dispose = gst_v4l2_allocator_dispose;
454 object_class->finalize = gst_v4l2_allocator_finalize;
456 gst_v4l2_allocator_signals[GROUP_RELEASED] = g_signal_new ("group-released",
457 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
460 GST_DEBUG_CATEGORY_INIT (v4l2allocator_debug, "v4l2allocator", 0,
465 gst_v4l2_allocator_init (GstV4l2Allocator * allocator)
467 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
469 alloc->mem_type = GST_V4L2_MEMORY_TYPE;
470 alloc->mem_map = (GstMemoryMapFunction) _v4l2mem_map;
471 alloc->mem_unmap = (GstMemoryUnmapFunction) _v4l2mem_unmap;
472 alloc->mem_share = (GstMemoryShareFunction) _v4l2mem_share;
473 alloc->mem_is_span = (GstMemoryIsSpanFunction) _v4l2mem_is_span;
474 /* Use the default, fallback copy function */
476 allocator->free_queue = gst_atomic_queue_new (VIDEO_MAX_FRAME);
478 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
481 #define GST_V4L2_ALLOCATOR_PROBE(obj,type) \
482 gst_v4l2_allocator_probe ((obj), V4L2_MEMORY_ ## type, \
483 GST_V4L2_ALLOCATOR_FLAG_ ## type ## _REQBUFS, \
484 GST_V4L2_ALLOCATOR_FLAG_ ## type ## _CREATE_BUFS)
486 gst_v4l2_allocator_probe (GstV4l2Allocator * allocator, guint32 memory,
487 guint32 breq_flag, guint32 bcreate_flag)
489 struct v4l2_requestbuffers breq = { 0 };
492 breq.type = allocator->type;
494 breq.memory = memory;
496 if (v4l2_ioctl (allocator->video_fd, VIDIOC_REQBUFS, &breq) == 0) {
497 struct v4l2_create_buffers bcreate = { 0 };
501 bcreate.memory = allocator->type;
502 bcreate.format = allocator->format;
504 if ((v4l2_ioctl (allocator->video_fd, VIDIOC_CREATE_BUFS, &bcreate) == 0))
505 flags |= bcreate_flag;
511 static GstV4l2MemoryGroup *
512 gst_v4l2_allocator_create_buf (GstV4l2Allocator * allocator)
514 struct v4l2_create_buffers bcreate = { 0 };
515 GstV4l2MemoryGroup *group = NULL;
517 GST_OBJECT_LOCK (allocator);
519 if (!g_atomic_int_get (&allocator->active))
522 bcreate.memory = allocator->memory;
523 bcreate.format = allocator->format;
526 if (!allocator->can_allocate)
529 if (v4l2_ioctl (allocator->video_fd, VIDIOC_CREATE_BUFS, &bcreate) < 0)
530 goto create_bufs_failed;
532 if (allocator->groups[bcreate.index] != NULL)
533 goto create_bufs_bug;
535 group = gst_v4l2_memory_group_new (allocator, bcreate.index);
538 allocator->groups[bcreate.index] = group;
543 GST_OBJECT_UNLOCK (allocator);
548 GST_WARNING_OBJECT (allocator, "error creating a new buffer: %s",
554 GST_ERROR_OBJECT (allocator, "created buffer has already used buffer "
555 "index %i, this means there is an bug in your driver or libv4l2",
561 static GstV4l2MemoryGroup *
562 gst_v4l2_allocator_alloc (GstV4l2Allocator * allocator)
564 GstV4l2MemoryGroup *group;
566 if (!g_atomic_int_get (&allocator->active))
569 group = gst_atomic_queue_pop (allocator->free_queue);
572 if (allocator->can_allocate) {
573 group = gst_v4l2_allocator_create_buf (allocator);
575 /* Don't hammer on CREATE_BUFS */
577 allocator->can_allocate = FALSE;
585 gst_v4l2_allocator_reset_size (GstV4l2Allocator * allocator,
586 GstV4l2MemoryGroup * group)
589 gboolean imported = FALSE;
591 switch (allocator->memory) {
592 case V4L2_MEMORY_USERPTR:
593 case V4L2_MEMORY_DMABUF:
598 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
601 for (i = 0; i < group->n_mem; i++) {
602 size = allocator->format.fmt.pix_mp.plane_fmt[i].sizeimage;
605 group->mem[i]->maxsize = size;
607 gst_memory_resize (group->mem[i], 0, size);
611 size = allocator->format.fmt.pix.sizeimage;
614 group->mem[0]->maxsize = size;
616 gst_memory_resize (group->mem[0], 0, size);
621 _cleanup_failed_alloc (GstV4l2Allocator * allocator, GstV4l2MemoryGroup * group)
623 if (group->mems_allocated > 0) {
625 /* If one or more mmap worked, we need to unref the memory, otherwise
626 * they will keep a ref on the allocator and leak it. This will put back
627 * the group into the free_queue */
628 for (i = 0; i < group->n_mem; i++)
629 gst_memory_unref (group->mem[i]);
631 /* Otherwise, group has to be on free queue for _stop() to work */
632 gst_atomic_queue_push (allocator->free_queue, group);
639 gst_v4l2_allocator_new (GstObject * parent, gint video_fd,
640 struct v4l2_format *format)
642 GstV4l2Allocator *allocator;
644 gchar *name, *parent_name;
646 parent_name = gst_object_get_name (parent);
647 name = g_strconcat (parent_name, ":allocator", NULL);
648 g_free (parent_name);
650 allocator = g_object_new (GST_TYPE_V4L2_ALLOCATOR, "name", name, NULL);
653 /* Save everything */
654 allocator->video_fd = v4l2_dup (video_fd);
655 allocator->type = format->type;
656 allocator->format = *format;
658 flags |= GST_V4L2_ALLOCATOR_PROBE (allocator, MMAP);
659 flags |= GST_V4L2_ALLOCATOR_PROBE (allocator, USERPTR);
660 flags |= GST_V4L2_ALLOCATOR_PROBE (allocator, DMABUF);
664 /* Drivers not ported from videobuf to videbuf2 don't allow freeing buffers
665 * using REQBUFS(0). This is a workaround to still support these drivers,
666 * which are known to have MMAP support. */
667 GST_WARNING_OBJECT (allocator, "Could not probe supported memory type, "
668 "assuming MMAP is supported, this is expected for older drivers not "
669 " yet ported to videobuf2 framework");
670 flags = GST_V4L2_ALLOCATOR_FLAG_MMAP_REQBUFS;
673 GST_OBJECT_FLAG_SET (allocator, flags);
679 gst_v4l2_allocator_start (GstV4l2Allocator * allocator, guint32 count,
682 struct v4l2_requestbuffers breq = { count, allocator->type, memory };
683 gboolean can_allocate;
686 g_return_val_if_fail (count != 0, 0);
688 GST_OBJECT_LOCK (allocator);
690 if (g_atomic_int_get (&allocator->active))
693 if (v4l2_ioctl (allocator->video_fd, VIDIOC_REQBUFS, &breq) < 0)
700 case V4L2_MEMORY_MMAP:
701 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (allocator, MMAP);
703 case V4L2_MEMORY_USERPTR:
704 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (allocator, USERPTR);
706 case V4L2_MEMORY_DMABUF:
707 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (allocator, DMABUF);
710 can_allocate = FALSE;
714 GST_DEBUG_OBJECT (allocator, "allocated %u %s buffers out of %u requested",
715 breq.count, memory_type_to_str (memory), count);
717 allocator->can_allocate = can_allocate;
718 allocator->count = breq.count;
719 allocator->memory = memory;
721 /* Create memory groups */
722 for (i = 0; i < allocator->count; i++) {
723 allocator->groups[i] = gst_v4l2_memory_group_new (allocator, i);
724 if (allocator->groups[i] == NULL)
727 gst_atomic_queue_push (allocator->free_queue, allocator->groups[i]);
730 g_atomic_int_set (&allocator->active, TRUE);
733 GST_OBJECT_UNLOCK (allocator);
738 GST_ERROR_OBJECT (allocator, "allocator already active");
743 GST_ERROR_OBJECT (allocator,
744 "error requesting %d buffers: %s", count, g_strerror (errno));
749 GST_ERROR_OBJECT (allocator, "Not enough memory to allocate buffers");
760 gst_v4l2_allocator_stop (GstV4l2Allocator * allocator)
762 struct v4l2_requestbuffers breq = { 0, allocator->type, allocator->memory };
764 GstV4l2Return ret = GST_V4L2_OK;
766 GST_DEBUG_OBJECT (allocator, "stop allocator");
768 GST_OBJECT_LOCK (allocator);
770 if (!g_atomic_int_get (&allocator->active))
773 if (gst_atomic_queue_length (allocator->free_queue) != allocator->count) {
774 GST_DEBUG_OBJECT (allocator, "allocator is still in use");
779 while (gst_atomic_queue_pop (allocator->free_queue)) {
783 for (i = 0; i < allocator->count; i++) {
784 GstV4l2MemoryGroup *group = allocator->groups[i];
785 allocator->groups[i] = NULL;
787 gst_v4l2_memory_group_free (group);
790 /* Not all drivers support rebufs(0), so warn only */
791 if (v4l2_ioctl (allocator->video_fd, VIDIOC_REQBUFS, &breq) < 0)
792 GST_WARNING_OBJECT (allocator,
793 "error releasing buffers buffers: %s", g_strerror (errno));
795 allocator->count = 0;
797 g_atomic_int_set (&allocator->active, FALSE);
800 GST_OBJECT_UNLOCK (allocator);
805 gst_v4l2_allocator_alloc_mmap (GstV4l2Allocator * allocator)
807 GstV4l2MemoryGroup *group;
810 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_MMAP, NULL);
812 group = gst_v4l2_allocator_alloc (allocator);
817 for (i = 0; i < group->n_mem; i++) {
818 if (group->mem[i] == NULL) {
820 data = v4l2_mmap (NULL, group->planes[i].length, PROT_READ | PROT_WRITE,
821 MAP_SHARED, allocator->video_fd, group->planes[i].m.mem_offset);
823 if (data == MAP_FAILED)
826 GST_LOG_OBJECT (allocator,
827 "mmap buffer length %d, data offset %d, plane %d",
828 group->planes[i].length, group->planes[i].data_offset, i);
830 group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
831 NULL, group->planes[i].length, 0, 0, group->planes[i].length, i,
834 /* Take back the allocator reference */
835 gst_object_ref (allocator);
838 group->mems_allocated++;
841 /* Ensure group size. Unlike GST, v4l2 have size (bytesused) initially set
842 * to 0. As length might be bigger then the expected size exposed in the
843 * format, we simply set bytesused initially and reset it here for
845 gst_v4l2_allocator_reset_size (allocator, group);
851 GST_ERROR_OBJECT (allocator, "Failed to mmap buffer: %s",
853 _cleanup_failed_alloc (allocator, group);
859 gst_v4l2_allocator_alloc_dmabuf (GstV4l2Allocator * allocator,
860 GstAllocator * dmabuf_allocator)
862 GstV4l2MemoryGroup *group;
865 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_MMAP, NULL);
867 group = gst_v4l2_allocator_alloc (allocator);
872 for (i = 0; i < group->n_mem; i++) {
877 if (group->mem[i] == NULL) {
878 struct v4l2_exportbuffer expbuf = { 0 };
880 expbuf.type = allocator->type;
881 expbuf.index = group->buffer.index;
883 expbuf.flags = O_CLOEXEC | O_RDWR;
885 if (v4l2_ioctl (allocator->video_fd, VIDIOC_EXPBUF, &expbuf) < 0)
888 GST_LOG_OBJECT (allocator, "exported DMABUF as fd %i plane %d",
891 group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
892 NULL, group->planes[i].length, 0, 0, group->planes[i].length, i,
893 NULL, expbuf.fd, group);
895 /* Take back the allocator reference */
896 gst_object_ref (allocator);
899 g_assert (gst_is_v4l2_memory (group->mem[i]));
900 mem = (GstV4l2Memory *) group->mem[i];
902 if ((dmafd = dup (mem->dmafd)) < 0)
905 dma_mem = gst_dmabuf_allocator_alloc (dmabuf_allocator, dmafd,
908 gst_mini_object_set_qdata (GST_MINI_OBJECT (dma_mem),
909 GST_V4L2_MEMORY_QUARK, mem, (GDestroyNotify) gst_memory_unref);
911 group->mem[i] = dma_mem;
912 group->mems_allocated++;
915 gst_v4l2_allocator_reset_size (allocator, group);
921 GST_ERROR_OBJECT (allocator, "Failed to export DMABUF: %s",
927 GST_ERROR_OBJECT (allocator, "Failed to dup DMABUF descriptor: %s",
933 _cleanup_failed_alloc (allocator, group);
939 gst_v4l2_allocator_clear_dmabufin (GstV4l2Allocator * allocator,
940 GstV4l2MemoryGroup * group)
945 g_return_if_fail (allocator->memory == V4L2_MEMORY_DMABUF);
947 for (i = 0; i < group->n_mem; i++) {
949 mem = (GstV4l2Memory *) group->mem[i];
951 GST_LOG_OBJECT (allocator, "clearing DMABUF import, fd %i plane %d",
958 mem->mem.maxsize = 0;
963 /* Update v4l2 structure */
964 group->planes[i].length = 0;
965 group->planes[i].bytesused = 0;
966 group->planes[i].m.fd = -1;
967 group->planes[i].data_offset = 0;
970 if (!V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
971 group->buffer.bytesused = 0;
972 group->buffer.length = 0;
973 group->buffer.m.fd = -1;
978 gst_v4l2_allocator_alloc_dmabufin (GstV4l2Allocator * allocator)
980 GstV4l2MemoryGroup *group;
983 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_DMABUF, NULL);
985 group = gst_v4l2_allocator_alloc (allocator);
990 GST_LOG_OBJECT (allocator, "allocating empty DMABUF import group");
992 for (i = 0; i < group->n_mem; i++) {
993 if (group->mem[i] == NULL) {
994 group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
995 NULL, 0, 0, 0, 0, i, NULL, -1, group);
997 /* Take back the allocator reference */
998 gst_object_ref (allocator);
1001 group->mems_allocated++;
1004 gst_v4l2_allocator_clear_dmabufin (allocator, group);
1010 gst_v4l2_allocator_clear_userptr (GstV4l2Allocator * allocator,
1011 GstV4l2MemoryGroup * group)
1016 g_return_if_fail (allocator->memory == V4L2_MEMORY_USERPTR);
1018 for (i = 0; i < group->n_mem; i++) {
1019 mem = (GstV4l2Memory *) group->mem[i];
1021 GST_LOG_OBJECT (allocator, "clearing USERPTR %p plane %d size %"
1022 G_GSIZE_FORMAT, mem->data, i, mem->mem.size);
1024 mem->mem.maxsize = 0;
1028 group->planes[i].length = 0;
1029 group->planes[i].bytesused = 0;
1030 group->planes[i].m.userptr = 0;
1033 if (!V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1034 group->buffer.bytesused = 0;
1035 group->buffer.length = 0;
1036 group->buffer.m.userptr = 0;
1040 GstV4l2MemoryGroup *
1041 gst_v4l2_allocator_alloc_userptr (GstV4l2Allocator * allocator)
1043 GstV4l2MemoryGroup *group;
1046 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_USERPTR, NULL);
1048 group = gst_v4l2_allocator_alloc (allocator);
1053 GST_LOG_OBJECT (allocator, "allocating empty USERPTR group");
1055 for (i = 0; i < group->n_mem; i++) {
1057 if (group->mem[i] == NULL) {
1058 group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
1059 NULL, 0, 0, 0, 0, i, NULL, -1, group);
1061 /* Take back the allocator reference */
1062 gst_object_ref (allocator);
1065 group->mems_allocated++;
1068 gst_v4l2_allocator_clear_userptr (allocator, group);
1074 gst_v4l2_allocator_import_dmabuf (GstV4l2Allocator * allocator,
1075 GstV4l2MemoryGroup * group, gint n_mem, GstMemory ** dma_mem)
1080 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_DMABUF, FALSE);
1082 if (group->n_mem != n_mem)
1083 goto n_mem_missmatch;
1085 for (i = 0; i < group->n_mem; i++) {
1087 gsize size, offset, maxsize;
1089 if (!gst_is_dmabuf_memory (dma_mem[i]))
1092 size = gst_memory_get_sizes (dma_mem[i], &offset, &maxsize);
1094 if ((dmafd = dup (gst_dmabuf_memory_get_fd (dma_mem[i]))) < 0)
1097 GST_LOG_OBJECT (allocator, "imported DMABUF as fd %i plane %d", dmafd, i);
1099 mem = (GstV4l2Memory *) group->mem[i];
1102 mem->mem.maxsize = maxsize;
1103 mem->mem.offset = offset;
1104 mem->mem.size = size;
1107 /* Update v4l2 structure */
1108 group->planes[i].length = maxsize;
1109 group->planes[i].bytesused = size;
1110 group->planes[i].m.fd = dmafd;
1111 group->planes[i].data_offset = offset;
1114 /* Copy into buffer structure if not using planes */
1115 if (!V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1116 group->buffer.bytesused = group->planes[0].bytesused;
1117 group->buffer.length = group->planes[0].length;
1118 group->buffer.m.fd = group->planes[0].m.userptr;
1120 group->buffer.length = group->n_mem;
1127 GST_ERROR_OBJECT (allocator, "Got %i dmabuf but needed %i", n_mem,
1133 GST_ERROR_OBJECT (allocator, "Memory %i is not of DMABUF", i);
1138 GST_ERROR_OBJECT (allocator, "Failed to dup DMABUF descriptor: %s",
1139 g_strerror (errno));
1145 gst_v4l2_allocator_import_userptr (GstV4l2Allocator * allocator,
1146 GstV4l2MemoryGroup * group, gsize img_size, int n_planes,
1147 gpointer * data, gsize * offset)
1152 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_USERPTR, FALSE);
1154 /* TODO Support passing N plane from 1 memory to MPLANE v4l2 format */
1155 if (n_planes != group->n_mem)
1156 goto n_mem_missmatch;
1158 for (i = 0; i < group->n_mem; i++) {
1159 gsize size, maxsize;
1161 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1162 struct v4l2_pix_format_mplane *pix = &allocator->format.fmt.pix_mp;
1163 maxsize = pix->plane_fmt[i].sizeimage;
1165 maxsize = allocator->format.fmt.pix.sizeimage;
1168 if ((i + 1) == n_planes) {
1169 size = img_size - offset[i];
1171 size = offset[i + 1] - offset[i];
1174 g_assert (size <= img_size);
1176 GST_LOG_OBJECT (allocator, "imported USERPTR %p plane %d size %"
1177 G_GSIZE_FORMAT, data[i], i, size);
1179 mem = (GstV4l2Memory *) group->mem[i];
1181 mem->mem.maxsize = maxsize;
1182 mem->mem.size = size;
1183 mem->data = data[i];
1185 group->planes[i].length = maxsize;
1186 group->planes[i].bytesused = size;
1187 group->planes[i].m.userptr = (unsigned long) data[i];
1188 group->planes[i].data_offset = 0;
1191 /* Copy into buffer structure if not using planes */
1192 if (!V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1193 group->buffer.bytesused = group->planes[0].bytesused;
1194 group->buffer.length = group->planes[0].length;
1195 group->buffer.m.userptr = group->planes[0].m.userptr;
1197 group->buffer.length = group->n_mem;
1204 GST_ERROR_OBJECT (allocator, "Got %i userptr plane while driver need %i",
1205 n_planes, group->n_mem);
1211 gst_v4l2_allocator_flush (GstV4l2Allocator * allocator)
1215 GST_OBJECT_LOCK (allocator);
1217 if (!g_atomic_int_get (&allocator->active))
1220 for (i = 0; i < allocator->count; i++) {
1221 GstV4l2MemoryGroup *group = allocator->groups[i];
1224 if (IS_QUEUED (group->buffer)) {
1225 UNSET_QUEUED (group->buffer);
1227 gst_v4l2_allocator_reset_group (allocator, group);
1229 for (n = 0; n < group->n_mem; n++)
1230 gst_memory_unref (group->mem[n]);
1235 GST_OBJECT_UNLOCK (allocator);
1239 gst_v4l2_allocator_qbuf (GstV4l2Allocator * allocator,
1240 GstV4l2MemoryGroup * group)
1242 gboolean ret = TRUE;
1245 g_return_val_if_fail (g_atomic_int_get (&allocator->active), FALSE);
1248 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1249 for (i = 0; i < group->n_mem; i++)
1250 group->planes[i].bytesused =
1251 gst_memory_get_sizes (group->mem[i], NULL, NULL);
1253 group->buffer.bytesused = gst_memory_get_sizes (group->mem[0], NULL, NULL);
1256 if (v4l2_ioctl (allocator->video_fd, VIDIOC_QBUF, &group->buffer) < 0) {
1257 GST_ERROR_OBJECT (allocator, "failed queing buffer %i: %s",
1258 group->buffer.index, g_strerror (errno));
1260 if (IS_QUEUED (group->buffer)) {
1261 GST_DEBUG_OBJECT (allocator,
1262 "driver pretends buffer is queued even if queue failed");
1263 UNSET_QUEUED (group->buffer);
1268 GST_LOG_OBJECT (allocator, "queued buffer %i (flags 0x%X)",
1269 group->buffer.index, group->buffer.flags);
1271 if (!IS_QUEUED (group->buffer)) {
1272 GST_DEBUG_OBJECT (allocator,
1273 "driver pretends buffer is not queued even if queue succeeded");
1274 SET_QUEUED (group->buffer);
1277 /* Ensure the memory will stay around and is RO */
1278 for (i = 0; i < group->n_mem; i++)
1279 gst_memory_ref (group->mem[i]);
1285 GstV4l2MemoryGroup *
1286 gst_v4l2_allocator_dqbuf (GstV4l2Allocator * allocator)
1288 struct v4l2_buffer buffer = { 0 };
1289 struct v4l2_plane planes[VIDEO_MAX_PLANES] = { {0} };
1292 GstV4l2MemoryGroup *group = NULL;
1294 g_return_val_if_fail (g_atomic_int_get (&allocator->active), FALSE);
1296 buffer.type = allocator->type;
1297 buffer.memory = allocator->memory;
1299 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1300 buffer.length = allocator->format.fmt.pix_mp.num_planes;
1301 buffer.m.planes = planes;
1304 if (v4l2_ioctl (allocator->video_fd, VIDIOC_DQBUF, &buffer) < 0)
1307 group = allocator->groups[buffer.index];
1308 group->buffer = buffer;
1310 GST_LOG_OBJECT (allocator, "dequeued buffer %i (flags 0x%X)", buffer.index,
1313 if (IS_QUEUED (group->buffer)) {
1314 GST_DEBUG_OBJECT (allocator,
1315 "driver pretends buffer is queued even if dequeue succeeded");
1316 UNSET_QUEUED (group->buffer);
1319 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1320 group->buffer.m.planes = group->planes;
1321 memcpy (group->planes, buffer.m.planes, sizeof (planes));
1323 group->planes[0].bytesused = group->buffer.bytesused;
1324 group->planes[0].length = group->buffer.length;
1325 g_assert (sizeof (group->planes[0].m) == sizeof (group->buffer.m));
1326 memcpy (&group->planes[0].m, &group->buffer.m, sizeof (group->buffer.m));
1329 /* And update memory size */
1330 if (V4L2_TYPE_IS_OUTPUT (allocator->type)) {
1331 gst_v4l2_allocator_reset_size (allocator, group);
1333 /* for capture, simply read the size */
1334 for (i = 0; i < group->n_mem; i++) {
1335 gst_memory_resize (group->mem[i], 0, group->planes[i].bytesused);
1339 /* Release the memory, possibly making it RW again */
1340 for (i = 0; i < group->n_mem; i++)
1341 gst_memory_unref (group->mem[i]);
1346 GST_ERROR_OBJECT (allocator, "failed dequeuing a %s buffer: %s",
1347 memory_type_to_str (allocator->memory), g_strerror (errno));
1351 GST_WARNING_OBJECT (allocator,
1352 "Non-blocking I/O has been selected using O_NONBLOCK and"
1353 " no buffer was in the outgoing queue.");
1356 GST_ERROR_OBJECT (allocator,
1357 "The buffer type is not supported, or the index is out of bounds, "
1358 "or no buffers have been allocated yet, or the userptr "
1359 "or length are invalid.");
1362 GST_ERROR_OBJECT (allocator,
1363 "insufficient memory to enqueue a user pointer buffer");
1366 GST_INFO_OBJECT (allocator,
1367 "VIDIOC_DQBUF failed due to an internal error."
1368 " Can also indicate temporary problems like signal loss."
1369 " Note the driver might dequeue an (empty) buffer despite"
1370 " returning an error, or even stop capturing.");
1371 /* have we de-queued a buffer ? */
1372 if (!IS_QUEUED (buffer)) {
1373 GST_DEBUG_OBJECT (allocator, "reenqueing buffer");
1374 /* FIXME ... should we do something here? */
1378 GST_WARNING_OBJECT (allocator, "could not sync on a buffer on device");
1381 GST_WARNING_OBJECT (allocator,
1382 "Grabbing frame got interrupted unexpectedly. %d: %s.", errno,
1383 g_strerror (errno));
1391 gst_v4l2_allocator_reset_group (GstV4l2Allocator * allocator,
1392 GstV4l2MemoryGroup * group)
1394 switch (allocator->memory) {
1395 case V4L2_MEMORY_USERPTR:
1396 gst_v4l2_allocator_clear_userptr (allocator, group);
1398 case V4L2_MEMORY_DMABUF:
1399 gst_v4l2_allocator_clear_dmabufin (allocator, group);
1401 case V4L2_MEMORY_MMAP:
1404 g_assert_not_reached ();
1408 gst_v4l2_allocator_reset_size (allocator, group);
1412 gst_v4l2_allocator_num_allocated (GstV4l2Allocator * allocator)
1414 gsize num_allocated;
1416 GST_OBJECT_LOCK (allocator);
1418 num_allocated = allocator->count;
1420 GST_OBJECT_UNLOCK (allocator);
1422 return num_allocated;