2 * This copyright notice applies to this header file only:
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the software, and to permit persons to whom the
12 * software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
28 #ifndef AV_COMPAT_CUDA_DYNLINK_LOADER_H
29 #define AV_COMPAT_CUDA_DYNLINK_LOADER_H
31 #include "compat/cuda/dynlink_cuda.h"
32 #include "compat/cuda/dynlink_nvcuvid.h"
33 #include "compat/nvenc/nvEncodeAPI.h"
34 #include "compat/w32dlfcn.h"
36 #include "libavutil/log.h"
37 #include "libavutil/error.h"
40 # define LIB_HANDLE HMODULE
42 # define LIB_HANDLE void*
45 #if defined(_WIN32) || defined(__CYGWIN__)
46 # define CUDA_LIBNAME "nvcuda.dll"
47 # define NVCUVID_LIBNAME "nvcuvid.dll"
49 # define NVENC_LIBNAME "nvEncodeAPI64.dll"
51 # define NVENC_LIBNAME "nvEncodeAPI.dll"
54 # define CUDA_LIBNAME "libcuda.so.1"
55 # define NVCUVID_LIBNAME "libnvcuvid.so.1"
56 # define NVENC_LIBNAME "libnvidia-encode.so.1"
59 #define LOAD_LIBRARY(l, path) \
61 if (!((l) = dlopen(path, RTLD_LAZY))) { \
62 av_log(NULL, AV_LOG_ERROR, "Cannot load %s\n", path); \
63 ret = AVERROR_UNKNOWN; \
66 av_log(NULL, AV_LOG_TRACE, "Loaded lib: %s\n", path); \
69 #define LOAD_SYMBOL(fun, tp, symbol) \
71 if (!((f->fun) = (tp*)dlsym(f->lib, symbol))) { \
72 av_log(NULL, AV_LOG_ERROR, "Cannot load %s\n", symbol); \
73 ret = AVERROR_UNKNOWN; \
76 av_log(NULL, AV_LOG_TRACE, "Loaded sym: %s\n", symbol); \
79 #define LOAD_SYMBOL_OPT(fun, tp, symbol) \
81 if (!((f->fun) = (tp*)dlsym(f->lib, symbol))) { \
82 av_log(NULL, AV_LOG_DEBUG, "Cannot load optional %s\n", symbol); \
84 av_log(NULL, AV_LOG_TRACE, "Loaded sym: %s\n", symbol); \
88 #define GENERIC_LOAD_FUNC_PREAMBLE(T, n, N) \
92 n##_free_functions(functions); \
94 f = *functions = av_mallocz(sizeof(*f)); \
96 return AVERROR(ENOMEM); \
98 LOAD_LIBRARY(f->lib, N);
100 #define GENERIC_LOAD_FUNC_FINALE(n) \
103 n##_free_functions(functions); \
106 #define GENERIC_FREE_FUNC() \
109 if (*functions && (*functions)->lib) \
110 dlclose((*functions)->lib); \
113 #ifdef AV_COMPAT_DYNLINK_CUDA_H
114 typedef struct CudaFunctions {
116 tcuDeviceGetCount *cuDeviceGetCount;
117 tcuDeviceGet *cuDeviceGet;
118 tcuDeviceGetName *cuDeviceGetName;
119 tcuDeviceComputeCapability *cuDeviceComputeCapability;
120 tcuCtxCreate_v2 *cuCtxCreate;
121 tcuCtxPushCurrent_v2 *cuCtxPushCurrent;
122 tcuCtxPopCurrent_v2 *cuCtxPopCurrent;
123 tcuCtxDestroy_v2 *cuCtxDestroy;
124 tcuMemAlloc_v2 *cuMemAlloc;
125 tcuMemFree_v2 *cuMemFree;
126 tcuMemcpy2D_v2 *cuMemcpy2D;
127 tcuGetErrorName *cuGetErrorName;
128 tcuGetErrorString *cuGetErrorString;
133 typedef struct CudaFunctions CudaFunctions;
136 typedef struct CuvidFunctions {
137 tcuvidGetDecoderCaps *cuvidGetDecoderCaps;
138 tcuvidCreateDecoder *cuvidCreateDecoder;
139 tcuvidDestroyDecoder *cuvidDestroyDecoder;
140 tcuvidDecodePicture *cuvidDecodePicture;
141 tcuvidMapVideoFrame *cuvidMapVideoFrame;
142 tcuvidUnmapVideoFrame *cuvidUnmapVideoFrame;
143 tcuvidCtxLockCreate *cuvidCtxLockCreate;
144 tcuvidCtxLockDestroy *cuvidCtxLockDestroy;
145 tcuvidCtxLock *cuvidCtxLock;
146 tcuvidCtxUnlock *cuvidCtxUnlock;
148 tcuvidCreateVideoSource *cuvidCreateVideoSource;
149 tcuvidCreateVideoSourceW *cuvidCreateVideoSourceW;
150 tcuvidDestroyVideoSource *cuvidDestroyVideoSource;
151 tcuvidSetVideoSourceState *cuvidSetVideoSourceState;
152 tcuvidGetVideoSourceState *cuvidGetVideoSourceState;
153 tcuvidGetSourceVideoFormat *cuvidGetSourceVideoFormat;
154 tcuvidGetSourceAudioFormat *cuvidGetSourceAudioFormat;
155 tcuvidCreateVideoParser *cuvidCreateVideoParser;
156 tcuvidParseVideoData *cuvidParseVideoData;
157 tcuvidDestroyVideoParser *cuvidDestroyVideoParser;
162 typedef NVENCSTATUS NVENCAPI tNvEncodeAPICreateInstance(NV_ENCODE_API_FUNCTION_LIST *functionList);
163 typedef NVENCSTATUS NVENCAPI tNvEncodeAPIGetMaxSupportedVersion(uint32_t* version);
165 typedef struct NvencFunctions {
166 tNvEncodeAPICreateInstance *NvEncodeAPICreateInstance;
167 tNvEncodeAPIGetMaxSupportedVersion *NvEncodeAPIGetMaxSupportedVersion;
172 #ifdef AV_COMPAT_DYNLINK_CUDA_H
173 static inline void cuda_free_functions(CudaFunctions **functions)
179 static inline void cuvid_free_functions(CuvidFunctions **functions)
184 static inline void nvenc_free_functions(NvencFunctions **functions)
189 #ifdef AV_COMPAT_DYNLINK_CUDA_H
190 static inline int cuda_load_functions(CudaFunctions **functions)
192 GENERIC_LOAD_FUNC_PREAMBLE(CudaFunctions, cuda, CUDA_LIBNAME);
194 LOAD_SYMBOL(cuInit, tcuInit, "cuInit");
195 LOAD_SYMBOL(cuDeviceGetCount, tcuDeviceGetCount, "cuDeviceGetCount");
196 LOAD_SYMBOL(cuDeviceGet, tcuDeviceGet, "cuDeviceGet");
197 LOAD_SYMBOL(cuDeviceGetName, tcuDeviceGetName, "cuDeviceGetName");
198 LOAD_SYMBOL(cuDeviceComputeCapability, tcuDeviceComputeCapability, "cuDeviceComputeCapability");
199 LOAD_SYMBOL(cuCtxCreate, tcuCtxCreate_v2, "cuCtxCreate_v2");
200 LOAD_SYMBOL(cuCtxPushCurrent, tcuCtxPushCurrent_v2, "cuCtxPushCurrent_v2");
201 LOAD_SYMBOL(cuCtxPopCurrent, tcuCtxPopCurrent_v2, "cuCtxPopCurrent_v2");
202 LOAD_SYMBOL(cuCtxDestroy, tcuCtxDestroy_v2, "cuCtxDestroy_v2");
203 LOAD_SYMBOL(cuMemAlloc, tcuMemAlloc_v2, "cuMemAlloc_v2");
204 LOAD_SYMBOL(cuMemFree, tcuMemFree_v2, "cuMemFree_v2");
205 LOAD_SYMBOL(cuMemcpy2D, tcuMemcpy2D_v2, "cuMemcpy2D_v2");
206 LOAD_SYMBOL(cuGetErrorName, tcuGetErrorName, "cuGetErrorName");
207 LOAD_SYMBOL(cuGetErrorString, tcuGetErrorString, "cuGetErrorString");
209 GENERIC_LOAD_FUNC_FINALE(cuda);
213 static inline int cuvid_load_functions(CuvidFunctions **functions)
215 GENERIC_LOAD_FUNC_PREAMBLE(CuvidFunctions, cuvid, NVCUVID_LIBNAME);
217 LOAD_SYMBOL_OPT(cuvidGetDecoderCaps, tcuvidGetDecoderCaps, "cuvidGetDecoderCaps");
218 LOAD_SYMBOL(cuvidCreateDecoder, tcuvidCreateDecoder, "cuvidCreateDecoder");
219 LOAD_SYMBOL(cuvidDestroyDecoder, tcuvidDestroyDecoder, "cuvidDestroyDecoder");
220 LOAD_SYMBOL(cuvidDecodePicture, tcuvidDecodePicture, "cuvidDecodePicture");
221 #ifdef __CUVID_DEVPTR64
222 LOAD_SYMBOL(cuvidMapVideoFrame, tcuvidMapVideoFrame, "cuvidMapVideoFrame64");
223 LOAD_SYMBOL(cuvidUnmapVideoFrame, tcuvidUnmapVideoFrame, "cuvidUnmapVideoFrame64");
225 LOAD_SYMBOL(cuvidMapVideoFrame, tcuvidMapVideoFrame, "cuvidMapVideoFrame");
226 LOAD_SYMBOL(cuvidUnmapVideoFrame, tcuvidUnmapVideoFrame, "cuvidUnmapVideoFrame");
228 LOAD_SYMBOL(cuvidCtxLockCreate, tcuvidCtxLockCreate, "cuvidCtxLockCreate");
229 LOAD_SYMBOL(cuvidCtxLockDestroy, tcuvidCtxLockDestroy, "cuvidCtxLockDestroy");
230 LOAD_SYMBOL(cuvidCtxLock, tcuvidCtxLock, "cuvidCtxLock");
231 LOAD_SYMBOL(cuvidCtxUnlock, tcuvidCtxUnlock, "cuvidCtxUnlock");
233 LOAD_SYMBOL(cuvidCreateVideoSource, tcuvidCreateVideoSource, "cuvidCreateVideoSource");
234 LOAD_SYMBOL(cuvidCreateVideoSourceW, tcuvidCreateVideoSourceW, "cuvidCreateVideoSourceW");
235 LOAD_SYMBOL(cuvidDestroyVideoSource, tcuvidDestroyVideoSource, "cuvidDestroyVideoSource");
236 LOAD_SYMBOL(cuvidSetVideoSourceState, tcuvidSetVideoSourceState, "cuvidSetVideoSourceState");
237 LOAD_SYMBOL(cuvidGetVideoSourceState, tcuvidGetVideoSourceState, "cuvidGetVideoSourceState");
238 LOAD_SYMBOL(cuvidGetSourceVideoFormat, tcuvidGetSourceVideoFormat, "cuvidGetSourceVideoFormat");
239 LOAD_SYMBOL(cuvidGetSourceAudioFormat, tcuvidGetSourceAudioFormat, "cuvidGetSourceAudioFormat");
240 LOAD_SYMBOL(cuvidCreateVideoParser, tcuvidCreateVideoParser, "cuvidCreateVideoParser");
241 LOAD_SYMBOL(cuvidParseVideoData, tcuvidParseVideoData, "cuvidParseVideoData");
242 LOAD_SYMBOL(cuvidDestroyVideoParser, tcuvidDestroyVideoParser, "cuvidDestroyVideoParser");
244 GENERIC_LOAD_FUNC_FINALE(cuvid);
247 static inline int nvenc_load_functions(NvencFunctions **functions)
249 GENERIC_LOAD_FUNC_PREAMBLE(NvencFunctions, nvenc, NVENC_LIBNAME);
251 LOAD_SYMBOL(NvEncodeAPICreateInstance, tNvEncodeAPICreateInstance, "NvEncodeAPICreateInstance");
252 LOAD_SYMBOL(NvEncodeAPIGetMaxSupportedVersion, tNvEncodeAPIGetMaxSupportedVersion, "NvEncodeAPIGetMaxSupportedVersion");
254 GENERIC_LOAD_FUNC_FINALE(nvenc);
257 #undef GENERIC_LOAD_FUNC_PREAMBLE
260 #undef GENERIC_LOAD_FUNC_FINALE
261 #undef GENERIC_FREE_FUNC
263 #undef NVCUVID_LIBNAME