Libav 0.7.1
libavformat/mpegts.c
Go to the documentation of this file.
00001 /*
00002  * MPEG2 transport stream (aka DVB) demuxer
00003  * Copyright (c) 2002-2003 Fabrice Bellard
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 //#define USE_SYNCPOINT_SEARCH
00023 
00024 #include "libavutil/crc.h"
00025 #include "libavutil/intreadwrite.h"
00026 #include "libavutil/log.h"
00027 #include "libavutil/dict.h"
00028 #include "libavutil/opt.h"
00029 #include "libavcodec/bytestream.h"
00030 #include "avformat.h"
00031 #include "mpegts.h"
00032 #include "internal.h"
00033 #include "avio_internal.h"
00034 #include "seek.h"
00035 #include "mpeg.h"
00036 #include "isom.h"
00037 
00038 /* maximum size in which we look for synchronisation if
00039    synchronisation is lost */
00040 #define MAX_RESYNC_SIZE 65536
00041 
00042 #define MAX_PES_PAYLOAD 200*1024
00043 
00044 enum MpegTSFilterType {
00045     MPEGTS_PES,
00046     MPEGTS_SECTION,
00047 };
00048 
00049 typedef struct MpegTSFilter MpegTSFilter;
00050 
00051 typedef int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos);
00052 
00053 typedef struct MpegTSPESFilter {
00054     PESCallback *pes_cb;
00055     void *opaque;
00056 } MpegTSPESFilter;
00057 
00058 typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
00059 
00060 typedef void SetServiceCallback(void *opaque, int ret);
00061 
00062 typedef struct MpegTSSectionFilter {
00063     int section_index;
00064     int section_h_size;
00065     uint8_t *section_buf;
00066     unsigned int check_crc:1;
00067     unsigned int end_of_section_reached:1;
00068     SectionCallback *section_cb;
00069     void *opaque;
00070 } MpegTSSectionFilter;
00071 
00072 struct MpegTSFilter {
00073     int pid;
00074     int last_cc; /* last cc code (-1 if first packet) */
00075     enum MpegTSFilterType type;
00076     union {
00077         MpegTSPESFilter pes_filter;
00078         MpegTSSectionFilter section_filter;
00079     } u;
00080 };
00081 
00082 #define MAX_PIDS_PER_PROGRAM 64
00083 struct Program {
00084     unsigned int id; //program id/service id
00085     unsigned int nb_pids;
00086     unsigned int pids[MAX_PIDS_PER_PROGRAM];
00087 };
00088 
00089 struct MpegTSContext {
00090     const AVClass *class;
00091     /* user data */
00092     AVFormatContext *stream;
00094     int raw_packet_size;
00095 
00096     int pos47;
00097 
00099     int auto_guess;
00100 
00102     int mpeg2ts_compute_pcr;
00103 
00104     int64_t cur_pcr;    
00105     int pcr_incr;       
00107     /* data needed to handle file based ts */
00109     int stop_parse;
00111     AVPacket *pkt;
00113     int64_t last_pos;
00114 
00115     /******************************************/
00116     /* private mpegts data */
00117     /* scan context */
00119     unsigned int nb_prg;
00120     struct Program *prg;
00121 
00122 
00124     MpegTSFilter *pids[NB_PID_MAX];
00125 };
00126 
00127 static const AVOption options[] = {
00128     {"compute_pcr", "Compute exact PCR for each transport stream packet.", offsetof(MpegTSContext, mpeg2ts_compute_pcr), FF_OPT_TYPE_INT,
00129      {.dbl = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
00130     { NULL },
00131 };
00132 
00133 static const AVClass mpegtsraw_class = {
00134     .class_name = "mpegtsraw demuxer",
00135     .item_name  = av_default_item_name,
00136     .option     = options,
00137     .version    = LIBAVUTIL_VERSION_INT,
00138 };
00139 
00140 /* TS stream handling */
00141 
00142 enum MpegTSState {
00143     MPEGTS_HEADER = 0,
00144     MPEGTS_PESHEADER,
00145     MPEGTS_PESHEADER_FILL,
00146     MPEGTS_PAYLOAD,
00147     MPEGTS_SKIP,
00148 };
00149 
00150 /* enough for PES header + length */
00151 #define PES_START_SIZE  6
00152 #define PES_HEADER_SIZE 9
00153 #define MAX_PES_HEADER_SIZE (9 + 255)
00154 
00155 typedef struct PESContext {
00156     int pid;
00157     int pcr_pid; 
00158     int stream_type;
00159     MpegTSContext *ts;
00160     AVFormatContext *stream;
00161     AVStream *st;
00162     AVStream *sub_st; 
00163     enum MpegTSState state;
00164     /* used to get the format */
00165     int data_index;
00166     int total_size;
00167     int pes_header_size;
00168     int extended_stream_id;
00169     int64_t pts, dts;
00170     int64_t ts_packet_pos; 
00171     uint8_t header[MAX_PES_HEADER_SIZE];
00172     uint8_t *buffer;
00173 } PESContext;
00174 
00175 extern AVInputFormat ff_mpegts_demuxer;
00176 
00177 static void clear_program(MpegTSContext *ts, unsigned int programid)
00178 {
00179     int i;
00180 
00181     for(i=0; i<ts->nb_prg; i++)
00182         if(ts->prg[i].id == programid)
00183             ts->prg[i].nb_pids = 0;
00184 }
00185 
00186 static void clear_programs(MpegTSContext *ts)
00187 {
00188     av_freep(&ts->prg);
00189     ts->nb_prg=0;
00190 }
00191 
00192 static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
00193 {
00194     struct Program *p;
00195     void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(struct Program));
00196     if(!tmp)
00197         return;
00198     ts->prg = tmp;
00199     p = &ts->prg[ts->nb_prg];
00200     p->id = programid;
00201     p->nb_pids = 0;
00202     ts->nb_prg++;
00203 }
00204 
00205 static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
00206 {
00207     int i;
00208     struct Program *p = NULL;
00209     for(i=0; i<ts->nb_prg; i++) {
00210         if(ts->prg[i].id == programid) {
00211             p = &ts->prg[i];
00212             break;
00213         }
00214     }
00215     if(!p)
00216         return;
00217 
00218     if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
00219         return;
00220     p->pids[p->nb_pids++] = pid;
00221 }
00222 
00231 static int discard_pid(MpegTSContext *ts, unsigned int pid)
00232 {
00233     int i, j, k;
00234     int used = 0, discarded = 0;
00235     struct Program *p;
00236     for(i=0; i<ts->nb_prg; i++) {
00237         p = &ts->prg[i];
00238         for(j=0; j<p->nb_pids; j++) {
00239             if(p->pids[j] != pid)
00240                 continue;
00241             //is program with id p->id set to be discarded?
00242             for(k=0; k<ts->stream->nb_programs; k++) {
00243                 if(ts->stream->programs[k]->id == p->id) {
00244                     if(ts->stream->programs[k]->discard == AVDISCARD_ALL)
00245                         discarded++;
00246                     else
00247                         used++;
00248                 }
00249             }
00250         }
00251     }
00252 
00253     return !used && discarded;
00254 }
00255 
00260 static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
00261                                const uint8_t *buf, int buf_size, int is_start)
00262 {
00263     MpegTSSectionFilter *tss = &tss1->u.section_filter;
00264     int len;
00265 
00266     if (is_start) {
00267         memcpy(tss->section_buf, buf, buf_size);
00268         tss->section_index = buf_size;
00269         tss->section_h_size = -1;
00270         tss->end_of_section_reached = 0;
00271     } else {
00272         if (tss->end_of_section_reached)
00273             return;
00274         len = 4096 - tss->section_index;
00275         if (buf_size < len)
00276             len = buf_size;
00277         memcpy(tss->section_buf + tss->section_index, buf, len);
00278         tss->section_index += len;
00279     }
00280 
00281     /* compute section length if possible */
00282     if (tss->section_h_size == -1 && tss->section_index >= 3) {
00283         len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
00284         if (len > 4096)
00285             return;
00286         tss->section_h_size = len;
00287     }
00288 
00289     if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
00290         tss->end_of_section_reached = 1;
00291         if (!tss->check_crc ||
00292             av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
00293                    tss->section_buf, tss->section_h_size) == 0)
00294             tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
00295     }
00296 }
00297 
00298 static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
00299                                          SectionCallback *section_cb, void *opaque,
00300                                          int check_crc)
00301 
00302 {
00303     MpegTSFilter *filter;
00304     MpegTSSectionFilter *sec;
00305 
00306     av_dlog(ts->stream, "Filter: pid=0x%x\n", pid);
00307 
00308     if (pid >= NB_PID_MAX || ts->pids[pid])
00309         return NULL;
00310     filter = av_mallocz(sizeof(MpegTSFilter));
00311     if (!filter)
00312         return NULL;
00313     ts->pids[pid] = filter;
00314     filter->type = MPEGTS_SECTION;
00315     filter->pid = pid;
00316     filter->last_cc = -1;
00317     sec = &filter->u.section_filter;
00318     sec->section_cb = section_cb;
00319     sec->opaque = opaque;
00320     sec->section_buf = av_malloc(MAX_SECTION_SIZE);
00321     sec->check_crc = check_crc;
00322     if (!sec->section_buf) {
00323         av_free(filter);
00324         return NULL;
00325     }
00326     return filter;
00327 }
00328 
00329 static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
00330                                      PESCallback *pes_cb,
00331                                      void *opaque)
00332 {
00333     MpegTSFilter *filter;
00334     MpegTSPESFilter *pes;
00335 
00336     if (pid >= NB_PID_MAX || ts->pids[pid])
00337         return NULL;
00338     filter = av_mallocz(sizeof(MpegTSFilter));
00339     if (!filter)
00340         return NULL;
00341     ts->pids[pid] = filter;
00342     filter->type = MPEGTS_PES;
00343     filter->pid = pid;
00344     filter->last_cc = -1;
00345     pes = &filter->u.pes_filter;
00346     pes->pes_cb = pes_cb;
00347     pes->opaque = opaque;
00348     return filter;
00349 }
00350 
00351 static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
00352 {
00353     int pid;
00354 
00355     pid = filter->pid;
00356     if (filter->type == MPEGTS_SECTION)
00357         av_freep(&filter->u.section_filter.section_buf);
00358     else if (filter->type == MPEGTS_PES) {
00359         PESContext *pes = filter->u.pes_filter.opaque;
00360         av_freep(&pes->buffer);
00361         /* referenced private data will be freed later in
00362          * av_close_input_stream */
00363         if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
00364             av_freep(&filter->u.pes_filter.opaque);
00365         }
00366     }
00367 
00368     av_free(filter);
00369     ts->pids[pid] = NULL;
00370 }
00371 
00372 static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
00373     int stat[TS_MAX_PACKET_SIZE];
00374     int i;
00375     int x=0;
00376     int best_score=0;
00377 
00378     memset(stat, 0, packet_size*sizeof(int));
00379 
00380     for(x=i=0; i<size-3; i++){
00381         if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && (buf[i+3] & 0x30)){
00382             stat[x]++;
00383             if(stat[x] > best_score){
00384                 best_score= stat[x];
00385                 if(index) *index= x;
00386             }
00387         }
00388 
00389         x++;
00390         if(x == packet_size) x= 0;
00391     }
00392 
00393     return best_score;
00394 }
00395 
00396 /* autodetect fec presence. Must have at least 1024 bytes  */
00397 static int get_packet_size(const uint8_t *buf, int size)
00398 {
00399     int score, fec_score, dvhs_score;
00400 
00401     if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
00402         return -1;
00403 
00404     score    = analyze(buf, size, TS_PACKET_SIZE, NULL);
00405     dvhs_score    = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
00406     fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
00407 //    av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
00408 
00409     if     (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
00410     else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
00411     else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
00412     else                       return -1;
00413 }
00414 
00415 typedef struct SectionHeader {
00416     uint8_t tid;
00417     uint16_t id;
00418     uint8_t version;
00419     uint8_t sec_num;
00420     uint8_t last_sec_num;
00421 } SectionHeader;
00422 
00423 static inline int get8(const uint8_t **pp, const uint8_t *p_end)
00424 {
00425     const uint8_t *p;
00426     int c;
00427 
00428     p = *pp;
00429     if (p >= p_end)
00430         return -1;
00431     c = *p++;
00432     *pp = p;
00433     return c;
00434 }
00435 
00436 static inline int get16(const uint8_t **pp, const uint8_t *p_end)
00437 {
00438     const uint8_t *p;
00439     int c;
00440 
00441     p = *pp;
00442     if ((p + 1) >= p_end)
00443         return -1;
00444     c = AV_RB16(p);
00445     p += 2;
00446     *pp = p;
00447     return c;
00448 }
00449 
00450 /* read and allocate a DVB string preceeded by its length */
00451 static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
00452 {
00453     int len;
00454     const uint8_t *p;
00455     char *str;
00456 
00457     p = *pp;
00458     len = get8(&p, p_end);
00459     if (len < 0)
00460         return NULL;
00461     if ((p + len) > p_end)
00462         return NULL;
00463     str = av_malloc(len + 1);
00464     if (!str)
00465         return NULL;
00466     memcpy(str, p, len);
00467     str[len] = '\0';
00468     p += len;
00469     *pp = p;
00470     return str;
00471 }
00472 
00473 static int parse_section_header(SectionHeader *h,
00474                                 const uint8_t **pp, const uint8_t *p_end)
00475 {
00476     int val;
00477 
00478     val = get8(pp, p_end);
00479     if (val < 0)
00480         return -1;
00481     h->tid = val;
00482     *pp += 2;
00483     val = get16(pp, p_end);
00484     if (val < 0)
00485         return -1;
00486     h->id = val;
00487     val = get8(pp, p_end);
00488     if (val < 0)
00489         return -1;
00490     h->version = (val >> 1) & 0x1f;
00491     val = get8(pp, p_end);
00492     if (val < 0)
00493         return -1;
00494     h->sec_num = val;
00495     val = get8(pp, p_end);
00496     if (val < 0)
00497         return -1;
00498     h->last_sec_num = val;
00499     return 0;
00500 }
00501 
00502 typedef struct {
00503     uint32_t stream_type;
00504     enum AVMediaType codec_type;
00505     enum CodecID codec_id;
00506 } StreamType;
00507 
00508 static const StreamType ISO_types[] = {
00509     { 0x01, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
00510     { 0x02, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
00511     { 0x03, AVMEDIA_TYPE_AUDIO,        CODEC_ID_MP3 },
00512     { 0x04, AVMEDIA_TYPE_AUDIO,        CODEC_ID_MP3 },
00513     { 0x0f, AVMEDIA_TYPE_AUDIO,        CODEC_ID_AAC },
00514     { 0x10, AVMEDIA_TYPE_VIDEO,      CODEC_ID_MPEG4 },
00515     { 0x11, AVMEDIA_TYPE_AUDIO,   CODEC_ID_AAC_LATM }, /* LATM syntax */
00516     { 0x1b, AVMEDIA_TYPE_VIDEO,       CODEC_ID_H264 },
00517     { 0xd1, AVMEDIA_TYPE_VIDEO,      CODEC_ID_DIRAC },
00518     { 0xea, AVMEDIA_TYPE_VIDEO,        CODEC_ID_VC1 },
00519     { 0 },
00520 };
00521 
00522 static const StreamType HDMV_types[] = {
00523     { 0x80, AVMEDIA_TYPE_AUDIO, CODEC_ID_PCM_BLURAY },
00524     { 0x81, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
00525     { 0x82, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
00526     { 0x83, AVMEDIA_TYPE_AUDIO, CODEC_ID_TRUEHD },
00527     { 0x84, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 },
00528     { 0x90, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
00529     { 0 },
00530 };
00531 
00532 /* ATSC ? */
00533 static const StreamType MISC_types[] = {
00534     { 0x81, AVMEDIA_TYPE_AUDIO,   CODEC_ID_AC3 },
00535     { 0x8a, AVMEDIA_TYPE_AUDIO,   CODEC_ID_DTS },
00536     { 0 },
00537 };
00538 
00539 static const StreamType REGD_types[] = {
00540     { MKTAG('d','r','a','c'), AVMEDIA_TYPE_VIDEO, CODEC_ID_DIRAC },
00541     { MKTAG('A','C','-','3'), AVMEDIA_TYPE_AUDIO,   CODEC_ID_AC3 },
00542     { MKTAG('B','S','S','D'), AVMEDIA_TYPE_AUDIO, CODEC_ID_S302M },
00543     { 0 },
00544 };
00545 
00546 /* descriptor present */
00547 static const StreamType DESC_types[] = {
00548     { 0x6a, AVMEDIA_TYPE_AUDIO,             CODEC_ID_AC3 }, /* AC-3 descriptor */
00549     { 0x7a, AVMEDIA_TYPE_AUDIO,            CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
00550     { 0x7b, AVMEDIA_TYPE_AUDIO,             CODEC_ID_DTS },
00551     { 0x56, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_TELETEXT },
00552     { 0x59, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
00553     { 0 },
00554 };
00555 
00556 static void mpegts_find_stream_type(AVStream *st,
00557                                     uint32_t stream_type, const StreamType *types)
00558 {
00559     for (; types->stream_type; types++) {
00560         if (stream_type == types->stream_type) {
00561             st->codec->codec_type = types->codec_type;
00562             st->codec->codec_id   = types->codec_id;
00563             return;
00564         }
00565     }
00566 }
00567 
00568 static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
00569                                   uint32_t stream_type, uint32_t prog_reg_desc)
00570 {
00571     av_set_pts_info(st, 33, 1, 90000);
00572     st->priv_data = pes;
00573     st->codec->codec_type = AVMEDIA_TYPE_DATA;
00574     st->codec->codec_id   = CODEC_ID_NONE;
00575     st->need_parsing = AVSTREAM_PARSE_FULL;
00576     pes->st = st;
00577     pes->stream_type = stream_type;
00578 
00579     av_log(pes->stream, AV_LOG_DEBUG,
00580            "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
00581            st->index, pes->stream_type, pes->pid, (char*)&prog_reg_desc);
00582 
00583     st->codec->codec_tag = pes->stream_type;
00584 
00585     mpegts_find_stream_type(st, pes->stream_type, ISO_types);
00586     if (prog_reg_desc == AV_RL32("HDMV") &&
00587         st->codec->codec_id == CODEC_ID_NONE) {
00588         mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
00589         if (pes->stream_type == 0x83) {
00590             // HDMV TrueHD streams also contain an AC3 coded version of the
00591             // audio track - add a second stream for this
00592             AVStream *sub_st;
00593             // priv_data cannot be shared between streams
00594             PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
00595             if (!sub_pes)
00596                 return AVERROR(ENOMEM);
00597             memcpy(sub_pes, pes, sizeof(*sub_pes));
00598 
00599             sub_st = av_new_stream(pes->stream, pes->pid);
00600             if (!sub_st) {
00601                 av_free(sub_pes);
00602                 return AVERROR(ENOMEM);
00603             }
00604 
00605             av_set_pts_info(sub_st, 33, 1, 90000);
00606             sub_st->priv_data = sub_pes;
00607             sub_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00608             sub_st->codec->codec_id   = CODEC_ID_AC3;
00609             sub_st->need_parsing = AVSTREAM_PARSE_FULL;
00610             sub_pes->sub_st = pes->sub_st = sub_st;
00611         }
00612     }
00613     if (st->codec->codec_id == CODEC_ID_NONE)
00614         mpegts_find_stream_type(st, pes->stream_type, MISC_types);
00615 
00616     return 0;
00617 }
00618 
00619 static void new_pes_packet(PESContext *pes, AVPacket *pkt)
00620 {
00621     av_init_packet(pkt);
00622 
00623     pkt->destruct = av_destruct_packet;
00624     pkt->data = pes->buffer;
00625     pkt->size = pes->data_index;
00626     memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00627 
00628     // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
00629     if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
00630         pkt->stream_index = pes->sub_st->index;
00631     else
00632         pkt->stream_index = pes->st->index;
00633     pkt->pts = pes->pts;
00634     pkt->dts = pes->dts;
00635     /* store position of first TS packet of this PES packet */
00636     pkt->pos = pes->ts_packet_pos;
00637 
00638     /* reset pts values */
00639     pes->pts = AV_NOPTS_VALUE;
00640     pes->dts = AV_NOPTS_VALUE;
00641     pes->buffer = NULL;
00642     pes->data_index = 0;
00643 }
00644 
00645 /* return non zero if a packet could be constructed */
00646 static int mpegts_push_data(MpegTSFilter *filter,
00647                             const uint8_t *buf, int buf_size, int is_start,
00648                             int64_t pos)
00649 {
00650     PESContext *pes = filter->u.pes_filter.opaque;
00651     MpegTSContext *ts = pes->ts;
00652     const uint8_t *p;
00653     int len, code;
00654 
00655     if(!ts->pkt)
00656         return 0;
00657 
00658     if (is_start) {
00659         if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
00660             new_pes_packet(pes, ts->pkt);
00661             ts->stop_parse = 1;
00662         }
00663         pes->state = MPEGTS_HEADER;
00664         pes->data_index = 0;
00665         pes->ts_packet_pos = pos;
00666     }
00667     p = buf;
00668     while (buf_size > 0) {
00669         switch(pes->state) {
00670         case MPEGTS_HEADER:
00671             len = PES_START_SIZE - pes->data_index;
00672             if (len > buf_size)
00673                 len = buf_size;
00674             memcpy(pes->header + pes->data_index, p, len);
00675             pes->data_index += len;
00676             p += len;
00677             buf_size -= len;
00678             if (pes->data_index == PES_START_SIZE) {
00679                 /* we got all the PES or section header. We can now
00680                    decide */
00681                 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
00682                     pes->header[2] == 0x01) {
00683                     /* it must be an mpeg2 PES stream */
00684                     code = pes->header[3] | 0x100;
00685                     av_dlog(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
00686 
00687                     if ((pes->st && pes->st->discard == AVDISCARD_ALL) ||
00688                         code == 0x1be) /* padding_stream */
00689                         goto skip;
00690 
00691                     /* stream not present in PMT */
00692                     if (!pes->st) {
00693                         pes->st = av_new_stream(ts->stream, pes->pid);
00694                         if (!pes->st)
00695                             return AVERROR(ENOMEM);
00696                         mpegts_set_stream_info(pes->st, pes, 0, 0);
00697                     }
00698 
00699                     pes->total_size = AV_RB16(pes->header + 4);
00700                     /* NOTE: a zero total size means the PES size is
00701                        unbounded */
00702                     if (!pes->total_size)
00703                         pes->total_size = MAX_PES_PAYLOAD;
00704 
00705                     /* allocate pes buffer */
00706                     pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
00707                     if (!pes->buffer)
00708                         return AVERROR(ENOMEM);
00709 
00710                     if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
00711                         code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
00712                         code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
00713                         code != 0x1f8) {                  /* ITU-T Rec. H.222.1 type E stream */
00714                         pes->state = MPEGTS_PESHEADER;
00715                         if (pes->st->codec->codec_id == CODEC_ID_NONE) {
00716                             av_dlog(pes->stream, "pid=%x stream_type=%x probing\n",
00717                                     pes->pid, pes->stream_type);
00718                             pes->st->codec->codec_id = CODEC_ID_PROBE;
00719                         }
00720                     } else {
00721                         pes->state = MPEGTS_PAYLOAD;
00722                         pes->data_index = 0;
00723                     }
00724                 } else {
00725                     /* otherwise, it should be a table */
00726                     /* skip packet */
00727                 skip:
00728                     pes->state = MPEGTS_SKIP;
00729                     continue;
00730                 }
00731             }
00732             break;
00733             /**********************************************/
00734             /* PES packing parsing */
00735         case MPEGTS_PESHEADER:
00736             len = PES_HEADER_SIZE - pes->data_index;
00737             if (len < 0)
00738                 return -1;
00739             if (len > buf_size)
00740                 len = buf_size;
00741             memcpy(pes->header + pes->data_index, p, len);
00742             pes->data_index += len;
00743             p += len;
00744             buf_size -= len;
00745             if (pes->data_index == PES_HEADER_SIZE) {
00746                 pes->pes_header_size = pes->header[8] + 9;
00747                 pes->state = MPEGTS_PESHEADER_FILL;
00748             }
00749             break;
00750         case MPEGTS_PESHEADER_FILL:
00751             len = pes->pes_header_size - pes->data_index;
00752             if (len < 0)
00753                 return -1;
00754             if (len > buf_size)
00755                 len = buf_size;
00756             memcpy(pes->header + pes->data_index, p, len);
00757             pes->data_index += len;
00758             p += len;
00759             buf_size -= len;
00760             if (pes->data_index == pes->pes_header_size) {
00761                 const uint8_t *r;
00762                 unsigned int flags, pes_ext, skip;
00763 
00764                 flags = pes->header[7];
00765                 r = pes->header + 9;
00766                 pes->pts = AV_NOPTS_VALUE;
00767                 pes->dts = AV_NOPTS_VALUE;
00768                 if ((flags & 0xc0) == 0x80) {
00769                     pes->dts = pes->pts = ff_parse_pes_pts(r);
00770                     r += 5;
00771                 } else if ((flags & 0xc0) == 0xc0) {
00772                     pes->pts = ff_parse_pes_pts(r);
00773                     r += 5;
00774                     pes->dts = ff_parse_pes_pts(r);
00775                     r += 5;
00776                 }
00777                 pes->extended_stream_id = -1;
00778                 if (flags & 0x01) { /* PES extension */
00779                     pes_ext = *r++;
00780                     /* Skip PES private data, program packet sequence counter and P-STD buffer */
00781                     skip = (pes_ext >> 4) & 0xb;
00782                     skip += skip & 0x9;
00783                     r += skip;
00784                     if ((pes_ext & 0x41) == 0x01 &&
00785                         (r + 2) <= (pes->header + pes->pes_header_size)) {
00786                         /* PES extension 2 */
00787                         if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
00788                             pes->extended_stream_id = r[1];
00789                     }
00790                 }
00791 
00792                 /* we got the full header. We parse it and get the payload */
00793                 pes->state = MPEGTS_PAYLOAD;
00794                 pes->data_index = 0;
00795             }
00796             break;
00797         case MPEGTS_PAYLOAD:
00798             if (buf_size > 0 && pes->buffer) {
00799                 if (pes->data_index > 0 && pes->data_index+buf_size > pes->total_size) {
00800                     new_pes_packet(pes, ts->pkt);
00801                     pes->total_size = MAX_PES_PAYLOAD;
00802                     pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
00803                     if (!pes->buffer)
00804                         return AVERROR(ENOMEM);
00805                     ts->stop_parse = 1;
00806                 } else if (pes->data_index == 0 && buf_size > pes->total_size) {
00807                     // pes packet size is < ts size packet and pes data is padded with 0xff
00808                     // not sure if this is legal in ts but see issue #2392
00809                     buf_size = pes->total_size;
00810                 }
00811                 memcpy(pes->buffer+pes->data_index, p, buf_size);
00812                 pes->data_index += buf_size;
00813             }
00814             buf_size = 0;
00815             /* emit complete packets with known packet size
00816              * decreases demuxer delay for infrequent packets like subtitles from
00817              * a couple of seconds to milliseconds for properly muxed files.
00818              * total_size is the number of bytes following pes_packet_length
00819              * in the pes header, i.e. not counting the first 6 bytes */
00820             if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD &&
00821                 pes->pes_header_size + pes->data_index == pes->total_size + 6) {
00822                 ts->stop_parse = 1;
00823                 new_pes_packet(pes, ts->pkt);
00824             }
00825             break;
00826         case MPEGTS_SKIP:
00827             buf_size = 0;
00828             break;
00829         }
00830     }
00831 
00832     return 0;
00833 }
00834 
00835 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
00836 {
00837     MpegTSFilter *tss;
00838     PESContext *pes;
00839 
00840     /* if no pid found, then add a pid context */
00841     pes = av_mallocz(sizeof(PESContext));
00842     if (!pes)
00843         return 0;
00844     pes->ts = ts;
00845     pes->stream = ts->stream;
00846     pes->pid = pid;
00847     pes->pcr_pid = pcr_pid;
00848     pes->state = MPEGTS_SKIP;
00849     pes->pts = AV_NOPTS_VALUE;
00850     pes->dts = AV_NOPTS_VALUE;
00851     tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
00852     if (!tss) {
00853         av_free(pes);
00854         return 0;
00855     }
00856     return pes;
00857 }
00858 
00859 static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
00860                          int *es_id, uint8_t **dec_config_descr,
00861                          int *dec_config_descr_size)
00862 {
00863     AVIOContext pb;
00864     int tag;
00865     unsigned len;
00866 
00867     ffio_init_context(&pb, buf, size, 0, NULL, NULL, NULL, NULL);
00868 
00869     len = ff_mp4_read_descr(s, &pb, &tag);
00870     if (tag == MP4IODescrTag) {
00871         avio_rb16(&pb); // ID
00872         avio_r8(&pb);
00873         avio_r8(&pb);
00874         avio_r8(&pb);
00875         avio_r8(&pb);
00876         avio_r8(&pb);
00877         len = ff_mp4_read_descr(s, &pb, &tag);
00878         if (tag == MP4ESDescrTag) {
00879             *es_id = avio_rb16(&pb); /* ES_ID */
00880             av_dlog(s, "ES_ID %#x\n", *es_id);
00881             avio_r8(&pb); /* priority */
00882             len = ff_mp4_read_descr(s, &pb, &tag);
00883             if (tag == MP4DecConfigDescrTag) {
00884                 *dec_config_descr = av_malloc(len);
00885                 if (!*dec_config_descr)
00886                     return AVERROR(ENOMEM);
00887                 *dec_config_descr_size = len;
00888                 avio_read(&pb, *dec_config_descr, len);
00889             }
00890         }
00891     }
00892     return 0;
00893 }
00894 
00895 int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
00896                               const uint8_t **pp, const uint8_t *desc_list_end,
00897                               int mp4_dec_config_descr_len, int mp4_es_id, int pid,
00898                               uint8_t *mp4_dec_config_descr)
00899 {
00900     const uint8_t *desc_end;
00901     int desc_len, desc_tag;
00902     char language[252];
00903     int i;
00904 
00905     desc_tag = get8(pp, desc_list_end);
00906     if (desc_tag < 0)
00907         return -1;
00908     desc_len = get8(pp, desc_list_end);
00909     if (desc_len < 0)
00910         return -1;
00911     desc_end = *pp + desc_len;
00912     if (desc_end > desc_list_end)
00913         return -1;
00914 
00915     av_dlog(fc, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
00916 
00917     if (st->codec->codec_id == CODEC_ID_NONE &&
00918         stream_type == STREAM_TYPE_PRIVATE_DATA)
00919         mpegts_find_stream_type(st, desc_tag, DESC_types);
00920 
00921     switch(desc_tag) {
00922     case 0x1F: /* FMC descriptor */
00923         get16(pp, desc_end);
00924         if (st->codec->codec_id == CODEC_ID_AAC_LATM &&
00925             mp4_dec_config_descr_len && mp4_es_id == pid) {
00926             AVIOContext pb;
00927             ffio_init_context(&pb, mp4_dec_config_descr,
00928                           mp4_dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
00929             ff_mp4_read_dec_config_descr(fc, st, &pb);
00930             if (st->codec->codec_id == CODEC_ID_AAC &&
00931                 st->codec->extradata_size > 0)
00932                 st->need_parsing = 0;
00933         }
00934         break;
00935     case 0x56: /* DVB teletext descriptor */
00936         language[0] = get8(pp, desc_end);
00937         language[1] = get8(pp, desc_end);
00938         language[2] = get8(pp, desc_end);
00939         language[3] = 0;
00940         av_dict_set(&st->metadata, "language", language, 0);
00941         break;
00942     case 0x59: /* subtitling descriptor */
00943         language[0] = get8(pp, desc_end);
00944         language[1] = get8(pp, desc_end);
00945         language[2] = get8(pp, desc_end);
00946         language[3] = 0;
00947         /* hearing impaired subtitles detection */
00948         switch(get8(pp, desc_end)) {
00949         case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
00950         case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
00951         case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
00952         case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
00953         case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
00954         case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
00955             st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
00956             break;
00957         }
00958         if (st->codec->extradata) {
00959             if (st->codec->extradata_size == 4 && memcmp(st->codec->extradata, *pp, 4))
00960                 av_log_ask_for_sample(fc, "DVB sub with multiple IDs\n");
00961         } else {
00962             st->codec->extradata = av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
00963             if (st->codec->extradata) {
00964                 st->codec->extradata_size = 4;
00965                 memcpy(st->codec->extradata, *pp, 4);
00966             }
00967         }
00968         *pp += 4;
00969         av_dict_set(&st->metadata, "language", language, 0);
00970         break;
00971     case 0x0a: /* ISO 639 language descriptor */
00972         for (i = 0; i + 4 <= desc_len; i += 4) {
00973             language[i + 0] = get8(pp, desc_end);
00974             language[i + 1] = get8(pp, desc_end);
00975             language[i + 2] = get8(pp, desc_end);
00976             language[i + 3] = ',';
00977         switch (get8(pp, desc_end)) {
00978             case 0x01: st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS; break;
00979             case 0x02: st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; break;
00980             case 0x03: st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED; break;
00981         }
00982         }
00983         if (i) {
00984             language[i - 1] = 0;
00985             av_dict_set(&st->metadata, "language", language, 0);
00986         }
00987         break;
00988     case 0x05: /* registration descriptor */
00989         st->codec->codec_tag = bytestream_get_le32(pp);
00990         av_dlog(fc, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
00991         if (st->codec->codec_id == CODEC_ID_NONE &&
00992             stream_type == STREAM_TYPE_PRIVATE_DATA)
00993             mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
00994         break;
00995     default:
00996         break;
00997     }
00998     *pp = desc_end;
00999     return 0;
01000 }
01001 
01002 static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
01003 {
01004     MpegTSContext *ts = filter->u.section_filter.opaque;
01005     SectionHeader h1, *h = &h1;
01006     PESContext *pes;
01007     AVStream *st;
01008     const uint8_t *p, *p_end, *desc_list_end;
01009     int program_info_length, pcr_pid, pid, stream_type;
01010     int desc_list_len;
01011     uint32_t prog_reg_desc = 0; /* registration descriptor */
01012     uint8_t *mp4_dec_config_descr = NULL;
01013     int mp4_dec_config_descr_len = 0;
01014     int mp4_es_id = 0;
01015 
01016     av_dlog(ts->stream, "PMT: len %i\n", section_len);
01017     hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
01018 
01019     p_end = section + section_len - 4;
01020     p = section;
01021     if (parse_section_header(h, &p, p_end) < 0)
01022         return;
01023 
01024     av_dlog(ts->stream, "sid=0x%x sec_num=%d/%d\n",
01025            h->id, h->sec_num, h->last_sec_num);
01026 
01027     if (h->tid != PMT_TID)
01028         return;
01029 
01030     clear_program(ts, h->id);
01031     pcr_pid = get16(&p, p_end) & 0x1fff;
01032     if (pcr_pid < 0)
01033         return;
01034     add_pid_to_pmt(ts, h->id, pcr_pid);
01035 
01036     av_dlog(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
01037 
01038     program_info_length = get16(&p, p_end) & 0xfff;
01039     if (program_info_length < 0)
01040         return;
01041     while(program_info_length >= 2) {
01042         uint8_t tag, len;
01043         tag = get8(&p, p_end);
01044         len = get8(&p, p_end);
01045 
01046         av_dlog(ts->stream, "program tag: 0x%02x len=%d\n", tag, len);
01047 
01048         if(len > program_info_length - 2)
01049             //something else is broken, exit the program_descriptors_loop
01050             break;
01051         program_info_length -= len + 2;
01052         if (tag == 0x1d) { // IOD descriptor
01053             get8(&p, p_end); // scope
01054             get8(&p, p_end); // label
01055             len -= 2;
01056             mp4_read_iods(ts->stream, p, len, &mp4_es_id,
01057                           &mp4_dec_config_descr, &mp4_dec_config_descr_len);
01058         } else if (tag == 0x05 && len >= 4) { // registration descriptor
01059             prog_reg_desc = bytestream_get_le32(&p);
01060             len -= 4;
01061         }
01062         p += len;
01063     }
01064     p += program_info_length;
01065     if (p >= p_end)
01066         goto out;
01067 
01068     // stop parsing after pmt, we found header
01069     if (!ts->stream->nb_streams)
01070         ts->stop_parse = 1;
01071 
01072     for(;;) {
01073         st = 0;
01074         stream_type = get8(&p, p_end);
01075         if (stream_type < 0)
01076             break;
01077         pid = get16(&p, p_end) & 0x1fff;
01078         if (pid < 0)
01079             break;
01080 
01081         /* now create ffmpeg stream */
01082         if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
01083             pes = ts->pids[pid]->u.pes_filter.opaque;
01084             if (!pes->st)
01085                 pes->st = av_new_stream(pes->stream, pes->pid);
01086             st = pes->st;
01087         } else {
01088             if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
01089             pes = add_pes_stream(ts, pid, pcr_pid);
01090             if (pes)
01091                 st = av_new_stream(pes->stream, pes->pid);
01092         }
01093 
01094         if (!st)
01095             goto out;
01096 
01097         if (!pes->stream_type)
01098             mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
01099 
01100         add_pid_to_pmt(ts, h->id, pid);
01101 
01102         ff_program_add_stream_index(ts->stream, h->id, st->index);
01103 
01104         desc_list_len = get16(&p, p_end) & 0xfff;
01105         if (desc_list_len < 0)
01106             break;
01107         desc_list_end = p + desc_list_len;
01108         if (desc_list_end > p_end)
01109             break;
01110         for(;;) {
01111             if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, desc_list_end,
01112                 mp4_dec_config_descr_len, mp4_es_id, pid, mp4_dec_config_descr) < 0)
01113                 break;
01114 
01115             if (prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) {
01116                 ff_program_add_stream_index(ts->stream, h->id, pes->sub_st->index);
01117                 pes->sub_st->codec->codec_tag = st->codec->codec_tag;
01118             }
01119         }
01120         p = desc_list_end;
01121     }
01122 
01123  out:
01124     av_free(mp4_dec_config_descr);
01125 }
01126 
01127 static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
01128 {
01129     MpegTSContext *ts = filter->u.section_filter.opaque;
01130     SectionHeader h1, *h = &h1;
01131     const uint8_t *p, *p_end;
01132     int sid, pmt_pid;
01133 
01134     av_dlog(ts->stream, "PAT:\n");
01135     hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
01136 
01137     p_end = section + section_len - 4;
01138     p = section;
01139     if (parse_section_header(h, &p, p_end) < 0)
01140         return;
01141     if (h->tid != PAT_TID)
01142         return;
01143 
01144     clear_programs(ts);
01145     for(;;) {
01146         sid = get16(&p, p_end);
01147         if (sid < 0)
01148             break;
01149         pmt_pid = get16(&p, p_end) & 0x1fff;
01150         if (pmt_pid < 0)
01151             break;
01152 
01153         av_dlog(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
01154 
01155         if (sid == 0x0000) {
01156             /* NIT info */
01157         } else {
01158             av_new_program(ts->stream, sid);
01159             if (ts->pids[pmt_pid])
01160                 mpegts_close_filter(ts, ts->pids[pmt_pid]);
01161             mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
01162             add_pat_entry(ts, sid);
01163             add_pid_to_pmt(ts, sid, 0); //add pat pid to program
01164             add_pid_to_pmt(ts, sid, pmt_pid);
01165         }
01166     }
01167 }
01168 
01169 static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
01170 {
01171     MpegTSContext *ts = filter->u.section_filter.opaque;
01172     SectionHeader h1, *h = &h1;
01173     const uint8_t *p, *p_end, *desc_list_end, *desc_end;
01174     int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
01175     char *name, *provider_name;
01176 
01177     av_dlog(ts->stream, "SDT:\n");
01178     hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
01179 
01180     p_end = section + section_len - 4;
01181     p = section;
01182     if (parse_section_header(h, &p, p_end) < 0)
01183         return;
01184     if (h->tid != SDT_TID)
01185         return;
01186     onid = get16(&p, p_end);
01187     if (onid < 0)
01188         return;
01189     val = get8(&p, p_end);
01190     if (val < 0)
01191         return;
01192     for(;;) {
01193         sid = get16(&p, p_end);
01194         if (sid < 0)
01195             break;
01196         val = get8(&p, p_end);
01197         if (val < 0)
01198             break;
01199         desc_list_len = get16(&p, p_end) & 0xfff;
01200         if (desc_list_len < 0)
01201             break;
01202         desc_list_end = p + desc_list_len;
01203         if (desc_list_end > p_end)
01204             break;
01205         for(;;) {
01206             desc_tag = get8(&p, desc_list_end);
01207             if (desc_tag < 0)
01208                 break;
01209             desc_len = get8(&p, desc_list_end);
01210             desc_end = p + desc_len;
01211             if (desc_end > desc_list_end)
01212                 break;
01213 
01214             av_dlog(ts->stream, "tag: 0x%02x len=%d\n",
01215                    desc_tag, desc_len);
01216 
01217             switch(desc_tag) {
01218             case 0x48:
01219                 service_type = get8(&p, p_end);
01220                 if (service_type < 0)
01221                     break;
01222                 provider_name = getstr8(&p, p_end);
01223                 if (!provider_name)
01224                     break;
01225                 name = getstr8(&p, p_end);
01226                 if (name) {
01227                     AVProgram *program = av_new_program(ts->stream, sid);
01228                     if(program) {
01229                         av_dict_set(&program->metadata, "service_name", name, 0);
01230                         av_dict_set(&program->metadata, "service_provider", provider_name, 0);
01231                     }
01232                 }
01233                 av_free(name);
01234                 av_free(provider_name);
01235                 break;
01236             default:
01237                 break;
01238             }
01239             p = desc_end;
01240         }
01241         p = desc_list_end;
01242     }
01243 }
01244 
01245 /* handle one TS packet */
01246 static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
01247 {
01248     AVFormatContext *s = ts->stream;
01249     MpegTSFilter *tss;
01250     int len, pid, cc, expected_cc, cc_ok, afc, is_start;
01251     const uint8_t *p, *p_end;
01252     int64_t pos;
01253 
01254     pid = AV_RB16(packet + 1) & 0x1fff;
01255     if(pid && discard_pid(ts, pid))
01256         return 0;
01257     is_start = packet[1] & 0x40;
01258     tss = ts->pids[pid];
01259     if (ts->auto_guess && tss == NULL && is_start) {
01260         add_pes_stream(ts, pid, -1);
01261         tss = ts->pids[pid];
01262     }
01263     if (!tss)
01264         return 0;
01265 
01266     /* continuity check (currently not used) */
01267     cc = (packet[3] & 0xf);
01268     expected_cc = (packet[3] & 0x10) ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
01269     cc_ok = (tss->last_cc < 0) || (expected_cc == cc);
01270     tss->last_cc = cc;
01271 
01272     /* skip adaptation field */
01273     afc = (packet[3] >> 4) & 3;
01274     p = packet + 4;
01275     if (afc == 0) /* reserved value */
01276         return 0;
01277     if (afc == 2) /* adaptation field only */
01278         return 0;
01279     if (afc == 3) {
01280         /* skip adapation field */
01281         p += p[0] + 1;
01282     }
01283     /* if past the end of packet, ignore */
01284     p_end = packet + TS_PACKET_SIZE;
01285     if (p >= p_end)
01286         return 0;
01287 
01288     pos = avio_tell(ts->stream->pb);
01289     ts->pos47= pos % ts->raw_packet_size;
01290 
01291     if (tss->type == MPEGTS_SECTION) {
01292         if (is_start) {
01293             /* pointer field present */
01294             len = *p++;
01295             if (p + len > p_end)
01296                 return 0;
01297             if (len && cc_ok) {
01298                 /* write remaining section bytes */
01299                 write_section_data(s, tss,
01300                                    p, len, 0);
01301                 /* check whether filter has been closed */
01302                 if (!ts->pids[pid])
01303                     return 0;
01304             }
01305             p += len;
01306             if (p < p_end) {
01307                 write_section_data(s, tss,
01308                                    p, p_end - p, 1);
01309             }
01310         } else {
01311             if (cc_ok) {
01312                 write_section_data(s, tss,
01313                                    p, p_end - p, 0);
01314             }
01315         }
01316     } else {
01317         int ret;
01318         // Note: The position here points actually behind the current packet.
01319         if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
01320                                             pos - ts->raw_packet_size)) < 0)
01321             return ret;
01322     }
01323 
01324     return 0;
01325 }
01326 
01327 /* XXX: try to find a better synchro over several packets (use
01328    get_packet_size() ?) */
01329 static int mpegts_resync(AVFormatContext *s)
01330 {
01331     AVIOContext *pb = s->pb;
01332     int c, i;
01333 
01334     for(i = 0;i < MAX_RESYNC_SIZE; i++) {
01335         c = avio_r8(pb);
01336         if (pb->eof_reached)
01337             return -1;
01338         if (c == 0x47) {
01339             avio_seek(pb, -1, SEEK_CUR);
01340             return 0;
01341         }
01342     }
01343     av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
01344     /* no sync found */
01345     return -1;
01346 }
01347 
01348 /* return -1 if error or EOF. Return 0 if OK. */
01349 static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size)
01350 {
01351     AVIOContext *pb = s->pb;
01352     int skip, len;
01353 
01354     for(;;) {
01355         len = avio_read(pb, buf, TS_PACKET_SIZE);
01356         if (len != TS_PACKET_SIZE)
01357             return len < 0 ? len : AVERROR_EOF;
01358         /* check paquet sync byte */
01359         if (buf[0] != 0x47) {
01360             /* find a new packet start */
01361             avio_seek(pb, -TS_PACKET_SIZE, SEEK_CUR);
01362             if (mpegts_resync(s) < 0)
01363                 return AVERROR(EAGAIN);
01364             else
01365                 continue;
01366         } else {
01367             skip = raw_packet_size - TS_PACKET_SIZE;
01368             if (skip > 0)
01369                 avio_skip(pb, skip);
01370             break;
01371         }
01372     }
01373     return 0;
01374 }
01375 
01376 static int handle_packets(MpegTSContext *ts, int nb_packets)
01377 {
01378     AVFormatContext *s = ts->stream;
01379     uint8_t packet[TS_PACKET_SIZE];
01380     int packet_num, ret;
01381 
01382     ts->stop_parse = 0;
01383     packet_num = 0;
01384     for(;;) {
01385         if (ts->stop_parse>0)
01386             break;
01387         packet_num++;
01388         if (nb_packets != 0 && packet_num >= nb_packets)
01389             break;
01390         ret = read_packet(s, packet, ts->raw_packet_size);
01391         if (ret != 0)
01392             return ret;
01393         ret = handle_packet(ts, packet);
01394         if (ret != 0)
01395             return ret;
01396     }
01397     return 0;
01398 }
01399 
01400 static int mpegts_probe(AVProbeData *p)
01401 {
01402 #if 1
01403     const int size= p->buf_size;
01404     int score, fec_score, dvhs_score;
01405     int check_count= size / TS_FEC_PACKET_SIZE;
01406 #define CHECK_COUNT 10
01407 
01408     if (check_count < CHECK_COUNT)
01409         return -1;
01410 
01411     score     = analyze(p->buf, TS_PACKET_SIZE     *check_count, TS_PACKET_SIZE     , NULL)*CHECK_COUNT/check_count;
01412     dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
01413     fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
01414 //    av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
01415 
01416 // we need a clear definition for the returned score otherwise things will become messy sooner or later
01417     if     (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score     - CHECK_COUNT;
01418     else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score  - CHECK_COUNT;
01419     else if(                 fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
01420     else                                    return -1;
01421 #else
01422     /* only use the extension for safer guess */
01423     if (av_match_ext(p->filename, "ts"))
01424         return AVPROBE_SCORE_MAX;
01425     else
01426         return 0;
01427 #endif
01428 }
01429 
01430 /* return the 90kHz PCR and the extension for the 27MHz PCR. return
01431    (-1) if not available */
01432 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
01433                      const uint8_t *packet)
01434 {
01435     int afc, len, flags;
01436     const uint8_t *p;
01437     unsigned int v;
01438 
01439     afc = (packet[3] >> 4) & 3;
01440     if (afc <= 1)
01441         return -1;
01442     p = packet + 4;
01443     len = p[0];
01444     p++;
01445     if (len == 0)
01446         return -1;
01447     flags = *p++;
01448     len--;
01449     if (!(flags & 0x10))
01450         return -1;
01451     if (len < 6)
01452         return -1;
01453     v = AV_RB32(p);
01454     *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
01455     *ppcr_low = ((p[4] & 1) << 8) | p[5];
01456     return 0;
01457 }
01458 
01459 static int mpegts_read_header(AVFormatContext *s,
01460                               AVFormatParameters *ap)
01461 {
01462     MpegTSContext *ts = s->priv_data;
01463     AVIOContext *pb = s->pb;
01464     uint8_t buf[5*1024];
01465     int len;
01466     int64_t pos;
01467 
01468 #if FF_API_FORMAT_PARAMETERS
01469     if (ap) {
01470         if (ap->mpeg2ts_compute_pcr)
01471             ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
01472         if(ap->mpeg2ts_raw){
01473             av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
01474             return -1;
01475         }
01476     }
01477 #endif
01478 
01479     /* read the first 1024 bytes to get packet size */
01480     pos = avio_tell(pb);
01481     len = avio_read(pb, buf, sizeof(buf));
01482     if (len != sizeof(buf))
01483         goto fail;
01484     ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
01485     if (ts->raw_packet_size <= 0)
01486         goto fail;
01487     ts->stream = s;
01488     ts->auto_guess = 0;
01489 
01490     if (s->iformat == &ff_mpegts_demuxer) {
01491         /* normal demux */
01492 
01493         /* first do a scaning to get all the services */
01494         if (avio_seek(pb, pos, SEEK_SET) < 0)
01495             av_log(s, AV_LOG_ERROR, "Unable to seek back to the start\n");
01496 
01497         mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
01498 
01499         mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
01500 
01501         handle_packets(ts, s->probesize / ts->raw_packet_size);
01502         /* if could not find service, enable auto_guess */
01503 
01504         ts->auto_guess = 1;
01505 
01506         av_dlog(ts->stream, "tuning done\n");
01507 
01508         s->ctx_flags |= AVFMTCTX_NOHEADER;
01509     } else {
01510         AVStream *st;
01511         int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
01512         int64_t pcrs[2], pcr_h;
01513         int packet_count[2];
01514         uint8_t packet[TS_PACKET_SIZE];
01515 
01516         /* only read packets */
01517 
01518         st = av_new_stream(s, 0);
01519         if (!st)
01520             goto fail;
01521         av_set_pts_info(st, 60, 1, 27000000);
01522         st->codec->codec_type = AVMEDIA_TYPE_DATA;
01523         st->codec->codec_id = CODEC_ID_MPEG2TS;
01524 
01525         /* we iterate until we find two PCRs to estimate the bitrate */
01526         pcr_pid = -1;
01527         nb_pcrs = 0;
01528         nb_packets = 0;
01529         for(;;) {
01530             ret = read_packet(s, packet, ts->raw_packet_size);
01531             if (ret < 0)
01532                 return -1;
01533             pid = AV_RB16(packet + 1) & 0x1fff;
01534             if ((pcr_pid == -1 || pcr_pid == pid) &&
01535                 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
01536                 pcr_pid = pid;
01537                 packet_count[nb_pcrs] = nb_packets;
01538                 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
01539                 nb_pcrs++;
01540                 if (nb_pcrs >= 2)
01541                     break;
01542             }
01543             nb_packets++;
01544         }
01545 
01546         /* NOTE1: the bitrate is computed without the FEC */
01547         /* NOTE2: it is only the bitrate of the start of the stream */
01548         ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
01549         ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
01550         s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
01551         st->codec->bit_rate = s->bit_rate;
01552         st->start_time = ts->cur_pcr;
01553         av_dlog(ts->stream, "start=%0.3f pcr=%0.3f incr=%d\n",
01554                 st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
01555     }
01556 
01557     avio_seek(pb, pos, SEEK_SET);
01558     return 0;
01559  fail:
01560     return -1;
01561 }
01562 
01563 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
01564 
01565 static int mpegts_raw_read_packet(AVFormatContext *s,
01566                                   AVPacket *pkt)
01567 {
01568     MpegTSContext *ts = s->priv_data;
01569     int ret, i;
01570     int64_t pcr_h, next_pcr_h, pos;
01571     int pcr_l, next_pcr_l;
01572     uint8_t pcr_buf[12];
01573 
01574     if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
01575         return AVERROR(ENOMEM);
01576     pkt->pos= avio_tell(s->pb);
01577     ret = read_packet(s, pkt->data, ts->raw_packet_size);
01578     if (ret < 0) {
01579         av_free_packet(pkt);
01580         return ret;
01581     }
01582     if (ts->mpeg2ts_compute_pcr) {
01583         /* compute exact PCR for each packet */
01584         if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
01585             /* we read the next PCR (XXX: optimize it by using a bigger buffer */
01586             pos = avio_tell(s->pb);
01587             for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
01588                 avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
01589                 avio_read(s->pb, pcr_buf, 12);
01590                 if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
01591                     /* XXX: not precise enough */
01592                     ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
01593                         (i + 1);
01594                     break;
01595                 }
01596             }
01597             avio_seek(s->pb, pos, SEEK_SET);
01598             /* no next PCR found: we use previous increment */
01599             ts->cur_pcr = pcr_h * 300 + pcr_l;
01600         }
01601         pkt->pts = ts->cur_pcr;
01602         pkt->duration = ts->pcr_incr;
01603         ts->cur_pcr += ts->pcr_incr;
01604     }
01605     pkt->stream_index = 0;
01606     return 0;
01607 }
01608 
01609 static int mpegts_read_packet(AVFormatContext *s,
01610                               AVPacket *pkt)
01611 {
01612     MpegTSContext *ts = s->priv_data;
01613     int ret, i;
01614 
01615     if (avio_tell(s->pb) != ts->last_pos) {
01616         /* seek detected, flush pes buffer */
01617         for (i = 0; i < NB_PID_MAX; i++) {
01618             if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
01619                 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
01620                 av_freep(&pes->buffer);
01621                 pes->data_index = 0;
01622                 pes->state = MPEGTS_SKIP; /* skip until pes header */
01623             }
01624         }
01625     }
01626 
01627     ts->pkt = pkt;
01628     ret = handle_packets(ts, 0);
01629     if (ret < 0) {
01630         /* flush pes data left */
01631         for (i = 0; i < NB_PID_MAX; i++) {
01632             if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
01633                 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
01634                 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
01635                     new_pes_packet(pes, pkt);
01636                     pes->state = MPEGTS_SKIP;
01637                     ret = 0;
01638                     break;
01639                 }
01640             }
01641         }
01642     }
01643 
01644     ts->last_pos = avio_tell(s->pb);
01645 
01646     return ret;
01647 }
01648 
01649 static int mpegts_read_close(AVFormatContext *s)
01650 {
01651     MpegTSContext *ts = s->priv_data;
01652     int i;
01653 
01654     clear_programs(ts);
01655 
01656     for(i=0;i<NB_PID_MAX;i++)
01657         if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
01658 
01659     return 0;
01660 }
01661 
01662 static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
01663                               int64_t *ppos, int64_t pos_limit)
01664 {
01665     MpegTSContext *ts = s->priv_data;
01666     int64_t pos, timestamp;
01667     uint8_t buf[TS_PACKET_SIZE];
01668     int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
01669     const int find_next= 1;
01670     pos = ((*ppos  + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
01671     if (find_next) {
01672         for(;;) {
01673             avio_seek(s->pb, pos, SEEK_SET);
01674             if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
01675                 return AV_NOPTS_VALUE;
01676             if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
01677                 parse_pcr(&timestamp, &pcr_l, buf) == 0) {
01678                 break;
01679             }
01680             pos += ts->raw_packet_size;
01681         }
01682     } else {
01683         for(;;) {
01684             pos -= ts->raw_packet_size;
01685             if (pos < 0)
01686                 return AV_NOPTS_VALUE;
01687             avio_seek(s->pb, pos, SEEK_SET);
01688             if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
01689                 return AV_NOPTS_VALUE;
01690             if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
01691                 parse_pcr(&timestamp, &pcr_l, buf) == 0) {
01692                 break;
01693             }
01694         }
01695     }
01696     *ppos = pos;
01697 
01698     return timestamp;
01699 }
01700 
01701 #ifdef USE_SYNCPOINT_SEARCH
01702 
01703 static int read_seek2(AVFormatContext *s,
01704                       int stream_index,
01705                       int64_t min_ts,
01706                       int64_t target_ts,
01707                       int64_t max_ts,
01708                       int flags)
01709 {
01710     int64_t pos;
01711 
01712     int64_t ts_ret, ts_adj;
01713     int stream_index_gen_search;
01714     AVStream *st;
01715     AVParserState *backup;
01716 
01717     backup = ff_store_parser_state(s);
01718 
01719     // detect direction of seeking for search purposes
01720     flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ?
01721              AVSEEK_FLAG_BACKWARD : 0;
01722 
01723     if (flags & AVSEEK_FLAG_BYTE) {
01724         // use position directly, we will search starting from it
01725         pos = target_ts;
01726     } else {
01727         // search for some position with good timestamp match
01728         if (stream_index < 0) {
01729             stream_index_gen_search = av_find_default_stream_index(s);
01730             if (stream_index_gen_search < 0) {
01731                 ff_restore_parser_state(s, backup);
01732                 return -1;
01733             }
01734 
01735             st = s->streams[stream_index_gen_search];
01736             // timestamp for default must be expressed in AV_TIME_BASE units
01737             ts_adj = av_rescale(target_ts,
01738                                 st->time_base.den,
01739                                 AV_TIME_BASE * (int64_t)st->time_base.num);
01740         } else {
01741             ts_adj = target_ts;
01742             stream_index_gen_search = stream_index;
01743         }
01744         pos = av_gen_search(s, stream_index_gen_search, ts_adj,
01745                             0, INT64_MAX, -1,
01746                             AV_NOPTS_VALUE,
01747                             AV_NOPTS_VALUE,
01748                             flags, &ts_ret, mpegts_get_pcr);
01749         if (pos < 0) {
01750             ff_restore_parser_state(s, backup);
01751             return -1;
01752         }
01753     }
01754 
01755     // search for actual matching keyframe/starting position for all streams
01756     if (ff_gen_syncpoint_search(s, stream_index, pos,
01757                                 min_ts, target_ts, max_ts,
01758                                 flags) < 0) {
01759         ff_restore_parser_state(s, backup);
01760         return -1;
01761     }
01762 
01763     ff_free_parser_state(s, backup);
01764     return 0;
01765 }
01766 
01767 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
01768 {
01769     int ret;
01770     if (flags & AVSEEK_FLAG_BACKWARD) {
01771         flags &= ~AVSEEK_FLAG_BACKWARD;
01772         ret = read_seek2(s, stream_index, INT64_MIN, target_ts, target_ts, flags);
01773         if (ret < 0)
01774             // for compatibility reasons, seek to the best-fitting timestamp
01775             ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
01776     } else {
01777         ret = read_seek2(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
01778         if (ret < 0)
01779             // for compatibility reasons, seek to the best-fitting timestamp
01780             ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
01781     }
01782     return ret;
01783 }
01784 
01785 #else
01786 
01787 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
01788     MpegTSContext *ts = s->priv_data;
01789     uint8_t buf[TS_PACKET_SIZE];
01790     int64_t pos;
01791 
01792     if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
01793         return -1;
01794 
01795     pos= avio_tell(s->pb);
01796 
01797     for(;;) {
01798         avio_seek(s->pb, pos, SEEK_SET);
01799         if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
01800             return -1;
01801 //        pid = AV_RB16(buf + 1) & 0x1fff;
01802         if(buf[1] & 0x40) break;
01803         pos += ts->raw_packet_size;
01804     }
01805     avio_seek(s->pb, pos, SEEK_SET);
01806 
01807     return 0;
01808 }
01809 
01810 #endif
01811 
01812 /**************************************************************/
01813 /* parsing functions - called from other demuxers such as RTP */
01814 
01815 MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
01816 {
01817     MpegTSContext *ts;
01818 
01819     ts = av_mallocz(sizeof(MpegTSContext));
01820     if (!ts)
01821         return NULL;
01822     /* no stream case, currently used by RTP */
01823     ts->raw_packet_size = TS_PACKET_SIZE;
01824     ts->stream = s;
01825     ts->auto_guess = 1;
01826     return ts;
01827 }
01828 
01829 /* return the consumed length if a packet was output, or -1 if no
01830    packet is output */
01831 int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
01832                         const uint8_t *buf, int len)
01833 {
01834     int len1;
01835 
01836     len1 = len;
01837     ts->pkt = pkt;
01838     ts->stop_parse = 0;
01839     for(;;) {
01840         if (ts->stop_parse>0)
01841             break;
01842         if (len < TS_PACKET_SIZE)
01843             return -1;
01844         if (buf[0] != 0x47) {
01845             buf++;
01846             len--;
01847         } else {
01848             handle_packet(ts, buf);
01849             buf += TS_PACKET_SIZE;
01850             len -= TS_PACKET_SIZE;
01851         }
01852     }
01853     return len1 - len;
01854 }
01855 
01856 void ff_mpegts_parse_close(MpegTSContext *ts)
01857 {
01858     int i;
01859 
01860     for(i=0;i<NB_PID_MAX;i++)
01861         av_free(ts->pids[i]);
01862     av_free(ts);
01863 }
01864 
01865 AVInputFormat ff_mpegts_demuxer = {
01866     "mpegts",
01867     NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
01868     sizeof(MpegTSContext),
01869     mpegts_probe,
01870     mpegts_read_header,
01871     mpegts_read_packet,
01872     mpegts_read_close,
01873     read_seek,
01874     mpegts_get_pcr,
01875     .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
01876 #ifdef USE_SYNCPOINT_SEARCH
01877     .read_seek2 = read_seek2,
01878 #endif
01879 };
01880 
01881 AVInputFormat ff_mpegtsraw_demuxer = {
01882     "mpegtsraw",
01883     NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
01884     sizeof(MpegTSContext),
01885     NULL,
01886     mpegts_read_header,
01887     mpegts_raw_read_packet,
01888     mpegts_read_close,
01889     read_seek,
01890     mpegts_get_pcr,
01891     .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
01892 #ifdef USE_SYNCPOINT_SEARCH
01893     .read_seek2 = read_seek2,
01894 #endif
01895     .priv_class = &mpegtsraw_class,
01896 };