1 /* GStreamer 2 * Copyright (C) <2018-2019> Seungha Yang <seungha.yang@navercorp.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 St, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 */ 19 20 #ifndef __GST_CUDA_MEMORY_H__ 21 #define __GST_CUDA_MEMORY_H__ 22 23 #include <gst/gst.h> 24 #include <gst/gstallocator.h> 25 #include <gst/video/video.h> 26 #include "gstcudaloader.h" 27 #include "gstcudacontext.h" 28 29 G_BEGIN_DECLS 30 31 #define GST_TYPE_CUDA_ALLOCATOR (gst_cuda_allocator_get_type()) 32 #define GST_CUDA_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_CUDA_ALLOCATOR,GstCudaAllocator)) 33 #define GST_CUDA_ALLOCATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_CUDA_ALLOCATOR,GstCudaAllocatorClass)) 34 #define GST_CUDA_ALLOCATOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_CUDA_ALLOCATOR,GstCudaAllocatorClass)) 35 #define GST_IS_CUDA_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_CUDA_ALLOCATOR)) 36 #define GST_IS_CUDA_ALLOCATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_CUDA_ALLOCATOR)) 37 #define GST_CUDA_ALLOCATOR_CAST(obj) ((GstCudaAllocator *)(obj)) 38 #define GST_CUDA_MEMORY_CAST(mem) ((GstCudaMemory *) (mem)) 39 40 typedef struct _GstCudaAllocationParams GstCudaAllocationParams; 41 typedef struct _GstCudaAllocator GstCudaAllocator; 42 typedef struct _GstCudaAllocatorClass GstCudaAllocatorClass; 43 typedef struct _GstCudaMemory GstCudaMemory; 44 45 /** 46 * GST_MAP_CUDA: 47 * 48 * Flag indicating that we should map the CUDA device memory 49 * instead of to system memory. 50 * 51 * Combining #GST_MAP_CUDA with #GST_MAP_WRITE has the same semantics as though 52 * you are writing to CUDA device/host memory. 53 * Conversely, combining #GST_MAP_CUDA with 54 * #GST_MAP_READ has the same semantics as though you are reading from 55 * CUDA device/host memory 56 */ 57 #define GST_MAP_CUDA (GST_MAP_FLAG_LAST << 1) 58 59 #define GST_CUDA_MEMORY_TYPE_NAME "gst.cuda.memory" 60 61 /** 62 * GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY: 63 * 64 * Name of the caps feature for indicating the use of #GstCudaMemory 65 */ 66 #define GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY "memory:CUDAMemory" 67 68 struct _GstCudaAllocationParams 69 { 70 GstAllocationParams parent; 71 72 GstVideoInfo info; 73 }; 74 75 struct _GstCudaAllocator 76 { 77 GstAllocator parent; 78 GstCudaContext *context; 79 }; 80 81 struct _GstCudaAllocatorClass 82 { 83 GstAllocatorClass parent_class; 84 }; 85 86 GType gst_cuda_allocator_get_type (void); 87 88 GstAllocator * gst_cuda_allocator_new (GstCudaContext * context); 89 90 GstMemory * gst_cuda_allocator_alloc (GstAllocator * allocator, 91 gsize size, 92 GstCudaAllocationParams * params); 93 94 /** 95 * GstCudaMemoryTransfer: 96 * @GST_CUDA_MEMORY_TRANSFER_NEED_DOWNLOAD: the device memory needs downloading 97 * to the staging memory 98 * @GST_CUDA_MEMORY_TRANSFER_NEED_UPLOAD: the staging memory needs uploading 99 * to the device memory 100 */ 101 typedef enum 102 { 103 GST_CUDA_MEMORY_TRANSFER_NEED_DOWNLOAD = (GST_MEMORY_FLAG_LAST << 0), 104 GST_CUDA_MEMORY_TRANSFER_NEED_UPLOAD = (GST_MEMORY_FLAG_LAST << 1) 105 } GstCudaMemoryTransfer; 106 107 struct _GstCudaMemory 108 { 109 GstMemory mem; 110 111 GstCudaContext *context; 112 CUdeviceptr data; 113 114 GstCudaAllocationParams alloc_params; 115 116 /* offset and stride of CUDA device memory */ 117 gsize offset[GST_VIDEO_MAX_PLANES]; 118 gint stride; 119 120 /* allocated CUDA Host memory */ 121 gpointer map_alloc_data; 122 123 /* aligned CUDA Host memory */ 124 guint8 *align_data; 125 126 /* pointing align_data if the memory is mapped */ 127 gpointer map_data; 128 129 gint map_count; 130 131 GMutex lock; 132 }; 133 134 gboolean gst_is_cuda_memory (GstMemory * mem); 135 136 G_END_DECLS 137 138 #endif /* __GST_CUDA_MEMORY_H__ */ 139