• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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

Generated on Wed Apr 11 2012 07:31:36 for FFmpeg by  doxygen 1.7.1