New upstream version 24.0.1+dfsg1

This commit is contained in:
Sebastian Ramacher 2019-09-22 23:19:10 +02:00
parent b14f9eae6d
commit 5a730d6ec3
842 changed files with 42245 additions and 33385 deletions

View file

@ -32,92 +32,92 @@
#include "obs-ffmpeg-compat.h"
struct ffmpeg_cfg {
const char *url;
const char *format_name;
const char *format_mime_type;
const char *muxer_settings;
int gop_size;
int video_bitrate;
int audio_bitrate;
const char *video_encoder;
int video_encoder_id;
const char *audio_encoder;
int audio_encoder_id;
const char *video_settings;
const char *audio_settings;
int audio_mix_count;
int audio_tracks;
const char *url;
const char *format_name;
const char *format_mime_type;
const char *muxer_settings;
int gop_size;
int video_bitrate;
int audio_bitrate;
const char *video_encoder;
int video_encoder_id;
const char *audio_encoder;
int audio_encoder_id;
const char *video_settings;
const char *audio_settings;
int audio_mix_count;
int audio_tracks;
enum AVPixelFormat format;
enum AVColorRange color_range;
enum AVColorSpace color_space;
int scale_width;
int scale_height;
int width;
int height;
enum AVColorRange color_range;
enum AVColorSpace color_space;
int scale_width;
int scale_height;
int width;
int height;
};
struct ffmpeg_data {
AVStream *video;
AVStream **audio_streams;
AVCodec *acodec;
AVCodec *vcodec;
AVFormatContext *output;
struct SwsContext *swscale;
AVStream *video;
AVStream **audio_streams;
AVCodec *acodec;
AVCodec *vcodec;
AVFormatContext *output;
struct SwsContext *swscale;
int64_t total_frames;
AVFrame *vframe;
int frame_size;
int64_t total_frames;
AVFrame *vframe;
int frame_size;
uint64_t start_timestamp;
uint64_t start_timestamp;
int64_t total_samples[MAX_AUDIO_MIXES];
uint32_t audio_samplerate;
enum audio_format audio_format;
size_t audio_planes;
size_t audio_size;
int num_audio_streams;
int64_t total_samples[MAX_AUDIO_MIXES];
uint32_t audio_samplerate;
enum audio_format audio_format;
size_t audio_planes;
size_t audio_size;
int num_audio_streams;
/* audio_tracks is a bitmask storing the indices of the mixes */
int audio_tracks;
struct circlebuf excess_frames[MAX_AUDIO_MIXES][MAX_AV_PLANES];
uint8_t *samples[MAX_AUDIO_MIXES][MAX_AV_PLANES];
AVFrame *aframe[MAX_AUDIO_MIXES];
int audio_tracks;
struct circlebuf excess_frames[MAX_AUDIO_MIXES][MAX_AV_PLANES];
uint8_t *samples[MAX_AUDIO_MIXES][MAX_AV_PLANES];
AVFrame *aframe[MAX_AUDIO_MIXES];
struct ffmpeg_cfg config;
struct ffmpeg_cfg config;
bool initialized;
bool initialized;
char *last_error;
char *last_error;
};
struct ffmpeg_output {
obs_output_t *output;
volatile bool active;
obs_output_t *output;
volatile bool active;
struct ffmpeg_data ff_data;
bool connecting;
pthread_t start_thread;
bool connecting;
pthread_t start_thread;
uint64_t total_bytes;
uint64_t total_bytes;
uint64_t audio_start_ts;
uint64_t video_start_ts;
uint64_t stop_ts;
volatile bool stopping;
uint64_t audio_start_ts;
uint64_t video_start_ts;
uint64_t stop_ts;
volatile bool stopping;
bool write_thread_active;
pthread_mutex_t write_mutex;
pthread_t write_thread;
os_sem_t *write_sem;
os_event_t *stop_event;
bool write_thread_active;
pthread_mutex_t write_mutex;
pthread_t write_thread;
os_sem_t *write_sem;
os_event_t *stop_event;
DARRAY(AVPacket) packets;
DARRAY(AVPacket) packets;
};
/* ------------------------------------------------------------------------- */
static void ffmpeg_output_set_last_error(struct ffmpeg_data *data,
const char *error)
const char *error)
{
if (data->last_error)
bfree(data->last_error);
@ -126,7 +126,7 @@ static void ffmpeg_output_set_last_error(struct ffmpeg_data *data,
}
void ffmpeg_log_error(int log_level, struct ffmpeg_data *data,
const char *format, ...)
const char *format, ...)
{
va_list args;
char out[4096];
@ -141,26 +141,27 @@ void ffmpeg_log_error(int log_level, struct ffmpeg_data *data,
}
static bool new_stream(struct ffmpeg_data *data, AVStream **stream,
AVCodec **codec, enum AVCodecID id, const char *name)
AVCodec **codec, enum AVCodecID id, const char *name)
{
*codec = (!!name && *name) ?
avcodec_find_encoder_by_name(name) :
avcodec_find_encoder(id);
*codec = (!!name && *name) ? avcodec_find_encoder_by_name(name)
: avcodec_find_encoder(id);
if (!*codec) {
ffmpeg_log_error(LOG_WARNING, data, "Couldn't find encoder '%s'",
avcodec_get_name(id));
ffmpeg_log_error(LOG_WARNING, data,
"Couldn't find encoder '%s'",
avcodec_get_name(id));
return false;
}
*stream = avformat_new_stream(data->output, *codec);
if (!*stream) {
ffmpeg_log_error(LOG_WARNING, data, "Couldn't create stream for encoder '%s'",
avcodec_get_name(id));
ffmpeg_log_error(LOG_WARNING, data,
"Couldn't create stream for encoder '%s'",
avcodec_get_name(id));
return false;
}
(*stream)->id = data->output->nb_streams-1;
(*stream)->id = data->output->nb_streams - 1;
return true;
}
@ -180,10 +181,11 @@ static bool parse_params(AVCodecContext *context, char **opts)
char *value;
*assign = 0;
value = assign+1;
value = assign + 1;
if (av_opt_set(context->priv_data, name, value, 0)) {
blog(LOG_WARNING, "Failed to set %s=%s", name, value);
blog(LOG_WARNING, "Failed to set %s=%s", name,
value);
ret = false;
}
}
@ -205,34 +207,39 @@ static bool open_video_codec(struct ffmpeg_data *data)
if (opts) {
// libav requires x264 parameters in a special format which may be non-obvious
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");
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");
strlist_free(opts);
}
ret = avcodec_open2(context, data->vcodec, NULL);
if (ret < 0) {
ffmpeg_log_error(LOG_WARNING, data, "Failed to open video codec: %s",
av_err2str(ret));
ffmpeg_log_error(LOG_WARNING, data,
"Failed to open video codec: %s",
av_err2str(ret));
return false;
}
data->vframe = av_frame_alloc();
if (!data->vframe) {
ffmpeg_log_error(LOG_WARNING, data, "Failed to allocate video frame");
ffmpeg_log_error(LOG_WARNING, data,
"Failed to allocate video frame");
return false;
}
data->vframe->format = context->pix_fmt;
data->vframe->width = context->width;
data->vframe->width = context->width;
data->vframe->height = context->height;
data->vframe->colorspace = data->config.color_space;
data->vframe->color_range = data->config.color_range;
ret = av_frame_get_buffer(data->vframe, base_get_alignment());
if (ret < 0) {
ffmpeg_log_error(LOG_WARNING, data, "Failed to allocate vframe: %s",
av_err2str(ret));
ffmpeg_log_error(LOG_WARNING, data,
"Failed to allocate vframe: %s",
av_err2str(ret));
return false;
}
@ -242,14 +249,13 @@ static bool open_video_codec(struct ffmpeg_data *data)
static bool init_swscale(struct ffmpeg_data *data, AVCodecContext *context)
{
data->swscale = sws_getContext(
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);
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);
if (!data->swscale) {
ffmpeg_log_error(LOG_WARNING, data, "Could not initialize swscale");
ffmpeg_log_error(LOG_WARNING, data,
"Could not initialize swscale");
return false;
}
@ -268,23 +274,23 @@ static bool create_video_stream(struct ffmpeg_data *data)
}
if (!new_stream(data, &data->video, &data->vcodec,
data->output->oformat->video_codec,
data->config.video_encoder))
data->output->oformat->video_codec,
data->config.video_encoder))
return false;
closest_format = get_closest_format(data->config.format,
data->vcodec->pix_fmts);
closest_format =
get_closest_format(data->config.format, data->vcodec->pix_fmts);
context = data->video->codec;
context->bit_rate = data->config.video_bitrate * 1000;
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;
context = data->video->codec;
context->bit_rate = data->config.video_bitrate * 1000;
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;
data->video->time_base = context->time_base;
@ -294,8 +300,8 @@ static bool create_video_stream(struct ffmpeg_data *data)
if (!open_video_codec(data))
return false;
if (context->pix_fmt != data->config.format ||
data->config.width != data->config.scale_width ||
if (context->pix_fmt != data->config.format ||
data->config.width != data->config.scale_width ||
data->config.height != data->config.scale_height) {
if (!init_swscale(data, context))
@ -318,7 +324,8 @@ static bool open_audio_codec(struct ffmpeg_data *data, int idx)
data->aframe[idx] = av_frame_alloc();
if (!data->aframe[idx]) {
ffmpeg_log_error(LOG_WARNING, data, "Failed to allocate audio frame");
ffmpeg_log_error(LOG_WARNING, data,
"Failed to allocate audio frame");
return false;
}
@ -331,18 +338,20 @@ static bool open_audio_codec(struct ffmpeg_data *data, int idx)
ret = avcodec_open2(context, data->acodec, NULL);
if (ret < 0) {
ffmpeg_log_error(LOG_WARNING, data, "Failed to open audio codec: %s",
av_err2str(ret));
ffmpeg_log_error(LOG_WARNING, data,
"Failed to open audio codec: %s",
av_err2str(ret));
return false;
}
data->frame_size = context->frame_size ? context->frame_size : 1024;
ret = av_samples_alloc(data->samples[idx], NULL, context->channels,
data->frame_size, context->sample_fmt, 0);
data->frame_size, context->sample_fmt, 0);
if (ret < 0) {
ffmpeg_log_error(LOG_WARNING, data, "Failed to create audio buffer: %s",
av_err2str(ret));
ffmpeg_log_error(LOG_WARNING, data,
"Failed to create audio buffer: %s",
av_err2str(ret));
return false;
}
@ -361,25 +370,26 @@ static bool create_audio_stream(struct ffmpeg_data *data, int idx)
}
if (!new_stream(data, &stream, &data->acodec,
data->output->oformat->audio_codec,
data->config.audio_encoder))
data->output->oformat->audio_codec,
data->config.audio_encoder))
return false;
data->audio_streams[idx] = stream;
context = data->audio_streams[idx]->codec;
context->bit_rate = data->config.audio_bitrate * 1000;
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);
context = data->audio_streams[idx]->codec;
context->bit_rate = data->config.audio_bitrate * 1000;
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);
//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");
context->sample_fmt = data->acodec->sample_fmts ?
data->acodec->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
context->sample_fmt = data->acodec->sample_fmts
? data->acodec->sample_fmts[0]
: AV_SAMPLE_FMT_FLTP;
data->audio_streams[idx]->time_base = context->time_base;
@ -402,9 +412,10 @@ static inline bool init_streams(struct ffmpeg_data *data)
if (!create_video_stream(data))
return false;
if (format->audio_codec != AV_CODEC_ID_NONE && data->num_audio_streams) {
data->audio_streams = calloc(1,
data->num_audio_streams * sizeof(void*));
if (format->audio_codec != AV_CODEC_ID_NONE &&
data->num_audio_streams) {
data->audio_streams =
calloc(1, data->num_audio_streams * sizeof(void *));
for (int i = 0; i < data->num_audio_streams; i++) {
if (!create_audio_stream(data, i))
return false;
@ -420,10 +431,11 @@ static inline bool open_output_file(struct ffmpeg_data *data)
int ret;
AVDictionary *dict = NULL;
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);
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);
av_dict_free(&dict);
return false;
@ -434,7 +446,7 @@ static inline bool open_output_file(struct ffmpeg_data *data)
AVDictionaryEntry *entry = NULL;
while ((entry = av_dict_get(dict, "", entry,
AV_DICT_IGNORE_SUFFIX)))
AV_DICT_IGNORE_SUFFIX)))
dstr_catf(&str, "\n\t%s=%s", entry->key, entry->value);
blog(LOG_INFO, "Using muxer settings: %s", str.array);
@ -443,24 +455,24 @@ static inline bool open_output_file(struct ffmpeg_data *data)
if ((format->flags & AVFMT_NOFILE) == 0) {
ret = avio_open2(&data->output->pb, data->config.url,
AVIO_FLAG_WRITE, NULL, &dict);
AVIO_FLAG_WRITE, NULL, &dict);
if (ret < 0) {
ffmpeg_log_error(LOG_WARNING, data,
"Couldn't open '%s', %s", data->config.url,
av_err2str(ret));
"Couldn't open '%s', %s",
data->config.url, av_err2str(ret));
av_dict_free(&dict);
return false;
}
}
strncpy(data->output->filename, data->config.url,
sizeof(data->output->filename));
sizeof(data->output->filename));
data->output->filename[sizeof(data->output->filename) - 1] = 0;
ret = avformat_write_header(data->output, &dict);
if (ret < 0) {
ffmpeg_log_error(LOG_WARNING, data, "Error opening '%s': %s",
data->config.url, av_err2str(ret));
data->config.url, av_err2str(ret));
return false;
}
@ -469,7 +481,7 @@ static inline bool open_output_file(struct ffmpeg_data *data)
AVDictionaryEntry *entry = NULL;
while ((entry = av_dict_get(dict, "", entry,
AV_DICT_IGNORE_SUFFIX)))
AV_DICT_IGNORE_SUFFIX)))
dstr_catf(&str, "\n\t%s=%s", entry->key, entry->value);
blog(LOG_INFO, "Invalid muxer settings: %s", str.array);
@ -564,16 +576,14 @@ static enum AVCodecID get_codec_id(const char *name, int id)
static void set_encoder_ids(struct ffmpeg_data *data)
{
data->output->oformat->video_codec = get_codec_id(
data->config.video_encoder,
data->config.video_encoder_id);
data->config.video_encoder, data->config.video_encoder_id);
data->output->oformat->audio_codec = get_codec_id(
data->config.audio_encoder,
data->config.audio_encoder_id);
data->config.audio_encoder, data->config.audio_encoder_id);
}
static bool ffmpeg_data_init(struct ffmpeg_data *data,
struct ffmpeg_cfg *config)
struct ffmpeg_cfg *config)
{
bool is_rtmp = false;
@ -584,35 +594,36 @@ static bool ffmpeg_data_init(struct ffmpeg_data *data,
if (!config->url || !*config->url)
return false;
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
av_register_all();
#endif
avformat_network_init();
is_rtmp = (astrcmpi_n(config->url, "rtmp://", 7) == 0);
AVOutputFormat *output_format = av_guess_format(
is_rtmp ? "flv" : data->config.format_name,
data->config.url,
is_rtmp ? NULL : data->config.format_mime_type);
is_rtmp ? "flv" : data->config.format_name, data->config.url,
is_rtmp ? NULL : data->config.format_mime_type);
if (output_format == NULL) {
ffmpeg_log_error(LOG_WARNING, data,
ffmpeg_log_error(
LOG_WARNING, data,
"Couldn't find matching output format with "
"parameters: name=%s, url=%s, mime=%s",
safe_str(is_rtmp ?
"flv" : data->config.format_name),
safe_str(is_rtmp ? "flv" : data->config.format_name),
safe_str(data->config.url),
safe_str(is_rtmp ?
NULL : data->config.format_mime_type));
safe_str(is_rtmp ? NULL
: data->config.format_mime_type));
goto fail;
}
avformat_alloc_output_context2(&data->output, output_format,
NULL, NULL);
avformat_alloc_output_context2(&data->output, output_format, NULL,
NULL);
if (!data->output) {
ffmpeg_log_error(LOG_WARNING, data,
"Couldn't create avformat context");
"Couldn't create avformat context");
goto fail;
}
@ -653,7 +664,7 @@ static const char *ffmpeg_output_getname(void *unused)
}
static void ffmpeg_log_callback(void *param, int level, const char *format,
va_list args)
va_list args)
{
if (level <= AV_LOG_INFO)
blogva(LOG_DEBUG, format, args);
@ -707,27 +718,27 @@ static void ffmpeg_output_destroy(void *data)
}
static inline void copy_data(AVFrame *pic, const struct video_data *frame,
int height, enum AVPixelFormat format)
int height, enum AVPixelFormat format)
{
int h_chroma_shift, v_chroma_shift;
av_pix_fmt_get_chroma_sub_sample(format, &h_chroma_shift, &v_chroma_shift);
av_pix_fmt_get_chroma_sub_sample(format, &h_chroma_shift,
&v_chroma_shift);
for (int plane = 0; plane < MAX_AV_PLANES; plane++) {
if (!frame->data[plane])
continue;
int frame_rowsize = (int)frame->linesize[plane];
int pic_rowsize = pic->linesize[plane];
int bytes = frame_rowsize < pic_rowsize ?
frame_rowsize : pic_rowsize;
int pic_rowsize = pic->linesize[plane];
int bytes = frame_rowsize < pic_rowsize ? frame_rowsize
: pic_rowsize;
int plane_height = height >> (plane ? v_chroma_shift : 0);
for (int y = 0; y < plane_height; y++) {
int pos_frame = y * frame_rowsize;
int pos_pic = y * pic_rowsize;
int pos_pic = y * pic_rowsize;
memcpy(pic->data[plane] + pos_pic,
frame->data[plane] + pos_frame,
bytes);
frame->data[plane] + pos_frame, bytes);
}
}
}
@ -735,7 +746,7 @@ static inline void copy_data(AVFrame *pic, const struct video_data *frame,
static void receive_video(void *param, struct video_data *frame)
{
struct ffmpeg_output *output = param;
struct ffmpeg_data *data = &output->ff_data;
struct ffmpeg_data *data = &output->ff_data;
// codec doesn't support video or none configured
if (!data->video)
@ -754,17 +765,17 @@ static void receive_video(void *param, struct video_data *frame)
if (!!data->swscale)
sws_scale(data->swscale, (const uint8_t *const *)frame->data,
(const int*)frame->linesize,
0, data->config.height, data->vframe->data,
data->vframe->linesize);
(const int *)frame->linesize, 0, data->config.height,
data->vframe->data, data->vframe->linesize);
else
copy_data(data->vframe, frame, context->height, context->pix_fmt);
copy_data(data->vframe, frame, context->height,
context->pix_fmt);
#if LIBAVFORMAT_VERSION_MAJOR < 58
if (data->output->flags & AVFMT_RAWPICTURE) {
packet.flags |= AV_PKT_FLAG_KEY;
packet.stream_index = data->video->index;
packet.data = data->vframe->data[0];
packet.size = sizeof(AVPicture);
packet.flags |= AV_PKT_FLAG_KEY;
packet.stream_index = data->video->index;
packet.data = data->vframe->data[0];
packet.size = sizeof(AVPicture);
pthread_mutex_lock(&output->write_mutex);
da_push_back(output->packets, &packet);
@ -784,24 +795,26 @@ static void receive_video(void *param, struct video_data *frame)
if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
ret = 0;
#else
ret = avcodec_encode_video2(context, &packet, data->vframe,
&got_packet);
ret = avcodec_encode_video2(context, &packet, data->vframe,
&got_packet);
#endif
if (ret < 0) {
blog(LOG_WARNING, "receive_video: Error encoding "
"video: %s", av_err2str(ret));
blog(LOG_WARNING,
"receive_video: Error encoding "
"video: %s",
av_err2str(ret));
//FIXME: stop the encode with an error
return;
}
if (!ret && got_packet && packet.size) {
packet.pts = rescale_ts(packet.pts, context,
data->video->time_base);
data->video->time_base);
packet.dts = rescale_ts(packet.dts, context,
data->video->time_base);
packet.duration = (int)av_rescale_q(packet.duration,
context->time_base,
data->video->time_base);
data->video->time_base);
packet.duration = (int)av_rescale_q(
packet.duration, context->time_base,
data->video->time_base);
pthread_mutex_lock(&output->write_mutex);
da_push_back(output->packets, &packet);
@ -815,7 +828,7 @@ static void receive_video(void *param, struct video_data *frame)
#endif
if (ret != 0) {
blog(LOG_WARNING, "receive_video: Error writing video: %s",
av_err2str(ret));
av_err2str(ret));
//FIXME: stop the encode with an error
}
@ -823,7 +836,7 @@ static void receive_video(void *param, struct video_data *frame)
}
static void encode_audio(struct ffmpeg_output *output, int idx,
struct AVCodecContext *context, size_t block_size)
struct AVCodecContext *context, size_t block_size)
{
struct ffmpeg_data *data = &output->ff_data;
@ -832,16 +845,19 @@ static void encode_audio(struct ffmpeg_output *output, int idx,
size_t total_size = data->frame_size * block_size * context->channels;
data->aframe[idx]->nb_samples = data->frame_size;
data->aframe[idx]->pts = av_rescale_q(data->total_samples[idx],
(AVRational){1, context->sample_rate},
context->time_base);
data->aframe[idx]->pts = av_rescale_q(
data->total_samples[idx], (AVRational){1, context->sample_rate},
context->time_base);
ret = avcodec_fill_audio_frame(data->aframe[idx], context->channels,
context->sample_fmt, data->samples[idx][0],
(int)total_size, 1);
context->sample_fmt,
data->samples[idx][0], (int)total_size,
1);
if (ret < 0) {
blog(LOG_WARNING, "encode_audio: avcodec_fill_audio_frame "
"failed: %s", av_err2str(ret));
blog(LOG_WARNING,
"encode_audio: avcodec_fill_audio_frame "
"failed: %s",
av_err2str(ret));
//FIXME: stop the encode with an error
return;
}
@ -859,11 +875,11 @@ static void encode_audio(struct ffmpeg_output *output, int idx,
ret = 0;
#else
ret = avcodec_encode_audio2(context, &packet, data->aframe[idx],
&got_packet);
&got_packet);
#endif
if (ret < 0) {
blog(LOG_WARNING, "encode_audio: Error encoding audio: %s",
av_err2str(ret));
av_err2str(ret));
//FIXME: stop the encode with an error
return;
}
@ -872,11 +888,12 @@ static void encode_audio(struct ffmpeg_output *output, int idx,
return;
packet.pts = rescale_ts(packet.pts, context,
data->audio_streams[idx]->time_base);
data->audio_streams[idx]->time_base);
packet.dts = rescale_ts(packet.dts, context,
data->audio_streams[idx]->time_base);
packet.duration = (int)av_rescale_q(packet.duration, context->time_base,
data->audio_streams[idx]->time_base);
data->audio_streams[idx]->time_base);
packet.duration =
(int)av_rescale_q(packet.duration, context->time_base,
data->audio_streams[idx]->time_base);
packet.stream_index = data->audio_streams[idx]->index;
pthread_mutex_lock(&output->write_mutex);
@ -885,34 +902,6 @@ static void encode_audio(struct ffmpeg_output *output, int idx,
os_sem_post(output->write_sem);
}
static bool prepare_audio(struct ffmpeg_data *data,
const struct audio_data *frame, struct audio_data *output)
{
*output = *frame;
if (frame->timestamp < data->start_timestamp) {
uint64_t duration = (uint64_t)frame->frames * 1000000000 /
(uint64_t)data->audio_samplerate;
uint64_t end_ts = (frame->timestamp + duration);
uint64_t cutoff;
if (end_ts <= data->start_timestamp)
return false;
cutoff = data->start_timestamp - frame->timestamp;
output->timestamp += cutoff;
cutoff = cutoff * (uint64_t)data->audio_samplerate /
1000000000;
for (size_t i = 0; i < data->audio_planes; i++)
output->data[i] += data->audio_size * (uint32_t)cutoff;
output->frames -= (uint32_t)cutoff;
}
return true;
}
/* 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)
@ -928,9 +917,9 @@ static int get_track_order(int track_config, size_t mix_index)
static void receive_audio(void *param, size_t mix_idx, struct audio_data *frame)
{
struct ffmpeg_output *output = param;
struct ffmpeg_data *data = &output->ff_data;
struct ffmpeg_data *data = &output->ff_data;
size_t frame_size_bytes;
struct audio_data in;
struct audio_data in = *frame;
int track_order;
// codec doesn't support audio or none configured
@ -948,8 +937,6 @@ static void receive_audio(void *param, size_t mix_idx, struct audio_data *frame)
if (!data->start_timestamp)
return;
if (!prepare_audio(data, frame, &in))
return;
if (!output->audio_start_ts)
output->audio_start_ts = in.timestamp;
@ -958,22 +945,24 @@ static void receive_audio(void *param, size_t mix_idx, struct audio_data *frame)
for (size_t i = 0; i < data->audio_planes; i++)
circlebuf_push_back(&data->excess_frames[track_order][i],
in.data[i], in.frames * data->audio_size);
in.data[i], in.frames * data->audio_size);
while (data->excess_frames[track_order][0].size >= frame_size_bytes) {
for (size_t i = 0; i < data->audio_planes; i++)
circlebuf_pop_front(&data->excess_frames[track_order][i],
data->samples[track_order][i],
frame_size_bytes);
circlebuf_pop_front(
&data->excess_frames[track_order][i],
data->samples[track_order][i],
frame_size_bytes);
encode_audio(output, track_order, context, data->audio_size);
}
}
static uint64_t get_packet_sys_dts(struct ffmpeg_output *output,
AVPacket *packet)
AVPacket *packet)
{
struct ffmpeg_data *data = &output->ff_data;
uint64_t pause_offset = obs_output_get_pause_offset(output->output);
uint64_t start_ts;
AVRational time_base;
@ -986,8 +975,9 @@ static uint64_t get_packet_sys_dts(struct ffmpeg_output *output,
start_ts = output->audio_start_ts;
}
return start_ts + (uint64_t)av_rescale_q(packet->dts,
time_base, (AVRational){1, 1000000000});
return start_ts + pause_offset +
(uint64_t)av_rescale_q(packet->dts, time_base,
(AVRational){1, 1000000000});
}
static int process_packet(struct ffmpeg_output *output)
@ -1026,8 +1016,8 @@ static int process_packet(struct ffmpeg_output *output)
if (ret < 0) {
av_free_packet(&packet);
ffmpeg_log_error(LOG_WARNING, &output->ff_data,
"receive_audio: Error writing packet: %s",
av_err2str(ret));
"receive_audio: Error writing packet: %s",
av_err2str(ret));
return ret;
}
@ -1064,7 +1054,7 @@ static void *write_thread(void *data)
}
static inline const char *get_string_or_null(obs_data_t *settings,
const char *name)
const char *name)
{
const char *value = obs_data_get_string(settings, name);
if (!value || !strlen(value))
@ -1099,34 +1089,36 @@ static bool try_connect(struct ffmpeg_output *output)
config.url = obs_data_get_string(settings, "url");
config.format_name = get_string_or_null(settings, "format_name");
config.format_mime_type = get_string_or_null(settings,
"format_mime_type");
config.format_mime_type =
get_string_or_null(settings, "format_mime_type");
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");
config.gop_size = (int)obs_data_get_int(settings, "gop_size");
config.video_encoder = get_string_or_null(settings, "video_encoder");
config.video_encoder_id = (int)obs_data_get_int(settings,
"video_encoder_id");
config.video_encoder_id =
(int)obs_data_get_int(settings, "video_encoder_id");
config.audio_encoder = get_string_or_null(settings, "audio_encoder");
config.audio_encoder_id = (int)obs_data_get_int(settings,
"audio_encoder_id");
config.audio_encoder_id =
(int)obs_data_get_int(settings, "audio_encoder_id");
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");
config.width = (int)obs_output_get_width(output->output);
config.width = (int)obs_output_get_width(output->output);
config.height = (int)obs_output_get_height(output->output);
config.format = obs_to_ffmpeg_video_format(
video_output_get_format(video));
config.format =
obs_to_ffmpeg_video_format(video_output_get_format(video));
config.audio_tracks = (int)obs_output_get_mixers(output->output);
config.audio_mix_count = get_audio_mix_count(config.audio_tracks);
if (format_is_yuv(voi->format)) {
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;
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;
} else {
config.color_range = AVCOL_RANGE_UNSPECIFIED;
config.color_space = AVCOL_SPC_RGB;
@ -1148,15 +1140,14 @@ static bool try_connect(struct ffmpeg_output *output)
if (!success) {
if (output->ff_data.last_error) {
obs_output_set_last_error(output->output,
output->ff_data.last_error);
output->ff_data.last_error);
}
ffmpeg_data_free(&output->ff_data);
return false;
}
struct audio_convert_info aci = {
.format = output->ff_data.audio_format
};
struct audio_convert_info aci = {.format =
output->ff_data.audio_format};
output->active = true;
@ -1166,8 +1157,8 @@ static bool try_connect(struct ffmpeg_output *output)
ret = pthread_create(&output->write_thread, NULL, write_thread, output);
if (ret != 0) {
ffmpeg_log_error(LOG_WARNING, &output->ff_data,
"ffmpeg_output_start: failed to create write "
"thread.");
"ffmpeg_output_start: failed to create write "
"thread.");
ffmpeg_output_full_stop(output);
return false;
}
@ -1185,7 +1176,7 @@ static void *start_thread(void *data)
if (!try_connect(output))
obs_output_signal_stop(output->output,
OBS_OUTPUT_CONNECT_FAILED);
OBS_OUTPUT_CONNECT_FAILED);
output->connecting = false;
return NULL;
@ -1244,7 +1235,7 @@ static void ffmpeg_deactivate(struct ffmpeg_output *output)
pthread_mutex_lock(&output->write_mutex);
for (size_t i = 0; i < output->packets.num; i++)
av_free_packet(output->packets.array+i);
av_free_packet(output->packets.array + i);
da_free(output->packets);
pthread_mutex_unlock(&output->write_mutex);
@ -1259,15 +1250,14 @@ static uint64_t ffmpeg_output_total_bytes(void *data)
}
struct obs_output_info ffmpeg_output = {
.id = "ffmpeg_output",
.flags = OBS_OUTPUT_AUDIO |
OBS_OUTPUT_VIDEO |
OBS_OUTPUT_MULTI_TRACK,
.get_name = ffmpeg_output_getname,
.create = ffmpeg_output_create,
.destroy = ffmpeg_output_destroy,
.start = ffmpeg_output_start,
.stop = ffmpeg_output_stop,
.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,
.raw_video = receive_video,
.raw_audio2 = receive_audio,
.get_total_bytes = ffmpeg_output_total_bytes,