1 diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
2 index d827ba3e28..c8c6db979b 100644
3 --- a/libavformat/mpegtsenc.c
4 +++ b/libavformat/mpegtsenc.c
5 @@ -56,7 +56,8 @@ typedef struct MpegTSService {
6 int sid; /* service ID */
8 uint8_t provider_name[256];
10 + int64_t pcr, pcr_packet_timer, pcr_packet_period;
11 + int pcr_sid, pcr_pid;
15 @@ -76,11 +77,12 @@ typedef struct MpegTSWrite {
16 MpegTSSection pat; /* MPEG-2 PAT table */
17 MpegTSSection sdt; /* MPEG-2 SDT table context */
18 MpegTSService **services;
19 - int64_t sdt_period; /* SDT period in PCR time base */
20 - int64_t pat_period; /* PAT/PMT period in PCR time base */
21 + int64_t sdt_packet_timer, sdt_packet_period;
22 + int64_t pat_packet_timer, pat_packet_period;
28 + int64_t pcr, first_pcr, delay;
29 int mux_rate; ///< set to 1 when VBR
32 @@ -90,14 +92,14 @@ typedef struct MpegTSWrite {
41 - int m2ts_pgssub_pid;
42 - int m2ts_textsub_pid;
46 + int reemit_pat_pmt; // backward compatibility
49 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
50 #define MPEGTS_FLAG_AAC_LATM 0x02
51 #define MPEGTS_FLAG_PAT_PMT_AT_FRAMES 0x04
52 @@ -106,10 +108,8 @@ typedef struct MpegTSWrite {
56 - int64_t pat_period_us;
57 - int64_t sdt_period_us;
58 - int64_t last_pat_ts;
59 - int64_t last_sdt_ts;
63 int omit_video_pes_length;
65 @@ -217,14 +217,15 @@ static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
68 #define DEFAULT_PROVIDER_NAME "FFmpeg"
69 -#define DEFAULT_SERVICE_NAME "Service"
70 +#define DEFAULT_SERVICE_NAME "Service01"
72 -/* we retransmit the SI info at this rate */
73 +/* we retransmit the SI info at this rate (ms) */
74 #define SDT_RETRANS_TIME 500
75 #define PAT_RETRANS_TIME 100
76 -#define PCR_RETRANS_TIME 20
77 +#define PCR_RETRANS_TIME 50
79 typedef struct MpegTSWriteStream {
80 + struct MpegTSService *service;
81 int pid; /* stream associated pid */
84 @@ -236,10 +237,7 @@ typedef struct MpegTSWriteStream {
87 AVFormatContext *amux;
88 - int data_st_warning;
90 - int64_t pcr_period; /* PCR period in PCR time base */
95 int opus_queued_samples;
96 @@ -259,7 +257,7 @@ static void mpegts_write_pat(AVFormatContext *s)
97 put16(&q, service->sid);
98 put16(&q, 0xe000 | service->pmt.pid);
100 - mpegts_write_section1(&ts->pat, PAT_TID, ts->transport_stream_id, ts->tables_version, 0, 0,
101 + mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, ts->tables_version, 0, 0,
105 @@ -281,148 +279,6 @@ static void put_registration_descriptor(uint8_t **q_ptr, uint32_t tag)
109 -static int get_dvb_stream_type(AVFormatContext *s, AVStream *st)
111 - MpegTSWrite *ts = s->priv_data;
112 - MpegTSWriteStream *ts_st = st->priv_data;
115 - switch (st->codecpar->codec_id) {
116 - case AV_CODEC_ID_MPEG1VIDEO:
117 - case AV_CODEC_ID_MPEG2VIDEO:
118 - stream_type = STREAM_TYPE_VIDEO_MPEG2;
120 - case AV_CODEC_ID_MPEG4:
121 - stream_type = STREAM_TYPE_VIDEO_MPEG4;
123 - case AV_CODEC_ID_H264:
124 - stream_type = STREAM_TYPE_VIDEO_H264;
126 - case AV_CODEC_ID_HEVC:
127 - stream_type = STREAM_TYPE_VIDEO_HEVC;
129 - case AV_CODEC_ID_CAVS:
130 - stream_type = STREAM_TYPE_VIDEO_CAVS;
132 - case AV_CODEC_ID_DIRAC:
133 - stream_type = STREAM_TYPE_VIDEO_DIRAC;
135 - case AV_CODEC_ID_VC1:
136 - stream_type = STREAM_TYPE_VIDEO_VC1;
138 - case AV_CODEC_ID_MP2:
139 - case AV_CODEC_ID_MP3:
140 - if ( st->codecpar->sample_rate > 0
141 - && st->codecpar->sample_rate < 32000) {
142 - stream_type = STREAM_TYPE_AUDIO_MPEG2;
144 - stream_type = STREAM_TYPE_AUDIO_MPEG1;
147 - case AV_CODEC_ID_AAC:
148 - stream_type = (ts->flags & MPEGTS_FLAG_AAC_LATM)
149 - ? STREAM_TYPE_AUDIO_AAC_LATM
150 - : STREAM_TYPE_AUDIO_AAC;
152 - case AV_CODEC_ID_AAC_LATM:
153 - stream_type = STREAM_TYPE_AUDIO_AAC_LATM;
155 - case AV_CODEC_ID_AC3:
156 - stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
157 - ? STREAM_TYPE_PRIVATE_DATA
158 - : STREAM_TYPE_AUDIO_AC3;
160 - case AV_CODEC_ID_EAC3:
161 - stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
162 - ? STREAM_TYPE_PRIVATE_DATA
163 - : STREAM_TYPE_AUDIO_EAC3;
165 - case AV_CODEC_ID_DTS:
166 - stream_type = STREAM_TYPE_AUDIO_DTS;
168 - case AV_CODEC_ID_TRUEHD:
169 - stream_type = STREAM_TYPE_AUDIO_TRUEHD;
171 - case AV_CODEC_ID_OPUS:
172 - stream_type = STREAM_TYPE_PRIVATE_DATA;
174 - case AV_CODEC_ID_TIMED_ID3:
175 - stream_type = STREAM_TYPE_METADATA;
177 - case AV_CODEC_ID_DVB_SUBTITLE:
178 - case AV_CODEC_ID_DVB_TELETEXT:
179 - stream_type = STREAM_TYPE_PRIVATE_DATA;
181 - case AV_CODEC_ID_SMPTE_KLV:
182 - if (st->codecpar->profile == FF_PROFILE_KLVA_SYNC) {
183 - stream_type = STREAM_TYPE_METADATA;
185 - stream_type = STREAM_TYPE_PRIVATE_DATA;
189 - av_log_once(s, AV_LOG_WARNING, AV_LOG_DEBUG, &ts_st->data_st_warning,
190 - "Stream %d, codec %s, is muxed as a private data stream "
191 - "and may not be recognized upon reading.\n", st->index,
192 - avcodec_get_name(st->codecpar->codec_id));
193 - stream_type = STREAM_TYPE_PRIVATE_DATA;
197 - return stream_type;
200 -static int get_m2ts_stream_type(AVFormatContext *s, AVStream *st)
203 - MpegTSWriteStream *ts_st = st->priv_data;
205 - switch (st->codecpar->codec_id) {
206 - case AV_CODEC_ID_MPEG2VIDEO:
207 - stream_type = STREAM_TYPE_VIDEO_MPEG2;
209 - case AV_CODEC_ID_H264:
210 - stream_type = STREAM_TYPE_VIDEO_H264;
212 - case AV_CODEC_ID_VC1:
213 - stream_type = STREAM_TYPE_VIDEO_VC1;
215 - case AV_CODEC_ID_HEVC:
216 - stream_type = STREAM_TYPE_VIDEO_HEVC;
218 - case AV_CODEC_ID_PCM_BLURAY:
219 - stream_type = 0x80;
221 - case AV_CODEC_ID_AC3:
222 - stream_type = 0x81;
224 - case AV_CODEC_ID_DTS:
225 - stream_type = (st->codecpar->channels > 6) ? 0x85 : 0x82;
227 - case AV_CODEC_ID_TRUEHD:
228 - stream_type = 0x83;
230 - case AV_CODEC_ID_EAC3:
231 - stream_type = 0x84;
233 - case AV_CODEC_ID_HDMV_PGS_SUBTITLE:
234 - stream_type = 0x90;
236 - case AV_CODEC_ID_HDMV_TEXT_SUBTITLE:
237 - stream_type = 0x92;
240 - av_log_once(s, AV_LOG_WARNING, AV_LOG_DEBUG, &ts_st->data_st_warning,
241 - "Stream %d, codec %s, is muxed as a private data stream "
242 - "and may not be recognized upon reading.\n", st->index,
243 - avcodec_get_name(st->codecpar->codec_id));
244 - stream_type = STREAM_TYPE_PRIVATE_DATA;
248 - return stream_type;
251 static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
253 MpegTSWrite *ts = s->priv_data;
254 @@ -436,14 +292,6 @@ static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
255 q += 2; /* patched after */
257 /* put program info here */
258 - if (ts->m2ts_mode) {
259 - put_registration_descriptor(&q, MKTAG('H', 'D', 'M', 'V'));
260 - *q++ = 0x88; // descriptor_tag - hdmv_copy_control_descriptor
261 - *q++ = 0x04; // descriptor_length
262 - put16(&q, 0x0fff); // CA_System_ID
263 - *q++ = 0xfc; // private_data_byte
264 - *q++ = 0xfc; // private_data_byte
267 val = 0xf000 | (q - program_info_length_ptr - 2);
268 program_info_length_ptr[0] = val >> 8;
269 @@ -472,8 +320,72 @@ static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
274 - stream_type = ts->m2ts_mode ? get_m2ts_stream_type(s, st) : get_dvb_stream_type(s, st);
275 + switch (st->codecpar->codec_id) {
276 + case AV_CODEC_ID_MPEG1VIDEO:
277 + case AV_CODEC_ID_MPEG2VIDEO:
278 + stream_type = STREAM_TYPE_VIDEO_MPEG2;
280 + case AV_CODEC_ID_MPEG4:
281 + stream_type = STREAM_TYPE_VIDEO_MPEG4;
283 + case AV_CODEC_ID_H264:
284 + stream_type = STREAM_TYPE_VIDEO_H264;
286 + case AV_CODEC_ID_HEVC:
287 + stream_type = STREAM_TYPE_VIDEO_HEVC;
289 + case AV_CODEC_ID_CAVS:
290 + stream_type = STREAM_TYPE_VIDEO_CAVS;
292 + case AV_CODEC_ID_DIRAC:
293 + stream_type = STREAM_TYPE_VIDEO_DIRAC;
295 + case AV_CODEC_ID_VC1:
296 + stream_type = STREAM_TYPE_VIDEO_VC1;
298 + case AV_CODEC_ID_MP2:
299 + case AV_CODEC_ID_MP3:
300 + if ( st->codecpar->sample_rate > 0
301 + && st->codecpar->sample_rate < 32000) {
302 + stream_type = STREAM_TYPE_AUDIO_MPEG2;
304 + stream_type = STREAM_TYPE_AUDIO_MPEG1;
307 + case AV_CODEC_ID_AAC:
308 + stream_type = (ts->flags & MPEGTS_FLAG_AAC_LATM)
309 + ? STREAM_TYPE_AUDIO_AAC_LATM
310 + : STREAM_TYPE_AUDIO_AAC;
312 + case AV_CODEC_ID_AAC_LATM:
313 + stream_type = STREAM_TYPE_AUDIO_AAC_LATM;
315 + case AV_CODEC_ID_AC3:
316 + stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
317 + ? STREAM_TYPE_PRIVATE_DATA
318 + : STREAM_TYPE_AUDIO_AC3;
320 + case AV_CODEC_ID_EAC3:
321 + stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
322 + ? STREAM_TYPE_PRIVATE_DATA
323 + : STREAM_TYPE_AUDIO_EAC3;
325 + case AV_CODEC_ID_DTS:
326 + stream_type = STREAM_TYPE_AUDIO_DTS;
328 + case AV_CODEC_ID_TRUEHD:
329 + stream_type = STREAM_TYPE_AUDIO_TRUEHD;
331 + case AV_CODEC_ID_OPUS:
332 + stream_type = STREAM_TYPE_PRIVATE_DATA;
334 + case AV_CODEC_ID_TIMED_ID3:
335 + stream_type = STREAM_TYPE_METADATA;
338 + stream_type = STREAM_TYPE_PRIVATE_DATA;
343 put16(&q, 0xe000 | ts_st->pid);
344 @@ -736,7 +648,7 @@ static void mpegts_write_sdt(AVFormatContext *s)
345 int i, running_status, free_ca_mode, val;
348 - put16(&q, ts->original_network_id);
349 + put16(&q, ts->onid);
351 for (i = 0; i < ts->nb_services; i++) {
352 service = ts->services[i];
353 @@ -762,7 +674,7 @@ static void mpegts_write_sdt(AVFormatContext *s)
354 desc_list_len_ptr[0] = val >> 8;
355 desc_list_len_ptr[1] = val;
357 - mpegts_write_section1(&ts->sdt, SDT_TID, ts->transport_stream_id, ts->tables_version, 0, 0,
358 + mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, ts->tables_version, 0, 0,
362 @@ -802,49 +714,12 @@ invalid:
366 -static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
368 - return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
372 -static void write_packet(AVFormatContext *s, const uint8_t *packet)
374 - MpegTSWrite *ts = s->priv_data;
375 - if (ts->m2ts_mode) {
376 - int64_t pcr = get_pcr(s->priv_data, s->pb);
377 - uint32_t tp_extra_header = pcr % 0x3fffffff;
378 - tp_extra_header = AV_RB32(&tp_extra_header);
379 - avio_write(s->pb, (unsigned char *) &tp_extra_header,
380 - sizeof(tp_extra_header));
382 - avio_write(s->pb, packet, TS_PACKET_SIZE);
385 -static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
387 - AVFormatContext *ctx = s->opaque;
388 - write_packet(ctx, packet);
391 static MpegTSService *mpegts_add_service(AVFormatContext *s, int sid,
392 - const AVDictionary *metadata,
393 - AVProgram *program)
394 + const char *provider_name,
397 MpegTSWrite *ts = s->priv_data;
398 MpegTSService *service;
399 - AVDictionaryEntry *title, *provider;
400 - char default_service_name[32];
401 - const char *service_name;
402 - const char *provider_name;
404 - title = av_dict_get(metadata, "service_name", NULL, 0);
406 - title = av_dict_get(metadata, "title", NULL, 0);
407 - snprintf(default_service_name, sizeof(default_service_name), "%s%02d", DEFAULT_SERVICE_NAME, ts->nb_services + 1);
408 - service_name = title ? title->value : default_service_name;
409 - provider = av_dict_get(metadata, "service_provider", NULL, 0);
410 - provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
412 service = av_mallocz(sizeof(MpegTSService));
414 @@ -852,92 +727,57 @@ static MpegTSService *mpegts_add_service(AVFormatContext *s, int sid,
415 service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
417 service->pcr_pid = 0x1fff;
418 + service->pcr_sid = 0x1fff;
419 if (encode_str8(service->provider_name, provider_name) < 0 ||
420 - encode_str8(service->name, service_name) < 0) {
421 + encode_str8(service->name, name) < 0) {
422 av_log(s, AV_LOG_ERROR, "Too long service or provider name\n");
425 if (av_dynarray_add_nofree(&ts->services, &ts->nb_services, service) < 0)
428 - service->pmt.write_packet = section_write_packet;
429 - service->pmt.opaque = s;
430 - service->pmt.cc = 15;
431 - service->pmt.discontinuity= ts->flags & MPEGTS_FLAG_DISCONT;
432 - service->program = program;
440 -static void enable_pcr_generation_for_stream(AVFormatContext *s, AVStream *pcr_st)
441 +static void mpegts_prefix_m2ts_header(AVFormatContext *s)
443 MpegTSWrite *ts = s->priv_data;
444 - MpegTSWriteStream *ts_st = pcr_st->priv_data;
446 - if (ts->mux_rate > 1 || ts->pcr_period_ms >= 0) {
447 - int pcr_period_ms = ts->pcr_period_ms == -1 ? PCR_RETRANS_TIME : ts->pcr_period_ms;
448 - ts_st->pcr_period = av_rescale(pcr_period_ms, PCR_TIME_BASE, 1000);
450 - /* By default, for VBR we select the highest multiple of frame duration which is less than 100 ms. */
451 - int64_t frame_period = 0;
452 - if (pcr_st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
453 - int frame_size = av_get_audio_frame_duration2(pcr_st->codecpar, 0);
455 - av_log(s, AV_LOG_WARNING, "frame size not set\n");
458 - frame_period = av_rescale_rnd(frame_size, PCR_TIME_BASE, pcr_st->codecpar->sample_rate, AV_ROUND_UP);
459 - } else if (pcr_st->avg_frame_rate.num) {
460 - frame_period = av_rescale_rnd(pcr_st->avg_frame_rate.den, PCR_TIME_BASE, pcr_st->avg_frame_rate.num, AV_ROUND_UP);
462 - if (frame_period > 0 && frame_period <= PCR_TIME_BASE / 10)
463 - ts_st->pcr_period = frame_period * (PCR_TIME_BASE / 10 / frame_period);
465 - ts_st->pcr_period = 1;
466 + if (ts->m2ts_mode) {
467 + uint32_t tp_extra_header = ts->pcr % 0x3fffffff;
468 + tp_extra_header = AV_RB32(&tp_extra_header);
469 + avio_write(s->pb, (unsigned char *) &tp_extra_header,
470 + sizeof(tp_extra_header));
473 - // output a PCR as soon as possible
474 - ts_st->last_pcr = ts->first_pcr - ts_st->pcr_period;
477 -static void select_pcr_streams(AVFormatContext *s)
478 +static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
480 - MpegTSWrite *ts = s->priv_data;
482 - for (int i = 0; i < ts->nb_services; i++) {
483 - MpegTSService *service = ts->services[i];
484 - AVStream *pcr_st = NULL;
485 - AVProgram *program = service->program;
486 - int nb_streams = program ? program->nb_stream_indexes : s->nb_streams;
488 - for (int j = 0; j < nb_streams; j++) {
489 - AVStream *st = s->streams[program ? program->stream_index[j] : j];
491 - pcr_st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
498 - MpegTSWriteStream *ts_st = pcr_st->priv_data;
499 - service->pcr_pid = ts_st->pid;
500 - enable_pcr_generation_for_stream(s, pcr_st);
501 - av_log(s, AV_LOG_VERBOSE, "service %i using PCR in pid=%i, pcr_period=%"PRId64"ms\n",
502 - service->sid, service->pcr_pid, av_rescale(ts_st->pcr_period, 1000, PCR_TIME_BASE));
505 + AVFormatContext *ctx = s->opaque;
506 + mpegts_prefix_m2ts_header(ctx);
507 + avio_write(ctx->pb, packet, TS_PACKET_SIZE);
510 static int mpegts_init(AVFormatContext *s)
512 MpegTSWrite *ts = s->priv_data;
513 + MpegTSWriteStream *ts_st;
514 + MpegTSService *service;
515 + AVStream *st, *pcr_st = NULL;
516 + AVDictionaryEntry *title, *provider;
519 + const char *service_name;
520 + const char *provider_name;
524 + if (s->max_delay < 0) /* Not set by the caller */
526 + ts->delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
528 if (ts->m2ts_mode == -1) {
529 if (av_match_ext(s->url, "m2ts")) {
531 @@ -946,36 +786,53 @@ static int mpegts_init(AVFormatContext *s)
535 - ts->m2ts_video_pid = M2TS_VIDEO_PID;
536 - ts->m2ts_audio_pid = M2TS_AUDIO_START_PID;
537 - ts->m2ts_pgssub_pid = M2TS_PGSSUB_START_PID;
538 - ts->m2ts_textsub_pid = M2TS_TEXTSUB_PID;
540 - if (ts->m2ts_mode) {
541 - ts->pmt_start_pid = M2TS_PMT_PID;
542 - if (s->nb_programs > 1) {
543 - av_log(s, AV_LOG_ERROR, "Only one program is allowed in m2ts mode!\n");
544 - return AVERROR(EINVAL);
548 - if (s->max_delay < 0) /* Not set by the caller */
551 // round up to a whole number of TS packets
552 ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
554 + ts->tsid = ts->transport_stream_id;
555 + ts->onid = ts->original_network_id;
556 if (!s->nb_programs) {
557 /* allocate a single DVB service */
558 - if (!mpegts_add_service(s, ts->service_id, s->metadata, NULL))
559 + title = av_dict_get(s->metadata, "service_name", NULL, 0);
561 + title = av_dict_get(s->metadata, "title", NULL, 0);
562 + service_name = title ? title->value : DEFAULT_SERVICE_NAME;
563 + provider = av_dict_get(s->metadata, "service_provider", NULL, 0);
564 + provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
565 + service = mpegts_add_service(s, ts->service_id,
566 + provider_name, service_name);
569 return AVERROR(ENOMEM);
571 + service->pmt.write_packet = section_write_packet;
572 + service->pmt.opaque = s;
573 + service->pmt.cc = 15;
574 + service->pmt.discontinuity= ts->flags & MPEGTS_FLAG_DISCONT;
576 for (i = 0; i < s->nb_programs; i++) {
577 AVProgram *program = s->programs[i];
578 - if (!mpegts_add_service(s, program->id, program->metadata, program))
579 + title = av_dict_get(program->metadata, "service_name", NULL, 0);
581 + title = av_dict_get(program->metadata, "title", NULL, 0);
582 + service_name = title ? title->value : DEFAULT_SERVICE_NAME;
583 + provider = av_dict_get(program->metadata, "service_provider", NULL, 0);
584 + provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
585 + service = mpegts_add_service(s, program->id,
586 + provider_name, service_name);
589 return AVERROR(ENOMEM);
591 + service->pmt.write_packet = section_write_packet;
592 + service->pmt.opaque = s;
593 + service->pmt.cc = 15;
594 + service->pmt.discontinuity= ts->flags & MPEGTS_FLAG_DISCONT;
595 + service->program = program;
598 + if (ts->m2ts_mode > 1)
599 + service->pmt.pid = 0x00ff + ts->service_id;
601 ts->pat.pid = PAT_PID;
602 /* Initialize at 15 so that it wraps and is equal to 0 for the
603 @@ -991,158 +848,175 @@ static int mpegts_init(AVFormatContext *s)
604 ts->sdt.write_packet = section_write_packet;
607 + pids = av_malloc_array(s->nb_streams, sizeof(*pids));
609 + ret = AVERROR(ENOMEM);
613 /* assign pids to each stream */
614 for (i = 0; i < s->nb_streams; i++) {
615 - AVStream *st = s->streams[i];
616 - MpegTSWriteStream *ts_st;
617 + AVProgram *program;
618 + st = s->streams[i];
620 ts_st = av_mallocz(sizeof(MpegTSWriteStream));
622 - return AVERROR(ENOMEM);
623 + ret = AVERROR(ENOMEM);
626 st->priv_data = ts_st;
628 + ts_st->user_tb = st->time_base;
629 avpriv_set_pts_info(st, 33, 1, 90000);
631 ts_st->payload = av_mallocz(ts->pes_payload_size);
632 if (!ts_st->payload) {
633 - return AVERROR(ENOMEM);
634 + ret = AVERROR(ENOMEM);
638 - /* MPEG pid values < 16 are reserved. Applications which set st->id in
639 - * this range are assigned a calculated pid. */
641 - if (ts->m2ts_mode) {
642 - switch (st->codecpar->codec_type) {
643 - case AVMEDIA_TYPE_VIDEO:
644 - ts_st->pid = ts->m2ts_video_pid++;
646 - case AVMEDIA_TYPE_AUDIO:
647 - ts_st->pid = ts->m2ts_audio_pid++;
649 - case AVMEDIA_TYPE_SUBTITLE:
650 - switch (st->codecpar->codec_id) {
651 - case AV_CODEC_ID_HDMV_PGS_SUBTITLE:
652 - ts_st->pid = ts->m2ts_pgssub_pid++;
654 - case AV_CODEC_ID_HDMV_TEXT_SUBTITLE:
655 - ts_st->pid = ts->m2ts_textsub_pid++;
658 + program = av_find_program_from_stream(s, NULL, i);
660 + for (j = 0; j < ts->nb_services; j++) {
661 + if (ts->services[j]->program == program) {
662 + service = ts->services[j];
665 - if (ts->m2ts_video_pid > M2TS_VIDEO_PID + 1 ||
666 - ts->m2ts_audio_pid > M2TS_AUDIO_START_PID + 32 ||
667 - ts->m2ts_pgssub_pid > M2TS_PGSSUB_START_PID + 32 ||
668 - ts->m2ts_textsub_pid > M2TS_TEXTSUB_PID + 1 ||
670 - av_log(s, AV_LOG_ERROR, "Cannot automatically assign PID for stream %d\n", st->index);
671 - return AVERROR(EINVAL);
674 - ts_st->pid = ts->start_pid + i;
677 - ts_st->pid = st->id;
679 - if (ts_st->pid >= 0x1FFF) {
681 + ts_st->service = service;
682 + /* MPEG pid values < 16 are reserved. Applications which set st->id in
683 + * this range are assigned a calculated pid. */
685 + ts_st->pid = ts->start_pid + i;
686 + } else if (st->id < 0x1FFF) {
687 + ts_st->pid = st->id;
689 av_log(s, AV_LOG_ERROR,
690 "Invalid stream id %d, must be less than 8191\n", st->id);
691 - return AVERROR(EINVAL);
692 + ret = AVERROR(EINVAL);
695 - for (j = 0; j < ts->nb_services; j++) {
696 - if (ts->services[j]->pmt.pid > LAST_OTHER_PID) {
697 - av_log(s, AV_LOG_ERROR,
698 - "Invalid PMT PID %d, must be less than %d\n", ts->services[j]->pmt.pid, LAST_OTHER_PID + 1);
699 - return AVERROR(EINVAL);
701 - if (ts_st->pid == ts->services[j]->pmt.pid) {
702 - av_log(s, AV_LOG_ERROR, "PID %d cannot be both elementary and PMT PID\n", ts_st->pid);
703 - return AVERROR(EINVAL);
705 + if (ts_st->pid == service->pmt.pid) {
706 + av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
707 + ret = AVERROR(EINVAL);
710 for (j = 0; j < i; j++) {
711 - MpegTSWriteStream *ts_st_prev = s->streams[j]->priv_data;
712 - if (ts_st_prev->pid == ts_st->pid) {
713 + if (pids[j] == ts_st->pid) {
714 av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
715 - return AVERROR(EINVAL);
716 + ret = AVERROR(EINVAL);
720 + pids[i] = ts_st->pid;
721 ts_st->payload_pts = AV_NOPTS_VALUE;
722 ts_st->payload_dts = AV_NOPTS_VALUE;
723 ts_st->first_pts_check = 1;
725 ts_st->discontinuity = ts->flags & MPEGTS_FLAG_DISCONT;
726 + /* update PCR pid by using the first video stream */
727 + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
728 + service->pcr_sid == 0x1fff)
731 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
732 st->codecpar->extradata_size > 0) {
734 ts_st->amux = avformat_alloc_context();
736 - return AVERROR(ENOMEM);
737 + ret = AVERROR(ENOMEM);
740 ts_st->amux->oformat =
741 av_guess_format((ts->flags & MPEGTS_FLAG_AAC_LATM) ? "latm" : "adts",
743 if (!ts_st->amux->oformat) {
744 - return AVERROR(EINVAL);
745 + ret = AVERROR(EINVAL);
748 if (!(ast = avformat_new_stream(ts_st->amux, NULL))) {
749 - return AVERROR(ENOMEM);
750 + ret = AVERROR(ENOMEM);
753 ret = avcodec_parameters_copy(ast->codecpar, st->codecpar);
757 ast->time_base = st->time_base;
758 ret = avformat_write_header(ts_st->amux, NULL);
763 if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
764 ts_st->opus_pending_trim_start = st->codecpar->initial_padding * 48000 / st->codecpar->sample_rate;
770 + /* if no video stream, use the first stream as PCR */
771 + if (!pcr_st && s->nb_streams > 0)
772 + pcr_st = s->streams[0];
774 + av_log(s, AV_LOG_ERROR, "no streams\n");
775 + ret = AVERROR(EINVAL);
778 + ts_st = pcr_st->priv_data;
779 + if (service->pcr_sid == 0x1fff)
780 + service->pcr_sid = ts_st->pid;
781 + if (service->pcr_pid == 0x1fff)
782 + service->pcr_pid = ts->m2ts_mode > 1 ?
783 + 0x1000 + ts->service_id : service->pcr_sid ;
784 + if (service->pmt.pid == service->pcr_pid) {
785 + av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", service->pcr_pid);
786 + ret = AVERROR(EINVAL);
790 + clk_rate = ts->mux_rate > 1 ? ts->mux_rate : PCR_TIME_BASE;
791 + ts->sdt_packet_period = ts->sdt_period < 0 ? -1 : ts->sdt_period/1000 * clk_rate;
792 + ts->pat_packet_period = ts->pat_period/1000 * clk_rate;
793 + service->pcr_packet_period = ts->pcr_period/1000 * clk_rate;
794 + if (service->pcr_packet_period < (TS_PACKET_SIZE*8*10))
795 + service->pcr_packet_period = (TS_PACKET_SIZE*8*10);
796 + av_log(s, AV_LOG_VERBOSE, "clk_rate %f: ticks/pkt %d pcr, %d sdt, %d pmt\n", clk_rate,
797 + (int)service->pcr_packet_period, (int)ts->sdt_packet_period, (int)ts->pat_packet_period);
800 ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
802 - select_pcr_streams(s);
804 - ts->last_pat_ts = AV_NOPTS_VALUE;
805 - ts->last_sdt_ts = AV_NOPTS_VALUE;
806 - ts->pat_period = av_rescale(ts->pat_period_us, PCR_TIME_BASE, AV_TIME_BASE);
807 - ts->sdt_period = av_rescale(ts->sdt_period_us, PCR_TIME_BASE, AV_TIME_BASE);
808 + // output a PCR as soon as possible
810 + service->pcr_packet_timer = 0;
811 + ts->pat_packet_timer = 0;
812 + ts->sdt_packet_timer = 0;
814 if (ts->mux_rate == 1)
815 av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
817 av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
818 - av_log(s, AV_LOG_VERBOSE,
819 - "sdt every %"PRId64" ms, pat/pmt every %"PRId64" ms\n",
820 - av_rescale(ts->sdt_period, 1000, PCR_TIME_BASE),
821 - av_rescale(ts->pat_period, 1000, PCR_TIME_BASE));
830 /* send SDT, PAT and PMT tables regularly */
831 -static void retransmit_si_info(AVFormatContext *s, int force_pat, int force_sdt, int64_t pcr)
832 +static void retransmit_si_info(AVFormatContext *s, int force_pat, int64_t dts)
834 MpegTSWrite *ts = s->priv_data;
837 - if ((pcr != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
838 - (pcr != AV_NOPTS_VALUE && pcr - ts->last_sdt_ts >= ts->sdt_period) ||
841 - if (pcr != AV_NOPTS_VALUE)
842 - ts->last_sdt_ts = FFMAX(pcr, ts->last_sdt_ts);
843 + if ( ts->sdt_packet_period >= 0 && ts->pcr >= ts->sdt_packet_timer ) {
844 + ts->sdt_packet_timer = ts->pcr + ts->sdt_packet_period;
847 - if ((pcr != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) ||
848 - (pcr != AV_NOPTS_VALUE && pcr - ts->last_pat_ts >= ts->pat_period) ||
850 - if (pcr != AV_NOPTS_VALUE)
851 - ts->last_pat_ts = FFMAX(pcr, ts->last_pat_ts);
852 + if (ts->pcr >= ts->pat_packet_timer || force_pat) {
853 + ts->pat_packet_timer = ts->pcr + ts->pat_packet_period;
855 for (i = 0; i < ts->nb_services; i++)
856 mpegts_write_pmt(s, ts->services[i]);
857 @@ -1175,7 +1049,8 @@ static void mpegts_insert_null_packet(AVFormatContext *s)
860 memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
861 - write_packet(s, buf);
862 + mpegts_prefix_m2ts_header(s);
863 + avio_write(s->pb, buf, TS_PACKET_SIZE);
866 /* Write a single transport stream packet with a PCR and no payload */
867 @@ -1183,13 +1058,14 @@ static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
869 MpegTSWrite *ts = s->priv_data;
870 MpegTSWriteStream *ts_st = st->priv_data;
871 + uint32_t pcr_pid = ts_st->service->pcr_pid;
873 uint8_t buf[TS_PACKET_SIZE];
877 - *q++ = ts_st->pid >> 8;
879 + *q++ = pcr_pid >> 8;
881 *q++ = 0x20 | ts_st->cc; /* Adaptation only */
882 /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
883 *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
884 @@ -1200,11 +1076,12 @@ static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
887 /* PCR coded into 6 bytes */
888 - q += write_pcr_bits(q, get_pcr(ts, s->pb));
889 + q += write_pcr_bits(q, ts->pcr);
892 memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
893 - write_packet(s, buf);
894 + mpegts_prefix_m2ts_header(s);
895 + avio_write(s->pb, buf, TS_PACKET_SIZE);
898 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
899 @@ -1268,84 +1145,55 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
901 int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags;
902 int afc_len, stuffing_len;
903 - int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
904 int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
907 av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);
908 if (ts->flags & MPEGTS_FLAG_PAT_PMT_AT_FRAMES && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
912 - if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
915 - ts->flags &= ~MPEGTS_FLAG_REEMIT_PAT_PMT;
919 while (payload_size > 0) {
920 - int64_t pcr = AV_NOPTS_VALUE;
921 - if (ts->mux_rate > 1)
922 - pcr = get_pcr(ts, s->pb);
923 - else if (dts != AV_NOPTS_VALUE)
924 - pcr = (dts - delay) * 300;
925 + ts->pcr = ts->first_pcr + (ts->mux_rate == 1 ?
926 + (dts == AV_NOPTS_VALUE ? 0 : (dts - ts->delay) * 300) :
927 + // add 11, pcr references the last byte of program clock reference base
928 + av_rescale(avio_tell(s->pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate));
930 - retransmit_si_info(s, force_pat, force_sdt, pcr);
931 + retransmit_si_info(s, force_pat, dts);
936 - if (ts->mux_rate > 1) {
937 - /* Send PCR packets for all PCR streams if needed */
938 - pcr = get_pcr(ts, s->pb);
939 - if (pcr >= ts->next_pcr) {
940 - int64_t next_pcr = INT64_MAX;
941 - for (int i = 0; i < s->nb_streams; i++) {
942 - /* Make the current stream the last, because for that we
943 - * can insert the pcr into the payload later */
944 - int st2_index = i < st->index ? i : (i + 1 == s->nb_streams ? st->index : i + 1);
945 - AVStream *st2 = s->streams[st2_index];
946 - MpegTSWriteStream *ts_st2 = st2->priv_data;
947 - if (ts_st2->pcr_period) {
948 - if (pcr - ts_st2->last_pcr >= ts_st2->pcr_period) {
949 - ts_st2->last_pcr = FFMAX(pcr - ts_st2->pcr_period, ts_st2->last_pcr + ts_st2->pcr_period);
951 - mpegts_insert_pcr_only(s, st2);
952 - pcr = get_pcr(ts, s->pb);
957 - next_pcr = FFMIN(next_pcr, ts_st2->last_pcr + ts_st2->pcr_period);
960 - ts->next_pcr = next_pcr;
962 - if (dts != AV_NOPTS_VALUE && (dts - pcr / 300) > delay) {
963 - /* pcr insert gets priority over null packet insert */
965 - mpegts_insert_pcr_only(s, st);
967 - mpegts_insert_null_packet(s);
968 - /* recalculate write_pcr and possibly retransmit si_info */
971 - } else if (ts_st->pcr_period && pcr != AV_NOPTS_VALUE) {
972 - if (pcr - ts_st->last_pcr >= ts_st->pcr_period && is_start) {
973 - ts_st->last_pcr = FFMAX(pcr - ts_st->pcr_period, ts_st->last_pcr + ts_st->pcr_period);
974 + if (ts_st->pid == ts_st->service->pcr_sid) {
975 + if( ts->pcr >= ts_st->service->pcr_packet_timer ) {
976 + ts_st->service->pcr_packet_timer = ts->pcr + ts_st->service->pcr_packet_period;
980 + if (write_pcr && ts_st->service->pcr_sid != ts_st->service->pcr_pid) {
981 + mpegts_insert_pcr_only(s, st);
984 + if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
985 + (dts - ts->pcr / 300) > ts->delay) {
986 + /* pcr insert gets priority over null packet insert */
988 + mpegts_insert_pcr_only(s, st);
990 + mpegts_insert_null_packet(s);
991 + /* recalculate write_pcr and possibly retransimit si_info */
995 /* prepare packet header */
998 val = ts_st->pid >> 8;
999 - if (ts->m2ts_mode && st->codecpar->codec_id == AV_CODEC_ID_AC3)
1003 + if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1004 + st->codecpar->codec_id == AV_CODEC_ID_AC3 &&
1005 + ts->m2ts_mode > 1)
1009 ts_st->cc = ts_st->cc + 1 & 0xf;
1010 @@ -1357,7 +1205,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
1012 if (key && is_start && pts != AV_NOPTS_VALUE) {
1013 // set Random Access for key frames
1014 - if (ts_st->pcr_period)
1015 + if (ts_st->pid == ts_st->service->pcr_sid)
1017 set_af_flag(buf, 0x40);
1018 q = get_ts_payload_start(buf);
1019 @@ -1365,10 +1213,10 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
1021 set_af_flag(buf, 0x10);
1022 q = get_ts_payload_start(buf);
1023 - // add 11, pcr references the last byte of program clock reference base
1024 - if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
1025 + if (ts->mux_rate > 1)
1026 + if (dts != AV_NOPTS_VALUE && dts < ts->pcr / 300)
1027 av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
1028 - extend_af(buf, write_pcr_bits(q, pcr));
1029 + extend_af(buf, write_pcr_bits(q, ts->pcr));
1030 q = get_ts_payload_start(buf);
1033 @@ -1439,10 +1287,10 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
1034 if (ts->m2ts_mode &&
1035 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1036 st->codecpar->codec_id == AV_CODEC_ID_AC3) {
1037 - /* set PES_extension_flag */
1038 - pes_extension = 1;
1041 + /* set PES_extension_flag */
1042 + pes_extension = 1;
1046 if (is_dvb_teletext) {
1047 pes_header_stuffing_bytes = 0x24 - header_len;
1048 @@ -1469,11 +1317,13 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
1051 if (pts != AV_NOPTS_VALUE) {
1052 - write_pts(q, flags >> 6, pts);
1053 + int64_t ts_pts = pts + ts->ts_offset;
1054 + write_pts(q, flags >> 6, ts_pts);
1057 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1058 - write_pts(q, 1, dts);
1059 + int64_t ts_dts = dts + ts->ts_offset;
1060 + write_pts(q, 1, ts_dts);
1063 if (pes_extension && st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {
1064 @@ -1485,14 +1335,14 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
1067 /* For Blu-ray AC3 Audio Setting extended flags */
1068 - if (ts->m2ts_mode &&
1070 - st->codecpar->codec_id == AV_CODEC_ID_AC3) {
1071 - flags = 0x01; /* set PES_extension_flag_2 */
1073 - *q++ = 0x80 | 0x01; /* marker bit + extension length */
1074 - *q++ = 0x00 | 0x71; /* for AC3 Audio (specifically on blue-rays) */
1076 + if (ts->m2ts_mode &&
1078 + st->codecpar->codec_id == AV_CODEC_ID_AC3) {
1079 + flags = 0x01; /* set PES_extension_flag_2 */
1081 + *q++ = 0x80 | 0x01; /* marker bit + extension length */
1082 + *q++ = 0x00 | 0x71; /* for AC3 Audio (specifically on blue-rays) */
1086 if (is_dvb_subtitle) {
1087 @@ -1546,7 +1396,8 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
1090 payload_size -= len;
1091 - write_packet(s, buf);
1092 + mpegts_prefix_m2ts_header(s);
1093 + avio_write(s->pb, buf, TS_PACKET_SIZE);
1095 ts_st->prev_payload_key = key;
1097 @@ -1643,8 +1494,6 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
1098 uint8_t *data = NULL;
1099 MpegTSWrite *ts = s->priv_data;
1100 MpegTSWriteStream *ts_st = st->priv_data;
1101 - const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
1102 - const int64_t max_audio_delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) / 2;
1103 int64_t dts = pkt->dts, pts = pkt->pts;
1104 int opus_samples = 0;
1106 @@ -1657,11 +1506,23 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
1108 stream_id = side_data[0];
1110 + if (ts->reemit_pat_pmt) {
1111 + av_log(s, AV_LOG_WARNING,
1112 + "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1113 + ts->reemit_pat_pmt = 0;
1114 + ts->flags |= MPEGTS_FLAG_REEMIT_PAT_PMT;
1117 + if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
1118 + ts->pat_packet_timer = ts->sdt_packet_timer = 0;
1119 + ts->flags &= ~MPEGTS_FLAG_REEMIT_PAT_PMT;
1122 if (ts->copyts < 1) {
1123 if (pts != AV_NOPTS_VALUE)
1125 + pts += 2*ts->delay;
1126 if (dts != AV_NOPTS_VALUE)
1128 + dts += 2*ts->delay;
1131 if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
1132 @@ -1724,7 +1585,7 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
1134 ret = avio_open_dyn_buf(&ts_st->amux->pb);
1137 + return AVERROR(ENOMEM);
1139 ret = av_write_frame(ts_st->amux, &pkt2);
1141 @@ -1755,7 +1616,7 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
1142 } while (p < buf_end && (state & 0x7e) != 2*35 &&
1143 (state & 0x7e) >= 2*32);
1145 - if ((state & 0x7e) < 2*16 || (state & 0x7e) >= 2*24)
1146 + if ((state & 0x7e) < 2*16 && (state & 0x7e) >= 2*24)
1148 if ((state & 0x7e) != 2*35) { // AUD NAL
1149 data = av_malloc(pkt->size + 7 + extradd);
1150 @@ -1843,9 +1704,25 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
1154 + if (pkt->dts != AV_NOPTS_VALUE) {
1156 + for(i=0; i<s->nb_streams; i++) {
1157 + AVStream *st2 = s->streams[i];
1158 + MpegTSWriteStream *ts_st2 = st2->priv_data;
1159 + if ( ts_st2->payload_size
1160 + && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > ts->delay)) {
1161 + mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
1162 + ts_st2->payload_pts, ts_st2->payload_dts,
1163 + ts_st2->payload_flags & AV_PKT_FLAG_KEY, stream_id);
1164 + ts_st2->payload_size = 0;
1169 if (ts_st->payload_size && (ts_st->payload_size + size > ts->pes_payload_size ||
1170 (dts != AV_NOPTS_VALUE && ts_st->payload_dts != AV_NOPTS_VALUE &&
1171 - dts - ts_st->payload_dts >= max_audio_delay) ||
1172 + av_compare_ts(dts - ts_st->payload_dts, st->time_base,
1173 + s->max_delay, AV_TIME_BASE_Q) >= 0) ||
1174 ts_st->opus_queued_samples + opus_samples >= 5760 /* 120ms */)) {
1175 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1176 ts_st->payload_pts, ts_st->payload_dts,
1177 @@ -1881,7 +1758,6 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
1179 static void mpegts_write_flush(AVFormatContext *s)
1181 - MpegTSWrite *ts = s->priv_data;
1184 /* flush current packets */
1185 @@ -1896,12 +1772,6 @@ static void mpegts_write_flush(AVFormatContext *s)
1186 ts_st->opus_queued_samples = 0;
1190 - if (ts->m2ts_mode) {
1191 - int packets = (avio_tell(s->pb) / (TS_PACKET_SIZE + 4)) % 32;
1192 - while (packets++ < 32)
1193 - mpegts_insert_null_packet(s);
1197 static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
1198 @@ -1969,62 +1839,104 @@ static int mpegts_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt
1202 -#define OFFSET(x) offsetof(MpegTSWrite, x)
1203 -#define ENC AV_OPT_FLAG_ENCODING_PARAM
1204 static const AVOption options[] = {
1205 { "mpegts_transport_stream_id", "Set transport_stream_id field.",
1206 - OFFSET(transport_stream_id), AV_OPT_TYPE_INT, { .i64 = 0x0001 }, 0x0001, 0xffff, ENC },
1207 + offsetof(MpegTSWrite, transport_stream_id), AV_OPT_TYPE_INT,
1208 + { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1209 { "mpegts_original_network_id", "Set original_network_id field.",
1210 - OFFSET(original_network_id), AV_OPT_TYPE_INT, { .i64 = DVB_PRIVATE_NETWORK_START }, 0x0001, 0xffff, ENC },
1211 + offsetof(MpegTSWrite, original_network_id), AV_OPT_TYPE_INT,
1212 + { .i64 = DVB_PRIVATE_NETWORK_START }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1213 { "mpegts_service_id", "Set service_id field.",
1214 - OFFSET(service_id), AV_OPT_TYPE_INT, { .i64 = 0x0001 }, 0x0001, 0xffff, ENC },
1215 + offsetof(MpegTSWrite, service_id), AV_OPT_TYPE_INT,
1216 + { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1217 { "mpegts_service_type", "Set service_type field.",
1218 - OFFSET(service_type), AV_OPT_TYPE_INT, { .i64 = 0x01 }, 0x01, 0xff, ENC, "mpegts_service_type" },
1219 + offsetof(MpegTSWrite, service_type), AV_OPT_TYPE_INT,
1220 + { .i64 = 0x01 }, 0x01, 0xff, AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1221 { "digital_tv", "Digital Television.",
1222 - 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_TV }, 0x01, 0xff, ENC, "mpegts_service_type" },
1223 + 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_TV }, 0x01, 0xff,
1224 + AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1225 { "digital_radio", "Digital Radio.",
1226 - 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_RADIO }, 0x01, 0xff, ENC, "mpegts_service_type" },
1227 + 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_RADIO }, 0x01, 0xff,
1228 + AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1229 { "teletext", "Teletext.",
1230 - 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_TELETEXT }, 0x01, 0xff, ENC, "mpegts_service_type" },
1231 + 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_TELETEXT }, 0x01, 0xff,
1232 + AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1233 { "advanced_codec_digital_radio", "Advanced Codec Digital Radio.",
1234 - 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO }, 0x01, 0xff, ENC, "mpegts_service_type" },
1235 + 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO }, 0x01, 0xff,
1236 + AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1237 { "mpeg2_digital_hdtv", "MPEG2 Digital HDTV.",
1238 - 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV }, 0x01, 0xff, ENC, "mpegts_service_type" },
1239 + 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV }, 0x01, 0xff,
1240 + AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1241 { "advanced_codec_digital_sdtv", "Advanced Codec Digital SDTV.",
1242 - 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV }, 0x01, 0xff, ENC, "mpegts_service_type" },
1243 + 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV }, 0x01, 0xff,
1244 + AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1245 { "advanced_codec_digital_hdtv", "Advanced Codec Digital HDTV.",
1246 - 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV }, 0x01, 0xff, ENC, "mpegts_service_type" },
1247 + 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV }, 0x01, 0xff,
1248 + AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1249 { "hevc_digital_hdtv", "HEVC Digital Television Service.",
1250 - 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV }, 0x01, 0xff, ENC, "mpegts_service_type" },
1251 + 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV }, 0x01, 0xff,
1252 + AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1253 { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
1254 - OFFSET(pmt_start_pid), AV_OPT_TYPE_INT, { .i64 = 0x1000 }, FIRST_OTHER_PID, LAST_OTHER_PID, ENC },
1255 + offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT,
1256 + { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
1257 + { "mpegts_pcr_start_pid", "Set the first pid of the PCR.",
1258 + offsetof(MpegTSWrite, pcr_start_pid), AV_OPT_TYPE_INT,
1259 + { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
1260 { "mpegts_start_pid", "Set the first pid.",
1261 - OFFSET(start_pid), AV_OPT_TYPE_INT, { .i64 = 0x0100 }, FIRST_OTHER_PID, LAST_OTHER_PID, ENC },
1262 - { "mpegts_m2ts_mode", "Enable m2ts mode.", OFFSET(m2ts_mode), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, ENC },
1263 - { "muxrate", NULL, OFFSET(mux_rate), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, ENC },
1264 + offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT,
1265 + { .i64 = 0x0100 }, 0x0010, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM },
1266 + { "mpegts_m2ts_mode", "Enable m2ts mode.",
1267 + offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_BOOL,
1268 + { .i64 = -1 }, -1, 2, AV_OPT_FLAG_ENCODING_PARAM },
1269 + { "mpegts_pcr_offset", "clock offset.",
1270 + offsetof(MpegTSWrite, ts_offset), AV_OPT_TYPE_BOOL,
1271 + { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1272 + { "muxrate", NULL,
1273 + offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT,
1274 + { .i64 = 1 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1275 { "pes_payload_size", "Minimum PES packet payload in bytes",
1276 - OFFSET(pes_payload_size), AV_OPT_TYPE_INT, { .i64 = DEFAULT_PES_PAYLOAD_SIZE }, 0, INT_MAX, ENC },
1277 - { "mpegts_flags", "MPEG-TS muxing flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX, ENC, "mpegts_flags" },
1278 + offsetof(MpegTSWrite, pes_payload_size), AV_OPT_TYPE_INT,
1279 + { .i64 = DEFAULT_PES_PAYLOAD_SIZE }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1280 + { "mpegts_flags", "MPEG-TS muxing flags",
1281 + offsetof(MpegTSWrite, flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX,
1282 + AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1283 { "resend_headers", "Reemit PAT/PMT before writing the next packet",
1284 - 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_REEMIT_PAT_PMT }, 0, INT_MAX, ENC, "mpegts_flags" },
1285 + 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_REEMIT_PAT_PMT }, 0, INT_MAX,
1286 + AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1287 { "latm", "Use LATM packetization for AAC",
1288 - 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_AAC_LATM }, 0, INT_MAX, ENC, "mpegts_flags" },
1289 + 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_AAC_LATM }, 0, INT_MAX,
1290 + AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1291 { "pat_pmt_at_frames", "Reemit PAT and PMT at each video frame",
1292 - 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_PAT_PMT_AT_FRAMES}, 0, INT_MAX, ENC, "mpegts_flags" },
1293 + 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_PAT_PMT_AT_FRAMES}, 0, INT_MAX,
1294 + AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1295 { "system_b", "Conform to System B (DVB) instead of System A (ATSC)",
1296 - 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_SYSTEM_B }, 0, INT_MAX, ENC, "mpegts_flags" },
1297 + 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_SYSTEM_B }, 0, INT_MAX,
1298 + AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1299 { "initial_discontinuity", "Mark initial packets as discontinuous",
1300 - 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_DISCONT }, 0, INT_MAX, ENC, "mpegts_flags" },
1301 - { "mpegts_copyts", "don't offset dts/pts", OFFSET(copyts), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, ENC },
1302 - { "tables_version", "set PAT, PMT and SDT version", OFFSET(tables_version), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 31, ENC },
1303 + 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_DISCONT }, 0, INT_MAX,
1304 + AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1305 + // backward compatibility
1306 + { "resend_headers", "Reemit PAT/PMT before writing the next packet",
1307 + offsetof(MpegTSWrite, reemit_pat_pmt), AV_OPT_TYPE_INT,
1308 + { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1309 + { "mpegts_copyts", "don't offset dts/pts",
1310 + offsetof(MpegTSWrite, copyts), AV_OPT_TYPE_BOOL,
1311 + { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
1312 + { "tables_version", "set PAT, PMT and SDT version",
1313 + offsetof(MpegTSWrite, tables_version), AV_OPT_TYPE_INT,
1314 + { .i64 = 0 }, 0, 31, AV_OPT_FLAG_ENCODING_PARAM },
1315 { "omit_video_pes_length", "Omit the PES packet length for video packets",
1316 - OFFSET(omit_video_pes_length), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, ENC },
1317 - { "pcr_period", "PCR retransmission time in milliseconds",
1318 - OFFSET(pcr_period_ms), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, ENC },
1319 - { "pat_period", "PAT/PMT retransmission time limit in seconds",
1320 - OFFSET(pat_period_us), AV_OPT_TYPE_DURATION, { .i64 = PAT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, ENC },
1321 - { "sdt_period", "SDT retransmission time limit in seconds",
1322 - OFFSET(sdt_period_us), AV_OPT_TYPE_DURATION, { .i64 = SDT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, ENC },
1323 + offsetof(MpegTSWrite, omit_video_pes_length), AV_OPT_TYPE_BOOL,
1324 + { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
1325 + { "pcr_period", "PCR retransmission time limit in msecs",
1326 + offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_DOUBLE,
1327 + { .dbl = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1328 + { "pat_period", "PAT/PMT retransmission time limit in msecs",
1329 + offsetof(MpegTSWrite, pat_period), AV_OPT_TYPE_DOUBLE,
1330 + { .dbl = PAT_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1331 + { "sdt_period", "SDT retransmission time limit in msecs",
1332 + offsetof(MpegTSWrite, sdt_period), AV_OPT_TYPE_DOUBLE,
1333 + { .dbl = SDT_RETRANS_TIME }, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },