obs-ffmpeg.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(55, 27, 100)
  26. #define LIBAVUTIL_VAAPI_AVAILABLE
  27. #endif
  28. #ifdef LIBAVUTIL_VAAPI_AVAILABLE
  29. extern struct obs_encoder_info vaapi_encoder_info;
  30. #endif
  31. #ifndef __APPLE__
  32. static const char *nvenc_check_name = "nvenc_check";
  33. #ifdef _WIN32
  34. static const wchar_t *blacklisted_adapters[] = {
  35. L"720M", L"730M", L"740M", L"745M", L"820M", L"830M",
  36. L"840M", L"845M", L"920M", L"930M", L"940M", L"945M",
  37. L"1030", L"MX110", L"MX130", L"MX150", L"MX230", L"MX250",
  38. L"M520", L"M500", L"P500", L"K620M",
  39. };
  40. static const size_t num_blacklisted =
  41. sizeof(blacklisted_adapters) / sizeof(blacklisted_adapters[0]);
  42. static bool is_adapter(const wchar_t *name, const wchar_t *adapter)
  43. {
  44. const wchar_t *find = wstrstri(name, adapter);
  45. if (!find) {
  46. return false;
  47. }
  48. /* check before string for potential numeric mismatch */
  49. if (find > name && iswdigit(find[-1]) && iswdigit(find[0])) {
  50. return false;
  51. }
  52. /* check after string for potential numeric mismatch */
  53. size_t len = wcslen(adapter);
  54. if (iswdigit(find[len - 1]) && iswdigit(find[len])) {
  55. return false;
  56. }
  57. return true;
  58. }
  59. static bool is_blacklisted(const wchar_t *name)
  60. {
  61. for (size_t i = 0; i < num_blacklisted; i++) {
  62. const wchar_t *blacklisted_adapter = blacklisted_adapters[i];
  63. if (is_adapter(name, blacklisted_adapter)) {
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. typedef HRESULT(WINAPI *create_dxgi_proc)(const IID *, IDXGIFactory1 **);
  70. static bool nvenc_device_available(void)
  71. {
  72. static HMODULE dxgi = NULL;
  73. static create_dxgi_proc create = NULL;
  74. IDXGIFactory1 *factory;
  75. IDXGIAdapter1 *adapter;
  76. bool available = false;
  77. HRESULT hr;
  78. UINT i = 0;
  79. if (!dxgi) {
  80. dxgi = GetModuleHandleW(L"dxgi");
  81. if (!dxgi) {
  82. dxgi = LoadLibraryW(L"dxgi");
  83. if (!dxgi) {
  84. return true;
  85. }
  86. }
  87. }
  88. if (!create) {
  89. create = (create_dxgi_proc)GetProcAddress(dxgi,
  90. "CreateDXGIFactory1");
  91. if (!create) {
  92. return true;
  93. }
  94. }
  95. hr = create(&IID_IDXGIFactory1, &factory);
  96. if (FAILED(hr)) {
  97. return true;
  98. }
  99. while (factory->lpVtbl->EnumAdapters1(factory, i++, &adapter) == S_OK) {
  100. DXGI_ADAPTER_DESC desc;
  101. hr = adapter->lpVtbl->GetDesc(adapter, &desc);
  102. adapter->lpVtbl->Release(adapter);
  103. if (FAILED(hr)) {
  104. continue;
  105. }
  106. if (wstrstri(desc.Description, L"nvidia") &&
  107. !is_blacklisted(desc.Description)) {
  108. available = true;
  109. goto finish;
  110. }
  111. }
  112. finish:
  113. factory->lpVtbl->Release(factory);
  114. return available;
  115. }
  116. #endif
  117. #ifdef _WIN32
  118. extern bool load_nvenc_lib(void);
  119. #endif
  120. static bool nvenc_supported(void)
  121. {
  122. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  123. av_register_all();
  124. #endif
  125. profile_start(nvenc_check_name);
  126. AVCodec *nvenc = avcodec_find_encoder_by_name("nvenc_h264");
  127. void *lib = NULL;
  128. bool success = false;
  129. if (!nvenc) {
  130. goto cleanup;
  131. }
  132. #if defined(_WIN32)
  133. if (!nvenc_device_available()) {
  134. goto cleanup;
  135. }
  136. if (load_nvenc_lib()) {
  137. success = true;
  138. goto finish;
  139. }
  140. #else
  141. lib = os_dlopen("libnvidia-encode.so.1");
  142. #endif
  143. /* ------------------------------------------- */
  144. success = !!lib;
  145. cleanup:
  146. if (lib)
  147. os_dlclose(lib);
  148. #if defined(_WIN32)
  149. finish:
  150. #endif
  151. profile_end(nvenc_check_name);
  152. return success;
  153. }
  154. #endif
  155. #ifdef LIBAVUTIL_VAAPI_AVAILABLE
  156. static bool vaapi_supported(void)
  157. {
  158. AVCodec *vaenc = avcodec_find_encoder_by_name("h264_vaapi");
  159. return !!vaenc;
  160. }
  161. #endif
  162. #ifdef _WIN32
  163. extern void jim_nvenc_load(void);
  164. extern void jim_nvenc_unload(void);
  165. #endif
  166. #if ENABLE_FFMPEG_LOGGING
  167. extern void obs_ffmpeg_load_logging(void);
  168. extern void obs_ffmpeg_unload_logging(void);
  169. #endif
  170. bool obs_module_load(void)
  171. {
  172. obs_register_source(&ffmpeg_source);
  173. obs_register_output(&ffmpeg_output);
  174. obs_register_output(&ffmpeg_muxer);
  175. obs_register_output(&replay_buffer);
  176. obs_register_encoder(&aac_encoder_info);
  177. obs_register_encoder(&opus_encoder_info);
  178. #ifndef __APPLE__
  179. if (nvenc_supported()) {
  180. blog(LOG_INFO, "NVENC supported");
  181. #ifdef _WIN32
  182. if (get_win_ver_int() > 0x0601) {
  183. jim_nvenc_load();
  184. }
  185. #endif
  186. obs_register_encoder(&nvenc_encoder_info);
  187. }
  188. #if !defined(_WIN32) && defined(LIBAVUTIL_VAAPI_AVAILABLE)
  189. if (vaapi_supported()) {
  190. blog(LOG_INFO, "FFMPEG VAAPI supported");
  191. obs_register_encoder(&vaapi_encoder_info);
  192. }
  193. #endif
  194. #endif
  195. #if ENABLE_FFMPEG_LOGGING
  196. obs_ffmpeg_load_logging();
  197. #endif
  198. return true;
  199. }
  200. void obs_module_unload(void)
  201. {
  202. #if ENABLE_FFMPEG_LOGGING
  203. obs_ffmpeg_unload_logging();
  204. #endif
  205. #ifdef _WIN32
  206. jim_nvenc_unload();
  207. #endif
  208. }