obs-ffmpeg.c 10 KB

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