1 diff -git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
2 --- a/libavformat/mpegtsenc.c 2021-04-08 15:28:40.000000000 -0600
3 +++ b/libavformat/mpegtsenc.c 2021-05-29 11:47:43.834039463 -0600
5 int64_t sdt_period; /* SDT period in PCR time base */
6 int64_t pat_period; /* PAT/PMT period in PCR time base */
11 + int64_t pcr_pos, pcr;
12 + int64_t first_pcr, next_pcr;
15 int mux_rate; ///< set to 1 when VBR
21 int64_t pcr_period; /* PCR period in PCR time base */
26 int opus_queued_samples;
31 -static int64_t get_pcr(const MpegTSWrite *ts)
32 +static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
34 - return av_rescale(ts->total_size + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
36 + int64_t pos = avio_tell(pb) + 11;
37 + return ts->pcr + (ts->mux_rate == 1 ? (pos - ts->pcr_pos) * 8 :
38 + av_rescale(pos - ts->pcr_pos, 8 * PCR_TIME_BASE, ts->mux_rate));
41 static void write_packet(AVFormatContext *s, const uint8_t *packet)
43 MpegTSWrite *ts = s->priv_data;
45 - int64_t pcr = get_pcr(s->priv_data);
46 - uint32_t tp_extra_header = pcr % 0x3fffffff;
47 + uint32_t tp_extra_header = get_pcr(ts, s->pb) % 0x3fffffff;
48 tp_extra_header = AV_RB32(&tp_extra_header);
49 avio_write(s->pb, (unsigned char *) &tp_extra_header,
50 sizeof(tp_extra_header));
53 ts_st->pcr_period = 1;
56 - // output a PCR as soon as possible
57 - ts_st->last_pcr = ts->first_pcr - ts_st->pcr_period;
60 static void select_pcr_streams(AVFormatContext *s)
63 if (s->max_delay < 0) /* Not set by the caller */
65 + ts->delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
67 // round up to a whole number of TS packets
68 ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
70 /* MPEG pid values < 16 are reserved. Applications which set st->id in
71 * this range are assigned a calculated pid. */
73 - if (ts->m2ts_mode) {
74 + if (ts->start_pid >= 0)
75 + ts_st->pid = ts->start_pid + i;
76 + else if (ts->m2ts_mode) {
77 switch (st->codecpar->codec_type) {
78 case AVMEDIA_TYPE_VIDEO:
79 ts_st->pid = ts->m2ts_video_pid++;
81 av_log(s, AV_LOG_ERROR, "Cannot automatically assign PID for stream %d\n", st->index);
82 return AVERROR(EINVAL);
85 - ts_st->pid = ts->start_pid + i;
88 + ts_st->pid = START_PID + i;
92 @@ -1144,8 +1146,12 @@
94 ts->last_pat_ts = AV_NOPTS_VALUE;
95 ts->last_sdt_ts = AV_NOPTS_VALUE;
96 - ts->pat_period = av_rescale(ts->pat_period_us, PCR_TIME_BASE, AV_TIME_BASE);
97 - ts->sdt_period = av_rescale(ts->sdt_period_us, PCR_TIME_BASE, AV_TIME_BASE);
98 + ts->pat_period = ts->pat_period_us < 0 ? -1 :
99 + av_rescale(ts->pat_period_us, PCR_TIME_BASE, AV_TIME_BASE);
100 + ts->sdt_period = ts->sdt_period_us < 0 ? -1 :
101 + av_rescale(ts->sdt_period_us, PCR_TIME_BASE, AV_TIME_BASE);
105 if (ts->mux_rate == 1)
106 av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
107 @@ -1153,34 +1159,37 @@
108 av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
109 av_log(s, AV_LOG_VERBOSE,
110 "sdt every %"PRId64" ms, pat/pmt every %"PRId64" ms\n",
111 - av_rescale(ts->sdt_period, 1000, PCR_TIME_BASE),
112 - av_rescale(ts->pat_period, 1000, PCR_TIME_BASE));
113 + ts->sdt_period < 0 ? -1 : av_rescale(ts->sdt_period, 1000, PCR_TIME_BASE),
114 + ts->pat_period < 0 ? -1 : av_rescale(ts->pat_period, 1000, PCR_TIME_BASE));
119 /* send SDT, PAT and PMT tables regularly */
120 -static void retransmit_si_info(AVFormatContext *s, int force_pat, int force_sdt, int64_t pcr)
121 +static void retransmit_si_info(AVFormatContext *s, int force_pat, int force_sdt)
123 MpegTSWrite *ts = s->priv_data;
126 - if ((pcr != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
127 - (pcr != AV_NOPTS_VALUE && pcr - ts->last_sdt_ts >= ts->sdt_period) ||
130 - if (pcr != AV_NOPTS_VALUE)
131 - ts->last_sdt_ts = FFMAX(pcr, ts->last_sdt_ts);
132 - mpegts_write_sdt(s);
134 - if ((pcr != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) ||
135 - (pcr != AV_NOPTS_VALUE && pcr - ts->last_pat_ts >= ts->pat_period) ||
137 - if (pcr != AV_NOPTS_VALUE)
138 - ts->last_pat_ts = FFMAX(pcr, ts->last_pat_ts);
139 - mpegts_write_pat(s);
140 - for (i = 0; i < ts->nb_services; i++)
141 - mpegts_write_pmt(s, ts->services[i]);
142 + if (ts->sdt_period >= 0) {
143 + int64_t pcr = get_pcr(ts, s->pb);
144 + if (ts->last_sdt_ts == AV_NOPTS_VALUE || pcr >= ts->last_sdt_ts + ts->sdt_period)
147 + ts->last_sdt_ts = pcr;
148 + mpegts_write_sdt(s);
151 + if (ts->pat_period >= 0) {
152 + int64_t pcr = get_pcr(ts, s->pb);
153 + if (ts->last_pat_ts == AV_NOPTS_VALUE || pcr >= ts->last_pat_ts + ts->pat_period)
156 + ts->last_pat_ts = pcr;
157 + mpegts_write_pat(s);
158 + for (i = 0; i < ts->nb_services; i++)
159 + mpegts_write_pmt(s, ts->services[i]);
164 @@ -1217,25 +1226,29 @@
165 static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
167 MpegTSWrite *ts = s->priv_data;
168 - MpegTSWriteStream *ts_st = st->priv_data;
169 + int64_t pcr = get_pcr(ts, s->pb);
170 + MpegTSWriteStream *ts_st = st ? st->priv_data : 0;
171 + uint32_t pcr_pid = ts_st ? ts_st->pid : ts->pcr_stream_pid;
173 uint8_t buf[TS_PACKET_SIZE];
177 - *q++ = ts_st->pid >> 8;
179 - *q++ = 0x20 | ts_st->cc; /* Adaptation only */
180 + *q++ = pcr_pid >> 8;
182 + uint32_t flags = 0x20; /* Adaptation only */
183 /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
184 + if(ts_st) flags |= ts_st->cc;
186 *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
187 *q++ = 0x10; /* Adaptation flags: PCR present */
188 - if (ts_st->discontinuity) {
189 + if (ts_st && ts_st->discontinuity) {
191 ts_st->discontinuity = 0;
194 /* PCR coded into 6 bytes */
195 - q += write_pcr_bits(q, get_pcr(ts));
196 + q += write_pcr_bits(q, pcr);
199 memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
200 @@ -1303,9 +1316,9 @@
202 int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags;
203 int afc_len, stuffing_len;
204 - int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
205 int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
209 av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);
210 if (ts->flags & MPEGTS_FLAG_PAT_PMT_AT_FRAMES && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
211 @@ -1320,20 +1333,19 @@
214 while (payload_size > 0) {
215 - int64_t pcr = AV_NOPTS_VALUE;
216 - if (ts->mux_rate > 1)
218 - else if (dts != AV_NOPTS_VALUE)
219 - pcr = (dts - delay) * 300;
221 - retransmit_si_info(s, force_pat, force_sdt, pcr);
224 + // add 11, pcr references the last byte of program clock reference base
225 + ts->pcr_pos = avio_tell(s->pb) + 11;
226 + pcr = ts->pcr = ts->mux_rate != 1 ?
227 + av_rescale(ts->pcr_pos, 8 * PCR_TIME_BASE, ts->mux_rate) :
228 + (dts == AV_NOPTS_VALUE ? 0 : (dts - ts->delay) * 300);
229 + if (force_pat || force_sdt) {
230 + retransmit_si_info(s, force_pat, force_sdt);
231 + force_pat = force_sdt = 0;
235 if (ts->mux_rate > 1) {
236 /* Send PCR packets for all PCR streams if needed */
238 if (pcr >= ts->next_pcr) {
239 int64_t next_pcr = INT64_MAX;
240 for (int i = 0; i < s->nb_streams; i++) {
241 @@ -1343,36 +1355,43 @@
242 AVStream *st2 = s->streams[st2_index];
243 MpegTSWriteStream *ts_st2 = st2->priv_data;
244 if (ts_st2->pcr_period) {
245 - if (pcr - ts_st2->last_pcr >= ts_st2->pcr_period) {
246 - ts_st2->last_pcr = FFMAX(pcr - ts_st2->pcr_period, ts_st2->last_pcr + ts_st2->pcr_period);
248 + if (pcr >= ts_st2->pcr_timer) {
249 + ts_st2->pcr_timer = pcr + ts_st2->pcr_period;
251 mpegts_insert_pcr_only(s, st2);
257 - next_pcr = FFMIN(next_pcr, ts_st2->last_pcr + ts_st2->pcr_period);
258 + next_pcr = FFMIN(next_pcr, ts_st2->pcr_timer);
261 ts->next_pcr = next_pcr;
263 - if (dts != AV_NOPTS_VALUE && (dts - pcr / 300) > delay) {
264 - /* pcr insert gets priority over null packet insert */
266 - mpegts_insert_pcr_only(s, st);
268 - mpegts_insert_null_packet(s);
269 - /* recalculate write_pcr and possibly retransmit si_info */
272 - } else if (ts_st->pcr_period && pcr != AV_NOPTS_VALUE) {
273 - if (pcr - ts_st->last_pcr >= ts_st->pcr_period && is_start) {
274 - ts_st->last_pcr = FFMAX(pcr - ts_st->pcr_period, ts_st->last_pcr + ts_st->pcr_period);
276 + else if (ts_st->pcr_period) {
277 + if (pcr >= ts_st->pcr_timer) {
278 + ts_st->pcr_timer = pcr + ts_st->pcr_period;
283 + if (write_pcr && ts->pcr_stream_pid >= 0) {
284 + mpegts_insert_pcr_only(s, 0);
288 + if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
289 + (dts - pcr / 300) > ts->delay) {
290 + /* pcr insert gets priority over null packet insert */
292 + mpegts_insert_pcr_only(s, st);
294 + mpegts_insert_null_packet(s);
295 + /* recalculate write_pcr and possibly retransimit si_info */
299 /* prepare packet header */
302 @@ -1400,7 +1419,6 @@
304 set_af_flag(buf, 0x10);
305 q = get_ts_payload_start(buf);
306 - // add 11, pcr references the last byte of program clock reference base
307 if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
308 av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
309 extend_af(buf, write_pcr_bits(q, pcr));
310 @@ -1678,8 +1696,8 @@
311 uint8_t *data = NULL;
312 MpegTSWrite *ts = s->priv_data;
313 MpegTSWriteStream *ts_st = st->priv_data;
314 - const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
315 - const int64_t max_audio_delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) / 2;
316 + const int64_t delay_ticks2 = ts->delay * 2;
317 + const int64_t max_audio_delay = ts->delay / 2;
318 int64_t dts = pkt->dts, pts = pkt->pts;
319 int opus_samples = 0;
320 buffer_size_t side_data_size;
321 @@ -1699,9 +1717,9 @@
324 if (pts != AV_NOPTS_VALUE)
326 + pts += delay_ticks2;
327 if (dts != AV_NOPTS_VALUE)
329 + dts += delay_ticks2;
332 if (!ts_st->first_timestamp_checked && (pts == AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE)) {
333 @@ -2098,8 +2116,10 @@
334 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV }, 0x01, 0xff, ENC, "mpegts_service_type" },
335 { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
336 OFFSET(pmt_start_pid), AV_OPT_TYPE_INT, { .i64 = 0x1000 }, FIRST_OTHER_PID, LAST_OTHER_PID, ENC },
337 + { "mpegts_pcr_stream_pid", "create seperate PCR stream on this pid.",
338 + OFFSET(pcr_stream_pid), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 0x1f00, ENC },
339 { "mpegts_start_pid", "Set the first pid.",
340 - OFFSET(start_pid), AV_OPT_TYPE_INT, { .i64 = 0x0100 }, FIRST_OTHER_PID, LAST_OTHER_PID, ENC },
341 + OFFSET(start_pid), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, LAST_OTHER_PID, ENC },
342 { "mpegts_m2ts_mode", "Enable m2ts mode.", OFFSET(m2ts_mode), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, ENC },
343 { "muxrate", NULL, OFFSET(mux_rate), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, ENC },
344 { "pes_payload_size", "Minimum PES packet payload in bytes",
345 @@ -2121,10 +2141,10 @@
346 OFFSET(omit_video_pes_length), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, ENC },
347 { "pcr_period", "PCR retransmission time in milliseconds",
348 OFFSET(pcr_period_ms), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, ENC },
349 - { "pat_period", "PAT/PMT retransmission time limit in seconds",
350 + { "pat_period", "PAT/PMT retransmission time limit in ms, -1 no pat",
351 OFFSET(pat_period_us), AV_OPT_TYPE_DURATION, { .i64 = PAT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, ENC },
352 - { "sdt_period", "SDT retransmission time limit in seconds",
353 - OFFSET(sdt_period_us), AV_OPT_TYPE_DURATION, { .i64 = SDT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, ENC },
354 + { "sdt_period", "SDT retransmission time limit in ms, -1 no sdt",
355 + OFFSET(sdt_period_us), AV_OPT_TYPE_INT64, { .i64 = SDT_RETRANS_TIME * 1000LL }, -1, INT64_MAX, ENC },
359 diff -git a/libavformat/mpegts.h b/libavformat/mpegts.h
360 --- a/libavformat/mpegts.h 2020-10-15 12:32:22.417967898 -0600
361 +++ b/libavformat/mpegts.h 2020-10-15 12:32:46.463055553 -0600
363 /* PID from 0x1FFC to 0x1FFE may be assigned as needed to PMT, elementary
364 * streams and other data tables */
365 #define NULL_PID 0x1FFF /* Null packet (used for fixed bandwidth padding) */
366 +#define START_PID 0x0400
369 #define M2TS_PMT_PID 0x0100