37 #define PCR_TIME_BASE 27000000
99 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
100 #define MPEGTS_FLAG_AAC_LATM 0x02
109 #define DEFAULT_PES_HEADER_FREQ 16
110 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
114 #define SECTION_LENGTH 1020
121 const unsigned char *buf_ptr;
123 int first,
b, len1, left;
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;
136 first = buf == buf_ptr;
144 s->
cc = s->
cc + 1 & 0xf;
151 memcpy(q, buf_ptr, len1);
156 memset(q, 0xff, left);
175 int version,
int sec_num,
int last_sec_num,
179 unsigned int tot_len;
183 tot_len = 3 + 5 + len + 4;
190 put16(&q, flags | (len + 5 + 4));
192 *q++ = 0xc1 | (version << 1);
204 #define DEFAULT_PROVIDER_NAME "FFmpeg"
205 #define DEFAULT_SERVICE_NAME "Service01"
208 #define SDT_RETRANS_TIME 500
209 #define PAT_RETRANS_TIME 100
210 #define PCR_RETRANS_TIME 20
248 int val, stream_type, i, err = 0;
253 program_info_length_ptr = q;
258 val = 0xf000 | (q - program_info_length_ptr - 2);
259 program_info_length_ptr[0] = val >> 8;
260 program_info_length_ptr[1] =
val;
341 char *next = lang->
value;
348 for (p = lang->
value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
353 next = strchr(p,
',');
354 if (strlen(p) != 3 && (!next || next != p + 3))
379 const char default_language[] =
"und";
380 const char *language = lang && strlen(lang->
value) >= 3 ? lang->
value : default_language;
384 int extradata_copied = 0;
389 while (strlen(language) >= 3) {
390 if (
sizeof(data) - (q - data) < 8) {
398 if (*language !=
'\0')
404 extradata_copied += 5;
414 extradata_copied += 4;
423 *len_ptr = q - len_ptr - 1;
426 int extradata_copied = 0;
432 while (strlen(language) >= 3 && q - data <
sizeof(data) - 6) {
437 if (*language !=
'\0')
442 extradata_copied += 2;
454 *len_ptr = q - len_ptr - 1;
480 val = 0xf000 | (q - desc_length_ptr - 2);
481 desc_length_ptr[0] = val >> 8;
482 desc_length_ptr[1] =
val;
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);
518 int i, running_status, free_ca_mode,
val;
527 desc_list_len_ptr = q;
539 desc_len_ptr[0] = q - desc_len_ptr - 1;
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;
589 uint32_t tp_extra_header = pcr % 0x3fffffff;
590 tp_extra_header =
AV_RB32(&tp_extra_header);
592 sizeof(tp_extra_header));
611 const char *service_name;
632 provider_name, service_name);
639 service->
pmt.
cc = 15;
683 }
else if (st->
id < 0x1FFF) {
687 "Invalid stream id %d, must be less than 8191\n", st->
id);
696 for (j = 0; j < i; j++) {
697 if (pids[j] == ts_st->
pid) {
703 pids[i] = ts_st->
pid;
796 "pcr every %d pkts, sdt every %d, pat/pmt every %d pkts\n",
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;
893 *q++ = ts_st->
pid >> 8;
895 *q++ = 0x20 | ts_st->
cc;
913 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
915 val = (((pts >> 15) & 0x7fff) << 1) | 1;
918 val = (((
pts) & 0x7fff) << 1) | 1;
929 if ((pkt[3] & 0x20) == 0) {
951 return pkt + 5 + pkt[4];
961 const uint8_t *payload,
int payload_size,
962 int64_t
pts, int64_t dts,
int key)
968 int val, is_start,
len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext,
flags;
969 int afc_len, stuffing_len;
975 while (payload_size > 0) {
991 (dts -
get_pcr(ts, s->
pb) / 300) > delay) {
1004 val = ts_st->
pid >> 8;
1009 ts_st->
cc = ts_st->
cc + 1 & 0xf;
1010 *q++ = 0x10 | ts_st->
cc;
1025 pcr = (dts - delay) * 300;
1032 int pes_extension = 0;
1033 int pes_header_stuffing_bytes = 0;
1038 is_dvb_subtitle = 0;
1039 is_dvb_teletext = 0;
1058 is_dvb_subtitle = 1;
1060 is_dvb_teletext = 1;
1096 if (is_dvb_teletext) {
1097 pes_header_stuffing_bytes = 0x24 - header_len;
1100 len = payload_size + header_len + 3;
1102 if (is_dvb_subtitle) {
1147 if (is_dvb_subtitle) {
1154 if (is_dvb_teletext) {
1155 memset(q, 0xff, pes_header_stuffing_bytes);
1156 q += pes_header_stuffing_bytes;
1161 header_len = q -
buf;
1164 if (len > payload_size)
1167 if (stuffing_len > 0) {
1169 if (buf[3] & 0x20) {
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) {
1197 payload_size -=
len;
1209 "no startcode found, use the video bitstream filter 'h264_mp4toannexb' to fix it "
1210 "('-bsf:v h264_mp4toannexb' option with ffmpeg)\n");
1247 "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1273 uint32_t
state = -1;
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) {
1297 memcpy(data + 6 + extradd, pkt->
data, pkt->
size);
1302 size = pkt->
size + 6 + extradd;
1305 if (pkt->
size < 2) {
1315 "and extradata missing\n");
1447 {
"mpegts_transport_stream_id",
"Set transport_stream_id field.",
1450 {
"mpegts_original_network_id",
"Set original_network_id field.",
1453 {
"mpegts_service_id",
"Set service_id field.",
1456 {
"mpegts_service_type",
"Set service_type field.",
1459 {
"digital_tv",
"Digital Television.",
1462 {
"digital_radio",
"Digital Radio.",
1465 {
"teletext",
"Teletext.",
1468 {
"advanced_codec_digital_radio",
"Advanced Codec Digital Radio.",
1471 {
"mpeg2_digital_hdtv",
"MPEG2 Digital HDTV.",
1474 {
"advanced_codec_digital_sdtv",
"Advanced Codec Digital SDTV.",
1477 {
"advanced_codec_digital_hdtv",
"Advanced Codec Digital HDTV.",
1480 {
"mpegts_pmt_start_pid",
"Set the first pid of the PMT.",
1483 {
"mpegts_start_pid",
"Set the first pid.",
1486 {
"mpegts_m2ts_mode",
"Enable m2ts mode.",
1492 {
"pes_payload_size",
"Minimum PES packet payload in bytes",
1495 {
"mpegts_flags",
"MPEG-TS muxing flags",
1498 {
"resend_headers",
"Reemit PAT/PMT before writing the next packet",
1501 {
"latm",
"Use LATM packetization for AAC",
1505 {
"resend_headers",
"Reemit PAT/PMT before writing the next packet",
1508 {
"mpegts_copyts",
"don't offset dts/pts",
1511 {
"tables_version",
"set PAT, PMT and SDT version",
1514 {
"omit_video_pes_length",
"Omit the PES packet length for video packets",
1517 {
"pcr_period",
"PCR retransmission time",
1533 .mime_type =
"video/MP2T",
1534 .extensions =
"ts,m2t,m2ts,mts",
1542 .priv_class = &mpegts_muxer_class,