From patchwork Thu Jun 16 19:55: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: 36279 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1129099pzb; Thu, 16 Jun 2022 13:07:11 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vjtChSX8is1zP7IfPr5CW8qPPuJKHQQGGlnwTuASMoG5NOtnai+v+cFagSBA49t3NYg+O4 X-Received: by 2002:a17:906:b208:b0:70e:c7f1:f8bf with SMTP id p8-20020a170906b20800b0070ec7f1f8bfmr6066129ejz.143.1655410031125; Thu, 16 Jun 2022 13:07:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410031; cv=none; d=google.com; s=arc-20160816; b=FwGyLQPp/Wm86g65+hl9mOlkn5rEepJN4+MlHVRnF9envqxHhuxPv3FyHFj3GUuCfr 4sf2Ht3m709DkQjCSPSR3YNWgJ8RXWafElr/D8dckCc4ocxl84z9GQd9p60zqbPlbbN0 J3Qow1dP9MtlBQcL2BhpcPqmmRGDEoAQljKD1ICbxIUkYWuFUrXBCsKBJ/gHWKPvEF4w iviJPmb5LNKvYfYaJz3NCWsaD++InTVkuUB8nZFZa/5aOuOE+lJNiKXDUeIo7dlvdbp6 j8JqjXnDax8d77n18PeFoiIJTud/hXWk/MLqjfoNsWGpnUu2Oz8Ss3KUlI13NiK4FPoO MnJg== 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:message-id:date:to:from :delivered-to; bh=Co/MbI6RlNTAKVeKQM+V1SuItBpZN6cTD4OuUYNAozk=; b=LwBVFX/or/s9/6b6rU9fukJfbhplDFTFCZg02nAigVukjlcszHAs2nz09en9LbFoui xxWnYdHUJAjsnmSU/wVTLD1H2vv6ZkmlCMkP4NNbrZYHf5CwMpM+NqcMOkE4nYlGWT9o fLo1uFPHNmyNkkbP77PR9LWNUZRawDTX6euc/PIqN8AGxDb6di7E9I28JduN86HjhI94 XQTTi+4zhYkeyqtwW1QQPF9JoAIsKOZoerR+wUs/V0f6pRRhNnUK+g/W4a3tCHiUumNd drBGKGAKve+Om+cG97Mm50i2Gyq/Ipc3lkQvfEPJVKN8n/EZaXjPXG7o40yodi0klm0t WDug== 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 k18-20020a05640212d200b0042dffafcf4dsi768005edx.35.2022.06.16.13.07.10; Thu, 16 Jun 2022 13:07: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 5E04F68B8C7; Thu, 16 Jun 2022 23:04: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 90E7568B850 for ; Thu, 16 Jun 2022 23:03:41 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 86D27240512 for ; Thu, 16 Jun 2022 22:03:39 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id D_3KlKcVwZE9 for ; Thu, 16 Jun 2022 22:03:39 +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 0537F24055A for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 794913A0A3A; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:00 +0200 Message-Id: <20220616195534.5278-1-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 01/35] 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: F5vOJuyi+5Zn 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 69a368b8d1..c6ffe9fdf6 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -581,9 +581,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; @@ -594,8 +597,6 @@ typedef struct OutputFile { uint64_t limit_filesize; /* filesize limit expressed in bytes */ int shortest; - - int header_written; } OutputFile; extern InputStream **input_streams; @@ -694,6 +695,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 a55fd18f8f..e47a55c4e9 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)) { @@ -182,7 +186,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; } @@ -246,7 +250,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++; @@ -282,7 +286,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", @@ -313,5 +317,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 398067da96..ab8d307c3c 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2947,6 +2947,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 Thu Jun 16 19:55: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: 36260 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1127511pzb; Thu, 16 Jun 2022 13:04:08 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uTtiA9bHn4RMHhdRNliM2kPZlAB+xanT7TZh1voHo9Gqz3Qh/IYObr2b9xwMAB+CkwVHEt X-Received: by 2002:a17:907:d15:b0:71c:9ef2:2440 with SMTP id gn21-20020a1709070d1500b0071c9ef22440mr2182082ejc.266.1655409848584; Thu, 16 Jun 2022 13:04:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409848; cv=none; d=google.com; s=arc-20160816; b=n5wz53zfLXz3Yc9ddFRVEgbww8+vKs2xH7ie3t/sA43JnpFUTqe9sLzLX6tsqHLPJk kh5m4axkz2tjl5MWq+ESb3gtq30QfGUZhOgfwXjRoN3sHqjGlqwfeP4JqpU6I7FP+6Bz KMJDbMwnTB5dofvY3Guvb9EwlvA9ugoY40IJVhVusWDgSfR8JUGnwIQm/+uDb5vWafie 3UrjCWA+T/pj7EOHgXExSvfMOIneZycVPwR4fuQg0HG55s5rci+x94gb8BNTVXEpiWdW +sKPru/LGN8dkhPlY44NB13z1m2cS2FViB42TuBlJar57cMuHNj5OCwsnDQZt0qHpHwj FHPQ== 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=1fJ5JRfV1RqXcxA5Xk+DRNLctu8A0B8fbINKn9fETcw=; b=pI9gFTHlP5Y5zIq84jrYhoZuZznRlNTmuXBSyRljGsy4d+/wiKa4b57iH+9rJhuama hFJTC3rS+g3Ev7ynV3tv45qZ1jFwulbucpNvI92/cFOicnM5R8QCU3M2OBzyUNs615yM gz+aK29u7BFNajFaKeDtFTKTCPDJ0C6n4x9Zaa5jjbFD3PJv/m3glFqiNSrT6y3wBA6U fgS7bW3EsLQZAfYvAChoL3OWv6Y8DACk9NydMEdipQhruwypJTiFP/UK1V4QbR7JM/5G zpgTUms0egZBsNS+zb8CZ1zBqwwgwCdBL830nLzpGFuEsJ5dwPU++NorNjHck3xotdzn BuVQ== 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 qc25-20020a170906d8b900b00711d56b4ec3si2218119ejb.795.2022.06.16.13.03.45; Thu, 16 Jun 2022 13:04:08 -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 11AE668B7B3; Thu, 16 Jun 2022 23:03: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 C512768B7B3 for ; Thu, 16 Jun 2022 23:03:34 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 2DC802405F0 for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id Mt1OZUvFCS3d for ; Thu, 16 Jun 2022 22:03:33 +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 92B582400F5 for ; Thu, 16 Jun 2022 22:03:33 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 7B8423A0398; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:01 +0200 Message-Id: <20220616195534.5278-2-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 02/35] 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: zPWa0iWnTbwq 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 09caa3e3c4..5763f231c6 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1508,8 +1508,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; @@ -1538,13 +1537,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 c6ffe9fdf6..10a51db469 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -703,5 +703,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 e47a55c4e9..83558f7f7d 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -333,3 +333,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 Thu Jun 16 19:55: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: 36273 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1128612pzb; Thu, 16 Jun 2022 13:06:19 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sgXNfIYwNi5P4kV91WP+Py7Jj6AG/APaqBIB8Geom9yVpAxTM36G9WURrLyac0co/moVCb X-Received: by 2002:a05:6402:139a:b0:431:6b50:11a2 with SMTP id b26-20020a056402139a00b004316b5011a2mr8861518edv.72.1655409979686; Thu, 16 Jun 2022 13:06:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409979; cv=none; d=google.com; s=arc-20160816; b=n1ABGo+wwgy5pkfJcy3ijkvhBvil3ccBkdblvQvHKT0RsIqOXzN3vBnd6W800lJscq aXCIFJzfzEInQmz8N+Tk3XtsUGTITERr95JeVG9sqS5dbrQzGZGh0E9Z+baEHt7Llh1m Pdrh9P2hXMKxDLOtEjtSNAwUWjVbMA/ov4Z19rqbVLJ4TsGGy530cH61bMuZi17Ce4wa cGgAfHqaE4XjPIUw/nM92pSltVUByVmPg9+5jrDZrf3PiqiSnd8AtK/k1a9jLQmnmY5B ptxWrx4EUPfYfC8xb9VTHmyMB8E77lfi47l6qmo+0+fd0mdCkN7Ei9exQPvSWO1r7lkG +zcA== 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=Zca3WHjZFqKSxfqrukFyc6CfN0IOjR7jACuPGBH+A4o=; b=clszDnZsTh41VUveuJO8XQE5x4dg3W5WKV+D3/9ck5b8Sdb7nPZBWnVpCrTEVDc/1h QwqSUqgJq9CwZcDPGXBEruI1OQWdh5gFGuiqb6ApDTuDZlIGjc40giBPSAUGLVCeq01F 1K06/jOEJKIZxKVv3sHoh1nm2x7jahEYTX7eJfFrzswP1OSI4Z65TRiTC/kz2DgNNeNA CYkJiRMwQEcQVW2b+sNTRZpsFleuITYkEWAzORvupRV61rjAnHiaPLbtuz/6rtAcDxvs MOWUsL9SDbrTBU//jir0nWCdNbijB0shVFN0QI4/R0C7M5lBMJWhuOi7QaafYHJO4HjF 2xTw== 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 i19-20020a1709064fd300b00718c984da20si2810512ejw.908.2022.06.16.13.06.12; Thu, 16 Jun 2022 13:06: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 C2E3768B8AD; Thu, 16 Jun 2022 23:03: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 413AF68B83A for ; Thu, 16 Jun 2022 23:03:40 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 0F66024050B 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 rvh1hLaoDO1o for ; Thu, 16 Jun 2022 22:03:33 +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 94A6E240175 for ; Thu, 16 Jun 2022 22:03:33 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 80ABD3A11FE; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:02 +0200 Message-Id: <20220616195534.5278-3-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 03/35] 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: sMsHN1qyoNgh 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 10a51db469..d126f5d8e6 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; @@ -594,7 +594,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 ab8d307c3c..8cd07eddb0 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -230,7 +230,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 Thu Jun 16 19:55: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: 36259 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1127499pzb; Thu, 16 Jun 2022 13:04:07 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sT2JYXLz8ilJOUCjZRZ7aTmdLRZjllpm+av7+mW2ZZpoVZZyWOV3r8Tjo4dPUDuoS7D/QT X-Received: by 2002:a17:906:dff9:b0:6ff:2f22:7d0 with SMTP id lc25-20020a170906dff900b006ff2f2207d0mr5912019ejc.198.1655409847003; Thu, 16 Jun 2022 13:04:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409846; cv=none; d=google.com; s=arc-20160816; b=nl+ISEh9jE8bq0PYknMTp8xeB37qb8e6SnHb4n14Ue8iUD2pIE3WR84OYEX56UDdj2 TRULYr1eEpf6Oiqj8wckRiHOg06qlhV0uP7NaN1ebaEJcVVp5MxwL1+bGp5s9Im3YeiG +2WDPAoWH9Oy7SQ213KV6XCMy6rM8wo4d0RJ+F47u2I3S+dX7iDphI1WaAHu18llKXtj lbUWT9C22HdHlNRISbeGHVyIy01K60YBityNHVu3erCzEFNXi4xjsInk2W+MvXQKnFRz 4jCLBhlwc07+rRQRd86Zu5XmSgJogABRR9AhpbO4UzO7VvLv6xVf6DSanMKOjS3i5ydT E75A== 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=6xJvu9N9I3jNuYLMPzOsk212UKLxd+8FPyUZPT7wJEg=; b=mYDyeKqW6CT24mf36RQXPc9bNvowKXK/7DNt+dm9vHky7vXuvomYusOpSJw5RD6873 iob3jggjzztquLpdky/hSFD+DFkUXz0TiB3/cnrDWywhaaMGrNhBeLyf6he9hGrSqNEa h15K+667cc75jYYkwO+WMtrZAZYVaqGX4Mk+LOTpCpeOGfjguCqHhlZdbayVoIXSLMWu 02B8GWGMUJiY8hYWa2C6ZtWcogDKbCwyTZwNlBfi24tVzTb3482OZTzr9GgW91tKkL0j X0uPrfMznwxbcAcx2+JfTbzLE1pUf3N5opmGD3PzbEo5lGbmK40CTxi/JgBx3di0lGl/ m2XQ== 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 oz42-20020a1709077daa00b006fe4c66b711si3126283ejc.46.2022.06.16.13.04.06; Thu, 16 Jun 2022 13:04: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 1457968B80E; Thu, 16 Jun 2022 23:03: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 987C468B7B3 for ; Thu, 16 Jun 2022 23:03:35 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 4CA1D24017E for ; Thu, 16 Jun 2022 22:03:35 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id uL3EDKtJdAH4 for ; Thu, 16 Jun 2022 22:03:34 +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 9E93924017C for ; Thu, 16 Jun 2022 22:03:33 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 85C953A1209; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:03 +0200 Message-Id: <20220616195534.5278-4-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 04/35] 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: Jc1bV6Mdqgls 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 5763f231c6..8d5f071fd1 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3454,10 +3454,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 d126f5d8e6..a97cd2a8e4 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -594,7 +594,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; @@ -695,7 +694,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); @@ -703,6 +702,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 83558f7f7d..207231f33b 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; }; @@ -322,7 +324,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)); @@ -331,9 +333,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 8cd07eddb0..28809d0c0b 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2344,7 +2344,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); @@ -2947,7 +2946,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 Thu Jun 16 19:55: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: 36258 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1127422pzb; Thu, 16 Jun 2022 13:03:56 -0700 (PDT) X-Google-Smtp-Source: AGRyM1viPrZN4mZZvbu4wLDtirQK9kn0kxKxWJzlNmWydjSfL9cRFJsZ/w/WsW1cWfTf518DZDX3 X-Received: by 2002:a17:907:7da2:b0:711:f36e:b855 with SMTP id oz34-20020a1709077da200b00711f36eb855mr6159366ejc.308.1655409835799; Thu, 16 Jun 2022 13:03:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409835; cv=none; d=google.com; s=arc-20160816; b=CYSBCbbTg2WuTiG06S23YDi6A9o73X1mIwP7n9V0m5GOuDkxWrtTy5p0nyByCb64RB oa+qjYPX7uKq49KcJjKpG8O8qt30MoP389qpuG0YnCHJTUh1kURn88jv+X4yfs7vdNNh S4AjUgeHgSOVUiTGoimbiGXvZ6PMXFiesagbeGQUEDgUCG7OwtDYJ7i692af36Nrk9h5 jH+7ZpCvPY5gy+DpFz980PKlKi+tofw7RmJxC60gcI3xaMREo+xboFF/EluZndFuOhim 6i59VN5IEgWmRhDpnzSWT3nsU9QzkZylFFZeZALRVCjiTmAp+efrggtPzJRfEoebbd+2 DzHA== 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=kyNdR/6jsa646si/p0dTeBzkiYuGbwj5+2HmZR3v7u4=; b=J5Ymkr5xV4iFFVzUGVuTucUkP2BTLsnYB6RApDBO2uvJzF1nf3Y/Pb07AxWA1DfVuL dnk4mNi6tk9RuxPhl83zJJDk9oGHEHZMUw9w9p+99Bp04p8GmYcB8A7A/F/w7ecDJ/hC BCJNV9Qik7/j9l1u4N6/QdZZJuqV/0epI8EkI7J+LCEnSE865qs8OEDDtwwBmSwEpgpr df2g3+gSK9MR0VNh0O4v9TgzMJk4XMh8266XBbkQFddztH4J7mqhf6vsNdwQJ8llEC+1 hKs1qp6ykoIvJTAcudzmLJf8H4tS5gPiRwch7W7ijOUj5R4rpXBlEzdVHNmnz6h7PzXT 3HHw== 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 f39-20020a05640232a700b00425d3e0c7a9si3247787eda.151.2022.06.16.13.03.55; Thu, 16 Jun 2022 13:03:55 -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 F237068B810; Thu, 16 Jun 2022 23:03: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 EF6F968B7B3 for ; Thu, 16 Jun 2022 23:03:34 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id ABF1224069C for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id KOjBw54bwv9L for ; Thu, 16 Jun 2022 22:03:34 +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 9F55B24017E for ; Thu, 16 Jun 2022 22:03:33 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 8AA6B3A1249; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:04 +0200 Message-Id: <20220616195534.5278-5-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 05/35] 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: SO1Z2sWBeO9Q 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 8d5f071fd1..51616cf51d 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -137,8 +137,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; @@ -4505,7 +4503,7 @@ static int64_t getmaxrss(void) int main(int argc, char **argv) { - int i, ret; + int ret; BenchmarkTimeStamps ti; init_dynload(); @@ -4541,11 +4539,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 a97cd2a8e4..e3cf7794e6 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -654,7 +654,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 207231f33b..db04b7858d 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; @@ -335,6 +337,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 Thu Jun 16 19:55: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: 36261 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1127573pzb; Thu, 16 Jun 2022 13:04:18 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sKbsMjXmGn3bmmKbpdMRC5S/pgTyiWG/QhU8y3IHTYs7H8nE73bs2GMWYL7eOHhBBJ520J X-Received: by 2002:a17:906:52c7:b0:6ce:a880:50a3 with SMTP id w7-20020a17090652c700b006cea88050a3mr6076547ejn.437.1655409857806; Thu, 16 Jun 2022 13:04:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409857; cv=none; d=google.com; s=arc-20160816; b=jehHxDGDLb6E1p0rIWpvliu3jbiqd/ZQK31gHeAJSey0qz4/Jheewras+fu8gVHEqR a5+YLXa8LQd8Sne6+piySME8uNjBxiHJ4iDayF7qBSmI3jXBvd1G5yDfhwddafUUE+Jb mFzW1HDrDmGTdDqebpzCXGbD37lUlSVc8ZmXkdR3emm/X0DyCcR69AAFoAj/pK+xyK/F 0jNYafJjT12j1o79MvXcI2ZJ7zoYzVh+IsjUkIGCrGpgSnZxQFo5FtkldfrB2W38f2Dm thqiQ5trshaNCQHf/cqJzQBmpfO7p26StXc+lJaaSylXHYWyqyfuzB0Ih6f9rSOcvZJ8 Vzdw== 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=MBSVGK2EXdyukbUL4rIyhSi9oqJ2eVyj1o2WKlb16ls=; b=CcvCE05mh7SjKhMdgoiwepKh4UgCyzjq3cfBNkemtk7knOyGf+r06WReL2pHk9KQfu CfEfqFVrKbyxXhRqD8N6TG+NO8cm/SUIK+wKx4jncobWQevG0F79dUA+oc2UGCn/al3o SMTOBiGjBJIi/C+XCHuiglasecCzU6V7Qmg8buHPZPT22s1Ic3ERliCwHz3yg63eqIh7 CMYIvGzIJ1wip38MpAYJiAVTldfBqYzs2lyQDYatwyNG+jZBJIMDk/FGUp9tPPBPhpCa 0821fHzYIJ9plOVx48ISeCSjNJkejzZAFAEDSCEm3nTgQk7ZDIk3ZvNjVrvyZeE686Nd oWgg== 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 ey8-20020a0564022a0800b0042e09854962si3178161edb.608.2022.06.16.13.04.17; Thu, 16 Jun 2022 13:04: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 2C65668B834; Thu, 16 Jun 2022 23:03: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 3E68F68B7DD for ; Thu, 16 Jun 2022 23:03:36 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id D8C3624017C for ; Thu, 16 Jun 2022 22:03:35 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id rkf5syncI5N7 for ; Thu, 16 Jun 2022 22:03:35 +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 A39CF2404FE for ; Thu, 16 Jun 2022 22:03:33 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 8EEBD3A127F; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:05 +0200 Message-Id: <20220616195534.5278-6-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 06/35] 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: Q8+xHJ145sDr 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 51616cf51d..2463056f8f 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3266,7 +3266,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}; @@ -3340,16 +3339,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 db04b7858d..396e91184b 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -340,6 +340,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 Thu Jun 16 19:55: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: 36262 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1127676pzb; Thu, 16 Jun 2022 13:04:28 -0700 (PDT) X-Google-Smtp-Source: AGRyM1slHgwuLDwjIjqH9OD1q3+Ikxbcjz/XIIIU8Msx1IKrSpLpLKAOleP61ORYFdllvVBV/6DT X-Received: by 2002:a17:906:b:b0:718:d0ff:fcbf with SMTP id 11-20020a170906000b00b00718d0fffcbfmr6029156eja.717.1655409868711; Thu, 16 Jun 2022 13:04:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409868; cv=none; d=google.com; s=arc-20160816; b=opmY0jIlx/TVm/i0qRpATQFWBqn8kbgl0/207+e7YHGMiCYxfaouVZ1v9vBZCtuBj1 bkyKtak5E2XuD4CSUMoByFIWrWHKfCazjgIlyrBS4DDc4UzhYMphcnklvD6n2wmmgk2H uJeMITtfylKsYlD1G0Z1PXaDxFXUdz8J31FlWp1h9WeugHpTi6DcqIXw/+XjeM/OR7xb /JvyyOAA0k+Fpza9zgQRs7cpK5ikvFSADR4Lw0z3isuL9Oz6pNir+v9ALqKlE7HpU8Vl jjlBv+rK+qfLOAm/rgoy7XbDM2YynrcRuo3dNQalkweiRcMuXMRO1Lnjh8g1JlWv5jUt l01A== 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=ZVWwZurcQuiXq9M4p0L8XyFVrvwQW1ccrqgsT8WrtP4=; b=nO256wSBfmIF/i/+fEjWqME3ixu4RIaocQGWzA7ml/AKy/jnRg+s81cFWv6yKalTGZ mRpKU7pCIIzG5T/udH36aNyOLSNefELSYKPk4axKtWOlcblpCaeVTB79yncwhIC3nvzB LVo4NUA9e4gUfnqFGujaAa1GKhCYNCukux9Tj34QQdAyBVUcJsqAxpYgpB7gOtlpCn4+ XmAjkReH411O/+UBZWHcSgHnHdZeW2VUimpUCVG7/Fm23p587R2jRKGlVugpA3Woo/mZ tUVXbA0lQ1LyZp6W6R9NDPF2Ryc/ulZBF0D9EWtePpW6045gIxB+sQlXumi8LaIbsIJl Y3Mw== 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 qf38-20020a1709077f2600b006fed9433169si2996906ejc.826.2022.06.16.13.04.28; Thu, 16 Jun 2022 13:04:28 -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 2BFBB68B7FB; Thu, 16 Jun 2022 23:03: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 CB62D68B7FB for ; Thu, 16 Jun 2022 23:03:36 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 6C3142404FE for ; Thu, 16 Jun 2022 22:03:36 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id NwmUjtTxVq43 for ; Thu, 16 Jun 2022 22:03:35 +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 AF868240506 for ; Thu, 16 Jun 2022 22:03:33 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 938DE3A12AD; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:06 +0200 Message-Id: <20220616195534.5278-7-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 07/35] 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: tGGZ9XSTrbLa 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 2463056f8f..0364948b1a 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -4304,7 +4304,6 @@ static int transcode_step(void) static int transcode(void) { int ret, i; - AVFormatContext *os; OutputStream *ost; InputStream *ist; int64_t timer_start; @@ -4373,18 +4372,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 396e91184b..78395bebb4 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; }; @@ -304,6 +305,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; } @@ -360,7 +372,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 Thu Jun 16 19:55: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: 36265 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1127957pzb; Thu, 16 Jun 2022 13:04:57 -0700 (PDT) X-Google-Smtp-Source: AGRyM1srzuglUCfpCtRrmQlA+F0W+kqs53J0IPgc9SW664riXVeQT7EJd5nmUg2tt6fwtoaM0Iyj X-Received: by 2002:a05:6402:3546:b0:42e:2f58:2c90 with SMTP id f6-20020a056402354600b0042e2f582c90mr8709894edd.84.1655409897278; Thu, 16 Jun 2022 13:04:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409897; cv=none; d=google.com; s=arc-20160816; b=jH8H5sZUqQM5p2KFemOjMSGNRZkxcOIzV7hcs8EOjEZTaA9wCdV/EWZEx+5aoMMDVt OyRV4Ds/Cp7v39uLukrPWNLbdE4tqQgcPuptZVLnaHEEwGsc199abPu7ldkT0K5Tqw29 OPwaaLVoy3J6Gf6BdoT+GEFrkisCalzpQ09rBTrY93XU/ixDXJnas545TyZgTQYT8L9n 85MSXOQ7cug887wePfNGTj9niaXk6qphRx7v++bGN4p7x19XDiCm7EPkQ0JCYoTE6Su7 K3BL8eZ1/fl16LLylTY8/UG+Pz2Dk6PTvxFUmFbdiLu0Gu/siOvGTYpMA6OoRDfTeAdL DVuQ== 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=I4IiVY7baPeAZKd3TtIIJGvxnxhRsxu+a+KMxAGcrQc=; b=QW/4tunj0AvVF+cZVrvuq1/nqjJHuOPMKUWhMUgpzc0qvZSxR6iR9hEXtDAvJVQhza 8BMByYjxb/TSFthzT2BVNOJscVRdbHq6/biXhjRcIriTsaH4c1jnHo5G6Fm8mdNMtSC6 fQKPUlZTPEGZQAikwdoplgn8uopThfXh9tlWlYQZhjBuUAH5kLspqqiZqKz5jBHlC3cy MxyCzp9NG1NXDgXsdtZzst+9aplkp1JJo6lMhNgs3BLOmv66QWpFyeA6xply0KnTc3HG fFczOczgGAOpZEId604RcxRFl1QGaxR+wDaXtZQqRy0JPlbAiF9xpvM1NFpcQvKHpGhX h27Q== 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 s19-20020a170906779300b00718d0a2a448si2874194ejm.25.2022.06.16.13.04.56; Thu, 16 Jun 2022 13:04:57 -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 EB14A68B882; Thu, 16 Jun 2022 23:03: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 EA7D068B800 for ; Thu, 16 Jun 2022 23:03:42 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 601382404FE for ; Thu, 16 Jun 2022 22:03:42 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id DSudUFByy8ln for ; Thu, 16 Jun 2022 22:03:40 +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 29E31240593 for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 987DF3A12C5; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:07 +0200 Message-Id: <20220616195534.5278-8-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 08/35] 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: JEwya7n5XHqL 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 0364948b1a..75c2322091 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2815,37 +2815,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 e3cf7794e6..ea226eb901 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -496,6 +496,7 @@ typedef struct OutputStream { int top_field_first; int rotate_overridden; int autoscale; + int bitexact; int bits_per_raw_sample; double rotate_override_value; @@ -596,6 +597,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 28809d0c0b..6f2bf33d7d 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -1459,6 +1459,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; @@ -1551,8 +1567,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) { @@ -2365,6 +2386,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 Thu Jun 16 19:55: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: 36276 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1128847pzb; Thu, 16 Jun 2022 13:06:46 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vd8Ts6CVyaV+iBd+Ns1/Evqa3eWUhaj+AcqrDFGpWK5bRzAxYLUVN8x95CpSbu6QqjL9Dw X-Received: by 2002:a05:6402:190d:b0:431:4e5d:d780 with SMTP id e13-20020a056402190d00b004314e5dd780mr8649397edz.279.1655410005920; Thu, 16 Jun 2022 13:06:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410005; cv=none; d=google.com; s=arc-20160816; b=qdEXes5ZDFB0Wwz1pIwEAV7tFgpxc/umOj2oMeaBXiRGC1OIlZCzO0H0g6ICEAtpCt ULxZZbW7DPzxRca8QZv8d9m22lvxHlHQD9RWYCPXYy3STevoQJzrlOWzKOEz9seLhXv3 bZvqe1oHGxsFL4cmmdm1Vk4NQMKI/BCqY2kyDF+UyDiQWXUcsTwsKZTIQm3EX57iYIYT sADcHk1WBX/1qVKMnhq0aisqAuoETH4L0ZGoMXPQsaVEih5iZYUAZszGnmla5IFFAXyX H7G/wV6b1ulwgrF8RnI1EcuR7CdYio5H0KCKUk2XoqiA7h1GTkC8AOiTo+nC+EggX8zo jQ3A== 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=53Ynzo2FOo8MWlKHQuhGFmeVRQB8V3kD3Ih10qOMYEs=; b=xQpGAGC/tiqBc0csvrrW/+Pha2EfDO0oboBIckRhlVYUu35rJu4mMi4c340zkO2lrp isToMyx76IKIxfGKZJtZunJ/ICTFX9gMFDL/nIW7H7xgP7ywckiTBDNdxQJkTRtR01kD SVTeicfryu1019czfTO+BjQsGFpWFINFUc8GqpX0q0F2na//e0bcW0dtsVk+fDwOcFz6 /UWA6Vh6McBtg3PYKTM7XEsrCfMAtIFdhyedkWEYWRVTQy5cZkCZ+U11TwvZxghQLjt7 NjA+I0EQrFm+xAoG9EGsK0Nx5qnH6xS+kRiWLVFZQtm1yn/QEHMFmzHeu0OAkgA8K79M RNlw== 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 eg42-20020a05640228aa00b004351e9a54d5si3084205edb.351.2022.06.16.13.06.41; Thu, 16 Jun 2022 13:06:45 -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 5B0BB68B887; Thu, 16 Jun 2022 23:03: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 D822768B80F for ; Thu, 16 Jun 2022 23:03:40 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 10069240511 for ; Thu, 16 Jun 2022 22:03:39 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id IE3v6Z79i9I8 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 EDB48240555 for ; Thu, 16 Jun 2022 22:03:33 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 9D3533A141E; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:08 +0200 Message-Id: <20220616195534.5278-9-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 09/35] 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: 9VNeEeQL7Zcr 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 75c2322091..d8ab0ffb9c 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2860,12 +2860,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"); @@ -2874,8 +2877,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 ea226eb901..7bc5d2ce2e 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -705,5 +705,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 78395bebb4..19519052fe 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -382,3 +382,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 Thu Jun 16 19:55: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: 36270 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1128347pzb; Thu, 16 Jun 2022 13:05:45 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v7BMC8Qogf9GX0fSbxlONmyD0OnOi1Z/+bmWbhBgGsH66zGea9a163CSG0CwjrjLFl809g X-Received: by 2002:a05:6402:440f:b0:435:2e63:aca9 with SMTP id y15-20020a056402440f00b004352e63aca9mr6866893eda.162.1655409944472; Thu, 16 Jun 2022 13:05:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409944; cv=none; d=google.com; s=arc-20160816; b=bbYqPommwt747YVKfK1ZCx008LOj/mih7yR8xCBkWH8OsmcUJWdlyhFI+wgO+qIXII bYCbeZGl3WGXLKADxZbI6wLsve9haqnnicJhPVMUV47iv6dNx/om55j83vgyLVzbWpMX qu2ua7viG1ikgFaH0lwym1rUgiA9tGaqHkkciIuQLWjZrtL/hs6bJyjgpRvl3XrlfS26 hoODvQavsDKgs1egRMZogxwo1vqoLyRUNiTmlIvsmqN3XhYQQ46ikOOWOzvV367cJ65t KSqrYHGEYoZ9qYiwQVoAP80BfS+QLiNcq3vvTjmROR5snq9HJsMoncklLDSFrOew48uQ 8wqw== 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=1l5b4vRTYCFj0v700ojwZJT14Ez0eJRMZrzK00LZ3C8=; b=s4XwYQEYkHZGKxyJk2AX6xL8hQ+zEL6PE6VwcmHQl+yJmhrP8xCfksmr4A3dHF+kwf F/a7QcpTZqiP48cDqQhsrfwCHPks1hIz0YWxEeIo0PdkDcbo5JVrkmHSU6IxJYdjF+kw 6mU9AjOcCmixSGwF2AgzJ1UTgJGfGWEYYchHz1CMACnOlRe+ZcDxHdei3d7myohjfaQX xkUK+1chJKIG7G9xJQ/yYd7cKo0dtp8iD9tNQEGI065M80yLhSHMuZcN6rVwN8esfHKi sukAR6w0suMboBwP0+5gAI7jFSEYGrAhWziRqIRWZcveAyZRT/3h9X9rg8FHNPP53q+8 gMWA== 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 b13-20020a056402350d00b0042d8ee7ada0si3669200edd.607.2022.06.16.13.05.43; Thu, 16 Jun 2022 13:05:44 -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 AE14968B89B; Thu, 16 Jun 2022 23:03: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 15C0D68B7DD for ; Thu, 16 Jun 2022 23:03:40 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 10345240506 for ; Thu, 16 Jun 2022 22:03:37 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 6ptUqHxJZd-I for ; Thu, 16 Jun 2022 22:03:36 +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 B20F9240507 for ; Thu, 16 Jun 2022 22:03:33 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id A201B3A174A; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:09 +0200 Message-Id: <20220616195534.5278-10-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 10/35] 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: AFNOhj8EAJE+ All other logging goes to NULL context. --- fftools/ffmpeg.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index d8ab0ffb9c..8f5901137b 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2905,7 +2905,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; @@ -2918,8 +2917,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; @@ -2931,7 +2931,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); @@ -2994,7 +2993,7 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) if ( av_q2d(enc_ctx->time_base) < 0.001 && ost->vsync_method != VSYNC_PASSTHROUGH && (ost->vsync_method == VSYNC_CFR || ost->vsync_method == VSYNC_VSCFR || (ost->vsync_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" + av_log(NULL, 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 " "setting vsync/fps_mode to vfr\n"); } From patchwork Thu Jun 16 19:55: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: 36266 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1128106pzb; Thu, 16 Jun 2022 13:05:13 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sSqSztgpKbTYxs3a9nakFV9sBSpuOKiADcwBTVBdvHehpC5xQhi0ig7C/oeFouvzHYWaMm X-Received: by 2002:a05:6402:2708:b0:431:45d1:3aa0 with SMTP id y8-20020a056402270800b0043145d13aa0mr8703650edd.408.1655409913062; Thu, 16 Jun 2022 13:05:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409913; cv=none; d=google.com; s=arc-20160816; b=fDdbGNi3MOQJ9PdT3/ndD0YHgnGoRDfKTYotBx9cghxm0zKuoZdlriY1cTsAx17MUK V76+t1GmLS1cVn6ZWDnRB80Q9xoiDztfSJNtsf/i9AlCxDeTdaNsMyFcnbjZQmn9Afjp c2boNEvvUODdbFHtH33nwfZ+vwZqHHI4ea0TMwfB0rp7bYzIG/zihe3rvqONbvxXUZpv HITRdXtAknZo5XhdJV7K1443sfpSZRWPOYCDOab7NfS4uy2mNr2LZelM8NOuNmWqmRgN E4qIu83qhY+HkC1S6l9jnqJtUqrHexq/fUC0hY22yB2waf/ZmNGFPUwNFsgPB0E53ZgT ag4g== 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=dUwh/QrZ1XVNd14XHUQwNnxDwoeKk4x/zBTUnMoERzs=; b=F+an/HcE3fYCY6PwiPZI5irmbWxp/Aw3KNSF7F2mM1jQFjQ1daMU+8y4lzHcng2Fst jSuHj/kYGBAX/8pw0JxT305Zv8yr1W13Q3IjwEJkmuiUh5AkgkGvLyQUcp1cUf/U43Oq 2nnib47qF3912CbAxVB9bbsm8t2uAtQ1rR6LTn9QiuI41WthH16eMwJuqQMEetWBa5Rf 6jcxoiNmGaaJzq2kw10Sx7paXNj9lDRHLceTFGuPjW1qAu9Xd2rq1G434Dmd1nuuSH12 CAv1gt8YcQYI75Q/UA1zLoz9R7ty1R3Z9JUu+MLAo7wXzviJJJ4nP4Sxr5n0jTQ8TluT BCzA== 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 sb6-20020a1709076d8600b00702d8b7438csi2945689ejc.326.2022.06.16.13.05.06; Thu, 16 Jun 2022 13:05: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 0323F68B83A; Thu, 16 Jun 2022 23:03: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 F0A6668B808 for ; Thu, 16 Jun 2022 23:03:42 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 0F93A24017E for ; Thu, 16 Jun 2022 22:03:41 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id JENSuTPA9TxG for ; Thu, 16 Jun 2022 22:03:40 +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 3697924062A for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id A71633A17B2; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:10 +0200 Message-Id: <20220616195534.5278-11-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 11/35] 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: tP5Vi4dc826i 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 8f5901137b..492c8cbfd2 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -591,13 +591,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 7bc5d2ce2e..5996fd7858 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -563,15 +563,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 19519052fe..d49235ee63 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; } @@ -270,15 +284,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); } @@ -319,6 +334,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; @@ -328,25 +366,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")) @@ -354,12 +409,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 6f2bf33d7d..4d4939c6fd 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -1640,8 +1640,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); @@ -1665,13 +1663,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 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; } From patchwork Thu Jun 16 19:55: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: 36268 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1128219pzb; Thu, 16 Jun 2022 13:05:26 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v7jKGL+9wSTc28MzpmAZCt7QqPUaJEht5NaTfWqwxq7delOVOGbqiTh3l6AJQZvtajdbot X-Received: by 2002:a17:907:3c81:b0:6e6:cf3e:6e14 with SMTP id gl1-20020a1709073c8100b006e6cf3e6e14mr6031567ejc.181.1655409925970; Thu, 16 Jun 2022 13:05:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409925; cv=none; d=google.com; s=arc-20160816; b=VJchlXhI78lzjtJXU5IHWm1fMQZFrZm0sCDh4ASxpAZ9tMHtTCd6D1xOmpc7Vd9COq eVfbp74sfTDSxtUh1EzRcfHLO1i5/n2UGTVTyvkUptXUikEkSF7zddTlW8SSG2ZAs6pE cyScRkF3De8O3IFQjA3tdN6txIjo/793xl5yNc6aOLXuNUEm2sX8qcvIaVtrdoc3sf67 rkws6S2M1I/GosRK53hlg6vCDGmeoHiACVJsObSsJELxehqwanCGwUYXWIs0jOE1lbmp qTAZJiQacG3r5o5WwS1nOGNpwQItUAfSVkN9CIHUoLEq3JPgq6U6xa0aFiFU7jdYHnGg dVdA== 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=m0iX/dYaFAbdNE0LuZhgD1oGrobQC/cQ6Uz3xtmivjg=; b=AgAz7INZrXGB+IxktM+PfL5YiVk5scW4K6DdYOq44mAchxGPErgo2t1hvwq+0ONXIP 69O9AaQf1schIKhrtspx80K9ACduJ2nOr8wokAGlHpZgHQqvWu/9D2ug0XFYFrXqt/3q iuI+wU+xqUbA8NaeIV4cqM3ndptGypVUaIiuIdnHJa3quPvvu1FaFdF9LaIx//PvKo4D f86EaYk/E6WhcONVHtRdll6RbMddZhpZ9dGdGsRxwRc3STXgt8Gnono/duDjJCJnt3kt 5KWa06Absh06vqfbVr2Q9qSFaiiRIxVpBev65N/pWrUXaFp5+hhXkob5xV9znGdU7AIB 8uZg== 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 qb29-20020a1709077e9d00b007122faebbdasi2977803ejc.494.2022.06.16.13.05.25; Thu, 16 Jun 2022 13:05: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 C340468B85F; Thu, 16 Jun 2022 23:03: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 80AA268B81A for ; Thu, 16 Jun 2022 23:03:43 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 0F99424017C for ; Thu, 16 Jun 2022 22:03:43 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id YTl4lSWHQMlp for ; Thu, 16 Jun 2022 22:03: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 4A3ED240685 for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id B03C23A1ACC; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:12 +0200 Message-Id: <20220616195534.5278-13-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 13/35] 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: z8YoVowkLlwV 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 492c8cbfd2..7496ca1dc6 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -724,11 +724,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 5996fd7858..c7db12a640 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -692,8 +692,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 98637004aa..ed091a3d2d 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 && ost->vsync_method == VSYNC_DROP) || (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0)) pkt->pts = pkt->dts = AV_NOPTS_VALUE; @@ -212,6 +185,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]; @@ -310,7 +315,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 Thu Jun 16 19:55: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: 36271 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1128498pzb; Thu, 16 Jun 2022 13:06:02 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vyUpy0MzdAtjpkoPmLcLXMy/MtCEtyvNyM4ndPNi5Nlqx1ui77Yd24bA1Nn843nOHZl1Sr X-Received: by 2002:a17:906:3e96:b0:711:5a8:5081 with SMTP id a22-20020a1709063e9600b0071105a85081mr6192111ejj.703.1655409962629; Thu, 16 Jun 2022 13:06:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409962; cv=none; d=google.com; s=arc-20160816; b=pJA9x8Daew3JXDVCGbuxHzmC6yc6Mvru3H6IvUOI4DDE7G3qurLDC3eD5OKsEYMYqf DAZbWfHJCgGK34QnJBTojQ4LIsj0iNKly+cGNxFqq+HkUguMeQsHRHs33/iT9ZrVmpoJ 0TkKrvn9BpQzGP5Y77kBP15Xzh7hIcri5A50lRlgleV/8alIHP9E+atiIcqHGg+vOLEk uBsCa+DiQg8mU1pU1NXAiFYFPDMYCvSgj1HPO9hh1DqvNh1WSQnD+fcTmZo+nFW3pBrM sHLR99wmoj3kZohRZi4DE26fdff+wnjDO04Ar4RNE3JZfCPLuHIZXzJYAw2vY7HVzYpK l9cg== 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=UfhNF6D5raUctIWDXUWfGEZs2B3dpeG8Ca+s6985KKI=; b=m0QsXF2HGV9Sldg2qt5RD/Ambph0UQbj8/DV61p9NcNg50QQdna44H5rDtDa7ANkai 36/o3rZGAqTmLJe852r7PdTULfZoLWInmzszwHrottbSmd2B6PAZ3Sui5mq/gXzO9PDx 3kRrBs0+fZOsWRVCj3tX3m1OEoUbV7tK/7JLWsvJSgf4/P7nZ1z/HVvnTfzVOY4qGokz OH6kNV832tdzDaiD+r5F3Ybj696ro6zG//wDoznCtsiGsO76miEEH28WA746Wuc72ili m4CM8EZoSFQ948Fl/D/3+XT444aZGIm+EDp5q7RKiug2FhB4bJZ+x556UYrvhAreWBlk 2BuQ== 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 q20-20020a056402519400b00435262bde24si4048222edd.413.2022.06.16.13.05.53; Thu, 16 Jun 2022 13:06: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 CDC3368B89A; Thu, 16 Jun 2022 23:03: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 1E52F68B82F for ; Thu, 16 Jun 2022 23:03:40 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id B5CE6240507 for ; Thu, 16 Jun 2022 22:03:37 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id SsB_kjF1Jbax for ; Thu, 16 Jun 2022 22:03:37 +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 BE37A24050B for ; Thu, 16 Jun 2022 22:03:33 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id B508D3A1BF3; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:13 +0200 Message-Id: <20220616195534.5278-14-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 14/35] 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: eTU/9ZuvsmlV 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 c7db12a640..2aa220da29 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -582,7 +582,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 @@ -686,7 +685,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 ed091a3d2d..0fd7888d4f 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; @@ -281,7 +283,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 " @@ -374,6 +376,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); } @@ -393,12 +396,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; @@ -424,6 +426,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 4d4939c6fd..1bcb7a3006 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2332,7 +2332,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) { @@ -2357,7 +2357,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:"; @@ -2379,7 +2379,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); } @@ -2760,7 +2760,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); } @@ -2768,7 +2768,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); @@ -2962,7 +2962,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 Thu Jun 16 19:55: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: 36274 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1128696pzb; Thu, 16 Jun 2022 13:06:31 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t1qOmR/oK5cWgeFjI6iqSgxFXvwnFEIhjco1F02QP4EilxS744Ptats9Xv99Zj3BFVlK6J X-Received: by 2002:a17:907:6e25:b0:711:c6ce:b7bc with SMTP id sd37-20020a1709076e2500b00711c6ceb7bcmr6303879ejc.752.1655409990912; Thu, 16 Jun 2022 13:06:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409990; cv=none; d=google.com; s=arc-20160816; b=XF4kdDoNmKH/979qpYxLKt4O6KY3sTF8lDVqfmimvlnOLTeLg5QEbt/pl08wOBiXCU xKGDNXWd7Bg99mL8trvvR3iVvC7v+vijZU3D5RSojy78aPsBTi8bv8n6bLHaAMsbQmgv yOB1/zC/eossMYumXSXx7u+QrOB6knN9FaibyJpocwQBpoua8Sr1XXA1FZ+TJkgcDiYi E6ucU6n+yGmAeLE/24HsJ6mFziB8FSODTQpx7e7wwOYAKhIGGY4yLnoECMRHw8b7efq6 W4sV3+9u++lOzrcv8TGYQi94SH6T+Sd45H4uCmNGc8UC0m9lCG+DDqMS8kqGmBkbNg6G 3iOQ== 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=iaNQSneQyXCNjkq3EWdEQcpdGo4WvvlhMIInUhJTWOw=; b=wgmIB26KwDKjKaQqE6NvC/KDWvJXcUu1HO/N9kvixvUrd+lNEyLopPJsQk4pYCGj59 DrmgGOl4vssVT12neVvAobnerrmGnwIcvfnvgikVK/AIjICotCH/XYVdJ2tdOqelUi2D Cg5E8mZyWYwDZ2VXeS4LxhKjteQuv8Es5zAdUUDhfrPrprsWS3IZXprEAqehbixt+u5U fzS8dqKmcybKZeaX0G1s+pHW7X7R9UMkLdua6bn15iZ7a4M5IMaXxsgSBvmLGVSzDXIl EZ1ICH+pvwn8n07RDwEJdkTmKJj66vraaJNpy335iYkp/AKgw1UPZzCyWTbaM0sJNyBg tOjQ== 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 go33-20020a1709070da100b007101a8a5b89si2878039ejc.943.2022.06.16.13.06.22; Thu, 16 Jun 2022 13:06:30 -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 A892768B865; Thu, 16 Jun 2022 23:03: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 4902668B817 for ; Thu, 16 Jun 2022 23:03:40 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 43411240175 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 m9qHU5MHOArM for ; Thu, 16 Jun 2022 22:03:37 +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 D7F1E24052D for ; Thu, 16 Jun 2022 22:03:33 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id B9A933A1EE6; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:14 +0200 Message-Id: <20220616195534.5278-15-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 15/35] fftools/ffmpeg: move freeing 2pass input stats to a better 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: PcZvX6ULof4c The current placement of this free is historical - it used to be followed by avcodec_close(), since removed. The proper place for freeing the stats is currently right before the encoder context itself is freed. --- fftools/ffmpeg.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 7496ca1dc6..f60013eace 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -588,6 +588,8 @@ static void ffmpeg_cleanup(int ret) av_dict_free(&ost->sws_dict); av_dict_free(&ost->swr_opts); + if (ost->enc_ctx) + av_freep(&ost->enc_ctx->stats_in); avcodec_free_context(&ost->enc_ctx); avcodec_parameters_free(&ost->ref_par); @@ -4351,9 +4353,6 @@ static int transcode(void) /* close each encoder */ for (i = 0; i < nb_output_streams; i++) { ost = output_streams[i]; - if (ost->encoding_needed) { - av_freep(&ost->enc_ctx->stats_in); - } total_packets_written += ost->packets_written; if (!ost->packets_written && (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM)) { av_log(NULL, AV_LOG_FATAL, "Empty output on stream %d.\n", i); From patchwork Thu Jun 16 19:55: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: 36283 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1129367pzb; Thu, 16 Jun 2022 13:07:49 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vfvRCdczhqcVbWiItiMYVtDXwfOrHmBFrb8hPIy7ywwghLaLDb5MmlGaWhEygN+X1FWGNe X-Received: by 2002:a05:6402:354c:b0:431:51e2:eef9 with SMTP id f12-20020a056402354c00b0043151e2eef9mr8770430edd.344.1655410068989; Thu, 16 Jun 2022 13:07:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410068; cv=none; d=google.com; s=arc-20160816; b=Irpggkzz/hc1MCydiW9yNUBMn4Emv9JyIMmTGhMEinLyUu6VT2mN9jkOdz7iVkpKo2 7wn+g37Rs2VLX92+07NAE/DaZ4OGgLqmv0CIIrSEFN2nLdnIweT/vIhL01p6iSNw7KiC 8LFQr+1/QcWFvpCoGag3uJjd4uzhs7hoexD7+76uKugLsKFZWCEELDnc4iA+TnyzZ2JG LJUPcGFtZOs1Y7Xw193IJy++23McbGYo1CR/EMf4Yc5fGgnwPKpNkJIqb0rw6YnPqifG layRQwVBd84tASiP8pJfMCZv+whW6SDexp7tZsYMGq28Zdj08uwnbeL4EQlPgbl7zwjk XIUA== 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=heV27fZEAOCMlcJmhxB2ZmUlYT+lenOyrYEI9GDKf1U=; b=sLSG7JUpUEtW34TajgiutfstLgI/fiIvr9FYXK2cFfKSGye3iw1chV6tWvZnMpRZ67 L997rgglMvS5+AHmDB/IB/H5VAjHfOzSQXwwGkmItshC8xTuA+nKX/LaDNCMeipswVAx b5wICQeoh7XztuQkBY7gl/7efQFeR+0KXP44nQ+Xb1EvAnQN5kRKAL+0jRgx53/H//X9 nK8IBCcQa3fiMeiBn6ADGzSRsPwWgT3hSp2xznx/pu9EC6IsKcyAaVabEoI9ktPyajBN ixEUtgFdSAg9bWKLeXLH1LQZQb43wREjGqSNLAdLvfZoZOsRBh6dBFyxM1SYVF2nqwfI gM/A== 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 sg6-20020a170907a40600b00711fca6bc59si2979871ejc.263.2022.06.16.13.07.48; Thu, 16 Jun 2022 13:07: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 9B55D68B85C; Thu, 16 Jun 2022 23:04:06 +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 6F5D568B878 for ; Thu, 16 Jun 2022 23:03:48 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 08C8A240175 for ; Thu, 16 Jun 2022 22:03:48 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 0hv-wgcWUSkm for ; Thu, 16 Jun 2022 22:03: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 4E63F240687 for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id BE16E3A1EEB; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:15 +0200 Message-Id: <20220616195534.5278-16-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 16/35] 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: sxwzPP0Z+x5G --- fftools/ffmpeg.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index f60013eace..b8a11818b8 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -140,8 +140,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; @@ -558,8 +556,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]); @@ -989,7 +985,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; @@ -1003,14 +999,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 ? */ @@ -1030,6 +1018,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); @@ -1040,8 +1034,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) { @@ -1049,8 +1042,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 Thu Jun 16 19:55: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: 36278 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1128998pzb; Thu, 16 Jun 2022 13:07:01 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tuSiu+VoJKZ7PaMdZ8fdH8YqLzxM7PWGnkfCQtDXi3j3wT9eAV6wss6+xPnv0MNocBDq8j X-Received: by 2002:a17:907:6e9f:b0:711:d2cb:63d8 with SMTP id sh31-20020a1709076e9f00b00711d2cb63d8mr5948776ejc.232.1655410021778; Thu, 16 Jun 2022 13:07:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410021; cv=none; d=google.com; s=arc-20160816; b=wjzO9nfduZ+HmzEayFRfYgX9z4FHipzxJYaDZCqUl6esaAYKXeqxPOq9OsyUEILFIz ybE06VJoZ7DSS8FkJnQc+U2ehJTgMyv/Pfy6qhLEdpNq3kef6pGOJTXsCMg0UnxZfbRC Pre32l1VEDdeVFHZf/2nvU7CoyT5vq4MSDEYxp5NOaXzgC0s23WfgHAwXPvMP5oFw8BH QPZWpBE9Lpcz/1nao7m8XobF8JrxONrXTlUKAIjbUwAFFoeHH2CgRgPG+1Yhe/Yhlkqa 7CDPSFp98iHPBnw8/7WLaErGa2U+kosq/3Y17eGP6fdm5dwZqY1dzAp9Kr1rTTMjMsg3 PhEw== 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=BrTb6N4W1rVXGzYKgWS6NgEl3oqF92Zl4iskb7lp1u0=; b=ZKFuDMGBCyUVfdjnHIwsMkLodCgohjT8UAmWAEv+dylu2KkfZemir5RJHVhGcxOl8m 6wseeLROrDO76GUAn14Y/EEtQvY0DFn3NOHlBgt8EQSL+AjvoNdKWy9E3OAJVCY9sNdz hVFVE2sxG0HbkmxIoGQQ0PduyYuhO86Ik02eZY4BFo4901nTptj84PQOam8/1GH4LVOS 2582Zt/UmHgUbgZNtXzFKdv5Hzvf1X3I54xJAUoz/Bcqahzk4nyxnESufj+Z9gpEoK0V /DcLoGOmoQLY487GwVpu16ditua0R83ydeMim9/0zyuQOjgkWuoPrbXW8w1pYrxKz8R2 Q6Xw== 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-20020a056402510a00b0042bcbb48fe4si3966163edd.240.2022.06.16.13.07.01; Thu, 16 Jun 2022 13:07:01 -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 6066468B8C0; Thu, 16 Jun 2022 23:04: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 8FC4B68B84F for ; Thu, 16 Jun 2022 23:03:41 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 6EC91240555 for ; Thu, 16 Jun 2022 22:03:39 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id POXSqQeAW9x8 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 D7EC9240512 for ; Thu, 16 Jun 2022 22:03:33 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id C2A583A20D5; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:16 +0200 Message-Id: <20220616195534.5278-17-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 17/35] fftools/ffmpeg: do not send spurious EOF for streamcopy when looping 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: LCIY9oqHLuu7 --- fftools/ffmpeg.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index b8a11818b8..53ca8c7f7b 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2516,7 +2516,8 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eo for (i = 0; i < nb_output_streams; i++) { OutputStream *ost = output_streams[i]; - if (!check_output_constraints(ist, ost) || ost->encoding_needed) + if (!check_output_constraints(ist, ost) || ost->encoding_needed || + (!pkt && no_eof)) continue; do_streamcopy(ist, ost, pkt); From patchwork Thu Jun 16 19:55: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: 36317 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a25:bb42:0:0:0:0:0 with SMTP id b2csp1520649ybk; Thu, 16 Jun 2022 13:08:50 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vHcLOdnLNnQKUBMdvCr29IersvNy5GKSk/0NGXFlZCCl4hlfsjQG5FCMXPM8zSyGL3tTc6 X-Received: by 2002:a17:906:64cb:b0:711:f686:a843 with SMTP id p11-20020a17090664cb00b00711f686a843mr6185898ejn.333.1655410130749; Thu, 16 Jun 2022 13:08:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410130; cv=none; d=google.com; s=arc-20160816; b=N+ajQMmGNuxs8Wgw0qBlr3O7luOqgU5TbPKhEfJlWJrVY+ctiLa1fBGpOs7/Ub3hFw 8LOa6Ljq4T8pUMDdZz5zZq0PnhJK8gn3Dka8of1bD+IyMdXn4k2MBybEXno/SWpz53j0 Jfgha/ppnbt2cTvoT7Aba/WH7hVooqNyAPNKV5kACAlOFA9ULlHtHkfrg1rRLd35dsCV woXFHKUx8+CUp+igeoovbt1BjeVmBNHgnOZAHdMQkNOW+/JPTFSVqZ4E6DSmMgRJr136 FsdWRa0JRx8tB9v9qhiJhlacAd6AlLuYNjFwwNbst01gOrXpc+KW36XSBUvV8yqWY8xS KcTg== 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=GbaGFc8DtJa8h2VfVCLW/BSGZ6eM8byyJ6rRj+/zF88=; b=lQZHY8fWhHoxVc47iSQW8AKQaU9b7yUEDDyk96mzNSeRRloKMviUCSuQNsLcNqBNot OVmEyJV+00rgq9jYfMVeLdxoODeI2o3JMffoCFUkMfLL0YPMkcSz4j4y+0hrXmJTY+lp ydwu0jteep0be4ssYHkghYWHBfhPYVlb0+V/CU6NrgV6zgkMYU4BHp1forCupHiEDhWn eHdu7rWa9S5W3vwu/KngBqckHr4/LBh/G9bizt6oMWjyXNfoKneLP+jF20oTQ3QYgcvv Uh/UUO/x/EF/IJ3L3DaCKytmBBzVuC6ZlqxhrdJjvCfxSahoOwrmnK6XwTyuRZsBB87J 8qbA== 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 a27-20020a50c31b000000b0042df4ea0c4asi3711434edb.560.2022.06.16.13.08.50; Thu, 16 Jun 2022 13:08: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 7473768B8FB; Thu, 16 Jun 2022 23:04:11 +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 9A50668B88F for ; Thu, 16 Jun 2022 23:03:50 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 3E2692400F5 for ; Thu, 16 Jun 2022 22:03:50 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id HJ1iILgkcICT for ; Thu, 16 Jun 2022 22:03: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 5DF8824068D for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id C72193A20D6; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:17 +0200 Message-Id: <20220616195534.5278-18-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 18/35] fate/ffmpeg: add a test for interleaving video+subs 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: A5bOBiZaav2b Content-Length: 2474 --- tests/fate/ffmpeg.mak | 8 ++++++++ tests/ref/fate/shortest-sub | 4 ++++ 2 files changed, 12 insertions(+) create mode 100644 tests/ref/fate/shortest-sub diff --git a/tests/fate/ffmpeg.mak b/tests/fate/ffmpeg.mak index 94f50423be..154af2fac8 100644 --- a/tests/fate/ffmpeg.mak +++ b/tests/fate/ffmpeg.mak @@ -99,6 +99,14 @@ FATE_SAMPLES_FFMPEG-$(call FILTERDEMDEC, AMIX ARESAMPLE SINE, RAWVIDEO, \ fate-shortest: tests/data/vsynth_lena.yuv fate-shortest: CMD = framecrc -auto_conversion_filters -f lavfi -i "sine=3000:d=10" -f lavfi -i "sine=1000:d=1" -sws_flags +accurate_rnd+bitexact -fflags +bitexact -flags +bitexact -idct simple -f rawvideo -s 352x288 -pix_fmt yuv420p -i $(TARGET_PATH)/tests/data/vsynth_lena.yuv -filter_complex "[0:a:0][1:a:0]amix=inputs=2[audio]" -map 2:v:0 -map "[audio]" -sws_flags +accurate_rnd+bitexact -fflags +bitexact -flags +bitexact -idct simple -dct fastint -qscale 10 -threads 1 -c:v mpeg4 -c:a ac3_fixed -shortest +# test interleaving video with a sparse subtitle stream +FATE_SAMPLES_FFMPEG-$(call ALLYES, COLOR_FILTER, VOBSUB_DEMUXER, MATROSKA_DEMUXER,, \ + RAWVIDEO_ENCODER, MATROSKA_MUXER, FRAMECRC_MUXER) += fate-shortest-sub +fate-shortest-sub: CMD = enc_dec \ + vobsub $(TARGET_SAMPLES)/sub/vobsub.idx matroska \ + "-filter_complex 'color=s=1x1:rate=1:duration=400' -pix_fmt rgb24 -allow_raw_vfw 1 -c:s copy -c:v rawvideo" \ + framecrc "-map 0 -c copy -shortest" + # Basic test for fix_sub_duration, which calculates duration based on the # following subtitle's pts. FATE_SAMPLES_FFMPEG-$(call FILTERDEMDECENCMUX, MOVIE, MPEGVIDEO, \ diff --git a/tests/ref/fate/shortest-sub b/tests/ref/fate/shortest-sub new file mode 100644 index 0000000000..3b28898177 --- /dev/null +++ b/tests/ref/fate/shortest-sub @@ -0,0 +1,4 @@ +52bc3d6a0c80e639095a2c28da4ef42c *tests/data/fate/shortest-sub.matroska +139246 tests/data/fate/shortest-sub.matroska +d71f5d359ef788ea689415bc1e4a90df *tests/data/fate/shortest-sub.out.framecrc +stddev:11541.12 PSNR: 15.08 MAXDIFF:22854 bytes: 2591/ 26055 From patchwork Thu Jun 16 19:55: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: 36280 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1129167pzb; Thu, 16 Jun 2022 13:07:21 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ulXVqaFFUeD1hvRSTz+wEstCTbDQ0k2FwWmhlnXD3ABtWPXoul0g0KqXj5fwEsWhR6gzFK X-Received: by 2002:a05:6402:1f0e:b0:42d:e38a:51f7 with SMTP id b14-20020a0564021f0e00b0042de38a51f7mr8874510edb.68.1655410041091; Thu, 16 Jun 2022 13:07:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410041; cv=none; d=google.com; s=arc-20160816; b=uMR/77DIrw0BcGEHD5e/bVhbitBZOeI4QgbRzsGy3JRfIZ0/S2/MzVfugG3Fzb4wgi 6TtoRJ6edPes27Y/oYynz4cB7QDDNdGTQ+d3XLL/CgPpTIP3iNn7wClyD9izQ1WDx0T+ 8XLe47erm5G9JRGb2j+ki8C6YOXZQgA3QjauZ2qOZmmDnQieLOAgIKorEQIpjnCzH47/ lUVXwO9bWpuhm3pECVUbc7OAOwyJUVLEynl2qjqq6Bzk7L7k3Vnlis9x3GOLHRlAvzP4 sBx9gHgu7CpdcWJkLkRdHDLQBN6B3gpKDWB9O9fPp3UqnIxDZcfzuD3aIgAGgEzgttU1 UdhA== 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=oLqkT1R2knfp0jk/joFk94xUZugL4RTSVwrSYNvQx9k=; b=sne9RQZKmYLtgNbOFe+R7ch2E6NlWDnQebevIMwZHcq5wAcg9dPW20InghFpPEK3zU sCIvm9ep9Ec57XwYW1rN4T4seneACa5YzkJ/cv18KQho7scmYPCDBYFty4LxEmZTI263 dL78NjDqiufBF5KyKrPiCAuka/i4Kd7iALJupEsU+k1tsqPio6M9QmWbWAZnEBa11+u1 Sb/oFBvmu5v7XkcrJRlhClhy8iqqLEp7nesBhfq2MwaVi9LtmUoZZSVV/zts6CqoTPzu 21ImjBO8v+5cbNffbfTA8prARidLXpzW/1bE97PCvgU0PH8Qf7q71mFldqeyuO8tKNsx zjww== 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 a2-20020a509b42000000b004351fbc9afcsi2277346edj.466.2022.06.16.13.07.20; Thu, 16 Jun 2022 13:07: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 5294668B860; Thu, 16 Jun 2022 23:04: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 C382E68B850 for ; Thu, 16 Jun 2022 23:03:46 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 5D98D240175 for ; Thu, 16 Jun 2022 22:03:46 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id rpE4bhzM-KuK for ; Thu, 16 Jun 2022 22:03: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 5011F240688 for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id CBC0F3A20E7; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:18 +0200 Message-Id: <20220616195534.5278-19-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 19/35] fftools/ffmpeg: use last filter output pts to choose next 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: Lb1Kz5TOZqlK This will be needed in the following commit that will add a new buffering stages after encoding and bitstream filtering. --- fftools/ffmpeg.c | 23 ++++++++++++++++++----- fftools/ffmpeg.h | 2 ++ fftools/ffmpeg_opt.c | 1 + 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 53ca8c7f7b..4647555ebf 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1343,6 +1343,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) @@ -3440,13 +3446,20 @@ 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 : + 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 : 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); + 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; diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 2aa220da29..861f8140cf 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -468,6 +468,8 @@ typedef struct OutputStream { int64_t first_pts; /* dts of the last packet sent to the muxer */ 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; diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 1bcb7a3006..09a281dba3 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -1662,6 +1662,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; } From patchwork Thu Jun 16 19:55: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: 36263 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1127781pzb; Thu, 16 Jun 2022 13:04:38 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uq5XLuKWnJxJlodweBqxvHfXSA+tjOpFPdBAzCmGKc2IkoZUebCgV30sTboXuuZddnZXUL X-Received: by 2002:a17:907:60d3:b0:711:cef9:6a85 with SMTP id hv19-20020a17090760d300b00711cef96a85mr5957058ejc.99.1655409878422; Thu, 16 Jun 2022 13:04:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409878; cv=none; d=google.com; s=arc-20160816; b=eSAU4s+SOZ5z5XbTQClKBpsf1MVwF7LGcr7EHqMpTgQgKErhj45mkBw+pP7MabO3Ig SXOxfulkPfXLrkkwQzEDZyk/NH7VCTb9iG9qH2i+zDHg4lnzf+RM9rSJCxC29dtv/aoE lIw+efjHYyikgDaHlQh3RbyizV9uyGkhoJywpNPBWAf+62SwNZ19rgQHEMrU4nqX1cJd X4sWUl5psXm7RzaXxpqQ0NzzDRjQX5Ybw54Pz2XBuxBZFWVJScxUwhwhdqyNRZwHeuF1 S51EE6S0Bdq0XkAj6zlS+hpEFC7f4J7z0ZzIfMg3og1sEtlKmrWADNPzNy97F6J8B9jN V/ow== 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=pIeMwDA8K2iN7q7IdHF6kCytaZUTt8IJhS67g1D8UOU=; b=FSxjM7E+jEzKleL6LTNVsP/+gXd8lA6afv7l2HaHqT+okUPx2eovff2ETadvgDJy6J lXi61FOsN9n4eZQLHzVPFQ5OY+FbSDVLJ2jeVYpU4FN48AGMtZJtWxVaetaUtD4nmKh7 tPRqnzqDiFdO9JVC7RSN+Fd1tXLPPMUp+3x5NrdlQrNdT9TxKq/9DmQuQNappIL9WZn2 q1p70TpkLWsVByne95sJkqzNfhgWr4Yve5zEME+OI5mfeqbIhV87Y9CaTkEDh+kt5N3u peNtgG45pfujzFq85HtErueQULriv7ZmoftWa1bIxs12kr+ZOvDQ0d9X8wuozK1RWn6S 2yFg== 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 dz10-20020a0564021d4a00b0042dd005675esi3805586edb.410.2022.06.16.13.04.38; Thu, 16 Jun 2022 13:04: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 F075568B832; Thu, 16 Jun 2022 23:03: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 7100968B79F for ; Thu, 16 Jun 2022 23:03:42 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 2C85724055A for ; Thu, 16 Jun 2022 22:03:40 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id gkXGCKWDKwcg for ; Thu, 16 Jun 2022 22:03:39 +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 1D82E24056A for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id D068E3A20E9; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:19 +0200 Message-Id: <20220616195534.5278-20-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 20/35] fftools/ffmpeg: use pre-BSF DTS for choosing next 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: WNmjBGQeyDiU The following commits will add a new buffering stage after bitstream filters, which should not be taken into account for choosing next output. OutputStream.last_mux_dts is also used by the muxing code to make up missing DTS values - that field is now moved to the muxer-private MuxStream object. --- fftools/ffmpeg.c | 8 +++++--- fftools/ffmpeg.h | 2 +- fftools/ffmpeg_mux.c | 20 +++++++++++++------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 4647555ebf..6cd471d5cd 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -716,6 +716,9 @@ 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); @@ -3452,9 +3455,8 @@ static OutputStream *choose_output(void) 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 : - av_rescale_q(ost->last_mux_dts, ost->st->time_base, - AV_TIME_BASE_Q); + 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", diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 861f8140cf..5403f9998b 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -466,7 +466,7 @@ 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; diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 0fd7888d4f..a3350a73e9 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -41,6 +41,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 { @@ -106,6 +110,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; @@ -133,21 +138,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); @@ -161,7 +166,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++; @@ -423,6 +428,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; From patchwork Thu Jun 16 19:55: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: 36277 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1128945pzb; Thu, 16 Jun 2022 13:06:56 -0700 (PDT) X-Google-Smtp-Source: AGRyM1usUvOAmgk1QAGF0a9m37hdBx1mqNFIcy/5tTd7MVSe42Ackn0+JyJjyp41Mz1gJ5K19U+V X-Received: by 2002:a17:907:a407:b0:704:34ac:835c with SMTP id sg7-20020a170907a40700b0070434ac835cmr6016645ejc.663.1655410016312; Thu, 16 Jun 2022 13:06:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410016; cv=none; d=google.com; s=arc-20160816; b=uXrTXg6JNzo49Lg291msYyCZDw0YUQGVC7etGeM7dh+o4iX1nNGt+BpXbE0QdwY58o T85gMajGczTa2ILD+E1lI0TC9JpfCXLxRX/JbbZN+GmAX1xhbFbddsYERDVZkiJuO8PF 4Lqghk7t5KB3tGIqCg5ThyovSd4Panh5KjEJ1ja1zLdec7WngQjnFq2CkXJ56WwOW2+g hrhUzG3oHFB7kkIhJjuCARhI1JMbnrEumnj8mHovbrTnu+JViS3Ghz2S14kERZ9/M/Vw S1q3pU+vkIaZrrrsxVcmRbGVgDZDRQfV4b7cuTqxdolaxVTqsKOtS8ijstbSC6ZjT9Jo YeZQ== 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=DuDVfFGzjpslbhkidUnzaENRH/jlySZz4M1KQ0gdWus=; b=erXiVISafwKj0jHP+EZwYhTIjpnrJHAbtWnohACHNjohmiKB+UdKKA363cnn9onlsQ hpjOT8zjlJqnXB73Wexo0ZtDMw2guTXUL//eI+VfvRr1dy6kT0sE8JjC5aPudFJzG+z3 yCSfgTzTbkB5+cptCp3M3gOI6cYkcDvO46Q1bGxPouOhOKejYtKxOPUil7Z6b4l4EZNc hwiYvW7TQRrvC+/mJrk/zY1obQNv7wmbKo/XuXwkMpPuHrLo9UejhOv9nWDt5uw8JL1n C8xYWcwPlDzN1Z1DQ8hjHWwc5xz7onr6V01CQ4f0FeuAywu3hs7vnowb6CndFMMYHy+/ 0N4Q== 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 v30-20020a17090651de00b00715790f7183si469472ejk.301.2022.06.16.13.06.51; Thu, 16 Jun 2022 13:06: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 7419A68B820; Thu, 16 Jun 2022 23:04: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 EC47068B84C for ; Thu, 16 Jun 2022 23:03:45 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id AA061240175 for ; Thu, 16 Jun 2022 22:03:45 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 483E_z3lNVSe for ; Thu, 16 Jun 2022 22:03: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 7D49F240692 for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id D4A3A3A20EF; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:20 +0200 Message-Id: <20220616195534.5278-21-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 21/35] fftools: add an object pool 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: Pki1O2/Z71zB Allows to avoid constantly allocating and freeing objects like AVFrame or AVPacket. --- fftools/objpool.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++ fftools/objpool.h | 37 +++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 fftools/objpool.c create mode 100644 fftools/objpool.h diff --git a/fftools/objpool.c b/fftools/objpool.c new file mode 100644 index 0000000000..b1561ecd69 --- /dev/null +++ b/fftools/objpool.c @@ -0,0 +1,131 @@ +/* + * 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 "libavcodec/packet.h" + +#include "libavutil/common.h" +#include "libavutil/error.h" +#include "libavutil/frame.h" +#include "libavutil/mem.h" + +#include "objpool.h" + +struct ObjPool { + void *pool[32]; + unsigned int pool_count; + + ObjPoolCBAlloc alloc; + ObjPoolCBReset reset; + ObjPoolCBFree free; +}; + +ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset, + ObjPoolCBFree cb_free) +{ + ObjPool *op = av_mallocz(sizeof(*op)); + + if (!op) + return NULL; + + op->alloc = cb_alloc; + op->reset = cb_reset; + op->free = cb_free; + + return op; +} + +void objpool_free(ObjPool **pop) +{ + ObjPool *op = *pop; + + if (!op) + return; + + for (unsigned int i = 0; i < op->pool_count; i++) + op->free(&op->pool[i]); + + av_freep(pop); +} + +int objpool_get(ObjPool *op, void **obj) +{ + if (op->pool_count) { + *obj = op->pool[--op->pool_count]; + op->pool[op->pool_count] = NULL; + } else + *obj = op->alloc(); + + return *obj ? 0 : AVERROR(ENOMEM); +} + +void objpool_release(ObjPool *op, void **obj) +{ + if (!*obj) + return; + + op->reset(*obj); + + if (op->pool_count < FF_ARRAY_ELEMS(op->pool)) + op->pool[op->pool_count++] = *obj; + else + op->free(obj); + + *obj = NULL; +} + +static void *alloc_packet(void) +{ + return av_packet_alloc(); +} +static void *alloc_frame(void) +{ + return av_frame_alloc(); +} + +static void reset_packet(void *obj) +{ + return av_packet_unref(obj); +} +static void reset_frame(void *obj) +{ + return av_frame_unref(obj); +} + +static void free_packet(void **obj) +{ + AVPacket *pkt = *obj; + av_packet_free(&pkt); + *obj = NULL; +} +static void free_frame(void **obj) +{ + AVFrame *frame = *obj; + av_frame_free(&frame); + *obj = NULL; +} + +ObjPool *objpool_alloc_packets(void) +{ + return objpool_alloc(alloc_packet, reset_packet, free_packet); +} +ObjPool *objpool_alloc_frames(void) +{ + return objpool_alloc(alloc_frame, reset_frame, free_frame); +} diff --git a/fftools/objpool.h b/fftools/objpool.h new file mode 100644 index 0000000000..1b2aea6aca --- /dev/null +++ b/fftools/objpool.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_OBJPOOL_H +#define FFTOOLS_OBJPOOL_H + +typedef struct ObjPool ObjPool; + +typedef void* (*ObjPoolCBAlloc)(void); +typedef void (*ObjPoolCBReset)(void *); +typedef void (*ObjPoolCBFree)(void **); + +void objpool_free(ObjPool **op); +ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset, + ObjPoolCBFree cb_free); +ObjPool *objpool_alloc_packets(void); +ObjPool *objpool_alloc_frames(void); + +int objpool_get(ObjPool *op, void **obj); +void objpool_release(ObjPool *op, void **obj); + +#endif // FFTOOLS_OBJPOOL_H From patchwork Thu Jun 16 19:55:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 36286 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1129599pzb; Thu, 16 Jun 2022 13:08:20 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uWIbMi/hTa8Ij3uBe6EmTC6qxLN5/B3jizyIu1JXD6U+q0yShPcWDuF5Vy04diFHWebVm5 X-Received: by 2002:a17:907:2d2a:b0:710:7ea6:2b9e with SMTP id gs42-20020a1709072d2a00b007107ea62b9emr5841067ejc.371.1655410100247; Thu, 16 Jun 2022 13:08:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410100; cv=none; d=google.com; s=arc-20160816; b=p9SVw993hyL9o94DEQrLLZIQWsNffw1MwYQf0S7ybZbFNJ19NUtYgZu9H6IJ7uGXZH h8koSvljjNPLWR13Z37+aAhpCJGI75taeaHb8Q5s1ZdNeNbPav54hw3p727esApx1kM2 Ilecb8hf6r18A4HhdY4ZuU/bUWp3Mkq0VJt92VRF0lARHpi6WYlUfRcLE1r7YAQ3nRoe nj4QE9sv4s14Ea044xR4sfGZ/MmIgGVrzkTHX688SWQHNnuhRjMKM8NwElciYaS7GJHq bKkB2+jPG3rAuRvs7nvLOWyH6yqL5KeqMe4ai48MtK4C0oU3r82rhwUu0O0xsjHQl6lC i05g== 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=tuz7OtyjQNVrd1p+X2lFk/Ulq+TOS3pZjuS0Ffg8604=; b=jRf1X4IZe+x5yufmqkzsj7EGVH5RRTXGk/MiFQdIH2nioHOp9gRnzSQVPJSazzD+cC 9nvHGtUVDuX1xGYtmNTRLp54kCcamam0b6rYbQPrvAeGdyPqLVkfiXP77XDhgoBFYoAd B898hauGxfZRZxML/0CbCjK4TX35neFnqpQ9HL4/a3GjJ/q81+r9ZWE3knZsi3dJAUpd 7n2xtMW4m3qsZN21hoJX+6lK8kvuSgLnHS2WG5JwldfJ2riW89uUUs04l+8UEPUo5e7O 6zNM9A1ED/RFO/VUbA76uM16Nzzd6wn/9efkgCQY47k2/z5qjjELiO01fu4mOqsumNTQ 3byw== 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 i20-20020a05640242d400b004355043e3fbsi609579edc.440.2022.06.16.13.08.19; Thu, 16 Jun 2022 13:08:20 -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 B44F368B8E5; Thu, 16 Jun 2022 23:04:08 +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 8BAD268B887 for ; Thu, 16 Jun 2022 23:03:44 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id E6AB12400F5 for ; Thu, 16 Jun 2022 22:03:43 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id U0dkHm6JpeBc for ; Thu, 16 Jun 2022 22:03: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 2892B240590 for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id DAD283A20F1; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:21 +0200 Message-Id: <20220616195534.5278-22-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 22/35] 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: wwjtsLxqT/db 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 cause its stream to 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 specified. A new option – -shortest_buf_duration – controls the maximum number of queued packets, to avoid runaway memory usage. This commit changes the results of the following tests: - copy-shortest[12]: the last audio frame is now gone. This is correct, since it actually outlasts the last video frame. - shortest-sub: the video packets following the last subtitle packet are now gone. This is also correct. --- doc/ffmpeg.texi | 16 ++ fftools/Makefile | 2 + fftools/ffmpeg.c | 104 ++++++--- fftools/ffmpeg.h | 9 + fftools/ffmpeg_mux.c | 67 +++++- fftools/ffmpeg_opt.c | 82 +++++++ fftools/sync_queue.c | 427 ++++++++++++++++++++++++++++++++++ fftools/sync_queue.h | 100 ++++++++ tests/fate/ffmpeg.mak | 2 +- tests/ref/fate/copy-shortest1 | 1 - tests/ref/fate/copy-shortest2 | 1 - tests/ref/fate/shortest-sub | 4 +- 12 files changed, 775 insertions(+), 40 deletions(-) create mode 100644 fftools/sync_queue.c create mode 100644 fftools/sync_queue.h diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi index d943f4d6f5..7542832eb3 100644 --- a/doc/ffmpeg.texi +++ b/doc/ffmpeg.texi @@ -1750,6 +1750,22 @@ Default value is 0. Enable bitexact mode for (de)muxer and (de/en)coder @item -shortest (@emph{output}) Finish encoding when the shortest input stream ends. + +Note that this option may require buffering frames, which introduces extra +latency. The maximum amount of this latency may be controlled with the +@code{-shortest_buf_duration} option. + +@item -shortest_buf_duration @var{duration} (@emph{output}) +The @code{-shortest} option may require buffering potentially large amounts +of data when at least one of the streams is "sparse" (i.e. has large gaps +between frames – this is typically the case for subtitles). + +This option controls the maximum duration of buffered frames in seconds. +Larger values may allow the @code{-shortest} option to produce more accurate +results, but increase memory use and latency. + +The default value is 10 seconds. + @item -dts_delta_threshold Timestamp discontinuity delta threshold. @item -dts_error_threshold @var{seconds} diff --git a/fftools/Makefile b/fftools/Makefile index 81ad6c4f4f..bc57ebe748 100644 --- a/fftools/Makefile +++ b/fftools/Makefile @@ -14,6 +14,8 @@ OBJS-ffmpeg += \ fftools/ffmpeg_hw.o \ fftools/ffmpeg_mux.o \ fftools/ffmpeg_opt.o \ + fftools/objpool.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 6cd471d5cd..1a14637ece 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" @@ -569,6 +570,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); @@ -691,13 +693,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)); } /* @@ -726,10 +725,15 @@ static void output_packet(OutputFile *of, AVPacket *pkt, goto finish; while ((ret = av_bsf_receive_packet(ost->bsf_ctx, pkt)) >= 0) of_submit_packet(of, pkt, ost); + if (ret == AVERROR_EOF) + of_submit_packet(of, NULL, ost); if (ret == AVERROR(EAGAIN)) ret = 0; - } else if (!eof) - of_submit_packet(of, pkt, ost); + } else + of_submit_packet(of, eof ? NULL : pkt, ost); + + if (eof) + ost->finished |= MUXER_FINISHED; finish: if (ret < 0 && ret != AVERROR_EOF) { @@ -963,6 +967,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) { @@ -978,8 +1028,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); } @@ -1265,8 +1315,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); ost->sync_opts++; @@ -1278,19 +1328,6 @@ static void do_video_out(OutputFile *of, av_frame_move_ref(ost->last_frame, next_picture); } -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; - - 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); - } -} - /** * Get and encode new output from any of the filtergraphs, without causing * activity. @@ -1758,7 +1795,7 @@ static void flush_encoders(void) exit_program(1); } - finish_output_stream(ost); + output_packet(of, ost->pkt, ost, 1); } init_output_stream_wrapper(ost, NULL, 1); @@ -1767,7 +1804,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); } @@ -3078,6 +3115,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; @@ -3086,6 +3126,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) { @@ -3218,6 +3259,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]); @@ -3923,8 +3967,10 @@ static int process_input(int file_index) OutputStream *ost = output_streams[j]; if (ost->source_index == ifile->ist_index + i && - (ost->stream_copy || ost->enc->type == AVMEDIA_TYPE_SUBTITLE)) - finish_output_stream(ost); + (ost->stream_copy || ost->enc->type == AVMEDIA_TYPE_SUBTITLE)) { + OutputFile *of = output_files[ost->file_index]; + output_packet(of, ost->pkt, ost, 1); + } } } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 5403f9998b..106580adb2 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" @@ -150,6 +151,7 @@ typedef struct OptionsContext { int64_t limit_filesize; float mux_preload; float mux_max_delay; + float shortest_buf_duration; int shortest; int bitexact; @@ -482,6 +484,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]; @@ -573,6 +576,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; @@ -583,6 +589,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 diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index a3350a73e9..453ccac912 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" @@ -56,6 +57,8 @@ struct Muxer { int64_t limit_filesize; int64_t final_filesize; int header_written; + + AVPacket *sq_pkt; }; static int want_sdp = 1; @@ -72,13 +75,14 @@ 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; + AVPacket *tmp_pkt = NULL; int ret; if (!av_fifo_can_write(ms->muxing_queue)) { size_t cur_size = av_fifo_can_read(ms->muxing_queue); + size_t pkt_size = pkt ? pkt->size : 0; unsigned int are_we_over_size = - (ms->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); @@ -93,6 +97,7 @@ static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) return ret; } + if (pkt) { ret = av_packet_make_refcounted(pkt); if (ret < 0) return ret; @@ -103,6 +108,7 @@ static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) 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; @@ -192,11 +198,44 @@ static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) } } +static void submit_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) +{ + if (ost->sq_idx_mux >= 0) { + int ret = sq_send(of->sq_mux, ost->sq_idx_mux, SQPKT(pkt)); + if (ret < 0) { + if (pkt) + 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(of->mux->sq_pkt)); + if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) + return; + else if (ret < 0) + exit_program(1); + + write_packet(of, output_streams[of->ost_index + ret], + of->mux->sq_pkt); + } + } else { + if (pkt) + write_packet(of, ost, pkt); + else + ost->finished |= MUXER_FINISHED; + } +} + void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) { AVStream *st = ost->st; int ret; + if (pkt) { /* * Audio encoders may split the packets -- #frames in != #packets out. * But there is no reordering, so we can limit the number of output packets @@ -211,9 +250,10 @@ void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) } ost->frame_number++; } + } if (of->mux->header_written) { - write_packet(of, ost, pkt); + submit_packet(of, ost, pkt); } else { /* the muxer is not initialized yet, buffer the packet */ ret = queue_packet(of, ost, pkt); @@ -321,9 +361,11 @@ int of_check_init(OutputFile *of) ost->mux_timebase = ost->st->time_base; while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) { - ms->muxing_queue_data_size -= pkt->size; - write_packet(of, ost, pkt); - av_packet_free(&pkt); + submit_packet(of, ost, pkt); + if (pkt) { + ms->muxing_queue_data_size -= pkt->size; + av_packet_free(&pkt); + } } } @@ -383,6 +425,8 @@ static void mux_free(Muxer **pmux, int nb_streams) av_freep(&mux->streams); av_dict_free(&mux->opts); + av_packet_free(&mux->sq_pkt); + av_freep(pmux); } @@ -394,6 +438,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); @@ -437,6 +484,14 @@ int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize) if (strcmp(of->format->name, "rtp")) want_sdp = 0; + if (of->sq_mux) { + mux->sq_pkt = av_packet_alloc(); + if (!mux->sq_pkt) { + ret = AVERROR(ENOMEM); + goto fail; + } + } + /* write the header for files with no streams */ if (of->format->flags & AVFMT_NOSTREAMS && of->ctx->nb_streams == 0) { ret = of_check_init(of); diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 09a281dba3..2cd4d42f2a 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -31,6 +31,7 @@ #include "fopen_utf8.h" #include "cmdutils.h" #include "opt_common.h" +#include "sync_queue.h" #include "libavformat/avformat.h" @@ -234,6 +235,7 @@ static void init_options(OptionsContext *o) o->chapters_input_file = INT_MAX; o->accurate_seek = 1; o->thread_queue_size = -1; + o->shortest_buf_duration = 10.f; } static int show_hwaccels(void *optctx, const char *opt, const char *arg) @@ -2326,6 +2328,78 @@ static int init_complex_filters(void) return 0; } +static int setup_sync_queues(OutputFile *of, AVFormatContext *oc, int64_t buf_size_us) +{ + 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, buf_size_us); + 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 are 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, buf_size_us); + 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; @@ -2963,6 +3037,12 @@ loop_end: exit_program(1); } + err = setup_sync_queues(of, oc, o->shortest_buf_duration * AV_TIME_BASE); + 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"); @@ -3675,6 +3755,8 @@ const OptionDef options[] = { { "shortest", OPT_BOOL | OPT_EXPERT | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(shortest) }, "finish encoding within shortest input" }, + { "shortest_buf_duration", HAS_ARG | OPT_FLOAT | OPT_EXPERT | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(shortest_buf_duration) }, + "maximum buffering duration (in seconds) for the -shortest option" }, { "bitexact", OPT_BOOL | OPT_EXPERT | OPT_OFFSET | OPT_OUTPUT | OPT_INPUT, { .off = OFFSET(bitexact) }, "bitexact mode" }, diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c new file mode 100644 index 0000000000..49f0600b6c --- /dev/null +++ b/fftools/sync_queue.c @@ -0,0 +1,427 @@ +/* + * 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 "objpool.h" +#include "sync_queue.h" + +typedef struct SyncQueueStream { + AVFifo *fifo; + AVRational tb; + + /* stream head: largest timestamp seen */ + int64_t head_ts; + /* no more frames will be sent for this stream */ + int finished; +} SyncQueueStream; + +struct SyncQueue { + enum SyncQueueType type; + + /* no more frames will be sent for any stream */ + int finished; + /* sync head: the stream with the _smallest_ head timestamp + * this stream determines which frames can be output */ + int head_stream; + /* the finished stream with the smallest finish timestamp or -1 */ + int head_finished_stream; + + // maximum buffering duration in microseconds + int64_t buf_size_us; + + SyncQueueStream *streams; + unsigned int nb_streams; + + // pool of preallocated frames to avoid constant allocations + ObjPool *pool; + SyncQueueFrame free_frames[32]; + unsigned int nb_free_frames; +}; + +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; +} + +static void queue_head_update(SyncQueue *sq) +{ + if (sq->head_stream < 0) { + /* wait for one timestamp in each stream before determining + * the queue head */ + for (unsigned int i = 0; i < sq->nb_streams; i++) { + SyncQueueStream *st = &sq->streams[i]; + if (st->head_ts == AV_NOPTS_VALUE) + return; + } + + // placeholder value, correct one will be found below + sq->head_stream = 0; + } + + 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; + } +} + +/* update this stream's head timestamp */ +static void stream_update_ts(SyncQueue *sq, unsigned int stream_idx, int64_t ts) +{ + SyncQueueStream *st = &sq->streams[stream_idx]; + + if (ts == AV_NOPTS_VALUE || + (st->head_ts != AV_NOPTS_VALUE && st->head_ts >= ts)) + return; + + 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) + finish_stream(sq, stream_idx); + + /* update the overall head timestamp if it could have changed */ + if (sq->head_stream < 0 || sq->head_stream == stream_idx) + queue_head_update(sq); +} + +/* If the queue for the given stream (or all streams when stream_idx=-1) + * is overflowing, trigger a fake heartbeat on lagging streams. + * + * @return 1 if heartbeat triggered, 0 otherwise + */ +static int overflow_heartbeat(SyncQueue *sq, int stream_idx) +{ + SyncQueueStream *st; + SyncQueueFrame frame; + int64_t tail_ts = AV_NOPTS_VALUE; + + /* if no stream specified, pick the one that is most ahead */ + if (stream_idx < 0) { + int64_t ts = AV_NOPTS_VALUE; + + for (int i = 0; i < sq->nb_streams; i++) { + st = &sq->streams[i]; + if (st->head_ts != AV_NOPTS_VALUE && + (ts == AV_NOPTS_VALUE || + av_compare_ts(ts, sq->streams[stream_idx].tb, + st->head_ts, st->tb) < 0)) { + ts = st->head_ts; + stream_idx = i; + } + } + /* no stream has a timestamp yet -> nothing to do */ + if (stream_idx < 0) + return 0; + } + + st = &sq->streams[stream_idx]; + + /* get the chosen stream's tail timestamp */ + for (size_t i = 0; tail_ts == AV_NOPTS_VALUE && + av_fifo_peek(st->fifo, &frame, 1, i) >= 0; i++) + tail_ts = frame_ts(sq, frame); + + /* overflow triggers when the tail is over 10 seconds behind the head */ + if (tail_ts == AV_NOPTS_VALUE || tail_ts >= st->head_ts || + av_rescale_q(st->head_ts - tail_ts, st->tb, AV_TIME_BASE_Q) < sq->buf_size_us) + return 0; + + /* signal a fake timestamp for all streams that prevent tail_ts from being output */ + tail_ts++; + for (unsigned int i = 0; i < sq->nb_streams; i++) { + SyncQueueStream *st1 = &sq->streams[i]; + int64_t ts; + + if (st == st1 || st1->finished || + (st1->head_ts != AV_NOPTS_VALUE && + av_compare_ts(tail_ts, st->tb, st1->head_ts, st1->tb) <= 0)) + continue; + + ts = av_rescale_q(tail_ts, st->tb, st1->tb); + if (st1->head_ts != AV_NOPTS_VALUE) + ts = FFMAX(st1->head_ts + 1, ts); + + stream_update_ts(sq, i, ts); + } + + return 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]; + + av_assert0(st->tb.num > 0 && st->tb.den > 0); + + if (frame_null(sq, frame)) { + finish_stream(sq, stream_idx); + return 0; + } + if (st->finished) + return AVERROR_EOF; + + ret = objpool_get(sq->pool, (void**)&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); + objpool_release(sq->pool, (void**)&dst); + return ret; + } + + stream_update_ts(sq, stream_idx, ts); + + return 0; +} + +static int receive_for_stream(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 = 1; + + av_fifo_peek(st->fifo, &peek, 1, 0); + ts = frame_ts(sq, peek); + + /* check if this stream's tail timestamp does not overtake + * 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); + + /* We can release frames that do not end after the queue head. + * Frames with no timestamps are just passed through with no conditions. + */ + if (cmp <= 0 || ts == AV_NOPTS_VALUE) { + frame_move(sq, frame, peek); + objpool_release(sq->pool, (void**)&peek); + av_fifo_drain2(st->fifo, 1); + return 0; + } + } + + return (sq->finished || (st->finished && !av_fifo_can_read(st->fifo))) ? + AVERROR_EOF : AVERROR(EAGAIN); +} + +static int receive_internal(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 = receive_for_stream(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 = receive_for_stream(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_receive(SyncQueue *sq, int stream_idx, SyncQueueFrame frame) +{ + int ret = receive_internal(sq, stream_idx, frame); + + /* try again if the queue overflowed and triggered a fake heartbeat + * for lagging streams */ + if (ret == AVERROR(EAGAIN) && overflow_heartbeat(sq, stream_idx)) + ret = receive_internal(sq, stream_idx, frame); + + return ret; +} + +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); + + /* we set a valid default, so that a pathological stream that never + * receives even a real timebase (and no frames) won't stall all other + * streams forever; cf. overflow_heartbeat() */ + st->tb = (AVRational){ 1, 1 }; + st->head_ts = AV_NOPTS_VALUE; + + return sq->nb_streams++; +} + +void sq_set_tb(SyncQueue *sq, unsigned int stream_idx, AVRational tb) +{ + SyncQueueStream *st; + + av_assert0(stream_idx < sq->nb_streams); + st = &sq->streams[stream_idx]; + + av_assert0(!av_fifo_can_read(st->fifo)); + + if (st->head_ts != AV_NOPTS_VALUE) + st->head_ts = av_rescale_q(st->head_ts, st->tb, tb); + + st->tb = tb; +} + +SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us) +{ + SyncQueue *sq = av_mallocz(sizeof(*sq)); + + if (!sq) + return NULL; + + sq->type = type; + sq->buf_size_us = buf_size_us; + + sq->head_stream = -1; + sq->head_finished_stream = -1; + + sq->pool = (type == SYNC_QUEUE_PACKETS) ? objpool_alloc_packets() : + objpool_alloc_frames(); + if (!sq->pool) { + av_freep(&sq); + return NULL; + } + + 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) + objpool_release(sq->pool, (void**)&frame); + + av_fifo_freep2(&sq->streams[i].fifo); + } + + av_freep(&sq->streams); + + objpool_free(&sq->pool); + + av_freep(psq); +} diff --git a/fftools/sync_queue.h b/fftools/sync_queue.h new file mode 100644 index 0000000000..e08780b7bf --- /dev/null +++ b/fftools/sync_queue.h @@ -0,0 +1,100 @@ +/* + * 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 + +#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; + +/** + * Allocate a sync queue of the given type. + * + * @param buf_size_us maximum duration that will be buffered in microseconds + */ +SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us); +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/fate/ffmpeg.mak b/tests/fate/ffmpeg.mak index 154af2fac8..38a1ae7ed5 100644 --- a/tests/fate/ffmpeg.mak +++ b/tests/fate/ffmpeg.mak @@ -105,7 +105,7 @@ FATE_SAMPLES_FFMPEG-$(call ALLYES, COLOR_FILTER, VOBSUB_DEMUXER, MATROSKA_DEMUXE fate-shortest-sub: CMD = enc_dec \ vobsub $(TARGET_SAMPLES)/sub/vobsub.idx matroska \ "-filter_complex 'color=s=1x1:rate=1:duration=400' -pix_fmt rgb24 -allow_raw_vfw 1 -c:s copy -c:v rawvideo" \ - framecrc "-map 0 -c copy -shortest" + framecrc "-map 0 -c copy -shortest -shortest_buf_duration 40" # Basic test for fix_sub_duration, which calculates duration based on the # following subtitle's pts. 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 diff --git a/tests/ref/fate/shortest-sub b/tests/ref/fate/shortest-sub index 3b28898177..ff223d0fa0 100644 --- a/tests/ref/fate/shortest-sub +++ b/tests/ref/fate/shortest-sub @@ -1,4 +1,4 @@ 52bc3d6a0c80e639095a2c28da4ef42c *tests/data/fate/shortest-sub.matroska 139246 tests/data/fate/shortest-sub.matroska -d71f5d359ef788ea689415bc1e4a90df *tests/data/fate/shortest-sub.out.framecrc -stddev:11541.12 PSNR: 15.08 MAXDIFF:22854 bytes: 2591/ 26055 +876ac3fa52e467050ab843969d4cf343 *tests/data/fate/shortest-sub.out.framecrc +stddev:11541.12 PSNR: 15.08 MAXDIFF:22854 bytes: 2591/ 23735 From patchwork Thu Jun 16 19:55: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: 36281 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1129242pzb; Thu, 16 Jun 2022 13:07:30 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vCV+vzhb3H5ikgBj5aarDe5I2+Zl4VAdg6dyAkPddcwROfpZjKtJQDkYWArPs27lAX1PAU X-Received: by 2002:a17:906:eb54:b0:708:99d6:83e with SMTP id mc20-20020a170906eb5400b0070899d6083emr5804977ejb.745.1655410050387; Thu, 16 Jun 2022 13:07:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410050; cv=none; d=google.com; s=arc-20160816; b=c5JVl6LP8y6AINfUy70q1hNglFWl7pkcbcXONDNZ9sWlxAS72mPJOaz+EYsWQzO+ia 4owm/tnftMRPcbIkBTF2c6A8c3CtiR2qY9h7hdBDUSNe6YRofiwHBKfER0+fCk2Mu3Hm 5ohg7tnwHkmeT2LbLZkou6fIfLmyf8NA0yUX4w8r9zb9pvw6Kq9wLE3pe9HPEO0u/hQv G5BRlajo3UCIscEtDYAYgoh7VTpthFsAoaMIRlmwqNEtTFSQ6CsiT/9W/KD/a66TvVek 7f4+QhU4V6ISEPENI1Wt/y7lRegyLgZ1LWqHzWNS3YHU6kguIaWSL2wGIjLb4HeavZIV 9oCA== 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=hlM7HI9wU46r0aKQNfVi459JMwGnznoEv5fNyd4aoag=; b=EF0U0nJV7rRFN8m+tcIfZnX+Lmlb5JayNy5RBTFr6TZ76YaPLO0umy9+WNRMr4B9Mr /GB0kX992HJcLaswyFQSVY4OI5Zuv/aNuG99IgUPNkpfymVLrKHrMIPXF2/Z7gSF3pXU KqCKN7gX3t9tvJ6+PyeJKHVx2f5KDVT6N51p9rR8CKsu2+44zpt4jle25A/7nMiTGO1B EryLU71EYOKUzXIlpaYt+RxMr84QKjb/joteFLn0gghNXbUqeOxFNAGPU67j4PwhuVhm OocdLVzd822BaC/SYReXLPLEJHcLqB1lHWEmIkuCKLnOsYRuDi4cp/xkEPzwdo+WoTYb 1l1g== 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 d25-20020a1709067a1900b006feb7f30ef7si2412243ejo.58.2022.06.16.13.07.30; Thu, 16 Jun 2022 13:07:30 -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 56A6F68B8CF; Thu, 16 Jun 2022 23:04: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 31BC968B865 for ; Thu, 16 Jun 2022 23:03:47 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id D58B5240175 for ; Thu, 16 Jun 2022 22:03:46 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id uXoBHYS5jh_Q for ; Thu, 16 Jun 2022 22:03: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 91E97240699 for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id DF8753A20F3; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:22 +0200 Message-Id: <20220616195534.5278-23-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 23/35] fftools/ffmpeg_mux: reindent 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: yKZVNroiTT6b --- fftools/ffmpeg_mux.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 453ccac912..641bdb98b0 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -98,16 +98,16 @@ static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) } if (pkt) { - ret = av_packet_make_refcounted(pkt); - 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); + 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_packet_move_ref(tmp_pkt, pkt); + ms->muxing_queue_data_size += tmp_pkt->size; } av_fifo_write(ms->muxing_queue, &tmp_pkt, 1); @@ -236,20 +236,20 @@ void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) int ret; if (pkt) { - /* - * 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; + /* + * 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++; } - ost->frame_number++; - } } if (of->mux->header_written) { From patchwork Thu Jun 16 19:55: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: 36264 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1127875pzb; Thu, 16 Jun 2022 13:04:48 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vC3PzP14FTao7ASPmzvZSHXvDcWrE8fnb9lcFuY+z//TH8Dbuig/+/Hq3CPouMNCks1be7 X-Received: by 2002:a05:6402:4003:b0:42d:fb4d:dada with SMTP id d3-20020a056402400300b0042dfb4ddadamr8528225eda.183.1655409888119; Thu, 16 Jun 2022 13:04:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409888; cv=none; d=google.com; s=arc-20160816; b=yJDoiHdCjqAGn0w5grli30hz40XlnbMSC7zte/PTr1nHLWcOdCOWi/s0ud6xOsm/8f BAFCjT9/qguwnVy/0YSN7ipsAC6IeSyNnrhefJDCR7Nm9vSQqas6f3CHeuc3NSlM23Wh qxgj/LkbKIidZfldu6oeX6zEcnyXEdveKLE2ySF6mouKIOtLnJ1MPw2DKEDVOUAwQgRj 5grQZcw8b8toSFmSwYGB3NHR8Ghb0JLGwW5QFAX9pGB7PR81hSnqJy3OyaNEEe8A/nhw BtzYiieigYDV5my4/sJG1Uig1vDITl90IiE26RYUIPfW2xj266ukujyMIfaPxTnDWe4r Qmvg== 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=+wOVISu4SE4M7JH/fYlo0t5pmEtpIBADpW6mrY3yz7I=; b=kX9kGa07+0YSwyKu5KjXkbaU2fJMNDCiJ1bOEx3ZERgU3NE0OR+UxHEyfTqlM9aNoS EyKy07nkQdN0gMCo5JQSeU/uk9RNhH4U0+3tA3S8RXXxKkRGS19h6QxSIbDkyFoBQaMt b6VBUhAl47yKHZjnH4iqhbEA0+GLMLGCzSjiHb2NgVy8yWcanUsaHuBdvoLV9tyC+BiA QyghLro0YZtqqkJyC2HMqbFJDiBoaSRzSETw1EugQu30a1AtTIo0OVi7CsASfaf9Di1r NTgyVknrbTzjroucwofYlbo0/xRC7z02kPljJGv0CsK51zVJaywDmFf//6vYv3VGSGsG ut8Q== 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 y3-20020aa7d503000000b0042dfcc43f17si2921492edq.274.2022.06.16.13.04.47; Thu, 16 Jun 2022 13:04: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 E34C168B874; Thu, 16 Jun 2022 23:03: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 73EAE68B7F5 for ; Thu, 16 Jun 2022 23:03:42 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 59C4624056A for ; Thu, 16 Jun 2022 22:03:40 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 0KWw1uCCLgp2 for ; Thu, 16 Jun 2022 22:03:39 +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 28A84240592 for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id E44513A20FA; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:23 +0200 Message-Id: <20220616195534.5278-24-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 24/35] 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: HHhme2BMC2RA 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 | 20 +++++++++++++++++++ fftools/sync_queue.h | 7 +++++++ tests/ref/fate/matroska-flac-extradata-update | 16 +++++---------- tests/ref/fate/mkv-1242 | 3 --- 7 files changed, 48 insertions(+), 32 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 1a14637ece..a471fa0f8f 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3467,12 +3467,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 641bdb98b0..56444770bf 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -237,19 +237,11 @@ void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) if (pkt) { /* - * 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 (of->mux->header_written) { diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 2cd4d42f2a..22eb558ff3 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2331,6 +2331,7 @@ static int init_complex_filters(void) static int setup_sync_queues(OutputFile *of, AVFormatContext *oc, int64_t buf_size_us) { 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)) @@ -2345,14 +2346,19 @@ static int setup_sync_queues(OutputFile *of, AVFormatContext *oc, int64_t buf_si 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, buf_size_us); if (!of->sq_encode) return AVERROR(ENOMEM); @@ -2371,6 +2377,9 @@ static int setup_sync_queues(OutputFile *of, AVFormatContext *oc, int64_t buf_si 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); } } @@ -2391,6 +2400,9 @@ static int setup_sync_queues(OutputFile *of, AVFormatContext *oc, int64_t buf_si 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 49f0600b6c..08c3cf4f2a 100644 --- a/fftools/sync_queue.c +++ b/fftools/sync_queue.c @@ -36,6 +36,9 @@ typedef struct SyncQueueStream { int64_t head_ts; /* no more frames will be sent for this stream */ int finished; + + uint64_t frames_sent; + uint64_t frames_max; } SyncQueueStream; struct SyncQueue { @@ -264,6 +267,10 @@ int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame) stream_update_ts(sq, stream_idx, ts); + st->frames_sent++; + if (st->frames_sent >= st->frames_max) + finish_stream(sq, stream_idx); + return 0; } @@ -362,6 +369,7 @@ int sq_add_stream(SyncQueue *sq) * streams forever; cf. overflow_heartbeat() */ st->tb = (AVRational){ 1, 1 }; st->head_ts = AV_NOPTS_VALUE; + st->frames_max = UINT64_MAX; return sq->nb_streams++; } @@ -381,6 +389,18 @@ void sq_set_tb(SyncQueue *sq, unsigned int stream_idx, AVRational tb) st->tb = tb; } +void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, uint64_t frames) +{ + SyncQueueStream *st; + + av_assert0(stream_idx < sq->nb_streams); + st = &sq->streams[stream_idx]; + + st->frames_max = frames; + if (st->frames_sent >= st->frames_max) + finish_stream(sq, stream_idx); +} + SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us) { SyncQueue *sq = av_mallocz(sizeof(*sq)); diff --git a/fftools/sync_queue.h b/fftools/sync_queue.h index e08780b7bf..7beddf8c4a 100644 --- a/fftools/sync_queue.h +++ b/fftools/sync_queue.h @@ -63,6 +63,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 Thu Jun 16 19:55: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: 36288 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1129771pzb; Thu, 16 Jun 2022 13:08:40 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vHOGmPjvR5FThdpFjJNQcKJTw5Wl0j91Z8CYaE88JZpAk7n3TVx6OerzX9TDqZ/r1RdZMc X-Received: by 2002:a17:907:6d1a:b0:71b:e858:d187 with SMTP id sa26-20020a1709076d1a00b0071be858d187mr3704293ejc.596.1655410120284; Thu, 16 Jun 2022 13:08:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410120; cv=none; d=google.com; s=arc-20160816; b=ovOZ/MUv5SeQTMQHTjKnEEI2mDWHZDlISLbGhapGJQ0vC4VCVMAVNEEZHBu2zidrg8 Hodmz0jCpsnQm/t+cSax+Kp9uLmX/14PJ3isuHChfKa9LN9shbB6It2s0SetZOz3hsnO OC9eHlu7AYaySxHfn7M9Hp95npq3zUSxsp63MbYj7zXJtJc98Zb3pKntLXNwcaLXQ4Tw Tkj5sEDf2x2kF6hxARCd/st590Qv/S02EyqGmg4FGQ+M0+IBdEqRPykA3Z7FdIwkmDMj ZHFoYurCZO8B0MGYIjpF84IH2yCOOWSHA1eGIDER6/wnL5rhT+Ry7I9yJFADKklBAOYc 9GcA== 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=9j45SkixfzLzuH2SGGB/L5dbQJYT+xt6Yp6GhqWWVtc=; b=oCiqgmn1d5nvX+1AzLnTBAFU8sccW3LolnugadwtLXQeoTmqI2J+hiAmpTtXm/IMAK LuNMLL5WVClvrW5vCB8zOS/YlGd3mWTjlfmbbJZvaTzNXSLVGhotm4X5rHZ9x8L4QrfK jN5wFmgwyPu6ybQ9qTt8mk5Oxs2juiop4pQkTO5N8TPLYZxXaQT705n88J3ATK5MV9VB C8JWRw/gaGDc8QelegEKdnvyJR2JH20GLJ1woPmbhgKk/Gi/wIExzJHv45lfQ7oJVGKd 13hFgxXcUt7etb9VIhRcPSmkTUP8W2PTb6XSqGIP0Fd7JXkN/67WcWPpkybPptDE7V0y +FrQ== 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 cr18-20020a170906d55200b006ff46987deasi2710524ejc.311.2022.06.16.13.08.39; Thu, 16 Jun 2022 13:08: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 469E068B8F2; Thu, 16 Jun 2022 23:04:10 +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 10AB568B79F for ; Thu, 16 Jun 2022 23:03:45 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id ABB7224017C for ; Thu, 16 Jun 2022 22:03:44 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 8_ydfs2uayHR for ; Thu, 16 Jun 2022 22:03: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 8A4AA240694 for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id E8E503A20FB; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:24 +0200 Message-Id: <20220616195534.5278-25-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 25/35] 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: J08VwT2sJ2Zt This field means different things when the video is encoded (number of frames emitted to the encoding sync queue/encoder by the video sync code) or copied (number of packets sent to the muxer sync queue). Print the value of packets_written instead, which means the same thing in both cases. It is also more accurate, since packets may be dropped by the sync queue or bitstream filters. --- fftools/ffmpeg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index a471fa0f8f..beb07928bf 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1585,7 +1585,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 Thu Jun 16 19:55: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: 36267 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1128138pzb; Thu, 16 Jun 2022 13:05:16 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sajT5TudPmzEyC7TrfNntPnVmWmiOOjXkWa/HPuqlA29RUq8tRybXwi9uNNxFDih5dmBxQ X-Received: by 2002:a17:906:7f06:b0:70e:2dc9:aec0 with SMTP id d6-20020a1709067f0600b0070e2dc9aec0mr5813813ejr.674.1655409916448; Thu, 16 Jun 2022 13:05:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409916; cv=none; d=google.com; s=arc-20160816; b=VPJZWukMEl7WGYwSE6Ak2QV3jES4/LZFLPDgLr8Z0qHLpEWbu7/X++1TBrcmKHSi9h YRuFkdCGfDyg+C9gV56WNgm1f4Rsk+xWzi/wgibKgVusBpxFJrDayW6m2ZMI/Gksbr2o Wc9HSCZqXA4n2UVCyI4ZwNXqhT8Bwsvb1/V9FUYLkin1dE2x4KstcssHA45n1oY0xVIF j25av7bRey1xaOpsJWSFFqhX7Zs7dgO1Qyzu7gYZuGECnxRkuExIS2RdgLQqeM7xQJZ7 vzGUa1KLXy68i9QMppI/mGFvLJsw16P3Fmg+mUFxQeG8vwwqRr/0gY1nLTnuoBgL4AyZ ZXMg== 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=nQhnmz763wUUtBPWQmC8b0Gxt8Q1k79U0ZAplOg4AGU=; b=JNEd+/3w+vX1H19gR/XdsBlCH7ocewcVfNaiL5gK/dezfMnDo3bfxG8UsKIoMN7nqS 0VvQIy3pjid66xcfYIZSG2XpTWMKcvIdXcURJ3A9A2Np6CI6jk2/McKKgZipahMe2DBT QrovEee+G6fqiYkHMNCQkn0bu9WsaEFOBVcOyl8I+8tQCR2zA761IbPLc35HKhlrQ28m J85C6csqWt5PK1c940jJ6DhRcDtcaT9MjpEYLb/0/FTarLlF/HnSNj1B1sXImGpWpp0N zVzG45d0zzuSX1xzrfhzJe1N3aURQmNbaIpbueY/oKqBt9MnQ028x0gFyubzzlKfZNO6 BWTA== 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 g19-20020a170906349300b0071208964108si2579928ejb.635.2022.06.16.13.05.16; Thu, 16 Jun 2022 13:05:16 -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 E827D68B85A; Thu, 16 Jun 2022 23:03: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 EF4FE68B81A for ; Thu, 16 Jun 2022 23:03:42 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id B4AFD24017C for ; Thu, 16 Jun 2022 22:03:41 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id de3zb58faLgh for ; Thu, 16 Jun 2022 22:03: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 31B0A2405F5 for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id ED8CC3A2101; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:25 +0200 Message-Id: <20220616195534.5278-26-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 26/35] 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: 7zpiELt0uGzn 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 | 10 ---------- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index beb07928bf..ab3665e53c 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1176,7 +1176,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; @@ -1184,7 +1184,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; @@ -1214,7 +1214,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, @@ -1226,7 +1226,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) { @@ -1320,7 +1320,7 @@ static void do_video_out(OutputFile *of, exit_program(1); 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 106580adb2..3d55370d3a 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -460,7 +460,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 56444770bf..87c7bdc4e3 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -232,18 +232,8 @@ static void submit_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) { - AVStream *st = ost->st; int ret; - if (pkt) { - /* - * 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 (of->mux->header_written) { submit_packet(of, ost, pkt); } else { From patchwork Thu Jun 16 19:55: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: 36291 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1130384pzb; Thu, 16 Jun 2022 13:10:04 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v2YuJC2jCYFsm6sjeeDZb6/efbWqyJA/bHAsS9qHz7K7uaBffEETSvLDdpJNbaNYEvSfhH X-Received: by 2002:a17:907:8c1a:b0:715:73d2:df36 with SMTP id ta26-20020a1709078c1a00b0071573d2df36mr6198119ejc.256.1655410203954; Thu, 16 Jun 2022 13:10:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410203; cv=none; d=google.com; s=arc-20160816; b=b4/P5blJCKgKpF2uZw7SOW2tSErFyTuLZH+9fVxCgLwXQkpECpVlJgQCon9dZwsoAJ qsup6KIiKBCBUv6H9BblIzUHxlLyoAj/U4KZryr7GKHQ5a8mR61dHqNR+RR8G4u54Se6 +UChJ4GWKGpRm6FuMgU0qa+wkU9ZhNVHVTSkC8x8/rnbxx692lkaZZUFuO+36j95lIEh 1RdNuaFLJGW/Hu1Gfq9QCv0h6rrZV69yv4Z39EuRR/YRxyVta+JGtXuv5WU6ZtQMlPRB fSGRUs0HfFaGcK/x+q9oRAJgSqwaZVvehmLDIFuiAJOYqc54BYa2XXG1/B8RVTHr775r dQ0w== 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=EvvV/N7/vI74m7sEJlZBICCCylupLN/j2UVeTVG3hF8=; b=eBOZ0HMxpWzFNGRzrMFEDFhBqG481PLs0zNqW3n7Bj0mB6OjupU5vHaaKlPc6pyBuS bYqrAIRuU/xWU0y+MyBdHZ4bMqgwPaMQkP5bBB+1QD4umaqJdpc78OsWe8cCjVeKfb60 +UMB8l3dW/GLBBAGVzmd+6w7rlLiBucu9L/nRGiJWZdwRMGdMxG/iaEY7puKoAcMZNDu 48RLf8TgtEiX4B8+hLKeNZG4xC+vAf7fwFfLuY1npDko3p2C8kZ3azl8Qms1Pi+SR7zj besc4yCRGjEMAbFDBz129PVF/PLOYeglLtqOE5N6y2Eocc2HjZROi/8N17Hy8tYJ021T o1bA== 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 h26-20020a170906261a00b006f375d5e033si934353ejc.631.2022.06.16.13.10.03; Thu, 16 Jun 2022 13:10: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 447E168B8D6; Thu, 16 Jun 2022 23:09: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 EDFD368B84E for ; Thu, 16 Jun 2022 23:09:36 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 9F8062400F5 for ; Thu, 16 Jun 2022 22:09:36 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id jeObSV3oZVsy for ; Thu, 16 Jun 2022 22:09:35 +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 8092624017C for ; Thu, 16 Jun 2022 22:09:35 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id F26C83A2192; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:26 +0200 Message-Id: <20220616195534.5278-27-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 27/35] 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: b+ItUMMUFdbi 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 ab3665e53c..4321652848 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1496,9 +1496,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 3d55370d3a..c757f72c0e 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -589,11 +589,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 @@ -697,7 +698,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 87c7bdc4e3..c44ff0f1df 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; @@ -117,7 +119,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; @@ -263,8 +265,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++; } } @@ -302,15 +304,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 " @@ -321,7 +324,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) { @@ -333,7 +336,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; @@ -356,29 +359,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; } } @@ -386,14 +390,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; @@ -409,13 +427,14 @@ static void mux_free(Muxer **pmux, int nb_streams) av_packet_free(&mux->sq_pkt); + fc_close(&mux->fc); + av_freep(pmux); } void of_close(OutputFile **pof) { OutputFile *of = *pof; - AVFormatContext *s; if (!of) return; @@ -423,34 +442,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) { @@ -475,7 +493,7 @@ int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize) } /* 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; @@ -483,7 +501,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; } @@ -495,7 +513,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) @@ -512,6 +530,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 22eb558ff3..5ef4115cdb 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2455,7 +2455,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; @@ -3055,7 +3054,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 Thu Jun 16 19:55: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: 36269 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1128284pzb; Thu, 16 Jun 2022 13:05:35 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ubgvcySa9fq9+FqBLekCE5gIuR+G1U7j1YD0moSH5vLWcNAXFTYq1DDvTKAgeNMO3xUC/U X-Received: by 2002:a17:906:f84e:b0:70e:6ec8:cc4a with SMTP id ks14-20020a170906f84e00b0070e6ec8cc4amr5873283ejb.694.1655409935072; Thu, 16 Jun 2022 13:05:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409935; cv=none; d=google.com; s=arc-20160816; b=kWGpIb/UFZv377Dpwbi8P4HW2PmKqRKkqvN0IEG3t27Sc3GkUPf4FKa4rG5e15663Q wQoz1T0UcUDWK/PT3WGkWhgPEGWyeGFsWoKU564qdy1k1kIKIE7gu7LudVX3qZNO6UXt WjcUX1tlAcA+LvmhtqK+4PUzPXk4RypX5ACa7HZW3PPqNRybuUKxd4FWSRz8yxH8QCdJ 2NQJs1jlYRE0dJRzU5ihizzUA15pgAdwg0fem1eK8T4IZUUJFb3ItjnOKhJrtaGRwpHa wTX+vxb/fKy1PtosKrg55xtyF0tHt6eJ4m6epDe14SqjrdZyxvaHDsR0pux/IHt34X1R GHRw== 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=3OuIg2aGho/voGEiIzneXL+iCQ7UeBiZ0T84ZGvbThE=; b=VQ6mj+4yI20l6EsETRwhNsEPlXLYoc50fV67VJJa3vrnDz22xCLLP1eZVF1eMn29vP JKn4o2t1HB8g4rJ5SfdnxAZynpbp8da7z+F7MIkz3Bn8+jOaH5OVNQZy4L2HVAeVy/xg vr+YXIG3Hduns0Xj3Yry35SLeLi2xS41B6EGFF+urQSRTPI4TdIihofxZsIepOOYRP73 wt7AiczL2iA2iJDaVzqH1Goa2lz8X2tGPEh3KyIuCxzHqzkpyOQC1Hrv5xKnKZDUViDv vdJQHom2xoO0fUtFWGicbPDVqbXLF9omc0HWPOig3RvLxSh3k3FiJJApFCWzXl2CimaU wr1w== 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 c30-20020a1709063f1e00b0070eb188249bsi2646808ejj.358.2022.06.16.13.05.34; Thu, 16 Jun 2022 13:05:35 -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 B01F568B81D; Thu, 16 Jun 2022 23:03: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 4FDA168B817 for ; Thu, 16 Jun 2022 23:03:44 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id B4CE724017C for ; Thu, 16 Jun 2022 22:03:43 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id iZ5Lb88bQwgl for ; Thu, 16 Jun 2022 22:03: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 45CFE2400F5 for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 02E323A2193; Thu, 16 Jun 2022 22:03:31 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:27 +0200 Message-Id: <20220616195534.5278-28-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 28/35] 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: rawcTMfN7q3E Do not call exit_program(), as that would conflict with moving this code into a separate thread. --- fftools/ffmpeg.c | 54 +++++++++++++++++++++++++++++++------------- fftools/ffmpeg.h | 2 +- fftools/ffmpeg_mux.c | 6 +++-- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 4321652848..5df453c2d9 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -713,6 +713,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) @@ -720,28 +721,49 @@ 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) { + err_msg = "submitting a packet for bitstream filtering"; + goto fail; + } + + 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, bsf_eof ? NULL : pkt, ost); + if (ret < 0) + goto mux_fail; + } + } else { + ret = of_submit_packet(of, eof ? NULL : pkt, ost); if (ret < 0) - goto finish; - while ((ret = av_bsf_receive_packet(ost->bsf_ctx, pkt)) >= 0) - of_submit_packet(of, pkt, ost); - if (ret == AVERROR_EOF) - of_submit_packet(of, NULL, ost); - if (ret == AVERROR(EAGAIN)) - ret = 0; - } else - of_submit_packet(of, eof ? NULL : pkt, ost); + goto mux_fail; + } if (eof) ost->finished |= MUXER_FINISHED; -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); - } + 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 c757f72c0e..91f5b4024b 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -705,7 +705,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 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 c44ff0f1df..490e0e54eb 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -232,7 +232,7 @@ static void submit_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) } } -void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) +int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) { int ret; @@ -243,9 +243,11 @@ void of_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; } static int print_sdp(void) From patchwork Thu Jun 16 19:55: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: 36282 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1129312pzb; Thu, 16 Jun 2022 13:07:40 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tFqRVTPNQA6qhSt84TP4zjOrXrZwBWrTgCdWeqw3Jk5MrRBPiDEVywg/8yuHXbzT+4qyGB X-Received: by 2002:a05:6402:2812:b0:434:dcb3:c85 with SMTP id h18-20020a056402281200b00434dcb30c85mr8660277ede.1.1655410059843; Thu, 16 Jun 2022 13:07:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410059; cv=none; d=google.com; s=arc-20160816; b=Ndj7s0vepNWhcBZkzDeZbmpHOBtfop/a5HPUZOqBWBwF34AU7UkzOJbO5et3duB29N dtKBefhCAV0vQd/4NUbJbqT8NKnHsk1z0jVWoNRlbkvIh0mrJNOtMcB3oUwFUzWrsmX3 r07bmkqPwus5kT7jp5lZ/cbno2OPmOhUSluzf3PDn+/q2hV5CAb9lOv6S/1sZQLXB3Zl K5QDMhb/FH/tLJqcDR+ONKGQifTX/cYHTujQDMO0dBa5r/KpxFC8CR1InZj7X2Qx4q/B VtQhYyarUJspVAQjifEU/EOsG6qCrse9ITPhLPlNBj3w8mr9ZEAVc0QC3PFArjESTsHR VpUQ== 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=nwK4feh3pF40cDXx98VX5sfWbwAVDQcF8iY25QhvfGA=; b=Zc2+3y8W/2lUUVPKutSLdqN6BlD27Wp4BnhcrmwYJJEltOFHvtXHp4OMtmn/fPpEr9 +3dTt+KbLKM76g80YZTQ5BgJoghuJzG1upDej+APhsaJ7jE8mWcQCVojEJlOemh4S+me /UpfqKeuUcrzx6tZgw5Ym+H/laEFbcCnjqVc0LIF4GbUi+goIEtNDWwn8TqB84WQR3+/ LjhHeSy6EwCf3L3qtNb79QFwCyuprguFOGUdUAIWEwiAF/LWo6W/fgJAUXKztnOhBdk6 1HCNoLCuohYu7Q2jqjEivB9iHC39K/eEhy5B5IWWnI0SgrNDFMCUSc/wnzAUNvClhJ1E qvOA== 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 er17-20020a056402449100b004355e4ebcd3si112232edb.344.2022.06.16.13.07.39; Thu, 16 Jun 2022 13:07: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 2B05868B8D5; Thu, 16 Jun 2022 23:04:05 +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 AC81668B874 for ; Thu, 16 Jun 2022 23:03:47 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 6E2E6240175 for ; Thu, 16 Jun 2022 22:03:47 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id Z7UDBqecv3J3 for ; Thu, 16 Jun 2022 22:03: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 91EB224069A for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 0771A3A21BB; Thu, 16 Jun 2022 22:03:31 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:28 +0200 Message-Id: <20220616195534.5278-29-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 29/35] 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: ZQ8b9Uk1JXDc Do not call exit_program(), as that would conflict with moving this code into a separate thread. --- fftools/ffmpeg_mux.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 490e0e54eb..98d3adaeac 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -200,7 +200,7 @@ static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) } } -static void submit_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) +static int submit_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) { if (ost->sq_idx_mux >= 0) { int ret = sq_send(of->sq_mux, ost->sq_idx_mux, SQPKT(pkt)); @@ -209,17 +209,17 @@ static void submit_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) 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(of->mux->sq_pkt)); if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) - return; + return 0; else if (ret < 0) - exit_program(1); + return ret; write_packet(of, output_streams[of->ost_index + ret], of->mux->sq_pkt); @@ -230,6 +230,8 @@ static void submit_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) else ost->finished |= MUXER_FINISHED; } + + return 0; } int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) @@ -237,7 +239,7 @@ int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) int ret; if (of->mux->header_written) { - submit_packet(of, ost, pkt); + return submit_packet(of, ost, pkt); } else { /* the muxer is not initialized yet, buffer the packet */ ret = queue_packet(of, ost, pkt); @@ -348,11 +350,13 @@ int of_check_init(OutputFile *of) ost->mux_timebase = ost->st->time_base; while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) { - submit_packet(of, ost, pkt); + ret = submit_packet(of, ost, pkt); if (pkt) { ms->muxing_queue_data_size -= pkt->size; av_packet_free(&pkt); } + if (ret < 0) + return ret; } } From patchwork Thu Jun 16 19:55: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: 36284 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1129436pzb; Thu, 16 Jun 2022 13:07:58 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uXXXzoBzUVS9ueAtVR8dCXNLIcvIQUX76JgzTfvZMqSWqabHVSyr/JI/rzsaVaBvwBBWPi X-Received: by 2002:a05:6402:28b6:b0:433:2b53:157a with SMTP id eg54-20020a05640228b600b004332b53157amr8783476edb.395.1655410078634; Thu, 16 Jun 2022 13:07:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410078; cv=none; d=google.com; s=arc-20160816; b=wS/DYqXaglclsUd5xXcB+bQmT+CcICNIBEz8ZA8l5gAfaQoY3jw9f9xbevfJljGib6 jyzmVry7xsu+akB42x293Z3S4dASMSMY1pPX5jzuMfF33R85LGN1ZSfw8XzBxXLFPca4 q+BD2sHh86A+gRaThD7FuSLurXUqLimHCGqVYLoChOSCaXbGHHTFwD+VAAENxZ8F0ICP TVrpGTVEhoCyYFGFLTEbcgVstkiA+Mzj7k3F5QErippN02czHVV7Hq4DGT5z2Bw0h5IB onNd2fHW3lq797YWqI+OM/7ilqEJpLb7ZJA9shsXsmM9R53tQ7gcmFncuNc/v/zZlCyo /Shw== 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=SE6INjYHNkvb2vNUdx5Gka6/vSnk+3CFBwdgCkBqyfY=; b=CIF6moyaXLh0M9qjxaAvY0PpdaNLVK8I8Rzy/OkX9A5tB15KOJrgj66YUKBYtxgOsK Hr3ux+5q+sBJj6tzDA0RnYioKUCZoy1mZ/NYCCv17B9KCkj2a1uMRN+JoobgW/XNt38A 5mP5c9kw2poaSUyISQxGn2bjEdz/uYj687Hlj7y3n2vOaO0hV1ls7cJ2aehUUw7a7jad MxGJH0WPqKu7xaThA7lOwSX0KkBiLqMPtcwJq8g5uYwXgh0umIm1Dj8pvSRL8+NbAXjU UeahmzxLeMpmFE8RO2xhy6LMQlMr/dUYE4Y77N8itIPAi93Fyt3xNiHrugQUvP6XnSWp qD5w== 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 js22-20020a17090797d600b00718c84e45d4si3005258ejc.794.2022.06.16.13.07.58; Thu, 16 Jun 2022 13:07: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 5122E68B8DE; Thu, 16 Jun 2022 23:04:07 +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 D684368B881 for ; Thu, 16 Jun 2022 23:03:48 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 9B8C0240175 for ; Thu, 16 Jun 2022 22:03:48 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id CFrCwSGy87G5 for ; Thu, 16 Jun 2022 22:03: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 5DB5824068C for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 0BC973A21CE; Thu, 16 Jun 2022 22:03:31 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:29 +0200 Message-Id: <20220616195534.5278-30-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 30/35] 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: oN2Ia9kx7LU/ Do not call exit_program(), as that would conflict with moving this code into a separate thread. --- fftools/ffmpeg_mux.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 98d3adaeac..cf24ae7700 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -116,7 +116,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; @@ -161,10 +161,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); @@ -197,7 +195,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, OutputStream *ost, AVPacket *pkt) @@ -221,14 +222,16 @@ static int submit_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) else if (ret < 0) return ret; - write_packet(of, output_streams[of->ost_index + ret], - of->mux->sq_pkt); + ret = write_packet(of, output_streams[of->ost_index + ret], + of->mux->sq_pkt); + if (ret < 0) + return ret; } } else { if (pkt) - write_packet(of, ost, pkt); - else - ost->finished |= MUXER_FINISHED; + return write_packet(of, ost, pkt); + + ost->finished |= MUXER_FINISHED; } return 0; From patchwork Thu Jun 16 19:55: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: 36289 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1130234pzb; Thu, 16 Jun 2022 13:09:45 -0700 (PDT) X-Google-Smtp-Source: AGRyM1shY2NPGAeoLrJyNf2kXmIEbkm1wm1VcYGDbyKJcxw6qn07lhhae8NBNOny8o7pYRkH8htx X-Received: by 2002:a05:6402:5250:b0:42e:726e:e064 with SMTP id t16-20020a056402525000b0042e726ee064mr8544006edd.153.1655410185615; Thu, 16 Jun 2022 13:09:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410185; cv=none; d=google.com; s=arc-20160816; b=DjI7tB3PR6YVUHjbK+lfGxg1gg0bPnJhy1ogIl5CmLdPkHjIiv2qtiXrHuz+ugYQDB WSfTm5xkqdXguBqhsVykSrQxU+NPmRNdaZz8/n6XLT5B/UGD02JTxGlikS6hp0fK7j4U 6nB7Rh2CRhPMAA21SNub/p6HmYw1f+b4LGLnUUXe1lsZsYeEs+cLK4cvWXGML7Wjaqgc JkdCEYImuEQLYFRUJqNdmHXPag+IaodFgZxs1T+PTNiIJhC+ssKxBD0lE0N6SkhcvZVR j36Cz7wQOQV/RAPex1JS2Wzwtxzf2bfFRv0V4YT5gd1SRNdnbpsgkf1ECwKV80knAFnN Dcsw== 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=LT4u2dY0w2e+Xkc78y4S5hnoD/ln6g7G3P5DmF+I/8U=; b=f0l5VIHQVmRaWNRMSmErOnC2e0jTUb65QtEQF0F5FYZBR6HG50ovw3Ev5Xbx4D/X4o L//rRi0NZ/g+tKVNv6MJBazSG/HozK9Lqf1HAArH1uiYkqnKetFx56wWidSPE58ZzNv6 ibzDL/WtXNolzx+vOqpssCSFffrPOCC+IonXCpiJjI7q9ydzBKnoGrIxzTJhSlGcec+n 1ZD2C8gouvcyPTf+w2cclO7zk9JB0/WrUyaTWXDCX76YyJIN9CQnLgpkb4IMWBo4jDrG Gfpeoai/i6HfIYmapT6Ls8O/kjTp+NB6IcXwSXKWStWEAVux+3VuKV3pLqcALYov0zsE flNQ== 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 bo23-20020a0564020b3700b0042e2569656csi2824519edb.160.2022.06.16.13.09.45; Thu, 16 Jun 2022 13:09:45 -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 8836668B8C6; Thu, 16 Jun 2022 23:09: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 43BAB68B834 for ; Thu, 16 Jun 2022 23:09:36 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id E6D6724017E for ; Thu, 16 Jun 2022 22:09:35 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id sG3F3xTpWvC5 for ; Thu, 16 Jun 2022 22:09:35 +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 7BA31240175 for ; Thu, 16 Jun 2022 22:09:35 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 1050E3A21D9; Thu, 16 Jun 2022 22:03:31 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:30 +0200 Message-Id: <20220616195534.5278-31-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 31/35] 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: BxkbQIZKdoHw 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 cf24ae7700..2abadd3f9b 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -270,7 +270,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 Thu Jun 16 19:55: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: 36287 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1129672pzb; Thu, 16 Jun 2022 13:08:30 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v3ebpRlBuJw4oPt/MPfdi4jheHerK6hmB2507M7mpcz2GqVsa+vnKxmH6CrlRGPthj8W+o X-Received: by 2002:a17:906:ae92:b0:711:2b64:c829 with SMTP id md18-20020a170906ae9200b007112b64c829mr6184197ejb.89.1655410110485; Thu, 16 Jun 2022 13:08:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410110; cv=none; d=google.com; s=arc-20160816; b=JSbIa7xPopba5efqLk3zEqzwOMsP4bTaPHjtto4hhJ8tvEpwDZQTRekfALleAO0BOU hHTYoN5sj4vQoloFfat+rIwmfdliQv4i6URK81abAN8FKAJVcaeR6voPCnU3YJn6EB3K T2YpiC8n2LRCFpGVXpYxmlZVm72UlGor4PvKUtRCB2wTt93O73TYcuToHiy463Tqn3jY W3TCeH7mplv2HSck+BCpXBckUwy/Y6JC0kunaU5bIXGGAH4pAaNPgLYDtLOlJqh60AJv FavQhx+SOk4Ppv3IBHoHfK+Tb6pUWA3GE80pnZyZoblIPUVyZGJosoXFPwbSKX773936 z9wA== 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=2BDOmRDmxRTm0V1jUz8Weelms56PBjBALLD0PCJf4j4=; b=D7Ne8xWEDSShCREkD6AIAu1di2GJauqqJ/EAIkimpXROEUW8ndG73y6ePtkdPO56sX 026fTFGn3IvExJQpZw9cg23YitNB/Uj8c2cNhh9MfvYChp/EoaGtEjnVgV3ZjV6oXvXX AqFxI99YPouZJCeU9UG0R7RCPAAdBiuGDDssYL10hIDQYtIeDKKjme66YFWNoalz78y9 nwMrep745XLvW45d7B6DYBH+rxKQLa6EuCStKq1VMKbNMY4sgrJllaJ/Pd9D4qA7NumJ peaSUhfn9L/iwxOBjA+e6PU3y9piX+wnfaOx9KtMMtv+gE1lMmj9hiBoMkIlNY7/tkTY EjdQ== 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 qa19-20020a170907869300b00718c1086665si3028096ejc.328.2022.06.16.13.08.30; Thu, 16 Jun 2022 13:08:30 -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 7FAA268B8EC; Thu, 16 Jun 2022 23:04:09 +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 B111B68B85A for ; Thu, 16 Jun 2022 23:03:44 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 2C9E224017E for ; Thu, 16 Jun 2022 22:03:44 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id vNgbwkcXYFw0 for ; Thu, 16 Jun 2022 22:03: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 6B40524068F for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 14C103A2214; Thu, 16 Jun 2022 22:03:31 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:31 +0200 Message-Id: <20220616195534.5278-32-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 32/35] 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: WD1Udt3TZFDh It retrieves the 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 5df453c2d9..f57028b9b7 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1658,9 +1658,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 Thu Jun 16 19:55: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: 36290 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1130308pzb; Thu, 16 Jun 2022 13:09:54 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vS3NsohK2V7BsTte1csppcAYMu2OTZPn9FHysAIoXVQcMNMEJG8/SeNZPd+Iyq00mEisSN X-Received: by 2002:a17:906:58cf:b0:711:7acb:821f with SMTP id e15-20020a17090658cf00b007117acb821fmr5970236ejs.588.1655410194274; Thu, 16 Jun 2022 13:09:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410194; cv=none; d=google.com; s=arc-20160816; b=DuugxD77NITXsRPWLo9MU31WnLFuYIZDEcTqFQdvmOMDJ1T/BVvysP95IxKSfqOWon 1E+JJuN4rQzejJyLLjBiXRbqZePNkZhiux8OOH/CGfIlRZpRxmRqrjfP+wbORya3FyxC X7lSiuPucWhDY3h/KiRI6EJqUAFa0rrbRH4j2rC/e/6J9qKSCwg6t7qWT7mSXIFq4ePM JQAyDTTsH4HfFt3ONWm1moIp6QVOJhPk84z72MmWT0jU7/WylphiH/WjGY5sHSrnaf75 smEOt9vKVu8p1661sKNpy+mJebYGctBBGS+Zb3r9bgNvKaiFpv5hPatrAtN0fWBV+E5n CdAg== 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=9bNbL0ClOLHPYiyg4D7i0wwreP8hcyRBwND0deSW368=; b=pP6rMgV2V7h4hUtCtMWGG5ahERh/vCVS15v2YVK3sE5bjwM53oVXfepRa77CkbeADD AcIC0H7a9RLwsqUxRmsmXHgk7DVXQVSfkLa6QVK1WRWE01p1Zt5tTyG+2u9XC85K51bU y0uo9mOf1oi/5f36TLsaCdVhZhU/17fR7KG2AEaj8DeCvvxKSB2J0qm36IVTK1SH8aQ/ Z4YR4mh6YLZs9FSijxvRbAheab7QD75Ql+516PEDSXP5yq/BlH1x7+3HYi7Ekutm3mL0 mRAToFSuTuy1i45wlayjOnTXHKyDtIpTZVltUp7ujzhTkOODznQ6EAYO9VqiGN5/nrjy a/gA== 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 h14-20020aa7cdce000000b0042e17781f86si3052998edw.268.2022.06.16.13.09.53; Thu, 16 Jun 2022 13:09: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 5D3BB68B8DA; Thu, 16 Jun 2022 23:09: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 7F30968B834 for ; Thu, 16 Jun 2022 23:09:36 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 20F72240175 for ; Thu, 16 Jun 2022 22:09:36 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id KC3h4zrmI5kr for ; Thu, 16 Jun 2022 22:09:35 +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 7B2B32400F5 for ; Thu, 16 Jun 2022 22:09:35 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 1961E3A2216; Thu, 16 Jun 2022 22:03:31 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:32 +0200 Message-Id: <20220616195534.5278-33-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 33/35] 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: RQ1CIcteSe/o 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 3dca1c4bd3..1c80c54baf 100755 --- a/configure +++ b/configure @@ -3808,7 +3808,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 f57028b9b7..aea7335c8f 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -161,9 +161,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. @@ -593,9 +591,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); @@ -3660,7 +3656,6 @@ static int check_keyboard_interaction(int64_t cur_time) return 0; } -#if HAVE_THREADS static void *input_thread(void *arg) { InputFile *f = arg; @@ -3778,7 +3773,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) { @@ -3801,10 +3795,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); } @@ -3944,15 +3936,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 @@ -4194,11 +4182,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; @@ -4365,10 +4351,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(); @@ -4393,9 +4377,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++) { @@ -4449,9 +4431,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 91f5b4024b..9baa701c67 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -426,13 +426,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 5ef4115cdb..7f46238534 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -1328,9 +1328,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 Thu Jun 16 19:55: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: 36272 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1128570pzb; Thu, 16 Jun 2022 13:06:12 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vnTNNDyCvPzSuzy0ueGsE1+wv5D9I+BjPEanRTtPdAEoSsf/bNKPkDoDSlrEPGLM9KzDxj X-Received: by 2002:aa7:c7c4:0:b0:431:75d6:6b3 with SMTP id o4-20020aa7c7c4000000b0043175d606b3mr8457413eds.280.1655409972785; Thu, 16 Jun 2022 13:06:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409972; cv=none; d=google.com; s=arc-20160816; b=xZEU192W0Nb+5fxNlMsN4d2XOsyJa3/VAt9frObe5Ja4N59thiGnrH9IwK4yr0gY/T Y5YkiNap+25R1ZGVw7Vykej/hI3+RQG4RxtVdMaOPxjMT4yip+9ND+/5AjI8wWQr4YI0 hHxQwbF3smjyvJO120Je2MiBeqxIv82Qp/UUSF/TRaL6IQ99vw/7Q4Si2f3Gdbw/5OYU H9S0/YUU5AMgObuyrF0WRqRhaOeb0jMOs1w23e1vGve9eDUWBIHoG2ZR+TOffRDukWIZ 94LNCLZN4m3Y6N1n0VOtkn3RctxjbzeKAa3xJkd7kqXq8sXqSU+6cRw2juZI+LwV3w28 hLvA== 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=MOdu4LTbO9tr+RR5JVL9Yw4ydnk3AElJ8yhsSDMebA8=; b=IxCf1sAuzo2ueGM33gyVrhW/WfQT5kPr5eXxI74T0Rsx1Zx/5bfyKqXxrRN5w2lKLq w92sN88D5m0BsPG/ZBQIwbuXpbdt4Zk6e9L1dqmSuOK3m3I3DhxbGI/8f3OOj3e6vbiI mg9Fd7hKNZHkFxe2h9CWXUDPWp3feJ3Q3c6QLFpEArDh0pLae6JGzbN7CUNyURQPSNwI FlSUm8TXgA3egvAUqYnc3Wp1WZBKV+PHNe1FYHBWIJ4XR4XOi7wvrsVX/T+qXH7Uairu Ox4KacMtrG36q77EWPrNpIedXuDbLm8dyDuvmF34ahO2QaIB+hZ9sUM31+y506GFsa4y fTdA== 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 z13-20020a1709063acd00b00707dcb33dc8si725590ejd.402.2022.06.16.13.06.02; Thu, 16 Jun 2022 13:06: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 D724868B8A0; Thu, 16 Jun 2022 23:03: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 40EDF68B82F for ; Thu, 16 Jun 2022 23:03:45 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id E27872404FE for ; Thu, 16 Jun 2022 22:03:44 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id HHs-9taLxv89 for ; Thu, 16 Jun 2022 22:03: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 784E6240691 for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 1E0133A222E; Thu, 16 Jun 2022 22:03:31 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:33 +0200 Message-Id: <20220616195534.5278-34-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 34/35] 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: JrCc6oqppEpZ It is similar to AVThreadMessageQueue, but supports multiple streams, each with its own EOF state. --- fftools/Makefile | 1 + fftools/thread_queue.c | 245 +++++++++++++++++++++++++++++++++++++++++ fftools/thread_queue.h | 81 ++++++++++++++ 3 files changed, 327 insertions(+) create mode 100644 fftools/thread_queue.c create mode 100644 fftools/thread_queue.h diff --git a/fftools/Makefile b/fftools/Makefile index bc57ebe748..6285e6eacb 100644 --- a/fftools/Makefile +++ b/fftools/Makefile @@ -16,6 +16,7 @@ OBJS-ffmpeg += \ fftools/ffmpeg_opt.o \ fftools/objpool.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..a1ab4ce92e --- /dev/null +++ b/fftools/thread_queue.c @@ -0,0 +1,245 @@ +/* + * 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/intreadwrite.h" +#include "libavutil/mem.h" +#include "libavutil/thread.h" + +#include "objpool.h" +#include "thread_queue.h" + +enum { + FINISHED_SEND = (1 << 0), + FINISHED_RECV = (1 << 1), +}; + +typedef struct FifoElem { + void *obj; + unsigned int stream_idx; +} FifoElem; + +struct ThreadQueue { + int *finished; + unsigned int nb_streams; + + AVFifo *fifo; + + ObjPool *obj_pool; + void (*obj_move)(void *dst, void *src); + + pthread_mutex_t lock; + pthread_cond_t cond; +}; + +void tq_free(ThreadQueue **ptq) +{ + ThreadQueue *tq = *ptq; + + if (!tq) + return; + + if (tq->fifo) { + FifoElem elem; + while (av_fifo_read(tq->fifo, &elem, 1) >= 0) + objpool_release(tq->obj_pool, &elem.obj); + } + av_fifo_freep2(&tq->fifo); + + objpool_free(&tq->obj_pool); + + av_freep(&tq->finished); + + pthread_cond_destroy(&tq->cond); + pthread_mutex_destroy(&tq->lock); + + av_freep(ptq); +} + +ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size, + ObjPool *obj_pool, void (*obj_move)(void *dst, void *src)) +{ + ThreadQueue *tq; + int ret; + + tq = av_mallocz(sizeof(*tq)); + if (!tq) + return NULL; + + 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->finished = av_calloc(nb_streams, sizeof(*tq->finished)); + if (!tq->finished) + goto fail; + tq->nb_streams = nb_streams; + + tq->fifo = av_fifo_alloc2(queue_size, sizeof(FifoElem), 0); + if (!tq->fifo) + goto fail; + + tq->obj_pool = obj_pool; + tq->obj_move = obj_move; + + return tq; +fail: + tq_free(&tq); + return NULL; +} + +int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data) +{ + int *finished; + int ret; + + av_assert0(stream_idx < tq->nb_streams); + finished = &tq->finished[stream_idx]; + + pthread_mutex_lock(&tq->lock); + + if (*finished & FINISHED_SEND) { + ret = AVERROR(EINVAL); + goto finish; + } + + while (!(*finished & FINISHED_RECV) && !av_fifo_can_write(tq->fifo)) + pthread_cond_wait(&tq->cond, &tq->lock); + + if (*finished & FINISHED_RECV) { + ret = AVERROR_EOF; + *finished |= FINISHED_SEND; + } else { + FifoElem elem = { .stream_idx = stream_idx }; + + ret = objpool_get(tq->obj_pool, &elem.obj); + if (ret < 0) + goto finish; + + tq->obj_move(elem.obj, data); + + ret = av_fifo_write(tq->fifo, &elem, 1); + av_assert0(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) +{ + FifoElem elem; + unsigned int nb_finished = 0; + + if (av_fifo_read(tq->fifo, &elem, 1) >= 0) { + tq->obj_move(data, elem.obj); + objpool_release(tq->obj_pool, &elem.obj); + *stream_idx = elem.stream_idx; + return 0; + } + + for (unsigned int i = 0; i < tq->nb_streams; i++) { + if (!(tq->finished[i] & FINISHED_SEND)) + continue; + + /* return EOF to the consumer at most once for each stream */ + if (!(tq->finished[i] & FINISHED_RECV)) { + tq->finished[i] |= 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; + * next time the consumer thread tries to read this stream it will get + * an EOF and recv-finished flag will be set */ + tq->finished[stream_idx] |= 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 recv-finished; + * next time the producer thread tries to send for this stream, it will + * get an EOF and send-finished flag will be set */ + tq->finished[stream_idx] |= 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..0cc8c71ebd --- /dev/null +++ b/fftools/thread_queue.h @@ -0,0 +1,81 @@ +/* + * 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 + +#include "objpool.h" + +typedef struct ThreadQueue ThreadQueue; + +/** + * Allocate a queue for sending data between threads. + * + * @param nb_streams number of streams for which a distinct EOF state is + * maintained + * @param queue_size number of items that can be stored in the queue without + * blocking + * @param obj_pool object pool that will be used to allocate items stored in the + * queue; the pool becomes owned by the queue + * @param callback that moves the contents between two data pointers + */ +ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size, + ObjPool *obj_pool, void (*obj_move)(void *dst, void *src)); +void tq_free(ThreadQueue **tq); + +/** + * Send an item for the given stream to the queue. + * + * @param data the item to send, its contents will be moved using the callback + * provided to tq_alloc(); on failure the item will be left + * untouched + * @return + * - 0 the item was successfully sent + * - AVERROR(ENOMEM) could not allocate an item for writing to the FIFO + * - AVERROR(EINVAL) the sending side has previously been marked as finished + * - AVERROR_EOF the receiving side has marked the given stream as finished + */ +int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data); +/** + * Mark the given stream finished from the sending side. + */ +void tq_send_finish(ThreadQueue *tq, unsigned int stream_idx); + +/** + * Read the next item from the queue. + * + * @param stream_idx the index of the stream that was processed or -1 will be + * written here + * @param data the data item will be written here on success using the + * callback provided to tq_alloc() + * @return + * - 0 a data item was successfully read; *stream_idx contains a non-negative + * stream index + * - AVERROR_EOF When *stream_idx is non-negative, this signals that the sending + * side has marked the given stream as finished. This will happen at most once + * for each stream. When *stream_idx is -1, all streams are done. + */ +int tq_receive(ThreadQueue *tq, int *stream_idx, void *data); +/** + * Mark the given stream finished from the receiving side. + */ +void tq_receive_finish(ThreadQueue *tq, unsigned int stream_idx); + +#endif // FFTOOLS_THREAD_QUEUE_H From patchwork Thu Jun 16 19:55: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: 36285 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1129517pzb; Thu, 16 Jun 2022 13:08:09 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ssUzG09M3vJyxk0hwrH/I82A37yxs1/6vdpf8mNP//BWK+0gt2s35YHqHr/2Ji+h/GfZfu X-Received: by 2002:a17:907:3f0a:b0:711:f0e2:ad67 with SMTP id hq10-20020a1709073f0a00b00711f0e2ad67mr5945738ejc.277.1655410088806; Thu, 16 Jun 2022 13:08:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410088; cv=none; d=google.com; s=arc-20160816; b=y9l8M0IJloziJ6I80eVZvlFWTsAeQNo1aAWhbgewFo0x2ADHOCamaoJIqjtqTyNcQw 7dxCYtQeBs/5k0I82UUacXUKbY2B6NPuR8UrE2KRxJCiT5DLFwOHWm5BsvH5U/zqwH1i +iPYybSiH2aJvtyr7GzngORpEoE8dGfpez5OS0b1wd+swWbjhqFtGPusFMUSj6KPW2H4 0FRPbwxWw8010mBrSOgPVtqzi5ByBS3th/L5DPrrC6mSutA3Vu6QaPPHCjX/5jM5Mk6p HdVnJAfGW8o5dek2p8sFORr/ld7TqTpqCo/0uHKp4r7Gln2RqiTIoUkxhn/HLJgPQ1F4 rAlg== 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=YtRks+bJu+X7LrRn4/VFHL+bSAlB2Rc7sbmM5M1JwNE=; b=avUAPJjrRNLsrnCPDWK4y3BDoA7QBBsXP8S83aKowQQubrDp3sLvFT/epnwTppSjh/ e9gK1A4o4wbaq7iUdMhyCV1KKv0/azusM/AsTVP47effQAcb8jGFDLPIEMQFxpMKwYr8 4o7s2rqrheWbaJm+EyVJZ/A5pxr5PhUQImr48HSEWTkEwQQBHDLzisg0BwfB7FaTO/oY jvseCs31ffsLNHK874YS1tBa29Jb0ejvQBaLFBm2PEzxu5rtczOdmIVPUa4rdZ8/5gbO jKtQ4YCrrcrUNPR0DwBbhjPZmc1DFSe/RmWQrWyhFRXNjRBtFBWjcs+Hxr/tj2palGzt mUOQ== 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 hb44-20020a170907162c00b006feb0e0856dsi3049946ejc.653.2022.06.16.13.08.08; Thu, 16 Jun 2022 13:08:08 -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 0822E68B8E0; Thu, 16 Jun 2022 23:04:08 +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 6FF2A68B884 for ; Thu, 16 Jun 2022 23:03:49 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 19373240175 for ; Thu, 16 Jun 2022 22:03:49 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id JU8ovR3avZnU for ; Thu, 16 Jun 2022 22:03: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 77FAE240690 for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 228903A2237; Thu, 16 Jun 2022 22:03:31 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:34 +0200 Message-Id: <20220616195534.5278-35-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 35/35] 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: BMwxAgA+EubG --- doc/ffmpeg.texi | 11 +- fftools/ffmpeg.c | 18 ++- fftools/ffmpeg.h | 7 +- fftools/ffmpeg_mux.c | 299 +++++++++++++++++++++++++++++++------------ fftools/ffmpeg_opt.c | 4 +- 5 files changed, 236 insertions(+), 103 deletions(-) diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi index 7542832eb3..e4e2d6ddac 100644 --- a/doc/ffmpeg.texi +++ b/doc/ffmpeg.texi @@ -1900,13 +1900,16 @@ to the @option{-ss} option is considered an actual timestamp, and is not offset by the start time of the file. This matters only for files which do not start from timestamp 0, such as transport streams. -@item -thread_queue_size @var{size} (@emph{input}) -This option sets the maximum number of queued packets when reading from the -file or device. With low latency / high rate live streams, packets may be -discarded if they are not read in a timely manner; setting this value can +@item -thread_queue_size @var{size} (@emph{input/output}) +For input, this option sets the maximum number of queued packets when reading +from the file or device. With low latency / high rate live streams, packets may +be discarded if they are not read in a timely manner; setting this value can force ffmpeg to use a separate input thread and read packets as soon as they arrive. By default ffmpeg only does this if multiple inputs are specified. +For output, this option specified the maximum number of packets that may be +queued to each muxing thread. + @item -sdp_file @var{file} (@emph{global}) Print sdp information for an output stream to @var{file}. This allows dumping sdp information when at least one output isn't an diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index aea7335c8f..2a7ff16b74 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -746,9 +746,6 @@ static void output_packet(OutputFile *of, AVPacket *pkt, goto mux_fail; } - if (eof) - ost->finished |= MUXER_FINISHED; - return; mux_fail: @@ -1521,7 +1518,7 @@ static void print_final_stats(int64_t total_size) enum AVMediaType type = ost->enc_ctx->codec_type; total_size += ost->data_size; - total_packets += ost->packets_written; + total_packets += atomic_load(&ost->packets_written); av_log(NULL, AV_LOG_VERBOSE, " Output stream #%d:%d (%s): ", i, j, av_get_media_type_string(type)); @@ -1534,7 +1531,7 @@ static void print_final_stats(int64_t total_size) } av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ", - ost->packets_written, ost->data_size); + atomic_load(&ost->packets_written), ost->data_size); av_log(NULL, AV_LOG_VERBOSE, "\n"); } @@ -1603,7 +1600,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->packets_written; + frame_number = atomic_load(&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); @@ -3480,9 +3477,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; @@ -4402,9 +4398,11 @@ static int transcode(void) /* close each encoder */ for (i = 0; i < nb_output_streams; i++) { + uint64_t packets_written; ost = output_streams[i]; - total_packets_written += ost->packets_written; - if (!ost->packets_written && (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM)) { + packets_written = atomic_load(&ost->packets_written); + total_packets_written += packets_written; + if (!packets_written && (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM)) { av_log(NULL, AV_LOG_FATAL, "Empty output on stream %d.\n", i); exit_program(1); } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 9baa701c67..8940f719b0 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -21,6 +21,7 @@ #include "config.h" +#include #include #include #include @@ -555,7 +556,7 @@ typedef struct OutputStream { // combined size of all the packets written uint64_t data_size; // number of packets send to the muxer - uint64_t packets_written; + atomic_uint_least64_t packets_written; // number of frames/samples sent to the encoder uint64_t frames_encoded; uint64_t samples_encoded; @@ -697,14 +698,14 @@ int hw_device_setup_for_filter(FilterGraph *fg); int hwaccel_decode_init(AVCodecContext *avctx); int of_muxer_init(OutputFile *of, AVFormatContext *fc, - AVDictionary *opts, int64_t limit_filesize); + AVDictionary *opts, int64_t limit_filesize, + int thread_queue_size); /* 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); int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost); -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 2abadd3f9b..67b875d41d 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -16,17 +16,21 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include #include #include #include "ffmpeg.h" +#include "objpool.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" @@ -51,13 +55,18 @@ typedef struct MuxStream { struct Muxer { AVFormatContext *fc; + pthread_t thread; + ThreadQueue *tq; + MuxStream *streams; AVDictionary *opts; + int thread_queue_size; + /* filesize limit expressed in bytes */ int64_t limit_filesize; - int64_t final_filesize; + atomic_int_least64_t last_filesize; int header_written; AVPacket *sq_pkt; @@ -65,15 +74,6 @@ struct Muxer { static int want_sdp = 1; -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 int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) { MuxStream *ms = &of->mux->streams[ost->index]; @@ -116,13 +116,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 && ost->vsync_method == VSYNC_DROP) || (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0)) pkt->pts = pkt->dts = AV_NOPTS_VALUE; @@ -175,7 +194,7 @@ static int write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) ms->last_mux_dts = pkt->dts; ost->data_size += pkt->size; - ost->packets_written++; + atomic_fetch_add(&ost->packets_written, 1); pkt->stream_index = ost->index; @@ -193,66 +212,81 @@ static int write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) 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); return ret; } return 0; } -static int submit_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) +static int sync_queue_process(OutputFile *of, OutputStream *ost, AVPacket *pkt) { if (ost->sq_idx_mux >= 0) { int ret = sq_send(of->sq_mux, ost->sq_idx_mux, SQPKT(pkt)); - if (ret < 0) { - if (pkt) - av_packet_unref(pkt); - if (ret == AVERROR_EOF) { - ost->finished |= MUXER_FINISHED; - return 0; - } else - return ret; - } + if (ret < 0) + return ret; while (1) { ret = sq_receive(of->sq_mux, -1, SQPKT(of->mux->sq_pkt)); - if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) - return 0; - else if (ret < 0) - return ret; + if (ret < 0) + return (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) ? 0 : ret; ret = write_packet(of, output_streams[of->ost_index + ret], of->mux->sq_pkt); if (ret < 0) return ret; } - } else { - if (pkt) - return write_packet(of, ost, pkt); - - ost->finished |= MUXER_FINISHED; - } + } else if (pkt) + return write_packet(of, ost, pkt); return 0; } -int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) +static void *muxer_thread(void *arg) { - int ret; + OutputFile *of = arg; + Muxer *mux = of->mux; + AVPacket *pkt = NULL; + int ret = 0; + + pkt = av_packet_alloc(); + if (!pkt) { + ret = AVERROR(ENOMEM); + goto finish; + } - if (of->mux->header_written) { - return submit_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; + while (1) { + OutputStream *ost; + int stream_idx; + + 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); + ret = 0; + break; + } + + ost = output_streams[of->ost_index + stream_idx]; + ret = sync_queue_process(of, ost, ret < 0 ? NULL : pkt); + av_packet_unref(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; } } - return 0; +finish: + av_packet_free(&pkt); + + for (unsigned int i = 0; i < mux->fc->nb_streams; i++) + tq_receive_finish(mux->tq, i); + + av_log(NULL, AV_LOG_DEBUG, "Terminating muxer thread %d\n", of->index); + + return (void*)(intptr_t)ret; } static int print_sdp(void) @@ -303,11 +337,125 @@ static int print_sdp(void) av_freep(&sdp_filename); } + // SDP successfully written, allow muxer threads to start + ret = 1; + fail: av_freep(&avc); return ret; } +static int submit_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) +{ + Muxer *mux = of->mux; + int ret = 0; + + if (!pkt || ost->finished & MUXER_FINISHED) + goto finish; + + ret = tq_send(mux->tq, ost->index, pkt); + if (ret < 0) + goto finish; + + return 0; + +finish: + if (pkt) + av_packet_unref(pkt); + + ost->finished |= MUXER_FINISHED; + tq_send_finish(mux->tq, ost->index); + return ret == AVERROR_EOF ? 0 : ret; +} + +int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) +{ + int ret; + + if (of->mux->tq) { + return submit_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; +} + +static int thread_stop(OutputFile *of) +{ + Muxer *mux = of->mux; + void *ret; + + if (!mux || !mux->tq) + return 0; + + for (unsigned int i = 0; i < mux->fc->nb_streams; i++) + tq_send_finish(mux->tq, i); + + pthread_join(mux->thread, &ret); + + tq_free(&mux->tq); + + return (int)(intptr_t)ret; +} + +static void pkt_move(void *dst, void *src) +{ + av_packet_move_ref(dst, src); +} + +static int thread_start(OutputFile *of) +{ + Muxer *mux = of->mux; + AVFormatContext *fc = mux->fc; + ObjPool *op; + int ret; + + op = objpool_alloc_packets(); + if (!op) + return AVERROR(ENOMEM); + + mux->tq = tq_alloc(fc->nb_streams, mux->thread_queue_size, op, pkt_move); + if (!mux->tq) { + objpool_free(&op); + return AVERROR(ENOMEM); + } + + ret = pthread_create(&mux->thread, NULL, muxer_thread, (void*)of); + if (ret) { + tq_free(&mux->tq); + return AVERROR(ret); + } + + /* flush the muxing queues */ + for (int i = 0; i < fc->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(ms->muxing_queue)) + ost->mux_timebase = ost->st->time_base; + + while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) { + ret = submit_packet(of, ost, pkt); + if (pkt) { + ms->muxing_queue_data_size -= pkt->size; + av_packet_free(&pkt); + } + if (ret < 0) + return ret; + } + } + + return 0; +} + /* open the muxer when all the streams are initialized */ int of_check_init(OutputFile *of) { @@ -339,28 +487,19 @@ int of_check_init(OutputFile *of) if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Error writing the SDP.\n"); return ret; - } - } - - /* flush the muxing queues */ - for (i = 0; i < fc->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(ms->muxing_queue)) - ost->mux_timebase = ost->st->time_base; - - while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) { - ret = submit_packet(of, ost, pkt); - if (pkt) { - ms->muxing_queue_data_size -= pkt->size; - av_packet_free(&pkt); + } else if (ret == 1) { + /* SDP is written only after all the muxers are ready, so now we + * start ALL the threads */ + for (i = 0; i < nb_output_files; i++) { + ret = thread_start(output_files[i]); + if (ret < 0) + return ret; } - if (ret < 0) - return ret; } + } else { + ret = thread_start(of); + if (ret < 0) + return ret; } return 0; @@ -371,7 +510,7 @@ int of_write_trailer(OutputFile *of) AVFormatContext *fc = of->mux->fc; int ret; - if (!of->mux->header_written) { + if (!of->mux->tq) { 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", @@ -379,13 +518,17 @@ int of_write_trailer(OutputFile *of) return AVERROR(EINVAL); } + ret = thread_stop(of); + if (ret < 0) + main_return_code = ret; + ret = av_write_trailer(fc); if (ret < 0) { 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); + of->mux->last_filesize = filesize(fc->pb); if (!(of->format->flags & AVFMT_NOFILE)) { ret = avio_closep(&fc->pb); @@ -448,6 +591,8 @@ void of_close(OutputFile **pof) if (!of) return; + thread_stop(of); + sq_free(&of->sq_encode); sq_free(&of->sq_mux); @@ -457,7 +602,8 @@ void of_close(OutputFile **pof) } int of_muxer_init(OutputFile *of, AVFormatContext *fc, - AVDictionary *opts, int64_t limit_filesize) + AVDictionary *opts, int64_t limit_filesize, + int thread_queue_size) { Muxer *mux = av_mallocz(sizeof(*mux)); int ret = 0; @@ -487,6 +633,7 @@ int of_muxer_init(OutputFile *of, AVFormatContext *fc, ms->last_mux_dts = AV_NOPTS_VALUE; } + mux->thread_queue_size = thread_queue_size > 0 ? thread_queue_size : 8; mux->limit_filesize = limit_filesize; mux->opts = opts; @@ -515,25 +662,9 @@ fail: return ret; } -int of_finished(OutputFile *of) -{ - return of_filesize(of) >= of->mux->limit_filesize; -} - int64_t of_filesize(OutputFile *of) { - AVIOContext *pb = of->mux->fc->pb; - int64_t ret = -1; - - 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); - } - - return ret; + return atomic_load(&of->mux->last_filesize); } AVChapter * const * diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 7f46238534..807c0263b9 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -3055,7 +3055,7 @@ loop_end: of->nb_streams = oc->nb_streams; of->url = filename; - err = of_muxer_init(of, oc, format_opts, o->limit_filesize); + err = of_muxer_init(of, oc, format_opts, o->limit_filesize, o->thread_queue_size); if (err < 0) { av_log(NULL, AV_LOG_FATAL, "Error initializing internal muxing state\n"); exit_program(1); @@ -3841,7 +3841,7 @@ const OptionDef options[] = { { "disposition", OPT_STRING | HAS_ARG | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(disposition) }, "disposition", "" }, - { "thread_queue_size", HAS_ARG | OPT_INT | OPT_OFFSET | OPT_EXPERT | OPT_INPUT, + { "thread_queue_size", HAS_ARG | OPT_INT | OPT_OFFSET | OPT_EXPERT | OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(thread_queue_size) }, "set the maximum number of queued packets from the demuxer" }, { "find_stream_info", OPT_BOOL | OPT_PERFILE | OPT_INPUT | OPT_EXPERT, { &find_stream_info },