obs-ffmpeg.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. 0x1c96, // GP107 [GeForce MX350]
  86. 0x1f97, // TU117 [GeForce MX450]
  87. 0x1f98, // TU117 [GeForce MX450]
  88. 0x137b, // GM108GLM [Quadro M520 Mobile]
  89. 0x1d33, // GP108GLM [Quadro P500 Mobile]
  90. 0x137a, // GM108GLM [Quadro K620M / Quadro M500M]
  91. };
  92. static const size_t num_blacklisted =
  93. sizeof(blacklisted_adapters) / sizeof(blacklisted_adapters[0]);
  94. static bool is_blacklisted(const int device_id)
  95. {
  96. for (size_t i = 0; i < num_blacklisted; i++) {
  97. const int blacklisted_adapter = blacklisted_adapters[i];
  98. if (device_id == blacklisted_adapter) {
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. #endif
  105. #if defined(_WIN32)
  106. typedef HRESULT(WINAPI *create_dxgi_proc)(const IID *, IDXGIFactory1 **);
  107. static bool nvenc_device_available(void)
  108. {
  109. static HMODULE dxgi = NULL;
  110. static create_dxgi_proc create = NULL;
  111. IDXGIFactory1 *factory;
  112. IDXGIAdapter1 *adapter;
  113. bool available = false;
  114. HRESULT hr;
  115. UINT i = 0;
  116. if (!dxgi) {
  117. dxgi = GetModuleHandleW(L"dxgi");
  118. if (!dxgi) {
  119. dxgi = LoadLibraryW(L"dxgi");
  120. if (!dxgi) {
  121. return true;
  122. }
  123. }
  124. }
  125. if (!create) {
  126. create = (create_dxgi_proc)GetProcAddress(dxgi,
  127. "CreateDXGIFactory1");
  128. if (!create) {
  129. return true;
  130. }
  131. }
  132. hr = create(&IID_IDXGIFactory1, &factory);
  133. if (FAILED(hr)) {
  134. return true;
  135. }
  136. while (factory->lpVtbl->EnumAdapters1(factory, i++, &adapter) == S_OK) {
  137. DXGI_ADAPTER_DESC desc;
  138. hr = adapter->lpVtbl->GetDesc(adapter, &desc);
  139. adapter->lpVtbl->Release(adapter);
  140. if (FAILED(hr)) {
  141. continue;
  142. }
  143. // 0x10de = NVIDIA Corporation
  144. if (desc.VendorId == 0x10de && !is_blacklisted(desc.DeviceId)) {
  145. available = true;
  146. goto finish;
  147. }
  148. }
  149. finish:
  150. factory->lpVtbl->Release(factory);
  151. return available;
  152. }
  153. #endif
  154. #if defined(__linux__)
  155. static int get_id_from_sys(char *d_name, char *type)
  156. {
  157. char file_name[1024];
  158. char *c;
  159. int id;
  160. snprintf(file_name, sizeof(file_name), "/sys/bus/pci/devices/%s/%s",
  161. d_name, type);
  162. if ((c = os_quick_read_utf8_file(file_name)) == NULL) {
  163. return -1;
  164. }
  165. id = strtol(c, NULL, 16);
  166. bfree(c);
  167. return id;
  168. }
  169. static bool nvenc_device_available(void)
  170. {
  171. os_dir_t *dir;
  172. struct os_dirent *dirent;
  173. bool available = false;
  174. if ((dir = os_opendir("/sys/bus/pci/devices")) == NULL) {
  175. return true;
  176. }
  177. while ((dirent = os_readdir(dir)) != NULL) {
  178. int id;
  179. if (get_id_from_sys(dirent->d_name, "class") != 0x030000 &&
  180. get_id_from_sys(dirent->d_name, "class") !=
  181. 0x030200) { // 0x030000 = VGA compatible controller
  182. // 0x030200 = 3D controller
  183. continue;
  184. }
  185. if (get_id_from_sys(dirent->d_name, "vendor") !=
  186. 0x10de) { // 0x10de = NVIDIA Corporation
  187. continue;
  188. }
  189. if ((id = get_id_from_sys(dirent->d_name, "device")) > 0 &&
  190. !is_blacklisted(id)) {
  191. available = true;
  192. break;
  193. }
  194. }
  195. os_closedir(dir);
  196. return available;
  197. }
  198. #endif
  199. #ifdef _WIN32
  200. extern bool load_nvenc_lib(void);
  201. extern uint32_t get_nvenc_ver();
  202. #endif
  203. /* please remove this annoying garbage and the associated garbage in
  204. * obs-ffmpeg-nvenc.c when ubuntu 20.04 is finally gone for good. */
  205. #ifdef __linux__
  206. bool ubuntu_20_04_nvenc_fallback = false;
  207. static void do_nvenc_check_for_ubuntu_20_04(void)
  208. {
  209. FILE *fp;
  210. char *line = NULL;
  211. size_t linecap = 0;
  212. fp = fopen("/etc/os-release", "r");
  213. if (!fp) {
  214. return;
  215. }
  216. while (getline(&line, &linecap, fp) != -1) {
  217. if (strncmp(line, "VERSION_CODENAME=focal", 22) == 0) {
  218. ubuntu_20_04_nvenc_fallback = true;
  219. }
  220. }
  221. fclose(fp);
  222. free(line);
  223. }
  224. #endif
  225. static bool nvenc_codec_exists(const char *name, const char *fallback)
  226. {
  227. const AVCodec *nvenc = avcodec_find_encoder_by_name(name);
  228. if (!nvenc)
  229. nvenc = avcodec_find_encoder_by_name(fallback);
  230. return nvenc != NULL;
  231. }
  232. static bool nvenc_supported(bool *out_h264, bool *out_hevc, bool *out_av1)
  233. {
  234. profile_start(nvenc_check_name);
  235. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  236. av_register_all();
  237. #endif
  238. const bool h264 = nvenc_codec_exists("h264_nvenc", "nvenc_h264");
  239. #ifdef ENABLE_HEVC
  240. const bool hevc = nvenc_codec_exists("hevc_nvenc", "nvenc_hevc");
  241. #else
  242. const bool hevc = false;
  243. #endif
  244. bool av1 = false;
  245. bool success = h264 || hevc;
  246. if (success) {
  247. #if defined(_WIN32)
  248. success = nvenc_device_available() && load_nvenc_lib();
  249. av1 = success && (get_nvenc_ver() >= ((12 << 4) | 0));
  250. #elif defined(__linux__)
  251. success = nvenc_device_available();
  252. if (success) {
  253. void *const lib = os_dlopen("libnvidia-encode.so.1");
  254. success = lib != NULL;
  255. if (success)
  256. os_dlclose(lib);
  257. }
  258. #else
  259. void *const lib = os_dlopen("libnvidia-encode.so.1");
  260. success = lib != NULL;
  261. if (success)
  262. os_dlclose(lib);
  263. #endif
  264. if (success) {
  265. *out_h264 = h264;
  266. *out_hevc = hevc;
  267. *out_av1 = av1;
  268. }
  269. }
  270. profile_end(nvenc_check_name);
  271. return success;
  272. }
  273. #endif
  274. #ifdef LIBAVUTIL_VAAPI_AVAILABLE
  275. static bool h264_vaapi_supported(void)
  276. {
  277. const AVCodec *vaenc = avcodec_find_encoder_by_name("h264_vaapi");
  278. if (!vaenc)
  279. return false;
  280. /* NOTE: If default device is NULL, it means there is no device
  281. * that support H264. */
  282. return vaapi_get_h264_default_device() != NULL;
  283. }
  284. #endif
  285. #ifdef _WIN32
  286. extern void jim_nvenc_load(bool h264, bool hevc, bool av1);
  287. extern void jim_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. #ifndef __APPLE__
  316. bool h264 = false;
  317. bool hevc = false;
  318. bool av1 = false;
  319. if (nvenc_supported(&h264, &hevc, &av1)) {
  320. blog(LOG_INFO, "NVENC supported");
  321. #ifdef __linux__
  322. /* why are we here? just to suffer? */
  323. do_nvenc_check_for_ubuntu_20_04();
  324. #endif
  325. #ifdef _WIN32
  326. if (get_win_ver_int() > 0x0601) {
  327. jim_nvenc_load(h264, hevc, av1);
  328. } else {
  329. // if on Win 7, new nvenc isn't available so there's
  330. // no nvenc encoder for the user to select, expose
  331. // the old encoder directly
  332. if (h264) {
  333. h264_nvenc_encoder_info.caps &=
  334. ~OBS_ENCODER_CAP_INTERNAL;
  335. }
  336. #ifdef ENABLE_HEVC
  337. if (hevc) {
  338. hevc_nvenc_encoder_info.caps &=
  339. ~OBS_ENCODER_CAP_INTERNAL;
  340. }
  341. #endif
  342. }
  343. #endif
  344. if (h264)
  345. obs_register_encoder(&h264_nvenc_encoder_info);
  346. #ifdef ENABLE_HEVC
  347. if (hevc)
  348. obs_register_encoder(&hevc_nvenc_encoder_info);
  349. #endif
  350. }
  351. #ifdef _WIN32
  352. amf_load();
  353. #endif
  354. #ifdef LIBAVUTIL_VAAPI_AVAILABLE
  355. const char *libva_env = getenv("LIBVA_DRIVER_NAME");
  356. if (!!libva_env)
  357. blog(LOG_WARNING,
  358. "LIBVA_DRIVER_NAME variable is set,"
  359. " this could prevent FFmpeg VAAPI from working correctly");
  360. if (h264_vaapi_supported()) {
  361. blog(LOG_INFO, "FFmpeg VAAPI H264 encoding supported");
  362. obs_register_encoder(&vaapi_encoder_info);
  363. } else {
  364. blog(LOG_INFO, "FFmpeg VAAPI H264 encoding not supported");
  365. }
  366. #endif
  367. #endif
  368. #if ENABLE_FFMPEG_LOGGING
  369. obs_ffmpeg_load_logging();
  370. #endif
  371. return true;
  372. }
  373. void obs_module_unload(void)
  374. {
  375. #if ENABLE_FFMPEG_LOGGING
  376. obs_ffmpeg_unload_logging();
  377. #endif
  378. #ifdef _WIN32
  379. amf_unload();
  380. jim_nvenc_unload();
  381. #endif
  382. }