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
38
39 /* maximum size in which we look for synchronisation if
40 synchronisation is lost */
41 #define MAX_RESYNC_SIZE 65536
42
43 #define MAX_PES_PAYLOAD 200*1024
44
45 #define MAX_MP4_DESCR_COUNT 16
46
50 };
51
53
55
60
62
64
74
78 int last_cc;
/* last cc code (-1 if first packet) */
80 union {
84 };
85
86 #define MAX_PIDS_PER_PROGRAM 64
88 unsigned int id;
//program id/service id
91 };
92
95 /* user data */
97 /** raw packet size, including FEC if present */
99
101
102 /** if true, all pids are analyzed to find streams */
104
105 /** compute exact PCR for each transport stream packet */
107
108 int64_t
cur_pcr;
/**< used to estimate the exact PCR */
109 int pcr_incr;
/**< used to estimate the exact PCR */
110
111 /* data needed to handle file based ts */
112 /** stop parsing loop */
114 /** packet containing Audio/Video data */
116 /** to detect seek */
118
119 /******************************************/
120 /* private mpegts data */
121 /* scan context */
122 /** structure to keep track of Program->pids mapping */
125
127
128 /** filters for various streams specified by PMT + for the PAT and PMT */
131 };
132
137 };
138
144 };
145
146 /* TS stream handling */
147
154 };
155
156 /* enough for PES header + length */
157 #define PES_START_SIZE 6
158 #define PES_HEADER_SIZE 9
159 #define MAX_PES_HEADER_SIZE (9 + 255)
160
163 int pcr_pid;
/**< if -1 then all packets containing PCR are considered */
170 /* used to get the format */
172 int flags;
/**< copied to the AVPacket flags */
177 int64_t
ts_packet_pos;
/**< position of first TS packet of this PES packet */
182
184
186 {
188 int i;
192 break;
193 }
194 if (!prg)
195 return;
197 }
198
200 {
201 int i;
202
204 for(i=0; i<ts->
nb_prg; i++)
205 if(ts->
prg[i].
id == programid)
207 }
208
210 {
213 }
214
216 {
219 if(!tmp)
220 return;
226 }
227
229 {
230 int i;
232 for(i=0; i<ts->
nb_prg; i++) {
233 if(ts->
prg[i].
id == programid) {
235 break;
236 }
237 }
238 if(!p)
239 return;
240
242 return;
244 }
245
247 {
248 int i;
252 break;
253 }
254 }
255 }
256
257 /**
258 * @brief discard_pid() decides if the pid is to be discarded according
259 * to caller's programs selection
260 * @param ts : - TS context
261 * @param pid : - pid
262 * @return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
263 * 0 otherwise
264 */
266 {
267 int i, j, k;
268 int used = 0, discarded = 0;
270 for(i=0; i<ts->
nb_prg; i++) {
273 if(p->
pids[j] != pid)
274 continue;
275 //is program with id p->id set to be discarded?
279 discarded++;
280 else
281 used++;
282 }
283 }
284 }
285 }
286
287 return !used && discarded;
288 }
289
290 /**
291 * Assemble PES packets out of TS packets, and then call the "section_cb"
292 * function when they are complete.
293 */
295 const uint8_t *buf,
int buf_size,
int is_start)
296 {
300
301 if (is_start) {
306 } else {
308 return;
310 if (buf_size < len)
311 len = buf_size;
314 }
315
316 /* compute section length if possible */
319 if (len > 4096)
320 return;
322 }
323
325 int crc_valid = 1;
327
330 if (crc_valid){
334 }else
335 crc_valid = 2;
336 }
337 if (crc_valid)
339 }
340 }
341
344 int check_crc)
345
346 {
349
351
355 if (!filter)
370 }
372 }
373
376 void *opaque)
377 {
380
384 if (!filter)
395 }
396
398 {
399 int pid;
400
407 /* referenced private data will be freed later in
408 * avformat_close_input */
411 }
412 }
413
416 }
417
420 int i;
421 int x=0;
422 int best_score=0;
423
424 memset(stat, 0, packet_size*sizeof(int));
425
426 for(x=i=0; i<size-3; i++){
427 if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && buf[i+3] != 0x47){
428 stat[x]++;
429 if(stat[x] > best_score){
430 best_score= stat[x];
431 if(index) *index= x;
432 }
433 }
434
435 x++;
436 if(x == packet_size) x= 0;
437 }
438
439 return best_score;
440 }
441
442 /* autodetect fec presence. Must have at least 1024 bytes */
444 {
445 int score, fec_score, dvhs_score;
446
448 return -1;
449
453 av_dlog(
NULL,
"score: %d, dvhs_score: %d, fec_score: %d \n",
454 score, dvhs_score, fec_score);
455
456 if (score > fec_score && score > dvhs_score)
return TS_PACKET_SIZE;
459 else return -1;
460 }
461
469
471 {
474
475 p = *pp;
476 if (p >= p_end)
477 return -1;
478 c = *p++;
479 *pp = p;
481 }
482
484 {
487
488 p = *pp;
489 if ((p + 1) >= p_end)
490 return -1;
492 p += 2;
493 *pp = p;
495 }
496
497 /* read and allocate a DVB string preceded by its length */
499 {
502 char *str;
503
504 p = *pp;
505 len =
get8(&p, p_end);
506 if (len < 0)
508 if ((p + len) > p_end)
511 if (!str)
513 memcpy(str, p, len);
516 *pp = p;
517 return str;
518 }
519
522 {
523 int val;
524
525 val =
get8(pp, p_end);
526 if (val < 0)
527 return -1;
529 *pp += 2;
530 val =
get16(pp, p_end);
531 if (val < 0)
532 return -1;
534 val =
get8(pp, p_end);
535 if (val < 0)
536 return -1;
537 h->
version = (val >> 1) & 0x1f;
538 val =
get8(pp, p_end);
539 if (val < 0)
540 return -1;
542 val =
get8(pp, p_end);
543 if (val < 0)
544 return -1;
546 return 0;
547 }
548
554
562 /* Makito encoder sets stream type 0x11 for AAC,
563 * so auto-detect LOAS/LATM instead of hardcoding it. */
564 // { 0x11, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC_LATM }, /* LATM syntax */
568 { 0 },
569 };
570
582 { 0 },
583 };
584
585 /* ATSC ? */
589 { 0 },
590 };
591
601 { 0 },
602 };
603
604 /* descriptor present */
611 { 0 },
612 };
613
615 uint32_t stream_type,
const StreamType *types)
616 {
619 return;
620 }
621
627 return;
628 }
629 }
630 }
631
633 uint32_t stream_type, uint32_t prog_reg_desc)
634 {
637
640 return 0;
641 }
642
650
652 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
654
656
658 if ((prog_reg_desc ==
AV_RL32(
"HDMV") ||
659 prog_reg_desc ==
AV_RL32(
"HDPR")) &&
663 // HDMV TrueHD streams also contain an AC3 coded version of the
664 // audio track - add a second stream for this
666 // priv_data cannot be shared between streams
668 if (!sub_pes)
670 memcpy(sub_pes, pes, sizeof(*sub_pes));
671
673 if (!sub_st) {
676 }
677
678 sub_st->
id = pes->
pid;
685 }
686 }
692 }
693
694 return 0;
695 }
696
698 {
700
704
709 }
711
712 // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
715 else
719 /* store position of first TS packet of this PES packet */
722
723 /* reset pts values */
729 }
730
732 {
736 }
737
739 {
741 int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
742 int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
743 int dts_flag = -1, cts_flag = -1;
745
747
753 au_start_flag = au_end_flag = 1;
760 if (padding_flag)
762
763 if (!idle_flag && (!padding_flag || padding_bits != 0)) {
769 if (ocr_flag)
771 if (au_start_flag) {
779 }
780 }
783 if (dts_flag == 1)
785 if (cts_flag == 1)
789 if (inst_bitrate_flag)
791 }
792
797
800
802 }
803
804 /* return non zero if a packet could be constructed */
806 const uint8_t *buf,
int buf_size,
int is_start,
807 int64_t pos)
808 {
813
815 return 0;
816
817 if (is_start) {
821 }
825 }
826 p = buf;
827 while (buf_size > 0) {
831 if (len > buf_size)
832 len = buf_size;
838 /* we got all the PES or section header. We can now
839 decide */
842 /* it must be an mpeg2 PES stream */
843 code = pes->
header[3] | 0x100;
845
848 code == 0x1be) /* padding_stream */
849 goto skip;
850
851 /* stream not present in PMT */
858 }
859
861 /* NOTE: a zero total size means the PES size is
862 unbounded */
865
866 /* allocate pes buffer */
870
871 if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
872 code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
873 code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
874 code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
880 }
881 } else {
884 }
885 } else {
886 /* otherwise, it should be a table */
887 /* skip packet */
888 skip:
890 continue;
891 }
892 }
893 break;
894 /**********************************************/
895 /* PES packing parsing */
898 if (len < 0)
899 return -1;
900 if (len > buf_size)
901 len = buf_size;
909 }
910 break;
913 if (len < 0)
914 return -1;
915 if (len > buf_size)
916 len = buf_size;
923 unsigned int flags, pes_ext, skip;
924
929 if ((flags & 0xc0) == 0x80) {
931 r += 5;
932 } else if ((flags & 0xc0) == 0xc0) {
934 r += 5;
936 r += 5;
937 }
939 if (flags & 0x01) { /* PES extension */
940 pes_ext = *r++;
941 /* Skip PES private data, program packet sequence counter and P-STD buffer */
942 skip = (pes_ext >> 4) & 0xb;
943 skip += skip & 0x9;
944 r += skip;
945 if ((pes_ext & 0x41) == 0x01 &&
947 /* PES extension 2 */
948 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
950 }
951 }
952
953 /* we got the full header. We parse it and get the payload */
959 p += sl_header_bytes;
960 buf_size -= sl_header_bytes;
961 }
962 }
963 break;
965 if (buf_size > 0 && pes->
buffer) {
974 // pes packet size is < ts size packet and pes data is padded with 0xff
975 // not sure if this is legal in ts but see issue #2392
977 }
980 }
981 buf_size = 0;
982 /* emit complete packets with known packet size
983 * decreases demuxer delay for infrequent packets like subtitles from
984 * a couple of seconds to milliseconds for properly muxed files.
985 * total_size is the number of bytes following pes_packet_length
986 * in the pes header, i.e. not counting the first PES_START_SIZE bytes */
991 }
992 break;
994 buf_size = 0;
995 break;
996 }
997 }
998
999 return 0;
1000 }
1001
1003 {
1006
1007 /* if no pid found, then add a pid context */
1009 if (!pes)
1010 return 0;
1019 if (!tss) {
1021 return 0;
1022 }
1023 return pes;
1024 }
1025
1036
1040 {
1041 int ret;
1042 if (size > (1<<30))
1044
1047 return ret;
1048
1055
1056 return 0;
1057 }
1058
1061 (*len) -= new_off - *
off;
1062 *off = new_off;
1063 }
1064
1066 int target_tag);
1067
1069 {
1070 while (len > 0) {
1072 return -1;
1074 }
1075 return 0;
1076 }
1077
1079 {
1088 }
1089
1091 {
1092 int id_flags;
1093 if (len < 2)
1094 return 0;
1096 if (!(id_flags & 0x0020)) { //URL_Flag
1099 } else {
1100 return 0;
1101 }
1102 }
1103
1105 {
1106 int es_id = 0;
1108 return -1;
1111
1116 if (len > 0)
1119 return 0;
1120 }
1121
1123 {
1125 if (!descr)
1126 return -1;
1132 return 0;
1133 }
1134
1136 {
1138 int predefined;
1139 if (!descr)
1140 return -1;
1141
1143 if (!predefined) {
1144 int lengths;
1162 } else {
1164 }
1165 return 0;
1166 }
1167
1169 int target_tag) {
1173 if (len < 0 || len1 > len || len1 <= 0) {
1174 av_log(d->
s,
AV_LOG_ERROR,
"Tag %x length violation new length %d bytes remaining %d\n", tag, len1, len);
1175 return -1;
1176 }
1177
1180 goto done;
1181 }
1182
1183 if (target_tag && tag != target_tag) {
1185 goto done;
1186 }
1187
1188 switch (tag) {
1191 break;
1194 break;
1197 break;
1200 break;
1203 break;
1204 }
1205
1206 done:
1209 return 0;
1210 }
1211
1213 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1214 {
1217 return -1;
1218
1220
1222 return 0;
1223 }
1224
1226 Mp4Descr *descr,
int *descr_count,
int max_descr_count)
1227 {
1230 return -1;
1231
1233
1235 return 0;
1236 }
1237
1239 {
1245 int mp4_descr_count = 0;
1246 int i, pid;
1248
1249 p_end = section + section_len - 4;
1250 p = section;
1252 return;
1254 return;
1255
1257
1260 continue;
1261 for (i = 0; i < mp4_descr_count; i++) {
1265 continue;
1268 continue;
1269 }
1272 if (!st) {
1273 continue;
1274 }
1275
1276 pes->
sl = mp4_descr[i].
sl;
1277
1287
1295 }
1296 }
1297 }
1298 for (i = 0; i < mp4_descr_count; i++)
1299 av_free(mp4_descr[i].dec_config_descr);
1300 }
1301
1304 Mp4Descr *mp4_descr,
int mp4_descr_count,
int pid,
1306 {
1308 int desc_len, desc_tag, desc_es_id;
1309 char language[252];
1310 int i;
1311
1312 desc_tag =
get8(pp, desc_list_end);
1313 if (desc_tag < 0)
1314 return -1;
1315 desc_len =
get8(pp, desc_list_end);
1316 if (desc_len < 0)
1317 return -1;
1318 desc_end = *pp + desc_len;
1319 if (desc_end > desc_list_end)
1320 return -1;
1321
1322 av_dlog(fc,
"tag: 0x%02x len=%d\n", desc_tag, desc_len);
1323
1327
1328 switch(desc_tag) {
1329 case 0x1E: /* SL descriptor */
1330 desc_es_id =
get16(pp, desc_end);
1331 if (ts && ts->
pids[pid])
1333 for (i = 0; i < mp4_descr_count; i++)
1334 if (mp4_descr[i].dec_config_descr_len &&
1335 mp4_descr[i].es_id == desc_es_id) {
1345 }
1346 break;
1347 case 0x1F: /* FMC descriptor */
1348 get16(pp, desc_end);
1359 }
1360 }
1361 break;
1362 case 0x56: /* DVB teletext descriptor */
1363 language[0] =
get8(pp, desc_end);
1364 language[1] =
get8(pp, desc_end);
1365 language[2] =
get8(pp, desc_end);
1366 language[3] = 0;
1368 break;
1369 case 0x59: /* subtitling descriptor */
1370 language[0] =
get8(pp, desc_end);
1371 language[1] =
get8(pp, desc_end);
1372 language[2] =
get8(pp, desc_end);
1373 language[3] = 0;
1374 /* hearing impaired subtitles detection */
1375 switch(
get8(pp, desc_end)) {
1376 case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
1377 case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
1378 case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
1379 case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
1380 case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
1381 case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
1383 break;
1384 }
1388 } else {
1393 }
1394 }
1395 *pp += 4;
1397 break;
1398 case 0x0a: /* ISO 639 language descriptor */
1399 for (i = 0; i + 4 <= desc_len; i += 4) {
1400 language[i + 0] =
get8(pp, desc_end);
1401 language[i + 1] =
get8(pp, desc_end);
1402 language[i + 2] =
get8(pp, desc_end);
1403 language[i + 3] = ',';
1404 switch (
get8(pp, desc_end)) {
1408 }
1409 }
1410 if (i) {
1411 language[i - 1] = 0;
1413 }
1414 break;
1415 case 0x05: /* registration descriptor */
1420 break;
1421 case 0x52: /* stream identifier descriptor */
1423 break;
1424 default:
1425 break;
1426 }
1427 *pp = desc_end;
1428 return 0;
1429 }
1430
1432 {
1437 const uint8_t *p, *p_end, *desc_list_end;
1438 int program_info_length, pcr_pid, pid, stream_type;
1439 int desc_list_len;
1440 uint32_t prog_reg_desc = 0; /* registration descriptor */
1441
1443 int mp4_descr_count = 0;
1444 int i;
1445
1448
1449 p_end = section + section_len - 4;
1450 p = section;
1452 return;
1453
1456
1458 return;
1459
1461 pcr_pid =
get16(&p, p_end);
1462 if (pcr_pid < 0)
1463 return;
1464 pcr_pid &= 0x1fff;
1467
1469
1470 program_info_length =
get16(&p, p_end);
1471 if (program_info_length < 0)
1472 return;
1473 program_info_length &= 0xfff;
1474 while(program_info_length >= 2) {
1476 tag =
get8(&p, p_end);
1477 len =
get8(&p, p_end);
1478
1479 av_dlog(ts->
stream,
"program tag: 0x%02x len=%d\n", tag, len);
1480
1481 if(len > program_info_length - 2)
1482 //something else is broken, exit the program_descriptors_loop
1483 break;
1484 program_info_length -= len + 2;
1485 if (tag == 0x1d) { // IOD descriptor
1486 get8(&p, p_end);
// scope
1487 get8(&p, p_end);
// label
1488 len -= 2;
1491 } else if (tag == 0x05 && len >= 4) { // registration descriptor
1492 prog_reg_desc = bytestream_get_le32(&p);
1493 len -= 4;
1494 }
1496 }
1497 p += program_info_length;
1498 if (p >= p_end)
1499 goto out;
1500
1501 // stop parsing after pmt, we found header
1504
1505 for(;;) {
1506 st = 0;
1508 stream_type =
get8(&p, p_end);
1509 if (stream_type < 0)
1510 break;
1511 pid =
get16(&p, p_end);
1512 if (pid < 0)
1513 break;
1514 pid &= 0x1fff;
1516 break;
1517
1518 /* now create stream */
1524 goto out;
1526 }
1528 } else if (stream_type != 0x13) {
1531 if (pes) {
1533 if (!st)
1534 goto out;
1536 }
1537 } else {
1539 if (idx >= 0) {
1541 } else {
1543 if (!st)
1544 goto out;
1547 }
1548 }
1549
1550 if (!st)
1551 goto out;
1552
1555
1557
1559
1560 desc_list_len =
get16(&p, p_end);
1561 if (desc_list_len < 0)
1562 break;
1563 desc_list_len &= 0xfff;
1564 desc_list_end = p + desc_list_len;
1565 if (desc_list_end > p_end)
1566 break;
1567 for(;;) {
1569 mp4_descr, mp4_descr_count, pid, ts) < 0)
1570 break;
1571
1572 if (pes && prog_reg_desc ==
AV_RL32(
"HDMV") && stream_type == 0x83 && pes->
sub_st) {
1575 }
1576 }
1577 p = desc_list_end;
1578 }
1579
1580 out:
1581 for (i = 0; i < mp4_descr_count; i++)
1582 av_free(mp4_descr[i].dec_config_descr);
1583 }
1584
1586 {
1590 int sid, pmt_pid;
1592
1595
1596 p_end = section + section_len - 4;
1597 p = section;
1599 return;
1601 return;
1602
1604
1606 for(;;) {
1607 sid =
get16(&p, p_end);
1608 if (sid < 0)
1609 break;
1610 pmt_pid =
get16(&p, p_end);
1611 if (pmt_pid < 0)
1612 break;
1613 pmt_pid &= 0x1fff;
1614
1616 break;
1617
1619
1620 if (sid == 0x0000) {
1621 /* NIT info */
1622 } else {
1626 if (ts->
pids[pmt_pid])
1632 }
1633 }
1634
1635 if (sid < 0) {
1636 int i,j;
1638 for (i=0; i<ts->
nb_prg; i++)
1640 break;
1643 }
1644 }
1645 }
1646
1648 {
1651 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
1652 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
1653 char *
name, *provider_name;
1654
1657
1658 p_end = section + section_len - 4;
1659 p = section;
1661 return;
1663 return;
1664 onid =
get16(&p, p_end);
1665 if (onid < 0)
1666 return;
1667 val =
get8(&p, p_end);
1668 if (val < 0)
1669 return;
1670 for(;;) {
1671 sid =
get16(&p, p_end);
1672 if (sid < 0)
1673 break;
1674 val =
get8(&p, p_end);
1675 if (val < 0)
1676 break;
1677 desc_list_len =
get16(&p, p_end);
1678 if (desc_list_len < 0)
1679 break;
1680 desc_list_len &= 0xfff;
1681 desc_list_end = p + desc_list_len;
1682 if (desc_list_end > p_end)
1683 break;
1684 for(;;) {
1685 desc_tag =
get8(&p, desc_list_end);
1686 if (desc_tag < 0)
1687 break;
1688 desc_len =
get8(&p, desc_list_end);
1689 desc_end = p + desc_len;
1690 if (desc_end > desc_list_end)
1691 break;
1692
1694 desc_tag, desc_len);
1695
1696 switch(desc_tag) {
1697 case 0x48:
1698 service_type =
get8(&p, p_end);
1699 if (service_type < 0)
1700 break;
1701 provider_name =
getstr8(&p, p_end);
1702 if (!provider_name)
1703 break;
1705 if (name) {
1707 if(program) {
1710 }
1711 }
1714 break;
1715 default:
1716 break;
1717 }
1718 p = desc_end;
1719 }
1720 p = desc_list_end;
1721 }
1722 }
1723
1724 /* handle one TS packet */
1726 {
1729 int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
1730 has_adaptation, has_payload;
1732 int64_t pos;
1733
1734 pid =
AV_RB16(packet + 1) & 0x1fff;
1736 return 0;
1737 is_start = packet[1] & 0x40;
1738 tss = ts->
pids[pid];
1741 tss = ts->
pids[pid];
1742 }
1743 if (!tss)
1744 return 0;
1746
1747 afc = (packet[3] >> 4) & 3;
1748 if (afc == 0) /* reserved value */
1749 return 0;
1750 has_adaptation = afc & 2;
1751 has_payload = afc & 1;
1752 is_discontinuity = has_adaptation
1753 && packet[4] != 0 /* with length > 0 */
1754 && (packet[5] & 0x80); /* and discontinuity indicated */
1755
1756 /* continuity check (currently not used) */
1757 cc = (packet[3] & 0xf);
1758 expected_cc = has_payload ? (tss->
last_cc + 1) & 0x0f : tss->
last_cc;
1759 cc_ok = pid == 0x1FFF // null packet PID
1760 || is_discontinuity
1762 || expected_cc == cc;
1763
1767 "Continuity check failed for pid %d expected %d got %d\n",
1768 pid, expected_cc, cc);
1772 }
1773 }
1774
1775 if (!has_payload)
1776 return 0;
1777 p = packet + 4;
1778 if (has_adaptation) {
1779 /* skip adaptation field */
1780 p += p[0] + 1;
1781 }
1782 /* if past the end of packet, ignore */
1784 if (p >= p_end)
1785 return 0;
1786
1789
1791 if (is_start) {
1792 /* pointer field present */
1793 len = *p++;
1794 if (p + len > p_end)
1795 return 0;
1796 if (len && cc_ok) {
1797 /* write remaining section bytes */
1799 p, len, 0);
1800 /* check whether filter has been closed */
1802 return 0;
1803 }
1805 if (p < p_end) {
1807 p, p_end - p, 1);
1808 }
1809 } else {
1810 if (cc_ok) {
1812 p, p_end - p, 0);
1813 }
1814 }
1815 } else {
1816 int ret;
1817 // Note: The position here points actually behind the current packet.
1820 return ret;
1821 }
1822
1823 return 0;
1824 }
1825
1826 /* XXX: try to find a better synchro over several packets (use
1827 get_packet_size() ?) */
1829 {
1832
1836 return -1;
1837 if (c == 0x47) {
1839 return 0;
1840 }
1841 }
1843 /* no sync found */
1844 return -1;
1845 }
1846
1847 /* return -1 if error or EOF. Return 0 if OK. */
1849 {
1852
1853 for(;;) {
1857 /* check packet sync byte */
1858 if (buf[0] != 0x47) {
1859 /* find a new packet start */
1863 else
1864 continue;
1865 } else {
1867 if (skip > 0)
1869 break;
1870 }
1871 }
1872 return 0;
1873 }
1874
1876 {
1879 int packet_num, ret = 0;
1880
1882 int i;
1884 /* seek detected, flush pes buffer */
1892 }
1894 }
1895 }
1896 }
1897
1899 packet_num = 0;
1901 for(;;) {
1902 packet_num++;
1903 if (nb_packets != 0 && packet_num >= nb_packets ||
1906 break;
1907 }
1909 break;
1910
1912 if (ret != 0)
1913 break;
1915 if (ret != 0)
1916 break;
1917 }
1919 return ret;
1920 }
1921
1923 {
1925 int maxscore=0;
1926 int sumscore=0;
1927 int i;
1929 #define CHECK_COUNT 10
1930 #define CHECK_BLOCK 100
1931
1933 return -1;
1934
1940 score =
FFMAX3(score, dvhs_score, fec_score);
1941 sumscore += score;
1942 maxscore =
FFMAX(maxscore, score);
1943 }
1944
1947
1948 av_dlog(0,
"TS score: %d %d\n", sumscore, maxscore);
1949
1952 else return -1;
1953 }
1954
1955 /* return the 90kHz PCR and the extension for the 27MHz PCR. return
1956 (-1) if not available */
1959 {
1962 unsigned int v;
1963
1964 afc = (packet[3] >> 4) & 3;
1965 if (afc <= 1)
1966 return -1;
1967 p = packet + 4;
1968 len = p[0];
1969 p++;
1970 if (len == 0)
1971 return -1;
1972 flags = *p++;
1973 len--;
1974 if (!(flags & 0x10))
1975 return -1;
1976 if (len < 6)
1977 return -1;
1979 *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
1980 *ppcr_low = ((p[4] & 1) << 8) | p[5];
1981 return 0;
1982 }
1983
1985 {
1990 int64_t pos;
1991
1992 /* read the first 8192 bytes to get packet size */
1999 }
2002
2003 if (s->
iformat == &ff_mpegts_demuxer) {
2004 /* normal demux */
2005
2006 /* first do a scan to get all the services */
2007 /* NOTE: We attempt to seek on non-seekable files as well, as the
2008 * probe buffer usually is big enough. Only warn if the seek failed
2009 * on files where the seek should work. */
2012
2014
2016
2018 /* if could not find service, enable auto_guess */
2019
2021
2023
2025 } else {
2027 int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
2028 int64_t pcrs[2], pcr_h;
2029 int packet_count[2];
2031
2032 /* only read packets */
2033
2035 if (!st)
2036 goto fail;
2040
2041 /* we iterate until we find two PCRs to estimate the bitrate */
2042 pcr_pid = -1;
2043 nb_pcrs = 0;
2044 nb_packets = 0;
2045 for(;;) {
2047 if (ret < 0)
2048 return -1;
2049 pid =
AV_RB16(packet + 1) & 0x1fff;
2050 if ((pcr_pid == -1 || pcr_pid == pid) &&
2051 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
2052 pcr_pid = pid;
2053 packet_count[nb_pcrs] = nb_packets;
2054 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
2055 nb_pcrs++;
2056 if (nb_pcrs >= 2)
2057 break;
2058 }
2059 nb_packets++;
2060 }
2061
2062 /* NOTE1: the bitrate is computed without the FEC */
2063 /* NOTE2: it is only the bitrate of the start of the stream */
2064 ts->
pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
2071 }
2072
2074 return 0;
2075 fail:
2076 return -1;
2077 }
2078
2079 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
2080
2083 {
2085 int ret, i;
2086 int64_t pcr_h, next_pcr_h, pos;
2087 int pcr_l, next_pcr_l;
2089
2094 if (ret < 0) {
2096 return ret;
2097 }
2099 /* compute exact PCR for each packet */
2101 /* we read the next PCR (XXX: optimize it by using a bigger buffer */
2106 if (
parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
2107 /* XXX: not precise enough */
2108 ts->
pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
2109 (i + 1);
2110 break;
2111 }
2112 }
2114 /* no next PCR found: we use previous increment */
2115 ts->
cur_pcr = pcr_h * 300 + pcr_l;
2116 }
2120 }
2122 return 0;
2123 }
2124
2127 {
2129 int ret, i;
2130
2134 if (ret < 0) {
2136 /* flush pes data left */
2143 ret = 0;
2144 break;
2145 }
2146 }
2147 }
2148 }
2149
2150 if (!ret && pkt->
size < 0)
2152 return ret;
2153 }
2154
2156 {
2158 int i;
2159
2161
2164
2165 return 0;
2166 }
2167
2169 int64_t *ppos, int64_t pos_limit)
2170 {
2172 int64_t pos, timestamp;
2176 while(pos < pos_limit) {
2181 if (buf[0] != 0x47) {
2185 continue;
2186 }
2187 if ((pcr_pid < 0 || (
AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
2188 parse_pcr(×tamp, &pcr_l, buf) == 0) {
2189 *ppos = pos;
2190 return timestamp;
2191 }
2193 }
2194
2196 }
2197
2199 int64_t *ppos, int64_t pos_limit)
2200 {
2202 int64_t pos;
2207 while(pos < pos_limit) {
2208 int ret;
2212 if(ret < 0)
2221 }
2222 }
2224 }
2225
2227 }
2228
2229 /**************************************************************/
2230 /* parsing functions - called from other demuxers such as RTP */
2231
2233 {
2235
2237 if (!ts)
2239 /* no stream case, currently used by RTP */
2245
2246 return ts;
2247 }
2248
2249 /* return the consumed length if a packet was output, or -1 if no
2250 packet is output */
2253 {
2254 int len1;
2255
2258 for(;;) {
2261 return -1;
2262 if (buf[0] != 0x47) {
2263 buf++;
2264 len--;
2265 } else {
2270 break;
2271 }
2272 }
2274 }
2275
2277 {
2278 int i;
2279
2283 }
2284
2295 };
2296
2298 .
name =
"mpegtsraw",
2306 .priv_class = &mpegtsraw_class,
2307 };