obs-ffmpeg.c 6.4 KB

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