|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// This source code is licensed under the BSD-style license found in the |
| 5 | +// LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +#include "src/torchcodec/_core/NVCUVIDDynamicLoader.h" |
| 8 | + |
| 9 | +// Include NVCUVID headers to get types |
| 10 | +#include "src/torchcodec/_core/nvcuvid_include/cuviddec.h" |
| 11 | +#include "src/torchcodec/_core/nvcuvid_include/nvcuvid.h" |
| 12 | + |
| 13 | +#include <dlfcn.h> |
| 14 | +#include <torch/types.h> |
| 15 | +#include <cstdio> |
| 16 | +#include <mutex> |
| 17 | + |
| 18 | +namespace facebook::torchcodec { |
| 19 | + |
| 20 | +// Function typedefs |
| 21 | +typedef CUresult CUDAAPI |
| 22 | +tcuvidCreateVideoParser(CUvideoparser* pObj, CUVIDPARSERPARAMS* pParams); |
| 23 | +typedef CUresult CUDAAPI |
| 24 | +tcuvidParseVideoData(CUvideoparser obj, CUVIDSOURCEDATAPACKET* pPacket); |
| 25 | +typedef CUresult CUDAAPI tcuvidDestroyVideoParser(CUvideoparser obj); |
| 26 | +typedef CUresult CUDAAPI tcuvidGetDecoderCaps(CUVIDDECODECAPS* pdc); |
| 27 | +typedef CUresult CUDAAPI |
| 28 | +tcuvidCreateDecoder(CUvideodecoder* phDecoder, CUVIDDECODECREATEINFO* pdci); |
| 29 | +typedef CUresult CUDAAPI tcuvidDestroyDecoder(CUvideodecoder hDecoder); |
| 30 | +typedef CUresult CUDAAPI |
| 31 | +tcuvidDecodePicture(CUvideodecoder hDecoder, CUVIDPICPARAMS* pPicParams); |
| 32 | +typedef CUresult CUDAAPI tcuvidMapVideoFrame64( |
| 33 | + CUvideodecoder hDecoder, |
| 34 | + int nPicIdx, |
| 35 | + unsigned long long* pDevPtr, |
| 36 | + unsigned int* pPitch, |
| 37 | + CUVIDPROCPARAMS* pVPP); |
| 38 | +typedef CUresult CUDAAPI |
| 39 | +tcuvidUnmapVideoFrame64(CUvideodecoder hDecoder, unsigned long long DevPtr); |
| 40 | + |
| 41 | +// Global function pointers |
| 42 | +static tcuvidCreateVideoParser* _dlcuvidCreateVideoParser = nullptr; |
| 43 | +static tcuvidParseVideoData* _dlcuvidParseVideoData = nullptr; |
| 44 | +static tcuvidDestroyVideoParser* _dlcuvidDestroyVideoParser = nullptr; |
| 45 | +static tcuvidGetDecoderCaps* _dlcuvidGetDecoderCaps = nullptr; |
| 46 | +static tcuvidCreateDecoder* _dlcuvidCreateDecoder = nullptr; |
| 47 | +static tcuvidDestroyDecoder* _dlcuvidDestroyDecoder = nullptr; |
| 48 | +static tcuvidDecodePicture* _dlcuvidDecodePicture = nullptr; |
| 49 | +static tcuvidMapVideoFrame64* _dlcuvidMapVideoFrame64 = nullptr; |
| 50 | +static tcuvidUnmapVideoFrame64* _dlcuvidUnmapVideoFrame64 = nullptr; |
| 51 | + |
| 52 | +static void* g_nvcuvid_handle = nullptr; |
| 53 | +static std::mutex g_nvcuvid_mutex; |
| 54 | + |
| 55 | +template <typename T> |
| 56 | +T* loadFunction(const char* functionName) { |
| 57 | + return reinterpret_cast<T*>(dlsym(g_nvcuvid_handle, functionName)); |
| 58 | +} |
| 59 | + |
| 60 | +bool initNVCUVID() { |
| 61 | + std::lock_guard<std::mutex> lock(g_nvcuvid_mutex); |
| 62 | + |
| 63 | + if (g_nvcuvid_handle != nullptr) { |
| 64 | + return true; // Already loaded |
| 65 | + } |
| 66 | + |
| 67 | + g_nvcuvid_handle = dlopen("libnvcuvid.so", RTLD_NOW); |
| 68 | + if (g_nvcuvid_handle == nullptr) { |
| 69 | + g_nvcuvid_handle = dlopen("libnvcuvid.so.1", RTLD_NOW); |
| 70 | + if (g_nvcuvid_handle == nullptr) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Load all function pointers |
| 76 | + _dlcuvidCreateVideoParser = |
| 77 | + loadFunction<tcuvidCreateVideoParser>("cuvidCreateVideoParser"); |
| 78 | + _dlcuvidParseVideoData = |
| 79 | + loadFunction<tcuvidParseVideoData>("cuvidParseVideoData"); |
| 80 | + _dlcuvidDestroyVideoParser = |
| 81 | + loadFunction<tcuvidDestroyVideoParser>("cuvidDestroyVideoParser"); |
| 82 | + _dlcuvidGetDecoderCaps = |
| 83 | + loadFunction<tcuvidGetDecoderCaps>("cuvidGetDecoderCaps"); |
| 84 | + _dlcuvidCreateDecoder = |
| 85 | + loadFunction<tcuvidCreateDecoder>("cuvidCreateDecoder"); |
| 86 | + _dlcuvidDestroyDecoder = |
| 87 | + loadFunction<tcuvidDestroyDecoder>("cuvidDestroyDecoder"); |
| 88 | + _dlcuvidDecodePicture = |
| 89 | + loadFunction<tcuvidDecodePicture>("cuvidDecodePicture"); |
| 90 | + _dlcuvidMapVideoFrame64 = |
| 91 | + loadFunction<tcuvidMapVideoFrame64>("cuvidMapVideoFrame64"); |
| 92 | + _dlcuvidUnmapVideoFrame64 = |
| 93 | + loadFunction<tcuvidUnmapVideoFrame64>("cuvidUnmapVideoFrame64"); |
| 94 | + |
| 95 | + // Check if all critical functions loaded successfully |
| 96 | + return (_dlcuvidCreateVideoParser && _dlcuvidParseVideoData && |
| 97 | + _dlcuvidDestroyVideoParser && _dlcuvidGetDecoderCaps && |
| 98 | + _dlcuvidCreateDecoder && _dlcuvidDestroyDecoder && |
| 99 | + _dlcuvidDecodePicture && _dlcuvidMapVideoFrame64 && |
| 100 | + _dlcuvidUnmapVideoFrame64); |
| 101 | +} |
| 102 | + |
| 103 | +bool isNVCUVIDLoaded() { |
| 104 | + return g_nvcuvid_handle != nullptr && _dlcuvidCreateDecoder != nullptr; |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +} // namespace facebook::torchcodec |
| 109 | + |
| 110 | +// Provide C-style wrapper functions that replace the original NVCUVID functions |
| 111 | +extern "C" { |
| 112 | + |
| 113 | +CUresult CUDAAPI |
| 114 | +cuvidCreateVideoParser(CUvideoparser* pObj, CUVIDPARSERPARAMS* pParams) { |
| 115 | + TORCH_CHECK( |
| 116 | + facebook::torchcodec::_dlcuvidCreateVideoParser, |
| 117 | + "cuvidCreateVideoParser called but NVCUVID not loaded!"); |
| 118 | + return facebook::torchcodec::_dlcuvidCreateVideoParser(pObj, pParams); |
| 119 | +} |
| 120 | + |
| 121 | +CUresult CUDAAPI |
| 122 | +cuvidParseVideoData(CUvideoparser obj, CUVIDSOURCEDATAPACKET* pPacket) { |
| 123 | + TORCH_CHECK( |
| 124 | + facebook::torchcodec::_dlcuvidParseVideoData, |
| 125 | + "cuvidParseVideoData called but NVCUVID not loaded!"); |
| 126 | + return facebook::torchcodec::_dlcuvidParseVideoData(obj, pPacket); |
| 127 | +} |
| 128 | + |
| 129 | +CUresult CUDAAPI cuvidDestroyVideoParser(CUvideoparser obj) { |
| 130 | + TORCH_CHECK( |
| 131 | + facebook::torchcodec::_dlcuvidDestroyVideoParser, |
| 132 | + "cuvidDestroyVideoParser called but NVCUVID not loaded!"); |
| 133 | + return facebook::torchcodec::_dlcuvidDestroyVideoParser(obj); |
| 134 | +} |
| 135 | + |
| 136 | +CUresult CUDAAPI cuvidGetDecoderCaps(CUVIDDECODECAPS* pdc) { |
| 137 | + TORCH_CHECK( |
| 138 | + facebook::torchcodec::_dlcuvidGetDecoderCaps, |
| 139 | + "cuvidGetDecoderCaps called but NVCUVID not loaded!"); |
| 140 | + return facebook::torchcodec::_dlcuvidGetDecoderCaps(pdc); |
| 141 | +} |
| 142 | + |
| 143 | +CUresult CUDAAPI |
| 144 | +cuvidCreateDecoder(CUvideodecoder* phDecoder, CUVIDDECODECREATEINFO* pdci) { |
| 145 | + TORCH_CHECK( |
| 146 | + facebook::torchcodec::_dlcuvidCreateDecoder, |
| 147 | + "cuvidCreateDecoder called but NVCUVID not loaded!"); |
| 148 | + return facebook::torchcodec::_dlcuvidCreateDecoder(phDecoder, pdci); |
| 149 | +} |
| 150 | + |
| 151 | +CUresult CUDAAPI cuvidDestroyDecoder(CUvideodecoder hDecoder) { |
| 152 | + TORCH_CHECK( |
| 153 | + facebook::torchcodec::_dlcuvidDestroyDecoder, |
| 154 | + "cuvidDestroyDecoder called but NVCUVID not loaded!"); |
| 155 | + return facebook::torchcodec::_dlcuvidDestroyDecoder(hDecoder); |
| 156 | +} |
| 157 | + |
| 158 | +CUresult CUDAAPI |
| 159 | +cuvidDecodePicture(CUvideodecoder hDecoder, CUVIDPICPARAMS* pPicParams) { |
| 160 | + TORCH_CHECK( |
| 161 | + facebook::torchcodec::_dlcuvidDecodePicture, |
| 162 | + "cuvidDecodePicture called but NVCUVID not loaded!"); |
| 163 | + return facebook::torchcodec::_dlcuvidDecodePicture(hDecoder, pPicParams); |
| 164 | +} |
| 165 | + |
| 166 | +CUresult CUDAAPI cuvidMapVideoFrame64( |
| 167 | + CUvideodecoder hDecoder, |
| 168 | + int nPicIdx, |
| 169 | + unsigned long long* pDevPtr, |
| 170 | + unsigned int* pPitch, |
| 171 | + CUVIDPROCPARAMS* pVPP) { |
| 172 | + TORCH_CHECK( |
| 173 | + facebook::torchcodec::_dlcuvidMapVideoFrame64, |
| 174 | + "cuvidMapVideoFrame64 called but NVCUVID not loaded!"); |
| 175 | + return facebook::torchcodec::_dlcuvidMapVideoFrame64( |
| 176 | + hDecoder, nPicIdx, pDevPtr, pPitch, pVPP); |
| 177 | +} |
| 178 | + |
| 179 | +CUresult CUDAAPI |
| 180 | +cuvidUnmapVideoFrame64(CUvideodecoder hDecoder, unsigned long long DevPtr) { |
| 181 | + TORCH_CHECK( |
| 182 | + facebook::torchcodec::_dlcuvidUnmapVideoFrame64, |
| 183 | + "cuvidUnmapVideoFrame64 called but NVCUVID not loaded!"); |
| 184 | + return facebook::torchcodec::_dlcuvidUnmapVideoFrame64(hDecoder, DevPtr); |
| 185 | +} |
| 186 | + |
| 187 | +} // extern "C" |
0 commit comments