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.

255 lines
9.1KB

  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. tcuvidCreateDecoder *cuvidCreateDecoder;
  116. tcuvidDestroyDecoder *cuvidDestroyDecoder;
  117. tcuvidDecodePicture *cuvidDecodePicture;
  118. tcuvidMapVideoFrame *cuvidMapVideoFrame;
  119. tcuvidUnmapVideoFrame *cuvidUnmapVideoFrame;
  120. tcuvidCtxLockCreate *cuvidCtxLockCreate;
  121. tcuvidCtxLockDestroy *cuvidCtxLockDestroy;
  122. tcuvidCtxLock *cuvidCtxLock;
  123. tcuvidCtxUnlock *cuvidCtxUnlock;
  124. tcuvidCreateVideoSource *cuvidCreateVideoSource;
  125. tcuvidCreateVideoSourceW *cuvidCreateVideoSourceW;
  126. tcuvidDestroyVideoSource *cuvidDestroyVideoSource;
  127. tcuvidSetVideoSourceState *cuvidSetVideoSourceState;
  128. tcuvidGetVideoSourceState *cuvidGetVideoSourceState;
  129. tcuvidGetSourceVideoFormat *cuvidGetSourceVideoFormat;
  130. tcuvidGetSourceAudioFormat *cuvidGetSourceAudioFormat;
  131. tcuvidCreateVideoParser *cuvidCreateVideoParser;
  132. tcuvidParseVideoData *cuvidParseVideoData;
  133. tcuvidDestroyVideoParser *cuvidDestroyVideoParser;
  134. LIB_HANDLE lib;
  135. } CuvidFunctions;
  136. typedef struct NvencFunctions {
  137. NVENCSTATUS (NVENCAPI *NvEncodeAPICreateInstance)(NV_ENCODE_API_FUNCTION_LIST *functionList);
  138. NVENCSTATUS (NVENCAPI *NvEncodeAPIGetMaxSupportedVersion)(uint32_t* version);
  139. LIB_HANDLE lib;
  140. } NvencFunctions;
  141. #ifdef AV_COMPAT_DYNLINK_CUDA_H
  142. static inline void cuda_free_functions(CudaFunctions **functions)
  143. {
  144. GENERIC_FREE_FUNC();
  145. }
  146. #endif
  147. static inline void cuvid_free_functions(CuvidFunctions **functions)
  148. {
  149. GENERIC_FREE_FUNC();
  150. }
  151. static inline void nvenc_free_functions(NvencFunctions **functions)
  152. {
  153. GENERIC_FREE_FUNC();
  154. }
  155. #ifdef AV_COMPAT_DYNLINK_CUDA_H
  156. static inline int cuda_load_functions(CudaFunctions **functions)
  157. {
  158. GENERIC_LOAD_FUNC_PREAMBLE(CudaFunctions, cuda, CUDA_LIBNAME);
  159. LOAD_SYMBOL(cuInit, "cuInit");
  160. LOAD_SYMBOL(cuDeviceGetCount, "cuDeviceGetCount");
  161. LOAD_SYMBOL(cuDeviceGet, "cuDeviceGet");
  162. LOAD_SYMBOL(cuDeviceGetName, "cuDeviceGetName");
  163. LOAD_SYMBOL(cuDeviceComputeCapability, "cuDeviceComputeCapability");
  164. LOAD_SYMBOL(cuCtxCreate, "cuCtxCreate_v2");
  165. LOAD_SYMBOL(cuCtxPushCurrent, "cuCtxPushCurrent_v2");
  166. LOAD_SYMBOL(cuCtxPopCurrent, "cuCtxPopCurrent_v2");
  167. LOAD_SYMBOL(cuCtxDestroy, "cuCtxDestroy_v2");
  168. LOAD_SYMBOL(cuMemAlloc, "cuMemAlloc_v2");
  169. LOAD_SYMBOL(cuMemFree, "cuMemFree_v2");
  170. LOAD_SYMBOL(cuMemcpy2D, "cuMemcpy2D_v2");
  171. LOAD_SYMBOL(cuGetErrorName, "cuGetErrorName");
  172. LOAD_SYMBOL(cuGetErrorString, "cuGetErrorString");
  173. GENERIC_LOAD_FUNC_FINALE(cuda);
  174. }
  175. #endif
  176. static inline int cuvid_load_functions(CuvidFunctions **functions)
  177. {
  178. GENERIC_LOAD_FUNC_PREAMBLE(CuvidFunctions, cuvid, NVCUVID_LIBNAME);
  179. LOAD_SYMBOL(cuvidCreateDecoder, "cuvidCreateDecoder");
  180. LOAD_SYMBOL(cuvidDestroyDecoder, "cuvidDestroyDecoder");
  181. LOAD_SYMBOL(cuvidDecodePicture, "cuvidDecodePicture");
  182. #ifdef __CUVID_DEVPTR64
  183. LOAD_SYMBOL(cuvidMapVideoFrame, "cuvidMapVideoFrame64");
  184. LOAD_SYMBOL(cuvidUnmapVideoFrame, "cuvidUnmapVideoFrame64");
  185. #else
  186. LOAD_SYMBOL(cuvidMapVideoFrame, "cuvidMapVideoFrame");
  187. LOAD_SYMBOL(cuvidUnmapVideoFrame, "cuvidUnmapVideoFrame");
  188. #endif
  189. LOAD_SYMBOL(cuvidCtxLockCreate, "cuvidCtxLockCreate");
  190. LOAD_SYMBOL(cuvidCtxLockDestroy, "cuvidCtxLockDestroy");
  191. LOAD_SYMBOL(cuvidCtxLock, "cuvidCtxLock");
  192. LOAD_SYMBOL(cuvidCtxUnlock, "cuvidCtxUnlock");
  193. LOAD_SYMBOL(cuvidCreateVideoSource, "cuvidCreateVideoSource");
  194. LOAD_SYMBOL(cuvidCreateVideoSourceW, "cuvidCreateVideoSourceW");
  195. LOAD_SYMBOL(cuvidDestroyVideoSource, "cuvidDestroyVideoSource");
  196. LOAD_SYMBOL(cuvidSetVideoSourceState, "cuvidSetVideoSourceState");
  197. LOAD_SYMBOL(cuvidGetVideoSourceState, "cuvidGetVideoSourceState");
  198. LOAD_SYMBOL(cuvidGetSourceVideoFormat, "cuvidGetSourceVideoFormat");
  199. LOAD_SYMBOL(cuvidGetSourceAudioFormat, "cuvidGetSourceAudioFormat");
  200. LOAD_SYMBOL(cuvidCreateVideoParser, "cuvidCreateVideoParser");
  201. LOAD_SYMBOL(cuvidParseVideoData, "cuvidParseVideoData");
  202. LOAD_SYMBOL(cuvidDestroyVideoParser, "cuvidDestroyVideoParser");
  203. GENERIC_LOAD_FUNC_FINALE(cuvid);
  204. }
  205. static inline int nvenc_load_functions(NvencFunctions **functions)
  206. {
  207. GENERIC_LOAD_FUNC_PREAMBLE(NvencFunctions, nvenc, NVENC_LIBNAME);
  208. LOAD_SYMBOL(NvEncodeAPICreateInstance, "NvEncodeAPICreateInstance");
  209. LOAD_SYMBOL(NvEncodeAPIGetMaxSupportedVersion, "NvEncodeAPIGetMaxSupportedVersion");
  210. GENERIC_LOAD_FUNC_FINALE(nvenc);
  211. }
  212. #undef GENERIC_LOAD_FUNC_PREAMBLE
  213. #undef LOAD_LIBRARY
  214. #undef LOAD_SYMBOL
  215. #undef GENERIC_LOAD_FUNC_FINALE
  216. #undef GENERIC_FREE_FUNC
  217. #undef CUDA_LIBNAME
  218. #undef NVCUVID_LIBNAME
  219. #undef NVENC_LIBNAME
  220. #undef LIB_HANDLE
  221. #endif