obs-ffmpeg.c 8.1 KB

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