[FFmpeg-devel] mpegtsenc: add option to define a timestamp offset

Harald Axmann harald.axmann at hotmail.com
Sun Mar 17 21:19:28 CET 2013


Hello,

Mark Essel reported the problem, that the "setpts"-filter is not working 
for mpegts [1]. I could not find a solution for the problem with current 
ffmpeg, so I prepared a quick workaround for him, which adds an 
mpegts-specific offset option ("-timestamp_offset"). The offset is 
added, when the PES-packet is written. I have attached the patch as base 
for discussion.

Best regards,
   Harald Axmann

[1] http://ffmpeg.org/pipermail/ffmpeg-user/2013-March/014050.html

-------------- next part --------------
diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 7016774..8a78e7f 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -84,6 +84,7 @@ typedef struct MpegTSWrite {
 #define MPEGTS_FLAG_AAC_LATM        0x02
     int flags;
     int copyts;
+    int64_t timestamp_offset;
 } MpegTSWrite;
 
 /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
@@ -120,6 +121,8 @@ static const AVOption options[] = {
       offsetof(MpegTSWrite, reemit_pat_pmt), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
     { "mpegts_copyts", "dont offset dts/pts",
       offsetof(MpegTSWrite, copyts), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
+    { "timestamp_offset", "Set the timestamp offset in micro seconds (default 0)", 
+      offsetof(MpegTSWrite, timestamp_offset), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
     { NULL },
 };
 
@@ -643,7 +646,7 @@ static int mpegts_write_header(AVFormatContext *s)
             (TS_PACKET_SIZE * 8 * 1000);
 
         if(ts->copyts < 1)
-            ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
+            ts->first_pcr = av_rescale(s->max_delay + ts->timestamp_offset, PCR_TIME_BASE, AV_TIME_BASE);
     } else {
         /* Arbitrary values, PAT/PMT will also be written on video key frames */
         ts->sdt_packet_period = 200;
@@ -849,8 +852,12 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
     int afc_len, stuffing_len;
     int64_t pcr = -1; /* avoid warning */
     int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
+    int64_t offset = av_rescale(ts->timestamp_offset, 90000, AV_TIME_BASE);
     int force_pat = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
 
+    dts += offset;
+    pts += offset;
+
     is_start = 1;
     while (payload_size > 0) {
         retransmit_si_info(s, force_pat);



More information about the ffmpeg-devel mailing list