obs-ffmpeg.c 11 KB

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