• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • 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 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, 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     cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
01162     tss->last_cc = cc;
01163 
01164     /* skip adaptation field */
01165     afc = (packet[3] >> 4) & 3;
01166     p = packet + 4;
01167     if (afc == 0) /* reserved value */
01168         return 0;
01169     if (afc == 2) /* adaptation field only */
01170         return 0;
01171     if (afc == 3) {
01172         /* skip adapation field */
01173         p += p[0] + 1;
01174     }
01175     /* if past the end of packet, ignore */
01176     p_end = packet + TS_PACKET_SIZE;
01177     if (p >= p_end)
01178         return 0;
01179 
01180     pos = url_ftell(ts->stream->pb);
01181     ts->pos47= pos % ts->raw_packet_size;
01182 
01183     if (tss->type == MPEGTS_SECTION) {
01184         if (is_start) {
01185             /* pointer field present */
01186             len = *p++;
01187             if (p + len > p_end)
01188                 return 0;
01189             if (len && cc_ok) {
01190                 /* write remaining section bytes */
01191                 write_section_data(s, tss,
01192                                    p, len, 0);
01193                 /* check whether filter has been closed */
01194                 if (!ts->pids[pid])
01195                     return 0;
01196             }
01197             p += len;
01198             if (p < p_end) {
01199                 write_section_data(s, tss,
01200                                    p, p_end - p, 1);
01201             }
01202         } else {
01203             if (cc_ok) {
01204                 write_section_data(s, tss,
01205                                    p, p_end - p, 0);
01206             }
01207         }
01208     } else {
01209         int ret;
01210         // Note: The position here points actually behind the current packet.
01211         if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
01212                                             pos - ts->raw_packet_size)) < 0)
01213             return ret;
01214     }
01215 
01216     return 0;
01217 }
01218 
01219 /* XXX: try to find a better synchro over several packets (use
01220    get_packet_size() ?) */
01221 static int mpegts_resync(AVFormatContext *s)
01222 {
01223     ByteIOContext *pb = s->pb;
01224     int c, i;
01225 
01226     for(i = 0;i < MAX_RESYNC_SIZE; i++) {
01227         c = url_fgetc(pb);
01228         if (c < 0)
01229             return -1;
01230         if (c == 0x47) {
01231             url_fseek(pb, -1, SEEK_CUR);
01232             return 0;
01233         }
01234     }
01235     av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
01236     /* no sync found */
01237     return -1;
01238 }
01239 
01240 /* return -1 if error or EOF. Return 0 if OK. */
01241 static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size)
01242 {
01243     ByteIOContext *pb = s->pb;
01244     int skip, len;
01245 
01246     for(;;) {
01247         len = get_buffer(pb, buf, TS_PACKET_SIZE);
01248         if (len != TS_PACKET_SIZE)
01249             return AVERROR(EIO);
01250         /* check paquet sync byte */
01251         if (buf[0] != 0x47) {
01252             /* find a new packet start */
01253             url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
01254             if (mpegts_resync(s) < 0)
01255                 return AVERROR(EAGAIN);
01256             else
01257                 continue;
01258         } else {
01259             skip = raw_packet_size - TS_PACKET_SIZE;
01260             if (skip > 0)
01261                 url_fskip(pb, skip);
01262             break;
01263         }
01264     }
01265     return 0;
01266 }
01267 
01268 static int handle_packets(MpegTSContext *ts, int nb_packets)
01269 {
01270     AVFormatContext *s = ts->stream;
01271     uint8_t packet[TS_PACKET_SIZE];
01272     int packet_num, ret;
01273 
01274     ts->stop_parse = 0;
01275     packet_num = 0;
01276     for(;;) {
01277         if (ts->stop_parse>0)
01278             break;
01279         packet_num++;
01280         if (nb_packets != 0 && packet_num >= nb_packets)
01281             break;
01282         ret = read_packet(s, packet, ts->raw_packet_size);
01283         if (ret != 0)
01284             return ret;
01285         ret = handle_packet(ts, packet);
01286         if (ret != 0)
01287             return ret;
01288     }
01289     return 0;
01290 }
01291 
01292 static int mpegts_probe(AVProbeData *p)
01293 {
01294 #if 1
01295     const int size= p->buf_size;
01296     int score, fec_score, dvhs_score;
01297     int check_count= size / TS_FEC_PACKET_SIZE;
01298 #define CHECK_COUNT 10
01299 
01300     if (check_count < CHECK_COUNT)
01301         return -1;
01302 
01303     score     = analyze(p->buf, TS_PACKET_SIZE     *check_count, TS_PACKET_SIZE     , NULL)*CHECK_COUNT/check_count;
01304     dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
01305     fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
01306 //    av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
01307 
01308 // we need a clear definition for the returned score otherwise things will become messy sooner or later
01309     if     (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score     - CHECK_COUNT;
01310     else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score  - CHECK_COUNT;
01311     else if(                 fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
01312     else                                    return -1;
01313 #else
01314     /* only use the extension for safer guess */
01315     if (av_match_ext(p->filename, "ts"))
01316         return AVPROBE_SCORE_MAX;
01317     else
01318         return 0;
01319 #endif
01320 }
01321 
01322 /* return the 90kHz PCR and the extension for the 27MHz PCR. return
01323    (-1) if not available */
01324 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
01325                      const uint8_t *packet)
01326 {
01327     int afc, len, flags;
01328     const uint8_t *p;
01329     unsigned int v;
01330 
01331     afc = (packet[3] >> 4) & 3;
01332     if (afc <= 1)
01333         return -1;
01334     p = packet + 4;
01335     len = p[0];
01336     p++;
01337     if (len == 0)
01338         return -1;
01339     flags = *p++;
01340     len--;
01341     if (!(flags & 0x10))
01342         return -1;
01343     if (len < 6)
01344         return -1;
01345     v = AV_RB32(p);
01346     *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
01347     *ppcr_low = ((p[4] & 1) << 8) | p[5];
01348     return 0;
01349 }
01350 
01351 static int mpegts_read_header(AVFormatContext *s,
01352                               AVFormatParameters *ap)
01353 {
01354     MpegTSContext *ts = s->priv_data;
01355     ByteIOContext *pb = s->pb;
01356     uint8_t buf[5*1024];
01357     int len;
01358     int64_t pos;
01359 
01360     if (ap) {
01361         ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
01362         if(ap->mpeg2ts_raw){
01363             av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
01364             return -1;
01365         }
01366     }
01367 
01368     /* read the first 1024 bytes to get packet size */
01369     pos = url_ftell(pb);
01370     len = get_buffer(pb, buf, sizeof(buf));
01371     if (len != sizeof(buf))
01372         goto fail;
01373     ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
01374     if (ts->raw_packet_size <= 0)
01375         goto fail;
01376     ts->stream = s;
01377     ts->auto_guess = 0;
01378 
01379     if (s->iformat == &mpegts_demuxer) {
01380         /* normal demux */
01381 
01382         /* first do a scaning to get all the services */
01383         url_fseek(pb, pos, SEEK_SET);
01384 
01385         mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
01386 
01387         mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
01388 
01389         handle_packets(ts, s->probesize / ts->raw_packet_size);
01390         /* if could not find service, enable auto_guess */
01391 
01392         ts->auto_guess = 1;
01393 
01394         dprintf(ts->stream, "tuning done\n");
01395 
01396         s->ctx_flags |= AVFMTCTX_NOHEADER;
01397     } else {
01398         AVStream *st;
01399         int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
01400         int64_t pcrs[2], pcr_h;
01401         int packet_count[2];
01402         uint8_t packet[TS_PACKET_SIZE];
01403 
01404         /* only read packets */
01405 
01406         st = av_new_stream(s, 0);
01407         if (!st)
01408             goto fail;
01409         av_set_pts_info(st, 60, 1, 27000000);
01410         st->codec->codec_type = AVMEDIA_TYPE_DATA;
01411         st->codec->codec_id = CODEC_ID_MPEG2TS;
01412 
01413         /* we iterate until we find two PCRs to estimate the bitrate */
01414         pcr_pid = -1;
01415         nb_pcrs = 0;
01416         nb_packets = 0;
01417         for(;;) {
01418             ret = read_packet(s, packet, ts->raw_packet_size);
01419             if (ret < 0)
01420                 return -1;
01421             pid = AV_RB16(packet + 1) & 0x1fff;
01422             if ((pcr_pid == -1 || pcr_pid == pid) &&
01423                 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
01424                 pcr_pid = pid;
01425                 packet_count[nb_pcrs] = nb_packets;
01426                 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
01427                 nb_pcrs++;
01428                 if (nb_pcrs >= 2)
01429                     break;
01430             }
01431             nb_packets++;
01432         }
01433 
01434         /* NOTE1: the bitrate is computed without the FEC */
01435         /* NOTE2: it is only the bitrate of the start of the stream */
01436         ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
01437         ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
01438         s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
01439         st->codec->bit_rate = s->bit_rate;
01440         st->start_time = ts->cur_pcr;
01441 #if 0
01442         av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
01443                st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
01444 #endif
01445     }
01446 
01447     url_fseek(pb, pos, SEEK_SET);
01448     return 0;
01449  fail:
01450     return -1;
01451 }
01452 
01453 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
01454 
01455 static int mpegts_raw_read_packet(AVFormatContext *s,
01456                                   AVPacket *pkt)
01457 {
01458     MpegTSContext *ts = s->priv_data;
01459     int ret, i;
01460     int64_t pcr_h, next_pcr_h, pos;
01461     int pcr_l, next_pcr_l;
01462     uint8_t pcr_buf[12];
01463 
01464     if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
01465         return AVERROR(ENOMEM);
01466     pkt->pos= url_ftell(s->pb);
01467     ret = read_packet(s, pkt->data, ts->raw_packet_size);
01468     if (ret < 0) {
01469         av_free_packet(pkt);
01470         return ret;
01471     }
01472     if (ts->mpeg2ts_compute_pcr) {
01473         /* compute exact PCR for each packet */
01474         if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
01475             /* we read the next PCR (XXX: optimize it by using a bigger buffer */
01476             pos = url_ftell(s->pb);
01477             for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
01478                 url_fseek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
01479                 get_buffer(s->pb, pcr_buf, 12);
01480                 if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
01481                     /* XXX: not precise enough */
01482                     ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
01483                         (i + 1);
01484                     break;
01485                 }
01486             }
01487             url_fseek(s->pb, pos, SEEK_SET);
01488             /* no next PCR found: we use previous increment */
01489             ts->cur_pcr = pcr_h * 300 + pcr_l;
01490         }
01491         pkt->pts = ts->cur_pcr;
01492         pkt->duration = ts->pcr_incr;
01493         ts->cur_pcr += ts->pcr_incr;
01494     }
01495     pkt->stream_index = 0;
01496     return 0;
01497 }
01498 
01499 static int mpegts_read_packet(AVFormatContext *s,
01500                               AVPacket *pkt)
01501 {
01502     MpegTSContext *ts = s->priv_data;
01503     int ret, i;
01504 
01505     if (url_ftell(s->pb) != ts->last_pos) {
01506         /* seek detected, flush pes buffer */
01507         for (i = 0; i < NB_PID_MAX; i++) {
01508             if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
01509                 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
01510                 av_freep(&pes->buffer);
01511                 pes->data_index = 0;
01512                 pes->state = MPEGTS_SKIP; /* skip until pes header */
01513             }
01514         }
01515     }
01516 
01517     ts->pkt = pkt;
01518     ret = handle_packets(ts, 0);
01519     if (ret < 0) {
01520         /* flush pes data left */
01521         for (i = 0; i < NB_PID_MAX; i++) {
01522             if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
01523                 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
01524                 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
01525                     new_pes_packet(pes, pkt);
01526                     pes->state = MPEGTS_SKIP;
01527                     ret = 0;
01528                     break;
01529                 }
01530             }
01531         }
01532     }
01533 
01534     ts->last_pos = url_ftell(s->pb);
01535 
01536     return ret;
01537 }
01538 
01539 static int mpegts_read_close(AVFormatContext *s)
01540 {
01541     MpegTSContext *ts = s->priv_data;
01542     int i;
01543 
01544     clear_programs(ts);
01545 
01546     for(i=0;i<NB_PID_MAX;i++)
01547         if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
01548 
01549     return 0;
01550 }
01551 
01552 static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
01553                               int64_t *ppos, int64_t pos_limit)
01554 {
01555     MpegTSContext *ts = s->priv_data;
01556     int64_t pos, timestamp;
01557     uint8_t buf[TS_PACKET_SIZE];
01558     int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
01559     const int find_next= 1;
01560     pos = ((*ppos  + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
01561     if (find_next) {
01562         for(;;) {
01563             url_fseek(s->pb, pos, SEEK_SET);
01564             if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
01565                 return AV_NOPTS_VALUE;
01566             if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
01567                 parse_pcr(&timestamp, &pcr_l, buf) == 0) {
01568                 break;
01569             }
01570             pos += ts->raw_packet_size;
01571         }
01572     } else {
01573         for(;;) {
01574             pos -= ts->raw_packet_size;
01575             if (pos < 0)
01576                 return AV_NOPTS_VALUE;
01577             url_fseek(s->pb, pos, SEEK_SET);
01578             if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
01579                 return AV_NOPTS_VALUE;
01580             if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
01581                 parse_pcr(&timestamp, &pcr_l, buf) == 0) {
01582                 break;
01583             }
01584         }
01585     }
01586     *ppos = pos;
01587 
01588     return timestamp;
01589 }
01590 
01591 #ifdef USE_SYNCPOINT_SEARCH
01592 
01593 static int read_seek2(AVFormatContext *s,
01594                       int stream_index,
01595                       int64_t min_ts,
01596                       int64_t target_ts,
01597                       int64_t max_ts,
01598                       int flags)
01599 {
01600     int64_t pos;
01601 
01602     int64_t ts_ret, ts_adj;
01603     int stream_index_gen_search;
01604     AVStream *st;
01605     AVParserState *backup;
01606 
01607     backup = ff_store_parser_state(s);
01608 
01609     // detect direction of seeking for search purposes
01610     flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ?
01611              AVSEEK_FLAG_BACKWARD : 0;
01612 
01613     if (flags & AVSEEK_FLAG_BYTE) {
01614         // use position directly, we will search starting from it
01615         pos = target_ts;
01616     } else {
01617         // search for some position with good timestamp match
01618         if (stream_index < 0) {
01619             stream_index_gen_search = av_find_default_stream_index(s);
01620             if (stream_index_gen_search < 0) {
01621                 ff_restore_parser_state(s, backup);
01622                 return -1;
01623             }
01624 
01625             st = s->streams[stream_index_gen_search];
01626             // timestamp for default must be expressed in AV_TIME_BASE units
01627             ts_adj = av_rescale(target_ts,
01628                                 st->time_base.den,
01629                                 AV_TIME_BASE * (int64_t)st->time_base.num);
01630         } else {
01631             ts_adj = target_ts;
01632             stream_index_gen_search = stream_index;
01633         }
01634         pos = av_gen_search(s, stream_index_gen_search, ts_adj,
01635                             0, INT64_MAX, -1,
01636                             AV_NOPTS_VALUE,
01637                             AV_NOPTS_VALUE,
01638                             flags, &ts_ret, mpegts_get_pcr);
01639         if (pos < 0) {
01640             ff_restore_parser_state(s, backup);
01641             return -1;
01642         }
01643     }
01644 
01645     // search for actual matching keyframe/starting position for all streams
01646     if (ff_gen_syncpoint_search(s, stream_index, pos,
01647                                 min_ts, target_ts, max_ts,
01648                                 flags) < 0) {
01649         ff_restore_parser_state(s, backup);
01650         return -1;
01651     }
01652 
01653     ff_free_parser_state(s, backup);
01654     return 0;
01655 }
01656 
01657 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
01658 {
01659     int ret;
01660     if (flags & AVSEEK_FLAG_BACKWARD) {
01661         flags &= ~AVSEEK_FLAG_BACKWARD;
01662         ret = read_seek2(s, stream_index, INT64_MIN, target_ts, target_ts, flags);
01663         if (ret < 0)
01664             // for compatibility reasons, seek to the best-fitting timestamp
01665             ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
01666     } else {
01667         ret = read_seek2(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
01668         if (ret < 0)
01669             // for compatibility reasons, seek to the best-fitting timestamp
01670             ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
01671     }
01672     return ret;
01673 }
01674 
01675 #else
01676 
01677 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
01678     MpegTSContext *ts = s->priv_data;
01679     uint8_t buf[TS_PACKET_SIZE];
01680     int64_t pos;
01681 
01682     if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
01683         return -1;
01684 
01685     pos= url_ftell(s->pb);
01686 
01687     for(;;) {
01688         url_fseek(s->pb, pos, SEEK_SET);
01689         if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
01690             return -1;
01691 //        pid = AV_RB16(buf + 1) & 0x1fff;
01692         if(buf[1] & 0x40) break;
01693         pos += ts->raw_packet_size;
01694     }
01695     url_fseek(s->pb, pos, SEEK_SET);
01696 
01697     return 0;
01698 }
01699 
01700 #endif
01701 
01702 /**************************************************************/
01703 /* parsing functions - called from other demuxers such as RTP */
01704 
01705 MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
01706 {
01707     MpegTSContext *ts;
01708 
01709     ts = av_mallocz(sizeof(MpegTSContext));
01710     if (!ts)
01711         return NULL;
01712     /* no stream case, currently used by RTP */
01713     ts->raw_packet_size = TS_PACKET_SIZE;
01714     ts->stream = s;
01715     ts->auto_guess = 1;
01716     return ts;
01717 }
01718 
01719 /* return the consumed length if a packet was output, or -1 if no
01720    packet is output */
01721 int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
01722                         const uint8_t *buf, int len)
01723 {
01724     int len1;
01725 
01726     len1 = len;
01727     ts->pkt = pkt;
01728     ts->stop_parse = 0;
01729     for(;;) {
01730         if (ts->stop_parse>0)
01731             break;
01732         if (len < TS_PACKET_SIZE)
01733             return -1;
01734         if (buf[0] != 0x47) {
01735             buf++;
01736             len--;
01737         } else {
01738             handle_packet(ts, buf);
01739             buf += TS_PACKET_SIZE;
01740             len -= TS_PACKET_SIZE;
01741         }
01742     }
01743     return len1 - len;
01744 }
01745 
01746 void ff_mpegts_parse_close(MpegTSContext *ts)
01747 {
01748     int i;
01749 
01750     for(i=0;i<NB_PID_MAX;i++)
01751         av_free(ts->pids[i]);
01752     av_free(ts);
01753 }
01754 
01755 AVInputFormat mpegts_demuxer = {
01756     "mpegts",
01757     NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
01758     sizeof(MpegTSContext),
01759     mpegts_probe,
01760     mpegts_read_header,
01761     mpegts_read_packet,
01762     mpegts_read_close,
01763     read_seek,
01764     mpegts_get_pcr,
01765     .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
01766 #ifdef USE_SYNCPOINT_SEARCH
01767     .read_seek2 = read_seek2,
01768 #endif
01769 };
01770 
01771 AVInputFormat mpegtsraw_demuxer = {
01772     "mpegtsraw",
01773     NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
01774     sizeof(MpegTSContext),
01775     NULL,
01776     mpegts_read_header,
01777     mpegts_raw_read_packet,
01778     mpegts_read_close,
01779     read_seek,
01780     mpegts_get_pcr,
01781     .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
01782 #ifdef USE_SYNCPOINT_SEARCH
01783     .read_seek2 = read_seek2,
01784 #endif
01785 };

Generated on Fri Sep 16 2011 17:17:49 for FFmpeg by  doxygen 1.7.1