obs-ffmpeg.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. #include <obs-module.h>
  2. #include <util/platform.h>
  3. #include <libavutil/avutil.h>
  4. #include <libavcodec/avcodec.h>
  5. #include <libavformat/avformat.h>
  6. #include "obs-ffmpeg-config.h"
  7. #ifdef _WIN32
  8. #include <dxgi.h>
  9. #include <util/dstr.h>
  10. #include <util/windows/win-version.h>
  11. #endif
  12. OBS_DECLARE_MODULE()
  13. OBS_MODULE_USE_DEFAULT_LOCALE("obs-ffmpeg", "en-US")
  14. MODULE_EXPORT const char *obs_module_description(void)
  15. {
  16. return "FFmpeg based sources/outputs/encoders";
  17. }
  18. extern struct obs_source_info ffmpeg_source;
  19. extern struct obs_output_info ffmpeg_output;
  20. extern struct obs_output_info ffmpeg_muxer;
  21. extern struct obs_output_info ffmpeg_mpegts_muxer;
  22. extern struct obs_output_info replay_buffer;
  23. extern struct obs_output_info ffmpeg_hls_muxer;
  24. extern struct obs_encoder_info aac_encoder_info;
  25. extern struct obs_encoder_info opus_encoder_info;
  26. extern struct obs_encoder_info h264_nvenc_encoder_info;
  27. #ifdef ENABLE_HEVC
  28. extern struct obs_encoder_info hevc_nvenc_encoder_info;
  29. #endif
  30. extern struct obs_encoder_info svt_av1_encoder_info;
  31. extern struct obs_encoder_info aom_av1_encoder_info;
  32. #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(55, 27, 100)
  33. #define LIBAVUTIL_VAAPI_AVAILABLE
  34. #endif
  35. #ifdef LIBAVUTIL_VAAPI_AVAILABLE
  36. extern struct obs_encoder_info vaapi_encoder_info;
  37. #endif
  38. #ifndef __APPLE__
  39. static const char *nvenc_check_name = "nvenc_check";
  40. #if defined(_WIN32) || defined(__linux__)
  41. static const int blacklisted_adapters[] = {
  42. 0x1298, // GK208M [GeForce GT 720M]
  43. 0x1140, // GF117M [GeForce 610M/710M/810M/820M / GT 620M/625M/630M/720M]
  44. 0x1293, // GK208M [GeForce GT 730M]
  45. 0x1290, // GK208M [GeForce GT 730M]
  46. 0x0fe1, // GK107M [GeForce GT 730M]
  47. 0x0fdf, // GK107M [GeForce GT 740M]
  48. 0x1294, // GK208M [GeForce GT 740M]
  49. 0x1292, // GK208M [GeForce GT 740M]
  50. 0x0fe2, // GK107M [GeForce GT 745M]
  51. 0x0fe3, // GK107M [GeForce GT 745M]
  52. 0x1140, // GF117M [GeForce 610M/710M/810M/820M / GT 620M/625M/630M/720M]
  53. 0x0fed, // GK107M [GeForce 820M]
  54. 0x1340, // GM108M [GeForce 830M]
  55. 0x1393, // GM107M [GeForce 840M]
  56. 0x1341, // GM108M [GeForce 840M]
  57. 0x1398, // GM107M [GeForce 845M]
  58. 0x1390, // GM107M [GeForce 845M]
  59. 0x1344, // GM108M [GeForce 845M]
  60. 0x1299, // GK208BM [GeForce 920M]
  61. 0x134f, // GM108M [GeForce 920MX]
  62. 0x134e, // GM108M [GeForce 930MX]
  63. 0x1349, // GM108M [GeForce 930M]
  64. 0x1346, // GM108M [GeForce 930M]
  65. 0x179c, // GM107 [GeForce 940MX]
  66. 0x139c, // GM107M [GeForce 940M]
  67. 0x1347, // GM108M [GeForce 940M]
  68. 0x134d, // GM108M [GeForce 940MX]
  69. 0x134b, // GM108M [GeForce 940MX]
  70. 0x1399, // GM107M [GeForce 945M]
  71. 0x1348, // GM108M [GeForce 945M / 945A]
  72. 0x1d01, // GP108 [GeForce GT 1030]
  73. 0x0fc5, // GK107 [GeForce GT 1030]
  74. 0x174e, // GM108M [GeForce MX110]
  75. 0x174d, // GM108M [GeForce MX130]
  76. 0x1d10, // GP108M [GeForce MX150]
  77. 0x1d12, // GP108M [GeForce MX150]
  78. 0x1d11, // GP108M [GeForce MX230]
  79. 0x1d13, // GP108M [GeForce MX250]
  80. 0x1d52, // GP108BM [GeForce MX250]
  81. 0x1c94, // GP107 [GeForce MX350]
  82. 0x1f97, // TU117 [GeForce MX450]
  83. 0x137b, // GM108GLM [Quadro M520 Mobile]
  84. 0x1d33, // GP108GLM [Quadro P500 Mobile]
  85. 0x137a, // GM108GLM [Quadro K620M / Quadro M500M]
  86. };
  87. static const size_t num_blacklisted =
  88. sizeof(blacklisted_adapters) / sizeof(blacklisted_adapters[0]);
  89. static bool is_blacklisted(const int device_id)
  90. {
  91. for (size_t i = 0; i < num_blacklisted; i++) {
  92. const int blacklisted_adapter = blacklisted_adapters[i];
  93. if (device_id == blacklisted_adapter) {
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. #endif
  100. #if defined(_WIN32)
  101. typedef HRESULT(WINAPI *create_dxgi_proc)(const IID *, IDXGIFactory1 **);
  102. static bool nvenc_device_available(void)
  103. {
  104. static HMODULE dxgi = NULL;
  105. static create_dxgi_proc create = NULL;
  106. IDXGIFactory1 *factory;
  107. IDXGIAdapter1 *adapter;
  108. bool available = false;
  109. HRESULT hr;
  110. UINT i = 0;
  111. if (!dxgi) {
  112. dxgi = GetModuleHandleW(L"dxgi");
  113. if (!dxgi) {
  114. dxgi = LoadLibraryW(L"dxgi");
  115. if (!dxgi) {
  116. return true;
  117. }
  118. }
  119. }
  120. if (!create) {
  121. create = (create_dxgi_proc)GetProcAddress(dxgi,
  122. "CreateDXGIFactory1");
  123. if (!create) {
  124. return true;
  125. }
  126. }
  127. hr = create(&IID_IDXGIFactory1, &factory);
  128. if (FAILED(hr)) {
  129. return true;
  130. }
  131. while (factory->lpVtbl->EnumAdapters1(factory, i++, &adapter) == S_OK) {
  132. DXGI_ADAPTER_DESC desc;
  133. hr = adapter->lpVtbl->GetDesc(adapter, &desc);
  134. adapter->lpVtbl->Release(adapter);
  135. if (FAILED(hr)) {
  136. continue;
  137. }
  138. // 0x10de = NVIDIA Corporation
  139. if (desc.VendorId == 0x10de && !is_blacklisted(desc.DeviceId)) {
  140. available = true;
  141. goto finish;
  142. }
  143. }
  144. finish:
  145. factory->lpVtbl->Release(factory);
  146. return available;
  147. }
  148. #endif
  149. #if defined(__linux__)
  150. static int get_id_from_sys(char *d_name, char *type)
  151. {
  152. char file_name[1024];
  153. char *c;
  154. int id;
  155. snprintf(file_name, sizeof(file_name), "/sys/bus/pci/devices/%s/%s",
  156. d_name, type);
  157. if ((c = os_quick_read_utf8_file(file_name)) == NULL) {
  158. return -1;
  159. }
  160. id = strtol(c, NULL, 16);
  161. bfree(c);
  162. return id;
  163. }
  164. static bool nvenc_device_available(void)
  165. {
  166. os_dir_t *dir;
  167. struct os_dirent *dirent;
  168. bool available = false;
  169. if ((dir = os_opendir("/sys/bus/pci/devices")) == NULL) {
  170. return true;
  171. }
  172. while ((dirent = os_readdir(dir)) != NULL) {
  173. int id;
  174. if (get_id_from_sys(dirent->d_name, "class") != 0x030000 &&
  175. get_id_from_sys(dirent->d_name, "class") !=
  176. 0x030200) { // 0x030000 = VGA compatible controller
  177. // 0x030200 = 3D controller
  178. continue;
  179. }
  180. if (get_id_from_sys(dirent->d_name, "vendor") !=
  181. 0x10de) { // 0x10de = NVIDIA Corporation
  182. continue;
  183. }
  184. if ((id = get_id_from_sys(dirent->d_name, "device")) > 0 &&
  185. !is_blacklisted(id)) {
  186. available = true;
  187. break;
  188. }
  189. }
  190. os_closedir(dir);
  191. return available;
  192. }
  193. #endif
  194. #ifdef _WIN32
  195. extern bool load_nvenc_lib(void);
  196. #endif
  197. static bool nvenc_codec_exists(const char *name, const char *fallback)
  198. {
  199. const AVCodec *nvenc = avcodec_find_encoder_by_name(name);
  200. if (!nvenc)
  201. nvenc = avcodec_find_encoder_by_name(fallback);
  202. return nvenc != NULL;
  203. }
  204. static bool nvenc_supported(bool *out_h264, bool *out_hevc)
  205. {
  206. profile_start(nvenc_check_name);
  207. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  208. av_register_all();
  209. #endif
  210. const bool h264 = nvenc_codec_exists("h264_nvenc", "nvenc_h264");
  211. #ifdef ENABLE_HEVC
  212. const bool hevc = nvenc_codec_exists("hevc_nvenc", "nvenc_hevc");
  213. #else
  214. const bool hevc = false;
  215. #endif
  216. bool success = h264 || hevc;
  217. if (success) {
  218. #if defined(_WIN32)
  219. success = nvenc_device_available() && load_nvenc_lib();
  220. #elif defined(__linux__)
  221. success = nvenc_device_available();
  222. if (success) {
  223. void *const lib = os_dlopen("libnvidia-encode.so.1");
  224. success = lib != NULL;
  225. if (success)
  226. os_dlclose(lib);
  227. }
  228. #else
  229. void *const lib = os_dlopen("libnvidia-encode.so.1");
  230. success = lib != NULL;
  231. if (success)
  232. os_dlclose(lib);
  233. #endif
  234. if (success) {
  235. *out_h264 = h264;
  236. *out_hevc = hevc;
  237. }
  238. }
  239. profile_end(nvenc_check_name);
  240. return success;
  241. }
  242. #endif
  243. #ifdef LIBAVUTIL_VAAPI_AVAILABLE
  244. static bool vaapi_supported(void)
  245. {
  246. const AVCodec *vaenc = avcodec_find_encoder_by_name("h264_vaapi");
  247. return !!vaenc;
  248. }
  249. #endif
  250. #ifdef _WIN32
  251. extern void jim_nvenc_load(bool h264, bool hevc);
  252. extern void jim_nvenc_unload(void);
  253. extern void amf_load(void);
  254. extern void amf_unload(void);
  255. #endif
  256. #if ENABLE_FFMPEG_LOGGING
  257. extern void obs_ffmpeg_load_logging(void);
  258. extern void obs_ffmpeg_unload_logging(void);
  259. #endif
  260. static void register_encoder_if_available(struct obs_encoder_info *info,
  261. const char *id)
  262. {
  263. const AVCodec *c = avcodec_find_encoder_by_name(id);
  264. if (c) {
  265. obs_register_encoder(info);
  266. }
  267. }
  268. bool obs_module_load(void)
  269. {
  270. obs_register_source(&ffmpeg_source);
  271. obs_register_output(&ffmpeg_output);
  272. obs_register_output(&ffmpeg_muxer);
  273. obs_register_output(&ffmpeg_mpegts_muxer);
  274. obs_register_output(&ffmpeg_hls_muxer);
  275. obs_register_output(&replay_buffer);
  276. obs_register_encoder(&aac_encoder_info);
  277. register_encoder_if_available(&svt_av1_encoder_info, "libsvtav1");
  278. register_encoder_if_available(&aom_av1_encoder_info, "libaom-av1");
  279. obs_register_encoder(&opus_encoder_info);
  280. #ifndef __APPLE__
  281. bool h264 = false;
  282. bool hevc = false;
  283. if (nvenc_supported(&h264, &hevc)) {
  284. blog(LOG_INFO, "NVENC supported");
  285. #ifdef _WIN32
  286. if (get_win_ver_int() > 0x0601) {
  287. jim_nvenc_load(h264, hevc);
  288. } else {
  289. // if on Win 7, new nvenc isn't available so there's
  290. // no nvenc encoder for the user to select, expose
  291. // the old encoder directly
  292. if (h264) {
  293. h264_nvenc_encoder_info.caps &=
  294. ~OBS_ENCODER_CAP_INTERNAL;
  295. }
  296. #ifdef ENABLE_HEVC
  297. if (hevc) {
  298. hevc_nvenc_encoder_info.caps &=
  299. ~OBS_ENCODER_CAP_INTERNAL;
  300. }
  301. #endif
  302. }
  303. #endif
  304. if (h264)
  305. obs_register_encoder(&h264_nvenc_encoder_info);
  306. #ifdef ENABLE_HEVC
  307. if (hevc)
  308. obs_register_encoder(&hevc_nvenc_encoder_info);
  309. #endif
  310. }
  311. #ifdef _WIN32
  312. amf_load();
  313. #endif
  314. #if !defined(_WIN32) && defined(LIBAVUTIL_VAAPI_AVAILABLE)
  315. if (vaapi_supported()) {
  316. blog(LOG_INFO, "FFMPEG VAAPI supported");
  317. obs_register_encoder(&vaapi_encoder_info);
  318. }
  319. #endif
  320. #endif
  321. #if ENABLE_FFMPEG_LOGGING
  322. obs_ffmpeg_load_logging();
  323. #endif
  324. return true;
  325. }
  326. void obs_module_unload(void)
  327. {
  328. #if ENABLE_FFMPEG_LOGGING
  329. obs_ffmpeg_unload_logging();
  330. #endif
  331. #ifdef _WIN32
  332. amf_unload();
  333. jim_nvenc_unload();
  334. #endif
  335. }