obs-ffmpeg.c 11 KB

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