From patchwork Fri Nov 19 22:59:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 31512 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:d206:0:0:0:0:0 with SMTP id q6csp3119396iob; Fri, 19 Nov 2021 14:59:20 -0800 (PST) X-Google-Smtp-Source: ABdhPJxuqU90DUaWHf2xZ2FG87GUnlev+c6dPI8uP0tfE4JpkbO2e1Zz1OP8M0mj0BU+MovMNiLA X-Received: by 2002:a17:907:8a13:: with SMTP id sc19mr12510171ejc.130.1637362760691; Fri, 19 Nov 2021 14:59:20 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id g24si2263958edv.447.2021.11.19.14.59.19; Fri, 19 Nov 2021 14:59:20 -0800 (PST) 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=WCecQP4r; 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 B0CCB68A973; Sat, 20 Nov 2021 00:59:15 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-oln040092075078.outbound.protection.outlook.com [40.92.75.78]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5DC7D68A630 for ; Sat, 20 Nov 2021 00:59:09 +0200 (EET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GRpyGnVlnmJ8Iydd+0LyuvyIhdk1OlLxxZl2j3q65NnTo9Ne9RFC7VwfTYm6KH+jouhhKvKAQksgnUaEJKwmztE/1axP2cjGBYrgJDqyOtAI2knJwuDki32A16nJmFZzotYtrpnsEsFP9Z/8nMJdWA/VvKLYFUH8qM7PGSsEfOo5NuSRTcbcd5WR8I4MGxE9vywauVt36wG98DF1OPboM5XXLTIepgLTsMy9S5Mq4ww7NuCz4IAPShQY5AbuCirpY+hBAeG1XVXKC0NwoxV2jENPqt+CnCbzBNbmzjO6E7xoeb07SUiHxipQdyx1a71nZ1ZA4WDeHdM7lXRV55TZuA== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Uh+7XnXkZt9yy6XGHFg1Wb5BdIas94efq8cVnw6xoSI=; b=KR2vhnpFgO1j+GggaAMcRAkLC6PWb1ThCc2NTFRfnXw88bGt2FdM0K1e9joQghCXHu57GYYGrwSjFfsu7DXgFmncf0AZFqVMYvttFLM4Qsf4uEwDwJY6TvWw6xKo6sZEZRDGQ1z8mIaOfjgwxQVQOpPY9i6ADQYaERGjlgs1pjjDpCtkv2ljm6gubBppOwVQNHosmHcIzlsx0pjiwlovVH+CUsB3JiaWvILdBTxSY5ZyIaaW5ovIXI9qO0cl1Iq4uohQUeD5ScQH58NKPjbdE3uDJ87TjR/UXYiPXO7BnBsIX26H60fK2n9L489oldDH/DM99tSKDndPMu7ujDTYFA== 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=Uh+7XnXkZt9yy6XGHFg1Wb5BdIas94efq8cVnw6xoSI=; b=WCecQP4rfpFAHAzrM5osvbCE0WEZA21jwii07G9E9pS6QCC2yAi42wI8axjXEQYMDRGDAATLP6ggBWOIdZgBhE4jKgf7ITvFXnw7TbNg+LMfqD917n7dcFnsiZjIldNLU12JroVvUtyMjHesPxMdebr7e1tsHCLYzr9PpRdhFG2My8n0QTJCtcJa/Cj2gM0+akOkM4nsGamzsgZNSm5QwPmzSKm9pPFhMWYywZgJBu+EhaT3gWR3JeF8LQbHLbQrrfCgFUv561D5sbf6V8K6UilheQt5a6LiqMNwGaaomNpZ5DKQ38xRBXn0nUA7ygzfA+yeFNJk7hyagqAEi8C83Q== Received: from AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) by AM6PR03MB4614.eurprd03.prod.outlook.com (2603:10a6:20b:9::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4690.26; Fri, 19 Nov 2021 22:59:07 +0000 Received: from AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::700f:d70b:3bb8:4d51]) by AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::700f:d70b:3bb8:4d51%9]) with mapi id 15.20.4713.022; Fri, 19 Nov 2021 22:59:07 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 19 Nov 2021 23:59:00 +0100 Message-ID: X-Mailer: git-send-email 2.30.2 X-TMN: [Kv1ufp1Don7CDZE7X5WhJC0TFwOBqW+1] X-ClientProxiedBy: AS8P189CA0002.EURP189.PROD.OUTLOOK.COM (2603:10a6:20b:31f::21) To AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) X-Microsoft-Original-Message-ID: <20211119225900.1195920-1-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sblaptop.fritz.box (188.192.137.12) by AS8P189CA0002.EURP189.PROD.OUTLOOK.COM (2603:10a6:20b:31f::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4713.21 via Frontend Transport; Fri, 19 Nov 2021 22:59:06 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: face54f5-dd7c-49b1-d588-08d9abb030bf X-MS-Exchange-SLBlob-MailProps: f36zkjAOy3V7gDvsffph3HbG0V6eNcrr+f8g73wPS2dFiIIt1SHNVtwNc6R/OenDRP/dgzWt4TKckrxC3gcehkQn42TMqi1GxkpdIGBHy15WCJOEirBfNC6hfBd03oBR6+iz0nyD1EUReBCv8z26E7NUsF6gcsz9Hs3DwDf/+URDotVvoNogzJAXYmBCqZOHxOjzOJyqzmk8gO/C78fUHoOMbNXLKh5d3dZNtmKRM6D1FkjT/jupaY216B1M4UlhpFrI5oX4cU0ScrZP8lB7EDaGA+hruYkKRHqZuW9BAluhtWJKTU+JZLIol667rz0R74Hm4yHwfbxh1y+t+up50Vi7GSG3LCJnMvxrMCtxMv8mmwMO8GlfVxN43TfejEXp578ses/8gYq0wDHbdN9ciS9gWR7c2n6DDgB4f4ri71G3EXLjslQ+0iU/AnUuRsehcqAizb4UB0g2H/h0sy7RQPJcHkybedYwL4nIJQLdLG94I0BRvRM+HTbZSvQC48Kt9qWl2NLJjR+6BUfYASJ58xWHELI8MNh2mFzn3KBG2+J4a7JTkN8t8AZsT3EO4geRcWSZSU1+BIRZ49tCNiSz/Q7NREAQFxUAT5iLl/Ar6P/4AnitS3HmgLigBpkHND9KLwYc6UaX2fKBNt8SXFe/LbOfUpmU+MprRO+WM/12WhYcZ2SdWETwGlhUULgSQc9+warbAo0zrTspG0/BW7F/QHuCzMssrSTcVdG4CngJNYs= X-MS-TrafficTypeDiagnostic: AM6PR03MB4614: X-Microsoft-Antispam: BCL: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/OauR79wE++tymfvv/fa6pj22Jitj2gxMSG1Y8gREIGkkTe2AZEXo4sAKCM9dJL1pYIyUAGnj0yBKWIhVD8RCruM89Jr/Ypdy4/AfXmXtKiQMO8yheY9V9+Vba/D7irKWSda7ekhYC3MHMh2uprQzgWnOv7ciuL3k8+TTuWVkC0YCu3T6bpDQUG4ELn7jM+snYB4wOEJKPqlLv0JDCfGNNgp5eeMZthyVQ+11cs4Vdg3GDn7AXM208WU3APdthy670DKqJLiPbAlbV+T5wFOhT+gmawh1o9RaKTLYSC2/MlKjD8ZswZ+XAPOQARsA9o92V1L9J8OCH40bQnQ== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: face54f5-dd7c-49b1-d588-08d9abb030bf X-MS-Exchange-CrossTenant-AuthSource: AM7PR03MB6660.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Nov 2021 22:59:07.4100 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR03MB4614 Subject: [FFmpeg-devel] [PATCH v2] fftools/ffmpeg: Avoid temporary frame 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: T9vkC/At6rEc send_frame_to_filters() sends a frame to all the filters that need said frame; for every filter except the last one this involves creating a reference to the frame, because av_buffersrc_add_frame_flags() by default takes ownership of the supplied references. Yet said function has a flag which changes its behaviour to create a reference itself. This commit uses this flag and stops creating the references itself; this allows to remove the spare AVFrame holding the temporary references; it also avoids unreferencing said frame. Signed-off-by: Andreas Rheinhardt --- fftools/ffmpeg.c | 22 +++++++--------------- fftools/ffmpeg.h | 1 - fftools/ffmpeg_opt.c | 4 ---- 3 files changed, 7 insertions(+), 20 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index fa66aa1cf9..c9a9cdfcd6 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -633,7 +633,6 @@ static void ffmpeg_cleanup(int ret) InputStream *ist = input_streams[i]; av_frame_free(&ist->decoded_frame); - av_frame_free(&ist->filter_frame); av_packet_free(&ist->pkt); av_dict_free(&ist->decoder_opts); avsubtitle_free(&ist->prev_sub.subtitle); @@ -2171,11 +2170,15 @@ static int ifilter_has_all_input_formats(FilterGraph *fg) return 1; } -static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame) +static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame, int keep_reference) { FilterGraph *fg = ifilter->graph; AVFrameSideData *sd; int need_reinit, ret; + int buffersrc_flags = AV_BUFFERSRC_FLAG_PUSH; + + if (keep_reference) + buffersrc_flags |= AV_BUFFERSRC_FLAG_KEEP_REF; /* determine if the parameters for this input changed */ need_reinit = ifilter->format != frame->format; @@ -2217,7 +2220,6 @@ static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame) AVFrame *tmp = av_frame_clone(frame); if (!tmp) return AVERROR(ENOMEM); - av_frame_unref(frame); if (!av_fifo_space(ifilter->frame_queue)) { ret = av_fifo_realloc2(ifilter->frame_queue, 2 * av_fifo_size(ifilter->frame_queue)); @@ -2243,7 +2245,7 @@ static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame) } } - ret = av_buffersrc_add_frame_flags(ifilter->filter, frame, AV_BUFFERSRC_FLAG_PUSH); + ret = av_buffersrc_add_frame_flags(ifilter->filter, frame, buffersrc_flags); if (ret < 0) { if (ret != AVERROR_EOF) av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret)); @@ -2306,18 +2308,10 @@ static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacke static int send_frame_to_filters(InputStream *ist, AVFrame *decoded_frame) { int i, ret; - AVFrame *f; av_assert1(ist->nb_filters > 0); /* ensure ret is initialized */ for (i = 0; i < ist->nb_filters; i++) { - if (i < ist->nb_filters - 1) { - f = ist->filter_frame; - ret = av_frame_ref(f, decoded_frame); - if (ret < 0) - break; - } else - f = decoded_frame; - ret = ifilter_send_frame(ist->filters[i], f); + ret = ifilter_send_frame(ist->filters[i], decoded_frame, i < ist->nb_filters - 1); if (ret == AVERROR_EOF) ret = 0; /* ignore */ if (ret < 0) { @@ -2385,7 +2379,6 @@ static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output, ist->nb_samples = decoded_frame->nb_samples; err = send_frame_to_filters(ist, decoded_frame); - av_frame_unref(ist->filter_frame); av_frame_unref(decoded_frame); return err < 0 ? err : ret; } @@ -2511,7 +2504,6 @@ static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output, int64_ err = send_frame_to_filters(ist, decoded_frame); fail: - av_frame_unref(ist->filter_frame); av_frame_unref(decoded_frame); return err < 0 ? err : ret; } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index e86ab61047..1728010f56 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -311,7 +311,6 @@ typedef struct InputStream { AVCodecContext *dec_ctx; const AVCodec *dec; AVFrame *decoded_frame; - AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */ AVPacket *pkt; int64_t prev_pkt_pts; diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 98bd3b47b6..6732a29625 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -893,10 +893,6 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic) if (!ist->decoded_frame) exit_program(1); - ist->filter_frame = av_frame_alloc(); - if (!ist->filter_frame) - exit_program(1); - ist->pkt = av_packet_alloc(); if (!ist->pkt) exit_program(1);