From 17d513132196a70bcfe7d6cd94d04560a39c5e0a Mon Sep 17 00:00:00 2001 From: eisneinechse <42617957+eisneinechse@users.noreply.github.com> Date: Sat, 1 May 2021 17:18:26 -0700 Subject: [PATCH 1/3] (WIP) Patches to let libopenshot compile with ffmpeg 4.4 This is a work in progress. The export of the audio is still broken which started on June 18 with the removal of deprecated call in ffmpeg. --- src/FFmpegReader.cpp | 4 ++-- src/FFmpegUtilities.h | 4 ++++ src/FFmpegWriter.cpp | 33 +++++++++++++++++++++++++-------- src/FFmpegWriter.h | 2 +- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp index e4eaab2c8..ead698b8f 100644 --- a/src/FFmpegReader.cpp +++ b/src/FFmpegReader.cpp @@ -263,7 +263,7 @@ void FFmpegReader::Open() { AVCodecID codecId = AV_FIND_DECODER_CODEC_ID(pStream); // Get codec and codec context from stream - AVCodec *pCodec = avcodec_find_decoder(codecId); + const AVCodec *pCodec = avcodec_find_decoder(codecId); AVDictionary *opts = NULL; int retry_decode_open = 2; // If hw accel is selected but hardware cannot handle repeat with software decoding @@ -517,7 +517,7 @@ void FFmpegReader::Open() { AVCodecID codecId = AV_FIND_DECODER_CODEC_ID(aStream); // Get codec and codec context from stream - AVCodec *aCodec = avcodec_find_decoder(codecId); + const AVCodec *aCodec = avcodec_find_decoder(codecId); aCodecCtx = AV_GET_CODEC_CONTEXT(aStream, aCodec); // Set number of threads equal to number of processors (not to exceed 16) diff --git a/src/FFmpegUtilities.h b/src/FFmpegUtilities.h index 2f914128a..496874317 100644 --- a/src/FFmpegUtilities.h +++ b/src/FFmpegUtilities.h @@ -41,6 +41,10 @@ #define IS_FFMPEG_3_2 (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)) #endif + #ifndef IS_FFMPEG_4_4 + #define IS_FFMPEG_4_4 (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(58, 134, 100)) + #endif + #ifndef HAVE_HW_ACCEL #define HAVE_HW_ACCEL (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 107, 100)) #endif diff --git a/src/FFmpegWriter.cpp b/src/FFmpegWriter.cpp index b3965ff66..60d22618f 100644 --- a/src/FFmpegWriter.cpp +++ b/src/FFmpegWriter.cpp @@ -172,7 +172,7 @@ void FFmpegWriter::initialize_streams() { void FFmpegWriter::SetVideoOptions(bool has_video, std::string codec, Fraction fps, int width, int height, Fraction pixel_ratio, bool interlaced, bool top_field_first, int bit_rate) { // Set the video options if (codec.length() > 0) { - AVCodec *new_codec; + const AVCodec *new_codec; // Check if the codec selected is a hardware accelerated codec #if HAVE_HW_ACCEL #if defined(__linux__) @@ -236,7 +236,11 @@ void FFmpegWriter::SetVideoOptions(bool has_video, std::string codec, Fraction f info.vcodec = new_codec->name; // Update video codec in fmt +#if IS_FFMPEG_4_4 +// fmt->video_codec = new_codec->id; +#else fmt->video_codec = new_codec->id; +#endif } } if (fps.num > 0) { @@ -294,7 +298,7 @@ void FFmpegWriter::SetVideoOptions(std::string codec, int width, int height, Fr void FFmpegWriter::SetAudioOptions(bool has_audio, std::string codec, int sample_rate, int channels, ChannelLayout channel_layout, int bit_rate) { // Set audio options if (codec.length() > 0) { - AVCodec *new_codec = avcodec_find_encoder_by_name(codec.c_str()); + const AVCodec *new_codec = avcodec_find_encoder_by_name(codec.c_str()); if (new_codec == NULL) throw InvalidCodec("A valid audio codec could not be found for this file.", path); else { @@ -302,7 +306,11 @@ void FFmpegWriter::SetAudioOptions(bool has_audio, std::string codec, int sample info.acodec = new_codec->name; // Update audio codec in fmt +#if IS_FFMPEG_4_4 + // fmt->audio_codec = new_codec->id; +#else fmt->audio_codec = new_codec->id; +#endif } } if (sample_rate > 7999) @@ -1067,7 +1075,7 @@ AVStream *FFmpegWriter::add_audio_stream() { AVStream *st; // Find the audio codec - AVCodec *codec = avcodec_find_encoder_by_name(info.acodec.c_str()); + const AVCodec *codec = avcodec_find_encoder_by_name(info.acodec.c_str()); if (codec == NULL) throw InvalidCodec("A valid audio codec could not be found for this file.", path); @@ -1152,7 +1160,7 @@ AVStream *FFmpegWriter::add_video_stream() { AVStream *st; // Find the video codec - AVCodec *codec = avcodec_find_encoder_by_name(info.vcodec.c_str()); + const AVCodec *codec = avcodec_find_encoder_by_name(info.vcodec.c_str()); if (codec == NULL) throw InvalidCodec("A valid video codec could not be found for this file.", path); @@ -1267,8 +1275,13 @@ AVStream *FFmpegWriter::add_video_stream() { #if (LIBAVFORMAT_VERSION_MAJOR >= 58) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#if IS_FFMPEG_4_4 + //st->codec->time_base.num = info.video_timebase.num; + //st->codec->time_base.den = info.video_timebase.den; +#else st->codec->time_base.num = info.video_timebase.num; st->codec->time_base.den = info.video_timebase.den; +#endif #pragma GCC diagnostic pop #endif @@ -1332,14 +1345,14 @@ AVStream *FFmpegWriter::add_video_stream() { // open audio codec void FFmpegWriter::open_audio(AVFormatContext *oc, AVStream *st) { - AVCodec *codec; + //AVCodec *codec; AV_GET_CODEC_FROM_STREAM(st, audio_codec_ctx) // Set number of threads equal to number of processors (not to exceed 16) audio_codec_ctx->thread_count = std::min(FF_NUM_PROCESSORS, 16); // Find the audio encoder - codec = avcodec_find_encoder_by_name(info.acodec.c_str()); + const AVCodec *codec = avcodec_find_encoder_by_name(info.acodec.c_str()); if (!codec) codec = avcodec_find_encoder(audio_codec_ctx->codec_id); if (!codec) @@ -1403,7 +1416,7 @@ void FFmpegWriter::open_audio(AVFormatContext *oc, AVStream *st) { // open video codec void FFmpegWriter::open_video(AVFormatContext *oc, AVStream *st) { - AVCodec *codec; + //AVCodec *codec; AV_GET_CODEC_FROM_STREAM(st, video_codec_ctx) // Set number of threads equal to number of processors (not to exceed 16) @@ -1451,7 +1464,7 @@ void FFmpegWriter::open_video(AVFormatContext *oc, AVStream *st) { #endif // HAVE_HW_ACCEL /* find the video encoder */ - codec = avcodec_find_encoder_by_name(info.vcodec.c_str()); + const AVCodec *codec = avcodec_find_encoder_by_name(info.vcodec.c_str()); if (!codec) codec = avcodec_find_encoder(AV_FIND_DECODER_CODEC_ID(st)); if (!codec) @@ -2046,7 +2059,11 @@ bool FFmpegWriter::write_video_packet(std::shared_ptr frame, AVFrame *fra pkt.flags |= AV_PKT_FLAG_KEY; pkt.stream_index = video_st->index; pkt.data = (uint8_t *) frame_final->data; +#if IS_FFMPEG_4_4 + pkt.size = sizeof(AVFrame); +#else pkt.size = sizeof(AVPicture); +#endif // Increment PTS (in frames and scaled to the codec's timebase) write_video_count += av_rescale_q(1, av_make_q(info.fps.den, info.fps.num), video_codec_ctx->time_base); diff --git a/src/FFmpegWriter.h b/src/FFmpegWriter.h index 44f7cb355..9ebbe7b30 100644 --- a/src/FFmpegWriter.h +++ b/src/FFmpegWriter.h @@ -158,7 +158,7 @@ namespace openshot { bool write_header; bool write_trailer; - AVOutputFormat *fmt; + const AVOutputFormat *fmt; AVFormatContext *oc; AVStream *audio_st, *video_st; AVCodecContext *video_codec_ctx; From 4f8cb990472ab7984cb59c945cb32bd075e6c94a Mon Sep 17 00:00:00 2001 From: eisneinechse <42617957+eisneinechse@users.noreply.github.com> Date: Sat, 1 May 2021 17:37:07 -0700 Subject: [PATCH 2/3] forgotten patch --- src/FFmpegWriter.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/FFmpegWriter.h b/src/FFmpegWriter.h index 9ebbe7b30..96eeac26e 100644 --- a/src/FFmpegWriter.h +++ b/src/FFmpegWriter.h @@ -157,8 +157,11 @@ namespace openshot { bool prepare_streams; bool write_header; bool write_trailer; - +#if IS_FFMPEG_4_4 const AVOutputFormat *fmt; +#else + AVOutputFormat *fmt; +#endif AVFormatContext *oc; AVStream *audio_st, *video_st; AVCodecContext *video_codec_ctx; From 80425e9a86cd68adc1de0a71e434e5a2bdc14f4e Mon Sep 17 00:00:00 2001 From: eisneinechse <42617957+eisneinechse@users.noreply.github.com> Date: Sun, 13 Jun 2021 18:55:58 -0700 Subject: [PATCH 3/3] Changes are only needed for versions after 4.4 This adjusts this --- src/FFmpegUtilities.h | 4 ++-- src/FFmpegWriter.cpp | 8 ++++---- src/FFmpegWriter.h | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/FFmpegUtilities.h b/src/FFmpegUtilities.h index 496874317..43ee1ab25 100644 --- a/src/FFmpegUtilities.h +++ b/src/FFmpegUtilities.h @@ -41,8 +41,8 @@ #define IS_FFMPEG_3_2 (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)) #endif - #ifndef IS_FFMPEG_4_4 - #define IS_FFMPEG_4_4 (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(58, 134, 100)) + #ifndef IS_FFMPEG_4_5 + #define IS_FFMPEG_4_5 (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(58, 136, 100)) #endif #ifndef HAVE_HW_ACCEL diff --git a/src/FFmpegWriter.cpp b/src/FFmpegWriter.cpp index 60d22618f..7d673aca3 100644 --- a/src/FFmpegWriter.cpp +++ b/src/FFmpegWriter.cpp @@ -236,7 +236,7 @@ void FFmpegWriter::SetVideoOptions(bool has_video, std::string codec, Fraction f info.vcodec = new_codec->name; // Update video codec in fmt -#if IS_FFMPEG_4_4 +#if IS_FFMPEG_4_5 // fmt->video_codec = new_codec->id; #else fmt->video_codec = new_codec->id; @@ -306,7 +306,7 @@ void FFmpegWriter::SetAudioOptions(bool has_audio, std::string codec, int sample info.acodec = new_codec->name; // Update audio codec in fmt -#if IS_FFMPEG_4_4 +#if IS_FFMPEG_4_5 // fmt->audio_codec = new_codec->id; #else fmt->audio_codec = new_codec->id; @@ -1275,7 +1275,7 @@ AVStream *FFmpegWriter::add_video_stream() { #if (LIBAVFORMAT_VERSION_MAJOR >= 58) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" -#if IS_FFMPEG_4_4 +#if IS_FFMPEG_4_5 //st->codec->time_base.num = info.video_timebase.num; //st->codec->time_base.den = info.video_timebase.den; #else @@ -2059,7 +2059,7 @@ bool FFmpegWriter::write_video_packet(std::shared_ptr frame, AVFrame *fra pkt.flags |= AV_PKT_FLAG_KEY; pkt.stream_index = video_st->index; pkt.data = (uint8_t *) frame_final->data; -#if IS_FFMPEG_4_4 +#if IS_FFMPEG_4_5 pkt.size = sizeof(AVFrame); #else pkt.size = sizeof(AVPicture); diff --git a/src/FFmpegWriter.h b/src/FFmpegWriter.h index 96eeac26e..1a0a26117 100644 --- a/src/FFmpegWriter.h +++ b/src/FFmpegWriter.h @@ -157,7 +157,7 @@ namespace openshot { bool prepare_streams; bool write_header; bool write_trailer; -#if IS_FFMPEG_4_4 +#if IS_FFMPEG_4_5 const AVOutputFormat *fmt; #else AVOutputFormat *fmt;