Permalink
Browse files

Use preferred avcodec_parameters_to_context

From https://wiki.libav.org/Migration/12

"It used to be common practice to use this embedded AVCodecContext as the
actual encoding or decoding context. It also used to be passed to
bitstream filters and parsers. This process was extremely ill-defined
and unsafe, since all those components may see the stream at different
stages (since each component has its own, potentially quite large,
delay) and may try to overwrite various AVCodecContext fields in ways
that are unexpected for the other components."

"To solve those issues, a new AVCodecParameters object is introduced,
and its instance is added to AVStream as AVStream.codecpar. The demuxers
now export all codec parameters read from the input stream into this new
object. Similarly, the caller should set the stream parameters in this
object when muxing. For encoding or decoding, the caller should allocate
a separate AVCodecContext object.
avcodec_parameters_from_context()/avcodec_parameters_to_context()
functions are provided to transfer the stream parameters between an
AVCodecContext and AVCodecParameters, so that a caller can easily
initialise a decoder with demuxer information, or a muxer with encoder
information."
  • Loading branch information...
TheCycoONE committed Jun 22, 2017
1 parent cb3b74e commit bba218dc0b18c71d116b637f914ba47450e5d674
Showing with 17 additions and 2 deletions.
  1. +14 −2 CorsixTH/Src/th_movie.cpp
  2. +3 −0 CorsixTH/Src/th_movie.h
View
@@ -484,19 +484,31 @@ bool THMovie::load(const char* szFilepath)
}
m_iVideoStream = av_find_best_stream(m_pFormatContext, AVMEDIA_TYPE_VIDEO, -1, -1, &m_pVideoCodec, 0);
m_pVideoCodecContext = m_pFormatContext->streams[m_iVideoStream]->codec;
m_pVideoCodecContext = getCodecContextForStream(m_pVideoCodec, m_pFormatContext->streams[m_iVideoStream]);
avcodec_open2(m_pVideoCodecContext, m_pVideoCodec, nullptr);
m_iAudioStream = av_find_best_stream(m_pFormatContext, AVMEDIA_TYPE_AUDIO, -1, -1, &m_pAudioCodec, 0);
if(m_iAudioStream >= 0)
{
m_pAudioCodecContext = m_pFormatContext->streams[m_iAudioStream]->codec;
m_pAudioCodecContext = getCodecContextForStream(m_pAudioCodec, m_pFormatContext->streams[m_iAudioStream]);
avcodec_open2(m_pAudioCodecContext, m_pAudioCodec, nullptr);
}
return true;
}
AVCodecContext* THMovie::getCodecContextForStream(AVCodec* codec, AVStream* stream) const
{
#if (defined(CORSIX_TH_USE_LIBAV) && LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 14, 0)) || \
(defined(CORSIX_TH_USE_FFMPEG) && LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 33, 100))
AVCodecContext* ctx = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(ctx, stream->codecpar);
return ctx;
#else
return stream->codec;
#endif
}
void THMovie::unload()
{
m_fAborting = true;
View
@@ -288,6 +288,9 @@ class THMovie
static const size_t ms_movieErrorBufferSize = 128; ///< Buffer to hold last error description
static const size_t ms_audioBufferSize = 1024; ///< Buffer for audio playback
//! Get the AVCodecContext associated with a given stream
AVCodecContext* getCodecContextForStream(AVCodec* codec, AVStream* stream) const;
//! Decode audio from the movie into a format suitable for playback
int decodeAudioFrame(bool fFirst);

0 comments on commit bba218d

Please sign in to comment.