]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/mpegtsenc.c
Fix concat seeking SEEK_END case.
[ffmpeg] / libavformat / mpegtsenc.c
index 148507f5f6ddc6400e5702becfa112fec00f4a82..0d315a0fb0a2a231dbf3f556f751f399bd64d49c 100644 (file)
@@ -89,8 +89,8 @@ static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
             b |= 0x40;
         *q++ = b;
         *q++ = s->pid;
-        *q++ = 0x10 | s->cc;
         s->cc = (s->cc + 1) & 0xf;
+        *q++ = 0x10 | s->cc;
         if (first)
             *q++ = 0; /* 0 offset */
         len1 = TS_PACKET_SIZE - (q - packet);
@@ -399,12 +399,12 @@ static int mpegts_write_header(AVFormatContext *s)
     service->pmt.opaque = s;
 
     ts->pat.pid = PAT_PID;
-    ts->pat.cc = 0;
+    ts->pat.cc = 15; // Initialize at 15 so that it wraps and be equal to 0 for the first packet we write
     ts->pat.write_packet = section_write_packet;
     ts->pat.opaque = s;
 
     ts->sdt.pid = SDT_PID;
-    ts->sdt.cc = 0;
+    ts->sdt.cc = 15;
     ts->sdt.write_packet = section_write_packet;
     ts->sdt.opaque = s;
 
@@ -425,7 +425,16 @@ static int mpegts_write_header(AVFormatContext *s)
         if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
             service->pcr_pid == 0x1fff)
             service->pcr_pid = ts_st->pid;
-        total_bit_rate += st->codec->bit_rate;
+        if (st->codec->rc_max_rate)
+            total_bit_rate += st->codec->rc_max_rate;
+        else {
+            if (!st->codec->bit_rate) {
+                av_log(s, AV_LOG_WARNING,
+                       "stream %d, bit rate is not set, this will cause problems\n",
+                       st->index);
+            }
+            total_bit_rate += st->codec->bit_rate;
+        }
         /* PES header size */
         if (st->codec->codec_type == CODEC_TYPE_VIDEO ||
             st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
@@ -638,8 +647,8 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
             val |= 0x40;
         *q++ = val;
         *q++ = ts_st->pid;
-        *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
         ts_st->cc = (ts_st->cc + 1) & 0xf;
+        *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
         if (write_pcr) {
             // add 11, pcr references the last byte of program clock reference base
             pcr = ts->cur_pcr + (4+7)*8*90000LL / ts->mux_rate;
@@ -775,7 +784,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
 static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     AVStream *st = s->streams[pkt->stream_index];
-    int len, size = pkt->size;
+    int size = pkt->size;
     uint8_t *buf= pkt->data;
     uint8_t *data= NULL;
     MpegTSWriteStream *ts_st = st->priv_data;
@@ -818,29 +827,20 @@ static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
         return 0;
     }
 
-    if (ts_st->payload_pts == AV_NOPTS_VALUE) {
-        ts_st->payload_dts = dts;
-        ts_st->payload_pts = pts;
+    if (ts_st->payload_index + size > DEFAULT_PES_PAYLOAD_SIZE) {
+        mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
+                         ts_st->payload_pts, ts_st->payload_dts);
+        ts_st->payload_index = 0;
     }
 
-    // audio
-    while (size > 0) {
-        len = DEFAULT_PES_PAYLOAD_SIZE - ts_st->payload_index;
-        if (len > size)
-            len = size;
-        memcpy(ts_st->payload + ts_st->payload_index, buf, len);
-        buf += len;
-        size -= len;
-        ts_st->payload_index += len;
-        if (ts_st->payload_index >= DEFAULT_PES_PAYLOAD_SIZE) {
-            mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
-                             ts_st->payload_pts, ts_st->payload_dts);
-            ts_st->payload_pts = AV_NOPTS_VALUE;
-            ts_st->payload_dts = AV_NOPTS_VALUE;
-            ts_st->payload_index = 0;
-        }
+    if (!ts_st->payload_index) {
+        ts_st->payload_pts = pts;
+        ts_st->payload_dts = dts;
     }
 
+    memcpy(ts_st->payload + ts_st->payload_index, buf, size);
+    ts_st->payload_index += size;
+
     return 0;
 }