obs-ffmpeg.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #ifdef _WIN32
  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. typedef HRESULT(WINAPI *create_dxgi_proc)(const IID *, IDXGIFactory1 **);
  97. static bool nvenc_device_available(void)
  98. {
  99. static HMODULE dxgi = NULL;
  100. static create_dxgi_proc create = NULL;
  101. IDXGIFactory1 *factory;
  102. IDXGIAdapter1 *adapter;
  103. bool available = false;
  104. HRESULT hr;
  105. UINT i = 0;
  106. if (!dxgi) {
  107. dxgi = GetModuleHandleW(L"dxgi");
  108. if (!dxgi) {
  109. dxgi = LoadLibraryW(L"dxgi");
  110. if (!dxgi) {
  111. return true;
  112. }
  113. }
  114. }
  115. if (!create) {
  116. create = (create_dxgi_proc)GetProcAddress(dxgi,
  117. "CreateDXGIFactory1");
  118. if (!create) {
  119. return true;
  120. }
  121. }
  122. hr = create(&IID_IDXGIFactory1, &factory);
  123. if (FAILED(hr)) {
  124. return true;
  125. }
  126. while (factory->lpVtbl->EnumAdapters1(factory, i++, &adapter) == S_OK) {
  127. DXGI_ADAPTER_DESC desc;
  128. hr = adapter->lpVtbl->GetDesc(adapter, &desc);
  129. adapter->lpVtbl->Release(adapter);
  130. if (FAILED(hr)) {
  131. continue;
  132. }
  133. // 0x10de = NVIDIA Corporation
  134. if (desc.VendorId == 0x10de && !is_blacklisted(desc.DeviceId)) {
  135. available = true;
  136. goto finish;
  137. }
  138. }
  139. finish:
  140. factory->lpVtbl->Release(factory);
  141. return available;
  142. }
  143. #endif
  144. #ifdef _WIN32
  145. extern bool load_nvenc_lib(void);
  146. #endif
  147. static bool nvenc_supported(void)
  148. {
  149. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  150. av_register_all();
  151. #endif
  152. profile_start(nvenc_check_name);
  153. AVCodec *nvenc = avcodec_find_encoder_by_name("nvenc_h264");
  154. void *lib = NULL;
  155. bool success = false;
  156. if (!nvenc) {
  157. nvenc = avcodec_find_encoder_by_name("h264_nvenc");
  158. if (!nvenc)
  159. goto cleanup;
  160. }
  161. #if defined(_WIN32)
  162. if (!nvenc_device_available()) {
  163. goto cleanup;
  164. }
  165. if (load_nvenc_lib()) {
  166. success = true;
  167. goto finish;
  168. }
  169. #else
  170. lib = os_dlopen("libnvidia-encode.so.1");
  171. #endif
  172. /* ------------------------------------------- */
  173. success = !!lib;
  174. cleanup:
  175. if (lib)
  176. os_dlclose(lib);
  177. #if defined(_WIN32)
  178. finish:
  179. #endif
  180. profile_end(nvenc_check_name);
  181. return success;
  182. }
  183. #endif
  184. #ifdef LIBAVUTIL_VAAPI_AVAILABLE
  185. static bool vaapi_supported(void)
  186. {
  187. AVCodec *vaenc = avcodec_find_encoder_by_name("h264_vaapi");
  188. return !!vaenc;
  189. }
  190. #endif
  191. #ifdef _WIN32
  192. extern void jim_nvenc_load(void);
  193. extern void jim_nvenc_unload(void);
  194. #endif
  195. #if ENABLE_FFMPEG_LOGGING
  196. extern void obs_ffmpeg_load_logging(void);
  197. extern void obs_ffmpeg_unload_logging(void);
  198. #endif
  199. static void register_encoder_if_available(struct obs_encoder_info *info,
  200. const char *id)
  201. {
  202. AVCodec *c = avcodec_find_encoder_by_name(id);
  203. if (c) {
  204. obs_register_encoder(info);
  205. }
  206. }
  207. bool obs_module_load(void)
  208. {
  209. obs_register_source(&ffmpeg_source);
  210. obs_register_output(&ffmpeg_output);
  211. obs_register_output(&ffmpeg_muxer);
  212. obs_register_output(&ffmpeg_mpegts_muxer);
  213. obs_register_output(&ffmpeg_hls_muxer);
  214. obs_register_output(&replay_buffer);
  215. obs_register_encoder(&aac_encoder_info);
  216. register_encoder_if_available(&svt_av1_encoder_info, "libsvtav1");
  217. register_encoder_if_available(&aom_av1_encoder_info, "libaom-av1");
  218. obs_register_encoder(&opus_encoder_info);
  219. #ifndef __APPLE__
  220. if (nvenc_supported()) {
  221. blog(LOG_INFO, "NVENC supported");
  222. #ifdef _WIN32
  223. if (get_win_ver_int() > 0x0601) {
  224. jim_nvenc_load();
  225. } else {
  226. // if on Win 7, new nvenc isn't available so there's
  227. // no nvenc encoder for the user to select, expose
  228. // the old encoder directly
  229. nvenc_encoder_info.caps &= ~OBS_ENCODER_CAP_INTERNAL;
  230. }
  231. #endif
  232. obs_register_encoder(&nvenc_encoder_info);
  233. }
  234. #if !defined(_WIN32) && defined(LIBAVUTIL_VAAPI_AVAILABLE)
  235. if (vaapi_supported()) {
  236. blog(LOG_INFO, "FFMPEG VAAPI supported");
  237. obs_register_encoder(&vaapi_encoder_info);
  238. }
  239. #endif
  240. #endif
  241. #if ENABLE_FFMPEG_LOGGING
  242. obs_ffmpeg_load_logging();
  243. #endif
  244. return true;
  245. }
  246. void obs_module_unload(void)
  247. {
  248. #if ENABLE_FFMPEG_LOGGING
  249. obs_ffmpeg_unload_logging();
  250. #endif
  251. #ifdef _WIN32
  252. jim_nvenc_unload();
  253. #endif
  254. }