From patchwork Fri Apr 19 08:47:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Diego Felix de Souza via ffmpeg-devel X-Patchwork-Id: 12808 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 39894448074 for ; Fri, 19 Apr 2019 11:47:44 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 10D5268A6CE; Fri, 19 Apr 2019 11:47:44 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-40135.protonmail.ch (mail-40135.protonmail.ch [185.70.40.135]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 53FE6689A11 for ; Fri, 19 Apr 2019 11:47:37 +0300 (EEST) Date: Fri, 19 Apr 2019 08:47:34 +0000 To: FFmpeg development discussions and patches Message-ID: In-Reply-To: <54FngVg9fz3aNdpQwkm65W8zPLWU6TvxGVMANTuxYs1tr0PUVm6dZ23iFw7U0uIrjl-BCtDQPHMD8BRW60CY4HgpbvoXw3IGeiC_GSNZ1vM=@protonmail.com> References: <54FngVg9fz3aNdpQwkm65W8zPLWU6TvxGVMANTuxYs1tr0PUVm6dZ23iFw7U0uIrjl-BCtDQPHMD8BRW60CY4HgpbvoXw3IGeiC_GSNZ1vM=@protonmail.com> Feedback-ID: Mx8CaiV20jk_fqXDN0fFpg3vRaGkb9VCTrYRnZNHwEija3aOdqvFspzl6ODkmHrlSKJSx29p-LzkuvS_96L02A==:Ext:ProtonMail MIME-Version: 1.0 X-Spam-Status: No, score=-1.2 required=7.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail.protonmail.ch Subject: Re: [FFmpeg-devel] [PATCH] libavformat: fix copyts and muxrate in mpegts muxer 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: , X-Patchwork-Original-From: =?UTF-8?Q?Andreas_H=C3=A5kon?= via ffmpeg-devel From: Diego Felix de Souza via ffmpeg-devel Reply-To: FFmpeg development discussions and patches Cc: =?UTF-8?Q?Andreas_H=C3=A5kon?= Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐ On Thursday, 18 de April de 2019 11:01, Andreas Håkon via ffmpeg-devel wrote: > Hi, > > This patch resolves one very specific use case: > > - When you use the mpegts muxer; > - And use the global parameter “-copyts”; > - And use the parameter “-muxrate” for the mpegts muxer; > - And use too the parameter “-mpegts_copyts”. > > The problem is created because the member “first_pcr” of the MpegTSWrite struct isn’t initialized with a correct timestamp (so when copying the timestamps the initial value is 0). And in this case an infinite loop is created because the code never writes PES packets when the “mux_rate” isn’t VBR (equals to 1). See the block that creates the loop here (note the "continue" command at end): > https://github.com/FFmpeg/FFmpeg/blob/a0559fcd81f42f446c93357a943699f9d44eeb79/libavformat/mpegtsenc.c#L1211 > > So, this patch fixes the problem initializing the “first_pcr” with the first DTS value that comes when using the incoming timestamps. > > Regards. > A.H. > Hi, Here a new version of the patch. The "fist_pcr" value is now derived from DTS in a more consistent way. Regards, A.H. --- From c59569ca9426fef455edabfa648cb2ff678c1640 Mon Sep 17 00:00:00 2001 From: Andreas Hakon Date: Fri, 19 Apr 2019 09:32:33 +0100 Subject: [PATCH] libavformat: fix copyts and muxrate in mpegts muxer v2 When using "-copyts" and "-muxrate" with the mpegts muxer the muxing fails because the member "first_pcr" of the MpegTSWrite struct isn't initialized with a correct timestamp. The behaviour of the error is an infinite loop created in the function mpegts_write_pes() because the code never writes PES packets. This patch resolves the problem initializing the "first_pcr" with a value derived from the first DTS value seen. Signed-off-by: Andreas Hakon --- libavformat/mpegtsenc.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c index fc0ea22..858b0d7 100644 --- a/libavformat/mpegtsenc.c +++ b/libavformat/mpegtsenc.c @@ -971,6 +971,9 @@ static int mpegts_init(AVFormatContext *s) if (ts->copyts < 1) ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE); + else + ts->first_pcr = AV_NOPTS_VALUE; + } else { /* Arbitrary values, PAT/PMT will also be written on video key frames */ ts->sdt_packet_period = 200; @@ -1186,12 +1189,16 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, int64_t pcr = -1; /* avoid warning */ int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE); int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key; + int last_payload_size = 0; av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO); if (ts->flags & MPEGTS_FLAG_PAT_PMT_AT_FRAMES && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { force_pat = 1; } + if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE && ts->first_pcr == AV_NOPTS_VALUE) + ts->first_pcr = (dts * 300) - av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE); + is_start = 1; while (payload_size > 0) { retransmit_si_info(s, force_pat, dts); @@ -1209,12 +1216,13 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, } if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE && - (dts - get_pcr(ts, s->pb) / 300) > delay) { + last_payload_size != payload_size && (dts - get_pcr(ts, s->pb) / 300) > delay) { /* pcr insert gets priority over null packet insert */ if (write_pcr) mpegts_insert_pcr_only(s, st); else mpegts_insert_null_packet(s); + last_payload_size = payload_size; /* recalculate write_pcr and possibly retransmit si_info */ continue; }