New upstream version 23.2.1+dfsg1

This commit is contained in:
Simon Chopin 2019-07-27 14:47:10 +02:00
parent cdc9a9fc87
commit b14f9eae6d
1017 changed files with 37232 additions and 11111 deletions

View file

@ -45,6 +45,8 @@ struct ffmpeg_cfg {
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;
@ -56,7 +58,7 @@ struct ffmpeg_cfg {
struct ffmpeg_data {
AVStream *video;
AVStream *audio;
AVStream **audio_streams;
AVCodec *acodec;
AVCodec *vcodec;
AVFormatContext *output;
@ -68,18 +70,24 @@ struct ffmpeg_data {
uint64_t start_timestamp;
int64_t total_samples;
int64_t total_samples[MAX_AUDIO_MIXES];
uint32_t audio_samplerate;
enum audio_format audio_format;
size_t audio_planes;
size_t audio_size;
struct circlebuf excess_frames[MAX_AV_PLANES];
uint8_t *samples[MAX_AV_PLANES];
AVFrame *aframe;
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];
struct ffmpeg_cfg config;
bool initialized;
char *last_error;
};
struct ffmpeg_output {
@ -108,6 +116,30 @@ struct ffmpeg_output {
/* ------------------------------------------------------------------------- */
static void ffmpeg_output_set_last_error(struct ffmpeg_data *data,
const char *error)
{
if (data->last_error)
bfree(data->last_error);
data->last_error = bstrdup(error);
}
void ffmpeg_log_error(int log_level, struct ffmpeg_data *data,
const char *format, ...)
{
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);
}
static bool new_stream(struct ffmpeg_data *data, AVStream **stream,
AVCodec **codec, enum AVCodecID id, const char *name)
{
@ -116,14 +148,14 @@ static bool new_stream(struct ffmpeg_data *data, AVStream **stream,
avcodec_find_encoder(id);
if (!*codec) {
blog(LOG_WARNING, "Couldn't find encoder '%s'",
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) {
blog(LOG_WARNING, "Couldn't create stream for encoder '%s'",
ffmpeg_log_error(LOG_WARNING, data, "Couldn't create stream for encoder '%s'",
avcodec_get_name(id));
return false;
}
@ -180,14 +212,14 @@ static bool open_video_codec(struct ffmpeg_data *data)
ret = avcodec_open2(context, data->vcodec, NULL);
if (ret < 0) {
blog(LOG_WARNING, "Failed to open video codec: %s",
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) {
blog(LOG_WARNING, "Failed to allocate video frame");
ffmpeg_log_error(LOG_WARNING, data, "Failed to allocate video frame");
return false;
}
@ -199,7 +231,7 @@ static bool open_video_codec(struct ffmpeg_data *data)
ret = av_frame_get_buffer(data->vframe, base_get_alignment());
if (ret < 0) {
blog(LOG_WARNING, "Failed to allocate vframe: %s",
ffmpeg_log_error(LOG_WARNING, data, "Failed to allocate vframe: %s",
av_err2str(ret));
return false;
}
@ -217,7 +249,7 @@ static bool init_swscale(struct ffmpeg_data *data, AVCodecContext *context)
SWS_BICUBIC, NULL, NULL, NULL);
if (!data->swscale) {
blog(LOG_WARNING, "Could not initialize swscale");
ffmpeg_log_error(LOG_WARNING, data, "Could not initialize swscale");
return false;
}
@ -231,7 +263,7 @@ static bool create_video_stream(struct ffmpeg_data *data)
struct obs_video_info ovi;
if (!obs_get_video_info(&ovi)) {
blog(LOG_WARNING, "No active video");
ffmpeg_log_error(LOG_WARNING, data, "No active video");
return false;
}
@ -273,9 +305,9 @@ static bool create_video_stream(struct ffmpeg_data *data)
return true;
}
static bool open_audio_codec(struct ffmpeg_data *data)
static bool open_audio_codec(struct ffmpeg_data *data, int idx)
{
AVCodecContext *context = data->audio->codec;
AVCodecContext *context = data->audio_streams[idx]->codec;
char **opts = strlist_split(data->config.audio_settings, ' ', false);
int ret;
@ -284,32 +316,32 @@ static bool open_audio_codec(struct ffmpeg_data *data)
strlist_free(opts);
}
data->aframe = av_frame_alloc();
if (!data->aframe) {
blog(LOG_WARNING, "Failed to allocate audio frame");
data->aframe[idx] = av_frame_alloc();
if (!data->aframe[idx]) {
ffmpeg_log_error(LOG_WARNING, data, "Failed to allocate audio frame");
return false;
}
data->aframe->format = context->sample_fmt;
data->aframe->channels = context->channels;
data->aframe->channel_layout = context->channel_layout;
data->aframe->sample_rate = context->sample_rate;
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;
context->strict_std_compliance = -2;
ret = avcodec_open2(context, data->acodec, NULL);
if (ret < 0) {
blog(LOG_WARNING, "Failed to open audio codec: %s",
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, NULL, context->channels,
ret = av_samples_alloc(data->samples[idx], NULL, context->channels,
data->frame_size, context->sample_fmt, 0);
if (ret < 0) {
blog(LOG_WARNING, "Failed to create audio buffer: %s",
ffmpeg_log_error(LOG_WARNING, data, "Failed to create audio buffer: %s",
av_err2str(ret));
return false;
}
@ -317,27 +349,29 @@ static bool open_audio_codec(struct ffmpeg_data *data)
return true;
}
static bool create_audio_stream(struct ffmpeg_data *data)
static bool create_audio_stream(struct ffmpeg_data *data, int idx)
{
AVCodecContext *context;
AVStream *stream;
struct obs_audio_info aoi;
if (!obs_get_audio_info(&aoi)) {
blog(LOG_WARNING, "No active audio");
ffmpeg_log_error(LOG_WARNING, data, "No active audio");
return false;
}
if (!new_stream(data, &data->audio, &data->acodec,
if (!new_stream(data, &stream, &data->acodec,
data->output->oformat->audio_codec,
data->config.audio_encoder))
return false;
context = data->audio->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 =
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);
//AVlib default channel layout for 5 channels is 5.0 ; fix for 4.1
@ -347,7 +381,7 @@ static bool create_audio_stream(struct ffmpeg_data *data)
context->sample_fmt = data->acodec->sample_fmts ?
data->acodec->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
data->audio->time_base = context->time_base;
data->audio_streams[idx]->time_base = context->time_base;
data->audio_samplerate = aoi.samples_per_sec;
data->audio_format = convert_ffmpeg_sample_format(context->sample_fmt);
@ -357,7 +391,7 @@ static bool create_audio_stream(struct ffmpeg_data *data)
if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
context->flags |= CODEC_FLAG_GLOBAL_H;
return open_audio_codec(data);
return open_audio_codec(data, idx);
}
static inline bool init_streams(struct ffmpeg_data *data)
@ -368,9 +402,14 @@ static inline bool init_streams(struct ffmpeg_data *data)
if (!create_video_stream(data))
return false;
if (format->audio_codec != AV_CODEC_ID_NONE)
if (!create_audio_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*));
for (int i = 0; i < data->num_audio_streams; i++) {
if (!create_audio_stream(data, i))
return false;
}
}
return true;
}
@ -383,7 +422,7 @@ static inline bool open_output_file(struct ffmpeg_data *data)
AVDictionary *dict = NULL;
if ((ret = av_dict_parse_string(&dict, data->config.muxer_settings,
"=", " ", 0))) {
blog(LOG_WARNING, "Failed to parse muxer settings: %s\n%s",
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);
@ -406,8 +445,9 @@ static inline bool open_output_file(struct ffmpeg_data *data)
ret = avio_open2(&data->output->pb, data->config.url,
AVIO_FLAG_WRITE, NULL, &dict);
if (ret < 0) {
blog(LOG_WARNING, "Couldn't open '%s', %s",
data->config.url, av_err2str(ret));
ffmpeg_log_error(LOG_WARNING, data,
"Couldn't open '%s', %s", data->config.url,
av_err2str(ret));
av_dict_free(&dict);
return false;
}
@ -419,7 +459,7 @@ static inline bool open_output_file(struct ffmpeg_data *data)
ret = avformat_write_header(data->output, &dict);
if (ret < 0) {
blog(LOG_WARNING, "Error opening '%s': %s",
ffmpeg_log_error(LOG_WARNING, data, "Error opening '%s': %s",
data->config.url, av_err2str(ret));
return false;
}
@ -457,12 +497,17 @@ static void close_video(struct ffmpeg_data *data)
static void close_audio(struct ffmpeg_data *data)
{
for (size_t i = 0; i < MAX_AV_PLANES; i++)
circlebuf_free(&data->excess_frames[i]);
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]);
av_freep(&data->samples[0]);
avcodec_close(data->audio->codec);
av_frame_free(&data->aframe);
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]);
}
}
static void ffmpeg_data_free(struct ffmpeg_data *data)
@ -472,8 +517,11 @@ static void ffmpeg_data_free(struct ffmpeg_data *data)
if (data->video)
close_video(data);
if (data->audio)
if (data->audio_streams) {
close_audio(data);
free(data->audio_streams);
data->audio_streams = NULL;
}
if (data->output) {
if ((data->output->oformat->flags & AVFMT_NOFILE) == 0)
@ -482,6 +530,9 @@ static void ffmpeg_data_free(struct ffmpeg_data *data)
avformat_free_context(data->output);
}
if (data->last_error)
bfree(data->last_error);
memset(data, 0, sizeof(struct ffmpeg_data));
}
@ -528,7 +579,8 @@ static bool ffmpeg_data_init(struct ffmpeg_data *data,
memset(data, 0, sizeof(struct ffmpeg_data));
data->config = *config;
data->num_audio_streams = config->audio_mix_count;
data->audio_tracks = config->audio_tracks;
if (!config->url || !*config->url)
return false;
@ -543,19 +595,27 @@ static bool ffmpeg_data_init(struct ffmpeg_data *data,
is_rtmp ? NULL : data->config.format_mime_type);
if (output_format == NULL) {
blog(LOG_WARNING, "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(data->config.url),
safe_str(is_rtmp ?
NULL : data->config.format_mime_type));
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(data->config.url),
safe_str(is_rtmp ?
NULL : data->config.format_mime_type));
goto fail;
}
avformat_alloc_output_context2(&data->output, output_format,
NULL, NULL);
if (!data->output) {
ffmpeg_log_error(LOG_WARNING, data,
"Couldn't create avformat context");
goto fail;
}
if (is_rtmp) {
data->output->oformat->video_codec = AV_CODEC_ID_H264;
data->output->oformat->audio_codec = AV_CODEC_ID_AAC;
@ -564,11 +624,6 @@ static bool ffmpeg_data_init(struct ffmpeg_data *data,
set_encoder_ids(data);
}
if (!data->output) {
blog(LOG_WARNING, "Couldn't create avformat context");
goto fail;
}
if (!init_streams(data))
goto fail;
if (!open_output_file(data))
@ -581,7 +636,6 @@ static bool ffmpeg_data_init(struct ffmpeg_data *data,
fail:
blog(LOG_WARNING, "ffmpeg_data_init failed");
ffmpeg_data_free(data);
return false;
}
@ -736,6 +790,7 @@ static void receive_video(void *param, struct video_data *frame)
if (ret < 0) {
blog(LOG_WARNING, "receive_video: Error encoding "
"video: %s", av_err2str(ret));
//FIXME: stop the encode with an error
return;
}
@ -761,12 +816,13 @@ static void receive_video(void *param, struct video_data *frame)
if (ret != 0) {
blog(LOG_WARNING, "receive_video: Error writing video: %s",
av_err2str(ret));
//FIXME: stop the encode with an error
}
data->total_frames++;
}
static void encode_audio(struct ffmpeg_output *output,
static void encode_audio(struct ffmpeg_output *output, int idx,
struct AVCodecContext *context, size_t block_size)
{
struct ffmpeg_data *data = &output->ff_data;
@ -775,24 +831,25 @@ static void encode_audio(struct ffmpeg_output *output,
int ret, got_packet;
size_t total_size = data->frame_size * block_size * context->channels;
data->aframe->nb_samples = data->frame_size;
data->aframe->pts = av_rescale_q(data->total_samples,
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);
ret = avcodec_fill_audio_frame(data->aframe, context->channels,
context->sample_fmt, data->samples[0],
ret = avcodec_fill_audio_frame(data->aframe[idx], context->channels,
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));
//FIXME: stop the encode with an error
return;
}
data->total_samples += data->frame_size;
data->total_samples[idx] += data->frame_size;
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
ret = avcodec_send_frame(context, data->aframe);
ret = avcodec_send_frame(context, data->aframe[idx]);
if (ret == 0)
ret = avcodec_receive_packet(context, &packet);
@ -801,23 +858,26 @@ static void encode_audio(struct ffmpeg_output *output,
if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
ret = 0;
#else
ret = avcodec_encode_audio2(context, &packet, data->aframe,
ret = avcodec_encode_audio2(context, &packet, data->aframe[idx],
&got_packet);
#endif
if (ret < 0) {
blog(LOG_WARNING, "encode_audio: Error encoding audio: %s",
av_err2str(ret));
//FIXME: stop the encode with an error
return;
}
if (!got_packet)
return;
packet.pts = rescale_ts(packet.pts, context, data->audio->time_base);
packet.dts = rescale_ts(packet.dts, context, data->audio->time_base);
packet.pts = rescale_ts(packet.pts, context,
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->time_base);
packet.stream_index = data->audio->index;
data->audio_streams[idx]->time_base);
packet.stream_index = data->audio_streams[idx]->index;
pthread_mutex_lock(&output->write_mutex);
da_push_back(output->packets, &packet);
@ -853,18 +913,38 @@ static bool prepare_audio(struct ffmpeg_data *data,
return true;
}
static void receive_audio(void *param, struct audio_data *frame)
/* 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)
{
struct ffmpeg_output *output = param;
struct ffmpeg_data *data = &output->ff_data;
size_t frame_size_bytes;
struct audio_data in;
int track_order;
// codec doesn't support audio or none configured
if (!data->audio)
if (!data->audio_streams)
return;
AVCodecContext *context = data->audio->codec;
/* 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;
if (!data->start_timestamp)
return;
@ -877,15 +957,16 @@ static void receive_audio(void *param, struct audio_data *frame)
frame_size_bytes = (size_t)data->frame_size * data->audio_size;
for (size_t i = 0; i < data->audio_planes; i++)
circlebuf_push_back(&data->excess_frames[i], in.data[i],
in.frames * data->audio_size);
circlebuf_push_back(&data->excess_frames[track_order][i],
in.data[i], in.frames * data->audio_size);
while (data->excess_frames[0].size >= frame_size_bytes) {
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[i],
data->samples[i], frame_size_bytes);
circlebuf_pop_front(&data->excess_frames[track_order][i],
data->samples[track_order][i],
frame_size_bytes);
encode_audio(output, context, data->audio_size);
encode_audio(output, track_order, context, data->audio_size);
}
}
@ -901,7 +982,7 @@ static uint64_t get_packet_sys_dts(struct ffmpeg_output *output,
time_base = data->video->time_base;
start_ts = output->video_start_ts;
} else {
time_base = data->audio->time_base;
time_base = data->audio_streams[0]->time_base;
start_ts = output->audio_start_ts;
}
@ -944,8 +1025,9 @@ static int process_packet(struct ffmpeg_output *output)
ret = av_interleaved_write_frame(output->ff_data.output, &packet);
if (ret < 0) {
av_free_packet(&packet);
blog(LOG_WARNING, "receive_audio: Error writing packet: %s",
av_err2str(ret));
ffmpeg_log_error(LOG_WARNING, &output->ff_data,
"receive_audio: Error writing packet: %s",
av_err2str(ret));
return ret;
}
@ -990,6 +1072,18 @@ static inline const char *get_string_or_null(obs_data_t *settings,
return value;
}
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;
}
static bool try_connect(struct ffmpeg_output *output)
{
video_t *video = obs_output_video(output->output);
@ -1025,6 +1119,8 @@ static bool try_connect(struct ffmpeg_output *output)
config.height = (int)obs_output_get_height(output->output);
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 ?
@ -1049,8 +1145,14 @@ static bool try_connect(struct ffmpeg_output *output)
success = ffmpeg_data_init(&output->ff_data, &config);
obs_data_release(settings);
if (!success)
if (!success) {
if (output->ff_data.last_error) {
obs_output_set_last_error(output->output,
output->ff_data.last_error);
}
ffmpeg_data_free(&output->ff_data);
return false;
}
struct audio_convert_info aci = {
.format = output->ff_data.audio_format
@ -1063,8 +1165,9 @@ static bool try_connect(struct ffmpeg_output *output)
ret = pthread_create(&output->write_thread, NULL, write_thread, output);
if (ret != 0) {
blog(LOG_WARNING, "ffmpeg_output_start: failed to create write "
"thread.");
ffmpeg_log_error(LOG_WARNING, &output->ff_data,
"ffmpeg_output_start: failed to create write "
"thread.");
ffmpeg_output_full_stop(output);
return false;
}
@ -1157,13 +1260,15 @@ 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,
.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,
.raw_video = receive_video,
.raw_audio = receive_audio,
.raw_audio2 = receive_audio,
.get_total_bytes = ffmpeg_output_total_bytes,
};