obs-ffmpeg.c 10 KB

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