obs-ffmpeg: Fix for channel layout API change

The channel_layout API was overhauled by FFmpeg [1-4]. The previous
bitmask channel_layout is replaced by a struct ch_layout which combines
the number of channels, a bitmask and other infos. This struct must now
be supplied to AVframes since avutil >= 57.24.100 and to
AVCodecContext since avcodec 59.24.100 per (1].
This commit provides the required info to ffmpeg-mux,
obs-ffmpeg-output & to obs-ffmpeg-audio-encoders.

[1] Bump minor versions after the channel layout changes
https://github.com/FFmpeg/FFmpeg/commit/cdba98bb80e2ab73d34659c610771b020afc6a77
[2] lavc: switch to the new channel layout API
https://github.com/FFmpeg/FFmpeg/commit/548aeb93834b8425c86d1ce60fddc1d41805724d
[3] avutil/channel_layout: Add a new channel layout API
https://github.com/FFmpeg/FFmpeg/commit/086a8048061bf9fb4c63943f6962db48175f655c
[4] avframe: switch to the new channel layout API db6efa18
https://github.com/FFmpeg/FFmpeg/commit/db6efa1815e217ed76f39aee8b15ee5c64698537

Signed-off-by: pkv <pkv@obsproject.com>
This commit is contained in:
pkv
2022-05-16 16:17:32 -04:00
committed by Ryan Foster
parent e66542075d
commit 5b6cc73c24
3 changed files with 46 additions and 30 deletions
@@ -495,6 +495,7 @@ static void create_audio_stream(struct ffmpeg_mux *ffm, int idx)
context->time_base = stream->time_base;
context->extradata = extradata;
context->extradata_size = ffm->audio_header[idx].size;
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
context->channel_layout =
av_get_default_channel_layout(context->channels);
//avutil default channel layout for 4 channels is 4.0 ; fix for quad
@@ -503,6 +504,12 @@ static void create_audio_stream(struct ffmpeg_mux *ffm, int idx)
//avutil default channel layout for 5 channels is 5.0 ; fix for 4.1
if (context->channels == 5)
context->channel_layout = av_get_channel_layout("4.1");
#else
av_channel_layout_default(&context->ch_layout, context->channels);
//avutil default channel layout for 5 channels is 5.0 ; fix for 4.1
if (context->channels == 5)
context->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT1;
#endif
if (ffm->output->oformat->flags & AVFMT_GLOBALHEADER)
context->flags |= CODEC_FLAG_GLOBAL_H;
+29 -28
View File
@@ -57,6 +57,7 @@ struct enc_encoder {
int frame_size_bytes;
};
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
static inline uint64_t convert_speaker_layout(enum speaker_layout layout)
{
switch (layout) {
@@ -81,30 +82,7 @@ static inline uint64_t convert_speaker_layout(enum speaker_layout layout)
/* shouldn't get here */
return 0;
}
static inline enum speaker_layout
convert_ff_channel_layout(uint64_t channel_layout)
{
switch (channel_layout) {
case AV_CH_LAYOUT_MONO:
return SPEAKERS_MONO;
case AV_CH_LAYOUT_STEREO:
return SPEAKERS_STEREO;
case AV_CH_LAYOUT_SURROUND:
return SPEAKERS_2POINT1;
case AV_CH_LAYOUT_4POINT0:
return SPEAKERS_4POINT0;
case AV_CH_LAYOUT_4POINT1:
return SPEAKERS_4POINT1;
case AV_CH_LAYOUT_5POINT1_BACK:
return SPEAKERS_5POINT1;
case AV_CH_LAYOUT_7POINT1:
return SPEAKERS_7POINT1;
}
/* shouldn't get here */
return SPEAKERS_UNKNOWN;
}
#endif
static const char *aac_getname(void *unused)
{
@@ -157,7 +135,11 @@ static bool initialize_codec(struct enc_encoder *enc)
}
enc->aframe->format = enc->context->sample_fmt;
enc->aframe->channels = enc->context->channels;
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
enc->aframe->channel_layout = enc->context->channel_layout;
#else
enc->aframe->ch_layout = enc->context->ch_layout;
#endif
enc->aframe->sample_rate = enc->context->sample_rate;
enc->frame_size = enc->context->frame_size;
@@ -235,7 +217,15 @@ static void *enc_create(obs_data_t *settings, obs_encoder_t *encoder,
const struct audio_output_info *aoi;
aoi = audio_output_get_info(audio);
enc->context->channels = (int)audio_output_get_channels(audio);
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
enc->context->channel_layout = convert_speaker_layout(aoi->speakers);
#else
av_channel_layout_default(&enc->context->ch_layout,
enc->context->channels);
if (aoi->speakers == SPEAKERS_4POINT1)
enc->context->ch_layout =
(AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT1;
#endif
enc->context->sample_rate = audio_output_get_sample_rate(audio);
enc->context->sample_fmt = enc->codec->sample_fmts
? enc->codec->sample_fmts[0]
@@ -264,11 +254,18 @@ static void *enc_create(obs_data_t *settings, obs_encoder_t *encoder,
av_opt_set(enc->context->priv_data, "aac_coder", "fast", 0);
}
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
info("bitrate: %" PRId64 ", channels: %d, channel_layout: %x\n",
(int64_t)enc->context->bit_rate / 1000,
(int)enc->context->channels,
(unsigned int)enc->context->channel_layout);
#else
char buf[256];
av_channel_layout_describe(&enc->context->ch_layout, buf, 256);
info("bitrate: %" PRId64 ", channels: %d, channel_layout: %s\n",
(int64_t)enc->context->bit_rate / 1000,
(int)enc->context->channels, buf);
#endif
init_sizes(enc, audio);
/* enable experimental FFmpeg encoder if the only one available */
@@ -306,7 +303,9 @@ static bool do_encode(struct enc_encoder *enc, struct encoder_packet *packet,
enc->aframe->pts = av_rescale_q(
enc->total_samples, (AVRational){1, enc->context->sample_rate},
enc->context->time_base);
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 24, 100)
enc->aframe->ch_layout = enc->context->ch_layout;
#endif
ret = avcodec_fill_audio_frame(
enc->aframe, enc->context->channels, enc->context->sample_fmt,
enc->samples[0], enc->frame_size_bytes * enc->context->channels,
@@ -394,8 +393,10 @@ static void enc_audio_info(void *data, struct audio_convert_info *info)
struct enc_encoder *enc = data;
info->format = convert_ffmpeg_sample_format(enc->context->sample_fmt);
info->samples_per_sec = (uint32_t)enc->context->sample_rate;
info->speakers =
convert_ff_channel_layout(enc->context->channel_layout);
if (enc->context->channels != 7 && enc->context->channels <= 8)
info->speakers = (enum speaker_layout)(enc->context->channels);
else
info->speakers = SPEAKERS_UNKNOWN;
}
static size_t enc_frame_size(void *data)
+10 -2
View File
@@ -321,9 +321,12 @@ static bool open_audio_codec(struct ffmpeg_data *data, int idx)
data->aframe[idx]->format = context->sample_fmt;
data->aframe[idx]->channels = context->channels;
#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 24, 100)
data->aframe[idx]->channel_layout = context->channel_layout;
#else
data->aframe[idx]->ch_layout = context->ch_layout;
#endif
data->aframe[idx]->sample_rate = context->sample_rate;
context->strict_std_compliance = -2;
ret = avcodec_open2(context, data->acodec, NULL);
@@ -378,13 +381,18 @@ static bool create_audio_stream(struct ffmpeg_data *data, int idx)
context->time_base = (AVRational){1, aoi.samples_per_sec};
context->channels = get_audio_channels(aoi.speakers);
context->sample_rate = aoi.samples_per_sec;
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
context->channel_layout =
av_get_default_channel_layout(context->channels);
//avutil 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");
#else
av_channel_layout_default(&context->ch_layout, context->channels);
if (aoi.speakers == SPEAKERS_4POINT1)
context->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT1;
#endif
context->sample_fmt = data->acodec->sample_fmts
? data->acodec->sample_fmts[0]
: AV_SAMPLE_FMT_FLTP;