@@ -357,6 +357,8 @@ gst_v4l2_allocator_release (GstV4l2Allocator * allocator, GstV4l2Memory * mem)
357
357
358
358
switch (allocator -> memory ) {
359
359
case V4L2_MEMORY_DMABUF :
360
+ if (V4L2_TYPE_IS_CAPTURE (allocator -> obj -> type ) && (allocator -> obj -> mode == GST_V4L2_IO_DMABUF_IMPORT ))
361
+ break ;
360
362
mem -> dmafd = -1 ;
361
363
break ;
362
364
case V4L2_MEMORY_USERPTR :
@@ -395,8 +397,10 @@ gst_v4l2_allocator_free (GstAllocator * gallocator, GstMemory * gmem)
395
397
obj -> munmap (mem -> data , group -> planes [mem -> plane ].length );
396
398
}
397
399
398
- if (allocator -> memory == V4L2_MEMORY_MMAP && mem -> dmafd >= 0 )
400
+ if (allocator -> memory == V4L2_MEMORY_MMAP && mem -> dmafd >= 0 ){
399
401
close (mem -> dmafd );
402
+ GST_LOG_OBJECT (allocator , "close fd: %d" , mem -> dmafd );
403
+ }
400
404
}
401
405
402
406
g_slice_free (GstV4l2Memory , mem );
@@ -1021,6 +1025,91 @@ gst_v4l2_allocator_alloc_dmabufin (GstV4l2Allocator * allocator)
1021
1025
return group ;
1022
1026
}
1023
1027
1028
+ GstV4l2MemoryGroup *
1029
+ gst_v4l2_allocator_alloc_dmabufin_capture (GstV4l2Allocator * allocator ,
1030
+ GstAllocator * dmabuf_allocator , GstBuffer * downstream_buffer )
1031
+ {
1032
+ GstV4l2Object * obj = allocator -> obj ;
1033
+ GstV4l2MemoryGroup * group ;
1034
+ gint i ;
1035
+
1036
+ g_return_val_if_fail (allocator -> memory == V4L2_MEMORY_DMABUF , NULL );
1037
+
1038
+ group = gst_v4l2_allocator_alloc (allocator );
1039
+
1040
+ if (group == NULL )
1041
+ return NULL ;
1042
+
1043
+ for (i = 0 ; i < group -> n_mem ; i ++ ) {
1044
+ GstV4l2Memory * mem ;
1045
+ GstMemory * dma_mem ;
1046
+ gsize size , offset , maxsize ;
1047
+
1048
+
1049
+ if (group -> mem [i ] == NULL ) {
1050
+ dma_mem = gst_buffer_peek_memory (downstream_buffer , i );
1051
+ int downstream_fd = gst_dmabuf_memory_get_fd (dma_mem );
1052
+
1053
+ GST_LOG_OBJECT (allocator , "import DMABUF fd %d into fd %i plane %d" ,
1054
+ downstream_fd , downstream_fd , i );
1055
+
1056
+ group -> planes [i ].m .fd = downstream_fd ;
1057
+ group -> planes [i ].length = dma_mem -> size ;
1058
+ group -> planes [i ].data_offset = dma_mem -> offset ;
1059
+
1060
+ group -> mem [i ] = (GstMemory * ) _v4l2mem_new (0 , GST_ALLOCATOR (allocator ),
1061
+ NULL , group -> planes [i ].length , 0 , group -> planes [i ].data_offset ,
1062
+ group -> planes [i ].length - group -> planes [i ].data_offset , i , NULL ,
1063
+ downstream_fd , group );
1064
+ } else {
1065
+ /* Take back the allocator reference */
1066
+ gst_object_ref (allocator );
1067
+ }
1068
+
1069
+ group -> mems_allocated ++ ;
1070
+
1071
+ g_assert (gst_is_v4l2_memory (group -> mem [i ]));
1072
+ mem = (GstV4l2Memory * ) group -> mem [i ];
1073
+
1074
+ dma_mem = gst_fd_allocator_alloc (dmabuf_allocator , mem -> dmafd ,
1075
+ group -> planes [i ].length , GST_FD_MEMORY_FLAG_DONT_CLOSE );
1076
+ gst_memory_resize (dma_mem , group -> planes [i ].data_offset ,
1077
+ group -> planes [i ].length - group -> planes [i ].data_offset );
1078
+
1079
+ gst_mini_object_set_qdata (GST_MINI_OBJECT (dma_mem ),
1080
+ GST_V4L2_MEMORY_QUARK , mem , (GDestroyNotify ) gst_memory_unref );
1081
+
1082
+ group -> mem [i ] = dma_mem ;
1083
+ }
1084
+
1085
+ if (!V4L2_TYPE_IS_MULTIPLANAR (obj -> type )) {
1086
+ group -> buffer .bytesused = group -> planes [0 ].bytesused ;
1087
+ group -> buffer .length = group -> planes [0 ].length ;
1088
+ group -> buffer .m .fd = group -> planes [0 ].m .fd ;
1089
+
1090
+ /* FIXME Check if data_offset > 0 and fail for non-multi-planar */
1091
+ g_assert (group -> planes [0 ].data_offset == 0 );
1092
+ } else {
1093
+ group -> buffer .length = group -> n_mem ;
1094
+ }
1095
+
1096
+ gst_v4l2_allocator_reset_size (allocator , group );
1097
+
1098
+ return group ;
1099
+
1100
+ expbuf_failed :
1101
+ {
1102
+ GST_ERROR_OBJECT (allocator , "Failed to export DMABUF: %s" ,
1103
+ g_strerror (errno ));
1104
+ goto cleanup ;
1105
+ }
1106
+ cleanup :
1107
+ {
1108
+ _cleanup_failed_alloc (allocator , group );
1109
+ return NULL ;
1110
+ }
1111
+ }
1112
+
1024
1113
static void
1025
1114
gst_v4l2_allocator_clear_userptr (GstV4l2Allocator * allocator ,
1026
1115
GstV4l2MemoryGroup * group )
@@ -1453,7 +1542,12 @@ gst_v4l2_allocator_reset_group (GstV4l2Allocator * allocator,
1453
1542
gst_v4l2_allocator_clear_userptr (allocator , group );
1454
1543
break ;
1455
1544
case V4L2_MEMORY_DMABUF :
1456
- gst_v4l2_allocator_clear_dmabufin (allocator , group );
1545
+ if (V4L2_TYPE_IS_CAPTURE (allocator -> obj -> type ) && (allocator -> obj -> mode == GST_V4L2_IO_DMABUF_IMPORT )) {
1546
+ break ;
1547
+ }
1548
+ else {
1549
+ gst_v4l2_allocator_clear_dmabufin (allocator , group );
1550
+ }
1457
1551
break ;
1458
1552
case V4L2_MEMORY_MMAP :
1459
1553
break ;
0 commit comments