From patchwork Thu Aug 25 08:59:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 37472 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:139a:b0:8f:1db5:eae2 with SMTP id w26csp201349pzh; Thu, 25 Aug 2022 02:02:16 -0700 (PDT) X-Google-Smtp-Source: AA6agR4bNu6CmR15zgn2NlJRkE5/HPod0h3TewYCWmTRyBcHQ0fpbddRM5OfSGUf99hrW3z0WvW3 X-Received: by 2002:a17:907:6e1c:b0:73d:7806:3c5e with SMTP id sd28-20020a1709076e1c00b0073d78063c5emr1797388ejc.36.1661418136369; Thu, 25 Aug 2022 02:02:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661418136; cv=none; d=google.com; s=arc-20160816; b=BDfALmL91XVz9WxouyF/C7rqlmlJUa8q3VN8++mLntzGPIVz/8cTBqgEPp24ez9e42 tgWBHKaXFGlkr4GLgGISXLLctap49VlOXO6V261nyREspmZDHJojv+4vZBmb9QyAS9SO t1Wi/D0ugFpcX+dcWpttQ56Mb5k2wGMKZQ4KFymztzNsC0BGV6nnGLQU9UTFdImPkIJT HiNjoSuyijsmjm0uScinmDB8zU4EEAghiw+keUT4tbsMgBrz+IT20EcMXsyw90TMFO9T EvUSrqoo4BqV6biTZci8a6Rxax0aZwnmF5amDW0efflq6aeCZWPdNIep5Rd2++8aju3h Wz7g== 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=e1srGVlOFyXzoyZ0e79Fphxe0io4D/oRvmF1a+sYeJk=; b=cOTTMlc8DO7hvH5nCkA7Rtp6r3kRfYVCajl75yamxHmEqY4xlZjq9PUawYDQ3Yjl8+ baC5VCF+AQu51tzcSKmW+BaGmAkIsU2HyYQ7vH30syJCDpw6vReEBy/keQf5smGSFCDs KHggdjyjVMPLMmuIYe/tZCH6c+LQXgn4wWyJ3VuEZeYFULpVyrtSCDYDXWfYYEPsD5Tk rvRIztAHHAtOTq6qbBM52RDgyTwED6l8pBX/NlGiOjvSR3t+vgzL2aA7fwkApbQWKeaM 44qoegjXMDC60Laj5/JlLTA+hQe7l6CRGnUr8AUi5WLCd2kEm3smOTTYBX73ZnPqjBZM SNJw== 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 hg6-20020a1709072cc600b0072a6c18f1fasi3354135ejc.639.2022.08.25.02.02.15; Thu, 25 Aug 2022 02:02: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 1B33B68B6F0; Thu, 25 Aug 2022 12:01:24 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C187D68B9F2 for ; Thu, 25 Aug 2022 12:01:17 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 2EDD0240D1A for ; Thu, 25 Aug 2022 11:01:17 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id JEVitLkteA0B for ; Thu, 25 Aug 2022 11:01:12 +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 164CA240D1B for ; Thu, 25 Aug 2022 11:01:11 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 8C7E43A159F; Thu, 25 Aug 2022 11:01:04 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Aug 2022 10:59:56 +0200 Message-Id: <20220825085956.4790-6-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220825085956.4790-1-anton@khirnov.net> References: <20220825085956.4790-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 6/6] fftools/ffmpeg: use a separate counter for encoded packet data 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: DGDwqhXJc+qO update_video_stats() currently uses OutputStream.data_size to print the total size of the encoded stream so far and the average bitrate. However, that field is updated in the muxer thread, right before the packet is sent to the muxer. Not only is this racy, but the numbers may not match even if muxing was in the main thread due to bitstream filters, filesize limiting, etc. Introduce a new counter, data_size_enc, for total size of the packets received from the encoder and use that in update_video_stats(). Rename data_size to data_size_mux to indicate its semantics more clearly. No synchronization is needed for data_size_mux, because it is only read in the main thread in print_final_stats(), which runs after the muxer threads are terminated. --- fftools/ffmpeg.c | 22 +++++++++++++--------- fftools/ffmpeg.h | 6 ++++-- fftools/ffmpeg_mux.c | 2 +- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 4a825aa6b5..0bc97811b3 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -901,9 +901,9 @@ static void update_video_stats(OutputStream *ost, const AVPacket *pkt, int write ti1 = 0.01; bitrate = (pkt->size * 8) / av_q2d(enc->time_base) / 1000.0; - avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0; + avg_bitrate = (double)(ost->data_size_enc * 8) / ti1 / 1000.0; fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ", - (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate); + (double)ost->data_size_enc / 1024, ti1, bitrate, avg_bitrate); fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type)); } @@ -979,6 +979,8 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); } + ost->data_size_enc += pkt->size; + if (enc->codec_type == AVMEDIA_TYPE_VIDEO) update_video_stats(ost, pkt, !!vstats_filename); @@ -1454,14 +1456,16 @@ static void print_final_stats(int64_t total_size) for (i = 0; i < nb_output_streams; i++) { OutputStream *ost = output_streams[i]; AVCodecParameters *par = ost->st->codecpar; + const uint64_t s = ost->data_size_mux; + switch (par->codec_type) { - case AVMEDIA_TYPE_VIDEO: video_size += ost->data_size; break; - case AVMEDIA_TYPE_AUDIO: audio_size += ost->data_size; break; - case AVMEDIA_TYPE_SUBTITLE: subtitle_size += ost->data_size; break; - default: other_size += ost->data_size; break; + case AVMEDIA_TYPE_VIDEO: video_size += s; break; + case AVMEDIA_TYPE_AUDIO: audio_size += s; break; + case AVMEDIA_TYPE_SUBTITLE: subtitle_size += s; break; + default: other_size += s; break; } extra_size += par->extradata_size; - data_size += ost->data_size; + data_size += s; if (ost->enc_ctx && (ost->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) != AV_CODEC_FLAG_PASS1) @@ -1529,7 +1533,7 @@ static void print_final_stats(int64_t total_size) OutputStream *ost = output_streams[of->ost_index + j]; enum AVMediaType type = ost->st->codecpar->codec_type; - total_size += ost->data_size; + total_size += ost->data_size_mux; total_packets += atomic_load(&ost->packets_written); av_log(NULL, AV_LOG_VERBOSE, " Output stream #%d:%d (%s): ", @@ -1543,7 +1547,7 @@ static void print_final_stats(int64_t total_size) } av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ", - atomic_load(&ost->packets_written), ost->data_size); + atomic_load(&ost->packets_written), ost->data_size_mux); av_log(NULL, AV_LOG_VERBOSE, "\n"); } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 481b836d30..672f91dc13 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -579,8 +579,10 @@ typedef struct OutputStream { int keep_pix_fmt; /* stats */ - // combined size of all the packets written - uint64_t data_size; + // combined size of all the packets sent to the muxer + uint64_t data_size_mux; + // combined size of all the packets received from the encoder + uint64_t data_size_enc; // number of packets send to the muxer atomic_uint_least64_t packets_written; // number of frames/samples sent to the encoder diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index b424ef0021..b781e1f5a6 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -156,7 +156,7 @@ static int write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) } ms->last_mux_dts = pkt->dts; - ost->data_size += pkt->size; + ost->data_size_mux += pkt->size; atomic_fetch_add(&ost->packets_written, 1); pkt->stream_index = ost->index;