Skip to content

Commit

Permalink
Return using AVStream::codec instead of codecpar
Browse files Browse the repository at this point in the history
Ref #21
  • Loading branch information
h4tr3d committed Apr 10, 2017
1 parent fa4437b commit 8b8255d
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 20 deletions.
16 changes: 16 additions & 0 deletions src/avutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ extern "C" {
#include <libavfilter/avfilter.h>
}

#define USE_CODECPAR (LIBAVFORMAT_VERSION_MAJOR >= 58)

#if defined(__ICL) || defined (__INTEL_COMPILER)
# define FF_DISABLE_DEPRECATION_WARNINGS __pragma(warning(push)) __pragma(warning(disable:1478))
# define FF_ENABLE_DEPRECATION_WARNINGS __pragma(warning(pop))
#elif defined(_MSC_VER)
# define FF_DISABLE_DEPRECATION_WARNINGS __pragma(warning(push)) __pragma(warning(disable:4996))
# define FF_ENABLE_DEPRECATION_WARNINGS __pragma(warning(pop))
#elif defined(__GNUC__) || defined(__clang__)
# define FF_DISABLE_DEPRECATION_WARNINGS _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
# define FF_ENABLE_DEPRECATION_WARNINGS _Pragma("GCC diagnostic warning \"-Wdeprecated-declarations\"")
#else
# define FF_DISABLE_DEPRECATION_WARNINGS
# define FF_ENABLE_DEPRECATION_WARNINGS
#endif

//
// Functions
//
Expand Down
18 changes: 13 additions & 5 deletions src/codeccontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,10 @@ CodecContext2::CodecContext2(const Stream &st, const Codec &codec, Direction dir
if (st.mediaType() != type)
throw av::Exception(make_avcpp_error(Errors::CodecInvalidMediaType));

#if !defined(FF_API_LAVF_AVCTX)
#if !USE_CODECPAR
FF_DISABLE_DEPRECATION_WARNINGS
auto const codecId = st.raw()->codec->codec_id;
FF_ENABLE_DEPRECATION_WARNINGS
#else
auto const codecId = st.raw()->codecpar->codec_id;
#endif
Expand All @@ -272,10 +274,12 @@ CodecContext2::CodecContext2(const Stream &st, const Codec &codec, Direction dir
c = findEncodingCodec(codecId);
}

#if !defined(FF_API_LAVF_AVCTX)
#if !USE_CODECPAR
FF_DISABLE_DEPRECATION_WARNINGS
m_raw = st.raw()->codec;
if (!c.isNull())
setCodec(c, false, direction, type);
FF_ENABLE_DEPRECATION_WARNINGS
#else
m_raw = avcodec_alloc_context3(c.raw());
if (m_raw) {
Expand All @@ -301,7 +305,7 @@ CodecContext2::~CodecContext2()
// So only stream-independ CodecContext's must be tracked and closed in ctor.
// Note: new FFmpeg API declares, that AVStream not owned by codec and deals only with codecpar
//
#if !defined(FF_API_LAVF_AVCTX)
#if !USE_CODECPAR
if (!m_stream.isNull())
return;
#endif
Expand Down Expand Up @@ -357,10 +361,12 @@ void CodecContext2::setCodec(const Codec &codec, bool resetDefaults, Direction d
}
}

#if !defined(FF_API_LAVF_AVCTX) // AVFORMAT < 57.5.0
#if !USE_CODECPAR // AVFORMAT < 57.5.0
FF_DISABLE_DEPRECATION_WARNINGS
if (m_stream.isValid()) {
m_stream.raw()->codec = m_raw;
}
FF_ENABLE_DEPRECATION_WARNINGS
#else
//if (m_stream.isValid())
// avcodec_parameters_from_context(m_stream.raw()->codecpar, m_raw);
Expand Down Expand Up @@ -465,10 +471,12 @@ void CodecContext2::copyContextFrom(const CodecContext2 &other, error_code &ec)
return;
}

