From 4f63a812de6ed8a76dde2a3739513933748ac2af Mon Sep 17 00:00:00 2001 From: Rudolf Polzer Date: Wed, 12 Nov 2014 12:16:07 +0100 Subject: ao_lavc, vo_lavc: Fix crashes in case of multiple init attempts. When initialization failed, vo_lavc may cause an irrecoverable state in the ffmpeg-related structs. Therefore, we reject additional initialization attempts at least until we know a better way to clean up the mess. ao_lavc currently cannot be initialized more than once, yet it's good to do consistent changes there as well. Also, clean up uninit-after-failure handling to be less spammy. --- video/out/vo_lavc.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'video/out/vo_lavc.c') diff --git a/video/out/vo_lavc.c b/video/out/vo_lavc.c index d5cf263ce2..9bb3ebd866 100644 --- a/video/out/vo_lavc.c +++ b/video/out/vo_lavc.c @@ -56,6 +56,8 @@ struct priv { int worst_time_base_is_stream; struct mp_image_params real_colorspace; + + bool shutdown; }; static int preinit(struct vo *vo) @@ -75,7 +77,7 @@ static void draw_image_unlocked(struct vo *vo, mp_image_t *mpi); static void uninit(struct vo *vo) { struct priv *vc = vo->priv; - if (!vc) + if (!vc || vc->shutdown) return; pthread_mutex_lock(&vo->encode_lavc_ctx->lock); @@ -86,6 +88,8 @@ static void uninit(struct vo *vo) mp_image_unrefp(&vc->lastimg); pthread_mutex_unlock(&vo->encode_lavc_ctx->lock); + + vc->shutdown = true; } static int reconfig(struct vo *vo, struct mp_image_params *params, int flags) @@ -97,7 +101,7 @@ static int reconfig(struct vo *vo, struct mp_image_params *params, int flags) uint32_t width = params->w; uint32_t height = params->h; - if (!vc) + if (!vc || vc->shutdown) return -1; pthread_mutex_lock(&vo->encode_lavc_ctx->lock); @@ -132,6 +136,14 @@ static int reconfig(struct vo *vo, struct mp_image_params *params, int flags) goto error; } + // When we get here, this must be the first call to reconfigure(). Thus, we + // can rely on no existing data in vc having been allocated yet. + // Reason: + // - Second calls after reconfigure() already failed once fail (due to the + // vc->shutdown check above). + // - Second calls after reconfigure() already succeeded once return early + // (due to the vc->stream check above). + vc->lastipts = AV_NOPTS_VALUE; vc->lastframeipts = AV_NOPTS_VALUE; vc->lastencodedipts = AV_NOPTS_VALUE; @@ -169,15 +181,13 @@ static int reconfig(struct vo *vo, struct mp_image_params *params, int flags) vc->buffer = talloc_size(vc, vc->buffer_size); - mp_image_unrefp(&vc->lastimg); - done: pthread_mutex_unlock(&vo->encode_lavc_ctx->lock); return 0; error: pthread_mutex_unlock(&vo->encode_lavc_ctx->lock); - uninit(vo); + vc->shutdown = true; return -1; } @@ -288,7 +298,7 @@ static void draw_image_unlocked(struct vo *vo, mp_image_t *mpi) double pts = mpi ? mpi->pts : MP_NOPTS_VALUE; - if (!vc) + if (!vc || vc->shutdown) goto done; if (!encode_lavc_start(ectx)) { MP_WARN(vo, "NOTE: skipped initial video frame (probably because audio is not there yet)\n"); -- cgit v1.2.3