Libav

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