obs-ffmpeg.c 6.3 KB

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