34 #define PCR_TIME_BASE 27000000
85 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
86 #define MPEGTS_FLAG_AAC_LATM 0x02
95 #define DEFAULT_PES_HEADER_FREQ 16
96 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
103 const unsigned char *buf_ptr;
105 int first,
b, len1, left;
108 buf[len - 4] = (crc >> 24) & 0xff;
109 buf[len - 3] = (crc >> 16) & 0xff;
110 buf[len - 2] = (crc >> 8) & 0xff;
111 buf[len - 1] = (crc) & 0xff;
116 first = (buf == buf_ptr);
124 s->
cc = (s->
cc + 1) & 0xf;
131 memcpy(q, buf_ptr, len1);
136 memset(q, 0xff, left);
155 int version,
int sec_num,
int last_sec_num,
159 unsigned int tot_len;
163 tot_len = 3 + 5 + len + 4;
170 put16(&q, flags | (len + 5 + 4));
172 *q++ = 0xc1 | (version << 1);
184 #define DEFAULT_PROVIDER_NAME "FFmpeg"
185 #define DEFAULT_SERVICE_NAME "Service01"
188 #define SDT_RETRANS_TIME 500
189 #define PAT_RETRANS_TIME 100
190 #define PCR_RETRANS_TIME 20
227 uint8_t data[1012], *q, *desc_length_ptr, *program_info_length_ptr;
228 int val, stream_type, i;
233 program_info_length_ptr = q;
238 val = 0xf000 | (q - program_info_length_ptr - 2);
239 program_info_length_ptr[0] = val >> 8;
240 program_info_length_ptr[1] =
val;
290 if (q - data >
sizeof(data) - 32)
317 char *next = lang->
value;
324 for (p = lang->
value; next && *len_ptr < 255 / 4 * 4 && q - data <
sizeof(data) - 4; p = next + 1) {
325 next = strchr(p,
',');
326 if (strlen(p) != 3 && (!next || next != p + 3))
351 const char default_language[] =
"und";
352 const char *language = lang && strlen(lang->
value) >= 3 ? lang->
value : default_language;
356 int extradata_copied = 0;
361 while (strlen(language) >= 3 && (
sizeof(data) - (q - data)) >= 8) {
366 if (*language !=
'\0')
372 extradata_copied += 5;
382 extradata_copied += 4;
391 *len_ptr = q - len_ptr - 1;
394 int extradata_copied = 0;
400 while (strlen(language) >= 3 && q - data <
sizeof(data) - 6) {
405 if (*language !=
'\0')
410 extradata_copied += 2;
422 *len_ptr = q - len_ptr - 1;
448 val = 0xf000 | (q - desc_length_ptr - 2);
449 desc_length_ptr[0] = val >> 8;
450 desc_length_ptr[1] =
val;
478 uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
479 int i, running_status, free_ca_mode,
val;
488 desc_list_len_ptr = q;
500 desc_len_ptr[0] = q - desc_len_ptr - 1;
503 val = (running_status << 13) | (free_ca_mode << 12) |
504 (q - desc_list_len_ptr - 2);
505 desc_list_len_ptr[0] = val >> 8;
506 desc_list_len_ptr[1] =
val;
542 uint32_t tp_extra_header = pcr % 0x3fffffff;
543 tp_extra_header =
AV_RB32(&tp_extra_header);
545 sizeof(tp_extra_header));
564 const char *service_name;
587 service->
pmt.
cc = 15;
627 }
else if (st->
id < 0x1FFF) {
639 for (j = 0; j < i; j++)
640 if (pids[j] == ts_st->
pid) {
645 pids[i] = ts_st->
pid;
737 "sdt every %d, pat/pmt every %d pkts\n",
792 int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
794 *buf++ = pcr_high >> 25;
795 *buf++ = pcr_high >> 17;
796 *buf++ = pcr_high >> 9;
797 *buf++ = pcr_high >> 1;
798 *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
830 *q++ = ts_st->
pid >> 8;
832 *q++ = 0x20 | ts_st->
cc;
850 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
852 val = (((pts >> 15) & 0x7fff) << 1) | 1;
855 val = (((pts) & 0x7fff) << 1) | 1;
866 if ((pkt[3] & 0x20) == 0) {
888 return pkt + 5 + pkt[4];
899 const uint8_t *payload,
int payload_size,
900 int64_t pts, int64_t dts,
int key)
906 int val, is_start,
len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext,
flags;
907 int afc_len, stuffing_len;
913 while (payload_size > 0) {
929 (dts -
get_pcr(ts, s->
pb)/300) > delay) {
941 val = (ts_st->
pid >> 8);
946 ts_st->
cc = (ts_st->
cc + 1) & 0xf;
947 *q++ = 0x10 | ts_st->
cc;
962 pcr = (dts - delay)*300;
969 int pes_extension = 0;
970 int pes_header_stuffing_bytes = 0;
1035 if (is_dvb_teletext) {
1036 pes_header_stuffing_bytes = 0x24 - header_len;
1039 len = payload_size + header_len + 3;
1041 if (is_dvb_subtitle) {
1088 if (is_dvb_subtitle) {
1095 if (is_dvb_teletext) {
1096 memset(q, 0xff, pes_header_stuffing_bytes);
1097 q += pes_header_stuffing_bytes;
1102 header_len = q -
buf;
1105 if (len > payload_size)
1108 if (stuffing_len > 0) {
1110 if (buf[3] & 0x20) {
1112 afc_len = buf[4] + 1;
1113 memmove(buf + 4 + afc_len + stuffing_len,
1115 header_len - (4 + afc_len));
1116 buf[4] += stuffing_len;
1117 memset(buf + 4 + afc_len, 0xff, stuffing_len);
1120 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
1122 buf[4] = stuffing_len - 1;
1123 if (stuffing_len >= 2) {
1125 memset(buf + 6, 0xff, stuffing_len - 2);
1130 if (is_dvb_subtitle && payload_size == len) {
1138 payload_size -=
len;
1151 "no startcode found, use the h264_mp4toannexb bitstream filter (-bsf h264_mp4toannexb)\n");
1168 int64_t dts = pkt->
dts, pts = pkt->
pts;
1171 av_log(s,
AV_LOG_WARNING,
"resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
1197 uint32_t
state = -1;
1204 av_dlog(s,
"nal %d\n", state & 0x1f);
1205 }
while (p < buf_end && (state & 0x1f) != 9 &&
1206 (state & 0x1f) != 5 && (state & 0x1f) != 1);
1208 if ((state & 0x1f) != 9) {
1212 memcpy(data+6, pkt->
data, pkt->
size);
1220 if (pkt->
size < 2) {
1230 "and extradata missing\n");
1358 {
"mpegts_transport_stream_id",
"Set transport_stream_id field.",
1360 {
"mpegts_original_network_id",
"Set original_network_id field.",
1362 {
"mpegts_service_id",
"Set service_id field.",
1364 {
"mpegts_pmt_start_pid",
"Set the first pid of the PMT.",
1366 {
"mpegts_start_pid",
"Set the first pid.",
1368 {
"mpegts_m2ts_mode",
"Enable m2ts mode.",
1372 {
"pes_payload_size",
"Minimum PES packet payload in bytes",
1376 {
"resend_headers",
"Reemit PAT/PMT before writing the next packet",
1379 {
"latm",
"Use LATM packetization for AAC",
1383 {
"resend_headers",
"Reemit PAT/PMT before writing the next packet",
1385 {
"mpegts_copyts",
"don't offset dts/pts",
1387 {
"tables_version",
"set PAT, PMT and SDT version",
1389 {
"omit_video_pes_length",
"Omit the PES packet length for video packets",
1391 {
"pcr_period",
"PCR retransmission time",
1406 .mime_type =
"video/MP2T",
1407 .extensions =
"ts,m2t,m2ts,mts",
1415 .priv_class = &mpegts_muxer_class,