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