34 #define PCR_TIME_BASE 27000000
84 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
85 #define MPEGTS_FLAG_AAC_LATM 0x02
92 #define DEFAULT_PES_HEADER_FREQ 16
93 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
96 {
"mpegts_transport_stream_id",
"Set transport_stream_id field.",
98 {
"mpegts_original_network_id",
"Set original_network_id field.",
100 {
"mpegts_service_id",
"Set service_id field.",
102 {
"mpegts_pmt_start_pid",
"Set the first pid of the PMT.",
104 {
"mpegts_start_pid",
"Set the first pid.",
106 {
"mpegts_m2ts_mode",
"Enable m2ts mode.",
110 {
"pes_payload_size",
"Minimum PES packet payload in bytes",
114 {
"resend_headers",
"Reemit PAT/PMT before writing the next packet",
117 {
"latm",
"Use LATM packetization for AAC",
121 {
"resend_headers",
"Reemit PAT/PMT before writing the next packet",
123 {
"mpegts_copyts",
"don't offset dts/pts",
125 {
"tables_version",
"set PAT, PMT and SDT version",
142 const unsigned char *buf_ptr;
144 int first,
b, len1, left;
147 buf[len - 4] = (crc >> 24) & 0xff;
148 buf[len - 3] = (crc >> 16) & 0xff;
149 buf[len - 2] = (crc >> 8) & 0xff;
150 buf[len - 1] = (crc) & 0xff;
155 first = (buf == buf_ptr);
163 s->
cc = (s->
cc + 1) & 0xf;
170 memcpy(q, buf_ptr, len1);
175 memset(q, 0xff, left);
194 int version,
int sec_num,
int last_sec_num,
198 unsigned int tot_len;
202 tot_len = 3 + 5 + len + 4;
209 put16(&q, flags | (len + 5 + 4));
211 *q++ = 0xc1 | (version << 1);
223 #define DEFAULT_PROVIDER_NAME "FFmpeg"
224 #define DEFAULT_SERVICE_NAME "Service01"
227 #define SDT_RETRANS_TIME 500
228 #define PAT_RETRANS_TIME 100
229 #define PCR_RETRANS_TIME 20
265 uint8_t data[1012], *q, *desc_length_ptr, *program_info_length_ptr;
266 int val, stream_type, i;
271 program_info_length_ptr = q;
276 val = 0xf000 | (q - program_info_length_ptr - 2);
277 program_info_length_ptr[0] = val >> 8;
278 program_info_length_ptr[1] =
val;
319 if (q - data >
sizeof(data) - 32)
346 char *next = lang->
value;
353 for (p = lang->
value; next && *len_ptr < 255 / 4 * 4 && q - data <
sizeof(data) - 4; p = next + 1) {
354 next = strchr(p,
',');
355 if (strlen(p) != 3 && (!next || next != p + 3))
380 const char default_language[] =
"und";
381 const char *language = lang && strlen(lang->
value) >= 3 ? lang->
value : default_language;
385 int extradata_copied = 0;
390 while (strlen(language) >= 3 && (
sizeof(data) - (q - data)) >= 8) {
395 if (*language !=
'\0')
401 extradata_copied += 5;
411 extradata_copied += 4;
420 *len_ptr = q - len_ptr - 1;
423 int extradata_copied = 0;
429 while (strlen(language) >= 3 && q - data <
sizeof(data) - 6) {
434 if (*language !=
'\0')
439 extradata_copied += 2;
451 *len_ptr = q - len_ptr - 1;
477 val = 0xf000 | (q - desc_length_ptr - 2);
478 desc_length_ptr[0] = val >> 8;
479 desc_length_ptr[1] =
val;
507 uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
508 int i, running_status, free_ca_mode,
val;
517 desc_list_len_ptr = q;
529 desc_len_ptr[0] = q - desc_len_ptr - 1;
532 val = (running_status << 13) | (free_ca_mode << 12) |
533 (q - desc_list_len_ptr - 2);
534 desc_list_len_ptr[0] = val >> 8;
535 desc_list_len_ptr[1] =
val;
571 uint32_t tp_extra_header = pcr % 0x3fffffff;
572 tp_extra_header =
AV_RB32(&tp_extra_header);
574 sizeof(tp_extra_header));
593 const char *service_name;
616 service->
pmt.
cc = 15;
652 }
else if (st->
id < 0x1FFF) {
664 for (j = 0; j < i; j++)
665 if (pids[j] == ts_st->
pid) {
670 pids[i] = ts_st->
pid;
756 "sdt every %d, pat/pmt every %d pkts\n",
811 int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
813 *buf++ = pcr_high >> 25;
814 *buf++ = pcr_high >> 17;
815 *buf++ = pcr_high >> 9;
816 *buf++ = pcr_high >> 1;
817 *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
849 *q++ = ts_st->
pid >> 8;
851 *q++ = 0x20 | ts_st->
cc;
869 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
871 val = (((pts >> 15) & 0x7fff) << 1) | 1;
874 val = (((pts) & 0x7fff) << 1) | 1;
885 if ((pkt[3] & 0x20) == 0) {
907 return pkt + 5 + pkt[4];
918 const uint8_t *payload,
int payload_size,
919 int64_t pts, int64_t dts,
int key)
925 int val, is_start,
len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext,
flags;
926 int afc_len, stuffing_len;
932 while (payload_size > 0) {
948 (dts -
get_pcr(ts, s->
pb)/300) > delay) {
960 val = (ts_st->
pid >> 8);
965 ts_st->
cc = (ts_st->
cc + 1) & 0xf;
966 *q++ = 0x10 | ts_st->
cc;
981 pcr = (dts - delay)*300;
988 int pes_extension = 0;
989 int pes_header_stuffing_bytes = 0;
1014 is_dvb_subtitle = 1;
1016 is_dvb_teletext = 1;
1054 if (is_dvb_teletext) {
1055 pes_header_stuffing_bytes = 0x24 - header_len;
1058 len = payload_size + header_len + 3;
1060 if (is_dvb_subtitle) {
1104 if (is_dvb_subtitle) {
1111 if (is_dvb_teletext) {
1112 memset(q, 0xff, pes_header_stuffing_bytes);
1113 q += pes_header_stuffing_bytes;
1118 header_len = q -
buf;
1121 if (len > payload_size)
1124 if (stuffing_len > 0) {
1126 if (buf[3] & 0x20) {
1128 afc_len = buf[4] + 1;
1129 memmove(buf + 4 + afc_len + stuffing_len,
1131 header_len - (4 + afc_len));
1132 buf[4] += stuffing_len;
1133 memset(buf + 4 + afc_len, 0xff, stuffing_len);
1136 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1138 buf[4] = stuffing_len - 1;
1139 if (stuffing_len >= 2) {
1141 memset(buf + 6, 0xff, stuffing_len - 2);
1146 if (is_dvb_subtitle && payload_size == len) {
1154 payload_size -=
len;
1171 int64_t dts = pkt->
dts, pts = pkt->
pts;
1174 av_log(s,
AV_LOG_WARNING,
"resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1200 uint32_t
state = -1;
1205 "no startcode found, use the h264_mp4toannexb bitstream filter (-bsf h264_mp4toannexb)\n");
1213 av_dlog(s,
"nal %d\n", state & 0x1f);
1214 }
while (p < buf_end && (state & 0x1f) != 9 &&
1215 (state & 0x1f) != 5 && (state & 0x1f) != 1);
1217 if ((state & 0x1f) != 9) {
1221 memcpy(data+6, pkt->
data, pkt->
size);
1229 if (pkt->
size < 2) {
1239 "and extradata missing\n");
1367 .mime_type =
"video/x-mpegts",
1368 .extensions =
"ts,m2t,m2ts,mts",
1376 .priv_class = &mpegts_muxer_class,