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"
30 #include "gstv4l2object.h"
31 #include "gstv4l2allocator.h"
33 #include <gst/allocators/gstdmabuf.h>
38 #include <sys/types.h>
42 #define GST_V4L2_MEMORY_TYPE "V4l2Memory"
44 #define gst_v4l2_allocator_parent_class parent_class
45 G_DEFINE_TYPE (GstV4l2Allocator, gst_v4l2_allocator, GST_TYPE_ALLOCATOR);
47 GST_DEBUG_CATEGORY_STATIC (v4l2allocator_debug);
48 #define GST_CAT_DEFAULT v4l2allocator_debug
50 #define UNSET_QUEUED(buffer) \
51 ((buffer).flags &= ~(V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
53 #define SET_QUEUED(buffer) ((buffer).flags |= V4L2_BUF_FLAG_QUEUED)
55 #define IS_QUEUED(buffer) \
56 ((buffer).flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
64 static guint gst_v4l2_allocator_signals[LAST_SIGNAL] = { 0 };
66 static void gst_v4l2_allocator_release (GstV4l2Allocator * allocator,
70 memory_type_to_str (guint32 memory)
73 case V4L2_MEMORY_MMAP:
75 case V4L2_MEMORY_USERPTR:
77 case V4L2_MEMORY_DMABUF:
84 /*************************************/
85 /* GstV4lMemory implementation */
86 /*************************************/
89 _v4l2mem_map (GstV4l2Memory * mem, gsize maxsize, GstMapFlags flags)
93 switch (mem->group->buffer.memory) {
94 case V4L2_MEMORY_MMAP:
95 case V4L2_MEMORY_USERPTR:
98 case V4L2_MEMORY_DMABUF:
99 /* v4l2 dmabuf memory are not shared with downstream */
100 g_assert_not_reached ();
103 GST_WARNING ("Unknown memory type %i", mem->group->buffer.memory);
110 _v4l2mem_unmap (GstV4l2Memory * mem)
112 gboolean ret = FALSE;
114 switch (mem->group->buffer.memory) {
115 case V4L2_MEMORY_MMAP:
116 case V4L2_MEMORY_USERPTR:
119 case V4L2_MEMORY_DMABUF:
120 /* v4l2 dmabuf memory are not share with downstream */
121 g_assert_not_reached ();
124 GST_WARNING ("Unknown memory type %i", mem->group->buffer.memory);
131 _v4l2mem_dispose (GstV4l2Memory * mem)
133 GstV4l2Allocator *allocator = (GstV4l2Allocator *) mem->mem.allocator;
134 GstV4l2MemoryGroup *group = mem->group;
137 if (group->mem[mem->plane]) {
138 /* We may have a dmabuf, replace it with returned original memory */
139 group->mem[mem->plane] = gst_memory_ref ((GstMemory *) mem);
140 gst_v4l2_allocator_release (allocator, mem);
143 gst_object_ref (allocator);
150 static inline GstV4l2Memory *
151 _v4l2mem_new (GstMemoryFlags flags, GstAllocator * allocator,
152 GstMemory * parent, gsize maxsize, gsize align, gsize offset, gsize size,
153 gint plane, gpointer data, int dmafd, GstV4l2MemoryGroup * group)
157 mem = g_slice_new0 (GstV4l2Memory);
158 gst_memory_init (GST_MEMORY_CAST (mem),
159 flags, allocator, parent, maxsize, align, offset, size);
162 mem->mem.mini_object.dispose =
163 (GstMiniObjectDisposeFunction) _v4l2mem_dispose;
173 static GstV4l2Memory *
174 _v4l2mem_share (GstV4l2Memory * mem, gssize offset, gsize size)
179 /* find the real parent */
180 if ((parent = mem->mem.parent) == NULL)
181 parent = (GstMemory *) mem;
184 size = mem->mem.size - offset;
186 /* the shared memory is always readonly */
187 sub = _v4l2mem_new (GST_MINI_OBJECT_FLAGS (parent) |
188 GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->mem.allocator, parent,
189 mem->mem.maxsize, mem->mem.align, offset, size, mem->plane, mem->data,
196 _v4l2mem_is_span (GstV4l2Memory * mem1, GstV4l2Memory * mem2, gsize * offset)
199 *offset = mem1->mem.offset - mem1->mem.parent->offset;
201 /* and memory is contiguous */
202 return mem1->mem.offset + mem1->mem.size == mem2->mem.offset;
206 gst_is_v4l2_memory (GstMemory * mem)
208 return gst_memory_is_type (mem, GST_V4L2_MEMORY_TYPE);
212 gst_v4l2_memory_quark (void)
214 static GQuark quark = 0;
217 quark = g_quark_from_string ("GstV4l2Memory");
223 /*************************************/
224 /* GstV4l2MemoryGroup implementation */
225 /*************************************/
228 gst_v4l2_memory_group_free (GstV4l2MemoryGroup * group)
232 for (i = 0; i < group->n_mem; i++) {
233 GstMemory *mem = group->mem[i];
234 group->mem[i] = NULL;
236 gst_memory_unref (mem);
239 g_slice_free (GstV4l2MemoryGroup, group);
242 static GstV4l2MemoryGroup *
243 gst_v4l2_memory_group_new (GstV4l2Allocator * allocator, guint32 index)
245 GstV4l2Object *obj = allocator->obj;
246 guint32 memory = allocator->memory;
247 struct v4l2_format *format = &obj->format;
248 GstV4l2MemoryGroup *group;
249 gsize img_size, buf_size;
251 group = g_slice_new0 (GstV4l2MemoryGroup);
253 group->buffer.type = format->type;
254 group->buffer.index = index;
255 group->buffer.memory = memory;
257 if (V4L2_TYPE_IS_MULTIPLANAR (format->type)) {
258 group->n_mem = group->buffer.length = format->fmt.pix_mp.num_planes;
259 group->buffer.m.planes = group->planes;
264 if (obj->ioctl (obj->video_fd, VIDIOC_QUERYBUF, &group->buffer) < 0)
265 goto querybuf_failed;
267 if (group->buffer.index != index) {
268 GST_ERROR_OBJECT (allocator, "Buffer index returned by VIDIOC_QUERYBUF "
269 "didn't match, this indicate the presence of a bug in your driver or "
271 g_slice_free (GstV4l2MemoryGroup, group);
275 /* Check that provided size matches the format we have negotiation. Failing
276 * there usually means a driver of libv4l bug. */
277 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
280 for (i = 0; i < group->n_mem; i++) {
281 img_size = obj->format.fmt.pix_mp.plane_fmt[i].sizeimage;
282 buf_size = group->planes[i].length;
283 if (buf_size < img_size)
284 goto buffer_too_short;
287 img_size = obj->format.fmt.pix.sizeimage;
288 buf_size = group->buffer.length;
289 if (buf_size < img_size)
290 goto buffer_too_short;
293 /* We save non planar buffer information into the multi-planar plane array
294 * to avoid duplicating the code later */
295 if (!V4L2_TYPE_IS_MULTIPLANAR (format->type)) {
296 group->planes[0].bytesused = group->buffer.bytesused;
297 group->planes[0].length = group->buffer.length;
298 g_assert (sizeof (group->planes[0].m) == sizeof (group->buffer.m));
299 memcpy (&group->planes[0].m, &group->buffer.m, sizeof (group->buffer.m));
302 GST_LOG_OBJECT (allocator, "Got %s buffer", memory_type_to_str (memory));
303 GST_LOG_OBJECT (allocator, " index: %u", group->buffer.index);
304 GST_LOG_OBJECT (allocator, " type: %d", group->buffer.type);
305 GST_LOG_OBJECT (allocator, " flags: %08x", group->buffer.flags);
306 GST_LOG_OBJECT (allocator, " field: %d", group->buffer.field);
307 GST_LOG_OBJECT (allocator, " memory: %d", group->buffer.memory);
308 GST_LOG_OBJECT (allocator, " planes: %d", group->n_mem);
310 #ifndef GST_DISABLE_GST_DEBUG
311 if (memory == V4L2_MEMORY_MMAP) {
313 for (i = 0; i < group->n_mem; i++) {
314 GST_LOG_OBJECT (allocator, " [%u] bytesused: %u, length: %u", i,
315 group->planes[i].bytesused, group->planes[i].length);
316 GST_LOG_OBJECT (allocator, " [%u] MMAP offset: %u", i,
317 group->planes[i].m.mem_offset);
326 GST_ERROR ("error querying buffer %d: %s", index, g_strerror (errno));
331 GST_ERROR ("buffer size %" G_GSIZE_FORMAT
332 " is smaller then negotiated size %" G_GSIZE_FORMAT
333 ", this is usually the result of a bug in the v4l2 driver or libv4l.",
338 gst_v4l2_memory_group_free (group);
343 /*************************************/
344 /* GstV4lAllocator implementation */
345 /*************************************/
348 gst_v4l2_allocator_release (GstV4l2Allocator * allocator, GstV4l2Memory * mem)
350 GstV4l2MemoryGroup *group = mem->group;
352 GST_LOG_OBJECT (allocator, "plane %i of buffer %u released",
353 mem->plane, group->buffer.index);
355 switch (allocator->memory) {
356 case V4L2_MEMORY_DMABUF:
360 case V4L2_MEMORY_USERPTR:
367 /* When all memory are back, put the group back in the free queue */
368 if (g_atomic_int_dec_and_test (&group->mems_allocated)) {
369 GST_LOG_OBJECT (allocator, "buffer %u released", group->buffer.index);
370 gst_atomic_queue_push (allocator->free_queue, group);
371 g_signal_emit (allocator, gst_v4l2_allocator_signals[GROUP_RELEASED], 0);
374 /* Keep last, allocator may be freed after this call */
375 g_object_unref (allocator);
379 gst_v4l2_allocator_free (GstAllocator * gallocator, GstMemory * gmem)
381 GstV4l2Allocator *allocator = (GstV4l2Allocator *) gallocator;
382 GstV4l2Object *obj = allocator->obj;
383 GstV4l2Memory *mem = (GstV4l2Memory *) gmem;
384 GstV4l2MemoryGroup *group = mem->group;
386 /* Only free unparented memory */
387 if (mem->mem.parent == NULL) {
388 GST_LOG_OBJECT (allocator, "freeing plane %i of buffer %u",
389 mem->plane, group->buffer.index);
391 if (allocator->memory == V4L2_MEMORY_MMAP) {
393 obj->munmap (mem->data, group->planes[mem->plane].length);
396 /* This apply for both mmap with expbuf, and dmabuf imported memory */
401 g_slice_free (GstV4l2Memory, mem);
405 gst_v4l2_allocator_dispose (GObject * obj)
407 GstV4l2Allocator *allocator = (GstV4l2Allocator *) obj;
410 GST_LOG_OBJECT (obj, "called");
412 for (i = 0; i < allocator->count; i++) {
413 GstV4l2MemoryGroup *group = allocator->groups[i];
414 allocator->groups[i] = NULL;
416 gst_v4l2_memory_group_free (group);
419 G_OBJECT_CLASS (parent_class)->dispose (obj);
423 gst_v4l2_allocator_finalize (GObject * obj)
425 GstV4l2Allocator *allocator = (GstV4l2Allocator *) obj;
427 GST_LOG_OBJECT (obj, "called");
429 gst_atomic_queue_unref (allocator->free_queue);
430 gst_object_unref (allocator->obj->element);
432 G_OBJECT_CLASS (parent_class)->finalize (obj);
436 gst_v4l2_allocator_class_init (GstV4l2AllocatorClass * klass)
438 GObjectClass *object_class;
439 GstAllocatorClass *allocator_class;
441 allocator_class = (GstAllocatorClass *) klass;
442 object_class = (GObjectClass *) klass;
444 allocator_class->alloc = NULL;
445 allocator_class->free = gst_v4l2_allocator_free;
447 object_class->dispose = gst_v4l2_allocator_dispose;
448 object_class->finalize = gst_v4l2_allocator_finalize;
450 gst_v4l2_allocator_signals[GROUP_RELEASED] = g_signal_new ("group-released",
451 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
454 GST_DEBUG_CATEGORY_INIT (v4l2allocator_debug, "v4l2allocator", 0,
459 gst_v4l2_allocator_init (GstV4l2Allocator * allocator)
461 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
463 alloc->mem_type = GST_V4L2_MEMORY_TYPE;
464 alloc->mem_map = (GstMemoryMapFunction) _v4l2mem_map;
465 alloc->mem_unmap = (GstMemoryUnmapFunction) _v4l2mem_unmap;
466 alloc->mem_share = (GstMemoryShareFunction) _v4l2mem_share;
467 alloc->mem_is_span = (GstMemoryIsSpanFunction) _v4l2mem_is_span;
468 /* Use the default, fallback copy function */
470 allocator->free_queue = gst_atomic_queue_new (VIDEO_MAX_FRAME);
472 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
475 #define GST_V4L2_ALLOCATOR_PROBE(obj,type) \
476 gst_v4l2_allocator_probe ((obj), V4L2_MEMORY_ ## type, \
477 GST_V4L2_ALLOCATOR_FLAG_ ## type ## _REQBUFS, \
478 GST_V4L2_ALLOCATOR_FLAG_ ## type ## _CREATE_BUFS)
480 gst_v4l2_allocator_probe (GstV4l2Allocator * allocator, guint32 memory,
481 guint32 breq_flag, guint32 bcreate_flag)
483 GstV4l2Object *obj = allocator->obj;
484 struct v4l2_requestbuffers breq = { 0 };
487 breq.type = obj->type;
489 breq.memory = memory;
491 if (obj->ioctl (obj->video_fd, VIDIOC_REQBUFS, &breq) == 0) {
492 struct v4l2_create_buffers bcreate = { 0 };
496 bcreate.memory = memory;
497 bcreate.format = obj->format;
499 if ((obj->ioctl (obj->video_fd, VIDIOC_CREATE_BUFS, &bcreate) == 0))
500 flags |= bcreate_flag;
506 static GstV4l2MemoryGroup *
507 gst_v4l2_allocator_create_buf (GstV4l2Allocator * allocator)
509 GstV4l2Object *obj = allocator->obj;
510 struct v4l2_create_buffers bcreate = { 0 };
511 GstV4l2MemoryGroup *group = NULL;
513 GST_OBJECT_LOCK (allocator);
515 if (!g_atomic_int_get (&allocator->active))
518 bcreate.memory = allocator->memory;
519 bcreate.format = obj->format;
522 if (!allocator->can_allocate)
525 if (obj->ioctl (obj->video_fd, VIDIOC_CREATE_BUFS, &bcreate) < 0)
526 goto create_bufs_failed;
528 if (allocator->groups[bcreate.index] != NULL)
529 goto create_bufs_bug;
531 group = gst_v4l2_memory_group_new (allocator, bcreate.index);
534 allocator->groups[bcreate.index] = group;
539 GST_OBJECT_UNLOCK (allocator);
544 GST_WARNING_OBJECT (allocator, "error creating a new buffer: %s",
550 GST_ERROR_OBJECT (allocator, "created buffer has already used buffer "
551 "index %i, this means there is an bug in your driver or libv4l2",
557 static GstV4l2MemoryGroup *
558 gst_v4l2_allocator_alloc (GstV4l2Allocator * allocator)
560 GstV4l2MemoryGroup *group;
562 if (!g_atomic_int_get (&allocator->active))
565 group = gst_atomic_queue_pop (allocator->free_queue);
568 if (allocator->can_allocate) {
569 group = gst_v4l2_allocator_create_buf (allocator);
571 /* Don't hammer on CREATE_BUFS */
573 allocator->can_allocate = FALSE;
581 gst_v4l2_allocator_reset_size (GstV4l2Allocator * allocator,
582 GstV4l2MemoryGroup * group)
584 GstV4l2Object *obj = allocator->obj;
586 gboolean imported = FALSE;
588 switch (allocator->memory) {
589 case V4L2_MEMORY_USERPTR:
590 case V4L2_MEMORY_DMABUF:
595 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
598 for (i = 0; i < group->n_mem; i++) {
599 size = obj->format.fmt.pix_mp.plane_fmt[i].sizeimage;
602 group->mem[i]->maxsize = size;
604 gst_memory_resize (group->mem[i], 0, size);
608 size = obj->format.fmt.pix.sizeimage;
611 group->mem[0]->maxsize = size;
613 gst_memory_resize (group->mem[0], 0, size);
618 _cleanup_failed_alloc (GstV4l2Allocator * allocator, GstV4l2MemoryGroup * group)
620 if (group->mems_allocated > 0) {
622 /* If one or more mmap worked, we need to unref the memory, otherwise
623 * they will keep a ref on the allocator and leak it. This will put back
624 * the group into the free_queue */
625 for (i = 0; i < group->n_mem; i++)
626 gst_memory_unref (group->mem[i]);
628 /* Otherwise, group has to be on free queue for _stop() to work */
629 gst_atomic_queue_push (allocator->free_queue, group);
636 gst_v4l2_allocator_new (GstObject * parent, GstV4l2Object * v4l2object)
638 GstV4l2Allocator *allocator;
640 gchar *name, *parent_name;
642 parent_name = gst_object_get_name (parent);
643 name = g_strconcat (parent_name, ":allocator", NULL);
644 g_free (parent_name);
646 allocator = g_object_new (GST_TYPE_V4L2_ALLOCATOR, "name", name, NULL);
647 gst_object_ref_sink (allocator);
650 /* Save everything */
651 allocator->obj = v4l2object;
653 /* Keep a ref on the elemnt so obj does not disapear */
654 gst_object_ref (allocator->obj->element);
656 flags |= GST_V4L2_ALLOCATOR_PROBE (allocator, MMAP);
657 flags |= GST_V4L2_ALLOCATOR_PROBE (allocator, USERPTR);
658 flags |= GST_V4L2_ALLOCATOR_PROBE (allocator, DMABUF);
662 /* Drivers not ported from videobuf to videbuf2 don't allow freeing buffers
663 * using REQBUFS(0). This is a workaround to still support these drivers,
664 * which are known to have MMAP support. */
665 GST_WARNING_OBJECT (allocator, "Could not probe supported memory type, "
666 "assuming MMAP is supported, this is expected for older drivers not "
667 " yet ported to videobuf2 framework");
668 flags = GST_V4L2_ALLOCATOR_FLAG_MMAP_REQBUFS;
671 GST_OBJECT_FLAG_SET (allocator, flags);
677 gst_v4l2_allocator_start (GstV4l2Allocator * allocator, guint32 count,
680 GstV4l2Object *obj = allocator->obj;
681 struct v4l2_requestbuffers breq = { count, obj->type, memory };
682 gboolean can_allocate;
685 g_return_val_if_fail (count != 0, 0);
687 GST_OBJECT_LOCK (allocator);
689 if (g_atomic_int_get (&allocator->active))
692 if (obj->ioctl (obj->video_fd, VIDIOC_REQBUFS, &breq) < 0)
699 case V4L2_MEMORY_MMAP:
700 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (allocator, MMAP);
702 case V4L2_MEMORY_USERPTR:
703 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (allocator, USERPTR);
705 case V4L2_MEMORY_DMABUF:
706 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (allocator, DMABUF);
709 can_allocate = FALSE;
713 GST_DEBUG_OBJECT (allocator, "allocated %u %s buffers out of %u requested",
714 breq.count, memory_type_to_str (memory), count);
716 allocator->can_allocate = can_allocate;
717 allocator->count = breq.count;
718 allocator->memory = memory;
720 /* Create memory groups */
721 for (i = 0; i < allocator->count; i++) {
722 allocator->groups[i] = gst_v4l2_memory_group_new (allocator, i);
723 if (allocator->groups[i] == NULL)
726 gst_atomic_queue_push (allocator->free_queue, allocator->groups[i]);
729 g_atomic_int_set (&allocator->active, TRUE);
732 GST_OBJECT_UNLOCK (allocator);
737 GST_ERROR_OBJECT (allocator, "allocator already active");
742 GST_ERROR_OBJECT (allocator,
743 "error requesting %d buffers: %s", count, g_strerror (errno));
748 GST_ERROR_OBJECT (allocator, "Not enough memory to allocate buffers");
759 gst_v4l2_allocator_stop (GstV4l2Allocator * allocator)
761 GstV4l2Object *obj = allocator->obj;
762 struct v4l2_requestbuffers breq = { 0, obj->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 (obj->ioctl (obj->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 GstV4l2Object *obj = allocator->obj;
808 GstV4l2MemoryGroup *group;
811 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_MMAP, NULL);
813 group = gst_v4l2_allocator_alloc (allocator);
818 for (i = 0; i < group->n_mem; i++) {
819 if (group->mem[i] == NULL) {
821 data = obj->mmap (NULL, group->planes[i].length, PROT_READ | PROT_WRITE,
822 MAP_SHARED, obj->video_fd, group->planes[i].m.mem_offset);
824 if (data == MAP_FAILED)
827 GST_LOG_OBJECT (allocator,
828 "mmap buffer length %d, data offset %d, plane %d",
829 group->planes[i].length, group->planes[i].data_offset, i);
831 group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
832 NULL, group->planes[i].length, 0, 0, group->planes[i].length, i,
835 /* Take back the allocator reference */
836 gst_object_ref (allocator);
839 group->mems_allocated++;
842 /* Ensure group size. Unlike GST, v4l2 have size (bytesused) initially set
843 * to 0. As length might be bigger then the expected size exposed in the
844 * format, we simply set bytesused initially and reset it here for
846 gst_v4l2_allocator_reset_size (allocator, group);
852 GST_ERROR_OBJECT (allocator, "Failed to mmap buffer: %s",
854 _cleanup_failed_alloc (allocator, group);
860 gst_v4l2_allocator_alloc_dmabuf (GstV4l2Allocator * allocator,
861 GstAllocator * dmabuf_allocator)
863 GstV4l2Object *obj = allocator->obj;
864 GstV4l2MemoryGroup *group;
867 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_MMAP, NULL);
869 group = gst_v4l2_allocator_alloc (allocator);
874 for (i = 0; i < group->n_mem; i++) {
879 if (group->mem[i] == NULL) {
880 struct v4l2_exportbuffer expbuf = { 0 };
882 expbuf.type = obj->type;
883 expbuf.index = group->buffer.index;
885 expbuf.flags = O_CLOEXEC | O_RDWR;
887 if (obj->ioctl (obj->video_fd, VIDIOC_EXPBUF, &expbuf) < 0)
890 GST_LOG_OBJECT (allocator, "exported DMABUF as fd %i plane %d",
893 group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
894 NULL, group->planes[i].length, 0, 0, group->planes[i].length, i,
895 NULL, expbuf.fd, group);
897 /* Take back the allocator reference */
898 gst_object_ref (allocator);
901 group->mems_allocated++;
903 g_assert (gst_is_v4l2_memory (group->mem[i]));
904 mem = (GstV4l2Memory *) group->mem[i];
906 if ((dmafd = dup (mem->dmafd)) < 0)
909 dma_mem = gst_dmabuf_allocator_alloc (dmabuf_allocator, dmafd,
912 gst_mini_object_set_qdata (GST_MINI_OBJECT (dma_mem),
913 GST_V4L2_MEMORY_QUARK, mem, (GDestroyNotify) gst_memory_unref);
915 group->mem[i] = dma_mem;
918 gst_v4l2_allocator_reset_size (allocator, group);
924 GST_ERROR_OBJECT (allocator, "Failed to export DMABUF: %s",
930 GST_ERROR_OBJECT (allocator, "Failed to dup DMABUF descriptor: %s",
936 _cleanup_failed_alloc (allocator, group);
942 gst_v4l2_allocator_clear_dmabufin (GstV4l2Allocator * allocator,
943 GstV4l2MemoryGroup * group)
945 GstV4l2Object *obj = allocator->obj;
949 g_return_if_fail (allocator->memory == V4L2_MEMORY_DMABUF);
951 for (i = 0; i < group->n_mem; i++) {
953 mem = (GstV4l2Memory *) group->mem[i];
955 GST_LOG_OBJECT (allocator, "clearing DMABUF import, fd %i plane %d",
962 mem->mem.maxsize = 0;
967 /* Update v4l2 structure */
968 group->planes[i].length = 0;
969 group->planes[i].bytesused = 0;
970 group->planes[i].m.fd = -1;
971 group->planes[i].data_offset = 0;
974 if (!V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
975 group->buffer.bytesused = 0;
976 group->buffer.length = 0;
977 group->buffer.m.fd = -1;
982 gst_v4l2_allocator_alloc_dmabufin (GstV4l2Allocator * allocator)
984 GstV4l2MemoryGroup *group;
987 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_DMABUF, NULL);
989 group = gst_v4l2_allocator_alloc (allocator);
994 GST_LOG_OBJECT (allocator, "allocating empty DMABUF import group");
996 for (i = 0; i < group->n_mem; i++) {
997 if (group->mem[i] == NULL) {
998 group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
999 NULL, 0, 0, 0, 0, i, NULL, -1, group);
1001 /* Take back the allocator reference */
1002 gst_object_ref (allocator);
1005 group->mems_allocated++;
1008 gst_v4l2_allocator_clear_dmabufin (allocator, group);
1014 gst_v4l2_allocator_clear_userptr (GstV4l2Allocator * allocator,
1015 GstV4l2MemoryGroup * group)
1017 GstV4l2Object *obj = allocator->obj;
1021 g_return_if_fail (allocator->memory == V4L2_MEMORY_USERPTR);
1023 for (i = 0; i < group->n_mem; i++) {
1024 mem = (GstV4l2Memory *) group->mem[i];
1026 GST_LOG_OBJECT (allocator, "clearing USERPTR %p plane %d size %"
1027 G_GSIZE_FORMAT, mem->data, i, mem->mem.size);
1029 mem->mem.maxsize = 0;
1033 group->planes[i].length = 0;
1034 group->planes[i].bytesused = 0;
1035 group->planes[i].m.userptr = 0;
1038 if (!V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
1039 group->buffer.bytesused = 0;
1040 group->buffer.length = 0;
1041 group->buffer.m.userptr = 0;
1045 GstV4l2MemoryGroup *
1046 gst_v4l2_allocator_alloc_userptr (GstV4l2Allocator * allocator)
1048 GstV4l2MemoryGroup *group;
1051 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_USERPTR, NULL);
1053 group = gst_v4l2_allocator_alloc (allocator);
1058 GST_LOG_OBJECT (allocator, "allocating empty USERPTR group");
1060 for (i = 0; i < group->n_mem; i++) {
1062 if (group->mem[i] == NULL) {
1063 group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
1064 NULL, 0, 0, 0, 0, i, NULL, -1, group);
1066 /* Take back the allocator reference */
1067 gst_object_ref (allocator);
1070 group->mems_allocated++;
1073 gst_v4l2_allocator_clear_userptr (allocator, group);
1079 gst_v4l2_allocator_import_dmabuf (GstV4l2Allocator * allocator,
1080 GstV4l2MemoryGroup * group, gint n_mem, GstMemory ** dma_mem)
1082 GstV4l2Object *obj = allocator->obj;
1086 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_DMABUF, FALSE);
1088 if (group->n_mem != n_mem)
1089 goto n_mem_missmatch;
1091 for (i = 0; i < group->n_mem; i++) {
1093 gsize size, offset, maxsize;
1095 if (!gst_is_dmabuf_memory (dma_mem[i]))
1098 size = gst_memory_get_sizes (dma_mem[i], &offset, &maxsize);
1100 if ((dmafd = dup (gst_dmabuf_memory_get_fd (dma_mem[i]))) < 0)
1103 GST_LOG_OBJECT (allocator, "imported DMABUF as fd %i plane %d", dmafd, i);
1105 mem = (GstV4l2Memory *) group->mem[i];
1108 mem->mem.maxsize = maxsize;
1109 mem->mem.offset = offset;
1110 mem->mem.size = size;
1113 /* Update v4l2 structure */
1114 group->planes[i].length = maxsize;
1115 group->planes[i].bytesused = size;
1116 group->planes[i].m.fd = dmafd;
1117 group->planes[i].data_offset = offset;
1120 /* Copy into buffer structure if not using planes */
1121 if (!V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
1122 group->buffer.bytesused = group->planes[0].bytesused;
1123 group->buffer.length = group->planes[0].length;
1124 group->buffer.m.fd = group->planes[0].m.userptr;
1126 group->buffer.length = group->n_mem;
1133 GST_ERROR_OBJECT (allocator, "Got %i dmabuf but needed %i", n_mem,
1139 GST_ERROR_OBJECT (allocator, "Memory %i is not of DMABUF", i);
1144 GST_ERROR_OBJECT (allocator, "Failed to dup DMABUF descriptor: %s",
1145 g_strerror (errno));
1151 gst_v4l2_allocator_import_userptr (GstV4l2Allocator * allocator,
1152 GstV4l2MemoryGroup * group, gsize img_size, int n_planes,
1153 gpointer * data, gsize * size)
1155 GstV4l2Object *obj = allocator->obj;
1159 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_USERPTR, FALSE);
1161 /* TODO Support passing N plane from 1 memory to MPLANE v4l2 format */
1162 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type) && n_planes != group->n_mem)
1163 goto n_mem_missmatch;
1165 for (i = 0; i < group->n_mem; i++) {
1166 gsize maxsize, psize;
1168 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
1169 struct v4l2_pix_format_mplane *pix = &obj->format.fmt.pix_mp;
1170 maxsize = pix->plane_fmt[i].sizeimage;
1173 maxsize = obj->format.fmt.pix.sizeimage;
1177 g_assert (psize <= img_size);
1179 GST_LOG_OBJECT (allocator, "imported USERPTR %p plane %d size %"
1180 G_GSIZE_FORMAT, data[i], i, psize);
1182 mem = (GstV4l2Memory *) group->mem[i];
1184 mem->mem.maxsize = maxsize;
1185 mem->mem.size = psize;
1186 mem->data = data[i];
1188 group->planes[i].length = maxsize;
1189 group->planes[i].bytesused = psize;
1190 group->planes[i].m.userptr = (unsigned long) data[i];
1191 group->planes[i].data_offset = 0;
1194 /* Copy into buffer structure if not using planes */
1195 if (!V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
1196 group->buffer.bytesused = group->planes[0].bytesused;
1197 group->buffer.length = group->planes[0].length;
1198 group->buffer.m.userptr = group->planes[0].m.userptr;
1200 group->buffer.length = group->n_mem;
1207 GST_ERROR_OBJECT (allocator, "Got %i userptr plane while driver need %i",
1208 n_planes, group->n_mem);
1214 gst_v4l2_allocator_flush (GstV4l2Allocator * allocator)
1218 GST_OBJECT_LOCK (allocator);
1220 if (!g_atomic_int_get (&allocator->active))
1223 for (i = 0; i < allocator->count; i++) {
1224 GstV4l2MemoryGroup *group = allocator->groups[i];
1227 if (IS_QUEUED (group->buffer)) {
1228 UNSET_QUEUED (group->buffer);
1230 gst_v4l2_allocator_reset_group (allocator, group);
1232 for (n = 0; n < group->n_mem; n++)
1233 gst_memory_unref (group->mem[n]);
1238 GST_OBJECT_UNLOCK (allocator);
1242 gst_v4l2_allocator_qbuf (GstV4l2Allocator * allocator,
1243 GstV4l2MemoryGroup * group)
1245 GstV4l2Object *obj = allocator->obj;
1246 gboolean ret = TRUE;
1249 g_return_val_if_fail (g_atomic_int_get (&allocator->active), FALSE);
1252 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
1253 for (i = 0; i < group->n_mem; i++)
1254 group->planes[i].bytesused =
1255 gst_memory_get_sizes (group->mem[i], NULL, NULL);
1257 group->buffer.bytesused = gst_memory_get_sizes (group->mem[0], NULL, NULL);
1260 /* Ensure the memory will stay around and is RO */
1261 for (i = 0; i < group->n_mem; i++)
1262 gst_memory_ref (group->mem[i]);
1264 if (obj->ioctl (obj->video_fd, VIDIOC_QBUF, &group->buffer) < 0) {
1265 GST_ERROR_OBJECT (allocator, "failed queueing buffer %i: %s",
1266 group->buffer.index, g_strerror (errno));
1268 /* Release the memory, possibly making it RW again */
1269 for (i = 0; i < group->n_mem; i++)
1270 gst_memory_unref (group->mem[i]);
1273 if (IS_QUEUED (group->buffer)) {
1274 GST_DEBUG_OBJECT (allocator,
1275 "driver pretends buffer is queued even if queue failed");
1276 UNSET_QUEUED (group->buffer);
1281 GST_LOG_OBJECT (allocator, "queued buffer %i (flags 0x%X)",
1282 group->buffer.index, group->buffer.flags);
1284 if (!IS_QUEUED (group->buffer)) {
1285 GST_DEBUG_OBJECT (allocator,
1286 "driver pretends buffer is not queued even if queue succeeded");
1287 SET_QUEUED (group->buffer);
1295 gst_v4l2_allocator_dqbuf (GstV4l2Allocator * allocator,
1296 GstV4l2MemoryGroup ** group_out)
1298 GstV4l2Object *obj = allocator->obj;
1299 struct v4l2_buffer buffer = { 0 };
1300 struct v4l2_plane planes[VIDEO_MAX_PLANES] = { {0} };
1303 GstV4l2MemoryGroup *group = NULL;
1305 g_return_val_if_fail (g_atomic_int_get (&allocator->active), GST_FLOW_ERROR);
1307 buffer.type = obj->type;
1308 buffer.memory = allocator->memory;
1310 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
1311 buffer.length = obj->format.fmt.pix_mp.num_planes;
1312 buffer.m.planes = planes;
1315 if (obj->ioctl (obj->video_fd, VIDIOC_DQBUF, &buffer) < 0)
1318 group = allocator->groups[buffer.index];
1320 if (!IS_QUEUED (group->buffer)) {
1321 GST_ERROR_OBJECT (allocator,
1322 "buffer %i was not queued, this indicate a driver bug.", buffer.index);
1323 return GST_FLOW_ERROR;
1326 group->buffer = buffer;
1328 GST_LOG_OBJECT (allocator, "dequeued buffer %i (flags 0x%X)", buffer.index,
1331 if (IS_QUEUED (group->buffer)) {
1332 GST_DEBUG_OBJECT (allocator,
1333 "driver pretends buffer is queued even if dequeue succeeded");
1334 UNSET_QUEUED (group->buffer);
1337 if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
1338 group->buffer.m.planes = group->planes;
1339 memcpy (group->planes, buffer.m.planes, sizeof (planes));
1341 group->planes[0].bytesused = group->buffer.bytesused;
1342 group->planes[0].length = group->buffer.length;
1343 g_assert (sizeof (group->planes[0].m) == sizeof (group->buffer.m));
1344 memcpy (&group->planes[0].m, &group->buffer.m, sizeof (group->buffer.m));
1347 /* And update memory size */
1348 if (V4L2_TYPE_IS_OUTPUT (obj->type)) {
1349 gst_v4l2_allocator_reset_size (allocator, group);
1351 /* for capture, simply read the size */
1352 for (i = 0; i < group->n_mem; i++) {
1353 if (G_LIKELY (group->planes[i].bytesused <= group->mem[i]->maxsize))
1354 gst_memory_resize (group->mem[i], 0, group->planes[i].bytesused);
1356 GST_WARNING_OBJECT (allocator,
1357 "v4l2 provided buffer that is too big for the memory it was "
1358 "writing into. v4l2 claims %" G_GUINT32_FORMAT " bytes used but "
1359 "memory is only %" G_GSIZE_FORMAT "B. This is probably a driver "
1360 "bug.", group->planes[i].bytesused, group->mem[i]->maxsize);
1361 gst_memory_resize (group->mem[i], 0, group->mem[i]->maxsize);
1366 /* Release the memory, possibly making it RW again */
1367 for (i = 0; i < group->n_mem; i++)
1368 gst_memory_unref (group->mem[i]);
1374 if (errno == EPIPE) {
1375 GST_DEBUG_OBJECT (allocator, "broken pipe signals last buffer");
1376 return GST_FLOW_EOS;
1379 GST_ERROR_OBJECT (allocator, "failed dequeuing a %s buffer: %s",
1380 memory_type_to_str (allocator->memory), g_strerror (errno));
1384 GST_WARNING_OBJECT (allocator,
1385 "Non-blocking I/O has been selected using O_NONBLOCK and"
1386 " no buffer was in the outgoing queue.");
1389 GST_ERROR_OBJECT (allocator,
1390 "The buffer type is not supported, or the index is out of bounds, "
1391 "or no buffers have been allocated yet, or the userptr "
1392 "or length are invalid.");
1395 GST_ERROR_OBJECT (allocator,
1396 "insufficient memory to enqueue a user pointer buffer");
1399 GST_INFO_OBJECT (allocator,
1400 "VIDIOC_DQBUF failed due to an internal error."
1401 " Can also indicate temporary problems like signal loss."
1402 " Note the driver might dequeue an (empty) buffer despite"
1403 " returning an error, or even stop capturing.");
1404 /* have we de-queued a buffer ? */
1405 if (!IS_QUEUED (buffer)) {
1406 GST_DEBUG_OBJECT (allocator, "reenqueueing buffer");
1407 /* FIXME ... should we do something here? */
1411 GST_WARNING_OBJECT (allocator, "could not sync on a buffer on device");
1414 GST_WARNING_OBJECT (allocator,
1415 "Grabbing frame got interrupted unexpectedly. %d: %s.", errno,
1416 g_strerror (errno));
1420 return GST_FLOW_ERROR;
1424 gst_v4l2_allocator_reset_group (GstV4l2Allocator * allocator,
1425 GstV4l2MemoryGroup * group)
1427 switch (allocator->memory) {
1428 case V4L2_MEMORY_USERPTR:
1429 gst_v4l2_allocator_clear_userptr (allocator, group);
1431 case V4L2_MEMORY_DMABUF:
1432 gst_v4l2_allocator_clear_dmabufin (allocator, group);
1434 case V4L2_MEMORY_MMAP:
1437 g_assert_not_reached ();
1441 gst_v4l2_allocator_reset_size (allocator, group);