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 | #include "config.h" |
| 21 | |
| 22 | #include "ext/videodev2.h" |
| 23 | |
| 24 | #include "gstamlv4l2object.h" |
| 25 | #include "gstamlv4l2allocator.h" |
| 26 | |
| 27 | #include <gst/allocators/gstdmabuf.h> |
| 28 | |
| 29 | #include <fcntl.h> |
| 30 | #include <string.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/mman.h> |
| 34 | #include <unistd.h> |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame] | 35 | #include <stdio.h> |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 36 | |
| 37 | #define GST_AML_V4L2_MEMORY_TYPE "V4l2Memory" |
| 38 | |
| 39 | #define gst_aml_v4l2_allocator_parent_class parent_class |
| 40 | G_DEFINE_TYPE(GstAmlV4l2Allocator, gst_aml_v4l2_allocator, GST_TYPE_ALLOCATOR); |
| 41 | |
| 42 | GST_DEBUG_CATEGORY_STATIC(amlv4l2allocator_debug); |
| 43 | #define GST_CAT_DEFAULT amlv4l2allocator_debug |
| 44 | |
| 45 | #define UNSET_QUEUED(buffer) \ |
| 46 | ((buffer).flags &= ~(V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)) |
| 47 | |
| 48 | #define SET_QUEUED(buffer) ((buffer).flags |= V4L2_BUF_FLAG_QUEUED) |
| 49 | |
| 50 | #define IS_QUEUED(buffer) \ |
| 51 | ((buffer).flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)) |
| 52 | |
| 53 | enum |
| 54 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 55 | GROUP_RELEASED, |
| 56 | LAST_SIGNAL |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | static guint gst_aml_v4l2_allocator_signals[LAST_SIGNAL] = {0}; |
| 60 | |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame] | 61 | static void gst_aml_v4l2_allocator_dump_es_buf(GstAmlV4l2Allocator *allocator, GstAmlV4l2MemoryGroup *group) |
| 62 | { |
| 63 | const gchar *dump_dir = NULL; |
| 64 | gchar *full_file_name = NULL; |
| 65 | FILE *out = NULL; |
| 66 | |
| 67 | dump_dir = g_getenv("GST_AML_DUMP_AML_V4L2_ES_BUF_DIR"); |
| 68 | if (G_LIKELY(dump_dir == NULL)) |
| 69 | return; |
| 70 | |
| 71 | if (allocator->obj->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && allocator->obj->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) |
| 72 | return; |
| 73 | |
| 74 | GST_DEBUG_OBJECT(allocator, "assert ok start dump"); |
| 75 | full_file_name = g_strdup_printf("%s" G_DIR_SEPARATOR_S "amlv4l2_es.bin", dump_dir); |
| 76 | if ((out = fopen(full_file_name, "ab"))) |
| 77 | { |
| 78 | GST_DEBUG_OBJECT(allocator, "open dir ok"); |
| 79 | GstMapInfo map; |
| 80 | memset(&map, 0, sizeof(GstMapInfo)); |
| 81 | for (int i = 0; i < group->n_mem; i++) |
| 82 | { |
| 83 | if (gst_memory_map(group->mem[i], &map, GST_MAP_READ)) |
| 84 | { |
| 85 | GST_DEBUG_OBJECT(allocator, "es ts:%llu dump_size:%d,v4l2_buf_byteused:%d,%d", |
| 86 | group->buffer.timestamp.tv_sec * 1000000000ULL + group->buffer.timestamp.tv_usec * 1000, |
| 87 | map.size, |
| 88 | group->buffer.bytesused, group->planes[0].bytesused); |
| 89 | fwrite(map.data, map.size, 1, out); |
| 90 | gst_memory_unmap(group->mem[i], &map); |
| 91 | } |
| 92 | } |
| 93 | fclose(out); |
| 94 | out = NULL; |
| 95 | } |
| 96 | g_free(full_file_name); |
| 97 | } |
| 98 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 99 | static void gst_aml_v4l2_allocator_release(GstAmlV4l2Allocator *allocator, |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 100 | GstAmlV4l2Memory *mem); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 101 | |
| 102 | static const gchar * |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 103 | memory_type_to_str (guint32 memory) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 104 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 105 | switch (memory) |
| 106 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 107 | case V4L2_MEMORY_MMAP: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 108 | return "mmap"; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 109 | case V4L2_MEMORY_USERPTR: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 110 | return "userptr"; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 111 | case V4L2_MEMORY_DMABUF: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 112 | return "dmabuf"; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 113 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 114 | return "unknown"; |
| 115 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /*************************************/ |
| 119 | /* GstAmlV4lMemory implementation */ |
| 120 | /*************************************/ |
| 121 | |
| 122 | static gpointer |
| 123 | _v4l2mem_map(GstAmlV4l2Memory *mem, gsize maxsize, GstMapFlags flags) |
| 124 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 125 | gpointer data = NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 126 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 127 | switch (mem->group->buffer.memory) |
| 128 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 129 | case V4L2_MEMORY_MMAP: |
| 130 | case V4L2_MEMORY_USERPTR: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 131 | data = mem->data; |
| 132 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 133 | case V4L2_MEMORY_DMABUF: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 134 | /* v4l2 dmabuf memory are not shared with downstream */ |
| 135 | g_assert_not_reached (); |
| 136 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 137 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 138 | GST_WARNING ("Unknown memory type %i", mem->group->buffer.memory); |
| 139 | break; |
| 140 | } |
| 141 | return data; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static gboolean |
| 145 | _v4l2mem_unmap(GstAmlV4l2Memory *mem) |
| 146 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 147 | gboolean ret = FALSE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 148 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 149 | switch (mem->group->buffer.memory) |
| 150 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 151 | case V4L2_MEMORY_MMAP: |
| 152 | case V4L2_MEMORY_USERPTR: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 153 | ret = TRUE; |
| 154 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 155 | case V4L2_MEMORY_DMABUF: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 156 | /* v4l2 dmabuf memory are not share with downstream */ |
| 157 | g_assert_not_reached (); |
| 158 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 159 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 160 | GST_WARNING ("Unknown memory type %i", mem->group->buffer.memory); |
| 161 | break; |
| 162 | } |
| 163 | return ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static gboolean |
| 167 | _v4l2mem_dispose(GstAmlV4l2Memory *mem) |
| 168 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 169 | GstAmlV4l2Allocator *allocator = (GstAmlV4l2Allocator *)mem->mem.allocator; |
| 170 | GstAmlV4l2MemoryGroup *group = mem->group; |
| 171 | gboolean ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 172 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 173 | if (group->mem[mem->plane]) |
| 174 | { |
| 175 | /* We may have a dmabuf, replace it with returned original memory */ |
| 176 | group->mem[mem->plane] = gst_memory_ref ((GstMemory *) mem); |
| 177 | gst_aml_v4l2_allocator_release(allocator, mem); |
| 178 | ret = FALSE; |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | gst_object_ref (allocator); |
| 183 | ret = TRUE; |
| 184 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 185 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 186 | return ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static inline GstAmlV4l2Memory * |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 190 | _v4l2mem_new (GstMemoryFlags flags, GstAllocator * allocator, |
| 191 | GstMemory * parent, gsize maxsize, gsize align, gsize offset, gsize size, |
| 192 | gint plane, gpointer data, int dmafd, GstAmlV4l2MemoryGroup *group) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 193 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 194 | GstAmlV4l2Memory *mem; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 195 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 196 | mem = g_slice_new0 (GstAmlV4l2Memory); |
| 197 | gst_memory_init (GST_MEMORY_CAST (mem), |
| 198 | flags, allocator, parent, maxsize, align, offset, size); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 199 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 200 | if (parent == NULL) |
| 201 | mem->mem.mini_object.dispose = |
| 202 | (GstMiniObjectDisposeFunction) _v4l2mem_dispose; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 203 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 204 | mem->plane = plane; |
| 205 | mem->data = data; |
| 206 | mem->dmafd = dmafd; |
| 207 | mem->group = group; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 208 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 209 | return mem; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static GstAmlV4l2Memory * |
| 213 | _v4l2mem_share(GstAmlV4l2Memory *mem, gssize offset, gsize size) |
| 214 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 215 | GstAmlV4l2Memory *sub; |
| 216 | GstMemory *parent; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 217 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 218 | /* find the real parent */ |
| 219 | if ((parent = mem->mem.parent) == NULL) |
| 220 | parent = (GstMemory *) mem; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 221 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 222 | if (size == -1) |
| 223 | size = mem->mem.size - offset; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 224 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 225 | /* the shared memory is always readonly */ |
| 226 | sub = _v4l2mem_new (GST_MINI_OBJECT_FLAGS (parent) | |
| 227 | GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->mem.allocator, parent, |
| 228 | mem->mem.maxsize, mem->mem.align, offset, size, mem->plane, mem->data, |
| 229 | -1, mem->group); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 230 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 231 | return sub; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static gboolean |
| 235 | _v4l2mem_is_span(GstAmlV4l2Memory *mem1, GstAmlV4l2Memory *mem2, gsize *offset) |
| 236 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 237 | if (offset) |
| 238 | *offset = mem1->mem.offset - mem1->mem.parent->offset; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 239 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 240 | /* and memory is contiguous */ |
| 241 | return mem1->mem.offset + mem1->mem.size == mem2->mem.offset; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | gboolean |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 245 | gst_aml_is_v4l2_memory(GstMemory *mem) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 246 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 247 | return gst_memory_is_type(mem, GST_AML_V4L2_MEMORY_TYPE); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | GQuark |
| 251 | gst_aml_v4l2_memory_quark(void) |
| 252 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 253 | static GQuark quark = 0; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 254 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 255 | if (quark == 0) |
| 256 | quark = g_quark_from_string ("GstAmlV4l2Memory"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 257 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 258 | return quark; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /*************************************/ |
| 262 | /* GstAmlV4l2MemoryGroup implementation */ |
| 263 | /*************************************/ |
| 264 | |
| 265 | static void |
| 266 | gst_aml_v4l2_memory_group_free(GstAmlV4l2MemoryGroup *group) |
| 267 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 268 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 269 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 270 | for (i = 0; i < group->n_mem; i++) |
| 271 | { |
| 272 | GstMemory *mem = group->mem[i]; |
| 273 | group->mem[i] = NULL; |
| 274 | if (mem) |
| 275 | gst_memory_unref (mem); |
| 276 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 277 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 278 | g_slice_free(GstAmlV4l2MemoryGroup, group); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static GstAmlV4l2MemoryGroup * |
| 282 | gst_aml_v4l2_memory_group_new(GstAmlV4l2Allocator *allocator, guint32 index) |
| 283 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 284 | GstAmlV4l2Object *obj = allocator->obj; |
| 285 | guint32 memory = allocator->memory; |
| 286 | struct v4l2_format *format = &obj->format; |
| 287 | GstAmlV4l2MemoryGroup *group; |
| 288 | gsize img_size, buf_size; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 289 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 290 | group = g_slice_new0 (GstAmlV4l2MemoryGroup); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 291 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 292 | group->buffer.type = format->type; |
| 293 | group->buffer.index = index; |
| 294 | group->buffer.memory = memory; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 295 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 296 | if (V4L2_TYPE_IS_MULTIPLANAR(format->type)) |
| 297 | { |
| 298 | group->n_mem = group->buffer.length = format->fmt.pix_mp.num_planes; |
| 299 | group->buffer.m.planes = group->planes; |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | group->n_mem = 1; |
| 304 | } |
| 305 | |
| 306 | if (obj->ioctl (obj->video_fd, VIDIOC_QUERYBUF, &group->buffer) < 0) |
| 307 | goto querybuf_failed; |
| 308 | |
| 309 | if (group->buffer.index != index) |
| 310 | { |
| 311 | GST_ERROR_OBJECT (allocator, "Buffer index returned by VIDIOC_QUERYBUF " |
| 312 | "didn't match, this indicate the presence of a bug in your driver or " |
| 313 | "libv4l2"); |
| 314 | g_slice_free (GstAmlV4l2MemoryGroup, group); |
| 315 | return NULL; |
| 316 | } |
| 317 | |
| 318 | /* Check that provided size matches the format we have negotiation. Failing |
| 319 | * there usually means a driver of libv4l bug. */ |
| 320 | if (V4L2_TYPE_IS_MULTIPLANAR(obj->type)) |
| 321 | { |
| 322 | gint i; |
| 323 | |
| 324 | for (i = 0; i < group->n_mem; i++) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 325 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 326 | img_size = obj->format.fmt.pix_mp.plane_fmt[i].sizeimage; |
| 327 | buf_size = group->planes[i].length; |
| 328 | if (buf_size < img_size) |
| 329 | goto buffer_too_short; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 330 | } |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 331 | } |
| 332 | else |
| 333 | { |
| 334 | img_size = obj->format.fmt.pix.sizeimage; |
| 335 | buf_size = group->buffer.length; |
| 336 | if (buf_size < img_size) |
| 337 | goto buffer_too_short; |
| 338 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 339 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 340 | /* We save non planar buffer information into the multi-planar plane array |
| 341 | * to avoid duplicating the code later */ |
| 342 | if (!V4L2_TYPE_IS_MULTIPLANAR(format->type)) |
| 343 | { |
| 344 | group->planes[0].bytesused = group->buffer.bytesused; |
| 345 | group->planes[0].length = group->buffer.length; |
| 346 | group->planes[0].data_offset = 0; |
| 347 | g_assert (sizeof (group->planes[0].m) == sizeof (group->buffer.m)); |
| 348 | memcpy (&group->planes[0].m, &group->buffer.m, sizeof (group->buffer.m)); |
| 349 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 350 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 351 | GST_LOG_OBJECT (allocator, "Got %s buffer", memory_type_to_str (memory)); |
| 352 | GST_LOG_OBJECT (allocator, " index: %u", group->buffer.index); |
| 353 | GST_LOG_OBJECT (allocator, " type: %d", group->buffer.type); |
| 354 | GST_LOG_OBJECT (allocator, " flags: %08x", group->buffer.flags); |
| 355 | GST_LOG_OBJECT (allocator, " field: %d", group->buffer.field); |
| 356 | GST_LOG_OBJECT (allocator, " memory: %d", group->buffer.memory); |
| 357 | GST_LOG_OBJECT (allocator, " planes: %d", group->n_mem); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 358 | |
| 359 | #ifndef GST_DISABLE_GST_DEBUG |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 360 | if (memory == V4L2_MEMORY_MMAP) |
| 361 | { |
| 362 | gint i; |
| 363 | for (i = 0; i < group->n_mem; i++) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 364 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 365 | GST_LOG_OBJECT (allocator, |
| 366 | " [%u] bytesused: %u, length: %u, offset: %u", i, |
| 367 | group->planes[i].bytesused, group->planes[i].length, |
| 368 | group->planes[i].data_offset); |
| 369 | GST_LOG_OBJECT (allocator, " [%u] MMAP offset: %u", i, |
| 370 | group->planes[i].m.mem_offset); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 371 | } |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 372 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 373 | #endif |
| 374 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 375 | return group; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 376 | |
| 377 | querybuf_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 378 | { |
| 379 | GST_ERROR ("error querying buffer %d: %s", index, g_strerror (errno)); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 380 | goto failed; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 381 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 382 | buffer_too_short: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 383 | { |
| 384 | GST_ERROR ("buffer size %" G_GSIZE_FORMAT |
| 385 | " is smaller then negotiated size %" G_GSIZE_FORMAT |
| 386 | ", this is usually the result of a bug in the v4l2 driver or libv4l.", |
| 387 | buf_size, img_size); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 388 | goto failed; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 389 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 390 | failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 391 | gst_aml_v4l2_memory_group_free(group); |
| 392 | return NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 393 | } |
| 394 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 395 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 396 | /*************************************/ |
| 397 | /* GstV4lAllocator implementation */ |
| 398 | /*************************************/ |
| 399 | |
| 400 | static void |
| 401 | gst_aml_v4l2_allocator_release(GstAmlV4l2Allocator *allocator, GstAmlV4l2Memory *mem) |
| 402 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 403 | GstAmlV4l2MemoryGroup *group = mem->group; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 404 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 405 | GST_LOG_OBJECT (allocator, "plane %i of buffer %u released", |
| 406 | mem->plane, group->buffer.index); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 407 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 408 | switch (allocator->memory) |
| 409 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 410 | case V4L2_MEMORY_DMABUF: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 411 | close (mem->dmafd); |
| 412 | mem->dmafd = -1; |
| 413 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 414 | case V4L2_MEMORY_USERPTR: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 415 | mem->data = NULL; |
| 416 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 417 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 418 | break; |
| 419 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 420 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 421 | /* When all memory are back, put the group back in the free queue */ |
| 422 | if (g_atomic_int_dec_and_test(&group->mems_allocated)) |
| 423 | { |
| 424 | GST_LOG_OBJECT (allocator, "buffer %u released", group->buffer.index); |
| 425 | gst_atomic_queue_push (allocator->free_queue, group); |
| 426 | g_signal_emit (allocator, gst_aml_v4l2_allocator_signals[GROUP_RELEASED], 0); |
| 427 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 428 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 429 | /* Keep last, allocator may be freed after this call */ |
| 430 | g_object_unref (allocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static void |
| 434 | gst_aml_v4l2_allocator_free(GstAllocator *gallocator, GstMemory *gmem) |
| 435 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 436 | GstAmlV4l2Allocator *allocator = (GstAmlV4l2Allocator *)gallocator; |
| 437 | GstAmlV4l2Object *obj = allocator->obj; |
| 438 | GstAmlV4l2Memory *mem = (GstAmlV4l2Memory *)gmem; |
| 439 | GstAmlV4l2MemoryGroup *group = mem->group; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 440 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 441 | /* Only free unparented memory */ |
| 442 | if (mem->mem.parent == NULL) |
| 443 | { |
| 444 | GST_LOG_OBJECT (allocator, "freeing plane %i of buffer %u", |
| 445 | mem->plane, group->buffer.index); |
| 446 | |
| 447 | if (allocator->memory == V4L2_MEMORY_MMAP) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 448 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 449 | if (mem->data) |
| 450 | obj->munmap (mem->data, group->planes[mem->plane].length); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 451 | } |
| 452 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 453 | /* This apply for both mmap with expbuf, and dmabuf imported memory */ |
| 454 | if (mem->dmafd >= 0) |
| 455 | close (mem->dmafd); |
| 456 | } |
| 457 | |
| 458 | g_slice_free (GstAmlV4l2Memory, mem); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | static void |
| 462 | gst_aml_v4l2_allocator_dispose(GObject *obj) |
| 463 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 464 | GstAmlV4l2Allocator *allocator = (GstAmlV4l2Allocator *)obj; |
| 465 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 466 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 467 | GST_LOG_OBJECT (obj, "called"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 468 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 469 | for (i = 0; i < allocator->count; i++) |
| 470 | { |
| 471 | GstAmlV4l2MemoryGroup *group = allocator->groups[i]; |
| 472 | allocator->groups[i] = NULL; |
| 473 | if (group) |
| 474 | gst_aml_v4l2_memory_group_free(group); |
| 475 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 476 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 477 | G_OBJECT_CLASS (parent_class)->dispose (obj); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | static void |
| 481 | gst_aml_v4l2_allocator_finalize(GObject *obj) |
| 482 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 483 | GstAmlV4l2Allocator *allocator = (GstAmlV4l2Allocator *)obj; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 484 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 485 | GST_LOG_OBJECT (obj, "called"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 486 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 487 | gst_atomic_queue_unref (allocator->free_queue); |
| 488 | gst_object_unref (allocator->obj->element); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 489 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 490 | G_OBJECT_CLASS (parent_class)->finalize (obj); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static void |
| 494 | gst_aml_v4l2_allocator_class_init(GstAmlV4l2AllocatorClass *klass) |
| 495 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 496 | GObjectClass *object_class; |
| 497 | GstAllocatorClass *allocator_class; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 498 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 499 | allocator_class = (GstAllocatorClass *) klass; |
| 500 | object_class = (GObjectClass *) klass; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 501 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 502 | allocator_class->alloc = NULL; |
| 503 | allocator_class->free = gst_aml_v4l2_allocator_free; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 504 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 505 | object_class->dispose = gst_aml_v4l2_allocator_dispose; |
| 506 | object_class->finalize = gst_aml_v4l2_allocator_finalize; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 507 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 508 | gst_aml_v4l2_allocator_signals[GROUP_RELEASED] = g_signal_new("group-released", |
| 509 | G_TYPE_FROM_CLASS(object_class), G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, |
| 510 | G_TYPE_NONE, 0); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 511 | |
xuesong.jiang | 5fccce2 | 2024-10-22 20:28:50 +0800 | [diff] [blame] | 512 | GST_DEBUG_CATEGORY_INIT(amlv4l2allocator_debug, "amlv4l2allocator", 0, |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 513 | "V4L2 Allocator"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | static void |
| 517 | gst_aml_v4l2_allocator_init(GstAmlV4l2Allocator *allocator) |
| 518 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 519 | GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 520 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 521 | alloc->mem_type = GST_AML_V4L2_MEMORY_TYPE; |
| 522 | alloc->mem_map = (GstMemoryMapFunction) _v4l2mem_map; |
| 523 | alloc->mem_unmap = (GstMemoryUnmapFunction) _v4l2mem_unmap; |
| 524 | alloc->mem_share = (GstMemoryShareFunction) _v4l2mem_share; |
| 525 | alloc->mem_is_span = (GstMemoryIsSpanFunction) _v4l2mem_is_span; |
| 526 | /* Use the default, fallback copy function */ |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 527 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 528 | allocator->free_queue = gst_atomic_queue_new (VIDEO_MAX_FRAME); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 529 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 530 | GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | #define GST_AML_V4L2_ALLOCATOR_PROBE(obj, type) \ |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 534 | gst_aml_v4l2_allocator_probe((obj), V4L2_MEMORY_##type, \ |
| 535 | GST_V4L2_ALLOCATOR_FLAG_##type##_REQBUFS, \ |
| 536 | GST_V4L2_ALLOCATOR_FLAG_##type##_CREATE_BUFS) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 537 | static guint32 |
| 538 | gst_aml_v4l2_allocator_probe(GstAmlV4l2Allocator *allocator, guint32 memory, |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 539 | guint32 breq_flag, guint32 bcreate_flag) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 540 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 541 | GstAmlV4l2Object *obj = allocator->obj; |
| 542 | struct v4l2_requestbuffers breq = { 0 }; |
| 543 | guint32 flags = 0; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 544 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 545 | breq.type = obj->type; |
| 546 | breq.count = 0; |
| 547 | breq.memory = memory; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 548 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 549 | if (obj->ioctl(obj->video_fd, VIDIOC_REQBUFS, &breq) == 0) |
| 550 | { |
| 551 | struct v4l2_create_buffers bcreate = { 0 }; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 552 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 553 | flags |= breq_flag; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 554 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 555 | bcreate.memory = memory; |
| 556 | bcreate.format = obj->format; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 557 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 558 | if ((obj->ioctl (obj->video_fd, VIDIOC_CREATE_BUFS, &bcreate) == 0)) |
| 559 | flags |= bcreate_flag; |
| 560 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 561 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 562 | if (breq.capabilities & V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS) |
| 563 | { |
| 564 | flags |= GST_V4L2_ALLOCATOR_FLAG_SUPPORTS_ORPHANED_BUFS; |
| 565 | GST_DEBUG_OBJECT(allocator, "v4l2 support GST_V4L2_ALLOCATOR_FLAG_SUPPORTS_ORPHANED_BUFS"); |
| 566 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 567 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 568 | return flags; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | static GstAmlV4l2MemoryGroup * |
| 572 | gst_aml_v4l2_allocator_create_buf(GstAmlV4l2Allocator *allocator) |
| 573 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 574 | GstAmlV4l2Object *obj = allocator->obj; |
| 575 | struct v4l2_create_buffers bcreate = { 0 }; |
| 576 | GstAmlV4l2MemoryGroup *group = NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 577 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 578 | GST_OBJECT_LOCK (allocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 579 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 580 | if (!g_atomic_int_get (&allocator->active)) |
| 581 | goto done; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 582 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 583 | if (GST_AML_V4L2_ALLOCATOR_IS_ORPHANED(allocator)) |
| 584 | goto orphaned_bug; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 585 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 586 | bcreate.memory = allocator->memory; |
| 587 | bcreate.format = obj->format; |
| 588 | bcreate.count = 1; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 589 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 590 | if (!allocator->can_allocate) |
| 591 | goto done; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 592 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 593 | if (obj->ioctl (obj->video_fd, VIDIOC_CREATE_BUFS, &bcreate) < 0) |
| 594 | goto create_bufs_failed; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 595 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 596 | if (allocator->groups[bcreate.index] != NULL) |
| 597 | goto create_bufs_bug; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 598 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 599 | group = gst_aml_v4l2_memory_group_new(allocator, bcreate.index); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 600 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 601 | if (group) |
| 602 | { |
| 603 | allocator->groups[bcreate.index] = group; |
| 604 | allocator->count++; |
| 605 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 606 | |
| 607 | done: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 608 | GST_OBJECT_UNLOCK (allocator); |
| 609 | return group; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 610 | |
| 611 | orphaned_bug: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 612 | { |
| 613 | GST_ERROR_OBJECT (allocator, "allocator was orphaned, " |
| 614 | "not creating new buffers"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 615 | goto done; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 616 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 617 | create_bufs_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 618 | { |
| 619 | GST_WARNING_OBJECT (allocator, "error creating a new buffer: %s", |
| 620 | g_strerror (errno)); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 621 | goto done; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 622 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 623 | create_bufs_bug: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 624 | { |
| 625 | GST_ERROR_OBJECT (allocator, "created buffer has already used buffer " |
| 626 | "index %i, this means there is an bug in your driver or libv4l2", |
| 627 | bcreate.index); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 628 | goto done; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 629 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | static GstAmlV4l2MemoryGroup * |
| 633 | gst_aml_v4l2_allocator_alloc(GstAmlV4l2Allocator *allocator) |
| 634 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 635 | GstAmlV4l2MemoryGroup *group; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 636 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 637 | if (!g_atomic_int_get (&allocator->active)) |
| 638 | return NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 639 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 640 | group = gst_atomic_queue_pop (allocator->free_queue); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 641 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 642 | if (group == NULL) |
| 643 | { |
| 644 | if (allocator->can_allocate) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 645 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 646 | group = gst_aml_v4l2_allocator_create_buf(allocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 647 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 648 | /* Don't hammer on CREATE_BUFS */ |
| 649 | if (group == NULL) |
| 650 | allocator->can_allocate = FALSE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 651 | } |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 652 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 653 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 654 | return group; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | static void |
| 658 | gst_aml_v4l2_allocator_reset_size(GstAmlV4l2Allocator *allocator, |
| 659 | GstAmlV4l2MemoryGroup *group) |
| 660 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 661 | gint i; |
| 662 | for (i = 0; i < group->n_mem; i++) |
| 663 | { |
| 664 | group->mem[i]->maxsize = group->planes[i].length; |
| 665 | group->mem[i]->offset = 0; |
| 666 | group->mem[i]->size = group->planes[i].length; |
| 667 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static void |
| 671 | _cleanup_failed_alloc(GstAmlV4l2Allocator *allocator, GstAmlV4l2MemoryGroup *group) |
| 672 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 673 | if (group->mems_allocated > 0) |
| 674 | { |
| 675 | gint i; |
| 676 | /* If one or more mmap worked, we need to unref the memory, otherwise |
| 677 | * they will keep a ref on the allocator and leak it. This will put back |
| 678 | * the group into the free_queue */ |
| 679 | for (i = 0; i < group->n_mem; i++) |
| 680 | gst_memory_unref (group->mem[i]); |
| 681 | } |
| 682 | else |
| 683 | { |
| 684 | /* Otherwise, group has to be on free queue for _stop() to work */ |
| 685 | gst_atomic_queue_push (allocator->free_queue, group); |
| 686 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | GstAmlV4l2Allocator * |
| 690 | gst_aml_v4l2_allocator_new(GstObject *parent, GstAmlV4l2Object *v4l2object) |
| 691 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 692 | GstAmlV4l2Allocator *allocator; |
| 693 | guint32 flags = 0; |
| 694 | gchar *name, *parent_name; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 695 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 696 | parent_name = gst_object_get_name (parent); |
| 697 | name = g_strconcat (parent_name, ":allocator", NULL); |
| 698 | g_free (parent_name); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 699 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 700 | allocator = g_object_new(GST_TYPE_AML_V4L2_ALLOCATOR, "name", name, NULL); |
| 701 | gst_object_ref_sink (allocator); |
| 702 | g_free (name); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 703 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 704 | /* Save everything */ |
| 705 | allocator->obj = v4l2object; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 706 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 707 | /* Keep a ref on the element so obj does not disappear */ |
| 708 | gst_object_ref (allocator->obj->element); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 709 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 710 | flags |= GST_AML_V4L2_ALLOCATOR_PROBE(allocator, MMAP); |
| 711 | flags |= GST_AML_V4L2_ALLOCATOR_PROBE(allocator, USERPTR); |
| 712 | flags |= GST_AML_V4L2_ALLOCATOR_PROBE(allocator, DMABUF); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 713 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 714 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 715 | if (flags == 0) |
| 716 | { |
| 717 | /* Drivers not ported from videobuf to videbuf2 don't allow freeing buffers |
| 718 | * using REQBUFS(0). This is a workaround to still support these drivers, |
| 719 | * which are known to have MMAP support. */ |
| 720 | GST_WARNING_OBJECT (allocator, "Could not probe supported memory type, " |
| 721 | "assuming MMAP is supported, this is expected for older drivers not " |
| 722 | " yet ported to videobuf2 framework"); |
| 723 | flags = GST_V4L2_ALLOCATOR_FLAG_MMAP_REQBUFS; |
| 724 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 725 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 726 | GST_OBJECT_FLAG_SET (allocator, flags); |
| 727 | |
| 728 | return allocator; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 729 | } |
| 730 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 731 | guint |
| 732 | gst_aml_v4l2_allocator_start(GstAmlV4l2Allocator *allocator, guint32 count, |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 733 | guint32 memory) |
| 734 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 735 | GstAmlV4l2Object *obj = allocator->obj; |
| 736 | struct v4l2_requestbuffers breq = { count, obj->type, memory }; |
| 737 | gboolean can_allocate; |
| 738 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 739 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 740 | g_return_val_if_fail (count != 0, 0); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 741 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 742 | GST_OBJECT_LOCK (allocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 743 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 744 | if (g_atomic_int_get (&allocator->active)) |
| 745 | goto already_active; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 746 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 747 | if (GST_AML_V4L2_ALLOCATOR_IS_ORPHANED(allocator)) |
| 748 | goto orphaned; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 749 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 750 | if (obj->ioctl (obj->video_fd, VIDIOC_REQBUFS, &breq) < 0) |
| 751 | goto reqbufs_failed; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 752 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 753 | if (breq.count < 1) |
| 754 | goto out_of_memory; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 755 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 756 | switch (memory) |
| 757 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 758 | case V4L2_MEMORY_MMAP: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 759 | can_allocate = GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(allocator, MMAP); |
| 760 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 761 | case V4L2_MEMORY_USERPTR: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 762 | can_allocate = GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(allocator, USERPTR); |
| 763 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 764 | case V4L2_MEMORY_DMABUF: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 765 | can_allocate = GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(allocator, DMABUF); |
| 766 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 767 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 768 | can_allocate = FALSE; |
| 769 | break; |
| 770 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 771 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 772 | GST_DEBUG_OBJECT (allocator, "allocated %u %s buffers out of %u requested", |
| 773 | breq.count, memory_type_to_str (memory), count); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 774 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 775 | allocator->can_allocate = can_allocate; |
| 776 | allocator->count = breq.count; |
| 777 | allocator->memory = memory; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 778 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 779 | /* Create memory groups */ |
| 780 | for (i = 0; i < allocator->count; i++) |
| 781 | { |
| 782 | allocator->groups[i] = gst_aml_v4l2_memory_group_new(allocator, i); |
| 783 | if (allocator->groups[i] == NULL) |
| 784 | goto error; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 785 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 786 | gst_atomic_queue_push (allocator->free_queue, allocator->groups[i]); |
| 787 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 788 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 789 | g_atomic_int_set (&allocator->active, TRUE); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 790 | |
| 791 | done: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 792 | GST_OBJECT_UNLOCK (allocator); |
| 793 | return breq.count; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 794 | |
| 795 | already_active: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 796 | { |
| 797 | GST_ERROR_OBJECT (allocator, "allocator already active"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 798 | goto error; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 799 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 800 | orphaned: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 801 | { |
| 802 | GST_ERROR_OBJECT (allocator, "allocator was orphaned"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 803 | goto error; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 804 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 805 | reqbufs_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 806 | { |
| 807 | GST_ERROR_OBJECT (allocator, |
| 808 | "error requesting %d buffers: %s", count, g_strerror (errno)); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 809 | goto error; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 810 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 811 | out_of_memory: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 812 | { |
| 813 | GST_ERROR_OBJECT (allocator, "Not enough memory to allocate buffers"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 814 | goto error; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 815 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 816 | error: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 817 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 818 | breq.count = 0; |
| 819 | goto done; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 820 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | GstAmlV4l2Return |
| 824 | gst_aml_v4l2_allocator_stop(GstAmlV4l2Allocator *allocator) |
| 825 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 826 | GstAmlV4l2Object *obj = allocator->obj; |
| 827 | struct v4l2_requestbuffers breq = { 0, obj->type, allocator->memory }; |
| 828 | gint i = 0; |
| 829 | GstAmlV4l2Return ret = GST_AML_V4L2_OK; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 830 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 831 | GST_DEBUG_OBJECT (allocator, "stop allocator"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 832 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 833 | GST_OBJECT_LOCK (allocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 834 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 835 | if (!g_atomic_int_get (&allocator->active)) |
| 836 | goto done; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 837 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 838 | if (gst_atomic_queue_length(allocator->free_queue) != allocator->count) |
| 839 | { |
| 840 | GST_DEBUG_OBJECT (allocator, "allocator is still in use"); |
| 841 | ret = GST_AML_V4L2_BUSY; |
| 842 | goto done; |
| 843 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 844 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 845 | while (gst_atomic_queue_pop(allocator->free_queue)) |
| 846 | { |
| 847 | /* nothing */ |
| 848 | }; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 849 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 850 | for (i = 0; i < allocator->count; i++) |
| 851 | { |
| 852 | GstAmlV4l2MemoryGroup *group = allocator->groups[i]; |
| 853 | allocator->groups[i] = NULL; |
| 854 | if (group) |
| 855 | gst_aml_v4l2_memory_group_free(group); |
| 856 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 857 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 858 | if (!GST_AML_V4L2_ALLOCATOR_IS_ORPHANED(allocator)) |
| 859 | { |
| 860 | /* Not all drivers support rebufs(0), so warn only */ |
| 861 | if (obj->ioctl (obj->video_fd, VIDIOC_REQBUFS, &breq) < 0) |
| 862 | GST_WARNING_OBJECT (allocator, |
| 863 | "error releasing buffers buffers: %s", g_strerror (errno)); |
| 864 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 865 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 866 | allocator->count = 0; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 867 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 868 | g_atomic_int_set (&allocator->active, FALSE); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 869 | |
| 870 | done: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 871 | GST_OBJECT_UNLOCK (allocator); |
| 872 | return ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | gboolean |
| 876 | gst_aml_v4l2_allocator_orphan(GstAmlV4l2Allocator *allocator) |
| 877 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 878 | GstAmlV4l2Object *obj = allocator->obj; |
| 879 | struct v4l2_requestbuffers breq = { 0, obj->type, allocator->memory }; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 880 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 881 | if (!GST_AML_V4L2_ALLOCATOR_CAN_ORPHAN_BUFS(allocator)) |
| 882 | return FALSE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 883 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 884 | GST_OBJECT_FLAG_SET (allocator, GST_V4L2_ALLOCATOR_FLAG_ORPHANED); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 885 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 886 | if (obj->ioctl(obj->video_fd, VIDIOC_REQBUFS, &breq) < 0) |
| 887 | { |
| 888 | GST_ERROR_OBJECT (allocator, |
| 889 | "error orphaning buffers buffers: %s", g_strerror (errno)); |
| 890 | return FALSE; |
| 891 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 892 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 893 | return TRUE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | GstAmlV4l2MemoryGroup * |
| 897 | gst_aml_v4l2_allocator_alloc_mmap(GstAmlV4l2Allocator *allocator) |
| 898 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 899 | GstAmlV4l2Object *obj = allocator->obj; |
| 900 | GstAmlV4l2MemoryGroup *group; |
| 901 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 902 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 903 | g_return_val_if_fail (allocator->memory == V4L2_MEMORY_MMAP, NULL); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 904 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 905 | group = gst_aml_v4l2_allocator_alloc(allocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 906 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 907 | if (group == NULL) |
| 908 | return NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 909 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 910 | for (i = 0; i < group->n_mem; i++) |
| 911 | { |
| 912 | if (group->mem[i] == NULL) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 913 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 914 | gpointer data; |
| 915 | data = obj->mmap (NULL, group->planes[i].length, PROT_READ | PROT_WRITE, |
| 916 | MAP_SHARED, obj->video_fd, group->planes[i].m.mem_offset); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 917 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 918 | if (data == MAP_FAILED) |
| 919 | goto mmap_failed; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 920 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 921 | GST_LOG_OBJECT (allocator, |
| 922 | "mmap buffer length %d, data offset %d, plane %d", |
| 923 | group->planes[i].length, group->planes[i].data_offset, i); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 924 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 925 | group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator), |
| 926 | NULL, group->planes[i].length, 0, 0, group->planes[i].length, i, data, |
| 927 | -1, group); |
| 928 | } |
| 929 | else |
| 930 | { |
| 931 | /* Take back the allocator reference */ |
| 932 | gst_object_ref (allocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 933 | } |
| 934 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 935 | group->mems_allocated++; |
| 936 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 937 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 938 | /* Ensure group size. Unlike GST, v4l2 have size (bytesused) initially set |
| 939 | * to 0. As length might be bigger then the expected size exposed in the |
| 940 | * format, we simply set bytesused initially and reset it here for |
| 941 | * simplicity */ |
| 942 | gst_aml_v4l2_allocator_reset_size(allocator, group); |
| 943 | |
| 944 | return group; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 945 | |
| 946 | mmap_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 947 | { |
| 948 | GST_ERROR_OBJECT (allocator, "Failed to mmap buffer: %s", |
| 949 | g_strerror (errno)); |
| 950 | _cleanup_failed_alloc (allocator, group); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 951 | return NULL; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 952 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | GstAmlV4l2MemoryGroup * |
| 956 | gst_aml_v4l2_allocator_alloc_dmabuf(GstAmlV4l2Allocator *allocator, |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 957 | GstAllocator * dmabuf_allocator) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 958 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 959 | GstAmlV4l2Object *obj = allocator->obj; |
| 960 | GstAmlV4l2MemoryGroup *group; |
| 961 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 962 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 963 | g_return_val_if_fail (allocator->memory == V4L2_MEMORY_MMAP, NULL); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 964 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 965 | group = gst_aml_v4l2_allocator_alloc(allocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 966 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 967 | if (group == NULL) |
| 968 | return NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 969 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 970 | for (i = 0; i < group->n_mem; i++) |
| 971 | { |
| 972 | GstAmlV4l2Memory *mem; |
| 973 | GstMemory *dma_mem; |
| 974 | |
| 975 | if (group->mem[i] == NULL) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 976 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 977 | struct v4l2_exportbuffer expbuf = { 0 }; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 978 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 979 | expbuf.type = obj->type; |
| 980 | expbuf.index = group->buffer.index; |
| 981 | expbuf.plane = i; |
| 982 | expbuf.flags = O_CLOEXEC | O_RDWR; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 983 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 984 | if (obj->ioctl (obj->video_fd, VIDIOC_EXPBUF, &expbuf) < 0) |
| 985 | goto expbuf_failed; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 986 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 987 | GST_LOG_OBJECT (allocator, "exported DMABUF as fd %i plane %d", |
| 988 | expbuf.fd, i); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 989 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 990 | group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator), |
| 991 | NULL, group->planes[i].length, 0, group->planes[i].data_offset, |
| 992 | group->planes[i].length - group->planes[i].data_offset, i, NULL, |
| 993 | expbuf.fd, group); |
| 994 | } |
| 995 | else |
| 996 | { |
| 997 | /* Take back the allocator reference */ |
| 998 | gst_object_ref (allocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 999 | } |
| 1000 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1001 | group->mems_allocated++; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1002 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1003 | g_assert(gst_aml_is_v4l2_memory(group->mem[i])); |
| 1004 | mem = (GstAmlV4l2Memory *)group->mem[i]; |
| 1005 | |
| 1006 | dma_mem = gst_fd_allocator_alloc (dmabuf_allocator, mem->dmafd, |
| 1007 | group->planes[i].length, GST_FD_MEMORY_FLAG_DONT_CLOSE); |
| 1008 | gst_memory_resize (dma_mem, group->planes[i].data_offset, |
| 1009 | group->planes[i].length - group->planes[i].data_offset); |
| 1010 | |
| 1011 | gst_mini_object_set_qdata (GST_MINI_OBJECT (dma_mem), |
| 1012 | GST_AML_V4L2_MEMORY_QUARK, mem, (GDestroyNotify)gst_memory_unref); |
| 1013 | |
| 1014 | group->mem[i] = dma_mem; |
| 1015 | } |
| 1016 | |
| 1017 | gst_aml_v4l2_allocator_reset_size(allocator, group); |
| 1018 | |
| 1019 | return group; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1020 | |
| 1021 | expbuf_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1022 | { |
| 1023 | GST_ERROR_OBJECT (allocator, "Failed to export DMABUF: %s", |
| 1024 | g_strerror (errno)); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1025 | goto cleanup; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1026 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1027 | cleanup: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1028 | { |
| 1029 | _cleanup_failed_alloc (allocator, group); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1030 | return NULL; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1031 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | static void |
| 1035 | gst_aml_v4l2_allocator_clear_dmabufin(GstAmlV4l2Allocator *allocator, |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1036 | GstAmlV4l2MemoryGroup *group) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1037 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1038 | GstAmlV4l2Object *obj = allocator->obj; |
| 1039 | GstAmlV4l2Memory *mem; |
| 1040 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1041 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1042 | g_return_if_fail (allocator->memory == V4L2_MEMORY_DMABUF); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1043 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1044 | for (i = 0; i < group->n_mem; i++) |
| 1045 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1046 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1047 | mem = (GstAmlV4l2Memory *)group->mem[i]; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1048 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1049 | GST_LOG_OBJECT (allocator, "[%i] clearing DMABUF import, fd %i plane %d", |
| 1050 | group->buffer.index, mem->dmafd, i); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1051 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1052 | /* Update memory */ |
| 1053 | mem->mem.maxsize = 0; |
| 1054 | mem->mem.offset = 0; |
| 1055 | mem->mem.size = 0; |
| 1056 | mem->dmafd = -1; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1057 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1058 | /* Update v4l2 structure */ |
| 1059 | group->planes[i].length = 0; |
| 1060 | group->planes[i].bytesused = 0; |
| 1061 | group->planes[i].m.fd = -1; |
| 1062 | group->planes[i].data_offset = 0; |
| 1063 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1064 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1065 | if (!V4L2_TYPE_IS_MULTIPLANAR(obj->type)) |
| 1066 | { |
| 1067 | group->buffer.bytesused = 0; |
| 1068 | group->buffer.length = 0; |
| 1069 | group->buffer.m.fd = -1; |
| 1070 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | GstAmlV4l2MemoryGroup * |
| 1074 | gst_aml_v4l2_allocator_alloc_dmabufin(GstAmlV4l2Allocator *allocator) |
| 1075 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1076 | GstAmlV4l2MemoryGroup *group; |
| 1077 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1078 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1079 | g_return_val_if_fail (allocator->memory == V4L2_MEMORY_DMABUF, NULL); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1080 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1081 | group = gst_aml_v4l2_allocator_alloc(allocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1082 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1083 | if (group == NULL) |
| 1084 | return NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1085 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1086 | GST_LOG_OBJECT (allocator, "allocating empty DMABUF import group"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1087 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1088 | for (i = 0; i < group->n_mem; i++) |
| 1089 | { |
| 1090 | if (group->mem[i] == NULL) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1091 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1092 | group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator), |
| 1093 | NULL, 0, 0, 0, 0, i, NULL, -1, group); |
| 1094 | } |
| 1095 | else |
| 1096 | { |
| 1097 | /* Take back the allocator reference */ |
| 1098 | gst_object_ref (allocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1099 | } |
| 1100 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1101 | group->mems_allocated++; |
| 1102 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1103 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1104 | gst_aml_v4l2_allocator_clear_dmabufin(allocator, group); |
| 1105 | |
| 1106 | return group; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | static void |
| 1110 | gst_aml_v4l2_allocator_clear_userptr(GstAmlV4l2Allocator *allocator, |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1111 | GstAmlV4l2MemoryGroup *group) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1112 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1113 | GstAmlV4l2Object *obj = allocator->obj; |
| 1114 | GstAmlV4l2Memory *mem; |
| 1115 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1116 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1117 | g_return_if_fail (allocator->memory == V4L2_MEMORY_USERPTR); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1118 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1119 | for (i = 0; i < group->n_mem; i++) |
| 1120 | { |
| 1121 | mem = (GstAmlV4l2Memory *)group->mem[i]; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1122 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1123 | GST_LOG_OBJECT (allocator, "[%i] clearing USERPTR %p plane %d size %" |
| 1124 | G_GSIZE_FORMAT, group->buffer.index, mem->data, i, mem->mem.size); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1125 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1126 | mem->mem.maxsize = 0; |
| 1127 | mem->mem.size = 0; |
| 1128 | mem->data = NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1129 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1130 | group->planes[i].length = 0; |
| 1131 | group->planes[i].bytesused = 0; |
| 1132 | group->planes[i].m.userptr = 0; |
| 1133 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1134 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1135 | if (!V4L2_TYPE_IS_MULTIPLANAR(obj->type)) |
| 1136 | { |
| 1137 | group->buffer.bytesused = 0; |
| 1138 | group->buffer.length = 0; |
| 1139 | group->buffer.m.userptr = 0; |
| 1140 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | GstAmlV4l2MemoryGroup * |
| 1144 | gst_aml_v4l2_allocator_alloc_userptr(GstAmlV4l2Allocator *allocator) |
| 1145 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1146 | GstAmlV4l2MemoryGroup *group; |
| 1147 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1148 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1149 | g_return_val_if_fail (allocator->memory == V4L2_MEMORY_USERPTR, NULL); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1150 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1151 | group = gst_aml_v4l2_allocator_alloc (allocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1152 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1153 | if (group == NULL) |
| 1154 | return NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1155 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1156 | GST_LOG_OBJECT (allocator, "allocating empty USERPTR group"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1157 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1158 | for (i = 0; i < group->n_mem; i++) |
| 1159 | { |
| 1160 | |
| 1161 | if (group->mem[i] == NULL) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1162 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1163 | group->mem[i] = (GstMemory *) _v4l2mem_new (0, GST_ALLOCATOR (allocator), |
| 1164 | NULL, 0, 0, 0, 0, i, NULL, -1, group); |
| 1165 | } |
| 1166 | else |
| 1167 | { |
| 1168 | /* Take back the allocator reference */ |
| 1169 | gst_object_ref (allocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1170 | } |
| 1171 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1172 | group->mems_allocated++; |
| 1173 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1174 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1175 | gst_aml_v4l2_allocator_clear_userptr(allocator, group); |
| 1176 | |
| 1177 | return group; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | gboolean |
| 1181 | gst_aml_v4l2_allocator_import_dmabuf(GstAmlV4l2Allocator *allocator, |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1182 | GstAmlV4l2MemoryGroup *group, gint n_mem, GstMemory **dma_mem) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1183 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1184 | GstAmlV4l2Object *obj = allocator->obj; |
| 1185 | GstAmlV4l2Memory *mem; |
| 1186 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1187 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1188 | g_return_val_if_fail (allocator->memory == V4L2_MEMORY_DMABUF, FALSE); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1189 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1190 | if (group->n_mem != n_mem) |
| 1191 | goto n_mem_missmatch; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1192 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1193 | for (i = 0; i < group->n_mem; i++) |
| 1194 | { |
| 1195 | gint dmafd; |
| 1196 | gsize size, offset, maxsize; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1197 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1198 | if (!gst_is_dmabuf_memory (dma_mem[i])) |
| 1199 | goto not_dmabuf; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1200 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1201 | size = gst_memory_get_sizes (dma_mem[i], &offset, &maxsize); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1202 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1203 | dmafd = gst_dmabuf_memory_get_fd (dma_mem[i]); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1204 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1205 | GST_LOG_OBJECT (allocator, "[%i] imported DMABUF as fd %i plane %d", |
| 1206 | group->buffer.index, dmafd, i); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1207 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1208 | mem = (GstAmlV4l2Memory *) group->mem[i]; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1209 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1210 | /* Update memory */ |
| 1211 | mem->mem.maxsize = maxsize; |
| 1212 | mem->mem.offset = offset; |
| 1213 | mem->mem.size = size; |
| 1214 | mem->dmafd = dmafd; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1215 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1216 | /* Update v4l2 structure */ |
| 1217 | group->planes[i].length = maxsize; |
| 1218 | group->planes[i].bytesused = size + offset; |
| 1219 | group->planes[i].m.fd = dmafd; |
| 1220 | group->planes[i].data_offset = offset; |
| 1221 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1222 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1223 | /* Copy into buffer structure if not using planes */ |
| 1224 | if (!V4L2_TYPE_IS_MULTIPLANAR(obj->type)) |
| 1225 | { |
| 1226 | group->buffer.bytesused = group->planes[0].bytesused; |
| 1227 | group->buffer.length = group->planes[0].length; |
| 1228 | group->buffer.m.fd = group->planes[0].m.fd; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1229 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1230 | /* FIXME Check if data_offset > 0 and fail for non-multi-planar */ |
| 1231 | g_assert (group->planes[0].data_offset == 0); |
| 1232 | } |
| 1233 | else |
| 1234 | { |
| 1235 | group->buffer.length = group->n_mem; |
| 1236 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1237 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1238 | return TRUE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1239 | |
| 1240 | n_mem_missmatch: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1241 | { |
| 1242 | GST_ERROR_OBJECT (allocator, "Got %i dmabuf but needed %i", n_mem, |
| 1243 | group->n_mem); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1244 | return FALSE; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1245 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1246 | not_dmabuf: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1247 | { |
| 1248 | GST_ERROR_OBJECT (allocator, "Memory %i is not of DMABUF", i); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1249 | return FALSE; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1250 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | gboolean |
| 1254 | gst_aml_v4l2_allocator_import_userptr(GstAmlV4l2Allocator *allocator, |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1255 | GstAmlV4l2MemoryGroup *group, gsize img_size, int n_planes, |
| 1256 | gpointer * data, gsize * size) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1257 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1258 | GstAmlV4l2Object *obj = allocator->obj; |
| 1259 | GstAmlV4l2Memory *mem; |
| 1260 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1261 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1262 | g_return_val_if_fail (allocator->memory == V4L2_MEMORY_USERPTR, FALSE); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1263 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1264 | /* TODO Support passing N plane from 1 memory to MPLANE v4l2 format */ |
| 1265 | if (V4L2_TYPE_IS_MULTIPLANAR (obj->type) && n_planes != group->n_mem) |
| 1266 | goto n_mem_missmatch; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1267 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1268 | for (i = 0; i < group->n_mem; i++) |
| 1269 | { |
| 1270 | gsize maxsize, psize; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1271 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1272 | /* TODO request used size and maxsize separately */ |
| 1273 | if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) |
| 1274 | maxsize = psize = size[i]; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1275 | else |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1276 | maxsize = psize = img_size; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1277 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1278 | g_assert (psize <= img_size); |
| 1279 | |
| 1280 | GST_LOG_OBJECT(allocator, "[%i] imported USERPTR %p plane %d size %" G_GSIZE_FORMAT, group->buffer.index, data[i], i, psize); |
| 1281 | |
| 1282 | mem = (GstAmlV4l2Memory *)group->mem[i]; |
| 1283 | |
| 1284 | mem->mem.maxsize = maxsize; |
| 1285 | mem->mem.size = psize; |
| 1286 | mem->data = data[i]; |
| 1287 | |
| 1288 | group->planes[i].length = maxsize; |
| 1289 | group->planes[i].bytesused = psize; |
| 1290 | group->planes[i].m.userptr = (unsigned long) data[i]; |
| 1291 | group->planes[i].data_offset = 0; |
| 1292 | } |
| 1293 | |
| 1294 | /* Copy into buffer structure if not using planes */ |
| 1295 | if (!V4L2_TYPE_IS_MULTIPLANAR(obj->type)) |
| 1296 | { |
| 1297 | group->buffer.bytesused = group->planes[0].bytesused; |
| 1298 | group->buffer.length = group->planes[0].length; |
| 1299 | group->buffer.m.userptr = group->planes[0].m.userptr; |
| 1300 | } |
| 1301 | else |
| 1302 | { |
| 1303 | group->buffer.length = group->n_mem; |
| 1304 | } |
| 1305 | |
| 1306 | return TRUE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1307 | |
| 1308 | n_mem_missmatch: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1309 | { |
| 1310 | GST_ERROR_OBJECT (allocator, "Got %i userptr plane while driver need %i", |
| 1311 | n_planes, group->n_mem); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1312 | return FALSE; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1313 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | void gst_aml_v4l2_allocator_flush(GstAmlV4l2Allocator *allocator) |
| 1317 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1318 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1319 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1320 | GST_OBJECT_LOCK (allocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1321 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1322 | if (!g_atomic_int_get (&allocator->active)) |
| 1323 | goto done; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1324 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1325 | for (i = 0; i < allocator->count; i++) |
| 1326 | { |
| 1327 | GstAmlV4l2MemoryGroup *group = allocator->groups[i]; |
| 1328 | gint n; |
| 1329 | |
| 1330 | if (IS_QUEUED (group->buffer)) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1331 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1332 | UNSET_QUEUED (group->buffer); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1333 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1334 | gst_aml_v4l2_allocator_reset_group (allocator, group); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1335 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1336 | for (n = 0; n < group->n_mem; n++) |
| 1337 | gst_memory_unref (group->mem[n]); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1338 | } |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1339 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1340 | |
| 1341 | done: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1342 | GST_OBJECT_UNLOCK (allocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | gboolean |
| 1346 | gst_aml_v4l2_allocator_qbuf(GstAmlV4l2Allocator *allocator, |
| 1347 | GstAmlV4l2MemoryGroup *group) |
| 1348 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1349 | GstAmlV4l2Object *obj = allocator->obj; |
| 1350 | gboolean ret = TRUE; |
| 1351 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1352 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1353 | g_return_val_if_fail (g_atomic_int_get (&allocator->active), FALSE); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1354 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1355 | /* update sizes */ |
| 1356 | if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) |
| 1357 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1358 | for (i = 0; i < group->n_mem; i++) |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1359 | group->planes[i].bytesused = |
| 1360 | gst_memory_get_sizes (group->mem[i], NULL, NULL); |
| 1361 | } |
| 1362 | else |
| 1363 | { |
| 1364 | group->buffer.bytesused = gst_memory_get_sizes (group->mem[0], NULL, NULL); |
| 1365 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1366 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1367 | /* Ensure the memory will stay around and is RO */ |
| 1368 | for (i = 0; i < group->n_mem; i++) |
| 1369 | gst_memory_ref (group->mem[i]); |
fei.deng | bee2086 | 2022-06-14 14:59:48 +0800 | [diff] [blame] | 1370 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1371 | gint64 currFramePTS= 0; |
| 1372 | if (group->buffer.timestamp.tv_sec != -1) { |
| 1373 | currFramePTS= group->buffer.timestamp.tv_sec * 1000000LL + group->buffer.timestamp.tv_usec; |
| 1374 | } |
xuesong.jiang | 5fccce2 | 2024-10-22 20:28:50 +0800 | [diff] [blame] | 1375 | GST_LOG_OBJECT(allocator, "q buffer, timestamp:%lld(us), tv_sec:%ld, tv_usec:%ld",currFramePTS, group->buffer.timestamp.tv_sec, group->buffer.timestamp.tv_usec); |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame] | 1376 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1377 | gst_aml_v4l2_allocator_dump_es_buf(allocator, group); |
| 1378 | |
| 1379 | if (obj->ioctl(obj->video_fd, VIDIOC_QBUF, &group->buffer) < 0) |
| 1380 | { |
| 1381 | GST_ERROR_OBJECT (allocator, "failed queueing buffer %i: %s", |
| 1382 | group->buffer.index, g_strerror (errno)); |
| 1383 | |
| 1384 | /* Release the memory, possibly making it RW again */ |
| 1385 | for (i = 0; i < group->n_mem; i++) |
| 1386 | gst_memory_unref (group->mem[i]); |
| 1387 | |
| 1388 | ret = FALSE; |
| 1389 | if (IS_QUEUED(group->buffer)) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1390 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1391 | GST_DEBUG_OBJECT (allocator, |
| 1392 | "driver pretends buffer is queued even if queue failed"); |
| 1393 | UNSET_QUEUED (group->buffer); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1394 | } |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1395 | goto done; |
| 1396 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1397 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1398 | GST_LOG_OBJECT (allocator, "queued buffer %i (flags 0x%X)", |
| 1399 | group->buffer.index, group->buffer.flags); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1400 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1401 | if (!IS_QUEUED(group->buffer)) |
| 1402 | { |
| 1403 | GST_DEBUG_OBJECT (allocator, |
| 1404 | "driver pretends buffer is not queued even if queue succeeded"); |
| 1405 | SET_QUEUED (group->buffer); |
| 1406 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1407 | |
| 1408 | done: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1409 | return ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | GstFlowReturn |
| 1413 | gst_aml_v4l2_allocator_dqbuf(GstAmlV4l2Allocator *allocator, |
| 1414 | GstAmlV4l2MemoryGroup **group_out) |
| 1415 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1416 | GstAmlV4l2Object *obj = allocator->obj; |
| 1417 | struct v4l2_buffer buffer = { 0 }; |
| 1418 | struct v4l2_plane planes[VIDEO_MAX_PLANES] = { {0} }; |
| 1419 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1420 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1421 | GstAmlV4l2MemoryGroup *group = NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1422 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1423 | g_return_val_if_fail (g_atomic_int_get (&allocator->active), GST_FLOW_ERROR); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1424 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1425 | buffer.type = obj->type; |
| 1426 | buffer.memory = allocator->memory; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1427 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1428 | if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) |
| 1429 | { |
| 1430 | buffer.length = obj->format.fmt.pix_mp.num_planes; |
| 1431 | buffer.m.planes = planes; |
| 1432 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1433 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1434 | if (obj->ioctl (obj->video_fd, VIDIOC_DQBUF, &buffer) < 0) |
| 1435 | goto error; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1436 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1437 | group = allocator->groups[buffer.index]; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1438 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1439 | if (!IS_QUEUED(group->buffer)) |
| 1440 | { |
| 1441 | GST_ERROR_OBJECT (allocator, |
| 1442 | "buffer %i was not queued, this indicate a driver bug.", buffer.index); |
| 1443 | return GST_FLOW_ERROR; |
| 1444 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1445 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1446 | group->buffer = buffer; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1447 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1448 | GST_LOG_OBJECT (allocator, "dequeued buffer %i (flags 0x%X)", buffer.index, |
| 1449 | buffer.flags); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1450 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1451 | if (IS_QUEUED(group->buffer)) |
| 1452 | { |
| 1453 | GST_DEBUG_OBJECT (allocator, |
| 1454 | "driver pretends buffer is queued even if dequeue succeeded"); |
| 1455 | UNSET_QUEUED (group->buffer); |
| 1456 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1457 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1458 | if (V4L2_TYPE_IS_MULTIPLANAR(obj->type)) |
| 1459 | { |
| 1460 | group->buffer.m.planes = group->planes; |
| 1461 | memcpy (group->planes, buffer.m.planes, sizeof (planes)); |
| 1462 | } |
| 1463 | else |
| 1464 | { |
| 1465 | group->planes[0].bytesused = group->buffer.bytesused; |
| 1466 | group->planes[0].length = group->buffer.length; |
| 1467 | g_assert (sizeof (group->planes[0].m) == sizeof (group->buffer.m)); |
| 1468 | memcpy (&group->planes[0].m, &group->buffer.m, sizeof (group->buffer.m)); |
| 1469 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1470 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1471 | /* And update memory size */ |
| 1472 | if (V4L2_TYPE_IS_OUTPUT(obj->type)) |
| 1473 | { |
| 1474 | gst_aml_v4l2_allocator_reset_size(allocator, group); |
| 1475 | } |
| 1476 | else |
| 1477 | { |
| 1478 | /* for capture, simply read the size */ |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1479 | for (i = 0; i < group->n_mem; i++) |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1480 | { |
| 1481 | gsize size, offset; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1482 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1483 | GST_LOG_OBJECT (allocator, |
| 1484 | "Dequeued capture buffer, length: %u bytesused: %u data_offset: %u", |
| 1485 | group->planes[i].length, group->planes[i].bytesused, |
| 1486 | group->planes[i].data_offset); |
| 1487 | |
| 1488 | offset = group->planes[i].data_offset; |
| 1489 | |
| 1490 | if (group->planes[i].bytesused > group->planes[i].data_offset) |
| 1491 | { |
| 1492 | size = group->planes[i].bytesused - group->planes[i].data_offset; |
| 1493 | } |
| 1494 | else |
| 1495 | { |
| 1496 | GST_WARNING_OBJECT(allocator, "V4L2 provided buffer has bytesused %" G_GUINT32_FORMAT " which is too small to include data_offset %" G_GUINT32_FORMAT, group->planes[i].bytesused, |
| 1497 | group->planes[i].data_offset); |
| 1498 | size = group->planes[i].bytesused; |
| 1499 | /*if this buffer is last buffer,bytesused is 0,so size equal 0,this cause assert fail of gst_memory_resize*/ |
| 1500 | if ( (group->buffer.flags & V4L2_BUF_FLAG_LAST) &&(group->buffer.bytesused == 0)) |
| 1501 | { |
| 1502 | size = group->planes[i].length; |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | if (G_LIKELY (size + offset <= group->mem[i]->maxsize)) |
| 1507 | gst_memory_resize (group->mem[i], offset, size); |
| 1508 | else |
| 1509 | { |
| 1510 | GST_WARNING_OBJECT (allocator, |
| 1511 | "v4l2 provided buffer that is too big for the memory it was " |
| 1512 | "writing into. v4l2 claims %" G_GSIZE_FORMAT " bytes used but " |
| 1513 | "memory is only %" G_GSIZE_FORMAT "B. This is probably a driver " |
| 1514 | "bug.", size, group->mem[i]->maxsize); |
| 1515 | gst_memory_resize (group->mem[i], 0, group->mem[i]->maxsize); |
| 1516 | } |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | /* Release the memory, possibly making it RW again */ |
| 1521 | for (i = 0; i < group->n_mem; i++) |
| 1522 | gst_memory_unref (group->mem[i]); |
| 1523 | |
| 1524 | *group_out = group; |
| 1525 | return GST_FLOW_OK; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1526 | |
| 1527 | error: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1528 | if (errno == EPIPE) |
| 1529 | { |
| 1530 | GST_DEBUG_OBJECT (allocator, "broken pipe signals last buffer"); |
| 1531 | return GST_FLOW_EOS; |
| 1532 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1533 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1534 | GST_ERROR_OBJECT (allocator, "failed dequeuing a %s buffer: %s", |
| 1535 | memory_type_to_str (allocator->memory), g_strerror (errno)); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1536 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1537 | switch (errno) |
| 1538 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1539 | case EAGAIN: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1540 | GST_WARNING_OBJECT (allocator, |
| 1541 | "Non-blocking I/O has been selected using O_NONBLOCK and" |
| 1542 | " no buffer was in the outgoing queue."); |
| 1543 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1544 | case EINVAL: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1545 | GST_ERROR_OBJECT (allocator, |
| 1546 | "The buffer type is not supported, or the index is out of bounds, " |
| 1547 | "or no buffers have been allocated yet, or the userptr " |
| 1548 | "or length are invalid."); |
| 1549 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1550 | case ENOMEM: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1551 | GST_ERROR_OBJECT (allocator, |
| 1552 | "insufficient memory to enqueue a user pointer buffer"); |
| 1553 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1554 | case EIO: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1555 | GST_INFO_OBJECT (allocator, |
| 1556 | "VIDIOC_DQBUF failed due to an internal error." |
| 1557 | " Can also indicate temporary problems like signal loss." |
| 1558 | " Note the driver might dequeue an (empty) buffer despite" |
| 1559 | " returning an error, or even stop capturing."); |
| 1560 | /* have we de-queued a buffer ? */ |
| 1561 | if (!IS_QUEUED(buffer)) |
| 1562 | { |
| 1563 | GST_DEBUG_OBJECT (allocator, "reenqueueing buffer"); |
| 1564 | /* FIXME ... should we do something here? */ |
| 1565 | } |
| 1566 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1567 | case EINTR: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1568 | GST_WARNING_OBJECT (allocator, "could not sync on a buffer on device"); |
| 1569 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1570 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1571 | GST_WARNING_OBJECT (allocator, |
| 1572 | "Grabbing frame got interrupted unexpectedly. %d: %s.", errno, |
| 1573 | g_strerror (errno)); |
| 1574 | break; |
| 1575 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1576 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1577 | return GST_FLOW_ERROR; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1578 | } |
| 1579 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1580 | void |
| 1581 | gst_aml_v4l2_allocator_reset_group (GstAmlV4l2Allocator *allocator, |
| 1582 | GstAmlV4l2MemoryGroup *group) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1583 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1584 | switch (allocator->memory) |
| 1585 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1586 | case V4L2_MEMORY_USERPTR: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1587 | gst_aml_v4l2_allocator_clear_userptr (allocator, group); |
| 1588 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1589 | case V4L2_MEMORY_DMABUF: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1590 | gst_aml_v4l2_allocator_clear_dmabufin (allocator, group); |
| 1591 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1592 | case V4L2_MEMORY_MMAP: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1593 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1594 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1595 | g_assert_not_reached (); |
| 1596 | break; |
| 1597 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1598 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1599 | gst_aml_v4l2_allocator_reset_size(allocator, group); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1600 | } |