43 #define MAX_RESYNC_SIZE 65536
45 #define MAX_PES_PAYLOAD 200*1024
47 #define MAX_MP4_DESCR_COUNT 16
88 #define MAX_PIDS_PER_PROGRAM 64
107 #define SIZE_STAT_THRESHOLD 10
186 #define PES_START_SIZE 6
187 #define PES_HEADER_SIZE 9
188 #define MAX_PES_HEADER_SIZE (9 + 255)
218 for(i=0; i<ts->
nb_prg; i++) {
219 if(ts->
prg[i].
id == programid) {
245 for(i=0; i<ts->
nb_prg; i++)
246 if(ts->
prg[i].
id == programid) {
314 int used = 0, discarded = 0;
327 for(i=0; i<ts->
nb_prg; i++) {
330 if(p->
pids[j] != pid)
344 return !used && discarded;
352 const uint8_t *
buf,
int buf_size,
int is_start)
472 ts->
pids[pid] = NULL;
480 memset(stat, 0, packet_size*
sizeof(*stat));
482 for(i=0; i<size-3; i++){
483 if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && buf[i+3] != 0x47){
484 int x = i % packet_size;
486 if(stat[x] > best_score){
499 int score, fec_score, dvhs_score;
507 av_dlog(NULL,
"score: %d, dvhs_score: %d, fec_score: %d \n",
508 score, dvhs_score, fec_score);
510 if (score > fec_score && score > dvhs_score)
return TS_PACKET_SIZE;
543 if ((p + 1) >= p_end)
559 len =
get8(&p, p_end);
562 if ((p + len) > p_end)
579 val =
get8(pp, p_end);
584 val =
get16(pp, p_end);
588 val =
get8(pp, p_end);
591 h->
version = (val >> 1) & 0x1f;
592 val =
get8(pp, p_end);
596 val =
get8(pp, p_end);
618 #if !CONFIG_LOAS_DEMUXER
680 uint32_t stream_type,
const StreamType *types)
698 uint32_t stream_type, uint32_t prog_reg_desc)
717 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
723 if ((prog_reg_desc ==
AV_RL32(
"HDMV") ||
724 prog_reg_desc ==
AV_RL32(
"HDPR")) &&
735 memcpy(sub_pes, pes,
sizeof(*sub_pes));
743 sub_st->
id = pes->
pid;
806 int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
807 int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
808 int dts_flag = -1, cts_flag = -1;
818 au_start_flag = au_end_flag = 1;
828 if (!idle_flag && (!padding_flag || padding_bits != 0)) {
854 if (inst_bitrate_flag)
871 const uint8_t *
buf,
int buf_size,
int is_start,
872 int64_t pos, int64_t pcr)
895 while (buf_size > 0) {
911 code = pes->
header[3] | 0x100;
940 if (code != 0x1bc && code != 0x1bf &&
941 code != 0x1f0 && code != 0x1f1 &&
942 code != 0x1ff && code != 0x1f2 &&
992 unsigned int flags, pes_ext, skip;
998 if ((flags & 0xc0) == 0x80) {
1001 }
else if ((flags & 0xc0) == 0xc0) {
1011 skip = (pes_ext >> 4) & 0xb;
1014 if ((pes_ext & 0x41) == 0x01 &&
1017 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
1028 p += sl_header_bytes;
1029 buf_size -= sl_header_bytes;
1049 int64_t pcr = pcrpes->
last_pcr / 300;
1053 pes->
pts = pes->
dts = pcr;
1054 }
else if (pes->
dts > pcr + 3654 + 9000) {
1055 pes->
pts = pes->
dts = pcr + 3654 + 9000;
1066 if (buf_size > 0 && pes->
buffer) {
1148 NULL, NULL, NULL, NULL)) < 0)
1163 (*len) -= new_off - *
off;
1198 if (!(id_flags & 0x0020)) {
1280 if (len < 0 || len1 > len || len1 <= 0) {
1281 av_log(d->
s,
AV_LOG_ERROR,
"Tag %x length violation new length %d bytes remaining %d\n", tag, len1, len);
1290 if (target_tag && tag != target_tag) {
1320 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1333 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1352 int mp4_descr_count = 0;
1356 p_end = section + section_len - 4;
1368 for (i = 0; i < mp4_descr_count; i++) {
1383 pes->
sl = mp4_descr[i].
sl;
1386 mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
1405 for (i = 0; i < mp4_descr_count; i++)
1406 av_free(mp4_descr[i].dec_config_descr);
1411 Mp4Descr *mp4_descr,
int mp4_descr_count,
int pid,
1415 int desc_len, desc_tag, desc_es_id;
1419 desc_tag =
get8(pp, desc_list_end);
1422 desc_len =
get8(pp, desc_list_end);
1425 desc_end = *pp + desc_len;
1426 if (desc_end > desc_list_end)
1429 av_dlog(fc,
"tag: 0x%02x len=%d\n", desc_tag, desc_len);
1437 desc_es_id =
get16(pp, desc_end);
1438 if (ts && ts->
pids[pid])
1440 for (i = 0; i < mp4_descr_count; i++)
1441 if (mp4_descr[i].dec_config_descr_len &&
1442 mp4_descr[i].es_id == desc_es_id) {
1445 mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
1455 get16(pp, desc_end);
1472 int language_count = desc_len / 5;
1474 if (desc_len > 0 && desc_len % 5 != 0)
1477 if (language_count > 0) {
1479 if (language_count >
sizeof(language) / 4) {
1480 language_count =
sizeof(language) / 4;
1494 for (i = 0; i < language_count; i++) {
1495 language[i * 4 + 0] =
get8(pp, desc_end);
1496 language[i * 4 + 1] =
get8(pp, desc_end);
1497 language[i * 4 + 2] =
get8(pp, desc_end);
1498 language[i * 4 + 3] =
',';
1500 memcpy(extradata, *pp, 2);
1506 language[i * 4 - 1] = 0;
1518 int language_count = desc_len / 8;
1520 if (desc_len > 0 && desc_len % 8 != 0)
1523 if (language_count > 1) {
1527 if (language_count > 0) {
1531 if (language_count >
sizeof(language) / 4) {
1532 language_count =
sizeof(language) / 4;
1546 for (i = 0; i < language_count; i++) {
1547 language[i * 4 + 0] =
get8(pp, desc_end);
1548 language[i * 4 + 1] =
get8(pp, desc_end);
1549 language[i * 4 + 2] =
get8(pp, desc_end);
1550 language[i * 4 + 3] =
',';
1564 extradata[4] =
get8(pp, desc_end);
1565 memcpy(extradata, *pp, 4);
1571 language[i * 4 - 1] = 0;
1577 for (i = 0; i + 4 <= desc_len; i += 4) {
1578 language[i + 0] =
get8(pp, desc_end);
1579 language[i + 1] =
get8(pp, desc_end);
1580 language[i + 2] =
get8(pp, desc_end);
1581 language[i + 3] =
',';
1582 switch (
get8(pp, desc_end)) {
1589 language[i - 1] = 0;
1603 if (
get16(pp, desc_end) == 0xFFFF)
1605 if (
get8(pp, desc_end) == 0xFF) {
1624 const uint8_t *p, *p_end, *desc_list_end;
1625 int program_info_length, pcr_pid, pid, stream_type;
1627 uint32_t prog_reg_desc = 0;
1630 int mp4_descr_count = 0;
1636 p_end = section + section_len - 4;
1648 pcr_pid =
get16(&p, p_end);
1657 program_info_length =
get16(&p, p_end);
1658 if (program_info_length < 0)
1660 program_info_length &= 0xfff;
1661 while(program_info_length >= 2) {
1663 tag =
get8(&p, p_end);
1664 len =
get8(&p, p_end);
1666 av_dlog(ts->
stream,
"program tag: 0x%02x len=%d\n", tag, len);
1668 if(len > program_info_length - 2)
1671 program_info_length -= len + 2;
1678 }
else if (tag == 0x05 && len >= 4) {
1679 prog_reg_desc = bytestream_get_le32(&p);
1684 p += program_info_length;
1697 stream_type =
get8(&p, p_end);
1698 if (stream_type < 0)
1700 pid =
get16(&p, p_end);
1717 }
else if (stream_type != 0x13) {
1749 desc_list_len =
get16(&p, p_end);
1750 if (desc_list_len < 0)
1752 desc_list_len &= 0xfff;
1753 desc_list_end = p + desc_list_len;
1754 if (desc_list_end > p_end)
1758 mp4_descr, mp4_descr_count, pid, ts) < 0)
1761 if (pes && prog_reg_desc ==
AV_RL32(
"HDMV") && stream_type == 0x83 && pes->
sub_st) {
1770 for (i = 0; i < mp4_descr_count; i++)
1771 av_free(mp4_descr[i].dec_config_descr);
1785 p_end = section + section_len - 4;
1796 sid =
get16(&p, p_end);
1799 pmt_pid =
get16(&p, p_end);
1809 if (sid == 0x0000) {
1818 || fil->
pid != pmt_pid
1822 if (!ts->
pids[pmt_pid])
1833 for (i=0; i<ts->
nb_prg; i++)
1846 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
1847 int onid,
val, sid, desc_list_len, desc_tag, desc_len, service_type;
1848 char *
name, *provider_name;
1853 p_end = section + section_len - 4;
1859 onid =
get16(&p, p_end);
1862 val =
get8(&p, p_end);
1866 sid =
get16(&p, p_end);
1869 val =
get8(&p, p_end);
1872 desc_list_len =
get16(&p, p_end);
1873 if (desc_list_len < 0)
1875 desc_list_len &= 0xfff;
1876 desc_list_end = p + desc_list_len;
1877 if (desc_list_end > p_end)
1880 desc_tag =
get8(&p, desc_list_end);
1883 desc_len =
get8(&p, desc_list_end);
1884 desc_end = p + desc_len;
1885 if (desc_end > desc_list_end)
1889 desc_tag, desc_len);
1893 service_type =
get8(&p, p_end);
1894 if (service_type < 0)
1896 provider_name =
getstr8(&p, p_end);
1919 static int parse_pcr(int64_t *ppcr_high,
int *ppcr_low,
1927 int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
1928 has_adaptation, has_payload;
1932 pid =
AV_RB16(packet + 1) & 0x1fff;
1935 is_start = packet[1] & 0x40;
1936 tss = ts->
pids[pid];
1937 if (ts->
auto_guess && tss == NULL && is_start) {
1939 tss = ts->
pids[pid];
1945 afc = (packet[3] >> 4) & 3;
1948 has_adaptation = afc & 2;
1949 has_payload = afc & 1;
1950 is_discontinuity = has_adaptation
1952 && (packet[5] & 0x80);
1955 cc = (packet[3] & 0xf);
1956 expected_cc = has_payload ? (tss->
last_cc + 1) & 0x0f : tss->
last_cc;
1957 cc_ok = pid == 0x1FFF
1960 || expected_cc == cc;
1965 "Continuity check failed for pid %d expected %d got %d\n",
1966 pid, expected_cc, cc);
1976 if (has_adaptation) {
1995 if (p + len > p_end)
2021 for(i=0; i<ts->
nb_prg; i++) {
2038 if (
parse_pcr(&pcr_h, &pcr_l, packet) == 0)
2039 pcr = pcr_h * 300 + pcr_l;
2115 if ((*data)[0] != 0x47) {
2144 int packet_num,
ret = 0;
2169 if (nb_packets != 0 && packet_num >= nb_packets ||
2196 #define CHECK_COUNT 10
2197 #define CHECK_BLOCK 100
2207 score =
FFMAX3(score, dvhs_score, fec_score);
2209 maxscore =
FFMAX(maxscore, score);
2215 av_dlog(0,
"TS score: %d %d\n", sumscore, maxscore);
2231 afc = (packet[3] >> 4) & 3;
2241 if (!(flags & 0x10))
2246 *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
2247 *ppcr_low = ((p[4] & 1) << 8) | p[5];
2281 if (s->
iformat == &ff_mpegts_demuxer) {
2301 int pcr_pid, pid, nb_packets, nb_pcrs,
ret, pcr_l;
2302 int64_t pcrs[2], pcr_h;
2303 int packet_count[2];
2324 pid =
AV_RB16(data + 1) & 0x1fff;
2325 if ((pcr_pid == -1 || pcr_pid == pid) &&
2329 packet_count[nb_pcrs] = nb_packets;
2330 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
2342 ts->
pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
2357 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
2364 int64_t pcr_h, next_pcr_h, pos;
2365 int pcr_l, next_pcr_l;
2377 if (data != pkt->
data)
2388 if (
parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
2390 ts->
pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
2397 ts->
cur_pcr = pcr_h * 300 + pcr_l;
2432 if (!ret && pkt->
size < 0)
2455 int64_t *ppos, int64_t pos_limit)
2458 int64_t pos, timestamp;
2463 while(pos < pos_limit) {
2468 if (buf[0] != 0x47) {
2475 if ((pcr_pid < 0 || (
AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
2476 parse_pcr(×tamp, &pcr_l, buf) == 0) {
2487 int64_t *ppos, int64_t pos_limit)
2496 while(pos < pos_limit) {
2551 if (buf[0] != 0x47) {
2581 .priv_class = &mpegts_class,
2585 .
name =
"mpegtsraw",
2593 .priv_class = &mpegtsraw_class,