Browse Source

obs-ffmpeg: Fix deprecation of channels member of several structs

This fixes deprecation warnings since the channels member of
AVCodecContext is marked as deprecated [1], as well as the channels
member of AVFrame [2].
In all instances where a warning appear, a switch to the new API is
done.
[1] lavc: switch to the new channel layout API
FFmpeg/FFmpeg@548aeb9
[2] Bump minor versions after the channel layout changes
FFmpeg/FFmpeg@cdba98bb80

Signed-off-by: pkv <[email protected]>
pkv 3 years ago
parent
commit
ce2d1ffab7

+ 7 - 1
deps/media-playback/media-playback/media.c

@@ -353,6 +353,12 @@ static void mp_media_next_audio(mp_media_t *m)
 	struct mp_decode *d = &m->a;
 	struct obs_source_audio audio = {0};
 	AVFrame *f = d->frame;
+	int channels;
+#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 19, 100)
+	channels = f->channels;
+#else
+	channels = f->ch_layout.nb_channels;
+#endif
 
 	if (!mp_media_can_play_frame(m, d))
 		return;
@@ -365,7 +371,7 @@ static void mp_media_next_audio(mp_media_t *m)
 		audio.data[i] = f->data[i];
 
 	audio.samples_per_sec = f->sample_rate * m->speed / 100;
-	audio.speakers = convert_speaker_layout(f->channels);
+	audio.speakers = convert_speaker_layout(channels);
 	audio.format = convert_sample_format(f->format);
 	audio.frames = f->nb_samples;
 

+ 9 - 6
plugins/obs-ffmpeg/ffmpeg-mux/ffmpeg-mux.c

@@ -520,6 +520,7 @@ static void create_audio_stream(struct ffmpeg_mux *ffm, int idx)
 	AVStream *stream;
 	void *extradata = NULL;
 	const char *name = ffm->params.acodec;
+	int channels;
 
 	const AVCodecDescriptor *codec = avcodec_descriptor_get_by_name(name);
 	if (!codec) {
@@ -543,7 +544,10 @@ static void create_audio_stream(struct ffmpeg_mux *ffm, int idx)
 	context->codec_type = codec->type;
 	context->codec_id = codec->id;
 	context->bit_rate = (int64_t)ffm->audio[idx].abitrate * 1000;
-	context->channels = ffm->audio[idx].channels;
+	channels = ffm->audio[idx].channels;
+#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 24, 100)
+	context->channels = channels;
+#endif
 	context->sample_rate = ffm->audio[idx].sample_rate;
 	context->frame_size = ffm->audio[idx].frame_size;
 	context->sample_fmt = AV_SAMPLE_FMT_S16;
@@ -551,15 +555,14 @@ static void create_audio_stream(struct ffmpeg_mux *ffm, int idx)
 	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);
+	context->channel_layout = av_get_default_channel_layout(channels);
 	//avutil default channel layout for 5 channels is 5.0 ; fix for 4.1
-	if (context->channels == 5)
+	if (channels == 5)
 		context->channel_layout = av_get_channel_layout("4.1");
 #else
-	av_channel_layout_default(&context->ch_layout, context->channels);
+	av_channel_layout_default(&context->ch_layout, channels);
 	//avutil default channel layout for 5 channels is 5.0 ; fix for 4.1
-	if (context->channels == 5)
+	if (channels == 5)
 		context->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT1;
 #endif
 	if (ffm->output->oformat->flags & AVFMT_GLOBALHEADER)

+ 31 - 10
plugins/obs-ffmpeg/obs-ffmpeg-audio-encoders.c

