1
0

obs-ffmpeg-source.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright (c) 2015 John R. Bradley <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <obs-module.h>
  17. #include <util/platform.h>
  18. #include "obs-ffmpeg-compat.h"
  19. #include "obs-ffmpeg-formats.h"
  20. #include <libff/ff-demuxer.h>
  21. #include <libswscale/swscale.h>
  22. static bool video_frame(struct ff_frame *frame, void *opaque);
  23. static bool video_format(AVCodecContext *codec_context, void *opaque);
  24. static void ffmpeg_log_callback(void* context, int level, const char* format,
  25. va_list args)
  26. {
  27. UNUSED_PARAMETER(context);
  28. int obs_level;
  29. switch (level) {
  30. case AV_LOG_PANIC:
  31. case AV_LOG_FATAL:
  32. obs_level = LOG_ERROR;
  33. break;
  34. case AV_LOG_ERROR:
  35. case AV_LOG_WARNING:
  36. obs_level = LOG_WARNING;
  37. break;
  38. case AV_LOG_INFO:
  39. case AV_LOG_VERBOSE:
  40. obs_level = LOG_INFO;
  41. break;
  42. case AV_LOG_DEBUG:
  43. default:
  44. obs_level = LOG_DEBUG;
  45. }
  46. blogva(obs_level, format, args);
  47. }
  48. void initialize_ffmpeg_source()
  49. {
  50. av_log_set_callback(ffmpeg_log_callback);
  51. }
  52. struct ffmpeg_source {
  53. struct ff_demuxer *demuxer;
  54. struct SwsContext *sws_ctx;
  55. obs_source_t *source;
  56. enum AVPixelFormat format;
  57. bool is_forcing_scale;
  58. bool is_hw_decoding;
  59. };
  60. static bool set_obs_frame_colorprops(struct ff_frame *frame,
  61. struct obs_source_frame *obs_frame)
  62. {
  63. enum AVColorSpace frame_cs = av_frame_get_colorspace(frame->frame);
  64. enum video_colorspace obs_cs;
  65. switch(frame_cs) {
  66. case AVCOL_SPC_BT709: obs_cs = VIDEO_CS_709; break;
  67. case AVCOL_SPC_BT470BG: obs_cs = VIDEO_CS_601; break;
  68. case AVCOL_SPC_UNSPECIFIED: obs_cs = VIDEO_CS_DEFAULT; break;
  69. default:
  70. blog(LOG_WARNING, "frame using an unsupported colorspace %d",
  71. frame_cs);
  72. return false;
  73. }
  74. enum video_range_type range;
  75. obs_frame->format = ffmpeg_to_obs_video_format(frame->frame->format);
  76. obs_frame->full_range =
  77. frame->frame->color_range == AVCOL_RANGE_JPEG;
  78. range = obs_frame->full_range ? VIDEO_RANGE_FULL : VIDEO_RANGE_PARTIAL;
  79. if (!video_format_get_parameters(obs_cs,
  80. range, obs_frame->color_matrix,
  81. obs_frame->color_range_min,
  82. obs_frame->color_range_max)) {
  83. blog(LOG_ERROR, "Failed to get video format "
  84. "parameters for video format %u",
  85. obs_cs);
  86. return false;
  87. }
  88. return true;
  89. }
  90. bool update_sws_context(struct ffmpeg_source *source, AVFrame *frame)
  91. {
  92. source->sws_ctx = sws_getCachedContext(
  93. source->sws_ctx,
  94. frame->width,
  95. frame->height,
  96. frame->format,
  97. frame->width,
  98. frame->height,
  99. AV_PIX_FMT_BGRA,
  100. SWS_BILINEAR,
  101. NULL, NULL, NULL);
  102. return source->sws_ctx != NULL;
  103. }
  104. static bool video_frame_scale(struct ff_frame *frame,
  105. struct ffmpeg_source *s, struct obs_source_frame *obs_frame)
  106. {
  107. int linesize = frame->frame->width * 4;
  108. uint8_t *picture_data =
  109. malloc(linesize * frame->frame->height);
  110. update_sws_context(s, frame->frame);
  111. sws_scale(
  112. s->sws_ctx,
  113. (uint8_t const *const *)frame->frame->data,
  114. frame->frame->linesize,
  115. 0,
  116. frame->frame->height,
  117. &picture_data,
  118. &linesize
  119. );
  120. obs_frame->data[0] = picture_data;
  121. obs_frame->linesize[0] = linesize;
  122. obs_frame->format = VIDEO_FORMAT_BGRA;
  123. obs_source_output_video(s->source, obs_frame);
  124. free(picture_data);
  125. return true;
  126. }
  127. static bool video_frame_hwaccel(struct ff_frame *frame,
  128. obs_source_t *source, struct obs_source_frame *obs_frame)
  129. {
  130. // 4th plane is pixelbuf reference for mac
  131. for (int i = 0; i < 3; i++) {
  132. obs_frame->data[i] = frame->frame->data[i];
  133. obs_frame->linesize[i] = frame->frame->linesize[i];
  134. }
  135. if (!set_obs_frame_colorprops(frame, obs_frame))
  136. return false;
  137. obs_source_output_video(source, obs_frame);
  138. return true;
  139. }
  140. static bool video_frame_direct(struct ff_frame *frame,
  141. struct ffmpeg_source *s, struct obs_source_frame *obs_frame)
  142. {
  143. int i;
  144. for (i = 0; i < MAX_AV_PLANES; i++) {
  145. obs_frame->data[i] = frame->frame->data[i];
  146. obs_frame->linesize[i] = frame->frame->linesize[i];
  147. }
  148. if (!set_obs_frame_colorprops(frame, obs_frame))
  149. return false;
  150. obs_source_output_video(s->source, obs_frame);
  151. return true;
  152. }
  153. static bool video_frame(struct ff_frame *frame, void *opaque)
  154. {
  155. struct ffmpeg_source *s = opaque;
  156. struct obs_source_frame obs_frame = {0};
  157. double d_pts = ff_get_sync_clock(&s->demuxer->clock) - frame->pts;
  158. uint64_t pts = os_gettime_ns() - (uint64_t)(d_pts * 1000000000.0L);
  159. obs_frame.timestamp = pts;
  160. obs_frame.width = frame->frame->width;
  161. obs_frame.height = frame->frame->height;
  162. enum video_format format =
  163. ffmpeg_to_obs_video_format(frame->frame->format);
  164. if (s->is_forcing_scale || format == VIDEO_FORMAT_NONE)
  165. return video_frame_scale(frame, s, &obs_frame);
  166. else if (s->is_hw_decoding)
  167. return video_frame_hwaccel(frame, s->source, &obs_frame);
  168. else
  169. return video_frame_direct(frame, s, &obs_frame);
  170. }
  171. static bool audio_frame(struct ff_frame *frame, void *opaque)
  172. {
  173. struct ffmpeg_source *s = opaque;
  174. struct obs_source_audio audio_data = {0};
  175. double d_pts = ff_get_sync_clock(&s->demuxer->clock) - frame->pts;
  176. uint64_t pts = os_gettime_ns() - (uint64_t)(d_pts * 1000000000.0L);
  177. int channels = av_get_channel_layout_nb_channels(
  178. av_frame_get_channel_layout(frame->frame));
  179. for(int i = 0; i < channels; i++)
  180. audio_data.data[i] = frame->frame->data[i];
  181. audio_data.samples_per_sec = frame->frame->sample_rate;
  182. audio_data.frames = frame->frame->nb_samples;
  183. audio_data.timestamp = pts;
  184. audio_data.format =
  185. convert_ffmpeg_sample_format(frame->frame->format);
  186. audio_data.speakers = channels;
  187. obs_source_output_audio(s->source, &audio_data);
  188. return true;
  189. }
  190. static bool is_local_file_modified(obs_properties_t *props,
  191. obs_property_t *prop, obs_data_t *settings)
  192. {
  193. UNUSED_PARAMETER(prop);
  194. bool enabled = obs_data_get_bool(settings, "is_local_file");
  195. obs_property_t *input = obs_properties_get(props, "input");
  196. obs_property_t *input_format =obs_properties_get(props,
  197. "input_format");
  198. obs_property_t *local_file = obs_properties_get(props, "local_file");
  199. obs_property_t *looping = obs_properties_get(props, "looping");
  200. obs_property_set_visible(input, !enabled);
  201. obs_property_set_visible(input_format, !enabled);
  202. obs_property_set_visible(local_file, enabled);
  203. obs_property_set_visible(looping, enabled);
  204. return true;
  205. }
  206. static bool is_advanced_modified(obs_properties_t *props,
  207. obs_property_t *prop, obs_data_t *settings)
  208. {
  209. UNUSED_PARAMETER(prop);
  210. bool enabled = obs_data_get_bool(settings, "advanced");
  211. obs_property_t *abuf = obs_properties_get(props, "audio_buffer_size");
  212. obs_property_t *vbuf = obs_properties_get(props, "video_buffer_size");
  213. obs_property_set_visible(abuf, enabled);
  214. obs_property_set_visible(vbuf, enabled);
  215. return true;
  216. }
  217. static obs_properties_t *ffmpeg_source_getproperties(void *data)
  218. {
  219. UNUSED_PARAMETER(data);
  220. obs_properties_t *props = obs_properties_create();
  221. obs_property_t *prop;
  222. // use this when obs allows non-readonly paths
  223. prop = obs_properties_add_bool(props, "is_local_file",
  224. obs_module_text("LocalFile"));
  225. obs_property_set_modified_callback(prop, is_local_file_modified);
  226. obs_properties_add_path(props, "local_file",
  227. obs_module_text("LocalFile"), OBS_PATH_FILE, "*.*",
  228. NULL);
  229. obs_properties_add_bool(props, "looping", obs_module_text("Looping"));
  230. obs_properties_add_text(props, "input",
  231. obs_module_text("Input"), OBS_TEXT_DEFAULT);
  232. obs_properties_add_text(props, "input_format",
  233. obs_module_text("InputFormat"), OBS_TEXT_DEFAULT);
  234. obs_properties_add_bool(props, "force_scale",
  235. obs_module_text("ForceFormat"));
  236. obs_properties_add_bool(props, "hw_decode",
  237. obs_module_text("HardwareDecode"));
  238. prop = obs_properties_add_bool(props, "advanced",
  239. obs_module_text("Advanced"));
  240. obs_property_set_modified_callback(prop, is_advanced_modified);
  241. prop = obs_properties_add_int(props, "audio_buffer_size",
  242. obs_module_text("AudioBufferSize"), 1, 9999, 1);
  243. obs_property_set_visible(prop, false);
  244. prop = obs_properties_add_int(props, "video_buffer_size",
  245. obs_module_text("VideoBufferSize"), 1, 9999, 1);
  246. obs_property_set_visible(prop, false);
  247. return props;
  248. }
  249. static void ffmpeg_source_update(void *data, obs_data_t *settings)
  250. {
  251. struct ffmpeg_source *s = data;
  252. bool is_local_file = obs_data_get_bool(settings, "is_local_file");
  253. bool is_advanced = obs_data_get_bool(settings, "advanced");
  254. bool is_looping;
  255. char *input;
  256. char *input_format;
  257. if (is_local_file) {
  258. input = (char *)obs_data_get_string(settings, "local_file");
  259. input_format = NULL;
  260. is_looping = obs_data_get_bool(settings, "looping");
  261. } else {
  262. input = (char *)obs_data_get_string(settings, "input");
  263. input_format = (char *)obs_data_get_string(settings,
  264. "input_format");
  265. is_looping = false;
  266. }
  267. s->is_forcing_scale = obs_data_get_bool(settings, "force_scale");
  268. s->is_hw_decoding = obs_data_get_bool(settings, "hw_decode");
  269. if (s->demuxer != NULL)
  270. ff_demuxer_free(s->demuxer);
  271. s->demuxer = ff_demuxer_init();
  272. s->demuxer->options.is_hw_decoding = s->is_hw_decoding;
  273. s->demuxer->options.is_looping = is_looping;
  274. if (is_advanced) {
  275. int audio_buffer_size = (int)obs_data_get_int(settings,
  276. "audio_buffer_size");
  277. int video_buffer_size = (int)obs_data_get_int(settings,
  278. "video_buffer_size");
  279. if (audio_buffer_size < 1) {
  280. audio_buffer_size = 1;
  281. blog(LOG_WARNING, "invalid audio_buffer_size %d",
  282. audio_buffer_size);
  283. }
  284. if (video_buffer_size < 1) {
  285. video_buffer_size = 1;
  286. blog(LOG_WARNING, "invalid audio_buffer_size %d",
  287. audio_buffer_size);
  288. }
  289. s->demuxer->options.audio_frame_queue_size = audio_buffer_size;
  290. s->demuxer->options.video_frame_queue_size = video_buffer_size;
  291. }
  292. ff_demuxer_set_callbacks(&s->demuxer->video_callbacks,
  293. video_frame, NULL,
  294. NULL, NULL, NULL, s);
  295. ff_demuxer_set_callbacks(&s->demuxer->audio_callbacks,
  296. audio_frame, NULL,
  297. NULL, NULL, NULL, s);
  298. ff_demuxer_open(s->demuxer, input, input_format);
  299. }
  300. static const char *ffmpeg_source_getname(void)
  301. {
  302. return obs_module_text("FFMpegSource");
  303. }
  304. static void *ffmpeg_source_create(obs_data_t *settings, obs_source_t *source)
  305. {
  306. UNUSED_PARAMETER(settings);
  307. struct ffmpeg_source *s = bzalloc(sizeof(struct ffmpeg_source));
  308. s->source = source;
  309. ffmpeg_source_update(s, settings);
  310. return s;
  311. }
  312. static void ffmpeg_source_destroy(void *data)
  313. {
  314. struct ffmpeg_source *s = data;
  315. ff_demuxer_free(s->demuxer);
  316. if (s->sws_ctx != NULL) {
  317. sws_freeContext(s->sws_ctx);
  318. }
  319. bfree(s);
  320. }
  321. struct obs_source_info ffmpeg_source = {
  322. .id = "ffmpeg_source",
  323. .type = OBS_SOURCE_TYPE_INPUT,
  324. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO,
  325. .get_name = ffmpeg_source_getname,
  326. .create = ffmpeg_source_create,
  327. .destroy = ffmpeg_source_destroy,
  328. .get_properties = ffmpeg_source_getproperties,
  329. .update = ffmpeg_source_update
  330. };