[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#1072431: Patch



The attached patch allows compilation of libopenshot against ffmpeg 7

diff -Nru libopenshot-0.3.2.orig/src/FFmpegReader.cpp libopenshot-0.3.2/src/FFmpegReader.cpp
--- libopenshot-0.3.2.orig/src/FFmpegReader.cpp	2023-04-19 18:01:02
+++ libopenshot-0.3.2/src/FFmpegReader.cpp	2024-09-05 14:14:42
@@ -671,8 +671,8 @@
 
 void FFmpegReader::UpdateAudioInfo() {
 	// Set default audio channel layout (if needed)
-	if (AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->channel_layout == 0)
-		AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->channel_layout = av_get_default_channel_layout(AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->channels);
+    if (!av_channel_layout_check(&(AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->ch_layout)))
+        AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->ch_layout = (AVChannelLayout) AV_CHANNEL_LAYOUT_STEREO;
 
 	if (info.sample_rate > 0) {
 		// Skip init - if info struct already populated
@@ -683,8 +683,10 @@
 	info.has_audio = true;
 	info.file_size = pFormatCtx->pb ? avio_size(pFormatCtx->pb) : -1;
 	info.acodec = aCodecCtx->codec->name;
-	info.channels = AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->channels;
-	info.channel_layout = (ChannelLayout) AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->channel_layout;
+	
+    info.channels = AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->ch_layout.nb_channels;
+    info.channel_layout = (ChannelLayout) AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->ch_layout.u.mask;
+    
 	info.sample_rate = AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->sample_rate;
 	info.audio_bit_rate = AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->bit_rate;
 	if (info.audio_bit_rate <= 0) {
@@ -1593,13 +1595,15 @@
 
 		// determine how many samples were decoded
 		int plane_size = -1;
-		data_size = av_samples_get_buffer_size(&plane_size,
-											   AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->channels,
+        int nb_channels = AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->ch_layout.nb_channels;
+        
+        data_size = av_samples_get_buffer_size(&plane_size,
+											   nb_channels,
 											   audio_frame->nb_samples,
 											   (AVSampleFormat) (AV_GET_SAMPLE_FORMAT(aStream, aCodecCtx)), 1);
 
 		// Calculate total number of samples
-		packet_samples = audio_frame->nb_samples * AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->channels;
+		packet_samples = audio_frame->nb_samples * nb_channels;
 	} else {
 		if (audio_frame) {
 			// Free audio frame
@@ -1662,14 +1666,12 @@
 
 	// setup resample context
 	avr = SWR_ALLOC();
-	av_opt_set_int(avr, "in_channel_layout", AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->channel_layout, 0);
-	av_opt_set_int(avr, "out_channel_layout", AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->channel_layout, 0);
+    av_opt_set_chlayout(avr, "in_chlayout", &AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->ch_layout, 0);
+    av_opt_set_chlayout(avr, "out_chlayout", &AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->ch_layout, 0);
 	av_opt_set_int(avr, "in_sample_fmt", AV_GET_SAMPLE_FORMAT(aStream, aCodecCtx), 0);
 	av_opt_set_int(avr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
 	av_opt_set_int(avr, "in_sample_rate", info.sample_rate, 0);
 	av_opt_set_int(avr, "out_sample_rate", info.sample_rate, 0);
-	av_opt_set_int(avr, "in_channels", info.channels, 0);
-	av_opt_set_int(avr, "out_channels", info.channels, 0);
 	SWR_INIT(avr);
 
 	// Convert audio samples
diff -Nru libopenshot-0.3.2.orig/src/FFmpegWriter.cpp libopenshot-0.3.2/src/FFmpegWriter.cpp
--- libopenshot-0.3.2.orig/src/FFmpegWriter.cpp	2023-04-19 18:01:02
+++ libopenshot-0.3.2/src/FFmpegWriter.cpp	2024-09-05 14:05:58
@@ -1113,7 +1113,6 @@
 
 	// Set the sample parameters
 	c->bit_rate = info.audio_bit_rate;
-	c->channels = info.channels;
 
 	// Set valid sample rate (or throw error)
 	if (codec->supported_samplerates) {
@@ -1132,20 +1131,23 @@
 
 
 	// Set a valid number of channels (or throw error)
-	const uint64_t channel_layout = info.channel_layout;
-	if (codec->channel_layouts) {
-		int i;
-		for (i = 0; codec->channel_layouts[i] != 0; i++)
-			if (channel_layout == codec->channel_layouts[i]) {
-				// Set valid channel layout
-				c->channel_layout = channel_layout;
-				break;
-			}
-		if (codec->channel_layouts[i] == 0)
-			throw InvalidChannels("An invalid channel layout was detected (i.e. MONO / STEREO).", path);
-	} else
-		// Set valid channel layout
-		c->channel_layout = channel_layout;
+	uint64_t channel_layout = info.channel_layout;
+    // Set a valid number of channels (or throw error)
+    AVChannelLayout ch_layout;
+    av_channel_layout_from_mask(&ch_layout, info.channel_layout);
+    if (codec->ch_layouts) {
+        int i;
+        for (i = 0; av_channel_layout_check(&codec->ch_layouts[i]); i++)
+            if (av_channel_layout_compare(&ch_layout, &codec->ch_layouts[i])) {
+                // Set valid channel layout
+                av_channel_layout_copy(&c->ch_layout, &ch_layout);
+                break;
+            }
+        if (!av_channel_layout_check(&codec->ch_layouts[i]))
+            throw InvalidChannels("An invalid channel layout was detected (i.e. MONO / STEREO).", path);
+    } else
+        // Set valid channel layout
+        av_channel_layout_copy(&c->ch_layout, &ch_layout);
 
 	// Choose a valid sample_fmt
 	if (codec->sample_fmts) {
@@ -1171,13 +1173,22 @@
 
 	AV_COPY_PARAMS_FROM_CONTEXT(st, c);
 
+int nb_channels;
+const char* nb_channels_label;
+const char* channel_layout_label;
+
+nb_channels = c->ch_layout.nb_channels;
+channel_layout = c->ch_layout.u.mask;
+nb_channels_label = "c->ch_layout.nb_channels";
+channel_layout_label = "c->ch_layout.u.mask";
+
 	ZmqLogger::Instance()->AppendDebugMethod(
 		"FFmpegWriter::add_audio_stream",
 		"c->codec_id", c->codec_id,
 		"c->bit_rate", c->bit_rate,
-		"c->channels", c->channels,
+		nb_channels_label, nb_channels,
 		"c->sample_fmt", c->sample_fmt,
-		"c->channel_layout", c->channel_layout,
+		channel_layout_label, channel_layout,
 		"c->sample_rate", c->sample_rate);
 
 	return st;
@@ -1878,9 +1889,7 @@
 
 			// Create output frame (and allocate arrays)
 			frame_final->nb_samples = audio_input_frame_size;
-			frame_final->channels = info.channels;
-			frame_final->format = audio_codec_ctx->sample_fmt;
-			frame_final->channel_layout = info.channel_layout;
+			av_channel_layout_from_mask(&frame_final->ch_layout, info.channel_layout);
 			av_samples_alloc(frame_final->data, frame_final->linesize, info.channels,
 				frame_final->nb_samples, audio_codec_ctx->sample_fmt, 0);
 
@@ -1929,7 +1938,8 @@
 			frame_final->nb_samples = audio_input_frame_size;
 
 			// Fill the final_frame AVFrame with audio (non planar)
-			avcodec_fill_audio_frame(frame_final, audio_codec_ctx->channels,
+            int nb_channels = audio_codec_ctx->ch_layout.nb_channels;
+			avcodec_fill_audio_frame(frame_final, nb_channels,
 				audio_codec_ctx->sample_fmt, (uint8_t *) final_samples,
 				audio_encoder_buffer_size, 0);
 		}

Reply to: