• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "gstnvrtcloader.h"
25 #include "gstcudaloader.h"
26 
27 #include <gmodule.h>
28 
29 GST_DEBUG_CATEGORY_EXTERN (gst_nvcodec_debug);
30 #define GST_CAT_DEFAULT gst_nvcodec_debug
31 
32 #ifndef G_OS_WIN32
33 #define NVRTC_LIBNAME "libnvrtc.so"
34 #else
35 #define NVRTC_LIBNAME "nvrtc64_%d%d_0.dll"
36 #endif
37 
38 #define LOAD_SYMBOL(name,func) G_STMT_START { \
39   if (!g_module_symbol (module, G_STRINGIFY (name), (gpointer *) &vtable->func)) { \
40     GST_ERROR ("Failed to load '%s' from %s, %s", G_STRINGIFY (name), fname, g_module_error()); \
41     goto error; \
42   } \
43 } G_STMT_END;
44 
45 typedef struct _GstNvCodecNvrtcVtahle
46 {
47   gboolean loaded;
48 
49     nvrtcResult (*NvrtcCompileProgram) (nvrtcProgram prog, int numOptions,
50       const char **options);
51     nvrtcResult (*NvrtcCreateProgram) (nvrtcProgram * prog, const char *src,
52       const char *name, int numHeaders, const char **headers,
53       const char **includeNames);
54     nvrtcResult (*NvrtcDestroyProgram) (nvrtcProgram * prog);
55     nvrtcResult (*NvrtcGetPTX) (nvrtcProgram prog, char *ptx);
56     nvrtcResult (*NvrtcGetPTXSize) (nvrtcProgram prog, size_t * ptxSizeRet);
57     nvrtcResult (*NvrtcGetProgramLog) (nvrtcProgram prog, char *log);
58     nvrtcResult (*NvrtcGetProgramLogSize) (nvrtcProgram prog,
59       size_t * logSizeRet);
60 } GstNvCodecNvrtcVtahle;
61 
62 static GstNvCodecNvrtcVtahle gst_nvrtc_vtable = { 0, };
63 
64 gboolean
gst_nvrtc_load_library(void)65 gst_nvrtc_load_library (void)
66 {
67   GModule *module = NULL;
68   gchar *filename = NULL;
69   const gchar *filename_env;
70   const gchar *fname;
71   gint cuda_version;
72   GstNvCodecNvrtcVtahle *vtable;
73 
74   if (gst_nvrtc_vtable.loaded)
75     return TRUE;
76 
77   CuDriverGetVersion (&cuda_version);
78 
79   fname = filename_env = g_getenv ("GST_NVCODEC_NVRTC_LIBNAME");
80   if (filename_env)
81     module = g_module_open (filename_env, G_MODULE_BIND_LAZY);
82 
83   if (!module) {
84 #ifndef G_OS_WIN32
85     filename = g_strdup (NVRTC_LIBNAME);
86     fname = filename;
87     module = g_module_open (filename, G_MODULE_BIND_LAZY);
88 #else
89     /* XXX: On Windows, minor version of nvrtc library might not be exactly
90      * same as CUDA library */
91     {
92       gint cuda_major_version = cuda_version / 1000;
93       gint cuda_minor_version = (cuda_version % 1000) / 10;
94       gint minor_version;
95 
96       for (minor_version = cuda_minor_version; minor_version >= 0;
97           minor_version--) {
98         g_free (filename);
99         filename = g_strdup_printf (NVRTC_LIBNAME, cuda_major_version,
100             minor_version);
101         fname = filename;
102 
103         module = g_module_open (filename, G_MODULE_BIND_LAZY);
104         if (module) {
105           GST_INFO ("%s is available", filename);
106           break;
107         }
108 
109         GST_DEBUG ("Couldn't open library %s", filename);
110       }
111     }
112 #endif
113   }
114 
115   if (module == NULL) {
116     GST_WARNING ("Could not open library %s, %s", filename, g_module_error ());
117     g_free (filename);
118     return FALSE;
119   }
120 
121   vtable = &gst_nvrtc_vtable;
122 
123   LOAD_SYMBOL (nvrtcCompileProgram, NvrtcCompileProgram);
124   LOAD_SYMBOL (nvrtcCreateProgram, NvrtcCreateProgram);
125   LOAD_SYMBOL (nvrtcDestroyProgram, NvrtcDestroyProgram);
126   LOAD_SYMBOL (nvrtcGetPTX, NvrtcGetPTX);
127   LOAD_SYMBOL (nvrtcGetPTXSize, NvrtcGetPTXSize);
128   LOAD_SYMBOL (nvrtcGetProgramLog, NvrtcGetProgramLog);
129   LOAD_SYMBOL (nvrtcGetProgramLogSize, NvrtcGetProgramLogSize);
130 
131   vtable->loaded = TRUE;
132   g_free (filename);
133 
134   return TRUE;
135 
136 error:
137   g_module_close (module);
138   g_free (filename);
139 
140   return FALSE;
141 }
142 
143 nvrtcResult
NvrtcCompileProgram(nvrtcProgram prog,int numOptions,const char ** options)144 NvrtcCompileProgram (nvrtcProgram prog, int numOptions, const char **options)
145 {
146   g_assert (gst_nvrtc_vtable.NvrtcCompileProgram != NULL);
147 
148   return gst_nvrtc_vtable.NvrtcCompileProgram (prog, numOptions, options);
149 }
150 
151 nvrtcResult
NvrtcCreateProgram(nvrtcProgram * prog,const char * src,const char * name,int numHeaders,const char ** headers,const char ** includeNames)152 NvrtcCreateProgram (nvrtcProgram * prog, const char *src, const char *name,
153     int numHeaders, const char **headers, const char **includeNames)
154 {
155   g_assert (gst_nvrtc_vtable.NvrtcCreateProgram != NULL);
156 
157   return gst_nvrtc_vtable.NvrtcCreateProgram (prog, src, name, numHeaders,
158       headers, includeNames);
159 }
160 
161 nvrtcResult
NvrtcDestroyProgram(nvrtcProgram * prog)162 NvrtcDestroyProgram (nvrtcProgram * prog)
163 {
164   g_assert (gst_nvrtc_vtable.NvrtcDestroyProgram != NULL);
165 
166   return gst_nvrtc_vtable.NvrtcDestroyProgram (prog);
167 }
168 
169 nvrtcResult
NvrtcGetPTX(nvrtcProgram prog,char * ptx)170 NvrtcGetPTX (nvrtcProgram prog, char *ptx)
171 {
172   g_assert (gst_nvrtc_vtable.NvrtcGetPTX != NULL);
173 
174   return gst_nvrtc_vtable.NvrtcGetPTX (prog, ptx);
175 }
176 
177 nvrtcResult
NvrtcGetPTXSize(nvrtcProgram prog,size_t * ptxSizeRet)178 NvrtcGetPTXSize (nvrtcProgram prog, size_t * ptxSizeRet)
179 {
180   g_assert (gst_nvrtc_vtable.NvrtcGetPTXSize != NULL);
181 
182   return gst_nvrtc_vtable.NvrtcGetPTXSize (prog, ptxSizeRet);
183 }
184 
185 nvrtcResult
NvrtcGetProgramLog(nvrtcProgram prog,char * log)186 NvrtcGetProgramLog (nvrtcProgram prog, char *log)
187 {
188   g_assert (gst_nvrtc_vtable.NvrtcGetProgramLog != NULL);
189 
190   return gst_nvrtc_vtable.NvrtcGetProgramLog (prog, log);
191 }
192 
193 nvrtcResult
NvrtcGetProgramLogSize(nvrtcProgram prog,size_t * logSizeRet)194 NvrtcGetProgramLogSize (nvrtcProgram prog, size_t * logSizeRet)
195 {
196   g_assert (gst_nvrtc_vtable.NvrtcGetProgramLogSize != NULL);
197 
198   return gst_nvrtc_vtable.NvrtcGetProgramLogSize (prog, logSizeRet);
199 }
200