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 | #ifdef HAVE_CONFIG_H |
| 21 | #include <config.h> |
| 22 | #endif |
| 23 | |
| 24 | #include <fcntl.h> |
| 25 | |
| 26 | #include <sys/mman.h> |
| 27 | #include <string.h> |
| 28 | #include <unistd.h> |
| 29 | #include <stdio.h> |
| 30 | |
| 31 | #include "gst/video/video.h" |
| 32 | #include "gst/video/gstvideometa.h" |
| 33 | #include "gst/video/gstvideopool.h" |
| 34 | #include "gst/allocators/gstdmabuf.h" |
| 35 | |
| 36 | #include <gstamlv4l2bufferpool.h> |
| 37 | |
| 38 | #include "gstamlv4l2object.h" |
| 39 | #include "gst/gst-i18n-plugin.h" |
| 40 | #include <gst/glib-compat-private.h> |
| 41 | |
| 42 | #define GST_DUMP_CAPTURE_BP_STAT_FILENAME "amlv4l2dec_capture_bp_buf_stat" |
| 43 | #define GST_DUMP_OUTPUT_BP_STAT_FILENAME "amlv4l2dec_output_bp_buf_stat" |
| 44 | |
| 45 | GST_DEBUG_CATEGORY_STATIC(amlv4l2bufferpool_debug); |
| 46 | GST_DEBUG_CATEGORY_STATIC(CAT_PERFORMANCE); |
| 47 | #define GST_CAT_DEFAULT amlv4l2bufferpool_debug |
| 48 | |
| 49 | #define GST_AML_V4L2_IMPORT_QUARK gst_aml_v4l2_buffer_pool_import_quark() |
| 50 | |
| 51 | /* |
| 52 | * GstAmlV4l2BufferPool: |
| 53 | */ |
| 54 | #define gst_aml_v4l2_buffer_pool_parent_class parent_class |
| 55 | G_DEFINE_TYPE(GstAmlV4l2BufferPool, gst_aml_v4l2_buffer_pool, GST_TYPE_BUFFER_POOL); |
| 56 | |
| 57 | enum _GstAmlV4l2BufferPoolAcquireFlags |
| 58 | { |
| 59 | GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT = |
| 60 | GST_BUFFER_POOL_ACQUIRE_FLAG_LAST, |
| 61 | GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_LAST |
| 62 | }; |
| 63 | |
| 64 | static void gst_aml_v4l2_buffer_pool_release_buffer(GstBufferPool *bpool, |
| 65 | GstBuffer *buffer); |
| 66 | |
| 67 | #ifdef GST_AML_SPEC_FLOW_FOR_VBP |
| 68 | static gboolean gst_aml_v4l2_buffer_pool_release_buffer_aml_patch(GstBufferPool *bpool); |
| 69 | #endif |
| 70 | |
| 71 | static gboolean |
| 72 | gst_aml_v4l2_is_buffer_valid(GstBuffer *buffer, GstAmlV4l2MemoryGroup **out_group) |
| 73 | { |
| 74 | GstMemory *mem = gst_buffer_peek_memory(buffer, 0); |
| 75 | gboolean valid = FALSE; |
| 76 | |
| 77 | if (GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_TAG_MEMORY)) |
| 78 | goto done; |
| 79 | |
| 80 | if (gst_is_dmabuf_memory(mem)) |
| 81 | mem = gst_mini_object_get_qdata(GST_MINI_OBJECT(mem), |
| 82 | GST_AML_V4L2_MEMORY_QUARK); |
| 83 | |
| 84 | if (mem && gst_is_aml_v4l2_memory(mem)) |
| 85 | { |
| 86 | GstAmlV4l2Memory *vmem = (GstAmlV4l2Memory *)mem; |
| 87 | GstAmlV4l2MemoryGroup *group = vmem->group; |
| 88 | gint i; |
| 89 | |
| 90 | if (group->n_mem != gst_buffer_n_memory(buffer)) |
| 91 | goto done; |
| 92 | |
| 93 | for (i = 0; i < group->n_mem; i++) |
| 94 | { |
| 95 | if (group->mem[i] != gst_buffer_peek_memory(buffer, i)) |
| 96 | goto done; |
| 97 | |
| 98 | if (!gst_memory_is_writable(group->mem[i])) |
| 99 | goto done; |
| 100 | } |
| 101 | |
| 102 | valid = TRUE; |
| 103 | if (out_group) |
| 104 | *out_group = group; |
| 105 | } |
| 106 | |
| 107 | done: |
| 108 | return valid; |
| 109 | } |
| 110 | |
| 111 | static GstFlowReturn |
| 112 | gst_aml_v4l2_buffer_pool_copy_buffer(GstAmlV4l2BufferPool *pool, GstBuffer *dest, |
| 113 | GstBuffer *src) |
| 114 | { |
| 115 | const GstVideoFormatInfo *finfo = pool->caps_info.finfo; |
| 116 | |
| 117 | GST_LOG_OBJECT(pool, "copying buffer"); |
| 118 | |
| 119 | if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN && |
| 120 | finfo->format != GST_VIDEO_FORMAT_ENCODED)) |
| 121 | { |
| 122 | GstVideoFrame src_frame, dest_frame; |
| 123 | |
| 124 | GST_DEBUG_OBJECT(pool, "copy video frame"); |
| 125 | |
| 126 | /* we have raw video, use videoframe copy to get strides right */ |
| 127 | if (!gst_video_frame_map(&src_frame, &pool->caps_info, src, GST_MAP_READ)) |
| 128 | goto invalid_buffer; |
| 129 | |
| 130 | if (!gst_video_frame_map(&dest_frame, &pool->caps_info, dest, |
| 131 | GST_MAP_WRITE)) |
| 132 | { |
| 133 | gst_video_frame_unmap(&src_frame); |
| 134 | goto invalid_buffer; |
| 135 | } |
| 136 | |
| 137 | gst_video_frame_copy(&dest_frame, &src_frame); |
| 138 | |
| 139 | gst_video_frame_unmap(&src_frame); |
| 140 | gst_video_frame_unmap(&dest_frame); |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | GstMapInfo map; |
| 145 | |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame^] | 146 | GST_DEBUG_OBJECT(pool, "copy raw bytes size:%d", gst_buffer_get_size(src)); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 147 | |
| 148 | if (!gst_buffer_map(src, &map, GST_MAP_READ)) |
| 149 | goto invalid_buffer; |
| 150 | |
| 151 | gst_buffer_fill(dest, 0, map.data, gst_buffer_get_size(src)); |
| 152 | |
| 153 | gst_buffer_unmap(src, &map); |
| 154 | gst_buffer_resize(dest, 0, gst_buffer_get_size(src)); |
| 155 | } |
| 156 | |
| 157 | gst_buffer_copy_into(dest, src, |
| 158 | GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1); |
| 159 | |
| 160 | GST_CAT_LOG_OBJECT(CAT_PERFORMANCE, pool, "slow copy into buffer %p", dest); |
| 161 | |
| 162 | return GST_FLOW_OK; |
| 163 | |
| 164 | invalid_buffer: |
| 165 | { |
| 166 | GST_ERROR_OBJECT(pool, "could not map buffer"); |
| 167 | return GST_FLOW_ERROR; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | struct UserPtrData |
| 172 | { |
| 173 | GstBuffer *buffer; |
| 174 | gboolean is_frame; |
| 175 | GstVideoFrame frame; |
| 176 | GstMapInfo map; |
| 177 | }; |
| 178 | |
| 179 | static GQuark |
| 180 | gst_aml_v4l2_buffer_pool_import_quark(void) |
| 181 | { |
| 182 | static GQuark quark = 0; |
| 183 | |
| 184 | if (quark == 0) |
| 185 | quark = g_quark_from_string("GstAmlV4l2BufferPoolUsePtrData"); |
| 186 | |
| 187 | return quark; |
| 188 | } |
| 189 | |
| 190 | static void |
| 191 | _unmap_userptr_frame(struct UserPtrData *data) |
| 192 | { |
| 193 | if (data->is_frame) |
| 194 | gst_video_frame_unmap(&data->frame); |
| 195 | else |
| 196 | gst_buffer_unmap(data->buffer, &data->map); |
| 197 | |
| 198 | if (data->buffer) |
| 199 | gst_buffer_unref(data->buffer); |
| 200 | |
| 201 | g_slice_free(struct UserPtrData, data); |
| 202 | } |
| 203 | |
| 204 | static GstFlowReturn |
| 205 | gst_aml_v4l2_buffer_pool_import_userptr(GstAmlV4l2BufferPool *pool, |
| 206 | GstBuffer *dest, GstBuffer *src) |
| 207 | { |
| 208 | GstFlowReturn ret = GST_FLOW_OK; |
| 209 | GstAmlV4l2MemoryGroup *group = NULL; |
| 210 | GstMapFlags flags; |
| 211 | const GstVideoFormatInfo *finfo = pool->caps_info.finfo; |
| 212 | struct UserPtrData *data = NULL; |
| 213 | |
| 214 | GST_LOG_OBJECT(pool, "importing userptr"); |
| 215 | |
| 216 | /* get the group */ |
| 217 | if (!gst_aml_v4l2_is_buffer_valid(dest, &group)) |
| 218 | goto not_our_buffer; |
| 219 | |
| 220 | if (V4L2_TYPE_IS_OUTPUT(pool->obj->type)) |
| 221 | flags = GST_MAP_READ; |
| 222 | else |
| 223 | flags = GST_MAP_WRITE; |
| 224 | |
| 225 | data = g_slice_new0(struct UserPtrData); |
| 226 | |
| 227 | if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN && |
| 228 | finfo->format != GST_VIDEO_FORMAT_ENCODED)) |
| 229 | { |
| 230 | gsize size[GST_VIDEO_MAX_PLANES] = { |
| 231 | 0, |
| 232 | }; |
| 233 | gint i; |
| 234 | |
| 235 | data->is_frame = TRUE; |
| 236 | |
| 237 | if (!gst_video_frame_map(&data->frame, &pool->caps_info, src, flags)) |
| 238 | goto invalid_buffer; |
| 239 | |
| 240 | for (i = 0; i < GST_VIDEO_FORMAT_INFO_N_PLANES(finfo); i++) |
| 241 | { |
| 242 | if (GST_VIDEO_FORMAT_INFO_IS_TILED(finfo)) |
| 243 | { |
| 244 | gint tinfo = GST_VIDEO_FRAME_PLANE_STRIDE(&data->frame, i); |
| 245 | gint pstride; |
| 246 | guint pheight; |
| 247 | |
| 248 | pstride = GST_VIDEO_TILE_X_TILES(tinfo) << GST_VIDEO_FORMAT_INFO_TILE_WS(finfo); |
| 249 | |
| 250 | pheight = GST_VIDEO_TILE_Y_TILES(tinfo) << GST_VIDEO_FORMAT_INFO_TILE_HS(finfo); |
| 251 | |
| 252 | size[i] = pstride * pheight; |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | size[i] = GST_VIDEO_FRAME_PLANE_STRIDE(&data->frame, i) * |
| 257 | GST_VIDEO_FRAME_COMP_HEIGHT(&data->frame, i); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* In the single planar API, planes must be contiguous in memory and |
| 262 | * therefore they must have expected size. ie: no padding. |
| 263 | * To check these conditions, we check that plane 'i' start address |
| 264 | * + plane 'i' size equals to plane 'i+1' start address */ |
| 265 | if (!V4L2_TYPE_IS_MULTIPLANAR(pool->obj->type)) |
| 266 | { |
| 267 | for (i = 0; i < (GST_VIDEO_FORMAT_INFO_N_PLANES(finfo) - 1); i++) |
| 268 | { |
| 269 | const struct v4l2_pix_format *pix_fmt = &pool->obj->format.fmt.pix; |
| 270 | gpointer tmp; |
| 271 | gint estride = gst_aml_v4l2_object_extrapolate_stride(finfo, i, |
| 272 | pix_fmt->bytesperline); |
| 273 | guint eheight = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT(finfo, i, |
| 274 | pix_fmt->height); |
| 275 | |
| 276 | tmp = ((guint8 *)data->frame.data[i]) + estride * eheight; |
| 277 | if (tmp != data->frame.data[i + 1]) |
| 278 | goto non_contiguous_mem; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | if (!gst_aml_v4l2_allocator_import_userptr(pool->vallocator, group, |
| 283 | data->frame.info.size, finfo->n_planes, data->frame.data, size)) |
| 284 | goto import_failed; |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | gpointer ptr[1]; |
| 289 | gsize size[1]; |
| 290 | |
| 291 | data->is_frame = FALSE; |
| 292 | |
| 293 | if (!gst_buffer_map(src, &data->map, flags)) |
| 294 | goto invalid_buffer; |
| 295 | |
| 296 | ptr[0] = data->map.data; |
| 297 | size[0] = data->map.size; |
| 298 | |
| 299 | if (!gst_aml_v4l2_allocator_import_userptr(pool->vallocator, group, |
| 300 | data->map.size, 1, ptr, size)) |
| 301 | goto import_failed; |
| 302 | } |
| 303 | |
| 304 | data->buffer = gst_buffer_ref(src); |
| 305 | |
| 306 | gst_mini_object_set_qdata(GST_MINI_OBJECT(dest), GST_AML_V4L2_IMPORT_QUARK, |
| 307 | data, (GDestroyNotify)_unmap_userptr_frame); |
| 308 | |
| 309 | gst_buffer_copy_into(dest, src, |
| 310 | GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1); |
| 311 | |
| 312 | return ret; |
| 313 | |
| 314 | not_our_buffer: |
| 315 | { |
| 316 | GST_ERROR_OBJECT(pool, "destination buffer invalid or not from our pool"); |
| 317 | return GST_FLOW_ERROR; |
| 318 | } |
| 319 | invalid_buffer: |
| 320 | { |
| 321 | GST_ERROR_OBJECT(pool, "could not map buffer"); |
| 322 | g_slice_free(struct UserPtrData, data); |
| 323 | return GST_FLOW_ERROR; |
| 324 | } |
| 325 | non_contiguous_mem: |
| 326 | { |
| 327 | GST_ERROR_OBJECT(pool, "memory is not contiguous or plane size mismatch"); |
| 328 | _unmap_userptr_frame(data); |
| 329 | return GST_FLOW_ERROR; |
| 330 | } |
| 331 | import_failed: |
| 332 | { |
| 333 | GST_ERROR_OBJECT(pool, "failed to import data"); |
| 334 | _unmap_userptr_frame(data); |
| 335 | return GST_FLOW_ERROR; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | static GstFlowReturn |
| 340 | gst_aml_v4l2_buffer_pool_import_dmabuf(GstAmlV4l2BufferPool *pool, |
| 341 | GstBuffer *dest, GstBuffer *src) |
| 342 | { |
| 343 | GstAmlV4l2MemoryGroup *group = NULL; |
| 344 | GstMemory *dma_mem[GST_VIDEO_MAX_PLANES] = {0}; |
| 345 | guint n_mem = gst_buffer_n_memory(src); |
| 346 | gint i; |
| 347 | |
| 348 | GST_LOG_OBJECT(pool, "importing dmabuf"); |
| 349 | |
| 350 | if (!gst_aml_v4l2_is_buffer_valid(dest, &group)) |
| 351 | goto not_our_buffer; |
| 352 | |
| 353 | if (n_mem > GST_VIDEO_MAX_PLANES) |
| 354 | goto too_many_mems; |
| 355 | |
| 356 | for (i = 0; i < n_mem; i++) |
| 357 | dma_mem[i] = gst_buffer_peek_memory(src, i); |
| 358 | |
| 359 | if (!gst_aml_v4l2_allocator_import_dmabuf(pool->vallocator, group, n_mem, |
| 360 | dma_mem)) |
| 361 | goto import_failed; |
| 362 | |
sheng.liu | 01cd7b5 | 2022-06-09 18:12:11 +0800 | [diff] [blame] | 363 | // Output buf is secure memory, Need to unref by itselt |
| 364 | // Capture buf is secure memory, Need to unref by downstreaming element gstvideosink |
| 365 | if (V4L2_TYPE_IS_OUTPUT (pool->obj->type)) |
| 366 | gst_mini_object_set_qdata(GST_MINI_OBJECT(dest), GST_AML_V4L2_IMPORT_QUARK, |
| 367 | gst_buffer_ref(src), (GDestroyNotify)gst_buffer_unref); |
| 368 | else |
| 369 | gst_mini_object_set_qdata(GST_MINI_OBJECT(dest), GST_AML_V4L2_IMPORT_QUARK, gst_buffer_ref(src), NULL); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 370 | |
| 371 | gst_buffer_copy_into(dest, src, |
| 372 | GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1); |
| 373 | |
| 374 | GST_DEBUG_OBJECT(pool, "v4l2 buf:%p, import buf:%p as qdata", dest, src); |
| 375 | return GST_FLOW_OK; |
| 376 | |
| 377 | not_our_buffer: |
| 378 | { |
| 379 | GST_ERROR_OBJECT(pool, "destination buffer invalid or not from our pool"); |
| 380 | return GST_FLOW_ERROR; |
| 381 | } |
| 382 | too_many_mems: |
| 383 | { |
| 384 | GST_ERROR_OBJECT(pool, "could not map buffer"); |
| 385 | return GST_FLOW_ERROR; |
| 386 | } |
| 387 | import_failed: |
| 388 | { |
| 389 | GST_ERROR_OBJECT(pool, "failed to import dmabuf"); |
| 390 | return GST_FLOW_ERROR; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | static GstFlowReturn |
| 395 | gst_aml_v4l2_buffer_pool_prepare_buffer(GstAmlV4l2BufferPool *pool, |
| 396 | GstBuffer *dest, GstBuffer *src) |
| 397 | { |
| 398 | GstFlowReturn ret = GST_FLOW_OK; |
| 399 | gboolean own_src = FALSE; |
| 400 | |
| 401 | if (src == NULL) |
| 402 | { |
| 403 | if (pool->other_pool == NULL) |
| 404 | { |
| 405 | GST_ERROR_OBJECT(pool, "can't prepare buffer, source buffer missing"); |
| 406 | return GST_FLOW_ERROR; |
| 407 | } |
| 408 | |
| 409 | ret = gst_buffer_pool_acquire_buffer(pool->other_pool, &src, NULL); |
| 410 | if (ret != GST_FLOW_OK) |
| 411 | { |
| 412 | GST_ERROR_OBJECT(pool, "failed to acquire buffer from downstream pool"); |
| 413 | goto done; |
| 414 | } |
| 415 | |
| 416 | own_src = TRUE; |
| 417 | } |
| 418 | |
| 419 | switch (pool->obj->mode) |
| 420 | { |
| 421 | case GST_V4L2_IO_MMAP: |
| 422 | case GST_V4L2_IO_DMABUF: |
| 423 | ret = gst_aml_v4l2_buffer_pool_copy_buffer(pool, dest, src); |
| 424 | break; |
| 425 | case GST_V4L2_IO_USERPTR: |
| 426 | ret = gst_aml_v4l2_buffer_pool_import_userptr(pool, dest, src); |
| 427 | break; |
| 428 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 429 | ret = gst_aml_v4l2_buffer_pool_import_dmabuf(pool, dest, src); |
| 430 | break; |
| 431 | default: |
| 432 | break; |
| 433 | } |
| 434 | |
| 435 | if (own_src) |
| 436 | gst_buffer_unref(src); |
| 437 | |
| 438 | done: |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | static GstFlowReturn |
| 443 | gst_aml_v4l2_buffer_pool_alloc_buffer(GstBufferPool *bpool, GstBuffer **buffer, |
| 444 | GstBufferPoolAcquireParams *params) |
| 445 | { |
| 446 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(bpool); |
| 447 | GstAmlV4l2MemoryGroup *group = NULL; |
| 448 | GstBuffer *newbuf = NULL; |
| 449 | GstAmlV4l2Object *obj; |
| 450 | GstVideoInfo *info; |
| 451 | |
| 452 | obj = pool->obj; |
| 453 | info = &obj->info; |
| 454 | |
| 455 | switch (obj->mode) |
| 456 | { |
| 457 | case GST_V4L2_IO_RW: |
| 458 | newbuf = |
| 459 | gst_buffer_new_allocate(pool->allocator, pool->size, &pool->params); |
| 460 | break; |
| 461 | case GST_V4L2_IO_MMAP: |
| 462 | group = gst_aml_v4l2_allocator_alloc_mmap(pool->vallocator); |
| 463 | break; |
| 464 | case GST_V4L2_IO_DMABUF: |
| 465 | group = gst_aml_v4l2_allocator_alloc_dmabuf(pool->vallocator, |
| 466 | pool->allocator); |
| 467 | break; |
| 468 | case GST_V4L2_IO_USERPTR: |
| 469 | group = gst_aml_v4l2_allocator_alloc_userptr(pool->vallocator); |
| 470 | break; |
| 471 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 472 | group = gst_aml_v4l2_allocator_alloc_dmabufin(pool->vallocator); |
| 473 | break; |
| 474 | default: |
| 475 | newbuf = NULL; |
| 476 | g_assert_not_reached(); |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | if (group != NULL) |
| 481 | { |
| 482 | gint i; |
| 483 | newbuf = gst_buffer_new(); |
| 484 | |
| 485 | for (i = 0; i < group->n_mem; i++) |
| 486 | gst_buffer_append_memory(newbuf, group->mem[i]); |
| 487 | } |
| 488 | else if (newbuf == NULL) |
| 489 | { |
| 490 | goto allocation_failed; |
| 491 | } |
| 492 | |
| 493 | /* add metadata to raw video buffers */ |
| 494 | if (pool->add_videometa) |
| 495 | gst_buffer_add_video_meta_full(newbuf, GST_VIDEO_FRAME_FLAG_NONE, |
| 496 | GST_VIDEO_INFO_FORMAT(info), GST_VIDEO_INFO_WIDTH(info), |
| 497 | GST_VIDEO_INFO_HEIGHT(info), GST_VIDEO_INFO_N_PLANES(info), |
| 498 | info->offset, info->stride); |
| 499 | |
| 500 | *buffer = newbuf; |
| 501 | |
| 502 | return GST_FLOW_OK; |
| 503 | |
| 504 | /* ERRORS */ |
| 505 | allocation_failed: |
| 506 | { |
| 507 | GST_ERROR_OBJECT(pool, "failed to allocate buffer"); |
| 508 | return GST_FLOW_ERROR; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | static gboolean |
| 513 | gst_aml_v4l2_buffer_pool_set_config(GstBufferPool *bpool, GstStructure *config) |
| 514 | { |
| 515 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(bpool); |
| 516 | GstAmlV4l2Object *obj = pool->obj; |
| 517 | GstCaps *caps; |
| 518 | guint size, min_buffers, max_buffers; |
| 519 | GstAllocator *allocator; |
| 520 | GstAllocationParams params; |
| 521 | gboolean can_allocate = FALSE; |
| 522 | gboolean updated = FALSE; |
| 523 | gboolean ret; |
| 524 | |
| 525 | pool->add_videometa = |
| 526 | gst_buffer_pool_config_has_option(config, |
| 527 | GST_BUFFER_POOL_OPTION_VIDEO_META); |
| 528 | |
| 529 | /* parse the config and keep around */ |
| 530 | if (!gst_buffer_pool_config_get_params(config, &caps, &size, &min_buffers, |
| 531 | &max_buffers)) |
| 532 | goto wrong_config; |
| 533 | |
| 534 | if (!gst_buffer_pool_config_get_allocator(config, &allocator, ¶ms)) |
| 535 | goto wrong_config; |
| 536 | |
| 537 | GST_DEBUG_OBJECT(pool, "config %" GST_PTR_FORMAT, config); |
| 538 | |
| 539 | if (pool->allocator) |
| 540 | gst_object_unref(pool->allocator); |
| 541 | pool->allocator = NULL; |
| 542 | |
| 543 | switch (obj->mode) |
| 544 | { |
| 545 | case GST_V4L2_IO_DMABUF: |
| 546 | pool->allocator = gst_dmabuf_allocator_new(); |
| 547 | can_allocate = GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(pool->vallocator, MMAP); |
| 548 | break; |
| 549 | case GST_V4L2_IO_MMAP: |
| 550 | can_allocate = GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(pool->vallocator, MMAP); |
| 551 | break; |
| 552 | case GST_V4L2_IO_USERPTR: |
| 553 | can_allocate = |
| 554 | GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(pool->vallocator, USERPTR); |
| 555 | break; |
| 556 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 557 | can_allocate = GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(pool->vallocator, DMABUF); |
| 558 | break; |
| 559 | case GST_V4L2_IO_RW: |
| 560 | if (allocator) |
| 561 | pool->allocator = g_object_ref(allocator); |
| 562 | pool->params = params; |
| 563 | /* No need to change the configuration */ |
| 564 | goto done; |
| 565 | break; |
| 566 | default: |
| 567 | g_assert_not_reached(); |
| 568 | break; |
| 569 | } |
| 570 | |
| 571 | /* libv4l2 conversion code does not handle CREATE_BUFS, and may lead to |
| 572 | * instability and crash, disable it for now */ |
| 573 | if (can_allocate && obj->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) |
| 574 | { |
| 575 | GST_WARNING_OBJECT(pool, |
| 576 | "libv4l2 converter detected, disabling CREATE_BUFS"); |
| 577 | can_allocate = FALSE; |
| 578 | GST_OBJECT_FLAG_UNSET(pool->vallocator, |
| 579 | GST_V4L2_ALLOCATOR_FLAG_MMAP_CREATE_BUFS | GST_V4L2_ALLOCATOR_FLAG_USERPTR_CREATE_BUFS | GST_V4L2_ALLOCATOR_FLAG_DMABUF_CREATE_BUFS); |
| 580 | } |
| 581 | |
| 582 | if (min_buffers < GST_AML_V4L2_MIN_BUFFERS) |
| 583 | { |
| 584 | updated = TRUE; |
| 585 | min_buffers = GST_AML_V4L2_MIN_BUFFERS; |
| 586 | GST_INFO_OBJECT(pool, "increasing minimum buffers to %u", min_buffers); |
| 587 | } |
| 588 | |
| 589 | /* respect driver requirements */ |
| 590 | if (min_buffers < obj->min_buffers) |
| 591 | { |
| 592 | updated = TRUE; |
| 593 | min_buffers = obj->min_buffers; |
| 594 | GST_INFO_OBJECT(pool, "increasing minimum buffers to %u", min_buffers); |
| 595 | } |
| 596 | |
| 597 | if (max_buffers > VIDEO_MAX_FRAME || max_buffers == 0) |
| 598 | { |
| 599 | updated = TRUE; |
| 600 | max_buffers = VIDEO_MAX_FRAME; |
| 601 | GST_INFO_OBJECT(pool, "reducing maximum buffers to %u", max_buffers); |
| 602 | } |
| 603 | |
| 604 | if (min_buffers > max_buffers) |
| 605 | { |
| 606 | updated = TRUE; |
| 607 | min_buffers = max_buffers; |
| 608 | GST_INFO_OBJECT(pool, "reducing minimum buffers to %u", min_buffers); |
| 609 | } |
| 610 | else if (min_buffers != max_buffers) |
| 611 | { |
| 612 | if (!can_allocate) |
| 613 | { |
| 614 | updated = TRUE; |
| 615 | max_buffers = min_buffers; |
| 616 | GST_INFO_OBJECT(pool, "can't allocate, setting maximum to minimum"); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | if (!pool->add_videometa && obj->need_video_meta) |
| 621 | { |
| 622 | GST_INFO_OBJECT(pool, "adding needed video meta"); |
| 623 | updated = TRUE; |
| 624 | gst_buffer_pool_config_add_option(config, |
| 625 | GST_BUFFER_POOL_OPTION_VIDEO_META); |
| 626 | } |
| 627 | |
| 628 | /* Always update the config to ensure the configured size matches */ |
| 629 | gst_buffer_pool_config_set_params(config, caps, obj->info.size, min_buffers, |
| 630 | max_buffers); |
| 631 | |
| 632 | /* keep a GstVideoInfo with defaults for the when we need to copy */ |
| 633 | gst_video_info_from_caps(&pool->caps_info, caps); |
| 634 | |
| 635 | done: |
| 636 | ret = GST_BUFFER_POOL_CLASS(parent_class)->set_config(bpool, config); |
| 637 | |
| 638 | /* If anything was changed documentation recommand to return FALSE */ |
| 639 | return !updated && ret; |
| 640 | |
| 641 | /* ERRORS */ |
| 642 | wrong_config: |
| 643 | { |
| 644 | GST_ERROR_OBJECT(pool, "invalid config %" GST_PTR_FORMAT, config); |
| 645 | return FALSE; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | static GstFlowReturn |
| 650 | gst_aml_v4l2_buffer_pool_resurrect_buffer(GstAmlV4l2BufferPool *pool) |
| 651 | { |
| 652 | GstBufferPoolAcquireParams params = {0}; |
| 653 | GstBuffer *buffer = NULL; |
| 654 | GstFlowReturn ret; |
| 655 | |
| 656 | GST_DEBUG_OBJECT(pool, "A buffer was lost, reallocating it"); |
| 657 | |
| 658 | /* block recursive calls to this function */ |
| 659 | g_signal_handler_block(pool->vallocator, pool->group_released_handler); |
| 660 | |
| 661 | params.flags = |
| 662 | (GstBufferPoolAcquireFlags)GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT | |
| 663 | GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT; |
| 664 | ret = |
| 665 | gst_buffer_pool_acquire_buffer(GST_BUFFER_POOL(pool), &buffer, ¶ms); |
| 666 | |
| 667 | if (ret == GST_FLOW_OK) |
| 668 | gst_buffer_unref(buffer); |
| 669 | |
| 670 | g_signal_handler_unblock(pool->vallocator, pool->group_released_handler); |
| 671 | |
| 672 | return ret; |
| 673 | } |
| 674 | |
| 675 | static gboolean |
| 676 | gst_aml_v4l2_buffer_pool_streamon(GstAmlV4l2BufferPool *pool) |
| 677 | { |
| 678 | GstAmlV4l2Object *obj = pool->obj; |
| 679 | |
| 680 | if (pool->streaming) |
| 681 | return TRUE; |
| 682 | |
| 683 | switch (obj->mode) |
| 684 | { |
| 685 | case GST_V4L2_IO_MMAP: |
| 686 | case GST_V4L2_IO_USERPTR: |
| 687 | case GST_V4L2_IO_DMABUF: |
| 688 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 689 | if (!V4L2_TYPE_IS_OUTPUT(pool->obj->type)) |
| 690 | { |
| 691 | guint i; |
| 692 | |
| 693 | /* For captures, we need to enqueue buffers before we start streaming, |
| 694 | * so the driver don't underflow immediatly. As we have put then back |
| 695 | * into the base class queue, resurrect them, then releasing will queue |
| 696 | * them back. */ |
| 697 | for (i = 0; i < pool->num_allocated; i++) |
| 698 | gst_aml_v4l2_buffer_pool_resurrect_buffer(pool); |
| 699 | } |
| 700 | |
| 701 | if (obj->ioctl(pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0) |
| 702 | goto streamon_failed; |
| 703 | |
| 704 | pool->streaming = TRUE; |
| 705 | |
| 706 | GST_DEBUG_OBJECT(pool, "Started streaming"); |
| 707 | break; |
| 708 | default: |
| 709 | break; |
| 710 | } |
| 711 | |
| 712 | return TRUE; |
| 713 | |
| 714 | streamon_failed: |
| 715 | { |
| 716 | GST_ERROR_OBJECT(pool, "error with STREAMON %d (%s)", errno, |
| 717 | g_strerror(errno)); |
| 718 | return FALSE; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | /* Call with streamlock held, or when streaming threads are down */ |
| 723 | static void |
| 724 | gst_aml_v4l2_buffer_pool_streamoff(GstAmlV4l2BufferPool *pool) |
| 725 | { |
| 726 | GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS(parent_class); |
| 727 | GstAmlV4l2Object *obj = pool->obj; |
| 728 | gint i; |
| 729 | |
| 730 | if (!pool->streaming) |
| 731 | return; |
| 732 | |
| 733 | switch (obj->mode) |
| 734 | { |
| 735 | case GST_V4L2_IO_MMAP: |
| 736 | case GST_V4L2_IO_USERPTR: |
| 737 | case GST_V4L2_IO_DMABUF: |
| 738 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 739 | |
| 740 | if (obj->ioctl(pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0) |
| 741 | GST_WARNING_OBJECT(pool, "STREAMOFF failed with errno %d (%s)", |
| 742 | errno, g_strerror(errno)); |
| 743 | |
| 744 | pool->streaming = FALSE; |
| 745 | |
| 746 | GST_DEBUG_OBJECT(pool, "Stopped streaming"); |
| 747 | |
| 748 | if (pool->vallocator) |
| 749 | gst_aml_v4l2_allocator_flush(pool->vallocator); |
| 750 | break; |
| 751 | default: |
| 752 | break; |
| 753 | } |
| 754 | |
| 755 | GstBufferPool *bpool = GST_BUFFER_POOL(pool); |
| 756 | if (V4L2_TYPE_IS_OUTPUT(pool->obj->type)) |
| 757 | { |
| 758 | for (i = 0; i < VIDEO_MAX_FRAME; i++) |
| 759 | { |
| 760 | GST_INFO_OBJECT(pool, "deal with output buf index:%d, buf:%p", i, pool->buffers[i]); |
| 761 | if (pool->buffers[i]) |
| 762 | { |
| 763 | GstBuffer *buffer = pool->buffers[i]; |
| 764 | pool->buffers[i] = NULL; |
| 765 | gst_aml_v4l2_buffer_pool_release_buffer(bpool, buffer); |
| 766 | g_atomic_int_add(&pool->num_queued, -1); |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | else |
| 771 | { |
| 772 | #ifdef GST_AML_SPEC_FLOW_FOR_VBP |
| 773 | if (GST_V4L2_IO_DMABUF_IMPORT == obj->mode) |
| 774 | { |
| 775 | GST_DEBUG_OBJECT(pool, "have %d ready to free capture buffer", pool->ready_to_free_buf_num); |
| 776 | for (i = 0; i < VIDEO_MAX_FRAME; i++) |
| 777 | { |
| 778 | GST_DEBUG_OBJECT(pool, "buffers[%d]:%p, read_to_free_bufs[%d]:%p", i, pool->buffers[i], i, pool->read_to_free_bufs[i]); |
| 779 | if (pool->buffers[i]) |
| 780 | { |
| 781 | if (pool->other_pool) |
| 782 | { |
| 783 | GstBuffer *other_pool_buf = gst_mini_object_get_qdata(GST_MINI_OBJECT(pool->buffers[i]), GST_AML_V4L2_IMPORT_QUARK); |
| 784 | GST_DEBUG_OBJECT(pool, "release v4l2 capture buf[%d]:%p other pool buf:%p", i, pool->buffers[i], other_pool_buf); |
| 785 | gst_buffer_unref(other_pool_buf); |
| 786 | } |
| 787 | } |
| 788 | else if (pool->read_to_free_bufs[i]) |
| 789 | { |
| 790 | pool->buffers[i] = pool->read_to_free_bufs[i]; |
| 791 | pool->read_to_free_bufs[i] = NULL; |
| 792 | pool->ready_to_free_buf_num--; |
| 793 | } |
| 794 | } |
| 795 | GST_DEBUG_OBJECT(pool, "%d ready to free capture buffer left", pool->ready_to_free_buf_num); |
| 796 | pool->num_queued = 0; |
| 797 | } |
| 798 | #endif |
| 799 | for (i = 0; i < VIDEO_MAX_FRAME; i++) |
| 800 | { |
| 801 | GST_INFO_OBJECT(pool, "deal with caputre buf index:%d, buf:%p", i, pool->buffers[i]); |
| 802 | if (pool->buffers[i]) |
| 803 | { |
| 804 | GstBuffer *buffer = pool->buffers[i]; |
| 805 | pool->buffers[i] = NULL; |
| 806 | pclass->release_buffer(bpool, buffer); |
| 807 | #ifndef GST_AML_SPEC_FLOW_FOR_VBP |
| 808 | g_atomic_int_add(&pool->num_queued, -1); |
| 809 | #endif |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | static gboolean |
| 816 | gst_aml_v4l2_buffer_pool_start(GstBufferPool *bpool) |
| 817 | { |
| 818 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(bpool); |
| 819 | GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS(parent_class); |
| 820 | GstAmlV4l2Object *obj = pool->obj; |
| 821 | GstStructure *config; |
| 822 | GstCaps *caps; |
| 823 | guint size, min_buffers, max_buffers; |
| 824 | guint max_latency, min_latency, copy_threshold = 0; |
| 825 | gboolean can_allocate = FALSE, ret = TRUE; |
| 826 | |
| 827 | GST_DEBUG_OBJECT(pool, "activating pool"); |
| 828 | |
| 829 | if (pool->other_pool) |
| 830 | { |
| 831 | GstBuffer *buffer; |
| 832 | |
| 833 | if (!gst_buffer_pool_set_active(pool->other_pool, TRUE)) |
| 834 | goto other_pool_failed; |
| 835 | |
| 836 | if (gst_buffer_pool_acquire_buffer(pool->other_pool, &buffer, NULL) != |
| 837 | GST_FLOW_OK) |
| 838 | goto other_pool_failed; |
| 839 | |
| 840 | if (!gst_aml_v4l2_object_try_import(obj, buffer)) |
| 841 | { |
| 842 | gst_buffer_unref(buffer); |
| 843 | goto cannot_import; |
| 844 | } |
| 845 | gst_buffer_unref(buffer); |
| 846 | } |
| 847 | |
| 848 | config = gst_buffer_pool_get_config(bpool); |
| 849 | if (!gst_buffer_pool_config_get_params(config, &caps, &size, &min_buffers, |
| 850 | &max_buffers)) |
| 851 | goto wrong_config; |
| 852 | |
| 853 | min_latency = MAX(GST_AML_V4L2_MIN_BUFFERS, obj->min_buffers); |
| 854 | |
| 855 | switch (obj->mode) |
| 856 | { |
| 857 | case GST_V4L2_IO_RW: |
| 858 | can_allocate = TRUE; |
| 859 | #ifdef HAVE_LIBV4L2 |
| 860 | /* This workaround a unfixable bug in libv4l2 when RW is emulated on top |
| 861 | * of MMAP. In this case, the first read initialize the queues, but the |
| 862 | * poll before that will always fail. Doing an empty read, forces the |
| 863 | * queue to be initialized now. We only do this if we have a streaming |
| 864 | * driver. */ |
| 865 | if (obj->device_caps & V4L2_CAP_STREAMING) |
| 866 | obj->read(obj->video_fd, NULL, 0); |
| 867 | #endif |
| 868 | break; |
| 869 | case GST_V4L2_IO_DMABUF: |
| 870 | case GST_V4L2_IO_MMAP: |
| 871 | { |
| 872 | guint count; |
| 873 | |
| 874 | can_allocate = GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(pool->vallocator, MMAP); |
| 875 | |
| 876 | /* first, lets request buffers, and see how many we can get: */ |
| 877 | GST_DEBUG_OBJECT(pool, "requesting %d MMAP buffers", min_buffers); |
| 878 | |
| 879 | count = gst_aml_v4l2_allocator_start(pool->vallocator, min_buffers, |
| 880 | V4L2_MEMORY_MMAP); |
| 881 | pool->num_allocated = count; |
| 882 | |
| 883 | if (count < GST_AML_V4L2_MIN_BUFFERS) |
| 884 | { |
| 885 | min_buffers = count; |
| 886 | goto no_buffers; |
| 887 | } |
| 888 | |
| 889 | /* V4L2 buffer pool are often very limited in the amount of buffers it |
| 890 | * can offer. The copy_threshold will workaround this limitation by |
| 891 | * falling back to copy if the pipeline needed more buffers. This also |
| 892 | * prevent having to do REQBUFS(N)/REQBUFS(0) everytime configure is |
| 893 | * called. */ |
| 894 | if (count != min_buffers || pool->enable_copy_threshold) |
| 895 | { |
| 896 | GST_WARNING_OBJECT(pool, |
| 897 | "Uncertain or not enough buffers, enabling copy threshold"); |
| 898 | min_buffers = count; |
| 899 | copy_threshold = min_latency; |
| 900 | } |
| 901 | |
| 902 | break; |
| 903 | } |
| 904 | case GST_V4L2_IO_USERPTR: |
| 905 | { |
| 906 | guint count; |
| 907 | |
| 908 | can_allocate = |
| 909 | GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(pool->vallocator, USERPTR); |
| 910 | |
| 911 | GST_DEBUG_OBJECT(pool, "requesting %d USERPTR buffers", min_buffers); |
| 912 | |
| 913 | count = gst_aml_v4l2_allocator_start(pool->vallocator, min_buffers, |
| 914 | V4L2_MEMORY_USERPTR); |
| 915 | |
| 916 | /* There is no rational to not get what we asked */ |
| 917 | if (count < min_buffers) |
| 918 | { |
| 919 | min_buffers = count; |
| 920 | goto no_buffers; |
| 921 | } |
| 922 | |
| 923 | min_buffers = count; |
| 924 | break; |
| 925 | } |
| 926 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 927 | { |
| 928 | guint count; |
| 929 | |
| 930 | can_allocate = GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(pool->vallocator, DMABUF); |
| 931 | |
| 932 | GST_DEBUG_OBJECT(pool, "requesting %d DMABUF buffers", min_buffers); |
| 933 | |
| 934 | count = gst_aml_v4l2_allocator_start(pool->vallocator, min_buffers, |
| 935 | V4L2_MEMORY_DMABUF); |
| 936 | |
| 937 | /* There is no rational to not get what we asked */ |
| 938 | if (count < min_buffers) |
| 939 | { |
| 940 | min_buffers = count; |
| 941 | goto no_buffers; |
| 942 | } |
| 943 | |
| 944 | min_buffers = count; |
| 945 | break; |
| 946 | } |
| 947 | default: |
| 948 | min_buffers = 0; |
| 949 | copy_threshold = 0; |
| 950 | g_assert_not_reached(); |
| 951 | break; |
| 952 | } |
| 953 | |
| 954 | if (can_allocate) |
| 955 | max_latency = max_buffers; |
| 956 | else |
| 957 | max_latency = min_buffers; |
| 958 | |
| 959 | pool->size = size; |
| 960 | pool->copy_threshold = copy_threshold; |
| 961 | pool->max_latency = max_latency; |
| 962 | pool->min_latency = min_latency; |
| 963 | pool->num_queued = 0; |
| 964 | |
| 965 | if (max_buffers != 0 && max_buffers < min_buffers) |
| 966 | max_buffers = min_buffers; |
| 967 | |
| 968 | gst_buffer_pool_config_set_params(config, caps, size, min_buffers, |
| 969 | max_buffers); |
| 970 | pclass->set_config(bpool, config); |
| 971 | gst_structure_free(config); |
| 972 | |
| 973 | /* now, allocate the buffers: */ |
| 974 | if (!pclass->start(bpool)) |
| 975 | goto start_failed; |
| 976 | |
| 977 | if (!V4L2_TYPE_IS_OUTPUT(obj->type)) |
| 978 | { |
| 979 | if (g_atomic_int_get(&pool->num_queued) < min_buffers) |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame^] | 980 | { |
| 981 | if (obj->old_other_pool || obj->old_old_other_pool) |
| 982 | GST_DEBUG_OBJECT(pool, "resolution switching flow, need to wait other pool recycle"); |
| 983 | else |
| 984 | goto queue_failed; |
| 985 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 986 | |
| 987 | pool->group_released_handler = |
| 988 | g_signal_connect_swapped(pool->vallocator, "group-released", |
| 989 | G_CALLBACK(gst_aml_v4l2_buffer_pool_resurrect_buffer), pool); |
| 990 | ret = gst_aml_v4l2_buffer_pool_streamon(pool); |
| 991 | } |
| 992 | |
| 993 | return ret; |
| 994 | |
| 995 | /* ERRORS */ |
| 996 | wrong_config: |
| 997 | { |
| 998 | GST_ERROR_OBJECT(pool, "invalid config %" GST_PTR_FORMAT, config); |
| 999 | gst_structure_free(config); |
| 1000 | return FALSE; |
| 1001 | } |
| 1002 | no_buffers: |
| 1003 | { |
| 1004 | GST_ERROR_OBJECT(pool, |
| 1005 | "we received %d buffer from device '%s', we want at least %d", |
| 1006 | min_buffers, obj->videodev, GST_AML_V4L2_MIN_BUFFERS); |
| 1007 | gst_structure_free(config); |
| 1008 | return FALSE; |
| 1009 | } |
| 1010 | start_failed: |
| 1011 | { |
| 1012 | GST_ERROR_OBJECT(pool, "allocate failed"); |
| 1013 | return FALSE; |
| 1014 | } |
| 1015 | other_pool_failed: |
| 1016 | { |
| 1017 | GST_ERROR_OBJECT(pool, "failed to activate the other pool %" GST_PTR_FORMAT, pool->other_pool); |
| 1018 | return FALSE; |
| 1019 | } |
| 1020 | queue_failed: |
| 1021 | { |
| 1022 | GST_ERROR_OBJECT(pool, "failed to queue buffers into the capture queue"); |
| 1023 | return FALSE; |
| 1024 | } |
| 1025 | cannot_import: |
| 1026 | { |
| 1027 | GST_ERROR_OBJECT(pool, "cannot import buffers from downstream pool"); |
| 1028 | return FALSE; |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | static gboolean |
| 1033 | gst_aml_v4l2_buffer_pool_vallocator_stop(GstAmlV4l2BufferPool *pool) |
| 1034 | { |
| 1035 | GstAmlV4l2Return vret; |
| 1036 | |
| 1037 | if (!pool->vallocator) |
| 1038 | return TRUE; |
| 1039 | |
| 1040 | vret = gst_aml_v4l2_allocator_stop(pool->vallocator); |
| 1041 | |
| 1042 | if (vret == GST_V4L2_BUSY) |
| 1043 | GST_WARNING_OBJECT(pool, "some buffers are still outstanding"); |
| 1044 | |
| 1045 | return (vret == GST_V4L2_OK); |
| 1046 | } |
| 1047 | |
| 1048 | static gboolean |
| 1049 | gst_aml_v4l2_buffer_pool_stop(GstBufferPool *bpool) |
| 1050 | { |
| 1051 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(bpool); |
| 1052 | gboolean ret; |
| 1053 | |
| 1054 | if (pool->orphaned) |
| 1055 | return gst_aml_v4l2_buffer_pool_vallocator_stop(pool); |
| 1056 | |
| 1057 | GST_DEBUG_OBJECT(pool, "stopping pool"); |
| 1058 | |
| 1059 | if (pool->group_released_handler > 0) |
| 1060 | { |
| 1061 | g_signal_handler_disconnect(pool->vallocator, |
| 1062 | pool->group_released_handler); |
| 1063 | pool->group_released_handler = 0; |
| 1064 | } |
| 1065 | |
| 1066 | gst_aml_v4l2_buffer_pool_streamoff(pool); |
| 1067 | |
| 1068 | ret = GST_BUFFER_POOL_CLASS(parent_class)->stop(bpool); |
| 1069 | |
| 1070 | if (ret) |
| 1071 | ret = gst_aml_v4l2_buffer_pool_vallocator_stop(pool); |
| 1072 | |
| 1073 | GST_DEBUG_OBJECT(pool, "stopping other_pool"); |
| 1074 | if (pool->other_pool) |
| 1075 | { |
| 1076 | gst_buffer_pool_set_active(pool->other_pool, FALSE); |
| 1077 | gst_object_unref(pool->other_pool); |
| 1078 | pool->other_pool = NULL; |
| 1079 | } |
| 1080 | |
| 1081 | return ret; |
| 1082 | } |
| 1083 | |
| 1084 | gboolean |
| 1085 | gst_aml_v4l2_buffer_pool_orphan(GstBufferPool **bpool) |
| 1086 | { |
| 1087 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(*bpool); |
| 1088 | gboolean ret; |
| 1089 | |
| 1090 | if (!GST_AML_V4L2_ALLOCATOR_CAN_ORPHAN_BUFS(pool->vallocator)) |
| 1091 | return FALSE; |
| 1092 | |
| 1093 | if (g_getenv("GST_V4L2_FORCE_DRAIN")) |
| 1094 | return FALSE; |
| 1095 | |
| 1096 | GST_DEBUG_OBJECT(pool, "orphaning pool"); |
| 1097 | |
| 1098 | gst_buffer_pool_set_active(*bpool, FALSE); |
| 1099 | /* |
| 1100 | * If the buffer pool has outstanding buffers, it will not be stopped |
| 1101 | * by the base class when set inactive. Stop it manually and mark it |
| 1102 | * as orphaned |
| 1103 | */ |
| 1104 | ret = gst_aml_v4l2_buffer_pool_stop(*bpool); |
| 1105 | if (!ret) |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame^] | 1106 | { |
| 1107 | GST_DEBUG_OBJECT(pool, "stop poll fail, try to orphaning allocator"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1108 | ret = gst_aml_v4l2_allocator_orphan(pool->vallocator); |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame^] | 1109 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1110 | |
| 1111 | if (!ret) |
| 1112 | goto orphan_failed; |
| 1113 | |
| 1114 | pool->orphaned = TRUE; |
| 1115 | gst_object_unref(*bpool); |
| 1116 | *bpool = NULL; |
| 1117 | |
| 1118 | orphan_failed: |
| 1119 | return ret; |
| 1120 | } |
| 1121 | |
| 1122 | static void |
| 1123 | gst_aml_v4l2_buffer_pool_flush_start(GstBufferPool *bpool) |
| 1124 | { |
| 1125 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(bpool); |
| 1126 | |
| 1127 | GST_DEBUG_OBJECT(pool, "start flushing"); |
| 1128 | |
| 1129 | gst_poll_set_flushing(pool->poll, TRUE); |
| 1130 | |
| 1131 | GST_OBJECT_LOCK(pool); |
| 1132 | pool->empty = FALSE; |
| 1133 | g_cond_broadcast(&pool->empty_cond); |
| 1134 | GST_OBJECT_UNLOCK(pool); |
| 1135 | |
| 1136 | if (pool->other_pool) |
| 1137 | gst_buffer_pool_set_flushing(pool->other_pool, TRUE); |
| 1138 | } |
| 1139 | |
| 1140 | static void |
| 1141 | gst_aml_v4l2_buffer_pool_flush_stop(GstBufferPool *bpool) |
| 1142 | { |
| 1143 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(bpool); |
| 1144 | |
| 1145 | GST_DEBUG_OBJECT(pool, "stop flushing"); |
| 1146 | |
| 1147 | if (pool->other_pool) |
| 1148 | gst_buffer_pool_set_flushing(pool->other_pool, FALSE); |
| 1149 | |
| 1150 | gst_poll_set_flushing(pool->poll, FALSE); |
| 1151 | } |
| 1152 | |
| 1153 | static GstFlowReturn |
| 1154 | gst_aml_v4l2_buffer_pool_poll(GstAmlV4l2BufferPool *pool, gboolean wait) |
| 1155 | { |
| 1156 | gint ret; |
| 1157 | GstClockTime timeout; |
| 1158 | gint try_num = 0; |
| 1159 | |
| 1160 | if (wait) |
| 1161 | timeout = GST_CLOCK_TIME_NONE; |
| 1162 | else |
| 1163 | timeout = 0; |
| 1164 | |
| 1165 | /* In RW mode there is no queue, hence no need to wait while the queue is |
| 1166 | * empty */ |
xuesong.jiang | 93f2dcb | 2022-07-15 14:24:55 +0800 | [diff] [blame] | 1167 | |
| 1168 | if ((pool->obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE || pool->obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) && |
| 1169 | pool->obj->mode == GST_V4L2_IO_DMABUF_IMPORT) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1170 | { |
xuesong.jiang | 93f2dcb | 2022-07-15 14:24:55 +0800 | [diff] [blame] | 1171 | GST_LOG_OBJECT(pool, "CAPTURE DMA don't quit when empty buf"); |
| 1172 | } |
| 1173 | else |
| 1174 | { |
| 1175 | if (pool->obj->mode != GST_V4L2_IO_RW) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1176 | { |
xuesong.jiang | 93f2dcb | 2022-07-15 14:24:55 +0800 | [diff] [blame] | 1177 | GST_OBJECT_LOCK(pool); |
| 1178 | |
| 1179 | if (!wait && pool->empty) |
| 1180 | { |
| 1181 | GST_OBJECT_UNLOCK(pool); |
| 1182 | goto no_buffers; |
| 1183 | } |
| 1184 | |
| 1185 | while (pool->empty) |
| 1186 | g_cond_wait(&pool->empty_cond, GST_OBJECT_GET_LOCK(pool)); |
| 1187 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1188 | GST_OBJECT_UNLOCK(pool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1189 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | if (!pool->can_poll_device) |
| 1193 | { |
| 1194 | if (wait) |
| 1195 | goto done; |
| 1196 | else |
| 1197 | goto no_buffers; |
| 1198 | } |
| 1199 | |
| 1200 | GST_LOG_OBJECT(pool, "polling device"); |
| 1201 | |
| 1202 | again: |
| 1203 | ret = gst_poll_wait(pool->poll, timeout); |
| 1204 | #ifdef GST_AML_SPEC_FLOW_FOR_VBP |
| 1205 | GST_DEBUG_OBJECT(pool, "amlmodbuf poll timeout:%lld, ret:%d, errno:%d", timeout, ret, errno); |
| 1206 | #endif |
| 1207 | if (G_UNLIKELY(ret < 0)) |
| 1208 | { |
| 1209 | switch (errno) |
| 1210 | { |
| 1211 | case EBUSY: |
| 1212 | goto stopped; |
| 1213 | case EAGAIN: |
| 1214 | case EINTR: |
| 1215 | goto again; |
| 1216 | case ENXIO: |
| 1217 | GST_WARNING_OBJECT(pool, |
| 1218 | "v4l2 device doesn't support polling. Disabling" |
| 1219 | " using libv4l2 in this case may cause deadlocks"); |
| 1220 | pool->can_poll_device = FALSE; |
| 1221 | goto done; |
| 1222 | default: |
| 1223 | goto select_error; |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | if (gst_poll_fd_has_error(pool->poll, &pool->pollfd)) |
| 1228 | goto select_error; |
| 1229 | |
| 1230 | if (ret == 0) |
| 1231 | { |
| 1232 | #ifdef GST_AML_SPEC_FLOW_FOR_VBP |
| 1233 | if ((pool->obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE || pool->obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) && |
| 1234 | pool->obj->mode == GST_V4L2_IO_DMABUF_IMPORT) |
| 1235 | { |
| 1236 | GST_DEBUG_OBJECT(pool, "amlmodbuf can't get buffer in capture obj dmaimport mode, try release buf from other pool"); |
| 1237 | gst_aml_v4l2_buffer_pool_dump_stat(pool, GST_DUMP_CAPTURE_BP_STAT_FILENAME, try_num++); |
| 1238 | gst_aml_v4l2_buffer_pool_release_buffer_aml_patch((GstBufferPool *)pool); |
| 1239 | goto again; |
| 1240 | } |
| 1241 | else |
| 1242 | #endif |
| 1243 | goto no_buffers; |
| 1244 | } |
| 1245 | |
| 1246 | done: |
| 1247 | return GST_FLOW_OK; |
| 1248 | |
| 1249 | /* ERRORS */ |
| 1250 | stopped: |
| 1251 | { |
| 1252 | GST_DEBUG_OBJECT(pool, "stop called"); |
| 1253 | return GST_FLOW_FLUSHING; |
| 1254 | } |
| 1255 | select_error: |
| 1256 | { |
| 1257 | GST_ELEMENT_ERROR(pool->obj->element, RESOURCE, READ, (NULL), |
| 1258 | ("poll error %d: %s (%d)", ret, g_strerror(errno), errno)); |
| 1259 | return GST_FLOW_ERROR; |
| 1260 | } |
| 1261 | no_buffers: |
| 1262 | return GST_FLOW_CUSTOM_SUCCESS; |
| 1263 | } |
| 1264 | |
| 1265 | static GstFlowReturn |
| 1266 | gst_aml_v4l2_buffer_pool_qbuf(GstAmlV4l2BufferPool *pool, GstBuffer *buf, |
| 1267 | GstAmlV4l2MemoryGroup *group) |
| 1268 | { |
| 1269 | const GstAmlV4l2Object *obj = pool->obj; |
| 1270 | GstClockTime timestamp; |
| 1271 | gint index; |
| 1272 | |
| 1273 | index = group->buffer.index; |
| 1274 | |
| 1275 | if (pool->buffers[index] != NULL) |
| 1276 | goto already_queued; |
| 1277 | |
| 1278 | GST_LOG_OBJECT(pool, "queuing buffer %i", index); |
| 1279 | |
| 1280 | if (V4L2_TYPE_IS_OUTPUT(obj->type)) |
| 1281 | { |
| 1282 | enum v4l2_field field; |
| 1283 | |
| 1284 | /* Except when field is set to alternate, buffer field is the same as |
| 1285 | * the one defined in format */ |
| 1286 | if (V4L2_TYPE_IS_MULTIPLANAR(obj->type)) |
| 1287 | field = obj->format.fmt.pix_mp.field; |
| 1288 | else |
| 1289 | field = obj->format.fmt.pix.field; |
| 1290 | |
| 1291 | /* NB: At this moment, we can't have alternate mode because it not handled |
| 1292 | * yet */ |
| 1293 | if (field == V4L2_FIELD_ALTERNATE) |
| 1294 | { |
| 1295 | if (GST_BUFFER_FLAG_IS_SET(buf, GST_VIDEO_FRAME_FLAG_TFF)) |
| 1296 | field = V4L2_FIELD_TOP; |
| 1297 | else |
| 1298 | field = V4L2_FIELD_BOTTOM; |
| 1299 | } |
| 1300 | |
| 1301 | group->buffer.field = field; |
| 1302 | } |
| 1303 | |
| 1304 | if (GST_BUFFER_TIMESTAMP_IS_VALID(buf)) |
| 1305 | { |
| 1306 | timestamp = GST_BUFFER_TIMESTAMP(buf); |
| 1307 | GST_TIME_TO_TIMEVAL(timestamp, group->buffer.timestamp); |
| 1308 | } |
zengliang.li | 32cb11e | 2022-11-24 12:10:26 +0800 | [diff] [blame] | 1309 | else |
| 1310 | { |
| 1311 | group->buffer.timestamp.tv_sec= -1; |
| 1312 | group->buffer.timestamp.tv_usec= 0; |
| 1313 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1314 | |
| 1315 | GST_OBJECT_LOCK(pool); |
| 1316 | g_atomic_int_inc(&pool->num_queued); |
| 1317 | pool->buffers[index] = buf; |
| 1318 | |
| 1319 | if (!gst_aml_v4l2_allocator_qbuf(pool->vallocator, group)) |
| 1320 | goto queue_failed; |
| 1321 | |
| 1322 | if (!V4L2_TYPE_IS_OUTPUT(obj->type)) |
| 1323 | { |
| 1324 | gst_aml_v4l2_buffer_pool_dump_stat(pool, GST_DUMP_CAPTURE_BP_STAT_FILENAME, 0); |
| 1325 | } |
| 1326 | |
| 1327 | pool->empty = FALSE; |
| 1328 | g_cond_signal(&pool->empty_cond); |
| 1329 | GST_OBJECT_UNLOCK(pool); |
| 1330 | |
| 1331 | return GST_FLOW_OK; |
| 1332 | |
| 1333 | already_queued: |
| 1334 | { |
| 1335 | GST_ERROR_OBJECT(pool, "the buffer %i was already queued", index); |
| 1336 | return GST_FLOW_ERROR; |
| 1337 | } |
| 1338 | queue_failed: |
| 1339 | { |
| 1340 | GST_ERROR_OBJECT(pool, "could not queue a buffer %i", index); |
| 1341 | /* Mark broken buffer to the allocator */ |
| 1342 | GST_BUFFER_FLAG_SET(buf, GST_BUFFER_FLAG_TAG_MEMORY); |
| 1343 | g_atomic_int_add(&pool->num_queued, -1); |
| 1344 | pool->buffers[index] = NULL; |
| 1345 | GST_OBJECT_UNLOCK(pool); |
| 1346 | return GST_FLOW_ERROR; |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | static GstFlowReturn |
| 1351 | gst_aml_v4l2_buffer_pool_dqevent(GstAmlV4l2BufferPool *pool) |
| 1352 | { |
| 1353 | GstAmlV4l2Object *v4l2object = pool->obj; |
| 1354 | struct v4l2_event evt; |
| 1355 | |
| 1356 | memset(&evt, 0x00, sizeof(struct v4l2_event)); |
| 1357 | if (v4l2object->ioctl(pool->video_fd, VIDIOC_DQEVENT, &evt) < 0) |
| 1358 | goto dqevent_failed; |
| 1359 | |
| 1360 | switch (evt.type) |
| 1361 | { |
| 1362 | case V4L2_EVENT_SOURCE_CHANGE: |
sheng.liu | 8d18ed2 | 2022-05-26 17:28:15 +0800 | [diff] [blame] | 1363 | |
| 1364 | if (evt.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION) |
| 1365 | return GST_AML_V4L2_FLOW_SOURCE_CHANGE; |
| 1366 | GST_WARNING_OBJECT (pool, "Unknown source change 0x%x - skipped", evt.u.src_change.changes); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1367 | break; |
| 1368 | case V4L2_EVENT_EOS: |
| 1369 | return GST_AML_V4L2_FLOW_LAST_BUFFER; |
| 1370 | break; |
| 1371 | default: |
| 1372 | break; |
| 1373 | } |
| 1374 | |
| 1375 | return GST_FLOW_OK; |
| 1376 | |
| 1377 | /* ERRORS */ |
| 1378 | dqevent_failed: |
| 1379 | { |
| 1380 | return GST_FLOW_ERROR; |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | static GstFlowReturn |
| 1385 | gst_aml_v4l2_buffer_pool_dqbuf(GstAmlV4l2BufferPool *pool, GstBuffer **buffer, |
| 1386 | gboolean wait) |
| 1387 | { |
| 1388 | GstFlowReturn res; |
| 1389 | GstBuffer *outbuf = NULL; |
| 1390 | GstAmlV4l2Object *obj = pool->obj; |
| 1391 | GstClockTime timestamp; |
| 1392 | GstAmlV4l2MemoryGroup *group; |
| 1393 | GstVideoMeta *vmeta; |
| 1394 | gsize size; |
| 1395 | gint i; |
| 1396 | |
| 1397 | res = gst_aml_v4l2_allocator_dqbuf(pool->vallocator, &group); |
| 1398 | if (res == GST_FLOW_EOS) |
| 1399 | goto eos; |
| 1400 | if (res != GST_FLOW_OK) |
| 1401 | goto dqbuf_failed; |
| 1402 | |
| 1403 | /* get our GstBuffer with that index from the pool, if the buffer was |
| 1404 | * outstanding we have a serious problem. |
| 1405 | */ |
| 1406 | outbuf = pool->buffers[group->buffer.index]; |
| 1407 | if (outbuf == NULL) |
| 1408 | goto no_buffer; |
| 1409 | |
| 1410 | /* mark the buffer outstanding */ |
| 1411 | pool->buffers[group->buffer.index] = NULL; |
| 1412 | if (g_atomic_int_dec_and_test(&pool->num_queued)) |
| 1413 | { |
| 1414 | GST_OBJECT_LOCK(pool); |
| 1415 | pool->empty = TRUE; |
| 1416 | GST_OBJECT_UNLOCK(pool); |
| 1417 | } |
| 1418 | |
| 1419 | timestamp = GST_TIMEVAL_TO_TIME(group->buffer.timestamp); |
| 1420 | |
| 1421 | size = 0; |
| 1422 | vmeta = gst_buffer_get_video_meta(outbuf); |
| 1423 | for (i = 0; i < group->n_mem; i++) |
| 1424 | { |
| 1425 | GST_LOG_OBJECT(pool, |
| 1426 | "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %" GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf, |
| 1427 | group->buffer.sequence, group->buffer.index, group->mem[i], |
| 1428 | group->planes[i].bytesused, i, group->buffer.flags, |
| 1429 | GST_TIME_ARGS(timestamp), pool->num_queued, outbuf); |
| 1430 | |
| 1431 | if (vmeta) |
| 1432 | { |
| 1433 | vmeta->offset[i] = size; |
| 1434 | size += gst_memory_get_sizes(group->mem[i], NULL, NULL); |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | /* Ignore timestamp and field for OUTPUT device */ |
| 1439 | if (V4L2_TYPE_IS_OUTPUT(obj->type)) |
| 1440 | goto done; |
| 1441 | |
| 1442 | /* Check for driver bug in reporting feild */ |
| 1443 | if (group->buffer.field == V4L2_FIELD_ANY) |
| 1444 | { |
| 1445 | /* Only warn once to avoid the spamming */ |
| 1446 | #ifndef GST_DISABLE_GST_DEBUG |
| 1447 | if (!pool->has_warned_on_buggy_field) |
| 1448 | { |
| 1449 | pool->has_warned_on_buggy_field = TRUE; |
| 1450 | GST_WARNING_OBJECT(pool, |
| 1451 | "Driver should never set v4l2_buffer.field to ANY"); |
| 1452 | } |
| 1453 | #endif |
| 1454 | |
| 1455 | /* Use the value from the format (works for UVC bug) */ |
| 1456 | group->buffer.field = obj->format.fmt.pix.field; |
| 1457 | |
| 1458 | /* If driver also has buggy S_FMT, assume progressive */ |
| 1459 | if (group->buffer.field == V4L2_FIELD_ANY) |
| 1460 | { |
| 1461 | #ifndef GST_DISABLE_GST_DEBUG |
| 1462 | if (!pool->has_warned_on_buggy_field) |
| 1463 | { |
| 1464 | pool->has_warned_on_buggy_field = TRUE; |
| 1465 | GST_WARNING_OBJECT(pool, |
| 1466 | "Driver should never set v4l2_format.pix.field to ANY"); |
| 1467 | } |
| 1468 | #endif |
| 1469 | |
| 1470 | group->buffer.field = V4L2_FIELD_NONE; |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | /* set top/bottom field first if v4l2_buffer has the information */ |
| 1475 | switch (group->buffer.field) |
| 1476 | { |
| 1477 | case V4L2_FIELD_NONE: |
| 1478 | GST_BUFFER_FLAG_UNSET(outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED); |
| 1479 | GST_BUFFER_FLAG_UNSET(outbuf, GST_VIDEO_BUFFER_FLAG_TFF); |
| 1480 | break; |
| 1481 | case V4L2_FIELD_INTERLACED_TB: |
| 1482 | GST_BUFFER_FLAG_SET(outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED); |
| 1483 | GST_BUFFER_FLAG_SET(outbuf, GST_VIDEO_BUFFER_FLAG_TFF); |
| 1484 | break; |
| 1485 | case V4L2_FIELD_INTERLACED_BT: |
| 1486 | GST_BUFFER_FLAG_SET(outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED); |
| 1487 | GST_BUFFER_FLAG_UNSET(outbuf, GST_VIDEO_BUFFER_FLAG_TFF); |
| 1488 | break; |
| 1489 | case V4L2_FIELD_INTERLACED: |
| 1490 | GST_BUFFER_FLAG_SET(outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED); |
| 1491 | if (obj->tv_norm == V4L2_STD_NTSC_M || |
| 1492 | obj->tv_norm == V4L2_STD_NTSC_M_JP || |
| 1493 | obj->tv_norm == V4L2_STD_NTSC_M_KR) |
| 1494 | { |
| 1495 | GST_BUFFER_FLAG_UNSET(outbuf, GST_VIDEO_BUFFER_FLAG_TFF); |
| 1496 | } |
| 1497 | else |
| 1498 | { |
| 1499 | GST_BUFFER_FLAG_SET(outbuf, GST_VIDEO_BUFFER_FLAG_TFF); |
| 1500 | } |
| 1501 | break; |
| 1502 | default: |
| 1503 | GST_BUFFER_FLAG_UNSET(outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED); |
| 1504 | GST_BUFFER_FLAG_UNSET(outbuf, GST_VIDEO_BUFFER_FLAG_TFF); |
| 1505 | GST_FIXME_OBJECT(pool, |
| 1506 | "Unhandled enum v4l2_field %d - treating as progressive", |
| 1507 | group->buffer.field); |
| 1508 | break; |
| 1509 | } |
| 1510 | |
| 1511 | if (GST_VIDEO_INFO_FORMAT(&obj->info) == GST_VIDEO_FORMAT_ENCODED) |
| 1512 | { |
| 1513 | if ((group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME) || |
| 1514 | GST_AML_V4L2_PIXELFORMAT(obj) == V4L2_PIX_FMT_MJPEG || |
| 1515 | GST_AML_V4L2_PIXELFORMAT(obj) == V4L2_PIX_FMT_JPEG || |
| 1516 | GST_AML_V4L2_PIXELFORMAT(obj) == V4L2_PIX_FMT_PJPG) |
| 1517 | GST_BUFFER_FLAG_UNSET(outbuf, GST_BUFFER_FLAG_DELTA_UNIT); |
| 1518 | else |
| 1519 | GST_BUFFER_FLAG_SET(outbuf, GST_BUFFER_FLAG_DELTA_UNIT); |
| 1520 | } |
| 1521 | |
| 1522 | if (group->buffer.flags & V4L2_BUF_FLAG_ERROR) |
| 1523 | GST_BUFFER_FLAG_SET(outbuf, GST_BUFFER_FLAG_CORRUPTED); |
| 1524 | |
| 1525 | GST_BUFFER_TIMESTAMP(outbuf) = timestamp; |
| 1526 | GST_BUFFER_OFFSET(outbuf) = group->buffer.sequence; |
| 1527 | GST_BUFFER_OFFSET_END(outbuf) = group->buffer.sequence + 1; |
| 1528 | |
| 1529 | done: |
| 1530 | *buffer = outbuf; |
| 1531 | |
| 1532 | if (!V4L2_TYPE_IS_OUTPUT(obj->type)) |
| 1533 | { |
| 1534 | gst_aml_v4l2_buffer_pool_dump_stat(pool, GST_DUMP_CAPTURE_BP_STAT_FILENAME, 0); |
| 1535 | } |
| 1536 | |
| 1537 | return res; |
| 1538 | |
| 1539 | /* ERRORS */ |
| 1540 | eos: |
| 1541 | { |
| 1542 | return GST_FLOW_EOS; |
| 1543 | } |
| 1544 | dqbuf_failed: |
| 1545 | { |
| 1546 | return GST_FLOW_ERROR; |
| 1547 | } |
| 1548 | no_buffer: |
| 1549 | { |
| 1550 | GST_ERROR_OBJECT(pool, "No free buffer found in the pool at index %d.", |
| 1551 | group->buffer.index); |
| 1552 | return GST_FLOW_ERROR; |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | static GstFlowReturn |
| 1557 | gst_aml_v4l2_buffer_pool_dequeue(GstAmlV4l2BufferPool *pool, GstBuffer **buffer, |
| 1558 | gboolean wait) |
| 1559 | { |
| 1560 | GstFlowReturn res; |
| 1561 | GstAmlV4l2Object *obj = pool->obj; |
| 1562 | |
| 1563 | if ((res = gst_aml_v4l2_buffer_pool_poll(pool, wait)) != GST_FLOW_OK) |
| 1564 | goto poll_failed; |
| 1565 | |
| 1566 | if (obj->can_wait_event && gst_poll_fd_can_read_pri(pool->poll, &pool->pollfd)) |
| 1567 | { |
sheng.liu | 8d18ed2 | 2022-05-26 17:28:15 +0800 | [diff] [blame] | 1568 | GstFlowReturn res_event = gst_aml_v4l2_buffer_pool_dqevent(pool); |
| 1569 | if (res_event != GST_FLOW_OK) |
| 1570 | return res_event; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | if (res == GST_FLOW_CUSTOM_SUCCESS) |
| 1574 | { |
| 1575 | GST_LOG_OBJECT(pool, "nothing to dequeue"); |
| 1576 | *buffer = NULL; |
| 1577 | return res; |
| 1578 | } |
| 1579 | |
| 1580 | GST_LOG_OBJECT(pool, "dequeueing a buffer"); |
| 1581 | return gst_aml_v4l2_buffer_pool_dqbuf(pool, buffer, wait); |
| 1582 | |
| 1583 | /* ERRORS */ |
| 1584 | poll_failed: |
| 1585 | { |
| 1586 | GST_DEBUG_OBJECT(pool, "poll error %s", gst_flow_get_name(res)); |
| 1587 | return res; |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | static GstFlowReturn |
| 1592 | gst_aml_v4l2_buffer_pool_acquire_buffer(GstBufferPool *bpool, GstBuffer **buffer, |
| 1593 | GstBufferPoolAcquireParams *params) |
| 1594 | { |
| 1595 | GstFlowReturn ret; |
| 1596 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(bpool); |
| 1597 | GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS(parent_class); |
| 1598 | GstAmlV4l2Object *obj = pool->obj; |
| 1599 | |
| 1600 | GST_DEBUG_OBJECT(pool, "acquire"); |
| 1601 | |
| 1602 | /* If this is being called to resurrect a lost buffer */ |
| 1603 | if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) |
| 1604 | { |
| 1605 | ret = pclass->acquire_buffer(bpool, buffer, params); |
| 1606 | goto done; |
| 1607 | } |
| 1608 | |
| 1609 | switch (obj->type) |
| 1610 | { |
| 1611 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 1612 | case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: |
| 1613 | /* capture, This function should return a buffer with new captured data */ |
| 1614 | switch (obj->mode) |
| 1615 | { |
| 1616 | case GST_V4L2_IO_RW: |
| 1617 | { |
| 1618 | /* take empty buffer from the pool */ |
| 1619 | ret = pclass->acquire_buffer(bpool, buffer, params); |
| 1620 | break; |
| 1621 | } |
| 1622 | case GST_V4L2_IO_DMABUF: |
| 1623 | case GST_V4L2_IO_MMAP: |
| 1624 | case GST_V4L2_IO_USERPTR: |
xuesong.jiang | 3df0f7b | 2022-11-29 14:52:45 +0800 | [diff] [blame] | 1625 | { |
| 1626 | /* just dequeue a buffer, we basically use the queue of v4l2 as the |
| 1627 | * storage for our buffers. This function does poll first so we can |
| 1628 | * interrupt it fine. */ |
| 1629 | ret = gst_aml_v4l2_buffer_pool_dequeue(pool, buffer, TRUE); |
| 1630 | |
| 1631 | break; |
| 1632 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1633 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 1634 | { |
| 1635 | #ifdef GST_AML_SPEC_FLOW_FOR_VBP |
| 1636 | GST_DEBUG_OBJECT(pool, "amlmodbuf return free buf before acquire buf"); |
| 1637 | gst_aml_v4l2_buffer_pool_release_buffer_aml_patch(bpool); |
| 1638 | ret = gst_aml_v4l2_buffer_pool_dequeue(pool, buffer, FALSE); |
| 1639 | #else |
| 1640 | /* just dequeue a buffer, we basically use the queue of v4l2 as the |
| 1641 | * storage for our buffers. This function does poll first so we can |
| 1642 | * interrupt it fine. */ |
| 1643 | ret = gst_aml_v4l2_buffer_pool_dequeue(pool, buffer, TRUE); |
| 1644 | #endif |
| 1645 | break; |
| 1646 | } |
| 1647 | default: |
| 1648 | ret = GST_FLOW_ERROR; |
| 1649 | g_assert_not_reached(); |
| 1650 | break; |
| 1651 | } |
| 1652 | break; |
| 1653 | |
| 1654 | case V4L2_BUF_TYPE_VIDEO_OUTPUT: |
| 1655 | case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: |
| 1656 | /* playback, This function should return an empty buffer */ |
| 1657 | switch (obj->mode) |
| 1658 | { |
| 1659 | case GST_V4L2_IO_RW: |
| 1660 | /* get an empty buffer */ |
| 1661 | ret = pclass->acquire_buffer(bpool, buffer, params); |
| 1662 | break; |
| 1663 | |
| 1664 | case GST_V4L2_IO_MMAP: |
| 1665 | case GST_V4L2_IO_DMABUF: |
| 1666 | case GST_V4L2_IO_USERPTR: |
| 1667 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 1668 | /* get a free unqueued buffer */ |
| 1669 | ret = pclass->acquire_buffer(bpool, buffer, params); |
| 1670 | break; |
| 1671 | |
| 1672 | default: |
| 1673 | ret = GST_FLOW_ERROR; |
| 1674 | g_assert_not_reached(); |
| 1675 | break; |
| 1676 | } |
| 1677 | break; |
| 1678 | |
| 1679 | default: |
| 1680 | ret = GST_FLOW_ERROR; |
| 1681 | g_assert_not_reached(); |
| 1682 | break; |
| 1683 | } |
| 1684 | done: |
| 1685 | return ret; |
| 1686 | } |
| 1687 | |
| 1688 | static void |
| 1689 | gst_aml_v4l2_buffer_pool_release_buffer(GstBufferPool *bpool, GstBuffer *buffer) |
| 1690 | { |
| 1691 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(bpool); |
| 1692 | GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS(parent_class); |
| 1693 | GstAmlV4l2Object *obj = pool->obj; |
| 1694 | |
| 1695 | GST_DEBUG_OBJECT(pool, "release buffer %p", buffer); |
| 1696 | |
| 1697 | /* If the buffer's pool has been orphaned, dispose of it so that |
| 1698 | * the pool resources can be freed */ |
| 1699 | if (pool->orphaned) |
| 1700 | { |
| 1701 | GST_BUFFER_FLAG_SET(buffer, GST_BUFFER_FLAG_TAG_MEMORY); |
| 1702 | pclass->release_buffer(bpool, buffer); |
| 1703 | return; |
| 1704 | } |
| 1705 | |
| 1706 | switch (obj->type) |
| 1707 | { |
| 1708 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 1709 | case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: |
| 1710 | /* capture, put the buffer back in the queue so that we can refill it |
| 1711 | * later. */ |
| 1712 | switch (obj->mode) |
| 1713 | { |
| 1714 | case GST_V4L2_IO_RW: |
| 1715 | /* release back in the pool */ |
| 1716 | pclass->release_buffer(bpool, buffer); |
| 1717 | break; |
| 1718 | |
| 1719 | case GST_V4L2_IO_DMABUF: |
| 1720 | case GST_V4L2_IO_MMAP: |
| 1721 | case GST_V4L2_IO_USERPTR: |
| 1722 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 1723 | { |
| 1724 | GstAmlV4l2MemoryGroup *group; |
| 1725 | if (gst_aml_v4l2_is_buffer_valid(buffer, &group)) |
| 1726 | { |
| 1727 | GstFlowReturn ret = GST_FLOW_OK; |
| 1728 | |
| 1729 | gst_aml_v4l2_allocator_reset_group(pool->vallocator, group); |
| 1730 | |
| 1731 | #ifdef GST_AML_SPEC_FLOW_FOR_VBP |
| 1732 | GST_DEBUG_OBJECT(pool, "amlmodbuf trace in add flow with buf:%p index:%d", buffer, group->buffer.index); |
| 1733 | pool->read_to_free_bufs[group->buffer.index] = buffer; |
| 1734 | pool->ready_to_free_buf_num++; |
| 1735 | if (gst_aml_v4l2_buffer_pool_release_buffer_aml_patch(bpool)) |
| 1736 | { |
| 1737 | GST_DEBUG_OBJECT(pool, "amlmodbuf execute aml code logic, skip the following flow"); |
| 1738 | return; |
| 1739 | } |
| 1740 | #endif |
| 1741 | /* queue back in the device */ |
| 1742 | if (pool->other_pool) |
| 1743 | ret = gst_aml_v4l2_buffer_pool_prepare_buffer(pool, buffer, NULL); |
| 1744 | if (ret != GST_FLOW_OK || |
| 1745 | gst_aml_v4l2_buffer_pool_qbuf(pool, buffer, group) != GST_FLOW_OK) |
| 1746 | pclass->release_buffer(bpool, buffer); |
| 1747 | } |
| 1748 | else |
| 1749 | { |
| 1750 | /* Simply release invalide/modified buffer, the allocator will |
| 1751 | * give it back later */ |
| 1752 | GST_BUFFER_FLAG_SET(buffer, GST_BUFFER_FLAG_TAG_MEMORY); |
| 1753 | pclass->release_buffer(bpool, buffer); |
| 1754 | } |
| 1755 | break; |
| 1756 | } |
| 1757 | default: |
| 1758 | g_assert_not_reached(); |
| 1759 | break; |
| 1760 | } |
| 1761 | break; |
| 1762 | |
| 1763 | case V4L2_BUF_TYPE_VIDEO_OUTPUT: |
| 1764 | case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: |
| 1765 | switch (obj->mode) |
| 1766 | { |
| 1767 | case GST_V4L2_IO_RW: |
| 1768 | /* release back in the pool */ |
| 1769 | pclass->release_buffer(bpool, buffer); |
| 1770 | break; |
| 1771 | |
| 1772 | case GST_V4L2_IO_MMAP: |
| 1773 | case GST_V4L2_IO_DMABUF: |
| 1774 | case GST_V4L2_IO_USERPTR: |
| 1775 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 1776 | { |
| 1777 | GstAmlV4l2MemoryGroup *group; |
| 1778 | guint index; |
| 1779 | |
| 1780 | if (!gst_aml_v4l2_is_buffer_valid(buffer, &group)) |
| 1781 | { |
| 1782 | /* Simply release invalide/modified buffer, the allocator will |
| 1783 | * give it back later */ |
| 1784 | GST_BUFFER_FLAG_SET(buffer, GST_BUFFER_FLAG_TAG_MEMORY); |
| 1785 | pclass->release_buffer(bpool, buffer); |
| 1786 | break; |
| 1787 | } |
| 1788 | |
| 1789 | index = group->buffer.index; |
| 1790 | |
| 1791 | if (pool->buffers[index] == NULL) |
| 1792 | { |
| 1793 | GST_LOG_OBJECT(pool, "buffer %u not queued, putting on free list", |
| 1794 | index); |
| 1795 | |
| 1796 | /* Remove qdata, this will unmap any map data in userptr */ |
| 1797 | gst_mini_object_set_qdata(GST_MINI_OBJECT(buffer), |
| 1798 | GST_AML_V4L2_IMPORT_QUARK, NULL, NULL); |
| 1799 | |
| 1800 | /* reset to default size */ |
| 1801 | gst_aml_v4l2_allocator_reset_group(pool->vallocator, group); |
| 1802 | |
| 1803 | /* playback, put the buffer back in the queue to refill later. */ |
| 1804 | pclass->release_buffer(bpool, buffer); |
| 1805 | } |
| 1806 | else |
| 1807 | { |
| 1808 | /* the buffer is queued in the device but maybe not played yet. We just |
| 1809 | * leave it there and not make it available for future calls to acquire |
| 1810 | * for now. The buffer will be dequeued and reused later. */ |
| 1811 | GST_LOG_OBJECT(pool, "buffer %u is queued", index); |
| 1812 | } |
| 1813 | break; |
| 1814 | } |
| 1815 | |
| 1816 | default: |
| 1817 | g_assert_not_reached(); |
| 1818 | break; |
| 1819 | } |
| 1820 | break; |
| 1821 | |
| 1822 | default: |
| 1823 | g_assert_not_reached(); |
| 1824 | break; |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | #ifdef GST_AML_SPEC_FLOW_FOR_VBP |
| 1829 | static gboolean |
| 1830 | gst_aml_v4l2_buffer_pool_release_buffer_aml_patch(GstBufferPool *bpool) |
| 1831 | { |
| 1832 | GstFlowReturn ret = GST_FLOW_OK; |
| 1833 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(bpool); |
| 1834 | GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS(parent_class); |
| 1835 | GstAmlV4l2Object *obj = pool->obj; |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame^] | 1836 | GstAmlV4l2VideoDec *dec = (GstAmlV4l2VideoDec *)obj->element; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1837 | |
| 1838 | if (obj->mode == GST_V4L2_IO_DMABUF_IMPORT && pool->other_pool) |
| 1839 | { |
| 1840 | GstBuffer *src = NULL; |
| 1841 | GstBufferPoolAcquireParams params; |
| 1842 | |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame^] | 1843 | if (obj->old_other_pool || obj->old_old_other_pool) |
| 1844 | { |
| 1845 | guint outstanding_buf_num = 0; |
| 1846 | |
| 1847 | outstanding_buf_num = gst_aml_v4l2_object_get_outstanding_capture_buf_num(obj); |
| 1848 | GST_DEBUG_OBJECT(pool, "amlmodbuf oop outstanding buf num %d", outstanding_buf_num); |
| 1849 | if (outstanding_buf_num != obj->outstanding_buf_num) |
| 1850 | { |
| 1851 | guint update = 0; |
| 1852 | |
| 1853 | if (outstanding_buf_num > obj->outstanding_buf_num) |
| 1854 | { |
| 1855 | GST_ERROR_OBJECT(pool, "amlmodbuf old other pool recycle buffer error, outstanding from %d to %d", obj->outstanding_buf_num, outstanding_buf_num); |
| 1856 | return FALSE; |
| 1857 | } |
| 1858 | GST_DEBUG_OBJECT(pool, "amlmodbuf oop outstanding buf num from %d reduce to %d", obj->outstanding_buf_num, outstanding_buf_num); |
| 1859 | |
| 1860 | update = obj->outstanding_buf_num - outstanding_buf_num; |
| 1861 | if (!gst_buffer_pool_increase_max_num(pool->other_pool, update)) |
| 1862 | { |
| 1863 | GST_ERROR_OBJECT(pool, "amlmodbuf update other pool max buffer num error"); |
| 1864 | return FALSE; |
| 1865 | } |
| 1866 | |
| 1867 | obj->outstanding_buf_num = outstanding_buf_num; |
| 1868 | } |
| 1869 | } |
| 1870 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1871 | memset(¶ms, 0, sizeof(GstBufferPoolAcquireParams)); |
| 1872 | params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT; |
| 1873 | GST_DEBUG_OBJECT(pool, "amlmodbuf trace in aml release buf flow ready_to_free_buf_num:%d", pool->ready_to_free_buf_num); |
| 1874 | while (pool->ready_to_free_buf_num && gst_buffer_pool_acquire_buffer(pool->other_pool, &src, ¶ms) != GST_FLOW_ERROR && src != NULL) |
| 1875 | { |
| 1876 | gint i = 0; |
| 1877 | |
| 1878 | GST_DEBUG_OBJECT(pool, "amlmodbuf acquire buf:%p form other pool", src); |
| 1879 | for (; i < VIDEO_MAX_FRAME; i++) |
| 1880 | { |
| 1881 | GST_DEBUG_OBJECT(pool, "amlmodbuf check index:%d", i); |
| 1882 | if (pool->read_to_free_bufs[i]) |
| 1883 | { |
| 1884 | GstBuffer *bind_drm_buf = gst_mini_object_get_qdata(GST_MINI_OBJECT(pool->read_to_free_bufs[i]), GST_AML_V4L2_IMPORT_QUARK); |
| 1885 | if (bind_drm_buf == NULL) |
| 1886 | { |
| 1887 | GST_DEBUG_OBJECT(pool, "init flow, bind v4l2 capture buf[%d]:%p with drm buf:%p", i, pool->read_to_free_bufs[i], src); |
| 1888 | } |
| 1889 | else if (src != bind_drm_buf) |
| 1890 | { |
| 1891 | GST_DEBUG_OBJECT(pool, "v4l2 capture buf[%d]:%p bind drm buf:%p, not this one:%p, continue match", i, pool->read_to_free_bufs[i], bind_drm_buf, src); |
| 1892 | continue; |
| 1893 | } |
| 1894 | |
| 1895 | GST_DEBUG_OBJECT(pool, "v4l2 capture buf[%d]:%p found bind drm buf:%p", i, pool->read_to_free_bufs[i], src); |
| 1896 | GstFlowReturn isvalid = GST_FLOW_OK; |
| 1897 | GstAmlV4l2MemoryGroup *tmp_group = NULL; |
| 1898 | |
| 1899 | // bind_drm_buf= gst_mini_object_steal_qdata(GST_MINI_OBJECT(pool->read_to_free_bufs[i]), GST_AML_V4L2_IMPORT_QUARK); |
| 1900 | ret = gst_aml_v4l2_buffer_pool_import_dmabuf(pool, pool->read_to_free_bufs[i], src); |
| 1901 | gst_buffer_unref(src); |
| 1902 | src = NULL; |
| 1903 | isvalid = gst_aml_v4l2_is_buffer_valid(pool->read_to_free_bufs[i], &tmp_group); |
| 1904 | if ((ret != GST_FLOW_OK && isvalid) || gst_aml_v4l2_buffer_pool_qbuf(pool, pool->read_to_free_bufs[i], tmp_group) != GST_FLOW_OK) |
| 1905 | { |
| 1906 | GST_DEBUG_OBJECT(pool, "amlmodbuf go into error flow"); |
| 1907 | pclass->release_buffer(bpool, pool->read_to_free_bufs[i]); |
| 1908 | } |
| 1909 | pool->read_to_free_bufs[i] = NULL; |
| 1910 | pool->ready_to_free_buf_num--; |
| 1911 | GST_DEBUG_OBJECT(pool, "amlmodbuf queued buf:%d, into v4l2 bp", i); |
| 1912 | break; |
| 1913 | } |
| 1914 | } |
| 1915 | if (i == VIDEO_MAX_FRAME) |
| 1916 | { |
| 1917 | GST_ERROR_OBJECT(pool, "drm buf:%p can't match any v4l2 capture buf, error", src); |
| 1918 | gst_buffer_unref(src); |
| 1919 | src = NULL; |
| 1920 | return FALSE; |
| 1921 | } |
| 1922 | } |
| 1923 | GST_DEBUG_OBJECT(pool, "update all free drm buf into v4l2 capture buf pool, now ready_to_free_buf_num:%d", pool->ready_to_free_buf_num); |
| 1924 | return TRUE; |
| 1925 | } |
| 1926 | return FALSE; |
| 1927 | } |
| 1928 | #endif |
| 1929 | |
| 1930 | static void |
| 1931 | gst_aml_v4l2_buffer_pool_dispose(GObject *object) |
| 1932 | { |
| 1933 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(object); |
| 1934 | |
| 1935 | if (pool->vallocator) |
| 1936 | gst_object_unref(pool->vallocator); |
| 1937 | pool->vallocator = NULL; |
| 1938 | |
| 1939 | if (pool->allocator) |
| 1940 | gst_object_unref(pool->allocator); |
| 1941 | pool->allocator = NULL; |
| 1942 | |
| 1943 | if (pool->other_pool) |
| 1944 | gst_object_unref(pool->other_pool); |
| 1945 | pool->other_pool = NULL; |
| 1946 | |
| 1947 | G_OBJECT_CLASS(parent_class)->dispose(object); |
| 1948 | } |
| 1949 | |
| 1950 | static void |
| 1951 | gst_aml_v4l2_buffer_pool_finalize(GObject *object) |
| 1952 | { |
| 1953 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(object); |
| 1954 | |
| 1955 | if (pool->video_fd >= 0) |
| 1956 | pool->obj->close(pool->video_fd); |
| 1957 | |
| 1958 | gst_poll_free(pool->poll); |
| 1959 | |
| 1960 | /* This can't be done in dispose method because we must not set pointer |
| 1961 | * to NULL as it is part of the v4l2object and dispose could be called |
| 1962 | * multiple times */ |
| 1963 | gst_object_unref(pool->obj->element); |
| 1964 | |
| 1965 | g_cond_clear(&pool->empty_cond); |
| 1966 | |
| 1967 | /* FIXME have we done enough here ? */ |
| 1968 | |
| 1969 | G_OBJECT_CLASS(parent_class)->finalize(object); |
| 1970 | } |
| 1971 | |
| 1972 | static void |
| 1973 | gst_aml_v4l2_buffer_pool_init(GstAmlV4l2BufferPool *pool) |
| 1974 | { |
| 1975 | pool->poll = gst_poll_new(TRUE); |
| 1976 | pool->can_poll_device = TRUE; |
| 1977 | g_cond_init(&pool->empty_cond); |
hanghang.luo | 3128f10 | 2022-08-18 10:36:19 +0800 | [diff] [blame] | 1978 | GST_OBJECT_LOCK(pool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1979 | pool->empty = TRUE; |
hanghang.luo | 3128f10 | 2022-08-18 10:36:19 +0800 | [diff] [blame] | 1980 | GST_OBJECT_UNLOCK(pool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1981 | pool->orphaned = FALSE; |
| 1982 | } |
| 1983 | |
| 1984 | static void |
| 1985 | gst_aml_v4l2_buffer_pool_class_init(GstAmlV4l2BufferPoolClass *klass) |
| 1986 | { |
| 1987 | GObjectClass *object_class = G_OBJECT_CLASS(klass); |
| 1988 | GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS(klass); |
| 1989 | |
| 1990 | object_class->dispose = gst_aml_v4l2_buffer_pool_dispose; |
| 1991 | object_class->finalize = gst_aml_v4l2_buffer_pool_finalize; |
| 1992 | |
| 1993 | bufferpool_class->start = gst_aml_v4l2_buffer_pool_start; |
| 1994 | bufferpool_class->stop = gst_aml_v4l2_buffer_pool_stop; |
| 1995 | bufferpool_class->set_config = gst_aml_v4l2_buffer_pool_set_config; |
| 1996 | bufferpool_class->alloc_buffer = gst_aml_v4l2_buffer_pool_alloc_buffer; |
| 1997 | bufferpool_class->acquire_buffer = gst_aml_v4l2_buffer_pool_acquire_buffer; |
| 1998 | bufferpool_class->release_buffer = gst_aml_v4l2_buffer_pool_release_buffer; |
| 1999 | bufferpool_class->flush_start = gst_aml_v4l2_buffer_pool_flush_start; |
| 2000 | bufferpool_class->flush_stop = gst_aml_v4l2_buffer_pool_flush_stop; |
| 2001 | |
| 2002 | GST_DEBUG_CATEGORY_INIT(amlv4l2bufferpool_debug, "amlv4l2bufferpool", 0, |
| 2003 | "V4L2 Buffer Pool"); |
| 2004 | GST_DEBUG_CATEGORY_GET(CAT_PERFORMANCE, "GST_PERFORMANCE"); |
| 2005 | } |
| 2006 | |
| 2007 | /** |
| 2008 | * gst_aml_v4l2_buffer_pool_new: |
| 2009 | * @obj: the v4l2 object owning the pool |
| 2010 | * |
| 2011 | * Construct a new buffer pool. |
| 2012 | * |
| 2013 | * Returns: the new pool, use gst_object_unref() to free resources |
| 2014 | */ |
| 2015 | GstBufferPool * |
| 2016 | gst_aml_v4l2_buffer_pool_new(GstAmlV4l2Object *obj, GstCaps *caps) |
| 2017 | { |
| 2018 | GstAmlV4l2BufferPool *pool; |
| 2019 | GstStructure *config; |
| 2020 | gchar *name, *parent_name; |
| 2021 | gint fd; |
| 2022 | |
| 2023 | fd = obj->dup(obj->video_fd); |
| 2024 | if (fd < 0) |
| 2025 | goto dup_failed; |
| 2026 | |
| 2027 | /* setting a significant unique name */ |
| 2028 | parent_name = gst_object_get_name(GST_OBJECT(obj->element)); |
| 2029 | name = g_strconcat(parent_name, ":", "pool:", |
| 2030 | V4L2_TYPE_IS_OUTPUT(obj->type) ? "sink" : "src", NULL); |
| 2031 | g_free(parent_name); |
| 2032 | |
| 2033 | pool = (GstAmlV4l2BufferPool *)g_object_new(GST_TYPE_AML_V4L2_BUFFER_POOL, |
| 2034 | "name", name, NULL); |
| 2035 | g_object_ref_sink(pool); |
| 2036 | g_free(name); |
| 2037 | |
| 2038 | gst_poll_fd_init(&pool->pollfd); |
| 2039 | pool->pollfd.fd = fd; |
| 2040 | gst_poll_add_fd(pool->poll, &pool->pollfd); |
| 2041 | if (V4L2_TYPE_IS_OUTPUT(obj->type)) |
sheng.liu | 8d18ed2 | 2022-05-26 17:28:15 +0800 | [diff] [blame] | 2042 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2043 | gst_poll_fd_ctl_write(pool->poll, &pool->pollfd, TRUE); |
sheng.liu | 8d18ed2 | 2022-05-26 17:28:15 +0800 | [diff] [blame] | 2044 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2045 | else |
sheng.liu | 8d18ed2 | 2022-05-26 17:28:15 +0800 | [diff] [blame] | 2046 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2047 | gst_poll_fd_ctl_read(pool->poll, &pool->pollfd, TRUE); |
sheng.liu | 8d18ed2 | 2022-05-26 17:28:15 +0800 | [diff] [blame] | 2048 | gst_poll_fd_ctl_pri (pool->poll, &pool->pollfd, TRUE); |
| 2049 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2050 | |
| 2051 | pool->video_fd = fd; |
| 2052 | pool->obj = obj; |
| 2053 | pool->can_poll_device = TRUE; |
| 2054 | |
| 2055 | pool->vallocator = gst_aml_v4l2_allocator_new(GST_OBJECT(pool), obj); |
| 2056 | if (pool->vallocator == NULL) |
| 2057 | goto allocator_failed; |
| 2058 | |
| 2059 | gst_object_ref(obj->element); |
| 2060 | |
| 2061 | config = gst_buffer_pool_get_config(GST_BUFFER_POOL_CAST(pool)); |
| 2062 | gst_buffer_pool_config_set_params(config, caps, obj->info.size, 0, 0); |
| 2063 | /* This will simply set a default config, but will not configure the pool |
| 2064 | * because min and max are not valid */ |
hanghang.luo | 3128f10 | 2022-08-18 10:36:19 +0800 | [diff] [blame] | 2065 | (void)gst_buffer_pool_set_config(GST_BUFFER_POOL_CAST(pool), config); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2066 | |
| 2067 | return GST_BUFFER_POOL(pool); |
| 2068 | |
| 2069 | /* ERRORS */ |
| 2070 | dup_failed: |
| 2071 | { |
| 2072 | GST_ERROR("failed to dup fd %d (%s)", errno, g_strerror(errno)); |
| 2073 | return NULL; |
| 2074 | } |
| 2075 | allocator_failed: |
| 2076 | { |
| 2077 | GST_ERROR_OBJECT(pool, "Failed to create V4L2 allocator"); |
| 2078 | gst_object_unref(pool); |
| 2079 | return NULL; |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | static GstFlowReturn |
| 2084 | gst_aml_v4l2_do_read(GstAmlV4l2BufferPool *pool, GstBuffer *buf) |
| 2085 | { |
| 2086 | GstFlowReturn res; |
| 2087 | GstAmlV4l2Object *obj = pool->obj; |
| 2088 | gint amount; |
| 2089 | GstMapInfo map; |
| 2090 | gint toread; |
| 2091 | |
| 2092 | toread = obj->info.size; |
| 2093 | |
| 2094 | GST_LOG_OBJECT(pool, "reading %d bytes into buffer %p", toread, buf); |
| 2095 | |
| 2096 | gst_buffer_map(buf, &map, GST_MAP_WRITE); |
| 2097 | |
| 2098 | do |
| 2099 | { |
| 2100 | if ((res = gst_aml_v4l2_buffer_pool_poll(pool, TRUE)) != GST_FLOW_OK) |
| 2101 | goto poll_error; |
| 2102 | |
| 2103 | amount = obj->read(obj->video_fd, map.data, toread); |
| 2104 | |
| 2105 | if (amount == toread) |
| 2106 | { |
| 2107 | break; |
| 2108 | } |
| 2109 | else if (amount == -1) |
| 2110 | { |
| 2111 | if (errno == EAGAIN || errno == EINTR) |
| 2112 | { |
| 2113 | continue; |
| 2114 | } |
| 2115 | else |
| 2116 | goto read_error; |
| 2117 | } |
| 2118 | else |
| 2119 | { |
| 2120 | /* short reads can happen if a signal interrupts the read */ |
| 2121 | continue; |
| 2122 | } |
| 2123 | } while (TRUE); |
| 2124 | |
| 2125 | GST_LOG_OBJECT(pool, "read %d bytes", amount); |
| 2126 | gst_buffer_unmap(buf, &map); |
| 2127 | gst_buffer_resize(buf, 0, amount); |
| 2128 | |
| 2129 | return GST_FLOW_OK; |
| 2130 | |
| 2131 | /* ERRORS */ |
| 2132 | poll_error: |
| 2133 | { |
| 2134 | GST_DEBUG("poll error %s", gst_flow_get_name(res)); |
| 2135 | goto cleanup; |
| 2136 | } |
| 2137 | read_error: |
| 2138 | { |
| 2139 | GST_ELEMENT_ERROR(obj->element, RESOURCE, READ, |
| 2140 | (_("Error reading %d bytes from device '%s'."), |
| 2141 | toread, obj->videodev), |
| 2142 | GST_ERROR_SYSTEM); |
| 2143 | res = GST_FLOW_ERROR; |
| 2144 | goto cleanup; |
| 2145 | } |
| 2146 | cleanup: |
| 2147 | { |
| 2148 | gst_buffer_unmap(buf, &map); |
| 2149 | gst_buffer_resize(buf, 0, 0); |
| 2150 | return res; |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | /** |
| 2155 | * gst_aml_v4l2_buffer_pool_process: |
| 2156 | * @bpool: a #GstBufferPool |
| 2157 | * @buf: a #GstBuffer, maybe be replaced |
| 2158 | * |
| 2159 | * Process @buf in @bpool. For capture devices, this functions fills @buf with |
| 2160 | * data from the device. For output devices, this functions send the contents of |
| 2161 | * @buf to the device for playback. |
| 2162 | * |
| 2163 | * Returns: %GST_FLOW_OK on success. |
| 2164 | */ |
| 2165 | GstFlowReturn |
| 2166 | gst_aml_v4l2_buffer_pool_process(GstAmlV4l2BufferPool *pool, GstBuffer **buf) |
| 2167 | { |
| 2168 | GstFlowReturn ret = GST_FLOW_OK; |
| 2169 | GstBufferPool *bpool = GST_BUFFER_POOL_CAST(pool); |
| 2170 | GstAmlV4l2Object *obj = pool->obj; |
| 2171 | |
| 2172 | GST_DEBUG_OBJECT(pool, "process buffer %p, buf_pool:%p, v4l2 output pool:%p", buf, (*buf)->pool, bpool); |
| 2173 | |
| 2174 | if (GST_BUFFER_POOL_IS_FLUSHING(pool)) |
| 2175 | return GST_FLOW_FLUSHING; |
| 2176 | |
| 2177 | switch (obj->type) |
| 2178 | { |
| 2179 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 2180 | case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: |
| 2181 | /* capture */ |
| 2182 | switch (obj->mode) |
| 2183 | { |
| 2184 | case GST_V4L2_IO_RW: |
| 2185 | /* capture into the buffer */ |
| 2186 | ret = gst_aml_v4l2_do_read(pool, *buf); |
| 2187 | break; |
| 2188 | |
| 2189 | case GST_V4L2_IO_MMAP: |
| 2190 | case GST_V4L2_IO_DMABUF: |
| 2191 | { |
| 2192 | GstBuffer *tmp; |
| 2193 | |
| 2194 | if ((*buf)->pool == bpool) |
| 2195 | { |
| 2196 | guint num_queued; |
| 2197 | gsize size = gst_buffer_get_size(*buf); |
| 2198 | |
| 2199 | /* Legacy M2M devices return empty buffer when drained */ |
| 2200 | if (size == 0 && GST_AML_V4L2_IS_M2M(obj->device_caps)) |
| 2201 | goto eos; |
| 2202 | |
| 2203 | if (GST_VIDEO_INFO_FORMAT(&pool->caps_info) != |
| 2204 | GST_VIDEO_FORMAT_ENCODED && |
| 2205 | size < pool->size) |
| 2206 | goto buffer_truncated; |
| 2207 | |
| 2208 | num_queued = g_atomic_int_get(&pool->num_queued); |
| 2209 | GST_TRACE_OBJECT(pool, "Only %i buffer left in the capture queue.", |
| 2210 | num_queued); |
| 2211 | |
| 2212 | /* If we have no more buffer, and can allocate it time to do so */ |
| 2213 | if (num_queued == 0) |
| 2214 | { |
| 2215 | if (GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(pool->vallocator, MMAP)) |
| 2216 | { |
| 2217 | ret = gst_aml_v4l2_buffer_pool_resurrect_buffer(pool); |
| 2218 | if (ret == GST_FLOW_OK) |
| 2219 | goto done; |
| 2220 | } |
| 2221 | } |
| 2222 | |
| 2223 | /* start copying buffers when we are running low on buffers */ |
| 2224 | if (num_queued < pool->copy_threshold) |
| 2225 | { |
| 2226 | GstBuffer *copy; |
| 2227 | |
| 2228 | if (GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(pool->vallocator, MMAP)) |
| 2229 | { |
| 2230 | ret = gst_aml_v4l2_buffer_pool_resurrect_buffer(pool); |
| 2231 | if (ret == GST_FLOW_OK) |
| 2232 | goto done; |
| 2233 | } |
| 2234 | |
| 2235 | /* copy the buffer */ |
| 2236 | copy = gst_buffer_copy_region(*buf, |
| 2237 | GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1); |
| 2238 | GST_LOG_OBJECT(pool, "copy buffer %p->%p", *buf, copy); |
| 2239 | |
| 2240 | /* and requeue so that we can continue capturing */ |
| 2241 | gst_buffer_unref(*buf); |
| 2242 | *buf = copy; |
| 2243 | } |
| 2244 | |
| 2245 | ret = GST_FLOW_OK; |
| 2246 | /* nothing, data was inside the buffer when we did _acquire() */ |
| 2247 | goto done; |
| 2248 | } |
| 2249 | |
| 2250 | /* buffer not from our pool, grab a frame and copy it into the target */ |
| 2251 | if ((ret = gst_aml_v4l2_buffer_pool_dequeue(pool, &tmp, TRUE)) != GST_FLOW_OK) |
| 2252 | goto done; |
| 2253 | |
| 2254 | if (obj->dumpframefile) |
| 2255 | { |
| 2256 | FILE *pFile = fopen(obj->dumpframefile, "ab"); |
| 2257 | if (pFile) |
| 2258 | { |
| 2259 | int n = gst_buffer_n_memory(tmp); |
| 2260 | int i; |
| 2261 | GstMapInfo map_info; |
| 2262 | GstMemory *mem; |
| 2263 | |
| 2264 | for (i = 0; i < n; ++i) |
| 2265 | { |
| 2266 | mem = gst_buffer_peek_memory(tmp, i); |
| 2267 | if (gst_memory_map(mem, &map_info, GST_MAP_READ)) |
| 2268 | { |
| 2269 | fwrite(map_info.data, map_info.size, 1, pFile); |
| 2270 | gst_memory_unmap(mem, &map_info); |
| 2271 | } |
| 2272 | } |
| 2273 | fclose(pFile); |
| 2274 | } |
| 2275 | } |
| 2276 | /* An empty buffer on capture indicates the end of stream */ |
| 2277 | if (gst_buffer_get_size(tmp) == 0) |
| 2278 | { |
| 2279 | gst_aml_v4l2_buffer_pool_release_buffer(bpool, tmp); |
| 2280 | |
| 2281 | /* Legacy M2M devices return empty buffer when drained */ |
| 2282 | if (GST_AML_V4L2_IS_M2M(obj->device_caps)) |
| 2283 | goto eos; |
| 2284 | } |
| 2285 | |
| 2286 | ret = gst_aml_v4l2_buffer_pool_copy_buffer(pool, *buf, tmp); |
| 2287 | |
| 2288 | /* an queue the buffer again after the copy */ |
| 2289 | gst_aml_v4l2_buffer_pool_release_buffer(bpool, tmp); |
| 2290 | |
| 2291 | if (ret != GST_FLOW_OK) |
| 2292 | goto copy_failed; |
| 2293 | break; |
| 2294 | } |
| 2295 | |
| 2296 | case GST_V4L2_IO_USERPTR: |
| 2297 | { |
| 2298 | struct UserPtrData *data; |
| 2299 | GstBuffer *tmp; |
| 2300 | |
| 2301 | /* Replace our buffer with downstream allocated buffer */ |
| 2302 | data = gst_mini_object_steal_qdata(GST_MINI_OBJECT(*buf), |
| 2303 | GST_AML_V4L2_IMPORT_QUARK); |
| 2304 | tmp = gst_buffer_ref(data->buffer); |
| 2305 | _unmap_userptr_frame(data); |
| 2306 | |
| 2307 | /* Now tmp is writable, copy the flags and timestamp */ |
| 2308 | gst_buffer_copy_into(tmp, *buf, |
| 2309 | GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1); |
| 2310 | |
| 2311 | gst_buffer_replace(buf, tmp); |
| 2312 | gst_buffer_unref(tmp); |
| 2313 | break; |
| 2314 | } |
| 2315 | |
| 2316 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 2317 | { |
| 2318 | GstBuffer *tmp; |
| 2319 | |
| 2320 | /* Replace our buffer with downstream allocated buffer */ |
| 2321 | // tmp = gst_mini_object_steal_qdata(GST_MINI_OBJECT(*buf), |
| 2322 | // GST_AML_V4L2_IMPORT_QUARK); |
| 2323 | tmp = gst_mini_object_get_qdata(GST_MINI_OBJECT(*buf), GST_AML_V4L2_IMPORT_QUARK); |
| 2324 | GST_DEBUG("got v4l2 capture buf:%p, with qdata drm buf:%p", *buf, tmp); |
| 2325 | |
| 2326 | gst_buffer_copy_into(tmp, *buf, |
| 2327 | GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1); |
| 2328 | |
| 2329 | gst_buffer_replace(buf, tmp); |
| 2330 | gst_buffer_unref(tmp); |
| 2331 | break; |
| 2332 | } |
| 2333 | |
| 2334 | default: |
| 2335 | g_assert_not_reached(); |
| 2336 | break; |
| 2337 | } |
| 2338 | break; |
| 2339 | |
| 2340 | case V4L2_BUF_TYPE_VIDEO_OUTPUT: |
| 2341 | case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: |
| 2342 | /* playback */ |
| 2343 | switch (obj->mode) |
| 2344 | { |
| 2345 | case GST_V4L2_IO_RW: |
| 2346 | /* FIXME, do write() */ |
| 2347 | GST_WARNING_OBJECT(pool, "implement write()"); |
| 2348 | break; |
| 2349 | |
| 2350 | case GST_V4L2_IO_USERPTR: |
| 2351 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 2352 | case GST_V4L2_IO_DMABUF: |
| 2353 | case GST_V4L2_IO_MMAP: |
| 2354 | { |
| 2355 | GstBuffer *to_queue = NULL; |
| 2356 | GstBuffer *buffer; |
| 2357 | GstAmlV4l2MemoryGroup *group; |
| 2358 | gint index; |
| 2359 | |
| 2360 | if ((*buf)->pool != bpool) |
| 2361 | goto copying; |
| 2362 | |
| 2363 | if (!gst_aml_v4l2_is_buffer_valid(*buf, &group)) |
| 2364 | goto copying; |
| 2365 | |
| 2366 | index = group->buffer.index; |
| 2367 | |
| 2368 | GST_LOG_OBJECT(pool, "processing buffer %i from our pool", index); |
| 2369 | |
| 2370 | if (pool->buffers[index] != NULL) |
| 2371 | { |
| 2372 | GST_LOG_OBJECT(pool, "buffer %i already queued, copying", index); |
| 2373 | goto copying; |
| 2374 | } |
| 2375 | |
| 2376 | /* we can queue directly */ |
| 2377 | to_queue = gst_buffer_ref(*buf); |
| 2378 | |
| 2379 | copying: |
| 2380 | if (to_queue == NULL) |
| 2381 | { |
| 2382 | GstBufferPoolAcquireParams params = {0}; |
| 2383 | |
| 2384 | GST_LOG_OBJECT(pool, "alloc buffer from our pool"); |
| 2385 | |
| 2386 | /* this can return EOS if all buffers are outstanding which would |
| 2387 | * be strange because we would expect the upstream element to have |
| 2388 | * allocated them and returned to us.. */ |
| 2389 | params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT; |
| 2390 | ret = gst_buffer_pool_acquire_buffer(bpool, &to_queue, ¶ms); |
| 2391 | if (ret != GST_FLOW_OK) |
| 2392 | goto acquire_failed; |
| 2393 | |
| 2394 | ret = gst_aml_v4l2_buffer_pool_prepare_buffer(pool, to_queue, *buf); |
| 2395 | if (ret != GST_FLOW_OK) |
| 2396 | { |
| 2397 | gst_buffer_unref(to_queue); |
| 2398 | goto prepare_failed; |
| 2399 | } |
| 2400 | |
| 2401 | /* retreive the group */ |
| 2402 | gst_aml_v4l2_is_buffer_valid(to_queue, &group); |
| 2403 | } |
| 2404 | |
| 2405 | if ((ret = gst_aml_v4l2_buffer_pool_qbuf(pool, to_queue, group)) != GST_FLOW_OK) |
| 2406 | goto queue_failed; |
| 2407 | |
| 2408 | /* if we are not streaming yet (this is the first buffer, start |
| 2409 | * streaming now */ |
| 2410 | if (!gst_aml_v4l2_buffer_pool_streamon(pool)) |
| 2411 | { |
| 2412 | /* don't check return value because qbuf would have failed */ |
| 2413 | gst_aml_v4l2_is_buffer_valid(to_queue, &group); |
| 2414 | |
| 2415 | /* qbuf has stored to_queue buffer but we are not in |
| 2416 | * streaming state, so the flush logic won't be performed. |
| 2417 | * To avoid leaks, flush the allocator and restore the queued |
| 2418 | * buffer as non-queued */ |
| 2419 | gst_aml_v4l2_allocator_flush(pool->vallocator); |
| 2420 | |
| 2421 | pool->buffers[group->buffer.index] = NULL; |
| 2422 | |
| 2423 | gst_mini_object_set_qdata(GST_MINI_OBJECT(to_queue), |
| 2424 | GST_AML_V4L2_IMPORT_QUARK, NULL, NULL); |
| 2425 | gst_buffer_unref(to_queue); |
| 2426 | g_atomic_int_add(&pool->num_queued, -1); |
| 2427 | goto start_failed; |
| 2428 | } |
| 2429 | |
| 2430 | /* Remove our ref, we will still hold this buffer in acquire as needed, |
| 2431 | * otherwise the pool will think it is outstanding and will refuse to stop. */ |
| 2432 | gst_buffer_unref(to_queue); |
| 2433 | |
| 2434 | /* release as many buffer as possible */ |
| 2435 | while (gst_aml_v4l2_buffer_pool_dequeue(pool, &buffer, FALSE) == |
| 2436 | GST_FLOW_OK) |
| 2437 | { |
| 2438 | if (buffer->pool == NULL) |
| 2439 | gst_aml_v4l2_buffer_pool_release_buffer(bpool, buffer); |
| 2440 | } |
| 2441 | |
| 2442 | if (g_atomic_int_get(&pool->num_queued) >= pool->min_latency) |
| 2443 | { |
| 2444 | /* all buffers are queued, try to dequeue one and release it back |
| 2445 | * into the pool so that _acquire can get to it again. */ |
| 2446 | ret = gst_aml_v4l2_buffer_pool_dequeue(pool, &buffer, TRUE); |
| 2447 | if (ret == GST_FLOW_OK && buffer->pool == NULL) |
| 2448 | /* release the rendered buffer back into the pool. This wakes up any |
| 2449 | * thread waiting for a buffer in _acquire(). */ |
| 2450 | gst_aml_v4l2_buffer_pool_release_buffer(bpool, buffer); |
| 2451 | } |
| 2452 | break; |
| 2453 | } |
| 2454 | default: |
| 2455 | g_assert_not_reached(); |
| 2456 | break; |
| 2457 | } |
| 2458 | break; |
| 2459 | default: |
| 2460 | g_assert_not_reached(); |
| 2461 | break; |
| 2462 | } |
| 2463 | done: |
| 2464 | return ret; |
| 2465 | |
| 2466 | /* ERRORS */ |
| 2467 | copy_failed: |
| 2468 | { |
| 2469 | GST_ERROR_OBJECT(pool, "failed to copy buffer"); |
| 2470 | return ret; |
| 2471 | } |
| 2472 | buffer_truncated: |
| 2473 | { |
| 2474 | GST_WARNING_OBJECT(pool, |
| 2475 | "Dropping truncated buffer, this is likely a driver bug."); |
| 2476 | gst_buffer_unref(*buf); |
| 2477 | *buf = NULL; |
| 2478 | return GST_AML_V4L2_FLOW_CORRUPTED_BUFFER; |
| 2479 | } |
| 2480 | eos: |
| 2481 | { |
| 2482 | GST_DEBUG_OBJECT(pool, "end of stream reached"); |
| 2483 | gst_buffer_unref(*buf); |
| 2484 | *buf = NULL; |
| 2485 | return GST_AML_V4L2_FLOW_LAST_BUFFER; |
| 2486 | } |
| 2487 | acquire_failed: |
| 2488 | { |
| 2489 | if (ret == GST_FLOW_FLUSHING) |
| 2490 | GST_DEBUG_OBJECT(pool, "flushing"); |
| 2491 | else |
| 2492 | GST_WARNING_OBJECT(pool, "failed to acquire a buffer: %s", |
| 2493 | gst_flow_get_name(ret)); |
| 2494 | return ret; |
| 2495 | } |
| 2496 | prepare_failed: |
| 2497 | { |
| 2498 | GST_ERROR_OBJECT(pool, "failed to prepare data"); |
| 2499 | return ret; |
| 2500 | } |
| 2501 | queue_failed: |
| 2502 | { |
| 2503 | GST_ERROR_OBJECT(pool, "failed to queue buffer"); |
| 2504 | return ret; |
| 2505 | } |
| 2506 | start_failed: |
| 2507 | { |
| 2508 | GST_ERROR_OBJECT(pool, "failed to start streaming"); |
| 2509 | return GST_FLOW_ERROR; |
| 2510 | } |
| 2511 | } |
| 2512 | |
| 2513 | void gst_aml_v4l2_buffer_pool_set_other_pool(GstAmlV4l2BufferPool *pool, |
| 2514 | GstBufferPool *other_pool) |
| 2515 | { |
| 2516 | g_return_if_fail(!gst_buffer_pool_is_active(GST_BUFFER_POOL(pool))); |
| 2517 | |
| 2518 | if (pool->other_pool) |
| 2519 | gst_object_unref(pool->other_pool); |
| 2520 | pool->other_pool = gst_object_ref(other_pool); |
| 2521 | } |
| 2522 | |
| 2523 | void gst_aml_v4l2_buffer_pool_copy_at_threshold(GstAmlV4l2BufferPool *pool, gboolean copy) |
| 2524 | { |
| 2525 | GST_OBJECT_LOCK(pool); |
| 2526 | pool->enable_copy_threshold = copy; |
| 2527 | GST_OBJECT_UNLOCK(pool); |
| 2528 | } |
| 2529 | |
| 2530 | gboolean |
| 2531 | gst_aml_v4l2_buffer_pool_flush(GstBufferPool *bpool) |
| 2532 | { |
| 2533 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(bpool); |
| 2534 | gboolean ret = TRUE; |
| 2535 | |
| 2536 | gst_aml_v4l2_buffer_pool_streamoff(pool); |
| 2537 | |
| 2538 | if (!V4L2_TYPE_IS_OUTPUT(pool->obj->type)) |
| 2539 | ret = gst_aml_v4l2_buffer_pool_streamon(pool); |
| 2540 | |
| 2541 | return ret; |
| 2542 | } |
| 2543 | |
| 2544 | void gst_aml_v4l2_buffer_pool_dump_stat(GstAmlV4l2BufferPool *pool, const gchar *file_name, gint try_num) |
| 2545 | { |
| 2546 | const gchar *dump_dir = NULL; |
| 2547 | gchar *full_file_name = NULL; |
| 2548 | FILE *out = NULL; |
| 2549 | |
| 2550 | dump_dir = g_getenv("GST_DEBUG_DUMP_AMLV4L2DEC_STAT_DIR"); |
| 2551 | if (G_LIKELY(dump_dir == NULL)) |
| 2552 | return; |
| 2553 | |
| 2554 | if (!file_name) |
| 2555 | { |
| 2556 | file_name = "unnamed"; |
| 2557 | } |
| 2558 | |
| 2559 | full_file_name = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s.stat", dump_dir, file_name); |
| 2560 | |
| 2561 | if ((out = fopen(full_file_name, "w"))) |
| 2562 | { |
| 2563 | GstStructure *config = NULL; |
| 2564 | config = gst_buffer_pool_get_config((GstBufferPool *)pool); |
| 2565 | if (config) |
| 2566 | { |
| 2567 | GstCaps *caps; |
| 2568 | guint size, min_buffers, max_buffers; |
| 2569 | if (gst_buffer_pool_config_get_params(config, &caps, &size, &min_buffers, &max_buffers)) |
| 2570 | { |
| 2571 | gchar *stat_info; |
| 2572 | |
| 2573 | /* set local pool info*/ |
| 2574 | gint already_queued = 0; |
| 2575 | gint ready_to_queue_num = 0; |
| 2576 | for (gint i = 0; i < VIDEO_MAX_FRAME; i++) |
| 2577 | { |
| 2578 | if (pool->buffers[i]) |
| 2579 | { |
| 2580 | already_queued++; |
| 2581 | } |
| 2582 | if (pool->read_to_free_bufs[i]) |
| 2583 | { |
| 2584 | ready_to_queue_num++; |
| 2585 | } |
| 2586 | } |
| 2587 | |
| 2588 | stat_info = g_strdup_printf("local pool | size:%d, min_bufs:%d, max_bufs:%d | queued:%d, allocated:%d | already_queued:%d, ready_to_queue:%d | try_num:%d\n", |
| 2589 | size, min_buffers, max_buffers, |
| 2590 | pool->num_queued, pool->num_allocated, |
| 2591 | already_queued, ready_to_queue_num, try_num); |
| 2592 | fputs(stat_info, out); |
| 2593 | g_free(stat_info); |
| 2594 | |
| 2595 | /* set other pool info*/ |
| 2596 | if (pool->other_pool) |
| 2597 | { |
| 2598 | GstStructure *other_config = NULL; |
| 2599 | other_config = gst_buffer_pool_get_config((GstBufferPool *)pool); |
| 2600 | if (other_config) |
| 2601 | { |
| 2602 | GstCaps *other_caps; |
| 2603 | guint other_size, other_min_buffers, other_max_buffers; |
| 2604 | if (gst_buffer_pool_config_get_params(config, &other_caps, &other_size, &other_min_buffers, &other_max_buffers)) |
| 2605 | { |
| 2606 | stat_info = g_strdup_printf("other pool | size:%d, min_bufs:%d, max_bufs:%d\n", |
| 2607 | other_size, other_min_buffers, other_max_buffers); |
| 2608 | |
| 2609 | // TODO:GstBufferPool中没有获取outstanding的接口,所以这里没有统计otherpool中在用buffer的状态,后续需要修改gstreamer-1.0中code新增接口 |
| 2610 | fputs(stat_info, out); |
| 2611 | g_free(stat_info); |
| 2612 | } |
| 2613 | } |
| 2614 | } |
| 2615 | GST_INFO("wrote amlv4l2 bufferpool stat to : '%s' succ", full_file_name); |
| 2616 | } |
| 2617 | } |
| 2618 | else |
| 2619 | { |
| 2620 | GST_WARNING("Failed to get config for pool:%p", pool); |
| 2621 | } |
| 2622 | fclose(out); |
| 2623 | } |
| 2624 | else |
| 2625 | { |
| 2626 | GST_WARNING("Failed to open file '%s' for writing: %s", full_file_name, g_strerror(errno)); |
| 2627 | } |
| 2628 | g_free(full_file_name); |
| 2629 | } |