#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57,5,0)
#if !USE_CODECPAR
FF_DISABLE_DEPRECATION_WARNINGS
int stat = avcodec_copy_context(m_raw, other.m_raw);
if (stat < 0)
throws_if(ec, stat, ffmpeg_category());
FF_ENABLE_DEPRECATION_WARNINGS
#else
AVCodecParameters params{};
avcodec_parameters_from_context(&params, other.m_raw);
Expand Down
4 changes: 3 additions & 1 deletion src/codeccontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,14 @@ class CodecContext2 : public FFWrapperPtr<AVCodecContext>, public noncopyable
outPacket.setTimeBase(inFrame.timeBase());
outPacket.setStreamIndex(inFrame.streamIndex());
} else if (m_stream.isValid()) {
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 51, 100)
#if USE_CODECPAR
outPacket.setTimeBase(av_stream_get_codec_timebase(m_stream.raw()));
#else
FF_DISABLE_DEPRECATION_WARNINGS
if (m_stream.raw()->codec) {
outPacket.setTimeBase(m_stream.raw()->codec->time_base);
}
FF_ENABLE_DEPRECATION_WARNINGS
#endif
outPacket.setStreamIndex(m_stream.index());
}
Expand Down
30 changes: 20 additions & 10 deletions src/codeccontext_deprecated.inl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ CodecContextDeprecated::CodecContextDeprecated(const Stream &st, const Codec &co

Codec c = codec;

#if !defined(FF_API_LAVF_AVCTX)
#if !USE_CODECPAR
FF_DISABLE_DEPRECATION_WARNINGS
auto const codecId = st.raw()->codec->codec_id;
FF_ENABLE_DEPRECATION_WARNINGS
#else
auto const codecId = st.raw()->codecpar->codec_id;
#endif
Expand All @@ -40,8 +42,10 @@ CodecContextDeprecated::CodecContextDeprecated(const Stream &st, const Codec &co
}


#if !defined(FF_API_LAVF_AVCTX)
#if !USE_CODECPAR
FF_DISABLE_DEPRECATION_WARNINGS
m_raw = st.raw()->codec;
FF_ENABLE_DEPRECATION_WARNINGS
#else
m_raw = avcodec_alloc_context3(c.raw());
#endif
Expand All @@ -59,7 +63,7 @@ CodecContextDeprecated::CodecContextDeprecated(const Codec &codec)

CodecContextDeprecated::~CodecContextDeprecated()
{
#if !defined(FF_API_LAVF_AVCTX)
#if !USE_CODECPAR
if (m_stream.isNull())
return;
#endif
Expand Down Expand Up @@ -155,10 +159,12 @@ void CodecContextDeprecated::setCodec(const Codec &codec, bool resetDefaults, er
}
}

#if !defined(FF_API_LAVF_AVCTX) // AVFORMAT < 57.5.0
#if !USE_CODECPAR
FF_DISABLE_DEPRECATION_WARNINGS
if (m_stream.isValid()) {
m_stream.raw()->codec = m_raw;
}
FF_ENABLE_DEPRECATION_WARNINGS
#else
avcodec_parameters_from_context(m_stream.raw()->codecpar, m_raw);
#endif
Expand Down Expand Up @@ -259,10 +265,12 @@ void CodecContextDeprecated::copyContextFrom(const CodecContextDeprecated &other
return;
}

#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57,5,0)
#if !USE_CODECPAR
FF_DISABLE_DEPRECATION_WARNINGS
int stat = avcodec_copy_context(m_raw, other.m_raw);
if (stat < 0)
throws_if(ec, stat, ffmpeg_category());
FF_ENABLE_DEPRECATION_WARNINGS
#else
AVCodecParameters params{};
avcodec_parameters_from_context(&params, other.m_raw);
Expand Down Expand Up @@ -951,12 +959,14 @@ std::pair<ssize_t, const error_category *> CodecContextDeprecated::encodeCommon(
outPacket.setTimeBase(inFrame.timeBase());
outPacket.setStreamIndex(inFrame.streamIndex());
} else if (m_stream.isValid()) {
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 51, 100)
outPacket.setTimeBase(av_stream_get_codec_timebase(m_stream.raw()));
#if USE_CODECPAR
outPacket.setTimeBase(av_stream_get_codec_timebase(m_stream.raw()));
#else
if (m_stream.raw()->codec) {
outPacket.setTimeBase(m_stream.raw()->codec->time_base);
}
FF_DISABLE_DEPRECATION_WARNINGS
if (m_stream.raw()->codec) {
outPacket.setTimeBase(m_stream.raw()->codec->time_base);
}
FF_ENABLE_DEPRECATION_WARNINGS
#endif
outPacket.setStreamIndex(m_stream.index());
}
Expand Down
12 changes: 9 additions & 3 deletions src/formatcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,14 @@ Stream FormatContext::addStream(const Codec &codec, error_code &ec)

auto stream = Stream(m_monitor, st, Direction::Encoding);

#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57,5,0)
#if !USE_CODECPAR
FF_DISABLE_DEPRECATION_WARNINGS
if (st->codec) {
if (outputFormat().isFlags(AVFMT_GLOBALHEADER)) {
st->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
}
FF_ENABLE_DEPRECATION_WARNINGS
#endif

return stream;
Expand Down Expand Up @@ -589,7 +591,8 @@ void FormatContext::openOutput(const string &uri, OutputFormat format, AVDiction
}

// Fix stream flags
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57,5,0)
#if !USE_CODECPAR
FF_DISABLE_DEPRECATION_WARNINGS
for (size_t i = 0; i < streamsCount(); ++i) {
auto st = stream(i);
if (st.raw()->codec) {
Expand All @@ -598,6 +601,7 @@ void FormatContext::openOutput(const string &uri, OutputFormat format, AVDiction
}
}
}
FF_ENABLE_DEPRECATION_WARNINGS
#endif

resetSocketAccess();
Expand Down Expand Up @@ -920,13 +924,15 @@ void FormatContext::findStreamInfo(AVDictionary **options, size_t optionsCount,
void FormatContext::closeCodecContexts()
{
// HACK: This is hack to correct cleanup codec contexts in independ way
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57,5,0)
#if !USE_CODECPAR
FF_DISABLE_DEPRECATION_WARNINGS
auto nb = m_raw->nb_streams;
for (size_t i = 0; i < nb; ++i) {
auto st = m_raw->streams[i];
auto ctx = st->codec;
avcodec_close(ctx);
}
FF_ENABLE_DEPRECATION_WARNINGS
#endif
}

Expand Down
4 changes: 3 additions & 1 deletion src/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ Timestamp Stream::currentDts() const

AVMediaType Stream::mediaType() const
{
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57,5,0)
#if !USE_CODECPAR
FF_DISABLE_DEPRECATION_WARNINGS
return RAW_GET2(isValid() && m_raw->codec, codec->codec_type, AVMEDIA_TYPE_UNKNOWN);
FF_ENABLE_DEPRECATION_WARNINGS
#else
return RAW_GET2(isValid() && m_raw->codecpar, codecpar->codec_type, AVMEDIA_TYPE_UNKNOWN);
#endif
Expand Down

0 comments on commit 8b8255d

Please sign in to comment.