obs-ffmpeg.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #include <obs-module.h>
  2. #include <util/darray.h>
  3. #include <util/platform.h>
  4. #include <libavutil/log.h>
  5. #include <libavutil/avutil.h>
  6. #include <libavcodec/avcodec.h>
  7. #include <libavformat/avformat.h>
  8. #include <pthread.h>
  9. #ifdef _WIN32
  10. #include <dxgi.h>
  11. #include <util/dstr.h>
  12. #include <util/windows/win-version.h>
  13. #endif
  14. OBS_DECLARE_MODULE()
  15. OBS_MODULE_USE_DEFAULT_LOCALE("obs-ffmpeg", "en-US")
  16. MODULE_EXPORT const char *obs_module_description(void)
  17. {
  18. return "FFmpeg based sources/outputs/encoders";
  19. }
  20. extern struct obs_source_info ffmpeg_source;
  21. extern struct obs_output_info ffmpeg_output;
  22. extern struct obs_output_info ffmpeg_muxer;
  23. extern struct obs_output_info replay_buffer;
  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. static DARRAY(struct log_context {
  34. void *context;
  35. char str[4096];
  36. int print_prefix;
  37. } *) active_log_contexts;
  38. static DARRAY(struct log_context *) cached_log_contexts;
  39. pthread_mutex_t log_contexts_mutex = PTHREAD_MUTEX_INITIALIZER;
  40. static struct log_context *create_or_fetch_log_context(void *context)
  41. {
  42. pthread_mutex_lock(&log_contexts_mutex);
  43. for (size_t i = 0; i < active_log_contexts.num; i++) {
  44. if (context == active_log_contexts.array[i]->context) {
  45. pthread_mutex_unlock(&log_contexts_mutex);
  46. return active_log_contexts.array[i];
  47. }
  48. }
  49. struct log_context *new_log_context = NULL;
  50. size_t cnt = cached_log_contexts.num;
  51. if (!!cnt) {
  52. new_log_context = cached_log_contexts.array[cnt - 1];
  53. da_pop_back(cached_log_contexts);
  54. }
  55. if (!new_log_context)
  56. new_log_context = bzalloc(sizeof(struct log_context));
  57. new_log_context->context = context;
  58. new_log_context->str[0] = '\0';
  59. new_log_context->print_prefix = 1;
  60. da_push_back(active_log_contexts, &new_log_context);
  61. pthread_mutex_unlock(&log_contexts_mutex);
  62. return new_log_context;
  63. }
  64. static void destroy_log_context(struct log_context *log_context)
  65. {
  66. pthread_mutex_lock(&log_contexts_mutex);
  67. da_erase_item(active_log_contexts, &log_context);
  68. da_push_back(cached_log_contexts, &log_context);
  69. pthread_mutex_unlock(&log_contexts_mutex);
  70. }
  71. static void ffmpeg_log_callback(void* context, int level, const char* format,
  72. va_list args)
  73. {
  74. if (format == NULL)
  75. return;
  76. struct log_context *log_context = create_or_fetch_log_context(context);
  77. char *str = log_context->str;
  78. av_log_format_line(context, level, format, args, str + strlen(str),
  79. (int)(sizeof(log_context->str) - strlen(str)),
  80. &log_context->print_prefix);
  81. int obs_level;
  82. switch (level) {
  83. case AV_LOG_PANIC:
  84. case AV_LOG_FATAL:
  85. obs_level = LOG_ERROR;
  86. break;
  87. case AV_LOG_ERROR:
  88. case AV_LOG_WARNING:
  89. obs_level = LOG_WARNING;
  90. break;
  91. case AV_LOG_INFO:
  92. case AV_LOG_VERBOSE:
  93. obs_level = LOG_INFO;
  94. break;
  95. case AV_LOG_DEBUG:
  96. default:
  97. obs_level = LOG_DEBUG;
  98. }
  99. if (!log_context->print_prefix)
  100. return;
  101. char *str_end = str + strlen(str) - 1;
  102. while(str < str_end) {
  103. if (*str_end != '\n')
  104. break;
  105. *str_end-- = '\0';
  106. }
  107. if (str_end <= str)
  108. goto cleanup;
  109. blog(obs_level, "[ffmpeg] %s", str);
  110. cleanup:
  111. destroy_log_context(log_context);
  112. }
  113. #ifndef __APPLE__
  114. static const char *nvenc_check_name = "nvenc_check";
  115. #ifdef _WIN32
  116. static const wchar_t *blacklisted_adapters[] = {
  117. L"720M",
  118. L"730M",
  119. L"740M",
  120. L"745M",
  121. L"820M",
  122. L"830M",
  123. L"840M",
  124. L"845M",
  125. L"920M",
  126. L"930M",
  127. L"940M",
  128. L"945M",
  129. L"720A",
  130. L"730A",
  131. L"740A",
  132. L"745A",
  133. L"820A",
  134. L"830A",
  135. L"840A",
  136. L"845A",
  137. L"920A",
  138. L"930A",
  139. L"940A",
  140. L"945A",
  141. L"1030",
  142. L"MX110",
  143. L"MX130",
  144. L"MX150",
  145. L"MX230",
  146. L"MX250",
  147. L"M520",
  148. L"M500",
  149. L"P500",
  150. L"K620M"
  151. };
  152. static const size_t num_blacklisted =
  153. sizeof(blacklisted_adapters) / sizeof(blacklisted_adapters[0]);
  154. static bool is_adapter(const wchar_t *name, const wchar_t *adapter)
  155. {
  156. const wchar_t *find = wstrstri(name, adapter);
  157. if (!find) {
  158. return false;
  159. }
  160. /* check before string for potential numeric mismatch */
  161. if (find > name && iswdigit(find[-1]) && iswdigit(find[0])) {
  162. return false;
  163. }
  164. /* check after string for potential numeric mismatch */
  165. size_t len = wcslen(adapter);
  166. if (iswdigit(find[len - 1]) && iswdigit(find[len])) {
  167. return false;
  168. }
  169. return true;
  170. }
  171. static bool is_blacklisted(const wchar_t *name)
  172. {
  173. for (size_t i = 0; i < num_blacklisted; i++) {
  174. const wchar_t *blacklisted_adapter = blacklisted_adapters[i];
  175. if (is_adapter(name, blacklisted_adapter)) {
  176. return true;
  177. }
  178. }
  179. return false;
  180. }
  181. typedef HRESULT (WINAPI *create_dxgi_proc)(const IID *, IDXGIFactory1 **);
  182. static bool nvenc_device_available(void)
  183. {
  184. static HMODULE dxgi = NULL;
  185. static create_dxgi_proc create = NULL;
  186. IDXGIFactory1 *factory;
  187. IDXGIAdapter1 *adapter;
  188. bool available = false;
  189. HRESULT hr;
  190. UINT i = 0;
  191. if (!dxgi) {
  192. dxgi = GetModuleHandleW(L"dxgi");
  193. if (!dxgi) {
  194. dxgi = LoadLibraryW(L"dxgi");
  195. if (!dxgi) {
  196. return true;
  197. }
  198. }
  199. }
  200. if (!create) {
  201. create = (create_dxgi_proc)GetProcAddress(dxgi,
  202. "CreateDXGIFactory1");
  203. if (!create) {
  204. return true;
  205. }
  206. }
  207. hr = create(&IID_IDXGIFactory1, &factory);
  208. if (FAILED(hr)) {
  209. return true;
  210. }
  211. while (factory->lpVtbl->EnumAdapters1(factory, i++, &adapter) == S_OK) {
  212. DXGI_ADAPTER_DESC desc;
  213. hr = adapter->lpVtbl->GetDesc(adapter, &desc);
  214. adapter->lpVtbl->Release(adapter);
  215. if (FAILED(hr)) {
  216. continue;
  217. }
  218. if (wstrstri(desc.Description, L"nvidia") &&
  219. !is_blacklisted(desc.Description)) {
  220. available = true;
  221. goto finish;
  222. }
  223. }
  224. finish:
  225. factory->lpVtbl->Release(factory);
  226. return available;
  227. }
  228. #endif
  229. #ifdef _WIN32
  230. extern bool load_nvenc_lib(void);
  231. #endif
  232. static bool nvenc_supported(void)
  233. {
  234. av_register_all();
  235. profile_start(nvenc_check_name);
  236. AVCodec *nvenc = avcodec_find_encoder_by_name("nvenc_h264");
  237. void *lib = NULL;
  238. bool success = false;
  239. if (!nvenc) {
  240. goto cleanup;
  241. }
  242. #if defined(_WIN32)
  243. if (!nvenc_device_available()) {
  244. goto cleanup;
  245. }
  246. if (load_nvenc_lib()) {
  247. success = true;
  248. goto finish;
  249. }
  250. #else
  251. lib = os_dlopen("libnvidia-encode.so.1");
  252. #endif
  253. /* ------------------------------------------- */
  254. success = !!lib;
  255. cleanup:
  256. if (lib)
  257. os_dlclose(lib);
  258. finish:
  259. profile_end(nvenc_check_name);
  260. return success;
  261. }
  262. #endif
  263. #ifdef LIBAVUTIL_VAAPI_AVAILABLE
  264. static bool vaapi_supported(void)
  265. {
  266. AVCodec *vaenc = avcodec_find_encoder_by_name("h264_vaapi");
  267. return !!vaenc;
  268. }
  269. #endif
  270. #ifdef _WIN32
  271. extern void jim_nvenc_load(void);
  272. extern void jim_nvenc_unload(void);
  273. #endif
  274. bool obs_module_load(void)
  275. {
  276. da_init(active_log_contexts);
  277. da_init(cached_log_contexts);
  278. //av_log_set_callback(ffmpeg_log_callback);
  279. obs_register_source(&ffmpeg_source);
  280. obs_register_output(&ffmpeg_output);
  281. obs_register_output(&ffmpeg_muxer);
  282. obs_register_output(&replay_buffer);
  283. obs_register_encoder(&aac_encoder_info);
  284. obs_register_encoder(&opus_encoder_info);
  285. #ifndef __APPLE__
  286. if (nvenc_supported()) {
  287. blog(LOG_INFO, "NVENC supported");
  288. #ifdef _WIN32
  289. if (get_win_ver_int() > 0x0601) {
  290. jim_nvenc_load();
  291. }
  292. #endif
  293. obs_register_encoder(&nvenc_encoder_info);
  294. }
  295. #if !defined(_WIN32) && defined(LIBAVUTIL_VAAPI_AVAILABLE)
  296. if (vaapi_supported()) {
  297. blog(LOG_INFO, "FFMPEG VAAPI supported");
  298. obs_register_encoder(&vaapi_encoder_info);
  299. }
  300. #endif
  301. #endif
  302. return true;
  303. }
  304. void obs_module_unload(void)
  305. {
  306. av_log_set_callback(av_log_default_callback);
  307. #ifdef _WIN32
  308. pthread_mutex_destroy(&log_contexts_mutex);
  309. #endif
  310. for (size_t i = 0; i < active_log_contexts.num; i++) {
  311. bfree(active_log_contexts.array[i]);
  312. }
  313. for (size_t i = 0; i < cached_log_contexts.num; i++) {
  314. bfree(cached_log_contexts.array[i]);
  315. }
  316. da_free(active_log_contexts);
  317. da_free(cached_log_contexts);
  318. #ifdef _WIN32
  319. jim_nvenc_unload();
  320. #endif
  321. }