xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1 | /* GStreamer |
| 2 | * Copyright (C) 2022 <xuesong.jiang@amlogic.com> |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public |
| 15 | * License along with this library; if not, write to the |
| 16 | * Free Software Foundation, Inc., 51 Franklin Street, Suite 500, |
| 17 | * Boston, MA 02110-1335, USA. |
| 18 | */ |
| 19 | |
| 20 | #ifndef __GST_AML_V4L2_ALLOCATOR_H__ |
| 21 | #define __GST_AML_V4L2_ALLOCATOR_H__ |
| 22 | |
| 23 | #include "ext/videodev2.h" |
| 24 | #include <gst/gst.h> |
| 25 | #include <gst/gstatomicqueue.h> |
| 26 | |
| 27 | G_BEGIN_DECLS |
| 28 | |
| 29 | #define GST_TYPE_AML_V4L2_ALLOCATOR (gst_aml_v4l2_allocator_get_type()) |
| 30 | #define GST_IS_AML_V4L2_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_AML_V4L2_ALLOCATOR)) |
| 31 | #define GST_IS_AML_V4L2_ALLOCATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_AML_V4L2_ALLOCATOR)) |
| 32 | #define GST_AML_V4L2_ALLOCATOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_AML_V4L2_ALLOCATOR, GstAmlV4l2AllocatorClass)) |
| 33 | #define GST_AML_V4L2_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_AML_V4L2_ALLOCATOR, GstAmlV4l2Allocator)) |
| 34 | #define GST_AML_V4L2_ALLOCATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_AML_V4L2_ALLOCATOR, GstAmlV4l2AllocatorClass)) |
| 35 | #define GST_AML_V4L2_ALLOCATOR_CAST(obj) ((GstAmlV4l2Allocator *)(obj)) |
| 36 | |
| 37 | #define GST_AML_V4L2_ALLOCATOR_CAN_REQUEST(obj, type) \ |
| 38 | (GST_OBJECT_FLAG_IS_SET(obj, GST_V4L2_ALLOCATOR_FLAG_##type##_REQBUFS)) |
| 39 | #define GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(obj, type) \ |
| 40 | (GST_OBJECT_FLAG_IS_SET(obj, GST_V4L2_ALLOCATOR_FLAG_##type##_CREATE_BUFS)) |
| 41 | #define GST_AML_V4L2_ALLOCATOR_CAN_ORPHAN_BUFS(obj) \ |
| 42 | (GST_OBJECT_FLAG_IS_SET(obj, GST_V4L2_ALLOCATOR_FLAG_SUPPORTS_ORPHANED_BUFS)) |
| 43 | #define GST_AML_V4L2_ALLOCATOR_IS_ORPHANED(obj) \ |
| 44 | (GST_OBJECT_FLAG_IS_SET(obj, GST_V4L2_ALLOCATOR_FLAG_ORPHANED)) |
| 45 | |
| 46 | #define GST_AML_V4L2_MEMORY_QUARK gst_aml_v4l2_memory_quark() |
| 47 | |
| 48 | typedef struct _GstAmlV4l2Allocator GstAmlV4l2Allocator; |
| 49 | typedef struct _GstAmlV4l2AllocatorClass GstAmlV4l2AllocatorClass; |
| 50 | typedef struct _GstAmlV4l2MemoryGroup GstAmlV4l2MemoryGroup; |
| 51 | typedef struct _GstAmlV4l2Memory GstAmlV4l2Memory; |
| 52 | typedef enum _GstAmlV4l2Capabilities GstAmlV4l2Capabilities; |
| 53 | typedef enum _GstAmlV4l2Return GstAmlV4l2Return; |
| 54 | |
| 55 | enum _GstAmlV4l2AllocatorFlags |
| 56 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 57 | GST_V4L2_ALLOCATOR_FLAG_MMAP_REQBUFS = (GST_ALLOCATOR_FLAG_LAST << 0), |
| 58 | GST_V4L2_ALLOCATOR_FLAG_MMAP_CREATE_BUFS = (GST_ALLOCATOR_FLAG_LAST << 1), |
| 59 | GST_V4L2_ALLOCATOR_FLAG_USERPTR_REQBUFS = (GST_ALLOCATOR_FLAG_LAST << 2), |
| 60 | GST_V4L2_ALLOCATOR_FLAG_USERPTR_CREATE_BUFS = (GST_ALLOCATOR_FLAG_LAST << 3), |
| 61 | GST_V4L2_ALLOCATOR_FLAG_DMABUF_REQBUFS = (GST_ALLOCATOR_FLAG_LAST << 4), |
| 62 | GST_V4L2_ALLOCATOR_FLAG_DMABUF_CREATE_BUFS = (GST_ALLOCATOR_FLAG_LAST << 5), |
| 63 | GST_V4L2_ALLOCATOR_FLAG_SUPPORTS_ORPHANED_BUFS = (GST_ALLOCATOR_FLAG_LAST << 6), |
| 64 | GST_V4L2_ALLOCATOR_FLAG_ORPHANED = (GST_ALLOCATOR_FLAG_LAST << 7), |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | enum _GstAmlV4l2Return |
| 68 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 69 | GST_AML_V4L2_OK = 0, |
| 70 | GST_AML_V4L2_ERROR = -1, |
| 71 | GST_AML_V4L2_BUSY = -2 |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | struct _GstAmlV4l2Memory |
| 75 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 76 | GstMemory mem; |
| 77 | gint plane; |
| 78 | GstAmlV4l2MemoryGroup *group; |
| 79 | gpointer data; |
| 80 | gint dmafd; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | struct _GstAmlV4l2MemoryGroup |
| 84 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 85 | gint n_mem; |
| 86 | GstMemory * mem[VIDEO_MAX_PLANES]; |
| 87 | gint mems_allocated; |
| 88 | struct v4l2_buffer buffer; |
| 89 | struct v4l2_plane planes[VIDEO_MAX_PLANES]; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | struct _GstAmlV4l2Allocator |
| 93 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 94 | GstAllocator parent; |
| 95 | GstAmlV4l2Object *obj; |
| 96 | guint32 count; |
| 97 | guint32 memory; |
| 98 | gboolean can_allocate; |
| 99 | gboolean active; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 100 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 101 | GstAmlV4l2MemoryGroup *groups[VIDEO_MAX_FRAME]; |
| 102 | GstAtomicQueue *free_queue; |
| 103 | GstAtomicQueue *pending_queue; |
| 104 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | struct _GstAmlV4l2AllocatorClass |
| 108 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 109 | GstAllocatorClass parent_class; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | GType gst_aml_v4l2_allocator_get_type(void); |
| 113 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 114 | gboolean gst_aml_is_v4l2_memory(GstMemory *mem); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 115 | |
| 116 | GQuark gst_aml_v4l2_memory_quark(void); |
| 117 | |
| 118 | gboolean gst_aml_v4l2_allocator_is_active(GstAmlV4l2Allocator *allocator); |
| 119 | |
| 120 | guint gst_aml_v4l2_allocator_get_size(GstAmlV4l2Allocator *allocator); |
| 121 | |
| 122 | GstAmlV4l2Allocator *gst_aml_v4l2_allocator_new(GstObject *parent, GstAmlV4l2Object *obj); |
| 123 | |
| 124 | guint gst_aml_v4l2_allocator_start(GstAmlV4l2Allocator *allocator, |
| 125 | guint32 count, guint32 memory); |
| 126 | |
| 127 | GstAmlV4l2Return gst_aml_v4l2_allocator_stop(GstAmlV4l2Allocator *allocator); |
| 128 | |
| 129 | gboolean gst_aml_v4l2_allocator_orphan(GstAmlV4l2Allocator *allocator); |
| 130 | |
| 131 | GstAmlV4l2MemoryGroup *gst_aml_v4l2_allocator_alloc_mmap(GstAmlV4l2Allocator *allocator); |
| 132 | |
| 133 | GstAmlV4l2MemoryGroup *gst_aml_v4l2_allocator_alloc_dmabuf(GstAmlV4l2Allocator *allocator, |
| 134 | GstAllocator *dmabuf_allocator); |
| 135 | |
| 136 | GstAmlV4l2MemoryGroup *gst_aml_v4l2_allocator_alloc_dmabufin(GstAmlV4l2Allocator *allocator); |
| 137 | |
| 138 | GstAmlV4l2MemoryGroup *gst_aml_v4l2_allocator_alloc_userptr(GstAmlV4l2Allocator *allocator); |
| 139 | |
| 140 | gboolean gst_aml_v4l2_allocator_import_dmabuf(GstAmlV4l2Allocator *allocator, |
| 141 | GstAmlV4l2MemoryGroup *group, |
| 142 | gint n_mem, GstMemory **dma_mem); |
| 143 | |
| 144 | gboolean gst_aml_v4l2_allocator_import_userptr(GstAmlV4l2Allocator *allocator, |
| 145 | GstAmlV4l2MemoryGroup *group, |
| 146 | gsize img_size, int n_planes, |
| 147 | gpointer *data, gsize *size); |
| 148 | |
| 149 | void gst_aml_v4l2_allocator_flush(GstAmlV4l2Allocator *allocator); |
| 150 | |
| 151 | gboolean gst_aml_v4l2_allocator_qbuf(GstAmlV4l2Allocator *allocator, |
| 152 | GstAmlV4l2MemoryGroup *group); |
| 153 | |
| 154 | GstFlowReturn gst_aml_v4l2_allocator_dqbuf(GstAmlV4l2Allocator *allocator, |
| 155 | GstAmlV4l2MemoryGroup **group); |
| 156 | |
| 157 | void gst_aml_v4l2_allocator_reset_group(GstAmlV4l2Allocator *allocator, |
| 158 | GstAmlV4l2MemoryGroup *group); |
| 159 | |
| 160 | G_END_DECLS |
| 161 | |
| 162 | #endif /* __GST_AML_V4L2_ALLOCATOR_H__ */ |