obs-ffmpeg.c 9.2 KB

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