42 #define MAX_RESYNC_SIZE 65536
44 #define MAX_PES_PAYLOAD 200*1024
46 #define MAX_MP4_DESCR_COUNT 16
87 #define MAX_PIDS_PER_PROGRAM 64
158 #define PES_START_SIZE 6
159 #define PES_HEADER_SIZE 9
160 #define MAX_PES_HEADER_SIZE (9 + 255)
205 for(i=0; i<ts->
nb_prg; i++)
206 if(ts->
prg[i].
id == programid)
233 for(i=0; i<ts->
nb_prg; i++) {
234 if(ts->
prg[i].
id == programid) {
269 int used = 0, discarded = 0;
271 for(i=0; i<ts->
nb_prg; i++) {
274 if(p->
pids[j] != pid)
288 return !used && discarded;
296 const uint8_t *
buf,
int buf_size,
int is_start)
416 ts->
pids[pid] = NULL;
425 memset(stat, 0, packet_size*
sizeof(
int));
427 for(x=i=0; i<size-3; i++){
428 if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && buf[i+3] != 0x47){
430 if(stat[x] > best_score){
437 if(x == packet_size) x= 0;
446 int score, fec_score, dvhs_score;
454 av_dlog(NULL,
"score: %d, dvhs_score: %d, fec_score: %d \n",
455 score, dvhs_score, fec_score);
457 if (score > fec_score && score > dvhs_score)
return TS_PACKET_SIZE;
490 if ((p + 1) >= p_end)
506 len =
get8(&p, p_end);
509 if ((p + len) > p_end)
526 val =
get8(pp, p_end);
531 val =
get16(pp, p_end);
535 val =
get8(pp, p_end);
538 h->
version = (val >> 1) & 0x1f;
539 val =
get8(pp, p_end);
543 val =
get8(pp, p_end);
565 #if !CONFIG_LOAS_DEMUXER
619 uint32_t stream_type,
const StreamType *types)
637 uint32_t stream_type, uint32_t prog_reg_desc)
656 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
662 if ((prog_reg_desc ==
AV_RL32(
"HDMV") ||
663 prog_reg_desc ==
AV_RL32(
"HDPR")) &&
674 memcpy(sub_pes, pes,
sizeof(*sub_pes));
682 sub_st->
id = pes->
pid;
745 int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
746 int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
747 int dts_flag = -1, cts_flag = -1;
757 au_start_flag = au_end_flag = 1;
767 if (!idle_flag && (!padding_flag || padding_bits != 0)) {
793 if (inst_bitrate_flag)
810 const uint8_t *
buf,
int buf_size,
int is_start,
831 while (buf_size > 0) {
847 code = pes->
header[3] | 0x100;
876 if (code != 0x1bc && code != 0x1bf &&
877 code != 0x1f0 && code != 0x1f1 &&
878 code != 0x1ff && code != 0x1f2 &&
928 unsigned int flags, pes_ext, skip;
934 if ((flags & 0xc0) == 0x80) {
937 }
else if ((flags & 0xc0) == 0xc0) {
947 skip = (pes_ext >> 4) & 0xb;
950 if ((pes_ext & 0x41) == 0x01 &&
953 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
964 p += sl_header_bytes;
965 buf_size -= sl_header_bytes;
970 if (buf_size > 0 && pes->
buffer) {
1051 NULL, NULL, NULL, NULL)) < 0)
1066 (*len) -= new_off - *
off;
1101 if (!(id_flags & 0x0020)) {
1178 if (len < 0 || len1 > len || len1 <= 0) {
1179 av_log(d->
s,
AV_LOG_ERROR,
"Tag %x length violation new length %d bytes remaining %d\n", tag, len1, len);
1188 if (target_tag && tag != target_tag) {
1218 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1231 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1250 int mp4_descr_count = 0;
1254 p_end = section + section_len - 4;
1266 for (i = 0; i < mp4_descr_count; i++) {
1281 pes->
sl = mp4_descr[i].
sl;
1284 mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
1303 for (i = 0; i < mp4_descr_count; i++)
1304 av_free(mp4_descr[i].dec_config_descr);
1309 Mp4Descr *mp4_descr,
int mp4_descr_count,
int pid,
1313 int desc_len, desc_tag, desc_es_id;
1317 desc_tag =
get8(pp, desc_list_end);
1320 desc_len =
get8(pp, desc_list_end);
1323 desc_end = *pp + desc_len;
1324 if (desc_end > desc_list_end)
1327 av_dlog(fc,
"tag: 0x%02x len=%d\n", desc_tag, desc_len);
1335 desc_es_id =
get16(pp, desc_end);
1336 if (ts && ts->
pids[pid])
1338 for (i = 0; i < mp4_descr_count; i++)
1339 if (mp4_descr[i].dec_config_descr_len &&
1340 mp4_descr[i].es_id == desc_es_id) {
1343 mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
1353 get16(pp, desc_end);
1368 language[0] =
get8(pp, desc_end);
1369 language[1] =
get8(pp, desc_end);
1370 language[2] =
get8(pp, desc_end);
1375 language[0] =
get8(pp, desc_end);
1376 language[1] =
get8(pp, desc_end);
1377 language[2] =
get8(pp, desc_end);
1380 switch(
get8(pp, desc_end)) {
1404 for (i = 0; i + 4 <= desc_len; i += 4) {
1405 language[i + 0] =
get8(pp, desc_end);
1406 language[i + 1] =
get8(pp, desc_end);
1407 language[i + 2] =
get8(pp, desc_end);
1408 language[i + 3] =
',';
1409 switch (
get8(pp, desc_end)) {
1416 language[i - 1] = 0;
1442 const uint8_t *p, *p_end, *desc_list_end;
1443 int program_info_length, pcr_pid, pid, stream_type;
1445 uint32_t prog_reg_desc = 0;
1448 int mp4_descr_count = 0;
1454 p_end = section + section_len - 4;
1466 pcr_pid =
get16(&p, p_end);
1475 program_info_length =
get16(&p, p_end);
1476 if (program_info_length < 0)
1478 program_info_length &= 0xfff;
1479 while(program_info_length >= 2) {
1481 tag =
get8(&p, p_end);
1482 len =
get8(&p, p_end);
1484 av_dlog(ts->
stream,
"program tag: 0x%02x len=%d\n", tag, len);
1486 if(len > program_info_length - 2)
1489 program_info_length -= len + 2;
1496 }
else if (tag == 0x05 && len >= 4) {
1497 prog_reg_desc = bytestream_get_le32(&p);
1502 p += program_info_length;
1513 stream_type =
get8(&p, p_end);
1514 if (stream_type < 0)
1516 pid =
get16(&p, p_end);
1533 }
else if (stream_type != 0x13) {
1565 desc_list_len =
get16(&p, p_end);
1566 if (desc_list_len < 0)
1568 desc_list_len &= 0xfff;
1569 desc_list_end = p + desc_list_len;
1570 if (desc_list_end > p_end)
1574 mp4_descr, mp4_descr_count, pid, ts) < 0)
1577 if (pes && prog_reg_desc ==
AV_RL32(
"HDMV") && stream_type == 0x83 && pes->
sub_st) {
1586 for (i = 0; i < mp4_descr_count; i++)
1587 av_free(mp4_descr[i].dec_config_descr);
1601 p_end = section + section_len - 4;
1612 sid =
get16(&p, p_end);
1615 pmt_pid =
get16(&p, p_end);
1625 if (sid == 0x0000) {
1634 || fil->
pid != pmt_pid
1638 if (!ts->
pids[pmt_pid])
1649 for (i=0; i<ts->
nb_prg; i++)
1662 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
1663 int onid,
val, sid, desc_list_len, desc_tag, desc_len, service_type;
1664 char *
name, *provider_name;
1669 p_end = section + section_len - 4;
1675 onid =
get16(&p, p_end);
1678 val =
get8(&p, p_end);
1682 sid =
get16(&p, p_end);
1685 val =
get8(&p, p_end);
1688 desc_list_len =
get16(&p, p_end);
1689 if (desc_list_len < 0)
1691 desc_list_len &= 0xfff;
1692 desc_list_end = p + desc_list_len;
1693 if (desc_list_end > p_end)
1696 desc_tag =
get8(&p, desc_list_end);
1699 desc_len =
get8(&p, desc_list_end);
1700 desc_end = p + desc_len;
1701 if (desc_end > desc_list_end)
1705 desc_tag, desc_len);
1709 service_type =
get8(&p, p_end);
1710 if (service_type < 0)
1712 provider_name =
getstr8(&p, p_end);
1740 int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
1741 has_adaptation, has_payload;
1745 pid =
AV_RB16(packet + 1) & 0x1fff;
1748 is_start = packet[1] & 0x40;
1749 tss = ts->
pids[pid];
1750 if (ts->
auto_guess && tss == NULL && is_start) {
1752 tss = ts->
pids[pid];
1758 afc = (packet[3] >> 4) & 3;
1761 has_adaptation = afc & 2;
1762 has_payload = afc & 1;
1763 is_discontinuity = has_adaptation
1765 && (packet[5] & 0x80);
1768 cc = (packet[3] & 0xf);
1769 expected_cc = has_payload ? (tss->
last_cc + 1) & 0x0f : tss->
last_cc;
1770 cc_ok = pid == 0x1FFF
1773 || expected_cc == cc;
1778 "Continuity check failed for pid %d expected %d got %d\n",
1779 pid, expected_cc, cc);
1789 if (has_adaptation) {
1805 if (p + len > p_end)
1869 if (buf[0] != 0x47) {
1890 int packet_num,
ret = 0;
1914 if (nb_packets != 0 && packet_num >= nb_packets ||
1940 #define CHECK_COUNT 10
1941 #define CHECK_BLOCK 100
1951 score =
FFMAX3(score, dvhs_score, fec_score);
1953 maxscore =
FFMAX(maxscore, score);
1959 av_dlog(0,
"TS score: %d %d\n", sumscore, maxscore);
1975 afc = (packet[3] >> 4) & 3;
1985 if (!(flags & 0x10))
1990 *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
1991 *ppcr_low = ((p[4] & 1) << 8) | p[5];
2025 if (s->
iformat == &ff_mpegts_demuxer) {
2045 int pcr_pid, pid, nb_packets, nb_pcrs,
ret, pcr_l;
2046 int64_t pcrs[2], pcr_h;
2047 int packet_count[2];
2067 pid =
AV_RB16(packet + 1) & 0x1fff;
2068 if ((pcr_pid == -1 || pcr_pid == pid) &&
2069 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
2071 packet_count[nb_pcrs] = nb_packets;
2072 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
2082 ts->
pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
2097 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
2104 int64_t pcr_h, next_pcr_h, pos;
2105 int pcr_l, next_pcr_l;
2124 if (
parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
2126 ts->
pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
2133 ts->
cur_pcr = pcr_h * 300 + pcr_l;
2168 if (!ret && pkt->
size < 0)
2191 int64_t *ppos, int64_t pos_limit)
2194 int64_t pos, timestamp;
2198 while(pos < pos_limit) {
2203 if (buf[0] != 0x47) {
2209 if ((pcr_pid < 0 || (
AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
2210 parse_pcr(×tamp, &pcr_l, buf) == 0) {
2221 int64_t *ppos, int64_t pos_limit)
2229 while(pos < pos_limit) {
2284 if (buf[0] != 0x47) {
2317 .
name =
"mpegtsraw",
2325 .priv_class = &mpegtsraw_class,