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.
24 #include "ext/videodev2.h"
25 #include "gstv4l2allocator.h"
26 #include "v4l2_calls.h"
28 #include <gst/allocators/gstdmabuf.h>
33 #include <sys/types.h>
36 #define GST_V4L2_MEMORY_TYPE "V4l2Memory"
38 #define gst_v4l2_allocator_parent_class parent_class
39 G_DEFINE_TYPE (GstV4l2Allocator, gst_v4l2_allocator, GST_TYPE_ALLOCATOR);
41 GST_DEBUG_CATEGORY_STATIC (v4l2allocator_debug);
42 #define GST_CAT_DEFAULT v4l2allocator_debug
44 #define UNSET_QUEUED(buffer) \
45 ((buffer).flags &= ~(V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
47 #define SET_QUEUED(buffer) ((buffer).flags |= V4L2_BUF_FLAG_QUEUED)
49 #define IS_QUEUED(buffer) \
50 ((buffer).flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
58 static guint gst_v4l2_allocator_signals[LAST_SIGNAL] = { 0 };
60 static void gst_v4l2_allocator_release (GstV4l2Allocator * allocator,
64 memory_type_to_str (guint32 memory)
67 case V4L2_MEMORY_MMAP:
69 case V4L2_MEMORY_USERPTR:
71 case V4L2_MEMORY_DMABUF:
78 /*************************************/
79 /* GstV4lMemory implementation */
80 /*************************************/
83 _v4l2mem_map (GstV4l2Memory * mem, gsize maxsize, GstMapFlags flags)
87 switch (mem->group->buffer.memory) {
88 case V4L2_MEMORY_MMAP:
89 case V4L2_MEMORY_USERPTR:
92 case V4L2_MEMORY_DMABUF:
93 /* v4l2 dmabuf memory are not shared with downstream */
94 g_assert_not_reached ();
97 GST_WARNING ("Unknown memory type %i", mem->group->buffer.memory);
104 _v4l2mem_unmap (GstV4l2Memory * mem)
106 gboolean ret = FALSE;
108 switch (mem->group->buffer.memory) {
109 case V4L2_MEMORY_MMAP:
110 case V4L2_MEMORY_USERPTR:
113 case V4L2_MEMORY_DMABUF:
114 /* v4l2 dmabuf memory are not share with downstream */
115 g_assert_not_reached ();
118 GST_WARNING ("Unknown memory type %i", mem->group->buffer.memory);
125 _v4l2mem_dispose (GstV4l2Memory * mem)
127 GstV4l2Allocator *allocator = (GstV4l2Allocator *) mem->mem.allocator;
128 GstV4l2MemoryGroup *group = mem->group;
131 if (group->mem[mem->plane]) {
132 /* We may have a dmabuf, replace it with returned original memory */
133 group->mem[mem->plane] = gst_memory_ref ((GstMemory *) mem);
134 gst_v4l2_allocator_release (allocator, mem);
137 gst_object_ref (allocator);
145 _v4l2mem_free (GstV4l2Memory * mem)
149 g_slice_free (GstV4l2Memory, mem);
152 static inline GstV4l2Memory *
153 _v4l2mem_new (GstMemoryFlags flags, GstAllocator * allocator,
154 GstMemory * parent, gsize maxsize, gsize align, gsize offset, gsize size,
155 gint plane, gpointer data, int dmafd, GstV4l2MemoryGroup * group)
159 mem = g_slice_new0 (GstV4l2Memory);
160 gst_memory_init (GST_MEMORY_CAST (mem),
161 flags, allocator, parent, maxsize, align, offset, size);
164 mem->mem.mini_object.dispose =
165 (GstMiniObjectDisposeFunction) _v4l2mem_dispose;
175 static GstV4l2Memory *
176 _v4l2mem_share (GstV4l2Memory * mem, gssize offset, gsize size)
181 /* find the real parent */
182 if ((parent = mem->mem.parent) == NULL)
183 parent = (GstMemory *) mem;
186 size = mem->mem.size - offset;
188 /* the shared memory is always readonly */
189 sub = _v4l2mem_new (GST_MINI_OBJECT_FLAGS (parent) |
190 GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->mem.allocator, parent,
191 mem->mem.maxsize, mem->mem.align, offset, size, mem->plane, mem->data,
198 _v4l2mem_is_span (GstV4l2Memory * mem1, GstV4l2Memory * mem2, gsize * offset)
201 *offset = mem1->mem.offset - mem1->mem.parent->offset;
203 /* and memory is contiguous */
204 return mem1->mem.offset + mem1->mem.size == mem2->mem.offset;
208 _v4l2mem_parent_to_dmabuf (GstV4l2Memory * mem, GstMemory * dma_mem)
210 gst_memory_lock (&mem->mem, GST_LOCK_FLAG_EXCLUSIVE);
211 dma_mem->parent = gst_memory_ref (&mem->mem);
215 gst_is_v4l2_memory (GstMemory * mem)
217 return gst_memory_is_type (mem, GST_V4L2_MEMORY_TYPE);
221 /*************************************/
222 /* GstV4l2MemoryGroup implementation */
223 /*************************************/
226 gst_v4l2_memory_group_free (GstV4l2MemoryGroup * group)
230 for (i = 0; i < group->n_mem; i++) {
231 GstMemory *mem = group->mem[i];
232 group->mem[i] = NULL;
234 gst_memory_unref (mem);
237 g_slice_free (GstV4l2MemoryGroup, group);
240 static GstV4l2MemoryGroup *
241 gst_v4l2_memory_group_new (GstV4l2Allocator * allocator, guint32 index)
243 gint video_fd = allocator->video_fd;
244 guint32 memory = allocator->memory;
245 struct v4l2_format *format = &allocator->format;
246 GstV4l2MemoryGroup *group;
247 gsize img_size, buf_size;
249 group = g_slice_new0 (GstV4l2MemoryGroup);
251 group->buffer.type = format->type;
252 group->buffer.index = index;
253 group->buffer.memory = memory;
255 if (V4L2_TYPE_IS_MULTIPLANAR (format->type)) {
256 group->n_mem = group->buffer.length = format->fmt.pix_mp.num_planes;
257 group->buffer.m.planes = group->planes;
262 if (v4l2_ioctl (video_fd, VIDIOC_QUERYBUF, &group->buffer) < 0)
263 goto querybuf_failed;
265 /* Check that provided size matches the format we have negotiation. Failing
266 * there usually means a driver of libv4l bug. */
267 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
270 for (i = 0; i < group->n_mem; i++) {
271 img_size = allocator->format.fmt.pix_mp.plane_fmt[i].sizeimage;
272 buf_size = group->planes[i].length;
273 if (buf_size < img_size)
274 goto buffer_too_short;
277 img_size = allocator->format.fmt.pix.sizeimage;
278 buf_size = group->buffer.length;
279 if (buf_size < img_size)
280 goto buffer_too_short;
283 /* We save non planar buffer information into the multi-planar plane array
284 * to avoid duplicating the code later */
285 if (!V4L2_TYPE_IS_MULTIPLANAR (format->type)) {
286 group->planes[0].bytesused = group->buffer.bytesused;
287 group->planes[0].length = group->buffer.length;
288 g_assert (sizeof (group->planes[0].m) == sizeof (group->buffer.m));
289 memcpy (&group->planes[0].m, &group->buffer.m, sizeof (group->buffer.m));
292 GST_LOG_OBJECT (allocator, "Got %s buffer", memory_type_to_str (memory));
293 GST_LOG_OBJECT (allocator, " index: %u", group->buffer.index);
294 GST_LOG_OBJECT (allocator, " type: %d", group->buffer.type);
295 GST_LOG_OBJECT (allocator, " flags: %08x", group->buffer.flags);
296 GST_LOG_OBJECT (allocator, " field: %d", group->buffer.field);
297 GST_LOG_OBJECT (allocator, " memory: %d", group->buffer.memory);
298 GST_LOG_OBJECT (allocator, " planes: %d", group->n_mem);
300 #ifndef GST_DISABLE_GST_DEBUG
301 if (memory == V4L2_MEMORY_MMAP) {
303 for (i = 0; i < group->n_mem; i++) {
304 GST_LOG_OBJECT (allocator, " [%u] bytesused: %u, length: %u", i,
305 group->planes[i].bytesused, group->planes[i].length);
306 GST_LOG_OBJECT (allocator, " [%u] MMAP offset: %u", i,
307 group->planes[i].m.mem_offset);
316 GST_ERROR ("error querying buffer %d: %s", index, g_strerror (errno));
321 GST_ERROR ("buffer size %" G_GSIZE_FORMAT
322 " is smaller then negotiated size %" G_GSIZE_FORMAT
323 ", this is usually the result of a bug in the v4l2 driver or libv4l.",
328 gst_v4l2_memory_group_free (group);
333 /*************************************/
334 /* GstV4lAllocator implementation */
335 /*************************************/
338 gst_v4l2_allocator_release (GstV4l2Allocator * allocator, GstV4l2Memory * mem)
340 GstV4l2MemoryGroup *group = mem->group;
342 GST_LOG_OBJECT (allocator, "plane %i of buffer %u released",
343 mem->plane, group->buffer.index);
345 switch (allocator->memory) {
346 case V4L2_MEMORY_DMABUF:
350 case V4L2_MEMORY_USERPTR:
357 /* When all memory are back, put the group back in the free queue */
358 if (g_atomic_int_dec_and_test (&group->mems_allocated)) {
359 GST_LOG_OBJECT (allocator, "buffer %u released", group->buffer.index);
360 gst_atomic_queue_push (allocator->free_queue, group);
361 g_signal_emit (allocator, gst_v4l2_allocator_signals[GROUP_RELEASED], 0);
364 /* Keep last, allocator may be freed after this call */
365 g_object_unref (allocator);
369 gst_v4l2_allocator_free (GstAllocator * gallocator, GstMemory * gmem)
371 GstV4l2Allocator *allocator = (GstV4l2Allocator *) gallocator;
372 GstV4l2Memory *mem = (GstV4l2Memory *) gmem;
373 GstV4l2MemoryGroup *group = mem->group;
375 GST_LOG_OBJECT (allocator, "freeing plane %i of buffer %u",
376 mem->plane, group->buffer.index);
378 switch (allocator->memory) {
379 case V4L2_MEMORY_MMAP:
381 v4l2_munmap (mem->data, group->planes[mem->plane].length);
382 } else if (group->planes[mem->plane].m.fd > 0) {
383 close (group->planes[mem->plane].m.fd);
395 gst_v4l2_allocator_dispose (GObject * obj)
397 GstV4l2Allocator *allocator = (GstV4l2Allocator *) obj;
400 GST_LOG_OBJECT (obj, "called");
402 for (i = 0; i < allocator->count; i++) {
403 GstV4l2MemoryGroup *group = allocator->groups[i];
404 allocator->groups[i] = NULL;
406 gst_v4l2_memory_group_free (group);
409 G_OBJECT_CLASS (parent_class)->dispose (obj);
413 gst_v4l2_allocator_finalize (GObject * obj)
415 GstV4l2Allocator *allocator = (GstV4l2Allocator *) obj;
417 GST_LOG_OBJECT (obj, "called");
419 v4l2_close (allocator->video_fd);
420 gst_atomic_queue_unref (allocator->free_queue);
422 G_OBJECT_CLASS (parent_class)->finalize (obj);
426 gst_v4l2_allocator_class_init (GstV4l2AllocatorClass * klass)
428 GObjectClass *object_class;
429 GstAllocatorClass *allocator_class;
431 allocator_class = (GstAllocatorClass *) klass;
432 object_class = (GObjectClass *) klass;
434 allocator_class->alloc = NULL;
435 allocator_class->free = gst_v4l2_allocator_free;
437 object_class->dispose = gst_v4l2_allocator_dispose;
438 object_class->finalize = gst_v4l2_allocator_finalize;
440 gst_v4l2_allocator_signals[GROUP_RELEASED] = g_signal_new ("group-released",
441 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
444 GST_DEBUG_CATEGORY_INIT (v4l2allocator_debug, "v4l2allocator", 0,
449 gst_v4l2_allocator_init (GstV4l2Allocator * allocator)
451 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
453 alloc->mem_type = GST_V4L2_MEMORY_TYPE;
454 alloc->mem_map = (GstMemoryMapFunction) _v4l2mem_map;
455 alloc->mem_unmap = (GstMemoryUnmapFunction) _v4l2mem_unmap;
456 alloc->mem_share = (GstMemoryShareFunction) _v4l2mem_share;
457 alloc->mem_is_span = (GstMemoryIsSpanFunction) _v4l2mem_is_span;
458 /* Use the default, fallback copy function */
460 allocator->free_queue = gst_atomic_queue_new (VIDEO_MAX_FRAME);
462 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
465 #define GST_V4L2_ALLOCATOR_PROBE(obj,type) \
466 gst_v4l2_allocator_probe ((obj), V4L2_MEMORY_ ## type, \
467 GST_V4L2_ALLOCATOR_FLAG_ ## type ## _REQBUFS, \
468 GST_V4L2_ALLOCATOR_FLAG_ ## type ## _CREATE_BUFS)
470 gst_v4l2_allocator_probe (GstV4l2Allocator * allocator, guint32 memory,
471 guint32 breq_flag, guint32 bcreate_flag)
473 struct v4l2_requestbuffers breq = { 0 };
476 breq.type = allocator->type;
478 breq.memory = memory;
480 if (v4l2_ioctl (allocator->video_fd, VIDIOC_REQBUFS, &breq) == 0) {
481 struct v4l2_create_buffers bcreate = { 0 };
485 bcreate.memory = V4L2_MEMORY_MMAP;
486 bcreate.format = allocator->format;
488 if ((v4l2_ioctl (allocator->video_fd, VIDIOC_CREATE_BUFS, &bcreate) == 0))
489 flags |= bcreate_flag;
495 static GstV4l2MemoryGroup *
496 gst_v4l2_allocator_create_buf (GstV4l2Allocator * allocator)
498 struct v4l2_create_buffers bcreate = { 0 };
499 GstV4l2MemoryGroup *group = NULL;
501 GST_OBJECT_LOCK (allocator);
503 if (!allocator->active)
506 bcreate.memory = allocator->memory;
507 bcreate.format = allocator->format;
510 if (!allocator->can_allocate)
513 if (v4l2_ioctl (allocator->video_fd, VIDIOC_CREATE_BUFS, &bcreate) < 0)
514 goto create_bufs_failed;
516 group = gst_v4l2_memory_group_new (allocator, bcreate.index);
519 allocator->groups[bcreate.index] = group;
524 GST_OBJECT_UNLOCK (allocator);
529 GST_WARNING_OBJECT (allocator, "error creating a new buffer: %s",
535 static GstV4l2MemoryGroup *
536 gst_v4l2_allocator_alloc (GstV4l2Allocator * allocator)
538 GstV4l2MemoryGroup *group;
540 if (!g_atomic_int_get (&allocator->active))
543 group = gst_atomic_queue_pop (allocator->free_queue);
546 if (allocator->can_allocate) {
547 group = gst_v4l2_allocator_create_buf (allocator);
549 /* Don't hammer on CREATE_BUFS */
551 allocator->can_allocate = FALSE;
559 gst_v4l2_allocator_reset_size (GstV4l2Allocator * allocator,
560 GstV4l2MemoryGroup * group)
563 gboolean imported = FALSE;
565 switch (allocator->memory) {
566 case V4L2_MEMORY_USERPTR:
567 case V4L2_MEMORY_DMABUF:
572 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
575 for (i = 0; i < group->n_mem; i++) {
576 size = allocator->format.fmt.pix_mp.plane_fmt[i].sizeimage;
579 group->mem[i]->maxsize = size;
581 gst_memory_resize (group->mem[i], 0, size);
585 size = allocator->format.fmt.pix.sizeimage;
588 group->mem[0]->maxsize = size;
590 gst_memory_resize (group->mem[0], 0, size);
595 _cleanup_failed_alloc (GstV4l2Allocator * allocator, GstV4l2MemoryGroup * group)
597 if (group->mems_allocated > 0) {
599 /* If one or more mmap worked, we need to unref the memory, otherwise
600 * they will keep a ref on the allocator and leak it. This will put back
601 * the group into the free_queue */
602 for (i = 0; i < group->n_mem; i++)
603 gst_memory_unref (group->mem[i]);
605 /* Otherwise, group has to be on free queue for _stop() to work */
606 gst_atomic_queue_push (allocator->free_queue, group);
613 gst_v4l2_allocator_new (GstObject * parent, gint video_fd,
614 struct v4l2_format *format)
616 GstV4l2Allocator *allocator;
618 gchar *name, *parent_name;
620 parent_name = gst_object_get_name (parent);
621 name = g_strconcat (parent_name, ":allocator", NULL);
622 g_free (parent_name);
624 allocator = g_object_new (GST_TYPE_V4L2_ALLOCATOR, "name", name, NULL);
627 /* Save everything */
628 allocator->video_fd = v4l2_dup (video_fd);
629 allocator->type = format->type;
630 allocator->format = *format;
632 flags |= GST_V4L2_ALLOCATOR_PROBE (allocator, MMAP);
633 flags |= GST_V4L2_ALLOCATOR_PROBE (allocator, USERPTR);
634 flags |= GST_V4L2_ALLOCATOR_PROBE (allocator, DMABUF);
636 GST_OBJECT_FLAG_SET (allocator, flags);
645 GST_ERROR_OBJECT (allocator,
646 "No memory model supported by GStreamer for this device");
647 g_object_unref (allocator);
653 gst_v4l2_allocator_start (GstV4l2Allocator * allocator, guint32 count,
656 struct v4l2_requestbuffers breq = { count, allocator->type, memory };
657 gboolean can_allocate;
660 g_return_val_if_fail (count != 0, 0);
662 GST_OBJECT_LOCK (allocator);
664 if (allocator->active)
667 if (v4l2_ioctl (allocator->video_fd, VIDIOC_REQBUFS, &breq) < 0)
674 case V4L2_MEMORY_MMAP:
675 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (allocator, MMAP);
677 case V4L2_MEMORY_USERPTR:
678 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (allocator, USERPTR);
680 case V4L2_MEMORY_DMABUF:
681 can_allocate = GST_V4L2_ALLOCATOR_CAN_ALLOCATE (allocator, DMABUF);
684 can_allocate = FALSE;
688 GST_DEBUG_OBJECT (allocator, "allocated %u %s buffers out of %u requested",
689 breq.count, memory_type_to_str (memory), count);
691 allocator->can_allocate = can_allocate;
692 allocator->count = breq.count;
693 allocator->memory = memory;
695 /* Create memory groups */
696 for (i = 0; i < allocator->count; i++) {
697 allocator->groups[i] = gst_v4l2_memory_group_new (allocator, i);
698 if (allocator->groups[i] == NULL)
701 gst_atomic_queue_push (allocator->free_queue, allocator->groups[i]);
704 g_atomic_int_set (&allocator->active, TRUE);
707 GST_OBJECT_UNLOCK (allocator);
712 GST_ERROR_OBJECT (allocator,
713 "error requesting %d buffers: %s", count, g_strerror (errno));
718 GST_ERROR_OBJECT (allocator,
719 "error requesting %d buffers: %s", count, g_strerror (errno));
724 GST_ERROR_OBJECT (allocator, "Not enough memory to allocate buffers");
735 gst_v4l2_allocator_stop (GstV4l2Allocator * allocator)
737 struct v4l2_requestbuffers breq = { 0, allocator->type, allocator->memory };
739 GstV4l2Return ret = GST_V4L2_OK;
741 GST_DEBUG_OBJECT (allocator, "stop allocator");
743 GST_OBJECT_LOCK (allocator);
745 if (!allocator->active)
748 if (gst_atomic_queue_length (allocator->free_queue) != allocator->count) {
749 GST_DEBUG_OBJECT (allocator, "allocator is still in use");
754 while (gst_atomic_queue_pop (allocator->free_queue)) {
758 for (i = 0; i < allocator->count; i++) {
759 GstV4l2MemoryGroup *group = allocator->groups[i];
760 allocator->groups[i] = NULL;
762 gst_v4l2_memory_group_free (group);
765 if (v4l2_ioctl (allocator->video_fd, VIDIOC_REQBUFS, &breq) < 0)
768 g_atomic_int_set (&allocator->active, FALSE);
771 GST_OBJECT_UNLOCK (allocator);
776 GST_ERROR_OBJECT (allocator,
777 "error releasing buffers buffers: %s", g_strerror (errno));
778 ret = GST_V4L2_ERROR;
784 gst_v4l2_allocator_alloc_mmap (GstV4l2Allocator * allocator)
786 GstV4l2MemoryGroup *group;
789 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_MMAP, NULL);
791 group = gst_v4l2_allocator_alloc (allocator);
796 for (i = 0; i < group->n_mem; i++) {
797 if (group->mem[i] == NULL) {
799 data = v4l2_mmap (NULL, group->planes[i].length, PROT_READ | PROT_WRITE,
800 MAP_SHARED, allocator->video_fd, group->planes[i].m.mem_offset);
802 if (data == MAP_FAILED)
805 GST_LOG_OBJECT (allocator,
806 "mmap buffer length %d, data offset %d, plane %d",
807 group->planes[i].length, group->planes[i].data_offset, i);
809 group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
810 NULL, group->planes[i].length, 0, 0, group->planes[i].length, i,
813 /* Take back the allocator reference */
814 gst_object_ref (allocator);
817 group->mems_allocated++;
820 /* Ensure group size. Unlike GST, v4l2 have size (bytesused) initially set
821 * to 0. As length might be bigger then the expected size exposed in the
822 * format, we simply set bytesused initially and reset it here for
824 gst_v4l2_allocator_reset_size (allocator, group);
830 GST_ERROR_OBJECT (allocator, "Failed to mmap buffer: %s",
832 _cleanup_failed_alloc (allocator, group);
838 gst_v4l2_allocator_alloc_dmabuf (GstV4l2Allocator * allocator,
839 GstAllocator * dmabuf_allocator)
841 GstV4l2MemoryGroup *group;
844 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_MMAP, NULL);
846 group = gst_v4l2_allocator_alloc (allocator);
851 for (i = 0; i < group->n_mem; i++) {
856 if (group->mem[i] == NULL) {
857 struct v4l2_exportbuffer expbuf = { 0 };
859 expbuf.type = allocator->type;
860 expbuf.index = group->buffer.index;
862 expbuf.flags = O_CLOEXEC | O_RDWR;
864 if (v4l2_ioctl (allocator->video_fd, VIDIOC_EXPBUF, &expbuf) < 0)
867 GST_LOG_OBJECT (allocator, "exported DMABUF as fd %i plane %d",
870 group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
871 NULL, group->planes[i].length, 0, 0, group->planes[i].length, i,
872 NULL, expbuf.fd, group);
874 /* Take back the allocator reference */
875 gst_object_ref (allocator);
878 g_assert (gst_is_v4l2_memory (group->mem[i]));
879 mem = (GstV4l2Memory *) group->mem[i];
881 if ((dmafd = dup (mem->dmafd)) < 0)
884 dma_mem = gst_dmabuf_allocator_alloc (dmabuf_allocator, dmafd,
886 _v4l2mem_parent_to_dmabuf (mem, dma_mem);
888 group->mem[i] = dma_mem;
889 group->mems_allocated++;
892 gst_v4l2_allocator_reset_size (allocator, group);
898 GST_ERROR_OBJECT (allocator, "Failed to export DMABUF: %s",
904 GST_ERROR_OBJECT (allocator, "Failed to dup DMABUF descriptor: %s",
910 _cleanup_failed_alloc (allocator, group);
916 gst_v4l2_allocator_clear_dmabufin (GstV4l2Allocator * allocator,
917 GstV4l2MemoryGroup * group)
922 g_return_if_fail (allocator->memory == V4L2_MEMORY_DMABUF);
924 for (i = 0; i < group->n_mem; i++) {
926 mem = (GstV4l2Memory *) group->mem[i];
928 GST_LOG_OBJECT (allocator, "clearing DMABUF import, fd %i plane %d",
935 mem->mem.maxsize = 0;
940 /* Update v4l2 structure */
941 group->planes[i].length = 0;
942 group->planes[i].bytesused = 0;
943 group->planes[i].m.fd = -1;
944 group->planes[i].data_offset = 0;
947 if (!V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
948 group->buffer.bytesused = 0;
949 group->buffer.length = 0;
950 group->buffer.m.fd = -1;
955 gst_v4l2_allocator_alloc_dmabufin (GstV4l2Allocator * allocator)
957 GstV4l2MemoryGroup *group;
960 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_DMABUF, NULL);
962 group = gst_v4l2_allocator_alloc (allocator);
967 for (i = 0; i < group->n_mem; i++) {
968 GST_LOG_OBJECT (allocator, "allocation empty DMABUF import group");
970 if (group->mem[i] == NULL) {
971 group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
972 NULL, 0, 0, 0, 0, i, NULL, -1, group);
974 /* Take back the allocator reference */
975 gst_object_ref (allocator);
978 group->mems_allocated++;
981 gst_v4l2_allocator_clear_dmabufin (allocator, group);
987 gst_v4l2_allocator_clear_userptr (GstV4l2Allocator * allocator,
988 GstV4l2MemoryGroup * group)
993 g_return_if_fail (allocator->memory == V4L2_MEMORY_USERPTR);
995 for (i = 0; i < group->n_mem; i++) {
996 mem = (GstV4l2Memory *) group->mem[i];
998 GST_LOG_OBJECT (allocator, "clearing USERPTR %p plane %d size %"
999 G_GSIZE_FORMAT, mem->data, i, mem->mem.size);
1001 mem->mem.maxsize = 0;
1005 group->planes[i].length = 0;
1006 group->planes[i].bytesused = 0;
1007 group->planes[i].m.userptr = 0;
1010 if (!V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1011 group->buffer.bytesused = 0;
1012 group->buffer.length = 0;
1013 group->buffer.m.userptr = 0;
1017 GstV4l2MemoryGroup *
1018 gst_v4l2_allocator_alloc_userptr (GstV4l2Allocator * allocator)
1020 GstV4l2MemoryGroup *group;
1023 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_USERPTR, NULL);
1025 group = gst_v4l2_allocator_alloc (allocator);
1030 for (i = 0; i < group->n_mem; i++) {
1032 GST_LOG_OBJECT (allocator, "allocating empty USERPTR group");
1034 if (group->mem[i] == NULL) {
1035 group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator),
1036 NULL, 0, 0, 0, 0, i, NULL, -1, group);
1038 /* Take back the allocator reference */
1039 gst_object_ref (allocator);
1042 group->mems_allocated++;
1045 gst_v4l2_allocator_clear_userptr (allocator, group);
1051 gst_v4l2_allocator_import_dmabuf (GstV4l2Allocator * allocator,
1052 GstV4l2MemoryGroup * group, gint n_mem, GstMemory ** dma_mem)
1057 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_DMABUF, FALSE);
1059 if (group->n_mem != n_mem)
1060 goto n_mem_missmatch;
1062 for (i = 0; i < group->n_mem; i++) {
1064 gsize size, offset, maxsize;
1066 if (!gst_is_dmabuf_memory (dma_mem[i]))
1069 size = gst_memory_get_sizes (dma_mem[i], &offset, &maxsize);
1071 if ((dmafd = dup (gst_dmabuf_memory_get_fd (dma_mem[i]))) < 0)
1074 GST_LOG_OBJECT (allocator, "imported DMABUF as fd %i plane %d", dmafd, i);
1076 mem = (GstV4l2Memory *) group->mem[i];
1079 mem->mem.maxsize = maxsize;
1080 mem->mem.offset = offset;
1081 mem->mem.size = size;
1084 /* Update v4l2 structure */
1085 group->planes[i].length = maxsize;
1086 group->planes[i].bytesused = size;
1087 group->planes[i].m.fd = dmafd;
1088 group->planes[i].data_offset = offset;
1091 /* Copy into buffer structure if not using planes */
1092 if (!V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1093 group->buffer.bytesused = group->planes[0].bytesused;
1094 group->buffer.length = group->planes[0].length;
1095 group->buffer.m.fd = group->planes[0].m.userptr;
1097 group->buffer.length = group->n_mem;
1104 GST_ERROR_OBJECT (allocator, "Got %i dmabuf but needed %i", n_mem,
1110 GST_ERROR_OBJECT (allocator, "Memory %i is not of DMABUF", i);
1115 GST_ERROR_OBJECT (allocator, "Failed to dup DMABUF descriptor: %s",
1116 g_strerror (errno));
1122 gst_v4l2_allocator_import_userptr (GstV4l2Allocator * allocator,
1123 GstV4l2MemoryGroup * group, gsize img_size, int n_planes,
1124 gpointer * data, gsize * offset)
1129 g_return_val_if_fail (allocator->memory == V4L2_MEMORY_USERPTR, FALSE);
1131 /* TODO Support passing N plane from 1 memory to MPLANE v4l2 format */
1132 if (n_planes != group->n_mem)
1133 goto n_mem_missmatch;
1135 for (i = 0; i < group->n_mem; i++) {
1136 gsize size, maxsize;
1138 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1139 struct v4l2_pix_format_mplane *pix = &allocator->format.fmt.pix_mp;
1140 maxsize = pix->plane_fmt[i].sizeimage;
1142 maxsize = allocator->format.fmt.pix.sizeimage;
1145 if ((i + 1) == n_planes) {
1146 size = img_size - offset[i];
1148 size = offset[i + 1] - offset[i];
1151 g_assert (size <= img_size);
1153 GST_LOG_OBJECT (allocator, "imported USERPTR %p plane %d size %"
1154 G_GSIZE_FORMAT, data[i], i, size);
1156 mem = (GstV4l2Memory *) group->mem[i];
1158 mem->mem.maxsize = maxsize;
1159 mem->mem.size = size;
1160 mem->data = data[i];
1162 group->planes[i].length = maxsize;
1163 group->planes[i].bytesused = size;
1164 group->planes[i].m.userptr = (unsigned long) data[i];
1165 group->planes[i].data_offset = 0;
1168 /* Copy into buffer structure if not using planes */
1169 if (!V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1170 group->buffer.bytesused = group->planes[0].bytesused;
1171 group->buffer.length = group->planes[0].length;
1172 group->buffer.m.userptr = group->planes[0].m.userptr;
1174 group->buffer.length = group->n_mem;
1181 GST_ERROR_OBJECT (allocator, "Got %i userptr plane while driver need %i",
1182 n_planes, group->n_mem);
1188 gst_v4l2_allocator_flush (GstV4l2Allocator * allocator)
1192 GST_OBJECT_LOCK (allocator);
1194 if (!allocator->active)
1197 for (i = 0; i < allocator->count; i++) {
1198 GstV4l2MemoryGroup *group = allocator->groups[i];
1201 if (IS_QUEUED (group->buffer)) {
1202 UNSET_QUEUED (group->buffer);
1204 gst_v4l2_allocator_reset_group (allocator, group);
1206 for (n = 0; n < group->n_mem; n++)
1207 gst_memory_unref (group->mem[n]);
1212 GST_OBJECT_UNLOCK (allocator);
1216 gst_v4l2_allocator_qbuf (GstV4l2Allocator * allocator,
1217 GstV4l2MemoryGroup * group)
1219 gboolean ret = TRUE;
1223 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1224 for (i = 0; i < group->n_mem; i++)
1225 group->planes[i].bytesused =
1226 gst_memory_get_sizes (group->mem[i], NULL, NULL);
1228 group->buffer.bytesused = gst_memory_get_sizes (group->mem[0], NULL, NULL);
1231 if (v4l2_ioctl (allocator->video_fd, VIDIOC_QBUF, &group->buffer) < 0) {
1232 GST_ERROR_OBJECT (allocator, "failed queing buffer %i: %s",
1233 group->buffer.index, g_strerror (errno));
1235 if (IS_QUEUED (group->buffer)) {
1236 GST_DEBUG_OBJECT (allocator,
1237 "driver pretends buffer is queued even if queue failed");
1238 UNSET_QUEUED (group->buffer);
1243 GST_LOG_OBJECT (allocator, "queued buffer %i (flags 0x%X)",
1244 group->buffer.index, group->buffer.flags);
1246 if (!IS_QUEUED (group->buffer)) {
1247 GST_DEBUG_OBJECT (allocator,
1248 "driver pretends buffer is not queued even if queue succeeded");
1249 SET_QUEUED (group->buffer);
1252 /* Ensure the memory will stay around and is RO */
1253 for (i = 0; i < group->n_mem; i++)
1254 gst_memory_ref (group->mem[i]);
1260 GstV4l2MemoryGroup *
1261 gst_v4l2_allocator_dqbuf (GstV4l2Allocator * allocator)
1263 struct v4l2_buffer buffer = { 0 };
1264 struct v4l2_plane planes[VIDEO_MAX_PLANES] = { {0} };
1267 GstV4l2MemoryGroup *group = NULL;
1269 buffer.type = allocator->type;
1270 buffer.memory = allocator->memory;
1272 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1273 buffer.length = allocator->format.fmt.pix_mp.num_planes;
1274 buffer.m.planes = planes;
1277 if (v4l2_ioctl (allocator->video_fd, VIDIOC_DQBUF, &buffer) < 0)
1280 group = allocator->groups[buffer.index];
1281 group->buffer = buffer;
1283 GST_LOG_OBJECT (allocator, "dequeued buffer %i (flags 0x%X)", buffer.index,
1286 if (IS_QUEUED (group->buffer)) {
1287 GST_DEBUG_OBJECT (allocator,
1288 "driver pretends buffer is queued even if dequeue succeeded");
1289 UNSET_QUEUED (group->buffer);
1292 if (V4L2_TYPE_IS_MULTIPLANAR (allocator->type)) {
1293 group->buffer.m.planes = group->planes;
1294 memcpy (group->planes, buffer.m.planes, sizeof (planes));
1296 group->planes[0].bytesused = group->buffer.bytesused;
1297 group->planes[0].length = group->buffer.length;
1298 g_assert (sizeof (group->planes[0].m) == sizeof (group->buffer.m));
1299 memcpy (&group->planes[0].m, &group->buffer.m, sizeof (group->buffer.m));
1302 /* And update memory size */
1303 if (V4L2_TYPE_IS_OUTPUT (allocator->type)) {
1304 gst_v4l2_allocator_reset_size (allocator, group);
1306 /* for capture, simply read the size */
1307 for (i = 0; i < group->n_mem; i++) {
1308 gst_memory_resize (group->mem[i], 0, group->planes[i].bytesused);
1312 /* Release the memory, possibly making it RW again */
1313 for (i = 0; i < group->n_mem; i++)
1314 gst_memory_unref (group->mem[i]);
1319 GST_ERROR_OBJECT (allocator, "failed dequeuing a %s buffer: %s",
1320 memory_type_to_str (allocator->memory), g_strerror (errno));
1324 GST_WARNING_OBJECT (allocator,
1325 "Non-blocking I/O has been selected using O_NONBLOCK and"
1326 " no buffer was in the outgoing queue.");
1329 GST_ERROR_OBJECT (allocator,
1330 "The buffer type is not supported, or the index is out of bounds, "
1331 "or no buffers have been allocated yet, or the userptr "
1332 "or length are invalid.");
1335 GST_ERROR_OBJECT (allocator,
1336 "insufficient memory to enqueue a user pointer buffer");
1339 GST_INFO_OBJECT (allocator,
1340 "VIDIOC_DQBUF failed due to an internal error."
1341 " Can also indicate temporary problems like signal loss."
1342 " Note the driver might dequeue an (empty) buffer despite"
1343 " returning an error, or even stop capturing.");
1344 /* have we de-queued a buffer ? */
1345 if (!IS_QUEUED (buffer)) {
1346 GST_DEBUG_OBJECT (allocator, "reenqueing buffer");
1347 /* FIXME ... should we do something here? */
1351 GST_WARNING_OBJECT (allocator, "could not sync on a buffer on device");
1354 GST_WARNING_OBJECT (allocator,
1355 "Grabbing frame got interrupted unexpectedly. %d: %s.", errno,
1356 g_strerror (errno));
1364 gst_v4l2_allocator_reset_group (GstV4l2Allocator * allocator,
1365 GstV4l2MemoryGroup * group)
1367 switch (allocator->memory) {
1368 case V4L2_MEMORY_USERPTR:
1369 gst_v4l2_allocator_clear_userptr (allocator, group);
1371 case V4L2_MEMORY_DMABUF:
1372 gst_v4l2_allocator_clear_dmabufin (allocator, group);
1374 case V4L2_MEMORY_MMAP:
1377 g_assert_not_reached ();
1381 gst_v4l2_allocator_reset_size (allocator, group);
1385 gst_v4l2_allocator_num_allocated (GstV4l2Allocator * allocator)
1387 gsize num_allocated;
1389 GST_OBJECT_LOCK (allocator);
1391 num_allocated = allocator->count;
1393 GST_OBJECT_UNLOCK (allocator);
1395 return num_allocated;