av: Fix some memory leaks and misuse of libav API

This commit is contained in:
Sebastian Dröge 2012-12-17 16:35:36 +01:00
parent d69dda0391
commit 022ab84288
7 changed files with 18 additions and 45 deletions

View file

@ -158,16 +158,12 @@ gst_ffmpegauddec_finalize (GObject * object)
static void static void
gst_ffmpegauddec_close (GstFFMpegAudDec * ffmpegdec) gst_ffmpegauddec_close (GstFFMpegAudDec * ffmpegdec)
{ {
if (!ffmpegdec->opened)
return;
GST_LOG_OBJECT (ffmpegdec, "closing libav codec"); GST_LOG_OBJECT (ffmpegdec, "closing libav codec");
gst_caps_replace (&ffmpegdec->last_caps, NULL); gst_caps_replace (&ffmpegdec->last_caps, NULL);
gst_buffer_replace (&ffmpegdec->outbuf, NULL); gst_buffer_replace (&ffmpegdec->outbuf, NULL);
if (ffmpegdec->opened) gst_ffmpeg_avcodec_close (ffmpegdec->context);
gst_ffmpeg_avcodec_close (ffmpegdec->context);
ffmpegdec->opened = FALSE; ffmpegdec->opened = FALSE;
if (ffmpegdec->context->extradata) { if (ffmpegdec->context->extradata) {
@ -297,9 +293,6 @@ gst_ffmpegauddec_set_format (GstAudioDecoder * decoder, GstCaps * caps)
gst_ffmpegauddec_drain (ffmpegdec); gst_ffmpegauddec_drain (ffmpegdec);
GST_OBJECT_LOCK (ffmpegdec); GST_OBJECT_LOCK (ffmpegdec);
gst_ffmpegauddec_close (ffmpegdec); gst_ffmpegauddec_close (ffmpegdec);
/* and reset the defaults that were set when a context is created */
avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin);
} }
/* get size and so */ /* get size and so */

View file

@ -192,10 +192,8 @@ gst_ffmpegaudenc_stop (GstAudioEncoder * encoder)
GstFFMpegAudEnc *ffmpegaudenc = (GstFFMpegAudEnc *) encoder; GstFFMpegAudEnc *ffmpegaudenc = (GstFFMpegAudEnc *) encoder;
/* close old session */ /* close old session */
if (ffmpegaudenc->opened) { gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
gst_ffmpeg_avcodec_close (ffmpegaudenc->context); ffmpegaudenc->opened = FALSE;
ffmpegaudenc->opened = FALSE;
}
return TRUE; return TRUE;
} }
@ -243,9 +241,6 @@ gst_ffmpegaudenc_set_format (GstAudioEncoder * encoder, GstAudioInfo * info)
ffmpegaudenc->opened = FALSE; ffmpegaudenc->opened = FALSE;
} }
/* set defaults */
avcodec_get_context_defaults3 (ffmpegaudenc->context, oclass->in_plugin);
/* if we set it in _getcaps we should set it also in _link */ /* if we set it in _getcaps we should set it also in _link */
ffmpegaudenc->context->strict_std_compliance = -1; ffmpegaudenc->context->strict_std_compliance = -1;

View file

