[FFmpeg-devel] [PATCH 08/10] avformat/mpegtsenc: simulate a CBR stream for m2ts mode

Marton Balint cus at passwd.hu
Tue Nov 12 23:29:04 EET 2019


Signed-off-by: Marton Balint <cus at passwd.hu>
---
 libavformat/mpegtsenc.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index a4b28f7d74..2191c134ac 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -35,6 +35,7 @@
 #include "mpegts.h"
 
 #define PCR_TIME_BASE 27000000
+#define M2TS_DEFAULT_MUXRATE 54000000
 
 /* write DVB SI sections */
 
@@ -869,8 +870,13 @@ static int mpegts_init(AVFormatContext *s)
     ts->m2ts_subtitle_pid = 0x1200;
     ts->m2ts_other_pid    = 0x1300; // should not be needed
 
-    if (ts->m2ts_mode)
+    if (ts->m2ts_mode) {
         ts->pmt_start_pid = 0x0100;
+        if (ts->mux_rate <= 1) {
+            ts->mux_rate = M2TS_DEFAULT_MUXRATE;
+            av_log(s, AV_LOG_INFO, "Muxrate is not set for m2ts mode, using %d bps\n", ts->mux_rate);
+        }
+    }
 
     if (s->max_delay < 0) /* Not set by the caller */
         s->max_delay = 0;
@@ -1088,11 +1094,17 @@ static int write_pcr_bits(uint8_t *buf, int64_t pcr)
 }
 
 /* Write a single null transport stream packet */
-static void mpegts_insert_null_packet(AVFormatContext *s)
+static void mpegts_insert_null_packet(AVFormatContext *s, int force)
 {
+    MpegTSWrite *ts = s->priv_data;
     uint8_t *q;
     uint8_t buf[TS_PACKET_SIZE];
 
+    if (ts->m2ts_mode && !force) {
+        ts->nb_packets++;
+        return;
+    }
+
     q    = buf;
     *q++ = 0x47;
     *q++ = 0x00 | 0x1f;
@@ -1251,7 +1263,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
                 if (write_pcr)
                     mpegts_insert_pcr_only(s, st);
                 else
-                    mpegts_insert_null_packet(s);
+                    mpegts_insert_null_packet(s, 0);
                 /* recalculate write_pcr and possibly retransmit si_info */
                 continue;
             }
@@ -1824,7 +1836,7 @@ static void mpegts_write_flush(AVFormatContext *s)
     if (ts->m2ts_mode) {
         int packets = (avio_tell(s->pb) / (TS_PACKET_SIZE + 4)) % 32;
         while (packets++ < 32)
-            mpegts_insert_null_packet(s);
+            mpegts_insert_null_packet(s, 1);
     }
 }
 
-- 
2.16.4



More information about the ffmpeg-devel mailing list