obs-ffmpeg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. #include <util/dstr.h>
  2. #include <obs-module.h>
  3. #include <util/platform.h>
  4. #include <libavutil/avutil.h>
  5. #include <libavcodec/avcodec.h>
  6. #include <libavformat/avformat.h>
  7. #ifdef _WIN32
  8. #include <dxgi.h>
  9. #include <util/windows/win-version.h>
  10. #include "obs-nvenc.h"
  11. #endif
  12. #if !defined(_WIN32) && !defined(__APPLE__)
  13. #include "vaapi-utils.h"
  14. #define LIBAVUTIL_VAAPI_AVAILABLE
  15. #endif
  16. OBS_DECLARE_MODULE()
  17. OBS_MODULE_USE_DEFAULT_LOCALE("obs-ffmpeg", "en-US")
  18. MODULE_EXPORT const char *obs_module_description(void)
  19. {
  20. return "FFmpeg based sources/outputs/encoders";
  21. }
  22. extern struct obs_source_info ffmpeg_source;
  23. extern struct obs_output_info ffmpeg_output;
  24. extern struct obs_output_info ffmpeg_muxer;
  25. extern struct obs_output_info ffmpeg_mpegts_muxer;
  26. extern struct obs_output_info replay_buffer;
  27. extern struct obs_output_info ffmpeg_hls_muxer;
  28. extern struct obs_encoder_info aac_encoder_info;
  29. extern struct obs_encoder_info opus_encoder_info;
  30. extern struct obs_encoder_info pcm_encoder_info;
  31. extern struct obs_encoder_info pcm24_encoder_info;
  32. extern struct obs_encoder_info pcm32_encoder_info;
  33. extern struct obs_encoder_info alac_encoder_info;
  34. extern struct obs_encoder_info flac_encoder_info;
  35. extern struct obs_encoder_info h264_nvenc_encoder_info;
  36. #ifdef ENABLE_HEVC
  37. extern struct obs_encoder_info hevc_nvenc_encoder_info;
  38. #endif
  39. extern struct obs_encoder_info svt_av1_encoder_info;
  40. extern struct obs_encoder_info aom_av1_encoder_info;
  41. #ifdef LIBAVUTIL_VAAPI_AVAILABLE
  42. extern struct obs_encoder_info h264_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. extern uint32_t get_nvenc_ver();
  208. #endif
  209. /* please remove this annoying garbage and the associated garbage in
  210. * obs-ffmpeg-nvenc.c when ubuntu 20.04 is finally gone for good. */
  211. #ifdef __linux__
  212. bool ubuntu_20_04_nvenc_fallback = false;
  213. static void do_nvenc_check_for_ubuntu_20_04(void)
  214. {
  215. FILE *fp;
  216. char *line = NULL;
  217. size_t linecap = 0;
  218. fp = fopen("/etc/os-release", "r");
  219. if (!fp) {
  220. return;
  221. }
  222. while (getline(&line, &linecap, fp) != -1) {
  223. if (strncmp(line, "VERSION_CODENAME=focal", 22) == 0) {
  224. ubuntu_20_04_nvenc_fallback = true;
  225. }
  226. }
  227. fclose(fp);
  228. free(line);
  229. }
  230. #endif
  231. static bool nvenc_codec_exists(const char *name, const char *fallback)
  232. {
  233. const AVCodec *nvenc = avcodec_find_encoder_by_name(name);
  234. if (!nvenc)
  235. nvenc = avcodec_find_encoder_by_name(fallback);
  236. return nvenc != NULL;
  237. }
  238. static bool nvenc_supported(bool *out_h264, bool *out_hevc, bool *out_av1)
  239. {
  240. profile_start(nvenc_check_name);
  241. const bool h264 = nvenc_codec_exists("h264_nvenc", "nvenc_h264");
  242. #ifdef ENABLE_HEVC
  243. const bool hevc = nvenc_codec_exists("hevc_nvenc", "nvenc_hevc");
  244. #else
  245. const bool hevc = false;
  246. #endif
  247. bool av1 = false;
  248. bool success = h264 || hevc;
  249. if (success) {
  250. #if defined(_WIN32)
  251. success = nvenc_device_available() && load_nvenc_lib();
  252. av1 = success && (get_nvenc_ver() >= ((12 << 4) | 0));
  253. #elif defined(__linux__)
  254. success = nvenc_device_available();
  255. if (success) {
  256. void *const lib = os_dlopen("libnvidia-encode.so.1");
  257. success = lib != NULL;
  258. if (success)
  259. os_dlclose(lib);
  260. }
  261. #else
  262. void *const lib = os_dlopen("libnvidia-encode.so.1");
  263. success = lib != NULL;
  264. if (success)
  265. os_dlclose(lib);
  266. #endif
  267. if (success) {
  268. *out_h264 = h264;
  269. *out_hevc = hevc;
  270. *out_av1 = av1;
  271. }
  272. }
  273. profile_end(nvenc_check_name);
  274. return success;
  275. }
  276. #endif
  277. #ifdef LIBAVUTIL_VAAPI_AVAILABLE
  278. static bool h264_vaapi_supported(void)
  279. {
  280. const AVCodec *vaenc = avcodec_find_encoder_by_name("h264_vaapi");
  281. if (!vaenc)
  282. return false;
  283. /* NOTE: If default device is NULL, it means there is no device
  284. * that support H264. */
  285. return vaapi_get_h264_default_device() != NULL;
  286. }
  287. #ifdef ENABLE_HEVC
  288. static bool hevc_vaapi_supported(void)
  289. {
  290. const AVCodec *vaenc = avcodec_find_encoder_by_name("hevc_vaapi");
  291. if (!vaenc)
  292. return false;
  293. /* NOTE: If default device is NULL, it means there is no device
  294. * that support HEVC. */
  295. return vaapi_get_hevc_default_device() != NULL;
  296. }
  297. #endif
  298. #endif
  299. #ifdef _WIN32
  300. extern void obs_nvenc_load(bool h264, bool hevc, bool av1);
  301. extern void obs_nvenc_unload(void);
  302. extern void amf_load(void);
  303. extern void amf_unload(void);
  304. #endif
  305. #if ENABLE_FFMPEG_LOGGING
  306. extern void obs_ffmpeg_load_logging(void);
  307. extern void obs_ffmpeg_unload_logging(void);
  308. #endif
  309. static void register_encoder_if_available(struct obs_encoder_info *info,
  310. const char *id)
  311. {
  312. const AVCodec *c = avcodec_find_encoder_by_name(id);
  313. if (c) {
  314. obs_register_encoder(info);
  315. }
  316. }
  317. bool obs_module_load(void)
  318. {
  319. obs_register_source(&ffmpeg_source);
  320. obs_register_output(&ffmpeg_output);
  321. obs_register_output(&ffmpeg_muxer);
  322. obs_register_output(&ffmpeg_mpegts_muxer);
  323. obs_register_output(&ffmpeg_hls_muxer);
  324. obs_register_output(&replay_buffer);
  325. obs_register_encoder(&aac_encoder_info);
  326. register_encoder_if_available(&svt_av1_encoder_info, "libsvtav1");
  327. register_encoder_if_available(&aom_av1_encoder_info, "libaom-av1");
  328. obs_register_encoder(&opus_encoder_info);
  329. obs_register_encoder(&pcm_encoder_info);
  330. obs_register_encoder(&pcm24_encoder_info);
  331. obs_register_encoder(&pcm32_encoder_info);
  332. obs_register_encoder(&alac_encoder_info);
  333. obs_register_encoder(&flac_encoder_info);
  334. #ifndef __APPLE__
  335. bool h264 = false;
  336. bool hevc = false;
  337. bool av1 = false;
  338. if (nvenc_supported(&h264, &hevc, &av1)) {
  339. blog(LOG_INFO, "NVENC supported");
  340. #ifdef __linux__
  341. /* why are we here? just to suffer? */
  342. do_nvenc_check_for_ubuntu_20_04();
  343. #endif
  344. #ifdef _WIN32
  345. if (get_win_ver_int() > 0x0601) {
  346. obs_nvenc_load(h264, hevc, av1);
  347. } else {
  348. // if on Win 7, new nvenc isn't available so there's
  349. // no nvenc encoder for the user to select, expose
  350. // the old encoder directly
  351. if (h264) {
  352. h264_nvenc_encoder_info.caps &=
  353. ~OBS_ENCODER_CAP_INTERNAL;
  354. }
  355. #ifdef ENABLE_HEVC
  356. if (hevc) {
  357. hevc_nvenc_encoder_info.caps &=
  358. ~OBS_ENCODER_CAP_INTERNAL;
  359. }
  360. #endif
  361. }
  362. #endif
  363. if (h264)
  364. obs_register_encoder(&h264_nvenc_encoder_info);
  365. #ifdef ENABLE_HEVC
  366. if (hevc)
  367. obs_register_encoder(&hevc_nvenc_encoder_info);
  368. #endif
  369. }
  370. #ifdef _WIN32
  371. amf_load();
  372. #endif
  373. #ifdef LIBAVUTIL_VAAPI_AVAILABLE
  374. const char *libva_env = getenv("LIBVA_DRIVER_NAME");
  375. if (!!libva_env)
  376. blog(LOG_WARNING,
  377. "LIBVA_DRIVER_NAME variable is set,"
  378. " this could prevent FFmpeg VAAPI from working correctly");
  379. if (h264_vaapi_supported()) {
  380. blog(LOG_INFO, "FFmpeg VAAPI H264 encoding supported");
  381. obs_register_encoder(&h264_vaapi_encoder_info);
  382. } else {
  383. blog(LOG_INFO, "FFmpeg VAAPI H264 encoding not supported");
  384. }
  385. #ifdef ENABLE_HEVC
  386. if (hevc_vaapi_supported()) {
  387. blog(LOG_INFO, "FFmpeg VAAPI HEVC encoding supported");
  388. obs_register_encoder(&hevc_vaapi_encoder_info);
  389. } else {
  390. blog(LOG_INFO, "FFmpeg VAAPI HEVC encoding not supported");
  391. }
  392. #endif
  393. #endif
  394. #endif
  395. #if ENABLE_FFMPEG_LOGGING
  396. obs_ffmpeg_load_logging();
  397. #endif
  398. return true;
  399. }
  400. void obs_module_unload(void)
  401. {
  402. #if ENABLE_FFMPEG_LOGGING
  403. obs_ffmpeg_unload_logging();
  404. #endif
  405. #ifdef _WIN32
  406. amf_unload();
  407. obs_nvenc_unload();
  408. #endif
  409. }