43 #define MAX_RESYNC_SIZE 65536
45 #define MAX_PES_PAYLOAD 200 * 1024
47 #define MAX_MP4_DESCR_COUNT 16
49 #define MOD_UNLIKELY(modulus, dividend, divisor, prev_dividend) \
51 if ((prev_dividend) == 0 || (dividend) - (prev_dividend) != (divisor)) \
52 (modulus) = (dividend) % (divisor); \
53 (prev_dividend) = (dividend); \
65 int is_start, int64_t pos);
98 #define MAX_PIDS_PER_PROGRAM 64
117 #define SIZE_STAT_THRESHOLD 10
161 #define MPEGTS_OPTIONS \
162 { "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 }
173 {.i64 = 0}, 0, 1, 0 },
175 {.i64 = 0}, 0, 1, 0 },
188 {
"compute_pcr",
"Compute exact PCR for each transport stream packet.",
191 {
"ts_packetsize",
"Output option carrying the raw packet size.",
216 #define PES_START_SIZE 6
217 #define PES_HEADER_SIZE 9
218 #define MAX_PES_HEADER_SIZE (9 + 255)
247 for (i = 0; i < ts->
nb_prg; i++) {
248 if (ts->
prg[i].
id == programid) {
275 for (i = 0; i < ts->
nb_prg; i++)
276 if (ts->
prg[i].
id == programid) {
313 for (i = 0; i < p->
nb_pids; i++)
314 if (p->
pids[i] == pid)
351 int used = 0, discarded = 0;
362 for (i = 0; i < ts->
nb_prg; i++) {
364 for (j = 0; j < p->
nb_pids; j++) {
365 if (p->
pids[j] != pid)
379 return !used && discarded;
387 const uint8_t *
buf,
int buf_size,
int is_start)
519 ts->
pids[pid] = NULL;
529 memset(stat, 0, packet_size *
sizeof(*stat));
531 for (i = 0; i < size - 3; i++) {
532 if (buf[i] == 0x47 && !(buf[i + 1] & 0x80) && buf[i + 3] != 0x47) {
533 int x = i % packet_size;
536 if (stat[x] > best_score) {
537 best_score = stat[x];
544 return best_score -
FFMAX(stat_all - 10*best_score, 0)/10;
550 int score, fec_score, dvhs_score;
558 av_dlog(NULL,
"score: %d, dvhs_score: %d, fec_score: %d \n",
559 score, dvhs_score, fec_score);
561 if (score > fec_score && score > dvhs_score)
563 else if (dvhs_score > score && dvhs_score > fec_score)
565 else if (score < fec_score && dvhs_score < fec_score)
598 if ((p + 1) >= p_end)
614 len =
get8(&p, p_end);
617 if ((p + len) > p_end)
634 val =
get8(pp, p_end);
639 val =
get16(pp, p_end);
643 val =
get8(pp, p_end);
646 h->
version = (val >> 1) & 0x1f;
647 val =
get8(pp, p_end);
651 val =
get8(pp, p_end);
673 #if !CONFIG_LOAS_DEMUXER
736 uint32_t stream_type,
754 uint32_t stream_type, uint32_t prog_reg_desc)
773 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
779 if ((prog_reg_desc ==
AV_RL32(
"HDMV") ||
780 prog_reg_desc ==
AV_RL32(
"HDPR")) &&
791 memcpy(sub_pes, pes,
sizeof(*sub_pes));
799 sub_st->
id = pes->
pid;
869 int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
870 int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
871 int dts_flag = -1, cts_flag = -1;
876 memcpy(buf_padded, buf, buf_padded_size);
885 au_start_flag = au_end_flag = 1;
895 if (!idle_flag && (!padding_flag || padding_bits != 0)) {
921 if (inst_bitrate_flag)
938 const uint8_t *
buf,
int buf_size,
int is_start,
960 while (buf_size > 0) {
961 switch (pes->
state) {
976 code = pes->
header[3] | 0x100;
1010 if (code != 0x1bc && code != 0x1bf &&
1011 code != 0x1f0 && code != 0x1f1 &&
1012 code != 0x1ff && code != 0x1f2 &&
1017 "pid=%x stream_type=%x probing\n",
1064 unsigned int flags, pes_ext, skip;
1070 if ((flags & 0xc0) == 0x80) {
1073 }
else if ((flags & 0xc0) == 0xc0) {
1083 skip = (pes_ext >> 4) & 0xb;
1086 if ((pes_ext & 0x41) == 0x01 &&
1089 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
1101 p += sl_header_bytes;
1102 buf_size -= sl_header_bytes;
1138 pes->
pts = pes->
dts = pcr;
1139 }
else if (pes->
dts > pcr + 3654 + 9000) {
1140 pes->
pts = pes->
dts = pcr + 3654 + 9000;
1229 Mp4Descr *descr,
int max_descr_count)
1232 if (size > (1 << 30))
1236 NULL, NULL, NULL, NULL)) < 0)
1252 (*len) -= new_off - *off;
1288 if (!(id_flags & 0x0020)) {
1373 if (len < 0 || len1 > len || len1 <= 0) {
1375 "Tag %x length violation new length %d bytes remaining %d\n",
1385 if (target_tag && tag != target_tag) {
1417 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1433 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1455 int mp4_descr_count = 0;
1460 p_end = section + section_len - 4;
1467 mp4_read_od(s, p, (
unsigned) (p_end - p), mp4_descr, &mp4_descr_count,
1473 for (i = 0; i < mp4_descr_count; i++) {
1487 pes->
sl = mp4_descr[i].
sl;
1490 mp4_descr[i].dec_config_descr_len, 0,
1491 NULL, NULL, NULL, NULL);
1510 for (i = 0; i < mp4_descr_count; i++)
1511 av_free(mp4_descr[i].dec_config_descr);
1515 1, 0, 1, 1, 2, 2, 2, 3, 3
1526 { 0,6,1,2,3,4,5,7 },
1531 Mp4Descr *mp4_descr,
int mp4_descr_count,
int pid,
1535 int desc_len, desc_tag, desc_es_id, ext_desc_tag, channels, channel_config_code;
1539 desc_tag =
get8(pp, desc_list_end);
1542 desc_len =
get8(pp, desc_list_end);
1545 desc_end = *pp + desc_len;
1546 if (desc_end > desc_list_end)
1549 av_dlog(fc,
"tag: 0x%02x len=%d\n", desc_tag, desc_len);
1557 desc_es_id =
get16(pp, desc_end);
1558 if (ts && ts->
pids[pid])
1560 for (i = 0; i < mp4_descr_count; i++)
1561 if (mp4_descr[i].dec_config_descr_len &&
1562 mp4_descr[i].es_id == desc_es_id) {
1565 mp4_descr[i].dec_config_descr_len, 0,
1566 NULL, NULL, NULL, NULL);
1576 get16(pp, desc_end);
1577 if (mp4_descr_count > 0 &&
1583 NULL, NULL, NULL, NULL);
1595 int language_count = desc_len / 5;
1597 if (desc_len > 0 && desc_len % 5 != 0)
1600 if (language_count > 0) {
1602 if (language_count >
sizeof(language) / 4) {
1603 language_count =
sizeof(language) / 4;
1617 for (i = 0; i < language_count; i++) {
1618 language[i * 4 + 0] =
get8(pp, desc_end);
1619 language[i * 4 + 1] =
get8(pp, desc_end);
1620 language[i * 4 + 2] =
get8(pp, desc_end);
1621 language[i * 4 + 3] =
',';
1623 memcpy(extradata, *pp, 2);
1629 language[i * 4 - 1] = 0;
1641 int language_count = desc_len / 8;
1643 if (desc_len > 0 && desc_len % 8 != 0)
1646 if (language_count > 1) {
1650 if (language_count > 0) {
1654 if (language_count >
sizeof(language) / 4) {
1655 language_count =
sizeof(language) / 4;
1669 for (i = 0; i < language_count; i++) {
1670 language[i * 4 + 0] =
get8(pp, desc_end);
1671 language[i * 4 + 1] =
get8(pp, desc_end);
1672 language[i * 4 + 2] =
get8(pp, desc_end);
1673 language[i * 4 + 3] =
',';
1687 extradata[4] =
get8(pp, desc_end);
1688 memcpy(extradata, *pp, 4);
1694 language[i * 4 - 1] = 0;
1700 for (i = 0; i + 4 <= desc_len; i += 4) {
1701 language[i + 0] =
get8(pp, desc_end);
1702 language[i + 1] =
get8(pp, desc_end);
1703 language[i + 2] =
get8(pp, desc_end);
1704 language[i + 3] =
',';
1705 switch (
get8(pp, desc_end)) {
1717 if (i && language[0]) {
1718 language[i - 1] = 0;
1732 if (
get16(pp, desc_end) == 0xFFFF)
1734 if (
get8(pp, desc_end) == 0xFF) {
1741 ext_desc_tag =
get8(pp, desc_end);
1742 if (ext_desc_tag < 0)
1745 ext_desc_tag == 0x80) {
1755 channel_config_code =
get8(pp, desc_end);
1756 if (channel_config_code < 0)
1758 if (channel_config_code <= 0x8) {
1759 st->
codec->
extradata[9] = channels = channel_config_code ? channel_config_code : 2;
1762 if (channel_config_code == 0) {
1788 const uint8_t *p, *p_end, *desc_list_end;
1789 int program_info_length, pcr_pid, pid, stream_type;
1791 uint32_t prog_reg_desc = 0;
1793 int mp4_descr_count = 0;
1800 p_end = section + section_len - 4;
1816 pcr_pid =
get16(&p, p_end);
1825 program_info_length =
get16(&p, p_end);
1826 if (program_info_length < 0)
1828 program_info_length &= 0xfff;
1829 while (program_info_length >= 2) {
1831 tag =
get8(&p, p_end);
1832 len =
get8(&p, p_end);
1834 av_dlog(ts->
stream,
"program tag: 0x%02x len=%d\n", tag, len);
1836 if (len > program_info_length - 2)
1839 program_info_length -= len + 2;
1846 }
else if (tag == 0x05 && len >= 4) {
1847 prog_reg_desc = bytestream_get_le32(&p);
1852 p += program_info_length;
1866 stream_type =
get8(&p, p_end);
1867 if (stream_type < 0)
1869 pid =
get16(&p, p_end);
1886 }
else if (stream_type != 0x13) {
1919 desc_list_len =
get16(&p, p_end);
1920 if (desc_list_len < 0)
1922 desc_list_len &= 0xfff;
1923 desc_list_end = p + desc_list_len;
1924 if (desc_list_end > p_end)
1928 desc_list_end, mp4_descr,
1929 mp4_descr_count, pid, ts) < 0)
1932 if (pes && prog_reg_desc ==
AV_RL32(
"HDMV") &&
1933 stream_type == 0x83 && pes->
sub_st) {
1942 if (!ts->
pids[pcr_pid])
1946 for (i = 0; i < mp4_descr_count; i++)
1947 av_free(mp4_descr[i].dec_config_descr);
1961 p_end = section + section_len - 4;
1974 sid =
get16(&p, p_end);
1977 pmt_pid =
get16(&p, p_end);
1987 if (sid == 0x0000) {
1998 || fil->
pid != pmt_pid
2002 if (!ts->
pids[pmt_pid])
2013 for (i = 0; i < ts->
nb_prg; i++)
2026 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
2027 int onid,
val, sid, desc_list_len, desc_tag, desc_len, service_type;
2028 char *
name, *provider_name;
2033 p_end = section + section_len - 4;
2041 onid =
get16(&p, p_end);
2044 val =
get8(&p, p_end);
2048 sid =
get16(&p, p_end);
2051 val =
get8(&p, p_end);
2054 desc_list_len =
get16(&p, p_end);
2055 if (desc_list_len < 0)
2057 desc_list_len &= 0xfff;
2058 desc_list_end = p + desc_list_len;
2059 if (desc_list_end > p_end)
2062 desc_tag =
get8(&p, desc_list_end);
2065 desc_len =
get8(&p, desc_list_end);
2066 desc_end = p + desc_len;
2067 if (desc_len < 0 || desc_end > desc_list_end)
2071 desc_tag, desc_len);
2075 service_type =
get8(&p, p_end);
2076 if (service_type < 0)
2078 provider_name =
getstr8(&p, p_end);
2102 static int parse_pcr(int64_t *ppcr_high,
int *ppcr_low,
2109 int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
2110 has_adaptation, has_payload;
2114 pid =
AV_RB16(packet + 1) & 0x1fff;
2117 is_start = packet[1] & 0x40;
2118 tss = ts->
pids[pid];
2121 tss = ts->
pids[pid];
2127 afc = (packet[3] >> 4) & 3;
2130 has_adaptation = afc & 2;
2131 has_payload = afc & 1;
2132 is_discontinuity = has_adaptation &&
2137 cc = (packet[3] & 0xf);
2138 expected_cc = has_payload ? (tss->
last_cc + 1) & 0x0f : tss->
last_cc;
2139 cc_ok = pid == 0x1FFF ||
2147 "Continuity check failed for pid %d expected %d got %d\n",
2148 pid, expected_cc, cc);
2156 if (has_adaptation) {
2159 if (
parse_pcr(&pcr_h, &pcr_l, packet) == 0)
2160 tss->
last_pcr = pcr_h * 300 + pcr_l;
2166 if (p >= p_end || !has_payload)
2179 if (p + len > p_end)
2205 for (i = 0; i < ts->
nb_prg; i++) {
2287 "max resync size reached, could not find sync byte\n");
2304 if ((*data)[0] != 0x47) {
2359 if (nb_packets != 0 && packet_num >= nb_packets ||
2386 #define CHECK_COUNT 10
2387 #define CHECK_BLOCK 100
2397 score =
FFMAX3(score, dvhs_score, fec_score);
2399 maxscore =
FFMAX(maxscore, score);
2405 av_dlog(0,
"TS score: %d %d\n", sumscore, maxscore);
2421 afc = (packet[3] >> 4) & 3;
2431 if (!(flags & 0x10))
2436 *ppcr_high = ((int64_t) v << 1) | (p[4] >> 7);
2437 *ppcr_low = ((p[4] & 1) << 8) | p[5];
2471 if (s->
iformat == &ff_mpegts_demuxer) {
2491 int pcr_pid, pid, nb_packets, nb_pcrs,
ret, pcr_l;
2492 int64_t pcrs[2], pcr_h;
2493 int packet_count[2];
2514 pid =
AV_RB16(data + 1) & 0x1fff;
2515 if ((pcr_pid == -1 || pcr_pid == pid) &&
2519 packet_count[nb_pcrs] = nb_packets;
2520 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
2532 ts->
pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
2545 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
2551 int64_t pcr_h, next_pcr_h, pos;
2552 int pcr_l, next_pcr_l;
2564 if (data != pkt->
data)
2575 if (
parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
2578 ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
2585 ts->
cur_pcr = pcr_h * 300 + pcr_l;
2618 if (!ret && pkt->
size < 0)
2642 int64_t *ppos, int64_t pos_limit)
2645 int64_t pos, timestamp;
2647 int pcr_l, pcr_pid =
2653 while(pos < pos_limit) {
2658 if (buf[0] != 0x47) {
2665 if ((pcr_pid < 0 || (
AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
2666 parse_pcr(×tamp, &pcr_l, buf) == 0) {
2677 int64_t *ppos, int64_t pos_limit)
2686 while(pos < pos_limit) {
2741 if (buf[0] != 0x47) {
2771 .priv_class = &mpegts_class,
2775 .
name =
"mpegtsraw",
2783 .priv_class = &mpegtsraw_class,