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;
784 "pcr every %d pkts, sdt every %d, pat/pmt every %d pkts\n",
843 int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
845 *buf++ = pcr_high >> 25;
846 *buf++ = pcr_high >> 17;
847 *buf++ = pcr_high >> 9;
848 *buf++ = pcr_high >> 1;
849 *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
881 *q++ = ts_st->
pid >> 8;
883 *q++ = 0x20 | ts_st->
cc;
901 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
903 val = (((pts >> 15) & 0x7fff) << 1) | 1;
906 val = (((pts) & 0x7fff) << 1) | 1;
917 if ((pkt[3] & 0x20) == 0) {
939 return pkt + 5 + pkt[4];
949 const uint8_t *payload,
int payload_size,
950 int64_t pts, int64_t dts,
int key)
956 int val, is_start,
len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext,
flags;
957 int afc_len, stuffing_len;
963 while (payload_size > 0) {
979 (dts -
get_pcr(ts, s->
pb) / 300) > delay) {
992 val = ts_st->
pid >> 8;
997 ts_st->
cc = ts_st->
cc + 1 & 0xf;
998 *q++ = 0x10 | ts_st->
cc;
1013 pcr = (dts - delay) * 300;
1020 int pes_extension = 0;
1021 int pes_header_stuffing_bytes = 0;
1026 is_dvb_subtitle = 0;
1027 is_dvb_teletext = 0;
1046 is_dvb_subtitle = 1;
1048 is_dvb_teletext = 1;
1084 if (is_dvb_teletext) {
1085 pes_header_stuffing_bytes = 0x24 - header_len;
1088 len = payload_size + header_len + 3;
1090 if (is_dvb_subtitle) {
1135 if (is_dvb_subtitle) {
1142 if (is_dvb_teletext) {
1143 memset(q, 0xff, pes_header_stuffing_bytes);
1144 q += pes_header_stuffing_bytes;
1149 header_len = q -
buf;
1152 if (len > payload_size)
1155 if (stuffing_len > 0) {
1157 if (buf[3] & 0x20) {
1159 afc_len = buf[4] + 1;
1160 memmove(buf + 4 + afc_len + stuffing_len,
1162 header_len - (4 + afc_len));
1163 buf[4] += stuffing_len;
1164 memset(buf + 4 + afc_len, 0xff, stuffing_len);
1167 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1169 buf[4] = stuffing_len - 1;
1170 if (stuffing_len >= 2) {
1172 memset(buf + 6, 0xff, stuffing_len - 2);
1177 if (is_dvb_subtitle && payload_size == len) {
1185 payload_size -=
len;
1197 "no startcode found, use the video bitstream filter 'h264_mp4toannexb' to fix it "
1198 "('-bsf:v h264_mp4toannexb' option with ffmpeg)\n");
1231 int64_t dts = pkt->
dts, pts = pkt->
pts;
1235 "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1261 uint32_t
state = -1;
1268 av_dlog(s,
"nal %d\n", state & 0x1f);
1269 }
while (p < buf_end && (state & 0x1f) != 9 &&
1270 (state & 0x1f) != 5 && (state & 0x1f) != 1);
1272 if ((state & 0x1f) != 9) {
1276 memcpy(data + 6, pkt->
data, pkt->
size);
1281 size = pkt->
size + 6;
1284 if (pkt->
size < 2) {
1294 "and extradata missing\n");
1427 {
"mpegts_transport_stream_id",
"Set transport_stream_id field.",
1430 {
"mpegts_original_network_id",
"Set original_network_id field.",
1433 {
"mpegts_service_id",
"Set service_id field.",
1436 {
"mpegts_pmt_start_pid",
"Set the first pid of the PMT.",
1439 {
"mpegts_start_pid",
"Set the first pid.",
1442 {
"mpegts_m2ts_mode",
"Enable m2ts mode.",
1448 {
"pes_payload_size",
"Minimum PES packet payload in bytes",
1451 {
"mpegts_flags",
"MPEG-TS muxing flags",
1454 {
"resend_headers",
"Reemit PAT/PMT before writing the next packet",
1457 {
"latm",
"Use LATM packetization for AAC",
1461 {
"resend_headers",
"Reemit PAT/PMT before writing the next packet",
1464 {
"mpegts_copyts",
"don't offset dts/pts",
1467 {
"tables_version",
"set PAT, PMT and SDT version",
1470 {
"omit_video_pes_length",
"Omit the PES packet length for video packets",
1473 {
"pcr_period",
"PCR retransmission time",
1489 .mime_type =
"video/MP2T",
1490 .extensions =
"ts,m2t,m2ts,mts",
1498 .priv_class = &mpegts_muxer_class,