OSDN Git Service

ffv1: Fixed size given to init_get_bits() in decoder.
[coroid/libav_saccubus.git] / libavformat / mpegts.c
1 /*
2  * MPEG2 transport stream (aka DVB) demuxer
3  * Copyright (c) 2002-2003 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 //#define USE_SYNCPOINT_SEARCH
23
24 #include "libavutil/crc.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/log.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/opt.h"
30 #include "libavcodec/bytestream.h"
31 #include "avformat.h"
32 #include "mpegts.h"
33 #include "internal.h"
34 #include "avio_internal.h"
35 #include "seek.h"
36 #include "mpeg.h"
37 #include "isom.h"
38
39 /* maximum size in which we look for synchronisation if
40    synchronisation is lost */
41 #define MAX_RESYNC_SIZE 65536
42
43 #define MAX_PES_PAYLOAD 200*1024
44
45 enum MpegTSFilterType {
46     MPEGTS_PES,
47     MPEGTS_SECTION,
48 };
49
50 typedef struct MpegTSFilter MpegTSFilter;
51
52 typedef int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos);
53
54 typedef struct MpegTSPESFilter {
55     PESCallback *pes_cb;
56     void *opaque;
57 } MpegTSPESFilter;
58
59 typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
60
61 typedef void SetServiceCallback(void *opaque, int ret);
62
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;
72
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;
81 };
82
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];
88 };
89
90 struct MpegTSContext {
91     const AVClass *class;
92     /* user data */
93     AVFormatContext *stream;
94     /** raw packet size, including FEC if present            */
95     int raw_packet_size;
96
97     int pos47;
98
99     /** if true, all pids are analyzed to find streams       */
100     int auto_guess;
101
102     /** compute exact PCR for each transport stream packet   */
103     int mpeg2ts_compute_pcr;
104
105     int64_t cur_pcr;    /**< used to estimate the exact PCR  */
106     int pcr_incr;       /**< used to estimate the exact PCR  */
107
108     /* data needed to handle file based ts */
109     /** stop parsing loop                                    */
110     int stop_parse;
111     /** packet containing Audio/Video data                   */
112     AVPacket *pkt;
113     /** to detect seek                                       */
114     int64_t last_pos;
115
116     /******************************************/
117     /* private mpegts data */
118     /* scan context */
119     /** structure to keep track of Program->pids mapping     */
120     unsigned int nb_prg;
121     struct Program *prg;
122
123
124     /** filters for various streams specified by PMT + for the PAT and PMT */
125     MpegTSFilter *pids[NB_PID_MAX];
126 };
127
128 static const AVOption options[] = {
129     {"compute_pcr", "Compute exact PCR for each transport stream packet.", offsetof(MpegTSContext, mpeg2ts_compute_pcr), FF_OPT_TYPE_INT,
130      {.dbl = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
131     { NULL },
132 };
133
134 static const AVClass mpegtsraw_class = {
135     .class_name = "mpegtsraw demuxer",
136     .item_name  = av_default_item_name,
137     .option     = options,
138     .version    = LIBAVUTIL_VERSION_INT,
139 };
140
141 /* TS stream handling */
142
143 enum MpegTSState {
144     MPEGTS_HEADER = 0,
145     MPEGTS_PESHEADER,
146     MPEGTS_PESHEADER_FILL,
147     MPEGTS_PAYLOAD,
148     MPEGTS_SKIP,
149 };
150
151 /* enough for PES header + length */
152 #define PES_START_SIZE  6
153 #define PES_HEADER_SIZE 9
154 #define MAX_PES_HEADER_SIZE (9 + 255)
155
156 typedef struct PESContext {
157     int pid;
158     int pcr_pid; /**< if -1 then all packets containing PCR are considered */
159     int stream_type;
160     MpegTSContext *ts;
161     AVFormatContext *stream;
162     AVStream *st;
163     AVStream *sub_st; /**< stream for the embedded AC3 stream in HDMV TrueHD */
164     enum MpegTSState state;
165     /* used to get the format */
166     int data_index;
167     int flags; /**< copied to the AVPacket flags */
168     int total_size;
169     int pes_header_size;
170     int extended_stream_id;
171     int64_t pts, dts;
172     int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
173     uint8_t header[MAX_PES_HEADER_SIZE];
174     uint8_t *buffer;
175 } PESContext;
176
177 extern AVInputFormat ff_mpegts_demuxer;
178
179 static void clear_program(MpegTSContext *ts, unsigned int programid)
180 {
181     int i;
182
183     for(i=0; i<ts->nb_prg; i++)
184         if(ts->prg[i].id == programid)
185             ts->prg[i].nb_pids = 0;
186 }
187
188 static void clear_programs(MpegTSContext *ts)
189 {
190     av_freep(&ts->prg);
191     ts->nb_prg=0;
192 }
193
194 static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
195 {
196     struct Program *p;
197     void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(struct Program));
198     if(!tmp)
199         return;
200     ts->prg = tmp;
201     p = &ts->prg[ts->nb_prg];
202     p->id = programid;
203     p->nb_pids = 0;
204     ts->nb_prg++;
205 }
206
207 static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
208 {
209     int i;
210     struct Program *p = NULL;
211     for(i=0; i<ts->nb_prg; i++) {
212         if(ts->prg[i].id == programid) {
213             p = &ts->prg[i];
214             break;
215         }
216     }
217     if(!p)
218         return;
219
220     if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
221         return;
222     p->pids[p->nb_pids++] = pid;
223 }
224
225 /**
226  * @brief discard_pid() decides if the pid is to be discarded according
227  *                      to caller's programs selection
228  * @param ts    : - TS context
229  * @param pid   : - pid
230  * @return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
231  *         0 otherwise
232  */
233 static int discard_pid(MpegTSContext *ts, unsigned int pid)
234 {
235     int i, j, k;
236     int used = 0, discarded = 0;
237     struct Program *p;
238     for(i=0; i<ts->nb_prg; i++) {
239         p = &ts->prg[i];
240         for(j=0; j<p->nb_pids; j++) {
241             if(p->pids[j] != pid)
242                 continue;
243             //is program with id p->id set to be discarded?
244             for(k=0; k<ts->stream->nb_programs; k++) {
245                 if(ts->stream->programs[k]->id == p->id) {
246                     if(ts->stream->programs[k]->discard == AVDISCARD_ALL)
247                         discarded++;
248                     else
249                         used++;
250                 }
251             }
252         }
253     }
254
255     return !used && discarded;
256 }
257
258 /**
259  *  Assemble PES packets out of TS packets, and then call the "section_cb"
260  *  function when they are complete.
261  */
262 static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
263                                const uint8_t *buf, int buf_size, int is_start)
264 {
265     MpegTSSectionFilter *tss = &tss1->u.section_filter;
266     int len;
267
268     if (is_start) {
269         memcpy(tss->section_buf, buf, buf_size);
270         tss->section_index = buf_size;
271         tss->section_h_size = -1;
272         tss->end_of_section_reached = 0;
273     } else {
274         if (tss->end_of_section_reached)
275             return;
276         len = 4096 - tss->section_index;
277         if (buf_size < len)
278             len = buf_size;
279         memcpy(tss->section_buf + tss->section_index, buf, len);
280         tss->section_index += len;
281     }
282
283     /* compute section length if possible */
284     if (tss->section_h_size == -1 && tss->section_index >= 3) {
285         len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
286         if (len > 4096)
287             return;
288         tss->section_h_size = len;
289     }
290
291     if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
292         tss->end_of_section_reached = 1;
293         if (!tss->check_crc ||
294             av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
295                    tss->section_buf, tss->section_h_size) == 0)
296             tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
297     }
298 }
299
300 static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
301                                          SectionCallback *section_cb, void *opaque,
302                                          int check_crc)
303
304 {
305     MpegTSFilter *filter;
306     MpegTSSectionFilter *sec;
307
308     av_dlog(ts->stream, "Filter: pid=0x%x\n", pid);
309
310     if (pid >= NB_PID_MAX || ts->pids[pid])
311         return NULL;
312     filter = av_mallocz(sizeof(MpegTSFilter));
313     if (!filter)
314         return NULL;
315     ts->pids[pid] = filter;
316     filter->type = MPEGTS_SECTION;
317     filter->pid = pid;
318     filter->last_cc = -1;
319     sec = &filter->u.section_filter;
320     sec->section_cb = section_cb;
321     sec->opaque = opaque;
322     sec->section_buf = av_malloc(MAX_SECTION_SIZE);
323     sec->check_crc = check_crc;
324     if (!sec->section_buf) {
325         av_free(filter);
326         return NULL;
327     }
328     return filter;
329 }
330
331 static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
332                                      PESCallback *pes_cb,
333                                      void *opaque)
334 {
335     MpegTSFilter *filter;
336     MpegTSPESFilter *pes;
337
338     if (pid >= NB_PID_MAX || ts->pids[pid])
339         return NULL;
340     filter = av_mallocz(sizeof(MpegTSFilter));
341     if (!filter)
342         return NULL;
343     ts->pids[pid] = filter;
344     filter->type = MPEGTS_PES;
345     filter->pid = pid;
346     filter->last_cc = -1;
347     pes = &filter->u.pes_filter;
348     pes->pes_cb = pes_cb;
349     pes->opaque = opaque;
350     return filter;
351 }
352
353 static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
354 {
355     int pid;
356
357     pid = filter->pid;
358     if (filter->type == MPEGTS_SECTION)
359         av_freep(&filter->u.section_filter.section_buf);
360     else if (filter->type == MPEGTS_PES) {
361         PESContext *pes = filter->u.pes_filter.opaque;
362         av_freep(&pes->buffer);
363         /* referenced private data will be freed later in
364          * av_close_input_stream */
365         if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
366             av_freep(&filter->u.pes_filter.opaque);
367         }
368     }
369
370     av_free(filter);
371     ts->pids[pid] = NULL;
372 }
373
374 static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
375     int stat[TS_MAX_PACKET_SIZE];
376     int i;
377     int x=0;
378     int best_score=0;
379
380     memset(stat, 0, packet_size*sizeof(int));
381
382     for(x=i=0; i<size-3; i++){
383         if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && (buf[i+3] & 0x30)){
384             stat[x]++;
385             if(stat[x] > best_score){
386                 best_score= stat[x];
387                 if(index) *index= x;
388             }
389         }
390
391         x++;
392         if(x == packet_size) x= 0;
393     }
394
395     return best_score;
396 }
397
398 /* autodetect fec presence. Must have at least 1024 bytes  */
399 static int get_packet_size(const uint8_t *buf, int size)
400 {
401     int score, fec_score, dvhs_score;
402
403     if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
404         return -1;
405
406     score    = analyze(buf, size, TS_PACKET_SIZE, NULL);
407     dvhs_score    = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
408     fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
409 //    av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
410
411     if     (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
412     else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
413     else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
414     else                       return -1;
415 }
416
417 typedef struct SectionHeader {
418     uint8_t tid;
419     uint16_t id;
420     uint8_t version;
421     uint8_t sec_num;
422     uint8_t last_sec_num;
423 } SectionHeader;
424
425 static inline int get8(const uint8_t **pp, const uint8_t *p_end)
426 {
427     const uint8_t *p;
428     int c;
429
430     p = *pp;
431     if (p >= p_end)
432         return -1;
433     c = *p++;
434     *pp = p;
435     return c;
436 }
437
438 static inline int get16(const uint8_t **pp, const uint8_t *p_end)
439 {
440     const uint8_t *p;
441     int c;
442
443     p = *pp;
444     if ((p + 1) >= p_end)
445         return -1;
446     c = AV_RB16(p);
447     p += 2;
448     *pp = p;
449     return c;
450 }
451
452 /* read and allocate a DVB string preceeded by its length */
453 static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
454 {
455     int len;
456     const uint8_t *p;
457     char *str;
458
459     p = *pp;
460     len = get8(&p, p_end);
461     if (len < 0)
462         return NULL;
463     if ((p + len) > p_end)
464         return NULL;
465     str = av_malloc(len + 1);
466     if (!str)
467         return NULL;
468     memcpy(str, p, len);
469     str[len] = '\0';
470     p += len;
471     *pp = p;
472     return str;
473 }
474
475 static int parse_section_header(SectionHeader *h,
476                                 const uint8_t **pp, const uint8_t *p_end)
477 {
478     int val;
479
480     val = get8(pp, p_end);
481     if (val < 0)
482         return -1;
483     h->tid = val;
484     *pp += 2;
485     val = get16(pp, p_end);
486     if (val < 0)
487         return -1;
488     h->id = val;
489     val = get8(pp, p_end);
490     if (val < 0)
491         return -1;
492     h->version = (val >> 1) & 0x1f;
493     val = get8(pp, p_end);
494     if (val < 0)
495         return -1;
496     h->sec_num = val;
497     val = get8(pp, p_end);
498     if (val < 0)
499         return -1;
500     h->last_sec_num = val;
501     return 0;
502 }
503
504 typedef struct {
505     uint32_t stream_type;
506     enum AVMediaType codec_type;
507     enum CodecID codec_id;
508 } StreamType;
509
510 static const StreamType ISO_types[] = {
511     { 0x01, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
512     { 0x02, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
513     { 0x03, AVMEDIA_TYPE_AUDIO,        CODEC_ID_MP3 },
514     { 0x04, AVMEDIA_TYPE_AUDIO,        CODEC_ID_MP3 },
515     { 0x0f, AVMEDIA_TYPE_AUDIO,        CODEC_ID_AAC },
516     { 0x10, AVMEDIA_TYPE_VIDEO,      CODEC_ID_MPEG4 },
517     { 0x11, AVMEDIA_TYPE_AUDIO,   CODEC_ID_AAC_LATM }, /* LATM syntax */
518     { 0x1b, AVMEDIA_TYPE_VIDEO,       CODEC_ID_H264 },
519     { 0xd1, AVMEDIA_TYPE_VIDEO,      CODEC_ID_DIRAC },
520     { 0xea, AVMEDIA_TYPE_VIDEO,        CODEC_ID_VC1 },
521     { 0 },
522 };
523
524 static const StreamType HDMV_types[] = {
525     { 0x80, AVMEDIA_TYPE_AUDIO, CODEC_ID_PCM_BLURAY },
526     { 0x81, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
527     { 0x82, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
528     { 0x83, AVMEDIA_TYPE_AUDIO, CODEC_ID_TRUEHD },
529     { 0x84, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 },
530     { 0x90, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
531     { 0 },
532 };
533
534 /* ATSC ? */
535 static const StreamType MISC_types[] = {
536     { 0x81, AVMEDIA_TYPE_AUDIO,   CODEC_ID_AC3 },
537     { 0x8a, AVMEDIA_TYPE_AUDIO,   CODEC_ID_DTS },
538     { 0 },
539 };
540
541 static const StreamType REGD_types[] = {
542     { MKTAG('d','r','a','c'), AVMEDIA_TYPE_VIDEO, CODEC_ID_DIRAC },
543     { MKTAG('A','C','-','3'), AVMEDIA_TYPE_AUDIO,   CODEC_ID_AC3 },
544     { MKTAG('B','S','S','D'), AVMEDIA_TYPE_AUDIO, CODEC_ID_S302M },
545     { 0 },
546 };
547
548 /* descriptor present */
549 static const StreamType DESC_types[] = {
550     { 0x6a, AVMEDIA_TYPE_AUDIO,             CODEC_ID_AC3 }, /* AC-3 descriptor */
551     { 0x7a, AVMEDIA_TYPE_AUDIO,            CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
552     { 0x7b, AVMEDIA_TYPE_AUDIO,             CODEC_ID_DTS },
553     { 0x56, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_TELETEXT },
554     { 0x59, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
555     { 0 },
556 };
557
558 static void mpegts_find_stream_type(AVStream *st,
559                                     uint32_t stream_type, const StreamType *types)
560 {
561     for (; types->stream_type; types++) {
562         if (stream_type == types->stream_type) {
563             st->codec->codec_type = types->codec_type;
564             st->codec->codec_id   = types->codec_id;
565             return;
566         }
567     }
568 }
569
570 static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
571                                   uint32_t stream_type, uint32_t prog_reg_desc)
572 {
573     av_set_pts_info(st, 33, 1, 90000);
574     st->priv_data = pes;
575     st->codec->codec_type = AVMEDIA_TYPE_DATA;
576     st->codec->codec_id   = CODEC_ID_NONE;
577     st->need_parsing = AVSTREAM_PARSE_FULL;
578     pes->st = st;
579     pes->stream_type = stream_type;
580
581     av_log(pes->stream, AV_LOG_DEBUG,
582            "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
583            st->index, pes->stream_type, pes->pid, (char*)&prog_reg_desc);
584
585     st->codec->codec_tag = pes->stream_type;
586
587     mpegts_find_stream_type(st, pes->stream_type, ISO_types);
588     if (prog_reg_desc == AV_RL32("HDMV") &&
589         st->codec->codec_id == CODEC_ID_NONE) {
590         mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
591         if (pes->stream_type == 0x83) {
592             // HDMV TrueHD streams also contain an AC3 coded version of the
593             // audio track - add a second stream for this
594             AVStream *sub_st;
595             // priv_data cannot be shared between streams
596             PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
597             if (!sub_pes)
598                 return AVERROR(ENOMEM);
599             memcpy(sub_pes, pes, sizeof(*sub_pes));
600
601             sub_st = av_new_stream(pes->stream, pes->pid);
602             if (!sub_st) {
603                 av_free(sub_pes);
604                 return AVERROR(ENOMEM);
605             }
606
607             av_set_pts_info(sub_st, 33, 1, 90000);
608             sub_st->priv_data = sub_pes;
609             sub_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
610             sub_st->codec->codec_id   = CODEC_ID_AC3;
611             sub_st->need_parsing = AVSTREAM_PARSE_FULL;
612             sub_pes->sub_st = pes->sub_st = sub_st;
613         }
614     }
615     if (st->codec->codec_id == CODEC_ID_NONE)
616         mpegts_find_stream_type(st, pes->stream_type, MISC_types);
617
618     return 0;
619 }
620
621 static void new_pes_packet(PESContext *pes, AVPacket *pkt)
622 {
623     av_init_packet(pkt);
624
625     pkt->destruct = av_destruct_packet;
626     pkt->data = pes->buffer;
627     pkt->size = pes->data_index;
628
629     if(pes->total_size != MAX_PES_PAYLOAD &&
630        pes->pes_header_size + pes->data_index != pes->total_size + 6) {
631         av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n");
632         pes->flags |= AV_PKT_FLAG_CORRUPT;
633     }
634     memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
635
636     // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
637     if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
638         pkt->stream_index = pes->sub_st->index;
639     else
640         pkt->stream_index = pes->st->index;
641     pkt->pts = pes->pts;
642     pkt->dts = pes->dts;
643     /* store position of first TS packet of this PES packet */
644     pkt->pos = pes->ts_packet_pos;
645     pkt->flags = pes->flags;
646
647     /* reset pts values */
648     pes->pts = AV_NOPTS_VALUE;
649     pes->dts = AV_NOPTS_VALUE;
650     pes->buffer = NULL;
651     pes->data_index = 0;
652     pes->flags = 0;
653 }
654
655 /* return non zero if a packet could be constructed */
656 static int mpegts_push_data(MpegTSFilter *filter,
657                             const uint8_t *buf, int buf_size, int is_start,
658                             int64_t pos)
659 {
660     PESContext *pes = filter->u.pes_filter.opaque;
661     MpegTSContext *ts = pes->ts;
662     const uint8_t *p;
663     int len, code;
664
665     if(!ts->pkt)
666         return 0;
667
668     if (is_start) {
669         if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
670             new_pes_packet(pes, ts->pkt);
671             ts->stop_parse = 1;
672         }
673         pes->state = MPEGTS_HEADER;
674         pes->data_index = 0;
675         pes->ts_packet_pos = pos;
676     }
677     p = buf;
678     while (buf_size > 0) {
679         switch(pes->state) {
680         case MPEGTS_HEADER:
681             len = PES_START_SIZE - pes->data_index;
682             if (len > buf_size)
683                 len = buf_size;
684             memcpy(pes->header + pes->data_index, p, len);
685             pes->data_index += len;
686             p += len;
687             buf_size -= len;
688             if (pes->data_index == PES_START_SIZE) {
689                 /* we got all the PES or section header. We can now
690                    decide */
691                 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
692                     pes->header[2] == 0x01) {
693                     /* it must be an mpeg2 PES stream */
694                     code = pes->header[3] | 0x100;
695                     av_dlog(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
696
697                     if ((pes->st && pes->st->discard == AVDISCARD_ALL) ||
698                         code == 0x1be) /* padding_stream */
699                         goto skip;
700
701                     /* stream not present in PMT */
702                     if (!pes->st) {
703                         pes->st = av_new_stream(ts->stream, pes->pid);
704                         if (!pes->st)
705                             return AVERROR(ENOMEM);
706                         mpegts_set_stream_info(pes->st, pes, 0, 0);
707                     }
708
709                     pes->total_size = AV_RB16(pes->header + 4);
710                     /* NOTE: a zero total size means the PES size is
711                        unbounded */
712                     if (!pes->total_size)
713                         pes->total_size = MAX_PES_PAYLOAD;
714
715                     /* allocate pes buffer */
716                     pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
717                     if (!pes->buffer)
718                         return AVERROR(ENOMEM);
719
720                     if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
721                         code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
722                         code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
723                         code != 0x1f8) {                  /* ITU-T Rec. H.222.1 type E stream */
724                         pes->state = MPEGTS_PESHEADER;
725                         if (pes->st->codec->codec_id == CODEC_ID_NONE) {
726                             av_dlog(pes->stream, "pid=%x stream_type=%x probing\n",
727                                     pes->pid, pes->stream_type);
728                             pes->st->codec->codec_id = CODEC_ID_PROBE;
729                         }
730                     } else {
731                         pes->state = MPEGTS_PAYLOAD;
732                         pes->data_index = 0;
733                     }
734                 } else {
735                     /* otherwise, it should be a table */
736                     /* skip packet */
737                 skip:
738                     pes->state = MPEGTS_SKIP;
739                     continue;
740                 }
741             }
742             break;
743             /**********************************************/
744             /* PES packing parsing */
745         case MPEGTS_PESHEADER:
746             len = PES_HEADER_SIZE - pes->data_index;
747             if (len < 0)
748                 return -1;
749             if (len > buf_size)
750                 len = buf_size;
751             memcpy(pes->header + pes->data_index, p, len);
752             pes->data_index += len;
753             p += len;
754             buf_size -= len;
755             if (pes->data_index == PES_HEADER_SIZE) {
756                 pes->pes_header_size = pes->header[8] + 9;
757                 pes->state = MPEGTS_PESHEADER_FILL;
758             }
759             break;
760         case MPEGTS_PESHEADER_FILL:
761             len = pes->pes_header_size - pes->data_index;
762             if (len < 0)
763                 return -1;
764             if (len > buf_size)
765                 len = buf_size;
766             memcpy(pes->header + pes->data_index, p, len);
767             pes->data_index += len;
768             p += len;
769             buf_size -= len;
770             if (pes->data_index == pes->pes_header_size) {
771                 const uint8_t *r;
772                 unsigned int flags, pes_ext, skip;
773
774                 flags = pes->header[7];
775                 r = pes->header + 9;
776                 pes->pts = AV_NOPTS_VALUE;
777                 pes->dts = AV_NOPTS_VALUE;
778                 if ((flags & 0xc0) == 0x80) {
779                     pes->dts = pes->pts = ff_parse_pes_pts(r);
780                     r += 5;
781                 } else if ((flags & 0xc0) == 0xc0) {
782                     pes->pts = ff_parse_pes_pts(r);
783                     r += 5;
784                     pes->dts = ff_parse_pes_pts(r);
785                     r += 5;
786                 }
787                 pes->extended_stream_id = -1;
788                 if (flags & 0x01) { /* PES extension */
789                     pes_ext = *r++;
790                     /* Skip PES private data, program packet sequence counter and P-STD buffer */
791                     skip = (pes_ext >> 4) & 0xb;
792                     skip += skip & 0x9;
793                     r += skip;
794                     if ((pes_ext & 0x41) == 0x01 &&
795                         (r + 2) <= (pes->header + pes->pes_header_size)) {
796                         /* PES extension 2 */
797                         if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
798                             pes->extended_stream_id = r[1];
799                     }
800                 }
801
802                 /* we got the full header. We parse it and get the payload */
803                 pes->state = MPEGTS_PAYLOAD;
804                 pes->data_index = 0;
805             }
806             break;
807         case MPEGTS_PAYLOAD:
808             if (buf_size > 0 && pes->buffer) {
809                 if (pes->data_index > 0 && pes->data_index+buf_size > pes->total_size) {
810                     new_pes_packet(pes, ts->pkt);
811                     pes->total_size = MAX_PES_PAYLOAD;
812                     pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
813                     if (!pes->buffer)
814                         return AVERROR(ENOMEM);
815                     ts->stop_parse = 1;
816                 } else if (pes->data_index == 0 && buf_size > pes->total_size) {
817                     // pes packet size is < ts size packet and pes data is padded with 0xff
818                     // not sure if this is legal in ts but see issue #2392
819                     buf_size = pes->total_size;
820                 }
821                 memcpy(pes->buffer+pes->data_index, p, buf_size);
822                 pes->data_index += buf_size;
823             }
824             buf_size = 0;
825             /* emit complete packets with known packet size
826              * decreases demuxer delay for infrequent packets like subtitles from
827              * a couple of seconds to milliseconds for properly muxed files.
828              * total_size is the number of bytes following pes_packet_length
829              * in the pes header, i.e. not counting the first 6 bytes */
830             if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD &&
831                 pes->pes_header_size + pes->data_index == pes->total_size + 6) {
832                 ts->stop_parse = 1;
833                 new_pes_packet(pes, ts->pkt);
834             }
835             break;
836         case MPEGTS_SKIP:
837             buf_size = 0;
838             break;
839         }
840     }
841
842     return 0;
843 }
844
845 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
846 {
847     MpegTSFilter *tss;
848     PESContext *pes;
849
850     /* if no pid found, then add a pid context */
851     pes = av_mallocz(sizeof(PESContext));
852     if (!pes)
853         return 0;
854     pes->ts = ts;
855     pes->stream = ts->stream;
856     pes->pid = pid;
857     pes->pcr_pid = pcr_pid;
858     pes->state = MPEGTS_SKIP;
859     pes->pts = AV_NOPTS_VALUE;
860     pes->dts = AV_NOPTS_VALUE;
861     tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
862     if (!tss) {
863         av_free(pes);
864         return 0;
865     }
866     return pes;
867 }
868
869 static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
870                          int *es_id, uint8_t **dec_config_descr,
871                          int *dec_config_descr_size)
872 {
873     AVIOContext pb;
874     int tag;
875     unsigned len;
876
877     ffio_init_context(&pb, buf, size, 0, NULL, NULL, NULL, NULL);
878
879     len = ff_mp4_read_descr(s, &pb, &tag);
880     if (tag == MP4IODescrTag) {
881         avio_rb16(&pb); // ID
882         avio_r8(&pb);
883         avio_r8(&pb);
884         avio_r8(&pb);
885         avio_r8(&pb);
886         avio_r8(&pb);
887         len = ff_mp4_read_descr(s, &pb, &tag);
888         if (tag == MP4ESDescrTag) {
889             ff_mp4_parse_es_descr(&pb, es_id);
890             av_dlog(s, "ES_ID %#x\n", *es_id);
891             len = ff_mp4_read_descr(s, &pb, &tag);
892             if (tag == MP4DecConfigDescrTag) {
893                 *dec_config_descr = av_malloc(len);
894                 if (!*dec_config_descr)
895                     return AVERROR(ENOMEM);
896                 *dec_config_descr_size = len;
897                 avio_read(&pb, *dec_config_descr, len);
898             }
899         }
900     }
901     return 0;
902 }
903
904 int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
905                               const uint8_t **pp, const uint8_t *desc_list_end,
906                               int mp4_dec_config_descr_len, int mp4_es_id, int pid,
907                               uint8_t *mp4_dec_config_descr)
908 {
909     const uint8_t *desc_end;
910     int desc_len, desc_tag;
911     char language[252];
912     int i;
913
914     desc_tag = get8(pp, desc_list_end);
915     if (desc_tag < 0)
916         return -1;
917     desc_len = get8(pp, desc_list_end);
918     if (desc_len < 0)
919         return -1;
920     desc_end = *pp + desc_len;
921     if (desc_end > desc_list_end)
922         return -1;
923
924     av_dlog(fc, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
925
926     if (st->codec->codec_id == CODEC_ID_NONE &&
927         stream_type == STREAM_TYPE_PRIVATE_DATA)
928         mpegts_find_stream_type(st, desc_tag, DESC_types);
929
930     switch(desc_tag) {
931     case 0x1F: /* FMC descriptor */
932         get16(pp, desc_end);
933         if (st->codec->codec_id == CODEC_ID_AAC_LATM &&
934             mp4_dec_config_descr_len && mp4_es_id == pid) {
935             AVIOContext pb;
936             ffio_init_context(&pb, mp4_dec_config_descr,
937                           mp4_dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
938             ff_mp4_read_dec_config_descr(fc, st, &pb);
939             if (st->codec->codec_id == CODEC_ID_AAC &&
940                 st->codec->extradata_size > 0)
941                 st->need_parsing = 0;
942         }
943         break;
944     case 0x56: /* DVB teletext descriptor */
945         language[0] = get8(pp, desc_end);
946         language[1] = get8(pp, desc_end);
947         language[2] = get8(pp, desc_end);
948         language[3] = 0;
949         av_dict_set(&st->metadata, "language", language, 0);
950         break;
951     case 0x59: /* subtitling descriptor */
952         language[0] = get8(pp, desc_end);
953         language[1] = get8(pp, desc_end);
954         language[2] = get8(pp, desc_end);
955         language[3] = 0;
956         /* hearing impaired subtitles detection */
957         switch(get8(pp, desc_end)) {
958         case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
959         case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
960         case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
961         case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
962         case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
963         case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
964             st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
965             break;
966         }
967         if (st->codec->extradata) {
968             if (st->codec->extradata_size == 4 && memcmp(st->codec->extradata, *pp, 4))
969                 av_log_ask_for_sample(fc, "DVB sub with multiple IDs\n");
970         } else {
971             st->codec->extradata = av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
972             if (st->codec->extradata) {
973                 st->codec->extradata_size = 4;
974                 memcpy(st->codec->extradata, *pp, 4);
975             }
976         }
977         *pp += 4;
978         av_dict_set(&st->metadata, "language", language, 0);
979         break;
980     case 0x0a: /* ISO 639 language descriptor */
981         for (i = 0; i + 4 <= desc_len; i += 4) {
982             language[i + 0] = get8(pp, desc_end);
983             language[i + 1] = get8(pp, desc_end);
984             language[i + 2] = get8(pp, desc_end);
985             language[i + 3] = ',';
986         switch (get8(pp, desc_end)) {
987             case 0x01: st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS; break;
988             case 0x02: st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; break;
989             case 0x03: st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED; break;
990         }
991         }
992         if (i) {
993             language[i - 1] = 0;
994             av_dict_set(&st->metadata, "language", language, 0);
995         }
996         break;
997     case 0x05: /* registration descriptor */
998         st->codec->codec_tag = bytestream_get_le32(pp);
999         av_dlog(fc, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
1000         if (st->codec->codec_id == CODEC_ID_NONE &&
1001             stream_type == STREAM_TYPE_PRIVATE_DATA)
1002             mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
1003         break;
1004     default:
1005         break;
1006     }
1007     *pp = desc_end;
1008     return 0;
1009 }
1010
1011 static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
1012 {
1013     MpegTSContext *ts = filter->u.section_filter.opaque;
1014     SectionHeader h1, *h = &h1;
1015     PESContext *pes;
1016     AVStream *st;
1017     const uint8_t *p, *p_end, *desc_list_end;
1018     int program_info_length, pcr_pid, pid, stream_type;
1019     int desc_list_len;
1020     uint32_t prog_reg_desc = 0; /* registration descriptor */
1021     uint8_t *mp4_dec_config_descr = NULL;
1022     int mp4_dec_config_descr_len = 0;
1023     int mp4_es_id = 0;
1024
1025     av_dlog(ts->stream, "PMT: len %i\n", section_len);
1026     hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
1027
1028     p_end = section + section_len - 4;
1029     p = section;
1030     if (parse_section_header(h, &p, p_end) < 0)
1031         return;
1032
1033     av_dlog(ts->stream, "sid=0x%x sec_num=%d/%d\n",
1034            h->id, h->sec_num, h->last_sec_num);
1035
1036     if (h->tid != PMT_TID)
1037         return;
1038
1039     clear_program(ts, h->id);
1040     pcr_pid = get16(&p, p_end) & 0x1fff;
1041     if (pcr_pid < 0)
1042         return;
1043     add_pid_to_pmt(ts, h->id, pcr_pid);
1044
1045     av_dlog(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
1046
1047     program_info_length = get16(&p, p_end) & 0xfff;
1048     if (program_info_length < 0)
1049         return;
1050     while(program_info_length >= 2) {
1051         uint8_t tag, len;
1052         tag = get8(&p, p_end);
1053         len = get8(&p, p_end);
1054
1055         av_dlog(ts->stream, "program tag: 0x%02x len=%d\n", tag, len);
1056
1057         if(len > program_info_length - 2)
1058             //something else is broken, exit the program_descriptors_loop
1059             break;
1060         program_info_length -= len + 2;
1061         if (tag == 0x1d) { // IOD descriptor
1062             get8(&p, p_end); // scope
1063             get8(&p, p_end); // label
1064             len -= 2;
1065             mp4_read_iods(ts->stream, p, len, &mp4_es_id,
1066                           &mp4_dec_config_descr, &mp4_dec_config_descr_len);
1067         } else if (tag == 0x05 && len >= 4) { // registration descriptor
1068             prog_reg_desc = bytestream_get_le32(&p);
1069             len -= 4;
1070         }
1071         p += len;
1072     }
1073     p += program_info_length;
1074     if (p >= p_end)
1075         goto out;
1076
1077     // stop parsing after pmt, we found header
1078     if (!ts->stream->nb_streams)
1079         ts->stop_parse = 1;
1080
1081     for(;;) {
1082         st = 0;
1083         stream_type = get8(&p, p_end);
1084         if (stream_type < 0)
1085             break;
1086         pid = get16(&p, p_end) & 0x1fff;
1087         if (pid < 0)
1088             break;
1089
1090         /* now create ffmpeg stream */
1091         if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
1092             pes = ts->pids[pid]->u.pes_filter.opaque;
1093             if (!pes->st)
1094                 pes->st = av_new_stream(pes->stream, pes->pid);
1095             st = pes->st;
1096         } else {
1097             if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
1098             pes = add_pes_stream(ts, pid, pcr_pid);
1099             if (pes)
1100                 st = av_new_stream(pes->stream, pes->pid);
1101         }
1102
1103         if (!st)
1104             goto out;
1105
1106         if (!pes->stream_type)
1107             mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
1108
1109         add_pid_to_pmt(ts, h->id, pid);
1110
1111         ff_program_add_stream_index(ts->stream, h->id, st->index);
1112
1113         desc_list_len = get16(&p, p_end) & 0xfff;
1114         if (desc_list_len < 0)
1115             break;
1116         desc_list_end = p + desc_list_len;
1117         if (desc_list_end > p_end)
1118             break;
1119         for(;;) {
1120             if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, desc_list_end,
1121                 mp4_dec_config_descr_len, mp4_es_id, pid, mp4_dec_config_descr) < 0)
1122                 break;
1123
1124             if (prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) {
1125                 ff_program_add_stream_index(ts->stream, h->id, pes->sub_st->index);
1126                 pes->sub_st->codec->codec_tag = st->codec->codec_tag;
1127             }
1128         }
1129         p = desc_list_end;
1130     }
1131
1132  out:
1133     av_free(mp4_dec_config_descr);
1134 }
1135
1136 static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
1137 {
1138     MpegTSContext *ts = filter->u.section_filter.opaque;
1139     SectionHeader h1, *h = &h1;
1140     const uint8_t *p, *p_end;
1141     int sid, pmt_pid;
1142
1143     av_dlog(ts->stream, "PAT:\n");
1144     hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
1145
1146     p_end = section + section_len - 4;
1147     p = section;
1148     if (parse_section_header(h, &p, p_end) < 0)
1149         return;
1150     if (h->tid != PAT_TID)
1151         return;
1152
1153     clear_programs(ts);
1154     for(;;) {
1155         sid = get16(&p, p_end);
1156         if (sid < 0)
1157             break;
1158         pmt_pid = get16(&p, p_end) & 0x1fff;
1159         if (pmt_pid < 0)
1160             break;
1161
1162         av_dlog(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
1163
1164         if (sid == 0x0000) {
1165             /* NIT info */
1166         } else {
1167             av_new_program(ts->stream, sid);
1168             if (ts->pids[pmt_pid])
1169                 mpegts_close_filter(ts, ts->pids[pmt_pid]);
1170             mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
1171             add_pat_entry(ts, sid);
1172             add_pid_to_pmt(ts, sid, 0); //add pat pid to program
1173             add_pid_to_pmt(ts, sid, pmt_pid);
1174         }
1175     }
1176 }
1177
1178 static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
1179 {
1180     MpegTSContext *ts = filter->u.section_filter.opaque;
1181     SectionHeader h1, *h = &h1;
1182     const uint8_t *p, *p_end, *desc_list_end, *desc_end;
1183     int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
1184     char *name, *provider_name;
1185
1186     av_dlog(ts->stream, "SDT:\n");
1187     hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
1188
1189     p_end = section + section_len - 4;
1190     p = section;
1191     if (parse_section_header(h, &p, p_end) < 0)
1192         return;
1193     if (h->tid != SDT_TID)
1194         return;
1195     onid = get16(&p, p_end);
1196     if (onid < 0)
1197         return;
1198     val = get8(&p, p_end);
1199     if (val < 0)
1200         return;
1201     for(;;) {
1202         sid = get16(&p, p_end);
1203         if (sid < 0)
1204             break;
1205         val = get8(&p, p_end);
1206         if (val < 0)
1207             break;
1208         desc_list_len = get16(&p, p_end) & 0xfff;
1209         if (desc_list_len < 0)
1210             break;
1211         desc_list_end = p + desc_list_len;
1212         if (desc_list_end > p_end)
1213             break;
1214         for(;;) {
1215             desc_tag = get8(&p, desc_list_end);
1216             if (desc_tag < 0)
1217                 break;
1218             desc_len = get8(&p, desc_list_end);
1219             desc_end = p + desc_len;
1220             if (desc_end > desc_list_end)
1221                 break;
1222
1223             av_dlog(ts->stream, "tag: 0x%02x len=%d\n",
1224                    desc_tag, desc_len);
1225
1226             switch(desc_tag) {
1227             case 0x48:
1228                 service_type = get8(&p, p_end);
1229                 if (service_type < 0)
1230                     break;
1231                 provider_name = getstr8(&p, p_end);
1232                 if (!provider_name)
1233                     break;
1234                 name = getstr8(&p, p_end);
1235                 if (name) {
1236                     AVProgram *program = av_new_program(ts->stream, sid);
1237                     if(program) {
1238                         av_dict_set(&program->metadata, "service_name", name, 0);
1239                         av_dict_set(&program->metadata, "service_provider", provider_name, 0);
1240                     }
1241                 }
1242                 av_free(name);
1243                 av_free(provider_name);
1244                 break;
1245             default:
1246                 break;
1247             }
1248             p = desc_end;
1249         }
1250         p = desc_list_end;
1251     }
1252 }
1253
1254 /* handle one TS packet */
1255 static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
1256 {
1257     AVFormatContext *s = ts->stream;
1258     MpegTSFilter *tss;
1259     int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
1260         has_adaptation, has_payload;
1261     const uint8_t *p, *p_end;
1262     int64_t pos;
1263
1264     pid = AV_RB16(packet + 1) & 0x1fff;
1265     if(pid && discard_pid(ts, pid))
1266         return 0;
1267     is_start = packet[1] & 0x40;
1268     tss = ts->pids[pid];
1269     if (ts->auto_guess && tss == NULL && is_start) {
1270         add_pes_stream(ts, pid, -1);
1271         tss = ts->pids[pid];
1272     }
1273     if (!tss)
1274         return 0;
1275
1276     afc = (packet[3] >> 4) & 3;
1277     if (afc == 0) /* reserved value */
1278         return 0;
1279     has_adaptation = afc & 2;
1280     has_payload = afc & 1;
1281     is_discontinuity = has_adaptation
1282                 && packet[4] != 0 /* with length > 0 */
1283                 && (packet[5] & 0x80); /* and discontinuity indicated */
1284
1285     /* continuity check (currently not used) */
1286     cc = (packet[3] & 0xf);
1287     expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
1288     cc_ok = pid == 0x1FFF // null packet PID
1289             || is_discontinuity
1290             || tss->last_cc < 0
1291             || expected_cc == cc;
1292
1293     tss->last_cc = cc;
1294     if (!cc_ok) {
1295         av_log(ts->stream, AV_LOG_WARNING, "Continuity Check Failed\n");
1296         if(tss->type == MPEGTS_PES) {
1297             PESContext *pc = tss->u.pes_filter.opaque;
1298             pc->flags |= AV_PKT_FLAG_CORRUPT;
1299         }
1300     }
1301
1302     if (!has_payload)
1303         return 0;
1304     p = packet + 4;
1305     if (has_adaptation) {
1306         /* skip adapation field */
1307         p += p[0] + 1;
1308     }
1309     /* if past the end of packet, ignore */
1310     p_end = packet + TS_PACKET_SIZE;
1311     if (p >= p_end)
1312         return 0;
1313
1314     pos = avio_tell(ts->stream->pb);
1315     ts->pos47= pos % ts->raw_packet_size;
1316
1317     if (tss->type == MPEGTS_SECTION) {
1318         if (is_start) {
1319             /* pointer field present */
1320             len = *p++;
1321             if (p + len > p_end)
1322                 return 0;
1323             if (len && cc_ok) {
1324                 /* write remaining section bytes */
1325                 write_section_data(s, tss,
1326                                    p, len, 0);
1327                 /* check whether filter has been closed */
1328                 if (!ts->pids[pid])
1329                     return 0;
1330             }
1331             p += len;
1332             if (p < p_end) {
1333                 write_section_data(s, tss,
1334                                    p, p_end - p, 1);
1335             }
1336         } else {
1337             if (cc_ok) {
1338                 write_section_data(s, tss,
1339                                    p, p_end - p, 0);
1340             }
1341         }
1342     } else {
1343         int ret;
1344         // Note: The position here points actually behind the current packet.
1345         if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
1346                                             pos - ts->raw_packet_size)) < 0)
1347             return ret;
1348     }
1349
1350     return 0;
1351 }
1352
1353 /* XXX: try to find a better synchro over several packets (use
1354    get_packet_size() ?) */
1355 static int mpegts_resync(AVFormatContext *s)
1356 {
1357     AVIOContext *pb = s->pb;
1358     int c, i;
1359
1360     for(i = 0;i < MAX_RESYNC_SIZE; i++) {
1361         c = avio_r8(pb);
1362         if (pb->eof_reached)
1363             return -1;
1364         if (c == 0x47) {
1365             avio_seek(pb, -1, SEEK_CUR);
1366             return 0;
1367         }
1368     }
1369     av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
1370     /* no sync found */
1371     return -1;
1372 }
1373
1374 /* return -1 if error or EOF. Return 0 if OK. */
1375 static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size)
1376 {
1377     AVIOContext *pb = s->pb;
1378     int skip, len;
1379
1380     for(;;) {
1381         len = avio_read(pb, buf, TS_PACKET_SIZE);
1382         if (len != TS_PACKET_SIZE)
1383             return len < 0 ? len : AVERROR_EOF;
1384         /* check paquet sync byte */
1385         if (buf[0] != 0x47) {
1386             /* find a new packet start */
1387             avio_seek(pb, -TS_PACKET_SIZE, SEEK_CUR);
1388             if (mpegts_resync(s) < 0)
1389                 return AVERROR(EAGAIN);
1390             else
1391                 continue;
1392         } else {
1393             skip = raw_packet_size - TS_PACKET_SIZE;
1394             if (skip > 0)
1395                 avio_skip(pb, skip);
1396             break;
1397         }
1398     }
1399     return 0;
1400 }
1401
1402 static int handle_packets(MpegTSContext *ts, int nb_packets)
1403 {
1404     AVFormatContext *s = ts->stream;
1405     uint8_t packet[TS_PACKET_SIZE];
1406     int packet_num, ret = 0;
1407
1408     if (avio_tell(s->pb) != ts->last_pos) {
1409         int i;
1410         av_dlog("Skipping after seek\n");
1411         /* seek detected, flush pes buffer */
1412         for (i = 0; i < NB_PID_MAX; i++) {
1413             if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
1414                 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
1415                 av_freep(&pes->buffer);
1416                 ts->pids[i]->last_cc = -1;
1417                 pes->data_index = 0;
1418                 pes->state = MPEGTS_SKIP; /* skip until pes header */
1419             }
1420         }
1421     }
1422
1423     ts->stop_parse = 0;
1424     packet_num = 0;
1425     for(;;) {
1426         if (ts->stop_parse>0)
1427             break;
1428         packet_num++;
1429         if (nb_packets != 0 && packet_num >= nb_packets)
1430             break;
1431         ret = read_packet(s, packet, ts->raw_packet_size);
1432         if (ret != 0)
1433             break;
1434         ret = handle_packet(ts, packet);
1435         if (ret != 0)
1436             break;
1437     }
1438     ts->last_pos = avio_tell(s->pb);
1439     return ret;
1440 }
1441
1442 static int mpegts_probe(AVProbeData *p)
1443 {
1444 #if 1
1445     const int size= p->buf_size;
1446     int score, fec_score, dvhs_score;
1447     int check_count= size / TS_FEC_PACKET_SIZE;
1448 #define CHECK_COUNT 10
1449
1450     if (check_count < CHECK_COUNT)
1451         return -1;
1452
1453     score     = analyze(p->buf, TS_PACKET_SIZE     *check_count, TS_PACKET_SIZE     , NULL)*CHECK_COUNT/check_count;
1454     dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
1455     fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
1456 //    av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
1457
1458 // we need a clear definition for the returned score otherwise things will become messy sooner or later
1459     if     (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score     - CHECK_COUNT;
1460     else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score  - CHECK_COUNT;
1461     else if(                 fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
1462     else                                    return -1;
1463 #else
1464     /* only use the extension for safer guess */
1465     if (av_match_ext(p->filename, "ts"))
1466         return AVPROBE_SCORE_MAX;
1467     else
1468         return 0;
1469 #endif
1470 }
1471
1472 /* return the 90kHz PCR and the extension for the 27MHz PCR. return
1473    (-1) if not available */
1474 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
1475                      const uint8_t *packet)
1476 {
1477     int afc, len, flags;
1478     const uint8_t *p;
1479     unsigned int v;
1480
1481     afc = (packet[3] >> 4) & 3;
1482     if (afc <= 1)
1483         return -1;
1484     p = packet + 4;
1485     len = p[0];
1486     p++;
1487     if (len == 0)
1488         return -1;
1489     flags = *p++;
1490     len--;
1491     if (!(flags & 0x10))
1492         return -1;
1493     if (len < 6)
1494         return -1;
1495     v = AV_RB32(p);
1496     *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
1497     *ppcr_low = ((p[4] & 1) << 8) | p[5];
1498     return 0;
1499 }
1500
1501 static int mpegts_read_header(AVFormatContext *s,
1502                               AVFormatParameters *ap)
1503 {
1504     MpegTSContext *ts = s->priv_data;
1505     AVIOContext *pb = s->pb;
1506     uint8_t buf[5*1024];
1507     int len;
1508     int64_t pos;
1509
1510     /* read the first 1024 bytes to get packet size */
1511     pos = avio_tell(pb);
1512     len = avio_read(pb, buf, sizeof(buf));
1513     if (len != sizeof(buf))
1514         goto fail;
1515     ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
1516     if (ts->raw_packet_size <= 0)
1517         goto fail;
1518     ts->stream = s;
1519     ts->auto_guess = 0;
1520
1521     if (s->iformat == &ff_mpegts_demuxer) {
1522         /* normal demux */
1523
1524         /* first do a scaning to get all the services */
1525         if (pb->seekable && avio_seek(pb, pos, SEEK_SET) < 0)
1526             av_log(s, AV_LOG_ERROR, "Unable to seek back to the start\n");
1527
1528         mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
1529
1530         mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
1531
1532         handle_packets(ts, s->probesize / ts->raw_packet_size);
1533         /* if could not find service, enable auto_guess */
1534
1535         ts->auto_guess = 1;
1536
1537         av_dlog(ts->stream, "tuning done\n");
1538
1539         s->ctx_flags |= AVFMTCTX_NOHEADER;
1540     } else {
1541         AVStream *st;
1542         int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
1543         int64_t pcrs[2], pcr_h;
1544         int packet_count[2];
1545         uint8_t packet[TS_PACKET_SIZE];
1546
1547         /* only read packets */
1548
1549         st = av_new_stream(s, 0);
1550         if (!st)
1551             goto fail;
1552         av_set_pts_info(st, 60, 1, 27000000);
1553         st->codec->codec_type = AVMEDIA_TYPE_DATA;
1554         st->codec->codec_id = CODEC_ID_MPEG2TS;
1555
1556         /* we iterate until we find two PCRs to estimate the bitrate */
1557         pcr_pid = -1;
1558         nb_pcrs = 0;
1559         nb_packets = 0;
1560         for(;;) {
1561             ret = read_packet(s, packet, ts->raw_packet_size);
1562             if (ret < 0)
1563                 return -1;
1564             pid = AV_RB16(packet + 1) & 0x1fff;
1565             if ((pcr_pid == -1 || pcr_pid == pid) &&
1566                 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
1567                 pcr_pid = pid;
1568                 packet_count[nb_pcrs] = nb_packets;
1569                 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
1570                 nb_pcrs++;
1571                 if (nb_pcrs >= 2)
1572                     break;
1573             }
1574             nb_packets++;
1575         }
1576
1577         /* NOTE1: the bitrate is computed without the FEC */
1578         /* NOTE2: it is only the bitrate of the start of the stream */
1579         ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
1580         ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
1581         s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
1582         st->codec->bit_rate = s->bit_rate;
1583         st->start_time = ts->cur_pcr;
1584         av_dlog(ts->stream, "start=%0.3f pcr=%0.3f incr=%d\n",
1585                 st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
1586     }
1587
1588     avio_seek(pb, pos, SEEK_SET);
1589     return 0;
1590  fail:
1591     return -1;
1592 }
1593
1594 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
1595
1596 static int mpegts_raw_read_packet(AVFormatContext *s,
1597                                   AVPacket *pkt)
1598 {
1599     MpegTSContext *ts = s->priv_data;
1600     int ret, i;
1601     int64_t pcr_h, next_pcr_h, pos;
1602     int pcr_l, next_pcr_l;
1603     uint8_t pcr_buf[12];
1604
1605     if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
1606         return AVERROR(ENOMEM);
1607     pkt->pos= avio_tell(s->pb);
1608     ret = read_packet(s, pkt->data, ts->raw_packet_size);
1609     if (ret < 0) {
1610         av_free_packet(pkt);
1611         return ret;
1612     }
1613     if (ts->mpeg2ts_compute_pcr) {
1614         /* compute exact PCR for each packet */
1615         if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
1616             /* we read the next PCR (XXX: optimize it by using a bigger buffer */
1617             pos = avio_tell(s->pb);
1618             for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
1619                 avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
1620                 avio_read(s->pb, pcr_buf, 12);
1621                 if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
1622                     /* XXX: not precise enough */
1623                     ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
1624                         (i + 1);
1625                     break;
1626                 }
1627             }
1628             avio_seek(s->pb, pos, SEEK_SET);
1629             /* no next PCR found: we use previous increment */
1630             ts->cur_pcr = pcr_h * 300 + pcr_l;
1631         }
1632         pkt->pts = ts->cur_pcr;
1633         pkt->duration = ts->pcr_incr;
1634         ts->cur_pcr += ts->pcr_incr;
1635     }
1636     pkt->stream_index = 0;
1637     return 0;
1638 }
1639
1640 static int mpegts_read_packet(AVFormatContext *s,
1641                               AVPacket *pkt)
1642 {
1643     MpegTSContext *ts = s->priv_data;
1644     int ret, i;
1645
1646     ts->pkt = pkt;
1647     ret = handle_packets(ts, 0);
1648     if (ret < 0) {
1649         /* flush pes data left */
1650         for (i = 0; i < NB_PID_MAX; i++) {
1651             if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
1652                 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
1653                 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
1654                     new_pes_packet(pes, pkt);
1655                     pes->state = MPEGTS_SKIP;
1656                     ret = 0;
1657                     break;
1658                 }
1659             }
1660         }
1661     }
1662
1663     return ret;
1664 }
1665
1666 static int mpegts_read_close(AVFormatContext *s)
1667 {
1668     MpegTSContext *ts = s->priv_data;
1669     int i;
1670
1671     clear_programs(ts);
1672
1673     for(i=0;i<NB_PID_MAX;i++)
1674         if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
1675
1676     return 0;
1677 }
1678
1679 static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
1680                               int64_t *ppos, int64_t pos_limit)
1681 {
1682     MpegTSContext *ts = s->priv_data;
1683     int64_t pos, timestamp;
1684     uint8_t buf[TS_PACKET_SIZE];
1685     int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
1686     const int find_next= 1;
1687     pos = ((*ppos  + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
1688     if (find_next) {
1689         for(;;) {
1690             avio_seek(s->pb, pos, SEEK_SET);
1691             if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1692                 return AV_NOPTS_VALUE;
1693             if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
1694                 parse_pcr(&timestamp, &pcr_l, buf) == 0) {
1695                 break;
1696             }
1697             pos += ts->raw_packet_size;
1698         }
1699     } else {
1700         for(;;) {
1701             pos -= ts->raw_packet_size;
1702             if (pos < 0)
1703                 return AV_NOPTS_VALUE;
1704             avio_seek(s->pb, pos, SEEK_SET);
1705             if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1706                 return AV_NOPTS_VALUE;
1707             if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
1708                 parse_pcr(&timestamp, &pcr_l, buf) == 0) {
1709                 break;
1710             }
1711         }
1712     }
1713     *ppos = pos;
1714
1715     return timestamp;
1716 }
1717
1718 #ifdef USE_SYNCPOINT_SEARCH
1719
1720 static int read_seek2(AVFormatContext *s,
1721                       int stream_index,
1722                       int64_t min_ts,
1723                       int64_t target_ts,
1724                       int64_t max_ts,
1725                       int flags)
1726 {
1727     int64_t pos;
1728
1729     int64_t ts_ret, ts_adj;
1730     int stream_index_gen_search;
1731     AVStream *st;
1732     AVParserState *backup;
1733
1734     backup = ff_store_parser_state(s);
1735
1736     // detect direction of seeking for search purposes
1737     flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ?
1738              AVSEEK_FLAG_BACKWARD : 0;
1739
1740     if (flags & AVSEEK_FLAG_BYTE) {
1741         // use position directly, we will search starting from it
1742         pos = target_ts;
1743     } else {
1744         // search for some position with good timestamp match
1745         if (stream_index < 0) {
1746             stream_index_gen_search = av_find_default_stream_index(s);
1747             if (stream_index_gen_search < 0) {
1748                 ff_restore_parser_state(s, backup);
1749                 return -1;
1750             }
1751
1752             st = s->streams[stream_index_gen_search];
1753             // timestamp for default must be expressed in AV_TIME_BASE units
1754             ts_adj = av_rescale(target_ts,
1755                                 st->time_base.den,
1756                                 AV_TIME_BASE * (int64_t)st->time_base.num);
1757         } else {
1758             ts_adj = target_ts;
1759             stream_index_gen_search = stream_index;
1760         }
1761         pos = av_gen_search(s, stream_index_gen_search, ts_adj,
1762                             0, INT64_MAX, -1,
1763                             AV_NOPTS_VALUE,
1764                             AV_NOPTS_VALUE,
1765                             flags, &ts_ret, mpegts_get_pcr);
1766         if (pos < 0) {
1767             ff_restore_parser_state(s, backup);
1768             return -1;
1769         }
1770     }
1771
1772     // search for actual matching keyframe/starting position for all streams
1773     if (ff_gen_syncpoint_search(s, stream_index, pos,
1774                                 min_ts, target_ts, max_ts,
1775                                 flags) < 0) {
1776         ff_restore_parser_state(s, backup);
1777         return -1;
1778     }
1779
1780     ff_free_parser_state(s, backup);
1781     return 0;
1782 }
1783
1784 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
1785 {
1786     int ret;
1787     if (flags & AVSEEK_FLAG_BACKWARD) {
1788         flags &= ~AVSEEK_FLAG_BACKWARD;
1789         ret = read_seek2(s, stream_index, INT64_MIN, target_ts, target_ts, flags);
1790         if (ret < 0)
1791             // for compatibility reasons, seek to the best-fitting timestamp
1792             ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
1793     } else {
1794         ret = read_seek2(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
1795         if (ret < 0)
1796             // for compatibility reasons, seek to the best-fitting timestamp
1797             ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
1798     }
1799     return ret;
1800 }
1801
1802 #else
1803
1804 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
1805     MpegTSContext *ts = s->priv_data;
1806     uint8_t buf[TS_PACKET_SIZE];
1807     int64_t pos;
1808
1809     if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
1810         return -1;
1811
1812     pos= avio_tell(s->pb);
1813
1814     for(;;) {
1815         avio_seek(s->pb, pos, SEEK_SET);
1816         if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1817             return -1;
1818 //        pid = AV_RB16(buf + 1) & 0x1fff;
1819         if(buf[1] & 0x40) break;
1820         pos += ts->raw_packet_size;
1821     }
1822     avio_seek(s->pb, pos, SEEK_SET);
1823
1824     return 0;
1825 }
1826
1827 #endif
1828
1829 /**************************************************************/
1830 /* parsing functions - called from other demuxers such as RTP */
1831
1832 MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
1833 {
1834     MpegTSContext *ts;
1835
1836     ts = av_mallocz(sizeof(MpegTSContext));
1837     if (!ts)
1838         return NULL;
1839     /* no stream case, currently used by RTP */
1840     ts->raw_packet_size = TS_PACKET_SIZE;
1841     ts->stream = s;
1842     ts->auto_guess = 1;
1843     return ts;
1844 }
1845
1846 /* return the consumed length if a packet was output, or -1 if no
1847    packet is output */
1848 int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
1849                         const uint8_t *buf, int len)
1850 {
1851     int len1;
1852
1853     len1 = len;
1854     ts->pkt = pkt;
1855     ts->stop_parse = 0;
1856     for(;;) {
1857         if (ts->stop_parse>0)
1858             break;
1859         if (len < TS_PACKET_SIZE)
1860             return -1;
1861         if (buf[0] != 0x47) {
1862             buf++;
1863             len--;
1864         } else {
1865             handle_packet(ts, buf);
1866             buf += TS_PACKET_SIZE;
1867             len -= TS_PACKET_SIZE;
1868         }
1869     }
1870     return len1 - len;
1871 }
1872
1873 void ff_mpegts_parse_close(MpegTSContext *ts)
1874 {
1875     int i;
1876
1877     for(i=0;i<NB_PID_MAX;i++)
1878         av_free(ts->pids[i]);
1879     av_free(ts);
1880 }
1881
1882 AVInputFormat ff_mpegts_demuxer = {
1883     .name           = "mpegts",
1884     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
1885     .priv_data_size = sizeof(MpegTSContext),
1886     .read_probe     = mpegts_probe,
1887     .read_header    = mpegts_read_header,
1888     .read_packet    = mpegts_read_packet,
1889     .read_close     = mpegts_read_close,
1890     .read_seek      = read_seek,
1891     .read_timestamp = mpegts_get_pcr,
1892     .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
1893 #ifdef USE_SYNCPOINT_SEARCH
1894     .read_seek2 = read_seek2,
1895 #endif
1896 };
1897
1898 AVInputFormat ff_mpegtsraw_demuxer = {
1899     .name           = "mpegtsraw",
1900     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
1901     .priv_data_size = sizeof(MpegTSContext),
1902     .read_header    = mpegts_read_header,
1903     .read_packet    = mpegts_raw_read_packet,
1904     .read_close     = mpegts_read_close,
1905     .read_seek      = read_seek,
1906     .read_timestamp = mpegts_get_pcr,
1907     .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
1908 #ifdef USE_SYNCPOINT_SEARCH
1909     .read_seek2 = read_seek2,
1910 #endif
1911     .priv_class = &mpegtsraw_class,
1912 };