obs-ffmpeg.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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"1030",
  130. L"MX110",
  131. L"MX130",
  132. L"MX150",
  133. L"MX230",
  134. L"MX250",
  135. L"M520",
  136. L"M500",
  137. L"P500",
  138. L"K620M"
  139. };
  140. static const size_t num_blacklisted =
  141. sizeof(blacklisted_adapters) / sizeof(blacklisted_adapters[0]);
  142. static bool is_adapter(const wchar_t *name, const wchar_t *adapter)
  143. {
  144. const wchar_t *find = wstrstri(name, adapter);
  145. if (!find) {
  146. return false;
  147. }
  148. /* check before string for potential numeric mismatch */
  149. if (find > name && iswdigit(find[-1]) && iswdigit(find[0])) {
  150. return false;
  151. }
  152. /* check after string for potential numeric mismatch */
  153. size_t len = wcslen(adapter);
  154. if (iswdigit(find[len - 1]) && iswdigit(find[len])) {
  155. return false;
  156. }
  157. return true;
  158. }
  159. static bool is_blacklisted(const wchar_t *name)
  160. {
  161. for (size_t i = 0; i < num_blacklisted; i++) {
  162. const wchar_t *blacklisted_adapter = blacklisted_adapters[i];
  163. if (is_adapter(name, blacklisted_adapter)) {
  164. return true;
  165. }
  166. }
  167. return false;
  168. }
  169. typedef HRESULT (WINAPI *create_dxgi_proc)(const IID *, IDXGIFactory1 **);
  170. static bool nvenc_device_available(void)
  171. {
  172. static HMODULE dxgi = NULL;
  173. static create_dxgi_proc create = NULL;
  174. IDXGIFactory1 *factory;
  175. IDXGIAdapter1 *adapter;
  176. bool available = false;
  177. HRESULT hr;
  178. UINT i = 0;
  179. if (!dxgi) {
  180. dxgi = GetModuleHandleW(L"dxgi");
  181. if (!dxgi) {
  182. dxgi = LoadLibraryW(L"dxgi");
  183. if (!dxgi) {
  184. return true;
  185. }
  186. }
  187. }
  188. if (!create) {
  189. create = (create_dxgi_proc)GetProcAddress(dxgi,
  190. "CreateDXGIFactory1");
  191. if (!create) {
  192. return true;
  193. }
  194. }
  195. hr = create(&IID_IDXGIFactory1, &factory);
  196. if (FAILED(hr)) {
  197. return true;
  198. }
  199. while (factory->lpVtbl->EnumAdapters1(factory, i++, &adapter) == S_OK) {
  200. DXGI_ADAPTER_DESC desc;
  201. hr = adapter->lpVtbl->GetDesc(adapter, &desc);
  202. adapter->lpVtbl->Release(adapter);
  203. if (FAILED(hr)) {
  204. continue;
  205. }
  206. if (wstrstri(desc.Description, L"nvidia") &&
  207. !is_blacklisted(desc.Description)) {
  208. available = true;
  209. goto finish;
  210. }
  211. }
  212. finish:
  213. factory->lpVtbl->Release(factory);
  214. return available;
  215. }
  216. #endif
  217. #ifdef _WIN32
  218. extern bool load_nvenc_lib(void);
  219. #endif
  220. static bool nvenc_supported(void)
  221. {
  222. av_register_all();
  223. profile_start(nvenc_check_name);
  224. AVCodec *nvenc = avcodec_find_encoder_by_name("nvenc_h264");
  225. void *lib = NULL;
  226. bool success = false;
  227. if (!nvenc) {
  228. goto cleanup;
  229. }
  230. #if defined(_WIN32)
  231. if (!nvenc_device_available()) {
  232. goto cleanup;
  233. }
  234. if (load_nvenc_lib()) {
  235. success = true;
  236. goto finish;
  237. }
  238. #else
  239. lib = os_dlopen("libnvidia-encode.so.1");
  240. #endif
  241. /* ------------------------------------------- */
  242. success = !!lib;
  243. cleanup:
  244. if (lib)
  245. os_dlclose(lib);
  246. #if defined(_WIN32)
  247. finish:
  248. #endif
  249. profile_end(nvenc_check_name);
  250. return success;
  251. }
  252. #endif
  253. #ifdef LIBAVUTIL_VAAPI_AVAILABLE
  254. static bool vaapi_supported(void)
  255. {
  256. AVCodec *vaenc = avcodec_find_encoder_by_name("h264_vaapi");
  257. return !!vaenc;
  258. }
  259. #endif
  260. #ifdef _WIN32
  261. extern void jim_nvenc_load(void);
  262. extern void jim_nvenc_unload(void);
  263. #endif
  264. bool obs_module_load(void)
  265. {
  266. da_init(active_log_contexts);
  267. da_init(cached_log_contexts);
  268. //av_log_set_callback(ffmpeg_log_callback);
  269. obs_register_source(&ffmpeg_source);
  270. obs_register_output(&ffmpeg_output);
  271. obs_register_output(&ffmpeg_muxer);
  272. obs_register_output(&replay_buffer);
  273. obs_register_encoder(&aac_encoder_info);
  274. obs_register_encoder(&opus_encoder_info);
  275. #ifndef __APPLE__
  276. if (nvenc_supported()) {
  277. blog(LOG_INFO, "NVENC supported");
  278. #ifdef _WIN32
  279. if (get_win_ver_int() > 0x0601) {
  280. jim_nvenc_load();
  281. }
  282. #endif
  283. obs_register_encoder(&nvenc_encoder_info);
  284. }
  285. #if !defined(_WIN32) && defined(LIBAVUTIL_VAAPI_AVAILABLE)
  286. if (vaapi_supported()) {
  287. blog(LOG_INFO, "FFMPEG VAAPI supported");
  288. obs_register_encoder(&vaapi_encoder_info);
  289. }
  290. #endif
  291. #endif
  292. return true;
  293. }
  294. void obs_module_unload(void)
  295. {
  296. av_log_set_callback(av_log_default_callback);
  297. #ifdef _WIN32
  298. pthread_mutex_destroy(&log_contexts_mutex);
  299. #endif
  300. for (size_t i = 0; i < active_log_contexts.num; i++) {
  301. bfree(active_log_contexts.array[i]);
  302. }
  303. for (size_t i = 0; i < cached_log_contexts.num; i++) {
  304. bfree(cached_log_contexts.array[i]);
  305. }
  306. da_free(active_log_contexts);
  307. da_free(cached_log_contexts);
  308. #ifdef _WIN32
  309. jim_nvenc_unload();
  310. #endif
  311. }