@ -738,10 +738,8 @@ gst_ffmpeg_cfg_install_property (GstFFMpegVidEncClass * klass, guint base)
prop_id = base; prop_id = base;
g_return_if_fail (base > 0); g_return_if_fail (base > 0);
ctx = avcodec_alloc_context3 (NULL); ctx = avcodec_alloc_context3 (klass->in_plugin);
if (ctx) if (!ctx)
avcodec_get_context_defaults3 (ctx, NULL);
else
g_warning ("could not get context"); g_warning ("could not get context");
for (list = property_list; list; list = list->next) { for (list = property_list; list; list = list->next) {

View file

@ -336,7 +336,11 @@ gst_ffmpegdemux_close (GstFFMpegDemux * demux)
gst_ffmpegdata_close (demux->context->pb); gst_ffmpegdata_close (demux->context->pb);
else else
gst_ffmpeg_pipe_close (demux->context->pb); gst_ffmpeg_pipe_close (demux->context->pb);
demux->context->pb = NULL;
avformat_close_input (&demux->context); avformat_close_input (&demux->context);
if (demux->context)
avformat_free_context (demux->context);
demux->context = NULL;
GST_OBJECT_LOCK (demux); GST_OBJECT_LOCK (demux);
demux->opened = FALSE; demux->opened = FALSE;

View file

@ -324,7 +324,7 @@ gst_ffmpegmux_init (GstFFMpegMux * ffmpegmux, GstFFMpegMuxClass * g_class)
gst_collect_pads_set_function (ffmpegmux->collect, gst_collect_pads_set_function (ffmpegmux->collect,
(GstCollectPadsFunction) gst_ffmpegmux_collected, ffmpegmux); (GstCollectPadsFunction) gst_ffmpegmux_collected, ffmpegmux);
ffmpegmux->context = g_new0 (AVFormatContext, 1); ffmpegmux->context = avformat_alloc_context ();
ffmpegmux->context->oformat = oclass->in_plugin; ffmpegmux->context->oformat = oclass->in_plugin;
ffmpegmux->context->nb_streams = 0; ffmpegmux->context->nb_streams = 0;
ffmpegmux->opened = FALSE; ffmpegmux->opened = FALSE;
@ -382,7 +382,9 @@ gst_ffmpegmux_finalize (GObject * object)
{ {
GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) object; GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) object;
g_free (ffmpegmux->context); avformat_free_context (ffmpegmux->context);
ffmpegmux->context = NULL;
gst_object_unref (ffmpegmux->collect); gst_object_unref (ffmpegmux->collect);
if (G_OBJECT_CLASS (parent_class)->finalize) if (G_OBJECT_CLASS (parent_class)->finalize)

View file

@ -268,10 +268,7 @@ gst_ffmpegviddec_finalize (GObject * object)
ffmpegdec->context = NULL; ffmpegdec->context = NULL;
} }
if (ffmpegdec->picture != NULL) { avcodec_free_frame (&ffmpegdec->picture);
av_free (ffmpegdec->picture);
ffmpegdec->picture = NULL;
}
G_OBJECT_CLASS (parent_class)->finalize (object); G_OBJECT_CLASS (parent_class)->finalize (object);
} }
@ -281,9 +278,6 @@ gst_ffmpegviddec_finalize (GObject * object)
static void static void
gst_ffmpegviddec_close (GstFFMpegVidDec * ffmpegdec) gst_ffmpegviddec_close (GstFFMpegVidDec * ffmpegdec)
{ {
if (!ffmpegdec->opened)
return;
GST_LOG_OBJECT (ffmpegdec, "closing ffmpeg codec"); GST_LOG_OBJECT (ffmpegdec, "closing ffmpeg codec");
gst_caps_replace (&ffmpegdec->last_caps, NULL); gst_caps_replace (&ffmpegdec->last_caps, NULL);
@ -393,9 +387,6 @@ gst_ffmpegviddec_set_format (GstVideoDecoder * decoder,
gst_ffmpegviddec_drain (ffmpegdec); gst_ffmpegviddec_drain (ffmpegdec);
GST_OBJECT_LOCK (ffmpegdec); GST_OBJECT_LOCK (ffmpegdec);
gst_ffmpegviddec_close (ffmpegdec); gst_ffmpegviddec_close (ffmpegdec);
/* and reset the defaults that were set when a context is created */
avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin);
} }
/* set buffer functions */ /* set buffer functions */

View file

@ -253,15 +253,9 @@ gst_ffmpegvidenc_finalize (GObject * object)
gst_ffmpeg_cfg_finalize (ffmpegenc); gst_ffmpeg_cfg_finalize (ffmpegenc);
/* close old session */
if (ffmpegenc->opened) {
gst_ffmpeg_avcodec_close (ffmpegenc->context);
ffmpegenc->opened = FALSE;
}
/* clean up remaining allocated data */ /* clean up remaining allocated data */
av_free (ffmpegenc->context); av_free (ffmpegenc->context);
av_free (ffmpegenc->picture); avcodec_free_frame (&ffmpegenc->picture);
g_free (ffmpegenc->filename); g_free (ffmpegenc->filename);
@ -300,9 +294,6 @@ gst_ffmpegvidenc_set_format (GstVideoEncoder * encoder,
ffmpegenc->opened = FALSE; ffmpegenc->opened = FALSE;
} }
/* set defaults */
avcodec_get_context_defaults3 (ffmpegenc->context, oclass->in_plugin);
/* if we set it in _getcaps we should set it also in _link */ /* if we set it in _getcaps we should set it also in _link */
ffmpegenc->context->strict_std_compliance = -1; ffmpegenc->context->strict_std_compliance = -1;
@ -808,10 +799,9 @@ gst_ffmpegvidenc_stop (GstVideoEncoder * encoder)
GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder; GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder;
gst_ffmpegvidenc_flush_buffers (ffmpegenc, FALSE); gst_ffmpegvidenc_flush_buffers (ffmpegenc, FALSE);
if (ffmpegenc->opened) { gst_ffmpeg_avcodec_close (ffmpegenc->context);
gst_ffmpeg_avcodec_close (ffmpegenc->context); ffmpegenc->opened = FALSE;
ffmpegenc->opened = FALSE;
}
if (ffmpegenc->file) { if (ffmpegenc->file) {
fclose (ffmpegenc->file); fclose (ffmpegenc->file);
ffmpegenc->file = NULL; ffmpegenc->file = NULL;