From patchwork Tue Mar 23 12:35:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26561 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id AB453449413 for ; Tue, 23 Mar 2021 15:05:24 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7CBB068AB11; Tue, 23 Mar 2021 15:05:24 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5F68268A77A for ; Tue, 23 Mar 2021 15:05:17 +0200 (EET) Received: by mail-lf1-f44.google.com with SMTP id m12so26497510lfq.10 for ; Tue, 23 Mar 2021 06:05:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=tF86qt0ZcqswGeBHt7JuCpKarqfgrTAovW9UKbDapGY=; b=Fdb3GtmRLmR1gU9nfTTHuJ1Cu2ymG3dRWrL2LRtfeBXvyFjRoPl5NgOCrD8GQqXEeU nOMvqDWYmrtjscgyxVJ5GRFLuBXfyDu6OytZUnKKHSMRmCIJka8kQ9JDaCj5P0m10y8r ue7WJvP2QGaI5G7mhfmWqzMOk6+a89BzhxPBp4VhUxHV4irpurxguLgQ8F829os74Le7 ehUC1oo10FLgU0krDJqv5ZdmjehbSOzEGdqUQhKQQwKjRweE/FxJHA0sEeUXKaC1fjkX /TSkLKMs8uz5uRUyOuqeq/x14Fm9rFDOIQ//ZeLWzJxWMhzlQ9cFzvUVFTW4+pobhInA nGzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=tF86qt0ZcqswGeBHt7JuCpKarqfgrTAovW9UKbDapGY=; b=tJpef8ujJZnj/Oq4YDF7IWTJ/+p3fF5CHf55dd5RmBBe/io+l2Qjj16QD6M7jG1Zvw n5omkXn7bDQux0neX93s0PyqICU1E5iiixJlk5S9rzeft/8INHHUNknTGQsKfPEMiEEg LQcuMW289L5BW/DAi+9jL+HQqxZ6IYXxDDWd5RyrapES5VzhzamKF98UnpIZyulzddYQ djQ4133cDukQc/we9jLT4foYd5zzowgDn4ajcoIeKVyB6wrSVMDhfnxU8zvstiFnEuwm iyTLeKEkjLwBwhpsZy3cqVf3LfTMslEBfsYgfjK1AgAdfsilgwoXAwykz28HCd+lNkKZ t2og== X-Gm-Message-State: AOAM532+1H14aL1tiQfVI781iL6OkzB+keRB0IQwFOdYZ/wjZOQuISML j0mjaCh3RxAV+0aAlLpfoj+ZhZjVC01jhQ== X-Google-Smtp-Source: ABdhPJxacNm0kNVzGttBgUElWpyol5/ERbtROchmEBrvmECTdVdGoTcF3hjwRZpM1TfOxRDewf0oKA== X-Received: by 2002:a17:906:3159:: with SMTP id e25mr4716102eje.303.1616503012073; Tue, 23 Mar 2021 05:36:52 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08960.dynamic.kabel-deutschland.de. [188.192.137.96]) by smtp.gmail.com with ESMTPSA id mp36sm10728532ejc.48.2021.03.23.05.36.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Mar 2021 05:36:49 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 23 Mar 2021 13:35:52 +0100 Message-Id: <20210323123554.1370260-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210323123554.1370260-1-andreas.rheinhardt@gmail.com> References: <20210323123554.1370260-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 2/4] avcodec/pthread_frame: Fix cleanup during init X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" In case an error happened when setting up the child threads, ff_frame_thread_init() would up until now call ff_frame_thread_free() to clean up all threads set up so far, including the current, not properly initialized one. But a half-allocated context needs special handling which ff_frame_thread_frame_free() doesn't provide. Notably, if allocating the AVCodecInternal, the codec's private data or setting the options fails, the codec's close function will be called (if there is one); it will also be called if the codec's init function fails, regardless of whether the FF_CODEC_CAP_INIT_CLEANUP is set. This is not supported by all codecs; in ticket #9099 it led to a crash. Signed-off-by: Andreas Rheinhardt --- libavcodec/pthread_frame.c | 137 ++++++++++++++++++------------------- 1 file changed, 67 insertions(+), 70 deletions(-) diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c index 311d6ed771..db2f0cb3d2 100644 --- a/libavcodec/pthread_frame.c +++ b/libavcodec/pthread_frame.c @@ -64,6 +64,12 @@ enum { STATE_SETUP_FINISHED, }; +enum { + UNINITIALIZED, ///< Thread has not been created, AVCodec->close mustn't be called + NEEDS_CLOSE, ///< AVCodec->close needs to be called + INITIALIZED, ///< Thread has been properly set up +}; + /** * Context used by codec threads and stored in their AVCodecInternal thread_ctx. */ @@ -698,27 +704,40 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count) for (i = 0; i < thread_count; i++) { PerThreadContext *p = &fctx->threads[i]; + AVCodecContext *ctx = p->avctx; - pthread_mutex_lock(&p->mutex); - p->die = 1; - pthread_cond_signal(&p->input_cond); - pthread_mutex_unlock(&p->mutex); - - if (p->thread_init) - pthread_join(p->thread, NULL); - p->thread_init=0; + if (ctx->internal) { + if (p->thread_init == INITIALIZED) { + pthread_mutex_lock(&p->mutex); + p->die = 1; + pthread_cond_signal(&p->input_cond); + pthread_mutex_unlock(&p->mutex); - if (codec->close && p->avctx) - codec->close(p->avctx); + pthread_join(p->thread, NULL); + } + if (codec->close && p->thread_init != UNINITIALIZED) + codec->close(ctx); #if FF_API_THREAD_SAFE_CALLBACKS - release_delayed_buffers(p); + release_delayed_buffers(p); + for (int j = 0; j < p->released_buffers_allocated; j++) + av_frame_free(&p->released_buffers[j]); + av_freep(&p->released_buffers); #endif - av_frame_free(&p->frame); - } + if (ctx->priv_data) { + if (codec->priv_class) + av_opt_free(ctx->priv_data); + av_freep(&ctx->priv_data); + } - for (i = 0; i < thread_count; i++) { - PerThreadContext *p = &fctx->threads[i]; + av_freep(&ctx->slice_offset); + + av_buffer_unref(&ctx->internal->pool); + av_freep(&ctx->internal); + av_buffer_unref(&ctx->hw_frames_ctx); + } + + av_frame_free(&p->frame); pthread_mutex_destroy(&p->mutex); pthread_mutex_destroy(&p->progress_mutex); @@ -727,26 +746,6 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count) pthread_cond_destroy(&p->output_cond); av_packet_free(&p->avpkt); -#if FF_API_THREAD_SAFE_CALLBACKS - for (int j = 0; j < p->released_buffers_allocated; j++) - av_frame_free(&p->released_buffers[j]); - av_freep(&p->released_buffers); -#endif - - if (p->avctx) { - if (codec->priv_class) - av_opt_free(p->avctx->priv_data); - av_freep(&p->avctx->priv_data); - - av_freep(&p->avctx->slice_offset); - } - - if (p->avctx) { - av_buffer_unref(&p->avctx->internal->pool); - av_freep(&p->avctx->internal); - av_buffer_unref(&p->avctx->hw_frames_ctx); - } - av_freep(&p->avctx); } @@ -763,47 +762,37 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count) avctx->codec = NULL; } -static av_cold int init_thread(PerThreadContext *p, +static av_cold int init_thread(PerThreadContext *p, int *threads_to_free, FrameThreadContext *fctx, AVCodecContext *avctx, AVCodecContext *src, const AVCodec *codec, int first) { - AVCodecContext *copy = av_malloc(sizeof(AVCodecContext)); + AVCodecContext *copy; int err; + atomic_init(&p->state, STATE_INPUT_READY); + + copy = av_memdup(src, sizeof(*src)); + if (!copy) + return AVERROR(ENOMEM); + copy->priv_data = NULL; + + /* From now on, this PerThreadContext will be cleaned up by + * ff_frame_thread_free in case of errors. */ + (*threads_to_free)++; + pthread_mutex_init(&p->mutex, NULL); pthread_mutex_init(&p->progress_mutex, NULL); pthread_cond_init(&p->input_cond, NULL); pthread_cond_init(&p->progress_cond, NULL); pthread_cond_init(&p->output_cond, NULL); - p->frame = av_frame_alloc(); - if (!p->frame) { - av_freep(©); - return AVERROR(ENOMEM); - } - p->avpkt = av_packet_alloc(); - if (!p->avpkt) { - av_freep(©); - return AVERROR(ENOMEM); - } - - p->parent = fctx; - p->avctx = copy; + p->parent = fctx; + p->avctx = copy; - if (!copy) { - return AVERROR(ENOMEM); - } - - *copy = *src; - - copy->internal = av_malloc(sizeof(AVCodecInternal)); - if (!copy->internal) { - copy->priv_data = NULL; - return AVERROR(ENOMEM); - } - *copy->internal = *src->internal; + copy->internal = av_memdup(src->internal, sizeof(*src->internal)); + if (!copy->internal) + return AVERROR(ENOMEM); copy->internal->thread_ctx = p; - copy->internal->last_pkt_props = p->avpkt; copy->delay = avctx->delay; @@ -821,14 +810,22 @@ static av_cold int init_thread(PerThreadContext *p, } } + if (!(p->frame = av_frame_alloc()) || + !(p->avpkt = av_packet_alloc())) + return AVERROR(ENOMEM); + copy->internal->last_pkt_props = p->avpkt; + if (!first) copy->internal->is_copy = 1; if (codec->init) err = codec->init(copy); if (err < 0) { + if (codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP) + p->thread_init = NEEDS_CLOSE; return err; } + p->thread_init = NEEDS_CLOSE; if (first) update_context_from_thread(avctx, copy, 1); @@ -836,9 +833,10 @@ static av_cold int init_thread(PerThreadContext *p, atomic_init(&p->debug_threads, (copy->debug & FF_DEBUG_THREADS) != 0); err = AVERROR(pthread_create(&p->thread, NULL, frame_worker_thread, p)); - p->thread_init= !err; - if(!p->thread_init) - return err; + if (err < 0) + return err; + p->thread_init = INITIALIZED; + return 0; } @@ -885,11 +883,11 @@ int ff_frame_thread_init(AVCodecContext *avctx) if (codec->type == AVMEDIA_TYPE_VIDEO) avctx->delay = src->thread_count - 1; - for (i = 0; i < thread_count; i++) { + for (i = 0; i < thread_count; ) { PerThreadContext *p = &fctx->threads[i]; int first = !i; - err = init_thread(p, fctx, avctx, src, codec, first); + err = init_thread(p, &i, fctx, avctx, src, codec, first); if (err < 0) goto error; } @@ -897,8 +895,7 @@ int ff_frame_thread_init(AVCodecContext *avctx) return 0; error: - ff_frame_thread_free(avctx, i+1); - + ff_frame_thread_free(avctx, i); return err; }