From patchwork Mon Apr 19 02:06:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26972 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a25:49c5:0:0:0:0:0 with SMTP id w188csp684650yba; Sun, 18 Apr 2021 19:06:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw41easc8kKWSMuweoPR001Z9FVGykhjcnLCqm4iGz7eZMdfZuMKFE0dSl2eXKkMz0xiPXl X-Received: by 2002:aa7:d916:: with SMTP id a22mr22730982edr.287.1618798010388; Sun, 18 Apr 2021 19:06:50 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id o6si12937481edi.480.2021.04.18.19.06.50; Sun, 18 Apr 2021 19:06:50 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@outlook.com header.s=selector1 header.b=UQQz5GzO; arc=fail (body hash mismatch); spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 40F4C680B68; Mon, 19 Apr 2021 05:06:39 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR02-AM5-obe.outbound.protection.outlook.com (mail-oln040092067086.outbound.protection.outlook.com [40.92.67.86]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 136C7680B17 for ; Mon, 19 Apr 2021 05:06:33 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=V3YENB5Xvi6aF9dFit+9PORs/L7SxcUFEN+StLWLmcTcdUgjW1j/K74S5IC7+27HNZk78GpnbFqMYQ3yl7cL7s2V7j5Lgo04Qw+YfskrhEPwbmOhBbKnnqQMKNy+EGb6ftDwagdklJWso8MYGyDfMjCKpkB9RcTJKYvwq8e1AARMgc+Sy8VqxWCJIdaFOEcRf3e5EOAB2xAnFhTnagOY+mQREHLBfgZw7d6lKQxeJmjlRQHHS74elohdOTD9/pZcIBCFzG9IfmsI1RzgfBdbvLiYc0X6SX9tq/Tu2ieT4D9Tcti0hGDGlYXDsyCpJ0aZH+AB/Xa2rGhKQ6DLSQDlAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ZQHppN7WXQf5ZhbQOlw4R3nWYrPH4oyjfrdpFboaEqs=; b=eKKQCELkfi1xyHS7utlOf+4ibONTxVGNneioTgDrLjS0e2LcwL85Jq1WsmJTR8rlNDufsZ//H7hodJNwmA3PErVPRF+ZgycTr6y9UwU4o2A3NDjEU1K9bBXM1RXxx1TM95QiKsEdCiUIPLZiCfllfqB4nMad2QhO7A4PhGDPvVpzNhVSxija2Irclg3+40c4zdRzLCwLTpjjMqoW/geg8g0K2G3jeukmusy11sB/LTboAGayM15Uif3foLGHyzf7Z5d8WzkUPyd/x/PbJ8CacI3L14TKkiU8Of7dJebyhOvwML2HG6bv6V98j9Y5ADVkCT0qlpA+QalSVk07N60TOg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ZQHppN7WXQf5ZhbQOlw4R3nWYrPH4oyjfrdpFboaEqs=; b=UQQz5GzOnjoA+dAcCN9ZpYe8N1LFn1OUZEhB+919IuRPxAMIr+QdNXIgoW3K9ytH3dEyBMDpPKJwHZsO+Rl+Ce8DqOBSQIMb6rotaGfRthhW9yrPU6r16+kMWjHGu3pULqGDXu84/2c3hDDceycT0qGeFSdVy5IG+KWsKank5oONME43Kps9VyTG3qlNc0sYc3S8iEadt0gors84un/+76zY2ieD5CDzI7JFpKUaybnI3YSdQTtDREtzyKopJ1BCH4+r5CL43hQUzf8EiBnIlF8lBnrmJZ394TfQyDq0pJmTgpizHo4c4BDejcQrXi3AmYYjQn5V4ye+wO2hSBN1dg== Received: from HE1EUR02FT005.eop-EUR02.prod.protection.outlook.com (2a01:111:e400:7e1d::41) by HE1EUR02HT154.eop-EUR02.prod.protection.outlook.com (2a01:111:e400:7e1d::491) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4042.16; Mon, 19 Apr 2021 02:06:31 +0000 Received: from HE1PR0301MB2154.eurprd03.prod.outlook.com (2a01:111:e400:7e1d::41) by HE1EUR02FT005.mail.protection.outlook.com (2a01:111:e400:7e1d::99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4042.16 via Frontend Transport; Mon, 19 Apr 2021 02:06:31 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:8D4FE0CDBD866EEC8A92FF87EEA2C4A691DABA5EE4A572C02C3DE612D36BB169; UpperCasedChecksum:5C7C46F83BD099D9E9098B5C25F4FA2B5254F445C9DB91327B6D6643B25940E9; SizeAsReceived:7606; Count:48 Received: from HE1PR0301MB2154.eurprd03.prod.outlook.com ([fe80::45bb:c44f:2b75:23b7]) by HE1PR0301MB2154.eurprd03.prod.outlook.com ([fe80::45bb:c44f:2b75:23b7%5]) with mapi id 15.20.4042.024; Mon, 19 Apr 2021 02:06:31 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 19 Apr 2021 04:06:12 +0200 Message-ID: X-Mailer: git-send-email 2.27.0 In-Reply-To: References: X-TMN: [JjVQRrG4Lp434tQhFm9vFwmSnil4Vs/O] X-ClientProxiedBy: AM4PR0501CA0051.eurprd05.prod.outlook.com (2603:10a6:200:68::19) To HE1PR0301MB2154.eurprd03.prod.outlook.com (2603:10a6:3:2a::22) X-Microsoft-Original-Message-ID: <20210419020614.92961-4-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sblaptop.fritz.box (188.192.137.96) by AM4PR0501CA0051.eurprd05.prod.outlook.com (2603:10a6:200:68::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4042.19 via Frontend Transport; Mon, 19 Apr 2021 02:06:31 +0000 X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 48 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: 3bc4db29-e928-46e6-1eab-08d902d7c03a X-MS-Exchange-SLBlob-MailProps: q+fD6XS3/UL5of3Fna0zoENiR+FBTuR3+yzugfzD3hakRxjiPlpZ5eLeJdLnZmGow9qJOGudl9UikhsxEf67J0+YRk3kvJ6nz9+75SGu3TaH003hdmP6lldpNxM26pizkIuKkNBkDiBZfwJp0xGRdqGaaEX4OxFtV82yJYx2PeNsms7fiVe1Xda66dBkCKEEuRSKjqU53hBaBINp5rC313vqxkeY+D2pJygK0gfZ6s1nQqL1WxmYMX0TMIROYKg71UXucrCp0I7KKVE+GUr8VKWMxnPxzl83HgNXWs4fUSGELxywibEZAtMcWkFwQwidgwEpXZgi51U8Zm8p5B7S1VN0MUpzvhuP22XBIZKAhVSNtWgp8YVi4byEO+xSP8gdbAmgbZue8hEgxb5HFTqWbfVrEDfgoZS9xHckKrj+7UBq1sqXjbP7OyXN5Hdzsh6bT9Dgo1XuOtXoDjEBVpszM/EEg8swYgfLrlrkaJPWsBTNMiiEB6FW2MtfYUI4L59YUPWXpxHssc8z/74cqfp70yFPISe5Y4vxeJ0PPAriVJFfE/XH6jrdWf18uLmexDOq0IigozNjDD/4P13wWvdF/SrMeChp2mP979MfdkWTkXKZJumMLvaRNVb07DI7A0kyl6NXmqPPP6ipvSB3q0yxjhiby3zfAvQLtpMwQzU3dqPCSogOoDSwQKOTVBYJKsHw0iMgGPbOWHhpdHRZZM0WjQ== X-MS-TrafficTypeDiagnostic: HE1EUR02HT154: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YSTvbR5fPhf4FGCuux4Y394fRa9b3ZUdrc+XXXy2Tx90ilf17ldT6Pw5iONHSkRi5JGQNw55O49md2vYKlSTQEyStx5fKfM2WicC+9EKAqr2+Uk7Ggzsx0ZwvvSEAGD7dVx6JVNSxx5uODyXUEO7TUwYvndiVhCT8mD+jUCnzw1iuP7aN3xWXv9uucmMxnXH3pFwAKarfI5w+sLRz9VjJr55iB9t9lCMQjR1N7yyRjk5pgtWRwVk99RSN07GTCITj18tpJDuEc6nE9rs5zI6TBiQIYVHJAQ+XoRMuhFo5Y+V4CyRwzMEc3VyIBa9hyFPDrH/ZxzXPELzXhBW+A9IJzXGPCV8rhAbwKEJBlRC5Ndet6zcSUX0kzDhjoQX6OolwOF9+rG1mLOuUKQXQ5QATg== X-MS-Exchange-AntiSpam-MessageData: llFYp+PQ5WFMZl9QOAE7854U5+QVanzFlLQL8EV/ZqGhyouAiFUkdG+e8JV1tIILJTrfAzQWsbZvX4Kgz4nCikDVYE28WdE/4tPNjvVLU5+UullIA4j1vswbpUx0NpnrZzIIkSPsYTAM5QZqlOu2wQ== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3bc4db29-e928-46e6-1eab-08d902d7c03a X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Apr 2021 02:06:31.7663 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-AuthSource: HE1EUR02FT005.eop-EUR02.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: Internet X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1EUR02HT154 Subject: [FFmpeg-devel] [PATCH 5/7] avcodec/avcodec: Store whether AVCodec->close needs to be called X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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" X-TUID: 3NzboB0psNdj Content-Length: 4371 Right now all AVCodecContexts except those using frame-threaded decoding call the codec's init function and expect its close function to be called. In order to make sure that the close function is not called for frame-threaded decoding ff_frame_thread_free() resets AVCodecContext.codec (and because of this it has to free the private AVOptions of the main AVCodecContext itself). This is not obvious and potentially fragile. Instead add a field to AVCodecInternal that indicates whether close should be called for this AVCodecContext. It is always zero when using frame-threaded decoding, so that resetting the codec is no longer necessary and has been removed. Signed-off-by: Andreas Rheinhardt --- libavcodec/avcodec.c | 23 +++++++++++------------ libavcodec/internal.h | 6 ++++++ libavcodec/pthread_frame.c | 4 ---- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c index 6cff596f6a..c7a8001608 100644 --- a/libavcodec/avcodec.c +++ b/libavcodec/avcodec.c @@ -142,7 +142,6 @@ static int64_t get_bit_rate(AVCodecContext *ctx) int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options) { int ret = 0; - int codec_init_ok = 0; AVDictionary *tmp = NULL; AVCodecInternal *avci; @@ -336,14 +335,16 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code if (!HAVE_THREADS && !(codec->caps_internal & FF_CODEC_CAP_AUTO_THREADS)) avctx->thread_count = 1; - if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME) - || avci->frame_thread_encoder)) { - ret = avctx->codec->init(avctx); - if (ret < 0) { - codec_init_ok = -1; - goto free_and_end; + if (!(avctx->active_thread_type & FF_THREAD_FRAME) || + avci->frame_thread_encoder) { + if (avctx->codec->init) { + ret = avctx->codec->init(avctx); + if (ret < 0) { + avci->needs_close = avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP; + goto free_and_end; + } } - codec_init_ok = 1; + avci->needs_close = 1; } ret=0; @@ -394,9 +395,7 @@ end: return ret; free_and_end: - if (avctx->codec && avctx->codec->close && - (codec_init_ok > 0 || (codec_init_ok < 0 && - avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))) + if (avci->needs_close && avctx->codec->close) avctx->codec->close(avctx); if (CONFIG_FRAME_THREAD_ENCODER && avci->frame_thread_encoder) @@ -539,7 +538,7 @@ av_cold int avcodec_close(AVCodecContext *avctx) } if (HAVE_THREADS && avci->thread_ctx) ff_thread_free(avctx); - if (avctx->codec && avctx->codec->close) + if (avci->needs_close && avctx->codec->close) avctx->codec->close(avctx); avci->byte_buffer_size = 0; av_freep(&avci->byte_buffer); diff --git a/libavcodec/internal.h b/libavcodec/internal.h index b57b996816..85d52dda07 100644 --- a/libavcodec/internal.h +++ b/libavcodec/internal.h @@ -164,6 +164,12 @@ typedef struct AVCodecInternal { EncodeSimpleContext es; + /** + * If this is set, then AVCodec->close (if existing) needs to be called + * for the parent AVCodecContext. + */ + int needs_close; + /** * Number of audio samples to skip at the start of the next decoded frame */ diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c index ae5b000d97..250b0068ab 100644 --- a/libavcodec/pthread_frame.c +++ b/libavcodec/pthread_frame.c @@ -806,10 +806,6 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count) free_pthread(fctx, thread_ctx_offsets); av_freep(&avctx->internal->thread_ctx); - - if (avctx->priv_data && avctx->codec && avctx->codec->priv_class) - av_opt_free(avctx->priv_data); - avctx->codec = NULL; } static av_cold int init_thread(PerThreadContext *p, int *threads_to_free,