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, symbol) \
71 if (!((f->fun) = 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 GENERIC_LOAD_FUNC_PREAMBLE(T, n, N) \
83 n##_free_functions(functions); \
85 f = *functions = av_mallocz(sizeof(*f)); \
87 return AVERROR(ENOMEM); \
89 LOAD_LIBRARY(f->lib, N);
91 #define GENERIC_LOAD_FUNC_FINALE(n) \
94 n##_free_functions(functions); \
97 #define GENERIC_FREE_FUNC() \
100 if (*functions && (*functions)->lib) \
101 dlclose((*functions)->lib); \
104 #ifdef AV_COMPAT_DYNLINK_CUDA_H
105 typedef struct CudaFunctions {
107 tcuDeviceGetCount *cuDeviceGetCount;
108 tcuDeviceGet *cuDeviceGet;
109 tcuDeviceGetName *cuDeviceGetName;
110 tcuDeviceComputeCapability *cuDeviceComputeCapability;
111 tcuCtxCreate_v2 *cuCtxCreate;
112 tcuCtxPushCurrent_v2 *cuCtxPushCurrent;
113 tcuCtxPopCurrent_v2 *cuCtxPopCurrent;
114 tcuCtxDestroy_v2 *cuCtxDestroy;
115 tcuMemAlloc_v2 *cuMemAlloc;
116 tcuMemFree_v2 *cuMemFree;
117 tcuMemcpy2D_v2 *cuMemcpy2D;
118 tcuGetErrorName *cuGetErrorName;
119 tcuGetErrorString *cuGetErrorString;
124 typedef struct CudaFunctions CudaFunctions;
127 typedef struct CuvidFunctions {
128 tcuvidGetDecoderCaps *cuvidGetDecoderCaps;
129 tcuvidCreateDecoder *cuvidCreateDecoder;
130 tcuvidDestroyDecoder *cuvidDestroyDecoder;
131 tcuvidDecodePicture *cuvidDecodePicture;
132 tcuvidMapVideoFrame *cuvidMapVideoFrame;
133 tcuvidUnmapVideoFrame *cuvidUnmapVideoFrame;
134 tcuvidCtxLockCreate *cuvidCtxLockCreate;
135 tcuvidCtxLockDestroy *cuvidCtxLockDestroy;
136 tcuvidCtxLock *cuvidCtxLock;
137 tcuvidCtxUnlock *cuvidCtxUnlock;
139 tcuvidCreateVideoSource *cuvidCreateVideoSource;
140 tcuvidCreateVideoSourceW *cuvidCreateVideoSourceW;
141 tcuvidDestroyVideoSource *cuvidDestroyVideoSource;
142 tcuvidSetVideoSourceState *cuvidSetVideoSourceState;
143 tcuvidGetVideoSourceState *cuvidGetVideoSourceState;
144 tcuvidGetSourceVideoFormat *cuvidGetSourceVideoFormat;
145 tcuvidGetSourceAudioFormat *cuvidGetSourceAudioFormat;
146 tcuvidCreateVideoParser *cuvidCreateVideoParser;
147 tcuvidParseVideoData *cuvidParseVideoData;
148 tcuvidDestroyVideoParser *cuvidDestroyVideoParser;
153 typedef struct NvencFunctions {
154 NVENCSTATUS (NVENCAPI *NvEncodeAPICreateInstance)(NV_ENCODE_API_FUNCTION_LIST *functionList);
155 NVENCSTATUS (NVENCAPI *NvEncodeAPIGetMaxSupportedVersion)(uint32_t* version);
160 #ifdef AV_COMPAT_DYNLINK_CUDA_H
161 static inline void cuda_free_functions(CudaFunctions **functions)
167 static inline void cuvid_free_functions(CuvidFunctions **functions)
172 static inline void nvenc_free_functions(NvencFunctions **functions)
177 #ifdef AV_COMPAT_DYNLINK_CUDA_H
178 static inline int cuda_load_functions(CudaFunctions **functions)
180 GENERIC_LOAD_FUNC_PREAMBLE(CudaFunctions, cuda, CUDA_LIBNAME);
182 LOAD_SYMBOL(cuInit, "cuInit");
183 LOAD_SYMBOL(cuDeviceGetCount, "cuDeviceGetCount");
184 LOAD_SYMBOL(cuDeviceGet, "cuDeviceGet");
185 LOAD_SYMBOL(cuDeviceGetName, "cuDeviceGetName");
186 LOAD_SYMBOL(cuDeviceComputeCapability, "cuDeviceComputeCapability");
187 LOAD_SYMBOL(cuCtxCreate, "cuCtxCreate_v2");
188 LOAD_SYMBOL(cuCtxPushCurrent, "cuCtxPushCurrent_v2");
189 LOAD_SYMBOL(cuCtxPopCurrent, "cuCtxPopCurrent_v2");
190 LOAD_SYMBOL(cuCtxDestroy, "cuCtxDestroy_v2");
191 LOAD_SYMBOL(cuMemAlloc, "cuMemAlloc_v2");
192 LOAD_SYMBOL(cuMemFree, "cuMemFree_v2");
193 LOAD_SYMBOL(cuMemcpy2D, "cuMemcpy2D_v2");
194 LOAD_SYMBOL(cuGetErrorName, "cuGetErrorName");
195 LOAD_SYMBOL(cuGetErrorString, "cuGetErrorString");
197 GENERIC_LOAD_FUNC_FINALE(cuda);
201 static inline int cuvid_load_functions(CuvidFunctions **functions)
203 GENERIC_LOAD_FUNC_PREAMBLE(CuvidFunctions, cuvid, NVCUVID_LIBNAME);
205 LOAD_SYMBOL(cuvidGetDecoderCaps, "cuvidGetDecoderCaps");
206 LOAD_SYMBOL(cuvidCreateDecoder, "cuvidCreateDecoder");
207 LOAD_SYMBOL(cuvidDestroyDecoder, "cuvidDestroyDecoder");
208 LOAD_SYMBOL(cuvidDecodePicture, "cuvidDecodePicture");
209 #ifdef __CUVID_DEVPTR64
210 LOAD_SYMBOL(cuvidMapVideoFrame, "cuvidMapVideoFrame64");
211 LOAD_SYMBOL(cuvidUnmapVideoFrame, "cuvidUnmapVideoFrame64");
213 LOAD_SYMBOL(cuvidMapVideoFrame, "cuvidMapVideoFrame");
214 LOAD_SYMBOL(cuvidUnmapVideoFrame, "cuvidUnmapVideoFrame");
216 LOAD_SYMBOL(cuvidCtxLockCreate, "cuvidCtxLockCreate");
217 LOAD_SYMBOL(cuvidCtxLockDestroy, "cuvidCtxLockDestroy");
218 LOAD_SYMBOL(cuvidCtxLock, "cuvidCtxLock");
219 LOAD_SYMBOL(cuvidCtxUnlock, "cuvidCtxUnlock");
221 LOAD_SYMBOL(cuvidCreateVideoSource, "cuvidCreateVideoSource");
222 LOAD_SYMBOL(cuvidCreateVideoSourceW, "cuvidCreateVideoSourceW");
223 LOAD_SYMBOL(cuvidDestroyVideoSource, "cuvidDestroyVideoSource");
224 LOAD_SYMBOL(cuvidSetVideoSourceState, "cuvidSetVideoSourceState");
225 LOAD_SYMBOL(cuvidGetVideoSourceState, "cuvidGetVideoSourceState");
226 LOAD_SYMBOL(cuvidGetSourceVideoFormat, "cuvidGetSourceVideoFormat");
227 LOAD_SYMBOL(cuvidGetSourceAudioFormat, "cuvidGetSourceAudioFormat");
228 LOAD_SYMBOL(cuvidCreateVideoParser, "cuvidCreateVideoParser");
229 LOAD_SYMBOL(cuvidParseVideoData, "cuvidParseVideoData");
230 LOAD_SYMBOL(cuvidDestroyVideoParser, "cuvidDestroyVideoParser");
232 GENERIC_LOAD_FUNC_FINALE(cuvid);
235 static inline int nvenc_load_functions(NvencFunctions **functions)
237 GENERIC_LOAD_FUNC_PREAMBLE(NvencFunctions, nvenc, NVENC_LIBNAME);
239 LOAD_SYMBOL(NvEncodeAPICreateInstance, "NvEncodeAPICreateInstance");
240 LOAD_SYMBOL(NvEncodeAPIGetMaxSupportedVersion, "NvEncodeAPIGetMaxSupportedVersion");
242 GENERIC_LOAD_FUNC_FINALE(nvenc);
245 #undef GENERIC_LOAD_FUNC_PREAMBLE
248 #undef GENERIC_LOAD_FUNC_FINALE
249 #undef GENERIC_FREE_FUNC
251 #undef NVCUVID_LIBNAME