42 #define MAX_RESYNC_SIZE 65536
44 #define MAX_PES_PAYLOAD 200 * 1024
46 #define MAX_MP4_DESCR_COUNT 16
48 #define MOD_UNLIKELY(modulus, dividend, divisor, prev_dividend) \
50 if ((prev_dividend) == 0 || (dividend) - (prev_dividend) != (divisor)) \
51 (modulus) = (dividend) % (divisor); \
52 (prev_dividend) = (dividend); \
64 int is_start, int64_t pos);
97 #define MAX_PIDS_PER_PROGRAM 64
116 #define SIZE_STAT_THRESHOLD 10
160 #define MPEGTS_OPTIONS \
161 { "resync_size", "Size limit for looking up a new synchronization.", offsetof(MpegTSContext, resync_size), AV_OPT_TYPE_INT, { .i64 = MAX_RESYNC_SIZE}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }
172 {.i64 = 0}, 0, 1, 0 },
174 {.i64 = 0}, 0, 1, 0 },
187 {
"compute_pcr",
"Compute exact PCR for each transport stream packet.",
190 {
"ts_packetsize",
"Output option carrying the raw packet size.",
215 #define PES_START_SIZE 6
216 #define PES_HEADER_SIZE 9
217 #define MAX_PES_HEADER_SIZE (9 + 255)
246 for (i = 0; i < ts->
nb_prg; i++) {
247 if (ts->
prg[i].
id == programid) {
274 for (i = 0; i < ts->
nb_prg; i++)
275 if (ts->
prg[i].
id == programid) {
312 for (i = 0; i < p->
nb_pids; i++)
313 if (p->
pids[i] == pid)
350 int used = 0, discarded = 0;
361 for (i = 0; i < ts->
nb_prg; i++) {
363 for (j = 0; j < p->
nb_pids; j++) {
364 if (p->
pids[j] != pid)
378 return !used && discarded;
386 const uint8_t *
buf,
int buf_size,
int is_start)
529 memset(stat, 0, packet_size *
sizeof(*stat));
531 for (i = 0; i < size - 3; i++) {
532 if (buf[i] == 0x47 &&
533 (!probe || (!(buf[i + 1] & 0x80) && buf[i + 3] != 0x47))) {
534 int x = i % packet_size;
537 if (stat[x] > best_score) {
538 best_score = stat[x];
545 return best_score -
FFMAX(stat_all - 10*best_score, 0)/10;
551 int score, fec_score, dvhs_score;
559 av_dlog(
NULL,
"score: %d, dvhs_score: %d, fec_score: %d \n",
560 score, dvhs_score, fec_score);
562 if (score > fec_score && score > dvhs_score)
564 else if (dvhs_score > score && dvhs_score > fec_score)
566 else if (score < fec_score && dvhs_score < fec_score)
599 if ((p + 1) >= p_end)
615 len =
get8(&p, p_end);
618 if ((p + len) > p_end)
635 val =
get8(pp, p_end);
640 val =
get16(pp, p_end);
644 val =
get8(pp, p_end);
647 h->
version = (val >> 1) & 0x1f;
648 val =
get8(pp, p_end);
652 val =
get8(pp, p_end);
674 #if !CONFIG_LOAS_DEMUXER
738 uint32_t stream_type,
756 uint32_t stream_type, uint32_t prog_reg_desc)
775 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
781 if ((prog_reg_desc ==
AV_RL32(
"HDMV") ||
782 prog_reg_desc ==
AV_RL32(
"HDPR")) &&
793 memcpy(sub_pes, pes,
sizeof(*sub_pes));
801 sub_st->
id = pes->
pid;
871 int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
872 int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
873 int dts_flag = -1, cts_flag = -1;
878 memcpy(buf_padded, buf, buf_padded_size);
887 au_start_flag = au_end_flag = 1;
897 if (!idle_flag && (!padding_flag || padding_bits != 0)) {
923 if (inst_bitrate_flag)
940 const uint8_t *
buf,
int buf_size,
int is_start,
962 while (buf_size > 0) {
963 switch (pes->
state) {
978 code = pes->
header[3] | 0x100;
1012 if (code != 0x1bc && code != 0x1bf &&
1013 code != 0x1f0 && code != 0x1f1 &&
1014 code != 0x1ff && code != 0x1f2 &&
1019 "pid=%x stream_type=%x probing\n",
1066 unsigned int flags, pes_ext, skip;
1072 if ((flags & 0xc0) == 0x80) {
1075 }
else if ((flags & 0xc0) == 0xc0) {
1085 skip = (pes_ext >> 4) & 0xb;
1088 if ((pes_ext & 0x41) == 0x01 &&
1091 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
1103 p += sl_header_bytes;
1104 buf_size -= sl_header_bytes;
1140 pes->
pts = pes->
dts = pcr;
1141 }
else if (pes->
dts > pcr + 3654 + 9000) {
1142 pes->
pts = pes->
dts = pcr + 3654 + 9000;
1231 Mp4Descr *descr,
int max_descr_count)
1234 if (size > (1 << 30))
1254 (*len) -= new_off - *off;
1290 if (!(id_flags & 0x0020)) {
1375 if (len < 0 || len1 > len || len1 <= 0) {
1377 "Tag %x length violation new length %d bytes remaining %d\n",
1387 if (target_tag && tag != target_tag) {
1419 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1435 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1457 int mp4_descr_count = 0;
1462 p_end = section + section_len - 4;
1469 mp4_read_od(s, p, (
unsigned) (p_end - p), mp4_descr, &mp4_descr_count,
1475 for (i = 0; i < mp4_descr_count; i++) {
1489 pes->
sl = mp4_descr[i].
sl;
1492 mp4_descr[i].dec_config_descr_len, 0,
1512 for (i = 0; i < mp4_descr_count; i++)
1513 av_free(mp4_descr[i].dec_config_descr);
1517 1, 0, 1, 1, 2, 2, 2, 3, 3
1521 1, 1, 1, 2, 2, 3, 4, 4, 5,
1532 { 0,6,1,2,3,4,5,7 },
1537 Mp4Descr *mp4_descr,
int mp4_descr_count,
int pid,
1541 int desc_len, desc_tag, desc_es_id, ext_desc_tag, channels, channel_config_code;
1545 desc_tag =
get8(pp, desc_list_end);
1548 desc_len =
get8(pp, desc_list_end);
1551 desc_end = *pp + desc_len;
1552 if (desc_end > desc_list_end)
1555 av_dlog(fc,
"tag: 0x%02x len=%d\n", desc_tag, desc_len);
1563 desc_es_id =
get16(pp, desc_end);
1566 if (ts && ts->
pids[pid])
1568 for (i = 0; i < mp4_descr_count; i++)
1569 if (mp4_descr[i].dec_config_descr_len &&
1570 mp4_descr[i].es_id == desc_es_id) {
1573 mp4_descr[i].dec_config_descr_len, 0,
1584 if (
get16(pp, desc_end) < 0)
1586 if (mp4_descr_count > 0 &&
1604 int language_count = desc_len / 5;
1606 if (desc_len > 0 && desc_len % 5 != 0)
1609 if (language_count > 0) {
1611 if (language_count >
sizeof(language) / 4) {
1612 language_count =
sizeof(language) / 4;
1626 for (i = 0; i < language_count; i++) {
1627 language[i * 4 + 0] =
get8(pp, desc_end);
1628 language[i * 4 + 1] =
get8(pp, desc_end);
1629 language[i * 4 + 2] =
get8(pp, desc_end);
1630 language[i * 4 + 3] =
',';
1632 memcpy(extradata, *pp, 2);
1638 language[i * 4 - 1] = 0;
1650 int language_count = desc_len / 8;
1652 if (desc_len > 0 && desc_len % 8 != 0)
1655 if (language_count > 1) {
1659 if (language_count > 0) {
1663 if (language_count >
sizeof(language) / 4) {
1664 language_count =
sizeof(language) / 4;
1678 for (i = 0; i < language_count; i++) {
1679 language[i * 4 + 0] =
get8(pp, desc_end);
1680 language[i * 4 + 1] =
get8(pp, desc_end);
1681 language[i * 4 + 2] =
get8(pp, desc_end);
1682 language[i * 4 + 3] =
',';
1696 extradata[4] =
get8(pp, desc_end);
1697 memcpy(extradata, *pp, 4);
1703 language[i * 4 - 1] = 0;
1709 for (i = 0; i + 4 <= desc_len; i += 4) {
1710 language[i + 0] =
get8(pp, desc_end);
1711 language[i + 1] =
get8(pp, desc_end);
1712 language[i + 2] =
get8(pp, desc_end);
1713 language[i + 3] =
',';
1714 switch (
get8(pp, desc_end)) {
1726 if (i && language[0]) {
1727 language[i - 1] = 0;
1741 if (
get16(pp, desc_end) == 0xFFFF)
1743 if (
get8(pp, desc_end) == 0xFF) {
1750 ext_desc_tag =
get8(pp, desc_end);
1751 if (ext_desc_tag < 0)
1754 ext_desc_tag == 0x80) {
1764 channel_config_code =
get8(pp, desc_end);
1765 if (channel_config_code < 0)
1767 if (channel_config_code <= 0x8) {
1768 st->
codec->
extradata[9] = channels = channel_config_code ? channel_config_code : 2;
1769 st->
codec->
extradata[18] = channel_config_code ? (channels > 2) : 255;
1793 const uint8_t *p, *p_end, *desc_list_end;
1794 int program_info_length, pcr_pid, pid, stream_type;
1796 uint32_t prog_reg_desc = 0;
1798 int mp4_descr_count = 0;
1805 p_end = section + section_len - 4;
1821 pcr_pid =
get16(&p, p_end);
1830 program_info_length =
get16(&p, p_end);
1831 if (program_info_length < 0)
1833 program_info_length &= 0xfff;
1834 while (program_info_length >= 2) {
1836 tag =
get8(&p, p_end);
1837 len =
get8(&p, p_end);
1839 av_dlog(ts->
stream,
"program tag: 0x%02x len=%d\n", tag, len);
1841 if (len > program_info_length - 2)
1844 program_info_length -= len + 2;
1851 }
else if (tag == 0x05 && len >= 4) {
1852 prog_reg_desc = bytestream_get_le32(&p);
1857 p += program_info_length;
1871 stream_type =
get8(&p, p_end);
1872 if (stream_type < 0)
1874 pid =
get16(&p, p_end);
1891 }
else if (stream_type != 0x13) {
1924 desc_list_len =
get16(&p, p_end);
1925 if (desc_list_len < 0)
1927 desc_list_len &= 0xfff;
1928 desc_list_end = p + desc_list_len;
1929 if (desc_list_end > p_end)
1933 desc_list_end, mp4_descr,
1934 mp4_descr_count, pid, ts) < 0)
1937 if (pes && prog_reg_desc ==
AV_RL32(
"HDMV") &&
1938 stream_type == 0x83 && pes->
sub_st) {
1947 if (!ts->
pids[pcr_pid])
1951 for (i = 0; i < mp4_descr_count; i++)
1952 av_free(mp4_descr[i].dec_config_descr);
1966 p_end = section + section_len - 4;
1979 sid =
get16(&p, p_end);
1982 pmt_pid =
get16(&p, p_end);
1992 if (sid == 0x0000) {
2003 || fil->
pid != pmt_pid
2007 if (!ts->
pids[pmt_pid])
2018 for (i = 0; i < ts->
nb_prg; i++)
2031 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
2032 int onid,
val, sid, desc_list_len, desc_tag, desc_len, service_type;
2033 char *
name, *provider_name;
2038 p_end = section + section_len - 4;
2046 onid =
get16(&p, p_end);
2049 val =
get8(&p, p_end);
2053 sid =
get16(&p, p_end);
2056 val =
get8(&p, p_end);
2059 desc_list_len =
get16(&p, p_end);
2060 if (desc_list_len < 0)
2062 desc_list_len &= 0xfff;
2063 desc_list_end = p + desc_list_len;
2064 if (desc_list_end > p_end)
2067 desc_tag =
get8(&p, desc_list_end);
2070 desc_len =
get8(&p, desc_list_end);
2071 desc_end = p + desc_len;
2072 if (desc_len < 0 || desc_end > desc_list_end)
2076 desc_tag, desc_len);
2080 service_type =
get8(&p, p_end);
2081 if (service_type < 0)
2083 provider_name =
getstr8(&p, p_end);
2107 static int parse_pcr(int64_t *ppcr_high,
int *ppcr_low,
2114 int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
2115 has_adaptation, has_payload;
2119 pid =
AV_RB16(packet + 1) & 0x1fff;
2122 is_start = packet[1] & 0x40;
2123 tss = ts->
pids[pid];
2126 tss = ts->
pids[pid];
2132 afc = (packet[3] >> 4) & 3;
2135 has_adaptation = afc & 2;
2136 has_payload = afc & 1;
2137 is_discontinuity = has_adaptation &&
2142 cc = (packet[3] & 0xf);
2143 expected_cc = has_payload ? (tss->
last_cc + 1) & 0x0f : tss->
last_cc;
2144 cc_ok = pid == 0x1FFF ||
2152 "Continuity check failed for pid %d expected %d got %d\n",
2153 pid, expected_cc, cc);
2161 if (has_adaptation) {
2164 if (
parse_pcr(&pcr_h, &pcr_l, packet) == 0)
2165 tss->
last_pcr = pcr_h * 300 + pcr_l;
2171 if (p >= p_end || !has_payload)
2184 if (p + len > p_end)
2210 for (i = 0; i < ts->
nb_prg; i++) {
2292 "max resync size reached, could not find sync byte\n");
2309 if ((*data)[0] != 0x47) {
2364 if (nb_packets != 0 && packet_num >= nb_packets ||
2391 #define CHECK_COUNT 10
2392 #define CHECK_BLOCK 100
2402 score =
FFMAX3(score, dvhs_score, fec_score);
2404 maxscore =
FFMAX(maxscore, score);
2410 av_dlog(0,
"TS score: %d %d\n", sumscore, maxscore);
2426 afc = (packet[3] >> 4) & 3;
2436 if (!(flags & 0x10))
2441 *ppcr_high = ((int64_t) v << 1) | (p[4] >> 7);
2442 *ppcr_low = ((p[4] & 1) << 8) | p[5];
2477 if (s->
iformat == &ff_mpegts_demuxer) {
2497 int pcr_pid, pid, nb_packets, nb_pcrs,
ret, pcr_l;
2498 int64_t pcrs[2], pcr_h;
2499 int packet_count[2];
2520 pid =
AV_RB16(data + 1) & 0x1fff;
2521 if ((pcr_pid == -1 || pcr_pid == pid) &&
2525 packet_count[nb_pcrs] = nb_packets;
2526 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
2538 ts->
pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
2551 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
2557 int64_t pcr_h, next_pcr_h, pos;
2558 int pcr_l, next_pcr_l;
2570 if (data != pkt->
data)
2581 if (
parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
2584 ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
2591 ts->
cur_pcr = pcr_h * 300 + pcr_l;
2624 if (!ret && pkt->
size < 0)
2648 int64_t *ppos, int64_t pos_limit)
2651 int64_t pos, timestamp;
2653 int pcr_l, pcr_pid =
2659 while(pos < pos_limit) {
2664 if (buf[0] != 0x47) {
2671 if ((pcr_pid < 0 || (
AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
2672 parse_pcr(×tamp, &pcr_l, buf) == 0) {
2683 int64_t *ppos, int64_t pos_limit)
2692 while(pos < pos_limit) {
2747 if (buf[0] != 0x47) {
2777 .priv_class = &mpegts_class,
2781 .
name =
"mpegtsraw",
2789 .priv_class = &mpegtsraw_class,