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 4096
41 #define MAX_PES_PAYLOAD 200*1024
43 typedef struct PESContext PESContext;
45 static PESContext* add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type);
47 enum MpegTSFilterType {
52 typedef struct MpegTSFilter MpegTSFilter;
54 typedef int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos);
56 typedef struct MpegTSPESFilter {
61 typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
63 typedef void SetServiceCallback(void *opaque, int ret);
65 typedef struct MpegTSSectionFilter {
69 unsigned int check_crc:1;
70 unsigned int end_of_section_reached:1;
71 SectionCallback *section_cb;
73 } MpegTSSectionFilter;
77 int last_cc; /* last cc code (-1 if first packet) */
78 enum MpegTSFilterType type;
80 MpegTSPESFilter pes_filter;
81 MpegTSSectionFilter section_filter;
85 #define MAX_PIDS_PER_PROGRAM 64
87 unsigned int id; //program id/service id
89 unsigned int pids[MAX_PIDS_PER_PROGRAM];
92 struct MpegTSContext {
94 AVFormatContext *stream;
95 /** raw packet size, including FEC if present */
100 /** if true, all pids are analyzed to find streams */
103 /** compute exact PCR for each transport stream packet */
104 int mpeg2ts_compute_pcr;
106 int64_t cur_pcr; /**< used to estimate the exact PCR */
107 int pcr_incr; /**< used to estimate the exact PCR */
109 /* data needed to handle file based ts */
110 /** stop parsing loop */
112 /** packet containing Audio/Video data */
114 /** to detect seek */
117 /******************************************/
118 /* private mpegts data */
120 /** structure to keep track of Program->pids mapping */
125 /** filters for various streams specified by PMT + for the PAT and PMT */
126 MpegTSFilter *pids[NB_PID_MAX];
129 /* TS stream handling */
134 MPEGTS_PESHEADER_FILL,
139 /* enough for PES header + length */
140 #define PES_START_SIZE 6
141 #define PES_HEADER_SIZE 9
142 #define MAX_PES_HEADER_SIZE (9 + 255)
146 int pcr_pid; /**< if -1 then all packets containing PCR are considered */
149 AVFormatContext *stream;
151 enum MpegTSState state;
152 /* used to get the format */
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 { 0x1b, CODEC_TYPE_VIDEO, CODEC_ID_H264 },
503 { 0xd1, CODEC_TYPE_VIDEO, CODEC_ID_DIRAC },
504 { 0xea, CODEC_TYPE_VIDEO, CODEC_ID_VC1 },
508 static const StreamType HDMV_types[] = {
509 { 0x80, CODEC_TYPE_AUDIO, CODEC_ID_PCM_BLURAY },
510 { 0x81, CODEC_TYPE_AUDIO, CODEC_ID_AC3 },
511 { 0x82, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
512 { 0x90, CODEC_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
517 static const StreamType MISC_types[] = {
518 { 0x81, CODEC_TYPE_AUDIO, CODEC_ID_AC3 },
519 { 0x8a, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
523 static const StreamType REGD_types[] = {
524 { MKTAG('d','r','a','c'), CODEC_TYPE_VIDEO, CODEC_ID_DIRAC },
525 { MKTAG('A','C','-','3'), CODEC_TYPE_AUDIO, CODEC_ID_AC3 },
529 /* descriptor present */
530 static const StreamType DESC_types[] = {
531 { 0x6a, CODEC_TYPE_AUDIO, CODEC_ID_AC3 }, /* AC-3 descriptor */
532 { 0x7a, CODEC_TYPE_AUDIO, CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
533 { 0x7b, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
534 { 0x59, CODEC_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
538 static void mpegts_find_stream_type(AVStream *st,
539 uint32_t stream_type, const StreamType *types)
541 for (; types->stream_type; types++) {
542 if (stream_type == types->stream_type) {
543 st->codec->codec_type = types->codec_type;
544 st->codec->codec_id = types->codec_id;
550 static AVStream *new_pes_av_stream(PESContext *pes, uint32_t prog_reg_desc, uint32_t code)
552 AVStream *st = av_new_stream(pes->stream, pes->pid);
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;
564 dprintf(pes->stream, "stream_type=%x pid=%x prog_reg_desc=%.4s\n",
565 pes->stream_type, pes->pid, (char*)&prog_reg_desc);
567 st->codec->codec_tag = pes->stream_type;
569 mpegts_find_stream_type(st, pes->stream_type, ISO_types);
570 if (prog_reg_desc == AV_RL32("HDMV") &&
571 st->codec->codec_id == CODEC_ID_NONE)
572 mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
573 if (st->codec->codec_id == CODEC_ID_NONE)
574 mpegts_find_stream_type(st, pes->stream_type, MISC_types);
576 /* stream was not present in PMT, guess based on PES start code */
577 if (st->codec->codec_id == CODEC_ID_NONE) {
578 if (code >= 0x1c0 && code <= 0x1df) {
579 st->codec->codec_type = CODEC_TYPE_AUDIO;
580 st->codec->codec_id = CODEC_ID_MP2;
581 } else if (code == 0x1bd) {
582 st->codec->codec_type = CODEC_TYPE_AUDIO;
583 st->codec->codec_id = CODEC_ID_AC3;
590 static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
592 MpegTSContext *ts = filter->u.section_filter.opaque;
593 SectionHeader h1, *h = &h1;
596 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
597 int program_info_length, pcr_pid, pid, stream_type;
598 int desc_list_len, desc_len, desc_tag;
599 int comp_page, anc_page;
601 uint32_t prog_reg_desc = 0; /* registration descriptor */
604 dprintf(ts->stream, "PMT: len %i\n", section_len);
605 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
608 p_end = section + section_len - 4;
610 if (parse_section_header(h, &p, p_end) < 0)
613 dprintf(ts->stream, "sid=0x%x sec_num=%d/%d\n",
614 h->id, h->sec_num, h->last_sec_num);
616 if (h->tid != PMT_TID)
619 clear_program(ts, h->id);
620 pcr_pid = get16(&p, p_end) & 0x1fff;
623 add_pid_to_pmt(ts, h->id, pcr_pid);
625 dprintf(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
627 program_info_length = get16(&p, p_end) & 0xfff;
628 if (program_info_length < 0)
630 while(program_info_length >= 2) {
632 tag = get8(&p, p_end);
633 len = get8(&p, p_end);
634 if(len > program_info_length - 2)
635 //something else is broken, exit the program_descriptors_loop
637 program_info_length -= len + 2;
638 if(tag == 0x05 && len >= 4) { // registration descriptor
639 prog_reg_desc = bytestream_get_le32(&p);
644 p += program_info_length;
648 // stop parsing after pmt, we found header
649 if (!ts->stream->nb_streams)
654 stream_type = get8(&p, p_end);
657 pid = get16(&p, p_end) & 0x1fff;
661 /* now create ffmpeg stream */
662 if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
663 pes = ts->pids[pid]->u.pes_filter.opaque;
666 if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
667 pes = add_pes_stream(ts, pid, pcr_pid, stream_type);
669 st = new_pes_av_stream(pes, prog_reg_desc, 0);
675 add_pid_to_pmt(ts, h->id, pid);
677 av_program_add_stream_index(ts->stream, h->id, st->index);
679 desc_list_len = get16(&p, p_end) & 0xfff;
680 if (desc_list_len < 0)
682 desc_list_end = p + desc_list_len;
683 if (desc_list_end > p_end)
686 desc_tag = get8(&p, desc_list_end);
689 desc_len = get8(&p, desc_list_end);
692 desc_end = p + desc_len;
693 if (desc_end > desc_list_end)
696 dprintf(ts->stream, "tag: 0x%02x len=%d\n",
699 if (st->codec->codec_id == CODEC_ID_NONE &&
700 stream_type == STREAM_TYPE_PRIVATE_DATA)
701 mpegts_find_stream_type(st, desc_tag, DESC_types);
704 case 0x59: /* subtitling descriptor */
705 language[0] = get8(&p, desc_end);
706 language[1] = get8(&p, desc_end);
707 language[2] = get8(&p, desc_end);
710 comp_page = get16(&p, desc_end);
711 anc_page = get16(&p, desc_end);
712 st->codec->sub_id = (anc_page << 16) | comp_page;
713 av_metadata_set(&st->metadata, "language", language);
715 case 0x0a: /* ISO 639 language descriptor */
716 language[0] = get8(&p, desc_end);
717 language[1] = get8(&p, desc_end);
718 language[2] = get8(&p, desc_end);
720 av_metadata_set(&st->metadata, "language", language);
722 case 0x05: /* registration descriptor */
723 st->codec->codec_tag = bytestream_get_le32(&p);
724 dprintf(ts->stream, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
725 if (st->codec->codec_id == CODEC_ID_NONE &&
726 stream_type == STREAM_TYPE_PRIVATE_DATA)
727 mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
736 /* all parameters are there */
737 mpegts_close_filter(ts, filter);
740 static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
742 MpegTSContext *ts = filter->u.section_filter.opaque;
743 SectionHeader h1, *h = &h1;
744 const uint8_t *p, *p_end;
748 dprintf(ts->stream, "PAT:\n");
749 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
751 p_end = section + section_len - 4;
753 if (parse_section_header(h, &p, p_end) < 0)
755 if (h->tid != PAT_TID)
760 sid = get16(&p, p_end);
763 pmt_pid = get16(&p, p_end) & 0x1fff;
767 dprintf(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
772 av_new_program(ts->stream, sid);
773 mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
774 add_pat_entry(ts, sid);
775 add_pid_to_pmt(ts, sid, 0); //add pat pid to program
776 add_pid_to_pmt(ts, sid, pmt_pid);
781 static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
783 MpegTSContext *ts = filter->u.section_filter.opaque;
784 SectionHeader h1, *h = &h1;
785 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
786 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
787 char *name, *provider_name;
790 dprintf(ts->stream, "SDT:\n");
791 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
794 p_end = section + section_len - 4;
796 if (parse_section_header(h, &p, p_end) < 0)
798 if (h->tid != SDT_TID)
800 onid = get16(&p, p_end);
803 val = get8(&p, p_end);
807 sid = get16(&p, p_end);
810 val = get8(&p, p_end);
813 desc_list_len = get16(&p, p_end) & 0xfff;
814 if (desc_list_len < 0)
816 desc_list_end = p + desc_list_len;
817 if (desc_list_end > p_end)
820 desc_tag = get8(&p, desc_list_end);
823 desc_len = get8(&p, desc_list_end);
824 desc_end = p + desc_len;
825 if (desc_end > desc_list_end)
828 dprintf(ts->stream, "tag: 0x%02x len=%d\n",
833 service_type = get8(&p, p_end);
834 if (service_type < 0)
836 provider_name = getstr8(&p, p_end);
839 name = getstr8(&p, p_end);
841 AVProgram *program = av_new_program(ts->stream, sid);
843 av_metadata_set(&program->metadata, "name", name);
844 av_metadata_set(&program->metadata, "provider_name", provider_name);
848 av_free(provider_name);
859 static int64_t get_pts(const uint8_t *p)
861 int64_t pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
862 pts |= (AV_RB16(p + 1) >> 1) << 15;
863 pts |= AV_RB16(p + 3) >> 1;
867 static void new_pes_packet(PESContext *pes, AVPacket *pkt)
871 pkt->destruct = av_destruct_packet;
872 pkt->data = pes->buffer;
873 pkt->size = pes->data_index;
874 memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
876 pkt->stream_index = pes->st->index;
879 /* store position of first TS packet of this PES packet */
880 pkt->pos = pes->ts_packet_pos;
882 /* reset pts values */
883 pes->pts = AV_NOPTS_VALUE;
884 pes->dts = AV_NOPTS_VALUE;
889 /* return non zero if a packet could be constructed */
890 static int mpegts_push_data(MpegTSFilter *filter,
891 const uint8_t *buf, int buf_size, int is_start,
894 PESContext *pes = filter->u.pes_filter.opaque;
895 MpegTSContext *ts = pes->ts;
903 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
904 new_pes_packet(pes, ts->pkt);
907 pes->state = MPEGTS_HEADER;
909 pes->ts_packet_pos = pos;
912 while (buf_size > 0) {
915 len = PES_START_SIZE - pes->data_index;
918 memcpy(pes->header + pes->data_index, p, len);
919 pes->data_index += len;
922 if (pes->data_index == PES_START_SIZE) {
923 /* we got all the PES or section header. We can now
926 av_hex_dump_log(pes->stream, AV_LOG_DEBUG, pes->header, pes->data_index);
928 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
929 pes->header[2] == 0x01) {
930 /* it must be an mpeg2 PES stream */
931 code = pes->header[3] | 0x100;
932 dprintf(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
934 if ((!pes->st && pes->stream->nb_streams == MAX_STREAMS) ||
935 (pes->st && pes->st->discard == AVDISCARD_ALL) ||
936 code == 0x1be) /* padding_stream */
939 /* stream not present in PMT */
941 pes->st = new_pes_av_stream(pes, 0, code);
943 return AVERROR(ENOMEM);
945 pes->total_size = AV_RB16(pes->header + 4);
946 /* NOTE: a zero total size means the PES size is
948 if (!pes->total_size)
949 pes->total_size = MAX_PES_PAYLOAD;
951 /* allocate pes buffer */
952 pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
954 return AVERROR(ENOMEM);
956 if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
957 code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
958 code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
959 code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
960 pes->state = MPEGTS_PESHEADER;
961 if (pes->st->codec->codec_id == CODEC_ID_NONE) {
962 dprintf(pes->stream, "pid=%x stream_type=%x probing\n",
963 pes->pid, pes->stream_type);
964 pes->st->codec->codec_id = CODEC_ID_PROBE;
967 pes->state = MPEGTS_PAYLOAD;
971 /* otherwise, it should be a table */
974 pes->state = MPEGTS_SKIP;
979 /**********************************************/
980 /* PES packing parsing */
981 case MPEGTS_PESHEADER:
982 len = PES_HEADER_SIZE - pes->data_index;
987 memcpy(pes->header + pes->data_index, p, len);
988 pes->data_index += len;
991 if (pes->data_index == PES_HEADER_SIZE) {
992 pes->pes_header_size = pes->header[8] + 9;
993 pes->state = MPEGTS_PESHEADER_FILL;
996 case MPEGTS_PESHEADER_FILL:
997 len = pes->pes_header_size - pes->data_index;
1002 memcpy(pes->header + pes->data_index, p, len);
1003 pes->data_index += len;
1006 if (pes->data_index == pes->pes_header_size) {
1010 flags = pes->header[7];
1011 r = pes->header + 9;
1012 pes->pts = AV_NOPTS_VALUE;
1013 pes->dts = AV_NOPTS_VALUE;
1014 if ((flags & 0xc0) == 0x80) {
1015 pes->dts = pes->pts = get_pts(r);
1017 } else if ((flags & 0xc0) == 0xc0) {
1018 pes->pts = get_pts(r);
1020 pes->dts = get_pts(r);
1024 /* we got the full header. We parse it and get the payload */
1025 pes->state = MPEGTS_PAYLOAD;
1026 pes->data_index = 0;
1029 case MPEGTS_PAYLOAD:
1031 if (pes->data_index+buf_size > pes->total_size) {
1032 new_pes_packet(pes, ts->pkt);
1033 pes->total_size = MAX_PES_PAYLOAD;
1034 pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
1036 return AVERROR(ENOMEM);
1039 memcpy(pes->buffer+pes->data_index, p, buf_size);
1040 pes->data_index += buf_size;
1053 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type)
1058 /* if no pid found, then add a pid context */
1059 pes = av_mallocz(sizeof(PESContext));
1063 pes->stream = ts->stream;
1065 pes->pcr_pid = pcr_pid;
1066 pes->stream_type = stream_type;
1067 pes->state = MPEGTS_SKIP;
1068 pes->pts = AV_NOPTS_VALUE;
1069 pes->dts = AV_NOPTS_VALUE;
1070 tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
1078 /* handle one TS packet */
1079 static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
1081 AVFormatContext *s = ts->stream;
1083 int len, pid, cc, cc_ok, afc, is_start;
1084 const uint8_t *p, *p_end;
1087 pid = AV_RB16(packet + 1) & 0x1fff;
1088 if(pid && discard_pid(ts, pid))
1090 is_start = packet[1] & 0x40;
1091 tss = ts->pids[pid];
1092 if (ts->auto_guess && tss == NULL && is_start) {
1093 add_pes_stream(ts, pid, -1, 0);
1094 tss = ts->pids[pid];
1099 /* continuity check (currently not used) */
1100 cc = (packet[3] & 0xf);
1101 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
1104 /* skip adaptation field */
1105 afc = (packet[3] >> 4) & 3;
1107 if (afc == 0) /* reserved value */
1109 if (afc == 2) /* adaptation field only */
1112 /* skip adapation field */
1115 /* if past the end of packet, ignore */
1116 p_end = packet + TS_PACKET_SIZE;
1120 pos = url_ftell(ts->stream->pb);
1121 ts->pos47= pos % ts->raw_packet_size;
1123 if (tss->type == MPEGTS_SECTION) {
1125 /* pointer field present */
1127 if (p + len > p_end)
1130 /* write remaining section bytes */
1131 write_section_data(s, tss,
1133 /* check whether filter has been closed */
1139 write_section_data(s, tss,
1144 write_section_data(s, tss,
1150 // Note: The position here points actually behind the current packet.
1151 if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
1152 pos - ts->raw_packet_size)) < 0)
1159 /* XXX: try to find a better synchro over several packets (use
1160 get_packet_size() ?) */
1161 static int mpegts_resync(ByteIOContext *pb)
1165 for(i = 0;i < MAX_RESYNC_SIZE; i++) {
1170 url_fseek(pb, -1, SEEK_CUR);
1178 /* return -1 if error or EOF. Return 0 if OK. */
1179 static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size)
1184 len = get_buffer(pb, buf, TS_PACKET_SIZE);
1185 if (len != TS_PACKET_SIZE)
1186 return AVERROR(EIO);
1187 /* check paquet sync byte */
1188 if (buf[0] != 0x47) {
1189 /* find a new packet start */
1190 url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
1191 if (mpegts_resync(pb) < 0)
1192 return AVERROR_INVALIDDATA;
1196 skip = raw_packet_size - TS_PACKET_SIZE;
1198 url_fskip(pb, skip);
1205 static int handle_packets(MpegTSContext *ts, int nb_packets)
1207 AVFormatContext *s = ts->stream;
1208 ByteIOContext *pb = s->pb;
1209 uint8_t packet[TS_PACKET_SIZE];
1210 int packet_num, ret;
1215 if (ts->stop_parse>0)
1218 if (nb_packets != 0 && packet_num >= nb_packets)
1220 ret = read_packet(pb, packet, ts->raw_packet_size);
1223 ret = handle_packet(ts, packet);
1230 static int mpegts_probe(AVProbeData *p)
1233 const int size= p->buf_size;
1234 int score, fec_score, dvhs_score;
1235 int check_count= size / TS_FEC_PACKET_SIZE;
1236 #define CHECK_COUNT 10
1238 if (check_count < CHECK_COUNT)
1241 score = analyze(p->buf, TS_PACKET_SIZE *check_count, TS_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
1242 dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
1243 fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
1244 // av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
1246 // we need a clear definition for the returned score otherwise things will become messy sooner or later
1247 if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
1248 else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
1249 else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
1252 /* only use the extension for safer guess */
1253 if (match_ext(p->filename, "ts"))
1254 return AVPROBE_SCORE_MAX;
1260 /* return the 90kHz PCR and the extension for the 27MHz PCR. return
1261 (-1) if not available */
1262 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
1263 const uint8_t *packet)
1265 int afc, len, flags;
1269 afc = (packet[3] >> 4) & 3;
1279 if (!(flags & 0x10))
1284 *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
1285 *ppcr_low = ((p[4] & 1) << 8) | p[5];
1289 static int mpegts_read_header(AVFormatContext *s,
1290 AVFormatParameters *ap)
1292 MpegTSContext *ts = s->priv_data;
1293 ByteIOContext *pb = s->pb;
1294 uint8_t buf[5*1024];
1299 ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
1300 if(ap->mpeg2ts_raw){
1301 av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
1306 /* read the first 1024 bytes to get packet size */
1307 pos = url_ftell(pb);
1308 len = get_buffer(pb, buf, sizeof(buf));
1309 if (len != sizeof(buf))
1311 ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
1312 if (ts->raw_packet_size <= 0)
1317 if (s->iformat == &mpegts_demuxer) {
1320 /* first do a scaning to get all the services */
1321 url_fseek(pb, pos, SEEK_SET);
1323 mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
1325 mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
1327 handle_packets(ts, s->probesize);
1328 /* if could not find service, enable auto_guess */
1332 dprintf(ts->stream, "tuning done\n");
1334 s->ctx_flags |= AVFMTCTX_NOHEADER;
1337 int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
1338 int64_t pcrs[2], pcr_h;
1339 int packet_count[2];
1340 uint8_t packet[TS_PACKET_SIZE];
1342 /* only read packets */
1344 st = av_new_stream(s, 0);
1347 av_set_pts_info(st, 60, 1, 27000000);
1348 st->codec->codec_type = CODEC_TYPE_DATA;
1349 st->codec->codec_id = CODEC_ID_MPEG2TS;
1351 /* we iterate until we find two PCRs to estimate the bitrate */
1356 ret = read_packet(s->pb, packet, ts->raw_packet_size);
1359 pid = AV_RB16(packet + 1) & 0x1fff;
1360 if ((pcr_pid == -1 || pcr_pid == pid) &&
1361 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
1363 packet_count[nb_pcrs] = nb_packets;
1364 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
1372 /* NOTE1: the bitrate is computed without the FEC */
1373 /* NOTE2: it is only the bitrate of the start of the stream */
1374 ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
1375 ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
1376 s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
1377 st->codec->bit_rate = s->bit_rate;
1378 st->start_time = ts->cur_pcr;
1380 av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
1381 st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
1385 url_fseek(pb, pos, SEEK_SET);
1391 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
1393 static int mpegts_raw_read_packet(AVFormatContext *s,
1396 MpegTSContext *ts = s->priv_data;
1398 int64_t pcr_h, next_pcr_h, pos;
1399 int pcr_l, next_pcr_l;
1400 uint8_t pcr_buf[12];
1402 if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
1403 return AVERROR(ENOMEM);
1404 pkt->pos= url_ftell(s->pb);
1405 ret = read_packet(s->pb, pkt->data, ts->raw_packet_size);
1407 av_free_packet(pkt);
1410 if (ts->mpeg2ts_compute_pcr) {
1411 /* compute exact PCR for each packet */
1412 if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
1413 /* we read the next PCR (XXX: optimize it by using a bigger buffer */
1414 pos = url_ftell(s->pb);
1415 for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
1416 url_fseek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
1417 get_buffer(s->pb, pcr_buf, 12);
1418 if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
1419 /* XXX: not precise enough */
1420 ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
1425 url_fseek(s->pb, pos, SEEK_SET);
1426 /* no next PCR found: we use previous increment */
1427 ts->cur_pcr = pcr_h * 300 + pcr_l;
1429 pkt->pts = ts->cur_pcr;
1430 pkt->duration = ts->pcr_incr;
1431 ts->cur_pcr += ts->pcr_incr;
1433 pkt->stream_index = 0;
1437 static int mpegts_read_packet(AVFormatContext *s,
1440 MpegTSContext *ts = s->priv_data;
1443 if (url_ftell(s->pb) != ts->last_pos) {
1444 /* seek detected, flush pes buffer */
1445 for (i = 0; i < NB_PID_MAX; i++) {
1446 if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
1447 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
1448 av_freep(&pes->buffer);
1449 pes->data_index = 0;
1450 pes->state = MPEGTS_SKIP; /* skip until pes header */
1456 ret = handle_packets(ts, 0);
1458 /* flush pes data left */
1459 for (i = 0; i < NB_PID_MAX; i++) {
1460 if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
1461 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
1462 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
1463 new_pes_packet(pes, pkt);
1464 pes->state = MPEGTS_SKIP;
1472 ts->last_pos = url_ftell(s->pb);
1477 static int mpegts_read_close(AVFormatContext *s)
1479 MpegTSContext *ts = s->priv_data;
1484 for(i=0;i<NB_PID_MAX;i++)
1485 if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
1490 static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
1491 int64_t *ppos, int64_t pos_limit)
1493 MpegTSContext *ts = s->priv_data;
1494 int64_t pos, timestamp;
1495 uint8_t buf[TS_PACKET_SIZE];
1496 int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
1497 const int find_next= 1;
1498 pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
1501 url_fseek(s->pb, pos, SEEK_SET);
1502 if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1503 return AV_NOPTS_VALUE;
1504 if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
1505 parse_pcr(×tamp, &pcr_l, buf) == 0) {
1508 pos += ts->raw_packet_size;
1512 pos -= ts->raw_packet_size;
1514 return AV_NOPTS_VALUE;
1515 url_fseek(s->pb, pos, SEEK_SET);
1516 if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1517 return AV_NOPTS_VALUE;
1518 if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
1519 parse_pcr(×tamp, &pcr_l, buf) == 0) {
1529 #ifdef USE_SYNCPOINT_SEARCH
1531 static int read_seek2(AVFormatContext *s,
1540 int64_t ts_ret, ts_adj;
1541 int stream_index_gen_search;
1543 AVParserState *backup;
1545 backup = ff_store_parser_state(s);
1547 // detect direction of seeking for search purposes
1548 flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ?
1549 AVSEEK_FLAG_BACKWARD : 0;
1551 if (flags & AVSEEK_FLAG_BYTE) {
1552 // use position directly, we will search starting from it
1555 // search for some position with good timestamp match
1556 if (stream_index < 0) {
1557 stream_index_gen_search = av_find_default_stream_index(s);
1558 if (stream_index_gen_search < 0) {
1559 ff_restore_parser_state(s, backup);
1563 st = s->streams[stream_index_gen_search];
1564 // timestamp for default must be expressed in AV_TIME_BASE units
1565 ts_adj = av_rescale(target_ts,
1567 AV_TIME_BASE * (int64_t)st->time_base.num);
1570 stream_index_gen_search = stream_index;
1572 pos = av_gen_search(s, stream_index_gen_search, ts_adj,
1576 flags, &ts_ret, mpegts_get_pcr);
1578 ff_restore_parser_state(s, backup);
1583 // search for actual matching keyframe/starting position for all streams
1584 if (ff_gen_syncpoint_search(s, stream_index, pos,
1585 min_ts, target_ts, max_ts,
1587 ff_restore_parser_state(s, backup);
1591 ff_free_parser_state(s, backup);
1595 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
1598 if (flags & AVSEEK_FLAG_BACKWARD) {
1599 flags &= ~AVSEEK_FLAG_BACKWARD;
1600 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, target_ts, flags);
1602 // for compatibility reasons, seek to the best-fitting timestamp
1603 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
1605 ret = read_seek2(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
1607 // for compatibility reasons, seek to the best-fitting timestamp
1608 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
1615 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
1616 MpegTSContext *ts = s->priv_data;
1617 uint8_t buf[TS_PACKET_SIZE];
1620 if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
1623 pos= url_ftell(s->pb);
1626 url_fseek(s->pb, pos, SEEK_SET);
1627 if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1629 // pid = AV_RB16(buf + 1) & 0x1fff;
1630 if(buf[1] & 0x40) break;
1631 pos += ts->raw_packet_size;
1633 url_fseek(s->pb, pos, SEEK_SET);
1640 /**************************************************************/
1641 /* parsing functions - called from other demuxers such as RTP */
1643 MpegTSContext *mpegts_parse_open(AVFormatContext *s)
1647 ts = av_mallocz(sizeof(MpegTSContext));
1650 /* no stream case, currently used by RTP */
1651 ts->raw_packet_size = TS_PACKET_SIZE;
1657 /* return the consumed length if a packet was output, or -1 if no
1659 int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
1660 const uint8_t *buf, int len)
1668 if (ts->stop_parse>0)
1670 if (len < TS_PACKET_SIZE)
1672 if (buf[0] != 0x47) {
1676 handle_packet(ts, buf);
1677 buf += TS_PACKET_SIZE;
1678 len -= TS_PACKET_SIZE;
1684 void mpegts_parse_close(MpegTSContext *ts)
1688 for(i=0;i<NB_PID_MAX;i++)
1689 av_free(ts->pids[i]);
1693 AVInputFormat mpegts_demuxer = {
1695 NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
1696 sizeof(MpegTSContext),
1703 .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
1704 #ifdef USE_SYNCPOINT_SEARCH
1705 .read_seek2 = read_seek2,
1709 AVInputFormat mpegtsraw_demuxer = {
1711 NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
1712 sizeof(MpegTSContext),
1715 mpegts_raw_read_packet,
1719 .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
1720 #ifdef USE_SYNCPOINT_SEARCH
1721 .read_seek2 = read_seek2,