yolobs-studio/plugins/obs-ffmpeg/obs-ffmpeg-output.c

1201 lines
31 KiB
C
Raw Normal View History

2016-02-23 23:16:51 +00:00
/******************************************************************************
Copyright (C) 2014 by Hugh Bailey <obs.jim@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include <obs-module.h>
#include <util/circlebuf.h>
#include <util/threading.h>
#include <util/dstr.h>
#include <util/darray.h>
#include <util/platform.h>
2020-03-25 08:07:22 +00:00
#include "obs-ffmpeg-output.h"
2016-02-23 23:16:51 +00:00
#include "obs-ffmpeg-formats.h"
#include "obs-ffmpeg-compat.h"
struct ffmpeg_output {
2019-09-22 21:19:10 +00:00
obs_output_t *output;
volatile bool active;
2016-02-23 23:16:51 +00:00
struct ffmpeg_data ff_data;
2019-09-22 21:19:10 +00:00
bool connecting;
pthread_t start_thread;
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
uint64_t total_bytes;
2017-06-29 19:01:10 +00:00
2019-09-22 21:19:10 +00:00
uint64_t audio_start_ts;
uint64_t video_start_ts;
uint64_t stop_ts;
volatile bool stopping;
2016-08-28 12:07:43 +00:00
2019-09-22 21:19:10 +00:00
bool write_thread_active;
pthread_mutex_t write_mutex;
pthread_t write_thread;
os_sem_t *write_sem;
os_event_t *stop_event;
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
DARRAY(AVPacket) packets;
2016-02-23 23:16:51 +00:00
};
/* ------------------------------------------------------------------------- */
2019-07-27 12:47:10 +00:00
static void ffmpeg_output_set_last_error(struct ffmpeg_data *data,
2019-09-22 21:19:10 +00:00
const char *error)
2019-07-27 12:47:10 +00:00
{
if (data->last_error)
bfree(data->last_error);
data->last_error = bstrdup(error);
}
void ffmpeg_log_error(int log_level, struct ffmpeg_data *data,
2019-09-22 21:19:10 +00:00
const char *format, ...)
2019-07-27 12:47:10 +00:00
{
va_list args;
char out[4096];
va_start(args, format);
vsnprintf(out, sizeof(out), format, args);
va_end(args);
ffmpeg_output_set_last_error(data, out);
blog(log_level, "%s", out);
}
2016-02-23 23:16:51 +00:00
static bool new_stream(struct ffmpeg_data *data, AVStream **stream,
2019-09-22 21:19:10 +00:00
AVCodec **codec, enum AVCodecID id, const char *name)
2016-02-23 23:16:51 +00:00
{
2019-09-22 21:19:10 +00:00
*codec = (!!name && *name) ? avcodec_find_encoder_by_name(name)
: avcodec_find_encoder(id);
2016-02-23 23:16:51 +00:00
if (!*codec) {
2019-09-22 21:19:10 +00:00
ffmpeg_log_error(LOG_WARNING, data,
"Couldn't find encoder '%s'",
avcodec_get_name(id));
2016-02-23 23:16:51 +00:00
return false;
}
*stream = avformat_new_stream(data->output, *codec);
if (!*stream) {
2019-09-22 21:19:10 +00:00
ffmpeg_log_error(LOG_WARNING, data,
"Couldn't create stream for encoder '%s'",
avcodec_get_name(id));
2016-02-23 23:16:51 +00:00
return false;
}
2019-09-22 21:19:10 +00:00
(*stream)->id = data->output->nb_streams - 1;
2016-02-23 23:16:51 +00:00
return true;
}
2017-04-19 19:54:15 +00:00
static bool parse_params(AVCodecContext *context, char **opts)
2016-02-23 23:16:51 +00:00
{
2017-04-19 19:54:15 +00:00
bool ret = true;
2016-02-23 23:16:51 +00:00
if (!context || !context->priv_data)
2017-04-19 19:54:15 +00:00
return true;
2016-02-23 23:16:51 +00:00
while (*opts) {
char *opt = *opts;
char *assign = strchr(opt, '=');
if (assign) {
char *name = opt;
char *value;
*assign = 0;
2019-09-22 21:19:10 +00:00
value = assign + 1;
2016-02-23 23:16:51 +00:00
2019-12-10 19:31:54 +00:00
if (av_opt_set(context, name, value,
AV_OPT_SEARCH_CHILDREN)) {
2019-09-22 21:19:10 +00:00
blog(LOG_WARNING, "Failed to set %s=%s", name,
value);
2017-04-19 19:54:15 +00:00
ret = false;
}
2016-02-23 23:16:51 +00:00
}
opts++;
}
2017-04-19 19:54:15 +00:00
return ret;
2016-02-23 23:16:51 +00:00
}
static bool open_video_codec(struct ffmpeg_data *data)
{
AVCodecContext *context = data->video->codec;
char **opts = strlist_split(data->config.video_settings, ' ', false);
int ret;
if (strcmp(data->vcodec->name, "libx264") == 0)
av_opt_set(context->priv_data, "preset", "veryfast", 0);
if (opts) {
2017-04-19 19:54:15 +00:00
// libav requires x264 parameters in a special format which may be non-obvious
2019-09-22 21:19:10 +00:00
if (!parse_params(context, opts) &&
strcmp(data->vcodec->name, "libx264") == 0)
blog(LOG_WARNING,
"If you're trying to set x264 parameters, use x264-params=name=value:name=value");
2016-02-23 23:16:51 +00:00
strlist_free(opts);
}
ret = avcodec_open2(context, data->vcodec, NULL);
if (ret < 0) {
2019-09-22 21:19:10 +00:00
ffmpeg_log_error(LOG_WARNING, data,
"Failed to open video codec: %s",
av_err2str(ret));
2016-02-23 23:16:51 +00:00
return false;
}
data->vframe = av_frame_alloc();
if (!data->vframe) {
2019-09-22 21:19:10 +00:00
ffmpeg_log_error(LOG_WARNING, data,
"Failed to allocate video frame");
2016-02-23 23:16:51 +00:00
return false;
}
data->vframe->format = context->pix_fmt;
2019-09-22 21:19:10 +00:00
data->vframe->width = context->width;
2016-02-23 23:16:51 +00:00
data->vframe->height = context->height;
data->vframe->colorspace = data->config.color_space;
data->vframe->color_range = data->config.color_range;
2018-02-19 19:54:37 +00:00
ret = av_frame_get_buffer(data->vframe, base_get_alignment());
2016-02-23 23:16:51 +00:00
if (ret < 0) {
2019-09-22 21:19:10 +00:00
ffmpeg_log_error(LOG_WARNING, data,
"Failed to allocate vframe: %s",
av_err2str(ret));
2016-02-23 23:16:51 +00:00
return false;
}
return true;
}
static bool init_swscale(struct ffmpeg_data *data, AVCodecContext *context)
{
data->swscale = sws_getContext(
2019-09-22 21:19:10 +00:00
data->config.width, data->config.height, data->config.format,
data->config.scale_width, data->config.scale_height,
context->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);
2016-02-23 23:16:51 +00:00
if (!data->swscale) {
2019-09-22 21:19:10 +00:00
ffmpeg_log_error(LOG_WARNING, data,
"Could not initialize swscale");
2016-02-23 23:16:51 +00:00
return false;
}
return true;
}
static bool create_video_stream(struct ffmpeg_data *data)
{
enum AVPixelFormat closest_format;
AVCodecContext *context;
struct obs_video_info ovi;
if (!obs_get_video_info(&ovi)) {
2019-07-27 12:47:10 +00:00
ffmpeg_log_error(LOG_WARNING, data, "No active video");
2016-02-23 23:16:51 +00:00
return false;
}
if (!new_stream(data, &data->video, &data->vcodec,
2019-09-22 21:19:10 +00:00
data->output->oformat->video_codec,
data->config.video_encoder))
2016-02-23 23:16:51 +00:00
return false;
2020-03-25 08:07:22 +00:00
closest_format = avcodec_find_best_pix_fmt_of_list(
data->vcodec->pix_fmts, data->config.format, 0, NULL);
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
context = data->video->codec;
2020-03-25 08:07:22 +00:00
context->bit_rate = (int64_t)data->config.video_bitrate * 1000;
2019-09-22 21:19:10 +00:00
context->width = data->config.scale_width;
context->height = data->config.scale_height;
context->time_base = (AVRational){ovi.fps_den, ovi.fps_num};
context->gop_size = data->config.gop_size;
context->pix_fmt = closest_format;
context->colorspace = data->config.color_space;
context->color_range = data->config.color_range;
context->thread_count = 0;
2016-02-23 23:16:51 +00:00
data->video->time_base = context->time_base;
if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
2018-02-19 19:54:37 +00:00
context->flags |= CODEC_FLAG_GLOBAL_H;
2016-02-23 23:16:51 +00:00
if (!open_video_codec(data))
return false;
2019-09-22 21:19:10 +00:00
if (context->pix_fmt != data->config.format ||
data->config.width != data->config.scale_width ||
2016-02-23 23:16:51 +00:00
data->config.height != data->config.scale_height) {
if (!init_swscale(data, context))
return false;
}
return true;
}
2019-07-27 12:47:10 +00:00
static bool open_audio_codec(struct ffmpeg_data *data, int idx)
2016-02-23 23:16:51 +00:00
{
2019-07-27 12:47:10 +00:00
AVCodecContext *context = data->audio_streams[idx]->codec;
2017-04-19 19:54:15 +00:00
char **opts = strlist_split(data->config.audio_settings, ' ', false);
2016-02-23 23:16:51 +00:00
int ret;
if (opts) {
parse_params(context, opts);
strlist_free(opts);
}
2019-07-27 12:47:10 +00:00
data->aframe[idx] = av_frame_alloc();
if (!data->aframe[idx]) {
2019-09-22 21:19:10 +00:00
ffmpeg_log_error(LOG_WARNING, data,
"Failed to allocate audio frame");
2016-02-23 23:16:51 +00:00
return false;
}
2019-07-27 12:47:10 +00:00
data->aframe[idx]->format = context->sample_fmt;
data->aframe[idx]->channels = context->channels;
data->aframe[idx]->channel_layout = context->channel_layout;
data->aframe[idx]->sample_rate = context->sample_rate;
2018-12-16 16:14:58 +00:00
2016-02-23 23:16:51 +00:00
context->strict_std_compliance = -2;
ret = avcodec_open2(context, data->acodec, NULL);
if (ret < 0) {
2019-09-22 21:19:10 +00:00
ffmpeg_log_error(LOG_WARNING, data,
"Failed to open audio codec: %s",
av_err2str(ret));
2016-02-23 23:16:51 +00:00
return false;
}
data->frame_size = context->frame_size ? context->frame_size : 1024;
2019-07-27 12:47:10 +00:00
ret = av_samples_alloc(data->samples[idx], NULL, context->channels,
2019-09-22 21:19:10 +00:00
data->frame_size, context->sample_fmt, 0);
2016-02-23 23:16:51 +00:00
if (ret < 0) {
2019-09-22 21:19:10 +00:00
ffmpeg_log_error(LOG_WARNING, data,
"Failed to create audio buffer: %s",
av_err2str(ret));
2016-02-23 23:16:51 +00:00
return false;
}
return true;
}
2019-07-27 12:47:10 +00:00
static bool create_audio_stream(struct ffmpeg_data *data, int idx)
2016-02-23 23:16:51 +00:00
{
AVCodecContext *context;
2019-07-27 12:47:10 +00:00
AVStream *stream;
2016-02-23 23:16:51 +00:00
struct obs_audio_info aoi;
if (!obs_get_audio_info(&aoi)) {
2019-07-27 12:47:10 +00:00
ffmpeg_log_error(LOG_WARNING, data, "No active audio");
2016-02-23 23:16:51 +00:00
return false;
}
2019-07-27 12:47:10 +00:00
if (!new_stream(data, &stream, &data->acodec,
2019-09-22 21:19:10 +00:00
data->output->oformat->audio_codec,
data->config.audio_encoder))
2016-02-23 23:16:51 +00:00
return false;
2019-07-27 12:47:10 +00:00
data->audio_streams[idx] = stream;
2019-09-22 21:19:10 +00:00
context = data->audio_streams[idx]->codec;
2020-03-25 08:07:22 +00:00
context->bit_rate = (int64_t)data->config.audio_bitrate * 1000;
2019-09-22 21:19:10 +00:00
context->time_base = (AVRational){1, aoi.samples_per_sec};
context->channels = get_audio_channels(aoi.speakers);
context->sample_rate = aoi.samples_per_sec;
context->channel_layout =
av_get_default_channel_layout(context->channels);
2018-02-19 19:54:37 +00:00
//AVlib default channel layout for 5 channels is 5.0 ; fix for 4.1
if (aoi.speakers == SPEAKERS_4POINT1)
context->channel_layout = av_get_channel_layout("4.1");
2019-09-22 21:19:10 +00:00
context->sample_fmt = data->acodec->sample_fmts
? data->acodec->sample_fmts[0]
: AV_SAMPLE_FMT_FLTP;
2016-02-23 23:16:51 +00:00
2019-07-27 12:47:10 +00:00
data->audio_streams[idx]->time_base = context->time_base;
2016-02-23 23:16:51 +00:00
data->audio_samplerate = aoi.samples_per_sec;
data->audio_format = convert_ffmpeg_sample_format(context->sample_fmt);
data->audio_planes = get_audio_planes(data->audio_format, aoi.speakers);
data->audio_size = get_audio_size(data->audio_format, aoi.speakers, 1);
if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
2018-02-19 19:54:37 +00:00
context->flags |= CODEC_FLAG_GLOBAL_H;
2016-02-23 23:16:51 +00:00
2019-07-27 12:47:10 +00:00
return open_audio_codec(data, idx);
2016-02-23 23:16:51 +00:00
}
static inline bool init_streams(struct ffmpeg_data *data)
{
AVOutputFormat *format = data->output->oformat;
if (format->video_codec != AV_CODEC_ID_NONE)
if (!create_video_stream(data))
return false;
2019-09-22 21:19:10 +00:00
if (format->audio_codec != AV_CODEC_ID_NONE &&
data->num_audio_streams) {
data->audio_streams =
calloc(1, data->num_audio_streams * sizeof(void *));
2019-07-27 12:47:10 +00:00
for (int i = 0; i < data->num_audio_streams; i++) {
if (!create_audio_stream(data, i))
return false;
}
}
2016-02-23 23:16:51 +00:00
return true;
}
static inline bool open_output_file(struct ffmpeg_data *data)
{
AVOutputFormat *format = data->output->oformat;
int ret;
AVDictionary *dict = NULL;
2019-09-22 21:19:10 +00:00
if ((ret = av_dict_parse_string(&dict, data->config.muxer_settings, "=",
" ", 0))) {
ffmpeg_log_error(LOG_WARNING, data,
"Failed to parse muxer settings: %s\n%s",
av_err2str(ret), data->config.muxer_settings);
2016-02-23 23:16:51 +00:00
av_dict_free(&dict);
return false;
}
if (av_dict_count(dict) > 0) {
struct dstr str = {0};
AVDictionaryEntry *entry = NULL;
while ((entry = av_dict_get(dict, "", entry,
2019-09-22 21:19:10 +00:00
AV_DICT_IGNORE_SUFFIX)))
2016-02-23 23:16:51 +00:00
dstr_catf(&str, "\n\t%s=%s", entry->key, entry->value);
2018-02-19 19:54:37 +00:00
blog(LOG_INFO, "Using muxer settings: %s", str.array);
2016-02-23 23:16:51 +00:00
dstr_free(&str);
}
2018-02-19 19:54:37 +00:00
if ((format->flags & AVFMT_NOFILE) == 0) {
ret = avio_open2(&data->output->pb, data->config.url,
2019-09-22 21:19:10 +00:00
AVIO_FLAG_WRITE, NULL, &dict);
2018-02-19 19:54:37 +00:00
if (ret < 0) {
2019-07-27 12:47:10 +00:00
ffmpeg_log_error(LOG_WARNING, data,
2019-09-22 21:19:10 +00:00
"Couldn't open '%s', %s",
data->config.url, av_err2str(ret));
2018-02-19 19:54:37 +00:00
av_dict_free(&dict);
return false;
}
}
strncpy(data->output->filename, data->config.url,
2019-09-22 21:19:10 +00:00
sizeof(data->output->filename));
2018-02-19 19:54:37 +00:00
data->output->filename[sizeof(data->output->filename) - 1] = 0;
2016-02-23 23:16:51 +00:00
ret = avformat_write_header(data->output, &dict);
if (ret < 0) {
2019-07-27 12:47:10 +00:00
ffmpeg_log_error(LOG_WARNING, data, "Error opening '%s': %s",
2019-09-22 21:19:10 +00:00
data->config.url, av_err2str(ret));
2016-02-23 23:16:51 +00:00
return false;
}
2018-02-19 19:54:37 +00:00
if (av_dict_count(dict) > 0) {
struct dstr str = {0};
AVDictionaryEntry *entry = NULL;
while ((entry = av_dict_get(dict, "", entry,
2019-09-22 21:19:10 +00:00
AV_DICT_IGNORE_SUFFIX)))
2018-02-19 19:54:37 +00:00
dstr_catf(&str, "\n\t%s=%s", entry->key, entry->value);
blog(LOG_INFO, "Invalid muxer settings: %s", str.array);
dstr_free(&str);
}
2016-02-23 23:16:51 +00:00
av_dict_free(&dict);
return true;
}
static void close_video(struct ffmpeg_data *data)
{
avcodec_close(data->video->codec);
2018-02-19 19:54:37 +00:00
av_frame_unref(data->vframe);
2016-02-23 23:16:51 +00:00
// This format for some reason derefs video frame
// too many times
if (data->vcodec->id == AV_CODEC_ID_A64_MULTI ||
data->vcodec->id == AV_CODEC_ID_A64_MULTI5)
return;
av_frame_free(&data->vframe);
}
static void close_audio(struct ffmpeg_data *data)
{
2019-07-27 12:47:10 +00:00
for (int idx = 0; idx < data->num_audio_streams; idx++) {
for (size_t i = 0; i < MAX_AV_PLANES; i++)
circlebuf_free(&data->excess_frames[idx][i]);
if (data->samples[idx][0])
av_freep(&data->samples[idx][0]);
if (data->audio_streams[idx])
avcodec_close(data->audio_streams[idx]->codec);
if (data->aframe[idx])
av_frame_free(&data->aframe[idx]);
}
2016-02-23 23:16:51 +00:00
}
2020-03-25 08:07:22 +00:00
void ffmpeg_data_free(struct ffmpeg_data *data)
2016-02-23 23:16:51 +00:00
{
if (data->initialized)
av_write_trailer(data->output);
if (data->video)
close_video(data);
2019-07-27 12:47:10 +00:00
if (data->audio_streams) {
2016-02-23 23:16:51 +00:00
close_audio(data);
2019-07-27 12:47:10 +00:00
free(data->audio_streams);
data->audio_streams = NULL;
}
2016-02-23 23:16:51 +00:00
if (data->output) {
if ((data->output->oformat->flags & AVFMT_NOFILE) == 0)
avio_close(data->output->pb);
avformat_free_context(data->output);
}
2019-07-27 12:47:10 +00:00
if (data->last_error)
bfree(data->last_error);
2016-02-23 23:16:51 +00:00
memset(data, 0, sizeof(struct ffmpeg_data));
}
static inline const char *safe_str(const char *s)
{
if (s == NULL)
return "(NULL)";
else
return s;
}
static enum AVCodecID get_codec_id(const char *name, int id)
{
AVCodec *codec;
if (id != 0)
return (enum AVCodecID)id;
if (!name || !*name)
return AV_CODEC_ID_NONE;
codec = avcodec_find_encoder_by_name(name);
if (!codec)
return AV_CODEC_ID_NONE;
return codec->id;
}
static void set_encoder_ids(struct ffmpeg_data *data)
{
data->output->oformat->video_codec = get_codec_id(
2019-09-22 21:19:10 +00:00
data->config.video_encoder, data->config.video_encoder_id);
2016-02-23 23:16:51 +00:00
data->output->oformat->audio_codec = get_codec_id(
2019-09-22 21:19:10 +00:00
data->config.audio_encoder, data->config.audio_encoder_id);
2016-02-23 23:16:51 +00:00
}
2020-03-25 08:07:22 +00:00
bool ffmpeg_data_init(struct ffmpeg_data *data, struct ffmpeg_cfg *config)
2016-02-23 23:16:51 +00:00
{
bool is_rtmp = false;
memset(data, 0, sizeof(struct ffmpeg_data));
data->config = *config;
2019-07-27 12:47:10 +00:00
data->num_audio_streams = config->audio_mix_count;
data->audio_tracks = config->audio_tracks;
2016-02-23 23:16:51 +00:00
if (!config->url || !*config->url)
return false;
2019-09-22 21:19:10 +00:00
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
2016-02-23 23:16:51 +00:00
av_register_all();
2019-09-22 21:19:10 +00:00
#endif
2016-02-23 23:16:51 +00:00
avformat_network_init();
is_rtmp = (astrcmpi_n(config->url, "rtmp://", 7) == 0);
AVOutputFormat *output_format = av_guess_format(
2019-09-22 21:19:10 +00:00
is_rtmp ? "flv" : data->config.format_name, data->config.url,
is_rtmp ? NULL : data->config.format_mime_type);
2016-02-23 23:16:51 +00:00
if (output_format == NULL) {
2019-09-22 21:19:10 +00:00
ffmpeg_log_error(
LOG_WARNING, data,
2019-07-27 12:47:10 +00:00
"Couldn't find matching output format with "
"parameters: name=%s, url=%s, mime=%s",
2019-09-22 21:19:10 +00:00
safe_str(is_rtmp ? "flv" : data->config.format_name),
2019-07-27 12:47:10 +00:00
safe_str(data->config.url),
2019-09-22 21:19:10 +00:00
safe_str(is_rtmp ? NULL
: data->config.format_mime_type));
2019-07-27 12:47:10 +00:00
2016-02-23 23:16:51 +00:00
goto fail;
}
2019-09-22 21:19:10 +00:00
avformat_alloc_output_context2(&data->output, output_format, NULL,
NULL);
2016-02-23 23:16:51 +00:00
2019-07-27 12:47:10 +00:00
if (!data->output) {
ffmpeg_log_error(LOG_WARNING, data,
2019-09-22 21:19:10 +00:00
"Couldn't create avformat context");
2019-07-27 12:47:10 +00:00
goto fail;
}
2016-02-23 23:16:51 +00:00
if (is_rtmp) {
data->output->oformat->video_codec = AV_CODEC_ID_H264;
data->output->oformat->audio_codec = AV_CODEC_ID_AAC;
} else {
if (data->config.format_name)
set_encoder_ids(data);
}
if (!init_streams(data))
goto fail;
if (!open_output_file(data))
goto fail;
av_dump_format(data->output, 0, NULL, 1);
data->initialized = true;
return true;
fail:
blog(LOG_WARNING, "ffmpeg_data_init failed");
return false;
}
/* ------------------------------------------------------------------------- */
2016-08-28 12:07:43 +00:00
static inline bool stopping(struct ffmpeg_output *output)
{
return os_atomic_load_bool(&output->stopping);
}
2016-02-23 23:16:51 +00:00
static const char *ffmpeg_output_getname(void *unused)
{
UNUSED_PARAMETER(unused);
return obs_module_text("FFmpegOutput");
}
static void ffmpeg_log_callback(void *param, int level, const char *format,
2019-09-22 21:19:10 +00:00
va_list args)
2016-02-23 23:16:51 +00:00
{
if (level <= AV_LOG_INFO)
blogva(LOG_DEBUG, format, args);
UNUSED_PARAMETER(param);
}
static void *ffmpeg_output_create(obs_data_t *settings, obs_output_t *output)
{
struct ffmpeg_output *data = bzalloc(sizeof(struct ffmpeg_output));
pthread_mutex_init_value(&data->write_mutex);
data->output = output;
if (pthread_mutex_init(&data->write_mutex, NULL) != 0)
goto fail;
if (os_event_init(&data->stop_event, OS_EVENT_TYPE_AUTO) != 0)
goto fail;
if (os_sem_init(&data->write_sem, 0) != 0)
goto fail;
av_log_set_callback(ffmpeg_log_callback);
UNUSED_PARAMETER(settings);
return data;
fail:
pthread_mutex_destroy(&data->write_mutex);
os_event_destroy(data->stop_event);
bfree(data);
return NULL;
}
2016-08-28 12:07:43 +00:00
static void ffmpeg_output_full_stop(void *data);
2016-02-23 23:16:51 +00:00
static void ffmpeg_deactivate(struct ffmpeg_output *output);
static void ffmpeg_output_destroy(void *data)
{
struct ffmpeg_output *output = data;
if (output) {
if (output->connecting)
pthread_join(output->start_thread, NULL);
2016-08-28 12:07:43 +00:00
ffmpeg_output_full_stop(output);
2016-02-23 23:16:51 +00:00
pthread_mutex_destroy(&output->write_mutex);
os_sem_destroy(output->write_sem);
os_event_destroy(output->stop_event);
bfree(data);
}
}
2018-02-19 19:54:37 +00:00
static inline void copy_data(AVFrame *pic, const struct video_data *frame,
2019-09-22 21:19:10 +00:00
int height, enum AVPixelFormat format)
2016-02-23 23:16:51 +00:00
{
2017-04-19 19:54:15 +00:00
int h_chroma_shift, v_chroma_shift;
2019-09-22 21:19:10 +00:00
av_pix_fmt_get_chroma_sub_sample(format, &h_chroma_shift,
&v_chroma_shift);
2016-02-23 23:16:51 +00:00
for (int plane = 0; plane < MAX_AV_PLANES; plane++) {
if (!frame->data[plane])
continue;
int frame_rowsize = (int)frame->linesize[plane];
2019-09-22 21:19:10 +00:00
int pic_rowsize = pic->linesize[plane];
int bytes = frame_rowsize < pic_rowsize ? frame_rowsize
: pic_rowsize;
2017-04-19 19:54:15 +00:00
int plane_height = height >> (plane ? v_chroma_shift : 0);
2016-02-23 23:16:51 +00:00
for (int y = 0; y < plane_height; y++) {
int pos_frame = y * frame_rowsize;
2019-09-22 21:19:10 +00:00
int pos_pic = y * pic_rowsize;
2016-02-23 23:16:51 +00:00
memcpy(pic->data[plane] + pos_pic,
2019-09-22 21:19:10 +00:00
frame->data[plane] + pos_frame, bytes);
2016-02-23 23:16:51 +00:00
}
}
}
static void receive_video(void *param, struct video_data *frame)
{
struct ffmpeg_output *output = param;
2019-09-22 21:19:10 +00:00
struct ffmpeg_data *data = &output->ff_data;
2016-02-23 23:16:51 +00:00
// codec doesn't support video or none configured
if (!data->video)
return;
AVCodecContext *context = data->video->codec;
AVPacket packet = {0};
int ret = 0, got_packet;
av_init_packet(&packet);
2016-08-28 12:07:43 +00:00
if (!output->video_start_ts)
output->video_start_ts = frame->timestamp;
2016-02-23 23:16:51 +00:00
if (!data->start_timestamp)
data->start_timestamp = frame->timestamp;
if (!!data->swscale)
sws_scale(data->swscale, (const uint8_t *const *)frame->data,
2019-09-22 21:19:10 +00:00
(const int *)frame->linesize, 0, data->config.height,
data->vframe->data, data->vframe->linesize);
2016-02-23 23:16:51 +00:00
else
2019-09-22 21:19:10 +00:00
copy_data(data->vframe, frame, context->height,
context->pix_fmt);
2018-02-19 19:54:37 +00:00
#if LIBAVFORMAT_VERSION_MAJOR < 58
2016-02-23 23:16:51 +00:00
if (data->output->flags & AVFMT_RAWPICTURE) {
2019-09-22 21:19:10 +00:00
packet.flags |= AV_PKT_FLAG_KEY;
packet.stream_index = data->video->index;
packet.data = data->vframe->data[0];
packet.size = sizeof(AVPicture);
2016-02-23 23:16:51 +00:00
pthread_mutex_lock(&output->write_mutex);
da_push_back(output->packets, &packet);
pthread_mutex_unlock(&output->write_mutex);
os_sem_post(output->write_sem);
} else {
2018-02-19 19:54:37 +00:00
#endif
2016-02-23 23:16:51 +00:00
data->vframe->pts = data->total_frames;
2018-02-19 19:54:37 +00:00
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
ret = avcodec_send_frame(context, data->vframe);
if (ret == 0)
ret = avcodec_receive_packet(context, &packet);
got_packet = (ret == 0);
if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
ret = 0;
#else
2019-09-22 21:19:10 +00:00
ret = avcodec_encode_video2(context, &packet, data->vframe,
&got_packet);
2018-02-19 19:54:37 +00:00
#endif
2016-02-23 23:16:51 +00:00
if (ret < 0) {
2019-09-22 21:19:10 +00:00
blog(LOG_WARNING,
"receive_video: Error encoding "
"video: %s",
av_err2str(ret));
2019-07-27 12:47:10 +00:00
//FIXME: stop the encode with an error
2016-02-23 23:16:51 +00:00
return;
}
if (!ret && got_packet && packet.size) {
packet.pts = rescale_ts(packet.pts, context,
2019-09-22 21:19:10 +00:00
data->video->time_base);
2016-02-23 23:16:51 +00:00
packet.dts = rescale_ts(packet.dts, context,
2019-09-22 21:19:10 +00:00
data->video->time_base);
packet.duration = (int)av_rescale_q(
packet.duration, context->time_base,
data->video->time_base);
2016-02-23 23:16:51 +00:00
pthread_mutex_lock(&output->write_mutex);
da_push_back(output->packets, &packet);
pthread_mutex_unlock(&output->write_mutex);
os_sem_post(output->write_sem);
} else {
ret = 0;
}
2018-02-19 19:54:37 +00:00
#if LIBAVFORMAT_VERSION_MAJOR < 58
2016-02-23 23:16:51 +00:00
}
2018-02-19 19:54:37 +00:00
#endif
2016-02-23 23:16:51 +00:00
if (ret != 0) {
blog(LOG_WARNING, "receive_video: Error writing video: %s",
2019-09-22 21:19:10 +00:00
av_err2str(ret));
2019-07-27 12:47:10 +00:00
//FIXME: stop the encode with an error
2016-02-23 23:16:51 +00:00
}
data->total_frames++;
}
2019-07-27 12:47:10 +00:00
static void encode_audio(struct ffmpeg_output *output, int idx,
2019-09-22 21:19:10 +00:00
struct AVCodecContext *context, size_t block_size)
2016-02-23 23:16:51 +00:00
{
struct ffmpeg_data *data = &output->ff_data;
AVPacket packet = {0};
int ret, got_packet;
size_t total_size = data->frame_size * block_size * context->channels;
2019-07-27 12:47:10 +00:00
data->aframe[idx]->nb_samples = data->frame_size;
2019-09-22 21:19:10 +00:00
data->aframe[idx]->pts = av_rescale_q(
data->total_samples[idx], (AVRational){1, context->sample_rate},
context->time_base);
2016-02-23 23:16:51 +00:00
2019-07-27 12:47:10 +00:00
ret = avcodec_fill_audio_frame(data->aframe[idx], context->channels,
2019-09-22 21:19:10 +00:00
context->sample_fmt,
data->samples[idx][0], (int)total_size,
1);
2016-02-23 23:16:51 +00:00
if (ret < 0) {
2019-09-22 21:19:10 +00:00
blog(LOG_WARNING,
"encode_audio: avcodec_fill_audio_frame "
"failed: %s",
av_err2str(ret));
2019-07-27 12:47:10 +00:00
//FIXME: stop the encode with an error
2016-02-23 23:16:51 +00:00
return;
}
2019-07-27 12:47:10 +00:00
data->total_samples[idx] += data->frame_size;
2016-02-23 23:16:51 +00:00
2018-02-19 19:54:37 +00:00
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
2019-07-27 12:47:10 +00:00
ret = avcodec_send_frame(context, data->aframe[idx]);
2018-02-19 19:54:37 +00:00
if (ret == 0)
ret = avcodec_receive_packet(context, &packet);
got_packet = (ret == 0);
if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
ret = 0;
#else
2019-07-27 12:47:10 +00:00
ret = avcodec_encode_audio2(context, &packet, data->aframe[idx],
2019-09-22 21:19:10 +00:00
&got_packet);
2018-02-19 19:54:37 +00:00
#endif
2016-02-23 23:16:51 +00:00
if (ret < 0) {
blog(LOG_WARNING, "encode_audio: Error encoding audio: %s",
2019-09-22 21:19:10 +00:00
av_err2str(ret));
2019-07-27 12:47:10 +00:00
//FIXME: stop the encode with an error
2016-02-23 23:16:51 +00:00
return;
}
if (!got_packet)
return;
2019-07-27 12:47:10 +00:00
packet.pts = rescale_ts(packet.pts, context,
2019-09-22 21:19:10 +00:00
data->audio_streams[idx]->time_base);
2019-07-27 12:47:10 +00:00
packet.dts = rescale_ts(packet.dts, context,
2019-09-22 21:19:10 +00:00
data->audio_streams[idx]->time_base);
packet.duration =
(int)av_rescale_q(packet.duration, context->time_base,
data->audio_streams[idx]->time_base);
2019-07-27 12:47:10 +00:00
packet.stream_index = data->audio_streams[idx]->index;
2016-02-23 23:16:51 +00:00
pthread_mutex_lock(&output->write_mutex);
da_push_back(output->packets, &packet);
pthread_mutex_unlock(&output->write_mutex);
os_sem_post(output->write_sem);
}
2019-07-27 12:47:10 +00:00
/* Given a bitmask for the selected tracks and the mix index,
* this returns the stream index which will be passed to the muxer. */
static int get_track_order(int track_config, size_t mix_index)
{
int position = 0;
for (size_t i = 0; i < mix_index; i++) {
if (track_config & 1 << i)
position++;
}
return position;
}
static void receive_audio(void *param, size_t mix_idx, struct audio_data *frame)
2016-02-23 23:16:51 +00:00
{
struct ffmpeg_output *output = param;
2019-09-22 21:19:10 +00:00
struct ffmpeg_data *data = &output->ff_data;
2016-02-23 23:16:51 +00:00
size_t frame_size_bytes;
2019-09-22 21:19:10 +00:00
struct audio_data in = *frame;
2019-07-27 12:47:10 +00:00
int track_order;
2016-02-23 23:16:51 +00:00
// codec doesn't support audio or none configured
2019-07-27 12:47:10 +00:00
if (!data->audio_streams)
2016-02-23 23:16:51 +00:00
return;
2019-07-27 12:47:10 +00:00
/* check that the track was selected */
if ((data->audio_tracks & (1 << mix_idx)) == 0)
return;
/* get track order (first selected, etc ...) */
track_order = get_track_order(data->audio_tracks, mix_idx);
AVCodecContext *context = data->audio_streams[track_order]->codec;
2016-02-23 23:16:51 +00:00
if (!data->start_timestamp)
return;
2016-08-28 12:07:43 +00:00
if (!output->audio_start_ts)
output->audio_start_ts = in.timestamp;
2016-02-23 23:16:51 +00:00
frame_size_bytes = (size_t)data->frame_size * data->audio_size;
for (size_t i = 0; i < data->audio_planes; i++)
2019-07-27 12:47:10 +00:00
circlebuf_push_back(&data->excess_frames[track_order][i],
2019-09-22 21:19:10 +00:00
in.data[i], in.frames * data->audio_size);
2016-02-23 23:16:51 +00:00
2019-07-27 12:47:10 +00:00
while (data->excess_frames[track_order][0].size >= frame_size_bytes) {
2016-02-23 23:16:51 +00:00
for (size_t i = 0; i < data->audio_planes; i++)
2019-09-22 21:19:10 +00:00
circlebuf_pop_front(
&data->excess_frames[track_order][i],
data->samples[track_order][i],
frame_size_bytes);
2016-02-23 23:16:51 +00:00
2019-07-27 12:47:10 +00:00
encode_audio(output, track_order, context, data->audio_size);
2016-02-23 23:16:51 +00:00
}
}
2016-08-28 12:07:43 +00:00
static uint64_t get_packet_sys_dts(struct ffmpeg_output *output,
2019-09-22 21:19:10 +00:00
AVPacket *packet)
2016-08-28 12:07:43 +00:00
{
struct ffmpeg_data *data = &output->ff_data;
2019-09-22 21:19:10 +00:00
uint64_t pause_offset = obs_output_get_pause_offset(output->output);
2016-08-28 12:07:43 +00:00
uint64_t start_ts;
AVRational time_base;
if (data->video && data->video->index == packet->stream_index) {
time_base = data->video->time_base;
start_ts = output->video_start_ts;
} else {
2019-07-27 12:47:10 +00:00
time_base = data->audio_streams[0]->time_base;
2016-08-28 12:07:43 +00:00
start_ts = output->audio_start_ts;
}
2019-09-22 21:19:10 +00:00
return start_ts + pause_offset +
(uint64_t)av_rescale_q(packet->dts, time_base,
(AVRational){1, 1000000000});
2016-08-28 12:07:43 +00:00
}
2016-02-23 23:16:51 +00:00
static int process_packet(struct ffmpeg_output *output)
{
AVPacket packet;
bool new_packet = false;
int ret;
pthread_mutex_lock(&output->write_mutex);
if (output->packets.num) {
packet = output->packets.array[0];
da_erase(output->packets, 0);
new_packet = true;
}
pthread_mutex_unlock(&output->write_mutex);
if (!new_packet)
return 0;
/*blog(LOG_DEBUG, "size = %d, flags = %lX, stream = %d, "
"packets queued: %lu",
packet.size, packet.flags,
packet.stream_index, output->packets.num);*/
2016-08-28 12:07:43 +00:00
if (stopping(output)) {
uint64_t sys_ts = get_packet_sys_dts(output, &packet);
if (sys_ts >= output->stop_ts) {
ffmpeg_output_full_stop(output);
return 0;
}
}
2017-06-29 19:01:10 +00:00
output->total_bytes += packet.size;
2016-02-23 23:16:51 +00:00
ret = av_interleaved_write_frame(output->ff_data.output, &packet);
if (ret < 0) {
av_free_packet(&packet);
2019-07-27 12:47:10 +00:00
ffmpeg_log_error(LOG_WARNING, &output->ff_data,
2019-09-22 21:19:10 +00:00
"receive_audio: Error writing packet: %s",
av_err2str(ret));
2016-02-23 23:16:51 +00:00
return ret;
}
return 0;
}
static void *write_thread(void *data)
{
struct ffmpeg_output *output = data;
while (os_sem_wait(output->write_sem) == 0) {
/* check to see if shutting down */
if (os_event_try(output->stop_event) == 0)
break;
int ret = process_packet(output);
if (ret != 0) {
int code = OBS_OUTPUT_ERROR;
pthread_detach(output->write_thread);
output->write_thread_active = false;
if (ret == -ENOSPC)
code = OBS_OUTPUT_NO_SPACE;
obs_output_signal_stop(output->output, code);
ffmpeg_deactivate(output);
break;
}
}
output->active = false;
return NULL;
}
static inline const char *get_string_or_null(obs_data_t *settings,
2019-09-22 21:19:10 +00:00
const char *name)
2016-02-23 23:16:51 +00:00
{
const char *value = obs_data_get_string(settings, name);
if (!value || !strlen(value))
return NULL;
return value;
}
2019-07-27 12:47:10 +00:00
static int get_audio_mix_count(int audio_mix_mask)
{
int mix_count = 0;
for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
if ((audio_mix_mask & (1 << i)) != 0) {
mix_count++;
}
}
return mix_count;
}
2016-02-23 23:16:51 +00:00
static bool try_connect(struct ffmpeg_output *output)
{
video_t *video = obs_output_video(output->output);
const struct video_output_info *voi = video_output_get_info(video);
struct ffmpeg_cfg config;
obs_data_t *settings;
bool success;
int ret;
settings = obs_output_get_settings(output->output);
2017-04-19 19:54:15 +00:00
obs_data_set_default_int(settings, "gop_size", 120);
2016-02-23 23:16:51 +00:00
config.url = obs_data_get_string(settings, "url");
config.format_name = get_string_or_null(settings, "format_name");
2019-09-22 21:19:10 +00:00
config.format_mime_type =
get_string_or_null(settings, "format_mime_type");
2016-02-23 23:16:51 +00:00
config.muxer_settings = obs_data_get_string(settings, "muxer_settings");
config.video_bitrate = (int)obs_data_get_int(settings, "video_bitrate");
config.audio_bitrate = (int)obs_data_get_int(settings, "audio_bitrate");
2017-04-19 19:54:15 +00:00
config.gop_size = (int)obs_data_get_int(settings, "gop_size");
2016-02-23 23:16:51 +00:00
config.video_encoder = get_string_or_null(settings, "video_encoder");
2019-09-22 21:19:10 +00:00
config.video_encoder_id =
(int)obs_data_get_int(settings, "video_encoder_id");
2016-02-23 23:16:51 +00:00
config.audio_encoder = get_string_or_null(settings, "audio_encoder");
2019-09-22 21:19:10 +00:00
config.audio_encoder_id =
(int)obs_data_get_int(settings, "audio_encoder_id");
2016-02-23 23:16:51 +00:00
config.video_settings = obs_data_get_string(settings, "video_settings");
config.audio_settings = obs_data_get_string(settings, "audio_settings");
config.scale_width = (int)obs_data_get_int(settings, "scale_width");
config.scale_height = (int)obs_data_get_int(settings, "scale_height");
2019-09-22 21:19:10 +00:00
config.width = (int)obs_output_get_width(output->output);
2016-02-23 23:16:51 +00:00
config.height = (int)obs_output_get_height(output->output);
2019-09-22 21:19:10 +00:00
config.format =
obs_to_ffmpeg_video_format(video_output_get_format(video));
2019-07-27 12:47:10 +00:00
config.audio_tracks = (int)obs_output_get_mixers(output->output);
config.audio_mix_count = get_audio_mix_count(config.audio_tracks);
2016-02-23 23:16:51 +00:00
if (format_is_yuv(voi->format)) {
2019-09-22 21:19:10 +00:00
config.color_range = voi->range == VIDEO_RANGE_FULL
? AVCOL_RANGE_JPEG
: AVCOL_RANGE_MPEG;
config.color_space = voi->colorspace == VIDEO_CS_709
? AVCOL_SPC_BT709
: AVCOL_SPC_BT470BG;
2016-02-23 23:16:51 +00:00
} else {
config.color_range = AVCOL_RANGE_UNSPECIFIED;
config.color_space = AVCOL_SPC_RGB;
}
if (config.format == AV_PIX_FMT_NONE) {
blog(LOG_DEBUG, "invalid pixel format used for FFmpeg output");
return false;
}
if (!config.scale_width)
config.scale_width = config.width;
if (!config.scale_height)
config.scale_height = config.height;
success = ffmpeg_data_init(&output->ff_data, &config);
obs_data_release(settings);
2019-07-27 12:47:10 +00:00
if (!success) {
if (output->ff_data.last_error) {
obs_output_set_last_error(output->output,
2019-09-22 21:19:10 +00:00
output->ff_data.last_error);
2019-07-27 12:47:10 +00:00
}
ffmpeg_data_free(&output->ff_data);
2016-02-23 23:16:51 +00:00
return false;
2019-07-27 12:47:10 +00:00
}
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
struct audio_convert_info aci = {.format =
output->ff_data.audio_format};
2016-02-23 23:16:51 +00:00
output->active = true;
if (!obs_output_can_begin_data_capture(output->output, 0))
return false;
ret = pthread_create(&output->write_thread, NULL, write_thread, output);
if (ret != 0) {
2019-07-27 12:47:10 +00:00
ffmpeg_log_error(LOG_WARNING, &output->ff_data,
2019-09-22 21:19:10 +00:00
"ffmpeg_output_start: failed to create write "
"thread.");
2016-08-28 12:07:43 +00:00
ffmpeg_output_full_stop(output);
2016-02-23 23:16:51 +00:00
return false;
}
obs_output_set_video_conversion(output->output, NULL);
obs_output_set_audio_conversion(output->output, &aci);
obs_output_begin_data_capture(output->output, 0);
output->write_thread_active = true;
return true;
}
static void *start_thread(void *data)
{
struct ffmpeg_output *output = data;
if (!try_connect(output))
obs_output_signal_stop(output->output,
2019-09-22 21:19:10 +00:00
OBS_OUTPUT_CONNECT_FAILED);
2016-02-23 23:16:51 +00:00
output->connecting = false;
return NULL;
}
static bool ffmpeg_output_start(void *data)
{
struct ffmpeg_output *output = data;
int ret;
if (output->connecting)
return false;
2016-08-28 12:07:43 +00:00
os_atomic_set_bool(&output->stopping, false);
output->audio_start_ts = 0;
output->video_start_ts = 0;
2017-06-29 19:01:10 +00:00
output->total_bytes = 0;
2016-08-28 12:07:43 +00:00
2016-02-23 23:16:51 +00:00
ret = pthread_create(&output->start_thread, NULL, start_thread, output);
return (output->connecting = (ret == 0));
}
2016-08-28 12:07:43 +00:00
static void ffmpeg_output_full_stop(void *data)
2016-02-23 23:16:51 +00:00
{
struct ffmpeg_output *output = data;
if (output->active) {
obs_output_end_data_capture(output->output);
ffmpeg_deactivate(output);
}
}
2016-08-28 12:07:43 +00:00
static void ffmpeg_output_stop(void *data, uint64_t ts)
{
struct ffmpeg_output *output = data;
if (output->active) {
if (ts == 0) {
ffmpeg_output_full_stop(output);
} else {
os_atomic_set_bool(&output->stopping, true);
output->stop_ts = ts;
}
}
}
2016-02-23 23:16:51 +00:00
static void ffmpeg_deactivate(struct ffmpeg_output *output)
{
if (output->write_thread_active) {
os_event_signal(output->stop_event);
os_sem_post(output->write_sem);
pthread_join(output->write_thread, NULL);
output->write_thread_active = false;
}
pthread_mutex_lock(&output->write_mutex);
for (size_t i = 0; i < output->packets.num; i++)
2019-09-22 21:19:10 +00:00
av_free_packet(output->packets.array + i);
2016-02-23 23:16:51 +00:00
da_free(output->packets);
pthread_mutex_unlock(&output->write_mutex);
ffmpeg_data_free(&output->ff_data);
}
2017-06-29 19:01:10 +00:00
static uint64_t ffmpeg_output_total_bytes(void *data)
{
struct ffmpeg_output *output = data;
return output->total_bytes;
}
2016-02-23 23:16:51 +00:00
struct obs_output_info ffmpeg_output = {
2019-09-22 21:19:10 +00:00
.id = "ffmpeg_output",
.flags = OBS_OUTPUT_AUDIO | OBS_OUTPUT_VIDEO | OBS_OUTPUT_MULTI_TRACK |
OBS_OUTPUT_CAN_PAUSE,
.get_name = ffmpeg_output_getname,
.create = ffmpeg_output_create,
.destroy = ffmpeg_output_destroy,
.start = ffmpeg_output_start,
.stop = ffmpeg_output_stop,
2016-02-23 23:16:51 +00:00
.raw_video = receive_video,
2019-07-27 12:47:10 +00:00
.raw_audio2 = receive_audio,
2017-06-29 19:01:10 +00:00
.get_total_bytes = ffmpeg_output_total_bytes,
2016-02-23 23:16:51 +00:00
};