Skip to content

Commit

Permalink
ffmpeg: deprecated AVStream::codec and avcodec_close
Browse files Browse the repository at this point in the history
In the old method, ffmpeg automatically creates an AVCodecContext
for every AVStream. The context can be opened and closed again any
number of times. This is deprecated and the application code is now
responsible for creating and freeing an  AVCodecContext when needed.
The AVCodecContext that the application creates may not be opened
and closed more than once. The deprecated function avcodec_close
must not be called on an AVCodecContext that you create, instead
you have to free the AVCodecContext (using avcodec_free_context)
and allocate a new one if you want to read the stream again.

In many cases, reference fields in AVStream::codec are now available
in AVStream::codecpar. Where possible the code now uses those fields,
to avoid having to create an AVCodecContext.

"codec" no longer shows in the online documentation for AVStream.
It is only defined in ffmpeg if you define FF_API_LAVF_AVCTX when
compiling ffmpeg. See external/FFmpeg/libavformat/version.h, which
has a list of defines that will be set in future to disable this
and others. For more information google FF_API_LAVF_AVCTX.
Instead of AVStream::codec, the AVStream::codecpar structure
contains the information you need to allocate your own AVStream.

Old code for AVStream::codec
 AVCodecContext *avctx = stream->codec;

new code using AVStream::codecpar
 AVCodec *pCodec = avcodec_find_decoder(stream->codecpar->codec_id);
 AVCodecContext *avctx = avcodec_alloc_context3(pCodec);
 avcodec_parameters_to_context(avctx, stream->codecpar);
 av_codec_set_pkt_timebase(avctx, stream->time_base);

The avctx must saved and the same value used for subsequent calls
with that stream. Existing code which uses stream->codec must
instead keep track of the axctx value to use each time a packet
is received.

Old Code for avcodec_close:
 avcodec_close(st->codec);

New code using avcodec_free_context:
 avcodec_free_context(&avctx);

Note the old code must still be used for any AVCodecContext
obtained from stream->codec.
  • Loading branch information
bennettpeter committed Dec 13, 2017
1 parent 60d583a commit f700555
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 140 deletions.
6 changes: 3 additions & 3 deletions mythtv/libs/libmythtv/DVD/avformatdecoderdvd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ int AvFormatDecoderDVD::ReadPacket(AVFormatContext *ctx, AVPacket* pkt, bool& st

AVStream *curstream = ic->streams[pkt->stream_index];

if ((curstream->codec->codec_type == AVMEDIA_TYPE_VIDEO) ||
(curstream->codec->codec_id == AV_CODEC_ID_DVD_NAV))
if ((curstream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ||
(curstream->codecpar->codec_id == AV_CODEC_ID_DVD_NAV))
{
// Allow video or NAV packets through
gotPacket = true;
Expand Down Expand Up @@ -371,7 +371,7 @@ bool AvFormatDecoderDVD::ProcessDataPacket(AVStream *curstream, AVPacket *pkt,
{
bool ret = true;

if (curstream->codec->codec_id == AV_CODEC_ID_DVD_NAV)
if (curstream->codecpar->codec_id == AV_CODEC_ID_DVD_NAV)
{
MythDVDContext* context = ringBuffer->DVD()->GetDVDContext();

Expand Down
Loading

0 comments on commit f700555

Please sign in to comment.