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
157 {
"compute_pcr",
"Compute exact PCR for each transport stream packet.",
160 {
"ts_packetsize",
"Output option carrying the raw packet size.",
180 {.i64 = 0}, 0, 1, 0 },
182 {.i64 = 0}, 0, 1, 0 },
204 #define PES_START_SIZE 6
205 #define PES_HEADER_SIZE 9
206 #define MAX_PES_HEADER_SIZE (9 + 255)
235 for (i = 0; i < ts->
nb_prg; i++) {
236 if (ts->
prg[i].
id == programid) {
263 for (i = 0; i < ts->
nb_prg; i++)
264 if (ts->
prg[i].
id == programid) {
333 int used = 0, discarded = 0;
344 for (i = 0; i < ts->
nb_prg; i++) {
346 for (j = 0; j < p->
nb_pids; j++) {
347 if (p->
pids[j] != pid)
361 return !used && discarded;
369 const uint8_t *
buf,
int buf_size,
int is_start)
501 ts->
pids[pid] = NULL;
510 memset(stat, 0, packet_size *
sizeof(*stat));
512 for (i = 0; i < size - 3; i++) {
513 if (buf[i] == 0x47 && !(buf[i + 1] & 0x80) && buf[i + 3] != 0x47) {
514 int x = i % packet_size;
516 if (stat[x] > best_score) {
517 best_score = stat[x];
530 int score, fec_score, dvhs_score;
538 av_dlog(NULL,
"score: %d, dvhs_score: %d, fec_score: %d \n",
539 score, dvhs_score, fec_score);
541 if (score > fec_score && score > dvhs_score)
543 else if (dvhs_score > score && dvhs_score > fec_score)
545 else if (score < fec_score && dvhs_score < fec_score)
578 if ((p + 1) >= p_end)
594 len =
get8(&p, p_end);
597 if ((p + len) > p_end)
614 val =
get8(pp, p_end);
619 val =
get16(pp, p_end);
623 val =
get8(pp, p_end);
626 h->
version = (val >> 1) & 0x1f;
627 val =
get8(pp, p_end);
631 val =
get8(pp, p_end);
653 #if !CONFIG_LOAS_DEMUXER
715 uint32_t stream_type,
733 uint32_t stream_type, uint32_t prog_reg_desc)
752 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
758 if ((prog_reg_desc ==
AV_RL32(
"HDMV") ||
759 prog_reg_desc ==
AV_RL32(
"HDPR")) &&
770 memcpy(sub_pes, pes,
sizeof(*sub_pes));
778 sub_st->
id = pes->
pid;
848 int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
849 int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
850 int dts_flag = -1, cts_flag = -1;
860 au_start_flag = au_end_flag = 1;
870 if (!idle_flag && (!padding_flag || padding_bits != 0)) {
896 if (inst_bitrate_flag)
913 const uint8_t *
buf,
int buf_size,
int is_start,
935 while (buf_size > 0) {
936 switch (pes->
state) {
951 code = pes->
header[3] | 0x100;
985 if (code != 0x1bc && code != 0x1bf &&
986 code != 0x1f0 && code != 0x1f1 &&
987 code != 0x1ff && code != 0x1f2 &&
992 "pid=%x stream_type=%x probing\n",
1039 unsigned int flags, pes_ext, skip;
1045 if ((flags & 0xc0) == 0x80) {
1048 }
else if ((flags & 0xc0) == 0xc0) {
1058 skip = (pes_ext >> 4) & 0xb;
1061 if ((pes_ext & 0x41) == 0x01 &&
1064 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
1076 p += sl_header_bytes;
1077 buf_size -= sl_header_bytes;
1113 pes->
pts = pes->
dts = pcr;
1114 }
else if (pes->
dts > pcr + 3654 + 9000) {
1115 pes->
pts = pes->
dts = pcr + 3654 + 9000;
1204 Mp4Descr *descr,
int max_descr_count)
1207 if (size > (1 << 30))
1211 NULL, NULL, NULL, NULL)) < 0)
1227 (*len) -= new_off - *off;
1263 if (!(id_flags & 0x0020)) {
1348 if (len < 0 || len1 > len || len1 <= 0) {
1350 "Tag %x length violation new length %d bytes remaining %d\n",
1360 if (target_tag && tag != target_tag) {
1392 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1408 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1430 int mp4_descr_count = 0;
1435 p_end = section + section_len - 4;
1442 mp4_read_od(s, p, (
unsigned) (p_end - p), mp4_descr, &mp4_descr_count,
1448 for (i = 0; i < mp4_descr_count; i++) {
1462 pes->
sl = mp4_descr[i].
sl;
1465 mp4_descr[i].dec_config_descr_len, 0,
1466 NULL, NULL, NULL, NULL);
1485 for (i = 0; i < mp4_descr_count; i++)
1486 av_free(mp4_descr[i].dec_config_descr);
1491 Mp4Descr *mp4_descr,
int mp4_descr_count,
int pid,
1495 int desc_len, desc_tag, desc_es_id;
1499 desc_tag =
get8(pp, desc_list_end);
1502 desc_len =
get8(pp, desc_list_end);
1505 desc_end = *pp + desc_len;
1506 if (desc_end > desc_list_end)
1509 av_dlog(fc,
"tag: 0x%02x len=%d\n", desc_tag, desc_len);
1517 desc_es_id =
get16(pp, desc_end);
1518 if (ts && ts->
pids[pid])
1520 for (i = 0; i < mp4_descr_count; i++)
1521 if (mp4_descr[i].dec_config_descr_len &&
1522 mp4_descr[i].es_id == desc_es_id) {
1525 mp4_descr[i].dec_config_descr_len, 0,
1526 NULL, NULL, NULL, NULL);
1536 get16(pp, desc_end);
1537 if (mp4_descr_count > 0 &&
1543 NULL, NULL, NULL, NULL);
1555 int language_count = desc_len / 5;
1557 if (desc_len > 0 && desc_len % 5 != 0)
1560 if (language_count > 0) {
1562 if (language_count >
sizeof(language) / 4) {
1563 language_count =
sizeof(language) / 4;
1577 for (i = 0; i < language_count; i++) {
1578 language[i * 4 + 0] =
get8(pp, desc_end);
1579 language[i * 4 + 1] =
get8(pp, desc_end);
1580 language[i * 4 + 2] =
get8(pp, desc_end);
1581 language[i * 4 + 3] =
',';
1583 memcpy(extradata, *pp, 2);
1589 language[i * 4 - 1] = 0;
1601 int language_count = desc_len / 8;
1603 if (desc_len > 0 && desc_len % 8 != 0)
1606 if (language_count > 1) {
1610 if (language_count > 0) {
1614 if (language_count >
sizeof(language) / 4) {
1615 language_count =
sizeof(language) / 4;
1629 for (i = 0; i < language_count; i++) {
1630 language[i * 4 + 0] =
get8(pp, desc_end);
1631 language[i * 4 + 1] =
get8(pp, desc_end);
1632 language[i * 4 + 2] =
get8(pp, desc_end);
1633 language[i * 4 + 3] =
',';
1647 extradata[4] =
get8(pp, desc_end);
1648 memcpy(extradata, *pp, 4);
1654 language[i * 4 - 1] = 0;
1660 for (i = 0; i + 4 <= desc_len; i += 4) {
1661 language[i + 0] =
get8(pp, desc_end);
1662 language[i + 1] =
get8(pp, desc_end);
1663 language[i + 2] =
get8(pp, desc_end);
1664 language[i + 3] =
',';
1665 switch (
get8(pp, desc_end)) {
1678 language[i - 1] = 0;
1692 if (
get16(pp, desc_end) == 0xFFFF)
1694 if (
get8(pp, desc_end) == 0xFF) {
1713 const uint8_t *p, *p_end, *desc_list_end;
1714 int program_info_length, pcr_pid, pid, stream_type;
1716 uint32_t prog_reg_desc = 0;
1718 int mp4_descr_count = 0;
1725 p_end = section + section_len - 4;
1739 pcr_pid =
get16(&p, p_end);
1748 program_info_length =
get16(&p, p_end);
1749 if (program_info_length < 0)
1751 program_info_length &= 0xfff;
1752 while (program_info_length >= 2) {
1754 tag =
get8(&p, p_end);
1755 len =
get8(&p, p_end);
1757 av_dlog(ts->
stream,
"program tag: 0x%02x len=%d\n", tag, len);
1759 if (len > program_info_length - 2)
1762 program_info_length -= len + 2;
1769 }
else if (tag == 0x05 && len >= 4) {
1770 prog_reg_desc = bytestream_get_le32(&p);
1775 p += program_info_length;
1789 stream_type =
get8(&p, p_end);
1790 if (stream_type < 0)
1792 pid =
get16(&p, p_end);
1809 }
else if (stream_type != 0x13) {
1842 desc_list_len =
get16(&p, p_end);
1843 if (desc_list_len < 0)
1845 desc_list_len &= 0xfff;
1846 desc_list_end = p + desc_list_len;
1847 if (desc_list_end > p_end)
1851 desc_list_end, mp4_descr,
1852 mp4_descr_count, pid, ts) < 0)
1855 if (pes && prog_reg_desc ==
AV_RL32(
"HDMV") &&
1856 stream_type == 0x83 && pes->
sub_st) {
1865 if (!ts->
pids[pcr_pid])
1869 for (i = 0; i < mp4_descr_count; i++)
1870 av_free(mp4_descr[i].dec_config_descr);
1884 p_end = section + section_len - 4;
1897 sid =
get16(&p, p_end);
1900 pmt_pid =
get16(&p, p_end);
1910 if (sid == 0x0000) {
1919 || fil->
pid != pmt_pid
1923 if (!ts->
pids[pmt_pid])
1934 for (i = 0; i < ts->
nb_prg; i++)
1947 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
1948 int onid,
val, sid, desc_list_len, desc_tag, desc_len, service_type;
1949 char *
name, *provider_name;
1954 p_end = section + section_len - 4;
1962 onid =
get16(&p, p_end);
1965 val =
get8(&p, p_end);
1969 sid =
get16(&p, p_end);
1972 val =
get8(&p, p_end);
1975 desc_list_len =
get16(&p, p_end);
1976 if (desc_list_len < 0)
1978 desc_list_len &= 0xfff;
1979 desc_list_end = p + desc_list_len;
1980 if (desc_list_end > p_end)
1983 desc_tag =
get8(&p, desc_list_end);
1986 desc_len =
get8(&p, desc_list_end);
1987 desc_end = p + desc_len;
1988 if (desc_end > desc_list_end)
1992 desc_tag, desc_len);
1996 service_type =
get8(&p, p_end);
1997 if (service_type < 0)
1999 provider_name =
getstr8(&p, p_end);
2023 static int parse_pcr(int64_t *ppcr_high,
int *ppcr_low,
2030 int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
2031 has_adaptation, has_payload;
2035 pid =
AV_RB16(packet + 1) & 0x1fff;
2038 is_start = packet[1] & 0x40;
2039 tss = ts->
pids[pid];
2040 if (ts->
auto_guess && tss == NULL && is_start) {
2042 tss = ts->
pids[pid];
2048 afc = (packet[3] >> 4) & 3;
2051 has_adaptation = afc & 2;
2052 has_payload = afc & 1;
2053 is_discontinuity = has_adaptation &&
2058 cc = (packet[3] & 0xf);
2059 expected_cc = has_payload ? (tss->
last_cc + 1) & 0x0f : tss->
last_cc;
2060 cc_ok = pid == 0x1FFF ||
2068 "Continuity check failed for pid %d expected %d got %d\n",
2069 pid, expected_cc, cc);
2079 if (has_adaptation) {
2098 if (p + len > p_end)
2124 for (i = 0; i < ts->
nb_prg; i++) {
2140 if (
parse_pcr(&pcr_h, &pcr_l, packet) == 0)
2141 tss->
last_pcr = pcr_h * 300 + pcr_l;
2204 "max resync size reached, could not find sync byte\n");
2221 if ((*data)[0] != 0x47) {
2250 int packet_num,
ret = 0;
2275 if (nb_packets != 0 && packet_num >= nb_packets ||
2302 #define CHECK_COUNT 10
2303 #define CHECK_BLOCK 100
2313 score =
FFMAX3(score, dvhs_score, fec_score);
2315 maxscore =
FFMAX(maxscore, score);
2321 av_dlog(0,
"TS score: %d %d\n", sumscore, maxscore);
2337 afc = (packet[3] >> 4) & 3;
2347 if (!(flags & 0x10))
2352 *ppcr_high = ((int64_t) v << 1) | (p[4] >> 7);
2353 *ppcr_low = ((p[4] & 1) << 8) | p[5];
2387 if (s->
iformat == &ff_mpegts_demuxer) {
2407 int pcr_pid, pid, nb_packets, nb_pcrs,
ret, pcr_l;
2408 int64_t pcrs[2], pcr_h;
2409 int packet_count[2];
2430 pid =
AV_RB16(data + 1) & 0x1fff;
2431 if ((pcr_pid == -1 || pcr_pid == pid) &&
2435 packet_count[nb_pcrs] = nb_packets;
2436 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
2448 ts->
pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
2461 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
2467 int64_t pcr_h, next_pcr_h, pos;
2468 int pcr_l, next_pcr_l;
2480 if (data != pkt->
data)
2491 if (
parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
2494 ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
2501 ts->
cur_pcr = pcr_h * 300 + pcr_l;
2534 if (!ret && pkt->
size < 0)
2558 int64_t *ppos, int64_t pos_limit)
2561 int64_t pos, timestamp;
2563 int pcr_l, pcr_pid =
2569 while(pos < pos_limit) {
2574 if (buf[0] != 0x47) {
2581 if ((pcr_pid < 0 || (
AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
2582 parse_pcr(×tamp, &pcr_l, buf) == 0) {
2593 int64_t *ppos, int64_t pos_limit)
2602 while(pos < pos_limit) {
2657 if (buf[0] != 0x47) {
2687 .priv_class = &mpegts_class,
2691 .
name =
"mpegtsraw",
2699 .priv_class = &mpegtsraw_class,