@@ -116,6 +116,7 @@ static void enc_destroy(void *data)
 static bool initialize_codec(struct enc_encoder *enc)
 {
 	int ret;
+	int channels;
 
 	enc->aframe = av_frame_alloc();
 	if (!enc->aframe) {
@@ -134,7 +135,12 @@ static bool initialize_codec(struct enc_encoder *enc)
 		return false;
 	}
 	enc->aframe->format = enc->context->sample_fmt;
+#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 24, 100)
 	enc->aframe->channels = enc->context->channels;
+	channels = enc->context->channels;
+#else
+	channels = enc->context->ch_layout.nb_channels;
+#endif
 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
 	enc->aframe->channel_layout = enc->context->channel_layout;
 #else
@@ -148,8 +154,8 @@ static bool initialize_codec(struct enc_encoder *enc)
 
 	enc->frame_size_bytes = enc->frame_size * (int)enc->audio_size;
 
-	ret = av_samples_alloc(enc->samples, NULL, enc->context->channels,
-			       enc->frame_size, enc->context->sample_fmt, 0);
+	ret = av_samples_alloc(enc->samples, NULL, channels, enc->frame_size,
+			       enc->context->sample_fmt, 0);
 	if (ret < 0) {
 		warn("Failed to create audio buffer: %s", av_err2str(ret));
 		return false;
@@ -216,16 +222,21 @@ static void *enc_create(obs_data_t *settings, obs_encoder_t *encoder,
 	enc->context->bit_rate = bitrate * 1000;
 	const struct audio_output_info *aoi;
 	aoi = audio_output_get_info(audio);
+
+#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 24, 100)
 	enc->context->channels = (int)audio_output_get_channels(audio);
+#endif
+
 #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);
+				  (int)audio_output_get_channels(audio));
 	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,7 +275,7 @@ static void *enc_create(obs_data_t *settings, obs_encoder_t *encoder,
 	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);
+	     (int)enc->context->ch_layout.nb_channels, buf);
 #endif
 	init_sizes(enc, audio);
 
@@ -298,6 +309,7 @@ static bool do_encode(struct enc_encoder *enc, struct encoder_packet *packet,
 	AVPacket avpacket = {0};
 	int got_packet;
 	int ret;
+	int channels;
 
 	enc->aframe->nb_samples = enc->frame_size;
 	enc->aframe->pts = av_rescale_q(
@@ -305,11 +317,14 @@ static bool do_encode(struct enc_encoder *enc, struct encoder_packet *packet,
 		enc->context->time_base);
 #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 24, 100)
 	enc->aframe->ch_layout = enc->context->ch_layout;
+	channels = enc->context->ch_layout.nb_channels;
+#else
+	channels = enc->context->channels;
 #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,
-		1);
+	ret = avcodec_fill_audio_frame(enc->aframe, channels,
+				       enc->context->sample_fmt,
+				       enc->samples[0],
+				       enc->frame_size_bytes * channels, 1);
 	if (ret < 0) {
 		warn("avcodec_fill_audio_frame failed: %s", av_err2str(ret));
 		return false;
@@ -391,10 +406,16 @@ static bool enc_extra_data(void *data, uint8_t **extra_data, size_t *size)
 static void enc_audio_info(void *data, struct audio_convert_info *info)
 {
 	struct enc_encoder *enc = data;
+	int channels;
+#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 24, 100)
+	channels = enc->context->ch_layout.nb_channels;
+#else
+	channels = enc->context->channels;
+#endif
 	info->format = convert_ffmpeg_sample_format(enc->context->sample_fmt);
 	info->samples_per_sec = (uint32_t)enc->context->sample_rate;
-	if (enc->context->channels != 7 && enc->context->channels <= 8)
-		info->speakers = (enum speaker_layout)(enc->context->channels);
+	if (channels != 7 && channels <= 8)
+		info->speakers = (enum speaker_layout)(channels);
 	else
 		info->speakers = SPEAKERS_UNKNOWN;
 }

+ 5 - 1
plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c

@@ -235,6 +235,7 @@ static bool create_audio_stream(struct ffmpeg_output *stream,
 	void *extradata = NULL;
 	struct obs_audio_info aoi;
 	const char *name = data->config.audio_encoder;
+	int channels;
 
 	const AVCodecDescriptor *codec = avcodec_descriptor_get_by_name(name);
 	if (!codec) {
@@ -255,7 +256,10 @@ static bool create_audio_stream(struct ffmpeg_output *stream,
 	context->codec_id = codec->id;
 	context->bit_rate = (int64_t)data->config.audio_bitrate * 1000;
 	context->time_base = (AVRational){1, aoi.samples_per_sec};
+	channels = get_audio_channels(aoi.speakers);
+#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 24, 100)
 	context->channels = get_audio_channels(aoi.speakers);
+#endif
 	context->sample_rate = aoi.samples_per_sec;
 
 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
@@ -266,7 +270,7 @@ static bool create_audio_stream(struct ffmpeg_output *stream,
 	if (aoi.speakers == SPEAKERS_4POINT1)
 		context->channel_layout = av_get_channel_layout("4.1");
 #else
-	av_channel_layout_default(&context->ch_layout, context->channels);
+	av_channel_layout_default(&context->ch_layout, channels);
 	if (aoi.speakers == SPEAKERS_4POINT1)
 		context->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT1;
 #endif

+ 20 - 7
plugins/obs-ffmpeg/obs-ffmpeg-output.c

@@ -294,6 +294,7 @@ static bool open_audio_codec(struct ffmpeg_data *data, int idx)
 	AVCodecContext *const context = data->audio_infos[idx].ctx;
 	char **opts = strlist_split(data->config.audio_settings, ' ', false);
 	int ret;
+	int channels;
 
 	if (opts) {
 		parse_params(context, opts);
@@ -308,11 +309,13 @@ 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]->channels = context->channels;
 	data->aframe[idx]->channel_layout = context->channel_layout;
+	channels = context->channels;
 #else
 	data->aframe[idx]->ch_layout = context->ch_layout;
+	channels = context->ch_layout.nb_channels;
 #endif
 	data->aframe[idx]->sample_rate = context->sample_rate;
 	context->strict_std_compliance = -2;
@@ -327,7 +330,7 @@ static bool open_audio_codec(struct ffmpeg_data *data, int idx)
 
 	data->frame_size = context->frame_size ? context->frame_size : 1024;
 
-	ret = av_samples_alloc(data->samples[idx], NULL, context->channels,
+	ret = av_samples_alloc(data->samples[idx], NULL, channels,
 			       data->frame_size, context->sample_fmt, 0);
 	if (ret < 0) {
 		ffmpeg_log_error(LOG_WARNING, data,
@@ -349,6 +352,7 @@ static bool create_audio_stream(struct ffmpeg_data *data, int idx)
 	AVCodecContext *context;
 	AVStream *stream;
 	struct obs_audio_info aoi;
+	int channels;
 
 	if (!obs_get_audio_info(&aoi)) {
 		ffmpeg_log_error(LOG_WARNING, data, "No active audio");
@@ -367,17 +371,20 @@ static bool create_audio_stream(struct ffmpeg_data *data, int idx)
 #endif
 	context->bit_rate = (int64_t)data->config.audio_bitrate * 1000;
 	context->time_base = (AVRational){1, aoi.samples_per_sec};
+#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 24, 100)
 	context->channels = get_audio_channels(aoi.speakers);
+#endif
+	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);
+	context->channel_layout = av_get_default_channel_layout(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);
+	av_channel_layout_default(&context->ch_layout, channels);
 	if (aoi.speakers == SPEAKERS_4POINT1)
 		context->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT1;
 #endif
@@ -865,14 +872,20 @@ static void encode_audio(struct ffmpeg_output *output, int idx,
 
 	AVPacket *packet = NULL;
 	int ret, got_packet;
-	size_t total_size = data->frame_size * block_size * context->channels;
+	int channels;
+#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 24, 100)
+	channels = context->ch_layout.nb_channels;
+#else
+	channels = context->channels;
+#endif
+	size_t total_size = data->frame_size * block_size * 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);
 
-	ret = avcodec_fill_audio_frame(data->aframe[idx], context->channels,
+	ret = avcodec_fill_audio_frame(data->aframe[idx], channels,
 				       context->sample_fmt,
 				       data->samples[idx][0], (int)total_size,
 				       1);