From patchwork Thu Oct 22 15:17:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Wang X-Patchwork-Id: 23153 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 41EB844B9FF for ; Thu, 22 Oct 2020 18:18:08 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 076EC68A8E9; Thu, 22 Oct 2020 18:18:08 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4A45368A48B for ; Thu, 22 Oct 2020 18:18:01 +0300 (EEST) Received: by mail-pg1-f196.google.com with SMTP id j7so1114241pgk.5 for ; Thu, 22 Oct 2020 08:18:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=+sMIYVYmvqOwHqD0uKSh0yvk7+tWX+JmdeVNbzz1KxQ=; b=sMz/e+39+H1uW8DXHa+IkxOsCk9CvD0PZ8ARqUKpfLs7lcFKc4JQjLV9Qeodhe7NsN xs0pxI4qwzrNLMDVkOMNMPi8dsEZ1dfZRLKLv+ZXk3GB/I8FpzjqXtON0F8qjj+4Jfob P/YciykaX74MjMI9ug5YCcQykfVZJniqlsTZUsbETPSmJfU8RhKgnAM1ta4i7BZXIhX3 6PPLVNIxOP3q3OM3wfNMcfbZSymY9IdMUc5rdf3Py/ZezBNdwAb5sN2WeyLCWK9o2ozS vh20b0zrK7CjkxwlJUs1VLBSh6ldA4WON3L66J+AdU4dFe74h8kEBFgz2r5qHQSyICbW 2DfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=+sMIYVYmvqOwHqD0uKSh0yvk7+tWX+JmdeVNbzz1KxQ=; b=mN68BtUiz8d6OQkyV3lwBW4rQDVELJIigTT7yeiXSeKZ+3sAaR8y5xcv/BlcGhqsTX +OSHlyzDj/qDdf9DDmUG4Z5xzb+eGvCdFDHbSQSan2ZXL7BnbiTzdfIVyg2yByRTWupe CwwgiIR6v8pfgKwUaTf7r//i9usEjkRBFKrgGmaCyMY9B2jPjo47u0IDtR/R/rE/XEAl M6adNupCiUQ63U0QIHjOSAWCQBafQdHPwb97vHwiHs/lI9S6SIS6tGjMUVaA1R5F7voc lC16QDLzSSFx7fCg4eMnt1EVU+X9a63Qc/wt1Maz+QC5hHncZJsYx3XnLG6IebuE7X4c 1Spg== X-Gm-Message-State: AOAM531tbse+KDsJuezPRwSvRPehPcSQLy7Rt4sLokjoW/F0i6uK/0Kv ISvFYTMyVxsUnQ8rJCyZiMdC8ghoe/Q= X-Google-Smtp-Source: ABdhPJzbxJwtCiiSvlH92WjJPFXrMLtmVqX6DfD2QS0iC85mpNwhyhPpzRqc1z1O+4UxGZ5lOAtFuw== X-Received: by 2002:a65:5785:: with SMTP id b5mr2510572pgr.44.1603379878860; Thu, 22 Oct 2020 08:17:58 -0700 (PDT) Received: from vpn2.localdomain ([161.117.202.209]) by smtp.gmail.com with ESMTPSA id mn11sm2370259pjb.19.2020.10.22.08.17.57 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Oct 2020 08:17:57 -0700 (PDT) From: lance.lmwang@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Thu, 22 Oct 2020 23:17:45 +0800 Message-Id: <1603379867-6325-1-git-send-email-lance.lmwang@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1603287614-11545-1-git-send-email-lance.lmwang@gmail.com> References: <1603287614-11545-1-git-send-email-lance.lmwang@gmail.com> Subject: [FFmpeg-devel] [PATCH v2 1/3] avformat/mpegtsenc: use total_size instead of avio_tell() X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Limin Wang MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Limin Wang avio_tell() fails to get the amount of data written so far when the underlying IO context is flushed to segments. Please test with below command: $ ./ffmpeg -f lavfi -i testsrc=duration=100:size=vga -c:v libx264 -x264opts \ nal-hrd=cbr:force-cfr=1 -b:v 500k -minrate 500k -maxrate 500k -bufsize 500k \ -f hls -hls_time 10 -hls_ts_options "muxrate=2000000" test%d.ts $ du -h test*.ts Before: 2.4M test00.ts 4.8M test11.ts 7.2M test22.ts 9.6M test33.ts 12M test44.ts 15M test55.ts 17M test66.ts 20M test77.ts 22M test88.ts 24M test99.ts After apply the patch: 2.4M test00.ts 2.4M test11.ts 2.4M test22.ts 2.4M test33.ts 2.4M test44.ts 2.4M test55.ts 2.4M test66.ts 2.4M test77.ts 2.4M test88.ts 2.4M test99.ts Signed-off-by: Limin Wang --- libavformat/mpegtsenc.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c index afdadda..5c97d63 100644 --- a/libavformat/mpegtsenc.c +++ b/libavformat/mpegtsenc.c @@ -84,6 +84,7 @@ typedef struct MpegTSWrite { int64_t next_pcr; int mux_rate; ///< set to 1 when VBR int pes_payload_size; + int64_t total_size; int transport_stream_id; int original_network_id; @@ -830,9 +831,9 @@ invalid: return 0; } -static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb) +static int64_t get_pcr(const MpegTSWrite *ts) { - return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) + + return av_rescale(ts->total_size + 11, 8 * PCR_TIME_BASE, ts->mux_rate) + ts->first_pcr; } @@ -840,13 +841,14 @@ static void write_packet(AVFormatContext *s, const uint8_t *packet) { MpegTSWrite *ts = s->priv_data; if (ts->m2ts_mode) { - int64_t pcr = get_pcr(s->priv_data, s->pb); + int64_t pcr = get_pcr(s->priv_data); uint32_t tp_extra_header = pcr % 0x3fffffff; tp_extra_header = AV_RB32(&tp_extra_header); avio_write(s->pb, (unsigned char *) &tp_extra_header, sizeof(tp_extra_header)); } avio_write(s->pb, packet, TS_PACKET_SIZE); + ts->total_size += TS_PACKET_SIZE; } static void section_write_packet(MpegTSSection *s, const uint8_t *packet) @@ -1228,7 +1230,7 @@ static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st) } /* PCR coded into 6 bytes */ - q += write_pcr_bits(q, get_pcr(ts, s->pb)); + q += write_pcr_bits(q, get_pcr(ts)); /* stuffing bytes */ memset(q, 0xFF, TS_PACKET_SIZE - (q - buf)); @@ -1315,7 +1317,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, while (payload_size > 0) { int64_t pcr = AV_NOPTS_VALUE; if (ts->mux_rate > 1) - pcr = get_pcr(ts, s->pb); + pcr = get_pcr(ts); else if (dts != AV_NOPTS_VALUE) pcr = (dts - delay) * 300; @@ -1326,7 +1328,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, write_pcr = 0; if (ts->mux_rate > 1) { /* Send PCR packets for all PCR streams if needed */ - pcr = get_pcr(ts, s->pb); + pcr = get_pcr(ts); if (pcr >= ts->next_pcr) { int64_t next_pcr = INT64_MAX; for (int i = 0; i < s->nb_streams; i++) { @@ -1340,7 +1342,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, ts_st2->last_pcr = FFMAX(pcr - ts_st2->pcr_period, ts_st2->last_pcr + ts_st2->pcr_period); if (st2 != st) { mpegts_insert_pcr_only(s, st2); - pcr = get_pcr(ts, s->pb); + pcr = get_pcr(ts); } else { write_pcr = 1; }