Commit 8a14ad73 authored by Seungha Yang's avatar Seungha Yang 🐑
Browse files

Move CUDA to gst-lib

parent b1df4f46
......@@ -2538,6 +2538,7 @@ gst-libs/gst/insertbin/Makefile
gst-libs/gst/interfaces/Makefile
gst-libs/gst/isoff/Makefile
gst-libs/gst/codecparsers/Makefile
gst-libs/gst/cuda/Makefile
gst-libs/gst/mpegts/Makefile
gst-libs/gst/sctp/Makefile
gst-libs/gst/uridownloader/Makefile
......
......@@ -6,19 +6,24 @@ if USE_OPENCV
OPENCV_DIR=opencv
endif
if USE_CUDA
CUDA_DIR=cuda
endif
SUBDIRS = uridownloader adaptivedemux interfaces basecamerabinsrc codecparsers \
insertbin mpegts audio sctp player isoff webrtc $(WAYLAND_DIR) \
$(OPENCV_DIR)
$(OPENCV_DIR) $(CUDA_DIR)
noinst_HEADERS = gst-i18n-plugin.h gettext.h glib-compat-private.h
DIST_SUBDIRS = uridownloader adaptivedemux interfaces basecamerabinsrc \
codecparsers insertbin mpegts wayland opencv audio player isoff sctp webrtc
codecparsers insertbin mpegts wayland opencv audio player isoff sctp webrtc \
cuda
adaptivedemux: uridownloader
INDEPENDENT_SUBDIRS = \
interfaces basecamerabinsrc codecparsers insertbin uridownloader \
mpegts player isoff sctp $(WAYLAND_DIR) $(OPENCV_DIR)
mpegts player isoff sctp $(WAYLAND_DIR) $(OPENCV_DIR) $(CUDA_DIR)
.PHONY: independent-subdirs $(INDEPENDENT_SUBDIRS)
......
lib_LTLIBRARIES = libgstcuda-@GST_API_VERSION@.la
libgstcuda_@GST_API_VERSION@_la_SOURCES = \
gstcudaloader.c \
gstcudautils.c \
gstcudacontext.c \
gstcudamemory.c \
gstcudabufferpool.c
noinst_HEADERS = \
gstcudaloader.h \
gstcuda_private.h
libgstcuda_@GST_API_VERSION@_la_CFLAGS = \
$(GST_PLUGINS_BAD_CFLAGS) \
$(GST_PLUGINS_BASE_CFLAGS) $(GST_BASE_CFLAGS) $(GST_CFLAGS) \
-DBUILDING_GST_CUDA \
$(GST_GL_CFLAGS) \
$(CUDA_CFLAGS)
libgstcuda_@GST_API_VERSION@_la_LIBADD = $(GST_LIBS) $(GST_BASE_LIBS) \
-lgstvideo-$(GST_API_VERSION)
libgstcuda_@GST_API_VERSION@_la_LDFLAGS = \
$(GST_LIB_LDFLAGS) $(GST_ALL_LDFLAGS) $(GST_LT_LDFLAGS) $(GST_GL_LIBS)
libgstcuda_@GST_API_VERSION@includedir = \
$(includedir)/gstreamer-@GST_API_VERSION@/gst/cuda
libgstcuda_@GST_API_VERSION@include_HEADERS = \
gstcuda.h \
gstcuda_fwd.h \
cuda-prelude.h \
gstcudautils.h \
gstcudacontext.h \
gstcudamemory.h \
gstcudabufferpool.h
/* GStreamer CUDA Library
* Copyright (C) 2019 GStreamer developers
*
* cuda-prelude.h: prelude include header for gst-cuda library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_CUDA_PRELUDE_H__
#define __GST_CUDA_PRELUDE_H__
#include <gst/gst.h>
#ifndef GST_CUDA_API
# ifdef BUILDING_GST_CUDA
# define GST_CUDA_API GST_API_EXPORT /* from config.h */
# else
# define GST_CUDA_API GST_API_IMPORT
# endif
#endif
#endif /* __GST_CUDA_PRELUDE_H__ */
/* GStreamer
* Copyright (C) <2018> Seungha Yang <seungha.yang@navercorp.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_CUDA_H__
#define __GST_CUDA_H__
#include <gst/cuda/gstcuda_fwd.h>
#include <gst/cuda/cuda-prelude.h>
#include <gst/cuda/gstcudacontext.h>
#include <gst/cuda/gstcudautils.h>
#include <gst/cuda/gstcudamemory.h>
#include <gst/cuda/gstcudabufferpool.h>
#endif /* __GST_CUDA_H__ */
/* GStreamer
* Copyright (C) <2019> Seungha Yang <seungha.yang@navercorp.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_CUDA_FWD_H__
#define __GST_CUDA_FWD_H__
#include <gst/gst.h>
G_BEGIN_DECLS
typedef struct _GstCudaContext GstCudaContext;
typedef struct _GstCudaContextClass GstCudaContextClass;
typedef struct _GstCudaAllocator GstCudaAllocator;
typedef struct _GstCudaAllocatorClass GstCudaAllocatorClass;
typedef struct _GstCudaMemory GstCudaMemory;
typedef struct _GstCudaBufferPool GstCudaBufferPool;
typedef struct _GstCudaBufferPoolClass GstCudaBufferPoolClass;
G_END_DECLS
#endif /* __GST_CUDA_FWD_H__ */
/* GStreamer
* Copyright (C) <2018> Seungha Yang <seungha.yang@navercorp.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_CUDA_PRIVATE_H__
#define __GST_CUDA_PRIVATE_H__
/* This header shouldn't be installed, Used by -bad plugins internally */
#define __GST_CUDA_PRIVATE_INSIDE_H__
#include <gst/cuda/gstcudaloader.h>
#undef __GST_CUDA_PRIVATE_INSIDE_H__
#endif /* __GST_CUDA_PRIVATE_H__ */
......@@ -23,7 +23,7 @@
#include "gstcudabufferpool.h"
#include "gstcudacontext.h"
#include "gstcudamemory.h"
#include "gstcuda_private.h"
GST_DEBUG_CATEGORY_STATIC (gst_cuda_buffer_pool_debug);
#define GST_CAT_DEFAULT gst_cuda_buffer_pool_debug
......
......@@ -23,7 +23,9 @@
#include <gst/video/gstvideometa.h>
#include <gst/video/gstvideopool.h>
#include "gstcudamemory.h"
#include <gst/cuda/gstcuda_fwd.h>
#include <gst/cuda/cuda-prelude.h>
#include <gst/cuda/gstcudamemory.h>
G_BEGIN_DECLS
......@@ -35,8 +37,6 @@ G_BEGIN_DECLS
#define GST_IS_CUDA_BUFFER_POOL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_CUDA_BUFFER_POOL))
#define GST_CUDA_BUFFER_POOL_CAST(obj) ((GstCudaBufferPool*)(obj))
typedef struct _GstCudaBufferPool GstCudaBufferPool;
typedef struct _GstCudaBufferPoolClass GstCudaBufferPoolClass;
typedef struct _GstCudaBufferPoolPrivate GstCudaBufferPoolPrivate;
/*
......@@ -57,8 +57,10 @@ struct _GstCudaBufferPoolClass
GstBufferPoolClass parent_class;
};
GST_CUDA_API
GType gst_cuda_buffer_pool_get_type (void);
GST_CUDA_API
GstBufferPool * gst_cuda_buffer_pool_new (GstCudaContext * context,
GstCudaMemoryTarget target);
......
......@@ -21,9 +21,10 @@
#include "config.h"
#endif
#include "gstcudaloader.h"
#include "gstcuda_private.h"
#include "gstcudacontext.h"
#include "gstcudautils.h"
#include "gstcudaloader.h"
GST_DEBUG_CATEGORY_STATIC (gst_cuda_context_debug);
#define GST_CAT_DEFAULT gst_cuda_context_debug
......
......@@ -21,6 +21,8 @@
#define __GST_CUDA_CONTEXT_H__
#include <gst/gst.h>
#include <gst/cuda/gstcuda_fwd.h>
#include <gst/cuda/cuda-prelude.h>
G_BEGIN_DECLS
......@@ -34,8 +36,6 @@ G_BEGIN_DECLS
#define GST_CUDA_CONTEXT_TYPE "gst.cuda.context"
typedef struct _GstCudaContext GstCudaContext;
typedef struct _GstCudaContextClass GstCudaContextClass;
typedef struct _GstCudaContextPrivate GstCudaContextPrivate;
/*
......@@ -57,14 +57,19 @@ struct _GstCudaContextClass
GstObjectClass parent_class;
};
GST_CUDA_API
GType gst_cuda_context_get_type (void);
GST_CUDA_API
GstCudaContext * gst_cuda_context_new (gint device_id);
GST_CUDA_API
gboolean gst_cuda_context_push (GstCudaContext * ctx);
GST_CUDA_API
gboolean gst_cuda_context_pop (void);
GST_CUDA_API
gpointer gst_cuda_context_get_context (GstCudaContext * ctx);
G_END_DECLS
......
......@@ -21,9 +21,11 @@
#include "config.h"
#endif
#include "gstcudaloader.h"
#include "gstcuda_private.h"
#include <gmodule.h>
G_LOCK_DEFINE_STATIC (init_lock);
#ifndef G_OS_WIN32
#define CUDA_LIBNAME "libcuda.so.1"
#else
......@@ -38,7 +40,7 @@
} G_STMT_END;
/* *INDENT-OFF* */
typedef struct _GstNvCodecCudaVTable
typedef struct _GstCudaVTable
{
GModule *module;
CUresult (*CuInit) (unsigned int Flags);
......@@ -76,28 +78,32 @@ typedef struct _GstNvCodecCudaVTable
CUresult (*CuGraphicsGLRegisterImage) (CUgraphicsResource * pCudaResource, unsigned int image, unsigned int target, unsigned int Flags);
CUresult (*CuGraphicsGLRegisterBuffer) (CUgraphicsResource * pCudaResource, unsigned int buffer, unsigned int Flags);
} GstNvCodecCudaVTable;
} GstCudaVTable;
/* *INDENT-ON* */
static GstNvCodecCudaVTable *gst_cuda_vtable = NULL;
static GstCudaVTable *gst_cuda_vtable = NULL;
gboolean
gst_cuda_load_library (void)
{
GModule *module;
const gchar *filename = CUDA_LIBNAME;
GstNvCodecCudaVTable *vtable;
GstCudaVTable *vtable;
if (gst_cuda_vtable)
G_LOCK (init_lock);
if (gst_cuda_vtable) {
G_UNLOCK (init_lock);
return TRUE;
}
module = g_module_open (filename, G_MODULE_BIND_LAZY);
if (module == NULL) {
G_UNLOCK (init_lock);
GST_ERROR ("Could not open library %s, %s", filename, g_module_error ());
return FALSE;
}
vtable = g_slice_new0 (GstNvCodecCudaVTable);
vtable = g_slice_new0 (GstCudaVTable);
/* cuda.h */
LOAD_SYMBOL (cuInit, CuInit);
......@@ -140,12 +146,14 @@ gst_cuda_load_library (void)
vtable->module = module;
gst_cuda_vtable = vtable;
G_UNLOCK (init_lock);
return TRUE;
error:
g_module_close (module);
g_slice_free (GstNvCodecCudaVTable, vtable);
g_slice_free (GstCudaVTable, vtable);
G_UNLOCK (init_lock);
return FALSE;
}
......
......@@ -21,47 +21,105 @@
#define __GST_CUDA_LOADER_H__
#include <gst/gst.h>
#include <gst/cuda/cuda-prelude.h>
#include <cuda.h>
#ifndef __GST_CUDA_PRIVATE_INSIDE_H__
#error "Only <gstcuda_private.h> can include this directly"
#endif
G_BEGIN_DECLS
GST_CUDA_API
gboolean gst_cuda_load_library (void);
/* cuda.h */
GST_CUDA_API
CUresult CuInit (unsigned int Flags);
GST_CUDA_API
CUresult CuGetErrorName (CUresult error, const char **pStr);
GST_CUDA_API
CUresult CuGetErrorString (CUresult error, const char **pStr);
GST_CUDA_API
CUresult CuCtxCreate (CUcontext *pctx, unsigned int flags, CUdevice dev);
GST_CUDA_API
CUresult CuCtxDestroy (CUcontext ctx);
GST_CUDA_API
CUresult CuCtxPopCurrent (CUcontext *pctx);
GST_CUDA_API
CUresult CuCtxPushCurrent (CUcontext ctx);
GST_CUDA_API
CUresult CuGraphicsMapResources (unsigned int count, CUgraphicsResource *resources, CUstream hStream);
GST_CUDA_API
CUresult CuGraphicsUnmapResources (unsigned int count, CUgraphicsResource *resources, CUstream hStream);
GST_CUDA_API
CUresult CuGraphicsSubResourceGetMappedArray (CUarray *pArray, CUgraphicsResource resource, unsigned int arrayIndex, unsigned int mipLevel);
GST_CUDA_API
CUresult CuGraphicsResourceGetMappedPointer (CUdeviceptr *pDevPtr, size_t *pSize, CUgraphicsResource resource);
GST_CUDA_API
CUresult CuGraphicsUnregisterResource (CUgraphicsResource resource);
GST_CUDA_API
CUresult CuMemAlloc (CUdeviceptr *dptr, unsigned int bytesize);
GST_CUDA_API
CUresult CuMemAllocPitch (CUdeviceptr *dptr, size_t *pPitch, size_t WidthInBytes, size_t Height, unsigned int ElementSizeBytes);
GST_CUDA_API
CUresult CuMemAllocHost (void **pp, unsigned int bytesize);
GST_CUDA_API
CUresult CuMemcpy2D (const CUDA_MEMCPY2D *pCopy);
GST_CUDA_API
CUresult CuMemcpy2DAsync (const CUDA_MEMCPY2D *pCopy, CUstream hStream);
GST_CUDA_API
CUresult CuMemcpyHtoD (CUdeviceptr dstDevice, const void *srcHost, unsigned int ByteCount);
GST_CUDA_API
CUresult CuMemcpyDtoH (void *dstHost, CUdeviceptr srcDevice, unsigned int ByteCount);
GST_CUDA_API
CUresult CuMemcpyDtoD (CUdeviceptr dstDevice, CUdeviceptr srcDevice, unsigned int ByteCount);
GST_CUDA_API
CUresult CuMemFree (CUdeviceptr dptr);
GST_CUDA_API
CUresult CuMemFreeHost (void *p);
GST_CUDA_API
CUresult CuStreamSynchronize (CUstream hStream);
GST_CUDA_API
CUresult CuDeviceGet (CUdevice *device, int ordinal);
GST_CUDA_API
CUresult CuDeviceGetCount (int *count);
GST_CUDA_API
CUresult CuDeviceGetName (char *name, int len, CUdevice dev);
GST_CUDA_API
CUresult CuDeviceGetAttribute (int *pi, CUdevice_attribute attrib, CUdevice dev);
/* cudaGL.h */
GST_CUDA_API
CUresult CuGraphicsGLRegisterImage (CUgraphicsResource *pCudaResource, unsigned int image, unsigned int target, unsigned int Flags);
GST_CUDA_API
CUresult CuGraphicsGLRegisterBuffer (CUgraphicsResource *pCudaResource, unsigned int buffer, unsigned int Flags);
G_END_DECLS
......
......@@ -23,6 +23,8 @@
#include "gstcudamemory.h"
#include "gstcudautils.h"
#include "gstcudacontext.h"
#include "gstcuda_private.h"
#include <string.h>
......
......@@ -22,8 +22,8 @@
#include <gst/gst.h>
#include <gst/gstallocator.h>
#include "gstcudaloader.h"
#include "gstcudacontext.h"
#include <gst/cuda/gstcuda_fwd.h>
#include <gst/cuda/cuda-prelude.h>
G_BEGIN_DECLS
......@@ -36,10 +36,6 @@ G_BEGIN_DECLS
#define GST_CUDA_ALLOCATOR_CAST(obj) ((GstCudaAllocator *)(obj))
#define GST_CUDA_MEMORY_CAST(mem) ((GstCudaMemory *) (mem))
typedef struct _GstCudaAllocator GstCudaAllocator;
typedef struct _GstCudaAllocatorClass GstCudaAllocatorClass;
typedef struct _GstCudaMemory GstCudaMemory;
/**
* GST_MAP_CUDA:
*
......@@ -83,8 +79,10 @@ struct _GstCudaAllocatorClass
GstAllocatorClass parent_class;
};
GST_CUDA_API
GType gst_cuda_allocator_get_type (void);
GST_CUDA_API
GstAllocator * gst_cuda_allocator_new (GstCudaContext * context,
GstCudaMemoryTarget target);
......@@ -108,8 +106,10 @@ struct _GstCudaMemory
GMutex lock;
};
GST_CUDA_API
gboolean gst_is_cuda_memory (GstMemory * mem);
GST_CUDA_API
gboolean gst_cuda_memory_get_target (GstCudaMemory * mem,
GstCudaMemoryTarget * target);
......
......@@ -21,9 +21,10 @@
#include "config.h"
#endif
#include "gstcudaloader.h"
#include "gstcuda_private.h"
#include "gstcudautils.h"
#include "gstcudacontext.h"
#include "gstcudaloader.h"
GST_DEBUG_CATEGORY_STATIC (gst_cuda_utils_debug);
#define GST_CAT_DEFAULT gst_cuda_utils_debug
......
......@@ -21,25 +21,31 @@
#define __GST_CUDA_UTILS_H__
#include <gst/gst.h>
#include "gstcudacontext.h"
#include <gst/cuda/cuda-prelude.h>
#include <gst/cuda/gstcuda_fwd.h>
G_BEGIN_DECLS
GST_CUDA_API
gboolean gst_cuda_result (gint result);
GST_CUDA_API
gboolean gst_cuda_ensure_element_context (GstElement * element,
GstCudaContext ** cuda_ctx,
gint device_id);
GST_CUDA_API
gboolean gst_cuda_handle_set_context (GstElement * element,
GstContext * context,
GstCudaContext ** cuda_ctx,
gint device_id);
GST_CUDA_API
gboolean gst_cuda_handle_context_query (GstElement * element,
GstQuery * query,
GstCudaContext * cuda_ctx);
GST_CUDA_API
GstContext * gst_context_new_cuda_context (GstCudaContext * context);
......
cuda_sources = [
'gstcudaloader.c',
'gstcudautils.c',
'gstcudacontext.c',
'gstcudamemory.c',
'gstcudabufferpool.c',
]
cuda_headers = [
'gstcuda.h',
'gstcuda_fwd.h',
'cuda-prelude.h',
'gstcudautils.h',
'gstcudacontext.h',
'gstcudamemory.h',
'gstcudabufferpool.h',
]
cuda_dep = dependency('', required : false)
cuda_header_dep = dependency('', required : false)
gstcuda_dep = dependency('', required : false)
cuda_incdir = ''
# FIXME: allow cuda without opengl
if not gstgl_dep.found()
subdir_done()
endif
# CUDA dependency
if host_machine.system() == 'windows'
# On windows, CUDA_PATH env will be set by installer
cuda_root = run_command(python3, '-c', 'import os; print(os.environ.get("CUDA_PATH"))').stdout().strip()
if cuda_root != '' and cuda_root != 'None'
cuda_incdir = join_paths (cuda_root, 'include')
if cc.has_header('cuda.h', args: '-I' + cuda_incdir)
cuda_header_dep = declare_dependency(include_directories: include_directories(cuda_incdir))
endif
endif
else
cuda_versions = [
'10.1',
'10.0',
'9.2',