diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp index b97d7345b..0839f885d 100644 --- a/src/FFmpegReader.cpp +++ b/src/FFmpegReader.cpp @@ -257,7 +257,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 @@ -511,7 +511,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 d8b1587fb..a4783e391 100644 --- a/src/FFmpegUtilities.h +++ b/src/FFmpegUtilities.h @@ -39,6 +39,14 @@ #define UINT64_C(c) (c ## ULL) #endif +#ifndef IS_FFMPEG_4_5 +#define IS_FFMPEG_4_5 (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(58, 136, 100)) +#endif + +#ifndef USE_HW_ACCEL +#define USE_HW_ACCEL (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 107, 100)) +#endif + #ifndef IS_FFMPEG_3_2 #define IS_FFMPEG_3_2 (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)) #endif diff --git a/src/FFmpegWriter.cpp b/src/FFmpegWriter.cpp index 4cfc46333..35a3f06bd 100644 --- a/src/FFmpegWriter.cpp +++ b/src/FFmpegWriter.cpp @@ -166,7 +166,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 USE_HW_ACCEL #if defined(__linux__) @@ -230,7 +230,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_5 +// fmt->video_codec = new_codec->id; +#else fmt->video_codec = new_codec->id; +#endif } } if (fps.num > 0) { @@ -288,7 +292,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 { @@ -296,7 +300,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_5 + // fmt->audio_codec = new_codec->id; +#else fmt->audio_codec = new_codec->id; +#endif } } if (sample_rate > 7999) @@ -1061,7 +1069,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); @@ -1146,7 +1154,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); @@ -1261,8 +1269,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_5 + //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 @@ -1326,14 +1339,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) @@ -1397,7 +1410,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) @@ -1445,7 +1458,7 @@ void FFmpegWriter::open_video(AVFormatContext *oc, AVStream *st) { #endif // USE_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) @@ -2040,7 +2053,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_5 + 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..1a0a26117 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_5 + const AVOutputFormat *fmt; +#else AVOutputFormat *fmt; +#endif AVFormatContext *oc; AVStream *audio_st, *video_st; AVCodecContext *video_codec_ctx;