-diff -urN a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
---- a/libavformat/mpegtsenc.c 2017-04-12 19:55:55.000000000 -0600
-+++ b/libavformat/mpegtsenc.c 2017-04-16 16:40:18.488361991 -0600
+diff -ur a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
+--- a/libavformat/mpegtsenc.c 2017-12-10 14:35:10.000000000 -0700
++++ b/libavformat/mpegtsenc.c 2017-12-18 10:54:14.260167666 -0700
@@ -56,9 +56,8 @@
int sid; /* service ID */
char *name;
typedef struct MpegTSWriteStream {
struct MpegTSService *service;
-@@ -717,6 +714,7 @@
+@@ -721,6 +718,7 @@
service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
service->sid = sid;
service->pcr_pid = 0x1fff;
service->provider_name = av_strdup(provider_name);
service->name = av_strdup(name);
if (!service->provider_name || !service->name)
-@@ -732,18 +730,11 @@
+@@ -736,18 +734,11 @@
return NULL;
}
tp_extra_header = AV_RB32(&tp_extra_header);
avio_write(s->pb, (unsigned char *) &tp_extra_header,
sizeof(tp_extra_header));
-@@ -764,6 +755,7 @@
+@@ -768,6 +759,7 @@
MpegTSService *service;
AVStream *st, *pcr_st = NULL;
AVDictionaryEntry *title, *provider;
int i, j;
const char *service_name;
const char *provider_name;
-@@ -772,6 +764,15 @@
+@@ -776,6 +768,15 @@
if (s->max_delay < 0) /* Not set by the caller */
s->max_delay = 0;
// round up to a whole number of TS packets
ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
-@@ -818,6 +819,8 @@
+@@ -822,6 +823,8 @@
service->program = program;
}
}
ts->pat.pid = PAT_PID;
/* Initialize at 15 so that it wraps and is equal to 0 for the
-@@ -903,10 +906,9 @@
+@@ -907,10 +910,9 @@
ts_st->discontinuity = ts->flags & MPEGTS_FLAG_DISCONT;
/* update PCR pid by using the first video stream */
if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
st->codecpar->extradata_size > 0) {
AVStream *ast;
-@@ -942,78 +944,47 @@
+@@ -946,78 +948,47 @@
av_freep(&pids);
/* if no video stream, use the first stream as PCR */
return 0;
-@@ -1028,22 +999,12 @@
+@@ -1032,22 +1003,12 @@
MpegTSWrite *ts = s->priv_data;
int i;
mpegts_write_pat(s);
for (i = 0; i < ts->nb_services; i++)
mpegts_write_pmt(s, ts->services[i]);
-@@ -1085,13 +1046,14 @@
+@@ -1089,13 +1050,14 @@
{
MpegTSWrite *ts = s->priv_data;
MpegTSWriteStream *ts_st = st->priv_data;
*q++ = 0x20 | ts_st->cc; /* Adaptation only */
/* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
*q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
-@@ -1102,7 +1064,7 @@
+@@ -1106,7 +1068,7 @@
}
/* PCR coded into 6 bytes */
/* stuffing bytes */
memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
-@@ -1171,8 +1133,6 @@
+@@ -1175,8 +1137,6 @@
uint8_t *q;
int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags;
int afc_len, stuffing_len;
int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);
-@@ -1182,28 +1142,33 @@
+@@ -1186,28 +1146,33 @@
is_start = 1;
while (payload_size > 0) {
continue;
}
-@@ -1213,6 +1178,10 @@
+@@ -1217,6 +1182,10 @@
val = ts_st->pid >> 8;
if (is_start)
val |= 0x40;
*q++ = val;
*q++ = ts_st->pid;
ts_st->cc = ts_st->cc + 1 & 0xf;
-@@ -1224,7 +1193,7 @@
+@@ -1228,7 +1197,7 @@
}
if (key && is_start && pts != AV_NOPTS_VALUE) {
// set Random Access for key frames
write_pcr = 1;
set_af_flag(buf, 0x40);
q = get_ts_payload_start(buf);
-@@ -1232,14 +1201,10 @@
+@@ -1236,14 +1205,10 @@
if (write_pcr) {
set_af_flag(buf, 0x10);
q = get_ts_payload_start(buf);
q = get_ts_payload_start(buf);
}
if (is_start) {
-@@ -1340,11 +1305,13 @@
+@@ -1344,11 +1309,13 @@
*q++ = flags;
*q++ = header_len;
if (pts != AV_NOPTS_VALUE) {
q += 5;
}
if (pes_extension && st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {
-@@ -1515,7 +1482,6 @@
+@@ -1519,7 +1486,6 @@
uint8_t *data = NULL;
MpegTSWrite *ts = s->priv_data;
MpegTSWriteStream *ts_st = st->priv_data;
int64_t dts = pkt->dts, pts = pkt->pts;
int opus_samples = 0;
int side_data_size;
-@@ -1536,16 +1502,15 @@
+@@ -1540,16 +1506,15 @@
}
if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
}
if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
-@@ -1733,7 +1698,7 @@
+@@ -1737,7 +1702,7 @@
AVStream *st2 = s->streams[i];
MpegTSWriteStream *ts_st2 = st2->priv_data;
if ( ts_st2->payload_size
mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
ts_st2->payload_pts, ts_st2->payload_dts,
ts_st2->payload_flags & AV_PKT_FLAG_KEY, stream_id);
-@@ -1904,12 +1869,18 @@
+@@ -1908,12 +1873,18 @@
{ "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT,
{ .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
{ "muxrate", NULL,
offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT,
{ .i64 = 1 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
-@@ -1947,15 +1918,15 @@
+@@ -1951,15 +1922,15 @@
{ "omit_video_pes_length", "Omit the PES packet length for video packets",
offsetof(MpegTSWrite, omit_video_pes_length), AV_OPT_TYPE_BOOL,
{ .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },