From patchwork Thu Jun 16 19:55: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: 36275 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1128791pzb; Thu, 16 Jun 2022 13:06:40 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sFHyS+2NVraSXuOBSKUs5ogjjhT6U5esAa0Q7CTkle9pP7ZOsXkHeRBR6V1PBvWIbaX9h6 X-Received: by 2002:a17:906:58c8:b0:6fe:91d5:18d2 with SMTP id e8-20020a17090658c800b006fe91d518d2mr5896831ejs.190.1655410000456; Thu, 16 Jun 2022 13:06:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410000; cv=none; d=google.com; s=arc-20160816; b=AyxmPU8RBTMlE+5XAcESFsIinP/bQR08odvdwfl8FAUj05KlmI46ex/60FW6N3PiqR 7h927WiE7nIyeS+orEEcCI77Sf+oGa72ENUSNAe6zdmmyVLXbuWdFcvomtp3WtAMpyiD iAE864a8jIpN59MTcksM3KvGID8Zp1tfMZt7JGF2n2ygbsVjG53gcxZYPF/dHzu/wI/g 7flanbh6ot1QqZZp8asJlqybw+aGyjQJb32BnIcwtFpNJFf+zUSb6iWgESjZ9gWrrzW/ Is+bNNDHktE0pDpo2fL5PRfo4tWGDySJT/GlI3UC1WAP9ZTvqnZRhfCf/ssj0Qek6YXs kJuA== 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=Wz29PtkmVAOw96fvPJgAL7t67YQ0Zfq31vgZ2TOQQ1M=; b=rJ3crLlKsa6vEheE8+lzvE9aAP3uwPYlhQ7U21t15vx1+8cG7atNYtD1DD8eWHrU8V Y1wOReLSZBY8fySOTT8ctICpl1bYmTztA5gZsCNVnEQStgt9NJLEjW3INBR3NbIqAvnF ByAGwEhh2RDdXiTNGK+ENXqPYPc1yF0UbNVtx15uHuOas32nZR8P/u3+lJ4o14EIhDWs jav6eqzB2Wj+6SitjnelvzDnFcLiLGY9MS6r/zLEE7jVmigPvo5ZxJEbOWfOSXI/X9Lb 4KkTaKfi6d8wq0rIObBOHsT7+MrHtJSeernsVhMz/NM4uwBnuxoy+nozxvNa8rgtD5hd sBKw== 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 hg4-20020a1709072cc400b006f392f11feasi2937834ejc.20.2022.06.16.13.06.32; Thu, 16 Jun 2022 13:06:40 -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 70B3F68B8B6; Thu, 16 Jun 2022 23:03: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 D054468B842 for ; Thu, 16 Jun 2022 23:03:40 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 8118724052D for ; Thu, 16 Jun 2022 22:03:38 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id fnV_akUYkn4k for ; Thu, 16 Jun 2022 22:03:38 +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 BFAA3240511 for ; Thu, 16 Jun 2022 22:03:33 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id ABB343A17CC; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:11 +0200 Message-Id: <20220616195534.5278-12-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220616195534.5278-1-anton@khirnov.net> References: <20220616195534.5278-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 12/35] 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: Raa5IoxzRdej --- 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 d49235ee63..98637004aa 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; }