dynlink_loader.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * This copyright notice applies to this header file only:
  3. *
  4. * Copyright (c) 2016
  5. *
  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
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  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.
  26. */
  27. #ifndef AV_COMPAT_CUDA_DYNLINK_LOADER_H
  28. #define AV_COMPAT_CUDA_DYNLINK_LOADER_H
  29. #include "compat/cuda/dynlink_cuda.h"
  30. #include "compat/cuda/dynlink_nvcuvid.h"
  31. #include "compat/nvenc/nvEncodeAPI.h"
  32. #include "compat/w32dlfcn.h"
  33. #include "libavutil/log.h"
  34. #include "libavutil/error.h"
  35. #if defined(_WIN32)
  36. # define LIB_HANDLE HMODULE
  37. #else
  38. # define LIB_HANDLE void*
  39. #endif
  40. #if defined(_WIN32) || defined(__CYGWIN__)
  41. # define CUDA_LIBNAME "nvcuda.dll"
  42. # define NVCUVID_LIBNAME "nvcuvid.dll"
  43. # if ARCH_X86_64
  44. # define NVENC_LIBNAME "nvEncodeAPI64.dll"
  45. # else
  46. # define NVENC_LIBNAME "nvEncodeAPI.dll"
  47. # endif
  48. #else
  49. # define CUDA_LIBNAME "libcuda.so.1"
  50. # define NVCUVID_LIBNAME "libnvcuvid.so.1"
  51. # define NVENC_LIBNAME "libnvidia-encode.so.1"
  52. #endif
  53. #define LOAD_LIBRARY(l, path) \
  54. do { \
  55. if (!((l) = dlopen(path, RTLD_LAZY))) { \
  56. av_log(logctx, AV_LOG_ERROR, "Cannot load %s\n", path); \
  57. ret = AVERROR_UNKNOWN; \
  58. goto error; \
  59. } \
  60. av_log(logctx, AV_LOG_TRACE, "Loaded lib: %s\n", path); \
  61. } while (0)
  62. #define LOAD_SYMBOL(fun, tp, symbol) \
  63. do { \
  64. if (!((f->fun) = (tp*)dlsym(f->lib, symbol))) { \
  65. av_log(logctx, AV_LOG_ERROR, "Cannot load %s\n", symbol); \
  66. ret = AVERROR_UNKNOWN; \
  67. goto error; \
  68. } \
  69. av_log(logctx, AV_LOG_TRACE, "Loaded sym: %s\n", symbol); \
  70. } while (0)
  71. #define LOAD_SYMBOL_OPT(fun, tp, symbol) \
  72. do { \
  73. if (!((f->fun) = (tp*)dlsym(f->lib, symbol))) { \
  74. av_log(logctx, AV_LOG_DEBUG, "Cannot load optional %s\n", symbol); \
  75. } else { \
  76. av_log(logctx, AV_LOG_TRACE, "Loaded sym: %s\n", symbol); \
  77. } \
  78. } while (0)
  79. #define GENERIC_LOAD_FUNC_PREAMBLE(T, n, N) \
  80. T *f; \
  81. int ret; \
  82. \
  83. n##_free_functions(functions); \
  84. \
  85. f = *functions = av_mallocz(sizeof(*f)); \
  86. if (!f) \
  87. return AVERROR(ENOMEM); \
  88. \
  89. LOAD_LIBRARY(f->lib, N);
  90. #define GENERIC_LOAD_FUNC_FINALE(n) \
  91. return 0; \
  92. error: \
  93. n##_free_functions(functions); \
  94. return ret;
  95. #define GENERIC_FREE_FUNC() \
  96. if (!functions) \
  97. return; \
  98. if (*functions && (*functions)->lib) \
  99. dlclose((*functions)->lib); \
  100. av_freep(functions);
  101. #ifdef AV_COMPAT_DYNLINK_CUDA_H
  102. typedef struct CudaFunctions {
  103. tcuInit *cuInit;
  104. tcuDeviceGetCount *cuDeviceGetCount;
  105. tcuDeviceGet *cuDeviceGet;
  106. tcuDeviceGetName *cuDeviceGetName;
  107. tcuDeviceComputeCapability *cuDeviceComputeCapability;
  108. tcuCtxCreate_v2 *cuCtxCreate;
  109. tcuCtxPushCurrent_v2 *cuCtxPushCurrent;
  110. tcuCtxPopCurrent_v2 *cuCtxPopCurrent;
  111. tcuCtxDestroy_v2 *cuCtxDestroy;
  112. tcuMemAlloc_v2 *cuMemAlloc;
  113. tcuMemFree_v2 *cuMemFree;
  114. tcuMemcpy2D_v2 *cuMemcpy2D;
  115. tcuGetErrorName *cuGetErrorName;
  116. tcuGetErrorString *cuGetErrorString;
  117. LIB_HANDLE lib;
  118. } CudaFunctions;
  119. #else
  120. typedef struct CudaFunctions CudaFunctions;
  121. #endif
  122. typedef struct CuvidFunctions {
  123. tcuvidGetDecoderCaps *cuvidGetDecoderCaps;
  124. tcuvidCreateDecoder *cuvidCreateDecoder;
  125. tcuvidDestroyDecoder *cuvidDestroyDecoder;
  126. tcuvidDecodePicture *cuvidDecodePicture;
  127. tcuvidMapVideoFrame *cuvidMapVideoFrame;
  128. tcuvidUnmapVideoFrame *cuvidUnmapVideoFrame;
  129. tcuvidCtxLockCreate *cuvidCtxLockCreate;
  130. tcuvidCtxLockDestroy *cuvidCtxLockDestroy;
  131. tcuvidCtxLock *cuvidCtxLock;
  132. tcuvidCtxUnlock *cuvidCtxUnlock;
  133. tcuvidCreateVideoSource *cuvidCreateVideoSource;
  134. tcuvidCreateVideoSourceW *cuvidCreateVideoSourceW;
  135. tcuvidDestroyVideoSource *cuvidDestroyVideoSource;
  136. tcuvidSetVideoSourceState *cuvidSetVideoSourceState;
  137. tcuvidGetVideoSourceState *cuvidGetVideoSourceState;
  138. tcuvidGetSourceVideoFormat *cuvidGetSourceVideoFormat;
  139. tcuvidGetSourceAudioFormat *cuvidGetSourceAudioFormat;
  140. tcuvidCreateVideoParser *cuvidCreateVideoParser;
  141. tcuvidParseVideoData *cuvidParseVideoData;
  142. tcuvidDestroyVideoParser *cuvidDestroyVideoParser;
  143. LIB_HANDLE lib;
  144. } CuvidFunctions;
  145. typedef NVENCSTATUS NVENCAPI tNvEncodeAPICreateInstance(NV_ENCODE_API_FUNCTION_LIST *functionList);
  146. typedef NVENCSTATUS NVENCAPI tNvEncodeAPIGetMaxSupportedVersion(uint32_t* version);
  147. typedef struct NvencFunctions {
  148. tNvEncodeAPICreateInstance *NvEncodeAPICreateInstance;
  149. tNvEncodeAPIGetMaxSupportedVersion *NvEncodeAPIGetMaxSupportedVersion;
  150. LIB_HANDLE lib;
  151. } NvencFunctions;
  152. #ifdef AV_COMPAT_DYNLINK_CUDA_H
  153. static inline void cuda_free_functions(CudaFunctions **functions)
  154. {
  155. GENERIC_FREE_FUNC();
  156. }
  157. #endif
  158. static inline void cuvid_free_functions(CuvidFunctions **functions)
  159. {
  160. GENERIC_FREE_FUNC();
  161. }
  162. static inline void nvenc_free_functions(NvencFunctions **functions)
  163. {
  164. GENERIC_FREE_FUNC();
  165. }
  166. #ifdef AV_COMPAT_DYNLINK_CUDA_H
  167. static inline int cuda_load_functions(CudaFunctions **functions, void *logctx)
  168. {
  169. GENERIC_LOAD_FUNC_PREAMBLE(CudaFunctions, cuda, CUDA_LIBNAME);
  170. LOAD_SYMBOL(cuInit, tcuInit, "cuInit");
  171. LOAD_SYMBOL(cuDeviceGetCount, tcuDeviceGetCount, "cuDeviceGetCount");
  172. LOAD_SYMBOL(cuDeviceGet, tcuDeviceGet, "cuDeviceGet");
  173. LOAD_SYMBOL(cuDeviceGetName, tcuDeviceGetName, "cuDeviceGetName");
  174. LOAD_SYMBOL(cuDeviceComputeCapability, tcuDeviceComputeCapability, "cuDeviceComputeCapability");
  175. LOAD_SYMBOL(cuCtxCreate, tcuCtxCreate_v2, "cuCtxCreate_v2");
  176. LOAD_SYMBOL(cuCtxPushCurrent, tcuCtxPushCurrent_v2, "cuCtxPushCurrent_v2");
  177. LOAD_SYMBOL(cuCtxPopCurrent, tcuCtxPopCurrent_v2, "cuCtxPopCurrent_v2");
  178. LOAD_SYMBOL(cuCtxDestroy, tcuCtxDestroy_v2, "cuCtxDestroy_v2");
  179. LOAD_SYMBOL(cuMemAlloc, tcuMemAlloc_v2, "cuMemAlloc_v2");
  180. LOAD_SYMBOL(cuMemFree, tcuMemFree_v2, "cuMemFree_v2");
  181. LOAD_SYMBOL(cuMemcpy2D, tcuMemcpy2D_v2, "cuMemcpy2D_v2");
  182. LOAD_SYMBOL(cuGetErrorName, tcuGetErrorName, "cuGetErrorName");
  183. LOAD_SYMBOL(cuGetErrorString, tcuGetErrorString, "cuGetErrorString");
  184. GENERIC_LOAD_FUNC_FINALE(cuda);
  185. }
  186. #endif
  187. static inline int cuvid_load_functions(CuvidFunctions **functions, void *logctx)
  188. {
  189. GENERIC_LOAD_FUNC_PREAMBLE(CuvidFunctions, cuvid, NVCUVID_LIBNAME);
  190. LOAD_SYMBOL_OPT(cuvidGetDecoderCaps, tcuvidGetDecoderCaps, "cuvidGetDecoderCaps");
  191. LOAD_SYMBOL(cuvidCreateDecoder, tcuvidCreateDecoder, "cuvidCreateDecoder");
  192. LOAD_SYMBOL(cuvidDestroyDecoder, tcuvidDestroyDecoder, "cuvidDestroyDecoder");
  193. LOAD_SYMBOL(cuvidDecodePicture, tcuvidDecodePicture, "cuvidDecodePicture");
  194. #ifdef __CUVID_DEVPTR64
  195. LOAD_SYMBOL(cuvidMapVideoFrame, tcuvidMapVideoFrame, "cuvidMapVideoFrame64");
  196. LOAD_SYMBOL(cuvidUnmapVideoFrame, tcuvidUnmapVideoFrame, "cuvidUnmapVideoFrame64");
  197. #else
  198. LOAD_SYMBOL(cuvidMapVideoFrame, tcuvidMapVideoFrame, "cuvidMapVideoFrame");
  199. LOAD_SYMBOL(cuvidUnmapVideoFrame, tcuvidUnmapVideoFrame, "cuvidUnmapVideoFrame");
  200. #endif
  201. LOAD_SYMBOL(cuvidCtxLockCreate, tcuvidCtxLockCreate, "cuvidCtxLockCreate");
  202. LOAD_SYMBOL(cuvidCtxLockDestroy, tcuvidCtxLockDestroy, "cuvidCtxLockDestroy");
  203. LOAD_SYMBOL(cuvidCtxLock, tcuvidCtxLock, "cuvidCtxLock");
  204. LOAD_SYMBOL(cuvidCtxUnlock, tcuvidCtxUnlock, "cuvidCtxUnlock");
  205. LOAD_SYMBOL(cuvidCreateVideoSource, tcuvidCreateVideoSource, "cuvidCreateVideoSource");
  206. LOAD_SYMBOL(cuvidCreateVideoSourceW, tcuvidCreateVideoSourceW, "cuvidCreateVideoSourceW");
  207. LOAD_SYMBOL(cuvidDestroyVideoSource, tcuvidDestroyVideoSource, "cuvidDestroyVideoSource");
  208. LOAD_SYMBOL(cuvidSetVideoSourceState, tcuvidSetVideoSourceState, "cuvidSetVideoSourceState");
  209. LOAD_SYMBOL(cuvidGetVideoSourceState, tcuvidGetVideoSourceState, "cuvidGetVideoSourceState");
  210. LOAD_SYMBOL(cuvidGetSourceVideoFormat, tcuvidGetSourceVideoFormat, "cuvidGetSourceVideoFormat");
  211. LOAD_SYMBOL(cuvidGetSourceAudioFormat, tcuvidGetSourceAudioFormat, "cuvidGetSourceAudioFormat");
  212. LOAD_SYMBOL(cuvidCreateVideoParser, tcuvidCreateVideoParser, "cuvidCreateVideoParser");
  213. LOAD_SYMBOL(cuvidParseVideoData, tcuvidParseVideoData, "cuvidParseVideoData");
  214. LOAD_SYMBOL(cuvidDestroyVideoParser, tcuvidDestroyVideoParser, "cuvidDestroyVideoParser");
  215. GENERIC_LOAD_FUNC_FINALE(cuvid);
  216. }
  217. static inline int nvenc_load_functions(NvencFunctions **functions, void *logctx)
  218. {
  219. GENERIC_LOAD_FUNC_PREAMBLE(NvencFunctions, nvenc, NVENC_LIBNAME);
  220. LOAD_SYMBOL(NvEncodeAPICreateInstance, tNvEncodeAPICreateInstance, "NvEncodeAPICreateInstance");
  221. LOAD_SYMBOL(NvEncodeAPIGetMaxSupportedVersion, tNvEncodeAPIGetMaxSupportedVersion, "NvEncodeAPIGetMaxSupportedVersion");
  222. GENERIC_LOAD_FUNC_FINALE(nvenc);
  223. }
  224. #undef GENERIC_LOAD_FUNC_PREAMBLE
  225. #undef LOAD_LIBRARY
  226. #undef LOAD_SYMBOL
  227. #undef GENERIC_LOAD_FUNC_FINALE
  228. #undef GENERIC_FREE_FUNC
  229. #undef CUDA_LIBNAME
  230. #undef NVCUVID_LIBNAME
  231. #undef NVENC_LIBNAME
  232. #undef LIB_HANDLE
  233. #endif