1 diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
2 index fc0ea225c6..c702bc5f07 100644
3 --- a/libavformat/mpegtsenc.c
4 +++ b/libavformat/mpegtsenc.c
5 @@ -56,9 +56,8 @@ typedef struct MpegTSService {
6 int sid; /* service ID */
8 uint8_t provider_name[256];
10 - int pcr_packet_count;
11 - int pcr_packet_period;
12 + int64_t pcr, pcr_packet_timer, pcr_packet_period;
13 + int pcr_sid, pcr_pid;
17 @@ -78,14 +77,12 @@ typedef struct MpegTSWrite {
18 MpegTSSection pat; /* MPEG-2 PAT table */
19 MpegTSSection sdt; /* MPEG-2 SDT table context */
20 MpegTSService **services;
21 - int sdt_packet_count;
22 - int sdt_packet_period;
23 - int pat_packet_count;
24 - int pat_packet_period;
25 + int64_t sdt_packet_timer, sdt_packet_period;
26 + int64_t pat_packet_timer, pat_packet_period;
31 + int64_t pcr, first_pcr, delay;
32 int mux_rate; ///< set to 1 when VBR
35 @@ -95,12 +92,14 @@ typedef struct MpegTSWrite {
44 int reemit_pat_pmt; // backward compatibility
48 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
49 #define MPEGTS_FLAG_AAC_LATM 0x02
50 #define MPEGTS_FLAG_PAT_PMT_AT_FRAMES 0x04
51 @@ -111,8 +110,6 @@ typedef struct MpegTSWrite {
55 - int64_t last_pat_ts;
56 - int64_t last_sdt_ts;
58 int omit_video_pes_length;
60 @@ -222,10 +219,10 @@ static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
61 #define DEFAULT_PROVIDER_NAME "FFmpeg"
62 #define DEFAULT_SERVICE_NAME "Service01"
64 -/* we retransmit the SI info at this rate */
65 +/* we retransmit the SI info at this rate (ms) */
66 #define SDT_RETRANS_TIME 500
67 #define PAT_RETRANS_TIME 100
68 -#define PCR_RETRANS_TIME 20
69 +#define PCR_RETRANS_TIME 50
71 typedef struct MpegTSWriteStream {
72 struct MpegTSService *service;
73 @@ -730,6 +727,7 @@ static MpegTSService *mpegts_add_service(AVFormatContext *s, int sid,
74 service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
76 service->pcr_pid = 0x1fff;
77 + service->pcr_sid = 0x1fff;
78 if (encode_str8(service->provider_name, provider_name) < 0 ||
79 encode_str8(service->name, name) < 0) {
80 av_log(s, AV_LOG_ERROR, "Too long service or provider name\n");
81 @@ -744,18 +742,11 @@ fail:
85 -static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
87 - return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
91 static void mpegts_prefix_m2ts_header(AVFormatContext *s)
93 MpegTSWrite *ts = s->priv_data;
95 - int64_t pcr = get_pcr(s->priv_data, s->pb);
96 - uint32_t tp_extra_header = pcr % 0x3fffffff;
97 + uint32_t tp_extra_header = ts->pcr % 0x3fffffff;
98 tp_extra_header = AV_RB32(&tp_extra_header);
99 avio_write(s->pb, (unsigned char *) &tp_extra_header,
100 sizeof(tp_extra_header));
101 @@ -776,6 +767,7 @@ static int mpegts_init(AVFormatContext *s)
102 MpegTSService *service;
103 AVStream *st, *pcr_st = NULL;
104 AVDictionaryEntry *title, *provider;
107 const char *service_name;
108 const char *provider_name;
109 @@ -784,6 +776,15 @@ static int mpegts_init(AVFormatContext *s)
111 if (s->max_delay < 0) /* Not set by the caller */
113 + ts->delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
115 + if (ts->m2ts_mode == -1) {
116 + if (av_match_ext(s->url, "m2ts")) {
123 // round up to a whole number of TS packets
124 ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
125 @@ -830,6 +831,8 @@ static int mpegts_init(AVFormatContext *s)
126 service->program = program;
129 + if (ts->m2ts_mode > 1)
130 + service->pmt.pid = 0x00ff + ts->service_id;
132 ts->pat.pid = PAT_PID;
133 /* Initialize at 15 so that it wraps and is equal to 0 for the
134 @@ -915,10 +918,9 @@ static int mpegts_init(AVFormatContext *s)
135 ts_st->discontinuity = ts->flags & MPEGTS_FLAG_DISCONT;
136 /* update PCR pid by using the first video stream */
137 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
138 - service->pcr_pid == 0x1fff) {
139 - service->pcr_pid = ts_st->pid;
140 + service->pcr_sid == 0x1fff)
144 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
145 st->codecpar->extradata_size > 0) {
147 @@ -954,78 +956,47 @@ static int mpegts_init(AVFormatContext *s)
150 /* if no video stream, use the first stream as PCR */
151 - if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
152 - pcr_st = s->streams[0];
153 - ts_st = pcr_st->priv_data;
154 - service->pcr_pid = ts_st->pid;
156 - ts_st = pcr_st->priv_data;
158 - if (ts->mux_rate > 1) {
159 - service->pcr_packet_period = (int64_t)ts->mux_rate * ts->pcr_period /
160 - (TS_PACKET_SIZE * 8 * 1000);
161 - ts->sdt_packet_period = (int64_t)ts->mux_rate * SDT_RETRANS_TIME /
162 - (TS_PACKET_SIZE * 8 * 1000);
163 - ts->pat_packet_period = (int64_t)ts->mux_rate * PAT_RETRANS_TIME /
164 - (TS_PACKET_SIZE * 8 * 1000);
166 - if (ts->copyts < 1)
167 - ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
169 - /* Arbitrary values, PAT/PMT will also be written on video key frames */
170 - ts->sdt_packet_period = 200;
171 - ts->pat_packet_period = 40;
172 - if (pcr_st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
173 - int frame_size = av_get_audio_frame_duration2(pcr_st->codecpar, 0);
175 - av_log(s, AV_LOG_WARNING, "frame size not set\n");
176 - service->pcr_packet_period =
177 - pcr_st->codecpar->sample_rate / (10 * 512);
179 - service->pcr_packet_period =
180 - pcr_st->codecpar->sample_rate / (10 * frame_size);
183 - // max delta PCR 0.1s
184 - // TODO: should be avg_frame_rate
185 - service->pcr_packet_period =
186 - ts_st->user_tb.den / (10 * ts_st->user_tb.num);
188 - if (!service->pcr_packet_period)
189 - service->pcr_packet_period = 1;
192 - ts->last_pat_ts = AV_NOPTS_VALUE;
193 - ts->last_sdt_ts = AV_NOPTS_VALUE;
194 - // The user specified a period, use only it
195 - if (ts->pat_period < INT_MAX/2) {
196 - ts->pat_packet_period = INT_MAX;
197 + if (!pcr_st && s->nb_streams > 0)
198 + pcr_st = s->streams[0];
200 + av_log(s, AV_LOG_ERROR, "no streams\n");
201 + ret = AVERROR(EINVAL);
204 - if (ts->sdt_period < INT_MAX/2) {
205 - ts->sdt_packet_period = INT_MAX;
206 + ts_st = pcr_st->priv_data;
207 + if (service->pcr_sid == 0x1fff)
208 + service->pcr_sid = ts_st->pid;
209 + if (service->pcr_pid == 0x1fff)
210 + service->pcr_pid = ts->m2ts_mode > 1 ?
211 + 0x1000 + ts->service_id : service->pcr_sid ;
212 + if (service->pmt.pid == service->pcr_pid) {
213 + av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", service->pcr_pid);
214 + ret = AVERROR(EINVAL);
218 + clk_rate = ts->mux_rate > 1 ? ts->mux_rate : PCR_TIME_BASE;
219 + ts->sdt_packet_period = ts->sdt_period < 0 ? -1 : ts->sdt_period/1000 * clk_rate;
220 + ts->pat_packet_period = ts->pat_period/1000 * clk_rate;
221 + service->pcr_packet_period = ts->pcr_period/1000 * clk_rate;
222 + if (service->pcr_packet_period < (TS_PACKET_SIZE*8*10))
223 + service->pcr_packet_period = (TS_PACKET_SIZE*8*10);
224 + av_log(s, AV_LOG_VERBOSE, "clk_rate %f: ticks/pkt %d pcr, %d sdt, %d pmt\n", clk_rate,
225 + (int)service->pcr_packet_period, (int)ts->sdt_packet_period, (int)ts->pat_packet_period);
227 + if (ts->copyts < 1)
228 + ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
230 // output a PCR as soon as possible
231 - service->pcr_packet_count = service->pcr_packet_period;
232 - ts->pat_packet_count = ts->pat_packet_period - 1;
233 - ts->sdt_packet_count = ts->sdt_packet_period - 1;
235 + service->pcr_packet_timer = 0;
236 + ts->pat_packet_timer = 0;
237 + ts->sdt_packet_timer = 0;
239 if (ts->mux_rate == 1)
240 av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
242 av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
243 - av_log(s, AV_LOG_VERBOSE,
244 - "pcr every %d pkts, sdt every %d, pat/pmt every %d pkts\n",
245 - service->pcr_packet_period,
246 - ts->sdt_packet_period, ts->pat_packet_period);
248 - if (ts->m2ts_mode == -1) {
249 - if (av_match_ext(s->url, "m2ts")) {
258 @@ -1040,22 +1011,12 @@ static void retransmit_si_info(AVFormatContext *s, int force_pat, int64_t dts)
259 MpegTSWrite *ts = s->priv_data;
262 - if (++ts->sdt_packet_count == ts->sdt_packet_period ||
263 - (dts != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
264 - (dts != AV_NOPTS_VALUE && dts - ts->last_sdt_ts >= ts->sdt_period*90000.0)
266 - ts->sdt_packet_count = 0;
267 - if (dts != AV_NOPTS_VALUE)
268 - ts->last_sdt_ts = FFMAX(dts, ts->last_sdt_ts);
269 + if ( ts->sdt_packet_period >= 0 && ts->pcr >= ts->sdt_packet_timer ) {
270 + ts->sdt_packet_timer = ts->pcr + ts->sdt_packet_period;
273 - if (++ts->pat_packet_count == ts->pat_packet_period ||
274 - (dts != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) ||
275 - (dts != AV_NOPTS_VALUE && dts - ts->last_pat_ts >= ts->pat_period*90000.0) ||
277 - ts->pat_packet_count = 0;
278 - if (dts != AV_NOPTS_VALUE)
279 - ts->last_pat_ts = FFMAX(dts, ts->last_pat_ts);
280 + if (ts->pcr >= ts->pat_packet_timer || force_pat) {
281 + ts->pat_packet_timer = ts->pcr + ts->pat_packet_period;
283 for (i = 0; i < ts->nb_services; i++)
284 mpegts_write_pmt(s, ts->services[i]);
285 @@ -1097,13 +1058,14 @@ static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
287 MpegTSWrite *ts = s->priv_data;
288 MpegTSWriteStream *ts_st = st->priv_data;
289 + uint32_t pcr_pid = ts_st->service->pcr_pid;
291 uint8_t buf[TS_PACKET_SIZE];
295 - *q++ = ts_st->pid >> 8;
297 + *q++ = pcr_pid >> 8;
299 *q++ = 0x20 | ts_st->cc; /* Adaptation only */
300 /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
301 *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
302 @@ -1114,7 +1076,7 @@ static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
305 /* PCR coded into 6 bytes */
306 - q += write_pcr_bits(q, get_pcr(ts, s->pb));
307 + q += write_pcr_bits(q, ts->pcr);
310 memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
311 @@ -1183,8 +1145,6 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
313 int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags;
314 int afc_len, stuffing_len;
315 - int64_t pcr = -1; /* avoid warning */
316 - int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
317 int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
319 av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);
320 @@ -1194,28 +1154,33 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
323 while (payload_size > 0) {
324 + ts->pcr = ts->first_pcr + (ts->mux_rate == 1 ?
325 + (dts == AV_NOPTS_VALUE ? 0 : (dts - ts->delay) * 300) :
326 + // add 11, pcr references the last byte of program clock reference base
327 + av_rescale(avio_tell(s->pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate));
329 retransmit_si_info(s, force_pat, dts);
333 - if (ts_st->pid == ts_st->service->pcr_pid) {
334 - if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
335 - ts_st->service->pcr_packet_count++;
336 - if (ts_st->service->pcr_packet_count >=
337 - ts_st->service->pcr_packet_period) {
338 - ts_st->service->pcr_packet_count = 0;
339 + if (ts_st->pid == ts_st->service->pcr_sid) {
340 + if( ts->pcr >= ts_st->service->pcr_packet_timer ) {
341 + ts_st->service->pcr_packet_timer = ts->pcr + ts_st->service->pcr_packet_period;
346 + if (write_pcr && ts_st->service->pcr_sid != ts_st->service->pcr_pid) {
347 + mpegts_insert_pcr_only(s, st);
350 if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
351 - (dts - get_pcr(ts, s->pb) / 300) > delay) {
352 - /* pcr insert gets priority over null packet insert */
354 - mpegts_insert_pcr_only(s, st);
355 + (dts - ts->pcr / 300) > ts->delay) {
356 + /* pcr insert gets priority over null packet insert */
358 + mpegts_insert_pcr_only(s, st);
360 - mpegts_insert_null_packet(s);
361 - /* recalculate write_pcr and possibly retransmit si_info */
362 + mpegts_insert_null_packet(s);
363 + /* recalculate write_pcr and possibly retransimit si_info */
367 @@ -1225,6 +1190,10 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
368 val = ts_st->pid >> 8;
371 + if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
372 + st->codecpar->codec_id == AV_CODEC_ID_AC3 &&
377 ts_st->cc = ts_st->cc + 1 & 0xf;
378 @@ -1236,7 +1205,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
380 if (key && is_start && pts != AV_NOPTS_VALUE) {
381 // set Random Access for key frames
382 - if (ts_st->pid == ts_st->service->pcr_pid)
383 + if (ts_st->pid == ts_st->service->pcr_sid)
385 set_af_flag(buf, 0x40);
386 q = get_ts_payload_start(buf);
387 @@ -1244,14 +1213,10 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
389 set_af_flag(buf, 0x10);
390 q = get_ts_payload_start(buf);
391 - // add 11, pcr references the last byte of program clock reference base
392 if (ts->mux_rate > 1)
393 - pcr = get_pcr(ts, s->pb);
395 - pcr = (dts - delay) * 300;
396 - if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
397 + if (dts != AV_NOPTS_VALUE && dts < ts->pcr / 300)
398 av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
399 - extend_af(buf, write_pcr_bits(q, pcr));
400 + extend_af(buf, write_pcr_bits(q, ts->pcr));
401 q = get_ts_payload_start(buf);
404 @@ -1352,11 +1317,13 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
407 if (pts != AV_NOPTS_VALUE) {
408 - write_pts(q, flags >> 6, pts);
409 + int64_t ts_pts = pts + ts->ts_offset;
410 + write_pts(q, flags >> 6, ts_pts);
413 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
414 - write_pts(q, 1, dts);
415 + int64_t ts_dts = dts + ts->ts_offset;
416 + write_pts(q, 1, ts_dts);
419 if (pes_extension && st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {
420 @@ -1527,7 +1494,6 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
421 uint8_t *data = NULL;
422 MpegTSWrite *ts = s->priv_data;
423 MpegTSWriteStream *ts_st = st->priv_data;
424 - const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
425 int64_t dts = pkt->dts, pts = pkt->pts;
426 int opus_samples = 0;
428 @@ -1548,16 +1514,15 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
431 if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
432 - ts->pat_packet_count = ts->pat_packet_period - 1;
433 - ts->sdt_packet_count = ts->sdt_packet_period - 1;
434 + ts->pat_packet_timer = ts->sdt_packet_timer = 0;
435 ts->flags &= ~MPEGTS_FLAG_REEMIT_PAT_PMT;
438 if (ts->copyts < 1) {
439 if (pts != AV_NOPTS_VALUE)
441 + pts += 2*ts->delay;
442 if (dts != AV_NOPTS_VALUE)
444 + dts += 2*ts->delay;
447 if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
448 @@ -1745,7 +1710,7 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
449 AVStream *st2 = s->streams[i];
450 MpegTSWriteStream *ts_st2 = st2->priv_data;
451 if ( ts_st2->payload_size
452 - && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > delay/2)) {
453 + && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > ts->delay)) {
454 mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
455 ts_st2->payload_pts, ts_st2->payload_dts,
456 ts_st2->payload_flags & AV_PKT_FLAG_KEY, stream_id);
457 @@ -1914,12 +1879,18 @@ static const AVOption options[] = {
458 { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
459 offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT,
460 { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
461 + { "mpegts_pcr_start_pid", "Set the first pid of the PCR.",
462 + offsetof(MpegTSWrite, pcr_start_pid), AV_OPT_TYPE_INT,
463 + { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
464 { "mpegts_start_pid", "Set the first pid.",
465 offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT,
466 { .i64 = 0x0100 }, 0x0010, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM },
467 { "mpegts_m2ts_mode", "Enable m2ts mode.",
468 offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_BOOL,
469 - { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
470 + { .i64 = -1 }, -1, 2, AV_OPT_FLAG_ENCODING_PARAM },
471 + { "mpegts_pcr_offset", "clock offset.",
472 + offsetof(MpegTSWrite, ts_offset), AV_OPT_TYPE_BOOL,
473 + { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
475 offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT,
476 { .i64 = 1 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
477 @@ -1957,15 +1928,15 @@ static const AVOption options[] = {
478 { "omit_video_pes_length", "Omit the PES packet length for video packets",
479 offsetof(MpegTSWrite, omit_video_pes_length), AV_OPT_TYPE_BOOL,
480 { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
481 - { "pcr_period", "PCR retransmission time in milliseconds",
482 - offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_INT,
483 - { .i64 = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
484 - { "pat_period", "PAT/PMT retransmission time limit in seconds",
485 + { "pcr_period", "PCR retransmission time limit in msecs",
486 + offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_DOUBLE,
487 + { .dbl = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
488 + { "pat_period", "PAT/PMT retransmission time limit in msecs",
489 offsetof(MpegTSWrite, pat_period), AV_OPT_TYPE_DOUBLE,
490 - { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
491 - { "sdt_period", "SDT retransmission time limit in seconds",
492 + { .dbl = PAT_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
493 + { "sdt_period", "SDT retransmission time limit in msecs",
494 offsetof(MpegTSWrite, sdt_period), AV_OPT_TYPE_DOUBLE,
495 - { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
496 + { .dbl = SDT_RETRANS_TIME }, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },