You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
9.2KB

  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(NULL, AV_LOG_ERROR, "Cannot load %s\n", path); \
  57. ret = AVERROR_UNKNOWN; \
  58. goto error; \
  59. } \
  60. av_log(NULL, AV_LOG_TRACE, "Loaded lib: %s\n", path); \
  61. } while (0)
  62. #define LOAD_SYMBOL(fun, symbol) \
  63. do { \
  64. if (!((f->fun) = dlsym(f->lib, symbol))) { \
  65. av_log(NULL, AV_LOG_ERROR, "Cannot load %s\n", symbol); \
  66. ret = AVERROR_UNKNOWN; \
  67. goto error; \
  68. } \
  69. av_log(NULL, AV_LOG_TRACE, "Loaded sym: %s\n", symbol); \
  70. } while (0)
  71. #define GENERIC_LOAD_FUNC_PREAMBLE(T, n, N) \
  72. T *f; \
  73. int ret; \
  74. \
  75. n##_free_functions(functions); \
  76. \
  77. f = *functions = av_mallocz(sizeof(*f)); \
  78. if (!f) \
  79. return AVERROR(ENOMEM); \
  80. \
  81. LOAD_LIBRARY(f->lib, N);
  82. #define GENERIC_LOAD_FUNC_FINALE(n) \
  83. return 0; \
  84. error: \
  85. n##_free_functions(functions); \
  86. return ret;
  87. #define GENERIC_FREE_FUNC() \
  88. if (!functions) \
  89. return; \
  90. if (*functions && (*functions)->lib) \
  91. dlclose((*functions)->lib); \
  92. av_freep(functions);
  93. #ifdef AV_COMPAT_DYNLINK_CUDA_H
  94. typedef struct CudaFunctions {
  95. tcuInit *cuInit;
  96. tcuDeviceGetCount *cuDeviceGetCount;
  97. tcuDeviceGet *cuDeviceGet;
  98. tcuDeviceGetName *cuDeviceGetName;
  99. tcuDeviceComputeCapability *cuDeviceComputeCapability;
  100. tcuCtxCreate_v2 *cuCtxCreate;
  101. tcuCtxPushCurrent_v2 *cuCtxPushCurrent;
  102. tcuCtxPopCurrent_v2 *cuCtxPopCurrent;
  103. tcuCtxDestroy_v2 *cuCtxDestroy;
  104. tcuMemAlloc_v2 *cuMemAlloc;
  105. tcuMemFree_v2 *cuMemFree;
  106. tcuMemcpy2D_v2 *cuMemcpy2D;
  107. tcuGetErrorName *cuGetErrorName;
  108. tcuGetErrorString *cuGetErrorString;
  109. LIB_HANDLE lib;
  110. } CudaFunctions;
  111. #else
  112. typedef struct CudaFunctions CudaFunctions;
  113. #endif
  114. typedef struct CuvidFunctions {
  115. tcuvidGetDecoderCaps *cuvidGetDecoderCaps;
  116. tcuvidCreateDecoder *cuvidCreateDecoder;
  117. tcuvidDestroyDecoder *cuvidDestroyDecoder;
  118. tcuvidDecodePicture *cuvidDecodePicture;
  119. tcuvidMapVideoFrame *cuvidMapVideoFrame;
  120. tcuvidUnmapVideoFrame *cuvidUnmapVideoFrame;
  121. tcuvidCtxLockCreate *cuvidCtxLockCreate;
  122. tcuvidCtxLockDestroy *cuvidCtxLockDestroy;
  123. tcuvidCtxLock *cuvidCtxLock;
  124. tcuvidCtxUnlock *cuvidCtxUnlock;
  125. tcuvidCreateVideoSource *cuvidCreateVideoSource;
  126. tcuvidCreateVideoSourceW *cuvidCreateVideoSourceW;
  127. tcuvidDestroyVideoSource *cuvidDestroyVideoSource;
  128. tcuvidSetVideoSourceState *cuvidSetVideoSourceState;
  129. tcuvidGetVideoSourceState *cuvidGetVideoSourceState;
  130. tcuvidGetSourceVideoFormat *cuvidGetSourceVideoFormat;
  131. tcuvidGetSourceAudioFormat *cuvidGetSourceAudioFormat;
  132. tcuvidCreateVideoParser *cuvidCreateVideoParser;
  133. tcuvidParseVideoData *cuvidParseVideoData;
  134. tcuvidDestroyVideoParser *cuvidDestroyVideoParser;
  135. LIB_HANDLE lib;
  136. } CuvidFunctions;
  137. typedef struct NvencFunctions {
  138. NVENCSTATUS (NVENCAPI *NvEncodeAPICreateInstance)(NV_ENCODE_API_FUNCTION_LIST *functionList);
  139. NVENCSTATUS (NVENCAPI *NvEncodeAPIGetMaxSupportedVersion)(uint32_t* version);
  140. LIB_HANDLE lib;
  141. } NvencFunctions;
  142. #ifdef AV_COMPAT_DYNLINK_CUDA_H
  143. static inline void cuda_free_functions(CudaFunctions **functions)
  144. {
  145. GENERIC_FREE_FUNC();
  146. }
  147. #endif
  148. static inline void cuvid_free_functions(CuvidFunctions **functions)
  149. {
  150. GENERIC_FREE_FUNC();
  151. }
  152. static inline void nvenc_free_functions(NvencFunctions **functions)
  153. {
  154. GENERIC_FREE_FUNC();
  155. }
  156. #ifdef AV_COMPAT_DYNLINK_CUDA_H
  157. static inline int cuda_load_functions(CudaFunctions **functions)
  158. {
  159. GENERIC_LOAD_FUNC_PREAMBLE(CudaFunctions, cuda, CUDA_LIBNAME);
  160. LOAD_SYMBOL(cuInit, "cuInit");
  161. LOAD_SYMBOL(cuDeviceGetCount, "cuDeviceGetCount");
  162. LOAD_SYMBOL(cuDeviceGet, "cuDeviceGet");
  163. LOAD_SYMBOL(cuDeviceGetName, "cuDeviceGetName");
  164. LOAD_SYMBOL(cuDeviceComputeCapability, "cuDeviceComputeCapability");
  165. LOAD_SYMBOL(cuCtxCreate, "cuCtxCreate_v2");
  166. LOAD_SYMBOL(cuCtxPushCurrent, "cuCtxPushCurrent_v2");
  167. LOAD_SYMBOL(cuCtxPopCurrent, "cuCtxPopCurrent_v2");
  168. LOAD_SYMBOL(cuCtxDestroy, "cuCtxDestroy_v2");
  169. LOAD_SYMBOL(cuMemAlloc, "cuMemAlloc_v2");
  170. LOAD_SYMBOL(cuMemFree, "cuMemFree_v2");
  171. LOAD_SYMBOL(cuMemcpy2D, "cuMemcpy2D_v2");
  172. LOAD_SYMBOL(cuGetErrorName, "cuGetErrorName");
  173. LOAD_SYMBOL(cuGetErrorString, "cuGetErrorString");
  174. GENERIC_LOAD_FUNC_FINALE(cuda);
  175. }
  176. #endif
  177. static inline int cuvid_load_functions(CuvidFunctions **functions)
  178. {
  179. GENERIC_LOAD_FUNC_PREAMBLE(CuvidFunctions, cuvid, NVCUVID_LIBNAME);
  180. LOAD_SYMBOL(cuvidGetDecoderCaps, "cuvidGetDecoderCaps");
  181. LOAD_SYMBOL(cuvidCreateDecoder, "cuvidCreateDecoder");
  182. LOAD_SYMBOL(cuvidDestroyDecoder, "cuvidDestroyDecoder");
  183. LOAD_SYMBOL(cuvidDecodePicture, "cuvidDecodePicture");
  184. #ifdef __CUVID_DEVPTR64
  185. LOAD_SYMBOL(cuvidMapVideoFrame, "cuvidMapVideoFrame64");
  186. LOAD_SYMBOL(cuvidUnmapVideoFrame, "cuvidUnmapVideoFrame64");
  187. #else
  188. LOAD_SYMBOL(cuvidMapVideoFrame, "cuvidMapVideoFrame");
  189. LOAD_SYMBOL(cuvidUnmapVideoFrame, "cuvidUnmapVideoFrame");
  190. #endif
  191. LOAD_SYMBOL(cuvidCtxLockCreate, "cuvidCtxLockCreate");
  192. LOAD_SYMBOL(cuvidCtxLockDestroy, "cuvidCtxLockDestroy");
  193. LOAD_SYMBOL(cuvidCtxLock, "cuvidCtxLock");
  194. LOAD_SYMBOL(cuvidCtxUnlock, "cuvidCtxUnlock");
  195. LOAD_SYMBOL(cuvidCreateVideoSource, "cuvidCreateVideoSource");
  196. LOAD_SYMBOL(cuvidCreateVideoSourceW, "cuvidCreateVideoSourceW");
  197. LOAD_SYMBOL(cuvidDestroyVideoSource, "cuvidDestroyVideoSource");
  198. LOAD_SYMBOL(cuvidSetVideoSourceState, "cuvidSetVideoSourceState");
  199. LOAD_SYMBOL(cuvidGetVideoSourceState, "cuvidGetVideoSourceState");
  200. LOAD_SYMBOL(cuvidGetSourceVideoFormat, "cuvidGetSourceVideoFormat");
  201. LOAD_SYMBOL(cuvidGetSourceAudioFormat, "cuvidGetSourceAudioFormat");
  202. LOAD_SYMBOL(cuvidCreateVideoParser, "cuvidCreateVideoParser");
  203. LOAD_SYMBOL(cuvidParseVideoData, "cuvidParseVideoData");
  204. LOAD_SYMBOL(cuvidDestroyVideoParser, "cuvidDestroyVideoParser");
  205. GENERIC_LOAD_FUNC_FINALE(cuvid);
  206. }
  207. static inline int nvenc_load_functions(NvencFunctions **functions)
  208. {
  209. GENERIC_LOAD_FUNC_PREAMBLE(NvencFunctions, nvenc, NVENC_LIBNAME);
  210. LOAD_SYMBOL(NvEncodeAPICreateInstance, "NvEncodeAPICreateInstance");
  211. LOAD_SYMBOL(NvEncodeAPIGetMaxSupportedVersion, "NvEncodeAPIGetMaxSupportedVersion");
  212. GENERIC_LOAD_FUNC_FINALE(nvenc);
  213. }
  214. #undef GENERIC_LOAD_FUNC_PREAMBLE
  215. #undef LOAD_LIBRARY
  216. #undef LOAD_SYMBOL
  217. #undef GENERIC_LOAD_FUNC_FINALE
  218. #undef GENERIC_FREE_FUNC
  219. #undef CUDA_LIBNAME
  220. #undef NVCUVID_LIBNAME
  221. #undef NVENC_LIBNAME
  222. #undef LIB_HANDLE
  223. #endif