obs-ffmpeg.c 8.8 KB

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