2015-03-04 10:45:50 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 John R. Bradley <jrb@turrettech.com>
|
|
|
|
*
|
|
|
|
* 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>
|
2016-01-23 02:28:36 -08:00
|
|
|
#include <util/dstr.h>
|
2015-03-04 10:45:50 -08:00
|
|
|
|
|
|
|
#include "obs-ffmpeg-compat.h"
|
|
|
|
#include "obs-ffmpeg-formats.h"
|
|
|
|
|
|
|
|
#include <libff/ff-demuxer.h>
|
|
|
|
|
|
|
|
#include <libswscale/swscale.h>
|
|
|
|
|
2015-08-03 13:40:40 -07:00
|
|
|
#define FF_LOG(level, format, ...) \
|
|
|
|
blog(level, "[Media Source]: " format, ##__VA_ARGS__)
|
|
|
|
#define FF_LOG_S(source, level, format, ...) \
|
|
|
|
blog(level, "[Media Source '%s']: " format, \
|
|
|
|
obs_source_get_name(source), ##__VA_ARGS__)
|
|
|
|
#define FF_BLOG(level, format, ...) \
|
|
|
|
FF_LOG_S(s->source, level, format, ##__VA_ARGS__)
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
static bool video_frame(struct ff_frame *frame, void *opaque);
|
|
|
|
static bool video_format(AVCodecContext *codec_context, void *opaque);
|
|
|
|
|
|
|
|
struct ffmpeg_source {
|
|
|
|
struct ff_demuxer *demuxer;
|
|
|
|
struct SwsContext *sws_ctx;
|
2015-03-22 21:03:28 -07:00
|
|
|
int sws_width;
|
|
|
|
int sws_height;
|
|
|
|
enum AVPixelFormat sws_format;
|
2015-03-10 12:37:28 -07:00
|
|
|
uint8_t *sws_data;
|
|
|
|
int sws_linesize;
|
2015-03-04 10:45:50 -08:00
|
|
|
obs_source_t *source;
|
|
|
|
bool is_forcing_scale;
|
|
|
|
bool is_hw_decoding;
|
2015-03-23 22:21:24 -07:00
|
|
|
bool is_clear_on_media_end;
|
2015-03-04 10:45:50 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool set_obs_frame_colorprops(struct ff_frame *frame,
|
2015-08-03 13:40:40 -07:00
|
|
|
struct ffmpeg_source *s, struct obs_source_frame *obs_frame)
|
2015-03-04 10:45:50 -08:00
|
|
|
{
|
|
|
|
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;
|
2015-03-12 12:46:45 -07:00
|
|
|
case AVCOL_SPC_SMPTE170M:
|
2015-03-04 10:45:50 -08:00
|
|
|
case AVCOL_SPC_BT470BG: obs_cs = VIDEO_CS_601; break;
|
|
|
|
case AVCOL_SPC_UNSPECIFIED: obs_cs = VIDEO_CS_DEFAULT; break;
|
|
|
|
default:
|
2015-08-03 13:40:40 -07:00
|
|
|
FF_BLOG(LOG_WARNING, "frame using an unsupported colorspace %d",
|
2015-03-04 10:45:50 -08:00
|
|
|
frame_cs);
|
2015-03-12 12:50:58 -07:00
|
|
|
obs_cs = VIDEO_CS_DEFAULT;
|
2015-03-04 10:45:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2015-08-03 13:40:40 -07:00
|
|
|
FF_BLOG(LOG_ERROR, "Failed to get video format "
|
2015-03-04 10:45:50 -08:00
|
|
|
"parameters for video format %u",
|
|
|
|
obs_cs);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:28:46 -07:00
|
|
|
bool update_sws_context(struct ffmpeg_source *s, AVFrame *frame)
|
2015-03-10 10:53:17 -07:00
|
|
|
{
|
2015-08-03 13:28:46 -07:00
|
|
|
if (frame->width != s->sws_width
|
|
|
|
|| frame->height != s->sws_height
|
|
|
|
|| frame->format != s->sws_format) {
|
|
|
|
if (s->sws_ctx != NULL)
|
|
|
|
sws_freeContext(s->sws_ctx);
|
2015-03-22 21:03:28 -07:00
|
|
|
|
|
|
|
if (frame->width <= 0 || frame->height <= 0) {
|
2015-08-03 13:40:40 -07:00
|
|
|
FF_BLOG(LOG_ERROR, "unable to create a sws "
|
2015-03-22 21:03:28 -07:00
|
|
|
"context that has a width(%d) or "
|
|
|
|
"height(%d) of zero.", frame->width,
|
|
|
|
frame->height);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:28:46 -07:00
|
|
|
s->sws_ctx = sws_getContext(
|
2015-03-10 10:53:17 -07:00
|
|
|
frame->width,
|
|
|
|
frame->height,
|
|
|
|
frame->format,
|
|
|
|
frame->width,
|
|
|
|
frame->height,
|
|
|
|
AV_PIX_FMT_BGRA,
|
|
|
|
SWS_BILINEAR,
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
|
2015-08-03 13:28:46 -07:00
|
|
|
if (s->sws_ctx == NULL) {
|
2015-08-03 13:40:40 -07:00
|
|
|
FF_BLOG(LOG_ERROR, "unable to create sws "
|
2015-03-22 21:03:28 -07:00
|
|
|
"context with src{w:%d,h:%d,f:%d}->"
|
|
|
|
"dst{w:%d,h:%d,f:%d}",
|
|
|
|
frame->width, frame->height,
|
|
|
|
frame->format, frame->width,
|
|
|
|
frame->height, AV_PIX_FMT_BGRA);
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:28:46 -07:00
|
|
|
if (s->sws_data != NULL)
|
|
|
|
bfree(s->sws_data);
|
|
|
|
s->sws_data = bzalloc(frame->width * frame->height * 4);
|
|
|
|
if (s->sws_data == NULL) {
|
2015-08-03 13:40:40 -07:00
|
|
|
FF_BLOG(LOG_ERROR, "unable to allocate sws "
|
2015-03-22 21:03:28 -07:00
|
|
|
"pixel data with size %d",
|
|
|
|
frame->width * frame->height * 4);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:28:46 -07:00
|
|
|
s->sws_linesize = frame->width * 4;
|
|
|
|
s->sws_width = frame->width;
|
|
|
|
s->sws_height = frame->height;
|
|
|
|
s->sws_format = frame->format;
|
2015-03-10 12:37:28 -07:00
|
|
|
}
|
|
|
|
|
2015-03-22 21:03:28 -07:00
|
|
|
return true;
|
|
|
|
|
|
|
|
fail:
|
2015-08-03 13:28:46 -07:00
|
|
|
if (s->sws_ctx != NULL)
|
|
|
|
sws_freeContext(s->sws_ctx);
|
|
|
|
s->sws_ctx = NULL;
|
|
|
|
|
|
|
|
if (s->sws_data)
|
|
|
|
bfree(s->sws_data);
|
|
|
|
s->sws_data = NULL;
|
|
|
|
|
|
|
|
s->sws_linesize = 0;
|
|
|
|
s->sws_width = 0;
|
|
|
|
s->sws_height = 0;
|
|
|
|
s->sws_format = 0;
|
2015-03-10 12:37:28 -07:00
|
|
|
|
2015-03-22 21:03:28 -07:00
|
|
|
return false;
|
2015-03-10 10:53:17 -07:00
|
|
|
}
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
static bool video_frame_scale(struct ff_frame *frame,
|
|
|
|
struct ffmpeg_source *s, struct obs_source_frame *obs_frame)
|
|
|
|
{
|
2015-03-10 12:33:21 -07:00
|
|
|
if (!update_sws_context(s, frame->frame))
|
|
|
|
return false;
|
2015-03-10 10:53:17 -07:00
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
sws_scale(
|
|
|
|
s->sws_ctx,
|
|
|
|
(uint8_t const *const *)frame->frame->data,
|
|
|
|
frame->frame->linesize,
|
|
|
|
0,
|
|
|
|
frame->frame->height,
|
2015-03-10 12:37:28 -07:00
|
|
|
&s->sws_data,
|
|
|
|
&s->sws_linesize
|
2015-03-04 10:45:50 -08:00
|
|
|
);
|
|
|
|
|
2015-03-10 12:37:28 -07:00
|
|
|
obs_frame->data[0] = s->sws_data;
|
|
|
|
obs_frame->linesize[0] = s->sws_linesize;
|
2015-03-04 10:45:50 -08:00
|
|
|
obs_frame->format = VIDEO_FORMAT_BGRA;
|
|
|
|
|
|
|
|
obs_source_output_video(s->source, obs_frame);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool video_frame_hwaccel(struct ff_frame *frame,
|
2015-08-03 13:26:27 -07:00
|
|
|
struct ffmpeg_source *s, struct obs_source_frame *obs_frame)
|
2015-03-04 10:45:50 -08:00
|
|
|
{
|
|
|
|
// 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];
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:40:40 -07:00
|
|
|
if (!set_obs_frame_colorprops(frame, s, obs_frame))
|
2015-03-04 10:45:50 -08:00
|
|
|
return false;
|
|
|
|
|
2015-08-03 13:26:27 -07:00
|
|
|
obs_source_output_video(s->source, obs_frame);
|
2015-03-04 10:45:50 -08:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:40:40 -07:00
|
|
|
if (!set_obs_frame_colorprops(frame, s, obs_frame))
|
2015-03-04 10:45:50 -08:00
|
|
|
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};
|
2015-03-23 22:12:10 -07:00
|
|
|
uint64_t pts;
|
|
|
|
|
2015-03-23 22:21:24 -07:00
|
|
|
// Media ended
|
2015-03-23 22:16:35 -07:00
|
|
|
if (frame == NULL) {
|
2015-03-23 22:21:24 -07:00
|
|
|
if (s->is_clear_on_media_end)
|
|
|
|
obs_source_output_video(s->source, NULL);
|
2015-03-23 22:16:35 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-10 09:41:42 -07:00
|
|
|
pts = (uint64_t)(frame->pts * 1000000000.0L);
|
2015-03-04 10:45:50 -08:00
|
|
|
|
|
|
|
obs_frame.timestamp = pts;
|
|
|
|
obs_frame.width = frame->frame->width;
|
|
|
|
obs_frame.height = frame->frame->height;
|
|
|
|
|
2015-03-10 10:53:17 -07:00
|
|
|
enum video_format format =
|
|
|
|
ffmpeg_to_obs_video_format(frame->frame->format);
|
|
|
|
|
|
|
|
if (s->is_forcing_scale || format == VIDEO_FORMAT_NONE)
|
2015-03-04 10:45:50 -08:00
|
|
|
return video_frame_scale(frame, s, &obs_frame);
|
|
|
|
else if (s->is_hw_decoding)
|
2015-08-03 13:26:27 -07:00
|
|
|
return video_frame_hwaccel(frame, s, &obs_frame);
|
2015-03-04 10:45:50 -08:00
|
|
|
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};
|
|
|
|
|
2015-03-23 22:12:10 -07:00
|
|
|
uint64_t pts;
|
|
|
|
|
2015-03-23 22:21:24 -07:00
|
|
|
// Media ended
|
2015-03-23 22:16:35 -07:00
|
|
|
if (frame == NULL)
|
|
|
|
return true;
|
|
|
|
|
2015-06-10 09:41:42 -07:00
|
|
|
pts = (uint64_t)(frame->pts * 1000000000.0L);
|
2015-03-04 10:45:50 -08:00
|
|
|
|
2015-03-30 08:56:43 -07:00
|
|
|
int channels = av_frame_get_channels(frame->frame);
|
2015-03-04 10:45:50 -08:00
|
|
|
|
|
|
|
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 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");
|
2015-03-12 14:17:13 -07:00
|
|
|
obs_property_t *frame_drop = obs_properties_get(props, "frame_drop");
|
2015-03-04 10:45:50 -08:00
|
|
|
obs_property_set_visible(abuf, enabled);
|
|
|
|
obs_property_set_visible(vbuf, enabled);
|
2015-03-12 14:17:13 -07:00
|
|
|
obs_property_set_visible(frame_drop, enabled);
|
2015-03-04 10:45:50 -08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-23 02:28:36 -08:00
|
|
|
static const char *media_filter =
|
|
|
|
" (*.mp4 *.ts *.mov *.flv *.mkv *.avi *.mp3 *.ogg *.aac *.wav *.gif *.webm);;";
|
|
|
|
static const char *video_filter =
|
|
|
|
" (*.mp4 *.ts *.mov *.flv *.mkv *.avi *.gif *.webm);;";
|
|
|
|
static const char *audio_filter =
|
|
|
|
" (*.mp3 *.aac *.ogg *.wav);;";
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
static obs_properties_t *ffmpeg_source_getproperties(void *data)
|
|
|
|
{
|
2016-01-23 02:28:36 -08:00
|
|
|
struct dstr filter = {0};
|
2015-03-04 10:45:50 -08:00
|
|
|
UNUSED_PARAMETER(data);
|
|
|
|
|
|
|
|
obs_properties_t *props = obs_properties_create();
|
2015-03-19 14:01:10 -07:00
|
|
|
|
|
|
|
obs_properties_set_flags(props, OBS_PROPERTIES_DEFER_UPDATE);
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
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);
|
|
|
|
|
2016-01-23 02:28:36 -08:00
|
|
|
dstr_copy(&filter, obs_module_text("MediaFileFilter.AllMediaFiles"));
|
|
|
|
dstr_cat(&filter, media_filter);
|
|
|
|
dstr_cat(&filter, obs_module_text("MediaFileFilter.VideoFiles"));
|
|
|
|
dstr_cat(&filter, video_filter);
|
|
|
|
dstr_cat(&filter, obs_module_text("MediaFileFilter.AudioFiles"));
|
|
|
|
dstr_cat(&filter, audio_filter);
|
|
|
|
dstr_cat(&filter, obs_module_text("MediaFileFilter.AllFiles"));
|
|
|
|
dstr_cat(&filter, " (*.*)");
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
obs_properties_add_path(props, "local_file",
|
2016-01-23 02:28:36 -08:00
|
|
|
obs_module_text("LocalFile"), OBS_PATH_FILE,
|
|
|
|
filter.array, NULL);
|
|
|
|
dstr_free(&filter);
|
2015-03-04 10:45:50 -08:00
|
|
|
|
|
|
|
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"));
|
|
|
|
|
2015-03-23 22:21:24 -07:00
|
|
|
obs_properties_add_bool(props, "clear_on_media_end",
|
|
|
|
obs_module_text("ClearOnMediaEnd"));
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
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);
|
|
|
|
|
2015-03-12 14:17:13 -07:00
|
|
|
prop = obs_properties_add_list(props, "frame_drop",
|
|
|
|
obs_module_text("FrameDropping"), OBS_COMBO_TYPE_LIST,
|
|
|
|
OBS_COMBO_FORMAT_INT);
|
|
|
|
|
|
|
|
obs_property_list_add_int(prop, obs_module_text("DiscardNone"),
|
|
|
|
AVDISCARD_NONE);
|
|
|
|
obs_property_list_add_int(prop, obs_module_text("DiscardDefault"),
|
|
|
|
AVDISCARD_DEFAULT);
|
|
|
|
obs_property_list_add_int(prop, obs_module_text("DiscardNonRef"),
|
|
|
|
AVDISCARD_NONREF);
|
|
|
|
obs_property_list_add_int(prop, obs_module_text("DiscardBiDir"),
|
|
|
|
AVDISCARD_BIDIR);
|
2015-04-01 03:50:22 -07:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 67, 100)
|
2015-03-12 14:17:13 -07:00
|
|
|
obs_property_list_add_int(prop, obs_module_text("DiscardNonIntra"),
|
|
|
|
AVDISCARD_NONINTRA);
|
2015-04-01 03:50:22 -07:00
|
|
|
#endif
|
2015-03-12 14:17:13 -07:00
|
|
|
obs_property_list_add_int(prop, obs_module_text("DiscardNonKey"),
|
|
|
|
AVDISCARD_NONKEY);
|
|
|
|
obs_property_list_add_int(prop, obs_module_text("DiscardAll"),
|
|
|
|
AVDISCARD_ALL);
|
|
|
|
|
|
|
|
obs_property_set_visible(prop, false);
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|
2015-08-03 14:27:34 -07:00
|
|
|
static const char *frame_drop_to_str(enum AVDiscard discard)
|
|
|
|
{
|
|
|
|
#define DISCARD_CASE(x) case AVDISCARD_ ## x: return "AVDISCARD_" #x
|
|
|
|
switch (discard)
|
|
|
|
{
|
|
|
|
DISCARD_CASE(NONE);
|
|
|
|
DISCARD_CASE(DEFAULT);
|
|
|
|
DISCARD_CASE(NONREF);
|
|
|
|
DISCARD_CASE(BIDIR);
|
2015-08-31 08:51:37 -07:00
|
|
|
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 67, 100)
|
2015-08-03 14:27:34 -07:00
|
|
|
DISCARD_CASE(NONINTRA);
|
2015-08-31 08:51:37 -07:00
|
|
|
#endif
|
2015-08-03 14:27:34 -07:00
|
|
|
DISCARD_CASE(NONKEY);
|
|
|
|
DISCARD_CASE(ALL);
|
|
|
|
default: return "(Unknown)";
|
|
|
|
};
|
|
|
|
#undef DISCARD_CASE
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_source_info(struct ffmpeg_source *s, const char *input,
|
|
|
|
const char *input_format, bool is_advanced)
|
|
|
|
{
|
|
|
|
FF_BLOG(LOG_INFO,
|
|
|
|
"settings:\n"
|
|
|
|
"\tinput: %s\n"
|
|
|
|
"\tinput_format: %s\n"
|
|
|
|
"\tis_looping: %s\n"
|
|
|
|
"\tis_forcing_scale: %s\n"
|
|
|
|
"\tis_hw_decoding: %s\n"
|
|
|
|
"\tis_clear_on_media_end: %s",
|
|
|
|
input ? input : "(null)",
|
|
|
|
input_format ? input_format : "(null)",
|
|
|
|
s->demuxer->options.is_looping ? "yes" : "no",
|
|
|
|
s->is_forcing_scale ? "yes" : "no",
|
|
|
|
s->is_hw_decoding ? "yes" : "no",
|
|
|
|
s->is_clear_on_media_end ? "yes" : "no");
|
|
|
|
|
|
|
|
if (!is_advanced)
|
|
|
|
return;
|
|
|
|
|
|
|
|
FF_BLOG(LOG_INFO,
|
|
|
|
"advanced settings:\n"
|
|
|
|
"\taudio_buffer_size: %d\n"
|
|
|
|
"\tvideo_buffer_size: %d\n"
|
|
|
|
"\tframe_drop: %s",
|
|
|
|
s->demuxer->options.audio_frame_queue_size,
|
|
|
|
s->demuxer->options.video_frame_queue_size,
|
|
|
|
frame_drop_to_str(s->demuxer->options.frame_drop));
|
|
|
|
}
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
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) {
|
2015-03-10 10:18:19 -07:00
|
|
|
input = (char *)obs_data_get_string(settings, "local_file");
|
2015-03-04 10:45:50 -08:00
|
|
|
input_format = NULL;
|
2015-03-10 10:18:19 -07:00
|
|
|
is_looping = obs_data_get_bool(settings, "looping");
|
2015-03-04 10:45:50 -08:00
|
|
|
} else {
|
2015-03-10 10:18:19 -07:00
|
|
|
input = (char *)obs_data_get_string(settings, "input");
|
|
|
|
input_format = (char *)obs_data_get_string(settings,
|
2015-03-04 10:45:50 -08:00
|
|
|
"input_format");
|
|
|
|
is_looping = false;
|
|
|
|
}
|
|
|
|
|
2015-03-10 10:18:19 -07:00
|
|
|
s->is_forcing_scale = obs_data_get_bool(settings, "force_scale");
|
|
|
|
s->is_hw_decoding = obs_data_get_bool(settings, "hw_decode");
|
2015-03-23 22:21:24 -07:00
|
|
|
s->is_clear_on_media_end = obs_data_get_bool(settings,
|
|
|
|
"clear_on_media_end");
|
2015-03-04 10:45:50 -08:00
|
|
|
|
|
|
|
if (s->demuxer != NULL)
|
|
|
|
ff_demuxer_free(s->demuxer);
|
|
|
|
|
|
|
|
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");
|
2015-03-12 14:17:13 -07:00
|
|
|
enum AVDiscard frame_drop =
|
|
|
|
(enum AVDiscard)obs_data_get_int(settings,
|
|
|
|
"frame_drop");
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
if (audio_buffer_size < 1) {
|
|
|
|
audio_buffer_size = 1;
|
2015-08-03 13:40:40 -07:00
|
|
|
FF_BLOG(LOG_WARNING, "invalid audio_buffer_size %d",
|
2015-03-04 10:45:50 -08:00
|
|
|
audio_buffer_size);
|
|
|
|
}
|
|
|
|
if (video_buffer_size < 1) {
|
|
|
|
video_buffer_size = 1;
|
2015-08-03 13:40:40 -07:00
|
|
|
FF_BLOG(LOG_WARNING, "invalid audio_buffer_size %d",
|
2015-03-04 10:45:50 -08:00
|
|
|
audio_buffer_size);
|
|
|
|
}
|
|
|
|
s->demuxer->options.audio_frame_queue_size = audio_buffer_size;
|
|
|
|
s->demuxer->options.video_frame_queue_size = video_buffer_size;
|
2015-03-12 14:17:13 -07:00
|
|
|
|
|
|
|
if (frame_drop < AVDISCARD_NONE || frame_drop > AVDISCARD_ALL) {
|
|
|
|
frame_drop = AVDISCARD_NONE;
|
2015-08-03 13:40:40 -07:00
|
|
|
FF_BLOG(LOG_WARNING, "invalid frame_drop %d",
|
|
|
|
frame_drop);
|
2015-03-12 14:17:13 -07:00
|
|
|
}
|
|
|
|
s->demuxer->options.frame_drop = frame_drop;
|
2015-03-04 10:45:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
ff_demuxer_set_callbacks(&s->demuxer->video_callbacks,
|
2015-03-10 10:53:17 -07:00
|
|
|
video_frame, NULL,
|
2015-03-04 10:45:50 -08:00
|
|
|
NULL, NULL, NULL, s);
|
|
|
|
|
|
|
|
ff_demuxer_set_callbacks(&s->demuxer->audio_callbacks,
|
|
|
|
audio_frame, NULL,
|
|
|
|
NULL, NULL, NULL, s);
|
|
|
|
|
2015-08-03 14:27:34 -07:00
|
|
|
dump_source_info(s, input, input_format, is_advanced);
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
ff_demuxer_open(s->demuxer, input, input_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-16 01:30:51 -07:00
|
|
|
static const char *ffmpeg_source_getname(void *unused)
|
2015-03-04 10:45:50 -08:00
|
|
|
{
|
2015-09-16 01:30:51 -07:00
|
|
|
UNUSED_PARAMETER(unused);
|
2015-03-04 10:45:50 -08:00
|
|
|
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);
|
2015-03-10 10:53:17 -07:00
|
|
|
|
2015-03-10 12:34:37 -07:00
|
|
|
if (s->sws_ctx != NULL)
|
2015-03-10 10:53:17 -07:00
|
|
|
sws_freeContext(s->sws_ctx);
|
2015-03-10 12:37:28 -07:00
|
|
|
if (s->sws_data != NULL)
|
|
|
|
bfree(s->sws_data);
|
2015-03-10 10:53:17 -07:00
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
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
|
|
|
|
};
|