41 #define MAX_RESYNC_SIZE 65536
43 #define MAX_PES_PAYLOAD 200*1024
45 #define MAX_MP4_DESCR_COUNT 16
86 #define MAX_PIDS_PER_PROGRAM 64
157 #define PES_START_SIZE 6
158 #define PES_HEADER_SIZE 9
159 #define MAX_PES_HEADER_SIZE (9 + 255)
204 for(i=0; i<ts->
nb_prg; i++)
205 if(ts->
prg[i].
id == programid)
232 for(i=0; i<ts->
nb_prg; i++) {
233 if(ts->
prg[i].
id == programid) {
268 int used = 0, discarded = 0;
270 for(i=0; i<ts->
nb_prg; i++) {
273 if(p->
pids[j] != pid)
287 return !used && discarded;
295 const uint8_t *buf,
int buf_size,
int is_start)
424 memset(stat, 0, packet_size*
sizeof(
int));
426 for(x=i=0; i<size-3; i++){
427 if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && buf[i+3] != 0x47){
429 if(stat[x] > best_score){
436 if(x == packet_size) x= 0;
445 int score, fec_score, dvhs_score;
453 av_dlog(
NULL,
"score: %d, dvhs_score: %d, fec_score: %d \n",
454 score, dvhs_score, fec_score);
456 if (score > fec_score && score > dvhs_score)
return TS_PACKET_SIZE;
489 if ((p + 1) >= p_end)
505 len =
get8(&p, p_end);
508 if ((p + len) > p_end)
525 val =
get8(pp, p_end);
530 val =
get16(pp, p_end);
534 val =
get8(pp, p_end);
537 h->
version = (val >> 1) & 0x1f;
538 val =
get8(pp, p_end);
542 val =
get8(pp, p_end);
564 #if !CONFIG_LOAS_DEMUXER
617 uint32_t stream_type,
const StreamType *types)
635 uint32_t stream_type, uint32_t prog_reg_desc)
654 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
660 if ((prog_reg_desc ==
AV_RL32(
"HDMV") ||
661 prog_reg_desc ==
AV_RL32(
"HDPR")) &&
672 memcpy(sub_pes, pes,
sizeof(*sub_pes));
680 sub_st->
id = pes->
pid;
743 int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
744 int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
745 int dts_flag = -1, cts_flag = -1;
755 au_start_flag = au_end_flag = 1;
765 if (!idle_flag && (!padding_flag || padding_bits != 0)) {
791 if (inst_bitrate_flag)
808 const uint8_t *buf,
int buf_size,
int is_start,
829 while (buf_size > 0) {
845 code = pes->
header[3] | 0x100;
873 if (code != 0x1bc && code != 0x1bf &&
874 code != 0x1f0 && code != 0x1f1 &&
875 code != 0x1ff && code != 0x1f2 &&
925 unsigned int flags, pes_ext, skip;
931 if ((flags & 0xc0) == 0x80) {
934 }
else if ((flags & 0xc0) == 0xc0) {
944 skip = (pes_ext >> 4) & 0xb;
947 if ((pes_ext & 0x41) == 0x01 &&
950 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
961 p += sl_header_bytes;
962 buf_size -= sl_header_bytes;
967 if (buf_size > 0 && pes->
buffer) {
1063 (*len) -= new_off - *
off;
1098 if (!(id_flags & 0x0020)) {
1175 if (len < 0 || len1 > len || len1 <= 0) {
1176 av_log(d->
s,
AV_LOG_ERROR,
"Tag %x length violation new length %d bytes remaining %d\n", tag, len1, len);
1185 if (target_tag && tag != target_tag) {
1215 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1228 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1247 int mp4_descr_count = 0;
1251 p_end = section + section_len - 4;
1263 for (i = 0; i < mp4_descr_count; i++) {
1278 pes->
sl = mp4_descr[i].
sl;
1300 for (i = 0; i < mp4_descr_count; i++)
1301 av_free(mp4_descr[i].dec_config_descr);
1306 Mp4Descr *mp4_descr,
int mp4_descr_count,
int pid,
1310 int desc_len, desc_tag, desc_es_id;
1314 desc_tag =
get8(pp, desc_list_end);
1317 desc_len =
get8(pp, desc_list_end);
1320 desc_end = *pp + desc_len;
1321 if (desc_end > desc_list_end)
1324 av_dlog(fc,
"tag: 0x%02x len=%d\n", desc_tag, desc_len);
1332 desc_es_id =
get16(pp, desc_end);
1333 if (ts && ts->
pids[pid])
1335 for (i = 0; i < mp4_descr_count; i++)
1336 if (mp4_descr[i].dec_config_descr_len &&
1337 mp4_descr[i].es_id == desc_es_id) {
1350 get16(pp, desc_end);
1365 language[0] =
get8(pp, desc_end);
1366 language[1] =
get8(pp, desc_end);
1367 language[2] =
get8(pp, desc_end);
1372 language[0] =
get8(pp, desc_end);
1373 language[1] =
get8(pp, desc_end);
1374 language[2] =
get8(pp, desc_end);
1377 switch(
get8(pp, desc_end)) {
1401 for (i = 0; i + 4 <= desc_len; i += 4) {
1402 language[i + 0] =
get8(pp, desc_end);
1403 language[i + 1] =
get8(pp, desc_end);
1404 language[i + 2] =
get8(pp, desc_end);
1405 language[i + 3] =
',';
1406 switch (
get8(pp, desc_end)) {
1413 language[i - 1] = 0;
1439 const uint8_t *p, *p_end, *desc_list_end;
1440 int program_info_length, pcr_pid, pid, stream_type;
1442 uint32_t prog_reg_desc = 0;
1445 int mp4_descr_count = 0;
1451 p_end = section + section_len - 4;
1463 pcr_pid =
get16(&p, p_end);
1472 program_info_length =
get16(&p, p_end);
1473 if (program_info_length < 0)
1475 program_info_length &= 0xfff;
1476 while(program_info_length >= 2) {
1478 tag =
get8(&p, p_end);
1479 len =
get8(&p, p_end);
1481 av_dlog(ts->
stream,
"program tag: 0x%02x len=%d\n", tag, len);
1483 if(len > program_info_length - 2)
1486 program_info_length -= len + 2;
1493 }
else if (tag == 0x05 && len >= 4) {
1494 prog_reg_desc = bytestream_get_le32(&p);
1499 p += program_info_length;
1510 stream_type =
get8(&p, p_end);
1511 if (stream_type < 0)
1513 pid =
get16(&p, p_end);
1530 }
else if (stream_type != 0x13) {
1562 desc_list_len =
get16(&p, p_end);
1563 if (desc_list_len < 0)
1565 desc_list_len &= 0xfff;
1566 desc_list_end = p + desc_list_len;
1567 if (desc_list_end > p_end)
1571 mp4_descr, mp4_descr_count, pid, ts) < 0)
1574 if (pes && prog_reg_desc ==
AV_RL32(
"HDMV") && stream_type == 0x83 && pes->
sub_st) {
1583 for (i = 0; i < mp4_descr_count; i++)
1584 av_free(mp4_descr[i].dec_config_descr);
1598 p_end = section + section_len - 4;
1609 sid =
get16(&p, p_end);
1612 pmt_pid =
get16(&p, p_end);
1622 if (sid == 0x0000) {
1628 if (ts->
pids[pmt_pid])
1640 for (i=0; i<ts->
nb_prg; i++)
1653 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
1654 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
1655 char *
name, *provider_name;
1660 p_end = section + section_len - 4;
1666 onid =
get16(&p, p_end);
1669 val =
get8(&p, p_end);
1673 sid =
get16(&p, p_end);
1676 val =
get8(&p, p_end);
1679 desc_list_len =
get16(&p, p_end);
1680 if (desc_list_len < 0)
1682 desc_list_len &= 0xfff;
1683 desc_list_end = p + desc_list_len;
1684 if (desc_list_end > p_end)
1687 desc_tag =
get8(&p, desc_list_end);
1690 desc_len =
get8(&p, desc_list_end);
1691 desc_end = p + desc_len;
1692 if (desc_end > desc_list_end)
1696 desc_tag, desc_len);
1700 service_type =
get8(&p, p_end);
1701 if (service_type < 0)
1703 provider_name =
getstr8(&p, p_end);
1731 int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
1732 has_adaptation, has_payload;
1736 pid =
AV_RB16(packet + 1) & 0x1fff;
1739 is_start = packet[1] & 0x40;
1740 tss = ts->
pids[pid];
1743 tss = ts->
pids[pid];
1749 afc = (packet[3] >> 4) & 3;
1752 has_adaptation = afc & 2;
1753 has_payload = afc & 1;
1754 is_discontinuity = has_adaptation
1756 && (packet[5] & 0x80);
1759 cc = (packet[3] & 0xf);
1760 expected_cc = has_payload ? (tss->
last_cc + 1) & 0x0f : tss->
last_cc;
1761 cc_ok = pid == 0x1FFF
1764 || expected_cc == cc;
1769 "Continuity check failed for pid %d expected %d got %d\n",
1770 pid, expected_cc, cc);
1780 if (has_adaptation) {
1796 if (p + len > p_end)
1860 if (buf[0] != 0x47) {
1881 int packet_num, ret = 0;
1905 if (nb_packets != 0 && packet_num >= nb_packets ||
1931 #define CHECK_COUNT 10
1932 #define CHECK_BLOCK 100
1942 score =
FFMAX3(score, dvhs_score, fec_score);
1944 maxscore =
FFMAX(maxscore, score);
1950 av_dlog(0,
"TS score: %d %d\n", sumscore, maxscore);
1966 afc = (packet[3] >> 4) & 3;
1976 if (!(flags & 0x10))
1981 *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
1982 *ppcr_low = ((p[4] & 1) << 8) | p[5];
2005 if (s->
iformat == &ff_mpegts_demuxer) {
2029 int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
2030 int64_t pcrs[2], pcr_h;
2031 int packet_count[2];
2051 pid =
AV_RB16(packet + 1) & 0x1fff;
2052 if ((pcr_pid == -1 || pcr_pid == pid) &&
2053 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
2055 packet_count[nb_pcrs] = nb_packets;
2056 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
2066 ts->
pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
2081 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
2088 int64_t pcr_h, next_pcr_h, pos;
2089 int pcr_l, next_pcr_l;
2108 if (
parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
2110 ts->
pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
2117 ts->
cur_pcr = pcr_h * 300 + pcr_l;
2152 if (!ret && pkt->
size < 0)
2175 int64_t *ppos, int64_t pos_limit)
2178 int64_t pos, timestamp;
2182 while(pos < pos_limit) {
2187 if (buf[0] != 0x47) {
2193 if ((pcr_pid < 0 || (
AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
2194 parse_pcr(×tamp, &pcr_l, buf) == 0) {
2205 int64_t *ppos, int64_t pos_limit)
2213 while(pos < pos_limit) {
2268 if (buf[0] != 0x47) {
2301 .
name =
"mpegtsraw",
2309 .priv_class = &mpegtsraw_class,