From patchwork Thu Apr 25 09:07:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Andreas_H=C3=A5kon?= X-Patchwork-Id: 12904 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 F08304485A4 for ; Thu, 25 Apr 2019 12:07:34 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D092E68A6D1; Thu, 25 Apr 2019 12:07:34 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 844436807BC for ; Thu, 25 Apr 2019 12:07:28 +0300 (EEST) Date: Thu, 25 Apr 2019 09:07:25 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=default; t=1556183247; bh=o9tzJW4lVYwNyYuft3Qd/d1Ua6xhnqITFnojXaJ64WA=; h=Date:To:From:Reply-To:Subject:Feedback-ID:From; b=FBJkvO4QAikfdg7TC0jo59esaL57HRKeiiKlWefM8pa3/yGRk7x00kLEfD6x3mhzj EkBrOj27cNVbzChnzxanG9CyRuj1H9m0SAREuZkZh3nluwGUueFGrSPPm0tN9HT/1u Hql3mFAZljW529/7EUTDwQKiHszysdmJRIddXYdk= To: FFmpeg development discussions and patches From: =?UTF-8?Q?Andreas_H=C3=A5kon?= Message-ID: 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,HTML_MESSAGE autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail.protonmail.ch X-Content-Filtered-By: Mailman/MimeDel 2.1.20 Subject: [FFmpeg-devel] [PATCH] libavformat/mpegtsenc: enforce PCR packets without payload 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Hi, A patch for a new optional parameter for the mpegtsenc muxer. Regards. A.H. --- From 7f9f1a992615e15121b661d4cd1b9c1e32e1c894 Mon Sep 17 00:00:00 2001 From: Andreas Hakon Date: Thu, 25 Apr 2019 09:49:29 +0100 Subject: [PATCH] libavformat/mpegtsenc: enforce PCR packets without payload This patch provides a new optional parameter for the mpegtsenc muxer. The parameter "-pcr_without_payload" can be used to override the default behaviour of writing PCR timestamps in TS packets with payload. Using a value greater than 0 all TS packets carrying PCR timestamps will be forced to have an empty payload. Otherwise (with 0) the regular behaviour is used and PCR packets can have payload. Futhermore with a value greater than 1 the packet with the PCR will be repeated the number of times indicated. The default value is "-pcr_without_payload 0", which corresponds to the current behaviour. This can be handy for many reasons. Signed-off-by: Andreas Hakon --- doc/muxers.texi | 5 +++++ libavformat/mpegtsenc.c | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/doc/muxers.texi b/doc/muxers.texi index 83ae017..9acdee3 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -1564,6 +1564,11 @@ Set a constant muxrate. Default is VBR. @item pes_payload_size @var{integer} Set minimum PES packet payload in bytes. Default is @code{2930}. +@item pcr_without_payload @var{integer} +Generates PCR packets without payload. When the value is greater than 1 +it repeats the PCR packet N times. With a 0 value it generates regular +PCR packets with payload. Default is @code{0}. + @item mpegts_flags @var{flags} Set mpegts flags. Accepts the following options: @table @samp diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c index fc0ea22..0cc42cd 100644 --- a/libavformat/mpegtsenc.c +++ b/libavformat/mpegtsenc.c @@ -97,6 +97,7 @@ typedef struct MpegTSWrite { int pmt_start_pid; int start_pid; int m2ts_mode; + int pcr_repetition; int reemit_pat_pmt; // backward compatibility @@ -1093,12 +1094,13 @@ static void mpegts_insert_null_packet(AVFormatContext *s) } /* Write a single transport stream packet with a PCR and no payload */ -static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st) +static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st, int repetition) { MpegTSWrite *ts = s->priv_data; MpegTSWriteStream *ts_st = st->priv_data; uint8_t *q; uint8_t buf[TS_PACKET_SIZE]; + int i; q = buf; *q++ = 0x47; @@ -1119,7 +1121,9 @@ static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st) /* stuffing bytes */ memset(q, 0xFF, TS_PACKET_SIZE - (q - buf)); mpegts_prefix_m2ts_header(s); - avio_write(s->pb, buf, TS_PACKET_SIZE); + for (i=0; ipb, buf, TS_PACKET_SIZE); + } } static void write_pts(uint8_t *q, int fourbits, int64_t pts) @@ -1212,12 +1216,15 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, (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); + mpegts_insert_pcr_only(s, st, ts->pcr_repetition < 1 ? 1 : ts->pcr_repetition); else mpegts_insert_null_packet(s); /* recalculate write_pcr and possibly retransmit si_info */ continue; } + if (write_pcr && ts->pcr_repetition > 0) { + mpegts_insert_pcr_only(s, st, ts->pcr_repetition); + } /* prepare packet header */ q = buf; @@ -1241,7 +1248,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, set_af_flag(buf, 0x40); q = get_ts_payload_start(buf); } - if (write_pcr) { + if (write_pcr && ts->pcr_repetition == 0) { set_af_flag(buf, 0x10); q = get_ts_payload_start(buf); // add 11, pcr references the last byte of program clock reference base @@ -1926,6 +1933,9 @@ static const AVOption options[] = { { "pes_payload_size", "Minimum PES packet payload in bytes", offsetof(MpegTSWrite, pes_payload_size), AV_OPT_TYPE_INT, { .i64 = DEFAULT_PES_PAYLOAD_SIZE }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, + { "pcr_without_payload", "Generate PCR packets without payload", + offsetof(MpegTSWrite, pcr_repetition), AV_OPT_TYPE_INT, + { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, { "mpegts_flags", "MPEG-TS muxing flags", offsetof(MpegTSWrite, flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },