2 * MPEG2 transport stream (aka DVB) muxer
3 * Copyright (c) 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avassert.h"
23 #include "libavutil/bswap.h"
24 #include "libavutil/crc.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/opt.h"
30 #include "libavcodec/internal.h"
33 #include "avio_internal.h"
37 #define PCR_TIME_BASE 27000000
39 /* write DVB SI sections */
41 /*********************************************/
42 /* mpegts section writer */
44 typedef struct MpegTSSection {
47 void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
51 typedef struct MpegTSService {
52 MpegTSSection pmt; /* MPEG2 pmt table context */
53 int sid; /* service ID */
58 int pcr_packet_period;
61 // service_type values as defined in ETSI 300 468
63 MPEGTS_SERVICE_TYPE_DIGITAL_TV = 0x01,
64 MPEGTS_SERVICE_TYPE_DIGITAL_RADIO = 0x02,
65 MPEGTS_SERVICE_TYPE_TELETEXT = 0x03,
66 MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO = 0x0A,
67 MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV = 0x11,
68 MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV = 0x16,
69 MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV = 0x19
71 typedef struct MpegTSWrite {
72 const AVClass *av_class;
73 MpegTSSection pat; /* MPEG2 pat table */
74 MpegTSSection sdt; /* MPEG2 sdt table context */
75 MpegTSService **services;
77 int sdt_packet_period;
79 int pat_packet_period;
84 int mux_rate; ///< set to 1 when VBR
87 int transport_stream_id;
88 int original_network_id;
96 int reemit_pat_pmt; // backward compatibility
99 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
100 #define MPEGTS_FLAG_AAC_LATM 0x02
105 int omit_video_pes_length;
108 /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
109 #define DEFAULT_PES_HEADER_FREQ 16
110 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
112 /* The section length is 12 bits. The first 2 are set to 0, the remaining
113 * 10 bits should not exceed 1021. */
114 #define SECTION_LENGTH 1020
116 /* NOTE: 4 bytes must be left at the end for the crc32 */
117 static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
120 unsigned char packet[TS_PACKET_SIZE];
121 const unsigned char *buf_ptr;
123 int first, b, len1, left;
125 crc = av_bswap32(av_crc(av_crc_get_table(AV_CRC_32_IEEE),
128 buf[len - 4] = (crc >> 24) & 0xff;
129 buf[len - 3] = (crc >> 16) & 0xff;
130 buf[len - 2] = (crc >> 8) & 0xff;
131 buf[len - 1] = crc & 0xff;
133 /* send each packet */
136 first = buf == buf_ptr;
144 s->cc = s->cc + 1 & 0xf;
147 *q++ = 0; /* 0 offset */
148 len1 = TS_PACKET_SIZE - (q - packet);
151 memcpy(q, buf_ptr, len1);
153 /* add known padding data */
154 left = TS_PACKET_SIZE - (q - packet);
156 memset(q, 0xff, left);
158 s->write_packet(s, packet);
165 static inline void put16(uint8_t **q_ptr, int val)
174 static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
175 int version, int sec_num, int last_sec_num,
176 uint8_t *buf, int len)
178 uint8_t section[1024], *q;
179 unsigned int tot_len;
180 /* reserved_future_use field must be set to 1 for SDT */
181 unsigned int flags = tid == SDT_TID ? 0xf000 : 0xb000;
183 tot_len = 3 + 5 + len + 4;
184 /* check if not too big */
186 return AVERROR_INVALIDDATA;
190 put16(&q, flags | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
192 *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
197 mpegts_write_section(s, section, tot_len);
201 /*********************************************/
204 #define DEFAULT_PROVIDER_NAME "FFmpeg"
205 #define DEFAULT_SERVICE_NAME "Service01"
207 /* we retransmit the SI info at this rate */
208 #define SDT_RETRANS_TIME 500
209 #define PAT_RETRANS_TIME 100
210 #define PCR_RETRANS_TIME 20
212 typedef struct MpegTSWriteStream {
213 struct MpegTSService *service;
214 int pid; /* stream associated pid */
217 int first_pts_check; ///< first pts check needed
218 int prev_payload_key;
223 AVFormatContext *amux;
227 static void mpegts_write_pat(AVFormatContext *s)
229 MpegTSWrite *ts = s->priv_data;
230 MpegTSService *service;
231 uint8_t data[SECTION_LENGTH], *q;
235 for (i = 0; i < ts->nb_services; i++) {
236 service = ts->services[i];
237 put16(&q, service->sid);
238 put16(&q, 0xe000 | service->pmt.pid);
240 mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, ts->tables_version, 0, 0,
244 static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
246 MpegTSWrite *ts = s->priv_data;
247 uint8_t data[SECTION_LENGTH], *q, *desc_length_ptr, *program_info_length_ptr;
248 int val, stream_type, i, err = 0;
251 put16(&q, 0xe000 | service->pcr_pid);
253 program_info_length_ptr = q;
254 q += 2; /* patched after */
256 /* put program info here */
258 val = 0xf000 | (q - program_info_length_ptr - 2);
259 program_info_length_ptr[0] = val >> 8;
260 program_info_length_ptr[1] = val;
262 for (i = 0; i < s->nb_streams; i++) {
263 AVStream *st = s->streams[i];
264 MpegTSWriteStream *ts_st = st->priv_data;
265 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
267 if (q - data > SECTION_LENGTH - 32) {
271 switch (st->codec->codec_id) {
272 case AV_CODEC_ID_MPEG1VIDEO:
273 case AV_CODEC_ID_MPEG2VIDEO:
274 stream_type = STREAM_TYPE_VIDEO_MPEG2;
276 case AV_CODEC_ID_MPEG4:
277 stream_type = STREAM_TYPE_VIDEO_MPEG4;
279 case AV_CODEC_ID_H264:
280 stream_type = STREAM_TYPE_VIDEO_H264;
282 case AV_CODEC_ID_HEVC:
283 stream_type = STREAM_TYPE_VIDEO_HEVC;
285 case AV_CODEC_ID_CAVS:
286 stream_type = STREAM_TYPE_VIDEO_CAVS;
288 case AV_CODEC_ID_DIRAC:
289 stream_type = STREAM_TYPE_VIDEO_DIRAC;
291 case AV_CODEC_ID_MP2:
292 case AV_CODEC_ID_MP3:
293 stream_type = STREAM_TYPE_AUDIO_MPEG1;
295 case AV_CODEC_ID_AAC:
296 stream_type = (ts->flags & MPEGTS_FLAG_AAC_LATM)
297 ? STREAM_TYPE_AUDIO_AAC_LATM
298 : STREAM_TYPE_AUDIO_AAC;
300 case AV_CODEC_ID_AAC_LATM:
301 stream_type = STREAM_TYPE_AUDIO_AAC_LATM;
303 case AV_CODEC_ID_AC3:
304 stream_type = STREAM_TYPE_AUDIO_AC3;
306 case AV_CODEC_ID_DTS:
307 stream_type = STREAM_TYPE_AUDIO_DTS;
309 case AV_CODEC_ID_TRUEHD:
310 stream_type = STREAM_TYPE_AUDIO_TRUEHD;
313 stream_type = STREAM_TYPE_PRIVATE_DATA;
318 put16(&q, 0xe000 | ts_st->pid);
320 q += 2; /* patched after */
322 /* write optional descriptors here */
323 switch (st->codec->codec_type) {
324 case AVMEDIA_TYPE_AUDIO:
325 if (st->codec->codec_id==AV_CODEC_ID_EAC3) {
326 *q++=0x7a; // EAC3 descriptor see A038 DVB SI
327 *q++=1; // 1 byte, all flags sets to 0
328 *q++=0; // omit all fields...
330 if (st->codec->codec_id==AV_CODEC_ID_S302M) {
331 *q++ = 0x05; /* MPEG-2 registration descriptor*/
341 char *next = lang->value;
344 *q++ = 0x0a; /* ISO 639 language descriptor */
348 for (p = lang->value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
349 if (q - data > SECTION_LENGTH - 4) {
353 next = strchr(p, ',');
354 if (strlen(p) != 3 && (!next || next != p + 3))
355 continue; /* not a 3-letter code */
361 if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
363 else if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
365 else if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
368 *q++ = 0; /* undefined type */
374 q -= 2; /* no language codes were written */
377 case AVMEDIA_TYPE_SUBTITLE:
379 const char default_language[] = "und";
380 const char *language = lang && strlen(lang->value) >= 3 ? lang->value : default_language;
382 if (st->codec->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
384 int extradata_copied = 0;
386 *q++ = 0x59; /* subtitling_descriptor */
389 while (strlen(language) >= 3) {
390 if (sizeof(data) - (q - data) < 8) { /* 8 bytes per DVB subtitle substream data */
398 if (*language != '\0')
401 if (st->codec->extradata_size - extradata_copied >= 5) {
402 *q++ = st->codec->extradata[extradata_copied + 4]; /* subtitling_type */
403 memcpy(q, st->codec->extradata + extradata_copied, 4); /* composition_page_id and ancillary_page_id */
404 extradata_copied += 5;
408 * 0x10 - normal with no monitor aspect ratio criticality
409 * 0x20 - for the hard of hearing with no monitor aspect ratio criticality */
410 *q++ = (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED) ? 0x20 : 0x10;
411 if ((st->codec->extradata_size == 4) && (extradata_copied == 0)) {
412 /* support of old 4-byte extradata format */
413 memcpy(q, st->codec->extradata, 4); /* composition_page_id and ancillary_page_id */
414 extradata_copied += 4;
417 put16(&q, 1); /* composition_page_id */
418 put16(&q, 1); /* ancillary_page_id */
423 *len_ptr = q - len_ptr - 1;
424 } else if (st->codec->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
425 uint8_t *len_ptr = NULL;
426 int extradata_copied = 0;
428 /* The descriptor tag. teletext_descriptor */
432 while (strlen(language) >= 3 && q - data < sizeof(data) - 6) {
437 if (*language != '\0')
440 if (st->codec->extradata_size - 1 > extradata_copied) {
441 memcpy(q, st->codec->extradata + extradata_copied, 2);
442 extradata_copied += 2;
445 /* The Teletext descriptor:
446 * teletext_type: This 5-bit field indicates the type of Teletext page indicated. (0x01 Initial Teletext page)
447 * teletext_magazine_number: This is a 3-bit field which identifies the magazine number.
448 * teletext_page_number: This is an 8-bit field giving two 4-bit hex digits identifying the page number. */
454 *len_ptr = q - len_ptr - 1;
458 case AVMEDIA_TYPE_VIDEO:
459 if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
460 *q++ = 0x05; /*MPEG-2 registration descriptor*/
468 case AVMEDIA_TYPE_DATA:
469 if (st->codec->codec_id == AV_CODEC_ID_SMPTE_KLV) {
470 *q++ = 0x05; /* MPEG-2 registration descriptor */
480 val = 0xf000 | (q - desc_length_ptr - 2);
481 desc_length_ptr[0] = val >> 8;
482 desc_length_ptr[1] = val;
486 av_log(s, AV_LOG_ERROR,
487 "The PMT section cannot fit stream %d and all following streams.\n"
488 "Try reducing the number of languages in the audio streams "
489 "or the total number of streams.\n", i);
491 mpegts_write_section1(&service->pmt, PMT_TID, service->sid, ts->tables_version, 0, 0,
496 /* NOTE: !str is accepted for an empty string */
497 static void putstr8(uint8_t **q_ptr, const char *str)
513 static void mpegts_write_sdt(AVFormatContext *s)
515 MpegTSWrite *ts = s->priv_data;
516 MpegTSService *service;
517 uint8_t data[SECTION_LENGTH], *q, *desc_list_len_ptr, *desc_len_ptr;
518 int i, running_status, free_ca_mode, val;
523 for (i = 0; i < ts->nb_services; i++) {
524 service = ts->services[i];
525 put16(&q, service->sid);
526 *q++ = 0xfc | 0x00; /* currently no EIT info */
527 desc_list_len_ptr = q;
529 running_status = 4; /* running */
532 /* write only one descriptor for the service name and provider */
536 *q++ = ts->service_type;
537 putstr8(&q, service->provider_name);
538 putstr8(&q, service->name);
539 desc_len_ptr[0] = q - desc_len_ptr - 1;
541 /* fill descriptor length */
542 val = (running_status << 13) | (free_ca_mode << 12) |
543 (q - desc_list_len_ptr - 2);
544 desc_list_len_ptr[0] = val >> 8;
545 desc_list_len_ptr[1] = val;
547 mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, ts->tables_version, 0, 0,
551 static MpegTSService *mpegts_add_service(MpegTSWrite *ts, int sid,
552 const char *provider_name,
555 MpegTSService *service;
557 service = av_mallocz(sizeof(MpegTSService));
560 service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
562 service->pcr_pid = 0x1fff;
563 service->provider_name = av_strdup(provider_name);
564 service->name = av_strdup(name);
565 if (!service->provider_name || !service->name)
567 if (av_dynarray_add_nofree(&ts->services, &ts->nb_services, service) < 0)
572 av_freep(&service->provider_name);
573 av_freep(&service->name);
578 static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
580 return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
584 static void mpegts_prefix_m2ts_header(AVFormatContext *s)
586 MpegTSWrite *ts = s->priv_data;
588 int64_t pcr = get_pcr(s->priv_data, s->pb);
589 uint32_t tp_extra_header = pcr % 0x3fffffff;
590 tp_extra_header = AV_RB32(&tp_extra_header);
591 avio_write(s->pb, (unsigned char *) &tp_extra_header,
592 sizeof(tp_extra_header));
596 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
598 AVFormatContext *ctx = s->opaque;
599 mpegts_prefix_m2ts_header(ctx);
600 avio_write(ctx->pb, packet, TS_PACKET_SIZE);
603 static int mpegts_write_header(AVFormatContext *s)
605 MpegTSWrite *ts = s->priv_data;
606 MpegTSWriteStream *ts_st;
607 MpegTSService *service;
608 AVStream *st, *pcr_st = NULL;
609 AVDictionaryEntry *title, *provider;
611 const char *service_name;
612 const char *provider_name;
616 if (s->max_delay < 0) /* Not set by the caller */
619 // round up to a whole number of TS packets
620 ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
622 ts->tsid = ts->transport_stream_id;
623 ts->onid = ts->original_network_id;
624 /* allocate a single DVB service */
625 title = av_dict_get(s->metadata, "service_name", NULL, 0);
627 title = av_dict_get(s->metadata, "title", NULL, 0);
628 service_name = title ? title->value : DEFAULT_SERVICE_NAME;
629 provider = av_dict_get(s->metadata, "service_provider", NULL, 0);
630 provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
631 service = mpegts_add_service(ts, ts->service_id,
632 provider_name, service_name);
635 return AVERROR(ENOMEM);
637 service->pmt.write_packet = section_write_packet;
638 service->pmt.opaque = s;
639 service->pmt.cc = 15;
641 ts->pat.pid = PAT_PID;
642 /* Initialize at 15 so that it wraps and is equal to 0 for the
643 * first packet we write. */
645 ts->pat.write_packet = section_write_packet;
648 ts->sdt.pid = SDT_PID;
650 ts->sdt.write_packet = section_write_packet;
653 pids = av_malloc_array(s->nb_streams, sizeof(*pids));
655 ret = AVERROR(ENOMEM);
659 /* assign pids to each stream */
660 for (i = 0; i < s->nb_streams; i++) {
663 ts_st = av_mallocz(sizeof(MpegTSWriteStream));
665 ret = AVERROR(ENOMEM);
668 st->priv_data = ts_st;
670 ts_st->user_tb = st->time_base;
671 avpriv_set_pts_info(st, 33, 1, 90000);
673 ts_st->payload = av_mallocz(ts->pes_payload_size);
674 if (!ts_st->payload) {
675 ret = AVERROR(ENOMEM);
678 ts_st->service = service;
679 /* MPEG pid values < 16 are reserved. Applications which set st->id in
680 * this range are assigned a calculated pid. */
682 ts_st->pid = ts->start_pid + i;
683 } else if (st->id < 0x1FFF) {
686 av_log(s, AV_LOG_ERROR,
687 "Invalid stream id %d, must be less than 8191\n", st->id);
688 ret = AVERROR(EINVAL);
691 if (ts_st->pid == service->pmt.pid) {
692 av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
693 ret = AVERROR(EINVAL);
696 for (j = 0; j < i; j++) {
697 if (pids[j] == ts_st->pid) {
698 av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
699 ret = AVERROR(EINVAL);
703 pids[i] = ts_st->pid;
704 ts_st->payload_pts = AV_NOPTS_VALUE;
705 ts_st->payload_dts = AV_NOPTS_VALUE;
706 ts_st->first_pts_check = 1;
708 /* update PCR pid by using the first video stream */
709 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
710 service->pcr_pid == 0x1fff) {
711 service->pcr_pid = ts_st->pid;
714 if (st->codec->codec_id == AV_CODEC_ID_AAC &&
715 st->codec->extradata_size > 0) {
717 ts_st->amux = avformat_alloc_context();
719 ret = AVERROR(ENOMEM);
722 ts_st->amux->oformat =
723 av_guess_format((ts->flags & MPEGTS_FLAG_AAC_LATM) ? "latm" : "adts",
725 if (!ts_st->amux->oformat) {
726 ret = AVERROR(EINVAL);
729 if (!(ast = avformat_new_stream(ts_st->amux, NULL))) {
730 ret = AVERROR(ENOMEM);
733 ret = avcodec_copy_context(ast->codec, st->codec);
736 ast->time_base = st->time_base;
737 ret = avformat_write_header(ts_st->amux, NULL);
745 /* if no video stream, use the first stream as PCR */
746 if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
747 pcr_st = s->streams[0];
748 ts_st = pcr_st->priv_data;
749 service->pcr_pid = ts_st->pid;
751 ts_st = pcr_st->priv_data;
753 if (ts->mux_rate > 1) {
754 service->pcr_packet_period = (ts->mux_rate * ts->pcr_period) /
755 (TS_PACKET_SIZE * 8 * 1000);
756 ts->sdt_packet_period = (ts->mux_rate * SDT_RETRANS_TIME) /
757 (TS_PACKET_SIZE * 8 * 1000);
758 ts->pat_packet_period = (ts->mux_rate * PAT_RETRANS_TIME) /
759 (TS_PACKET_SIZE * 8 * 1000);
762 ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
764 /* Arbitrary values, PAT/PMT will also be written on video key frames */
765 ts->sdt_packet_period = 200;
766 ts->pat_packet_period = 40;
767 if (pcr_st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
768 if (!pcr_st->codec->frame_size) {
769 av_log(s, AV_LOG_WARNING, "frame size not set\n");
770 service->pcr_packet_period =
771 pcr_st->codec->sample_rate / (10 * 512);
773 service->pcr_packet_period =
774 pcr_st->codec->sample_rate / (10 * pcr_st->codec->frame_size);
777 // max delta PCR 0.1s
778 // TODO: should be avg_frame_rate
779 service->pcr_packet_period =
780 ts_st->user_tb.den / (10 * ts_st->user_tb.num);
782 if (!service->pcr_packet_period)
783 service->pcr_packet_period = 1;
786 // output a PCR as soon as possible
787 service->pcr_packet_count = service->pcr_packet_period;
788 ts->pat_packet_count = ts->pat_packet_period - 1;
789 ts->sdt_packet_count = ts->sdt_packet_period - 1;
791 if (ts->mux_rate == 1)
792 av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
794 av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
795 av_log(s, AV_LOG_VERBOSE,
796 "pcr every %d pkts, sdt every %d, pat/pmt every %d pkts\n",
797 service->pcr_packet_period,
798 ts->sdt_packet_period, ts->pat_packet_period);
800 if (ts->m2ts_mode == -1) {
801 if (av_match_ext(s->filename, "m2ts")) {
812 for (i = 0; i < s->nb_streams; i++) {
814 ts_st = st->priv_data;
816 av_freep(&ts_st->payload);
818 avformat_free_context(ts_st->amux);
822 av_freep(&st->priv_data);
825 for (i = 0; i < ts->nb_services; i++) {
826 service = ts->services[i];
827 av_freep(&service->provider_name);
828 av_freep(&service->name);
831 av_freep(&ts->services);
835 /* send SDT, PAT and PMT tables regulary */
836 static void retransmit_si_info(AVFormatContext *s, int force_pat)
838 MpegTSWrite *ts = s->priv_data;
841 if (++ts->sdt_packet_count == ts->sdt_packet_period) {
842 ts->sdt_packet_count = 0;
845 if (++ts->pat_packet_count == ts->pat_packet_period || force_pat) {
846 ts->pat_packet_count = 0;
848 for (i = 0; i < ts->nb_services; i++)
849 mpegts_write_pmt(s, ts->services[i]);
853 static int write_pcr_bits(uint8_t *buf, int64_t pcr)
855 int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
857 *buf++ = pcr_high >> 25;
858 *buf++ = pcr_high >> 17;
859 *buf++ = pcr_high >> 9;
860 *buf++ = pcr_high >> 1;
861 *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
867 /* Write a single null transport stream packet */
868 static void mpegts_insert_null_packet(AVFormatContext *s)
871 uint8_t buf[TS_PACKET_SIZE];
878 memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
879 mpegts_prefix_m2ts_header(s);
880 avio_write(s->pb, buf, TS_PACKET_SIZE);
883 /* Write a single transport stream packet with a PCR and no payload */
884 static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
886 MpegTSWrite *ts = s->priv_data;
887 MpegTSWriteStream *ts_st = st->priv_data;
889 uint8_t buf[TS_PACKET_SIZE];
893 *q++ = ts_st->pid >> 8;
895 *q++ = 0x20 | ts_st->cc; /* Adaptation only */
896 /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
897 *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
898 *q++ = 0x10; /* Adaptation flags: PCR present */
900 /* PCR coded into 6 bytes */
901 q += write_pcr_bits(q, get_pcr(ts, s->pb));
904 memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
905 mpegts_prefix_m2ts_header(s);
906 avio_write(s->pb, buf, TS_PACKET_SIZE);
909 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
913 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
915 val = (((pts >> 15) & 0x7fff) << 1) | 1;
918 val = (((pts) & 0x7fff) << 1) | 1;
923 /* Set an adaptation field flag in an MPEG-TS packet*/
924 static void set_af_flag(uint8_t *pkt, int flag)
926 // expect at least one flag to set
929 if ((pkt[3] & 0x20) == 0) {
930 // no AF yet, set adaptation field flag
932 // 1 byte length, no flags
939 /* Extend the adaptation field by size bytes */
940 static void extend_af(uint8_t *pkt, int size)
942 // expect already existing adaptation field
943 av_assert0(pkt[3] & 0x20);
947 /* Get a pointer to MPEG-TS payload (right after TS packet header) */
948 static uint8_t *get_ts_payload_start(uint8_t *pkt)
951 return pkt + 5 + pkt[4];
956 /* Add a PES header to the front of the payload, and segment into an integer
957 * number of TS packets. The final TS packet is padded using an oversized
958 * adaptation header to exactly fill the last TS packet.
959 * NOTE: 'payload' contains a complete PES payload. */
960 static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
961 const uint8_t *payload, int payload_size,
962 int64_t pts, int64_t dts, int key)
964 MpegTSWriteStream *ts_st = st->priv_data;
965 MpegTSWrite *ts = s->priv_data;
966 uint8_t buf[TS_PACKET_SIZE];
968 int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags;
969 int afc_len, stuffing_len;
970 int64_t pcr = -1; /* avoid warning */
971 int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
972 int force_pat = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
975 while (payload_size > 0) {
976 retransmit_si_info(s, force_pat);
980 if (ts_st->pid == ts_st->service->pcr_pid) {
981 if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
982 ts_st->service->pcr_packet_count++;
983 if (ts_st->service->pcr_packet_count >=
984 ts_st->service->pcr_packet_period) {
985 ts_st->service->pcr_packet_count = 0;
990 if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
991 (dts - get_pcr(ts, s->pb) / 300) > delay) {
992 /* pcr insert gets priority over null packet insert */
994 mpegts_insert_pcr_only(s, st);
996 mpegts_insert_null_packet(s);
997 /* recalculate write_pcr and possibly retransmit si_info */
1001 /* prepare packet header */
1004 val = ts_st->pid >> 8;
1009 ts_st->cc = ts_st->cc + 1 & 0xf;
1010 *q++ = 0x10 | ts_st->cc; // payload indicator + CC
1011 if (key && is_start && pts != AV_NOPTS_VALUE) {
1012 // set Random Access for key frames
1013 if (ts_st->pid == ts_st->service->pcr_pid)
1015 set_af_flag(buf, 0x40);
1016 q = get_ts_payload_start(buf);
1019 set_af_flag(buf, 0x10);
1020 q = get_ts_payload_start(buf);
1021 // add 11, pcr references the last byte of program clock reference base
1022 if (ts->mux_rate > 1)
1023 pcr = get_pcr(ts, s->pb);
1025 pcr = (dts - delay) * 300;
1026 if (dts != AV_NOPTS_VALUE && dts < 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 q = get_ts_payload_start(buf);
1032 int pes_extension = 0;
1033 int pes_header_stuffing_bytes = 0;
1034 /* write PES header */
1038 is_dvb_subtitle = 0;
1039 is_dvb_teletext = 0;
1040 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
1041 if (st->codec->codec_id == AV_CODEC_ID_DIRAC)
1045 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
1046 (st->codec->codec_id == AV_CODEC_ID_MP2 ||
1047 st->codec->codec_id == AV_CODEC_ID_MP3 ||
1048 st->codec->codec_id == AV_CODEC_ID_AAC)) {
1050 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
1051 st->codec->codec_id == AV_CODEC_ID_AC3 &&
1056 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1057 if (st->codec->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
1058 is_dvb_subtitle = 1;
1059 } else if (st->codec->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
1060 is_dvb_teletext = 1;
1066 if (pts != AV_NOPTS_VALUE) {
1070 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1074 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
1075 st->codec->codec_id == AV_CODEC_ID_DIRAC) {
1076 /* set PES_extension_flag */
1080 /* One byte for PES2 extension flag +
1081 * one byte for extension length +
1082 * one byte for extension id */
1085 /* for Blu-ray AC3 Audio the PES Extension flag should be as follow
1086 * otherwise it will not play sound on blu-ray
1088 if (ts->m2ts_mode &&
1089 st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
1090 st->codec->codec_id == AV_CODEC_ID_AC3) {
1091 /* set PES_extension_flag */
1096 if (is_dvb_teletext) {
1097 pes_header_stuffing_bytes = 0x24 - header_len;
1100 len = payload_size + header_len + 3;
1101 /* 3 extra bytes should be added to DVB subtitle payload: 0x20 0x00 at the beginning and trailing 0xff */
1102 if (is_dvb_subtitle) {
1108 if (ts->omit_video_pes_length && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
1114 /* data alignment indicator is required for subtitle and data streams */
1115 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE || st->codec->codec_type == AVMEDIA_TYPE_DATA)
1120 if (pts != AV_NOPTS_VALUE) {
1121 write_pts(q, flags >> 6, pts);
1124 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
1125 write_pts(q, 1, dts);
1128 if (pes_extension && st->codec->codec_id == AV_CODEC_ID_DIRAC) {
1129 flags = 0x01; /* set PES_extension_flag_2 */
1131 *q++ = 0x80 | 0x01; /* marker bit + extension length */
1132 /* Set the stream ID extension flag bit to 0 and
1133 * write the extended stream ID. */
1136 /* For Blu-ray AC3 Audio Setting extended flags */
1137 if (ts->m2ts_mode &&
1139 st->codec->codec_id == AV_CODEC_ID_AC3) {
1140 flags = 0x01; /* set PES_extension_flag_2 */
1142 *q++ = 0x80 | 0x01; /* marker bit + extension length */
1143 *q++ = 0x00 | 0x71; /* for AC3 Audio (specifically on blue-rays) */
1147 if (is_dvb_subtitle) {
1148 /* First two fields of DVB subtitles PES data:
1149 * data_identifier: for DVB subtitle streams shall be coded with the value 0x20
1150 * subtitle_stream_id: for DVB subtitle stream shall be identified by the value 0x00 */
1154 if (is_dvb_teletext) {
1155 memset(q, 0xff, pes_header_stuffing_bytes);
1156 q += pes_header_stuffing_bytes;
1161 header_len = q - buf;
1163 len = TS_PACKET_SIZE - header_len;
1164 if (len > payload_size)
1166 stuffing_len = TS_PACKET_SIZE - header_len - len;
1167 if (stuffing_len > 0) {
1168 /* add stuffing with AFC */
1169 if (buf[3] & 0x20) {
1170 /* stuffing already present: increase its size */
1171 afc_len = buf[4] + 1;
1172 memmove(buf + 4 + afc_len + stuffing_len,
1174 header_len - (4 + afc_len));
1175 buf[4] += stuffing_len;
1176 memset(buf + 4 + afc_len, 0xff, stuffing_len);
1179 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1181 buf[4] = stuffing_len - 1;
1182 if (stuffing_len >= 2) {
1184 memset(buf + 6, 0xff, stuffing_len - 2);
1189 if (is_dvb_subtitle && payload_size == len) {
1190 memcpy(buf + TS_PACKET_SIZE - len, payload, len - 1);
1191 buf[TS_PACKET_SIZE - 1] = 0xff; /* end_of_PES_data_field_marker: an 8-bit field with fixed contents 0xff for DVB subtitle */
1193 memcpy(buf + TS_PACKET_SIZE - len, payload, len);
1197 payload_size -= len;
1198 mpegts_prefix_m2ts_header(s);
1199 avio_write(s->pb, buf, TS_PACKET_SIZE);
1201 ts_st->prev_payload_key = key;
1204 int ff_check_h264_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
1206 if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001 && AV_RB24(pkt->data) != 0x000001) {
1207 if (!st->nb_frames) {
1208 av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
1209 "no startcode found, use the video bitstream filter 'h264_mp4toannexb' to fix it "
1210 "('-bsf:v h264_mp4toannexb' option with ffmpeg)\n");
1211 return AVERROR_INVALIDDATA;
1213 av_log(s, AV_LOG_WARNING, "H.264 bitstream error, startcode missing, size %d", pkt->size);
1214 if (pkt->size) av_log(s, AV_LOG_WARNING, " data %08X", AV_RB32(pkt->data));
1215 av_log(s, AV_LOG_WARNING, "\n");
1220 static int check_hevc_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
1222 if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001) {
1223 if (!st->nb_frames) {
1224 av_log(s, AV_LOG_ERROR, "HEVC bitstream malformed, no startcode found\n");
1225 return AVERROR_PATCHWELCOME;
1227 av_log(s, AV_LOG_WARNING, "HEVC bitstream error, startcode missing, size %d", pkt->size);
1228 if (pkt->size) av_log(s, AV_LOG_WARNING, " data %08X", AV_RB32(pkt->data));
1229 av_log(s, AV_LOG_WARNING, "\n");
1234 static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
1236 AVStream *st = s->streams[pkt->stream_index];
1237 int size = pkt->size;
1238 uint8_t *buf = pkt->data;
1239 uint8_t *data = NULL;
1240 MpegTSWrite *ts = s->priv_data;
1241 MpegTSWriteStream *ts_st = st->priv_data;
1242 const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
1243 int64_t dts = pkt->dts, pts = pkt->pts;
1245 if (ts->reemit_pat_pmt) {
1246 av_log(s, AV_LOG_WARNING,
1247 "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1248 ts->reemit_pat_pmt = 0;
1249 ts->flags |= MPEGTS_FLAG_REEMIT_PAT_PMT;
1252 if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
1253 ts->pat_packet_count = ts->pat_packet_period - 1;
1254 ts->sdt_packet_count = ts->sdt_packet_period - 1;
1255 ts->flags &= ~MPEGTS_FLAG_REEMIT_PAT_PMT;
1258 if (ts->copyts < 1) {
1259 if (pts != AV_NOPTS_VALUE)
1261 if (dts != AV_NOPTS_VALUE)
1265 if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
1266 av_log(s, AV_LOG_ERROR, "first pts value must be set\n");
1267 return AVERROR_INVALIDDATA;
1269 ts_st->first_pts_check = 0;
1271 if (st->codec->codec_id == AV_CODEC_ID_H264) {
1272 const uint8_t *p = buf, *buf_end = p + size;
1273 uint32_t state = -1;
1274 int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codec->extradata_size : 0;
1275 int ret = ff_check_h264_startcode(s, st, pkt);
1279 if (extradd && AV_RB24(st->codec->extradata) > 1)
1283 p = avpriv_find_start_code(p, buf_end, &state);
1284 av_dlog(s, "nal %d\n", state & 0x1f);
1285 if ((state & 0x1f) == 7)
1287 } while (p < buf_end && (state & 0x1f) != 9 &&
1288 (state & 0x1f) != 5 && (state & 0x1f) != 1);
1290 if ((state & 0x1f) != 5)
1292 if ((state & 0x1f) != 9) { // AUD NAL
1293 data = av_malloc(pkt->size + 6 + extradd);
1295 return AVERROR(ENOMEM);
1296 memcpy(data + 6, st->codec->extradata, extradd);
1297 memcpy(data + 6 + extradd, pkt->data, pkt->size);
1298 AV_WB32(data, 0x00000001);
1300 data[5] = 0xf0; // any slice type (0xe) + rbsp stop one bit
1302 size = pkt->size + 6 + extradd;
1304 } else if (st->codec->codec_id == AV_CODEC_ID_AAC) {
1305 if (pkt->size < 2) {
1306 av_log(s, AV_LOG_ERROR, "AAC packet too short\n");
1307 return AVERROR_INVALIDDATA;
1309 if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
1314 av_log(s, AV_LOG_ERROR, "AAC bitstream not in ADTS format "
1315 "and extradata missing\n");
1316 return AVERROR_INVALIDDATA;
1319 av_init_packet(&pkt2);
1320 pkt2.data = pkt->data;
1321 pkt2.size = pkt->size;
1322 av_assert0(pkt->dts != AV_NOPTS_VALUE);
1323 pkt2.dts = av_rescale_q(pkt->dts, st->time_base, ts_st->amux->streams[0]->time_base);
1325 ret = avio_open_dyn_buf(&ts_st->amux->pb);
1327 return AVERROR(ENOMEM);
1329 ret = av_write_frame(ts_st->amux, &pkt2);
1331 ffio_free_dyn_buf(&ts_st->amux->pb);
1334 size = avio_close_dyn_buf(ts_st->amux->pb, &data);
1335 ts_st->amux->pb = NULL;
1338 } else if (st->codec->codec_id == AV_CODEC_ID_HEVC) {
1339 int ret = check_hevc_startcode(s, st, pkt);
1344 if (pkt->dts != AV_NOPTS_VALUE) {
1346 for(i=0; i<s->nb_streams; i++) {
1347 AVStream *st2 = s->streams[i];
1348 MpegTSWriteStream *ts_st2 = st2->priv_data;
1349 if ( ts_st2->payload_size
1350 && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > delay/2)) {
1351 mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
1352 ts_st2->payload_pts, ts_st2->payload_dts,
1353 ts_st2->payload_flags & AV_PKT_FLAG_KEY);
1354 ts_st2->payload_size = 0;
1359 if (ts_st->payload_size && ts_st->payload_size + size > ts->pes_payload_size) {
1360 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1361 ts_st->payload_pts, ts_st->payload_dts,
1362 ts_st->payload_flags & AV_PKT_FLAG_KEY);
1363 ts_st->payload_size = 0;
1366 if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO || size > ts->pes_payload_size) {
1367 av_assert0(!ts_st->payload_size);
1368 // for video and subtitle, write a single pes packet
1369 mpegts_write_pes(s, st, buf, size, pts, dts,
1370 pkt->flags & AV_PKT_FLAG_KEY);
1375 if (!ts_st->payload_size) {
1376 ts_st->payload_pts = pts;
1377 ts_st->payload_dts = dts;
1378 ts_st->payload_flags = pkt->flags;
1381 memcpy(ts_st->payload + ts_st->payload_size, buf, size);
1382 ts_st->payload_size += size;
1389 static void mpegts_write_flush(AVFormatContext *s)
1393 /* flush current packets */
1394 for (i = 0; i < s->nb_streams; i++) {
1395 AVStream *st = s->streams[i];
1396 MpegTSWriteStream *ts_st = st->priv_data;
1397 if (ts_st->payload_size > 0) {
1398 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
1399 ts_st->payload_pts, ts_st->payload_dts,
1400 ts_st->payload_flags & AV_PKT_FLAG_KEY);
1401 ts_st->payload_size = 0;
1406 static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
1409 mpegts_write_flush(s);
1412 return mpegts_write_packet_internal(s, pkt);
1416 static int mpegts_write_end(AVFormatContext *s)
1418 MpegTSWrite *ts = s->priv_data;
1419 MpegTSService *service;
1423 mpegts_write_flush(s);
1425 for (i = 0; i < s->nb_streams; i++) {
1426 AVStream *st = s->streams[i];
1427 MpegTSWriteStream *ts_st = st->priv_data;
1428 av_freep(&ts_st->payload);
1430 avformat_free_context(ts_st->amux);
1435 for (i = 0; i < ts->nb_services; i++) {
1436 service = ts->services[i];
1437 av_freep(&service->provider_name);
1438 av_freep(&service->name);
1441 av_freep(&ts->services);
1446 static const AVOption options[] = {
1447 { "mpegts_transport_stream_id", "Set transport_stream_id field.",
1448 offsetof(MpegTSWrite, transport_stream_id), AV_OPT_TYPE_INT,
1449 { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1450 { "mpegts_original_network_id", "Set original_network_id field.",
1451 offsetof(MpegTSWrite, original_network_id), AV_OPT_TYPE_INT,
1452 { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1453 { "mpegts_service_id", "Set service_id field.",
1454 offsetof(MpegTSWrite, service_id), AV_OPT_TYPE_INT,
1455 { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
1456 { "mpegts_service_type", "Set service_type field.",
1457 offsetof(MpegTSWrite, service_type), AV_OPT_TYPE_INT,
1458 { .i64 = 0x01 }, 0x01, 0xff, AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1459 { "digital_tv", "Digital Television.",
1460 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_TV }, 0x01, 0xff,
1461 AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1462 { "digital_radio", "Digital Radio.",
1463 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_RADIO }, 0x01, 0xff,
1464 AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1465 { "teletext", "Teletext.",
1466 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_TELETEXT }, 0x01, 0xff,
1467 AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1468 { "advanced_codec_digital_radio", "Advanced Codec Digital Radio.",
1469 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO }, 0x01, 0xff,
1470 AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1471 { "mpeg2_digital_hdtv", "MPEG2 Digital HDTV.",
1472 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV }, 0x01, 0xff,
1473 AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1474 { "advanced_codec_digital_sdtv", "Advanced Codec Digital SDTV.",
1475 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV }, 0x01, 0xff,
1476 AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1477 { "advanced_codec_digital_hdtv", "Advanced Codec Digital HDTV.",
1478 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV }, 0x01, 0xff,
1479 AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
1480 { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
1481 offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT,
1482 { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
1483 { "mpegts_start_pid", "Set the first pid.",
1484 offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT,
1485 { .i64 = 0x0100 }, 0x0020, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM },
1486 { "mpegts_m2ts_mode", "Enable m2ts mode.",
1487 offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_INT,
1488 { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
1490 offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT,
1491 { .i64 = 1 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1492 { "pes_payload_size", "Minimum PES packet payload in bytes",
1493 offsetof(MpegTSWrite, pes_payload_size), AV_OPT_TYPE_INT,
1494 { .i64 = DEFAULT_PES_PAYLOAD_SIZE }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1495 { "mpegts_flags", "MPEG-TS muxing flags",
1496 offsetof(MpegTSWrite, flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX,
1497 AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1498 { "resend_headers", "Reemit PAT/PMT before writing the next packet",
1499 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_REEMIT_PAT_PMT }, 0, INT_MAX,
1500 AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1501 { "latm", "Use LATM packetization for AAC",
1502 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_AAC_LATM }, 0, INT_MAX,
1503 AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
1504 // backward compatibility
1505 { "resend_headers", "Reemit PAT/PMT before writing the next packet",
1506 offsetof(MpegTSWrite, reemit_pat_pmt), AV_OPT_TYPE_INT,
1507 { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1508 { "mpegts_copyts", "don't offset dts/pts",
1509 offsetof(MpegTSWrite, copyts), AV_OPT_TYPE_INT,
1510 { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
1511 { "tables_version", "set PAT, PMT and SDT version",
1512 offsetof(MpegTSWrite, tables_version), AV_OPT_TYPE_INT,
1513 { .i64 = 0 }, 0, 31, AV_OPT_FLAG_ENCODING_PARAM },
1514 { "omit_video_pes_length", "Omit the PES packet length for video packets",
1515 offsetof(MpegTSWrite, omit_video_pes_length), AV_OPT_TYPE_INT,
1516 { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
1517 { "pcr_period", "PCR retransmission time",
1518 offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_INT,
1519 { .i64 = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1523 static const AVClass mpegts_muxer_class = {
1524 .class_name = "MPEGTS muxer",
1525 .item_name = av_default_item_name,
1527 .version = LIBAVUTIL_VERSION_INT,
1530 AVOutputFormat ff_mpegts_muxer = {
1532 .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
1533 .mime_type = "video/MP2T",
1534 .extensions = "ts,m2t,m2ts,mts",
1535 .priv_data_size = sizeof(MpegTSWrite),
1536 .audio_codec = AV_CODEC_ID_MP2,
1537 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1538 .write_header = mpegts_write_header,
1539 .write_packet = mpegts_write_packet,
1540 .write_trailer = mpegts_write_end,
1541 .flags = AVFMT_ALLOW_FLUSH,
1542 .priv_class = &mpegts_muxer_class,