From patchwork Wed Feb 24 09:33:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 25938 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 CE62744B760 for ; Wed, 24 Feb 2021 11:33:16 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A3B9E68A1EB; Wed, 24 Feb 2021 11:33:16 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 369EC68091F for ; Wed, 24 Feb 2021 11:33:10 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id B53E9240684 for ; Wed, 24 Feb 2021 10:33:09 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id UACG-V5TjBZq for ; Wed, 24 Feb 2021 10:33:09 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 4D76E24048A for ; Wed, 24 Feb 2021 10:33:09 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 4DC7E3A01E6; Wed, 24 Feb 2021 10:33:09 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Wed, 24 Feb 2021 10:33:07 +0100 Message-Id: <20210224093307.28627-1-anton@khirnov.net> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] vp9: fix a race in exporting encoding parameters 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Modifying shared state after ff_thread_finish_setup() is not allowed, so set the encoding parameters directly on the output frame. Found-by: James Almer --- libavcodec/vp9.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/libavcodec/vp9.c b/libavcodec/vp9.c index 4659f94ee8..46062f8a8f 100644 --- a/libavcodec/vp9.c +++ b/libavcodec/vp9.c @@ -1499,7 +1499,8 @@ int loopfilter_proc(AVCodecContext *avctx) } #endif -static int vp9_export_enc_params(VP9Context *s, VP9Frame *frame) +static int vp9_export_enc_params(VP9Context *s, AVFrame *frame_dst, + const VP9Frame *frame) { AVVideoEncParams *par; unsigned int tile, nb_blocks = 0; @@ -1509,7 +1510,7 @@ static int vp9_export_enc_params(VP9Context *s, VP9Frame *frame) nb_blocks += s->td[tile].nb_block_structure; } - par = av_video_enc_params_create_side_data(frame->tf.f, + par = av_video_enc_params_create_side_data(frame_dst, AV_VIDEO_ENC_PARAMS_VP9, nb_blocks); if (!par) return AVERROR(ENOMEM); @@ -1759,12 +1760,6 @@ FF_ENABLE_DEPRECATION_WARNINGS s->td->error_info = 0; return AVERROR_INVALIDDATA; } - if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) { - ret = vp9_export_enc_params(s, &s->s.frames[CUR_FRAME]); - if (ret < 0) - return ret; - } - finish: // ref frame setup for (i = 0; i < 8; i++) { @@ -1778,6 +1773,13 @@ finish: if (!s->s.h.invisible) { if ((ret = av_frame_ref(frame, s->s.frames[CUR_FRAME].tf.f)) < 0) return ret; + + if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) { + ret = vp9_export_enc_params(s, frame, &s->s.frames[CUR_FRAME]); + if (ret < 0) + return ret; + } + *got_frame = 1; }