From patchwork Mon Apr 4 11:29:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35159 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1911554pzb; Mon, 4 Apr 2022 04:32:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx+uMzDMMAXGy6MpMyH7FpPaJ5MfCx+P5lnwhjNDx9HocJMGAGtJJR6Xzl7hS+wmeHHiz7B X-Received: by 2002:a17:907:6e93:b0:6df:8c1a:d08b with SMTP id sh19-20020a1709076e9300b006df8c1ad08bmr10168462ejc.557.1649071970362; Mon, 04 Apr 2022 04:32:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649071970; cv=none; d=google.com; s=arc-20160816; b=kJZgrZkqbUHLMwwJgpPv7hG4MwHZxQ0dKRziTxOmfOzAH0gDSh+mSs7aL+xEJjIMrP L/U2H759AD2TysBbhWe8a6GQuGyA6uoVDwBOH/cSwBJDkYSsh6RDYzKuNVQtcvfRNgjg HkGGWQwrUtyVodJgt4AoVeHrgYzvTajlLTmgvbcyCeXyQLky9Rhbzqp0vvgwjMan8ReM n6NuO8CM0LL26l+ttIlG3JzMT6CWZ8x/JfPe3deD62Bcz+4qt0OMoHi1/Msr919X7agb IKNzsiJ+hfib7T89L/oDU4i4fYgjHu4QHoutNU1eN6OocYZoRSKGjIaYp2qI77lmGRZE neRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=W4wQuv1e0QLLMMI8AdVk7Lp6cUYbx3qcgHfW7gFAwJk=; b=hmn0U/A/gAq63NfSJeWvk0xoy1AGHJIKIdcPqMq1Jp9ImmnTxwucHPCMVoaJ5//Xi9 teOr/8WxOmqsjUbcjemRuEaPJ3PXEiYEvGsfQxVr8xYme2hRIwwLvbkpOKcUgYuXE5uw GRftqhy3eiQj1G+8Vwtz1ZEBvQzvK8O+iKb3EY8oKC/k5tBARimJvJy8CATgQNERhOfu 4JtYn1WoK87rE9AK7g/Cl8zj3GouTPmmdexPOsskxZ9gz7AyXf0OX+AcA3aZ2xlnu6OG J3qhvXRCIwsMrvWimEQsD+NxrZcK2KdF7kMXOj5RBjx58pxkwa/mr0MvXSiHuO2hSBac w3Aw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id qh2-20020a170906eca200b006df76385e33si6098308ejb.723.2022.04.04.04.32.49; Mon, 04 Apr 2022 04:32: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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5883D68B211; Mon, 4 Apr 2022 14:32:25 +0300 (EEST) 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 55D0968AEFE for ; Mon, 4 Apr 2022 14:32:17 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id DDC6424017E for ; Mon, 4 Apr 2022 13:32:16 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id ncgRf68KEOXN for ; Mon, 4 Apr 2022 13:32:16 +0200 (CEST) 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 A009724017C for ; Mon, 4 Apr 2022 13:32:14 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 9A0473A019B; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:29:49 +0200 Message-Id: <20220404113037.13070-2-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 01/49] fftools/ffmpeg: drop an obsolete hack 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Yuqv+TA5PISg Introduced in 05741d70c7a. All encoders should set the timestamps properly now, so it should never be necessary. --- fftools/ffmpeg.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 6d62bdc7b0..afa1b012a6 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1372,9 +1372,6 @@ static void do_video_out(OutputFile *of, av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); } - if (pkt->pts == AV_NOPTS_VALUE && !(enc->codec->capabilities & AV_CODEC_CAP_DELAY)) - pkt->pts = ost->sync_opts; - av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase); if (debug_ts) { From patchwork Mon Apr 4 11:29:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35158 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1911486pzb; Mon, 4 Apr 2022 04:32:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwKmXJks1xyPwq/NAVEXYM6OAI6haiccnXzgGA39ELcEyZLH8TNWGHbTEvs7sE8y+CsfFzo X-Received: by 2002:a17:906:a2c2:b0:6e7:efc2:17f2 with SMTP id by2-20020a170906a2c200b006e7efc217f2mr3706130ejb.542.1649071959863; Mon, 04 Apr 2022 04:32:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649071959; cv=none; d=google.com; s=arc-20160816; b=lJmgAO9kh7sn37eyHxj/ZiW0h9QYmHCZ0UIunhDUXlDJF74xagrtByaUutwE4l0Rsb e0Mfa5uxZWbXDq80gPISH0mBeQZpwuJp96c3nLrSYZevNP3bwRksz1tEgHmcDp7u7unc GsonXir0m65ebExbNVhdSkiOclkMIZ6JEFr/yHliheC8sysCMEEHziSLvLp3izfy9ssM Vgj9qoeDHvGFiTY9pMbv2iM5A9ZZgagW22MIKJyjthD6oqYihi9f6UZh/t72chRoBjaL WtmbC7gs2NXfoz2Xk2FOURc4P6BJjkh6Oe5SCPg4R6KHVXMzpf+kskg9BkahLPnTFqoy PkDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=SSyFQtTbgawKs68w8hTQOMi+AjyUtsAKoqosFFNE8yk=; b=gPX+K+WLtFN9k2FnGd+/vp+JfKHdAa63PNgxr4Tsu2bPXrelhquwE8kS/OQHXFncbY rHJdEsFRQ6Oav/aZ3UFU0A/mtHCK3QnwzxHd1N9hVOnVRjaMQQh8l5Z6tiT4PQDqYsIc m64vQ+2UaxCpmh2pCwUgySZigenCwyJH+dUf6xx32zhReub1m5Y0pramR2z7jXBk1ZC5 ExwkEdY7uHhv4pVFKtBrm/04HqbLwQYAWoc+E926UwS6OLb6WT8wokx8B958F0KRaRt6 L33sHurtalcaYyDxd9ZEOWNy7EpiKo3UQ0TuxUEffzLAWjUg1+gtTnEXLqBlvNf3+V+W cudg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id og12-20020a1709071dcc00b006e00eb3a62bsi5541433ejc.101.2022.04.04.04.32.39; Mon, 04 Apr 2022 04:32:39 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4642D68B20C; Mon, 4 Apr 2022 14:32:24 +0300 (EEST) 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 E718D68AEFE for ; Mon, 4 Apr 2022 14:32:16 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 0D85D240694 for ; Mon, 4 Apr 2022 13:32:16 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id IVqfeAx_juM9 for ; Mon, 4 Apr 2022 13:32:14 +0200 (CEST) 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 9CB13240179 for ; Mon, 4 Apr 2022 13:32:14 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 9FF893A0663; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:29:50 +0200 Message-Id: <20220404113037.13070-3-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 02/49] fftools/ffmpeg: move a comment to a more appropriate place 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 5Dh90+WWr8Cb --- fftools/ffmpeg.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index afa1b012a6..13be32f0cf 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1238,6 +1238,11 @@ static void do_video_out(OutputFile *of, } } + /* + * For video, number of frames in == number of packets out. + * But there may be reordering, so we can't throw away frames on encoder + * flush, we need to limit them here, before they go into encoder. + */ nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number); nb0_frames = FFMIN(nb0_frames, nb_frames); @@ -1392,11 +1397,6 @@ static void do_video_out(OutputFile *of, } } ost->sync_opts++; - /* - * For video, number of frames in == number of packets out. - * But there may be reordering, so we can't throw away frames on encoder - * flush, we need to limit them here, before they go into encoder. - */ ost->frame_number++; if (vstats_filename && frame_size) From patchwork Mon Apr 4 11:29:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35161 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1911777pzb; Mon, 4 Apr 2022 04:33:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxGUlGL7ToFRIF0u+CwZ1f+VxpmnULltw5yxoty205J5DzEO9UZUJEpTHZ5UM2Ab9UQCSQB X-Received: by 2002:a17:907:c16:b0:6db:1dfc:ca73 with SMTP id ga22-20020a1709070c1600b006db1dfcca73mr10377678ejc.192.1649071991564; Mon, 04 Apr 2022 04:33:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649071991; cv=none; d=google.com; s=arc-20160816; b=p9M7e5DKvnVduT3T373hWbX4fU9Gl/9cSMkV3vLWCVbxXLKnJn2T6EcfemgOY7mzUj AnjqlQvyh30xViojS8tIPe+UswFd+Ir+mR0uvIGaQGY9K/O/i/D37wArHPFZx0cZnqiZ LRwHNtfw5A73mra4Ud2vhizHp3Ww3kd3ZCXVKQz86SsUn0OeehAHmvZFs02D9N87S4ym GEgVRlgs1XaZNyxmJVso+ibMuSsm3xtdHLrIJKKv4I/xz5gQ4JzHT4pYPqXgxd1CD/Xz xJ/VTOSjsFRaUp6B0ORTyRSsZvgMVVBBkhWHStCJZnObKqA75D88/+MUN0EW6QazCkIP c+WA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=Yl+d7xQC8mOH5GCips+9mAr6MDFrw46E7vHimATOw8c=; b=g1Ix1AF1speVcPMWgWpNP7Eftgta4NNUfatfJ4LW2tJZnD2ML5MZ/gDi8qxjZn/iAj 8TFfVWp2jljkYKkJ+ds530r1ydlp199WgJc+yNjOFtzMbK2et9DT2pHvxyzJe648GTTB QuXJZ+hq1UnEz9mfrCnFbT1mfal0D/ThHC9BJrmOFKamnCbiQeet9gcg2t06wn+/Sj0y sqTY1wMtBUJaHJBv9eH0wS2SI48Zcpd2oq8DXVZ6xcStrswIKQTC4TFuVaVo2t4ZTix3 ZUBwrAapGReW58Lz1UskpCxcMrOmFxkMq9bDSjTN2U/y+SXkHtOlVyeU10fEkmcmSd3G 6qWg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id f23-20020a17090684d700b006df76385c1asi1460165ejy.186.2022.04.04.04.33.10; Mon, 04 Apr 2022 04:33:11 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7FA2568B256; Mon, 4 Apr 2022 14:32:27 +0300 (EEST) 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 1DBA568B109 for ; Mon, 4 Apr 2022 14:32:18 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id A58762404FE for ; Mon, 4 Apr 2022 13:32:17 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id KJvFrmNnTElR for ; Mon, 4 Apr 2022 13:32:14 +0200 (CEST) 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 9109E2400F5 for ; Mon, 4 Apr 2022 13:32:14 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id A37D83A06CD; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:29:51 +0200 Message-Id: <20220404113037.13070-4-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 03/49] fftools/ffmpeg: stop using OutputStream.frame_number for streamcopy 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: OzhF0ZdF7bi6 This field is currently used by checks - skipping packets before the first keyframe - skipping packets before start time to test whether any packets have been output already. But since frame_number is incremented after the bitstream filters are applied (which may involve delay), this use is incorrect. The keyframe check works around this by adding an extra flag, the start-time check does not. Simplify both checks by replacing the seen_kf flag with a flag tracking whether any packets have been output by do_streamcopy(). --- fftools/ffmpeg.c | 10 +++++----- fftools/ffmpeg.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 13be32f0cf..29b01f9d93 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -894,8 +894,6 @@ static void output_packet(OutputFile *of, AVPacket *pkt, /* apply the output bitstream filters */ if (ost->bsf_ctx) { - if (pkt->flags & AV_PKT_FLAG_KEY) - ost->seen_kf = 1; ret = av_bsf_send_packet(ost->bsf_ctx, eof ? NULL : pkt); if (ret < 0) goto finish; @@ -2043,11 +2041,11 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p return; } - if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) && - !ost->copy_initial_nonkeyframes && !ost->seen_kf) + if (!ost->streamcopy_started && !(pkt->flags & AV_PKT_FLAG_KEY) && + !ost->copy_initial_nonkeyframes) return; - if (!ost->frame_number && !ost->copy_prior_start) { + if (!ost->streamcopy_started && !ost->copy_prior_start) { int64_t comp_start = start_time; if (copy_ts && f->start_time != AV_NOPTS_VALUE) comp_start = FFMAX(start_time, f->start_time + f->ts_offset); @@ -2101,6 +2099,8 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p ost->sync_opts += opkt->duration; output_packet(of, opkt, ost, 0); + + ost->streamcopy_started = 1; } int guess_input_channel_layout(InputStream *ist) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 1e14bf9fa9..04369df139 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -536,7 +536,7 @@ typedef struct OutputStream { int inputs_done; const char *attachment_filename; - int seen_kf; + int streamcopy_started; int copy_initial_nonkeyframes; int copy_prior_start; char *disposition; From patchwork Mon Apr 4 11:29:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35160 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1911684pzb; Mon, 4 Apr 2022 04:33:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxBN6vdwWC3XuB/V87+97B8YdrQyXQqgJzzKeTznAKhWPxtY8Cn0sEc42Ix2ZfoxRfnKDkh X-Received: by 2002:a05:6402:458:b0:418:78a4:ac3f with SMTP id p24-20020a056402045800b0041878a4ac3fmr32307539edw.196.1649071980925; Mon, 04 Apr 2022 04:33:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649071980; cv=none; d=google.com; s=arc-20160816; b=ocsowYWj6Ah7ynvqqn7HK9ETlf3l+NfNINyXQX9K1lpoYP8H7TbIQwuRDKLZ4SsWEq hOnwEnPu79cJPO8WKmJ1Oj0ojuRLzyYcAQkVEfU12HHhIDv8IrFtpb28Oe3/lW66VKVo REN1s6KNCLznF8K7FPFn8/xOZZ+DYSajmloqQrOYsTCZEiAIRvRvHXfjIqLg6t2Qpt+c n+dmxwuYgm7SIcJF22Zba4Iko5aP195cie0J3LYx7q4VBvuyakF1wOMnutyxnXqyfCUW CP308oKz4VEZPE0fJZQBe9llWSDIRLDvGyvBJCJPplIsZtvb5/qtTYTBtfvOmHUwHZo7 /KUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=6EsOYdd0fKP4gLYKrr2M5lXcgcDlC6VUdKFsagaFeCM=; b=htLY4oejT0QbJZuUssv/eq7wyYeTgTiog0nzAoTd22WW8x0Bl8VGEGybQP2C6MdFzX bb+eUfIzsLYnScJSIPEj7iPreSDkMWEhyMzMeIJg4acTFlrvpgu/CvONekI++vVH7Iqm GR4CamLBbc3+/2cUldFJlciHNxXwk+BWDthjWOHU5AvA6ZHTi6x8MDhMGzgvpCoQKDUy kQ6oMKJBvIRvsg0+6yucP48CoLb3jJE4nk4L3j8MG2YvS6oHqJlwQjs0vL5VbH0TMlkG k4LXmsDl5fjilDtbSrLpFWN5SXto09HchkPNUpnEosNf/xBmeSPLVZdmG2e6WBgbvWoy kzpQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id bw7-20020a170906c1c700b006df76385d9fsi6268768ejb.575.2022.04.04.04.33.00; Mon, 04 Apr 2022 04:33:00 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7791A68B235; Mon, 4 Apr 2022 14:32:26 +0300 (EEST) 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 D8B1068B109 for ; Mon, 4 Apr 2022 14:32:17 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 6B19D24017C for ; Mon, 4 Apr 2022 13:32:17 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id sxQ2wPK-3FmC for ; Mon, 4 Apr 2022 13:32:16 +0200 (CEST) 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 B2BAF2404FE for ; Mon, 4 Apr 2022 13:32:14 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id A76453A06EC; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:29:52 +0200 Message-Id: <20220404113037.13070-5-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 04/49] fftools/ffmpeg: pass the muxer context explicitly to some functions 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: ln/pumYQfqNa Stop accessing OutputFile.ctx. This will be useful in the following commits, where it will become hidden. --- fftools/ffmpeg_opt.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 22c88287df..8e217af4ab 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2125,10 +2125,10 @@ static int opt_streamid(void *optctx, const char *opt, const char *arg) return 0; } -static int copy_chapters(InputFile *ifile, OutputFile *ofile, int copy_metadata) +static int copy_chapters(InputFile *ifile, OutputFile *ofile, AVFormatContext *os, + int copy_metadata) { AVFormatContext *is = ifile->ctx; - AVFormatContext *os = ofile->ctx; AVChapter **tmp; int i; @@ -2168,14 +2168,14 @@ static int copy_chapters(InputFile *ifile, OutputFile *ofile, int copy_metadata) return 0; } -static int set_dispositions(OutputFile *of) +static int set_dispositions(OutputFile *of, AVFormatContext *ctx) { int nb_streams[AVMEDIA_TYPE_NB] = { 0 }; int have_default[AVMEDIA_TYPE_NB] = { 0 }; int have_manual = 0; // first, copy the input dispositions - for (int i = 0; i< of->ctx->nb_streams; i++) { + for (int i = 0; i < ctx->nb_streams; i++) { OutputStream *ost = output_streams[of->ost_index + i]; nb_streams[ost->st->codecpar->codec_type]++; @@ -2192,7 +2192,7 @@ static int set_dispositions(OutputFile *of) if (have_manual) { // process manually set dispositions - they override the above copy - for (int i = 0; i< of->ctx->nb_streams; i++) { + for (int i = 0; i < ctx->nb_streams; i++) { OutputStream *ost = output_streams[of->ost_index + i]; int ret; @@ -2218,7 +2218,7 @@ static int set_dispositions(OutputFile *of) // For each media type with more than one stream, find a suitable stream to // mark as default, unless one is already marked default. // "Suitable" means the first of that type, skipping attached pictures. - for (int i = 0; i< of->ctx->nb_streams; i++) { + for (int i = 0; i < ctx->nb_streams; i++) { OutputStream *ost = output_streams[of->ost_index + i]; enum AVMediaType type = ost->st->codecpar->codec_type; @@ -2760,7 +2760,7 @@ loop_end: } } if (o->chapters_input_file >= 0) - copy_chapters(input_files[o->chapters_input_file], of, + copy_chapters(input_files[o->chapters_input_file], of, oc, !o->metadata_chapters_manual); /* copy global metadata by default */ @@ -2913,7 +2913,7 @@ loop_end: } } - err = set_dispositions(of); + err = set_dispositions(of, oc); if (err < 0) { av_log(NULL, AV_LOG_FATAL, "Error setting output stream dispositions\n"); exit_program(1); From patchwork Mon Apr 4 11:29:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35175 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1913227pzb; Mon, 4 Apr 2022 04:35:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyRD2+EhjKVZVyRtbWRjPNWwrcq5A1FNbyjM8dolxLoyiGY1dgAFCJVqQFMeRmjc7JGqo/A X-Received: by 2002:aa7:d553:0:b0:416:4dfc:126d with SMTP id u19-20020aa7d553000000b004164dfc126dmr32341932edr.213.1649072154139; Mon, 04 Apr 2022 04:35:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072154; cv=none; d=google.com; s=arc-20160816; b=fQe7yUBq7Na+A4FVvUGK8iUOv5cJkq93LAgol2nqn+16x9y5mf09JAs/wE6bhTQlD9 mWaNwFZcv4zSR0J8CFkEQn29SJJ9GJfH+9JXnvKeoFbOst4uXz5QT4yATFUYeMqzTrW0 v/tMgb5Pfu8X+5/gPrjGn11vVsWhZ78d0OoD2kmDNsp/YTZMrwJppRf2e1RHMHkVu1d2 KMos8DZIoxRde8nIgf5VwMFAZ9IhwhxZiUJ6a1/aaV9KeQOA5Kt+awV0ehZQp3jeEmQl eBd9D5Sf9OBvmy2C8gT9rJH9GAs2vdwPIikZxFZbhpeyDK8cPeG8TNBOrg9kxQ3EY/9I XFGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=tZVBFn/Fx2hYsEeiggkMso80WwndJQAWBBGTHhT7w10=; b=ZJWZnPLWT2OXPsBDQLip1AkkcuV9jOCp3W8XmWyh79A6Au9JBkgDEHiGJ4iKD+3FLr fM2hATby+PMK+pvLm0wMtty7EUeA4qII3DKq3pKJUIgi2MKVbQfJVCfzPJMi7spDYr6P B/TCzF8nBZ0HQ7REVtVA/eVperXeAfA+T0vFKdPTVSBDQljK2H1RuTwA4nDFEiKhJEeH ThFLMAdMz9wxdkgguqOCKLgq1sDfCwKM/4NbiHFTUM3O/t9cCf5vrQ+6A+xwIOIMZ0YM Ptd3hHJUtrMQ0227GRKnrRF7EqopzxqNKyjPgyuIAiBW6cqBkFLOifTz3X4A9OvZGfeM uPYg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id u1-20020a50c2c1000000b0041cd94de942si879007edf.591.2022.04.04.04.35.53; Mon, 04 Apr 2022 04:35:54 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2A2B368B30A; Mon, 4 Apr 2022 14:32:43 +0300 (EEST) 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 2E37368B255 for ; Mon, 4 Apr 2022 14:32:22 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id DD6042400F5 for ; Mon, 4 Apr 2022 13:32:17 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id awBSYeZpubcs for ; Mon, 4 Apr 2022 13:32:17 +0200 (CEST) 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 CA632240506 for ; Mon, 4 Apr 2022 13:32:14 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id AAF7D3A0763; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:29:53 +0200 Message-Id: <20220404113037.13070-6-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 05/49] fftools/ffmpeg: store the output file index in OutputFile 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Gao+Q7q9h1Po Use it to simplify check_init_output_file(). Will allow further simplifications in the following commits. --- fftools/ffmpeg.c | 10 +++++----- fftools/ffmpeg.h | 2 ++ fftools/ffmpeg_opt.c | 1 + 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 29b01f9d93..bc700052b6 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2947,7 +2947,7 @@ static int compare_int64(const void *a, const void *b) } /* open the muxer when all the streams are initialized */ -static int check_init_output_file(OutputFile *of, int file_index) +static int check_init_output_file(OutputFile *of) { int ret, i; @@ -2962,13 +2962,13 @@ static int check_init_output_file(OutputFile *of, int file_index) av_log(NULL, AV_LOG_ERROR, "Could not write header for output file #%d " "(incorrect codec parameters ?): %s\n", - file_index, av_err2str(ret)); + of->index, av_err2str(ret)); return ret; } //assert_avoptions(of->opts); of->header_written = 1; - av_dump_format(of->ctx, file_index, of->ctx->url, 1); + av_dump_format(of->ctx, of->index, of->ctx->url, 1); nb_output_dumped++; if (sdp_filename || want_sdp) { @@ -3571,7 +3571,7 @@ static int init_output_stream(OutputStream *ost, AVFrame *frame, ost->initialized = 1; - ret = check_init_output_file(output_files[ost->file_index], ost->file_index); + ret = check_init_output_file(output_files[ost->file_index]); if (ret < 0) return ret; @@ -3674,7 +3674,7 @@ static int transcode_init(void) for (i = 0; i < nb_output_files; i++) { oc = output_files[i]->ctx; if (oc->oformat->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) { - ret = check_init_output_file(output_files[i], i); + ret = check_init_output_file(output_files[i]); if (ret < 0) goto dump_format; } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 04369df139..0eee6f4da8 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -577,6 +577,8 @@ typedef struct OutputStream { } OutputStream; typedef struct OutputFile { + int index; + AVFormatContext *ctx; AVDictionary *opts; int ost_index; /* index of the first stream in output_streams */ diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 8e217af4ab..daecba9e57 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2314,6 +2314,7 @@ static int open_output_file(OptionsContext *o, const char *filename) of = ALLOC_ARRAY_ELEM(output_files, nb_output_files); + of->index = nb_output_files - 1; of->ost_index = nb_output_streams; of->recording_time = o->recording_time; of->start_time = o->start_time; From patchwork Mon Apr 4 11:29:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35164 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1912191pzb; Mon, 4 Apr 2022 04:33:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyLC5pnt9JhUy7q3paXPUe/EIFJhmATrbpDjuLccCYUOOACiitRSbk4DK5Gt2M9vuvakJy5 X-Received: by 2002:a17:906:280b:b0:6ce:f3c7:688f with SMTP id r11-20020a170906280b00b006cef3c7688fmr10421416ejc.468.1649072034151; Mon, 04 Apr 2022 04:33:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072034; cv=none; d=google.com; s=arc-20160816; b=cNX0lYMujCWXWL67fcE51zln541150ZxZvIU17V1UHmNs00z4XW6fqXiNwXZWvUwhr voTgUSnJtMNJ1hXip33PbJ2IwjRkFOrsuhyPMnbmAas4WsfQIT01CFavLI9W80IK/iIP ocjH0VeD6AF9Wbcrb8MpIIkAzoeHWrEghklc04nwzBeNak7TNrAl3ON92XyebdljFia+ CkAJIaagCMQnLx1/IOwGftZoFfTLnLjaYbe5OxD3U/xazG0wEJuEXsLJ0i3kPiYffJJx npIsoP2atAcr7LHJtV6Us2BZsQjD7M6JXi9yV+IRw7iNVDODb/t9Arcrd9lIimttHfIa kDZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=It2NUIXPMEendKpVLqIm5peZ4otq9Y9aZJCFqrdg7hY=; b=iPlL5MSjR2jmvUmI+wuryS6CPKol6G/H8x/wKQtVokBVgo8LPULOv3ZX+oXy3E/4uL jJoqPmjG5j8kj2hiuUrs5kpUXsexBz8GDLNBFJRPUfBuIA3+CjotWA8VvzzFfKdVa0cF UmgvGfi+K951AEKZOubMim0mxntsREVulHJ+ZNasq6nkn5BBKtJCgeFu81a/PvsmsWAv YR/hu08kI2IBtDxyagM+XZAZVfBFuUW1hb8jibVc8Q8DxjZ/DpIbuIEHeps7cngLRUy6 6QKB7Qbjx/JcG8Q1DGW8I1lwk6Ntbctm32ZCi2GF/kL5D9zjhvf9Ijr7ADdhvbqeY9Fo aJ+w== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id y1-20020a056402358100b00418c2b5bf35si6441621edc.535.2022.04.04.04.33.53; Mon, 04 Apr 2022 04:33:54 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0DE6D68B258; Mon, 4 Apr 2022 14:32:32 +0300 (EEST) 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 5D71268B0FD for ; Mon, 4 Apr 2022 14:32:23 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 36402240699 for ; Mon, 4 Apr 2022 13:32:20 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id I3h7vOJQ_nLr for ; Mon, 4 Apr 2022 13:32:19 +0200 (CEST) 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 EC7F2240512 for ; Mon, 4 Apr 2022 13:32:14 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id AF5403A07AE; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:29:54 +0200 Message-Id: <20220404113037.13070-7-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 06/49] fftools/ffmpeg: move some muxing-related code into a separate file 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: d2mw5SePeJoy This is a first step towards making muxers more independent from the rest of the code. --- fftools/Makefile | 6 +- fftools/ffmpeg.c | 272 ++-------------------------------------- fftools/ffmpeg.h | 10 ++ fftools/ffmpeg_mux.c | 292 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 314 insertions(+), 266 deletions(-) create mode 100644 fftools/ffmpeg_mux.c diff --git a/fftools/Makefile b/fftools/Makefile index 5ebf50d42e..81ad6c4f4f 100644 --- a/fftools/Makefile +++ b/fftools/Makefile @@ -9,7 +9,11 @@ AVBASENAMES = ffmpeg ffplay ffprobe ALLAVPROGS = $(AVBASENAMES:%=%$(PROGSSUF)$(EXESUF)) ALLAVPROGS_G = $(AVBASENAMES:%=%$(PROGSSUF)_g$(EXESUF)) -OBJS-ffmpeg += fftools/ffmpeg_opt.o fftools/ffmpeg_filter.o fftools/ffmpeg_hw.o +OBJS-ffmpeg += \ + fftools/ffmpeg_filter.o \ + fftools/ffmpeg_hw.o \ + fftools/ffmpeg_mux.o \ + fftools/ffmpeg_opt.o \ define DOFFTOOL OBJS-$(1) += fftools/cmdutils.o fftools/opt_common.o fftools/$(1).o $(OBJS-$(1)-yes) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index bc700052b6..47724f7231 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -137,9 +137,9 @@ static int nb_frames_dup = 0; static unsigned dup_warning = 1000; static int nb_frames_drop = 0; static int64_t decode_error_stat[2]; -static unsigned nb_output_dumped = 0; +unsigned nb_output_dumped = 0; -static int want_sdp = 1; +int want_sdp = 1; static BenchmarkTimeStamps current_time; AVIOContext *progress_avio = NULL; @@ -345,7 +345,7 @@ static volatile int received_sigterm = 0; static volatile int received_nb_signals = 0; static atomic_int transcode_init_done = ATOMIC_VAR_INIT(0); static volatile int ffmpeg_exited = 0; -static int main_return_code = 0; +int main_return_code = 0; static int64_t copy_ts_first_pts = AV_NOPTS_VALUE; static void @@ -711,159 +711,6 @@ static void update_benchmark(const char *fmt, ...) } } -static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others) -{ - int i; - for (i = 0; i < nb_output_streams; i++) { - OutputStream *ost2 = output_streams[i]; - ost2->finished |= ost == ost2 ? this_stream : others; - } -} - -static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue) -{ - AVFormatContext *s = of->ctx; - AVStream *st = ost->st; - int ret; - - /* - * Audio encoders may split the packets -- #frames in != #packets out. - * But there is no reordering, so we can limit the number of output packets - * by simply dropping them here. - * Counting encoded video frames needs to be done separately because of - * reordering, see do_video_out(). - * Do not count the packet when unqueued because it has been counted when queued. - */ - if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed) && !unqueue) { - if (ost->frame_number >= ost->max_frames) { - av_packet_unref(pkt); - return; - } - ost->frame_number++; - } - - if (!of->header_written) { - AVPacket *tmp_pkt; - /* the muxer is not initialized yet, buffer the packet */ - if (!av_fifo_can_write(ost->muxing_queue)) { - size_t cur_size = av_fifo_can_read(ost->muxing_queue); - unsigned int are_we_over_size = - (ost->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold; - size_t limit = are_we_over_size ? ost->max_muxing_queue_size : SIZE_MAX; - size_t new_size = FFMIN(2 * cur_size, limit); - - if (new_size <= cur_size) { - av_log(NULL, AV_LOG_ERROR, - "Too many packets buffered for output stream %d:%d.\n", - ost->file_index, ost->st->index); - exit_program(1); - } - ret = av_fifo_grow2(ost->muxing_queue, new_size - cur_size); - if (ret < 0) - exit_program(1); - } - ret = av_packet_make_refcounted(pkt); - if (ret < 0) - exit_program(1); - tmp_pkt = av_packet_alloc(); - if (!tmp_pkt) - exit_program(1); - av_packet_move_ref(tmp_pkt, pkt); - ost->muxing_queue_data_size += tmp_pkt->size; - av_fifo_write(ost->muxing_queue, &tmp_pkt, 1); - return; - } - - if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && video_sync_method == VSYNC_DROP) || - (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0)) - pkt->pts = pkt->dts = AV_NOPTS_VALUE; - - if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { - int i; - uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, - NULL); - ost->quality = sd ? AV_RL32(sd) : -1; - ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE; - - for (i = 0; ierror); i++) { - if (sd && i < sd[5]) - ost->error[i] = AV_RL64(sd + 8 + 8*i); - else - ost->error[i] = -1; - } - - if (ost->frame_rate.num && ost->is_cfr) { - if (pkt->duration > 0) - av_log(NULL, AV_LOG_WARNING, "Overriding packet duration by frame rate, this should not happen\n"); - pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate), - ost->mux_timebase); - } - } - - av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base); - - if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) { - if (pkt->dts != AV_NOPTS_VALUE && - pkt->pts != AV_NOPTS_VALUE && - pkt->dts > pkt->pts) { - av_log(s, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64" in output stream %d:%d, replacing by guess\n", - pkt->dts, pkt->pts, - ost->file_index, ost->st->index); - pkt->pts = - pkt->dts = pkt->pts + pkt->dts + ost->last_mux_dts + 1 - - FFMIN3(pkt->pts, pkt->dts, ost->last_mux_dts + 1) - - FFMAX3(pkt->pts, pkt->dts, ost->last_mux_dts + 1); - } - if ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) && - pkt->dts != AV_NOPTS_VALUE && - ost->last_mux_dts != AV_NOPTS_VALUE) { - int64_t max = ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT); - if (pkt->dts < max) { - int loglevel = max - pkt->dts > 2 || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG; - if (exit_on_error) - loglevel = AV_LOG_ERROR; - av_log(s, loglevel, "Non-monotonous DTS in output stream " - "%d:%d; previous: %"PRId64", current: %"PRId64"; ", - ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts); - if (exit_on_error) { - av_log(NULL, AV_LOG_FATAL, "aborting.\n"); - exit_program(1); - } - av_log(s, loglevel, "changing to %"PRId64". This may result " - "in incorrect timestamps in the output file.\n", - max); - if (pkt->pts >= pkt->dts) - pkt->pts = FFMAX(pkt->pts, max); - pkt->dts = max; - } - } - } - ost->last_mux_dts = pkt->dts; - - ost->data_size += pkt->size; - ost->packets_written++; - - pkt->stream_index = ost->index; - - if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "muxer <- type:%s " - "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s size:%d\n", - av_get_media_type_string(ost->enc_ctx->codec_type), - av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base), - av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base), - av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->st->time_base), - pkt->size - ); - } - - ret = av_interleaved_write_frame(s, pkt); - if (ret < 0) { - print_error("av_interleaved_write_frame()", ret); - main_return_code = 1; - close_all_output_streams(ost, MUXER_FINISHED | ENCODER_FINISHED, ENCODER_FINISHED); - } -} - static void close_output_stream(OutputStream *ost) { OutputFile *of = output_files[ost->file_index]; @@ -898,11 +745,11 @@ static void output_packet(OutputFile *of, AVPacket *pkt, if (ret < 0) goto finish; while ((ret = av_bsf_receive_packet(ost->bsf_ctx, pkt)) >= 0) - write_packet(of, pkt, ost, 0); + of_write_packet(of, pkt, ost, 0); if (ret == AVERROR(EAGAIN)) ret = 0; } else if (!eof) - write_packet(of, pkt, ost, 0); + of_write_packet(of, pkt, ost, 0); finish: if (ret < 0 && ret != AVERROR_EOF) { @@ -2767,59 +2614,6 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eo return !eof_reached; } -static int print_sdp(void) -{ - char sdp[16384]; - int i; - int j, ret; - AVIOContext *sdp_pb; - AVFormatContext **avc; - - for (i = 0; i < nb_output_files; i++) { - if (!output_files[i]->header_written) - return 0; - } - - avc = av_malloc_array(nb_output_files, sizeof(*avc)); - if (!avc) - exit_program(1); - for (i = 0, j = 0; i < nb_output_files; i++) { - if (!strcmp(output_files[i]->ctx->oformat->name, "rtp")) { - avc[j] = output_files[i]->ctx; - j++; - } - } - - if (!j) { - av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n"); - ret = AVERROR(EINVAL); - goto fail; - } - - ret = av_sdp_create(avc, j, sdp, sizeof(sdp)); - if (ret < 0) - goto fail; - - if (!sdp_filename) { - printf("SDP:\n%s\n", sdp); - fflush(stdout); - } else { - ret = avio_open2(&sdp_pb, sdp_filename, AVIO_FLAG_WRITE, &int_cb, NULL); - if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", sdp_filename); - goto fail; - } - - avio_print(sdp_pb, sdp); - avio_closep(&sdp_pb); - av_freep(&sdp_filename); - } - -fail: - av_freep(&avc); - return ret; -} - static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts) { InputStream *ist = s->opaque; @@ -2946,58 +2740,6 @@ static int compare_int64(const void *a, const void *b) return FFDIFFSIGN(*(const int64_t *)a, *(const int64_t *)b); } -/* open the muxer when all the streams are initialized */ -static int check_init_output_file(OutputFile *of) -{ - int ret, i; - - for (i = 0; i < of->ctx->nb_streams; i++) { - OutputStream *ost = output_streams[of->ost_index + i]; - if (!ost->initialized) - return 0; - } - - ret = avformat_write_header(of->ctx, &of->opts); - if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, - "Could not write header for output file #%d " - "(incorrect codec parameters ?): %s\n", - of->index, av_err2str(ret)); - return ret; - } - //assert_avoptions(of->opts); - of->header_written = 1; - - av_dump_format(of->ctx, of->index, of->ctx->url, 1); - nb_output_dumped++; - - if (sdp_filename || want_sdp) { - ret = print_sdp(); - if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, "Error writing the SDP.\n"); - return ret; - } - } - - /* flush the muxing queues */ - for (i = 0; i < of->ctx->nb_streams; i++) { - OutputStream *ost = output_streams[of->ost_index + i]; - AVPacket *pkt; - - /* try to improve muxing time_base (only possible if nothing has been written yet) */ - if (!av_fifo_can_read(ost->muxing_queue)) - ost->mux_timebase = ost->st->time_base; - - while (av_fifo_read(ost->muxing_queue, &pkt, 1) >= 0) { - ost->muxing_queue_data_size -= pkt->size; - write_packet(of, pkt, ost, 1); - av_packet_free(&pkt); - } - } - - return 0; -} - static int init_output_bsfs(OutputStream *ost) { AVBSFContext *ctx = ost->bsf_ctx; @@ -3571,7 +3313,7 @@ static int init_output_stream(OutputStream *ost, AVFrame *frame, ost->initialized = 1; - ret = check_init_output_file(output_files[ost->file_index]); + ret = of_check_init(output_files[ost->file_index]); if (ret < 0) return ret; @@ -3674,7 +3416,7 @@ static int transcode_init(void) for (i = 0; i < nb_output_files; i++) { oc = output_files[i]->ctx; if (oc->oformat->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) { - ret = check_init_output_file(output_files[i]); + ret = of_check_init(output_files[i]); if (ret < 0) goto dump_format; } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 0eee6f4da8..cb475bb690 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -647,6 +647,10 @@ extern char *qsv_device; #endif extern HWDevice *filter_hw_device; +extern int want_sdp; +extern unsigned nb_output_dumped; +extern int main_return_code; + void term_init(void); void term_exit(void); @@ -683,4 +687,10 @@ int hw_device_setup_for_filter(FilterGraph *fg); int hwaccel_decode_init(AVCodecContext *avctx); +/* open the muxer when all the streams are initialized */ +int of_check_init(OutputFile *of); + +void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, + int unqueue); + #endif /* FFTOOLS_FFMPEG_H */ diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c new file mode 100644 index 0000000000..92d8a8c088 --- /dev/null +++ b/fftools/ffmpeg_mux.c @@ -0,0 +1,292 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include "ffmpeg.h" + +#include "libavutil/fifo.h" +#include "libavutil/intreadwrite.h" +#include "libavutil/log.h" +#include "libavutil/mem.h" +#include "libavutil/timestamp.h" + +#include "libavcodec/packet.h" + +#include "libavformat/avformat.h" +#include "libavformat/avio.h" + +static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others) +{ + int i; + for (i = 0; i < nb_output_streams; i++) { + OutputStream *ost2 = output_streams[i]; + ost2->finished |= ost == ost2 ? this_stream : others; + } +} + +void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, + int unqueue) +{ + AVFormatContext *s = of->ctx; + AVStream *st = ost->st; + int ret; + + /* + * Audio encoders may split the packets -- #frames in != #packets out. + * But there is no reordering, so we can limit the number of output packets + * by simply dropping them here. + * Counting encoded video frames needs to be done separately because of + * reordering, see do_video_out(). + * Do not count the packet when unqueued because it has been counted when queued. + */ + if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed) && !unqueue) { + if (ost->frame_number >= ost->max_frames) { + av_packet_unref(pkt); + return; + } + ost->frame_number++; + } + + if (!of->header_written) { + AVPacket *tmp_pkt; + /* the muxer is not initialized yet, buffer the packet */ + if (!av_fifo_can_write(ost->muxing_queue)) { + size_t cur_size = av_fifo_can_read(ost->muxing_queue); + unsigned int are_we_over_size = + (ost->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold; + size_t limit = are_we_over_size ? ost->max_muxing_queue_size : SIZE_MAX; + size_t new_size = FFMIN(2 * cur_size, limit); + + if (new_size <= cur_size) { + av_log(NULL, AV_LOG_ERROR, + "Too many packets buffered for output stream %d:%d.\n", + ost->file_index, ost->st->index); + exit_program(1); + } + ret = av_fifo_grow2(ost->muxing_queue, new_size - cur_size); + if (ret < 0) + exit_program(1); + } + ret = av_packet_make_refcounted(pkt); + if (ret < 0) + exit_program(1); + tmp_pkt = av_packet_alloc(); + if (!tmp_pkt) + exit_program(1); + av_packet_move_ref(tmp_pkt, pkt); + ost->muxing_queue_data_size += tmp_pkt->size; + av_fifo_write(ost->muxing_queue, &tmp_pkt, 1); + return; + } + + if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && video_sync_method == VSYNC_DROP) || + (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0)) + pkt->pts = pkt->dts = AV_NOPTS_VALUE; + + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { + int i; + uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, + NULL); + ost->quality = sd ? AV_RL32(sd) : -1; + ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE; + + for (i = 0; ierror); i++) { + if (sd && i < sd[5]) + ost->error[i] = AV_RL64(sd + 8 + 8*i); + else + ost->error[i] = -1; + } + + if (ost->frame_rate.num && ost->is_cfr) { + if (pkt->duration > 0) + av_log(NULL, AV_LOG_WARNING, "Overriding packet duration by frame rate, this should not happen\n"); + pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate), + ost->mux_timebase); + } + } + + av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base); + + if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) { + if (pkt->dts != AV_NOPTS_VALUE && + pkt->pts != AV_NOPTS_VALUE && + pkt->dts > pkt->pts) { + av_log(s, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64" in output stream %d:%d, replacing by guess\n", + pkt->dts, pkt->pts, + ost->file_index, ost->st->index); + pkt->pts = + pkt->dts = pkt->pts + pkt->dts + ost->last_mux_dts + 1 + - FFMIN3(pkt->pts, pkt->dts, ost->last_mux_dts + 1) + - FFMAX3(pkt->pts, pkt->dts, ost->last_mux_dts + 1); + } + if ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) && + pkt->dts != AV_NOPTS_VALUE && + ost->last_mux_dts != AV_NOPTS_VALUE) { + int64_t max = ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT); + if (pkt->dts < max) { + int loglevel = max - pkt->dts > 2 || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG; + if (exit_on_error) + loglevel = AV_LOG_ERROR; + av_log(s, loglevel, "Non-monotonous DTS in output stream " + "%d:%d; previous: %"PRId64", current: %"PRId64"; ", + ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts); + if (exit_on_error) { + av_log(NULL, AV_LOG_FATAL, "aborting.\n"); + exit_program(1); + } + av_log(s, loglevel, "changing to %"PRId64". This may result " + "in incorrect timestamps in the output file.\n", + max); + if (pkt->pts >= pkt->dts) + pkt->pts = FFMAX(pkt->pts, max); + pkt->dts = max; + } + } + } + ost->last_mux_dts = pkt->dts; + + ost->data_size += pkt->size; + ost->packets_written++; + + pkt->stream_index = ost->index; + + if (debug_ts) { + av_log(NULL, AV_LOG_INFO, "muxer <- type:%s " + "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s size:%d\n", + av_get_media_type_string(ost->enc_ctx->codec_type), + av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base), + av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base), + av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->st->time_base), + pkt->size + ); + } + + ret = av_interleaved_write_frame(s, pkt); + if (ret < 0) { + print_error("av_interleaved_write_frame()", ret); + main_return_code = 1; + close_all_output_streams(ost, MUXER_FINISHED | ENCODER_FINISHED, ENCODER_FINISHED); + } +} + +static int print_sdp(void) +{ + char sdp[16384]; + int i; + int j, ret; + AVIOContext *sdp_pb; + AVFormatContext **avc; + + for (i = 0; i < nb_output_files; i++) { + if (!output_files[i]->header_written) + return 0; + } + + avc = av_malloc_array(nb_output_files, sizeof(*avc)); + if (!avc) + exit_program(1); + for (i = 0, j = 0; i < nb_output_files; i++) { + if (!strcmp(output_files[i]->ctx->oformat->name, "rtp")) { + avc[j] = output_files[i]->ctx; + j++; + } + } + + if (!j) { + av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n"); + ret = AVERROR(EINVAL); + goto fail; + } + + ret = av_sdp_create(avc, j, sdp, sizeof(sdp)); + if (ret < 0) + goto fail; + + if (!sdp_filename) { + printf("SDP:\n%s\n", sdp); + fflush(stdout); + } else { + ret = avio_open2(&sdp_pb, sdp_filename, AVIO_FLAG_WRITE, &int_cb, NULL); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", sdp_filename); + goto fail; + } + + avio_print(sdp_pb, sdp); + avio_closep(&sdp_pb); + av_freep(&sdp_filename); + } + +fail: + av_freep(&avc); + return ret; +} + +/* open the muxer when all the streams are initialized */ +int of_check_init(OutputFile *of) +{ + int ret, i; + + for (i = 0; i < of->ctx->nb_streams; i++) { + OutputStream *ost = output_streams[of->ost_index + i]; + if (!ost->initialized) + return 0; + } + + ret = avformat_write_header(of->ctx, &of->opts); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, + "Could not write header for output file #%d " + "(incorrect codec parameters ?): %s\n", + of->index, av_err2str(ret)); + return ret; + } + //assert_avoptions(of->opts); + of->header_written = 1; + + av_dump_format(of->ctx, of->index, of->ctx->url, 1); + nb_output_dumped++; + + if (sdp_filename || want_sdp) { + ret = print_sdp(); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Error writing the SDP.\n"); + return ret; + } + } + + /* flush the muxing queues */ + for (i = 0; i < of->ctx->nb_streams; i++) { + OutputStream *ost = output_streams[of->ost_index + i]; + AVPacket *pkt; + + /* try to improve muxing time_base (only possible if nothing has been written yet) */ + if (!av_fifo_can_read(ost->muxing_queue)) + ost->mux_timebase = ost->st->time_base; + + while (av_fifo_read(ost->muxing_queue, &pkt, 1) >= 0) { + ost->muxing_queue_data_size -= pkt->size; + of_write_packet(of, pkt, ost, 1); + av_packet_free(&pkt); + } + } + + return 0; +} From patchwork Mon Apr 4 11:29:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35176 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1913309pzb; Mon, 4 Apr 2022 04:36:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzomj/PGX4tDwaLdfCZiPZKDjuLw04X+MOJDnknLah9qv8DeGcHXpwPbAQ54o/0mHumbVff X-Received: by 2002:a17:906:c145:b0:6da:f8c8:34f1 with SMTP id dp5-20020a170906c14500b006daf8c834f1mr9830861ejc.310.1649072164636; Mon, 04 Apr 2022 04:36:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072164; cv=none; d=google.com; s=arc-20160816; b=iRdJL3/ex0TDsgP/0eD94ckOkj4nfvXNpuA6dzm7f0007rsMltlN5qRTC1B5+gRrch ArwPWnapNZh/qcWjfJcYsOge/KKIsvbbDdvRyG/j8s4rl8YHEO1LIrFwpjXrlgYFJjc6 AzMJbJ0Y9JCa/4eUtdbjck21TzW1pUi7zAwHcehTOPelKJ5FCwwng/aS45LHVUHJGfIC Q/jCO2VIFfrnPTHnvTJnAhOiOpNnz+Zb1xgSiuNM+V9eU1L/SY4/f+C7OkLKvw/BbWDt o2RAJHewvmyRwjbgLQu3V3GHCFoU6kbzW6oTFtiM9FfKQxYSI1ArPoeDRy6XQoOQ4tI5 V1qg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=2VYkmkvFDFb37CTDdejpFcwxjIh8N84O47iZ+lw+iEM=; b=h55V30qouFlUR8hMCV83qICMlIw8uGFal8gPO/ls9sRbQK7KpdeUt3qWQc1If0Yhwu /MriLK/yqmUxQuqAZ6qWr4mnmlHIn8K876NfDalUDXyEXzOh2ahldUofJy5WBe6qAZWg QVffhTu7uAUKfm7lyn0yEZAsvevyOKvXKlZ88ARuToUTNlbZPWt6GoIZZ+wAA8/R0VbM MKLYpcr5q5859Qa/vEozbwD0+lmihVadpRExjXjxKdmOSyGiXXoFqL7sZeg4PlgM/Uqn mcvRAFpen6oaMWaNTOYmspObJ7lZ26SOEZHPlsB3Jh0amh1g7wbFwEDFdXm8rVNtRgpG M0OA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id f7-20020a056402354700b0041b61d995ebsi7150053edd.323.2022.04.04.04.36.04; Mon, 04 Apr 2022 04:36:04 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0761168B314; Mon, 4 Apr 2022 14:32:44 +0300 (EEST) 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 38C6868B258 for ; Mon, 4 Apr 2022 14:32:22 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 92B9F240507 for ; Mon, 4 Apr 2022 13:32:18 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id UkIPnTNZA-1F for ; Mon, 4 Apr 2022 13:32:18 +0200 (CEST) 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 EA397240511 for ; Mon, 4 Apr 2022 13:32:14 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id B2E543A0836; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:29:55 +0200 Message-Id: <20220404113037.13070-8-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 07/49] fftools/ffmpeg: move writing the trailer to ffmpeg_mux.c 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: NfxFQuleqZqY --- fftools/ffmpeg.c | 16 +++------------- fftools/ffmpeg.h | 1 + fftools/ffmpeg_mux.c | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 47724f7231..cc7855a4cc 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -4450,19 +4450,9 @@ static int transcode(void) /* write the trailer if needed */ for (i = 0; i < nb_output_files; i++) { - os = output_files[i]->ctx; - if (!output_files[i]->header_written) { - av_log(NULL, AV_LOG_ERROR, - "Nothing was written into output file %d (%s), because " - "at least one of its streams received no packets.\n", - i, os->url); - continue; - } - if ((ret = av_write_trailer(os)) < 0) { - av_log(NULL, AV_LOG_ERROR, "Error writing trailer of %s: %s\n", os->url, av_err2str(ret)); - if (exit_on_error) - exit_program(1); - } + ret = of_write_trailer(output_files[i]); + if (ret < 0 && exit_on_error) + exit_program(1); } /* dump report by using the first video and audio streams */ diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index cb475bb690..4425b7a874 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -689,6 +689,7 @@ int hwaccel_decode_init(AVCodecContext *avctx); /* open the muxer when all the streams are initialized */ int of_check_init(OutputFile *of); +int of_write_trailer(OutputFile *of); void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 92d8a8c088..5348eef01d 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -290,3 +290,24 @@ int of_check_init(OutputFile *of) return 0; } + +int of_write_trailer(OutputFile *of) +{ + int ret; + + if (!of->header_written) { + av_log(NULL, AV_LOG_ERROR, + "Nothing was written into output file %d (%s), because " + "at least one of its streams received no packets.\n", + of->index, of->ctx->url); + return AVERROR(EINVAL); + } + + ret = av_write_trailer(of->ctx); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Error writing trailer of %s: %s\n", of->ctx->url, av_err2str(ret)); + return ret; + } + + return 0; +} From patchwork Mon Apr 4 11:29:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35174 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1913146pzb; Mon, 4 Apr 2022 04:35:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzUNF9bLzMeAlYdcZayggnf5qVqhid24E7vW2WnoAgp2fl/9zv+u6oG52KfGXIrOSIZDWak X-Received: by 2002:a05:6402:4396:b0:418:d776:14c1 with SMTP id o22-20020a056402439600b00418d77614c1mr32511576edc.127.1649072143596; Mon, 04 Apr 2022 04:35:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072143; cv=none; d=google.com; s=arc-20160816; b=Ny0pk7u2PL9FCL9kRQCMT1jloKEXraWhs9ISqeGAYwTLrPccSN/SpClVFZ3KJ3E0lv xhIkIHFU6fAJkai6r7A7nQcY0GbpQupydglLLy5PcRnL4QRR3vsLfG9yh3ot2/gPleu3 ryBMf7uS676E+0tbHZ31GK4gc7b6EcEpYYslYSKNgwqpNxHiKKD4qSgjtJyUQAUOFLJA CGzlNwAOrhZYOwKqeH8NB1VC3F5+voJOUKhkOlW0M+fC+vzoZ+9L8JzRcgBT7MIq3XZM 3cZc1UPLsr+ghxWl6MtWjs/ABL2yeUBN+OuS0X+HHW8Qs8voC5t9vYJCUaa79vJUWStf Hiaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=nGXhdv6rhxF7Aa4RAZbi0hdE9/WbKtpkjD3bCI0Pn9s=; b=XOAYobZMkUzhsa9CBJh83Hn/+OMzDyoNx+csnnY8pGjKJGqarAJpcXf6OfSBOAtYj6 YHR0gL7TFeNPA0tzDgvl6MK2QBcm5zqdvdUZ4kS9YDlV2tbxTzZ5UEqCB8a2Y+c5oc5v 1eJoAWNxEnOtJf5bUlBHAdJD12KDnIB4b/Sypj2h9iSnXu2skLxIoGtnL9RDo0A/abzO kuHnE5MAxkasBXQ764SyX4AlmELcCFbNWoSAyrbbVa31ckFhyyFHWw2O+7stDzkGgUvr jRp+Cx8NwAon8VgaPnsNj9LkC12Loqsj1ie38BFyAeWYe30iPczwbff78B5JRHc1FQO4 ULCw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id m10-20020a1709061eca00b006df889bdb15si6403752ejj.310.2022.04.04.04.35.43; Mon, 04 Apr 2022 04:35:43 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4A59168B304; Mon, 4 Apr 2022 14:32:42 +0300 (EEST) 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 2DB7E68B254 for ; Mon, 4 Apr 2022 14:32:22 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 142BE240506 for ; Mon, 4 Apr 2022 13:32:18 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 6BtX1xGOhoew for ; Mon, 4 Apr 2022 13:32:17 +0200 (CEST) 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 E9CCB24050B for ; Mon, 4 Apr 2022 13:32:14 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id B6BB23A0837; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:29:56 +0200 Message-Id: <20220404113037.13070-9-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 08/49] fftools/ffmpeg: move freeing the output file to ffmpeg_mux.c 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: vXmDJE37oSE1 --- fftools/ffmpeg.c | 14 ++------------ fftools/ffmpeg.h | 1 + fftools/ffmpeg_mux.c | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index cc7855a4cc..69d1949103 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -565,19 +565,9 @@ static void ffmpeg_cleanup(int ret) av_freep(&subtitle_out); /* close files */ - for (i = 0; i < nb_output_files; i++) { - OutputFile *of = output_files[i]; - AVFormatContext *s; - if (!of) - continue; - s = of->ctx; - if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE)) - avio_closep(&s->pb); - avformat_free_context(s); - av_dict_free(&of->opts); + for (i = 0; i < nb_output_files; i++) + of_close(&output_files[i]); - av_freep(&output_files[i]); - } for (i = 0; i < nb_output_streams; i++) { OutputStream *ost = output_streams[i]; diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 4425b7a874..ff8ebbfab5 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -690,6 +690,7 @@ int hwaccel_decode_init(AVCodecContext *avctx); /* open the muxer when all the streams are initialized */ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); +void of_close(OutputFile **pof); void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 5348eef01d..3cdaa494d7 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -311,3 +311,20 @@ int of_write_trailer(OutputFile *of) return 0; } + +void of_close(OutputFile **pof) +{ + OutputFile *of = *pof; + AVFormatContext *s; + + if (!of) + return; + + s = of->ctx; + if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE)) + avio_closep(&s->pb); + avformat_free_context(s); + av_dict_free(&of->opts); + + av_freep(pof); +} From patchwork Mon Apr 4 11:29:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35177 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1913384pzb; Mon, 4 Apr 2022 04:36:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxsSNAxZA+/m6QGf7rmiME3DD4So8StDG7jsBthtFHsEkF8DBARKjZoG56tlLzw6VnHEX6X X-Received: by 2002:a17:907:7e96:b0:6da:f7ee:4a25 with SMTP id qb22-20020a1709077e9600b006daf7ee4a25mr10061524ejc.436.1649072175527; Mon, 04 Apr 2022 04:36:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072175; cv=none; d=google.com; s=arc-20160816; b=SfCQGkxFUz/7esFlLHz/0z9XFzr19bxXvjK7eErcz3bupv5fNw1dZqouQbCUgN3FFD uV8FpfedfjgATBjEGZF0UkRv8qRIfUFoSxCoBtr6GKEW71EtcwSe+ncQYbSjcM4b+zg0 09ecqzTSlD7pX1kVA3WmIeIZ6BsXTDIb27GmJM+cQ9foGuzNVmSnMd7y+JCljjH2MVZ8 x+RFgG6q3C1sAyPDjQee37GfiuIQa67JKD0EeSBCOeruIVB99GAvxABbwKhZJRv8Gnwe 36NTxPbVVDyRqw+3epV79/AEkxZeQJZq+9aWtbtsdrAxNSQpMvaysXQTFjTSR0Pkpsea wzWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=qIIxt90Ea8kw6RIKZRUVJUY/lUHsTo4IMjR5eF1s74Y=; b=jgtI5T6xcuEy5pGh3JQGx4RYsCtv5drvFQedgkpSm7f58mVJt7pCn2V8OdN50HCul+ oE8ZlWorMgkt7IloY3G25a4hEFw6YNzNuWYlC0/AcFrjFeSUv9m73X/IL5pY9T3bzYgf A0Ps8uu0w6VzwNJ/pPiR4MGhqPvgKF1whm95e++l32itbHHQpT/9bQfycBOjYe+/9OBi YLYTSd4bVccng9s2B8v7i2dYUa30ul4XrFRf39Cjcvwox+kuP1XYOwfInVqwDAG4sDzb 2DVwHtDN9rbOJ4y1e52bwW00X+LFhNnGDxoZ6hM7p8+vdneiWSDBosycW4OioCSHD6sw JFKw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id x12-20020a50d9cc000000b00418c2b5be89si4888581edj.363.2022.04.04.04.36.15; Mon, 04 Apr 2022 04:36:15 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0396D68B318; Mon, 4 Apr 2022 14:32:45 +0300 (EEST) 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 34EB968B256 for ; Mon, 4 Apr 2022 14:32:22 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 76A2F24050B for ; Mon, 4 Apr 2022 13:32:18 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id CUdbMy5d9Jhu for ; Mon, 4 Apr 2022 13:32:17 +0200 (CEST) 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 E9AFF240507 for ; Mon, 4 Apr 2022 13:32:14 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id BA4663A0984; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:29:57 +0200 Message-Id: <20220404113037.13070-10-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 09/49] fftools/ffmpeg: store output format separately from the muxer context 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: wm2/MQZumOZP Allows accessing it without going through the muxer context. This will be useful in the following commits, where the muxer context will be hidden. --- fftools/ffmpeg.c | 18 ++++++++++-------- fftools/ffmpeg.h | 2 ++ fftools/ffmpeg_opt.c | 1 + 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 69d1949103..a85ed18b08 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2789,9 +2789,9 @@ static int init_output_stream_streamcopy(OutputStream *ost) if (!codec_tag) { unsigned int codec_tag_tmp; - if (!of->ctx->oformat->codec_tag || - av_codec_get_id (of->ctx->oformat->codec_tag, par_src->codec_tag) == par_src->codec_id || - !av_codec_get_tag2(of->ctx->oformat->codec_tag, par_src->codec_id, &codec_tag_tmp)) + if (!of->format->codec_tag || + av_codec_get_id (of->format->codec_tag, par_src->codec_tag) == par_src->codec_id || + !av_codec_get_tag2(of->format->codec_tag, par_src->codec_id, &codec_tag_tmp)) codec_tag = par_src->codec_tag; } @@ -2809,7 +2809,7 @@ static int init_output_stream_streamcopy(OutputStream *ost) else ost->st->avg_frame_rate = ist->st->avg_frame_rate; - ret = avformat_transfer_internal_stream_timing_info(of->ctx->oformat, ost->st, ist->st, copy_tb); + ret = avformat_transfer_internal_stream_timing_info(of->format, ost->st, ist->st, copy_tb); if (ret < 0) return ret; @@ -3011,7 +3011,8 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) InputStream *ist = get_input_stream(ost); AVCodecContext *enc_ctx = ost->enc_ctx; AVCodecContext *dec_ctx = NULL; - AVFormatContext *oc = output_files[ost->file_index]->ctx; + OutputFile *of = output_files[ost->file_index]; + AVFormatContext *oc = of->ctx; int ret; set_encoder_id(output_files[ost->file_index], ost); @@ -3072,7 +3073,8 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) if (!(enc_ctx->time_base.num && enc_ctx->time_base.den)) enc_ctx->time_base = av_buffersink_get_time_base(ost->filter->filter); if ( av_q2d(enc_ctx->time_base) < 0.001 && video_sync_method != VSYNC_PASSTHROUGH - && (video_sync_method == VSYNC_CFR || video_sync_method == VSYNC_VSCFR || (video_sync_method == VSYNC_AUTO && !(oc->oformat->flags & AVFMT_VARIABLE_FPS)))){ + && (video_sync_method == VSYNC_CFR || video_sync_method == VSYNC_VSCFR || + (video_sync_method == VSYNC_AUTO && !(of->format->flags & AVFMT_VARIABLE_FPS)))){ av_log(oc, AV_LOG_WARNING, "Frame rate very high for a muxer not efficiently supporting it.\n" "Please consider specifying a lower framerate, a different muxer or -vsync 2\n"); } @@ -3405,7 +3407,7 @@ static int transcode_init(void) /* write headers for files with no streams */ for (i = 0; i < nb_output_files; i++) { oc = output_files[i]->ctx; - if (oc->oformat->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) { + if (output_files[i]->format->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) { ret = of_check_init(output_files[i]); if (ret < 0) goto dump_format; @@ -4615,7 +4617,7 @@ int main(int argc, char **argv) } for (i = 0; i < nb_output_files; i++) { - if (strcmp(output_files[i]->ctx->oformat->name, "rtp")) + if (strcmp(output_files[i]->format->name, "rtp")) want_sdp = 0; } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index ff8ebbfab5..9f0c093e34 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -579,6 +579,8 @@ typedef struct OutputStream { typedef struct OutputFile { int index; + const AVOutputFormat *format; + AVFormatContext *ctx; AVDictionary *opts; int ost_index; /* index of the first stream in output_streams */ diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index daecba9e57..47e8b9b7bd 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2332,6 +2332,7 @@ static int open_output_file(OptionsContext *o, const char *filename) } of->ctx = oc; + of->format = oc->oformat; if (o->recording_time != INT64_MAX) oc->duration = o->recording_time; From patchwork Mon Apr 4 11:29:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35178 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1913478pzb; Mon, 4 Apr 2022 04:36:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwH+TTnOPMpnQ7ntDVbd5XHE/7xfxqiFA10aSdrSi3HDwsNXEZfjqWjgkNcqvq8kMmg2FCV X-Received: by 2002:a17:907:1c82:b0:6e0:acef:d238 with SMTP id nb2-20020a1709071c8200b006e0acefd238mr10552579ejc.96.1649072186861; Mon, 04 Apr 2022 04:36:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072186; cv=none; d=google.com; s=arc-20160816; b=QocOBCqrVo4H9nyYlSckMBfsL7MOp5ucdh2+lv24QqmBEqu/+9SvTdEbFszyGXxAdN JQFU1HD4apfX/U8g+IaMQdLPI5Hmov1arz8Flmwk8b2b862qC/ej2n29zrJw1/tdtRk9 1YDW+zXGV13NAjPmzx4fGZiusT7c1HasvPtvSXs4XJoGumstaDh9eZqjfbcRtvdyJI/K PzO0/AbxnaMMx7GeIaOBWijArin7YES8P2D1PkFewTXyoPvOGYhrS6MOFgQZyiIAlSH6 4I3SYKf58eeajKX0xace5yTXDC0wRMaYM4RXNqNwZGsnJlxQLW6HAKCuJ0GXwxZAplj8 GSDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=OBosacRrtr8qYh/eBvhqwOEju30YCnxXGVSLQXFALKs=; b=bmvzQrjbBDbdBaVFebhn2Y0XXLsCnMnh3OPGAlcGW1GXVisx9aWC1KeVaRWapBZwuM KzSE2BRftpZTsWkRfl0JMBZpvIUBpedAjxDlQgl67mgDsdniCIsKDYl7nP3u16bLRrRq aS2wlg61z/KkrwEbXCkaHlAUGurCqUA1p6MT3uAW5YE9uMdfiAZgC+o/BY0HUGQ1+1ib oRuy0TKDfBJvHOcfxraD+MA4TW64b9w9pO6erz7Dsl8tqEvb8kYsCxiPb1knjL8XXBGL NWJbv7W7chc97wCOlvA4xBGS1wigYy2NCr+TPquSNQYkED6rPOI12WXenJDRVNM6JAfS 0yuA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id j18-20020a170906831200b006e801269d02si17866ejx.240.2022.04.04.04.36.26; Mon, 04 Apr 2022 04:36:26 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6D65868B323; Mon, 4 Apr 2022 14:32:46 +0300 (EEST) 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 8C53A68B266 for ; Mon, 4 Apr 2022 14:32:22 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 00710240511 for ; Mon, 4 Apr 2022 13:32:19 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id YMLt4rzQ56n6 for ; Mon, 4 Apr 2022 13:32:18 +0200 (CEST) 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 ED60724052D for ; Mon, 4 Apr 2022 13:32:14 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id BDD1F3A09AA; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:29:58 +0200 Message-Id: <20220404113037.13070-11-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 10/49] fftools/ffmpeg_mux: add private muxer context 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 0A9t64I3pYgj Move header_written into it, which is not (and should not be) used by any code outside of ffmpeg_mux. In the future this context will contain more muxer-private state that should not be visible to other code. --- fftools/ffmpeg.h | 6 ++++-- fftools/ffmpeg_mux.c | 26 ++++++++++++++++++++++---- fftools/ffmpeg_opt.c | 6 ++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 9f0c093e34..3db03e0cfe 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -576,9 +576,12 @@ typedef struct OutputStream { int64_t error[4]; } OutputStream; +typedef struct Muxer Muxer; + typedef struct OutputFile { int index; + Muxer *mux; const AVOutputFormat *format; AVFormatContext *ctx; @@ -589,8 +592,6 @@ typedef struct OutputFile { uint64_t limit_filesize; /* filesize limit expressed in bytes */ int shortest; - - int header_written; } OutputFile; extern InputStream **input_streams; @@ -689,6 +690,7 @@ int hw_device_setup_for_filter(FilterGraph *fg); int hwaccel_decode_init(AVCodecContext *avctx); +int of_muxer_init(OutputFile *of); /* open the muxer when all the streams are initialized */ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 3cdaa494d7..84c94e46be 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -32,6 +32,10 @@ #include "libavformat/avformat.h" #include "libavformat/avio.h" +struct Muxer { + int header_written; +}; + static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others) { int i; @@ -64,7 +68,7 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, ost->frame_number++; } - if (!of->header_written) { + if (!of->mux->header_written) { AVPacket *tmp_pkt; /* the muxer is not initialized yet, buffer the packet */ if (!av_fifo_can_write(ost->muxing_queue)) { @@ -195,7 +199,7 @@ static int print_sdp(void) AVFormatContext **avc; for (i = 0; i < nb_output_files; i++) { - if (!output_files[i]->header_written) + if (!output_files[i]->mux->header_written) return 0; } @@ -259,7 +263,7 @@ int of_check_init(OutputFile *of) return ret; } //assert_avoptions(of->opts); - of->header_written = 1; + of->mux->header_written = 1; av_dump_format(of->ctx, of->index, of->ctx->url, 1); nb_output_dumped++; @@ -295,7 +299,7 @@ int of_write_trailer(OutputFile *of) { int ret; - if (!of->header_written) { + if (!of->mux->header_written) { av_log(NULL, AV_LOG_ERROR, "Nothing was written into output file %d (%s), because " "at least one of its streams received no packets.\n", @@ -326,5 +330,19 @@ void of_close(OutputFile **pof) avformat_free_context(s); av_dict_free(&of->opts); + av_freep(&of->mux); + av_freep(pof); } + +int of_muxer_init(OutputFile *of) +{ + Muxer *mux = av_mallocz(sizeof(*mux)); + + if (!mux) + return AVERROR(ENOMEM); + + of->mux = mux; + + return 0; +} diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 47e8b9b7bd..fff230eafc 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2921,6 +2921,12 @@ loop_end: exit_program(1); } + err = of_muxer_init(of); + if (err < 0) { + av_log(NULL, AV_LOG_FATAL, "Error initializing internal muxing state\n"); + exit_program(1); + } + return 0; } From patchwork Mon Apr 4 11:29:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35167 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1912506pzb; Mon, 4 Apr 2022 04:34:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzqvXIlLwt0vTyDI3umfWsMO9ADMmdznaJZ0SW9S+R28yb3EIXAkFy2na0cYrm0+HSp0PZh X-Received: by 2002:a17:906:99c5:b0:6df:8215:4ccd with SMTP id s5-20020a17090699c500b006df82154ccdmr10319442ejn.684.1649072067421; Mon, 04 Apr 2022 04:34:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072067; cv=none; d=google.com; s=arc-20160816; b=jMTiJB032u+Eail+iX3SXr1H7HGqgL1XzQg4luXgFoSWpItYJrCMpY2wyGselzmJ6G 5RfYP6YWdSvc6jy1oAvc1PWMhf+Po+EXoUBjiDjQHCGZSeilixEEe6XgNiQPipfq7/ge NY4/Da1T6qRYpMiXtQ17ZZzMhXqNrcDLjMo3oqEdPA7ibIje5MicSzrzHh1BqcPg7XUa LxH9qMeORxAgBN+LNGXTll/ocw6lIbCcgsa/Y0gT9GGWNSL/QNn76/lUNqoTSJFE6moS DQZ/5Ko8PqM+ihUbA8coCVO4ebT1iPZcQATcA0mTQt8njhhryXzWQuXAh7zTf7k2rsKK PvBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=6EBiTk8XwbVv3NmD/y4ti2iT3Sr4cggEiJ9ZZ0M85VU=; b=Yr+a84uvtHiWBuxFTSNGmuEbN3rGmQiU5oTpkPHzJ9OdUd4Q3Bsz69OaNNWF/FTuJ8 JxE0sBspcVxPZrCirCod10iilI7WpfzFtMp2JzVp23894O2EgrXfIfbhGq9TpNKu2XNh wLyvUWLkSSaoTbhMkR01m3KkvIsdIXMaq1BdVqYPLvzwUbYMjvEbM9yAKoT4i0fsLAYt m6ONTP0ZsCHVK3mBQT//gm2Fe9hdipjckPySSKAnjlEdhfCHv1tHgMpdui9wXlvqWQib 9sIZQ28hI70tRsTX7yviDE92+y5rko+8/7AbXTsw7hM520aZJDFKM625F/ToWg0tQ+rq q8EA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id v1-20020aa7d9c1000000b0041cdc4992aasi400057eds.456.2022.04.04.04.34.27; Mon, 04 Apr 2022 04:34:27 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EA17768B261; Mon, 4 Apr 2022 14:32:34 +0300 (EEST) 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 75CAE68A8A0 for ; Mon, 4 Apr 2022 14:32:23 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id B29E224017C for ; Mon, 4 Apr 2022 13:32:22 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id aJQz0QI9tGZQ for ; Mon, 4 Apr 2022 13:32:20 +0200 (CEST) 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 4E10024056A for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id C1A7E3A09B5; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:29:59 +0200 Message-Id: <20220404113037.13070-12-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 11/49] fftools/ffmpeg: add a helper function to access output file size 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: PM0sinZdu2LS Stop accessing muxer internals from outside of ffmpeg_mux. --- fftools/ffmpeg.c | 10 +--------- fftools/ffmpeg.h | 1 + fftools/ffmpeg_mux.c | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index a85ed18b08..52dffa57bd 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1513,8 +1513,7 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti { AVBPrint buf, buf_script; OutputStream *ost; - AVFormatContext *oc; - int64_t total_size; + int64_t total_size = of_filesize(output_files[0]); AVCodecContext *enc; int frame_number, vid, i; double bitrate; @@ -1543,13 +1542,6 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti t = (cur_time-timer_start) / 1000000.0; - - oc = output_files[0]->ctx; - - total_size = avio_size(oc->pb); - if (total_size <= 0) // FIXME improve avio_size() so it works with non seekable output too - total_size = avio_tell(oc->pb); - vid = 0; av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC); av_bprint_init(&buf_script, 0, AV_BPRINT_SIZE_AUTOMATIC); diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 3db03e0cfe..fa7b202ae6 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -698,5 +698,6 @@ void of_close(OutputFile **pof); void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue); +int64_t of_filesize(OutputFile *of); #endif /* FFTOOLS_FFMPEG_H */ diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 84c94e46be..930a7bdfe7 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -346,3 +346,17 @@ int of_muxer_init(OutputFile *of) return 0; } + +int64_t of_filesize(OutputFile *of) +{ + AVIOContext *pb = of->ctx->pb; + int64_t ret = -1; + + if (pb) { + ret = avio_size(pb); + if (ret <= 0) // FIXME improve avio_size() so it works with non seekable output too + ret = avio_tell(pb); + } + + return ret; +} From patchwork Mon Apr 4 11:30:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35181 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1913787pzb; Mon, 4 Apr 2022 04:37:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzZAjBCCs0uPbJXxILzKZCKlog0IBkuylfRvyHMLEkEjS4AHcuDVVFpfYnubbexiEssKDpT X-Received: by 2002:a05:6402:4315:b0:419:2e38:ab8f with SMTP id m21-20020a056402431500b004192e38ab8fmr32722278edc.250.1649072220841; Mon, 04 Apr 2022 04:37:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072220; cv=none; d=google.com; s=arc-20160816; b=vFfE4jGrHyH+jl/Z6QztIEuIJ/aCXWS59MvYXQ1H9Jm0WodyG7HXaFFIMrfqTIIVmi QpBNnZAyLq66otXhk83hSDiWIomp6JBJaqL04IQ5HvLf6tx11Sngz/Ao0lAlOjajnSO1 fXigDprrBdZYybY8CshblSV+J5RJut0VyJ6lneiT4J6xFOow6SQV2SR1hzxgYZFdZvQJ Gx+NJ8R0nfEkbVJCFn34C92mFd6MC4K8VIEtcsz1ewHTy9dIPgOvgAHyQza+rPyKyRua z6cYxLACmQe74V9yutfua2vzYsn7t9gptOyT8Ie+G2dhkaYtcD9OkyrD5VyS0ZAsF12q N5GA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=Sg26YoNJ+QKiu3qL1HjjCNYphIHOJIpL0UKtWCDxV2w=; b=UCmKhTtpArfi32uOE+Yt5NToeSe46z13E0icjQkzmp73czXcjh3LzrAofEkIRvMJT3 nsrr8o60ogU56uSiSw9RAq2w/37/QdEUrok7PBEL/ejsjc3bxaRl0CPdoTZaEkbb9hXM CTRQ0HeGLgZUwSTuLyrpMqnlvFjTdPX3vg36E676xEF7QGbBgWKV2rpTT9/8Ugw5Ajgj UMp910Pe+OtZsSEi2oc/xjjnXOYC26TgImjCLepMwXkOR8sa9AN3dhpiuWfZjYup2Zim sO5HTjKM5lLlejDD+FYzZ1Dmqtz8spHmqEXbDpY772u1MBctOjZRHSJrZ6p/dwnJ+log 7VXQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id f9-20020a50bf09000000b00418c2b5bd87si6172503edk.105.2022.04.04.04.37.00; Mon, 04 Apr 2022 04:37:00 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3C54D68B331; Mon, 4 Apr 2022 14:32:49 +0300 (EEST) 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 973B168AEFE for ; Mon, 4 Apr 2022 14:32:22 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 0C37024052D for ; Mon, 4 Apr 2022 13:32:19 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id RP16Jv7L9p76 for ; Mon, 4 Apr 2022 13:32:18 +0200 (CEST) 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 EE3DE240555 for ; Mon, 4 Apr 2022 13:32:14 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id C52913A0A30; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:00 +0200 Message-Id: <20220404113037.13070-13-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 12/49] fftools/ffmpeg: fix the type of limit_filesize 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Vph4/kQVcjUS The option is parsed as INT64 (signed). It is also compared to the output of avio_tell(), which is also int64_t. --- fftools/ffmpeg.h | 4 ++-- fftools/ffmpeg_opt.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index fa7b202ae6..d76f4266d6 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -147,7 +147,7 @@ typedef struct OptionsContext { int64_t recording_time; int64_t stop_time; - uint64_t limit_filesize; + int64_t limit_filesize; float mux_preload; float mux_max_delay; int shortest; @@ -589,7 +589,7 @@ typedef struct OutputFile { int ost_index; /* index of the first stream in output_streams */ int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units - uint64_t limit_filesize; /* filesize limit expressed in bytes */ + int64_t limit_filesize; /* filesize limit expressed in bytes */ int shortest; } OutputFile; diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index fff230eafc..4e601f1cf8 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -228,7 +228,7 @@ static void init_options(OptionsContext *o) o->start_time = AV_NOPTS_VALUE; o->start_time_eof = AV_NOPTS_VALUE; o->recording_time = INT64_MAX; - o->limit_filesize = UINT64_MAX; + o->limit_filesize = INT64_MAX; o->chapters_input_file = INT_MAX; o->accurate_seek = 1; o->thread_queue_size = -1; From patchwork Mon Apr 4 11:30:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35162 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1911876pzb; Mon, 4 Apr 2022 04:33:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyiyXo/UpOyUL/GnXawbNonTDvzJtrv73lEwtLkimzKw7NAaA77IxBvzfvFMejk5RJUYZYz X-Received: by 2002:a17:907:6d94:b0:6e7:fe12:6e9a with SMTP id sb20-20020a1709076d9400b006e7fe126e9amr1015192ejc.584.1649072001332; Mon, 04 Apr 2022 04:33:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072001; cv=none; d=google.com; s=arc-20160816; b=dDGiqH5CArOrk64ZvFfeyyiWEaG8/QcdYHNXhtZY9yf5jx4vQsmbkb/ns1BrwjvbRs nqucb+vbxPT5KFhDfG2MPBGiw8WXxXti3/aInG5OqIS+Qyuun5kQi31hMgoBsndOWItM ccOi3wTZ6Z2lGOxTZBVT0yQbHzY2uWswi/jZUZfkW2N9g5eZcNDrKx/pSB5xZSLJQ2a4 dA64djvlkyI2r1VS8upirJVXELqh3CiM41Nx3y7T4UsEq+E0GuEDg3iAMTLzEH6rtusg nsviUN+ZciPFeIBehLCLjptvTZKvhVnxY91KadKCmKVYHwN699ErqcQ7DOkAZ4+rNSIn NFCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=mJF4RjokMhQmJtiBLdy1Pqjq5j86v/FBL2UHxbxw0kQ=; b=eWEMqgwaMm1rTCNmYA5OzX6ZEA4ecFXRQDiM2BT1eF5scPLnrKSYDUiJylWT4e58R7 ursjMSURmMPrwdEZGokmLCVoqysHcQyNHOZnf3f6NEhE/1QjZuwe1kLGiuLgDpUO1v22 fF4IEiLucDdHYPKz45toXfjj0EBiA24QeiMQnG4+vzhESifDyYb+4sJ4SOytBuqN0kva M0edeOmJ9TTkHK+VzSNgeemPcRWKJTjTHeWPUs11iT0wWyycLsWROkozT3s42N39LPKs OIrSkwAUs6v7HGoPRKUdUob5YRKfhJyFK6VMhKbmp+PvF15Xv9R34pM28WlwtnlXo+i+ /emg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id s16-20020a170906501000b006df76385bdasi6504972ejj.122.2022.04.04.04.33.21; Mon, 04 Apr 2022 04:33:21 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9A6EB68B120; Mon, 4 Apr 2022 14:32:28 +0300 (EEST) 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 2FD65680602 for ; Mon, 4 Apr 2022 14:32:23 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id B48E2240555 for ; Mon, 4 Apr 2022 13:32:19 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id XuUNjfVYXL9n for ; Mon, 4 Apr 2022 13:32:19 +0200 (CEST) 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 4E7AB240590 for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id C89753A0A3C; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:01 +0200 Message-Id: <20220404113037.13070-14-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 13/49] fftools/ffmpeg: refactor limiting output file size with -fs 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 5IC0v+UPrt7S Move the file size checking code to ffmpeg_mux. Use the recently introduced of_filesize(), making this code consistent with the size shown by print_report(). --- fftools/ffmpeg.c | 4 +--- fftools/ffmpeg.h | 4 ++-- fftools/ffmpeg_mux.c | 11 ++++++++++- fftools/ffmpeg_opt.c | 3 +-- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 52dffa57bd..05e14c42c5 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3508,10 +3508,8 @@ static int need_output(void) for (i = 0; i < nb_output_streams; i++) { OutputStream *ost = output_streams[i]; OutputFile *of = output_files[ost->file_index]; - AVFormatContext *os = output_files[ost->file_index]->ctx; - if (ost->finished || - (os->pb && avio_tell(os->pb) >= of->limit_filesize)) + if (ost->finished || of_finished(of)) continue; if (ost->frame_number >= ost->max_frames) { int j; diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index d76f4266d6..c3b90ac9a2 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -589,7 +589,6 @@ typedef struct OutputFile { int ost_index; /* index of the first stream in output_streams */ int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units - int64_t limit_filesize; /* filesize limit expressed in bytes */ int shortest; } OutputFile; @@ -690,7 +689,7 @@ int hw_device_setup_for_filter(FilterGraph *fg); int hwaccel_decode_init(AVCodecContext *avctx); -int of_muxer_init(OutputFile *of); +int of_muxer_init(OutputFile *of, int64_t limit_filesize); /* open the muxer when all the streams are initialized */ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); @@ -698,6 +697,7 @@ void of_close(OutputFile **pof); void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue); +int of_finished(OutputFile *of); int64_t of_filesize(OutputFile *of); #endif /* FFTOOLS_FFMPEG_H */ diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 930a7bdfe7..78bb338bce 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -33,6 +33,8 @@ #include "libavformat/avio.h" struct Muxer { + /* filesize limit expressed in bytes */ + int64_t limit_filesize; int header_written; }; @@ -335,7 +337,7 @@ void of_close(OutputFile **pof) av_freep(pof); } -int of_muxer_init(OutputFile *of) +int of_muxer_init(OutputFile *of, int64_t limit_filesize) { Muxer *mux = av_mallocz(sizeof(*mux)); @@ -344,9 +346,16 @@ int of_muxer_init(OutputFile *of) of->mux = mux; + mux->limit_filesize = limit_filesize; + return 0; } +int of_finished(OutputFile *of) +{ + return of_filesize(of) >= of->mux->limit_filesize; +} + int64_t of_filesize(OutputFile *of) { AVIOContext *pb = of->ctx->pb; diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 4e601f1cf8..4a41ebc736 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2318,7 +2318,6 @@ static int open_output_file(OptionsContext *o, const char *filename) of->ost_index = nb_output_streams; of->recording_time = o->recording_time; of->start_time = o->start_time; - of->limit_filesize = o->limit_filesize; of->shortest = o->shortest; av_dict_copy(&of->opts, o->g->format_opts, 0); @@ -2921,7 +2920,7 @@ loop_end: exit_program(1); } - err = of_muxer_init(of); + err = of_muxer_init(of, o->limit_filesize); if (err < 0) { av_log(NULL, AV_LOG_FATAL, "Error initializing internal muxing state\n"); exit_program(1); From patchwork Mon Apr 4 11:30:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35163 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1911982pzb; Mon, 4 Apr 2022 04:33:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzOjfnpJi19Zw5AEBDGTjhW0VTsUqJ2gKLTWKfW2aE9hYag3yStjGpL5HEPB2CkEk9yaxTk X-Received: by 2002:a17:907:7d9e:b0:6df:9fe8:856a with SMTP id oz30-20020a1709077d9e00b006df9fe8856amr10219219ejc.373.1649072012219; Mon, 04 Apr 2022 04:33:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072012; cv=none; d=google.com; s=arc-20160816; b=OdRKQvj7jhbTnzV78dmPrOwU2k/hWaDOOlwX7X4dTKAR2pGcVignaZ+uAfrjVsRSxy YPyFo7bl1AEiAg4cDOcO+xUN89ZG9KuxYH5PlfwOaMe5mUhqhpBlUbFUhHurMgw2aTPE JHgT1dIAJ6tphherYocPJZpQThEaYZqFFk/6IIUYLPKadumTC4tHI6JUGT9y6VljfgYi AXuuGwQIq9nm+w2U2wX7e+yNmIEUXLUitvIpdjPFI2jmD7foN9l/sqIr6Jyvtp2lWoaI HOMMrfEIwf08MP/troUCQBIFPhGgbPqfbbQc5ahAWliZ1dAA+a76cZGLzzdaePTnL5xB 5y+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=x23T0Akd5366/Jj5x7kWbPVYfIr3iOiulwzn5Q4YDc8=; b=fvGGaxUXlMZntAVyU6aU7hJ/gUj+atTGzNh7zraMNPTX6GEzYicWM3Ovv66z+2+6Qj uZlUW7WEuYt1oWZvvOE62oIEgjRjCv9arBjGnTTuJBqWmBSdOUM+ZaUdklNlq65qWMSb u0Cif9jmd3/Uwe5GPycVgz6JySppra/Oi2L94f6BrTjvrACh1BqPjSAGUsK/I+7jaKsT angDZDm97Zk8WYClnJWvTF8SSExPG5U8uLuNYKRBmwBJzneXHMDtMpl9MuHzgKNs1se+ gHmbw3m2RiOkYdqZXPScrFKQDQ+pjYrMm/tGmpVFf552BnwwHVnwoKBrYodIoCV/8VED MLrA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ca23-20020aa7cd77000000b00418c2b5bf40si5999137edb.546.2022.04.04.04.33.31; Mon, 04 Apr 2022 04:33:32 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8DEE568B2B1; Mon, 4 Apr 2022 14:32:29 +0300 (EEST) 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 31B6068B0F6 for ; Mon, 4 Apr 2022 14:32:23 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 3207D240590 for ; Mon, 4 Apr 2022 13:32:20 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id LJhq7RIp68Me for ; Mon, 4 Apr 2022 13:32:19 +0200 (CEST) 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 4D90F24055A for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id CC2733A0A85; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:02 +0200 Message-Id: <20220404113037.13070-15-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 14/49] fftools/ffmpeg: set want_sdp when initializing the muxer 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: rbeCEmENK/Jd Allows making the variable local to ffmpeg_mux. --- fftools/ffmpeg.c | 9 +-------- fftools/ffmpeg.h | 1 - fftools/ffmpeg_mux.c | 5 +++++ 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 05e14c42c5..688e1c7e56 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -139,8 +139,6 @@ static int nb_frames_drop = 0; static int64_t decode_error_stat[2]; unsigned nb_output_dumped = 0; -int want_sdp = 1; - static BenchmarkTimeStamps current_time; AVIOContext *progress_avio = NULL; @@ -4563,7 +4561,7 @@ static void log_callback_null(void *ptr, int level, const char *fmt, va_list vl) int main(int argc, char **argv) { - int i, ret; + int ret; BenchmarkTimeStamps ti; init_dynload(); @@ -4606,11 +4604,6 @@ int main(int argc, char **argv) exit_program(1); } - for (i = 0; i < nb_output_files; i++) { - if (strcmp(output_files[i]->format->name, "rtp")) - want_sdp = 0; - } - current_time = ti = get_benchmark_time_stamps(); if (transcode() < 0) exit_program(1); diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index c3b90ac9a2..d0ddaeaeff 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -649,7 +649,6 @@ extern char *qsv_device; #endif extern HWDevice *filter_hw_device; -extern int want_sdp; extern unsigned nb_output_dumped; extern int main_return_code; diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 78bb338bce..f1ec5881c4 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -38,6 +38,8 @@ struct Muxer { int header_written; }; +static int want_sdp = 1; + static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others) { int i; @@ -348,6 +350,9 @@ int of_muxer_init(OutputFile *of, int64_t limit_filesize) mux->limit_filesize = limit_filesize; + if (strcmp(of->format->name, "rtp")) + want_sdp = 0; + return 0; } From patchwork Mon Apr 4 11:30:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35165 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1912331pzb; Mon, 4 Apr 2022 04:34:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy47vGX+O41Mo1p8Lekofbf46yE5A2x0upGqfGHkSbVgQbwGuk+CFQihDlcecVtrTMA/82J X-Received: by 2002:a17:906:2a0c:b0:6df:ec76:af8c with SMTP id j12-20020a1709062a0c00b006dfec76af8cmr10176825eje.269.1649072046637; Mon, 04 Apr 2022 04:34:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072046; cv=none; d=google.com; s=arc-20160816; b=uoRp9hVwXmZcE/fSIwGNJZBtuckX12z6HMCnMWhKe1FKIq0ZYp+tfhfE+yzrJbz25i rmQ5oPENsDzqVRb15gn+JWbPopHBZEu4RQJ3RV/aCLftRgCyp01dYMDg60wLZdP0DWQb f5SUb2IY96ktTpG3wX7v6l/PcFEy4Qjcqoz4J1IGEotHvwvXI3Jn+scqATEGzOsuDbNc A2NYb12SxwzM3ne5aI93emRZ6grLznGBU1VoxWhsv9OKpVjAPFfdTCfkKoBUZyRR4KW9 gRewOB2LPwY6gj39cFKNYFdBS0bXwLAK6a74gZWRLJRJIJslpRWVI13dK0GWLylisxDv 1FuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=FkaF4IMaXJ+HytzEpOHFQFsrgVhCXAVtKzDZdFoFxJA=; b=p3qK8cAqU+EKOZVAt/qNySP9qTsZq+i9xZK/nONYbVfXVZrdZgMw7V6clk6QRe+i60 yktp5xaBiMsF6VHU7rLeNPkLRr5bi7xWv32QPr2YFXoeYR9H0V3ppgyjRDuZ/0r1sbQ0 3aCeIf2fOok8lGd970Rfv8rmX0K0Q6eitoMxWfHQNMqT8zIEYit4Ky0gUWqPjE0WFsky lVTr/mao7OmV2a4pbgDelMjLhYTYxS+pNI9vwa1oSXGtSXIJe6hBGrxgrIE2ls6P+Hre muDqVqP5XAD4Qgn93trbmv8ft3oYc9sFZ/KBoZQKj/a1oejLj9OdaIIrAFOWohYFCEbI E2lg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id g9-20020a170906394900b006df76385c08si5964965eje.168.2022.04.04.04.34.06; Mon, 04 Apr 2022 04:34:06 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0FD8768B26D; Mon, 4 Apr 2022 14:32:33 +0300 (EEST) 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 69F0A68B181 for ; Mon, 4 Apr 2022 14:32:23 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 4305924055A for ; Mon, 4 Apr 2022 13:32:21 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id clek-FZcmshO for ; Mon, 4 Apr 2022 13:32:20 +0200 (CEST) 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 4F7D2240593 for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id CFB7E3A0A89; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:03 +0200 Message-Id: <20220404113037.13070-16-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 15/49] fftools/ffmpeg: write the header for stream-less outputs when initializing the muxer 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: /EItVVjkSemA There is no reason to delay this. --- fftools/ffmpeg.c | 11 ----------- fftools/ffmpeg_mux.c | 7 +++++++ 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 688e1c7e56..e862a57215 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3320,7 +3320,6 @@ static void report_new_stream(int input_index, AVPacket *pkt) static int transcode_init(void) { int ret = 0, i, j, k; - AVFormatContext *oc; OutputStream *ost; InputStream *ist; char error[1024] = {0}; @@ -3394,16 +3393,6 @@ static int transcode_init(void) } } - /* write headers for files with no streams */ - for (i = 0; i < nb_output_files; i++) { - oc = output_files[i]->ctx; - if (output_files[i]->format->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) { - ret = of_check_init(output_files[i]); - if (ret < 0) - goto dump_format; - } - } - dump_format: /* dump the stream mapping */ av_log(NULL, AV_LOG_INFO, "Stream mapping:\n"); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index f1ec5881c4..2a95b2602d 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -353,6 +353,13 @@ int of_muxer_init(OutputFile *of, int64_t limit_filesize) if (strcmp(of->format->name, "rtp")) want_sdp = 0; + /* write the header for files with no streams */ + if (of->format->flags & AVFMT_NOSTREAMS && of->ctx->nb_streams == 0) { + int ret = of_check_init(of); + if (ret < 0) + return ret; + } + return 0; } From patchwork Mon Apr 4 11:30:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35157 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1912089pzb; Mon, 4 Apr 2022 04:33:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwwIWQekKOE9im28KMI+5oLl9+bRDTBGCECjM1Som8PcrFUkSW83KPLMlyQ28G5Da/Hs1A5 X-Received: by 2002:a05:6402:4402:b0:41c:d169:d8ea with SMTP id y2-20020a056402440200b0041cd169d8eamr4335736eda.303.1649072022657; Mon, 04 Apr 2022 04:33:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072022; cv=none; d=google.com; s=arc-20160816; b=rBpoz6bT/pwnPEvFEZFW5pu9TDtZPywLSjduPkf+F9WG4Y41ISmB9I/6r5yJLCcMkx hhG7YOx8gpds8wjzTHfZabrKIknjoyfQXHneuftG6lkPZk8yNnWRFsiTiREC1e83TjHl VGCl4KUawDvKNBdtaPENk7nBvfyj41nvgsxNUJvCs9vTmNlf7lCyfpFA6A08AcpOCVZa Uf4/wcu5nZJYzL6Kke6j3uYb2oR3axtz54gHlCkzHAnpHI39tG4Su8K/t3HHWfdoTLZm gq0TcVDi+ZhZHE5E5COJzcqWZTR0hJgNoE0kY1VTWAOO3sLVfF/bXZyUkG140Lk+cjB6 H5gg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=nQt5lE8ws0x9Bdgkli1DDZis3ZfkuYkkl12vRIL7VF0=; b=YaE1RtCp2FDGmWKBoOzCK+bpEK2pBNl491zBYhYbrXW0m/3TwyIWPLSZp7E9tl42AI POX1ZV1ppAD0w+GfCwTqtIzZ+8IdzOYQgpWPLt7eLtQmDSbuRmK9kro5b4lQbs7yCtiD 7t4fVdqzhD2AW/L6S4rpXIs4kLOBKrVxvwXqv5V3QkJjA4L0RF8QbCTpTM5pvio9Rawg JohxxhzAF8G5ktG4N9dlGwVofZfmzlU/0NScHPCVWsE8T9G6aAK/bbVXrGxvJdBn0jTz wn+XvAsIDmF8G8szCpVfHYxnS3qggC3Yg74GGvP8lioYyiyi74441Rqa8+euXzmnGS5S 7DEg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id k4-20020a1709062a4400b006dfe65cdeefsi6125612eje.87.2022.04.04.04.33.42; Mon, 04 Apr 2022 04:33:42 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 89C2B68B2BA; Mon, 4 Apr 2022 14:32:30 +0300 (EEST) 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 5D87B68B160 for ; Mon, 4 Apr 2022 14:32:23 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id BC07F240512 for ; Mon, 4 Apr 2022 13:32:20 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 13A4af6JZF1R for ; Mon, 4 Apr 2022 13:32:20 +0200 (CEST) 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 502242405F0 for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id D347E3A0AD1; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:04 +0200 Message-Id: <20220404113037.13070-17-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 16/49] fftools/ffmpeg: move closing the file into of_write_trailer() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: inl5hTJOXUhF The current code postpones closing the files until after printing the final report, which accesses the output file size. Deal with this by storing the final file size before closing the file. --- fftools/ffmpeg.c | 13 ------------- fftools/ffmpeg_mux.c | 16 +++++++++++++++- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index e862a57215..252b14a710 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -4358,7 +4358,6 @@ static int transcode_step(void) static int transcode(void) { int ret, i; - AVFormatContext *os; OutputStream *ost; InputStream *ist; int64_t timer_start; @@ -4427,18 +4426,6 @@ static int transcode(void) /* dump report by using the first video and audio streams */ print_report(1, timer_start, av_gettime_relative()); - /* close the output files */ - for (i = 0; i < nb_output_files; i++) { - os = output_files[i]->ctx; - if (os && os->oformat && !(os->oformat->flags & AVFMT_NOFILE)) { - if ((ret = avio_closep(&os->pb)) < 0) { - av_log(NULL, AV_LOG_ERROR, "Error closing file %s: %s\n", os->url, av_err2str(ret)); - if (exit_on_error) - exit_program(1); - } - } - } - /* close each encoder */ for (i = 0; i < nb_output_streams; i++) { ost = output_streams[i]; diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 2a95b2602d..927d9228b7 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -35,6 +35,7 @@ struct Muxer { /* filesize limit expressed in bytes */ int64_t limit_filesize; + int64_t final_filesize; int header_written; }; @@ -317,6 +318,17 @@ int of_write_trailer(OutputFile *of) return ret; } + of->mux->final_filesize = of_filesize(of); + + if (!(of->format->flags & AVFMT_NOFILE)) { + ret = avio_closep(&of->ctx->pb); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Error closing file %s: %s\n", + of->ctx->url, av_err2str(ret)); + return ret; + } + } + return 0; } @@ -373,7 +385,9 @@ int64_t of_filesize(OutputFile *of) AVIOContext *pb = of->ctx->pb; int64_t ret = -1; - if (pb) { + if (of->mux->final_filesize) + ret = of->mux->final_filesize; + else if (pb) { ret = avio_size(pb); if (ret <= 0) // FIXME improve avio_size() so it works with non seekable output too ret = avio_tell(pb); From patchwork Mon Apr 4 11:30:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35170 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1912732pzb; Mon, 4 Apr 2022 04:34:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwrMqTLZ1BKi1zhW+FRE0XULxLFyeAnTODF8PVURwQg+AOPG6qnCPXgShz9uWqh1bui3cEN X-Received: by 2002:aa7:d398:0:b0:419:d011:fe8b with SMTP id x24-20020aa7d398000000b00419d011fe8bmr32980189edq.168.1649072098426; Mon, 04 Apr 2022 04:34:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072098; cv=none; d=google.com; s=arc-20160816; b=TnKvKj1XBqqz/Pw6FrzKtd/yeKCFzNXCgI6As2capZxC6fIE3rE3YO+l9smgGZW8eF n305Dr3iDkXXBS7gfLvFSpTaAKiigXUm30pjL4LakKNDb81jds/8CHZnonpiYu5WxBy7 +43xK849gw82KPnKtFMvtbZRGW4sBwM/iMqjfBMy/hvzJ3r2l1Kbhlzy4f1RMOTghmN3 w0kJbswbI1H4W8zVaswEua8CZggw+b8xIhbrHV2vf7Fet5Mv4xoI5xrXjKfbrkIWhaKf k647EImrnoXx9E5+GOhnseyBn+LebltMp2qXbXGMl/bV5P6Vs5jyditCgobJDrUlJZXM Gk5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=ExT1Gdw2lXpwN+Mjk+X8BejHOY9AaPEvshkPbRsiEF4=; b=RPl10TyIRygkAqm6/bpm9UMnzrkQD5tiX3RNi3iOQCu4D0UXHiORizknxHHP3HHvuc TXksJxNn8Ck5cqUeHNxhOYZxBr6IwYzchy5PS+FYXyP242daDcv81Uv1GruCvrURGrkV aHIlJCzrOP8M36pYiHvtOC+RAhkNhT10gqICxeQuDw1stZMcmMcz7Su1t3jmIjpdfmNb MxcMOXF1e9645xCQdNwpHAhZNScr96Z7m6/JV2GzrJZdgdiLprtwCcZdvJURXpZHnk07 P49B5YA3xy5z4NmwZGwnVN5B82HJGFZ8Pld3IZ66+Z8QwrWCMgw0x0O8vsDQIqy5fJRv BnIg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id g13-20020a056402090d00b0041b6412d2cesi7912237edz.171.2022.04.04.04.34.58; Mon, 04 Apr 2022 04:34:58 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0637768B2E1; Mon, 4 Apr 2022 14:32:38 +0300 (EEST) 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 4A5AC68B0FD for ; Mon, 4 Apr 2022 14:32:25 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id C36D4240179 for ; Mon, 4 Apr 2022 13:32:24 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id qFSJ06HX_Qd7 for ; Mon, 4 Apr 2022 13:32:24 +0200 (CEST) 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 5AD1B240688 for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id D6E963A0B48; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:05 +0200 Message-Id: <20220404113037.13070-18-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 17/49] fftools/ffmpeg: refactor the code checking for bitexact output 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: G1q6QWxbuZZO Figure out earlier whether the output stream/file should be bitexact and store this information in a flag in OutputFile/OutputStream. Stop accessing the muxer in set_encoder_id(), which will become forbidden in future commits. --- fftools/ffmpeg.c | 21 +-------------------- fftools/ffmpeg.h | 2 ++ fftools/ffmpeg_opt.c | 27 ++++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 252b14a710..dfd57d2d03 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2870,37 +2870,18 @@ static int init_output_stream_streamcopy(OutputStream *ost) static void set_encoder_id(OutputFile *of, OutputStream *ost) { - const AVDictionaryEntry *e; - uint8_t *encoder_string; int encoder_string_len; - int format_flags = 0; - int codec_flags = ost->enc_ctx->flags; if (av_dict_get(ost->st->metadata, "encoder", NULL, 0)) return; - e = av_dict_get(of->opts, "fflags", NULL, 0); - if (e) { - const AVOption *o = av_opt_find(of->ctx, "fflags", NULL, 0, 0); - if (!o) - return; - av_opt_eval_flags(of->ctx, o, e->value, &format_flags); - } - e = av_dict_get(ost->encoder_opts, "flags", NULL, 0); - if (e) { - const AVOption *o = av_opt_find(ost->enc_ctx, "flags", NULL, 0, 0); - if (!o) - return; - av_opt_eval_flags(ost->enc_ctx, o, e->value, &codec_flags); - } - encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(ost->enc->name) + 2; encoder_string = av_mallocz(encoder_string_len); if (!encoder_string) exit_program(1); - if (!(format_flags & AVFMT_FLAG_BITEXACT) && !(codec_flags & AV_CODEC_FLAG_BITEXACT)) + if (!of->bitexact && !ost->bitexact) av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len); else av_strlcpy(encoder_string, "Lavc ", encoder_string_len); diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index d0ddaeaeff..ec0d454fcc 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -493,6 +493,7 @@ typedef struct OutputStream { int top_field_first; int rotate_overridden; int autoscale; + int bitexact; int bits_per_raw_sample; double rotate_override_value; @@ -591,6 +592,7 @@ typedef struct OutputFile { int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units int shortest; + int bitexact; } OutputFile; extern InputStream **input_streams; diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 4a41ebc736..780c11903b 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -1437,6 +1437,22 @@ static int choose_encoder(OptionsContext *o, AVFormatContext *s, OutputStream *o return 0; } +static int check_opt_bitexact(void *ctx, const AVDictionary *opts, + const char *opt_name, int flag) +{ + const AVDictionaryEntry *e = av_dict_get(opts, opt_name, NULL, 0); + + if (e) { + const AVOption *o = av_opt_find(ctx, opt_name, NULL, 0, 0); + int val = 0; + if (!o) + return 0; + av_opt_eval_flags(ctx, o, e->value, &val); + return !!(val & flag); + } + return 0; +} + static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, enum AVMediaType type, int source_index) { OutputStream *ost; @@ -1529,8 +1545,13 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e } - if (o->bitexact) + if (o->bitexact) { ost->enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT; + ost->bitexact = 1; + } else { + ost->bitexact = check_opt_bitexact(ost->enc_ctx, ost->encoder_opts, "flags", + AV_CODEC_FLAG_BITEXACT); + } MATCH_PER_STREAM_OPT(time_bases, str, time_base, oc, st); if (time_base) { @@ -2339,6 +2360,10 @@ static int open_output_file(OptionsContext *o, const char *filename) if (o->bitexact) { oc->flags |= AVFMT_FLAG_BITEXACT; + of->bitexact = 1; + } else { + of->bitexact = check_opt_bitexact(oc, of->opts, "fflags", + AVFMT_FLAG_BITEXACT); } /* create streams for all unlabeled output pads */ From patchwork Mon Apr 4 11:30:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35173 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1913021pzb; Mon, 4 Apr 2022 04:35:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxhDA0WSqxKffXBxU+vLw6XOU1XhiyFmr/X9HPPrkLfyN6kap3biNPIvKyuvfzI/QTvUtLz X-Received: by 2002:a50:d64f:0:b0:418:f142:5d1c with SMTP id c15-20020a50d64f000000b00418f1425d1cmr32534314edj.192.1649072132309; Mon, 04 Apr 2022 04:35:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072132; cv=none; d=google.com; s=arc-20160816; b=coe06yDz9z5ICcoeocp0HooO7jAlZRYUTsfOF+vpBbExoZ56v3veYugsbQxoizuKqG kfISusOQmgUmLTyh1aydMSA1bCxnrFi0oyDXy1IMES8UPSby5BMrJKNXbvM5tGqVLbKK C2mINpEotO+uTj/VSu/po+WHHCvAkMBVe1MZ7JHHfeNCm2jOkPX18RcYiQxblUruUbIG 8d737kDTsbBus39xSPnm9RbCFD+re8GyIETrrLDAc/P9B3mfDb4JZ+77lCLE2+QCYPMi LpPbrJ+hAn2PnF4dlQPkLI3c0Iwddm1EVwM/vPryJOsjqmRTBNNtWBaFziUxmkYl+XfG crfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=MI9K3gFweuq9mS1DIzI/mowm1cc0ToVNfqrKg/BeXFc=; b=0Q/BQkAO8t3KJzRjFmTA1RDGH58pMozp5lY6ECZM8hJmrsv+geC+3VLhhU0z/Q1OH2 H4DkiomCblU04Ie1lXJ7gzpGAKnESh8UalQtaA12ORcKiWr0TX34VwiSENEZ0g+S3QPM EPYnbm9I2gLHApe3GLCvg4w8ybozM+cOdylQ150gBraMUiDljwwjjl6FzMQRW96mDuJP k03B0cAg33/KrU5Pnr8zDDEiugEAhXx+yBOI3IV0W+1T5IxfzHRrfsQK2G6RKKI/d4sX RCbgqJ8gAMEgxwVws/nYfnvb7euoZzeXiYaS2hruFwd8HYqd2/vyVbzq23sZRdf+D2pO DOtA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id qx23-20020a170906fcd700b006df76385d9esi6346749ejb.574.2022.04.04.04.35.32; Mon, 04 Apr 2022 04:35:32 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3F95168B2F9; Mon, 4 Apr 2022 14:32:41 +0300 (EEST) 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 D082668B24D for ; Mon, 4 Apr 2022 14:32:26 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 69D73240179 for ; Mon, 4 Apr 2022 13:32:26 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id imeLCcV3FY7h for ; Mon, 4 Apr 2022 13:32:25 +0200 (CEST) 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 7902924068D for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id DA7AB3A0B49; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:06 +0200 Message-Id: <20220404113037.13070-19-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 18/49] fftools/ffmpeg: access output file chapters through a wrapper 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: TlKsp9Lz4WOH Avoid accessing the muxer context directly, as this will become forbidden in future commits. --- fftools/ffmpeg.c | 15 +++++++++------ fftools/ffmpeg.h | 2 ++ fftools/ffmpeg_mux.c | 7 +++++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index dfd57d2d03..b9ecbf4739 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2915,12 +2915,15 @@ static void parse_forced_key_frames(char *kf, OutputStream *ost, *next++ = 0; if (!memcmp(p, "chapters", 8)) { - - AVFormatContext *avf = output_files[ost->file_index]->ctx; + OutputFile *of = output_files[ost->file_index]; + AVChapter * const *ch; + unsigned int nb_ch; int j; - if (avf->nb_chapters > INT_MAX - size || - !(pts = av_realloc_f(pts, size += avf->nb_chapters - 1, + ch = of_get_chapters(of, &nb_ch); + + if (nb_ch > INT_MAX - size || + !(pts = av_realloc_f(pts, size += nb_ch - 1, sizeof(*pts)))) { av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n"); @@ -2929,8 +2932,8 @@ static void parse_forced_key_frames(char *kf, OutputStream *ost, t = p[8] ? parse_time_or_die("force_key_frames", p + 8, 1) : 0; t = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base); - for (j = 0; j < avf->nb_chapters; j++) { - AVChapter *c = avf->chapters[j]; + for (j = 0; j < nb_ch; j++) { + const AVChapter *c = ch[j]; av_assert1(index < size); pts[index++] = av_rescale_q(c->start, c->time_base, avctx->time_base) + t; diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index ec0d454fcc..8e34ae0702 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -700,5 +700,7 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue); int of_finished(OutputFile *of); int64_t of_filesize(OutputFile *of); +AVChapter * const * +of_get_chapters(OutputFile *of, unsigned int *nb_chapters); #endif /* FFTOOLS_FFMPEG_H */ diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 927d9228b7..9dd5e8b076 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -395,3 +395,10 @@ int64_t of_filesize(OutputFile *of) return ret; } + +AVChapter * const * +of_get_chapters(OutputFile *of, unsigned int *nb_chapters) +{ + *nb_chapters = of->ctx->nb_chapters; + return of->ctx->chapters; +} From patchwork Mon Apr 4 11:30:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35172 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1912928pzb; Mon, 4 Apr 2022 04:35:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz14zLXCzLfUFSxhwAhflUhsP02kWJHurtqDjlNgfIks1DjfJLWCmCokqrOadGLMjMuXM4Z X-Received: by 2002:a05:6402:27d4:b0:419:5105:f7e7 with SMTP id c20-20020a05640227d400b004195105f7e7mr32540535ede.356.1649072121233; Mon, 04 Apr 2022 04:35:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072121; cv=none; d=google.com; s=arc-20160816; b=0xh6FDBbKi+RSk0I6C7tkeUhkhB9FwHuGDUknNdQXBDCspyJUbvA8RkmL0CrSBScU+ bEVdkrrR0BQpat14M65dCqOTljnekM1rhz0O7/dgMYkHTLCpnRs+JE6RF0LubqrXA+87 LBYzcyWEEr1amOH8TBw85VseVMM9YOH9lgXdRCHEL0R4hcU4fCHRZ5F5rp0tqS+cKYft Y0WSHnQPKncgRVUVFwYwv6kPvTbsCwqnes5mBpZqiQjlCwMMb16dg6Em2Lt4EsAPIMSD CemTFVTGTZHzhkfW17cIIJ/PXfUhpoe5mGelvMvewBv/uGY0gBxIQUWxUO5dlXuU1piy o8Qg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=HhWbowo7J/Pm8egOuYYGRtiFDO54cUjhPxgouebbND4=; b=X66dvqycj188lhKoQWIPDJZtSkbvZ7Pt4hrbGT2L1r0UIP6oC6e28gDq8SVHsUtoJ1 3fSQ9cZ3RriF6l4AFc/yH4wucMOOIqWlqm/lOnu4xaAy9Q4fIzanq6i/vbeQjeMjGCwE wcvXOIo6mYLtma1+MpQ0KaO4agfSvK9+KDaoK8smu2wgcIAzgysJpu08fTlOcqyd/V3p PQc+SEB0NrXaTctlSHcEEaFmb5f8Nem3dCrbA7g/e85o8aAXDKaf8GTBonVMocpKnclR UnmK5+erZFBGBiJ+oo7e5LFRlWjKSFyJ4HmZKFq1+Wr0vM7fbcbI+HU4FX95d5Dp0TDh ngbw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id o9-20020a50c909000000b0041cc9514712si2118822edh.446.2022.04.04.04.35.20; Mon, 04 Apr 2022 04:35:21 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 33D2B68B2F2; Mon, 4 Apr 2022 14:32:40 +0300 (EEST) 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 56C7368AB48 for ; Mon, 4 Apr 2022 14:32:26 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id D5EA2240179 for ; Mon, 4 Apr 2022 13:32:25 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id A2oNKPMilt-J for ; Mon, 4 Apr 2022 13:32:25 +0200 (CEST) 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 78FC624068C for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id DDFA23A0B54; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:07 +0200 Message-Id: <20220404113037.13070-20-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 19/49] fftools/ffmpeg: do not log to the muxer context 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: F1Os3zi3TOj/ All other logging goes to NULL context. --- fftools/ffmpeg.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index b9ecbf4739..a9a62eb8c3 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2960,7 +2960,6 @@ static void init_encoder_time_base(OutputStream *ost, AVRational default_time_ba { InputStream *ist = get_input_stream(ost); AVCodecContext *enc_ctx = ost->enc_ctx; - AVFormatContext *oc; if (ost->enc_timebase.num > 0) { enc_ctx->time_base = ost->enc_timebase; @@ -2973,8 +2972,9 @@ static void init_encoder_time_base(OutputStream *ost, AVRational default_time_ba return; } - oc = output_files[ost->file_index]->ctx; - av_log(oc, AV_LOG_WARNING, "Input stream data not available, using default time base\n"); + av_log(NULL, AV_LOG_WARNING, + "Input stream data for output stream #%d:%d not available, " + "using default time base\n", ost->file_index, ost->index); } enc_ctx->time_base = default_time_base; @@ -2986,7 +2986,6 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) AVCodecContext *enc_ctx = ost->enc_ctx; AVCodecContext *dec_ctx = NULL; OutputFile *of = output_files[ost->file_index]; - AVFormatContext *oc = of->ctx; int ret; set_encoder_id(output_files[ost->file_index], ost); @@ -3049,8 +3048,9 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) if ( av_q2d(enc_ctx->time_base) < 0.001 && video_sync_method != VSYNC_PASSTHROUGH && (video_sync_method == VSYNC_CFR || video_sync_method == VSYNC_VSCFR || (video_sync_method == VSYNC_AUTO && !(of->format->flags & AVFMT_VARIABLE_FPS)))){ - av_log(oc, AV_LOG_WARNING, "Frame rate very high for a muxer not efficiently supporting it.\n" - "Please consider specifying a lower framerate, a different muxer or -vsync 2\n"); + av_log(NULL, AV_LOG_WARNING, "Frame rate in output stream #%d:%d very high for a muxer not efficiently supporting it.\n" + "Please consider specifying a lower framerate, a different muxer or -vsync 2\n", + ost->file_index, ost->index); } enc_ctx->width = av_buffersink_get_w(ost->filter->filter); From patchwork Mon Apr 4 11:30:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35171 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1912816pzb; Mon, 4 Apr 2022 04:35:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzvUimdM2stM6VdZE5OH/mblQBwg5Wzx3TkYG0C4GPM0dTM6OKocMg+m46KSPFuXBAKN5a1 X-Received: by 2002:a05:6402:5201:b0:419:4c82:8f11 with SMTP id s1-20020a056402520100b004194c828f11mr31940134edd.133.1649072109297; Mon, 04 Apr 2022 04:35:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072109; cv=none; d=google.com; s=arc-20160816; b=PcYPfQmATcsgKrUdv+bASyqbfTRA49ODvJCU1qJfZpQg82DLpGKkeQbYeCIXiFmelF I8fgP86jngkn1AfTWvlF9h1HNAX2uV31c8vNyFEcGCEnKxWfTWyPJ1IUSdNbtyDcHOJg ajXnyz2wIJdY7SR0R9CcijZEF2VK3ZW6EkTAkgoEbyf1eL9bNwxf1DNuNxQgxf/46F5T 70nCrcVc94UuReAiXQY/MfnZ5o+vTHVeJgL5A+WosRyoT3erF5tqOpRTJhhR3Os85quD p88RA4PnMBiUwehO57jrpfQWx0pGtSdab8W3q44Y57p8m/nwqI8ZEuIROc12pDyV88jV fNaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=8wwPIoUYzZW42X1vqXjZC+RWIfEwkTwpg9g63hQM4tA=; b=pzBH2uAA+JHzQcVJaZlNi5rXMv1fkorCLOuYgyGAyAy8vUo2UDMqjjq196g9Y6/x9q y95t7NJfA8fHFF0N7Cv+4K1if7U0NPG4mj61SNLFBkABNA6Lz4yeSdwkkjlHQ+e64M6x aZD2VCxGHU95waBbmuvuf/ntZECXrYUrpmEK2ZxknrBgJpIafR0eML+FJ4eASiuVFPnt SPrYrJFeNozVKyWnh+RX2K4mKTeixSDfBjfyOZ8hizXO/Kh9iMOIg/ulmFsFh4aOilYS zFUvwchoxnAgTixTeTPqx8JjPrqps5Y91Gfh3fdtPUbPObeA31UPT9jVk+frE96UzHNw 0YIw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id c3-20020a17090654c300b006e7f189009esi1843458ejp.187.2022.04.04.04.35.08; Mon, 04 Apr 2022 04:35:09 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2C3CD68B2E8; Mon, 4 Apr 2022 14:32:39 +0300 (EEST) 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 A9F5E68B1F5 for ; Mon, 4 Apr 2022 14:32:25 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 460AA240179 for ; Mon, 4 Apr 2022 13:32:25 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 4ZL37hE0RUpZ for ; Mon, 4 Apr 2022 13:32:22 +0200 (CEST) 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 4EF90240592 for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id E1BAE3A0B55; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:08 +0200 Message-Id: <20220404113037.13070-21-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 20/49] fftools/ffmpeg: move the mux queue into muxer private data 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: zMynpiaCoFBO The muxing queue currently lives in OutputStream, which is a very large struct storing the state for both encoding and muxing. The muxing queue is only used by the code in ffmpeg_mux, so it makes sense to restrict it to that file. This makes the first step towards reducing the scope of OutputStream. --- fftools/ffmpeg.c | 7 ---- fftools/ffmpeg.h | 9 ----- fftools/ffmpeg_mux.c | 87 +++++++++++++++++++++++++++++++++++++------- fftools/ffmpeg_opt.c | 9 ----- 4 files changed, 73 insertions(+), 39 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index a9a62eb8c3..b240bbd984 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -593,13 +593,6 @@ static void ffmpeg_cleanup(int ret) avcodec_free_context(&ost->enc_ctx); avcodec_parameters_free(&ost->ref_par); - if (ost->muxing_queue) { - AVPacket *pkt; - while (av_fifo_read(ost->muxing_queue, &pkt, 1) >= 0) - av_packet_free(&pkt); - av_fifo_freep2(&ost->muxing_queue); - } - av_freep(&output_streams[i]); } #if HAVE_THREADS diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 8e34ae0702..f0c3a3de7c 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -558,15 +558,6 @@ typedef struct OutputStream { int max_muxing_queue_size; - /* the packets are buffered here until the muxer is ready to be initialized */ - AVFifo *muxing_queue; - - /* - * The size of the AVPackets' buffers in queue. - * Updated when a packet is either pushed or pulled from the queue. - */ - size_t muxing_queue_data_size; - /* Threshold after which max_muxing_queue_size will be in effect */ size_t muxing_queue_data_threshold; diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 9dd5e8b076..33cea56c62 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -32,7 +32,20 @@ #include "libavformat/avformat.h" #include "libavformat/avio.h" +typedef struct MuxStream { + /* the packets are buffered here until the muxer is ready to be initialized */ + AVFifo *muxing_queue; + + /* + * The size of the AVPackets' buffers in queue. + * Updated when a packet is either pushed or pulled from the queue. + */ + size_t muxing_queue_data_size; +} MuxStream; + struct Muxer { + MuxStream *streams; + /* filesize limit expressed in bytes */ int64_t limit_filesize; int64_t final_filesize; @@ -55,6 +68,7 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, { AVFormatContext *s = of->ctx; AVStream *st = ost->st; + MuxStream *ms = &of->mux->streams[st->index]; int ret; /* @@ -76,10 +90,10 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, if (!of->mux->header_written) { AVPacket *tmp_pkt; /* the muxer is not initialized yet, buffer the packet */ - if (!av_fifo_can_write(ost->muxing_queue)) { - size_t cur_size = av_fifo_can_read(ost->muxing_queue); + if (!av_fifo_can_write(ms->muxing_queue)) { + size_t cur_size = av_fifo_can_read(ms->muxing_queue); unsigned int are_we_over_size = - (ost->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold; + (ms->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold; size_t limit = are_we_over_size ? ost->max_muxing_queue_size : SIZE_MAX; size_t new_size = FFMIN(2 * cur_size, limit); @@ -89,7 +103,7 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, ost->file_index, ost->st->index); exit_program(1); } - ret = av_fifo_grow2(ost->muxing_queue, new_size - cur_size); + ret = av_fifo_grow2(ms->muxing_queue, new_size - cur_size); if (ret < 0) exit_program(1); } @@ -100,8 +114,8 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, if (!tmp_pkt) exit_program(1); av_packet_move_ref(tmp_pkt, pkt); - ost->muxing_queue_data_size += tmp_pkt->size; - av_fifo_write(ost->muxing_queue, &tmp_pkt, 1); + ms->muxing_queue_data_size += tmp_pkt->size; + av_fifo_write(ms->muxing_queue, &tmp_pkt, 1); return; } @@ -283,15 +297,16 @@ int of_check_init(OutputFile *of) /* flush the muxing queues */ for (i = 0; i < of->ctx->nb_streams; i++) { + MuxStream *ms = &of->mux->streams[i]; OutputStream *ost = output_streams[of->ost_index + i]; AVPacket *pkt; /* try to improve muxing time_base (only possible if nothing has been written yet) */ - if (!av_fifo_can_read(ost->muxing_queue)) + if (!av_fifo_can_read(ms->muxing_queue)) ost->mux_timebase = ost->st->time_base; - while (av_fifo_read(ost->muxing_queue, &pkt, 1) >= 0) { - ost->muxing_queue_data_size -= pkt->size; + while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) { + ms->muxing_queue_data_size -= pkt->size; of_write_packet(of, pkt, ost, 1); av_packet_free(&pkt); } @@ -332,6 +347,29 @@ int of_write_trailer(OutputFile *of) return 0; } +static void mux_free(Muxer **pmux, int nb_streams) +{ + Muxer *mux = *pmux; + + if (!mux) + return; + + for (int i = 0; i < nb_streams; i++) { + MuxStream *ms = &mux->streams[i]; + AVPacket *pkt; + + if (!ms->muxing_queue) + continue; + + while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) + av_packet_free(&pkt); + av_fifo_freep2(&ms->muxing_queue); + } + av_freep(&mux->streams); + + av_freep(pmux); +} + void of_close(OutputFile **pof) { OutputFile *of = *pof; @@ -341,25 +379,42 @@ void of_close(OutputFile **pof) return; s = of->ctx; + + mux_free(&of->mux, s ? s->nb_streams : 0); + if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE)) avio_closep(&s->pb); avformat_free_context(s); av_dict_free(&of->opts); - av_freep(&of->mux); - av_freep(pof); } int of_muxer_init(OutputFile *of, int64_t limit_filesize) { Muxer *mux = av_mallocz(sizeof(*mux)); + int ret = 0; if (!mux) return AVERROR(ENOMEM); + mux->streams = av_calloc(of->ctx->nb_streams, sizeof(*mux->streams)); + if (!mux->streams) { + av_freep(&mux); + return AVERROR(ENOMEM); + } + of->mux = mux; + for (int i = 0; i < of->ctx->nb_streams; i++) { + MuxStream *ms = &mux->streams[i]; + ms->muxing_queue = av_fifo_alloc2(8, sizeof(AVPacket*), 0); + if (!ms->muxing_queue) { + ret = AVERROR(ENOMEM); + goto fail; + } + } + mux->limit_filesize = limit_filesize; if (strcmp(of->format->name, "rtp")) @@ -367,12 +422,16 @@ int of_muxer_init(OutputFile *of, int64_t limit_filesize) /* write the header for files with no streams */ if (of->format->flags & AVFMT_NOSTREAMS && of->ctx->nb_streams == 0) { - int ret = of_check_init(of); + ret = of_check_init(of); if (ret < 0) - return ret; + goto fail; } - return 0; +fail: + if (ret < 0) + mux_free(&of->mux, of->ctx->nb_streams); + + return ret; } int of_finished(OutputFile *of) diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 780c11903b..a9944e637d 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -1618,8 +1618,6 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e ost->max_muxing_queue_size = 128; MATCH_PER_STREAM_OPT(max_muxing_queue_size, i, ost->max_muxing_queue_size, oc, st); - ost->muxing_queue_data_size = 0; - ost->muxing_queue_data_threshold = 50*1024*1024; MATCH_PER_STREAM_OPT(muxing_queue_data_threshold, i, ost->muxing_queue_data_threshold, oc, st); @@ -1643,13 +1641,6 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e } ost->last_mux_dts = AV_NOPTS_VALUE; - ost->muxing_queue = av_fifo_alloc2(8, sizeof(AVPacket*), 0); - if (!ost->muxing_queue) - exit_program(1); - - MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i, - ost->copy_initial_nonkeyframes, oc, st); - return ost; } From patchwork Mon Apr 4 11:30:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35168 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1912570pzb; Mon, 4 Apr 2022 04:34:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzUy6Wt6Lr0UG8UTlVqMXdzECBlZe9PlmbvJ9i+5Z6q5rvYWGvtrH+1nZzLCHnXzZsDOw+L X-Received: by 2002:a17:907:e8d:b0:6e0:19e7:9549 with SMTP id ho13-20020a1709070e8d00b006e019e79549mr10568171ejc.44.1649072078026; Mon, 04 Apr 2022 04:34:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072078; cv=none; d=google.com; s=arc-20160816; b=YLbXMeS20P9/0Jj8CtzmPVA0Uy43lUWKDQVR0RiEP/8LXK7UOp53GIssBvEpFQQ3Ki scg22XBpRhayh/4bubMRdqQz8oIQ3GUTWGSAte4pgP1/D+XMbtLxPmK6nFQBORQqz7SF IfMlhINKVZA3yZWL0+PK8gV/2yV8HW6Ib3lHuHOttvcOjCXTTvN8fGvgTrPRgq/iopWl RYhP31SpaI6a/OS6YO/G3oY0M2+tTrIxujHU8+DcDp4oQ5617Ln9WdjVjudvajVBgbC5 93F1WtMAcpSG7j6FPS/AkhhNpt3/PMHJBIoz7fNrjoyPPxh3N3vAu37fhO8MJEDAu8Av wvBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=+0XykIzFC17dhujmETfXXvHgApo/CnZ2t8segFmNhCg=; b=CHQXHjjtkA8SkP2ZDwBU5vUjo7KzNlyVHvyDdeVRFRQmWRO2Y3IvRkjixwEygxqj3B BBQaXgc22ofpO8JmHUWjRyKSzleC4BqOPWASA1NTeiZPrJhcNUlCcEkN0h2E5GnZ8jYu Ql00wkp0Oax4JfG9TkHWbT9JBOmFQdtVqFQRgk/Wtplwn8rxlrRF+z5Du9ZWsd79O8ur vdE54gHsoWyjwghwXB98CcQVCFFWoOz8VYrNh/x7dxN+zWEOEm22DQ+i7q2ZrS731uUJ cSmzedm1N7qipnsVtvvAD6hDsWzE4IHZpyqIbqmGVQMyTTX2zSDgC26wMRoF17lbVYaC ltAA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id h8-20020a1709067cc800b006df76385b91si6030475ejp.49.2022.04.04.04.34.37; Mon, 04 Apr 2022 04:34:38 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 016B868B2D0; Mon, 4 Apr 2022 14:32:36 +0300 (EEST) 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 B532668B1F7 for ; Mon, 4 Apr 2022 14:32:23 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 509CD240179 for ; Mon, 4 Apr 2022 13:32:23 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id eUiC_qNqjpce for ; Mon, 4 Apr 2022 13:32:22 +0200 (CEST) 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 536CE24062A for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id E54573A0B57; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:09 +0200 Message-Id: <20220404113037.13070-22-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 21/49] fftools/ffmpeg_mux: split queuing packets into a separate function 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: OwKR8z+i7BaR --- fftools/ffmpeg_mux.c | 72 +++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 33cea56c62..e7b0a9ca99 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -63,12 +63,50 @@ static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, } } +static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) +{ + MuxStream *ms = &of->mux->streams[ost->index]; + AVPacket *tmp_pkt; + int ret; + + if (!av_fifo_can_write(ms->muxing_queue)) { + size_t cur_size = av_fifo_can_read(ms->muxing_queue); + unsigned int are_we_over_size = + (ms->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold; + size_t limit = are_we_over_size ? ost->max_muxing_queue_size : SIZE_MAX; + size_t new_size = FFMIN(2 * cur_size, limit); + + if (new_size <= cur_size) { + av_log(NULL, AV_LOG_ERROR, + "Too many packets buffered for output stream %d:%d.\n", + ost->file_index, ost->st->index); + return AVERROR(ENOSPC); + } + ret = av_fifo_grow2(ms->muxing_queue, new_size - cur_size); + if (ret < 0) + return ret; + } + + ret = av_packet_make_refcounted(pkt); + if (ret < 0) + return ret; + + tmp_pkt = av_packet_alloc(); + if (!tmp_pkt) + return AVERROR(ENOMEM); + + av_packet_move_ref(tmp_pkt, pkt); + ms->muxing_queue_data_size += tmp_pkt->size; + av_fifo_write(ms->muxing_queue, &tmp_pkt, 1); + + return 0; +} + void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue) { AVFormatContext *s = of->ctx; AVStream *st = ost->st; - MuxStream *ms = &of->mux->streams[st->index]; int ret; /* @@ -87,35 +125,13 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, ost->frame_number++; } + /* the muxer is not initialized yet, buffer the packet */ if (!of->mux->header_written) { - AVPacket *tmp_pkt; - /* the muxer is not initialized yet, buffer the packet */ - if (!av_fifo_can_write(ms->muxing_queue)) { - size_t cur_size = av_fifo_can_read(ms->muxing_queue); - unsigned int are_we_over_size = - (ms->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold; - size_t limit = are_we_over_size ? ost->max_muxing_queue_size : SIZE_MAX; - size_t new_size = FFMIN(2 * cur_size, limit); - - if (new_size <= cur_size) { - av_log(NULL, AV_LOG_ERROR, - "Too many packets buffered for output stream %d:%d.\n", - ost->file_index, ost->st->index); - exit_program(1); - } - ret = av_fifo_grow2(ms->muxing_queue, new_size - cur_size); - if (ret < 0) - exit_program(1); - } - ret = av_packet_make_refcounted(pkt); - if (ret < 0) - exit_program(1); - tmp_pkt = av_packet_alloc(); - if (!tmp_pkt) + ret = queue_packet(of, ost, pkt); + if (ret < 0) { + av_packet_unref(pkt); exit_program(1); - av_packet_move_ref(tmp_pkt, pkt); - ms->muxing_queue_data_size += tmp_pkt->size; - av_fifo_write(ms->muxing_queue, &tmp_pkt, 1); + } return; } From patchwork Mon Apr 4 11:30:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35166 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1912415pzb; Mon, 4 Apr 2022 04:34:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzsBTUsjYYMcDXcKOUUDNBZErY/NLvhZYYtQfVg3IQT35wtHy4leMGMptYEu9syCI68aQih X-Received: by 2002:a05:6402:42d4:b0:416:5cac:a9a0 with SMTP id i20-20020a05640242d400b004165caca9a0mr33087105edc.86.1649072057111; Mon, 04 Apr 2022 04:34:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072057; cv=none; d=google.com; s=arc-20160816; b=MjMeR7b0qRY/5DVsRdHN15MAVKczIrW7aIJs+nxt1pmi50wLofZcBgYYWU8kuS2BgH 77qgoTK116j1NRDIV22hjd/C0bXvCiUGpOpHoTJpdyXwHK7JAZA+kZuFyB6EDMQNV09q U2TSvkYmgh6Df/mJ8+0rqB2gvllarOzN3X1k1sHZthYY4nyGr6xZ2G0zUqoLd3UiHiRv C6PpdOC2Qpa1Y3l2DCuk7a467fW0ACQIdf++xs3c2mF+5L1RPrP/HMmqAmkBc/qKbmdU VN0IcJfWaHuMjFenHRA2miTfFrXl3O8LQyko68+MzjPsKTdqEmvGgmyFC9F6FYlCL7o1 uVsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=uMH//8zALqkEyOBMvTCRjVpjzkgHBsMzOkHU6oD16zs=; b=CktYSetjXUMnX1/CNFSAnaPZXEI5c7J3LcRD9a0FzE8pmxF7y8Zn1SNoNAVr1aNGAA gpVBuOb92+hnn8rWRgDBzXvfHx8J2b3VlsVHgM6kgcoGf4BvWcGl2Q3tA0nF3Ciw2ozA GIHVxPTJz+sareVDOBj4wKl7XJU/7JYSHpraXP4+FT4LysYX87vzRXWwK83gmyZuliks 9PMASRPk+GeGyE+znKCKXIBWwY432g/v7DM2dQDdmcXHXQl/UI9i9rLrp8VZ7PSDus5x 6KiRuB1xy4wZwkZ7gLwPFgFES0qZIj73bJislUobGJ5jh54CjxdbYRMmbgE7wwrMPzZz a04w== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id v10-20020a170906292a00b006df76385ef3si6359313ejd.915.2022.04.04.04.34.16; Mon, 04 Apr 2022 04:34:17 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id F153168B109; Mon, 4 Apr 2022 14:32:33 +0300 (EEST) 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 6D84068B186 for ; Mon, 4 Apr 2022 14:32:23 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id EC986240179 for ; Mon, 4 Apr 2022 13:32:21 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id XC9ndrIQq4ZB for ; Mon, 4 Apr 2022 13:32:21 +0200 (CEST) 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 524F22405F5 for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id E8E213A0BA7; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:10 +0200 Message-Id: <20220404113037.13070-23-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 22/49] fftools/ffmpeg_mux: split of_write_packet() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: KcvAVeQJYgcZ It is currently called from two places: - output_packet() in ffmpeg.c, which submits the newly available output packet to the muxer - from of_check_init() in ffmpeg_mux.c after the header has been written, to flush the muxing queue Some packets will thus be processed by this function twice, so it requires an extra parameter to indicate the place it is called from and avoid modifying some state twice. This is fragile and hard to follow, so split this function into two. Also rename of_write_packet() to of_submit_packet() to better reflect its new purpose. --- fftools/ffmpeg.c | 4 +-- fftools/ffmpeg.h | 3 +-- fftools/ffmpeg_mux.c | 63 ++++++++++++++++++++++++-------------------- 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index b240bbd984..ef0d5156eb 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -726,11 +726,11 @@ static void output_packet(OutputFile *of, AVPacket *pkt, if (ret < 0) goto finish; while ((ret = av_bsf_receive_packet(ost->bsf_ctx, pkt)) >= 0) - of_write_packet(of, pkt, ost, 0); + of_submit_packet(of, pkt, ost); if (ret == AVERROR(EAGAIN)) ret = 0; } else if (!eof) - of_write_packet(of, pkt, ost, 0); + of_submit_packet(of, pkt, ost); finish: if (ret < 0 && ret != AVERROR_EOF) { diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index f0c3a3de7c..5f002521ab 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -687,8 +687,7 @@ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); void of_close(OutputFile **pof); -void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, - int unqueue); +void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost); int of_finished(OutputFile *of); int64_t of_filesize(OutputFile *of); AVChapter * const * diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index e7b0a9ca99..2b85d55504 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -102,39 +102,12 @@ static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) return 0; } -void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, - int unqueue) +static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) { AVFormatContext *s = of->ctx; AVStream *st = ost->st; int ret; - /* - * Audio encoders may split the packets -- #frames in != #packets out. - * But there is no reordering, so we can limit the number of output packets - * by simply dropping them here. - * Counting encoded video frames needs to be done separately because of - * reordering, see do_video_out(). - * Do not count the packet when unqueued because it has been counted when queued. - */ - if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed) && !unqueue) { - if (ost->frame_number >= ost->max_frames) { - av_packet_unref(pkt); - return; - } - ost->frame_number++; - } - - /* the muxer is not initialized yet, buffer the packet */ - if (!of->mux->header_written) { - ret = queue_packet(of, ost, pkt); - if (ret < 0) { - av_packet_unref(pkt); - exit_program(1); - } - return; - } - if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && video_sync_method == VSYNC_DROP) || (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0)) pkt->pts = pkt->dts = AV_NOPTS_VALUE; @@ -225,6 +198,38 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, } } +void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) +{ + AVStream *st = ost->st; + int ret; + + /* + * Audio encoders may split the packets -- #frames in != #packets out. + * But there is no reordering, so we can limit the number of output packets + * by simply dropping them here. + * Counting encoded video frames needs to be done separately because of + * reordering, see do_video_out(). + */ + if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) { + if (ost->frame_number >= ost->max_frames) { + av_packet_unref(pkt); + return; + } + ost->frame_number++; + } + + if (of->mux->header_written) { + write_packet(of, ost, pkt); + } else { + /* the muxer is not initialized yet, buffer the packet */ + ret = queue_packet(of, ost, pkt); + if (ret < 0) { + av_packet_unref(pkt); + exit_program(1); + } + } +} + static int print_sdp(void) { char sdp[16384]; @@ -323,7 +328,7 @@ int of_check_init(OutputFile *of) while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) { ms->muxing_queue_data_size -= pkt->size; - of_write_packet(of, pkt, ost, 1); + write_packet(of, ost, pkt); av_packet_free(&pkt); } } From patchwork Mon Apr 4 11:30:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35183 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1914052pzb; Mon, 4 Apr 2022 04:37:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzuX5EFyqjI01s7Mztf+7w2ezNQ7G0WlD+tpXiodclIMajvWZij2yslAIq17WsLP6GVLQCH X-Received: by 2002:a17:907:971e:b0:6e7:fdc1:2571 with SMTP id jg30-20020a170907971e00b006e7fdc12571mr1156476ejc.300.1649072243714; Mon, 04 Apr 2022 04:37:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072243; cv=none; d=google.com; s=arc-20160816; b=yt+w6UY/LCrHHlzwlTymwbMb+bPhwDG5/qPvz0uW2ZQAH7wiEY1RYN2Sizz+8f4mPm iAkS/zFZAusjhuXWZ4e4RnC5H0QJtX3TlQ5lRtLW3OkURfHODh5H8yf5lpKd8WfB0oAo WbDXkcNflhU1PlelyoqHyC7RDndvv40ZGkfa4NF9YZ2zy1wQt1MXaT6D83OoXxLvOtvt 8KhsKvNTe+YIRkV+kQwLhNzlRA1+POcjVoNRjQbYWDsmRe883Nfycx+8zQm5qJmwFK7g ohOXzI3FW+vCaSq7ha3T7FpV7xl3KYNXuyvStcb06EnKNS8A1XB65/SQNmGOYX63bCqM 6HYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=pp9UWvKRfRIQvXAIkzQOlDhMslG7gtaZb1LSVPCHYqc=; b=KA2+xVae3y6bcHQsSAYT5o+qhDW0aliPBjZNZ1fgeaRIubvQXwp84kpUZp1Fhj40RN 40kwBHcl2V6UrTpKu8x9niHr8EUKESnto0c2KPDqPTnkjc2o9SwcnxvA8TWsIRxLz0sg FNrTGIAMgGwWQlT4GzIYCDVfl5SjcH/7n1FAuhNF1rU3W+o44paJC5ev2cfvw25ASAgk 3Lc3ZmJ3WN1xr4b8yvGy3dG1jJvVnXJRgsvcUprjSQQ93ATaJVT9SqgBV/TRp0oBgtIU KOFpsed1jglpC7rP9ZCDZagCrj+/Tj4C0QAx/hmAOXGz4wF+V8tdWuEsf2zxAUaroIKV lHpw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 8-20020a508748000000b0041cd8409045si1196979edv.623.2022.04.04.04.37.23; Mon, 04 Apr 2022 04:37:23 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 52BA868B341; Mon, 4 Apr 2022 14:32:51 +0300 (EEST) 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 5A8BE68B1E7 for ; Mon, 4 Apr 2022 14:32:28 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id CE4732400F5 for ; Mon, 4 Apr 2022 13:32:27 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id RR5L3b7x3PPK for ; Mon, 4 Apr 2022 13:32:27 +0200 (CEST) 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 BC269240691 for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id EC75E3A0C17; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:11 +0200 Message-Id: <20220404113037.13070-24-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 23/49] fftools/ffmpeg: move output file opts into private context 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Q8arNA6xAYHC It is private to the muxer, no reason to access it from outside. --- fftools/ffmpeg.h | 3 +-- fftools/ffmpeg_mux.c | 9 ++++++--- fftools/ffmpeg_opt.c | 12 ++++++------ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 5f002521ab..c6bbc653b8 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -577,7 +577,6 @@ typedef struct OutputFile { const AVOutputFormat *format; AVFormatContext *ctx; - AVDictionary *opts; int ost_index; /* index of the first stream in output_streams */ int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units @@ -681,7 +680,7 @@ int hw_device_setup_for_filter(FilterGraph *fg); int hwaccel_decode_init(AVCodecContext *avctx); -int of_muxer_init(OutputFile *of, int64_t limit_filesize); +int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize); /* open the muxer when all the streams are initialized */ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 2b85d55504..f301b8a66a 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -46,6 +46,8 @@ typedef struct MuxStream { struct Muxer { MuxStream *streams; + AVDictionary *opts; + /* filesize limit expressed in bytes */ int64_t limit_filesize; int64_t final_filesize; @@ -294,7 +296,7 @@ int of_check_init(OutputFile *of) return 0; } - ret = avformat_write_header(of->ctx, &of->opts); + ret = avformat_write_header(of->ctx, &of->mux->opts); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Could not write header for output file #%d " @@ -387,6 +389,7 @@ static void mux_free(Muxer **pmux, int nb_streams) av_fifo_freep2(&ms->muxing_queue); } av_freep(&mux->streams); + av_dict_free(&mux->opts); av_freep(pmux); } @@ -406,12 +409,11 @@ void of_close(OutputFile **pof) if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE)) avio_closep(&s->pb); avformat_free_context(s); - av_dict_free(&of->opts); av_freep(pof); } -int of_muxer_init(OutputFile *of, int64_t limit_filesize) +int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize) { Muxer *mux = av_mallocz(sizeof(*mux)); int ret = 0; @@ -437,6 +439,7 @@ int of_muxer_init(OutputFile *of, int64_t limit_filesize) } mux->limit_filesize = limit_filesize; + mux->opts = opts; if (strcmp(of->format->name, "rtp")) want_sdp = 0; diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index a9944e637d..adad46de5f 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2306,7 +2306,7 @@ static int open_output_file(OptionsContext *o, const char *filename) OutputFile *of; OutputStream *ost; InputStream *ist; - AVDictionary *unused_opts = NULL; + AVDictionary *unused_opts = NULL, *format_opts = NULL; const AVDictionaryEntry *e = NULL; if (o->stop_time != INT64_MAX && o->recording_time != INT64_MAX) { @@ -2331,7 +2331,7 @@ static int open_output_file(OptionsContext *o, const char *filename) of->recording_time = o->recording_time; of->start_time = o->start_time; of->shortest = o->shortest; - av_dict_copy(&of->opts, o->g->format_opts, 0); + av_dict_copy(&format_opts, o->g->format_opts, 0); if (!strcmp(filename, "-")) filename = "pipe:"; @@ -2353,7 +2353,7 @@ static int open_output_file(OptionsContext *o, const char *filename) oc->flags |= AVFMT_FLAG_BITEXACT; of->bitexact = 1; } else { - of->bitexact = check_opt_bitexact(oc, of->opts, "fflags", + of->bitexact = check_opt_bitexact(oc, format_opts, "fflags", AVFMT_FLAG_BITEXACT); } @@ -2734,7 +2734,7 @@ loop_end: /* open the file */ if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE, &oc->interrupt_callback, - &of->opts)) < 0) { + &format_opts)) < 0) { print_error(filename, err); exit_program(1); } @@ -2742,7 +2742,7 @@ loop_end: assert_file_overwrite(filename); if (o->mux_preload) { - av_dict_set_int(&of->opts, "preload", o->mux_preload*AV_TIME_BASE, 0); + av_dict_set_int(&format_opts, "preload", o->mux_preload*AV_TIME_BASE, 0); } oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE); @@ -2936,7 +2936,7 @@ loop_end: exit_program(1); } - err = of_muxer_init(of, o->limit_filesize); + err = of_muxer_init(of, format_opts, o->limit_filesize); if (err < 0) { av_log(NULL, AV_LOG_FATAL, "Error initializing internal muxing state\n"); exit_program(1); From patchwork Mon Apr 4 11:30:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35169 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1912660pzb; Mon, 4 Apr 2022 04:34:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxUhl1vT+g0IbTqcaWV7mAVSdzlfdN/qFJBycd/yF5zrZrdZVRoq8rX6+D7a5F5Kugidzjd X-Received: by 2002:a05:6402:4315:b0:419:2e38:ab8f with SMTP id m21-20020a056402431500b004192e38ab8fmr32713836edc.250.1649072088850; Mon, 04 Apr 2022 04:34:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072088; cv=none; d=google.com; s=arc-20160816; b=lJAkGbVpjujsP5diG7Nqsea5jqHUi7OzRVDp82zcNeJ41ijlQjcLflF4/oTjkDPi3p e0paubXHTnNrCerBo3acoBoi9HpUTJuK01vg+CYpAn+FlIpkUepVR9A8sy4/iwDHmmYM VIIpivR82m9Ja00IkIBFIIhrvFDYf4kYAPDrndKw9gXRIySXim8GbiDZHse5L9ig3u0q xVDCBCwVaROh13LOu9qtCOBUoXWHpOMrmQYrntQOwKPIgMiXmgpLf55lojILzhtGYrsE fvTxrXAeTlvDJiSjVH1aBqoFO1LqUuHA0sA9drSg2uOX0h6PQELxehsvsRQxhyseiAgE K9jw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=H6PIoNMu1+P1ziOiUEvHYa8fXbZKuTebsF1Jp5t0DQ8=; b=p10oOmHGOpWE5UoYDNNMFDFWsn/alIBk/oGLLKIgP5/7IQ+W+5eb4AApTdXp0P1hz2 dqUfOKjjB1Dw+d4y6ZIW/CNdeauSZr7qpmZPGHH0r9gMm+R2brYmmjtrJd50P4ljMXqV T3ZiPCjrIf1vSa5whnZoW7kboVBQWyBFtHZPZ7h/pcFDu7Mktw3ih6LOvB5/hgqpA12G tnZRxTpyBhHks6AGfwlT/nW28uu/+HWMbS03FlTm7QsG7MLDHGjzvQIJy50dJmO/omtw kFtdv6M+o77JJrX8wu2bW2zQd7Iy+Ng+QoX5E5mTYQuVJfMiRF00wmPlFP99yTM/3F7I sqBw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id n26-20020aa7c45a000000b00418fe90d5e1si6730911edr.431.2022.04.04.04.34.48; Mon, 04 Apr 2022 04:34:48 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0854468B2D8; Mon, 4 Apr 2022 14:32:37 +0300 (EEST) 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 9C16768B183 for ; Mon, 4 Apr 2022 14:32:24 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 24AF8240179 for ; Mon, 4 Apr 2022 13:32:24 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id NmM9ZFrA1Ler for ; Mon, 4 Apr 2022 13:32:23 +0200 (CEST) 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 56095240685 for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id F01603A0C69; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:12 +0200 Message-Id: <20220404113037.13070-25-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 24/49] fftools/ffmpeg: move processing video stats to ffmpeg_mux 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: H6UwSwCCP0Ae Currently it is called from - do_video_out(), at the end of each encode loop iteration - from flush_encoders(), after muxing each packet Since this function processes the data from the last encoded packet, neither of the above is fully correct, because - an encoder can in principle produce multiple packets per one submitted frame - bitstream filters may modify the number of encoded packets or their properties. It thus makes most sense to call this function right before sending the packet to the muxer. --- fftools/ffmpeg.c | 61 +------------------------------------------- fftools/ffmpeg.h | 5 ++++ fftools/ffmpeg_mux.c | 47 ++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 60 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index ef0d5156eb..68e720d889 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -110,7 +110,7 @@ const char program_name[] = "ffmpeg"; const int program_birth_year = 2000; -static FILE *vstats_file; +FILE *vstats_file; const char *const forced_keyframes_const_names[] = { "n", @@ -127,7 +127,6 @@ typedef struct BenchmarkTimeStamps { int64_t sys_usec; } BenchmarkTimeStamps; -static void do_video_stats(OutputStream *ost, int frame_size); static BenchmarkTimeStamps get_benchmark_time_stamps(void); static int64_t getmaxrss(void); static int ifilter_has_all_input_formats(FilterGraph *fg); @@ -977,7 +976,6 @@ static void do_video_out(OutputFile *of, double delta, delta0; double duration = 0; double sync_ipts = AV_NOPTS_VALUE; - int frame_size = 0; InputStream *ist = NULL; AVFilterContext *filter = ost->filter->filter; @@ -1214,7 +1212,6 @@ static void do_video_out(OutputFile *of, av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->mux_timebase)); } - frame_size = pkt->size; output_packet(of, pkt, ost, 0); /* if two pass, output log */ @@ -1224,9 +1221,6 @@ static void do_video_out(OutputFile *of, } ost->sync_opts++; ost->frame_number++; - - if (vstats_filename && frame_size) - do_video_stats(ost, frame_size); } av_frame_unref(ost->last_frame); @@ -1239,54 +1233,6 @@ error: exit_program(1); } -static double psnr(double d) -{ - return -10.0 * log10(d); -} - -static void do_video_stats(OutputStream *ost, int frame_size) -{ - AVCodecContext *enc; - int frame_number; - double ti1, bitrate, avg_bitrate; - - /* this is executed just the first time do_video_stats is called */ - if (!vstats_file) { - vstats_file = fopen(vstats_filename, "w"); - if (!vstats_file) { - perror("fopen"); - exit_program(1); - } - } - - enc = ost->enc_ctx; - if (enc->codec_type == AVMEDIA_TYPE_VIDEO) { - frame_number = ost->st->nb_frames; - if (vstats_version <= 1) { - fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, - ost->quality / (float)FF_QP2LAMBDA); - } else { - fprintf(vstats_file, "out= %2d st= %2d frame= %5d q= %2.1f ", ost->file_index, ost->index, frame_number, - ost->quality / (float)FF_QP2LAMBDA); - } - - if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR)) - fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0))); - - fprintf(vstats_file,"f_size= %6d ", frame_size); - /* compute pts value */ - ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base); - if (ti1 < 0.01) - ti1 = 0.01; - - bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0; - avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0; - fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ", - (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate); - fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type)); - } -} - static void finish_output_stream(OutputStream *ost) { OutputFile *of = output_files[ost->file_index]; @@ -1773,7 +1719,6 @@ static void flush_encoders(void) for (;;) { const char *desc = NULL; AVPacket *pkt = ost->pkt; - int pkt_size; switch (enc->codec_type) { case AVMEDIA_TYPE_AUDIO: @@ -1817,11 +1762,7 @@ static void flush_encoders(void) continue; } av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase); - pkt_size = pkt->size; output_packet(of, pkt, ost, 0); - if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename) { - do_video_stats(ost, pkt_size); - } } } } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index c6bbc653b8..604f0145e3 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -627,6 +627,7 @@ extern int stdin_interaction; extern int frame_bits_per_raw_sample; extern AVIOContext *progress_avio; extern float max_error_rate; +extern FILE *vstats_file; extern char *filter_nbthreads; extern int filter_complex_nbthreads; @@ -644,6 +645,10 @@ extern HWDevice *filter_hw_device; extern unsigned nb_output_dumped; extern int main_return_code; +static inline double psnr(double d) +{ + return -10.0 * log10(d); +} void term_init(void); void term_exit(void); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index f301b8a66a..5facc9b1fc 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -56,6 +56,49 @@ struct Muxer { static int want_sdp = 1; +static void do_video_stats(OutputStream *ost, int frame_size) +{ + AVCodecContext *enc; + int frame_number; + double ti1, bitrate, avg_bitrate; + + /* this is executed just the first time do_video_stats is called */ + if (!vstats_file) { + vstats_file = fopen(vstats_filename, "w"); + if (!vstats_file) { + perror("fopen"); + exit_program(1); + } + } + + enc = ost->enc_ctx; + if (enc->codec_type == AVMEDIA_TYPE_VIDEO) { + frame_number = ost->st->nb_frames; + if (vstats_version <= 1) { + fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, + ost->quality / (float)FF_QP2LAMBDA); + } else { + fprintf(vstats_file, "out= %2d st= %2d frame= %5d q= %2.1f ", ost->file_index, ost->index, frame_number, + ost->quality / (float)FF_QP2LAMBDA); + } + + if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR)) + fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0))); + + fprintf(vstats_file,"f_size= %6d ", frame_size); + /* compute pts value */ + ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base); + if (ti1 < 0.01) + ti1 = 0.01; + + bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0; + avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0; + fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ", + (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate); + fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type)); + } +} + static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others) { int i; @@ -179,6 +222,10 @@ static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) ost->data_size += pkt->size; ost->packets_written++; + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && + ost->encoding_needed && vstats_filename) + do_video_stats(ost, pkt->size); + pkt->stream_index = ost->index; if (debug_ts) { From patchwork Mon Apr 4 11:30:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35179 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1913560pzb; Mon, 4 Apr 2022 04:36:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywEVBkE7odADgMlkV1Z62TY7hTLBYb2k3n/Am79HfIe1v6w+IKHa5VEmpaVG8UY4UdSGak X-Received: by 2002:a17:907:971c:b0:6e0:dd95:9fc6 with SMTP id jg28-20020a170907971c00b006e0dd959fc6mr10570400ejc.256.1649072197796; Mon, 04 Apr 2022 04:36:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072197; cv=none; d=google.com; s=arc-20160816; b=Nbp7IiZbasxRSNBprjXf9bV0CdyM5q9l+75m36JzYZiVMBVEPI+I9vsWNICeXfW2rO +WdrH3a/0SPbROGJSldJcBA/71bqpdhUNMfr2JNqWvAxjkLuSoXiothQIZqFazdzjVyJ QRpjeLzMcGa8yM6wfYDK+Y4Tcgcm0e/lNynonV1a4LAGtGkagpB8JvbZghrvQq1rxOlu bCMAhlPf2EuA9zz5gnw1u9esjG2XXmOo/K/Oh4ZxzlYhqyiIUu4orshK7sQHzXwaKWN3 R6om0nemawq0Iv31wmKFFf0fGMAkC5M7bxv/E3b6D/FBpBO0/sxfSeMllJJp8J9mpe+w Y+cg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=0caPpHGZpiPRPqyDn/j17AWlKDfbl2g/emMOjPltN6g=; b=jqRWwJnRBRtXkbAbQip4YQVs0jYQKKc8rgb9UVE2c46FX1TjX/VJODHzXaZFv4Z5/7 YWU1Diy7CicZgctD+nikrPmjjGHdL/m6fu8P9EPxcL76AWiFaA70VRgfl1Be+1xVd8b2 niqPcKjIvTwR4hl5BpF9rHvFgPxNx8IfUGAx4QYS5qbi4L2bIgQfBHUrtGCTNAo7DMf9 Qx68EjprixKS4fDhG4jmMRWVBvVhpIxmm0AlKPYe0GB0hWJgSMYc14GfP8SZyagP8zXT /1pDTEGtkbjmxSVQeK3ckk6JlaKfMyU3FmUed+zxh8+1AH34XSijX42Vq2fjeCW4E7wL R51w== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id r4-20020a170906c28400b006e0598b7d80si5667721ejz.821.2022.04.04.04.36.37; Mon, 04 Apr 2022 04:36:37 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3CF4E68B327; Mon, 4 Apr 2022 14:32:47 +0300 (EEST) 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 89EA868B265 for ; Mon, 4 Apr 2022 14:32:27 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 100F52400F5 for ; Mon, 4 Apr 2022 13:32:27 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id pdYTGbKMwaIx for ; Mon, 4 Apr 2022 13:32:26 +0200 (CEST) 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 BBEEF24068F for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id F39E93A0CD7; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:13 +0200 Message-Id: <20220404113037.13070-26-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 25/49] fftools/ffmpeg_mux: drop a useless check and reduce indentation 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: VqtYHdTzFYLk do_video_stats() is only ever called for video. --- fftools/ffmpeg_mux.c | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 5facc9b1fc..059beabaf3 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -72,31 +72,29 @@ static void do_video_stats(OutputStream *ost, int frame_size) } enc = ost->enc_ctx; - if (enc->codec_type == AVMEDIA_TYPE_VIDEO) { - frame_number = ost->st->nb_frames; - if (vstats_version <= 1) { - fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, - ost->quality / (float)FF_QP2LAMBDA); - } else { - fprintf(vstats_file, "out= %2d st= %2d frame= %5d q= %2.1f ", ost->file_index, ost->index, frame_number, - ost->quality / (float)FF_QP2LAMBDA); - } + frame_number = ost->st->nb_frames; + if (vstats_version <= 1) { + fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, + ost->quality / (float)FF_QP2LAMBDA); + } else { + fprintf(vstats_file, "out= %2d st= %2d frame= %5d q= %2.1f ", ost->file_index, ost->index, frame_number, + ost->quality / (float)FF_QP2LAMBDA); + } - if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR)) - fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0))); + if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR)) + fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0))); - fprintf(vstats_file,"f_size= %6d ", frame_size); - /* compute pts value */ - ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base); - if (ti1 < 0.01) - ti1 = 0.01; + fprintf(vstats_file,"f_size= %6d ", frame_size); + /* compute pts value */ + ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base); + if (ti1 < 0.01) + ti1 = 0.01; - bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0; - avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0; - fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ", - (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate); - fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type)); - } + bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0; + avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0; + fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ", + (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate); + fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type)); } static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others) From patchwork Mon Apr 4 11:30:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35182 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1913914pzb; Mon, 4 Apr 2022 04:37:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzed5VZHPh3wzKtEpq5zxCyoVr/T934CXBG8FH77i1tkizOZk9Av0N2GpmEhuzrN+f7lPoH X-Received: by 2002:a05:6402:524c:b0:419:4d8c:e959 with SMTP id t12-20020a056402524c00b004194d8ce959mr32742068edd.398.1649072232334; Mon, 04 Apr 2022 04:37:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072232; cv=none; d=google.com; s=arc-20160816; b=0oJDOWdivKwKr59rxBPvraSFp7FLIE+vkRintmUjmnwidbJ1Fat9QIoEWY6iqPOfMy JPtc2ObkMs87XwWc41sDk7UFotoE8u7mCTo+W6KKgBePtSTNqemFeyXvd60l59kO2JfB hBCxBGoojOp92GnOjO+KCos/mUSAKNyrWXc466OmbuWBr/1/nzROlxsNhsC95ERs3IDl hlXE2ij1FCs6Yn3/z5Q1Z8QvQoHPWZyaH3VXi4gq8Q8VXAyFLKdBP2ZHbAEL3vstdPYB QjpHTWL39jDkalL+Vnc/AjCyCpcNiALiJ008qu3olOl9MyNPORRT0Vvqh3b0CJrd8uR9 4AzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=7nDMlctgFjE+jNqbNIk8+Te7q5CK4svbw3y+NBelh5w=; b=vheZrINp2ezr2TFZ1bNQpvFBWGdVRVRCbaBM0VVUOUgts9DJw1pMx4twZwn0lcn/xN ib595dpRtqC9xQX+OO6kGgnslOoVPwA31DMvZKpdtyOTLl28L35lh6ze66Z7T32iAjGm iYxf6Ll3rx/LJ9LS96HtsHAHnvtl/hIBgbe5v/Y3mT4ycAOgdfJlChTDg53eR6OstKH7 WJbia0U26n05BxonHo90B21RJTI/AHJYGJKWpr0qQkhR9nqwb8a17jk7Fgy9MVjGIpbg pBUMcXKd1I6A/AiiPal5Jek2IvWZRat9CObQVHTaRK97IXZNvfKkXEtwOEIAw1VACRFW n4aw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id s14-20020a170906284e00b006e7ede451d8si2191648ejc.957.2022.04.04.04.37.11; Mon, 04 Apr 2022 04:37:12 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 41A8868B339; Mon, 4 Apr 2022 14:32:50 +0300 (EEST) 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 DDAC168A8A0 for ; Mon, 4 Apr 2022 14:32:27 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 773BF2400F5 for ; Mon, 4 Apr 2022 13:32:27 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id Nx5xUgTmlalr for ; Mon, 4 Apr 2022 13:32:27 +0200 (CEST) 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 BC14C240690 for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 031383A0CEB; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:14 +0200 Message-Id: <20220404113037.13070-27-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 26/49] fftools/ffmpeg_mux: stop using AVStream.nb_frames in do_video_stats() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: gosmjahA9zsP Its use for muxing is not documented, in practice it is incremented per each packet successfully passed to the muxer's write_packet(). Since there is a lot of indirection between ffmpeg submitting a packet to the muxer and it actually being written (e.g. the interleaving queue), using nb_frames to count packets sent to the muxer is incorrect. Use OutputStream.packets_written instead. --- fftools/ffmpeg_mux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 059beabaf3..49703ed727 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -72,7 +72,7 @@ static void do_video_stats(OutputStream *ost, int frame_size) } enc = ost->enc_ctx; - frame_number = ost->st->nb_frames; + frame_number = ost->packets_written - 1; if (vstats_version <= 1) { fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, ost->quality / (float)FF_QP2LAMBDA); From patchwork Mon Apr 4 11:30:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35191 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1915251pzb; Mon, 4 Apr 2022 04:39:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwN/LbuQayibYYtvXckTjoTH17ZOIw7U1oSdga/TSqnGsuk29gPxga1czE32ak+d4GEGnTn X-Received: by 2002:a17:907:2d07:b0:6e0:13e2:841b with SMTP id gs7-20020a1709072d0700b006e013e2841bmr10289981ejc.502.1649072383746; Mon, 04 Apr 2022 04:39:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072383; cv=none; d=google.com; s=arc-20160816; b=p5CYmtLLzXWtX4nhHBlvj1JN/lIpZ91CsI8/1qOhwZ9JJUYBY5eci4G2clyJHWcEPP n91wv1noj3PiUHnz7ouJelR6EJ7NnimSBlNohDWlqhKRSoGBRUDH099i3CKijUu9lyd2 8N4ZO0RDYPHPUp9W+STnu/fSuTe9nljxYnMc2j5oEZMiR+6tJ4Ng48gMxVbiCiOvpbLI 5m7iNLs7YQAgcUk/klJiC+4Y6IH4Rkzuz/7Vqys0TyUkQaIL1lPbKkFjdI2gX5h0Uvdm 2v2JaZ+IgJ6GZ2SPt2nPRsao2XXEiIRWea6yoTYUMdUTnvvnQ6dm8uC59nWaX1fftctT rnNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=R05cUVoeCt2GTrsN8QnpQyk4Ry0BMuxchvE1xm/7M5E=; b=YfwCBwd7bOPUU91Cv9gTJCkcOf4n53FkelHJBFr45L9Q1GP7O0KsfQTgfmScyakMv1 4RC3AjHdp7OEA3D0CGQ6YOCfem32AwfyNDef1BWk6zz5JGud+a97hPIvmtVI2zb0tQUy RkpbVrXHHUpEnInmLoUyhfAyrDISb6XKJTjVjfKWVZG6wlWuIuN2wAqpLG/erHly24V9 JAAk3P3xs9C/1Kxvc0nQ+DVAeIYB0cbJBMtSKUiKEVNTWO3jFqHoN5iOjVJrbOt5kwdO xXwHGMGMcpy+eJnTG8S6bV/xcX8vSVgdcoaCp24yOGOU2o6yhsvLJ8pwABIptifHQbD7 Uc6Q== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id z8-20020a17090665c800b006e66edfcccesi1828962ejn.756.2022.04.04.04.39.43; Mon, 04 Apr 2022 04:39:43 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6CFF468B2CD; Mon, 4 Apr 2022 14:37:54 +0300 (EEST) 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 C076468B34A for ; Mon, 4 Apr 2022 14:37:45 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 7E6B624017E for ; Mon, 4 Apr 2022 13:37:45 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id QI7NSdacaVMZ for ; Mon, 4 Apr 2022 13:37:45 +0200 (CEST) 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 B2438240511 for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 06A6B3A0CF7; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:15 +0200 Message-Id: <20220404113037.13070-28-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 27/49] fftools/ffmpeg_mux: stop using av_stream_get_end_pts() in do_video_stats() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: SwfOWy/UFNBe It retrieves libavformat's internal dts value (contrary to the function's name), which is not necessary here because we can access the packet directly. --- fftools/ffmpeg_mux.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 49703ed727..8e0e278e4f 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -56,7 +56,7 @@ struct Muxer { static int want_sdp = 1; -static void do_video_stats(OutputStream *ost, int frame_size) +static void do_video_stats(OutputStream *ost, const AVPacket *pkt) { AVCodecContext *enc; int frame_number; @@ -84,13 +84,13 @@ static void do_video_stats(OutputStream *ost, int frame_size) if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR)) fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0))); - fprintf(vstats_file,"f_size= %6d ", frame_size); + fprintf(vstats_file,"f_size= %6d ", pkt->size); /* compute pts value */ - ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base); + ti1 = pkt->dts * av_q2d(ost->st->time_base); if (ti1 < 0.01) ti1 = 0.01; - bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0; + bitrate = (pkt->size * 8) / av_q2d(enc->time_base) / 1000.0; avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0; fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ", (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate); @@ -222,7 +222,7 @@ static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed && vstats_filename) - do_video_stats(ost, pkt->size); + do_video_stats(ost, pkt); pkt->stream_index = ost->index; From patchwork Mon Apr 4 11:30:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35180 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1913679pzb; Mon, 4 Apr 2022 04:36:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwV1rhOGfHzfb3XoN1zvFraFQaShEGYMWgWM9vbAW3mWcsRANbUKg3+x8+8k+XYSX0r/rTV X-Received: by 2002:a17:907:6284:b0:6e0:f895:15a with SMTP id nd4-20020a170907628400b006e0f895015amr10463299ejc.713.1649072208987; Mon, 04 Apr 2022 04:36:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072208; cv=none; d=google.com; s=arc-20160816; b=wUYV4bl7GMOKAC2bCgQyNvhJ9ATzD44iU54eJ4iNwju67bs22CLpa19QrFl4eNfFb7 +bzLcux7zl2jukl47M3iMTQYNzBKp1l422RO4ZrOotryBr4TgiCjZQKgcCEN3SzV64Q4 wS/Y9PPct/3nK2PRXyXNXBc9bl7eDPh+cOnJioiSLKoLBfvuZphg6+xZhogS19T4iP+n wkdYhkKds3lHp3zNZDGygGNIQZ4j/V5s9ve1sLcq4wNP6qVinwxW/b+T6QDUdfCGlc8u M+I9qNzlTzSa2YY1g4BTlTghS3k6MdWgbm4Oj1yL14ryB5m1gtPDtdILoFoNkB81HDjo F7ag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=Q5JmxtMpLSKbHPbFf8PZ/JpDSDVOqLar8sUt2AX5vJs=; b=lyVFULLkwmiSCSP7v0MspcXo/ZnkxSvfoTnipaVEPFsLGj24yFmVVEQvlX3UZwA9uj WwzRROk+Xu+P9FJOwFA+cTiTNwF0VbFbaZxgXuNZYoZ3ZDIRCk8vJYGGN5fTXZMChFsy RmLLv37RfDxCG5DaDNe8iINHcazcSYN7MQxUWZnnfxQJ+JbEFm6WAjKu/79UuVYkbhP6 rF8a3Pp0ewchEYiSi6Y4tqEecL0yDb1GgOnsbrg7EFzItgWKLAmsgo9Ppw0iV5NsDKM0 osPFKA+sChqjtkveslYciFW46HeyMMF21vLu0Zc6IGnbmb8pzOo9PvHTiW5GlxO52bqD DXPA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id u7-20020a50d947000000b0041c7cde74fasi5682683edj.308.2022.04.04.04.36.48; Mon, 04 Apr 2022 04:36:48 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4617968B32B; Mon, 4 Apr 2022 14:32:48 +0300 (EEST) 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 9C41368B250 for ; Mon, 4 Apr 2022 14:32:27 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 37DC0240179 for ; Mon, 4 Apr 2022 13:32:27 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id K7ziSx_5TIS7 for ; Mon, 4 Apr 2022 13:32:24 +0200 (CEST) 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 58773240687 for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 0A3E63A0CFE; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:16 +0200 Message-Id: <20220404113037.13070-29-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 28/49] fftools/ffmpeg_mux: merge variable declaration and initialization 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: glb+y1XD/XYs --- fftools/ffmpeg_mux.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 8e0e278e4f..b7140fca5e 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -58,7 +58,7 @@ static int want_sdp = 1; static void do_video_stats(OutputStream *ost, const AVPacket *pkt) { - AVCodecContext *enc; + AVCodecContext *enc = ost->enc_ctx; int frame_number; double ti1, bitrate, avg_bitrate; @@ -71,7 +71,6 @@ static void do_video_stats(OutputStream *ost, const AVPacket *pkt) } } - enc = ost->enc_ctx; frame_number = ost->packets_written - 1; if (vstats_version <= 1) { fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, From patchwork Mon Apr 4 11:30:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35184 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1914208pzb; Mon, 4 Apr 2022 04:37:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw2jG029VotRIdheLY+rh7szexx2okX0MleuBSLStcz77V2c7QxlUemPLranyyi7uc1Ng0E X-Received: by 2002:a05:6402:f16:b0:419:d9f3:f8b with SMTP id i22-20020a0564020f1600b00419d9f30f8bmr32858075eda.33.1649072256076; Mon, 04 Apr 2022 04:37:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072256; cv=none; d=google.com; s=arc-20160816; b=B6H+MsJ57iAXl3WimvE6ePp/+LrzWouI/t2LmaBtqQafHTVRbLPP/kiMZNmQxM5k4y HLTUkn3y/+iqWOcx8qVptrnZ8FO/pnLHsUOkhVCNccd+SNlOXnTKLQd5lVYkgS5yUjY8 Fw3WltbDTExCHnJoN6fv8lVgdZ/MnFeV6JylKWa2pKHQjV05jn9wCWbZMxqvExxGHwbm 0XXoSDfTwIduTV31oykqpyoqAvn8rFxiL8ILMr8pDAA5o2NMcBJbbIXwjlWDc+ySB/y3 Cw4OjGPzpZ9CjOXHq74qmCyBABBCiUpwehaWVja7UArqfKoJmPUKmYvVJC+ZQaDIWfb4 Q8mA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=y4x4EZU7VXe1+xECCvxn9r0D02MOwXR/U6jY4sjy9Hw=; b=X+qKt1SHjsGVoeQSCHi9xRZSaDxdLEpjTTOXvUUnvs3d5v3ub71agOjhOmunpnkoHg NR8haZSt1NXW/RVtY9BPQ1PLYF2WOndO5ij4AK8W1+m4x8NUYZi63b7UyMWyfVLpxWPY sXShKr7HS4PA3c+chOnZtFy9UIF6pHfDSFydlcTaJH2ueycQzrtW4FdC9cSllQ1S4h5h i0wTna3B+DtDVZPAvtf7uh0sn3jsYWDj+6p2HfQk4mOD69U7NjzMko9dpuWrAdoW50dm QU7opmrUoBq21sWkyUrHWEA/ncuNIV1Tdhh3gZ0/Cz8uSGPytwPb2gKWtfoHNAF8zehG bVdA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id w15-20020a05640234cf00b004193289a2dfsi6796685edc.151.2022.04.04.04.37.35; Mon, 04 Apr 2022 04:37:36 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 41BEA68B346; Mon, 4 Apr 2022 14:32:52 +0300 (EEST) 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 77D2768B120 for ; Mon, 4 Apr 2022 14:32:28 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 13433240179 for ; Mon, 4 Apr 2022 13:32:28 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id fbkR7AaL9Ipn for ; Mon, 4 Apr 2022 13:32:27 +0200 (CEST) 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 E4B5C240692 for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 0DE163A0D17; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:17 +0200 Message-Id: <20220404113037.13070-30-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 29/49] fftools/ffmpeg_mux: move processing AV_PKT_DATA_QUALITY_STATS to do_video_stats() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Bv9qjKpCQnTl This is a more appropriate place for this code, since the values we read from AV_PKT_DATA_QUALITY_STATS side data are primarily written into video stats. Rename the function to update_video_stats() to better reflect its new purpose. --- fftools/ffmpeg_mux.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index b7140fca5e..ccfe31e09a 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -56,13 +56,28 @@ struct Muxer { static int want_sdp = 1; -static void do_video_stats(OutputStream *ost, const AVPacket *pkt) +static void update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats) { + const uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, + NULL); AVCodecContext *enc = ost->enc_ctx; int frame_number; double ti1, bitrate, avg_bitrate; - /* this is executed just the first time do_video_stats is called */ + ost->quality = sd ? AV_RL32(sd) : -1; + ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE; + + for (int i = 0; i < FF_ARRAY_ELEMS(ost->error); i++) { + if (sd && i < sd[5]) + ost->error[i] = AV_RL64(sd + 8 + 8 * i); + else + ost->error[i] = -1; + } + + if (!write_vstats) + return; + + /* this is executed just the first time update_video_stats is called */ if (!vstats_file) { vstats_file = fopen(vstats_filename, "w"); if (!vstats_file) { @@ -155,19 +170,6 @@ static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) pkt->pts = pkt->dts = AV_NOPTS_VALUE; if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { - int i; - uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, - NULL); - ost->quality = sd ? AV_RL32(sd) : -1; - ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE; - - for (i = 0; ierror); i++) { - if (sd && i < sd[5]) - ost->error[i] = AV_RL64(sd + 8 + 8*i); - else - ost->error[i] = -1; - } - if (ost->frame_rate.num && ost->is_cfr) { if (pkt->duration > 0) av_log(NULL, AV_LOG_WARNING, "Overriding packet duration by frame rate, this should not happen\n"); @@ -219,9 +221,8 @@ static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) ost->data_size += pkt->size; ost->packets_written++; - if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && - ost->encoding_needed && vstats_filename) - do_video_stats(ost, pkt); + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed) + update_video_stats(ost, pkt, !!vstats_filename); pkt->stream_index = ost->index; From patchwork Mon Apr 4 11:30:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35202 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a02:aa92:0:0:0:0:0 with SMTP id u18csp3587197jai; Mon, 4 Apr 2022 04:38:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyKZdZKFe5oTGKfet6BXO6p7qtMgYeyZ56Z9H6gzQYmZfkS9fPj4kyq0xwBBkYnbFQFM92l X-Received: by 2002:a05:6402:4302:b0:419:4be8:6493 with SMTP id m2-20020a056402430200b004194be86493mr32760040edc.294.1649072328758; Mon, 04 Apr 2022 04:38:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072328; cv=none; d=google.com; s=arc-20160816; b=YvqxZWaUnhRVja56EG6WmNfOnLMtKCVRJs1szTU8Ofs40ttG89GeAk8gs3FWXmInAN /uwD0Qro21InL/5bfiw4ncVybTJwH/rr7EzGB0JLFddLEYB4xrYhTI0NKRnPbxAkeaaT bfnnKpJEa9Iru1bAuyrbTwS74RApAR/gu3pAt/qkynp44sIliyb6teB3RhEPjS311WG+ GzsNUkBVEPNRkRJ7nCklyG+n2hKjEVDP03aK6/yahCdElKXOMsJB4pc9gQGPBkNEidlN Enzt3eGhvsowzI5YbAK/0Tt+Zipjr4+2CPKINPQcN4aDPKP9o0VMPVVW6bn26qB6DNWd xm3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=7Xvs6kHZqpUzGV9tR48N8oUTj+cTYvrihBa2DiljUCU=; b=Jueol6jKMuqz4r73plJb6m9m2AMMp9GzQQtGtJthcFMjG/AoTTVVEet2IhnINLYoRF E+rTgJqn5HyCKF9Yfw875bGPbg9x6NU7GS6rMt6TwbNQI5faR0teKm62H+5GbxYyBE1V xn1rzqeUYCrUQoMpRS1VskozRsmZkRtpLEp8wW1auH5FLYIozEqdw+101PPce/uVsFqf GJhx3YYw65EQXD9fq5rYJOizLsUHxbAP9DfuzDFFtCCMsUwGeNDwp3oz7nE4QEJ9PwR5 wwwQB/iv+TKclVHlSY/cIf/yexYVH/h5KRxdqzLOKc3B1Z64hqe/DK1aWNqszQ4K3F2d yYNQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id y14-20020a056402358e00b00418c2b5bdcasi6372534edc.172.2022.04.04.04.38.48; Mon, 04 Apr 2022 04:38:48 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C585668B31C; Mon, 4 Apr 2022 14:37:49 +0300 (EEST) 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 7B9B968B2BF for ; Mon, 4 Apr 2022 14:37:44 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 22C1024017E for ; Mon, 4 Apr 2022 13:37:44 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id fakfph9KAnki for ; Mon, 4 Apr 2022 13:37:43 +0200 (CEST) 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 9FC63240507 for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 1193D3A0D1F; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:18 +0200 Message-Id: <20220404113037.13070-31-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 30/49] fftools/ffmpeg: share the code encoding a single frame between video and audio 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: d9WbFk7M6LJR Content-Length: 7839 --- fftools/ffmpeg.c | 136 ++++++++++++++++++++--------------------------- 1 file changed, 58 insertions(+), 78 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 68e720d889..662a5d22b1 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -821,63 +821,93 @@ static int init_output_stream_wrapper(OutputStream *ost, AVFrame *frame, return ret; } -static void do_audio_out(OutputFile *of, OutputStream *ost, - AVFrame *frame) +static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) { - AVCodecContext *enc = ost->enc_ctx; - AVPacket *pkt = ost->pkt; + AVCodecContext *enc = ost->enc_ctx; + AVPacket *pkt = ost->pkt; + const char *type_desc = av_get_media_type_string(enc->codec_type); int ret; - adjust_frame_pts_to_encoder_tb(of, ost, frame); - - if (!check_recording_time(ost)) - return; - - if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0) - frame->pts = ost->sync_opts; - ost->sync_opts = frame->pts + frame->nb_samples; - ost->samples_encoded += frame->nb_samples; ost->frames_encoded++; update_benchmark(NULL); + if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "encoder <- type:audio " + av_log(NULL, AV_LOG_INFO, "encoder <- type:%s " "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n", + type_desc, av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base), enc->time_base.num, enc->time_base.den); } ret = avcodec_send_frame(enc, frame); - if (ret < 0) - goto error; + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n", + type_desc); + return ret; + } while (1) { ret = avcodec_receive_packet(enc, pkt); + update_benchmark("encode_%s %d.%d", type_desc, + ost->file_index, ost->index); if (ret == AVERROR(EAGAIN)) - break; - if (ret < 0) - goto error; + return 0; + else if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "%s encoding failed\n", type_desc); + return ret; + } - update_benchmark("encode_audio %d.%d", ost->file_index, ost->index); + if (debug_ts) { + av_log(NULL, AV_LOG_INFO, "encoder -> type:%s " + "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s " + "duration:%s duration_time:%s\n", + type_desc, + av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base), + av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base), + av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); + } av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase); if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "encoder -> type:audio " + av_log(NULL, AV_LOG_INFO, "encoder -> type:%s " "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s " "duration:%s duration_time:%s\n", + type_desc, av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base), av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base), av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); } output_packet(of, pkt, ost, 0); + + /* if two pass, output log */ + if (ost->logfile && enc->stats_out) + fprintf(ost->logfile, "%s", enc->stats_out); } - return; -error: - av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n"); - exit_program(1); + av_assert0(0); +} + +static void do_audio_out(OutputFile *of, OutputStream *ost, + AVFrame *frame) +{ + int ret; + + adjust_frame_pts_to_encoder_tb(of, ost, frame); + + if (!check_recording_time(ost)) + return; + + if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0) + frame->pts = ost->sync_opts; + ost->sync_opts = frame->pts + frame->nb_samples; + ost->samples_encoded += frame->nb_samples; + + ret = encode_frame(of, ost, frame); + if (ret < 0) + exit_program(1); } static void do_subtitle_out(OutputFile *of, @@ -969,7 +999,6 @@ static void do_video_out(OutputFile *of, AVFrame *next_picture) { int ret; - AVPacket *pkt = ost->pkt; AVCodecContext *enc = ost->enc_ctx; AVRational frame_rate; int nb_frames, nb0_frames, i; @@ -1168,57 +1197,13 @@ static void do_video_out(OutputFile *of, av_log(NULL, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time); } - update_benchmark(NULL); - if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "encoder <- type:video " - "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n", - av_ts2str(in_picture->pts), av_ts2timestr(in_picture->pts, &enc->time_base), - enc->time_base.num, enc->time_base.den); - } - - ost->frames_encoded++; - - ret = avcodec_send_frame(enc, in_picture); + ret = encode_frame(of, ost, in_picture); if (ret < 0) - goto error; + exit_program(1); + // Make sure Closed Captions will not be duplicated av_frame_remove_side_data(in_picture, AV_FRAME_DATA_A53_CC); - while (1) { - ret = avcodec_receive_packet(enc, pkt); - update_benchmark("encode_video %d.%d", ost->file_index, ost->index); - if (ret == AVERROR(EAGAIN)) - break; - if (ret < 0) - goto error; - - if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "encoder -> type:video " - "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s " - "duration:%s duration_time:%s\n", - av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base), - av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base), - av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); - } - - av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase); - - if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "encoder -> type:video " - "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s " - "duration:%s duration_time:%s\n", - av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->mux_timebase), - av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->mux_timebase), - av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->mux_timebase)); - } - - output_packet(of, pkt, ost, 0); - - /* if two pass, output log */ - if (ost->logfile && enc->stats_out) { - fprintf(ost->logfile, "%s", enc->stats_out); - } - } ost->sync_opts++; ost->frame_number++; } @@ -1226,11 +1211,6 @@ static void do_video_out(OutputFile *of, av_frame_unref(ost->last_frame); if (next_picture) av_frame_move_ref(ost->last_frame, next_picture); - - return; -error: - av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n"); - exit_program(1); } static void finish_output_stream(OutputStream *ost) From patchwork Mon Apr 4 11:30:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35189 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1914775pzb; Mon, 4 Apr 2022 04:38:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy0ItJxngyyVyVmcCdFJxXI1xd3NGUgF2/19/wgAGvTzSJQ6KppHtlGS6iJVThHZxdqck1U X-Received: by 2002:a17:907:7d91:b0:6d7:a1e:a47a with SMTP id oz17-20020a1709077d9100b006d70a1ea47amr10914954ejc.116.1649072316837; Mon, 04 Apr 2022 04:38:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072316; cv=none; d=google.com; s=arc-20160816; b=gEzdgNS04Cucgg+eJqsVErF5daamjcCvaCoTkVEDFvoam5OA6iiSc6/4Bg8cZN/bkd eYX8vqmEE6vNAhH12xqfU21E+IwPDRYRwRTqElIAefMyLHYE+KMFyI24rqzqiX4gOlIT jeZa4tv49Tn+8EdDkg3tVjnCsipfLvb/BWFeKk/zxMETWNV82UhnPfy62j8HowyGTGRl f+aWLk3nBdbaj/HbJw7GI42wRKNPiN2IpnbzMob/UASr5ZPGLmkuGWoy+bWVvUgvSHJY 3/x+NpiN3S6KuBa8FjBdc9TnPMAH5hk+J4sYUwB4N153ZeOx6q38GNGDujphvLWv/Q+C f4vg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=u8a/RGfxUeopdlKueMnS7urxJANvJJHaFaL5NHvEheo=; b=pf7DpAxBRLYwGndsD9B1n0EXCAUbSjj1cUxS7hh5iPIT1LBS96FEhoWCJlJS/b3BPs +lghkzA57JsVR+WM9VdVV8emet/u519nHtEpM4mx93E7L8KDYEI4SuxIcxe9RTL+MoTK 8ugcINZZrLIk0OZksYBDYqgNnq5MqjczBQ7iGL3yQwj/rnCqY4kqGu35rAoG99eARilb 4n9XVdLYxvqVDcRYI8K8ymlr40Sn1Vrf8NL6wiAPerrJwSF6uVBOQid2O896o6e1i4aT IZog0LcXRsSFjX8wl8i1UcD7tE1Ji4zzISnoj05MHpVqHspC+70rgcEQhy4ZRG7PU5M3 DseA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id bk17-20020a170906b0d100b006dff5e2d277si6189626ejb.211.2022.04.04.04.38.36; Mon, 04 Apr 2022 04:38:36 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B344568B300; Mon, 4 Apr 2022 14:37:47 +0300 (EEST) 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 98C1768B299 for ; Mon, 4 Apr 2022 14:37:43 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 3C79124017C for ; Mon, 4 Apr 2022 13:37:43 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id IgfCgzYPVGSR for ; Mon, 4 Apr 2022 13:37:42 +0200 (CEST) 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 9F2C5240506 for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 152AD3A0D20; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:19 +0200 Message-Id: <20220404113037.13070-32-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 31/49] fftools/ffmpeg: reuse the encoding code for flushing encoders 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: rsrPcswqOFDM --- fftools/ffmpeg.c | 72 ++++++++++++------------------------------------ 1 file changed, 18 insertions(+), 54 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 662a5d22b1..adf69cb43e 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -826,12 +826,12 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) AVCodecContext *enc = ost->enc_ctx; AVPacket *pkt = ost->pkt; const char *type_desc = av_get_media_type_string(enc->codec_type); + const char *action = frame ? "encode" : "flush"; int ret; + if (frame) { ost->frames_encoded++; - update_benchmark(NULL); - if (debug_ts) { av_log(NULL, AV_LOG_INFO, "encoder <- type:%s " "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n", @@ -839,9 +839,12 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base), enc->time_base.num, enc->time_base.den); } + } + + update_benchmark(NULL); ret = avcodec_send_frame(enc, frame); - if (ret < 0) { + if (ret < 0 && !(ret == AVERROR_EOF && !frame)) { av_log(NULL, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n", type_desc); return ret; @@ -849,15 +852,20 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) while (1) { ret = avcodec_receive_packet(enc, pkt); - update_benchmark("encode_%s %d.%d", type_desc, + update_benchmark("%s_%s %d.%d", action, type_desc, ost->file_index, ost->index); - if (ret == AVERROR(EAGAIN)) + if (ret == AVERROR(EAGAIN)) { + av_assert0(frame); // should never happen during flushing return 0; - else if (ret < 0) { + } else if (ret < 0 && ret != AVERROR_EOF) { av_log(NULL, AV_LOG_ERROR, "%s encoding failed\n", type_desc); return ret; } + if (ret == AVERROR_EOF) { + output_packet(of, pkt, ost, 1); + return ret; + } else { if (debug_ts) { av_log(NULL, AV_LOG_INFO, "encoder -> type:%s " "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s " @@ -881,6 +889,7 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) } output_packet(of, pkt, ost, 0); + } /* if two pass, output log */ if (ost->logfile && enc->stats_out) @@ -1696,54 +1705,9 @@ static void flush_encoders(void) if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO) continue; - for (;;) { - const char *desc = NULL; - AVPacket *pkt = ost->pkt; - - switch (enc->codec_type) { - case AVMEDIA_TYPE_AUDIO: - desc = "audio"; - break; - case AVMEDIA_TYPE_VIDEO: - desc = "video"; - break; - default: - av_assert0(0); - } - - update_benchmark(NULL); - - while ((ret = avcodec_receive_packet(enc, pkt)) == AVERROR(EAGAIN)) { - ret = avcodec_send_frame(enc, NULL); - if (ret < 0) { - av_log(NULL, AV_LOG_FATAL, "%s encoding failed: %s\n", - desc, - av_err2str(ret)); - exit_program(1); - } - } - - update_benchmark("flush_%s %d.%d", desc, ost->file_index, ost->index); - if (ret < 0 && ret != AVERROR_EOF) { - av_log(NULL, AV_LOG_FATAL, "%s encoding failed: %s\n", - desc, - av_err2str(ret)); - exit_program(1); - } - if (ost->logfile && enc->stats_out) { - fprintf(ost->logfile, "%s", enc->stats_out); - } - if (ret == AVERROR_EOF) { - output_packet(of, pkt, ost, 1); - break; - } - if (ost->finished & MUXER_FINISHED) { - av_packet_unref(pkt); - continue; - } - av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase); - output_packet(of, pkt, ost, 0); - } + ret = encode_frame(of, ost, NULL); + if (ret != AVERROR_EOF) + exit_program(1); } } From patchwork Mon Apr 4 11:30:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35203 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a02:aa92:0:0:0:0:0 with SMTP id u18csp3587437jai; Mon, 4 Apr 2022 04:39:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy34c5H52jIG5oOhWTkz5c/toas6jtzeCs1l3qfDo21kZv7Ge/hWrpBEiM+JGETrptCIErv X-Received: by 2002:a05:6402:2549:b0:419:6545:62af with SMTP id l9-20020a056402254900b00419654562afmr32663220edb.120.1649072352539; Mon, 04 Apr 2022 04:39:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072352; cv=none; d=google.com; s=arc-20160816; b=aTrCEsbYnoJq1jNWLGuMGqniHKZ3YpOOmRHke3rPQXy/ZyzZF9vKFXCkaNBQlUCD7m UY+ojbyrRhipwZ3Dnl9ZAObzVtkbv4bPd8Y2BXeWC/NQOzdUUatnwHVil0bj0+p4d5sZ iUCD3PZFXEMncKwgy+lCu4JuznZil+TR1nAhBBZ3fQRJgw/b6pib6DGIHRFO7U200FXt K27ciw2Jc4T3cUmgoh3jpKXgSvhvwd8fxFvnxTm01DTT4N/R6W0cpJVXd0yoWWYD6VgK Kcoi1xos9B3L5Ym7olFRWQokM+UV2wUv0NgmUUXRv7KWXeIfe8mMNYrdqfgxQDxMsw6U uYuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=4Vk4mWG1WZJm7DdG1jNAhXssk6M0Em7rQMDNIuAQzIM=; b=sH/fLC3lci06T+eOCyAD/DAwEOzGeUJt18xu8eeRZEi7FLG1df0j9B9RqA1Zru+egv sh4E/QkmCsJ3h5CTZzrklnw/wVKA6RT+oWRLz7oJ3rSrBx319ccg/NvwIo22q3MiwaR+ 34s39VtO5hpCUkUwnjrQ7njuQSM8SHVBJ8La06ocxsgcCfjXx1wBcf76B3il3VvrLtgA P/zK29AKLP10vRbcGNojWDh1mAnLZ5eBsuvmD0AfCQFaumVPrpCPQouQQdD2oxa0cT9M VhISlklZO4iGoL1R89gE7+xSJEhfgzYYXouhYrIuRC3ph8jOTAhQFAZnXCnB82n+G8zQ sqvA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id y2-20020a50d8c2000000b00418eee96003si6239268edj.315.2022.04.04.04.39.12; Mon, 04 Apr 2022 04:39:12 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A746B68B311; Mon, 4 Apr 2022 14:37:51 +0300 (EEST) 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 F31A768B2BC for ; Mon, 4 Apr 2022 14:37:44 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id B00112404FE for ; Mon, 4 Apr 2022 13:37:44 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id WSzR8gDDUgDG for ; Mon, 4 Apr 2022 13:37:44 +0200 (CEST) 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 A096C24050B for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 18C4B3A0D21; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:20 +0200 Message-Id: <20220404113037.13070-33-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 32/49] fftools/ffmpeg: reindent after previous commit 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: XRiPCE4ClCGS Content-Length: 4044 --- fftools/ffmpeg.c | 56 ++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index adf69cb43e..e0f15c0f61 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -830,15 +830,15 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) int ret; if (frame) { - ost->frames_encoded++; + ost->frames_encoded++; - if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "encoder <- type:%s " - "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n", - type_desc, - av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base), - enc->time_base.num, enc->time_base.den); - } + if (debug_ts) { + av_log(NULL, AV_LOG_INFO, "encoder <- type:%s " + "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n", + type_desc, + av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base), + enc->time_base.num, enc->time_base.den); + } } update_benchmark(NULL); @@ -866,29 +866,29 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) output_packet(of, pkt, ost, 1); return ret; } else { - if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "encoder -> type:%s " - "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s " - "duration:%s duration_time:%s\n", - type_desc, - av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base), - av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base), - av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); - } + if (debug_ts) { + av_log(NULL, AV_LOG_INFO, "encoder -> type:%s " + "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s " + "duration:%s duration_time:%s\n", + type_desc, + av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base), + av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base), + av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); + } - av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase); + av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase); - if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "encoder -> type:%s " - "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s " - "duration:%s duration_time:%s\n", - type_desc, - av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base), - av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base), - av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); - } + if (debug_ts) { + av_log(NULL, AV_LOG_INFO, "encoder -> type:%s " + "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s " + "duration:%s duration_time:%s\n", + type_desc, + av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base), + av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base), + av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); + } - output_packet(of, pkt, ost, 0); + output_packet(of, pkt, ost, 0); } /* if two pass, output log */ From patchwork Mon Apr 4 11:30:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35193 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1915378pzb; Mon, 4 Apr 2022 04:40:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxNCYZso1fiK+P9wa6JR3A4mmP8mdcfI3X5C25JjrIupKYoBZQJsSeeLm8uQEyjEe+F7Vye X-Received: by 2002:a17:907:86a3:b0:6da:870c:af44 with SMTP id qa35-20020a17090786a300b006da870caf44mr10048063ejc.445.1649072403504; Mon, 04 Apr 2022 04:40:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072403; cv=none; d=google.com; s=arc-20160816; b=EMa5hTCujmlJO1s06bQsLb6rraEI1VR8wLE+kR1tOKP5TCLBRcuLAymTCqo4Ze/ccT 40Q7E1+mkorlkZVjCTP55TLH6KgO+vhVj1hovB9AGAUoYTfRwLFsn4x8LxYDOhm0pV7K MqDeI9ID7t7YY7rIk5lLHrWlOl8cvmaCYLLdxcZ0M2wLMxlCfRxhksJFw1DGRXWvjHTu CW513vtjS/Dlh5yVsGM5n7T/i3MlMHvIs3nR2VcP283V5gYylgQS0MglzsQII3tSCS8W 7xHi8hylLdzpmj0kDoGs1AnzBUCs7diGUYCcx5+nW0wS5NyK81aPc3pWQUaUgGmPAk2z S/hw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=9khHpXqmmx1nLBr1EIp/D0swGodSykYtBPBCSjdSZxw=; b=GQqjeqx0bIorBuaYrMyP3F4D71lalTccVoutuuQxIQhCvICYeJJKBUrtAT/KXi84be wyVrI544q2D4bTl7IUpgSmB9ojE+X4vFwvyfKELF9F1Q3i8BTWQewETsdJUfURHlNbaI PyBTLfR/0N6QPr6KEErC7afkybNbw2ccOoh3QCJ9YOYUb7OELvNhdeOWfre6Cxup4NM5 MUyC6yVaw0fN41Cqe0ERGykcnMbhX1q3wxvd7TCT4A6S4SalkRC3p8UyPlVYaeRWHgpk ZVX0amBnJdYAZz9GUlgBpc8SXymg/D3C+seRyDWUynyhzankKKqraVC/t22psUjuDHmO MISg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id hw13-20020a170907a0cd00b006e0ae9e6ed9si2569333ejc.498.2022.04.04.04.40.03; Mon, 04 Apr 2022 04:40:03 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5CE0068B376; Mon, 4 Apr 2022 14:37:56 +0300 (EEST) 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 1711068B350 for ; Mon, 4 Apr 2022 14:37:46 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id BF6352404FE for ; Mon, 4 Apr 2022 13:37:45 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 5Kw5_1wD50C1 for ; Mon, 4 Apr 2022 13:37:45 +0200 (CEST) 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 B2570240512 for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 1C6053A0D35; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:21 +0200 Message-Id: <20220404113037.13070-34-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 33/49] fftools/ffmpeg: use refcounted packets for encoded subtitles 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: g4w1AEd6cciI --- fftools/ffmpeg.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index e0f15c0f61..07d1fc8a5d 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -141,8 +141,6 @@ unsigned nb_output_dumped = 0; static BenchmarkTimeStamps current_time; AVIOContext *progress_avio = NULL; -static uint8_t *subtitle_out; - InputStream **input_streams = NULL; int nb_input_streams = 0; InputFile **input_files = NULL; @@ -559,8 +557,6 @@ static void ffmpeg_cleanup(int ret) } av_freep(&filtergraphs); - av_freep(&subtitle_out); - /* close files */ for (i = 0; i < nb_output_files; i++) of_close(&output_files[i]); @@ -924,7 +920,7 @@ static void do_subtitle_out(OutputFile *of, AVSubtitle *sub) { int subtitle_out_max_size = 1024 * 1024; - int subtitle_out_size, nb, i; + int subtitle_out_size, nb, i, ret; AVCodecContext *enc; AVPacket *pkt = ost->pkt; int64_t pts; @@ -938,14 +934,6 @@ static void do_subtitle_out(OutputFile *of, enc = ost->enc_ctx; - if (!subtitle_out) { - subtitle_out = av_malloc(subtitle_out_max_size); - if (!subtitle_out) { - av_log(NULL, AV_LOG_FATAL, "Failed to allocate subtitle_out\n"); - exit_program(1); - } - } - /* Note: DVB subtitle need one packet to draw them and one other packet to clear them */ /* XXX: signal it in the codec context ? */ @@ -965,6 +953,12 @@ static void do_subtitle_out(OutputFile *of, if (!check_recording_time(ost)) return; + ret = av_new_packet(pkt, subtitle_out_max_size); + if (ret < 0) { + av_log(NULL, AV_LOG_FATAL, "Failed to allocate subtitle encode buffer\n"); + exit_program(1); + } + sub->pts = pts; // start_display_time is required to be 0 sub->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q); @@ -975,8 +969,7 @@ static void do_subtitle_out(OutputFile *of, ost->frames_encoded++; - subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out, - subtitle_out_max_size, sub); + subtitle_out_size = avcodec_encode_subtitle(enc, pkt->data, pkt->size, sub); if (i == 1) sub->num_rects = save_num_rects; if (subtitle_out_size < 0) { @@ -984,8 +977,6 @@ static void do_subtitle_out(OutputFile *of, exit_program(1); } - av_packet_unref(pkt); - pkt->data = subtitle_out; pkt->size = subtitle_out_size; pkt->pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->mux_timebase); pkt->duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase); From patchwork Mon Apr 4 11:30:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35221 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a02:aa92:0:0:0:0:0 with SMTP id u18csp3587286jai; Mon, 4 Apr 2022 04:38:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzjOEH1ljKpPa8NQua34xiuB4nZXJUZeyNY2/d6qvGpxYVW5j38iM5i63mhd2t25iOzAaH7 X-Received: by 2002:a50:ce03:0:b0:41c:c36b:c75 with SMTP id y3-20020a50ce03000000b0041cc36b0c75mr5555172edi.195.1649072336471; Mon, 04 Apr 2022 04:38:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072336; cv=none; d=google.com; s=arc-20160816; b=f1TfD7UMxQietEWH4KR6W5rlfCu+cGTsQM758WKTUY9AwKZGuW9vXzzMKDMfn/oGx6 5L7DFXVmYKkwIeUnhDn/HooeMcgqZFU+UnNMqV3QzfaGCrFhawYC+p9QuY0iJ4GQrZdJ 6gOmg15jJchGYqYdYFATpwQMV3NaSwt8ftZ1fOg0maArKdjsmh1Znjc27ch05WYP9XPQ Y+Yf2IM5Gb+9sjb4KmH33YDqGunN5se0oc/bDl4mc8BXPn55iY+KL1neoBjT+lT4j/pr srzXQi/41EO6FN/uaj5DsS4JXULoDa6Nf/6FdZ+3Wg7F8rvIqYdPdFPmUSbpw3Czx1/F Vrsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=VuLHu65CmZ9LK8iuTXwoTAT/d3MYpgxY/Dk9jMgm2mU=; b=KwsryJ7KnQQxk3a4jPkxi0D806BfLfKKESs7bD0XQupQ5unC0qpJR3FM0sOxGYS5To 2CLgtjxTVb/XGP0+rDyrpaPvEdG21owyLthYAhBmmVIaUKzfHrESDBrpgFtP6E6TC4Zz 4Eh/ko2bxLzPraTTm/Ss3vDRmzjkF7VJ6g1dyFvQVzHeucJ2bbmQ8l2DhKWv//nRIKo0 eTs/2AaJb9QMtpuPOelhCUdMb4csBxautOLnJOh3OdnHaG4Jc++67ZqNqxHKCbfqayJY 1hz6mlNnTPjgHy7A+YyzuWPAolMUoKb+5/2gcgFbw+9LBTiQNsYsag0KXkk8FslMqbbE b1Nw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id hk19-20020a170906c9d300b006dffb6427bdsi5967621ejb.269.2022.04.04.04.38.56; Mon, 04 Apr 2022 04:38:56 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 33BED68B32A; Mon, 4 Apr 2022 14:37:50 +0300 (EEST) 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 CC53268B2B7 for ; Mon, 4 Apr 2022 14:37:44 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 87CD624017E for ; Mon, 4 Apr 2022 13:37:44 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id ZLeZ_tclA4Ey for ; Mon, 4 Apr 2022 13:37:42 +0200 (CEST) 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 3F5242404FE for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 20B793A0D60; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:22 +0200 Message-Id: <20220404113037.13070-35-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 34/49] fftools/ffmpeg: rework -shortest implementation 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: hx6SwXN8bTEf Content-Length: 38616 The -shortest option (which finishes the output file at the time the shortest stream ends) is currently implemented by faking the -t option when an output stream ends. This approach is fragile, since it depends on the frames/packets being processed in a specific order. E.g. there are currently some situations in which the output file length will depend unpredictably on unrelated factors like encoder delay. More importantly, the present work aiming at splitting various ffmpeg components into different threads will make this approach completely unworkable, since the frames/packets will arrive in effectively random order. This commit introduces a "sync queue", which is essentially a collection of FIFOs, one per stream. Frames/packets are submitted to these FIFOs and are then released for further processing (encoding or muxing) when it is ensured that the frame in question will not make its stream get ahead of the other streams (the logic is similar to libavformat's interleaving queue). These sync queues are then used for encoding and/or muxing when the -shortest option is specifierd. This commit changes the results of the copy-shortest[12] tests, where the last audio frame is now gone. This is correct, since it actually outlasts the last video frame. --- fftools/Makefile | 1 + fftools/ffmpeg.c | 144 +++++++++++--- fftools/ffmpeg.h | 14 +- fftools/ffmpeg_mux.c | 88 ++++++--- fftools/ffmpeg_opt.c | 80 ++++++++ fftools/sync_queue.c | 346 ++++++++++++++++++++++++++++++++++ fftools/sync_queue.h | 93 +++++++++ tests/ref/fate/copy-shortest1 | 1 - tests/ref/fate/copy-shortest2 | 1 - 9 files changed, 712 insertions(+), 56 deletions(-) create mode 100644 fftools/sync_queue.c create mode 100644 fftools/sync_queue.h diff --git a/fftools/Makefile b/fftools/Makefile index 81ad6c4f4f..f015df6846 100644 --- a/fftools/Makefile +++ b/fftools/Makefile @@ -14,6 +14,7 @@ OBJS-ffmpeg += \ fftools/ffmpeg_hw.o \ fftools/ffmpeg_mux.o \ fftools/ffmpeg_opt.o \ + fftools/sync_queue.o \ define DOFFTOOL OBJS-$(1) += fftools/cmdutils.o fftools/opt_common.o fftools/$(1).o $(OBJS-$(1)-yes) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 07d1fc8a5d..8674f52047 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -104,6 +104,7 @@ #include "ffmpeg.h" #include "cmdutils.h" +#include "sync_queue.h" #include "libavutil/avassert.h" @@ -570,6 +571,7 @@ static void ffmpeg_cleanup(int ret) av_bsf_free(&ost->bsf_ctx); av_frame_free(&ost->filtered_frame); + av_frame_free(&ost->sq_frame); av_frame_free(&ost->last_frame); av_packet_free(&ost->pkt); av_dict_free(&ost->encoder_opts); @@ -690,13 +692,10 @@ static void update_benchmark(const char *fmt, ...) static void close_output_stream(OutputStream *ost) { OutputFile *of = output_files[ost->file_index]; - AVRational time_base = ost->stream_copy ? ost->mux_timebase : ost->enc_ctx->time_base; - ost->finished |= ENCODER_FINISHED; - if (of->shortest) { - int64_t end = av_rescale_q(ost->sync_opts - ost->first_pts, time_base, AV_TIME_BASE_Q); - of->recording_time = FFMIN(of->recording_time, end); - } + + if (ost->sq_idx_encode >= 0) + sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL)); } /* @@ -715,17 +714,22 @@ static void output_packet(OutputFile *of, AVPacket *pkt, { int ret = 0; + if (!eof && pkt->dts != AV_NOPTS_VALUE) + ost->last_mux_dts = av_rescale_q(pkt->dts, ost->mux_timebase, AV_TIME_BASE_Q); + /* apply the output bitstream filters */ if (ost->bsf_ctx) { ret = av_bsf_send_packet(ost->bsf_ctx, eof ? NULL : pkt); if (ret < 0) goto finish; while ((ret = av_bsf_receive_packet(ost->bsf_ctx, pkt)) >= 0) - of_submit_packet(of, pkt, ost); + of_submit_packet(of, pkt, ost, 0); + if (ret == AVERROR_EOF) + of_submit_packet(of, pkt, ost, 1); if (ret == AVERROR(EAGAIN)) ret = 0; - } else if (!eof) - of_submit_packet(of, pkt, ost); + } else + of_submit_packet(of, pkt, ost, eof); finish: if (ret < 0 && ret != AVERROR_EOF) { @@ -895,6 +899,52 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) av_assert0(0); } +static int submit_encode_frame(OutputFile *of, OutputStream *ost, + AVFrame *frame) +{ + int ret; + + if (ost->sq_idx_encode < 0) + return encode_frame(of, ost, frame); + + if (frame) { + ret = av_frame_ref(ost->sq_frame, frame); + if (ret < 0) + return ret; + frame = ost->sq_frame; + } + + ret = sq_send(of->sq_encode, ost->sq_idx_encode, + SQFRAME(frame)); + if (ret < 0) { + if (frame) + av_frame_unref(frame); + if (ret != AVERROR_EOF) + return ret; + } + + while (1) { + AVFrame *enc_frame = ost->sq_frame; + + ret = sq_receive(of->sq_encode, ost->sq_idx_encode, + SQFRAME(enc_frame)); + if (ret == AVERROR_EOF) { + enc_frame = NULL; + } else if (ret < 0) { + return (ret == AVERROR(EAGAIN)) ? 0 : ret; + } + + ret = encode_frame(of, ost, enc_frame); + if (enc_frame) + av_frame_unref(enc_frame); + if (ret < 0) { + if (ret == AVERROR_EOF) + close_output_stream(ost); + return ret; + } + } +} + static void do_audio_out(OutputFile *of, OutputStream *ost, AVFrame *frame) { @@ -910,8 +960,8 @@ static void do_audio_out(OutputFile *of, OutputStream *ost, ost->sync_opts = frame->pts + frame->nb_samples; ost->samples_encoded += frame->nb_samples; - ret = encode_frame(of, ost, frame); - if (ret < 0) + ret = submit_encode_frame(of, ost, frame); + if (ret < 0 && ret != AVERROR_EOF) exit_program(1); } @@ -1197,8 +1247,8 @@ static void do_video_out(OutputFile *of, av_log(NULL, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time); } - ret = encode_frame(of, ost, in_picture); - if (ret < 0) + ret = submit_encode_frame(of, ost, in_picture); + if (ret < 0 && ret != AVERROR_EOF) exit_program(1); // Make sure Closed Captions will not be duplicated @@ -1216,14 +1266,12 @@ static void do_video_out(OutputFile *of, static void finish_output_stream(OutputStream *ost) { OutputFile *of = output_files[ost->file_index]; - AVRational time_base = ost->stream_copy ? ost->mux_timebase : ost->enc_ctx->time_base; - - ost->finished = ENCODER_FINISHED | MUXER_FINISHED; + ost->finished = ENCODER_FINISHED; - if (of->shortest) { - int64_t end = av_rescale_q(ost->sync_opts - ost->first_pts, time_base, AV_TIME_BASE_Q); - of->recording_time = FFMIN(of->recording_time, end); - } + if (ost->sq_idx_mux >= 0) + sq_send(of->sq_mux, ost->sq_idx_mux, SQPKT(NULL)); + else + ost->finished |= MUXER_FINISHED; } /** @@ -1281,6 +1329,12 @@ static int reap_filters(int flush) continue; } + if (filtered_frame->pts != AV_NOPTS_VALUE) { + AVRational tb = av_buffersink_get_time_base(filter); + ost->last_filter_pts = av_rescale_q(filtered_frame->pts, tb, + AV_TIME_BASE_Q); + } + switch (av_buffersink_get_type(filter)) { case AVMEDIA_TYPE_VIDEO: if (!ost->frame_aspect_ratio.num) @@ -1696,7 +1750,7 @@ static void flush_encoders(void) if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO) continue; - ret = encode_frame(of, ost, NULL); + ret = submit_encode_frame(of, ost, NULL); if (ret != AVERROR_EOF) exit_program(1); } @@ -3006,6 +3060,9 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) break; } + if (ost->sq_idx_encode >= 0) + sq_set_tb(of->sq_encode, ost->sq_idx_encode, enc_ctx->time_base); + ost->mux_timebase = enc_ctx->time_base; return 0; @@ -3014,6 +3071,7 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) static int init_output_stream(OutputStream *ost, AVFrame *frame, char *error, int error_len) { + OutputFile *of = output_files[ost->file_index]; int ret = 0; if (ost->encoding_needed) { @@ -3146,6 +3204,9 @@ static int init_output_stream(OutputStream *ost, AVFrame *frame, if (ret < 0) return ret; + if (ost->sq_idx_mux >= 0) + sq_set_tb(of->sq_mux, ost->sq_idx_mux, ost->mux_timebase); + ost->initialized = 1; ret = of_check_init(output_files[ost->file_index]); @@ -3377,13 +3438,19 @@ static OutputStream *choose_output(void) for (i = 0; i < nb_output_streams; i++) { OutputStream *ost = output_streams[i]; - int64_t opts = ost->last_mux_dts == AV_NOPTS_VALUE ? INT64_MIN : - av_rescale_q(ost->last_mux_dts, ost->st->time_base, - AV_TIME_BASE_Q); - if (ost->last_mux_dts == AV_NOPTS_VALUE) - av_log(NULL, AV_LOG_DEBUG, - "cur_dts is invalid st:%d (%d) [init:%d i_done:%d finish:%d] (this is harmless if it occurs once at the start per stream)\n", - ost->st->index, ost->st->id, ost->initialized, ost->inputs_done, ost->finished); + int64_t opts; + + if (ost->filter) { + opts = ost->last_filter_pts == AV_NOPTS_VALUE ? + INT64_MIN : ost->last_filter_pts; + } else { + opts = ost->last_mux_dts == AV_NOPTS_VALUE ? + INT64_MIN : ost->last_mux_dts; + if (ost->last_mux_dts == AV_NOPTS_VALUE) + av_log(NULL, AV_LOG_DEBUG, + "cur_dts is invalid st:%d (%d) [init:%d i_done:%d finish:%d] (this is harmless if it occurs once at the start per stream)\n", + ost->st->index, ost->st->id, ost->initialized, ost->inputs_done, ost->finished); + } if (!ost->initialized && !ost->inputs_done) return ost->unavailable ? NULL : ost; @@ -4205,6 +4272,26 @@ static int transcode_step(void) return reap_filters(0); } +static void flush_sync_queues_mux(void) +{ + /* mark all queue inputs as done */ + for (int i = 0; i < nb_output_streams; i++) { + OutputStream *ost = output_streams[i]; + OutputFile *of = output_files[ost->file_index]; + if (ost->sq_idx_mux >= 0) + sq_send(of->sq_mux, ost->sq_idx_mux, SQPKT(NULL)); + } + + /* encode all packets remaining in the sync queues */ + for (int i = 0; i < nb_output_streams; i++) { + OutputStream *ost = output_streams[i]; + OutputFile *of = output_files[ost->file_index]; + + if (!(ost->finished & MUXER_FINISHED)) + output_packet(of, ost->pkt, ost, 1); + } +} + /* * The following code is the main loop of the file converter */ @@ -4266,6 +4353,7 @@ static int transcode(void) } } flush_encoders(); + flush_sync_queues_mux(); term_exit(); diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 604f0145e3..dfda6ccbe9 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -26,6 +26,7 @@ #include #include "cmdutils.h" +#include "sync_queue.h" #include "libavformat/avformat.h" #include "libavformat/avio.h" @@ -464,8 +465,10 @@ typedef struct OutputStream { /* pts of the first frame encoded for this stream, used for limiting * recording time */ int64_t first_pts; - /* dts of the last packet sent to the muxer */ + /* dts of the last packet sent to the muxing queue, in AV_TIME_BASE_Q */ int64_t last_mux_dts; + /* pts of the last frame received from the filters, in AV_TIME_BASE_Q */ + int64_t last_filter_pts; // the timebase of the packets sent to the muxer AVRational mux_timebase; AVRational enc_timebase; @@ -478,6 +481,7 @@ typedef struct OutputStream { int64_t max_frames; AVFrame *filtered_frame; AVFrame *last_frame; + AVFrame *sq_frame; AVPacket *pkt; int last_dropped; int last_nb0_frames[3]; @@ -566,6 +570,9 @@ typedef struct OutputStream { /* frame encode sum of squared error values */ int64_t error[4]; + + int sq_idx_encode; + int sq_idx_mux; } OutputStream; typedef struct Muxer Muxer; @@ -576,6 +583,9 @@ typedef struct OutputFile { Muxer *mux; const AVOutputFormat *format; + SyncQueue *sq_encode; + SyncQueue *sq_mux; + AVFormatContext *ctx; int ost_index; /* index of the first stream in output_streams */ int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units @@ -691,7 +701,7 @@ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); void of_close(OutputFile **pof); -void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost); +void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof); int of_finished(OutputFile *of); int64_t of_filesize(OutputFile *of); AVChapter * const * diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index ccfe31e09a..69f01448b8 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -20,6 +20,7 @@ #include #include "ffmpeg.h" +#include "sync_queue.h" #include "libavutil/fifo.h" #include "libavutil/intreadwrite.h" @@ -41,6 +42,10 @@ typedef struct MuxStream { * Updated when a packet is either pushed or pulled from the queue. */ size_t muxing_queue_data_size; + + /* dts of the last packet sent to the muxer, in the stream timebase + * used for making up missing dts values */ + int64_t last_mux_dts; } MuxStream; struct Muxer { @@ -161,6 +166,7 @@ static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) { + MuxStream *ms = &of->mux->streams[ost->index]; AVFormatContext *s = of->ctx; AVStream *st = ost->st; int ret; @@ -188,21 +194,21 @@ static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) pkt->dts, pkt->pts, ost->file_index, ost->st->index); pkt->pts = - pkt->dts = pkt->pts + pkt->dts + ost->last_mux_dts + 1 - - FFMIN3(pkt->pts, pkt->dts, ost->last_mux_dts + 1) - - FFMAX3(pkt->pts, pkt->dts, ost->last_mux_dts + 1); + pkt->dts = pkt->pts + pkt->dts + ms->last_mux_dts + 1 + - FFMIN3(pkt->pts, pkt->dts, ms->last_mux_dts + 1) + - FFMAX3(pkt->pts, pkt->dts, ms->last_mux_dts + 1); } if ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) && pkt->dts != AV_NOPTS_VALUE && - ost->last_mux_dts != AV_NOPTS_VALUE) { - int64_t max = ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT); + ms->last_mux_dts != AV_NOPTS_VALUE) { + int64_t max = ms->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT); if (pkt->dts < max) { int loglevel = max - pkt->dts > 2 || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG; if (exit_on_error) loglevel = AV_LOG_ERROR; av_log(s, loglevel, "Non-monotonous DTS in output stream " "%d:%d; previous: %"PRId64", current: %"PRId64"; ", - ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts); + ost->file_index, ost->st->index, ms->last_mux_dts, pkt->dts); if (exit_on_error) { av_log(NULL, AV_LOG_FATAL, "aborting.\n"); exit_program(1); @@ -216,7 +222,7 @@ static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) } } } - ost->last_mux_dts = pkt->dts; + ms->last_mux_dts = pkt->dts; ost->data_size += pkt->size; ost->packets_written++; @@ -245,26 +251,10 @@ static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) } } -void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) +static void submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) { - AVStream *st = ost->st; int ret; - /* - * Audio encoders may split the packets -- #frames in != #packets out. - * But there is no reordering, so we can limit the number of output packets - * by simply dropping them here. - * Counting encoded video frames needs to be done separately because of - * reordering, see do_video_out(). - */ - if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) { - if (ost->frame_number >= ost->max_frames) { - av_packet_unref(pkt); - return; - } - ost->frame_number++; - } - if (of->mux->header_written) { write_packet(of, ost, pkt); } else { @@ -277,6 +267,52 @@ void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) } } +void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) +{ + AVStream *st = ost->st; + + if (!eof) { + /* + * Audio encoders may split the packets -- #frames in != #packets out. + * But there is no reordering, so we can limit the number of output packets + * by simply dropping them here. + * Counting encoded video frames needs to be done separately because of + * reordering, see do_video_out(). + */ + if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) { + if (ost->frame_number >= ost->max_frames) { + av_packet_unref(pkt); + return; + } + ost->frame_number++; + } + } + + if (ost->sq_idx_mux >= 0) { + int ret = sq_send(of->sq_mux, ost->sq_idx_mux, + SQPKT(eof ? NULL: pkt)); + if (ret < 0) { + av_packet_unref(pkt); + if (ret == AVERROR_EOF) { + ost->finished |= MUXER_FINISHED; + return; + } else + exit_program(1); + } + + while (1) { + ret = sq_receive(of->sq_mux, -1, SQPKT(pkt)); + if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) + return; + else if (ret < 0) + exit_program(1); + + submit_packet(of, pkt, output_streams[of->ost_index + ret]); + } + } else if (!eof) + submit_packet(of, pkt, ost); +} + static int print_sdp(void) { char sdp[16384]; @@ -447,6 +483,9 @@ void of_close(OutputFile **pof) if (!of) return; + sq_free(&of->sq_encode); + sq_free(&of->sq_mux); + s = of->ctx; mux_free(&of->mux, s ? s->nb_streams : 0); @@ -481,6 +520,7 @@ int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize) ret = AVERROR(ENOMEM); goto fail; } + ms->last_mux_dts = AV_NOPTS_VALUE; } mux->limit_filesize = limit_filesize; diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index adad46de5f..8884a5f9ed 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -30,6 +30,7 @@ #include "ffmpeg.h" #include "cmdutils.h" #include "opt_common.h" +#include "sync_queue.h" #include "libavformat/avformat.h" @@ -1640,6 +1641,7 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e input_streams[source_index]->st->discard = input_streams[source_index]->user_set_discard; } ost->last_mux_dts = AV_NOPTS_VALUE; + ost->last_filter_pts = AV_NOPTS_VALUE; return ost; } @@ -2299,6 +2301,78 @@ static int init_complex_filters(void) return 0; } +static int setup_sync_queues(OutputFile *of, AVFormatContext *oc) +{ + int nb_av_enc = 0, nb_interleaved = 0; + +#define IS_AV_ENC(ost, type) \ + (ost->encoding_needed && (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) +#define IS_INTERLEAVED(type) (type != AVMEDIA_TYPE_ATTACHMENT) + + for (int i = 0; i < oc->nb_streams; i++) { + OutputStream *ost = output_streams[of->ost_index + i]; + enum AVMediaType type = ost->st->codecpar->codec_type; + + ost->sq_idx_encode = -1; + ost->sq_idx_mux = -1; + + nb_interleaved += IS_INTERLEAVED(type); + nb_av_enc += IS_AV_ENC(ost, type); + } + + if (!(nb_interleaved > 1 && of->shortest)) + return 0; + + /* if we have more than one encoded audio/video streams, then we + * synchronize them before encoding */ + if (nb_av_enc > 1) { + of->sq_encode = sq_alloc(SYNC_QUEUE_FRAMES); + if (!of->sq_encode) + return AVERROR(ENOMEM); + + for (int i = 0; i < oc->nb_streams; i++) { + OutputStream *ost = output_streams[of->ost_index + i]; + enum AVMediaType type = ost->st->codecpar->codec_type; + + if (!IS_AV_ENC(ost, type)) + continue; + + ost->sq_idx_encode = sq_add_stream(of->sq_encode); + if (ost->sq_idx_encode < 0) + return ost->sq_idx_encode; + + ost->sq_frame = av_frame_alloc(); + if (!ost->sq_frame) + return AVERROR(ENOMEM); + } + } + + /* if there any additional interleaved streams, then ALL the streams + * are also synchronized before sending them to the muxer */ + if (nb_interleaved > nb_av_enc) { + of->sq_mux = sq_alloc(SYNC_QUEUE_PACKETS); + if (!of->sq_mux) + return AVERROR(ENOMEM); + + for (int i = 0; i < oc->nb_streams; i++) { + OutputStream *ost = output_streams[of->ost_index + i]; + enum AVMediaType type = ost->st->codecpar->codec_type; + + if (!IS_INTERLEAVED(type)) + continue; + + ost->sq_idx_mux = sq_add_stream(of->sq_mux); + if (ost->sq_idx_mux < 0) + return ost->sq_idx_mux; + } + } + +#undef IS_AV_ENC +#undef IS_INTERLEAVED + + return 0; +} + static int open_output_file(OptionsContext *o, const char *filename) { AVFormatContext *oc; @@ -2936,6 +3010,12 @@ loop_end: exit_program(1); } + err = setup_sync_queues(of, oc); + if (err < 0) { + av_log(NULL, AV_LOG_FATAL, "Error setting up output sync queues\n"); + exit_program(1); + } + err = of_muxer_init(of, format_opts, o->limit_filesize); if (err < 0) { av_log(NULL, AV_LOG_FATAL, "Error initializing internal muxing state\n"); diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c new file mode 100644 index 0000000000..cdf6a32cae --- /dev/null +++ b/fftools/sync_queue.c @@ -0,0 +1,346 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include "libavutil/avassert.h" +#include "libavutil/error.h" +#include "libavutil/fifo.h" +#include "libavutil/mathematics.h" +#include "libavutil/mem.h" + +#include "sync_queue.h" + +typedef struct SyncQueueStream { + AVFifo *fifo; + AVRational tb; + int64_t head_ts; + int finished; +} SyncQueueStream; + +struct SyncQueue { + enum SyncQueueType type; + + SyncQueueStream *streams; + unsigned int nb_streams; + + // pool of preallocated frames to avoid constant allocations + SyncQueueFrame free_frames[32]; + unsigned int nb_free_frames; + + /* sync head: the stream with the smallest last timestamp */ + int head_stream; + /* the finished stream with the smallest finish timestamp or -1 */ + int head_finished_stream; + int finished; +}; + +static void frame_free(const SyncQueue *sq, SyncQueueFrame frame) +{ + if (sq->type == SYNC_QUEUE_PACKETS) + av_packet_free(&frame.p); + else + av_frame_free(&frame.f); +} + +static void frame_clear(const SyncQueue *sq, SyncQueueFrame frame) +{ + if (sq->type == SYNC_QUEUE_PACKETS) + av_packet_unref(frame.p); + else + av_frame_unref(frame.f); +} + +static void frame_release(SyncQueue *sq, SyncQueueFrame frame) +{ + if (sq->nb_free_frames < FF_ARRAY_ELEMS(sq->free_frames)) { + frame_clear(sq, frame); + sq->free_frames[sq->nb_free_frames++] = frame; + } else + frame_free(sq, frame); +} + +static int frame_get(SyncQueue *sq, SyncQueueFrame *frame) +{ + if (sq->nb_free_frames) { + *frame = sq->free_frames[--sq->nb_free_frames]; + memset(sq->free_frames + sq->nb_free_frames, 0, sizeof(sq->free_frames[0])); + return 0; + } + if (sq->type == SYNC_QUEUE_PACKETS) { + frame->p = av_packet_alloc(); + return frame->p ? 0 : AVERROR(ENOMEM); + } + frame->f = av_frame_alloc(); + return frame->f ? 0 : AVERROR(ENOMEM); +} + +static void frame_move(const SyncQueue *sq, SyncQueueFrame dst, + SyncQueueFrame src) +{ + if (sq->type == SYNC_QUEUE_PACKETS) + av_packet_move_ref(dst.p, src.p); + else + av_frame_move_ref(dst.f, src.f); +} + +static int64_t frame_ts(const SyncQueue *sq, SyncQueueFrame frame) +{ + return (sq->type == SYNC_QUEUE_PACKETS) ? + frame.p->pts + frame.p->duration : + frame.f->pts + frame.f->pkt_duration; +} + +static int frame_null(const SyncQueue *sq, SyncQueueFrame frame) +{ + return (sq->type == SYNC_QUEUE_PACKETS) ? (frame.p == NULL) : (frame.f == NULL); +} + +static void finish_stream(SyncQueue *sq, unsigned int stream_idx) +{ + SyncQueueStream *st = &sq->streams[stream_idx]; + + st->finished = 1; + + if (st->head_ts != AV_NOPTS_VALUE) { + /* check if this stream is the new finished head */ + if (sq->head_finished_stream < 0 || + av_compare_ts(st->head_ts, st->tb, + sq->streams[sq->head_finished_stream].head_ts, + sq->streams[sq->head_finished_stream].tb) < 0) { + sq->head_finished_stream = stream_idx; + } + + /* mark as finished all streams that should no longer receive new frames, + * due to them being ahead of some finished stream */ + st = &sq->streams[sq->head_finished_stream]; + for (unsigned int i = 0; i < sq->nb_streams; i++) { + SyncQueueStream *st1 = &sq->streams[i]; + if (st != st1 && st1->head_ts != AV_NOPTS_VALUE && + av_compare_ts(st->head_ts, st->tb, st1->head_ts, st1->tb) <= 0) + st1->finished = 1; + } + } + + /* mark the whole queue as finished if all streams are finished */ + for (unsigned int i = 0; i < sq->nb_streams; i++) { + if (!sq->streams[i].finished) + return; + } + sq->finished = 1; +} + +int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame) +{ + SyncQueueStream *st; + SyncQueueFrame dst; + int64_t ts; + int ret; + + av_assert0(stream_idx < sq->nb_streams); + st = &sq->streams[stream_idx]; + + if (frame_null(sq, frame)) { + finish_stream(sq, stream_idx); + return 0; + } + if (st->finished) + return AVERROR_EOF; + + ret = frame_get(sq, &dst); + if (ret < 0) + return ret; + + frame_move(sq, dst, frame); + + ts = frame_ts(sq, dst); + + ret = av_fifo_write(st->fifo, &dst, 1); + if (ret < 0) { + frame_move(sq, frame, dst); + frame_release(sq, dst); + return ret; + } + + /* update this stream's head timestamp */ + if (ts != AV_NOPTS_VALUE && + (st->head_ts == AV_NOPTS_VALUE || st->head_ts < ts)) { + st->head_ts = ts; + + /* if this stream is now ahead of some finished stream, then + * this stream is also finished */ + if (sq->head_finished_stream >= 0 && + av_compare_ts(sq->streams[sq->head_finished_stream].head_ts, + sq->streams[sq->head_finished_stream].tb, + ts, st->tb) <= 0) + st->finished = 1; + + /* update the overall head timestamp if it could have changed */ + if (sq->head_stream < 0 || sq->head_stream == stream_idx) { + sq->head_stream = stream_idx; + for (unsigned int i = 0; i < sq->nb_streams; i++) { + SyncQueueStream *st_head = &sq->streams[sq->head_stream]; + SyncQueueStream *st_other = &sq->streams[i]; + if (st_other->head_ts != AV_NOPTS_VALUE && + av_compare_ts(st_other->head_ts, st_other->tb, + st_head->head_ts, st_head->tb) < 0) + sq->head_stream = i; + } + } + } + + + return 0; +} + +static int frame_receive(SyncQueue *sq, unsigned int stream_idx, + SyncQueueFrame frame) +{ + SyncQueueStream *st_head = sq->head_stream >= 0 ? + &sq->streams[sq->head_stream] : NULL; + SyncQueueStream *st; + + av_assert0(stream_idx < sq->nb_streams); + st = &sq->streams[stream_idx]; + + if (av_fifo_can_read(st->fifo)) { + SyncQueueFrame peek; + int64_t ts; + int cmp = 0; + int overflow = 0; + + av_fifo_peek(st->fifo, &peek, 1, 0); + ts = frame_ts(sq, peek); + + /* check if this stream's tail timestamp is before + * the overall queue head */ + if (ts != AV_NOPTS_VALUE && st_head) + cmp = av_compare_ts(ts, st->tb, st_head->head_ts, st_head->tb); + + /* also release the tail frame if this stream's head timestamp + * is over 10 seconds after the overall queue head */ + if (st->head_ts != AV_NOPTS_VALUE && st_head) { + int64_t head_st = av_rescale_q(st->head_ts, st->tb, AV_TIME_BASE_Q); + int64_t head_queue = av_rescale_q(st_head->head_ts, st_head->tb, + AV_TIME_BASE_Q); + overflow = head_st - head_queue > 10 * AV_TIME_BASE; + } + + /* return frames that are before the head; + * after all inputs are finished we can also return the head itself */ + if (ts == AV_NOPTS_VALUE || cmp < 0 || overflow || + (sq->finished && cmp == 0)) { + frame_move(sq, frame, peek); + frame_release(sq, peek); + av_fifo_drain2(st->fifo, 1); + return 0; + } + } + + return sq->finished ? AVERROR_EOF : AVERROR(EAGAIN); +} + +int sq_receive(SyncQueue *sq, int stream_idx, SyncQueueFrame frame) +{ + int nb_eof = 0; + int ret; + + /* read a frame for a specific stream */ + if (stream_idx >= 0) { + ret = frame_receive(sq, stream_idx, frame); + return (ret < 0) ? ret : stream_idx; + } + + /* read a frame for any stream with available output */ + for (unsigned int i = 0; i < sq->nb_streams; i++) { + ret = frame_receive(sq, i, frame); + if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) { + nb_eof += ret == AVERROR_EOF; + continue; + } + return (ret < 0) ? ret : i; + } + + return (nb_eof == sq->nb_streams) ? AVERROR_EOF : AVERROR(EAGAIN); +} + +int sq_add_stream(SyncQueue *sq) +{ + SyncQueueStream *tmp, *st; + + tmp = av_realloc_array(sq->streams, sq->nb_streams + 1, sizeof(*sq->streams)); + if (!tmp) + return AVERROR(ENOMEM); + sq->streams = tmp; + + st = &sq->streams[sq->nb_streams]; + memset(st, 0, sizeof(*st)); + + st->fifo = av_fifo_alloc2(1, sizeof(SyncQueueFrame), AV_FIFO_FLAG_AUTO_GROW); + if (!st->fifo) + return AVERROR(ENOMEM); + + st->head_ts = AV_NOPTS_VALUE; + + return sq->nb_streams++; +} + +void sq_set_tb(SyncQueue *sq, unsigned int stream_idx, AVRational tb) +{ + av_assert0(stream_idx < sq->nb_streams); + sq->streams[stream_idx].tb = tb; +} + +SyncQueue *sq_alloc(enum SyncQueueType type) +{ + SyncQueue *sq = av_mallocz(sizeof(*sq)); + + if (!sq) + return NULL; + + sq->type = type; + + sq->head_stream = -1; + sq->head_finished_stream = -1; + + return sq; +} + +void sq_free(SyncQueue **psq) +{ + SyncQueue *sq = *psq; + + if (!sq) + return; + + for (unsigned int i = 0; i < sq->nb_streams; i++) { + SyncQueueFrame frame; + while (av_fifo_read(sq->streams[i].fifo, &frame, 1) >= 0) + frame_free(sq, frame); + + av_fifo_freep2(&sq->streams[i].fifo); + } + + av_freep(&sq->streams); + + for (unsigned int i = 0; i < sq->nb_free_frames; i++) + frame_free(sq, sq->free_frames[i]); + + av_freep(psq); +} diff --git a/fftools/sync_queue.h b/fftools/sync_queue.h new file mode 100644 index 0000000000..4a16ae3e62 --- /dev/null +++ b/fftools/sync_queue.h @@ -0,0 +1,93 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef FFTOOLS_SYNC_QUEUE_H +#define FFTOOLS_SYNC_QUEUE_H + +#include "libavcodec/packet.h" + +#include "libavutil/frame.h" + +enum SyncQueueType { + SYNC_QUEUE_PACKETS, + SYNC_QUEUE_FRAMES, +}; + +typedef union SyncQueueFrame { + AVFrame *f; + AVPacket *p; +} SyncQueueFrame; + +#define SQFRAME(frame) ((SyncQueueFrame){ .f = (frame) }) +#define SQPKT(pkt) ((SyncQueueFrame){ .p = (pkt) }) + +typedef struct SyncQueue SyncQueue; + +SyncQueue *sq_alloc(enum SyncQueueType type); +void sq_free(SyncQueue **sq); + +/** + * Add a new stream to the sync queue. + * + * @return + * - a non-negative stream index on success + * - a negative error code on error + */ +int sq_add_stream(SyncQueue *sq); + +/** + * Set the timebase for the stream with index stream_idx. Should be called + * before sending any frames for this stream. + */ +void sq_set_tb(SyncQueue *sq, unsigned int stream_idx, AVRational tb); + +/** + * Submit a frame for the stream with index stream_idx. + * + * On success, the sync queue takes ownership of the frame and will reset the + * contents of the supplied frame. On failure, the frame remains owned by the + * caller. + * + * Sending a frame with NULL contents marks the stream as finished. + * + * @return + * - 0 on success + * - AVERROR_EOF when no more frames should be submitted for this stream + * - another a negative error code on failure + */ +int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame); + +/** + * Read a frame from the queue. + * + * @param stream_idx index of the stream to read a frame for. May be -1, then + * try to read a frame from any stream that is ready for + * output. + * @param frame output frame will be written here on success. The frame is owned + * by the caller. + * + * @return + * - a non-negative index of the stream to which the returned frame belongs + * - AVERROR(EAGAIN) when more frames need to be submitted to the queue + * - AVERROR_EOF when no more frames will be available for this stream (for any + * stream if stream_idx is -1) + * - another negative error code on failure + */ +int sq_receive(SyncQueue *sq, int stream_idx, SyncQueueFrame frame); + +#endif // FFTOOLS_SYNC_QUEUE_H diff --git a/tests/ref/fate/copy-shortest1 b/tests/ref/fate/copy-shortest1 index 5038973e4e..87bee4c41f 100644 --- a/tests/ref/fate/copy-shortest1 +++ b/tests/ref/fate/copy-shortest1 @@ -120,4 +120,3 @@ 0, 98304, 98304, 2048, 11182, e35a2ab846029effdbca0e43639717f2 1, 85760, 85760, 1536, 418, cf52ea7fc69e4c5bc8f75b354dfe60af 0, 100352, 100352, 2048, 1423, f480272c7d0b97834bc8ea36cceca61d -1, 87296, 87296, 1536, 418, 78ab22657a1b6c8a0e5b8612ceb8081d diff --git a/tests/ref/fate/copy-shortest2 b/tests/ref/fate/copy-shortest2 index 5038973e4e..87bee4c41f 100644 --- a/tests/ref/fate/copy-shortest2 +++ b/tests/ref/fate/copy-shortest2 @@ -120,4 +120,3 @@ 0, 98304, 98304, 2048, 11182, e35a2ab846029effdbca0e43639717f2 1, 85760, 85760, 1536, 418, cf52ea7fc69e4c5bc8f75b354dfe60af 0, 100352, 100352, 2048, 1423, f480272c7d0b97834bc8ea36cceca61d -1, 87296, 87296, 1536, 418, 78ab22657a1b6c8a0e5b8612ceb8081d From patchwork Mon Apr 4 11:30:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35185 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1914357pzb; Mon, 4 Apr 2022 04:37:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxoc6tn0hXcWjKZHjjcfN8qTkIKP5Kj59KWta12fNLUiW8Ysv1SVvCCCt+JOxRWIkhvyjhp X-Received: by 2002:aa7:c3d6:0:b0:419:2370:4856 with SMTP id l22-20020aa7c3d6000000b0041923704856mr32725308edr.180.1649072269328; Mon, 04 Apr 2022 04:37:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072269; cv=none; d=google.com; s=arc-20160816; b=n9UvOuHYDd7hvu2SnndKpTK3uu5+wvQOU3NqW/xkVoTOhRL63GLq9YM5+pA08IspVk 33vWab1v/wyRk8iEDSyDm3kqjtCBQgmJdfX6rELniWB2Q4dP3ZXAWiqxMOZGYU7luzgc ljTjhKzCr4XF/95ByGVGA4FzvVrUavF1XBOuhJtqqwzzu9DEbwMWxybvzUhtN574Ve+9 DwWpQeaJIZQeHqIKebLdamQ+86KQaB3BhXoRwIm28nPYIdVAPBT3W59OyMlmNGeaWKuv yrVaJ+XtlfC6/wB0bnpEEVsfpk5xZoqZabD+OBCqnWHSQfAbpTDMsNm5r1dmKnc6EIgI 7xkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=Hr56+eXWqrRq3+qeYhUWa/AhQ0rTWr3g06PqZQ20tzo=; b=AN1uA/ioT3uOswgVU/1ukdBSpaLcnGCFPtZQDKTovzDdkaDuIxAFh+qwe9tOfNJUtE 17tzCD0jGX6JjOHvNTWfpU9bKv9cgRGsdb9855TPyeqzVOa4Pg/rZ63b6wXPuP0D23UV EsBwmfQpyKNQ1gQibQAl5QQOLO0I+FcQDpjCcC9UNRr+jw8d/pbKY9Z69dV9VFTYXEQ6 mp/FfIhLO2qN3kAuHYcdVy6FiDYnyq8SSW8ZcOOc8gKi87F0Jl0CztHQu4rw+CVeZWKC nwrmanwr18lUlBSJlSxTwPq4WAhVMksbxJQivYgbav7jAPoTzDiYG8SNmfPA0dDeEApZ xZkQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id u26-20020a17090657da00b006df7b230bfcsi6132879ejr.182.2022.04.04.04.37.49; Mon, 04 Apr 2022 04:37:49 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A623468B2B5; Mon, 4 Apr 2022 14:37:43 +0300 (EEST) 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 5145768B29B for ; Mon, 4 Apr 2022 14:37:42 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 0572F240593 for ; Mon, 4 Apr 2022 13:37:42 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id JIpiHCnkpGnz for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) 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 33FDF2400F5 for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 245C53A0D98; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:23 +0200 Message-Id: <20220404113037.13070-36-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 35/49] fftools/ffmpeg: use the sync queues to handle -frames 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 3ydhTen6SHJh Same issues apply to it as to -shortest. Changes the results of the following tests: - matroska-flac-extradata-update The test reencodes two input FLAC streams into three output FLAC streams. The last output stream is limited to 8 frames. The current code results in the first two output streams having 12 frames, after this commit all three streams have 8 frames and are the same length. This new result is better, since it is predictable. - mkv-1242 The test streamcopies one video and one audio stream, video is limited to 11 frames. The new result shortens the audio stream so that it is not longer than the video. --- fftools/ffmpeg.c | 6 ------ fftools/ffmpeg_mux.c | 10 +--------- fftools/ffmpeg_opt.c | 18 +++++++++++++++--- fftools/sync_queue.c | 13 +++++++++++++ fftools/sync_queue.h | 7 +++++++ tests/ref/fate/matroska-flac-extradata-update | 16 +++++----------- tests/ref/fate/mkv-1242 | 3 --- 7 files changed, 41 insertions(+), 32 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 8674f52047..e46554ddf4 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3412,12 +3412,6 @@ static int need_output(void) if (ost->finished || of_finished(of)) continue; - if (ost->frame_number >= ost->max_frames) { - int j; - for (j = 0; j < of->ctx->nb_streams; j++) - close_output_stream(output_streams[of->ost_index + j]); - continue; - } return 1; } diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 69f01448b8..11d5a0a885 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -273,19 +273,11 @@ void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) if (!eof) { /* - * Audio encoders may split the packets -- #frames in != #packets out. - * But there is no reordering, so we can limit the number of output packets - * by simply dropping them here. * Counting encoded video frames needs to be done separately because of * reordering, see do_video_out(). */ - if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) { - if (ost->frame_number >= ost->max_frames) { - av_packet_unref(pkt); - return; - } + if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) ost->frame_number++; - } } if (ost->sq_idx_mux >= 0) { diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 8884a5f9ed..ac0b4fc7d8 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2304,6 +2304,7 @@ static int init_complex_filters(void) static int setup_sync_queues(OutputFile *of, AVFormatContext *oc) { int nb_av_enc = 0, nb_interleaved = 0; + int limit_frames = 0, limit_frames_av_enc = 0; #define IS_AV_ENC(ost, type) \ (ost->encoding_needed && (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) @@ -2318,14 +2319,19 @@ static int setup_sync_queues(OutputFile *of, AVFormatContext *oc) nb_interleaved += IS_INTERLEAVED(type); nb_av_enc += IS_AV_ENC(ost, type); + + limit_frames |= ost->max_frames < INT64_MAX; + limit_frames_av_enc |= (ost->max_frames < INT64_MAX) && IS_AV_ENC(ost, type); } - if (!(nb_interleaved > 1 && of->shortest)) + if (!((nb_interleaved > 1 && of->shortest) || + (nb_interleaved > 0 && limit_frames))) return 0; - /* if we have more than one encoded audio/video streams, then we + /* if we have more than one encoded audio/video streams, or at least + * one encoded audio/video stream is frame-limited, then we * synchronize them before encoding */ - if (nb_av_enc > 1) { + if ((of->shortest && nb_av_enc > 1) || limit_frames_av_enc) { of->sq_encode = sq_alloc(SYNC_QUEUE_FRAMES); if (!of->sq_encode) return AVERROR(ENOMEM); @@ -2344,6 +2350,9 @@ static int setup_sync_queues(OutputFile *of, AVFormatContext *oc) ost->sq_frame = av_frame_alloc(); if (!ost->sq_frame) return AVERROR(ENOMEM); + + if (ost->max_frames != INT64_MAX) + sq_limit_frames(of->sq_encode, ost->sq_idx_encode, ost->max_frames); } } @@ -2364,6 +2373,9 @@ static int setup_sync_queues(OutputFile *of, AVFormatContext *oc) ost->sq_idx_mux = sq_add_stream(of->sq_mux); if (ost->sq_idx_mux < 0) return ost->sq_idx_mux; + + if (ost->max_frames != INT64_MAX) + sq_limit_frames(of->sq_mux, ost->sq_idx_mux, ost->max_frames); } } diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c index cdf6a32cae..8bfdcbab4d 100644 --- a/fftools/sync_queue.c +++ b/fftools/sync_queue.c @@ -32,6 +32,9 @@ typedef struct SyncQueueStream { AVRational tb; int64_t head_ts; int finished; + + uint64_t frames_sent; + uint64_t frames_max; } SyncQueueStream; struct SyncQueue { @@ -205,6 +208,9 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame) } } + st->frames_sent++; + if (st->frames_sent >= st->frames_max) + finish_stream(sq, stream_idx); return 0; } @@ -297,6 +303,7 @@ int sq_add_stream(SyncQueue *sq) return AVERROR(ENOMEM); st->head_ts = AV_NOPTS_VALUE; + st->frames_max = UINT64_MAX; return sq->nb_streams++; } @@ -307,6 +314,12 @@ void sq_set_tb(SyncQueue *sq, unsigned int stream_idx, AVRational tb) sq->streams[stream_idx].tb = tb; } +void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, uint64_t frames) +{ + av_assert0(stream_idx < sq->nb_streams); + sq->streams[stream_idx].frames_max = frames; +} + SyncQueue *sq_alloc(enum SyncQueueType type) { SyncQueue *sq = av_mallocz(sizeof(*sq)); diff --git a/fftools/sync_queue.h b/fftools/sync_queue.h index 4a16ae3e62..9afd067c25 100644 --- a/fftools/sync_queue.h +++ b/fftools/sync_queue.h @@ -56,6 +56,13 @@ int sq_add_stream(SyncQueue *sq); */ void sq_set_tb(SyncQueue *sq, unsigned int stream_idx, AVRational tb); +/** + * Limit the number of output frames for stream with index stream_idx + * to max_frames. + */ +void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, + uint64_t max_frames); + /** * Submit a frame for the stream with index stream_idx. * diff --git a/tests/ref/fate/matroska-flac-extradata-update b/tests/ref/fate/matroska-flac-extradata-update index b0276f734d..84dcc9c1d0 100644 --- a/tests/ref/fate/matroska-flac-extradata-update +++ b/tests/ref/fate/matroska-flac-extradata-update @@ -1,8 +1,8 @@ -56ff5763fd81ad3bc02c22402cd685e2 *tests/data/fate/matroska-flac-extradata-update.matroska -2008 tests/data/fate/matroska-flac-extradata-update.matroska -#extradata 0: 34, 0x7acb09e7 -#extradata 1: 34, 0x7acb09e7 -#extradata 2: 34, 0x443402dd +8ec02dffd603f44e08b2ae3b81a0d5a0 *tests/data/fate/matroska-flac-extradata-update.matroska +1816 tests/data/fate/matroska-flac-extradata-update.matroska +#extradata 0: 34, 0x93650c81 +#extradata 1: 34, 0x93650c81 +#extradata 2: 34, 0x93650c81 #tb 0: 1/1000 #media_type 0: audio #codec_id 0: flac @@ -42,9 +42,3 @@ 0, 672, 672, 96, 26, 0x50dd042e 1, 672, 672, 96, 26, 0x50dd042e 2, 672, 672, 96, 26, 0x50dd042e -0, 768, 768, 96, 26, 0x53de0499 -1, 768, 768, 96, 26, 0x53de0499 -0, 864, 864, 96, 26, 0x53df04b4 -1, 864, 864, 96, 26, 0x53df04b4 -0, 960, 960, 42, 26, 0x5740044b -1, 960, 960, 42, 26, 0x5740044b diff --git a/tests/ref/fate/mkv-1242 b/tests/ref/fate/mkv-1242 index e025701093..1d1a227832 100644 --- a/tests/ref/fate/mkv-1242 +++ b/tests/ref/fate/mkv-1242 @@ -42,6 +42,3 @@ 1, 383, 383, 21, 325, 0xcd7a9fd6 1, 404, 404, 22, 359, 0x6edeb91c 1, 426, 426, 21, 333, 0xb8999fb7 -1, 447, 447, 21, 317, 0xf2589e1a -1, 468, 468, 21, 319, 0x82ed9572 -1, 489, 489, 22, 473, 0xea54e696 From patchwork Mon Apr 4 11:30:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35186 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1914481pzb; Mon, 4 Apr 2022 04:38:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxLtuMn4qhnPiPryr/2sBQrBHuP7G9723zy/6qvZckKISY2BqnuqGhvR41/+kRG6aR2rb3B X-Received: by 2002:a17:907:3e1c:b0:6e4:e1b3:5384 with SMTP id hp28-20020a1709073e1c00b006e4e1b35384mr10295248ejc.700.1649072282017; Mon, 04 Apr 2022 04:38:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072282; cv=none; d=google.com; s=arc-20160816; b=bsb0/zqR7cl20iHtvMV/+jLXRMuyRZO6cJ1Yxfv5p3eVqkX87MaT3EINhuXhzZ2I6y AI3xNUMlxBMlDrQM0LNNjLnKi9IwnNLmeCp5b6NPmWXInMaP70MI8WtO1jBy3GtgCS1i NXi+mGgBP5bSj06n+JOiY31Q6it4TltbqMpR46VqXWADg4MVudeQGy23/NCFAWYMOGqe mDTaVeLtGyyGMkolOZK7nV24q+5MmccHAHJ+PrhDGabyWbzUSt5wZFxalVM7P+OIDaFr av/w25nWokQl72eoFjmPlBQtjaExS1eXY3w4TCp/IBBt8fCxC3JybAFQkjICj8ksERZL eauA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=uK6OfcKb8itZRuJiauZ1thtFCmy8+2110376OZEqUZA=; b=ED8odbTCl2KD5OzL+2wwqavfdBMrVifdtSNxRvXtz7M8+wbzdO//aJACnhA+dS0Nw8 o4X/DSwEc/AktKtwh9Bb01UPqikEDr61ZR/Pz9XE6oKjW8AKdwNUUusqm+w1hQS1AZbr Ewi+sO/GPuUCKI+fpvambJCbILKFjRP8IWA0y5RJBm3VhlGqQOILvRow1j+DAbkAroxh Na0GMOntxq8sdv7i4T2y4KLaoFpGm3qZn08vnJJ6O72ifQQ+jP7tkuyuUd76i+J9d+aG h+idfZIikAG9ipHjBGm8kiyzfeCV0DRt5HVW+umNT9AADXxUIX0BYGy6Top1BmC/ZHeU p3Og== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id v23-20020a1709062f1700b006df76385c10si6509922eji.176.2022.04.04.04.38.01; Mon, 04 Apr 2022 04:38:02 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B00D268B2BF; Mon, 4 Apr 2022 14:37:44 +0300 (EEST) 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 507FE68B299 for ; Mon, 4 Apr 2022 14:37:42 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id B9FFB240590 for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id IwaPiwWtH0yi for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) 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 3A38B240179 for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 27CDF3A0DA7; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:24 +0200 Message-Id: <20220404113037.13070-37-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 36/49] fftools/ffmpeg: stop using OutputStream.frame_number in print_report() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 6rfqV+cMIccK This field means different things for video encoding (number of frames emitted to the encoding sync queue/encoder by the video sync code) and for everything else (number of packets sent to the muxer sync queue). Print the value of packets_written instead, which means the same thing for all kinds of streams. It is also more consistent with the code below, which prints PSNR from the last muxed packet. --- fftools/ffmpeg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index e46554ddf4..53b3657ec9 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1531,7 +1531,7 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) { float fps; - frame_number = ost->frame_number; + frame_number = ost->packets_written; fps = t > 1 ? frame_number / t : 0; av_bprintf(&buf, "frame=%5d fps=%3.*f q=%3.1f ", frame_number, fps < 9.95, fps, q); From patchwork Mon Apr 4 11:30:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35188 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1914695pzb; Mon, 4 Apr 2022 04:38:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxbGSR9y1GdYR7J212AGgo/PVSBbHUzQCBhVtgjlCvq4dU+uWhrd2KElpf/H7pd891GkGwu X-Received: by 2002:a05:6402:d0e:b0:418:f011:275e with SMTP id eb14-20020a0564020d0e00b00418f011275emr32238314edb.323.1649072305206; Mon, 04 Apr 2022 04:38:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072305; cv=none; d=google.com; s=arc-20160816; b=m0cpwLkbRs4ChT2z+rSsLm4WlNKUyYXLRjUjjHWb2eii71cqbNe3yNseh5aVqtIl+P /aRHeT276z8RrSB+pOrJ9a7FgDbiQpBo2CljrHomCGoz95PaDDl10wwMWMAClnc5WdVU Hr6sza9XkGB1jEx5n9n+/kItCObhY5dlHm2nHWM9Se3zEkMpBuaXqwk0Wj57OvHbXjc2 V1GUKzpigdUy1PeLT3bQWnPnSBhVSCXxkd6Fma1wNt/w2/59uzOPFI15YEyiGd3Cu0ob FZbHxKNAT+NnC5VBOKMMstIT1fTq7w6YHovDURk/dVHHKWpdXWai2R95Kf9fJ5Chx1tO edVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=PM426Es02WnST6hVsb5AsyP2cz3qjJyaDbhMqeEQzOk=; b=VLhlWG47xcO3sAgM49AFpwlle2DxC27fWKJXJJtuu0WQOd2J6c7HPkKp3kWidltt7W 4vYYyG3zOvwj9EjJyGTnoxlka/rgFvPdZwcNs3XJUJh839/ep31UsGp4I5TWkiBmtLOV ylNDHKcpmQU7xlc3mIiIei8m/zEOoj9oEADr+7kl+vXsU/McyIJPVxkKeXAlHW60iKQv 2t5aSHTKmzVw7wHB+aPZHkei+OoCi/Xot5KIEiYEaGb6u1Rm+9LX+ttoQcAC0KcDjqRu 2x2zHTAqrlDZ/NXVAChREdjzvZ9oZ4NW7cCTqEU7YO+O2/qrG2GceVs34nBCq8GvhgAA PwxQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id h13-20020a170906854d00b006dffb2d6bdcsi6188130ejy.83.2022.04.04.04.38.24; Mon, 04 Apr 2022 04:38:25 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B208B68B2E8; Mon, 4 Apr 2022 14:37:46 +0300 (EEST) 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 D6AC468B29B for ; Mon, 4 Apr 2022 14:37:42 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 8C1F724017C for ; Mon, 4 Apr 2022 13:37:42 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id qGTKmtSzic3L for ; Mon, 4 Apr 2022 13:37:42 +0200 (CEST) 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 3E4EA24017E for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 2B63A3A0DEF; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:25 +0200 Message-Id: <20220404113037.13070-38-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 37/49] fftools/ffmpeg: only set OutputStream.frame_number for video encoding 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: wDuLtKahz58/ It is unused otherwise. Rename the field to vsync_frame_number to better reflect its current purpose. --- fftools/ffmpeg.c | 10 +++++----- fftools/ffmpeg.h | 3 ++- fftools/ffmpeg_mux.c | 11 ----------- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 53b3657ec9..701baa3a28 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1108,7 +1108,7 @@ static void do_video_out(OutputFile *of, switch (ost->vsync_method) { case VSYNC_VSCFR: - if (ost->frame_number == 0 && delta0 >= 0.5) { + if (ost->vsync_frame_number == 0 && delta0 >= 0.5) { av_log(NULL, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0)); delta = duration; delta0 = 0; @@ -1116,7 +1116,7 @@ static void do_video_out(OutputFile *of, } case VSYNC_CFR: // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c - if (frame_drop_threshold && delta < frame_drop_threshold && ost->frame_number) { + if (frame_drop_threshold && delta < frame_drop_threshold && ost->vsync_frame_number) { nb_frames = 0; } else if (delta < -1.1) nb_frames = 0; @@ -1146,7 +1146,7 @@ static void do_video_out(OutputFile *of, * But there may be reordering, so we can't throw away frames on encoder * flush, we need to limit them here, before they go into encoder. */ - nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number); + nb_frames = FFMIN(nb_frames, ost->max_frames - ost->vsync_frame_number); nb0_frames = FFMIN(nb0_frames, nb_frames); memmove(ost->last_nb0_frames + 1, @@ -1158,7 +1158,7 @@ static void do_video_out(OutputFile *of, nb_frames_drop++; av_log(NULL, AV_LOG_VERBOSE, "*** dropping frame %d from stream %d at ts %"PRId64"\n", - ost->frame_number, ost->st->index, ost->last_frame->pts); + ost->vsync_frame_number, ost->st->index, ost->last_frame->pts); } if (nb_frames > (nb0_frames && ost->last_dropped) + (nb_frames > nb0_frames)) { if (nb_frames > dts_error_threshold * 30) { @@ -1255,7 +1255,7 @@ static void do_video_out(OutputFile *of, av_frame_remove_side_data(in_picture, AV_FRAME_DATA_A53_CC); ost->sync_opts++; - ost->frame_number++; + ost->vsync_frame_number++; } av_frame_unref(ost->last_frame); diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index dfda6ccbe9..962867b783 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -457,7 +457,8 @@ typedef struct OutputStream { int source_index; /* InputStream index */ AVStream *st; /* stream in the output file */ int encoding_needed; /* true if encoding needed for this stream */ - int frame_number; + /* number of frames emitted by the video-encoding sync code */ + int vsync_frame_number; /* input pts and corresponding output pts for A/V sync */ struct InputStream *sync_ist; /* input stream to sync against */ diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 11d5a0a885..1d8827277d 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -269,17 +269,6 @@ static void submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) { - AVStream *st = ost->st; - - if (!eof) { - /* - * Counting encoded video frames needs to be done separately because of - * reordering, see do_video_out(). - */ - if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) - ost->frame_number++; - } - if (ost->sq_idx_mux >= 0) { int ret = sq_send(of->sq_mux, ost->sq_idx_mux, SQPKT(eof ? NULL: pkt)); From patchwork Mon Apr 4 11:30:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35187 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1914600pzb; Mon, 4 Apr 2022 04:38:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz7IpmBMfKrP58UeYVu0UMx3G9MRbDPdIJ19Z4WPHM3FwNqFLrxa+WIQgFD6bxtsLKXamEi X-Received: by 2002:a05:6402:202a:b0:41c:d9b0:e519 with SMTP id ay10-20020a056402202a00b0041cd9b0e519mr2147408edb.361.1649072294083; Mon, 04 Apr 2022 04:38:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072294; cv=none; d=google.com; s=arc-20160816; b=Z1CfNgNuXRHS73+5v0J8lQStDELv/nsDkzyQlUayOXdn79v6J+trBG/ewokT1sK73j zqEgUvnsIA3fC5t/H0McZy9rf9pkoPLLIlZPrsrtofbl5mF/SbMiVbyIeW8Dk9kxqQYg FZ9ZNw72Dw3GLLNgb06uoWvHJcXm/Rsvbz6mJGmV9LqawoOmD61kYuZp0LEoAb0QyO7T TIweQmmC8tHRLLrFHO8wQOdj9ccJfHTKEOlvlyN/OBvt2vCwkpY6USwb0OnPjPGIKAkD QoFF6jqTuGbJ75s3pbbfJRo2t18HjQYmQMaOTQxidb5afVUDjmchKyFPNl7T0LzpX0Mt 7YCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=ZxXA84/qllbHs2XuVc8cCWAsb8XZZWxx1zK02AD827w=; b=EbrLkXixOeYyoTwoHJyqcz/AcWHVixdYTTmYiOsO7gKk2u4QWll4JkA6DItp9m4Jyr dw+RvkxpBRsG3orUMf8qxPb369ZOYUujWG7hFRQsJxTpG6o7roO5K4Fe/tjytFwM0qmp b0+lyMJ9yFOJm0qQdm52HRGDH+z3Y3x8SvTHNejEtrZUmUiFv5AS5pzIrZhGFmzPzShT Sb6frbMMJVTYBrbut/O2fZkRqMqicPzan8AdqnVA4B8PMU62HCXy2SEWDKDQCYCPKLVM wmzbhoVByRm1M9w1EE9pnU+Ria9hJX61GpaJvFj+RDCGi3eAe/jOf4zdAfP+kJOucLK8 K1uA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id o19-20020a1709062e9300b006e7ee995c94si1625467eji.924.2022.04.04.04.38.13; Mon, 04 Apr 2022 04:38:14 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 988A768B2CE; Mon, 4 Apr 2022 14:37:45 +0300 (EEST) 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 D2AA168B299 for ; Mon, 4 Apr 2022 14:37:42 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 7290A240590 for ; Mon, 4 Apr 2022 13:37:42 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id ZCwABNhSxWF0 for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) 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 3DD1C24017C for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 2F2193A0DF7; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:26 +0200 Message-Id: <20220404113037.13070-39-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 38/49] fftools/ffmpeg: make the muxer AVFormatContext private to ffmpeg_mux.c 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: cGnGFWEiRJGc Since the muxer will operate in a separate thread in the future, the muxer context should not be accessed from the outside. --- fftools/ffmpeg.c | 4 +-- fftools/ffmpeg.h | 6 ++-- fftools/ffmpeg_filter.c | 6 ++-- fftools/ffmpeg_mux.c | 80 +++++++++++++++++++++++++---------------- fftools/ffmpeg_opt.c | 6 ++-- 5 files changed, 62 insertions(+), 40 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 701baa3a28..7e9ac943de 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1442,9 +1442,9 @@ static void print_final_stats(int64_t total_size) uint64_t total_packets = 0, total_size = 0; av_log(NULL, AV_LOG_VERBOSE, "Output file #%d (%s):\n", - i, of->ctx->url); + i, of->url); - for (j = 0; j < of->ctx->nb_streams; j++) { + for (j = 0; j < of->nb_streams; j++) { OutputStream *ost = output_streams[of->ost_index + j]; enum AVMediaType type = ost->enc_ctx->codec_type; diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 962867b783..2ba58b492f 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -583,11 +583,12 @@ typedef struct OutputFile { Muxer *mux; const AVOutputFormat *format; + const char *url; SyncQueue *sq_encode; SyncQueue *sq_mux; - AVFormatContext *ctx; + int nb_streams; int ost_index; /* index of the first stream in output_streams */ int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units @@ -696,7 +697,8 @@ int hw_device_setup_for_filter(FilterGraph *fg); int hwaccel_decode_init(AVCodecContext *avctx); -int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize); +int of_muxer_init(OutputFile *of, AVFormatContext *fc, + AVDictionary *opts, int64_t limit_filesize); /* open the muxer when all the streams are initialized */ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c index 0845c631a5..122b17686a 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -603,11 +603,11 @@ static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, if (ost->apad && of->shortest) { int i; - for (i=0; ictx->nb_streams; i++) - if (of->ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) + for (i = 0; i < of->nb_streams; i++) + if (output_streams[of->ost_index + i]->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) break; - if (ictx->nb_streams) { + if (i < of->nb_streams) { AUTO_INSERT_FILTER("-apad", "apad", ost->apad); } } diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 1d8827277d..0a8bdc36d9 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -49,6 +49,8 @@ typedef struct MuxStream { } MuxStream; struct Muxer { + AVFormatContext *fc; + MuxStream *streams; AVDictionary *opts; @@ -167,7 +169,7 @@ static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) { MuxStream *ms = &of->mux->streams[ost->index]; - AVFormatContext *s = of->ctx; + AVFormatContext *s = of->mux->fc; AVStream *st = ost->st; int ret; @@ -311,8 +313,8 @@ static int print_sdp(void) if (!avc) exit_program(1); for (i = 0, j = 0; i < nb_output_files; i++) { - if (!strcmp(output_files[i]->ctx->oformat->name, "rtp")) { - avc[j] = output_files[i]->ctx; + if (!strcmp(output_files[i]->format->name, "rtp")) { + avc[j] = output_files[i]->mux->fc; j++; } } @@ -350,15 +352,16 @@ fail: /* open the muxer when all the streams are initialized */ int of_check_init(OutputFile *of) { + AVFormatContext *fc = of->mux->fc; int ret, i; - for (i = 0; i < of->ctx->nb_streams; i++) { + for (i = 0; i < fc->nb_streams; i++) { OutputStream *ost = output_streams[of->ost_index + i]; if (!ost->initialized) return 0; } - ret = avformat_write_header(of->ctx, &of->mux->opts); + ret = avformat_write_header(fc, &of->mux->opts); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Could not write header for output file #%d " @@ -369,7 +372,7 @@ int of_check_init(OutputFile *of) //assert_avoptions(of->opts); of->mux->header_written = 1; - av_dump_format(of->ctx, of->index, of->ctx->url, 1); + av_dump_format(fc, of->index, fc->url, 1); nb_output_dumped++; if (sdp_filename || want_sdp) { @@ -381,7 +384,7 @@ int of_check_init(OutputFile *of) } /* flush the muxing queues */ - for (i = 0; i < of->ctx->nb_streams; i++) { + for (i = 0; i < fc->nb_streams; i++) { MuxStream *ms = &of->mux->streams[i]; OutputStream *ost = output_streams[of->ost_index + i]; AVPacket *pkt; @@ -402,29 +405,30 @@ int of_check_init(OutputFile *of) int of_write_trailer(OutputFile *of) { + AVFormatContext *fc = of->mux->fc; int ret; if (!of->mux->header_written) { av_log(NULL, AV_LOG_ERROR, "Nothing was written into output file %d (%s), because " "at least one of its streams received no packets.\n", - of->index, of->ctx->url); + of->index, fc->url); return AVERROR(EINVAL); } - ret = av_write_trailer(of->ctx); + ret = av_write_trailer(fc); if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, "Error writing trailer of %s: %s\n", of->ctx->url, av_err2str(ret)); + av_log(NULL, AV_LOG_ERROR, "Error writing trailer of %s: %s\n", fc->url, av_err2str(ret)); return ret; } of->mux->final_filesize = of_filesize(of); if (!(of->format->flags & AVFMT_NOFILE)) { - ret = avio_closep(&of->ctx->pb); + ret = avio_closep(&fc->pb); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Error closing file %s: %s\n", - of->ctx->url, av_err2str(ret)); + fc->url, av_err2str(ret)); return ret; } } @@ -432,14 +436,28 @@ int of_write_trailer(OutputFile *of) return 0; } -static void mux_free(Muxer **pmux, int nb_streams) +static void fc_close(AVFormatContext **pfc) +{ + AVFormatContext *fc = *pfc; + + if (!fc) + return; + + if (!(fc->oformat->flags & AVFMT_NOFILE)) + avio_closep(&fc->pb); + avformat_free_context(fc); + + *pfc = NULL; +} + +static void mux_free(Muxer **pmux) { Muxer *mux = *pmux; if (!mux) return; - for (int i = 0; i < nb_streams; i++) { + for (int i = 0; i < mux->fc->nb_streams; i++) { MuxStream *ms = &mux->streams[i]; AVPacket *pkt; @@ -453,13 +471,14 @@ static void mux_free(Muxer **pmux, int nb_streams) av_freep(&mux->streams); av_dict_free(&mux->opts); + fc_close(&mux->fc); + av_freep(pmux); } void of_close(OutputFile **pof) { OutputFile *of = *pof; - AVFormatContext *s; if (!of) return; @@ -467,34 +486,33 @@ void of_close(OutputFile **pof) sq_free(&of->sq_encode); sq_free(&of->sq_mux); - s = of->ctx; - - mux_free(&of->mux, s ? s->nb_streams : 0); - - if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE)) - avio_closep(&s->pb); - avformat_free_context(s); + mux_free(&of->mux); av_freep(pof); } -int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize) +int of_muxer_init(OutputFile *of, AVFormatContext *fc, + AVDictionary *opts, int64_t limit_filesize) { Muxer *mux = av_mallocz(sizeof(*mux)); int ret = 0; - if (!mux) + if (!mux) { + fc_close(&fc); return AVERROR(ENOMEM); + } - mux->streams = av_calloc(of->ctx->nb_streams, sizeof(*mux->streams)); + mux->streams = av_calloc(fc->nb_streams, sizeof(*mux->streams)); if (!mux->streams) { + fc_close(&fc); av_freep(&mux); return AVERROR(ENOMEM); } of->mux = mux; + mux->fc = fc; - for (int i = 0; i < of->ctx->nb_streams; i++) { + for (int i = 0; i < fc->nb_streams; i++) { MuxStream *ms = &mux->streams[i]; ms->muxing_queue = av_fifo_alloc2(8, sizeof(AVPacket*), 0); if (!ms->muxing_queue) { @@ -511,7 +529,7 @@ int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize) want_sdp = 0; /* write the header for files with no streams */ - if (of->format->flags & AVFMT_NOSTREAMS && of->ctx->nb_streams == 0) { + if (of->format->flags & AVFMT_NOSTREAMS && fc->nb_streams == 0) { ret = of_check_init(of); if (ret < 0) goto fail; @@ -519,7 +537,7 @@ int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize) fail: if (ret < 0) - mux_free(&of->mux, of->ctx->nb_streams); + mux_free(&of->mux); return ret; } @@ -531,7 +549,7 @@ int of_finished(OutputFile *of) int64_t of_filesize(OutputFile *of) { - AVIOContext *pb = of->ctx->pb; + AVIOContext *pb = of->mux->fc->pb; int64_t ret = -1; if (of->mux->final_filesize) @@ -548,6 +566,6 @@ int64_t of_filesize(OutputFile *of) AVChapter * const * of_get_chapters(OutputFile *of, unsigned int *nb_chapters) { - *nb_chapters = of->ctx->nb_chapters; - return of->ctx->chapters; + *nb_chapters = of->mux->fc->nb_chapters; + return of->mux->fc->chapters; } diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index ac0b4fc7d8..a86255e409 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2428,7 +2428,6 @@ static int open_output_file(OptionsContext *o, const char *filename) exit_program(1); } - of->ctx = oc; of->format = oc->oformat; if (o->recording_time != INT64_MAX) oc->duration = o->recording_time; @@ -3028,7 +3027,10 @@ loop_end: exit_program(1); } - err = of_muxer_init(of, format_opts, o->limit_filesize); + of->nb_streams = oc->nb_streams; + of->url = filename; + + err = of_muxer_init(of, oc, format_opts, o->limit_filesize); if (err < 0) { av_log(NULL, AV_LOG_FATAL, "Error initializing internal muxing state\n"); exit_program(1); From patchwork Mon Apr 4 11:30:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35205 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a02:aa92:0:0:0:0:0 with SMTP id u18csp3587679jai; Mon, 4 Apr 2022 04:39:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwRaPbViOcYvMnxeFLUtNT92USvAKl0sYpuolpi9Hp8GFiwhXqcMM4rYtPMOvwFbsDJlafM X-Received: by 2002:a05:6402:1255:b0:419:3808:3d98 with SMTP id l21-20020a056402125500b0041938083d98mr32455145edw.134.1649072374584; Mon, 04 Apr 2022 04:39:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072374; cv=none; d=google.com; s=arc-20160816; b=tL7CRhBB6S/kbUdjSPMSI9hDvk1yIQ6VUt/8jauEF9XbiatdI3nO5ho4PZRfdcd12V Vo0OFE2TGBZyxi/kLnFHh0BN/qHm3wkfPsRcC6CxeBei8irBL/e1gFQsjEjb6u7dp/0h dokfML45uUKzRvxIbwK3f0Cmzg5YGhlwZ3DIWs0kYdD5YkMHrV7mcqbiaT65vukVkPmg nT0XTqcmGPWyYNijKwGThBMnOr3RtyXtwrJGHCDIlPdt+xlE52KLjyGNGBHEf6m7UcDG tIMeoyoZojcInmEmSgZAuy5hgsB12TdR8t4EpHjkyNgFGD9qpxZcwvwLyeDLVwn7STxo XPaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=RnR3BQLDeZO22zEsVGReq7C4cZVlv8ZZM8XBaVxjyk0=; b=m8wxZidqh/XE/HoPbOMfd52XekWbYK7qtUJGTCSNORyuQ3EzKU3K2FegnVh1/6tDt+ +F7Q2LdRuGiWWVPOknfbkEB2D+1mdPiy4L0nZdgYNZmabcoUG3ZQ9qtVvGpgSHCQZc3E 268JKTyzTCmkkSJXYOByapXPxr/72NnSXdPfiQUsfFYfaefAtpKWBshFltvWxhUJZUL4 5PSFVZxvvT0edFDEAf2y3EpKdRoms1j5Db+a3LbGfTHEz61MKNn+oKT0J0qWcel6nU5k R1AAmKQgqaZexc4zNokUL5V6vx3H7OXpqCabGY2SAHyN7GXH2xWBf2b7EQFhqpqabc4q tYTg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id t19-20020aa7db13000000b0041cc16201e3si2653324eds.459.2022.04.04.04.39.34; Mon, 04 Apr 2022 04:39:34 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A7E2768B367; Mon, 4 Apr 2022 14:37:53 +0300 (EEST) 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 90AE968B2AE for ; Mon, 4 Apr 2022 14:37:45 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 3DB632404FE for ; Mon, 4 Apr 2022 13:37:45 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id K8nys_H13_z0 for ; Mon, 4 Apr 2022 13:37:44 +0200 (CEST) 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 B25F7240555 for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 32C373A0DFA; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:27 +0200 Message-Id: <20220404113037.13070-40-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 39/49] fftools/ffmpeg_mux: return errors from of_submit_packet() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: FcMUaaKJ8On0 Content-Length: 4729 --- fftools/ffmpeg.c | 54 +++++++++++++++++++++++++++++++------------- fftools/ffmpeg.h | 2 +- fftools/ffmpeg_mux.c | 12 ++++++---- 3 files changed, 46 insertions(+), 22 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 7e9ac943de..9987b15888 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -712,6 +712,7 @@ static void close_output_stream(OutputStream *ost) static void output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) { + const char *err_msg; int ret = 0; if (!eof && pkt->dts != AV_NOPTS_VALUE) @@ -719,25 +720,46 @@ static void output_packet(OutputFile *of, AVPacket *pkt, /* apply the output bitstream filters */ if (ost->bsf_ctx) { + int bsf_eof = 0; + ret = av_bsf_send_packet(ost->bsf_ctx, eof ? NULL : pkt); - if (ret < 0) - goto finish; - while ((ret = av_bsf_receive_packet(ost->bsf_ctx, pkt)) >= 0) - of_submit_packet(of, pkt, ost, 0); - if (ret == AVERROR_EOF) - of_submit_packet(of, pkt, ost, 1); - if (ret == AVERROR(EAGAIN)) - ret = 0; - } else - of_submit_packet(of, pkt, ost, eof); + if (ret < 0) { + err_msg = "submitting a packet for bitstream filtering"; + goto fail; + } -finish: - if (ret < 0 && ret != AVERROR_EOF) { - av_log(NULL, AV_LOG_ERROR, "Error applying bitstream filters to an output " - "packet for stream #%d:%d.\n", ost->file_index, ost->index); - if(exit_on_error) - exit_program(1); + while (!bsf_eof) { + ret = av_bsf_receive_packet(ost->bsf_ctx, pkt); + if (ret == AVERROR(EAGAIN)) + return; + else if (ret == AVERROR_EOF) + bsf_eof = 1; + else if (ret < 0) { + err_msg = "applying bitstream filters to a packet"; + goto fail; + } + + ret = of_submit_packet(of, pkt, ost, ret == AVERROR_EOF); + if (ret < 0) + goto mux_fail; + } + } else { + ret = of_submit_packet(of, pkt, ost, eof); + if (ret < 0) + goto mux_fail; } + + return; + +mux_fail: + err_msg = "submitting a packet to the muxer"; + +fail: + av_log(NULL, AV_LOG_ERROR, "Error %s for output stream #%d:%d.\n", + err_msg, ost->file_index, ost->index); + if (exit_on_error) + exit_program(1); + } static int check_recording_time(OutputStream *ost) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 2ba58b492f..fef66c2325 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -704,7 +704,7 @@ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); void of_close(OutputFile **pof); -void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof); +int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof); int of_finished(OutputFile *of); int64_t of_filesize(OutputFile *of); AVChapter * const * diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 0a8bdc36d9..3ea7636380 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -269,7 +269,7 @@ static void submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) } } -void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) +int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) { if (ost->sq_idx_mux >= 0) { int ret = sq_send(of->sq_mux, ost->sq_idx_mux, @@ -278,22 +278,24 @@ void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) av_packet_unref(pkt); if (ret == AVERROR_EOF) { ost->finished |= MUXER_FINISHED; - return; + return 0; } else - exit_program(1); + return ret; } while (1) { ret = sq_receive(of->sq_mux, -1, SQPKT(pkt)); if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) - return; + return 0; else if (ret < 0) - exit_program(1); + return ret; submit_packet(of, pkt, output_streams[of->ost_index + ret]); } } else if (!eof) submit_packet(of, pkt, ost); + + return 0; } static int print_sdp(void) From patchwork Mon Apr 4 11:30:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35195 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1915583pzb; Mon, 4 Apr 2022 04:40:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwdJemOWSSBztsYzbhArskT6rcFR9g3j8TWXUPo9xt0RxDd0izrJyOgIaZ/oxrvYeIjLkNJ X-Received: by 2002:a05:6402:1941:b0:413:2b7e:676e with SMTP id f1-20020a056402194100b004132b7e676emr33508362edz.114.1649072424594; Mon, 04 Apr 2022 04:40:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072424; cv=none; d=google.com; s=arc-20160816; b=fPA1fWpNTb3m5+RdS2zrOKh6zOgFD3Bnpcy4AaaHLp77aaj5af864mt4S8v88fhyDt 9kPBimVqgEUZPIIxDjVwx12QJJVZkgW55F1EeV5fQzbxLCFwSrd0uKyEITZh4cV80htu MJfuQU8kVGhT/MFoxYqBhB76bihQiN8SWJ1at/oueqLXuvky6DPHN4CqGS5toBoqdZML etPvbsewz2+ppelwTkSeL2/Ang+b6HwdAAYsKMLnbkGSmy94TRDZ/zJXMYo7sUDxQ9rx L0K57Ng8Uo20FBNPbhCV9t1iRLy/etVZjn+Ggsw4UPPJR8e9GN8R6Px3qdU42i3wcVfa GybQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=/sqvWa77ji8BUVrLZGr0KvekEXRMEWdzBybGnnwpn/4=; b=g34I6Jwlnm9mJ2DIC1eF10Cd61T7DKzrjrin7Y6aTbW5Fqngm2/l/XDh5ebNaBScRC 0p2G8ddXRiGMt9t6xTKoU3pAFd5f3YG3I0pfOXI0DZo4RXUxeTwE8cjHbU5op7IN1fkP N6YKI5rfWHawru50x1R9YbuA291CsvZy9MlhJHw7XM7HwMV5ipEP7P/og2z65z+ty6zt 0qx5J/VqZetNwhYb3jxrMc14YNgUnCpIPeLrgzX99L0k260gxqMjzqs1QSshvqWLcG8r S91owhngJnGQ09yJfwk2urRq3HpXVyNWoUJ5aU1OKrjg4Ex6sXH6lWP3duWlyRkDvHay R4cQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ek23-20020a056402371700b0041cb6e3d71dsi3169010edb.627.2022.04.04.04.40.24; Mon, 04 Apr 2022 04:40:24 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0AC7B68B388; Mon, 4 Apr 2022 14:37:58 +0300 (EEST) 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 2D36B68B2AF for ; Mon, 4 Apr 2022 14:37:46 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id E02C3240506 for ; Mon, 4 Apr 2022 13:37:45 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id WZDn2jI9ZA7u for ; Mon, 4 Apr 2022 13:37:45 +0200 (CEST) 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 B4CD324055A for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 36EA93A0E23; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:28 +0200 Message-Id: <20220404113037.13070-41-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 40/49] fftools/ffmpeg_mux: return errors from submit_packet() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: GFf78jCBKHH+ --- fftools/ffmpeg_mux.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 3ea7636380..69af2c8d46 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -253,7 +253,7 @@ static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) } } -static void submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) +static int submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) { int ret; @@ -264,9 +264,11 @@ static void submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) ret = queue_packet(of, ost, pkt); if (ret < 0) { av_packet_unref(pkt); - exit_program(1); + return ret; } } + + return 0; } int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) @@ -290,10 +292,12 @@ int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) else if (ret < 0) return ret; - submit_packet(of, pkt, output_streams[of->ost_index + ret]); + ret = submit_packet(of, pkt, output_streams[of->ost_index + ret]); + if (ret < 0) + return ret; } } else if (!eof) - submit_packet(of, pkt, ost); + return submit_packet(of, pkt, ost); return 0; } From patchwork Mon Apr 4 11:30:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35204 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a02:aa92:0:0:0:0:0 with SMTP id u18csp3587532jai; Mon, 4 Apr 2022 04:39:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwUN3mu3Y8sZsfLLmg/qfIXk55iR/ZECqiX/HzFliUsy6fGDVhLUC3dOg8Ssyye1TS6i96l X-Received: by 2002:a05:6402:d0e:b0:418:f011:275e with SMTP id eb14-20020a0564020d0e00b00418f011275emr32242004edb.323.1649072359905; Mon, 04 Apr 2022 04:39:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072359; cv=none; d=google.com; s=arc-20160816; b=WtsylvwtHk4/EQWh9SUGa4amDjQYdyZbDDYSbriYhLdruX2TDl0GJLDyT2gz3OFtsm vPHuZrFttQbT75h2PvdxYYFadoN7PQ+/0acsTGmtmxwkllPcsYPH79fsKTYkc9B2y0+0 cHnZn70sqPOkwLFfgkdkQCE0/llda40CVWKc8M3lbdsSDhTgxjgMZil32cqqo/ibEOvn NRvHoaJ7oBmioZiAGXV5Dd+mQCST8+0TdtCmVFiDXdd4jCo838dmkOCjzBuRPJ3I6azo A7laexFjnbTFvyadmzrzmhIoQ6mLrROw9T+4hUgY41GtCxHEs/PCape4TUo98/xVwPjk d2hw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=30Wu2uzhkv58yK0895mupt9vuWRZGQmovTQt5b4hxDA=; b=jc5yFYEG3QlVMI7hgUkq9OeklsxM1n7ak88vxRgGlkSakz5KKPH4cH/ych60Prya1J NkaeZE41Hv20njiV0oFQ3CegN8taRWrQOLp0YwwTvCXvmR6XPN+kyXKxT9PurGeIKIzo 9MGy2lGrta4UqkGsd/ZSYQTd+zIfxjWssEkEKK4PMLHV7ek0qL9fbwc76OaLwsX3Zjtt BGKAMeV44Q3UgVeAFlab8DbO6V5VhOb9s/xZD7n7cAZFSByqorzfgVZ2htk6mhLNkzYj 3BEt9D6RXVxtAsRvEx1F6IbDeqlJVj/idX2jDZ2AacmvAdqjD+s/DL7UkJ95ydsS+Q92 ml6Q== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id cb9-20020a0564020b6900b0041cd678a7c7si1205939edb.55.2022.04.04.04.39.19; Mon, 04 Apr 2022 04:39:19 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2D73268B34F; Mon, 4 Apr 2022 14:37:52 +0300 (EEST) 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 62B7F68B2DF for ; Mon, 4 Apr 2022 14:37:45 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 0F8DF24017E for ; Mon, 4 Apr 2022 13:37:45 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 75hTg92qodbL for ; Mon, 4 Apr 2022 13:37:44 +0200 (CEST) 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 B25C624052D for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 39EE43A0E24; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:29 +0200 Message-Id: <20220404113037.13070-42-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 41/49] fftools/ffmpeg_mux: return errors from write_packet() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: jel6L/35r7fR Content-Length: 2755 --- fftools/ffmpeg_mux.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 69af2c8d46..37ae61fee8 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -166,7 +166,7 @@ static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) return 0; } -static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) +static int write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) { MuxStream *ms = &of->mux->streams[ost->index]; AVFormatContext *s = of->mux->fc; @@ -211,10 +211,8 @@ static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) av_log(s, loglevel, "Non-monotonous DTS in output stream " "%d:%d; previous: %"PRId64", current: %"PRId64"; ", ost->file_index, ost->st->index, ms->last_mux_dts, pkt->dts); - if (exit_on_error) { - av_log(NULL, AV_LOG_FATAL, "aborting.\n"); - exit_program(1); - } + if (exit_on_error) + return AVERROR(EINVAL); av_log(s, loglevel, "changing to %"PRId64". This may result " "in incorrect timestamps in the output file.\n", max); @@ -250,7 +248,10 @@ static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) print_error("av_interleaved_write_frame()", ret); main_return_code = 1; close_all_output_streams(ost, MUXER_FINISHED | ENCODER_FINISHED, ENCODER_FINISHED); + return ret; } + + return 0; } static int submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) @@ -258,7 +259,7 @@ static int submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) int ret; if (of->mux->header_written) { - write_packet(of, ost, pkt); + return write_packet(of, ost, pkt); } else { /* the muxer is not initialized yet, buffer the packet */ ret = queue_packet(of, ost, pkt); @@ -401,8 +402,10 @@ int of_check_init(OutputFile *of) while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) { ms->muxing_queue_data_size -= pkt->size; - write_packet(of, ost, pkt); + ret = write_packet(of, ost, pkt); av_packet_free(&pkt); + if (ret < 0) + return ret; } } From patchwork Mon Apr 4 11:30:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35197 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1915926pzb; Mon, 4 Apr 2022 04:41:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyPAjKhR+OCqJwy40zHehoikF6A+RLO6p77RleBdk7/83FRSeHXPoZVeoV7D8fMipx81mS4 X-Received: by 2002:a17:906:9c8e:b0:6df:f6bf:7902 with SMTP id fj14-20020a1709069c8e00b006dff6bf7902mr10362734ejc.191.1649072459897; Mon, 04 Apr 2022 04:40:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072459; cv=none; d=google.com; s=arc-20160816; b=GGEPdqO70DRvZcKoJfZ/2kwSMBZk1f3CImLEo5P5HOQqCMjNxmjiCxy0bqPSEdMGnl r/bGarKcchslWeP/uZa22iSec48jltZN0whRnW/XjXz50ChsuqNh5cm8ULSVEPYae+v/ R7onRCFqZ7phYYGejghx7FePxH/2R0t+E4rVWr66X0mLnmEt4JiUpVqfCDDg6wYg8tYS agdtgUOv0vtinXr4rQl95XKR3B+WbBV7J13FbqY+xJoxVFESoNwSQcNHE05IzE1jtCgQ TB5fYjVBklO5BoNGOooAfpCn1b7tSr1Vxt2d+vaQJbL9DyJIb1i3Jjz53MQVdDdU16IT I6Vw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=RJIXBJrWGQQRT4IEmIcrkXdGoSi+OXqRISpLHSfzk+U=; b=Z6DYQYSWfSZSUb4nF10J2zCPmhJiCc17HoobLgeZwoUYXzza28hg6iviSdu6hytLay l9cjRWH1QIlYfqxyhOl4PJulP9P2TjJ8O8axuxejKnyLu5BKNqynwgXSVQ9hs8KQ3OGB phfrl7mQ917Sjy35BW66vfMMwKuYFnhRvLk9q9v8bqGzksMyychbYjKzQz1480z0WqX5 mwrwdemGX46DZvHLV3HuF4s/pq9u5wJauxQsjGtRl2GDEAR8Zgi9zbF+GKLCYGouvtxB 3aCtteSs/aIdD0/cRy3SMWbWuoEdo/po4W4SCh71fwLw9FsHfcG5bqFWqDMa6slUdeKq A/ag== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id bl17-20020a170906c25100b006e6c61a654fsi3688866ejb.635.2022.04.04.04.40.57; Mon, 04 Apr 2022 04:40:59 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 23E0268B2DF; Mon, 4 Apr 2022 14:38:01 +0300 (EEST) 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 9013B68B359 for ; Mon, 4 Apr 2022 14:37:46 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 4D35A240507 for ; Mon, 4 Apr 2022 13:37:46 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id zUgneuJVo_Y4 for ; Mon, 4 Apr 2022 13:37:45 +0200 (CEST) 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 E7608240592 for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 3D7E73A0E34; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:30 +0200 Message-Id: <20220404113037.13070-43-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 42/49] fftools/ffmpeg_mux: simplify submit_packet() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: lqndpBA8Evrd --- fftools/ffmpeg_mux.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 37ae61fee8..2cdbd5feef 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -258,18 +258,14 @@ static int submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) { int ret; - if (of->mux->header_written) { + if (of->mux->header_written) return write_packet(of, ost, pkt); - } else { - /* the muxer is not initialized yet, buffer the packet */ - ret = queue_packet(of, ost, pkt); - if (ret < 0) { - av_packet_unref(pkt); - return ret; - } - } - return 0; + /* the muxer is not initialized yet, buffer the packet */ + ret = queue_packet(of, ost, pkt); + if (ret < 0) + av_packet_unref(pkt); + return ret; } int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) From patchwork Mon Apr 4 11:30:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35190 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1915824pzb; Mon, 4 Apr 2022 04:40:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwtKaRkjER94iuqRdQnjyF1i4thEjQ4kIjS1A/XwEuqOkHmfsjp5xNuVLEWPUPIsMCKbr9n X-Received: by 2002:a17:906:9c8e:b0:6df:f6bf:7902 with SMTP id fj14-20020a1709069c8e00b006dff6bf7902mr10361951ejc.191.1649072447211; Mon, 04 Apr 2022 04:40:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072447; cv=none; d=google.com; s=arc-20160816; b=s1Q4xSKaQVEocbF9AgI007KVqWKlNrBzr3UMCruJw6pYzl8agO+eT6lxiWNx6fabvZ pGZwi+Tbk6BAxjylhD110IvD1dc0wXSKHb2UW4tvPGKIlcw80LG8INFm9kTIT/uvyomf /3e818blUuIDHLSDeGDo2wxsJiY/19usGQ8qkFuwA26xGJ6pz5ehsuMqPjkRaXkQ1d2v TDr0Zb6DiEmuqtD8c1AzZRRaAnE4LoNw+kCq5z0wsg40R/h3nNLBO7guIYu8UCoVl6kQ u9kR0KkRGPCD4heu4mFdxuhsxs4b6V0RDcA5ztXDS1FIJlafA15QbPmjoXni+9mw7+0S HYZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=DJ7R7EaaAONbb6VHM0Y+AtdtIi3nXS/M3yXtnmCnGcc=; b=YTUu3jAH2/i/rUIxlddwUkeJQX6UU8BawMmXmEcbm5PvsvIg4yuB0LXH30TUDgziOU YFAuoLT77iOrLbEN9VHhOywbO+k5oQ1YlDPljy8+Tmzwm1g+cpb6fkqA1PeowoKylfGz aNi3SFfPNZANNDg5EpTg2iBtKgaY/nXrEU6Gs6g7Tv7hJbIx/VayBqlymw0aA1lVp1pO s0QUtD4hu6HoyYRTkxgVu5IbRRN9R9t+LnUpAUZ9pft5ZbC+G3muOIMo8SdzAh9RUckm OGaztR/MmZEwilK8rRiXB7aOKWsTbnwYRE3KqzzwLOwqFX7eCFTwz2gkr0PwDTI0zoT0 kt1g== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id l25-20020a1709062a9900b006e4b4049463si6381475eje.429.2022.04.04.04.40.46; Mon, 04 Apr 2022 04:40:47 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2287E68B394; Mon, 4 Apr 2022 14:38:00 +0300 (EEST) 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 481DC68B357 for ; Mon, 4 Apr 2022 14:37:51 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id EA8752400F5 for ; Mon, 4 Apr 2022 13:37:48 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id WwbZugHVoBJM for ; Mon, 4 Apr 2022 13:37:47 +0200 (CEST) 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 0A86C24062A for ; Mon, 4 Apr 2022 13:37:42 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 410E23A0E35; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:31 +0200 Message-Id: <20220404113037.13070-44-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 43/49] fftools/ffmpeg_mux: return errors from update_video_stats() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: LnTmEWtSzN1l --- fftools/ffmpeg_mux.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 2cdbd5feef..087009e0eb 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -63,7 +63,7 @@ struct Muxer { static int want_sdp = 1; -static void update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats) +static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats) { const uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, NULL); @@ -82,14 +82,14 @@ static void update_video_stats(OutputStream *ost, const AVPacket *pkt, int write } if (!write_vstats) - return; + return 0; /* this is executed just the first time update_video_stats is called */ if (!vstats_file) { vstats_file = fopen(vstats_filename, "w"); if (!vstats_file) { perror("fopen"); - exit_program(1); + return AVERROR(errno); } } @@ -116,6 +116,8 @@ static void update_video_stats(OutputStream *ost, const AVPacket *pkt, int write fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ", (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate); fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type)); + + return 0; } static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others) @@ -227,8 +229,11 @@ static int write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) ost->data_size += pkt->size; ost->packets_written++; - if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed) - update_video_stats(ost, pkt, !!vstats_filename); + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed) { + ret = update_video_stats(ost, pkt, !!vstats_filename); + if (ret < 0) + return ret; + } pkt->stream_index = ost->index; From patchwork Mon Apr 4 11:30:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35199 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1916069pzb; Mon, 4 Apr 2022 04:41:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw9NlE/jKgSxKzwDfP9Rl6lPhozS7T799gvZYeVPGrfT2fWSebf7JyEZ3ILdPCcdlI8qhK8 X-Received: by 2002:a17:907:62aa:b0:6e0:f208:b869 with SMTP id nd42-20020a17090762aa00b006e0f208b869mr10139275ejc.270.1649072481499; Mon, 04 Apr 2022 04:41:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072481; cv=none; d=google.com; s=arc-20160816; b=r/+gR3gYTWDeULCgD8XYDxcoFyJBcFYRJGhUDsKAvfDfZouy+vEZZnzq9POtjfvtft aWBltdrWJDia4Vsplm+P6a7CMEXS4lJxm2l0n/ePq8mc3Bqm/JrsPuY8+K54ZWvfiWzo uO0oRxwjlRSlNwCDx8xOxI9BiVcC2UsKLxYT+bZo4CGfYtUcbcSqd9EDn5btzd4HGQqA QgboL1xmV8OEprQAQlNUOkIC/H9WtfdsZsk687jUcKjCzyKArDiu7Rq4HHZsVPfvgRv6 h2ltDEM2I/usBdRfM4YUvfezFB/9ZYGKS+PySVTwsjOEgSSXckYY15kPhqnUg9yJanuY 0GqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=/HXeE66kjk5C+kRlcs2QK0gherbrQ3BP26vlofYdiJg=; b=J0eKH96Nm0vV9h2tGOurGMAk0hj6/oBQYTKkcFak7+uHZBNPYiU3VsKIm1gr2n8mYq zlhGfR26Ibqm1M3C5pcph8SordUZFbUHsu6s6HyWy0Ctja0dToKHEG0yZpaPs7UkVsS+ j+YMzRTOB3kfA286WyWbLT0/mupxW6v3rtS4fS6hrOsPC+brBc+U0eJdAjnRhl6MJTVh qwGLYhxW99m2FH8sQh71zLFFNgkfiDNYDeOOLv+Uu/4+eMtgruiEQo9cA3OTLwh79hYC 0Y/VUA7VXxwhGex2Mj9xxHoh3ychgxIfyRxYGTNzrPSHVQ/Aih3QVbPmRTGubx4e1cKc oEdQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id v8-20020a1709060b4800b006df76385bc9si6275138ejg.105.2022.04.04.04.41.21; Mon, 04 Apr 2022 04:41:21 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3542E68B3A1; Mon, 4 Apr 2022 14:38:03 +0300 (EEST) 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 5CABE68B360 for ; Mon, 4 Apr 2022 14:37:47 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 12C48240511 for ; Mon, 4 Apr 2022 13:37:47 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id UA32qjnXuE-a for ; Mon, 4 Apr 2022 13:37:46 +0200 (CEST) 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 060D22405F0 for ; Mon, 4 Apr 2022 13:37:42 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 449F63A0E3A; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:32 +0200 Message-Id: <20220404113037.13070-45-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 44/49] fftools/ffmpeg_mux: do not call exit_program() in print_sdp() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 0+NgeYbg62JL Return an error instead, as is already done in other places in this function. --- fftools/ffmpeg_mux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 087009e0eb..2fd131d1bc 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -319,7 +319,7 @@ static int print_sdp(void) avc = av_malloc_array(nb_output_files, sizeof(*avc)); if (!avc) - exit_program(1); + return AVERROR(ENOMEM); for (i = 0, j = 0; i < nb_output_files; i++) { if (!strcmp(output_files[i]->format->name, "rtp")) { avc[j] = output_files[i]->mux->fc; From patchwork Mon Apr 4 11:30:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35192 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1915307pzb; Mon, 4 Apr 2022 04:39:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxwNg0cgzEPIhW7WzHBVKmBUhmlVYVvJwQePwBCiyZjKlkCkrwC1Re5G7qiXsalCcOhlDgr X-Received: by 2002:a17:906:6a05:b0:6e7:f5c8:1d55 with SMTP id qw5-20020a1709066a0500b006e7f5c81d55mr2471093ejc.443.1649072393449; Mon, 04 Apr 2022 04:39:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072393; cv=none; d=google.com; s=arc-20160816; b=MQO9DCDZuxCeoTwX2u0jpT1e8sefh4icGlowWkmrUG5YW4eliHz0OQk/vcTJTdqLsf Sa3nLexwRwekUyIENy7TKoeUiAn1vkimjgRvDajArSd03UC8ktBOn8RGegpQldAIK4l2 fjCdxec+U8CwzOD7gL+XyhXl5ERP4OpbFO48tlI2HaeOYdBVBStGXZ9DIUXdgMXqCx2t GLNdbFnHigP5+YreQwgYHvewlD2NpZLvajCyTBnqFJGVmZ4c5s0Zd6Gg8Tm1Oj87rb9O b99+asT3/Ne/kZGklm1rTCkdpU0cyQrmMEk+EkeHlOKBCwtCLRghRx1OP9e/hKXJHbjj fLvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=aGDr/X1csCfz87ItuFOseli11ku6Md295g44/KCtUYE=; b=Xwgb40dnYl84v4CRAk0HwOZc1+kYXvIuaZ1wOym5BhW8J3U6FfO8bNm+WgY0LMIDWw aNUmQmHsd0qot7x44/AvJgfa7gsZVjs0DgxX62rozBiXcMbcdbBWSIAbNF/4bnOIeIKD jGAuz9x9rSI2hzT9CcTAtp7cAaa+DI3FB/CLNNhpWV7a/jk08atCvl8Bj0iphwd6xRh2 qC77G4s3qu2hn8t4LwHedyWlutlOK7H/tsoFzAfM/31IvVLAKpF1MRYQ64odOKJRdZnL M4nCyQ7PfOyo/JzFqoN5gdo8en0EM5pZ+s1j8p6iz8+tpH9xjXp2aFJ3khlbCyhtwFEX bWCg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id q16-20020a50cc90000000b0041cdabdaad6si676450edi.621.2022.04.04.04.39.53; Mon, 04 Apr 2022 04:39:53 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6DD2D68B36E; Mon, 4 Apr 2022 14:37:55 +0300 (EEST) 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 110F568B34B for ; Mon, 4 Apr 2022 14:37:50 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 7BD40240179 for ; Mon, 4 Apr 2022 13:37:47 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id GqN-oY0wtkhy for ; Mon, 4 Apr 2022 13:37:47 +0200 (CEST) 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 0AA62240685 for ; Mon, 4 Apr 2022 13:37:42 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 482783A0E53; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:33 +0200 Message-Id: <20220404113037.13070-46-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 45/49] fftools/ffmpeg: stop using av_stream_get_end_pts() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: AFHysZRHTaDP It retrieves some muxer's internal timestamp with under-defined semantics. Continuing to use this value would also require synchronization once the muxer is moved to a separate thread. Replace the value with last_mux_dts. --- fftools/ffmpeg.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 9987b15888..0cab73ad7a 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1604,9 +1604,8 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti vid = 1; } /* compute min output value */ - if (av_stream_get_end_pts(ost->st) != AV_NOPTS_VALUE) { - pts = FFMAX(pts, av_rescale_q(av_stream_get_end_pts(ost->st), - ost->st->time_base, AV_TIME_BASE_Q)); + if (ost->last_mux_dts != AV_NOPTS_VALUE) { + pts = FFMAX(pts, ost->last_mux_dts); if (copy_ts) { if (copy_ts_first_pts == AV_NOPTS_VALUE && pts > 1) copy_ts_first_pts = pts; From patchwork Mon Apr 4 11:30:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35194 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1915486pzb; Mon, 4 Apr 2022 04:40:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxVq+AKHP2opSUdR9EXTTGaH2hzg7wNZc6s/o84Yn5NO4lJlGcmiFVbzJXaa9sV55dkGBSP X-Received: by 2002:a17:907:9493:b0:6e0:59f5:6705 with SMTP id dm19-20020a170907949300b006e059f56705mr10810889ejc.289.1649072413715; Mon, 04 Apr 2022 04:40:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072413; cv=none; d=google.com; s=arc-20160816; b=ifiLgxJ5XdhcsbpCpx796R5ce0/zohSrZjRhHb07hR/33x5CaMgVqukvyEFrv+ZWdg 8EUHhQ4s8I2aK2lkg83+l4fKyTQHCXwyL7EDvXc7LvGmcntvTM3mKqGW75q2t7Afq6cY WPkNgCgYohjazayC83Bw4X8/JwP0TEpAPzzW+Xd+HsBli8q2drUSMMQHX4UNgcMxfShJ 2Mj5Is/WHUUtzhQFzociqZSTw5lOyr5YEfBxP50l1p8vvWMI1p6WLU40xv5o6YO1JApR iFmudbC/jyzREsFdErO5cwOgPX/lEP/OTYQ+oO5ix+Pv+cM2mMWEJo6XlKgpmk1KDFsh iMMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=sFs9Yx+j73y+e+5evXXesAoWCOaH9/Do78BZufbqO9Q=; b=NYyopWXpi2T65iYKonI11gGyGnnLUbp8ypzsdxWxHwzoCkK40UEx4U+z9aNWQ006s6 bvVb6SlI46Am4JbO3CkoO3jrRUJ1rk7ckuOwLlM/6mP/j/WFrjzhlHemWIBgwi70VqOu TjRQneAwkA5Uqg0voXWZKqRBjFz5yYjaZdRHkxnXlR49d8ebiPCDnhHWYQJWrL9S3TZy CjXg1NEZqB8Wmn6Ftvceglce1KCn1vnKRD/w0JOnC3vZs+TNre4zCyhiHuyvbUZRctTd b/4S0MPEsTwgHqRMmnIl85e37h0I7+mBxDZ68q8olAjyy4cdPDEb0UUFZiouGGQJL2in Pjfg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id g14-20020a170906c18e00b006df76385c4dsi3042414ejz.237.2022.04.04.04.40.13; Mon, 04 Apr 2022 04:40:13 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3652468B382; Mon, 4 Apr 2022 14:37:57 +0300 (EEST) 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 1356B68B34F for ; Mon, 4 Apr 2022 14:37:51 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id F0D2D24052D for ; Mon, 4 Apr 2022 13:37:47 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 5lnenK6eyWgr for ; Mon, 4 Apr 2022 13:37:47 +0200 (CEST) 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 066F42405F5 for ; Mon, 4 Apr 2022 13:37:42 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 4BD9B3A0E7D; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:34 +0200 Message-Id: <20220404113037.13070-47-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 46/49] fftools/ffmpeg: do not write the output file header from init_output_stream() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 5BJ41wfovRim --- fftools/ffmpeg.c | 4 - fftools/ffmpeg.h | 2 - fftools/ffmpeg_mux.c | 186 ++++++++++++++++++++++--------------------- 3 files changed, 96 insertions(+), 96 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 0cab73ad7a..2dff41e57f 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3230,10 +3230,6 @@ static int init_output_stream(OutputStream *ost, AVFrame *frame, ost->initialized = 1; - ret = of_check_init(output_files[ost->file_index]); - if (ret < 0) - return ret; - return ret; } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index fef66c2325..14b98909d0 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -699,8 +699,6 @@ int hwaccel_decode_init(AVCodecContext *avctx); int of_muxer_init(OutputFile *of, AVFormatContext *fc, AVDictionary *opts, int64_t limit_filesize); -/* open the muxer when all the streams are initialized */ -int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); void of_close(OutputFile **pof); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 2fd131d1bc..6ca9a51dd6 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -63,6 +63,59 @@ struct Muxer { static int want_sdp = 1; +static int print_sdp(void) +{ + char sdp[16384]; + int i; + int j, ret; + AVIOContext *sdp_pb; + AVFormatContext **avc; + + for (i = 0; i < nb_output_files; i++) { + if (!output_files[i]->mux->header_written) + return 0; + } + + avc = av_malloc_array(nb_output_files, sizeof(*avc)); + if (!avc) + return AVERROR(ENOMEM); + for (i = 0, j = 0; i < nb_output_files; i++) { + if (!strcmp(output_files[i]->format->name, "rtp")) { + avc[j] = output_files[i]->mux->fc; + j++; + } + } + + if (!j) { + av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n"); + ret = AVERROR(EINVAL); + goto fail; + } + + ret = av_sdp_create(avc, j, sdp, sizeof(sdp)); + if (ret < 0) + goto fail; + + if (!sdp_filename) { + printf("SDP:\n%s\n", sdp); + fflush(stdout); + } else { + ret = avio_open2(&sdp_pb, sdp_filename, AVIO_FLAG_WRITE, &int_cb, NULL); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", sdp_filename); + goto fail; + } + + avio_print(sdp_pb, sdp); + avio_closep(&sdp_pb); + av_freep(&sdp_filename); + } + +fail: + av_freep(&avc); + return ret; +} + static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats) { const uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, @@ -273,92 +326,8 @@ static int submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) return ret; } -int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) -{ - if (ost->sq_idx_mux >= 0) { - int ret = sq_send(of->sq_mux, ost->sq_idx_mux, - SQPKT(eof ? NULL: pkt)); - if (ret < 0) { - av_packet_unref(pkt); - if (ret == AVERROR_EOF) { - ost->finished |= MUXER_FINISHED; - return 0; - } else - return ret; - } - - while (1) { - ret = sq_receive(of->sq_mux, -1, SQPKT(pkt)); - if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) - return 0; - else if (ret < 0) - return ret; - - ret = submit_packet(of, pkt, output_streams[of->ost_index + ret]); - if (ret < 0) - return ret; - } - } else if (!eof) - return submit_packet(of, pkt, ost); - - return 0; -} - -static int print_sdp(void) -{ - char sdp[16384]; - int i; - int j, ret; - AVIOContext *sdp_pb; - AVFormatContext **avc; - - for (i = 0; i < nb_output_files; i++) { - if (!output_files[i]->mux->header_written) - return 0; - } - - avc = av_malloc_array(nb_output_files, sizeof(*avc)); - if (!avc) - return AVERROR(ENOMEM); - for (i = 0, j = 0; i < nb_output_files; i++) { - if (!strcmp(output_files[i]->format->name, "rtp")) { - avc[j] = output_files[i]->mux->fc; - j++; - } - } - - if (!j) { - av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n"); - ret = AVERROR(EINVAL); - goto fail; - } - - ret = av_sdp_create(avc, j, sdp, sizeof(sdp)); - if (ret < 0) - goto fail; - - if (!sdp_filename) { - printf("SDP:\n%s\n", sdp); - fflush(stdout); - } else { - ret = avio_open2(&sdp_pb, sdp_filename, AVIO_FLAG_WRITE, &int_cb, NULL); - if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", sdp_filename); - goto fail; - } - - avio_print(sdp_pb, sdp); - avio_closep(&sdp_pb); - av_freep(&sdp_filename); - } - -fail: - av_freep(&avc); - return ret; -} - /* open the muxer when all the streams are initialized */ -int of_check_init(OutputFile *of) +static int check_write_header(OutputFile *of) { AVFormatContext *fc = of->mux->fc; int ret, i; @@ -397,10 +366,6 @@ int of_check_init(OutputFile *of) OutputStream *ost = output_streams[of->ost_index + i]; AVPacket *pkt; - /* try to improve muxing time_base (only possible if nothing has been written yet) */ - if (!av_fifo_can_read(ms->muxing_queue)) - ost->mux_timebase = ost->st->time_base; - while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) { ms->muxing_queue_data_size -= pkt->size; ret = write_packet(of, ost, pkt); @@ -413,6 +378,47 @@ int of_check_init(OutputFile *of) return 0; } +int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) +{ + int ret; + + if (!of->mux->header_written) { + ret = check_write_header(of); + if (ret < 0) { + av_packet_unref(pkt); + return ret; + } + } + + if (ost->sq_idx_mux >= 0) { + ret = sq_send(of->sq_mux, ost->sq_idx_mux, + SQPKT(eof ? NULL: pkt)); + if (ret < 0) { + av_packet_unref(pkt); + if (ret == AVERROR_EOF) { + ost->finished |= MUXER_FINISHED; + return 0; + } else + return ret; + } + + while (1) { + ret = sq_receive(of->sq_mux, -1, SQPKT(pkt)); + if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) + return 0; + else if (ret < 0) + return ret; + + ret = submit_packet(of, pkt, output_streams[of->ost_index + ret]); + if (ret < 0) + return ret; + } + } else if (!eof) + return submit_packet(of, pkt, ost); + + return 0; +} + int of_write_trailer(OutputFile *of) { AVFormatContext *fc = of->mux->fc; @@ -540,7 +546,7 @@ int of_muxer_init(OutputFile *of, AVFormatContext *fc, /* write the header for files with no streams */ if (of->format->flags & AVFMT_NOSTREAMS && fc->nb_streams == 0) { - ret = of_check_init(of); + ret = check_write_header(of); if (ret < 0) goto fail; } From patchwork Mon Apr 4 11:30:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35196 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1915740pzb; Mon, 4 Apr 2022 04:40:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzbV4f3mWSBHbhcWgugHmCapey/Hrlu9ygRiy0kE1JWTTcaKdpf5XWmSqMZ5wrwQXd86H5C X-Received: by 2002:a17:906:3adb:b0:6b7:876c:d11b with SMTP id z27-20020a1709063adb00b006b7876cd11bmr10371481ejd.250.1649072436675; Mon, 04 Apr 2022 04:40:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072436; cv=none; d=google.com; s=arc-20160816; b=BOMkrdEe3UJSmERqiAOv6r7yy3axURjon4VRFkS2SVqu04UlhM93mffctoK+S+/VIr D6z1HMFetSFaAIl7UD7jDh91io1idhuC1o9Zpc07axJnkTFnq4nuzGUI4qVuLPUoU8zv +aXbCutq+cOXLUl0lbjusCXrSDJrFEYSgHELCdSU2AJv3ZPqlpqqozZetP30eFzOwXux IrX28tZBqNCmn6gQp9K9P50q0FyKZeKW8RH+6LV4xJhk8a1QVMHN2UKkZPoRH8J6deIi ZunaH/Epbf/ZkGJ+OXd2AE5LDzCGw2AxXm3Uasg8FysJXTEMC0N/q7nYV7noyFrOj5iR W6MA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=vd1VDqoI/89M+0RV4q8KkIj924wJksZx3oNNkPbZt50=; b=O2D3UDyts50gnsIMOaCbme9HviPSU8EWPmKX35B7SC7Pp2ILJsi4EhP3gs3Bz34s6R ZqYZKT3y3SosEQ+NusfovottHgu5gxwFEHcClEFXWj1wsO98aA1O7AeiXal7VL0CJMrM Va8vClRZfZckOVKBut4wezRiKdco9Ns+Ieh2uGadE/N1N2RQ9ybf51pIaAcYcjhe6XPR nsiPVyE6WigJpTjWo1HiYEUlqyEI9jXWAsmhNixaj0FD4XXp2Vi4JjTBnpZG9a/P7vDD r+qnh4kJRlcM4AZh1kgwtwVWo/YO7Vt92qfl9bbEqnlbqT0FCAZwbQDjV/kCK4FxhD7F Qfxg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id dv24-20020a170906b81800b006e6f9db2c58si3490038ejb.544.2022.04.04.04.40.35; Mon, 04 Apr 2022 04:40:36 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 09DC968B389; Mon, 4 Apr 2022 14:37:59 +0300 (EEST) 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 44A7668B350 for ; Mon, 4 Apr 2022 14:37:51 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id AB821240555 for ; Mon, 4 Apr 2022 13:37:48 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id ROjdUVlEwywM for ; Mon, 4 Apr 2022 13:37:48 +0200 (CEST) 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 179592400F5 for ; Mon, 4 Apr 2022 13:37:42 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 4F5973A0E83; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:35 +0200 Message-Id: <20220404113037.13070-48-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 47/49] fftools/ffmpeg: depend on threads 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: sFdpaKUHCNpE ffmpeg will be switched to a fully threaded architecture, starting with muxers. --- configure | 2 +- fftools/ffmpeg.c | 20 -------------------- fftools/ffmpeg.h | 2 -- fftools/ffmpeg_opt.c | 2 -- 4 files changed, 1 insertion(+), 25 deletions(-) diff --git a/configure b/configure index e4d36aa639..4888b136a4 100755 --- a/configure +++ b/configure @@ -3796,7 +3796,7 @@ avfilter_extralibs="pthreads_extralibs" avutil_extralibs="d3d11va_extralibs nanosleep_extralibs pthreads_extralibs vaapi_drm_extralibs vaapi_x11_extralibs vdpau_x11_extralibs" # programs -ffmpeg_deps="avcodec avfilter avformat" +ffmpeg_deps="avcodec avfilter avformat threads" ffmpeg_select="aformat_filter anull_filter atrim_filter format_filter hflip_filter null_filter transpose_filter trim_filter vflip_filter" diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 2dff41e57f..9dfbc4216a 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -162,9 +162,7 @@ static struct termios oldtty; static int restore_tty; #endif -#if HAVE_THREADS static void free_input_threads(void); -#endif /* sub2video hack: Convert subtitles to video with alpha to insert them in filter graphs. @@ -592,9 +590,7 @@ static void ffmpeg_cleanup(int ret) av_freep(&output_streams[i]); } -#if HAVE_THREADS free_input_threads(); -#endif for (i = 0; i < nb_input_files; i++) { avformat_close_input(&input_files[i]->ctx); av_packet_free(&input_files[i]->pkt); @@ -3601,7 +3597,6 @@ static int check_keyboard_interaction(int64_t cur_time) return 0; } -#if HAVE_THREADS static void *input_thread(void *arg) { InputFile *f = arg; @@ -3719,7 +3714,6 @@ static int get_input_packet_mt(InputFile *f, AVPacket **pkt) f->non_blocking ? AV_THREAD_MESSAGE_NONBLOCK : 0); } -#endif static int get_input_packet(InputFile *f, AVPacket **pkt) { @@ -3742,10 +3736,8 @@ static int get_input_packet(InputFile *f, AVPacket **pkt) } } -#if HAVE_THREADS if (f->thread_queue_size) return get_input_packet_mt(f, pkt); -#endif *pkt = f->pkt; return av_read_frame(f->ctx, *pkt); } @@ -3885,15 +3877,11 @@ static int process_input(int file_index) avcodec_flush_buffers(avctx); } } -#if HAVE_THREADS free_input_thread(file_index); -#endif ret = seek_to_start(ifile, is); -#if HAVE_THREADS thread_ret = init_input_thread(file_index); if (thread_ret < 0) return thread_ret; -#endif if (ret < 0) av_log(NULL, AV_LOG_WARNING, "Seek to start failed.\n"); else @@ -4133,11 +4121,9 @@ static int process_input(int file_index) process_input_packet(ist, pkt, 0); discard_packet: -#if HAVE_THREADS if (ifile->thread_queue_size) av_packet_free(&pkt); else -#endif av_packet_unref(pkt); return 0; @@ -4324,10 +4310,8 @@ static int transcode(void) timer_start = av_gettime_relative(); -#if HAVE_THREADS if ((ret = init_input_threads()) < 0) goto fail; -#endif while (!received_sigterm) { int64_t cur_time= av_gettime_relative(); @@ -4352,9 +4336,7 @@ static int transcode(void) /* dump report by using the output first video and audio streams */ print_report(0, timer_start, cur_time); } -#if HAVE_THREADS free_input_threads(); -#endif /* at the end of stream, we must flush the decoder buffers */ for (i = 0; i < nb_input_streams; i++) { @@ -4412,9 +4394,7 @@ static int transcode(void) ret = 0; fail: -#if HAVE_THREADS free_input_threads(); -#endif if (output_streams) { for (i = 0; i < nb_output_streams; i++) { diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 14b98909d0..407342462f 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -423,13 +423,11 @@ typedef struct InputFile { AVPacket *pkt; -#if HAVE_THREADS AVThreadMessageQueue *in_thread_queue; pthread_t thread; /* thread reading from this file */ int non_blocking; /* reading packets from the thread should not block */ int joined; /* the thread has been joined */ int thread_queue_size; /* maximum number of queued packets */ -#endif } InputFile; enum forced_keyframes_const { diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index a86255e409..9bf22f6932 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -1305,9 +1305,7 @@ static int open_input_file(OptionsContext *o, const char *filename) f->pkt = av_packet_alloc(); if (!f->pkt) exit_program(1); -#if HAVE_THREADS f->thread_queue_size = o->thread_queue_size; -#endif /* check if all codec options have been used */ unused_opts = strip_specifiers(o->g->codec_opts); From patchwork Mon Apr 4 11:30:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35198 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1915972pzb; Mon, 4 Apr 2022 04:41:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzleHkvG4a/Kh6pXcd0EApbSnd5fnqdiEd0RvIein/9zzEUf48KRZp9TBkmTgoMYHB3Xx7q X-Received: by 2002:a50:ce03:0:b0:41c:c36b:c75 with SMTP id y3-20020a50ce03000000b0041cc36b0c75mr5564205edi.195.1649072470161; Mon, 04 Apr 2022 04:41:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072470; cv=none; d=google.com; s=arc-20160816; b=Gd7evNwbr8tZD3pqLMQrniFEP+Ago9XOfaKAeA336QKCZc3OJhiRN6RIBgaS1WSWaj KFEvaF27X4LsgZk4ZUUY9cAHcwOlVRF1JyIiw5fBLicW2WbwO3wAb9oUmfEBVm0uB8AI jZKXIGbWDQ3+I7kPbZgCIITwWA/Xnm5RP0BFWGOc+9dWi10FeYTN7/RDuH32G8LARb+2 2xXOwcwsAwsLC3ZXMkrHeQzQu23geO6fQgOiUvlC2LKh1457v3Tvy10DNva3XJxYt7ZB A1fxphu4cr65hbxh4i+p0Fn4w6Px3WcCXtTawuKoWsI+zPB0eLUoM4Vc0L4OTb/KqZFD swtg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=29A29b3UjNbLlYUfdiivd5B8rCi5LYKtuXZA9wf9uNY=; b=hJMwfnSHD9F5CuT3WlmbxzdajMS1Uni+h6QUITAScuxTFwwK5GHLTeAQVxXQdrNK1K girwJF4eMybtk4oR2gRrpg323wI38ExM09k5L3FU43O4ca5sjkD4jwj6SGgZTVrKsnAH FRcT6fl1qKDmnvZJG9njLxywOMJcxFh8KEk35wSkwjg0Zz8+jKoN0ok1ZG1HVql5ozCM BmD04hHqRJMjpb8rxqso/PpuGe5GAUdmCmnSrfNEsKjE/pxbLn+DDjJhCc3cY9B9VrmC 4pucfzePKSnFJ/RtqZ850OQXtFxfIXn1OXoel0qvq8wrJ2p4+XU/75qHC8V19uJ8GnB8 5Vyw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id d5-20020a1709067f0500b006e0df033cd5si3135856ejr.527.2022.04.04.04.41.09; Mon, 04 Apr 2022 04:41:10 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1A52868B398; Mon, 4 Apr 2022 14:38:02 +0300 (EEST) 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 E3F1F68B359 for ; Mon, 4 Apr 2022 14:37:46 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id A0D2B24050B for ; Mon, 4 Apr 2022 13:37:46 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 8zjOjZSNdjwP for ; Mon, 4 Apr 2022 13:37:45 +0200 (CEST) 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 B8E1D24056A for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 52FF93A0E88; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:36 +0200 Message-Id: <20220404113037.13070-49-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 48/49] fftools: add a multistream thread-safe queue 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 53W+CODo4gLn It is similar to AVThreadMessageQueue, but supports multiple streams, each with its own EOF state. --- fftools/Makefile | 1 + fftools/thread_queue.c | 232 +++++++++++++++++++++++++++++++++++++++++ fftools/thread_queue.h | 37 +++++++ 3 files changed, 270 insertions(+) create mode 100644 fftools/thread_queue.c create mode 100644 fftools/thread_queue.h diff --git a/fftools/Makefile b/fftools/Makefile index f015df6846..b0de4f4c61 100644 --- a/fftools/Makefile +++ b/fftools/Makefile @@ -15,6 +15,7 @@ OBJS-ffmpeg += \ fftools/ffmpeg_mux.o \ fftools/ffmpeg_opt.o \ fftools/sync_queue.o \ + fftools/thread_queue.o \ define DOFFTOOL OBJS-$(1) += fftools/cmdutils.o fftools/opt_common.o fftools/$(1).o $(OBJS-$(1)-yes) diff --git a/fftools/thread_queue.c b/fftools/thread_queue.c new file mode 100644 index 0000000000..9ec02ca772 --- /dev/null +++ b/fftools/thread_queue.c @@ -0,0 +1,232 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include "libavutil/avassert.h" +#include "libavutil/error.h" +#include "libavutil/fifo.h" +#include "libavutil/mem.h" +#include "libavutil/thread.h" + +#include "thread_queue.h" + +enum { + FINISHED_SEND = (1 << 0), + FINISHED_RECV = (1 << 1), +}; + +typedef struct ThreadQueueStream { + AVFifo *fifo; + int finished; +} ThreadQueueStream; + +struct ThreadQueue { + ThreadQueueStream *streams; + unsigned int nb_streams; + + void (*elem_free)(void *); + + pthread_mutex_t lock; + pthread_cond_t cond; +}; + +void tq_free(ThreadQueue **ptq) +{ + ThreadQueue *tq = *ptq; + + if (!tq) + return; + + for (unsigned int i = 0; i < tq->nb_streams; i++) { + ThreadQueueStream *st = &tq->streams[i]; + + if (st->fifo && tq->elem_free) { + void *elem; + while (av_fifo_read(st->fifo, &elem, 1) >= 0) + tq->elem_free(elem); + } + av_fifo_freep2(&tq->streams[i].fifo); + } + av_freep(&tq->streams); + + pthread_cond_destroy(&tq->cond); + pthread_mutex_destroy(&tq->lock); + + av_freep(ptq); +} + +ThreadQueue *tq_alloc(unsigned int nb_streams, + size_t nb_elems, size_t elem_size, + void (*elem_free)(void *)) +{ + ThreadQueue *tq; + int ret; + + tq = av_mallocz(sizeof(*tq)); + if (!tq) + return NULL; + + tq->elem_free = elem_free; + + ret = pthread_cond_init(&tq->cond, NULL); + if (ret) { + av_freep(&tq); + return NULL; + } + + ret = pthread_mutex_init(&tq->lock, NULL); + if (ret) { + pthread_cond_destroy(&tq->cond); + av_freep(&tq); + return NULL; + } + + tq->streams = av_calloc(nb_streams, sizeof(*tq->streams)); + if (!tq->streams) + goto fail; + tq->nb_streams = nb_streams; + + for (unsigned int i = 0; i < nb_streams; i++) { + ThreadQueueStream *st = &tq->streams[i]; + + st->fifo = av_fifo_alloc2(nb_elems, elem_size, 0); + if (!st->fifo) + goto fail; + } + + return tq; +fail: + tq_free(&tq); + return NULL; +} + +int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data) +{ + ThreadQueueStream *st; + int ret; + + av_assert0(stream_idx < tq->nb_streams); + st = &tq->streams[stream_idx]; + + pthread_mutex_lock(&tq->lock); + + if (st->finished & FINISHED_SEND) { + ret = AVERROR(EINVAL); + goto finish; + } + + while (!(st->finished & FINISHED_RECV) && !av_fifo_can_write(st->fifo)) + pthread_cond_wait(&tq->cond, &tq->lock); + + if (st->finished & FINISHED_RECV) + ret = AVERROR_EOF; + else { + ret = av_fifo_write(st->fifo, data, 1); + if (ret >= 0) + pthread_cond_broadcast(&tq->cond); + } + +finish: + pthread_mutex_unlock(&tq->lock); + + return ret; +} + +static int receive_locked(ThreadQueue *tq, int *stream_idx, + void *data) +{ + unsigned int nb_finished = 0; + + for (unsigned int i = 0; i < tq->nb_streams; i++) { + ThreadQueueStream *st = &tq->streams[i]; + + if (av_fifo_read(st->fifo, data, 1) >= 0) { + *stream_idx = i; + return 0; + } + + if (st->finished & FINISHED_SEND) { + /* return EOF to the consumer at most once for each stream */ + if (!(st->finished & FINISHED_RECV)) { + st->finished |= FINISHED_RECV; + *stream_idx = i; + return AVERROR_EOF; + } + + nb_finished++; + } + } + + return nb_finished == tq->nb_streams ? AVERROR_EOF : AVERROR(EAGAIN); +} + +int tq_receive(ThreadQueue *tq, int *stream_idx, void *data) +{ + int ret; + + *stream_idx = -1; + + pthread_mutex_lock(&tq->lock); + + while (1) { + ret = receive_locked(tq, stream_idx, data); + if (ret == AVERROR(EAGAIN)) { + pthread_cond_wait(&tq->cond, &tq->lock); + continue; + } + + break; + } + + if (ret == 0) + pthread_cond_broadcast(&tq->cond); + + pthread_mutex_unlock(&tq->lock); + + return ret; +} + +void tq_send_finish(ThreadQueue *tq, unsigned int stream_idx) +{ + av_assert0(stream_idx < tq->nb_streams); + + pthread_mutex_lock(&tq->lock); + + /* mark the stream as send-finished; + * the next time the consumer thread tries to read this stream it will get + * and EOF and recv-finished flag will be set */ + tq->streams[stream_idx].finished |= FINISHED_SEND; + pthread_cond_broadcast(&tq->cond); + + pthread_mutex_unlock(&tq->lock); +} + +void tq_receive_finish(ThreadQueue *tq, unsigned int stream_idx) +{ + av_assert0(stream_idx < tq->nb_streams); + + pthread_mutex_lock(&tq->lock); + + /* mark the stream as send/recv-finished; + * both send AND recvs for this stream will now return EOF */ + tq->streams[stream_idx].finished |= FINISHED_SEND | FINISHED_RECV; + pthread_cond_broadcast(&tq->cond); + + pthread_mutex_unlock(&tq->lock); +} diff --git a/fftools/thread_queue.h b/fftools/thread_queue.h new file mode 100644 index 0000000000..aaf491fe3c --- /dev/null +++ b/fftools/thread_queue.h @@ -0,0 +1,37 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef FFTOOLS_THREAD_QUEUE_H +#define FFTOOLS_THREAD_QUEUE_H + +#include + +typedef struct ThreadQueue ThreadQueue; + +ThreadQueue *tq_alloc(unsigned int nb_streams, + size_t nb_elems, size_t elem_size, + void (*elem_free)(void *)); +void tq_free(ThreadQueue **tq); + +int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data); +void tq_send_finish(ThreadQueue *tq, unsigned int stream_idx); + +int tq_receive(ThreadQueue *tq, int *stream_idx, void *data); +void tq_receive_finish(ThreadQueue *tq, unsigned int stream_idx); + +#endif // FFTOOLS_THREAD_QUEUE_H From patchwork Mon Apr 4 11:30:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35200 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1916166pzb; Mon, 4 Apr 2022 04:41:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxCUC4+1/5NfkP44lsU40V1tgatzenvVn5q3FN9MgXnQcgP2VylEzK7Y6j4e48wUTLGy6yn X-Received: by 2002:a50:ed91:0:b0:419:979a:ac57 with SMTP id h17-20020a50ed91000000b00419979aac57mr32843845edr.206.1649072493245; Mon, 04 Apr 2022 04:41:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072493; cv=none; d=google.com; s=arc-20160816; b=cFigxTF5V1rks3q5BR8rsFvsrChMCFGucQAGlFEvjOV+FFp/hUIE4+FYY3XCSa4x2V 9YmdTLlV1SverNG64W5d83By4otEfb5JFNJNG7KBjWGyuK24AE8+LtYeeOvFzf0Kp1sQ Bq/dy/8pRg4iIszP+d2nYVYxxHNVEleieNsekNktn0I79lYhewSFAZjwRBC4XDx2EZ2v GtrHx+B1m8WSkU9NUngbXwxFLs9HRc9J5syyA3XtpoSLlhVHX7NlKgHUkl3leiXZVapX YZRjg7YubSRmQG0nqUoteoEKMdNYBHa73oh/v4O86nPZFKJ+4I5E4IwaAhy5QFQLgv3y WKQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=qXOzASeDLjhxa7aDUgRwwjR/UkpSWq1IH79oYNM80mA=; b=ovGErGC+q+Yp/X44ZsItzmYrcj8P2fuLpbYMkyeu6yh8iGSwUrvDWBBLjQ88T1CZQM uo4l9GtfvWylEQqHOHihhUayfUJiYD21fGKsBXvZNRoXhxweFvFzmrWLvrvY78mVWSkI vfz6vQaNmEQKKSCr7QVtbXHaSWEayweDAF6QDL0YYhTB2g1dstHQrciIxZqAGcEMysPu QebP2PiifxHdnvOeiCfiTB5CEmCU/KrsLrbTbotjDumFZbWR2tdBPOwqJe2M+UMqwH2V 29RUbOhgZmFtLNOJho18igSkIwUg1jGTJlbRd0gXvRaOtL6UgmZdvsvSb+apvJpjr1KJ epRw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id y19-20020a056402441300b00419dbec993fsi9221465eda.374.2022.04.04.04.41.32; Mon, 04 Apr 2022 04:41:33 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1675D68B3A5; Mon, 4 Apr 2022 14:38:04 +0300 (EEST) 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 8587B68B360 for ; Mon, 4 Apr 2022 14:37:47 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 3FFBB240512 for ; Mon, 4 Apr 2022 13:37:47 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id wo0K9uLryBqp for ; Mon, 4 Apr 2022 13:37:46 +0200 (CEST) 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 E72C4240179 for ; Mon, 4 Apr 2022 13:37:41 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 56C5C3A0E93; Mon, 4 Apr 2022 13:32:12 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:37 +0200 Message-Id: <20220404113037.13070-50-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 49/49] fftools/ffmpeg: move each muxer to a separate thread 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: nZc5iy7c10Ih --- fftools/ffmpeg.c | 38 +++------ fftools/ffmpeg.h | 7 +- fftools/ffmpeg_mux.c | 197 +++++++++++++++++++++++++++++++++++-------- 3 files changed, 178 insertions(+), 64 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 9dfbc4216a..8ea27d3422 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1286,10 +1286,7 @@ static void finish_output_stream(OutputStream *ost) OutputFile *of = output_files[ost->file_index]; ost->finished = ENCODER_FINISHED; - if (ost->sq_idx_mux >= 0) - sq_send(of->sq_mux, ost->sq_idx_mux, SQPKT(NULL)); - else - ost->finished |= MUXER_FINISHED; + output_packet(of, ost->pkt, ost, 1); } /** @@ -3421,9 +3418,8 @@ static int need_output(void) for (i = 0; i < nb_output_streams; i++) { OutputStream *ost = output_streams[i]; - OutputFile *of = output_files[ost->file_index]; - if (ost->finished || of_finished(of)) + if (ost->finished) continue; return 1; @@ -4269,26 +4265,6 @@ static int transcode_step(void) return reap_filters(0); } -static void flush_sync_queues_mux(void) -{ - /* mark all queue inputs as done */ - for (int i = 0; i < nb_output_streams; i++) { - OutputStream *ost = output_streams[i]; - OutputFile *of = output_files[ost->file_index]; - if (ost->sq_idx_mux >= 0) - sq_send(of->sq_mux, ost->sq_idx_mux, SQPKT(NULL)); - } - - /* encode all packets remaining in the sync queues */ - for (int i = 0; i < nb_output_streams; i++) { - OutputStream *ost = output_streams[i]; - OutputFile *of = output_files[ost->file_index]; - - if (!(ost->finished & MUXER_FINISHED)) - output_packet(of, ost->pkt, ost, 1); - } -} - /* * The following code is the main loop of the file converter */ @@ -4310,6 +4286,12 @@ static int transcode(void) timer_start = av_gettime_relative(); + for (i = 0; i < nb_output_files; i++) { + ret = of_thread_start(output_files[i]); + if (ret < 0) + goto fail; + } + if ((ret = init_input_threads()) < 0) goto fail; @@ -4346,7 +4328,9 @@ static int transcode(void) } } flush_encoders(); - flush_sync_queues_mux(); + + for (i = 0; i < nb_output_files; i++) + of_thread_stop(output_files[i]); term_exit(); diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 407342462f..c4a5c2a0a2 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -583,6 +583,8 @@ typedef struct OutputFile { const AVOutputFormat *format; const char *url; + AVThreadMessageQueue *mux_queue; + SyncQueue *sq_encode; SyncQueue *sq_mux; @@ -697,11 +699,14 @@ int hwaccel_decode_init(AVCodecContext *avctx); int of_muxer_init(OutputFile *of, AVFormatContext *fc, AVDictionary *opts, int64_t limit_filesize); + +int of_thread_start(OutputFile *of); +void of_thread_stop(OutputFile *of); + int of_write_trailer(OutputFile *of); void of_close(OutputFile **pof); int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof); -int of_finished(OutputFile *of); int64_t of_filesize(OutputFile *of); AVChapter * const * of_get_chapters(OutputFile *of, unsigned int *nb_chapters); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 6ca9a51dd6..f99dd5ec3e 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -16,17 +16,20 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include #include #include #include "ffmpeg.h" #include "sync_queue.h" +#include "thread_queue.h" #include "libavutil/fifo.h" #include "libavutil/intreadwrite.h" #include "libavutil/log.h" #include "libavutil/mem.h" #include "libavutil/timestamp.h" +#include "libavutil/thread.h" #include "libavcodec/packet.h" @@ -46,18 +49,24 @@ typedef struct MuxStream { /* dts of the last packet sent to the muxer, in the stream timebase * used for making up missing dts values */ int64_t last_mux_dts; + + /* data (a real or a flush packet) was received for this stream */ + int got_data; } MuxStream; struct Muxer { AVFormatContext *fc; + pthread_t thread; + ThreadQueue *tq; + MuxStream *streams; AVDictionary *opts; /* filesize limit expressed in bytes */ int64_t limit_filesize; - int64_t final_filesize; + atomic_int_least64_t last_filesize; int header_written; }; @@ -221,13 +230,32 @@ static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) return 0; } +static int64_t filesize(AVIOContext *pb) +{ + int64_t ret = -1; + + if (pb) { + ret = avio_size(pb); + if (ret <= 0) // FIXME improve avio_size() so it works with non seekable output too + ret = avio_tell(pb); + } + + return ret; +} + static int write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) { MuxStream *ms = &of->mux->streams[ost->index]; AVFormatContext *s = of->mux->fc; AVStream *st = ost->st; + int64_t fs; int ret; + fs = filesize(s->pb); + atomic_store(&of->mux->last_filesize, fs); + if (fs >= of->mux->limit_filesize) + return AVERROR_EOF; + if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && video_sync_method == VSYNC_DROP) || (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0)) pkt->pts = pkt->dts = AV_NOPTS_VALUE; @@ -333,8 +361,8 @@ static int check_write_header(OutputFile *of) int ret, i; for (i = 0; i < fc->nb_streams; i++) { - OutputStream *ost = output_streams[of->ost_index + i]; - if (!ost->initialized) + MuxStream *ms = &of->mux->streams[i]; + if (!ms->got_data) return 0; } @@ -378,12 +406,15 @@ static int check_write_header(OutputFile *of) return 0; } -int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) +static int sync_queue_process(OutputFile *of, OutputStream *ost, AVPacket *pkt) { + Muxer *mux = of->mux; + MuxStream *ms = &mux->streams[ost->index]; int ret; - if (!of->mux->header_written) { - ret = check_write_header(of); + ms->got_data = 1; + if (!mux->header_written) { + ret = check_write_header(of); if (ret < 0) { av_packet_unref(pkt); return ret; @@ -391,34 +422,102 @@ int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) } if (ost->sq_idx_mux >= 0) { - ret = sq_send(of->sq_mux, ost->sq_idx_mux, - SQPKT(eof ? NULL: pkt)); + int ret = sq_send(of->sq_mux, ost->sq_idx_mux, SQPKT(pkt)); if (ret < 0) { - av_packet_unref(pkt); - if (ret == AVERROR_EOF) { - ost->finished |= MUXER_FINISHED; - return 0; - } else - return ret; + if (pkt) + av_packet_unref(pkt); + return ret; } while (1) { + pkt = av_packet_alloc(); + if (!pkt) + // XXX + abort(); + ret = sq_receive(of->sq_mux, -1, SQPKT(pkt)); - if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) - return 0; - else if (ret < 0) - return ret; + if (ret < 0) { + av_packet_free(&pkt); + return (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) ? 0 : ret; + } ret = submit_packet(of, pkt, output_streams[of->ost_index + ret]); + av_packet_free(&pkt); if (ret < 0) return ret; } - } else if (!eof) + } else if (pkt) return submit_packet(of, pkt, ost); return 0; } +static void *muxer_thread(void *arg) +{ + OutputFile *of = arg; + Muxer *mux = of->mux; + + while (1) { + OutputStream *ost; + AVPacket *pkt = NULL; + int stream_idx, ret; + + ret = tq_receive(mux->tq, &stream_idx, &pkt); + if (stream_idx < 0) { + av_log(NULL, AV_LOG_DEBUG, + "All streams finished for output file #%d\n", of->index); + break; + } + + ost = output_streams[of->ost_index + stream_idx]; + ret = sync_queue_process(of, ost, ret < 0 ? NULL : pkt); + av_packet_free(&pkt); + if (ret == AVERROR_EOF) + tq_receive_finish(mux->tq, stream_idx); + else if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, + "Error muxing a packet for output file #%d\n", of->index); + break; + } + } + + for (unsigned int i = 0; i < mux->fc->nb_streams; i++) { + sync_queue_process(of, output_streams[of->ost_index], NULL); + tq_receive_finish(mux->tq, i); + } + + av_log(NULL, AV_LOG_DEBUG, "Terminating muxer thread %d\n", of->index); + + return NULL; +} + +int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) +{ + AVPacket *pkt1; + int ret = 0; + + if (eof) { + tq_send_finish(of->mux->tq, ost->index); + return 0; + } + + pkt1 = av_packet_alloc(); + if (!pkt1) { + av_packet_unref(pkt); + return AVERROR(ENOMEM); + } + + av_packet_move_ref(pkt1, pkt); + + ret = tq_send(of->mux->tq, ost->index, &pkt1); + if (ret < 0) { + av_packet_free(&pkt1); + ost->finished |= MUXER_FINISHED; + } + + return ret == AVERROR_EOF ? 0 : ret; +} + int of_write_trailer(OutputFile *of) { AVFormatContext *fc = of->mux->fc; @@ -438,7 +537,7 @@ int of_write_trailer(OutputFile *of) return ret; } - of->mux->final_filesize = of_filesize(of); + of->mux->last_filesize = filesize(fc->pb); if (!(of->format->flags & AVFMT_NOFILE)) { ret = avio_closep(&fc->pb); @@ -487,6 +586,9 @@ static void mux_free(Muxer **pmux) av_freep(&mux->streams); av_dict_free(&mux->opts); + if (mux->tq) { + } + fc_close(&mux->fc); av_freep(pmux); @@ -558,30 +660,53 @@ fail: return ret; } -int of_finished(OutputFile *of) +int64_t of_filesize(OutputFile *of) { - return of_filesize(of) >= of->mux->limit_filesize; + return atomic_load(&of->mux->last_filesize); } -int64_t of_filesize(OutputFile *of) +AVChapter * const * +of_get_chapters(OutputFile *of, unsigned int *nb_chapters) { - AVIOContext *pb = of->mux->fc->pb; - int64_t ret = -1; + *nb_chapters = of->mux->fc->nb_chapters; + return of->mux->fc->chapters; +} - if (of->mux->final_filesize) - ret = of->mux->final_filesize; - else if (pb) { - ret = avio_size(pb); - if (ret <= 0) // FIXME improve avio_size() so it works with non seekable output too - ret = avio_tell(pb); +static void pkt_free(void *pkt) +{ + av_packet_free((AVPacket**)&pkt); +} + +int of_thread_start(OutputFile *of) +{ + Muxer *mux = of->mux; + int ret; + + mux->tq = tq_alloc(mux->fc->nb_streams, 8, sizeof(AVPacket*), + pkt_free); + if (!mux->tq) + return AVERROR(ENOMEM); + + ret = pthread_create(&mux->thread, NULL, muxer_thread, (void*)of); + if (ret) { + tq_free(&mux->tq); + return AVERROR(ret); } - return ret; + return 0; } -AVChapter * const * -of_get_chapters(OutputFile *of, unsigned int *nb_chapters) +void of_thread_stop(OutputFile *of) { - *nb_chapters = of->mux->fc->nb_chapters; - return of->mux->fc->chapters; + Muxer *mux = of->mux; + + if (!mux || !mux->tq) + return; + + for (unsigned int i = 0; i < mux->fc->nb_streams; i++) + tq_send_finish(mux->tq, i); + + pthread_join(mux->thread, NULL); + + tq_free(&mux->tq); }