Commit fbd4154e authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

avcodec: use avcodec_free_context() where applicable

Fix leak on error, fix mismatched free function on success.
parent f32044fc
...@@ -248,10 +248,9 @@ static int OpenDecoder( vlc_object_t *p_this ) ...@@ -248,10 +248,9 @@ static int OpenDecoder( vlc_object_t *p_this )
{ {
decoder_t *p_dec = (decoder_t*) p_this; decoder_t *p_dec = (decoder_t*) p_this;
unsigned i_codec_id; unsigned i_codec_id;
int i_cat, i_result; int i_cat;
const char *psz_namecodec; const char *psz_namecodec;
AVCodecContext *p_context = NULL;
const AVCodec *p_codec = NULL; const AVCodec *p_codec = NULL;
/* *** determine codec type *** */ /* *** determine codec type *** */
...@@ -288,37 +287,44 @@ static int OpenDecoder( vlc_object_t *p_this ) ...@@ -288,37 +287,44 @@ static int OpenDecoder( vlc_object_t *p_this )
} }
/* *** get a p_context *** */ /* *** get a p_context *** */
p_context = avcodec_alloc_context3(p_codec); AVCodecContext *avctx = avcodec_alloc_context3(p_codec);
if( !p_context ) if( unlikely(avctx == NULL) )
return VLC_ENOMEM; return VLC_ENOMEM;
p_context->debug = var_InheritInteger( p_dec, "avcodec-debug" );
p_context->opaque = (void *)p_this;
p_dec->b_need_packetized = true; avctx->debug = var_InheritInteger( p_dec, "avcodec-debug" );
avctx->opaque = p_dec;
int ret;
switch( i_cat ) switch( i_cat )
{ {
case VIDEO_ES: case VIDEO_ES:
i_result = InitVideoDec( p_dec, p_context, p_codec ); ret = InitVideoDec( p_dec, avctx, p_codec );
break; break;
case AUDIO_ES: case AUDIO_ES:
i_result = InitAudioDec( p_dec, p_context, p_codec ); ret = InitAudioDec( p_dec, avctx, p_codec );
break; break;
case SPU_ES: case SPU_ES:
i_result = InitSubtitleDec( p_dec, p_context, p_codec ); ret = InitSubtitleDec( p_dec, avctx, p_codec );
break; break;
default: default:
return VLC_EGENERIC; ret = VLC_EGENERIC;
} }
if( i_result == VLC_SUCCESS ) if( ret != VLC_SUCCESS )
{ {
if( p_context->profile != FF_PROFILE_UNKNOWN) avcodec_free_context( &avctx );
p_dec->fmt_in.i_profile = p_context->profile; return ret;
if( p_context->level != FF_LEVEL_UNKNOWN)
p_dec->fmt_in.i_level = p_context->level;
} }
return i_result; if( avctx->profile != FF_PROFILE_UNKNOWN)
p_dec->fmt_in.i_profile = avctx->profile;
if( avctx->level != FF_LEVEL_UNKNOWN)
p_dec->fmt_in.i_level = avctx->level;
p_dec->b_need_packetized = true;
return VLC_SUCCESS;
} }
/***************************************************************************** /*****************************************************************************
...@@ -336,21 +342,18 @@ static void CloseDecoder( vlc_object_t *p_this ) ...@@ -336,21 +342,18 @@ static void CloseDecoder( vlc_object_t *p_this )
break; break;
} }
if( p_sys->p_context ) av_free( p_sys->p_context->extradata );
{ p_sys->p_context->extradata = NULL;
av_free( p_sys->p_context->extradata );
p_sys->p_context->extradata = NULL;
if( !p_sys->b_delayed_open ) if( !p_sys->b_delayed_open )
{ {
vlc_avcodec_lock(); vlc_avcodec_lock();
avcodec_close( p_sys->p_context ); avcodec_close( p_sys->p_context );
vlc_avcodec_unlock(); vlc_avcodec_unlock();
}
msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->p_codec->name );
av_free( p_sys->p_context );
} }
msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->p_codec->name );
avcodec_free_context( &p_sys->p_context );
free( p_sys ); free( p_sys );
} }
......
...@@ -36,6 +36,13 @@ ...@@ -36,6 +36,13 @@
( (LIBAVCODEC_VERSION_MICRO < 100 && LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( a, b, c ) ) || \ ( (LIBAVCODEC_VERSION_MICRO < 100 && LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( a, b, c ) ) || \
(LIBAVCODEC_VERSION_MICRO >= 100 && LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( a, d, e ) ) ) (LIBAVCODEC_VERSION_MICRO >= 100 && LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( a, d, e ) ) )
# if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 52, 0))
static inline void avcodec_free_context( AVCodecContext **ctx )
{
av_freep( ctx );
}
#endif
#endif /* HAVE_LIBAVCODEC_AVCODEC_H */ #endif /* HAVE_LIBAVCODEC_AVCODEC_H */
#ifdef HAVE_LIBAVUTIL_AVUTIL_H #ifdef HAVE_LIBAVUTIL_AVUTIL_H
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment