Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/FFmpegUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 25 additions & 8 deletions src/FFmpegWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -294,15 +298,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_4
// fmt->audio_codec = new_codec->id;
#else
fmt->audio_codec = new_codec->id;
#endif
}
}
if (sample_rate > 7999)
Expand Down Expand Up @@ -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);

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

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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -2046,7 +2059,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_4
pkt.size = sizeof(AVFrame);
#else
pkt.size = sizeof(AVPicture);
#endif

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah! Well, crap. I only just noticed that this 4-month-old comment was still "Pending", and only visible to me! Whoops. Sorry about that.


Instead of directly setting packet.size, should this be using av_packet_from_data(&pkt, buffer, size)?

Or, in the cases where the data isn't in an av_malloc()'d buffer like _from_data() requires (i.e. audio), at the very least an av_new_packet(&pkt, size) followed by a memcpy()?

av_init_packet() has now been deprecated, a change that was formally made only recently... but in part, it's been deprecated because there have been better replacements for a very long time. Both av_new_packet() and av_packet_from_data() were available as far back as FFmpeg 2.4. So, we could switch our existing code away from it for all FFmpeg versions, not just 4.4+.


...Ultimately that's what I ended up doing in #784, since av_packet_from_data has "always" (as far as we're concerned) been available. So now we just leave it to avcodec (or is it avformat? avutil?) to figure out how to size things. I figure it should know best!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, no. That change was in #809.


// 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_4
const AVOutputFormat *fmt;
#else
AVOutputFormat *fmt;
#endif
AVFormatContext *oc;
AVStream *audio_st, *video_st;
AVCodecContext *video_codec_ctx;
Expand Down