36 #define PCR_TIME_BASE 27000000
87 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
88 #define MPEGTS_FLAG_AAC_LATM 0x02
97 #define DEFAULT_PES_HEADER_FREQ 16
98 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
102 #define SECTION_LENGTH 1020
109 const unsigned char *buf_ptr;
111 int first,
b, len1, left;
116 buf[len - 4] = (crc >> 24) & 0xff;
117 buf[len - 3] = (crc >> 16) & 0xff;
118 buf[len - 2] = (crc >> 8) & 0xff;
119 buf[len - 1] = crc & 0xff;
124 first = buf == buf_ptr;
132 s->
cc = s->
cc + 1 & 0xf;
139 memcpy(q, buf_ptr, len1);
144 memset(q, 0xff, left);
163 int version,
int sec_num,
int last_sec_num,
167 unsigned int tot_len;
171 tot_len = 3 + 5 + len + 4;
178 put16(&q, flags | (len + 5 + 4));
180 *q++ = 0xc1 | (version << 1);
192 #define DEFAULT_PROVIDER_NAME "FFmpeg"
193 #define DEFAULT_SERVICE_NAME "Service01"
196 #define SDT_RETRANS_TIME 500
197 #define PAT_RETRANS_TIME 100
198 #define PCR_RETRANS_TIME 20
236 int val, stream_type, i, err = 0;
241 program_info_length_ptr = q;
246 val = 0xf000 | (q - program_info_length_ptr - 2);
247 program_info_length_ptr[0] = val >> 8;
248 program_info_length_ptr[1] =
val;
329 char *next = lang->
value;
336 for (p = lang->
value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
341 next = strchr(p,
',');
342 if (strlen(p) != 3 && (!next || next != p + 3))
367 const char default_language[] =
"und";
368 const char *language = lang && strlen(lang->
value) >= 3 ? lang->
value : default_language;
372 int extradata_copied = 0;
377 while (strlen(language) >= 3) {
378 if (
sizeof(data) - (q - data) < 8) {
386 if (*language !=
'\0')
392 extradata_copied += 5;
402 extradata_copied += 4;
411 *len_ptr = q - len_ptr - 1;
414 int extradata_copied = 0;
420 while (strlen(language) >= 3 && q - data <
sizeof(data) - 6) {
425 if (*language !=
'\0')
430 extradata_copied += 2;
442 *len_ptr = q - len_ptr - 1;
468 val = 0xf000 | (q - desc_length_ptr - 2);
469 desc_length_ptr[0] = val >> 8;
470 desc_length_ptr[1] =
val;
475 "The PMT section cannot fit stream %d and all following streams.\n"
476 "Try reducing the number of languages in the audio streams "
477 "or the total number of streams.\n", i);
506 int i, running_status, free_ca_mode,
val;
515 desc_list_len_ptr = q;
527 desc_len_ptr[0] = q - desc_len_ptr - 1;
530 val = (running_status << 13) | (free_ca_mode << 12) |
531 (q - desc_list_len_ptr - 2);
532 desc_list_len_ptr[0] = val >> 8;
533 desc_list_len_ptr[1] =
val;
577 uint32_t tp_extra_header = pcr % 0x3fffffff;
578 tp_extra_header =
AV_RB32(&tp_extra_header);
580 sizeof(tp_extra_header));
599 const char *service_name;
620 provider_name, service_name);
627 service->
pmt.
cc = 15;
671 }
else if (st->
id < 0x1FFF) {
675 "Invalid stream id %d, must be less than 8191\n", st->
id);
684 for (j = 0; j < i; j++) {
685 if (pids[j] == ts_st->
pid) {
691 pids[i] = ts_st->
pid;
783 "pcr every %d pkts, sdt every %d, pat/pmt every %d pkts\n",
842 int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
844 *buf++ = pcr_high >> 25;
845 *buf++ = pcr_high >> 17;
846 *buf++ = pcr_high >> 9;
847 *buf++ = pcr_high >> 1;
848 *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
880 *q++ = ts_st->
pid >> 8;
882 *q++ = 0x20 | ts_st->
cc;
900 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
902 val = (((pts >> 15) & 0x7fff) << 1) | 1;
905 val = (((pts) & 0x7fff) << 1) | 1;
916 if ((pkt[3] & 0x20) == 0) {
938 return pkt + 5 + pkt[4];
948 const uint8_t *payload,
int payload_size,
949 int64_t pts, int64_t dts,
int key)
955 int val, is_start,
len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext,
flags;
956 int afc_len, stuffing_len;
962 while (payload_size > 0) {
978 (dts -
get_pcr(ts, s->
pb) / 300) > delay) {
991 val = ts_st->
pid >> 8;
996 ts_st->
cc = ts_st->
cc + 1 & 0xf;
997 *q++ = 0x10 | ts_st->
cc;
1012 pcr = (dts - delay) * 300;
1019 int pes_extension = 0;
1020 int pes_header_stuffing_bytes = 0;
1025 is_dvb_subtitle = 0;
1026 is_dvb_teletext = 0;
1045 is_dvb_subtitle = 1;
1047 is_dvb_teletext = 1;
1083 if (is_dvb_teletext) {
1084 pes_header_stuffing_bytes = 0x24 - header_len;
1087 len = payload_size + header_len + 3;
1089 if (is_dvb_subtitle) {
1134 if (is_dvb_subtitle) {
1141 if (is_dvb_teletext) {
1142 memset(q, 0xff, pes_header_stuffing_bytes);
1143 q += pes_header_stuffing_bytes;
1148 header_len = q -
buf;
1151 if (len > payload_size)
1154 if (stuffing_len > 0) {
1156 if (buf[3] & 0x20) {
1158 afc_len = buf[4] + 1;
1159 memmove(buf + 4 + afc_len + stuffing_len,
1161 header_len - (4 + afc_len));
1162 buf[4] += stuffing_len;
1163 memset(buf + 4 + afc_len, 0xff, stuffing_len);
1166 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1168 buf[4] = stuffing_len - 1;
1169 if (stuffing_len >= 2) {
1171 memset(buf + 6, 0xff, stuffing_len - 2);
1176 if (is_dvb_subtitle && payload_size == len) {
1184 payload_size -=
len;
1196 "no startcode found, use the video bitstream filter 'h264_mp4toannexb' to fix it "
1197 "('-bsf:v h264_mp4toannexb' option with ffmpeg)\n");
1226 int64_t dts = pkt->
dts, pts = pkt->
pts;
1230 "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1256 uint32_t
state = -1;
1263 av_dlog(s,
"nal %d\n", state & 0x1f);
1264 }
while (p < buf_end && (state & 0x1f) != 9 &&
1265 (state & 0x1f) != 5 && (state & 0x1f) != 1);
1267 if ((state & 0x1f) != 9) {
1271 memcpy(data + 6, pkt->
data, pkt->
size);
1276 size = pkt->
size + 6;
1279 if (pkt->
size < 2) {
1289 "and extradata missing\n");
1422 {
"mpegts_transport_stream_id",
"Set transport_stream_id field.",
1425 {
"mpegts_original_network_id",
"Set original_network_id field.",
1428 {
"mpegts_service_id",
"Set service_id field.",
1431 {
"mpegts_pmt_start_pid",
"Set the first pid of the PMT.",
1434 {
"mpegts_start_pid",
"Set the first pid.",
1437 {
"mpegts_m2ts_mode",
"Enable m2ts mode.",
1443 {
"pes_payload_size",
"Minimum PES packet payload in bytes",
1446 {
"mpegts_flags",
"MPEG-TS muxing flags",
1449 {
"resend_headers",
"Reemit PAT/PMT before writing the next packet",
1452 {
"latm",
"Use LATM packetization for AAC",
1456 {
"resend_headers",
"Reemit PAT/PMT before writing the next packet",
1459 {
"mpegts_copyts",
"don't offset dts/pts",
1462 {
"tables_version",
"set PAT, PMT and SDT version",
1465 {
"omit_video_pes_length",
"Omit the PES packet length for video packets",
1468 {
"pcr_period",
"PCR retransmission time",
1484 .mime_type =
"video/MP2T",
1485 .extensions =
"ts,m2t,m2ts,mts",
1493 .priv_class = &mpegts_muxer_class,