Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Patches to let libopenshot compile with ffmpeg version after 4.4 #670

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/FFmpegReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/FFmpegUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 25 additions & 8 deletions src/FFmpegWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -288,15 +292,19 @@ 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 {
// Set audio codec
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)
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -2040,7 +2053,11 @@ bool FFmpegWriter::write_video_packet(std::shared_ptr<Frame> 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);
Expand Down
5 changes: 4 additions & 1 deletion src/FFmpegWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down