Add missing #includes for avutil.h, required for the AV_VERSION* macros.
[ffmpeg-lucabe.git] / libavformat / mpegts.c
blob44112f0fc5faed67d6eac00241d640b4064078a3
1 /*
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
22 //#define DEBUG
23 //#define DEBUG_SEEK
24 //#define USE_SYNCPOINT_SEARCH
26 #include "libavutil/crc.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavcodec/bytestream.h"
29 #include "avformat.h"
30 #include "mpegts.h"
31 #include "internal.h"
32 #include "seek.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 {
46 MPEGTS_PES,
47 MPEGTS_SECTION,
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 {
55 PESCallback *pes_cb;
56 void *opaque;
57 } 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 {
64 int section_index;
65 int section_h_size;
66 uint8_t *section_buf;
67 unsigned int check_crc:1;
68 unsigned int end_of_section_reached:1;
69 SectionCallback *section_cb;
70 void *opaque;
71 } MpegTSSectionFilter;
73 struct MpegTSFilter {
74 int pid;
75 int last_cc; /* last cc code (-1 if first packet) */
76 enum MpegTSFilterType type;
77 union {
78 MpegTSPESFilter pes_filter;
79 MpegTSSectionFilter section_filter;
80 } u;
83 #define MAX_PIDS_PER_PROGRAM 64
84 struct Program {
85 unsigned int id; //program id/service id
86 unsigned int nb_pids;
87 unsigned int pids[MAX_PIDS_PER_PROGRAM];
90 struct MpegTSContext {
91 /* user data */
92 AVFormatContext *stream;
93 /** raw packet size, including FEC if present */
94 int raw_packet_size;
96 int pos47;
98 /** if true, all pids are analyzed to find streams */
99 int auto_guess;
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 */
109 int stop_parse;
110 /** packet containing Audio/Video data */
111 AVPacket *pkt;
112 /** to detect seek */
113 int64_t last_pos;
115 /******************************************/
116 /* private mpegts data */
117 /* scan context */
118 /** structure to keep track of Program->pids mapping */
119 unsigned int nb_prg;
120 struct Program *prg;
123 /** filters for various streams specified by PMT + for the PAT and PMT */
124 MpegTSFilter *pids[NB_PID_MAX];
127 /* TS stream handling */
129 enum MpegTSState {
130 MPEGTS_HEADER = 0,
131 MPEGTS_PESHEADER,
132 MPEGTS_PESHEADER_FILL,
133 MPEGTS_PAYLOAD,
134 MPEGTS_SKIP,
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)
142 struct PESContext {
143 int pid;
144 int pcr_pid; /**< if -1 then all packets containing PCR are considered */
145 int stream_type;
146 MpegTSContext *ts;
147 AVFormatContext *stream;
148 AVStream *st;
149 AVStream *sub_st; /**< stream for the embedded AC3 stream in HDMV TrueHD */
150 enum MpegTSState state;
151 /* used to get the format */
152 int data_index;
153 int total_size;
154 int pes_header_size;
155 int extended_stream_id;
156 int64_t pts, dts;
157 int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
158 uint8_t header[MAX_PES_HEADER_SIZE];
159 uint8_t *buffer;
162 extern AVInputFormat mpegts_demuxer;
164 static void clear_program(MpegTSContext *ts, unsigned int programid)
166 int i;
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)
175 av_freep(&ts->prg);
176 ts->nb_prg=0;
179 static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
181 struct Program *p;
182 void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(struct Program));
183 if(!tmp)
184 return;
185 ts->prg = tmp;
186 p = &ts->prg[ts->nb_prg];
187 p->id = programid;
188 p->nb_pids = 0;
189 ts->nb_prg++;
192 static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
194 int i;
195 struct Program *p = NULL;
196 for(i=0; i<ts->nb_prg; i++) {
197 if(ts->prg[i].id == programid) {
198 p = &ts->prg[i];
199 break;
202 if(!p)
203 return;
205 if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
206 return;
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
214 * \param pid : - pid
215 * \return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
216 * 0 otherwise
218 static int discard_pid(MpegTSContext *ts, unsigned int pid)
220 int i, j, k;
221 int used = 0, discarded = 0;
222 struct Program *p;
223 for(i=0; i<ts->nb_prg; i++) {
224 p = &ts->prg[i];
225 for(j=0; j<p->nb_pids; j++) {
226 if(p->pids[j] != pid)
227 continue;
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)
232 discarded++;
233 else
234 used++;
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;
251 int len;
253 if (is_start) {
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;
258 } else {
259 if (tss->end_of_section_reached)
260 return;
261 len = 4096 - tss->section_index;
262 if (buf_size < len)
263 len = buf_size;
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;
271 if (len > 4096)
272 return;
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,
287 int check_crc)
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])
296 return NULL;
297 filter = av_mallocz(sizeof(MpegTSFilter));
298 if (!filter)
299 return NULL;
300 ts->pids[pid] = filter;
301 filter->type = MPEGTS_SECTION;
302 filter->pid = pid;
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) {
310 av_free(filter);
311 return NULL;
313 return filter;
316 static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
317 PESCallback *pes_cb,
318 void *opaque)
320 MpegTSFilter *filter;
321 MpegTSPESFilter *pes;
323 if (pid >= NB_PID_MAX || ts->pids[pid])
324 return NULL;
325 filter = av_mallocz(sizeof(MpegTSFilter));
326 if (!filter)
327 return NULL;
328 ts->pids[pid] = filter;
329 filter->type = MPEGTS_PES;
330 filter->pid = pid;
331 filter->last_cc = -1;
332 pes = &filter->u.pes_filter;
333 pes->pes_cb = pes_cb;
334 pes->opaque = opaque;
335 return filter;
338 static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
340 int pid;
342 pid = filter->pid;
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);
355 av_free(filter);
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];
361 int i;
362 int x=0;
363 int best_score=0;
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)){
369 stat[x]++;
370 if(stat[x] > best_score){
371 best_score= stat[x];
372 if(index) *index= x;
376 x++;
377 if(x == packet_size) x= 0;
380 return best_score;
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))
389 return -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;
399 else return -1;
402 typedef struct SectionHeader {
403 uint8_t tid;
404 uint16_t id;
405 uint8_t version;
406 uint8_t sec_num;
407 uint8_t last_sec_num;
408 } SectionHeader;
410 static inline int get8(const uint8_t **pp, const uint8_t *p_end)
412 const uint8_t *p;
413 int c;
415 p = *pp;
416 if (p >= p_end)
417 return -1;
418 c = *p++;
419 *pp = p;
420 return c;
423 static inline int get16(const uint8_t **pp, const uint8_t *p_end)
425 const uint8_t *p;
426 int c;
428 p = *pp;
429 if ((p + 1) >= p_end)
430 return -1;
431 c = AV_RB16(p);
432 p += 2;
433 *pp = p;
434 return c;
437 /* read and allocate a DVB string preceeded by its length */
438 static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
440 int len;
441 const uint8_t *p;
442 char *str;
444 p = *pp;
445 len = get8(&p, p_end);
446 if (len < 0)
447 return NULL;
448 if ((p + len) > p_end)
449 return NULL;
450 str = av_malloc(len + 1);
451 if (!str)
452 return NULL;
453 memcpy(str, p, len);
454 str[len] = '\0';
455 p += len;
456 *pp = p;
457 return str;
460 static int parse_section_header(SectionHeader *h,
461 const uint8_t **pp, const uint8_t *p_end)
463 int val;
465 val = get8(pp, p_end);
466 if (val < 0)
467 return -1;
468 h->tid = val;
469 *pp += 2;
470 val = get16(pp, p_end);
471 if (val < 0)
472 return -1;
473 h->id = val;
474 val = get8(pp, p_end);
475 if (val < 0)
476 return -1;
477 h->version = (val >> 1) & 0x1f;
478 val = get8(pp, p_end);
479 if (val < 0)
480 return -1;
481 h->sec_num = val;
482 val = get8(pp, p_end);
483 if (val < 0)
484 return -1;
485 h->last_sec_num = val;
486 return 0;
489 typedef struct {
490 uint32_t stream_type;
491 enum CodecType codec_type;
492 enum CodecID codec_id;
493 } StreamType;
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 },
506 { 0 },
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 },
516 { 0 },
519 /* ATSC ? */
520 static const StreamType MISC_types[] = {
521 { 0x81, CODEC_TYPE_AUDIO, CODEC_ID_AC3 },
522 { 0x8a, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
523 { 0 },
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 },
529 { 0 },
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 */
539 { 0 },
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;
549 return;
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);
558 st->priv_data = pes;
559 st->codec->codec_type = CODEC_TYPE_DATA;
560 st->codec->codec_id = CODEC_ID_NONE;
561 st->need_parsing = AVSTREAM_PARSE_FULL;
562 pes->st = st;
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
578 AVStream *sub_st;
579 // priv_data cannot be shared between streams
580 PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
581 if (!sub_pes)
582 return AVERROR(ENOMEM);
583 memcpy(sub_pes, pes, sizeof(*sub_pes));
585 sub_st = av_new_stream(pes->stream, pes->pid);
586 if (!sub_st) {
587 av_free(sub_pes);
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);
602 return 0;
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;
610 return pts;
613 static void new_pes_packet(PESContext *pes, AVPacket *pkt)
615 av_init_packet(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;
625 else
626 pkt->stream_index = pes->st->index;
627 pkt->pts = pes->pts;
628 pkt->dts = pes->dts;
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;
635 pes->buffer = NULL;
636 pes->data_index = 0;
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,
642 int64_t pos)
644 PESContext *pes = filter->u.pes_filter.opaque;
645 MpegTSContext *ts = pes->ts;
646 const uint8_t *p;
647 int len, code;
649 if(!ts->pkt)
650 return 0;
652 if (is_start) {
653 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
654 new_pes_packet(pes, ts->pkt);
655 ts->stop_parse = 1;
657 pes->state = MPEGTS_HEADER;
658 pes->data_index = 0;
659 pes->ts_packet_pos = pos;
661 p = buf;
662 while (buf_size > 0) {
663 switch(pes->state) {
664 case MPEGTS_HEADER:
665 len = PES_START_SIZE - pes->data_index;
666 if (len > buf_size)
667 len = buf_size;
668 memcpy(pes->header + pes->data_index, p, len);
669 pes->data_index += len;
670 p += len;
671 buf_size -= len;
672 if (pes->data_index == PES_START_SIZE) {
673 /* we got all the PES or section header. We can now
674 decide */
675 #if 0
676 av_hex_dump_log(pes->stream, AV_LOG_DEBUG, pes->header, pes->data_index);
677 #endif
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 */
687 goto skip;
689 /* stream not present in PMT */
690 if (!pes->st) {
691 pes->st = av_new_stream(ts->stream, pes->pid);
692 if (!pes->st)
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
699 unbounded */
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);
705 if (!pes->buffer)
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;
718 } else {
719 pes->state = MPEGTS_PAYLOAD;
720 pes->data_index = 0;
722 } else {
723 /* otherwise, it should be a table */
724 /* skip packet */
725 skip:
726 pes->state = MPEGTS_SKIP;
727 continue;
730 break;
731 /**********************************************/
732 /* PES packing parsing */
733 case MPEGTS_PESHEADER:
734 len = PES_HEADER_SIZE - pes->data_index;
735 if (len < 0)
736 return -1;
737 if (len > buf_size)
738 len = buf_size;
739 memcpy(pes->header + pes->data_index, p, len);
740 pes->data_index += len;
741 p += len;
742 buf_size -= len;
743 if (pes->data_index == PES_HEADER_SIZE) {
744 pes->pes_header_size = pes->header[8] + 9;
745 pes->state = MPEGTS_PESHEADER_FILL;
747 break;
748 case MPEGTS_PESHEADER_FILL:
749 len = pes->pes_header_size - pes->data_index;
750 if (len < 0)
751 return -1;
752 if (len > buf_size)
753 len = buf_size;
754 memcpy(pes->header + pes->data_index, p, len);
755 pes->data_index += len;
756 p += len;
757 buf_size -= len;
758 if (pes->data_index == pes->pes_header_size) {
759 const uint8_t *r;
760 unsigned int flags, pes_ext, skip;
762 flags = pes->header[7];
763 r = pes->header + 9;
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);
768 r += 5;
769 } else if ((flags & 0xc0) == 0xc0) {
770 pes->pts = get_pts(r);
771 r += 5;
772 pes->dts = get_pts(r);
773 r += 5;
775 pes->extended_stream_id = -1;
776 if (flags & 0x01) { /* PES extension */
777 pes_ext = *r++;
778 /* Skip PES private data, program packet sequence counter and P-STD buffer */
779 skip = (pes_ext >> 4) & 0xb;
780 skip += skip & 0x9;
781 r += skip;
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;
792 pes->data_index = 0;
794 break;
795 case 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);
801 if (!pes->buffer)
802 return AVERROR(ENOMEM);
803 ts->stop_parse = 1;
805 memcpy(pes->buffer+pes->data_index, p, buf_size);
806 pes->data_index += buf_size;
808 buf_size = 0;
809 break;
810 case MPEGTS_SKIP:
811 buf_size = 0;
812 break;
816 return 0;
819 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
821 MpegTSFilter *tss;
822 PESContext *pes;
824 /* if no pid found, then add a pid context */
825 pes = av_mallocz(sizeof(PESContext));
826 if (!pes)
827 return 0;
828 pes->ts = ts;
829 pes->stream = ts->stream;
830 pes->pid = pid;
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);
836 if (!tss) {
837 av_free(pes);
838 return 0;
840 return 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;
847 PESContext *pes;
848 AVStream *st;
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;
853 char language[4];
854 uint32_t prog_reg_desc = 0; /* registration descriptor */
856 #ifdef DEBUG
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);
859 #endif
861 p_end = section + section_len - 4;
862 p = section;
863 if (parse_section_header(h, &p, p_end) < 0)
864 return;
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)
870 return;
872 clear_program(ts, h->id);
873 pcr_pid = get16(&p, p_end) & 0x1fff;
874 if (pcr_pid < 0)
875 return;
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)
882 return;
883 while(program_info_length >= 2) {
884 uint8_t tag, len;
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
889 break;
890 program_info_length -= len + 2;
891 if(tag == 0x05 && len >= 4) { // registration descriptor
892 prog_reg_desc = bytestream_get_le32(&p);
893 len -= 4;
895 p += len;
897 p += program_info_length;
898 if (p >= p_end)
899 return;
901 // stop parsing after pmt, we found header
902 if (!ts->stream->nb_streams)
903 ts->stop_parse = 1;
905 for(;;) {
906 st = 0;
907 stream_type = get8(&p, p_end);
908 if (stream_type < 0)
909 break;
910 pid = get16(&p, p_end) & 0x1fff;
911 if (pid < 0)
912 break;
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;
917 st = pes->st;
918 } else {
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);
921 if (pes)
922 st = av_new_stream(pes->stream, pes->pid);
925 if (!st)
926 return;
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)
937 break;
938 desc_list_end = p + desc_list_len;
939 if (desc_list_end > p_end)
940 break;
941 for(;;) {
942 desc_tag = get8(&p, desc_list_end);
943 if (desc_tag < 0)
944 break;
945 desc_len = get8(&p, desc_list_end);
946 if (desc_len < 0)
947 break;
948 desc_end = p + desc_len;
949 if (desc_end > desc_list_end)
950 break;
952 dprintf(ts->stream, "tag: 0x%02x len=%d\n",
953 desc_tag, desc_len);
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);
959 switch(desc_tag) {
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);
964 language[3] = 0;
965 av_metadata_set(&st->metadata, "language", language);
966 break;
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);
971 language[3] = 0;
972 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);
977 break;
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);
982 language[3] = 0;
983 av_metadata_set(&st->metadata, "language", language);
984 break;
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);
991 break;
992 default:
993 break;
995 p = desc_end;
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;
1002 p = desc_list_end;
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;
1013 int sid, pmt_pid;
1015 #ifdef DEBUG
1016 dprintf(ts->stream, "PAT:\n");
1017 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
1018 #endif
1019 p_end = section + section_len - 4;
1020 p = section;
1021 if (parse_section_header(h, &p, p_end) < 0)
1022 return;
1023 if (h->tid != PAT_TID)
1024 return;
1026 clear_programs(ts);
1027 for(;;) {
1028 sid = get16(&p, p_end);
1029 if (sid < 0)
1030 break;
1031 pmt_pid = get16(&p, p_end) & 0x1fff;
1032 if (pmt_pid < 0)
1033 break;
1035 dprintf(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
1037 if (sid == 0x0000) {
1038 /* NIT info */
1039 } else {
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;
1057 #ifdef DEBUG
1058 dprintf(ts->stream, "SDT:\n");
1059 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
1060 #endif
1062 p_end = section + section_len - 4;
1063 p = section;
1064 if (parse_section_header(h, &p, p_end) < 0)
1065 return;
1066 if (h->tid != SDT_TID)
1067 return;
1068 onid = get16(&p, p_end);
1069 if (onid < 0)
1070 return;
1071 val = get8(&p, p_end);
1072 if (val < 0)
1073 return;
1074 for(;;) {
1075 sid = get16(&p, p_end);
1076 if (sid < 0)
1077 break;
1078 val = get8(&p, p_end);
1079 if (val < 0)
1080 break;
1081 desc_list_len = get16(&p, p_end) & 0xfff;
1082 if (desc_list_len < 0)
1083 break;
1084 desc_list_end = p + desc_list_len;
1085 if (desc_list_end > p_end)
1086 break;
1087 for(;;) {
1088 desc_tag = get8(&p, desc_list_end);
1089 if (desc_tag < 0)
1090 break;
1091 desc_len = get8(&p, desc_list_end);
1092 desc_end = p + desc_len;
1093 if (desc_end > desc_list_end)
1094 break;
1096 dprintf(ts->stream, "tag: 0x%02x len=%d\n",
1097 desc_tag, desc_len);
1099 switch(desc_tag) {
1100 case 0x48:
1101 service_type = get8(&p, p_end);
1102 if (service_type < 0)
1103 break;
1104 provider_name = getstr8(&p, p_end);
1105 if (!provider_name)
1106 break;
1107 name = getstr8(&p, p_end);
1108 if (name) {
1109 AVProgram *program = av_new_program(ts->stream, sid);
1110 if(program) {
1111 av_metadata_set(&program->metadata, "name", name);
1112 av_metadata_set(&program->metadata, "provider_name", provider_name);
1115 av_free(name);
1116 av_free(provider_name);
1117 break;
1118 default:
1119 break;
1121 p = desc_end;
1123 p = desc_list_end;
1127 /* handle one TS packet */
1128 static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
1130 AVFormatContext *s = ts->stream;
1131 MpegTSFilter *tss;
1132 int len, pid, cc, cc_ok, afc, is_start;
1133 const uint8_t *p, *p_end;
1134 int64_t pos;
1136 pid = AV_RB16(packet + 1) & 0x1fff;
1137 if(pid && discard_pid(ts, pid))
1138 return 0;
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];
1145 if (!tss)
1146 return 0;
1148 /* continuity check (currently not used) */
1149 cc = (packet[3] & 0xf);
1150 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
1151 tss->last_cc = cc;
1153 /* skip adaptation field */
1154 afc = (packet[3] >> 4) & 3;
1155 p = packet + 4;
1156 if (afc == 0) /* reserved value */
1157 return 0;
1158 if (afc == 2) /* adaptation field only */
1159 return 0;
1160 if (afc == 3) {
1161 /* skip adapation field */
1162 p += p[0] + 1;
1164 /* if past the end of packet, ignore */
1165 p_end = packet + TS_PACKET_SIZE;
1166 if (p >= p_end)
1167 return 0;
1169 pos = url_ftell(ts->stream->pb);
1170 ts->pos47= pos % ts->raw_packet_size;
1172 if (tss->type == MPEGTS_SECTION) {
1173 if (is_start) {
1174 /* pointer field present */
1175 len = *p++;
1176 if (p + len > p_end)
1177 return 0;
1178 if (len && cc_ok) {
1179 /* write remaining section bytes */
1180 write_section_data(s, tss,
1181 p, len, 0);
1182 /* check whether filter has been closed */
1183 if (!ts->pids[pid])
1184 return 0;
1186 p += len;
1187 if (p < p_end) {
1188 write_section_data(s, tss,
1189 p, p_end - p, 1);
1191 } else {
1192 if (cc_ok) {
1193 write_section_data(s, tss,
1194 p, p_end - p, 0);
1197 } else {
1198 int ret;
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)
1202 return ret;
1205 return 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;
1213 int c, i;
1215 for(i = 0;i < MAX_RESYNC_SIZE; i++) {
1216 c = url_fgetc(pb);
1217 if (c < 0)
1218 return -1;
1219 if (c == 0x47) {
1220 url_fseek(pb, -1, SEEK_CUR);
1221 return 0;
1224 av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
1225 /* no sync found */
1226 return -1;
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;
1233 int skip, len;
1235 for(;;) {
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);
1245 else
1246 continue;
1247 } else {
1248 skip = raw_packet_size - TS_PACKET_SIZE;
1249 if (skip > 0)
1250 url_fskip(pb, skip);
1251 break;
1254 return 0;
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;
1263 ts->stop_parse = 0;
1264 packet_num = 0;
1265 for(;;) {
1266 if (ts->stop_parse>0)
1267 break;
1268 packet_num++;
1269 if (nb_packets != 0 && packet_num >= nb_packets)
1270 break;
1271 ret = read_packet(s, packet, ts->raw_packet_size);
1272 if (ret != 0)
1273 return ret;
1274 ret = handle_packet(ts, packet);
1275 if (ret != 0)
1276 return ret;
1278 return 0;
1281 static int mpegts_probe(AVProbeData *p)
1283 #if 1
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)
1290 return -1;
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;
1301 else return -1;
1302 #else
1303 /* only use the extension for safer guess */
1304 if (match_ext(p->filename, "ts"))
1305 return AVPROBE_SCORE_MAX;
1306 else
1307 return 0;
1308 #endif
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;
1317 const uint8_t *p;
1318 unsigned int v;
1320 afc = (packet[3] >> 4) & 3;
1321 if (afc <= 1)
1322 return -1;
1323 p = packet + 4;
1324 len = p[0];
1325 p++;
1326 if (len == 0)
1327 return -1;
1328 flags = *p++;
1329 len--;
1330 if (!(flags & 0x10))
1331 return -1;
1332 if (len < 6)
1333 return -1;
1334 v = AV_RB32(p);
1335 *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
1336 *ppcr_low = ((p[4] & 1) << 8) | p[5];
1337 return 0;
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];
1346 int len;
1347 int64_t pos;
1349 if (ap) {
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");
1353 return -1;
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))
1361 goto fail;
1362 ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
1363 if (ts->raw_packet_size <= 0)
1364 goto fail;
1365 ts->stream = s;
1366 ts->auto_guess = 0;
1368 if (s->iformat == &mpegts_demuxer) {
1369 /* normal demux */
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 */
1381 ts->auto_guess = 1;
1383 dprintf(ts->stream, "tuning done\n");
1385 s->ctx_flags |= AVFMTCTX_NOHEADER;
1386 } else {
1387 AVStream *st;
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);
1396 if (!st)
1397 goto fail;
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 */
1403 pcr_pid = -1;
1404 nb_pcrs = 0;
1405 nb_packets = 0;
1406 for(;;) {
1407 ret = read_packet(s, packet, ts->raw_packet_size);
1408 if (ret < 0)
1409 return -1;
1410 pid = AV_RB16(packet + 1) & 0x1fff;
1411 if ((pcr_pid == -1 || pcr_pid == pid) &&
1412 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
1413 pcr_pid = pid;
1414 packet_count[nb_pcrs] = nb_packets;
1415 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
1416 nb_pcrs++;
1417 if (nb_pcrs >= 2)
1418 break;
1420 nb_packets++;
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;
1430 #if 0
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);
1433 #endif
1436 url_fseek(pb, pos, SEEK_SET);
1437 return 0;
1438 fail:
1439 return -1;
1442 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
1444 static int mpegts_raw_read_packet(AVFormatContext *s,
1445 AVPacket *pkt)
1447 MpegTSContext *ts = s->priv_data;
1448 int ret, i;
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);
1457 if (ret < 0) {
1458 av_free_packet(pkt);
1459 return ret;
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)) /
1472 (i + 1);
1473 break;
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;
1485 return 0;
1488 static int mpegts_read_packet(AVFormatContext *s,
1489 AVPacket *pkt)
1491 MpegTSContext *ts = s->priv_data;
1492 int ret, i;
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 */
1506 ts->pkt = pkt;
1507 ret = handle_packets(ts, 0);
1508 if (ret < 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;
1516 ret = 0;
1517 break;
1523 ts->last_pos = url_ftell(s->pb);
1525 return ret;
1528 static int mpegts_read_close(AVFormatContext *s)
1530 MpegTSContext *ts = s->priv_data;
1531 int i;
1533 clear_programs(ts);
1535 for(i=0;i<NB_PID_MAX;i++)
1536 if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
1538 return 0;
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;
1550 if (find_next) {
1551 for(;;) {
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(&timestamp, &pcr_l, buf) == 0) {
1557 break;
1559 pos += ts->raw_packet_size;
1561 } else {
1562 for(;;) {
1563 pos -= ts->raw_packet_size;
1564 if (pos < 0)
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(&timestamp, &pcr_l, buf) == 0) {
1571 break;
1575 *ppos = pos;
1577 return timestamp;
1580 #ifdef USE_SYNCPOINT_SEARCH
1582 static int read_seek2(AVFormatContext *s,
1583 int stream_index,
1584 int64_t min_ts,
1585 int64_t target_ts,
1586 int64_t max_ts,
1587 int flags)
1589 int64_t pos;
1591 int64_t ts_ret, ts_adj;
1592 int stream_index_gen_search;
1593 AVStream *st;
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
1604 pos = target_ts;
1605 } else {
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);
1611 return -1;
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,
1617 st->time_base.den,
1618 AV_TIME_BASE * (int64_t)st->time_base.num);
1619 } else {
1620 ts_adj = target_ts;
1621 stream_index_gen_search = stream_index;
1623 pos = av_gen_search(s, stream_index_gen_search, ts_adj,
1624 0, INT64_MAX, -1,
1625 AV_NOPTS_VALUE,
1626 AV_NOPTS_VALUE,
1627 flags, &ts_ret, mpegts_get_pcr);
1628 if (pos < 0) {
1629 ff_restore_parser_state(s, backup);
1630 return -1;
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,
1637 flags) < 0) {
1638 ff_restore_parser_state(s, backup);
1639 return -1;
1642 ff_free_parser_state(s, backup);
1643 return 0;
1646 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
1648 int ret;
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);
1652 if (ret < 0)
1653 // for compatibility reasons, seek to the best-fitting timestamp
1654 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
1655 } else {
1656 ret = read_seek2(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
1657 if (ret < 0)
1658 // for compatibility reasons, seek to the best-fitting timestamp
1659 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
1661 return ret;
1664 #else
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];
1669 int64_t pos;
1671 if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
1672 return -1;
1674 pos= url_ftell(s->pb);
1676 for(;;) {
1677 url_fseek(s->pb, pos, SEEK_SET);
1678 if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1679 return -1;
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);
1686 return 0;
1689 #endif
1691 /**************************************************************/
1692 /* parsing functions - called from other demuxers such as RTP */
1694 MpegTSContext *mpegts_parse_open(AVFormatContext *s)
1696 MpegTSContext *ts;
1698 ts = av_mallocz(sizeof(MpegTSContext));
1699 if (!ts)
1700 return NULL;
1701 /* no stream case, currently used by RTP */
1702 ts->raw_packet_size = TS_PACKET_SIZE;
1703 ts->stream = s;
1704 ts->auto_guess = 1;
1705 return ts;
1708 /* return the consumed length if a packet was output, or -1 if no
1709 packet is output */
1710 int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
1711 const uint8_t *buf, int len)
1713 int len1;
1715 len1 = len;
1716 ts->pkt = pkt;
1717 ts->stop_parse = 0;
1718 for(;;) {
1719 if (ts->stop_parse>0)
1720 break;
1721 if (len < TS_PACKET_SIZE)
1722 return -1;
1723 if (buf[0] != 0x47) {
1724 buf++;
1725 len--;
1726 } else {
1727 handle_packet(ts, buf);
1728 buf += TS_PACKET_SIZE;
1729 len -= TS_PACKET_SIZE;
1732 return len1 - len;
1735 void mpegts_parse_close(MpegTSContext *ts)
1737 int i;
1739 for(i=0;i<NB_PID_MAX;i++)
1740 av_free(ts->pids[i]);
1741 av_free(ts);
1744 AVInputFormat mpegts_demuxer = {
1745 "mpegts",
1746 NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
1747 sizeof(MpegTSContext),
1748 mpegts_probe,
1749 mpegts_read_header,
1750 mpegts_read_packet,
1751 mpegts_read_close,
1752 read_seek,
1753 mpegts_get_pcr,
1754 .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
1755 #ifdef USE_SYNCPOINT_SEARCH
1756 .read_seek2 = read_seek2,
1757 #endif
1760 AVInputFormat mpegtsraw_demuxer = {
1761 "mpegtsraw",
1762 NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
1763 sizeof(MpegTSContext),
1764 NULL,
1765 mpegts_read_header,
1766 mpegts_raw_read_packet,
1767 mpegts_read_close,
1768 read_seek,
1769 mpegts_get_pcr,
1770 .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
1771 #ifdef USE_SYNCPOINT_SEARCH
1772 .read_seek2 = read_seek2,
1773 #endif