Przeglądaj źródła

obs-ffmpeg: Add FFMpeg input source

John Bradley 10 lat temu
rodzic
commit
2fc6c4d725

+ 5 - 3
plugins/obs-ffmpeg/CMakeLists.txt

@@ -6,7 +6,7 @@ if(MSVC)
 endif()
 
 find_package(FFMpeg REQUIRED
-	COMPONENTS avcodec avutil swscale avformat swresample)
+	COMPONENTS avcodec avfilter avdevice avutil swscale avformat swresample)
 include_directories(${FFMPEG_INCLUDE_DIRS})
 
 set(obs-ffmpeg_HEADERS
@@ -16,13 +16,15 @@ set(obs-ffmpeg_HEADERS
 set(obs-ffmpeg_SOURCES
 	obs-ffmpeg.c
 	obs-ffmpeg-aac.c
-	obs-ffmpeg-output.c)
-	
+	obs-ffmpeg-output.c
+	obs-ffmpeg-source.c)
+
 add_library(obs-ffmpeg MODULE
 	${obs-ffmpeg_HEADERS}
 	${obs-ffmpeg_SOURCES})
 target_link_libraries(obs-ffmpeg
 	libobs
+	libff
 	${obs-ffmpeg_PLATFORM_DEPS}
 	${FFMPEG_LIBRARIES})
 

+ 11 - 0
plugins/obs-ffmpeg/data/locale/en-US.ini

@@ -1,3 +1,14 @@
 FFmpegOutput="FFmpeg Output"
 FFmpegAAC="FFmpeg Default AAC Encoder"
 Bitrate="Bitrate"
+
+FFmpegSource="Media Source"
+LocalFile="Local File"
+Looping="Loop"
+Input="Input"
+InputFormat="Input Format"
+ForceFormat="Force format conversion"
+HardwareDecode="Use hardware decoding when available"
+Advanced="Advanced"
+AudioBufferSize="Audio Buffer Size (frames)"
+VideoBufferSize="Video Buffer Size (frames)"

+ 423 - 0
plugins/obs-ffmpeg/obs-ffmpeg-source.c

@@ -0,0 +1,423 @@
+/*
+ * Copyright (c) 2015 John R. Bradley <[email protected]>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <obs-module.h>
+#include <util/platform.h>
+
+#include "obs-ffmpeg-compat.h"
+#include "obs-ffmpeg-formats.h"
+
+#include <libff/ff-demuxer.h>
+
+#include <libswscale/swscale.h>
+
+static bool video_frame(struct ff_frame *frame, void *opaque);
+static bool video_format(AVCodecContext *codec_context, void *opaque);
+
+static void ffmpeg_log_callback(void* context, int level, const char* format,
+	va_list args)
+{
+	UNUSED_PARAMETER(context);
+	int obs_level;
+
+	switch (level) {
+	case AV_LOG_PANIC:
+	case AV_LOG_FATAL:
+		obs_level = LOG_ERROR;
+		break;
+	case AV_LOG_ERROR:
+	case AV_LOG_WARNING:
+		obs_level = LOG_WARNING;
+		break;
+	case AV_LOG_INFO:
+	case AV_LOG_VERBOSE:
+		obs_level = LOG_INFO;
+		break;
+	case AV_LOG_DEBUG:
+	default:
+		obs_level = LOG_DEBUG;
+	}
+
+	blogva(obs_level, format, args);
+}
+
+void initialize_ffmpeg_source()
+{
+	av_log_set_callback(ffmpeg_log_callback);
+}
+
+struct ffmpeg_source {
+	struct ff_demuxer *demuxer;
+	struct SwsContext *sws_ctx;
+	obs_source_t *source;
+	enum AVPixelFormat format;
+	bool is_forcing_scale;
+	bool is_hw_decoding;
+};
+
+static bool set_obs_frame_colorprops(struct ff_frame *frame,
+		struct obs_source_frame *obs_frame)
+{
+	enum AVColorSpace frame_cs = av_frame_get_colorspace(frame->frame);
+	enum video_colorspace obs_cs;
+
+	switch(frame_cs) {
+	case AVCOL_SPC_BT709:       obs_cs = VIDEO_CS_709; break;
+	case AVCOL_SPC_BT470BG:     obs_cs = VIDEO_CS_601; break;
+	case AVCOL_SPC_UNSPECIFIED: obs_cs = VIDEO_CS_DEFAULT; break;
+	default:
+		blog(LOG_WARNING, "frame using an unsupported colorspace %d",
+				frame_cs);
+		return false;
+	}
+
+	enum video_range_type range;
+	obs_frame->format = ffmpeg_to_obs_video_format(frame->frame->format);
+	obs_frame->full_range =
+		frame->frame->color_range == AVCOL_RANGE_JPEG;
+
+	range = obs_frame->full_range ? VIDEO_RANGE_FULL : VIDEO_RANGE_PARTIAL;
+
+	if (!video_format_get_parameters(obs_cs,
+			range, obs_frame->color_matrix,
+			obs_frame->color_range_min,
+			obs_frame->color_range_max)) {
+		blog(LOG_ERROR, "Failed to get video format "
+                                "parameters for video format %u",
+                                obs_cs);
+		return false;
+	}
+
+	return true;
+}
+
+static bool video_frame_scale(struct ff_frame *frame,
+		struct ffmpeg_source *s, struct obs_source_frame *obs_frame)
+{
+	int linesize = frame->frame->width * 4;
+	uint8_t *picture_data =
+			malloc(linesize * frame->frame->height);
+	sws_scale(
+		s->sws_ctx,
+		(uint8_t const *const *)frame->frame->data,
+		frame->frame->linesize,
+		0,
+		frame->frame->height,
+		&picture_data,
+		&linesize
+	);
+
+	obs_frame->data[0]     = picture_data;
+	obs_frame->linesize[0] = linesize;
+	obs_frame->format      = VIDEO_FORMAT_BGRA;
+
+	obs_source_output_video(s->source, obs_frame);
+
+	free(picture_data);
+
+	return true;
+}
+
+static bool video_frame_hwaccel(struct ff_frame *frame,
+		obs_source_t *source, struct obs_source_frame *obs_frame)
+{
+	// 4th plane is pixelbuf reference for mac
+	for (int i = 0; i < 3; i++) {
+		obs_frame->data[i] = frame->frame->data[i];
+		obs_frame->linesize[i] = frame->frame->linesize[i];
+	}
+
+	if (!set_obs_frame_colorprops(frame, obs_frame))
+		return false;
+
+	obs_source_output_video(source, obs_frame);
+	return true;
+}
+
+static bool video_frame_direct(struct ff_frame *frame,
+		struct ffmpeg_source *s, struct obs_source_frame *obs_frame)
+{
+	int i;
+
+	for (i = 0; i < MAX_AV_PLANES; i++) {
+		obs_frame->data[i] = frame->frame->data[i];
+		obs_frame->linesize[i] = frame->frame->linesize[i];
+	}
+
+	if (!set_obs_frame_colorprops(frame, obs_frame))
+		return false;
+
+	obs_source_output_video(s->source, obs_frame);
+	return true;
+}
+
+static bool video_frame(struct ff_frame *frame, void *opaque)
+{
+	struct ffmpeg_source *s = opaque;
+	struct obs_source_frame obs_frame = {0};
+
+	double d_pts = ff_get_sync_clock(&s->demuxer->clock) - frame->pts;
+	uint64_t pts = os_gettime_ns() - (uint64_t)(d_pts * 1000000000.0L);
+
+	obs_frame.timestamp = pts;
+	obs_frame.width = frame->frame->width;
+	obs_frame.height = frame->frame->height;
+
+	if (s->sws_ctx != NULL)
+		return video_frame_scale(frame, s, &obs_frame);
+	else if (s->is_hw_decoding)
+		return video_frame_hwaccel(frame, s->source, &obs_frame);
+	else
+		return video_frame_direct(frame, s, &obs_frame);
+}
+
+static bool audio_frame(struct ff_frame *frame, void *opaque)
+{
+	struct ffmpeg_source *s = opaque;
+
+	struct obs_source_audio audio_data = {0};
+
+	double d_pts = ff_get_sync_clock(&s->demuxer->clock) - frame->pts;
+	uint64_t pts = os_gettime_ns() - (uint64_t)(d_pts * 1000000000.0L);
+
+	int channels = av_get_channel_layout_nb_channels(
+				av_frame_get_channel_layout(frame->frame));
+
+	for(int i = 0; i < channels; i++)
+		audio_data.data[i] = frame->frame->data[i];
+
+	audio_data.samples_per_sec = frame->frame->sample_rate;
+	audio_data.frames = frame->frame->nb_samples;
+	audio_data.timestamp = pts;
+	audio_data.format =
+		convert_ffmpeg_sample_format(frame->frame->format);
+	audio_data.speakers = channels;
+
+	obs_source_output_audio(s->source, &audio_data);
+
+	return true;
+}
+
+static bool video_format(AVCodecContext *codec_context, void *opaque)
+{
+	struct ffmpeg_source *s = opaque;
+	if (s->sws_ctx)
+		sws_freeContext(s->sws_ctx);
+
+	enum video_format obs_video_format =
+			ffmpeg_to_obs_video_format(codec_context->pix_fmt);
+
+	if (obs_video_format == VIDEO_FORMAT_NONE || s->is_forcing_scale) {
+		s->sws_ctx = sws_getContext(
+			codec_context->width,
+			codec_context->height,
+			codec_context->pix_fmt,
+			codec_context->width,
+			codec_context->height,
+			AV_PIX_FMT_BGRA,
+			SWS_BILINEAR,
+			NULL,
+			NULL,
+			NULL
+		);
+	}
+
+	return true;
+}
+
+static bool is_local_file_modified(obs_properties_t *props,
+		obs_property_t *prop, obs_data_t *settings)
+{
+	UNUSED_PARAMETER(prop);
+
+	bool enabled = obs_data_get_bool(settings, "is_local_file");
+	obs_property_t *input = obs_properties_get(props, "input");
+	obs_property_t *input_format =obs_properties_get(props,
+			"input_format");
+	obs_property_t *local_file = obs_properties_get(props, "local_file");
+	obs_property_t *looping = obs_properties_get(props, "looping");
+	obs_property_set_visible(input, !enabled);
+	obs_property_set_visible(input_format, !enabled);
+	obs_property_set_visible(local_file, enabled);
+	obs_property_set_visible(looping, enabled);
+
+	return true;
+}
+
+static bool is_advanced_modified(obs_properties_t *props,
+		obs_property_t *prop, obs_data_t *settings)
+{
+	UNUSED_PARAMETER(prop);
+
+	bool enabled = obs_data_get_bool(settings, "advanced");
+	obs_property_t *abuf = obs_properties_get(props, "audio_buffer_size");
+	obs_property_t *vbuf = obs_properties_get(props, "video_buffer_size");
+	obs_property_set_visible(abuf, enabled);
+	obs_property_set_visible(vbuf, enabled);
+
+	return true;
+}
+
+static obs_properties_t *ffmpeg_source_getproperties(void *data)
+{
+	UNUSED_PARAMETER(data);
+
+	obs_properties_t *props = obs_properties_create();
+	obs_property_t *prop;
+	// use this when obs allows non-readonly paths
+	prop = obs_properties_add_bool(props, "is_local_file",
+			obs_module_text("LocalFile"));
+
+	obs_property_set_modified_callback(prop, is_local_file_modified);
+
+	obs_properties_add_path(props, "local_file",
+			obs_module_text("LocalFile"), OBS_PATH_FILE, "*.*",
+			NULL);
+
+	obs_properties_add_bool(props, "looping", obs_module_text("Looping"));
+
+	obs_properties_add_text(props, "input",
+			obs_module_text("Input"), OBS_TEXT_DEFAULT);
+
+	obs_properties_add_text(props, "input_format",
+			obs_module_text("InputFormat"), OBS_TEXT_DEFAULT);
+
+	obs_properties_add_bool(props, "force_scale",
+			obs_module_text("ForceFormat"));
+
+	obs_properties_add_bool(props, "hw_decode",
+			obs_module_text("HardwareDecode"));
+
+	prop = obs_properties_add_bool(props, "advanced",
+			obs_module_text("Advanced"));
+
+	obs_property_set_modified_callback(prop, is_advanced_modified);
+
+	prop = obs_properties_add_int(props, "audio_buffer_size",
+			obs_module_text("AudioBufferSize"), 1, 9999, 1);
+
+	obs_property_set_visible(prop, false);
+
+	prop = obs_properties_add_int(props, "video_buffer_size",
+			obs_module_text("VideoBufferSize"), 1, 9999, 1);
+
+	obs_property_set_visible(prop, false);
+
+	return props;
+}
+
+static void ffmpeg_source_update(void *data, obs_data_t *settings)
+{
+	struct ffmpeg_source *s = data;
+
+	bool is_local_file = obs_data_get_bool(settings, "is_local_file");
+	bool is_advanced = obs_data_get_bool(settings, "advanced");
+
+	bool is_looping;
+	char *input;
+	char *input_format;
+
+	if (is_local_file) {
+		input = (char *)obs_data_get_string(data, "local_file");
+		input_format = NULL;
+		is_looping = obs_data_get_bool(data, "looping");
+	} else {
+		input = (char *)obs_data_get_string(data, "input");
+		input_format = (char *)obs_data_get_string(data,
+				"input_format");
+		is_looping = false;
+	}
+
+	s->is_forcing_scale = obs_data_get_bool(data, "force_scale");
+	s->is_hw_decoding = obs_data_get_bool(data, "hw_decode");
+
+	if (s->demuxer != NULL)
+		ff_demuxer_free(s->demuxer);
+
+	if (s->sws_ctx != NULL) {
+		sws_freeContext(s->sws_ctx);
+		s->sws_ctx = NULL;
+	}
+
+	s->demuxer = ff_demuxer_init();
+	s->demuxer->options.is_hw_decoding = s->is_hw_decoding;
+	s->demuxer->options.is_looping = is_looping;
+
+	if (is_advanced) {
+		int audio_buffer_size = (int)obs_data_get_int(settings,
+				"audio_buffer_size");
+		int video_buffer_size = (int)obs_data_get_int(settings,
+				"video_buffer_size");
+		if (audio_buffer_size < 1) {
+			audio_buffer_size = 1;
+			blog(LOG_WARNING, "invalid audio_buffer_size %d",
+					audio_buffer_size);
+		}
+		if (video_buffer_size < 1) {
+			video_buffer_size = 1;
+			blog(LOG_WARNING, "invalid audio_buffer_size %d",
+					audio_buffer_size);
+		}
+		s->demuxer->options.audio_frame_queue_size = audio_buffer_size;
+		s->demuxer->options.video_frame_queue_size = video_buffer_size;
+	}
+
+	ff_demuxer_set_callbacks(&s->demuxer->video_callbacks,
+			video_frame, video_format,
+			NULL, NULL, NULL, s);
+
+	ff_demuxer_set_callbacks(&s->demuxer->audio_callbacks,
+			audio_frame, NULL,
+			NULL, NULL, NULL, s);
+
+	ff_demuxer_open(s->demuxer, input, input_format);
+}
+
+
+static const char *ffmpeg_source_getname(void)
+{
+	return obs_module_text("FFMpegSource");
+}
+
+static void *ffmpeg_source_create(obs_data_t *settings, obs_source_t *source)
+{
+	UNUSED_PARAMETER(settings);
+
+	struct ffmpeg_source *s = bzalloc(sizeof(struct ffmpeg_source));
+	s->source = source;
+
+	ffmpeg_source_update(s, settings);
+	return s;
+}
+
+static void ffmpeg_source_destroy(void *data)
+{
+	struct ffmpeg_source *s = data;
+
+	ff_demuxer_free(s->demuxer);
+	bfree(s);
+}
+
+struct obs_source_info ffmpeg_source = {
+	.id             = "ffmpeg_source",
+	.type           = OBS_SOURCE_TYPE_INPUT,
+	.output_flags   = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO,
+	.get_name       = ffmpeg_source_getname,
+	.create         = ffmpeg_source_create,
+	.destroy        = ffmpeg_source_destroy,
+	.get_properties = ffmpeg_source_getproperties,
+	.update         = ffmpeg_source_update
+};

+ 6 - 0
plugins/obs-ffmpeg/obs-ffmpeg.c

@@ -3,11 +3,17 @@
 OBS_DECLARE_MODULE()
 OBS_MODULE_USE_DEFAULT_LOCALE("obs-ffmpeg", "en-US")
 
+extern struct obs_source_info  ffmpeg_source;
 extern struct obs_output_info  ffmpeg_output;
 extern struct obs_encoder_info aac_encoder_info;
 
+void initialize_ffmpeg_source();
+
 bool obs_module_load(void)
 {
+	initialize_ffmpeg_source();
+
+	obs_register_source(&ffmpeg_source);
 	obs_register_output(&ffmpeg_output);
 	obs_register_encoder(&aac_encoder_info);
 	return true;