LCOV - code coverage report
Current view: top level - src/compat/cuda - dynlink_loader.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 51 0.0 %
Date: 2017-05-29 12:59:06 Functions: 0 6 0.0 %

          Line data    Source code
       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             : 
      28             : #ifndef AV_COMPAT_CUDA_DYNLINK_LOADER_H
      29             : #define AV_COMPAT_CUDA_DYNLINK_LOADER_H
      30             : 
      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"
      35             : 
      36             : #include "libavutil/log.h"
      37             : #include "libavutil/error.h"
      38             : 
      39             : #if defined(_WIN32)
      40             : # define LIB_HANDLE HMODULE
      41             : #else
      42             : # define LIB_HANDLE void*
      43             : #endif
      44             : 
      45             : #if defined(_WIN32) || defined(__CYGWIN__)
      46             : # define CUDA_LIBNAME "nvcuda.dll"
      47             : # define NVCUVID_LIBNAME "nvcuvid.dll"
      48             : # if ARCH_X86_64
      49             : #  define NVENC_LIBNAME "nvEncodeAPI64.dll"
      50             : # else
      51             : #  define NVENC_LIBNAME "nvEncodeAPI.dll"
      52             : # endif
      53             : #else
      54             : # define CUDA_LIBNAME "libcuda.so.1"
      55             : # define NVCUVID_LIBNAME "libnvcuvid.so.1"
      56             : # define NVENC_LIBNAME "libnvidia-encode.so.1"
      57             : #endif
      58             : 
      59             : #define LOAD_LIBRARY(l, path)                                     \
      60             :     do {                                                          \
      61             :         if (!((l) = dlopen(path, RTLD_LAZY))) {                   \
      62             :             av_log(NULL, AV_LOG_ERROR, "Cannot load %s\n", path); \
      63             :             ret = AVERROR_UNKNOWN;                                \
      64             :             goto error;                                           \
      65             :         }                                                         \
      66             :         av_log(NULL, AV_LOG_TRACE, "Loaded lib: %s\n", path);     \
      67             :     } while (0)
      68             : 
      69             : #define LOAD_SYMBOL(fun, tp, symbol)                                \
      70             :     do {                                                            \
      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;                                  \
      74             :             goto error;                                             \
      75             :         }                                                           \
      76             :         av_log(NULL, AV_LOG_TRACE, "Loaded sym: %s\n", symbol);     \
      77             :     } while (0)
      78             : 
      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             : 
      91             : #define GENERIC_LOAD_FUNC_FINALE(n) \
      92             :     return 0;                       \
      93             : error:                              \
      94             :     n##_free_functions(functions);  \
      95             :     return ret;
      96             : 
      97             : #define GENERIC_FREE_FUNC()              \
      98             :     if (!functions)                      \
      99             :         return;                          \
     100             :     if (*functions && (*functions)->lib) \
     101             :         dlclose((*functions)->lib);      \
     102             :     av_freep(functions);
     103             : 
     104             : #ifdef AV_COMPAT_DYNLINK_CUDA_H
     105             : typedef struct CudaFunctions {
     106             :     tcuInit *cuInit;
     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;
     120             : 
     121             :     LIB_HANDLE lib;
     122             : } CudaFunctions;
     123             : #else
     124             : typedef struct CudaFunctions CudaFunctions;
     125             : #endif
     126             : 
     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;
     138             : 
     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;
     149             : 
     150             :     LIB_HANDLE lib;
     151             : } CuvidFunctions;
     152             : 
     153             : typedef NVENCSTATUS NVENCAPI tNvEncodeAPICreateInstance(NV_ENCODE_API_FUNCTION_LIST *functionList);
     154             : typedef NVENCSTATUS NVENCAPI tNvEncodeAPIGetMaxSupportedVersion(uint32_t* version);
     155             : 
     156             : typedef struct NvencFunctions {
     157             :     tNvEncodeAPICreateInstance *NvEncodeAPICreateInstance;
     158             :     tNvEncodeAPIGetMaxSupportedVersion *NvEncodeAPIGetMaxSupportedVersion;
     159             : 
     160             :     LIB_HANDLE lib;
     161             : } NvencFunctions;
     162             : 
     163             : #ifdef AV_COMPAT_DYNLINK_CUDA_H
     164           0 : static inline void cuda_free_functions(CudaFunctions **functions)
     165             : {
     166           0 :     GENERIC_FREE_FUNC();
     167             : }
     168             : #endif
     169             : 
     170           0 : static inline void cuvid_free_functions(CuvidFunctions **functions)
     171             : {
     172           0 :     GENERIC_FREE_FUNC();
     173             : }
     174             : 
     175           0 : static inline void nvenc_free_functions(NvencFunctions **functions)
     176             : {
     177           0 :     GENERIC_FREE_FUNC();
     178             : }
     179             : 
     180             : #ifdef AV_COMPAT_DYNLINK_CUDA_H
     181           0 : static inline int cuda_load_functions(CudaFunctions **functions)
     182             : {
     183           0 :     GENERIC_LOAD_FUNC_PREAMBLE(CudaFunctions, cuda, CUDA_LIBNAME);
     184             : 
     185           0 :     LOAD_SYMBOL(cuInit, tcuInit, "cuInit");
     186           0 :     LOAD_SYMBOL(cuDeviceGetCount, tcuDeviceGetCount, "cuDeviceGetCount");
     187           0 :     LOAD_SYMBOL(cuDeviceGet, tcuDeviceGet, "cuDeviceGet");
     188           0 :     LOAD_SYMBOL(cuDeviceGetName, tcuDeviceGetName, "cuDeviceGetName");
     189           0 :     LOAD_SYMBOL(cuDeviceComputeCapability, tcuDeviceComputeCapability, "cuDeviceComputeCapability");
     190           0 :     LOAD_SYMBOL(cuCtxCreate, tcuCtxCreate_v2, "cuCtxCreate_v2");
     191           0 :     LOAD_SYMBOL(cuCtxPushCurrent, tcuCtxPushCurrent_v2, "cuCtxPushCurrent_v2");
     192           0 :     LOAD_SYMBOL(cuCtxPopCurrent, tcuCtxPopCurrent_v2, "cuCtxPopCurrent_v2");
     193           0 :     LOAD_SYMBOL(cuCtxDestroy, tcuCtxDestroy_v2, "cuCtxDestroy_v2");
     194           0 :     LOAD_SYMBOL(cuMemAlloc, tcuMemAlloc_v2, "cuMemAlloc_v2");
     195           0 :     LOAD_SYMBOL(cuMemFree, tcuMemFree_v2, "cuMemFree_v2");
     196           0 :     LOAD_SYMBOL(cuMemcpy2D, tcuMemcpy2D_v2, "cuMemcpy2D_v2");
     197           0 :     LOAD_SYMBOL(cuGetErrorName, tcuGetErrorName, "cuGetErrorName");
     198           0 :     LOAD_SYMBOL(cuGetErrorString, tcuGetErrorString, "cuGetErrorString");
     199             : 
     200           0 :     GENERIC_LOAD_FUNC_FINALE(cuda);
     201             : }
     202             : #endif
     203             : 
     204           0 : static inline int cuvid_load_functions(CuvidFunctions **functions)
     205             : {
     206           0 :     GENERIC_LOAD_FUNC_PREAMBLE(CuvidFunctions, cuvid, NVCUVID_LIBNAME);
     207             : 
     208           0 :     LOAD_SYMBOL(cuvidGetDecoderCaps, tcuvidGetDecoderCaps, "cuvidGetDecoderCaps");
     209           0 :     LOAD_SYMBOL(cuvidCreateDecoder, tcuvidCreateDecoder, "cuvidCreateDecoder");
     210           0 :     LOAD_SYMBOL(cuvidDestroyDecoder, tcuvidDestroyDecoder, "cuvidDestroyDecoder");
     211           0 :     LOAD_SYMBOL(cuvidDecodePicture, tcuvidDecodePicture, "cuvidDecodePicture");
     212             : #ifdef __CUVID_DEVPTR64
     213           0 :     LOAD_SYMBOL(cuvidMapVideoFrame, tcuvidMapVideoFrame, "cuvidMapVideoFrame64");
     214           0 :     LOAD_SYMBOL(cuvidUnmapVideoFrame, tcuvidUnmapVideoFrame, "cuvidUnmapVideoFrame64");
     215             : #else
     216             :     LOAD_SYMBOL(cuvidMapVideoFrame, tcuvidMapVideoFrame, "cuvidMapVideoFrame");
     217             :     LOAD_SYMBOL(cuvidUnmapVideoFrame, tcuvidUnmapVideoFrame, "cuvidUnmapVideoFrame");
     218             : #endif
     219           0 :     LOAD_SYMBOL(cuvidCtxLockCreate, tcuvidCtxLockCreate, "cuvidCtxLockCreate");
     220           0 :     LOAD_SYMBOL(cuvidCtxLockDestroy, tcuvidCtxLockDestroy, "cuvidCtxLockDestroy");
     221           0 :     LOAD_SYMBOL(cuvidCtxLock, tcuvidCtxLock, "cuvidCtxLock");
     222           0 :     LOAD_SYMBOL(cuvidCtxUnlock, tcuvidCtxUnlock, "cuvidCtxUnlock");
     223             : 
     224           0 :     LOAD_SYMBOL(cuvidCreateVideoSource, tcuvidCreateVideoSource, "cuvidCreateVideoSource");
     225           0 :     LOAD_SYMBOL(cuvidCreateVideoSourceW, tcuvidCreateVideoSourceW, "cuvidCreateVideoSourceW");
     226           0 :     LOAD_SYMBOL(cuvidDestroyVideoSource, tcuvidDestroyVideoSource, "cuvidDestroyVideoSource");
     227           0 :     LOAD_SYMBOL(cuvidSetVideoSourceState, tcuvidSetVideoSourceState, "cuvidSetVideoSourceState");
     228           0 :     LOAD_SYMBOL(cuvidGetVideoSourceState, tcuvidGetVideoSourceState, "cuvidGetVideoSourceState");
     229           0 :     LOAD_SYMBOL(cuvidGetSourceVideoFormat, tcuvidGetSourceVideoFormat, "cuvidGetSourceVideoFormat");
     230           0 :     LOAD_SYMBOL(cuvidGetSourceAudioFormat, tcuvidGetSourceAudioFormat, "cuvidGetSourceAudioFormat");
     231           0 :     LOAD_SYMBOL(cuvidCreateVideoParser, tcuvidCreateVideoParser, "cuvidCreateVideoParser");
     232           0 :     LOAD_SYMBOL(cuvidParseVideoData, tcuvidParseVideoData, "cuvidParseVideoData");
     233           0 :     LOAD_SYMBOL(cuvidDestroyVideoParser, tcuvidDestroyVideoParser, "cuvidDestroyVideoParser");
     234             : 
     235           0 :     GENERIC_LOAD_FUNC_FINALE(cuvid);
     236             : }
     237             : 
     238           0 : static inline int nvenc_load_functions(NvencFunctions **functions)
     239             : {
     240           0 :     GENERIC_LOAD_FUNC_PREAMBLE(NvencFunctions, nvenc, NVENC_LIBNAME);
     241             : 
     242           0 :     LOAD_SYMBOL(NvEncodeAPICreateInstance, tNvEncodeAPICreateInstance, "NvEncodeAPICreateInstance");
     243           0 :     LOAD_SYMBOL(NvEncodeAPIGetMaxSupportedVersion, tNvEncodeAPIGetMaxSupportedVersion, "NvEncodeAPIGetMaxSupportedVersion");
     244             : 
     245           0 :     GENERIC_LOAD_FUNC_FINALE(nvenc);
     246             : }
     247             : 
     248             : #undef GENERIC_LOAD_FUNC_PREAMBLE
     249             : #undef LOAD_LIBRARY
     250             : #undef LOAD_SYMBOL
     251             : #undef GENERIC_LOAD_FUNC_FINALE
     252             : #undef GENERIC_FREE_FUNC
     253             : #undef CUDA_LIBNAME
     254             : #undef NVCUVID_LIBNAME
     255             : #undef NVENC_LIBNAME
     256             : #undef LIB_HANDLE
     257             : 
     258             : #endif
     259             : 

Generated by: LCOV version 1.13