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
158 #define MPEGTS_OPTIONS \
159 { "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 }
168 {.i64 = 0}, 0, 1, 0 },
170 {.i64 = 0}, 0, 1, 0 },
183 {
"compute_pcr",
"Compute exact PCR for each transport stream packet.",
186 {
"ts_packetsize",
"Output option carrying the raw packet size.",
211 #define PES_START_SIZE 6
212 #define PES_HEADER_SIZE 9
213 #define MAX_PES_HEADER_SIZE (9 + 255)
242 for (i = 0; i < ts->
nb_prg; i++) {
243 if (ts->
prg[i].
id == programid) {
270 for (i = 0; i < ts->
nb_prg; i++)
271 if (ts->
prg[i].
id == programid) {
340 int used = 0, discarded = 0;
351 for (i = 0; i < ts->
nb_prg; i++) {
353 for (j = 0; j < p->
nb_pids; j++) {
354 if (p->
pids[j] != pid)
368 return !used && discarded;
376 const uint8_t *
buf,
int buf_size,
int is_start)
508 ts->
pids[pid] = NULL;
518 memset(stat, 0, packet_size *
sizeof(*stat));
520 for (i = 0; i < size - 3; i++) {
521 if (buf[i] == 0x47 && !(buf[i + 1] & 0x80) && buf[i + 3] != 0x47) {
522 int x = i % packet_size;
525 if (stat[x] > best_score) {
526 best_score = stat[x];
533 return best_score -
FFMAX(stat_all - 10*best_score, 0)/10;
539 int score, fec_score, dvhs_score;
547 av_dlog(NULL,
"score: %d, dvhs_score: %d, fec_score: %d \n",
548 score, dvhs_score, fec_score);
550 if (score > fec_score && score > dvhs_score)
552 else if (dvhs_score > score && dvhs_score > fec_score)
554 else if (score < fec_score && dvhs_score < fec_score)
587 if ((p + 1) >= p_end)
603 len =
get8(&p, p_end);
606 if ((p + len) > p_end)
623 val =
get8(pp, p_end);
628 val =
get16(pp, p_end);
632 val =
get8(pp, p_end);
635 h->
version = (val >> 1) & 0x1f;
636 val =
get8(pp, p_end);
640 val =
get8(pp, p_end);
662 #if !CONFIG_LOAS_DEMUXER
724 uint32_t stream_type,
742 uint32_t stream_type, uint32_t prog_reg_desc)
761 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
767 if ((prog_reg_desc ==
AV_RL32(
"HDMV") ||
768 prog_reg_desc ==
AV_RL32(
"HDPR")) &&
779 memcpy(sub_pes, pes,
sizeof(*sub_pes));
787 sub_st->
id = pes->
pid;
857 int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
858 int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
859 int dts_flag = -1, cts_flag = -1;
869 au_start_flag = au_end_flag = 1;
879 if (!idle_flag && (!padding_flag || padding_bits != 0)) {
905 if (inst_bitrate_flag)
922 const uint8_t *
buf,
int buf_size,
int is_start,
944 while (buf_size > 0) {
945 switch (pes->
state) {
960 code = pes->
header[3] | 0x100;
994 if (code != 0x1bc && code != 0x1bf &&
995 code != 0x1f0 && code != 0x1f1 &&
996 code != 0x1ff && code != 0x1f2 &&
1001 "pid=%x stream_type=%x probing\n",
1048 unsigned int flags, pes_ext, skip;
1054 if ((flags & 0xc0) == 0x80) {
1057 }
else if ((flags & 0xc0) == 0xc0) {
1067 skip = (pes_ext >> 4) & 0xb;
1070 if ((pes_ext & 0x41) == 0x01 &&
1073 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
1085 p += sl_header_bytes;
1086 buf_size -= sl_header_bytes;
1122 pes->
pts = pes->
dts = pcr;
1123 }
else if (pes->
dts > pcr + 3654 + 9000) {
1124 pes->
pts = pes->
dts = pcr + 3654 + 9000;
1213 Mp4Descr *descr,
int max_descr_count)
1216 if (size > (1 << 30))
1220 NULL, NULL, NULL, NULL)) < 0)
1236 (*len) -= new_off - *off;
1272 if (!(id_flags & 0x0020)) {
1357 if (len < 0 || len1 > len || len1 <= 0) {
1359 "Tag %x length violation new length %d bytes remaining %d\n",
1369 if (target_tag && tag != target_tag) {
1401 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1417 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1439 int mp4_descr_count = 0;
1444 p_end = section + section_len - 4;
1451 mp4_read_od(s, p, (
unsigned) (p_end - p), mp4_descr, &mp4_descr_count,
1457 for (i = 0; i < mp4_descr_count; i++) {
1471 pes->
sl = mp4_descr[i].
sl;
1474 mp4_descr[i].dec_config_descr_len, 0,
1475 NULL, NULL, NULL, NULL);
1494 for (i = 0; i < mp4_descr_count; i++)
1495 av_free(mp4_descr[i].dec_config_descr);
1500 Mp4Descr *mp4_descr,
int mp4_descr_count,
int pid,
1504 int desc_len, desc_tag, desc_es_id;
1508 desc_tag =
get8(pp, desc_list_end);
1511 desc_len =
get8(pp, desc_list_end);
1514 desc_end = *pp + desc_len;
1515 if (desc_end > desc_list_end)
1518 av_dlog(fc,
"tag: 0x%02x len=%d\n", desc_tag, desc_len);
1526 desc_es_id =
get16(pp, desc_end);
1527 if (ts && ts->
pids[pid])
1529 for (i = 0; i < mp4_descr_count; i++)
1530 if (mp4_descr[i].dec_config_descr_len &&
1531 mp4_descr[i].es_id == desc_es_id) {
1534 mp4_descr[i].dec_config_descr_len, 0,
1535 NULL, NULL, NULL, NULL);
1545 get16(pp, desc_end);
1546 if (mp4_descr_count > 0 &&
1552 NULL, NULL, NULL, NULL);
1564 int language_count = desc_len / 5;
1566 if (desc_len > 0 && desc_len % 5 != 0)
1569 if (language_count > 0) {
1571 if (language_count >
sizeof(language) / 4) {
1572 language_count =
sizeof(language) / 4;
1586 for (i = 0; i < language_count; i++) {
1587 language[i * 4 + 0] =
get8(pp, desc_end);
1588 language[i * 4 + 1] =
get8(pp, desc_end);
1589 language[i * 4 + 2] =
get8(pp, desc_end);
1590 language[i * 4 + 3] =
',';
1592 memcpy(extradata, *pp, 2);
1598 language[i * 4 - 1] = 0;
1610 int language_count = desc_len / 8;
1612 if (desc_len > 0 && desc_len % 8 != 0)
1615 if (language_count > 1) {
1619 if (language_count > 0) {
1623 if (language_count >
sizeof(language) / 4) {
1624 language_count =
sizeof(language) / 4;
1638 for (i = 0; i < language_count; i++) {
1639 language[i * 4 + 0] =
get8(pp, desc_end);
1640 language[i * 4 + 1] =
get8(pp, desc_end);
1641 language[i * 4 + 2] =
get8(pp, desc_end);
1642 language[i * 4 + 3] =
',';
1656 extradata[4] =
get8(pp, desc_end);
1657 memcpy(extradata, *pp, 4);
1663 language[i * 4 - 1] = 0;
1669 for (i = 0; i + 4 <= desc_len; i += 4) {
1670 language[i + 0] =
get8(pp, desc_end);
1671 language[i + 1] =
get8(pp, desc_end);
1672 language[i + 2] =
get8(pp, desc_end);
1673 language[i + 3] =
',';
1674 switch (
get8(pp, desc_end)) {
1686 if (i && language[0]) {
1687 language[i - 1] = 0;
1701 if (
get16(pp, desc_end) == 0xFFFF)
1703 if (
get8(pp, desc_end) == 0xFF) {
1722 const uint8_t *p, *p_end, *desc_list_end;
1723 int program_info_length, pcr_pid, pid, stream_type;
1725 uint32_t prog_reg_desc = 0;
1727 int mp4_descr_count = 0;
1734 p_end = section + section_len - 4;
1748 pcr_pid =
get16(&p, p_end);
1757 program_info_length =
get16(&p, p_end);
1758 if (program_info_length < 0)
1760 program_info_length &= 0xfff;
1761 while (program_info_length >= 2) {
1763 tag =
get8(&p, p_end);
1764 len =
get8(&p, p_end);
1766 av_dlog(ts->
stream,
"program tag: 0x%02x len=%d\n", tag, len);
1768 if (len > program_info_length - 2)
1771 program_info_length -= len + 2;
1778 }
else if (tag == 0x05 && len >= 4) {
1779 prog_reg_desc = bytestream_get_le32(&p);
1784 p += program_info_length;
1798 stream_type =
get8(&p, p_end);
1799 if (stream_type < 0)
1801 pid =
get16(&p, p_end);
1818 }
else if (stream_type != 0x13) {
1851 desc_list_len =
get16(&p, p_end);
1852 if (desc_list_len < 0)
1854 desc_list_len &= 0xfff;
1855 desc_list_end = p + desc_list_len;
1856 if (desc_list_end > p_end)
1860 desc_list_end, mp4_descr,
1861 mp4_descr_count, pid, ts) < 0)
1864 if (pes && prog_reg_desc ==
AV_RL32(
"HDMV") &&
1865 stream_type == 0x83 && pes->
sub_st) {
1874 if (!ts->
pids[pcr_pid])
1878 for (i = 0; i < mp4_descr_count; i++)
1879 av_free(mp4_descr[i].dec_config_descr);
1893 p_end = section + section_len - 4;
1906 sid =
get16(&p, p_end);
1909 pmt_pid =
get16(&p, p_end);
1919 if (sid == 0x0000) {
1928 || fil->
pid != pmt_pid
1932 if (!ts->
pids[pmt_pid])
1943 for (i = 0; i < ts->
nb_prg; i++)
1956 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
1957 int onid,
val, sid, desc_list_len, desc_tag, desc_len, service_type;
1958 char *
name, *provider_name;
1963 p_end = section + section_len - 4;
1971 onid =
get16(&p, p_end);
1974 val =
get8(&p, p_end);
1978 sid =
get16(&p, p_end);
1981 val =
get8(&p, p_end);
1984 desc_list_len =
get16(&p, p_end);
1985 if (desc_list_len < 0)
1987 desc_list_len &= 0xfff;
1988 desc_list_end = p + desc_list_len;
1989 if (desc_list_end > p_end)
1992 desc_tag =
get8(&p, desc_list_end);
1995 desc_len =
get8(&p, desc_list_end);
1996 desc_end = p + desc_len;
1997 if (desc_end > desc_list_end)
2001 desc_tag, desc_len);
2005 service_type =
get8(&p, p_end);
2006 if (service_type < 0)
2008 provider_name =
getstr8(&p, p_end);
2032 static int parse_pcr(int64_t *ppcr_high,
int *ppcr_low,
2039 int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
2040 has_adaptation, has_payload;
2044 pid =
AV_RB16(packet + 1) & 0x1fff;
2047 is_start = packet[1] & 0x40;
2048 tss = ts->
pids[pid];
2051 tss = ts->
pids[pid];
2057 afc = (packet[3] >> 4) & 3;
2060 has_adaptation = afc & 2;
2061 has_payload = afc & 1;
2062 is_discontinuity = has_adaptation &&
2067 cc = (packet[3] & 0xf);
2068 expected_cc = has_payload ? (tss->
last_cc + 1) & 0x0f : tss->
last_cc;
2069 cc_ok = pid == 0x1FFF ||
2077 "Continuity check failed for pid %d expected %d got %d\n",
2078 pid, expected_cc, cc);
2086 if (has_adaptation) {
2089 if (
parse_pcr(&pcr_h, &pcr_l, packet) == 0)
2090 tss->
last_pcr = pcr_h * 300 + pcr_l;
2096 if (p >= p_end || !has_payload)
2109 if (p + len > p_end)
2135 for (i = 0; i < ts->
nb_prg; i++) {
2212 "max resync size reached, could not find sync byte\n");
2229 if ((*data)[0] != 0x47) {
2284 if (nb_packets != 0 && packet_num >= nb_packets ||
2311 #define CHECK_COUNT 10
2312 #define CHECK_BLOCK 100
2322 score =
FFMAX3(score, dvhs_score, fec_score);
2324 maxscore =
FFMAX(maxscore, score);
2330 av_dlog(0,
"TS score: %d %d\n", sumscore, maxscore);
2346 afc = (packet[3] >> 4) & 3;
2356 if (!(flags & 0x10))
2361 *ppcr_high = ((int64_t) v << 1) | (p[4] >> 7);
2362 *ppcr_low = ((p[4] & 1) << 8) | p[5];
2396 if (s->
iformat == &ff_mpegts_demuxer) {
2416 int pcr_pid, pid, nb_packets, nb_pcrs,
ret, pcr_l;
2417 int64_t pcrs[2], pcr_h;
2418 int packet_count[2];
2439 pid =
AV_RB16(data + 1) & 0x1fff;
2440 if ((pcr_pid == -1 || pcr_pid == pid) &&
2444 packet_count[nb_pcrs] = nb_packets;
2445 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
2457 ts->
pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
2470 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
2476 int64_t pcr_h, next_pcr_h, pos;
2477 int pcr_l, next_pcr_l;
2489 if (data != pkt->
data)
2500 if (
parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
2503 ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
2510 ts->
cur_pcr = pcr_h * 300 + pcr_l;
2543 if (!ret && pkt->
size < 0)
2567 int64_t *ppos, int64_t pos_limit)
2570 int64_t pos, timestamp;
2572 int pcr_l, pcr_pid =
2578 while(pos < pos_limit) {
2583 if (buf[0] != 0x47) {
2590 if ((pcr_pid < 0 || (
AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
2591 parse_pcr(×tamp, &pcr_l, buf) == 0) {
2602 int64_t *ppos, int64_t pos_limit)
2611 while(pos < pos_limit) {
2666 if (buf[0] != 0x47) {
2696 .priv_class = &mpegts_class,
2700 .
name =
"mpegtsraw",
2708 .priv_class = &mpegtsraw_class,