From patchwork Sat Apr 24 18:00:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 27326 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:5014:0:0:0:0:0 with SMTP id e20csp2545072iob; Sat, 24 Apr 2021 11:01:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzE/lI/IyzSroDVnt1JY97GeYZ5JnpktC4ysE/sCB0egNLwmiIHLCYFK+EELmO5a8i3fSIi X-Received: by 2002:a17:907:6ec:: with SMTP id yh12mr9998156ejb.293.1619287278213; Sat, 24 Apr 2021 11:01:18 -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 c23si8396373ejk.382.2021.04.24.11.01.17; Sat, 24 Apr 2021 11:01:18 -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=NIpMn7P+; 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 63532680BE7; Sat, 24 Apr 2021 21:01:13 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR01-DB5-obe.outbound.protection.outlook.com (mail-oln040092064066.outbound.protection.outlook.com [40.92.64.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 31C186806E3 for ; Sat, 24 Apr 2021 21:01:05 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Gji1eb2FDInpzZ2GzKz74tsJEa7H5rFlVK6x6KO4VKxCRXoZXuwvi3EmNS82BdH+df/ENGe45Go7AySBcjOO/xuqctmaG/J8kNsrBdfGrDQf2bJp/9n3/WhUuefrW71fKKgrkzgePQJL5EYe16lyclxY2Zb8FV9ds/AnhYOOEVe2E5AYrte0xjM1nmWdyk/s5weuDhT7zckdMW8u1qx4hikpltrgnnM9b7deO+l9oOc+P6QyC6dqFZ48gOA6oyy5z4CcnAp3aBl4xuxlqKrUplnwXP5FXqiVw6XarrFFcAwQIGKxzOuyrjJ/GOrXDvbQXnysc7LNFRhf0km0wDDpeA== 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=oi01anEws5yqPfqYpAgKUjoRXU/kPf6mnIBzaUBxr9M=; b=BtjtaIncQ7tCCmDSzjf0UFhKtb3Tm4MwVtoGU9SqrdAPMrJ28H3O4OkQJi5uMrR7l+yTscfMRgdlwg0EZlXdIVydBGiTn6ZExoy7UhpXXCateAhq1OomKJJw/Kmvm2PIeIELuJ28/k8XCIT3DapC/5EQna56LCUkpC+JVvE16RD3blDNjLzUXB88ACwQ/m/5j4W9jpob1E8539YJjyOUqAkuDKxa2U6Idvc1NKTgU8+ri/yWSDWxPvCKCThA0QK2rYMU6Ts/TSDIoRsaK5eeu8NDVAP/HujOZpXafPEWXOblo+B1xPtpOfsel1tcsTumYEuAYPBKrii6X31MQ1+HIw== 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=oi01anEws5yqPfqYpAgKUjoRXU/kPf6mnIBzaUBxr9M=; b=NIpMn7P+/AEGtNE0exN57opz53fM6hrbheEElI5l0vEJcW3QZ/dD+tOiIftdBRGImfomWUIArulRadRHNjBto95/LsAi2pWOqRHb1Vv/OxCHl4UHomH6PIu/id8hHtSEc592CVKFtaVqWJb0zB9b/fuvUBI6HF1wWKnZVca/sY7K3OajAv4ir940t5RjMvNTB0hC1nmOUi6vCoQOA4uMskxJ8MHulpUWdoaK2ulgJnJK8/3luzOx+sRuewOrndnlzmh6bBloCxjG9/DwU01kLVigC4rdqP1R59zfW1aOFFitm4+goCaBIExeNzA5zoHZ5BYeSuB3NKMn7QtByA/jEg== Received: from DB5EUR01FT050.eop-EUR01.prod.protection.outlook.com (2a01:111:e400:7e1a::43) by DB5EUR01HT078.eop-EUR01.prod.protection.outlook.com (2a01:111:e400:7e1a::307) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4065.21; Sat, 24 Apr 2021 18:01:03 +0000 Received: from HE1PR0301MB2154.eurprd03.prod.outlook.com (2a01:111:e400:7e1a::41) by DB5EUR01FT050.mail.protection.outlook.com (2a01:111:e400:7e1a::338) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4065.21 via Frontend Transport; Sat, 24 Apr 2021 18:01:03 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:24DA3E8284B401E4CDDCCB445C43E425C6CB89DB2F7C9DE73D5E6BECA05262F8; UpperCasedChecksum:188DA41D395EBE4A0159831CAA4D36A027631906AFE42FABBE319ED82D34C73A; SizeAsReceived:7603; 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.4065.025; Sat, 24 Apr 2021 18:01:03 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 24 Apr 2021 20:00:48 +0200 Message-ID: X-Mailer: git-send-email 2.27.0 In-Reply-To: References: X-TMN: [cl8qvby+8lwSXrB1Q4pmMR2jd8Ea7Nta] X-ClientProxiedBy: ZR0P278CA0027.CHEP278.PROD.OUTLOOK.COM (2603:10a6:910:1c::14) To HE1PR0301MB2154.eurprd03.prod.outlook.com (2603:10a6:3:2a::22) X-Microsoft-Original-Message-ID: <20210424180048.259739-1-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sblaptop.fritz.box (188.193.248.86) by ZR0P278CA0027.CHEP278.PROD.OUTLOOK.COM (2603:10a6:910:1c::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4065.22 via Frontend Transport; Sat, 24 Apr 2021 18:01:02 +0000 X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 48 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: b5965d4c-2c29-456c-e219-08d9074aec97 X-MS-Exchange-SLBlob-MailProps: q+fD6XS3/UL2UDcFXhKBVGYTQ5VZMO6xC17Qg1GgViBF+T2vwK6CTeqESaHZ/yYf/bY/tPg9QEk+A86qt1BchagGY4bkL+KKADsfpL62FFAxniQ12rp3Udb2N/fNUBItCqK6cdk8mKiAvwhUop7mer/s3KLbb1dPpVTIi0VN+pSypsbK9E5PJG7ERYDnAmnIZmgbcxaRrO+RD/32JqGPqCuHH3GMXw7OpUAEoOLbnBgtqZy+Zk4/fiYM5mdzgii0ei+BnmdsbFjsh70bkJcD5YeIF72MjBHqlbkg5ggBZteA9XsXwVxIvaANDKIQjioULveh2Nnb05e3gNA7IJ+ExVkMSLeBYtm6wnnxohheVcodl/3a3w8PWu7eW9WTGUCVyR7Zhf7vrk3rDd70MYAtBUozzw6Jnt29Ijz6LeRey2YJIg22XUKn6TIix9+JpjitW3e52DBrbYw7LiAKi3gYSgJ/+Q2JeG0hw+yQmaaw6KrQxsIdNOev65DbeMcKQsRgewSKDW9uuUoRKGUheepHkr7oZmSM0AH3t3YLJ2z2oRnixLZjRFzQfTbtiyvEMgqUaEej7/GGhtR5CjxY8tTO4K1JJ+MUNqAPkVZQdSnLKj4hcY+n5ucvCx+8ve5AhYCFQAnLUa4mGBDRJJRaazEZZ5FUk4t/41LKOMbcdNXN0XoNjK4z2ptUj7s2c8r3s03CaEOLCfJJw392uVlztFKkcQ== X-MS-TrafficTypeDiagnostic: DB5EUR01HT078: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: u3I0//63UsvAGNVnATRHAAnE+g3D2xt2yR+0rgL/9SDNap7T+2Td5VeSzyMfyVvNRhJ6gKEu6cVpYG5aKOPS8Gjy2AvKXnqxev5FlFwAA0sJ+C6mirJMfshrrOftrhbdOmzS/1HknPCRNu0WGlVCQMvN6nHwdosDsA9jcIY6D562VjKwXHG/8G6f/FM8+oS82Av3bADxVA82SdCoPypCb1NlauHrO8Z8ayRoKz9kRbaJ3OFFGohfvulAGhkGeCzfsxjxg8UrpRPQ+A3IWdTn5tws73kKmSPgM/GI/1xbD3CqVXQa/+MnVb3xwPGK7vCs/nFLlNlh02SmQabFB904kIw9vwCBbljUOnvvR/OC5NkOE4Gn9TUkiWTM1PN4nJWGXmRZsfzphJn8VqvjDPbT+w== X-MS-Exchange-AntiSpam-MessageData: nC5q7vutzhHRpFCSbxtkRLxjSWccmWgpGoq7rvGnNVLownXw29ZjoS+78SBU1ctuSDx6N6CiTIA5e+S+6U96JmKRruuYU983euPCBf9ifph5dw5Z9/5ultNQdERs7iT8FZ2KAEsoKVSSAhRP8yu6sA== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: b5965d4c-2c29-456c-e219-08d9074aec97 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Apr 2021 18:01:03.2215 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-AuthSource: DB5EUR01FT050.eop-EUR01.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: DB5EUR01HT078 Subject: [FFmpeg-devel] [PATCH v2 6/7] avcodec/avcodec: Use avcodec_close() on avcodec_open2() failure 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: XwpcCNUr6I4k Compared to the earlier behaviour the following changes: a) AVCodecInternal.byte_buffer is freed. b) The last_pkt_props FIFO is emptied before freeing it. c) If set AVCodecContext.hwaccel is uninitialized and its private data is freed; hw_frames_ctx and hw_device_ctx are also unreferenced. d) coded_side_data is freed. e) active_thread_type is reset. a), b), d) should be no-ops as the buffer/fifo should be empty and no coded_side_data should exist at any point of avcodec_open2(). e) is obviously not bad. c) is in accordance with the documentation of hw_(frames|device)_ctx which states that libacodec takes over ownership of these references. At least in the case of VC-1 it is possible for the hw acceleration to be set during init and in this case freeing it actually fixes a memleak. avcodec_close() needed only minor adjustments to make it work with a potentially not fully initialized codec. Signed-off-by: Andreas Rheinhardt --- Now including minor necessary adjustments to make avcodec_close() work with a not properly-allocated AVCodecContext. libavcodec/avcodec.c | 59 +++++++------------------------------------- 1 file changed, 9 insertions(+), 50 deletions(-) diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c index c7a8001608..7f9c9c1c87 100644 --- a/libavcodec/avcodec.c +++ b/libavcodec/avcodec.c @@ -395,51 +395,8 @@ end: return ret; free_and_end: - if (avci->needs_close && avctx->codec->close) - avctx->codec->close(avctx); - - if (CONFIG_FRAME_THREAD_ENCODER && avci->frame_thread_encoder) - ff_frame_thread_encoder_free(avctx); - if (HAVE_THREADS && avci->thread_ctx) - ff_thread_free(avctx); - - if (codec->priv_class && avctx->priv_data) - av_opt_free(avctx->priv_data); - av_opt_free(avctx); - - if (av_codec_is_encoder(avctx->codec)) { -#if FF_API_CODED_FRAME -FF_DISABLE_DEPRECATION_WARNINGS - av_frame_free(&avctx->coded_frame); -FF_ENABLE_DEPRECATION_WARNINGS -#endif - av_freep(&avctx->extradata); - avctx->extradata_size = 0; - } - + avcodec_close(avctx); av_dict_free(&tmp); - av_freep(&avctx->priv_data); - if (av_codec_is_decoder(avctx->codec)) - av_freep(&avctx->subtitle_header); - -#if FF_API_OLD_ENCDEC - av_frame_free(&avci->to_free); - av_frame_free(&avci->compat_decode_frame); - av_packet_free(&avci->compat_encode_packet); -#endif - av_frame_free(&avci->buffer_frame); - av_packet_free(&avci->buffer_pkt); - av_packet_free(&avci->last_pkt_props); - av_fifo_freep(&avci->pkt_props); - - av_packet_free(&avci->ds.in_pkt); - av_frame_free(&avci->es.in_frame); - av_bsf_free(&avci->bsf); - - av_buffer_unref(&avci->pool); - av_freep(&avci); - avctx->internal = NULL; - avctx->codec = NULL; goto end; } @@ -549,14 +506,15 @@ av_cold int avcodec_close(AVCodecContext *avctx) #endif av_frame_free(&avci->buffer_frame); av_packet_free(&avci->buffer_pkt); - av_packet_unref(avci->last_pkt_props); - while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) { - av_fifo_generic_read(avci->pkt_props, avci->last_pkt_props, - sizeof(*avci->last_pkt_props), NULL); - av_packet_unref(avci->last_pkt_props); + if (avci->pkt_props) { + while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) { + av_packet_unref(avci->last_pkt_props); + av_fifo_generic_read(avci->pkt_props, avci->last_pkt_props, + sizeof(*avci->last_pkt_props), NULL); + } + av_fifo_freep(&avci->pkt_props); } av_packet_free(&avci->last_pkt_props); - av_fifo_freep(&avci->pkt_props); av_packet_free(&avci->ds.in_pkt); av_frame_free(&avci->es.in_frame); @@ -586,6 +544,7 @@ av_cold int avcodec_close(AVCodecContext *avctx) av_freep(&avctx->priv_data); if (av_codec_is_encoder(avctx->codec)) { av_freep(&avctx->extradata); + avctx->extradata_size = 0; #if FF_API_CODED_FRAME FF_DISABLE_DEPRECATION_WARNINGS av_frame_free(&avctx->coded_frame);