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 /* Check that provided size matches the format we have negotiation. Failing
274 * there usually means a driver of libv4l bug. */
275 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
278 for (i = 0; i < group->n_mem; i++) {
279 img_size = allocator->format.fmt.pix_mp.plane_fmt[i].sizeimage;
280 buf_size = group->planes[i].length;
281 if (buf_size < img_size)
282 goto buffer_too_short;
285 img_size = allocator->format.fmt.pix.sizeimage;
286 buf_size = group->buffer.length;
287 if (buf_size < img_size)
288 goto buffer_too_short;
291 /* We save non planar buffer information into the multi-planar plane array
292 * to avoid duplicating the code later */
293 if (!V4L2_TYPE_IS_MULTIPLANAR (format->type)) {
294 group->planes[0].bytesused = group->buffer.bytesused;
295 group->planes[0].length = group->buffer.length;
296 g_assert (sizeof (group->planes[0].m) == sizeof (group->buffer.m));
297 memcpy (&group->planes[0].m, &group->buffer.m, sizeof (group->buffer.m));
300 GST_LOG_OBJECT (allocator, "Got %s buffer", memory_type_to_str (memory));
301 GST_LOG_OBJECT (allocator, " index: %u", group->buffer.index);
302 GST_LOG_OBJECT (allocator, " type: %d", group->buffer.type);
303 GST_LOG_OBJECT (allocator, " flags: %08x", group->buffer.flags);
304 GST_LOG_OBJECT (allocator, " field: %d", group->buffer.field);
305 GST_LOG_OBJECT (allocator, " memory: %d", group->buffer.memory);
306 GST_LOG_OBJECT (allocator, " planes: %d", group->n_mem);
308 #ifndef GST_DISABLE_GST_DEBUG
309 if (memory == V4L2_MEMORY_MMAP) {
311 for (i = 0; i < group->n_mem; i++) {
312 GST_LOG_OBJECT (allocator, " [%u] bytesused: %u, length: %u", i,
313 group->planes[i].bytesused, group->planes[i].length);
314 GST_LOG_OBJECT (allocator, " [%u] MMAP offset: %u", i,
315 group->planes[i].m.mem_offset);
324 GST_ERROR ("error querying buffer %d: %s", index, g_strerror (errno));
329 GST_ERROR ("buffer size %" G_GSIZE_FORMAT
330 " is smaller then negotiated size %" G_GSIZE_FORMAT
331 ", this is usually the result of a bug in the v4l2 driver or libv4l.",
336 gst_v4l2_memory_group_free (group);
341 /*************************************/
342 /* GstV4lAllocator implementation */
343 /*************************************/
346 gst_v4l2_allocator_release (GstV4l2Allocator * allocator, GstV4l2Memory * mem)
348 GstV4l2MemoryGroup *group = mem->group;
350 GST_LOG_OBJECT (allocator, "plane %i of buffer %u released",
351 mem->plane, group->buffer.index);
353 switch (allocator->memory) {
354 case V4L2_MEMORY_DMABUF:
358 case V4L2_MEMORY_USERPTR:
365 /* When all memory are back, put the group back in the free queue */
366 if (g_atomic_int_dec_and_test (&group->mems_allocated)) {
367 GST_LOG_OBJECT (allocator, "buffer %u released", group->buffer.index);
368 gst_atomic_queue_push (allocator->free_queue, group);
369 g_signal_emit (allocator, gst_v4l2_allocator_signals[GROUP_RELEASED], 0);
372 /* Keep last, allocator may be freed after this call */
373 g_object_unref (allocator);
377 gst_v4l2_allocator_free (GstAllocator * gallocator, GstMemory * gmem)
379 GstV4l2Allocator *allocator = (GstV4l2Allocator *) gallocator;
380 GstV4l2Memory *mem = (GstV4l2Memory *) gmem;
381 GstV4l2MemoryGroup *group = mem->group;
383 GST_LOG_OBJECT (allocator, "freeing plane %i of buffer %u",
384 mem->plane, group->buffer.index);
386 switch (allocator->memory) {
387 case V4L2_MEMORY_MMAP:
389 v4l2_munmap (mem->data, group->planes[mem->plane].length);
390 } else if (group->planes[mem->plane].m.fd > 0) {
391 close (group->planes[mem->plane].m.fd);
403 gst_v4l2_allocator_dispose (GObject * obj)
405 GstV4l2Allocator *allocator = (GstV4l2Allocator *) obj;
408 GST_LOG_OBJECT (obj, "called");
410 for (i = 0; i < allocator->count; i++) {
411 GstV4l2MemoryGroup *group = allocator->groups[i];
412 allocator->groups[i] = NULL;
414 gst_v4l2_memory_group_free (group);
417 G_OBJECT_CLASS (parent_class)->dispose (obj);
421 gst_v4l2_allocator_finalize (GObject * obj)
423 GstV4l2Allocator *allocator = (GstV4l2Allocator *) obj;
425 GST_LOG_OBJECT (obj, "called");
427 v4l2_close (allocator->video_fd);
428 gst_atomic_queue_unref (allocator->free_queue);
430 G_OBJECT_CLASS (parent_class)->finalize (obj);
434 gst_v4l2_allocator_class_init (GstV4l2AllocatorClass * klass)
436 GObjectClass *object_class;
437 GstAllocatorClass *allocator_class;
439 allocator_class = (GstAllocatorClass *) klass;
440 object_class = (GObjectClass *) klass;
442 allocator_class->alloc = NULL;
443 allocator_class->free = gst_v4l2_allocator_free;
445 object_class->dispose = gst_v4l2_allocator_dispose;
446 object_class->finalize = gst_v4l2_allocator_finalize;
448 gst_v4l2_allocator_signals[GROUP_RELEASED] = g_signal_new ("group-released",
449 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
452 GST_DEBUG_CATEGORY_INIT (v4l2allocator_debug, "v4l2allocator", 0,
457 gst_v4l2_allocator_init (GstV4l2Allocator * allocator)
459 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
461 alloc->mem_type = GST_V4L2_MEMORY_TYPE;
462 alloc->mem_map = (GstMemoryMapFunction) _v4l2mem_map;
463 alloc->mem_unmap = (GstMemoryUnmapFunction) _v4l2mem_unmap;
464 alloc->mem_share = (GstMemoryShareFunction) _v4l2mem_share;
465 alloc->mem_is_span = (GstMemoryIsSpanFunction) _v4l2mem_is_span;
466 /* Use the default, fallback copy function */
468 allocator->free_queue = gst_atomic_queue_new (VIDEO_MAX_FRAME);
470 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
473 static GstV4l2MemoryGroup *
474 gst_v4l2_allocator_create_buf (GstV4l2Allocator * allocator)
476 struct v4l2_create_buffers bcreate = { 0 };
477 GstV4l2MemoryGroup *group = NULL;
479 GST_OBJECT_LOCK (allocator);
481 if (!g_atomic_int_get (&allocator->active))
484 bcreate.memory = allocator->memory;
485 bcreate.format = allocator->format;
488 if (!allocator->can_allocate)
491 if (v4l2_ioctl (allocator->video_fd, VIDIOC_CREATE_BUFS, &bcreate) < 0)
492 goto create_bufs_failed;
494 group = gst_v4l2_memory_group_new (allocator, bcreate.index);
497 allocator->groups[bcreate.index] = group;
502 GST_OBJECT_UNLOCK (allocator);
507 GST_WARNING_OBJECT (allocator, "error creating a new buffer: %s",
513 static GstV4l2MemoryGroup *
514 gst_v4l2_allocator_alloc (GstV4l2Allocator * allocator)
516 GstV4l2MemoryGroup *group;
518 if (!g_atomic_int_get (&allocator->active))
521 group = gst_atomic_queue_pop (allocator->free_queue);
524 if (allocator->can_allocate) {
525 group = gst_v4l2_allocator_create_buf (allocator);
527 /* Don't hammer on CREATE_BUFS */
529 allocator->can_allocate = FALSE;
537 gst_v4l2_allocator_reset_size (GstV4l2Allocator * allocator,
538 GstV4l2MemoryGroup * group)
541 gboolean imported = FALSE;
543 switch (allocator->memory) {
544 case V4L2_MEMORY_USERPTR:
545 case V4L2_MEMORY_DMABUF:
550 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
553 for (i = 0; i < group->n_mem; i++) {
554 size = allocator->format.fmt.pix_mp.plane_fmt[i].sizeimage;
557 group->mem[i]->maxsize = size;
559 gst_memory_resize (group->mem[i], 0, size);
563 size = allocator->format.fmt.pix.sizeimage;
566 group->mem[0]->maxsize = size;
568 gst_memory_resize (group->mem[0], 0, size);
573 _cleanup_failed_alloc (GstV4l2Allocator * allocator, GstV4l2MemoryGroup * group)
575 if (group->mems_allocated > 0) {
577 /* If one or more mmap worked, we need to unref the memory, otherwise
578 * they will keep a ref on the allocator and leak it. This will put back
579 * the group into the free_queue */
580 for (i = 0; i < group->n_mem; i++)
581 gst_memory_unref (group->mem[i]);
583 /* Otherwise, group has to be on free queue for _stop() to work */
584 gst_atomic_queue_push (allocator->free_queue, group);
589 gst_v4l2_allocator_new (GstObject * parent, gint video_fd,
590 struct v4l2_format *format)
592 GstV4l2Allocator *allocator;
594 gchar *name, *parent_name;
596 parent_name = gst_object_get_name (parent);
597 name = g_strconcat (parent_name, ":allocator", NULL);
598 g_free (parent_name);
600 allocator = g_object_new (GST_TYPE_V4L2_ALLOCATOR, "name", name, NULL);
603 /* Save everything */
604 allocator->video_fd = v4l2_dup (video_fd);
605 allocator->type = format->type;
606 allocator->format = *format;
608 GST_OBJECT_FLAG_SET (allocator, GST_OBJECT_FLAGS (parent));
614 gst_v4l2_allocator_start (GstV4l2Allocator * allocator, guint32 count,
617 struct v4l2_requestbuffers breq = { count, allocator->type, memory };
618 gboolean can_allocate;
621 g_return_val_if_fail (count != 0, 0);
623 GST_OBJECT_LOCK (allocator);
625 if (g_atomic_int_get (&allocator->active))
628 if (v4l2_ioctl (allocator->video_fd, VIDIOC_REQBUFS, &breq) < 0)
635 case V4L2_MEMORY_MMAP:
636 can_allocate = GST_V4L2_OBJECT_CAN_ALLOCATE (allocator, MMAP);
638 case V4L2_MEMORY_USERPTR:
639 can_allocate = GST_V4L2_OBJECT_CAN_ALLOCATE (allocator, USERPTR);
641 case V4L2_MEMORY_DMABUF:
642 can_allocate = GST_V4L2_OBJECT_CAN_ALLOCATE (allocator, DMABUF);
645 can_allocate = FALSE;
649 GST_DEBUG_OBJECT (allocator, "allocated %u %s buffers out of %u requested",
650 breq.count, memory_type_to_str (memory), count);
652 allocator->can_allocate = can_allocate;
653 allocator->count = breq.count;
654 allocator->memory = memory;
656 /* Create memory groups */
657 for (i = 0; i < allocator->count; i++) {
658 allocator->groups[i] = gst_v4l2_memory_group_new (allocator, i);
659 if (allocator->groups[i] == NULL)
662 gst_atomic_queue_push (allocator->free_queue, allocator->groups[i]);
665 g_atomic_int_set (&allocator->active, TRUE);
668 GST_OBJECT_UNLOCK (allocator);
673 GST_ERROR_OBJECT (allocator, "allocator already active");
678 GST_ERROR_OBJECT (allocator,
679 "error requesting %d buffers: %s", count, g_strerror (errno));
684 GST_ERROR_OBJECT (allocator, "Not enough memory to allocate buffers");
695 gst_v4l2_allocator_stop (GstV4l2Allocator * allocator)
697 struct v4l2_requestbuffers breq = { 0, allocator->type, allocator->memory };
699 GstV4l2Return ret = GST_V4L2_OK;
701 GST_DEBUG_OBJECT (allocator, "stop allocator");
703 GST_OBJECT_LOCK (allocator);
705 if (!g_atomic_int_get (&allocator->active))
708 if (gst_atomic_queue_length (allocator->free_queue) != allocator->count) {
709 GST_DEBUG_OBJECT (allocator, "allocator is still in use");
714 while (gst_atomic_queue_pop (allocator->free_queue)) {
718 for (i = 0; i < allocator->count; i++) {
719 GstV4l2MemoryGroup *group = allocator->groups[i];
720 allocator->groups[i] = NULL;
722 gst_v4l2_memory_group_free (group);
725 /* Not all drivers support rebufs(0), so warn only */
726 if (v4l2_ioctl (allocator->video_fd, VIDIOC_REQBUFS, &breq) < 0)
727 GST_WARNING_OBJECT (allocator,
728 "error releasing buffers buffers: %s", g_strerror (errno));
730 allocator->count = 0;
732 g_atomic_int_set (&allocator->active, FALSE);
735 GST_OBJECT_UNLOCK (allocator);
740 gst_v4l2_allocator_alloc_mmap (GstV4l2Allocator * allocator)
742 GstV4l2MemoryGroup *group;
745 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_MMAP, NULL);
747 group = gst_v4l2_allocator_alloc (allocator);
752 for (i = 0; i < group->n_mem; i++) {
753 if (group->mem[i] == NULL) {
755 data = v4l2_mmap (NULL, group->planes[i].length, PROT_READ | PROT_WRITE,
756 MAP_SHARED, allocator->video_fd, group->planes[i].m.mem_offset);
758 if (data == MAP_FAILED)
761 GST_LOG_OBJECT (allocator,
762 "mmap buffer length %d, data offset %d, plane %d",
763 group->planes[i].length, group->planes[i].data_offset, i);
765 group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
766 NULL, group->planes[i].length, 0, 0, group->planes[i].length, i,
769 /* Take back the allocator reference */
770 gst_object_ref (allocator);
773 group->mems_allocated++;
776 /* Ensure group size. Unlike GST, v4l2 have size (bytesused) initially set
777 * to 0. As length might be bigger then the expected size exposed in the
778 * format, we simply set bytesused initially and reset it here for
780 gst_v4l2_allocator_reset_size (allocator, group);
786 GST_ERROR_OBJECT (allocator, "Failed to mmap buffer: %s",
788 _cleanup_failed_alloc (allocator, group);
794 gst_v4l2_allocator_alloc_dmabuf (GstV4l2Allocator * allocator,
795 GstAllocator * dmabuf_allocator)
797 GstV4l2MemoryGroup *group;
800 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_MMAP, NULL);
802 group = gst_v4l2_allocator_alloc (allocator);
807 for (i = 0; i < group->n_mem; i++) {
812 if (group->mem[i] == NULL) {
813 struct v4l2_exportbuffer expbuf = { 0 };
815 expbuf.type = allocator->type;
816 expbuf.index = group->buffer.index;
818 expbuf.flags = O_CLOEXEC | O_RDWR;
820 if (v4l2_ioctl (allocator->video_fd, VIDIOC_EXPBUF, &expbuf) < 0)
823 GST_LOG_OBJECT (allocator, "exported DMABUF as fd %i plane %d",
826 group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
827 NULL, group->planes[i].length, 0, 0, group->planes[i].length, i,
828 NULL, expbuf.fd, group);
830 /* Take back the allocator reference */
831 gst_object_ref (allocator);
834 g_assert (gst_is_v4l2_memory (group->mem[i]));
835 mem = (GstV4l2Memory *) group->mem[i];
837 if ((dmafd = dup (mem->dmafd)) < 0)
840 dma_mem = gst_dmabuf_allocator_alloc (dmabuf_allocator, dmafd,
843 gst_mini_object_set_qdata (GST_MINI_OBJECT (dma_mem),
844 GST_V4L2_MEMORY_QUARK, mem, (GDestroyNotify) gst_memory_unref);
846 group->mem[i] = dma_mem;
847 group->mems_allocated++;
850 gst_v4l2_allocator_reset_size (allocator, group);
856 GST_ERROR_OBJECT (allocator, "Failed to export DMABUF: %s",
862 GST_ERROR_OBJECT (allocator, "Failed to dup DMABUF descriptor: %s",
868 _cleanup_failed_alloc (allocator, group);
874 gst_v4l2_allocator_clear_dmabufin (GstV4l2Allocator * allocator,
875 GstV4l2MemoryGroup * group)
880 g_return_if_fail (allocator->memory == V4L2_MEMORY_DMABUF);
882 for (i = 0; i < group->n_mem; i++) {
884 mem = (GstV4l2Memory *) group->mem[i];
886 GST_LOG_OBJECT (allocator, "clearing DMABUF import, fd %i plane %d",
893 mem->mem.maxsize = 0;
898 /* Update v4l2 structure */
899 group->planes[i].length = 0;
900 group->planes[i].bytesused = 0;
901 group->planes[i].m.fd = -1;
902 group->planes[i].data_offset = 0;
905 if (!V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
906 group->buffer.bytesused = 0;
907 group->buffer.length = 0;
908 group->buffer.m.fd = -1;
913 gst_v4l2_allocator_alloc_dmabufin (GstV4l2Allocator * allocator)
915 GstV4l2MemoryGroup *group;
918 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_DMABUF, NULL);
920 group = gst_v4l2_allocator_alloc (allocator);
925 GST_LOG_OBJECT (allocator, "allocating empty DMABUF import group");
927 for (i = 0; i < group->n_mem; i++) {
928 if (group->mem[i] == NULL) {
929 group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
930 NULL, 0, 0, 0, 0, i, NULL, -1, group);
932 /* Take back the allocator reference */
933 gst_object_ref (allocator);
936 group->mems_allocated++;
939 gst_v4l2_allocator_clear_dmabufin (allocator, group);
945 gst_v4l2_allocator_clear_userptr (GstV4l2Allocator * allocator,
946 GstV4l2MemoryGroup * group)
951 g_return_if_fail (allocator->memory == V4L2_MEMORY_USERPTR);
953 for (i = 0; i < group->n_mem; i++) {
954 mem = (GstV4l2Memory *) group->mem[i];
956 GST_LOG_OBJECT (allocator, "clearing USERPTR %p plane %d size %"
957 G_GSIZE_FORMAT, mem->data, i, mem->mem.size);
959 mem->mem.maxsize = 0;
963 group->planes[i].length = 0;
964 group->planes[i].bytesused = 0;
965 group->planes[i].m.userptr = 0;
968 if (!V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
969 group->buffer.bytesused = 0;
970 group->buffer.length = 0;
971 group->buffer.m.userptr = 0;
976 gst_v4l2_allocator_alloc_userptr (GstV4l2Allocator * allocator)
978 GstV4l2MemoryGroup *group;
981 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_USERPTR, NULL);
983 group = gst_v4l2_allocator_alloc (allocator);
988 GST_LOG_OBJECT (allocator, "allocating empty USERPTR group");
990 for (i = 0; i < group->n_mem; i++) {
992 if (group->mem[i] == NULL) {
993 group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
994 NULL, 0, 0, 0, 0, i, NULL, -1, group);
996 /* Take back the allocator reference */
997 gst_object_ref (allocator);
1000 group->mems_allocated++;
1003 gst_v4l2_allocator_clear_userptr (allocator, group);
1009 gst_v4l2_allocator_import_dmabuf (GstV4l2Allocator * allocator,
1010 GstV4l2MemoryGroup * group, gint n_mem, GstMemory ** dma_mem)
1015 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_DMABUF, FALSE);
1017 if (group->n_mem != n_mem)
1018 goto n_mem_missmatch;
1020 for (i = 0; i < group->n_mem; i++) {
1022 gsize size, offset, maxsize;
1024 if (!gst_is_dmabuf_memory (dma_mem[i]))
1027 size = gst_memory_get_sizes (dma_mem[i], &offset, &maxsize);
1029 if ((dmafd = dup (gst_dmabuf_memory_get_fd (dma_mem[i]))) < 0)
1032 GST_LOG_OBJECT (allocator, "imported DMABUF as fd %i plane %d", dmafd, i);
1034 mem = (GstV4l2Memory *) group->mem[i];
1037 mem->mem.maxsize = maxsize;
1038 mem->mem.offset = offset;
1039 mem->mem.size = size;
1042 /* Update v4l2 structure */
1043 group->planes[i].length = maxsize;
1044 group->planes[i].bytesused = size;
1045 group->planes[i].m.fd = dmafd;
1046 group->planes[i].data_offset = offset;
1049 /* Copy into buffer structure if not using planes */
1050 if (!V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1051 group->buffer.bytesused = group->planes[0].bytesused;
1052 group->buffer.length = group->planes[0].length;
1053 group->buffer.m.fd = group->planes[0].m.userptr;
1055 group->buffer.length = group->n_mem;
1062 GST_ERROR_OBJECT (allocator, "Got %i dmabuf but needed %i", n_mem,
1068 GST_ERROR_OBJECT (allocator, "Memory %i is not of DMABUF", i);
1073 GST_ERROR_OBJECT (allocator, "Failed to dup DMABUF descriptor: %s",
1074 g_strerror (errno));
1080 gst_v4l2_allocator_import_userptr (GstV4l2Allocator * allocator,
1081 GstV4l2MemoryGroup * group, gsize img_size, int n_planes,
1082 gpointer * data, gsize * offset)
1087 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_USERPTR, FALSE);
1089 /* TODO Support passing N plane from 1 memory to MPLANE v4l2 format */
1090 if (n_planes != group->n_mem)
1091 goto n_mem_missmatch;
1093 for (i = 0; i < group->n_mem; i++) {
1094 gsize size, maxsize;
1096 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1097 struct v4l2_pix_format_mplane *pix = &allocator->format.fmt.pix_mp;
1098 maxsize = pix->plane_fmt[i].sizeimage;
1100 maxsize = allocator->format.fmt.pix.sizeimage;
1103 if ((i + 1) == n_planes) {
1104 size = img_size - offset[i];
1106 size = offset[i + 1] - offset[i];
1109 g_assert (size <= img_size);
1111 GST_LOG_OBJECT (allocator, "imported USERPTR %p plane %d size %"
1112 G_GSIZE_FORMAT, data[i], i, size);
1114 mem = (GstV4l2Memory *) group->mem[i];
1116 mem->mem.maxsize = maxsize;
1117 mem->mem.size = size;
1118 mem->data = data[i];
1120 group->planes[i].length = maxsize;
1121 group->planes[i].bytesused = size;
1122 group->planes[i].m.userptr = (unsigned long) data[i];
1123 group->planes[i].data_offset = 0;
1126 /* Copy into buffer structure if not using planes */
1127 if (!V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1128 group->buffer.bytesused = group->planes[0].bytesused;
1129 group->buffer.length = group->planes[0].length;
1130 group->buffer.m.userptr = group->planes[0].m.userptr;
1132 group->buffer.length = group->n_mem;
1139 GST_ERROR_OBJECT (allocator, "Got %i userptr plane while driver need %i",
1140 n_planes, group->n_mem);
1146 gst_v4l2_allocator_flush (GstV4l2Allocator * allocator)
1150 GST_OBJECT_LOCK (allocator);
1152 if (!g_atomic_int_get (&allocator->active))
1155 for (i = 0; i < allocator->count; i++) {
1156 GstV4l2MemoryGroup *group = allocator->groups[i];
1159 if (IS_QUEUED (group->buffer)) {
1160 UNSET_QUEUED (group->buffer);
1162 gst_v4l2_allocator_reset_group (allocator, group);
1164 for (n = 0; n < group->n_mem; n++)
1165 gst_memory_unref (group->mem[n]);
1170 GST_OBJECT_UNLOCK (allocator);
1174 gst_v4l2_allocator_qbuf (GstV4l2Allocator * allocator,
1175 GstV4l2MemoryGroup * group)
1177 gboolean ret = TRUE;
1180 g_return_val_if_fail (g_atomic_int_get (&allocator->active), FALSE);
1183 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1184 for (i = 0; i < group->n_mem; i++)
1185 group->planes[i].bytesused =
1186 gst_memory_get_sizes (group->mem[i], NULL, NULL);
1188 group->buffer.bytesused = gst_memory_get_sizes (group->mem[0], NULL, NULL);
1191 if (v4l2_ioctl (allocator->video_fd, VIDIOC_QBUF, &group->buffer) < 0) {
1192 GST_ERROR_OBJECT (allocator, "failed queing buffer %i: %s",
1193 group->buffer.index, g_strerror (errno));
1195 if (IS_QUEUED (group->buffer)) {
1196 GST_DEBUG_OBJECT (allocator,
1197 "driver pretends buffer is queued even if queue failed");
1198 UNSET_QUEUED (group->buffer);
1203 GST_LOG_OBJECT (allocator, "queued buffer %i (flags 0x%X)",
1204 group->buffer.index, group->buffer.flags);
1206 if (!IS_QUEUED (group->buffer)) {
1207 GST_DEBUG_OBJECT (allocator,
1208 "driver pretends buffer is not queued even if queue succeeded");
1209 SET_QUEUED (group->buffer);
1212 /* Ensure the memory will stay around and is RO */
1213 for (i = 0; i < group->n_mem; i++)
1214 gst_memory_ref (group->mem[i]);
1220 GstV4l2MemoryGroup *
1221 gst_v4l2_allocator_dqbuf (GstV4l2Allocator * allocator)
1223 struct v4l2_buffer buffer = { 0 };
1224 struct v4l2_plane planes[VIDEO_MAX_PLANES] = { {0} };
1227 GstV4l2MemoryGroup *group = NULL;
1229 g_return_val_if_fail (g_atomic_int_get (&allocator->active), FALSE);
1231 buffer.type = allocator->type;
1232 buffer.memory = allocator->memory;
1234 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1235 buffer.length = allocator->format.fmt.pix_mp.num_planes;
1236 buffer.m.planes = planes;
1239 if (v4l2_ioctl (allocator->video_fd, VIDIOC_DQBUF, &buffer) < 0)
1242 group = allocator->groups[buffer.index];
1243 group->buffer = buffer;
1245 GST_LOG_OBJECT (allocator, "dequeued buffer %i (flags 0x%X)", buffer.index,
1248 if (IS_QUEUED (group->buffer)) {
1249 GST_DEBUG_OBJECT (allocator,
1250 "driver pretends buffer is queued even if dequeue succeeded");
1251 UNSET_QUEUED (group->buffer);
1254 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1255 group->buffer.m.planes = group->planes;
1256 memcpy (group->planes, buffer.m.planes, sizeof (planes));
1258 group->planes[0].bytesused = group->buffer.bytesused;
1259 group->planes[0].length = group->buffer.length;
1260 g_assert (sizeof (group->planes[0].m) == sizeof (group->buffer.m));
1261 memcpy (&group->planes[0].m, &group->buffer.m, sizeof (group->buffer.m));
1264 /* And update memory size */
1265 if (V4L2_TYPE_IS_OUTPUT (allocator->type)) {
1266 gst_v4l2_allocator_reset_size (allocator, group);
1268 /* for capture, simply read the size */
1269 for (i = 0; i < group->n_mem; i++) {
1270 gst_memory_resize (group->mem[i], 0, group->planes[i].bytesused);
1274 /* Release the memory, possibly making it RW again */
1275 for (i = 0; i < group->n_mem; i++)
1276 gst_memory_unref (group->mem[i]);
1281 GST_ERROR_OBJECT (allocator, "failed dequeuing a %s buffer: %s",
1282 memory_type_to_str (allocator->memory), g_strerror (errno));
1286 GST_WARNING_OBJECT (allocator,
1287 "Non-blocking I/O has been selected using O_NONBLOCK and"
1288 " no buffer was in the outgoing queue.");
1291 GST_ERROR_OBJECT (allocator,
1292 "The buffer type is not supported, or the index is out of bounds, "
1293 "or no buffers have been allocated yet, or the userptr "
1294 "or length are invalid.");
1297 GST_ERROR_OBJECT (allocator,
1298 "insufficient memory to enqueue a user pointer buffer");
1301 GST_INFO_OBJECT (allocator,
1302 "VIDIOC_DQBUF failed due to an internal error."
1303 " Can also indicate temporary problems like signal loss."
1304 " Note the driver might dequeue an (empty) buffer despite"
1305 " returning an error, or even stop capturing.");
1306 /* have we de-queued a buffer ? */
1307 if (!IS_QUEUED (buffer)) {
1308 GST_DEBUG_OBJECT (allocator, "reenqueing buffer");
1309 /* FIXME ... should we do something here? */
1313 GST_WARNING_OBJECT (allocator, "could not sync on a buffer on device");
1316 GST_WARNING_OBJECT (allocator,
1317 "Grabbing frame got interrupted unexpectedly. %d: %s.", errno,
1318 g_strerror (errno));
1326 gst_v4l2_allocator_reset_group (GstV4l2Allocator * allocator,
1327 GstV4l2MemoryGroup * group)
1329 switch (allocator->memory) {
1330 case V4L2_MEMORY_USERPTR:
1331 gst_v4l2_allocator_clear_userptr (allocator, group);
1333 case V4L2_MEMORY_DMABUF:
1334 gst_v4l2_allocator_clear_dmabufin (allocator, group);
1336 case V4L2_MEMORY_MMAP:
1339 g_assert_not_reached ();
1343 gst_v4l2_allocator_reset_size (allocator, group);
1347 gst_v4l2_allocator_num_allocated (GstV4l2Allocator * allocator)
1349 gsize num_allocated;
1351 GST_OBJECT_LOCK (allocator);
1353 num_allocated = allocator->count;
1355 GST_OBJECT_UNLOCK (allocator);
1357 return num_allocated;