1 diff -urN a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
2 --- a/libavformat/mpegtsenc.c 2018-04-20 04:02:57.000000000 -0600
3 +++ b/libavformat/mpegtsenc.c 2018-04-24 10:27:57.193689213 -0600
5 int sid; /* service ID */
9 - int pcr_packet_count;
10 - int pcr_packet_period;
11 + int64_t pcr, pcr_packet_timer, pcr_packet_period;
12 + int pcr_sid, pcr_pid;
17 MpegTSSection pat; /* MPEG-2 PAT table */
18 MpegTSSection sdt; /* MPEG-2 SDT table context */
19 MpegTSService **services;
20 - int sdt_packet_count;
21 - int sdt_packet_period;
22 - int pat_packet_count;
23 - int pat_packet_period;
24 + int64_t sdt_packet_timer, sdt_packet_period;
25 + int64_t pat_packet_timer, pat_packet_period;
30 + int64_t pcr, first_pcr, delay;
31 int mux_rate; ///< set to 1 when VBR
43 int reemit_pat_pmt; // backward compatibility
47 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
48 #define MPEGTS_FLAG_AAC_LATM 0x02
49 #define MPEGTS_FLAG_PAT_PMT_AT_FRAMES 0x04
54 - int64_t last_pat_ts;
55 - int64_t last_sdt_ts;
57 int omit_video_pes_length;
60 #define DEFAULT_PROVIDER_NAME "FFmpeg"
61 #define DEFAULT_SERVICE_NAME "Service01"
63 -/* we retransmit the SI info at this rate */
64 +/* we retransmit the SI info at this rate (ms) */
65 #define SDT_RETRANS_TIME 500
66 #define PAT_RETRANS_TIME 100
67 -#define PCR_RETRANS_TIME 20
68 +#define PCR_RETRANS_TIME 50
70 typedef struct MpegTSWriteStream {
71 struct MpegTSService *service;
73 service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
75 service->pcr_pid = 0x1fff;
76 + service->pcr_sid = 0x1fff;
77 service->provider_name = av_strdup(provider_name);
78 service->name = av_strdup(name);
79 if (!service->provider_name || !service->name)
84 -static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
86 - return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
90 static void mpegts_prefix_m2ts_header(AVFormatContext *s)
92 MpegTSWrite *ts = s->priv_data;
94 - int64_t pcr = get_pcr(s->priv_data, s->pb);
95 - uint32_t tp_extra_header = pcr % 0x3fffffff;
96 + uint32_t tp_extra_header = ts->pcr % 0x3fffffff;
97 tp_extra_header = AV_RB32(&tp_extra_header);
98 avio_write(s->pb, (unsigned char *) &tp_extra_header,
99 sizeof(tp_extra_header));
101 MpegTSService *service;
102 AVStream *st, *pcr_st = NULL;
103 AVDictionaryEntry *title, *provider;
106 const char *service_name;
107 const char *provider_name;
110 if (s->max_delay < 0) /* Not set by the caller */
112 + ts->delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
114 + if (ts->m2ts_mode == -1) {
115 + if (av_match_ext(s->url, "m2ts")) {
122 // round up to a whole number of TS packets
123 ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
125 service->program = program;
128 + if (ts->m2ts_mode > 1)
129 + service->pmt.pid = 0x00ff + ts->service_id;
131 ts->pat.pid = PAT_PID;
132 /* Initialize at 15 so that it wraps and is equal to 0 for the
134 ts_st->discontinuity = ts->flags & MPEGTS_FLAG_DISCONT;
135 /* update PCR pid by using the first video stream */
136 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
137 - service->pcr_pid == 0x1fff) {
138 - service->pcr_pid = ts_st->pid;
139 + service->pcr_sid == 0x1fff)
143 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
144 st->codecpar->extradata_size > 0) {
146 @@ -946,78 +948,47 @@
149 /* if no video stream, use the first stream as PCR */
150 - if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
151 - pcr_st = s->streams[0];
152 - ts_st = pcr_st->priv_data;
153 - service->pcr_pid = ts_st->pid;
155 - ts_st = pcr_st->priv_data;
157 - if (ts->mux_rate > 1) {
158 - service->pcr_packet_period = (int64_t)ts->mux_rate * ts->pcr_period /
159 - (TS_PACKET_SIZE * 8 * 1000);
160 - ts->sdt_packet_period = (int64_t)ts->mux_rate * SDT_RETRANS_TIME /
161 - (TS_PACKET_SIZE * 8 * 1000);
162 - ts->pat_packet_period = (int64_t)ts->mux_rate * PAT_RETRANS_TIME /
163 - (TS_PACKET_SIZE * 8 * 1000);
165 - if (ts->copyts < 1)
166 - ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
168 - /* Arbitrary values, PAT/PMT will also be written on video key frames */
169 - ts->sdt_packet_period = 200;
170 - ts->pat_packet_period = 40;
171 - if (pcr_st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
172 - int frame_size = av_get_audio_frame_duration2(pcr_st->codecpar, 0);
174 - av_log(s, AV_LOG_WARNING, "frame size not set\n");
175 - service->pcr_packet_period =
176 - pcr_st->codecpar->sample_rate / (10 * 512);
178 - service->pcr_packet_period =
179 - pcr_st->codecpar->sample_rate / (10 * frame_size);
182 - // max delta PCR 0.1s
183 - // TODO: should be avg_frame_rate
184 - service->pcr_packet_period =
185 - ts_st->user_tb.den / (10 * ts_st->user_tb.num);
187 - if (!service->pcr_packet_period)
188 - service->pcr_packet_period = 1;
191 - ts->last_pat_ts = AV_NOPTS_VALUE;
192 - ts->last_sdt_ts = AV_NOPTS_VALUE;
193 - // The user specified a period, use only it
194 - if (ts->pat_period < INT_MAX/2) {
195 - ts->pat_packet_period = INT_MAX;
196 + if (!pcr_st && s->nb_streams > 0)
197 + pcr_st = s->streams[0];
199 + av_log(s, AV_LOG_ERROR, "no streams\n");
200 + ret = AVERROR(EINVAL);
203 - if (ts->sdt_period < INT_MAX/2) {
204 - ts->sdt_packet_period = INT_MAX;
205 + ts_st = pcr_st->priv_data;
206 + if (service->pcr_sid == 0x1fff)
207 + service->pcr_sid = ts_st->pid;
208 + if (service->pcr_pid == 0x1fff)
209 + service->pcr_pid = ts->m2ts_mode > 1 ?
210 + 0x1000 + ts->service_id : service->pcr_sid ;
211 + if (service->pmt.pid == service->pcr_pid) {
212 + av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", service->pcr_pid);
213 + ret = AVERROR(EINVAL);
217 + clk_rate = ts->mux_rate > 1 ? ts->mux_rate : PCR_TIME_BASE;
218 + ts->sdt_packet_period = ts->sdt_period < 0 ? -1 : ts->sdt_period/1000 * clk_rate;
219 + ts->pat_packet_period = ts->pat_period/1000 * clk_rate;
220 + service->pcr_packet_period = ts->pcr_period/1000 * clk_rate;
221 + if (service->pcr_packet_period < (TS_PACKET_SIZE*8*10))
222 + service->pcr_packet_period = (TS_PACKET_SIZE*8*10);
223 + av_log(s, AV_LOG_VERBOSE, "clk_rate %f: ticks/pkt %d pcr, %d sdt, %d pmt\n", clk_rate,
224 + (int)service->pcr_packet_period, (int)ts->sdt_packet_period, (int)ts->pat_packet_period);
226 + if (ts->copyts < 1)
227 + ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
229 // output a PCR as soon as possible
230 - service->pcr_packet_count = service->pcr_packet_period;
231 - ts->pat_packet_count = ts->pat_packet_period - 1;
232 - ts->sdt_packet_count = ts->sdt_packet_period - 1;
234 + service->pcr_packet_timer = 0;
235 + ts->pat_packet_timer = 0;
236 + ts->sdt_packet_timer = 0;
238 if (ts->mux_rate == 1)
239 av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
241 av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
242 - av_log(s, AV_LOG_VERBOSE,
243 - "pcr every %d pkts, sdt every %d, pat/pmt every %d pkts\n",
244 - service->pcr_packet_period,
245 - ts->sdt_packet_period, ts->pat_packet_period);
247 - if (ts->m2ts_mode == -1) {
248 - if (av_match_ext(s->url, "m2ts")) {
257 @@ -1032,22 +1003,12 @@
258 MpegTSWrite *ts = s->priv_data;
261 - if (++ts->sdt_packet_count == ts->sdt_packet_period ||
262 - (dts != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
263 - (dts != AV_NOPTS_VALUE && dts - ts->last_sdt_ts >= ts->sdt_period*90000.0)
265 - ts->sdt_packet_count = 0;
266 - if (dts != AV_NOPTS_VALUE)
267 - ts->last_sdt_ts = FFMAX(dts, ts->last_sdt_ts);
268 + if ( ts->sdt_packet_period >= 0 && ts->pcr >= ts->sdt_packet_timer ) {
269 + ts->sdt_packet_timer = ts->pcr + ts->sdt_packet_period;
272 - if (++ts->pat_packet_count == ts->pat_packet_period ||
273 - (dts != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) ||
274 - (dts != AV_NOPTS_VALUE && dts - ts->last_pat_ts >= ts->pat_period*90000.0) ||
276 - ts->pat_packet_count = 0;
277 - if (dts != AV_NOPTS_VALUE)
278 - ts->last_pat_ts = FFMAX(dts, ts->last_pat_ts);
279 + if (ts->pcr >= ts->pat_packet_timer || force_pat) {
280 + ts->pat_packet_timer = ts->pcr + ts->pat_packet_period;
282 for (i = 0; i < ts->nb_services; i++)
283 mpegts_write_pmt(s, ts->services[i]);
284 @@ -1089,13 +1050,14 @@
286 MpegTSWrite *ts = s->priv_data;
287 MpegTSWriteStream *ts_st = st->priv_data;
288 + uint32_t pcr_pid = ts_st->service->pcr_pid;
290 uint8_t buf[TS_PACKET_SIZE];
294 - *q++ = ts_st->pid >> 8;
296 + *q++ = pcr_pid >> 8;
298 *q++ = 0x20 | ts_st->cc; /* Adaptation only */
299 /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
300 *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
301 @@ -1106,7 +1068,7 @@
304 /* PCR coded into 6 bytes */
305 - q += write_pcr_bits(q, get_pcr(ts, s->pb));
306 + q += write_pcr_bits(q, ts->pcr);
309 memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
310 @@ -1175,8 +1137,6 @@
312 int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags;
313 int afc_len, stuffing_len;
314 - int64_t pcr = -1; /* avoid warning */
315 - int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
316 int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
318 av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);
319 @@ -1186,28 +1146,33 @@
322 while (payload_size > 0) {
323 + ts->pcr = ts->first_pcr + (ts->mux_rate == 1 ?
324 + (dts == AV_NOPTS_VALUE ? 0 : (dts - ts->delay) * 300) :
325 + // add 11, pcr references the last byte of program clock reference base
326 + av_rescale(avio_tell(s->pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate));
328 retransmit_si_info(s, force_pat, dts);
332 - if (ts_st->pid == ts_st->service->pcr_pid) {
333 - if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
334 - ts_st->service->pcr_packet_count++;
335 - if (ts_st->service->pcr_packet_count >=
336 - ts_st->service->pcr_packet_period) {
337 - ts_st->service->pcr_packet_count = 0;
338 + if (ts_st->pid == ts_st->service->pcr_sid) {
339 + if( ts->pcr >= ts_st->service->pcr_packet_timer ) {
340 + ts_st->service->pcr_packet_timer = ts->pcr + ts_st->service->pcr_packet_period;
345 + if (write_pcr && ts_st->service->pcr_sid != ts_st->service->pcr_pid) {
346 + mpegts_insert_pcr_only(s, st);
349 if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
350 - (dts - get_pcr(ts, s->pb) / 300) > delay) {
351 - /* pcr insert gets priority over null packet insert */
353 - mpegts_insert_pcr_only(s, st);
354 + (dts - ts->pcr / 300) > ts->delay) {
355 + /* pcr insert gets priority over null packet insert */
357 + mpegts_insert_pcr_only(s, st);
359 - mpegts_insert_null_packet(s);
360 - /* recalculate write_pcr and possibly retransmit si_info */
361 + mpegts_insert_null_packet(s);
362 + /* recalculate write_pcr and possibly retransimit si_info */
366 @@ -1217,6 +1182,10 @@
367 val = ts_st->pid >> 8;
370 + if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
371 + st->codecpar->codec_id == AV_CODEC_ID_AC3 &&
376 ts_st->cc = ts_st->cc + 1 & 0xf;
377 @@ -1228,7 +1197,7 @@
379 if (key && is_start && pts != AV_NOPTS_VALUE) {
380 // set Random Access for key frames
381 - if (ts_st->pid == ts_st->service->pcr_pid)
382 + if (ts_st->pid == ts_st->service->pcr_sid)
384 set_af_flag(buf, 0x40);
385 q = get_ts_payload_start(buf);
386 @@ -1236,14 +1205,10 @@
388 set_af_flag(buf, 0x10);
389 q = get_ts_payload_start(buf);
390 - // add 11, pcr references the last byte of program clock reference base
391 if (ts->mux_rate > 1)
392 - pcr = get_pcr(ts, s->pb);
394 - pcr = (dts - delay) * 300;
395 - if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
396 + if (dts != AV_NOPTS_VALUE && dts < ts->pcr / 300)
397 av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
398 - extend_af(buf, write_pcr_bits(q, pcr));
399 + extend_af(buf, write_pcr_bits(q, ts->pcr));
400 q = get_ts_payload_start(buf);
403 @@ -1344,11 +1309,13 @@
406 if (pts != AV_NOPTS_VALUE) {
407 - write_pts(q, flags >> 6, pts);
408 + int64_t ts_pts = pts + ts->ts_offset;
409 + write_pts(q, flags >> 6, ts_pts);
412 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
413 - write_pts(q, 1, dts);
414 + int64_t ts_dts = dts + ts->ts_offset;
415 + write_pts(q, 1, ts_dts);
418 if (pes_extension && st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {
419 @@ -1519,7 +1486,6 @@
420 uint8_t *data = NULL;
421 MpegTSWrite *ts = s->priv_data;
422 MpegTSWriteStream *ts_st = st->priv_data;
423 - const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
424 int64_t dts = pkt->dts, pts = pkt->pts;
425 int opus_samples = 0;
427 @@ -1540,16 +1506,15 @@
430 if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
431 - ts->pat_packet_count = ts->pat_packet_period - 1;
432 - ts->sdt_packet_count = ts->sdt_packet_period - 1;
433 + ts->pat_packet_timer = ts->sdt_packet_timer = 0;
434 ts->flags &= ~MPEGTS_FLAG_REEMIT_PAT_PMT;
437 if (ts->copyts < 1) {
438 if (pts != AV_NOPTS_VALUE)
440 + pts += 2*ts->delay;
441 if (dts != AV_NOPTS_VALUE)
443 + dts += 2*ts->delay;
446 if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
447 @@ -1737,7 +1702,7 @@
448 AVStream *st2 = s->streams[i];
449 MpegTSWriteStream *ts_st2 = st2->priv_data;
450 if ( ts_st2->payload_size
451 - && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > delay/2)) {
452 + && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > ts->delay)) {
453 mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
454 ts_st2->payload_pts, ts_st2->payload_dts,
455 ts_st2->payload_flags & AV_PKT_FLAG_KEY, stream_id);
456 @@ -1908,12 +1873,18 @@
457 { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
458 offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT,
459 { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
460 + { "mpegts_pcr_start_pid", "Set the first pid of the PCR.",
461 + offsetof(MpegTSWrite, pcr_start_pid), AV_OPT_TYPE_INT,
462 + { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
463 { "mpegts_start_pid", "Set the first pid.",
464 offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT,
465 { .i64 = 0x0100 }, 0x0010, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM },
466 { "mpegts_m2ts_mode", "Enable m2ts mode.",
467 offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_BOOL,
468 - { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
469 + { .i64 = -1 }, -1, 2, AV_OPT_FLAG_ENCODING_PARAM },
470 + { "mpegts_pcr_offset", "clock offset.",
471 + offsetof(MpegTSWrite, ts_offset), AV_OPT_TYPE_BOOL,
472 + { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
474 offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT,
475 { .i64 = 1 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
476 @@ -1951,15 +1922,15 @@
477 { "omit_video_pes_length", "Omit the PES packet length for video packets",
478 offsetof(MpegTSWrite, omit_video_pes_length), AV_OPT_TYPE_BOOL,
479 { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
480 - { "pcr_period", "PCR retransmission time in milliseconds",
481 - offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_INT,
482 - { .i64 = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
483 - { "pat_period", "PAT/PMT retransmission time limit in seconds",
484 + { "pcr_period", "PCR retransmission time limit in msecs",
485 + offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_DOUBLE,
486 + { .dbl = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
487 + { "pat_period", "PAT/PMT retransmission time limit in msecs",
488 offsetof(MpegTSWrite, pat_period), AV_OPT_TYPE_DOUBLE,
489 - { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
490 - { "sdt_period", "SDT retransmission time limit in seconds",
491 + { .dbl = PAT_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
492 + { "sdt_period", "SDT retransmission time limit in msecs",
493 offsetof(MpegTSWrite, sdt_period), AV_OPT_TYPE_DOUBLE,
494 - { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
495 + { .dbl = SDT_RETRANS_TIME }, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },