2 * MPEG2 transport stream (aka DVB) demuxer
3 * Copyright (c) 2002-2003 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
24 //#define USE_SYNCPOINT_SEARCH
26 #include "libavutil/crc.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavcodec/bytestream.h"
34 /* 1.0 second at 24Mbit/s */
35 #define MAX_SCAN_PACKETS 32000
37 /* maximum size in which we look for synchronisation if
38 synchronisation is lost */
39 #define MAX_RESYNC_SIZE 65536
41 #define MAX_PES_PAYLOAD 200*1024
43 typedef struct PESContext PESContext;
45 enum MpegTSFilterType {
50 typedef struct MpegTSFilter MpegTSFilter;
52 typedef int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos);
54 typedef struct MpegTSPESFilter {
59 typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
61 typedef void SetServiceCallback(void *opaque, int ret);
63 typedef struct MpegTSSectionFilter {
67 unsigned int check_crc:1;
68 unsigned int end_of_section_reached:1;
69 SectionCallback *section_cb;
71 } MpegTSSectionFilter;
75 int last_cc; /* last cc code (-1 if first packet) */
76 enum MpegTSFilterType type;
78 MpegTSPESFilter pes_filter;
79 MpegTSSectionFilter section_filter;
83 #define MAX_PIDS_PER_PROGRAM 64
85 unsigned int id; //program id/service id
87 unsigned int pids[MAX_PIDS_PER_PROGRAM];
90 struct MpegTSContext {
92 AVFormatContext *stream;
93 /** raw packet size, including FEC if present */
98 /** if true, all pids are analyzed to find streams */
101 /** compute exact PCR for each transport stream packet */
102 int mpeg2ts_compute_pcr;
104 int64_t cur_pcr; /**< used to estimate the exact PCR */
105 int pcr_incr; /**< used to estimate the exact PCR */
107 /* data needed to handle file based ts */
108 /** stop parsing loop */
110 /** packet containing Audio/Video data */
112 /** to detect seek */
115 /******************************************/
116 /* private mpegts data */
118 /** structure to keep track of Program->pids mapping */
123 /** filters for various streams specified by PMT + for the PAT and PMT */
124 MpegTSFilter *pids[NB_PID_MAX];
127 /* TS stream handling */
132 MPEGTS_PESHEADER_FILL,
137 /* enough for PES header + length */
138 #define PES_START_SIZE 6
139 #define PES_HEADER_SIZE 9
140 #define MAX_PES_HEADER_SIZE (9 + 255)
144 int pcr_pid; /**< if -1 then all packets containing PCR are considered */
147 AVFormatContext *stream;
149 AVStream *sub_st; /**< stream for the embedded AC3 stream in HDMV TrueHD */
150 enum MpegTSState state;
151 /* used to get the format */
155 int extended_stream_id;
157 int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
158 uint8_t header[MAX_PES_HEADER_SIZE];
162 extern AVInputFormat mpegts_demuxer;
164 static void clear_program(MpegTSContext *ts, unsigned int programid)
168 for(i=0; i<ts->nb_prg; i++)
169 if(ts->prg[i].id == programid)
170 ts->prg[i].nb_pids = 0;
173 static void clear_programs(MpegTSContext *ts)
179 static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
182 void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(struct Program));
186 p = &ts->prg[ts->nb_prg];
192 static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
195 struct Program *p = NULL;
196 for(i=0; i<ts->nb_prg; i++) {
197 if(ts->prg[i].id == programid) {
205 if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
207 p->pids[p->nb_pids++] = pid;
211 * \brief discard_pid() decides if the pid is to be discarded according
212 * to caller's programs selection
213 * \param ts : - TS context
215 * \return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
218 static int discard_pid(MpegTSContext *ts, unsigned int pid)
221 int used = 0, discarded = 0;
223 for(i=0; i<ts->nb_prg; i++) {
225 for(j=0; j<p->nb_pids; j++) {
226 if(p->pids[j] != pid)
228 //is program with id p->id set to be discarded?
229 for(k=0; k<ts->stream->nb_programs; k++) {
230 if(ts->stream->programs[k]->id == p->id) {
231 if(ts->stream->programs[k]->discard == AVDISCARD_ALL)
240 return !used && discarded;
244 * Assembles PES packets out of TS packets, and then calls the "section_cb"
245 * function when they are complete.
247 static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
248 const uint8_t *buf, int buf_size, int is_start)
250 MpegTSSectionFilter *tss = &tss1->u.section_filter;
254 memcpy(tss->section_buf, buf, buf_size);
255 tss->section_index = buf_size;
256 tss->section_h_size = -1;
257 tss->end_of_section_reached = 0;
259 if (tss->end_of_section_reached)
261 len = 4096 - tss->section_index;
264 memcpy(tss->section_buf + tss->section_index, buf, len);
265 tss->section_index += len;
268 /* compute section length if possible */
269 if (tss->section_h_size == -1 && tss->section_index >= 3) {
270 len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
273 tss->section_h_size = len;
276 if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
277 tss->end_of_section_reached = 1;
278 if (!tss->check_crc ||
279 av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
280 tss->section_buf, tss->section_h_size) == 0)
281 tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
285 static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
286 SectionCallback *section_cb, void *opaque,
290 MpegTSFilter *filter;
291 MpegTSSectionFilter *sec;
293 dprintf(ts->stream, "Filter: pid=0x%x\n", pid);
295 if (pid >= NB_PID_MAX || ts->pids[pid])
297 filter = av_mallocz(sizeof(MpegTSFilter));
300 ts->pids[pid] = filter;
301 filter->type = MPEGTS_SECTION;
303 filter->last_cc = -1;
304 sec = &filter->u.section_filter;
305 sec->section_cb = section_cb;
306 sec->opaque = opaque;
307 sec->section_buf = av_malloc(MAX_SECTION_SIZE);
308 sec->check_crc = check_crc;
309 if (!sec->section_buf) {
316 static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
320 MpegTSFilter *filter;
321 MpegTSPESFilter *pes;
323 if (pid >= NB_PID_MAX || ts->pids[pid])
325 filter = av_mallocz(sizeof(MpegTSFilter));
328 ts->pids[pid] = filter;
329 filter->type = MPEGTS_PES;
331 filter->last_cc = -1;
332 pes = &filter->u.pes_filter;
333 pes->pes_cb = pes_cb;
334 pes->opaque = opaque;
338 static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
343 if (filter->type == MPEGTS_SECTION)
344 av_freep(&filter->u.section_filter.section_buf);
345 else if (filter->type == MPEGTS_PES) {
346 PESContext *pes = filter->u.pes_filter.opaque;
347 av_freep(&pes->buffer);
348 /* referenced private data will be freed later in
349 * av_close_input_stream */
350 if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
351 av_freep(&filter->u.pes_filter.opaque);
356 ts->pids[pid] = NULL;
359 static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
360 int stat[TS_MAX_PACKET_SIZE];
365 memset(stat, 0, packet_size*sizeof(int));
367 for(x=i=0; i<size-3; i++){
368 if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && (buf[i+3] & 0x30)){
370 if(stat[x] > best_score){
377 if(x == packet_size) x= 0;
383 /* autodetect fec presence. Must have at least 1024 bytes */
384 static int get_packet_size(const uint8_t *buf, int size)
386 int score, fec_score, dvhs_score;
388 if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
391 score = analyze(buf, size, TS_PACKET_SIZE, NULL);
392 dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
393 fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
394 // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
396 if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
397 else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
398 else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
402 typedef struct SectionHeader {
407 uint8_t last_sec_num;
410 static inline int get8(const uint8_t **pp, const uint8_t *p_end)
423 static inline int get16(const uint8_t **pp, const uint8_t *p_end)
429 if ((p + 1) >= p_end)
437 /* read and allocate a DVB string preceeded by its length */
438 static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
445 len = get8(&p, p_end);
448 if ((p + len) > p_end)
450 str = av_malloc(len + 1);
460 static int parse_section_header(SectionHeader *h,
461 const uint8_t **pp, const uint8_t *p_end)
465 val = get8(pp, p_end);
470 val = get16(pp, p_end);
474 val = get8(pp, p_end);
477 h->version = (val >> 1) & 0x1f;
478 val = get8(pp, p_end);
482 val = get8(pp, p_end);
485 h->last_sec_num = val;
490 uint32_t stream_type;
491 enum CodecType codec_type;
492 enum CodecID codec_id;
495 static const StreamType ISO_types[] = {
496 { 0x01, CODEC_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
497 { 0x02, CODEC_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
498 { 0x03, CODEC_TYPE_AUDIO, CODEC_ID_MP3 },
499 { 0x04, CODEC_TYPE_AUDIO, CODEC_ID_MP3 },
500 { 0x0f, CODEC_TYPE_AUDIO, CODEC_ID_AAC },
501 { 0x10, CODEC_TYPE_VIDEO, CODEC_ID_MPEG4 },
502 { 0x11, CODEC_TYPE_AUDIO, CODEC_ID_AAC }, /* LATM syntax */
503 { 0x1b, CODEC_TYPE_VIDEO, CODEC_ID_H264 },
504 { 0xd1, CODEC_TYPE_VIDEO, CODEC_ID_DIRAC },
505 { 0xea, CODEC_TYPE_VIDEO, CODEC_ID_VC1 },
509 static const StreamType HDMV_types[] = {
510 { 0x80, CODEC_TYPE_AUDIO, CODEC_ID_PCM_BLURAY },
511 { 0x81, CODEC_TYPE_AUDIO, CODEC_ID_AC3 },
512 { 0x82, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
513 { 0x83, CODEC_TYPE_AUDIO, CODEC_ID_TRUEHD },
514 { 0x84, CODEC_TYPE_AUDIO, CODEC_ID_EAC3 },
515 { 0x90, CODEC_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
520 static const StreamType MISC_types[] = {
521 { 0x81, CODEC_TYPE_AUDIO, CODEC_ID_AC3 },
522 { 0x8a, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
526 static const StreamType REGD_types[] = {
527 { MKTAG('d','r','a','c'), CODEC_TYPE_VIDEO, CODEC_ID_DIRAC },
528 { MKTAG('A','C','-','3'), CODEC_TYPE_AUDIO, CODEC_ID_AC3 },
532 /* descriptor present */
533 static const StreamType DESC_types[] = {
534 { 0x6a, CODEC_TYPE_AUDIO, CODEC_ID_AC3 }, /* AC-3 descriptor */
535 { 0x7a, CODEC_TYPE_AUDIO, CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
536 { 0x7b, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
537 { 0x56, CODEC_TYPE_SUBTITLE, CODEC_ID_DVB_TELETEXT },
538 { 0x59, CODEC_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
542 static void mpegts_find_stream_type(AVStream *st,
543 uint32_t stream_type, const StreamType *types)
545 for (; types->stream_type; types++) {
546 if (stream_type == types->stream_type) {
547 st->codec->codec_type = types->codec_type;
548 st->codec->codec_id = types->codec_id;
554 static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
555 uint32_t stream_type, uint32_t prog_reg_desc)
557 av_set_pts_info(st, 33, 1, 90000);
559 st->codec->codec_type = CODEC_TYPE_DATA;
560 st->codec->codec_id = CODEC_ID_NONE;
561 st->need_parsing = AVSTREAM_PARSE_FULL;
563 pes->stream_type = stream_type;
565 av_log(pes->stream, AV_LOG_DEBUG,
566 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
567 st->index, pes->stream_type, pes->pid, (char*)&prog_reg_desc);
569 st->codec->codec_tag = pes->stream_type;
571 mpegts_find_stream_type(st, pes->stream_type, ISO_types);
572 if (prog_reg_desc == AV_RL32("HDMV") &&
573 st->codec->codec_id == CODEC_ID_NONE) {
574 mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
575 if (pes->stream_type == 0x83) {
576 // HDMV TrueHD streams also contain an AC3 coded version of the
577 // audio track - add a second stream for this
579 // priv_data cannot be shared between streams
580 PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
582 return AVERROR(ENOMEM);
583 memcpy(sub_pes, pes, sizeof(*sub_pes));
585 sub_st = av_new_stream(pes->stream, pes->pid);
588 return AVERROR(ENOMEM);
591 av_set_pts_info(sub_st, 33, 1, 90000);
592 sub_st->priv_data = sub_pes;
593 sub_st->codec->codec_type = CODEC_TYPE_AUDIO;
594 sub_st->codec->codec_id = CODEC_ID_AC3;
595 sub_st->need_parsing = AVSTREAM_PARSE_FULL;
596 sub_pes->sub_st = pes->sub_st = sub_st;
599 if (st->codec->codec_id == CODEC_ID_NONE)
600 mpegts_find_stream_type(st, pes->stream_type, MISC_types);
605 static int64_t get_pts(const uint8_t *p)
607 int64_t pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
608 pts |= (AV_RB16(p + 1) >> 1) << 15;
609 pts |= AV_RB16(p + 3) >> 1;
613 static void new_pes_packet(PESContext *pes, AVPacket *pkt)
617 pkt->destruct = av_destruct_packet;
618 pkt->data = pes->buffer;
619 pkt->size = pes->data_index;
620 memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
622 // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
623 if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
624 pkt->stream_index = pes->sub_st->index;
626 pkt->stream_index = pes->st->index;
629 /* store position of first TS packet of this PES packet */
630 pkt->pos = pes->ts_packet_pos;
632 /* reset pts values */
633 pes->pts = AV_NOPTS_VALUE;
634 pes->dts = AV_NOPTS_VALUE;
639 /* return non zero if a packet could be constructed */
640 static int mpegts_push_data(MpegTSFilter *filter,
641 const uint8_t *buf, int buf_size, int is_start,
644 PESContext *pes = filter->u.pes_filter.opaque;
645 MpegTSContext *ts = pes->ts;
653 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
654 new_pes_packet(pes, ts->pkt);
657 pes->state = MPEGTS_HEADER;
659 pes->ts_packet_pos = pos;
662 while (buf_size > 0) {
665 len = PES_START_SIZE - pes->data_index;
668 memcpy(pes->header + pes->data_index, p, len);
669 pes->data_index += len;
672 if (pes->data_index == PES_START_SIZE) {
673 /* we got all the PES or section header. We can now
676 av_hex_dump_log(pes->stream, AV_LOG_DEBUG, pes->header, pes->data_index);
678 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
679 pes->header[2] == 0x01) {
680 /* it must be an mpeg2 PES stream */
681 code = pes->header[3] | 0x100;
682 dprintf(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
684 if ((!pes->st && pes->stream->nb_streams == MAX_STREAMS) ||
685 (pes->st && pes->st->discard == AVDISCARD_ALL) ||
686 code == 0x1be) /* padding_stream */
689 /* stream not present in PMT */
691 pes->st = av_new_stream(ts->stream, pes->pid);
693 return AVERROR(ENOMEM);
694 mpegts_set_stream_info(pes->st, pes, 0, 0);
697 pes->total_size = AV_RB16(pes->header + 4);
698 /* NOTE: a zero total size means the PES size is
700 if (!pes->total_size)
701 pes->total_size = MAX_PES_PAYLOAD;
703 /* allocate pes buffer */
704 pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
706 return AVERROR(ENOMEM);
708 if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
709 code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
710 code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
711 code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
712 pes->state = MPEGTS_PESHEADER;
713 if (pes->st->codec->codec_id == CODEC_ID_NONE) {
714 dprintf(pes->stream, "pid=%x stream_type=%x probing\n",
715 pes->pid, pes->stream_type);
716 pes->st->codec->codec_id = CODEC_ID_PROBE;
719 pes->state = MPEGTS_PAYLOAD;
723 /* otherwise, it should be a table */
726 pes->state = MPEGTS_SKIP;
731 /**********************************************/
732 /* PES packing parsing */
733 case MPEGTS_PESHEADER:
734 len = PES_HEADER_SIZE - pes->data_index;
739 memcpy(pes->header + pes->data_index, p, len);
740 pes->data_index += len;
743 if (pes->data_index == PES_HEADER_SIZE) {
744 pes->pes_header_size = pes->header[8] + 9;
745 pes->state = MPEGTS_PESHEADER_FILL;
748 case MPEGTS_PESHEADER_FILL:
749 len = pes->pes_header_size - pes->data_index;
754 memcpy(pes->header + pes->data_index, p, len);
755 pes->data_index += len;
758 if (pes->data_index == pes->pes_header_size) {
760 unsigned int flags, pes_ext, skip;
762 flags = pes->header[7];
764 pes->pts = AV_NOPTS_VALUE;
765 pes->dts = AV_NOPTS_VALUE;
766 if ((flags & 0xc0) == 0x80) {
767 pes->dts = pes->pts = get_pts(r);
769 } else if ((flags & 0xc0) == 0xc0) {
770 pes->pts = get_pts(r);
772 pes->dts = get_pts(r);
775 pes->extended_stream_id = -1;
776 if (flags & 0x01) { /* PES extension */
778 /* Skip PES private data, program packet sequence counter and P-STD buffer */
779 skip = (pes_ext >> 4) & 0xb;
782 if ((pes_ext & 0x41) == 0x01 &&
783 (r + 2) <= (pes->header + pes->pes_header_size)) {
784 /* PES extension 2 */
785 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
786 pes->extended_stream_id = r[1];
790 /* we got the full header. We parse it and get the payload */
791 pes->state = MPEGTS_PAYLOAD;
796 if (buf_size > 0 && pes->buffer) {
797 if (pes->data_index+buf_size > pes->total_size) {
798 new_pes_packet(pes, ts->pkt);
799 pes->total_size = MAX_PES_PAYLOAD;
800 pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
802 return AVERROR(ENOMEM);
805 memcpy(pes->buffer+pes->data_index, p, buf_size);
806 pes->data_index += buf_size;
819 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
824 /* if no pid found, then add a pid context */
825 pes = av_mallocz(sizeof(PESContext));
829 pes->stream = ts->stream;
831 pes->pcr_pid = pcr_pid;
832 pes->state = MPEGTS_SKIP;
833 pes->pts = AV_NOPTS_VALUE;
834 pes->dts = AV_NOPTS_VALUE;
835 tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
843 static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
845 MpegTSContext *ts = filter->u.section_filter.opaque;
846 SectionHeader h1, *h = &h1;
849 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
850 int program_info_length, pcr_pid, pid, stream_type;
851 int desc_list_len, desc_len, desc_tag;
852 int comp_page, anc_page;
854 uint32_t prog_reg_desc = 0; /* registration descriptor */
857 dprintf(ts->stream, "PMT: len %i\n", section_len);
858 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
861 p_end = section + section_len - 4;
863 if (parse_section_header(h, &p, p_end) < 0)
866 dprintf(ts->stream, "sid=0x%x sec_num=%d/%d\n",
867 h->id, h->sec_num, h->last_sec_num);
869 if (h->tid != PMT_TID)
872 clear_program(ts, h->id);
873 pcr_pid = get16(&p, p_end) & 0x1fff;
876 add_pid_to_pmt(ts, h->id, pcr_pid);
878 dprintf(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
880 program_info_length = get16(&p, p_end) & 0xfff;
881 if (program_info_length < 0)
883 while(program_info_length >= 2) {
885 tag = get8(&p, p_end);
886 len = get8(&p, p_end);
887 if(len > program_info_length - 2)
888 //something else is broken, exit the program_descriptors_loop
890 program_info_length -= len + 2;
891 if(tag == 0x05 && len >= 4) { // registration descriptor
892 prog_reg_desc = bytestream_get_le32(&p);
897 p += program_info_length;
901 // stop parsing after pmt, we found header
902 if (!ts->stream->nb_streams)
907 stream_type = get8(&p, p_end);
910 pid = get16(&p, p_end) & 0x1fff;
914 /* now create ffmpeg stream */
915 if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
916 pes = ts->pids[pid]->u.pes_filter.opaque;
919 if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
920 pes = add_pes_stream(ts, pid, pcr_pid);
922 st = av_new_stream(pes->stream, pes->pid);
928 if (!pes->stream_type)
929 mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
931 add_pid_to_pmt(ts, h->id, pid);
933 av_program_add_stream_index(ts->stream, h->id, st->index);
935 desc_list_len = get16(&p, p_end) & 0xfff;
936 if (desc_list_len < 0)
938 desc_list_end = p + desc_list_len;
939 if (desc_list_end > p_end)
942 desc_tag = get8(&p, desc_list_end);
945 desc_len = get8(&p, desc_list_end);
948 desc_end = p + desc_len;
949 if (desc_end > desc_list_end)
952 dprintf(ts->stream, "tag: 0x%02x len=%d\n",
955 if (st->codec->codec_id == CODEC_ID_NONE &&
956 stream_type == STREAM_TYPE_PRIVATE_DATA)
957 mpegts_find_stream_type(st, desc_tag, DESC_types);
960 case 0x56: /* DVB teletext descriptor */
961 language[0] = get8(&p, desc_end);
962 language[1] = get8(&p, desc_end);
963 language[2] = get8(&p, desc_end);
965 av_metadata_set(&st->metadata, "language", language);
967 case 0x59: /* subtitling descriptor */
968 language[0] = get8(&p, desc_end);
969 language[1] = get8(&p, desc_end);
970 language[2] = get8(&p, desc_end);
973 comp_page = get16(&p, desc_end);
974 anc_page = get16(&p, desc_end);
975 st->codec->sub_id = (anc_page << 16) | comp_page;
976 av_metadata_set(&st->metadata, "language", language);
978 case 0x0a: /* ISO 639 language descriptor */
979 language[0] = get8(&p, desc_end);
980 language[1] = get8(&p, desc_end);
981 language[2] = get8(&p, desc_end);
983 av_metadata_set(&st->metadata, "language", language);
985 case 0x05: /* registration descriptor */
986 st->codec->codec_tag = bytestream_get_le32(&p);
987 dprintf(ts->stream, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
988 if (st->codec->codec_id == CODEC_ID_NONE &&
989 stream_type == STREAM_TYPE_PRIVATE_DATA)
990 mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
997 if (prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) {
998 av_program_add_stream_index(ts->stream, h->id, pes->sub_st->index);
999 pes->sub_st->codec->codec_tag = st->codec->codec_tag;
1004 /* all parameters are there */
1005 mpegts_close_filter(ts, filter);
1008 static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
1010 MpegTSContext *ts = filter->u.section_filter.opaque;
1011 SectionHeader h1, *h = &h1;
1012 const uint8_t *p, *p_end;
1016 dprintf(ts->stream, "PAT:\n");
1017 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
1019 p_end = section + section_len - 4;
1021 if (parse_section_header(h, &p, p_end) < 0)
1023 if (h->tid != PAT_TID)
1028 sid = get16(&p, p_end);
1031 pmt_pid = get16(&p, p_end) & 0x1fff;
1035 dprintf(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
1037 if (sid == 0x0000) {
1040 av_new_program(ts->stream, sid);
1041 mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
1042 add_pat_entry(ts, sid);
1043 add_pid_to_pmt(ts, sid, 0); //add pat pid to program
1044 add_pid_to_pmt(ts, sid, pmt_pid);
1049 static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
1051 MpegTSContext *ts = filter->u.section_filter.opaque;
1052 SectionHeader h1, *h = &h1;
1053 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
1054 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
1055 char *name, *provider_name;
1058 dprintf(ts->stream, "SDT:\n");
1059 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
1062 p_end = section + section_len - 4;
1064 if (parse_section_header(h, &p, p_end) < 0)
1066 if (h->tid != SDT_TID)
1068 onid = get16(&p, p_end);
1071 val = get8(&p, p_end);
1075 sid = get16(&p, p_end);
1078 val = get8(&p, p_end);
1081 desc_list_len = get16(&p, p_end) & 0xfff;
1082 if (desc_list_len < 0)
1084 desc_list_end = p + desc_list_len;
1085 if (desc_list_end > p_end)
1088 desc_tag = get8(&p, desc_list_end);
1091 desc_len = get8(&p, desc_list_end);
1092 desc_end = p + desc_len;
1093 if (desc_end > desc_list_end)
1096 dprintf(ts->stream, "tag: 0x%02x len=%d\n",
1097 desc_tag, desc_len);
1101 service_type = get8(&p, p_end);
1102 if (service_type < 0)
1104 provider_name = getstr8(&p, p_end);
1107 name = getstr8(&p, p_end);
1109 AVProgram *program = av_new_program(ts->stream, sid);
1111 av_metadata_set(&program->metadata, "name", name);
1112 av_metadata_set(&program->metadata, "provider_name", provider_name);
1116 av_free(provider_name);
1127 /* handle one TS packet */
1128 static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
1130 AVFormatContext *s = ts->stream;
1132 int len, pid, cc, cc_ok, afc, is_start;
1133 const uint8_t *p, *p_end;
1136 pid = AV_RB16(packet + 1) & 0x1fff;
1137 if(pid && discard_pid(ts, pid))
1139 is_start = packet[1] & 0x40;
1140 tss = ts->pids[pid];
1141 if (ts->auto_guess && tss == NULL && is_start) {
1142 add_pes_stream(ts, pid, -1);
1143 tss = ts->pids[pid];
1148 /* continuity check (currently not used) */
1149 cc = (packet[3] & 0xf);
1150 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
1153 /* skip adaptation field */
1154 afc = (packet[3] >> 4) & 3;
1156 if (afc == 0) /* reserved value */
1158 if (afc == 2) /* adaptation field only */
1161 /* skip adapation field */
1164 /* if past the end of packet, ignore */
1165 p_end = packet + TS_PACKET_SIZE;
1169 pos = url_ftell(ts->stream->pb);
1170 ts->pos47= pos % ts->raw_packet_size;
1172 if (tss->type == MPEGTS_SECTION) {
1174 /* pointer field present */
1176 if (p + len > p_end)
1179 /* write remaining section bytes */
1180 write_section_data(s, tss,
1182 /* check whether filter has been closed */
1188 write_section_data(s, tss,
1193 write_section_data(s, tss,
1199 // Note: The position here points actually behind the current packet.
1200 if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
1201 pos - ts->raw_packet_size)) < 0)
1208 /* XXX: try to find a better synchro over several packets (use
1209 get_packet_size() ?) */
1210 static int mpegts_resync(AVFormatContext *s)
1212 ByteIOContext *pb = s->pb;
1215 for(i = 0;i < MAX_RESYNC_SIZE; i++) {
1220 url_fseek(pb, -1, SEEK_CUR);
1224 av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
1229 /* return -1 if error or EOF. Return 0 if OK. */
1230 static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size)
1232 ByteIOContext *pb = s->pb;
1236 len = get_buffer(pb, buf, TS_PACKET_SIZE);
1237 if (len != TS_PACKET_SIZE)
1238 return AVERROR(EIO);
1239 /* check paquet sync byte */
1240 if (buf[0] != 0x47) {
1241 /* find a new packet start */
1242 url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
1243 if (mpegts_resync(s) < 0)
1244 return AVERROR(EAGAIN);
1248 skip = raw_packet_size - TS_PACKET_SIZE;
1250 url_fskip(pb, skip);
1257 static int handle_packets(MpegTSContext *ts, int nb_packets)
1259 AVFormatContext *s = ts->stream;
1260 uint8_t packet[TS_PACKET_SIZE];
1261 int packet_num, ret;
1266 if (ts->stop_parse>0)
1269 if (nb_packets != 0 && packet_num >= nb_packets)
1271 ret = read_packet(s, packet, ts->raw_packet_size);
1274 ret = handle_packet(ts, packet);
1281 static int mpegts_probe(AVProbeData *p)
1284 const int size= p->buf_size;
1285 int score, fec_score, dvhs_score;
1286 int check_count= size / TS_FEC_PACKET_SIZE;
1287 #define CHECK_COUNT 10
1289 if (check_count < CHECK_COUNT)
1292 score = analyze(p->buf, TS_PACKET_SIZE *check_count, TS_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
1293 dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
1294 fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
1295 // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
1297 // we need a clear definition for the returned score otherwise things will become messy sooner or later
1298 if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
1299 else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
1300 else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
1303 /* only use the extension for safer guess */
1304 if (match_ext(p->filename, "ts"))
1305 return AVPROBE_SCORE_MAX;
1311 /* return the 90kHz PCR and the extension for the 27MHz PCR. return
1312 (-1) if not available */
1313 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
1314 const uint8_t *packet)
1316 int afc, len, flags;
1320 afc = (packet[3] >> 4) & 3;
1330 if (!(flags & 0x10))
1335 *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
1336 *ppcr_low = ((p[4] & 1) << 8) | p[5];
1340 static int mpegts_read_header(AVFormatContext *s,
1341 AVFormatParameters *ap)
1343 MpegTSContext *ts = s->priv_data;
1344 ByteIOContext *pb = s->pb;
1345 uint8_t buf[5*1024];
1350 ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
1351 if(ap->mpeg2ts_raw){
1352 av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
1357 /* read the first 1024 bytes to get packet size */
1358 pos = url_ftell(pb);
1359 len = get_buffer(pb, buf, sizeof(buf));
1360 if (len != sizeof(buf))
1362 ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
1363 if (ts->raw_packet_size <= 0)
1368 if (s->iformat == &mpegts_demuxer) {
1371 /* first do a scaning to get all the services */
1372 url_fseek(pb, pos, SEEK_SET);
1374 mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
1376 mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
1378 handle_packets(ts, s->probesize / ts->raw_packet_size);
1379 /* if could not find service, enable auto_guess */
1383 dprintf(ts->stream, "tuning done\n");
1385 s->ctx_flags |= AVFMTCTX_NOHEADER;
1388 int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
1389 int64_t pcrs[2], pcr_h;
1390 int packet_count[2];
1391 uint8_t packet[TS_PACKET_SIZE];
1393 /* only read packets */
1395 st = av_new_stream(s, 0);
1398 av_set_pts_info(st, 60, 1, 27000000);
1399 st->codec->codec_type = CODEC_TYPE_DATA;
1400 st->codec->codec_id = CODEC_ID_MPEG2TS;
1402 /* we iterate until we find two PCRs to estimate the bitrate */
1407 ret = read_packet(s, packet, ts->raw_packet_size);
1410 pid = AV_RB16(packet + 1) & 0x1fff;
1411 if ((pcr_pid == -1 || pcr_pid == pid) &&
1412 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
1414 packet_count[nb_pcrs] = nb_packets;
1415 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
1423 /* NOTE1: the bitrate is computed without the FEC */
1424 /* NOTE2: it is only the bitrate of the start of the stream */
1425 ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
1426 ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
1427 s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
1428 st->codec->bit_rate = s->bit_rate;
1429 st->start_time = ts->cur_pcr;
1431 av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
1432 st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
1436 url_fseek(pb, pos, SEEK_SET);
1442 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
1444 static int mpegts_raw_read_packet(AVFormatContext *s,
1447 MpegTSContext *ts = s->priv_data;
1449 int64_t pcr_h, next_pcr_h, pos;
1450 int pcr_l, next_pcr_l;
1451 uint8_t pcr_buf[12];
1453 if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
1454 return AVERROR(ENOMEM);
1455 pkt->pos= url_ftell(s->pb);
1456 ret = read_packet(s, pkt->data, ts->raw_packet_size);
1458 av_free_packet(pkt);
1461 if (ts->mpeg2ts_compute_pcr) {
1462 /* compute exact PCR for each packet */
1463 if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
1464 /* we read the next PCR (XXX: optimize it by using a bigger buffer */
1465 pos = url_ftell(s->pb);
1466 for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
1467 url_fseek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
1468 get_buffer(s->pb, pcr_buf, 12);
1469 if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
1470 /* XXX: not precise enough */
1471 ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
1476 url_fseek(s->pb, pos, SEEK_SET);
1477 /* no next PCR found: we use previous increment */
1478 ts->cur_pcr = pcr_h * 300 + pcr_l;
1480 pkt->pts = ts->cur_pcr;
1481 pkt->duration = ts->pcr_incr;
1482 ts->cur_pcr += ts->pcr_incr;
1484 pkt->stream_index = 0;
1488 static int mpegts_read_packet(AVFormatContext *s,
1491 MpegTSContext *ts = s->priv_data;
1494 if (url_ftell(s->pb) != ts->last_pos) {
1495 /* seek detected, flush pes buffer */
1496 for (i = 0; i < NB_PID_MAX; i++) {
1497 if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
1498 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
1499 av_freep(&pes->buffer);
1500 pes->data_index = 0;
1501 pes->state = MPEGTS_SKIP; /* skip until pes header */
1507 ret = handle_packets(ts, 0);
1509 /* flush pes data left */
1510 for (i = 0; i < NB_PID_MAX; i++) {
1511 if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
1512 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
1513 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
1514 new_pes_packet(pes, pkt);
1515 pes->state = MPEGTS_SKIP;
1523 ts->last_pos = url_ftell(s->pb);
1528 static int mpegts_read_close(AVFormatContext *s)
1530 MpegTSContext *ts = s->priv_data;
1535 for(i=0;i<NB_PID_MAX;i++)
1536 if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
1541 static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
1542 int64_t *ppos, int64_t pos_limit)
1544 MpegTSContext *ts = s->priv_data;
1545 int64_t pos, timestamp;
1546 uint8_t buf[TS_PACKET_SIZE];
1547 int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
1548 const int find_next= 1;
1549 pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
1552 url_fseek(s->pb, pos, SEEK_SET);
1553 if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1554 return AV_NOPTS_VALUE;
1555 if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
1556 parse_pcr(×tamp, &pcr_l, buf) == 0) {
1559 pos += ts->raw_packet_size;
1563 pos -= ts->raw_packet_size;
1565 return AV_NOPTS_VALUE;
1566 url_fseek(s->pb, pos, SEEK_SET);
1567 if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1568 return AV_NOPTS_VALUE;
1569 if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
1570 parse_pcr(×tamp, &pcr_l, buf) == 0) {
1580 #ifdef USE_SYNCPOINT_SEARCH
1582 static int read_seek2(AVFormatContext *s,
1591 int64_t ts_ret, ts_adj;
1592 int stream_index_gen_search;
1594 AVParserState *backup;
1596 backup = ff_store_parser_state(s);
1598 // detect direction of seeking for search purposes
1599 flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ?
1600 AVSEEK_FLAG_BACKWARD : 0;
1602 if (flags & AVSEEK_FLAG_BYTE) {
1603 // use position directly, we will search starting from it
1606 // search for some position with good timestamp match
1607 if (stream_index < 0) {
1608 stream_index_gen_search = av_find_default_stream_index(s);
1609 if (stream_index_gen_search < 0) {
1610 ff_restore_parser_state(s, backup);
1614 st = s->streams[stream_index_gen_search];
1615 // timestamp for default must be expressed in AV_TIME_BASE units
1616 ts_adj = av_rescale(target_ts,
1618 AV_TIME_BASE * (int64_t)st->time_base.num);
1621 stream_index_gen_search = stream_index;
1623 pos = av_gen_search(s, stream_index_gen_search, ts_adj,
1627 flags, &ts_ret, mpegts_get_pcr);
1629 ff_restore_parser_state(s, backup);
1634 // search for actual matching keyframe/starting position for all streams
1635 if (ff_gen_syncpoint_search(s, stream_index, pos,
1636 min_ts, target_ts, max_ts,
1638 ff_restore_parser_state(s, backup);
1642 ff_free_parser_state(s, backup);
1646 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
1649 if (flags & AVSEEK_FLAG_BACKWARD) {
1650 flags &= ~AVSEEK_FLAG_BACKWARD;
1651 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, target_ts, flags);
1653 // for compatibility reasons, seek to the best-fitting timestamp
1654 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
1656 ret = read_seek2(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
1658 // for compatibility reasons, seek to the best-fitting timestamp
1659 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
1666 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
1667 MpegTSContext *ts = s->priv_data;
1668 uint8_t buf[TS_PACKET_SIZE];
1671 if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
1674 pos= url_ftell(s->pb);
1677 url_fseek(s->pb, pos, SEEK_SET);
1678 if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1680 // pid = AV_RB16(buf + 1) & 0x1fff;
1681 if(buf[1] & 0x40) break;
1682 pos += ts->raw_packet_size;
1684 url_fseek(s->pb, pos, SEEK_SET);
1691 /**************************************************************/
1692 /* parsing functions - called from other demuxers such as RTP */
1694 MpegTSContext *mpegts_parse_open(AVFormatContext *s)
1698 ts = av_mallocz(sizeof(MpegTSContext));
1701 /* no stream case, currently used by RTP */
1702 ts->raw_packet_size = TS_PACKET_SIZE;
1708 /* return the consumed length if a packet was output, or -1 if no
1710 int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
1711 const uint8_t *buf, int len)
1719 if (ts->stop_parse>0)
1721 if (len < TS_PACKET_SIZE)
1723 if (buf[0] != 0x47) {
1727 handle_packet(ts, buf);
1728 buf += TS_PACKET_SIZE;
1729 len -= TS_PACKET_SIZE;
1735 void mpegts_parse_close(MpegTSContext *ts)
1739 for(i=0;i<NB_PID_MAX;i++)
1740 av_free(ts->pids[i]);
1744 AVInputFormat mpegts_demuxer = {
1746 NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
1747 sizeof(MpegTSContext),
1754 .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
1755 #ifdef USE_SYNCPOINT_SEARCH
1756 .read_seek2 = read_seek2,
1760 AVInputFormat mpegtsraw_demuxer = {
1762 NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
1763 sizeof(MpegTSContext),
1766 mpegts_raw_read_packet,
1770 .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
1771 #ifdef USE_SYNCPOINT_SEARCH
1772 .read_seek2 = read_seek2,