1 /*
2 * MPEG2 transport stream (aka DVB) demuxer
3 * Copyright (c) 2002-2003 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
39
40 /* maximum size in which we look for synchronisation if
41 * synchronisation is lost */
42 #define MAX_RESYNC_SIZE 65536
43
44 #define MAX_PES_PAYLOAD 200 * 1024
45
46 #define MAX_MP4_DESCR_COUNT 16
47
48 #define MOD_UNLIKELY(modulus, dividend, divisor, prev_dividend) \
49 do { \
50 if ((prev_dividend) == 0 || (dividend) - (prev_dividend) != (divisor)) \
51 (modulus) = (dividend) % (divisor); \
52 (prev_dividend) = (dividend); \
53 } while (0)
54
59 };
60
62
64 int is_start, int64_t pos);
65
70
72
74
84
88 int last_cc;
/* last cc code (-1 if first packet) */
91 union {
95 };
96
97 #define MAX_PIDS_PER_PROGRAM 64
99 unsigned int id;
// program id/service id
102
103 /** have we found pmt for this program */
105 };
106
109 /* user data */
111 /** raw packet size, including FEC if present */
113
116 #define SIZE_STAT_THRESHOLD 10
117
119
120 /** if true, all pids are analyzed to find streams */
122
123 /** compute exact PCR for each transport stream packet */
125
126 /** fix dvb teletext pts */
128
129 int64_t
cur_pcr;
/**< used to estimate the exact PCR */
130 int pcr_incr;
/**< used to estimate the exact PCR */
131
132 /* data needed to handle file based ts */
133 /** stop parsing loop */
135 /** packet containing Audio/Video data */
137 /** to detect seek */
139
142
143 /******************************************/
144 /* private mpegts data */
145 /* scan context */
146 /** structure to keep track of Program->pids mapping */
149
151 /** filters for various streams specified by PMT + for the PAT and PMT */
154 };
155
157 { "compute_pcr", "Compute exact PCR for each transport stream packet.",
160 { "ts_packetsize", "Output option carrying the raw packet size.",
162 { .i64 = 0 }, 0, 0,
164 { NULL },
165 };
166
172 };
173
180 {.i64 = 0}, 0, 1, 0 },
182 {.i64 = 0}, 0, 1, 0 },
183 { NULL },
184 };
185
191 };
192
193 /* TS stream handling */
194
201 };
202
203 /* enough for PES header + length */
204 #define PES_START_SIZE 6
205 #define PES_HEADER_SIZE 9
206 #define MAX_PES_HEADER_SIZE (9 + 255)
207
210 int pcr_pid;
/**< if -1 then all packets containing PCR are considered */
217 /* used to get the format */
219 int flags;
/**< copied to the AVPacket flags */
224 int64_t
ts_packet_pos;
/**< position of first TS packet of this PES packet */
229
231
233 {
234 int i;
235 for (i = 0; i < ts->
nb_prg; i++) {
236 if (ts->
prg[i].
id == programid) {
238 }
239 }
240 return NULL;
241 }
242
244 {
246 int i;
247
251 break;
252 }
253 if (!prg)
254 return;
256 }
257
259 {
260 int i;
261
263 for (i = 0; i < ts->
nb_prg; i++)
264 if (ts->
prg[i].
id == programid) {
267 }
268 }
269
271 {
274 }
275
277 {
281 return;
282 }
288 }
289
291 unsigned int pid)
292 {
294 if (!p)
295 return;
296
298 return;
300 }
301
303 {
305 if (!p)
306 return;
307
309 }
310
312 {
313 int i;
317 break;
318 }
319 }
320 }
321
322 /**
323 * @brief discard_pid() decides if the pid is to be discarded according
324 * to caller's programs selection
325 * @param ts : - TS context
326 * @param pid : - pid
327 * @return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
328 * 0 otherwise
329 */
331 {
332 int i, j, k;
333 int used = 0, discarded = 0;
335
336 /* If none of the programs have .discard=AVDISCARD_ALL then there's
337 * no way we have to discard this packet */
340 break;
342 return 0;
343
344 for (i = 0; i < ts->
nb_prg; i++) {
346 for (j = 0; j < p->
nb_pids; j++) {
347 if (p->
pids[j] != pid)
348 continue;
349 // is program with id p->id set to be discarded?
353 discarded++;
354 else
355 used++;
356 }
357 }
358 }
359 }
360
361 return !used && discarded;
362 }
363
364 /**
365 * Assemble PES packets out of TS packets, and then call the "section_cb"
366 * function when they are complete.
367 */
369 const uint8_t *
buf,
int buf_size,
int is_start)
370 {
373
374 if (is_start) {
379 } else {
381 return;
383 if (buf_size < len)
384 len = buf_size;
387 }
388
389 /* compute section length if possible */
392 if (len > 4096)
393 return;
395 }
396
399 int crc_valid = 1;
401
404 if (crc_valid) {
408 }else
409 crc_valid = 2;
410 }
411 if (crc_valid)
413 }
414 }
415
418 {
420
422
424 return NULL;
426 if (!filter)
427 return NULL;
429
435
437 }
438
440 unsigned int pid,
442 void *opaque,
443 int check_crc)
444 {
447
449 return NULL;
457 return NULL;
458 }
460 }
461
464 void *opaque)
465 {
468
470 return NULL;
471
476 }
477
479 {
481 }
482
484 {
485 int pid;
486
493 /* referenced private data will be freed later in
494 * avformat_close_input */
497 }
498 }
499
501 ts->
pids[pid] = NULL;
502 }
503
505 {
507 int i;
508 int best_score = 0;
509
510 memset(stat, 0, packet_size * sizeof(*stat));
511
512 for (i = 0; i < size - 3; i++) {
513 if (buf[i] == 0x47 && !(buf[i + 1] & 0x80) && buf[i + 3] != 0x47) {
514 int x = i % packet_size;
515 stat[x]++;
516 if (stat[x] > best_score) {
517 best_score = stat[x];
518 if (index)
519 *index = x;
520 }
521 }
522 }
523
524 return best_score;
525 }
526
527 /* autodetect fec presence. Must have at least 1024 bytes */
529 {
530 int score, fec_score, dvhs_score;
531
534
538 av_dlog(NULL,
"score: %d, dvhs_score: %d, fec_score: %d \n",
539 score, dvhs_score, fec_score);
540
541 if (score > fec_score && score > dvhs_score)
543 else if (dvhs_score > score && dvhs_score > fec_score)
545 else if (score < fec_score && dvhs_score < fec_score)
547 else
549 }
550
558
560 {
563
564 p = *pp;
565 if (p >= p_end)
567 c = *p++;
568 *pp = p;
570 }
571
573 {
576
577 p = *pp;
578 if ((p + 1) >= p_end)
581 p += 2;
582 *pp = p;
584 }
585
586 /* read and allocate a DVB string preceded by its length */
588 {
591 char *str;
592
593 p = *pp;
594 len =
get8(&p, p_end);
595 if (len < 0)
596 return NULL;
597 if ((p + len) > p_end)
598 return NULL;
600 if (!str)
601 return NULL;
602 memcpy(str, p, len);
605 *pp = p;
606 return str;
607 }
608
611 {
613
614 val =
get8(pp, p_end);
615 if (val < 0)
618 *pp += 2;
619 val =
get16(pp, p_end);
620 if (val < 0)
623 val =
get8(pp, p_end);
624 if (val < 0)
626 h->
version = (val >> 1) & 0x1f;
627 val =
get8(pp, p_end);
628 if (val < 0)
631 val =
get8(pp, p_end);
632 if (val < 0)
635 return 0;
636 }
637
643
651 /* Makito encoder sets stream type 0x11 for AAC,
652 * so auto-detect LOAS/LATM instead of hardcoding it. */
653 #if !CONFIG_LOAS_DEMUXER
655 #endif
661 { 0 },
662 };
663
675 { 0 },
676 };
677
678 /* ATSC ? */
682 { 0 },
683 };
684
695 { 0 },
696 };
697
701 { 0 },
702 };
703
704 /* descriptor present */
711 { 0 },
712 };
713
715 uint32_t stream_type,
717 {
720 return;
721 }
722
728 return;
729 }
730 }
731
733 uint32_t stream_type, uint32_t prog_reg_desc)
734 {
737
740 return 0;
741 }
742
750
752 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
754
756
758 if ((prog_reg_desc ==
AV_RL32(
"HDMV") ||
759 prog_reg_desc ==
AV_RL32(
"HDPR")) &&
763 // HDMV TrueHD streams also contain an AC3 coded version of the
764 // audio track - add a second stream for this
766 // priv_data cannot be shared between streams
768 if (!sub_pes)
770 memcpy(sub_pes, pes, sizeof(*sub_pes));
771
773 if (!sub_st) {
776 }
777
778 sub_st->
id = pes->
pid;
785 }
786 }
792 }
793
794 return 0;
795 }
796
798 {
804 }
805
807 {
809
813
819 }
821
822 // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
825 else
829 /* store position of first TS packet of this PES packet */
832
835 }
836
838 {
842 }
843
846 {
848 int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
849 int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
850 int dts_flag = -1, cts_flag = -1;
852
854
860 au_start_flag = au_end_flag = 1;
867 if (padding_flag)
869
870 if (!idle_flag && (!padding_flag || padding_bits != 0)) {
876 if (ocr_flag)
878 if (au_start_flag) {
886 }
887 }
890 if (dts_flag == 1)
892 if (cts_flag == 1)
896 if (inst_bitrate_flag)
898 }
899
904
907
909 }
910
911 /* return non zero if a packet could be constructed */
913 const uint8_t *
buf,
int buf_size,
int is_start,
914 int64_t pos)
915 {
920
922 return 0;
923
924 if (is_start) {
928 } else {
930 }
933 }
935 while (buf_size > 0) {
936 switch (pes->
state) {
939 if (len > buf_size)
940 len = buf_size;
946 /* we got all the PES or section header. We can now
947 * decide */
950 /* it must be an mpeg2 PES stream */
951 code = pes->
header[3] | 0x100;
953 code);
954
958 code == 0x1be) /* padding_stream */
959 goto skip;
960
961 /* stream not present in PMT */
964 goto skip;
965
971 }
972
974 /* NOTE: a zero total size means the PES size is
975 * unbounded */
978
979 /* allocate pes buffer */
984
985 if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
986 code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
987 code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
988 code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
992 "pid=%x stream_type=%x probing\n",
996 }
997 } else {
1000 }
1001 } else {
1002 /* otherwise, it should be a table */
1003 /* skip packet */
1004 skip:
1006 continue;
1007 }
1008 }
1009 break;
1010 /**********************************************/
1011 /* PES packing parsing */
1014 if (len < 0)
1016 if (len > buf_size)
1017 len = buf_size;
1025 }
1026 break;
1029 if (len < 0)
1031 if (len > buf_size)
1032 len = buf_size;
1039 unsigned int flags, pes_ext, skip;
1040
1045 if ((flags & 0xc0) == 0x80) {
1047 r += 5;
1048 } else if ((flags & 0xc0) == 0xc0) {
1050 r += 5;
1052 r += 5;
1053 }
1055 if (flags & 0x01) { /* PES extension */
1056 pes_ext = *r++;
1057 /* Skip PES private data, program packet sequence counter and P-STD buffer */
1058 skip = (pes_ext >> 4) & 0xb;
1059 skip += skip & 0x9;
1060 r += skip;
1061 if ((pes_ext & 0x41) == 0x01 &&
1063 /* PES extension 2 */
1064 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
1066 }
1067 }
1068
1069 /* we got the full header. We parse it and get the payload */
1074 buf_size);
1076 p += sl_header_bytes;
1077 buf_size -= sl_header_bytes;
1078 }
1080 /* skip metadata access unit header */
1082 p += 5;
1083 buf_size -= 5;
1084 }
1090 if (f) {
1094 if (pcrpes)
1097 int i;
1101 st = pst;
1102 }
1103 }
1105 // teletext packets do not always have correct timestamps,
1106 // the standard says they should be handled after 40.6 ms at most,
1107 // and the pcr error to this packet should be no more than 100 ms.
1108 // TODO: we should interpolate the PCR, not just use the last one
1113 pes->
pts = pes->
dts = pcr;
1114 }
else if (pes->
dts > pcr + 3654 + 9000) {
1115 pes->
pts = pes->
dts = pcr + 3654 + 9000;
1116 }
1117 break;
1118 }
1119 }
1120 }
1121 }
1122 }
1123 }
1124 break;
1138 // pes packet size is < ts size packet and pes data is padded with 0xff
1139 // not sure if this is legal in ts but see issue #2392
1141 }
1144 /* emit complete packets with known packet size
1145 * decreases demuxer delay for infrequent packets like subtitles from
1146 * a couple of seconds to milliseconds for properly muxed files.
1147 * total_size is the number of bytes following pes_packet_length
1148 * in the pes header, i.e. not counting the first PES_START_SIZE bytes */
1153 }
1154 }
1155 buf_size = 0;
1156 break;
1158 buf_size = 0;
1159 break;
1160 }
1161 }
1162
1163 return 0;
1164 }
1165
1167 {
1170
1171 /* if no pid found, then add a pid context */
1173 if (!pes)
1174 return 0;
1183 if (!tss) {
1185 return 0;
1186 }
1187 return pes;
1188 }
1189
1201
1204 Mp4Descr *descr,
int max_descr_count)
1205 {
1207 if (size > (1 << 30))
1209
1211 NULL, NULL, NULL, NULL)) < 0)
1213
1220
1221 return 0;
1222 }
1223
1225 {
1227 (*len) -= new_off - *off;
1228 *off = new_off;
1229 }
1230
1232 int target_tag);
1233
1235 {
1236 while (len > 0) {
1238 if (ret < 0)
1241 }
1242 return 0;
1243 }
1244
1246 {
1255 }
1256
1258 {
1259 int id_flags;
1260 if (len < 2)
1261 return 0;
1263 if (!(id_flags & 0x0020)) { // URL_Flag
1266 } else {
1267 return 0;
1268 }
1269 }
1270
1272 {
1273 int es_id = 0;
1278
1283 if (len > 0)
1286 return 0;
1287 }
1288
1291 {
1293 if (!descr)
1300 return 0;
1301 }
1302
1304 {
1306 int predefined;
1307 if (!descr)
1309
1311 if (!predefined) {
1312 int lengths;
1327 }
1338 }
1339 return 0;
1340 }
1341
1343 int target_tag)
1344 {
1348 if (len < 0 || len1 > len || len1 <= 0) {
1350 "Tag %x length violation new length %d bytes remaining %d\n",
1351 tag, len1, len);
1353 }
1354
1357 goto done;
1358 }
1359
1360 if (target_tag && tag != target_tag) {
1362 target_tag);
1363 goto done;
1364 }
1365
1366 switch (tag) {
1369 break;
1372 break;
1375 break;
1378 break;
1381 break;
1382 }
1383
1384
1385 done:
1388 return 0;
1389 }
1390
1392 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1393 {
1396
1398 if (ret < 0)
1400
1402
1405 }
1406
1408 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1409 {
1412
1414 if (ret < 0)
1416
1418
1421 }
1422
1424 int section_len)
1425 {
1430 int mp4_descr_count = 0;
1432 int i, pid;
1434
1435 p_end = section + section_len - 4;
1436 p = section;
1438 return;
1440 return;
1441
1442 mp4_read_od(s, p, (
unsigned) (p_end - p), mp4_descr, &mp4_descr_count,
1444
1447 continue;
1448 for (i = 0; i < mp4_descr_count; i++) {
1452 continue;
1455 continue;
1456 }
1459 if (!st)
1460 continue;
1461
1462 pes->
sl = mp4_descr[i].
sl;
1463
1465 mp4_descr[i].dec_config_descr_len, 0,
1466 NULL, NULL, NULL, NULL);
1474
1476 // do nothing
1483 }
1484 }
1485 for (i = 0; i < mp4_descr_count; i++)
1486 av_free(mp4_descr[i].dec_config_descr);
1487 }
1488
1491 Mp4Descr *mp4_descr,
int mp4_descr_count,
int pid,
1493 {
1495 int desc_len, desc_tag, desc_es_id;
1496 char language[252];
1497 int i;
1498
1499 desc_tag =
get8(pp, desc_list_end);
1500 if (desc_tag < 0)
1502 desc_len =
get8(pp, desc_list_end);
1503 if (desc_len < 0)
1505 desc_end = *pp + desc_len;
1506 if (desc_end > desc_list_end)
1508
1509 av_dlog(fc,
"tag: 0x%02x len=%d\n", desc_tag, desc_len);
1510
1514
1515 switch (desc_tag) {
1516 case 0x1E: /* SL descriptor */
1517 desc_es_id =
get16(pp, desc_end);
1518 if (ts && ts->
pids[pid])
1520 for (i = 0; i < mp4_descr_count; i++)
1521 if (mp4_descr[i].dec_config_descr_len &&
1522 mp4_descr[i].es_id == desc_es_id) {
1525 mp4_descr[i].dec_config_descr_len, 0,
1526 NULL, NULL, NULL, NULL);
1533 }
1534 break;
1535 case 0x1F: /* FMC descriptor */
1536 get16(pp, desc_end);
1537 if (mp4_descr_count > 0 &&
1543 NULL, NULL, NULL, NULL);
1549 }
1550 }
1551 break;
1552 case 0x56: /* DVB teletext descriptor */
1553 {
1555 int language_count = desc_len / 5;
1556
1557 if (desc_len > 0 && desc_len % 5 != 0)
1559
1560 if (language_count > 0) {
1561 /* 4 bytes per language code (3 bytes) with comma or NUL byte should fit language buffer */
1562 if (language_count > sizeof(language) / 4) {
1563 language_count = sizeof(language) / 4;
1564 }
1565
1569 }
1570 }
1571
1574
1576
1577 for (i = 0; i < language_count; i++) {
1578 language[i * 4 + 0] =
get8(pp, desc_end);
1579 language[i * 4 + 1] =
get8(pp, desc_end);
1580 language[i * 4 + 2] =
get8(pp, desc_end);
1581 language[i * 4 + 3] = ',';
1582
1583 memcpy(extradata, *pp, 2);
1584 extradata += 2;
1585
1586 *pp += 2;
1587 }
1588
1589 language[i * 4 - 1] = 0;
1591 }
1592 }
1593 break;
1594 case 0x59: /* subtitling descriptor */
1595 {
1596 /* 8 bytes per DVB subtitle substream data:
1597 * ISO_639_language_code (3 bytes),
1598 * subtitling_type (1 byte),
1599 * composition_page_id (2 bytes),
1600 * ancillary_page_id (2 bytes) */
1601 int language_count = desc_len / 8;
1602
1603 if (desc_len > 0 && desc_len % 8 != 0)
1605
1606 if (language_count > 1) {
1608 }
1609
1610 if (language_count > 0) {
1612
1613 /* 4 bytes per language code (3 bytes) with comma or NUL byte should fit language buffer */
1614 if (language_count > sizeof(language) / 4) {
1615 language_count = sizeof(language) / 4;
1616 }
1617
1621 }
1622 }
1623
1626
1628
1629 for (i = 0; i < language_count; i++) {
1630 language[i * 4 + 0] =
get8(pp, desc_end);
1631 language[i * 4 + 1] =
get8(pp, desc_end);
1632 language[i * 4 + 2] =
get8(pp, desc_end);
1633 language[i * 4 + 3] = ',';
1634
1635 /* hearing impaired subtitles detection using subtitling_type */
1636 switch (*pp[0]) {
1637 case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
1638 case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
1639 case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
1640 case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
1641 case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
1642 case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
1644 break;
1645 }
1646
1647 extradata[4] =
get8(pp, desc_end);
/* subtitling_type */
1648 memcpy(extradata, *pp, 4); /* composition_page_id and ancillary_page_id */
1649 extradata += 5;
1650
1651 *pp += 4;
1652 }
1653
1654 language[i * 4 - 1] = 0;
1656 }
1657 }
1658 break;
1659 case 0x0a: /* ISO 639 language descriptor */
1660 for (i = 0; i + 4 <= desc_len; i += 4) {
1661 language[i + 0] =
get8(pp, desc_end);
1662 language[i + 1] =
get8(pp, desc_end);
1663 language[i + 2] =
get8(pp, desc_end);
1664 language[i + 3] = ',';
1665 switch (
get8(pp, desc_end)) {
1666 case 0x01:
1668 break;
1669 case 0x02:
1671 break;
1672 case 0x03:
1674 break;
1675 }
1676 }
1677 if (i) {
1678 language[i - 1] = 0;
1680 }
1681 break;
1682 case 0x05: /* registration descriptor */
1687 break;
1688 case 0x52: /* stream identifier descriptor */
1690 break;
1691 case 0x26: /* metadata descriptor */
1692 if (
get16(pp, desc_end) == 0xFFFF)
1693 *pp += 4;
1694 if (
get8(pp, desc_end) == 0xFF) {
1698 }
1699 break;
1700 default:
1701 break;
1702 }
1703 *pp = desc_end;
1704 return 0;
1705 }
1706
1708 {
1713 const uint8_t *p, *p_end, *desc_list_end;
1714 int program_info_length, pcr_pid, pid, stream_type;
1715 int desc_list_len;
1716 uint32_t prog_reg_desc = 0; /* registration descriptor */
1717
1718 int mp4_descr_count = 0;
1720 int i;
1721
1724
1725 p_end = section + section_len - 4;
1726 p = section;
1728 return;
1729
1732
1734 return;
1736 return;
1737
1739 pcr_pid =
get16(&p, p_end);
1740 if (pcr_pid < 0)
1741 return;
1742 pcr_pid &= 0x1fff;
1745
1747
1748 program_info_length =
get16(&p, p_end);
1749 if (program_info_length < 0)
1750 return;
1751 program_info_length &= 0xfff;
1752 while (program_info_length >= 2) {
1754 tag =
get8(&p, p_end);
1755 len =
get8(&p, p_end);
1756
1757 av_dlog(ts->
stream,
"program tag: 0x%02x len=%d\n", tag, len);
1758
1759 if (len > program_info_length - 2)
1760 // something else is broken, exit the program_descriptors_loop
1761 break;
1762 program_info_length -= len + 2;
1763 if (tag == 0x1d) { // IOD descriptor
1764 get8(&p, p_end);
// scope
1765 get8(&p, p_end);
// label
1766 len -= 2;
1769 } else if (tag == 0x05 && len >= 4) { // registration descriptor
1770 prog_reg_desc = bytestream_get_le32(&p);
1771 len -= 4;
1772 }
1774 }
1775 p += program_info_length;
1776 if (p >= p_end)
1778
1779 // stop parsing after pmt, we found header
1782
1784
1785
1786 for (;;) {
1787 st = 0;
1788 pes = NULL;
1789 stream_type =
get8(&p, p_end);
1790 if (stream_type < 0)
1791 break;
1792 pid =
get16(&p, p_end);
1793 if (pid < 0)
1795 pid &= 0x1fff;
1798
1799 /* now create stream */
1807 }
1809 } else if (stream_type != 0x13) {
1813 if (pes) {
1815 if (!st)
1818 }
1819 } else {
1821 if (idx >= 0) {
1823 } else {
1825 if (!st)
1829 }
1830 }
1831
1832 if (!st)
1834
1837
1839
1841
1842 desc_list_len =
get16(&p, p_end);
1843 if (desc_list_len < 0)
1845 desc_list_len &= 0xfff;
1846 desc_list_end = p + desc_list_len;
1847 if (desc_list_end > p_end)
1849 for (;;) {
1851 desc_list_end, mp4_descr,
1852 mp4_descr_count, pid, ts) < 0)
1853 break;
1854
1855 if (pes && prog_reg_desc ==
AV_RL32(
"HDMV") &&
1856 stream_type == 0x83 && pes->
sub_st) {
1860 }
1861 }
1862 p = desc_list_end;
1863 }
1864
1865 if (!ts->
pids[pcr_pid])
1867
1869 for (i = 0; i < mp4_descr_count; i++)
1870 av_free(mp4_descr[i].dec_config_descr);
1871 }
1872
1874 {
1878 int sid, pmt_pid;
1880
1883
1884 p_end = section + section_len - 4;
1885 p = section;
1887 return;
1889 return;
1891 return;
1892
1894
1896 for (;;) {
1897 sid =
get16(&p, p_end);
1898 if (sid < 0)
1899 break;
1900 pmt_pid =
get16(&p, p_end);
1901 if (pmt_pid < 0)
1902 break;
1903 pmt_pid &= 0x1fff;
1904
1906 break;
1907
1909
1910 if (sid == 0x0000) {
1911 /* NIT info */
1912 } else {
1917 if (fil)
1919 || fil->
pid != pmt_pid
1922
1923 if (!ts->
pids[pmt_pid])
1928 }
1929 }
1930
1931 if (sid < 0) {
1932 int i,j;
1934 for (i = 0; i < ts->
nb_prg; i++)
1936 break;
1939 }
1940 }
1941 }
1942
1944 {
1947 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
1948 int onid,
val, sid, desc_list_len, desc_tag, desc_len, service_type;
1949 char *
name, *provider_name;
1950
1953
1954 p_end = section + section_len - 4;
1955 p = section;
1957 return;
1959 return;
1961 return;
1962 onid =
get16(&p, p_end);
1963 if (onid < 0)
1964 return;
1965 val =
get8(&p, p_end);
1966 if (val < 0)
1967 return;
1968 for (;;) {
1969 sid =
get16(&p, p_end);
1970 if (sid < 0)
1971 break;
1972 val =
get8(&p, p_end);
1973 if (val < 0)
1974 break;
1975 desc_list_len =
get16(&p, p_end);
1976 if (desc_list_len < 0)
1977 break;
1978 desc_list_len &= 0xfff;
1979 desc_list_end = p + desc_list_len;
1980 if (desc_list_end > p_end)
1981 break;
1982 for (;;) {
1983 desc_tag =
get8(&p, desc_list_end);
1984 if (desc_tag < 0)
1985 break;
1986 desc_len =
get8(&p, desc_list_end);
1987 desc_end = p + desc_len;
1988 if (desc_end > desc_list_end)
1989 break;
1990
1992 desc_tag, desc_len);
1993
1994 switch (desc_tag) {
1995 case 0x48:
1996 service_type =
get8(&p, p_end);
1997 if (service_type < 0)
1998 break;
1999 provider_name =
getstr8(&p, p_end);
2000 if (!provider_name)
2001 break;
2003 if (name) {
2005 if (program) {
2008 provider_name, 0);
2009 }
2010 }
2013 break;
2014 default:
2015 break;
2016 }
2017 p = desc_end;
2018 }
2019 p = desc_list_end;
2020 }
2021 }
2022
2023 static int parse_pcr(int64_t *ppcr_high,
int *ppcr_low,
2025
2026 /* handle one TS packet */
2028 {
2030 int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
2031 has_adaptation, has_payload;
2033 int64_t pos;
2034
2035 pid =
AV_RB16(packet + 1) & 0x1fff;
2037 return 0;
2038 is_start = packet[1] & 0x40;
2039 tss = ts->
pids[pid];
2040 if (ts->
auto_guess && tss == NULL && is_start) {
2042 tss = ts->
pids[pid];
2043 }
2044 if (!tss)
2045 return 0;
2047
2048 afc = (packet[3] >> 4) & 3;
2049 if (afc == 0) /* reserved value */
2050 return 0;
2051 has_adaptation = afc & 2;
2052 has_payload = afc & 1;
2053 is_discontinuity = has_adaptation &&
2054 packet[4] != 0 && /* with length > 0 */
2055 (packet[5] & 0x80); /* and discontinuity indicated */
2056
2057 /* continuity check (currently not used) */
2058 cc = (packet[3] & 0xf);
2059 expected_cc = has_payload ? (tss->
last_cc + 1) & 0x0f : tss->
last_cc;
2060 cc_ok = pid == 0x1FFF || // null packet PID
2061 is_discontinuity ||
2063 expected_cc == cc;
2064
2068 "Continuity check failed for pid %d expected %d got %d\n",
2069 pid, expected_cc, cc);
2073 }
2074 }
2075
2077 return 0;
2078 p = packet + 4;
2079 if (has_adaptation) {
2080 /* skip adaptation field */
2081 p += p[0] + 1;
2082 }
2083 /* if past the end of packet, ignore */
2086 return 0;
2087
2089 if (pos >= 0) {
2092 }
2093
2095 if (is_start) {
2096 /* pointer field present */
2097 len = *p++;
2098 if (p + len > p_end)
2099 return 0;
2100 if (len && cc_ok) {
2101 /* write remaining section bytes */
2103 p, len, 0);
2104 /* check whether filter has been closed */
2106 return 0;
2107 }
2109 if (p < p_end) {
2111 p, p_end - p, 1);
2112 }
2113 } else {
2114 if (cc_ok) {
2116 p, p_end - p, 0);
2117 }
2118 }
2119
2120 // stop find_stream_info from waiting for more streams
2121 // when all programs have received a PMT
2123 int i;
2124 for (i = 0; i < ts->
nb_prg; i++) {
2126 break;
2127 }
2132 }
2133 }
2134 }
2135
2136 } else {
2138 int64_t pcr_h;
2139 int pcr_l;
2140 if (
parse_pcr(&pcr_h, &pcr_l, packet) == 0)
2141 tss->
last_pcr = pcr_h * 300 + pcr_l;
2142 // Note: The position here points actually behind the current packet.
2147 }
2148 }
2149
2150 return 0;
2151 }
2152
2156 if (pos < 0)
2157 return;
2165 }
2166
2169 int newsize = 0;
2176 }
2180 }
2183 }
2184 }
2185
2186 /* XXX: try to find a better synchro over several packets (use
2187 * get_packet_size() ?) */
2189 {
2192
2197 if (c == 0x47) {
2200 return 0;
2201 }
2202 }
2204 "max resync size reached, could not find sync byte\n");
2205 /* no sync found */
2207 }
2208
2209 /* return AVERROR_something if error or EOF. Return 0 if OK. */
2212 {
2215
2216 for (;;) {
2220 /* check packet sync byte */
2221 if ((*data)[0] != 0x47) {
2222 /* find a new packet start */
2225
2228 else
2229 continue;
2230 } else {
2231 break;
2232 }
2233 }
2234 return 0;
2235 }
2236
2238 {
2241 if (skip > 0)
2243 }
2244
2246 {
2250 int packet_num,
ret = 0;
2251
2253 int i;
2255 /* seek detected, flush pes buffer */
2263 }
2266 }
2267 }
2268 }
2269
2271 packet_num = 0;
2273 for (;;) {
2274 packet_num++;
2275 if (nb_packets != 0 && packet_num >= nb_packets ||
2278 break;
2279 }
2281 break;
2282
2284 if (ret != 0)
2285 break;
2288 if (ret != 0)
2289 break;
2290 }
2293 }
2294
2296 {
2298 int maxscore = 0;
2299 int sumscore = 0;
2300 int i;
2302 #define CHECK_COUNT 10
2303 #define CHECK_BLOCK 100
2304
2307
2313 score =
FFMAX3(score, dvhs_score, fec_score);
2314 sumscore += score;
2315 maxscore =
FFMAX(maxscore, score);
2316 }
2317
2320
2321 av_dlog(0,
"TS score: %d %d\n", sumscore, maxscore);
2322
2325 else
2327 }
2328
2329 /* return the 90kHz PCR and the extension for the 27MHz PCR. return
2330 * (-1) if not available */
2332 {
2336
2337 afc = (packet[3] >> 4) & 3;
2338 if (afc <= 1)
2340 p = packet + 4;
2341 len = p[0];
2342 p++;
2343 if (len == 0)
2345 flags = *p++;
2346 len--;
2347 if (!(flags & 0x10))
2349 if (len < 6)
2352 *ppcr_high = ((int64_t) v << 1) | (p[4] >> 7);
2353 *ppcr_low = ((p[4] & 1) << 8) | p[5];
2354 return 0;
2355 }
2356
2358
2359 /* NOTE: We attempt to seek on non-seekable files as well, as the
2360 * probe buffer usually is big enough. Only warn if the seek failed
2361 * on files where the seek should work. */
2364 }
2365
2367 {
2372 int64_t pos;
2373
2375
2376 /* read the first 8192 bytes to get packet size */
2383 }
2386
2387 if (s->
iformat == &ff_mpegts_demuxer) {
2388 /* normal demux */
2389
2390 /* first do a scan to get all the services */
2392
2394
2396
2398 /* if could not find service, enable auto_guess */
2399
2401
2403
2405 } else {
2407 int pcr_pid, pid, nb_packets, nb_pcrs,
ret, pcr_l;
2408 int64_t pcrs[2], pcr_h;
2409 int packet_count[2];
2412
2413 /* only read packets */
2414
2416 if (!st)
2421
2422 /* we iterate until we find two PCRs to estimate the bitrate */
2423 pcr_pid = -1;
2424 nb_pcrs = 0;
2425 nb_packets = 0;
2426 for (;;) {
2428 if (ret < 0)
2430 pid =
AV_RB16(data + 1) & 0x1fff;
2431 if ((pcr_pid == -1 || pcr_pid == pid) &&
2434 pcr_pid = pid;
2435 packet_count[nb_pcrs] = nb_packets;
2436 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
2437 nb_pcrs++;
2438 if (nb_pcrs >= 2)
2439 break;
2440 } else {
2442 }
2443 nb_packets++;
2444 }
2445
2446 /* NOTE1: the bitrate is computed without the FEC */
2447 /* NOTE2: it is only the bitrate of the start of the stream */
2448 ts->
pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
2455 }
2456
2458 return 0;
2459 }
2460
2461 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
2462
2464 {
2467 int64_t pcr_h, next_pcr_h, pos;
2468 int pcr_l, next_pcr_l;
2471
2476 if (ret < 0) {
2479 }
2480 if (data != pkt->
data)
2484 /* compute exact PCR for each packet */
2486 /* we read the next PCR (XXX: optimize it by using a bigger buffer */
2491 if (
parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
2492 /* XXX: not precise enough */
2494 ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
2495 (i + 1);
2496 break;
2497 }
2498 }
2500 /* no next PCR found: we use previous increment */
2501 ts->
cur_pcr = pcr_h * 300 + pcr_l;
2502 }
2506 }
2508 return 0;
2509 }
2510
2512 {
2515
2519 if (ret < 0) {
2521 /* flush pes data left */
2528 ret = 0;
2529 break;
2530 }
2531 }
2532 }
2533
2534 if (!ret && pkt->
size < 0)
2537 }
2538
2540 {
2541 int i;
2542
2544
2548 }
2549
2551 {
2554 return 0;
2555 }
2556
2558 int64_t *ppos, int64_t pos_limit)
2559 {
2561 int64_t pos, timestamp;
2563 int pcr_l, pcr_pid =
2566 pos =
2569 while(pos < pos_limit) {
2574 if (buf[0] != 0x47) {
2579 continue;
2580 }
2581 if ((pcr_pid < 0 || (
AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
2582 parse_pcr(×tamp, &pcr_l, buf) == 0) {
2583 *ppos = pos;
2584 return timestamp;
2585 }
2587 }
2588
2590 }
2591
2593 int64_t *ppos, int64_t pos_limit)
2594 {
2596 int64_t pos;
2602 while(pos < pos_limit) {
2607 if (ret < 0)
2616 }
2617 }
2619 }
2620
2622 }
2623
2624 /**************************************************************/
2625 /* parsing functions - called from other demuxers such as RTP */
2626
2628 {
2630
2632 if (!ts)
2633 return NULL;
2634 /* no stream case, currently used by RTP */
2640
2641 return ts;
2642 }
2643
2644 /* return the consumed length if a packet was output, or -1 if no
2645 * packet is output */
2648 {
2649 int len1;
2650
2653 for (;;) {
2657 if (buf[0] != 0x47) {
2658 buf++;
2659 len--;
2660 } else {
2665 break;
2666 }
2667 }
2669 }
2670
2672 {
2675 }
2676
2687 .priv_class = &mpegts_class,
2688 };
2689
2691 .
name =
"mpegtsraw",
2699 .priv_class = &mpegtsraw_class,
2700 };